-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-agent.js
More file actions
813 lines (728 loc) · 29.7 KB
/
Copy pathcodex-agent.js
File metadata and controls
813 lines (728 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
"use strict";
const { spawn } = require("node:child_process");
const fs = require("node:fs");
const http = require("node:http");
const os = require("node:os");
const path = require("node:path");
const readline = require("node:readline");
const { WebSocketServer, WebSocket } = require("ws");
const ROOT = fs.realpathSync(__dirname);
const ENV_FILE = path.join(ROOT, ".env");
if (fs.existsSync(ENV_FILE)) {
if (typeof process.loadEnvFile !== "function") {
throw new Error("O Codex Agent requer Node.js 20.12 ou mais recente para carregar o arquivo .env.");
}
process.loadEnvFile(ENV_FILE);
}
const HOST = "127.0.0.1";
const PORT = Number.parseInt(process.env.CODEX_AGENT_PORT || "3091", 10);
const FILES_ROOT = fs.realpathSync(path.join(ROOT, "files"));
const MAX_MESSAGE_BYTES = 128 * 1024;
const MAX_PROMPT_CHARS = 64 * 1024;
const REQUEST_TIMEOUT_MS = 30_000;
const TURN_LIMIT_PER_MINUTE = 10;
const PERMISSION_MODES = Object.freeze({
"read-only": Object.freeze({ approvalPolicy: "on-request", sandbox: "read-only" }),
agent: Object.freeze({ approvalPolicy: "on-request", sandbox: "workspace-write" }),
"agent-full": Object.freeze({ approvalPolicy: "never", sandbox: "danger-full-access" }),
});
let codexProcess = null;
let codexReady = false;
let stopping = false;
let restartAttempt = 0;
let nextRpcId = 1;
const clients = new Set();
const pendingRpc = new Map();
const pendingServerRequests = new Map();
const threadOwners = new Map();
const threadWorkspaces = new Map();
const activeTurns = new Map();
const threadTokenUsage = new Map();
function redact(value) {
return String(value)
.replace(/(CODEX_ACCESS_TOKEN\s*[=:]\s*)\S+/gi, "$1[redacted]")
.replace(/\b(sk-[A-Za-z0-9_-]{12,})\b/g, "[redacted]");
}
function log(message) {
process.stderr.write(`[codex-agent] ${redact(message)}\n`);
}
function findCodexBinary() {
const pathCandidates = (process.env.PATH || "")
.split(path.delimiter)
.filter(Boolean)
.map((directory) => path.join(directory, "codex"));
const candidates = [
process.env.CODEX_BIN,
path.join(ROOT, ".runtime", "codex", "bin", "codex"),
path.join(ROOT, ".runtime", "node", "bin", "codex"),
...pathCandidates,
path.join(os.homedir(), ".local", "bin", "codex"),
"/usr/local/bin/codex",
"/usr/bin/codex",
"/home/lotus/.local/bin/codex"
].filter(Boolean);
for (const candidate of candidates) {
try {
fs.accessSync(candidate, fs.constants.X_OK);
return candidate;
} catch {
// Try the next known installation location.
}
}
throw new Error("Codex CLI não foi encontrado no runtime do File Manager, no PATH nem em ~/.local/bin.");
}
function isInside(base, candidate) {
const relative = path.relative(base, candidate);
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
}
function resolveWorkspace(value) {
const requested = typeof value === "string" ? value.trim() : "";
if (requested.includes("\0")) throw new Error("Diretório de trabalho inválido.");
const normalized = requested.replaceAll("\\", "/");
const relative = normalized.replace(/^\/+/, "");
const relatives = [relative];
if (relative === "files") {
relatives.push("");
} else if (relative.startsWith("files/")) {
relatives.push(relative.slice("files/".length));
}
const candidates = relatives.map((item) => path.resolve(FILES_ROOT, item || "."));
if (relative && relative !== "files" && !relative.startsWith("files/")) {
candidates.push(path.resolve(ROOT, relative));
}
if (path.isAbsolute(normalized)) candidates.push(path.resolve(normalized));
for (const candidate of new Set(candidates)) {
let real;
try {
real = fs.realpathSync(candidate);
} catch {
continue;
}
// The explorer path chooses the cwd for the next turn. It may point to
// another project on the same machine; the selected permission mode
// still defines the sandbox applied to that directory.
if (!fs.statSync(real).isDirectory()) continue;
return real;
}
throw new Error("A pasta selecionada não existe ou não é um diretório acessível.");
}
function resolveEditorFile(workspace, value) {
const requested = typeof value === "string" ? value.trim() : "";
if (!requested || requested.length > 4096 || requested.includes("\0")) {
throw new Error("Referência de arquivo inválida.");
}
const normalized = requested.replaceAll("\\", "/");
const relative = normalized.replace(/^\/+/, "");
const candidates = [];
if (path.isAbsolute(normalized)) candidates.push(path.resolve(normalized));
// publicPayload removes FILES_ROOT before paths reach the browser (for
// example, files/onago/document.md becomes /onago/document.md). Restore
// that public path here before trying paths relative to the workspace.
candidates.push(path.resolve(FILES_ROOT, relative));
candidates.push(path.resolve(workspace, relative));
candidates.push(path.resolve(ROOT, relative));
for (const candidate of new Set(candidates)) {
let real;
try {
real = fs.realpathSync(candidate);
} catch {
continue;
}
// A pasta da conversa define o cwd do agente, mas não limita os links
// que o usuário pode abrir no editor. Para referências, basta o caminho
// existir e apontar para um arquivo real.
if (!fs.statSync(real).isFile()) continue;
return real.replaceAll(path.sep, "/");
}
throw new Error("O arquivo referenciado não existe ou não é um arquivo válido.");
}
function publicPath(value) {
if (typeof value !== "string") return value;
return value.split(FILES_ROOT).join("") || "/";
}
function publicPayload(value) {
if (typeof value === "string") return publicPath(value);
if (Array.isArray(value)) return value.map(publicPayload);
if (!value || typeof value !== "object") return value;
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, publicPayload(item)]));
}
function sendClient(client, payload) {
if (client.readyState !== WebSocket.OPEN) return;
client.send(JSON.stringify(publicPayload(payload)));
}
function broadcastStatus(status, message) {
for (const client of clients) {
sendClient(client, { type: "status", status, message });
}
}
function sendCodex(message) {
if (!codexProcess || !codexProcess.stdin.writable) {
throw new Error("Codex app-server não está disponível.");
}
codexProcess.stdin.write(`${JSON.stringify(message)}\n`);
}
function rpc(method, params = {}, timeoutMs = REQUEST_TIMEOUT_MS) {
if (!codexProcess) return Promise.reject(new Error("Codex app-server não está disponível."));
const id = nextRpcId++;
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
pendingRpc.delete(id);
reject(new Error(`Tempo limite excedido em ${method}.`));
}, timeoutMs);
pendingRpc.set(id, { method, resolve, reject, timer });
try {
sendCodex({ method, id, params });
} catch (error) {
clearTimeout(timer);
pendingRpc.delete(id);
reject(error);
}
});
}
function notification(method, params = {}) {
sendCodex({ method, params });
}
function rejectPending(error) {
for (const pending of pendingRpc.values()) {
clearTimeout(pending.timer);
pending.reject(error);
}
pendingRpc.clear();
pendingServerRequests.clear();
}
function findThreadId(message) {
const params = message && message.params;
return params?.threadId || params?.thread?.id || null;
}
function containsHttpStatus(value, expected) {
if (!value || typeof value !== "object") return false;
if (value.httpStatusCode === expected) return true;
return Object.values(value).some((item) => containsHttpStatus(item, expected));
}
function ownersForThread(threadId) {
if (!threadId) return clients;
return threadOwners.get(threadId) || new Set();
}
function rememberThread(client, thread, workspace) {
const threadId = thread?.id;
if (!threadId) return;
if (!threadOwners.has(threadId)) threadOwners.set(threadId, new Set());
threadOwners.get(threadId).add(client);
threadWorkspaces.set(threadId, workspace);
const active = Array.isArray(thread.turns)
? thread.turns.find((turn) => turn.status === "inProgress")
: null;
if (active?.id) activeTurns.set(threadId, active.id);
}
function responseForServerRequest(record, payload) {
const method = record.method;
if (method === "item/commandExecution/requestApproval" || method === "item/fileChange/requestApproval") {
const allowed = new Set(["accept", "acceptForSession", "decline", "cancel"]);
const decision = allowed.has(payload.decision) ? payload.decision : "decline";
return { decision };
}
if (method === "item/tool/requestUserInput") {
const answers = {};
const source = payload.answers && typeof payload.answers === "object" ? payload.answers : {};
for (const [questionId, answer] of Object.entries(source)) {
const list = Array.isArray(answer) ? answer : [answer];
answers[questionId] = {
answers: list.map((item) => String(item).trim()).filter(Boolean).slice(0, 3),
};
}
return { answers };
}
if (method === "item/permissions/requestApproval") {
if (payload.decision !== "accept") return { permissions: {}, scope: "turn" };
const requested = record.params?.permissions || {};
const workspace = threadWorkspaces.get(record.params?.threadId);
const granted = {};
if (requested.network?.enabled === true) granted.network = { enabled: true };
const reads = Array.isArray(requested.fileSystem?.read)
? requested.fileSystem.read.filter((entry) => workspace && isInside(workspace, path.resolve(entry)))
: [];
const writes = Array.isArray(requested.fileSystem?.write)
? requested.fileSystem.write.filter((entry) => workspace && isInside(workspace, path.resolve(entry)))
: [];
if (reads.length || writes.length) granted.fileSystem = { read: reads, write: writes };
return {
permissions: granted,
scope: payload.scope === "session" ? "session" : "turn",
};
}
if (method === "mcpServer/elicitation/request") {
return { action: "cancel", content: null };
}
throw new Error("Tipo de aprovação não suportado pelo File Manager.");
}
function autoDeclineServerRequest(message) {
const method = message.method;
let result;
if (method === "item/commandExecution/requestApproval" || method === "item/fileChange/requestApproval") {
result = { decision: "cancel" };
} else if (method === "item/tool/requestUserInput") {
result = { answers: {} };
} else if (method === "item/permissions/requestApproval") {
result = { permissions: {}, scope: "turn" };
} else if (method === "mcpServer/elicitation/request") {
result = { action: "cancel", content: null };
} else {
result = { decision: "cancel" };
}
try {
sendCodex({ id: message.id, result });
} catch {
// The app-server is already unavailable.
}
}
function handleCodexMessage(message) {
if (Object.hasOwn(message, "id") && !message.method) {
const pending = pendingRpc.get(message.id);
if (!pending) return;
pendingRpc.delete(message.id);
clearTimeout(pending.timer);
if (message.error) {
const error = new Error(message.error.message || `Falha em ${pending.method}.`);
error.code = message.error.code;
pending.reject(error);
} else {
pending.resolve(message.result);
}
return;
}
if (Object.hasOwn(message, "id") && message.method) {
const threadId = findThreadId(message);
const owners = [...ownersForThread(threadId)].filter((client) => client.readyState === WebSocket.OPEN);
if (!owners.length) {
autoDeclineServerRequest(message);
return;
}
const owner = owners[0];
const timer = setTimeout(() => {
const record = pendingServerRequests.get(message.id);
if (!record) return;
pendingServerRequests.delete(message.id);
autoDeclineServerRequest(message);
}, 10 * 60_000);
pendingServerRequests.set(message.id, {
owner,
method: message.method,
params: message.params || {},
timer,
});
sendClient(owner, {
type: "server.request",
serverRequestId: message.id,
method: message.method,
params: message.params || {},
});
return;
}
if (!message.method) return;
const threadId = findThreadId(message);
if (message.method === "error" && containsHttpStatus(message.params?.error, 401)) {
codexReady = false;
message.params.error.message = "CODEX_ACCESS_TOKEN foi recusado (HTTP 401). Gere ou copie um token Codex válido do workspace e reinicie o serviço.";
broadcastStatus("error", message.params.error.message);
}
if (message.method === "turn/started" && threadId && message.params?.turn?.id) {
activeTurns.set(threadId, message.params.turn.id);
}
if (message.method === "turn/completed" && threadId) {
activeTurns.delete(threadId);
}
if (message.method === "thread/tokenUsage/updated" && threadId && message.params?.tokenUsage) {
threadTokenUsage.set(threadId, message.params.tokenUsage);
}
if (["thread/archived", "thread/deleted"].includes(message.method) && threadId) {
threadTokenUsage.delete(threadId);
}
for (const owner of ownersForThread(threadId)) {
sendClient(owner, { type: "event", method: message.method, params: message.params || {} });
}
}
async function initializeCodex() {
await rpc("initialize", {
clientInfo: { name: "lotus_filemanager", title: "Lotus File Manager", version: "0.1.0" },
capabilities: { experimentalApi: true },
});
notification("initialized", {});
await rpc("account/read", { refreshToken: false });
codexReady = true;
restartAttempt = 0;
broadcastStatus("ready", "Codex Enterprise conectado.");
log("Codex app-server inicializado.");
}
function scheduleCodexRestart() {
if (stopping) return;
const delays = [1_000, 2_000, 5_000, 10_000, 30_000];
const delay = delays[Math.min(restartAttempt++, delays.length - 1)];
broadcastStatus("restarting", `Codex indisponível; nova tentativa em ${Math.ceil(delay / 1000)}s.`);
setTimeout(startCodex, delay);
}
function startCodex() {
if (stopping || codexProcess) return;
if (!process.env.CODEX_ACCESS_TOKEN?.trim()) {
log("CODEX_ACCESS_TOKEN não foi definido.");
broadcastStatus("error", "CODEX_ACCESS_TOKEN não foi definido no servidor.");
scheduleCodexRestart();
return;
}
codexReady = false;
let executable;
try {
executable = findCodexBinary();
} catch (error) {
log(error.message);
broadcastStatus("error", error.message);
scheduleCodexRestart();
return;
}
const managedPaths = [
path.join(ROOT, ".runtime", "node", "bin"),
path.join(ROOT, ".runtime", "codex", "bin"),
process.env.PATH || "",
].filter(Boolean).join(path.delimiter);
codexProcess = spawn(executable, ["app-server", "--listen", "stdio://"], {
cwd: ROOT,
env: {...process.env, PATH: managedPaths},
stdio: ["pipe", "pipe", "pipe"],
});
const current = codexProcess;
const lines = readline.createInterface({ input: current.stdout });
lines.on("line", (line) => {
try {
handleCodexMessage(JSON.parse(line));
} catch (error) {
log(`Mensagem inválida do app-server: ${error.message}`);
}
});
current.stderr.on("data", (chunk) => {
const text = redact(chunk).trim();
if (text) log(text.slice(0, 2000));
});
current.on("error", (error) => log(`Não foi possível iniciar Codex: ${error.message}`));
current.on("exit", (code, signal) => {
if (codexProcess !== current) return;
codexProcess = null;
codexReady = false;
rejectPending(new Error("Codex app-server foi encerrado."));
log(`Codex app-server encerrou (code=${code ?? "null"}, signal=${signal ?? "none"}).`);
scheduleCodexRestart();
});
initializeCodex().catch((error) => {
log(`Falha ao inicializar Codex: ${error.message}`);
broadcastStatus("error", error.message);
current.kill("SIGTERM");
});
}
function requireThread(client, threadId) {
if (typeof threadId !== "string" || !threadOwners.get(threadId)?.has(client)) {
throw new Error("Conversa do Codex inválida para esta conexão.");
}
return threadWorkspaces.get(threadId);
}
function checkTurnRate(client) {
const now = Date.now();
client.turnStarts = (client.turnStarts || []).filter((time) => now - time < 60_000);
if (client.turnStarts.length >= TURN_LIMIT_PER_MINUTE) {
throw new Error("Muitas tarefas iniciadas em pouco tempo. Aguarde um minuto.");
}
client.turnStarts.push(now);
}
function textInput(value) {
const text = typeof value === "string" ? value.trim() : "";
if (!text) throw new Error("Digite uma tarefa para o Codex.");
if (text.length > MAX_PROMPT_CHARS) throw new Error("A tarefa ultrapassa o limite de 64 KiB.");
return [{ type: "text", text }];
}
function permissionMode(message) {
const mode = typeof message.permissionMode === "string" ? message.permissionMode.trim() : "agent";
if (!Object.hasOwn(PERMISSION_MODES, mode)) {
throw new Error("Modo de permissão do Codex inválido.");
}
return mode;
}
function threadPolicy(message) {
const policy = PERMISSION_MODES[permissionMode(message)];
return {
approvalPolicy: policy.approvalPolicy,
sandbox: policy.sandbox,
};
}
function turnPolicy(workspace, message) {
const mode = permissionMode(message);
const policy = PERMISSION_MODES[mode];
let sandboxPolicy;
if (mode === "read-only") {
sandboxPolicy = { type: "readOnly", networkAccess: false };
} else if (mode === "agent-full") {
sandboxPolicy = { type: "dangerFullAccess" };
} else {
sandboxPolicy = {
type: "workspaceWrite",
writableRoots: [workspace],
networkAccess: false,
};
}
return {
cwd: workspace,
approvalPolicy: policy.approvalPolicy,
sandboxPolicy,
};
}
function modelPreferences(message) {
const model = typeof message.model === "string" ? message.model.trim() : "";
const reasoningEffort = typeof message.reasoningEffort === "string"
? message.reasoningEffort.trim().toLowerCase()
: "";
if (model && (!/^[a-z0-9][a-z0-9._:+-]{0,127}$/i.test(model))) {
throw new Error("Modelo do Codex inválido.");
}
if (reasoningEffort && !/^[a-z][a-z0-9_-]{0,31}$/i.test(reasoningEffort)) {
throw new Error("Nível de raciocínio do Codex inválido.");
}
return { model: model || null, reasoningEffort: reasoningEffort || null };
}
function threadPreferenceOverrides(message) {
const preferences = modelPreferences(message);
const overrides = {};
if (preferences.model) overrides.model = preferences.model;
if (preferences.reasoningEffort) {
overrides.config = { model_reasoning_effort: preferences.reasoningEffort };
}
return overrides;
}
async function applyThreadPreferences(threadId, message) {
const preferences = modelPreferences(message);
if (!preferences.model && !preferences.reasoningEffort) return null;
return rpc("thread/settings/update", {
threadId,
model: preferences.model,
effort: preferences.reasoningEffort,
});
}
async function performAction(client, message) {
if (!codexReady && !["health", "file.resolve"].includes(message.action)) {
throw new Error("Codex ainda não está pronto.");
}
const action = message.action;
if (action === "health") {
return { ready: codexReady, tokenConfigured: Boolean(process.env.CODEX_ACCESS_TOKEN?.trim()) };
}
if (action === "model.list") {
return rpc("model/list", { limit: 100, includeHidden: false });
}
if (action === "thread.list") {
return rpc("thread/list", {
limit: 200,
sortKey: "updated_at",
sortDirection: "desc",
// Threads opened through this embedded app-server are persisted as
// `vscode` by current Codex releases. Keep `appServer` too so the
// history remains compatible if Codex starts using that source.
sourceKinds: ["vscode", "appServer"],
});
}
if (action === "thread.start") {
const workspace = resolveWorkspace(message.workspace);
const result = await rpc("thread/start", {
cwd: workspace,
...threadPolicy(message),
serviceName: "lotus_filemanager",
...threadPreferenceOverrides(message),
});
rememberThread(client, result?.thread, workspace);
return result;
}
if (action === "thread.resume") {
const workspace = resolveWorkspace(message.workspace);
const result = await rpc("thread/resume", {
threadId: message.threadId,
cwd: workspace,
...threadPolicy(message),
...threadPreferenceOverrides(message),
});
rememberThread(client, result?.thread, workspace);
return result;
}
if (action === "thread.read") {
requireThread(client, message.threadId);
const result = await rpc("thread/read", { threadId: message.threadId, includeTurns: true });
return { ...result, tokenUsage: threadTokenUsage.get(message.threadId) || null };
}
if (action === "file.resolve") {
const workspace = requireThread(client, message.threadId);
// Codifica o caminho para que publicPayload não o converta em um caminho
// relativo antes de ele chegar ao editor. O navegador o decodifica após
// esta validação de pertencimento ao workspace.
return { editorPath: encodeURIComponent(resolveEditorFile(workspace, message.filePath)) };
}
if (action === "thread.rename") {
requireThread(client, message.threadId);
const name = typeof message.name === "string" ? message.name.trim() : "";
if (!name) throw new Error("Digite um nome para a conversa.");
if (name.length > 120) throw new Error("O nome da conversa deve ter no máximo 120 caracteres.");
await rpc("thread/name/set", { threadId: message.threadId, name });
return { threadId: message.threadId, name };
}
if (action === "thread.archive") {
requireThread(client, message.threadId);
const result = await rpc("thread/archive", { threadId: message.threadId });
threadOwners.delete(message.threadId);
threadWorkspaces.delete(message.threadId);
activeTurns.delete(message.threadId);
threadTokenUsage.delete(message.threadId);
return result;
}
if (action === "turn.start") {
const previousWorkspace = requireThread(client, message.threadId);
const workspace = message.workspace === undefined
? previousWorkspace
: resolveWorkspace(message.workspace);
if (activeTurns.has(message.threadId)) throw new Error("Esta conversa já possui uma tarefa ativa.");
checkTurnRate(client);
threadWorkspaces.set(message.threadId, workspace);
await applyThreadPreferences(message.threadId, message);
const result = await rpc("turn/start", {
threadId: message.threadId,
input: textInput(message.text),
...turnPolicy(workspace, message),
});
if (result?.turn?.id) activeTurns.set(message.threadId, result.turn.id);
return result;
}
if (action === "thread.settings.update") {
requireThread(client, message.threadId);
const result = await applyThreadPreferences(message.threadId, message);
return { applied: Boolean(result), preferences: modelPreferences(message) };
}
if (action === "turn.steer") {
requireThread(client, message.threadId);
const turnId = activeTurns.get(message.threadId);
if (!turnId) throw new Error("Não existe tarefa ativa para orientar.");
return rpc("turn/steer", {
threadId: message.threadId,
expectedTurnId: turnId,
input: textInput(message.text),
});
}
if (action === "turn.interrupt") {
requireThread(client, message.threadId);
const turnId = activeTurns.get(message.threadId);
if (!turnId) throw new Error("Não existe tarefa ativa para interromper.");
return rpc("turn/interrupt", { threadId: message.threadId, turnId });
}
if (action === "server.respond") {
const record = pendingServerRequests.get(message.serverRequestId);
if (!record || record.owner !== client) throw new Error("A solicitação já expirou ou não pertence a esta sessão.");
const result = responseForServerRequest(record, message);
clearTimeout(record.timer);
pendingServerRequests.delete(message.serverRequestId);
sendCodex({ id: message.serverRequestId, result });
return { resolved: true };
}
throw new Error("Ação do Codex não permitida.");
}
const server = http.createServer((request, response) => {
if (request.url === "/healthz" || request.url === "/readyz") {
const readyProbe = request.url === "/readyz";
response.writeHead(readyProbe && !codexReady ? 503 : 200, { "Content-Type": "application/json" });
response.end(JSON.stringify({ running: true, ready: codexReady }));
return;
}
response.writeHead(404).end();
});
const wsServer = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE_BYTES });
server.on("upgrade", (request, socket, head) => {
const remote = request.socket.remoteAddress || "";
const loopback = remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1";
if (request.url !== "/agent" || !loopback) {
socket.write("HTTP/1.1 403 Forbidden\r\n\r\n");
socket.destroy();
return;
}
wsServer.handleUpgrade(request, socket, head, (client) => wsServer.emit("connection", client));
});
wsServer.on("connection", (client) => {
clients.add(client);
client.isAlive = true;
sendClient(client, {
type: "status",
status: codexReady ? "ready" : "starting",
message: codexReady ? "Codex Enterprise conectado." : "Inicializando Codex...",
});
client.on("pong", () => { client.isAlive = true; });
client.on("error", (error) => {
log(`Conexão WebSocket inválida encerrada: ${error.message}`);
if (client.readyState !== WebSocket.CLOSED) client.terminate();
});
client.on("message", async (raw) => {
let message;
try {
if (raw.length > MAX_MESSAGE_BYTES) throw new Error("Mensagem muito grande.");
message = JSON.parse(raw.toString("utf8"));
if (!message || typeof message !== "object" || typeof message.action !== "string") {
throw new Error("Mensagem inválida.");
}
const data = await performAction(client, message);
sendClient(client, {
type: "response",
requestId: message.requestId || null,
action: message.action,
ok: true,
data,
});
} catch (error) {
sendClient(client, {
type: "response",
requestId: message?.requestId || null,
action: message?.action || null,
ok: false,
error: error.message || "Falha ao processar a solicitação.",
});
}
});
client.on("close", () => {
clients.delete(client);
for (const owners of threadOwners.values()) owners.delete(client);
for (const [id, record] of pendingServerRequests.entries()) {
if (record.owner !== client) continue;
clearTimeout(record.timer);
pendingServerRequests.delete(id);
autoDeclineServerRequest({ id, method: record.method, params: record.params });
}
});
});
const heartbeat = setInterval(() => {
for (const client of clients) {
if (!client.isAlive) {
client.terminate();
continue;
}
client.isAlive = false;
client.ping();
}
}, 30_000);
function shutdown(signal) {
if (stopping) return;
stopping = true;
log(`Encerrando após ${signal}.`);
clearInterval(heartbeat);
for (const client of clients) client.close(1001, "Serviço reiniciado");
if (codexProcess) codexProcess.kill("SIGTERM");
server.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3_000).unref();
}
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("uncaughtException", (error) => {
log(`Erro não tratado: ${error.stack || error.message}`);
if (codexProcess) codexProcess.kill("SIGTERM");
process.exit(1);
});
process.on("unhandledRejection", (error) => log(`Promise rejeitada: ${error?.stack || error}`));
server.listen(PORT, HOST, () => {
log(`Bridge ouvindo em ws://${HOST}:${PORT}/agent.`);
startCodex();
});