@getpaseo/server 0.1.2

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