@draht/ai 2026.4.26 → 2026.6.11
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/api-registry.d.ts +1 -1
- package/dist/api-registry.d.ts.map +1 -1
- package/dist/api-registry.js.map +1 -1
- package/dist/bedrock-provider.d.ts +2 -2
- package/dist/bedrock-provider.d.ts.map +1 -1
- package/dist/bedrock-provider.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -0
- package/dist/cli.js.map +1 -1
- package/dist/env-api-keys.d.ts +10 -1
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +110 -36
- package/dist/env-api-keys.js.map +1 -1
- package/dist/image-models.d.ts +10 -0
- package/dist/image-models.d.ts.map +1 -0
- package/dist/image-models.generated.d.ts +485 -0
- package/dist/image-models.generated.d.ts.map +1 -0
- package/dist/image-models.generated.js +487 -0
- package/dist/image-models.generated.js.map +1 -0
- package/dist/image-models.js +23 -0
- package/dist/image-models.js.map +1 -0
- package/dist/images-api-registry.d.ts +14 -0
- package/dist/images-api-registry.d.ts.map +1 -0
- package/dist/images-api-registry.js +22 -0
- package/dist/images-api-registry.js.map +1 -0
- package/dist/images.d.ts +4 -0
- package/dist/images.d.ts.map +1 -0
- package/dist/images.js +14 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +31 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +5 -8
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +5197 -1721
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +7156 -5016
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +33 -6
- package/dist/models.js.map +1 -1
- package/dist/oauth.d.ts +1 -1
- package/dist/oauth.d.ts.map +1 -1
- package/dist/oauth.js.map +1 -1
- package/dist/providers/amazon-bedrock.d.ts +19 -1
- package/dist/providers/amazon-bedrock.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock.js +278 -89
- package/dist/providers/amazon-bedrock.js.map +1 -1
- package/dist/providers/anthropic.d.ts +37 -6
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +300 -114
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/azure-openai-responses.d.ts +1 -1
- package/dist/providers/azure-openai-responses.d.ts.map +1 -1
- package/dist/providers/azure-openai-responses.js +68 -21
- package/dist/providers/azure-openai-responses.js.map +1 -1
- package/dist/providers/cloudflare.d.ts +13 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +26 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/faux.d.ts +1 -1
- package/dist/providers/faux.d.ts.map +1 -1
- package/dist/providers/faux.js +1 -0
- package/dist/providers/faux.js.map +1 -1
- package/dist/providers/github-copilot-headers.d.ts +1 -1
- package/dist/providers/github-copilot-headers.d.ts.map +1 -1
- package/dist/providers/github-copilot-headers.js.map +1 -1
- package/dist/providers/google-shared.d.ts +8 -3
- package/dist/providers/google-shared.d.ts.map +1 -1
- package/dist/providers/google-shared.js +34 -17
- package/dist/providers/google-shared.js.map +1 -1
- package/dist/providers/google-vertex.d.ts +2 -2
- package/dist/providers/google-vertex.d.ts.map +1 -1
- package/dist/providers/google-vertex.js +45 -18
- package/dist/providers/google-vertex.js.map +1 -1
- package/dist/providers/google.d.ts +2 -2
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +9 -6
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/images/openrouter.d.ts +3 -0
- package/dist/providers/images/openrouter.d.ts.map +1 -0
- package/dist/providers/images/openrouter.js +128 -0
- package/dist/providers/images/openrouter.js.map +1 -0
- package/dist/providers/images/register-builtins.d.ts +4 -0
- package/dist/providers/images/register-builtins.d.ts.map +1 -0
- package/dist/providers/images/register-builtins.js +34 -0
- package/dist/providers/images/register-builtins.js.map +1 -0
- package/dist/providers/mistral.d.ts +4 -1
- package/dist/providers/mistral.d.ts.map +1 -1
- package/dist/providers/mistral.js +43 -10
- package/dist/providers/mistral.js.map +1 -1
- package/dist/providers/openai-codex-responses.d.ts +22 -1
- package/dist/providers/openai-codex-responses.d.ts.map +1 -1
- package/dist/providers/openai-codex-responses.js +542 -111
- package/dist/providers/openai-codex-responses.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +6 -2
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +446 -227
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-prompt-cache.d.ts +3 -0
- package/dist/providers/openai-prompt-cache.d.ts.map +1 -0
- package/dist/providers/openai-prompt-cache.js +10 -0
- package/dist/providers/openai-prompt-cache.js.map +1 -0
- package/dist/providers/openai-responses-shared.d.ts +3 -2
- package/dist/providers/openai-responses-shared.d.ts.map +1 -1
- package/dist/providers/openai-responses-shared.js +41 -15
- package/dist/providers/openai-responses-shared.js.map +1 -1
- package/dist/providers/openai-responses.d.ts +1 -1
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +85 -40
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/register-builtins.d.ts +10 -13
- package/dist/providers/register-builtins.d.ts.map +1 -1
- package/dist/providers/register-builtins.js +13 -20
- package/dist/providers/register-builtins.js.map +1 -1
- package/dist/providers/simple-options.d.ts +2 -2
- package/dist/providers/simple-options.d.ts.map +1 -1
- package/dist/providers/simple-options.js +8 -2
- package/dist/providers/simple-options.js.map +1 -1
- package/dist/providers/transform-messages.d.ts +1 -1
- package/dist/providers/transform-messages.d.ts.map +1 -1
- package/dist/providers/transform-messages.js +63 -34
- package/dist/providers/transform-messages.js.map +1 -1
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/stream.d.ts +3 -3
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +14 -2
- package/dist/stream.js.map +1 -1
- package/dist/types.d.ts +177 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/abort-signals.d.ts +6 -0
- package/dist/utils/abort-signals.d.ts.map +1 -0
- package/dist/utils/abort-signals.js +34 -0
- package/dist/utils/abort-signals.js.map +1 -0
- package/dist/utils/diagnostics.d.ts +19 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/event-stream.d.ts +3 -3
- package/dist/utils/event-stream.d.ts.map +1 -1
- package/dist/utils/event-stream.js +2 -2
- package/dist/utils/event-stream.js.map +1 -1
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +8 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/json-parse.d.ts +8 -1
- package/dist/utils/json-parse.d.ts.map +1 -1
- package/dist/utils/json-parse.js +89 -5
- package/dist/utils/json-parse.js.map +1 -1
- package/dist/utils/node-http-proxy.d.ts +10 -0
- package/dist/utils/node-http-proxy.d.ts.map +1 -0
- package/dist/utils/node-http-proxy.js +97 -0
- package/dist/utils/node-http-proxy.js.map +1 -0
- package/dist/utils/oauth/anthropic.d.ts +1 -1
- package/dist/utils/oauth/anthropic.d.ts.map +1 -1
- package/dist/utils/oauth/anthropic.js +1 -1
- package/dist/utils/oauth/anthropic.js.map +1 -1
- package/dist/utils/oauth/device-code.d.ts +21 -0
- package/dist/utils/oauth/device-code.d.ts.map +1 -0
- package/dist/utils/oauth/device-code.js +56 -0
- package/dist/utils/oauth/device-code.js.map +1 -0
- package/dist/utils/oauth/github-copilot.d.ts +3 -3
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -1
- package/dist/utils/oauth/github-copilot.js +58 -70
- package/dist/utils/oauth/github-copilot.js.map +1 -1
- package/dist/utils/oauth/index.d.ts +8 -11
- package/dist/utils/oauth/index.d.ts.map +1 -1
- package/dist/utils/oauth/index.js +2 -11
- package/dist/utils/oauth/index.js.map +1 -1
- package/dist/utils/oauth/openai-codex.d.ts +11 -2
- package/dist/utils/oauth/openai-codex.d.ts.map +1 -1
- package/dist/utils/oauth/openai-codex.js +187 -73
- package/dist/utils/oauth/openai-codex.js.map +1 -1
- package/dist/utils/oauth/types.d.ts +18 -1
- package/dist/utils/oauth/types.d.ts.map +1 -1
- package/dist/utils/oauth/types.js.map +1 -1
- package/dist/utils/overflow.d.ts +7 -3
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +25 -3
- package/dist/utils/overflow.js.map +1 -1
- package/dist/utils/typebox-helpers.d.ts +1 -1
- package/dist/utils/typebox-helpers.d.ts.map +1 -1
- package/dist/utils/typebox-helpers.js +1 -1
- package/dist/utils/typebox-helpers.js.map +1 -1
- package/dist/utils/validation.d.ts +1 -1
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +242 -41
- package/dist/utils/validation.js.map +1 -1
- package/package.json +15 -16
- package/dist/providers/google-gemini-cli.d.ts +0 -74
- package/dist/providers/google-gemini-cli.d.ts.map +0 -1
- package/dist/providers/google-gemini-cli.js +0 -776
- package/dist/providers/google-gemini-cli.js.map +0 -1
|
@@ -1,3 +1,41 @@
|
|
|
1
|
+
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
2
|
+
const NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
3
|
+
function replaceImagesWithPlaceholder(content, placeholder) {
|
|
4
|
+
const result = [];
|
|
5
|
+
let previousWasPlaceholder = false;
|
|
6
|
+
for (const block of content) {
|
|
7
|
+
if (block.type === "image") {
|
|
8
|
+
if (!previousWasPlaceholder) {
|
|
9
|
+
result.push({ type: "text", text: placeholder });
|
|
10
|
+
}
|
|
11
|
+
previousWasPlaceholder = true;
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
result.push(block);
|
|
15
|
+
previousWasPlaceholder = block.text === placeholder;
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
function downgradeUnsupportedImages(messages, model) {
|
|
20
|
+
if (model.input.includes("image")) {
|
|
21
|
+
return messages;
|
|
22
|
+
}
|
|
23
|
+
return messages.map((msg) => {
|
|
24
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
25
|
+
return {
|
|
26
|
+
...msg,
|
|
27
|
+
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (msg.role === "toolResult") {
|
|
31
|
+
return {
|
|
32
|
+
...msg,
|
|
33
|
+
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return msg;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
1
39
|
/**
|
|
2
40
|
* Normalize tool call ID for cross-provider compatibility.
|
|
3
41
|
* OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.
|
|
@@ -6,8 +44,9 @@
|
|
|
6
44
|
export function transformMessages(messages, model, normalizeToolCallId) {
|
|
7
45
|
// Build a map of original tool call IDs to normalized IDs
|
|
8
46
|
const toolCallIdMap = new Map();
|
|
9
|
-
|
|
10
|
-
|
|
47
|
+
const imageAwareMessages = downgradeUnsupportedImages(messages, model);
|
|
48
|
+
// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)
|
|
49
|
+
const transformed = imageAwareMessages.map((msg) => {
|
|
11
50
|
// User messages pass through unchanged
|
|
12
51
|
if (msg.role === "user") {
|
|
13
52
|
return msg;
|
|
@@ -85,26 +124,29 @@ export function transformMessages(messages, model, normalizeToolCallId) {
|
|
|
85
124
|
const result = [];
|
|
86
125
|
let pendingToolCalls = [];
|
|
87
126
|
let existingToolResultIds = new Set();
|
|
127
|
+
const insertSyntheticToolResults = () => {
|
|
128
|
+
if (pendingToolCalls.length > 0) {
|
|
129
|
+
for (const tc of pendingToolCalls) {
|
|
130
|
+
if (!existingToolResultIds.has(tc.id)) {
|
|
131
|
+
result.push({
|
|
132
|
+
role: "toolResult",
|
|
133
|
+
toolCallId: tc.id,
|
|
134
|
+
toolName: tc.name,
|
|
135
|
+
content: [{ type: "text", text: "No result provided" }],
|
|
136
|
+
isError: true,
|
|
137
|
+
timestamp: Date.now(),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
pendingToolCalls = [];
|
|
142
|
+
existingToolResultIds = new Set();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
88
145
|
for (let i = 0; i < transformed.length; i++) {
|
|
89
146
|
const msg = transformed[i];
|
|
90
147
|
if (msg.role === "assistant") {
|
|
91
148
|
// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now
|
|
92
|
-
|
|
93
|
-
for (const tc of pendingToolCalls) {
|
|
94
|
-
if (!existingToolResultIds.has(tc.id)) {
|
|
95
|
-
result.push({
|
|
96
|
-
role: "toolResult",
|
|
97
|
-
toolCallId: tc.id,
|
|
98
|
-
toolName: tc.name,
|
|
99
|
-
content: [{ type: "text", text: "No result provided" }],
|
|
100
|
-
isError: true,
|
|
101
|
-
timestamp: Date.now(),
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
pendingToolCalls = [];
|
|
106
|
-
existingToolResultIds = new Set();
|
|
107
|
-
}
|
|
149
|
+
insertSyntheticToolResults();
|
|
108
150
|
// Skip errored/aborted assistant messages entirely.
|
|
109
151
|
// These are incomplete turns that shouldn't be replayed:
|
|
110
152
|
// - May have partial content (reasoning without message, incomplete tool calls)
|
|
@@ -128,28 +170,15 @@ export function transformMessages(messages, model, normalizeToolCallId) {
|
|
|
128
170
|
}
|
|
129
171
|
else if (msg.role === "user") {
|
|
130
172
|
// User message interrupts tool flow - insert synthetic results for orphaned calls
|
|
131
|
-
|
|
132
|
-
for (const tc of pendingToolCalls) {
|
|
133
|
-
if (!existingToolResultIds.has(tc.id)) {
|
|
134
|
-
result.push({
|
|
135
|
-
role: "toolResult",
|
|
136
|
-
toolCallId: tc.id,
|
|
137
|
-
toolName: tc.name,
|
|
138
|
-
content: [{ type: "text", text: "No result provided" }],
|
|
139
|
-
isError: true,
|
|
140
|
-
timestamp: Date.now(),
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
pendingToolCalls = [];
|
|
145
|
-
existingToolResultIds = new Set();
|
|
146
|
-
}
|
|
173
|
+
insertSyntheticToolResults();
|
|
147
174
|
result.push(msg);
|
|
148
175
|
}
|
|
149
176
|
else {
|
|
150
177
|
result.push(msg);
|
|
151
178
|
}
|
|
152
179
|
}
|
|
180
|
+
// If the conversation ends with unresolved tool calls, synthesize results now.
|
|
181
|
+
insertSyntheticToolResults();
|
|
153
182
|
return result;
|
|
154
183
|
}
|
|
155
184
|
//# sourceMappingURL=transform-messages.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAChC,QAAmB,EACnB,KAAkB,EAClB,mBAA0F,EAC9E;IACZ,0DAA0D;IAC1D,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEhD,+EAA+E;IAC/E,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC;QACzC,uCAAuC;QACvC,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YACzB,OAAO,GAAG,CAAC;QACZ,CAAC;QAED,yEAAyE;QACzE,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YAC/B,MAAM,YAAY,GAAG,aAAa,CAAC,GAAG,CA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type { Api, AssistantMessage, Message, Model, ToolCall, ToolResultMessage } from \"../types.js\";\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\n\t// First pass: transform messages (thinking blocks, tool call ID normalization)\n\tconst transformed = messages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tif (pendingToolCalls.length > 0) {\n\t\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\t\tresult.push({\n\t\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"No result provided\" }],\n\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpendingToolCalls = [];\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tif (pendingToolCalls.length > 0) {\n\t\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\t\tresult.push({\n\t\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"No result provided\" }],\n\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpendingToolCalls = [];\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\treturn result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAWA,MAAM,iCAAiC,GAAG,gDAAgD,CAAC;AAC3F,MAAM,iCAAiC,GAAG,qDAAqD,CAAC;AAEhG,SAAS,4BAA4B,CAAC,OAAuC,EAAE,WAAmB,EAAiB;IAClH,MAAM,MAAM,GAAkB,EAAE,CAAC;IACjC,IAAI,sBAAsB,GAAG,KAAK,CAAC;IAEnC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAC5B,IAAI,CAAC,sBAAsB,EAAE,CAAC;gBAC7B,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;YAClD,CAAC;YACD,sBAAsB,GAAG,IAAI,CAAC;YAC9B,SAAS;QACV,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,sBA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F,SAAS;YACV,CAAC;YAED,+CAA+C;YAC/C,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAe,CAAC;YAC1F,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC1B,gBAAgB,GAAG,SAAS,CAAC;gBAC7B,qBAAqB,GAAG,IAAI,GAAG,EAAE,CAAC;YACnC,CAAC;YAED,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YACtC,qBAAqB,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAChC,kFAAkF;YAClF,0BAA0B,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,+EAA+E;IAC/E,0BAA0B,EAAE,CAAC;IAE7B,OAAO,MAAM,CAAC;AAAA,CACd","sourcesContent":["import type {\n\tApi,\n\tAssistantMessage,\n\tImageContent,\n\tMessage,\n\tModel,\n\tTextContent,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.ts\";\n\nconst NON_VISION_USER_IMAGE_PLACEHOLDER = \"(image omitted: model does not support images)\";\nconst NON_VISION_TOOL_IMAGE_PLACEHOLDER = \"(tool image omitted: model does not support images)\";\n\nfunction replaceImagesWithPlaceholder(content: (TextContent | ImageContent)[], placeholder: string): TextContent[] {\n\tconst result: TextContent[] = [];\n\tlet previousWasPlaceholder = false;\n\n\tfor (const block of content) {\n\t\tif (block.type === \"image\") {\n\t\t\tif (!previousWasPlaceholder) {\n\t\t\t\tresult.push({ type: \"text\", text: placeholder });\n\t\t\t}\n\t\t\tpreviousWasPlaceholder = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tresult.push(block);\n\t\tpreviousWasPlaceholder = block.text === placeholder;\n\t}\n\n\treturn result;\n}\n\nfunction downgradeUnsupportedImages<TApi extends Api>(messages: Message[], model: Model<TApi>): Message[] {\n\tif (model.input.includes(\"image\")) {\n\t\treturn messages;\n\t}\n\n\treturn messages.map((msg) => {\n\t\tif (msg.role === \"user\" && Array.isArray(msg.content)) {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\tif (msg.role === \"toolResult\") {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\treturn msg;\n\t});\n}\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\tconst imageAwareMessages = downgradeUnsupportedImages(messages, model);\n\n\t// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)\n\tconst transformed = imageAwareMessages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\tconst insertSyntheticToolResults = () => {\n\t\tif (pendingToolCalls.length > 0) {\n\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"No result provided\" }],\n\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t}\n\t\t\t}\n\t\t\tpendingToolCalls = [];\n\t\t\texistingToolResultIds = new Set();\n\t\t}\n\t};\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tinsertSyntheticToolResults();\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tinsertSyntheticToolResults();\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\t// If the conversation ends with unresolved tool calls, synthesize results now.\n\tinsertSyntheticToolResults();\n\n\treturn result;\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type SessionResourceCleanup = (sessionId?: string) => void;
|
|
2
|
+
export declare function registerSessionResourceCleanup(cleanup: SessionResourceCleanup): () => void;
|
|
3
|
+
export declare function cleanupSessionResources(sessionId?: string): void;
|
|
4
|
+
//# sourceMappingURL=session-resources.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-resources.d.ts","sourceRoot":"","sources":["../src/session-resources.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,sBAAsB,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;AAIlE,wBAAgB,8BAA8B,CAAC,OAAO,EAAE,sBAAsB,GAAG,MAAM,IAAI,CAK1F;AAED,wBAAgB,uBAAuB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAYhE","sourcesContent":["export type SessionResourceCleanup = (sessionId?: string) => void;\n\nconst sessionResourceCleanups = new Set<SessionResourceCleanup>();\n\nexport function registerSessionResourceCleanup(cleanup: SessionResourceCleanup): () => void {\n\tsessionResourceCleanups.add(cleanup);\n\treturn () => {\n\t\tsessionResourceCleanups.delete(cleanup);\n\t};\n}\n\nexport function cleanupSessionResources(sessionId?: string): void {\n\tconst errors: unknown[] = [];\n\tfor (const cleanup of sessionResourceCleanups) {\n\t\ttry {\n\t\t\tcleanup(sessionId);\n\t\t} catch (error) {\n\t\t\terrors.push(error);\n\t\t}\n\t}\n\tif (errors.length > 0) {\n\t\tthrow new AggregateError(errors, \"Failed to cleanup session resources\");\n\t}\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const sessionResourceCleanups = new Set();
|
|
2
|
+
export function registerSessionResourceCleanup(cleanup) {
|
|
3
|
+
sessionResourceCleanups.add(cleanup);
|
|
4
|
+
return () => {
|
|
5
|
+
sessionResourceCleanups.delete(cleanup);
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function cleanupSessionResources(sessionId) {
|
|
9
|
+
const errors = [];
|
|
10
|
+
for (const cleanup of sessionResourceCleanups) {
|
|
11
|
+
try {
|
|
12
|
+
cleanup(sessionId);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
errors.push(error);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (errors.length > 0) {
|
|
19
|
+
throw new AggregateError(errors, "Failed to cleanup session resources");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=session-resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-resources.js","sourceRoot":"","sources":["../src/session-resources.ts"],"names":[],"mappings":"AAEA,MAAM,uBAAuB,GAAG,IAAI,GAAG,EAA0B,CAAC;AAElE,MAAM,UAAU,8BAA8B,CAAC,OAA+B,EAAc;IAC3F,uBAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrC,OAAO,GAAG,EAAE,CAAC;QACZ,uBAAuB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAAA,CACxC,CAAC;AAAA,CACF;AAED,MAAM,UAAU,uBAAuB,CAAC,SAAkB,EAAQ;IACjE,MAAM,MAAM,GAAc,EAAE,CAAC;IAC7B,KAAK,MAAM,OAAO,IAAI,uBAAuB,EAAE,CAAC;QAC/C,IAAI,CAAC;YACJ,OAAO,CAAC,SAAS,CAAC,CAAC;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACF,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,cAAc,CAAC,MAAM,EAAE,qCAAqC,CAAC,CAAC;IACzE,CAAC;AAAA,CACD","sourcesContent":["export type SessionResourceCleanup = (sessionId?: string) => void;\n\nconst sessionResourceCleanups = new Set<SessionResourceCleanup>();\n\nexport function registerSessionResourceCleanup(cleanup: SessionResourceCleanup): () => void {\n\tsessionResourceCleanups.add(cleanup);\n\treturn () => {\n\t\tsessionResourceCleanups.delete(cleanup);\n\t};\n}\n\nexport function cleanupSessionResources(sessionId?: string): void {\n\tconst errors: unknown[] = [];\n\tfor (const cleanup of sessionResourceCleanups) {\n\t\ttry {\n\t\t\tcleanup(sessionId);\n\t\t} catch (error) {\n\t\t\terrors.push(error);\n\t\t}\n\t}\n\tif (errors.length > 0) {\n\t\tthrow new AggregateError(errors, \"Failed to cleanup session resources\");\n\t}\n}\n"]}
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./providers/register-builtins.
|
|
2
|
-
import type { Api, AssistantMessage, AssistantMessageEventStream, Context, Model, ProviderStreamOptions, SimpleStreamOptions } from "./types.
|
|
3
|
-
export { getEnvApiKey } from "./env-api-keys.
|
|
1
|
+
import "./providers/register-builtins.ts";
|
|
2
|
+
import type { Api, AssistantMessage, AssistantMessageEventStream, Context, Model, ProviderStreamOptions, SimpleStreamOptions } from "./types.ts";
|
|
3
|
+
export { getEnvApiKey } from "./env-api-keys.ts";
|
|
4
4
|
export declare function stream<TApi extends Api>(model: Model<TApi>, context: Context, options?: ProviderStreamOptions): AssistantMessageEventStream;
|
|
5
5
|
export declare function complete<TApi extends Api>(model: Model<TApi>, context: Context, options?: ProviderStreamOptions): Promise<AssistantMessage>;
|
|
6
6
|
export declare function streamSimple<TApi extends Api>(model: Model<TApi>, context: Context, options?: SimpleStreamOptions): AssistantMessageEventStream;
|
package/dist/stream.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;AAI1C,OAAO,KAAK,EACX,GAAG,EACH,gBAAgB,EAChB,2BAA2B,EAC3B,OAAO,EACP,KAAK,EACL,qBAAqB,EACrB,mBAAmB,EAEnB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAwBjD,wBAAgB,MAAM,CAAC,IAAI,SAAS,GAAG,EACtC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,qBAAqB,GAC7B,2BAA2B,CAG7B;AAED,wBAAsB,QAAQ,CAAC,IAAI,SAAS,GAAG,EAC9C,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,qBAAqB,GAC7B,OAAO,CAAC,gBAAgB,CAAC,CAG3B;AAED,wBAAgB,YAAY,CAAC,IAAI,SAAS,GAAG,EAC5C,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,mBAAmB,GAC3B,2BAA2B,CAG7B;AAED,wBAAsB,cAAc,CAAC,IAAI,SAAS,GAAG,EACpD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC,gBAAgB,CAAC,CAG3B","sourcesContent":["import \"./providers/register-builtins.ts\";\n\nimport { getApiProvider } from \"./api-registry.ts\";\nimport { getEnvApiKey } from \"./env-api-keys.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tAssistantMessageEventStream,\n\tContext,\n\tModel,\n\tProviderStreamOptions,\n\tSimpleStreamOptions,\n\tStreamOptions,\n} from \"./types.ts\";\n\nexport { getEnvApiKey } from \"./env-api-keys.ts\";\n\nfunction hasExplicitApiKey(apiKey: string | undefined): apiKey is string {\n\treturn typeof apiKey === \"string\" && apiKey.trim().length > 0;\n}\n\nfunction withEnvApiKey<TOptions extends StreamOptions>(\n\tmodel: Model<Api>,\n\toptions: TOptions | undefined,\n): TOptions | undefined {\n\tif (hasExplicitApiKey(options?.apiKey)) return options;\n\tconst apiKey = getEnvApiKey(model.provider);\n\tif (!apiKey) return options;\n\treturn { ...options, apiKey } as TOptions;\n}\n\nfunction resolveApiProvider(api: Api) {\n\tconst provider = getApiProvider(api);\n\tif (!provider) {\n\t\tthrow new Error(`No API provider registered for api: ${api}`);\n\t}\n\treturn provider;\n}\n\nexport function stream<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: ProviderStreamOptions,\n): AssistantMessageEventStream {\n\tconst provider = resolveApiProvider(model.api);\n\treturn provider.stream(model, context, withEnvApiKey(model, options) as StreamOptions);\n}\n\nexport async function complete<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: ProviderStreamOptions,\n): Promise<AssistantMessage> {\n\tconst s = stream(model, context, options);\n\treturn s.result();\n}\n\nexport function streamSimple<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream {\n\tconst provider = resolveApiProvider(model.api);\n\treturn provider.streamSimple(model, context, withEnvApiKey(model, options));\n}\n\nexport async function completeSimple<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): Promise<AssistantMessage> {\n\tconst s = streamSimple(model, context, options);\n\treturn s.result();\n}\n"]}
|
package/dist/stream.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import "./providers/register-builtins.js";
|
|
2
2
|
import { getApiProvider } from "./api-registry.js";
|
|
3
|
+
import { getEnvApiKey } from "./env-api-keys.js";
|
|
3
4
|
export { getEnvApiKey } from "./env-api-keys.js";
|
|
5
|
+
function hasExplicitApiKey(apiKey) {
|
|
6
|
+
return typeof apiKey === "string" && apiKey.trim().length > 0;
|
|
7
|
+
}
|
|
8
|
+
function withEnvApiKey(model, options) {
|
|
9
|
+
if (hasExplicitApiKey(options?.apiKey))
|
|
10
|
+
return options;
|
|
11
|
+
const apiKey = getEnvApiKey(model.provider);
|
|
12
|
+
if (!apiKey)
|
|
13
|
+
return options;
|
|
14
|
+
return { ...options, apiKey };
|
|
15
|
+
}
|
|
4
16
|
function resolveApiProvider(api) {
|
|
5
17
|
const provider = getApiProvider(api);
|
|
6
18
|
if (!provider) {
|
|
@@ -10,7 +22,7 @@ function resolveApiProvider(api) {
|
|
|
10
22
|
}
|
|
11
23
|
export function stream(model, context, options) {
|
|
12
24
|
const provider = resolveApiProvider(model.api);
|
|
13
|
-
return provider.stream(model, context, options);
|
|
25
|
+
return provider.stream(model, context, withEnvApiKey(model, options));
|
|
14
26
|
}
|
|
15
27
|
export async function complete(model, context, options) {
|
|
16
28
|
const s = stream(model, context, options);
|
|
@@ -18,7 +30,7 @@ export async function complete(model, context, options) {
|
|
|
18
30
|
}
|
|
19
31
|
export function streamSimple(model, context, options) {
|
|
20
32
|
const provider = resolveApiProvider(model.api);
|
|
21
|
-
return provider.streamSimple(model, context, options);
|
|
33
|
+
return provider.streamSimple(model, context, withEnvApiKey(model, options));
|
|
22
34
|
}
|
|
23
35
|
export async function completeSimple(model, context, options) {
|
|
24
36
|
const s = streamSimple(model, context, options);
|
package/dist/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;AAE1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;AAE1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAYjD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,SAAS,iBAAiB,CAAC,MAA0B,EAAoB;IACxE,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;AAAA,CAC9D;AAED,SAAS,aAAa,CACrB,KAAiB,EACjB,OAA6B,EACN;IACvB,IAAI,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC;QAAE,OAAO,OAAO,CAAC;IACvD,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,Q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AAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAChD,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;AAAA,CAClB","sourcesContent":["import \"./providers/register-builtins.ts\";\n\nimport { getApiProvider } from \"./api-registry.ts\";\nimport { getEnvApiKey } from \"./env-api-keys.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tAssistantMessageEventStream,\n\tContext,\n\tModel,\n\tProviderStreamOptions,\n\tSimpleStreamOptions,\n\tStreamOptions,\n} from \"./types.ts\";\n\nexport { getEnvApiKey } from \"./env-api-keys.ts\";\n\nfunction hasExplicitApiKey(apiKey: string | undefined): apiKey is string {\n\treturn typeof apiKey === \"string\" && apiKey.trim().length > 0;\n}\n\nfunction withEnvApiKey<TOptions extends StreamOptions>(\n\tmodel: Model<Api>,\n\toptions: TOptions | undefined,\n): TOptions | undefined {\n\tif (hasExplicitApiKey(options?.apiKey)) return options;\n\tconst apiKey = getEnvApiKey(model.provider);\n\tif (!apiKey) return options;\n\treturn { ...options, apiKey } as TOptions;\n}\n\nfunction resolveApiProvider(api: Api) {\n\tconst provider = getApiProvider(api);\n\tif (!provider) {\n\t\tthrow new Error(`No API provider registered for api: ${api}`);\n\t}\n\treturn provider;\n}\n\nexport function stream<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: ProviderStreamOptions,\n): AssistantMessageEventStream {\n\tconst provider = resolveApiProvider(model.api);\n\treturn provider.stream(model, context, withEnvApiKey(model, options) as StreamOptions);\n}\n\nexport async function complete<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: ProviderStreamOptions,\n): Promise<AssistantMessage> {\n\tconst s = stream(model, context, options);\n\treturn s.result();\n}\n\nexport function streamSimple<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream {\n\tconst provider = resolveApiProvider(model.api);\n\treturn provider.streamSimple(model, context, withEnvApiKey(model, options));\n}\n\nexport async function completeSimple<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): Promise<AssistantMessage> {\n\tconst s = streamSimple(model, context, options);\n\treturn s.result();\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export type
|
|
1
|
+
import type { AssistantMessageDiagnostic } from "./utils/diagnostics.ts";
|
|
2
|
+
import type { AssistantMessageEventStream } from "./utils/event-stream.ts";
|
|
3
|
+
export type { AssistantMessageEventStream } from "./utils/event-stream.ts";
|
|
4
|
+
export type KnownApi = "openai-completions" | "mistral-conversations" | "openai-responses" | "azure-openai-responses" | "openai-codex-responses" | "anthropic-messages" | "bedrock-converse-stream" | "google-generative-ai" | "google-vertex";
|
|
4
5
|
export type Api = KnownApi | (string & {});
|
|
5
|
-
export type
|
|
6
|
+
export type KnownImagesApi = "openrouter-images";
|
|
7
|
+
export type ImagesApi = KnownImagesApi | (string & {});
|
|
8
|
+
export type KnownProvider = "amazon-bedrock" | "ant-ling" | "anthropic" | "google" | "google-vertex" | "openai" | "azure-openai-responses" | "openai-codex" | "nvidia" | "deepseek" | "github-copilot" | "xai" | "groq" | "cerebras" | "openrouter" | "vercel-ai-gateway" | "zai" | "zai-coding-cn" | "mistral" | "minimax" | "minimax-cn" | "moonshotai" | "moonshotai-cn" | "huggingface" | "fireworks" | "together" | "opencode" | "opencode-go" | "kimi-coding" | "cloudflare-workers-ai" | "cloudflare-ai-gateway" | "xiaomi" | "xiaomi-token-plan-cn" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-sgp";
|
|
6
9
|
export type Provider = KnownProvider | string;
|
|
10
|
+
export type KnownImagesProvider = "openrouter";
|
|
11
|
+
export type ImagesProvider = KnownImagesProvider | string;
|
|
7
12
|
export type ThinkingLevel = "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
13
|
+
export type ModelThinkingLevel = "off" | ThinkingLevel;
|
|
14
|
+
export type ThinkingLevelMap = Partial<Record<ModelThinkingLevel, string | null>>;
|
|
8
15
|
/** Token budgets for each thinking level (token-based providers only) */
|
|
9
16
|
export interface ThinkingBudgets {
|
|
10
17
|
minimal?: number;
|
|
@@ -13,7 +20,11 @@ export interface ThinkingBudgets {
|
|
|
13
20
|
high?: number;
|
|
14
21
|
}
|
|
15
22
|
export type CacheRetention = "none" | "short" | "long";
|
|
16
|
-
export type Transport = "sse" | "websocket" | "auto";
|
|
23
|
+
export type Transport = "sse" | "websocket" | "websocket-cached" | "auto";
|
|
24
|
+
export interface ProviderResponse {
|
|
25
|
+
status: number;
|
|
26
|
+
headers: Record<string, string>;
|
|
27
|
+
}
|
|
17
28
|
export interface StreamOptions {
|
|
18
29
|
temperature?: number;
|
|
19
30
|
maxTokens?: number;
|
|
@@ -40,12 +51,35 @@ export interface StreamOptions {
|
|
|
40
51
|
* Return undefined to keep the payload unchanged.
|
|
41
52
|
*/
|
|
42
53
|
onPayload?: (payload: unknown, model: Model<Api>) => unknown | undefined | Promise<unknown | undefined>;
|
|
54
|
+
/**
|
|
55
|
+
* Optional callback invoked after an HTTP response is received and before
|
|
56
|
+
* its body stream is consumed.
|
|
57
|
+
*/
|
|
58
|
+
onResponse?: (response: ProviderResponse, model: Model<Api>) => void | Promise<void>;
|
|
43
59
|
/**
|
|
44
60
|
* Optional custom HTTP headers to include in API requests.
|
|
45
|
-
* Merged with provider defaults;
|
|
46
|
-
*
|
|
61
|
+
* Merged with provider defaults; caller values override default headers.
|
|
62
|
+
* On AWS Bedrock these are injected via a Smithy `build`-step middleware so
|
|
63
|
+
* they are covered by SigV4 signing; reserved headers (`x-amz-*`,
|
|
64
|
+
* `authorization`, `host`) are silently ignored to preserve SigV4 / bearer auth.
|
|
47
65
|
*/
|
|
48
66
|
headers?: Record<string, string>;
|
|
67
|
+
/**
|
|
68
|
+
* HTTP request timeout in milliseconds for providers/SDKs that support it.
|
|
69
|
+
* For example, OpenAI and Anthropic SDK clients default to 10 minutes.
|
|
70
|
+
*/
|
|
71
|
+
timeoutMs?: number;
|
|
72
|
+
/**
|
|
73
|
+
* WebSocket connect timeout in milliseconds for providers that support
|
|
74
|
+
* WebSocket transports. This covers the connection/open handshake only;
|
|
75
|
+
* stream idleness after connection uses timeoutMs.
|
|
76
|
+
*/
|
|
77
|
+
websocketConnectTimeoutMs?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Maximum retry attempts for providers/SDKs that support client-side retries.
|
|
80
|
+
* For example, OpenAI and Anthropic SDK clients default to 2.
|
|
81
|
+
*/
|
|
82
|
+
maxRetries?: number;
|
|
49
83
|
/**
|
|
50
84
|
* Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
|
|
51
85
|
* If the server's requested delay exceeds this value, the request fails immediately
|
|
@@ -62,12 +96,53 @@ export interface StreamOptions {
|
|
|
62
96
|
metadata?: Record<string, unknown>;
|
|
63
97
|
}
|
|
64
98
|
export type ProviderStreamOptions = StreamOptions & Record<string, unknown>;
|
|
99
|
+
export interface ImagesOptions {
|
|
100
|
+
signal?: AbortSignal;
|
|
101
|
+
apiKey?: string;
|
|
102
|
+
/**
|
|
103
|
+
* Optional callback for inspecting or replacing provider payloads before sending.
|
|
104
|
+
* Return undefined to keep the payload unchanged.
|
|
105
|
+
*/
|
|
106
|
+
onPayload?: (payload: unknown, model: ImagesModel<ImagesApi>) => unknown | undefined | Promise<unknown | undefined>;
|
|
107
|
+
/**
|
|
108
|
+
* Optional callback invoked after an HTTP response is received.
|
|
109
|
+
*/
|
|
110
|
+
onResponse?: (response: ProviderResponse, model: ImagesModel<ImagesApi>) => void | Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Optional custom HTTP headers to include in API requests.
|
|
113
|
+
* Merged with provider defaults; can override default headers.
|
|
114
|
+
*/
|
|
115
|
+
headers?: Record<string, string>;
|
|
116
|
+
/**
|
|
117
|
+
* HTTP request timeout in milliseconds for providers/SDKs that support it.
|
|
118
|
+
*/
|
|
119
|
+
timeoutMs?: number;
|
|
120
|
+
/**
|
|
121
|
+
* Maximum retry attempts for providers/SDKs that support client-side retries.
|
|
122
|
+
*/
|
|
123
|
+
maxRetries?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
|
|
126
|
+
* If the server's requested delay exceeds this value, the request fails immediately
|
|
127
|
+
* with an error containing the requested delay, allowing higher-level retry logic
|
|
128
|
+
* to handle it with user visibility.
|
|
129
|
+
* Default: 60000 (60 seconds). Set to 0 to disable the cap.
|
|
130
|
+
*/
|
|
131
|
+
maxRetryDelayMs?: number;
|
|
132
|
+
/**
|
|
133
|
+
* Optional metadata to include in API requests.
|
|
134
|
+
* Providers extract the fields they understand and ignore the rest.
|
|
135
|
+
*/
|
|
136
|
+
metadata?: Record<string, unknown>;
|
|
137
|
+
}
|
|
138
|
+
export type ProviderImagesOptions = ImagesOptions & Record<string, unknown>;
|
|
65
139
|
export interface SimpleStreamOptions extends StreamOptions {
|
|
66
140
|
reasoning?: ThinkingLevel;
|
|
67
141
|
/** Custom token budgets for thinking levels (token-based providers only) */
|
|
68
142
|
thinkingBudgets?: ThinkingBudgets;
|
|
69
143
|
}
|
|
70
144
|
export type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (model: Model<TApi>, context: Context, options?: TOptions) => AssistantMessageEventStream;
|
|
145
|
+
export type ImagesFunction<TApi extends ImagesApi = ImagesApi, TOptions extends ImagesOptions = ImagesOptions> = (model: ImagesModel<TApi>, context: ImagesContext, options?: TOptions) => Promise<AssistantImages>;
|
|
71
146
|
export interface TextSignatureV1 {
|
|
72
147
|
v: 1;
|
|
73
148
|
id: string;
|
|
@@ -125,7 +200,9 @@ export interface AssistantMessage {
|
|
|
125
200
|
api: Api;
|
|
126
201
|
provider: Provider;
|
|
127
202
|
model: string;
|
|
203
|
+
responseModel?: string;
|
|
128
204
|
responseId?: string;
|
|
205
|
+
diagnostics?: AssistantMessageDiagnostic[];
|
|
129
206
|
usage: Usage;
|
|
130
207
|
stopReason: StopReason;
|
|
131
208
|
errorMessage?: string;
|
|
@@ -141,7 +218,24 @@ export interface ToolResultMessage<TDetails = any> {
|
|
|
141
218
|
timestamp: number;
|
|
142
219
|
}
|
|
143
220
|
export type Message = UserMessage | AssistantMessage | ToolResultMessage;
|
|
144
|
-
|
|
221
|
+
export type ImagesInputContent = TextContent | ImageContent;
|
|
222
|
+
export type ImagesOutputContent = TextContent | ImageContent;
|
|
223
|
+
export interface ImagesContext {
|
|
224
|
+
input: ImagesInputContent[];
|
|
225
|
+
}
|
|
226
|
+
export type ImagesStopReason = "stop" | "error" | "aborted";
|
|
227
|
+
export interface AssistantImages {
|
|
228
|
+
api: ImagesApi;
|
|
229
|
+
provider: ImagesProvider;
|
|
230
|
+
model: string;
|
|
231
|
+
output: ImagesOutputContent[];
|
|
232
|
+
responseId?: string;
|
|
233
|
+
usage?: Usage;
|
|
234
|
+
stopReason: ImagesStopReason;
|
|
235
|
+
errorMessage?: string;
|
|
236
|
+
timestamp: number;
|
|
237
|
+
}
|
|
238
|
+
import type { TSchema } from "typebox";
|
|
145
239
|
export interface Tool<TParameters extends TSchema = TSchema> {
|
|
146
240
|
name: string;
|
|
147
241
|
description: string;
|
|
@@ -225,8 +319,6 @@ export interface OpenAICompletionsCompat {
|
|
|
225
319
|
supportsDeveloperRole?: boolean;
|
|
226
320
|
/** Whether the provider supports `reasoning_effort`. Default: auto-detected from URL. */
|
|
227
321
|
supportsReasoningEffort?: boolean;
|
|
228
|
-
/** Optional mapping from draht-ai reasoning levels to provider/model-specific `reasoning_effort` values. */
|
|
229
|
-
reasoningEffortMap?: Partial<Record<ThinkingLevel, string>>;
|
|
230
322
|
/** Whether the provider supports `stream_options: { include_usage: true }` for token usage in streaming responses. Default: true. */
|
|
231
323
|
supportsUsageInStreaming?: boolean;
|
|
232
324
|
/** Which field to use for max tokens. Default: auto-detected from URL. */
|
|
@@ -237,9 +329,11 @@ export interface OpenAICompletionsCompat {
|
|
|
237
329
|
requiresAssistantAfterToolResult?: boolean;
|
|
238
330
|
/** Whether thinking blocks must be converted to text blocks with <thinking> delimiters. Default: auto-detected from URL. */
|
|
239
331
|
requiresThinkingAsText?: boolean;
|
|
240
|
-
/**
|
|
241
|
-
|
|
242
|
-
/**
|
|
332
|
+
/** Whether all replayed assistant messages must include an empty reasoning_content field when reasoning is enabled. Default: auto-detected from URL. */
|
|
333
|
+
requiresReasoningContentOnAssistantMessages?: boolean;
|
|
334
|
+
/** Format for reasoning/thinking parameter. "openai" uses reasoning_effort, "openrouter" uses reasoning: { effort }, "deepseek" uses thinking: { type } plus reasoning_effort when supported, "together" uses reasoning: { enabled } plus reasoning_effort when supported, "zai" uses thinking: { type }, "qwen" uses top-level enable_thinking: boolean, "qwen-chat-template" uses chat_template_kwargs.enable_thinking, "string-thinking" uses top-level thinking: string, and "ant-ling" uses reasoning: { effort } only when the mapped effort is non-null. Default: "openai". */
|
|
335
|
+
thinkingFormat?: "openai" | "openrouter" | "deepseek" | "together" | "zai" | "qwen" | "qwen-chat-template" | "string-thinking" | "ant-ling";
|
|
336
|
+
/** OpenRouter-compatible routing preferences sent as the `provider` request field. */
|
|
243
337
|
openRouterRouting?: OpenRouterRouting;
|
|
244
338
|
/** Vercel AI Gateway routing preferences. Only used when baseUrl points to Vercel AI Gateway. */
|
|
245
339
|
vercelGatewayRouting?: VercelGatewayRouting;
|
|
@@ -247,9 +341,68 @@ export interface OpenAICompletionsCompat {
|
|
|
247
341
|
zaiToolStream?: boolean;
|
|
248
342
|
/** Whether the provider supports the `strict` field in tool definitions. Default: true. */
|
|
249
343
|
supportsStrictMode?: boolean;
|
|
344
|
+
/** Cache control convention for prompt caching. "anthropic" applies Anthropic-style `cache_control` markers to the system prompt, last tool definition, and last user/assistant text content. */
|
|
345
|
+
cacheControlFormat?: "anthropic";
|
|
346
|
+
/** Whether to send known session-affinity headers (`session_id`, `x-client-request-id`, `x-session-affinity`) from `options.sessionId` when caching is enabled. Default: false. */
|
|
347
|
+
sendSessionAffinityHeaders?: boolean;
|
|
348
|
+
/** Whether the provider supports long prompt cache retention (`prompt_cache_retention: "24h"` or Anthropic-style `cache_control.ttl: "1h"`, depending on format). Default: true. */
|
|
349
|
+
supportsLongCacheRetention?: boolean;
|
|
250
350
|
}
|
|
251
351
|
/** Compatibility settings for OpenAI Responses APIs. */
|
|
252
352
|
export interface OpenAIResponsesCompat {
|
|
353
|
+
/** Whether the provider supports the `developer` role (vs `system`). Default: true. */
|
|
354
|
+
supportsDeveloperRole?: boolean;
|
|
355
|
+
/** Whether to send the OpenAI `session_id` cache-affinity header from `options.sessionId` when caching is enabled. Default: true. */
|
|
356
|
+
sendSessionIdHeader?: boolean;
|
|
357
|
+
/** Whether the provider supports `prompt_cache_retention: "24h"`. Default: true. */
|
|
358
|
+
supportsLongCacheRetention?: boolean;
|
|
359
|
+
}
|
|
360
|
+
/** Compatibility settings for Anthropic Messages-compatible APIs. */
|
|
361
|
+
export interface AnthropicMessagesCompat {
|
|
362
|
+
/**
|
|
363
|
+
* Whether the provider accepts per-tool `eager_input_streaming`.
|
|
364
|
+
* When false, the Anthropic provider omits `tools[].eager_input_streaming`
|
|
365
|
+
* and sends the legacy `fine-grained-tool-streaming-2025-05-14` beta header
|
|
366
|
+
* for tool-enabled requests.
|
|
367
|
+
* Default: true.
|
|
368
|
+
*/
|
|
369
|
+
supportsEagerToolInputStreaming?: boolean;
|
|
370
|
+
/** Whether the provider supports Anthropic long cache retention (`cache_control.ttl: "1h"`). Default: true. */
|
|
371
|
+
supportsLongCacheRetention?: boolean;
|
|
372
|
+
/**
|
|
373
|
+
* Whether to send the `x-session-affinity` header from `options.sessionId`
|
|
374
|
+
* when caching is enabled. Required for providers like Fireworks that use
|
|
375
|
+
* session affinity for prompt cache routing (requests to the same replica
|
|
376
|
+
* maximize cache hits).
|
|
377
|
+
* Default: false.
|
|
378
|
+
*/
|
|
379
|
+
sendSessionAffinityHeaders?: boolean;
|
|
380
|
+
/**
|
|
381
|
+
* Whether the provider supports Anthropic-style `cache_control` markers on
|
|
382
|
+
* tool definitions. When false, `cache_control` is omitted from tool params.
|
|
383
|
+
* Some Anthropic-compatible providers (e.g., Fireworks) do not support this
|
|
384
|
+
* field on tools and may reject or ignore it.
|
|
385
|
+
* Default: true.
|
|
386
|
+
*/
|
|
387
|
+
supportsCacheControlOnTools?: boolean;
|
|
388
|
+
/**
|
|
389
|
+
* Whether the model accepts the Anthropic `temperature` request field.
|
|
390
|
+
* Claude Opus 4.7+ rejects non-default temperature values.
|
|
391
|
+
* Default: true.
|
|
392
|
+
*/
|
|
393
|
+
supportsTemperature?: boolean;
|
|
394
|
+
/**
|
|
395
|
+
* Whether to force adaptive thinking (`thinking.type: "adaptive"` plus
|
|
396
|
+
* `output_config.effort`) regardless of the model id. Built-in models that
|
|
397
|
+
* require adaptive thinking set this in generated metadata. Custom
|
|
398
|
+
* Anthropic-compatible providers can set this to `true` for any model whose
|
|
399
|
+
* upstream requires the adaptive format. Set to `false` to
|
|
400
|
+
* opt out on overridden built-in models.
|
|
401
|
+
* Default: false.
|
|
402
|
+
*/
|
|
403
|
+
forceAdaptiveThinking?: boolean;
|
|
404
|
+
/** Whether to replay empty thinking signatures as `signature: ""` instead of converting thinking to text. Default: false. */
|
|
405
|
+
allowEmptySignature?: boolean;
|
|
253
406
|
}
|
|
254
407
|
/**
|
|
255
408
|
* OpenRouter provider routing preferences.
|
|
@@ -337,6 +490,11 @@ export interface Model<TApi extends Api> {
|
|
|
337
490
|
provider: Provider;
|
|
338
491
|
baseUrl: string;
|
|
339
492
|
reasoning: boolean;
|
|
493
|
+
/**
|
|
494
|
+
* Maps pi thinking levels to provider/model-specific values.
|
|
495
|
+
* Missing keys use provider defaults. null marks a level as unsupported.
|
|
496
|
+
*/
|
|
497
|
+
thinkingLevelMap?: ThinkingLevelMap;
|
|
340
498
|
input: ("text" | "image")[];
|
|
341
499
|
cost: {
|
|
342
500
|
input: number;
|
|
@@ -348,6 +506,11 @@ export interface Model<TApi extends Api> {
|
|
|
348
506
|
maxTokens: number;
|
|
349
507
|
headers?: Record<string, string>;
|
|
350
508
|
/** Compatibility overrides for OpenAI-compatible APIs. If not set, auto-detected from baseUrl. */
|
|
351
|
-
compat?: TApi extends "openai-completions" ? OpenAICompletionsCompat : TApi extends "openai-responses" ? OpenAIResponsesCompat : never;
|
|
509
|
+
compat?: TApi extends "openai-completions" ? OpenAICompletionsCompat : TApi extends "openai-responses" ? OpenAIResponsesCompat : TApi extends "anthropic-messages" ? AnthropicMessagesCompat : never;
|
|
510
|
+
}
|
|
511
|
+
export interface ImagesModel<TApi extends ImagesApi> extends Omit<Model<Api>, "api" | "provider" | "reasoning" | "contextWindow" | "maxTokens" | "compat"> {
|
|
512
|
+
api: TApi;
|
|
513
|
+
provider: ImagesProvider;
|
|
514
|
+
output: ("text" | "image")[];
|
|
352
515
|
}
|
|
353
516
|
//# sourceMappingURL=types.d.ts.map
|