@omnicross/core 0.1.0
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/LICENSE +21 -0
- package/NOTICE +57 -0
- package/README.md +15 -0
- package/dist/ApiKeyPoolService-BmMkau07.d.cts +170 -0
- package/dist/ApiKeyPoolService-BmMkau07.d.ts +170 -0
- package/dist/ProviderProxy-f_8ziIhW.d.cts +120 -0
- package/dist/ProviderProxy-vjt8sQQk.d.ts +120 -0
- package/dist/SubscriptionAuthSource-Cr4fVEYY.d.cts +264 -0
- package/dist/SubscriptionAuthSource-D89zmiSS.d.ts +264 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.cjs +218 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.d.cts +68 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.d.ts +68 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.js +189 -0
- package/dist/completion/ApiKeyPoolService.cjs +331 -0
- package/dist/completion/ApiKeyPoolService.d.cts +2 -0
- package/dist/completion/ApiKeyPoolService.d.ts +2 -0
- package/dist/completion/ApiKeyPoolService.js +306 -0
- package/dist/completion.cjs +4027 -0
- package/dist/completion.d.cts +17 -0
- package/dist/completion.d.ts +17 -0
- package/dist/completion.js +3983 -0
- package/dist/index-BTSmc9Sm.d.ts +645 -0
- package/dist/index-DXazdTzZ.d.cts +645 -0
- package/dist/index.cjs +10428 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +10339 -0
- package/dist/outbound-api/subscriptionRegistryPort.cjs +38 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +73 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +73 -0
- package/dist/outbound-api/subscriptionRegistryPort.js +12 -0
- package/dist/outbound-api.cjs +5264 -0
- package/dist/outbound-api.d.cts +320 -0
- package/dist/outbound-api.d.ts +320 -0
- package/dist/outbound-api.js +5218 -0
- package/dist/pipeline/SubscriptionAuthSource.cjs +131 -0
- package/dist/pipeline/SubscriptionAuthSource.d.cts +3 -0
- package/dist/pipeline/SubscriptionAuthSource.d.ts +3 -0
- package/dist/pipeline/SubscriptionAuthSource.js +103 -0
- package/dist/pipeline/SubscriptionAuthStrategy.cjs +18 -0
- package/dist/pipeline/SubscriptionAuthStrategy.d.cts +61 -0
- package/dist/pipeline/SubscriptionAuthStrategy.d.ts +61 -0
- package/dist/pipeline/SubscriptionAuthStrategy.js +0 -0
- package/dist/ports/gemini-code-assist-resolver.cjs +38 -0
- package/dist/ports/gemini-code-assist-resolver.d.cts +26 -0
- package/dist/ports/gemini-code-assist-resolver.d.ts +26 -0
- package/dist/ports/gemini-code-assist-resolver.js +12 -0
- package/dist/ports.cjs +18 -0
- package/dist/ports.d.cts +15 -0
- package/dist/ports.d.ts +15 -0
- package/dist/ports.js +0 -0
- package/dist/provider-proxy/ingress/providerProxyShared.cjs +2958 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +77 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +77 -0
- package/dist/provider-proxy/ingress/providerProxyShared.js +2925 -0
- package/dist/provider-proxy/matchText.cjs +73 -0
- package/dist/provider-proxy/matchText.d.cts +47 -0
- package/dist/provider-proxy/matchText.d.ts +47 -0
- package/dist/provider-proxy/matchText.js +45 -0
- package/dist/provider-proxy/types.cjs +18 -0
- package/dist/provider-proxy/types.d.cts +12 -0
- package/dist/provider-proxy/types.d.ts +12 -0
- package/dist/provider-proxy/types.js +0 -0
- package/dist/provider-proxy.cjs +4667 -0
- package/dist/provider-proxy.d.cts +69 -0
- package/dist/provider-proxy.d.ts +69 -0
- package/dist/provider-proxy.js +4636 -0
- package/dist/serializeError.cjs +82 -0
- package/dist/serializeError.d.cts +24 -0
- package/dist/serializeError.d.ts +24 -0
- package/dist/serializeError.js +57 -0
- package/dist/sse-parser.cjs +456 -0
- package/dist/sse-parser.d.cts +143 -0
- package/dist/sse-parser.d.ts +143 -0
- package/dist/sse-parser.js +430 -0
- package/dist/transformer/TransformerChainExecutor.cjs +321 -0
- package/dist/transformer/TransformerChainExecutor.d.cts +104 -0
- package/dist/transformer/TransformerChainExecutor.d.ts +104 -0
- package/dist/transformer/TransformerChainExecutor.js +294 -0
- package/dist/transformer/TransformerService.cjs +290 -0
- package/dist/transformer/TransformerService.d.cts +138 -0
- package/dist/transformer/TransformerService.d.ts +138 -0
- package/dist/transformer/TransformerService.js +265 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +1115 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.cts +102 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.ts +102 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +1085 -0
- package/dist/transformer/transformers/GeminiTransformer.cjs +1013 -0
- package/dist/transformer/transformers/GeminiTransformer.d.cts +70 -0
- package/dist/transformer/transformers/GeminiTransformer.d.ts +70 -0
- package/dist/transformer/transformers/GeminiTransformer.js +986 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +538 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.d.cts +53 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.d.ts +53 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.js +513 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.cjs +73 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.cts +51 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.ts +51 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.js +48 -0
- package/dist/transformer/types.cjs +18 -0
- package/dist/transformer/types.d.cts +405 -0
- package/dist/transformer/types.d.ts +405 -0
- package/dist/transformer/types.js +0 -0
- package/dist/transformer.cjs +3736 -0
- package/dist/transformer.d.cts +33 -0
- package/dist/transformer.d.ts +33 -0
- package/dist/transformer.js +3712 -0
- package/dist/types-CGGrKqC_.d.cts +142 -0
- package/dist/types-CbCN2NQP.d.ts +142 -0
- package/dist/types-DCzHkhJt.d.ts +467 -0
- package/dist/types-DZIQbgp0.d.cts +467 -0
- package/dist/usage-event-sink-BX7FE1NL.d.cts +59 -0
- package/dist/usage-event-sink-BX7FE1NL.d.ts +59 -0
- package/package.json +62 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/transformer/transformers/OpenAIResponseTransformer.ts
|
|
21
|
+
var OpenAIResponseTransformer_exports = {};
|
|
22
|
+
__export(OpenAIResponseTransformer_exports, {
|
|
23
|
+
OpenAIResponseTransformer: () => OpenAIResponseTransformer
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(OpenAIResponseTransformer_exports);
|
|
26
|
+
var OpenAIResponseTransformer = class {
|
|
27
|
+
static TransformerName = "openai-response";
|
|
28
|
+
name = "openai-response";
|
|
29
|
+
endPoint = "/v1/responses";
|
|
30
|
+
logger;
|
|
31
|
+
/**
|
|
32
|
+
* Handle authentication - Bearer token
|
|
33
|
+
*/
|
|
34
|
+
async auth(request, provider, _context) {
|
|
35
|
+
return {
|
|
36
|
+
body: request,
|
|
37
|
+
config: {
|
|
38
|
+
headers: {
|
|
39
|
+
Authorization: `Bearer ${provider.apiKey}`,
|
|
40
|
+
"Content-Type": "application/json"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Transform unified request → Response API format
|
|
47
|
+
*/
|
|
48
|
+
async transformRequestIn(request, provider, _context) {
|
|
49
|
+
const input = [];
|
|
50
|
+
for (const msg of request.messages) {
|
|
51
|
+
if (msg.role === "system") {
|
|
52
|
+
input.push({
|
|
53
|
+
role: "developer",
|
|
54
|
+
content: typeof msg.content === "string" ? msg.content : flattenContent(msg.content)
|
|
55
|
+
});
|
|
56
|
+
} else if (msg.role === "tool") {
|
|
57
|
+
input.push({
|
|
58
|
+
type: "function_call_output",
|
|
59
|
+
call_id: msg.tool_call_id,
|
|
60
|
+
output: typeof msg.content === "string" ? msg.content : ""
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
const entry = {
|
|
64
|
+
role: msg.role,
|
|
65
|
+
content: typeof msg.content === "string" ? msg.content : flattenContent(msg.content)
|
|
66
|
+
};
|
|
67
|
+
if (msg.role === "assistant" && msg.tool_calls?.length) {
|
|
68
|
+
input.push(entry);
|
|
69
|
+
for (const tc of msg.tool_calls) {
|
|
70
|
+
input.push({
|
|
71
|
+
type: "function_call",
|
|
72
|
+
id: tc.id,
|
|
73
|
+
call_id: tc.id,
|
|
74
|
+
name: tc.function.name,
|
|
75
|
+
arguments: tc.function.arguments
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
input.push(entry);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const body = {
|
|
84
|
+
model: request.model,
|
|
85
|
+
input,
|
|
86
|
+
stream: request.stream ?? false,
|
|
87
|
+
...request.max_tokens ? { max_output_tokens: request.max_tokens } : {},
|
|
88
|
+
...request.temperature !== void 0 ? { temperature: request.temperature } : {}
|
|
89
|
+
};
|
|
90
|
+
if (request.reasoning?.effort && request.reasoning.effort !== "none") {
|
|
91
|
+
body.reasoning = { effort: request.reasoning.effort, summary: "auto" };
|
|
92
|
+
}
|
|
93
|
+
if (request.tools?.length) {
|
|
94
|
+
body.tools = request.tools.map((tool) => ({
|
|
95
|
+
type: "function",
|
|
96
|
+
name: tool.function.name,
|
|
97
|
+
description: tool.function.description,
|
|
98
|
+
parameters: tool.function.parameters
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
if (request.tool_choice) {
|
|
102
|
+
if (typeof request.tool_choice === "string") {
|
|
103
|
+
body.tool_choice = request.tool_choice;
|
|
104
|
+
} else if (typeof request.tool_choice === "object" && "function" in request.tool_choice) {
|
|
105
|
+
body.tool_choice = { type: "function", name: request.tool_choice.function.name };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const url = new URL("/v1/responses", provider.baseUrl);
|
|
109
|
+
return { body, config: { url } };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Transform Response API request → unified format
|
|
113
|
+
*/
|
|
114
|
+
async transformRequestOut(request, _context) {
|
|
115
|
+
const req = request;
|
|
116
|
+
const messages = [];
|
|
117
|
+
if (req.input) {
|
|
118
|
+
for (const item of req.input) {
|
|
119
|
+
const entry = item;
|
|
120
|
+
if (entry.type === "function_call_output") {
|
|
121
|
+
messages.push({
|
|
122
|
+
role: "tool",
|
|
123
|
+
content: entry.output || "",
|
|
124
|
+
tool_call_id: entry.call_id || void 0
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (entry.type === "function_call") {
|
|
129
|
+
const toolCall = {
|
|
130
|
+
id: (entry.call_id ?? entry.id) || "",
|
|
131
|
+
type: "function",
|
|
132
|
+
function: {
|
|
133
|
+
name: entry.name || "",
|
|
134
|
+
arguments: typeof entry.arguments === "string" ? entry.arguments : ""
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const last = messages[messages.length - 1];
|
|
138
|
+
if (last && last.role === "assistant") {
|
|
139
|
+
(last.tool_calls ??= []).push(toolCall);
|
|
140
|
+
} else {
|
|
141
|
+
messages.push({ role: "assistant", content: null, tool_calls: [toolCall] });
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const role = entry.role;
|
|
146
|
+
if (role === "developer") {
|
|
147
|
+
messages.push({
|
|
148
|
+
role: "system",
|
|
149
|
+
content: typeof entry.content === "string" ? entry.content : JSON.stringify(entry.content)
|
|
150
|
+
});
|
|
151
|
+
} else if (role === "user" || role === "assistant") {
|
|
152
|
+
messages.push({
|
|
153
|
+
role,
|
|
154
|
+
content: typeof entry.content === "string" ? entry.content : JSON.stringify(entry.content)
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const result = {
|
|
160
|
+
messages,
|
|
161
|
+
model: req.model,
|
|
162
|
+
max_tokens: req.max_output_tokens,
|
|
163
|
+
temperature: req.temperature,
|
|
164
|
+
stream: req.stream
|
|
165
|
+
};
|
|
166
|
+
if (req.reasoning?.effort) {
|
|
167
|
+
result.reasoning = {
|
|
168
|
+
effort: req.reasoning.effort,
|
|
169
|
+
enabled: true
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (req.tools?.length) {
|
|
173
|
+
result.tools = req.tools.filter((t) => t.type === "function").map((t) => ({
|
|
174
|
+
type: "function",
|
|
175
|
+
function: {
|
|
176
|
+
name: t.name || "",
|
|
177
|
+
description: t.description || "",
|
|
178
|
+
parameters: t.parameters || {}
|
|
179
|
+
}
|
|
180
|
+
}));
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Transform Response API response → unified (OpenAI CC) format
|
|
186
|
+
*/
|
|
187
|
+
async transformResponseOut(response, _context) {
|
|
188
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
189
|
+
if (contentType.includes("text/event-stream")) {
|
|
190
|
+
if (!response.body) {
|
|
191
|
+
throw new Error("Stream response body is null");
|
|
192
|
+
}
|
|
193
|
+
return new Response(convertResponseApiStreamToOpenAI(response.body), {
|
|
194
|
+
headers: {
|
|
195
|
+
"Content-Type": "text/event-stream",
|
|
196
|
+
"Cache-Control": "no-cache",
|
|
197
|
+
Connection: "keep-alive"
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
const data = await response.json();
|
|
202
|
+
return new Response(JSON.stringify(convertResponseApiJsonToOpenAI(data)), {
|
|
203
|
+
headers: { "Content-Type": "application/json" }
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Transform OpenAI CC response → Response API format
|
|
208
|
+
*/
|
|
209
|
+
async transformResponseIn(response, _context) {
|
|
210
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
211
|
+
if (contentType.includes("text/event-stream")) {
|
|
212
|
+
if (!response.body) {
|
|
213
|
+
throw new Error("Stream response body is null");
|
|
214
|
+
}
|
|
215
|
+
return new Response(convertOpenAIStreamToResponseApi(response.body), {
|
|
216
|
+
headers: {
|
|
217
|
+
"Content-Type": "text/event-stream",
|
|
218
|
+
"Cache-Control": "no-cache",
|
|
219
|
+
Connection: "keep-alive"
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const data = await response.json();
|
|
224
|
+
return new Response(JSON.stringify(convertOpenAIJsonToResponseApi(data)), {
|
|
225
|
+
headers: { "Content-Type": "application/json" }
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function flattenContent(content) {
|
|
230
|
+
if (typeof content === "string") return content;
|
|
231
|
+
if (!content) return "";
|
|
232
|
+
if (Array.isArray(content)) {
|
|
233
|
+
return content.filter((c) => c.type === "text").map((c) => c.text || "").join("\n");
|
|
234
|
+
}
|
|
235
|
+
return "";
|
|
236
|
+
}
|
|
237
|
+
function convertResponseApiJsonToOpenAI(data) {
|
|
238
|
+
let textContent = "";
|
|
239
|
+
const toolCalls = [];
|
|
240
|
+
const output = data.output;
|
|
241
|
+
if (output) {
|
|
242
|
+
for (const item of output) {
|
|
243
|
+
if (item.type === "message") {
|
|
244
|
+
const content = item.content;
|
|
245
|
+
if (content) {
|
|
246
|
+
for (const part of content) {
|
|
247
|
+
if (part.type === "output_text" && typeof part.text === "string") {
|
|
248
|
+
textContent += part.text;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
} else if (item.type === "function_call") {
|
|
253
|
+
toolCalls.push({
|
|
254
|
+
id: item.call_id || item.id || `call_${Date.now()}`,
|
|
255
|
+
type: "function",
|
|
256
|
+
function: {
|
|
257
|
+
name: item.name,
|
|
258
|
+
arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments || {})
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const usage = data.usage;
|
|
265
|
+
const message = {
|
|
266
|
+
role: "assistant",
|
|
267
|
+
content: textContent || null
|
|
268
|
+
};
|
|
269
|
+
if (toolCalls.length > 0) {
|
|
270
|
+
message.tool_calls = toolCalls;
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
id: data.id || `chatcmpl-${Date.now()}`,
|
|
274
|
+
object: "chat.completion",
|
|
275
|
+
created: Math.floor(Date.now() / 1e3),
|
|
276
|
+
model: data.model || "unknown",
|
|
277
|
+
choices: [
|
|
278
|
+
{
|
|
279
|
+
index: 0,
|
|
280
|
+
message,
|
|
281
|
+
finish_reason: toolCalls.length > 0 ? "tool_calls" : "stop"
|
|
282
|
+
}
|
|
283
|
+
],
|
|
284
|
+
usage: usage ? {
|
|
285
|
+
prompt_tokens: usage.input_tokens || 0,
|
|
286
|
+
completion_tokens: usage.output_tokens || 0,
|
|
287
|
+
total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0)
|
|
288
|
+
} : void 0
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function convertOpenAIJsonToResponseApi(data) {
|
|
292
|
+
const choices = data.choices;
|
|
293
|
+
const message = choices?.[0]?.message;
|
|
294
|
+
const output = [];
|
|
295
|
+
if (message) {
|
|
296
|
+
const contentParts = [];
|
|
297
|
+
if (message.content) {
|
|
298
|
+
contentParts.push({ type: "output_text", text: message.content });
|
|
299
|
+
}
|
|
300
|
+
if (contentParts.length > 0) {
|
|
301
|
+
output.push({ type: "message", role: "assistant", content: contentParts });
|
|
302
|
+
}
|
|
303
|
+
const toolCalls = message.tool_calls;
|
|
304
|
+
if (toolCalls?.length) {
|
|
305
|
+
for (const tc of toolCalls) {
|
|
306
|
+
const func = tc.function;
|
|
307
|
+
output.push({
|
|
308
|
+
type: "function_call",
|
|
309
|
+
id: tc.id,
|
|
310
|
+
call_id: tc.id,
|
|
311
|
+
name: func?.name,
|
|
312
|
+
arguments: func?.arguments
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const usage = data.usage;
|
|
318
|
+
return {
|
|
319
|
+
id: data.id || `resp_${Date.now()}`,
|
|
320
|
+
object: "response",
|
|
321
|
+
status: "completed",
|
|
322
|
+
model: data.model || "unknown",
|
|
323
|
+
output,
|
|
324
|
+
usage: usage ? {
|
|
325
|
+
input_tokens: usage.prompt_tokens || 0,
|
|
326
|
+
output_tokens: usage.completion_tokens || 0,
|
|
327
|
+
total_tokens: usage.total_tokens || (usage.prompt_tokens || 0) + (usage.completion_tokens || 0)
|
|
328
|
+
} : void 0
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function convertResponseApiStreamToOpenAI(responseApiStream) {
|
|
332
|
+
const decoder = new TextDecoder();
|
|
333
|
+
const encoder = new TextEncoder();
|
|
334
|
+
return new ReadableStream({
|
|
335
|
+
start: async (controller) => {
|
|
336
|
+
const reader = responseApiStream.getReader();
|
|
337
|
+
let buffer = "";
|
|
338
|
+
let isClosed = false;
|
|
339
|
+
const messageId = `chatcmpl-${Date.now()}`;
|
|
340
|
+
let model = "unknown";
|
|
341
|
+
let hasEmittedRole = false;
|
|
342
|
+
const safeEnqueue = (str) => {
|
|
343
|
+
if (!isClosed) {
|
|
344
|
+
try {
|
|
345
|
+
controller.enqueue(encoder.encode(str));
|
|
346
|
+
} catch {
|
|
347
|
+
isClosed = true;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const emitChunk = (choices, usage) => {
|
|
352
|
+
const chunk = {
|
|
353
|
+
id: messageId,
|
|
354
|
+
object: "chat.completion.chunk",
|
|
355
|
+
created: Math.floor(Date.now() / 1e3),
|
|
356
|
+
model,
|
|
357
|
+
choices
|
|
358
|
+
};
|
|
359
|
+
if (usage) chunk.usage = usage;
|
|
360
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
361
|
+
|
|
362
|
+
`);
|
|
363
|
+
};
|
|
364
|
+
try {
|
|
365
|
+
while (true) {
|
|
366
|
+
const { done, value } = await reader.read();
|
|
367
|
+
if (done) break;
|
|
368
|
+
buffer += decoder.decode(value, { stream: true });
|
|
369
|
+
const lines = buffer.split("\n");
|
|
370
|
+
buffer = lines.pop() || "";
|
|
371
|
+
for (const line of lines) {
|
|
372
|
+
if (isClosed) break;
|
|
373
|
+
if (!line.startsWith("data:")) continue;
|
|
374
|
+
const data = line.slice(5).trim();
|
|
375
|
+
if (data === "[DONE]") continue;
|
|
376
|
+
try {
|
|
377
|
+
const event = JSON.parse(data);
|
|
378
|
+
model = event.model || event.response?.model || model;
|
|
379
|
+
switch (event.type) {
|
|
380
|
+
case "response.output_text.delta":
|
|
381
|
+
if (event.delta) {
|
|
382
|
+
if (!hasEmittedRole) {
|
|
383
|
+
emitChunk([{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }]);
|
|
384
|
+
hasEmittedRole = true;
|
|
385
|
+
}
|
|
386
|
+
emitChunk([{ index: 0, delta: { content: event.delta }, finish_reason: null }]);
|
|
387
|
+
}
|
|
388
|
+
break;
|
|
389
|
+
case "response.reasoning_summary_text.delta":
|
|
390
|
+
if (event.delta) {
|
|
391
|
+
emitChunk([{
|
|
392
|
+
index: 0,
|
|
393
|
+
delta: { thinking: { content: event.delta } },
|
|
394
|
+
finish_reason: null
|
|
395
|
+
}]);
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
case "response.completed": {
|
|
399
|
+
const resp = event.response;
|
|
400
|
+
const respUsage = resp?.usage;
|
|
401
|
+
const usage = respUsage ? {
|
|
402
|
+
prompt_tokens: respUsage.input_tokens || 0,
|
|
403
|
+
completion_tokens: respUsage.output_tokens || 0,
|
|
404
|
+
total_tokens: (respUsage.input_tokens || 0) + (respUsage.output_tokens || 0)
|
|
405
|
+
} : void 0;
|
|
406
|
+
emitChunk([{ index: 0, delta: {}, finish_reason: "stop" }], usage);
|
|
407
|
+
safeEnqueue("data: [DONE]\n\n");
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "error":
|
|
411
|
+
emitChunk([{
|
|
412
|
+
index: 0,
|
|
413
|
+
delta: { content: `[Error: ${event.error?.message || "Unknown error"}]` },
|
|
414
|
+
finish_reason: "stop"
|
|
415
|
+
}]);
|
|
416
|
+
break;
|
|
417
|
+
default:
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
} catch {
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
} catch (e) {
|
|
425
|
+
if (!isClosed) controller.error(e);
|
|
426
|
+
} finally {
|
|
427
|
+
if (!isClosed) {
|
|
428
|
+
try {
|
|
429
|
+
controller.close();
|
|
430
|
+
} catch {
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
reader.releaseLock();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
function convertOpenAIStreamToResponseApi(openaiStream) {
|
|
439
|
+
const decoder = new TextDecoder();
|
|
440
|
+
const encoder = new TextEncoder();
|
|
441
|
+
return new ReadableStream({
|
|
442
|
+
start: async (controller) => {
|
|
443
|
+
const reader = openaiStream.getReader();
|
|
444
|
+
let buffer = "";
|
|
445
|
+
let isClosed = false;
|
|
446
|
+
let accumulatedContent = "";
|
|
447
|
+
let model = "unknown";
|
|
448
|
+
const responseId = `resp_${Date.now()}`;
|
|
449
|
+
const safeEnqueue = (str) => {
|
|
450
|
+
if (!isClosed) {
|
|
451
|
+
try {
|
|
452
|
+
controller.enqueue(encoder.encode(str));
|
|
453
|
+
} catch {
|
|
454
|
+
isClosed = true;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
const emitEvent = (event) => {
|
|
459
|
+
safeEnqueue(`data: ${JSON.stringify(event)}
|
|
460
|
+
|
|
461
|
+
`);
|
|
462
|
+
};
|
|
463
|
+
emitEvent({
|
|
464
|
+
type: "response.created",
|
|
465
|
+
response: { id: responseId, status: "in_progress" }
|
|
466
|
+
});
|
|
467
|
+
try {
|
|
468
|
+
while (true) {
|
|
469
|
+
const { done, value } = await reader.read();
|
|
470
|
+
if (done) break;
|
|
471
|
+
buffer += decoder.decode(value, { stream: true });
|
|
472
|
+
const lines = buffer.split("\n");
|
|
473
|
+
buffer = lines.pop() || "";
|
|
474
|
+
for (const line of lines) {
|
|
475
|
+
if (isClosed) break;
|
|
476
|
+
if (!line.startsWith("data:")) continue;
|
|
477
|
+
const data = line.slice(5).trim();
|
|
478
|
+
if (data === "[DONE]") continue;
|
|
479
|
+
try {
|
|
480
|
+
const chunk = JSON.parse(data);
|
|
481
|
+
const choice = chunk.choices?.[0];
|
|
482
|
+
model = chunk.model || model;
|
|
483
|
+
if (!choice) continue;
|
|
484
|
+
if (choice.delta?.content) {
|
|
485
|
+
accumulatedContent += choice.delta.content;
|
|
486
|
+
emitEvent({ type: "response.output_text.delta", delta: choice.delta.content });
|
|
487
|
+
}
|
|
488
|
+
if (choice.delta?.thinking?.content) {
|
|
489
|
+
emitEvent({
|
|
490
|
+
type: "response.reasoning_summary_text.delta",
|
|
491
|
+
delta: choice.delta.thinking.content
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
if (choice.finish_reason) {
|
|
495
|
+
emitEvent({ type: "response.output_text.done", text: accumulatedContent });
|
|
496
|
+
emitEvent({
|
|
497
|
+
type: "response.completed",
|
|
498
|
+
response: {
|
|
499
|
+
id: responseId,
|
|
500
|
+
status: "completed",
|
|
501
|
+
model,
|
|
502
|
+
output: [
|
|
503
|
+
{
|
|
504
|
+
type: "message",
|
|
505
|
+
role: "assistant",
|
|
506
|
+
content: [{ type: "output_text", text: accumulatedContent }]
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
usage: chunk.usage ? {
|
|
510
|
+
input_tokens: chunk.usage.prompt_tokens || 0,
|
|
511
|
+
output_tokens: chunk.usage.completion_tokens || 0,
|
|
512
|
+
total_tokens: chunk.usage.total_tokens || 0
|
|
513
|
+
} : void 0
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
} catch {
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
} catch (e) {
|
|
522
|
+
if (!isClosed) controller.error(e);
|
|
523
|
+
} finally {
|
|
524
|
+
if (!isClosed) {
|
|
525
|
+
try {
|
|
526
|
+
controller.close();
|
|
527
|
+
} catch {
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
reader.releaseLock();
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
536
|
+
0 && (module.exports = {
|
|
537
|
+
OpenAIResponseTransformer
|
|
538
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Transformer, TransformerLogger, LLMProvider, TransformerContext, UnifiedChatRequest } from '../types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OpenAIResponseTransformer - Transformer for OpenAI Responses API
|
|
5
|
+
*
|
|
6
|
+
* Handles bidirectional conversion between unified (OpenAI Chat Completions)
|
|
7
|
+
* format and OpenAI Responses API format (/v1/responses).
|
|
8
|
+
*
|
|
9
|
+
* @module transformer/transformers/OpenAIResponseTransformer
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* OpenAIResponseTransformer handles OpenAI Responses API format conversion
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - Converts unified (OpenAI CC) requests to Response API format
|
|
17
|
+
* - Converts Response API responses back to unified format
|
|
18
|
+
* - Supports both streaming SSE and non-streaming JSON responses
|
|
19
|
+
* - Bearer token authentication
|
|
20
|
+
*/
|
|
21
|
+
declare class OpenAIResponseTransformer implements Transformer {
|
|
22
|
+
static TransformerName: string;
|
|
23
|
+
name: string;
|
|
24
|
+
endPoint: string;
|
|
25
|
+
logger?: TransformerLogger;
|
|
26
|
+
/**
|
|
27
|
+
* Handle authentication - Bearer token
|
|
28
|
+
*/
|
|
29
|
+
auth(request: unknown, provider: LLMProvider, _context: TransformerContext): Promise<{
|
|
30
|
+
body: unknown;
|
|
31
|
+
config: {
|
|
32
|
+
headers: Record<string, string>;
|
|
33
|
+
};
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Transform unified request → Response API format
|
|
37
|
+
*/
|
|
38
|
+
transformRequestIn(request: UnifiedChatRequest, provider: LLMProvider, _context: TransformerContext): Promise<Record<string, unknown>>;
|
|
39
|
+
/**
|
|
40
|
+
* Transform Response API request → unified format
|
|
41
|
+
*/
|
|
42
|
+
transformRequestOut(request: unknown, _context: TransformerContext): Promise<UnifiedChatRequest>;
|
|
43
|
+
/**
|
|
44
|
+
* Transform Response API response → unified (OpenAI CC) format
|
|
45
|
+
*/
|
|
46
|
+
transformResponseOut(response: Response, _context: TransformerContext): Promise<Response>;
|
|
47
|
+
/**
|
|
48
|
+
* Transform OpenAI CC response → Response API format
|
|
49
|
+
*/
|
|
50
|
+
transformResponseIn(response: Response, _context?: TransformerContext): Promise<Response>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { OpenAIResponseTransformer };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Transformer, TransformerLogger, LLMProvider, TransformerContext, UnifiedChatRequest } from '../types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OpenAIResponseTransformer - Transformer for OpenAI Responses API
|
|
5
|
+
*
|
|
6
|
+
* Handles bidirectional conversion between unified (OpenAI Chat Completions)
|
|
7
|
+
* format and OpenAI Responses API format (/v1/responses).
|
|
8
|
+
*
|
|
9
|
+
* @module transformer/transformers/OpenAIResponseTransformer
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* OpenAIResponseTransformer handles OpenAI Responses API format conversion
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - Converts unified (OpenAI CC) requests to Response API format
|
|
17
|
+
* - Converts Response API responses back to unified format
|
|
18
|
+
* - Supports both streaming SSE and non-streaming JSON responses
|
|
19
|
+
* - Bearer token authentication
|
|
20
|
+
*/
|
|
21
|
+
declare class OpenAIResponseTransformer implements Transformer {
|
|
22
|
+
static TransformerName: string;
|
|
23
|
+
name: string;
|
|
24
|
+
endPoint: string;
|
|
25
|
+
logger?: TransformerLogger;
|
|
26
|
+
/**
|
|
27
|
+
* Handle authentication - Bearer token
|
|
28
|
+
*/
|
|
29
|
+
auth(request: unknown, provider: LLMProvider, _context: TransformerContext): Promise<{
|
|
30
|
+
body: unknown;
|
|
31
|
+
config: {
|
|
32
|
+
headers: Record<string, string>;
|
|
33
|
+
};
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Transform unified request → Response API format
|
|
37
|
+
*/
|
|
38
|
+
transformRequestIn(request: UnifiedChatRequest, provider: LLMProvider, _context: TransformerContext): Promise<Record<string, unknown>>;
|
|
39
|
+
/**
|
|
40
|
+
* Transform Response API request → unified format
|
|
41
|
+
*/
|
|
42
|
+
transformRequestOut(request: unknown, _context: TransformerContext): Promise<UnifiedChatRequest>;
|
|
43
|
+
/**
|
|
44
|
+
* Transform Response API response → unified (OpenAI CC) format
|
|
45
|
+
*/
|
|
46
|
+
transformResponseOut(response: Response, _context: TransformerContext): Promise<Response>;
|
|
47
|
+
/**
|
|
48
|
+
* Transform OpenAI CC response → Response API format
|
|
49
|
+
*/
|
|
50
|
+
transformResponseIn(response: Response, _context?: TransformerContext): Promise<Response>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { OpenAIResponseTransformer };
|