@omnicross/core 0.1.3 → 0.1.5
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 -21
- package/NOTICE +57 -57
- package/README.md +15 -15
- package/dist/auth/GeminiCodeAssistProjectResolver.cjs +6 -5
- package/dist/auth/GeminiCodeAssistProjectResolver.js +6 -5
- package/dist/{chunk-2DCNB7DF.cjs → chunk-3Q6Z6JTO.cjs} +154 -26
- package/dist/chunk-3QFXFCHG.js +33 -0
- package/dist/chunk-3Z5BQZFM.cjs +52 -0
- package/dist/{chunk-4VAVYUZS.cjs → chunk-4XMYPW46.cjs} +9 -9
- package/dist/chunk-4YINUUKQ.cjs +33 -0
- package/dist/{chunk-S4WBV25P.js → chunk-6QZGDLL4.js} +106 -23
- package/dist/{chunk-C72S6TTV.cjs → chunk-7FEBOPJO.cjs} +5 -4
- package/dist/chunk-APYG5QD7.cjs +50 -0
- package/dist/{chunk-5HTVET6E.js → chunk-BODMAL6G.js} +143 -15
- package/dist/{chunk-CWM2ZDU5.js → chunk-BPKCU575.js} +3 -10
- package/dist/{chunk-2FMI2B6V.cjs → chunk-CGKLE6IW.cjs} +350 -461
- package/dist/{chunk-BO4RFWBR.js → chunk-D3TDKWRP.js} +84 -3
- package/dist/{chunk-RSJ3TWJQ.cjs → chunk-DDMRUL76.cjs} +5 -5
- package/dist/chunk-FCR77GYM.cjs +33 -0
- package/dist/{chunk-4DECIFU7.js → chunk-FH2P4OCD.js} +178 -289
- package/dist/{chunk-N5QFKSDO.js → chunk-HRRYG2AX.js} +2 -1
- package/dist/{chunk-KUU2RNG6.cjs → chunk-HYN75H6G.cjs} +3 -0
- package/dist/{chunk-Z7LUNECE.js → chunk-IB3FDI4O.js} +5 -4
- package/dist/chunk-IFXJ2BG7.js +109 -0
- package/dist/{chunk-JO5NLTLY.js → chunk-IK3KE3AU.js} +3 -0
- package/dist/{chunk-C7PSERMH.js → chunk-IWQAX3CS.js} +1 -1
- package/dist/{chunk-YGXJGRFI.cjs → chunk-JWVTXXPX.cjs} +106 -23
- package/dist/{chunk-7YN22L2H.cjs → chunk-K52THW2C.cjs} +5 -5
- package/dist/chunk-K5NM7VAH.js +33 -0
- package/dist/{chunk-Y6OM7EV7.cjs → chunk-KFI44N2R.cjs} +2 -1
- package/dist/{chunk-Z7Y2NPHY.js → chunk-MFZ5437A.js} +1 -1
- package/dist/chunk-OE6TDIWW.js +22 -0
- package/dist/{chunk-MQNDWYPN.js → chunk-OQWKZCXP.js} +13 -6
- package/dist/chunk-OZFM4X3S.js +18 -0
- package/dist/chunk-PG7PS2NN.cjs +170 -0
- package/dist/{chunk-3QOSDDNI.cjs → chunk-SNYBEXLB.cjs} +6 -13
- package/dist/chunk-SYQF7OIR.js +52 -0
- package/dist/{chunk-CZHZGRU7.js → chunk-UHGVBFU3.js} +2 -0
- package/dist/chunk-UIBD2YY5.cjs +22 -0
- package/dist/chunk-UNWKG6EI.cjs +109 -0
- package/dist/{chunk-SVXC36MU.cjs → chunk-VACWOIRE.cjs} +2 -0
- package/dist/chunk-WLYIIEAF.js +50 -0
- package/dist/chunk-XSVNC2UD.cjs +18 -0
- package/dist/{chunk-FFALUUZZ.cjs → chunk-ZCIQ3L3R.cjs} +18 -11
- package/dist/completion/CompletionService.cjs +26 -19
- package/dist/completion/CompletionService.js +25 -18
- package/dist/completion.cjs +28 -21
- package/dist/completion.js +27 -20
- package/dist/index.cjs +35 -25
- package/dist/index.d.cts +7 -3
- package/dist/index.d.ts +7 -3
- package/dist/index.js +44 -34
- package/dist/keyPolicy-Cp0FntZl.d.cts +90 -0
- package/dist/keyPolicy-Cp0FntZl.d.ts +90 -0
- package/dist/outbound-api/auditCapture.cjs +9 -0
- package/dist/outbound-api/auditCapture.d.cts +54 -0
- package/dist/outbound-api/auditCapture.d.ts +54 -0
- package/dist/outbound-api/auditCapture.js +9 -0
- package/dist/outbound-api/auditRedact.cjs +8 -0
- package/dist/outbound-api/auditRedact.d.cts +30 -0
- package/dist/outbound-api/auditRedact.d.ts +30 -0
- package/dist/outbound-api/auditRedact.js +8 -0
- package/dist/outbound-api/billingCapture.cjs +8 -0
- package/dist/outbound-api/billingCapture.d.cts +61 -0
- package/dist/outbound-api/billingCapture.d.ts +61 -0
- package/dist/outbound-api/billingCapture.js +8 -0
- package/dist/outbound-api/quotaWarn.cjs +12 -0
- package/dist/outbound-api/quotaWarn.d.cts +43 -0
- package/dist/outbound-api/quotaWarn.d.ts +43 -0
- package/dist/outbound-api/quotaWarn.js +12 -0
- package/dist/outbound-api/routeResolver.cjs +2 -2
- package/dist/outbound-api/routeResolver.d.cts +3 -2
- package/dist/outbound-api/routeResolver.d.ts +3 -2
- package/dist/outbound-api/routeResolver.js +1 -1
- package/dist/outbound-api/types.d.cts +2 -1
- package/dist/outbound-api/types.d.ts +2 -1
- package/dist/outbound-api.cjs +33 -23
- package/dist/outbound-api.d.cts +9 -140
- package/dist/outbound-api.d.ts +9 -140
- package/dist/outbound-api.js +32 -22
- package/dist/pipeline/AuthSource.d.cts +6 -0
- package/dist/pipeline/AuthSource.d.ts +6 -0
- package/dist/pipeline/SubscriptionAuthSource.cjs +2 -2
- package/dist/pipeline/SubscriptionAuthSource.js +1 -1
- package/dist/pipeline/SubscriptionAuthStrategy.d.cts +6 -0
- package/dist/pipeline/SubscriptionAuthStrategy.d.ts +6 -0
- package/dist/pipeline/auditUsageStash.cjs +10 -0
- package/dist/pipeline/auditUsageStash.d.cts +37 -0
- package/dist/pipeline/auditUsageStash.d.ts +37 -0
- package/dist/pipeline/auditUsageStash.js +10 -0
- package/dist/pipeline/executeProviderCall.cjs +2 -2
- package/dist/pipeline/executeProviderCall.js +1 -1
- package/dist/pipeline/upstreamFetch.cjs +15 -0
- package/dist/pipeline/upstreamFetch.d.cts +89 -0
- package/dist/pipeline/upstreamFetch.d.ts +89 -0
- package/dist/pipeline/upstreamFetch.js +15 -0
- package/dist/pipeline/upstreamTrace.cjs +14 -0
- package/dist/pipeline/upstreamTrace.d.cts +85 -0
- package/dist/pipeline/upstreamTrace.d.ts +85 -0
- package/dist/pipeline/upstreamTrace.js +14 -0
- package/dist/pipeline/webhookEmit.cjs +10 -0
- package/dist/pipeline/webhookEmit.d.cts +36 -0
- package/dist/pipeline/webhookEmit.d.ts +36 -0
- package/dist/pipeline/webhookEmit.js +10 -0
- package/dist/ports.d.cts +2 -1
- package/dist/ports.d.ts +2 -1
- package/dist/provider-proxy/ProviderProxy.cjs +26 -19
- package/dist/provider-proxy/ProviderProxy.js +25 -18
- package/dist/provider-proxy/ingress/providerProxyShared.cjs +28 -19
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +17 -1
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +17 -1
- package/dist/provider-proxy/ingress/providerProxyShared.js +27 -18
- package/dist/provider-proxy/types.d.cts +7 -0
- package/dist/provider-proxy/types.d.ts +7 -0
- package/dist/provider-proxy.cjs +26 -19
- package/dist/provider-proxy.js +25 -18
- package/dist/{routeResolver-B1NglTfe.d.ts → routeResolver-C4T7yClx.d.ts} +1 -1
- package/dist/{routeResolver-CbG5uBgm.d.cts → routeResolver-CB3tvJKy.d.cts} +1 -1
- package/dist/transformer/transformers/AnthropicTransformer.cjs +2 -2
- package/dist/transformer/transformers/AnthropicTransformer.js +1 -1
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +3 -3
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +2 -2
- package/dist/transformer/transformers/GeminiTransformer.cjs +3 -3
- package/dist/transformer/transformers/GeminiTransformer.js +2 -2
- package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +2 -2
- package/dist/transformer/transformers/OpenAIResponseTransformer.js +1 -1
- package/dist/transformer/transformers/OpenCodeGoTransformer.cjs +2 -2
- package/dist/transformer/transformers/OpenCodeGoTransformer.js +1 -1
- package/dist/transformer/transformers.cjs +8 -8
- package/dist/transformer/transformers.js +7 -7
- package/dist/transformer.cjs +8 -8
- package/dist/transformer.js +7 -7
- package/dist/{types-aBFFXoBz.d.cts → types-B8Arpxn0.d.cts} +14 -90
- package/dist/{types-6OUY_xfA.d.ts → types-lgPYQuZS.d.ts} +14 -90
- package/dist/usage/usage-recorder.cjs +3 -2
- package/dist/usage/usage-recorder.js +2 -1
- package/dist/usage.cjs +3 -2
- package/dist/usage.js +2 -1
- package/package.json +64 -64
- package/dist/chunk-S6JGLF3G.cjs +0 -89
- package/dist/{chunk-FTQ7IQUC.js → chunk-C6OV7GCC.js} +8 -8
|
@@ -237,6 +237,14 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
|
|
|
237
237
|
}
|
|
238
238
|
const message = choice.message;
|
|
239
239
|
const content = [];
|
|
240
|
+
const thinking = message.thinking;
|
|
241
|
+
if (_optionalChain([thinking, 'optionalAccess', _13 => _13.content])) {
|
|
242
|
+
content.push({
|
|
243
|
+
type: "thinking",
|
|
244
|
+
thinking: thinking.content,
|
|
245
|
+
signature: thinking.signature
|
|
246
|
+
});
|
|
247
|
+
}
|
|
240
248
|
if (message.content) {
|
|
241
249
|
content.push({
|
|
242
250
|
type: "text",
|
|
@@ -244,7 +252,7 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
|
|
|
244
252
|
});
|
|
245
253
|
}
|
|
246
254
|
const toolCalls = message.tool_calls;
|
|
247
|
-
if (_optionalChain([toolCalls, 'optionalAccess',
|
|
255
|
+
if (_optionalChain([toolCalls, 'optionalAccess', _14 => _14.length])) {
|
|
248
256
|
for (const toolCall of toolCalls) {
|
|
249
257
|
const func = toolCall.function;
|
|
250
258
|
let parsedInput = {};
|
|
@@ -262,14 +270,6 @@ function convertOpenAIResponseToAnthropic(openaiResponse) {
|
|
|
262
270
|
});
|
|
263
271
|
}
|
|
264
272
|
}
|
|
265
|
-
const thinking = message.thinking;
|
|
266
|
-
if (_optionalChain([thinking, 'optionalAccess', _14 => _14.content])) {
|
|
267
|
-
content.push({
|
|
268
|
-
type: "thinking",
|
|
269
|
-
thinking: thinking.content,
|
|
270
|
-
signature: thinking.signature
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
273
|
const finishReason = choice.finish_reason;
|
|
274
274
|
const stopReasonMapping = {
|
|
275
275
|
stop: "end_turn",
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/outbound-api/quotaWarn.ts
|
|
2
|
+
var WEBHOOK_QUOTA_WARN_RATIO = 0.8;
|
|
3
|
+
function computeQuotaWarnings(limits, spend, ratio = WEBHOOK_QUOTA_WARN_RATIO) {
|
|
4
|
+
const out = [];
|
|
5
|
+
const consider = (scope, limit, spent, windowStart) => {
|
|
6
|
+
if (limit == null || limit <= 0) return;
|
|
7
|
+
if (spent >= limit) return;
|
|
8
|
+
if (spent >= ratio * limit) out.push({ scope, limitUsd: limit, spentUsd: spent, windowStart });
|
|
9
|
+
};
|
|
10
|
+
consider("daily", limits.dailyUsd, spend.dailyUsd, spend.dailyWindowStart);
|
|
11
|
+
consider("weekly", limits.weeklyUsd, spend.weeklyUsd, spend.weeklyWindowStart);
|
|
12
|
+
consider("total", limits.totalUsd, spend.totalUsd, 0);
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
var WARN_GUARD_MAX = 1e4;
|
|
16
|
+
var warned = /* @__PURE__ */ new Set();
|
|
17
|
+
function markQuotaWarnedOnce(keyId, scope, windowStart) {
|
|
18
|
+
const key = `${keyId}\0${scope}\0${windowStart}`;
|
|
19
|
+
if (warned.has(key)) return false;
|
|
20
|
+
if (warned.size >= WARN_GUARD_MAX) warned.clear();
|
|
21
|
+
warned.add(key);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function __resetQuotaWarnGuardForTests() {
|
|
25
|
+
warned.clear();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
exports.WEBHOOK_QUOTA_WARN_RATIO = WEBHOOK_QUOTA_WARN_RATIO; exports.computeQuotaWarnings = computeQuotaWarnings; exports.markQuotaWarnedOnce = markQuotaWarnedOnce; exports.__resetQuotaWarnGuardForTests = __resetQuotaWarnGuardForTests;
|
|
@@ -321,6 +321,7 @@ function handleStreamResponse(response, providerName, logger) {
|
|
|
321
321
|
let pendingContent = "";
|
|
322
322
|
let contentIndex = 0;
|
|
323
323
|
let toolCallIndex = -1;
|
|
324
|
+
let textBuffered = false;
|
|
324
325
|
const stream = new ReadableStream({
|
|
325
326
|
async start(controller) {
|
|
326
327
|
const reader = response.body.getReader();
|
|
@@ -338,6 +339,7 @@ function handleStreamResponse(response, providerName, logger) {
|
|
|
338
339
|
}
|
|
339
340
|
const candidate = chunk.candidates[0];
|
|
340
341
|
const parts = candidate.content?.parts || [];
|
|
342
|
+
textBuffered = false;
|
|
341
343
|
parts.filter((part) => part.text && part.thought === true).forEach((part) => {
|
|
342
344
|
hasThinkingContent = true;
|
|
343
345
|
const thinkingChunk = createChunk({
|
|
@@ -415,7 +417,7 @@ function handleStreamResponse(response, providerName, logger) {
|
|
|
415
417
|
if (hasThinkingContent && textContent && !signatureSent) {
|
|
416
418
|
if (chunk.modelVersion?.includes("3")) {
|
|
417
419
|
pendingContent += textContent;
|
|
418
|
-
|
|
420
|
+
textBuffered = true;
|
|
419
421
|
} else {
|
|
420
422
|
const signatureChunk = createChunk({
|
|
421
423
|
responseId: chunk.responseId,
|
|
@@ -433,7 +435,7 @@ function handleStreamResponse(response, providerName, logger) {
|
|
|
433
435
|
signatureSent = true;
|
|
434
436
|
}
|
|
435
437
|
}
|
|
436
|
-
if (textContent) {
|
|
438
|
+
if (textContent && !textBuffered) {
|
|
437
439
|
if (!pendingContent) contentIndex++;
|
|
438
440
|
const contentChunk = createChunk({
|
|
439
441
|
responseId: chunk.responseId,
|
|
@@ -854,35 +856,116 @@ function transformRequestOut(request) {
|
|
|
854
856
|
tool_choice: toolChoice
|
|
855
857
|
};
|
|
856
858
|
if (Array.isArray(contents)) {
|
|
859
|
+
const toolCallIdsByName = /* @__PURE__ */ new Map();
|
|
857
860
|
for (const content of contents) {
|
|
858
861
|
if (typeof content === "string") {
|
|
859
|
-
unifiedRequest.messages.push({
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
} else if ("text" in content && typeof content.text === "string") {
|
|
862
|
+
unifiedRequest.messages.push({ role: "user", content });
|
|
863
|
+
continue;
|
|
864
|
+
}
|
|
865
|
+
if (content && typeof content === "object" && "text" in content && typeof content.text === "string" && !("parts" in content)) {
|
|
864
866
|
unifiedRequest.messages.push({
|
|
865
867
|
role: "user",
|
|
866
868
|
content: content.text || null
|
|
867
869
|
});
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
if (!content || typeof content !== "object" || !("role" in content)) {
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
const geminiContent = content;
|
|
876
|
+
const isModel = geminiContent.role === "model";
|
|
877
|
+
const role = isModel ? "assistant" : "user";
|
|
878
|
+
const parts = geminiContent.parts ?? [];
|
|
879
|
+
const functionResponses = [];
|
|
880
|
+
const functionCalls = [];
|
|
881
|
+
const textParts = [];
|
|
882
|
+
const contentParts = [];
|
|
883
|
+
let thinkingText = "";
|
|
884
|
+
let signature;
|
|
885
|
+
for (const part of parts) {
|
|
886
|
+
if ("thoughtSignature" in part) {
|
|
887
|
+
signature = part.thoughtSignature;
|
|
888
|
+
}
|
|
889
|
+
if ("functionResponse" in part) {
|
|
890
|
+
functionResponses.push(part);
|
|
891
|
+
} else if ("functionCall" in part) {
|
|
892
|
+
functionCalls.push(part);
|
|
893
|
+
} else if ("inlineData" in part || "file_data" in part) {
|
|
894
|
+
const inline = part.inlineData;
|
|
895
|
+
const fileData = part.file_data;
|
|
896
|
+
if (inline?.data) {
|
|
897
|
+
const mime = inline.mime_type || "image/png";
|
|
898
|
+
contentParts.push({
|
|
899
|
+
type: "image_url",
|
|
900
|
+
image_url: { url: `data:${mime};base64,${inline.data}` },
|
|
901
|
+
media_type: mime
|
|
902
|
+
});
|
|
903
|
+
} else if (fileData?.file_uri) {
|
|
904
|
+
contentParts.push({
|
|
905
|
+
type: "image_url",
|
|
906
|
+
image_url: { url: fileData.file_uri },
|
|
907
|
+
media_type: fileData.mime_type
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
} else if ("text" in part) {
|
|
911
|
+
if (part.thought === true) {
|
|
912
|
+
thinkingText += part.text ?? "";
|
|
913
|
+
} else {
|
|
914
|
+
textParts.push(part);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
for (const fr of functionResponses) {
|
|
919
|
+
const fnName = fr.functionResponse.name ?? "";
|
|
920
|
+
const queue = toolCallIdsByName.get(fnName);
|
|
921
|
+
const id = queue && queue.length > 0 ? queue.shift() : `tool_${Math.random().toString(36).substring(2, 15)}`;
|
|
922
|
+
const response = fr.functionResponse.response;
|
|
923
|
+
const result = response?.result;
|
|
924
|
+
const toolContent = typeof result === "string" ? result : result !== void 0 ? JSON.stringify(result) : response !== void 0 ? JSON.stringify(response) : "";
|
|
878
925
|
unifiedRequest.messages.push({
|
|
879
|
-
role: "
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
text: part.text || ""
|
|
883
|
-
})) || []
|
|
926
|
+
role: "tool",
|
|
927
|
+
tool_call_id: id,
|
|
928
|
+
content: toolContent
|
|
884
929
|
});
|
|
885
930
|
}
|
|
931
|
+
const toolCalls = functionCalls.map((p) => {
|
|
932
|
+
const id = p.functionCall.id || `tool_${Math.random().toString(36).substring(2, 15)}`;
|
|
933
|
+
if (p.functionCall.name) {
|
|
934
|
+
const arr = toolCallIdsByName.get(p.functionCall.name) ?? [];
|
|
935
|
+
arr.push(id);
|
|
936
|
+
toolCallIdsByName.set(p.functionCall.name, arr);
|
|
937
|
+
}
|
|
938
|
+
return {
|
|
939
|
+
id,
|
|
940
|
+
type: "function",
|
|
941
|
+
function: {
|
|
942
|
+
name: p.functionCall.name,
|
|
943
|
+
arguments: JSON.stringify(p.functionCall.args ?? {})
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
});
|
|
947
|
+
const onlyToolResults = functionResponses.length > 0 && textParts.length === 0 && functionCalls.length === 0 && contentParts.length === 0 && !thinkingText && !signature;
|
|
948
|
+
if (onlyToolResults) continue;
|
|
949
|
+
let messageContent;
|
|
950
|
+
if (contentParts.length > 0) {
|
|
951
|
+
messageContent = [
|
|
952
|
+
...textParts.map((p) => ({ type: "text", text: p.text })),
|
|
953
|
+
...contentParts
|
|
954
|
+
];
|
|
955
|
+
} else {
|
|
956
|
+
messageContent = textParts.map((p) => p.text).join("\n") || null;
|
|
957
|
+
}
|
|
958
|
+
const message = {
|
|
959
|
+
role,
|
|
960
|
+
content: messageContent
|
|
961
|
+
};
|
|
962
|
+
if (toolCalls.length > 0) {
|
|
963
|
+
message.tool_calls = toolCalls;
|
|
964
|
+
}
|
|
965
|
+
if (signature || thinkingText) {
|
|
966
|
+
message.thinking = { content: thinkingText || "", signature };
|
|
967
|
+
}
|
|
968
|
+
unifiedRequest.messages.push(message);
|
|
886
969
|
}
|
|
887
970
|
}
|
|
888
971
|
if (Array.isArray(tools)) {
|
|
@@ -895,7 +978,7 @@ function transformRequestOut(request) {
|
|
|
895
978
|
function: {
|
|
896
979
|
name: funcDecl.name,
|
|
897
980
|
description: funcDecl.description ?? "",
|
|
898
|
-
parameters: funcDecl.parameters ?? {}
|
|
981
|
+
parameters: funcDecl.parameters ?? funcDecl.parametersJsonSchema ?? {}
|
|
899
982
|
}
|
|
900
983
|
});
|
|
901
984
|
}
|
|
@@ -15,10 +15,11 @@ var OpenCodeGoTransformer = (_class = class {constructor() { _class.prototype.__
|
|
|
15
15
|
async transformRequestIn(request, _provider, _context) {
|
|
16
16
|
const messages = request.messages.map((m) => {
|
|
17
17
|
const content = m.content;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const normalizedContent = Array.isArray(content) && content.length === 1 && _optionalChain([content, 'access', _ => _[0], 'optionalAccess', _2 => _2.type]) === "text" ? content[0].text : content;
|
|
19
|
+
const out2 = { role: m.role, content: normalizedContent };
|
|
20
|
+
if (m.tool_calls) out2.tool_calls = m.tool_calls;
|
|
21
|
+
if (m.tool_call_id) out2.tool_call_id = m.tool_call_id;
|
|
22
|
+
return out2;
|
|
22
23
|
});
|
|
23
24
|
const out = {
|
|
24
25
|
model: request.model,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkL74VDN4Acjs = require('./chunk-L74VDN4A.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkXSVNC2UDcjs = require('./chunk-XSVNC2UD.cjs');
|
|
8
|
+
|
|
9
|
+
// src/outbound-api/billingCapture.ts
|
|
10
|
+
var _crypto = require('crypto');
|
|
11
|
+
var defaultDefer = (fn) => {
|
|
12
|
+
setTimeout(fn, 0);
|
|
13
|
+
};
|
|
14
|
+
function beginBillingCapture(_req, res, now, defer = defaultDefer) {
|
|
15
|
+
if (!_chunkL74VDN4Acjs.getBillingCaptureConfig.call(void 0, )) return null;
|
|
16
|
+
const ctx = {};
|
|
17
|
+
let finished = false;
|
|
18
|
+
const finalize = () => {
|
|
19
|
+
if (finished) return;
|
|
20
|
+
finished = true;
|
|
21
|
+
const status = res.statusCode;
|
|
22
|
+
defer(() => {
|
|
23
|
+
try {
|
|
24
|
+
const usage = _chunkXSVNC2UDcjs.readAuditUsage.call(void 0, res);
|
|
25
|
+
if (!usage) return;
|
|
26
|
+
const event = {
|
|
27
|
+
id: _crypto.randomUUID.call(void 0, ),
|
|
28
|
+
ts: now,
|
|
29
|
+
model: _nullishCoalesce(_nullishCoalesce(ctx.model, () => ( usage.model)), () => ( "")),
|
|
30
|
+
authMode: _nullishCoalesce(ctx.authMode, () => ( "byo")),
|
|
31
|
+
inputTokens: _nullishCoalesce(usage.inputTokens, () => ( 0)),
|
|
32
|
+
outputTokens: _nullishCoalesce(usage.outputTokens, () => ( 0)),
|
|
33
|
+
costUsd: _nullishCoalesce(usage.costUsd, () => ( 0)),
|
|
34
|
+
status
|
|
35
|
+
};
|
|
36
|
+
if (ctx.keyId != null) event.keyId = ctx.keyId;
|
|
37
|
+
const provider = _nullishCoalesce(ctx.provider, () => ( usage.provider));
|
|
38
|
+
if (provider) event.provider = provider;
|
|
39
|
+
_chunkL74VDN4Acjs.publishBillingEvent.call(void 0, event);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
res.once("close", finalize);
|
|
45
|
+
return ctx;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
exports.beginBillingCapture = beginBillingCapture;
|
|
@@ -25,9 +25,10 @@ var OpenAIResponseTransformer = class {
|
|
|
25
25
|
const input = [];
|
|
26
26
|
for (const msg of request.messages) {
|
|
27
27
|
if (msg.role === "system") {
|
|
28
|
+
const sysText = typeof msg.content === "string" ? msg.content : flattenContent(msg.content);
|
|
28
29
|
input.push({
|
|
29
30
|
role: "developer",
|
|
30
|
-
content:
|
|
31
|
+
content: [{ type: "input_text", text: sysText }]
|
|
31
32
|
});
|
|
32
33
|
} else if (msg.role === "tool") {
|
|
33
34
|
input.push({
|
|
@@ -36,17 +37,20 @@ var OpenAIResponseTransformer = class {
|
|
|
36
37
|
output: typeof msg.content === "string" ? msg.content : ""
|
|
37
38
|
});
|
|
38
39
|
} else {
|
|
40
|
+
const text = typeof msg.content === "string" ? msg.content : flattenContent(msg.content);
|
|
39
41
|
const entry = {
|
|
40
42
|
role: msg.role,
|
|
41
|
-
content:
|
|
43
|
+
content: [{ type: msg.role === "assistant" ? "output_text" : "input_text", text }]
|
|
42
44
|
};
|
|
43
45
|
if (msg.role === "assistant" && msg.tool_calls?.length) {
|
|
44
46
|
input.push(entry);
|
|
45
47
|
for (const tc of msg.tool_calls) {
|
|
48
|
+
const callId = tc.id;
|
|
49
|
+
const itemId = callId.startsWith("fc_") ? callId : `fc_${callId.replace(/^(call_|fc_)/, "")}`;
|
|
46
50
|
input.push({
|
|
47
51
|
type: "function_call",
|
|
48
|
-
id:
|
|
49
|
-
call_id:
|
|
52
|
+
id: itemId,
|
|
53
|
+
call_id: callId,
|
|
50
54
|
name: tc.function.name,
|
|
51
55
|
arguments: tc.function.arguments
|
|
52
56
|
});
|
|
@@ -59,8 +63,18 @@ var OpenAIResponseTransformer = class {
|
|
|
59
63
|
const body = {
|
|
60
64
|
model: request.model,
|
|
61
65
|
input,
|
|
62
|
-
stream:
|
|
63
|
-
|
|
66
|
+
// codex's backend REQUIRES `stream:true` (it 400s with "Stream must be
|
|
67
|
+
// set to true" otherwise). Always force it; a non-streaming CLIENT is
|
|
68
|
+
// served by aggregating the SSE upstream of the wire (the ingress buffers
|
|
69
|
+
// it into a single message), not by asking codex for a non-streaming reply.
|
|
70
|
+
stream: true,
|
|
71
|
+
// `store:false` is required by the codex backend and accepted by the
|
|
72
|
+
// public Responses API (which defaults to store:true). Always set it so
|
|
73
|
+
// codex relays (no discoverable url token) work without configuration.
|
|
74
|
+
store: false,
|
|
75
|
+
// `max_output_tokens` is intentionally omitted: the codex backend rejects
|
|
76
|
+
// it, and the public API falls back to its default when absent. If a
|
|
77
|
+
// public provider ever needs it honored, gate that on an opt-out flag.
|
|
64
78
|
...request.temperature !== void 0 ? { temperature: request.temperature } : {}
|
|
65
79
|
};
|
|
66
80
|
if (request.reasoning?.effort && request.reasoning.effort !== "none") {
|
|
@@ -162,7 +176,19 @@ var OpenAIResponseTransformer = class {
|
|
|
162
176
|
*/
|
|
163
177
|
async transformResponseOut(response, _context) {
|
|
164
178
|
const contentType = response.headers.get("Content-Type") ?? "";
|
|
165
|
-
|
|
179
|
+
let isSse = contentType.includes("text/event-stream");
|
|
180
|
+
if (!isSse && response.body) {
|
|
181
|
+
const peek = response.clone();
|
|
182
|
+
try {
|
|
183
|
+
const reader = peek.body.getReader();
|
|
184
|
+
const { value } = await reader.read();
|
|
185
|
+
reader.releaseLock();
|
|
186
|
+
const head = value ? new TextDecoder().decode(value).trimStart() : "";
|
|
187
|
+
isSse = head.startsWith("event:") || head.startsWith("data:");
|
|
188
|
+
} catch {
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (isSse) {
|
|
166
192
|
if (!response.body) {
|
|
167
193
|
throw new Error("Stream response body is null");
|
|
168
194
|
}
|
|
@@ -315,6 +341,7 @@ function convertResponseApiStreamToOpenAI(responseApiStream) {
|
|
|
315
341
|
const messageId = `chatcmpl-${Date.now()}`;
|
|
316
342
|
let model = "unknown";
|
|
317
343
|
let hasEmittedRole = false;
|
|
344
|
+
let hasToolCalls = false;
|
|
318
345
|
const safeEnqueue = (str) => {
|
|
319
346
|
if (!isClosed) {
|
|
320
347
|
try {
|
|
@@ -371,6 +398,42 @@ function convertResponseApiStreamToOpenAI(responseApiStream) {
|
|
|
371
398
|
}]);
|
|
372
399
|
}
|
|
373
400
|
break;
|
|
401
|
+
case "response.output_item.added": {
|
|
402
|
+
const item = event.item;
|
|
403
|
+
if (item?.type !== "function_call") break;
|
|
404
|
+
if (!hasEmittedRole) {
|
|
405
|
+
emitChunk([{ index: 0, delta: { role: "assistant", content: null }, finish_reason: null }]);
|
|
406
|
+
hasEmittedRole = true;
|
|
407
|
+
}
|
|
408
|
+
hasToolCalls = true;
|
|
409
|
+
emitChunk([{
|
|
410
|
+
index: 0,
|
|
411
|
+
delta: {
|
|
412
|
+
tool_calls: [{
|
|
413
|
+
index: event.output_index ?? 0,
|
|
414
|
+
id: item.call_id,
|
|
415
|
+
type: "function",
|
|
416
|
+
function: { name: item.name, arguments: "" }
|
|
417
|
+
}]
|
|
418
|
+
},
|
|
419
|
+
finish_reason: null
|
|
420
|
+
}]);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "response.function_call_arguments.delta": {
|
|
424
|
+
hasToolCalls = true;
|
|
425
|
+
emitChunk([{
|
|
426
|
+
index: 0,
|
|
427
|
+
delta: {
|
|
428
|
+
tool_calls: [{
|
|
429
|
+
index: event.output_index ?? 0,
|
|
430
|
+
function: { arguments: event.delta ?? "" }
|
|
431
|
+
}]
|
|
432
|
+
},
|
|
433
|
+
finish_reason: null
|
|
434
|
+
}]);
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
374
437
|
case "response.completed": {
|
|
375
438
|
const resp = event.response;
|
|
376
439
|
const respUsage = resp?.usage;
|
|
@@ -379,7 +442,7 @@ function convertResponseApiStreamToOpenAI(responseApiStream) {
|
|
|
379
442
|
completion_tokens: respUsage.output_tokens || 0,
|
|
380
443
|
total_tokens: (respUsage.input_tokens || 0) + (respUsage.output_tokens || 0)
|
|
381
444
|
} : void 0;
|
|
382
|
-
emitChunk([{ index: 0, delta: {}, finish_reason: "stop" }], usage);
|
|
445
|
+
emitChunk([{ index: 0, delta: {}, finish_reason: hasToolCalls ? "tool_calls" : "stop" }], usage);
|
|
383
446
|
safeEnqueue("data: [DONE]\n\n");
|
|
384
447
|
break;
|
|
385
448
|
}
|
|
@@ -422,6 +485,9 @@ function convertOpenAIStreamToResponseApi(openaiStream) {
|
|
|
422
485
|
let accumulatedContent = "";
|
|
423
486
|
let model = "unknown";
|
|
424
487
|
const responseId = `resp_${Date.now()}`;
|
|
488
|
+
const toolCalls = /* @__PURE__ */ new Map();
|
|
489
|
+
const toolOutputIndex = /* @__PURE__ */ new Map();
|
|
490
|
+
let nextOutputIndex = 1;
|
|
425
491
|
const safeEnqueue = (str) => {
|
|
426
492
|
if (!isClosed) {
|
|
427
493
|
try {
|
|
@@ -467,21 +533,83 @@ function convertOpenAIStreamToResponseApi(openaiStream) {
|
|
|
467
533
|
delta: choice.delta.thinking.content
|
|
468
534
|
});
|
|
469
535
|
}
|
|
536
|
+
if (choice.delta?.tool_calls) {
|
|
537
|
+
for (const tc of choice.delta.tool_calls) {
|
|
538
|
+
const tcIndex = typeof tc["index"] === "number" ? tc["index"] : 0;
|
|
539
|
+
const func = tc["function"] ?? {};
|
|
540
|
+
let entry = toolCalls.get(tcIndex);
|
|
541
|
+
if (!entry) {
|
|
542
|
+
const callId = typeof tc["id"] === "string" ? tc["id"] : `call_${Date.now()}_${tcIndex}`;
|
|
543
|
+
const name = typeof func["name"] === "string" ? func["name"] : "";
|
|
544
|
+
entry = { callId, name, arguments: "" };
|
|
545
|
+
toolCalls.set(tcIndex, entry);
|
|
546
|
+
const outIdx = nextOutputIndex++;
|
|
547
|
+
toolOutputIndex.set(tcIndex, outIdx);
|
|
548
|
+
const itemId = callId.startsWith("fc_") ? callId : `fc_${callId.replace(/^(call_|fc_)/, "")}`;
|
|
549
|
+
emitEvent({
|
|
550
|
+
type: "response.output_item.added",
|
|
551
|
+
output_index: outIdx,
|
|
552
|
+
item: {
|
|
553
|
+
id: itemId,
|
|
554
|
+
type: "function_call",
|
|
555
|
+
status: "in_progress",
|
|
556
|
+
call_id: callId,
|
|
557
|
+
name,
|
|
558
|
+
arguments: ""
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
const argsFragment = typeof func["arguments"] === "string" ? func["arguments"] : "";
|
|
563
|
+
if (argsFragment) {
|
|
564
|
+
entry.arguments += argsFragment;
|
|
565
|
+
emitEvent({
|
|
566
|
+
type: "response.function_call_arguments.delta",
|
|
567
|
+
output_index: toolOutputIndex.get(tcIndex),
|
|
568
|
+
delta: argsFragment
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
470
573
|
if (choice.finish_reason) {
|
|
471
574
|
emitEvent({ type: "response.output_text.done", text: accumulatedContent });
|
|
575
|
+
const output = [];
|
|
576
|
+
if (accumulatedContent) {
|
|
577
|
+
output.push({
|
|
578
|
+
type: "message",
|
|
579
|
+
role: "assistant",
|
|
580
|
+
content: [{ type: "output_text", text: accumulatedContent }]
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
for (const [tcIndex, entry] of toolCalls) {
|
|
584
|
+
const itemId = entry.callId.startsWith("fc_") ? entry.callId : `fc_${entry.callId.replace(/^(call_|fc_)/, "")}`;
|
|
585
|
+
output.push({
|
|
586
|
+
id: itemId,
|
|
587
|
+
type: "function_call",
|
|
588
|
+
status: "completed",
|
|
589
|
+
call_id: entry.callId,
|
|
590
|
+
name: entry.name,
|
|
591
|
+
arguments: entry.arguments
|
|
592
|
+
});
|
|
593
|
+
emitEvent({
|
|
594
|
+
type: "response.output_item.done",
|
|
595
|
+
output_index: toolOutputIndex.get(tcIndex),
|
|
596
|
+
item: {
|
|
597
|
+
id: itemId,
|
|
598
|
+
type: "function_call",
|
|
599
|
+
status: "completed",
|
|
600
|
+
call_id: entry.callId,
|
|
601
|
+
name: entry.name,
|
|
602
|
+
arguments: entry.arguments
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
}
|
|
472
606
|
emitEvent({
|
|
473
607
|
type: "response.completed",
|
|
474
608
|
response: {
|
|
475
609
|
id: responseId,
|
|
476
610
|
status: "completed",
|
|
477
611
|
model,
|
|
478
|
-
output
|
|
479
|
-
{
|
|
480
|
-
type: "message",
|
|
481
|
-
role: "assistant",
|
|
482
|
-
content: [{ type: "output_text", text: accumulatedContent }]
|
|
483
|
-
}
|
|
484
|
-
],
|
|
612
|
+
output,
|
|
485
613
|
usage: chunk.usage ? {
|
|
486
614
|
input_tokens: chunk.usage.prompt_tokens || 0,
|
|
487
615
|
output_tokens: chunk.usage.completion_tokens || 0,
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const existing = stash.get(key);
|
|
5
|
-
stash.set(key, existing ? { ...existing, ...detail } : { ...detail });
|
|
6
|
-
}
|
|
7
|
-
function readAuditUsage(key) {
|
|
8
|
-
return stash.get(key);
|
|
9
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
stashAuditUsage
|
|
3
|
+
} from "./chunk-OZFM4X3S.js";
|
|
10
4
|
|
|
11
5
|
// src/usage/usage-recorder.ts
|
|
12
6
|
var UsageRecorder = class {
|
|
@@ -125,6 +119,5 @@ var safeStringify = (value) => {
|
|
|
125
119
|
};
|
|
126
120
|
|
|
127
121
|
export {
|
|
128
|
-
readAuditUsage,
|
|
129
122
|
UsageRecorder
|
|
130
123
|
};
|