Skip to content

Commit 6590c16

Browse files
committed
add truncation for all tools
1 parent 0ffe496 commit 6590c16

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

packages/opencode/src/tool/registry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { CodeSearchTool } from "./codesearch"
2323
import { Flag } from "@/flag/flag"
2424
import { Log } from "@/util/log"
2525
import { LspTool } from "./lsp"
26+
import { Truncate } from "../session/truncation"
2627

2728
export namespace ToolRegistry {
2829
const log = Log.create({ service: "tool.registry" })
@@ -64,10 +65,11 @@ export namespace ToolRegistry {
6465
description: def.description,
6566
execute: async (args, ctx) => {
6667
const result = await def.execute(args as any, ctx)
68+
const out = Truncate.output(result)
6769
return {
6870
title: "",
69-
output: result,
70-
metadata: {},
71+
output: out.truncated ? out.content : result,
72+
metadata: { truncated: out.truncated },
7173
}
7274
},
7375
}),

packages/opencode/src/tool/tool.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import z from "zod"
22
import type { MessageV2 } from "../session/message-v2"
33
import type { Agent } from "../agent/agent"
44
import type { PermissionNext } from "../permission/next"
5+
import { Truncate } from "../session/truncation"
56

67
export namespace Tool {
78
interface Metadata {
@@ -52,7 +53,7 @@ export namespace Tool {
5253
init: async (ctx) => {
5354
const toolInfo = init instanceof Function ? await init(ctx) : init
5455
const execute = toolInfo.execute
55-
toolInfo.execute = (args, ctx) => {
56+
toolInfo.execute = async (args, ctx) => {
5657
try {
5758
toolInfo.parameters.parse(args)
5859
} catch (error) {
@@ -64,7 +65,16 @@ export namespace Tool {
6465
{ cause: error },
6566
)
6667
}
67-
return execute(args, ctx)
68+
const result = await execute(args, ctx)
69+
const truncated = Truncate.output(result.output)
70+
return {
71+
...result,
72+
output: truncated.content,
73+
metadata: {
74+
...result.metadata,
75+
truncated: truncated.truncated,
76+
},
77+
}
6878
}
6979
return toolInfo
7080
},

0 commit comments

Comments
 (0)