@vibelet/cli 0.1.35 → 0.1.36
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/app.json +5 -0
- package/dist/advertised-hosts.d.ts +34 -0
- package/dist/advertised-hosts.d.ts.map +1 -0
- package/dist/advertised-hosts.js +176 -0
- package/dist/advertised-hosts.js.map +1 -0
- package/dist/advertised-hosts.test.d.ts +2 -0
- package/dist/advertised-hosts.test.d.ts.map +1 -0
- package/dist/advertised-hosts.test.js +96 -0
- package/dist/advertised-hosts.test.js.map +1 -0
- package/dist/audit.d.ts +30 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +73 -0
- package/dist/audit.js.map +1 -0
- package/dist/audit.test.d.ts +2 -0
- package/dist/audit.test.d.ts.map +1 -0
- package/dist/audit.test.js +33 -0
- package/dist/audit.test.js.map +1 -0
- package/dist/auth.d.ts +6 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +27 -0
- package/dist/auth.js.map +1 -0
- package/dist/claude-hooks.d.ts +58 -0
- package/dist/claude-hooks.d.ts.map +1 -0
- package/dist/claude-hooks.js +129 -0
- package/dist/claude-hooks.js.map +1 -0
- package/dist/cli-version.d.ts +3 -0
- package/dist/cli-version.d.ts.map +1 -0
- package/dist/cli-version.js +35 -0
- package/dist/cli-version.js.map +1 -0
- package/dist/cli-version.test.d.ts +2 -0
- package/dist/cli-version.test.d.ts.map +1 -0
- package/dist/cli-version.test.js +38 -0
- package/dist/cli-version.test.js.map +1 -0
- package/dist/config.d.ts +30 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +327 -0
- package/dist/config.js.map +1 -0
- package/dist/config.test.d.ts +2 -0
- package/dist/config.test.d.ts.map +1 -0
- package/dist/config.test.js +184 -0
- package/dist/config.test.js.map +1 -0
- package/dist/dev-auth.test.d.ts +2 -0
- package/dist/dev-auth.test.d.ts.map +1 -0
- package/dist/dev-auth.test.js +154 -0
- package/dist/dev-auth.test.js.map +1 -0
- package/dist/dev-script.test.d.ts +2 -0
- package/dist/dev-script.test.d.ts.map +1 -0
- package/dist/dev-script.test.js +412 -0
- package/dist/dev-script.test.js.map +1 -0
- package/dist/drivers/claude.d.ts +34 -0
- package/dist/drivers/claude.d.ts.map +1 -0
- package/dist/drivers/claude.js +413 -0
- package/dist/drivers/claude.js.map +1 -0
- package/dist/drivers/claude.test.d.ts +2 -0
- package/dist/drivers/claude.test.d.ts.map +1 -0
- package/dist/drivers/claude.test.js +951 -0
- package/dist/drivers/claude.test.js.map +1 -0
- package/dist/drivers/codex.d.ts +38 -0
- package/dist/drivers/codex.d.ts.map +1 -0
- package/dist/drivers/codex.js +771 -0
- package/dist/drivers/codex.js.map +1 -0
- package/dist/drivers/codex.test.d.ts +2 -0
- package/dist/drivers/codex.test.d.ts.map +1 -0
- package/dist/drivers/codex.test.js +939 -0
- package/dist/drivers/codex.test.js.map +1 -0
- package/dist/drivers/types.d.ts +14 -0
- package/dist/drivers/types.d.ts.map +1 -0
- package/dist/drivers/types.js +2 -0
- package/dist/drivers/types.js.map +1 -0
- package/dist/e2e.test.d.ts +2 -0
- package/dist/e2e.test.d.ts.map +1 -0
- package/dist/e2e.test.js +111 -0
- package/dist/e2e.test.js.map +1 -0
- package/dist/identity.d.ts +10 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +66 -0
- package/dist/identity.js.map +1 -0
- package/dist/identity.test.d.ts +2 -0
- package/dist/identity.test.d.ts.map +1 -0
- package/dist/identity.test.js +25 -0
- package/dist/identity.test.js.map +1 -0
- package/dist/index-entry.test.d.ts +2 -0
- package/dist/index-entry.test.d.ts.map +1 -0
- package/dist/index-entry.test.js +272 -0
- package/dist/index-entry.test.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +707 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +31 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +75 -0
- package/dist/logger.js.map +1 -0
- package/dist/metrics.d.ts +52 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +89 -0
- package/dist/metrics.js.map +1 -0
- package/dist/pairing-store.d.ts +29 -0
- package/dist/pairing-store.d.ts.map +1 -0
- package/dist/pairing-store.js +131 -0
- package/dist/pairing-store.js.map +1 -0
- package/dist/pairing-store.test.d.ts +2 -0
- package/dist/pairing-store.test.d.ts.map +1 -0
- package/dist/pairing-store.test.js +47 -0
- package/dist/pairing-store.test.js.map +1 -0
- package/dist/paths.d.ts +16 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +18 -0
- package/dist/paths.js.map +1 -0
- package/dist/perf-compare.d.ts +13 -0
- package/dist/perf-compare.d.ts.map +1 -0
- package/dist/perf-compare.js +125 -0
- package/dist/perf-compare.js.map +1 -0
- package/dist/port-conflict.d.ts +9 -0
- package/dist/port-conflict.d.ts.map +1 -0
- package/dist/port-conflict.js +33 -0
- package/dist/port-conflict.js.map +1 -0
- package/dist/port-conflict.test.d.ts +2 -0
- package/dist/port-conflict.test.d.ts.map +1 -0
- package/dist/port-conflict.test.js +38 -0
- package/dist/port-conflict.test.js.map +1 -0
- package/dist/process-scanner.d.ts +43 -0
- package/dist/process-scanner.d.ts.map +1 -0
- package/dist/process-scanner.js +453 -0
- package/dist/process-scanner.js.map +1 -0
- package/dist/process-scanner.perf.test.d.ts +2 -0
- package/dist/process-scanner.perf.test.d.ts.map +1 -0
- package/dist/process-scanner.perf.test.js +186 -0
- package/dist/process-scanner.perf.test.js.map +1 -0
- package/dist/process-scanner.test.d.ts +2 -0
- package/dist/process-scanner.test.d.ts.map +1 -0
- package/dist/process-scanner.test.js +399 -0
- package/dist/process-scanner.test.js.map +1 -0
- package/dist/push-protocol.d.ts +15 -0
- package/dist/push-protocol.d.ts.map +1 -0
- package/dist/push-protocol.js +23 -0
- package/dist/push-protocol.js.map +1 -0
- package/dist/push-protocol.test.d.ts +2 -0
- package/dist/push-protocol.test.d.ts.map +1 -0
- package/dist/push-protocol.test.js +57 -0
- package/dist/push-protocol.test.js.map +1 -0
- package/dist/push-store.d.ts +22 -0
- package/dist/push-store.d.ts.map +1 -0
- package/dist/push-store.js +103 -0
- package/dist/push-store.js.map +1 -0
- package/dist/push-store.test.d.ts +2 -0
- package/dist/push-store.test.d.ts.map +1 -0
- package/dist/push-store.test.js +79 -0
- package/dist/push-store.test.js.map +1 -0
- package/dist/push.d.ts +65 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +202 -0
- package/dist/push.js.map +1 -0
- package/dist/push.test.d.ts +2 -0
- package/dist/push.test.d.ts.map +1 -0
- package/dist/push.test.js +199 -0
- package/dist/push.test.js.map +1 -0
- package/dist/safe-stdio.d.ts +3 -0
- package/dist/safe-stdio.d.ts.map +1 -0
- package/dist/safe-stdio.js +46 -0
- package/dist/safe-stdio.js.map +1 -0
- package/dist/scanner.d.ts +30 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +859 -0
- package/dist/scanner.js.map +1 -0
- package/dist/scanner.perf.test.d.ts +2 -0
- package/dist/scanner.perf.test.d.ts.map +1 -0
- package/dist/scanner.perf.test.js +320 -0
- package/dist/scanner.perf.test.js.map +1 -0
- package/dist/scanner.test.d.ts +2 -0
- package/dist/scanner.test.d.ts.map +1 -0
- package/dist/scanner.test.js +948 -0
- package/dist/scanner.test.js.map +1 -0
- package/dist/session-inventory.d.ts +63 -0
- package/dist/session-inventory.d.ts.map +1 -0
- package/dist/session-inventory.js +525 -0
- package/dist/session-inventory.js.map +1 -0
- package/dist/session-inventory.perf.test.d.ts +2 -0
- package/dist/session-inventory.perf.test.d.ts.map +1 -0
- package/dist/session-inventory.perf.test.js +220 -0
- package/dist/session-inventory.perf.test.js.map +1 -0
- package/dist/session-inventory.test.d.ts +2 -0
- package/dist/session-inventory.test.d.ts.map +1 -0
- package/dist/session-inventory.test.js +712 -0
- package/dist/session-inventory.test.js.map +1 -0
- package/dist/session-manager.d.ts +75 -0
- package/dist/session-manager.d.ts.map +1 -0
- package/dist/session-manager.js +1515 -0
- package/dist/session-manager.js.map +1 -0
- package/dist/session-manager.test.d.ts +2 -0
- package/dist/session-manager.test.d.ts.map +1 -0
- package/dist/session-manager.test.js +2861 -0
- package/dist/session-manager.test.js.map +1 -0
- package/dist/session-store.d.ts +42 -0
- package/dist/session-store.d.ts.map +1 -0
- package/dist/session-store.js +163 -0
- package/dist/session-store.js.map +1 -0
- package/dist/session-store.test.d.ts +2 -0
- package/dist/session-store.test.d.ts.map +1 -0
- package/dist/session-store.test.js +236 -0
- package/dist/session-store.test.js.map +1 -0
- package/dist/session-title.d.ts +6 -0
- package/dist/session-title.d.ts.map +1 -0
- package/dist/session-title.js +105 -0
- package/dist/session-title.js.map +1 -0
- package/dist/session-title.perf.test.d.ts +2 -0
- package/dist/session-title.perf.test.d.ts.map +1 -0
- package/dist/session-title.perf.test.js +99 -0
- package/dist/session-title.perf.test.js.map +1 -0
- package/dist/session-title.test.d.ts +2 -0
- package/dist/session-title.test.d.ts.map +1 -0
- package/dist/session-title.test.js +199 -0
- package/dist/session-title.test.js.map +1 -0
- package/dist/shutdown-endpoint.test.d.ts +2 -0
- package/dist/shutdown-endpoint.test.d.ts.map +1 -0
- package/dist/shutdown-endpoint.test.js +93 -0
- package/dist/shutdown-endpoint.test.js.map +1 -0
- package/dist/storage-housekeeping.d.ts +28 -0
- package/dist/storage-housekeeping.d.ts.map +1 -0
- package/dist/storage-housekeeping.js +76 -0
- package/dist/storage-housekeeping.js.map +1 -0
- package/dist/storage-housekeeping.test.d.ts +2 -0
- package/dist/storage-housekeeping.test.d.ts.map +1 -0
- package/dist/storage-housekeeping.test.js +65 -0
- package/dist/storage-housekeeping.test.js.map +1 -0
- package/dist/test-daemon-harness.d.ts +31 -0
- package/dist/test-daemon-harness.d.ts.map +1 -0
- package/dist/test-daemon-harness.js +337 -0
- package/dist/test-daemon-harness.js.map +1 -0
- package/dist/token-auth.test.d.ts +2 -0
- package/dist/token-auth.test.d.ts.map +1 -0
- package/dist/token-auth.test.js +52 -0
- package/dist/token-auth.test.js.map +1 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +40 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.test.d.ts +2 -0
- package/dist/utils.test.d.ts.map +1 -0
- package/dist/utils.test.js +54 -0
- package/dist/utils.test.js.map +1 -0
- package/dist/ws-data.d.ts +4 -0
- package/dist/ws-data.d.ts.map +1 -0
- package/dist/ws-data.js +20 -0
- package/dist/ws-data.js.map +1 -0
- package/dist/ws-data.test.d.ts +2 -0
- package/dist/ws-data.test.d.ts.map +1 -0
- package/dist/ws-data.test.js +17 -0
- package/dist/ws-data.test.js.map +1 -0
- package/package.json +24 -27
- package/perf-reporter.mjs +138 -0
- package/scripts/build-release.mjs +41 -0
- package/scripts/dev.mjs +537 -0
- package/src/advertised-hosts.test.ts +125 -0
- package/src/advertised-hosts.ts +225 -0
- package/src/audit.test.ts +38 -0
- package/src/audit.ts +117 -0
- package/src/auth.ts +31 -0
- package/src/claude-hooks.ts +195 -0
- package/src/cli-version.test.ts +36 -0
- package/src/cli-version.ts +46 -0
- package/src/config.test.ts +254 -0
- package/src/config.ts +324 -0
- package/src/dev-auth.test.ts +183 -0
- package/src/dev-script.test.ts +511 -0
- package/src/drivers/claude.test.ts +1186 -0
- package/src/drivers/claude.ts +443 -0
- package/src/drivers/codex.test.ts +1096 -0
- package/src/drivers/codex.ts +879 -0
- package/src/drivers/types.ts +15 -0
- package/src/e2e.test.ts +139 -0
- package/src/identity.test.ts +26 -0
- package/src/identity.ts +82 -0
- package/src/index-entry.test.ts +336 -0
- package/src/index.ts +781 -0
- package/src/logger.ts +112 -0
- package/src/metrics.ts +117 -0
- package/src/pairing-store.test.ts +53 -0
- package/src/pairing-store.ts +154 -0
- package/src/paths.ts +19 -0
- package/src/perf-compare.ts +164 -0
- package/src/port-conflict.test.ts +45 -0
- package/src/port-conflict.ts +44 -0
- package/src/process-scanner.perf.test.ts +222 -0
- package/src/process-scanner.test.ts +575 -0
- package/src/process-scanner.ts +514 -0
- package/src/push-protocol.test.ts +74 -0
- package/src/push-protocol.ts +36 -0
- package/src/push-store.test.ts +89 -0
- package/src/push-store.ts +126 -0
- package/src/push.test.ts +234 -0
- package/src/push.ts +318 -0
- package/src/safe-stdio.ts +51 -0
- package/src/scanner.perf.test.ts +359 -0
- package/src/scanner.test.ts +1045 -0
- package/src/scanner.ts +924 -0
- package/src/session-inventory.perf.test.ts +250 -0
- package/src/session-inventory.test.ts +1002 -0
- package/src/session-inventory.ts +721 -0
- package/src/session-manager.test.ts +3430 -0
- package/src/session-manager.ts +1775 -0
- package/src/session-store.test.ts +276 -0
- package/src/session-store.ts +202 -0
- package/src/session-title.perf.test.ts +118 -0
- package/src/session-title.test.ts +286 -0
- package/src/session-title.ts +108 -0
- package/src/shutdown-endpoint.test.ts +95 -0
- package/src/storage-housekeeping.test.ts +78 -0
- package/src/storage-housekeeping.ts +111 -0
- package/src/test-daemon-harness.ts +410 -0
- package/src/token-auth.test.ts +67 -0
- package/src/utils.test.ts +65 -0
- package/src/utils.ts +47 -0
- package/src/ws-data.test.ts +20 -0
- package/src/ws-data.ts +26 -0
- package/tsconfig.json +12 -0
- package/README.md +0 -80
- package/bin/cloudflared-quick-tunnel.mjs +0 -11
- package/bin/cloudflared-resolver.mjs +0 -68
- package/bin/vibelet-runtime-policy.mjs +0 -36
- package/bin/vibelet.cjs +0 -12
- package/bin/vibelet.mjs +0 -1035
- package/dist/index.cjs +0 -125
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { config } from '../config.js';
|
|
4
|
+
import { logger as rootLogger } from '../logger.js';
|
|
5
|
+
import { metrics } from '../metrics.js';
|
|
6
|
+
import { audit } from '../audit.js';
|
|
7
|
+
import { CLI_VERSION } from '../cli-version.js';
|
|
8
|
+
const log = rootLogger.child({ module: 'codex' });
|
|
9
|
+
function serializeApprovalContext(request) {
|
|
10
|
+
switch (request.kind) {
|
|
11
|
+
case 'request-user-input-approval':
|
|
12
|
+
return {
|
|
13
|
+
provider: 'codex',
|
|
14
|
+
kind: request.kind,
|
|
15
|
+
rpcId: request.rpcId,
|
|
16
|
+
questionId: request.questionId,
|
|
17
|
+
approveLabel: request.approveLabel,
|
|
18
|
+
denyLabel: request.denyLabel,
|
|
19
|
+
};
|
|
20
|
+
default:
|
|
21
|
+
return {
|
|
22
|
+
provider: 'codex',
|
|
23
|
+
kind: request.kind,
|
|
24
|
+
rpcId: request.rpcId,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function restoreApprovalRequest(approval) {
|
|
29
|
+
const context = approval.approvalContext;
|
|
30
|
+
if (!context || context.provider !== 'codex') {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
switch (context.kind) {
|
|
34
|
+
case 'command-execution':
|
|
35
|
+
return {
|
|
36
|
+
kind: context.kind,
|
|
37
|
+
responseKind: 'v2',
|
|
38
|
+
rpcId: context.rpcId,
|
|
39
|
+
toolName: approval.toolName,
|
|
40
|
+
input: approval.input,
|
|
41
|
+
};
|
|
42
|
+
case 'file-change':
|
|
43
|
+
return {
|
|
44
|
+
kind: context.kind,
|
|
45
|
+
responseKind: 'v2',
|
|
46
|
+
rpcId: context.rpcId,
|
|
47
|
+
toolName: approval.toolName,
|
|
48
|
+
input: approval.input,
|
|
49
|
+
};
|
|
50
|
+
case 'request-user-input-approval':
|
|
51
|
+
if (!context.questionId || !context.approveLabel || !context.denyLabel) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
kind: context.kind,
|
|
56
|
+
rpcId: context.rpcId,
|
|
57
|
+
questionId: context.questionId,
|
|
58
|
+
approveLabel: context.approveLabel,
|
|
59
|
+
denyLabel: context.denyLabel,
|
|
60
|
+
toolName: approval.toolName,
|
|
61
|
+
input: approval.input,
|
|
62
|
+
};
|
|
63
|
+
case 'exec-command-legacy':
|
|
64
|
+
return {
|
|
65
|
+
kind: context.kind,
|
|
66
|
+
rpcId: context.rpcId,
|
|
67
|
+
toolName: approval.toolName,
|
|
68
|
+
input: approval.input,
|
|
69
|
+
};
|
|
70
|
+
case 'apply-patch-legacy':
|
|
71
|
+
return {
|
|
72
|
+
kind: context.kind,
|
|
73
|
+
rpcId: context.rpcId,
|
|
74
|
+
toolName: approval.toolName,
|
|
75
|
+
input: approval.input,
|
|
76
|
+
};
|
|
77
|
+
default:
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function asRecord(value) {
|
|
82
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
83
|
+
return null;
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
function readString(value) {
|
|
87
|
+
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
|
|
88
|
+
}
|
|
89
|
+
const INTERRUPTED_TURN_STATUSES = new Set(['aborted', 'interrupted', 'cancelled', 'canceled']);
|
|
90
|
+
function normalizeTurnStatus(value) {
|
|
91
|
+
const text = readString(value);
|
|
92
|
+
return text ? text.toLowerCase() : null;
|
|
93
|
+
}
|
|
94
|
+
function normalizeItemType(value) {
|
|
95
|
+
const text = readString(value);
|
|
96
|
+
return text ? text.replace(/[^a-z0-9]/gi, '').toLowerCase() : null;
|
|
97
|
+
}
|
|
98
|
+
function readItemId(record) {
|
|
99
|
+
if (!record)
|
|
100
|
+
return null;
|
|
101
|
+
return readString(record.itemId) ?? readString(record.id) ?? readString(record.callId) ?? readString(record.call_id);
|
|
102
|
+
}
|
|
103
|
+
function omitKeys(record, keys) {
|
|
104
|
+
const next = {};
|
|
105
|
+
for (const [key, value] of Object.entries(record)) {
|
|
106
|
+
if (!keys.includes(key))
|
|
107
|
+
next[key] = value;
|
|
108
|
+
}
|
|
109
|
+
return next;
|
|
110
|
+
}
|
|
111
|
+
function isApproveLabel(label) {
|
|
112
|
+
return /\bapprove\b|\ballow\b|\baccept\b|\byes\b|\bcontinue\b|\bproceed\b|\brun\b|\bapply\b/i.test(label);
|
|
113
|
+
}
|
|
114
|
+
function isDenyLabel(label) {
|
|
115
|
+
return /\bdeny\b|\breject\b|\bdecline\b|\bno\b|\bcancel\b|\babort\b|\bstop\b/i.test(label);
|
|
116
|
+
}
|
|
117
|
+
function resolveApprovalQuestion(questions) {
|
|
118
|
+
if (!Array.isArray(questions))
|
|
119
|
+
return null;
|
|
120
|
+
for (const question of questions) {
|
|
121
|
+
const record = asRecord(question);
|
|
122
|
+
if (!record)
|
|
123
|
+
continue;
|
|
124
|
+
const questionId = readString(record.id);
|
|
125
|
+
const options = Array.isArray(record.options)
|
|
126
|
+
? record.options.map((option) => asRecord(option)).filter((option) => Boolean(option))
|
|
127
|
+
: [];
|
|
128
|
+
if (!questionId || options.length === 0)
|
|
129
|
+
continue;
|
|
130
|
+
const labels = options
|
|
131
|
+
.map((option) => readString(option.label))
|
|
132
|
+
.filter((label) => Boolean(label));
|
|
133
|
+
if (labels.length < 2)
|
|
134
|
+
continue;
|
|
135
|
+
const approveLabel = labels.find((label) => isApproveLabel(label)) ?? labels[0];
|
|
136
|
+
const denyLabel = labels.find((label) => isDenyLabel(label)) ?? labels[labels.length - 1];
|
|
137
|
+
if (!approveLabel || !denyLabel || approveLabel === denyLabel) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
questionId,
|
|
142
|
+
approveLabel,
|
|
143
|
+
denyLabel,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
function looksLikeApprovalRequestUserInput(questions) {
|
|
149
|
+
if (!Array.isArray(questions) || questions.length === 0)
|
|
150
|
+
return false;
|
|
151
|
+
const labels = questions
|
|
152
|
+
.map((question) => asRecord(question))
|
|
153
|
+
.filter((question) => Boolean(question))
|
|
154
|
+
.flatMap((question) => {
|
|
155
|
+
const options = Array.isArray(question.options) ? question.options : [];
|
|
156
|
+
return options
|
|
157
|
+
.map((option) => asRecord(option))
|
|
158
|
+
.filter((option) => Boolean(option))
|
|
159
|
+
.map((option) => readString(option.label))
|
|
160
|
+
.filter((label) => Boolean(label));
|
|
161
|
+
});
|
|
162
|
+
const hasApproval = labels.some((label) => isApproveLabel(label));
|
|
163
|
+
const hasDeny = labels.some((label) => isDenyLabel(label));
|
|
164
|
+
return hasApproval && hasDeny;
|
|
165
|
+
}
|
|
166
|
+
function extractToolContext(item) {
|
|
167
|
+
const itemType = normalizeItemType(item.type ?? item.itemType);
|
|
168
|
+
if (itemType === 'commandexecution') {
|
|
169
|
+
return {
|
|
170
|
+
toolName: 'Bash',
|
|
171
|
+
input: omitKeys(item, ['id', 'itemId', 'type', 'itemType', 'stdout', 'stderr', 'exitCode', 'exit_code', 'status', 'success', 'error']),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (itemType === 'filechange') {
|
|
175
|
+
return {
|
|
176
|
+
toolName: 'Patch',
|
|
177
|
+
input: omitKeys(item, ['id', 'itemId', 'type', 'itemType', 'stdout', 'stderr', 'exitCode', 'exit_code', 'status', 'success', 'error']),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (itemType === 'mcptoolcall') {
|
|
181
|
+
const server = readString(item.server);
|
|
182
|
+
const tool = readString(item.tool) ?? readString(item.name);
|
|
183
|
+
if (!tool)
|
|
184
|
+
return null;
|
|
185
|
+
return {
|
|
186
|
+
toolName: server ? `mcp__${server}__${tool}` : tool,
|
|
187
|
+
input: (asRecord(item.arguments) ?? asRecord(item.input) ?? {}),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
function readRequestUserInputDescription(questions) {
|
|
193
|
+
if (!Array.isArray(questions))
|
|
194
|
+
return '';
|
|
195
|
+
for (const question of questions) {
|
|
196
|
+
const record = asRecord(question);
|
|
197
|
+
if (!record)
|
|
198
|
+
continue;
|
|
199
|
+
const questionText = readString(record.question) ?? readString(record.header);
|
|
200
|
+
if (questionText)
|
|
201
|
+
return questionText;
|
|
202
|
+
}
|
|
203
|
+
return '';
|
|
204
|
+
}
|
|
205
|
+
function resolveCodexPolicy(approvalMode, cwd) {
|
|
206
|
+
if (approvalMode === 'autoApprove') {
|
|
207
|
+
return {
|
|
208
|
+
approvalPolicy: 'never',
|
|
209
|
+
sandbox: 'danger-full-access',
|
|
210
|
+
sandboxPolicy: { type: 'dangerFullAccess' },
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (approvalMode === 'plan') {
|
|
214
|
+
return {
|
|
215
|
+
approvalPolicy: 'on-request',
|
|
216
|
+
sandbox: 'workspace-write',
|
|
217
|
+
sandboxPolicy: {
|
|
218
|
+
type: 'workspaceWrite',
|
|
219
|
+
writableRoots: [],
|
|
220
|
+
readOnlyAccess: { type: 'fullAccess' },
|
|
221
|
+
networkAccess: true,
|
|
222
|
+
excludeTmpdirEnvVar: false,
|
|
223
|
+
excludeSlashTmp: false,
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
approvalPolicy: 'on-request',
|
|
229
|
+
sandbox: 'workspace-write',
|
|
230
|
+
sandboxPolicy: {
|
|
231
|
+
type: 'workspaceWrite',
|
|
232
|
+
writableRoots: [cwd],
|
|
233
|
+
readOnlyAccess: { type: 'fullAccess' },
|
|
234
|
+
networkAccess: true,
|
|
235
|
+
excludeTmpdirEnvVar: false,
|
|
236
|
+
excludeSlashTmp: false,
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
export class CodexDriver {
|
|
241
|
+
proc = null;
|
|
242
|
+
handler = null;
|
|
243
|
+
exitHandler = null;
|
|
244
|
+
buffer = '';
|
|
245
|
+
rpcId = 0;
|
|
246
|
+
pending = new Map();
|
|
247
|
+
threadId = '';
|
|
248
|
+
lastStderr = [];
|
|
249
|
+
approvalRequests = new Map();
|
|
250
|
+
toolContextByCallId = new Map();
|
|
251
|
+
turnStartInFlight = false;
|
|
252
|
+
interruptRequestedDuringTurnStart = false;
|
|
253
|
+
approvalMode;
|
|
254
|
+
cwd = '';
|
|
255
|
+
async start(cwd, resumeSessionId, approvalMode) {
|
|
256
|
+
this.approvalMode = approvalMode;
|
|
257
|
+
this.cwd = cwd;
|
|
258
|
+
this.approvalRequests.clear();
|
|
259
|
+
this.toolContextByCallId.clear();
|
|
260
|
+
this.turnStartInFlight = false;
|
|
261
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
262
|
+
const codexPath = config.codexPath;
|
|
263
|
+
let spawnCmd;
|
|
264
|
+
let spawnArgs = this.buildSpawnArgs();
|
|
265
|
+
if (config.isTransientPath(codexPath)) {
|
|
266
|
+
const resolved = config.execViaLoginShell('codex', spawnArgs);
|
|
267
|
+
spawnCmd = resolved.command;
|
|
268
|
+
spawnArgs = resolved.args;
|
|
269
|
+
log.info({ spawnCmd, argsPreview: spawnArgs.slice(0, 2) }, 'spawning via login shell');
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
spawnCmd = codexPath;
|
|
273
|
+
log.info({ spawnCmd, args: spawnArgs }, 'spawning');
|
|
274
|
+
}
|
|
275
|
+
audit.emit('driver.spawn', { agent: 'codex', cwd, resumeSessionId });
|
|
276
|
+
this.lastStderr = [];
|
|
277
|
+
this.proc = spawn(spawnCmd, spawnArgs, {
|
|
278
|
+
cwd: cwd || undefined,
|
|
279
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
280
|
+
env: config.buildSanitizedEnv(),
|
|
281
|
+
...(process.platform === 'win32' ? { shell: true } : {}),
|
|
282
|
+
});
|
|
283
|
+
this.proc.on('error', (err) => {
|
|
284
|
+
let message = err.message;
|
|
285
|
+
if (err.code === 'ENOENT' && cwd && !existsSync(cwd)) {
|
|
286
|
+
message = `Working directory does not exist: ${cwd}`;
|
|
287
|
+
}
|
|
288
|
+
log.error({ error: message, cwd }, 'spawn error');
|
|
289
|
+
audit.emit('driver.error', { agent: 'codex', error: message });
|
|
290
|
+
});
|
|
291
|
+
this.proc.stdout.on('data', (chunk) => {
|
|
292
|
+
this.buffer += chunk.toString();
|
|
293
|
+
const lines = this.buffer.split('\n');
|
|
294
|
+
this.buffer = lines.pop();
|
|
295
|
+
for (const line of lines) {
|
|
296
|
+
if (!line.trim())
|
|
297
|
+
continue;
|
|
298
|
+
try {
|
|
299
|
+
this.handleRaw(JSON.parse(line));
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
log.warn({ linePreview: line.slice(0, 200) }, 'failed to parse stdout line');
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
this.proc.stderr.on('data', (chunk) => {
|
|
307
|
+
const text = chunk.toString().trim();
|
|
308
|
+
if (text) {
|
|
309
|
+
log.debug({ stderr: text }, 'stderr');
|
|
310
|
+
this.lastStderr.push(text);
|
|
311
|
+
if (this.lastStderr.length > 10)
|
|
312
|
+
this.lastStderr.shift();
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
this.proc.on('exit', (code) => {
|
|
316
|
+
log.info({ threadId: this.threadId, exitCode: code }, 'process exited');
|
|
317
|
+
if (code && code !== 0) {
|
|
318
|
+
log.error({ threadId: this.threadId, exitCode: code, lastStderr: this.lastStderr.slice(-3) }, 'abnormal exit');
|
|
319
|
+
}
|
|
320
|
+
this.proc = null;
|
|
321
|
+
this.approvalRequests.clear();
|
|
322
|
+
this.toolContextByCallId.clear();
|
|
323
|
+
this.turnStartInFlight = false;
|
|
324
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
325
|
+
for (const [, { reject }] of this.pending) {
|
|
326
|
+
reject(new Error(`Codex process exited with code ${code}`));
|
|
327
|
+
}
|
|
328
|
+
this.pending.clear();
|
|
329
|
+
this.exitHandler?.(code);
|
|
330
|
+
if (code && this.handler && this.threadId) {
|
|
331
|
+
this.handler({ type: 'error', sessionId: this.threadId, message: `Codex process exited with code ${code}` });
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
const endInit = metrics.startTimer('rpc.duration');
|
|
335
|
+
await this.rpc('initialize', {
|
|
336
|
+
clientInfo: { name: '@vibelet/cli', version: CLI_VERSION },
|
|
337
|
+
capabilities: {
|
|
338
|
+
experimentalApi: true,
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
endInit();
|
|
342
|
+
this.rpcNotify('initialized', {});
|
|
343
|
+
const policy = resolveCodexPolicy(this.approvalMode, cwd);
|
|
344
|
+
if (resumeSessionId) {
|
|
345
|
+
const res = await this.rpc('thread/resume', {
|
|
346
|
+
threadId: resumeSessionId,
|
|
347
|
+
approvalPolicy: policy.approvalPolicy,
|
|
348
|
+
sandbox: policy.sandbox,
|
|
349
|
+
persistExtendedHistory: true,
|
|
350
|
+
});
|
|
351
|
+
this.threadId = res?.thread?.id ?? resumeSessionId;
|
|
352
|
+
log.info({ threadId: this.threadId }, 'thread resumed');
|
|
353
|
+
audit.emit('driver.init', { agent: 'codex', sessionId: this.threadId });
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
const res = await this.rpc('thread/start', {
|
|
357
|
+
cwd,
|
|
358
|
+
approvalPolicy: policy.approvalPolicy,
|
|
359
|
+
sandbox: policy.sandbox,
|
|
360
|
+
experimentalRawEvents: true,
|
|
361
|
+
persistExtendedHistory: true,
|
|
362
|
+
});
|
|
363
|
+
this.threadId = res?.thread?.id ?? res?.threadId ?? '';
|
|
364
|
+
log.info({ threadId: this.threadId }, 'thread created');
|
|
365
|
+
audit.emit('driver.init', { agent: 'codex', sessionId: this.threadId });
|
|
366
|
+
}
|
|
367
|
+
return this.threadId;
|
|
368
|
+
}
|
|
369
|
+
buildSpawnArgs() {
|
|
370
|
+
return ['app-server', '--listen', 'stdio://'];
|
|
371
|
+
}
|
|
372
|
+
sendPrompt(text) {
|
|
373
|
+
const policy = resolveCodexPolicy(this.approvalMode, this.cwd || process.cwd());
|
|
374
|
+
this.turnStartInFlight = true;
|
|
375
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
376
|
+
log.info({ threadId: this.threadId, promptPreview: text.slice(0, 50) }, 'turn/start');
|
|
377
|
+
this.rpc('turn/start', {
|
|
378
|
+
threadId: this.threadId,
|
|
379
|
+
input: [{ type: 'text', text }],
|
|
380
|
+
approvalPolicy: policy.approvalPolicy,
|
|
381
|
+
sandboxPolicy: policy.sandboxPolicy,
|
|
382
|
+
})
|
|
383
|
+
.then((res) => {
|
|
384
|
+
log.debug({ resultPreview: JSON.stringify(res).slice(0, 200) }, 'turn/start result');
|
|
385
|
+
this.turnStartInFlight = false;
|
|
386
|
+
if (this.interruptRequestedDuringTurnStart) {
|
|
387
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
388
|
+
this.requestTurnInterrupt();
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
.catch((e) => {
|
|
392
|
+
this.turnStartInFlight = false;
|
|
393
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
394
|
+
log.error({ threadId: this.threadId, error: e.message }, 'sendPrompt error');
|
|
395
|
+
this.handler?.({ type: 'error', sessionId: this.threadId, message: e.message });
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
respondApproval(requestId, approved) {
|
|
399
|
+
if (!this.proc?.stdin?.writable)
|
|
400
|
+
return false;
|
|
401
|
+
const tracked = this.approvalRequests.get(requestId);
|
|
402
|
+
const rpcId = tracked?.rpcId ?? Number(requestId);
|
|
403
|
+
if (!Number.isFinite(rpcId))
|
|
404
|
+
return false;
|
|
405
|
+
let result;
|
|
406
|
+
if (!tracked) {
|
|
407
|
+
result = approved
|
|
408
|
+
? { decision: 'approve' }
|
|
409
|
+
: { decision: 'deny', reason: 'User denied from Vibelet' };
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
switch (tracked.kind) {
|
|
413
|
+
case 'command-execution':
|
|
414
|
+
case 'file-change':
|
|
415
|
+
result = { decision: approved ? 'accept' : 'decline' };
|
|
416
|
+
break;
|
|
417
|
+
case 'request-user-input-approval':
|
|
418
|
+
result = {
|
|
419
|
+
answers: {
|
|
420
|
+
[tracked.questionId]: {
|
|
421
|
+
answers: [approved ? tracked.approveLabel : tracked.denyLabel],
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
break;
|
|
426
|
+
case 'exec-command-legacy':
|
|
427
|
+
case 'apply-patch-legacy':
|
|
428
|
+
result = { decision: approved ? 'approved' : 'denied' };
|
|
429
|
+
break;
|
|
430
|
+
default:
|
|
431
|
+
result = { decision: approved ? 'accept' : 'decline' };
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
this.approvalRequests.delete(requestId);
|
|
435
|
+
}
|
|
436
|
+
log.info({ threadId: this.threadId, rpcId, approved }, 'approval response');
|
|
437
|
+
this.proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: rpcId, result }) + '\n');
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
restorePendingApproval(approval) {
|
|
441
|
+
const request = restoreApprovalRequest(approval);
|
|
442
|
+
if (!request) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
this.approvalRequests.set(approval.requestId, request);
|
|
446
|
+
}
|
|
447
|
+
interrupt() {
|
|
448
|
+
if (this.turnStartInFlight) {
|
|
449
|
+
this.interruptRequestedDuringTurnStart = true;
|
|
450
|
+
}
|
|
451
|
+
this.requestTurnInterrupt();
|
|
452
|
+
}
|
|
453
|
+
setApprovalMode(mode) {
|
|
454
|
+
this.approvalMode = mode;
|
|
455
|
+
log.info({ approvalMode: mode }, 'approval mode updated (takes effect on subsequent turns)');
|
|
456
|
+
}
|
|
457
|
+
stop() {
|
|
458
|
+
if (!this.proc)
|
|
459
|
+
return;
|
|
460
|
+
this.proc.kill('SIGTERM');
|
|
461
|
+
const p = this.proc;
|
|
462
|
+
setTimeout(() => {
|
|
463
|
+
if (!p.killed)
|
|
464
|
+
p.kill('SIGKILL');
|
|
465
|
+
}, 5000).unref();
|
|
466
|
+
this.proc = null;
|
|
467
|
+
this.approvalRequests.clear();
|
|
468
|
+
this.toolContextByCallId.clear();
|
|
469
|
+
this.turnStartInFlight = false;
|
|
470
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
471
|
+
for (const [, { reject }] of this.pending) {
|
|
472
|
+
reject(new Error('Process stopped'));
|
|
473
|
+
}
|
|
474
|
+
this.pending.clear();
|
|
475
|
+
}
|
|
476
|
+
onMessage(handler) {
|
|
477
|
+
this.handler = handler;
|
|
478
|
+
}
|
|
479
|
+
onExit(handler) {
|
|
480
|
+
this.exitHandler = handler;
|
|
481
|
+
}
|
|
482
|
+
rpc(method, params) {
|
|
483
|
+
const id = ++this.rpcId;
|
|
484
|
+
const endTimer = metrics.startTimer('rpc.duration');
|
|
485
|
+
return new Promise((resolve, reject) => {
|
|
486
|
+
this.pending.set(id, {
|
|
487
|
+
resolve: (v) => { endTimer(); resolve(v); },
|
|
488
|
+
reject: (e) => { endTimer(); reject(e); },
|
|
489
|
+
});
|
|
490
|
+
if (!this.proc?.stdin?.writable) {
|
|
491
|
+
this.pending.delete(id);
|
|
492
|
+
endTimer();
|
|
493
|
+
reject(new Error('Process not available'));
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
this.proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', method, id, params }) + '\n');
|
|
497
|
+
setTimeout(() => {
|
|
498
|
+
if (this.pending.has(id)) {
|
|
499
|
+
this.pending.delete(id);
|
|
500
|
+
log.error({ method, rpcId: id, threadId: this.threadId }, 'RPC timeout');
|
|
501
|
+
endTimer();
|
|
502
|
+
reject(new Error(`RPC timeout: ${method}`));
|
|
503
|
+
}
|
|
504
|
+
}, 30000).unref();
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
requestTurnInterrupt() {
|
|
508
|
+
this.rpc('turn/interrupt', { threadId: this.threadId }).catch(() => { });
|
|
509
|
+
}
|
|
510
|
+
rpcNotify(method, params) {
|
|
511
|
+
if (!this.proc?.stdin?.writable)
|
|
512
|
+
return;
|
|
513
|
+
this.proc.stdin.write(JSON.stringify({ jsonrpc: '2.0', method, params }) + '\n');
|
|
514
|
+
}
|
|
515
|
+
writeServerRequestError(rpcId, message) {
|
|
516
|
+
if (!this.proc?.stdin?.writable)
|
|
517
|
+
return;
|
|
518
|
+
this.proc.stdin.write(JSON.stringify({
|
|
519
|
+
jsonrpc: '2.0',
|
|
520
|
+
id: rpcId,
|
|
521
|
+
error: { code: -32601, message },
|
|
522
|
+
}) + '\n');
|
|
523
|
+
}
|
|
524
|
+
resolveToolContext(params) {
|
|
525
|
+
const item = asRecord(params?.item) ?? params ?? null;
|
|
526
|
+
const callId = readItemId(item);
|
|
527
|
+
if (!callId)
|
|
528
|
+
return null;
|
|
529
|
+
const extracted = item ? extractToolContext(item) : null;
|
|
530
|
+
if (extracted) {
|
|
531
|
+
this.toolContextByCallId.set(callId, extracted);
|
|
532
|
+
return { callId, toolContext: extracted };
|
|
533
|
+
}
|
|
534
|
+
const remembered = this.toolContextByCallId.get(callId);
|
|
535
|
+
if (!remembered)
|
|
536
|
+
return null;
|
|
537
|
+
return { callId, toolContext: remembered };
|
|
538
|
+
}
|
|
539
|
+
createFallbackInput(params, excludedKeys) {
|
|
540
|
+
return params ? omitKeys(params, ['threadId', 'turnId', ...excludedKeys]) : {};
|
|
541
|
+
}
|
|
542
|
+
queueApprovalRequest(requestId, request, description) {
|
|
543
|
+
this.approvalRequests.set(requestId, request);
|
|
544
|
+
audit.emit('approval.request', { agent: 'codex', sessionId: this.threadId, toolName: request.toolName });
|
|
545
|
+
if (!this.handler) {
|
|
546
|
+
log.warn({ threadId: this.threadId, requestId, toolName: request.toolName }, 'approval request received without handler');
|
|
547
|
+
if (!this.respondApproval(requestId, false)) {
|
|
548
|
+
this.writeServerRequestError(request.rpcId, 'Approval handler unavailable');
|
|
549
|
+
}
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
this.handler({
|
|
553
|
+
type: 'approval.request',
|
|
554
|
+
sessionId: this.threadId,
|
|
555
|
+
requestId,
|
|
556
|
+
toolName: request.toolName,
|
|
557
|
+
input: request.input,
|
|
558
|
+
description,
|
|
559
|
+
approvalContext: serializeApprovalContext(request),
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
handleServerRequest(method, rpcId, params) {
|
|
563
|
+
const requestId = String(rpcId);
|
|
564
|
+
const resolved = this.resolveToolContext(params);
|
|
565
|
+
if (method === 'item/commandExecution/requestApproval') {
|
|
566
|
+
this.queueApprovalRequest(requestId, {
|
|
567
|
+
kind: 'command-execution',
|
|
568
|
+
responseKind: 'v2',
|
|
569
|
+
rpcId,
|
|
570
|
+
toolName: resolved?.toolContext.toolName ?? 'Bash',
|
|
571
|
+
input: resolved?.toolContext.input ?? this.createFallbackInput(params, ['itemId', 'reason', 'description']),
|
|
572
|
+
}, readString(params?.reason) ?? readString(params?.description) ?? '');
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
if (method === 'item/fileChange/requestApproval') {
|
|
576
|
+
this.queueApprovalRequest(requestId, {
|
|
577
|
+
kind: 'file-change',
|
|
578
|
+
responseKind: 'v2',
|
|
579
|
+
rpcId,
|
|
580
|
+
toolName: resolved?.toolContext.toolName ?? 'Patch',
|
|
581
|
+
input: resolved?.toolContext.input ?? this.createFallbackInput(params, ['itemId', 'reason', 'description']),
|
|
582
|
+
}, readString(params?.reason) ?? readString(params?.description) ?? '');
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (method === 'item/tool/requestUserInput') {
|
|
586
|
+
const questions = params?.questions;
|
|
587
|
+
if (!looksLikeApprovalRequestUserInput(questions)) {
|
|
588
|
+
log.warn({ threadId: this.threadId, rpcId }, 'unsupported requestUserInput request');
|
|
589
|
+
this.writeServerRequestError(rpcId, 'Unsupported interactive request from Codex');
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const question = resolveApprovalQuestion(questions);
|
|
593
|
+
if (!question) {
|
|
594
|
+
this.writeServerRequestError(rpcId, 'Malformed requestUserInput approval request');
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
this.queueApprovalRequest(requestId, {
|
|
598
|
+
kind: 'request-user-input-approval',
|
|
599
|
+
rpcId,
|
|
600
|
+
questionId: question.questionId,
|
|
601
|
+
approveLabel: question.approveLabel,
|
|
602
|
+
denyLabel: question.denyLabel,
|
|
603
|
+
toolName: resolved?.toolContext.toolName ?? 'Bash',
|
|
604
|
+
input: resolved?.toolContext.input ?? this.createFallbackInput(params, ['itemId', 'questions']),
|
|
605
|
+
}, readRequestUserInputDescription(questions));
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
if (method === 'execCommandApproval') {
|
|
609
|
+
this.queueApprovalRequest(requestId, {
|
|
610
|
+
kind: 'exec-command-legacy',
|
|
611
|
+
rpcId,
|
|
612
|
+
toolName: resolved?.toolContext.toolName ?? 'Bash',
|
|
613
|
+
input: resolved?.toolContext.input ?? this.createFallbackInput(params, []),
|
|
614
|
+
}, readString(params?.reason) ?? readString(params?.description) ?? '');
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (method === 'applyPatchApproval') {
|
|
618
|
+
this.queueApprovalRequest(requestId, {
|
|
619
|
+
kind: 'apply-patch-legacy',
|
|
620
|
+
rpcId,
|
|
621
|
+
toolName: resolved?.toolContext.toolName ?? 'Patch',
|
|
622
|
+
input: resolved?.toolContext.input ?? this.createFallbackInput(params, []),
|
|
623
|
+
}, readString(params?.reason) ?? readString(params?.description) ?? '');
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
if (method === 'item/permissions/requestApproval') {
|
|
627
|
+
log.warn({ threadId: this.threadId, rpcId }, 'unsupported permissions approval request');
|
|
628
|
+
this.writeServerRequestError(rpcId, 'Unsupported permissions approval request');
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
log.warn({ threadId: this.threadId, rpcId, method }, 'unsupported server request');
|
|
632
|
+
this.writeServerRequestError(rpcId, `Unsupported server request: ${method}`);
|
|
633
|
+
}
|
|
634
|
+
handleRaw(msg) {
|
|
635
|
+
log.debug({ method: msg.method ?? '(response)', rpcId: msg.id ?? '-' }, 'handleRaw');
|
|
636
|
+
if (msg.id != null && !msg.method && this.pending.has(msg.id)) {
|
|
637
|
+
const p = this.pending.get(msg.id);
|
|
638
|
+
this.pending.delete(msg.id);
|
|
639
|
+
if (msg.error) {
|
|
640
|
+
p.reject(new Error(msg.error.message ?? 'RPC error'));
|
|
641
|
+
}
|
|
642
|
+
else {
|
|
643
|
+
p.resolve(msg.result);
|
|
644
|
+
}
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const method = typeof msg.method === 'string' ? msg.method : undefined;
|
|
648
|
+
const params = asRecord(msg.params);
|
|
649
|
+
if (method && msg.id != null) {
|
|
650
|
+
this.handleServerRequest(method, msg.id, params ?? undefined);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (method) {
|
|
654
|
+
log.debug({ method, paramsPreview: JSON.stringify(params ?? {}).slice(0, 300) }, 'notification');
|
|
655
|
+
}
|
|
656
|
+
switch (method) {
|
|
657
|
+
case 'item/agentMessage/delta': {
|
|
658
|
+
const deltaText = (params?.text ?? params?.delta ?? params?.content ?? '');
|
|
659
|
+
if (deltaText && this.handler) {
|
|
660
|
+
this.handler({
|
|
661
|
+
type: 'text.delta',
|
|
662
|
+
sessionId: this.threadId,
|
|
663
|
+
content: deltaText,
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
else if (!deltaText) {
|
|
667
|
+
log.debug({ paramsKeys: Object.keys(params ?? {}) }, 'agentMessage/delta with empty text');
|
|
668
|
+
}
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case 'item/commandExecution/outputDelta':
|
|
672
|
+
if (params?.output && this.handler) {
|
|
673
|
+
this.handler({
|
|
674
|
+
type: 'tool.result',
|
|
675
|
+
sessionId: this.threadId,
|
|
676
|
+
toolCallId: params.itemId ?? '',
|
|
677
|
+
output: String(params.output),
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
break;
|
|
681
|
+
case 'item/started': {
|
|
682
|
+
const item = asRecord(params?.item);
|
|
683
|
+
const callId = readItemId(item);
|
|
684
|
+
const toolContext = item ? extractToolContext(item) : null;
|
|
685
|
+
if (callId && toolContext) {
|
|
686
|
+
this.toolContextByCallId.set(callId, toolContext);
|
|
687
|
+
if (this.handler) {
|
|
688
|
+
this.handler({
|
|
689
|
+
type: 'tool.call',
|
|
690
|
+
sessionId: this.threadId,
|
|
691
|
+
toolName: toolContext.toolName,
|
|
692
|
+
input: toolContext.input,
|
|
693
|
+
toolCallId: callId,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
case 'item/completed': {
|
|
700
|
+
const item = asRecord(params?.item) ?? params;
|
|
701
|
+
const callId = readItemId(item);
|
|
702
|
+
if (callId)
|
|
703
|
+
this.toolContextByCallId.delete(callId);
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
case 'turn/aborted': {
|
|
707
|
+
this.turnStartInFlight = false;
|
|
708
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
709
|
+
const reason = readString(params?.reason) ?? readString(asRecord(params?.turn)?.reason);
|
|
710
|
+
log.info({ threadId: this.threadId, reason }, 'turn interrupted');
|
|
711
|
+
audit.emit('session.interrupted', {
|
|
712
|
+
agent: 'codex',
|
|
713
|
+
sessionId: this.threadId,
|
|
714
|
+
...(reason ? { reason } : {}),
|
|
715
|
+
});
|
|
716
|
+
if (this.handler) {
|
|
717
|
+
this.handler({
|
|
718
|
+
type: 'session.interrupted',
|
|
719
|
+
sessionId: this.threadId,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
break;
|
|
723
|
+
}
|
|
724
|
+
case 'turn/completed': {
|
|
725
|
+
this.turnStartInFlight = false;
|
|
726
|
+
this.interruptRequestedDuringTurnStart = false;
|
|
727
|
+
const turn = asRecord(params?.turn);
|
|
728
|
+
const turnStatus = normalizeTurnStatus(turn?.status);
|
|
729
|
+
const rawUsage = asRecord(params?.usage);
|
|
730
|
+
const usage = rawUsage
|
|
731
|
+
? {
|
|
732
|
+
inputTokens: Number(rawUsage.input_tokens ?? rawUsage.inputTokens ?? 0),
|
|
733
|
+
outputTokens: Number(rawUsage.output_tokens ?? rawUsage.outputTokens ?? 0),
|
|
734
|
+
}
|
|
735
|
+
: undefined;
|
|
736
|
+
if (turnStatus && INTERRUPTED_TURN_STATUSES.has(turnStatus)) {
|
|
737
|
+
log.info({ threadId: this.threadId, status: turnStatus }, 'turn interrupted');
|
|
738
|
+
audit.emit('session.interrupted', {
|
|
739
|
+
agent: 'codex',
|
|
740
|
+
sessionId: this.threadId,
|
|
741
|
+
status: turnStatus,
|
|
742
|
+
...(usage ? { usage } : {}),
|
|
743
|
+
});
|
|
744
|
+
if (this.handler) {
|
|
745
|
+
this.handler({
|
|
746
|
+
type: 'session.interrupted',
|
|
747
|
+
sessionId: this.threadId,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
break;
|
|
751
|
+
}
|
|
752
|
+
log.info({ threadId: this.threadId, status: turnStatus ?? 'completed' }, 'turn completed');
|
|
753
|
+
audit.emit('session.done', { agent: 'codex', sessionId: this.threadId, usage });
|
|
754
|
+
if (this.handler) {
|
|
755
|
+
this.handler({
|
|
756
|
+
type: 'session.done',
|
|
757
|
+
sessionId: this.threadId,
|
|
758
|
+
usage,
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
default:
|
|
764
|
+
if (method) {
|
|
765
|
+
log.debug({ method }, 'unhandled notification');
|
|
766
|
+
}
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
//# sourceMappingURL=codex.js.map
|