@finalai/agent-provider 0.13.0 → 0.14.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/README.md
CHANGED
|
@@ -47,6 +47,21 @@ checks that the settings name the harness. `harnessCheckInputOf` turns a compose
|
|
|
47
47
|
harness into the requirements check's input. A harness is opened with the agent
|
|
48
48
|
document, the resolved settings and one sandbox endpoint per capability it drives.
|
|
49
49
|
|
|
50
|
+
A harness that runs as a process in a sandbox gets its model credential through the
|
|
51
|
+
credential broker, not in its environment. `./model-credentials` (Node) carries both
|
|
52
|
+
sides: a host starts the process with `modelCredentialEnv(grant)` (the broker URL, the
|
|
53
|
+
`model` route grant and the harness session), and the process reads it with
|
|
54
|
+
`takeModelCredentialGrant(process.env)`, which also removes it so nothing the harness
|
|
55
|
+
runs inherits it. `harnessModelCredentials` redeems the grant at `<brokerUrl>/refresh`
|
|
56
|
+
for the at-open credential, renews it before each grant expires (and once after a
|
|
57
|
+
vendor refusal), and serves it through a loopback proxy: the harness is given the
|
|
58
|
+
proxy's base URL and a local key, and each request leaves with the current credential.
|
|
59
|
+
When a vendor answers 429 to a subscription or API key, the proxy reports it to the
|
|
60
|
+
broker (`<brokerUrl>/subscription-exhausted`, which skips that credential until it
|
|
61
|
+
resets) and sends that request once more on the next credential the broker resolves,
|
|
62
|
+
such as platform credits. Later requests follow the broker, which tries the
|
|
63
|
+
subscription first again once its limit resets; a 429 on platform credits is passed on.
|
|
64
|
+
|
|
50
65
|
## Sandboxes
|
|
51
66
|
|
|
52
67
|
A sandbox is a resource. A provider implements `create`, `attach` (reporting
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type AtOpenModelCredentialIface } from "./harness.mjs";
|
|
2
|
+
export declare enum ModelCredentialEnv {
|
|
3
|
+
BrokerUrl = "FINAL_BROKER_URL",
|
|
4
|
+
ModelGrant = "FINAL_MODEL_GRANT",
|
|
5
|
+
HarnessSessionId = "FINAL_HARNESS_SESSION_ID"
|
|
6
|
+
}
|
|
7
|
+
export interface ModelCredentialGrantIface {
|
|
8
|
+
brokerUrl: string;
|
|
9
|
+
token: string;
|
|
10
|
+
harnessSessionId: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const modelCredentialEnv: (grant: ModelCredentialGrantIface) => Record<string, string>;
|
|
13
|
+
export declare const modelCredentialGrantOf: (env: Record<string, string | undefined>) => ModelCredentialGrantIface | null;
|
|
14
|
+
export declare const takeModelCredentialGrant: (env: Record<string, string | undefined>) => ModelCredentialGrantIface | null;
|
|
15
|
+
export interface ModelCredentialsDepsIface {
|
|
16
|
+
fetch: typeof fetch;
|
|
17
|
+
now: () => number;
|
|
18
|
+
setTimer: (callback: () => void, delayMs: number) => () => void;
|
|
19
|
+
log: (line: string) => void;
|
|
20
|
+
}
|
|
21
|
+
export interface ModelCredentialsIface {
|
|
22
|
+
current(): AtOpenModelCredentialIface | null;
|
|
23
|
+
subscribe(listener: (credential: AtOpenModelCredentialIface | null) => void): () => void;
|
|
24
|
+
refresh(): Promise<AtOpenModelCredentialIface | null>;
|
|
25
|
+
rejected(): Promise<void>;
|
|
26
|
+
exhausted(report: {
|
|
27
|
+
fingerprint: string;
|
|
28
|
+
retryAfterSeconds: number | null;
|
|
29
|
+
}): Promise<AtOpenModelCredentialIface | null>;
|
|
30
|
+
close(): void;
|
|
31
|
+
}
|
|
32
|
+
export declare const openModelCredentials: (args: {
|
|
33
|
+
grant: ModelCredentialGrantIface;
|
|
34
|
+
log: (line: string) => void;
|
|
35
|
+
deps?: Partial<ModelCredentialsDepsIface>;
|
|
36
|
+
}) => Promise<ModelCredentialsIface>;
|
|
37
|
+
export declare const modelCredentialHeaders: (credential: AtOpenModelCredentialIface) => Record<string, string> | null;
|
|
38
|
+
export interface ModelUpstreamIface {
|
|
39
|
+
baseUrl: string;
|
|
40
|
+
headers: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
export interface ModelCredentialProxyIface {
|
|
43
|
+
baseUrl: string;
|
|
44
|
+
localKey: string;
|
|
45
|
+
update(upstream: ModelUpstreamIface | null): void;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
export declare const startModelCredentialProxy: (args: {
|
|
49
|
+
upstream: ModelUpstreamIface | null;
|
|
50
|
+
onUnauthorized?: () => void;
|
|
51
|
+
onLimited?: (limit: {
|
|
52
|
+
sent: ModelUpstreamIface;
|
|
53
|
+
retryAfter: string | null;
|
|
54
|
+
}) => Promise<ModelUpstreamIface | null>;
|
|
55
|
+
}) => Promise<ModelCredentialProxyIface>;
|
|
56
|
+
export interface HarnessModelCredentialsIface {
|
|
57
|
+
resolve(): Promise<{
|
|
58
|
+
credential: AtOpenModelCredentialIface | null;
|
|
59
|
+
proxy: ModelCredentialProxyIface;
|
|
60
|
+
}>;
|
|
61
|
+
refresh(): Promise<AtOpenModelCredentialIface | null>;
|
|
62
|
+
exhausted(report: {
|
|
63
|
+
fingerprint: string;
|
|
64
|
+
retryAfterSeconds: number | null;
|
|
65
|
+
}): Promise<AtOpenModelCredentialIface | null>;
|
|
66
|
+
close(): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
export declare const retryAfterSecondsOf: (value: string | null, nowMs: number) => number | null;
|
|
69
|
+
export declare const harnessModelCredentials: (args: {
|
|
70
|
+
grant: ModelCredentialGrantIface;
|
|
71
|
+
upstreamOf: (credential: AtOpenModelCredentialIface) => ModelUpstreamIface | null;
|
|
72
|
+
log: (line: string) => void;
|
|
73
|
+
deps?: Partial<ModelCredentialsDepsIface>;
|
|
74
|
+
}) => HarnessModelCredentialsIface;
|
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
createServer,
|
|
4
|
+
request as httpRequest
|
|
5
|
+
} from "node:http";
|
|
6
|
+
import { request as httpsRequest } from "node:https";
|
|
7
|
+
import { pipeline } from "node:stream";
|
|
8
|
+
import { InferenceSourceKind } from "@finalai/agent-protocol/agent-platform";
|
|
9
|
+
import * as z from "zod";
|
|
10
|
+
import { CredentialRoute } from "./credential-routes.mjs";
|
|
11
|
+
import {
|
|
12
|
+
credentialRefreshResultSchema
|
|
13
|
+
} from "./harness.mjs";
|
|
14
|
+
var ModelCredentialEnv = /* @__PURE__ */ ((ModelCredentialEnv2) => {
|
|
15
|
+
ModelCredentialEnv2["BrokerUrl"] = "FINAL_BROKER_URL";
|
|
16
|
+
ModelCredentialEnv2["ModelGrant"] = "FINAL_MODEL_GRANT";
|
|
17
|
+
ModelCredentialEnv2["HarnessSessionId"] = "FINAL_HARNESS_SESSION_ID";
|
|
18
|
+
return ModelCredentialEnv2;
|
|
19
|
+
})(ModelCredentialEnv || {});
|
|
20
|
+
const modelCredentialEnv = (grant) => ({
|
|
21
|
+
["FINAL_BROKER_URL" /* BrokerUrl */]: grant.brokerUrl,
|
|
22
|
+
["FINAL_MODEL_GRANT" /* ModelGrant */]: grant.token,
|
|
23
|
+
["FINAL_HARNESS_SESSION_ID" /* HarnessSessionId */]: grant.harnessSessionId
|
|
24
|
+
});
|
|
25
|
+
const modelCredentialGrantOf = (env) => {
|
|
26
|
+
const brokerUrl = env["FINAL_BROKER_URL" /* BrokerUrl */];
|
|
27
|
+
const token = env["FINAL_MODEL_GRANT" /* ModelGrant */];
|
|
28
|
+
const harnessSessionId = env["FINAL_HARNESS_SESSION_ID" /* HarnessSessionId */];
|
|
29
|
+
if (brokerUrl == null || brokerUrl === "" || token == null || token === "") return null;
|
|
30
|
+
if (harnessSessionId == null || harnessSessionId === "") return null;
|
|
31
|
+
return { brokerUrl, token, harnessSessionId };
|
|
32
|
+
};
|
|
33
|
+
const takeModelCredentialGrant = (env) => {
|
|
34
|
+
const grant = modelCredentialGrantOf(env);
|
|
35
|
+
for (const name of Object.values(ModelCredentialEnv)) Reflect.deleteProperty(env, name);
|
|
36
|
+
return grant;
|
|
37
|
+
};
|
|
38
|
+
const REFRESH_FRACTION = 2 / 3;
|
|
39
|
+
const MIN_DELAY_MS = 1e3;
|
|
40
|
+
const MAX_DELAY_MS = 2147483647;
|
|
41
|
+
const RETRY_BASE_MS = 2e3;
|
|
42
|
+
const RETRY_MAX_MS = 6e4;
|
|
43
|
+
const REJECTED_MIN_INTERVAL_MS = 3e4;
|
|
44
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
45
|
+
const DEFAULT_EXHAUSTION_SECONDS = 15 * 60;
|
|
46
|
+
const ERROR_BODY_CHARS = 300;
|
|
47
|
+
const brokerErrorSchema = z.object({
|
|
48
|
+
error: z.string(),
|
|
49
|
+
reason: z.string().nullish(),
|
|
50
|
+
detail: z.string().nullish()
|
|
51
|
+
});
|
|
52
|
+
const defaultDeps = (log) => ({
|
|
53
|
+
fetch: globalThis.fetch,
|
|
54
|
+
now: Date.now,
|
|
55
|
+
setTimer: (callback, delayMs) => {
|
|
56
|
+
const timer = setTimeout(callback, delayMs);
|
|
57
|
+
timer.unref();
|
|
58
|
+
return () => clearTimeout(timer);
|
|
59
|
+
},
|
|
60
|
+
log
|
|
61
|
+
});
|
|
62
|
+
const openModelCredentials = async (args) => {
|
|
63
|
+
const deps = { ...defaultDeps(args.log), ...args.deps };
|
|
64
|
+
let descriptor = {
|
|
65
|
+
url: `${args.grant.brokerUrl.replace(/\/+$/u, "")}/refresh`,
|
|
66
|
+
token: args.grant.token,
|
|
67
|
+
harnessSessionId: args.grant.harnessSessionId,
|
|
68
|
+
expiresAt: ""
|
|
69
|
+
};
|
|
70
|
+
let credential = null;
|
|
71
|
+
let heldAtMs = deps.now();
|
|
72
|
+
let inFlight = null;
|
|
73
|
+
let failures = 0;
|
|
74
|
+
let retryAtMs = 0;
|
|
75
|
+
let rejectedHandled = false;
|
|
76
|
+
let lastRejectedMs = Number.NEGATIVE_INFINITY;
|
|
77
|
+
let stopped = false;
|
|
78
|
+
let closed = false;
|
|
79
|
+
let cancelTimer = null;
|
|
80
|
+
const resetTimers = /* @__PURE__ */ new Set();
|
|
81
|
+
let brokerUrl = args.grant.brokerUrl.replace(/\/+$/u, "");
|
|
82
|
+
const exhausting = /* @__PURE__ */ new Map();
|
|
83
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
84
|
+
const redeem = async () => {
|
|
85
|
+
let response;
|
|
86
|
+
let body;
|
|
87
|
+
try {
|
|
88
|
+
response = await deps.fetch(descriptor.url, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: {
|
|
91
|
+
authorization: `Bearer ${descriptor.token}`,
|
|
92
|
+
"content-type": "application/json",
|
|
93
|
+
"cache-control": "no-store"
|
|
94
|
+
},
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
harnessSessionId: descriptor.harnessSessionId,
|
|
97
|
+
routes: [CredentialRoute.Model]
|
|
98
|
+
}),
|
|
99
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
100
|
+
});
|
|
101
|
+
body = await response.text();
|
|
102
|
+
} catch (cause) {
|
|
103
|
+
return {
|
|
104
|
+
status: null,
|
|
105
|
+
message: `the credential broker is unreachable: ${cause instanceof Error ? cause.message : String(cause)}`
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
let reason = body.slice(0, ERROR_BODY_CHARS);
|
|
110
|
+
try {
|
|
111
|
+
const refused = brokerErrorSchema.safeParse(JSON.parse(body));
|
|
112
|
+
if (refused.success) {
|
|
113
|
+
reason = [refused.data.error, refused.data.reason, refused.data.detail].filter((part) => part != null && part !== "").join(": ");
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
status: response.status,
|
|
119
|
+
message: `the credential broker refused the grant (HTTP ${response.status}${reason === "" ? "" : `, ${reason}`})`
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
let json;
|
|
123
|
+
try {
|
|
124
|
+
json = JSON.parse(body);
|
|
125
|
+
} catch {
|
|
126
|
+
return { status: null, message: "the credential broker answered a non-JSON body" };
|
|
127
|
+
}
|
|
128
|
+
const parsed = credentialRefreshResultSchema.safeParse(json);
|
|
129
|
+
if (!parsed.success)
|
|
130
|
+
return { status: null, message: "the credential broker answered an invalid grant" };
|
|
131
|
+
heldAtMs = deps.now();
|
|
132
|
+
brokerUrl = parsed.data.brokerUrl.replace(/\/+$/u, "");
|
|
133
|
+
credential = parsed.data.atOpen;
|
|
134
|
+
if (parsed.data.refresh == null) {
|
|
135
|
+
stopped = true;
|
|
136
|
+
deps.log("model credential: the renewed grant has no refresh route");
|
|
137
|
+
} else descriptor = parsed.data.refresh;
|
|
138
|
+
for (const listener of listeners) listener(credential);
|
|
139
|
+
return null;
|
|
140
|
+
};
|
|
141
|
+
const schedule = () => {
|
|
142
|
+
cancelTimer?.();
|
|
143
|
+
cancelTimer = null;
|
|
144
|
+
if (closed || stopped) return;
|
|
145
|
+
const expiresAtMs = Date.parse(descriptor.expiresAt);
|
|
146
|
+
if (!Number.isFinite(expiresAtMs)) {
|
|
147
|
+
deps.log(`model credential: unparseable grant expiry ${descriptor.expiresAt}`);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const dueAtMs = failures > 0 ? retryAtMs : heldAtMs + (expiresAtMs - heldAtMs) * REFRESH_FRACTION;
|
|
151
|
+
cancelTimer = deps.setTimer(
|
|
152
|
+
() => {
|
|
153
|
+
cancelTimer = null;
|
|
154
|
+
void pull();
|
|
155
|
+
},
|
|
156
|
+
Math.max(MIN_DELAY_MS, Math.min(MAX_DELAY_MS, dueAtMs - deps.now()))
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
const pull = () => {
|
|
160
|
+
if (inFlight != null) return inFlight;
|
|
161
|
+
if (closed || stopped) return Promise.resolve();
|
|
162
|
+
cancelTimer?.();
|
|
163
|
+
cancelTimer = null;
|
|
164
|
+
const pending = (async () => {
|
|
165
|
+
if (deps.now() >= Date.parse(descriptor.expiresAt)) {
|
|
166
|
+
stopped = true;
|
|
167
|
+
deps.log("model credential: the grant expired before it was renewed");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const failure = await redeem();
|
|
171
|
+
if (failure == null) {
|
|
172
|
+
failures = 0;
|
|
173
|
+
retryAtMs = 0;
|
|
174
|
+
rejectedHandled = false;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (failure.status === 401 || failure.status === 403) {
|
|
178
|
+
stopped = true;
|
|
179
|
+
deps.log(`model credential: renewal stopped: ${failure.message}`);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
failures += 1;
|
|
183
|
+
retryAtMs = deps.now() + Math.min(RETRY_MAX_MS, RETRY_BASE_MS * 2 ** (failures - 1));
|
|
184
|
+
deps.log(`model credential: renewal failed (attempt ${failures}): ${failure.message}`);
|
|
185
|
+
})().finally(() => {
|
|
186
|
+
if (inFlight === pending) inFlight = null;
|
|
187
|
+
schedule();
|
|
188
|
+
});
|
|
189
|
+
inFlight = pending;
|
|
190
|
+
return pending;
|
|
191
|
+
};
|
|
192
|
+
const report = async (exhausted2) => {
|
|
193
|
+
try {
|
|
194
|
+
const response = await deps.fetch(`${brokerUrl}/subscription-exhausted`, {
|
|
195
|
+
method: "POST",
|
|
196
|
+
headers: {
|
|
197
|
+
authorization: `Bearer ${descriptor.token}`,
|
|
198
|
+
"content-type": "application/json",
|
|
199
|
+
"cache-control": "no-store"
|
|
200
|
+
},
|
|
201
|
+
body: JSON.stringify({ harnessSessionId: descriptor.harnessSessionId, ...exhausted2 }),
|
|
202
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
203
|
+
});
|
|
204
|
+
await response.text();
|
|
205
|
+
if (!response.ok)
|
|
206
|
+
deps.log(`model credential: the broker refused the exhaustion (HTTP ${response.status})`);
|
|
207
|
+
} catch (cause) {
|
|
208
|
+
deps.log(
|
|
209
|
+
`model credential: the exhaustion was not reported: ${cause instanceof Error ? cause.message : String(cause)}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
const exhausted = (exhaustion) => {
|
|
214
|
+
const shared = exhausting.get(exhaustion.fingerprint);
|
|
215
|
+
if (shared != null) return shared;
|
|
216
|
+
if (credential?.fingerprint !== exhaustion.fingerprint || closed || stopped)
|
|
217
|
+
return Promise.resolve(credential);
|
|
218
|
+
const pending = (async () => {
|
|
219
|
+
await report(exhaustion);
|
|
220
|
+
if (inFlight != null) await inFlight;
|
|
221
|
+
await pull();
|
|
222
|
+
const resetMs = (exhaustion.retryAfterSeconds ?? DEFAULT_EXHAUSTION_SECONDS) * 1e3;
|
|
223
|
+
const cancel = deps.setTimer(
|
|
224
|
+
() => {
|
|
225
|
+
resetTimers.delete(cancel);
|
|
226
|
+
void pull();
|
|
227
|
+
},
|
|
228
|
+
Math.max(MIN_DELAY_MS, Math.min(MAX_DELAY_MS, resetMs + MIN_DELAY_MS))
|
|
229
|
+
);
|
|
230
|
+
resetTimers.add(cancel);
|
|
231
|
+
return credential;
|
|
232
|
+
})().finally(() => exhausting.delete(exhaustion.fingerprint));
|
|
233
|
+
exhausting.set(exhaustion.fingerprint, pending);
|
|
234
|
+
return pending;
|
|
235
|
+
};
|
|
236
|
+
const opened = await redeem();
|
|
237
|
+
if (opened != null) throw new Error(opened.message);
|
|
238
|
+
schedule();
|
|
239
|
+
return {
|
|
240
|
+
current: () => credential,
|
|
241
|
+
subscribe: (listener) => {
|
|
242
|
+
listeners.add(listener);
|
|
243
|
+
return () => listeners.delete(listener);
|
|
244
|
+
},
|
|
245
|
+
refresh: async () => {
|
|
246
|
+
await pull();
|
|
247
|
+
return credential;
|
|
248
|
+
},
|
|
249
|
+
rejected: () => {
|
|
250
|
+
if (inFlight != null) return inFlight;
|
|
251
|
+
const now = deps.now();
|
|
252
|
+
if (rejectedHandled || now < retryAtMs || now - lastRejectedMs < REJECTED_MIN_INTERVAL_MS)
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
rejectedHandled = true;
|
|
255
|
+
lastRejectedMs = now;
|
|
256
|
+
return pull();
|
|
257
|
+
},
|
|
258
|
+
exhausted,
|
|
259
|
+
close: () => {
|
|
260
|
+
closed = true;
|
|
261
|
+
cancelTimer?.();
|
|
262
|
+
cancelTimer = null;
|
|
263
|
+
for (const cancel of resetTimers) cancel();
|
|
264
|
+
resetTimers.clear();
|
|
265
|
+
listeners.clear();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
const modelCredentialHeaders = (credential) => {
|
|
270
|
+
if (credential.subscription != null) {
|
|
271
|
+
return credential.subscription.format === "claude-setup-token" ? { authorization: `Bearer ${credential.subscription.value}` } : null;
|
|
272
|
+
}
|
|
273
|
+
if (credential.apiKey == null) return null;
|
|
274
|
+
if (credential.kind === InferenceSourceKind.ApiKey && credential.vendor === "anthropic")
|
|
275
|
+
return { "x-api-key": credential.apiKey };
|
|
276
|
+
return { authorization: `Bearer ${credential.apiKey}` };
|
|
277
|
+
};
|
|
278
|
+
const HOP_BY_HOP = /* @__PURE__ */ new Set([
|
|
279
|
+
"connection",
|
|
280
|
+
"keep-alive",
|
|
281
|
+
"proxy-authenticate",
|
|
282
|
+
"proxy-authorization",
|
|
283
|
+
"te",
|
|
284
|
+
"trailer",
|
|
285
|
+
"transfer-encoding",
|
|
286
|
+
"upgrade",
|
|
287
|
+
"host"
|
|
288
|
+
]);
|
|
289
|
+
const firstHeader = (value) => [value ?? []].flat()[0] ?? null;
|
|
290
|
+
const presentedKey = (headers) => {
|
|
291
|
+
const apiKey = firstHeader(headers["x-api-key"]);
|
|
292
|
+
if (apiKey != null) return apiKey;
|
|
293
|
+
const authorization = firstHeader(headers.authorization);
|
|
294
|
+
const bearer = authorization == null ? null : /^Bearer\s+(.+)$/iu.exec(authorization);
|
|
295
|
+
return bearer?.[1] ?? null;
|
|
296
|
+
};
|
|
297
|
+
const sameSecret = (presented, expected) => {
|
|
298
|
+
const left = Buffer.from(presented);
|
|
299
|
+
const right = Buffer.from(expected);
|
|
300
|
+
return left.byteLength === right.byteLength && timingSafeEqual(left, right);
|
|
301
|
+
};
|
|
302
|
+
const upstreamHeaders = (headers, upstream) => {
|
|
303
|
+
const next = {};
|
|
304
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
305
|
+
if (value == null || HOP_BY_HOP.has(name)) continue;
|
|
306
|
+
if (name === "x-api-key" || name === "authorization") continue;
|
|
307
|
+
next[name] = value;
|
|
308
|
+
}
|
|
309
|
+
for (const [name, value] of Object.entries(upstream.headers)) next[name.toLowerCase()] = value;
|
|
310
|
+
return next;
|
|
311
|
+
};
|
|
312
|
+
const fail = (res, status, message) => {
|
|
313
|
+
if (res.headersSent) {
|
|
314
|
+
res.destroy();
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
res.writeHead(status, { "content-type": "application/json" });
|
|
318
|
+
res.end(JSON.stringify({ error: { type: "model_credential_proxy", message } }));
|
|
319
|
+
};
|
|
320
|
+
const startModelCredentialProxy = async (args) => {
|
|
321
|
+
const localKey = `local-${randomBytes(24).toString("base64url")}`;
|
|
322
|
+
let upstream = args.upstream;
|
|
323
|
+
const forward = async (req, res) => {
|
|
324
|
+
const presented = presentedKey(req.headers);
|
|
325
|
+
if (presented == null || !sameSecret(presented, localKey)) {
|
|
326
|
+
fail(res, 401, "unauthorized");
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const first = upstream;
|
|
330
|
+
if (first == null) {
|
|
331
|
+
fail(res, 503, "no model credential");
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const chunks = [];
|
|
335
|
+
try {
|
|
336
|
+
for await (const chunk of req)
|
|
337
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
338
|
+
} catch {
|
|
339
|
+
res.destroy();
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const body = Buffer.concat(chunks);
|
|
343
|
+
const url = new URL(req.url ?? "/", "http://proxy");
|
|
344
|
+
let outgoing = null;
|
|
345
|
+
res.on("close", () => {
|
|
346
|
+
if (!res.writableFinished) outgoing?.destroy();
|
|
347
|
+
});
|
|
348
|
+
const send = (sent, resent) => {
|
|
349
|
+
const target = new URL(`${sent.baseUrl.replace(/\/+$/u, "")}${url.pathname}${url.search}`);
|
|
350
|
+
const request = (target.protocol === "https:" ? httpsRequest : httpRequest)(
|
|
351
|
+
target,
|
|
352
|
+
{
|
|
353
|
+
method: req.method,
|
|
354
|
+
headers: {
|
|
355
|
+
...upstreamHeaders(req.headers, sent),
|
|
356
|
+
host: target.host,
|
|
357
|
+
"content-length": String(body.byteLength)
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
(response) => {
|
|
361
|
+
void (async () => {
|
|
362
|
+
if (response.statusCode === 401 && upstream === sent) args.onUnauthorized?.();
|
|
363
|
+
if (response.statusCode === 429 && !resent && args.onLimited != null) {
|
|
364
|
+
const retryAfter = firstHeader(response.headers["retry-after"]);
|
|
365
|
+
const next = await args.onLimited({ sent, retryAfter }).catch(() => null);
|
|
366
|
+
if (next != null && next !== sent && !res.destroyed) {
|
|
367
|
+
response.resume();
|
|
368
|
+
send(next, true);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const headers = {};
|
|
373
|
+
for (const [name, value] of Object.entries(response.headers)) {
|
|
374
|
+
if (value != null && !HOP_BY_HOP.has(name)) headers[name] = value;
|
|
375
|
+
}
|
|
376
|
+
res.writeHead(response.statusCode ?? 502, headers);
|
|
377
|
+
pipeline(response, res, (cause) => {
|
|
378
|
+
if (cause == null) return;
|
|
379
|
+
request.destroy();
|
|
380
|
+
res.destroy(cause);
|
|
381
|
+
});
|
|
382
|
+
})();
|
|
383
|
+
}
|
|
384
|
+
);
|
|
385
|
+
outgoing = request;
|
|
386
|
+
request.on("error", (cause) => fail(res, 502, `upstream request failed: ${cause.message}`));
|
|
387
|
+
request.end(body);
|
|
388
|
+
};
|
|
389
|
+
send(first, false);
|
|
390
|
+
};
|
|
391
|
+
const server = createServer((req, res) => void forward(req, res));
|
|
392
|
+
server.unref();
|
|
393
|
+
const port = await new Promise((resolve, reject) => {
|
|
394
|
+
server.once("error", reject);
|
|
395
|
+
server.listen(0, "127.0.0.1", () => {
|
|
396
|
+
const address = server.address();
|
|
397
|
+
if (address == null || typeof address === "string")
|
|
398
|
+
reject(new Error("the model credential proxy did not bind a TCP port"));
|
|
399
|
+
else resolve(address.port);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
return {
|
|
403
|
+
baseUrl: `http://127.0.0.1:${port}`,
|
|
404
|
+
localKey,
|
|
405
|
+
update: (next) => {
|
|
406
|
+
upstream = next;
|
|
407
|
+
},
|
|
408
|
+
close: () => new Promise((resolve) => {
|
|
409
|
+
server.close(() => resolve());
|
|
410
|
+
server.closeAllConnections();
|
|
411
|
+
})
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
const EXHAUSTIBLE_KINDS = /* @__PURE__ */ new Set([
|
|
415
|
+
InferenceSourceKind.Subscription,
|
|
416
|
+
InferenceSourceKind.ApiKey
|
|
417
|
+
]);
|
|
418
|
+
const retryAfterSecondsOf = (value, nowMs) => {
|
|
419
|
+
if (value == null) return null;
|
|
420
|
+
const trimmed = value.trim();
|
|
421
|
+
if (/^\d+$/u.test(trimmed)) return Number(trimmed);
|
|
422
|
+
const at = Date.parse(trimmed);
|
|
423
|
+
return Number.isFinite(at) ? Math.max(0, Math.ceil((at - nowMs) / 1e3)) : null;
|
|
424
|
+
};
|
|
425
|
+
const harnessModelCredentials = (args) => {
|
|
426
|
+
let opening = null;
|
|
427
|
+
let closed = false;
|
|
428
|
+
const now = args.deps?.now ?? Date.now;
|
|
429
|
+
const owners = /* @__PURE__ */ new WeakMap();
|
|
430
|
+
let latest = null;
|
|
431
|
+
const upstream = (credential) => {
|
|
432
|
+
const mapped = credential == null ? null : args.upstreamOf(credential);
|
|
433
|
+
if (mapped != null && credential != null) owners.set(mapped, credential);
|
|
434
|
+
latest = mapped;
|
|
435
|
+
return mapped;
|
|
436
|
+
};
|
|
437
|
+
const open = () => {
|
|
438
|
+
opening ??= (async () => {
|
|
439
|
+
const credentials = await openModelCredentials({
|
|
440
|
+
grant: args.grant,
|
|
441
|
+
log: args.log,
|
|
442
|
+
deps: args.deps
|
|
443
|
+
});
|
|
444
|
+
try {
|
|
445
|
+
const proxy = await startModelCredentialProxy({
|
|
446
|
+
upstream: upstream(credentials.current()),
|
|
447
|
+
onUnauthorized: () => void credentials.rejected(),
|
|
448
|
+
onLimited: async ({ sent, retryAfter }) => {
|
|
449
|
+
const refused = owners.get(sent);
|
|
450
|
+
if (refused == null || !EXHAUSTIBLE_KINDS.has(refused.kind)) return null;
|
|
451
|
+
const next = await credentials.exhausted({
|
|
452
|
+
fingerprint: refused.fingerprint,
|
|
453
|
+
retryAfterSeconds: retryAfterSecondsOf(retryAfter, now())
|
|
454
|
+
});
|
|
455
|
+
if (next == null || next.fingerprint === refused.fingerprint) return null;
|
|
456
|
+
return latest;
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
credentials.subscribe((next) => proxy.update(upstream(next)));
|
|
460
|
+
return { credentials, proxy };
|
|
461
|
+
} catch (cause) {
|
|
462
|
+
credentials.close();
|
|
463
|
+
throw cause;
|
|
464
|
+
}
|
|
465
|
+
})().catch((cause) => {
|
|
466
|
+
opening = null;
|
|
467
|
+
throw cause;
|
|
468
|
+
});
|
|
469
|
+
return opening;
|
|
470
|
+
};
|
|
471
|
+
return {
|
|
472
|
+
resolve: async () => {
|
|
473
|
+
if (closed) throw new Error("the model credential is closed");
|
|
474
|
+
const { credentials, proxy } = await open();
|
|
475
|
+
return { credential: credentials.current(), proxy };
|
|
476
|
+
},
|
|
477
|
+
refresh: async () => {
|
|
478
|
+
if (opening == null) return null;
|
|
479
|
+
return (await opening).credentials.refresh();
|
|
480
|
+
},
|
|
481
|
+
exhausted: async (report) => {
|
|
482
|
+
if (opening == null) return null;
|
|
483
|
+
return (await opening).credentials.exhausted(report);
|
|
484
|
+
},
|
|
485
|
+
close: async () => {
|
|
486
|
+
closed = true;
|
|
487
|
+
const current = opening;
|
|
488
|
+
opening = null;
|
|
489
|
+
if (current == null) return;
|
|
490
|
+
const opened = await current.catch(() => null);
|
|
491
|
+
opened?.credentials.close();
|
|
492
|
+
await opened?.proxy.close();
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
};
|
|
496
|
+
export {
|
|
497
|
+
ModelCredentialEnv,
|
|
498
|
+
harnessModelCredentials,
|
|
499
|
+
modelCredentialEnv,
|
|
500
|
+
modelCredentialGrantOf,
|
|
501
|
+
modelCredentialHeaders,
|
|
502
|
+
openModelCredentials,
|
|
503
|
+
retryAfterSecondsOf,
|
|
504
|
+
startModelCredentialProxy,
|
|
505
|
+
takeModelCredentialGrant
|
|
506
|
+
};
|
|
507
|
+
//# sourceMappingURL=model-credentials.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/model-credentials.ts"],
|
|
4
|
+
"sourcesContent": ["import { randomBytes, timingSafeEqual } from 'node:crypto';\nimport {\n createServer,\n request as httpRequest,\n type ClientRequest,\n type IncomingHttpHeaders,\n type IncomingMessage,\n type OutgoingHttpHeaders,\n type ServerResponse,\n} from 'node:http';\nimport { request as httpsRequest } from 'node:https';\nimport { pipeline } from 'node:stream';\n\nimport { InferenceSourceKind } from '@finalai/agent-protocol/agent-platform';\nimport * as z from 'zod';\n\nimport { CredentialRoute } from \"./credential-routes.mjs\";\nimport {\n credentialRefreshResultSchema,\n type AtOpenModelCredentialIface,\n type CredentialGrantRefreshIface,\n} from \"./harness.mjs\";\n\n// A harness process started in a sandbox gets its model credential from the credential\n// broker, never in its environment: the host starts it with the broker's base URL, the\n// `model` route grant and the harness session it belongs to (ModelCredentialEnv). The\n// process redeems the grant at `<brokerUrl>/refresh` for the at-open credential and the next\n// grant, and keeps renewing before each expires (a platform credit token lives minutes).\n// Node only.\n\nexport enum ModelCredentialEnv {\n BrokerUrl = 'FINAL_BROKER_URL',\n ModelGrant = 'FINAL_MODEL_GRANT',\n HarnessSessionId = 'FINAL_HARNESS_SESSION_ID',\n}\n\nexport interface ModelCredentialGrantIface {\n // Base URL of the broker routes (`CredentialGrantIface.brokerUrl`).\n brokerUrl: string;\n // The `model` route grant's token.\n token: string;\n harnessSessionId: string;\n}\n\n// The environment a host starts a harness process with so it can reach its credential.\nexport const modelCredentialEnv = (grant: ModelCredentialGrantIface): Record<string, string> => ({\n [ModelCredentialEnv.BrokerUrl]: grant.brokerUrl,\n [ModelCredentialEnv.ModelGrant]: grant.token,\n [ModelCredentialEnv.HarnessSessionId]: grant.harnessSessionId,\n});\n\n// The grant a process was started with; null when it was started without one (the\n// harness then uses its own login).\nexport const modelCredentialGrantOf = (\n env: Record<string, string | undefined>,\n): ModelCredentialGrantIface | null => {\n const brokerUrl = env[ModelCredentialEnv.BrokerUrl];\n const token = env[ModelCredentialEnv.ModelGrant];\n const harnessSessionId = env[ModelCredentialEnv.HarnessSessionId];\n if (brokerUrl == null || brokerUrl === '' || token == null || token === '') return null;\n if (harnessSessionId == null || harnessSessionId === '') return null;\n return { brokerUrl, token, harnessSessionId };\n};\n\n// The grant, removed from `env` (the process's own, say) so that nothing the harness runs\n// inherits it: a harness with a shell could otherwise redeem it itself.\nexport const takeModelCredentialGrant = (\n env: Record<string, string | undefined>,\n): ModelCredentialGrantIface | null => {\n const grant = modelCredentialGrantOf(env);\n for (const name of Object.values(ModelCredentialEnv)) Reflect.deleteProperty(env, name);\n return grant;\n};\n\nconst REFRESH_FRACTION = 2 / 3;\nconst MIN_DELAY_MS = 1_000;\nconst MAX_DELAY_MS = 2_147_483_647;\nconst RETRY_BASE_MS = 2_000;\nconst RETRY_MAX_MS = 60_000;\nconst REJECTED_MIN_INTERVAL_MS = 30_000;\nconst REQUEST_TIMEOUT_MS = 15_000;\n// How long the broker skips an exhausted credential when the vendor gives no retry-after.\nconst DEFAULT_EXHAUSTION_SECONDS = 15 * 60;\nconst ERROR_BODY_CHARS = 300;\n\nconst brokerErrorSchema = z.object({\n error: z.string(),\n reason: z.string().nullish(),\n detail: z.string().nullish(),\n});\n\n// Why a pull got no credential: the broker's HTTP status (null when it never answered).\ninterface RedeemFailureIface {\n status: number | null;\n message: string;\n}\n\nexport interface ModelCredentialsDepsIface {\n fetch: typeof fetch;\n now: () => number;\n // Schedules `callback`; returns a cancel function.\n setTimer: (callback: () => void, delayMs: number) => () => void;\n log: (line: string) => void;\n}\n\nexport interface ModelCredentialsIface {\n // The credential the broker resolved last; null when it hands over none (the harness's own\n // login).\n current(): AtOpenModelCredentialIface | null;\n // Called with each renewed credential; returns an unsubscribe function.\n subscribe(listener: (credential: AtOpenModelCredentialIface | null) => void): () => void;\n // Renews now, or waits for the renewal in flight; resolves with the credential then held.\n refresh(): Promise<AtOpenModelCredentialIface | null>;\n // The vendor refused the current credential: renews once for it, at most every 30 s.\n rejected(): Promise<void>;\n // The vendor refused a credential for its usage or rate limit: reports it to the broker,\n // which skips it until it resets, and renews onto the chain's next source; resolves with\n // the credential then held. Renews again once the limit resets, so later requests try the\n // exhausted credential first again. Reports of one credential in flight share one report.\n exhausted(report: {\n fingerprint: string;\n retryAfterSeconds: number | null;\n }): Promise<AtOpenModelCredentialIface | null>;\n close(): void;\n}\n\nconst defaultDeps = (log: (line: string) => void): ModelCredentialsDepsIface => ({\n fetch: globalThis.fetch,\n now: Date.now,\n setTimer: (callback, delayMs) => {\n const timer = setTimeout(callback, delayMs);\n timer.unref();\n return () => clearTimeout(timer);\n },\n log,\n});\n\n// Redeems the grant for the credential (rejecting with the broker's reason when it refuses or\n// is unreachable), then renews it before each grant expires. Renewal is pulled\n// once at a time; failures back off exponentially; a refused grant (revoked, or its harness\n// session closed) stops renewing and keeps the last credential.\nexport const openModelCredentials = async (args: {\n grant: ModelCredentialGrantIface;\n log: (line: string) => void;\n deps?: Partial<ModelCredentialsDepsIface>;\n}): Promise<ModelCredentialsIface> => {\n const deps: ModelCredentialsDepsIface = { ...defaultDeps(args.log), ...args.deps };\n let descriptor: CredentialGrantRefreshIface = {\n url: `${args.grant.brokerUrl.replace(/\\/+$/u, '')}/refresh`,\n token: args.grant.token,\n harnessSessionId: args.grant.harnessSessionId,\n expiresAt: '',\n };\n let credential: AtOpenModelCredentialIface | null = null;\n let heldAtMs = deps.now();\n let inFlight: Promise<void> | null = null;\n let failures = 0;\n let retryAtMs = 0;\n let rejectedHandled = false;\n let lastRejectedMs = Number.NEGATIVE_INFINITY;\n let stopped = false;\n let closed = false;\n let cancelTimer: (() => void) | null = null;\n // One per exhaustion reported: each resolves again when its limit resets.\n const resetTimers = new Set<() => void>();\n let brokerUrl = args.grant.brokerUrl.replace(/\\/+$/u, '');\n const exhausting = new Map<string, Promise<AtOpenModelCredentialIface | null>>();\n const listeners = new Set<(next: AtOpenModelCredentialIface | null) => void>();\n\n // One pull: the next grant and the credential it carries, or why there is none.\n const redeem = async (): Promise<RedeemFailureIface | null> => {\n let response: Response;\n let body: string;\n try {\n response = await deps.fetch(descriptor.url, {\n method: 'POST',\n headers: {\n authorization: `Bearer ${descriptor.token}`,\n 'content-type': 'application/json',\n 'cache-control': 'no-store',\n },\n body: JSON.stringify({\n harnessSessionId: descriptor.harnessSessionId,\n routes: [CredentialRoute.Model],\n }),\n signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),\n });\n body = await response.text();\n } catch (cause) {\n return {\n status: null,\n message: `the credential broker is unreachable: ${cause instanceof Error ? cause.message : String(cause)}`,\n };\n }\n if (!response.ok) {\n let reason = body.slice(0, ERROR_BODY_CHARS);\n try {\n const refused = brokerErrorSchema.safeParse(JSON.parse(body));\n if (refused.success) {\n reason = [refused.data.error, refused.data.reason, refused.data.detail]\n .filter((part) => part != null && part !== '')\n .join(': ');\n }\n } catch {\n // Not JSON: the body excerpt stands.\n }\n return {\n status: response.status,\n message: `the credential broker refused the grant (HTTP ${response.status}${reason === '' ? '' : `, ${reason}`})`,\n };\n }\n let json: unknown;\n try {\n json = JSON.parse(body);\n } catch {\n return { status: null, message: 'the credential broker answered a non-JSON body' };\n }\n const parsed = credentialRefreshResultSchema.safeParse(json);\n if (!parsed.success)\n return { status: null, message: 'the credential broker answered an invalid grant' };\n heldAtMs = deps.now();\n brokerUrl = parsed.data.brokerUrl.replace(/\\/+$/u, '');\n credential = parsed.data.atOpen;\n if (parsed.data.refresh == null) {\n stopped = true;\n deps.log('model credential: the renewed grant has no refresh route');\n } else descriptor = parsed.data.refresh;\n for (const listener of listeners) listener(credential);\n return null;\n };\n\n const schedule = (): void => {\n cancelTimer?.();\n cancelTimer = null;\n if (closed || stopped) return;\n const expiresAtMs = Date.parse(descriptor.expiresAt);\n if (!Number.isFinite(expiresAtMs)) {\n deps.log(`model credential: unparseable grant expiry ${descriptor.expiresAt}`);\n return;\n }\n const dueAtMs =\n failures > 0 ? retryAtMs : heldAtMs + (expiresAtMs - heldAtMs) * REFRESH_FRACTION;\n cancelTimer = deps.setTimer(\n () => {\n cancelTimer = null;\n void pull();\n },\n Math.max(MIN_DELAY_MS, Math.min(MAX_DELAY_MS, dueAtMs - deps.now())),\n );\n };\n\n const pull = (): Promise<void> => {\n if (inFlight != null) return inFlight;\n if (closed || stopped) return Promise.resolve();\n cancelTimer?.();\n cancelTimer = null;\n const pending = (async () => {\n if (deps.now() >= Date.parse(descriptor.expiresAt)) {\n stopped = true;\n deps.log('model credential: the grant expired before it was renewed');\n return;\n }\n const failure = await redeem();\n if (failure == null) {\n failures = 0;\n retryAtMs = 0;\n rejectedHandled = false;\n return;\n }\n if (failure.status === 401 || failure.status === 403) {\n // Revoked, expired or closed: the same token cannot succeed again.\n stopped = true;\n deps.log(`model credential: renewal stopped: ${failure.message}`);\n return;\n }\n failures += 1;\n retryAtMs = deps.now() + Math.min(RETRY_MAX_MS, RETRY_BASE_MS * 2 ** (failures - 1));\n deps.log(`model credential: renewal failed (attempt ${failures}): ${failure.message}`);\n })().finally(() => {\n if (inFlight === pending) inFlight = null;\n schedule();\n });\n inFlight = pending;\n return pending;\n };\n\n const report = async (exhausted: {\n fingerprint: string;\n retryAfterSeconds: number | null;\n }): Promise<void> => {\n try {\n const response = await deps.fetch(`${brokerUrl}/subscription-exhausted`, {\n method: 'POST',\n headers: {\n authorization: `Bearer ${descriptor.token}`,\n 'content-type': 'application/json',\n 'cache-control': 'no-store',\n },\n body: JSON.stringify({ harnessSessionId: descriptor.harnessSessionId, ...exhausted }),\n signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),\n });\n await response.text();\n if (!response.ok)\n deps.log(`model credential: the broker refused the exhaustion (HTTP ${response.status})`);\n } catch (cause) {\n deps.log(\n `model credential: the exhaustion was not reported: ${cause instanceof Error ? cause.message : String(cause)}`,\n );\n }\n };\n\n const exhausted = (exhaustion: {\n fingerprint: string;\n retryAfterSeconds: number | null;\n }): Promise<AtOpenModelCredentialIface | null> => {\n const shared = exhausting.get(exhaustion.fingerprint);\n if (shared != null) return shared;\n // Already renewed onto another credential: nothing to report.\n if (credential?.fingerprint !== exhaustion.fingerprint || closed || stopped)\n return Promise.resolve(credential);\n const pending = (async () => {\n await report(exhaustion);\n // A renewal already in flight may predate the report: renew after it.\n if (inFlight != null) await inFlight;\n await pull();\n const resetMs = (exhaustion.retryAfterSeconds ?? DEFAULT_EXHAUSTION_SECONDS) * 1000;\n const cancel = deps.setTimer(\n () => {\n resetTimers.delete(cancel);\n void pull();\n },\n Math.max(MIN_DELAY_MS, Math.min(MAX_DELAY_MS, resetMs + MIN_DELAY_MS)),\n );\n resetTimers.add(cancel);\n return credential;\n })().finally(() => exhausting.delete(exhaustion.fingerprint));\n exhausting.set(exhaustion.fingerprint, pending);\n return pending;\n };\n\n const opened = await redeem();\n if (opened != null) throw new Error(opened.message);\n schedule();\n\n return {\n current: () => credential,\n subscribe: (listener) => {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n refresh: async () => {\n await pull();\n return credential;\n },\n rejected: () => {\n if (inFlight != null) return inFlight;\n const now = deps.now();\n if (rejectedHandled || now < retryAtMs || now - lastRejectedMs < REJECTED_MIN_INTERVAL_MS)\n return Promise.resolve();\n rejectedHandled = true;\n lastRejectedMs = now;\n return pull();\n },\n exhausted,\n close: () => {\n closed = true;\n cancelTimer?.();\n cancelTimer = null;\n for (const cancel of resetTimers) cancel();\n resetTimers.clear();\n listeners.clear();\n },\n };\n};\n\n// The auth headers an upstream takes a key-based credential with: a direct Anthropic API\n// key as `x-api-key`, a Claude subscription's token and every other key as a bearer. Null\n// for a credential the harness cannot send as a header (a ChatGPT login, the harness's own).\nexport const modelCredentialHeaders = (\n credential: AtOpenModelCredentialIface,\n): Record<string, string> | null => {\n if (credential.subscription != null) {\n return credential.subscription.format === 'claude-setup-token'\n ? { authorization: `Bearer ${credential.subscription.value}` }\n : null;\n }\n if (credential.apiKey == null) return null;\n if (credential.kind === InferenceSourceKind.ApiKey && credential.vendor === 'anthropic')\n return { 'x-api-key': credential.apiKey };\n return { authorization: `Bearer ${credential.apiKey}` };\n};\n\nexport interface ModelUpstreamIface {\n // Base URL the harness's request paths are appended to.\n baseUrl: string;\n // Exactly the auth headers the upstream takes.\n headers: Record<string, string>;\n}\n\n// A loopback HTTP proxy between a harness and its model API. The harness reads its key and\n// base URL once at start; it is given the proxy's URL and a local key instead, and the proxy\n// sends each request with the current upstream credential, so a renewal applies at once,\n// mid-turn included, and the real credential never sits in the harness's environment.\nexport interface ModelCredentialProxyIface {\n // Base URL to hand the harness instead of the upstream's.\n baseUrl: string;\n // The secret the harness sends where it would send its key.\n localKey: string;\n // Null while there is no credential to send: requests are then refused (503).\n update(upstream: ModelUpstreamIface | null): void;\n close(): Promise<void>;\n}\n\nconst HOP_BY_HOP = new Set([\n 'connection',\n 'keep-alive',\n 'proxy-authenticate',\n 'proxy-authorization',\n 'te',\n 'trailer',\n 'transfer-encoding',\n 'upgrade',\n 'host',\n]);\n\nconst firstHeader = (value: string | string[] | undefined): string | null =>\n [value ?? []].flat()[0] ?? null;\n\nconst presentedKey = (headers: IncomingHttpHeaders): string | null => {\n const apiKey = firstHeader(headers['x-api-key']);\n if (apiKey != null) return apiKey;\n const authorization = firstHeader(headers.authorization);\n const bearer = authorization == null ? null : /^Bearer\\s+(.+)$/iu.exec(authorization);\n return bearer?.[1] ?? null;\n};\n\nconst sameSecret = (presented: string, expected: string): boolean => {\n const left = Buffer.from(presented);\n const right = Buffer.from(expected);\n return left.byteLength === right.byteLength && timingSafeEqual(left, right);\n};\n\nconst upstreamHeaders = (\n headers: IncomingHttpHeaders,\n upstream: ModelUpstreamIface,\n): OutgoingHttpHeaders => {\n const next: OutgoingHttpHeaders = {};\n for (const [name, value] of Object.entries(headers)) {\n if (value == null || HOP_BY_HOP.has(name)) continue;\n if (name === 'x-api-key' || name === 'authorization') continue;\n next[name] = value;\n }\n for (const [name, value] of Object.entries(upstream.headers)) next[name.toLowerCase()] = value;\n return next;\n};\n\nconst fail = (res: ServerResponse, status: number, message: string): void => {\n if (res.headersSent) {\n res.destroy();\n return;\n }\n res.writeHead(status, { 'content-type': 'application/json' });\n res.end(JSON.stringify({ error: { type: 'model_credential_proxy', message } }));\n};\n\nexport const startModelCredentialProxy = async (args: {\n upstream: ModelUpstreamIface | null;\n // The upstream answered 401 to the credential the request carried.\n onUnauthorized?: () => void;\n // The upstream answered 429 (a usage or rate limit) to the credential the request carried:\n // the upstream to send that request to once more, or null to pass the refusal on.\n onLimited?: (limit: {\n sent: ModelUpstreamIface;\n retryAfter: string | null;\n }) => Promise<ModelUpstreamIface | null>;\n}): Promise<ModelCredentialProxyIface> => {\n const localKey = `local-${randomBytes(24).toString('base64url')}`;\n let upstream = args.upstream;\n\n const forward = async (req: IncomingMessage, res: ServerResponse): Promise<void> => {\n const presented = presentedKey(req.headers);\n if (presented == null || !sameSecret(presented, localKey)) {\n fail(res, 401, 'unauthorized');\n return;\n }\n const first = upstream;\n if (first == null) {\n fail(res, 503, 'no model credential');\n return;\n }\n // Held whole, so a limited request can be sent once more on the next credential.\n const chunks: Buffer[] = [];\n try {\n for await (const chunk of req)\n chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));\n } catch {\n res.destroy();\n return;\n }\n const body = Buffer.concat(chunks);\n const url = new URL(req.url ?? '/', 'http://proxy');\n let outgoing: ClientRequest | null = null;\n // A harness that aborts cancels the upstream request.\n res.on('close', () => {\n if (!res.writableFinished) outgoing?.destroy();\n });\n\n const send = (sent: ModelUpstreamIface, resent: boolean): void => {\n const target = new URL(`${sent.baseUrl.replace(/\\/+$/u, '')}${url.pathname}${url.search}`);\n const request = (target.protocol === 'https:' ? httpsRequest : httpRequest)(\n target,\n {\n method: req.method,\n headers: {\n ...upstreamHeaders(req.headers, sent),\n host: target.host,\n 'content-length': String(body.byteLength),\n },\n },\n (response) => {\n void (async () => {\n // Only a refusal of the credential this request carried says anything about it.\n if (response.statusCode === 401 && upstream === sent) args.onUnauthorized?.();\n if (response.statusCode === 429 && !resent && args.onLimited != null) {\n const retryAfter = firstHeader(response.headers['retry-after']);\n const next = await args.onLimited({ sent, retryAfter }).catch(() => null);\n if (next != null && next !== sent && !res.destroyed) {\n response.resume();\n send(next, true);\n return;\n }\n }\n const headers: OutgoingHttpHeaders = {};\n for (const [name, value] of Object.entries(response.headers)) {\n if (value != null && !HOP_BY_HOP.has(name)) headers[name] = value;\n }\n res.writeHead(response.statusCode ?? 502, headers);\n // An upstream that fails mid-body fails the harness's response too.\n pipeline(response, res, (cause) => {\n if (cause == null) return;\n request.destroy();\n res.destroy(cause);\n });\n })();\n },\n );\n outgoing = request;\n request.on('error', (cause) => fail(res, 502, `upstream request failed: ${cause.message}`));\n request.end(body);\n };\n send(first, false);\n };\n\n const server = createServer((req, res) => void forward(req, res));\n // Never keeps the process alive on its own.\n server.unref();\n const port = await new Promise<number>((resolve, reject) => {\n server.once('error', reject);\n server.listen(0, '127.0.0.1', () => {\n const address = server.address();\n if (address == null || typeof address === 'string')\n reject(new Error('the model credential proxy did not bind a TCP port'));\n else resolve(address.port);\n });\n });\n return {\n baseUrl: `http://127.0.0.1:${port}`,\n localKey,\n update: (next) => {\n upstream = next;\n },\n close: () =>\n new Promise<void>((resolve) => {\n server.close(() => resolve());\n server.closeAllConnections();\n }),\n };\n};\n\nexport interface HarnessModelCredentialsIface {\n // The credential the harness runs on and the proxy it reaches it through. Opened on first\n // use, and again after a failure, which rejects with the broker's reason.\n resolve(): Promise<{\n credential: AtOpenModelCredentialIface | null;\n proxy: ModelCredentialProxyIface;\n }>;\n // Renews now (the harness found its credential expired); null before the first resolve.\n refresh(): Promise<AtOpenModelCredentialIface | null>;\n // A credential the harness spends without the proxy (a ChatGPT login) hit its usage or\n // rate limit: ModelCredentialsIface.exhausted. Null before the first resolve.\n exhausted(report: {\n fingerprint: string;\n retryAfterSeconds: number | null;\n }): Promise<AtOpenModelCredentialIface | null>;\n close(): Promise<void>;\n}\n\n// Credentials a vendor's limits exhaust (a subscription, a connected API key): the broker\n// skips one it is told is exhausted until it resets. Platform credits are never reported.\nconst EXHAUSTIBLE_KINDS: ReadonlySet<InferenceSourceKind> = new Set([\n InferenceSourceKind.Subscription,\n InferenceSourceKind.ApiKey,\n]);\n\n// Seconds until a `retry-after` header's moment (delta seconds or an HTTP date); null when\n// absent or unreadable.\nexport const retryAfterSecondsOf = (value: string | null, nowMs: number): number | null => {\n if (value == null) return null;\n const trimmed = value.trim();\n if (/^\\d+$/u.test(trimmed)) return Number(trimmed);\n const at = Date.parse(trimmed);\n return Number.isFinite(at) ? Math.max(0, Math.ceil((at - nowMs) / 1000)) : null;\n};\n\n// A harness process's model credential from its grant (modelCredentialGrantOf), behind one\n// loopback proxy whose upstream `upstreamOf` maps from each renewed credential (null: nothing\n// the proxy can send, such as the harness's own login). A request a vendor refuses with 429 on\n// a subscription or API key is reported (the broker then skips that credential until it\n// resets) and sent once more on the chain's next source, so the work goes on; later requests\n// follow the broker, which tries the exhausted credential first again once it resets.\nexport const harnessModelCredentials = (args: {\n grant: ModelCredentialGrantIface;\n upstreamOf: (credential: AtOpenModelCredentialIface) => ModelUpstreamIface | null;\n log: (line: string) => void;\n deps?: Partial<ModelCredentialsDepsIface>;\n}): HarnessModelCredentialsIface => {\n interface OpenedIface {\n credentials: ModelCredentialsIface;\n proxy: ModelCredentialProxyIface;\n }\n let opening: Promise<OpenedIface> | null = null;\n let closed = false;\n const now = args.deps?.now ?? Date.now;\n // The credential each upstream the proxy was given sends.\n const owners = new WeakMap<ModelUpstreamIface, AtOpenModelCredentialIface>();\n let latest: ModelUpstreamIface | null = null;\n const upstream = (credential: AtOpenModelCredentialIface | null) => {\n const mapped = credential == null ? null : args.upstreamOf(credential);\n if (mapped != null && credential != null) owners.set(mapped, credential);\n latest = mapped;\n return mapped;\n };\n\n const open = (): Promise<OpenedIface> => {\n opening ??= (async () => {\n const credentials = await openModelCredentials({\n grant: args.grant,\n log: args.log,\n deps: args.deps,\n });\n try {\n const proxy = await startModelCredentialProxy({\n upstream: upstream(credentials.current()),\n onUnauthorized: () => void credentials.rejected(),\n onLimited: async ({ sent, retryAfter }) => {\n const refused = owners.get(sent);\n if (refused == null || !EXHAUSTIBLE_KINDS.has(refused.kind)) return null;\n const next = await credentials.exhausted({\n fingerprint: refused.fingerprint,\n retryAfterSeconds: retryAfterSecondsOf(retryAfter, now()),\n });\n if (next == null || next.fingerprint === refused.fingerprint) return null;\n return latest;\n },\n });\n credentials.subscribe((next) => proxy.update(upstream(next)));\n return { credentials, proxy };\n } catch (cause) {\n credentials.close();\n throw cause;\n }\n })().catch((cause: unknown) => {\n opening = null;\n throw cause;\n });\n return opening;\n };\n\n return {\n resolve: async () => {\n if (closed) throw new Error('the model credential is closed');\n const { credentials, proxy } = await open();\n return { credential: credentials.current(), proxy };\n },\n refresh: async () => {\n if (opening == null) return null;\n return (await opening).credentials.refresh();\n },\n exhausted: async (report) => {\n if (opening == null) return null;\n return (await opening).credentials.exhausted(report);\n },\n close: async () => {\n closed = true;\n const current = opening;\n opening = null;\n if (current == null) return;\n const opened = await current.catch(() => null);\n opened?.credentials.close();\n await opened?.proxy.close();\n },\n };\n};\n"],
|
|
5
|
+
"mappings": "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AS,MAAM,QAAQ,MAAM,MAAM,IAAI;AAC7C,cAAQ,YAAY,MAAM;AAC1B,YAAM,QAAQ,MAAM,MAAM;AAAA,IAC5B;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["ModelCredentialEnv", "exhausted"]
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@finalai/agent-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Provider contracts for the agent stack: the agent document, requirements and inference, harnesses, sandboxes (handle, wire, grants, setups, MCP projection), the code host, channels, and the conformance kit.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -49,6 +49,11 @@
|
|
|
49
49
|
"import": "./dist/modules/credential-routes.mjs",
|
|
50
50
|
"default": "./dist/modules/credential-routes.mjs"
|
|
51
51
|
},
|
|
52
|
+
"./model-credentials": {
|
|
53
|
+
"types": "./dist/modules/model-credentials.d.mts",
|
|
54
|
+
"import": "./dist/modules/model-credentials.mjs",
|
|
55
|
+
"default": "./dist/modules/model-credentials.mjs"
|
|
56
|
+
},
|
|
52
57
|
"./channel": {
|
|
53
58
|
"types": "./dist/modules/channel.d.mts",
|
|
54
59
|
"import": "./dist/modules/channel.mjs",
|
|
@@ -237,8 +242,8 @@
|
|
|
237
242
|
},
|
|
238
243
|
"dependencies": {
|
|
239
244
|
"@agentclientprotocol/sdk": "1.8.0",
|
|
240
|
-
"@finalai/acp-session": "0.
|
|
241
|
-
"@finalai/agent-protocol": "0.
|
|
245
|
+
"@finalai/acp-session": "0.6.0",
|
|
246
|
+
"@finalai/agent-protocol": "0.13.0",
|
|
242
247
|
"@modelcontextprotocol/client": "^2.1.0",
|
|
243
248
|
"@modelcontextprotocol/core": "^2.1.0",
|
|
244
249
|
"@modelcontextprotocol/server": "^2.1.0",
|