-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeToolCallParser.ts
More file actions
2177 lines (1972 loc) · 71.9 KB
/
Copy pathNativeToolCallParser.ts
File metadata and controls
2177 lines (1972 loc) · 71.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { parseJSON } from "partial-json"
import { distance } from "fastest-levenshtein"
import { type ToolName, toolNames, type FileEntry, STUB_ARGUMENTS_JSON_PARAM } from "@shofer/types"
import { type ToolUse, type McpToolUse, type ToolParamName, type NativeToolArgs, toolParamNames } from "@shofer/types"
import { customToolRegistry } from "../custom-tools/index.js"
import { resolveToolAlias } from "../tools/tool-aliases.js"
import type {
ApiStreamToolCallStartChunk,
ApiStreamToolCallDeltaChunk,
ApiStreamToolCallEndChunk,
} from "../api/transform/stream.js"
import { MCP_TOOL_PREFIX, MCP_TOOL_SEPARATOR, parseMcpToolName, normalizeMcpToolName } from "../utils/mcp-name.js"
/**
* Helper type to extract properly typed native arguments for a given tool.
* Returns the type from NativeToolArgs if the tool is defined there, otherwise never.
*/
type NativeArgsFor<TName extends ToolName> = TName extends keyof NativeToolArgs ? NativeToolArgs[TName] : never
import { isPrivateLmTool } from "../tools/private-tool-registry.js"
import { webviewLog } from "../logging/subsystems.js"
/**
* Find the closest matching tool name from a list of candidates using
* Levenshtein distance. Returns the candidate with the smallest edit
* distance, or undefined if the candidates list is empty.
*/
function findClosestToolName(haystack: string, candidates: readonly string[]): string | undefined {
if (candidates.length === 0) return undefined
let best = candidates[0]!
let bestDist = distance(haystack, best)
for (let i = 1; i < candidates.length; i++) {
const dist = distance(haystack, candidates[i]!)
if (dist < bestDist) {
bestDist = dist
best = candidates[i]!
}
}
return best
}
/**
* Parser for native tool calls (OpenAI-style function calling).
* Converts native tool call format to ToolUse format for compatibility
* with existing tool execution infrastructure.
*
* For tools with refactored parsers (e.g., read_file), this parser provides
* typed arguments via nativeArgs. Tool-specific handlers should consume
* nativeArgs directly rather than relying on synthesized legacy params.
*/
/**
* Event types returned from raw chunk processing.
*/
export type ToolCallStreamEvent = ApiStreamToolCallStartChunk | ApiStreamToolCallDeltaChunk | ApiStreamToolCallEndChunk
/**
* Parser for native tool calls (OpenAI-style function calling).
* Converts native tool call format to ToolUse format for compatibility
* with existing tool execution infrastructure.
*
* For tools with refactored parsers (e.g., read_file), this parser provides
* typed arguments via nativeArgs. Tool-specific handlers should consume
* nativeArgs directly rather than relying on synthesized legacy params.
*
* This class also handles raw tool call chunk processing, converting
* provider-level raw chunks into start/delta/end events.
*
* **One parser INSTANCE per streaming task.** The parse itself is pure and
* stays static (`parseToolCall` and its helpers), but the stream-assembly half
* — `processRawChunk` and the `startStreamingToolCall` / `processStreamingChunk`
* / `finalizeStreamingToolCall` trio — carries state that is meaningful only
* within ONE stream and must never be shared. The wire `index` an
* OpenAI-compatible provider stamps on each argument fragment restarts at 0 for
* every stream, and after the first fragment a chunk carries nothing else: no
* id, no name. So a tracker shared by two concurrently streaming tasks — which
* a headless worker running many tasks per process has — collides on `index`,
* and the collision does not drop the second stream's fragments: it re-emits
* them under the FIRST stream's tool-call id, splicing one tool call's
* arguments character-by-character through the other's. `Task` therefore owns
* one of these for its lifetime (`Task.nativeToolCallParser`), and a provider
* that needs to know a tool call ended tracks the ids it saw itself rather than
* reading another task's assembly state.
*/
/**
* A single firing of a silent recovery layer (alias coercion, path recovery,
* etc.). `layerId` names the layer so its hit-rate/correctness can be tracked
* and the layer retired if it stops earning its keep (audit: tool-call-recovery).
*/
export interface ToolRecoveryRecord {
layerId: string
tool: string
[key: string]: unknown
}
/**
* A tool call carried the stub escape hatch (`arguments_json`) but its payload
* was not a JSON object. Distinguished from an ordinary JSON failure so the
* error handed back to the model names the property it must fix, instead of
* blaming the outer arguments — which parsed fine, and re-emitting which would
* reproduce the same failure.
*/
export class StubArgumentsJsonError extends Error {
constructor(message: string) {
super(message)
this.name = "StubArgumentsJsonError"
}
}
export class NativeToolCallParser {
/** Stores the last parse error so callers can include specifics in error messages. */
public static lastParseError: string | null = null
/** Read and clear the last parse error. */
public static consumeLastParseError(): string | null {
const err = this.lastParseError
this.lastParseError = null
return err
}
/**
* Silent recovery-layer firings recorded during the most recent parse, so the
* caller (which has model/task context) can emit telemetry for them. The parser
* is static and model-agnostic, so it records facts here rather than capturing
* telemetry directly — mirroring the {@link lastParseError} pattern. Drained per
* parse via {@link consumeRecoveries}.
*/
public static lastRecoveries: ToolRecoveryRecord[] = []
/** Read and clear the recovery-layer firings recorded since the last drain. */
public static consumeRecoveries(): ToolRecoveryRecord[] {
const recoveries = this.lastRecoveries
this.lastRecoveries = []
return recoveries
}
private static recordRecovery(record: ToolRecoveryRecord): void {
this.lastRecoveries.push(record)
}
/**
* Per-tool-call argument accumulation for ONE stream (keyed by tool call id).
*
* Instance state, not static: see the class docstring's "one parser per
* task" note. `name` is a string to accommodate dynamic MCP tools
* (`mcp--serverName--toolName`).
*/
private readonly streamingToolCalls = new Map<
string,
{
id: string
name: string
argumentsAccumulator: string
}
>()
/**
* Raw chunk tracking for ONE stream, keyed by the wire `index` the provider
* assigns to each tool call within that stream.
*
* The index restarts at 0 for every stream, so this map is only meaningful
* scoped to a single stream — hence instance state.
*/
private readonly rawChunkTracker = new Map<
number,
{
id: string
name: string
hasStarted: boolean
deltaBuffer: string[]
}
>()
private static coerceOptionalBoolean(value: unknown): boolean | undefined {
if (typeof value === "boolean") {
return value
}
if (typeof value === "string") {
const lower = value.trim().toLowerCase()
if (lower === "true") {
return true
}
if (lower === "false") {
return false
}
}
return undefined
}
/**
* Process a raw tool call chunk from the API stream.
* Handles tracking, buffering, and emits start/delta/end events.
*
* This is the entry point for providers that emit tool_call_partial chunks.
* Returns an array of events to be processed by the consumer.
*/
public processRawChunk(chunk: {
index: number
id?: string
name?: string
arguments?: string
}): ToolCallStreamEvent[] {
const events: ToolCallStreamEvent[] = []
const { index, id, name, arguments: args } = chunk
let tracked = this.rawChunkTracker.get(index)
// Initialize new tool call tracking when we receive an id
if (id && !tracked) {
tracked = {
id,
name: name || "",
hasStarted: false,
deltaBuffer: [],
}
this.rawChunkTracker.set(index, tracked)
}
if (!tracked) {
return events
}
// Update name if present in chunk and not yet set
if (name) {
tracked.name = name
}
// Emit start event when we have the name
if (!tracked.hasStarted && tracked.name) {
events.push({
type: "tool_call_start",
id: tracked.id,
name: tracked.name,
})
tracked.hasStarted = true
// Flush buffered deltas
for (const bufferedDelta of tracked.deltaBuffer) {
events.push({
type: "tool_call_delta",
id: tracked.id,
delta: bufferedDelta,
})
}
tracked.deltaBuffer = []
}
// Emit delta event for argument chunks
if (args) {
if (tracked.hasStarted) {
events.push({
type: "tool_call_delta",
id: tracked.id,
delta: args,
})
} else {
tracked.deltaBuffer.push(args)
}
}
return events
}
/**
* Finalize any remaining tool calls that weren't explicitly ended.
* Should be called at the end of stream processing.
*/
public finalizeRawChunks(): ToolCallStreamEvent[] {
const events: ToolCallStreamEvent[] = []
if (this.rawChunkTracker.size > 0) {
for (const [, tracked] of this.rawChunkTracker.entries()) {
if (tracked.hasStarted) {
events.push({
type: "tool_call_end",
id: tracked.id,
})
}
}
this.rawChunkTracker.clear()
}
return events
}
/**
* Clear this task's raw chunk tracking state.
* Should be called when a new API request starts on the SAME task — it must
* not be reachable for any other, which is what owning the parser gives.
*/
public clearRawChunkState(): void {
this.rawChunkTracker.clear()
}
/**
* Start streaming a new tool call.
* Initializes tracking for incremental argument parsing.
* Accepts string to support both ToolName and dynamic MCP tools (mcp--serverName--toolName).
*/
public startStreamingToolCall(id: string, name: string): void {
this.streamingToolCalls.set(id, {
id,
name,
argumentsAccumulator: "",
})
}
/**
* Clear this task's streaming tool call state.
* Should be called when a new API request starts on the SAME task, to
* prevent leaking accumulators from interrupted streams.
*/
public clearAllStreamingToolCalls(): void {
this.streamingToolCalls.clear()
}
/**
* Check if there are any active streaming tool calls.
* Useful for debugging and testing.
*/
public hasActiveStreamingToolCalls(): boolean {
return this.streamingToolCalls.size > 0
}
/**
* Process a chunk of JSON arguments for a streaming tool call.
* Uses partial-json-parser to extract values from incomplete JSON immediately.
* Returns a partial ToolUse with currently parsed parameters.
*/
public processStreamingChunk(id: string, chunk: string): ToolUse | null {
const toolCall = this.streamingToolCalls.get(id)
if (!toolCall) {
return null
}
// Accumulate the JSON string
toolCall.argumentsAccumulator += chunk
// For dynamic MCP tools, we don't return partial updates - wait for final
const mcpPrefix = MCP_TOOL_PREFIX + MCP_TOOL_SEPARATOR
if (toolCall.name.startsWith(mcpPrefix)) {
return null
}
// Parse whatever we can from the incomplete JSON!
// partial-json-parser extracts partial values (strings, arrays, objects) immediately
try {
// Resolve tool alias to canonical name
const resolvedName = resolveToolAlias(toolCall.name) as ToolName
// A stubbed tool's arguments may arrive JSON-encoded inside
// `arguments_json`; unwrap leniently so partial rendering sees the same
// keys a direct-arguments call would. Still-truncated inner JSON yields
// the wrapper unchanged, which renders as "no arguments yet".
const partialArgs = NativeToolCallParser.unwrapStubArguments(
parseJSON(toolCall.argumentsAccumulator),
resolvedName,
true,
)
NativeToolCallParser.normalizeArgAliases(partialArgs)
// Preserve original name if it differs from resolved (i.e., it was an alias)
const originalName = toolCall.name !== resolvedName ? toolCall.name : undefined
// Create partial ToolUse with extracted values
return NativeToolCallParser.createPartialToolUse(
toolCall.id,
resolvedName,
partialArgs || {},
true, // partial
originalName,
)
} catch {
// Even partial-json-parser can fail on severely malformed JSON
// Return null and wait for next chunk
return null
}
}
/**
* Finalize a streaming tool call.
* Parses the complete JSON and returns the final ToolUse or McpToolUse.
*/
public finalizeStreamingToolCall(id: string): ToolUse | McpToolUse | null {
const toolCall = this.streamingToolCalls.get(id)
if (!toolCall) {
NativeToolCallParser.lastParseError = `Unknown streaming tool call ID "${id}" — may have been finalized already or never started`
return null
}
// Parse the complete accumulated JSON
// Cast to any for the name since parseToolCall handles both ToolName and dynamic MCP tools
const finalToolUse = NativeToolCallParser.parseToolCall({
id: toolCall.id,
name: toolCall.name as ToolName,
arguments: toolCall.argumentsAccumulator,
})
// Clean up streaming state
this.streamingToolCalls.delete(id)
return finalToolUse
}
/**
* Some models (particularly vscode-lm with composite shofer/* models) leak
* XML-style <parameter> tags into JSON string values when the parameter
* list is complex. This is most commonly observed with apply_diff, where
* the model embeds a trailing "\n<parameter name=\"path\" string=\"true\">PATH"
* suffix inside the `diff` string value instead of emitting `path` as a
* separate JSON key. The JSON is structurally valid ({ "diff": "content" }),
* so JSON.parse succeeds, but the `path` guard in the parser switch case
* then fails because `args.path` is undefined.
*
* This helper attempts to recover a `path` from the suffix of a string value
* that ends with the proprietary <parameter> leak pattern. Returns the
* extracted path and the sanitized string, or null if no leak is detected.
*/
private static extractPathFromXMLLeak(value: unknown): { path: string; sanitized: string } | null {
if (typeof value !== "string") return null
// Pattern: newline + <parameter name="path" string="true">VALUE at end of string
// The closing </parameter> may or may not be present.
// Tolerate corrupted tag prefixes: vscode-lm / deepseek-v4-pro sometimes
// substitute Unicode box-drawing junk (U+FF5C, U+2BFF, etc.) for the
// expected "<" and subsequent characters before "parameter". The .*?
// quantifier lazily skips any arbitrary bytes between "<" and the
// literal "parameter" keyword, which is the only anchoring token.
//
// Known limitations:
// - A corrupted prefix that spans multiple lines is not recovered
// (no dotAll flag, so "." does not match newlines). The observed
// corruption is single-line, so this is acceptable.
// - Theoretically, if a diff SEARCH/REPLACE block legitimately ends
// with text matching "<parameter name=\"path\" string=\"true\">VALUE",
// the recovery could false-match. In practice the "$" end-of-string
// anchor and the ">>>>>>> REPLACE\n" structural barrier make this
// extremely unlikely. No false positives have been observed.
const match = value.match(/\n<.*?parameter\s+name="path"\s+string="true">([^\n<]+)\s*(?:<\/parameter>)?\s*$/)
if (!match) return null
const extractedPath = match[1]!.trim()
if (!extractedPath) return null
const sanitized = value.slice(0, match.index!)
return { path: extractedPath, sanitized }
}
/**
* Detect whether a diff string value has a leaked `:path` suffix —
* the pattern where the model appends "\n:path\nVALUE" after the last
* ">>>>>>> REPLACE" barrier instead of emitting `path` as a separate
* JSON key. This is a boolean detector (not a recovery extractor) —
* it returns true to trigger an actionable rejection that tells the
* model exactly what went wrong, so it self-corrects on the next call.
*
* Observed with: zhipu/glm-5.2 via the shofer proxy.
*
* The pattern is structurally unambiguous: the ":path" marker appears
* after the last ">>>>>>> REPLACE" barrier, which is the structural end
* of a well-formed diff. No legitimate diff content follows that barrier.
*/
private static detectColonLeak(value: unknown): boolean {
if (typeof value !== "string") return false
const lastReplaceIdx = value.lastIndexOf(">>>>>>> REPLACE")
if (lastReplaceIdx === -1) return false
const afterReplace = value.slice(lastReplaceIdx)
// Match: ">>>>>>> REPLACE" followed by optional whitespace/newlines,
// then ":path", then newline(s), then a non-empty value to end of string.
return />>>>>>> REPLACE\s*\n:path\s*\n.+$/s.test(afterReplace)
}
/**
* Unwrap the stub escape hatch: a tool call whose arguments are exactly
* `{ arguments_json: "<json object>" }` carries its real arguments inside that
* string, and everything downstream — the alias normalizer, the per-tool arg
* builders, the missing-field check, a plugin tool's Zod parse, an MCP server's
* own validation — must see the UNWRAPPED object. On-demand schema loading
* declares the property on every stub because a provider that decodes tool
* arguments under a grammar built from the declared schema can emit nothing
* else (`prompts/tools/tool-stubs.ts`).
*
* Applied to EVERY tool call, not only to tools stubbed in this request: a
* model that learned the pattern earlier in the conversation keeps using it
* after a tool's schema is no longer stubbed, and the parser has no view of
* which tier a name was in. That is safe because the trigger is unambiguous —
* the property is the SOLE key, its value is a string, and it decodes to a JSON
* object. No native, plugin or bundled MCP tool declares a parameter by that
* name.
*
* Three deliberate non-unwraps:
* - **alongside other keys** — ambiguous (is it a wrapper or a real argument?),
* so the object passes through untouched and execution-side validation
* speaks;
* - **a non-string value** — not the hatch;
* - **an empty string** — read as "no arguments", i.e. `{}`, which a zero-arg
* tool may legitimately produce under a constrained decoder; a tool that does
* need arguments then raises its ordinary missing-parameter error.
*
* @param partial Streaming: the inner string is still arriving, so it is parsed
* leniently and a failure yields the wrapper unchanged rather than an error —
* a half-streamed hatch must render as "nothing decided yet", never as a
* broken call. The authoritative unwrap is the final one.
* @throws StubArgumentsJsonError when a complete `arguments_json` is not a JSON
* object, so the model gets an error naming the property rather than one
* blaming the outer arguments, which were well-formed.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mirrors JSON.parse's `any`; every caller indexes the result by tool-specific keys.
private static unwrapStubArguments(args: any, toolName: string, partial = false): any {
if (!args || typeof args !== "object" || Array.isArray(args)) return args
const keys = Object.keys(args as Record<string, unknown>)
if (keys.length !== 1 || keys[0] !== STUB_ARGUMENTS_JSON_PARAM) return args
const encoded = (args as Record<string, unknown>)[STUB_ARGUMENTS_JSON_PARAM]
if (typeof encoded !== "string") return args
if (encoded.trim() === "") return {}
if (partial) {
try {
const optimistic = parseJSON(encoded)
return optimistic && typeof optimistic === "object" && !Array.isArray(optimistic) ? optimistic : args
} catch {
return args
}
}
let decoded: unknown
try {
decoded = JSON.parse(encoded)
} catch (error) {
throw new StubArgumentsJsonError(
`The '${STUB_ARGUMENTS_JSON_PARAM}' string for tool '${toolName}' is not valid JSON: ` +
`${error instanceof Error ? error.message : String(error)}. It must contain a JSON OBJECT of this ` +
`tool's real arguments — e.g. {"${STUB_ARGUMENTS_JSON_PARAM}": "{\\"path\\": \\"src/app.ts\\"}"} — ` +
`with every inner quote escaped. Re-emit the call with a valid JSON object in ` +
`'${STUB_ARGUMENTS_JSON_PARAM}', or with the arguments passed directly. Received (truncated): ` +
`${JSON.stringify(encoded.slice(0, 200))}`,
)
}
if (!decoded || typeof decoded !== "object" || Array.isArray(decoded)) {
throw new StubArgumentsJsonError(
`The '${STUB_ARGUMENTS_JSON_PARAM}' string for tool '${toolName}' decoded to ` +
`${Array.isArray(decoded) ? "an array" : `a ${decoded === null ? "null" : typeof decoded}`}, not a ` +
`JSON object. It must encode an OBJECT mapping this tool's parameter names to their values — ` +
`e.g. {"${STUB_ARGUMENTS_JSON_PARAM}": "{\\"path\\": \\"src/app.ts\\"}"}. Call describe_tools for ` +
`the contract if you do not have it.`,
)
}
this.recordRecovery({
layerId: "stub_arguments_json",
tool: toolName,
argCount: Object.keys(decoded as Record<string, unknown>).length,
})
return decoded
}
/**
* Common argument-name aliases that some models emit instead of Shofer's
* canonical `path` (e.g. Anthropic/Claude-Code's `file_path`, Cursor's
* `target_directory`). Mapped to `path` so a model trained on a different
* tool schema still parses. The existing per-tool `?? filePath` fallbacks
* stay as a backstop; this centralizes the snake_case variants too.
*/
private static readonly PATH_ARG_ALIASES = [
"directory",
"file_path",
"filePath",
"filepath",
"target_directory",
"targetDirectory",
"directory_path",
"dir_path",
]
/**
* Normalize known argument-name aliases onto their canonical Shofer names,
* in place. Only fills a canonical field when it is absent (never clobbers an
* explicitly-provided value). Applied right after JSON parsing so both the
* required-field check and the per-tool arg builders see the canonical name.
*/
private static normalizeArgAliases(args: unknown): void {
if (!args || typeof args !== "object") return
const a = args as Record<string, unknown>
if (a.path === undefined) {
for (const alias of this.PATH_ARG_ALIASES) {
if (a[alias] !== undefined) {
a.path = a[alias]
break
}
}
}
// Alias Anthropic/Claude naming conventions for delegation/messaging tools.
// `prompt` (full instructions) → `message` (Shofer canonical name).
// `description` (short summary) → `title` (optional display label).
// Only fills when the canonical key is absent, same as PATH_ARG_ALIASES above.
if (a.message === undefined && a.prompt !== undefined) {
a.message = a.prompt
}
if (a.title === undefined && a.description !== undefined) {
a.title = a.description
}
}
/**
* Compose a `find_files` glob from a (filename) pattern plus an optional
* directory. `find_files` runs the pattern through `vscode.RelativePattern`,
* where a bare glob like `*.ts` matches the workspace root **only** (not
* recursively). Cross-assistant aliases such as Claude Code's `search_file`
* supply a `directory` + filename `pattern` and expect a **recursive** search
* scoped to that directory — so without composition the call would silently
* miss everything in subdirectories. Anchoring the pattern recursively under
* the directory (a globstar segment between the dir and the filename pattern)
* preserves that intent. When no directory is given the pattern is returned
* unchanged (native `find_files` callers keep their exact glob semantics).
*/
private static composeFindFilesPattern(pattern: string, dir: unknown): string {
if (typeof dir !== "string" || dir.trim() === "") return pattern
const cleanDir = dir.trim().replace(/[/\\]+$/, "")
if (!cleanDir || cleanDir === ".") return pattern
// Already absolute or already anchored under the directory → leave as-is.
if (pattern.startsWith("/") || pattern.startsWith(`${cleanDir}/`)) return pattern
// Strip a leading `./` and, if the pattern is already recursive (`**/…`),
// just prefix the directory; otherwise make it recursive under the dir.
const cleanPattern = pattern.replace(/^\.?[/\\]+/, "")
return cleanPattern.startsWith("**") ? `${cleanDir}/${cleanPattern}` : `${cleanDir}/**/${cleanPattern}`
}
/**
* Return the list of required fields that are missing from the parsed
* arguments for the given tool. Used by the generic error message in
* parseToolCall to tell the model exactly what it forgot.
*/
private static missingRequiredFields(toolName: ToolName, args: Record<string, unknown>): string[] {
const missing: string[] = []
switch (toolName) {
case "apply_diff":
if (args.path === undefined && args.filePath === undefined) missing.push("path")
if (args.diff === undefined) missing.push("diff")
break
case "write_to_file":
if (args.path === undefined && args.filePath === undefined) missing.push("path")
if (args.content === undefined) missing.push("content")
break
case "execute_command":
if (args.command === undefined) missing.push("command")
break
case "grep_search":
if (args.path === undefined && args.filePath === undefined) missing.push("path")
if (args.query === undefined && args.pattern === undefined) missing.push("query")
break
case "read_file":
if (args.path === undefined && args.filePath === undefined) missing.push("path")
break
case "sed":
if (args.path === undefined && args.filePath === undefined) missing.push("path")
if (args.pattern === undefined) missing.push("pattern")
if (args.replacement === undefined) missing.push("replacement")
break
case "attempt_completion":
if (args.result === undefined) missing.push("result")
break
case "switch_mode":
if (args.mode_slug === undefined) missing.push("mode_slug")
if (args.reason === undefined) missing.push("reason")
break
case "new_task":
if (args.mode === undefined) missing.push("mode")
if (args.message === undefined) missing.push("message")
break
default:
break
}
return missing
}
private static coerceOptionalNumber(value: unknown): number | undefined {
if (typeof value === "number" && Number.isFinite(value)) {
return value
}
if (typeof value === "string") {
const n = Number(value)
if (Number.isFinite(n)) {
return n
}
}
return undefined
}
/**
* Convert raw file entries from API (with line_ranges) to FileEntry objects
* (with lineRanges). Handles multiple formats for backward compatibility:
*
* New tuple format: { path: string, line_ranges: [[1, 50], [100, 150]] }
* Object format: { path: string, line_ranges: [{ start: 1, end: 50 }] }
* Legacy string format: { path: string, line_ranges: ["1-50"] }
*
* Returns: { path: string, lineRanges: [{ start: 1, end: 50 }] }
*/
private static convertFileEntries(files: unknown[]): FileEntry[] {
return files.map((file: unknown) => {
const f = file as Record<string, unknown>
const entry: FileEntry = { path: f.path as string }
if (f.line_ranges && Array.isArray(f.line_ranges)) {
entry.lineRanges = (f.line_ranges as unknown[])
.map((range: unknown) => {
// Handle tuple format: [start, end]
if (Array.isArray(range) && range.length >= 2) {
return { start: Number(range[0]), end: Number(range[1]) }
}
// Handle object format: { start: number, end: number }
if (typeof range === "object" && range !== null && "start" in range && "end" in range) {
const r = range as { start: unknown; end: unknown }
return { start: Number(r.start), end: Number(r.end) }
}
// Handle legacy string format: "1-50"
if (typeof range === "string") {
const match = range.match(/^(\d+)-(\d+)$/)
if (match) {
return { start: parseInt(match[1]!, 10), end: parseInt(match[2]!, 10) }
}
}
return null
})
.filter((r): r is { start: number; end: number } => r !== null)
}
return entry
})
}
/**
* Create a partial ToolUse from currently parsed arguments.
* Used during streaming to show progress.
* @param originalName - The original tool name as called by the model (if different from canonical name)
*/
private static createPartialToolUse(
id: string,
name: ToolName,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
partialArgs: Record<string, any>,
partial: boolean,
originalName?: string,
): ToolUse | null {
// Build stringified params for display/partial-progress UI.
// NOTE: For streaming partial updates, we MUST populate params even for complex types
// because tool.handlePartial() methods rely on params to show UI updates.
const params: Partial<Record<ToolParamName, string>> = {}
// Allow private LM tool params through as well (they aren't in toolParamNames).
const isExternalTool = isPrivateLmTool(name)
for (const [key, value] of Object.entries(partialArgs)) {
if (toolParamNames.includes(key as ToolParamName) || isExternalTool) {
params[key as ToolParamName] = typeof value === "string" ? value : JSON.stringify(value)
}
}
// Build partial nativeArgs based on what we have so far
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let nativeArgs: any = undefined
// Track if legacy format was used (for telemetry)
let usedLegacyFormat = false
switch (name) {
case "read_file":
// Check for legacy format first: { files: [...] }
// Handle both array and stringified array (some models double-stringify)
if (partialArgs.files !== undefined) {
let filesArray: unknown[] | null = null
if (Array.isArray(partialArgs.files)) {
filesArray = partialArgs.files
} else if (typeof partialArgs.files === "string") {
// Handle double-stringified case: files is a string containing JSON array
try {
const parsed = JSON.parse(partialArgs.files)
if (Array.isArray(parsed)) {
filesArray = parsed
}
} catch {
// Not valid JSON, ignore
}
}
if (filesArray && filesArray.length > 0) {
usedLegacyFormat = true
nativeArgs = {
files: this.convertFileEntries(filesArray),
_legacyFormat: true as const,
}
}
}
// New format: { path: "...", mode: "..." }
// Accept filePath as alias for path (models sometimes hallucinate filePath for read_file)
if (!nativeArgs && (partialArgs.path !== undefined || partialArgs.filePath !== undefined)) {
nativeArgs = {
path: partialArgs.path ?? partialArgs.filePath,
mode: partialArgs.mode,
offset: this.coerceOptionalNumber(partialArgs.offset),
limit: this.coerceOptionalNumber(partialArgs.limit),
indentation:
partialArgs.indentation && typeof partialArgs.indentation === "object"
? {
anchor_line: this.coerceOptionalNumber(partialArgs.indentation.anchor_line),
max_levels: this.coerceOptionalNumber(partialArgs.indentation.max_levels),
max_lines: this.coerceOptionalNumber(partialArgs.indentation.max_lines),
include_siblings: this.coerceOptionalBoolean(
partialArgs.indentation.include_siblings,
),
include_header: this.coerceOptionalBoolean(
partialArgs.indentation.include_header,
),
}
: undefined,
}
}
break
case "attempt_completion":
if (partialArgs.result) {
nativeArgs = {
result: partialArgs.result,
rating: partialArgs.rating,
feedback: partialArgs.feedback,
}
}
break
case "wait":
// Every param is optional — always emit nativeArgs so the dispatcher
// does not reject the call as "missing nativeArgs".
nativeArgs = {
timeout_sec: this.coerceOptionalNumber(partialArgs.timeout_sec),
from: Array.isArray(partialArgs.from) ? (partialArgs.from as string[]) : undefined,
in_reply_to: partialArgs.in_reply_to,
}
break
case "execute_command":
if (partialArgs.command) {
nativeArgs = {
command: partialArgs.command,
cwd: partialArgs.cwd,
timeout: partialArgs.timeout,
}
}
break
case "write_to_file":
if (partialArgs.path || partialArgs.filePath || partialArgs.content) {
nativeArgs = {
path: partialArgs.path ?? partialArgs.filePath,
content: partialArgs.content,
}
}
break
case "ask_followup_question":
if (
partialArgs.question !== undefined ||
partialArgs.follow_up !== undefined ||
partialArgs.form !== undefined
) {
nativeArgs = {
question: partialArgs.question,
follow_up: Array.isArray(partialArgs.follow_up) ? partialArgs.follow_up : undefined,
form: Array.isArray(partialArgs.form) ? partialArgs.form : undefined,
}
}
break
case "apply_diff":
if (
partialArgs.path !== undefined ||
partialArgs.filePath !== undefined ||
partialArgs.diff !== undefined
) {
// No XML-leak path recovery here: we never silently guess the
// target file from a leaked `<parameter name="path">` suffix. If
// `path` is genuinely missing, the final parse rejects with
// actionable feedback (see the apply_diff case in createToolUse).
const path = (partialArgs.path ?? partialArgs.filePath) as string | undefined
const diff = partialArgs.diff as string | undefined
nativeArgs = {
path,
diff,
}
}
break
case "generate_image":
if (partialArgs.prompt !== undefined || partialArgs.path !== undefined) {
nativeArgs = {
prompt: partialArgs.prompt,
path: partialArgs.path,
image: partialArgs.image,
}
}
break
case "run_slash_command":
if (partialArgs.command !== undefined) {
nativeArgs = {
command: partialArgs.command,
args: partialArgs.args,
}
}
break
case "skills":
if (partialArgs.skill !== undefined) {
nativeArgs = {
skill: partialArgs.skill,
args: partialArgs.args,
}
}
break
case "grep_search":
if (
partialArgs.path !== undefined ||
partialArgs.query !== undefined ||
partialArgs.pattern !== undefined
) {
nativeArgs = {
path: partialArgs.path,
query: partialArgs.query ?? partialArgs.pattern,
fileTypes: partialArgs.fileTypes ?? partialArgs.file_pattern,
excludePattern: partialArgs.excludePattern,
isRegex: this.coerceOptionalBoolean(partialArgs.isRegex ?? partialArgs.regex),
caseSensitive: this.coerceOptionalBoolean(partialArgs.caseSensitive),
wholeWord: this.coerceOptionalBoolean(partialArgs.wholeWord),
maxResults: this.coerceOptionalNumber(partialArgs.maxResults),
contextBefore: this.coerceOptionalNumber(partialArgs.contextBefore),
contextAfter: this.coerceOptionalNumber(partialArgs.contextAfter),
}
}
break
case "switch_mode":
if (partialArgs.mode_slug !== undefined || partialArgs.reason !== undefined) {
nativeArgs = {
mode_slug: partialArgs.mode_slug,
reason: partialArgs.reason,
task_id: partialArgs.task_id,
}
}
break
case "update_todo_list":
if (partialArgs.todos !== undefined) {
nativeArgs = {
todos: partialArgs.todos,
}
}
break
case "set_task_title":
if (partialArgs.title !== undefined) {
nativeArgs = {
title: partialArgs.title,
}
}
break
case "give_feedback":
if (partialArgs.feedback !== undefined) {
nativeArgs = {
feedback: partialArgs.feedback,
}
}
break
case "use_mcp_tool":
if (partialArgs.server_name !== undefined || partialArgs.tool_name !== undefined) {
nativeArgs = {
server_name: partialArgs.server_name,
tool_name: partialArgs.tool_name,
arguments: partialArgs.arguments,
}
}
break
case "access_mcp_resource":
if (partialArgs.server_name !== undefined || partialArgs.uri !== undefined) {
nativeArgs = {
server_name: partialArgs.server_name,
uri: partialArgs.uri,
}
}
break
case "call_mcp_tool_async":
if (partialArgs.server_name !== undefined || partialArgs.tool_name !== undefined) {