Skip to content

Commit cd6d789

Browse files
committed
fix(session): use jsonrepair lib for truncated tool call JSON
Replace ~90 lines of custom JSON repair logic with jsonrepair library. This handles unterminated strings, missing braces/brackets, trailing commas, and other common truncation issues from LLMs like Kimi k2.6 and Qwen 3.5 running on vLLM. Refs: anomalyco#24177, anomalyco#20650, anomalyco#20786
1 parent 8299fb3 commit cd6d789

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎bun.lock‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"ignore": "7.0.5",
150150
"immer": "11.1.4",
151151
"jsonc-parser": "3.3.1",
152+
"jsonrepair": "3.14.0",
152153
"mime-types": "3.0.2",
153154
"minimatch": "10.0.3",
154155
"npm-package-arg": "13.0.2",

‎packages/opencode/src/session/llm.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Log from "@opencode-ai/core/util/log"
33
import { Context, Effect, Layer, Record } from "effect"
44
import * as Stream from "effect/Stream"
55
import { streamText, wrapLanguageModel, type ModelMessage, type Tool, tool, jsonSchema } from "ai"
6+
import { jsonrepair } from "jsonrepair"
67
import { mergeDeep } from "remeda"
78
import { GitLabWorkflowLanguageModel } from "gitlab-ai-provider"
89
import { ProviderTransform } from "@/provider/transform"
@@ -351,6 +352,27 @@ const live: Layer.Layer<
351352
toolName: lower,
352353
}
353354
}
355+
356+
const toolInput = failed.toolCall.input
357+
if (typeof toolInput === "string" && toolInput.trim().startsWith("{")) {
358+
try {
359+
const repaired = jsonrepair(toolInput)
360+
if (repaired !== toolInput) {
361+
l.info("repaired truncated tool call json", {
362+
tool: failed.toolCall.toolName,
363+
original: toolInput,
364+
repaired,
365+
})
366+
return {
367+
...failed.toolCall,
368+
input: repaired,
369+
}
370+
}
371+
} catch {
372+
// jsonrepair failed, fall through to invalid tool handler
373+
}
374+
}
375+
354376
return {
355377
...failed.toolCall,
356378
input: JSON.stringify({

0 commit comments

Comments
 (0)