@mnaimfaizy/agent-kit 1.0.0-beta.14 → 1.0.0-beta.15
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 +22 -4
- package/custom-harness.schema.json +243 -0
- package/harnesses.json +244 -6
- package/package.json +3 -2
- package/src/checks/ci-workflow.mjs +105 -0
- package/src/checks/claude-app.mjs +11 -2
- package/src/checks/custom-harness.mjs +157 -0
- package/src/checks/github-app.mjs +214 -0
- package/src/checks/rulesets.mjs +9 -8
- package/src/checks/secrets-and-variables.mjs +27 -8
- package/src/checks/stages.mjs +705 -0
- package/src/cli.mjs +270 -33
- package/src/deep-scan/agent.mjs +88 -21
- package/src/deep-scan/credential.mjs +81 -32
- package/src/deep-scan/docker-driver.mjs +105 -42
- package/src/deep-scan/harness-text.mjs +99 -0
- package/src/deep-scan/lifecycle.mjs +68 -178
- package/src/deep-scan/limits.mjs +13 -7
- package/src/deep-scan/model-proxy.mjs +21 -10
- package/src/deep-scan/preflight.mjs +19 -6
- package/src/deep-scan/recipe.mjs +4 -0
- package/src/deep-scan/runtime.mjs +54 -6
- package/src/deep-scan/sandbox-run.mjs +232 -0
- package/src/deep-scan/sbx-driver.mjs +224 -39
- package/src/deep-scan/tar.mjs +69 -22
- package/src/deep-scan/verify.mjs +6 -1
- package/src/deep-scan.mjs +73 -13
- package/src/doctor.mjs +113 -4
- package/src/harness/adapters.mjs +30 -0
- package/src/harness/antigravity.mjs +187 -0
- package/src/harness/claude-code.mjs +8 -1
- package/src/harness/codex.mjs +219 -0
- package/src/harness/cursor.mjs +199 -0
- package/src/harness/custom/registry.mjs +21 -0
- package/src/harness/custom/schema.mjs +90 -0
- package/src/harness/custom.mjs +299 -0
- package/src/harness/facts.mjs +44 -7
- package/src/harness/gemini.mjs +164 -0
- package/src/harness/install-layer.mjs +86 -3
- package/src/harness/opencode.mjs +289 -0
- package/src/harness/resolve.mjs +156 -0
- package/src/harness/support-levels.mjs +151 -3
- package/src/harness-live/assess.mjs +29 -6
- package/src/harness-live/cursor-real.mjs +146 -0
- package/src/harness-live/cursor-version.mjs +70 -0
- package/src/harness-live/cursor.mjs +80 -0
- package/src/harness-live/custom.mjs +91 -0
- package/src/harness-live/fixture-harness.mjs +45 -0
- package/src/harness-live/harnesses.mjs +263 -0
- package/src/harness-live/host.mjs +113 -4
- package/src/harness-live/opencode.mjs +289 -0
- package/src/harness-live/probes.mjs +87 -7
- package/src/harness-live/run.mjs +101 -20
- package/src/harness-live/setup.mjs +126 -0
- package/src/harness-live/stage.mjs +25 -15
- package/src/harness-live/suite.mjs +276 -95
- package/src/harness-test.mjs +116 -0
- package/src/init.mjs +313 -13
- package/src/model-proxy/core.mjs +128 -20
- package/src/model-proxy/profiles/anthropic.mjs +54 -10
- package/src/model-proxy/profiles/gemini-api.mjs +311 -0
- package/src/model-proxy/profiles/openai.mjs +327 -0
- package/src/model-proxy/stub-dialects/gemini-api.mjs +178 -0
- package/src/model-proxy/stub-dialects/openai.mjs +173 -0
- package/src/model-proxy/stub.mjs +9 -1
- package/src/stage/audit.mjs +79 -0
- package/src/stage/egress-route.mjs +207 -0
- package/src/stage/implement/carry.mjs +308 -0
- package/src/stage/implement/sandbox.mjs +271 -0
- package/src/stage/implement/verify.mjs +70 -0
- package/src/stage/implement.mjs +218 -0
- package/src/stage/plan.mjs +115 -0
- package/src/stage/review-response.mjs +172 -0
- package/src/stage/review.mjs +185 -0
- package/src/stage/workspace.mjs +274 -0
- package/src/stage.mjs +1068 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// The `gemini-api` provider profile (ADR 0023): everything the model proxy's
|
|
2
|
+
// core (../core.mjs) knows about the Gemini API. The real key goes in as
|
|
3
|
+
// `x-goog-api-key`, and only the request headers Gemini CLI's client sends
|
|
4
|
+
// (Antigravity CLI's client is the same SDK's) pass on: a project header from the sandbox would pick what the real key
|
|
5
|
+
// bills to.
|
|
6
|
+
//
|
|
7
|
+
// What it lets through is only what the sandboxed Gemini CLI or Antigravity
|
|
8
|
+
// CLI needs:
|
|
9
|
+
// - only `POST` to a model's `generateContent`, `streamGenerateContent` and
|
|
10
|
+
// `countTokens`, the model in the path, with no query string but a stream's
|
|
11
|
+
// `alt=sse`. So the API's `key=` parameter, which would carry a key, never
|
|
12
|
+
// goes on;
|
|
13
|
+
// - only a body with nothing that makes the API act outside the sandbox: only
|
|
14
|
+
// the fields the CLI sends, at every level the API would read one, so no
|
|
15
|
+
// server-side tool (Google Search, URL context, code execution) and no file
|
|
16
|
+
// or URL for the API to fetch passes, under either of the two spellings the
|
|
17
|
+
// API takes a field name in; one answer, of text; and a token count of
|
|
18
|
+
// contents alone.
|
|
19
|
+
//
|
|
20
|
+
// The body sent on is the one judged, written out again, with an output limit
|
|
21
|
+
// where a spending request declared none.
|
|
22
|
+
|
|
23
|
+
/** The model API every forwarded request goes to. */
|
|
24
|
+
export const MODEL_API = "https://generativelanguage.googleapis.com";
|
|
25
|
+
|
|
26
|
+
/** A model's method path: the model's id is the one segment before the colon. */
|
|
27
|
+
const method = (name) => new RegExp(`^/v1beta/models/[A-Za-z0-9][A-Za-z0-9._-]*:${name}$`);
|
|
28
|
+
|
|
29
|
+
const ROUTES = Object.freeze([
|
|
30
|
+
// A stream's answer is read as it comes only as server-sent events.
|
|
31
|
+
Object.freeze({
|
|
32
|
+
method: "POST",
|
|
33
|
+
pattern: method("streamGenerateContent"),
|
|
34
|
+
query: "alt=sse",
|
|
35
|
+
spends: true,
|
|
36
|
+
}),
|
|
37
|
+
Object.freeze({ method: "POST", pattern: method("generateContent"), query: "", spends: true }),
|
|
38
|
+
Object.freeze({ method: "POST", pattern: method("countTokens"), query: "", spends: false }),
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/** The largest request body the proxy reads, judges and forwards. */
|
|
42
|
+
export const MAX_REQUEST = 32 * 1024 * 1024;
|
|
43
|
+
|
|
44
|
+
/** The longest line of a stream the meter reads: an event holds a whole tool call. */
|
|
45
|
+
const MAX_STREAM_LINE = 32 * 1024 * 1024;
|
|
46
|
+
|
|
47
|
+
/** As the `anthropic` profile: fewer bytes per token than ordinary text takes. */
|
|
48
|
+
const BYTES_PER_TOKEN = 2;
|
|
49
|
+
|
|
50
|
+
// A request that declares no output limit is given one: written into the body
|
|
51
|
+
// sent on, so the API holds the answer to what the cap reserves for it. The
|
|
52
|
+
// API refuses a limit above the model's own ceiling, so it goes by the model.
|
|
53
|
+
|
|
54
|
+
/** The output ceiling of the Gemini 2.5 models and later. */
|
|
55
|
+
export const OUTPUT_CEILING = 65_536;
|
|
56
|
+
|
|
57
|
+
/** The limit for any other model, which every model that answers with tools takes. */
|
|
58
|
+
export const UNKNOWN_MODEL_OUTPUT = 8_192;
|
|
59
|
+
|
|
60
|
+
const CEILING_MODEL = /\/gemini-(2\.5|[3-9])[A-Za-z0-9._-]*:/;
|
|
61
|
+
|
|
62
|
+
/** The request headers passed on, by name. */
|
|
63
|
+
const FORWARDED_HEADERS = Object.freeze([
|
|
64
|
+
"accept",
|
|
65
|
+
"content-type",
|
|
66
|
+
"user-agent",
|
|
67
|
+
"x-goog-api-client",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
/** The error status and code for each of the proxy's own refusals. */
|
|
71
|
+
const ERRORS = Object.freeze({
|
|
72
|
+
unauthenticated: [401, "UNAUTHENTICATED"],
|
|
73
|
+
cap_reached: [403, "PERMISSION_DENIED"],
|
|
74
|
+
invalid_request: [400, "INVALID_ARGUMENT"],
|
|
75
|
+
too_large: [413, "INVALID_ARGUMENT"],
|
|
76
|
+
busy: [429, "RESOURCE_EXHAUSTED"],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/** The body fields passed on. `cachedContent`, which names what the API holds, is not one. */
|
|
80
|
+
export const BODY_FIELDS = Object.freeze([
|
|
81
|
+
"contents",
|
|
82
|
+
"generationConfig",
|
|
83
|
+
"safetySettings",
|
|
84
|
+
"systemInstruction",
|
|
85
|
+
"toolConfig",
|
|
86
|
+
"tools",
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
/** The generation settings passed on: none that makes an answer cost more than its limit says. */
|
|
90
|
+
export const GENERATION_SETTINGS = Object.freeze([
|
|
91
|
+
"candidateCount",
|
|
92
|
+
"frequencyPenalty",
|
|
93
|
+
"maxOutputTokens",
|
|
94
|
+
"mediaResolution",
|
|
95
|
+
"presencePenalty",
|
|
96
|
+
"responseJsonSchema",
|
|
97
|
+
"responseMimeType",
|
|
98
|
+
"responseModalities",
|
|
99
|
+
"responseSchema",
|
|
100
|
+
"seed",
|
|
101
|
+
"stopSequences",
|
|
102
|
+
"temperature",
|
|
103
|
+
"thinkingConfig",
|
|
104
|
+
"topK",
|
|
105
|
+
"topP",
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
/** The parts passed on: text, bytes the request holds, and the client's own calls. */
|
|
109
|
+
const PART_FIELDS = Object.freeze([
|
|
110
|
+
"functionCall",
|
|
111
|
+
"functionResponse",
|
|
112
|
+
"inlineData",
|
|
113
|
+
"text",
|
|
114
|
+
"thought",
|
|
115
|
+
"thoughtSignature",
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
/** A function response's fields: its `parts` hold content, so they are judged. */
|
|
119
|
+
const RESPONSE_FIELDS = Object.freeze([
|
|
120
|
+
"id",
|
|
121
|
+
"name",
|
|
122
|
+
"parts",
|
|
123
|
+
"response",
|
|
124
|
+
"scheduling",
|
|
125
|
+
"willContinue",
|
|
126
|
+
]);
|
|
127
|
+
|
|
128
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
129
|
+
const only = (value, fields) => Object.keys(value).every((key) => fields.includes(key));
|
|
130
|
+
|
|
131
|
+
/** @param {unknown} tools @returns {string | null} */
|
|
132
|
+
function toolsRefusal(tools) {
|
|
133
|
+
if (!Array.isArray(tools)) return "tools that are not a list";
|
|
134
|
+
for (const tool of tools) {
|
|
135
|
+
if (!isObject(tool)) return "a tool that is not an object";
|
|
136
|
+
// A function is the client's to run. Every other kind of tool, the API runs.
|
|
137
|
+
if (!only(tool, ["functionDeclarations"])) return "a server-side tool";
|
|
138
|
+
const declared = tool.functionDeclarations;
|
|
139
|
+
if (declared !== undefined && !Array.isArray(declared)) return "tools that are not a list";
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** @param {unknown} part @returns {string | null} */
|
|
145
|
+
function partRefusal(part) {
|
|
146
|
+
if (!isObject(part)) return "a part that is not an object";
|
|
147
|
+
if (Object.hasOwn(part, "fileData") || Object.hasOwn(part, "file_data")) {
|
|
148
|
+
return "a file or URL for the model API to fetch";
|
|
149
|
+
}
|
|
150
|
+
if (!only(part, PART_FIELDS)) return "a part the sandbox does not send";
|
|
151
|
+
const response = part.functionResponse;
|
|
152
|
+
if (response === undefined) return null;
|
|
153
|
+
if (!isObject(response) || !only(response, RESPONSE_FIELDS)) {
|
|
154
|
+
return "a part the sandbox does not send";
|
|
155
|
+
}
|
|
156
|
+
return response.parts === undefined ? null : partsRefusal(response.parts);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** @param {unknown} parts @returns {string | null} */
|
|
160
|
+
function partsRefusal(parts) {
|
|
161
|
+
if (!Array.isArray(parts)) return "parts that are not a list";
|
|
162
|
+
for (const part of parts) {
|
|
163
|
+
const why = partRefusal(part);
|
|
164
|
+
if (why) return why;
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @param {unknown} content @returns {string | null} */
|
|
170
|
+
function contentRefusal(content) {
|
|
171
|
+
if (!isObject(content)) return "a content that is not an object";
|
|
172
|
+
if (!only(content, ["parts", "role"])) return "a content field the sandbox does not send";
|
|
173
|
+
return content.parts === undefined ? null : partsRefusal(content.parts);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** @param {unknown} config @returns {string | null} */
|
|
177
|
+
function generationRefusal(config) {
|
|
178
|
+
if (!isObject(config) || !only(config, GENERATION_SETTINGS)) {
|
|
179
|
+
return "a generation setting the sandbox does not send";
|
|
180
|
+
}
|
|
181
|
+
const { candidateCount, maxOutputTokens, responseModalities } = config;
|
|
182
|
+
if (candidateCount !== undefined && candidateCount !== 1) return "more than one answer";
|
|
183
|
+
if (
|
|
184
|
+
responseModalities !== undefined &&
|
|
185
|
+
!(Array.isArray(responseModalities) && responseModalities.every((m) => m === "TEXT"))
|
|
186
|
+
) {
|
|
187
|
+
return "an answer that is not text";
|
|
188
|
+
}
|
|
189
|
+
if (
|
|
190
|
+
maxOutputTokens !== undefined &&
|
|
191
|
+
!(Number.isSafeInteger(maxOutputTokens) && maxOutputTokens > 0)
|
|
192
|
+
) {
|
|
193
|
+
return "an output limit that is not a positive whole number";
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Why a request body, a JSON object, is refused on `route`, or null.
|
|
200
|
+
* @param {Record<string, unknown>} body
|
|
201
|
+
* @param {{ route: { spends: boolean } }} on
|
|
202
|
+
* @returns {string | null} a short, fixed reason, never the sandbox's text
|
|
203
|
+
*/
|
|
204
|
+
export function requestRefusal(body, { route }) {
|
|
205
|
+
if (!only(body, route.spends ? BODY_FIELDS : ["contents"])) {
|
|
206
|
+
return "a request field the sandbox does not send";
|
|
207
|
+
}
|
|
208
|
+
const { contents, systemInstruction, tools, toolConfig, generationConfig } = body;
|
|
209
|
+
if (contents !== undefined) {
|
|
210
|
+
if (!Array.isArray(contents)) return "contents that are not a list";
|
|
211
|
+
for (const content of contents) {
|
|
212
|
+
const why = contentRefusal(content);
|
|
213
|
+
if (why) return why;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (systemInstruction !== undefined) {
|
|
217
|
+
const why = contentRefusal(systemInstruction);
|
|
218
|
+
if (why) return why;
|
|
219
|
+
}
|
|
220
|
+
if (tools !== undefined) {
|
|
221
|
+
const why = toolsRefusal(tools);
|
|
222
|
+
if (why) return why;
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
toolConfig !== undefined &&
|
|
226
|
+
!(isObject(toolConfig) && only(toolConfig, ["functionCallingConfig"]))
|
|
227
|
+
) {
|
|
228
|
+
return "a tool setting the sandbox does not send";
|
|
229
|
+
}
|
|
230
|
+
return generationConfig === undefined ? null : generationRefusal(generationConfig);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** A spending request's body, with an output limit where it declared none. */
|
|
234
|
+
function withLimit(body, { path, route }) {
|
|
235
|
+
if (!route.spends || body.generationConfig?.maxOutputTokens !== undefined) return body;
|
|
236
|
+
const maxOutputTokens = CEILING_MODEL.test(path) ? OUTPUT_CEILING : UNKNOWN_MODEL_OUTPUT;
|
|
237
|
+
return { ...body, generationConfig: { ...body.generationConfig, maxOutputTokens } };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The `usageMetadata` of one answer, each token counted once: its total, which
|
|
242
|
+
* holds the prompt (the cached tokens among it), the tool-use prompt, the
|
|
243
|
+
* thoughts and the answer; else those four parts. A stream's events repeat a
|
|
244
|
+
* running total, so only its highest value counts.
|
|
245
|
+
* @returns {(answer: any) => number} the tokens the body or event newly reports
|
|
246
|
+
*/
|
|
247
|
+
function usageReducer() {
|
|
248
|
+
let seen = 0;
|
|
249
|
+
const count = (n) => (typeof n === "number" && Number.isFinite(n) && n > 0 ? n : 0);
|
|
250
|
+
return (answer) => {
|
|
251
|
+
const usage = answer?.usageMetadata;
|
|
252
|
+
if (!usage || typeof usage !== "object") return 0;
|
|
253
|
+
const total =
|
|
254
|
+
count(usage.totalTokenCount) ||
|
|
255
|
+
count(usage.promptTokenCount) +
|
|
256
|
+
count(usage.toolUsePromptTokenCount) +
|
|
257
|
+
count(usage.thoughtsTokenCount) +
|
|
258
|
+
count(usage.candidatesTokenCount);
|
|
259
|
+
if (total <= seen) return 0;
|
|
260
|
+
const added = total - seen;
|
|
261
|
+
seen = total;
|
|
262
|
+
return added;
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Whether an answer says the API does not accept the key, beside a 401 or 403:
|
|
268
|
+
* a mistyped or expired one is answered `400`, its reason `API_KEY_INVALID`.
|
|
269
|
+
* @param {number} status @param {any} answer one error, or a list of them
|
|
270
|
+
*/
|
|
271
|
+
function refusesCredential(status, answer) {
|
|
272
|
+
if (status !== 400) return false;
|
|
273
|
+
return [answer]
|
|
274
|
+
.flat()
|
|
275
|
+
.some(
|
|
276
|
+
(one) =>
|
|
277
|
+
Array.isArray(one?.error?.details) &&
|
|
278
|
+
one.error.details.some((detail) => detail?.reason === "API_KEY_INVALID"),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** @type {Readonly<import("../core.mjs").ProviderProfile>} */
|
|
283
|
+
export const geminiApi = Object.freeze({
|
|
284
|
+
upstream: MODEL_API,
|
|
285
|
+
credentialKinds: Object.freeze({
|
|
286
|
+
"api-key": (credential) => ["x-goog-api-key", credential],
|
|
287
|
+
}),
|
|
288
|
+
keyHeaders: Object.freeze(["x-goog-api-key"]),
|
|
289
|
+
forwardsHeader: (header) => FORWARDED_HEADERS.includes(header),
|
|
290
|
+
inScope: (path, decoded) => path === decoded && decoded.startsWith("/v1beta/"),
|
|
291
|
+
routes: ROUTES,
|
|
292
|
+
rewrite: withLimit,
|
|
293
|
+
refusals: Object.freeze({
|
|
294
|
+
scope: "only the model API's /v1beta/ paths",
|
|
295
|
+
route:
|
|
296
|
+
"only POST to a model's generateContent, streamGenerateContent (with alt=sse) or countTokens, with no other query string",
|
|
297
|
+
}),
|
|
298
|
+
maxRequestBytes: MAX_REQUEST,
|
|
299
|
+
maxStreamLine: MAX_STREAM_LINE,
|
|
300
|
+
requestRefusal,
|
|
301
|
+
refusesCredential,
|
|
302
|
+
inputEstimate: (body) => Math.ceil(body.length / BYTES_PER_TOKEN),
|
|
303
|
+
declaredLimit: (body) => body.generationConfig?.maxOutputTokens,
|
|
304
|
+
errorBody: (kind, message) => {
|
|
305
|
+
const [code, status] = ERRORS[kind];
|
|
306
|
+
return { error: { code, message, status } };
|
|
307
|
+
},
|
|
308
|
+
streamHint: /"(usageMetadata|modelVersion)"/,
|
|
309
|
+
answeringModel: (answer) => answer?.modelVersion,
|
|
310
|
+
usageReducer,
|
|
311
|
+
});
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// The `openai` provider profile (ADR 0023): everything the model proxy's core
|
|
2
|
+
// (../core.mjs) knows about the OpenAI API. The real key goes in as the bearer
|
|
3
|
+
// value of `Authorization`, and only the request headers Codex CLI sends pass
|
|
4
|
+
// on: an organization or project header from the sandbox would pick what the
|
|
5
|
+
// real key bills to.
|
|
6
|
+
//
|
|
7
|
+
// What it lets through is only what the sandboxed Codex CLI needs:
|
|
8
|
+
// - only `POST /v1/responses`, with no query string;
|
|
9
|
+
// - only a body with nothing that makes the API act outside the sandbox: only
|
|
10
|
+
// the fields Codex CLI sends; `store` set to false, since the API keeps a
|
|
11
|
+
// request that says nothing; no premium service tier; every declared tool a
|
|
12
|
+
// client tool (a function, a custom tool, a namespace of those, or a tool
|
|
13
|
+
// search the client runs), wherever it is declared; only the input items
|
|
14
|
+
// Codex CLI sends, none naming something the API holds or ran; and no
|
|
15
|
+
// content that names a file or URL for the API to fetch.
|
|
16
|
+
//
|
|
17
|
+
// The body sent on is the one judged, written out again, with an output limit
|
|
18
|
+
// where the request declared none.
|
|
19
|
+
|
|
20
|
+
/** The model API every forwarded request goes to. */
|
|
21
|
+
export const MODEL_API = "https://api.openai.com";
|
|
22
|
+
|
|
23
|
+
/** The Responses API's own path, which alone spends tokens, and alone is forwarded. */
|
|
24
|
+
export const RESPONSES_PATH = "/v1/responses";
|
|
25
|
+
|
|
26
|
+
const ROUTES = Object.freeze([
|
|
27
|
+
Object.freeze({ method: "POST", path: RESPONSES_PATH, spends: true }),
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
/** The largest request body the proxy reads, judges and forwards. */
|
|
31
|
+
export const MAX_REQUEST = 32 * 1024 * 1024;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The longest line of a stream the meter reads. A stream's closing event holds
|
|
35
|
+
* the whole answer beside its usage, so a line far over the core's default is
|
|
36
|
+
* ordinary, and one left unread would leave its tokens uncounted.
|
|
37
|
+
*/
|
|
38
|
+
const MAX_STREAM_LINE = 32 * 1024 * 1024;
|
|
39
|
+
|
|
40
|
+
/** As the `anthropic` profile: fewer bytes per token than ordinary text takes. */
|
|
41
|
+
const BYTES_PER_TOKEN = 2;
|
|
42
|
+
|
|
43
|
+
// A request that declares no output limit, as Codex CLI's declare none, is
|
|
44
|
+
// given one: written into the body sent on, so the API holds the answer to what
|
|
45
|
+
// the cap reserves for it. The API refuses a limit above the model's own
|
|
46
|
+
// ceiling, so it goes by the model.
|
|
47
|
+
|
|
48
|
+
/** The output ceiling of the models in CEILING_MODELS. */
|
|
49
|
+
export const OUTPUT_CEILING = 128_000;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The limit for any other model: low enough for every model that serves the
|
|
53
|
+
* Responses API with tools, so a model the profile does not know still runs,
|
|
54
|
+
* with shorter answers.
|
|
55
|
+
*/
|
|
56
|
+
export const UNKNOWN_MODEL_OUTPUT = 16_384;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The models known to take OUTPUT_CEILING: those the pinned Codex CLI bundles
|
|
60
|
+
* (`codex debug models --bundled`). A pin bump updates the list.
|
|
61
|
+
*/
|
|
62
|
+
export const CEILING_MODELS = Object.freeze([
|
|
63
|
+
"codex-auto-review",
|
|
64
|
+
"gpt-5.5",
|
|
65
|
+
"gpt-5.6-luna",
|
|
66
|
+
"gpt-5.6-sol",
|
|
67
|
+
"gpt-5.6-terra",
|
|
68
|
+
"gpt-6-astra",
|
|
69
|
+
"gpt-6-luna",
|
|
70
|
+
"gpt-6-sol",
|
|
71
|
+
"gpt-6.1-sol",
|
|
72
|
+
"gpt-6.1-terra",
|
|
73
|
+
"gpt-6.2-astra",
|
|
74
|
+
"gpt-6.2-sol",
|
|
75
|
+
"gpt-daybreak-blue-latest",
|
|
76
|
+
"gpt-daybreak-red-latest",
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
/** A model id's date, as a snapshot of a model is named. */
|
|
80
|
+
const SNAPSHOT = /-\d{4}-\d{2}-\d{2}$/;
|
|
81
|
+
|
|
82
|
+
/** The output limit for a request naming `model` and declaring none. */
|
|
83
|
+
const outputLimit = (model) =>
|
|
84
|
+
typeof model === "string" && CEILING_MODELS.includes(model.replace(SNAPSHOT, ""))
|
|
85
|
+
? OUTPUT_CEILING
|
|
86
|
+
: UNKNOWN_MODEL_OUTPUT;
|
|
87
|
+
|
|
88
|
+
/** How deep the body's input is walked; deeper is refused. */
|
|
89
|
+
const MAX_DEPTH = 64;
|
|
90
|
+
|
|
91
|
+
/** The request headers passed on, by name, beside those that start with one of the prefixes. */
|
|
92
|
+
const FORWARDED_HEADERS = Object.freeze([
|
|
93
|
+
"accept",
|
|
94
|
+
"content-type",
|
|
95
|
+
"originator",
|
|
96
|
+
"session-id",
|
|
97
|
+
"thread-id",
|
|
98
|
+
"user-agent",
|
|
99
|
+
"x-client-request-id",
|
|
100
|
+
]);
|
|
101
|
+
const FORWARDED_PREFIXES = Object.freeze([
|
|
102
|
+
"x-codex-",
|
|
103
|
+
"x-openai-internal-codex-",
|
|
104
|
+
"x-openai-subagent",
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
/** The error type for each of the proxy's own refusals. */
|
|
108
|
+
const ERROR_TYPES = Object.freeze({
|
|
109
|
+
unauthenticated: "authentication_error",
|
|
110
|
+
cap_reached: "permission_error",
|
|
111
|
+
invalid_request: "invalid_request_error",
|
|
112
|
+
too_large: "invalid_request_error",
|
|
113
|
+
busy: "rate_limit_error",
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The body fields passed on: those Codex CLI sends. Any other is refused, so a
|
|
118
|
+
* field that names something the API holds (`prompt`, `conversation`,
|
|
119
|
+
* `previous_response_id`), or one the API adds later, never passes unjudged.
|
|
120
|
+
*/
|
|
121
|
+
export const BODY_FIELDS = Object.freeze([
|
|
122
|
+
"client_metadata",
|
|
123
|
+
"include",
|
|
124
|
+
"input",
|
|
125
|
+
"instructions",
|
|
126
|
+
"max_output_tokens",
|
|
127
|
+
"model",
|
|
128
|
+
"parallel_tool_calls",
|
|
129
|
+
"prompt_cache_key",
|
|
130
|
+
"reasoning",
|
|
131
|
+
"service_tier",
|
|
132
|
+
"store",
|
|
133
|
+
"stream",
|
|
134
|
+
"stream_options",
|
|
135
|
+
"text",
|
|
136
|
+
"tool_choice",
|
|
137
|
+
"tools",
|
|
138
|
+
]);
|
|
139
|
+
|
|
140
|
+
/** The service tiers that bill a token at its ordinary price. */
|
|
141
|
+
const SERVICE_TIERS = Object.freeze(["auto", "default"]);
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The input items passed on: those Codex CLI sends, each its own or a call a
|
|
145
|
+
* client ran. An item with no `type` is passed only as a message.
|
|
146
|
+
*/
|
|
147
|
+
export const INPUT_ITEMS = Object.freeze([
|
|
148
|
+
"additional_tools",
|
|
149
|
+
"agent_message",
|
|
150
|
+
"compaction",
|
|
151
|
+
"compaction_trigger",
|
|
152
|
+
"configuration_update",
|
|
153
|
+
"context_compaction",
|
|
154
|
+
"custom_tool_call",
|
|
155
|
+
"custom_tool_call_output",
|
|
156
|
+
"function_call",
|
|
157
|
+
"function_call_output",
|
|
158
|
+
"local_shell_call",
|
|
159
|
+
"message",
|
|
160
|
+
"reasoning",
|
|
161
|
+
"tool_search_call",
|
|
162
|
+
"tool_search_output",
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
/** Fields that name content for the API to fetch: any value but `data:` is a fetch. */
|
|
166
|
+
const FETCHED_URL = Object.freeze(["image_url", "audio_url", "file_url"]);
|
|
167
|
+
|
|
168
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Why a list of declared tools is refused, or null: each must be one the
|
|
172
|
+
* client runs.
|
|
173
|
+
* @param {unknown} tools @param {number} depth @returns {string | null}
|
|
174
|
+
*/
|
|
175
|
+
function toolsRefusal(tools, depth) {
|
|
176
|
+
if (!Array.isArray(tools)) return "tools that are not a list";
|
|
177
|
+
if (depth > MAX_DEPTH) return "content nested too deeply";
|
|
178
|
+
for (const tool of tools) {
|
|
179
|
+
if (!isObject(tool)) return "a tool that is not an object";
|
|
180
|
+
if (tool.type === "function" || tool.type === "custom") continue;
|
|
181
|
+
if (tool.type === "tool_search" && tool.execution === "client") continue;
|
|
182
|
+
if (tool.type !== "namespace") return "a server-side tool";
|
|
183
|
+
const why = toolsRefusal(tool.tools, depth + 1);
|
|
184
|
+
if (why) return why;
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** @param {unknown} value @param {number} depth @returns {string | null} */
|
|
190
|
+
function contentRefusal(value, depth) {
|
|
191
|
+
if (value === null || typeof value !== "object") return null;
|
|
192
|
+
if (depth > MAX_DEPTH) return "content nested too deeply";
|
|
193
|
+
if (Array.isArray(value)) {
|
|
194
|
+
for (const item of value) {
|
|
195
|
+
const why = contentRefusal(item, depth + 1);
|
|
196
|
+
if (why) return why;
|
|
197
|
+
}
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
if (value.file_id !== undefined && value.file_id !== null) return "a file reference";
|
|
201
|
+
for (const key of FETCHED_URL) {
|
|
202
|
+
const url = value[key];
|
|
203
|
+
if (url === undefined || url === null) continue;
|
|
204
|
+
if (typeof url !== "string" || !url.startsWith("data:")) return "a URL to fetch";
|
|
205
|
+
}
|
|
206
|
+
for (const [key, child] of Object.entries(value)) {
|
|
207
|
+
// A declaration of tools is judged as one: a tool's schema may well name a
|
|
208
|
+
// property `file_id`.
|
|
209
|
+
const why = key === "tools" ? toolsRefusal(child, depth + 1) : contentRefusal(child, depth + 1);
|
|
210
|
+
if (why) return why;
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Why the body's input is refused, or null: a string, or a list of the items
|
|
217
|
+
* passed on, with nothing in them to fetch.
|
|
218
|
+
* @param {unknown} input @returns {string | null}
|
|
219
|
+
*/
|
|
220
|
+
function inputRefusal(input) {
|
|
221
|
+
if (input === undefined || typeof input === "string") return null;
|
|
222
|
+
if (!Array.isArray(input)) return "an input that is neither text nor a list";
|
|
223
|
+
for (const item of input) {
|
|
224
|
+
if (!isObject(item)) return "an input item that is not an object";
|
|
225
|
+
const message = item.type === undefined && "role" in item && "content" in item;
|
|
226
|
+
if (!message && !INPUT_ITEMS.includes(item.type)) {
|
|
227
|
+
return "an input item the sandbox does not send";
|
|
228
|
+
}
|
|
229
|
+
if (item.type?.startsWith("tool_search_") && (item.execution ?? "client") !== "client") {
|
|
230
|
+
return "a tool search the model API runs";
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return contentRefusal(input, 0);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Why a Responses API request body, a JSON object, is refused, or null.
|
|
238
|
+
* @param {Record<string, unknown>} body
|
|
239
|
+
* @returns {string | null} a short, fixed reason, never the sandbox's text
|
|
240
|
+
*/
|
|
241
|
+
export function requestRefusal(body) {
|
|
242
|
+
if (!Object.keys(body).every((key) => BODY_FIELDS.includes(key))) {
|
|
243
|
+
return "a request field the sandbox does not send";
|
|
244
|
+
}
|
|
245
|
+
if (body.store !== false) return "a request the model API would keep (store is not false)";
|
|
246
|
+
const tier = body.service_tier;
|
|
247
|
+
if (tier !== undefined && tier !== null && !SERVICE_TIERS.includes(tier)) {
|
|
248
|
+
return "a premium service tier";
|
|
249
|
+
}
|
|
250
|
+
const limit = body.max_output_tokens;
|
|
251
|
+
if (limit !== undefined && limit !== null && !(Number.isSafeInteger(limit) && limit > 0)) {
|
|
252
|
+
return "an output limit that is not a positive whole number";
|
|
253
|
+
}
|
|
254
|
+
const instructions = body.instructions;
|
|
255
|
+
if (instructions !== undefined && instructions !== null && typeof instructions !== "string") {
|
|
256
|
+
return "instructions that are not text";
|
|
257
|
+
}
|
|
258
|
+
if (Object.hasOwn(body, "tools")) {
|
|
259
|
+
const why = toolsRefusal(body.tools, 0);
|
|
260
|
+
if (why) return why;
|
|
261
|
+
}
|
|
262
|
+
// An object here would name one tool, a server-side one among them.
|
|
263
|
+
if (body.tool_choice !== undefined && typeof body.tool_choice !== "string") {
|
|
264
|
+
return "a tool choice that names a tool";
|
|
265
|
+
}
|
|
266
|
+
return inputRefusal(body.input);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The `usage` of one answer, each token counted once: its input and its output
|
|
271
|
+
* tokens, from a JSON body or from a stream's closing event. The cached and
|
|
272
|
+
* the reasoning tokens are parts of those two, and so is the total. A figure
|
|
273
|
+
* repeated counts its highest value once.
|
|
274
|
+
* @returns {(answer: any) => number} the tokens the body or event newly reports
|
|
275
|
+
*/
|
|
276
|
+
function usageReducer() {
|
|
277
|
+
const seen = { input_tokens: 0, output_tokens: 0 };
|
|
278
|
+
return (answer) => {
|
|
279
|
+
const usage = answer?.usage ?? answer?.response?.usage;
|
|
280
|
+
if (!usage || typeof usage !== "object") return 0;
|
|
281
|
+
let added = 0;
|
|
282
|
+
for (const key of Object.keys(seen)) {
|
|
283
|
+
const n = usage[key];
|
|
284
|
+
if (typeof n === "number" && Number.isFinite(n) && n > seen[key]) {
|
|
285
|
+
added += n - seen[key];
|
|
286
|
+
seen[key] = n;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return added;
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** @type {Readonly<import("../core.mjs").ProviderProfile>} */
|
|
294
|
+
export const openai = Object.freeze({
|
|
295
|
+
upstream: MODEL_API,
|
|
296
|
+
credentialKinds: Object.freeze({
|
|
297
|
+
"api-key": (credential) => ["authorization", `Bearer ${credential}`],
|
|
298
|
+
}),
|
|
299
|
+
// The sandbox carries its caller key as a bearer value, which the core reads.
|
|
300
|
+
keyHeaders: Object.freeze([]),
|
|
301
|
+
forwardsHeader: (header) =>
|
|
302
|
+
FORWARDED_HEADERS.includes(header) ||
|
|
303
|
+
FORWARDED_PREFIXES.some((prefix) => header.startsWith(prefix)),
|
|
304
|
+
inScope: (path, decoded) => path === decoded && decoded.startsWith("/v1/"),
|
|
305
|
+
routes: ROUTES,
|
|
306
|
+
refusesQuery: true,
|
|
307
|
+
rewrite: (body) => ({
|
|
308
|
+
...body,
|
|
309
|
+
max_output_tokens: body.max_output_tokens ?? outputLimit(body.model),
|
|
310
|
+
}),
|
|
311
|
+
refusals: Object.freeze({
|
|
312
|
+
scope: "only the model API's /v1/ paths",
|
|
313
|
+
route: "only POST to the Responses API, with no query string",
|
|
314
|
+
}),
|
|
315
|
+
maxRequestBytes: MAX_REQUEST,
|
|
316
|
+
maxStreamLine: MAX_STREAM_LINE,
|
|
317
|
+
requestRefusal,
|
|
318
|
+
inputEstimate: (body) => Math.ceil(body.length / BYTES_PER_TOKEN),
|
|
319
|
+
declaredLimit: (body) => body.max_output_tokens,
|
|
320
|
+
errorBody: (kind, message) => ({
|
|
321
|
+
error: { message, type: ERROR_TYPES[kind], code: kind },
|
|
322
|
+
}),
|
|
323
|
+
streamHint: /"(usage|model)"/,
|
|
324
|
+
// A JSON body names its model, and so does each of a stream's `response` events.
|
|
325
|
+
answeringModel: (answer) => answer?.model ?? answer?.response?.model,
|
|
326
|
+
usageReducer,
|
|
327
|
+
});
|