Skip to content

The open source coding agent.

TypeScript

add truncation for all tools

Aiden Cline6590c1641f34
diff --git a/packages/opencode/src/tool/registry.ts b/packages/opencode/src/tool/registry.ts
index db515284741..bca6626db70 100644
--- a/packages/opencode/src/tool/registry.ts
+++ b/packages/opencode/src/tool/registry.ts
@@ -23,6 +23,7 @@ import { CodeSearchTool } from "./codesearch"
import { Flag } from "@/flag/flag"
import { Log } from "@/util/log"
import { LspTool } from "./lsp"
+import { Truncate } from "../session/truncation"
export namespace ToolRegistry {
const log = Log.create({ service: "tool.registry" })
@@ -64,10 +65,11 @@ export namespace ToolRegistry {
description: def.description,
execute: async (args, ctx) => {
const result = await def.execute(args as any, ctx)
+ const out = Truncate.output(result)
return {
title: "",
- output: result,
- metadata: {},
+ output: out.truncated ? out.content : result,
+ metadata: { truncated: out.truncated },
}
},
}),
diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts
index 434a3d42660..060da0ae763 100644
--- a/packages/opencode/src/tool/tool.ts
+++ b/packages/opencode/src/tool/tool.ts
@@ -2,6 +2,7 @@ import z from "zod"
import type { MessageV2 } from "../session/message-v2"
import type { Agent } from "../agent/agent"
import type { PermissionNext } from "../permission/next"
+import { Truncate } from "../session/truncation"
export namespace Tool {
interface Metadata {
@@ -52,7 +53,7 @@ export namespace Tool {
init: async (ctx) => {
const toolInfo = init instanceof Function ? await init(ctx) : init
const execute = toolInfo.execute
- toolInfo.execute = (args, ctx) => {
+ toolInfo.execute = async (args, ctx) => {
try {
toolInfo.parameters.parse(args)
} catch (error) {
@@ -64,7 +65,16 @@ export namespace Tool {
{ cause: error },
)
}
- return execute(args, ctx)
+ const result = await execute(args, ctx)
+ const truncated = Truncate.output(result.output)
+ return {
+ ...result,
+ output: truncated.content,
+ metadata: {
+ ...result.metadata,
+ truncated: truncated.truncated,
+ },
+ }
}
return toolInfo
},