@getpaseo/server 0.1.37 → 0.1.38

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 (295) hide show
  1. package/dist/scripts/dev-runner.js +1 -1
  2. package/dist/scripts/dev-runner.js.map +1 -1
  3. package/dist/scripts/{daemon-runner.js → supervisor-entrypoint.js} +7 -9
  4. package/dist/scripts/supervisor-entrypoint.js.map +1 -0
  5. package/dist/scripts/supervisor.js +17 -1
  6. package/dist/scripts/supervisor.js.map +1 -1
  7. package/dist/server/server/bootstrap.d.ts +0 -4
  8. package/dist/server/server/bootstrap.d.ts.map +1 -1
  9. package/dist/server/server/bootstrap.js +8 -22
  10. package/dist/server/server/bootstrap.js.map +1 -1
  11. package/dist/server/server/index.js +0 -4
  12. package/dist/server/server/index.js.map +1 -1
  13. package/dist/server/server/pid-lock.d.ts +7 -2
  14. package/dist/server/server/pid-lock.d.ts.map +1 -1
  15. package/dist/server/server/pid-lock.js +21 -0
  16. package/dist/server/server/pid-lock.js.map +1 -1
  17. package/dist/server/server/session.d.ts.map +1 -1
  18. package/dist/server/server/session.js +2 -2
  19. package/dist/server/server/session.js.map +1 -1
  20. package/dist/src/server/pid-lock.js +21 -0
  21. package/dist/src/server/pid-lock.js.map +1 -1
  22. package/package.json +3 -3
  23. package/dist/scripts/daemon-runner.js.map +0 -1
  24. package/dist/src/server/agent/activity-curator.js +0 -243
  25. package/dist/src/server/agent/activity-curator.js.map +0 -1
  26. package/dist/src/server/agent/agent-manager.js +0 -1855
  27. package/dist/src/server/agent/agent-manager.js.map +0 -1
  28. package/dist/src/server/agent/agent-metadata-generator.js +0 -161
  29. package/dist/src/server/agent/agent-metadata-generator.js.map +0 -1
  30. package/dist/src/server/agent/agent-projections.js +0 -254
  31. package/dist/src/server/agent/agent-projections.js.map +0 -1
  32. package/dist/src/server/agent/agent-response-loop.js +0 -304
  33. package/dist/src/server/agent/agent-response-loop.js.map +0 -1
  34. package/dist/src/server/agent/agent-sdk-types.js +0 -12
  35. package/dist/src/server/agent/agent-sdk-types.js.map +0 -1
  36. package/dist/src/server/agent/agent-storage.js +0 -302
  37. package/dist/src/server/agent/agent-storage.js.map +0 -1
  38. package/dist/src/server/agent/agent-title-limits.js +0 -3
  39. package/dist/src/server/agent/agent-title-limits.js.map +0 -1
  40. package/dist/src/server/agent/audio-utils.js +0 -19
  41. package/dist/src/server/agent/audio-utils.js.map +0 -1
  42. package/dist/src/server/agent/dictation-debug.js +0 -50
  43. package/dist/src/server/agent/dictation-debug.js.map +0 -1
  44. package/dist/src/server/agent/mcp-server.js +0 -754
  45. package/dist/src/server/agent/mcp-server.js.map +0 -1
  46. package/dist/src/server/agent/orchestrator-instructions.js +0 -51
  47. package/dist/src/server/agent/orchestrator-instructions.js.map +0 -1
  48. package/dist/src/server/agent/pcm16-resampler.js +0 -63
  49. package/dist/src/server/agent/pcm16-resampler.js.map +0 -1
  50. package/dist/src/server/agent/provider-launch-config.js +0 -213
  51. package/dist/src/server/agent/provider-launch-config.js.map +0 -1
  52. package/dist/src/server/agent/provider-manifest.js +0 -127
  53. package/dist/src/server/agent/provider-manifest.js.map +0 -1
  54. package/dist/src/server/agent/provider-registry.js +0 -45
  55. package/dist/src/server/agent/provider-registry.js.map +0 -1
  56. package/dist/src/server/agent/providers/claude/partial-json.js +0 -306
  57. package/dist/src/server/agent/providers/claude/partial-json.js.map +0 -1
  58. package/dist/src/server/agent/providers/claude/sdk-model-resolver.js +0 -104
  59. package/dist/src/server/agent/providers/claude/sdk-model-resolver.js.map +0 -1
  60. package/dist/src/server/agent/providers/claude/sidechain-tracker.js +0 -230
  61. package/dist/src/server/agent/providers/claude/sidechain-tracker.js.map +0 -1
  62. package/dist/src/server/agent/providers/claude/task-notification-tool-call.js +0 -267
  63. package/dist/src/server/agent/providers/claude/task-notification-tool-call.js.map +0 -1
  64. package/dist/src/server/agent/providers/claude/tool-call-detail-parser.js +0 -121
  65. package/dist/src/server/agent/providers/claude/tool-call-detail-parser.js.map +0 -1
  66. package/dist/src/server/agent/providers/claude/tool-call-mapper.js +0 -252
  67. package/dist/src/server/agent/providers/claude/tool-call-mapper.js.map +0 -1
  68. package/dist/src/server/agent/providers/claude-agent.js +0 -3166
  69. package/dist/src/server/agent/providers/claude-agent.js.map +0 -1
  70. package/dist/src/server/agent/providers/codex/tool-call-detail-parser.js +0 -104
  71. package/dist/src/server/agent/providers/codex/tool-call-detail-parser.js.map +0 -1
  72. package/dist/src/server/agent/providers/codex/tool-call-mapper.js +0 -758
  73. package/dist/src/server/agent/providers/codex/tool-call-mapper.js.map +0 -1
  74. package/dist/src/server/agent/providers/codex-app-server-agent.js +0 -2949
  75. package/dist/src/server/agent/providers/codex-app-server-agent.js.map +0 -1
  76. package/dist/src/server/agent/providers/codex-rollout-timeline.js +0 -544
  77. package/dist/src/server/agent/providers/codex-rollout-timeline.js.map +0 -1
  78. package/dist/src/server/agent/providers/opencode/tool-call-detail-parser.js +0 -39
  79. package/dist/src/server/agent/providers/opencode/tool-call-detail-parser.js.map +0 -1
  80. package/dist/src/server/agent/providers/opencode/tool-call-mapper.js +0 -144
  81. package/dist/src/server/agent/providers/opencode/tool-call-mapper.js.map +0 -1
  82. package/dist/src/server/agent/providers/opencode-agent.js +0 -1193
  83. package/dist/src/server/agent/providers/opencode-agent.js.map +0 -1
  84. package/dist/src/server/agent/providers/tool-call-detail-primitives.js +0 -686
  85. package/dist/src/server/agent/providers/tool-call-detail-primitives.js.map +0 -1
  86. package/dist/src/server/agent/providers/tool-call-mapper-utils.js +0 -115
  87. package/dist/src/server/agent/providers/tool-call-mapper-utils.js.map +0 -1
  88. package/dist/src/server/agent/recordings-debug.js +0 -19
  89. package/dist/src/server/agent/recordings-debug.js.map +0 -1
  90. package/dist/src/server/agent/stt-debug.js +0 -33
  91. package/dist/src/server/agent/stt-debug.js.map +0 -1
  92. package/dist/src/server/agent/stt-manager.js +0 -232
  93. package/dist/src/server/agent/stt-manager.js.map +0 -1
  94. package/dist/src/server/agent/timeline-append.js +0 -27
  95. package/dist/src/server/agent/timeline-append.js.map +0 -1
  96. package/dist/src/server/agent/timeline-projection.js +0 -215
  97. package/dist/src/server/agent/timeline-projection.js.map +0 -1
  98. package/dist/src/server/agent/tool-name-normalization.js +0 -45
  99. package/dist/src/server/agent/tool-name-normalization.js.map +0 -1
  100. package/dist/src/server/agent/tts-debug.js +0 -24
  101. package/dist/src/server/agent/tts-debug.js.map +0 -1
  102. package/dist/src/server/agent/tts-manager.js +0 -374
  103. package/dist/src/server/agent/tts-manager.js.map +0 -1
  104. package/dist/src/server/agent/wait-for-agent-tracker.js +0 -53
  105. package/dist/src/server/agent/wait-for-agent-tracker.js.map +0 -1
  106. package/dist/src/server/agent-attention-policy.js +0 -40
  107. package/dist/src/server/agent-attention-policy.js.map +0 -1
  108. package/dist/src/server/allowed-hosts.js +0 -94
  109. package/dist/src/server/allowed-hosts.js.map +0 -1
  110. package/dist/src/server/bootstrap.js +0 -620
  111. package/dist/src/server/bootstrap.js.map +0 -1
  112. package/dist/src/server/chat/chat-mentions.js +0 -71
  113. package/dist/src/server/chat/chat-mentions.js.map +0 -1
  114. package/dist/src/server/chat/chat-rpc-schemas.js +0 -103
  115. package/dist/src/server/chat/chat-rpc-schemas.js.map +0 -1
  116. package/dist/src/server/chat/chat-service.js +0 -330
  117. package/dist/src/server/chat/chat-service.js.map +0 -1
  118. package/dist/src/server/chat/chat-types.js +0 -22
  119. package/dist/src/server/chat/chat-types.js.map +0 -1
  120. package/dist/src/server/checkout-diff-manager.js +0 -272
  121. package/dist/src/server/checkout-diff-manager.js.map +0 -1
  122. package/dist/src/server/checkout-git-utils.js +0 -37
  123. package/dist/src/server/checkout-git-utils.js.map +0 -1
  124. package/dist/src/server/client-message-id.js +0 -12
  125. package/dist/src/server/client-message-id.js.map +0 -1
  126. package/dist/src/server/config.js +0 -73
  127. package/dist/src/server/config.js.map +0 -1
  128. package/dist/src/server/connection-offer.js +0 -59
  129. package/dist/src/server/connection-offer.js.map +0 -1
  130. package/dist/src/server/daemon-keypair.js +0 -40
  131. package/dist/src/server/daemon-keypair.js.map +0 -1
  132. package/dist/src/server/daemon-version.js +0 -22
  133. package/dist/src/server/daemon-version.js.map +0 -1
  134. package/dist/src/server/dictation/dictation-stream-manager.js +0 -571
  135. package/dist/src/server/dictation/dictation-stream-manager.js.map +0 -1
  136. package/dist/src/server/file-download/token-store.js +0 -40
  137. package/dist/src/server/file-download/token-store.js.map +0 -1
  138. package/dist/src/server/file-explorer/service.js +0 -180
  139. package/dist/src/server/file-explorer/service.js.map +0 -1
  140. package/dist/src/server/json-utils.js +0 -45
  141. package/dist/src/server/json-utils.js.map +0 -1
  142. package/dist/src/server/loop/rpc-schemas.js +0 -159
  143. package/dist/src/server/loop/rpc-schemas.js.map +0 -1
  144. package/dist/src/server/loop-service.js +0 -741
  145. package/dist/src/server/loop-service.js.map +0 -1
  146. package/dist/src/server/messages.js +0 -29
  147. package/dist/src/server/messages.js.map +0 -1
  148. package/dist/src/server/package-version.js +0 -46
  149. package/dist/src/server/package-version.js.map +0 -1
  150. package/dist/src/server/pairing-offer.js +0 -45
  151. package/dist/src/server/pairing-offer.js.map +0 -1
  152. package/dist/src/server/pairing-qr.js +0 -45
  153. package/dist/src/server/pairing-qr.js.map +0 -1
  154. package/dist/src/server/path-utils.js +0 -20
  155. package/dist/src/server/path-utils.js.map +0 -1
  156. package/dist/src/server/persisted-config.js +0 -265
  157. package/dist/src/server/persisted-config.js.map +0 -1
  158. package/dist/src/server/persistence-hooks.js +0 -60
  159. package/dist/src/server/persistence-hooks.js.map +0 -1
  160. package/dist/src/server/push/push-service.js +0 -68
  161. package/dist/src/server/push/push-service.js.map +0 -1
  162. package/dist/src/server/push/token-store.js +0 -70
  163. package/dist/src/server/push/token-store.js.map +0 -1
  164. package/dist/src/server/relay-transport.js +0 -461
  165. package/dist/src/server/relay-transport.js.map +0 -1
  166. package/dist/src/server/schedule/cron.js +0 -103
  167. package/dist/src/server/schedule/cron.js.map +0 -1
  168. package/dist/src/server/schedule/rpc-schemas.js +0 -112
  169. package/dist/src/server/schedule/rpc-schemas.js.map +0 -1
  170. package/dist/src/server/schedule/service.js +0 -397
  171. package/dist/src/server/schedule/service.js.map +0 -1
  172. package/dist/src/server/schedule/store.js +0 -56
  173. package/dist/src/server/schedule/store.js.map +0 -1
  174. package/dist/src/server/schedule/types.js +0 -73
  175. package/dist/src/server/schedule/types.js.map +0 -1
  176. package/dist/src/server/server-id.js +0 -63
  177. package/dist/src/server/server-id.js.map +0 -1
  178. package/dist/src/server/session.js +0 -6381
  179. package/dist/src/server/session.js.map +0 -1
  180. package/dist/src/server/speech/audio.js +0 -101
  181. package/dist/src/server/speech/audio.js.map +0 -1
  182. package/dist/src/server/speech/provider-resolver.js +0 -7
  183. package/dist/src/server/speech/provider-resolver.js.map +0 -1
  184. package/dist/src/server/speech/providers/local/config.js +0 -74
  185. package/dist/src/server/speech/providers/local/config.js.map +0 -1
  186. package/dist/src/server/speech/providers/local/models.js +0 -17
  187. package/dist/src/server/speech/providers/local/models.js.map +0 -1
  188. package/dist/src/server/speech/providers/local/pocket/pocket-tts-onnx.js +0 -436
  189. package/dist/src/server/speech/providers/local/pocket/pocket-tts-onnx.js.map +0 -1
  190. package/dist/src/server/speech/providers/local/runtime.js +0 -238
  191. package/dist/src/server/speech/providers/local/runtime.js.map +0 -1
  192. package/dist/src/server/speech/providers/local/sherpa/model-catalog.js +0 -166
  193. package/dist/src/server/speech/providers/local/sherpa/model-catalog.js.map +0 -1
  194. package/dist/src/server/speech/providers/local/sherpa/model-downloader.js +0 -165
  195. package/dist/src/server/speech/providers/local/sherpa/model-downloader.js.map +0 -1
  196. package/dist/src/server/speech/providers/local/sherpa/sherpa-offline-recognizer.js +0 -73
  197. package/dist/src/server/speech/providers/local/sherpa/sherpa-offline-recognizer.js.map +0 -1
  198. package/dist/src/server/speech/providers/local/sherpa/sherpa-online-recognizer.js +0 -84
  199. package/dist/src/server/speech/providers/local/sherpa/sherpa-online-recognizer.js.map +0 -1
  200. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-loader.js +0 -11
  201. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-loader.js.map +0 -1
  202. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-node-loader.js +0 -102
  203. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-node-loader.js.map +0 -1
  204. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-realtime-session.js +0 -135
  205. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-realtime-session.js.map +0 -1
  206. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-stt.js +0 -130
  207. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-stt.js.map +0 -1
  208. package/dist/src/server/speech/providers/local/sherpa/sherpa-realtime-session.js +0 -110
  209. package/dist/src/server/speech/providers/local/sherpa/sherpa-realtime-session.js.map +0 -1
  210. package/dist/src/server/speech/providers/local/sherpa/sherpa-stt.js +0 -138
  211. package/dist/src/server/speech/providers/local/sherpa/sherpa-stt.js.map +0 -1
  212. package/dist/src/server/speech/providers/local/sherpa/sherpa-tts.js +0 -98
  213. package/dist/src/server/speech/providers/local/sherpa/sherpa-tts.js.map +0 -1
  214. package/dist/src/server/speech/providers/local/sherpa/silero-vad-provider.js +0 -23
  215. package/dist/src/server/speech/providers/local/sherpa/silero-vad-provider.js.map +0 -1
  216. package/dist/src/server/speech/providers/local/sherpa/silero-vad-session.js +0 -107
  217. package/dist/src/server/speech/providers/local/sherpa/silero-vad-session.js.map +0 -1
  218. package/dist/src/server/speech/providers/openai/config.js +0 -80
  219. package/dist/src/server/speech/providers/openai/config.js.map +0 -1
  220. package/dist/src/server/speech/providers/openai/realtime-transcription-session.js +0 -168
  221. package/dist/src/server/speech/providers/openai/realtime-transcription-session.js.map +0 -1
  222. package/dist/src/server/speech/providers/openai/runtime.js +0 -112
  223. package/dist/src/server/speech/providers/openai/runtime.js.map +0 -1
  224. package/dist/src/server/speech/providers/openai/stt.js +0 -206
  225. package/dist/src/server/speech/providers/openai/stt.js.map +0 -1
  226. package/dist/src/server/speech/providers/openai/tts.js +0 -46
  227. package/dist/src/server/speech/providers/openai/tts.js.map +0 -1
  228. package/dist/src/server/speech/speech-config-resolver.js +0 -102
  229. package/dist/src/server/speech/speech-config-resolver.js.map +0 -1
  230. package/dist/src/server/speech/speech-provider.js +0 -2
  231. package/dist/src/server/speech/speech-provider.js.map +0 -1
  232. package/dist/src/server/speech/speech-runtime.js +0 -530
  233. package/dist/src/server/speech/speech-runtime.js.map +0 -1
  234. package/dist/src/server/speech/speech-types.js +0 -8
  235. package/dist/src/server/speech/speech-types.js.map +0 -1
  236. package/dist/src/server/speech/turn-detection-provider.js +0 -2
  237. package/dist/src/server/speech/turn-detection-provider.js.map +0 -1
  238. package/dist/src/server/utils/diff-highlighter.js +0 -257
  239. package/dist/src/server/utils/diff-highlighter.js.map +0 -1
  240. package/dist/src/server/voice/fixed-duration-pcm-ring-buffer.js +0 -35
  241. package/dist/src/server/voice/fixed-duration-pcm-ring-buffer.js.map +0 -1
  242. package/dist/src/server/voice/voice-turn-controller.js +0 -159
  243. package/dist/src/server/voice/voice-turn-controller.js.map +0 -1
  244. package/dist/src/server/voice-config.js +0 -51
  245. package/dist/src/server/voice-config.js.map +0 -1
  246. package/dist/src/server/voice-mcp-bridge-command.js +0 -31
  247. package/dist/src/server/voice-mcp-bridge-command.js.map +0 -1
  248. package/dist/src/server/voice-mcp-bridge.js +0 -109
  249. package/dist/src/server/voice-mcp-bridge.js.map +0 -1
  250. package/dist/src/server/voice-permission-policy.js +0 -13
  251. package/dist/src/server/voice-permission-policy.js.map +0 -1
  252. package/dist/src/server/voice-types.js +0 -2
  253. package/dist/src/server/voice-types.js.map +0 -1
  254. package/dist/src/server/websocket-server.js +0 -1048
  255. package/dist/src/server/websocket-server.js.map +0 -1
  256. package/dist/src/server/workspace-registry-bootstrap.js +0 -98
  257. package/dist/src/server/workspace-registry-bootstrap.js.map +0 -1
  258. package/dist/src/server/workspace-registry-model.js +0 -175
  259. package/dist/src/server/workspace-registry-model.js.map +0 -1
  260. package/dist/src/server/workspace-registry.js +0 -151
  261. package/dist/src/server/workspace-registry.js.map +0 -1
  262. package/dist/src/server/worktree-bootstrap.js +0 -508
  263. package/dist/src/server/worktree-bootstrap.js.map +0 -1
  264. package/dist/src/shared/agent-attention-notification.js +0 -130
  265. package/dist/src/shared/agent-attention-notification.js.map +0 -1
  266. package/dist/src/shared/agent-lifecycle.js +0 -8
  267. package/dist/src/shared/agent-lifecycle.js.map +0 -1
  268. package/dist/src/shared/connection-offer.js +0 -17
  269. package/dist/src/shared/connection-offer.js.map +0 -1
  270. package/dist/src/shared/daemon-endpoints.js +0 -122
  271. package/dist/src/shared/daemon-endpoints.js.map +0 -1
  272. package/dist/src/shared/messages.js +0 -2107
  273. package/dist/src/shared/messages.js.map +0 -1
  274. package/dist/src/shared/path-utils.js +0 -16
  275. package/dist/src/shared/path-utils.js.map +0 -1
  276. package/dist/src/shared/terminal-stream-protocol.js +0 -99
  277. package/dist/src/shared/terminal-stream-protocol.js.map +0 -1
  278. package/dist/src/shared/tool-call-display.js +0 -122
  279. package/dist/src/shared/tool-call-display.js.map +0 -1
  280. package/dist/src/terminal/terminal-manager.js +0 -136
  281. package/dist/src/terminal/terminal-manager.js.map +0 -1
  282. package/dist/src/terminal/terminal.js +0 -333
  283. package/dist/src/terminal/terminal.js.map +0 -1
  284. package/dist/src/utils/checkout-git.js +0 -1518
  285. package/dist/src/utils/checkout-git.js.map +0 -1
  286. package/dist/src/utils/directory-suggestions.js +0 -671
  287. package/dist/src/utils/directory-suggestions.js.map +0 -1
  288. package/dist/src/utils/path.js +0 -15
  289. package/dist/src/utils/path.js.map +0 -1
  290. package/dist/src/utils/project-icon.js +0 -389
  291. package/dist/src/utils/project-icon.js.map +0 -1
  292. package/dist/src/utils/worktree-metadata.js +0 -116
  293. package/dist/src/utils/worktree-metadata.js.map +0 -1
  294. package/dist/src/utils/worktree.js +0 -744
  295. package/dist/src/utils/worktree.js.map +0 -1
@@ -1,2949 +0,0 @@
1
- import { execSync, spawn } from "node:child_process";
2
- import { randomUUID } from "node:crypto";
3
- import { existsSync } from "node:fs";
4
- import fs from "node:fs/promises";
5
- import os from "node:os";
6
- import path from "node:path";
7
- import readline from "node:readline";
8
- import { z } from "zod";
9
- import { loadCodexPersistedTimeline } from "./codex-rollout-timeline.js";
10
- import { mapCodexRolloutToolCall, mapCodexToolCallFromThreadItem, } from "./codex/tool-call-mapper.js";
11
- import { applyProviderEnv, findExecutable, resolveProviderCommandPrefix, } from "../provider-launch-config.js";
12
- import { extractCodexTerminalSessionId, nonEmptyString } from "./tool-call-mapper-utils.js";
13
- const DEFAULT_TIMEOUT_MS = 14 * 24 * 60 * 60 * 1000;
14
- const TURN_START_TIMEOUT_MS = 90 * 1000;
15
- const CODEX_PROVIDER = "codex";
16
- const CODEX_IMAGE_ATTACHMENT_DIR = "paseo-attachments";
17
- const CODEX_APP_SERVER_CAPABILITIES = {
18
- supportsStreaming: true,
19
- supportsSessionPersistence: true,
20
- supportsDynamicModes: false,
21
- supportsMcpServers: true,
22
- supportsReasoningStream: true,
23
- supportsToolInvocations: true,
24
- };
25
- const CODEX_MODES = [
26
- {
27
- id: "read-only",
28
- label: "Read Only",
29
- description: "Read files and answer questions. Manual approval required for edits, commands, or network ops.",
30
- },
31
- {
32
- id: "auto",
33
- label: "Auto",
34
- description: "Edit files and run commands but still request approval before escalating scope.",
35
- },
36
- {
37
- id: "full-access",
38
- label: "Full Access",
39
- description: "Edit files, run commands, and access the network without additional prompts.",
40
- },
41
- ];
42
- const DEFAULT_CODEX_MODE_ID = "auto";
43
- const MODE_PRESETS = {
44
- "read-only": {
45
- approvalPolicy: "on-request",
46
- sandbox: "read-only",
47
- },
48
- auto: {
49
- approvalPolicy: "on-request",
50
- sandbox: "workspace-write",
51
- },
52
- "full-access": {
53
- approvalPolicy: "never",
54
- sandbox: "danger-full-access",
55
- networkAccess: true,
56
- },
57
- };
58
- function validateCodexMode(modeId) {
59
- if (!(modeId in MODE_PRESETS)) {
60
- const validModes = Object.keys(MODE_PRESETS).join(", ");
61
- throw new Error(`Invalid Codex mode "${modeId}". Valid modes are: ${validModes}`);
62
- }
63
- }
64
- function normalizeCodexThinkingOptionId(thinkingOptionId) {
65
- if (typeof thinkingOptionId !== "string") {
66
- return undefined;
67
- }
68
- const normalized = thinkingOptionId.trim();
69
- if (!normalized || normalized === "default") {
70
- return undefined;
71
- }
72
- return normalized;
73
- }
74
- function normalizeCodexModelId(modelId) {
75
- if (typeof modelId !== "string") {
76
- return undefined;
77
- }
78
- const normalized = modelId.trim();
79
- if (!normalized) {
80
- return undefined;
81
- }
82
- return normalized;
83
- }
84
- function normalizeCodexModelLabel(displayName) {
85
- return displayName.replace(/\bgpt\b/gi, "GPT");
86
- }
87
- function mergeCodexConfiguredDefaults(primary, fallback) {
88
- return {
89
- model: primary.model ?? fallback.model,
90
- thinkingOptionId: primary.thinkingOptionId ?? fallback.thinkingOptionId,
91
- };
92
- }
93
- function resolveCodexBinary() {
94
- const found = findExecutable("codex");
95
- if (found) {
96
- return found;
97
- }
98
- throw new Error("Codex binary not found. Install the Codex CLI (https://github.com/openai/codex) and ensure it is available in your shell PATH.");
99
- }
100
- function resolveCodexLaunchPrefix(runtimeSettings) {
101
- return resolveProviderCommandPrefix(runtimeSettings?.command, resolveCodexBinary);
102
- }
103
- function resolveCodexHomeDir() {
104
- return process.env.CODEX_HOME ?? path.join(os.homedir(), ".codex");
105
- }
106
- function tokenizeCommandArgs(args) {
107
- const tokens = [];
108
- let current = "";
109
- let quote = null;
110
- for (let i = 0; i < args.length; i += 1) {
111
- const ch = args[i];
112
- if (quote) {
113
- if (ch === quote) {
114
- quote = null;
115
- continue;
116
- }
117
- if (ch === "\\" && i + 1 < args.length) {
118
- const next = args[i + 1];
119
- if (next === quote || next === "\\" || next === "n" || next === "t") {
120
- i += 1;
121
- current += next === "n" ? "\n" : next === "t" ? "\t" : next;
122
- continue;
123
- }
124
- }
125
- current += ch;
126
- continue;
127
- }
128
- if (ch === "'" || ch === '"') {
129
- quote = ch;
130
- continue;
131
- }
132
- if (/\s/.test(ch)) {
133
- if (current) {
134
- tokens.push(current);
135
- current = "";
136
- }
137
- continue;
138
- }
139
- current += ch;
140
- }
141
- if (current) {
142
- tokens.push(current);
143
- }
144
- return tokens;
145
- }
146
- function parseFrontMatter(markdown) {
147
- const lines = markdown.split("\n");
148
- if (lines[0]?.trim() !== "---") {
149
- return { frontMatter: {}, body: markdown };
150
- }
151
- let end = -1;
152
- for (let i = 1; i < lines.length; i += 1) {
153
- if (lines[i]?.trim() === "---") {
154
- end = i;
155
- break;
156
- }
157
- }
158
- if (end === -1) {
159
- return { frontMatter: {}, body: markdown };
160
- }
161
- const metaLines = lines.slice(1, end);
162
- const body = lines.slice(end + 1).join("\n");
163
- const frontMatter = {};
164
- for (const line of metaLines) {
165
- const trimmed = line.trim();
166
- if (!trimmed || trimmed.startsWith("#")) {
167
- continue;
168
- }
169
- const idx = trimmed.indexOf(":");
170
- if (idx <= 0) {
171
- continue;
172
- }
173
- const key = trimmed.slice(0, idx).trim();
174
- let value = trimmed.slice(idx + 1).trim();
175
- value = value.replace(/^['"]/, "").replace(/['"]$/, "");
176
- if (key && value) {
177
- frontMatter[key] = value;
178
- }
179
- }
180
- return { frontMatter, body };
181
- }
182
- async function listCodexCustomPrompts() {
183
- const codexHome = resolveCodexHomeDir();
184
- const promptsDir = path.join(codexHome, "prompts");
185
- let entries;
186
- try {
187
- entries = await fs.readdir(promptsDir, { withFileTypes: true });
188
- }
189
- catch {
190
- return [];
191
- }
192
- const commands = [];
193
- for (const entry of entries) {
194
- if (!entry.isFile()) {
195
- continue;
196
- }
197
- if (!entry.name.endsWith(".md")) {
198
- continue;
199
- }
200
- const name = entry.name.slice(0, -".md".length);
201
- if (!name) {
202
- continue;
203
- }
204
- const fullPath = path.join(promptsDir, entry.name);
205
- let content;
206
- try {
207
- content = await fs.readFile(fullPath, "utf8");
208
- }
209
- catch {
210
- continue;
211
- }
212
- const parsed = parseFrontMatter(content);
213
- const description = parsed.frontMatter["description"] ?? "Custom prompt";
214
- const argumentHint = parsed.frontMatter["argument-hint"] ?? parsed.frontMatter["argument_hint"] ?? "";
215
- commands.push({
216
- name: `prompts:${name}`,
217
- description,
218
- argumentHint,
219
- });
220
- }
221
- return commands.sort((a, b) => a.name.localeCompare(b.name));
222
- }
223
- async function listCodexSkills(cwd) {
224
- const candidates = [];
225
- candidates.push(path.join(cwd, ".codex", "skills"));
226
- const repoRoot = (() => {
227
- try {
228
- const output = execSync("git rev-parse --show-toplevel", {
229
- cwd,
230
- encoding: "utf8",
231
- stdio: ["ignore", "pipe", "ignore"],
232
- });
233
- const trimmed = output.trim();
234
- return trimmed ? trimmed : null;
235
- }
236
- catch {
237
- return null;
238
- }
239
- })();
240
- if (repoRoot) {
241
- candidates.push(path.join(path.dirname(cwd), ".codex", "skills"));
242
- candidates.push(path.join(repoRoot, ".codex", "skills"));
243
- }
244
- candidates.push(path.join(resolveCodexHomeDir(), "skills"));
245
- const commandsByName = new Map();
246
- for (const dir of candidates) {
247
- let entries;
248
- try {
249
- entries = await fs.readdir(dir, { withFileTypes: true });
250
- }
251
- catch {
252
- continue;
253
- }
254
- for (const entry of entries) {
255
- if (!entry.isDirectory() && !entry.isSymbolicLink()) {
256
- continue;
257
- }
258
- const skillDir = path.join(dir, entry.name);
259
- const skillPath = path.join(skillDir, "SKILL.md");
260
- let content;
261
- try {
262
- content = await fs.readFile(skillPath, "utf8");
263
- }
264
- catch {
265
- continue;
266
- }
267
- const { frontMatter } = parseFrontMatter(content);
268
- const name = frontMatter["name"];
269
- const description = frontMatter["description"];
270
- if (!name || !description) {
271
- continue;
272
- }
273
- if (!commandsByName.has(name)) {
274
- commandsByName.set(name, {
275
- name,
276
- description,
277
- argumentHint: "",
278
- });
279
- }
280
- }
281
- }
282
- return Array.from(commandsByName.values()).sort((a, b) => a.name.localeCompare(b.name));
283
- }
284
- function escapeRegExp(value) {
285
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
286
- }
287
- function expandCodexCustomPrompt(template, args) {
288
- const trimmedArgs = args ? args.trim() : "";
289
- const tokens = trimmedArgs ? tokenizeCommandArgs(trimmedArgs) : [];
290
- const named = {};
291
- const positional = [];
292
- for (const token of tokens) {
293
- const idx = token.indexOf("=");
294
- if (idx > 0) {
295
- const key = token.slice(0, idx);
296
- const value = token.slice(idx + 1);
297
- if (key) {
298
- named[key] = value;
299
- continue;
300
- }
301
- }
302
- positional.push(token);
303
- }
304
- const dollarPlaceholder = "__CODEX_DOLLAR_PLACEHOLDER__";
305
- let out = template.split("$$").join(dollarPlaceholder);
306
- out = out.split("$ARGUMENTS").join(trimmedArgs);
307
- for (let i = 1; i <= 9; i += 1) {
308
- const value = positional[i - 1] ?? "";
309
- out = out.split(`$${i}`).join(value);
310
- }
311
- const namedKeys = Object.keys(named).sort((a, b) => b.length - a.length);
312
- for (const key of namedKeys) {
313
- const value = named[key] ?? "";
314
- const re = new RegExp(`\\$${escapeRegExp(key)}\\b`, "g");
315
- out = out.replace(re, value);
316
- }
317
- out = out.split(dollarPlaceholder).join("$");
318
- return out;
319
- }
320
- function toCodexMcpConfig(config) {
321
- switch (config.type) {
322
- case "stdio":
323
- return {
324
- command: config.command,
325
- args: config.args,
326
- env: config.env,
327
- };
328
- case "http":
329
- return {
330
- url: config.url,
331
- http_headers: config.headers,
332
- };
333
- case "sse":
334
- return {
335
- url: config.url,
336
- http_headers: config.headers,
337
- };
338
- }
339
- }
340
- class CodexAppServerClient {
341
- constructor(child, logger) {
342
- this.child = child;
343
- this.logger = logger;
344
- this.pending = new Map();
345
- this.requestHandlers = new Map();
346
- this.notificationHandler = null;
347
- this.nextId = 1;
348
- this.disposed = false;
349
- this.stderrBuffer = "";
350
- this.resolveExitPromise = null;
351
- this.rl = readline.createInterface({ input: child.stdout });
352
- this.exitPromise = new Promise((resolve) => {
353
- this.resolveExitPromise = resolve;
354
- });
355
- this.rl.on("line", (line) => this.handleLine(line));
356
- child.stderr.on("data", (chunk) => {
357
- this.stderrBuffer += chunk.toString();
358
- if (this.stderrBuffer.length > 8192) {
359
- this.stderrBuffer = this.stderrBuffer.slice(-8192);
360
- }
361
- });
362
- child.on("exit", (code, signal) => {
363
- const message = code === 0 && !signal
364
- ? "Codex app-server exited"
365
- : `Codex app-server exited with code ${code ?? "null"} and signal ${signal ?? "null"}`;
366
- const error = new Error(`${message}\n${this.stderrBuffer}`.trim());
367
- for (const pending of this.pending.values()) {
368
- clearTimeout(pending.timer);
369
- pending.reject(error);
370
- }
371
- this.pending.clear();
372
- this.disposed = true;
373
- this.resolveExitPromise?.();
374
- this.resolveExitPromise = null;
375
- });
376
- }
377
- setNotificationHandler(handler) {
378
- this.notificationHandler = handler;
379
- }
380
- setRequestHandler(method, handler) {
381
- this.requestHandlers.set(method, handler);
382
- }
383
- request(method, params, timeoutMs = DEFAULT_TIMEOUT_MS) {
384
- if (this.disposed) {
385
- return Promise.reject(new Error("Codex app-server client is closed"));
386
- }
387
- const id = this.nextId++;
388
- const payload = { id, method, params };
389
- const serialized = JSON.stringify(payload);
390
- this.child.stdin.write(`${serialized}\n`);
391
- return new Promise((resolve, reject) => {
392
- const timer = setTimeout(() => {
393
- this.pending.delete(id);
394
- reject(new Error(`Codex app-server request timed out for ${method}`));
395
- }, timeoutMs);
396
- this.pending.set(id, { resolve, reject, timer });
397
- });
398
- }
399
- notify(method, params) {
400
- if (this.disposed) {
401
- return;
402
- }
403
- const payload = { method, params };
404
- this.child.stdin.write(`${JSON.stringify(payload)}\n`);
405
- }
406
- async dispose() {
407
- if (this.disposed)
408
- return;
409
- this.disposed = true;
410
- this.rl.close();
411
- try {
412
- this.child.stdin.end();
413
- }
414
- catch {
415
- // ignore
416
- }
417
- terminateChildProcessTree(this.child);
418
- await this.exitPromise;
419
- }
420
- async handleLine(line) {
421
- if (!line.trim())
422
- return;
423
- let msg;
424
- try {
425
- msg = JSON.parse(line);
426
- }
427
- catch (error) {
428
- this.logger.warn({ error, line }, "Failed to parse Codex app-server JSON");
429
- return;
430
- }
431
- if (typeof msg.id === "number") {
432
- const id = msg.id;
433
- if (msg.result !== undefined || msg.error) {
434
- const pending = this.pending.get(id);
435
- if (!pending)
436
- return;
437
- clearTimeout(pending.timer);
438
- this.pending.delete(id);
439
- if (msg.error) {
440
- pending.reject(new Error(msg.error?.message ?? "Unknown error"));
441
- }
442
- else {
443
- pending.resolve(msg.result);
444
- }
445
- return;
446
- }
447
- // Server-initiated request
448
- if (typeof msg.method === "string") {
449
- const request = msg;
450
- const handler = this.requestHandlers.get(request.method);
451
- try {
452
- const result = handler ? await handler(request.params) : {};
453
- const response = { id: request.id, result };
454
- this.child.stdin.write(`${JSON.stringify(response)}\n`);
455
- }
456
- catch (error) {
457
- const response = {
458
- id: request.id,
459
- error: { message: error instanceof Error ? error.message : String(error) },
460
- };
461
- this.child.stdin.write(`${JSON.stringify(response)}\n`);
462
- }
463
- return;
464
- }
465
- }
466
- if (typeof msg.method === "string") {
467
- const notification = msg;
468
- this.notificationHandler?.(notification.method, notification.params);
469
- }
470
- }
471
- }
472
- function terminateChildProcessTree(child) {
473
- if (child.killed) {
474
- return;
475
- }
476
- if (process.platform !== "win32" && typeof child.pid === "number" && child.pid > 0) {
477
- try {
478
- process.kill(-child.pid, "SIGTERM");
479
- return;
480
- }
481
- catch {
482
- // Fall back to the direct child when no separate process group exists.
483
- }
484
- }
485
- try {
486
- child.kill("SIGTERM");
487
- }
488
- catch {
489
- // ignore
490
- }
491
- }
492
- function toAgentUsage(tokenUsage) {
493
- if (!tokenUsage || typeof tokenUsage !== "object")
494
- return undefined;
495
- const usage = tokenUsage;
496
- return {
497
- inputTokens: usage.last?.inputTokens,
498
- cachedInputTokens: usage.last?.cachedInputTokens,
499
- outputTokens: usage.last?.outputTokens,
500
- };
501
- }
502
- function extractUserText(content) {
503
- if (!Array.isArray(content))
504
- return null;
505
- const parts = [];
506
- for (const item of content) {
507
- if (item && typeof item === "object") {
508
- const obj = item;
509
- if (obj.type === "text" && typeof obj.text === "string") {
510
- parts.push(obj.text);
511
- }
512
- }
513
- }
514
- return parts.length > 0 ? parts.join("\n") : null;
515
- }
516
- function parsePlanTextToTodoItems(text) {
517
- const lines = text
518
- .split("\n")
519
- .map((line) => line.trim())
520
- .filter((line) => line.length > 0);
521
- if (lines.length === 0) {
522
- return [{ text, completed: false }];
523
- }
524
- return lines.map((line) => ({
525
- text: line.replace(/^[-*]\s+/, ""),
526
- completed: false,
527
- }));
528
- }
529
- function planStepsToTodoItems(steps) {
530
- return steps.map((entry) => ({
531
- text: entry.step,
532
- completed: entry.status === "completed",
533
- }));
534
- }
535
- function extractPatchLikeText(value) {
536
- if (!value || typeof value !== "object") {
537
- return undefined;
538
- }
539
- const record = value;
540
- const candidates = [
541
- record.diff,
542
- record.patch,
543
- record.unified_diff,
544
- record.unifiedDiff,
545
- record.content,
546
- record.newString,
547
- ];
548
- for (const candidate of candidates) {
549
- if (typeof candidate === "string" && candidate.length > 0) {
550
- return candidate;
551
- }
552
- }
553
- return undefined;
554
- }
555
- function normalizeCodexThreadItemType(rawType) {
556
- if (!rawType) {
557
- return rawType;
558
- }
559
- switch (rawType) {
560
- case "UserMessage":
561
- return "userMessage";
562
- case "AgentMessage":
563
- return "agentMessage";
564
- case "Reasoning":
565
- return "reasoning";
566
- case "Plan":
567
- return "plan";
568
- case "CommandExecution":
569
- return "commandExecution";
570
- case "FileChange":
571
- return "fileChange";
572
- case "McpToolCall":
573
- return "mcpToolCall";
574
- case "WebSearch":
575
- return "webSearch";
576
- default:
577
- return rawType;
578
- }
579
- }
580
- function normalizeCodexCommandValue(value) {
581
- if (typeof value === "string") {
582
- const trimmed = value.trim();
583
- if (!trimmed.length) {
584
- return null;
585
- }
586
- const wrapperMatch = trimmed.match(/^(?:\/bin\/)?(?:zsh|bash|sh)\s+-(?:lc|c)\s+([\s\S]+)$/);
587
- if (!wrapperMatch) {
588
- return trimmed;
589
- }
590
- const candidate = wrapperMatch[1]?.trim() ?? "";
591
- if (!candidate.length) {
592
- return trimmed;
593
- }
594
- if ((candidate.startsWith('"') && candidate.endsWith('"')) ||
595
- (candidate.startsWith("'") && candidate.endsWith("'"))) {
596
- return candidate.slice(1, -1);
597
- }
598
- return candidate;
599
- }
600
- if (!Array.isArray(value)) {
601
- return null;
602
- }
603
- const parts = value
604
- .filter((entry) => typeof entry === "string")
605
- .map((entry) => entry.trim())
606
- .filter((entry) => entry.length > 0);
607
- if (parts.length === 0) {
608
- return null;
609
- }
610
- if (parts.length >= 3 && (parts[1] === "-lc" || parts[1] === "-c")) {
611
- return parts[2] ?? parts;
612
- }
613
- return parts;
614
- }
615
- function parseCodexPatchChanges(changes) {
616
- const resolvePathFromRecord = (record) => {
617
- const directPath = (typeof record.path === "string" && record.path.trim().length > 0
618
- ? record.path.trim()
619
- : "") ||
620
- (typeof record.file_path === "string" && record.file_path.trim().length > 0
621
- ? record.file_path.trim()
622
- : "") ||
623
- (typeof record.filePath === "string" && record.filePath.trim().length > 0
624
- ? record.filePath.trim()
625
- : "");
626
- return directPath;
627
- };
628
- if (!changes || typeof changes !== "object") {
629
- return [];
630
- }
631
- if (Array.isArray(changes)) {
632
- return changes
633
- .map((entry) => {
634
- if (!entry || typeof entry !== "object") {
635
- return null;
636
- }
637
- const record = entry;
638
- const pathValue = resolvePathFromRecord(record);
639
- if (!pathValue) {
640
- return null;
641
- }
642
- return {
643
- path: pathValue,
644
- kind: (typeof record.kind === "string" && record.kind) ||
645
- (typeof record.type === "string" && record.type) ||
646
- undefined,
647
- content: extractPatchLikeText(record),
648
- };
649
- })
650
- .filter((entry) => entry !== null);
651
- }
652
- const recordChanges = changes;
653
- const directPathValue = resolvePathFromRecord(recordChanges);
654
- if (directPathValue) {
655
- return [
656
- {
657
- path: directPathValue,
658
- kind: (typeof recordChanges.kind === "string" && recordChanges.kind) ||
659
- (typeof recordChanges.type === "string" && recordChanges.type) ||
660
- undefined,
661
- content: extractPatchLikeText(recordChanges),
662
- },
663
- ];
664
- }
665
- return Object.entries(recordChanges)
666
- .map(([path, value]) => {
667
- const normalizedPath = path.trim();
668
- if (!normalizedPath) {
669
- return null;
670
- }
671
- return {
672
- path: normalizedPath,
673
- kind: value &&
674
- typeof value === "object" &&
675
- typeof value.type === "string"
676
- ? (value.type ?? undefined)
677
- : undefined,
678
- content: extractPatchLikeText(value),
679
- };
680
- })
681
- .filter((entry) => entry !== null);
682
- }
683
- function codexPatchTextFields(text) {
684
- if (typeof text !== "string") {
685
- return {};
686
- }
687
- const normalized = text.trimStart();
688
- const looksLikeUnifiedDiff = normalized.startsWith("diff --git") ||
689
- normalized.startsWith("@@") ||
690
- normalized.startsWith("--- ") ||
691
- normalized.startsWith("+++ ");
692
- return looksLikeUnifiedDiff ? { patch: text } : { content: text };
693
- }
694
- function toRunningToolCall(item) {
695
- return {
696
- ...item,
697
- status: "running",
698
- error: null,
699
- };
700
- }
701
- function isEditToolCallWithoutContent(item) {
702
- if (item.type !== "tool_call") {
703
- return false;
704
- }
705
- if (item.detail.type !== "edit") {
706
- return false;
707
- }
708
- const hasDiff = typeof item.detail.unifiedDiff === "string" && item.detail.unifiedDiff.trim().length > 0;
709
- const hasNewString = typeof item.detail.newString === "string" && item.detail.newString.trim().length > 0;
710
- return !hasDiff && !hasNewString;
711
- }
712
- function decodeCodexOutputDeltaChunk(chunk) {
713
- const trimmed = chunk.trim();
714
- if (trimmed.length === 0) {
715
- return chunk;
716
- }
717
- if (!/^[A-Za-z0-9+/=]+$/.test(trimmed) || trimmed.length % 4 !== 0) {
718
- return chunk;
719
- }
720
- try {
721
- const decoded = Buffer.from(trimmed, "base64").toString("utf8");
722
- if (decoded.length === 0) {
723
- return chunk;
724
- }
725
- const normalizedInput = trimmed.replace(/=+$/, "");
726
- const normalizedRoundTrip = Buffer.from(decoded, "utf8").toString("base64").replace(/=+$/, "");
727
- return normalizedRoundTrip === normalizedInput ? decoded : chunk;
728
- }
729
- catch {
730
- return chunk;
731
- }
732
- }
733
- function mapCodexExecNotificationToToolCall(params) {
734
- const command = normalizeCodexCommandValue(params.command);
735
- if (!command) {
736
- return null;
737
- }
738
- const isFailure = params.running
739
- ? false
740
- : params.success === false || (typeof params.exitCode === "number" && params.exitCode !== 0);
741
- const output = params.running
742
- ? null
743
- : {
744
- command,
745
- ...(params.output !== null && params.output !== undefined ? { output: params.output } : {}),
746
- ...(params.exitCode !== null && params.exitCode !== undefined
747
- ? { exitCode: params.exitCode }
748
- : {}),
749
- };
750
- const mapped = mapCodexRolloutToolCall({
751
- callId: params.callId ?? null,
752
- name: "shell",
753
- input: {
754
- command,
755
- ...(params.cwd ? { cwd: params.cwd } : {}),
756
- },
757
- output,
758
- error: isFailure ? { message: params.stderr?.trim() || "Command failed" } : null,
759
- cwd: params.cwd ?? null,
760
- });
761
- if (!mapped) {
762
- return null;
763
- }
764
- return params.running ? toRunningToolCall(mapped) : mapped;
765
- }
766
- function mapCodexPatchNotificationToToolCall(params) {
767
- const files = parseCodexPatchChanges(params.changes);
768
- const firstPath = files[0]?.path;
769
- const firstPatchText = files
770
- .map((file) => file.content?.trim())
771
- .find((value) => typeof value === "string" && value.length > 0);
772
- const patchText = firstPatchText;
773
- const patchFields = codexPatchTextFields(patchText);
774
- const mapped = mapCodexRolloutToolCall({
775
- callId: params.callId ?? null,
776
- name: "apply_patch",
777
- input: firstPath
778
- ? {
779
- path: firstPath,
780
- ...patchFields,
781
- files: files.map((file) => ({ path: file.path, kind: file.kind })),
782
- }
783
- : {
784
- changes: params.changes ?? null,
785
- ...patchFields,
786
- },
787
- output: params.running
788
- ? null
789
- : {
790
- ...(files.length > 0
791
- ? {
792
- files: files.map((file) => ({
793
- path: file.path,
794
- ...(file.kind ? { kind: file.kind } : {}),
795
- ...codexPatchTextFields(file.content ?? patchText),
796
- })),
797
- }
798
- : {}),
799
- ...(params.stdout ? { stdout: params.stdout } : {}),
800
- ...(params.stderr ? { stderr: params.stderr } : {}),
801
- ...(params.success !== null && params.success !== undefined
802
- ? { success: params.success }
803
- : {}),
804
- },
805
- error: params.running || params.success !== false
806
- ? null
807
- : { message: params.stderr?.trim() || "Patch apply failed" },
808
- cwd: params.cwd ?? null,
809
- });
810
- if (!mapped) {
811
- return null;
812
- }
813
- return params.running ? toRunningToolCall(mapped) : mapped;
814
- }
815
- function mapCodexTerminalInteractionToToolCall(params) {
816
- const processId = nonEmptyString(params.processId ?? undefined);
817
- const callId = processId
818
- ? `terminal-session-${processId}`
819
- : (nonEmptyString(params.fallbackCallId ?? undefined) ?? "terminal-interaction");
820
- const label = nonEmptyString(params.command ?? undefined);
821
- return {
822
- type: "tool_call",
823
- callId,
824
- name: "terminal",
825
- status: "completed",
826
- error: null,
827
- detail: {
828
- type: "plain_text",
829
- ...(label ? { label } : {}),
830
- icon: "square_terminal",
831
- },
832
- ...(processId ? { metadata: { processId } } : {}),
833
- };
834
- }
835
- function threadItemToTimeline(item, options) {
836
- if (!item || typeof item !== "object")
837
- return null;
838
- const includeUserMessage = options?.includeUserMessage ?? true;
839
- const cwd = options?.cwd ?? null;
840
- const normalizedType = normalizeCodexThreadItemType(typeof item.type === "string" ? item.type : undefined);
841
- const normalizedItem = normalizedType && normalizedType !== item.type
842
- ? { ...item, type: normalizedType }
843
- : item;
844
- switch (normalizedType) {
845
- case "userMessage": {
846
- if (!includeUserMessage) {
847
- return null;
848
- }
849
- const text = extractUserText(normalizedItem.content) ?? "";
850
- return { type: "user_message", text };
851
- }
852
- case "agentMessage": {
853
- return { type: "assistant_message", text: normalizedItem.text ?? "" };
854
- }
855
- case "plan": {
856
- const text = normalizedItem.text ?? "";
857
- const items = parsePlanTextToTodoItems(text);
858
- return { type: "todo", items };
859
- }
860
- case "reasoning": {
861
- const summary = Array.isArray(normalizedItem.summary)
862
- ? normalizedItem.summary.join("\n")
863
- : "";
864
- const content = Array.isArray(normalizedItem.content)
865
- ? normalizedItem.content.join("\n")
866
- : "";
867
- const text = summary || content;
868
- return text ? { type: "reasoning", text } : null;
869
- }
870
- case "commandExecution":
871
- case "fileChange":
872
- case "mcpToolCall":
873
- case "webSearch":
874
- return mapCodexToolCallFromThreadItem(normalizedItem, { cwd });
875
- default:
876
- return null;
877
- }
878
- }
879
- function toSandboxPolicy(type, networkAccess) {
880
- switch (type) {
881
- case "read-only":
882
- return { type: "readOnly" };
883
- case "workspace-write":
884
- return { type: "workspaceWrite", networkAccess: networkAccess ?? false };
885
- case "danger-full-access":
886
- return { type: "dangerFullAccess" };
887
- default:
888
- return { type: "workspaceWrite", networkAccess: networkAccess ?? false };
889
- }
890
- }
891
- function getImageExtension(mimeType) {
892
- switch (mimeType) {
893
- case "image/jpeg":
894
- return "jpg";
895
- case "image/png":
896
- return "png";
897
- case "image/webp":
898
- return "webp";
899
- case "image/gif":
900
- return "gif";
901
- case "image/bmp":
902
- return "bmp";
903
- case "image/tiff":
904
- return "tiff";
905
- default:
906
- return "bin";
907
- }
908
- }
909
- function normalizeImageData(mimeType, data) {
910
- if (data.startsWith("data:")) {
911
- const match = data.match(/^data:([^;]+);base64,(.*)$/);
912
- if (match) {
913
- return { mimeType: match[1], data: match[2] };
914
- }
915
- }
916
- return { mimeType, data };
917
- }
918
- const ThreadStartedNotificationSchema = z
919
- .object({
920
- thread: z.object({ id: z.string() }).passthrough(),
921
- })
922
- .passthrough();
923
- const TurnStartedNotificationSchema = z
924
- .object({
925
- turn: z.object({ id: z.string() }).passthrough(),
926
- })
927
- .passthrough();
928
- const TurnCompletedNotificationSchema = z
929
- .object({
930
- turn: z
931
- .object({
932
- status: z.string(),
933
- error: z
934
- .object({
935
- message: z.string().optional(),
936
- })
937
- .passthrough()
938
- .nullable()
939
- .optional(),
940
- })
941
- .passthrough(),
942
- })
943
- .passthrough();
944
- const TurnPlanUpdatedNotificationSchema = z
945
- .object({
946
- plan: z.array(z
947
- .object({
948
- step: z.string().optional(),
949
- status: z.string().optional(),
950
- })
951
- .passthrough()),
952
- })
953
- .passthrough();
954
- const TurnDiffUpdatedNotificationSchema = z
955
- .object({
956
- diff: z.string(),
957
- })
958
- .passthrough();
959
- const ThreadTokenUsageUpdatedNotificationSchema = z
960
- .object({
961
- tokenUsage: z.unknown(),
962
- })
963
- .passthrough();
964
- const ItemTextDeltaNotificationSchema = z
965
- .object({
966
- itemId: z.string(),
967
- delta: z.string(),
968
- })
969
- .passthrough();
970
- const ItemLifecycleNotificationSchema = z
971
- .object({
972
- item: z
973
- .object({
974
- id: z.string().optional(),
975
- type: z.string().optional(),
976
- })
977
- .passthrough(),
978
- })
979
- .passthrough();
980
- const CodexEventTurnAbortedNotificationSchema = z
981
- .object({
982
- msg: z
983
- .object({
984
- type: z.literal("turn_aborted"),
985
- reason: z.string().optional(),
986
- })
987
- .passthrough(),
988
- })
989
- .passthrough();
990
- const CodexEventTaskCompleteNotificationSchema = z
991
- .object({
992
- msg: z
993
- .object({
994
- type: z.literal("task_complete"),
995
- })
996
- .passthrough(),
997
- })
998
- .passthrough();
999
- const CodexEventItemLifecycleNotificationSchema = z
1000
- .object({
1001
- msg: z
1002
- .object({
1003
- type: z.enum(["item_started", "item_completed"]),
1004
- item: z
1005
- .object({
1006
- id: z.string().optional(),
1007
- type: z.string().optional(),
1008
- })
1009
- .passthrough(),
1010
- })
1011
- .passthrough(),
1012
- })
1013
- .passthrough();
1014
- const CodexEventExecCommandBeginNotificationSchema = z
1015
- .object({
1016
- msg: z
1017
- .object({
1018
- type: z.literal("exec_command_begin"),
1019
- call_id: z.string().optional(),
1020
- command: z.unknown().optional(),
1021
- cwd: z.string().optional(),
1022
- })
1023
- .passthrough(),
1024
- })
1025
- .passthrough();
1026
- const CodexEventExecCommandEndNotificationSchema = z
1027
- .object({
1028
- msg: z
1029
- .object({
1030
- type: z.literal("exec_command_end"),
1031
- call_id: z.string().optional(),
1032
- command: z.unknown().optional(),
1033
- cwd: z.string().optional(),
1034
- stdout: z.string().optional(),
1035
- stderr: z.string().optional(),
1036
- aggregated_output: z.string().optional(),
1037
- aggregatedOutput: z.string().optional(),
1038
- formatted_output: z.string().optional(),
1039
- exit_code: z.number().nullable().optional(),
1040
- exitCode: z.number().nullable().optional(),
1041
- success: z.boolean().optional(),
1042
- })
1043
- .passthrough(),
1044
- })
1045
- .passthrough();
1046
- const CodexEventExecCommandOutputDeltaNotificationSchema = z
1047
- .object({
1048
- msg: z
1049
- .object({
1050
- type: z.literal("exec_command_output_delta"),
1051
- call_id: z.string().optional(),
1052
- stream: z.string().optional(),
1053
- chunk: z.string().optional(),
1054
- delta: z.string().optional(),
1055
- })
1056
- .passthrough(),
1057
- })
1058
- .passthrough();
1059
- const CodexEventTerminalInteractionNotificationSchema = z
1060
- .object({
1061
- msg: z
1062
- .object({
1063
- type: z.literal("terminal_interaction"),
1064
- call_id: z.string().optional(),
1065
- process_id: z.union([z.string(), z.number()]).optional(),
1066
- stdin: z.string().optional(),
1067
- })
1068
- .passthrough(),
1069
- })
1070
- .passthrough();
1071
- const ItemCommandExecutionTerminalInteractionNotificationSchema = z
1072
- .object({
1073
- itemId: z.string().optional(),
1074
- processId: z.union([z.string(), z.number()]).optional(),
1075
- stdin: z.string().optional(),
1076
- })
1077
- .passthrough();
1078
- const CodexEventPatchApplyBeginNotificationSchema = z
1079
- .object({
1080
- msg: z
1081
- .object({
1082
- type: z.literal("patch_apply_begin"),
1083
- call_id: z.string().optional(),
1084
- changes: z.unknown().optional(),
1085
- })
1086
- .passthrough(),
1087
- })
1088
- .passthrough();
1089
- const CodexEventPatchApplyEndNotificationSchema = z
1090
- .object({
1091
- msg: z
1092
- .object({
1093
- type: z.literal("patch_apply_end"),
1094
- call_id: z.string().optional(),
1095
- changes: z.unknown().optional(),
1096
- stdout: z.string().optional(),
1097
- stderr: z.string().optional(),
1098
- success: z.boolean().optional(),
1099
- })
1100
- .passthrough(),
1101
- })
1102
- .passthrough();
1103
- const ItemFileChangeOutputDeltaNotificationSchema = z
1104
- .object({
1105
- itemId: z.string(),
1106
- delta: z.string().optional(),
1107
- chunk: z.string().optional(),
1108
- })
1109
- .passthrough();
1110
- const CodexEventTurnDiffNotificationSchema = z
1111
- .object({
1112
- msg: z
1113
- .object({
1114
- type: z.literal("turn_diff"),
1115
- unified_diff: z.string().optional(),
1116
- diff: z.string().optional(),
1117
- })
1118
- .passthrough(),
1119
- })
1120
- .passthrough();
1121
- const CodexNotificationSchema = z.union([
1122
- z
1123
- .object({ method: z.literal("thread/started"), params: ThreadStartedNotificationSchema })
1124
- .transform(({ params }) => ({
1125
- kind: "thread_started",
1126
- threadId: params.thread.id,
1127
- })),
1128
- z.object({ method: z.literal("thread/started"), params: z.unknown() }).transform(({ method, params }) => ({
1129
- kind: "invalid_payload",
1130
- method,
1131
- params,
1132
- })),
1133
- z
1134
- .object({ method: z.literal("turn/started"), params: TurnStartedNotificationSchema })
1135
- .transform(({ params }) => ({ kind: "turn_started", turnId: params.turn.id })),
1136
- z.object({ method: z.literal("turn/started"), params: z.unknown() }).transform(({ method, params }) => ({
1137
- kind: "invalid_payload",
1138
- method,
1139
- params,
1140
- })),
1141
- z
1142
- .object({ method: z.literal("turn/completed"), params: TurnCompletedNotificationSchema })
1143
- .transform(({ params }) => ({
1144
- kind: "turn_completed",
1145
- status: params.turn.status,
1146
- errorMessage: params.turn.error?.message ?? null,
1147
- })),
1148
- z.object({ method: z.literal("turn/completed"), params: z.unknown() }).transform(({ method, params }) => ({
1149
- kind: "invalid_payload",
1150
- method,
1151
- params,
1152
- })),
1153
- z
1154
- .object({ method: z.literal("turn/plan/updated"), params: TurnPlanUpdatedNotificationSchema })
1155
- .transform(({ params }) => ({
1156
- kind: "plan_updated",
1157
- plan: params.plan.map((entry) => ({
1158
- step: entry.step ?? null,
1159
- status: entry.status ?? null,
1160
- })),
1161
- })),
1162
- z.object({ method: z.literal("turn/plan/updated"), params: z.unknown() }).transform(({ method, params }) => ({
1163
- kind: "invalid_payload",
1164
- method,
1165
- params,
1166
- })),
1167
- z
1168
- .object({ method: z.literal("turn/diff/updated"), params: TurnDiffUpdatedNotificationSchema })
1169
- .transform(({ params }) => ({ kind: "diff_updated", diff: params.diff })),
1170
- z.object({ method: z.literal("turn/diff/updated"), params: z.unknown() }).transform(({ method, params }) => ({
1171
- kind: "invalid_payload",
1172
- method,
1173
- params,
1174
- })),
1175
- z
1176
- .object({
1177
- method: z.literal("thread/tokenUsage/updated"),
1178
- params: ThreadTokenUsageUpdatedNotificationSchema,
1179
- })
1180
- .transform(({ params }) => ({
1181
- kind: "token_usage_updated",
1182
- tokenUsage: params.tokenUsage,
1183
- })),
1184
- z.object({ method: z.literal("thread/tokenUsage/updated"), params: z.unknown() }).transform(({ method, params }) => ({
1185
- kind: "invalid_payload",
1186
- method,
1187
- params,
1188
- })),
1189
- z
1190
- .object({
1191
- method: z.literal("item/agentMessage/delta"),
1192
- params: ItemTextDeltaNotificationSchema,
1193
- })
1194
- .transform(({ params }) => ({
1195
- kind: "agent_message_delta",
1196
- itemId: params.itemId,
1197
- delta: params.delta,
1198
- })),
1199
- z.object({ method: z.literal("item/agentMessage/delta"), params: z.unknown() }).transform(({ method, params }) => ({
1200
- kind: "invalid_payload",
1201
- method,
1202
- params,
1203
- })),
1204
- z
1205
- .object({
1206
- method: z.literal("item/reasoning/summaryTextDelta"),
1207
- params: ItemTextDeltaNotificationSchema,
1208
- })
1209
- .transform(({ params }) => ({
1210
- kind: "reasoning_delta",
1211
- itemId: params.itemId,
1212
- delta: params.delta,
1213
- })),
1214
- z.object({ method: z.literal("item/reasoning/summaryTextDelta"), params: z.unknown() }).transform(({ method, params }) => ({
1215
- kind: "invalid_payload",
1216
- method,
1217
- params,
1218
- })),
1219
- z
1220
- .object({ method: z.literal("item/completed"), params: ItemLifecycleNotificationSchema })
1221
- .transform(({ params }) => ({
1222
- kind: "item_completed",
1223
- source: "item",
1224
- item: params.item,
1225
- })),
1226
- z.object({ method: z.literal("item/completed"), params: z.unknown() }).transform(({ method, params }) => ({
1227
- kind: "invalid_payload",
1228
- method,
1229
- params,
1230
- })),
1231
- z
1232
- .object({ method: z.literal("item/started"), params: ItemLifecycleNotificationSchema })
1233
- .transform(({ params }) => ({
1234
- kind: "item_started",
1235
- source: "item",
1236
- item: params.item,
1237
- })),
1238
- z.object({ method: z.literal("item/started"), params: z.unknown() }).transform(({ method, params }) => ({
1239
- kind: "invalid_payload",
1240
- method,
1241
- params,
1242
- })),
1243
- z
1244
- .object({
1245
- method: z.literal("codex/event/item_started"),
1246
- params: CodexEventItemLifecycleNotificationSchema,
1247
- })
1248
- .transform(({ params }) => ({
1249
- kind: "item_started",
1250
- source: "codex_event",
1251
- item: params.msg.item,
1252
- })),
1253
- z.object({ method: z.literal("codex/event/item_started"), params: z.unknown() }).transform(({ method, params }) => ({
1254
- kind: "invalid_payload",
1255
- method,
1256
- params,
1257
- })),
1258
- z
1259
- .object({
1260
- method: z.literal("codex/event/item_completed"),
1261
- params: CodexEventItemLifecycleNotificationSchema,
1262
- })
1263
- .transform(({ params }) => ({
1264
- kind: "item_completed",
1265
- source: "codex_event",
1266
- item: params.msg.item,
1267
- })),
1268
- z.object({ method: z.literal("codex/event/item_completed"), params: z.unknown() }).transform(({ method, params }) => ({
1269
- kind: "invalid_payload",
1270
- method,
1271
- params,
1272
- })),
1273
- z
1274
- .object({
1275
- method: z.literal("codex/event/exec_command_begin"),
1276
- params: CodexEventExecCommandBeginNotificationSchema,
1277
- })
1278
- .transform(({ params }) => ({
1279
- kind: "exec_command_started",
1280
- callId: params.msg.call_id ?? null,
1281
- command: params.msg.command ?? null,
1282
- cwd: params.msg.cwd ?? null,
1283
- })),
1284
- z.object({ method: z.literal("codex/event/exec_command_begin"), params: z.unknown() }).transform(({ method, params }) => ({
1285
- kind: "invalid_payload",
1286
- method,
1287
- params,
1288
- })),
1289
- z
1290
- .object({
1291
- method: z.literal("codex/event/exec_command_end"),
1292
- params: CodexEventExecCommandEndNotificationSchema,
1293
- })
1294
- .transform(({ params }) => ({
1295
- kind: "exec_command_completed",
1296
- callId: params.msg.call_id ?? null,
1297
- command: params.msg.command ?? null,
1298
- cwd: params.msg.cwd ?? null,
1299
- output: params.msg.aggregated_output ??
1300
- params.msg.aggregatedOutput ??
1301
- params.msg.formatted_output ??
1302
- params.msg.stdout ??
1303
- null,
1304
- exitCode: params.msg.exit_code ?? params.msg.exitCode ?? null,
1305
- success: params.msg.success ?? null,
1306
- stderr: params.msg.stderr ?? null,
1307
- })),
1308
- z.object({ method: z.literal("codex/event/exec_command_end"), params: z.unknown() }).transform(({ method, params }) => ({
1309
- kind: "invalid_payload",
1310
- method,
1311
- params,
1312
- })),
1313
- z
1314
- .object({
1315
- method: z.literal("codex/event/exec_command_output_delta"),
1316
- params: CodexEventExecCommandOutputDeltaNotificationSchema,
1317
- })
1318
- .transform(({ params }) => ({
1319
- kind: "exec_command_output_delta",
1320
- callId: params.msg.call_id ?? null,
1321
- stream: params.msg.stream ?? null,
1322
- chunk: params.msg.chunk ?? params.msg.delta ?? null,
1323
- })),
1324
- z
1325
- .object({
1326
- method: z.literal("codex/event/exec_command_output_delta"),
1327
- params: z.unknown(),
1328
- })
1329
- .transform(({ method, params }) => ({
1330
- kind: "invalid_payload",
1331
- method,
1332
- params,
1333
- })),
1334
- z
1335
- .object({
1336
- method: z.literal("codex/event/terminal_interaction"),
1337
- params: CodexEventTerminalInteractionNotificationSchema,
1338
- })
1339
- .transform(({ params }) => ({
1340
- kind: "terminal_interaction",
1341
- source: "codex_event",
1342
- callId: params.msg.call_id ?? null,
1343
- processId: typeof params.msg.process_id === "number"
1344
- ? String(params.msg.process_id)
1345
- : (params.msg.process_id ?? null),
1346
- stdin: params.msg.stdin ?? null,
1347
- })),
1348
- z
1349
- .object({ method: z.literal("codex/event/terminal_interaction"), params: z.unknown() })
1350
- .transform(({ method, params }) => ({
1351
- kind: "invalid_payload",
1352
- method,
1353
- params,
1354
- })),
1355
- z
1356
- .object({
1357
- method: z.literal("item/commandExecution/terminalInteraction"),
1358
- params: ItemCommandExecutionTerminalInteractionNotificationSchema,
1359
- })
1360
- .transform(({ params }) => ({
1361
- kind: "terminal_interaction",
1362
- source: "item",
1363
- callId: params.itemId ?? null,
1364
- processId: typeof params.processId === "number"
1365
- ? String(params.processId)
1366
- : (params.processId ?? null),
1367
- stdin: params.stdin ?? null,
1368
- })),
1369
- z
1370
- .object({
1371
- method: z.literal("item/commandExecution/terminalInteraction"),
1372
- params: z.unknown(),
1373
- })
1374
- .transform(({ method, params }) => ({
1375
- kind: "invalid_payload",
1376
- method,
1377
- params,
1378
- })),
1379
- z
1380
- .object({
1381
- method: z.literal("codex/event/patch_apply_begin"),
1382
- params: CodexEventPatchApplyBeginNotificationSchema,
1383
- })
1384
- .transform(({ params }) => ({
1385
- kind: "patch_apply_started",
1386
- callId: params.msg.call_id ?? null,
1387
- changes: params.msg.changes ?? null,
1388
- })),
1389
- z.object({ method: z.literal("codex/event/patch_apply_begin"), params: z.unknown() }).transform(({ method, params }) => ({
1390
- kind: "invalid_payload",
1391
- method,
1392
- params,
1393
- })),
1394
- z
1395
- .object({
1396
- method: z.literal("codex/event/patch_apply_end"),
1397
- params: CodexEventPatchApplyEndNotificationSchema,
1398
- })
1399
- .transform(({ params }) => ({
1400
- kind: "patch_apply_completed",
1401
- callId: params.msg.call_id ?? null,
1402
- changes: params.msg.changes ?? null,
1403
- stdout: params.msg.stdout ?? null,
1404
- stderr: params.msg.stderr ?? null,
1405
- success: params.msg.success ?? null,
1406
- })),
1407
- z.object({ method: z.literal("codex/event/patch_apply_end"), params: z.unknown() }).transform(({ method, params }) => ({
1408
- kind: "invalid_payload",
1409
- method,
1410
- params,
1411
- })),
1412
- z
1413
- .object({
1414
- method: z.literal("item/fileChange/outputDelta"),
1415
- params: ItemFileChangeOutputDeltaNotificationSchema,
1416
- })
1417
- .transform(({ params }) => ({
1418
- kind: "file_change_output_delta",
1419
- itemId: params.itemId,
1420
- delta: params.delta ?? params.chunk ?? null,
1421
- })),
1422
- z.object({ method: z.literal("item/fileChange/outputDelta"), params: z.unknown() }).transform(({ method, params }) => ({
1423
- kind: "invalid_payload",
1424
- method,
1425
- params,
1426
- })),
1427
- z
1428
- .object({
1429
- method: z.literal("codex/event/turn_diff"),
1430
- params: CodexEventTurnDiffNotificationSchema,
1431
- })
1432
- .transform(({ params }) => ({
1433
- kind: "diff_updated",
1434
- diff: params.msg.unified_diff ?? params.msg.diff ?? "",
1435
- })),
1436
- z.object({ method: z.literal("codex/event/turn_diff"), params: z.unknown() }).transform(({ method, params }) => ({
1437
- kind: "invalid_payload",
1438
- method,
1439
- params,
1440
- })),
1441
- z
1442
- .object({
1443
- method: z.literal("codex/event/turn_aborted"),
1444
- params: CodexEventTurnAbortedNotificationSchema,
1445
- })
1446
- .transform(() => ({
1447
- kind: "turn_completed",
1448
- status: "interrupted",
1449
- errorMessage: null,
1450
- })),
1451
- z.object({ method: z.literal("codex/event/turn_aborted"), params: z.unknown() }).transform(({ method, params }) => ({
1452
- kind: "invalid_payload",
1453
- method,
1454
- params,
1455
- })),
1456
- z
1457
- .object({
1458
- method: z.literal("codex/event/task_complete"),
1459
- params: CodexEventTaskCompleteNotificationSchema,
1460
- })
1461
- .transform(() => ({
1462
- kind: "turn_completed",
1463
- status: "completed",
1464
- errorMessage: null,
1465
- })),
1466
- z.object({ method: z.literal("codex/event/task_complete"), params: z.unknown() }).transform(({ method, params }) => ({
1467
- kind: "invalid_payload",
1468
- method,
1469
- params,
1470
- })),
1471
- z
1472
- .object({ method: z.string(), params: z.unknown() })
1473
- .transform(({ method, params }) => ({ kind: "unknown_method", method, params })),
1474
- ]);
1475
- async function writeImageAttachment(mimeType, data) {
1476
- const attachmentsDir = path.join(os.tmpdir(), CODEX_IMAGE_ATTACHMENT_DIR);
1477
- await fs.mkdir(attachmentsDir, { recursive: true });
1478
- const normalized = normalizeImageData(mimeType, data);
1479
- const extension = getImageExtension(normalized.mimeType);
1480
- const filename = `${randomUUID()}.${extension}`;
1481
- const filePath = path.join(attachmentsDir, filename);
1482
- await fs.writeFile(filePath, Buffer.from(normalized.data, "base64"));
1483
- return filePath;
1484
- }
1485
- async function readCodexConfiguredDefaults(client, logger) {
1486
- let savedConfigDefaults = {};
1487
- try {
1488
- const response = (await client.request("getUserSavedConfig", {}));
1489
- savedConfigDefaults = {
1490
- model: normalizeCodexModelId(response?.config?.model),
1491
- thinkingOptionId: normalizeCodexThinkingOptionId(response?.config?.modelReasoningEffort ?? null),
1492
- };
1493
- }
1494
- catch (error) {
1495
- logger.debug({ error }, "Failed to read Codex saved config defaults");
1496
- }
1497
- if (savedConfigDefaults.model && savedConfigDefaults.thinkingOptionId) {
1498
- return savedConfigDefaults;
1499
- }
1500
- let configReadDefaults = {};
1501
- try {
1502
- const response = (await client.request("config/read", {}));
1503
- configReadDefaults = {
1504
- model: normalizeCodexModelId(response?.config?.model),
1505
- thinkingOptionId: normalizeCodexThinkingOptionId(response?.config?.model_reasoning_effort ?? null),
1506
- };
1507
- }
1508
- catch (error) {
1509
- logger.debug({ error }, "Failed to read Codex config defaults");
1510
- }
1511
- return mergeCodexConfiguredDefaults(savedConfigDefaults, configReadDefaults);
1512
- }
1513
- export async function codexAppServerTurnInputFromPrompt(prompt, logger) {
1514
- if (typeof prompt === "string") {
1515
- return [{ type: "text", text: prompt }];
1516
- }
1517
- const blocks = prompt;
1518
- const output = [];
1519
- for (const block of blocks) {
1520
- if (!block || typeof block !== "object") {
1521
- output.push(block);
1522
- continue;
1523
- }
1524
- const record = block;
1525
- if (record.type === "image" &&
1526
- typeof record.mimeType === "string" &&
1527
- typeof record.data === "string") {
1528
- try {
1529
- const filePath = await writeImageAttachment(record.mimeType, record.data);
1530
- output.push({ type: "localImage", path: filePath });
1531
- }
1532
- catch (error) {
1533
- const message = error instanceof Error ? error.message : String(error);
1534
- logger.warn({ message }, "Failed to write Codex image attachment");
1535
- output.push({
1536
- type: "text",
1537
- text: `User attached image (failed to write temp file): ${message}`,
1538
- });
1539
- }
1540
- continue;
1541
- }
1542
- output.push(block);
1543
- }
1544
- return output;
1545
- }
1546
- function buildCodexAppServerEnv(runtimeSettings, launchEnv) {
1547
- const env = applyProviderEnv(process.env, runtimeSettings);
1548
- if (!launchEnv) {
1549
- return env;
1550
- }
1551
- return {
1552
- ...env,
1553
- ...launchEnv,
1554
- };
1555
- }
1556
- export const __codexAppServerInternals = {
1557
- buildCodexAppServerEnv,
1558
- mapCodexPatchNotificationToToolCall,
1559
- };
1560
- class CodexAppServerAgentSession {
1561
- constructor(config, resumeHandle, logger, spawnAppServer) {
1562
- this.resumeHandle = resumeHandle;
1563
- this.spawnAppServer = spawnAppServer;
1564
- this.provider = CODEX_PROVIDER;
1565
- this.capabilities = CODEX_APP_SERVER_CAPABILITIES;
1566
- this.currentThreadId = null;
1567
- this.currentTurnId = null;
1568
- this.client = null;
1569
- this.subscribers = new Set();
1570
- this.nextTurnOrdinal = 0;
1571
- this.activeForegroundTurnId = null;
1572
- this.cachedRuntimeInfo = null;
1573
- this.historyPending = false;
1574
- this.persistedHistory = [];
1575
- this.pendingPermissions = new Map();
1576
- this.pendingPermissionHandlers = new Map();
1577
- this.resolvedPermissionRequests = new Set();
1578
- this.pendingAgentMessages = new Map();
1579
- this.pendingReasoning = new Map();
1580
- this.pendingCommandOutputDeltas = new Map();
1581
- this.pendingFileChangeOutputDeltas = new Map();
1582
- this.terminalCommandByProcessId = new Map();
1583
- this.pendingUnlabeledTerminalInteractions = new Set();
1584
- this.emittedTerminalInteractionKeys = new Set();
1585
- this.emittedExecCommandStartedCallIds = new Set();
1586
- this.emittedExecCommandCompletedCallIds = new Set();
1587
- this.emittedItemStartedIds = new Set();
1588
- this.emittedItemCompletedIds = new Set();
1589
- this.warnedUnknownNotificationMethods = new Set();
1590
- this.warnedInvalidNotificationPayloads = new Set();
1591
- this.warnedIncompleteEditToolCallIds = new Set();
1592
- this.connected = false;
1593
- this.collaborationModes = [];
1594
- this.resolvedCollaborationMode = null;
1595
- this.cachedSkills = [];
1596
- this.logger = logger.child({ module: "agent", provider: CODEX_PROVIDER });
1597
- if (config.modeId === undefined) {
1598
- throw new Error("Codex agent requires modeId to be specified");
1599
- }
1600
- validateCodexMode(config.modeId);
1601
- this.currentMode = config.modeId;
1602
- this.config = config;
1603
- this.config.thinkingOptionId = normalizeCodexThinkingOptionId(this.config.thinkingOptionId);
1604
- if (this.resumeHandle?.sessionId) {
1605
- this.currentThreadId = this.resumeHandle.sessionId;
1606
- this.historyPending = true;
1607
- }
1608
- }
1609
- get id() {
1610
- return this.currentThreadId;
1611
- }
1612
- async connect() {
1613
- if (this.connected)
1614
- return;
1615
- const child = this.spawnAppServer();
1616
- this.client = new CodexAppServerClient(child, this.logger);
1617
- this.client.setNotificationHandler((method, params) => this.handleNotification(method, params));
1618
- this.registerRequestHandlers();
1619
- await this.client.request("initialize", {
1620
- clientInfo: {
1621
- name: "paseo",
1622
- title: "Paseo",
1623
- version: "0.0.0",
1624
- },
1625
- });
1626
- this.client.notify("initialized", {});
1627
- await this.loadCollaborationModes();
1628
- await this.loadSkills();
1629
- if (this.currentThreadId) {
1630
- await this.loadPersistedHistory();
1631
- await this.ensureThreadLoaded();
1632
- }
1633
- this.connected = true;
1634
- }
1635
- async loadCollaborationModes() {
1636
- if (!this.client)
1637
- return;
1638
- try {
1639
- const response = (await this.client.request("collaborationMode/list", {}));
1640
- const data = Array.isArray(response?.data) ? response.data : [];
1641
- this.collaborationModes = data.map((entry) => ({
1642
- name: String(entry.name ?? ""),
1643
- mode: entry.mode ?? null,
1644
- model: entry.model ?? null,
1645
- reasoning_effort: entry.reasoning_effort ?? null,
1646
- developer_instructions: entry.developer_instructions ?? null,
1647
- }));
1648
- }
1649
- catch (error) {
1650
- this.logger.trace({ error }, "Failed to load collaboration modes");
1651
- this.collaborationModes = [];
1652
- }
1653
- this.resolvedCollaborationMode = this.resolveCollaborationMode(this.currentMode);
1654
- }
1655
- async loadSkills() {
1656
- if (!this.client)
1657
- return;
1658
- try {
1659
- const response = (await this.client.request("skills/list", {
1660
- cwd: [this.config.cwd],
1661
- }));
1662
- const entries = Array.isArray(response?.data) ? response.data : [];
1663
- const skills = [];
1664
- for (const entry of entries) {
1665
- const list = Array.isArray(entry.skills) ? entry.skills : [];
1666
- for (const skill of list) {
1667
- if (!skill?.name || !skill?.path)
1668
- continue;
1669
- skills.push({
1670
- name: skill.name,
1671
- description: skill.description ?? skill.shortDescription ?? "Skill",
1672
- path: skill.path,
1673
- });
1674
- }
1675
- }
1676
- this.cachedSkills = skills;
1677
- }
1678
- catch (error) {
1679
- this.logger.trace({ error }, "Failed to load skills list");
1680
- this.cachedSkills = [];
1681
- }
1682
- }
1683
- resolveCollaborationMode(modeId) {
1684
- if (this.collaborationModes.length === 0)
1685
- return null;
1686
- const normalized = modeId.toLowerCase();
1687
- const findByName = (predicate) => this.collaborationModes.find((entry) => predicate(entry.name.toLowerCase()));
1688
- let match = normalized === "read-only"
1689
- ? findByName((name) => name.includes("read") || name.includes("plan"))
1690
- : normalized === "full-access"
1691
- ? findByName((name) => name.includes("full") || name.includes("exec"))
1692
- : findByName((name) => name.includes("auto") || name.includes("code"));
1693
- if (!match) {
1694
- match = this.collaborationModes[0] ?? null;
1695
- }
1696
- if (!match)
1697
- return null;
1698
- const settings = {};
1699
- if (match.model)
1700
- settings.model = match.model;
1701
- if (match.reasoning_effort)
1702
- settings.reasoning_effort = match.reasoning_effort;
1703
- const developerInstructions = [
1704
- match.developer_instructions?.trim(),
1705
- this.config.systemPrompt?.trim(),
1706
- ]
1707
- .filter((entry) => typeof entry === "string" && entry.length > 0)
1708
- .join("\n\n");
1709
- if (developerInstructions)
1710
- settings.developer_instructions = developerInstructions;
1711
- if (this.config.model)
1712
- settings.model = this.config.model;
1713
- const thinkingOptionId = normalizeCodexThinkingOptionId(this.config.thinkingOptionId);
1714
- if (thinkingOptionId)
1715
- settings.reasoning_effort = thinkingOptionId;
1716
- return { mode: match.mode ?? "code", settings, name: match.name };
1717
- }
1718
- registerRequestHandlers() {
1719
- if (!this.client)
1720
- return;
1721
- this.client.setRequestHandler("item/commandExecution/requestApproval", (params) => this.handleCommandApprovalRequest(params));
1722
- this.client.setRequestHandler("item/fileChange/requestApproval", (params) => this.handleFileChangeApprovalRequest(params));
1723
- this.client.setRequestHandler("tool/requestUserInput", (params) => this.handleToolApprovalRequest(params));
1724
- }
1725
- async loadPersistedHistory() {
1726
- if (!this.client || !this.currentThreadId)
1727
- return;
1728
- try {
1729
- let rolloutTimeline = [];
1730
- try {
1731
- rolloutTimeline = await loadCodexPersistedTimeline(this.currentThreadId, undefined, this.logger);
1732
- }
1733
- catch {
1734
- rolloutTimeline = [];
1735
- }
1736
- const response = (await this.client.request("thread/read", {
1737
- threadId: this.currentThreadId,
1738
- includeTurns: true,
1739
- }));
1740
- const thread = response?.thread;
1741
- const threadTimeline = [];
1742
- if (thread && Array.isArray(thread.turns)) {
1743
- for (const turn of thread.turns) {
1744
- const items = Array.isArray(turn.items) ? turn.items : [];
1745
- for (const item of items) {
1746
- const timelineItem = threadItemToTimeline(item, {
1747
- cwd: this.config.cwd ?? null,
1748
- });
1749
- if (timelineItem) {
1750
- if (timelineItem.type === "tool_call") {
1751
- this.warnOnIncompleteEditToolCall(timelineItem, "thread_read", item);
1752
- }
1753
- threadTimeline.push(timelineItem);
1754
- }
1755
- }
1756
- }
1757
- }
1758
- const timeline = rolloutTimeline.length > 0 ? rolloutTimeline : threadTimeline;
1759
- if (timeline.length > 0) {
1760
- this.persistedHistory = timeline;
1761
- this.historyPending = true;
1762
- }
1763
- }
1764
- catch (error) {
1765
- this.logger.warn({ error }, "Failed to load Codex thread history");
1766
- }
1767
- }
1768
- async ensureThreadLoaded() {
1769
- if (!this.client || !this.currentThreadId)
1770
- return;
1771
- try {
1772
- const loaded = (await this.client.request("thread/loaded/list", {}));
1773
- const ids = Array.isArray(loaded?.data) ? loaded.data : [];
1774
- if (ids.includes(this.currentThreadId)) {
1775
- return;
1776
- }
1777
- const params = { threadId: this.currentThreadId };
1778
- if (this.config.systemPrompt?.trim()) {
1779
- params.developerInstructions = this.config.systemPrompt.trim();
1780
- }
1781
- const codexConfig = this.buildCodexInnerConfig();
1782
- if (codexConfig) {
1783
- params.config = codexConfig;
1784
- }
1785
- await this.client.request("thread/resume", params);
1786
- }
1787
- catch (error) {
1788
- this.logger.warn({ error }, "Failed to resume Codex thread, starting new thread");
1789
- this.currentThreadId = null;
1790
- await this.ensureThread();
1791
- }
1792
- }
1793
- parseSlashCommandInput(text) {
1794
- const trimmed = text.trim();
1795
- if (!trimmed.startsWith("/") || trimmed.length <= 1) {
1796
- return null;
1797
- }
1798
- const withoutPrefix = trimmed.slice(1);
1799
- const firstWhitespaceIdx = withoutPrefix.search(/\s/);
1800
- const commandName = firstWhitespaceIdx === -1 ? withoutPrefix : withoutPrefix.slice(0, firstWhitespaceIdx);
1801
- if (!commandName || commandName.includes("/")) {
1802
- return null;
1803
- }
1804
- const rawArgs = firstWhitespaceIdx === -1 ? "" : withoutPrefix.slice(firstWhitespaceIdx + 1).trim();
1805
- return rawArgs.length > 0 ? { commandName, args: rawArgs } : { commandName };
1806
- }
1807
- async resolveSlashCommandInvocation(prompt) {
1808
- if (typeof prompt !== "string") {
1809
- return null;
1810
- }
1811
- const parsed = this.parseSlashCommandInput(prompt);
1812
- if (!parsed) {
1813
- return null;
1814
- }
1815
- try {
1816
- const commands = await this.listCommands();
1817
- return commands.some((command) => command.name === parsed.commandName) ? parsed : null;
1818
- }
1819
- catch (error) {
1820
- this.logger.warn({ err: error, commandName: parsed.commandName }, "Failed to resolve slash command; falling back to plain prompt input");
1821
- return null;
1822
- }
1823
- }
1824
- async buildCommandPromptInput(commandName, args) {
1825
- if (commandName.startsWith("prompts:")) {
1826
- const promptName = commandName.slice("prompts:".length);
1827
- const codexHome = resolveCodexHomeDir();
1828
- const promptPath = path.join(codexHome, "prompts", `${promptName}.md`);
1829
- const raw = await fs.readFile(promptPath, "utf8");
1830
- const parsed = parseFrontMatter(raw);
1831
- return expandCodexCustomPrompt(parsed.body, args);
1832
- }
1833
- if (!this.connected) {
1834
- await this.connect();
1835
- }
1836
- else {
1837
- await this.loadSkills();
1838
- }
1839
- const skill = this.cachedSkills.find((entry) => entry.name === commandName);
1840
- if (skill) {
1841
- const input = [
1842
- { type: "skill", name: skill.name, path: skill.path },
1843
- ];
1844
- if (args && args.trim().length > 0) {
1845
- input.push({ type: "text", text: args.trim() });
1846
- }
1847
- else {
1848
- input.push({ type: "text", text: `$${skill.name}` });
1849
- }
1850
- return input;
1851
- }
1852
- return args ? `$${commandName} ${args}` : `$${commandName}`;
1853
- }
1854
- async run(prompt, options) {
1855
- const timeline = [];
1856
- let finalText = "";
1857
- let usage;
1858
- let turnId = null;
1859
- const bufferedEvents = [];
1860
- let settled = false;
1861
- let resolveCompletion;
1862
- let rejectCompletion;
1863
- const processEvent = (event) => {
1864
- if (settled) {
1865
- return;
1866
- }
1867
- const eventTurnId = event.turnId;
1868
- if (turnId && eventTurnId && eventTurnId !== turnId) {
1869
- return;
1870
- }
1871
- if (event.type === "timeline") {
1872
- timeline.push(event.item);
1873
- if (event.item.type === "assistant_message") {
1874
- finalText = event.item.text;
1875
- }
1876
- return;
1877
- }
1878
- if (event.type === "turn_completed") {
1879
- usage = event.usage;
1880
- settled = true;
1881
- resolveCompletion();
1882
- return;
1883
- }
1884
- if (event.type === "turn_failed") {
1885
- settled = true;
1886
- rejectCompletion(new Error(event.error));
1887
- return;
1888
- }
1889
- if (event.type === "turn_canceled") {
1890
- settled = true;
1891
- resolveCompletion();
1892
- }
1893
- };
1894
- const completion = new Promise((resolve, reject) => {
1895
- resolveCompletion = resolve;
1896
- rejectCompletion = reject;
1897
- });
1898
- const unsubscribe = this.subscribe((event) => {
1899
- if (!turnId) {
1900
- bufferedEvents.push(event);
1901
- return;
1902
- }
1903
- processEvent(event);
1904
- });
1905
- try {
1906
- const result = await this.startTurn(prompt, options);
1907
- turnId = result.turnId;
1908
- for (const event of bufferedEvents) {
1909
- processEvent(event);
1910
- }
1911
- if (!settled) {
1912
- await completion;
1913
- }
1914
- }
1915
- finally {
1916
- unsubscribe();
1917
- }
1918
- const info = await this.getRuntimeInfo();
1919
- return {
1920
- sessionId: info.sessionId ?? "",
1921
- finalText,
1922
- usage,
1923
- timeline,
1924
- };
1925
- }
1926
- async startTurn(prompt, options) {
1927
- if (this.activeForegroundTurnId) {
1928
- throw new Error("A foreground turn is already active");
1929
- }
1930
- await this.connect();
1931
- if (!this.client) {
1932
- throw new Error("Codex client not initialized");
1933
- }
1934
- const slashCommand = await this.resolveSlashCommandInvocation(prompt);
1935
- const effectivePrompt = slashCommand
1936
- ? await this.buildCommandPromptInput(slashCommand.commandName, slashCommand.args)
1937
- : prompt;
1938
- if (this.currentThreadId) {
1939
- await this.ensureThreadLoaded();
1940
- }
1941
- else {
1942
- await this.ensureThread();
1943
- }
1944
- const input = await this.buildUserInput(effectivePrompt);
1945
- const preset = MODE_PRESETS[this.currentMode] ?? MODE_PRESETS[DEFAULT_CODEX_MODE_ID];
1946
- const approvalPolicy = this.config.approvalPolicy ?? preset.approvalPolicy;
1947
- const sandboxPolicyType = this.config.sandboxMode ?? preset.sandbox;
1948
- const params = {
1949
- threadId: this.currentThreadId,
1950
- input,
1951
- approvalPolicy,
1952
- sandboxPolicy: toSandboxPolicy(sandboxPolicyType, typeof this.config.networkAccess === "boolean"
1953
- ? this.config.networkAccess
1954
- : preset.networkAccess),
1955
- };
1956
- if (this.config.model) {
1957
- params.model = this.config.model;
1958
- }
1959
- const thinkingOptionId = normalizeCodexThinkingOptionId(this.config.thinkingOptionId);
1960
- if (thinkingOptionId) {
1961
- params.effort = thinkingOptionId;
1962
- }
1963
- if (this.resolvedCollaborationMode) {
1964
- params.collaborationMode = {
1965
- mode: this.resolvedCollaborationMode.mode,
1966
- settings: this.resolvedCollaborationMode.settings,
1967
- };
1968
- }
1969
- if (this.config.cwd) {
1970
- params.cwd = this.config.cwd;
1971
- }
1972
- if (options?.outputSchema) {
1973
- params.outputSchema = options.outputSchema;
1974
- }
1975
- if (this.config.systemPrompt?.trim()) {
1976
- params.developerInstructions = this.config.systemPrompt.trim();
1977
- }
1978
- const codexConfig = this.buildCodexInnerConfig();
1979
- if (codexConfig) {
1980
- params.config = codexConfig;
1981
- }
1982
- const turnId = this.createTurnId();
1983
- this.activeForegroundTurnId = turnId;
1984
- try {
1985
- await this.client.request("turn/start", params, TURN_START_TIMEOUT_MS);
1986
- }
1987
- catch (error) {
1988
- this.activeForegroundTurnId = null;
1989
- throw error;
1990
- }
1991
- return { turnId };
1992
- }
1993
- subscribe(callback) {
1994
- this.subscribers.add(callback);
1995
- return () => {
1996
- this.subscribers.delete(callback);
1997
- };
1998
- }
1999
- async *streamHistory() {
2000
- if (!this.historyPending || this.persistedHistory.length === 0) {
2001
- return;
2002
- }
2003
- const history = this.persistedHistory;
2004
- this.persistedHistory = [];
2005
- this.historyPending = false;
2006
- for (const item of history) {
2007
- yield { type: "timeline", provider: CODEX_PROVIDER, item };
2008
- }
2009
- }
2010
- async getRuntimeInfo() {
2011
- if (this.cachedRuntimeInfo)
2012
- return { ...this.cachedRuntimeInfo };
2013
- if (!this.connected) {
2014
- await this.connect();
2015
- }
2016
- if (!this.currentThreadId) {
2017
- await this.ensureThread();
2018
- }
2019
- const info = {
2020
- provider: CODEX_PROVIDER,
2021
- sessionId: this.currentThreadId,
2022
- model: this.config.model ?? null,
2023
- thinkingOptionId: normalizeCodexThinkingOptionId(this.config.thinkingOptionId) ?? null,
2024
- modeId: this.currentMode ?? null,
2025
- extra: this.resolvedCollaborationMode
2026
- ? { collaborationMode: this.resolvedCollaborationMode.name }
2027
- : undefined,
2028
- };
2029
- this.cachedRuntimeInfo = info;
2030
- return { ...info };
2031
- }
2032
- async getAvailableModes() {
2033
- return CODEX_MODES;
2034
- }
2035
- async getCurrentMode() {
2036
- return this.currentMode ?? null;
2037
- }
2038
- async setMode(modeId) {
2039
- validateCodexMode(modeId);
2040
- this.currentMode = modeId;
2041
- this.resolvedCollaborationMode = this.resolveCollaborationMode(modeId);
2042
- this.cachedRuntimeInfo = null;
2043
- }
2044
- async setModel(modelId) {
2045
- this.config.model = modelId ?? undefined;
2046
- this.resolvedCollaborationMode = this.resolveCollaborationMode(this.currentMode);
2047
- this.cachedRuntimeInfo = null;
2048
- }
2049
- async setThinkingOption(thinkingOptionId) {
2050
- this.config.thinkingOptionId = normalizeCodexThinkingOptionId(thinkingOptionId);
2051
- this.resolvedCollaborationMode = this.resolveCollaborationMode(this.currentMode);
2052
- this.cachedRuntimeInfo = null;
2053
- }
2054
- getPendingPermissions() {
2055
- return Array.from(this.pendingPermissions.values());
2056
- }
2057
- async respondToPermission(requestId, response) {
2058
- const pending = this.pendingPermissionHandlers.get(requestId);
2059
- if (!pending) {
2060
- throw new Error(`No pending Codex app-server permission request with id '${requestId}'`);
2061
- }
2062
- const pendingRequest = this.pendingPermissions.get(requestId) ?? null;
2063
- this.pendingPermissionHandlers.delete(requestId);
2064
- this.pendingPermissions.delete(requestId);
2065
- this.resolvedPermissionRequests.add(requestId);
2066
- if (response.behavior === "deny" && pendingRequest?.kind === "tool") {
2067
- const fallbackName = pendingRequest.name === "CodexBash"
2068
- ? "shell"
2069
- : pendingRequest.name === "CodexFileChange"
2070
- ? "apply_patch"
2071
- : pendingRequest.name;
2072
- this.emitEvent({
2073
- type: "timeline",
2074
- provider: CODEX_PROVIDER,
2075
- item: {
2076
- type: "tool_call",
2077
- callId: requestId,
2078
- name: fallbackName,
2079
- status: "failed",
2080
- error: { message: response.message ?? "Permission denied" },
2081
- detail: pendingRequest.detail ?? {
2082
- type: "unknown",
2083
- input: pendingRequest.input ?? null,
2084
- output: null,
2085
- },
2086
- metadata: {
2087
- permissionRequestId: requestId,
2088
- denied: true,
2089
- },
2090
- },
2091
- });
2092
- }
2093
- this.emitEvent({
2094
- type: "permission_resolved",
2095
- provider: CODEX_PROVIDER,
2096
- requestId,
2097
- resolution: response,
2098
- });
2099
- if (pending.kind === "command") {
2100
- const decision = response.behavior === "allow" ? "accept" : response.interrupt ? "cancel" : "decline";
2101
- pending.resolve({ decision });
2102
- return;
2103
- }
2104
- if (pending.kind === "file") {
2105
- const decision = response.behavior === "allow" ? "accept" : response.interrupt ? "cancel" : "decline";
2106
- pending.resolve({ decision });
2107
- return;
2108
- }
2109
- // tool/requestUserInput
2110
- const answers = {};
2111
- const questions = pending.questions ?? [];
2112
- const decision = response.behavior === "allow" ? "accept" : response.interrupt ? "cancel" : "decline";
2113
- for (const question of questions) {
2114
- let picked = decision;
2115
- const options = question.options ?? [];
2116
- if (options.length > 0) {
2117
- const byLabel = options.find((opt) => (opt.label ?? "").toLowerCase().includes(decision));
2118
- const byValue = options.find((opt) => (opt.value ?? "").toLowerCase().includes(decision));
2119
- const option = byLabel ?? byValue ?? options[0];
2120
- picked = option.value ?? option.label ?? decision;
2121
- }
2122
- answers[question.id] = { answers: [picked] };
2123
- }
2124
- if (questions.length === 0) {
2125
- answers["default"] = { answers: [decision] };
2126
- }
2127
- pending.resolve({ answers });
2128
- }
2129
- describePersistence() {
2130
- if (!this.currentThreadId)
2131
- return null;
2132
- const thinkingOptionId = normalizeCodexThinkingOptionId(this.config.thinkingOptionId) ?? null;
2133
- return {
2134
- provider: CODEX_PROVIDER,
2135
- sessionId: this.currentThreadId,
2136
- nativeHandle: this.currentThreadId,
2137
- metadata: {
2138
- provider: CODEX_PROVIDER,
2139
- cwd: this.config.cwd,
2140
- title: this.config.title ?? null,
2141
- threadId: this.currentThreadId,
2142
- modeId: this.currentMode,
2143
- model: this.config.model ?? null,
2144
- thinkingOptionId,
2145
- extra: this.config.extra,
2146
- systemPrompt: this.config.systemPrompt,
2147
- mcpServers: this.config.mcpServers,
2148
- },
2149
- };
2150
- }
2151
- async interrupt() {
2152
- if (!this.client || !this.currentThreadId || !this.currentTurnId)
2153
- return;
2154
- try {
2155
- await this.client.request("turn/interrupt", {
2156
- threadId: this.currentThreadId,
2157
- turnId: this.currentTurnId,
2158
- });
2159
- }
2160
- catch (error) {
2161
- this.logger.warn({ error }, "Failed to interrupt Codex turn");
2162
- }
2163
- }
2164
- async close() {
2165
- for (const pending of this.pendingPermissionHandlers.values()) {
2166
- pending.resolve({ decision: "cancel" });
2167
- }
2168
- this.pendingPermissionHandlers.clear();
2169
- this.pendingPermissions.clear();
2170
- this.resolvedPermissionRequests.clear();
2171
- this.subscribers.clear();
2172
- this.activeForegroundTurnId = null;
2173
- if (this.client) {
2174
- await this.client.dispose();
2175
- }
2176
- this.client = null;
2177
- this.connected = false;
2178
- this.currentThreadId = null;
2179
- this.currentTurnId = null;
2180
- }
2181
- async listCommands() {
2182
- const prompts = await listCodexCustomPrompts();
2183
- if (!this.connected) {
2184
- await this.connect();
2185
- }
2186
- else {
2187
- await this.loadSkills();
2188
- }
2189
- const appServerSkills = this.cachedSkills.map((skill) => ({
2190
- name: skill.name,
2191
- description: skill.description,
2192
- argumentHint: "",
2193
- }));
2194
- const fallbackSkills = appServerSkills.length === 0 ? await listCodexSkills(this.config.cwd) : [];
2195
- return [...appServerSkills, ...fallbackSkills, ...prompts].sort((a, b) => a.name.localeCompare(b.name));
2196
- }
2197
- async ensureThread() {
2198
- if (!this.client)
2199
- return;
2200
- if (this.currentThreadId)
2201
- return;
2202
- // Resolve model + thinking defaults when omitted.
2203
- let configuredDefaults = {};
2204
- let model = this.config.model;
2205
- let thinkingOptionId = normalizeCodexThinkingOptionId(this.config.thinkingOptionId);
2206
- if (!model || !thinkingOptionId) {
2207
- configuredDefaults = await readCodexConfiguredDefaults(this.client, this.logger);
2208
- }
2209
- if (!model) {
2210
- model = configuredDefaults.model;
2211
- }
2212
- if (!thinkingOptionId) {
2213
- thinkingOptionId = configuredDefaults.thinkingOptionId;
2214
- }
2215
- if (!model || !thinkingOptionId) {
2216
- const modelResponse = (await this.client.request("model/list", {}));
2217
- const models = modelResponse?.data ?? [];
2218
- const defaultModel = models.find((m) => m.isDefault) ?? models[0];
2219
- if (!defaultModel) {
2220
- throw new Error("No models available from Codex app-server");
2221
- }
2222
- const selectedModel = (model ? models.find((candidate) => candidate.id === model) : undefined) ?? defaultModel;
2223
- if (!model) {
2224
- model = selectedModel.id;
2225
- }
2226
- if (!thinkingOptionId) {
2227
- thinkingOptionId = normalizeCodexThinkingOptionId(selectedModel.defaultReasoningEffort);
2228
- }
2229
- }
2230
- this.config.model = model;
2231
- this.config.thinkingOptionId = thinkingOptionId;
2232
- const preset = MODE_PRESETS[this.currentMode] ?? MODE_PRESETS[DEFAULT_CODEX_MODE_ID];
2233
- const approvalPolicy = this.config.approvalPolicy ?? preset.approvalPolicy;
2234
- const sandbox = this.config.sandboxMode ?? preset.sandbox;
2235
- const innerConfig = this.buildCodexInnerConfig();
2236
- const response = (await this.client.request("thread/start", {
2237
- model,
2238
- cwd: this.config.cwd ?? null,
2239
- approvalPolicy,
2240
- sandbox,
2241
- ...(this.config.systemPrompt?.trim()
2242
- ? { developerInstructions: this.config.systemPrompt.trim() }
2243
- : {}),
2244
- ...(innerConfig ? { config: innerConfig } : {}),
2245
- }));
2246
- const threadId = response?.thread?.id;
2247
- if (!threadId) {
2248
- throw new Error("Codex app-server did not return thread id");
2249
- }
2250
- this.currentThreadId = threadId;
2251
- }
2252
- buildCodexInnerConfig() {
2253
- const innerConfig = {};
2254
- if (this.config.mcpServers) {
2255
- const mcpServers = {};
2256
- for (const [name, serverConfig] of Object.entries(this.config.mcpServers)) {
2257
- mcpServers[name] = toCodexMcpConfig(serverConfig);
2258
- }
2259
- innerConfig.mcp_servers = mcpServers;
2260
- }
2261
- if (this.config.extra?.codex) {
2262
- Object.assign(innerConfig, this.config.extra.codex);
2263
- }
2264
- return Object.keys(innerConfig).length > 0 ? innerConfig : null;
2265
- }
2266
- async buildUserInput(prompt) {
2267
- if (typeof prompt === "string") {
2268
- return [{ type: "text", text: prompt }];
2269
- }
2270
- const blocks = prompt;
2271
- return await codexAppServerTurnInputFromPrompt(blocks, this.logger);
2272
- }
2273
- emitEvent(event) {
2274
- if (event.type === "timeline") {
2275
- if (event.item.type === "assistant_message") {
2276
- this.pendingAgentMessages.clear();
2277
- }
2278
- }
2279
- this.notifySubscribers(event);
2280
- }
2281
- notifySubscribers(event) {
2282
- const turnId = this.activeForegroundTurnId;
2283
- const tagged = turnId ? { ...event, turnId } : event;
2284
- for (const callback of this.subscribers) {
2285
- try {
2286
- callback(tagged);
2287
- }
2288
- catch (error) {
2289
- this.logger.warn({ err: error }, "Subscriber callback threw");
2290
- }
2291
- }
2292
- }
2293
- createTurnId() {
2294
- return `codex-turn-${this.nextTurnOrdinal++}`;
2295
- }
2296
- handleNotification(method, params) {
2297
- const parsed = CodexNotificationSchema.parse({ method, params });
2298
- if (parsed.kind === "thread_started") {
2299
- this.currentThreadId = parsed.threadId;
2300
- this.emitEvent({
2301
- type: "thread_started",
2302
- provider: CODEX_PROVIDER,
2303
- sessionId: parsed.threadId,
2304
- });
2305
- return;
2306
- }
2307
- if (parsed.kind === "turn_started") {
2308
- this.currentTurnId = parsed.turnId;
2309
- this.emittedItemStartedIds.clear();
2310
- this.emittedItemCompletedIds.clear();
2311
- this.emittedExecCommandStartedCallIds.clear();
2312
- this.emittedExecCommandCompletedCallIds.clear();
2313
- this.pendingCommandOutputDeltas.clear();
2314
- this.pendingFileChangeOutputDeltas.clear();
2315
- this.warnedIncompleteEditToolCallIds.clear();
2316
- this.emitEvent({ type: "turn_started", provider: CODEX_PROVIDER });
2317
- return;
2318
- }
2319
- if (parsed.kind === "turn_completed") {
2320
- if (parsed.status === "failed") {
2321
- this.emitEvent({
2322
- type: "turn_failed",
2323
- provider: CODEX_PROVIDER,
2324
- error: parsed.errorMessage ?? "Codex turn failed",
2325
- });
2326
- }
2327
- else if (parsed.status === "interrupted") {
2328
- this.emitEvent({ type: "turn_canceled", provider: CODEX_PROVIDER, reason: "interrupted" });
2329
- }
2330
- else {
2331
- this.emitEvent({
2332
- type: "turn_completed",
2333
- provider: CODEX_PROVIDER,
2334
- usage: this.latestUsage,
2335
- });
2336
- }
2337
- this.activeForegroundTurnId = null;
2338
- this.emittedItemStartedIds.clear();
2339
- this.emittedItemCompletedIds.clear();
2340
- this.emittedExecCommandStartedCallIds.clear();
2341
- this.emittedExecCommandCompletedCallIds.clear();
2342
- this.pendingCommandOutputDeltas.clear();
2343
- this.pendingFileChangeOutputDeltas.clear();
2344
- this.warnedIncompleteEditToolCallIds.clear();
2345
- return;
2346
- }
2347
- if (parsed.kind === "plan_updated") {
2348
- const items = planStepsToTodoItems(parsed.plan.map((entry) => ({
2349
- step: entry.step ?? "",
2350
- status: entry.status ?? "pending",
2351
- })));
2352
- this.emitEvent({
2353
- type: "timeline",
2354
- provider: CODEX_PROVIDER,
2355
- item: { type: "todo", items },
2356
- });
2357
- return;
2358
- }
2359
- if (parsed.kind === "diff_updated") {
2360
- // NOTE: Codex app-server emits frequent `turn/diff/updated` notifications
2361
- // containing a full accumulated unified diff for the *entire turn*.
2362
- // This is not a concrete file-change tool call; it is progress telemetry.
2363
- // We intentionally do NOT store every diff update in the timeline.
2364
- return;
2365
- }
2366
- if (parsed.kind === "token_usage_updated") {
2367
- this.latestUsage = toAgentUsage(parsed.tokenUsage);
2368
- return;
2369
- }
2370
- if (parsed.kind === "agent_message_delta") {
2371
- const prev = this.pendingAgentMessages.get(parsed.itemId) ?? "";
2372
- this.pendingAgentMessages.set(parsed.itemId, prev + parsed.delta);
2373
- return;
2374
- }
2375
- if (parsed.kind === "reasoning_delta") {
2376
- const prev = this.pendingReasoning.get(parsed.itemId) ?? [];
2377
- prev.push(parsed.delta);
2378
- this.pendingReasoning.set(parsed.itemId, prev);
2379
- return;
2380
- }
2381
- if (parsed.kind === "exec_command_output_delta") {
2382
- this.appendOutputDeltaChunk(this.pendingCommandOutputDeltas, parsed.callId, parsed.chunk, {
2383
- decodeBase64: true,
2384
- });
2385
- return;
2386
- }
2387
- if (parsed.kind === "file_change_output_delta") {
2388
- this.appendOutputDeltaChunk(this.pendingFileChangeOutputDeltas, parsed.itemId, parsed.delta);
2389
- return;
2390
- }
2391
- if (parsed.kind === "exec_command_started") {
2392
- if (parsed.callId) {
2393
- this.emittedExecCommandStartedCallIds.add(parsed.callId);
2394
- this.pendingCommandOutputDeltas.delete(parsed.callId);
2395
- }
2396
- const timelineItem = mapCodexExecNotificationToToolCall({
2397
- callId: parsed.callId,
2398
- command: parsed.command,
2399
- cwd: parsed.cwd ?? this.config.cwd ?? null,
2400
- running: true,
2401
- });
2402
- if (timelineItem) {
2403
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2404
- }
2405
- return;
2406
- }
2407
- if (parsed.kind === "exec_command_completed") {
2408
- const bufferedOutput = this.consumeOutputDelta(this.pendingCommandOutputDeltas, parsed.callId);
2409
- const resolvedOutput = parsed.output ?? bufferedOutput;
2410
- this.rememberTerminalProcessForCommand(parsed.command, resolvedOutput);
2411
- const timelineItem = mapCodexExecNotificationToToolCall({
2412
- callId: parsed.callId,
2413
- command: parsed.command,
2414
- cwd: parsed.cwd ?? this.config.cwd ?? null,
2415
- output: resolvedOutput,
2416
- exitCode: parsed.exitCode,
2417
- success: parsed.success,
2418
- stderr: parsed.stderr,
2419
- running: false,
2420
- });
2421
- if (timelineItem) {
2422
- this.emittedExecCommandCompletedCallIds.add(timelineItem.callId);
2423
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2424
- }
2425
- return;
2426
- }
2427
- if (parsed.kind === "terminal_interaction") {
2428
- const interactionKey = [parsed.processId ?? "", parsed.stdin ?? ""].join("\u0000");
2429
- if (!this.shouldEmitTerminalInteractionKey(interactionKey)) {
2430
- return;
2431
- }
2432
- const command = (parsed.processId ? this.terminalCommandByProcessId.get(parsed.processId) : undefined) ??
2433
- null;
2434
- if (!command && parsed.processId) {
2435
- this.pendingUnlabeledTerminalInteractions.add(parsed.processId);
2436
- }
2437
- const timelineItem = mapCodexTerminalInteractionToToolCall({
2438
- processId: parsed.processId,
2439
- fallbackCallId: parsed.callId,
2440
- command,
2441
- });
2442
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2443
- return;
2444
- }
2445
- if (parsed.kind === "patch_apply_started") {
2446
- if (parsed.callId) {
2447
- this.pendingFileChangeOutputDeltas.delete(parsed.callId);
2448
- }
2449
- const timelineItem = mapCodexPatchNotificationToToolCall({
2450
- callId: parsed.callId,
2451
- changes: parsed.changes,
2452
- cwd: this.config.cwd ?? null,
2453
- running: true,
2454
- });
2455
- if (timelineItem) {
2456
- this.warnOnIncompleteEditToolCall(timelineItem, "patch_apply_started", {
2457
- callId: parsed.callId,
2458
- changes: parsed.changes,
2459
- });
2460
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2461
- }
2462
- return;
2463
- }
2464
- if (parsed.kind === "patch_apply_completed") {
2465
- const bufferedOutput = this.consumeOutputDelta(this.pendingFileChangeOutputDeltas, parsed.callId);
2466
- const timelineItem = mapCodexPatchNotificationToToolCall({
2467
- callId: parsed.callId,
2468
- changes: parsed.changes,
2469
- cwd: this.config.cwd ?? null,
2470
- stdout: parsed.stdout ?? bufferedOutput,
2471
- stderr: parsed.stderr,
2472
- success: parsed.success,
2473
- running: false,
2474
- });
2475
- if (timelineItem) {
2476
- this.warnOnIncompleteEditToolCall(timelineItem, "patch_apply_completed", {
2477
- callId: parsed.callId,
2478
- changes: parsed.changes,
2479
- stdout: parsed.stdout,
2480
- });
2481
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2482
- }
2483
- return;
2484
- }
2485
- if (parsed.kind === "item_completed") {
2486
- // Codex emits mirrored lifecycle notifications via both `codex/event/item_*`
2487
- // and canonical `item/*`. We render only the canonical channel to avoid
2488
- // duplicated assistant/reasoning rows.
2489
- if (parsed.source === "codex_event") {
2490
- return;
2491
- }
2492
- const timelineItem = threadItemToTimeline(parsed.item, {
2493
- includeUserMessage: false,
2494
- cwd: this.config.cwd ?? null,
2495
- });
2496
- if (timelineItem) {
2497
- const normalizedItemType = normalizeCodexThreadItemType(typeof parsed.item.type === "string" ? parsed.item.type : undefined);
2498
- const itemId = parsed.item.id;
2499
- // For commandExecution items, codex/event/exec_command_* is authoritative.
2500
- // Keep item/completed as fallback only when no exec_command completion was seen.
2501
- if (timelineItem.type === "tool_call" && normalizedItemType === "commandExecution") {
2502
- const callId = timelineItem.callId || itemId;
2503
- if (callId && this.emittedExecCommandCompletedCallIds.has(callId)) {
2504
- return;
2505
- }
2506
- }
2507
- if (itemId && this.emittedItemCompletedIds.has(itemId)) {
2508
- return;
2509
- }
2510
- if (timelineItem.type === "assistant_message" && itemId) {
2511
- const buffered = this.pendingAgentMessages.get(itemId);
2512
- if (buffered && buffered.length > 0) {
2513
- timelineItem.text = buffered;
2514
- }
2515
- }
2516
- if (timelineItem.type === "reasoning" && itemId) {
2517
- const buffered = this.pendingReasoning.get(itemId);
2518
- if (buffered && buffered.length > 0) {
2519
- timelineItem.text = buffered.join("");
2520
- }
2521
- }
2522
- if (timelineItem.type === "tool_call") {
2523
- this.warnOnIncompleteEditToolCall(timelineItem, "item_completed", parsed.item);
2524
- }
2525
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2526
- if (itemId) {
2527
- this.emittedItemCompletedIds.add(itemId);
2528
- this.emittedItemStartedIds.delete(itemId);
2529
- this.pendingCommandOutputDeltas.delete(itemId);
2530
- this.pendingFileChangeOutputDeltas.delete(itemId);
2531
- }
2532
- }
2533
- return;
2534
- }
2535
- if (parsed.kind === "item_started") {
2536
- if (parsed.source === "codex_event") {
2537
- return;
2538
- }
2539
- const timelineItem = threadItemToTimeline(parsed.item, {
2540
- includeUserMessage: false,
2541
- cwd: this.config.cwd ?? null,
2542
- });
2543
- if (timelineItem && timelineItem.type === "tool_call") {
2544
- const normalizedItemType = normalizeCodexThreadItemType(typeof parsed.item.type === "string" ? parsed.item.type : undefined);
2545
- const itemId = parsed.item.id;
2546
- if (normalizedItemType === "commandExecution") {
2547
- const callId = timelineItem.callId || itemId;
2548
- if (callId && this.emittedExecCommandStartedCallIds.has(callId)) {
2549
- return;
2550
- }
2551
- }
2552
- if (itemId && this.emittedItemStartedIds.has(itemId)) {
2553
- return;
2554
- }
2555
- this.warnOnIncompleteEditToolCall(timelineItem, "item_started", parsed.item);
2556
- this.emitEvent({ type: "timeline", provider: CODEX_PROVIDER, item: timelineItem });
2557
- if (itemId) {
2558
- this.emittedItemStartedIds.add(itemId);
2559
- this.pendingCommandOutputDeltas.delete(itemId);
2560
- this.pendingFileChangeOutputDeltas.delete(itemId);
2561
- }
2562
- }
2563
- return;
2564
- }
2565
- if (parsed.kind === "invalid_payload") {
2566
- this.warnInvalidNotificationPayload(parsed.method, parsed.params);
2567
- return;
2568
- }
2569
- this.warnUnknownNotificationMethod(parsed.method, parsed.params);
2570
- }
2571
- warnUnknownNotificationMethod(method, params) {
2572
- if (this.warnedUnknownNotificationMethods.has(method)) {
2573
- return;
2574
- }
2575
- this.warnedUnknownNotificationMethods.add(method);
2576
- this.logger.trace({ method, params }, "Unhandled Codex app-server notification method");
2577
- }
2578
- warnInvalidNotificationPayload(method, params) {
2579
- const key = method;
2580
- if (this.warnedInvalidNotificationPayloads.has(key)) {
2581
- return;
2582
- }
2583
- this.warnedInvalidNotificationPayloads.add(key);
2584
- this.logger.warn({ method, params }, "Invalid Codex app-server notification payload");
2585
- }
2586
- appendOutputDeltaChunk(store, id, chunk, options) {
2587
- if (!id || !chunk) {
2588
- return;
2589
- }
2590
- const normalized = options?.decodeBase64 ? decodeCodexOutputDeltaChunk(chunk) : chunk;
2591
- if (!normalized.length) {
2592
- return;
2593
- }
2594
- const prev = store.get(id) ?? [];
2595
- prev.push(normalized);
2596
- store.set(id, prev);
2597
- }
2598
- consumeOutputDelta(store, id) {
2599
- if (!id) {
2600
- return null;
2601
- }
2602
- const buffered = store.get(id);
2603
- if (!buffered || buffered.length === 0) {
2604
- return null;
2605
- }
2606
- store.delete(id);
2607
- return buffered.join("");
2608
- }
2609
- rememberTerminalProcessForCommand(command, output) {
2610
- const normalizedCommand = normalizeCodexCommandValue(command);
2611
- if (!normalizedCommand) {
2612
- return;
2613
- }
2614
- const displayCommand = typeof normalizedCommand === "string"
2615
- ? normalizedCommand
2616
- : normalizedCommand.join(" ").trim();
2617
- if (!displayCommand) {
2618
- return;
2619
- }
2620
- const processId = extractCodexTerminalSessionId(output ?? undefined);
2621
- if (!processId) {
2622
- return;
2623
- }
2624
- this.terminalCommandByProcessId.set(processId, displayCommand);
2625
- if (!this.pendingUnlabeledTerminalInteractions.has(processId)) {
2626
- return;
2627
- }
2628
- this.pendingUnlabeledTerminalInteractions.delete(processId);
2629
- this.emitEvent({
2630
- type: "timeline",
2631
- provider: CODEX_PROVIDER,
2632
- item: mapCodexTerminalInteractionToToolCall({
2633
- processId,
2634
- command: displayCommand,
2635
- }),
2636
- });
2637
- }
2638
- shouldEmitTerminalInteractionKey(key) {
2639
- if (this.emittedTerminalInteractionKeys.has(key)) {
2640
- return false;
2641
- }
2642
- this.emittedTerminalInteractionKeys.add(key);
2643
- return true;
2644
- }
2645
- warnOnIncompleteEditToolCall(item, source, payload) {
2646
- if (!isEditToolCallWithoutContent(item)) {
2647
- return;
2648
- }
2649
- const warnKey = `${source}:${item.callId}`;
2650
- if (this.warnedIncompleteEditToolCallIds.has(warnKey)) {
2651
- return;
2652
- }
2653
- this.warnedIncompleteEditToolCallIds.add(warnKey);
2654
- this.logger.warn({
2655
- source,
2656
- callId: item.callId,
2657
- status: item.status,
2658
- name: item.name,
2659
- detail: item.detail,
2660
- payload,
2661
- }, "Codex edit tool call is missing diff/content fields");
2662
- }
2663
- handleCommandApprovalRequest(params) {
2664
- const parsed = params;
2665
- const commandPreview = mapCodexExecNotificationToToolCall({
2666
- callId: parsed.itemId,
2667
- command: parsed.command,
2668
- cwd: parsed.cwd ?? this.config.cwd ?? null,
2669
- running: true,
2670
- });
2671
- const requestId = `permission-${parsed.itemId}`;
2672
- const title = parsed.command ? `Run command: ${parsed.command}` : "Run command";
2673
- const request = {
2674
- id: requestId,
2675
- provider: CODEX_PROVIDER,
2676
- name: "CodexBash",
2677
- kind: "tool",
2678
- title,
2679
- description: parsed.reason ?? undefined,
2680
- input: {
2681
- command: parsed.command ?? undefined,
2682
- cwd: parsed.cwd ?? undefined,
2683
- },
2684
- detail: commandPreview?.detail ?? {
2685
- type: "unknown",
2686
- input: {
2687
- command: parsed.command ?? null,
2688
- cwd: parsed.cwd ?? null,
2689
- },
2690
- output: null,
2691
- },
2692
- metadata: {
2693
- itemId: parsed.itemId,
2694
- threadId: parsed.threadId,
2695
- turnId: parsed.turnId,
2696
- },
2697
- };
2698
- this.pendingPermissions.set(requestId, request);
2699
- this.emitEvent({ type: "permission_requested", provider: CODEX_PROVIDER, request });
2700
- return new Promise((resolve) => {
2701
- this.pendingPermissionHandlers.set(requestId, { resolve, kind: "command" });
2702
- });
2703
- }
2704
- handleFileChangeApprovalRequest(params) {
2705
- const parsed = params;
2706
- const requestId = `permission-${parsed.itemId}`;
2707
- const request = {
2708
- id: requestId,
2709
- provider: CODEX_PROVIDER,
2710
- name: "CodexFileChange",
2711
- kind: "tool",
2712
- title: "Apply file changes",
2713
- description: parsed.reason ?? undefined,
2714
- detail: {
2715
- type: "unknown",
2716
- input: {
2717
- reason: parsed.reason ?? null,
2718
- },
2719
- output: null,
2720
- },
2721
- metadata: {
2722
- itemId: parsed.itemId,
2723
- threadId: parsed.threadId,
2724
- turnId: parsed.turnId,
2725
- },
2726
- };
2727
- this.pendingPermissions.set(requestId, request);
2728
- this.emitEvent({ type: "permission_requested", provider: CODEX_PROVIDER, request });
2729
- return new Promise((resolve) => {
2730
- this.pendingPermissionHandlers.set(requestId, { resolve, kind: "file" });
2731
- });
2732
- }
2733
- handleToolApprovalRequest(params) {
2734
- const parsed = params;
2735
- const requestId = `permission-${parsed.itemId}`;
2736
- const request = {
2737
- id: requestId,
2738
- provider: CODEX_PROVIDER,
2739
- name: "CodexTool",
2740
- kind: "tool",
2741
- title: "Tool action requires approval",
2742
- description: undefined,
2743
- detail: {
2744
- type: "unknown",
2745
- input: {
2746
- questions: Array.isArray(parsed.questions) ? parsed.questions : [],
2747
- },
2748
- output: null,
2749
- },
2750
- metadata: {
2751
- itemId: parsed.itemId,
2752
- threadId: parsed.threadId,
2753
- turnId: parsed.turnId,
2754
- questions: parsed.questions,
2755
- },
2756
- };
2757
- this.pendingPermissions.set(requestId, request);
2758
- this.emitEvent({ type: "permission_requested", provider: CODEX_PROVIDER, request });
2759
- return new Promise((resolve) => {
2760
- this.pendingPermissionHandlers.set(requestId, {
2761
- resolve,
2762
- kind: "tool",
2763
- questions: Array.isArray(parsed.questions) ? parsed.questions : [],
2764
- });
2765
- });
2766
- }
2767
- }
2768
- export class CodexAppServerAgentClient {
2769
- constructor(logger, runtimeSettings) {
2770
- this.logger = logger;
2771
- this.runtimeSettings = runtimeSettings;
2772
- this.provider = CODEX_PROVIDER;
2773
- this.capabilities = CODEX_APP_SERVER_CAPABILITIES;
2774
- }
2775
- spawnAppServer(launchEnv) {
2776
- const launchPrefix = resolveCodexLaunchPrefix(this.runtimeSettings);
2777
- this.logger.trace({
2778
- launchPrefix,
2779
- }, "Spawning Codex app server");
2780
- return spawn(launchPrefix.command, [...launchPrefix.args, "app-server"], {
2781
- detached: process.platform !== "win32",
2782
- stdio: ["pipe", "pipe", "pipe"],
2783
- env: buildCodexAppServerEnv(this.runtimeSettings, launchEnv),
2784
- });
2785
- }
2786
- async createSession(config, launchContext) {
2787
- const sessionConfig = { ...config, provider: CODEX_PROVIDER };
2788
- const session = new CodexAppServerAgentSession(sessionConfig, null, this.logger, () => this.spawnAppServer(launchContext?.env));
2789
- await session.connect();
2790
- return session;
2791
- }
2792
- async resumeSession(handle, overrides, launchContext) {
2793
- const storedConfig = (handle.metadata ?? {});
2794
- const merged = {
2795
- ...storedConfig,
2796
- ...overrides,
2797
- provider: CODEX_PROVIDER,
2798
- cwd: overrides?.cwd ?? storedConfig.cwd ?? process.cwd(),
2799
- };
2800
- const session = new CodexAppServerAgentSession(merged, handle, this.logger, () => this.spawnAppServer(launchContext?.env));
2801
- await session.connect();
2802
- return session;
2803
- }
2804
- async listPersistedAgents(options) {
2805
- const child = this.spawnAppServer();
2806
- const client = new CodexAppServerClient(child, this.logger);
2807
- try {
2808
- await client.request("initialize", {
2809
- clientInfo: { name: "paseo", title: "Paseo", version: "0.0.0" },
2810
- });
2811
- client.notify("initialized", {});
2812
- const limit = options?.limit ?? 20;
2813
- const response = (await client.request("thread/list", { limit }));
2814
- const threads = Array.isArray(response?.data) ? response.data : [];
2815
- const descriptors = [];
2816
- for (const thread of threads.slice(0, limit)) {
2817
- const threadId = thread.id;
2818
- const cwd = thread.cwd ?? process.cwd();
2819
- const title = thread.preview ?? null;
2820
- let timeline = [];
2821
- try {
2822
- const rolloutTimeline = await loadCodexPersistedTimeline(threadId, undefined, this.logger);
2823
- const read = (await client.request("thread/read", {
2824
- threadId,
2825
- includeTurns: true,
2826
- }));
2827
- const turns = read.thread?.turns ?? [];
2828
- const itemsFromThreadRead = [];
2829
- for (const turn of turns) {
2830
- for (const item of turn.items ?? []) {
2831
- const timelineItem = threadItemToTimeline(item, { cwd });
2832
- if (timelineItem)
2833
- itemsFromThreadRead.push(timelineItem);
2834
- }
2835
- }
2836
- timeline = rolloutTimeline.length > 0 ? rolloutTimeline : itemsFromThreadRead;
2837
- }
2838
- catch {
2839
- timeline = [];
2840
- }
2841
- descriptors.push({
2842
- provider: CODEX_PROVIDER,
2843
- sessionId: threadId,
2844
- cwd,
2845
- title,
2846
- lastActivityAt: new Date((thread.updatedAt ?? thread.createdAt ?? 0) * 1000),
2847
- persistence: {
2848
- provider: CODEX_PROVIDER,
2849
- sessionId: threadId,
2850
- nativeHandle: threadId,
2851
- metadata: {
2852
- provider: CODEX_PROVIDER,
2853
- cwd,
2854
- title,
2855
- threadId,
2856
- },
2857
- },
2858
- timeline,
2859
- });
2860
- }
2861
- return descriptors;
2862
- }
2863
- finally {
2864
- await client.dispose();
2865
- }
2866
- }
2867
- async listModels(_options) {
2868
- const child = this.spawnAppServer();
2869
- const client = new CodexAppServerClient(child, this.logger);
2870
- try {
2871
- await client.request("initialize", {
2872
- clientInfo: {
2873
- name: "paseo",
2874
- title: "Paseo",
2875
- version: "0.0.0",
2876
- },
2877
- });
2878
- client.notify("initialized", {});
2879
- const response = (await client.request("model/list", {}));
2880
- const models = Array.isArray(response?.data) ? response.data : [];
2881
- const configuredDefaults = await readCodexConfiguredDefaults(client, this.logger);
2882
- const configuredDefaultModelId = configuredDefaults.model;
2883
- const configuredDefaultThinkingOptionId = configuredDefaults.thinkingOptionId;
2884
- const hasConfiguredDefaultModel = typeof configuredDefaultModelId === "string"
2885
- ? models.some((model) => model?.id === configuredDefaultModelId)
2886
- : false;
2887
- return models.map((model) => {
2888
- const defaultReasoningEffort = normalizeCodexThinkingOptionId(typeof model.defaultReasoningEffort === "string" ? model.defaultReasoningEffort : null);
2889
- const resolvedDefaultReasoningEffort = configuredDefaultThinkingOptionId ?? defaultReasoningEffort;
2890
- const thinkingById = new Map();
2891
- if (Array.isArray(model.supportedReasoningEfforts)) {
2892
- for (const entry of model.supportedReasoningEfforts) {
2893
- const id = normalizeCodexThinkingOptionId(typeof entry?.reasoningEffort === "string" ? entry.reasoningEffort : null);
2894
- if (!id)
2895
- continue;
2896
- const description = typeof entry?.description === "string" && entry.description.trim().length > 0
2897
- ? entry.description
2898
- : undefined;
2899
- thinkingById.set(id, { id, label: id, description });
2900
- }
2901
- }
2902
- if (resolvedDefaultReasoningEffort && !thinkingById.has(resolvedDefaultReasoningEffort)) {
2903
- thinkingById.set(resolvedDefaultReasoningEffort, {
2904
- id: resolvedDefaultReasoningEffort,
2905
- label: resolvedDefaultReasoningEffort,
2906
- description: configuredDefaultThinkingOptionId === resolvedDefaultReasoningEffort
2907
- ? "Configured default reasoning effort"
2908
- : "Model default reasoning effort",
2909
- });
2910
- }
2911
- const thinkingOptions = Array.from(thinkingById.values()).map((option) => ({
2912
- ...option,
2913
- isDefault: option.id === resolvedDefaultReasoningEffort,
2914
- }));
2915
- const defaultThinkingOptionId = resolvedDefaultReasoningEffort ??
2916
- thinkingOptions.find((option) => option.isDefault)?.id ??
2917
- thinkingOptions[0]?.id;
2918
- const isDefaultModel = hasConfiguredDefaultModel
2919
- ? model.id === configuredDefaultModelId
2920
- : model.isDefault;
2921
- return {
2922
- provider: CODEX_PROVIDER,
2923
- id: model.id,
2924
- label: normalizeCodexModelLabel(model.displayName),
2925
- description: model.description,
2926
- isDefault: isDefaultModel,
2927
- thinkingOptions: thinkingOptions.length > 0 ? thinkingOptions : undefined,
2928
- defaultThinkingOptionId,
2929
- metadata: {
2930
- model: model.model,
2931
- defaultReasoningEffort: model.defaultReasoningEffort,
2932
- supportedReasoningEfforts: model.supportedReasoningEfforts,
2933
- },
2934
- };
2935
- });
2936
- }
2937
- finally {
2938
- await client.dispose();
2939
- }
2940
- }
2941
- async isAvailable() {
2942
- const command = this.runtimeSettings?.command;
2943
- if (command?.mode === "replace") {
2944
- return existsSync(command.argv[0]);
2945
- }
2946
- return true;
2947
- }
2948
- }
2949
- //# sourceMappingURL=codex-app-server-agent.js.map