@getpaseo/server 0.1.37 → 0.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/dist/scripts/dev-runner.js +1 -1
  2. package/dist/scripts/dev-runner.js.map +1 -1
  3. package/dist/scripts/{daemon-runner.js → supervisor-entrypoint.js} +7 -9
  4. package/dist/scripts/supervisor-entrypoint.js.map +1 -0
  5. package/dist/scripts/supervisor.js +17 -1
  6. package/dist/scripts/supervisor.js.map +1 -1
  7. package/dist/server/server/bootstrap.d.ts +0 -4
  8. package/dist/server/server/bootstrap.d.ts.map +1 -1
  9. package/dist/server/server/bootstrap.js +8 -22
  10. package/dist/server/server/bootstrap.js.map +1 -1
  11. package/dist/server/server/index.js +0 -4
  12. package/dist/server/server/index.js.map +1 -1
  13. package/dist/server/server/pid-lock.d.ts +7 -2
  14. package/dist/server/server/pid-lock.d.ts.map +1 -1
  15. package/dist/server/server/pid-lock.js +21 -0
  16. package/dist/server/server/pid-lock.js.map +1 -1
  17. package/dist/server/server/session.d.ts.map +1 -1
  18. package/dist/server/server/session.js +2 -2
  19. package/dist/server/server/session.js.map +1 -1
  20. package/dist/src/server/pid-lock.js +21 -0
  21. package/dist/src/server/pid-lock.js.map +1 -1
  22. package/package.json +3 -3
  23. package/dist/scripts/daemon-runner.js.map +0 -1
  24. package/dist/src/server/agent/activity-curator.js +0 -243
  25. package/dist/src/server/agent/activity-curator.js.map +0 -1
  26. package/dist/src/server/agent/agent-manager.js +0 -1855
  27. package/dist/src/server/agent/agent-manager.js.map +0 -1
  28. package/dist/src/server/agent/agent-metadata-generator.js +0 -161
  29. package/dist/src/server/agent/agent-metadata-generator.js.map +0 -1
  30. package/dist/src/server/agent/agent-projections.js +0 -254
  31. package/dist/src/server/agent/agent-projections.js.map +0 -1
  32. package/dist/src/server/agent/agent-response-loop.js +0 -304
  33. package/dist/src/server/agent/agent-response-loop.js.map +0 -1
  34. package/dist/src/server/agent/agent-sdk-types.js +0 -12
  35. package/dist/src/server/agent/agent-sdk-types.js.map +0 -1
  36. package/dist/src/server/agent/agent-storage.js +0 -302
  37. package/dist/src/server/agent/agent-storage.js.map +0 -1
  38. package/dist/src/server/agent/agent-title-limits.js +0 -3
  39. package/dist/src/server/agent/agent-title-limits.js.map +0 -1
  40. package/dist/src/server/agent/audio-utils.js +0 -19
  41. package/dist/src/server/agent/audio-utils.js.map +0 -1
  42. package/dist/src/server/agent/dictation-debug.js +0 -50
  43. package/dist/src/server/agent/dictation-debug.js.map +0 -1
  44. package/dist/src/server/agent/mcp-server.js +0 -754
  45. package/dist/src/server/agent/mcp-server.js.map +0 -1
  46. package/dist/src/server/agent/orchestrator-instructions.js +0 -51
  47. package/dist/src/server/agent/orchestrator-instructions.js.map +0 -1
  48. package/dist/src/server/agent/pcm16-resampler.js +0 -63
  49. package/dist/src/server/agent/pcm16-resampler.js.map +0 -1
  50. package/dist/src/server/agent/provider-launch-config.js +0 -213
  51. package/dist/src/server/agent/provider-launch-config.js.map +0 -1
  52. package/dist/src/server/agent/provider-manifest.js +0 -127
  53. package/dist/src/server/agent/provider-manifest.js.map +0 -1
  54. package/dist/src/server/agent/provider-registry.js +0 -45
  55. package/dist/src/server/agent/provider-registry.js.map +0 -1
  56. package/dist/src/server/agent/providers/claude/partial-json.js +0 -306
  57. package/dist/src/server/agent/providers/claude/partial-json.js.map +0 -1
  58. package/dist/src/server/agent/providers/claude/sdk-model-resolver.js +0 -104
  59. package/dist/src/server/agent/providers/claude/sdk-model-resolver.js.map +0 -1
  60. package/dist/src/server/agent/providers/claude/sidechain-tracker.js +0 -230
  61. package/dist/src/server/agent/providers/claude/sidechain-tracker.js.map +0 -1
  62. package/dist/src/server/agent/providers/claude/task-notification-tool-call.js +0 -267
  63. package/dist/src/server/agent/providers/claude/task-notification-tool-call.js.map +0 -1
  64. package/dist/src/server/agent/providers/claude/tool-call-detail-parser.js +0 -121
  65. package/dist/src/server/agent/providers/claude/tool-call-detail-parser.js.map +0 -1
  66. package/dist/src/server/agent/providers/claude/tool-call-mapper.js +0 -252
  67. package/dist/src/server/agent/providers/claude/tool-call-mapper.js.map +0 -1
  68. package/dist/src/server/agent/providers/claude-agent.js +0 -3166
  69. package/dist/src/server/agent/providers/claude-agent.js.map +0 -1
  70. package/dist/src/server/agent/providers/codex/tool-call-detail-parser.js +0 -104
  71. package/dist/src/server/agent/providers/codex/tool-call-detail-parser.js.map +0 -1
  72. package/dist/src/server/agent/providers/codex/tool-call-mapper.js +0 -758
  73. package/dist/src/server/agent/providers/codex/tool-call-mapper.js.map +0 -1
  74. package/dist/src/server/agent/providers/codex-app-server-agent.js +0 -2949
  75. package/dist/src/server/agent/providers/codex-app-server-agent.js.map +0 -1
  76. package/dist/src/server/agent/providers/codex-rollout-timeline.js +0 -544
  77. package/dist/src/server/agent/providers/codex-rollout-timeline.js.map +0 -1
  78. package/dist/src/server/agent/providers/opencode/tool-call-detail-parser.js +0 -39
  79. package/dist/src/server/agent/providers/opencode/tool-call-detail-parser.js.map +0 -1
  80. package/dist/src/server/agent/providers/opencode/tool-call-mapper.js +0 -144
  81. package/dist/src/server/agent/providers/opencode/tool-call-mapper.js.map +0 -1
  82. package/dist/src/server/agent/providers/opencode-agent.js +0 -1193
  83. package/dist/src/server/agent/providers/opencode-agent.js.map +0 -1
  84. package/dist/src/server/agent/providers/tool-call-detail-primitives.js +0 -686
  85. package/dist/src/server/agent/providers/tool-call-detail-primitives.js.map +0 -1
  86. package/dist/src/server/agent/providers/tool-call-mapper-utils.js +0 -115
  87. package/dist/src/server/agent/providers/tool-call-mapper-utils.js.map +0 -1
  88. package/dist/src/server/agent/recordings-debug.js +0 -19
  89. package/dist/src/server/agent/recordings-debug.js.map +0 -1
  90. package/dist/src/server/agent/stt-debug.js +0 -33
  91. package/dist/src/server/agent/stt-debug.js.map +0 -1
  92. package/dist/src/server/agent/stt-manager.js +0 -232
  93. package/dist/src/server/agent/stt-manager.js.map +0 -1
  94. package/dist/src/server/agent/timeline-append.js +0 -27
  95. package/dist/src/server/agent/timeline-append.js.map +0 -1
  96. package/dist/src/server/agent/timeline-projection.js +0 -215
  97. package/dist/src/server/agent/timeline-projection.js.map +0 -1
  98. package/dist/src/server/agent/tool-name-normalization.js +0 -45
  99. package/dist/src/server/agent/tool-name-normalization.js.map +0 -1
  100. package/dist/src/server/agent/tts-debug.js +0 -24
  101. package/dist/src/server/agent/tts-debug.js.map +0 -1
  102. package/dist/src/server/agent/tts-manager.js +0 -374
  103. package/dist/src/server/agent/tts-manager.js.map +0 -1
  104. package/dist/src/server/agent/wait-for-agent-tracker.js +0 -53
  105. package/dist/src/server/agent/wait-for-agent-tracker.js.map +0 -1
  106. package/dist/src/server/agent-attention-policy.js +0 -40
  107. package/dist/src/server/agent-attention-policy.js.map +0 -1
  108. package/dist/src/server/allowed-hosts.js +0 -94
  109. package/dist/src/server/allowed-hosts.js.map +0 -1
  110. package/dist/src/server/bootstrap.js +0 -620
  111. package/dist/src/server/bootstrap.js.map +0 -1
  112. package/dist/src/server/chat/chat-mentions.js +0 -71
  113. package/dist/src/server/chat/chat-mentions.js.map +0 -1
  114. package/dist/src/server/chat/chat-rpc-schemas.js +0 -103
  115. package/dist/src/server/chat/chat-rpc-schemas.js.map +0 -1
  116. package/dist/src/server/chat/chat-service.js +0 -330
  117. package/dist/src/server/chat/chat-service.js.map +0 -1
  118. package/dist/src/server/chat/chat-types.js +0 -22
  119. package/dist/src/server/chat/chat-types.js.map +0 -1
  120. package/dist/src/server/checkout-diff-manager.js +0 -272
  121. package/dist/src/server/checkout-diff-manager.js.map +0 -1
  122. package/dist/src/server/checkout-git-utils.js +0 -37
  123. package/dist/src/server/checkout-git-utils.js.map +0 -1
  124. package/dist/src/server/client-message-id.js +0 -12
  125. package/dist/src/server/client-message-id.js.map +0 -1
  126. package/dist/src/server/config.js +0 -73
  127. package/dist/src/server/config.js.map +0 -1
  128. package/dist/src/server/connection-offer.js +0 -59
  129. package/dist/src/server/connection-offer.js.map +0 -1
  130. package/dist/src/server/daemon-keypair.js +0 -40
  131. package/dist/src/server/daemon-keypair.js.map +0 -1
  132. package/dist/src/server/daemon-version.js +0 -22
  133. package/dist/src/server/daemon-version.js.map +0 -1
  134. package/dist/src/server/dictation/dictation-stream-manager.js +0 -571
  135. package/dist/src/server/dictation/dictation-stream-manager.js.map +0 -1
  136. package/dist/src/server/file-download/token-store.js +0 -40
  137. package/dist/src/server/file-download/token-store.js.map +0 -1
  138. package/dist/src/server/file-explorer/service.js +0 -180
  139. package/dist/src/server/file-explorer/service.js.map +0 -1
  140. package/dist/src/server/json-utils.js +0 -45
  141. package/dist/src/server/json-utils.js.map +0 -1
  142. package/dist/src/server/loop/rpc-schemas.js +0 -159
  143. package/dist/src/server/loop/rpc-schemas.js.map +0 -1
  144. package/dist/src/server/loop-service.js +0 -741
  145. package/dist/src/server/loop-service.js.map +0 -1
  146. package/dist/src/server/messages.js +0 -29
  147. package/dist/src/server/messages.js.map +0 -1
  148. package/dist/src/server/package-version.js +0 -46
  149. package/dist/src/server/package-version.js.map +0 -1
  150. package/dist/src/server/pairing-offer.js +0 -45
  151. package/dist/src/server/pairing-offer.js.map +0 -1
  152. package/dist/src/server/pairing-qr.js +0 -45
  153. package/dist/src/server/pairing-qr.js.map +0 -1
  154. package/dist/src/server/path-utils.js +0 -20
  155. package/dist/src/server/path-utils.js.map +0 -1
  156. package/dist/src/server/persisted-config.js +0 -265
  157. package/dist/src/server/persisted-config.js.map +0 -1
  158. package/dist/src/server/persistence-hooks.js +0 -60
  159. package/dist/src/server/persistence-hooks.js.map +0 -1
  160. package/dist/src/server/push/push-service.js +0 -68
  161. package/dist/src/server/push/push-service.js.map +0 -1
  162. package/dist/src/server/push/token-store.js +0 -70
  163. package/dist/src/server/push/token-store.js.map +0 -1
  164. package/dist/src/server/relay-transport.js +0 -461
  165. package/dist/src/server/relay-transport.js.map +0 -1
  166. package/dist/src/server/schedule/cron.js +0 -103
  167. package/dist/src/server/schedule/cron.js.map +0 -1
  168. package/dist/src/server/schedule/rpc-schemas.js +0 -112
  169. package/dist/src/server/schedule/rpc-schemas.js.map +0 -1
  170. package/dist/src/server/schedule/service.js +0 -397
  171. package/dist/src/server/schedule/service.js.map +0 -1
  172. package/dist/src/server/schedule/store.js +0 -56
  173. package/dist/src/server/schedule/store.js.map +0 -1
  174. package/dist/src/server/schedule/types.js +0 -73
  175. package/dist/src/server/schedule/types.js.map +0 -1
  176. package/dist/src/server/server-id.js +0 -63
  177. package/dist/src/server/server-id.js.map +0 -1
  178. package/dist/src/server/session.js +0 -6381
  179. package/dist/src/server/session.js.map +0 -1
  180. package/dist/src/server/speech/audio.js +0 -101
  181. package/dist/src/server/speech/audio.js.map +0 -1
  182. package/dist/src/server/speech/provider-resolver.js +0 -7
  183. package/dist/src/server/speech/provider-resolver.js.map +0 -1
  184. package/dist/src/server/speech/providers/local/config.js +0 -74
  185. package/dist/src/server/speech/providers/local/config.js.map +0 -1
  186. package/dist/src/server/speech/providers/local/models.js +0 -17
  187. package/dist/src/server/speech/providers/local/models.js.map +0 -1
  188. package/dist/src/server/speech/providers/local/pocket/pocket-tts-onnx.js +0 -436
  189. package/dist/src/server/speech/providers/local/pocket/pocket-tts-onnx.js.map +0 -1
  190. package/dist/src/server/speech/providers/local/runtime.js +0 -238
  191. package/dist/src/server/speech/providers/local/runtime.js.map +0 -1
  192. package/dist/src/server/speech/providers/local/sherpa/model-catalog.js +0 -166
  193. package/dist/src/server/speech/providers/local/sherpa/model-catalog.js.map +0 -1
  194. package/dist/src/server/speech/providers/local/sherpa/model-downloader.js +0 -165
  195. package/dist/src/server/speech/providers/local/sherpa/model-downloader.js.map +0 -1
  196. package/dist/src/server/speech/providers/local/sherpa/sherpa-offline-recognizer.js +0 -73
  197. package/dist/src/server/speech/providers/local/sherpa/sherpa-offline-recognizer.js.map +0 -1
  198. package/dist/src/server/speech/providers/local/sherpa/sherpa-online-recognizer.js +0 -84
  199. package/dist/src/server/speech/providers/local/sherpa/sherpa-online-recognizer.js.map +0 -1
  200. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-loader.js +0 -11
  201. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-loader.js.map +0 -1
  202. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-node-loader.js +0 -102
  203. package/dist/src/server/speech/providers/local/sherpa/sherpa-onnx-node-loader.js.map +0 -1
  204. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-realtime-session.js +0 -135
  205. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-realtime-session.js.map +0 -1
  206. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-stt.js +0 -130
  207. package/dist/src/server/speech/providers/local/sherpa/sherpa-parakeet-stt.js.map +0 -1
  208. package/dist/src/server/speech/providers/local/sherpa/sherpa-realtime-session.js +0 -110
  209. package/dist/src/server/speech/providers/local/sherpa/sherpa-realtime-session.js.map +0 -1
  210. package/dist/src/server/speech/providers/local/sherpa/sherpa-stt.js +0 -138
  211. package/dist/src/server/speech/providers/local/sherpa/sherpa-stt.js.map +0 -1
  212. package/dist/src/server/speech/providers/local/sherpa/sherpa-tts.js +0 -98
  213. package/dist/src/server/speech/providers/local/sherpa/sherpa-tts.js.map +0 -1
  214. package/dist/src/server/speech/providers/local/sherpa/silero-vad-provider.js +0 -23
  215. package/dist/src/server/speech/providers/local/sherpa/silero-vad-provider.js.map +0 -1
  216. package/dist/src/server/speech/providers/local/sherpa/silero-vad-session.js +0 -107
  217. package/dist/src/server/speech/providers/local/sherpa/silero-vad-session.js.map +0 -1
  218. package/dist/src/server/speech/providers/openai/config.js +0 -80
  219. package/dist/src/server/speech/providers/openai/config.js.map +0 -1
  220. package/dist/src/server/speech/providers/openai/realtime-transcription-session.js +0 -168
  221. package/dist/src/server/speech/providers/openai/realtime-transcription-session.js.map +0 -1
  222. package/dist/src/server/speech/providers/openai/runtime.js +0 -112
  223. package/dist/src/server/speech/providers/openai/runtime.js.map +0 -1
  224. package/dist/src/server/speech/providers/openai/stt.js +0 -206
  225. package/dist/src/server/speech/providers/openai/stt.js.map +0 -1
  226. package/dist/src/server/speech/providers/openai/tts.js +0 -46
  227. package/dist/src/server/speech/providers/openai/tts.js.map +0 -1
  228. package/dist/src/server/speech/speech-config-resolver.js +0 -102
  229. package/dist/src/server/speech/speech-config-resolver.js.map +0 -1
  230. package/dist/src/server/speech/speech-provider.js +0 -2
  231. package/dist/src/server/speech/speech-provider.js.map +0 -1
  232. package/dist/src/server/speech/speech-runtime.js +0 -530
  233. package/dist/src/server/speech/speech-runtime.js.map +0 -1
  234. package/dist/src/server/speech/speech-types.js +0 -8
  235. package/dist/src/server/speech/speech-types.js.map +0 -1
  236. package/dist/src/server/speech/turn-detection-provider.js +0 -2
  237. package/dist/src/server/speech/turn-detection-provider.js.map +0 -1
  238. package/dist/src/server/utils/diff-highlighter.js +0 -257
  239. package/dist/src/server/utils/diff-highlighter.js.map +0 -1
  240. package/dist/src/server/voice/fixed-duration-pcm-ring-buffer.js +0 -35
  241. package/dist/src/server/voice/fixed-duration-pcm-ring-buffer.js.map +0 -1
  242. package/dist/src/server/voice/voice-turn-controller.js +0 -159
  243. package/dist/src/server/voice/voice-turn-controller.js.map +0 -1
  244. package/dist/src/server/voice-config.js +0 -51
  245. package/dist/src/server/voice-config.js.map +0 -1
  246. package/dist/src/server/voice-mcp-bridge-command.js +0 -31
  247. package/dist/src/server/voice-mcp-bridge-command.js.map +0 -1
  248. package/dist/src/server/voice-mcp-bridge.js +0 -109
  249. package/dist/src/server/voice-mcp-bridge.js.map +0 -1
  250. package/dist/src/server/voice-permission-policy.js +0 -13
  251. package/dist/src/server/voice-permission-policy.js.map +0 -1
  252. package/dist/src/server/voice-types.js +0 -2
  253. package/dist/src/server/voice-types.js.map +0 -1
  254. package/dist/src/server/websocket-server.js +0 -1048
  255. package/dist/src/server/websocket-server.js.map +0 -1
  256. package/dist/src/server/workspace-registry-bootstrap.js +0 -98
  257. package/dist/src/server/workspace-registry-bootstrap.js.map +0 -1
  258. package/dist/src/server/workspace-registry-model.js +0 -175
  259. package/dist/src/server/workspace-registry-model.js.map +0 -1
  260. package/dist/src/server/workspace-registry.js +0 -151
  261. package/dist/src/server/workspace-registry.js.map +0 -1
  262. package/dist/src/server/worktree-bootstrap.js +0 -508
  263. package/dist/src/server/worktree-bootstrap.js.map +0 -1
  264. package/dist/src/shared/agent-attention-notification.js +0 -130
  265. package/dist/src/shared/agent-attention-notification.js.map +0 -1
  266. package/dist/src/shared/agent-lifecycle.js +0 -8
  267. package/dist/src/shared/agent-lifecycle.js.map +0 -1
  268. package/dist/src/shared/connection-offer.js +0 -17
  269. package/dist/src/shared/connection-offer.js.map +0 -1
  270. package/dist/src/shared/daemon-endpoints.js +0 -122
  271. package/dist/src/shared/daemon-endpoints.js.map +0 -1
  272. package/dist/src/shared/messages.js +0 -2107
  273. package/dist/src/shared/messages.js.map +0 -1
  274. package/dist/src/shared/path-utils.js +0 -16
  275. package/dist/src/shared/path-utils.js.map +0 -1
  276. package/dist/src/shared/terminal-stream-protocol.js +0 -99
  277. package/dist/src/shared/terminal-stream-protocol.js.map +0 -1
  278. package/dist/src/shared/tool-call-display.js +0 -122
  279. package/dist/src/shared/tool-call-display.js.map +0 -1
  280. package/dist/src/terminal/terminal-manager.js +0 -136
  281. package/dist/src/terminal/terminal-manager.js.map +0 -1
  282. package/dist/src/terminal/terminal.js +0 -333
  283. package/dist/src/terminal/terminal.js.map +0 -1
  284. package/dist/src/utils/checkout-git.js +0 -1518
  285. package/dist/src/utils/checkout-git.js.map +0 -1
  286. package/dist/src/utils/directory-suggestions.js +0 -671
  287. package/dist/src/utils/directory-suggestions.js.map +0 -1
  288. package/dist/src/utils/path.js +0 -15
  289. package/dist/src/utils/path.js.map +0 -1
  290. package/dist/src/utils/project-icon.js +0 -389
  291. package/dist/src/utils/project-icon.js.map +0 -1
  292. package/dist/src/utils/worktree-metadata.js +0 -116
  293. package/dist/src/utils/worktree-metadata.js.map +0 -1
  294. package/dist/src/utils/worktree.js +0 -744
  295. package/dist/src/utils/worktree.js.map +0 -1
@@ -1,1855 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { resolve } from "node:path";
3
- import { stat } from "node:fs/promises";
4
- import { AGENT_LIFECYCLE_STATUSES, } from "../../shared/agent-lifecycle.js";
5
- import { z } from "zod";
6
- import { AGENT_PROVIDER_IDS } from "./provider-manifest.js";
7
- export { AGENT_LIFECYCLE_STATUSES };
8
- const SYSTEM_ERROR_PREFIX = "[System Error]";
9
- function attachPersistenceCwd(handle, cwd) {
10
- if (!handle) {
11
- return null;
12
- }
13
- return {
14
- ...handle,
15
- metadata: {
16
- ...(handle.metadata ?? {}),
17
- cwd,
18
- },
19
- };
20
- }
21
- const DEFAULT_TIMELINE_FETCH_LIMIT = 200;
22
- const BUSY_STATUSES = ["initializing", "running"];
23
- const AgentIdSchema = z.string().uuid();
24
- function isAgentBusy(status) {
25
- return BUSY_STATUSES.includes(status);
26
- }
27
- function isTurnTerminalEvent(event) {
28
- return (event.type === "turn_completed" ||
29
- event.type === "turn_failed" ||
30
- event.type === "turn_canceled");
31
- }
32
- function createAbortError(signal, fallbackMessage) {
33
- const reason = signal?.reason;
34
- const message = typeof reason === "string"
35
- ? reason
36
- : reason instanceof Error
37
- ? reason.message
38
- : fallbackMessage;
39
- return Object.assign(new Error(message), { name: "AbortError" });
40
- }
41
- function validateAgentId(agentId, source) {
42
- const result = AgentIdSchema.safeParse(agentId);
43
- if (!result.success) {
44
- throw new Error(`${source}: agentId must be a UUID`);
45
- }
46
- return result.data;
47
- }
48
- function normalizeMessageId(messageId) {
49
- if (typeof messageId !== "string") {
50
- return undefined;
51
- }
52
- const trimmed = messageId.trim();
53
- return trimmed.length > 0 ? trimmed : undefined;
54
- }
55
- export class AgentManager {
56
- constructor(options) {
57
- this.clients = new Map();
58
- this.agents = new Map();
59
- this.pendingForegroundRuns = new Map();
60
- this.subscribers = new Set();
61
- this.previousStatuses = new Map();
62
- this.backgroundTasks = new Set();
63
- const maxTimelineItems = options?.maxTimelineItems;
64
- this.maxTimelineItems =
65
- typeof maxTimelineItems === "number" &&
66
- Number.isFinite(maxTimelineItems) &&
67
- maxTimelineItems >= 0
68
- ? Math.floor(maxTimelineItems)
69
- : null;
70
- this.idFactory = options?.idFactory ?? (() => randomUUID());
71
- this.registry = options?.registry;
72
- this.onAgentAttention = options?.onAgentAttention;
73
- this.logger = options.logger.child({ module: "agent", component: "agent-manager" });
74
- if (options?.clients) {
75
- for (const [provider, client] of Object.entries(options.clients)) {
76
- if (client) {
77
- this.registerClient(provider, client);
78
- }
79
- }
80
- }
81
- }
82
- registerClient(provider, client) {
83
- this.clients.set(provider, client);
84
- }
85
- setAgentAttentionCallback(callback) {
86
- this.onAgentAttention = callback;
87
- }
88
- getMetricsSnapshot() {
89
- const byLifecycle = {};
90
- let withActiveForegroundTurn = 0;
91
- let totalItems = 0;
92
- let maxItemsPerAgent = 0;
93
- for (const agent of this.agents.values()) {
94
- byLifecycle[agent.lifecycle] = (byLifecycle[agent.lifecycle] ?? 0) + 1;
95
- if (agent.activeForegroundTurnId !== null) {
96
- withActiveForegroundTurn++;
97
- }
98
- const len = agent.timeline.length;
99
- totalItems += len;
100
- if (len > maxItemsPerAgent) {
101
- maxItemsPerAgent = len;
102
- }
103
- }
104
- return {
105
- total: this.agents.size,
106
- byLifecycle,
107
- withActiveForegroundTurn,
108
- timelineStats: {
109
- totalItems,
110
- maxItemsPerAgent,
111
- },
112
- };
113
- }
114
- touchUpdatedAt(agent) {
115
- const nowMs = Date.now();
116
- const previousMs = agent.updatedAt.getTime();
117
- const nextMs = nowMs > previousMs ? nowMs : previousMs + 1;
118
- const next = new Date(nextMs);
119
- agent.updatedAt = next;
120
- return next;
121
- }
122
- hasInFlightRun(agentId) {
123
- const agent = this.agents.get(agentId);
124
- if (!agent) {
125
- return false;
126
- }
127
- return (agent.lifecycle === "running" ||
128
- Boolean(agent.activeForegroundTurnId) ||
129
- this.hasPendingForegroundRun(agentId));
130
- }
131
- subscribe(callback, options) {
132
- const targetAgentId = options?.agentId == null ? null : validateAgentId(options.agentId, "subscribe");
133
- const record = {
134
- callback,
135
- agentId: targetAgentId,
136
- };
137
- this.subscribers.add(record);
138
- if (options?.replayState !== false) {
139
- if (record.agentId) {
140
- const agent = this.agents.get(record.agentId);
141
- if (agent) {
142
- callback({
143
- type: "agent_state",
144
- agent: { ...agent },
145
- });
146
- }
147
- }
148
- else {
149
- // For global subscribers, skip internal agents during replay
150
- for (const agent of this.agents.values()) {
151
- if (agent.internal) {
152
- continue;
153
- }
154
- callback({
155
- type: "agent_state",
156
- agent: { ...agent },
157
- });
158
- }
159
- }
160
- }
161
- return () => {
162
- this.subscribers.delete(record);
163
- };
164
- }
165
- listAgents() {
166
- return Array.from(this.agents.values())
167
- .filter((agent) => !agent.internal)
168
- .map((agent) => ({
169
- ...agent,
170
- }));
171
- }
172
- async listPersistedAgents(options) {
173
- if (options?.provider) {
174
- const client = this.requireClient(options.provider);
175
- if (!client.listPersistedAgents) {
176
- return [];
177
- }
178
- return client.listPersistedAgents({ limit: options.limit });
179
- }
180
- const descriptors = [];
181
- for (const [provider, client] of this.clients.entries()) {
182
- if (!client.listPersistedAgents) {
183
- continue;
184
- }
185
- try {
186
- const entries = await client.listPersistedAgents({
187
- limit: options?.limit,
188
- });
189
- descriptors.push(...entries);
190
- }
191
- catch (error) {
192
- this.logger.warn({ err: error, provider }, "Failed to list persisted agents for provider");
193
- }
194
- }
195
- const limit = options?.limit ?? 20;
196
- return descriptors
197
- .sort((a, b) => b.lastActivityAt.getTime() - a.lastActivityAt.getTime())
198
- .slice(0, limit);
199
- }
200
- async listProviderAvailability() {
201
- const checks = AGENT_PROVIDER_IDS.map(async (providerId) => {
202
- const provider = providerId;
203
- const client = this.clients.get(provider);
204
- if (!client) {
205
- return {
206
- provider,
207
- available: false,
208
- error: `No client registered for provider '${provider}'`,
209
- };
210
- }
211
- try {
212
- const available = await client.isAvailable();
213
- return {
214
- provider,
215
- available,
216
- error: null,
217
- };
218
- }
219
- catch (error) {
220
- const message = error instanceof Error ? error.message : String(error);
221
- this.logger.warn({ err: error, provider }, "Failed to check provider availability");
222
- return {
223
- provider,
224
- available: false,
225
- error: message,
226
- };
227
- }
228
- });
229
- return Promise.all(checks);
230
- }
231
- async listDraftCommands(config) {
232
- const normalizedConfig = await this.normalizeConfig(config);
233
- const client = this.requireClient(normalizedConfig.provider);
234
- const available = await client.isAvailable();
235
- if (!available) {
236
- throw new Error(`Provider '${normalizedConfig.provider}' is not available. Please ensure the CLI is installed.`);
237
- }
238
- const session = await client.createSession(normalizedConfig);
239
- try {
240
- if (!session.listCommands) {
241
- throw new Error(`Provider '${normalizedConfig.provider}' does not support listing commands`);
242
- }
243
- return await session.listCommands();
244
- }
245
- finally {
246
- try {
247
- await session.close();
248
- }
249
- catch (error) {
250
- this.logger.warn({ err: error, provider: normalizedConfig.provider }, "Failed to close draft command listing session");
251
- }
252
- }
253
- }
254
- getAgent(id) {
255
- const agent = this.agents.get(id);
256
- return agent ? { ...agent } : null;
257
- }
258
- getTimeline(id) {
259
- const agent = this.requireAgent(id);
260
- return [...agent.timeline];
261
- }
262
- getTimelineRows(id) {
263
- const agent = this.requireAgent(id);
264
- const { rows } = this.ensureTimelineState(agent);
265
- return rows.map((row) => ({ ...row }));
266
- }
267
- fetchTimeline(id, options) {
268
- const agent = this.requireAgent(id);
269
- const { rows, epoch, nextSeq, minSeq, maxSeq } = this.ensureTimelineState(agent);
270
- const direction = options?.direction ?? "tail";
271
- const requestedLimit = options?.limit;
272
- const limit = requestedLimit === undefined
273
- ? DEFAULT_TIMELINE_FETCH_LIMIT
274
- : Math.max(0, Math.floor(requestedLimit));
275
- const cursor = options?.cursor;
276
- const window = { minSeq, maxSeq, nextSeq };
277
- if (cursor && cursor.epoch !== epoch) {
278
- return {
279
- epoch,
280
- direction,
281
- reset: true,
282
- staleCursor: true,
283
- gap: false,
284
- window,
285
- hasOlder: false,
286
- hasNewer: false,
287
- rows: rows.map((row) => ({ ...row })),
288
- };
289
- }
290
- const selectAll = limit === 0;
291
- const cloneRows = (items) => items.map((row) => ({ ...row }));
292
- if (direction === "after" && cursor && rows.length > 0 && cursor.seq < minSeq - 1) {
293
- return {
294
- epoch,
295
- direction,
296
- reset: true,
297
- staleCursor: false,
298
- gap: true,
299
- window,
300
- hasOlder: false,
301
- hasNewer: false,
302
- rows: cloneRows(rows),
303
- };
304
- }
305
- if (rows.length === 0) {
306
- return {
307
- epoch,
308
- direction,
309
- reset: false,
310
- staleCursor: false,
311
- gap: false,
312
- window,
313
- hasOlder: false,
314
- hasNewer: false,
315
- rows: [],
316
- };
317
- }
318
- if (direction === "tail") {
319
- const selected = selectAll || limit >= rows.length ? rows : rows.slice(rows.length - limit);
320
- const hasOlder = selected.length > 0 && selected[0].seq > minSeq;
321
- return {
322
- epoch,
323
- direction,
324
- reset: false,
325
- staleCursor: false,
326
- gap: false,
327
- window,
328
- hasOlder,
329
- hasNewer: false,
330
- rows: cloneRows(selected),
331
- };
332
- }
333
- if (direction === "after") {
334
- const baseSeq = cursor?.seq ?? 0;
335
- const startIdx = rows.findIndex((row) => row.seq > baseSeq);
336
- if (startIdx < 0) {
337
- return {
338
- epoch,
339
- direction,
340
- reset: false,
341
- staleCursor: false,
342
- gap: false,
343
- window,
344
- hasOlder: baseSeq >= minSeq,
345
- hasNewer: false,
346
- rows: [],
347
- };
348
- }
349
- const selected = selectAll ? rows.slice(startIdx) : rows.slice(startIdx, startIdx + limit);
350
- const lastSelected = selected[selected.length - 1];
351
- return {
352
- epoch,
353
- direction,
354
- reset: false,
355
- staleCursor: false,
356
- gap: false,
357
- window,
358
- hasOlder: selected[0].seq > minSeq,
359
- hasNewer: Boolean(lastSelected && lastSelected.seq < maxSeq),
360
- rows: cloneRows(selected),
361
- };
362
- }
363
- // direction === "before"
364
- const beforeSeq = cursor?.seq ?? nextSeq;
365
- const endExclusive = rows.findIndex((row) => row.seq >= beforeSeq);
366
- const boundedRows = endExclusive < 0 ? rows : rows.slice(0, endExclusive);
367
- const selected = selectAll || limit >= boundedRows.length
368
- ? boundedRows
369
- : boundedRows.slice(boundedRows.length - limit);
370
- const hasOlder = selected.length > 0 && selected[0].seq > minSeq;
371
- const hasNewer = endExclusive >= 0;
372
- return {
373
- epoch,
374
- direction,
375
- reset: false,
376
- staleCursor: false,
377
- gap: false,
378
- window,
379
- hasOlder,
380
- hasNewer,
381
- rows: cloneRows(selected),
382
- };
383
- }
384
- async createAgent(config, agentId, options) {
385
- // Generate agent ID early so we can use it in MCP config
386
- const resolvedAgentId = validateAgentId(agentId ?? this.idFactory(), "createAgent");
387
- const normalizedConfig = await this.normalizeConfig(config);
388
- const launchContext = this.buildLaunchContext(resolvedAgentId);
389
- const client = this.requireClient(normalizedConfig.provider);
390
- const available = await client.isAvailable();
391
- if (!available) {
392
- throw new Error(`Provider '${normalizedConfig.provider}' is not available. Please ensure the CLI is installed.`);
393
- }
394
- const session = await client.createSession(normalizedConfig, launchContext);
395
- return this.registerSession(session, normalizedConfig, resolvedAgentId, {
396
- labels: options?.labels,
397
- });
398
- }
399
- // Reconstruct an agent from provider persistence. Callers should explicitly
400
- // hydrate timeline history after resume.
401
- async resumeAgentFromPersistence(handle, overrides, agentId, options) {
402
- const resolvedAgentId = validateAgentId(agentId ?? this.idFactory(), "resumeAgentFromPersistence");
403
- const metadata = (handle.metadata ?? {});
404
- const mergedConfig = {
405
- ...metadata,
406
- ...overrides,
407
- provider: handle.provider,
408
- };
409
- const normalizedConfig = await this.normalizeConfig(mergedConfig);
410
- const resumeOverrides = normalizedConfig.model !== mergedConfig.model
411
- ? { ...overrides, model: normalizedConfig.model }
412
- : overrides;
413
- const launchContext = this.buildLaunchContext(resolvedAgentId);
414
- const client = this.requireClient(handle.provider);
415
- const session = await client.resumeSession(handle, resumeOverrides, launchContext);
416
- return this.registerSession(session, normalizedConfig, resolvedAgentId, options);
417
- }
418
- // Hot-reload an active agent session with config overrides while preserving
419
- // in-memory timeline state.
420
- async reloadAgentSession(agentId, overrides) {
421
- let existing = this.requireAgent(agentId);
422
- if (this.hasInFlightRun(agentId)) {
423
- await this.cancelAgentRun(agentId);
424
- existing = this.requireAgent(agentId);
425
- }
426
- const timelineState = this.ensureTimelineState(existing);
427
- const preservedTimeline = [...existing.timeline];
428
- const preservedTimelineRows = timelineState.rows.map((row) => ({ ...row }));
429
- const preservedTimelineEpoch = timelineState.epoch;
430
- const preservedTimelineNextSeq = timelineState.nextSeq;
431
- const preservedHistoryPrimed = existing.historyPrimed;
432
- const preservedLastUsage = existing.lastUsage;
433
- const preservedLastError = existing.lastError;
434
- const preservedAttention = existing.attention;
435
- const handle = existing.persistence;
436
- const provider = handle?.provider ?? existing.provider;
437
- const client = this.requireClient(provider);
438
- const refreshConfig = {
439
- ...existing.config,
440
- ...overrides,
441
- provider,
442
- };
443
- const normalizedConfig = await this.normalizeConfig(refreshConfig);
444
- const launchContext = this.buildLaunchContext(agentId);
445
- const session = handle
446
- ? await client.resumeSession(handle, normalizedConfig, launchContext)
447
- : await client.createSession(normalizedConfig, launchContext);
448
- // Remove the existing agent entry before swapping sessions
449
- this.agents.delete(agentId);
450
- if (existing.unsubscribeSession) {
451
- existing.unsubscribeSession();
452
- existing.unsubscribeSession = null;
453
- }
454
- for (const waiter of existing.foregroundTurnWaiters) {
455
- this.settleForegroundTurnWaiter(waiter);
456
- }
457
- existing.foregroundTurnWaiters.clear();
458
- this.settlePendingForegroundRun(agentId);
459
- try {
460
- await existing.session.close();
461
- }
462
- catch (error) {
463
- this.logger.warn({ err: error, agentId }, "Failed to close previous session during refresh");
464
- }
465
- // Preserve existing labels and timeline during reload.
466
- return this.registerSession(session, normalizedConfig, agentId, {
467
- labels: existing.labels,
468
- createdAt: existing.createdAt,
469
- updatedAt: existing.updatedAt,
470
- lastUserMessageAt: existing.lastUserMessageAt,
471
- timeline: preservedTimeline,
472
- timelineRows: preservedTimelineRows,
473
- timelineEpoch: preservedTimelineEpoch,
474
- timelineNextSeq: preservedTimelineNextSeq,
475
- historyPrimed: preservedHistoryPrimed,
476
- lastUsage: preservedLastUsage,
477
- lastError: preservedLastError,
478
- attention: preservedAttention,
479
- });
480
- }
481
- async closeAgent(agentId) {
482
- const agent = this.requireAgent(agentId);
483
- this.logger.trace({
484
- agentId,
485
- lifecycle: agent.lifecycle,
486
- activeForegroundTurnId: agent.activeForegroundTurnId,
487
- pendingPermissions: agent.pendingPermissions.size,
488
- }, "closeAgent: start");
489
- this.agents.delete(agentId);
490
- // Clean up previousStatus to prevent memory leak
491
- this.previousStatuses.delete(agentId);
492
- if (agent.unsubscribeSession) {
493
- agent.unsubscribeSession();
494
- agent.unsubscribeSession = null;
495
- }
496
- for (const waiter of agent.foregroundTurnWaiters) {
497
- // Wake up the generator so it can exit the await loop
498
- waiter.callback({
499
- type: "turn_canceled",
500
- provider: agent.provider,
501
- reason: "agent closed",
502
- turnId: waiter.turnId,
503
- });
504
- this.settleForegroundTurnWaiter(waiter);
505
- }
506
- agent.foregroundTurnWaiters.clear();
507
- this.settlePendingForegroundRun(agentId);
508
- const session = agent.session;
509
- const closedAgent = {
510
- ...agent,
511
- lifecycle: "closed",
512
- session: null,
513
- activeForegroundTurnId: null,
514
- };
515
- await session.close();
516
- this.emitState(closedAgent);
517
- this.logger.trace({ agentId }, "closeAgent: completed");
518
- }
519
- async archiveAgent(agentId) {
520
- const agent = this.requireAgent(agentId);
521
- if (!this.registry) {
522
- throw new Error("Agent storage is not configured");
523
- }
524
- await this.registry.applySnapshot(agent, {
525
- internal: agent.internal,
526
- });
527
- const stored = await this.registry.get(agentId);
528
- if (!stored) {
529
- throw new Error(`Agent ${agentId} not found in storage after snapshot`);
530
- }
531
- const archivedAt = new Date().toISOString();
532
- const normalizedStatus = stored.lastStatus === "running" || stored.lastStatus === "initializing"
533
- ? "idle"
534
- : stored.lastStatus;
535
- await this.registry.upsert({
536
- ...stored,
537
- archivedAt,
538
- lastStatus: normalizedStatus,
539
- requiresAttention: false,
540
- attentionReason: null,
541
- attentionTimestamp: null,
542
- });
543
- await this.closeAgent(agentId);
544
- return { archivedAt };
545
- }
546
- async setAgentMode(agentId, modeId) {
547
- const agent = this.requireAgent(agentId);
548
- await agent.session.setMode(modeId);
549
- agent.currentModeId = modeId;
550
- // Update runtimeInfo to reflect the new mode
551
- if (agent.runtimeInfo) {
552
- agent.runtimeInfo = { ...agent.runtimeInfo, modeId };
553
- }
554
- this.touchUpdatedAt(agent);
555
- this.emitState(agent);
556
- }
557
- async setAgentModel(agentId, modelId) {
558
- const agent = this.requireAgent(agentId);
559
- const normalizedModelId = typeof modelId === "string" && modelId.trim().length > 0 ? modelId : null;
560
- if (agent.session.setModel) {
561
- await agent.session.setModel(normalizedModelId);
562
- }
563
- agent.config.model = normalizedModelId ?? undefined;
564
- if (agent.runtimeInfo) {
565
- agent.runtimeInfo = { ...agent.runtimeInfo, model: normalizedModelId };
566
- }
567
- this.touchUpdatedAt(agent);
568
- this.emitState(agent);
569
- }
570
- async setAgentThinkingOption(agentId, thinkingOptionId) {
571
- const agent = this.requireAgent(agentId);
572
- const normalizedThinkingOptionId = typeof thinkingOptionId === "string" && thinkingOptionId.trim().length > 0
573
- ? thinkingOptionId
574
- : null;
575
- if (agent.session.setThinkingOption) {
576
- await agent.session.setThinkingOption(normalizedThinkingOptionId);
577
- }
578
- agent.config.thinkingOptionId = normalizedThinkingOptionId ?? undefined;
579
- this.touchUpdatedAt(agent);
580
- this.emitState(agent);
581
- }
582
- async setTitle(agentId, title) {
583
- const agent = this.requireAgent(agentId);
584
- const normalizedTitle = title.trim();
585
- if (!normalizedTitle) {
586
- return;
587
- }
588
- this.touchUpdatedAt(agent);
589
- await this.persistSnapshot(agent, { title: normalizedTitle });
590
- this.emitState(agent);
591
- }
592
- async setLabels(agentId, labels) {
593
- const agent = this.requireAgent(agentId);
594
- agent.labels = { ...agent.labels, ...labels };
595
- await this.persistSnapshot(agent);
596
- this.touchUpdatedAt(agent);
597
- this.emitState(agent);
598
- }
599
- notifyAgentState(agentId) {
600
- const agent = this.agents.get(agentId);
601
- if (!agent || agent.internal) {
602
- return;
603
- }
604
- this.touchUpdatedAt(agent);
605
- this.emitState(agent);
606
- }
607
- async clearAgentAttention(agentId) {
608
- const agent = this.requireAgent(agentId);
609
- if (agent.attention.requiresAttention) {
610
- agent.attention = { requiresAttention: false };
611
- await this.persistSnapshot(agent);
612
- this.emitState(agent);
613
- }
614
- }
615
- async runAgent(agentId, prompt, options) {
616
- const events = this.streamAgent(agentId, prompt, options);
617
- const timeline = [];
618
- let finalText = "";
619
- let usage;
620
- let canceled = false;
621
- for await (const event of events) {
622
- if (event.type === "timeline") {
623
- timeline.push(event.item);
624
- }
625
- else if (event.type === "turn_completed") {
626
- usage = event.usage;
627
- }
628
- else if (event.type === "turn_failed") {
629
- throw new Error(this.formatTurnFailedMessage(event));
630
- }
631
- else if (event.type === "turn_canceled") {
632
- canceled = true;
633
- }
634
- }
635
- finalText = this.getLastAssistantMessageFromTimeline(timeline) ?? "";
636
- const agent = this.requireAgent(agentId);
637
- const sessionId = agent.persistence?.sessionId;
638
- if (!sessionId) {
639
- throw new Error(`Agent ${agentId} has no persistence.sessionId after run completed`);
640
- }
641
- return {
642
- sessionId,
643
- finalText,
644
- usage,
645
- timeline,
646
- canceled,
647
- };
648
- }
649
- recordUserMessage(agentId, text, options) {
650
- const agent = this.requireAgent(agentId);
651
- const normalizedMessageId = normalizeMessageId(options?.messageId);
652
- const item = {
653
- type: "user_message",
654
- text,
655
- messageId: normalizedMessageId,
656
- };
657
- const updatedAt = this.touchUpdatedAt(agent);
658
- agent.lastUserMessageAt = updatedAt;
659
- const row = this.recordTimeline(agent, item);
660
- this.dispatchStream(agentId, {
661
- type: "timeline",
662
- item,
663
- provider: agent.provider,
664
- }, {
665
- seq: row.seq,
666
- epoch: this.ensureTimelineState(agent).epoch,
667
- });
668
- if (options?.emitState !== false) {
669
- this.emitState(agent);
670
- }
671
- }
672
- async appendTimelineItem(agentId, item) {
673
- const agent = this.requireAgent(agentId);
674
- this.touchUpdatedAt(agent);
675
- const row = this.recordTimeline(agent, item);
676
- this.dispatchStream(agentId, {
677
- type: "timeline",
678
- item,
679
- provider: agent.provider,
680
- }, {
681
- seq: row.seq,
682
- epoch: this.ensureTimelineState(agent).epoch,
683
- });
684
- await this.persistSnapshot(agent);
685
- }
686
- async emitLiveTimelineItem(agentId, item) {
687
- const agent = this.requireAgent(agentId);
688
- this.touchUpdatedAt(agent);
689
- this.dispatchStream(agentId, {
690
- type: "timeline",
691
- item,
692
- provider: agent.provider,
693
- });
694
- }
695
- streamAgent(agentId, prompt, options) {
696
- const existingAgent = this.requireAgent(agentId);
697
- this.logger.trace({
698
- agentId,
699
- lifecycle: existingAgent.lifecycle,
700
- activeForegroundTurnId: existingAgent.activeForegroundTurnId,
701
- hasPendingForegroundRun: this.hasPendingForegroundRun(agentId),
702
- promptType: typeof prompt === "string" ? "string" : "structured",
703
- hasRunOptions: Boolean(options),
704
- }, "streamAgent: requested");
705
- if (existingAgent.activeForegroundTurnId || this.hasPendingForegroundRun(agentId)) {
706
- this.logger.trace({
707
- agentId,
708
- lifecycle: existingAgent.lifecycle,
709
- hasPendingForegroundRun: this.hasPendingForegroundRun(agentId),
710
- }, "streamAgent: rejected because a foreground run is already in flight");
711
- throw new Error(`Agent ${agentId} already has an active run`);
712
- }
713
- const agent = existingAgent;
714
- agent.pendingReplacement = false;
715
- agent.lastError = undefined;
716
- const self = this;
717
- const streamForwarder = (async function* streamForwarder() {
718
- const pendingRun = self.createPendingForegroundRun();
719
- self.pendingForegroundRuns.set(agentId, pendingRun);
720
- let turnId;
721
- let waiter = null;
722
- try {
723
- const result = await agent.session.startTurn(prompt, options);
724
- turnId = result.turnId;
725
- }
726
- catch (error) {
727
- const errorMsg = error instanceof Error ? error.message : "Failed to start turn";
728
- self.handleStreamEvent(agent, {
729
- type: "turn_failed",
730
- provider: agent.provider,
731
- error: errorMsg,
732
- });
733
- self.finalizeForegroundTurn(agent);
734
- throw error;
735
- }
736
- pendingRun.started = true;
737
- agent.activeForegroundTurnId = turnId;
738
- agent.lifecycle = "running";
739
- self.touchUpdatedAt(agent);
740
- self.emitState(agent);
741
- self.logger.trace({
742
- agentId,
743
- lifecycle: agent.lifecycle,
744
- activeForegroundTurnId: agent.activeForegroundTurnId,
745
- }, "streamAgent: started");
746
- // Create a pushable queue for this foreground turn
747
- const queue = [];
748
- let queueResolve = null;
749
- let done = false;
750
- let resolveSettled;
751
- const settledPromise = new Promise((resolve) => {
752
- resolveSettled = resolve;
753
- });
754
- waiter = {
755
- turnId,
756
- settled: false,
757
- settledPromise,
758
- resolveSettled,
759
- callback: (event) => {
760
- queue.push(event);
761
- if (queueResolve) {
762
- queueResolve();
763
- queueResolve = null;
764
- }
765
- },
766
- };
767
- agent.foregroundTurnWaiters.add(waiter);
768
- try {
769
- while (!done) {
770
- while (queue.length > 0) {
771
- const event = queue.shift();
772
- yield event;
773
- if (isTurnTerminalEvent(event)) {
774
- done = true;
775
- break;
776
- }
777
- }
778
- if (!done && queue.length === 0) {
779
- if (waiter.settled) {
780
- break;
781
- }
782
- await new Promise((resolve) => {
783
- queueResolve = resolve;
784
- });
785
- }
786
- }
787
- }
788
- finally {
789
- if (waiter) {
790
- agent.foregroundTurnWaiters.delete(waiter);
791
- self.settleForegroundTurnWaiter(waiter);
792
- }
793
- self.settlePendingForegroundRun(agentId, pendingRun.token);
794
- if (!agent.activeForegroundTurnId) {
795
- await self.refreshRuntimeInfo(agent);
796
- }
797
- }
798
- })();
799
- return streamForwarder;
800
- }
801
- finalizeForegroundTurn(agent) {
802
- const mutableAgent = agent;
803
- mutableAgent.activeForegroundTurnId = null;
804
- const terminalError = mutableAgent.lastError;
805
- const shouldHoldBusyForReplacement = mutableAgent.pendingReplacement && !terminalError;
806
- mutableAgent.lifecycle = shouldHoldBusyForReplacement
807
- ? "running"
808
- : terminalError
809
- ? "error"
810
- : "idle";
811
- const persistenceHandle = mutableAgent.session.describePersistence() ??
812
- (mutableAgent.runtimeInfo?.sessionId
813
- ? { provider: mutableAgent.provider, sessionId: mutableAgent.runtimeInfo.sessionId }
814
- : null);
815
- if (persistenceHandle) {
816
- mutableAgent.persistence = attachPersistenceCwd(persistenceHandle, mutableAgent.cwd);
817
- }
818
- this.logger.trace({
819
- agentId: agent.id,
820
- lifecycle: mutableAgent.lifecycle,
821
- terminalError,
822
- pendingReplacement: mutableAgent.pendingReplacement,
823
- }, "finalizeForegroundTurn: applying terminal state");
824
- if (!shouldHoldBusyForReplacement) {
825
- this.touchUpdatedAt(mutableAgent);
826
- this.emitState(mutableAgent);
827
- }
828
- }
829
- replaceAgentRun(agentId, prompt, options) {
830
- const snapshot = this.requireAgent(agentId);
831
- if (snapshot.lifecycle !== "running" &&
832
- !snapshot.activeForegroundTurnId &&
833
- !this.hasPendingForegroundRun(agentId)) {
834
- return this.streamAgent(agentId, prompt, options);
835
- }
836
- const agent = snapshot;
837
- agent.pendingReplacement = true;
838
- const self = this;
839
- return (async function* replaceRunForwarder() {
840
- try {
841
- await self.cancelAgentRun(agentId);
842
- const nextRun = self.streamAgent(agentId, prompt, options);
843
- for await (const event of nextRun) {
844
- yield event;
845
- }
846
- }
847
- catch (error) {
848
- const latest = self.agents.get(agentId);
849
- if (latest) {
850
- const latestActive = latest;
851
- latestActive.pendingReplacement = false;
852
- if (!latestActive.activeForegroundTurnId && latestActive.lifecycle === "running") {
853
- latestActive.lifecycle = "idle";
854
- self.touchUpdatedAt(latestActive);
855
- self.emitState(latestActive);
856
- }
857
- }
858
- throw error;
859
- }
860
- })();
861
- }
862
- async waitForAgentRunStart(agentId, options) {
863
- const snapshot = this.getAgent(agentId);
864
- if (!snapshot) {
865
- throw new Error(`Agent ${agentId} not found`);
866
- }
867
- const pendingRun = this.getPendingForegroundRun(agentId);
868
- if ((snapshot.lifecycle === "running" || pendingRun?.started) &&
869
- !snapshot.pendingReplacement) {
870
- return;
871
- }
872
- if (!snapshot.activeForegroundTurnId && !pendingRun && !snapshot.pendingReplacement) {
873
- throw new Error(`Agent ${agentId} has no pending run`);
874
- }
875
- if (options?.signal?.aborted) {
876
- throw createAbortError(options.signal, "wait_for_agent_start aborted");
877
- }
878
- await new Promise((resolve, reject) => {
879
- if (options?.signal?.aborted) {
880
- reject(createAbortError(options.signal, "wait_for_agent_start aborted"));
881
- return;
882
- }
883
- let unsubscribe = null;
884
- let abortHandler = null;
885
- const cleanup = () => {
886
- if (unsubscribe) {
887
- try {
888
- unsubscribe();
889
- }
890
- catch {
891
- // ignore cleanup errors
892
- }
893
- unsubscribe = null;
894
- }
895
- if (abortHandler && options?.signal) {
896
- try {
897
- options.signal.removeEventListener("abort", abortHandler);
898
- }
899
- catch {
900
- // ignore cleanup errors
901
- }
902
- abortHandler = null;
903
- }
904
- };
905
- const finishOk = () => {
906
- cleanup();
907
- resolve();
908
- };
909
- const finishErr = (error) => {
910
- cleanup();
911
- reject(error);
912
- };
913
- if (options?.signal) {
914
- abortHandler = () => finishErr(createAbortError(options.signal, "wait_for_agent_start aborted"));
915
- options.signal.addEventListener("abort", abortHandler, { once: true });
916
- }
917
- const checkCurrentState = () => {
918
- const current = this.getAgent(agentId);
919
- if (!current) {
920
- finishErr(new Error(`Agent ${agentId} not found`));
921
- return true;
922
- }
923
- const currentPendingRun = this.getPendingForegroundRun(agentId);
924
- if ((current.lifecycle === "running" || currentPendingRun?.started) &&
925
- !current.pendingReplacement) {
926
- finishOk();
927
- return true;
928
- }
929
- if (current.lifecycle === "error" && !currentPendingRun?.started) {
930
- finishErr(new Error(current.lastError ?? `Agent ${agentId} failed to start`));
931
- return true;
932
- }
933
- if (!currentPendingRun &&
934
- !current.activeForegroundTurnId &&
935
- !current.pendingReplacement) {
936
- finishErr(new Error(`Agent ${agentId} run finished before starting`));
937
- return true;
938
- }
939
- return false;
940
- };
941
- unsubscribe = this.subscribe((event) => {
942
- if (event.type !== "agent_state" || event.agent.id !== agentId) {
943
- return;
944
- }
945
- checkCurrentState();
946
- }, { agentId, replayState: false });
947
- checkCurrentState();
948
- });
949
- }
950
- async respondToPermission(agentId, requestId, response) {
951
- const agent = this.requireAgent(agentId);
952
- await agent.session.respondToPermission(requestId, response);
953
- agent.pendingPermissions.delete(requestId);
954
- // Update currentModeId - the session may have changed mode internally
955
- // (e.g., plan approval changes mode from "plan" to "acceptEdits")
956
- try {
957
- agent.currentModeId = await agent.session.getCurrentMode();
958
- }
959
- catch {
960
- // Ignore errors from getCurrentMode - mode tracking is best effort
961
- }
962
- this.emitState(agent);
963
- }
964
- async cancelAgentRun(agentId) {
965
- const agent = this.requireAgent(agentId);
966
- const pendingRun = this.getPendingForegroundRun(agentId);
967
- const foregroundTurnId = agent.activeForegroundTurnId;
968
- const hasForegroundTurn = Boolean(foregroundTurnId);
969
- const isAutonomousRunning = agent.lifecycle === "running" && !hasForegroundTurn && !pendingRun;
970
- if (!hasForegroundTurn && !isAutonomousRunning && !pendingRun) {
971
- return false;
972
- }
973
- try {
974
- await agent.session.interrupt();
975
- }
976
- catch (error) {
977
- this.logger.error({ err: error, agentId }, "Failed to interrupt session");
978
- }
979
- // The interrupt will produce a turn_canceled/turn_failed event via subscribe(),
980
- // which flows through the session event dispatcher and settles the foreground turn waiter.
981
- // Wait briefly for the event to propagate if there's an active foreground turn.
982
- if (foregroundTurnId) {
983
- const waiter = Array.from(agent.foregroundTurnWaiters).find((candidate) => candidate.turnId === foregroundTurnId);
984
- const timeout = new Promise((resolve) => setTimeout(resolve, 2000));
985
- if (waiter) {
986
- await Promise.race([waiter.settledPromise, timeout]);
987
- }
988
- else if (agent.activeForegroundTurnId === foregroundTurnId) {
989
- await Promise.race([
990
- new Promise((resolve) => {
991
- const unsubscribe = this.subscribe((event) => {
992
- if (event.type === "agent_state" &&
993
- event.agent.id === agentId &&
994
- !event.agent.activeForegroundTurnId) {
995
- unsubscribe();
996
- resolve();
997
- }
998
- }, { agentId, replayState: false });
999
- }),
1000
- timeout,
1001
- ]);
1002
- }
1003
- // The waiter settling wakes up the streamForwarder generator, but its
1004
- // finally block (which deletes the pendingForegroundRun) runs asynchronously.
1005
- // Wait for the pending run to be fully cleaned up so the next streamAgent
1006
- // call doesn't see a stale entry and reject with "already has an active run".
1007
- if (pendingRun && !pendingRun.settled) {
1008
- await Promise.race([pendingRun.settledPromise, timeout]);
1009
- }
1010
- }
1011
- else if (pendingRun) {
1012
- const timeout = new Promise((resolve) => setTimeout(resolve, 2000));
1013
- await Promise.race([pendingRun.settledPromise, timeout]);
1014
- }
1015
- // If the foreground turn is still stuck after the timeout, force-dispatch a
1016
- // synthetic turn_canceled so the normal event pipeline cleans up
1017
- // activeForegroundTurnId, settles waiters, and unblocks the streamForwarder.
1018
- if (foregroundTurnId && agent.activeForegroundTurnId === foregroundTurnId) {
1019
- this.logger.warn({ agentId, foregroundTurnId }, "cancelAgentRun: foreground turn still active after timeout, force-canceling");
1020
- this.dispatchSessionEvent(agent, {
1021
- type: "turn_canceled",
1022
- provider: agent.provider,
1023
- reason: "interrupted",
1024
- turnId: foregroundTurnId,
1025
- });
1026
- // The synthetic event unblocks the streamForwarder generator, whose finally
1027
- // block settles the pending foreground run asynchronously. Wait for it.
1028
- const staleRun = this.getPendingForegroundRun(agentId);
1029
- if (staleRun && !staleRun.settled) {
1030
- await staleRun.settledPromise;
1031
- }
1032
- }
1033
- // Clear any pending permissions that weren't cleaned up by handleStreamEvent.
1034
- if (agent.pendingPermissions.size > 0) {
1035
- for (const [requestId] of agent.pendingPermissions) {
1036
- this.dispatchStream(agent.id, {
1037
- type: "permission_resolved",
1038
- provider: agent.provider,
1039
- requestId,
1040
- resolution: { behavior: "deny", message: "Interrupted" },
1041
- });
1042
- }
1043
- agent.pendingPermissions.clear();
1044
- this.touchUpdatedAt(agent);
1045
- this.emitState(agent);
1046
- }
1047
- return true;
1048
- }
1049
- getPendingPermissions(agentId) {
1050
- const agent = this.requireAgent(agentId);
1051
- return Array.from(agent.pendingPermissions.values());
1052
- }
1053
- peekPendingPermission(agent) {
1054
- const iterator = agent.pendingPermissions.values().next();
1055
- return iterator.done ? null : iterator.value;
1056
- }
1057
- async hydrateTimelineFromProvider(agentId) {
1058
- const agent = this.requireAgent(agentId);
1059
- await this.hydrateTimeline(agent);
1060
- }
1061
- getLastAssistantMessage(agentId) {
1062
- const agent = this.agents.get(agentId);
1063
- if (!agent) {
1064
- return null;
1065
- }
1066
- return this.getLastAssistantMessageFromTimeline(agent.timeline);
1067
- }
1068
- getLastAssistantMessageFromTimeline(timeline) {
1069
- // Collect the last contiguous assistant messages (Claude streams chunks)
1070
- const chunks = [];
1071
- for (let i = timeline.length - 1; i >= 0; i--) {
1072
- const item = timeline[i];
1073
- if (item.type !== "assistant_message") {
1074
- if (chunks.length) {
1075
- break;
1076
- }
1077
- continue;
1078
- }
1079
- chunks.push(item.text);
1080
- }
1081
- if (!chunks.length) {
1082
- return null;
1083
- }
1084
- return chunks.reverse().join("");
1085
- }
1086
- async waitForAgentEvent(agentId, options) {
1087
- const snapshot = this.getAgent(agentId);
1088
- if (!snapshot) {
1089
- throw new Error(`Agent ${agentId} not found`);
1090
- }
1091
- const hasForegroundTurn = Boolean(snapshot.activeForegroundTurnId) || this.hasPendingForegroundRun(agentId);
1092
- const immediatePermission = this.peekPendingPermission(snapshot);
1093
- if (immediatePermission) {
1094
- return {
1095
- status: snapshot.lifecycle,
1096
- permission: immediatePermission,
1097
- lastMessage: this.getLastAssistantMessage(agentId),
1098
- };
1099
- }
1100
- const initialStatus = snapshot.lifecycle;
1101
- const initialBusy = isAgentBusy(initialStatus) || hasForegroundTurn;
1102
- const waitForActive = options?.waitForActive ?? false;
1103
- if (!waitForActive && !initialBusy) {
1104
- return {
1105
- status: initialStatus,
1106
- permission: null,
1107
- lastMessage: this.getLastAssistantMessage(agentId),
1108
- };
1109
- }
1110
- if (waitForActive && !initialBusy && !hasForegroundTurn) {
1111
- return {
1112
- status: initialStatus,
1113
- permission: null,
1114
- lastMessage: this.getLastAssistantMessage(agentId),
1115
- };
1116
- }
1117
- if (options?.signal?.aborted) {
1118
- throw createAbortError(options.signal, "wait_for_agent aborted");
1119
- }
1120
- return await new Promise((resolve, reject) => {
1121
- // Bug #1 Fix: Check abort signal AGAIN inside Promise constructor
1122
- // to avoid race condition between pre-Promise check and abort listener registration
1123
- if (options?.signal?.aborted) {
1124
- reject(createAbortError(options.signal, "wait_for_agent aborted"));
1125
- return;
1126
- }
1127
- let currentStatus = initialStatus;
1128
- let hasStarted = initialBusy || hasForegroundTurn;
1129
- let terminalStatusOverride = null;
1130
- // Bug #3 Fix: Declare unsubscribe and abortHandler upfront so cleanup can reference them
1131
- let unsubscribe = null;
1132
- let abortHandler = null;
1133
- const cleanup = () => {
1134
- // Clean up subscription
1135
- if (unsubscribe) {
1136
- try {
1137
- unsubscribe();
1138
- }
1139
- catch {
1140
- // ignore cleanup errors
1141
- }
1142
- unsubscribe = null;
1143
- }
1144
- // Clean up abort listener
1145
- if (abortHandler && options?.signal) {
1146
- try {
1147
- options.signal.removeEventListener("abort", abortHandler);
1148
- }
1149
- catch {
1150
- // ignore cleanup errors
1151
- }
1152
- abortHandler = null;
1153
- }
1154
- };
1155
- const finish = (permission) => {
1156
- cleanup();
1157
- resolve({
1158
- status: currentStatus,
1159
- permission,
1160
- lastMessage: this.getLastAssistantMessage(agentId),
1161
- });
1162
- };
1163
- // Bug #3 Fix: Set up abort handler BEFORE subscription
1164
- // to ensure cleanup handlers exist before callback can fire
1165
- if (options?.signal) {
1166
- abortHandler = () => {
1167
- cleanup();
1168
- reject(createAbortError(options.signal, "wait_for_agent aborted"));
1169
- };
1170
- options.signal.addEventListener("abort", abortHandler, { once: true });
1171
- }
1172
- // Bug #3 Fix: Now subscribe with cleanup handlers already in place
1173
- // This prevents race condition if callback fires synchronously with replayState: true
1174
- unsubscribe = this.subscribe((event) => {
1175
- if (event.type === "agent_state") {
1176
- currentStatus = event.agent.lifecycle;
1177
- const pending = this.peekPendingPermission(event.agent);
1178
- if (pending) {
1179
- finish(pending);
1180
- return;
1181
- }
1182
- if (isAgentBusy(event.agent.lifecycle)) {
1183
- hasStarted = true;
1184
- return;
1185
- }
1186
- if (!waitForActive || hasStarted) {
1187
- if (terminalStatusOverride) {
1188
- currentStatus = terminalStatusOverride;
1189
- }
1190
- finish(null);
1191
- }
1192
- return;
1193
- }
1194
- if (event.type === "agent_stream") {
1195
- if (event.event.type === "permission_requested") {
1196
- finish(event.event.request);
1197
- return;
1198
- }
1199
- if (event.event.type === "turn_failed") {
1200
- hasStarted = true;
1201
- terminalStatusOverride = "error";
1202
- return;
1203
- }
1204
- if (event.event.type === "turn_completed") {
1205
- hasStarted = true;
1206
- }
1207
- if (event.event.type === "turn_canceled") {
1208
- hasStarted = true;
1209
- }
1210
- }
1211
- }, { agentId, replayState: true });
1212
- });
1213
- }
1214
- async registerSession(session, config, agentId, options) {
1215
- const resolvedAgentId = validateAgentId(agentId, "registerSession");
1216
- if (this.agents.has(resolvedAgentId)) {
1217
- throw new Error(`Agent with id ${resolvedAgentId} already exists`);
1218
- }
1219
- const initialPersistedTitle = await this.resolveInitialPersistedTitle(resolvedAgentId, config);
1220
- const now = new Date();
1221
- const initialTimeline = options?.timeline ? [...options.timeline] : [];
1222
- const initialTimelineRows = options?.timelineRows?.length
1223
- ? options.timelineRows.map((row) => ({ ...row }))
1224
- : this.buildTimelineRowsFromItems(initialTimeline, options?.timelineNextSeq ?? 1, (options?.updatedAt ?? options?.createdAt ?? now).toISOString());
1225
- const derivedNextSeq = options?.timelineNextSeq ??
1226
- (initialTimelineRows.length
1227
- ? initialTimelineRows[initialTimelineRows.length - 1].seq + 1
1228
- : 1);
1229
- const managed = {
1230
- id: resolvedAgentId,
1231
- provider: config.provider,
1232
- cwd: config.cwd,
1233
- session,
1234
- capabilities: session.capabilities,
1235
- config,
1236
- runtimeInfo: undefined,
1237
- lifecycle: "initializing",
1238
- createdAt: options?.createdAt ?? now,
1239
- updatedAt: options?.updatedAt ?? now,
1240
- availableModes: [],
1241
- currentModeId: null,
1242
- pendingPermissions: new Map(),
1243
- pendingReplacement: false,
1244
- activeForegroundTurnId: null,
1245
- foregroundTurnWaiters: new Set(),
1246
- unsubscribeSession: null,
1247
- timeline: initialTimeline,
1248
- timelineRows: initialTimelineRows,
1249
- timelineEpoch: options?.timelineEpoch ?? randomUUID(),
1250
- timelineNextSeq: derivedNextSeq,
1251
- persistence: attachPersistenceCwd(session.describePersistence(), config.cwd),
1252
- historyPrimed: options?.historyPrimed ?? false,
1253
- lastUserMessageAt: options?.lastUserMessageAt ?? null,
1254
- lastUsage: options?.lastUsage,
1255
- lastError: options?.lastError,
1256
- attention: options?.attention != null
1257
- ? options.attention.requiresAttention
1258
- ? {
1259
- requiresAttention: true,
1260
- attentionReason: options.attention.attentionReason,
1261
- attentionTimestamp: new Date(options.attention.attentionTimestamp),
1262
- }
1263
- : { requiresAttention: false }
1264
- : { requiresAttention: false },
1265
- internal: config.internal ?? false,
1266
- labels: options?.labels ?? {},
1267
- };
1268
- this.agents.set(resolvedAgentId, managed);
1269
- // Initialize previousStatus to track transitions
1270
- this.previousStatuses.set(resolvedAgentId, managed.lifecycle);
1271
- await this.refreshRuntimeInfo(managed);
1272
- await this.persistSnapshot(managed, {
1273
- title: initialPersistedTitle,
1274
- });
1275
- this.emitState(managed);
1276
- await this.refreshSessionState(managed);
1277
- managed.lifecycle = "idle";
1278
- await this.persistSnapshot(managed);
1279
- this.emitState(managed);
1280
- this.subscribeToSession(managed);
1281
- return { ...managed };
1282
- }
1283
- subscribeToSession(agent) {
1284
- if (agent.unsubscribeSession) {
1285
- return;
1286
- }
1287
- const agentId = agent.id;
1288
- const unsubscribe = agent.session.subscribe((event) => {
1289
- const current = this.agents.get(agentId);
1290
- if (!current) {
1291
- return;
1292
- }
1293
- this.dispatchSessionEvent(current, event);
1294
- });
1295
- agent.unsubscribeSession = unsubscribe;
1296
- }
1297
- dispatchSessionEvent(agent, event) {
1298
- const turnId = event.turnId;
1299
- const matchingWaiters = turnId == null
1300
- ? []
1301
- : Array.from(agent.foregroundTurnWaiters).filter((waiter) => waiter.turnId === turnId && !waiter.settled);
1302
- this.handleStreamEvent(agent, event);
1303
- for (const waiter of matchingWaiters) {
1304
- waiter.callback(event);
1305
- if (isTurnTerminalEvent(event)) {
1306
- this.settleForegroundTurnWaiter(waiter);
1307
- }
1308
- }
1309
- }
1310
- settleForegroundTurnWaiter(waiter) {
1311
- if (waiter.settled) {
1312
- return;
1313
- }
1314
- waiter.settled = true;
1315
- waiter.resolveSettled();
1316
- }
1317
- createPendingForegroundRun() {
1318
- let resolveSettled;
1319
- const settledPromise = new Promise((resolve) => {
1320
- resolveSettled = resolve;
1321
- });
1322
- return {
1323
- token: randomUUID(),
1324
- started: false,
1325
- settled: false,
1326
- settledPromise,
1327
- resolveSettled,
1328
- };
1329
- }
1330
- getPendingForegroundRun(agentId) {
1331
- return this.pendingForegroundRuns.get(agentId) ?? null;
1332
- }
1333
- hasPendingForegroundRun(agentId) {
1334
- return this.pendingForegroundRuns.has(agentId);
1335
- }
1336
- settlePendingForegroundRun(agentId, token) {
1337
- const pendingRun = this.pendingForegroundRuns.get(agentId);
1338
- if (!pendingRun) {
1339
- return;
1340
- }
1341
- if (token && pendingRun.token !== token) {
1342
- return;
1343
- }
1344
- this.pendingForegroundRuns.delete(agentId);
1345
- if (pendingRun.settled) {
1346
- return;
1347
- }
1348
- pendingRun.settled = true;
1349
- pendingRun.resolveSettled();
1350
- }
1351
- async resolveInitialPersistedTitle(agentId, config) {
1352
- const existing = await this.registry?.get(agentId);
1353
- if (existing) {
1354
- return existing.title ?? null;
1355
- }
1356
- if (Object.prototype.hasOwnProperty.call(config, "title")) {
1357
- return config.title ?? null;
1358
- }
1359
- return null;
1360
- }
1361
- buildTimelineRowsFromItems(items, startSeq, timestamp) {
1362
- let nextSeq = startSeq;
1363
- return items.map((item) => {
1364
- const row = {
1365
- seq: nextSeq,
1366
- timestamp,
1367
- item,
1368
- };
1369
- nextSeq += 1;
1370
- return row;
1371
- });
1372
- }
1373
- ensureTimelineState(agent) {
1374
- const minSeq = agent.timelineRows.length ? agent.timelineRows[0].seq : 0;
1375
- const maxSeq = agent.timelineRows.length
1376
- ? agent.timelineRows[agent.timelineRows.length - 1].seq
1377
- : 0;
1378
- return {
1379
- rows: agent.timelineRows,
1380
- epoch: agent.timelineEpoch,
1381
- nextSeq: agent.timelineNextSeq,
1382
- minSeq,
1383
- maxSeq,
1384
- };
1385
- }
1386
- async persistSnapshot(agent, options) {
1387
- if (!this.registry) {
1388
- return;
1389
- }
1390
- // Don't persist internal agents - they're ephemeral system tasks
1391
- if (agent.internal) {
1392
- return;
1393
- }
1394
- await this.registry.applySnapshot(agent, options);
1395
- }
1396
- async refreshSessionState(agent) {
1397
- try {
1398
- const modes = await agent.session.getAvailableModes();
1399
- agent.availableModes = modes;
1400
- }
1401
- catch {
1402
- agent.availableModes = [];
1403
- }
1404
- try {
1405
- agent.currentModeId = await agent.session.getCurrentMode();
1406
- }
1407
- catch {
1408
- agent.currentModeId = null;
1409
- }
1410
- try {
1411
- const pending = agent.session.getPendingPermissions();
1412
- agent.pendingPermissions = new Map(pending.map((request) => [request.id, request]));
1413
- }
1414
- catch {
1415
- agent.pendingPermissions.clear();
1416
- }
1417
- await this.refreshRuntimeInfo(agent);
1418
- }
1419
- async refreshRuntimeInfo(agent) {
1420
- try {
1421
- const newInfo = await agent.session.getRuntimeInfo();
1422
- const changed = newInfo.model !== agent.runtimeInfo?.model ||
1423
- newInfo.thinkingOptionId !== agent.runtimeInfo?.thinkingOptionId ||
1424
- newInfo.sessionId !== agent.runtimeInfo?.sessionId ||
1425
- newInfo.modeId !== agent.runtimeInfo?.modeId;
1426
- agent.runtimeInfo = newInfo;
1427
- if (!agent.persistence && newInfo.sessionId) {
1428
- agent.persistence = attachPersistenceCwd({ provider: agent.provider, sessionId: newInfo.sessionId }, agent.cwd);
1429
- }
1430
- // Emit state if runtimeInfo changed so clients get the updated model
1431
- if (changed) {
1432
- this.emitState(agent);
1433
- }
1434
- }
1435
- catch {
1436
- // Keep existing runtimeInfo if refresh fails.
1437
- }
1438
- }
1439
- async hydrateTimeline(agent) {
1440
- if (agent.historyPrimed) {
1441
- return;
1442
- }
1443
- agent.historyPrimed = true;
1444
- const canonicalUserMessagesById = new Map(agent.timelineRows.flatMap((row) => {
1445
- if (row.item.type !== "user_message") {
1446
- return [];
1447
- }
1448
- const messageId = normalizeMessageId(row.item.messageId);
1449
- if (!messageId) {
1450
- return [];
1451
- }
1452
- return [[messageId, row.item.text]];
1453
- }));
1454
- try {
1455
- for await (const event of agent.session.streamHistory()) {
1456
- this.handleStreamEvent(agent, event, {
1457
- fromHistory: true,
1458
- canonicalUserMessagesById: canonicalUserMessagesById.size > 0 ? canonicalUserMessagesById : undefined,
1459
- });
1460
- }
1461
- }
1462
- catch {
1463
- // ignore history failures
1464
- }
1465
- }
1466
- handleStreamEvent(agent, event, options) {
1467
- const eventTurnId = event.turnId;
1468
- const isForegroundEvent = Boolean(eventTurnId && agent.activeForegroundTurnId === eventTurnId);
1469
- // Only update timestamp for live events, not history replay
1470
- if (!options?.fromHistory) {
1471
- this.touchUpdatedAt(agent);
1472
- }
1473
- let timelineRow = null;
1474
- switch (event.type) {
1475
- case "thread_started":
1476
- {
1477
- const previousSessionId = agent.persistence?.sessionId ?? null;
1478
- const handle = agent.session.describePersistence();
1479
- if (handle) {
1480
- agent.persistence = attachPersistenceCwd(handle, agent.cwd);
1481
- if (agent.persistence?.sessionId !== previousSessionId) {
1482
- this.emitState(agent);
1483
- }
1484
- }
1485
- void this.refreshRuntimeInfo(agent);
1486
- }
1487
- break;
1488
- case "timeline":
1489
- // Skip provider-replayed user_message items during history hydration.
1490
- if (options?.fromHistory && event.item.type === "user_message") {
1491
- const eventMessageId = normalizeMessageId(event.item.messageId);
1492
- if (eventMessageId) {
1493
- const canonicalText = options?.canonicalUserMessagesById?.get(eventMessageId);
1494
- if (canonicalText === event.item.text) {
1495
- break;
1496
- }
1497
- }
1498
- }
1499
- // Suppress user_message echoes for the active foreground turn —
1500
- // these are already recorded by recordUserMessage().
1501
- if (!options?.fromHistory &&
1502
- event.item.type === "user_message" &&
1503
- isForegroundEvent) {
1504
- const eventMessageId = normalizeMessageId(event.item.messageId);
1505
- const eventText = event.item.text;
1506
- if (eventMessageId) {
1507
- const alreadyRecorded = agent.timelineRows.some((row) => {
1508
- if (row.item.type !== "user_message") {
1509
- return false;
1510
- }
1511
- const rowMessageId = normalizeMessageId(row.item.messageId);
1512
- return rowMessageId === eventMessageId && row.item.text === eventText;
1513
- });
1514
- if (alreadyRecorded) {
1515
- break;
1516
- }
1517
- }
1518
- }
1519
- timelineRow = this.recordTimeline(agent, event.item);
1520
- if (!options?.fromHistory && event.item.type === "user_message") {
1521
- agent.lastUserMessageAt = new Date();
1522
- this.emitState(agent);
1523
- }
1524
- break;
1525
- case "turn_completed":
1526
- this.logger.trace({
1527
- agentId: agent.id,
1528
- lifecycle: agent.lifecycle,
1529
- activeForegroundTurnId: agent.activeForegroundTurnId,
1530
- eventTurnId,
1531
- }, "handleStreamEvent: turn_completed");
1532
- agent.lastUsage = event.usage;
1533
- agent.lastError = undefined;
1534
- // For autonomous turns (not foreground), transition to idle
1535
- // unless a replacement is pending (avoid idle flash during replace)
1536
- if (!isForegroundEvent && agent.lifecycle !== "idle" && !agent.pendingReplacement) {
1537
- agent.lifecycle = "idle";
1538
- this.emitState(agent);
1539
- }
1540
- void this.refreshRuntimeInfo(agent);
1541
- break;
1542
- case "turn_failed":
1543
- this.logger.trace({
1544
- agentId: agent.id,
1545
- lifecycle: agent.lifecycle,
1546
- activeForegroundTurnId: agent.activeForegroundTurnId,
1547
- eventTurnId,
1548
- error: event.error,
1549
- code: event.code,
1550
- diagnostic: event.diagnostic,
1551
- }, "handleStreamEvent: turn_failed");
1552
- // For autonomous turns, set error state directly
1553
- if (!isForegroundEvent) {
1554
- agent.lifecycle = "error";
1555
- }
1556
- agent.lastError = event.error;
1557
- this.appendSystemErrorTimelineMessage(agent, event.provider, this.formatTurnFailedMessage(event), options);
1558
- for (const [requestId] of agent.pendingPermissions) {
1559
- agent.pendingPermissions.delete(requestId);
1560
- if (!options?.fromHistory) {
1561
- this.dispatchStream(agent.id, {
1562
- type: "permission_resolved",
1563
- provider: event.provider,
1564
- requestId,
1565
- resolution: { behavior: "deny", message: "Turn failed" },
1566
- });
1567
- }
1568
- }
1569
- if (!isForegroundEvent) {
1570
- this.emitState(agent);
1571
- }
1572
- break;
1573
- case "turn_canceled":
1574
- this.logger.trace({
1575
- agentId: agent.id,
1576
- lifecycle: agent.lifecycle,
1577
- activeForegroundTurnId: agent.activeForegroundTurnId,
1578
- eventTurnId,
1579
- }, "handleStreamEvent: turn_canceled");
1580
- // For autonomous turns, transition to idle
1581
- // unless a replacement is pending (avoid idle flash during replace)
1582
- if (!isForegroundEvent && !agent.pendingReplacement) {
1583
- agent.lifecycle = "idle";
1584
- }
1585
- agent.lastError = undefined;
1586
- for (const [requestId] of agent.pendingPermissions) {
1587
- agent.pendingPermissions.delete(requestId);
1588
- if (!options?.fromHistory) {
1589
- this.dispatchStream(agent.id, {
1590
- type: "permission_resolved",
1591
- provider: event.provider,
1592
- requestId,
1593
- resolution: { behavior: "deny", message: "Interrupted" },
1594
- });
1595
- }
1596
- }
1597
- if (!isForegroundEvent) {
1598
- this.emitState(agent);
1599
- }
1600
- break;
1601
- case "turn_started":
1602
- this.logger.trace({
1603
- agentId: agent.id,
1604
- lifecycle: agent.lifecycle,
1605
- activeForegroundTurnId: agent.activeForegroundTurnId,
1606
- eventTurnId,
1607
- }, "handleStreamEvent: turn_started");
1608
- // For autonomous turn_started (no foreground match), set running
1609
- if (!isForegroundEvent) {
1610
- agent.lifecycle = "running";
1611
- this.emitState(agent);
1612
- }
1613
- break;
1614
- case "permission_requested":
1615
- {
1616
- const hadPendingPermissions = agent.pendingPermissions.size > 0;
1617
- agent.pendingPermissions.set(event.request.id, event.request);
1618
- if (!hadPendingPermissions && !agent.internal) {
1619
- this.broadcastAgentAttention(agent, "permission");
1620
- }
1621
- }
1622
- this.emitState(agent);
1623
- break;
1624
- case "permission_resolved":
1625
- agent.pendingPermissions.delete(event.requestId);
1626
- this.emitState(agent);
1627
- break;
1628
- default:
1629
- break;
1630
- }
1631
- if (!options?.fromHistory && isForegroundEvent && isTurnTerminalEvent(event)) {
1632
- this.finalizeForegroundTurn(agent);
1633
- }
1634
- // Skip dispatching individual stream events during history replay.
1635
- if (!options?.fromHistory) {
1636
- this.dispatchStream(agent.id, event, timelineRow
1637
- ? {
1638
- seq: timelineRow.seq,
1639
- epoch: this.ensureTimelineState(agent).epoch,
1640
- }
1641
- : undefined);
1642
- }
1643
- }
1644
- appendSystemErrorTimelineMessage(agent, provider, message, options) {
1645
- if (options?.fromHistory) {
1646
- return;
1647
- }
1648
- const normalized = message.trim();
1649
- if (!normalized) {
1650
- return;
1651
- }
1652
- const text = `${SYSTEM_ERROR_PREFIX} ${normalized}`;
1653
- const lastItem = agent.timelineRows[agent.timelineRows.length - 1]?.item;
1654
- if (lastItem?.type === "assistant_message" && lastItem.text === text) {
1655
- return;
1656
- }
1657
- const item = { type: "assistant_message", text };
1658
- const row = this.recordTimeline(agent, item);
1659
- this.dispatchStream(agent.id, {
1660
- type: "timeline",
1661
- item,
1662
- provider,
1663
- }, {
1664
- seq: row.seq,
1665
- epoch: this.ensureTimelineState(agent).epoch,
1666
- });
1667
- }
1668
- formatTurnFailedMessage(event) {
1669
- const base = event.error.trim();
1670
- const parts = [base.length > 0 ? base : "Provider run failed"];
1671
- const code = event.code?.trim();
1672
- if (code) {
1673
- parts.push(`code: ${code}`);
1674
- }
1675
- const diagnostic = event.diagnostic?.trim();
1676
- if (diagnostic && diagnostic !== base) {
1677
- parts.push(diagnostic);
1678
- }
1679
- return parts.join("\n\n");
1680
- }
1681
- recordTimeline(agent, item) {
1682
- const timelineState = this.ensureTimelineState(agent);
1683
- const row = {
1684
- seq: timelineState.nextSeq,
1685
- timestamp: new Date().toISOString(),
1686
- item,
1687
- };
1688
- agent.timelineNextSeq = timelineState.nextSeq + 1;
1689
- agent.timeline.push(item);
1690
- timelineState.rows.push(row);
1691
- if (typeof this.maxTimelineItems === "number" &&
1692
- agent.timeline.length > this.maxTimelineItems) {
1693
- const removeCount = agent.timeline.length - this.maxTimelineItems;
1694
- agent.timeline.splice(0, removeCount);
1695
- timelineState.rows.splice(0, removeCount);
1696
- }
1697
- return row;
1698
- }
1699
- emitState(agent) {
1700
- // Keep attention as an edge-triggered unread signal, not a level signal.
1701
- this.checkAndSetAttention(agent);
1702
- this.dispatch({
1703
- type: "agent_state",
1704
- agent: { ...agent },
1705
- });
1706
- }
1707
- checkAndSetAttention(agent) {
1708
- const previousStatus = this.previousStatuses.get(agent.id);
1709
- const currentStatus = agent.lifecycle;
1710
- // Track the new status
1711
- this.previousStatuses.set(agent.id, currentStatus);
1712
- // Skip attention tracking for internal agents
1713
- if (agent.internal) {
1714
- return;
1715
- }
1716
- // Skip if already requires attention
1717
- if (agent.attention.requiresAttention) {
1718
- return;
1719
- }
1720
- // Check if agent transitioned from running to idle (finished)
1721
- if (previousStatus === "running" && currentStatus === "idle") {
1722
- agent.attention = {
1723
- requiresAttention: true,
1724
- attentionReason: "finished",
1725
- attentionTimestamp: new Date(),
1726
- };
1727
- this.broadcastAgentAttention(agent, "finished");
1728
- this.enqueueBackgroundPersist(agent);
1729
- return;
1730
- }
1731
- // Check if agent entered error state
1732
- if (previousStatus !== "error" && currentStatus === "error") {
1733
- agent.attention = {
1734
- requiresAttention: true,
1735
- attentionReason: "error",
1736
- attentionTimestamp: new Date(),
1737
- };
1738
- this.broadcastAgentAttention(agent, "error");
1739
- this.enqueueBackgroundPersist(agent);
1740
- return;
1741
- }
1742
- }
1743
- enqueueBackgroundPersist(agent) {
1744
- const task = this.persistSnapshot(agent).catch((err) => {
1745
- this.logger.error({ err, agentId: agent.id }, "Failed to persist agent snapshot");
1746
- });
1747
- this.trackBackgroundTask(task);
1748
- }
1749
- trackBackgroundTask(task) {
1750
- this.backgroundTasks.add(task);
1751
- void task.finally(() => {
1752
- this.backgroundTasks.delete(task);
1753
- });
1754
- }
1755
- /**
1756
- * Flush any background persistence work (best-effort).
1757
- * Used by daemon shutdown paths to avoid unhandled rejections after cleanup.
1758
- */
1759
- async flush() {
1760
- // Drain tasks, including tasks spawned while awaiting.
1761
- while (this.backgroundTasks.size > 0) {
1762
- const pending = Array.from(this.backgroundTasks);
1763
- await Promise.allSettled(pending);
1764
- }
1765
- }
1766
- broadcastAgentAttention(agent, reason) {
1767
- this.onAgentAttention?.({
1768
- agentId: agent.id,
1769
- provider: agent.provider,
1770
- reason,
1771
- });
1772
- }
1773
- dispatchStream(agentId, event, metadata) {
1774
- this.dispatch({ type: "agent_stream", agentId, event, ...metadata });
1775
- }
1776
- dispatch(event) {
1777
- for (const subscriber of this.subscribers) {
1778
- if (subscriber.agentId &&
1779
- event.type === "agent_stream" &&
1780
- subscriber.agentId !== event.agentId) {
1781
- continue;
1782
- }
1783
- if (subscriber.agentId &&
1784
- event.type === "agent_state" &&
1785
- subscriber.agentId !== event.agent.id) {
1786
- continue;
1787
- }
1788
- // Skip internal agents for global subscribers (those without a specific agentId)
1789
- if (!subscriber.agentId) {
1790
- if (event.type === "agent_state" && event.agent.internal) {
1791
- continue;
1792
- }
1793
- if (event.type === "agent_stream") {
1794
- const agent = this.agents.get(event.agentId);
1795
- if (agent?.internal) {
1796
- continue;
1797
- }
1798
- }
1799
- }
1800
- subscriber.callback(event);
1801
- }
1802
- }
1803
- async normalizeConfig(config) {
1804
- const normalized = { ...config };
1805
- // Always resolve cwd to absolute path for consistent history file lookup
1806
- if (normalized.cwd) {
1807
- normalized.cwd = resolve(normalized.cwd);
1808
- try {
1809
- const cwdStats = await stat(normalized.cwd);
1810
- if (!cwdStats.isDirectory()) {
1811
- throw new Error(`Working directory is not a directory: ${normalized.cwd}`);
1812
- }
1813
- }
1814
- catch (error) {
1815
- if (error instanceof Error &&
1816
- "code" in error &&
1817
- error.code === "ENOENT") {
1818
- throw new Error(`Working directory does not exist: ${normalized.cwd}`);
1819
- }
1820
- if (error instanceof Error) {
1821
- throw error;
1822
- }
1823
- throw new Error(`Failed to access working directory: ${normalized.cwd}`);
1824
- }
1825
- }
1826
- if (typeof normalized.model === "string") {
1827
- const trimmed = normalized.model.trim();
1828
- normalized.model = trimmed.length > 0 ? trimmed : undefined;
1829
- }
1830
- return normalized;
1831
- }
1832
- buildLaunchContext(agentId) {
1833
- return {
1834
- env: {
1835
- PASEO_AGENT_ID: agentId,
1836
- },
1837
- };
1838
- }
1839
- requireClient(provider) {
1840
- const client = this.clients.get(provider);
1841
- if (!client) {
1842
- throw new Error(`No client registered for provider '${provider}'`);
1843
- }
1844
- return client;
1845
- }
1846
- requireAgent(id) {
1847
- const normalizedId = validateAgentId(id, "requireAgent");
1848
- const agent = this.agents.get(normalizedId);
1849
- if (!agent) {
1850
- throw new Error(`Unknown agent '${normalizedId}'`);
1851
- }
1852
- return agent;
1853
- }
1854
- }
1855
- //# sourceMappingURL=agent-manager.js.map