codex-grok-bridge 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +160 -30
- package/package.json +5 -1
- package/src/bridge.mjs +204 -8
- package/src/imagine.mjs +184 -0
- package/src/proxy.mjs +87 -2
- package/src/realtime.mjs +212 -0
- package/src/runtime.mjs +3 -0
- package/src/tools.mjs +445 -43
- package/src/videogen.mjs +345 -0
- package/src/voice.mjs +1359 -0
package/src/realtime.mjs
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
2
|
+
import tls from "node:tls";
|
|
3
|
+
import { GrokAuthError, readGrokBearerToken } from "./auth.mjs";
|
|
4
|
+
import { acceptLocalSideband, isLocalVoiceSideband } from "./voice.mjs";
|
|
5
|
+
|
|
6
|
+
// Codex posts the WebRTC SDP offer to the provider base
|
|
7
|
+
// (`POST /v1/realtime/calls`, and v3 `POST /v1/live`). The body is raw bytes
|
|
8
|
+
// (Content-Type application/sdp, or whatever Codex sent). xAI answers with
|
|
9
|
+
// the SDP body, its status, and a Location header. This is a byte pipe:
|
|
10
|
+
// nothing here parses JSON or writes an SDP answer.
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_REALTIME_API_BASE = "https://api.x.ai/v1";
|
|
13
|
+
|
|
14
|
+
export async function forwardRealtime(options) {
|
|
15
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
16
|
+
const base = (options.baseUrl ?? DEFAULT_REALTIME_API_BASE).replace(/\/$/, "");
|
|
17
|
+
const headers = { authorization: `Bearer ${options.token}` };
|
|
18
|
+
if (typeof options.contentType === "string") headers["content-type"] = options.contentType;
|
|
19
|
+
const response = await fetchImpl(`${base}${options.path}${options.search ?? ""}`, {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers,
|
|
22
|
+
body: options.body,
|
|
23
|
+
redirect: "manual",
|
|
24
|
+
signal: options.signal,
|
|
25
|
+
});
|
|
26
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
27
|
+
return {
|
|
28
|
+
status: response.status,
|
|
29
|
+
body,
|
|
30
|
+
contentType: response.headers.get("content-type"),
|
|
31
|
+
location: response.headers.get("location"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function connectSideband() {
|
|
36
|
+
return tls.connect({ host: "api.x.ai", port: 443, servername: "api.x.ai" });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isSidebandUpgradePath(url) {
|
|
40
|
+
let pathname = "/";
|
|
41
|
+
try {
|
|
42
|
+
pathname = new URL(url ?? "/", "http://127.0.0.1").pathname;
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return (
|
|
47
|
+
pathname === "/v1/realtime" ||
|
|
48
|
+
pathname.startsWith("/v1/realtime/") ||
|
|
49
|
+
pathname === "/v1/live" ||
|
|
50
|
+
pathname.startsWith("/v1/live/")
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function headerText(value) {
|
|
55
|
+
const text = Array.isArray(value) ? value.join(", ") : String(value ?? "");
|
|
56
|
+
if (!text || /[\r\n]/.test(text)) return null;
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function bearerMatches(header, token) {
|
|
61
|
+
const expected = Buffer.from(`Bearer ${token}`);
|
|
62
|
+
const actual = Buffer.from(header ?? "");
|
|
63
|
+
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function upgradeRequest(url, headers, token) {
|
|
67
|
+
const lines = [
|
|
68
|
+
`GET ${url} HTTP/1.1`,
|
|
69
|
+
"Host: api.x.ai",
|
|
70
|
+
"Upgrade: websocket",
|
|
71
|
+
"Connection: Upgrade",
|
|
72
|
+
`Sec-WebSocket-Key: ${headers.key}`,
|
|
73
|
+
`Sec-WebSocket-Version: ${headers.version}`,
|
|
74
|
+
`Authorization: Bearer ${token}`,
|
|
75
|
+
];
|
|
76
|
+
if (headers.protocol) lines.push(`Sec-WebSocket-Protocol: ${headers.protocol}`);
|
|
77
|
+
if (headers.extensions) lines.push(`Sec-WebSocket-Extensions: ${headers.extensions}`);
|
|
78
|
+
lines.push("", "");
|
|
79
|
+
return lines.join("\r\n");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function readHead(socket) {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
let buf = Buffer.alloc(0);
|
|
85
|
+
const cleanup = () => {
|
|
86
|
+
socket.off("readable", onReadable);
|
|
87
|
+
socket.off("error", onError);
|
|
88
|
+
socket.off("close", onClose);
|
|
89
|
+
};
|
|
90
|
+
let reading = false;
|
|
91
|
+
const onReadable = () => {
|
|
92
|
+
if (reading) return;
|
|
93
|
+
reading = true;
|
|
94
|
+
let chunk = socket.read();
|
|
95
|
+
while (chunk) {
|
|
96
|
+
buf = Buffer.concat([buf, chunk]);
|
|
97
|
+
chunk = socket.read();
|
|
98
|
+
}
|
|
99
|
+
reading = false;
|
|
100
|
+
const end = buf.indexOf("\r\n\r\n");
|
|
101
|
+
if (end === -1) {
|
|
102
|
+
if (buf.length > 65536) {
|
|
103
|
+
cleanup();
|
|
104
|
+
reject(new Error("header too large"));
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
cleanup();
|
|
109
|
+
resolve({ head: buf.subarray(0, end + 4), rest: buf.subarray(end + 4) });
|
|
110
|
+
};
|
|
111
|
+
const onError = (error) => {
|
|
112
|
+
cleanup();
|
|
113
|
+
reject(error);
|
|
114
|
+
};
|
|
115
|
+
const onClose = () => {
|
|
116
|
+
cleanup();
|
|
117
|
+
reject(new Error("closed"));
|
|
118
|
+
};
|
|
119
|
+
socket.on("readable", onReadable);
|
|
120
|
+
socket.on("error", onError);
|
|
121
|
+
socket.on("close", onClose);
|
|
122
|
+
onReadable();
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function switching(head) {
|
|
127
|
+
const line = head.toString("latin1").split("\r\n", 1)[0] ?? "";
|
|
128
|
+
return /^HTTP\/1\.[01] 101\b/.test(line);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function fail(socket, upstream) {
|
|
132
|
+
socket.destroy();
|
|
133
|
+
upstream?.destroy();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function attachSidebandUpgrade(server, options = {}) {
|
|
137
|
+
server.on("upgrade", (req, socket, head) => {
|
|
138
|
+
pipeSidebandUpgrade(req, socket, head, options).catch(() => fail(socket));
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function pipeSidebandUpgrade(req, socket, head, options) {
|
|
143
|
+
const url = String(req.url ?? "/");
|
|
144
|
+
if (!isSidebandUpgradePath(url) || req.headers.origin) return fail(socket);
|
|
145
|
+
if (!bearerMatches(req.headers.authorization, options.token)) return fail(socket);
|
|
146
|
+
const key = headerText(req.headers["sec-websocket-key"]);
|
|
147
|
+
if (isLocalVoiceSideband(url)) {
|
|
148
|
+
if (!key) return fail(socket);
|
|
149
|
+
acceptLocalSideband(socket, key, url, head);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const version =
|
|
153
|
+
req.headers["sec-websocket-version"] == null
|
|
154
|
+
? "13"
|
|
155
|
+
: headerText(req.headers["sec-websocket-version"]);
|
|
156
|
+
if (!key || !version) return fail(socket);
|
|
157
|
+
const protocol = headerText(req.headers["sec-websocket-protocol"]);
|
|
158
|
+
const extensions = headerText(req.headers["sec-websocket-extensions"]);
|
|
159
|
+
if (
|
|
160
|
+
(req.headers["sec-websocket-protocol"] && !protocol) ||
|
|
161
|
+
(req.headers["sec-websocket-extensions"] && !extensions)
|
|
162
|
+
) {
|
|
163
|
+
return fail(socket);
|
|
164
|
+
}
|
|
165
|
+
let token;
|
|
166
|
+
try {
|
|
167
|
+
token = (options.grokSession ?? readGrokBearerToken(options.grokHome)).token;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error instanceof GrokAuthError) return fail(socket);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
const connect = options.sidebandConnect ?? connectSideband;
|
|
173
|
+
const upstream = connect();
|
|
174
|
+
if (!upstream) return fail(socket);
|
|
175
|
+
socket.on("error", () => upstream.destroy());
|
|
176
|
+
upstream.on("error", () => socket.destroy());
|
|
177
|
+
let settled = false;
|
|
178
|
+
const timer = setTimeout(() => {
|
|
179
|
+
if (settled) return;
|
|
180
|
+
settled = true;
|
|
181
|
+
fail(socket, upstream);
|
|
182
|
+
}, options.sidebandHandshakeMs ?? 10_000);
|
|
183
|
+
const finishHandshake = () => {
|
|
184
|
+
if (settled) return false;
|
|
185
|
+
settled = true;
|
|
186
|
+
clearTimeout(timer);
|
|
187
|
+
return true;
|
|
188
|
+
};
|
|
189
|
+
try {
|
|
190
|
+
socket.pause();
|
|
191
|
+
upstream.write(
|
|
192
|
+
upgradeRequest(url, { key, version, protocol, extensions }, token),
|
|
193
|
+
);
|
|
194
|
+
if (head?.length) upstream.write(head);
|
|
195
|
+
const response = await readHead(upstream);
|
|
196
|
+
if (!finishHandshake()) return;
|
|
197
|
+
if (!switching(response.head)) return fail(socket, upstream);
|
|
198
|
+
socket.write(response.head);
|
|
199
|
+
if (response.rest.length) socket.write(response.rest);
|
|
200
|
+
socket.resume();
|
|
201
|
+
upstream.pipe(socket);
|
|
202
|
+
socket.pipe(upstream);
|
|
203
|
+
socket.on("close", () => upstream.destroy());
|
|
204
|
+
upstream.on("close", () => socket.destroy());
|
|
205
|
+
} catch {
|
|
206
|
+
if (!settled) {
|
|
207
|
+
settled = true;
|
|
208
|
+
clearTimeout(timer);
|
|
209
|
+
}
|
|
210
|
+
fail(socket, upstream);
|
|
211
|
+
}
|
|
212
|
+
}
|
package/src/runtime.mjs
CHANGED
|
@@ -43,6 +43,9 @@ export async function startRuntime(options = {}) {
|
|
|
43
43
|
"-c",
|
|
44
44
|
`model_providers.grok_build_cli.${key}=${tomlValue(value)}`,
|
|
45
45
|
]);
|
|
46
|
+
// Desktop WebRTC reads this for the sideband. Unset, Codex keeps
|
|
47
|
+
// https://api.openai.com/v1. The value is this bridge, not xAI.
|
|
48
|
+
args.push("-c", `experimental_realtime_ws_base_url=${tomlValue(provider.base_url)}`);
|
|
46
49
|
return {
|
|
47
50
|
server,
|
|
48
51
|
token,
|