add truncation for all tools

This commit is contained in:
Aiden Cline
2026-01-07 02:01:23 -06:00
parent 0ffe496869
commit 6590c1641f
2 changed files with 16 additions and 4 deletions

View File

@@ -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
},