@vibelet/cli 0.1.37 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (323) hide show
  1. package/README.md +80 -0
  2. package/bin/cloudflared-quick-tunnel.mjs +11 -0
  3. package/bin/cloudflared-resolver.mjs +171 -0
  4. package/bin/vibelet-runtime-policy.mjs +36 -0
  5. package/bin/vibelet.cjs +12 -0
  6. package/bin/vibelet.mjs +1062 -0
  7. package/dist/index.cjs +126 -0
  8. package/package.json +25 -24
  9. package/app.json +0 -5
  10. package/dist/advertised-hosts.d.ts +0 -34
  11. package/dist/advertised-hosts.d.ts.map +0 -1
  12. package/dist/advertised-hosts.js +0 -176
  13. package/dist/advertised-hosts.js.map +0 -1
  14. package/dist/advertised-hosts.test.d.ts +0 -2
  15. package/dist/advertised-hosts.test.d.ts.map +0 -1
  16. package/dist/advertised-hosts.test.js +0 -96
  17. package/dist/advertised-hosts.test.js.map +0 -1
  18. package/dist/audit.d.ts +0 -30
  19. package/dist/audit.d.ts.map +0 -1
  20. package/dist/audit.js +0 -73
  21. package/dist/audit.js.map +0 -1
  22. package/dist/audit.test.d.ts +0 -2
  23. package/dist/audit.test.d.ts.map +0 -1
  24. package/dist/audit.test.js +0 -33
  25. package/dist/audit.test.js.map +0 -1
  26. package/dist/auth.d.ts +0 -6
  27. package/dist/auth.d.ts.map +0 -1
  28. package/dist/auth.js +0 -27
  29. package/dist/auth.js.map +0 -1
  30. package/dist/claude-hooks.d.ts +0 -58
  31. package/dist/claude-hooks.d.ts.map +0 -1
  32. package/dist/claude-hooks.js +0 -129
  33. package/dist/claude-hooks.js.map +0 -1
  34. package/dist/cli-version.d.ts +0 -3
  35. package/dist/cli-version.d.ts.map +0 -1
  36. package/dist/cli-version.js +0 -35
  37. package/dist/cli-version.js.map +0 -1
  38. package/dist/cli-version.test.d.ts +0 -2
  39. package/dist/cli-version.test.d.ts.map +0 -1
  40. package/dist/cli-version.test.js +0 -38
  41. package/dist/cli-version.test.js.map +0 -1
  42. package/dist/config.d.ts +0 -30
  43. package/dist/config.d.ts.map +0 -1
  44. package/dist/config.js +0 -327
  45. package/dist/config.js.map +0 -1
  46. package/dist/config.test.d.ts +0 -2
  47. package/dist/config.test.d.ts.map +0 -1
  48. package/dist/config.test.js +0 -184
  49. package/dist/config.test.js.map +0 -1
  50. package/dist/dev-auth.test.d.ts +0 -2
  51. package/dist/dev-auth.test.d.ts.map +0 -1
  52. package/dist/dev-auth.test.js +0 -154
  53. package/dist/dev-auth.test.js.map +0 -1
  54. package/dist/dev-script.test.d.ts +0 -2
  55. package/dist/dev-script.test.d.ts.map +0 -1
  56. package/dist/dev-script.test.js +0 -412
  57. package/dist/dev-script.test.js.map +0 -1
  58. package/dist/drivers/claude.d.ts +0 -34
  59. package/dist/drivers/claude.d.ts.map +0 -1
  60. package/dist/drivers/claude.js +0 -413
  61. package/dist/drivers/claude.js.map +0 -1
  62. package/dist/drivers/claude.test.d.ts +0 -2
  63. package/dist/drivers/claude.test.d.ts.map +0 -1
  64. package/dist/drivers/claude.test.js +0 -951
  65. package/dist/drivers/claude.test.js.map +0 -1
  66. package/dist/drivers/codex.d.ts +0 -38
  67. package/dist/drivers/codex.d.ts.map +0 -1
  68. package/dist/drivers/codex.js +0 -771
  69. package/dist/drivers/codex.js.map +0 -1
  70. package/dist/drivers/codex.test.d.ts +0 -2
  71. package/dist/drivers/codex.test.d.ts.map +0 -1
  72. package/dist/drivers/codex.test.js +0 -939
  73. package/dist/drivers/codex.test.js.map +0 -1
  74. package/dist/drivers/types.d.ts +0 -14
  75. package/dist/drivers/types.d.ts.map +0 -1
  76. package/dist/drivers/types.js +0 -2
  77. package/dist/drivers/types.js.map +0 -1
  78. package/dist/e2e.test.d.ts +0 -2
  79. package/dist/e2e.test.d.ts.map +0 -1
  80. package/dist/e2e.test.js +0 -111
  81. package/dist/e2e.test.js.map +0 -1
  82. package/dist/identity.d.ts +0 -10
  83. package/dist/identity.d.ts.map +0 -1
  84. package/dist/identity.js +0 -66
  85. package/dist/identity.js.map +0 -1
  86. package/dist/identity.test.d.ts +0 -2
  87. package/dist/identity.test.d.ts.map +0 -1
  88. package/dist/identity.test.js +0 -25
  89. package/dist/identity.test.js.map +0 -1
  90. package/dist/index-entry.test.d.ts +0 -2
  91. package/dist/index-entry.test.d.ts.map +0 -1
  92. package/dist/index-entry.test.js +0 -272
  93. package/dist/index-entry.test.js.map +0 -1
  94. package/dist/index.d.ts +0 -2
  95. package/dist/index.d.ts.map +0 -1
  96. package/dist/index.js +0 -707
  97. package/dist/index.js.map +0 -1
  98. package/dist/logger.d.ts +0 -31
  99. package/dist/logger.d.ts.map +0 -1
  100. package/dist/logger.js +0 -75
  101. package/dist/logger.js.map +0 -1
  102. package/dist/metrics.d.ts +0 -52
  103. package/dist/metrics.d.ts.map +0 -1
  104. package/dist/metrics.js +0 -89
  105. package/dist/metrics.js.map +0 -1
  106. package/dist/pairing-store.d.ts +0 -29
  107. package/dist/pairing-store.d.ts.map +0 -1
  108. package/dist/pairing-store.js +0 -131
  109. package/dist/pairing-store.js.map +0 -1
  110. package/dist/pairing-store.test.d.ts +0 -2
  111. package/dist/pairing-store.test.d.ts.map +0 -1
  112. package/dist/pairing-store.test.js +0 -47
  113. package/dist/pairing-store.test.js.map +0 -1
  114. package/dist/paths.d.ts +0 -16
  115. package/dist/paths.d.ts.map +0 -1
  116. package/dist/paths.js +0 -18
  117. package/dist/paths.js.map +0 -1
  118. package/dist/perf-compare.d.ts +0 -13
  119. package/dist/perf-compare.d.ts.map +0 -1
  120. package/dist/perf-compare.js +0 -125
  121. package/dist/perf-compare.js.map +0 -1
  122. package/dist/port-conflict.d.ts +0 -9
  123. package/dist/port-conflict.d.ts.map +0 -1
  124. package/dist/port-conflict.js +0 -33
  125. package/dist/port-conflict.js.map +0 -1
  126. package/dist/port-conflict.test.d.ts +0 -2
  127. package/dist/port-conflict.test.d.ts.map +0 -1
  128. package/dist/port-conflict.test.js +0 -38
  129. package/dist/port-conflict.test.js.map +0 -1
  130. package/dist/process-scanner.d.ts +0 -43
  131. package/dist/process-scanner.d.ts.map +0 -1
  132. package/dist/process-scanner.js +0 -453
  133. package/dist/process-scanner.js.map +0 -1
  134. package/dist/process-scanner.perf.test.d.ts +0 -2
  135. package/dist/process-scanner.perf.test.d.ts.map +0 -1
  136. package/dist/process-scanner.perf.test.js +0 -186
  137. package/dist/process-scanner.perf.test.js.map +0 -1
  138. package/dist/process-scanner.test.d.ts +0 -2
  139. package/dist/process-scanner.test.d.ts.map +0 -1
  140. package/dist/process-scanner.test.js +0 -399
  141. package/dist/process-scanner.test.js.map +0 -1
  142. package/dist/push-protocol.d.ts +0 -15
  143. package/dist/push-protocol.d.ts.map +0 -1
  144. package/dist/push-protocol.js +0 -23
  145. package/dist/push-protocol.js.map +0 -1
  146. package/dist/push-protocol.test.d.ts +0 -2
  147. package/dist/push-protocol.test.d.ts.map +0 -1
  148. package/dist/push-protocol.test.js +0 -57
  149. package/dist/push-protocol.test.js.map +0 -1
  150. package/dist/push-store.d.ts +0 -22
  151. package/dist/push-store.d.ts.map +0 -1
  152. package/dist/push-store.js +0 -103
  153. package/dist/push-store.js.map +0 -1
  154. package/dist/push-store.test.d.ts +0 -2
  155. package/dist/push-store.test.d.ts.map +0 -1
  156. package/dist/push-store.test.js +0 -79
  157. package/dist/push-store.test.js.map +0 -1
  158. package/dist/push.d.ts +0 -65
  159. package/dist/push.d.ts.map +0 -1
  160. package/dist/push.js +0 -202
  161. package/dist/push.js.map +0 -1
  162. package/dist/push.test.d.ts +0 -2
  163. package/dist/push.test.d.ts.map +0 -1
  164. package/dist/push.test.js +0 -199
  165. package/dist/push.test.js.map +0 -1
  166. package/dist/safe-stdio.d.ts +0 -3
  167. package/dist/safe-stdio.d.ts.map +0 -1
  168. package/dist/safe-stdio.js +0 -46
  169. package/dist/safe-stdio.js.map +0 -1
  170. package/dist/scanner.d.ts +0 -30
  171. package/dist/scanner.d.ts.map +0 -1
  172. package/dist/scanner.js +0 -859
  173. package/dist/scanner.js.map +0 -1
  174. package/dist/scanner.perf.test.d.ts +0 -2
  175. package/dist/scanner.perf.test.d.ts.map +0 -1
  176. package/dist/scanner.perf.test.js +0 -320
  177. package/dist/scanner.perf.test.js.map +0 -1
  178. package/dist/scanner.test.d.ts +0 -2
  179. package/dist/scanner.test.d.ts.map +0 -1
  180. package/dist/scanner.test.js +0 -948
  181. package/dist/scanner.test.js.map +0 -1
  182. package/dist/session-inventory.d.ts +0 -63
  183. package/dist/session-inventory.d.ts.map +0 -1
  184. package/dist/session-inventory.js +0 -525
  185. package/dist/session-inventory.js.map +0 -1
  186. package/dist/session-inventory.perf.test.d.ts +0 -2
  187. package/dist/session-inventory.perf.test.d.ts.map +0 -1
  188. package/dist/session-inventory.perf.test.js +0 -220
  189. package/dist/session-inventory.perf.test.js.map +0 -1
  190. package/dist/session-inventory.test.d.ts +0 -2
  191. package/dist/session-inventory.test.d.ts.map +0 -1
  192. package/dist/session-inventory.test.js +0 -712
  193. package/dist/session-inventory.test.js.map +0 -1
  194. package/dist/session-manager.d.ts +0 -75
  195. package/dist/session-manager.d.ts.map +0 -1
  196. package/dist/session-manager.js +0 -1515
  197. package/dist/session-manager.js.map +0 -1
  198. package/dist/session-manager.test.d.ts +0 -2
  199. package/dist/session-manager.test.d.ts.map +0 -1
  200. package/dist/session-manager.test.js +0 -2861
  201. package/dist/session-manager.test.js.map +0 -1
  202. package/dist/session-store.d.ts +0 -42
  203. package/dist/session-store.d.ts.map +0 -1
  204. package/dist/session-store.js +0 -163
  205. package/dist/session-store.js.map +0 -1
  206. package/dist/session-store.test.d.ts +0 -2
  207. package/dist/session-store.test.d.ts.map +0 -1
  208. package/dist/session-store.test.js +0 -236
  209. package/dist/session-store.test.js.map +0 -1
  210. package/dist/session-title.d.ts +0 -6
  211. package/dist/session-title.d.ts.map +0 -1
  212. package/dist/session-title.js +0 -105
  213. package/dist/session-title.js.map +0 -1
  214. package/dist/session-title.perf.test.d.ts +0 -2
  215. package/dist/session-title.perf.test.d.ts.map +0 -1
  216. package/dist/session-title.perf.test.js +0 -99
  217. package/dist/session-title.perf.test.js.map +0 -1
  218. package/dist/session-title.test.d.ts +0 -2
  219. package/dist/session-title.test.d.ts.map +0 -1
  220. package/dist/session-title.test.js +0 -199
  221. package/dist/session-title.test.js.map +0 -1
  222. package/dist/shutdown-endpoint.test.d.ts +0 -2
  223. package/dist/shutdown-endpoint.test.d.ts.map +0 -1
  224. package/dist/shutdown-endpoint.test.js +0 -93
  225. package/dist/shutdown-endpoint.test.js.map +0 -1
  226. package/dist/storage-housekeeping.d.ts +0 -28
  227. package/dist/storage-housekeeping.d.ts.map +0 -1
  228. package/dist/storage-housekeeping.js +0 -76
  229. package/dist/storage-housekeeping.js.map +0 -1
  230. package/dist/storage-housekeeping.test.d.ts +0 -2
  231. package/dist/storage-housekeeping.test.d.ts.map +0 -1
  232. package/dist/storage-housekeeping.test.js +0 -65
  233. package/dist/storage-housekeeping.test.js.map +0 -1
  234. package/dist/test-daemon-harness.d.ts +0 -31
  235. package/dist/test-daemon-harness.d.ts.map +0 -1
  236. package/dist/test-daemon-harness.js +0 -337
  237. package/dist/test-daemon-harness.js.map +0 -1
  238. package/dist/token-auth.test.d.ts +0 -2
  239. package/dist/token-auth.test.d.ts.map +0 -1
  240. package/dist/token-auth.test.js +0 -52
  241. package/dist/token-auth.test.js.map +0 -1
  242. package/dist/utils.d.ts +0 -4
  243. package/dist/utils.d.ts.map +0 -1
  244. package/dist/utils.js +0 -40
  245. package/dist/utils.js.map +0 -1
  246. package/dist/utils.test.d.ts +0 -2
  247. package/dist/utils.test.d.ts.map +0 -1
  248. package/dist/utils.test.js +0 -54
  249. package/dist/utils.test.js.map +0 -1
  250. package/dist/ws-data.d.ts +0 -4
  251. package/dist/ws-data.d.ts.map +0 -1
  252. package/dist/ws-data.js +0 -20
  253. package/dist/ws-data.js.map +0 -1
  254. package/dist/ws-data.test.d.ts +0 -2
  255. package/dist/ws-data.test.d.ts.map +0 -1
  256. package/dist/ws-data.test.js +0 -17
  257. package/dist/ws-data.test.js.map +0 -1
  258. package/perf-reporter.mjs +0 -138
  259. package/scripts/build-release.mjs +0 -41
  260. package/scripts/dev.mjs +0 -537
  261. package/src/advertised-hosts.test.ts +0 -125
  262. package/src/advertised-hosts.ts +0 -225
  263. package/src/audit.test.ts +0 -38
  264. package/src/audit.ts +0 -117
  265. package/src/auth.ts +0 -31
  266. package/src/claude-hooks.ts +0 -195
  267. package/src/cli-version.test.ts +0 -36
  268. package/src/cli-version.ts +0 -46
  269. package/src/config.test.ts +0 -254
  270. package/src/config.ts +0 -324
  271. package/src/dev-auth.test.ts +0 -183
  272. package/src/dev-script.test.ts +0 -511
  273. package/src/drivers/claude.test.ts +0 -1186
  274. package/src/drivers/claude.ts +0 -443
  275. package/src/drivers/codex.test.ts +0 -1096
  276. package/src/drivers/codex.ts +0 -879
  277. package/src/drivers/types.ts +0 -15
  278. package/src/e2e.test.ts +0 -139
  279. package/src/identity.test.ts +0 -26
  280. package/src/identity.ts +0 -82
  281. package/src/index-entry.test.ts +0 -336
  282. package/src/index.ts +0 -781
  283. package/src/logger.ts +0 -112
  284. package/src/metrics.ts +0 -117
  285. package/src/pairing-store.test.ts +0 -53
  286. package/src/pairing-store.ts +0 -154
  287. package/src/paths.ts +0 -19
  288. package/src/perf-compare.ts +0 -164
  289. package/src/port-conflict.test.ts +0 -45
  290. package/src/port-conflict.ts +0 -44
  291. package/src/process-scanner.perf.test.ts +0 -222
  292. package/src/process-scanner.test.ts +0 -575
  293. package/src/process-scanner.ts +0 -514
  294. package/src/push-protocol.test.ts +0 -74
  295. package/src/push-protocol.ts +0 -36
  296. package/src/push-store.test.ts +0 -89
  297. package/src/push-store.ts +0 -126
  298. package/src/push.test.ts +0 -234
  299. package/src/push.ts +0 -318
  300. package/src/safe-stdio.ts +0 -51
  301. package/src/scanner.perf.test.ts +0 -359
  302. package/src/scanner.test.ts +0 -1045
  303. package/src/scanner.ts +0 -924
  304. package/src/session-inventory.perf.test.ts +0 -250
  305. package/src/session-inventory.test.ts +0 -1002
  306. package/src/session-inventory.ts +0 -721
  307. package/src/session-manager.test.ts +0 -3430
  308. package/src/session-manager.ts +0 -1775
  309. package/src/session-store.test.ts +0 -276
  310. package/src/session-store.ts +0 -202
  311. package/src/session-title.perf.test.ts +0 -118
  312. package/src/session-title.test.ts +0 -286
  313. package/src/session-title.ts +0 -108
  314. package/src/shutdown-endpoint.test.ts +0 -95
  315. package/src/storage-housekeeping.test.ts +0 -78
  316. package/src/storage-housekeeping.ts +0 -111
  317. package/src/test-daemon-harness.ts +0 -410
  318. package/src/token-auth.test.ts +0 -67
  319. package/src/utils.test.ts +0 -65
  320. package/src/utils.ts +0 -47
  321. package/src/ws-data.test.ts +0 -20
  322. package/src/ws-data.ts +0 -26
  323. package/tsconfig.json +0 -12
@@ -1,771 +0,0 @@
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