@posthog/agent 2.1.111 → 2.1.113
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/claude/conversion/tool-use-to-acp.js +20 -1
- package/dist/adapters/claude/conversion/tool-use-to-acp.js.map +1 -1
- package/dist/adapters/claude/permissions/permission-options.js +0 -4
- package/dist/adapters/claude/permissions/permission-options.js.map +1 -1
- package/dist/adapters/claude/tools.js +0 -2
- package/dist/adapters/claude/tools.js.map +1 -1
- package/dist/agent.js +93 -81
- package/dist/agent.js.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/posthog-api.js +1 -2
- package/dist/posthog-api.js.map +1 -1
- package/dist/server/agent-server.js +97 -85
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +97 -85
- package/dist/server/bin.cjs.map +1 -1
- package/package.json +1 -2
- package/src/adapters/claude/claude-agent.ts +4 -8
- package/src/adapters/claude/conversion/tool-use-to-acp.ts +22 -1
- package/src/adapters/claude/mcp/tool-metadata.ts +65 -71
|
@@ -88,6 +88,12 @@ var Logger = class _Logger {
|
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
+
// src/adapters/claude/mcp/tool-metadata.ts
|
|
92
|
+
var mcpToolMetadataCache = /* @__PURE__ */ new Map();
|
|
93
|
+
function getMcpToolMetadata(toolName) {
|
|
94
|
+
return mcpToolMetadataCache.get(toolName);
|
|
95
|
+
}
|
|
96
|
+
|
|
91
97
|
// src/adapters/claude/conversion/tool-use-to-acp.ts
|
|
92
98
|
var SYSTEM_REMINDER = `
|
|
93
99
|
|
|
@@ -418,14 +424,27 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
|
|
|
418
424
|
${output}\`\`\``).build()
|
|
419
425
|
};
|
|
420
426
|
}
|
|
421
|
-
default:
|
|
427
|
+
default: {
|
|
428
|
+
if (name?.startsWith("mcp__")) {
|
|
429
|
+
return mcpToolInfo(name, input);
|
|
430
|
+
}
|
|
422
431
|
return {
|
|
423
432
|
title: name || "Unknown Tool",
|
|
424
433
|
kind: "other",
|
|
425
434
|
content: []
|
|
426
435
|
};
|
|
436
|
+
}
|
|
427
437
|
}
|
|
428
438
|
}
|
|
439
|
+
function mcpToolInfo(name, _input) {
|
|
440
|
+
const metadata = getMcpToolMetadata(name);
|
|
441
|
+
const title = metadata?.name ?? (name.split("__").slice(2).join("__") || name);
|
|
442
|
+
return {
|
|
443
|
+
title,
|
|
444
|
+
kind: "other",
|
|
445
|
+
content: []
|
|
446
|
+
};
|
|
447
|
+
}
|
|
429
448
|
function toolUpdateFromToolResult(toolResult, toolUse) {
|
|
430
449
|
switch (toolUse?.name) {
|
|
431
450
|
case "Read":
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/utils/acp-content.ts","../../../../src/utils/logger.ts","../../../../src/adapters/claude/conversion/tool-use-to-acp.ts"],"sourcesContent":["import type { ContentBlock, ToolCallContent } from \"@agentclientprotocol/sdk\";\n\nexport function text(value: string): ContentBlock {\n return { type: \"text\", text: value };\n}\n\nexport function image(\n data: string,\n mimeType: string,\n uri?: string,\n): ContentBlock {\n return { type: \"image\", data, mimeType, uri };\n}\n\nexport function resourceLink(\n uri: string,\n name: string,\n options?: {\n mimeType?: string;\n title?: string;\n description?: string;\n size?: bigint;\n },\n): ContentBlock {\n return {\n type: \"resource_link\",\n uri,\n name,\n ...options,\n };\n}\n\nclass ToolContentBuilder {\n private items: ToolCallContent[] = [];\n\n text(value: string): this {\n this.items.push({ type: \"content\", content: text(value) });\n return this;\n }\n\n image(data: string, mimeType: string, uri?: string): this {\n this.items.push({ type: \"content\", content: image(data, mimeType, uri) });\n return this;\n }\n\n diff(path: string, oldText: string | null, newText: string): this {\n this.items.push({ type: \"diff\", path, oldText, newText });\n return this;\n }\n\n build(): ToolCallContent[] {\n return this.items;\n }\n}\n\nexport function toolContent(): ToolContentBuilder {\n return new ToolContentBuilder();\n}\n","import type { LogLevel as LogLevelType, OnLogCallback } from \"../types.js\";\n\nexport interface LoggerConfig {\n debug?: boolean;\n prefix?: string;\n scope?: string;\n onLog?: OnLogCallback;\n}\n\nexport class Logger {\n private debugEnabled: boolean;\n private prefix: string;\n private scope: string;\n private onLog?: OnLogCallback;\n\n constructor(config: LoggerConfig = {}) {\n this.debugEnabled = config.debug ?? false;\n this.prefix = config.prefix ?? \"[PostHog Agent]\";\n this.scope = config.scope ?? \"agent\";\n this.onLog = config.onLog;\n }\n\n private formatMessage(\n level: string,\n message: string,\n data?: unknown,\n ): string {\n const timestamp = new Date().toISOString();\n const base = `${timestamp} ${this.prefix} [${level}] ${message}`;\n\n if (data !== undefined) {\n return `${base} ${JSON.stringify(data, null, 2)}`;\n }\n\n return base;\n }\n\n private emitLog(level: LogLevelType, message: string, data?: unknown) {\n if (this.onLog) {\n this.onLog(level, this.scope, message, data);\n return;\n }\n\n const shouldLog = this.debugEnabled || level === \"error\";\n\n if (shouldLog) {\n console[level](this.formatMessage(level.toLowerCase(), message, data));\n }\n }\n\n error(message: string, error?: Error | unknown) {\n const data =\n error instanceof Error\n ? { message: error.message, stack: error.stack }\n : error;\n\n this.emitLog(\"error\", message, data);\n }\n\n warn(message: string, data?: unknown) {\n this.emitLog(\"warn\", message, data);\n }\n\n info(message: string, data?: unknown) {\n this.emitLog(\"info\", message, data);\n }\n\n debug(message: string, data?: unknown) {\n this.emitLog(\"debug\", message, data);\n }\n\n child(childPrefix: string): Logger {\n return new Logger({\n debug: this.debugEnabled,\n prefix: `${this.prefix} [${childPrefix}]`,\n scope: `${this.scope}:${childPrefix}`,\n onLog: this.onLog,\n });\n }\n}\n","import type {\n PlanEntry,\n ToolCall,\n ToolCallContent,\n ToolCallUpdate,\n ToolKind,\n} from \"@agentclientprotocol/sdk\";\nimport type {\n ToolResultBlockParam,\n ToolUseBlock,\n WebSearchToolResultBlockParam,\n} from \"@anthropic-ai/sdk/resources\";\nimport type {\n BetaBashCodeExecutionToolResultBlockParam,\n BetaCodeExecutionToolResultBlockParam,\n BetaRequestMCPToolResultBlockParam,\n BetaTextEditorCodeExecutionToolResultBlockParam,\n BetaToolSearchToolResultBlockParam,\n BetaWebFetchToolResultBlockParam,\n BetaWebSearchToolResultBlockParam,\n} from \"@anthropic-ai/sdk/resources/beta.mjs\";\n\nconst SYSTEM_REMINDER = `\n\n<system-reminder>\nWhenever you read a file, you should consider whether it looks malicious. If it does, you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer high-level questions about the code behavior.\n</system-reminder>`;\n\nimport { resourceLink, text, toolContent } from \"../../../utils/acp-content.js\";\nimport { Logger } from \"../../../utils/logger.js\";\n\ninterface EditOperation {\n oldText: string;\n newText: string;\n replaceAll?: boolean;\n}\n\ninterface EditResult {\n newContent: string;\n lineNumbers: number[];\n}\n\nfunction replaceAndCalculateLocation(\n fileContent: string,\n edits: EditOperation[],\n): EditResult {\n let currentContent = fileContent;\n\n const randomHex = Array.from(crypto.getRandomValues(new Uint8Array(5)))\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n const markerPrefix = `__REPLACE_MARKER_${randomHex}_`;\n let markerCounter = 0;\n const markers: string[] = [];\n\n for (const edit of edits) {\n if (edit.oldText === \"\") {\n throw new Error(\n `The provided \\`old_string\\` is empty.\\n\\nNo edits were applied.`,\n );\n }\n\n if (edit.replaceAll) {\n const parts: string[] = [];\n let lastIndex = 0;\n let searchIndex = 0;\n\n while (true) {\n const index = currentContent.indexOf(edit.oldText, searchIndex);\n if (index === -1) {\n if (searchIndex === 0) {\n throw new Error(\n `The provided \\`old_string\\` does not appear in the file: \"${edit.oldText}\".\\n\\nNo edits were applied.`,\n );\n }\n break;\n }\n\n parts.push(currentContent.substring(lastIndex, index));\n\n const marker = `${markerPrefix}${markerCounter++}__`;\n markers.push(marker);\n parts.push(marker + edit.newText);\n\n lastIndex = index + edit.oldText.length;\n searchIndex = lastIndex;\n }\n\n parts.push(currentContent.substring(lastIndex));\n currentContent = parts.join(\"\");\n } else {\n const index = currentContent.indexOf(edit.oldText);\n if (index === -1) {\n throw new Error(\n `The provided \\`old_string\\` does not appear in the file: \"${edit.oldText}\".\\n\\nNo edits were applied.`,\n );\n } else {\n const marker = `${markerPrefix}${markerCounter++}__`;\n markers.push(marker);\n currentContent =\n currentContent.substring(0, index) +\n marker +\n edit.newText +\n currentContent.substring(index + edit.oldText.length);\n }\n }\n }\n\n const lineNumbers: number[] = [];\n for (const marker of markers) {\n const index = currentContent.indexOf(marker);\n if (index !== -1) {\n const lineNumber = Math.max(\n 0,\n currentContent.substring(0, index).split(/\\r\\n|\\r|\\n/).length - 1,\n );\n lineNumbers.push(lineNumber);\n }\n }\n\n let finalContent = currentContent;\n for (const marker of markers) {\n finalContent = finalContent.replace(marker, \"\");\n }\n\n const uniqueLineNumbers = [...new Set(lineNumbers)].sort();\n\n return { newContent: finalContent, lineNumbers: uniqueLineNumbers };\n}\n\ntype ToolInfo = Pick<ToolCall, \"title\" | \"kind\" | \"content\" | \"locations\">;\n\nexport function toolInfoFromToolUse(\n toolUse: Pick<ToolUseBlock, \"name\" | \"input\">,\n cachedFileContent: { [key: string]: string },\n logger: Logger = new Logger({ debug: false, prefix: \"[ClaudeTools]\" }),\n): ToolInfo {\n const name = toolUse.name;\n const input = toolUse.input as Record<string, unknown> | undefined;\n\n switch (name) {\n case \"Task\":\n return {\n title: input?.description ? String(input.description) : \"Task\",\n kind: \"think\",\n content: input?.prompt\n ? toolContent().text(String(input.prompt)).build()\n : [],\n };\n\n case \"NotebookRead\":\n return {\n title: input?.notebook_path\n ? `Read Notebook ${String(input.notebook_path)}`\n : \"Read Notebook\",\n kind: \"read\",\n content: [],\n locations: input?.notebook_path\n ? [{ path: String(input.notebook_path) }]\n : [],\n };\n\n case \"NotebookEdit\":\n return {\n title: input?.notebook_path\n ? `Edit Notebook ${String(input.notebook_path)}`\n : \"Edit Notebook\",\n kind: \"edit\",\n content: input?.new_source\n ? toolContent().text(String(input.new_source)).build()\n : [],\n locations: input?.notebook_path\n ? [{ path: String(input.notebook_path) }]\n : [],\n };\n\n case \"Bash\":\n return {\n title: input?.description\n ? String(input.description)\n : \"Execute command\",\n kind: \"execute\",\n content: input?.command\n ? toolContent().text(String(input.command)).build()\n : [],\n };\n\n case \"BashOutput\":\n return {\n title: \"Tail Logs\",\n kind: \"execute\",\n content: [],\n };\n\n case \"KillShell\":\n return {\n title: \"Kill Process\",\n kind: \"execute\",\n content: [],\n };\n\n case \"Read\": {\n let limit = \"\";\n const inputLimit = input?.limit as number | undefined;\n const inputOffset = (input?.offset as number | undefined) ?? 0;\n if (inputLimit) {\n limit = ` (${inputOffset + 1} - ${inputOffset + inputLimit})`;\n } else if (inputOffset) {\n limit = ` (from line ${inputOffset + 1})`;\n }\n return {\n title: `Read ${input?.file_path ? String(input.file_path) : \"File\"}${limit}`,\n kind: \"read\",\n locations: input?.file_path\n ? [\n {\n path: String(input.file_path),\n line: inputOffset,\n },\n ]\n : [],\n content: [],\n };\n }\n\n case \"LS\":\n return {\n title: `List the ${input?.path ? `\\`${String(input.path)}\\`` : \"current\"} directory's contents`,\n kind: \"search\",\n content: [],\n locations: [],\n };\n\n case \"Edit\": {\n const path = input?.file_path ? String(input.file_path) : undefined;\n let oldText = input?.old_string ? String(input.old_string) : null;\n let newText = input?.new_string ? String(input.new_string) : \"\";\n let affectedLines: number[] = [];\n\n if (path && oldText) {\n try {\n const oldContent = cachedFileContent[path] || \"\";\n const newContent = replaceAndCalculateLocation(oldContent, [\n {\n oldText,\n newText,\n replaceAll: false,\n },\n ]);\n oldText = oldContent;\n newText = newContent.newContent;\n affectedLines = newContent.lineNumbers;\n } catch (e) {\n logger.error(\"Failed to edit file\", e);\n }\n }\n return {\n title: path ? `Edit \\`${path}\\`` : \"Edit\",\n kind: \"edit\",\n content:\n input && path\n ? [\n {\n type: \"diff\",\n path,\n oldText,\n newText,\n },\n ]\n : [],\n locations: path\n ? affectedLines.length > 0\n ? affectedLines.map((line) => ({ line, path }))\n : [{ path }]\n : [],\n };\n }\n\n case \"Write\": {\n let contentResult: ToolCallContent[] = [];\n const filePath = input?.file_path ? String(input.file_path) : undefined;\n const contentStr = input?.content ? String(input.content) : undefined;\n if (filePath) {\n contentResult = toolContent()\n .diff(filePath, null, contentStr ?? \"\")\n .build();\n } else if (contentStr) {\n contentResult = toolContent().text(contentStr).build();\n }\n return {\n title: filePath ? `Write ${filePath}` : \"Write\",\n kind: \"edit\",\n content: contentResult,\n locations: filePath ? [{ path: filePath }] : [],\n };\n }\n\n case \"Glob\": {\n let label = \"Find\";\n const pathStr = input?.path ? String(input.path) : undefined;\n if (pathStr) {\n label += ` \"${pathStr}\"`;\n }\n if (input?.pattern) {\n label += ` \"${String(input.pattern)}\"`;\n }\n return {\n title: label,\n kind: \"search\",\n content: [],\n locations: pathStr ? [{ path: pathStr }] : [],\n };\n }\n\n case \"Grep\": {\n let label = \"grep\";\n\n if (input?.[\"-i\"]) {\n label += \" -i\";\n }\n if (input?.[\"-n\"]) {\n label += \" -n\";\n }\n\n if (input?.[\"-A\"] !== undefined) {\n label += ` -A ${input[\"-A\"]}`;\n }\n if (input?.[\"-B\"] !== undefined) {\n label += ` -B ${input[\"-B\"]}`;\n }\n if (input?.[\"-C\"] !== undefined) {\n label += ` -C ${input[\"-C\"]}`;\n }\n\n if (input?.output_mode) {\n switch (input.output_mode) {\n case \"FilesWithMatches\":\n label += \" -l\";\n break;\n case \"Count\":\n label += \" -c\";\n break;\n default:\n break;\n }\n }\n\n if (input?.head_limit !== undefined) {\n label += ` | head -${input.head_limit}`;\n }\n\n if (input?.glob) {\n label += ` --include=\"${String(input.glob)}\"`;\n }\n\n if (input?.type) {\n label += ` --type=${String(input.type)}`;\n }\n\n if (input?.multiline) {\n label += \" -P\";\n }\n\n label += ` \"${input?.pattern ? String(input.pattern) : \"\"}\"`;\n\n if (input?.path) {\n label += ` ${String(input.path)}`;\n }\n\n return {\n title: label,\n kind: \"search\",\n content: [],\n };\n }\n\n case \"WebFetch\":\n return {\n title: \"Fetch\",\n kind: \"fetch\",\n content: input?.url\n ? [\n {\n type: \"content\",\n content: resourceLink(String(input.url), String(input.url), {\n description: input?.prompt ? String(input.prompt) : undefined,\n }),\n },\n ]\n : [],\n };\n\n case \"WebSearch\": {\n let label = `\"${input?.query ? String(input.query) : \"\"}\"`;\n const allowedDomains = input?.allowed_domains as string[] | undefined;\n const blockedDomains = input?.blocked_domains as string[] | undefined;\n\n if (allowedDomains && allowedDomains.length > 0) {\n label += ` (allowed: ${allowedDomains.join(\", \")})`;\n }\n\n if (blockedDomains && blockedDomains.length > 0) {\n label += ` (blocked: ${blockedDomains.join(\", \")})`;\n }\n\n return {\n title: label,\n kind: \"fetch\",\n content: [],\n };\n }\n\n case \"TodoWrite\":\n return {\n title: Array.isArray(input?.todos)\n ? `Update TODOs: ${input.todos.map((todo: { content?: string }) => todo.content).join(\", \")}`\n : \"Update TODOs\",\n kind: \"think\",\n content: [],\n };\n\n case \"ExitPlanMode\":\n return {\n title: \"Ready to code?\",\n kind: \"switch_mode\",\n content: input?.plan\n ? toolContent().text(String(input.plan)).build()\n : [],\n };\n\n case \"AskUserQuestion\": {\n const questions = input?.questions as\n | Array<{ question?: string }>\n | undefined;\n return {\n title: questions?.[0]?.question || \"Question\",\n kind: \"other\" as ToolKind,\n content: questions\n ? toolContent()\n .text(JSON.stringify(questions, null, 2))\n .build()\n : [],\n };\n }\n\n case \"Other\": {\n let output: string;\n try {\n output = JSON.stringify(input, null, 2);\n } catch {\n output = typeof input === \"string\" ? input : \"{}\";\n }\n return {\n title: name || \"Unknown Tool\",\n kind: \"other\",\n content: toolContent().text(`\\`\\`\\`json\\n${output}\\`\\`\\``).build(),\n };\n }\n\n default:\n return {\n title: name || \"Unknown Tool\",\n kind: \"other\",\n content: [],\n };\n }\n}\n\nexport function toolUpdateFromToolResult(\n toolResult:\n | ToolResultBlockParam\n | BetaWebSearchToolResultBlockParam\n | BetaWebFetchToolResultBlockParam\n | WebSearchToolResultBlockParam\n | BetaCodeExecutionToolResultBlockParam\n | BetaBashCodeExecutionToolResultBlockParam\n | BetaTextEditorCodeExecutionToolResultBlockParam\n | BetaRequestMCPToolResultBlockParam\n | BetaToolSearchToolResultBlockParam,\n toolUse: Pick<ToolUseBlock, \"name\" | \"input\"> | undefined,\n): Pick<ToolCallUpdate, \"title\" | \"content\" | \"locations\"> {\n switch (toolUse?.name) {\n case \"Read\":\n if (Array.isArray(toolResult.content) && toolResult.content.length > 0) {\n return {\n content: toolResult.content.map((item) => {\n const itemObj = item as { type?: string; text?: string };\n if (itemObj.type === \"text\") {\n return {\n type: \"content\" as const,\n content: text(\n markdownEscape(\n (itemObj.text ?? \"\").replace(SYSTEM_REMINDER, \"\"),\n ),\n ),\n };\n }\n return {\n type: \"content\" as const,\n content: item as { type: \"text\"; text: string },\n };\n }),\n };\n } else if (\n typeof toolResult.content === \"string\" &&\n toolResult.content.length > 0\n ) {\n return {\n content: toolContent()\n .text(\n markdownEscape(toolResult.content.replace(SYSTEM_REMINDER, \"\")),\n )\n .build(),\n };\n }\n return {};\n\n case \"Bash\": {\n return toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n }\n case \"Edit\":\n case \"Write\": {\n if (\n \"is_error\" in toolResult &&\n toolResult.is_error &&\n toolResult.content &&\n toolResult.content.length > 0\n ) {\n return toAcpContentUpdate(toolResult.content, true);\n }\n return {};\n }\n\n case \"ExitPlanMode\": {\n return { title: \"Exited Plan Mode\" };\n }\n case \"AskUserQuestion\": {\n const content = toolResult.content;\n if (Array.isArray(content) && content.length > 0) {\n const firstItem = content[0];\n if (\n typeof firstItem === \"object\" &&\n firstItem !== null &&\n \"text\" in firstItem\n ) {\n return {\n title: \"Answer received\",\n content: toolContent().text(String(firstItem.text)).build(),\n };\n }\n }\n return { title: \"Question answered\" };\n }\n case \"WebFetch\": {\n const input = toolUse?.input as Record<string, unknown> | undefined;\n const url = input?.url ? String(input.url) : \"\";\n const prompt = input?.prompt ? String(input.prompt) : undefined;\n\n const resultContent = toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n\n const content: ToolCallContent[] = [];\n if (url) {\n content.push({\n type: \"content\",\n content: resourceLink(url, url, {\n description: prompt,\n }),\n });\n }\n if (resultContent.content) {\n content.push(...resultContent.content);\n }\n\n return { content };\n }\n default: {\n return toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n }\n }\n}\n\nfunction toAcpContentUpdate(\n content: unknown,\n isError: boolean = false,\n): Pick<ToolCallUpdate, \"content\"> {\n if (Array.isArray(content) && content.length > 0) {\n return {\n content: content.map((item) => {\n const itemObj = item as { type?: string; text?: string };\n if (isError && itemObj.type === \"text\") {\n return {\n type: \"content\" as const,\n content: text(`\\`\\`\\`\\n${itemObj.text ?? \"\"}\\n\\`\\`\\``),\n };\n }\n return {\n type: \"content\" as const,\n content: item as { type: \"text\"; text: string },\n };\n }),\n };\n } else if (typeof content === \"string\" && content.length > 0) {\n return {\n content: toolContent()\n .text(isError ? `\\`\\`\\`\\n${content}\\n\\`\\`\\`` : content)\n .build(),\n };\n }\n return {};\n}\n\nexport type ClaudePlanEntry = {\n content: string;\n status: \"pending\" | \"in_progress\" | \"completed\";\n activeForm: string;\n};\n\nexport function planEntries(input: { todos: ClaudePlanEntry[] }): PlanEntry[] {\n return input.todos.map((input) => ({\n content: input.content,\n status: input.status,\n priority: \"medium\",\n }));\n}\n\nfunction markdownEscape(text: string): string {\n let escapedText = \"```\";\n for (const [m] of text.matchAll(/^```+/gm)) {\n while (m.length >= escapedText.length) {\n escapedText += \"`\";\n }\n }\n return `${escapedText}\\n${text}${text.endsWith(\"\\n\") ? \"\" : \"\\n\"}${escapedText}`;\n}\n"],"mappings":";AAEO,SAAS,KAAK,OAA6B;AAChD,SAAO,EAAE,MAAM,QAAQ,MAAM,MAAM;AACrC;AAEO,SAAS,MACd,MACA,UACA,KACc;AACd,SAA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AAA,IACf,QAAQA,OAAM;AAAA,IACd,UAAU;AAAA,EACZ,EAAE;AACJ;AAEA,SAAS,eAAeC,OAAsB;AAC5C,MAAI,cAAc;AAClB,aAAW,CAAC,CAAC,KAAKA,MAAK,SAAS,SAAS,GAAG;AAC1C,WAAO,EAAE,UAAU,YAAY,QAAQ;AACrC,qBAAe;AAAA,IACjB;AAAA,EACF;AACA,SAAO,GAAG,WAAW;AAAA,EAAKA,KAAI,GAAGA,MAAK,SAAS,IAAI,IAAI,KAAK,IAAI,GAAG,WAAW;AAChF;","names":["input","text"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/acp-content.ts","../../../../src/utils/logger.ts","../../../../src/adapters/claude/mcp/tool-metadata.ts","../../../../src/adapters/claude/conversion/tool-use-to-acp.ts"],"sourcesContent":["import type { ContentBlock, ToolCallContent } from \"@agentclientprotocol/sdk\";\n\nexport function text(value: string): ContentBlock {\n return { type: \"text\", text: value };\n}\n\nexport function image(\n data: string,\n mimeType: string,\n uri?: string,\n): ContentBlock {\n return { type: \"image\", data, mimeType, uri };\n}\n\nexport function resourceLink(\n uri: string,\n name: string,\n options?: {\n mimeType?: string;\n title?: string;\n description?: string;\n size?: bigint;\n },\n): ContentBlock {\n return {\n type: \"resource_link\",\n uri,\n name,\n ...options,\n };\n}\n\nclass ToolContentBuilder {\n private items: ToolCallContent[] = [];\n\n text(value: string): this {\n this.items.push({ type: \"content\", content: text(value) });\n return this;\n }\n\n image(data: string, mimeType: string, uri?: string): this {\n this.items.push({ type: \"content\", content: image(data, mimeType, uri) });\n return this;\n }\n\n diff(path: string, oldText: string | null, newText: string): this {\n this.items.push({ type: \"diff\", path, oldText, newText });\n return this;\n }\n\n build(): ToolCallContent[] {\n return this.items;\n }\n}\n\nexport function toolContent(): ToolContentBuilder {\n return new ToolContentBuilder();\n}\n","import type { LogLevel as LogLevelType, OnLogCallback } from \"../types.js\";\n\nexport interface LoggerConfig {\n debug?: boolean;\n prefix?: string;\n scope?: string;\n onLog?: OnLogCallback;\n}\n\nexport class Logger {\n private debugEnabled: boolean;\n private prefix: string;\n private scope: string;\n private onLog?: OnLogCallback;\n\n constructor(config: LoggerConfig = {}) {\n this.debugEnabled = config.debug ?? false;\n this.prefix = config.prefix ?? \"[PostHog Agent]\";\n this.scope = config.scope ?? \"agent\";\n this.onLog = config.onLog;\n }\n\n private formatMessage(\n level: string,\n message: string,\n data?: unknown,\n ): string {\n const timestamp = new Date().toISOString();\n const base = `${timestamp} ${this.prefix} [${level}] ${message}`;\n\n if (data !== undefined) {\n return `${base} ${JSON.stringify(data, null, 2)}`;\n }\n\n return base;\n }\n\n private emitLog(level: LogLevelType, message: string, data?: unknown) {\n if (this.onLog) {\n this.onLog(level, this.scope, message, data);\n return;\n }\n\n const shouldLog = this.debugEnabled || level === \"error\";\n\n if (shouldLog) {\n console[level](this.formatMessage(level.toLowerCase(), message, data));\n }\n }\n\n error(message: string, error?: Error | unknown) {\n const data =\n error instanceof Error\n ? { message: error.message, stack: error.stack }\n : error;\n\n this.emitLog(\"error\", message, data);\n }\n\n warn(message: string, data?: unknown) {\n this.emitLog(\"warn\", message, data);\n }\n\n info(message: string, data?: unknown) {\n this.emitLog(\"info\", message, data);\n }\n\n debug(message: string, data?: unknown) {\n this.emitLog(\"debug\", message, data);\n }\n\n child(childPrefix: string): Logger {\n return new Logger({\n debug: this.debugEnabled,\n prefix: `${this.prefix} [${childPrefix}]`,\n scope: `${this.scope}:${childPrefix}`,\n onLog: this.onLog,\n });\n }\n}\n","import type { McpServerStatus, Query } from \"@anthropic-ai/claude-agent-sdk\";\nimport { Logger } from \"../../../utils/logger.js\";\n\nexport interface McpToolMetadata {\n readOnly: boolean;\n name: string;\n description?: string;\n}\n\nconst mcpToolMetadataCache: Map<string, McpToolMetadata> = new Map();\n\nconst PENDING_RETRY_INTERVAL_MS = 1_000;\nconst PENDING_MAX_RETRIES = 10;\n\nfunction buildToolKey(serverName: string, toolName: string): string {\n return `mcp__${serverName}__${toolName}`;\n}\n\nfunction delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport async function fetchMcpToolMetadata(\n q: Query,\n logger: Logger = new Logger({ debug: false, prefix: \"[McpToolMetadata]\" }),\n): Promise<void> {\n let retries = 0;\n\n while (retries <= PENDING_MAX_RETRIES) {\n let statuses: McpServerStatus[];\n try {\n statuses = await q.mcpServerStatus();\n } catch (error) {\n logger.error(\"Failed to fetch MCP server status\", {\n error: error instanceof Error ? error.message : String(error),\n });\n return;\n }\n\n const pendingServers = statuses.filter((s) => s.status === \"pending\");\n\n for (const server of statuses) {\n if (server.status !== \"connected\" || !server.tools) {\n continue;\n }\n\n let readOnlyCount = 0;\n for (const tool of server.tools) {\n const toolKey = buildToolKey(server.name, tool.name);\n const readOnly = tool.annotations?.readOnly === true;\n mcpToolMetadataCache.set(toolKey, {\n readOnly,\n name: tool.name,\n description: tool.description,\n });\n if (readOnly) readOnlyCount++;\n }\n\n logger.info(\"Fetched MCP tool metadata\", {\n serverName: server.name,\n toolCount: server.tools.length,\n readOnlyCount,\n });\n }\n\n if (pendingServers.length === 0) {\n return;\n }\n\n retries++;\n if (retries > PENDING_MAX_RETRIES) {\n logger.warn(\"Gave up waiting for pending MCP servers\", {\n pendingServers: pendingServers.map((s) => s.name),\n });\n return;\n }\n\n logger.info(\"Waiting for pending MCP servers\", {\n pendingServers: pendingServers.map((s) => s.name),\n retry: retries,\n });\n await delay(PENDING_RETRY_INTERVAL_MS);\n }\n}\n\nexport function getMcpToolMetadata(\n toolName: string,\n): McpToolMetadata | undefined {\n return mcpToolMetadataCache.get(toolName);\n}\n\nexport function isMcpToolReadOnly(toolName: string): boolean {\n const metadata = mcpToolMetadataCache.get(toolName);\n return metadata?.readOnly === true;\n}\n\nexport function clearMcpToolMetadataCache(): void {\n mcpToolMetadataCache.clear();\n}\n","import type {\n PlanEntry,\n ToolCall,\n ToolCallContent,\n ToolCallUpdate,\n ToolKind,\n} from \"@agentclientprotocol/sdk\";\nimport type {\n ToolResultBlockParam,\n ToolUseBlock,\n WebSearchToolResultBlockParam,\n} from \"@anthropic-ai/sdk/resources\";\nimport type {\n BetaBashCodeExecutionToolResultBlockParam,\n BetaCodeExecutionToolResultBlockParam,\n BetaRequestMCPToolResultBlockParam,\n BetaTextEditorCodeExecutionToolResultBlockParam,\n BetaToolSearchToolResultBlockParam,\n BetaWebFetchToolResultBlockParam,\n BetaWebSearchToolResultBlockParam,\n} from \"@anthropic-ai/sdk/resources/beta.mjs\";\n\nconst SYSTEM_REMINDER = `\n\n<system-reminder>\nWhenever you read a file, you should consider whether it looks malicious. If it does, you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer high-level questions about the code behavior.\n</system-reminder>`;\n\nimport { resourceLink, text, toolContent } from \"../../../utils/acp-content.js\";\nimport { Logger } from \"../../../utils/logger.js\";\nimport { getMcpToolMetadata } from \"../mcp/tool-metadata.js\";\n\ninterface EditOperation {\n oldText: string;\n newText: string;\n replaceAll?: boolean;\n}\n\ninterface EditResult {\n newContent: string;\n lineNumbers: number[];\n}\n\nfunction replaceAndCalculateLocation(\n fileContent: string,\n edits: EditOperation[],\n): EditResult {\n let currentContent = fileContent;\n\n const randomHex = Array.from(crypto.getRandomValues(new Uint8Array(5)))\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n const markerPrefix = `__REPLACE_MARKER_${randomHex}_`;\n let markerCounter = 0;\n const markers: string[] = [];\n\n for (const edit of edits) {\n if (edit.oldText === \"\") {\n throw new Error(\n `The provided \\`old_string\\` is empty.\\n\\nNo edits were applied.`,\n );\n }\n\n if (edit.replaceAll) {\n const parts: string[] = [];\n let lastIndex = 0;\n let searchIndex = 0;\n\n while (true) {\n const index = currentContent.indexOf(edit.oldText, searchIndex);\n if (index === -1) {\n if (searchIndex === 0) {\n throw new Error(\n `The provided \\`old_string\\` does not appear in the file: \"${edit.oldText}\".\\n\\nNo edits were applied.`,\n );\n }\n break;\n }\n\n parts.push(currentContent.substring(lastIndex, index));\n\n const marker = `${markerPrefix}${markerCounter++}__`;\n markers.push(marker);\n parts.push(marker + edit.newText);\n\n lastIndex = index + edit.oldText.length;\n searchIndex = lastIndex;\n }\n\n parts.push(currentContent.substring(lastIndex));\n currentContent = parts.join(\"\");\n } else {\n const index = currentContent.indexOf(edit.oldText);\n if (index === -1) {\n throw new Error(\n `The provided \\`old_string\\` does not appear in the file: \"${edit.oldText}\".\\n\\nNo edits were applied.`,\n );\n } else {\n const marker = `${markerPrefix}${markerCounter++}__`;\n markers.push(marker);\n currentContent =\n currentContent.substring(0, index) +\n marker +\n edit.newText +\n currentContent.substring(index + edit.oldText.length);\n }\n }\n }\n\n const lineNumbers: number[] = [];\n for (const marker of markers) {\n const index = currentContent.indexOf(marker);\n if (index !== -1) {\n const lineNumber = Math.max(\n 0,\n currentContent.substring(0, index).split(/\\r\\n|\\r|\\n/).length - 1,\n );\n lineNumbers.push(lineNumber);\n }\n }\n\n let finalContent = currentContent;\n for (const marker of markers) {\n finalContent = finalContent.replace(marker, \"\");\n }\n\n const uniqueLineNumbers = [...new Set(lineNumbers)].sort();\n\n return { newContent: finalContent, lineNumbers: uniqueLineNumbers };\n}\n\ntype ToolInfo = Pick<ToolCall, \"title\" | \"kind\" | \"content\" | \"locations\">;\n\nexport function toolInfoFromToolUse(\n toolUse: Pick<ToolUseBlock, \"name\" | \"input\">,\n cachedFileContent: { [key: string]: string },\n logger: Logger = new Logger({ debug: false, prefix: \"[ClaudeTools]\" }),\n): ToolInfo {\n const name = toolUse.name;\n const input = toolUse.input as Record<string, unknown> | undefined;\n\n switch (name) {\n case \"Task\":\n return {\n title: input?.description ? String(input.description) : \"Task\",\n kind: \"think\",\n content: input?.prompt\n ? toolContent().text(String(input.prompt)).build()\n : [],\n };\n\n case \"NotebookRead\":\n return {\n title: input?.notebook_path\n ? `Read Notebook ${String(input.notebook_path)}`\n : \"Read Notebook\",\n kind: \"read\",\n content: [],\n locations: input?.notebook_path\n ? [{ path: String(input.notebook_path) }]\n : [],\n };\n\n case \"NotebookEdit\":\n return {\n title: input?.notebook_path\n ? `Edit Notebook ${String(input.notebook_path)}`\n : \"Edit Notebook\",\n kind: \"edit\",\n content: input?.new_source\n ? toolContent().text(String(input.new_source)).build()\n : [],\n locations: input?.notebook_path\n ? [{ path: String(input.notebook_path) }]\n : [],\n };\n\n case \"Bash\":\n return {\n title: input?.description\n ? String(input.description)\n : \"Execute command\",\n kind: \"execute\",\n content: input?.command\n ? toolContent().text(String(input.command)).build()\n : [],\n };\n\n case \"BashOutput\":\n return {\n title: \"Tail Logs\",\n kind: \"execute\",\n content: [],\n };\n\n case \"KillShell\":\n return {\n title: \"Kill Process\",\n kind: \"execute\",\n content: [],\n };\n\n case \"Read\": {\n let limit = \"\";\n const inputLimit = input?.limit as number | undefined;\n const inputOffset = (input?.offset as number | undefined) ?? 0;\n if (inputLimit) {\n limit = ` (${inputOffset + 1} - ${inputOffset + inputLimit})`;\n } else if (inputOffset) {\n limit = ` (from line ${inputOffset + 1})`;\n }\n return {\n title: `Read ${input?.file_path ? String(input.file_path) : \"File\"}${limit}`,\n kind: \"read\",\n locations: input?.file_path\n ? [\n {\n path: String(input.file_path),\n line: inputOffset,\n },\n ]\n : [],\n content: [],\n };\n }\n\n case \"LS\":\n return {\n title: `List the ${input?.path ? `\\`${String(input.path)}\\`` : \"current\"} directory's contents`,\n kind: \"search\",\n content: [],\n locations: [],\n };\n\n case \"Edit\": {\n const path = input?.file_path ? String(input.file_path) : undefined;\n let oldText = input?.old_string ? String(input.old_string) : null;\n let newText = input?.new_string ? String(input.new_string) : \"\";\n let affectedLines: number[] = [];\n\n if (path && oldText) {\n try {\n const oldContent = cachedFileContent[path] || \"\";\n const newContent = replaceAndCalculateLocation(oldContent, [\n {\n oldText,\n newText,\n replaceAll: false,\n },\n ]);\n oldText = oldContent;\n newText = newContent.newContent;\n affectedLines = newContent.lineNumbers;\n } catch (e) {\n logger.error(\"Failed to edit file\", e);\n }\n }\n return {\n title: path ? `Edit \\`${path}\\`` : \"Edit\",\n kind: \"edit\",\n content:\n input && path\n ? [\n {\n type: \"diff\",\n path,\n oldText,\n newText,\n },\n ]\n : [],\n locations: path\n ? affectedLines.length > 0\n ? affectedLines.map((line) => ({ line, path }))\n : [{ path }]\n : [],\n };\n }\n\n case \"Write\": {\n let contentResult: ToolCallContent[] = [];\n const filePath = input?.file_path ? String(input.file_path) : undefined;\n const contentStr = input?.content ? String(input.content) : undefined;\n if (filePath) {\n contentResult = toolContent()\n .diff(filePath, null, contentStr ?? \"\")\n .build();\n } else if (contentStr) {\n contentResult = toolContent().text(contentStr).build();\n }\n return {\n title: filePath ? `Write ${filePath}` : \"Write\",\n kind: \"edit\",\n content: contentResult,\n locations: filePath ? [{ path: filePath }] : [],\n };\n }\n\n case \"Glob\": {\n let label = \"Find\";\n const pathStr = input?.path ? String(input.path) : undefined;\n if (pathStr) {\n label += ` \"${pathStr}\"`;\n }\n if (input?.pattern) {\n label += ` \"${String(input.pattern)}\"`;\n }\n return {\n title: label,\n kind: \"search\",\n content: [],\n locations: pathStr ? [{ path: pathStr }] : [],\n };\n }\n\n case \"Grep\": {\n let label = \"grep\";\n\n if (input?.[\"-i\"]) {\n label += \" -i\";\n }\n if (input?.[\"-n\"]) {\n label += \" -n\";\n }\n\n if (input?.[\"-A\"] !== undefined) {\n label += ` -A ${input[\"-A\"]}`;\n }\n if (input?.[\"-B\"] !== undefined) {\n label += ` -B ${input[\"-B\"]}`;\n }\n if (input?.[\"-C\"] !== undefined) {\n label += ` -C ${input[\"-C\"]}`;\n }\n\n if (input?.output_mode) {\n switch (input.output_mode) {\n case \"FilesWithMatches\":\n label += \" -l\";\n break;\n case \"Count\":\n label += \" -c\";\n break;\n default:\n break;\n }\n }\n\n if (input?.head_limit !== undefined) {\n label += ` | head -${input.head_limit}`;\n }\n\n if (input?.glob) {\n label += ` --include=\"${String(input.glob)}\"`;\n }\n\n if (input?.type) {\n label += ` --type=${String(input.type)}`;\n }\n\n if (input?.multiline) {\n label += \" -P\";\n }\n\n label += ` \"${input?.pattern ? String(input.pattern) : \"\"}\"`;\n\n if (input?.path) {\n label += ` ${String(input.path)}`;\n }\n\n return {\n title: label,\n kind: \"search\",\n content: [],\n };\n }\n\n case \"WebFetch\":\n return {\n title: \"Fetch\",\n kind: \"fetch\",\n content: input?.url\n ? [\n {\n type: \"content\",\n content: resourceLink(String(input.url), String(input.url), {\n description: input?.prompt ? String(input.prompt) : undefined,\n }),\n },\n ]\n : [],\n };\n\n case \"WebSearch\": {\n let label = `\"${input?.query ? String(input.query) : \"\"}\"`;\n const allowedDomains = input?.allowed_domains as string[] | undefined;\n const blockedDomains = input?.blocked_domains as string[] | undefined;\n\n if (allowedDomains && allowedDomains.length > 0) {\n label += ` (allowed: ${allowedDomains.join(\", \")})`;\n }\n\n if (blockedDomains && blockedDomains.length > 0) {\n label += ` (blocked: ${blockedDomains.join(\", \")})`;\n }\n\n return {\n title: label,\n kind: \"fetch\",\n content: [],\n };\n }\n\n case \"TodoWrite\":\n return {\n title: Array.isArray(input?.todos)\n ? `Update TODOs: ${input.todos.map((todo: { content?: string }) => todo.content).join(\", \")}`\n : \"Update TODOs\",\n kind: \"think\",\n content: [],\n };\n\n case \"ExitPlanMode\":\n return {\n title: \"Ready to code?\",\n kind: \"switch_mode\",\n content: input?.plan\n ? toolContent().text(String(input.plan)).build()\n : [],\n };\n\n case \"AskUserQuestion\": {\n const questions = input?.questions as\n | Array<{ question?: string }>\n | undefined;\n return {\n title: questions?.[0]?.question || \"Question\",\n kind: \"other\" as ToolKind,\n content: questions\n ? toolContent()\n .text(JSON.stringify(questions, null, 2))\n .build()\n : [],\n };\n }\n\n case \"Other\": {\n let output: string;\n try {\n output = JSON.stringify(input, null, 2);\n } catch {\n output = typeof input === \"string\" ? input : \"{}\";\n }\n return {\n title: name || \"Unknown Tool\",\n kind: \"other\",\n content: toolContent().text(`\\`\\`\\`json\\n${output}\\`\\`\\``).build(),\n };\n }\n\n default: {\n if (name?.startsWith(\"mcp__\")) {\n return mcpToolInfo(name, input);\n }\n return {\n title: name || \"Unknown Tool\",\n kind: \"other\",\n content: [],\n };\n }\n }\n}\n\nfunction mcpToolInfo(\n name: string,\n _input: Record<string, unknown> | undefined,\n): ToolInfo {\n const metadata = getMcpToolMetadata(name);\n // Fallback: parse tool name from mcp__<server>__<tool> prefix\n const title =\n metadata?.name ?? (name.split(\"__\").slice(2).join(\"__\") || name);\n\n return {\n title,\n kind: \"other\",\n content: [],\n };\n}\n\nexport function toolUpdateFromToolResult(\n toolResult:\n | ToolResultBlockParam\n | BetaWebSearchToolResultBlockParam\n | BetaWebFetchToolResultBlockParam\n | WebSearchToolResultBlockParam\n | BetaCodeExecutionToolResultBlockParam\n | BetaBashCodeExecutionToolResultBlockParam\n | BetaTextEditorCodeExecutionToolResultBlockParam\n | BetaRequestMCPToolResultBlockParam\n | BetaToolSearchToolResultBlockParam,\n toolUse: Pick<ToolUseBlock, \"name\" | \"input\"> | undefined,\n): Pick<ToolCallUpdate, \"title\" | \"content\" | \"locations\"> {\n switch (toolUse?.name) {\n case \"Read\":\n if (Array.isArray(toolResult.content) && toolResult.content.length > 0) {\n return {\n content: toolResult.content.map((item) => {\n const itemObj = item as { type?: string; text?: string };\n if (itemObj.type === \"text\") {\n return {\n type: \"content\" as const,\n content: text(\n markdownEscape(\n (itemObj.text ?? \"\").replace(SYSTEM_REMINDER, \"\"),\n ),\n ),\n };\n }\n return {\n type: \"content\" as const,\n content: item as { type: \"text\"; text: string },\n };\n }),\n };\n } else if (\n typeof toolResult.content === \"string\" &&\n toolResult.content.length > 0\n ) {\n return {\n content: toolContent()\n .text(\n markdownEscape(toolResult.content.replace(SYSTEM_REMINDER, \"\")),\n )\n .build(),\n };\n }\n return {};\n\n case \"Bash\": {\n return toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n }\n case \"Edit\":\n case \"Write\": {\n if (\n \"is_error\" in toolResult &&\n toolResult.is_error &&\n toolResult.content &&\n toolResult.content.length > 0\n ) {\n return toAcpContentUpdate(toolResult.content, true);\n }\n return {};\n }\n\n case \"ExitPlanMode\": {\n return { title: \"Exited Plan Mode\" };\n }\n case \"AskUserQuestion\": {\n const content = toolResult.content;\n if (Array.isArray(content) && content.length > 0) {\n const firstItem = content[0];\n if (\n typeof firstItem === \"object\" &&\n firstItem !== null &&\n \"text\" in firstItem\n ) {\n return {\n title: \"Answer received\",\n content: toolContent().text(String(firstItem.text)).build(),\n };\n }\n }\n return { title: \"Question answered\" };\n }\n case \"WebFetch\": {\n const input = toolUse?.input as Record<string, unknown> | undefined;\n const url = input?.url ? String(input.url) : \"\";\n const prompt = input?.prompt ? String(input.prompt) : undefined;\n\n const resultContent = toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n\n const content: ToolCallContent[] = [];\n if (url) {\n content.push({\n type: \"content\",\n content: resourceLink(url, url, {\n description: prompt,\n }),\n });\n }\n if (resultContent.content) {\n content.push(...resultContent.content);\n }\n\n return { content };\n }\n default: {\n return toAcpContentUpdate(\n toolResult.content,\n \"is_error\" in toolResult ? toolResult.is_error : false,\n );\n }\n }\n}\n\nfunction toAcpContentUpdate(\n content: unknown,\n isError: boolean = false,\n): Pick<ToolCallUpdate, \"content\"> {\n if (Array.isArray(content) && content.length > 0) {\n return {\n content: content.map((item) => {\n const itemObj = item as { type?: string; text?: string };\n if (isError && itemObj.type === \"text\") {\n return {\n type: \"content\" as const,\n content: text(`\\`\\`\\`\\n${itemObj.text ?? \"\"}\\n\\`\\`\\``),\n };\n }\n return {\n type: \"content\" as const,\n content: item as { type: \"text\"; text: string },\n };\n }),\n };\n } else if (typeof content === \"string\" && content.length > 0) {\n return {\n content: toolContent()\n .text(isError ? `\\`\\`\\`\\n${content}\\n\\`\\`\\`` : content)\n .build(),\n };\n }\n return {};\n}\n\nexport type ClaudePlanEntry = {\n content: string;\n status: \"pending\" | \"in_progress\" | \"completed\";\n activeForm: string;\n};\n\nexport function planEntries(input: { todos: ClaudePlanEntry[] }): PlanEntry[] {\n return input.todos.map((input) => ({\n content: input.content,\n status: input.status,\n priority: \"medium\",\n }));\n}\n\nfunction markdownEscape(text: string): string {\n let escapedText = \"```\";\n for (const [m] of text.matchAll(/^```+/gm)) {\n while (m.length >= escapedText.length) {\n escapedText += \"`\";\n }\n }\n return `${escapedText}\\n${text}${text.endsWith(\"\\n\") ? \"\" : \"\\n\"}${escapedText}`;\n}\n"],"mappings":";AAEO,SAAS,KAAK,OAA6B;AAChD,SAAO,EAAE,MAAM,QAAQ,MAAM,MAAM;AACrC;AAEO,SAAS,MACd,MACA,UACA,KACc;AACd,SAAO,EAAE,MAAM,SAAS,MAAM,UAAU,IAAI;AAC9C;AAEO,SAAS,aACd,KACA,MACA,SAMc;AACd,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EACL;AACF;AAEA,IAAM,qBAAN,MAAyB;AAAA,EACf,QAA2B,CAAC;AAAA,EAEpC,KAAK,OAAqB;AACxB,SAAK,MAAM,KAAK,EAAE,MAAM,WAAW,SAAS,KAAK,KAAK,EAAE,CAAC;AACzD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,MAAc,UAAkB,KAAoB;AACxD,SAAK,MAAM,KAAK,EAAE,MAAM,WAAW,SAAS,MAAM,MAAM,UAAU,GAAG,EAAE,CAAC;AACxE,WAAO;AAAA,EACT;AAAA,EAEA,KAAK,MAAc,SAAwB,SAAuB;AAChE,SAAK,MAAM,KAAK,EAAE,MAAM,QAAQ,MAAM,SAAS,QAAQ,CAAC;AACxD,WAAO;AAAA,EACT;AAAA,EAEA,QAA2B;AACzB,WAAO,KAAK;AAAA,EACd;AACF;AAEO,SAAS,cAAkC;AAChD,SAAO,IAAI,mBAAmB;AAChC;;;AChDO,IAAM,SAAN,MAAM,QAAO;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,SAAuB,CAAC,GAAG;AACrC,SA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.ts
|
|
2
2
|
var IS_ROOT = typeof process !== "undefined" && (process.geteuid?.() ?? process.getuid?.()) === 0;
|
|
3
3
|
|
|
4
|
-
// src/adapters/claude/mcp/tool-metadata.ts
|
|
5
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
7
|
-
|
|
8
4
|
// src/adapters/claude/tools.ts
|
|
9
5
|
var READ_TOOLS = /* @__PURE__ */ new Set(["Read", "NotebookRead"]);
|
|
10
6
|
var WRITE_TOOLS = /* @__PURE__ */ new Set([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/utils/common.ts","../../../../src/adapters/claude/mcp/tool-metadata.ts","../../../../src/adapters/claude/tools.ts","../../../../src/adapters/claude/permissions/permission-options.ts"],"sourcesContent":["import type { Logger } from \"./logger.js\";\n\n/**\n * Races an operation against a timeout.\n * Returns success with the value if the operation completes in time,\n * or timeout if the operation takes longer than the specified duration.\n */\nexport async function withTimeout<T>(\n operation: Promise<T>,\n timeoutMs: number,\n): Promise<{ result: \"success\"; value: T } | { result: \"timeout\" }> {\n const timeoutPromise = new Promise<{ result: \"timeout\" }>((resolve) =>\n setTimeout(() => resolve({ result: \"timeout\" }), timeoutMs),\n );\n const operationPromise = operation.then((value) => ({\n result: \"success\" as const,\n value,\n }));\n return Promise.race([operationPromise, timeoutPromise]);\n}\n\nexport const IS_ROOT =\n typeof process !== \"undefined\" &&\n (process.geteuid?.() ?? process.getuid?.()) === 0;\n\nexport function unreachable(value: never, logger: Logger): void {\n let valueAsString: string;\n try {\n valueAsString = JSON.stringify(value);\n } catch {\n valueAsString = value;\n }\n logger.error(`Unexpected case: ${valueAsString}`);\n}\n","import type {\n McpHttpServerConfig,\n McpServerConfig,\n} from \"@anthropic-ai/claude-agent-sdk\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport type { Tool } from \"@modelcontextprotocol/sdk/types.js\";\nimport { Logger } from \"../../../utils/logger.js\";\n\nexport interface McpToolMetadata {\n readOnly: boolean;\n}\n\nconst mcpToolMetadataCache: Map<string, McpToolMetadata> = new Map();\n\nfunction buildToolKey(serverName: string, toolName: string): string {\n return `mcp__${serverName}__${toolName}`;\n}\n\nfunction isHttpMcpServer(\n config: McpServerConfig,\n): config is McpHttpServerConfig {\n return config.type === \"http\" && typeof config.url === \"string\";\n}\n\nasync function fetchToolsFromHttpServer(\n _serverName: string,\n config: McpHttpServerConfig,\n): Promise<Tool[]> {\n const transport = new StreamableHTTPClientTransport(new URL(config.url), {\n requestInit: {\n headers: config.headers ?? {},\n },\n });\n\n const client = new Client({\n name: \"twig-metadata-fetcher\",\n version: \"1.0.0\",\n });\n\n try {\n await client.connect(transport);\n const result = await client.listTools();\n return result.tools;\n } finally {\n await client.close().catch(() => {});\n }\n}\n\nfunction extractToolMetadata(tool: Tool): McpToolMetadata {\n return {\n readOnly: tool.annotations?.readOnlyHint === true,\n };\n}\n\nexport async function fetchMcpToolMetadata(\n mcpServers: Record<string, McpServerConfig>,\n logger: Logger = new Logger({ debug: false, prefix: \"[McpToolMetadata]\" }),\n): Promise<void> {\n const fetchPromises: Promise<void>[] = [];\n\n for (const [serverName, config] of Object.entries(mcpServers)) {\n if (!isHttpMcpServer(config)) {\n continue;\n }\n\n const fetchPromise = fetchToolsFromHttpServer(serverName, config)\n .then((tools) => {\n const toolCount = tools.length;\n const readOnlyCount = tools.filter(\n (t) => t.annotations?.readOnlyHint === true,\n ).length;\n\n for (const tool of tools) {\n const toolKey = buildToolKey(serverName, tool.name);\n mcpToolMetadataCache.set(toolKey, extractToolMetadata(tool));\n }\n\n logger.info(\"Fetched MCP tool metadata\", {\n serverName,\n toolCount,\n readOnlyCount,\n });\n })\n .catch((error) => {\n logger.error(\"Failed to fetch MCP tool metadata\", {\n serverName,\n error: error instanceof Error ? error.message : String(error),\n });\n });\n\n fetchPromises.push(fetchPromise);\n }\n\n await Promise.all(fetchPromises);\n}\n\nexport function isMcpToolReadOnly(toolName: string): boolean {\n const metadata = mcpToolMetadataCache.get(toolName);\n return metadata?.readOnly === true;\n}\n\nexport function clearMcpToolMetadataCache(): void {\n mcpToolMetadataCache.clear();\n}\n","export {\n getAvailableModes,\n type ModeInfo,\n TWIG_EXECUTION_MODES,\n type TwigExecutionMode,\n} from \"../../execution-mode.js\";\n\nimport type { TwigExecutionMode } from \"../../execution-mode.js\";\nimport { isMcpToolReadOnly } from \"./mcp/tool-metadata.js\";\n\nexport const READ_TOOLS: Set<string> = new Set([\"Read\", \"NotebookRead\"]);\n\nexport const WRITE_TOOLS: Set<string> = new Set([\n \"Edit\",\n \"Write\",\n \"NotebookEdit\",\n]);\n\nexport const BASH_TOOLS: Set<string> = new Set([\n \"Bash\",\n \"BashOutput\",\n \"KillShell\",\n]);\n\nexport const SEARCH_TOOLS: Set<string> = new Set([\"Glob\", \"Grep\", \"LS\"]);\n\nexport const WEB_TOOLS: Set<string> = new Set([\"WebSearch\", \"WebFetch\"]);\n\nexport const AGENT_TOOLS: Set<string> = new Set([\"Task\", \"TodoWrite\", \"Skill\"]);\n\nconst BASE_ALLOWED_TOOLS = [\n ...READ_TOOLS,\n ...SEARCH_TOOLS,\n ...WEB_TOOLS,\n ...AGENT_TOOLS,\n];\n\nconst AUTO_ALLOWED_TOOLS: Record<string, Set<string>> = {\n default: new Set(BASE_ALLOWED_TOOLS),\n acceptEdits: new Set([...BASE_ALLOWED_TOOLS, ...WRITE_TOOLS]),\n plan: new Set(BASE_ALLOWED_TOOLS),\n};\n\nexport function isToolAllowedForMode(\n toolName: string,\n mode: TwigExecutionMode,\n): boolean {\n if (mode === \"bypassPermissions\") {\n return true;\n }\n if (AUTO_ALLOWED_TOOLS[mode]?.has(toolName) === true) {\n return true;\n }\n if (isMcpToolReadOnly(toolName)) {\n return true;\n }\n return false;\n}\n","import type { PermissionUpdate } from \"@anthropic-ai/claude-agent-sdk\";\nimport { BASH_TOOLS, READ_TOOLS, SEARCH_TOOLS, WRITE_TOOLS } from \"../tools.js\";\n\nexport interface PermissionOption {\n kind: \"allow_once\" | \"allow_always\" | \"reject_once\" | \"reject_always\";\n name: string;\n optionId: string;\n _meta?: { description?: string; customInput?: boolean };\n}\n\nfunction permissionOptions(allowAlwaysLabel: string): PermissionOption[] {\n return [\n { kind: \"allow_once\", name: \"Yes\", optionId: \"allow\" },\n { kind: \"allow_always\", name: allowAlwaysLabel, optionId: \"allow_always\" },\n {\n kind: \"reject_once\",\n name: \"No, and tell the agent what to do differently\",\n optionId: \"reject\",\n _meta: { customInput: true },\n },\n ];\n}\n\nexport function buildPermissionOptions(\n toolName: string,\n toolInput: Record<string, unknown>,\n cwd?: string,\n suggestions?: PermissionUpdate[],\n): PermissionOption[] {\n if (BASH_TOOLS.has(toolName)) {\n const rawRuleContent = suggestions\n ?.flatMap((s) => (\"rules\" in s ? s.rules : []))\n .find((r) => r.toolName === \"Bash\" && r.ruleContent)?.ruleContent;\n const ruleContent = rawRuleContent?.replace(/:?\\*$/, \"\");\n\n const command = toolInput?.command as string | undefined;\n const cmdName = command?.split(/\\s+/)[0] ?? \"this command\";\n const cwdLabel = cwd ? ` in ${cwd}` : \"\";\n const label = ruleContent ?? `\\`${cmdName}\\` commands`;\n\n return permissionOptions(\n `Yes, and don't ask again for ${label}${cwdLabel}`,\n );\n }\n\n if (toolName === \"BashOutput\") {\n return permissionOptions(\"Yes, allow all background process reads\");\n }\n\n if (toolName === \"KillShell\") {\n return permissionOptions(\"Yes, allow killing processes\");\n }\n\n if (WRITE_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all edits during this session\");\n }\n\n if (READ_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all reads during this session\");\n }\n\n if (SEARCH_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all searches during this session\");\n }\n\n if (toolName === \"WebFetch\") {\n const url = toolInput?.url as string | undefined;\n let domain = \"\";\n try {\n domain = url ? new URL(url).hostname : \"\";\n } catch {}\n return permissionOptions(\n domain\n ? `Yes, allow all fetches from ${domain}`\n : \"Yes, allow all fetches\",\n );\n }\n\n if (toolName === \"WebSearch\") {\n return permissionOptions(\"Yes, allow all web searches\");\n }\n\n if (toolName === \"Task\") {\n return permissionOptions(\"Yes, allow all sub-tasks\");\n }\n\n if (toolName === \"TodoWrite\") {\n return permissionOptions(\"Yes, allow all todo updates\");\n }\n\n return permissionOptions(\"Yes, always allow\");\n}\n\nexport function buildExitPlanModePermissionOptions(): PermissionOption[] {\n return [\n {\n kind: \"allow_always\",\n name: \"Yes, and auto-accept edits\",\n optionId: \"acceptEdits\",\n },\n {\n kind: \"allow_once\",\n name: \"Yes, and manually approve edits\",\n optionId: \"default\",\n },\n {\n kind: \"reject_once\",\n name: \"No, keep planning\",\n optionId: \"plan\",\n },\n ];\n}\n"],"mappings":";AAqBO,IAAM,UACX,OAAO,YAAY,gBAClB,QAAQ,UAAU,KAAK,QAAQ,SAAS,OAAO;;;ACnBlD,SAAS,cAAc;AACvB,SAAS,qCAAqC;;;ACKvC,IAAM,aAA0B,oBAAI,IAAI,CAAC,QAAQ,cAAc,CAAC;AAEhE,IAAM,cAA2B,oBAAI,IAAI;AAAA,EAC9C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,aAA0B,oBAAI,IAAI;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,eAA4B,oBAAI,IAAI,CAAC,QAAQ,QAAQ,IAAI,CAAC;AAEhE,IAAM,YAAyB,oBAAI,IAAI,CAAC,aAAa,UAAU,CAAC;AAEhE,IAAM,cAA2B,oBAAI,IAAI,CAAC,QAAQ,aAAa,OAAO,CAAC;AAE9E,IAAM,qBAAqB;AAAA,EACzB,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AACL;AAEA,IAAM,qBAAkD;AA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EA,MAAI,aAAa,IAAI,QAAQ,GAAG;AAC9B,WAAO,kBAAkB,6CAA6C;AAAA,EACxE;AAEA,MAAI,aAAa,YAAY;AAC3B,UAAM,MAAM,WAAW;AACvB,QAAI,SAAS;AACb,QAAI;AACF,eAAS,MAAM,IAAI,IAAI,GAAG,EAAE,WAAW;AAAA,IACzC,QAAQ;AAAA,IAAC;AACT,WAAO;AAAA,MACL,SACI,+BAA+B,MAAM,KACrC;AAAA,IACN;AAAA,EACF;AAEA,MAAI,aAAa,aAAa;AAC5B,WAAO,kBAAkB,6BAA6B;AAAA,EACxD;AAEA,MAAI,aAAa,QAAQ;AACvB,WAAO,kBAAkB,0BAA0B;AAAA,EACrD;AAEA,MAAI,aAAa,aAAa;AAC5B,WAAO,kBAAkB,6BAA6B;AAAA,EACxD;AAEA,SAAO,kBAAkB,mBAAmB;AAC9C;AAEO,SAAS,qCAAyD;AACvE,SAAO;AAAA,IACL;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/common.ts","../../../../src/adapters/claude/tools.ts","../../../../src/adapters/claude/permissions/permission-options.ts"],"sourcesContent":["import type { Logger } from \"./logger.js\";\n\n/**\n * Races an operation against a timeout.\n * Returns success with the value if the operation completes in time,\n * or timeout if the operation takes longer than the specified duration.\n */\nexport async function withTimeout<T>(\n operation: Promise<T>,\n timeoutMs: number,\n): Promise<{ result: \"success\"; value: T } | { result: \"timeout\" }> {\n const timeoutPromise = new Promise<{ result: \"timeout\" }>((resolve) =>\n setTimeout(() => resolve({ result: \"timeout\" }), timeoutMs),\n );\n const operationPromise = operation.then((value) => ({\n result: \"success\" as const,\n value,\n }));\n return Promise.race([operationPromise, timeoutPromise]);\n}\n\nexport const IS_ROOT =\n typeof process !== \"undefined\" &&\n (process.geteuid?.() ?? process.getuid?.()) === 0;\n\nexport function unreachable(value: never, logger: Logger): void {\n let valueAsString: string;\n try {\n valueAsString = JSON.stringify(value);\n } catch {\n valueAsString = value;\n }\n logger.error(`Unexpected case: ${valueAsString}`);\n}\n","export {\n getAvailableModes,\n type ModeInfo,\n TWIG_EXECUTION_MODES,\n type TwigExecutionMode,\n} from \"../../execution-mode.js\";\n\nimport type { TwigExecutionMode } from \"../../execution-mode.js\";\nimport { isMcpToolReadOnly } from \"./mcp/tool-metadata.js\";\n\nexport const READ_TOOLS: Set<string> = new Set([\"Read\", \"NotebookRead\"]);\n\nexport const WRITE_TOOLS: Set<string> = new Set([\n \"Edit\",\n \"Write\",\n \"NotebookEdit\",\n]);\n\nexport const BASH_TOOLS: Set<string> = new Set([\n \"Bash\",\n \"BashOutput\",\n \"KillShell\",\n]);\n\nexport const SEARCH_TOOLS: Set<string> = new Set([\"Glob\", \"Grep\", \"LS\"]);\n\nexport const WEB_TOOLS: Set<string> = new Set([\"WebSearch\", \"WebFetch\"]);\n\nexport const AGENT_TOOLS: Set<string> = new Set([\"Task\", \"TodoWrite\", \"Skill\"]);\n\nconst BASE_ALLOWED_TOOLS = [\n ...READ_TOOLS,\n ...SEARCH_TOOLS,\n ...WEB_TOOLS,\n ...AGENT_TOOLS,\n];\n\nconst AUTO_ALLOWED_TOOLS: Record<string, Set<string>> = {\n default: new Set(BASE_ALLOWED_TOOLS),\n acceptEdits: new Set([...BASE_ALLOWED_TOOLS, ...WRITE_TOOLS]),\n plan: new Set(BASE_ALLOWED_TOOLS),\n};\n\nexport function isToolAllowedForMode(\n toolName: string,\n mode: TwigExecutionMode,\n): boolean {\n if (mode === \"bypassPermissions\") {\n return true;\n }\n if (AUTO_ALLOWED_TOOLS[mode]?.has(toolName) === true) {\n return true;\n }\n if (isMcpToolReadOnly(toolName)) {\n return true;\n }\n return false;\n}\n","import type { PermissionUpdate } from \"@anthropic-ai/claude-agent-sdk\";\nimport { BASH_TOOLS, READ_TOOLS, SEARCH_TOOLS, WRITE_TOOLS } from \"../tools.js\";\n\nexport interface PermissionOption {\n kind: \"allow_once\" | \"allow_always\" | \"reject_once\" | \"reject_always\";\n name: string;\n optionId: string;\n _meta?: { description?: string; customInput?: boolean };\n}\n\nfunction permissionOptions(allowAlwaysLabel: string): PermissionOption[] {\n return [\n { kind: \"allow_once\", name: \"Yes\", optionId: \"allow\" },\n { kind: \"allow_always\", name: allowAlwaysLabel, optionId: \"allow_always\" },\n {\n kind: \"reject_once\",\n name: \"No, and tell the agent what to do differently\",\n optionId: \"reject\",\n _meta: { customInput: true },\n },\n ];\n}\n\nexport function buildPermissionOptions(\n toolName: string,\n toolInput: Record<string, unknown>,\n cwd?: string,\n suggestions?: PermissionUpdate[],\n): PermissionOption[] {\n if (BASH_TOOLS.has(toolName)) {\n const rawRuleContent = suggestions\n ?.flatMap((s) => (\"rules\" in s ? s.rules : []))\n .find((r) => r.toolName === \"Bash\" && r.ruleContent)?.ruleContent;\n const ruleContent = rawRuleContent?.replace(/:?\\*$/, \"\");\n\n const command = toolInput?.command as string | undefined;\n const cmdName = command?.split(/\\s+/)[0] ?? \"this command\";\n const cwdLabel = cwd ? ` in ${cwd}` : \"\";\n const label = ruleContent ?? `\\`${cmdName}\\` commands`;\n\n return permissionOptions(\n `Yes, and don't ask again for ${label}${cwdLabel}`,\n );\n }\n\n if (toolName === \"BashOutput\") {\n return permissionOptions(\"Yes, allow all background process reads\");\n }\n\n if (toolName === \"KillShell\") {\n return permissionOptions(\"Yes, allow killing processes\");\n }\n\n if (WRITE_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all edits during this session\");\n }\n\n if (READ_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all reads during this session\");\n }\n\n if (SEARCH_TOOLS.has(toolName)) {\n return permissionOptions(\"Yes, allow all searches during this session\");\n }\n\n if (toolName === \"WebFetch\") {\n const url = toolInput?.url as string | undefined;\n let domain = \"\";\n try {\n domain = url ? new URL(url).hostname : \"\";\n } catch {}\n return permissionOptions(\n domain\n ? `Yes, allow all fetches from ${domain}`\n : \"Yes, allow all fetches\",\n );\n }\n\n if (toolName === \"WebSearch\") {\n return permissionOptions(\"Yes, allow all web searches\");\n }\n\n if (toolName === \"Task\") {\n return permissionOptions(\"Yes, allow all sub-tasks\");\n }\n\n if (toolName === \"TodoWrite\") {\n return permissionOptions(\"Yes, allow all todo updates\");\n }\n\n return permissionOptions(\"Yes, always allow\");\n}\n\nexport function buildExitPlanModePermissionOptions(): PermissionOption[] {\n return [\n {\n kind: \"allow_always\",\n name: \"Yes, and auto-accept edits\",\n optionId: \"acceptEdits\",\n },\n {\n kind: \"allow_once\",\n name: \"Yes, and manually approve edits\",\n optionId: \"default\",\n },\n {\n kind: \"reject_once\",\n name: \"No, keep planning\",\n optionId: \"plan\",\n },\n ];\n}\n"],"mappings":";AAqBO,IAAM,UACX,OAAO,YAAY,gBAClB,QAAQ,UAAU,KAAK,QAAQ,SAAS,OAAO;;;ACb3C,IAAM,aAA0B,oBAAI,IAAI,CAAC,QAAQ,cAAc,CAAC;AAEhE,IAAM,cAA2B,oBAAI,IAAI;AAAA,EAC9C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,aAA0B,oBAAI,IAAI;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,eAA4B,oBAAI,IAAI,CAAC,QAAQ,QAAQ,IAAI,CAAC;AAEhE,IAAM,YAAyB,oBAAI,IAAI,CAAC,aAAa,UAAU,CAAC;AAEhE,IAAM,cAA2B,oBAAI,IAAI,CAAC,QAAQ,aAAa,OAAO,CAAC;AAE9E,IAAM,qBAAqB;AAAA,EACzB,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AACL;AAEA,IAAM,qBAAkD;AAAA,EACtD,SAAS,IA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AI,QAAQ,GAAG;AAC9B,WAAO,kBAAkB,6CAA6C;AAAA,EACxE;AAEA,MAAI,aAAa,YAAY;AAC3B,UAAM,MAAM,WAAW;AACvB,QAAI,SAAS;AACb,QAAI;AACF,eAAS,MAAM,IAAI,IAAI,GAAG,EAAE,WAAW;AAAA,IACzC,QAAQ;AAAA,IAAC;AACT,WAAO;AAAA,MACL,SACI,+BAA+B,MAAM,KACrC;AAAA,IACN;AAAA,EACF;AAEA,MAAI,aAAa,aAAa;AAC5B,WAAO,kBAAkB,6BAA6B;AAAA,EACxD;AAEA,MAAI,aAAa,QAAQ;AACvB,WAAO,kBAAkB,0BAA0B;AAAA,EACrD;AAEA,MAAI,aAAa,aAAa;AAC5B,WAAO,kBAAkB,6BAA6B;AAAA,EACxD;AAEA,SAAO,kBAAkB,mBAAmB;AAC9C;AAEO,SAAS,qCAAyD;AACvE,SAAO;AAAA,IACL;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACZ;AAAA,EACF;AACF;","names":[]}
|
|
@@ -35,8 +35,6 @@ function getAvailableModes() {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// src/adapters/claude/mcp/tool-metadata.ts
|
|
38
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
39
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
40
38
|
var mcpToolMetadataCache = /* @__PURE__ */ new Map();
|
|
41
39
|
function isMcpToolReadOnly(toolName) {
|
|
42
40
|
const metadata = mcpToolMetadataCache.get(toolName);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/common.ts","../../../src/execution-mode.ts","../../../src/adapters/claude/mcp/tool-metadata.ts","../../../src/adapters/claude/tools.ts"],"sourcesContent":["import type { Logger } from \"./logger.js\";\n\n/**\n * Races an operation against a timeout.\n * Returns success with the value if the operation completes in time,\n * or timeout if the operation takes longer than the specified duration.\n */\nexport async function withTimeout<T>(\n operation: Promise<T>,\n timeoutMs: number,\n): Promise<{ result: \"success\"; value: T } | { result: \"timeout\" }> {\n const timeoutPromise = new Promise<{ result: \"timeout\" }>((resolve) =>\n setTimeout(() => resolve({ result: \"timeout\" }), timeoutMs),\n );\n const operationPromise = operation.then((value) => ({\n result: \"success\" as const,\n value,\n }));\n return Promise.race([operationPromise, timeoutPromise]);\n}\n\nexport const IS_ROOT =\n typeof process !== \"undefined\" &&\n (process.geteuid?.() ?? process.getuid?.()) === 0;\n\nexport function unreachable(value: never, logger: Logger): void {\n let valueAsString: string;\n try {\n valueAsString = JSON.stringify(value);\n } catch {\n valueAsString = value;\n }\n logger.error(`Unexpected case: ${valueAsString}`);\n}\n","import { IS_ROOT } from \"./utils/common.js\";\n\nexport interface ModeInfo {\n id: TwigExecutionMode;\n name: string;\n description: string;\n}\n\nconst MODES: ModeInfo[] = [\n {\n id: \"default\",\n name: \"Always Ask\",\n description: \"Prompts for permission on first use of each tool\",\n },\n {\n id: \"acceptEdits\",\n name: \"Accept Edits\",\n description: \"Automatically accepts file edit permissions for the session\",\n },\n {\n id: \"plan\",\n name: \"Plan Mode\",\n description: \"Claude can analyze but not modify files or execute commands\",\n },\n {\n id: \"bypassPermissions\",\n name: \"Bypass Permissions\",\n description: \"Skips all permission prompts\",\n },\n];\n\nexport const TWIG_EXECUTION_MODES = [\n \"default\",\n \"acceptEdits\",\n \"plan\",\n \"bypassPermissions\",\n] as const;\n\nexport type TwigExecutionMode = (typeof TWIG_EXECUTION_MODES)[number];\n\nexport function getAvailableModes(): ModeInfo[] {\n return IS_ROOT ? MODES.filter((m) => m.id !== \"bypassPermissions\") : MODES;\n}\n","import type {
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/common.ts","../../../src/execution-mode.ts","../../../src/adapters/claude/mcp/tool-metadata.ts","../../../src/adapters/claude/tools.ts"],"sourcesContent":["import type { Logger } from \"./logger.js\";\n\n/**\n * Races an operation against a timeout.\n * Returns success with the value if the operation completes in time,\n * or timeout if the operation takes longer than the specified duration.\n */\nexport async function withTimeout<T>(\n operation: Promise<T>,\n timeoutMs: number,\n): Promise<{ result: \"success\"; value: T } | { result: \"timeout\" }> {\n const timeoutPromise = new Promise<{ result: \"timeout\" }>((resolve) =>\n setTimeout(() => resolve({ result: \"timeout\" }), timeoutMs),\n );\n const operationPromise = operation.then((value) => ({\n result: \"success\" as const,\n value,\n }));\n return Promise.race([operationPromise, timeoutPromise]);\n}\n\nexport const IS_ROOT =\n typeof process !== \"undefined\" &&\n (process.geteuid?.() ?? process.getuid?.()) === 0;\n\nexport function unreachable(value: never, logger: Logger): void {\n let valueAsString: string;\n try {\n valueAsString = JSON.stringify(value);\n } catch {\n valueAsString = value;\n }\n logger.error(`Unexpected case: ${valueAsString}`);\n}\n","import { IS_ROOT } from \"./utils/common.js\";\n\nexport interface ModeInfo {\n id: TwigExecutionMode;\n name: string;\n description: string;\n}\n\nconst MODES: ModeInfo[] = [\n {\n id: \"default\",\n name: \"Always Ask\",\n description: \"Prompts for permission on first use of each tool\",\n },\n {\n id: \"acceptEdits\",\n name: \"Accept Edits\",\n description: \"Automatically accepts file edit permissions for the session\",\n },\n {\n id: \"plan\",\n name: \"Plan Mode\",\n description: \"Claude can analyze but not modify files or execute commands\",\n },\n {\n id: \"bypassPermissions\",\n name: \"Bypass Permissions\",\n description: \"Skips all permission prompts\",\n },\n];\n\nexport const TWIG_EXECUTION_MODES = [\n \"default\",\n \"acceptEdits\",\n \"plan\",\n \"bypassPermissions\",\n] as const;\n\nexport type TwigExecutionMode = (typeof TWIG_EXECUTION_MODES)[number];\n\nexport function getAvailableModes(): ModeInfo[] {\n return IS_ROOT ? MODES.filter((m) => m.id !== \"bypassPermissions\") : MODES;\n}\n","import type { McpServerStatus, Query } from \"@anthropic-ai/claude-agent-sdk\";\nimport { Logger } from \"../../../utils/logger.js\";\n\nexport interface McpToolMetadata {\n readOnly: boolean;\n name: string;\n description?: string;\n}\n\nconst mcpToolMetadataCache: Map<string, McpToolMetadata> = new Map();\n\nconst PENDING_RETRY_INTERVAL_MS = 1_000;\nconst PENDING_MAX_RETRIES = 10;\n\nfunction buildToolKey(serverName: string, toolName: string): string {\n return `mcp__${serverName}__${toolName}`;\n}\n\nfunction delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport async function fetchMcpToolMetadata(\n q: Query,\n logger: Logger = new Logger({ debug: false, prefix: \"[McpToolMetadata]\" }),\n): Promise<void> {\n let retries = 0;\n\n while (retries <= PENDING_MAX_RETRIES) {\n let statuses: McpServerStatus[];\n try {\n statuses = await q.mcpServerStatus();\n } catch (error) {\n logger.error(\"Failed to fetch MCP server status\", {\n error: error instanceof Error ? error.message : String(error),\n });\n return;\n }\n\n const pendingServers = statuses.filter((s) => s.status === \"pending\");\n\n for (const server of statuses) {\n if (server.status !== \"connected\" || !server.tools) {\n continue;\n }\n\n let readOnlyCount = 0;\n for (const tool of server.tools) {\n const toolKey = buildToolKey(server.name, tool.name);\n const readOnly = tool.annotations?.readOnly === true;\n mcpToolMetadataCache.set(toolKey, {\n readOnly,\n name: tool.name,\n description: tool.description,\n });\n if (readOnly) readOnlyCount++;\n }\n\n logger.info(\"Fetched MCP tool metadata\", {\n serverName: server.name,\n toolCount: server.tools.length,\n readOnlyCount,\n });\n }\n\n if (pendingServers.length === 0) {\n return;\n }\n\n retries++;\n if (retries > PENDING_MAX_RETRIES) {\n logger.warn(\"Gave up waiting for pending MCP servers\", {\n pendingServers: pendingServers.map((s) => s.name),\n });\n return;\n }\n\n logger.info(\"Waiting for pending MCP servers\", {\n pendingServers: pendingServers.map((s) => s.name),\n retry: retries,\n });\n await delay(PENDING_RETRY_INTERVAL_MS);\n }\n}\n\nexport function getMcpToolMetadata(\n toolName: string,\n): McpToolMetadata | undefined {\n return mcpToolMetadataCache.get(toolName);\n}\n\nexport function isMcpToolReadOnly(toolName: string): boolean {\n const metadata = mcpToolMetadataCache.get(toolName);\n return metadata?.readOnly === true;\n}\n\nexport function clearMcpToolMetadataCache(): void {\n mcpToolMetadataCache.clear();\n}\n","export {\n getAvailableModes,\n type ModeInfo,\n TWIG_EXECUTION_MODES,\n type TwigExecutionMode,\n} from \"../../execution-mode.js\";\n\nimport type { TwigExecutionMode } from \"../../execution-mode.js\";\nimport { isMcpToolReadOnly } from \"./mcp/tool-metadata.js\";\n\nexport const READ_TOOLS: Set<string> = new Set([\"Read\", \"NotebookRead\"]);\n\nexport const WRITE_TOOLS: Set<string> = new Set([\n \"Edit\",\n \"Write\",\n \"NotebookEdit\",\n]);\n\nexport const BASH_TOOLS: Set<string> = new Set([\n \"Bash\",\n \"BashOutput\",\n \"KillShell\",\n]);\n\nexport const SEARCH_TOOLS: Set<string> = new Set([\"Glob\", \"Grep\", \"LS\"]);\n\nexport const WEB_TOOLS: Set<string> = new Set([\"WebSearch\", \"WebFetch\"]);\n\nexport const AGENT_TOOLS: Set<string> = new Set([\"Task\", \"TodoWrite\", \"Skill\"]);\n\nconst BASE_ALLOWED_TOOLS = [\n ...READ_TOOLS,\n ...SEARCH_TOOLS,\n ...WEB_TOOLS,\n ...AGENT_TOOLS,\n];\n\nconst AUTO_ALLOWED_TOOLS: Record<string, Set<string>> = {\n default: new Set(BASE_ALLOWED_TOOLS),\n acceptEdits: new Set([...BASE_ALLOWED_TOOLS, ...WRITE_TOOLS]),\n plan: new Set(BASE_ALLOWED_TOOLS),\n};\n\nexport function isToolAllowedForMode(\n toolName: string,\n mode: TwigExecutionMode,\n): boolean {\n if (mode === \"bypassPermissions\") {\n return true;\n }\n if (AUTO_ALLOWED_TOOLS[mode]?.has(toolName) === true) {\n return true;\n }\n if (isMcpToolReadOnly(toolName)) {\n return true;\n }\n return false;\n}\n"],"mappings":";AAqBO,I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,IAAI,IAAI,kBAAkB;AAAA,EACnC,aAAa,oBAAI,IAAI,CAAC,GAAG,oBAAoB,GAAG,WAAW,CAAC;AAAA,EAC5D,MAAM,IAAI,IAAI,kBAAkB;AAClC;AAEO,SAAS,qBACd,UACA,MACS;AACT,MAAI,SAAS,qBAAqB;AAChC,WAAO;AAAA,EACT;AACA,MAAI,mBAAmB,IAAI,GAAG,IAAI,QAAQ,MAAM,MAAM;AACpD,WAAO;AAAA,EACT;AACA,MAAI,kBAAkB,QAAQ,GAAG;AAC/B,WAAO;AAAA,EACT;AACA,SAAO;AACT;","names":[]}
|
package/dist/agent.js
CHANGED
|
@@ -276,7 +276,7 @@ import { v7 as uuidv7 } from "uuid";
|
|
|
276
276
|
// package.json
|
|
277
277
|
var package_default = {
|
|
278
278
|
name: "@posthog/agent",
|
|
279
|
-
version: "2.1.
|
|
279
|
+
version: "2.1.113",
|
|
280
280
|
repository: "https://github.com/PostHog/twig",
|
|
281
281
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
282
282
|
exports: {
|
|
@@ -364,7 +364,6 @@ var package_default = {
|
|
|
364
364
|
"@anthropic-ai/claude-agent-sdk": "0.2.59",
|
|
365
365
|
"@anthropic-ai/sdk": "^0.71.0",
|
|
366
366
|
"@hono/node-server": "^1.19.9",
|
|
367
|
-
"@modelcontextprotocol/sdk": "^1.25.3",
|
|
368
367
|
"@opentelemetry/api-logs": "^0.208.0",
|
|
369
368
|
"@opentelemetry/exporter-logs-otlp-http": "^0.208.0",
|
|
370
369
|
"@opentelemetry/resources": "^2.0.0",
|
|
@@ -740,6 +739,75 @@ var createPostToolUseHook = ({ onModeChange }) => async (input, toolUseID) => {
|
|
|
740
739
|
return { continue: true };
|
|
741
740
|
};
|
|
742
741
|
|
|
742
|
+
// src/adapters/claude/mcp/tool-metadata.ts
|
|
743
|
+
var mcpToolMetadataCache = /* @__PURE__ */ new Map();
|
|
744
|
+
var PENDING_RETRY_INTERVAL_MS = 1e3;
|
|
745
|
+
var PENDING_MAX_RETRIES = 10;
|
|
746
|
+
function buildToolKey(serverName, toolName) {
|
|
747
|
+
return `mcp__${serverName}__${toolName}`;
|
|
748
|
+
}
|
|
749
|
+
function delay(ms) {
|
|
750
|
+
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
751
|
+
}
|
|
752
|
+
async function fetchMcpToolMetadata(q, logger = new Logger({ debug: false, prefix: "[McpToolMetadata]" })) {
|
|
753
|
+
let retries = 0;
|
|
754
|
+
while (retries <= PENDING_MAX_RETRIES) {
|
|
755
|
+
let statuses;
|
|
756
|
+
try {
|
|
757
|
+
statuses = await q.mcpServerStatus();
|
|
758
|
+
} catch (error) {
|
|
759
|
+
logger.error("Failed to fetch MCP server status", {
|
|
760
|
+
error: error instanceof Error ? error.message : String(error)
|
|
761
|
+
});
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
const pendingServers = statuses.filter((s) => s.status === "pending");
|
|
765
|
+
for (const server of statuses) {
|
|
766
|
+
if (server.status !== "connected" || !server.tools) {
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
let readOnlyCount = 0;
|
|
770
|
+
for (const tool of server.tools) {
|
|
771
|
+
const toolKey = buildToolKey(server.name, tool.name);
|
|
772
|
+
const readOnly = tool.annotations?.readOnly === true;
|
|
773
|
+
mcpToolMetadataCache.set(toolKey, {
|
|
774
|
+
readOnly,
|
|
775
|
+
name: tool.name,
|
|
776
|
+
description: tool.description
|
|
777
|
+
});
|
|
778
|
+
if (readOnly) readOnlyCount++;
|
|
779
|
+
}
|
|
780
|
+
logger.info("Fetched MCP tool metadata", {
|
|
781
|
+
serverName: server.name,
|
|
782
|
+
toolCount: server.tools.length,
|
|
783
|
+
readOnlyCount
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
if (pendingServers.length === 0) {
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
retries++;
|
|
790
|
+
if (retries > PENDING_MAX_RETRIES) {
|
|
791
|
+
logger.warn("Gave up waiting for pending MCP servers", {
|
|
792
|
+
pendingServers: pendingServers.map((s) => s.name)
|
|
793
|
+
});
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
logger.info("Waiting for pending MCP servers", {
|
|
797
|
+
pendingServers: pendingServers.map((s) => s.name),
|
|
798
|
+
retry: retries
|
|
799
|
+
});
|
|
800
|
+
await delay(PENDING_RETRY_INTERVAL_MS);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
function getMcpToolMetadata(toolName) {
|
|
804
|
+
return mcpToolMetadataCache.get(toolName);
|
|
805
|
+
}
|
|
806
|
+
function isMcpToolReadOnly(toolName) {
|
|
807
|
+
const metadata = mcpToolMetadataCache.get(toolName);
|
|
808
|
+
return metadata?.readOnly === true;
|
|
809
|
+
}
|
|
810
|
+
|
|
743
811
|
// src/adapters/claude/conversion/tool-use-to-acp.ts
|
|
744
812
|
var SYSTEM_REMINDER = `
|
|
745
813
|
|
|
@@ -1070,14 +1138,27 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
|
|
|
1070
1138
|
${output}\`\`\``).build()
|
|
1071
1139
|
};
|
|
1072
1140
|
}
|
|
1073
|
-
default:
|
|
1141
|
+
default: {
|
|
1142
|
+
if (name?.startsWith("mcp__")) {
|
|
1143
|
+
return mcpToolInfo(name, input);
|
|
1144
|
+
}
|
|
1074
1145
|
return {
|
|
1075
1146
|
title: name || "Unknown Tool",
|
|
1076
1147
|
kind: "other",
|
|
1077
1148
|
content: []
|
|
1078
1149
|
};
|
|
1150
|
+
}
|
|
1079
1151
|
}
|
|
1080
1152
|
}
|
|
1153
|
+
function mcpToolInfo(name, _input) {
|
|
1154
|
+
const metadata = getMcpToolMetadata(name);
|
|
1155
|
+
const title = metadata?.name ?? (name.split("__").slice(2).join("__") || name);
|
|
1156
|
+
return {
|
|
1157
|
+
title,
|
|
1158
|
+
kind: "other",
|
|
1159
|
+
content: []
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1081
1162
|
function toolUpdateFromToolResult(toolResult, toolUse) {
|
|
1082
1163
|
switch (toolUse?.name) {
|
|
1083
1164
|
case "Read":
|
|
@@ -1613,75 +1694,6 @@ async function handleUserAssistantMessage(message, context) {
|
|
|
1613
1694
|
return {};
|
|
1614
1695
|
}
|
|
1615
1696
|
|
|
1616
|
-
// src/adapters/claude/mcp/tool-metadata.ts
|
|
1617
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
1618
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
1619
|
-
var mcpToolMetadataCache = /* @__PURE__ */ new Map();
|
|
1620
|
-
function buildToolKey(serverName, toolName) {
|
|
1621
|
-
return `mcp__${serverName}__${toolName}`;
|
|
1622
|
-
}
|
|
1623
|
-
function isHttpMcpServer(config) {
|
|
1624
|
-
return config.type === "http" && typeof config.url === "string";
|
|
1625
|
-
}
|
|
1626
|
-
async function fetchToolsFromHttpServer(_serverName, config) {
|
|
1627
|
-
const transport = new StreamableHTTPClientTransport(new URL(config.url), {
|
|
1628
|
-
requestInit: {
|
|
1629
|
-
headers: config.headers ?? {}
|
|
1630
|
-
}
|
|
1631
|
-
});
|
|
1632
|
-
const client = new Client({
|
|
1633
|
-
name: "twig-metadata-fetcher",
|
|
1634
|
-
version: "1.0.0"
|
|
1635
|
-
});
|
|
1636
|
-
try {
|
|
1637
|
-
await client.connect(transport);
|
|
1638
|
-
const result = await client.listTools();
|
|
1639
|
-
return result.tools;
|
|
1640
|
-
} finally {
|
|
1641
|
-
await client.close().catch(() => {
|
|
1642
|
-
});
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
|
-
function extractToolMetadata(tool) {
|
|
1646
|
-
return {
|
|
1647
|
-
readOnly: tool.annotations?.readOnlyHint === true
|
|
1648
|
-
};
|
|
1649
|
-
}
|
|
1650
|
-
async function fetchMcpToolMetadata(mcpServers, logger = new Logger({ debug: false, prefix: "[McpToolMetadata]" })) {
|
|
1651
|
-
const fetchPromises = [];
|
|
1652
|
-
for (const [serverName, config] of Object.entries(mcpServers)) {
|
|
1653
|
-
if (!isHttpMcpServer(config)) {
|
|
1654
|
-
continue;
|
|
1655
|
-
}
|
|
1656
|
-
const fetchPromise = fetchToolsFromHttpServer(serverName, config).then((tools) => {
|
|
1657
|
-
const toolCount = tools.length;
|
|
1658
|
-
const readOnlyCount = tools.filter(
|
|
1659
|
-
(t) => t.annotations?.readOnlyHint === true
|
|
1660
|
-
).length;
|
|
1661
|
-
for (const tool of tools) {
|
|
1662
|
-
const toolKey = buildToolKey(serverName, tool.name);
|
|
1663
|
-
mcpToolMetadataCache.set(toolKey, extractToolMetadata(tool));
|
|
1664
|
-
}
|
|
1665
|
-
logger.info("Fetched MCP tool metadata", {
|
|
1666
|
-
serverName,
|
|
1667
|
-
toolCount,
|
|
1668
|
-
readOnlyCount
|
|
1669
|
-
});
|
|
1670
|
-
}).catch((error) => {
|
|
1671
|
-
logger.error("Failed to fetch MCP tool metadata", {
|
|
1672
|
-
serverName,
|
|
1673
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1674
|
-
});
|
|
1675
|
-
});
|
|
1676
|
-
fetchPromises.push(fetchPromise);
|
|
1677
|
-
}
|
|
1678
|
-
await Promise.all(fetchPromises);
|
|
1679
|
-
}
|
|
1680
|
-
function isMcpToolReadOnly(toolName) {
|
|
1681
|
-
const metadata = mcpToolMetadataCache.get(toolName);
|
|
1682
|
-
return metadata?.readOnly === true;
|
|
1683
|
-
}
|
|
1684
|
-
|
|
1685
1697
|
// src/adapters/claude/plan/utils.ts
|
|
1686
1698
|
import * as os from "os";
|
|
1687
1699
|
import * as path from "path";
|
|
@@ -2548,7 +2560,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
2548
2560
|
});
|
|
2549
2561
|
}
|
|
2550
2562
|
const modelOptions = await this.getModelConfigOptions();
|
|
2551
|
-
this.deferBackgroundFetches(q, sessionId
|
|
2563
|
+
this.deferBackgroundFetches(q, sessionId);
|
|
2552
2564
|
session.modelId = modelOptions.currentModelId;
|
|
2553
2565
|
const resolvedSdkModel = toSdkModelId(modelOptions.currentModelId);
|
|
2554
2566
|
if (resolvedSdkModel !== DEFAULT_MODEL) {
|
|
@@ -2593,7 +2605,7 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
2593
2605
|
if (validation.result === "timeout") {
|
|
2594
2606
|
throw new Error("Session validation timed out");
|
|
2595
2607
|
}
|
|
2596
|
-
this.deferBackgroundFetches(q, sessionId
|
|
2608
|
+
this.deferBackgroundFetches(q, sessionId);
|
|
2597
2609
|
const configOptions = await this.buildConfigOptions();
|
|
2598
2610
|
return { configOptions };
|
|
2599
2611
|
}
|
|
@@ -2781,10 +2793,10 @@ var ClaudeAcpAgent = class extends BaseAcpAgent {
|
|
|
2781
2793
|
* Fire-and-forget: fetch slash commands and MCP tool metadata in parallel.
|
|
2782
2794
|
* Both populate caches used later — neither is needed to return configOptions.
|
|
2783
2795
|
*/
|
|
2784
|
-
deferBackgroundFetches(q, sessionId
|
|
2796
|
+
deferBackgroundFetches(q, sessionId) {
|
|
2785
2797
|
Promise.all([
|
|
2786
2798
|
getAvailableSlashCommands(q),
|
|
2787
|
-
fetchMcpToolMetadata(
|
|
2799
|
+
fetchMcpToolMetadata(q, this.logger)
|
|
2788
2800
|
]).then(([slashCommands]) => {
|
|
2789
2801
|
this.sendAvailableCommandsUpdate(sessionId, slashCommands);
|
|
2790
2802
|
}).catch((err) => {
|
|
@@ -3716,18 +3728,18 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3716
3728
|
const retryCount = this.retryCounts.get(sessionId) ?? 0;
|
|
3717
3729
|
const lastAttempt = this.lastFlushAttemptTime.get(sessionId) ?? 0;
|
|
3718
3730
|
const elapsed = Date.now() - lastAttempt;
|
|
3719
|
-
let
|
|
3731
|
+
let delay2;
|
|
3720
3732
|
if (retryCount > 0) {
|
|
3721
|
-
|
|
3733
|
+
delay2 = Math.min(
|
|
3722
3734
|
_SessionLogWriter.FLUSH_DEBOUNCE_MS * 2 ** retryCount,
|
|
3723
3735
|
_SessionLogWriter.MAX_RETRY_DELAY_MS
|
|
3724
3736
|
);
|
|
3725
3737
|
} else if (elapsed >= _SessionLogWriter.FLUSH_MAX_INTERVAL_MS) {
|
|
3726
|
-
|
|
3738
|
+
delay2 = 0;
|
|
3727
3739
|
} else {
|
|
3728
|
-
|
|
3740
|
+
delay2 = _SessionLogWriter.FLUSH_DEBOUNCE_MS;
|
|
3729
3741
|
}
|
|
3730
|
-
const timeout = setTimeout(() => this.flush(sessionId),
|
|
3742
|
+
const timeout = setTimeout(() => this.flush(sessionId), delay2);
|
|
3731
3743
|
this.flushTimeouts.set(sessionId, timeout);
|
|
3732
3744
|
}
|
|
3733
3745
|
writeToLocalCache(sessionId, entry) {
|