@zvada/agent-server 0.3.21 → 0.3.23

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 (367) hide show
  1. package/CHANGELOG.md +332 -0
  2. package/README.md +2 -1
  3. package/dist/types/accounts/chatgpt.d.ts +1 -1
  4. package/dist/types/accounts/chatgpt.d.ts.map +1 -1
  5. package/dist/types/accounts/http.d.ts +2 -4
  6. package/dist/types/accounts/http.d.ts.map +1 -1
  7. package/dist/types/client/client.d.ts +37 -11
  8. package/dist/types/client/client.d.ts.map +1 -1
  9. package/dist/types/client/connection.d.ts +66 -13
  10. package/dist/types/client/connection.d.ts.map +1 -1
  11. package/dist/types/client/index.d.ts +1 -1
  12. package/dist/types/client/index.d.ts.map +1 -1
  13. package/dist/types/client/logins.d.ts +4 -3
  14. package/dist/types/client/logins.d.ts.map +1 -1
  15. package/dist/types/client/streams.d.ts +9 -3
  16. package/dist/types/client/streams.d.ts.map +1 -1
  17. package/dist/types/client/turns.d.ts +1 -1
  18. package/dist/types/client/turns.d.ts.map +1 -1
  19. package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
  20. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  21. package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
  22. package/dist/types/core/agents/acp/client.d.ts +12 -3
  23. package/dist/types/core/agents/acp/client.d.ts.map +1 -1
  24. package/dist/types/core/agents/acp/mappings.d.ts +8 -2
  25. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  26. package/dist/types/core/agents/base.d.ts +22 -8
  27. package/dist/types/core/agents/base.d.ts.map +1 -1
  28. package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
  29. package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
  30. package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
  31. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  32. package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
  33. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  34. package/dist/types/core/agents/claude-code/events.d.ts +9 -1
  35. package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
  36. package/dist/types/core/agents/claude-code/login.d.ts +4 -1
  37. package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
  38. package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
  39. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  40. package/dist/types/core/agents/claude-code/models.d.ts +5 -1
  41. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  42. package/dist/types/core/agents/claude-code/options.d.ts +10 -4
  43. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  44. package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
  45. package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
  46. package/dist/types/core/agents/claude-code/results.d.ts +16 -0
  47. package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
  48. package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
  49. package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
  50. package/dist/types/core/agents/claude-code/session.d.ts +50 -12
  51. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  52. package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
  53. package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
  54. package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
  55. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  56. package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
  57. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  58. package/dist/types/core/agents/codex-app-server/client.d.ts +21 -2
  59. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  60. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +61 -14
  61. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  62. package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
  63. package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
  64. package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
  65. package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
  66. package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
  67. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  68. package/dist/types/core/agents/codex-app-server/homes.d.ts +26 -5
  69. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  70. package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
  71. package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
  72. package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
  73. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
  74. package/dist/types/core/agents/codex-app-server/mcp.d.ts +45 -20
  75. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  76. package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
  77. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  78. package/dist/types/core/agents/codex-app-server/options.d.ts +32 -5
  79. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  80. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
  81. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
  82. package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
  83. package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
  84. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  85. package/dist/types/core/agents/codex-app-server/session.d.ts +70 -33
  86. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  87. package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
  88. package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
  89. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
  90. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  91. package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
  92. package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
  93. package/dist/types/core/agents/codex-app-server/turn.d.ts +37 -8
  94. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
  95. package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
  96. package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
  97. package/dist/types/core/agents/credentials.d.ts +3 -1
  98. package/dist/types/core/agents/credentials.d.ts.map +1 -1
  99. package/dist/types/core/agents/diffs.d.ts +9 -0
  100. package/dist/types/core/agents/diffs.d.ts.map +1 -0
  101. package/dist/types/core/agents/elicitation.d.ts +3 -2
  102. package/dist/types/core/agents/elicitation.d.ts.map +1 -1
  103. package/dist/types/core/agents/login.d.ts +6 -0
  104. package/dist/types/core/agents/login.d.ts.map +1 -1
  105. package/dist/types/core/agents/mcp-config.d.ts +0 -4
  106. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  107. package/dist/types/core/agents/plan.d.ts +2 -13
  108. package/dist/types/core/agents/plan.d.ts.map +1 -1
  109. package/dist/types/core/agents/process-pool.d.ts +193 -0
  110. package/dist/types/core/agents/process-pool.d.ts.map +1 -0
  111. package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
  112. package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
  113. package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
  114. package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
  115. package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
  116. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
  117. package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
  118. package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
  119. package/dist/types/core/agents/scripted/steps.d.ts +132 -0
  120. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
  121. package/dist/types/core/agents/session-agent.d.ts +49 -6
  122. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  123. package/dist/types/core/diagnostics.d.ts +4 -1
  124. package/dist/types/core/diagnostics.d.ts.map +1 -1
  125. package/dist/types/core/index.d.ts +6 -1
  126. package/dist/types/core/index.d.ts.map +1 -1
  127. package/dist/types/core/presets.d.ts +32 -6
  128. package/dist/types/core/presets.d.ts.map +1 -1
  129. package/dist/types/core/processes/lifeline.d.ts +0 -20
  130. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  131. package/dist/types/core/provision/index.d.ts +2 -2
  132. package/dist/types/core/provision/index.d.ts.map +1 -1
  133. package/dist/types/core/provision/install-build.d.ts +2 -1
  134. package/dist/types/core/provision/install-build.d.ts.map +1 -1
  135. package/dist/types/core/provision/npm.d.ts +20 -8
  136. package/dist/types/core/provision/npm.d.ts.map +1 -1
  137. package/dist/types/core/provision/provisioner.d.ts +4 -1
  138. package/dist/types/core/provision/provisioner.d.ts.map +1 -1
  139. package/dist/types/core/provision/tools.d.ts +2 -1
  140. package/dist/types/core/provision/tools.d.ts.map +1 -1
  141. package/dist/types/core/runtime/agent-runtime.d.ts +18 -9
  142. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  143. package/dist/types/core/runtime/event-processor.d.ts +5 -1
  144. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  145. package/dist/types/core/runtime/harness-clis.d.ts +9 -0
  146. package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
  147. package/dist/types/core/runtime/harness-logins.d.ts +33 -5
  148. package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
  149. package/dist/types/core/runtime/model-lists.d.ts +2 -0
  150. package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
  151. package/dist/types/core/runtime/permission-policy.d.ts +5 -3
  152. package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
  153. package/dist/types/core/runtime/request-broker.d.ts +2 -1
  154. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  155. package/dist/types/core/runtime/turn.d.ts +14 -1
  156. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  157. package/dist/types/core/utils/deadline.d.ts +6 -0
  158. package/dist/types/core/utils/deadline.d.ts.map +1 -1
  159. package/dist/types/core/utils/errors.d.ts +15 -1
  160. package/dist/types/core/utils/errors.d.ts.map +1 -1
  161. package/dist/types/operator/index.d.ts +11 -0
  162. package/dist/types/operator/index.d.ts.map +1 -0
  163. package/dist/types/operator/mcp.d.ts +33 -0
  164. package/dist/types/operator/mcp.d.ts.map +1 -0
  165. package/dist/types/operator/operator.d.ts +206 -0
  166. package/dist/types/operator/operator.d.ts.map +1 -0
  167. package/dist/types/operator/render.d.ts +89 -0
  168. package/dist/types/operator/render.d.ts.map +1 -0
  169. package/dist/types/operator/view.d.ts +44 -0
  170. package/dist/types/operator/view.d.ts.map +1 -0
  171. package/dist/types/protocol/auth.d.ts +54 -24
  172. package/dist/types/protocol/auth.d.ts.map +1 -1
  173. package/dist/types/protocol/clis.d.ts +14 -7
  174. package/dist/types/protocol/clis.d.ts.map +1 -1
  175. package/dist/types/protocol/config.d.ts +2 -0
  176. package/dist/types/protocol/config.d.ts.map +1 -1
  177. package/dist/types/protocol/conversation.d.ts +40 -3
  178. package/dist/types/protocol/conversation.d.ts.map +1 -1
  179. package/dist/types/protocol/credentials.d.ts +7 -0
  180. package/dist/types/protocol/credentials.d.ts.map +1 -1
  181. package/dist/types/protocol/execution.d.ts +2 -1
  182. package/dist/types/protocol/execution.d.ts.map +1 -1
  183. package/dist/types/protocol/factories.d.ts +12 -2
  184. package/dist/types/protocol/factories.d.ts.map +1 -1
  185. package/dist/types/protocol/harness.d.ts +6 -2
  186. package/dist/types/protocol/harness.d.ts.map +1 -1
  187. package/dist/types/protocol/history.d.ts +19 -0
  188. package/dist/types/protocol/history.d.ts.map +1 -1
  189. package/dist/types/protocol/lifecycle.d.ts +47 -8
  190. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  191. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  192. package/dist/types/protocol/models.d.ts +3 -0
  193. package/dist/types/protocol/models.d.ts.map +1 -1
  194. package/dist/types/protocol/parts.d.ts +14 -0
  195. package/dist/types/protocol/parts.d.ts.map +1 -1
  196. package/dist/types/protocol/plan.d.ts +20 -0
  197. package/dist/types/protocol/plan.d.ts.map +1 -0
  198. package/dist/types/protocol/tool-state.d.ts +12 -0
  199. package/dist/types/protocol/tool-state.d.ts.map +1 -1
  200. package/dist/types/protocol/wire-api.d.ts +129 -23
  201. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  202. package/dist/types/protocol/wire.d.ts +70 -10
  203. package/dist/types/protocol/wire.d.ts.map +1 -1
  204. package/dist/types/server/acp/binding.d.ts +6 -1
  205. package/dist/types/server/acp/binding.d.ts.map +1 -1
  206. package/dist/types/server/acp/translate.d.ts +33 -7
  207. package/dist/types/server/acp/translate.d.ts.map +1 -1
  208. package/dist/types/server/agent-server.d.ts +6 -0
  209. package/dist/types/server/agent-server.d.ts.map +1 -1
  210. package/dist/types/server/feedback.d.ts +2 -1
  211. package/dist/types/server/feedback.d.ts.map +1 -1
  212. package/dist/types/server/install.d.ts +11 -0
  213. package/dist/types/server/install.d.ts.map +1 -1
  214. package/dist/types/server/session-gate.d.ts +2 -1
  215. package/dist/types/server/session-gate.d.ts.map +1 -1
  216. package/dist/types/server/session-store.d.ts +1 -0
  217. package/dist/types/server/session-store.d.ts.map +1 -1
  218. package/dist/types/server/subscriptions.d.ts +2 -1
  219. package/dist/types/server/subscriptions.d.ts.map +1 -1
  220. package/dist/types/server/transports.d.ts.map +1 -1
  221. package/dist/types/sync/fit.d.ts +8 -7
  222. package/dist/types/sync/fit.d.ts.map +1 -1
  223. package/dist/types/sync/history-sqlite.d.ts.map +1 -1
  224. package/dist/types/sync/index.d.ts +1 -0
  225. package/dist/types/sync/index.d.ts.map +1 -1
  226. package/dist/types/sync/journal-sqlite.d.ts +2 -1
  227. package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
  228. package/dist/types/sync/journal.d.ts +11 -3
  229. package/dist/types/sync/journal.d.ts.map +1 -1
  230. package/dist/types/sync/projection.d.ts +6 -0
  231. package/dist/types/sync/projection.d.ts.map +1 -1
  232. package/dist/types/sync/recovery.d.ts +2 -1
  233. package/dist/types/sync/recovery.d.ts.map +1 -1
  234. package/dist/types/sync/schema.d.ts +10 -0
  235. package/dist/types/sync/schema.d.ts.map +1 -1
  236. package/dist/types/sync/sql.d.ts +2 -1
  237. package/dist/types/sync/sql.d.ts.map +1 -1
  238. package/dist/types/sync/worker.d.ts.map +1 -1
  239. package/docs/consuming.md +464 -40
  240. package/docs/deploy.md +36 -8
  241. package/docs/durable-sessions.md +35 -5
  242. package/docs/harnesses.md +396 -30
  243. package/package.json +11 -1
  244. package/src/accounts/chatgpt.ts +2 -2
  245. package/src/accounts/http.ts +2 -16
  246. package/src/client/client.ts +92 -31
  247. package/src/client/connection.ts +113 -22
  248. package/src/client/index.ts +1 -0
  249. package/src/client/logins.ts +24 -15
  250. package/src/client/streams.ts +12 -4
  251. package/src/client/turns.ts +1 -1
  252. package/src/core/agents/acp/acp-agent.ts +181 -52
  253. package/src/core/agents/acp/adapter.ts +9 -1
  254. package/src/core/agents/acp/client.ts +16 -4
  255. package/src/core/agents/acp/mappings.ts +27 -0
  256. package/src/core/agents/base.ts +25 -10
  257. package/src/core/agents/claude-code/adapter.ts +47 -17
  258. package/src/core/agents/claude-code/approvals.ts +6 -5
  259. package/src/core/agents/claude-code/claude-agent.ts +60 -30
  260. package/src/core/agents/claude-code/events.ts +11 -1
  261. package/src/core/agents/claude-code/login.ts +14 -2
  262. package/src/core/agents/claude-code/mcp.ts +31 -15
  263. package/src/core/agents/claude-code/models.ts +7 -2
  264. package/src/core/agents/claude-code/options.ts +11 -3
  265. package/src/core/agents/claude-code/outcome.ts +10 -2
  266. package/src/core/agents/claude-code/results.ts +73 -0
  267. package/src/core/agents/claude-code/sdk-pair.ts +66 -0
  268. package/src/core/agents/claude-code/session.ts +166 -37
  269. package/src/core/agents/claude-code/spend.ts +1 -1
  270. package/src/core/agents/claude-code/tools.ts +108 -12
  271. package/src/core/agents/codex-app-server/adapter.ts +37 -12
  272. package/src/core/agents/codex-app-server/client.ts +69 -10
  273. package/src/core/agents/codex-app-server/codex-agent.ts +141 -55
  274. package/src/core/agents/codex-app-server/commands.ts +2 -2
  275. package/src/core/agents/codex-app-server/credentials.ts +10 -5
  276. package/src/core/agents/codex-app-server/effort.ts +1 -1
  277. package/src/core/agents/codex-app-server/homes.ts +59 -10
  278. package/src/core/agents/codex-app-server/items.ts +219 -31
  279. package/src/core/agents/codex-app-server/login.ts +14 -2
  280. package/src/core/agents/codex-app-server/mcp.ts +142 -26
  281. package/src/core/agents/codex-app-server/models.ts +9 -8
  282. package/src/core/agents/codex-app-server/options.ts +79 -11
  283. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  284. package/src/core/agents/codex-app-server/protocol.ts +14 -4
  285. package/src/core/agents/codex-app-server/requests.ts +14 -6
  286. package/src/core/agents/codex-app-server/session.ts +156 -81
  287. package/src/core/agents/codex-app-server/subagents.ts +21 -14
  288. package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
  289. package/src/core/agents/codex-app-server/turn-items.ts +1 -1
  290. package/src/core/agents/codex-app-server/turn.ts +96 -35
  291. package/src/core/agents/codex-app-server/user-login.ts +107 -0
  292. package/src/core/agents/credentials.ts +5 -2
  293. package/src/core/agents/diffs.ts +25 -0
  294. package/src/core/agents/elicitation.ts +2 -2
  295. package/src/core/agents/login.ts +6 -0
  296. package/src/core/agents/mcp-config.ts +2 -2
  297. package/src/core/agents/plan.ts +3 -19
  298. package/src/core/agents/process-pool.ts +432 -0
  299. package/src/core/agents/scripted/adapter.ts +142 -0
  300. package/src/core/agents/scripted/mcp-call.ts +134 -0
  301. package/src/core/agents/scripted/scripted-agent.ts +418 -0
  302. package/src/core/agents/scripted/scripts.ts +171 -0
  303. package/src/core/agents/scripted/steps.ts +127 -0
  304. package/src/core/agents/session-agent.ts +105 -8
  305. package/src/core/diagnostics.ts +9 -1
  306. package/src/core/index.ts +12 -0
  307. package/src/core/presets.ts +99 -14
  308. package/src/core/processes/lifeline.ts +99 -25
  309. package/src/core/provision/index.ts +1 -2
  310. package/src/core/provision/install-build.ts +4 -1
  311. package/src/core/provision/npm.ts +309 -97
  312. package/src/core/provision/provisioner.ts +22 -8
  313. package/src/core/provision/tools.ts +44 -19
  314. package/src/core/runtime/agent-runtime.ts +70 -34
  315. package/src/core/runtime/event-processor.ts +18 -2
  316. package/src/core/runtime/harness-clis.ts +36 -1
  317. package/src/core/runtime/harness-logins.ts +82 -31
  318. package/src/core/runtime/model-lists.ts +1 -1
  319. package/src/core/runtime/permission-policy.ts +4 -3
  320. package/src/core/runtime/request-broker.ts +1 -1
  321. package/src/core/runtime/turn.ts +53 -3
  322. package/src/core/utils/deadline.ts +23 -0
  323. package/src/core/utils/errors.ts +24 -4
  324. package/src/operator/index.ts +40 -0
  325. package/src/operator/mcp.ts +171 -0
  326. package/src/operator/operator.ts +782 -0
  327. package/src/operator/render.ts +473 -0
  328. package/src/operator/view.ts +82 -0
  329. package/src/protocol/auth.ts +30 -23
  330. package/src/protocol/clis.ts +12 -4
  331. package/src/protocol/credentials.ts +19 -0
  332. package/src/protocol/execution.ts +5 -0
  333. package/src/protocol/factories.ts +25 -3
  334. package/src/protocol/harness.ts +11 -1
  335. package/src/protocol/history.ts +20 -0
  336. package/src/protocol/lifecycle.ts +10 -1
  337. package/src/protocol/model-catalog.ts +19 -8
  338. package/src/protocol/parts.ts +12 -1
  339. package/src/protocol/plan.ts +35 -0
  340. package/src/protocol/tool-state.ts +14 -1
  341. package/src/protocol/wire.ts +14 -0
  342. package/src/server/acp/binding.ts +113 -22
  343. package/src/server/acp/translate.ts +161 -8
  344. package/src/server/agent-server.ts +10 -1
  345. package/src/server/bin.ts +83 -9
  346. package/src/server/feedback.ts +1 -1
  347. package/src/server/install.ts +82 -4
  348. package/src/server/session-gate.ts +1 -1
  349. package/src/server/session-store.ts +5 -1
  350. package/src/server/subscriptions.ts +1 -1
  351. package/src/server/transports.ts +14 -0
  352. package/src/sync/fit.ts +115 -28
  353. package/src/sync/history-sqlite.ts +6 -2
  354. package/src/sync/index.ts +1 -0
  355. package/src/sync/journal-sqlite.ts +20 -2
  356. package/src/sync/journal.ts +10 -2
  357. package/src/sync/projection.ts +10 -0
  358. package/src/sync/recovery.ts +11 -4
  359. package/src/sync/schema.ts +7 -5
  360. package/src/sync/sql.ts +1 -1
  361. package/src/sync/worker.ts +4 -0
  362. package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
  363. package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
  364. package/dist/types/core/agents/session-pool.d.ts +0 -41
  365. package/dist/types/core/agents/session-pool.d.ts.map +0 -1
  366. package/src/core/agents/claude-code/session-pool.ts +0 -136
  367. package/src/core/agents/session-pool.ts +0 -111
@@ -0,0 +1,473 @@
1
+ /**
2
+ * A session, in words a model can act on: short, with the next step named as
3
+ * the call that takes it. Pure functions over a `SessionView`.
4
+ *
5
+ * `ref` is how the hints name the session: its name, or `group/name` where the
6
+ * name alone is ambiguous.
7
+ */
8
+
9
+ import type { ElicitationOutcome, PermissionOutcome } from "../protocol/lifecycle.ts";
10
+ import type { Part, UnknownPart } from "../protocol/parts.ts";
11
+ import { PLAN_OPTION_IDS } from "../protocol/plan.ts";
12
+ import {
13
+ type DecisionRequest,
14
+ type SessionView,
15
+ type ViewMessage,
16
+ type Waiting,
17
+ isPlan,
18
+ phaseOf,
19
+ } from "./view.ts";
20
+
21
+ const clip = (text: string, limit: number) =>
22
+ text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
23
+
24
+ // @ref LLP 0014#untrusted-text — text an agent wrote or a repository holds never starts a line
25
+ // Untrusted text (what an agent or a person wrote, a question, an option, an
26
+ // error, a permission's title) is either a block of `QUOTE` lines or one
27
+ // JSON-encoded line after a label. The tool's own hints are ` -> tool(...)`,
28
+ // which no quoted line can produce: each of its lines begins with the marker.
29
+ /** The marker every line of untrusted text begins with. */
30
+ export const QUOTE = " | ";
31
+
32
+ /** Line breaks a reader may honour, the C0/C1 controls and bidirectional overrides. */
33
+ const BREAKS = /\r\n|[\n\r\v\f\u0085\u2028\u2029]/g;
34
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: the controls are what it drops
35
+ const CONTROLS = /[\u0000-\u0008\u000e-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/g;
36
+
37
+ /** What a JSON string still carries raw that may break a line or reorder it: escaped in a hint. */
38
+ const UNSAFE_IN_JSON = new RegExp(`${BREAKS.source}|${CONTROLS.source}`, "g");
39
+
40
+ /** The most messages `read_session` reads. */
41
+ export const MAX_LAST = 100;
42
+
43
+ /** Untrusted text as one line: no line breaks or controls, runs of space collapsed. */
44
+ export function oneLine(text: string): string {
45
+ return text.replace(BREAKS, " ").replace(CONTROLS, "").replace(/\s+/g, " ").trim();
46
+ }
47
+
48
+ /** Untrusted text as a block: each of its lines carried after `QUOTE`. */
49
+ export function quote(text: string): string {
50
+ return text
51
+ .replace(CONTROLS, "")
52
+ .split(BREAKS)
53
+ .map((line) => `${QUOTE}${line}`.trimEnd())
54
+ .join("\n");
55
+ }
56
+
57
+ /** Untrusted text named in prose (a session, a project, an option): one JSON-quoted line. */
58
+ export const quoted = (text: string) => JSON.stringify(oneLine(text));
59
+
60
+ /** A label, then the untrusted text as a quoted block under it. */
61
+ const block = (label: string, text: string) => `${label}\n${quote(text)}`;
62
+
63
+ const textOf = (parts: readonly (Part | UnknownPart)[]) =>
64
+ parts
65
+ .flatMap((part) => (part.type === "text" && "text" in part ? [String(part.text)] : []))
66
+ .join("")
67
+ .trim();
68
+
69
+ /**
70
+ * The next step, as the tool call that takes it:
71
+ * `-> answer_request(session: "voyager", answer: "<your answer>")`.
72
+ */
73
+ export function callHint(
74
+ tool: string,
75
+ args: Record<string, string | number | boolean> = {},
76
+ ): string {
77
+ // `\uXXXX` for what JSON leaves raw (U+2028, C1, bidi controls): the value reads back the same.
78
+ const json = (value: string | number | boolean) =>
79
+ JSON.stringify(value).replace(UNSAFE_IN_JSON, (found) =>
80
+ [...found].map((c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`).join(""),
81
+ );
82
+ const named = Object.entries(args).map(([key, value]) => `${key}: ${json(value)}`);
83
+ return `-> ${tool}(${named.join(", ")})`;
84
+ }
85
+
86
+ /** A choice's labels on one line: `options: "USD", "EUR"`. */
87
+ const optionsLine = (labels: readonly string[]) => `options: ${labels.map(quoted).join(", ")}`;
88
+
89
+ /** What ends a path in a command: a space, a quote, or the shell's punctuation. */
90
+ const PATH_END = "\\s\"'`;&|()<>";
91
+ /** Not inside a longer path: what comes before the folder is no part of a name. */
92
+ const PATH_START = "(?<![\\w.~\\\\/-])";
93
+ /** What a shell escapes with a backslash in a path (`Live\ Mac`). */
94
+ const SHELL_ESCAPED = /[\s'"\\$`!&*()[\]{};|<>?]/g;
95
+ const REGEXP_SPECIAL = /[.*+?^${}()|[\]\\]/g;
96
+
97
+ /**
98
+ * `text` with the folder `root` read relative wherever it appears: a path
99
+ * under it as the rest of the path, the folder itself as `.`. Its form with
100
+ * spaces escaped, as a shell command writes it, too. A folder whose name
101
+ * merely starts with it (`root-old`) is left alone.
102
+ */
103
+ function relativeTo(root: string, text: string): string {
104
+ const folder = root.replace(/\/+$/, "");
105
+ // "" or "/": there is nothing to read relative to.
106
+ if (folder.length < 2 || !text) return text;
107
+ const forms = [...new Set([folder, folder.replace(SHELL_ESCAPED, "\\$&")])].map((form) =>
108
+ form.replace(REGEXP_SPECIAL, "\\$&"),
109
+ );
110
+ const pattern = new RegExp(
111
+ `${PATH_START}(?:${forms.join("|")})(?:(/)(?=[^${PATH_END}])|/?(?=[${PATH_END}]|$))`,
112
+ "g",
113
+ );
114
+ return text.replace(pattern, (_match, slash?: string) => (slash ? "" : "."));
115
+ }
116
+
117
+ /** One question a tool call put to the person, as its input carries it. */
118
+ interface AskedQuestion {
119
+ question: string;
120
+ options: string[];
121
+ }
122
+
123
+ /**
124
+ * What a tool call put to the person, keyed by what its input carries, not
125
+ * by a tool's name: questions (`questions`, each with a `question`, its
126
+ * options labels or `{ label }`s), or a plan (a `switch_mode` call's `plan`).
127
+ */
128
+ function askedIn(
129
+ part: Part | UnknownPart,
130
+ ): { questions: AskedQuestion[] } | { plan: string } | undefined {
131
+ if (part.type !== "tool" || !("toolName" in part)) return undefined;
132
+ const input = (part.state as { input?: unknown }).input;
133
+ if (!input || typeof input !== "object") return undefined;
134
+ const { questions, plan } = input as { questions?: unknown; plan?: unknown };
135
+ if (Array.isArray(questions)) {
136
+ const asked = questions.flatMap((entry): AskedQuestion[] => {
137
+ const { question, options } = (entry ?? {}) as { question?: unknown; options?: unknown };
138
+ if (typeof question !== "string" || !question.trim()) return [];
139
+ const labels = (Array.isArray(options) ? options : []).flatMap((option) => {
140
+ const label = typeof option === "string" ? option : (option as { label?: unknown })?.label;
141
+ return typeof label === "string" ? [label] : [];
142
+ });
143
+ return [{ question, options: labels }];
144
+ });
145
+ if (asked.length) return { questions: asked };
146
+ }
147
+ if (part.kind === "switch_mode" && typeof plan === "string" && plan.trim()) return { plan };
148
+ return undefined;
149
+ }
150
+
151
+ /**
152
+ * What a tool call put to the person, for the lines under its own: each
153
+ * question with its options and, once the call has settled, what the agent
154
+ * was told (the answers, or a dismissal), or the plan. All of it is
155
+ * untrusted text.
156
+ */
157
+ function askedLines(part: Part | UnknownPart, root = ""): string[] {
158
+ const asked = askedIn(part);
159
+ if (!asked) return [];
160
+ const state = (part as { state: { status: string; output?: unknown; error?: unknown } }).state;
161
+ const told = String(state.output ?? state.error ?? "");
162
+ const toldLines = told.trim()
163
+ ? [block("agent was told:", clip(relativeTo(root, told), 600))]
164
+ : [];
165
+ if ("plan" in asked) {
166
+ // Sent back, the agent was told what to change. An approval's words are the
167
+ // harness's, and Claude's hold the plan file's path: the call's line says it.
168
+ return [block("plan:", clip(asked.plan, 800)), ...(state.status === "failed" ? toldLines : [])];
169
+ }
170
+ const settled = state.status === "completed" || state.status === "failed";
171
+ const lines = asked.questions.flatMap((question) => [
172
+ block("question:", clip(question.question, 800)),
173
+ ...(question.options.length ? [optionsLine(question.options)] : []),
174
+ ]);
175
+ return settled ? [...lines, ...toldLines] : lines;
176
+ }
177
+
178
+ /**
179
+ * One tool call on one line: its name, what it acted on, and whether it failed.
180
+ * `working: false` (its session runs no turn) shows a tool left open as
181
+ * interrupted, not running: its turn ended without it. Paths under `root` read
182
+ * relative to it, wherever they appear. A call that put a question to the
183
+ * person says whether it was answered, one that put a plan whether it was
184
+ * approved (`askedIn`).
185
+ */
186
+ export function toolLine(part: Part | UnknownPart, root = "", working = true): string | null {
187
+ if (part.type !== "tool" || !("toolName" in part)) return null;
188
+ const state = part.state as { status: string; input?: Record<string, unknown>; output?: unknown };
189
+ const input = state.input ?? {};
190
+ // @ref LLP 0002#a-tool-call-says-what-it-acts-on — the command the model wrote, not a harness's shell around it
191
+ const { command, url } = part as { command?: string; url?: string };
192
+ const raw = String(
193
+ command ??
194
+ url ??
195
+ input.file_path ??
196
+ input.path ??
197
+ input.command ??
198
+ input.pattern ??
199
+ input.url ??
200
+ input.description ??
201
+ "",
202
+ );
203
+ const target = oneLine(relativeTo(root, raw));
204
+ const mark = markOf(part, state, working);
205
+ return clip(`${oneLine(String(part.toolName))}${target ? ` ${target}` : ""}${mark}`, 140);
206
+ }
207
+
208
+ /** How a call stands, in the words for what it is: a tool failed, a question answered, a plan approved. */
209
+ function markOf(
210
+ part: Part | UnknownPart,
211
+ state: { status: string; output?: unknown },
212
+ working: boolean,
213
+ ): string {
214
+ const open = state.status === "in_progress" || state.status === "pending";
215
+ const asked = askedIn(part);
216
+ if (!asked) {
217
+ if (state.status === "failed") return " (failed)";
218
+ return open ? (working ? " (running)" : " (interrupted)") : "";
219
+ }
220
+ const plan = "plan" in asked;
221
+ if (open) {
222
+ if (!working) return " (interrupted)";
223
+ return plan ? " (waiting for approval)" : " (waiting for an answer)";
224
+ }
225
+ const ended = state.status === "failed" || state.status === "cancelled";
226
+ if (!plan) return ended ? " (not answered)" : "";
227
+ if (ended) return " (not approved)";
228
+ // A plan presented with nobody to ask completes with no words: no verdict.
229
+ return state.output ? " (approved)" : "";
230
+ }
231
+
232
+ /**
233
+ * Why only the person can answer what the agent waits on, where the session
234
+ * runs: a form to fill in, a page to visit, a secret to type, or a kind of
235
+ * request these tools do not know. Undefined for one a model may answer.
236
+ */
237
+ export function personOnly(waiting: Waiting): string | undefined {
238
+ if (waiting.kind !== "question") return undefined;
239
+ const { mode, questions = [] } = waiting.request;
240
+ if (mode === "form") return "a form to fill in";
241
+ if (mode === "url") return "a page to visit";
242
+ if (mode !== "question") return "a request these tools cannot answer";
243
+ return questions.some((question) => question.secret) ? "a secret to type" : undefined;
244
+ }
245
+
246
+ /**
247
+ * What a permission is about, a line each, quoted: its command, else its
248
+ * address, else its files (under `root` relative to it), then why the agent
249
+ * asks, when it says. Empty when the request carries none of them.
250
+ */
251
+ function aboutLines(toolCall: DecisionRequest["toolCall"], root: string): string {
252
+ if (!toolCall) return "";
253
+ const files = (toolCall.locations ?? []).map((location) => relativeTo(root, location.path));
254
+ const more = files.length > 10 ? ` (and ${files.length - 10} more)` : "";
255
+ const what = toolCall.command?.trim()
256
+ ? `command: ${quoted(clip(relativeTo(root, toolCall.command), 500))}`
257
+ : toolCall.url?.trim()
258
+ ? `url: ${quoted(clip(toolCall.url, 500))}`
259
+ : files.length
260
+ ? `files: ${files.slice(0, 10).map(quoted).join(", ")}${more}`
261
+ : "";
262
+ const why = toolCall.reason?.trim() ? `reason: ${quoted(clip(toolCall.reason, 300))}` : "";
263
+ return [what, why]
264
+ .filter(Boolean)
265
+ .map((line) => `\n${line}`)
266
+ .join("");
267
+ }
268
+
269
+ /**
270
+ * What the agent waits on, and the exact call that answers it. What only the
271
+ * person can answer (`personOnly`) is one line, with no call. Paths under
272
+ * `root` read relative to it.
273
+ */
274
+ export function describeWaiting(waiting: Waiting, ref: string, root = ""): string {
275
+ const only = waiting.kind === "question" && personOnly(waiting);
276
+ if (only) {
277
+ return `only the person can answer this, where the session runs (${only}): ${quoted(clip(waiting.request.message, 300))}`;
278
+ }
279
+ const answer = (text: string) => callHint("answer_request", { session: ref, answer: text });
280
+ const options = (labels: readonly string[], ownWords = false) =>
281
+ labels.length ? `\n${optionsLine(labels)}${ownWords ? " (or your own words)" : ""}` : "";
282
+ if (waiting.kind === "question") {
283
+ const questions = waiting.request.questions ?? [];
284
+ const asked = questions.length
285
+ ? questions
286
+ .map(
287
+ (q) =>
288
+ `${block("question:", q.question)}${options(
289
+ (q.options ?? []).map((o) => o.label),
290
+ true,
291
+ )}`,
292
+ )
293
+ .join("\n")
294
+ : block("question:", waiting.request.message);
295
+ const form =
296
+ questions.length > 1 ? "<one answer per question, separated by ; >" : "<your answer>";
297
+ return `${asked}\n ${answer(form)}`;
298
+ }
299
+ if (isPlan(waiting.request)) {
300
+ const approve = ` ${answer("approve")} (or answer: "<what to change>")`;
301
+ // Never approved unseen: the plan itself, or the call that shows it.
302
+ const plan = waiting.request.toolCall?.rawInput?.plan;
303
+ return typeof plan === "string" && plan.trim()
304
+ ? `plan waiting for approval:\n${quote(clip(plan, 800))}\n${approve}`
305
+ : `plan waiting for approval; read it before answering\n ${callHint("read_session", { session: ref })}\n${approve}`;
306
+ }
307
+ const named = waiting.request.options.map((option) => option.name);
308
+ const about = aboutLines(waiting.request.toolCall, root);
309
+ return `${block("permission:", waiting.request.title)}${about}${options(named)}\n ${answer("allow")} (or answer: "deny")`;
310
+ }
311
+
312
+ /** The agent's words since the person last spoke. */
313
+ export function lastReply(messages: readonly ViewMessage[]): string {
314
+ // A loop, not `findLastIndex`: consumers compile this source with their own `lib`.
315
+ let lastUser = messages.length - 1;
316
+ while (lastUser >= 0 && messages[lastUser]?.role !== "user") lastUser--;
317
+ return messages
318
+ .slice(lastUser + 1)
319
+ .filter((message) => message.role === "assistant" && !message.nested)
320
+ .map((message) => textOf(message.parts))
321
+ .filter(Boolean)
322
+ .join("\n");
323
+ }
324
+
325
+ /**
326
+ * How a turn stands: finished, waiting on the person, failed, or still going.
327
+ * `waitedSeconds` is how long this call waited; `note` is the host's line of
328
+ * facts for a turn that is not working any more; paths under `root` in what it
329
+ * waits on read relative to it.
330
+ */
331
+ export function renderOutcome(
332
+ view: SessionView,
333
+ ref: string,
334
+ waitedSeconds?: number,
335
+ note?: string,
336
+ root?: string,
337
+ ): string {
338
+ const phase = phaseOf(view);
339
+ if (phase === "working") {
340
+ // No messages at all: it is being set up, or its first turn has not shown yet.
341
+ const detail = [
342
+ view.messages.length ? "" : "starting up, no messages yet",
343
+ waitedSeconds ? `still running after ${waitedSeconds}s` : "",
344
+ ].filter(Boolean);
345
+ return `status: working${detail.length ? ` (${detail.join("; ")})` : ""}\n ${callHint("wait_for_session", { session: ref })}`;
346
+ }
347
+ const fresh = phase === "idle" && !view.messages.length;
348
+ const lines = [`status: ${fresh ? "idle (no messages yet)" : phase === "idle" ? "done" : phase}`];
349
+ const reply = lastReply(view.messages);
350
+ if (reply) lines.push(block("agent said:", clip(reply, 1500)));
351
+ for (const waiting of view.waiting) lines.push(describeWaiting(waiting, ref, root));
352
+ if (phase === "error") lines.push(block("error:", view.error ?? "the turn failed"));
353
+ if (note) lines.push(oneLine(note));
354
+ if (phase === "error" || reply.length > 1500) {
355
+ lines.push(` ${callHint("read_session", { session: ref })}`);
356
+ }
357
+ return lines.join("\n");
358
+ }
359
+
360
+ export interface RenderOptions {
361
+ /** How many recent messages (default 10). */
362
+ last?: number;
363
+ /** Paths in tool lines are shown relative to this folder. */
364
+ root?: string;
365
+ }
366
+
367
+ /**
368
+ * The conversation, condensed: what was asked, what the agent said, which
369
+ * tools it ran, and under a call that asked the person its questions and
370
+ * what the agent was told (or its plan).
371
+ */
372
+ export function renderConversation(
373
+ view: SessionView,
374
+ ref: string,
375
+ options: RenderOptions = {},
376
+ ): string {
377
+ const lines: string[] = [];
378
+ // The host was asked for `last`; a host that gives more is cut here.
379
+ const shown = view.messages.slice(-Math.max(1, options.last ?? 10));
380
+ // A tool awaiting approval is open in a working turn; one an ended turn left open was interrupted.
381
+ const working = view.status === "working";
382
+ for (const message of shown) {
383
+ const text = textOf(message.parts);
384
+ if (message.role === "user") {
385
+ lines.push(block("[you]", clip(text, 800)));
386
+ continue;
387
+ }
388
+ for (const part of message.parts) {
389
+ const line = toolLine(part, options.root, working);
390
+ if (!line) continue;
391
+ lines.push(` · ${line}`);
392
+ // Under the call's line, aligned with its name.
393
+ for (const asked of askedLines(part, options.root)) {
394
+ lines.push(asked.replace(/^/gm, " "));
395
+ }
396
+ }
397
+ if (text) lines.push(block("[agent]", clip(text, 1200)));
398
+ }
399
+ if (!shown.length) lines.push("(no messages yet)");
400
+ const cut = view.older || shown.length < view.messages.length;
401
+ lines.push(`(status: ${phaseOf(view)})`);
402
+ if (view.error && phaseOf(view) === "error") lines.push(block("error:", view.error));
403
+ for (const waiting of view.waiting) lines.push(describeWaiting(waiting, ref, options.root));
404
+ if (cut) {
405
+ lines.push(
406
+ shown.length >= MAX_LAST
407
+ ? `older messages exist; read_session shows at most the last ${MAX_LAST}.`
408
+ : `older messages exist: ${callHint("read_session", { session: ref, last: Math.min(shown.length + 10, MAX_LAST) })}`,
409
+ );
410
+ }
411
+ return lines.join("\n");
412
+ }
413
+
414
+ /** A person's words as the engine takes them, for the request the agent waits on. */
415
+ export type Answer =
416
+ | { kind: "question"; requestId: string; outcome: ElicitationOutcome }
417
+ | { kind: "decision"; requestId: string; outcome: PermissionOutcome };
418
+
419
+ /** A yes is the whole answer: "yes, but drop step 3" is not one. */
420
+ const YES =
421
+ /^(approve|approved|yes|yes please|yep|ok|okay|allow|accept|go|go ahead|do it|proceed|sure|lgtm)[\s.!]*$/i;
422
+ /** A no may carry its reason: "deny, it deletes the cache". */
423
+ const NO = /^(no|deny|denied|reject|stop|cancel)\b/i;
424
+
425
+ /**
426
+ * What a person said, as the outcome of the request the agent waits on (one
427
+ * a model may answer: not `personOnly`).
428
+ * A question takes the words (one per question, split on new lines or `;`,
429
+ * matched to an option's label where one fits). A plan is approved only by
430
+ * an answer that is a yes and nothing more ("approve", "ok", "go ahead"), and
431
+ * otherwise sent back with the words as what to change. A permission takes an
432
+ * option's name, a whole yes (allow once) or a no (reject once, the words as
433
+ * the reason); anything else cancels it.
434
+ */
435
+ export function answerFor(waiting: Waiting, text: string): Answer {
436
+ const said = text.trim();
437
+ if (waiting.kind === "question") {
438
+ const questions = waiting.request.questions ?? [];
439
+ // One answer per question, in order; a single question takes the whole text.
440
+ const each = questions.length > 1 ? said.split(/\s*\n\s*|\s*;\s*/) : [said];
441
+ const content: Record<string, string> = {};
442
+ questions.forEach((question, index) => {
443
+ const given = each[index];
444
+ if (!given) return;
445
+ const option = question.options?.find((o) => o.label.toLowerCase() === given.toLowerCase());
446
+ content[question.id] = option?.label ?? given;
447
+ });
448
+ if (questions.length === 0) content.answer = said;
449
+ return {
450
+ kind: "question",
451
+ requestId: waiting.request.requestId,
452
+ outcome: { action: "accept", content },
453
+ };
454
+ }
455
+ const { requestId, options } = waiting.request;
456
+ const pick = (optionId: string, message?: string): Answer => ({
457
+ kind: "decision",
458
+ requestId,
459
+ outcome: { outcome: "selected", optionId, ...(message ? { message } : {}) },
460
+ });
461
+ if (isPlan(waiting.request)) {
462
+ // Approving starts the work with edits still asked for; anything else is what to change.
463
+ return YES.test(said)
464
+ ? pick(PLAN_OPTION_IDS.approve)
465
+ : pick(PLAN_OPTION_IDS.keepPlanning, said);
466
+ }
467
+ const named = options.find((option) => option.name.toLowerCase() === said.toLowerCase());
468
+ if (named) return pick(named.optionId);
469
+ const kind = YES.test(said) ? "allow_once" : NO.test(said) ? "reject_once" : undefined;
470
+ const chosen = kind && options.find((option) => option.kind === kind);
471
+ if (chosen) return pick(chosen.optionId, kind === "reject_once" ? said : undefined);
472
+ return { kind: "decision", requestId, outcome: { outcome: "cancelled" } };
473
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * What an operator needs to know about one session, however its host keeps it.
3
+ *
4
+ * A host that holds a folded `ConversationState` gets this with `viewOf`. A host
5
+ * that keeps its own rows (messages with their parts, a status column) builds
6
+ * the same shape directly. Nothing here knows about projects, workspaces,
7
+ * accounts or storage: those are the host's.
8
+ */
9
+
10
+ import {
11
+ type ConversationElicitation,
12
+ type ConversationPermission,
13
+ type ConversationState,
14
+ conversationMessages,
15
+ pendingElicitations,
16
+ pendingPermissions,
17
+ } from "../protocol/conversation.ts";
18
+ import type { Part, UnknownPart } from "../protocol/parts.ts";
19
+ import { PLAN_OPTION_IDS } from "../protocol/plan.ts";
20
+
21
+ export interface ViewMessage {
22
+ role: "user" | "assistant";
23
+ parts: readonly (Part | UnknownPart)[];
24
+ /** A sub-agent's output: shown nested, never as the agent's reply. */
25
+ nested?: boolean;
26
+ }
27
+
28
+ /** A question the agent asked, as the engine's `elicitation.requested` carries it. */
29
+ export type QuestionRequest = Pick<
30
+ ConversationElicitation,
31
+ "requestId" | "mode" | "message" | "questions" | "url"
32
+ >;
33
+ /** A decision the agent waits on (a tool's approval, or a plan), as `permission.requested` carries it. */
34
+ export type DecisionRequest = Pick<
35
+ ConversationPermission,
36
+ "requestId" | "title" | "toolCall" | "options"
37
+ >;
38
+
39
+ export type Waiting =
40
+ | { kind: "question"; request: QuestionRequest }
41
+ | { kind: "decision"; request: DecisionRequest };
42
+
43
+ export interface SessionView {
44
+ /** Whether a turn is running. "Waiting on a person" is `waiting`, not a status. */
45
+ status: "idle" | "working" | "error";
46
+ messages: readonly ViewMessage[];
47
+ waiting: readonly Waiting[];
48
+ error?: string;
49
+ /** The host holds earlier messages than the ones given here. */
50
+ older?: boolean;
51
+ }
52
+
53
+ /** What a person would call the session's state. Waiting on them is not "finished". */
54
+ export type Phase = "idle" | "working" | "needs_input" | "error";
55
+
56
+ export function phaseOf(view: Pick<SessionView, "status" | "waiting">): Phase {
57
+ return view.waiting.length > 0 ? "needs_input" : view.status;
58
+ }
59
+
60
+ /** A plan's approval is a decision whose options include "keep planning" (`PLAN_OPTIONS`). */
61
+ export function isPlan(request: DecisionRequest): boolean {
62
+ return request.options.some((option) => option.optionId === PLAN_OPTION_IDS.keepPlanning);
63
+ }
64
+
65
+ /** The view of a session whose events were folded with the engine's reducer. */
66
+ export function viewOf(state: ConversationState): SessionView {
67
+ const last = state.turns.at(-1);
68
+ const failed = last?.status === "ended" ? last.error : undefined;
69
+ return {
70
+ status: last?.status === "active" ? "working" : failed ? "error" : "idle",
71
+ messages: conversationMessages(state).map((message) => ({
72
+ role: message.role,
73
+ parts: message.parts,
74
+ ...(message.parentToolCallId ? { nested: true } : {}),
75
+ })),
76
+ waiting: [
77
+ ...pendingElicitations(state).map((request) => ({ kind: "question" as const, request })),
78
+ ...pendingPermissions(state).map((request) => ({ kind: "decision" as const, request })),
79
+ ],
80
+ ...(failed ? { error: failed.message } : {}),
81
+ };
82
+ }
@@ -68,6 +68,24 @@ export const AuthMethodSchema = z.object({
68
68
  });
69
69
  export type AuthMethod = z.infer<typeof AuthMethodSchema>;
70
70
 
71
+ /**
72
+ * A login under way, answered at once: the user signs in at `url` (entering
73
+ * `userCode` there, for a device code) or, from another device, at `codeUrl`
74
+ * and pastes the code it shows (`auth/complete`). An `api_key` login has none
75
+ * of them. `auth/updated` says how it ended.
76
+ */
77
+ export const AuthLoginSchema = z.object({
78
+ loginId: z.string(),
79
+ harness: HarnessIdSchema,
80
+ method: z.string(),
81
+ url: z.string().optional(),
82
+ codeUrl: z.string().optional(),
83
+ userCode: z.string().optional(),
84
+ /** When the server gives up on it (epoch ms). */
85
+ expiresAt: z.number().int().nonnegative(),
86
+ });
87
+ export type AuthLogin = z.infer<typeof AuthLoginSchema>;
88
+
71
89
  /**
72
90
  * One harness's login, as `auth/status` and `auth/logout` answer it, and as
73
91
  * a product shows one it holds (`grantAuth`): who, on what kind of
@@ -85,6 +103,13 @@ export const HarnessAuthSchema = AccountInfoSchema.extend({
85
103
  /** The logins this harness offers here; none where its agent signs in on its own. */
86
104
  methods: z.array(AuthMethodSchema),
87
105
  error: z.string().optional(),
106
+ /**
107
+ * The login under way to this harness and store, as `auth/login` answered
108
+ * it: present from its start until its end is told, in every status read
109
+ * and `auth/updated`, so every window shows it (its `url`, `userCode`)
110
+ * and can cancel it; absent otherwise.
111
+ */
112
+ loginUnderWay: AuthLoginSchema.optional(),
88
113
  });
89
114
  export type HarnessAuth = z.infer<typeof HarnessAuthSchema>;
90
115
 
@@ -129,24 +154,6 @@ export const AuthLoginParamsSchema = z.object({
129
154
  });
130
155
  export type AuthLoginParams = z.infer<typeof AuthLoginParamsSchema>;
131
156
 
132
- /**
133
- * A login under way, answered at once: the user signs in at `url` (entering
134
- * `userCode` there, for a device code) or, from another device, at `codeUrl`
135
- * and pastes the code it shows (`auth/complete`). An `api_key` login has none
136
- * of them. `auth/updated` says how it ended.
137
- */
138
- export const AuthLoginSchema = z.object({
139
- loginId: z.string(),
140
- harness: HarnessIdSchema,
141
- method: z.string(),
142
- url: z.string().optional(),
143
- codeUrl: z.string().optional(),
144
- userCode: z.string().optional(),
145
- /** When the server gives up on it (epoch ms). */
146
- expiresAt: z.number().int().nonnegative(),
147
- });
148
- export type AuthLogin = z.infer<typeof AuthLoginSchema>;
149
-
150
157
  /** `auth/complete`: the code a `codeUrl` page showed, as pasted (`code#state`, or the page's address). */
151
158
  export const AuthCompleteParamsSchema = z.object({
152
159
  loginId: z.string(),
@@ -173,11 +180,11 @@ export const AuthLoginEndSchema = z.object({
173
180
  export type AuthLoginEnd = z.infer<typeof AuthLoginEndSchema>;
174
181
 
175
182
  /**
176
- * `auth/updated` (server -> every connection): a harness's login changed, or
177
- * a login ended (`login`), whoever caused it: `auth/login`, `auth/logout`, or
178
- * a login in a terminal that a status read noticed. A login's end is always
179
- * one of these, with the harness's login after it, as `auth/complete`
180
- * answers it.
183
+ * `auth/updated` (server -> every connection): a harness's login changed, a
184
+ * login started (`auth.loginUnderWay`, no `login`), or a login ended
185
+ * (`login`), whoever caused it: `auth/login`, `auth/logout`, or a login in a
186
+ * terminal that a status read noticed. A login's end is always one of these,
187
+ * with the harness's login after it, as `auth/complete` answers it.
181
188
  */
182
189
  export const AuthUpdatedSchema = z.object({
183
190
  auth: HarnessAuthSchema,
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ import { ErrorCategorySchema } from "./errors.ts";
2
3
  import { AgentHarnessSchema, HarnessIdSchema } from "./harness.ts";
3
4
  import { openVocabulary } from "./vocabulary.ts";
4
5
 
@@ -10,8 +11,10 @@ import { openVocabulary } from "./vocabulary.ts";
10
11
  * cannot run here;
11
12
  * - `installing` — being installed now, `receivedBytes` of `totalBytes`
12
13
  * downloaded (none before its download starts);
13
- * - `failed` — the last install failed, `error` says why; the next turn or
14
- * prepare tries again;
14
+ * - `failed` — the last install failed, `error` says why and `category`
15
+ * what failed (the error vocabulary's: `network` for a registry that
16
+ * cannot be reached or answers 5xx, or a transfer that was cut); the next
17
+ * turn or prepare tries again;
15
18
  * - `unmanaged` — the engine does not install this harness's agent (ACP: the
16
19
  * operator's launch command).
17
20
  * OPEN: treat a state this build does not know as `missing`.
@@ -37,6 +40,8 @@ export const HarnessCliSchema = z.object({
37
40
  receivedBytes: z.number().int().nonnegative().optional(),
38
41
  totalBytes: z.number().int().nonnegative().optional(),
39
42
  error: z.string().optional(),
43
+ /** What failed, with `error` (`failed` only). OPEN, as every category is. */
44
+ category: ErrorCategorySchema.optional(),
40
45
  });
41
46
  export type HarnessCli = z.infer<typeof HarnessCliSchema>;
42
47
 
@@ -55,8 +60,9 @@ export type CliPhase = (typeof CLI_PHASES)[number] | (string & {});
55
60
 
56
61
  /**
57
62
  * `clis/progress` (server -> every connection): a harness's CLI being
58
- * installed, whoever caused it (a turn, a model list, `clis/prepare`). At
59
- * most four a second per install, and always its last phase.
63
+ * installed, whoever caused it (a turn, `clis/prepare`, `auth/login`,
64
+ * `auth/logout`; a read such as `models/list` or `auth/status` installs
65
+ * nothing). At most four a second per install, and always its last phase.
60
66
  */
61
67
  export const HarnessCliProgressSchema = z.object({
62
68
  harness: HarnessIdSchema,
@@ -65,5 +71,7 @@ export const HarnessCliProgressSchema = z.object({
65
71
  receivedBytes: z.number().int().nonnegative(),
66
72
  totalBytes: z.number().int().nonnegative().optional(),
67
73
  error: z.string().optional(),
74
+ /** What failed, with `error` (`failed` only), as on `HarnessCli`. */
75
+ category: ErrorCategorySchema.optional(),
68
76
  });
69
77
  export type HarnessCliProgress = z.infer<typeof HarnessCliProgressSchema>;