codex-grok-bridge 1.8.3 → 1.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +66 -19
- package/package.json +2 -1
- package/scripts/codex-grok.mjs +7 -1
- package/scripts/codex-wrapper.mjs +9 -1
- package/scripts/install-codex-grok-app.ps1 +14 -6
- package/scripts/install-codex-grok-app.sh +47 -21
- package/scripts/launch-desktop.mjs +19 -22
- package/scripts/verify-app-server.mjs +133 -0
- package/src/bridge.mjs +17 -5
- package/src/diagnostics.mjs +1 -1
- package/src/errors.mjs +6 -0
- package/src/imagegen.mjs +11 -8
- package/src/provider-align.mjs +231 -0
- package/src/proxy.mjs +60 -9
- package/src/rollout.mjs +36 -5
- package/src/router.mjs +40 -22
- package/src/runtime.mjs +2 -0
- package/src/tools.mjs +11 -6
- package/src/transport.mjs +37 -5
- package/src/videogen.mjs +177 -2
- package/src/voice.mjs +16 -2
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { mkdtemp, mkdir, writeFile, readFile, readdir, rm } from "node:fs/promises";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import { once } from "node:events";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { resolveCodexBinary } from "../src/paths.mjs";
|
|
12
|
+
|
|
13
|
+
async function readRolloutTree(dir) {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
async function walk(current) {
|
|
16
|
+
let entries;
|
|
17
|
+
try {
|
|
18
|
+
entries = await readdir(current, { withFileTypes: true });
|
|
19
|
+
} catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
for (const entry of entries) {
|
|
23
|
+
const full = path.join(current, entry.name);
|
|
24
|
+
if (entry.isDirectory()) await walk(full);
|
|
25
|
+
else if (entry.name.endsWith(".jsonl")) chunks.push(await readFile(full, "utf8"));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
await walk(dir);
|
|
29
|
+
return chunks.join("\n");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const bundledCodex = resolveCodexBinary();
|
|
33
|
+
if (!existsSync(bundledCodex)) {
|
|
34
|
+
console.error(`verify:app-server skipped: bundled Codex CLI not found at ${bundledCodex}`);
|
|
35
|
+
console.error("Set CODEX_BINARY, or install Codex (macOS /Applications/Codex.app, Linux /usr/lib/chatgpt/resources/codex, Windows store pointer).");
|
|
36
|
+
process.exit(2);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const home = await mkdtemp(path.join(tmpdir(), "codex-provider-verifier-"));
|
|
40
|
+
const child = spawn(
|
|
41
|
+
process.execPath,
|
|
42
|
+
[fileURLToPath(new URL("./codex-wrapper.mjs", import.meta.url)), "app-server"],
|
|
43
|
+
{ stdio: ["pipe", "pipe", "ignore"], env: { ...process.env, CODEX_HOME: home } },
|
|
44
|
+
);
|
|
45
|
+
const exited = once(child, "close");
|
|
46
|
+
const pending = new Map();
|
|
47
|
+
let counter = 0;
|
|
48
|
+
const lines = createInterface({ input: child.stdout });
|
|
49
|
+
lines.on("line", (line) => {
|
|
50
|
+
const m = JSON.parse(line);
|
|
51
|
+
if (pending.has(m.id)) {
|
|
52
|
+
pending.get(m.id)(m);
|
|
53
|
+
pending.delete(m.id);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
const request = (method, params) =>
|
|
57
|
+
new Promise((resolve, reject) => {
|
|
58
|
+
const id = ++counter;
|
|
59
|
+
const timer = setTimeout(() => {
|
|
60
|
+
pending.delete(id);
|
|
61
|
+
reject(Error("RPC timeout: " + method));
|
|
62
|
+
}, 30000);
|
|
63
|
+
pending.set(id, (msg) => {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
msg.error ? reject(Error(msg.error.message)) : resolve(msg.result);
|
|
66
|
+
});
|
|
67
|
+
child.stdin.write(JSON.stringify({ id, method, params }) + "\n");
|
|
68
|
+
});
|
|
69
|
+
try {
|
|
70
|
+
await request("initialize", {
|
|
71
|
+
clientInfo: { name: "grok-bridge-verifier", version: "0.2.0" },
|
|
72
|
+
capabilities: { experimentalApi: true },
|
|
73
|
+
});
|
|
74
|
+
child.stdin.write(JSON.stringify({ method: "initialized" }) + "\n");
|
|
75
|
+
const models = await request("model/list", { limit: 100 });
|
|
76
|
+
assert.ok(models.data.some((m) => m.id === "grok-4.7"));
|
|
77
|
+
assert.ok(models.data.some((m) => m.id === "grok-4.6"));
|
|
78
|
+
const gpt = models.data.find((m) => m.id.startsWith("gpt"));
|
|
79
|
+
assert.ok(gpt);
|
|
80
|
+
const threadId = randomUUID();
|
|
81
|
+
const timestamp = new Date().toISOString();
|
|
82
|
+
const sessions = path.join(home, "sessions");
|
|
83
|
+
await mkdir(sessions);
|
|
84
|
+
const rollout = path.join(sessions, `rollout-${timestamp.slice(0, 19).replaceAll(":", "-")}-${threadId}.jsonl`);
|
|
85
|
+
const fixture = [
|
|
86
|
+
{ type: "session_meta", payload: { id: threadId, timestamp, cwd: process.cwd(), originator: "provider-verifier", cli_version: "0.153.4", model_provider: "openai", source: "cli" } },
|
|
87
|
+
{ type: "response_item", payload: { type: "message", role: "user", content: [{ type: "input_text", text: "Keep this verification history." }] } },
|
|
88
|
+
{ type: "response_item", payload: { type: "message", role: "assistant", content: [{ type: "output_text", text: "History fixture; no inference was executed." }] } },
|
|
89
|
+
].map((item) => JSON.stringify({ timestamp, ...item }) + "\n").join("");
|
|
90
|
+
await writeFile(rollout, fixture, { mode: 0o600 });
|
|
91
|
+
const original = await request("thread/resume", {
|
|
92
|
+
threadId,
|
|
93
|
+
path: rollout,
|
|
94
|
+
model: gpt.model,
|
|
95
|
+
modelProvider: "openai",
|
|
96
|
+
cwd: process.cwd(),
|
|
97
|
+
sandbox: "read-only",
|
|
98
|
+
approvalPolicy: "on-request",
|
|
99
|
+
});
|
|
100
|
+
const transitions = [];
|
|
101
|
+
for (const [model, provider] of [["grok-4.7", "grok_build_cli"], ["grok-4.6", "grok_build_cli"], [gpt.model, "openai"]]) {
|
|
102
|
+
await request("thread/settings/update", { threadId, model });
|
|
103
|
+
const resumed = await request("thread/resume", { threadId, excludeTurns: true });
|
|
104
|
+
assert.equal(resumed.thread.id, threadId);
|
|
105
|
+
assert.equal(resumed.modelProvider, provider);
|
|
106
|
+
assert.equal(resumed.model, model);
|
|
107
|
+
assert.equal(resumed.approvalPolicy, original.approvalPolicy);
|
|
108
|
+
assert.deepEqual(resumed.sandbox, original.sandbox);
|
|
109
|
+
transitions.push({ model: resumed.model, provider: resumed.modelProvider });
|
|
110
|
+
}
|
|
111
|
+
const savedRollout = await readRolloutTree(home);
|
|
112
|
+
assert.match(savedRollout, /Keep this verification history/);
|
|
113
|
+
assert.match(savedRollout, /History fixture; no inference was executed/);
|
|
114
|
+
console.log(JSON.stringify({
|
|
115
|
+
models: models.data.map((m) => m.id),
|
|
116
|
+
transitions,
|
|
117
|
+
sameThread: true,
|
|
118
|
+
permissionsPreserved: true,
|
|
119
|
+
inferenceExecuted: false,
|
|
120
|
+
}));
|
|
121
|
+
} finally {
|
|
122
|
+
child.stdin.end();
|
|
123
|
+
child.kill("SIGTERM");
|
|
124
|
+
const killer = new Promise((resolve) =>
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
child.kill("SIGKILL");
|
|
127
|
+
resolve();
|
|
128
|
+
}, 5000),
|
|
129
|
+
);
|
|
130
|
+
await Promise.race([exited, killer]);
|
|
131
|
+
lines.close();
|
|
132
|
+
await rm(home, { recursive: true, force: true });
|
|
133
|
+
}
|
package/src/bridge.mjs
CHANGED
|
@@ -9,7 +9,15 @@ import {
|
|
|
9
9
|
runGrok,
|
|
10
10
|
} from "./cli-inference.mjs";
|
|
11
11
|
import { emitProxySse, readRelayProxySse } from "./proxy.mjs";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
addUsage,
|
|
14
|
+
retainedInputItems,
|
|
15
|
+
stampUsage,
|
|
16
|
+
stripVideoToolEvents,
|
|
17
|
+
usageFromParts,
|
|
18
|
+
videoCallsFromParts,
|
|
19
|
+
videoToolOutput,
|
|
20
|
+
} from "./videogen.mjs";
|
|
13
21
|
import { forwardImagine } from "./imagine.mjs";
|
|
14
22
|
import { attachSidebandUpgrade, forwardRealtime } from "./realtime.mjs";
|
|
15
23
|
import {
|
|
@@ -38,18 +46,21 @@ const VIDEO_FOLLOW_UPS = 2;
|
|
|
38
46
|
|
|
39
47
|
async function relayWithVideo(options, output, map, usageBox, onClientByte) {
|
|
40
48
|
let body = options.body;
|
|
49
|
+
let totalUsage = {};
|
|
41
50
|
for (let step = 0; step <= VIDEO_FOLLOW_UPS; step += 1) {
|
|
42
51
|
const parts = await readRelayProxySse({ ...options, body }, output);
|
|
52
|
+
totalUsage = addUsage(totalUsage, usageFromParts(parts));
|
|
43
53
|
const calls =
|
|
44
54
|
process.env.GROK_BRIDGE_VIDEO_GEN === "off" ? [] : videoCallsFromParts(parts);
|
|
45
55
|
if (!calls.length || step === VIDEO_FOLLOW_UPS) {
|
|
46
|
-
|
|
56
|
+
const visible = calls.length ? stripVideoToolEvents(parts) : parts;
|
|
57
|
+
await emitProxySse(stampUsage(visible, totalUsage), output, map, usageBox, onClientByte);
|
|
47
58
|
return;
|
|
48
59
|
}
|
|
49
60
|
// The videos API poll can sit for minutes. Keepalive is safe here: this
|
|
50
61
|
// upstream reply is finished, and nothing has been written to Codex yet.
|
|
51
62
|
onClientByte();
|
|
52
|
-
const additions = [];
|
|
63
|
+
const additions = [...retainedInputItems(parts)];
|
|
53
64
|
for (const call of calls) {
|
|
54
65
|
const toolOutput = await videoToolOutput(call, options.video);
|
|
55
66
|
additions.push(
|
|
@@ -312,6 +323,7 @@ export function createBridgeServer(options = {}) {
|
|
|
312
323
|
let acquired = false;
|
|
313
324
|
let queuedMs = 0;
|
|
314
325
|
let cacheUsage = null;
|
|
326
|
+
const usageBox = {};
|
|
315
327
|
try {
|
|
316
328
|
await slots.acquire(controller.signal);
|
|
317
329
|
acquired = true;
|
|
@@ -363,7 +375,6 @@ export function createBridgeServer(options = {}) {
|
|
|
363
375
|
const { request, map, projected } = toProxyRequest(sanitizeImages(body));
|
|
364
376
|
if (convId) request.prompt_cache_key = convId;
|
|
365
377
|
request.input = prefixes.reuse(convId, projected);
|
|
366
|
-
const usageBox = {};
|
|
367
378
|
try {
|
|
368
379
|
await relayWithVideo(
|
|
369
380
|
{
|
|
@@ -403,7 +414,8 @@ export function createBridgeServer(options = {}) {
|
|
|
403
414
|
}
|
|
404
415
|
}
|
|
405
416
|
diagnostics.record({
|
|
406
|
-
event: "turn_ok",
|
|
417
|
+
event: usageBox.upstreamFailed ? "turn_failed" : "turn_ok",
|
|
418
|
+
...(usageBox.upstreamFailed ? { kind: "upstream_status" } : {}),
|
|
407
419
|
...shape,
|
|
408
420
|
...(cacheUsage ?? {}),
|
|
409
421
|
queuedMs,
|
package/src/diagnostics.mjs
CHANGED
|
@@ -11,7 +11,7 @@ export const MAX_LOG_BYTES = 4 * 1024 * 1024;
|
|
|
11
11
|
// Values that must never reach disk, matched structurally rather than by name,
|
|
12
12
|
// because the field a token arrives in is not stable.
|
|
13
13
|
const SECRET_PATTERNS = [
|
|
14
|
-
[/Bearer\s+[A-Za-z0-9._~+/-]+=*/
|
|
14
|
+
[/Bearer\s+[A-Za-z0-9._~+/-]+=*/gi, "Bearer [redacted]"],
|
|
15
15
|
[/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+/g, "[jwt]"],
|
|
16
16
|
[/sk-[A-Za-z0-9_-]{16,}/g, "[key]"],
|
|
17
17
|
[/xai-[A-Za-z0-9_-]{16,}/g, "[key]"],
|
package/src/errors.mjs
CHANGED
|
@@ -29,6 +29,7 @@ const MESSAGES = Object.freeze({
|
|
|
29
29
|
"Grok closed the connection before finishing. Retry.",
|
|
30
30
|
[BRIDGE_ERROR.UPSTREAM_PROTOCOL]:
|
|
31
31
|
"Grok sent a malformed response. Retry.",
|
|
32
|
+
[BRIDGE_ERROR.UPSTREAM_STATUS]: "Grok returned an error status. Retry.",
|
|
32
33
|
[BRIDGE_ERROR.PAYLOAD]: "Grok rejected the request payload",
|
|
33
34
|
[BRIDGE_ERROR.INTERNAL]: "Grok bridge failed to process the turn",
|
|
34
35
|
});
|
|
@@ -62,6 +63,11 @@ function* chain(error, depth = 0) {
|
|
|
62
63
|
export function classifyBridgeError(error, context = {}) {
|
|
63
64
|
for (const link of chain(error)) {
|
|
64
65
|
const code = typeof link.code === "string" ? link.code : "";
|
|
66
|
+
if (link.name === "GrokAuthError") return BRIDGE_ERROR.AUTH;
|
|
67
|
+
const status = Number(link.status);
|
|
68
|
+
if (status === 401 || status === 403) return BRIDGE_ERROR.AUTH;
|
|
69
|
+
if (status === 400 || status === 422) return BRIDGE_ERROR.PAYLOAD;
|
|
70
|
+
if (status === 429 || (status >= 500 && status <= 599)) return BRIDGE_ERROR.UPSTREAM_STATUS;
|
|
65
71
|
if (link.name === "AbortError" || ABORT_CODES.has(code)) {
|
|
66
72
|
// The bridge aborts the upstream only when the client hangs up, so an
|
|
67
73
|
// abort-shaped error means the client left. If the bridge ever aborts for
|
package/src/imagegen.mjs
CHANGED
|
@@ -15,15 +15,18 @@ export const DEFAULT_IMAGE_DIR = path.join(
|
|
|
15
15
|
".local/share/codex-grok-bridge/generated-images",
|
|
16
16
|
);
|
|
17
17
|
|
|
18
|
-
const
|
|
19
|
-
[Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), "png"],
|
|
20
|
-
[Buffer.from([255, 216, 255]), "jpg"],
|
|
21
|
-
[Buffer.from("RIFF", "ascii"), "webp"],
|
|
22
|
-
];
|
|
18
|
+
const PNG_MAGIC = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
23
19
|
|
|
24
20
|
function extensionFor(bytes) {
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
if (bytes.length >= 8 && bytes.subarray(0, 8).equals(PNG_MAGIC)) return "png";
|
|
22
|
+
if (bytes.length >= 3 && bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255)
|
|
23
|
+
return "jpg";
|
|
24
|
+
if (
|
|
25
|
+
bytes.length >= 12 &&
|
|
26
|
+
bytes.toString("ascii", 0, 4) === "RIFF" &&
|
|
27
|
+
bytes.toString("ascii", 8, 12) === "WEBP"
|
|
28
|
+
)
|
|
29
|
+
return "webp";
|
|
27
30
|
return "bin";
|
|
28
31
|
}
|
|
29
32
|
|
|
@@ -62,7 +65,7 @@ export function saveGeneratedImage(item, options = {}) {
|
|
|
62
65
|
* colour type: 4 is greyscale+alpha, 6 is RGBA.
|
|
63
66
|
*/
|
|
64
67
|
export function hasAlphaChannel(bytes) {
|
|
65
|
-
const isPng = bytes.subarray(0, 8).equals(
|
|
68
|
+
const isPng = bytes.length >= 8 && bytes.subarray(0, 8).equals(PNG_MAGIC);
|
|
66
69
|
if (!isPng || bytes.length < 26) return false;
|
|
67
70
|
const colorType = bytes[25];
|
|
68
71
|
return colorType === 4 || colorType === 6;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Codex CLI 0.166+ rejects a turn when the thread provider differs from the
|
|
2
|
+
// app-server's configured provider (openai/codex#57840). The desktop wrapper
|
|
3
|
+
// keeps one app-server for both providers, so a switch has to make those two
|
|
4
|
+
// values match before resume or turn/start. The value lives in the user's
|
|
5
|
+
// config.toml for that process only; the baseline is restored on exit.
|
|
6
|
+
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, rmSync } from "node:fs";
|
|
9
|
+
import { lstat, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { rewriteRolloutProviderText } from "./rollout.mjs";
|
|
12
|
+
|
|
13
|
+
const STATE_DBS = ["state_5.sqlite", "state_4.sqlite"];
|
|
14
|
+
|
|
15
|
+
export function providerMarkerPath(codexHome) {
|
|
16
|
+
return path.join(codexHome, "grok-bridge-provider.json");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function readModelProvider(text) {
|
|
20
|
+
let inTable = false;
|
|
21
|
+
for (const line of String(text ?? "").split(/\r?\n/)) {
|
|
22
|
+
if (/^\s*\[/.test(line)) inTable = true;
|
|
23
|
+
if (inTable) continue;
|
|
24
|
+
const match = line.match(/^\s*model_provider\s*=\s*(.*?)\s*(?:#.*)?$/);
|
|
25
|
+
if (!match) continue;
|
|
26
|
+
return unquoteToml(match[1].trim());
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function unquoteToml(value) {
|
|
32
|
+
if (
|
|
33
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
34
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
35
|
+
) {
|
|
36
|
+
return value
|
|
37
|
+
.slice(1, -1)
|
|
38
|
+
.replace(/\\n/g, "\n")
|
|
39
|
+
.replace(/\\"/g, '"')
|
|
40
|
+
.replace(/\\'/g, "'")
|
|
41
|
+
.replace(/\\\\/g, "\\");
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function renderModelProvider(text, provider) {
|
|
47
|
+
const source = String(text ?? "");
|
|
48
|
+
const lines = source.split(/\r?\n/);
|
|
49
|
+
if (lines.at(-1) === "") lines.pop();
|
|
50
|
+
let inTable = false;
|
|
51
|
+
let replaced = false;
|
|
52
|
+
const out = [];
|
|
53
|
+
for (const line of lines) {
|
|
54
|
+
if (/^\s*\[/.test(line)) inTable = true;
|
|
55
|
+
if (!inTable && /^\s*model_provider\s*=/.test(line)) {
|
|
56
|
+
replaced = true;
|
|
57
|
+
if (provider != null) out.push(`model_provider = ${JSON.stringify(provider)}`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
out.push(line);
|
|
61
|
+
}
|
|
62
|
+
if (!replaced && provider != null) {
|
|
63
|
+
let insertAt = 0;
|
|
64
|
+
while (
|
|
65
|
+
insertAt < out.length &&
|
|
66
|
+
(/^\s*$/.test(out[insertAt]) || /^\s*#/.test(out[insertAt]))
|
|
67
|
+
) {
|
|
68
|
+
insertAt += 1;
|
|
69
|
+
}
|
|
70
|
+
out.splice(insertAt, 0, `model_provider = ${JSON.stringify(provider)}`);
|
|
71
|
+
}
|
|
72
|
+
if (out.length === 0) return "";
|
|
73
|
+
return `${out.join("\n")}\n`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function configPath(codexHome) {
|
|
77
|
+
return path.join(codexHome, "config.toml");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function alignConfiguredProvider(codexHome, provider) {
|
|
81
|
+
if (!codexHome || !provider) return false;
|
|
82
|
+
const file = configPath(codexHome);
|
|
83
|
+
const text = existsSync(file) ? readFileSync(file, "utf8") : "";
|
|
84
|
+
if (readModelProvider(text) === provider) return false;
|
|
85
|
+
const next = renderModelProvider(text, provider);
|
|
86
|
+
mkdirSync(codexHome, { recursive: true, mode: 0o700 });
|
|
87
|
+
writeFileSync(file, next, { mode: 0o600 });
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function applyModelProvider(codexHome, provider) {
|
|
92
|
+
const file = configPath(codexHome);
|
|
93
|
+
const exists = existsSync(file);
|
|
94
|
+
const text = exists ? readFileSync(file, "utf8") : "";
|
|
95
|
+
const next = renderModelProvider(text, provider);
|
|
96
|
+
if (next === text) return false;
|
|
97
|
+
if (!next && !exists) return false;
|
|
98
|
+
if (!next) {
|
|
99
|
+
rmSync(file, { force: true });
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
mkdirSync(codexHome, { recursive: true, mode: 0o700 });
|
|
103
|
+
writeFileSync(file, next, exists ? undefined : { mode: 0o600 });
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function captureProviderBaseline(codexHome) {
|
|
108
|
+
const marker = providerMarkerPath(codexHome);
|
|
109
|
+
if (existsSync(marker)) {
|
|
110
|
+
try {
|
|
111
|
+
const saved = JSON.parse(readFileSync(marker, "utf8"));
|
|
112
|
+
if (saved && saved.codexHome === codexHome)
|
|
113
|
+
applyModelProvider(codexHome, saved.original ?? null);
|
|
114
|
+
} catch {
|
|
115
|
+
// A damaged marker must not block startup. The next exit rewrites it.
|
|
116
|
+
}
|
|
117
|
+
rmSync(marker, { force: true });
|
|
118
|
+
}
|
|
119
|
+
const file = configPath(codexHome);
|
|
120
|
+
const original = existsSync(file) ? readModelProvider(readFileSync(file, "utf8")) : null;
|
|
121
|
+
mkdirSync(codexHome, { recursive: true, mode: 0o700 });
|
|
122
|
+
writeFileSync(
|
|
123
|
+
marker,
|
|
124
|
+
`${JSON.stringify({ codexHome, original })}\n`,
|
|
125
|
+
{ mode: 0o600 },
|
|
126
|
+
);
|
|
127
|
+
return original;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function restoreProviderBaseline(codexHome) {
|
|
131
|
+
const marker = providerMarkerPath(codexHome);
|
|
132
|
+
if (!existsSync(marker)) return false;
|
|
133
|
+
try {
|
|
134
|
+
const saved = JSON.parse(readFileSync(marker, "utf8"));
|
|
135
|
+
if (saved?.codexHome === codexHome) applyModelProvider(codexHome, saved.original ?? null);
|
|
136
|
+
} catch {
|
|
137
|
+
// Leave the config alone if the marker cannot be read.
|
|
138
|
+
}
|
|
139
|
+
rmSync(marker, { force: true });
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function updateStateDb(file, threadId, provider, model) {
|
|
144
|
+
let DatabaseSync;
|
|
145
|
+
try {
|
|
146
|
+
({ DatabaseSync } = await import("node:sqlite"));
|
|
147
|
+
} catch {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
let db;
|
|
151
|
+
try {
|
|
152
|
+
db = new DatabaseSync(file, { timeout: 3000 });
|
|
153
|
+
let row;
|
|
154
|
+
try {
|
|
155
|
+
row = db.prepare("SELECT model_provider, rollout_path FROM threads WHERE id = ?").get(threadId);
|
|
156
|
+
} catch {
|
|
157
|
+
row = db.prepare("SELECT model_provider FROM threads WHERE id = ?").get(threadId);
|
|
158
|
+
}
|
|
159
|
+
if (!row) return null;
|
|
160
|
+
if (model != null) {
|
|
161
|
+
try {
|
|
162
|
+
db.prepare("UPDATE threads SET model_provider = ?, model = ? WHERE id = ?").run(
|
|
163
|
+
provider,
|
|
164
|
+
model,
|
|
165
|
+
threadId,
|
|
166
|
+
);
|
|
167
|
+
} catch {
|
|
168
|
+
db.prepare("UPDATE threads SET model_provider = ? WHERE id = ?").run(provider, threadId);
|
|
169
|
+
}
|
|
170
|
+
} else if (row.model_provider !== provider) {
|
|
171
|
+
db.prepare("UPDATE threads SET model_provider = ? WHERE id = ?").run(provider, threadId);
|
|
172
|
+
}
|
|
173
|
+
return row;
|
|
174
|
+
} catch {
|
|
175
|
+
return null;
|
|
176
|
+
} finally {
|
|
177
|
+
try {
|
|
178
|
+
db?.close();
|
|
179
|
+
} catch {
|
|
180
|
+
// The resume path still has the rollout fallback.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function rewriteSavedModelProvider({
|
|
186
|
+
codexHome,
|
|
187
|
+
threadId,
|
|
188
|
+
rolloutPath,
|
|
189
|
+
provider,
|
|
190
|
+
model,
|
|
191
|
+
}) {
|
|
192
|
+
if (!provider || !threadId) return false;
|
|
193
|
+
let savedPath = rolloutPath;
|
|
194
|
+
let updated = false;
|
|
195
|
+
if (codexHome) {
|
|
196
|
+
for (const name of STATE_DBS) {
|
|
197
|
+
const file = path.join(codexHome, name);
|
|
198
|
+
if (!existsSync(file)) continue;
|
|
199
|
+
const row = await updateStateDb(file, threadId, provider, model);
|
|
200
|
+
if (row) {
|
|
201
|
+
updated = true;
|
|
202
|
+
savedPath = savedPath || row.rollout_path;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (savedPath && (await rewriteRolloutModelProvider(savedPath, provider, model))) updated = true;
|
|
207
|
+
return updated;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function rewriteRolloutModelProvider(file, provider, model) {
|
|
211
|
+
if (typeof file !== "string" || !file || !provider) return false;
|
|
212
|
+
let info;
|
|
213
|
+
try {
|
|
214
|
+
info = await lstat(file);
|
|
215
|
+
} catch {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (!info.isFile()) return false;
|
|
219
|
+
const first = await readFile(file);
|
|
220
|
+
const next = rewriteRolloutProviderText(first.toString("utf8"), provider, model);
|
|
221
|
+
if (next === first.toString("utf8")) return true;
|
|
222
|
+
const tmp = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
223
|
+
await writeFile(tmp, next, { mode: info.mode & 0o777 });
|
|
224
|
+
const again = await readFile(file);
|
|
225
|
+
if (!again.equals(first)) {
|
|
226
|
+
await rm(tmp, { force: true });
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
await rename(tmp, file);
|
|
230
|
+
return true;
|
|
231
|
+
}
|
package/src/proxy.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { homedir } from "node:os";
|
|
|
3
3
|
import { resolveGrokBinary } from "./paths.mjs";
|
|
4
4
|
import { createSseRewriter } from "./tools.mjs";
|
|
5
5
|
import { requestStream } from "./transport.mjs";
|
|
6
|
+
import { redact } from "./diagnostics.mjs";
|
|
6
7
|
import { BRIDGE_ERROR, classifyBridgeError } from "./errors.mjs";
|
|
7
8
|
|
|
8
9
|
export const DEFAULT_PROXY_BASE = "https://cli-chat-proxy.grok.com/v1";
|
|
@@ -32,10 +33,10 @@ export function detectGrokClientVersion(home = homedir()) {
|
|
|
32
33
|
return cachedClientVersion;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
function redactedError(status, text) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
function redactedError(status, text, token) {
|
|
37
|
+
let trimmed = String(text || "");
|
|
38
|
+
if (token) trimmed = trimmed.split(token).join("[redacted]");
|
|
39
|
+
trimmed = redact(trimmed).slice(0, 200);
|
|
39
40
|
if (status === 401 || status === 403)
|
|
40
41
|
return "Grok login expired. Run grok login.";
|
|
41
42
|
return `Grok Responses proxy failed (${status})${trimmed ? `: ${trimmed}` : ""}`;
|
|
@@ -73,7 +74,9 @@ export async function openProxyStream(options) {
|
|
|
73
74
|
});
|
|
74
75
|
if (!response.ok) {
|
|
75
76
|
const text = await response.text().catch(() => "");
|
|
76
|
-
|
|
77
|
+
const error = new Error(redactedError(response.status, text, options.token));
|
|
78
|
+
error.status = response.status;
|
|
79
|
+
throw error;
|
|
77
80
|
}
|
|
78
81
|
if (!response.body) throw new Error("Grok Responses proxy returned no body");
|
|
79
82
|
return response;
|
|
@@ -129,6 +132,40 @@ const TERMINAL_EVENTS = new Set([
|
|
|
129
132
|
"response.failed",
|
|
130
133
|
]);
|
|
131
134
|
|
|
135
|
+
export const MAX_SSE_BYTES = 40 * 1024 * 1024;
|
|
136
|
+
|
|
137
|
+
function sseTooLarge() {
|
|
138
|
+
return Object.assign(new Error("Grok response exceeded the bridge buffer"), {
|
|
139
|
+
code: "ERR_UPSTREAM_TOO_LARGE",
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function noteSseBytes(total, extra) {
|
|
144
|
+
const next = total + extra;
|
|
145
|
+
if (next > MAX_SSE_BYTES) throw sseTooLarge();
|
|
146
|
+
return next;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function isFailedSseBlock(part) {
|
|
150
|
+
for (const line of String(part).split("\n")) {
|
|
151
|
+
if (line.startsWith("event:") && line.slice(6).trim() === "response.failed")
|
|
152
|
+
return true;
|
|
153
|
+
if (!line.startsWith("data:")) continue;
|
|
154
|
+
const payload = line.slice(5).trim();
|
|
155
|
+
if (!payload || payload === "[DONE]") continue;
|
|
156
|
+
try {
|
|
157
|
+
if (JSON.parse(payload)?.type === "response.failed") return true;
|
|
158
|
+
} catch {
|
|
159
|
+
// A non-JSON data line is not a failure event.
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function noteUpstreamFailure(usageBox, part) {
|
|
166
|
+
if (usageBox && isFailedSseBlock(part)) usageBox.upstreamFailed = true;
|
|
167
|
+
}
|
|
168
|
+
|
|
132
169
|
export function isTerminalSseBlock(part) {
|
|
133
170
|
for (const line of part.split("\n")) {
|
|
134
171
|
if (line.startsWith("event:") && TERMINAL_EVENTS.has(line.slice(6).trim()))
|
|
@@ -182,10 +219,18 @@ export async function openProxyStreamWithRetry(options, attempts = PROXY_ATTEMPT
|
|
|
182
219
|
throw lastError;
|
|
183
220
|
}
|
|
184
221
|
|
|
185
|
-
function completeBlocks(buffer) {
|
|
186
|
-
const parts =
|
|
187
|
-
|
|
188
|
-
|
|
222
|
+
export function completeBlocks(buffer) {
|
|
223
|
+
const parts = [];
|
|
224
|
+
let rest = String(buffer ?? "");
|
|
225
|
+
const boundary = /\r?\n\r?\n/;
|
|
226
|
+
while (rest.length) {
|
|
227
|
+
const match = boundary.exec(rest);
|
|
228
|
+
if (!match) break;
|
|
229
|
+
const part = rest.slice(0, match.index).replaceAll("\r\n", "\n").replaceAll("\r", "\n");
|
|
230
|
+
rest = rest.slice(match.index + match[0].length);
|
|
231
|
+
if (part.trim()) parts.push(part);
|
|
232
|
+
}
|
|
233
|
+
return { parts, rest };
|
|
189
234
|
}
|
|
190
235
|
|
|
191
236
|
// Read the upstream body to memory. Write to Codex only after a terminal
|
|
@@ -194,6 +239,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
|
|
|
194
239
|
const reader = stream.getReader();
|
|
195
240
|
const decoder = new TextDecoder();
|
|
196
241
|
let buffer = "";
|
|
242
|
+
let bufferedBytes = 0;
|
|
197
243
|
const rewrite = createSseRewriter(map);
|
|
198
244
|
const pending = [];
|
|
199
245
|
let clientBytes = 0;
|
|
@@ -205,6 +251,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
|
|
|
205
251
|
const rewritten = rewrite(part);
|
|
206
252
|
if (rewritten === null) continue;
|
|
207
253
|
const block = `${rewritten}\n\n`;
|
|
254
|
+
noteUpstreamFailure(usageBox, rewritten);
|
|
208
255
|
if (output.destroyed || output.writableEnded) throw clientClosed();
|
|
209
256
|
// Count the byte as soon as the client stream accepts it. A later drain
|
|
210
257
|
// error must not look like "Codex saw nothing" and resubmit the prompt.
|
|
@@ -238,6 +285,7 @@ export async function pipeProxySse(stream, output, map, usageBox, onClientByte)
|
|
|
238
285
|
if (output.destroyed || output.writableEnded) throw clientClosed();
|
|
239
286
|
const { done, value } = await reader.read();
|
|
240
287
|
if (done) break;
|
|
288
|
+
bufferedBytes = noteSseBytes(bufferedBytes, value?.byteLength ?? value?.length ?? 0);
|
|
241
289
|
buffer += decoder.decode(value, { stream: true });
|
|
242
290
|
const split = completeBlocks(buffer);
|
|
243
291
|
buffer = split.rest;
|
|
@@ -301,6 +349,7 @@ export async function readUntilTerminal(stream, output) {
|
|
|
301
349
|
const reader = stream.getReader();
|
|
302
350
|
const decoder = new TextDecoder();
|
|
303
351
|
let buffer = "";
|
|
352
|
+
let bufferedBytes = 0;
|
|
304
353
|
const collected = [];
|
|
305
354
|
let released = false;
|
|
306
355
|
const finish = async (error) => {
|
|
@@ -313,6 +362,7 @@ export async function readUntilTerminal(stream, output) {
|
|
|
313
362
|
if (output?.destroyed || output?.writableEnded) throw clientClosed();
|
|
314
363
|
const { done, value } = await reader.read();
|
|
315
364
|
if (done) break;
|
|
365
|
+
bufferedBytes = noteSseBytes(bufferedBytes, value?.byteLength ?? value?.length ?? 0);
|
|
316
366
|
buffer += decoder.decode(value, { stream: true });
|
|
317
367
|
const split = completeBlocks(buffer);
|
|
318
368
|
buffer = split.rest;
|
|
@@ -356,6 +406,7 @@ export async function emitProxySse(parts, output, map, usageBox, onClientByte) {
|
|
|
356
406
|
const rewritten = rewrite(part);
|
|
357
407
|
if (rewritten === null) continue;
|
|
358
408
|
const block = `${rewritten}\n\n`;
|
|
409
|
+
noteUpstreamFailure(usageBox, rewritten);
|
|
359
410
|
if (output.destroyed || output.writableEnded) throw clientClosed();
|
|
360
411
|
const accepted = output.write(block);
|
|
361
412
|
clientBytes += Buffer.byteLength(block);
|