@omnicross/core 0.1.4 → 0.1.6
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/ApiConverter.cjs +2 -2
- package/dist/ApiConverter.js +1 -1
- package/dist/{CompletionService-DdWaAxq0.d.ts → CompletionService-C3BX0Ovy.d.ts} +4 -3
- package/dist/{CompletionService-D3a47vdU.d.cts → CompletionService-kNA17cfI.d.cts} +4 -3
- package/dist/auth/GeminiCodeAssistProjectResolver.cjs +7 -5
- package/dist/auth/GeminiCodeAssistProjectResolver.js +5 -3
- package/dist/{chunk-HRRYG2AX.js → chunk-23S5OPIO.js} +10 -1
- package/dist/chunk-25GXJCEZ.cjs +40 -0
- package/dist/chunk-2JYRIADS.js +40 -0
- package/dist/chunk-3Z5BQZFM.cjs +52 -0
- package/dist/{chunk-4VAVYUZS.cjs → chunk-4VLRHHFE.cjs} +82 -40
- package/dist/{chunk-VACWOIRE.cjs → chunk-4VZX5F4T.cjs} +39 -15
- package/dist/{chunk-FTQ7IQUC.js → chunk-6CL5HTPQ.js} +52 -10
- package/dist/chunk-6QQG7YG3.cjs +153 -0
- package/dist/{chunk-SU2GCLAC.cjs → chunk-6VUQPOV3.cjs} +359 -60
- package/dist/chunk-7AK7B36B.cjs +44 -0
- package/dist/{chunk-5ZQBEOMD.js → chunk-AT7B7SOZ.js} +25 -2
- package/dist/{chunk-IK3KE3AU.js → chunk-DE2SRBFZ.js} +1 -1
- package/dist/chunk-DLXZEZWO.cjs +285 -0
- package/dist/{chunk-UHGVBFU3.js → chunk-EBLFCNPY.js} +38 -14
- package/dist/chunk-EJLH6NFT.cjs +166 -0
- package/dist/chunk-HVBBDBS3.cjs +350 -0
- package/dist/{chunk-7Y47UCF2.js → chunk-IFXJ2BG7.js} +5 -38
- package/dist/{chunk-46ZIFWYG.cjs → chunk-IIRPUVPH.cjs} +7 -1
- package/dist/{chunk-YGXJGRFI.cjs → chunk-KBM2W6UY.cjs} +111 -24
- package/dist/{chunk-OCEUVQHW.cjs → chunk-KCVJWJST.cjs} +73 -3
- package/dist/chunk-LB6EBS6Y.js +69 -0
- package/dist/{chunk-C7PSERMH.js → chunk-LVUCAWMK.js} +1 -1
- package/dist/chunk-MKF46GNZ.js +350 -0
- package/dist/{chunk-HYN75H6G.cjs → chunk-MUHWHQVM.cjs} +1 -1
- package/dist/{chunk-Z7Y2NPHY.js → chunk-N5T4M4GU.js} +1 -1
- package/dist/{chunk-TIER4SUC.js → chunk-OEWZUX4G.js} +346 -47
- package/dist/chunk-OF3BECU2.cjs +69 -0
- package/dist/{chunk-RSJ3TWJQ.cjs → chunk-OKMJLEXX.cjs} +5 -5
- package/dist/chunk-OPRS4IQZ.js +153 -0
- package/dist/{chunk-XK5CIRMQ.js → chunk-SIZQQD6T.js} +72 -2
- package/dist/{chunk-DKPXN34V.cjs → chunk-SQYVORNP.cjs} +1334 -510
- package/dist/chunk-SYQF7OIR.js +52 -0
- package/dist/{chunk-S4WBV25P.js → chunk-TOVPMMHM.js} +111 -24
- package/dist/{chunk-QYTT6GE3.js → chunk-UDCC7EMR.js} +1093 -269
- package/dist/{chunk-E3UCVHLP.cjs → chunk-UNWKG6EI.cjs} +5 -38
- package/dist/{chunk-ZLX3KGXX.js → chunk-UX7VWA46.js} +135 -23
- package/dist/{chunk-7YN22L2H.cjs → chunk-V4TXMEJS.cjs} +5 -5
- package/dist/chunk-VRFBB5DB.js +44 -0
- package/dist/chunk-WB6PH56J.js +166 -0
- package/dist/{chunk-G2UTNSGU.js → chunk-WNKWAEUR.js} +7 -1
- package/dist/{chunk-SUEGJ75D.cjs → chunk-WYTEQ4Q5.cjs} +25 -2
- package/dist/{chunk-5ENKBSWO.js → chunk-YJVHCHQX.js} +42 -6
- package/dist/{chunk-VQY5W4YW.cjs → chunk-YK57NMIF.cjs} +44 -8
- package/dist/{chunk-KFI44N2R.cjs → chunk-YNJNTCSQ.cjs} +10 -1
- package/dist/completion/ApiKeyPoolService.cjs +12 -0
- package/dist/completion/ApiKeyPoolService.d.cts +6 -0
- package/dist/completion/ApiKeyPoolService.d.ts +6 -0
- package/dist/completion/ApiKeyPoolService.js +12 -0
- package/dist/completion/CompletionService.cjs +27 -24
- package/dist/completion/CompletionService.d.cts +2 -1
- package/dist/completion/CompletionService.d.ts +2 -1
- package/dist/completion/CompletionService.js +26 -23
- package/dist/completion.cjs +27 -24
- package/dist/completion.d.cts +2 -1
- package/dist/completion.d.ts +2 -1
- package/dist/completion.js +26 -23
- package/dist/index.cjs +53 -29
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +56 -32
- package/dist/{keyPolicy-Cp0FntZl.d.cts → keyPolicy-zdWTu3CQ.d.cts} +1 -1
- package/dist/{keyPolicy-Cp0FntZl.d.ts → keyPolicy-zdWTu3CQ.d.ts} +1 -1
- package/dist/outbound-api/auditCapture.cjs +2 -2
- package/dist/outbound-api/auditCapture.d.cts +5 -3
- package/dist/outbound-api/auditCapture.d.ts +5 -3
- package/dist/outbound-api/auditCapture.js +1 -1
- package/dist/outbound-api/quotaWarn.d.cts +1 -1
- package/dist/outbound-api/quotaWarn.d.ts +1 -1
- package/dist/outbound-api/routeResolver.cjs +2 -2
- package/dist/outbound-api/routeResolver.d.cts +4 -3
- package/dist/outbound-api/routeResolver.d.ts +4 -3
- package/dist/outbound-api/routeResolver.js +1 -1
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +1 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +1 -0
- package/dist/outbound-api/types.d.cts +3 -2
- package/dist/outbound-api/types.d.ts +3 -2
- package/dist/outbound-api.cjs +49 -25
- package/dist/outbound-api.d.cts +127 -85
- package/dist/outbound-api.d.ts +127 -85
- package/dist/outbound-api.js +52 -28
- package/dist/pipeline/AccountAllowanceScheduling.cjs +15 -0
- package/dist/pipeline/AccountAllowanceScheduling.d.cts +90 -0
- package/dist/pipeline/AccountAllowanceScheduling.d.ts +90 -0
- package/dist/pipeline/AccountAllowanceScheduling.js +15 -0
- package/dist/pipeline/AccountAllowanceStore.cjs +20 -0
- package/dist/pipeline/AccountAllowanceStore.d.cts +77 -0
- package/dist/pipeline/AccountAllowanceStore.d.ts +77 -0
- package/dist/pipeline/AccountAllowanceStore.js +20 -0
- package/dist/pipeline/AuthSource.d.cts +11 -2
- package/dist/pipeline/AuthSource.d.ts +11 -2
- package/dist/pipeline/BoundAccountSelectionError.cjs +10 -0
- package/dist/pipeline/BoundAccountSelectionError.d.cts +28 -0
- package/dist/pipeline/BoundAccountSelectionError.d.ts +28 -0
- package/dist/pipeline/BoundAccountSelectionError.js +10 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.cts +1 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.ts +1 -0
- package/dist/pipeline/SubscriptionAccountHealth.cjs +4 -2
- package/dist/pipeline/SubscriptionAccountHealth.d.cts +34 -6
- package/dist/pipeline/SubscriptionAccountHealth.d.ts +34 -6
- package/dist/pipeline/SubscriptionAccountHealth.js +3 -1
- package/dist/pipeline/SubscriptionAuthSource.cjs +5 -2
- package/dist/pipeline/SubscriptionAuthSource.d.cts +1 -0
- package/dist/pipeline/SubscriptionAuthSource.d.ts +1 -0
- package/dist/pipeline/SubscriptionAuthSource.js +4 -1
- package/dist/pipeline/SubscriptionAuthStrategy.d.cts +8 -3
- package/dist/pipeline/SubscriptionAuthStrategy.d.ts +8 -3
- package/dist/pipeline/executeProviderCall.cjs +2 -2
- package/dist/pipeline/executeProviderCall.d.cts +8 -3
- package/dist/pipeline/executeProviderCall.d.ts +8 -3
- package/dist/pipeline/executeProviderCall.js +1 -1
- package/dist/pipeline/resolveProviderChain.d.cts +2 -3
- package/dist/pipeline/resolveProviderChain.d.ts +2 -3
- package/dist/pipeline/resolveSubscriptionChain.d.cts +1 -0
- package/dist/pipeline/resolveSubscriptionChain.d.ts +1 -0
- package/dist/pipeline/upstreamFetch.cjs +4 -2
- package/dist/pipeline/upstreamFetch.d.cts +6 -0
- package/dist/pipeline/upstreamFetch.d.ts +6 -0
- package/dist/pipeline/upstreamFetch.js +3 -1
- 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/ports/pricing-store.d.cts +4 -3
- package/dist/ports/pricing-store.d.ts +4 -3
- package/dist/ports.d.cts +4 -3
- package/dist/ports.d.ts +4 -3
- package/dist/provider-proxy/ProviderProxy.cjs +27 -24
- package/dist/provider-proxy/ProviderProxy.d.cts +1 -0
- package/dist/provider-proxy/ProviderProxy.d.ts +1 -0
- package/dist/provider-proxy/ProviderProxy.js +26 -23
- package/dist/provider-proxy/ingress/providerProxyShared.cjs +31 -24
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +50 -5
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +50 -5
- package/dist/provider-proxy/ingress/providerProxyShared.js +30 -23
- package/dist/provider-proxy/matchText.cjs +4 -2
- package/dist/provider-proxy/matchText.d.cts +41 -6
- package/dist/provider-proxy/matchText.d.ts +41 -6
- package/dist/provider-proxy/matchText.js +3 -1
- package/dist/provider-proxy/types.d.cts +30 -2
- package/dist/provider-proxy/types.d.ts +30 -2
- package/dist/provider-proxy.cjs +27 -24
- package/dist/provider-proxy.d.cts +1 -0
- package/dist/provider-proxy.d.ts +1 -0
- package/dist/provider-proxy.js +26 -23
- package/dist/{routeResolver-C4T7yClx.d.ts → routeResolver-CHTv_Bs5.d.ts} +1 -1
- package/dist/{routeResolver-CB3tvJKy.d.cts → routeResolver-kepxnfcC.d.cts} +1 -1
- package/dist/transformer/TransformerChainExecutor.cjs +2 -2
- package/dist/transformer/TransformerChainExecutor.js +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.d.cts +1 -1
- package/dist/transformer/transformers/OpenAIResponseTransformer.d.ts +1 -1
- package/dist/transformer/transformers/OpenAIResponseTransformer.js +1 -1
- package/dist/transformer/transformers/OpenAITransformer.cjs +6 -0
- package/dist/transformer/transformers/OpenAITransformer.d.cts +68 -0
- package/dist/transformer/transformers/OpenAITransformer.d.ts +68 -0
- package/dist/transformer/transformers/OpenAITransformer.js +6 -0
- package/dist/transformer/transformers.cjs +8 -14
- package/dist/transformer/transformers.d.cts +12 -48
- package/dist/transformer/transformers.d.ts +12 -48
- package/dist/transformer/transformers.js +11 -17
- package/dist/transformer/types.d.cts +17 -0
- package/dist/transformer/types.d.ts +17 -0
- package/dist/transformer.cjs +11 -12
- package/dist/transformer.d.cts +1 -2
- package/dist/transformer.d.ts +1 -2
- package/dist/transformer.js +11 -12
- package/dist/{types-B8Arpxn0.d.cts → types-H3wGZSOo.d.cts} +134 -7
- package/dist/{types-lgPYQuZS.d.ts → types-hsoKrVQS.d.ts} +134 -7
- package/dist/usage/pricing-engine.cjs +2 -2
- package/dist/usage/pricing-engine.d.cts +9 -1
- package/dist/usage/pricing-engine.d.ts +9 -1
- package/dist/usage/pricing-engine.js +1 -1
- package/dist/usage.cjs +2 -2
- package/dist/usage.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-C72S6TTV.cjs +0 -49
- package/dist/chunk-DWKQZ2SK.cjs +0 -254
- package/dist/chunk-IAXWICZU.js +0 -69
- package/dist/chunk-QWBWGODS.cjs +0 -69
- package/dist/chunk-RGTR7CIO.cjs +0 -235
- package/dist/chunk-VPBAUQ2M.js +0 -235
- package/dist/chunk-YMFAJFQT.cjs +0 -173
- package/dist/chunk-Z7LUNECE.js +0 -49
- package/dist/chunk-ZJZAOKI7.js +0 -254
- package/dist/transformer/transformers/OpenCodeGoTransformer.cjs +0 -6
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.cts +0 -51
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.ts +0 -51
- package/dist/transformer/transformers/OpenCodeGoTransformer.js +0 -6
- package/dist/transformer/transformers/ReasoningTransformer.cjs +0 -6
- package/dist/transformer/transformers/ReasoningTransformer.d.cts +0 -47
- package/dist/transformer/transformers/ReasoningTransformer.d.ts +0 -47
- package/dist/transformer/transformers/ReasoningTransformer.js +0 -6
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSharedAccountAllowanceStore
|
|
3
|
+
} from "./chunk-MKF46GNZ.js";
|
|
4
|
+
|
|
5
|
+
// src/pipeline/AccountAllowanceScheduling.ts
|
|
6
|
+
var DEFAULT_POLICY = {
|
|
7
|
+
enabled: false,
|
|
8
|
+
demoteAtPercent: 80,
|
|
9
|
+
pauseAtPercent: 98,
|
|
10
|
+
priorityPenalty: 100
|
|
11
|
+
};
|
|
12
|
+
var HISTORY_LIMIT = 200;
|
|
13
|
+
var AccountAllowanceExhaustedError = class extends Error {
|
|
14
|
+
constructor(providerId, resumeAt) {
|
|
15
|
+
super(
|
|
16
|
+
resumeAt ? `All ${providerId} subscription accounts are paused by the allowance policy until ${resumeAt}` : `All ${providerId} subscription accounts are paused by the allowance policy`
|
|
17
|
+
);
|
|
18
|
+
this.providerId = providerId;
|
|
19
|
+
this.resumeAt = resumeAt;
|
|
20
|
+
this.name = "AccountAllowanceExhaustedError";
|
|
21
|
+
}
|
|
22
|
+
providerId;
|
|
23
|
+
resumeAt;
|
|
24
|
+
code = "account_allowance_exhausted";
|
|
25
|
+
status = 429;
|
|
26
|
+
};
|
|
27
|
+
function isAccountAllowanceExhaustedError(error) {
|
|
28
|
+
if (error instanceof AccountAllowanceExhaustedError) return true;
|
|
29
|
+
if (!error || typeof error !== "object") return false;
|
|
30
|
+
const value = error;
|
|
31
|
+
return value.code === "account_allowance_exhausted" && value.status === 429;
|
|
32
|
+
}
|
|
33
|
+
function freshWorstWindow(snapshot) {
|
|
34
|
+
return snapshot.windows.filter((window) => window.state === "fresh" && typeof window.usedPercent === "number").sort((left, right) => (right.usedPercent ?? -1) - (left.usedPercent ?? -1))[0];
|
|
35
|
+
}
|
|
36
|
+
var AccountAllowanceScheduling = class {
|
|
37
|
+
constructor(store = getSharedAccountAllowanceStore(), now = Date.now) {
|
|
38
|
+
this.store = store;
|
|
39
|
+
this.now = now;
|
|
40
|
+
}
|
|
41
|
+
store;
|
|
42
|
+
now;
|
|
43
|
+
config = { ...DEFAULT_POLICY };
|
|
44
|
+
history = [];
|
|
45
|
+
lastRecorded = /* @__PURE__ */ new Map();
|
|
46
|
+
configure(config) {
|
|
47
|
+
this.config = config ? { ...config } : { ...DEFAULT_POLICY };
|
|
48
|
+
}
|
|
49
|
+
getConfig() {
|
|
50
|
+
return { ...this.config };
|
|
51
|
+
}
|
|
52
|
+
evaluate(providerId, accountId, basePriority, now = this.now()) {
|
|
53
|
+
const decision = this.decide(providerId, accountId, basePriority, now);
|
|
54
|
+
this.recordApplied(decision);
|
|
55
|
+
return decision;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Project the current policy result without recording an applied decision.
|
|
59
|
+
* Admin/read-model callers use this so polling an account list never creates
|
|
60
|
+
* synthetic demotion/pause history entries.
|
|
61
|
+
*/
|
|
62
|
+
preview(providerId, accountId, basePriority, now = this.now()) {
|
|
63
|
+
return this.decide(providerId, accountId, basePriority, now);
|
|
64
|
+
}
|
|
65
|
+
decide(providerId, accountId, basePriority, now) {
|
|
66
|
+
const decidedAt = new Date(now).toISOString();
|
|
67
|
+
const base = {
|
|
68
|
+
providerId,
|
|
69
|
+
accountId,
|
|
70
|
+
basePriority,
|
|
71
|
+
effectivePriority: basePriority,
|
|
72
|
+
schedulable: true,
|
|
73
|
+
decidedAt
|
|
74
|
+
};
|
|
75
|
+
if (!this.config.enabled) return { ...base, action: "ignore", reason: "policy-disabled" };
|
|
76
|
+
if (providerId !== "claude" && providerId !== "codex") {
|
|
77
|
+
return { ...base, action: "ignore", reason: "provider-unsupported" };
|
|
78
|
+
}
|
|
79
|
+
const snapshot = this.store.get(providerId, accountId, now);
|
|
80
|
+
if (!snapshot) return { ...base, action: "ignore", reason: "snapshot-missing" };
|
|
81
|
+
const worst = freshWorstWindow(snapshot);
|
|
82
|
+
if (!worst || worst.usedPercent === null) {
|
|
83
|
+
return { ...base, action: "ignore", reason: "snapshot-not-fresh", observedAt: snapshot.observedAt };
|
|
84
|
+
}
|
|
85
|
+
const resumeAt = worst.resetsAt ?? snapshot.expiresAt;
|
|
86
|
+
const resumeMs = resumeAt ? Date.parse(resumeAt) : Number.NaN;
|
|
87
|
+
const canPauseUntilKnownDeadline = Number.isFinite(resumeMs) && resumeMs > now;
|
|
88
|
+
let decision;
|
|
89
|
+
if (worst.usedPercent >= this.config.pauseAtPercent && canPauseUntilKnownDeadline) {
|
|
90
|
+
decision = {
|
|
91
|
+
...base,
|
|
92
|
+
action: "pause",
|
|
93
|
+
reason: "pause-threshold",
|
|
94
|
+
schedulable: false,
|
|
95
|
+
usedPercent: worst.usedPercent,
|
|
96
|
+
observedAt: snapshot.observedAt,
|
|
97
|
+
resumeAt
|
|
98
|
+
};
|
|
99
|
+
} else if (worst.usedPercent >= this.config.demoteAtPercent) {
|
|
100
|
+
decision = {
|
|
101
|
+
...base,
|
|
102
|
+
action: "demote",
|
|
103
|
+
reason: "demote-threshold",
|
|
104
|
+
effectivePriority: basePriority + this.config.priorityPenalty,
|
|
105
|
+
usedPercent: worst.usedPercent,
|
|
106
|
+
observedAt: snapshot.observedAt,
|
|
107
|
+
resumeAt: canPauseUntilKnownDeadline ? resumeAt : void 0
|
|
108
|
+
};
|
|
109
|
+
} else {
|
|
110
|
+
decision = {
|
|
111
|
+
...base,
|
|
112
|
+
action: "normal",
|
|
113
|
+
reason: "below-threshold",
|
|
114
|
+
usedPercent: worst.usedPercent,
|
|
115
|
+
observedAt: snapshot.observedAt,
|
|
116
|
+
resumeAt: canPauseUntilKnownDeadline ? resumeAt : void 0
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return decision;
|
|
120
|
+
}
|
|
121
|
+
getHistory() {
|
|
122
|
+
return this.history.map((decision) => ({ ...decision }));
|
|
123
|
+
}
|
|
124
|
+
clearHistory() {
|
|
125
|
+
this.history.length = 0;
|
|
126
|
+
this.lastRecorded.clear();
|
|
127
|
+
}
|
|
128
|
+
recordApplied(decision) {
|
|
129
|
+
if (decision.action !== "demote" && decision.action !== "pause") return;
|
|
130
|
+
const key = `${decision.providerId}\0${decision.accountId}`;
|
|
131
|
+
const signature = `${decision.action}\0${decision.observedAt ?? ""}\0${decision.usedPercent ?? ""}`;
|
|
132
|
+
if (this.lastRecorded.get(key) === signature) return;
|
|
133
|
+
this.lastRecorded.set(key, signature);
|
|
134
|
+
this.history.push({ ...decision });
|
|
135
|
+
if (this.history.length > HISTORY_LIMIT) this.history.splice(0, this.history.length - HISTORY_LIMIT);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var sharedScheduling = null;
|
|
139
|
+
function getSharedAccountAllowanceScheduling() {
|
|
140
|
+
if (!sharedScheduling) sharedScheduling = new AccountAllowanceScheduling();
|
|
141
|
+
return sharedScheduling;
|
|
142
|
+
}
|
|
143
|
+
function __resetSharedAccountAllowanceSchedulingForTests() {
|
|
144
|
+
sharedScheduling = null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export {
|
|
148
|
+
AccountAllowanceExhaustedError,
|
|
149
|
+
isAccountAllowanceExhaustedError,
|
|
150
|
+
AccountAllowanceScheduling,
|
|
151
|
+
getSharedAccountAllowanceScheduling,
|
|
152
|
+
__resetSharedAccountAllowanceSchedulingForTests
|
|
153
|
+
};
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getUpstreamTracePath,
|
|
3
|
+
redactHeaders,
|
|
4
|
+
writeUpstreamTrace
|
|
5
|
+
} from "./chunk-SYQF7OIR.js";
|
|
6
|
+
import {
|
|
7
|
+
getSharedAccountAllowanceStore
|
|
8
|
+
} from "./chunk-MKF46GNZ.js";
|
|
9
|
+
|
|
1
10
|
// src/pipeline/upstreamFetch.ts
|
|
2
11
|
import { Agent, buildConnector, ProxyAgent } from "undici";
|
|
3
12
|
import { SocksClient } from "socks";
|
|
@@ -95,8 +104,69 @@ function resolveUpstreamDispatcher(ctx) {
|
|
|
95
104
|
}
|
|
96
105
|
function fetchUpstream(url, init, ctx) {
|
|
97
106
|
const dispatcher = resolveUpstreamDispatcher({ ...ctx ?? {}, url });
|
|
98
|
-
|
|
99
|
-
|
|
107
|
+
const doFetch = dispatcher ? () => fetch(url, { ...init, dispatcher }) : () => fetch(url, init);
|
|
108
|
+
const doFetchWithAllowanceTap = () => doFetch().then((response) => {
|
|
109
|
+
if (ctx?.providerId === "codex" && ctx.accountId) {
|
|
110
|
+
try {
|
|
111
|
+
getSharedAccountAllowanceStore().recordCodexHeaders(ctx.accountId, response.headers);
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return response;
|
|
116
|
+
});
|
|
117
|
+
const tracePath = getUpstreamTracePath();
|
|
118
|
+
if (!tracePath || !ctx?.providerId) return doFetchWithAllowanceTap();
|
|
119
|
+
const startedAt = Date.now();
|
|
120
|
+
const method = typeof init.method === "string" ? init.method : "GET";
|
|
121
|
+
const requestHeaders = redactHeaders(
|
|
122
|
+
init.headers
|
|
123
|
+
);
|
|
124
|
+
const requestBody = typeof init.body === "string" ? init.body : null;
|
|
125
|
+
const providerId = ctx.providerId;
|
|
126
|
+
const accountId = ctx.accountId;
|
|
127
|
+
return doFetchWithAllowanceTap().then((res) => {
|
|
128
|
+
const clone = res.clone();
|
|
129
|
+
const status = res.status;
|
|
130
|
+
const statusText = res.statusText;
|
|
131
|
+
const responseHeaders = redactHeaders(res.headers);
|
|
132
|
+
void clone.text().then((body) => {
|
|
133
|
+
writeUpstreamTrace({
|
|
134
|
+
ts: startedAt,
|
|
135
|
+
durationMs: Date.now() - startedAt,
|
|
136
|
+
providerId,
|
|
137
|
+
accountId,
|
|
138
|
+
url,
|
|
139
|
+
method,
|
|
140
|
+
requestHeaders,
|
|
141
|
+
requestBody,
|
|
142
|
+
status,
|
|
143
|
+
statusText,
|
|
144
|
+
responseHeaders,
|
|
145
|
+
responseBody: body,
|
|
146
|
+
responseBytes: Buffer.byteLength(body, "utf8")
|
|
147
|
+
});
|
|
148
|
+
}).catch(() => {
|
|
149
|
+
});
|
|
150
|
+
return res;
|
|
151
|
+
}).catch((err) => {
|
|
152
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
153
|
+
writeUpstreamTrace({
|
|
154
|
+
ts: startedAt,
|
|
155
|
+
durationMs: Date.now() - startedAt,
|
|
156
|
+
providerId,
|
|
157
|
+
accountId,
|
|
158
|
+
url,
|
|
159
|
+
method,
|
|
160
|
+
requestHeaders,
|
|
161
|
+
requestBody,
|
|
162
|
+
status: 0,
|
|
163
|
+
statusText: "",
|
|
164
|
+
responseHeaders: {},
|
|
165
|
+
responseBody: `<UPSTREAM FETCH ERROR: ${msg}>`,
|
|
166
|
+
responseBytes: 0
|
|
167
|
+
});
|
|
168
|
+
throw err;
|
|
169
|
+
});
|
|
100
170
|
}
|
|
101
171
|
function __resetUpstreamProxyForTests() {
|
|
102
172
|
resolver = null;
|