@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
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hashToolCallId,
|
|
3
|
+
orderedAssistantBlocks,
|
|
4
|
+
transformMessagesForReplay,
|
|
5
|
+
} from '@tanstack/ai/adapter-internals'
|
|
1
6
|
import {
|
|
2
7
|
isFileSource,
|
|
3
8
|
normalizeSystemPrompts,
|
|
@@ -32,6 +37,14 @@ function base64ToBytes(b64: string): Uint8Array {
|
|
|
32
37
|
return new Uint8Array(Buffer.from(b64, 'base64'))
|
|
33
38
|
}
|
|
34
39
|
|
|
40
|
+
// AWS ToolResultContentBlock.image: Nova and Claude 3/4 ids only.
|
|
41
|
+
// ponytail: id substring, not the image-input catalog (Llama accepts user images).
|
|
42
|
+
function supportsToolResultImage(model: string | undefined): boolean {
|
|
43
|
+
return /amazon\.nova|anthropic\.claude-(?:3|(?:opus|sonnet|haiku)-4)/i.test(
|
|
44
|
+
model ?? '',
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
35
48
|
function imageFormat(mime: string): 'png' | 'jpeg' | 'gif' | 'webp' {
|
|
36
49
|
switch (mime) {
|
|
37
50
|
case 'image/png':
|
|
@@ -153,9 +166,15 @@ function contentPartToBlock(part: ContentPart, docIndex: number): ContentBlock {
|
|
|
153
166
|
)
|
|
154
167
|
}
|
|
155
168
|
|
|
169
|
+
interface ConverseReplayContext {
|
|
170
|
+
model: string
|
|
171
|
+
provider?: string
|
|
172
|
+
}
|
|
173
|
+
|
|
156
174
|
function messageToBlocks(
|
|
157
175
|
msg: ModelMessage,
|
|
158
176
|
docCounter: { value: number },
|
|
177
|
+
context?: ConverseReplayContext,
|
|
159
178
|
): Array<ContentBlock> {
|
|
160
179
|
const blocks: Array<ContentBlock> = []
|
|
161
180
|
|
|
@@ -166,17 +185,80 @@ function messageToBlocks(
|
|
|
166
185
|
)
|
|
167
186
|
}
|
|
168
187
|
const textContent = stringContent(msg.content)
|
|
169
|
-
const
|
|
188
|
+
const content: Array<ToolResultContentBlock> = textContent
|
|
189
|
+
? [{ text: textContent }]
|
|
190
|
+
: []
|
|
191
|
+
for (const part of Array.isArray(msg.content) ? msg.content : []) {
|
|
192
|
+
// Inline bytes on Nova and Claude 3/4 only. Skip the rest, as before.
|
|
193
|
+
if (
|
|
194
|
+
!supportsToolResultImage(context?.model) ||
|
|
195
|
+
!isImagePart(part) ||
|
|
196
|
+
!isDataSource(part.source)
|
|
197
|
+
)
|
|
198
|
+
continue
|
|
199
|
+
let image: ContentBlock['image']
|
|
200
|
+
try {
|
|
201
|
+
image = contentPartToBlock(part, 0).image
|
|
202
|
+
} catch {
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
if (image) content.push({ image })
|
|
206
|
+
}
|
|
207
|
+
if (content.length === 0) content.push({ text: textContent })
|
|
170
208
|
blocks.push({
|
|
171
209
|
toolResult: {
|
|
172
210
|
toolUseId: msg.toolCallId,
|
|
173
|
-
content
|
|
211
|
+
content,
|
|
174
212
|
status: msg.error !== undefined ? 'error' : 'success',
|
|
175
213
|
},
|
|
176
214
|
})
|
|
177
215
|
return blocks
|
|
178
216
|
}
|
|
179
217
|
|
|
218
|
+
// Thinking goes back as reasoning blocks. Claude needs its signature, so
|
|
219
|
+
// unsigned Claude thinking goes back as text.
|
|
220
|
+
function appendThinking(
|
|
221
|
+
thinking: NonNullable<ModelMessage['thinking']>[number],
|
|
222
|
+
): void {
|
|
223
|
+
if (thinking.redacted) {
|
|
224
|
+
if (thinking.signature)
|
|
225
|
+
blocks.push({
|
|
226
|
+
reasoningContent: {
|
|
227
|
+
redactedContent: base64ToBytes(thinking.signature),
|
|
228
|
+
},
|
|
229
|
+
})
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
const text = thinking.content
|
|
233
|
+
if (!text) return
|
|
234
|
+
const claude = /anthropic[./]claude/i.test(context?.model ?? '')
|
|
235
|
+
if (claude && !thinking.signature) blocks.push({ text })
|
|
236
|
+
else
|
|
237
|
+
blocks.push({
|
|
238
|
+
reasoningContent: {
|
|
239
|
+
reasoningText: {
|
|
240
|
+
text,
|
|
241
|
+
...(claude && thinking.signature
|
|
242
|
+
? { signature: thinking.signature }
|
|
243
|
+
: {}),
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
const ordered =
|
|
249
|
+
msg.role === 'assistant' ? orderedAssistantBlocks(msg) : undefined
|
|
250
|
+
if (ordered) {
|
|
251
|
+
for (const block of ordered) {
|
|
252
|
+
if (block.type === 'thinking') appendThinking(block.thinking)
|
|
253
|
+
else if (block.type === 'tool-call')
|
|
254
|
+
blocks.push(toolUseBlock(block.toolCall))
|
|
255
|
+
else blocks.push({ text: block.text })
|
|
256
|
+
}
|
|
257
|
+
return blocks
|
|
258
|
+
}
|
|
259
|
+
if (msg.role === 'assistant')
|
|
260
|
+
for (const thinking of msg.thinking ?? []) appendThinking(thinking)
|
|
261
|
+
|
|
180
262
|
// Map content field to text/image/document blocks
|
|
181
263
|
if (typeof msg.content === 'string') {
|
|
182
264
|
if (msg.content !== '') {
|
|
@@ -200,38 +282,38 @@ function messageToBlocks(
|
|
|
200
282
|
// `{}` and forwarding a corrupted tool call (the adapter's catch surfaces it
|
|
201
283
|
// as a RUN_ERROR).
|
|
202
284
|
if (msg.role === 'assistant' && msg.toolCalls) {
|
|
203
|
-
for (const call of msg.toolCalls)
|
|
204
|
-
const rawArguments = call.function.arguments || '{}'
|
|
205
|
-
let parsed: unknown
|
|
206
|
-
try {
|
|
207
|
-
parsed = JSON.parse(rawArguments)
|
|
208
|
-
} catch (error: unknown) {
|
|
209
|
-
throw new Error(
|
|
210
|
-
`Bedrock Converse: tool call "${call.function.name}" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,
|
|
211
|
-
)
|
|
212
|
-
}
|
|
213
|
-
if (
|
|
214
|
-
parsed === null ||
|
|
215
|
-
typeof parsed !== 'object' ||
|
|
216
|
-
Array.isArray(parsed)
|
|
217
|
-
) {
|
|
218
|
-
throw new Error(
|
|
219
|
-
`Bedrock Converse: tool call "${call.function.name}" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,
|
|
220
|
-
)
|
|
221
|
-
}
|
|
222
|
-
blocks.push({
|
|
223
|
-
toolUse: {
|
|
224
|
-
toolUseId: call.id,
|
|
225
|
-
name: call.function.name,
|
|
226
|
-
input: parsed as DocumentType,
|
|
227
|
-
},
|
|
228
|
-
})
|
|
229
|
-
}
|
|
285
|
+
for (const call of msg.toolCalls) blocks.push(toolUseBlock(call))
|
|
230
286
|
}
|
|
231
287
|
|
|
232
288
|
return blocks
|
|
233
289
|
}
|
|
234
290
|
|
|
291
|
+
function toolUseBlock(
|
|
292
|
+
call: NonNullable<ModelMessage['toolCalls']>[number],
|
|
293
|
+
): ContentBlock {
|
|
294
|
+
const rawArguments = call.function.arguments || '{}'
|
|
295
|
+
let parsed: unknown
|
|
296
|
+
try {
|
|
297
|
+
parsed = JSON.parse(rawArguments)
|
|
298
|
+
} catch (error: unknown) {
|
|
299
|
+
throw new Error(
|
|
300
|
+
`Bedrock Converse: tool call "${call.function.name}" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`Bedrock Converse: tool call "${call.function.name}" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
toolUse: {
|
|
310
|
+
toolUseId: call.id,
|
|
311
|
+
name: call.function.name,
|
|
312
|
+
input: parsed as DocumentType,
|
|
313
|
+
},
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
235
317
|
// ---------------------------------------------------------------------------
|
|
236
318
|
// Public API
|
|
237
319
|
// ---------------------------------------------------------------------------
|
|
@@ -248,6 +330,7 @@ function messageToBlocks(
|
|
|
248
330
|
export function toConverseMessages(
|
|
249
331
|
messages: Array<ModelMessage>,
|
|
250
332
|
systemPrompts?: Array<SystemPrompt>,
|
|
333
|
+
context?: ConverseReplayContext,
|
|
251
334
|
): { system: Array<SystemContentBlock>; messages: Array<Message> } {
|
|
252
335
|
// Build system blocks (uses normalizeSystemPrompts for runtime validation)
|
|
253
336
|
const system: Array<SystemContentBlock> =
|
|
@@ -264,12 +347,33 @@ export function toConverseMessages(
|
|
|
264
347
|
// gets a unique name, preventing Bedrock ValidationException for duplicate names.
|
|
265
348
|
const docCounter = { value: 0 }
|
|
266
349
|
|
|
267
|
-
|
|
350
|
+
// History from another model loses its signatures, and its tool IDs are
|
|
351
|
+
// rewritten to the shape Converse accepts. Each call stays paired.
|
|
352
|
+
const replay = transformMessagesForReplay(
|
|
353
|
+
messages,
|
|
354
|
+
context
|
|
355
|
+
? {
|
|
356
|
+
provider: context.provider ?? 'amazon-bedrock',
|
|
357
|
+
api: 'bedrock-converse-stream',
|
|
358
|
+
model: context.model,
|
|
359
|
+
}
|
|
360
|
+
: undefined,
|
|
361
|
+
(id, { foreign, attempt }) => {
|
|
362
|
+
if (!foreign && attempt === 0) return id
|
|
363
|
+
const clean = id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64)
|
|
364
|
+
return attempt === 0
|
|
365
|
+
? clean
|
|
366
|
+
: clean.slice(0, 47) +
|
|
367
|
+
'_' +
|
|
368
|
+
hashToolCallId(id + ':' + attempt).slice(0, 16)
|
|
369
|
+
},
|
|
370
|
+
)
|
|
371
|
+
for (const msg of replay.messages) {
|
|
268
372
|
// Map TanStack roles to Converse roles
|
|
269
373
|
const converseRole: 'user' | 'assistant' =
|
|
270
374
|
msg.role === 'assistant' ? 'assistant' : 'user'
|
|
271
375
|
|
|
272
|
-
const blocks = messageToBlocks(msg, docCounter)
|
|
376
|
+
const blocks = messageToBlocks(msg, docCounter, context)
|
|
273
377
|
|
|
274
378
|
// Skip messages that produce no content blocks (e.g. assistant with
|
|
275
379
|
// null content and no toolCalls). Pushing an empty-content message to
|
|
@@ -287,3 +391,34 @@ export function toConverseMessages(
|
|
|
287
391
|
|
|
288
392
|
return { system, messages: converseMessages }
|
|
289
393
|
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Write the `toolUse` and `toolResult` blocks as text blocks. Bedrock rejects
|
|
397
|
+
* these blocks in a request with no `toolConfig`, so a request with no tools
|
|
398
|
+
* sends its tool history this way.
|
|
399
|
+
*/
|
|
400
|
+
export function toolBlocksToText(messages: Array<Message>): Array<Message> {
|
|
401
|
+
return messages.map((message) => ({
|
|
402
|
+
...message,
|
|
403
|
+
content: message.content?.flatMap((block): Array<ContentBlock> => {
|
|
404
|
+
if (block.toolUse) {
|
|
405
|
+
const { toolUseId, name, input } = block.toolUse
|
|
406
|
+
return [
|
|
407
|
+
{
|
|
408
|
+
text: `[Tool call ${toolUseId} ${name}(${JSON.stringify(input)})]`,
|
|
409
|
+
},
|
|
410
|
+
]
|
|
411
|
+
}
|
|
412
|
+
if (block.toolResult) {
|
|
413
|
+
const { toolUseId, content = [] } = block.toolResult
|
|
414
|
+
const text = content.map((part) => part.text ?? '').join('')
|
|
415
|
+
// Keep the images of the result as image blocks after the text.
|
|
416
|
+
const images = content.flatMap((part) =>
|
|
417
|
+
part.image ? [{ image: part.image }] : [],
|
|
418
|
+
)
|
|
419
|
+
return [{ text: `[Tool result ${toolUseId}: ${text}]` }, ...images]
|
|
420
|
+
}
|
|
421
|
+
return [block]
|
|
422
|
+
}),
|
|
423
|
+
}))
|
|
424
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ConverseCommandInput } from '@aws-sdk/client-bedrock-runtime'
|
|
2
|
+
import type { ResolvedPromptCache } from '@tanstack/ai'
|
|
3
|
+
import type { BedrockCachePoint } from '../message-types'
|
|
4
|
+
|
|
5
|
+
/** True when one of the blocks is a cache point. */
|
|
6
|
+
function hasCachePoint(
|
|
7
|
+
blocks: ReadonlyArray<{ cachePoint?: unknown }> | undefined,
|
|
8
|
+
) {
|
|
9
|
+
return blocks?.some((block) => block.cachePoint !== undefined) ?? false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Add the automatic Bedrock cache points for `chat({ promptCache })` (pi's
|
|
14
|
+
* rule). Only Claude models get them. When the request already has a manual
|
|
15
|
+
* cache point (system, message, or tool), the caller owns caching and the
|
|
16
|
+
* request does not change. Otherwise:
|
|
17
|
+
* - one cache point goes at the end of `system`, when there is a system prompt.
|
|
18
|
+
* - one cache point goes at the end of the last message, when its role is
|
|
19
|
+
* `user`.
|
|
20
|
+
*
|
|
21
|
+
* `'long'` asks for the 1-hour TTL. Bedrock has no cache key, so `key` is not
|
|
22
|
+
* used. The function returns a new input and does not change the arrays of
|
|
23
|
+
* `input`.
|
|
24
|
+
*/
|
|
25
|
+
export function addPromptCachePoints(
|
|
26
|
+
model: string,
|
|
27
|
+
input: ConverseCommandInput,
|
|
28
|
+
promptCache: ResolvedPromptCache | undefined,
|
|
29
|
+
) {
|
|
30
|
+
const retention = promptCache?.retention ?? 'none'
|
|
31
|
+
const system = input.system ?? []
|
|
32
|
+
const messages = input.messages ?? []
|
|
33
|
+
const hasManualCachePoint =
|
|
34
|
+
hasCachePoint(system) ||
|
|
35
|
+
hasCachePoint(input.toolConfig?.tools) ||
|
|
36
|
+
messages.some((message) => hasCachePoint(message.content))
|
|
37
|
+
const isClaude = /claude/i.test(model)
|
|
38
|
+
if (retention === 'none' || !isClaude || hasManualCachePoint) return input
|
|
39
|
+
|
|
40
|
+
const cachePoint: BedrockCachePoint =
|
|
41
|
+
retention === 'long' ? { type: 'default', ttl: '1h' } : { type: 'default' }
|
|
42
|
+
const last = messages.at(-1)
|
|
43
|
+
return {
|
|
44
|
+
...input,
|
|
45
|
+
...(system.length > 0 && { system: [...system, { cachePoint }] }),
|
|
46
|
+
...(last?.role === 'user' && {
|
|
47
|
+
messages: [
|
|
48
|
+
...messages.slice(0, -1),
|
|
49
|
+
{ ...last, content: [...(last.content ?? []), { cachePoint }] },
|
|
50
|
+
],
|
|
51
|
+
}),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
reasoningBudget,
|
|
3
|
+
resolveReasoning,
|
|
4
|
+
} from '@tanstack/ai/adapter-internals'
|
|
5
|
+
import type { ModelReasoning, ReasoningRequest } from '@tanstack/ai'
|
|
6
|
+
|
|
7
|
+
/** The answer room pi keeps next to a thinking budget. */
|
|
8
|
+
const MIN_ANSWER_TOKENS = 1024
|
|
9
|
+
|
|
10
|
+
export interface ConverseThinking {
|
|
11
|
+
additionalModelRequestFields?:
|
|
12
|
+
| {
|
|
13
|
+
thinking: { type: 'enabled'; budget_tokens: number }
|
|
14
|
+
anthropic_beta: Array<string>
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
thinking: { type: 'adaptive' }
|
|
18
|
+
output_config: { effort: string }
|
|
19
|
+
}
|
|
20
|
+
/** The smallest `maxTokens` that leaves room for the thinking budget. */
|
|
21
|
+
minMaxTokens?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The Converse thinking fields for `chat({ reasoning })`. Only Claude takes
|
|
26
|
+
* them on Converse (pi's rule):
|
|
27
|
+
* - a budget model: enabled thinking with the budget (pi's table when the
|
|
28
|
+
* request sets none), and the interleaved-thinking beta.
|
|
29
|
+
* - a model with effort levels: adaptive thinking with the effort.
|
|
30
|
+
* `off` sends nothing, because Claude does not think unless asked.
|
|
31
|
+
*/
|
|
32
|
+
export function converseThinking(
|
|
33
|
+
model: string,
|
|
34
|
+
request: ReasoningRequest | undefined,
|
|
35
|
+
reasoning: ModelReasoning | undefined,
|
|
36
|
+
): ConverseThinking {
|
|
37
|
+
if (!model.includes('anthropic.claude')) return {}
|
|
38
|
+
const resolved = resolveReasoning(request, reasoning)
|
|
39
|
+
if (!resolved || !reasoning || resolved.level === 'off') return {}
|
|
40
|
+
if (reasoning.budget) {
|
|
41
|
+
const budget = reasoningBudget({
|
|
42
|
+
level: resolved.level,
|
|
43
|
+
summary: resolved.summary,
|
|
44
|
+
...(resolved.budgetTokens !== undefined
|
|
45
|
+
? { budgetTokens: resolved.budgetTokens }
|
|
46
|
+
: {}),
|
|
47
|
+
})
|
|
48
|
+
return {
|
|
49
|
+
additionalModelRequestFields: {
|
|
50
|
+
thinking: { type: 'enabled', budget_tokens: budget },
|
|
51
|
+
anthropic_beta: ['interleaved-thinking-2025-05-14'],
|
|
52
|
+
},
|
|
53
|
+
minMaxTokens: budget + MIN_ANSWER_TOKENS,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
additionalModelRequestFields: {
|
|
58
|
+
thinking: { type: 'adaptive' },
|
|
59
|
+
output_config: { effort: resolved.value },
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -29,35 +29,9 @@ export function throwIfConverseStreamError(ev: ConverseStreamOutput): void {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* lifecycle shape so the activity layer / agent loop behave identically across
|
|
36
|
-
* providers. (Reasoning surfaces only the message-level events —
|
|
37
|
-
* `REASONING_MESSAGE_START/CONTENT/END` — which is all the core accumulator
|
|
38
|
-
* consumes; the `REASONING_START`/`REASONING_END`/`STEP_*` boundary events
|
|
39
|
-
* openai-base also emits are no-ops in the engine and are not reproduced here.)
|
|
40
|
-
*
|
|
41
|
-
* Lifecycle ownership matches openai-base: this processor emits the full
|
|
42
|
-
* success-path lifecycle itself — `RUN_STARTED` lazily before the first event,
|
|
43
|
-
* `TEXT_MESSAGE_*` / `TOOL_CALL_*` / `REASONING_MESSAGE_*` for content, and a
|
|
44
|
-
* single terminal `RUN_FINISHED` once the iterator is exhausted (so the trailing
|
|
45
|
-
* `metadata` usage event is folded into the finish event regardless of arrival
|
|
46
|
-
* order). The calling adapter only owns the catch/`RUN_ERROR` path — so any
|
|
47
|
-
* in-band Converse error event (see `throwIfConverseStreamError`) is thrown to
|
|
48
|
-
* surface there rather than ending the stream as a clean truncated success.
|
|
49
|
-
*
|
|
50
|
-
* Converse streams tool-call arguments as partial-JSON string fragments inside
|
|
51
|
-
* `contentBlockDelta.delta.toolUse.input`; each fragment is emitted as a
|
|
52
|
-
* `TOOL_CALL_ARGS` `delta`, mirroring OpenAI's `function.arguments` deltas.
|
|
53
|
-
*
|
|
54
|
-
* @param stream - The Converse event stream from `ConverseStreamCommand`.
|
|
55
|
-
* @param newMessageId - Factory for fresh ids — run, thread, message, and
|
|
56
|
-
* tool-call ids (the adapter passes `() => this.generateId()`).
|
|
57
|
-
* @param lifecycle - Incoming run lifecycle ids, threaded onto the emitted
|
|
58
|
-
* `RUN_STARTED`/`RUN_FINISHED` so the chat path matches every sibling adapter
|
|
59
|
-
* (openai-base reuses `options.threadId`/`parentRunId`). Defaults preserve the
|
|
60
|
-
* previous behaviour (fresh `threadId`, no `parentRunId`).
|
|
32
|
+
/** Map Converse blocks to run, text, reasoning, and tool events.
|
|
33
|
+
* Keep signatures and encrypted bytes separate for each contentBlockIndex.
|
|
34
|
+
* Finish after trailing usage events. The adapter handles thrown errors.
|
|
61
35
|
*/
|
|
62
36
|
export async function* processConverseStream(
|
|
63
37
|
stream: AsyncIterable<ConverseStreamOutput>,
|
|
@@ -76,15 +50,29 @@ export async function* processConverseStream(
|
|
|
76
50
|
let hasEmittedTextMessageStart = false
|
|
77
51
|
|
|
78
52
|
// Reasoning lifecycle
|
|
79
|
-
|
|
80
|
-
|
|
53
|
+
const reasoningByIndex = new Map<
|
|
54
|
+
number,
|
|
55
|
+
{
|
|
56
|
+
id: string
|
|
57
|
+
text: string
|
|
58
|
+
signature: string
|
|
59
|
+
bytes: Array<Uint8Array>
|
|
60
|
+
redacted: boolean
|
|
61
|
+
closed: boolean
|
|
62
|
+
encrypted?: string
|
|
63
|
+
}
|
|
64
|
+
>()
|
|
81
65
|
|
|
82
66
|
// Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a
|
|
83
67
|
// tool-use block with `contentBlockStart`, streams arg fragments via
|
|
84
68
|
// `contentBlockDelta`, and closes it with `contentBlockStop`.
|
|
85
69
|
const toolCallsByIndex = new Map<
|
|
86
70
|
number,
|
|
87
|
-
{
|
|
71
|
+
{
|
|
72
|
+
id: string
|
|
73
|
+
name: string
|
|
74
|
+
started: boolean
|
|
75
|
+
}
|
|
88
76
|
>()
|
|
89
77
|
|
|
90
78
|
// Usage + finish-reason are captured during iteration and folded into the
|
|
@@ -109,12 +97,50 @@ export async function* processConverseStream(
|
|
|
109
97
|
|
|
110
98
|
// Close an open reasoning message before text/tool content begins, mirroring
|
|
111
99
|
// openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.
|
|
112
|
-
function*
|
|
113
|
-
|
|
114
|
-
|
|
100
|
+
function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {
|
|
101
|
+
const reasoning = reasoningByIndex.get(index)
|
|
102
|
+
if (!reasoning) return
|
|
103
|
+
const encrypted = reasoning.redacted
|
|
104
|
+
? Buffer.concat(reasoning.bytes).toString('base64')
|
|
105
|
+
: reasoning.signature
|
|
106
|
+
if (!encrypted || encrypted === reasoning.encrypted) return
|
|
107
|
+
reasoning.encrypted = encrypted
|
|
108
|
+
yield {
|
|
109
|
+
type: EventType.REASONING_ENCRYPTED_VALUE,
|
|
110
|
+
entityId: reasoning.id,
|
|
111
|
+
subtype: 'message',
|
|
112
|
+
encryptedValue: encrypted,
|
|
113
|
+
...(reasoning.redacted && {
|
|
114
|
+
stepId: reasoning.id.startsWith('redacted_thinking-')
|
|
115
|
+
? reasoning.id
|
|
116
|
+
: 'redacted_thinking-' + reasoning.id,
|
|
117
|
+
}),
|
|
118
|
+
timestamp: Date.now(),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {
|
|
123
|
+
for (const [blockIndex, reasoning] of reasoningByIndex) {
|
|
124
|
+
if (index !== undefined && index !== blockIndex) continue
|
|
125
|
+
yield* encryptedReasoning(blockIndex)
|
|
126
|
+
if (reasoning.closed) continue
|
|
127
|
+
reasoning.closed = true
|
|
128
|
+
yield {
|
|
129
|
+
type: EventType.STEP_FINISHED,
|
|
130
|
+
stepId: reasoning.id,
|
|
131
|
+
stepName: reasoning.id,
|
|
132
|
+
delta: '',
|
|
133
|
+
content: reasoning.text,
|
|
134
|
+
timestamp: Date.now(),
|
|
135
|
+
}
|
|
115
136
|
yield {
|
|
116
137
|
type: EventType.REASONING_MESSAGE_END,
|
|
117
|
-
messageId:
|
|
138
|
+
messageId: reasoning.id,
|
|
139
|
+
timestamp: Date.now(),
|
|
140
|
+
}
|
|
141
|
+
yield {
|
|
142
|
+
type: EventType.REASONING_END,
|
|
143
|
+
messageId: reasoning.id,
|
|
118
144
|
timestamp: Date.now(),
|
|
119
145
|
}
|
|
120
146
|
}
|
|
@@ -173,28 +199,66 @@ export async function* processConverseStream(
|
|
|
173
199
|
continue
|
|
174
200
|
}
|
|
175
201
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
202
|
+
if (delta && 'reasoningContent' in delta && delta.reasoningContent) {
|
|
203
|
+
const fragment = delta.reasoningContent
|
|
204
|
+
let reasoning = reasoningByIndex.get(index)
|
|
205
|
+
if (!reasoning) {
|
|
206
|
+
const redacted =
|
|
207
|
+
'redactedContent' in fragment &&
|
|
208
|
+
(fragment.redactedContent?.length ?? 0) > 0
|
|
209
|
+
reasoning = {
|
|
210
|
+
id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),
|
|
211
|
+
text: '',
|
|
212
|
+
signature: '',
|
|
213
|
+
bytes: [],
|
|
214
|
+
redacted,
|
|
215
|
+
closed: false,
|
|
216
|
+
}
|
|
217
|
+
reasoningByIndex.set(index, reasoning)
|
|
218
|
+
yield {
|
|
219
|
+
type: EventType.STEP_STARTED,
|
|
220
|
+
stepId: reasoning.id,
|
|
221
|
+
stepName: reasoning.id,
|
|
222
|
+
stepType: 'thinking',
|
|
223
|
+
timestamp: Date.now(),
|
|
224
|
+
}
|
|
186
225
|
yield {
|
|
187
226
|
type: EventType.REASONING_MESSAGE_START,
|
|
188
|
-
messageId:
|
|
227
|
+
messageId: reasoning.id,
|
|
189
228
|
role: 'reasoning',
|
|
190
229
|
timestamp: Date.now(),
|
|
191
230
|
}
|
|
192
231
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
232
|
+
if (
|
|
233
|
+
!reasoning.redacted &&
|
|
234
|
+
'text' in fragment &&
|
|
235
|
+
fragment.text !== undefined
|
|
236
|
+
) {
|
|
237
|
+
reasoning.text += fragment.text
|
|
238
|
+
yield {
|
|
239
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
240
|
+
messageId: reasoning.id,
|
|
241
|
+
delta: fragment.text,
|
|
242
|
+
timestamp: Date.now(),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (
|
|
246
|
+
!reasoning.redacted &&
|
|
247
|
+
'signature' in fragment &&
|
|
248
|
+
fragment.signature !== undefined
|
|
249
|
+
)
|
|
250
|
+
reasoning.signature += fragment.signature
|
|
251
|
+
if (
|
|
252
|
+
'redactedContent' in fragment &&
|
|
253
|
+
fragment.redactedContent &&
|
|
254
|
+
fragment.redactedContent.length > 0
|
|
255
|
+
) {
|
|
256
|
+
if (!reasoning.redacted) reasoning.encrypted = undefined
|
|
257
|
+
reasoning.redacted = true
|
|
258
|
+
reasoning.text = ''
|
|
259
|
+
reasoning.signature = ''
|
|
260
|
+
reasoning.bytes.push(fragment.redactedContent)
|
|
261
|
+
yield* encryptedReasoning(index)
|
|
198
262
|
}
|
|
199
263
|
continue
|
|
200
264
|
}
|
|
@@ -225,6 +289,7 @@ export async function* processConverseStream(
|
|
|
225
289
|
|
|
226
290
|
if ('contentBlockStop' in ev) {
|
|
227
291
|
const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0
|
|
292
|
+
yield* closeReasoning(stopIndex)
|
|
228
293
|
const toolCall = toolCallsByIndex.get(stopIndex)
|
|
229
294
|
if (toolCall?.started) {
|
|
230
295
|
yield {
|