Repository navigation
Commit c9e4b8a
committed
fix: tool-call pipeline, crash recovery, and Responses API tool shape
Problem A (service stops working):
- /v1/responses catch block called res.json() unconditionally; after SSE
headers were sent this threw ERR_HTTP_HEADERS_SENT which escaped the async
handler as an unhandled rejection, killing the Node process under the
default --unhandled-rejections=throw. Added res.headersSent guard and
added a process-level unhandledRejection handler in index.js as a safety
net so a single bad request can never bring the whole proxy down.
Problem B (tool calls never fired from pi):
- buildExternalToolRegistry only read tool.function.name; the Responses API
sends a flat shape {type,name,parameters} without a .function wrapper, so
every pi tool was silently dropped. Added normalizeToolDefinition() that
accepts both the Chat Completions nested shape and the flat Responses shape.
Also fixed router.js tool_choice to accept flat {type,name} alongside
{type,function:{name}}.
Problem C (polling truncation):
- pollForAssistantResponse returned as soon as any content or reasoning
appeared, even mid-generation (done=false). Reasoning models emit the
reasoning part first and the text part only after; returning early produced
reasoning-only responses. Now waits for done===true and keeps the last
partial snapshot only as a timeout fallback.
Problem D (streaming first-delta timeout too short):
- DEFAULT_EVENT_FIRST_DELTA_TIMEOUT_MS was hardcoded to 4000ms. DeepSeek
takes ~10-30s before its first token. The event stream consistently timed
out and fell back to polling, which then hit the truncation bug above.
Changed to 30000ms (configurable via OPENCODE2API_EVENT_FIRST_DELTA_TIMEOUT_MS).
Problem E (nested function_calls tag not parsed):
- Models echo the <function_calls> example from the contract reminder and
wrap the actual JSON in a second outer tag, producing:
<function_calls>\n<function_calls>\n{...}\n</function_calls>
rawCallsFromJsonText called JSON.parse(body) which failed because the body
starts with the stray tag, and returned []. Added findFirstJsonValue as a
fallback to scan past the prefix and extract the embedded JSON.
Problem F (upstream error detection):
- collectFromEvents waited out the full first-delta window (30s) before
discovering a MessageAbortedError via polling. Now resolves immediately
when a message.updated event carries info.error.
Additional fixes:
- XML child element arguments (Cline/Roo style): <read><path>a.txt</path></read>
was matched but children were discarded, producing empty arguments. Added
argsFromXmlChildren() that maps child tag names to declared schema properties.
- Contract reminder injected as the last prompt part (just before generation),
not only in the 16KB+ system prompt where it gets buried. Empirically raised
tool call compliance from 50% to 100% in baseline tests.
- finish==='tool' treated as intermediate (not done) in pollForAssistantResponse.
- All changes covered by 128 passing tests (+23 new in this commit).1 parent 67b1e2f commit c9e4b8a
7 files changed
Lines changed: 665 additions & 36 deletions
File tree
- src
- tool-runtime
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
150 | 161 | | |
151 | 162 | | |
152 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
197 | 200 | | |
198 | 201 | | |
199 | 202 | | |
| |||
1078 | 1081 | | |
1079 | 1082 | | |
1080 | 1083 | | |
1081 | | - | |
| 1084 | + | |
1082 | 1085 | | |
1083 | 1086 | | |
1084 | | - | |
| 1087 | + | |
| 1088 | + | |
1085 | 1089 | | |
1086 | 1090 | | |
1087 | 1091 | | |
1088 | 1092 | | |
1089 | 1093 | | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
1090 | 1100 | | |
1091 | 1101 | | |
1092 | 1102 | | |
| |||
1095 | 1105 | | |
1096 | 1106 | | |
1097 | 1107 | | |
1098 | | - | |
| 1108 | + | |
1099 | 1109 | | |
1100 | | - | |
1101 | | - | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
1102 | 1127 | | |
1103 | 1128 | | |
1104 | 1129 | | |
| |||
1112 | 1137 | | |
1113 | 1138 | | |
1114 | 1139 | | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1115 | 1144 | | |
1116 | 1145 | | |
1117 | 1146 | | |
1118 | 1147 | | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
1119 | 1157 | | |
1120 | 1158 | | |
1121 | 1159 | | |
| |||
1236 | 1274 | | |
1237 | 1275 | | |
1238 | 1276 | | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
1239 | 1293 | | |
1240 | 1294 | | |
1241 | 1295 | | |
| |||
1523 | 1577 | | |
1524 | 1578 | | |
1525 | 1579 | | |
1526 | | - | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
1527 | 1587 | | |
1528 | 1588 | | |
1529 | 1589 | | |
| |||
2239 | 2299 | | |
2240 | 2300 | | |
2241 | 2301 | | |
2242 | | - | |
| 2302 | + | |
| 2303 | + | |
| 2304 | + | |
2243 | 2305 | | |
2244 | 2306 | | |
2245 | 2307 | | |
| |||
2679 | 2741 | | |
2680 | 2742 | | |
2681 | 2743 | | |
2682 | | - | |
| 2744 | + | |
2683 | 2745 | | |
2684 | | - | |
| 2746 | + | |
| 2747 | + | |
| 2748 | + | |
| 2749 | + | |
| 2750 | + | |
| 2751 | + | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
| 2755 | + | |
| 2756 | + | |
| 2757 | + | |
| 2758 | + | |
| 2759 | + | |
| 2760 | + | |
| 2761 | + | |
| 2762 | + | |
2685 | 2763 | | |
2686 | 2764 | | |
2687 | 2765 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
73 | 85 | | |
74 | 86 | | |
75 | 87 | | |
| |||
133 | 145 | | |
134 | 146 | | |
135 | 147 | | |
136 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
137 | 158 | | |
138 | 159 | | |
139 | 160 | | |
| |||
270 | 291 | | |
271 | 292 | | |
272 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
273 | 354 | | |
274 | 355 | | |
275 | 356 | | |
| |||
306 | 387 | | |
307 | 388 | | |
308 | 389 | | |
309 | | - | |
| 390 | + | |
310 | 391 | | |
311 | 392 | | |
312 | 393 | | |
| |||
366 | 447 | | |
367 | 448 | | |
368 | 449 | | |
369 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
370 | 455 | | |
371 | 456 | | |
372 | 457 | | |
| |||
399 | 484 | | |
400 | 485 | | |
401 | 486 | | |
| 487 | + | |
402 | 488 | | |
403 | 489 | | |
404 | 490 | | |
405 | 491 | | |
406 | 492 | | |
407 | | - | |
| 493 | + | |
408 | 494 | | |
409 | 495 | | |
410 | 496 | | |
| |||
0 commit comments