@jeffusion/bungee-core 6.14.1 → 6.15.1
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/config-storage-worker.js +413 -216
- package/dist/main.js +16093 -13501
- package/dist/plugins/anthropic-request-sanitizer/index.js.map +1 -1
- package/dist/plugins/anthropic-tool-name-transformer/index.js.map +1 -1
- package/dist/plugins/chatgpt-oauth/control.js +13 -5
- package/dist/plugins/chatgpt-oauth/control.js.map +4 -4
- package/dist/plugins/chatgpt-oauth/index.js.map +1 -1
- package/dist/plugins/codex-router/control.js +2 -2
- package/dist/plugins/codex-router/control.js.map +4 -4
- package/dist/plugins/codex-router/index.js +99 -3
- package/dist/plugins/codex-router/index.js.map +8 -7
- package/dist/plugins/codex-router/manifest.json +6 -0
- package/dist/plugins/deepseek-reasoning-fix/index.js.map +1 -1
- package/dist/plugins/key-access/control.js +2 -2
- package/dist/plugins/key-access/control.js.map +4 -4
- package/dist/plugins/key-access/index.js.map +1 -1
- package/dist/plugins/key-access/ingress.js.map +1 -1
- package/dist/plugins/key-rate-limit/control.js +2 -2
- package/dist/plugins/key-rate-limit/control.js.map +4 -4
- package/dist/plugins/key-rate-limit/index.js +19 -7
- package/dist/plugins/key-rate-limit/index.js.map +4 -4
- package/dist/plugins/key-rate-limit/ingress.js +19 -7
- package/dist/plugins/key-rate-limit/ingress.js.map +3 -3
- package/dist/plugins/llm-protocol-adapter/control.js +113 -20
- package/dist/plugins/llm-protocol-adapter/control.js.map +9 -9
- package/dist/plugins/llm-protocol-adapter/index.js +100 -16
- package/dist/plugins/llm-protocol-adapter/index.js.map +8 -8
- package/dist/plugins/llm-tps/control.js +2 -2
- package/dist/plugins/llm-tps/control.js.map +3 -3
- package/dist/plugins/llm-tps/index.js.map +1 -1
- package/dist/plugins/model-mapping/control.js +2 -2
- package/dist/plugins/model-mapping/control.js.map +3 -3
- package/dist/plugins/model-mapping/index.js.map +1 -1
- package/dist/plugins/models-dev/control.js +13 -7
- package/dist/plugins/models-dev/control.js.map +3 -3
- package/dist/plugins/models-dev/index.js.map +1 -1
- package/dist/plugins/request-logs/index.js.map +1 -1
- package/dist/plugins/signature-repair/index.js.map +1 -1
- package/dist/plugins/token-budget/control.js +58 -47
- package/dist/plugins/token-budget/control.js.map +5 -5
- package/dist/plugins/token-budget/index.js.map +1 -1
- package/dist/plugins/token-metering/index.js.map +1 -1
- package/dist/plugins/token-stats/control.js +2 -2
- package/dist/plugins/token-stats/control.js.map +4 -4
- package/dist/plugins/token-stats/index.js.map +1 -1
- package/dist/plugins/user-auth/control.js +16 -15
- package/dist/plugins/user-auth/control.js.map +4 -4
- package/dist/plugins/user-auth/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -15,6 +15,16 @@ function refill(state, now) {
|
|
|
15
15
|
const at = Math.max(now, prior.at);
|
|
16
16
|
return { ...prior, at, tokens: Math.min(prior.burst, prior.tokens + (at - prior.at) / 1000 * prior.rps) };
|
|
17
17
|
}
|
|
18
|
+
function applyRate(bucket, policy) {
|
|
19
|
+
return { at: bucket.at, tokens: Math.min(bucket.tokens, policy.burst), rps: policy.rps, burst: policy.burst };
|
|
20
|
+
}
|
|
21
|
+
function effectiveBucket(state, activatedAt, now) {
|
|
22
|
+
const pending = state.pending;
|
|
23
|
+
if (!pending)
|
|
24
|
+
return refill(state, now);
|
|
25
|
+
const before = refill(state, Math.min(activatedAt, now));
|
|
26
|
+
return refill(applyRate(before, pending), now);
|
|
27
|
+
}
|
|
18
28
|
function createIngress() {
|
|
19
29
|
return {
|
|
20
30
|
bodyRequirements() {
|
|
@@ -27,14 +37,16 @@ function createIngress() {
|
|
|
27
37
|
},
|
|
28
38
|
expiresAt(state) {
|
|
29
39
|
const b = state;
|
|
30
|
-
|
|
40
|
+
const current = b.at + (b.burst - b.tokens) / b.rps * 1000;
|
|
41
|
+
const pending = b.pending ? b.pending.burst / b.pending.rps * 1000 : 0;
|
|
42
|
+
return Math.min(Number.MAX_VALUE, Math.ceil(current + pending));
|
|
31
43
|
},
|
|
32
|
-
reconcile(state, value, now) {
|
|
33
|
-
const b =
|
|
34
|
-
return p ? { ...b,
|
|
44
|
+
reconcile(state, value, now, transition) {
|
|
45
|
+
const b = effectiveBucket(state, transition?.previousActivation ?? now, now), p = validatePolicy(value);
|
|
46
|
+
return p ? transition?.deferUntilActivation ? { ...b, pending: p } : applyRate(b, p) : b;
|
|
35
47
|
}
|
|
36
48
|
},
|
|
37
|
-
plan(target, value, state) {
|
|
49
|
+
plan(target, value, state, activatedAt) {
|
|
38
50
|
if (target.principal.domain === "anonymous")
|
|
39
51
|
return { snapshot: null };
|
|
40
52
|
try {
|
|
@@ -43,7 +55,7 @@ function createIngress() {
|
|
|
43
55
|
return { snapshot: null };
|
|
44
56
|
if (!Number.isFinite(target.now))
|
|
45
57
|
throw new Error("invalid_time");
|
|
46
|
-
const bucket = state === null ? { tokens: p.burst, at: target.now, rps: p.rps, burst: p.burst } :
|
|
58
|
+
const bucket = state === null ? { tokens: p.burst, at: target.now, rps: p.rps, burst: p.burst } : effectiveBucket(state, activatedAt ?? target.now, target.now);
|
|
47
59
|
if (bucket.tokens < 1)
|
|
48
60
|
return { denial: { error: "key-rate-limit.exhausted", status: 429, retryAfter: Math.max(1, Math.ceil((1 - bucket.tokens) / p.rps)) } };
|
|
49
61
|
return { state: { ...bucket, tokens: bucket.tokens - 1 }, snapshot: p };
|
|
@@ -61,4 +73,4 @@ export {
|
|
|
61
73
|
validatePolicy
|
|
62
74
|
};
|
|
63
75
|
|
|
64
|
-
//# debugId=
|
|
76
|
+
//# debugId=ACEA5589D4401B4264756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../plugins/key-rate-limit/server/policy.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { IngressPlugin } from '@jeffusion/bungee-core/plugin';\nimport type { DurableJson } from '@jeffusion/bungee-core/plugin';\nexport interface RatePolicy { rps: number; burst: number; unit?: 'second'|'minute' }\ninterface Bucket { tokens: number; at: number; rps: number; burst: number }\nexport function validatePolicy(value: unknown): RatePolicy | null {\n if (value === null) return null;\n const p = value as RatePolicy;\n if (!p || !Number.isFinite(p.rps) || p.rps <= 0 || !Number.isSafeInteger(p.burst) || p.burst < 1\n || !Number.isFinite(p.burst / p.rps * 1000)\n || (p.unit !== undefined && p.unit !== 'second' && p.unit !== 'minute')\n || (p.unit === 'minute' && !Number.isFinite(p.rps * 60))) throw new Error('invalid_policy');\n return {rps:p.rps,burst:p.burst,...(p.unit ? {unit:p.unit} : {})};\n}\nfunction refill(state: DurableJson, now: number): Bucket {\n const prior = state as unknown as Bucket;\n if (!prior || !Number.isFinite(prior.at) || !Number.isFinite(prior.tokens) || prior.tokens < 0\n || prior.tokens > prior.burst || !validatePolicy({rps:prior.rps,burst:prior.burst})) throw new Error('invalid_state');\n const at = Math.max(now,prior.at);\n return {...prior,at,tokens:Math.min(prior.burst,prior.tokens+(at-prior.at)/1000*prior.rps)};\n}\nexport function createIngress(): IngressPlugin { return {\n bodyRequirements() { return { request: 'none' }; },\n keyedState: {\n capacity: 10000,\n policyForKey(value,key) { return (value as any)?.byKey?.[key] ?? null; },\n expiresAt(state) {\n const b = state as unknown as Bucket;\n // Round upwards: deleting even slightly early must not create free capacity.\n
|
|
5
|
+
"import type { IngressPlugin } from '@jeffusion/bungee-core/plugin';\nimport type { DurableJson } from '@jeffusion/bungee-core/plugin';\nexport interface RatePolicy { rps: number; burst: number; unit?: 'second'|'minute' }\ninterface Bucket { tokens: number; at: number; rps: number; burst: number; pending?: RatePolicy }\nexport function validatePolicy(value: unknown): RatePolicy | null {\n if (value === null) return null;\n const p = value as RatePolicy;\n if (!p || !Number.isFinite(p.rps) || p.rps <= 0 || !Number.isSafeInteger(p.burst) || p.burst < 1\n || !Number.isFinite(p.burst / p.rps * 1000)\n || (p.unit !== undefined && p.unit !== 'second' && p.unit !== 'minute')\n || (p.unit === 'minute' && !Number.isFinite(p.rps * 60))) throw new Error('invalid_policy');\n return {rps:p.rps,burst:p.burst,...(p.unit ? {unit:p.unit} : {})};\n}\nfunction refill(state: DurableJson, now: number): Bucket {\n const prior = state as unknown as Bucket;\n if (!prior || !Number.isFinite(prior.at) || !Number.isFinite(prior.tokens) || prior.tokens < 0\n || prior.tokens > prior.burst || !validatePolicy({rps:prior.rps,burst:prior.burst})) throw new Error('invalid_state');\n const at = Math.max(now,prior.at);\n return {...prior,at,tokens:Math.min(prior.burst,prior.tokens+(at-prior.at)/1000*prior.rps)};\n}\nfunction applyRate(bucket: Bucket, policy: RatePolicy): Bucket {\n return { at: bucket.at, tokens: Math.min(bucket.tokens, policy.burst), rps: policy.rps, burst: policy.burst };\n}\nfunction effectiveBucket(state: DurableJson, activatedAt: number, now: number): Bucket {\n const pending = (state as unknown as Bucket).pending;\n if (!pending) return refill(state, now);\n const before = refill(state, Math.min(activatedAt, now));\n return refill(applyRate(before, pending) as unknown as DurableJson, now);\n}\nexport function createIngress(): IngressPlugin { return {\n bodyRequirements() { return { request: 'none' }; },\n keyedState: {\n capacity: 10000,\n policyForKey(value,key) { return (value as any)?.byKey?.[key] ?? null; },\n expiresAt(state) {\n const b = state as unknown as Bucket;\n // Round upwards: deleting even slightly early must not create free capacity.\n const current = b.at+(b.burst-b.tokens)/b.rps*1000;\n const pending = b.pending ? b.pending.burst / b.pending.rps * 1000 : 0;\n return Math.min(Number.MAX_VALUE, Math.ceil(current + pending));\n },\n reconcile(state,value,now,transition) {\n const b = effectiveBucket(state, transition?.previousActivation ?? now, now), p = validatePolicy(value);\n // Removed/disabled rules expire naturally at their previous refill rate.\n // Rate changes accrue old-rate credit first, then apply the new rate.\n return (p ? (transition?.deferUntilActivation ? {...b,pending:p} : applyRate(b,p)) : b) as unknown as DurableJson;\n },\n },\n plan(target,value,state,activatedAt) {\n if (target.principal.domain === 'anonymous') return {snapshot:null};\n try {\n const p = validatePolicy(value);\n if (!p) return {snapshot:null};\n if (!Number.isFinite(target.now)) throw new Error('invalid_time');\n const bucket = state === null ? {tokens:p.burst,at:target.now,rps:p.rps,burst:p.burst}\n : effectiveBucket(state, activatedAt ?? target.now, target.now);\n if (bucket.tokens < 1) return {denial:{error:'key-rate-limit.exhausted',status:429,retryAfter:Math.max(1,Math.ceil((1-bucket.tokens)/p.rps))}};\n return {state:{...bucket,tokens:bucket.tokens-1},snapshot:p as unknown as DurableJson};\n } catch { return {denial:{error:'key-rate-limit.unavailable',status:503}}; }\n },\n beforeAttempt() { return null; },\n}; }\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;AAIO,SAAS,cAAc,CAAC,OAAmC;AAAA,EAChE,IAAI,UAAU;AAAA,IAAM,OAAO;AAAA,EAC3B,MAAM,IAAI;AAAA,EACV,IAAI,CAAC,KAAK,CAAC,OAAO,SAAS,EAAE,GAAG,KAAK,EAAE,OAAO,KAAK,CAAC,OAAO,cAAc,EAAE,KAAK,KAAK,EAAE,QAAQ,KAC1F,CAAC,OAAO,SAAS,EAAE,QAAQ,EAAE,MAAM,IAAI,KACtC,EAAE,SAAS,aAAa,EAAE,SAAS,YAAY,EAAE,SAAS,YAC1D,EAAE,SAAS,YAAY,CAAC,OAAO,SAAS,EAAE,MAAM,EAAE;AAAA,IAAI,MAAM,IAAI,MAAM,gBAAgB;AAAA,EAC5F,OAAO,EAAC,KAAI,EAAE,KAAI,OAAM,EAAE,UAAU,EAAE,OAAO,EAAC,MAAK,EAAE,KAAI,IAAI,CAAC,EAAE;AAAA;AAElE,SAAS,MAAM,CAAC,OAAoB,KAAqB;AAAA,EACvD,MAAM,QAAQ;AAAA,EACd,IAAI,CAAC,SAAS,CAAC,OAAO,SAAS,MAAM,EAAE,KAAK,CAAC,OAAO,SAAS,MAAM,MAAM,KAAK,MAAM,SAAS,KACxF,MAAM,SAAS,MAAM,SAAS,CAAC,eAAe,EAAC,KAAI,MAAM,KAAI,OAAM,MAAM,MAAK,CAAC;AAAA,IAAG,MAAM,IAAI,MAAM,eAAe;AA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;AAAA;AAAA,IAEzF;AAAA,IACA,IAAI,CAAC,QAAO,OAAM,OAAM,aAAa;AAAA,MACnC,IAAI,OAAO,UAAU,WAAW;AAAA,QAAa,OAAO,EAAC,UAAS,KAAI;AAAA,MAClE,IAAI;AAAA,QACF,MAAM,IAAI,eAAe,KAAK;AAAA,QAC9B,IAAI,CAAC;AAAA,UAAG,OAAO,EAAC,UAAS,KAAI;AAAA,QAC7B,IAAI,CAAC,OAAO,SAAS,OAAO,GAAG;AAAA,UAAG,MAAM,IAAI,MAAM,cAAc;AAAA,QAChE,MAAM,SAAS,UAAU,OAAO,EAAC,QAAO,EAAE,OAAM,IAAG,OAAO,KAAI,KAAI,EAAE,KAAI,OAAM,EAAE,MAAK,IACjF,gBAAgB,OAAO,eAAe,OAAO,KAAK,OAAO,GAAG;AAAA,QAChE,IAAI,OAAO,SAAS;AAAA,UAAG,OAAO,EAAC,QAAO,EAAC,OAAM,4BAA2B,QAAO,KAAI,YAAW,KAAK,IAAI,GAAE,KAAK,MAAM,IAAE,OAAO,UAAQ,EAAE,GAAG,CAAC,EAAC,EAAC;AAAA,QAC7I,OAAO,EAAC,OAAM,KAAI,QAAO,QAAO,OAAO,SAAO,EAAC,GAAE,UAAS,EAA2B;AAAA,QACrF,MAAM;AAAA,QAAE,OAAO,EAAC,QAAO,EAAC,OAAM,8BAA6B,QAAO,IAAG,EAAC;AAAA;AAAA;AAAA,IAE1E,aAAa,GAAG;AAAA,MAAE,OAAO;AAAA;AAAA,EAC3B;AAAA;",
|
|
8
|
+
"debugId": "ACEA5589D4401B4264756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -4483,7 +4483,9 @@ function readGeminiSignatures(item, budget) {
|
|
|
4483
4483
|
}
|
|
4484
4484
|
function restoreGeminiHistory(input, budget) {
|
|
4485
4485
|
const original = typeof input === "string" ? [] : list(input, "input").map((raw) => record(raw, "input item"));
|
|
4486
|
-
|
|
4486
|
+
if (original.length > budget.maxItems)
|
|
4487
|
+
fail("resource_limit", "Too many input items", "input");
|
|
4488
|
+
const retained = new Map, parts = new Map, ids = new Map;
|
|
4487
4489
|
original.forEach((item, i) => {
|
|
4488
4490
|
if (typeof item.id === "string") {
|
|
4489
4491
|
if (ids.has(item.id))
|
|
@@ -4492,10 +4494,19 @@ function restoreGeminiHistory(input, budget) {
|
|
|
4492
4494
|
}
|
|
4493
4495
|
});
|
|
4494
4496
|
original.forEach((carrier, index) => {
|
|
4497
|
+
if (carrier.type === "configuration_update") {
|
|
4498
|
+
retained.set(carrier, carrier);
|
|
4499
|
+
return;
|
|
4500
|
+
}
|
|
4495
4501
|
if (carrier.encrypted_content == null)
|
|
4496
4502
|
return;
|
|
4497
|
-
const
|
|
4498
|
-
|
|
4503
|
+
const { content, internal_chat_message_metadata_passthrough: metadata, ...normalized } = carrier;
|
|
4504
|
+
if (content !== undefined && content !== null)
|
|
4505
|
+
fail("unsupported_reasoning", "Gemini signature carriers cannot contain reasoning content", `input[${index}].content`);
|
|
4506
|
+
if (metadata !== undefined)
|
|
4507
|
+
atParam(`input[${index}].internal_chat_message_metadata_passthrough`, () => record(metadata, "message metadata"));
|
|
4508
|
+
const signed = readGeminiSignatures(normalized, budget);
|
|
4509
|
+
retained.set(carrier, normalized);
|
|
4499
4510
|
let previous = -1;
|
|
4500
4511
|
for (const item of signed) {
|
|
4501
4512
|
const at = ids.get(item.id);
|
|
@@ -4525,14 +4536,18 @@ function restoreGeminiHistory(input, budget) {
|
|
|
4525
4536
|
parts.set(item.id, restored);
|
|
4526
4537
|
}
|
|
4527
4538
|
});
|
|
4539
|
+
const positions = original.flatMap((item, index) => retained.has(item) ? [] : [index]);
|
|
4528
4540
|
return {
|
|
4529
|
-
input: typeof input === "string" ? input :
|
|
4541
|
+
input: typeof input === "string" ? input : positions.map((index) => original[index]),
|
|
4530
4542
|
parts,
|
|
4543
|
+
sourceParam(param) {
|
|
4544
|
+
return param.replace(/^input\[(\d+)\]/, (match, index) => positions[Number(index)] === undefined ? match : `input[${positions[Number(index)]}]`);
|
|
4545
|
+
},
|
|
4531
4546
|
canonicalHistory(decoded) {
|
|
4532
|
-
if (!
|
|
4547
|
+
if (!retained.size)
|
|
4533
4548
|
return decoded;
|
|
4534
4549
|
let next = 0;
|
|
4535
|
-
return original.
|
|
4550
|
+
return original.filter((item) => item.type !== "additional_tools").map((item) => retained.get(item) ?? decoded[next++]);
|
|
4536
4551
|
}
|
|
4537
4552
|
};
|
|
4538
4553
|
}
|
|
@@ -4898,6 +4913,8 @@ function canonicalRequest(raw, protocol, model) {
|
|
|
4898
4913
|
return out;
|
|
4899
4914
|
}
|
|
4900
4915
|
function validateCanonical(b, context) {
|
|
4916
|
+
const responsesToGemini = context.sourceProtocol === "responses" && context.targetProtocol === "gemini_generate_content";
|
|
4917
|
+
let configuration;
|
|
4901
4918
|
for (const key of ["stream", "parallel_tool_calls", "store", "background"])
|
|
4902
4919
|
if (b[key] !== undefined)
|
|
4903
4920
|
bool(b[key], key);
|
|
@@ -4927,6 +4944,19 @@ function validateCanonical(b, context) {
|
|
|
4927
4944
|
const input = typeof b.input === "string" ? [] : list(b.input, "input");
|
|
4928
4945
|
for (const [i, raw] of input.entries()) {
|
|
4929
4946
|
const t = record(raw, "input item"), p = `input[${i}]`;
|
|
4947
|
+
if (responsesToGemini && t.type === "configuration_update") {
|
|
4948
|
+
fields2(t, ["type", "reasoning"], p, "unsupported_content");
|
|
4949
|
+
const reasoning = atParam(`${p}.reasoning`, () => record(t.reasoning, "configuration reasoning"));
|
|
4950
|
+
fields2(reasoning, ["effort"], `${p}.reasoning`, "unsupported_reasoning");
|
|
4951
|
+
const param = `${p}.reasoning.effort`;
|
|
4952
|
+
const effort = atParam(param, () => string(reasoning.effort, "configuration effort", true));
|
|
4953
|
+
const map = context.reasoningPolicy?.effortMap;
|
|
4954
|
+
const level = map && Object.hasOwn(map, effort) ? map[effort] : undefined;
|
|
4955
|
+
if (level === undefined || !["minimal", "low", "medium", "high"].includes(level))
|
|
4956
|
+
fail("unsupported_reasoning", "Configuration update requires an explicit Gemini level mapping", param);
|
|
4957
|
+
configuration = { effort, level, param };
|
|
4958
|
+
continue;
|
|
4959
|
+
}
|
|
4930
4960
|
if (t.type === "agent_message") {
|
|
4931
4961
|
validateAgentMessage(t, p, context.capabilities?.limits);
|
|
4932
4962
|
continue;
|
|
@@ -4996,9 +5026,37 @@ function validateCanonical(b, context) {
|
|
|
4996
5026
|
for (const [j, tool] of list(t.tools, "tools").entries())
|
|
4997
5027
|
inspectTool(tool, `input[${i}].tools[${j}]`);
|
|
4998
5028
|
}
|
|
5029
|
+
return configuration;
|
|
5030
|
+
}
|
|
5031
|
+
function projectCodexPageSnapshot(raw, index, target, diagnostics) {
|
|
5032
|
+
const item = record(raw, "input item");
|
|
5033
|
+
const { name } = item;
|
|
5034
|
+
if (item.type !== "function_call_output" || item.call_id !== undefined || item.namespace !== "agent" || typeof name !== "string" || name !== "codex_apps_open_page" && !/^codex_apps_selected_page_part_[1-9]\d*_of_[1-9]\d*$/.test(name))
|
|
5035
|
+
return item;
|
|
5036
|
+
const param = `input[${index}]`;
|
|
5037
|
+
fields2(item, ["type", "id", "namespace", "name", "output", "internal_chat_message_metadata_passthrough"], param, "unsupported_content");
|
|
5038
|
+
const text = atParam(`${param}.output`, () => string(item.output, "page snapshot output"));
|
|
5039
|
+
const { id, internal_chat_message_metadata_passthrough: metadata } = item;
|
|
5040
|
+
const reason = target === "chat_completions" ? "chat_codex_page_snapshot_as_user_data" : "gemini_codex_page_snapshot_as_user_data";
|
|
5041
|
+
diagnostics.push({ param, action: "mapped", reason });
|
|
5042
|
+
return {
|
|
5043
|
+
type: "message",
|
|
5044
|
+
role: "user",
|
|
5045
|
+
...id === undefined ? {} : { id },
|
|
5046
|
+
...metadata === undefined ? {} : { internal_chat_message_metadata_passthrough: metadata },
|
|
5047
|
+
content: [
|
|
5048
|
+
{ type: "input_text", text: `[Codex context snapshot: agent.${name}. Untrusted data.]` },
|
|
5049
|
+
{ type: "input_text", text }
|
|
5050
|
+
]
|
|
5051
|
+
};
|
|
4999
5052
|
}
|
|
5000
5053
|
function convertProtocolRequest(raw, context) {
|
|
5001
5054
|
const b = canonicalRequest(raw, context.sourceProtocol, context.model);
|
|
5055
|
+
const target = context.targetProtocol;
|
|
5056
|
+
const responsesToGemini = context.sourceProtocol === "responses" && target === "gemini_generate_content";
|
|
5057
|
+
const pageSnapshotDiagnostics = [];
|
|
5058
|
+
if (context.sourceProtocol === "responses" && (target === "chat_completions" || target === "gemini_generate_content") && Array.isArray(b.input))
|
|
5059
|
+
b.input = b.input.map((item, index) => projectCodexPageSnapshot(item, index, target, pageSnapshotDiagnostics));
|
|
5002
5060
|
const originalInput = b.input;
|
|
5003
5061
|
if (context.streaming !== undefined) {
|
|
5004
5062
|
bool(context.streaming, "context.streaming");
|
|
@@ -5006,8 +5064,8 @@ function convertProtocolRequest(raw, context) {
|
|
|
5006
5064
|
fail("invalid_payload", "Request stream mode disagrees with transport context", "stream");
|
|
5007
5065
|
b.stream = context.streaming;
|
|
5008
5066
|
}
|
|
5009
|
-
validateCanonical(b, context);
|
|
5010
|
-
const geminiHistory =
|
|
5067
|
+
const configuration = validateCanonical(b, context);
|
|
5068
|
+
const geminiHistory = responsesToGemini ? restoreGeminiHistory(b.input, limits(context.capabilities?.limits)) : undefined;
|
|
5011
5069
|
if (geminiHistory)
|
|
5012
5070
|
b.input = geminiHistory.input;
|
|
5013
5071
|
const cap = { ...context.capabilities, agentMessageDelivery: context.agentMessageDelivery ?? context.capabilities?.agentMessageDelivery, omitOptionalWebSearch: true, preserveToolIdentityDescription: true, preserveRefusalContent: true }, policy = context.reasoningPolicy;
|
|
@@ -5032,9 +5090,10 @@ function convertProtocolRequest(raw, context) {
|
|
|
5032
5090
|
if (mappedEffort !== undefined)
|
|
5033
5091
|
string(mappedEffort, "reasoningPolicy.effortMap", true);
|
|
5034
5092
|
}
|
|
5093
|
+
if (configuration)
|
|
5094
|
+
mappedEffort = configuration.level;
|
|
5035
5095
|
if (sourceThinking !== undefined && reasoning?.effort === undefined && policy?.targetEffort !== undefined)
|
|
5036
5096
|
mappedEffort = string(policy.targetEffort, "reasoningPolicy.targetEffort", true);
|
|
5037
|
-
const target = context.targetProtocol;
|
|
5038
5097
|
if (target === "gemini_generate_content" && reasoning?.effort !== undefined && mappedEffort === undefined && policy?.geminiThinkingBudget === undefined && policy?.geminiThinkingLevel === undefined)
|
|
5039
5098
|
fail("unsupported_reasoning", "Gemini requires an explicit thinking budget or level", "reasoning.effort");
|
|
5040
5099
|
if (sourceThinking !== undefined) {
|
|
@@ -5097,8 +5156,21 @@ function convertProtocolRequest(raw, context) {
|
|
|
5097
5156
|
const decodingTarget = target === "gemini_generate_content" ? "chat_completions" : target;
|
|
5098
5157
|
if (target === "gemini_generate_content" && reasoning)
|
|
5099
5158
|
b.reasoning = { ...reasoning, effort: undefined };
|
|
5100
|
-
|
|
5159
|
+
let decoded;
|
|
5160
|
+
try {
|
|
5161
|
+
decoded = decodeResponsesRequest(b, decodingTarget, { ...cap, reasoningHistory: target !== "anthropic_messages" && cap.reasoningHistory === true, reasoningEffort: target === "chat_completions" && cap.reasoningEffort === true, anthropicThinkingBudget: undefined }, { preserveContentOrder: target === "gemini_generate_content", toolOutputImages: responsesToGemini });
|
|
5162
|
+
} catch (error) {
|
|
5163
|
+
if (geminiHistory && error instanceof ResponsesCodecError && error.param !== undefined)
|
|
5164
|
+
throw new ResponsesCodecError(error.code, error.message, geminiHistory.sourceParam(error.param));
|
|
5165
|
+
throw error;
|
|
5166
|
+
}
|
|
5101
5167
|
const diagnostics = decoded.diagnostics;
|
|
5168
|
+
if (geminiHistory)
|
|
5169
|
+
for (const diagnostic of diagnostics)
|
|
5170
|
+
diagnostic.param = geminiHistory.sourceParam(diagnostic.param);
|
|
5171
|
+
diagnostics.push(...pageSnapshotDiagnostics);
|
|
5172
|
+
if (configuration)
|
|
5173
|
+
diagnostics.push({ param: configuration.param, action: "mapped", reason: "gemini_configuration_effort_update" });
|
|
5102
5174
|
if (target === "gemini_generate_content" && decoded.body.parallel_tool_calls === true) {
|
|
5103
5175
|
delete decoded.body.parallel_tool_calls;
|
|
5104
5176
|
diagnostics.push({ param: "parallel_tool_calls", action: "omitted", reason: "gemini_parallel_tool_calls_allowed" });
|
|
@@ -5128,17 +5200,24 @@ function convertProtocolRequest(raw, context) {
|
|
|
5128
5200
|
if (target === "gemini_generate_content" && (mappedEffort !== undefined || policy?.geminiThinkingBudget !== undefined || policy?.geminiThinkingLevel !== undefined)) {
|
|
5129
5201
|
const level = policy?.geminiThinkingLevel ?? mappedEffort;
|
|
5130
5202
|
if (policy?.geminiThinkingBudget !== undefined && level !== undefined)
|
|
5131
|
-
fail("unsupported_reasoning", "Choose one Gemini thinking policy", "reasoningPolicy");
|
|
5203
|
+
fail("unsupported_reasoning", "Choose one Gemini thinking policy", configuration?.param ?? "reasoningPolicy");
|
|
5132
5204
|
if (level !== undefined && !["minimal", "low", "medium", "high"].includes(level))
|
|
5133
5205
|
fail("unsupported_reasoning", "Unknown Gemini thinking level", "reasoning.effort");
|
|
5134
5206
|
if (mappedEffort !== undefined && policy?.geminiThinkingLevel !== undefined && mappedEffort !== policy.geminiThinkingLevel)
|
|
5135
|
-
fail("unsupported_reasoning", "Conflicting Gemini thinking levels", "reasoning.effort");
|
|
5207
|
+
fail("unsupported_reasoning", "Conflicting Gemini thinking levels", configuration?.param ?? "reasoning.effort");
|
|
5136
5208
|
const thinkingConfig = policy?.geminiThinkingBudget !== undefined ? { thinkingBudget: integer(policy.geminiThinkingBudget, "reasoningPolicy.geminiThinkingBudget", -1) } : { thinkingLevel: string(level, "reasoningPolicy.geminiThinkingLevel", true) };
|
|
5137
5209
|
body.generationConfig = { ...record(body.generationConfig ?? {}, "generationConfig"), thinkingConfig };
|
|
5138
5210
|
}
|
|
5139
5211
|
if (mappedEffort !== undefined || sourceThinking !== undefined || policy?.anthropicThinkingBudget !== undefined)
|
|
5140
5212
|
diagnostics.push({ param: "reasoning", action: "mapped", reason: "explicit_verified_wire_policy" });
|
|
5141
|
-
return {
|
|
5213
|
+
return {
|
|
5214
|
+
body,
|
|
5215
|
+
canonicalHistory: geminiHistory?.canonicalHistory(decoded.canonicalInput) ?? decoded.canonicalInput,
|
|
5216
|
+
diagnostics,
|
|
5217
|
+
toolNames: decoded.toolNames,
|
|
5218
|
+
streaming: b.stream === true,
|
|
5219
|
+
...configuration ? { effectiveReasoningEffort: configuration.effort } : {}
|
|
5220
|
+
};
|
|
5142
5221
|
}
|
|
5143
5222
|
function renderGemini(chat, context, input, signedParts, diagnostics) {
|
|
5144
5223
|
const result = { contents: [] }, contents = result.contents, system = [], calls = new Map;
|
|
@@ -6043,7 +6122,7 @@ class CanonicalStreamRenderer {
|
|
|
6043
6122
|
var LLM_PROTOCOLS = ["responses", "chat_completions", "anthropic_messages", "gemini_generate_content"];
|
|
6044
6123
|
|
|
6045
6124
|
// packages/llms/src/protocol-session/index.ts
|
|
6046
|
-
var PROTOCOL_CONVERSION_RULES_VERSION = "1.0.
|
|
6125
|
+
var PROTOCOL_CONVERSION_RULES_VERSION = "1.0.5";
|
|
6047
6126
|
function describeProtocolConversion() {
|
|
6048
6127
|
return {
|
|
6049
6128
|
rulesVersion: PROTOCOL_CONVERSION_RULES_VERSION,
|
|
@@ -6677,7 +6756,7 @@ class AgentRelay {
|
|
|
6677
6756
|
}
|
|
6678
6757
|
|
|
6679
6758
|
// plugins/llm-protocol-adapter/server/service.ts
|
|
6680
|
-
var REASONING_RULES_VERSION = "2026-
|
|
6759
|
+
var REASONING_RULES_VERSION = "2026-11-11.1";
|
|
6681
6760
|
var concrete = new Set(["none", "minimal", "low", "medium", "high", "xhigh", "max"]);
|
|
6682
6761
|
var effortProtocols = new Set(["responses", "chat_completions", "anthropic_messages", "gemini_generate_content"]);
|
|
6683
6762
|
var geminiEfforts = new Set(["minimal", "low", "medium", "high"]);
|
|
@@ -6794,6 +6873,11 @@ function conversionService(catalog, worker, history, runBackground) {
|
|
|
6794
6873
|
selected = effort;
|
|
6795
6874
|
}
|
|
6796
6875
|
const result = engine.convertRequest(raw);
|
|
6876
|
+
if (result.effectiveReasoningEffort !== undefined) {
|
|
6877
|
+
if (!profile?.supportedEfforts.includes(result.effectiveReasoningEffort))
|
|
6878
|
+
fail2("reasoning.effort");
|
|
6879
|
+
selected = result.effectiveReasoningEffort;
|
|
6880
|
+
}
|
|
6797
6881
|
rewrittenDeveloper = false;
|
|
6798
6882
|
if (deepseekChat) {
|
|
6799
6883
|
for (const message of chatMessages(result.body))
|
|
@@ -6875,8 +6959,12 @@ function createControl(host) {
|
|
|
6875
6959
|
const catalog = host.services.consume("models-dev", MODELS_DEV_CAPABILITIES_SERVICE_ID, MODELS_DEV_CAPABILITIES_CONTRACT_VERSION);
|
|
6876
6960
|
const conversion = conversionService(catalog, false);
|
|
6877
6961
|
host.services.publish(CONVERSION_SERVICE_ID, CONVERSION_VERSION, conversion);
|
|
6878
|
-
const history = new HistoryTransport
|
|
6879
|
-
timer
|
|
6962
|
+
const history = new HistoryTransport;
|
|
6963
|
+
let timer;
|
|
6964
|
+
const sweepHistory = () => {
|
|
6965
|
+
timer = setInterval(() => history.sweep(), 30000);
|
|
6966
|
+
timer.unref?.();
|
|
6967
|
+
};
|
|
6880
6968
|
const dispose = () => {
|
|
6881
6969
|
clearInterval(timer);
|
|
6882
6970
|
history.clear();
|
|
@@ -6892,14 +6980,19 @@ function createControl(host) {
|
|
|
6892
6980
|
throw error;
|
|
6893
6981
|
}
|
|
6894
6982
|
} });
|
|
6895
|
-
return { api: [], rpc: [], start() {
|
|
6983
|
+
return { api: [], rpc: [], start() {
|
|
6984
|
+
if (host.afterActivation)
|
|
6985
|
+
host.afterActivation(sweepHistory);
|
|
6986
|
+
else
|
|
6987
|
+
sweepHistory();
|
|
6988
|
+
}, async dispose() {
|
|
6896
6989
|
dispose();
|
|
6897
6990
|
} };
|
|
6898
6991
|
}
|
|
6899
|
-
var control_default = { createControl };
|
|
6992
|
+
var control_default = { supportsOnlinePreparation: true, createControl };
|
|
6900
6993
|
export {
|
|
6901
6994
|
createControl,
|
|
6902
6995
|
control_default as default
|
|
6903
6996
|
};
|
|
6904
6997
|
|
|
6905
|
-
//# debugId=
|
|
6998
|
+
//# debugId=03634829068B51AD64756E2164756E21
|