@amux.ai/adapter-openai 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1546 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +390 -0
- package/dist/index.d.ts +390 -0
- package/dist/index.js +1543 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1543 @@
|
|
|
1
|
+
import { contentToString, parseOpenAICompatibleError, mapFinishReason, parseOpenAIUsage } from '@amux.ai/llm-bridge';
|
|
2
|
+
|
|
3
|
+
// src/inbound/error-parser.ts
|
|
4
|
+
function parseError(error) {
|
|
5
|
+
return parseOpenAICompatibleError(error);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/inbound/request-parser.ts
|
|
9
|
+
function parseRequest(request) {
|
|
10
|
+
const req = request;
|
|
11
|
+
let system;
|
|
12
|
+
const messages = [];
|
|
13
|
+
for (const msg of req.messages) {
|
|
14
|
+
if (msg.role === "system") {
|
|
15
|
+
if (typeof msg.content === "string") {
|
|
16
|
+
system = system ? `${system}
|
|
17
|
+
${msg.content}` : msg.content;
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
messages.push(parseMessage(msg));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const tools = req.tools?.map((tool) => parseTool(tool));
|
|
24
|
+
const toolChoice = req.tool_choice ? parseToolChoice(req.tool_choice) : void 0;
|
|
25
|
+
return {
|
|
26
|
+
messages,
|
|
27
|
+
model: req.model,
|
|
28
|
+
tools,
|
|
29
|
+
toolChoice,
|
|
30
|
+
stream: req.stream,
|
|
31
|
+
system,
|
|
32
|
+
generation: {
|
|
33
|
+
temperature: req.temperature,
|
|
34
|
+
topP: req.top_p,
|
|
35
|
+
maxTokens: req.max_tokens ?? req.max_completion_tokens,
|
|
36
|
+
stopSequences: req.stop ? Array.isArray(req.stop) ? req.stop : [req.stop] : void 0,
|
|
37
|
+
presencePenalty: req.presence_penalty,
|
|
38
|
+
frequencyPenalty: req.frequency_penalty,
|
|
39
|
+
n: req.n,
|
|
40
|
+
seed: req.seed,
|
|
41
|
+
responseFormat: req.response_format ? {
|
|
42
|
+
type: req.response_format.type,
|
|
43
|
+
jsonSchema: req.response_format.json_schema
|
|
44
|
+
} : void 0,
|
|
45
|
+
logprobs: req.logprobs,
|
|
46
|
+
topLogprobs: req.top_logprobs
|
|
47
|
+
},
|
|
48
|
+
metadata: {
|
|
49
|
+
userId: req.user
|
|
50
|
+
},
|
|
51
|
+
raw: request
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function parseMessage(msg) {
|
|
55
|
+
return {
|
|
56
|
+
role: msg.role,
|
|
57
|
+
content: parseContent(msg.content),
|
|
58
|
+
name: msg.name,
|
|
59
|
+
toolCalls: msg.tool_calls,
|
|
60
|
+
toolCallId: msg.tool_call_id
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function parseContent(content) {
|
|
64
|
+
if (content === null || content === void 0) {
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
if (typeof content === "string") {
|
|
68
|
+
return content;
|
|
69
|
+
}
|
|
70
|
+
return content.map((part) => {
|
|
71
|
+
if (part.type === "text") {
|
|
72
|
+
return {
|
|
73
|
+
type: "text",
|
|
74
|
+
text: part.text
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (part.type === "image_url") {
|
|
78
|
+
const url = part.image_url.url;
|
|
79
|
+
if (url.startsWith("data:")) {
|
|
80
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
81
|
+
if (match) {
|
|
82
|
+
return {
|
|
83
|
+
type: "image",
|
|
84
|
+
source: {
|
|
85
|
+
type: "base64",
|
|
86
|
+
mediaType: match[1],
|
|
87
|
+
data: match[2]
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
type: "image",
|
|
94
|
+
source: {
|
|
95
|
+
type: "url",
|
|
96
|
+
url
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
type: "text",
|
|
102
|
+
text: JSON.stringify(part)
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function parseTool(tool) {
|
|
107
|
+
return {
|
|
108
|
+
type: "function",
|
|
109
|
+
function: {
|
|
110
|
+
name: tool.function.name,
|
|
111
|
+
description: tool.function.description,
|
|
112
|
+
parameters: tool.function.parameters,
|
|
113
|
+
strict: tool.function.strict
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function parseToolChoice(choice) {
|
|
118
|
+
if (typeof choice === "string") {
|
|
119
|
+
return choice;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
type: "function",
|
|
123
|
+
function: {
|
|
124
|
+
name: choice.function.name
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function parseResponse(response) {
|
|
129
|
+
const res = response;
|
|
130
|
+
const choices = res.choices.map((choice) => ({
|
|
131
|
+
index: choice.index,
|
|
132
|
+
message: {
|
|
133
|
+
role: choice.message.role,
|
|
134
|
+
content: choice.message.content ?? "",
|
|
135
|
+
toolCalls: choice.message.tool_calls
|
|
136
|
+
},
|
|
137
|
+
finishReason: mapFinishReason(choice.finish_reason),
|
|
138
|
+
logprobs: choice.logprobs
|
|
139
|
+
}));
|
|
140
|
+
return {
|
|
141
|
+
id: res.id,
|
|
142
|
+
model: res.model,
|
|
143
|
+
choices,
|
|
144
|
+
created: res.created,
|
|
145
|
+
systemFingerprint: res.system_fingerprint,
|
|
146
|
+
usage: parseOpenAIUsage(res.usage),
|
|
147
|
+
raw: response
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/inbound/stream-parser.ts
|
|
152
|
+
function mapFinishReason2(reason) {
|
|
153
|
+
const reasonMap = {
|
|
154
|
+
stop: "stop",
|
|
155
|
+
length: "length",
|
|
156
|
+
tool_calls: "tool_calls",
|
|
157
|
+
content_filter: "content_filter",
|
|
158
|
+
function_call: "tool_calls"
|
|
159
|
+
};
|
|
160
|
+
return reasonMap[reason] ?? "stop";
|
|
161
|
+
}
|
|
162
|
+
function parseStream(chunk) {
|
|
163
|
+
const data = chunk;
|
|
164
|
+
if (!data.choices || data.choices.length === 0) {
|
|
165
|
+
if (data.usage) {
|
|
166
|
+
return {
|
|
167
|
+
type: "end",
|
|
168
|
+
id: data.id,
|
|
169
|
+
model: data.model,
|
|
170
|
+
usage: {
|
|
171
|
+
promptTokens: data.usage.prompt_tokens,
|
|
172
|
+
completionTokens: data.usage.completion_tokens,
|
|
173
|
+
totalTokens: data.usage.total_tokens,
|
|
174
|
+
details: data.usage.completion_tokens_details ? {
|
|
175
|
+
reasoningTokens: data.usage.completion_tokens_details.reasoning_tokens
|
|
176
|
+
} : void 0
|
|
177
|
+
},
|
|
178
|
+
raw: chunk
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const choice = data.choices[0];
|
|
184
|
+
if (!choice) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const delta = choice.delta;
|
|
188
|
+
if (delta.role && !delta.content && !delta.tool_calls) {
|
|
189
|
+
return {
|
|
190
|
+
type: "start",
|
|
191
|
+
id: data.id,
|
|
192
|
+
model: data.model,
|
|
193
|
+
raw: chunk
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (delta.content) {
|
|
197
|
+
return {
|
|
198
|
+
type: "content",
|
|
199
|
+
id: data.id,
|
|
200
|
+
model: data.model,
|
|
201
|
+
content: {
|
|
202
|
+
type: "content",
|
|
203
|
+
delta: delta.content,
|
|
204
|
+
index: choice.index
|
|
205
|
+
},
|
|
206
|
+
raw: chunk
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (delta.tool_calls && delta.tool_calls.length > 0) {
|
|
210
|
+
const toolCall = delta.tool_calls[0];
|
|
211
|
+
if (toolCall) {
|
|
212
|
+
return {
|
|
213
|
+
type: "tool_call",
|
|
214
|
+
id: data.id,
|
|
215
|
+
model: data.model,
|
|
216
|
+
toolCall: {
|
|
217
|
+
type: "tool_call",
|
|
218
|
+
id: toolCall.id,
|
|
219
|
+
name: toolCall.function?.name,
|
|
220
|
+
arguments: toolCall.function?.arguments,
|
|
221
|
+
index: toolCall.index
|
|
222
|
+
},
|
|
223
|
+
raw: chunk
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (choice.finish_reason) {
|
|
228
|
+
return {
|
|
229
|
+
type: "end",
|
|
230
|
+
id: data.id,
|
|
231
|
+
model: data.model,
|
|
232
|
+
finishReason: mapFinishReason2(choice.finish_reason),
|
|
233
|
+
usage: data.usage ? {
|
|
234
|
+
promptTokens: data.usage.prompt_tokens,
|
|
235
|
+
completionTokens: data.usage.completion_tokens,
|
|
236
|
+
totalTokens: data.usage.total_tokens,
|
|
237
|
+
details: data.usage.completion_tokens_details ? {
|
|
238
|
+
reasoningTokens: data.usage.completion_tokens_details.reasoning_tokens
|
|
239
|
+
} : void 0
|
|
240
|
+
} : void 0,
|
|
241
|
+
raw: chunk
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/outbound/request-builder.ts
|
|
248
|
+
function buildRequest(ir) {
|
|
249
|
+
const messages = [];
|
|
250
|
+
if (ir.system) {
|
|
251
|
+
messages.push({
|
|
252
|
+
role: "system",
|
|
253
|
+
content: ir.system
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
for (const msg of ir.messages) {
|
|
257
|
+
messages.push({
|
|
258
|
+
role: msg.role,
|
|
259
|
+
content: buildContent(msg.content),
|
|
260
|
+
name: msg.name,
|
|
261
|
+
tool_calls: msg.toolCalls,
|
|
262
|
+
tool_call_id: msg.toolCallId
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const request = {
|
|
266
|
+
model: ir.model ?? "gpt-4",
|
|
267
|
+
messages,
|
|
268
|
+
stream: ir.stream
|
|
269
|
+
};
|
|
270
|
+
if (ir.tools && ir.tools.length > 0) {
|
|
271
|
+
request.tools = ir.tools.map((tool) => ({
|
|
272
|
+
type: "function",
|
|
273
|
+
function: {
|
|
274
|
+
name: tool.function.name,
|
|
275
|
+
description: tool.function.description,
|
|
276
|
+
parameters: tool.function.parameters,
|
|
277
|
+
strict: tool.function.strict
|
|
278
|
+
}
|
|
279
|
+
}));
|
|
280
|
+
}
|
|
281
|
+
if (ir.toolChoice) {
|
|
282
|
+
request.tool_choice = ir.toolChoice;
|
|
283
|
+
}
|
|
284
|
+
if (ir.generation) {
|
|
285
|
+
if (ir.generation.temperature !== void 0) {
|
|
286
|
+
request.temperature = ir.generation.temperature;
|
|
287
|
+
}
|
|
288
|
+
if (ir.generation.topP !== void 0) {
|
|
289
|
+
request.top_p = ir.generation.topP;
|
|
290
|
+
}
|
|
291
|
+
if (ir.generation.maxTokens !== void 0) {
|
|
292
|
+
request.max_tokens = ir.generation.maxTokens;
|
|
293
|
+
}
|
|
294
|
+
if (ir.generation.stopSequences && ir.generation.stopSequences.length > 0) {
|
|
295
|
+
request.stop = ir.generation.stopSequences;
|
|
296
|
+
}
|
|
297
|
+
if (ir.generation.presencePenalty !== void 0) {
|
|
298
|
+
request.presence_penalty = ir.generation.presencePenalty;
|
|
299
|
+
}
|
|
300
|
+
if (ir.generation.frequencyPenalty !== void 0) {
|
|
301
|
+
request.frequency_penalty = ir.generation.frequencyPenalty;
|
|
302
|
+
}
|
|
303
|
+
if (ir.generation.n !== void 0) {
|
|
304
|
+
request.n = ir.generation.n;
|
|
305
|
+
}
|
|
306
|
+
if (ir.generation.seed !== void 0) {
|
|
307
|
+
request.seed = ir.generation.seed;
|
|
308
|
+
}
|
|
309
|
+
if (ir.generation.responseFormat) {
|
|
310
|
+
request.response_format = {
|
|
311
|
+
type: ir.generation.responseFormat.type,
|
|
312
|
+
json_schema: ir.generation.responseFormat.jsonSchema
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (ir.generation.logprobs !== void 0) {
|
|
316
|
+
request.logprobs = ir.generation.logprobs;
|
|
317
|
+
}
|
|
318
|
+
if (ir.generation.topLogprobs !== void 0) {
|
|
319
|
+
request.top_logprobs = ir.generation.topLogprobs;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (ir.metadata?.userId) {
|
|
323
|
+
request.user = ir.metadata.userId;
|
|
324
|
+
}
|
|
325
|
+
if (ir.stream) {
|
|
326
|
+
request.stream_options = { include_usage: true };
|
|
327
|
+
}
|
|
328
|
+
return request;
|
|
329
|
+
}
|
|
330
|
+
function buildContent(content) {
|
|
331
|
+
if (typeof content === "string") {
|
|
332
|
+
return content || null;
|
|
333
|
+
}
|
|
334
|
+
if (!content || content.length === 0) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const allText = content.every((part) => part.type === "text");
|
|
338
|
+
if (allText) {
|
|
339
|
+
return content.map((part) => part.type === "text" ? part.text : "").join("");
|
|
340
|
+
}
|
|
341
|
+
return content.map((part) => {
|
|
342
|
+
if (part.type === "text") {
|
|
343
|
+
return { type: "text", text: part.text };
|
|
344
|
+
}
|
|
345
|
+
if (part.type === "image") {
|
|
346
|
+
const imgPart = part;
|
|
347
|
+
if (imgPart.source.type === "url") {
|
|
348
|
+
return {
|
|
349
|
+
type: "image_url",
|
|
350
|
+
image_url: { url: imgPart.source.url }
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
type: "image_url",
|
|
355
|
+
image_url: {
|
|
356
|
+
url: `data:${imgPart.source.mediaType};base64,${imgPart.source.data}`
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
return { type: "text", text: JSON.stringify(part) };
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
function buildResponse(ir) {
|
|
364
|
+
return {
|
|
365
|
+
id: ir.id,
|
|
366
|
+
object: "chat.completion",
|
|
367
|
+
created: ir.created ?? Math.floor(Date.now() / 1e3),
|
|
368
|
+
model: ir.model,
|
|
369
|
+
system_fingerprint: ir.systemFingerprint,
|
|
370
|
+
choices: ir.choices.map((choice) => ({
|
|
371
|
+
index: choice.index,
|
|
372
|
+
message: {
|
|
373
|
+
role: choice.message.role,
|
|
374
|
+
content: contentToString(choice.message.content),
|
|
375
|
+
tool_calls: choice.message.toolCalls
|
|
376
|
+
},
|
|
377
|
+
finish_reason: choice.finishReason ?? "stop",
|
|
378
|
+
logprobs: choice.logprobs
|
|
379
|
+
})),
|
|
380
|
+
usage: ir.usage ? {
|
|
381
|
+
prompt_tokens: ir.usage.promptTokens,
|
|
382
|
+
completion_tokens: ir.usage.completionTokens,
|
|
383
|
+
total_tokens: ir.usage.totalTokens,
|
|
384
|
+
completion_tokens_details: ir.usage.details?.reasoningTokens ? {
|
|
385
|
+
reasoning_tokens: ir.usage.details.reasoningTokens
|
|
386
|
+
} : void 0
|
|
387
|
+
} : void 0
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// src/outbound/stream-builder.ts
|
|
392
|
+
function createStreamBuilder() {
|
|
393
|
+
let chunkId = `chatcmpl-${Date.now()}`;
|
|
394
|
+
let model = "";
|
|
395
|
+
const created = Math.floor(Date.now() / 1e3);
|
|
396
|
+
const toolCallsState = /* @__PURE__ */ new Map();
|
|
397
|
+
return {
|
|
398
|
+
process(event) {
|
|
399
|
+
const events = [];
|
|
400
|
+
if (event.id) chunkId = event.id;
|
|
401
|
+
if (event.model) model = event.model;
|
|
402
|
+
if (event.type === "start") {
|
|
403
|
+
events.push({
|
|
404
|
+
event: "data",
|
|
405
|
+
data: {
|
|
406
|
+
id: chunkId,
|
|
407
|
+
object: "chat.completion.chunk",
|
|
408
|
+
created,
|
|
409
|
+
model,
|
|
410
|
+
choices: [{
|
|
411
|
+
index: 0,
|
|
412
|
+
delta: { role: "assistant", content: "" },
|
|
413
|
+
finish_reason: null
|
|
414
|
+
}]
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
if (event.type === "content" && event.content?.delta) {
|
|
419
|
+
events.push({
|
|
420
|
+
event: "data",
|
|
421
|
+
data: {
|
|
422
|
+
id: chunkId,
|
|
423
|
+
object: "chat.completion.chunk",
|
|
424
|
+
created,
|
|
425
|
+
model,
|
|
426
|
+
choices: [{
|
|
427
|
+
index: 0,
|
|
428
|
+
delta: { content: event.content.delta },
|
|
429
|
+
finish_reason: null
|
|
430
|
+
}]
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
if (event.type === "reasoning" && event.reasoning?.delta) {
|
|
435
|
+
events.push({
|
|
436
|
+
event: "data",
|
|
437
|
+
data: {
|
|
438
|
+
id: chunkId,
|
|
439
|
+
object: "chat.completion.chunk",
|
|
440
|
+
created,
|
|
441
|
+
model,
|
|
442
|
+
choices: [{
|
|
443
|
+
index: 0,
|
|
444
|
+
delta: { content: event.reasoning.delta },
|
|
445
|
+
finish_reason: null
|
|
446
|
+
}]
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
if (event.type === "tool_call" && event.toolCall) {
|
|
451
|
+
const toolIndex = event.toolCall.index ?? 0;
|
|
452
|
+
const toolCallDelta = { index: toolIndex };
|
|
453
|
+
if (event.toolCall.name) {
|
|
454
|
+
toolCallDelta.id = event.toolCall.id || `call_${Date.now()}_${toolIndex}`;
|
|
455
|
+
toolCallDelta.type = "function";
|
|
456
|
+
toolCallDelta.function = { name: event.toolCall.name };
|
|
457
|
+
toolCallsState.set(toolIndex, {
|
|
458
|
+
id: toolCallDelta.id,
|
|
459
|
+
name: event.toolCall.name
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
if (event.toolCall.arguments) {
|
|
463
|
+
toolCallDelta.function = {
|
|
464
|
+
...toolCallDelta.function,
|
|
465
|
+
arguments: event.toolCall.arguments
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
events.push({
|
|
469
|
+
event: "data",
|
|
470
|
+
data: {
|
|
471
|
+
id: chunkId,
|
|
472
|
+
object: "chat.completion.chunk",
|
|
473
|
+
created,
|
|
474
|
+
model,
|
|
475
|
+
choices: [{
|
|
476
|
+
index: 0,
|
|
477
|
+
delta: { tool_calls: [toolCallDelta] },
|
|
478
|
+
finish_reason: null
|
|
479
|
+
}]
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
if (event.type === "end") {
|
|
484
|
+
const finishReason = mapFinishReason3(event.finishReason);
|
|
485
|
+
const finalChunk = {
|
|
486
|
+
id: chunkId,
|
|
487
|
+
object: "chat.completion.chunk",
|
|
488
|
+
created,
|
|
489
|
+
model,
|
|
490
|
+
choices: [{
|
|
491
|
+
index: 0,
|
|
492
|
+
delta: {},
|
|
493
|
+
finish_reason: finishReason
|
|
494
|
+
}]
|
|
495
|
+
};
|
|
496
|
+
if (event.usage) {
|
|
497
|
+
finalChunk.usage = {
|
|
498
|
+
prompt_tokens: event.usage.promptTokens ?? 0,
|
|
499
|
+
completion_tokens: event.usage.completionTokens ?? 0,
|
|
500
|
+
total_tokens: event.usage.totalTokens ?? 0
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
events.push({ event: "data", data: finalChunk });
|
|
504
|
+
}
|
|
505
|
+
if (event.type === "error" && event.error) {
|
|
506
|
+
events.push({
|
|
507
|
+
event: "data",
|
|
508
|
+
data: {
|
|
509
|
+
error: {
|
|
510
|
+
message: event.error.message,
|
|
511
|
+
type: "server_error",
|
|
512
|
+
code: event.error.code
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
return events;
|
|
518
|
+
},
|
|
519
|
+
finalize() {
|
|
520
|
+
return [{ event: "data", data: "[DONE]" }];
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
function mapFinishReason3(reason) {
|
|
525
|
+
if (!reason) return "stop";
|
|
526
|
+
const reasonMap = {
|
|
527
|
+
stop: "stop",
|
|
528
|
+
length: "length",
|
|
529
|
+
tool_calls: "tool_calls",
|
|
530
|
+
content_filter: "content_filter",
|
|
531
|
+
end_turn: "stop",
|
|
532
|
+
max_tokens: "length"
|
|
533
|
+
};
|
|
534
|
+
return reasonMap[reason] ?? "stop";
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// src/adapter.ts
|
|
538
|
+
var openaiAdapter = {
|
|
539
|
+
name: "openai",
|
|
540
|
+
version: "1.0.0",
|
|
541
|
+
capabilities: {
|
|
542
|
+
streaming: true,
|
|
543
|
+
tools: true,
|
|
544
|
+
vision: true,
|
|
545
|
+
multimodal: true,
|
|
546
|
+
systemPrompt: true,
|
|
547
|
+
toolChoice: true,
|
|
548
|
+
reasoning: false,
|
|
549
|
+
webSearch: false,
|
|
550
|
+
jsonMode: true,
|
|
551
|
+
logprobs: true,
|
|
552
|
+
seed: true
|
|
553
|
+
},
|
|
554
|
+
inbound: {
|
|
555
|
+
parseRequest: (request) => {
|
|
556
|
+
return parseRequest(request);
|
|
557
|
+
},
|
|
558
|
+
parseResponse: (response) => {
|
|
559
|
+
return parseResponse(response);
|
|
560
|
+
},
|
|
561
|
+
parseStream: (chunk) => {
|
|
562
|
+
return parseStream(chunk);
|
|
563
|
+
},
|
|
564
|
+
parseError: (error) => {
|
|
565
|
+
return parseError(error);
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
outbound: {
|
|
569
|
+
buildRequest: (ir) => {
|
|
570
|
+
return buildRequest(ir);
|
|
571
|
+
},
|
|
572
|
+
buildResponse: (ir) => {
|
|
573
|
+
return buildResponse(ir);
|
|
574
|
+
},
|
|
575
|
+
createStreamBuilder
|
|
576
|
+
},
|
|
577
|
+
getInfo() {
|
|
578
|
+
return {
|
|
579
|
+
name: this.name,
|
|
580
|
+
version: this.version,
|
|
581
|
+
capabilities: this.capabilities,
|
|
582
|
+
endpoint: {
|
|
583
|
+
baseUrl: "https://api.openai.com",
|
|
584
|
+
chatPath: "/v1/chat/completions",
|
|
585
|
+
modelsPath: "/v1/models"
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
// src/responses/request-parser.ts
|
|
592
|
+
function parseResponsesRequest(request) {
|
|
593
|
+
const req = request;
|
|
594
|
+
let system = req.instructions;
|
|
595
|
+
const { messages, developerSystem } = parseInput(req.input);
|
|
596
|
+
if (developerSystem) {
|
|
597
|
+
system = system ? `${system}
|
|
598
|
+
${developerSystem}` : developerSystem;
|
|
599
|
+
}
|
|
600
|
+
const tools = req.tools?.filter((tool) => tool.type === "function").map((tool) => ({
|
|
601
|
+
type: "function",
|
|
602
|
+
function: {
|
|
603
|
+
name: tool.name,
|
|
604
|
+
description: tool.description,
|
|
605
|
+
parameters: tool.parameters,
|
|
606
|
+
strict: tool.strict
|
|
607
|
+
}
|
|
608
|
+
}));
|
|
609
|
+
const toolChoice = req.tool_choice ? parseToolChoice2(req.tool_choice) : void 0;
|
|
610
|
+
let responseFormat;
|
|
611
|
+
if (req.text?.format) {
|
|
612
|
+
if (req.text.format.type === "json_object") {
|
|
613
|
+
responseFormat = { type: "json_object" };
|
|
614
|
+
} else if (req.text.format.type === "json_schema" && req.text.format.json_schema) {
|
|
615
|
+
responseFormat = {
|
|
616
|
+
type: "json_schema",
|
|
617
|
+
jsonSchema: {
|
|
618
|
+
name: req.text.format.json_schema.name,
|
|
619
|
+
description: req.text.format.json_schema.description,
|
|
620
|
+
schema: req.text.format.json_schema.schema,
|
|
621
|
+
strict: req.text.format.json_schema.strict
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const generation = {
|
|
627
|
+
temperature: req.temperature,
|
|
628
|
+
topP: req.top_p,
|
|
629
|
+
maxTokens: req.max_output_tokens,
|
|
630
|
+
responseFormat
|
|
631
|
+
};
|
|
632
|
+
return {
|
|
633
|
+
messages,
|
|
634
|
+
model: req.model,
|
|
635
|
+
tools: tools && tools.length > 0 ? tools : void 0,
|
|
636
|
+
toolChoice,
|
|
637
|
+
stream: req.stream,
|
|
638
|
+
system,
|
|
639
|
+
generation,
|
|
640
|
+
metadata: {
|
|
641
|
+
userId: req.user,
|
|
642
|
+
...req.metadata || {}
|
|
643
|
+
},
|
|
644
|
+
extensions: {
|
|
645
|
+
responses: {
|
|
646
|
+
truncation: req.truncation,
|
|
647
|
+
store: req.store,
|
|
648
|
+
reasoning: req.reasoning,
|
|
649
|
+
previousResponseId: req.previous_response_id,
|
|
650
|
+
parallelToolCalls: req.parallel_tool_calls,
|
|
651
|
+
text: req.text,
|
|
652
|
+
// Track built-in tools for outbound
|
|
653
|
+
builtInTools: req.tools?.filter((t) => t.type !== "function")
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
raw: request
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function parseInput(input) {
|
|
660
|
+
if (typeof input === "string") {
|
|
661
|
+
return {
|
|
662
|
+
messages: [
|
|
663
|
+
{
|
|
664
|
+
role: "user",
|
|
665
|
+
content: input
|
|
666
|
+
}
|
|
667
|
+
]
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
const messages = [];
|
|
671
|
+
let developerSystem;
|
|
672
|
+
for (const item of input) {
|
|
673
|
+
if (item.type === "item_reference") {
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
const role = item.role;
|
|
677
|
+
const content = item.content;
|
|
678
|
+
if (role === "system" || role === "developer") {
|
|
679
|
+
if (typeof content === "string") {
|
|
680
|
+
developerSystem = developerSystem ? `${developerSystem}
|
|
681
|
+
${content}` : content;
|
|
682
|
+
} else {
|
|
683
|
+
const text = content.filter((p) => p.type === "input_text").map((p) => p.text).join("");
|
|
684
|
+
developerSystem = developerSystem ? `${developerSystem}
|
|
685
|
+
${text}` : text;
|
|
686
|
+
}
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
messages.push({
|
|
690
|
+
role,
|
|
691
|
+
content: parseContent2(content)
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
return { messages, developerSystem };
|
|
695
|
+
}
|
|
696
|
+
function parseContent2(content) {
|
|
697
|
+
if (typeof content === "string") {
|
|
698
|
+
return content;
|
|
699
|
+
}
|
|
700
|
+
return content.map((part) => {
|
|
701
|
+
if (part.type === "input_text") {
|
|
702
|
+
return {
|
|
703
|
+
type: "text",
|
|
704
|
+
text: part.text
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
if (part.type === "input_image") {
|
|
708
|
+
if (part.image_url.startsWith("data:")) {
|
|
709
|
+
const match = part.image_url.match(/^data:([^;]+);base64,(.+)$/);
|
|
710
|
+
if (match) {
|
|
711
|
+
return {
|
|
712
|
+
type: "image",
|
|
713
|
+
source: {
|
|
714
|
+
type: "base64",
|
|
715
|
+
mediaType: match[1],
|
|
716
|
+
data: match[2]
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return {
|
|
722
|
+
type: "image",
|
|
723
|
+
source: {
|
|
724
|
+
type: "url",
|
|
725
|
+
url: part.image_url
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
if (part.type === "input_file") {
|
|
730
|
+
return {
|
|
731
|
+
type: "text",
|
|
732
|
+
text: `[File: ${part.file_id}]`
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
return {
|
|
736
|
+
type: "text",
|
|
737
|
+
text: JSON.stringify(part)
|
|
738
|
+
};
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
function parseToolChoice2(choice) {
|
|
742
|
+
if (typeof choice === "string") {
|
|
743
|
+
return choice;
|
|
744
|
+
}
|
|
745
|
+
return {
|
|
746
|
+
type: "function",
|
|
747
|
+
function: {
|
|
748
|
+
name: choice.name
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// src/responses/response-parser.ts
|
|
754
|
+
function mapFinishReason4(status) {
|
|
755
|
+
const statusMap = {
|
|
756
|
+
completed: "stop",
|
|
757
|
+
failed: "stop",
|
|
758
|
+
incomplete: "length",
|
|
759
|
+
in_progress: "stop"
|
|
760
|
+
};
|
|
761
|
+
return statusMap[status] ?? "stop";
|
|
762
|
+
}
|
|
763
|
+
function parseResponsesResponse(response) {
|
|
764
|
+
const res = response;
|
|
765
|
+
const { content, toolCalls, reasoning } = extractOutputContent(res.output, res.output_text);
|
|
766
|
+
const choices = [
|
|
767
|
+
{
|
|
768
|
+
index: 0,
|
|
769
|
+
message: {
|
|
770
|
+
role: "assistant",
|
|
771
|
+
content,
|
|
772
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
773
|
+
reasoningContent: reasoning || void 0
|
|
774
|
+
},
|
|
775
|
+
finishReason: mapFinishReason4(res.status)
|
|
776
|
+
}
|
|
777
|
+
];
|
|
778
|
+
return {
|
|
779
|
+
id: res.id,
|
|
780
|
+
model: res.model,
|
|
781
|
+
choices,
|
|
782
|
+
created: res.created_at,
|
|
783
|
+
usage: res.usage ? {
|
|
784
|
+
promptTokens: res.usage.input_tokens,
|
|
785
|
+
completionTokens: res.usage.output_tokens,
|
|
786
|
+
totalTokens: res.usage.total_tokens,
|
|
787
|
+
details: {
|
|
788
|
+
...res.usage.output_tokens_details?.reasoning_tokens ? { reasoningTokens: res.usage.output_tokens_details.reasoning_tokens } : {},
|
|
789
|
+
...res.usage.input_tokens_details?.cached_tokens ? { cachedTokens: res.usage.input_tokens_details.cached_tokens } : {}
|
|
790
|
+
}
|
|
791
|
+
} : void 0,
|
|
792
|
+
raw: response
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
function extractOutputContent(output, outputText) {
|
|
796
|
+
let content = "";
|
|
797
|
+
const toolCalls = [];
|
|
798
|
+
let reasoning = null;
|
|
799
|
+
if (outputText && (!output || output.length === 0)) {
|
|
800
|
+
return { content: outputText, toolCalls: [], reasoning: null };
|
|
801
|
+
}
|
|
802
|
+
for (const item of output) {
|
|
803
|
+
if (item.type === "message") {
|
|
804
|
+
for (const part of item.content) {
|
|
805
|
+
if (part.type === "output_text") {
|
|
806
|
+
content += part.text;
|
|
807
|
+
} else if (part.type === "refusal") {
|
|
808
|
+
content += `[Refusal: ${part.refusal}]`;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
} else if (item.type === "function_call") {
|
|
812
|
+
toolCalls.push({
|
|
813
|
+
id: item.call_id,
|
|
814
|
+
type: "function",
|
|
815
|
+
function: {
|
|
816
|
+
name: item.name,
|
|
817
|
+
arguments: item.arguments
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
} else if (item.type === "reasoning") {
|
|
821
|
+
const reasoningItem = item;
|
|
822
|
+
if (reasoningItem.summary && reasoningItem.summary.length > 0) {
|
|
823
|
+
reasoning = reasoningItem.summary.filter((s) => s.type === "summary_text").map((s) => s.text).join("");
|
|
824
|
+
} else if (reasoningItem.content && reasoningItem.content.length > 0) {
|
|
825
|
+
reasoning = reasoningItem.content.filter((c) => c.type === "reasoning_text").map((c) => c.text).join("");
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
if (!content && outputText) {
|
|
830
|
+
content = outputText;
|
|
831
|
+
}
|
|
832
|
+
return { content, toolCalls, reasoning };
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// src/responses/stream-parser.ts
|
|
836
|
+
function mapFinishReason5(status) {
|
|
837
|
+
const statusMap = {
|
|
838
|
+
completed: "stop",
|
|
839
|
+
failed: "stop",
|
|
840
|
+
incomplete: "length"
|
|
841
|
+
};
|
|
842
|
+
return statusMap[status] ?? "stop";
|
|
843
|
+
}
|
|
844
|
+
function parseResponsesStream(chunk) {
|
|
845
|
+
const event = chunk;
|
|
846
|
+
switch (event.type) {
|
|
847
|
+
case "response.created":
|
|
848
|
+
case "response.in_progress":
|
|
849
|
+
return {
|
|
850
|
+
type: "start",
|
|
851
|
+
id: event.response?.id ?? "",
|
|
852
|
+
model: event.response?.model ?? "",
|
|
853
|
+
raw: chunk
|
|
854
|
+
};
|
|
855
|
+
case "response.output_text.delta":
|
|
856
|
+
return {
|
|
857
|
+
type: "content",
|
|
858
|
+
id: "",
|
|
859
|
+
model: "",
|
|
860
|
+
content: {
|
|
861
|
+
type: "content",
|
|
862
|
+
delta: event.delta ?? "",
|
|
863
|
+
index: event.output_index ?? 0
|
|
864
|
+
},
|
|
865
|
+
raw: chunk
|
|
866
|
+
};
|
|
867
|
+
case "response.reasoning_summary_text.delta":
|
|
868
|
+
return {
|
|
869
|
+
type: "reasoning",
|
|
870
|
+
id: "",
|
|
871
|
+
model: "",
|
|
872
|
+
reasoning: {
|
|
873
|
+
type: "reasoning",
|
|
874
|
+
delta: event.delta ?? ""
|
|
875
|
+
},
|
|
876
|
+
raw: chunk
|
|
877
|
+
};
|
|
878
|
+
case "response.function_call_arguments.delta":
|
|
879
|
+
return {
|
|
880
|
+
type: "tool_call",
|
|
881
|
+
id: "",
|
|
882
|
+
model: "",
|
|
883
|
+
toolCall: {
|
|
884
|
+
type: "tool_call",
|
|
885
|
+
arguments: event.delta,
|
|
886
|
+
index: event.output_index ?? 0
|
|
887
|
+
},
|
|
888
|
+
raw: chunk
|
|
889
|
+
};
|
|
890
|
+
case "response.output_item.added":
|
|
891
|
+
if (event.item?.type === "function_call") {
|
|
892
|
+
return {
|
|
893
|
+
type: "tool_call",
|
|
894
|
+
id: "",
|
|
895
|
+
model: "",
|
|
896
|
+
toolCall: {
|
|
897
|
+
type: "tool_call",
|
|
898
|
+
id: event.item.call_id,
|
|
899
|
+
name: event.item.name,
|
|
900
|
+
index: event.output_index ?? 0
|
|
901
|
+
},
|
|
902
|
+
raw: chunk
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
return null;
|
|
906
|
+
case "response.completed":
|
|
907
|
+
case "response.failed":
|
|
908
|
+
case "response.incomplete":
|
|
909
|
+
return {
|
|
910
|
+
type: "end",
|
|
911
|
+
id: event.response?.id ?? "",
|
|
912
|
+
model: event.response?.model ?? "",
|
|
913
|
+
finishReason: mapFinishReason5(event.response?.status ?? "completed"),
|
|
914
|
+
usage: event.response?.usage ? {
|
|
915
|
+
promptTokens: event.response.usage.input_tokens,
|
|
916
|
+
completionTokens: event.response.usage.output_tokens,
|
|
917
|
+
totalTokens: event.response.usage.total_tokens,
|
|
918
|
+
details: event.response.usage.output_tokens_details?.reasoning_tokens ? {
|
|
919
|
+
reasoningTokens: event.response.usage.output_tokens_details.reasoning_tokens
|
|
920
|
+
} : void 0
|
|
921
|
+
} : void 0,
|
|
922
|
+
raw: chunk
|
|
923
|
+
};
|
|
924
|
+
case "error":
|
|
925
|
+
return {
|
|
926
|
+
type: "error",
|
|
927
|
+
id: "",
|
|
928
|
+
model: "",
|
|
929
|
+
error: {
|
|
930
|
+
type: "api",
|
|
931
|
+
message: event.error?.message ?? "Unknown error",
|
|
932
|
+
code: event.error?.code
|
|
933
|
+
},
|
|
934
|
+
raw: chunk
|
|
935
|
+
};
|
|
936
|
+
default:
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// src/responses/request-builder.ts
|
|
942
|
+
function buildResponsesRequest(ir) {
|
|
943
|
+
const input = [];
|
|
944
|
+
for (const msg of ir.messages) {
|
|
945
|
+
if (msg.role === "system") {
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
input.push({
|
|
949
|
+
type: "message",
|
|
950
|
+
role: msg.role,
|
|
951
|
+
content: buildContent2(msg.content)
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
const request = {
|
|
955
|
+
model: ir.model ?? "gpt-5",
|
|
956
|
+
input: input.length === 1 && input[0]?.type === "message" && typeof input[0].content === "string" && input[0].role === "user" ? input[0].content : input,
|
|
957
|
+
stream: ir.stream
|
|
958
|
+
};
|
|
959
|
+
if (ir.system) {
|
|
960
|
+
request.instructions = ir.system;
|
|
961
|
+
}
|
|
962
|
+
const tools = [];
|
|
963
|
+
if (ir.tools && ir.tools.length > 0) {
|
|
964
|
+
for (const tool of ir.tools) {
|
|
965
|
+
tools.push({
|
|
966
|
+
type: "function",
|
|
967
|
+
name: tool.function.name,
|
|
968
|
+
description: tool.function.description,
|
|
969
|
+
parameters: tool.function.parameters,
|
|
970
|
+
strict: tool.function.strict
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
const responsesExt = ir.extensions?.responses;
|
|
975
|
+
const builtInTools = responsesExt?.builtInTools;
|
|
976
|
+
if (builtInTools) {
|
|
977
|
+
tools.push(...builtInTools);
|
|
978
|
+
}
|
|
979
|
+
if (tools.length > 0) {
|
|
980
|
+
request.tools = tools;
|
|
981
|
+
}
|
|
982
|
+
if (ir.toolChoice) {
|
|
983
|
+
if (typeof ir.toolChoice === "string") {
|
|
984
|
+
request.tool_choice = ir.toolChoice;
|
|
985
|
+
} else if (ir.toolChoice.type === "function" && ir.toolChoice.function) {
|
|
986
|
+
request.tool_choice = {
|
|
987
|
+
type: "function",
|
|
988
|
+
name: ir.toolChoice.function.name
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
if (ir.generation) {
|
|
993
|
+
if (ir.generation.temperature !== void 0) {
|
|
994
|
+
request.temperature = ir.generation.temperature;
|
|
995
|
+
}
|
|
996
|
+
if (ir.generation.topP !== void 0) {
|
|
997
|
+
request.top_p = ir.generation.topP;
|
|
998
|
+
}
|
|
999
|
+
if (ir.generation.maxTokens !== void 0) {
|
|
1000
|
+
request.max_output_tokens = ir.generation.maxTokens;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (ir.metadata?.userId) {
|
|
1004
|
+
request.user = ir.metadata.userId;
|
|
1005
|
+
}
|
|
1006
|
+
if (responsesExt) {
|
|
1007
|
+
if (responsesExt.truncation !== void 0) {
|
|
1008
|
+
request.truncation = responsesExt.truncation;
|
|
1009
|
+
}
|
|
1010
|
+
if (responsesExt.store !== void 0) {
|
|
1011
|
+
request.store = responsesExt.store;
|
|
1012
|
+
}
|
|
1013
|
+
if (responsesExt.reasoning !== void 0) {
|
|
1014
|
+
request.reasoning = responsesExt.reasoning;
|
|
1015
|
+
}
|
|
1016
|
+
if (responsesExt.previousResponseId !== void 0) {
|
|
1017
|
+
request.previous_response_id = responsesExt.previousResponseId;
|
|
1018
|
+
}
|
|
1019
|
+
if (responsesExt.parallelToolCalls !== void 0) {
|
|
1020
|
+
request.parallel_tool_calls = responsesExt.parallelToolCalls;
|
|
1021
|
+
}
|
|
1022
|
+
if (responsesExt.text !== void 0) {
|
|
1023
|
+
request.text = responsesExt.text;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (ir.generation?.responseFormat) {
|
|
1027
|
+
if (ir.generation.responseFormat.type === "json_object") {
|
|
1028
|
+
request.text = {
|
|
1029
|
+
format: { type: "json_object" }
|
|
1030
|
+
};
|
|
1031
|
+
} else if (ir.generation.responseFormat.type === "json_schema" && ir.generation.responseFormat.jsonSchema) {
|
|
1032
|
+
request.text = {
|
|
1033
|
+
format: {
|
|
1034
|
+
type: "json_schema",
|
|
1035
|
+
json_schema: {
|
|
1036
|
+
name: ir.generation.responseFormat.jsonSchema.name,
|
|
1037
|
+
description: ir.generation.responseFormat.jsonSchema.description,
|
|
1038
|
+
schema: ir.generation.responseFormat.jsonSchema.schema,
|
|
1039
|
+
strict: ir.generation.responseFormat.jsonSchema.strict
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return request;
|
|
1046
|
+
}
|
|
1047
|
+
function buildContent2(content) {
|
|
1048
|
+
if (typeof content === "string") {
|
|
1049
|
+
return content;
|
|
1050
|
+
}
|
|
1051
|
+
if (!content || content.length === 0) {
|
|
1052
|
+
return "";
|
|
1053
|
+
}
|
|
1054
|
+
const allText = content.every((part) => part.type === "text");
|
|
1055
|
+
if (allText) {
|
|
1056
|
+
return content.map((part) => part.type === "text" ? part.text : "").join("");
|
|
1057
|
+
}
|
|
1058
|
+
return content.map((part) => {
|
|
1059
|
+
if (part.type === "text") {
|
|
1060
|
+
return { type: "input_text", text: part.text };
|
|
1061
|
+
}
|
|
1062
|
+
if (part.type === "image") {
|
|
1063
|
+
const imgPart = part;
|
|
1064
|
+
if (imgPart.source.type === "url") {
|
|
1065
|
+
return {
|
|
1066
|
+
type: "input_image",
|
|
1067
|
+
image_url: imgPart.source.url
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
return {
|
|
1071
|
+
type: "input_image",
|
|
1072
|
+
image_url: `data:${imgPart.source.mediaType};base64,${imgPart.source.data}`
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
return { type: "input_text", text: JSON.stringify(part) };
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// src/responses/response-builder.ts
|
|
1080
|
+
function contentToOutput(content) {
|
|
1081
|
+
if (typeof content === "string") {
|
|
1082
|
+
return content ? [{ type: "output_text", text: content }] : [];
|
|
1083
|
+
}
|
|
1084
|
+
if (!content || content.length === 0) {
|
|
1085
|
+
return [];
|
|
1086
|
+
}
|
|
1087
|
+
const text = content.filter((part) => part.type === "text").map((part) => part.type === "text" ? part.text : "").join("");
|
|
1088
|
+
return text ? [{ type: "output_text", text }] : [];
|
|
1089
|
+
}
|
|
1090
|
+
function buildResponsesResponse(ir) {
|
|
1091
|
+
const output = [];
|
|
1092
|
+
const choice = ir.choices[0];
|
|
1093
|
+
if (choice) {
|
|
1094
|
+
const messageContent = contentToOutput(choice.message.content);
|
|
1095
|
+
if (messageContent.length > 0) {
|
|
1096
|
+
output.push({
|
|
1097
|
+
type: "message",
|
|
1098
|
+
id: `msg_${ir.id}`,
|
|
1099
|
+
role: "assistant",
|
|
1100
|
+
content: messageContent,
|
|
1101
|
+
status: "completed"
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
if (choice.message.toolCalls) {
|
|
1105
|
+
for (const toolCall of choice.message.toolCalls) {
|
|
1106
|
+
output.push({
|
|
1107
|
+
type: "function_call",
|
|
1108
|
+
id: `fc_${toolCall.id}`,
|
|
1109
|
+
call_id: toolCall.id,
|
|
1110
|
+
name: toolCall.function.name,
|
|
1111
|
+
arguments: toolCall.function.arguments,
|
|
1112
|
+
status: "completed"
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
if (choice.message.reasoningContent) {
|
|
1117
|
+
output.unshift({
|
|
1118
|
+
type: "reasoning",
|
|
1119
|
+
id: `rs_${ir.id}`,
|
|
1120
|
+
content: [{ type: "reasoning_text", text: choice.message.reasoningContent }]
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
return {
|
|
1125
|
+
id: ir.id,
|
|
1126
|
+
object: "response",
|
|
1127
|
+
created_at: ir.created ?? Math.floor(Date.now() / 1e3),
|
|
1128
|
+
model: ir.model,
|
|
1129
|
+
status: "completed",
|
|
1130
|
+
output,
|
|
1131
|
+
usage: ir.usage ? {
|
|
1132
|
+
input_tokens: ir.usage.promptTokens,
|
|
1133
|
+
output_tokens: ir.usage.completionTokens,
|
|
1134
|
+
total_tokens: ir.usage.totalTokens,
|
|
1135
|
+
output_tokens_details: ir.usage.details?.reasoningTokens ? {
|
|
1136
|
+
reasoning_tokens: ir.usage.details.reasoningTokens
|
|
1137
|
+
} : void 0
|
|
1138
|
+
} : void 0
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// src/responses/stream-builder.ts
|
|
1143
|
+
function createResponsesStreamBuilder() {
|
|
1144
|
+
let responseId = "";
|
|
1145
|
+
let model = "";
|
|
1146
|
+
let currentOutputIndex = -1;
|
|
1147
|
+
let reasoningItemAdded = false;
|
|
1148
|
+
let reasoningPartAdded = false;
|
|
1149
|
+
let messageItemAdded = false;
|
|
1150
|
+
let contentPartAdded = false;
|
|
1151
|
+
let accumulatedText = "";
|
|
1152
|
+
let accumulatedReasoning = "";
|
|
1153
|
+
let sequenceNumber = 0;
|
|
1154
|
+
let reasoningItemId = "";
|
|
1155
|
+
let messageItemId = "";
|
|
1156
|
+
function nextSeq() {
|
|
1157
|
+
return sequenceNumber++;
|
|
1158
|
+
}
|
|
1159
|
+
function ensureReasoningItemAdded(events) {
|
|
1160
|
+
if (!reasoningItemAdded) {
|
|
1161
|
+
reasoningItemAdded = true;
|
|
1162
|
+
currentOutputIndex++;
|
|
1163
|
+
reasoningItemId = `rs_${responseId || Date.now()}`;
|
|
1164
|
+
events.push({
|
|
1165
|
+
event: "response.output_item.added",
|
|
1166
|
+
data: {
|
|
1167
|
+
type: "response.output_item.added",
|
|
1168
|
+
output_index: currentOutputIndex,
|
|
1169
|
+
item_id: reasoningItemId,
|
|
1170
|
+
sequence_number: nextSeq(),
|
|
1171
|
+
item: {
|
|
1172
|
+
type: "reasoning",
|
|
1173
|
+
id: reasoningItemId,
|
|
1174
|
+
summary: [],
|
|
1175
|
+
status: "in_progress"
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
function ensureReasoningPartAdded(events) {
|
|
1182
|
+
if (!reasoningPartAdded) {
|
|
1183
|
+
reasoningPartAdded = true;
|
|
1184
|
+
events.push({
|
|
1185
|
+
event: "response.reasoning_summary_part.added",
|
|
1186
|
+
data: {
|
|
1187
|
+
type: "response.reasoning_summary_part.added",
|
|
1188
|
+
item_id: reasoningItemId,
|
|
1189
|
+
output_index: currentOutputIndex,
|
|
1190
|
+
summary_index: 0,
|
|
1191
|
+
sequence_number: nextSeq(),
|
|
1192
|
+
part: {
|
|
1193
|
+
type: "summary_text",
|
|
1194
|
+
text: ""
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
function ensureMessageItemAdded(events) {
|
|
1201
|
+
if (!messageItemAdded) {
|
|
1202
|
+
messageItemAdded = true;
|
|
1203
|
+
currentOutputIndex++;
|
|
1204
|
+
messageItemId = `msg_${responseId || Date.now()}`;
|
|
1205
|
+
events.push({
|
|
1206
|
+
event: "response.output_item.added",
|
|
1207
|
+
data: {
|
|
1208
|
+
type: "response.output_item.added",
|
|
1209
|
+
output_index: currentOutputIndex,
|
|
1210
|
+
item_id: messageItemId,
|
|
1211
|
+
sequence_number: nextSeq(),
|
|
1212
|
+
item: {
|
|
1213
|
+
type: "message",
|
|
1214
|
+
id: messageItemId,
|
|
1215
|
+
role: "assistant",
|
|
1216
|
+
content: [],
|
|
1217
|
+
status: "in_progress"
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
function ensureContentPartAdded(events) {
|
|
1224
|
+
if (!contentPartAdded) {
|
|
1225
|
+
contentPartAdded = true;
|
|
1226
|
+
events.push({
|
|
1227
|
+
event: "response.content_part.added",
|
|
1228
|
+
data: {
|
|
1229
|
+
type: "response.content_part.added",
|
|
1230
|
+
output_index: currentOutputIndex,
|
|
1231
|
+
item_id: messageItemId,
|
|
1232
|
+
content_index: 0,
|
|
1233
|
+
sequence_number: nextSeq(),
|
|
1234
|
+
part: {
|
|
1235
|
+
type: "output_text",
|
|
1236
|
+
text: ""
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
return {
|
|
1243
|
+
process(event) {
|
|
1244
|
+
const events = [];
|
|
1245
|
+
if (event.id) responseId = event.id || `resp_${Date.now()}`;
|
|
1246
|
+
if (event.model) model = event.model || "";
|
|
1247
|
+
if (event.type === "start") {
|
|
1248
|
+
events.push({
|
|
1249
|
+
event: "response.created",
|
|
1250
|
+
data: {
|
|
1251
|
+
type: "response.created",
|
|
1252
|
+
sequence_number: nextSeq(),
|
|
1253
|
+
response: {
|
|
1254
|
+
id: responseId,
|
|
1255
|
+
object: "response",
|
|
1256
|
+
created_at: Math.floor(Date.now() / 1e3),
|
|
1257
|
+
model,
|
|
1258
|
+
status: "in_progress",
|
|
1259
|
+
output: []
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
if (event.type === "reasoning" && event.reasoning?.delta) {
|
|
1265
|
+
ensureReasoningItemAdded(events);
|
|
1266
|
+
ensureReasoningPartAdded(events);
|
|
1267
|
+
accumulatedReasoning += event.reasoning.delta;
|
|
1268
|
+
events.push({
|
|
1269
|
+
event: "response.reasoning_summary_text.delta",
|
|
1270
|
+
data: {
|
|
1271
|
+
type: "response.reasoning_summary_text.delta",
|
|
1272
|
+
item_id: reasoningItemId,
|
|
1273
|
+
output_index: 0,
|
|
1274
|
+
// Reasoning is always at output_index 0
|
|
1275
|
+
summary_index: 0,
|
|
1276
|
+
sequence_number: nextSeq(),
|
|
1277
|
+
delta: event.reasoning.delta
|
|
1278
|
+
}
|
|
1279
|
+
});
|
|
1280
|
+
}
|
|
1281
|
+
if (event.type === "content" && event.content?.delta) {
|
|
1282
|
+
ensureMessageItemAdded(events);
|
|
1283
|
+
ensureContentPartAdded(events);
|
|
1284
|
+
accumulatedText += event.content.delta;
|
|
1285
|
+
events.push({
|
|
1286
|
+
event: "response.output_text.delta",
|
|
1287
|
+
data: {
|
|
1288
|
+
type: "response.output_text.delta",
|
|
1289
|
+
output_index: currentOutputIndex,
|
|
1290
|
+
item_id: messageItemId,
|
|
1291
|
+
content_index: 0,
|
|
1292
|
+
sequence_number: nextSeq(),
|
|
1293
|
+
delta: event.content.delta
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
if (event.type === "tool_call" && event.toolCall) {
|
|
1298
|
+
if (event.toolCall.name) {
|
|
1299
|
+
currentOutputIndex++;
|
|
1300
|
+
const toolItemId = `fc_${event.toolCall.id || currentOutputIndex}`;
|
|
1301
|
+
events.push({
|
|
1302
|
+
event: "response.output_item.added",
|
|
1303
|
+
data: {
|
|
1304
|
+
type: "response.output_item.added",
|
|
1305
|
+
output_index: currentOutputIndex,
|
|
1306
|
+
item_id: toolItemId,
|
|
1307
|
+
sequence_number: nextSeq(),
|
|
1308
|
+
item: {
|
|
1309
|
+
type: "function_call",
|
|
1310
|
+
id: toolItemId,
|
|
1311
|
+
call_id: event.toolCall.id || `call_${currentOutputIndex}`,
|
|
1312
|
+
name: event.toolCall.name,
|
|
1313
|
+
arguments: "",
|
|
1314
|
+
status: "in_progress"
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
if (event.toolCall.arguments) {
|
|
1320
|
+
events.push({
|
|
1321
|
+
event: "response.function_call_arguments.delta",
|
|
1322
|
+
data: {
|
|
1323
|
+
type: "response.function_call_arguments.delta",
|
|
1324
|
+
output_index: currentOutputIndex,
|
|
1325
|
+
item_id: messageItemId,
|
|
1326
|
+
sequence_number: nextSeq(),
|
|
1327
|
+
delta: event.toolCall.arguments
|
|
1328
|
+
}
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
if (event.type === "end") {
|
|
1333
|
+
const output = [];
|
|
1334
|
+
if (reasoningItemAdded) {
|
|
1335
|
+
output.push({
|
|
1336
|
+
type: "reasoning",
|
|
1337
|
+
id: reasoningItemId,
|
|
1338
|
+
summary: accumulatedReasoning ? [{ type: "summary_text", text: accumulatedReasoning }] : [],
|
|
1339
|
+
status: "completed"
|
|
1340
|
+
});
|
|
1341
|
+
events.push({
|
|
1342
|
+
event: "response.reasoning_summary_text.done",
|
|
1343
|
+
data: {
|
|
1344
|
+
type: "response.reasoning_summary_text.done",
|
|
1345
|
+
item_id: reasoningItemId,
|
|
1346
|
+
output_index: 0,
|
|
1347
|
+
summary_index: 0,
|
|
1348
|
+
sequence_number: nextSeq(),
|
|
1349
|
+
text: accumulatedReasoning
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1352
|
+
events.push({
|
|
1353
|
+
event: "response.reasoning_summary_part.done",
|
|
1354
|
+
data: {
|
|
1355
|
+
type: "response.reasoning_summary_part.done",
|
|
1356
|
+
item_id: reasoningItemId,
|
|
1357
|
+
output_index: 0,
|
|
1358
|
+
summary_index: 0,
|
|
1359
|
+
sequence_number: nextSeq(),
|
|
1360
|
+
part: {
|
|
1361
|
+
type: "summary_text",
|
|
1362
|
+
text: accumulatedReasoning
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
});
|
|
1366
|
+
events.push({
|
|
1367
|
+
event: "response.output_item.done",
|
|
1368
|
+
data: {
|
|
1369
|
+
type: "response.output_item.done",
|
|
1370
|
+
output_index: 0,
|
|
1371
|
+
item_id: reasoningItemId,
|
|
1372
|
+
sequence_number: nextSeq(),
|
|
1373
|
+
item: {
|
|
1374
|
+
type: "reasoning",
|
|
1375
|
+
id: reasoningItemId,
|
|
1376
|
+
summary: [{ type: "summary_text", text: accumulatedReasoning }],
|
|
1377
|
+
status: "completed"
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
if (messageItemAdded) {
|
|
1383
|
+
const messageOutputIndex = reasoningItemAdded ? 1 : 0;
|
|
1384
|
+
output.push({
|
|
1385
|
+
type: "message",
|
|
1386
|
+
id: messageItemId,
|
|
1387
|
+
role: "assistant",
|
|
1388
|
+
content: accumulatedText ? [{ type: "output_text", text: accumulatedText }] : [],
|
|
1389
|
+
status: "completed"
|
|
1390
|
+
});
|
|
1391
|
+
if (contentPartAdded) {
|
|
1392
|
+
events.push({
|
|
1393
|
+
event: "response.output_text.done",
|
|
1394
|
+
data: {
|
|
1395
|
+
type: "response.output_text.done",
|
|
1396
|
+
output_index: messageOutputIndex,
|
|
1397
|
+
item_id: messageItemId,
|
|
1398
|
+
content_index: 0,
|
|
1399
|
+
sequence_number: nextSeq(),
|
|
1400
|
+
text: accumulatedText
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
events.push({
|
|
1404
|
+
event: "response.content_part.done",
|
|
1405
|
+
data: {
|
|
1406
|
+
type: "response.content_part.done",
|
|
1407
|
+
output_index: messageOutputIndex,
|
|
1408
|
+
item_id: messageItemId,
|
|
1409
|
+
content_index: 0,
|
|
1410
|
+
sequence_number: nextSeq(),
|
|
1411
|
+
part: {
|
|
1412
|
+
type: "output_text",
|
|
1413
|
+
text: accumulatedText
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
events.push({
|
|
1419
|
+
event: "response.output_item.done",
|
|
1420
|
+
data: {
|
|
1421
|
+
type: "response.output_item.done",
|
|
1422
|
+
output_index: messageOutputIndex,
|
|
1423
|
+
item_id: messageItemId,
|
|
1424
|
+
sequence_number: nextSeq(),
|
|
1425
|
+
item: {
|
|
1426
|
+
type: "message",
|
|
1427
|
+
id: messageItemId,
|
|
1428
|
+
role: "assistant",
|
|
1429
|
+
content: accumulatedText ? [{ type: "output_text", text: accumulatedText }] : [],
|
|
1430
|
+
status: "completed"
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
events.push({
|
|
1436
|
+
event: "response.completed",
|
|
1437
|
+
data: {
|
|
1438
|
+
type: "response.completed",
|
|
1439
|
+
sequence_number: nextSeq(),
|
|
1440
|
+
response: {
|
|
1441
|
+
id: responseId,
|
|
1442
|
+
object: "response",
|
|
1443
|
+
created_at: Math.floor(Date.now() / 1e3),
|
|
1444
|
+
model,
|
|
1445
|
+
status: "completed",
|
|
1446
|
+
output,
|
|
1447
|
+
output_text: accumulatedText || void 0,
|
|
1448
|
+
usage: event.usage ? {
|
|
1449
|
+
input_tokens: event.usage.promptTokens,
|
|
1450
|
+
output_tokens: event.usage.completionTokens,
|
|
1451
|
+
total_tokens: event.usage.totalTokens,
|
|
1452
|
+
output_tokens_details: event.usage.details?.reasoningTokens ? {
|
|
1453
|
+
reasoning_tokens: event.usage.details.reasoningTokens
|
|
1454
|
+
} : void 0
|
|
1455
|
+
} : void 0
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
}
|
|
1460
|
+
if (event.type === "error" && event.error) {
|
|
1461
|
+
events.push({
|
|
1462
|
+
event: "error",
|
|
1463
|
+
data: {
|
|
1464
|
+
type: "error",
|
|
1465
|
+
sequence_number: nextSeq(),
|
|
1466
|
+
error: {
|
|
1467
|
+
type: "api_error",
|
|
1468
|
+
code: event.error.code || "unknown",
|
|
1469
|
+
message: event.error.message
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
return events;
|
|
1475
|
+
},
|
|
1476
|
+
finalize() {
|
|
1477
|
+
return [];
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// src/responses-adapter.ts
|
|
1483
|
+
var openaiResponsesAdapter = {
|
|
1484
|
+
name: "openai-responses",
|
|
1485
|
+
version: "1.0.0",
|
|
1486
|
+
capabilities: {
|
|
1487
|
+
streaming: true,
|
|
1488
|
+
tools: true,
|
|
1489
|
+
vision: true,
|
|
1490
|
+
multimodal: true,
|
|
1491
|
+
systemPrompt: true,
|
|
1492
|
+
toolChoice: true,
|
|
1493
|
+
reasoning: true,
|
|
1494
|
+
// Responses API supports reasoning models (o3, o4-mini)
|
|
1495
|
+
webSearch: true,
|
|
1496
|
+
// Built-in web search tool
|
|
1497
|
+
jsonMode: true,
|
|
1498
|
+
// Supported via text.format
|
|
1499
|
+
logprobs: false,
|
|
1500
|
+
// Not supported in Responses API
|
|
1501
|
+
seed: false
|
|
1502
|
+
// Not supported in Responses API
|
|
1503
|
+
},
|
|
1504
|
+
inbound: {
|
|
1505
|
+
parseRequest: (request) => {
|
|
1506
|
+
return parseResponsesRequest(request);
|
|
1507
|
+
},
|
|
1508
|
+
parseResponse: (response) => {
|
|
1509
|
+
return parseResponsesResponse(response);
|
|
1510
|
+
},
|
|
1511
|
+
parseStream: (chunk) => {
|
|
1512
|
+
return parseResponsesStream(chunk);
|
|
1513
|
+
},
|
|
1514
|
+
parseError: (error) => {
|
|
1515
|
+
return parseError(error);
|
|
1516
|
+
}
|
|
1517
|
+
},
|
|
1518
|
+
outbound: {
|
|
1519
|
+
buildRequest: (ir) => {
|
|
1520
|
+
return buildResponsesRequest(ir);
|
|
1521
|
+
},
|
|
1522
|
+
buildResponse: (ir) => {
|
|
1523
|
+
return buildResponsesResponse(ir);
|
|
1524
|
+
},
|
|
1525
|
+
createStreamBuilder: createResponsesStreamBuilder
|
|
1526
|
+
},
|
|
1527
|
+
getInfo() {
|
|
1528
|
+
return {
|
|
1529
|
+
name: this.name,
|
|
1530
|
+
version: this.version,
|
|
1531
|
+
capabilities: this.capabilities,
|
|
1532
|
+
endpoint: {
|
|
1533
|
+
baseUrl: "https://api.openai.com",
|
|
1534
|
+
chatPath: "/v1/responses",
|
|
1535
|
+
modelsPath: "/v1/models"
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
};
|
|
1540
|
+
|
|
1541
|
+
export { openaiAdapter, openaiResponsesAdapter };
|
|
1542
|
+
//# sourceMappingURL=index.js.map
|
|
1543
|
+
//# sourceMappingURL=index.js.map
|