@tanstack/ai-bedrock 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +91 -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 +93 -52
- 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 +168 -36
- package/src/converse/prompt-cache.ts +53 -0
- package/src/converse/reasoning.ts +62 -0
- package/src/converse/stream-processor.ts +122 -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 !== '') {
|
|
@@ -194,44 +276,41 @@ function messageToBlocks(
|
|
|
194
276
|
}
|
|
195
277
|
// null → no text blocks
|
|
196
278
|
|
|
197
|
-
// Append toolUse blocks for assistant tool calls. Malformed
|
|
198
|
-
//
|
|
199
|
-
//
|
|
200
|
-
//
|
|
201
|
-
// as a RUN_ERROR).
|
|
279
|
+
// Append toolUse blocks for assistant tool calls. Malformed JSON stays in
|
|
280
|
+
// the history next to its tool error, so it goes out as `{}`. Non-object
|
|
281
|
+
// arguments signal a real upstream problem and throw (the adapter's catch
|
|
282
|
+
// surfaces it as a RUN_ERROR).
|
|
202
283
|
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
|
-
}
|
|
284
|
+
for (const call of msg.toolCalls) blocks.push(toolUseBlock(call))
|
|
230
285
|
}
|
|
231
286
|
|
|
232
287
|
return blocks
|
|
233
288
|
}
|
|
234
289
|
|
|
290
|
+
function toolUseBlock(
|
|
291
|
+
call: NonNullable<ModelMessage['toolCalls']>[number],
|
|
292
|
+
): ContentBlock {
|
|
293
|
+
const rawArguments = call.function.arguments || '{}'
|
|
294
|
+
let parsed: unknown = {}
|
|
295
|
+
try {
|
|
296
|
+
parsed = JSON.parse(rawArguments)
|
|
297
|
+
} catch {
|
|
298
|
+
// The tool result already told the model the arguments were malformed.
|
|
299
|
+
}
|
|
300
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`Bedrock Converse: tool call "${call.function.name}" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
toolUse: {
|
|
307
|
+
toolUseId: call.id,
|
|
308
|
+
name: call.function.name,
|
|
309
|
+
input: parsed as DocumentType,
|
|
310
|
+
},
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
235
314
|
// ---------------------------------------------------------------------------
|
|
236
315
|
// Public API
|
|
237
316
|
// ---------------------------------------------------------------------------
|
|
@@ -248,6 +327,7 @@ function messageToBlocks(
|
|
|
248
327
|
export function toConverseMessages(
|
|
249
328
|
messages: Array<ModelMessage>,
|
|
250
329
|
systemPrompts?: Array<SystemPrompt>,
|
|
330
|
+
context?: ConverseReplayContext,
|
|
251
331
|
): { system: Array<SystemContentBlock>; messages: Array<Message> } {
|
|
252
332
|
// Build system blocks (uses normalizeSystemPrompts for runtime validation)
|
|
253
333
|
const system: Array<SystemContentBlock> =
|
|
@@ -264,12 +344,33 @@ export function toConverseMessages(
|
|
|
264
344
|
// gets a unique name, preventing Bedrock ValidationException for duplicate names.
|
|
265
345
|
const docCounter = { value: 0 }
|
|
266
346
|
|
|
267
|
-
|
|
347
|
+
// History from another model loses its signatures, and its tool IDs are
|
|
348
|
+
// rewritten to the shape Converse accepts. Each call stays paired.
|
|
349
|
+
const replay = transformMessagesForReplay(
|
|
350
|
+
messages,
|
|
351
|
+
context
|
|
352
|
+
? {
|
|
353
|
+
provider: context.provider ?? 'amazon-bedrock',
|
|
354
|
+
api: 'bedrock-converse-stream',
|
|
355
|
+
model: context.model,
|
|
356
|
+
}
|
|
357
|
+
: undefined,
|
|
358
|
+
(id, { foreign, attempt }) => {
|
|
359
|
+
if (!foreign && attempt === 0) return id
|
|
360
|
+
const clean = id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64)
|
|
361
|
+
return attempt === 0
|
|
362
|
+
? clean
|
|
363
|
+
: clean.slice(0, 47) +
|
|
364
|
+
'_' +
|
|
365
|
+
hashToolCallId(id + ':' + attempt).slice(0, 16)
|
|
366
|
+
},
|
|
367
|
+
)
|
|
368
|
+
for (const msg of replay.messages) {
|
|
268
369
|
// Map TanStack roles to Converse roles
|
|
269
370
|
const converseRole: 'user' | 'assistant' =
|
|
270
371
|
msg.role === 'assistant' ? 'assistant' : 'user'
|
|
271
372
|
|
|
272
|
-
const blocks = messageToBlocks(msg, docCounter)
|
|
373
|
+
const blocks = messageToBlocks(msg, docCounter, context)
|
|
273
374
|
|
|
274
375
|
// Skip messages that produce no content blocks (e.g. assistant with
|
|
275
376
|
// null content and no toolCalls). Pushing an empty-content message to
|
|
@@ -287,3 +388,34 @@ export function toConverseMessages(
|
|
|
287
388
|
|
|
288
389
|
return { system, messages: converseMessages }
|
|
289
390
|
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Write the `toolUse` and `toolResult` blocks as text blocks. Bedrock rejects
|
|
394
|
+
* these blocks in a request with no `toolConfig`, so a request with no tools
|
|
395
|
+
* sends its tool history this way.
|
|
396
|
+
*/
|
|
397
|
+
export function toolBlocksToText(messages: Array<Message>): Array<Message> {
|
|
398
|
+
return messages.map((message) => ({
|
|
399
|
+
...message,
|
|
400
|
+
content: message.content?.flatMap((block): Array<ContentBlock> => {
|
|
401
|
+
if (block.toolUse) {
|
|
402
|
+
const { toolUseId, name, input } = block.toolUse
|
|
403
|
+
return [
|
|
404
|
+
{
|
|
405
|
+
text: `[Tool call ${toolUseId} ${name}(${JSON.stringify(input)})]`,
|
|
406
|
+
},
|
|
407
|
+
]
|
|
408
|
+
}
|
|
409
|
+
if (block.toolResult) {
|
|
410
|
+
const { toolUseId, content = [] } = block.toolResult
|
|
411
|
+
const text = content.map((part) => part.text ?? '').join('')
|
|
412
|
+
// Keep the images of the result as image blocks after the text.
|
|
413
|
+
const images = content.flatMap((part) =>
|
|
414
|
+
part.image ? [{ image: part.image }] : [],
|
|
415
|
+
)
|
|
416
|
+
return [{ text: `[Tool result ${toolUseId}: ${text}]` }, ...images]
|
|
417
|
+
}
|
|
418
|
+
return [block]
|
|
419
|
+
}),
|
|
420
|
+
}))
|
|
421
|
+
}
|
|
@@ -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,30 @@ 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
|
+
arguments: string
|
|
76
|
+
}
|
|
88
77
|
>()
|
|
89
78
|
|
|
90
79
|
// Usage + finish-reason are captured during iteration and folded into the
|
|
@@ -109,12 +98,50 @@ export async function* processConverseStream(
|
|
|
109
98
|
|
|
110
99
|
// Close an open reasoning message before text/tool content begins, mirroring
|
|
111
100
|
// openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.
|
|
112
|
-
function*
|
|
113
|
-
|
|
114
|
-
|
|
101
|
+
function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {
|
|
102
|
+
const reasoning = reasoningByIndex.get(index)
|
|
103
|
+
if (!reasoning) return
|
|
104
|
+
const encrypted = reasoning.redacted
|
|
105
|
+
? Buffer.concat(reasoning.bytes).toString('base64')
|
|
106
|
+
: reasoning.signature
|
|
107
|
+
if (!encrypted || encrypted === reasoning.encrypted) return
|
|
108
|
+
reasoning.encrypted = encrypted
|
|
109
|
+
yield {
|
|
110
|
+
type: EventType.REASONING_ENCRYPTED_VALUE,
|
|
111
|
+
entityId: reasoning.id,
|
|
112
|
+
subtype: 'message',
|
|
113
|
+
encryptedValue: encrypted,
|
|
114
|
+
...(reasoning.redacted && {
|
|
115
|
+
stepId: reasoning.id.startsWith('redacted_thinking-')
|
|
116
|
+
? reasoning.id
|
|
117
|
+
: 'redacted_thinking-' + reasoning.id,
|
|
118
|
+
}),
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {
|
|
124
|
+
for (const [blockIndex, reasoning] of reasoningByIndex) {
|
|
125
|
+
if (index !== undefined && index !== blockIndex) continue
|
|
126
|
+
yield* encryptedReasoning(blockIndex)
|
|
127
|
+
if (reasoning.closed) continue
|
|
128
|
+
reasoning.closed = true
|
|
129
|
+
yield {
|
|
130
|
+
type: EventType.STEP_FINISHED,
|
|
131
|
+
stepId: reasoning.id,
|
|
132
|
+
stepName: reasoning.id,
|
|
133
|
+
delta: '',
|
|
134
|
+
content: reasoning.text,
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
}
|
|
115
137
|
yield {
|
|
116
138
|
type: EventType.REASONING_MESSAGE_END,
|
|
117
|
-
messageId:
|
|
139
|
+
messageId: reasoning.id,
|
|
140
|
+
timestamp: Date.now(),
|
|
141
|
+
}
|
|
142
|
+
yield {
|
|
143
|
+
type: EventType.REASONING_END,
|
|
144
|
+
messageId: reasoning.id,
|
|
118
145
|
timestamp: Date.now(),
|
|
119
146
|
}
|
|
120
147
|
}
|
|
@@ -141,6 +168,7 @@ export async function* processConverseStream(
|
|
|
141
168
|
id,
|
|
142
169
|
name,
|
|
143
170
|
started: true,
|
|
171
|
+
arguments: '',
|
|
144
172
|
})
|
|
145
173
|
yield {
|
|
146
174
|
type: EventType.TOOL_CALL_START,
|
|
@@ -163,6 +191,7 @@ export async function* processConverseStream(
|
|
|
163
191
|
if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {
|
|
164
192
|
const toolCall = toolCallsByIndex.get(index)
|
|
165
193
|
if (toolCall?.started) {
|
|
194
|
+
toolCall.arguments += delta.toolUse.input
|
|
166
195
|
yield {
|
|
167
196
|
type: EventType.TOOL_CALL_ARGS,
|
|
168
197
|
toolCallId: toolCall.id,
|
|
@@ -173,28 +202,66 @@ export async function* processConverseStream(
|
|
|
173
202
|
continue
|
|
174
203
|
}
|
|
175
204
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
205
|
+
if (delta && 'reasoningContent' in delta && delta.reasoningContent) {
|
|
206
|
+
const fragment = delta.reasoningContent
|
|
207
|
+
let reasoning = reasoningByIndex.get(index)
|
|
208
|
+
if (!reasoning) {
|
|
209
|
+
const redacted =
|
|
210
|
+
'redactedContent' in fragment &&
|
|
211
|
+
(fragment.redactedContent?.length ?? 0) > 0
|
|
212
|
+
reasoning = {
|
|
213
|
+
id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),
|
|
214
|
+
text: '',
|
|
215
|
+
signature: '',
|
|
216
|
+
bytes: [],
|
|
217
|
+
redacted,
|
|
218
|
+
closed: false,
|
|
219
|
+
}
|
|
220
|
+
reasoningByIndex.set(index, reasoning)
|
|
221
|
+
yield {
|
|
222
|
+
type: EventType.STEP_STARTED,
|
|
223
|
+
stepId: reasoning.id,
|
|
224
|
+
stepName: reasoning.id,
|
|
225
|
+
stepType: 'thinking',
|
|
226
|
+
timestamp: Date.now(),
|
|
227
|
+
}
|
|
186
228
|
yield {
|
|
187
229
|
type: EventType.REASONING_MESSAGE_START,
|
|
188
|
-
messageId:
|
|
230
|
+
messageId: reasoning.id,
|
|
189
231
|
role: 'reasoning',
|
|
190
232
|
timestamp: Date.now(),
|
|
191
233
|
}
|
|
192
234
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
235
|
+
if (
|
|
236
|
+
!reasoning.redacted &&
|
|
237
|
+
'text' in fragment &&
|
|
238
|
+
fragment.text !== undefined
|
|
239
|
+
) {
|
|
240
|
+
reasoning.text += fragment.text
|
|
241
|
+
yield {
|
|
242
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
243
|
+
messageId: reasoning.id,
|
|
244
|
+
delta: fragment.text,
|
|
245
|
+
timestamp: Date.now(),
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (
|
|
249
|
+
!reasoning.redacted &&
|
|
250
|
+
'signature' in fragment &&
|
|
251
|
+
fragment.signature !== undefined
|
|
252
|
+
)
|
|
253
|
+
reasoning.signature += fragment.signature
|
|
254
|
+
if (
|
|
255
|
+
'redactedContent' in fragment &&
|
|
256
|
+
fragment.redactedContent &&
|
|
257
|
+
fragment.redactedContent.length > 0
|
|
258
|
+
) {
|
|
259
|
+
if (!reasoning.redacted) reasoning.encrypted = undefined
|
|
260
|
+
reasoning.redacted = true
|
|
261
|
+
reasoning.text = ''
|
|
262
|
+
reasoning.signature = ''
|
|
263
|
+
reasoning.bytes.push(fragment.redactedContent)
|
|
264
|
+
yield* encryptedReasoning(index)
|
|
198
265
|
}
|
|
199
266
|
continue
|
|
200
267
|
}
|
|
@@ -225,11 +292,13 @@ export async function* processConverseStream(
|
|
|
225
292
|
|
|
226
293
|
if ('contentBlockStop' in ev) {
|
|
227
294
|
const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0
|
|
295
|
+
yield* closeReasoning(stopIndex)
|
|
228
296
|
const toolCall = toolCallsByIndex.get(stopIndex)
|
|
229
297
|
if (toolCall?.started) {
|
|
230
298
|
yield {
|
|
231
299
|
type: EventType.TOOL_CALL_END,
|
|
232
300
|
toolCallId: toolCall.id,
|
|
301
|
+
args: toolCall.arguments,
|
|
233
302
|
toolCallName: toolCall.name,
|
|
234
303
|
toolName: toolCall.name,
|
|
235
304
|
timestamp: Date.now(),
|
|
@@ -272,6 +341,7 @@ export async function* processConverseStream(
|
|
|
272
341
|
yield {
|
|
273
342
|
type: EventType.TOOL_CALL_END,
|
|
274
343
|
toolCallId: toolCall.id,
|
|
344
|
+
args: toolCall.arguments,
|
|
275
345
|
toolCallName: toolCall.name,
|
|
276
346
|
toolName: toolCall.name,
|
|
277
347
|
timestamp: Date.now(),
|