{"version":3,"file":"turn-delta.d.ts","sourceRoot":"","sources":["../../../src/watchdog/turn-delta.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,sBAAsB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;IACrB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IACnB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAiJD,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CA0BhF;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,GAAG,MAAM,CAgB7E","sourcesContent":["import { SUBAGENT_WATCHDOG_WARNING_TYPE } from \"./types.ts\";\n\ntype MessageLike = {\n\trole?: string;\n\tcontent?: unknown;\n\tcustomType?: string;\n\ttoolName?: string;\n\tname?: string;\n\tinput?: unknown;\n\targs?: unknown;\n\targuments?: unknown;\n\terror?: unknown;\n\tisError?: boolean;\n\tdetails?: unknown;\n\tstopReason?: string;\n};\n\nexport interface WatchdogTurnDeltaInput {\n\tuserPrompt?: string;\n\tincludeUserPrompt?: boolean;\n\tmessages?: unknown[];\n\tevents?: unknown[];\n\tfinalAssistantStop?: boolean;\n}\n\nfunction textFromContent(content: unknown): string {\n\tif (typeof content === \"string\") return content;\n\tif (Array.isArray(content)) {\n\t\treturn content\n\t\t\t.map((block) => {\n\t\t\t\tif (typeof block === \"string\") return block;\n\t\t\t\tif (block && typeof block === \"object\") {\n\t\t\t\t\tconst item = block as {\n\t\t\t\t\t\ttype?: string;\n\t\t\t\t\t\ttext?: unknown;\n\t\t\t\t\t\tthinking?: unknown;\n\t\t\t\t\t\tcontent?: unknown;\n\t\t\t\t\t\tname?: unknown;\n\t\t\t\t\t\tinput?: unknown;\n\t\t\t\t\t\targs?: unknown;\n\t\t\t\t\t\targuments?: unknown;\n\t\t\t\t\t};\n\t\t\t\t\tif (item.type === \"text\" && typeof item.text === \"string\") return item.text;\n\t\t\t\t\tif (item.type === \"thinking\" && typeof item.thinking === \"string\")\n\t\t\t\t\t\treturn [\"Thinking:\", item.thinking].join(\"\\n\");\n\t\t\t\t\tif (item.type === \"toolCall\")\n\t\t\t\t\t\treturn formatToolCall(item.name, item.input ?? item.args ?? item.arguments);\n\t\t\t\t\tif (typeof item.content === \"string\") return item.content;\n\t\t\t\t}\n\t\t\t\treturn \"\";\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\");\n\t}\n\tif (content === undefined || content === null) return \"\";\n\treturn formatValue(content);\n}\n\nfunction formatValue(value: unknown, indent = \"\"): string {\n\tif (typeof value === \"string\") return value;\n\tif (value === undefined) return \"undefined\";\n\tif (value === null || typeof value === \"number\" || typeof value === \"boolean\") return JSON.stringify(value);\n\tif (Array.isArray(value)) {\n\t\treturn value.map((item, _index) => `${indent}- ${formatValue(item, `${indent}  `)}`).join(\"\\n\");\n\t}\n\tif (typeof value === \"object\") {\n\t\tconst lines: string[] = [];\n\t\tfor (const [key, item] of Object.entries(value as Record<string, unknown>)) {\n\t\t\tif (typeof item === \"string\" && item.includes(\"\\n\")) lines.push(`${indent}${key}:\\n${item}`);\n\t\t\telse lines.push(`${indent}${key}: ${formatValue(item, `${indent}  `)}`);\n\t\t}\n\t\treturn lines.join(\"\\n\");\n\t}\n\treturn String(value);\n}\n\nfunction redactEditWriteInput(input: unknown): unknown {\n\tif (Array.isArray(input)) return input.map(redactEditWriteInput);\n\tif (!input || typeof input !== \"object\") return input;\n\tconst sanitized: Record<string, unknown> = {};\n\tfor (const [key, value] of Object.entries(input as Record<string, unknown>)) {\n\t\tsanitized[key] =\n\t\t\t[\"oldText\", \"newText\", \"content\"].includes(key) && typeof value === \"string\"\n\t\t\t\t? `[omitted ${value.length} chars; use tool result diff]`\n\t\t\t\t: redactEditWriteInput(value);\n\t}\n\treturn sanitized;\n}\n\nfunction formatToolArguments(name: string, input: unknown): string {\n\tif (name === \"edit\" || name === \"write\") return formatValue(redactEditWriteInput(input));\n\treturn formatValue(input);\n}\n\nfunction formatToolCall(name: unknown, input: unknown): string {\n\tconst toolName = typeof name === \"string\" && name ? name : \"tool\";\n\treturn [`Tool call: ${toolName}`, \"Arguments:\", formatToolArguments(toolName, input ?? {})].join(\"\\n\");\n}\n\nfunction formatToolResult(\n\tname: string,\n\tcontent: unknown,\n\tdetails: unknown,\n\terror: unknown,\n\tisError: boolean | undefined,\n): string {\n\tconst failed = isError === true || Boolean(error);\n\tconst lines = [`Tool result: ${name}`];\n\tif (failed)\n\t\tlines.push(\n\t\t\t`Error: ${error ? (typeof error === \"string\" ? error : formatValue(error)) : \"tool reported an error\"}`,\n\t\t);\n\tif (\n\t\t!failed &&\n\t\tdetails &&\n\t\ttypeof details === \"object\" &&\n\t\t\"diff\" in details &&\n\t\ttypeof (details as { diff?: unknown }).diff === \"string\"\n\t) {\n\t\tlines.push(\"Diff:\", (details as { diff: string }).diff);\n\t} else {\n\t\tconst body = textFromContent(content);\n\t\tif (body) lines.push(failed ? \"Output:\" : \"Result:\", body);\n\t}\n\treturn lines.join(\"\\n\");\n}\n\nfunction messagesFromEvent(event: unknown): unknown[] {\n\tif (!event || typeof event !== \"object\") return [];\n\tconst input = event as Record<string, unknown>;\n\tif (input.type === \"turn_end\" || input.event === \"turn_end\") {\n\t\treturn [input.message, ...(Array.isArray(input.toolResults) ? input.toolResults : [])].filter(Boolean);\n\t}\n\tif (input.message) return [input.message];\n\tif (input.type === \"tool_execution_start\" || input.event === \"tool_execution_start\") {\n\t\treturn [{ role: \"toolCall\", name: input.toolName ?? input.name, input: input.args ?? input.input }];\n\t}\n\tif (input.type === \"tool_execution_end\" || input.event === \"tool_execution_end\") {\n\t\tconst result =\n\t\t\tinput.result && typeof input.result === \"object\" && !Array.isArray(input.result)\n\t\t\t\t? (input.result as Record<string, unknown>)\n\t\t\t\t: undefined;\n\t\treturn [\n\t\t\t{\n\t\t\t\trole: \"toolResult\",\n\t\t\t\ttoolName: input.toolName ?? input.name,\n\t\t\t\tcontent: result?.content ?? input.content ?? input.result,\n\t\t\t\tdetails: result?.details ?? input.details,\n\t\t\t\terror: input.error,\n\t\t\t\tisError: input.isError,\n\t\t\t},\n\t\t];\n\t}\n\tif (input.type === \"tool_result\" || input.event === \"tool_result\") {\n\t\treturn [\n\t\t\t{\n\t\t\t\trole: \"toolResult\",\n\t\t\t\ttoolName: input.toolName ?? input.name,\n\t\t\t\tcontent: input.content,\n\t\t\t\tdetails: input.details,\n\t\t\t\terror: input.error,\n\t\t\t\tisError: input.isError,\n\t\t\t},\n\t\t];\n\t}\n\treturn [];\n}\n\nexport function formatWatchdogReviewMessage(message: unknown): string | undefined {\n\tif (!message || typeof message !== \"object\") return undefined;\n\tconst input = message as MessageLike;\n\tif (input.role === \"custom\" && input.customType === SUBAGENT_WATCHDOG_WARNING_TYPE) return undefined;\n\tif (input.role === \"assistant\") {\n\t\tconst text = textFromContent(input.content);\n\t\tconst lines = text ? [\"Assistant:\", text] : [\"Assistant: (no text)\"];\n\t\tif (input.stopReason === \"stop\") lines.push(\"Assistant stop: stop\");\n\t\treturn lines.join(\"\\n\");\n\t}\n\tif (input.role === \"toolCall\")\n\t\treturn formatToolCall(input.name ?? input.toolName, input.input ?? input.args ?? input.arguments);\n\tif (input.role === \"toolResult\" || input.role === \"tool\") {\n\t\treturn formatToolResult(\n\t\t\tinput.toolName ?? input.name ?? \"tool\",\n\t\t\tinput.content,\n\t\t\tinput.details,\n\t\t\tinput.error,\n\t\t\tinput.isError,\n\t\t);\n\t}\n\tif (input.role === \"user\") {\n\t\tconst text = textFromContent(input.content);\n\t\treturn text ? [\"User:\", text].join(\"\\n\") : undefined;\n\t}\n\treturn undefined;\n}\n\nexport function formatWatchdogTurnDelta(input: WatchdogTurnDeltaInput): string {\n\tconst sections: string[] = [];\n\tif (input.includeUserPrompt && input.userPrompt?.trim())\n\t\tsections.push([\"User prompt:\", input.userPrompt].join(\"\\n\"));\n\tfor (const message of input.messages ?? []) {\n\t\tconst section = formatWatchdogReviewMessage(message);\n\t\tif (section) sections.push(section);\n\t}\n\tfor (const event of input.events ?? []) {\n\t\tfor (const message of messagesFromEvent(event)) {\n\t\t\tconst section = formatWatchdogReviewMessage(message);\n\t\t\tif (section) sections.push(section);\n\t\t}\n\t}\n\tif (input.finalAssistantStop) sections.push(\"Final assistant stop: stop without tool call\");\n\treturn sections.join(\"\\n\\n---\\n\\n\");\n}\n"]}