@code-yeongyu/senpi-ai 2026.7.29 → 2026.7.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/dist/api/openai-completions.d.ts.map +1 -1
  2. package/dist/api/openai-completions.js +121 -38
  3. package/dist/api/openai-completions.js.map +1 -1
  4. package/dist/providers/data/.manifest.json +1 -1
  5. package/dist/providers/data/openrouter.json +1 -1
  6. package/dist/tool-call-middleware/recovery-diagnostics.d.ts +2 -1
  7. package/dist/tool-call-middleware/recovery-diagnostics.d.ts.map +1 -1
  8. package/dist/tool-call-middleware/recovery-diagnostics.js +2 -2
  9. package/dist/tool-call-middleware/recovery-diagnostics.js.map +1 -1
  10. package/dist/tool-call-middleware/recovery-native-projection.d.ts +3 -2
  11. package/dist/tool-call-middleware/recovery-native-projection.d.ts.map +1 -1
  12. package/dist/tool-call-middleware/recovery-native-projection.js +3 -2
  13. package/dist/tool-call-middleware/recovery-native-projection.js.map +1 -1
  14. package/dist/tool-call-middleware/recovery-stream-failure.d.ts +6 -1
  15. package/dist/tool-call-middleware/recovery-stream-failure.d.ts.map +1 -1
  16. package/dist/tool-call-middleware/recovery-stream-failure.js +3 -2
  17. package/dist/tool-call-middleware/recovery-stream-failure.js.map +1 -1
  18. package/dist/tool-call-middleware/recovery-stream-wrapper.d.ts +6 -1
  19. package/dist/tool-call-middleware/recovery-stream-wrapper.d.ts.map +1 -1
  20. package/dist/tool-call-middleware/recovery-stream-wrapper.js +11 -5
  21. package/dist/tool-call-middleware/recovery-stream-wrapper.js.map +1 -1
  22. package/dist/tool-call-middleware/recovery-text-projection.d.ts +6 -1
  23. package/dist/tool-call-middleware/recovery-text-projection.d.ts.map +1 -1
  24. package/dist/tool-call-middleware/recovery-text-projection.js +6 -4
  25. package/dist/tool-call-middleware/recovery-text-projection.js.map +1 -1
  26. package/package.json +1 -1
@@ -1,4 +1,5 @@
1
1
  import type { ToolCall } from "../types.ts";
2
2
  import type { StreamMessageProjection } from "./stream-wrapper-shared.ts";
3
- export declare function appendRecoveryDiagnostic(projection: StreamMessageProjection, toolCall: ToolCall): void;
3
+ import type { ToolCallFormat } from "./types.ts";
4
+ export declare function appendRecoveryDiagnostic(projection: StreamMessageProjection, toolCall: ToolCall, protocol: ToolCallFormat): void;
4
5
  //# sourceMappingURL=recovery-diagnostics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-diagnostics.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-diagnostics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAE1E,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,uBAAuB,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAWtG"}
1
+ {"version":3,"file":"recovery-diagnostics.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-diagnostics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,wBAAgB,wBAAwB,CACvC,UAAU,EAAE,uBAAuB,EACnC,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,cAAc,GACtB,IAAI,CAWN"}
@@ -1,9 +1,9 @@
1
- export function appendRecoveryDiagnostic(projection, toolCall) {
1
+ export function appendRecoveryDiagnostic(projection, toolCall, protocol) {
2
2
  projection.appendDiagnostic({
3
3
  type: "text_tool_call_recovery",
4
4
  timestamp: Date.now(),
5
5
  details: {
6
- protocol: "antml",
6
+ protocol,
7
7
  toolName: toolCall.name,
8
8
  id: toolCall.id,
9
9
  status: toolCall.incomplete === true ? "incomplete" : "complete",
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-diagnostics.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-diagnostics.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,wBAAwB,CAAC,UAAmC,EAAE,QAAkB;IAC/F,UAAU,CAAC,gBAAgB,CAAC;QAC3B,IAAI,EAAE,yBAAyB;QAC/B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;QACrB,OAAO,EAAE;YACR,QAAQ,EAAE,OAAO;YACjB,QAAQ,EAAE,QAAQ,CAAC,IAAI;YACvB,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,MAAM,EAAE,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU;SAChE;KACD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { ToolCall } from \"../types.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\n\nexport function appendRecoveryDiagnostic(projection: StreamMessageProjection, toolCall: ToolCall): void {\n\tprojection.appendDiagnostic({\n\t\ttype: \"text_tool_call_recovery\",\n\t\ttimestamp: Date.now(),\n\t\tdetails: {\n\t\t\tprotocol: \"antml\",\n\t\t\ttoolName: toolCall.name,\n\t\t\tid: toolCall.id,\n\t\t\tstatus: toolCall.incomplete === true ? \"incomplete\" : \"complete\",\n\t\t},\n\t});\n}\n"]}
1
+ {"version":3,"file":"recovery-diagnostics.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-diagnostics.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,wBAAwB,CACvC,UAAmC,EACnC,QAAkB,EAClB,QAAwB;IAExB,UAAU,CAAC,gBAAgB,CAAC;QAC3B,IAAI,EAAE,yBAAyB;QAC/B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;QACrB,OAAO,EAAE;YACR,QAAQ;YACR,QAAQ,EAAE,QAAQ,CAAC,IAAI;YACvB,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,MAAM,EAAE,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU;SAChE;KACD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { ToolCall } from \"../types.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\nimport type { ToolCallFormat } from \"./types.ts\";\n\nexport function appendRecoveryDiagnostic(\n\tprojection: StreamMessageProjection,\n\ttoolCall: ToolCall,\n\tprotocol: ToolCallFormat,\n): void {\n\tprojection.appendDiagnostic({\n\t\ttype: \"text_tool_call_recovery\",\n\t\ttimestamp: Date.now(),\n\t\tdetails: {\n\t\t\tprotocol,\n\t\t\ttoolName: toolCall.name,\n\t\t\tid: toolCall.id,\n\t\t\tstatus: toolCall.incomplete === true ? \"incomplete\" : \"complete\",\n\t\t},\n\t});\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import type { AssistantMessage, AssistantMessageEventStream, ToolCall } from "../types.ts";
2
- import type { StreamParserEvent } from "./types.ts";
2
+ import type { StreamParserEvent, ToolCallFormat } from "./types.ts";
3
3
  /** Maintains source-index order and native/recovered ID ownership for recovery streams. */
4
4
  export declare class RecoveryNativeProjection {
5
5
  private readonly stream;
@@ -9,9 +9,10 @@ export declare class RecoveryNativeProjection {
9
9
  private readonly reservedIds;
10
10
  private readonly recoveredIds;
11
11
  private readonly recoveredIdByParserIndex;
12
+ private readonly recoveredIdPrefix;
12
13
  private highestProjectedInnerIndex;
13
14
  private nextRecoveredId;
14
- constructor(stream: AssistantMessageEventStream, message: AssistantMessage);
15
+ constructor(stream: AssistantMessageEventStream, message: AssistantMessage, protocol?: ToolCallFormat);
15
16
  reserveVisibleIds(source: AssistantMessage): void;
16
17
  synchronizeLower(source: AssistantMessage, contentIndex: number): boolean;
17
18
  synchronizeRemaining(source: AssistantMessage): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-native-projection.d.ts","sourceRoot":"","sources":["../../src/tool-call-middle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AM,GAAG,OAAO,CAMxE;IAED,oBAAoB,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAMtD;IAED,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAGzD;IAED,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAEpE;IAED,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAGnC;IAED,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,GAAG,WAAW,GAAG,WAAW,GAAG,SAAS,CAYtG;IAED,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAQvF;IAED,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAchE;IAED,kBAAkB,CAAC,MAAM,EAAE,SAAS,iBAAiB,EAAE,GAAG,iBAAiB,EAAE,CAc5E;IAED,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,WAAW;IAOnB,OAAO,CAAC,mBAAmB;CAU3B"}
@@ -8,7 +8,7 @@ function cloneToolCall(toolCall) {
8
8
  }
9
9
  /** Maintains source-index order and native/recovered ID ownership for recovery streams. */
10
10
  export class RecoveryNativeProjection {
11
- constructor(stream, message) {
11
+ constructor(stream, message, protocol = "antml") {
12
12
  this.rangesByInnerIndex = new Map();
13
13
  this.nativeLifecycleByInnerIndex = new Map();
14
14
  this.reservedIds = new Set();
@@ -18,6 +18,7 @@ export class RecoveryNativeProjection {
18
18
  this.nextRecoveredId = 0;
19
19
  this.stream = stream;
20
20
  this.message = message;
21
+ this.recoveredIdPrefix = `recovered-${protocol}-`;
21
22
  }
22
23
  reserveVisibleIds(source) {
23
24
  for (const block of source.content) {
@@ -135,7 +136,7 @@ export class RecoveryNativeProjection {
135
136
  }
136
137
  allocateRecoveredId() {
137
138
  for (;;) {
138
- const id = `recovered-antml-${this.nextRecoveredId}`;
139
+ const id = `${this.recoveredIdPrefix}${this.nextRecoveredId}`;
139
140
  this.nextRecoveredId += 1;
140
141
  if (this.reservedIds.has(id))
141
142
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-native-projection.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-native-projection.ts"],"names":[],"mappings":"AAOA,SAAS,iBAAiB,CAAC,KAAmB;IAC7C,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU;QAAE,OAAO,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC;IACtF,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC;AACrB,CAAC;AAED,SAAS,aAAa,CAAC,QAAkB;IACxC,OAAO,EAAE,GAAG,QAAQ,EAAE,SAAS,EAAE,EAAE,GAAG,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC;AAC9D,CAAC;AAED,2FAA2F;AAC3F,MAAM,OAAO,wBAAwB;IAWpC,YAAY,MAAmC,EAAE,OAAyB;QARzD,uBAAkB,GAAG,IAAI,GAAG,EAAwB,CAAC;QACrD,gCAA2B,GAAG,IAAI,GAAG,EAA2B,CAAC;QACjE,gBAAW,GAAG,IAAI,GAAG,EAAU,CAAC;QAChC,iBAAY,GAAG,IAAI,GAAG,EAAU,CAAC;QACjC,6BAAwB,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC9D,+BAA0B,GAAG,CAAC,CAAC,CAAC;QAChC,oBAAe,GAAG,CAAC,CAAC;QAG3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;IAED,iBAAiB,CAAC,MAAwB;QACzC,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACpC,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU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AAK,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAAE,OAAO,KAAK,CAAC;QAC/E,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;QAC/C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC;QACpD,OAAO,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,EAAE,CAAC,CAAC;IACjF,CAAC;IAEO,WAAW,CAAC,UAAkB,EAAE,KAAmB;QAC1D,IAAI,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,IAAI,CAAC,0BAA0B;YAAE,OAAO,KAAK,CAAC;QAC3G,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC/C,IAAI,CAAC,0BAA0B,GAAG,UAAU,CAAC;QAC7C,OAAO,IAAI,CAAC;IACb,CAAC;IAEO,mBAAmB;QAC1B,SAAS,CAAC;YACT,MAAM,EAAE,GAAG,mBAAmB,IAAI,CAAC,eAAe,EAAE,CAAC;YACrD,IAAI,CAAC,eAAe,IAAI,CAAC,CAAC;YAC1B,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;gBAAE,SAAS;YACvC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YACzB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAC1B,OAAO,EAAE,CAAC;QACX,CAAC;IACF,CAAC;CACD","sourcesContent":["import type { AssistantMessage, AssistantMessageEventStream, ToolCall } from \"../types.ts\";\nimport type { StreamParserEvent } from \"./types.ts\";\n\ntype ContentBlock = AssistantMessage[\"content\"][number];\ntype ContentRange = { start: number; end: number };\ntype NativeLifecycle = \"started\" | \"ended\";\n\nfunction cloneContentBlock(block: ContentBlock): ContentBlock {\n\tif (block.type === \"toolCall\") return { ...block, arguments: { ...block.arguments } };\n\treturn { ...block };\n}\n\nfunction cloneToolCall(toolCall: ToolCall): ToolCall {\n\treturn { ...toolCall, arguments: { ...toolCall.arguments } };\n}\n\n/** Maintains source-index order and native/recovered ID ownership for recovery streams. */\nexport class RecoveryNativeProjection {\n\tprivate readonly stream: AssistantMessageEventStream;\n\tprivate readonly message: AssistantMessage;\n\tprivate readonly rangesByInnerIndex = new Map<number, ContentRange>();\n\tprivate readonly nativeLifecycleByInnerIndex = new Map<number, NativeLifecycle>();\n\tprivate readonly reservedIds = new Set<string>();\n\tprivate readonly recoveredIds = new Set<string>();\n\tprivate readonly recoveredIdByParserIndex = new Map<number, string>();\n\tprivate highestProjectedInnerIndex = -1;\n\tprivate nextRecoveredId = 0;\n\n\tconstructor(stream: AssistantMessageEventStream, message: AssistantMessage) {\n\t\tthis.stream = stream;\n\t\tthis.message = message;\n\t}\n\n\treserveVisibleIds(source: AssistantMessage): void {\n\t\tfor (const block of source.content) {\n\t\t\tif (block.type === \"toolCall\") this.reservedIds.add(block.id);\n\t\t}\n\t}\n\n\tsynchronizeLower(source: AssistantMessage, contentIndex: number): boolean {\n\t\tfor (let innerIndex = 0; innerIndex < contentIndex && innerIndex < source.content.length; innerIndex += 1) {\n\t\t\tif (this.rangesByInnerIndex.has(innerIndex)) continue;\n\t\t\tif (!this.appendUnannounced(source, innerIndex)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tsynchronizeRemaining(source: AssistantMessage): boolean {\n\t\tfor (let innerIndex = 0; innerIndex < source.content.length; innerIndex += 1) {\n\t\t\tif (this.rangesByInnerIndex.has(innerIndex)) continue;\n\t\t\tif (!this.appendUnannounced(source, innerIndex)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tstartText(innerIndex: number, outerIndex: number): boolean {\n\t\tif (!this.recordRange(innerIndex, { start: outerIndex, end: this.message.content.length })) return false;\n\t\treturn true;\n\t}\n\n\trecordProjectedBlock(innerIndex: number, outerIndex: number): boolean {\n\t\treturn this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 });\n\t}\n\n\textendText(innerIndex: number): void {\n\t\tconst range = this.rangesByInnerIndex.get(innerIndex);\n\t\tif (range) range.end = this.message.content.length;\n\t}\n\n\tprojectNativeStart(source: AssistantMessage, innerIndex: number): \"projected\" | \"collision\" | \"invalid\" {\n\t\tif (this.nativeLifecycleByInnerIndex.has(innerIndex) || this.rangesByInnerIndex.has(innerIndex)) return \"invalid\";\n\t\tconst block = source.content[innerIndex];\n\t\tif (block?.type !== \"toolCall\" || innerIndex <= this.highestProjectedInnerIndex) return \"invalid\";\n\t\tif (this.recoveredIds.has(block.id)) return \"collision\";\n\t\tthis.reservedIds.add(block.id);\n\t\tconst outerIndex = this.message.content.length;\n\t\tthis.message.content.push(cloneContentBlock(block));\n\t\tif (!this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 })) return \"invalid\";\n\t\tthis.nativeLifecycleByInnerIndex.set(innerIndex, \"started\");\n\t\tthis.stream.push({ type: \"toolcall_start\", contentIndex: outerIndex, partial: this.message });\n\t\treturn \"projected\";\n\t}\n\n\tprojectNativeDelta(source: AssistantMessage, innerIndex: number, delta: string): boolean {\n\t\tif (this.nativeLifecycleByInnerIndex.get(innerIndex) !== \"started\") return false;\n\t\tconst outerIndex = this.rangesByInnerIndex.get(innerIndex)?.start;\n\t\tconst block = source.content[innerIndex];\n\t\tif (outerIndex == null || block?.type !== \"toolCall\") return false;\n\t\tthis.message.content[outerIndex] = cloneContentBlock(block);\n\t\tthis.stream.push({ type: \"toolcall_delta\", contentIndex: outerIndex, delta, partial: this.message });\n\t\treturn true;\n\t}\n\n\tprojectNativeEnd(innerIndex: number, toolCall: ToolCall): boolean {\n\t\tif (this.nativeLifecycleByInnerIndex.get(innerIndex) !== \"started\") return false;\n\t\tconst outerIndex = this.rangesByInnerIndex.get(innerIndex)?.start;\n\t\tif (outerIndex == null) return false;\n\t\tconst finalToolCall = cloneToolCall(toolCall);\n\t\tthis.message.content[outerIndex] = finalToolCall;\n\t\tthis.nativeLifecycleByInnerIndex.set(innerIndex, \"ended\");\n\t\tthis.stream.push({\n\t\t\ttype: \"toolcall_end\",\n\t\t\tcontentIndex: outerIndex,\n\t\t\ttoolCall: finalToolCall,\n\t\t\tpartial: this.message,\n\t\t});\n\t\treturn true;\n\t}\n\n\tassignRecoveredIds(events: readonly StreamParserEvent[]): StreamParserEvent[] {\n\t\treturn events.map((event) => {\n\t\t\tif (event.type === \"toolcall_start\") {\n\t\t\t\tconst id = this.allocateRecoveredId();\n\t\t\t\tthis.recoveredIdByParserIndex.set(event.index, id);\n\t\t\t\treturn { ...event, id };\n\t\t\t}\n\t\t\tif (event.type === \"toolcall_end\") {\n\t\t\t\tconst id = this.recoveredIdByParserIndex.get(event.index) ?? event.id;\n\t\t\t\tthis.recoveredIdByParserIndex.delete(event.index);\n\t\t\t\treturn { ...event, id };\n\t\t\t}\n\t\t\treturn event;\n\t\t});\n\t}\n\n\tprivate appendUnannounced(source: AssistantMessage, innerIndex: number): boolean {\n\t\tconst block = source.content[innerIndex];\n\t\tif (!block || innerIndex <= this.highestProjectedInnerIndex) return false;\n\t\tif (block.type === \"toolCall\" && this.recoveredIds.has(block.id)) return false;\n\t\tconst outerIndex = this.message.content.length;\n\t\tthis.message.content.push(cloneContentBlock(block));\n\t\treturn this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 });\n\t}\n\n\tprivate recordRange(innerIndex: number, range: ContentRange): boolean {\n\t\tif (this.rangesByInnerIndex.has(innerIndex) || innerIndex <= this.highestProjectedInnerIndex) return false;\n\t\tthis.rangesByInnerIndex.set(innerIndex, range);\n\t\tthis.highestProjectedInnerIndex = innerIndex;\n\t\treturn true;\n\t}\n\n\tprivate allocateRecoveredId(): string {\n\t\tfor (;;) {\n\t\t\tconst id = `recovered-antml-${this.nextRecoveredId}`;\n\t\t\tthis.nextRecoveredId += 1;\n\t\t\tif (this.reservedIds.has(id)) continue;\n\t\t\tthis.reservedIds.add(id);\n\t\t\tthis.recoveredIds.add(id);\n\t\t\treturn id;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"recovery-native-projection.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-native-projection.ts"],"names":[],"mappings":"AAOA,SAAS,iBAAiB,CAAC,KAAmB;IAC7C,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU;QAAE,OAAO,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC;IACtF,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC;AACrB,CAAC;AAED,SAAS,aAAa,CAAC,QAAkB;IACxC,OAAO,EAAE,GAAG,QAAQ,EAAE,SAAS,EAAE,EAAE,GAAG,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC;AAC9D,CAAC;AAED,2FAA2F;AAC3F,MAAM,OAAO,wBAAwB;IAYpC,YAAY,MAAmC,EAAE,OAAyB,EAAE,QAAQ,GAAmB,OAAO;QAT7F,uBAAkB,GAAG,IAAI,GAAG,EAAwB,CAAC;QACrD,gCAA2B,GAAG,IAAI,GAAG,EAA2B,CAAC;QACjE,gBAAW,GAAG,IAAI,GAAG,EAAU,CAAC;QAChC,iBAAY,GAAG,IAAI,GAAG,EAAU,CAAC;QACjC,6BAAwB,GAAG,IAAI,GAAG,EAAkB,CAAC;QAE9D,+BAA0B,GAAG,CAAC,CAAC,CAAC;QAChC,oBAAe,GAAG,CAAC,CAAC;QAG3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,iBAAiB,GAAG,aAAa,QAAQ,GAAG,CAAC;IACnD,CAAC;IAED,iBAAiB,CAAC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,CAAC;QACX,CAAC;IACF,CAAC;CACD","sourcesContent":["import type { AssistantMessage, AssistantMessageEventStream, ToolCall } from \"../types.ts\";\nimport type { StreamParserEvent, ToolCallFormat } from \"./types.ts\";\n\ntype ContentBlock = AssistantMessage[\"content\"][number];\ntype ContentRange = { start: number; end: number };\ntype NativeLifecycle = \"started\" | \"ended\";\n\nfunction cloneContentBlock(block: ContentBlock): ContentBlock {\n\tif (block.type === \"toolCall\") return { ...block, arguments: { ...block.arguments } };\n\treturn { ...block };\n}\n\nfunction cloneToolCall(toolCall: ToolCall): ToolCall {\n\treturn { ...toolCall, arguments: { ...toolCall.arguments } };\n}\n\n/** Maintains source-index order and native/recovered ID ownership for recovery streams. */\nexport class RecoveryNativeProjection {\n\tprivate readonly stream: AssistantMessageEventStream;\n\tprivate readonly message: AssistantMessage;\n\tprivate readonly rangesByInnerIndex = new Map<number, ContentRange>();\n\tprivate readonly nativeLifecycleByInnerIndex = new Map<number, NativeLifecycle>();\n\tprivate readonly reservedIds = new Set<string>();\n\tprivate readonly recoveredIds = new Set<string>();\n\tprivate readonly recoveredIdByParserIndex = new Map<number, string>();\n\tprivate readonly recoveredIdPrefix: string;\n\tprivate highestProjectedInnerIndex = -1;\n\tprivate nextRecoveredId = 0;\n\n\tconstructor(stream: AssistantMessageEventStream, message: AssistantMessage, protocol: ToolCallFormat = \"antml\") {\n\t\tthis.stream = stream;\n\t\tthis.message = message;\n\t\tthis.recoveredIdPrefix = `recovered-${protocol}-`;\n\t}\n\n\treserveVisibleIds(source: AssistantMessage): void {\n\t\tfor (const block of source.content) {\n\t\t\tif (block.type === \"toolCall\") this.reservedIds.add(block.id);\n\t\t}\n\t}\n\n\tsynchronizeLower(source: AssistantMessage, contentIndex: number): boolean {\n\t\tfor (let innerIndex = 0; innerIndex < contentIndex && innerIndex < source.content.length; innerIndex += 1) {\n\t\t\tif (this.rangesByInnerIndex.has(innerIndex)) continue;\n\t\t\tif (!this.appendUnannounced(source, innerIndex)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tsynchronizeRemaining(source: AssistantMessage): boolean {\n\t\tfor (let innerIndex = 0; innerIndex < source.content.length; innerIndex += 1) {\n\t\t\tif (this.rangesByInnerIndex.has(innerIndex)) continue;\n\t\t\tif (!this.appendUnannounced(source, innerIndex)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tstartText(innerIndex: number, outerIndex: number): boolean {\n\t\tif (!this.recordRange(innerIndex, { start: outerIndex, end: this.message.content.length })) return false;\n\t\treturn true;\n\t}\n\n\trecordProjectedBlock(innerIndex: number, outerIndex: number): boolean {\n\t\treturn this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 });\n\t}\n\n\textendText(innerIndex: number): void {\n\t\tconst range = this.rangesByInnerIndex.get(innerIndex);\n\t\tif (range) range.end = this.message.content.length;\n\t}\n\n\tprojectNativeStart(source: AssistantMessage, innerIndex: number): \"projected\" | \"collision\" | \"invalid\" {\n\t\tif (this.nativeLifecycleByInnerIndex.has(innerIndex) || this.rangesByInnerIndex.has(innerIndex)) return \"invalid\";\n\t\tconst block = source.content[innerIndex];\n\t\tif (block?.type !== \"toolCall\" || innerIndex <= this.highestProjectedInnerIndex) return \"invalid\";\n\t\tif (this.recoveredIds.has(block.id)) return \"collision\";\n\t\tthis.reservedIds.add(block.id);\n\t\tconst outerIndex = this.message.content.length;\n\t\tthis.message.content.push(cloneContentBlock(block));\n\t\tif (!this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 })) return \"invalid\";\n\t\tthis.nativeLifecycleByInnerIndex.set(innerIndex, \"started\");\n\t\tthis.stream.push({ type: \"toolcall_start\", contentIndex: outerIndex, partial: this.message });\n\t\treturn \"projected\";\n\t}\n\n\tprojectNativeDelta(source: AssistantMessage, innerIndex: number, delta: string): boolean {\n\t\tif (this.nativeLifecycleByInnerIndex.get(innerIndex) !== \"started\") return false;\n\t\tconst outerIndex = this.rangesByInnerIndex.get(innerIndex)?.start;\n\t\tconst block = source.content[innerIndex];\n\t\tif (outerIndex == null || block?.type !== \"toolCall\") return false;\n\t\tthis.message.content[outerIndex] = cloneContentBlock(block);\n\t\tthis.stream.push({ type: \"toolcall_delta\", contentIndex: outerIndex, delta, partial: this.message });\n\t\treturn true;\n\t}\n\n\tprojectNativeEnd(innerIndex: number, toolCall: ToolCall): boolean {\n\t\tif (this.nativeLifecycleByInnerIndex.get(innerIndex) !== \"started\") return false;\n\t\tconst outerIndex = this.rangesByInnerIndex.get(innerIndex)?.start;\n\t\tif (outerIndex == null) return false;\n\t\tconst finalToolCall = cloneToolCall(toolCall);\n\t\tthis.message.content[outerIndex] = finalToolCall;\n\t\tthis.nativeLifecycleByInnerIndex.set(innerIndex, \"ended\");\n\t\tthis.stream.push({\n\t\t\ttype: \"toolcall_end\",\n\t\t\tcontentIndex: outerIndex,\n\t\t\ttoolCall: finalToolCall,\n\t\t\tpartial: this.message,\n\t\t});\n\t\treturn true;\n\t}\n\n\tassignRecoveredIds(events: readonly StreamParserEvent[]): StreamParserEvent[] {\n\t\treturn events.map((event) => {\n\t\t\tif (event.type === \"toolcall_start\") {\n\t\t\t\tconst id = this.allocateRecoveredId();\n\t\t\t\tthis.recoveredIdByParserIndex.set(event.index, id);\n\t\t\t\treturn { ...event, id };\n\t\t\t}\n\t\t\tif (event.type === \"toolcall_end\") {\n\t\t\t\tconst id = this.recoveredIdByParserIndex.get(event.index) ?? event.id;\n\t\t\t\tthis.recoveredIdByParserIndex.delete(event.index);\n\t\t\t\treturn { ...event, id };\n\t\t\t}\n\t\t\treturn event;\n\t\t});\n\t}\n\n\tprivate appendUnannounced(source: AssistantMessage, innerIndex: number): boolean {\n\t\tconst block = source.content[innerIndex];\n\t\tif (!block || innerIndex <= this.highestProjectedInnerIndex) return false;\n\t\tif (block.type === \"toolCall\" && this.recoveredIds.has(block.id)) return false;\n\t\tconst outerIndex = this.message.content.length;\n\t\tthis.message.content.push(cloneContentBlock(block));\n\t\treturn this.recordRange(innerIndex, { start: outerIndex, end: outerIndex + 1 });\n\t}\n\n\tprivate recordRange(innerIndex: number, range: ContentRange): boolean {\n\t\tif (this.rangesByInnerIndex.has(innerIndex) || innerIndex <= this.highestProjectedInnerIndex) return false;\n\t\tthis.rangesByInnerIndex.set(innerIndex, range);\n\t\tthis.highestProjectedInnerIndex = innerIndex;\n\t\treturn true;\n\t}\n\n\tprivate allocateRecoveredId(): string {\n\t\tfor (;;) {\n\t\t\tconst id = `${this.recoveredIdPrefix}${this.nextRecoveredId}`;\n\t\t\tthis.nextRecoveredId += 1;\n\t\t\tif (this.reservedIds.has(id)) continue;\n\t\t\tthis.reservedIds.add(id);\n\t\t\tthis.recoveredIds.add(id);\n\t\t\treturn id;\n\t\t}\n\t}\n}\n"]}
@@ -1,5 +1,10 @@
1
1
  import type { AssistantMessage, AssistantMessageEventStream } from "../types.ts";
2
2
  import type { StreamMessageProjection } from "./stream-wrapper-shared.ts";
3
+ import type { ToolCallFormat } from "./types.ts";
3
4
  export type RecoveryStreamFailure = "collision" | "invalid_content_event_order" | "invalid_native_event_order";
4
- export declare function terminateRecoveryStreamForFailure(stream: AssistantMessageEventStream, projection: StreamMessageProjection, source: AssistantMessage, failure: RecoveryStreamFailure): void;
5
+ export declare function terminateRecoveryStreamForFailure(stream: AssistantMessageEventStream, projection: StreamMessageProjection, options: {
6
+ source: AssistantMessage;
7
+ failure: RecoveryStreamFailure;
8
+ protocol: ToolCallFormat;
9
+ }): void;
5
10
  //# sourceMappingURL=recovery-stream-failure.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-stream-failure.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-failure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAE1E,MAAM,MAAM,qBAAqB,GAAG,WAAW,GAAG,6BAA6B,GAAG,4BAA4B,CAAC;AAqB/G,wBAAgB,iCAAiC,CAChD,MAAM,EAAE,2BAA2B,EACnC,UAAU,EAAE,uBAAuB,EACnC,MAAM,EAAE,gBAAgB,EACxB,OAAO,EAAE,qBAAqB,GAC5B,IAAI,CAiBN"}
1
+ {"version":3,"file":"recovery-stream-failure.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-failure.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,MAAM,qBAAqB,GAAG,WAAW,GAAG,6BAA6B,GAAG,4BAA4B,CAAC;AAqB/G,wBAAgB,iCAAiC,CAChD,MAAM,EAAE,2BAA2B,EACnC,UAAU,EAAE,uBAAuB,EACnC,OAAO,EAAE;IACR,MAAM,EAAE,gBAAgB,CAAC;IACzB,OAAO,EAAE,qBAAqB,CAAC;IAC/B,QAAQ,EAAE,cAAc,CAAC;CACzB,GACC,IAAI,CAkBN"}
@@ -15,7 +15,8 @@ const failureDetails = {
15
15
  status: "invalid_native_event_order",
16
16
  },
17
17
  };
18
- export function terminateRecoveryStreamForFailure(stream, projection, source, failure) {
18
+ export function terminateRecoveryStreamForFailure(stream, projection, options) {
19
+ const { source, failure, protocol } = options;
19
20
  const details = failureDetails[failure];
20
21
  projection.sync(source);
21
22
  const message = projection.finalize(source, false);
@@ -27,7 +28,7 @@ export function terminateRecoveryStreamForFailure(stream, projection, source, fa
27
28
  {
28
29
  type: details.diagnosticType,
29
30
  timestamp: Date.now(),
30
- details: { protocol: "antml", status: details.status },
31
+ details: { protocol, status: details.status },
31
32
  },
32
33
  ];
33
34
  stream.push({ type: "error", reason: "error", error: message });
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-stream-failure.js","sourceRoot":"","sources":["../../src/tool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AO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;IAChE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEventStream } from \"../types.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\n\nexport type RecoveryStreamFailure = \"collision\" | \"invalid_content_event_order\" | \"invalid_native_event_order\";\n\nconst failureDetails: Record<RecoveryStreamFailure, { diagnosticType: string; errorMessage: string; status: string }> =\n\t{\n\t\tcollision: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_collision\",\n\t\t\terrorMessage: \"Tool call ID collision in provider stream\",\n\t\t\tstatus: \"collision\",\n\t\t},\n\t\tinvalid_content_event_order: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_invalid_content_event\",\n\t\t\terrorMessage: \"Invalid assistant content event order\",\n\t\t\tstatus: \"invalid_content_event_order\",\n\t\t},\n\t\tinvalid_native_event_order: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_invalid_native_event\",\n\t\t\terrorMessage: \"Invalid native tool call event order\",\n\t\t\tstatus: \"invalid_native_event_order\",\n\t\t},\n\t};\n\nexport function terminateRecoveryStreamForFailure(\n\tstream: AssistantMessageEventStream,\n\tprojection: StreamMessageProjection,\n\tsource: AssistantMessage,\n\tfailure: RecoveryStreamFailure,\n): void {\n\tconst details = failureDetails[failure];\n\tprojection.sync(source);\n\tconst message = projection.finalize(source, false);\n\tmessage.content = message.content.filter((block) => block.type !== \"toolCall\");\n\tmessage.stopReason = \"error\";\n\tmessage.errorMessage = details.errorMessage;\n\tmessage.diagnostics = [\n\t\t...(source.diagnostics ?? []),\n\t\t{\n\t\t\ttype: details.diagnosticType,\n\t\t\ttimestamp: Date.now(),\n\t\t\tdetails: { protocol: \"antml\", status: details.status },\n\t\t},\n\t];\n\tstream.push({ type: \"error\", reason: \"error\", error: message });\n\tstream.end(message);\n}\n"]}
1
+ {"version":3,"file":"recovery-stream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AC;IACF,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;IAChE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACrB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEventStream } from \"../types.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\nimport type { ToolCallFormat } from \"./types.ts\";\n\nexport type RecoveryStreamFailure = \"collision\" | \"invalid_content_event_order\" | \"invalid_native_event_order\";\n\nconst failureDetails: Record<RecoveryStreamFailure, { diagnosticType: string; errorMessage: string; status: string }> =\n\t{\n\t\tcollision: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_collision\",\n\t\t\terrorMessage: \"Tool call ID collision in provider stream\",\n\t\t\tstatus: \"collision\",\n\t\t},\n\t\tinvalid_content_event_order: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_invalid_content_event\",\n\t\t\terrorMessage: \"Invalid assistant content event order\",\n\t\t\tstatus: \"invalid_content_event_order\",\n\t\t},\n\t\tinvalid_native_event_order: {\n\t\t\tdiagnosticType: \"text_tool_call_recovery_invalid_native_event\",\n\t\t\terrorMessage: \"Invalid native tool call event order\",\n\t\t\tstatus: \"invalid_native_event_order\",\n\t\t},\n\t};\n\nexport function terminateRecoveryStreamForFailure(\n\tstream: AssistantMessageEventStream,\n\tprojection: StreamMessageProjection,\n\toptions: {\n\t\tsource: AssistantMessage;\n\t\tfailure: RecoveryStreamFailure;\n\t\tprotocol: ToolCallFormat;\n\t},\n): void {\n\tconst { source, failure, protocol } = options;\n\tconst details = failureDetails[failure];\n\tprojection.sync(source);\n\tconst message = projection.finalize(source, false);\n\tmessage.content = message.content.filter((block) => block.type !== \"toolCall\");\n\tmessage.stopReason = \"error\";\n\tmessage.errorMessage = details.errorMessage;\n\tmessage.diagnostics = [\n\t\t...(source.diagnostics ?? []),\n\t\t{\n\t\t\ttype: details.diagnosticType,\n\t\t\ttimestamp: Date.now(),\n\t\t\tdetails: { protocol, status: details.status },\n\t\t},\n\t];\n\tstream.push({ type: \"error\", reason: \"error\", error: message });\n\tstream.end(message);\n}\n"]}
@@ -1,4 +1,9 @@
1
1
  import type { AssistantMessageEventStream, Tool } from "../types.ts";
2
2
  import type { RecoveryStreamParser } from "./protocols/anthropic-xml/recovery-stream.ts";
3
- export declare function wrapStreamWithInvokeRecovery(innerStream: AssistantMessageEventStream, tools: readonly Tool[], createParser?: (tools: readonly Tool[]) => RecoveryStreamParser): AssistantMessageEventStream;
3
+ import type { ToolCallFormat } from "./types.ts";
4
+ export interface InvokeRecoveryOptions {
5
+ createParser?: (tools: readonly Tool[]) => RecoveryStreamParser;
6
+ protocol?: ToolCallFormat;
7
+ }
8
+ export declare function wrapStreamWithInvokeRecovery(innerStream: AssistantMessageEventStream, tools: readonly Tool[], options?: InvokeRecoveryOptions | ((tools: readonly Tool[]) => RecoveryStreamParser)): AssistantMessageEventStream;
4
9
  //# sourceMappingURL=recovery-stream-wrapper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-stream-wrapper.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2C,2BAA2B,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC9G,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AAUzF,wBAAgB,4BAA4B,CAC3C,WAAW,EAAE,2BAA2B,EACxC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,YAAY,GAAE,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,oBAA4D,GACpG,2BAA2B,CA8N7B"}
1
+ {"version":3,"file":"recovery-stream-wrapper.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2C,2BAA2B,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC9G,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AASzF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,WAAW,qBAAqB;IACrC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,oBAAoB,CAAC;IAChE,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED,wBAAgB,4BAA4B,CAC3C,WAAW,EAAE,2BAA2B,EACxC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,OAAO,GAAE,qBAAqB,GAAG,CAAC,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,oBAAoB,CAAM,GACtF,2BAA2B,CA8N7B"}
@@ -6,8 +6,11 @@ import { terminateRecoveryStreamForFailure } from "./recovery-stream-failure.js"
6
6
  import { RecoveryStreamTerminal } from "./recovery-stream-terminal.js";
7
7
  import { RecoveryTextProjection } from "./recovery-text-projection.js";
8
8
  import { StreamMessageProjection } from "./stream-wrapper-shared.js";
9
- export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser = createAntmlInvokeRecoveryStreamParser) {
9
+ export function wrapStreamWithInvokeRecovery(innerStream, tools, options = {}) {
10
10
  const outerStream = new RecoveryAssistantMessageEventStream();
11
+ const resolvedOptions = typeof options === "function" ? { createParser: options } : options;
12
+ const createParser = resolvedOptions.createParser ?? createAntmlInvokeRecoveryStreamParser;
13
+ const protocol = resolvedOptions.protocol ?? "antml";
11
14
  void (async () => {
12
15
  const innerIterator = innerStream[Symbol.asyncIterator]();
13
16
  const innerEvents = { [Symbol.asyncIterator]: () => innerIterator };
@@ -41,7 +44,7 @@ export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser =
41
44
  if (!projection || !outerStream.markSourceClosed())
42
45
  return;
43
46
  finishText();
44
- terminateRecoveryStreamForFailure(outerStream, projection, source, failure);
47
+ terminateRecoveryStreamForFailure(outerStream, projection, { source, failure, protocol });
45
48
  };
46
49
  const prepareContentEvent = (source, contentIndex) => {
47
50
  if (!projection || !nativeProjection)
@@ -79,7 +82,7 @@ export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser =
79
82
  };
80
83
  const synchronizeTerminal = (source) => {
81
84
  projection ??= new StreamMessageProjection(outerStream, source, { preserveSourceMetadata: true });
82
- nativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message);
85
+ nativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message, protocol);
83
86
  projection.sync(source);
84
87
  nativeProjection.reserveVisibleIds(source);
85
88
  finishText();
@@ -97,7 +100,7 @@ export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser =
97
100
  projection = new StreamMessageProjection(outerStream, event.partial, {
98
101
  preserveSourceMetadata: true,
99
102
  });
100
- nativeProjection = new RecoveryNativeProjection(outerStream, projection.message);
103
+ nativeProjection = new RecoveryNativeProjection(outerStream, projection.message, protocol);
101
104
  nativeProjection.reserveVisibleIds(event.partial);
102
105
  outerStream.push({ type: "start", partial: projection.message });
103
106
  break;
@@ -106,7 +109,10 @@ export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser =
106
109
  return;
107
110
  if (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)
108
111
  return;
109
- const nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex, createParser);
112
+ const nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex, {
113
+ createParser,
114
+ protocol,
115
+ });
110
116
  if (!nextText.start(event.partial)) {
111
117
  terminateForFailure(event.partial, "invalid_native_event_order");
112
118
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-stream-wrapper.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAA4B,wBAAwB,EAAE,MAAM,iCAAi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CAAC,IAAI,CAAC,UAAU;4BAAE,OAAO;wBACnF,UAAU,CAAC,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;wBAC5E,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;wBACrD,MAAM;gBACR,CAAC;YACF,CAAC;YAED,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,eAAe,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Tool } from \"../types.ts\";\nimport type { RecoveryStreamParser } from \"./protocols/anthropic-xml/recovery-stream.ts\";\nimport { createAntmlInvokeRecoveryStreamParser } from \"./protocols/antml/recovery-stream.ts\";\nimport { type RecoveryContentKind, RecoveryContentLifecycle } from \"./recovery-content-lifecycle.ts\";\nimport { RecoveryAssistantMessageEventStream } from \"./recovery-event-stream.ts\";\nimport { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport { type RecoveryStreamFailure, terminateRecoveryStreamForFailure } from \"./recovery-stream-failure.ts\";\nimport { RecoveryStreamTerminal } from \"./recovery-stream-terminal.ts\";\nimport { RecoveryTextProjection } from \"./recovery-text-projection.ts\";\nimport { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\n\nexport function wrapStreamWithInvokeRecovery(\n\tinnerStream: AssistantMessageEventStream,\n\ttools: readonly Tool[],\n\tcreateParser: (tools: readonly Tool[]) => RecoveryStreamParser = createAntmlInvokeRecoveryStreamParser,\n): AssistantMessageEventStream {\n\tconst outerStream = new RecoveryAssistantMessageEventStream();\n\n\tvoid (async (): Promise<void> => {\n\t\tconst innerIterator = innerStream[Symbol.asyncIterator]();\n\t\tconst innerEvents: AsyncIterable<AssistantMessageEvent> = { [Symbol.asyncIterator]: () => innerIterator };\n\t\tlet projection: StreamMessageProjection | null = null;\n\t\tlet nativeProjection: RecoveryNativeProjection | null = null;\n\t\tlet textProjection: RecoveryTextProjection | null = null;\n\t\tlet sawToolCall = false;\n\t\tlet cancellationRequested = false;\n\t\tconst contentLifecycle = new RecoveryContentLifecycle();\n\t\tconst terminal = new RecoveryStreamTerminal(outerStream);\n\n\t\tconst finishText = (): void => {\n\t\t\tif (!textProjection) return;\n\t\t\tsawToolCall = textProjection.finish() || sawToolCall;\n\t\t\ttextProjection = null;\n\t\t};\n\n\t\touterStream.setCancellationHandler(async () => {\n\t\t\tcancellationRequested = true;\n\t\t\ttry {\n\t\t\t\tawait innerIterator.return?.();\n\t\t\t\tfinishText();\n\t\t\t\tterminal.cancelled(projection);\n\t\t\t} catch (error) {\n\t\t\t\tfinishText();\n\t\t\t\tterminal.iteratorFailure(projection, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t});\n\n\t\tconst terminateForFailure = (source: AssistantMessage, failure: RecoveryStreamFailure): void => {\n\t\t\tif (!projection || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminateRecoveryStreamForFailure(outerStream, projection, source, failure);\n\t\t};\n\n\t\tconst prepareContentEvent = (source: AssistantMessage, contentIndex: number): boolean => {\n\t\t\tif (!projection || !nativeProjection) return false;\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, \"invalid_native_event_order\");\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tif (nativeProjection.synchronizeLower(source, contentIndex)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\tconst canStart = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, failure);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst block = source.content[contentIndex];\n\t\t\tconst blockMatches =\n\t\t\t\t(kind === \"text\" && block?.type === \"text\") ||\n\t\t\t\t(kind === \"thinking\" && block?.type === \"thinking\") ||\n\t\t\t\t(kind === \"toolCall\" && block?.type === \"toolCall\");\n\t\t\tif (blockMatches && contentLifecycle.canStart(contentIndex)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst isActive = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (contentLifecycle.isActive(contentIndex, kind)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst synchronizeTerminal = (source: AssistantMessage): boolean => {\n\t\t\tprojection ??= new StreamMessageProjection(outerStream, source, { preserveSourceMetadata: true });\n\t\t\tnativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tfinishText();\n\t\t\tif (nativeProjection.synchronizeRemaining(source)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\ttry {\n\t\t\tfor await (const event of innerEvents) {\n\t\t\t\tif (cancellationRequested) continue;\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"start\":\n\t\t\t\t\t\tprojection = new StreamMessageProjection(outerStream, event.partial, {\n\t\t\t\t\t\t\tpreserveSourceMetadata: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tnativeProjection = new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\t\t\t\tnativeProjection.reserveVisibleIds(event.partial);\n\t\t\t\t\t\touterStream.push({ type: \"start\", partial: projection.message });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst nextText = new RecoveryTextProjection(\n\t\t\t\t\t\t\ttools,\n\t\t\t\t\t\t\tprojection,\n\t\t\t\t\t\t\tnativeProjection,\n\t\t\t\t\t\t\tevent.contentIndex,\n\t\t\t\t\t\t\tcreateParser,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (!nextText.start(event.partial)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttextProjection = nextText;\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"text_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !textProjection) return;\n\t\t\t\t\t\tsawToolCall = textProjection.feed(event.delta) || sawToolCall;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex)) return;\n\t\t\t\t\t\tfinishText();\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"done\":\n\t\t\t\t\t\tif (!synchronizeTerminal(event.message) || !projection) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.done(projection, event.message, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"error\":\n\t\t\t\t\t\tif (!projection) {\n\t\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!synchronizeTerminal(event.error)) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.sourceError(projection, event.error, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"toolcall_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tconst status = nativeProjection.projectNativeStart(event.partial, event.contentIndex);\n\t\t\t\t\t\tif (status !== \"projected\") {\n\t\t\t\t\t\t\tterminateForFailure(\n\t\t\t\t\t\t\t\tevent.partial,\n\t\t\t\t\t\t\t\tstatus === \"collision\" ? \"collision\" : \"invalid_native_event_order\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"toolcall_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeDelta(event.partial, event.contentIndex, event.delta)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolcall_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeEnd(event.contentIndex, event.toolCall)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst outerIndex = projection.startThinking(event.contentIndex, event.partial);\n\t\t\t\t\t\tif (!nativeProjection.recordProjectedBlock(event.contentIndex, outerIndex)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"thinking_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.projectThinkingDelta(event.contentIndex, event.delta, event.partial);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.finishThinking(event.contentIndex, event.content, event.partial);\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.exhausted(projection);\n\t\t} catch (error) {\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.iteratorFailure(projection, error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n"]}
1
+ {"version":3,"file":"recovery-stream-wrapper.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAA4B,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AACrG,OAAO,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAA8B,iCAAiC,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAQrE,MAAM,UAAU,4BAA4B,CAC3C,WAAwC,EACxC,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,UAAU,CAAC,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;wBAC5E,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;wBACrD,MAAM;gBACR,CAAC;YACF,CAAC;YAED,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,eAAe,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Tool } from \"../types.ts\";\nimport type { RecoveryStreamParser } from \"./protocols/anthropic-xml/recovery-stream.ts\";\nimport { createAntmlInvokeRecoveryStreamParser } from \"./protocols/antml/recovery-stream.ts\";\nimport { type RecoveryContentKind, RecoveryContentLifecycle } from \"./recovery-content-lifecycle.ts\";\nimport { RecoveryAssistantMessageEventStream } from \"./recovery-event-stream.ts\";\nimport { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport { type RecoveryStreamFailure, terminateRecoveryStreamForFailure } from \"./recovery-stream-failure.ts\";\nimport { RecoveryStreamTerminal } from \"./recovery-stream-terminal.ts\";\nimport { RecoveryTextProjection } from \"./recovery-text-projection.ts\";\nimport { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\nimport type { ToolCallFormat } from \"./types.ts\";\n\nexport interface InvokeRecoveryOptions {\n\tcreateParser?: (tools: readonly Tool[]) => RecoveryStreamParser;\n\tprotocol?: ToolCallFormat;\n}\n\nexport function wrapStreamWithInvokeRecovery(\n\tinnerStream: AssistantMessageEventStream,\n\ttools: readonly Tool[],\n\toptions: InvokeRecoveryOptions | ((tools: readonly Tool[]) => RecoveryStreamParser) = {},\n): AssistantMessageEventStream {\n\tconst outerStream = new RecoveryAssistantMessageEventStream();\n\tconst resolvedOptions = typeof options === \"function\" ? { createParser: options } : options;\n\tconst createParser = resolvedOptions.createParser ?? createAntmlInvokeRecoveryStreamParser;\n\tconst protocol = resolvedOptions.protocol ?? \"antml\";\n\n\tvoid (async (): Promise<void> => {\n\t\tconst innerIterator = innerStream[Symbol.asyncIterator]();\n\t\tconst innerEvents: AsyncIterable<AssistantMessageEvent> = { [Symbol.asyncIterator]: () => innerIterator };\n\t\tlet projection: StreamMessageProjection | null = null;\n\t\tlet nativeProjection: RecoveryNativeProjection | null = null;\n\t\tlet textProjection: RecoveryTextProjection | null = null;\n\t\tlet sawToolCall = false;\n\t\tlet cancellationRequested = false;\n\t\tconst contentLifecycle = new RecoveryContentLifecycle();\n\t\tconst terminal = new RecoveryStreamTerminal(outerStream);\n\n\t\tconst finishText = (): void => {\n\t\t\tif (!textProjection) return;\n\t\t\tsawToolCall = textProjection.finish() || sawToolCall;\n\t\t\ttextProjection = null;\n\t\t};\n\n\t\touterStream.setCancellationHandler(async () => {\n\t\t\tcancellationRequested = true;\n\t\t\ttry {\n\t\t\t\tawait innerIterator.return?.();\n\t\t\t\tfinishText();\n\t\t\t\tterminal.cancelled(projection);\n\t\t\t} catch (error) {\n\t\t\t\tfinishText();\n\t\t\t\tterminal.iteratorFailure(projection, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t});\n\n\t\tconst terminateForFailure = (source: AssistantMessage, failure: RecoveryStreamFailure): void => {\n\t\t\tif (!projection || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminateRecoveryStreamForFailure(outerStream, projection, { source, failure, protocol });\n\t\t};\n\n\t\tconst prepareContentEvent = (source: AssistantMessage, contentIndex: number): boolean => {\n\t\t\tif (!projection || !nativeProjection) return false;\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, \"invalid_native_event_order\");\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tif (nativeProjection.synchronizeLower(source, contentIndex)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\tconst canStart = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, failure);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst block = source.content[contentIndex];\n\t\t\tconst blockMatches =\n\t\t\t\t(kind === \"text\" && block?.type === \"text\") ||\n\t\t\t\t(kind === \"thinking\" && block?.type === \"thinking\") ||\n\t\t\t\t(kind === \"toolCall\" && block?.type === \"toolCall\");\n\t\t\tif (blockMatches && contentLifecycle.canStart(contentIndex)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst isActive = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (contentLifecycle.isActive(contentIndex, kind)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst synchronizeTerminal = (source: AssistantMessage): boolean => {\n\t\t\tprojection ??= new StreamMessageProjection(outerStream, source, { preserveSourceMetadata: true });\n\t\t\tnativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message, protocol);\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tfinishText();\n\t\t\tif (nativeProjection.synchronizeRemaining(source)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\ttry {\n\t\t\tfor await (const event of innerEvents) {\n\t\t\t\tif (cancellationRequested) continue;\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"start\":\n\t\t\t\t\t\tprojection = new StreamMessageProjection(outerStream, event.partial, {\n\t\t\t\t\t\t\tpreserveSourceMetadata: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tnativeProjection = new RecoveryNativeProjection(outerStream, projection.message, protocol);\n\t\t\t\t\t\tnativeProjection.reserveVisibleIds(event.partial);\n\t\t\t\t\t\touterStream.push({ type: \"start\", partial: projection.message });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex, {\n\t\t\t\t\t\t\tcreateParser,\n\t\t\t\t\t\t\tprotocol,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tif (!nextText.start(event.partial)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttextProjection = nextText;\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"text_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !textProjection) return;\n\t\t\t\t\t\tsawToolCall = textProjection.feed(event.delta) || sawToolCall;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex)) return;\n\t\t\t\t\t\tfinishText();\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"done\":\n\t\t\t\t\t\tif (!synchronizeTerminal(event.message) || !projection) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.done(projection, event.message, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"error\":\n\t\t\t\t\t\tif (!projection) {\n\t\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!synchronizeTerminal(event.error)) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.sourceError(projection, event.error, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"toolcall_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tconst status = nativeProjection.projectNativeStart(event.partial, event.contentIndex);\n\t\t\t\t\t\tif (status !== \"projected\") {\n\t\t\t\t\t\t\tterminateForFailure(\n\t\t\t\t\t\t\t\tevent.partial,\n\t\t\t\t\t\t\t\tstatus === \"collision\" ? \"collision\" : \"invalid_native_event_order\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"toolcall_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeDelta(event.partial, event.contentIndex, event.delta)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolcall_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeEnd(event.contentIndex, event.toolCall)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst outerIndex = projection.startThinking(event.contentIndex, event.partial);\n\t\t\t\t\t\tif (!nativeProjection.recordProjectedBlock(event.contentIndex, outerIndex)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"thinking_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.projectThinkingDelta(event.contentIndex, event.delta, event.partial);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.finishThinking(event.contentIndex, event.content, event.partial);\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.exhausted(projection);\n\t\t} catch (error) {\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.iteratorFailure(projection, error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n"]}
@@ -2,18 +2,23 @@ import type { AssistantMessage, Tool } from "../types.ts";
2
2
  import type { RecoveryStreamParser } from "./protocols/anthropic-xml/recovery-stream.ts";
3
3
  import type { RecoveryNativeProjection } from "./recovery-native-projection.ts";
4
4
  import type { StreamMessageProjection } from "./stream-wrapper-shared.ts";
5
+ import type { ToolCallFormat } from "./types.ts";
5
6
  /** Owns one ordinary assistant text block's mask/parser/projection lifecycle. */
6
7
  export declare class RecoveryTextProjection {
7
8
  private readonly projection;
8
9
  private readonly nativeProjection;
9
10
  private readonly innerIndex;
10
11
  private readonly parser;
12
+ private readonly protocol;
11
13
  private readonly mask;
12
14
  private activeInvoke;
13
15
  private textBuffer;
14
16
  private sawToolCall;
15
17
  private finished;
16
- constructor(tools: readonly Tool[], projection: StreamMessageProjection, nativeProjection: RecoveryNativeProjection, innerIndex: number, createParser?: (tools: readonly Tool[]) => RecoveryStreamParser);
18
+ constructor(tools: readonly Tool[], projection: StreamMessageProjection, nativeProjection: RecoveryNativeProjection, innerIndex: number, options?: {
19
+ createParser?: (tools: readonly Tool[]) => RecoveryStreamParser;
20
+ protocol?: ToolCallFormat;
21
+ });
17
22
  start(source: AssistantMessage): boolean;
18
23
  feed(text: string): boolean;
19
24
  finish(): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-text-projection.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-text-projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AAIzF,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAG1E,iFAAiF;AACjF,qBAAa,sBAAsB;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0B;IACrD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA2B;IAC5D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4B;IACjD,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,UAAU,CAAM;IACxB,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAS;IAEzB,YACC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,UAAU,EAAE,uBAAuB,EACnC,gBAAgB,EAAE,wBAAwB,EAC1C,UAAU,EAAE,MAAM,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,EAC1C,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE;QACR,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,oBAAoB,CAAC;QAChE,QAAQ,CAAC,EAAE,cAAc,CAAC;KACrB,EAON;IAED,KAAK,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAGvC;IAED,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAQ1B;IAED,MAAM,IAAI,OAAO,CAShB;IAED,OAAO,CAAC,cAAc;IAMtB,OAAO,CAAC,mBAAmB;IAoB3B,OAAO,CAAC,SAAS;CAKjB"}
@@ -3,7 +3,7 @@ import { createRecoveryCodeMask } from "./recovery-code-mask.js";
3
3
  import { appendRecoveryDiagnostic } from "./recovery-diagnostics.js";
4
4
  /** Owns one ordinary assistant text block's mask/parser/projection lifecycle. */
5
5
  export class RecoveryTextProjection {
6
- constructor(tools, projection, nativeProjection, innerIndex, createParser = createAntmlInvokeRecoveryStreamParser) {
6
+ constructor(tools, projection, nativeProjection, innerIndex, options = {}) {
7
7
  this.mask = createRecoveryCodeMask();
8
8
  this.activeInvoke = false;
9
9
  this.textBuffer = "";
@@ -12,7 +12,8 @@ export class RecoveryTextProjection {
12
12
  this.projection = projection;
13
13
  this.nativeProjection = nativeProjection;
14
14
  this.innerIndex = innerIndex;
15
- this.parser = createParser(tools);
15
+ this.parser = (options.createParser ?? createAntmlInvokeRecoveryStreamParser)(tools);
16
+ this.protocol = options.protocol ?? "antml";
16
17
  }
17
18
  start(source) {
18
19
  const outerIndex = this.projection.startText(this.innerIndex, source.content[this.innerIndex]);
@@ -61,8 +62,9 @@ export class RecoveryTextProjection {
61
62
  this.activeInvoke = true;
62
63
  if (event.type === "toolcall_end") {
63
64
  this.activeInvoke = false;
64
- for (const toolCall of result.completedToolCalls)
65
- appendRecoveryDiagnostic(this.projection, toolCall);
65
+ for (const toolCall of result.completedToolCalls) {
66
+ appendRecoveryDiagnostic(this.projection, toolCall, this.protocol);
67
+ }
66
68
  }
67
69
  }
68
70
  this.nativeProjection.extendText(this.innerIndex);
@@ -1 +1 @@
1
- {"version":3,"file":"recovery-text-projection.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-text-projection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAAE,sBAAsB,EAAgC,MAAM,yBAAyB,CAAC;AAC/F,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAKrE,iFAAiF;AACjF,MAAM,OAAO,sBAAsB;IAWlC,YACC,KAAsB,EACtB,UAAmC,EACnC,gBAA0C,EAC1C,UAAkB,EAClB,YAAY,GAAqD,qCAAqC;QAXtF,SAAI,GAAG,sBAAsB,EAAE,CAAC;QACzC,iBAAY,GAAG,KAAK,CAAC;QACrB,eAAU,GAAG,EAAE,CAAC;QAChB,gBAAW,GAAG,KAAK,CAAC;QACpB,aAAQ,GAAG,KAAK,CAAC;QASxB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,MAAwB;QAC7B,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;QAC/F,OAAO,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IACrE,CAAC;IAED,IAAI,CAAC,IAAY;QAChB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EA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{ AssistantMessage, Tool } from \"../types.ts\";\nimport type { RecoveryStreamParser } from \"./protocols/anthropic-xml/recovery-stream.ts\";\nimport { createAntmlInvokeRecoveryStreamParser } from \"./protocols/antml/recovery-stream.ts\";\nimport { createRecoveryCodeMask, type RecoveryCodeMaskSegment } from \"./recovery-code-mask.ts\";\nimport { appendRecoveryDiagnostic } from \"./recovery-diagnostics.ts\";\nimport type { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\nimport type { StreamParserEvent } from \"./types.ts\";\n\n/** Owns one ordinary assistant text block's mask/parser/projection lifecycle. */\nexport class RecoveryTextProjection {\n\tprivate readonly projection: StreamMessageProjection;\n\tprivate readonly nativeProjection: RecoveryNativeProjection;\n\tprivate readonly innerIndex: number;\n\tprivate readonly parser: RecoveryStreamParser;\n\tprivate readonly mask = createRecoveryCodeMask();\n\tprivate activeInvoke = false;\n\tprivate textBuffer = \"\";\n\tprivate sawToolCall = false;\n\tprivate finished = false;\n\n\tconstructor(\n\t\ttools: readonly Tool[],\n\t\tprojection: StreamMessageProjection,\n\t\tnativeProjection: RecoveryNativeProjection,\n\t\tinnerIndex: number,\n\t\tcreateParser: (tools: readonly Tool[]) => RecoveryStreamParser = createAntmlInvokeRecoveryStreamParser,\n\t) {\n\t\tthis.projection = projection;\n\t\tthis.nativeProjection = nativeProjection;\n\t\tthis.innerIndex = innerIndex;\n\t\tthis.parser = createParser(tools);\n\t}\n\n\tstart(source: AssistantMessage): boolean {\n\t\tconst outerIndex = this.projection.startText(this.innerIndex, source.content[this.innerIndex]);\n\t\treturn this.nativeProjection.startText(this.innerIndex, outerIndex);\n\t}\n\n\tfeed(text: string): boolean {\n\t\tfor (let index = 0; index < text.length; index += 1) {\n\t\t\tconst character = text.charAt(index);\n\t\t\tconst options = this.activeInvoke ? { activeInvoke: true } : undefined;\n\t\t\tfor (const segment of this.mask.feed(character, options)) this.processSegment(segment);\n\t\t}\n\t\tthis.flushText();\n\t\treturn this.sawToolCall;\n\t}\n\n\tfinish(): boolean {\n\t\tif (this.finished) return this.sawToolCall;\n\t\tfor (const segment of this.mask.finish()) this.processSegment(segment);\n\t\tthis.projectParserEvents(this.parser.finish());\n\t\tthis.flushText();\n\t\tthis.projection.finishText();\n\t\tthis.nativeProjection.extendText(this.innerIndex);\n\t\tthis.finished = true;\n\t\treturn this.sawToolCall;\n\t}\n\n\tprivate processSegment(segment: RecoveryCodeMaskSegment): void {\n\t\tif (segment.recoveryBoundary) this.projectParserEvents(this.parser.interrupt());\n\t\tif (segment.scan) this.projectParserEvents(this.parser.feed(segment.text));\n\t\telse this.textBuffer += segment.text;\n\t}\n\n\tprivate projectParserEvents(events: readonly StreamParserEvent[]): void {\n\t\tfor (const event of this.nativeProjection.assignRecoveredIds(events)) {\n\t\t\tif (event.type === \"text\") {\n\t\t\t\tthis.textBuffer += event.text;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.flushText();\n\t\t\tconst result = this.projection.projectParserEvents([event]);\n\t\t\tthis.sawToolCall = this.sawToolCall || result.sawToolCall;\n\t\t\tif (event.type === \"toolcall_start\") this.activeInvoke = true;\n\t\t\tif (event.type === \"toolcall_end\") {\n\t\t\t\tthis.activeInvoke = false;\n\t\t\t\tfor (const toolCall of result.completedToolCalls) appendRecoveryDiagnostic(this.projection, toolCall);\n\t\t\t}\n\t\t}\n\t\tthis.nativeProjection.extendText(this.innerIndex);\n\t}\n\n\tprivate flushText(): void {\n\t\tif (this.textBuffer.length === 0) return;\n\t\tthis.projection.projectParserEvents([{ type: \"text\", text: this.textBuffer }]);\n\t\tthis.textBuffer = \"\";\n\t}\n}\n"]}
1
+ {"version":3,"file":"recovery-text-projection.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-text-projection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAAE,sBAAsB,EAAgC,MAAM,yBAAyB,CAA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AC,EAAE,CAAC;YACtE,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC3B,IAAI,CAAC,UAAU,IAAI,KAAK,CAAC,IAAI,CAAC;gBAC9B,SAAS;YACV,CAAC;YACD,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAC5D,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,MAAM,CAAC,WAAW,CAAC;YAC1D,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB;gBAAE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;YAC9D,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;gBACnC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;gBAC1B,KAAK,MAAM,QAAQ,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC;oBAClD,wBAAwB,CAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;gBACpE,CAAC;YACF,CAAC;QACF,CAAC;QACD,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACnD,CAAC;IAEO,SAAS;QAChB,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO;QACzC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;QAC/E,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;IACtB,CAAC;CACD","sourcesContent":["import type { AssistantMessage, Tool } from \"../types.ts\";\nimport type { RecoveryStreamParser } from \"./protocols/anthropic-xml/recovery-stream.ts\";\nimport { createAntmlInvokeRecoveryStreamParser } from \"./protocols/antml/recovery-stream.ts\";\nimport { createRecoveryCodeMask, type RecoveryCodeMaskSegment } from \"./recovery-code-mask.ts\";\nimport { appendRecoveryDiagnostic } from \"./recovery-diagnostics.ts\";\nimport type { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport type { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\nimport type { StreamParserEvent, ToolCallFormat } from \"./types.ts\";\n\n/** Owns one ordinary assistant text block's mask/parser/projection lifecycle. */\nexport class RecoveryTextProjection {\n\tprivate readonly projection: StreamMessageProjection;\n\tprivate readonly nativeProjection: RecoveryNativeProjection;\n\tprivate readonly innerIndex: number;\n\tprivate readonly parser: RecoveryStreamParser;\n\tprivate readonly protocol: ToolCallFormat;\n\tprivate readonly mask = createRecoveryCodeMask();\n\tprivate activeInvoke = false;\n\tprivate textBuffer = \"\";\n\tprivate sawToolCall = false;\n\tprivate finished = false;\n\n\tconstructor(\n\t\ttools: readonly Tool[],\n\t\tprojection: StreamMessageProjection,\n\t\tnativeProjection: RecoveryNativeProjection,\n\t\tinnerIndex: number,\n\t\toptions: {\n\t\t\tcreateParser?: (tools: readonly Tool[]) => RecoveryStreamParser;\n\t\t\tprotocol?: ToolCallFormat;\n\t\t} = {},\n\t) {\n\t\tthis.projection = projection;\n\t\tthis.nativeProjection = nativeProjection;\n\t\tthis.innerIndex = innerIndex;\n\t\tthis.parser = (options.createParser ?? createAntmlInvokeRecoveryStreamParser)(tools);\n\t\tthis.protocol = options.protocol ?? \"antml\";\n\t}\n\n\tstart(source: AssistantMessage): boolean {\n\t\tconst outerIndex = this.projection.startText(this.innerIndex, source.content[this.innerIndex]);\n\t\treturn this.nativeProjection.startText(this.innerIndex, outerIndex);\n\t}\n\n\tfeed(text: string): boolean {\n\t\tfor (let index = 0; index < text.length; index += 1) {\n\t\t\tconst character = text.charAt(index);\n\t\t\tconst options = this.activeInvoke ? { activeInvoke: true } : undefined;\n\t\t\tfor (const segment of this.mask.feed(character, options)) this.processSegment(segment);\n\t\t}\n\t\tthis.flushText();\n\t\treturn this.sawToolCall;\n\t}\n\n\tfinish(): boolean {\n\t\tif (this.finished) return this.sawToolCall;\n\t\tfor (const segment of this.mask.finish()) this.processSegment(segment);\n\t\tthis.projectParserEvents(this.parser.finish());\n\t\tthis.flushText();\n\t\tthis.projection.finishText();\n\t\tthis.nativeProjection.extendText(this.innerIndex);\n\t\tthis.finished = true;\n\t\treturn this.sawToolCall;\n\t}\n\n\tprivate processSegment(segment: RecoveryCodeMaskSegment): void {\n\t\tif (segment.recoveryBoundary) this.projectParserEvents(this.parser.interrupt());\n\t\tif (segment.scan) this.projectParserEvents(this.parser.feed(segment.text));\n\t\telse this.textBuffer += segment.text;\n\t}\n\n\tprivate projectParserEvents(events: readonly StreamParserEvent[]): void {\n\t\tfor (const event of this.nativeProjection.assignRecoveredIds(events)) {\n\t\t\tif (event.type === \"text\") {\n\t\t\t\tthis.textBuffer += event.text;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.flushText();\n\t\t\tconst result = this.projection.projectParserEvents([event]);\n\t\t\tthis.sawToolCall = this.sawToolCall || result.sawToolCall;\n\t\t\tif (event.type === \"toolcall_start\") this.activeInvoke = true;\n\t\t\tif (event.type === \"toolcall_end\") {\n\t\t\t\tthis.activeInvoke = false;\n\t\t\t\tfor (const toolCall of result.completedToolCalls) {\n\t\t\t\t\tappendRecoveryDiagnostic(this.projection, toolCall, this.protocol);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.nativeProjection.extendText(this.innerIndex);\n\t}\n\n\tprivate flushText(): void {\n\t\tif (this.textBuffer.length === 0) return;\n\t\tthis.projection.projectParserEvents([{ type: \"text\", text: this.textBuffer }]);\n\t\tthis.textBuffer = \"\";\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-yeongyu/senpi-ai",
3
- "version": "2026.7.29",
3
+ "version": "2026.7.30",
4
4
  "description": "Unified LLM API with automatic model discovery and provider configuration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",