feat(hooks): add tool-output-truncator for dynamic context-aware truncation
Refactor grep-output-truncator into a general-purpose tool-output-truncator that applies dynamic truncation to multiple tools based on context window usage. Truncated tools: - Grep, safe_grep (existing) - Glob, safe_glob (new) - lsp_find_references (new) - lsp_document_symbols (new) - lsp_workspace_symbols (new) - lsp_diagnostics (new) - ast_grep_search (new) Uses the new dynamic-truncator utility from shared/ for context-aware output size limits based on remaining context window tokens. 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
38
src/hooks/tool-output-truncator.ts
Normal file
38
src/hooks/tool-output-truncator.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import { createDynamicTruncator } from "../shared/dynamic-truncator"
|
||||
|
||||
const TRUNCATABLE_TOOLS = [
|
||||
"Grep",
|
||||
"safe_grep",
|
||||
"Glob",
|
||||
"safe_glob",
|
||||
"lsp_find_references",
|
||||
"lsp_document_symbols",
|
||||
"lsp_workspace_symbols",
|
||||
"lsp_diagnostics",
|
||||
"ast_grep_search",
|
||||
]
|
||||
|
||||
export function createToolOutputTruncatorHook(ctx: PluginInput) {
|
||||
const truncator = createDynamicTruncator(ctx)
|
||||
|
||||
const toolExecuteAfter = async (
|
||||
input: { tool: string; sessionID: string; callID: string },
|
||||
output: { title: string; output: string; metadata: unknown }
|
||||
) => {
|
||||
if (!TRUNCATABLE_TOOLS.includes(input.tool)) return
|
||||
|
||||
try {
|
||||
const { result, truncated } = await truncator.truncate(input.sessionID, output.output)
|
||||
if (truncated) {
|
||||
output.output = result
|
||||
}
|
||||
} catch {
|
||||
// Graceful degradation - don't break tool execution
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
"tool.execute.after": toolExecuteAfter,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user