@tanstack/ai-bedrock 0.5.1 → 0.6.0
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.
- package/dist/esm/adapters/converse-text.d.ts +9 -2
- package/dist/esm/adapters/converse-text.js +115 -29
- package/dist/esm/adapters/converse-text.js.map +1 -1
- package/dist/esm/adapters/responses-text.d.ts +11 -2
- package/dist/esm/adapters/responses-text.js +10 -1
- package/dist/esm/adapters/responses-text.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +11 -2
- package/dist/esm/adapters/text.js +10 -1
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/converse/message-converter.d.ts +12 -1
- package/dist/esm/converse/message-converter.js +93 -22
- package/dist/esm/converse/message-converter.js.map +1 -1
- package/dist/esm/converse/prompt-cache.d.ts +16 -0
- package/dist/esm/converse/prompt-cache.js +43 -0
- package/dist/esm/converse/prompt-cache.js.map +1 -0
- package/dist/esm/converse/reasoning.d.ts +28 -0
- package/dist/esm/converse/reasoning.js +42 -0
- package/dist/esm/converse/reasoning.js.map +1 -0
- package/dist/esm/converse/stream-processor.d.ts +3 -29
- package/dist/esm/converse/stream-processor.js +80 -45
- package/dist/esm/converse/stream-processor.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/model-meta.d.ts +80 -0
- package/dist/esm/model-meta.js +54 -1
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/text/responses-provider-options.d.ts +0 -4
- package/dist/esm/text/text-provider-options.d.ts +2 -4
- package/dist/esm/utils/client.d.ts +10 -0
- package/dist/esm/utils/client.js +10 -1
- package/dist/esm/utils/client.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/converse-text.ts +213 -20
- package/src/adapters/responses-text.ts +32 -2
- package/src/adapters/text.ts +33 -3
- package/src/converse/message-converter.ts +166 -31
- package/src/converse/prompt-cache.ts +53 -0
- package/src/converse/reasoning.ts +62 -0
- package/src/converse/stream-processor.ts +117 -52
- package/src/model-meta.ts +76 -0
- package/src/text/responses-provider-options.ts +0 -2
- package/src/text/text-provider-options.ts +2 -4
- package/src/utils/client.ts +16 -0
|
@@ -16,35 +16,9 @@ function throwIfConverseStreamError(ev) {
|
|
|
16
16
|
if ("throttlingException" in ev && ev.throttlingException) throw ev.throttlingException;
|
|
17
17
|
if ("serviceUnavailableException" in ev && ev.serviceUnavailableException) throw ev.serviceUnavailableException;
|
|
18
18
|
}
|
|
19
|
-
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* lifecycle shape so the activity layer / agent loop behave identically across
|
|
23
|
-
* providers. (Reasoning surfaces only the message-level events —
|
|
24
|
-
* `REASONING_MESSAGE_START/CONTENT/END` — which is all the core accumulator
|
|
25
|
-
* consumes; the `REASONING_START`/`REASONING_END`/`STEP_*` boundary events
|
|
26
|
-
* openai-base also emits are no-ops in the engine and are not reproduced here.)
|
|
27
|
-
*
|
|
28
|
-
* Lifecycle ownership matches openai-base: this processor emits the full
|
|
29
|
-
* success-path lifecycle itself — `RUN_STARTED` lazily before the first event,
|
|
30
|
-
* `TEXT_MESSAGE_*` / `TOOL_CALL_*` / `REASONING_MESSAGE_*` for content, and a
|
|
31
|
-
* single terminal `RUN_FINISHED` once the iterator is exhausted (so the trailing
|
|
32
|
-
* `metadata` usage event is folded into the finish event regardless of arrival
|
|
33
|
-
* order). The calling adapter only owns the catch/`RUN_ERROR` path — so any
|
|
34
|
-
* in-band Converse error event (see `throwIfConverseStreamError`) is thrown to
|
|
35
|
-
* surface there rather than ending the stream as a clean truncated success.
|
|
36
|
-
*
|
|
37
|
-
* Converse streams tool-call arguments as partial-JSON string fragments inside
|
|
38
|
-
* `contentBlockDelta.delta.toolUse.input`; each fragment is emitted as a
|
|
39
|
-
* `TOOL_CALL_ARGS` `delta`, mirroring OpenAI's `function.arguments` deltas.
|
|
40
|
-
*
|
|
41
|
-
* @param stream - The Converse event stream from `ConverseStreamCommand`.
|
|
42
|
-
* @param newMessageId - Factory for fresh ids — run, thread, message, and
|
|
43
|
-
* tool-call ids (the adapter passes `() => this.generateId()`).
|
|
44
|
-
* @param lifecycle - Incoming run lifecycle ids, threaded onto the emitted
|
|
45
|
-
* `RUN_STARTED`/`RUN_FINISHED` so the chat path matches every sibling adapter
|
|
46
|
-
* (openai-base reuses `options.threadId`/`parentRunId`). Defaults preserve the
|
|
47
|
-
* previous behaviour (fresh `threadId`, no `parentRunId`).
|
|
19
|
+
/** Map Converse blocks to run, text, reasoning, and tool events.
|
|
20
|
+
* Keep signatures and encrypted bytes separate for each contentBlockIndex.
|
|
21
|
+
* Finish after trailing usage events. The adapter handles thrown errors.
|
|
48
22
|
*/
|
|
49
23
|
async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
|
|
50
24
|
const runId = newMessageId();
|
|
@@ -54,8 +28,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
|
|
|
54
28
|
let hasEmittedRunStarted = false;
|
|
55
29
|
let accumulatedContent = "";
|
|
56
30
|
let hasEmittedTextMessageStart = false;
|
|
57
|
-
|
|
58
|
-
let hasClosedReasoning = false;
|
|
31
|
+
const reasoningByIndex = /* @__PURE__ */ new Map();
|
|
59
32
|
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
60
33
|
let usage;
|
|
61
34
|
let finishReason;
|
|
@@ -71,12 +44,43 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
|
|
|
71
44
|
timestamp: Date.now()
|
|
72
45
|
};
|
|
73
46
|
}
|
|
74
|
-
function*
|
|
75
|
-
|
|
76
|
-
|
|
47
|
+
function* encryptedReasoning(index) {
|
|
48
|
+
const reasoning = reasoningByIndex.get(index);
|
|
49
|
+
if (!reasoning) return;
|
|
50
|
+
const encrypted = reasoning.redacted ? Buffer.concat(reasoning.bytes).toString("base64") : reasoning.signature;
|
|
51
|
+
if (!encrypted || encrypted === reasoning.encrypted) return;
|
|
52
|
+
reasoning.encrypted = encrypted;
|
|
53
|
+
yield {
|
|
54
|
+
type: EventType.REASONING_ENCRYPTED_VALUE,
|
|
55
|
+
entityId: reasoning.id,
|
|
56
|
+
subtype: "message",
|
|
57
|
+
encryptedValue: encrypted,
|
|
58
|
+
...reasoning.redacted && { stepId: reasoning.id.startsWith("redacted_thinking-") ? reasoning.id : "redacted_thinking-" + reasoning.id },
|
|
59
|
+
timestamp: Date.now()
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function* closeReasoning(index) {
|
|
63
|
+
for (const [blockIndex, reasoning] of reasoningByIndex) {
|
|
64
|
+
if (index !== void 0 && index !== blockIndex) continue;
|
|
65
|
+
yield* encryptedReasoning(blockIndex);
|
|
66
|
+
if (reasoning.closed) continue;
|
|
67
|
+
reasoning.closed = true;
|
|
68
|
+
yield {
|
|
69
|
+
type: EventType.STEP_FINISHED,
|
|
70
|
+
stepId: reasoning.id,
|
|
71
|
+
stepName: reasoning.id,
|
|
72
|
+
delta: "",
|
|
73
|
+
content: reasoning.text,
|
|
74
|
+
timestamp: Date.now()
|
|
75
|
+
};
|
|
77
76
|
yield {
|
|
78
77
|
type: EventType.REASONING_MESSAGE_END,
|
|
79
|
-
messageId:
|
|
78
|
+
messageId: reasoning.id,
|
|
79
|
+
timestamp: Date.now()
|
|
80
|
+
};
|
|
81
|
+
yield {
|
|
82
|
+
type: EventType.REASONING_END,
|
|
83
|
+
messageId: reasoning.id,
|
|
80
84
|
timestamp: Date.now()
|
|
81
85
|
};
|
|
82
86
|
}
|
|
@@ -123,22 +127,52 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
|
|
|
123
127
|
};
|
|
124
128
|
continue;
|
|
125
129
|
}
|
|
126
|
-
if (delta && "reasoningContent" in delta && delta.reasoningContent
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
if (delta && "reasoningContent" in delta && delta.reasoningContent) {
|
|
131
|
+
const fragment = delta.reasoningContent;
|
|
132
|
+
let reasoning = reasoningByIndex.get(index);
|
|
133
|
+
if (!reasoning) {
|
|
134
|
+
const redacted = "redactedContent" in fragment && (fragment.redactedContent?.length ?? 0) > 0;
|
|
135
|
+
reasoning = {
|
|
136
|
+
id: (redacted ? "redacted_thinking-" : "") + newMessageId(),
|
|
137
|
+
text: "",
|
|
138
|
+
signature: "",
|
|
139
|
+
bytes: [],
|
|
140
|
+
redacted,
|
|
141
|
+
closed: false
|
|
142
|
+
};
|
|
143
|
+
reasoningByIndex.set(index, reasoning);
|
|
144
|
+
yield {
|
|
145
|
+
type: EventType.STEP_STARTED,
|
|
146
|
+
stepId: reasoning.id,
|
|
147
|
+
stepName: reasoning.id,
|
|
148
|
+
stepType: "thinking",
|
|
149
|
+
timestamp: Date.now()
|
|
150
|
+
};
|
|
129
151
|
yield {
|
|
130
152
|
type: EventType.REASONING_MESSAGE_START,
|
|
131
|
-
messageId:
|
|
153
|
+
messageId: reasoning.id,
|
|
132
154
|
role: "reasoning",
|
|
133
155
|
timestamp: Date.now()
|
|
134
156
|
};
|
|
135
157
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
158
|
+
if (!reasoning.redacted && "text" in fragment && fragment.text !== void 0) {
|
|
159
|
+
reasoning.text += fragment.text;
|
|
160
|
+
yield {
|
|
161
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
162
|
+
messageId: reasoning.id,
|
|
163
|
+
delta: fragment.text,
|
|
164
|
+
timestamp: Date.now()
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
if (!reasoning.redacted && "signature" in fragment && fragment.signature !== void 0) reasoning.signature += fragment.signature;
|
|
168
|
+
if ("redactedContent" in fragment && fragment.redactedContent && fragment.redactedContent.length > 0) {
|
|
169
|
+
if (!reasoning.redacted) reasoning.encrypted = void 0;
|
|
170
|
+
reasoning.redacted = true;
|
|
171
|
+
reasoning.text = "";
|
|
172
|
+
reasoning.signature = "";
|
|
173
|
+
reasoning.bytes.push(fragment.redactedContent);
|
|
174
|
+
yield* encryptedReasoning(index);
|
|
175
|
+
}
|
|
142
176
|
continue;
|
|
143
177
|
}
|
|
144
178
|
if (delta && "text" in delta && delta.text !== void 0) {
|
|
@@ -165,6 +199,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
|
|
|
165
199
|
}
|
|
166
200
|
if ("contentBlockStop" in ev) {
|
|
167
201
|
const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0;
|
|
202
|
+
yield* closeReasoning(stopIndex);
|
|
168
203
|
const toolCall = toolCallsByIndex.get(stopIndex);
|
|
169
204
|
if (toolCall?.started) {
|
|
170
205
|
yield {
|
|
@@ -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/**\n * Maps a Bedrock Converse `ConverseStreamOutput` event stream to the TanStack\n * AG-UI `StreamChunk` lifecycle, following `openai-base`'s `processStreamChunks`\n * lifecycle shape so the activity layer / agent loop behave identically across\n * providers. (Reasoning surfaces only the message-level events —\n * `REASONING_MESSAGE_START/CONTENT/END` — which is all the core accumulator\n * consumes; the `REASONING_START`/`REASONING_END`/`STEP_*` boundary events\n * openai-base also emits are no-ops in the engine and are not reproduced here.)\n *\n * Lifecycle ownership matches openai-base: this processor emits the full\n * success-path lifecycle itself — `RUN_STARTED` lazily before the first event,\n * `TEXT_MESSAGE_*` / `TOOL_CALL_*` / `REASONING_MESSAGE_*` for content, and a\n * single terminal `RUN_FINISHED` once the iterator is exhausted (so the trailing\n * `metadata` usage event is folded into the finish event regardless of arrival\n * order). The calling adapter only owns the catch/`RUN_ERROR` path — so any\n * in-band Converse error event (see `throwIfConverseStreamError`) is thrown to\n * surface there rather than ending the stream as a clean truncated success.\n *\n * Converse streams tool-call arguments as partial-JSON string fragments inside\n * `contentBlockDelta.delta.toolUse.input`; each fragment is emitted as a\n * `TOOL_CALL_ARGS` `delta`, mirroring OpenAI's `function.arguments` deltas.\n *\n * @param stream - The Converse event stream from `ConverseStreamCommand`.\n * @param newMessageId - Factory for fresh ids — run, thread, message, and\n * tool-call ids (the adapter passes `() => this.generateId()`).\n * @param lifecycle - Incoming run lifecycle ids, threaded onto the emitted\n * `RUN_STARTED`/`RUN_FINISHED` so the chat path matches every sibling adapter\n * (openai-base reuses `options.threadId`/`parentRunId`). Defaults preserve the\n * previous behaviour (fresh `threadId`, no `parentRunId`).\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 = lifecycle.threadId ?? newMessageId()\n const { 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 let reasoningMessageId: string | undefined\n let hasClosedReasoning = false\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 { id: string; name: string; started: boolean }\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* closeReasoning(): Generator<AdapterYieldChunk> {\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\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 })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n toolName: 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 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 // Reasoning content.\n if (\n delta &&\n 'reasoningContent' in delta &&\n delta.reasoningContent &&\n 'text' in delta.reasoningContent &&\n delta.reasoningContent.text !== undefined\n ) {\n if (!reasoningMessageId) {\n reasoningMessageId = newMessageId()\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: delta.reasoningContent.text,\n timestamp: Date.now(),\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 const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: 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 ? '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 toolCallName: toolCall.name,\n toolName: 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,gBAAuB,sBACrB,QACA,cACA,YAAyE,CAAC,GACxC;CAClC,MAAM,QAAQ,aAAa;CAC3B,MAAM,WA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BACA;GACV;EACF;EAEA,IAAI,cAAc,IAAI;GACpB,MAAM,IAAI,GAAG,UAAU;GACvB,IAAI,GACF,QAAQ,mBAAmB,CAAC;GAE9B;EACF;CACF;CAIA,OAAO,iBAAiB;CAGxB,KAAK,MAAM,CAAC,OAAO,aAAa,kBAAkB;EAChD,IAAI,CAAC,SAAS,SAAS;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB,YAAY,SAAS;GACrB,cAAc,SAAS;GACvB,UAAU,SAAS;GACnB,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 { 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 = lifecycle.threadId ?? newMessageId()\n const { 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 text: 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 }\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 ...(reasoning.redacted && {\n stepId: reasoning.id.startsWith('redacted_thinking-')\n ? reasoning.id\n : 'redacted_thinking-' + reasoning.id,\n }),\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.STEP_FINISHED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n delta: '',\n content: reasoning.text,\n timestamp: Date.now(),\n }\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 })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n toolName: 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 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 text: '',\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.STEP_STARTED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n stepType: 'thinking',\n timestamp: Date.now(),\n }\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 reasoning.text += fragment.text\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.text = ''\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 toolCallName: toolCall.name,\n toolName: 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 ? '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 toolCallName: toolCall.name,\n toolName: 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,YAAyE,CAAC,GACxC;CAClC,MAAM,QAAQ,aAAa;CAC3B,MAAM,WAAW,UAAU,YAAY,aAAa;CACpD,MAAM,EAAE,aAAa,UAAU;CAC/B,MAAM,YAAY,aAAa;CAE/B,IAAI,uBAAuB;CAG3B,IAAI,qBAAqB;CACzB,IAAI,6BAA6B;CAGjC,MAAM,mCAAmB,IAAI,IAW3B;CAKF,MAAM,mCAAmB,IAAI,IAO3B;CAKF,IAAI;CACJ,IAAI;CAGJ,UAAU,mBAAiD;EACzD,IAAI,sBAAsB;EAC1B,uBAAuB;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB;GACA;GACA;GACA,GAAI,SAAS,EAAE,MAAM;GACrB,WAAW,KAAK,IAAI;EACtB;CACF;CAIA,UAAU,mBAAmB,OAA6C;EACxE,MAAM,YAAY,iBAAiB,IAAI,KAAK;EAC5C,IAAI,CAAC,WAAW;EAChB,MAAM,YAAY,UAAU,WACxB,OAAO,OAAO,UAAU,KAAK,CAAC,CAAC,SAAS,QAAQ,IAChD,UAAU;EACd,IAAI,CAAC,aAAa,cAAc,UAAU,WAAW;EACrD,UAAU,YAAY;EACtB,MAAM;GACJ,MAAM,UAAU;GAChB,UAAU,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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"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { resolveBedrockAuth } from "./utils/auth.js";
|
|
2
2
|
import { withBedrockDefaults } from "./utils/client.js";
|
|
3
|
+
import { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RESPONSES_MODELS } from "./model-meta.js";
|
|
3
4
|
import { BedrockTextAdapter, createBedrockChat } from "./adapters/text.js";
|
|
4
5
|
import { BedrockResponsesTextAdapter, createBedrockResponsesText } from "./adapters/responses-text.js";
|
|
5
6
|
import { BedrockConverseTextAdapter, createBedrockConverse } from "./adapters/converse-text.js";
|
|
6
|
-
import { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RESPONSES_MODELS } from "./model-meta.js";
|
|
7
7
|
import { BedrockEmbeddingAdapter, bedrockEmbedding, createBedrockEmbedding } from "./adapters/embedding.js";
|
|
8
8
|
//#region src/index.ts
|
|
9
9
|
/**
|
package/dist/esm/model-meta.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { GENERATED_BEDROCK_MODELS } from './model-catalog.generated.js';
|
|
2
|
+
import { Modality, ModelReasoning } from '@tanstack/ai';
|
|
3
|
+
import { ModelReasoningCapability } from '@tanstack/ai/adapter-internals';
|
|
2
4
|
import { BedrockTextProviderOptions } from './text/text-provider-options.js';
|
|
3
5
|
import { BedrockConverseProviderOptions } from './converse/provider-options.js';
|
|
4
6
|
import { BedrockCohereEmbeddingProviderOptions, BedrockEmbeddingProviderOptions, BedrockTitanImageEmbeddingProviderOptions, BedrockTitanTextEmbeddingProviderOptions } from './embedding/embedding-provider-options.js';
|
|
@@ -21,6 +23,11 @@ export declare const BEDROCK_RESPONSES_MODELS: ReadonlyArray<BedrockResponsesMod
|
|
|
21
23
|
export type BedrockModelInputModalitiesByName = {
|
|
22
24
|
[E in Entry as E['id']]: E['input'];
|
|
23
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Runtime map from model id to its input modalities, for the text adapters'
|
|
28
|
+
* `inputModalities`. An unknown id gives `undefined`.
|
|
29
|
+
*/
|
|
30
|
+
export declare const BEDROCK_MODEL_INPUT_MODALITIES: Readonly<Record<string, ReadonlyArray<Modality>>>;
|
|
24
31
|
/** Provider options per model. Same options for every model; keyed over the full catalog. */
|
|
25
32
|
export type BedrockChatModelProviderOptionsByName = {
|
|
26
33
|
[E in Entry as E['id']]: BedrockTextProviderOptions;
|
|
@@ -36,6 +43,79 @@ export type BedrockChatModelToolCapabilitiesByName = {
|
|
|
36
43
|
export type ResolveProviderOptions<TModel extends string> = TModel extends keyof BedrockChatModelProviderOptionsByName ? BedrockChatModelProviderOptionsByName[TModel] : BedrockTextProviderOptions;
|
|
37
44
|
export type ResolveConverseProviderOptions<TModel extends string> = TModel extends keyof BedrockConverseModelProviderOptionsByName ? BedrockConverseModelProviderOptionsByName[TModel] : BedrockConverseProviderOptions;
|
|
38
45
|
export type ResolveInputModalities<TModel extends string> = TModel extends keyof BedrockModelInputModalitiesByName ? BedrockModelInputModalitiesByName[TModel] : readonly ['text'];
|
|
46
|
+
declare const BEDROCK_REASONING: {
|
|
47
|
+
'openai.gpt-oss-120b-1:0': {
|
|
48
|
+
readonly levels: {
|
|
49
|
+
readonly low: "low";
|
|
50
|
+
readonly medium: "medium";
|
|
51
|
+
readonly high: "high";
|
|
52
|
+
};
|
|
53
|
+
readonly budget: false;
|
|
54
|
+
};
|
|
55
|
+
'openai.gpt-oss-20b-1:0': {
|
|
56
|
+
readonly levels: {
|
|
57
|
+
readonly low: "low";
|
|
58
|
+
readonly medium: "medium";
|
|
59
|
+
readonly high: "high";
|
|
60
|
+
};
|
|
61
|
+
readonly budget: false;
|
|
62
|
+
};
|
|
63
|
+
'us.anthropic.claude-sonnet-4-5-20250929-v1:0': {
|
|
64
|
+
readonly levels: {
|
|
65
|
+
readonly off: "off";
|
|
66
|
+
readonly high: "high";
|
|
67
|
+
};
|
|
68
|
+
readonly budget: true;
|
|
69
|
+
};
|
|
70
|
+
'us.anthropic.claude-haiku-4-5-20251001-v1:0': {
|
|
71
|
+
readonly levels: {
|
|
72
|
+
readonly off: "off";
|
|
73
|
+
readonly high: "high";
|
|
74
|
+
};
|
|
75
|
+
readonly budget: true;
|
|
76
|
+
};
|
|
77
|
+
'google.gemma-4-31b': {
|
|
78
|
+
readonly levels: {
|
|
79
|
+
readonly off: "none";
|
|
80
|
+
readonly high: "high";
|
|
81
|
+
};
|
|
82
|
+
readonly budget: false;
|
|
83
|
+
};
|
|
84
|
+
'google.gemma-4-26b-a4b': {
|
|
85
|
+
readonly levels: {
|
|
86
|
+
readonly off: "none";
|
|
87
|
+
readonly high: "high";
|
|
88
|
+
};
|
|
89
|
+
readonly budget: false;
|
|
90
|
+
};
|
|
91
|
+
'google.gemma-4-e2b': {
|
|
92
|
+
readonly levels: {
|
|
93
|
+
readonly off: "none";
|
|
94
|
+
readonly high: "high";
|
|
95
|
+
};
|
|
96
|
+
readonly budget: false;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Each model's reasoning levels, and whether it takes a token budget, for
|
|
101
|
+
* `chat({ reasoning })`. A model that is not here does not reason.
|
|
102
|
+
*/
|
|
103
|
+
export type BedrockModelReasoningByName = {
|
|
104
|
+
[K in keyof typeof BEDROCK_REASONING]: ModelReasoningCapability<(typeof BEDROCK_REASONING)[K]>;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Runtime map from model id to its reasoning data, for the text adapters. An
|
|
108
|
+
* unknown id gives `undefined`: the adapter sends no reasoning field.
|
|
109
|
+
*/
|
|
110
|
+
export declare const BEDROCK_MODEL_REASONING: Readonly<Record<string, ModelReasoning>>;
|
|
111
|
+
/**
|
|
112
|
+
* The Claude families that reject a forced tool (`any` or a named `tool`) on
|
|
113
|
+
* every request, with or without thinking. A Bedrock id has the family name
|
|
114
|
+
* inside it, for example `us.anthropic.claude-opus-5-5-...`.
|
|
115
|
+
* ponytail: names, not catalog entries, because the generated catalog has no
|
|
116
|
+
* entry for these models yet. Move it to the catalog when it has them.
|
|
117
|
+
*/
|
|
118
|
+
export declare const BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES: ReadonlyArray<string>;
|
|
39
119
|
/**
|
|
40
120
|
* Embedding models reachable through Bedrock's `InvokeModel` API. These are
|
|
41
121
|
* not part of the generated Converse catalog (embedding models have no
|
package/dist/esm/model-meta.js
CHANGED
|
@@ -1,10 +1,63 @@
|
|
|
1
1
|
import { GENERATED_BEDROCK_MODELS } from "./model-catalog.generated.js";
|
|
2
2
|
//#region src/model-meta.ts
|
|
3
|
+
var BUDGET_OFF_HIGH = {
|
|
4
|
+
levels: {
|
|
5
|
+
off: "off",
|
|
6
|
+
high: "high"
|
|
7
|
+
},
|
|
8
|
+
budget: true
|
|
9
|
+
};
|
|
10
|
+
var EFFORT_LOW_TO_HIGH = {
|
|
11
|
+
levels: {
|
|
12
|
+
low: "low",
|
|
13
|
+
medium: "medium",
|
|
14
|
+
high: "high"
|
|
15
|
+
},
|
|
16
|
+
budget: false
|
|
17
|
+
};
|
|
18
|
+
var EFFORT_NONE_HIGH = {
|
|
19
|
+
levels: {
|
|
20
|
+
off: "none",
|
|
21
|
+
high: "high"
|
|
22
|
+
},
|
|
23
|
+
budget: false
|
|
24
|
+
};
|
|
3
25
|
/** Runtime catalogs. Cast-free narrowing via a type predicate (the ai-bedrock pattern). */
|
|
4
26
|
var BEDROCK_CONVERSE_MODELS = GENERATED_BEDROCK_MODELS.filter((m) => m.apis.converse).map((m) => m.id);
|
|
5
27
|
var BEDROCK_CHAT_MODELS = GENERATED_BEDROCK_MODELS.filter((m) => m.apis.chat).map((m) => m.id);
|
|
6
28
|
var BEDROCK_RESPONSES_MODELS = GENERATED_BEDROCK_MODELS.filter((m) => m.apis.responses).map((m) => m.id);
|
|
7
29
|
/**
|
|
30
|
+
* Runtime map from model id to its input modalities, for the text adapters'
|
|
31
|
+
* `inputModalities`. An unknown id gives `undefined`.
|
|
32
|
+
*/
|
|
33
|
+
var BEDROCK_MODEL_INPUT_MODALITIES = Object.fromEntries(GENERATED_BEDROCK_MODELS.map((m) => [m.id, m.input]));
|
|
34
|
+
/**
|
|
35
|
+
* Runtime map from model id to its reasoning data, for the text adapters. An
|
|
36
|
+
* unknown id gives `undefined`: the adapter sends no reasoning field.
|
|
37
|
+
*/
|
|
38
|
+
var BEDROCK_MODEL_REASONING = {
|
|
39
|
+
"openai.gpt-oss-120b-1:0": EFFORT_LOW_TO_HIGH,
|
|
40
|
+
"openai.gpt-oss-20b-1:0": EFFORT_LOW_TO_HIGH,
|
|
41
|
+
"us.anthropic.claude-sonnet-4-5-20250929-v1:0": BUDGET_OFF_HIGH,
|
|
42
|
+
"us.anthropic.claude-haiku-4-5-20251001-v1:0": BUDGET_OFF_HIGH,
|
|
43
|
+
"google.gemma-4-31b": EFFORT_NONE_HIGH,
|
|
44
|
+
"google.gemma-4-26b-a4b": EFFORT_NONE_HIGH,
|
|
45
|
+
"google.gemma-4-e2b": EFFORT_NONE_HIGH
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The Claude families that reject a forced tool (`any` or a named `tool`) on
|
|
49
|
+
* every request, with or without thinking. A Bedrock id has the family name
|
|
50
|
+
* inside it, for example `us.anthropic.claude-opus-5-5-...`.
|
|
51
|
+
* ponytail: names, not catalog entries, because the generated catalog has no
|
|
52
|
+
* entry for these models yet. Move it to the catalog when it has them.
|
|
53
|
+
*/
|
|
54
|
+
var BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES = [
|
|
55
|
+
"claude-fable-5-1",
|
|
56
|
+
"claude-mythos-5-1",
|
|
57
|
+
"claude-opus-5-5",
|
|
58
|
+
"claude-sonnet-5-5"
|
|
59
|
+
];
|
|
60
|
+
/**
|
|
8
61
|
* Embedding models reachable through Bedrock's `InvokeModel` API. These are
|
|
9
62
|
* not part of the generated Converse catalog (embedding models have no
|
|
10
63
|
* conversational surface), so they're maintained by hand here.
|
|
@@ -16,6 +69,6 @@ var BEDROCK_EMBEDDING_MODELS = [
|
|
|
16
69
|
"cohere.embed-multilingual-v3"
|
|
17
70
|
];
|
|
18
71
|
//#endregion
|
|
19
|
-
export { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RESPONSES_MODELS };
|
|
72
|
+
export { BEDROCK_CHAT_MODELS, BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING, BEDROCK_RESPONSES_MODELS };
|
|
20
73
|
|
|
21
74
|
//# sourceMappingURL=model-meta.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-meta.js","names":[],"sources":["../../src/model-meta.ts"],"sourcesContent":["import { GENERATED_BEDROCK_MODELS } from './model-catalog.generated.js'\nimport type { BedrockTextProviderOptions } from './text/text-provider-options'\nimport type { BedrockConverseProviderOptions } from './converse/provider-options'\nimport type {\n BedrockCohereEmbeddingProviderOptions,\n BedrockEmbeddingProviderOptions,\n BedrockTitanImageEmbeddingProviderOptions,\n BedrockTitanTextEmbeddingProviderOptions,\n} from './embedding/embedding-provider-options'\n\ntype Entry = (typeof GENERATED_BEDROCK_MODELS)[number]\n\n/**\n * Type-level per-API filter over the generated catalog. Because the catalog is\n * `as const`, `Extract` preserves literal `id` unions (no widening to `string`).\n */\ntype IdsWhere<TApi extends 'converse' | 'chat' | 'responses'> = Extract<\n Entry,\n { apis: Record<TApi, true> }\n>['id']\n\nexport type BedrockConverseModels = IdsWhere<'converse'>\nexport type BedrockChatModels = IdsWhere<'chat'>\nexport type BedrockResponsesModels = IdsWhere<'responses'>\n\n/** Runtime catalogs. Cast-free narrowing via a type predicate (the ai-bedrock pattern). */\nexport const BEDROCK_CONVERSE_MODELS: ReadonlyArray<BedrockConverseModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { converse: true } }> => m.apis.converse,\n ).map((m) => m.id)\n\nexport const BEDROCK_CHAT_MODELS: ReadonlyArray<BedrockChatModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { chat: true } }> => m.apis.chat,\n ).map((m) => m.id)\n\nexport const BEDROCK_RESPONSES_MODELS: ReadonlyArray<BedrockResponsesModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { responses: true } }> => m.apis.responses,\n ).map((m) => m.id)\n\n/** Per-model input modalities (drives type-safe multimodal content). Covers ALL models. */\nexport type BedrockModelInputModalitiesByName = {\n [E in Entry as E['id']]: E['input']\n}\n\n/** Provider options per model. Same options for every model; keyed over the full catalog. */\nexport type BedrockChatModelProviderOptionsByName = {\n [E in Entry as E['id']]: BedrockTextProviderOptions\n}\n\n/** Converse provider options per model (narrower than the Chat Completions set). */\nexport type BedrockConverseModelProviderOptionsByName = {\n [E in Entry as E['id']]: BedrockConverseProviderOptions\n}\n\n/** No provider-specific tools — empty tuple makes cross-provider ProviderTool a compile error. */\nexport type BedrockChatModelToolCapabilitiesByName = {\n [E in Entry as E['id']]: readonly []\n}\n\nexport type ResolveProviderOptions<TModel extends string> =\n TModel extends keyof BedrockChatModelProviderOptionsByName\n ? BedrockChatModelProviderOptionsByName[TModel]\n : BedrockTextProviderOptions\n\nexport type ResolveConverseProviderOptions<TModel extends string> =\n TModel extends keyof BedrockConverseModelProviderOptionsByName\n ? BedrockConverseModelProviderOptionsByName[TModel]\n : BedrockConverseProviderOptions\n\nexport type ResolveInputModalities<TModel extends string> =\n TModel extends keyof BedrockModelInputModalitiesByName\n ? BedrockModelInputModalitiesByName[TModel]\n : readonly ['text']\n\n// ============================================================================\n// Embedding models\n// ============================================================================\n\n/**\n * Embedding models reachable through Bedrock's `InvokeModel` API. These are\n * not part of the generated Converse catalog (embedding models have no\n * conversational surface), so they're maintained by hand here.\n */\nexport const BEDROCK_EMBEDDING_MODELS = [\n 'amazon.titan-embed-text-v2:0',\n 'amazon.titan-embed-image-v1',\n 'cohere.embed-english-v3',\n 'cohere.embed-multilingual-v3',\n] as const\n\nexport type BedrockEmbeddingModel = (typeof BEDROCK_EMBEDDING_MODELS)[number]\n\n/**\n * Type-only map from embedding model name to its provider options type.\n * The Cohere models make `modelOptions` REQUIRED at the `embed()` call site\n * because `inputType` is a required field.\n */\nexport type BedrockEmbeddingModelProviderOptionsByName = {\n 'amazon.titan-embed-text-v2:0': BedrockTitanTextEmbeddingProviderOptions\n 'amazon.titan-embed-image-v1': BedrockTitanImageEmbeddingProviderOptions\n 'cohere.embed-english-v3': BedrockCohereEmbeddingProviderOptions\n 'cohere.embed-multilingual-v3': BedrockCohereEmbeddingProviderOptions\n}\n\n/**\n * Per-model input modalities for embedding models. Titan Multimodal accepts\n * text and/or images (including fused text+image items embedded into one\n * vector); the rest are text-only, so image inputs fail at compile time.\n */\nexport type BedrockEmbeddingModelInputModalitiesByName = {\n 'amazon.titan-embed-text-v2:0': readonly ['text']\n 'amazon.titan-embed-image-v1': readonly ['text', 'image']\n 'cohere.embed-english-v3': readonly ['text']\n 'cohere.embed-multilingual-v3': readonly ['text']\n}\n\nexport type ResolveEmbeddingProviderOptions<TModel extends string> =\n TModel extends keyof BedrockEmbeddingModelProviderOptionsByName\n ? BedrockEmbeddingModelProviderOptionsByName[TModel]\n : BedrockEmbeddingProviderOptions\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"model-meta.js","names":[],"sources":["../../src/model-meta.ts"],"sourcesContent":["import { GENERATED_BEDROCK_MODELS } from './model-catalog.generated.js'\nimport type { Modality, ModelReasoning } from '@tanstack/ai'\nimport type { ModelReasoningCapability } from '@tanstack/ai/adapter-internals'\nimport type { BedrockTextProviderOptions } from './text/text-provider-options'\nimport type { BedrockConverseProviderOptions } from './converse/provider-options'\nimport type {\n BedrockCohereEmbeddingProviderOptions,\n BedrockEmbeddingProviderOptions,\n BedrockTitanImageEmbeddingProviderOptions,\n BedrockTitanTextEmbeddingProviderOptions,\n} from './embedding/embedding-provider-options'\n\n// Reasoning shapes: the levels a model has for `chat({ reasoning })`, and\n// the provider value to send for each. Each model points at one of them.\nconst BUDGET_OFF_HIGH = {\n levels: { off: 'off', high: 'high' },\n budget: true,\n} as const\nconst EFFORT_LOW_TO_HIGH = {\n levels: { low: 'low', medium: 'medium', high: 'high' },\n budget: false,\n} as const\nconst EFFORT_NONE_HIGH = {\n levels: { off: 'none', high: 'high' },\n budget: false,\n} as const\n\ntype Entry = (typeof GENERATED_BEDROCK_MODELS)[number]\n\n/**\n * Type-level per-API filter over the generated catalog. Because the catalog is\n * `as const`, `Extract` preserves literal `id` unions (no widening to `string`).\n */\ntype IdsWhere<TApi extends 'converse' | 'chat' | 'responses'> = Extract<\n Entry,\n { apis: Record<TApi, true> }\n>['id']\n\nexport type BedrockConverseModels = IdsWhere<'converse'>\nexport type BedrockChatModels = IdsWhere<'chat'>\nexport type BedrockResponsesModels = IdsWhere<'responses'>\n\n/** Runtime catalogs. Cast-free narrowing via a type predicate (the ai-bedrock pattern). */\nexport const BEDROCK_CONVERSE_MODELS: ReadonlyArray<BedrockConverseModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { converse: true } }> => m.apis.converse,\n ).map((m) => m.id)\n\nexport const BEDROCK_CHAT_MODELS: ReadonlyArray<BedrockChatModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { chat: true } }> => m.apis.chat,\n ).map((m) => m.id)\n\nexport const BEDROCK_RESPONSES_MODELS: ReadonlyArray<BedrockResponsesModels> =\n GENERATED_BEDROCK_MODELS.filter(\n (m): m is Extract<Entry, { apis: { responses: true } }> => m.apis.responses,\n ).map((m) => m.id)\n\n/** Per-model input modalities (drives type-safe multimodal content). Covers ALL models. */\nexport type BedrockModelInputModalitiesByName = {\n [E in Entry as E['id']]: E['input']\n}\n\n/**\n * Runtime map from model id to its input modalities, for the text adapters'\n * `inputModalities`. An unknown id gives `undefined`.\n */\nexport const BEDROCK_MODEL_INPUT_MODALITIES: Readonly<\n Record<string, ReadonlyArray<Modality>>\n> = Object.fromEntries(GENERATED_BEDROCK_MODELS.map((m) => [m.id, m.input]))\n\n/** Provider options per model. Same options for every model; keyed over the full catalog. */\nexport type BedrockChatModelProviderOptionsByName = {\n [E in Entry as E['id']]: BedrockTextProviderOptions\n}\n\n/** Converse provider options per model (narrower than the Chat Completions set). */\nexport type BedrockConverseModelProviderOptionsByName = {\n [E in Entry as E['id']]: BedrockConverseProviderOptions\n}\n\n/** No provider-specific tools — empty tuple makes cross-provider ProviderTool a compile error. */\nexport type BedrockChatModelToolCapabilitiesByName = {\n [E in Entry as E['id']]: readonly []\n}\n\nexport type ResolveProviderOptions<TModel extends string> =\n TModel extends keyof BedrockChatModelProviderOptionsByName\n ? BedrockChatModelProviderOptionsByName[TModel]\n : BedrockTextProviderOptions\n\nexport type ResolveConverseProviderOptions<TModel extends string> =\n TModel extends keyof BedrockConverseModelProviderOptionsByName\n ? BedrockConverseModelProviderOptionsByName[TModel]\n : BedrockConverseProviderOptions\n\nexport type ResolveInputModalities<TModel extends string> =\n TModel extends keyof BedrockModelInputModalitiesByName\n ? BedrockModelInputModalitiesByName[TModel]\n : readonly ['text']\n\n// ============================================================================\n// Reasoning\n// ============================================================================\n//\n// Each model's data for `chat({ reasoning })`: the levels it has, with the\n// provider value for each, and whether it takes a thinking token budget. The\n// data comes from models.dev. The generator that writes it comes with the model\n// catalog later. Until then, edit these entries by hand.\n// scripts/fetch-bedrock-models.ts does not touch them.\n\nconst BEDROCK_REASONING = {\n 'openai.gpt-oss-120b-1:0': EFFORT_LOW_TO_HIGH,\n 'openai.gpt-oss-20b-1:0': EFFORT_LOW_TO_HIGH,\n 'us.anthropic.claude-sonnet-4-5-20250929-v1:0': BUDGET_OFF_HIGH,\n 'us.anthropic.claude-haiku-4-5-20251001-v1:0': BUDGET_OFF_HIGH,\n 'google.gemma-4-31b': EFFORT_NONE_HIGH,\n 'google.gemma-4-26b-a4b': EFFORT_NONE_HIGH,\n 'google.gemma-4-e2b': EFFORT_NONE_HIGH,\n} satisfies Partial<Record<Entry['id'], ModelReasoning>>\n\n/**\n * Each model's reasoning levels, and whether it takes a token budget, for\n * `chat({ reasoning })`. A model that is not here does not reason.\n */\nexport type BedrockModelReasoningByName = {\n [K in keyof typeof BEDROCK_REASONING]: ModelReasoningCapability<\n (typeof BEDROCK_REASONING)[K]\n >\n}\n\n/**\n * Runtime map from model id to its reasoning data, for the text adapters. An\n * unknown id gives `undefined`: the adapter sends no reasoning field.\n */\nexport const BEDROCK_MODEL_REASONING: Readonly<Record<string, ModelReasoning>> =\n BEDROCK_REASONING\n\n/**\n * The Claude families that reject a forced tool (`any` or a named `tool`) on\n * every request, with or without thinking. A Bedrock id has the family name\n * inside it, for example `us.anthropic.claude-opus-5-5-...`.\n * ponytail: names, not catalog entries, because the generated catalog has no\n * entry for these models yet. Move it to the catalog when it has them.\n */\nexport const BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES: ReadonlyArray<string> = [\n 'claude-fable-5-1',\n 'claude-mythos-5-1',\n 'claude-opus-5-5',\n 'claude-sonnet-5-5',\n]\n\n// ============================================================================\n// Embedding models\n// ============================================================================\n\n/**\n * Embedding models reachable through Bedrock's `InvokeModel` API. These are\n * not part of the generated Converse catalog (embedding models have no\n * conversational surface), so they're maintained by hand here.\n */\nexport const BEDROCK_EMBEDDING_MODELS = [\n 'amazon.titan-embed-text-v2:0',\n 'amazon.titan-embed-image-v1',\n 'cohere.embed-english-v3',\n 'cohere.embed-multilingual-v3',\n] as const\n\nexport type BedrockEmbeddingModel = (typeof BEDROCK_EMBEDDING_MODELS)[number]\n\n/**\n * Type-only map from embedding model name to its provider options type.\n * The Cohere models make `modelOptions` REQUIRED at the `embed()` call site\n * because `inputType` is a required field.\n */\nexport type BedrockEmbeddingModelProviderOptionsByName = {\n 'amazon.titan-embed-text-v2:0': BedrockTitanTextEmbeddingProviderOptions\n 'amazon.titan-embed-image-v1': BedrockTitanImageEmbeddingProviderOptions\n 'cohere.embed-english-v3': BedrockCohereEmbeddingProviderOptions\n 'cohere.embed-multilingual-v3': BedrockCohereEmbeddingProviderOptions\n}\n\n/**\n * Per-model input modalities for embedding models. Titan Multimodal accepts\n * text and/or images (including fused text+image items embedded into one\n * vector); the rest are text-only, so image inputs fail at compile time.\n */\nexport type BedrockEmbeddingModelInputModalitiesByName = {\n 'amazon.titan-embed-text-v2:0': readonly ['text']\n 'amazon.titan-embed-image-v1': readonly ['text', 'image']\n 'cohere.embed-english-v3': readonly ['text']\n 'cohere.embed-multilingual-v3': readonly ['text']\n}\n\nexport type ResolveEmbeddingProviderOptions<TModel extends string> =\n TModel extends keyof BedrockEmbeddingModelProviderOptionsByName\n ? BedrockEmbeddingModelProviderOptionsByName[TModel]\n : BedrockEmbeddingProviderOptions\n"],"mappings":";;AAcA,IAAM,kBAAkB;CACtB,QAAQ;EAAE,KAAK;EAAO,MAAM;CAAO;CACnC,QAAQ;AACV;AACA,IAAM,qBAAqB;CACzB,QAAQ;EAAE,KAAK;EAAO,QAAQ;EAAU,MAAM;CAAO;CACrD,QAAQ;AACV;AACA,IAAM,mBAAmB;CACvB,QAAQ;EAAE,KAAK;EAAQ,MAAM;CAAO;CACpC,QAAQ;AACV;;AAkBA,IAAa,0BACX,yBAAyB,QACtB,MAAyD,EAAE,KAAK,QACnE,CAAC,CAAC,KAAK,MAAM,EAAE,EAAE;AAEnB,IAAa,sBACX,yBAAyB,QACtB,MAAqD,EAAE,KAAK,IAC/D,CAAC,CAAC,KAAK,MAAM,EAAE,EAAE;AAEnB,IAAa,2BACX,yBAAyB,QACtB,MAA0D,EAAE,KAAK,SACpE,CAAC,CAAC,KAAK,MAAM,EAAE,EAAE;;;;;AAWnB,IAAa,iCAET,OAAO,YAAY,yBAAyB,KAAK,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;;;;;AAkE3E,IAAa,0BACX;CAxBA,2BAA2B;CAC3B,0BAA0B;CAC1B,gDAAgD;CAChD,+CAA+C;CAC/C,sBAAsB;CACtB,0BAA0B;CAC1B,sBAAsB;AAkBtB;;;;;;;;AASF,IAAa,yCAAgE;CAC3E;CACA;CACA;CACA;AACF;;;;;;AAWA,IAAa,2BAA2B;CACtC;CACA;CACA;CACA;AACF"}
|
|
@@ -20,10 +20,6 @@ export interface BedrockResponsesProviderOptions {
|
|
|
20
20
|
type: 'function';
|
|
21
21
|
name: string;
|
|
22
22
|
} | null;
|
|
23
|
-
/** Reasoning controls for reasoning-capable models. */
|
|
24
|
-
reasoning?: {
|
|
25
|
-
effort?: 'low' | 'medium' | 'high';
|
|
26
|
-
} | null;
|
|
27
23
|
user?: string | null;
|
|
28
24
|
}
|
|
29
25
|
export type ExternalResponsesProviderOptions = BedrockResponsesProviderOptions;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Bedrock Chat Completions provider options. Bedrock accepts the standard
|
|
3
|
-
* OpenAI Chat Completions request fields; we surface the commonly-used ones
|
|
4
|
-
*
|
|
3
|
+
* OpenAI Chat Completions request fields; we surface the commonly-used ones.
|
|
4
|
+
* Reasoning effort is set with `chat({ reasoning })`.
|
|
5
5
|
*
|
|
6
6
|
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-openai.html
|
|
7
7
|
*/
|
|
@@ -19,8 +19,6 @@ export interface BedrockTextProviderOptions {
|
|
|
19
19
|
} | null;
|
|
20
20
|
n?: number | null;
|
|
21
21
|
parallel_tool_calls?: boolean | null;
|
|
22
|
-
/** gpt-oss / reasoning models: 'low' | 'medium' (default) | 'high'. */
|
|
23
|
-
reasoning_effort?: 'low' | 'medium' | 'high' | null;
|
|
24
22
|
seed?: number | null;
|
|
25
23
|
stop?: string | Array<string> | null;
|
|
26
24
|
temperature?: number | null;
|
|
@@ -17,3 +17,13 @@ export interface BedrockClientConfig extends Omit<ClientOptions, 'apiKey' | 'bas
|
|
|
17
17
|
}
|
|
18
18
|
/** Builds OpenAI ClientOptions for the requested endpoint. `forced` pins the endpoint (responses → 'mantle'). */
|
|
19
19
|
export declare function withBedrockDefaults(config: BedrockClientConfig, forced?: BedrockEndpoint, model?: string): ClientOptions;
|
|
20
|
+
/**
|
|
21
|
+
* True when an error from Bedrock's OpenAI-compatible endpoints says the input
|
|
22
|
+
* does not fit in the model's context window. They have no error code for it.
|
|
23
|
+
* They answer `validation_error` for every bad request, so the message text
|
|
24
|
+
* is the only signal.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isBedrockOpenAIContextOverflow(error: {
|
|
27
|
+
message: string;
|
|
28
|
+
code: string | undefined;
|
|
29
|
+
}): boolean;
|
package/dist/esm/utils/client.js
CHANGED
|
@@ -32,7 +32,16 @@ function withBedrockDefaults(config, forced, model) {
|
|
|
32
32
|
fetch: fetch ?? createSigV4Fetch(resolved)
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* True when an error from Bedrock's OpenAI-compatible endpoints says the input
|
|
37
|
+
* does not fit in the model's context window. They have no error code for it.
|
|
38
|
+
* They answer `validation_error` for every bad request, so the message text
|
|
39
|
+
* is the only signal.
|
|
40
|
+
*/
|
|
41
|
+
function isBedrockOpenAIContextOverflow(error) {
|
|
42
|
+
return error.code === "validation_error" && error.message.includes("maximum context length");
|
|
43
|
+
}
|
|
35
44
|
//#endregion
|
|
36
|
-
export { resolveBedrockAuth, withBedrockDefaults };
|
|
45
|
+
export { isBedrockOpenAIContextOverflow, resolveBedrockAuth, withBedrockDefaults };
|
|
37
46
|
|
|
38
47
|
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["import { resolveBedrockAuth } from './auth'\nimport { createSigV4Fetch } from './openai-sigv4-fetch'\nimport { mantlePathForModel } from '../api-compatibility'\nimport type { ClientOptions } from 'openai'\nimport type { BedrockEndpoint } from './auth'\n\nexport type { BedrockEndpoint } from './auth'\nexport { resolveBedrockAuth } from './auth'\nexport type { ResolvedBedrockAuth } from './auth'\n\nexport interface BedrockClientConfig extends Omit<\n ClientOptions,\n 'apiKey' | 'baseURL'\n> {\n /** Bedrock API key (bearer). Optional — falls back to env, then SigV4. */\n apiKey?: string\n /** Full AWS region (e.g. 'us-east-1'). Default 'us-east-1'. */\n region?: string\n /** Chat adapter only; the responses adapter forces 'mantle'. Default 'runtime'. */\n endpoint?: BedrockEndpoint\n /** Auth strategy. Default 'auto' (apiKey → env → SigV4). */\n auth?: 'apikey' | 'sigv4' | 'auto'\n /** Explicit override; wins over the computed endpoint URL (used by E2E → aimock). */\n baseURL?: string\n}\n\nconst DEFAULT_REGION = 'us-east-1'\n/** OpenAI SDK requires a non-empty apiKey even when a signed fetch overrides Authorization. */\nconst SIGV4_PLACEHOLDER_KEY = 'bedrock-sigv4'\n\nfunction buildBaseURL(\n region: string,\n endpoint: BedrockEndpoint,\n model?: string,\n): string {\n return endpoint === 'mantle'\n ? `https://bedrock-mantle.${region}.api.aws${mantlePathForModel(model)}`\n : `https://bedrock-runtime.${region}.amazonaws.com/openai/v1`\n}\n\n/** Builds OpenAI ClientOptions for the requested endpoint. `forced` pins the endpoint (responses → 'mantle'). */\nexport function withBedrockDefaults(\n config: BedrockClientConfig,\n forced?: BedrockEndpoint,\n model?: string,\n): ClientOptions {\n const { region, endpoint, auth, apiKey, baseURL, fetch, ...rest } = config\n const resolvedRegion = region ?? DEFAULT_REGION\n const resolvedEndpoint = forced ?? endpoint ?? 'runtime'\n const resolved = resolveBedrockAuth(\n { apiKey, region: resolvedRegion, auth },\n resolvedEndpoint,\n )\n const resolvedBaseURL =\n baseURL ?? buildBaseURL(resolvedRegion, resolvedEndpoint, model)\n if (resolved.kind === 'bearer') {\n return {\n ...rest,\n baseURL: resolvedBaseURL,\n apiKey: resolved.token,\n ...(fetch ? { fetch } : {}),\n }\n }\n return {\n ...rest,\n baseURL: resolvedBaseURL,\n apiKey: SIGV4_PLACEHOLDER_KEY,\n fetch: fetch ?? createSigV4Fetch(resolved),\n }\n}\n"],"mappings":";;;;AA0BA,IAAM,iBAAiB;;AAEvB,IAAM,wBAAwB;AAE9B,SAAS,aACP,QACA,UACA,OACQ;CACR,OAAO,aAAa,WAChB,0BAA0B,OAAO,UAAU,mBAAmB,KAAK,MACnE,2BAA2B,OAAO;AACxC;;AAGA,SAAgB,oBACd,QACA,QACA,OACe;CACf,MAAM,EAAE,QAAQ,UAAU,MAAM,QAAQ,SAAS,OAAO,GAAG,SAAS;CACpE,MAAM,iBAAiB,UAAU;CACjC,MAAM,mBAAmB,UAAU,YAAY;CAC/C,MAAM,WAAW,mBACf;EAAE;EAAQ,QAAQ;EAAgB;CAAK,GACvC,gBACF;CACA,MAAM,kBACJ,WAAW,aAAa,gBAAgB,kBAAkB,KAAK;CACjE,IAAI,SAAS,SAAS,UACpB,OAAO;EACL,GAAG;EACH,SAAS;EACT,QAAQ,SAAS;EACjB,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;CAC3B;CAEF,OAAO;EACL,GAAG;EACH,SAAS;EACT,QAAQ;EACR,OAAO,SAAS,iBAAiB,QAAQ;CAC3C;AACF"}
|
|
1
|
+
{"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["import { resolveBedrockAuth } from './auth'\nimport { createSigV4Fetch } from './openai-sigv4-fetch'\nimport { mantlePathForModel } from '../api-compatibility'\nimport type { ClientOptions } from 'openai'\nimport type { BedrockEndpoint } from './auth'\n\nexport type { BedrockEndpoint } from './auth'\nexport { resolveBedrockAuth } from './auth'\nexport type { ResolvedBedrockAuth } from './auth'\n\nexport interface BedrockClientConfig extends Omit<\n ClientOptions,\n 'apiKey' | 'baseURL'\n> {\n /** Bedrock API key (bearer). Optional — falls back to env, then SigV4. */\n apiKey?: string\n /** Full AWS region (e.g. 'us-east-1'). Default 'us-east-1'. */\n region?: string\n /** Chat adapter only; the responses adapter forces 'mantle'. Default 'runtime'. */\n endpoint?: BedrockEndpoint\n /** Auth strategy. Default 'auto' (apiKey → env → SigV4). */\n auth?: 'apikey' | 'sigv4' | 'auto'\n /** Explicit override; wins over the computed endpoint URL (used by E2E → aimock). */\n baseURL?: string\n}\n\nconst DEFAULT_REGION = 'us-east-1'\n/** OpenAI SDK requires a non-empty apiKey even when a signed fetch overrides Authorization. */\nconst SIGV4_PLACEHOLDER_KEY = 'bedrock-sigv4'\n\nfunction buildBaseURL(\n region: string,\n endpoint: BedrockEndpoint,\n model?: string,\n): string {\n return endpoint === 'mantle'\n ? `https://bedrock-mantle.${region}.api.aws${mantlePathForModel(model)}`\n : `https://bedrock-runtime.${region}.amazonaws.com/openai/v1`\n}\n\n/** Builds OpenAI ClientOptions for the requested endpoint. `forced` pins the endpoint (responses → 'mantle'). */\nexport function withBedrockDefaults(\n config: BedrockClientConfig,\n forced?: BedrockEndpoint,\n model?: string,\n): ClientOptions {\n const { region, endpoint, auth, apiKey, baseURL, fetch, ...rest } = config\n const resolvedRegion = region ?? DEFAULT_REGION\n const resolvedEndpoint = forced ?? endpoint ?? 'runtime'\n const resolved = resolveBedrockAuth(\n { apiKey, region: resolvedRegion, auth },\n resolvedEndpoint,\n )\n const resolvedBaseURL =\n baseURL ?? buildBaseURL(resolvedRegion, resolvedEndpoint, model)\n if (resolved.kind === 'bearer') {\n return {\n ...rest,\n baseURL: resolvedBaseURL,\n apiKey: resolved.token,\n ...(fetch ? { fetch } : {}),\n }\n }\n return {\n ...rest,\n baseURL: resolvedBaseURL,\n apiKey: SIGV4_PLACEHOLDER_KEY,\n fetch: fetch ?? createSigV4Fetch(resolved),\n }\n}\n\n/**\n * True when an error from Bedrock's OpenAI-compatible endpoints says the input\n * does not fit in the model's context window. They have no error code for it.\n * They answer `validation_error` for every bad request, so the message text\n * is the only signal.\n */\nexport function isBedrockOpenAIContextOverflow(error: {\n message: string\n code: string | undefined\n}): boolean {\n return (\n error.code === 'validation_error' &&\n error.message.includes('maximum context length')\n )\n}\n"],"mappings":";;;;AA0BA,IAAM,iBAAiB;;AAEvB,IAAM,wBAAwB;AAE9B,SAAS,aACP,QACA,UACA,OACQ;CACR,OAAO,aAAa,WAChB,0BAA0B,OAAO,UAAU,mBAAmB,KAAK,MACnE,2BAA2B,OAAO;AACxC;;AAGA,SAAgB,oBACd,QACA,QACA,OACe;CACf,MAAM,EAAE,QAAQ,UAAU,MAAM,QAAQ,SAAS,OAAO,GAAG,SAAS;CACpE,MAAM,iBAAiB,UAAU;CACjC,MAAM,mBAAmB,UAAU,YAAY;CAC/C,MAAM,WAAW,mBACf;EAAE;EAAQ,QAAQ;EAAgB;CAAK,GACvC,gBACF;CACA,MAAM,kBACJ,WAAW,aAAa,gBAAgB,kBAAkB,KAAK;CACjE,IAAI,SAAS,SAAS,UACpB,OAAO;EACL,GAAG;EACH,SAAS;EACT,QAAQ,SAAS;EACjB,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;CAC3B;CAEF,OAAO;EACL,GAAG;EACH,SAAS;EACT,QAAQ;EACR,OAAO,SAAS,iBAAiB,QAAQ;CAC3C;AACF;;;;;;;AAQA,SAAgB,+BAA+B,OAGnC;CACV,OACE,MAAM,SAAS,sBACf,MAAM,QAAQ,SAAS,wBAAwB;AAEnD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-bedrock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Amazon Bedrock adapter for TanStack AI — OpenAI-compatible chat, responses, tools, and reasoning.",
|
|
6
6
|
"author": "",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"vite": "^8.2.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@tanstack/ai": "^0.
|
|
46
|
+
"@tanstack/ai": "^0.69.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@aws-crypto/sha256-js": "^5.2.0",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@smithy/signature-v4": "^5.4.5",
|
|
53
53
|
"@smithy/types": "^4.14.2",
|
|
54
54
|
"openai": "^7.30.0",
|
|
55
|
-
"@tanstack/openai-base": "^0.
|
|
55
|
+
"@tanstack/openai-base": "^0.14.0"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "vite build",
|