codex-grok-bridge 1.8.4 → 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 +11 -0
- package/README.md +56 -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
package/src/rollout.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { lstat, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { isDeepStrictEqual } from "node:util";
|
|
@@ -19,9 +20,37 @@ function recordedProvider(record) {
|
|
|
19
20
|
const modelProvider = typeof record.model_provider === "string" ? record.model_provider : "";
|
|
20
21
|
const model = typeof record.model === "string" ? record.model : "";
|
|
21
22
|
if (modelProvider === "grok_build_cli" || model.startsWith("grok-")) return "grok_build_cli";
|
|
23
|
+
if (model && !model.startsWith("grok-")) return modelProvider || "non_grok";
|
|
22
24
|
return modelProvider || null;
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
// Codex 0.166+ resume checks the first session_meta provider. A model-only
|
|
28
|
+
// line is not enough to leave Grok, so a later GPT line names a non-Grok provider.
|
|
29
|
+
export function rewriteRolloutProviderText(text, provider, model) {
|
|
30
|
+
if (typeof text !== "string" || !provider) return text;
|
|
31
|
+
const lines = text.split("\n");
|
|
32
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
33
|
+
if (!lines[i]) continue;
|
|
34
|
+
let value;
|
|
35
|
+
try {
|
|
36
|
+
value = JSON.parse(lines[i]);
|
|
37
|
+
} catch {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (value?.type !== "session_meta" || !value.payload || typeof value.payload !== "object")
|
|
41
|
+
continue;
|
|
42
|
+
const sameProvider = value.payload.model_provider === provider;
|
|
43
|
+
const sameModel = model == null || value.payload.model === model;
|
|
44
|
+
if (sameProvider && sameModel) return text;
|
|
45
|
+
const copy = structuredClone(value);
|
|
46
|
+
copy.payload.model_provider = provider;
|
|
47
|
+
if (model != null) copy.payload.model = model;
|
|
48
|
+
lines[i] = JSON.stringify(copy);
|
|
49
|
+
return lines.join("\n");
|
|
50
|
+
}
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
|
|
25
54
|
// session_meta sets the provider from model_provider. A later line that names
|
|
26
55
|
// grok_build_cli, or a model starting with grok-, switches to Grok. Anything
|
|
27
56
|
// else, including unknown, is not Grok.
|
|
@@ -73,17 +102,19 @@ export function rewriteRolloutText(text) {
|
|
|
73
102
|
|
|
74
103
|
export async function migrateRolloutFile(file) {
|
|
75
104
|
if (typeof file !== "string" || !file || isSkippedHistoryPath(file)) return false;
|
|
76
|
-
let
|
|
105
|
+
let info;
|
|
77
106
|
try {
|
|
78
|
-
|
|
107
|
+
info = await lstat(file);
|
|
79
108
|
} catch (error) {
|
|
80
109
|
if (error.code === "ENOENT") return false;
|
|
81
110
|
throw error;
|
|
82
111
|
}
|
|
112
|
+
if (!info.isFile()) return false;
|
|
113
|
+
const first = await readFile(file);
|
|
83
114
|
const next = rewriteRolloutText(first.toString("utf8"));
|
|
84
115
|
if (next === first.toString("utf8")) return false;
|
|
85
|
-
const mode =
|
|
86
|
-
const tmp = `${file}.${process.pid}.tmp`;
|
|
116
|
+
const mode = info.mode & 0o777;
|
|
117
|
+
const tmp = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
87
118
|
await writeFile(tmp, next, { mode });
|
|
88
119
|
const again = await readFile(file);
|
|
89
120
|
if (!again.equals(first)) {
|
package/src/router.mjs
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from "node:util";
|
|
2
2
|
import { catalogModelEntries, isGrokModel, MODEL_ENTRY } from "./models.mjs";
|
|
3
|
+
import { alignConfiguredProvider, rewriteSavedModelProvider } from "./provider-align.mjs";
|
|
3
4
|
|
|
4
5
|
export { MODEL_ENTRY };
|
|
5
6
|
|
|
7
|
+
// A non-grok model on a Grok thread is an OpenAI model even when the slug
|
|
8
|
+
// does not start with gpt- (o3, o4-mini, codex-*).
|
|
9
|
+
export function targetProvider(model, snapshotProvider) {
|
|
10
|
+
if (isGrokModel(model)) return "grok_build_cli";
|
|
11
|
+
if (
|
|
12
|
+
snapshotProvider === "grok_build_cli" &&
|
|
13
|
+
typeof model === "string" &&
|
|
14
|
+
model.length > 0
|
|
15
|
+
)
|
|
16
|
+
return "openai";
|
|
17
|
+
return snapshotProvider;
|
|
18
|
+
}
|
|
19
|
+
|
|
6
20
|
export class Router {
|
|
7
|
-
constructor(catalogPath) {
|
|
21
|
+
constructor(catalogPath, options = {}) {
|
|
8
22
|
this.catalogPath = catalogPath;
|
|
23
|
+
this.codexHome = options.codexHome ?? null;
|
|
9
24
|
this.pending = new Map();
|
|
10
|
-
this.threads = new Map();
|
|
11
25
|
this.unstarted = new Map();
|
|
26
|
+
this.alignProvider = options.alignProvider ?? ((provider) => {
|
|
27
|
+
if (this.codexHome) alignConfiguredProvider(this.codexHome, provider);
|
|
28
|
+
});
|
|
29
|
+
this.rewriteSaved = options.rewriteSaved ?? ((threadId, provider, snapshot, model) => {
|
|
30
|
+
if (!this.codexHome) return false;
|
|
31
|
+
return rewriteSavedModelProvider({
|
|
32
|
+
codexHome: this.codexHome,
|
|
33
|
+
threadId,
|
|
34
|
+
rolloutPath: snapshot?.path,
|
|
35
|
+
provider,
|
|
36
|
+
model,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
12
39
|
}
|
|
13
40
|
applyGrokProvider(params) {
|
|
14
41
|
params.modelProvider = "grok_build_cli";
|
|
@@ -27,15 +54,21 @@ export class Router {
|
|
|
27
54
|
const fresh = this.unstarted.get(p.threadId);
|
|
28
55
|
const snapshot = fresh ?? await rpc("thread/resume", { threadId: p.threadId, excludeTurns: true });
|
|
29
56
|
const model = selected ?? snapshot.model;
|
|
30
|
-
const provider =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
57
|
+
const provider = targetProvider(model, snapshot.modelProvider);
|
|
58
|
+
if (provider === snapshot.modelProvider) {
|
|
59
|
+
await this.alignProvider(provider);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
35
62
|
if (fresh) throw new Error("Save the first turn before switching providers, or start a new thread with the desired model.");
|
|
36
63
|
if (snapshot.thread.status?.type !== "idle" || snapshot.thread.ephemeral || snapshot.thread.parentThreadId)
|
|
37
64
|
throw new Error("Model provider switching requires an idle, saved root thread. Finish the active turn before switching.");
|
|
38
65
|
await rpc("thread/unsubscribe", { threadId: p.threadId });
|
|
66
|
+
await this.rewriteSaved(p.threadId, provider, snapshot, model);
|
|
67
|
+
await this.alignProvider(provider);
|
|
68
|
+
// Codex 0.166 keeps the loaded thread's provider even after unsubscribe.
|
|
69
|
+
// Archive drops that copy; unarchive reads the provider we just saved.
|
|
70
|
+
await rpc("thread/archive", { threadId: p.threadId });
|
|
71
|
+
await rpc("thread/unarchive", { threadId: p.threadId });
|
|
39
72
|
const resumed = await rpc("thread/resume", {
|
|
40
73
|
threadId: p.threadId, model, modelProvider: provider, excludeTurns: true,
|
|
41
74
|
cwd: snapshot.cwd, approvalPolicy: snapshot.approvalPolicy,
|
|
@@ -63,7 +96,6 @@ export class Router {
|
|
|
63
96
|
grok
|
|
64
97
|
) {
|
|
65
98
|
msg.params = this.applyGrokProvider(p);
|
|
66
|
-
if (p.threadId) this.threads.set(p.threadId, "grok_build_cli");
|
|
67
99
|
}
|
|
68
100
|
if (msg.id !== undefined && msg.method)
|
|
69
101
|
this.pending.set(msg.id, { method: msg.method, params: msg.params ?? p });
|
|
@@ -83,20 +115,6 @@ export class Router {
|
|
|
83
115
|
msg.result.data.push(entry);
|
|
84
116
|
}
|
|
85
117
|
}
|
|
86
|
-
if (
|
|
87
|
-
["thread/start", "thread/resume", "thread/fork"].includes(
|
|
88
|
-
request.method,
|
|
89
|
-
) &&
|
|
90
|
-
msg.result?.thread?.id
|
|
91
|
-
) {
|
|
92
|
-
this.threads.set(
|
|
93
|
-
msg.result.thread.id,
|
|
94
|
-
msg.result.modelProvider ??
|
|
95
|
-
msg.result.thread.modelProvider ??
|
|
96
|
-
request.params.modelProvider ??
|
|
97
|
-
"openai",
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
118
|
return msg;
|
|
101
119
|
}
|
|
102
120
|
}
|
package/src/runtime.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import path from "node:path";
|
|
|
5
5
|
import { once } from "node:events";
|
|
6
6
|
import { createBridgeServer } from "./bridge.mjs";
|
|
7
7
|
import { catalogModelInfos } from "./models.mjs";
|
|
8
|
+
import { closeVoiceCalls } from "./voice.mjs";
|
|
8
9
|
|
|
9
10
|
export async function startRuntime(options = {}) {
|
|
10
11
|
const token = randomBytes(32).toString("hex");
|
|
@@ -52,6 +53,7 @@ export async function startRuntime(options = {}) {
|
|
|
52
53
|
catalogPath,
|
|
53
54
|
args,
|
|
54
55
|
async close() {
|
|
56
|
+
closeVoiceCalls();
|
|
55
57
|
server.closeAllConnections();
|
|
56
58
|
await new Promise((resolve) => server.close(resolve));
|
|
57
59
|
await rm(dir, { recursive: true, force: true });
|
package/src/tools.mjs
CHANGED
|
@@ -197,12 +197,11 @@ function readPointer(root, tokens) {
|
|
|
197
197
|
|
|
198
198
|
function inlineLocalSchemaRefs(schema) {
|
|
199
199
|
function resolve(ref, trail) {
|
|
200
|
+
if (typeof ref !== "string" || trail.includes(ref)) return {};
|
|
200
201
|
const tokens = definitionPointer(ref);
|
|
201
|
-
if (!tokens
|
|
202
|
-
throw new Error(`Unresolvable schema $ref: ${ref}`);
|
|
202
|
+
if (!tokens) return {};
|
|
203
203
|
const target = readPointer(schema, tokens);
|
|
204
|
-
if (target === undefined)
|
|
205
|
-
throw new Error(`Unresolvable schema $ref: ${ref}`);
|
|
204
|
+
if (target === undefined) return {};
|
|
206
205
|
return walk(structuredClone(target), trail.concat(ref));
|
|
207
206
|
}
|
|
208
207
|
|
|
@@ -210,8 +209,6 @@ function inlineLocalSchemaRefs(schema) {
|
|
|
210
209
|
if (Array.isArray(node)) return node.map((item) => walk(item, trail));
|
|
211
210
|
if (!plainObject(node)) return node;
|
|
212
211
|
if (Object.prototype.hasOwnProperty.call(node, "$ref")) {
|
|
213
|
-
if (typeof node.$ref !== "string")
|
|
214
|
-
throw new Error("Unresolvable schema $ref");
|
|
215
212
|
const resolved = resolve(node.$ref, trail);
|
|
216
213
|
const rest = {};
|
|
217
214
|
for (const [key, value] of Object.entries(node)) {
|
|
@@ -241,6 +238,14 @@ function emitsObjectParameters(schema) {
|
|
|
241
238
|
}
|
|
242
239
|
|
|
243
240
|
function usableParameters(parameters) {
|
|
241
|
+
try {
|
|
242
|
+
return parametersForTool(parameters);
|
|
243
|
+
} catch {
|
|
244
|
+
return OBJECT_SCHEMA;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parametersForTool(parameters) {
|
|
244
249
|
if (!parameters || typeof parameters !== "object" || Array.isArray(parameters))
|
|
245
250
|
return OBJECT_SCHEMA;
|
|
246
251
|
parameters = inlineLocalSchemaRefs(parameters);
|
package/src/transport.mjs
CHANGED
|
@@ -18,11 +18,36 @@ export const MAX_SOCKETS = 4;
|
|
|
18
18
|
export const SOCKET_TIMEOUT_MS = 120_000;
|
|
19
19
|
|
|
20
20
|
const dnsCache = new Map();
|
|
21
|
+
let lookupImpl = dns.lookup;
|
|
21
22
|
|
|
22
23
|
export function clearDnsCache() {
|
|
23
24
|
dnsCache.clear();
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
export function expireDnsCache() {
|
|
28
|
+
for (const entry of dnsCache.values()) entry.expiresAt = 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Tests replace dns.lookup so a cached name can fail on the next query.
|
|
32
|
+
export function setLookupForTests(fn) {
|
|
33
|
+
lookupImpl = fn ?? dns.lookup;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function lookupKey(hostname, settings) {
|
|
37
|
+
return [
|
|
38
|
+
hostname,
|
|
39
|
+
settings.family ?? 0,
|
|
40
|
+
settings.all ? 1 : 0,
|
|
41
|
+
settings.hints ?? 0,
|
|
42
|
+
settings.verbatim ? 1 : 0,
|
|
43
|
+
].join(":");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function answerLookup(resolve, settings, address, family) {
|
|
47
|
+
if (settings.all) resolve(null, address);
|
|
48
|
+
else resolve(null, address, family);
|
|
49
|
+
}
|
|
50
|
+
|
|
26
51
|
/**
|
|
27
52
|
* dns.lookup with a short-lived cache, in the shape http.Agent expects.
|
|
28
53
|
* A failed lookup falls back to a still-known address rather than failing the
|
|
@@ -31,20 +56,20 @@ export function clearDnsCache() {
|
|
|
31
56
|
export function cachedLookup(hostname, options, callback) {
|
|
32
57
|
const resolve = typeof options === "function" ? options : callback;
|
|
33
58
|
const settings = typeof options === "function" ? {} : (options ?? {});
|
|
34
|
-
const key =
|
|
59
|
+
const key = lookupKey(hostname, settings);
|
|
35
60
|
const cached = dnsCache.get(key);
|
|
36
61
|
if (cached && cached.expiresAt > Date.now()) {
|
|
37
|
-
process.nextTick(() => resolve
|
|
62
|
+
process.nextTick(() => answerLookup(resolve, settings, cached.address, cached.family));
|
|
38
63
|
return;
|
|
39
64
|
}
|
|
40
|
-
|
|
65
|
+
lookupImpl(hostname, settings, (error, address, family) => {
|
|
41
66
|
if (!error) {
|
|
42
67
|
dnsCache.set(key, { address, family, expiresAt: Date.now() + DNS_TTL_MS });
|
|
43
|
-
resolve
|
|
68
|
+
answerLookup(resolve, settings, address, family);
|
|
44
69
|
return;
|
|
45
70
|
}
|
|
46
71
|
if (cached) {
|
|
47
|
-
resolve
|
|
72
|
+
answerLookup(resolve, settings, cached.address, cached.family);
|
|
48
73
|
return;
|
|
49
74
|
}
|
|
50
75
|
resolve(error);
|
|
@@ -102,6 +127,13 @@ export function requestStream(url, init = {}) {
|
|
|
102
127
|
);
|
|
103
128
|
},
|
|
104
129
|
);
|
|
130
|
+
const timeoutMs = init.timeout ?? SOCKET_TIMEOUT_MS;
|
|
131
|
+
const timedOut = () =>
|
|
132
|
+
request.destroy(
|
|
133
|
+
Object.assign(new Error("Upstream socket timed out"), { code: "ETIMEDOUT" }),
|
|
134
|
+
);
|
|
135
|
+
request.setTimeout(timeoutMs, timedOut);
|
|
136
|
+
request.once("timeout", timedOut);
|
|
105
137
|
request.once("error", reject);
|
|
106
138
|
if (init.body === undefined || init.body === null) request.end();
|
|
107
139
|
else request.end(init.body);
|
package/src/videogen.mjs
CHANGED
|
@@ -3,6 +3,17 @@ import { homedir } from "node:os";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
|
|
6
|
+
function stripCiphertext(value) {
|
|
7
|
+
if (!value || typeof value !== "object") return;
|
|
8
|
+
if (Array.isArray(value)) {
|
|
9
|
+
for (const item of value) stripCiphertext(item);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
delete value.encrypted_content;
|
|
13
|
+
delete value.encrypted_function_args;
|
|
14
|
+
for (const child of Object.values(value)) stripCiphertext(child);
|
|
15
|
+
}
|
|
16
|
+
|
|
6
17
|
// cli-chat-proxy has no Responses `video_generation` tool. grok-build generates
|
|
7
18
|
// video by calling the videos API itself when the model invokes a function
|
|
8
19
|
// (`image_to_video` / `reference_to_video`): POST {xai_api_base}/videos/generations,
|
|
@@ -24,6 +35,21 @@ const VIDEO_MODEL = "grok-imagine-video-1.5";
|
|
|
24
35
|
const POLL_LIMIT = 60;
|
|
25
36
|
const MAX_VIDEO_BYTES = 80 * 1024 * 1024;
|
|
26
37
|
const MAX_VIDEO_REDIRECTS = 3;
|
|
38
|
+
const VIDEO_REQUEST_TIMEOUT_MS = 300_000;
|
|
39
|
+
const USAGE_KEYS = [
|
|
40
|
+
"input_tokens",
|
|
41
|
+
"output_tokens",
|
|
42
|
+
"total_tokens",
|
|
43
|
+
"cached_prompt_tokens",
|
|
44
|
+
"cache_read_input_tokens",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
function combineSignal(signal, ms = VIDEO_REQUEST_TIMEOUT_MS) {
|
|
48
|
+
const timeout = AbortSignal.timeout(ms);
|
|
49
|
+
if (!signal) return timeout;
|
|
50
|
+
if (typeof AbortSignal.any === "function") return AbortSignal.any([signal, timeout]);
|
|
51
|
+
return signal;
|
|
52
|
+
}
|
|
27
53
|
|
|
28
54
|
export const GROK_VIDEO_TOOL = Object.freeze({
|
|
29
55
|
type: "function",
|
|
@@ -124,7 +150,11 @@ function videoPayload(args) {
|
|
|
124
150
|
}
|
|
125
151
|
|
|
126
152
|
async function videoHttp(fetchImpl, url, init) {
|
|
127
|
-
const response = await fetchImpl(url, {
|
|
153
|
+
const response = await fetchImpl(url, {
|
|
154
|
+
...init,
|
|
155
|
+
redirect: "manual",
|
|
156
|
+
signal: combineSignal(init.signal),
|
|
157
|
+
});
|
|
128
158
|
const text = await response.text().catch(() => "");
|
|
129
159
|
if (response.status >= 300 && response.status < 400) {
|
|
130
160
|
const error = new Error("Video generation failed (redirect).");
|
|
@@ -229,7 +259,7 @@ async function downloadGeneratedVideo(url, options, redirects = 0) {
|
|
|
229
259
|
response = await fetchImpl(url, {
|
|
230
260
|
method: "GET",
|
|
231
261
|
redirect: "manual",
|
|
232
|
-
signal: options.signal,
|
|
262
|
+
signal: combineSignal(options.signal),
|
|
233
263
|
});
|
|
234
264
|
} catch {
|
|
235
265
|
return null;
|
|
@@ -311,6 +341,151 @@ export async function videoToolOutput(call, options) {
|
|
|
311
341
|
}
|
|
312
342
|
}
|
|
313
343
|
|
|
344
|
+
function doneItems(parts) {
|
|
345
|
+
const items = [];
|
|
346
|
+
for (const part of parts ?? []) {
|
|
347
|
+
for (const line of String(part).split("\n")) {
|
|
348
|
+
if (!line.startsWith("data:")) continue;
|
|
349
|
+
const payload = line.slice(5).trim();
|
|
350
|
+
if (!payload || payload === "[DONE]") continue;
|
|
351
|
+
let value;
|
|
352
|
+
try {
|
|
353
|
+
value = JSON.parse(payload);
|
|
354
|
+
} catch {
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (value?.type === "response.output_item.done" && value.item) items.push(value.item);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return items;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function retainedInputItems(parts) {
|
|
364
|
+
const items = [];
|
|
365
|
+
for (const item of doneItems(parts)) {
|
|
366
|
+
if (item?.type === "function_call" && item.name === GROK_VIDEO_TOOL_NAME) continue;
|
|
367
|
+
if (item?.type === "function_call") {
|
|
368
|
+
items.push({
|
|
369
|
+
type: "function_call",
|
|
370
|
+
name: item.name,
|
|
371
|
+
call_id: item.call_id,
|
|
372
|
+
arguments:
|
|
373
|
+
typeof item.arguments === "string"
|
|
374
|
+
? item.arguments
|
|
375
|
+
: JSON.stringify(item.arguments ?? {}),
|
|
376
|
+
});
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (item?.type === "reasoning" || item?.type === "message") {
|
|
380
|
+
const copy = structuredClone(item);
|
|
381
|
+
stripCiphertext(copy);
|
|
382
|
+
items.push(copy);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return items;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function stripVideoToolEvents(parts) {
|
|
389
|
+
const kept = [];
|
|
390
|
+
for (const part of parts ?? []) {
|
|
391
|
+
const lines = [];
|
|
392
|
+
let droppedData = false;
|
|
393
|
+
for (const line of String(part).split("\n")) {
|
|
394
|
+
if (!line.startsWith("data:")) {
|
|
395
|
+
lines.push(line);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const payload = line.slice(5).trim();
|
|
399
|
+
if (!payload || payload === "[DONE]") {
|
|
400
|
+
lines.push(line);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
let value;
|
|
404
|
+
try {
|
|
405
|
+
value = JSON.parse(payload);
|
|
406
|
+
} catch {
|
|
407
|
+
lines.push(line);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
const item = value?.item;
|
|
411
|
+
if (item?.type === "function_call" && item.name === GROK_VIDEO_TOOL_NAME) {
|
|
412
|
+
droppedData = true;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (Array.isArray(value?.response?.output)) {
|
|
416
|
+
value.response.output = value.response.output.filter(
|
|
417
|
+
(entry) => !(entry?.type === "function_call" && entry?.name === GROK_VIDEO_TOOL_NAME),
|
|
418
|
+
);
|
|
419
|
+
lines.push(`data: ${JSON.stringify(value)}`);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
lines.push(line);
|
|
423
|
+
}
|
|
424
|
+
if (droppedData && !lines.some((line) => line.startsWith("data:"))) continue;
|
|
425
|
+
const text = lines.join("\n");
|
|
426
|
+
if (text.trim()) kept.push(text);
|
|
427
|
+
}
|
|
428
|
+
return kept;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export function usageFromParts(parts) {
|
|
432
|
+
let found = null;
|
|
433
|
+
for (const part of parts ?? []) {
|
|
434
|
+
for (const line of String(part).split("\n")) {
|
|
435
|
+
if (!line.startsWith("data:")) continue;
|
|
436
|
+
const payload = line.slice(5).trim();
|
|
437
|
+
if (!payload || payload === "[DONE]") continue;
|
|
438
|
+
try {
|
|
439
|
+
const value = JSON.parse(payload);
|
|
440
|
+
if (
|
|
441
|
+
(value?.type === "response.completed" || value?.type === "response.incomplete") &&
|
|
442
|
+
value.response?.usage
|
|
443
|
+
)
|
|
444
|
+
found = value.response.usage;
|
|
445
|
+
} catch {
|
|
446
|
+
// Ignore non-JSON data lines.
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return found;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function addUsage(total, usage) {
|
|
454
|
+
const next = { ...(total ?? {}) };
|
|
455
|
+
if (!usage) return next;
|
|
456
|
+
for (const key of USAGE_KEYS) {
|
|
457
|
+
if (typeof usage[key] === "number") next[key] = (next[key] ?? 0) + usage[key];
|
|
458
|
+
}
|
|
459
|
+
return next;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export function stampUsage(parts, usage) {
|
|
463
|
+
if (!usage || Object.keys(usage).length === 0) return parts;
|
|
464
|
+
return (parts ?? []).map((part) =>
|
|
465
|
+
String(part)
|
|
466
|
+
.split("\n")
|
|
467
|
+
.map((line) => {
|
|
468
|
+
if (!line.startsWith("data:")) return line;
|
|
469
|
+
const payload = line.slice(5).trim();
|
|
470
|
+
if (!payload || payload === "[DONE]") return line;
|
|
471
|
+
try {
|
|
472
|
+
const value = JSON.parse(payload);
|
|
473
|
+
if (
|
|
474
|
+
value?.response &&
|
|
475
|
+
(value.type === "response.completed" || value.type === "response.incomplete")
|
|
476
|
+
) {
|
|
477
|
+
value.response.usage = { ...(value.response.usage ?? {}), ...usage };
|
|
478
|
+
return `data: ${JSON.stringify(value)}`;
|
|
479
|
+
}
|
|
480
|
+
} catch {
|
|
481
|
+
// Keep a non-JSON data line unchanged.
|
|
482
|
+
}
|
|
483
|
+
return line;
|
|
484
|
+
})
|
|
485
|
+
.join("\n"),
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
|
|
314
489
|
export function videoCallsFromParts(parts) {
|
|
315
490
|
const calls = [];
|
|
316
491
|
const seen = new Set();
|
package/src/voice.mjs
CHANGED
|
@@ -260,6 +260,10 @@ export function rememberLocalCall(id = randomUUID()) {
|
|
|
260
260
|
return id;
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
+
export function forgetLocalCall(id) {
|
|
264
|
+
localCalls.delete(id);
|
|
265
|
+
}
|
|
266
|
+
|
|
263
267
|
function callIdIn(url) {
|
|
264
268
|
const text = String(url ?? "");
|
|
265
269
|
for (const id of localCalls) {
|
|
@@ -276,7 +280,14 @@ export function encodeServerFrame(opcode, payload) {
|
|
|
276
280
|
const data = Buffer.isBuffer(payload) ? payload : Buffer.from(String(payload));
|
|
277
281
|
let header;
|
|
278
282
|
if (data.length < 126) header = Buffer.from([0x80 | opcode, data.length]);
|
|
279
|
-
else
|
|
283
|
+
else if (data.length < 65536)
|
|
284
|
+
header = Buffer.from([0x80 | opcode, 126, data.length >> 8, data.length & 0xff]);
|
|
285
|
+
else {
|
|
286
|
+
header = Buffer.alloc(10);
|
|
287
|
+
header[0] = 0x80 | opcode;
|
|
288
|
+
header[1] = 127;
|
|
289
|
+
header.writeBigUInt64BE(BigInt(data.length), 2);
|
|
290
|
+
}
|
|
280
291
|
return Buffer.concat([header, data]);
|
|
281
292
|
}
|
|
282
293
|
|
|
@@ -1269,7 +1280,10 @@ export async function answerVoiceCall({
|
|
|
1269
1280
|
if (speechTimer) clearTimeout(speechTimer);
|
|
1270
1281
|
voiceState.closing = true;
|
|
1271
1282
|
active.delete(session);
|
|
1272
|
-
if (session.id)
|
|
1283
|
+
if (session.id) {
|
|
1284
|
+
sessionsById.delete(session.id);
|
|
1285
|
+
forgetLocalCall(session.id);
|
|
1286
|
+
}
|
|
1273
1287
|
try {
|
|
1274
1288
|
bridge?.close();
|
|
1275
1289
|
socket?.close();
|