diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 57c038afa..58f451b94 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -36,6 +36,7 @@ export namespace SessionProcessor { let attempt = 0 let needsCompaction = false let stepStart = 0 // kilocode_change + let shouldStopAfterTool = false const result = { get message() { @@ -190,6 +191,10 @@ export namespace SessionProcessor { }, }) + if (value.output.metadata?.stop === true) { + shouldStopAfterTool = true + } + delete toolcalls[value.toolCallId] } break @@ -358,6 +363,7 @@ export namespace SessionProcessor { continue } if (needsCompaction) break + if (shouldStopAfterTool) break } } catch (e: any) { log.error("process", { @@ -424,6 +430,7 @@ export namespace SessionProcessor { if (needsCompaction) return "compact" if (blocked) return "stop" if (input.assistantMessage.error) return "stop" + if (shouldStopAfterTool) return "stop" return "continue" } }, diff --git a/packages/opencode/src/tool/registry.ts b/packages/opencode/src/tool/registry.ts index 5d61cfa48..2d83819bb 100644 --- a/packages/opencode/src/tool/registry.ts +++ b/packages/opencode/src/tool/registry.ts @@ -70,11 +70,25 @@ export namespace ToolRegistry { worktree: Instance.worktree, } as unknown as PluginToolContext const result = await def.execute(args as any, pluginCtx) - const out = await Truncate.output(result, {}, initCtx?.agent) + if (typeof result === "string") { + const out = await Truncate.output(result, {}, initCtx?.agent) + return { + title: id, + output: out.truncated ? out.content : result, + metadata: { truncated: out.truncated, outputPath: out.truncated ? out.outputPath : undefined }, + } + } + if (typeof result !== "object" || result === null || !("output" in result)) + throw new Error(`Tool ${id} returned unexpected value: ${typeof result}`) + const out = await Truncate.output(result.output, {}, initCtx?.agent) return { - title: "", - output: out.truncated ? out.content : result, - metadata: { truncated: out.truncated, outputPath: out.truncated ? out.outputPath : undefined }, + title: result.title ?? id, + output: out.truncated ? out.content : result.output, + metadata: { + ...(result.metadata ?? {}), + truncated: out.truncated, + outputPath: out.truncated ? out.outputPath : undefined, + }, } }, }), diff --git a/packages/plugin/src/tool.ts b/packages/plugin/src/tool.ts index 23aa512d9..cfe3ce362 100644 --- a/packages/plugin/src/tool.ts +++ b/packages/plugin/src/tool.ts @@ -26,10 +26,12 @@ type AskInput = { metadata: { [key: string]: any } } +export type ToolResult = string | { title?: string; output: string; metadata?: Record } + export function tool(input: { description: string args: Args - execute(args: z.infer>, context: ToolContext): Promise + execute(args: z.infer>, context: ToolContext): Promise }) { return input }