{"version":3,"file":"tool-actions.d.ts","sourceRoot":"","sources":["../../../src/watchdog/tool-actions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAElE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAOlD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAOxD,UAAU,kBAAkB;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAwGD,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,kBAAkB,EAC1B,GAAG,EAAE,gBAAgB,EACrB,OAAO,CAAC,EAAE,mBAAmB,GAC3B,eAAe,CAAC,OAAO,CAAC,CA6C1B;AAED,eAAO,MAAM,qBAAqB,kGAKxB,CAAC;AACX,eAAO,MAAM,wBAAwB,iFAA2C,CAAC","sourcesContent":["import type { AgentToolResult } from \"@lpb-work/pi-agent-core\";\nimport type { ExtensionContext } from \"@lpb-work/pi-coding-agent\";\nimport { THINKING_LEVELS, type ThinkingLevel } from \"../shared/model-info.ts\";\nimport type { Details } from \"../shared/types.ts\";\nimport {\n\tparseWatchdogThinkingInput,\n\trecommendStrongWatchdogModel,\n\tresolveWatchdogModelInput,\n} from \"./model-selection.ts\";\nimport { buildWatchdogStatus } from \"./register-main.ts\";\nimport type { MainWatchdogRuntime } from \"./runtime.ts\";\nimport {\n\ttype WatchdogModelSettingsTarget,\n\ttype WatchdogSettingsWriteScope,\n\twriteWatchdogModelSettings,\n} from \"./settings.ts\";\n\ninterface WatchdogToolParams {\n\taction?: string;\n\tscope?: string;\n\ttarget?: string;\n\tagent?: string;\n\tmodel?: string;\n\tthinking?: string | false;\n\tcwd?: string;\n}\n\nfunction result(text: string, isError = false): AgentToolResult<Details> {\n\treturn {\n\t\tcontent: [{ type: \"text\", text }],\n\t\t...(isError ? { isError: true } : {}),\n\t\tdetails: { mode: \"management\", results: [] },\n\t};\n}\n\nfunction messageFromError(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\nfunction parseScope(raw: string | undefined): \"session\" | WatchdogSettingsWriteScope {\n\tif (raw === undefined || raw === \"session\") return \"session\";\n\tif (raw === \"user\" || raw === \"project\") return raw;\n\tthrow new Error(\"watchdog.configure scope must be 'session', 'user', or 'project'.\");\n}\n\nfunction parseTarget(params: WatchdogToolParams): WatchdogModelSettingsTarget {\n\tconst target = params.target ?? \"main\";\n\tif (target === \"main\") return { kind: \"main\" };\n\tif (target === \"children\") return { kind: \"children\" };\n\tif (target === \"child\") {\n\t\tif (!params.agent?.trim()) throw new Error(\"watchdog.configure target='child' requires agent.\");\n\t\treturn { kind: \"child\", agent: params.agent.trim() };\n\t}\n\tthrow new Error(\"watchdog.configure target must be 'main', 'children', or 'child'.\");\n}\n\nfunction parseThinking(raw: string | false | undefined): ThinkingLevel | false | null | undefined {\n\tif (raw === undefined) return undefined;\n\tif (raw === \"inherit\") return null;\n\treturn parseWatchdogThinkingInput(raw, \"watchdog.configure thinking\") ?? undefined;\n}\n\nfunction resolveConfiguredValue(\n\tctx: ExtensionContext,\n\tparams: WatchdogToolParams,\n): { model?: string | null; thinking?: ThinkingLevel | false | null; description: string } {\n\tconst thinking = parseThinking(params.thinking);\n\tconst rawModel = params.model?.trim();\n\tif (!rawModel) {\n\t\tif (thinking === undefined) throw new Error(\"watchdog.configure requires model, thinking, or both.\");\n\t\treturn {\n\t\t\tthinking,\n\t\t\tdescription: `thinking ${thinking === null ? \"inherit\" : thinking === false ? \"off\" : thinking}`,\n\t\t};\n\t}\n\tif (rawModel === \"inherit\") return { model: null, thinking: thinking ?? null, description: \"inherit\" };\n\tif (rawModel === \"recommended\") {\n\t\tconst recommendation = recommendStrongWatchdogModel(ctx);\n\t\treturn {\n\t\t\tmodel: recommendation.model,\n\t\t\tthinking: recommendation.thinking,\n\t\t\tdescription: `${recommendation.model}:${recommendation.thinking}`,\n\t\t};\n\t}\n\tconst resolved = resolveWatchdogModelInput(ctx, rawModel);\n\treturn {\n\t\tmodel: resolved.model,\n\t\tthinking: resolved.thinking ?? thinking,\n\t\tdescription: `${resolved.model}${(resolved.thinking ?? thinking) ? `:${resolved.thinking ?? thinking}` : \"\"}`,\n\t};\n}\n\nfunction buildRecommendationText(ctx: ExtensionContext): string {\n\tconst recommendation = recommendStrongWatchdogModel(ctx);\n\treturn [\n\t\t\"Subagent watchdog recommended model\",\n\t\t`Recommended: ${recommendation.model}:${recommendation.thinking}`,\n\t\t`Reason: ${recommendation.reason}`,\n\t\t'Apply temporarily with subagent({ action: \"watchdog.configure\", scope: \"session\", model: \"recommended\" }).',\n\t\t'Persist with scope: \"project\" or scope: \"user\" only when the user asks for that scope.',\n\t].join(\"\\n\");\n}\n\nfunction buildCheckText(runtime: MainWatchdogRuntime | undefined, ctx: ExtensionContext): string {\n\tif (!runtime) return \"Subagent watchdog runtime is unavailable.\";\n\tconst snapshot = runtime.getSnapshot(ctx.cwd);\n\tif (!snapshot.configOk)\n\t\treturn [\n\t\t\t\"Subagent watchdog config check\",\n\t\t\t\"Config errors:\",\n\t\t\t...snapshot.errors.map((error) => `- ${error.message}`),\n\t\t].join(\"\\n\");\n\tconst lines = [\"Subagent watchdog config check\", \"Config: ok\"];\n\tif (snapshot.config.main.model) {\n\t\tconst resolved = resolveWatchdogModelInput(ctx, snapshot.config.main.model);\n\t\tlines.push(`Main model: ${resolved.model} auth ok`);\n\t} else {\n\t\tlines.push(\"Main model: current session\");\n\t}\n\tlines.push(`LSP diagnostics: ${snapshot.lsp.enabled ? \"on\" : \"off\"} · ${snapshot.lsp.status}`);\n\ttry {\n\t\tconst recommendation = recommendStrongWatchdogModel(ctx);\n\t\tlines.push(`Recommended strong watchdog: ${recommendation.model}:${recommendation.thinking}`);\n\t} catch (error) {\n\t\tlines.push(`Recommended strong watchdog unavailable: ${messageFromError(error)}`);\n\t}\n\treturn lines.join(\"\\n\");\n}\n\nexport function handleWatchdogToolAction(\n\taction: string,\n\tparams: WatchdogToolParams,\n\tctx: ExtensionContext,\n\truntime?: MainWatchdogRuntime,\n): AgentToolResult<Details> {\n\ttry {\n\t\tif (action === \"watchdog.status\") {\n\t\t\tif (!runtime) return result(\"Subagent watchdog runtime is unavailable.\", true);\n\t\t\treturn result(buildWatchdogStatus(runtime.getSnapshot(ctx.cwd), ctx));\n\t\t}\n\t\tif (action === \"watchdog.recommend-model\") return result(buildRecommendationText(ctx));\n\t\tif (action === \"watchdog.check\") return result(buildCheckText(runtime, ctx));\n\t\tif (action !== \"watchdog.configure\") return result(`Unknown watchdog action: ${action}`, true);\n\n\t\tconst scope = parseScope(params.scope);\n\t\tconst target = parseTarget(params);\n\t\tconst value = resolveConfiguredValue(ctx, params);\n\t\tif (scope === \"session\") {\n\t\t\tif (!runtime) return result(\"Subagent watchdog runtime is unavailable.\", true);\n\t\t\tif (target.kind !== \"main\")\n\t\t\t\treturn result(\"Session-scoped watchdog.configure currently supports target='main' only.\", true);\n\t\t\truntime.setSessionModel({ model: value.model, thinking: value.thinking }, ctx.cwd);\n\t\t\treturn result(\n\t\t\t\t[\n\t\t\t\t\t`Subagent watchdog session model configured: ${value.description}.`,\n\t\t\t\t\t\"No settings files were changed.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\tbuildWatchdogStatus(runtime.getSnapshot(ctx.cwd), ctx),\n\t\t\t\t].join(\"\\n\"),\n\t\t\t);\n\t\t}\n\n\t\tconst settingsPath = writeWatchdogModelSettings({\n\t\t\tscope,\n\t\t\tcwd: ctx.cwd,\n\t\t\ttarget,\n\t\t\tmodel: value.model,\n\t\t\tthinking: value.thinking,\n\t\t});\n\t\truntime?.refreshConfig(ctx.cwd);\n\t\tconst targetLabel = target.kind === \"child\" ? `child ${target.agent}` : target.kind;\n\t\treturn result(\n\t\t\t[`Subagent watchdog ${targetLabel} model configured: ${value.description}.`, `Updated: ${settingsPath}`].join(\n\t\t\t\t\"\\n\",\n\t\t\t),\n\t\t);\n\t} catch (error) {\n\t\treturn result(`Subagent watchdog action failed: ${messageFromError(error)}`, true);\n\t}\n}\n\nexport const WATCHDOG_TOOL_ACTIONS = [\n\t\"watchdog.status\",\n\t\"watchdog.check\",\n\t\"watchdog.configure\",\n\t\"watchdog.recommend-model\",\n] as const;\nexport const WATCHDOG_THINKING_VALUES = [\"inherit\", ...THINKING_LEVELS] as const;\n"]}