@code-yeongyu/senpi-ai 2026.7.31 → 2026.8.3
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/README.md +2 -1
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +56 -25
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/anthropic-tool-pairs.d.ts +3 -0
- package/dist/api/anthropic-tool-pairs.d.ts.map +1 -0
- package/dist/api/anthropic-tool-pairs.js +134 -0
- package/dist/api/anthropic-tool-pairs.js.map +1 -0
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +7 -3
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +64 -4
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +12 -2
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +13 -6
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +12 -2
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +19 -10
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses/fallback-state.d.ts +24 -0
- package/dist/api/openai-codex-responses/fallback-state.d.ts.map +1 -0
- package/dist/api/openai-codex-responses/fallback-state.js +70 -0
- package/dist/api/openai-codex-responses/fallback-state.js.map +1 -0
- package/dist/api/openai-codex-responses.d.ts +2 -16
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +94 -105
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +19 -10
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-prompt-cache.d.ts +1 -0
- package/dist/api/openai-prompt-cache.d.ts.map +1 -1
- package/dist/api/openai-prompt-cache.js +7 -0
- package/dist/api/openai-prompt-cache.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +11 -1
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +9 -5
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/openrouter-images.js +3 -2
- package/dist/api/openrouter-images.js.map +1 -1
- package/dist/api/pi-messages.js +2 -2
- package/dist/api/pi-messages.js.map +1 -1
- package/dist/api/simple-options.d.ts.map +1 -1
- package/dist/api/simple-options.js +1 -0
- package/dist/api/simple-options.js.map +1 -1
- package/dist/auth/oauth/openrouter.d.ts +3 -1
- package/dist/auth/oauth/openrouter.d.ts.map +1 -1
- package/dist/auth/oauth/openrouter.js +81 -26
- package/dist/auth/oauth/openrouter.js.map +1 -1
- package/dist/auth/resolve.d.ts +2 -0
- package/dist/auth/resolve.d.ts.map +1 -1
- package/dist/auth/resolve.js +16 -8
- package/dist/auth/resolve.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/model.d.ts +2 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js.map +1 -1
- package/dist/models.d.ts +14 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +95 -19
- package/dist/models.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/azure-openai-responses.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/huggingface.json +1 -1
- package/dist/providers/data/mistral.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/dist/providers/data/qwen-token-plan.json +1 -1
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/data/zai-coding-cn.json +1 -1
- package/dist/providers/data/zai.json +1 -1
- package/dist/providers/faux.d.ts.map +1 -1
- package/dist/providers/faux.js +4 -1
- package/dist/providers/faux.js.map +1 -1
- package/dist/providers/opencode-go.js +1 -1
- package/dist/providers/opencode-go.js.map +1 -1
- package/dist/types.d.ts +18 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/error-body.d.ts.map +1 -1
- package/dist/utils/error-body.js +23 -4
- package/dist/utils/error-body.js.map +1 -1
- package/dist/utils/prompt-cache-ttl.d.ts.map +1 -1
- package/dist/utils/prompt-cache-ttl.js +10 -1
- package/dist/utils/prompt-cache-ttl.js.map +1 -1
- package/dist/utils/provider-retry.d.ts +3 -0
- package/dist/utils/provider-retry.d.ts.map +1 -1
- package/dist/utils/provider-retry.js +13 -12
- package/dist/utils/provider-retry.js.map +1 -1
- package/dist/utils/retry-hint.d.ts +24 -0
- package/dist/utils/retry-hint.d.ts.map +1 -0
- package/dist/utils/retry-hint.js +233 -0
- package/dist/utils/retry-hint.js.map +1 -0
- package/dist/utils/unavailable-tool-text.d.ts +3 -0
- package/dist/utils/unavailable-tool-text.d.ts.map +1 -0
- package/dist/utils/unavailable-tool-text.js +36 -0
- package/dist/utils/unavailable-tool-text.js.map +1 -0
- package/package.json +10 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-tool-pairs.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-tool-pairs.ts"],"names":[],"mappings":"AAsGA,kGAAkG;AAClG,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAoDpE"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
function isObject(value) {
|
|
2
|
+
return typeof value === "object" && value !== null;
|
|
3
|
+
}
|
|
4
|
+
function hasMessagesArray(value) {
|
|
5
|
+
return isObject(value) && Array.isArray(value.messages);
|
|
6
|
+
}
|
|
7
|
+
function isContentBlockParam(value) {
|
|
8
|
+
return isObject(value) && typeof value.type === "string";
|
|
9
|
+
}
|
|
10
|
+
function isToolUseBlock(block) {
|
|
11
|
+
return block.type === "tool_use" && "id" in block && typeof block.id === "string";
|
|
12
|
+
}
|
|
13
|
+
function isToolResultBlock(block) {
|
|
14
|
+
return block.type === "tool_result";
|
|
15
|
+
}
|
|
16
|
+
const SYNTHETIC_OUTPUT = "Tool output unavailable (interrupted before result)";
|
|
17
|
+
function isMessageWithArrayContent(value, role) {
|
|
18
|
+
if (!isObject(value) || value.role !== role || !Array.isArray(value.content))
|
|
19
|
+
return false;
|
|
20
|
+
for (const block of value.content) {
|
|
21
|
+
if (!isContentBlockParam(block))
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
function isUserMessageWithStringContent(value) {
|
|
27
|
+
return isObject(value) && value.role === "user" && typeof value.content === "string";
|
|
28
|
+
}
|
|
29
|
+
function getToolUseIds(message) {
|
|
30
|
+
const ids = [];
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
for (const block of message.content) {
|
|
33
|
+
if (!isToolUseBlock(block) || block.id.length === 0 || seen.has(block.id))
|
|
34
|
+
continue;
|
|
35
|
+
seen.add(block.id);
|
|
36
|
+
ids.push(block.id);
|
|
37
|
+
}
|
|
38
|
+
return ids;
|
|
39
|
+
}
|
|
40
|
+
function syntheticToolResult(toolUseId) {
|
|
41
|
+
return {
|
|
42
|
+
type: "tool_result",
|
|
43
|
+
tool_use_id: toolUseId,
|
|
44
|
+
content: SYNTHETIC_OUTPUT,
|
|
45
|
+
is_error: true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function sameBlocks(left, right) {
|
|
49
|
+
return left.length === right.length && left.every((block, index) => block === right[index]);
|
|
50
|
+
}
|
|
51
|
+
function repairFollowingUserMessage(message, expectedIds) {
|
|
52
|
+
const expected = new Set(expectedIds);
|
|
53
|
+
const found = new Set();
|
|
54
|
+
const results = [];
|
|
55
|
+
const ordinary = [];
|
|
56
|
+
for (const block of message.content) {
|
|
57
|
+
if (!isToolResultBlock(block)) {
|
|
58
|
+
ordinary.push(block);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const id = block.tool_use_id;
|
|
62
|
+
if (typeof id !== "string" || !expected.has(id) || found.has(id))
|
|
63
|
+
continue;
|
|
64
|
+
found.add(id);
|
|
65
|
+
results.push(block);
|
|
66
|
+
}
|
|
67
|
+
for (const id of expectedIds) {
|
|
68
|
+
if (!found.has(id))
|
|
69
|
+
results.push(syntheticToolResult(id));
|
|
70
|
+
}
|
|
71
|
+
const content = [...results, ...ordinary];
|
|
72
|
+
if (sameBlocks(content, message.content))
|
|
73
|
+
return { message, changed: false };
|
|
74
|
+
return { message: { ...message, content }, changed: true };
|
|
75
|
+
}
|
|
76
|
+
function removeOrphanResults(message) {
|
|
77
|
+
const content = message.content.filter((block) => !isToolResultBlock(block));
|
|
78
|
+
if (content.length === message.content.length)
|
|
79
|
+
return { message, changed: false };
|
|
80
|
+
if (content.length === 0)
|
|
81
|
+
return { changed: true };
|
|
82
|
+
return { message: { ...message, content }, changed: true };
|
|
83
|
+
}
|
|
84
|
+
/** Repairs Anthropic client tool_use/tool_result adjacency without mutating the input payload. */
|
|
85
|
+
export function sanitizeAnthropicToolPairs(payload) {
|
|
86
|
+
if (!hasMessagesArray(payload))
|
|
87
|
+
return payload;
|
|
88
|
+
let changed = false;
|
|
89
|
+
const sanitizedMessages = [];
|
|
90
|
+
for (let index = 0; index < payload.messages.length; index++) {
|
|
91
|
+
const unknownMessage = payload.messages[index];
|
|
92
|
+
if (isMessageWithArrayContent(unknownMessage, "assistant")) {
|
|
93
|
+
sanitizedMessages.push(unknownMessage);
|
|
94
|
+
const expectedIds = getToolUseIds(unknownMessage);
|
|
95
|
+
if (expectedIds.length === 0)
|
|
96
|
+
continue;
|
|
97
|
+
const nextMessage = payload.messages[index + 1];
|
|
98
|
+
if (isMessageWithArrayContent(nextMessage, "user")) {
|
|
99
|
+
const repaired = repairFollowingUserMessage(nextMessage, expectedIds);
|
|
100
|
+
sanitizedMessages.push(repaired.message);
|
|
101
|
+
changed ||= repaired.changed;
|
|
102
|
+
index++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (isUserMessageWithStringContent(nextMessage)) {
|
|
106
|
+
sanitizedMessages.push({
|
|
107
|
+
...nextMessage,
|
|
108
|
+
content: [...expectedIds.map(syntheticToolResult), { type: "text", text: nextMessage.content }],
|
|
109
|
+
});
|
|
110
|
+
changed = true;
|
|
111
|
+
index++;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
sanitizedMessages.push({
|
|
115
|
+
role: "user",
|
|
116
|
+
content: expectedIds.map(syntheticToolResult),
|
|
117
|
+
});
|
|
118
|
+
changed = true;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (isMessageWithArrayContent(unknownMessage, "user")) {
|
|
122
|
+
const repaired = removeOrphanResults(unknownMessage);
|
|
123
|
+
if (repaired.message)
|
|
124
|
+
sanitizedMessages.push(repaired.message);
|
|
125
|
+
changed ||= repaired.changed;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
sanitizedMessages.push(unknownMessage);
|
|
129
|
+
}
|
|
130
|
+
if (!changed)
|
|
131
|
+
return payload;
|
|
132
|
+
return { ...payload, messages: sanitizedMessages };
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=anthropic-tool-pairs.js.map
|
|
@@ -0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AC;YAED,IAAI,8BAA8B,CAAC,WAAW,CAAC,EAAE,CAAC;gBACjD,iBAAiB,CAAC,IAAI,CAAC;oBACtB,GAAG,WAAW;oBACd,OAAO,EAAE,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC;iBAC/F,CAAC,CAAC;gBACH,OAAO,GAAG,IAAI,CAAC;gBACf,KAAK,EAAE,CAAC;gBACR,SAAS;YACV,CAAC;YAED,iBAAiB,CAAC,IAAI,CAAC;gBACtB,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE,WAAW,CAAC,GAAG,CAAC,mBAAmB,CAAC;aAC7C,CAAC,CAAC;YACH,OAAO,GAAG,IAAI,CAAC;YACf,SAAS;QACV,CAAC;QAED,IAAI,yBAAyB,CAAC,cAAc,EAAE,MAAM,CAAC,EAAE,CAAC;YACvD,MAAM,QAAQ,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAC;YACrD,IAAI,QAAQ,CAAC,OAAO;gBAAE,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YAC/D,OAAO,KAAK,QAAQ,CAAC,OAAO,CAAC;YAC7B,SAAS;QACV,CAAC;QAED,iBAAiB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IACxC,CAAC;IAED,IAAI,CAAC,OAAO;QAAE,OAAO,OAAO,CAAC;IAC7B,OAAO,EAAE,GAAG,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import type { ContentBlockParam, MessageParam } from \"@anthropic-ai/sdk/resources/messages.js\";\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction hasMessagesArray(value: unknown): value is { messages: unknown[] } {\n\treturn isObject(value) && Array.isArray(value.messages);\n}\n\nfunction isContentBlockParam(value: unknown): value is ContentBlockParam {\n\treturn isObject(value) && typeof value.type === \"string\";\n}\n\nfunction isToolUseBlock(block: ContentBlockParam): block is Extract<ContentBlockParam, { type: \"tool_use\" }> {\n\treturn block.type === \"tool_use\" && \"id\" in block && typeof block.id === \"string\";\n}\n\nfunction isToolResultBlock(block: ContentBlockParam): block is Extract<ContentBlockParam, { type: \"tool_result\" }> {\n\treturn block.type === \"tool_result\";\n}\n\nconst SYNTHETIC_OUTPUT = \"Tool output unavailable (interrupted before result)\";\n\nfunction isMessageWithArrayContent<Role extends MessageParam[\"role\"]>(\n\tvalue: unknown,\n\trole: Role,\n): value is MessageParam & { role: Role; content: ContentBlockParam[] } {\n\tif (!isObject(value) || value.role !== role || !Array.isArray(value.content)) return false;\n\tfor (const block of value.content) {\n\t\tif (!isContentBlockParam(block)) return false;\n\t}\n\treturn true;\n}\n\nfunction isUserMessageWithStringContent(value: unknown): value is MessageParam & { role: \"user\"; content: string } {\n\treturn isObject(value) && value.role === \"user\" && typeof value.content === \"string\";\n}\n\nfunction getToolUseIds(message: MessageParam & { content: ContentBlockParam[] }): string[] {\n\tconst ids: string[] = [];\n\tconst seen = new Set<string>();\n\tfor (const block of message.content) {\n\t\tif (!isToolUseBlock(block) || block.id.length === 0 || seen.has(block.id)) continue;\n\t\tseen.add(block.id);\n\t\tids.push(block.id);\n\t}\n\treturn ids;\n}\n\nfunction syntheticToolResult(toolUseId: string): Extract<ContentBlockParam, { type: \"tool_result\" }> {\n\treturn {\n\t\ttype: \"tool_result\",\n\t\ttool_use_id: toolUseId,\n\t\tcontent: SYNTHETIC_OUTPUT,\n\t\tis_error: true,\n\t};\n}\n\nfunction sameBlocks(left: ContentBlockParam[], right: ContentBlockParam[]): boolean {\n\treturn left.length === right.length && left.every((block, index) => block === right[index]);\n}\n\nfunction repairFollowingUserMessage(\n\tmessage: MessageParam & { role: \"user\"; content: ContentBlockParam[] },\n\texpectedIds: string[],\n): { message: MessageParam; changed: boolean } {\n\tconst expected = new Set(expectedIds);\n\tconst found = new Set<string>();\n\tconst results: ContentBlockParam[] = [];\n\tconst ordinary: ContentBlockParam[] = [];\n\n\tfor (const block of message.content) {\n\t\tif (!isToolResultBlock(block)) {\n\t\t\tordinary.push(block);\n\t\t\tcontinue;\n\t\t}\n\t\tconst id = block.tool_use_id;\n\t\tif (typeof id !== \"string\" || !expected.has(id) || found.has(id)) continue;\n\t\tfound.add(id);\n\t\tresults.push(block);\n\t}\n\n\tfor (const id of expectedIds) {\n\t\tif (!found.has(id)) results.push(syntheticToolResult(id));\n\t}\n\n\tconst content = [...results, ...ordinary];\n\tif (sameBlocks(content, message.content)) return { message, changed: false };\n\treturn { message: { ...message, content }, changed: true };\n}\n\nfunction removeOrphanResults(message: MessageParam & { role: \"user\"; content: ContentBlockParam[] }): {\n\tmessage?: MessageParam;\n\tchanged: boolean;\n} {\n\tconst content = message.content.filter((block) => !isToolResultBlock(block));\n\tif (content.length === message.content.length) return { message, changed: false };\n\tif (content.length === 0) return { changed: true };\n\treturn { message: { ...message, content }, changed: true };\n}\n\n/** Repairs Anthropic client tool_use/tool_result adjacency without mutating the input payload. */\nexport function sanitizeAnthropicToolPairs(payload: unknown): unknown {\n\tif (!hasMessagesArray(payload)) return payload;\n\n\tlet changed = false;\n\tconst sanitizedMessages: unknown[] = [];\n\n\tfor (let index = 0; index < payload.messages.length; index++) {\n\t\tconst unknownMessage = payload.messages[index];\n\t\tif (isMessageWithArrayContent(unknownMessage, \"assistant\")) {\n\t\t\tsanitizedMessages.push(unknownMessage);\n\t\t\tconst expectedIds = getToolUseIds(unknownMessage);\n\t\t\tif (expectedIds.length === 0) continue;\n\n\t\t\tconst nextMessage = payload.messages[index + 1];\n\t\t\tif (isMessageWithArrayContent(nextMessage, \"user\")) {\n\t\t\t\tconst repaired = repairFollowingUserMessage(nextMessage, expectedIds);\n\t\t\t\tsanitizedMessages.push(repaired.message);\n\t\t\t\tchanged ||= repaired.changed;\n\t\t\t\tindex++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (isUserMessageWithStringContent(nextMessage)) {\n\t\t\t\tsanitizedMessages.push({\n\t\t\t\t\t...nextMessage,\n\t\t\t\t\tcontent: [...expectedIds.map(syntheticToolResult), { type: \"text\", text: nextMessage.content }],\n\t\t\t\t});\n\t\t\t\tchanged = true;\n\t\t\t\tindex++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tsanitizedMessages.push({\n\t\t\t\trole: \"user\",\n\t\t\t\tcontent: expectedIds.map(syntheticToolResult),\n\t\t\t});\n\t\t\tchanged = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (isMessageWithArrayContent(unknownMessage, \"user\")) {\n\t\t\tconst repaired = removeOrphanResults(unknownMessage);\n\t\t\tif (repaired.message) sanitizedMessages.push(repaired.message);\n\t\t\tchanged ||= repaired.changed;\n\t\t\tcontinue;\n\t\t}\n\n\t\tsanitizedMessages.push(unknownMessage);\n\t}\n\n\tif (!changed) return payload;\n\treturn { ...payload, messages: sanitizedMessages };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-responses.d.ts","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAKX,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,MAAM,aAAa,CAAC;AA4CrB,MAAM,WAAW,2BAA4B,SAAQ,aAAa;IACjE,eAAe,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;IAC1E,gBAAgB,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;IAC1D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,wBAAwB,EAAE,2BAA2B,
|
|
1
|
+
{"version":3,"file":"azure-openai-responses.d.ts","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAKX,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,MAAM,aAAa,CAAC;AA4CrB,MAAM,WAAW,2BAA4B,SAAQ,aAAa;IACjE,eAAe,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;IAC1E,gBAAgB,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;IAC1D,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,wBAAwB,EAAE,2BAA2B,CA2FxF,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,wBAAwB,EAAE,mBAAmB,CAyBtF,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AzureOpenAI } from "openai";
|
|
2
|
-
import { clampThinkingLevel } from "../models.js";
|
|
2
|
+
import { clampThinkingLevel, supportsMax } from "../models.js";
|
|
3
3
|
import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
|
|
4
4
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
5
|
import { headersToRecord } from "../utils/headers.js";
|
|
@@ -60,7 +60,7 @@ export const stream = (model, context, options) => {
|
|
|
60
60
|
totalTokens: 0,
|
|
61
61
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
62
62
|
},
|
|
63
|
-
stopReason: "
|
|
63
|
+
stopReason: "pending",
|
|
64
64
|
timestamp: Date.now(),
|
|
65
65
|
};
|
|
66
66
|
try {
|
|
@@ -92,6 +92,9 @@ export const stream = (model, context, options) => {
|
|
|
92
92
|
if (options?.signal?.aborted) {
|
|
93
93
|
throw new Error("Request was aborted");
|
|
94
94
|
}
|
|
95
|
+
if (output.stopReason === "pending") {
|
|
96
|
+
throw new Error("Azure OpenAI Responses stream ended without a stop reason");
|
|
97
|
+
}
|
|
95
98
|
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
96
99
|
throw new Error("An unknown error occurred");
|
|
97
100
|
}
|
|
@@ -122,7 +125,7 @@ export const streamSimple = (model, context, options) => {
|
|
|
122
125
|
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;
|
|
123
126
|
const reasoningEffort = clampedReasoning === "off"
|
|
124
127
|
? undefined
|
|
125
|
-
: clampedReasoning === "max" && model
|
|
128
|
+
: clampedReasoning === "max" && supportsMax(model)
|
|
126
129
|
? "max"
|
|
127
130
|
: clampedReasoning === "max"
|
|
128
131
|
? "high"
|
|
@@ -191,6 +194,7 @@ function createClient(model, apiKey, options) {
|
|
|
191
194
|
apiKey,
|
|
192
195
|
apiVersion,
|
|
193
196
|
dangerouslyAllowBrowser: true,
|
|
197
|
+
fetch: options?.fetch,
|
|
194
198
|
defaultHeaders: headers,
|
|
195
199
|
baseURL: baseUrl,
|
|
196
200
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-responses.js","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAUlD,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBA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AS,EAAE,kCAAkC,CAAC,CAAC;IAC5F,CAAC;IAED,IAAI,OAAO,EAAE,WAAW,KAAK,SAAS,EAAE,CAAC;QACxC,MAAM,CAAC,WAAW,GAAG,OAAO,EAAE,WAAW,CAAC;IAC3C,CAAC;IAED,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,CAAC,KAAK,GAAG,qBAAqB,CAAC,OAAO,CAAC,KAAK,EAAE;YACnD,kBAAkB,EAAE,KAAK,CAAC,MAAM,EAAE,kBAAkB,IAAI,IAAI;YAC5D,0BAA0B,EAAE,KAAK,CAAC,MAAM,EAAE,0BAA0B,IAAI,KAAK;SAC7E,CAAC,CAAC;IACJ,CAAC;IAED,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;QACrB,IAAI,kBAAkB,EAAE,CAAC;YACxB,MAAM,CAAC,SAAS,GAAG;gBAClB,MAAM,EAAE,eAAiE;gBACzE,GAAG,CAAC,OAAO,EAAE,gBAAgB,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,IAAI,MAAM,EAAE,CAAC;aAC/F,CAAC;YACF,MAAM,CAAC,OAAO,GAAG,CAAC,6BAA6B,CAAC,CAAC;QAClD,CAAC;aAAM,IAAI,CAAC,oBAAoB,IAAI,KAAK,CAAC,gBAAgB,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;YAC1E,MAAM,CAAC,SAAS,GAAG;gBAClB,MAAM,EAAE,CAAC,KAAK,CAAC,gBAAgB,EAAE,GAAG,IAAI,MAAM,CAAmD;aACjG,CAAC;QACH,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { AzureOpenAI } from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport { convertResponsesMessages, convertResponsesTools, processResponsesStream } from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nconst DEFAULT_AZURE_API_VERSION = \"v1\";\nconst AZURE_TOOL_CALL_PROVIDERS = new Set([\"openai\", \"openai-codex\", \"opencode\", \"azure-openai-responses\"]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\n\nfunction parseDeploymentNameMap(value: string | undefined): Map<string, string> {\n\tconst map = new Map<string, string>();\n\tif (!value) return map;\n\tfor (const entry of value.split(\",\")) {\n\t\tconst trimmed = entry.trim();\n\t\tif (!trimmed) continue;\n\t\tconst [modelId, deploymentName] = trimmed.split(\"=\", 2);\n\t\tif (!modelId || !deploymentName) continue;\n\t\tmap.set(modelId.trim(), deploymentName.trim());\n\t}\n\treturn map;\n}\n\nfunction resolveDeploymentName(model: Model<\"azure-openai-responses\">, options?: AzureOpenAIResponsesOptions): string {\n\tif (options?.azureDeploymentName) {\n\t\treturn options.azureDeploymentName;\n\t}\n\tconst mappedDeployment = parseDeploymentNameMap(\n\t\tgetProviderEnvValue(\"AZURE_OPENAI_DEPLOYMENT_NAME_MAP\", options?.env),\n\t).get(model.id);\n\treturn mappedDeployment || model.id;\n}\n\nfunction formatAzureOpenAIError(error: unknown): string {\n\treturn formatProviderError(normalizeProviderError(error), \"Azure OpenAI API error\");\n}\n\n// Azure OpenAI Responses-specific options\nexport interface AzureOpenAIResponsesOptions extends StreamOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n\tazureApiVersion?: string;\n\tazureResourceName?: string;\n\tazureBaseUrl?: string;\n\tazureDeploymentName?: string;\n}\n\n/**\n * Generate function for Azure OpenAI Responses API\n */\nexport const stream: StreamFunction<\"azure-openai-responses\", AzureOpenAIResponsesOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions?: AzureOpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t// Start async processing\n\t(async () => {\n\t\tconst deploymentName = resolveDeploymentName(model, options);\n\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"azure-openai-responses\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create Azure OpenAI client\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\t\t\tconst client = createClient(model, apiKey, options);\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tcontext.tools,\n\t\t\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t\t);\n\t\t\tlet params = buildParams(model, context, options, deploymentName, grammarToolInputProperties);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, { grammarToolInputProperties });\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tfor (const block of output.content) {\n\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t// Streaming scratch buffers are only used during parsing; never persist them.\n\t\t\t\tdelete (block as { partialJson?: string }).partialJson;\n\t\t\t\tdelete (block as { customInput?: unknown }).customInput;\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatAzureOpenAIError(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"azure-openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey;\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = buildBaseOptions(model, context, options, apiKey);\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort =\n\t\tclampedReasoning === \"off\"\n\t\t\t? undefined\n\t\t\t: clampedReasoning === \"max\" && model.thinkingLevelMap?.max !== undefined\n\t\t\t\t? \"max\"\n\t\t\t\t: clampedReasoning === \"max\"\n\t\t\t\t\t? \"high\"\n\t\t\t\t\t: clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies AzureOpenAIResponsesOptions);\n};\n\nfunction normalizeAzureBaseUrl(baseUrl: string): string {\n\tconst trimmed = baseUrl.trim().replace(/\\/+$/, \"\");\n\tlet url: URL;\n\ttry {\n\t\turl = new URL(trimmed);\n\t} catch {\n\t\tthrow new Error(`Invalid Azure OpenAI base URL: ${baseUrl}`);\n\t}\n\n\tconst isAzureHost =\n\t\turl.hostname.endsWith(\".openai.azure.com\") ||\n\t\turl.hostname.endsWith(\".cognitiveservices.azure.com\") ||\n\t\turl.hostname.endsWith(\".ai.azure.com\");\n\tconst normalizedPath = url.pathname.replace(/\\/+$/, \"\");\n\n\t// Ensure Azure hosts have /openai/v1 as base path so the AzureOpenAI SDK\n\t// can append /deployments/<model>/... and ?api-version=v1 correctly.\n\tif (\n\t\tisAzureHost &&\n\t\t(normalizedPath === \"\" ||\n\t\t\tnormalizedPath === \"/\" ||\n\t\t\tnormalizedPath === \"/openai\" ||\n\t\t\tnormalizedPath === \"/openai/v1/responses\")\n\t) {\n\t\turl.pathname = \"/openai/v1\";\n\t\turl.search = \"\";\n\t}\n\n\treturn url.toString().replace(/\\/+$/, \"\");\n}\n\nfunction buildDefaultBaseUrl(resourceName: string): string {\n\treturn `https://${resourceName}.openai.azure.com/openai/v1`;\n}\n\nfunction resolveAzureConfig(\n\tmodel: Model<\"azure-openai-responses\">,\n\toptions?: AzureOpenAIResponsesOptions,\n): { baseUrl: string; apiVersion: string } {\n\tconst apiVersion =\n\t\toptions?.azureApiVersion ||\n\t\tgetProviderEnvValue(\"AZURE_OPENAI_API_VERSION\", options?.env) ||\n\t\tDEFAULT_AZURE_API_VERSION;\n\n\tconst baseUrl =\n\t\toptions?.azureBaseUrl?.trim() || getProviderEnvValue(\"AZURE_OPENAI_BASE_URL\", options?.env)?.trim() || undefined;\n\tconst resourceName = options?.azureResourceName || getProviderEnvValue(\"AZURE_OPENAI_RESOURCE_NAME\", options?.env);\n\n\tlet resolvedBaseUrl = baseUrl;\n\n\tif (!resolvedBaseUrl && resourceName) {\n\t\tresolvedBaseUrl = buildDefaultBaseUrl(resourceName);\n\t}\n\n\tif (!resolvedBaseUrl && model.baseUrl) {\n\t\tresolvedBaseUrl = model.baseUrl;\n\t}\n\n\tif (!resolvedBaseUrl) {\n\t\tthrow new Error(\n\t\t\t\"Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.\",\n\t\t);\n\t}\n\n\treturn {\n\t\tbaseUrl: normalizeAzureBaseUrl(resolvedBaseUrl),\n\t\tapiVersion,\n\t};\n}\n\nfunction createClient(model: Model<\"azure-openai-responses\">, apiKey: string, options?: AzureOpenAIResponsesOptions) {\n\tconst headers = { ...model.headers };\n\n\tif (options?.headers) {\n\t\tObject.assign(headers, options.headers);\n\t}\n\n\tconst { baseUrl, apiVersion } = resolveAzureConfig(model, options);\n\n\treturn new AzureOpenAI({\n\t\tapiKey,\n\t\tapiVersion,\n\t\tdangerouslyAllowBrowser: true,\n\t\tdefaultHeaders: headers,\n\t\tbaseURL: baseUrl,\n\t});\n}\n\nfunction buildParams(\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions: AzureOpenAIResponsesOptions | undefined,\n\tdeploymentName: string,\n\tgrammarToolInputProperties: ReadonlyMap<string, string> = createGrammarToolInputProperties(\n\t\tcontext.tools,\n\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t),\n) {\n\tconst requestedReasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tconst mappedReasoningEffort =\n\t\trequestedReasoningEffort === undefined ? undefined : model.thinkingLevelMap?.[requestedReasoningEffort];\n\tconst reasoningEffort = mappedReasoningEffort === undefined ? requestedReasoningEffort : mappedReasoningEffort;\n\tconst reasoningRequested = reasoningEffort !== undefined && reasoningEffort !== null;\n\tconst reasoningUnavailable = reasoningEffort === null;\n\tconst messages = convertResponsesMessages(model, context, AZURE_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t});\n\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: deploymentName,\n\t\tinput: messages,\n\t\tstream: true,\n\t\t...((options?.cacheRetention ?? model.cacheRetention) === \"none\"\n\t\t\t? {}\n\t\t\t: { prompt_cache_key: clampOpenAIPromptCacheKey(options?.sessionId) }),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (context.tools && context.tools.length > 0) {\n\t\tparams.tools = convertResponsesTools(context.tools, {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t});\n\t}\n\n\tif (model.reasoning) {\n\t\tif (reasoningRequested) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: reasoningEffort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\t...(options?.reasoningSummary === null ? {} : { summary: options?.reasoningSummary || \"auto\" }),\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (!reasoningUnavailable && model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t}\n\n\treturn params;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"azure-openai-responses.js","sourceRoot":"","sources":["../../src/api/azure-openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAU/D,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,gCAAgC,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACvH,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,yBAAyB,GAAG,IAAI,CAAC;AACvC,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,cAAc,EAAE,UAAU,EAAE,wBAAwB,CA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E,kBAAkB,IAAI,IAAI;YAC5D,0BAA0B,EAAE,KAAK,CAAC,MAAM,EAAE,0BAA0B,IAAI,KAAK;SAC7E,CAAC,CAAC;IACJ,CAAC;IAED,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;QACrB,IAAI,kBAAkB,EAAE,CAAC;YACxB,MAAM,CAAC,SAAS,GAAG;gBAClB,MAAM,EAAE,eAAiE;gBACzE,GAAG,CAAC,OAAO,EAAE,gBAAgB,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,IAAI,MAAM,EAAE,CAAC;aAC/F,CAAC;YACF,MAAM,CAAC,OAAO,GAAG,CAAC,6BAA6B,CAAC,CAAC;QAClD,CAAC;aAAM,IAAI,CAAC,oBAAoB,IAAI,KAAK,CAAC,gBAAgB,EAAE,GAAG,KAAK,IAAI,EAAE,CAAC;YAC1E,MAAM,CAAC,SAAS,GAAG;gBAClB,MAAM,EAAE,CAAC,KAAK,CAAC,gBAAgB,EAAE,GAAG,IAAI,MAAM,CAAmD;aACjG,CAAC;QACH,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { AzureOpenAI } from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel, supportsMax } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport { convertResponsesMessages, convertResponsesTools, processResponsesStream } from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nconst DEFAULT_AZURE_API_VERSION = \"v1\";\nconst AZURE_TOOL_CALL_PROVIDERS = new Set([\"openai\", \"openai-codex\", \"opencode\", \"azure-openai-responses\"]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\n\nfunction parseDeploymentNameMap(value: string | undefined): Map<string, string> {\n\tconst map = new Map<string, string>();\n\tif (!value) return map;\n\tfor (const entry of value.split(\",\")) {\n\t\tconst trimmed = entry.trim();\n\t\tif (!trimmed) continue;\n\t\tconst [modelId, deploymentName] = trimmed.split(\"=\", 2);\n\t\tif (!modelId || !deploymentName) continue;\n\t\tmap.set(modelId.trim(), deploymentName.trim());\n\t}\n\treturn map;\n}\n\nfunction resolveDeploymentName(model: Model<\"azure-openai-responses\">, options?: AzureOpenAIResponsesOptions): string {\n\tif (options?.azureDeploymentName) {\n\t\treturn options.azureDeploymentName;\n\t}\n\tconst mappedDeployment = parseDeploymentNameMap(\n\t\tgetProviderEnvValue(\"AZURE_OPENAI_DEPLOYMENT_NAME_MAP\", options?.env),\n\t).get(model.id);\n\treturn mappedDeployment || model.id;\n}\n\nfunction formatAzureOpenAIError(error: unknown): string {\n\treturn formatProviderError(normalizeProviderError(error), \"Azure OpenAI API error\");\n}\n\n// Azure OpenAI Responses-specific options\nexport interface AzureOpenAIResponsesOptions extends StreamOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n\tazureApiVersion?: string;\n\tazureResourceName?: string;\n\tazureBaseUrl?: string;\n\tazureDeploymentName?: string;\n}\n\n/**\n * Generate function for Azure OpenAI Responses API\n */\nexport const stream: StreamFunction<\"azure-openai-responses\", AzureOpenAIResponsesOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions?: AzureOpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t// Start async processing\n\t(async () => {\n\t\tconst deploymentName = resolveDeploymentName(model, options);\n\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"azure-openai-responses\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"pending\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create Azure OpenAI client\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\t\t\tconst client = createClient(model, apiKey, options);\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tcontext.tools,\n\t\t\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t\t);\n\t\t\tlet params = buildParams(model, context, options, deploymentName, grammarToolInputProperties);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, { grammarToolInputProperties });\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"pending\") {\n\t\t\t\tthrow new Error(\"Azure OpenAI Responses stream ended without a stop reason\");\n\t\t\t}\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tfor (const block of output.content) {\n\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t// Streaming scratch buffers are only used during parsing; never persist them.\n\t\t\t\tdelete (block as { partialJson?: string }).partialJson;\n\t\t\t\tdelete (block as { customInput?: unknown }).customInput;\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatAzureOpenAIError(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"azure-openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey;\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = buildBaseOptions(model, context, options, apiKey);\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort =\n\t\tclampedReasoning === \"off\"\n\t\t\t? undefined\n\t\t\t: clampedReasoning === \"max\" && supportsMax(model)\n\t\t\t\t? \"max\"\n\t\t\t\t: clampedReasoning === \"max\"\n\t\t\t\t\t? \"high\"\n\t\t\t\t\t: clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies AzureOpenAIResponsesOptions);\n};\n\nfunction normalizeAzureBaseUrl(baseUrl: string): string {\n\tconst trimmed = baseUrl.trim().replace(/\\/+$/, \"\");\n\tlet url: URL;\n\ttry {\n\t\turl = new URL(trimmed);\n\t} catch {\n\t\tthrow new Error(`Invalid Azure OpenAI base URL: ${baseUrl}`);\n\t}\n\n\tconst isAzureHost =\n\t\turl.hostname.endsWith(\".openai.azure.com\") ||\n\t\turl.hostname.endsWith(\".cognitiveservices.azure.com\") ||\n\t\turl.hostname.endsWith(\".ai.azure.com\");\n\tconst normalizedPath = url.pathname.replace(/\\/+$/, \"\");\n\n\t// Ensure Azure hosts have /openai/v1 as base path so the AzureOpenAI SDK\n\t// can append /deployments/<model>/... and ?api-version=v1 correctly.\n\tif (\n\t\tisAzureHost &&\n\t\t(normalizedPath === \"\" ||\n\t\t\tnormalizedPath === \"/\" ||\n\t\t\tnormalizedPath === \"/openai\" ||\n\t\t\tnormalizedPath === \"/openai/v1/responses\")\n\t) {\n\t\turl.pathname = \"/openai/v1\";\n\t\turl.search = \"\";\n\t}\n\n\treturn url.toString().replace(/\\/+$/, \"\");\n}\n\nfunction buildDefaultBaseUrl(resourceName: string): string {\n\treturn `https://${resourceName}.openai.azure.com/openai/v1`;\n}\n\nfunction resolveAzureConfig(\n\tmodel: Model<\"azure-openai-responses\">,\n\toptions?: AzureOpenAIResponsesOptions,\n): { baseUrl: string; apiVersion: string } {\n\tconst apiVersion =\n\t\toptions?.azureApiVersion ||\n\t\tgetProviderEnvValue(\"AZURE_OPENAI_API_VERSION\", options?.env) ||\n\t\tDEFAULT_AZURE_API_VERSION;\n\n\tconst baseUrl =\n\t\toptions?.azureBaseUrl?.trim() || getProviderEnvValue(\"AZURE_OPENAI_BASE_URL\", options?.env)?.trim() || undefined;\n\tconst resourceName = options?.azureResourceName || getProviderEnvValue(\"AZURE_OPENAI_RESOURCE_NAME\", options?.env);\n\n\tlet resolvedBaseUrl = baseUrl;\n\n\tif (!resolvedBaseUrl && resourceName) {\n\t\tresolvedBaseUrl = buildDefaultBaseUrl(resourceName);\n\t}\n\n\tif (!resolvedBaseUrl && model.baseUrl) {\n\t\tresolvedBaseUrl = model.baseUrl;\n\t}\n\n\tif (!resolvedBaseUrl) {\n\t\tthrow new Error(\n\t\t\t\"Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.\",\n\t\t);\n\t}\n\n\treturn {\n\t\tbaseUrl: normalizeAzureBaseUrl(resolvedBaseUrl),\n\t\tapiVersion,\n\t};\n}\n\nfunction createClient(model: Model<\"azure-openai-responses\">, apiKey: string, options?: AzureOpenAIResponsesOptions) {\n\tconst headers = { ...model.headers };\n\n\tif (options?.headers) {\n\t\tObject.assign(headers, options.headers);\n\t}\n\n\tconst { baseUrl, apiVersion } = resolveAzureConfig(model, options);\n\n\treturn new AzureOpenAI({\n\t\tapiKey,\n\t\tapiVersion,\n\t\tdangerouslyAllowBrowser: true,\n\t\tfetch: options?.fetch,\n\t\tdefaultHeaders: headers,\n\t\tbaseURL: baseUrl,\n\t});\n}\n\nfunction buildParams(\n\tmodel: Model<\"azure-openai-responses\">,\n\tcontext: Context,\n\toptions: AzureOpenAIResponsesOptions | undefined,\n\tdeploymentName: string,\n\tgrammarToolInputProperties: ReadonlyMap<string, string> = createGrammarToolInputProperties(\n\t\tcontext.tools,\n\t\tmodel.compat?.supportsOpenAIGrammarTools ?? false,\n\t),\n) {\n\tconst requestedReasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tconst mappedReasoningEffort =\n\t\trequestedReasoningEffort === undefined ? undefined : model.thinkingLevelMap?.[requestedReasoningEffort];\n\tconst reasoningEffort = mappedReasoningEffort === undefined ? requestedReasoningEffort : mappedReasoningEffort;\n\tconst reasoningRequested = reasoningEffort !== undefined && reasoningEffort !== null;\n\tconst reasoningUnavailable = reasoningEffort === null;\n\tconst messages = convertResponsesMessages(model, context, AZURE_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t});\n\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: deploymentName,\n\t\tinput: messages,\n\t\tstream: true,\n\t\t...((options?.cacheRetention ?? model.cacheRetention) === \"none\"\n\t\t\t? {}\n\t\t\t: { prompt_cache_key: clampOpenAIPromptCacheKey(options?.sessionId) }),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (context.tools && context.tools.length > 0) {\n\t\tparams.tools = convertResponsesTools(context.tools, {\n\t\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? true,\n\t\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\t});\n\t}\n\n\tif (model.reasoning) {\n\t\tif (reasoningRequested) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: reasoningEffort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\t...(options?.reasoningSummary === null ? {} : { summary: options?.reasoningSummary || \"auto\" }),\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (!reasoningUnavailable && model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t}\n\n\treturn params;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAQX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EAEf,aAAa,EAIb,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAQX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EAEf,aAAa,EAIb,MAAM,aAAa,CAAC;AAyBrB,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,MAAM,sBAAsB,GAAG,YAAY,GAAG,SAAS,CAAC;AAE9D,MAAM,WAAW,cAAe,SAAQ,aAAa;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAEtE,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC;;;sGAGkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC;;;;yGAIqG;IACrG,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAMD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,yBAAyB,EAAE,cAAc,CAgO5E,CAAC;AA8IF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,yBAAyB,EAAE,mBAAmB,CA8CvF,CAAC"}
|
|
@@ -3,6 +3,7 @@ import { NodeHttpHandler } from "@smithy/node-http-handler";
|
|
|
3
3
|
import { HttpProxyAgent } from "http-proxy-agent";
|
|
4
4
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
5
5
|
import { calculateCost } from "../models.js";
|
|
6
|
+
import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
|
|
6
7
|
import { normalizeProviderError } from "../utils/error-body.js";
|
|
7
8
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
8
9
|
import { providerHeadersToRecord } from "../utils/headers.js";
|
|
@@ -34,12 +35,18 @@ export const stream = (model, context, options = {}) => {
|
|
|
34
35
|
totalTokens: 0,
|
|
35
36
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
36
37
|
},
|
|
37
|
-
stopReason: "
|
|
38
|
+
stopReason: "pending",
|
|
38
39
|
timestamp: Date.now(),
|
|
39
40
|
};
|
|
40
41
|
const blocks = output.content;
|
|
42
|
+
// A profile explicitly configured through pi's auth flow (the `profile`
|
|
43
|
+
// option or scoped `AWS_PROFILE` on the stored credential's env) must win
|
|
44
|
+
// over ambient AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY. The SDK default
|
|
45
|
+
// chain already prefers a configured profile over env keys, but only when
|
|
46
|
+
// `credentials` is not set on the client config. See #6957.
|
|
47
|
+
const optionsProfile = options.profile || options.env?.AWS_PROFILE;
|
|
41
48
|
const config = {
|
|
42
|
-
profile:
|
|
49
|
+
profile: optionsProfile || getProviderEnvValue("AWS_PROFILE", options.env),
|
|
43
50
|
};
|
|
44
51
|
const configuredRegion = getConfiguredBedrockRegion(options);
|
|
45
52
|
const hasAmbientConfiguredProfile = Boolean(getProviderEnvValue("AWS_PROFILE"));
|
|
@@ -84,7 +91,7 @@ export const stream = (model, context, options = {}) => {
|
|
|
84
91
|
};
|
|
85
92
|
}
|
|
86
93
|
const credentials = getConfiguredBedrockCredentials(options.env);
|
|
87
|
-
if (!skipAuth && credentials) {
|
|
94
|
+
if (!skipAuth && credentials && !optionsProfile) {
|
|
88
95
|
config.credentials = credentials;
|
|
89
96
|
}
|
|
90
97
|
const proxyUrl = resolveHttpProxyUrlForTarget(model.baseUrl, options.env);
|
|
@@ -112,6 +119,9 @@ export const stream = (model, context, options = {}) => {
|
|
|
112
119
|
config.token = { token: bearerToken };
|
|
113
120
|
config.authSchemePreference = ["httpBearerAuth"];
|
|
114
121
|
}
|
|
122
|
+
// Kept outside the try so the catch can still correlate a mid-stream failure:
|
|
123
|
+
// exceptions delivered as stream events carry no HTTP metadata of their own.
|
|
124
|
+
let responseRequestId;
|
|
115
125
|
try {
|
|
116
126
|
const client = new BedrockRuntimeClient(config);
|
|
117
127
|
const customHeaders = providerHeadersToRecord(options.headers);
|
|
@@ -142,6 +152,7 @@ export const stream = (model, context, options = {}) => {
|
|
|
142
152
|
}
|
|
143
153
|
const command = new ConverseStreamCommand(commandInput);
|
|
144
154
|
const response = await client.send(command, { abortSignal: options.signal });
|
|
155
|
+
responseRequestId = normalizeDiagnosticValue(response.$metadata.requestId);
|
|
145
156
|
if (response.$metadata.httpStatusCode !== undefined) {
|
|
146
157
|
const responseHeaders = {};
|
|
147
158
|
if (response.$metadata.requestId) {
|
|
@@ -166,6 +177,7 @@ export const stream = (model, context, options = {}) => {
|
|
|
166
177
|
handleContentBlockStop(item.contentBlockStop, blocks, output, stream);
|
|
167
178
|
}
|
|
168
179
|
else if (item.messageStop) {
|
|
180
|
+
output.rawStopReason = item.messageStop.stopReason;
|
|
169
181
|
const { stopReason, errorMessage } = mapStopReason(item.messageStop.stopReason);
|
|
170
182
|
output.stopReason = stopReason;
|
|
171
183
|
if (errorMessage) {
|
|
@@ -194,6 +206,9 @@ export const stream = (model, context, options = {}) => {
|
|
|
194
206
|
if (options.signal?.aborted) {
|
|
195
207
|
throw new Error("Request was aborted");
|
|
196
208
|
}
|
|
209
|
+
if (output.stopReason === "pending") {
|
|
210
|
+
throw new Error("Bedrock stream ended without a stop reason");
|
|
211
|
+
}
|
|
197
212
|
if (output.stopReason === "error" || output.stopReason === "aborted") {
|
|
198
213
|
throw new Error(output.errorMessage || "An unknown error occurred");
|
|
199
214
|
}
|
|
@@ -208,6 +223,9 @@ export const stream = (model, context, options = {}) => {
|
|
|
208
223
|
}
|
|
209
224
|
output.stopReason = options.signal?.aborted ? "aborted" : "error";
|
|
210
225
|
output.errorMessage = formatBedrockError(error);
|
|
226
|
+
if (output.stopReason === "error") {
|
|
227
|
+
appendBedrockFailureDiagnostic(output, error, responseRequestId);
|
|
228
|
+
}
|
|
211
229
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
212
230
|
stream.end();
|
|
213
231
|
}
|
|
@@ -257,6 +275,46 @@ function formatBedrockError(error) {
|
|
|
257
275
|
}
|
|
258
276
|
return `${core}${dataRetentionHint}`;
|
|
259
277
|
}
|
|
278
|
+
/** Over-long header values are dropped rather than truncated: a truncated request id is not a request id. */
|
|
279
|
+
const MAX_BEDROCK_DIAGNOSTIC_VALUE_CHARS = 200;
|
|
280
|
+
function normalizeDiagnosticValue(value) {
|
|
281
|
+
if (typeof value !== "string")
|
|
282
|
+
return undefined;
|
|
283
|
+
const trimmed = value.trim();
|
|
284
|
+
if (trimmed.length === 0 || trimmed.length > MAX_BEDROCK_DIAGNOSTIC_VALUE_CHARS)
|
|
285
|
+
return undefined;
|
|
286
|
+
return trimmed;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* The SDK puts the modeled code on `error.name` for service exceptions and unmodeled stream errors alike, so
|
|
290
|
+
* do not narrow to `BedrockRuntimeServiceException`. Modeled Bedrock errors all end in `Exception`, unlike
|
|
291
|
+
* transport names such as `TimeoutError`.
|
|
292
|
+
*/
|
|
293
|
+
function extractBedrockErrorCode(error) {
|
|
294
|
+
if (!(error instanceof Error) || !error.name.endsWith("Exception"))
|
|
295
|
+
return undefined;
|
|
296
|
+
return normalizeDiagnosticValue(error.name);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Structured metadata alongside `errorMessage`, which stays byte-identical because `isRetryableAssistantError`
|
|
300
|
+
* matches against it. Unknown fields are omitted, never guessed: a modeled mid-stream exception reaches us as
|
|
301
|
+
* a bare object literal, leaving only `fallbackRequestId`. `details` only, as the throw is not always `Error`.
|
|
302
|
+
*/
|
|
303
|
+
function appendBedrockFailureDiagnostic(output, error, fallbackRequestId) {
|
|
304
|
+
const metadata = error?.$metadata;
|
|
305
|
+
const details = {};
|
|
306
|
+
if (typeof metadata?.httpStatusCode === "number")
|
|
307
|
+
details.status = metadata.httpStatusCode;
|
|
308
|
+
const errorCode = extractBedrockErrorCode(error);
|
|
309
|
+
if (errorCode !== undefined)
|
|
310
|
+
details.errorCode = errorCode;
|
|
311
|
+
const requestId = normalizeDiagnosticValue(metadata?.requestId) ?? fallbackRequestId;
|
|
312
|
+
if (requestId !== undefined)
|
|
313
|
+
details.requestId = requestId;
|
|
314
|
+
if (Object.keys(details).length === 0)
|
|
315
|
+
return;
|
|
316
|
+
appendAssistantMessageDiagnostic(output, { type: "bedrock_response_failure", timestamp: Date.now(), details });
|
|
317
|
+
}
|
|
260
318
|
/**
|
|
261
319
|
* Header keys that must never be overwritten by caller-supplied headers.
|
|
262
320
|
* `host` and `x-amz-*` participate in the SigV4 canonical request; `authorization`
|
|
@@ -765,7 +823,9 @@ function mapStopReason(reason) {
|
|
|
765
823
|
case BedrockStopReason.TOOL_USE:
|
|
766
824
|
return { stopReason: "toolUse" };
|
|
767
825
|
default:
|
|
768
|
-
return reason
|
|
826
|
+
return reason
|
|
827
|
+
? { stopReason: "error", errorMessage: `Provider stopped with: ${reason}` }
|
|
828
|
+
: { stopReason: "error" };
|
|
769
829
|
}
|
|
770
830
|
}
|
|
771
831
|
function getConfiguredBedrockRegion(options) {
|