


























@@ -536,6 +536,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
536536index: number;
537537};
538538const blocks = output.content as Block[];
539+const blockIndexes = new Map<number, number>();
539540540541for await (const event of iterateAnthropicEvents(
541542response,
@@ -568,6 +569,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
568569index: event.index,
569570};
570571output.content.push(block);
572+blockIndexes.set(event.index, output.content.length - 1);
571573eventSink.push({
572574type: "text_start",
573575contentIndex: output.content.length - 1,
@@ -581,6 +583,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
581583index: event.index,
582584};
583585output.content.push(block);
586+blockIndexes.set(event.index, output.content.length - 1);
584587eventSink.push({
585588type: "thinking_start",
586589contentIndex: output.content.length - 1,
@@ -595,6 +598,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
595598index: event.index,
596599};
597600output.content.push(block);
601+blockIndexes.set(event.index, output.content.length - 1);
598602eventSink.push({
599603type: "thinking_start",
600604contentIndex: output.content.length - 1,
@@ -612,6 +616,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
612616index: event.index,
613617};
614618output.content.push(block);
619+blockIndexes.set(event.index, output.content.length - 1);
615620eventSink.push({
616621type: "toolcall_start",
617622contentIndex: output.content.length - 1,
@@ -620,9 +625,9 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
620625}
621626} else if (event.type === "content_block_delta") {
622627if (event.delta.type === "text_delta") {
623-const index = blocks.findIndex((b) => b.index === event.index);
624-const block = blocks[index];
625-if (block && block.type === "text") {
628+const index = blockIndexes.get(event.index);
629+const block = index === undefined ? undefined : blocks[index];
630+if (index !== undefined && block?.type === "text") {
626631block.text += event.delta.text;
627632eventSink.push({
628633type: "text_delta",
@@ -632,9 +637,9 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
632637});
633638}
634639} else if (event.delta.type === "thinking_delta") {
635-const index = blocks.findIndex((b) => b.index === event.index);
636-const block = blocks[index];
637-if (block && block.type === "thinking") {
640+const index = blockIndexes.get(event.index);
641+const block = index === undefined ? undefined : blocks[index];
642+if (index !== undefined && block?.type === "thinking") {
638643block.thinking += event.delta.thinking;
639644eventSink.push({
640645type: "thinking_delta",
@@ -644,9 +649,9 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
644649});
645650}
646651} else if (event.delta.type === "input_json_delta") {
647-const index = blocks.findIndex((b) => b.index === event.index);
648-const block = blocks[index];
649-if (block && block.type === "toolCall") {
652+const index = blockIndexes.get(event.index);
653+const block = index === undefined ? undefined : blocks[index];
654+if (index !== undefined && block?.type === "toolCall") {
650655block.partialJson += event.delta.partial_json;
651656block.arguments = parseStreamingJson(block.partialJson);
652657eventSink.push({
@@ -657,17 +662,18 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
657662});
658663}
659664} else if (event.delta.type === "signature_delta") {
660-const index = blocks.findIndex((b) => b.index === event.index);
661-const block = blocks[index];
662-if (block && block.type === "thinking") {
665+const index = blockIndexes.get(event.index);
666+const block = index === undefined ? undefined : blocks[index];
667+if (index !== undefined && block?.type === "thinking") {
663668block.thinkingSignature = block.thinkingSignature || "";
664669block.thinkingSignature += event.delta.signature;
665670}
666671}
667672} else if (event.type === "content_block_stop") {
668-const index = blocks.findIndex((b) => b.index === event.index);
669-const block = blocks[index];
670-if (block) {
673+const index = blockIndexes.get(event.index);
674+const block = index === undefined ? undefined : blocks[index];
675+if (index !== undefined && block) {
676+blockIndexes.delete(event.index);
671677delete (block as Partial<Block>).index;
672678if (block.type === "text") {
673679eventSink.push({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。