@providerprotocol/ai 0.0.34 → 0.0.36
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 +546 -3
- package/dist/anthropic/index.d.ts +2 -1
- package/dist/anthropic/index.js +151 -145
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +392 -0
- package/dist/cerebras/index.js +648 -0
- package/dist/cerebras/index.js.map +1 -0
- package/dist/chunk-2YXFLRQ6.js +49 -0
- package/dist/chunk-2YXFLRQ6.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-4RX4VQCB.js +31 -0
- package/dist/chunk-4RX4VQCB.js.map +1 -0
- package/dist/chunk-5IWHCXKN.js +30 -0
- package/dist/chunk-5IWHCXKN.js.map +1 -0
- package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
- package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
- package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
- package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
- package/dist/chunk-AY55T37A.js.map +1 -0
- package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
- package/dist/chunk-BRP5XJ6Q.js.map +1 -0
- package/dist/chunk-C4JP64VW.js +298 -0
- package/dist/chunk-C4JP64VW.js.map +1 -0
- package/dist/chunk-COS4ON4G.js +111 -0
- package/dist/chunk-COS4ON4G.js.map +1 -0
- package/dist/chunk-CRP6Y7NF.js +31 -0
- package/dist/chunk-CRP6Y7NF.js.map +1 -0
- package/dist/chunk-EPB3GQNL.js +118 -0
- package/dist/chunk-EPB3GQNL.js.map +1 -0
- package/dist/chunk-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-I53CI6ZZ.js +142 -0
- package/dist/chunk-I53CI6ZZ.js.map +1 -0
- package/dist/chunk-IDZOVWP3.js +29 -0
- package/dist/chunk-IDZOVWP3.js.map +1 -0
- package/dist/chunk-JA3UZALR.js +88 -0
- package/dist/chunk-JA3UZALR.js.map +1 -0
- package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
- package/dist/chunk-N5DX5JW3.js.map +1 -0
- package/dist/chunk-OIEWDFQU.js +97 -0
- package/dist/chunk-OIEWDFQU.js.map +1 -0
- package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
- package/dist/chunk-PMK5LZ5Z.js.map +1 -0
- package/dist/{chunk-6S222DHN.js → chunk-RJGTRQ47.js} +20 -1
- package/dist/chunk-RJGTRQ47.js.map +1 -0
- package/dist/chunk-UFFJDYCE.js +94 -0
- package/dist/chunk-UFFJDYCE.js.map +1 -0
- package/dist/chunk-VGKZIGVI.js +222 -0
- package/dist/chunk-VGKZIGVI.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/{embedding-D2BYIehX.d.ts → embedding-BXA72PlJ.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -1
- package/dist/google/index.js +202 -199
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +410 -0
- package/dist/groq/index.js +649 -0
- package/dist/groq/index.js.map +1 -0
- package/dist/http/index.d.ts +3 -2
- package/dist/http/index.js +5 -4
- package/dist/image-stream-CCgwB7ve.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +520 -769
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-ByUFPcFH.d.ts} +12 -1632
- package/dist/middleware/logging/index.d.ts +76 -0
- package/dist/middleware/logging/index.js +74 -0
- package/dist/middleware/logging/index.js.map +1 -0
- package/dist/middleware/parsed-object/index.d.ts +45 -0
- package/dist/middleware/parsed-object/index.js +73 -0
- package/dist/middleware/parsed-object/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +97 -0
- package/dist/middleware/pubsub/index.js +160 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +66 -0
- package/dist/middleware/pubsub/server/express/index.js +11 -0
- package/dist/middleware/pubsub/server/express/index.js.map +1 -0
- package/dist/middleware/pubsub/server/fastify/index.d.ts +67 -0
- package/dist/middleware/pubsub/server/fastify/index.js +11 -0
- package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
- package/dist/middleware/pubsub/server/h3/index.d.ts +70 -0
- package/dist/middleware/pubsub/server/h3/index.js +11 -0
- package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
- package/dist/middleware/pubsub/server/index.d.ts +78 -0
- package/dist/middleware/pubsub/server/index.js +34 -0
- package/dist/middleware/pubsub/server/index.js.map +1 -0
- package/dist/middleware/pubsub/server/webapi/index.d.ts +63 -0
- package/dist/middleware/pubsub/server/webapi/index.js +11 -0
- package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
- package/dist/ollama/index.d.ts +2 -1
- package/dist/ollama/index.js +48 -45
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -1
- package/dist/openai/index.js +319 -313
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -1
- package/dist/openrouter/index.js +381 -385
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +10 -914
- package/dist/proxy/index.js +275 -1007
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +161 -0
- package/dist/proxy/server/express/index.js +24 -0
- package/dist/proxy/server/express/index.js.map +1 -0
- package/dist/proxy/server/fastify/index.d.ts +162 -0
- package/dist/proxy/server/fastify/index.js +24 -0
- package/dist/proxy/server/fastify/index.js.map +1 -0
- package/dist/proxy/server/h3/index.d.ts +189 -0
- package/dist/proxy/server/h3/index.js +28 -0
- package/dist/proxy/server/h3/index.js.map +1 -0
- package/dist/proxy/server/index.d.ts +151 -0
- package/dist/proxy/server/index.js +48 -0
- package/dist/proxy/server/index.js.map +1 -0
- package/dist/proxy/server/webapi/index.d.ts +278 -0
- package/dist/proxy/server/webapi/index.js +32 -0
- package/dist/proxy/server/webapi/index.js.map +1 -0
- package/dist/responses/index.d.ts +650 -0
- package/dist/responses/index.js +930 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/{retry-8Ch-WWgX.d.ts → retry-BDMo4AVu.d.ts} +1 -1
- package/dist/stream-S7nwQRqM.d.ts +1643 -0
- package/dist/types-CE4B7pno.d.ts +96 -0
- package/dist/utils/index.d.ts +53 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/xai/index.d.ts +2 -1
- package/dist/xai/index.js +310 -310
- package/dist/xai/index.js.map +1 -1
- package/package.json +82 -4
- package/dist/chunk-4J6OFUKX.js.map +0 -1
- package/dist/chunk-6S222DHN.js.map +0 -1
- package/dist/chunk-ILR2D5PN.js.map +0 -1
- package/dist/chunk-TOJCZMVU.js.map +0 -1
- package/dist/chunk-WAKD3OO5.js.map +0 -1
- /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
- /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseSSEStream
|
|
3
|
+
} from "../chunk-PMK5LZ5Z.js";
|
|
4
|
+
import {
|
|
5
|
+
parseJsonResponse
|
|
6
|
+
} from "../chunk-A2IM7PGT.js";
|
|
7
|
+
import {
|
|
8
|
+
resolveApiKey
|
|
9
|
+
} from "../chunk-ARVM24K2.js";
|
|
10
|
+
import {
|
|
11
|
+
createProvider
|
|
12
|
+
} from "../chunk-JA3UZALR.js";
|
|
13
|
+
import {
|
|
14
|
+
AssistantMessage,
|
|
15
|
+
generateId,
|
|
16
|
+
isAssistantMessage,
|
|
17
|
+
isToolResultMessage,
|
|
18
|
+
isUserMessage
|
|
19
|
+
} from "../chunk-BRP5XJ6Q.js";
|
|
20
|
+
import {
|
|
21
|
+
doFetch,
|
|
22
|
+
doStreamFetch,
|
|
23
|
+
normalizeHttpError,
|
|
24
|
+
toError
|
|
25
|
+
} from "../chunk-AY55T37A.js";
|
|
26
|
+
import {
|
|
27
|
+
ErrorCode,
|
|
28
|
+
ModalityType,
|
|
29
|
+
UPPError
|
|
30
|
+
} from "../chunk-COS4ON4G.js";
|
|
31
|
+
import {
|
|
32
|
+
StreamEventType,
|
|
33
|
+
objectDelta
|
|
34
|
+
} from "../chunk-RJGTRQ47.js";
|
|
35
|
+
|
|
36
|
+
// src/providers/responses/transform.ts
|
|
37
|
+
function normalizeSystem(system) {
|
|
38
|
+
if (system === void 0 || system === null) return void 0;
|
|
39
|
+
if (typeof system === "string") return system;
|
|
40
|
+
if (!Array.isArray(system)) {
|
|
41
|
+
throw new UPPError(
|
|
42
|
+
"System prompt must be a string or an array of text blocks",
|
|
43
|
+
ErrorCode.InvalidRequest,
|
|
44
|
+
"responses",
|
|
45
|
+
ModalityType.LLM
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const texts = [];
|
|
49
|
+
for (const block of system) {
|
|
50
|
+
if (!block || typeof block !== "object" || !("text" in block)) {
|
|
51
|
+
throw new UPPError(
|
|
52
|
+
"System prompt array must contain objects with a text field",
|
|
53
|
+
ErrorCode.InvalidRequest,
|
|
54
|
+
"responses",
|
|
55
|
+
ModalityType.LLM
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const textValue = block.text;
|
|
59
|
+
if (typeof textValue !== "string") {
|
|
60
|
+
throw new UPPError(
|
|
61
|
+
"System prompt text must be a string",
|
|
62
|
+
ErrorCode.InvalidRequest,
|
|
63
|
+
"responses",
|
|
64
|
+
ModalityType.LLM
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (textValue.length > 0) {
|
|
68
|
+
texts.push(textValue);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return texts.length > 0 ? texts.join("\n\n") : void 0;
|
|
72
|
+
}
|
|
73
|
+
function filterValidContent(content) {
|
|
74
|
+
return content.filter((c) => c && typeof c.type === "string");
|
|
75
|
+
}
|
|
76
|
+
function transformContentPart(block) {
|
|
77
|
+
switch (block.type) {
|
|
78
|
+
case "text":
|
|
79
|
+
return { type: "input_text", text: block.text };
|
|
80
|
+
case "image": {
|
|
81
|
+
const imageBlock = block;
|
|
82
|
+
if (imageBlock.source.type === "base64") {
|
|
83
|
+
return {
|
|
84
|
+
type: "input_image",
|
|
85
|
+
image_url: `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (imageBlock.source.type === "url") {
|
|
89
|
+
return {
|
|
90
|
+
type: "input_image",
|
|
91
|
+
image_url: imageBlock.source.url
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (imageBlock.source.type === "bytes") {
|
|
95
|
+
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
96
|
+
return {
|
|
97
|
+
type: "input_image",
|
|
98
|
+
image_url: `data:${imageBlock.mimeType};base64,${base64}`
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
throw new UPPError(
|
|
102
|
+
"Unknown image source type",
|
|
103
|
+
ErrorCode.InvalidRequest,
|
|
104
|
+
"responses",
|
|
105
|
+
ModalityType.LLM
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
case "document": {
|
|
109
|
+
const documentBlock = block;
|
|
110
|
+
if (documentBlock.source.type === "base64") {
|
|
111
|
+
return {
|
|
112
|
+
type: "input_file",
|
|
113
|
+
filename: documentBlock.title ?? "document",
|
|
114
|
+
file_data: `data:${documentBlock.mimeType};base64,${documentBlock.source.data}`
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (documentBlock.source.type === "url") {
|
|
118
|
+
return {
|
|
119
|
+
type: "input_file",
|
|
120
|
+
file_url: documentBlock.source.url
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (documentBlock.source.type === "text") {
|
|
124
|
+
const base64 = Buffer.from(documentBlock.source.data).toString("base64");
|
|
125
|
+
return {
|
|
126
|
+
type: "input_file",
|
|
127
|
+
filename: documentBlock.title ?? "document.txt",
|
|
128
|
+
file_data: `data:text/plain;base64,${base64}`
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
throw new UPPError(
|
|
132
|
+
"Unknown document source type",
|
|
133
|
+
ErrorCode.InvalidRequest,
|
|
134
|
+
"responses",
|
|
135
|
+
ModalityType.LLM
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
case "video": {
|
|
139
|
+
const videoBlock = block;
|
|
140
|
+
const base64 = Buffer.from(videoBlock.data).toString("base64");
|
|
141
|
+
return {
|
|
142
|
+
type: "input_video",
|
|
143
|
+
video: `data:${videoBlock.mimeType};base64,${base64}`
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
case "audio": {
|
|
147
|
+
const audioBlock = block;
|
|
148
|
+
const base64 = Buffer.from(audioBlock.data).toString("base64");
|
|
149
|
+
return {
|
|
150
|
+
type: "input_file",
|
|
151
|
+
filename: "audio",
|
|
152
|
+
file_data: `data:${audioBlock.mimeType};base64,${base64}`
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
throw new UPPError(
|
|
157
|
+
`Unsupported content type: ${block.type}`,
|
|
158
|
+
ErrorCode.InvalidRequest,
|
|
159
|
+
"responses",
|
|
160
|
+
ModalityType.LLM
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function transformMessage(message) {
|
|
165
|
+
if (isUserMessage(message)) {
|
|
166
|
+
const validContent = filterValidContent(message.content);
|
|
167
|
+
if (validContent.length === 1 && validContent[0]?.type === "text") {
|
|
168
|
+
return [
|
|
169
|
+
{
|
|
170
|
+
type: "message",
|
|
171
|
+
role: "user",
|
|
172
|
+
content: validContent[0].text
|
|
173
|
+
}
|
|
174
|
+
];
|
|
175
|
+
}
|
|
176
|
+
return [
|
|
177
|
+
{
|
|
178
|
+
type: "message",
|
|
179
|
+
role: "user",
|
|
180
|
+
content: validContent.map(transformContentPart)
|
|
181
|
+
}
|
|
182
|
+
];
|
|
183
|
+
}
|
|
184
|
+
if (isAssistantMessage(message)) {
|
|
185
|
+
const validContent = filterValidContent(message.content);
|
|
186
|
+
const items = [];
|
|
187
|
+
const contentParts = validContent.filter((c) => c.type === "text").map((c) => ({
|
|
188
|
+
type: "output_text",
|
|
189
|
+
text: c.text
|
|
190
|
+
}));
|
|
191
|
+
if (contentParts.length > 0) {
|
|
192
|
+
items.push({
|
|
193
|
+
type: "message",
|
|
194
|
+
role: "assistant",
|
|
195
|
+
content: contentParts
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
const responsesMeta = message.metadata?.responses;
|
|
199
|
+
const functionCallItems = responsesMeta?.functionCallItems;
|
|
200
|
+
if (responsesMeta?.reasoningEncryptedContent) {
|
|
201
|
+
try {
|
|
202
|
+
const reasoningData = JSON.parse(responsesMeta.reasoningEncryptedContent);
|
|
203
|
+
items.push({
|
|
204
|
+
type: "reasoning",
|
|
205
|
+
id: reasoningData.id,
|
|
206
|
+
summary: reasoningData.summary,
|
|
207
|
+
encrypted_content: reasoningData.encrypted_content
|
|
208
|
+
});
|
|
209
|
+
} catch {
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (functionCallItems && functionCallItems.length > 0) {
|
|
213
|
+
for (const fc of functionCallItems) {
|
|
214
|
+
items.push({
|
|
215
|
+
type: "function_call",
|
|
216
|
+
id: fc.id,
|
|
217
|
+
call_id: fc.call_id,
|
|
218
|
+
name: fc.name,
|
|
219
|
+
arguments: fc.arguments
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
} else if (message.toolCalls && message.toolCalls.length > 0) {
|
|
223
|
+
for (const call of message.toolCalls) {
|
|
224
|
+
items.push({
|
|
225
|
+
type: "function_call",
|
|
226
|
+
id: `fc_${call.toolCallId}`,
|
|
227
|
+
call_id: call.toolCallId,
|
|
228
|
+
name: call.toolName,
|
|
229
|
+
arguments: JSON.stringify(call.arguments)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return items;
|
|
234
|
+
}
|
|
235
|
+
if (isToolResultMessage(message)) {
|
|
236
|
+
return message.results.map((result) => ({
|
|
237
|
+
type: "function_call_output",
|
|
238
|
+
call_id: result.toolCallId,
|
|
239
|
+
output: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
function transformInputItems(messages, system) {
|
|
245
|
+
const result = [];
|
|
246
|
+
const normalizedSystem = normalizeSystem(system);
|
|
247
|
+
if (normalizedSystem) {
|
|
248
|
+
result.push({
|
|
249
|
+
type: "message",
|
|
250
|
+
role: "system",
|
|
251
|
+
content: normalizedSystem
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
for (const message of messages) {
|
|
255
|
+
const items = transformMessage(message);
|
|
256
|
+
result.push(...items);
|
|
257
|
+
}
|
|
258
|
+
if (result.length === 1 && result[0]?.type === "message") {
|
|
259
|
+
const item = result[0];
|
|
260
|
+
if (item.role === "user" && typeof item.content === "string") {
|
|
261
|
+
return item.content;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
function extractToolOptions(tool) {
|
|
267
|
+
const meta = tool.metadata?.responses;
|
|
268
|
+
return { strict: meta?.strict };
|
|
269
|
+
}
|
|
270
|
+
function transformTool(tool) {
|
|
271
|
+
const { strict } = extractToolOptions(tool);
|
|
272
|
+
return {
|
|
273
|
+
type: "function",
|
|
274
|
+
name: tool.name,
|
|
275
|
+
description: tool.description,
|
|
276
|
+
parameters: {
|
|
277
|
+
type: "object",
|
|
278
|
+
properties: tool.parameters.properties,
|
|
279
|
+
required: tool.parameters.required,
|
|
280
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
281
|
+
},
|
|
282
|
+
...strict !== void 0 ? { strict } : {}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function transformRequest(request, modelId) {
|
|
286
|
+
const params = request.params ?? {};
|
|
287
|
+
const { tools: builtInTools, ...restParams } = params;
|
|
288
|
+
const responsesRequest = {
|
|
289
|
+
...restParams,
|
|
290
|
+
model: modelId,
|
|
291
|
+
input: transformInputItems(request.messages, request.system)
|
|
292
|
+
};
|
|
293
|
+
const functionTools = request.tools?.map(transformTool) ?? [];
|
|
294
|
+
const allTools = [
|
|
295
|
+
...functionTools,
|
|
296
|
+
...builtInTools ?? []
|
|
297
|
+
];
|
|
298
|
+
if (allTools.length > 0) {
|
|
299
|
+
responsesRequest.tools = allTools;
|
|
300
|
+
}
|
|
301
|
+
if (request.structure) {
|
|
302
|
+
const schema = {
|
|
303
|
+
type: "object",
|
|
304
|
+
properties: request.structure.properties,
|
|
305
|
+
required: request.structure.required,
|
|
306
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
307
|
+
};
|
|
308
|
+
if (request.structure.description) {
|
|
309
|
+
schema.description = request.structure.description;
|
|
310
|
+
}
|
|
311
|
+
responsesRequest.text = {
|
|
312
|
+
format: {
|
|
313
|
+
type: "json_schema",
|
|
314
|
+
name: "json_response",
|
|
315
|
+
description: request.structure.description,
|
|
316
|
+
schema,
|
|
317
|
+
strict: true
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return responsesRequest;
|
|
322
|
+
}
|
|
323
|
+
function transformResponse(data) {
|
|
324
|
+
const content = [];
|
|
325
|
+
const toolCalls = [];
|
|
326
|
+
const functionCallItems = [];
|
|
327
|
+
let hadRefusal = false;
|
|
328
|
+
let structuredData;
|
|
329
|
+
let reasoningEncryptedContent;
|
|
330
|
+
for (const item of data.output) {
|
|
331
|
+
if (item.type === "message") {
|
|
332
|
+
const messageItem = item;
|
|
333
|
+
for (const part of messageItem.content) {
|
|
334
|
+
if (part.type === "output_text") {
|
|
335
|
+
content.push({ type: "text", text: part.text });
|
|
336
|
+
if (structuredData === void 0) {
|
|
337
|
+
try {
|
|
338
|
+
structuredData = JSON.parse(part.text);
|
|
339
|
+
} catch {
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} else if (part.type === "refusal") {
|
|
343
|
+
content.push({ type: "text", text: part.refusal });
|
|
344
|
+
hadRefusal = true;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
} else if (item.type === "function_call") {
|
|
348
|
+
const functionCall = item;
|
|
349
|
+
let args = {};
|
|
350
|
+
try {
|
|
351
|
+
args = JSON.parse(functionCall.arguments);
|
|
352
|
+
} catch {
|
|
353
|
+
}
|
|
354
|
+
toolCalls.push({
|
|
355
|
+
toolCallId: functionCall.call_id,
|
|
356
|
+
toolName: functionCall.name,
|
|
357
|
+
arguments: args
|
|
358
|
+
});
|
|
359
|
+
functionCallItems.push({
|
|
360
|
+
id: functionCall.id,
|
|
361
|
+
call_id: functionCall.call_id,
|
|
362
|
+
name: functionCall.name,
|
|
363
|
+
arguments: functionCall.arguments
|
|
364
|
+
});
|
|
365
|
+
} else if (item.type === "reasoning") {
|
|
366
|
+
const reasoningItem = item;
|
|
367
|
+
const reasoningText = reasoningItem.summary.filter((s) => s.type === "summary_text").map((s) => s.text).join("");
|
|
368
|
+
if (reasoningText) {
|
|
369
|
+
content.push({ type: "reasoning", text: reasoningText });
|
|
370
|
+
}
|
|
371
|
+
reasoningEncryptedContent = JSON.stringify({
|
|
372
|
+
id: reasoningItem.id,
|
|
373
|
+
summary: reasoningItem.summary,
|
|
374
|
+
encrypted_content: reasoningItem.encrypted_content
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const responseId = data.id || generateId();
|
|
379
|
+
const message = new AssistantMessage(
|
|
380
|
+
content,
|
|
381
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
382
|
+
{
|
|
383
|
+
id: responseId,
|
|
384
|
+
metadata: {
|
|
385
|
+
responses: {
|
|
386
|
+
model: data.model,
|
|
387
|
+
status: data.status,
|
|
388
|
+
response_id: responseId,
|
|
389
|
+
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0,
|
|
390
|
+
reasoningEncryptedContent
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
const usage = {
|
|
396
|
+
inputTokens: data.usage.input_tokens,
|
|
397
|
+
outputTokens: data.usage.output_tokens,
|
|
398
|
+
totalTokens: data.usage.total_tokens,
|
|
399
|
+
cacheReadTokens: data.usage.input_tokens_details?.cached_tokens ?? 0,
|
|
400
|
+
cacheWriteTokens: 0
|
|
401
|
+
};
|
|
402
|
+
let stopReason = "end_turn";
|
|
403
|
+
if (data.status === "completed") {
|
|
404
|
+
stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
|
|
405
|
+
} else if (data.status === "incomplete") {
|
|
406
|
+
stopReason = data.incomplete_details?.reason === "max_output_tokens" ? "max_tokens" : "end_turn";
|
|
407
|
+
} else if (data.status === "failed") {
|
|
408
|
+
stopReason = "error";
|
|
409
|
+
}
|
|
410
|
+
if (hadRefusal && stopReason !== "error") {
|
|
411
|
+
stopReason = "content_filter";
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
message,
|
|
415
|
+
usage,
|
|
416
|
+
stopReason,
|
|
417
|
+
data: structuredData
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function createStreamState() {
|
|
421
|
+
return {
|
|
422
|
+
id: "",
|
|
423
|
+
model: "",
|
|
424
|
+
textByIndex: /* @__PURE__ */ new Map(),
|
|
425
|
+
reasoningByIndex: /* @__PURE__ */ new Map(),
|
|
426
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
427
|
+
status: "in_progress",
|
|
428
|
+
incompleteReason: void 0,
|
|
429
|
+
inputTokens: 0,
|
|
430
|
+
outputTokens: 0,
|
|
431
|
+
cacheReadTokens: 0,
|
|
432
|
+
hadRefusal: false
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function transformStreamEvent(event, state) {
|
|
436
|
+
const events = [];
|
|
437
|
+
const updateFromResponse = (response) => {
|
|
438
|
+
state.id = response.id || state.id;
|
|
439
|
+
state.model = response.model || state.model;
|
|
440
|
+
state.status = response.status;
|
|
441
|
+
if (response.incomplete_details?.reason) {
|
|
442
|
+
state.incompleteReason = response.incomplete_details.reason;
|
|
443
|
+
} else if (response.status !== "incomplete") {
|
|
444
|
+
state.incompleteReason = void 0;
|
|
445
|
+
}
|
|
446
|
+
if (response.usage) {
|
|
447
|
+
state.inputTokens = response.usage.input_tokens;
|
|
448
|
+
state.outputTokens = response.usage.output_tokens;
|
|
449
|
+
state.cacheReadTokens = response.usage.input_tokens_details?.cached_tokens ?? 0;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
switch (event.type) {
|
|
453
|
+
case "response.created":
|
|
454
|
+
updateFromResponse(event.response);
|
|
455
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
456
|
+
break;
|
|
457
|
+
case "response.queued":
|
|
458
|
+
case "response.in_progress":
|
|
459
|
+
updateFromResponse(event.response);
|
|
460
|
+
break;
|
|
461
|
+
case "response.completed":
|
|
462
|
+
updateFromResponse(event.response);
|
|
463
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
464
|
+
break;
|
|
465
|
+
case "response.failed":
|
|
466
|
+
case "response.incomplete":
|
|
467
|
+
updateFromResponse(event.response);
|
|
468
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
469
|
+
break;
|
|
470
|
+
case "response.output_item.added":
|
|
471
|
+
if (event.item.type === "function_call") {
|
|
472
|
+
const functionCall = event.item;
|
|
473
|
+
const existing = state.toolCalls.get(event.output_index) ?? {
|
|
474
|
+
arguments: ""
|
|
475
|
+
};
|
|
476
|
+
existing.itemId = functionCall.id;
|
|
477
|
+
existing.callId = functionCall.call_id;
|
|
478
|
+
existing.name = functionCall.name;
|
|
479
|
+
if (functionCall.arguments) {
|
|
480
|
+
existing.arguments = functionCall.arguments;
|
|
481
|
+
}
|
|
482
|
+
state.toolCalls.set(event.output_index, existing);
|
|
483
|
+
}
|
|
484
|
+
events.push({
|
|
485
|
+
type: StreamEventType.ContentBlockStart,
|
|
486
|
+
index: event.output_index,
|
|
487
|
+
delta: {}
|
|
488
|
+
});
|
|
489
|
+
break;
|
|
490
|
+
case "response.output_item.done":
|
|
491
|
+
if (event.item.type === "function_call") {
|
|
492
|
+
const functionCall = event.item;
|
|
493
|
+
const existing = state.toolCalls.get(event.output_index) ?? {
|
|
494
|
+
arguments: ""
|
|
495
|
+
};
|
|
496
|
+
existing.itemId = functionCall.id;
|
|
497
|
+
existing.callId = functionCall.call_id;
|
|
498
|
+
existing.name = functionCall.name;
|
|
499
|
+
if (functionCall.arguments) {
|
|
500
|
+
existing.arguments = functionCall.arguments;
|
|
501
|
+
}
|
|
502
|
+
state.toolCalls.set(event.output_index, existing);
|
|
503
|
+
} else if (event.item.type === "reasoning") {
|
|
504
|
+
const reasoningItem = event.item;
|
|
505
|
+
state.reasoningEncryptedContent = JSON.stringify({
|
|
506
|
+
id: reasoningItem.id,
|
|
507
|
+
summary: reasoningItem.summary,
|
|
508
|
+
encrypted_content: reasoningItem.encrypted_content
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
events.push({
|
|
512
|
+
type: StreamEventType.ContentBlockStop,
|
|
513
|
+
index: event.output_index,
|
|
514
|
+
delta: {}
|
|
515
|
+
});
|
|
516
|
+
break;
|
|
517
|
+
case "response.output_text.delta": {
|
|
518
|
+
const currentText = state.textByIndex.get(event.output_index) ?? "";
|
|
519
|
+
state.textByIndex.set(event.output_index, currentText + event.delta);
|
|
520
|
+
events.push({
|
|
521
|
+
type: StreamEventType.TextDelta,
|
|
522
|
+
index: event.output_index,
|
|
523
|
+
delta: { text: event.delta }
|
|
524
|
+
});
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
case "response.output_text.done":
|
|
528
|
+
state.textByIndex.set(event.output_index, event.text);
|
|
529
|
+
break;
|
|
530
|
+
case "response.refusal.delta": {
|
|
531
|
+
state.hadRefusal = true;
|
|
532
|
+
const currentRefusal = state.textByIndex.get(event.output_index) ?? "";
|
|
533
|
+
state.textByIndex.set(event.output_index, currentRefusal + event.delta);
|
|
534
|
+
events.push({
|
|
535
|
+
type: StreamEventType.TextDelta,
|
|
536
|
+
index: event.output_index,
|
|
537
|
+
delta: { text: event.delta }
|
|
538
|
+
});
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
case "response.refusal.done":
|
|
542
|
+
state.hadRefusal = true;
|
|
543
|
+
state.textByIndex.set(event.output_index, event.refusal);
|
|
544
|
+
break;
|
|
545
|
+
case "response.function_call_arguments.delta": {
|
|
546
|
+
let toolCall = state.toolCalls.get(event.output_index);
|
|
547
|
+
if (!toolCall) {
|
|
548
|
+
toolCall = { arguments: "" };
|
|
549
|
+
state.toolCalls.set(event.output_index, toolCall);
|
|
550
|
+
}
|
|
551
|
+
if (event.item_id && !toolCall.itemId) {
|
|
552
|
+
toolCall.itemId = event.item_id;
|
|
553
|
+
}
|
|
554
|
+
if (event.call_id && !toolCall.callId) {
|
|
555
|
+
toolCall.callId = event.call_id;
|
|
556
|
+
}
|
|
557
|
+
toolCall.arguments += event.delta;
|
|
558
|
+
events.push({
|
|
559
|
+
type: StreamEventType.ToolCallDelta,
|
|
560
|
+
index: event.output_index,
|
|
561
|
+
delta: {
|
|
562
|
+
toolCallId: toolCall.callId ?? toolCall.itemId ?? "",
|
|
563
|
+
toolName: toolCall.name,
|
|
564
|
+
argumentsJson: event.delta
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
case "response.function_call_arguments.done": {
|
|
570
|
+
let toolCall = state.toolCalls.get(event.output_index);
|
|
571
|
+
if (!toolCall) {
|
|
572
|
+
toolCall = { arguments: "" };
|
|
573
|
+
state.toolCalls.set(event.output_index, toolCall);
|
|
574
|
+
}
|
|
575
|
+
if (event.item_id) {
|
|
576
|
+
toolCall.itemId = event.item_id;
|
|
577
|
+
}
|
|
578
|
+
if (event.call_id) {
|
|
579
|
+
toolCall.callId = event.call_id;
|
|
580
|
+
}
|
|
581
|
+
toolCall.name = event.name;
|
|
582
|
+
toolCall.arguments = event.arguments;
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
case "response.reasoning_summary_text.delta": {
|
|
586
|
+
const currentReasoning = state.reasoningByIndex.get(event.output_index) ?? "";
|
|
587
|
+
state.reasoningByIndex.set(event.output_index, currentReasoning + event.delta);
|
|
588
|
+
events.push({
|
|
589
|
+
type: StreamEventType.ReasoningDelta,
|
|
590
|
+
index: event.output_index,
|
|
591
|
+
delta: { text: event.delta }
|
|
592
|
+
});
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case "response.reasoning_summary_text.done":
|
|
596
|
+
state.reasoningByIndex.set(event.output_index, event.text);
|
|
597
|
+
break;
|
|
598
|
+
case "error":
|
|
599
|
+
break;
|
|
600
|
+
default:
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
return events;
|
|
604
|
+
}
|
|
605
|
+
function buildResponseFromState(state) {
|
|
606
|
+
const content = [];
|
|
607
|
+
let structuredData;
|
|
608
|
+
const orderedReasoningEntries = [...state.reasoningByIndex.entries()].sort(
|
|
609
|
+
([leftIndex], [rightIndex]) => leftIndex - rightIndex
|
|
610
|
+
);
|
|
611
|
+
for (const [, reasoning] of orderedReasoningEntries) {
|
|
612
|
+
if (reasoning) {
|
|
613
|
+
content.push({ type: "reasoning", text: reasoning });
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
const orderedTextEntries = [...state.textByIndex.entries()].sort(
|
|
617
|
+
([leftIndex], [rightIndex]) => leftIndex - rightIndex
|
|
618
|
+
);
|
|
619
|
+
for (const [, text] of orderedTextEntries) {
|
|
620
|
+
if (text) {
|
|
621
|
+
content.push({ type: "text", text });
|
|
622
|
+
if (structuredData === void 0) {
|
|
623
|
+
try {
|
|
624
|
+
structuredData = JSON.parse(text);
|
|
625
|
+
} catch {
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const toolCalls = [];
|
|
631
|
+
const functionCallItems = [];
|
|
632
|
+
const orderedToolEntries = [...state.toolCalls.entries()].sort(
|
|
633
|
+
([leftIndex], [rightIndex]) => leftIndex - rightIndex
|
|
634
|
+
);
|
|
635
|
+
for (const [, toolCall] of orderedToolEntries) {
|
|
636
|
+
let args = {};
|
|
637
|
+
if (toolCall.arguments) {
|
|
638
|
+
try {
|
|
639
|
+
args = JSON.parse(toolCall.arguments);
|
|
640
|
+
} catch {
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const itemId = toolCall.itemId ?? "";
|
|
644
|
+
const callId = toolCall.callId ?? toolCall.itemId ?? "";
|
|
645
|
+
const name = toolCall.name ?? "";
|
|
646
|
+
if (!name || !callId) {
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
toolCalls.push({
|
|
650
|
+
toolCallId: callId,
|
|
651
|
+
toolName: name,
|
|
652
|
+
arguments: args
|
|
653
|
+
});
|
|
654
|
+
if (itemId && callId && name) {
|
|
655
|
+
functionCallItems.push({
|
|
656
|
+
id: itemId,
|
|
657
|
+
call_id: callId,
|
|
658
|
+
name,
|
|
659
|
+
arguments: toolCall.arguments
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
const responseId = state.id || generateId();
|
|
664
|
+
const message = new AssistantMessage(
|
|
665
|
+
content,
|
|
666
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
667
|
+
{
|
|
668
|
+
id: responseId,
|
|
669
|
+
metadata: {
|
|
670
|
+
responses: {
|
|
671
|
+
model: state.model,
|
|
672
|
+
status: state.status,
|
|
673
|
+
response_id: responseId,
|
|
674
|
+
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0,
|
|
675
|
+
reasoningEncryptedContent: state.reasoningEncryptedContent
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
);
|
|
680
|
+
const usage = {
|
|
681
|
+
inputTokens: state.inputTokens,
|
|
682
|
+
outputTokens: state.outputTokens,
|
|
683
|
+
totalTokens: state.inputTokens + state.outputTokens,
|
|
684
|
+
cacheReadTokens: state.cacheReadTokens,
|
|
685
|
+
cacheWriteTokens: 0
|
|
686
|
+
};
|
|
687
|
+
let stopReason = "end_turn";
|
|
688
|
+
if (state.status === "completed") {
|
|
689
|
+
stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
|
|
690
|
+
} else if (state.status === "incomplete") {
|
|
691
|
+
stopReason = state.incompleteReason === "max_output_tokens" ? "max_tokens" : "end_turn";
|
|
692
|
+
} else if (state.status === "failed") {
|
|
693
|
+
stopReason = "error";
|
|
694
|
+
}
|
|
695
|
+
if (state.hadRefusal && stopReason !== "error") {
|
|
696
|
+
stopReason = "content_filter";
|
|
697
|
+
}
|
|
698
|
+
return {
|
|
699
|
+
message,
|
|
700
|
+
usage,
|
|
701
|
+
stopReason,
|
|
702
|
+
data: structuredData
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// src/providers/responses/llm.ts
|
|
707
|
+
var RESPONSES_CAPABILITIES = {
|
|
708
|
+
streaming: true,
|
|
709
|
+
tools: true,
|
|
710
|
+
structuredOutput: true,
|
|
711
|
+
imageInput: true,
|
|
712
|
+
documentInput: true,
|
|
713
|
+
videoInput: true,
|
|
714
|
+
audioInput: true
|
|
715
|
+
};
|
|
716
|
+
function extractContext(request) {
|
|
717
|
+
const config = request.config;
|
|
718
|
+
const responsesConfig = config._responsesContext;
|
|
719
|
+
if (!responsesConfig) {
|
|
720
|
+
throw new UPPError(
|
|
721
|
+
'OpenResponses provider requires host configuration. Use responses(modelId, { host: "..." })',
|
|
722
|
+
ErrorCode.InvalidRequest,
|
|
723
|
+
"responses",
|
|
724
|
+
ModalityType.LLM
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
return responsesConfig;
|
|
728
|
+
}
|
|
729
|
+
function createLLMHandler() {
|
|
730
|
+
let providerRef = null;
|
|
731
|
+
return {
|
|
732
|
+
_setProvider(provider) {
|
|
733
|
+
providerRef = provider;
|
|
734
|
+
},
|
|
735
|
+
bind(modelId) {
|
|
736
|
+
if (!providerRef) {
|
|
737
|
+
throw new UPPError(
|
|
738
|
+
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
739
|
+
ErrorCode.InvalidRequest,
|
|
740
|
+
"responses",
|
|
741
|
+
ModalityType.LLM
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
const model = {
|
|
745
|
+
modelId,
|
|
746
|
+
capabilities: RESPONSES_CAPABILITIES,
|
|
747
|
+
get provider() {
|
|
748
|
+
return providerRef;
|
|
749
|
+
},
|
|
750
|
+
async complete(request) {
|
|
751
|
+
const context = extractContext(request);
|
|
752
|
+
const apiKey = await resolveApiKey(
|
|
753
|
+
request.config,
|
|
754
|
+
context.apiKeyEnv,
|
|
755
|
+
"responses",
|
|
756
|
+
"llm"
|
|
757
|
+
);
|
|
758
|
+
const baseUrl = request.config.baseUrl ?? `${context.host}/responses`;
|
|
759
|
+
const body = transformRequest(request, modelId);
|
|
760
|
+
const headers = {
|
|
761
|
+
"Content-Type": "application/json",
|
|
762
|
+
Authorization: `Bearer ${apiKey}`
|
|
763
|
+
};
|
|
764
|
+
if (request.config.headers) {
|
|
765
|
+
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
766
|
+
if (value !== void 0) {
|
|
767
|
+
headers[key] = value;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
const response = await doFetch(
|
|
772
|
+
baseUrl,
|
|
773
|
+
{
|
|
774
|
+
method: "POST",
|
|
775
|
+
headers,
|
|
776
|
+
body: JSON.stringify(body),
|
|
777
|
+
signal: request.signal
|
|
778
|
+
},
|
|
779
|
+
request.config,
|
|
780
|
+
"responses",
|
|
781
|
+
"llm"
|
|
782
|
+
);
|
|
783
|
+
const data = await parseJsonResponse(response, "responses", "llm");
|
|
784
|
+
if (data.status === "failed") {
|
|
785
|
+
const message = data.error?.message ?? "Provider returned a failed response.";
|
|
786
|
+
throw new UPPError(
|
|
787
|
+
message,
|
|
788
|
+
ErrorCode.ProviderError,
|
|
789
|
+
"responses",
|
|
790
|
+
ModalityType.LLM
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
return transformResponse(data);
|
|
794
|
+
},
|
|
795
|
+
stream(request) {
|
|
796
|
+
const state = createStreamState();
|
|
797
|
+
let responseResolve;
|
|
798
|
+
let responseReject;
|
|
799
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
800
|
+
responseResolve = resolve;
|
|
801
|
+
responseReject = reject;
|
|
802
|
+
});
|
|
803
|
+
async function* generateEvents() {
|
|
804
|
+
try {
|
|
805
|
+
const context = extractContext(request);
|
|
806
|
+
const apiKey = await resolveApiKey(
|
|
807
|
+
request.config,
|
|
808
|
+
context.apiKeyEnv,
|
|
809
|
+
"responses",
|
|
810
|
+
"llm"
|
|
811
|
+
);
|
|
812
|
+
const baseUrl = request.config.baseUrl ?? `${context.host}/responses`;
|
|
813
|
+
const body = transformRequest(request, modelId);
|
|
814
|
+
body.stream = true;
|
|
815
|
+
const headers = {
|
|
816
|
+
"Content-Type": "application/json",
|
|
817
|
+
Authorization: `Bearer ${apiKey}`,
|
|
818
|
+
Accept: "text/event-stream"
|
|
819
|
+
};
|
|
820
|
+
if (request.config.headers) {
|
|
821
|
+
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
822
|
+
if (value !== void 0) {
|
|
823
|
+
headers[key] = value;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
const response = await doStreamFetch(
|
|
828
|
+
baseUrl,
|
|
829
|
+
{
|
|
830
|
+
method: "POST",
|
|
831
|
+
headers,
|
|
832
|
+
body: JSON.stringify(body),
|
|
833
|
+
signal: request.signal
|
|
834
|
+
},
|
|
835
|
+
request.config,
|
|
836
|
+
"responses",
|
|
837
|
+
"llm"
|
|
838
|
+
);
|
|
839
|
+
if (!response.ok) {
|
|
840
|
+
const error = await normalizeHttpError(response, "responses", "llm");
|
|
841
|
+
responseReject(error);
|
|
842
|
+
throw error;
|
|
843
|
+
}
|
|
844
|
+
if (!response.body) {
|
|
845
|
+
const error = new UPPError(
|
|
846
|
+
"No response body for streaming request",
|
|
847
|
+
ErrorCode.ProviderError,
|
|
848
|
+
"responses",
|
|
849
|
+
ModalityType.LLM
|
|
850
|
+
);
|
|
851
|
+
responseReject(error);
|
|
852
|
+
throw error;
|
|
853
|
+
}
|
|
854
|
+
for await (const data of parseSSEStream(response.body)) {
|
|
855
|
+
if (data === "[DONE]") {
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (typeof data === "object" && data !== null) {
|
|
859
|
+
const event = data;
|
|
860
|
+
if (event.type === "error") {
|
|
861
|
+
const errorEvent = event;
|
|
862
|
+
const error = new UPPError(
|
|
863
|
+
errorEvent.error.message,
|
|
864
|
+
ErrorCode.ProviderError,
|
|
865
|
+
"responses",
|
|
866
|
+
ModalityType.LLM
|
|
867
|
+
);
|
|
868
|
+
responseReject(error);
|
|
869
|
+
throw error;
|
|
870
|
+
}
|
|
871
|
+
const uppEvents = transformStreamEvent(event, state);
|
|
872
|
+
for (const uppEvent of uppEvents) {
|
|
873
|
+
yield uppEvent;
|
|
874
|
+
if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
|
|
875
|
+
yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
responseResolve(buildResponseFromState(state));
|
|
881
|
+
} catch (error) {
|
|
882
|
+
const err = toError(error);
|
|
883
|
+
responseReject(err);
|
|
884
|
+
throw err;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
return {
|
|
888
|
+
[Symbol.asyncIterator]() {
|
|
889
|
+
return generateEvents();
|
|
890
|
+
},
|
|
891
|
+
response: responsePromise
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
return model;
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// src/providers/responses/index.ts
|
|
901
|
+
var responses = createProvider({
|
|
902
|
+
name: "responses",
|
|
903
|
+
version: "1.0.0",
|
|
904
|
+
handlers: {
|
|
905
|
+
llm: createLLMHandler()
|
|
906
|
+
},
|
|
907
|
+
createModelReference: (modelId, options, provider) => {
|
|
908
|
+
if (!options?.host) {
|
|
909
|
+
throw new Error(
|
|
910
|
+
'OpenResponses provider requires a host option. Usage: responses("model-id", { host: "https://api.example.com/v1" })'
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
const providerConfig = {
|
|
914
|
+
_responsesContext: {
|
|
915
|
+
host: options.host,
|
|
916
|
+
apiKeyEnv: options.apiKeyEnv ?? "OPENRESPONSES_API_KEY"
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
return {
|
|
920
|
+
modelId,
|
|
921
|
+
provider,
|
|
922
|
+
options,
|
|
923
|
+
providerConfig
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
export {
|
|
928
|
+
responses
|
|
929
|
+
};
|
|
930
|
+
//# sourceMappingURL=index.js.map
|