|
| 1 | +import { pluginLog } from "./log-bridge.js"; |
| 2 | + |
| 3 | +// eslint-disable-next-line no-control-regex |
| 4 | +const ANSI_PATTERN = /\x1b\[[0-9;]*m/g; |
| 5 | + |
| 6 | +function stripAnsi(input: string): string { |
| 7 | + return input.replace(ANSI_PATTERN, ""); |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * `@cursor/sdk`'s bundled local-exec runtime formats its "rules"/"skills" |
| 12 | + * loading diagnostics (context logger `local-exec:cursor-rules`) into one |
| 13 | + * preformatted string and writes it straight to `console.log` — there is no |
| 14 | + * public logger hook to redirect it instead. Observed shapes (colors |
| 15 | + * stripped): |
| 16 | + * |
| 17 | + * 16:05:53.036 INFO LocalCursorRulesService load completed meta={durationMs: 89, ruleCount: 1} |
| 18 | + * 16:05:53.036 INFO AgentSkillsCursorRulesService load completed meta={durationMs: 86, ruleCount: 18, skillCount: 18} |
| 19 | + * 16:05:53.036 INFO CursorPluginsAgentSkillsService load completed meta={durationMs: 12, ruleCount: 2, skillCount: 0} |
| 20 | + * |
| 21 | + * The context path (`ctx=...`) is only present in some builds/configs. |
| 22 | + */ |
| 23 | +const RULE_LOAD_PATTERN = |
| 24 | + /^\d{2}:\d{2}:\d{2}\.\d{3}\s+INFO\s+(LocalCursorRulesService|AgentSkillsCursorRulesService|CursorPluginsAgentSkillsService) load completed(?:\s+ctx=\S+)?\s+meta=\{([^}]*)\}\s*$/; |
| 25 | + |
| 26 | +/** Parses the `meta={key: value, ...}` tail into a plain numeric object. */ |
| 27 | +export function parseCursorLogMeta(raw: string): Record<string, number> { |
| 28 | + const out: Record<string, number> = {}; |
| 29 | + for (const part of raw.split(",")) { |
| 30 | + const [key, value] = part.split(":").map((s) => s.trim()); |
| 31 | + if (!key || value === undefined) continue; |
| 32 | + const num = Number(value); |
| 33 | + if (Number.isFinite(num)) out[key] = num; |
| 34 | + } |
| 35 | + return out; |
| 36 | +} |
| 37 | + |
| 38 | +export interface ParsedCursorRuleLog { |
| 39 | + service: string; |
| 40 | + meta: Record<string, number>; |
| 41 | +} |
| 42 | + |
| 43 | +/** Matches one line against the known Cursor rules/skills load-completion shape. */ |
| 44 | +export function parseCursorRuleLoadLine(line: string): ParsedCursorRuleLog | undefined { |
| 45 | + const match = RULE_LOAD_PATTERN.exec(stripAnsi(line)); |
| 46 | + if (!match) return undefined; |
| 47 | + const [, service, meta] = match; |
| 48 | + if (!service) return undefined; |
| 49 | + return { service, meta: parseCursorLogMeta(meta ?? "") }; |
| 50 | +} |
| 51 | + |
| 52 | +let installed = false; |
| 53 | +let original: typeof console.log | undefined; |
| 54 | + |
| 55 | +/** |
| 56 | + * Installs a narrowly-scoped `console.log` interceptor that recognizes only |
| 57 | + * the known Cursor rules/skills "load completed" messages (see |
| 58 | + * {@link parseCursorRuleLoadLine}) and re-emits them as structured opencode |
| 59 | + * logs via {@link pluginLog}. Every other `console.log` call — including |
| 60 | + * anything else the SDK or the host process writes — passes through |
| 61 | + * unchanged. |
| 62 | + * |
| 63 | + * Only relevant to the in-process transport, where the SDK runs inside this |
| 64 | + * process and writes directly to the shared global `console`. The sidecar |
| 65 | + * transport intercepts the same messages in the child process instead (see |
| 66 | + * `src/sidecar/agent-host.mjs`) and forwards them over the JSONL protocol. |
| 67 | + * |
| 68 | + * Idempotent: safe to call on every agent creation. |
| 69 | + */ |
| 70 | +export function installCursorLogInterceptor(): void { |
| 71 | + if (installed) return; |
| 72 | + original = console.log.bind(console); |
| 73 | + const passthrough = original; |
| 74 | + console.log = (...args: unknown[]) => { |
| 75 | + if (args.length === 1 && typeof args[0] === "string") { |
| 76 | + const parsed = parseCursorRuleLoadLine(args[0]); |
| 77 | + if (parsed) { |
| 78 | + pluginLog("info", `${parsed.service} load completed`, parsed.meta); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + passthrough(...(args as Parameters<typeof console.log>)); |
| 83 | + }; |
| 84 | + installed = true; |
| 85 | +} |
| 86 | + |
| 87 | +/** Test hook. */ |
| 88 | +export function resetCursorLogInterceptor(): void { |
| 89 | + if (original) console.log = original; |
| 90 | + original = undefined; |
| 91 | + installed = false; |
| 92 | +} |
0 commit comments