@tanstack/ai-bedrock 1.4.0 → 1.4.1
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildConverseUsage } from "./usage.js";
|
|
2
|
-
import { EventType } from "@tanstack/ai";
|
|
2
|
+
import { CONTEXT_OVERFLOW_CODE, EventType } from "@tanstack/ai";
|
|
3
3
|
// src/converse/stream-processor.ts
|
|
4
4
|
/**
|
|
5
5
|
* Converse delivers server-side failures — throttling, request validation,
|
|
@@ -31,6 +31,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
31
31
|
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
32
32
|
let usage;
|
|
33
33
|
let finishReason;
|
|
34
|
+
let contextWindowExceeded = false;
|
|
34
35
|
function* ensureRunStarted() {
|
|
35
36
|
if (hasEmittedRunStarted) return;
|
|
36
37
|
hasEmittedRunStarted = true;
|
|
@@ -196,6 +197,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
196
197
|
}
|
|
197
198
|
if ("messageStop" in ev) {
|
|
198
199
|
const stopReason = ev.messageStop?.stopReason;
|
|
200
|
+
contextWindowExceeded = stopReason === "model_context_window_exceeded";
|
|
199
201
|
finishReason = stopReason === "tool_use" ? "tool_calls" : stopReason === "max_tokens" ? "length" : stopReason === "content_filtered" || stopReason === "guardrail_intervened" ? "content_filter" : "stop";
|
|
200
202
|
continue;
|
|
201
203
|
}
|
|
@@ -223,6 +225,22 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
223
225
|
timestamp: Date.now()
|
|
224
226
|
};
|
|
225
227
|
yield* closeReasoning();
|
|
228
|
+
if (contextWindowExceeded) {
|
|
229
|
+
const message = "The response was cut off because the model reached the end of its context window.";
|
|
230
|
+
yield {
|
|
231
|
+
type: EventType.RUN_ERROR,
|
|
232
|
+
...model && { model },
|
|
233
|
+
timestamp: Date.now(),
|
|
234
|
+
message,
|
|
235
|
+
code: CONTEXT_OVERFLOW_CODE,
|
|
236
|
+
error: {
|
|
237
|
+
message,
|
|
238
|
+
code: CONTEXT_OVERFLOW_CODE
|
|
239
|
+
},
|
|
240
|
+
...usage && { usage }
|
|
241
|
+
};
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
226
244
|
yield {
|
|
227
245
|
type: EventType.RUN_FINISHED,
|
|
228
246
|
runId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId: string; parentRunId?: string; model?: string },\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const { threadId, parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n arguments: string\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n arguments: '',\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n toolCall.arguments += delta.toolUse.input\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.now(),\n finishReason: finishReason ?? 'stop',\n ...(usage && { usage }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,2BAA2B,IAAgC;CACzE,IAAI,6BAA6B,MAAM,GAAG,yBACxC,MAAM,GAAG;CAEX,IAAI,+BAA+B,MAAM,GAAG,2BAC1C,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,iCAAiC,MAAM,GAAG,6BAC5C,MAAM,GAAG;AAEb;;;;;AAMA,gBAAuB,sBACrB,QACA,cACA,WACkC;CAClC,MAAM,QAAQ,aAAa;CAC3B,MAAM,EAAE,UAAU,aAAa,UAAU;CACzC,MAAM,YAAY,aAAa;CAE/B,IAAI,uBAA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,MAAM,CAAC,OAAO,aAAa,kBAAkB;EAChD,IAAI,CAAC,SAAS,SAAS;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB,YAAY,SAAS;GACrB,MAAM,SAAS;GACf,cAAc,SAAS;GACvB,WAAW,KAAK,IAAI;EACtB;EACA,iBAAiB,OAAO,KAAK;CAC/B;CAGA,IAAI,4BACF,MAAM;EACJ,MAAM,UAAU;EAChB;EACA,WAAW,KAAK,IAAI;CACtB;CAIF,OAAO,eAAe;CAItB,MAAM;EACJ,MAAM,UAAU;EAChB;EACA;EACA,WAAW,KAAK,IAAI;EACpB,cAAc,gBAAgB;EAC9B,GAAI,SAAS,EAAE,MAAM;CACvB;AACF"}
|
|
1
|
+
{"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { CONTEXT_OVERFLOW_CODE, EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId: string; parentRunId?: string; model?: string },\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const { threadId, parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n arguments: string\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n // The model filled its context window before it finished (stopReason\n // `model_context_window_exceeded`). The answer is cut off, so the run ends\n // with a RUN_ERROR instead of a clean RUN_FINISHED.\n let contextWindowExceeded = false\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n arguments: '',\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n toolCall.arguments += delta.toolUse.input\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason: string | undefined = ev.messageStop?.stopReason\n contextWindowExceeded = stopReason === 'model_context_window_exceeded'\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n if (contextWindowExceeded) {\n const message =\n 'The response was cut off because the model reached the end of its context window.'\n yield {\n type: EventType.RUN_ERROR,\n ...(model && { model }),\n timestamp: Date.now(),\n message,\n code: CONTEXT_OVERFLOW_CODE,\n error: { message, code: CONTEXT_OVERFLOW_CODE },\n ...(usage && { usage }),\n }\n return\n }\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.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|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-bedrock",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Amazon Bedrock adapter for TanStack AI with chat, tools, reasoning, embeddings, and reranking.",
|
|
6
6
|
"author": "",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@aws-sdk/client-bedrock-runtime": "^3.1057.0",
|
|
53
53
|
"@aws-sdk/credential-providers": "^3.1057.0",
|
|
54
54
|
"@smithy/types": "^4.14.2",
|
|
55
|
-
"@tanstack/ai": "^1.
|
|
55
|
+
"@tanstack/ai": "^1.7.0"
|
|
56
56
|
},
|
|
57
57
|
"peerDependenciesMeta": {
|
|
58
58
|
"@aws-sdk/client-bedrock-runtime": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"openai": "^7.30.0",
|
|
70
|
-
"@tanstack/openai-base": "^1.0
|
|
70
|
+
"@tanstack/openai-base": "^1.1.0"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "vite build",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventType } from '@tanstack/ai'
|
|
1
|
+
import { CONTEXT_OVERFLOW_CODE, EventType } from '@tanstack/ai'
|
|
2
2
|
import { buildConverseUsage } from './usage'
|
|
3
3
|
import type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'
|
|
4
4
|
import type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'
|
|
@@ -79,6 +79,10 @@ export async function* processConverseStream(
|
|
|
79
79
|
// contract (usage may arrive after the finish signal).
|
|
80
80
|
let usage: TokenUsage | undefined
|
|
81
81
|
let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined
|
|
82
|
+
// The model filled its context window before it finished (stopReason
|
|
83
|
+
// `model_context_window_exceeded`). The answer is cut off, so the run ends
|
|
84
|
+
// with a RUN_ERROR instead of a clean RUN_FINISHED.
|
|
85
|
+
let contextWindowExceeded = false
|
|
82
86
|
|
|
83
87
|
// Lazily emit RUN_STARTED exactly once, before the first content event.
|
|
84
88
|
function* ensureRunStarted(): Generator<AdapterYieldChunk> {
|
|
@@ -282,7 +286,8 @@ export async function* processConverseStream(
|
|
|
282
286
|
}
|
|
283
287
|
|
|
284
288
|
if ('messageStop' in ev) {
|
|
285
|
-
const stopReason = ev.messageStop?.stopReason
|
|
289
|
+
const stopReason: string | undefined = ev.messageStop?.stopReason
|
|
290
|
+
contextWindowExceeded = stopReason === 'model_context_window_exceeded'
|
|
286
291
|
// Map Converse stopReason to AG-UI's narrower finishReason vocabulary.
|
|
287
292
|
finishReason =
|
|
288
293
|
stopReason === 'tool_use'
|
|
@@ -334,6 +339,21 @@ export async function* processConverseStream(
|
|
|
334
339
|
// Close any reasoning lifecycle that text never closed.
|
|
335
340
|
yield* closeReasoning()
|
|
336
341
|
|
|
342
|
+
if (contextWindowExceeded) {
|
|
343
|
+
const message =
|
|
344
|
+
'The response was cut off because the model reached the end of its context window.'
|
|
345
|
+
yield {
|
|
346
|
+
type: EventType.RUN_ERROR,
|
|
347
|
+
...(model && { model }),
|
|
348
|
+
timestamp: Date.now(),
|
|
349
|
+
message,
|
|
350
|
+
code: CONTEXT_OVERFLOW_CODE,
|
|
351
|
+
error: { message, code: CONTEXT_OVERFLOW_CODE },
|
|
352
|
+
...(usage && { usage }),
|
|
353
|
+
}
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
|
|
337
357
|
// Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire
|
|
338
358
|
// shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).
|
|
339
359
|
yield {
|