@skillcdn/console 0.4.7 → 0.5.1

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 (599) hide show
  1. package/README.md +74 -14
  2. package/SKILLCDN.md +5 -3
  3. package/dist/agent-page.d.ts +44 -0
  4. package/dist/agent-page.d.ts.map +1 -0
  5. package/dist/agent-page.js +415 -0
  6. package/dist/agent-page.js.map +1 -0
  7. package/dist/agent-stream.d.ts +52 -0
  8. package/dist/agent-stream.d.ts.map +1 -0
  9. package/dist/agent-stream.js +155 -0
  10. package/dist/agent-stream.js.map +1 -0
  11. package/dist/api.d.ts +12 -6
  12. package/dist/api.d.ts.map +1 -1
  13. package/dist/api.js +11 -5
  14. package/dist/api.js.map +1 -1
  15. package/dist/avatar.d.ts +137 -0
  16. package/dist/avatar.d.ts.map +1 -0
  17. package/dist/avatar.js +1216 -0
  18. package/dist/avatar.js.map +1 -0
  19. package/dist/charts.d.ts +103 -0
  20. package/dist/charts.d.ts.map +1 -0
  21. package/dist/charts.js +182 -0
  22. package/dist/charts.js.map +1 -0
  23. package/dist/cli/cli.d.ts +39 -1
  24. package/dist/cli/cli.d.ts.map +1 -1
  25. package/dist/cli/cli.js +2424 -296
  26. package/dist/cli/cli.js.map +1 -1
  27. package/dist/cli/command-words.d.ts +20 -0
  28. package/dist/cli/command-words.d.ts.map +1 -0
  29. package/dist/cli/command-words.js +51 -0
  30. package/dist/cli/command-words.js.map +1 -0
  31. package/dist/cli/credentials.d.ts +59 -5
  32. package/dist/cli/credentials.d.ts.map +1 -1
  33. package/dist/cli/credentials.js +128 -16
  34. package/dist/cli/credentials.js.map +1 -1
  35. package/dist/cli/directory.d.ts +19 -4
  36. package/dist/cli/directory.d.ts.map +1 -1
  37. package/dist/cli/directory.js +16 -8
  38. package/dist/cli/directory.js.map +1 -1
  39. package/dist/cli/format.d.ts +91 -8
  40. package/dist/cli/format.d.ts.map +1 -1
  41. package/dist/cli/format.js +386 -57
  42. package/dist/cli/format.js.map +1 -1
  43. package/dist/cli/help.d.ts +13 -4
  44. package/dist/cli/help.d.ts.map +1 -1
  45. package/dist/cli/help.js +202 -62
  46. package/dist/cli/help.js.map +1 -1
  47. package/dist/cli/main.js +94 -0
  48. package/dist/cli/main.js.map +1 -1
  49. package/dist/cli/pack.d.ts +69 -36
  50. package/dist/cli/pack.d.ts.map +1 -1
  51. package/dist/cli/pack.js +241 -188
  52. package/dist/cli/pack.js.map +1 -1
  53. package/dist/cli/plugin-check.d.ts +42 -0
  54. package/dist/cli/plugin-check.d.ts.map +1 -1
  55. package/dist/cli/plugin-check.js +221 -36
  56. package/dist/cli/plugin-check.js.map +1 -1
  57. package/dist/cli/plugin-cli.d.ts +6 -1
  58. package/dist/cli/plugin-cli.d.ts.map +1 -1
  59. package/dist/cli/plugin-cli.js +21 -4
  60. package/dist/cli/plugin-cli.js.map +1 -1
  61. package/dist/cli/plugin-main.js +1 -0
  62. package/dist/cli/plugin-main.js.map +1 -1
  63. package/dist/cli/runner-host.d.ts +36 -0
  64. package/dist/cli/runner-host.d.ts.map +1 -0
  65. package/dist/cli/runner-host.js +154 -0
  66. package/dist/cli/runner-host.js.map +1 -0
  67. package/dist/cli/serve.d.ts +33 -1
  68. package/dist/cli/serve.d.ts.map +1 -1
  69. package/dist/cli/serve.js +275 -6
  70. package/dist/cli/serve.js.map +1 -1
  71. package/dist/cli/setup-machine.d.ts +36 -0
  72. package/dist/cli/setup-machine.d.ts.map +1 -0
  73. package/dist/cli/setup-machine.js +109 -0
  74. package/dist/cli/setup-machine.js.map +1 -0
  75. package/dist/client.d.ts +189 -10
  76. package/dist/client.d.ts.map +1 -1
  77. package/dist/client.js +192 -57
  78. package/dist/client.js.map +1 -1
  79. package/dist/compat.d.ts +1 -1
  80. package/dist/compat.js +2 -2
  81. package/dist/components/agent-page.d.ts +384 -0
  82. package/dist/components/agent-page.d.ts.map +1 -0
  83. package/dist/components/agent-page.js +985 -0
  84. package/dist/components/agent-page.js.map +1 -0
  85. package/dist/components/agents.d.ts +102 -0
  86. package/dist/components/agents.d.ts.map +1 -0
  87. package/dist/components/agents.js +246 -0
  88. package/dist/components/agents.js.map +1 -0
  89. package/dist/components/calendar.d.ts +17 -17
  90. package/dist/components/calendar.d.ts.map +1 -1
  91. package/dist/components/calendar.js +7 -2
  92. package/dist/components/calendar.js.map +1 -1
  93. package/dist/components/charts.d.ts +157 -0
  94. package/dist/components/charts.d.ts.map +1 -0
  95. package/dist/components/charts.js +182 -0
  96. package/dist/components/charts.js.map +1 -0
  97. package/dist/components/comments.d.ts +2 -0
  98. package/dist/components/comments.d.ts.map +1 -1
  99. package/dist/components/comments.js +1 -1
  100. package/dist/components/comments.js.map +1 -1
  101. package/dist/components/connect.d.ts.map +1 -1
  102. package/dist/components/connect.js +7 -2
  103. package/dist/components/connect.js.map +1 -1
  104. package/dist/components/decision-form.d.ts +21 -0
  105. package/dist/components/decision-form.d.ts.map +1 -1
  106. package/dist/components/decision-form.js +20 -6
  107. package/dist/components/decision-form.js.map +1 -1
  108. package/dist/components/decision-list.d.ts +24 -0
  109. package/dist/components/decision-list.d.ts.map +1 -1
  110. package/dist/components/decision-list.js +54 -5
  111. package/dist/components/decision-list.js.map +1 -1
  112. package/dist/components/desk-records.d.ts +102 -0
  113. package/dist/components/desk-records.d.ts.map +1 -0
  114. package/dist/components/desk-records.js +221 -0
  115. package/dist/components/desk-records.js.map +1 -0
  116. package/dist/components/desk.d.ts +85 -0
  117. package/dist/components/desk.d.ts.map +1 -0
  118. package/dist/components/desk.js +135 -0
  119. package/dist/components/desk.js.map +1 -0
  120. package/dist/components/event-feed.d.ts.map +1 -1
  121. package/dist/components/event-feed.js +26 -1
  122. package/dist/components/event-feed.js.map +1 -1
  123. package/dist/components/home.d.ts +12 -1
  124. package/dist/components/home.d.ts.map +1 -1
  125. package/dist/components/home.js +175 -16
  126. package/dist/components/home.js.map +1 -1
  127. package/dist/components/my-decisions.d.ts +5 -0
  128. package/dist/components/my-decisions.d.ts.map +1 -1
  129. package/dist/components/my-decisions.js +3 -3
  130. package/dist/components/my-decisions.js.map +1 -1
  131. package/dist/components/people.d.ts +17 -0
  132. package/dist/components/people.d.ts.map +1 -1
  133. package/dist/components/people.js +55 -19
  134. package/dist/components/people.js.map +1 -1
  135. package/dist/components/picker.d.ts +2 -0
  136. package/dist/components/picker.d.ts.map +1 -1
  137. package/dist/components/picker.js +1 -1
  138. package/dist/components/picker.js.map +1 -1
  139. package/dist/components/pictures.d.ts +38 -10
  140. package/dist/components/pictures.d.ts.map +1 -1
  141. package/dist/components/pictures.js +195 -40
  142. package/dist/components/pictures.js.map +1 -1
  143. package/dist/components/plugin-events.d.ts +29 -0
  144. package/dist/components/plugin-events.d.ts.map +1 -0
  145. package/dist/components/plugin-events.js +139 -0
  146. package/dist/components/plugin-events.js.map +1 -0
  147. package/dist/components/plugin-frame.d.ts.map +1 -1
  148. package/dist/components/plugin-frame.js +14 -37
  149. package/dist/components/plugin-frame.js.map +1 -1
  150. package/dist/components/plugin-settings.d.ts.map +1 -1
  151. package/dist/components/plugin-settings.js +58 -7
  152. package/dist/components/plugin-settings.js.map +1 -1
  153. package/dist/components/projects.d.ts.map +1 -1
  154. package/dist/components/projects.js +30 -10
  155. package/dist/components/projects.js.map +1 -1
  156. package/dist/components/rules.d.ts +8 -10
  157. package/dist/components/rules.d.ts.map +1 -1
  158. package/dist/components/rules.js +36 -14
  159. package/dist/components/rules.js.map +1 -1
  160. package/dist/components/sessions.d.ts.map +1 -1
  161. package/dist/components/sessions.js +8 -4
  162. package/dist/components/sessions.js.map +1 -1
  163. package/dist/components/setup.d.ts +59 -0
  164. package/dist/components/setup.d.ts.map +1 -0
  165. package/dist/components/setup.js +93 -0
  166. package/dist/components/setup.js.map +1 -0
  167. package/dist/components/sign-in.js +1 -1
  168. package/dist/components/sign-in.js.map +1 -1
  169. package/dist/components/task-form.d.ts.map +1 -1
  170. package/dist/components/task-form.js +2 -2
  171. package/dist/components/task-form.js.map +1 -1
  172. package/dist/components/task-list.d.ts +7 -6
  173. package/dist/components/task-list.d.ts.map +1 -1
  174. package/dist/components/task-list.js.map +1 -1
  175. package/dist/components/task-view.d.ts +3 -0
  176. package/dist/components/task-view.d.ts.map +1 -1
  177. package/dist/components/task-view.js +1 -1
  178. package/dist/components/task-view.js.map +1 -1
  179. package/dist/components/ui.d.ts +78 -8
  180. package/dist/components/ui.d.ts.map +1 -1
  181. package/dist/components/ui.js +88 -9
  182. package/dist/components/ui.js.map +1 -1
  183. package/dist/components/work.d.ts +5 -5
  184. package/dist/components/work.d.ts.map +1 -1
  185. package/dist/components/work.js.map +1 -1
  186. package/dist/components/workspace.d.ts +17 -0
  187. package/dist/components/workspace.d.ts.map +1 -1
  188. package/dist/components/workspace.js +129 -2
  189. package/dist/components/workspace.js.map +1 -1
  190. package/dist/connect-page.d.ts.map +1 -1
  191. package/dist/connect-page.js +5 -2
  192. package/dist/connect-page.js.map +1 -1
  193. package/dist/console.css +7551 -4580
  194. package/dist/console.d.ts +20 -0
  195. package/dist/console.d.ts.map +1 -1
  196. package/dist/console.js +331 -57
  197. package/dist/console.js.map +1 -1
  198. package/dist/data.d.ts +22 -1
  199. package/dist/data.d.ts.map +1 -1
  200. package/dist/data.js +62 -34
  201. package/dist/data.js.map +1 -1
  202. package/dist/desk-page.d.ts +38 -0
  203. package/dist/desk-page.d.ts.map +1 -0
  204. package/dist/desk-page.js +184 -0
  205. package/dist/desk-page.js.map +1 -0
  206. package/dist/desk-records-page.d.ts +18 -0
  207. package/dist/desk-records-page.d.ts.map +1 -0
  208. package/dist/desk-records-page.js +130 -0
  209. package/dist/desk-records-page.js.map +1 -0
  210. package/dist/desk-records.d.ts +247 -0
  211. package/dist/desk-records.d.ts.map +1 -0
  212. package/dist/desk-records.js +543 -0
  213. package/dist/desk-records.js.map +1 -0
  214. package/dist/desk.d.ts +122 -0
  215. package/dist/desk.d.ts.map +1 -0
  216. package/dist/desk.js +211 -0
  217. package/dist/desk.js.map +1 -0
  218. package/dist/feed-stream.d.ts +39 -0
  219. package/dist/feed-stream.d.ts.map +1 -0
  220. package/dist/feed-stream.js +117 -0
  221. package/dist/feed-stream.js.map +1 -0
  222. package/dist/glances.d.ts +39 -0
  223. package/dist/glances.d.ts.map +1 -0
  224. package/dist/glances.js +94 -0
  225. package/dist/glances.js.map +1 -0
  226. package/dist/home-tiles.d.ts +69 -5
  227. package/dist/home-tiles.d.ts.map +1 -1
  228. package/dist/home-tiles.js +347 -9
  229. package/dist/home-tiles.js.map +1 -1
  230. package/dist/i18n/en.d.ts +773 -3
  231. package/dist/i18n/en.d.ts.map +1 -1
  232. package/dist/i18n/en.js +770 -9
  233. package/dist/i18n/en.js.map +1 -1
  234. package/dist/i18n/ko.d.ts.map +1 -1
  235. package/dist/i18n/ko.js +736 -9
  236. package/dist/i18n/ko.js.map +1 -1
  237. package/dist/index.d.ts +19 -7
  238. package/dist/index.d.ts.map +1 -1
  239. package/dist/index.js +17 -5
  240. package/dist/index.js.map +1 -1
  241. package/dist/limits.d.ts +67 -0
  242. package/dist/limits.d.ts.map +1 -1
  243. package/dist/limits.js +67 -0
  244. package/dist/limits.js.map +1 -1
  245. package/dist/onboarding.d.ts +21 -0
  246. package/dist/onboarding.d.ts.map +1 -0
  247. package/dist/onboarding.js +43 -0
  248. package/dist/onboarding.js.map +1 -0
  249. package/dist/persona.d.ts +34 -0
  250. package/dist/persona.d.ts.map +1 -0
  251. package/dist/persona.js +71 -0
  252. package/dist/persona.js.map +1 -0
  253. package/dist/plugin/context.d.ts +42 -11
  254. package/dist/plugin/context.d.ts.map +1 -1
  255. package/dist/plugin/limits.d.ts +2 -0
  256. package/dist/plugin/limits.d.ts.map +1 -1
  257. package/dist/plugin/limits.js +2 -0
  258. package/dist/plugin/limits.js.map +1 -1
  259. package/dist/plugin/manifest.d.ts +13 -0
  260. package/dist/plugin/manifest.d.ts.map +1 -1
  261. package/dist/plugin/manifest.js +34 -4
  262. package/dist/plugin/manifest.js.map +1 -1
  263. package/dist/plugin/rest-schemas.d.ts +56 -0
  264. package/dist/plugin/rest-schemas.d.ts.map +1 -1
  265. package/dist/plugin/rest-schemas.js +42 -0
  266. package/dist/plugin/rest-schemas.js.map +1 -1
  267. package/dist/plugin/sandbox.d.ts.map +1 -1
  268. package/dist/plugin/sandbox.js +4 -1
  269. package/dist/plugin/sandbox.js.map +1 -1
  270. package/dist/plugin/testing.d.ts +6 -2
  271. package/dist/plugin/testing.d.ts.map +1 -1
  272. package/dist/plugin/testing.js +52 -10
  273. package/dist/plugin/testing.js.map +1 -1
  274. package/dist/plugin.d.ts +1 -1
  275. package/dist/plugin.d.ts.map +1 -1
  276. package/dist/plugin.js +1 -1
  277. package/dist/plugin.js.map +1 -1
  278. package/dist/records.d.ts +283 -0
  279. package/dist/records.d.ts.map +1 -0
  280. package/dist/records.js +543 -0
  281. package/dist/records.js.map +1 -0
  282. package/dist/router.d.ts +50 -3
  283. package/dist/router.d.ts.map +1 -1
  284. package/dist/router.js +78 -7
  285. package/dist/router.js.map +1 -1
  286. package/dist/routes.d.ts +12 -2
  287. package/dist/routes.d.ts.map +1 -1
  288. package/dist/routes.js +14 -1
  289. package/dist/routes.js.map +1 -1
  290. package/dist/rules.d.ts +30 -11
  291. package/dist/rules.d.ts.map +1 -1
  292. package/dist/rules.js +99 -42
  293. package/dist/rules.js.map +1 -1
  294. package/dist/runner/acp.d.ts +20 -0
  295. package/dist/runner/acp.d.ts.map +1 -0
  296. package/dist/runner/acp.js +1132 -0
  297. package/dist/runner/acp.js.map +1 -0
  298. package/dist/runner/claude.d.ts +13 -0
  299. package/dist/runner/claude.d.ts.map +1 -0
  300. package/dist/runner/claude.js +615 -0
  301. package/dist/runner/claude.js.map +1 -0
  302. package/dist/runner/codex.d.ts +19 -0
  303. package/dist/runner/codex.d.ts.map +1 -0
  304. package/dist/runner/codex.js +597 -0
  305. package/dist/runner/codex.js.map +1 -0
  306. package/dist/runner/config.d.ts +35 -0
  307. package/dist/runner/config.d.ts.map +1 -0
  308. package/dist/runner/config.js +74 -0
  309. package/dist/runner/config.js.map +1 -0
  310. package/dist/runner/desk.d.ts +81 -0
  311. package/dist/runner/desk.d.ts.map +1 -0
  312. package/dist/runner/desk.js +303 -0
  313. package/dist/runner/desk.js.map +1 -0
  314. package/dist/runner/launchd.d.ts +7 -0
  315. package/dist/runner/launchd.d.ts.map +1 -0
  316. package/dist/runner/launchd.js +78 -0
  317. package/dist/runner/launchd.js.map +1 -0
  318. package/dist/runner/message.d.ts +66 -0
  319. package/dist/runner/message.d.ts.map +1 -0
  320. package/dist/runner/message.js +181 -0
  321. package/dist/runner/message.js.map +1 -0
  322. package/dist/runner/modules.d.ts +13 -0
  323. package/dist/runner/modules.d.ts.map +1 -0
  324. package/dist/runner/modules.js +30 -0
  325. package/dist/runner/modules.js.map +1 -0
  326. package/dist/runner/process.d.ts +7 -0
  327. package/dist/runner/process.d.ts.map +1 -0
  328. package/dist/runner/process.js +65 -0
  329. package/dist/runner/process.js.map +1 -0
  330. package/dist/runner/redact.d.ts +48 -0
  331. package/dist/runner/redact.d.ts.map +1 -0
  332. package/dist/runner/redact.js +214 -0
  333. package/dist/runner/redact.js.map +1 -0
  334. package/dist/runner/registry.d.ts +37 -0
  335. package/dist/runner/registry.d.ts.map +1 -0
  336. package/dist/runner/registry.js +26 -0
  337. package/dist/runner/registry.js.map +1 -0
  338. package/dist/runner/runner.d.ts +130 -0
  339. package/dist/runner/runner.d.ts.map +1 -0
  340. package/dist/runner/runner.js +1551 -0
  341. package/dist/runner/runner.js.map +1 -0
  342. package/dist/runner/slack.d.ts +16 -0
  343. package/dist/runner/slack.d.ts.map +1 -0
  344. package/dist/runner/slack.js +157 -0
  345. package/dist/runner/slack.js.map +1 -0
  346. package/dist/runner/systemd-user.d.ts +7 -0
  347. package/dist/runner/systemd-user.d.ts.map +1 -0
  348. package/dist/runner/systemd-user.js +93 -0
  349. package/dist/runner/systemd-user.js.map +1 -0
  350. package/dist/runner/task-scheduler.d.ts +38 -0
  351. package/dist/runner/task-scheduler.d.ts.map +1 -0
  352. package/dist/runner/task-scheduler.js +216 -0
  353. package/dist/runner/task-scheduler.js.map +1 -0
  354. package/dist/runner/types.d.ts +315 -0
  355. package/dist/runner/types.d.ts.map +1 -0
  356. package/dist/runner/types.js +24 -0
  357. package/dist/runner/types.js.map +1 -0
  358. package/dist/runner.d.ts +18 -0
  359. package/dist/runner.d.ts.map +1 -0
  360. package/dist/runner.js +20 -0
  361. package/dist/runner.js.map +1 -0
  362. package/dist/schemas.d.ts +6082 -1295
  363. package/dist/schemas.d.ts.map +1 -1
  364. package/dist/schemas.js +930 -31
  365. package/dist/schemas.js.map +1 -1
  366. package/dist/services.d.ts +110 -0
  367. package/dist/services.d.ts.map +1 -0
  368. package/dist/services.js +305 -0
  369. package/dist/services.js.map +1 -0
  370. package/dist/setup-guide.d.ts +64 -0
  371. package/dist/setup-guide.d.ts.map +1 -0
  372. package/dist/setup-guide.js +256 -0
  373. package/dist/setup-guide.js.map +1 -0
  374. package/dist/setup-page.d.ts +6 -0
  375. package/dist/setup-page.d.ts.map +1 -0
  376. package/dist/setup-page.js +29 -0
  377. package/dist/setup-page.js.map +1 -0
  378. package/dist/sse.d.ts +15 -0
  379. package/dist/sse.d.ts.map +1 -0
  380. package/dist/sse.js +73 -0
  381. package/dist/sse.js.map +1 -0
  382. package/dist/tab-title.d.ts +6 -0
  383. package/dist/tab-title.d.ts.map +1 -0
  384. package/dist/tab-title.js +22 -0
  385. package/dist/tab-title.js.map +1 -0
  386. package/dist/tokens.d.ts.map +1 -1
  387. package/dist/tokens.js +3 -0
  388. package/dist/tokens.js.map +1 -1
  389. package/dist/use-action.d.ts +5 -2
  390. package/dist/use-action.d.ts.map +1 -1
  391. package/dist/use-action.js +6 -1
  392. package/dist/use-action.js.map +1 -1
  393. package/dist/vocabulary.d.ts +84 -4
  394. package/dist/vocabulary.d.ts.map +1 -1
  395. package/dist/vocabulary.js +115 -3
  396. package/dist/vocabulary.js.map +1 -1
  397. package/docs/specs/cli.md +147 -41
  398. package/docs/specs/plugins.md +37 -27
  399. package/docs/specs/records.md +113 -0
  400. package/docs/specs/rest.md +155 -20
  401. package/docs/specs/runner.md +347 -0
  402. package/package.json +12 -3
  403. package/skills/building-a-console/SKILL.md +4 -4
  404. package/skills/building-a-console/references/configuring.md +1 -0
  405. package/skills/building-a-plugin/SKILL.md +7 -7
  406. package/skills/deploying-a-console/SKILL.md +4 -4
  407. package/skills/deploying-a-console/references/resources.md +48 -8
  408. package/skills/deploying-a-console/references/running-it-again.md +3 -2
  409. package/skills/keeping-a-desk/SKILL.md +133 -0
  410. package/skills/keeping-a-desk/collections.md +96 -0
  411. package/skills/planning-the-board/SKILL.md +14 -18
  412. package/skills/planning-the-board/references/a-task.md +2 -2
  413. package/skills/planning-the-board/references/project-rules.md +13 -14
  414. package/skills/planning-the-board/references/setting-up.md +22 -9
  415. package/skills/running-an-agent/SKILL.md +107 -0
  416. package/skills/running-an-agent/references/slack-app.md +61 -0
  417. package/skills/working-the-board/SKILL.md +66 -39
  418. package/skills/working-the-board/references/terms.md +9 -5
  419. package/skills/working-the-board/references/the-pack.md +14 -9
  420. package/skills/working-the-board/references/the-sentence.md +2 -2
  421. package/src/agent-page.tsx +1039 -0
  422. package/src/agent-stream.ts +199 -0
  423. package/src/api.ts +317 -2
  424. package/src/avatar.ts +1506 -0
  425. package/src/charts.ts +250 -0
  426. package/src/cli/cli.ts +3210 -503
  427. package/src/cli/command-words.ts +56 -0
  428. package/src/cli/credentials.ts +188 -16
  429. package/src/cli/directory.ts +28 -10
  430. package/src/cli/format.ts +470 -67
  431. package/src/cli/help.ts +233 -61
  432. package/src/cli/main.ts +89 -0
  433. package/src/cli/pack.ts +332 -263
  434. package/src/cli/plugin-check.ts +275 -34
  435. package/src/cli/plugin-cli.ts +28 -4
  436. package/src/cli/plugin-main.ts +1 -0
  437. package/src/cli/runner-host.ts +188 -0
  438. package/src/cli/serve.ts +320 -5
  439. package/src/cli/setup-machine.ts +153 -0
  440. package/src/client.ts +711 -126
  441. package/src/compat.ts +2 -2
  442. package/src/components/agent-page.tsx +1946 -0
  443. package/src/components/agents.tsx +638 -0
  444. package/src/components/calendar.tsx +74 -53
  445. package/src/components/charts.tsx +680 -0
  446. package/src/components/comments.tsx +3 -0
  447. package/src/components/connect.tsx +37 -2
  448. package/src/components/decision-form.tsx +89 -51
  449. package/src/components/decision-list.tsx +140 -4
  450. package/src/components/desk-records.tsx +724 -0
  451. package/src/components/desk.tsx +471 -0
  452. package/src/components/event-feed.tsx +30 -1
  453. package/src/components/home.tsx +353 -12
  454. package/src/components/my-decisions.tsx +8 -2
  455. package/src/components/people.tsx +300 -203
  456. package/src/components/picker.tsx +3 -1
  457. package/src/components/pictures.tsx +256 -44
  458. package/src/components/plugin-events.ts +159 -0
  459. package/src/components/plugin-frame.tsx +14 -38
  460. package/src/components/plugin-settings.tsx +178 -22
  461. package/src/components/projects.tsx +33 -15
  462. package/src/components/rules.tsx +96 -36
  463. package/src/components/sessions.tsx +7 -4
  464. package/src/components/setup.tsx +252 -0
  465. package/src/components/sign-in.tsx +1 -1
  466. package/src/components/task-form.tsx +11 -2
  467. package/src/components/task-list.tsx +7 -6
  468. package/src/components/task-view.tsx +4 -0
  469. package/src/components/ui.tsx +169 -25
  470. package/src/components/work.tsx +9 -9
  471. package/src/components/workspace.tsx +345 -4
  472. package/src/connect-page.tsx +15 -2
  473. package/src/console.tsx +657 -83
  474. package/src/data.ts +109 -43
  475. package/src/desk-page.tsx +332 -0
  476. package/src/desk-records-page.tsx +256 -0
  477. package/src/desk-records.ts +808 -0
  478. package/src/desk.ts +329 -0
  479. package/src/feed-stream.ts +144 -0
  480. package/src/glances.ts +132 -0
  481. package/src/home-tiles.ts +411 -9
  482. package/src/i18n/en.ts +828 -10
  483. package/src/i18n/ko.ts +792 -10
  484. package/src/index.ts +172 -3
  485. package/src/limits.ts +67 -0
  486. package/src/onboarding.ts +84 -0
  487. package/src/persona.ts +100 -0
  488. package/src/plugin/context.ts +55 -12
  489. package/src/plugin/limits.ts +2 -0
  490. package/src/plugin/manifest.ts +42 -3
  491. package/src/plugin/rest-schemas.ts +46 -0
  492. package/src/plugin/sandbox.ts +4 -1
  493. package/src/plugin/testing.ts +63 -12
  494. package/src/plugin.ts +2 -0
  495. package/src/records.ts +702 -0
  496. package/src/router.ts +120 -10
  497. package/src/routes.ts +17 -1
  498. package/src/rules.ts +109 -44
  499. package/src/runner/acp.ts +1267 -0
  500. package/src/runner/claude.ts +695 -0
  501. package/src/runner/codex.ts +691 -0
  502. package/src/runner/config.ts +97 -0
  503. package/src/runner/desk.ts +377 -0
  504. package/src/runner/launchd.ts +86 -0
  505. package/src/runner/message.ts +232 -0
  506. package/src/runner/modules.ts +30 -0
  507. package/src/runner/process.ts +72 -0
  508. package/src/runner/redact.ts +242 -0
  509. package/src/runner/registry.ts +67 -0
  510. package/src/runner/runner.ts +1879 -0
  511. package/src/runner/slack.ts +180 -0
  512. package/src/runner/systemd-user.ts +103 -0
  513. package/src/runner/task-scheduler.ts +236 -0
  514. package/src/runner/types.ts +334 -0
  515. package/src/runner.ts +110 -0
  516. package/src/schemas.ts +1183 -30
  517. package/src/services.ts +424 -0
  518. package/src/setup-guide.ts +336 -0
  519. package/src/setup-page.tsx +46 -0
  520. package/src/sse.ts +84 -0
  521. package/src/styles/README.md +111 -0
  522. package/src/styles/base.css +130 -0
  523. package/src/styles/console.css +85 -7095
  524. package/src/styles/drawn-by-pages.json +256 -0
  525. package/src/styles/pages/agents.css +1195 -0
  526. package/src/styles/pages/board.css +380 -0
  527. package/src/styles/pages/calendar.css +214 -0
  528. package/src/styles/pages/connect.css +182 -0
  529. package/src/styles/pages/decisions.css +370 -0
  530. package/src/styles/pages/desk.css +126 -0
  531. package/src/styles/pages/documents.css +273 -0
  532. package/src/styles/pages/feed.css +118 -0
  533. package/src/styles/pages/home.css +718 -0
  534. package/src/styles/pages/people.css +90 -0
  535. package/src/styles/pages/plugins.css +212 -0
  536. package/src/styles/pages/roadmap.css +377 -0
  537. package/src/styles/pages/settings.css +171 -0
  538. package/src/styles/pages/shell.css +208 -0
  539. package/src/styles/pages/sign-in.css +178 -0
  540. package/src/styles/pages/task.css +108 -0
  541. package/src/styles/pages/thread.css +331 -0
  542. package/src/styles/pages/work.css +68 -0
  543. package/src/styles/parts/avatar.css +82 -0
  544. package/src/styles/parts/badge.css +227 -0
  545. package/src/styles/parts/bars.css +80 -0
  546. package/src/styles/parts/button.css +99 -0
  547. package/src/styles/parts/calendar.css +547 -0
  548. package/src/styles/parts/callout.css +41 -0
  549. package/src/styles/parts/category.css +8 -0
  550. package/src/styles/parts/chart.css +173 -0
  551. package/src/styles/parts/chip.css +90 -0
  552. package/src/styles/parts/code.css +48 -0
  553. package/src/styles/parts/count.css +27 -0
  554. package/src/styles/parts/crumbs.css +61 -0
  555. package/src/styles/parts/detail.css +110 -0
  556. package/src/styles/parts/empty.css +21 -0
  557. package/src/styles/parts/eyebrow.css +9 -0
  558. package/src/styles/parts/facts.css +147 -0
  559. package/src/styles/parts/figures.css +65 -0
  560. package/src/styles/parts/files.css +38 -0
  561. package/src/styles/parts/filters.css +42 -0
  562. package/src/styles/parts/form.css +109 -0
  563. package/src/styles/parts/frame.css +45 -0
  564. package/src/styles/parts/input.css +243 -0
  565. package/src/styles/parts/legend.css +48 -0
  566. package/src/styles/parts/links.css +37 -0
  567. package/src/styles/parts/mark.css +79 -0
  568. package/src/styles/parts/menu.css +147 -0
  569. package/src/styles/parts/page.css +174 -0
  570. package/src/styles/parts/pager.css +19 -0
  571. package/src/styles/parts/panel.css +33 -0
  572. package/src/styles/parts/person.css +257 -0
  573. package/src/styles/parts/picker.css +185 -0
  574. package/src/styles/parts/progress.css +25 -0
  575. package/src/styles/parts/prose.css +128 -0
  576. package/src/styles/parts/rows.css +63 -0
  577. package/src/styles/parts/series.css +32 -0
  578. package/src/styles/parts/settings.css +101 -0
  579. package/src/styles/parts/share.css +25 -0
  580. package/src/styles/parts/spark.css +23 -0
  581. package/src/styles/parts/spinner.css +29 -0
  582. package/src/styles/parts/stat.css +69 -0
  583. package/src/styles/parts/steps.css +68 -0
  584. package/src/styles/parts/tabs.css +175 -0
  585. package/src/styles/parts/text.css +61 -0
  586. package/src/styles/parts/toggle.css +71 -0
  587. package/src/styles/parts/workers.css +37 -0
  588. package/src/styles/tokens.css +295 -0
  589. package/src/tab-title.ts +25 -0
  590. package/src/tokens.ts +3 -0
  591. package/src/use-action.ts +7 -2
  592. package/src/vocabulary.ts +151 -3
  593. package/template/desk-web/README.md +8 -0
  594. package/template/desk-web/index.html +13 -0
  595. package/template/desk-web/package.json +25 -0
  596. package/template/desk-web/src/main.tsx +24 -0
  597. package/template/desk-web/src/page.tsx +51 -0
  598. package/template/desk-web/tsconfig.json +16 -0
  599. package/template/desk-web/vite.config.ts +15 -0
@@ -0,0 +1,1551 @@
1
+ import { ApiError } from "../client.js";
2
+ import { MAX_RUNNER_LINE_LENGTH, MAX_RUNNER_OFFERS, MAX_TRANSCRIPT_ATTACHMENTS, MAX_TRANSCRIPT_BATCH, MAX_TRANSCRIPT_BATCH_BYTES, MAX_TRANSCRIPT_FACT_LENGTH, MAX_TRANSCRIPT_FACT_NAME_LENGTH, MAX_TRANSCRIPT_FACTS, MAX_TRANSCRIPT_TEXT_BYTES, } from "../limits.js";
3
+ import { RUNNER_CHOICE } from "../schemas.js";
4
+ import { hasForbiddenCodePoint } from "../text.js";
5
+ import { composeSignals, deskName, GROUP_WINDOW_MS, introOf, makeMark, } from "./message.js";
6
+ import { cutShown, cutShownEnd, redactorOf } from "./redact.js";
7
+ // The runner (ADR-0085, docs/specs/runner.md): one process per agent account on its machine. It
8
+ // keeps one session of the agent's product at a time, feeds it what the console sends, reports
9
+ // what the product does, and says every thirty seconds that it is there. It restarts nothing by
10
+ // itself: an agent that cannot go on waits, and a supervisor's signal is what is tried next.
11
+ // The one thing it ends for by itself is an update of the command it runs from (ADR-0101): when
12
+ // no turn is under way, for the service that keeps it to start it again with the new one.
13
+ //
14
+ // Everything that changes what the runner holds goes through one queue, handled one at a time:
15
+ // a signal received, a word of the driver's, a timer that fired. What the product prints is
16
+ // numbered and kept for the next batch as it arrives, so that nothing of a session is left
17
+ // behind when the session ends.
18
+ /** How often the runner says it is there. */
19
+ export const HEARTBEAT_MS = 30_000;
20
+ /** How often what the product did is sent, while there is something to send. */
21
+ export const BATCH_MS = 1000;
22
+ /** How long what arrives by itself waits after the agent could not go on: at first, and at most, twice as long after each try that ends the same way. */
23
+ export const COOL_DOWN_MS = 15 * 60_000;
24
+ export const MAX_COOL_DOWN_MS = 4 * 60 * 60_000;
25
+ /** How long the runner waits before it asks for its stream of signals again. */
26
+ export const RECONNECT_MS = 5000;
27
+ /** What an autosave's commit says: the runner's, and never the agent's own words. */
28
+ export const AUTOSAVE_MESSAGE = "Autosave at the end of a session";
29
+ /**
30
+ * How long the runner holds back what the console refused as more than an hour takes
31
+ * (ADR-0132), the events of a session and the wakes of its wake sources: a minute at first,
32
+ * twice as long after each refusal that follows, a quarter of an hour at most.
33
+ */
34
+ export const HOLD_MS = 60_000;
35
+ export const MAX_HOLD_MS = 15 * 60_000;
36
+ /** The longest the runner waits before it starts a wake source again that keeps ending at once. */
37
+ export const MAX_SOURCE_PAUSE_MS = 5 * 60_000;
38
+ /** How long a wake source holds before the runner takes it to have worked. */
39
+ const SOURCE_HELD_MS = 60_000;
40
+ /** The kinds of file a console keeps as a picture: what a product is handed as one. */
41
+ const PICTURE_TYPES = new Set([
42
+ "image/png",
43
+ "image/jpeg",
44
+ "image/gif",
45
+ "image/webp",
46
+ ]);
47
+ /**
48
+ * How many events wait to be sent, and how many bytes of them, their pictures counted, before
49
+ * the oldest are let go: a console away for hours is not kept up with in memory. What is let go
50
+ * is said on the record, in the place of the first of them.
51
+ */
52
+ const MAX_OUTBOX = 20_000;
53
+ export const MAX_OUTBOX_BYTES = 64 * 1024 * 1024;
54
+ /** Room left in a batch for what wraps its events. */
55
+ const BATCH_ROOM_BYTES = MAX_TRANSCRIPT_BATCH_BYTES - 64 * 1024;
56
+ /** What the id of a picture takes of a batch, with its quotes and its comma. */
57
+ const FILE_ID_BYTES = 48;
58
+ /** The fewest bytes a text is cut down to where the console refuses its event alone as too large. */
59
+ const LEAST_CUT_BYTES = 1024;
60
+ /** The name of the secret an account keeps its git host's token under (ADR-0088): what the autosave's push is handed. */
61
+ export const GIT_TOKEN_SECRET = "git.token";
62
+ /** What an event takes of a batch as the console counts one: its JSON, and the ids of its pictures. */
63
+ const sentBytes = (event) => Buffer.byteLength(JSON.stringify({
64
+ n: event.n,
65
+ kind: event.kind,
66
+ at: event.at,
67
+ text: event.text,
68
+ data: event.data,
69
+ })) +
70
+ event.pictures.length * FILE_ID_BYTES +
71
+ 16;
72
+ /** What an event holds of memory: its text, its facts roughly, and the bytes of its pictures. */
73
+ const heldBytes = (event) => Buffer.byteLength(event.text) +
74
+ 1024 +
75
+ event.pictures.reduce((sum, picture) => sum + picture.bytes.byteLength, 0);
76
+ /** The word a text the runner cut ends with: how long it was whole. */
77
+ const CUT_NOTE = /\n\[cut by the runner: (\d+) bytes in all\]$/;
78
+ /**
79
+ * A text within `bytes` of UTF-8, cut between characters, with a word that says it was cut. A
80
+ * text cut before and cut again still says how long it was whole.
81
+ */
82
+ function boundedText(text, bytes) {
83
+ if (text.length * 3 <= bytes) {
84
+ return text;
85
+ }
86
+ const encoded = Buffer.from(text, "utf8");
87
+ if (encoded.byteLength <= bytes) {
88
+ return text;
89
+ }
90
+ const before = CUT_NOTE.exec(text);
91
+ const whole = before === null ? encoded.byteLength : Number(before[1]);
92
+ const body = before === null ? encoded : Buffer.from(text.slice(0, before.index), "utf8");
93
+ const note = `\n[cut by the runner: ${whole} bytes in all]`;
94
+ let end = Math.min(body.byteLength, Math.max(0, bytes - Buffer.byteLength(note)));
95
+ while (end > 0 && ((body[end] ?? 0) & 0xc0) === 0x80) {
96
+ end -= 1;
97
+ }
98
+ return `${body.subarray(0, end).toString("utf8")}${note}`;
99
+ }
100
+ /** The facts of a block within the bounds the console takes them in, a text among them with its secrets taken out. */
101
+ function boundedFacts(data, redact) {
102
+ const facts = {};
103
+ for (const [name, fact] of Object.entries(data ?? {}).slice(0, MAX_TRANSCRIPT_FACTS)) {
104
+ if (name.length === 0 || name.length > MAX_TRANSCRIPT_FACT_NAME_LENGTH) {
105
+ continue;
106
+ }
107
+ if (typeof fact === "string") {
108
+ facts[name] = redact(fact).slice(0, MAX_TRANSCRIPT_FACT_LENGTH);
109
+ }
110
+ else if (typeof fact !== "number" || Number.isFinite(fact)) {
111
+ facts[name] = fact;
112
+ }
113
+ }
114
+ return facts;
115
+ }
116
+ /** A model or a level of effort as the console takes one, or nothing for a name it would refuse. */
117
+ const choiceOf = (value) => typeof value === "string" && value.length <= MAX_RUNNER_LINE_LENGTH && RUNNER_CHOICE.test(value)
118
+ ? value
119
+ : null;
120
+ /** Of what a product offers to choose from, what the console takes: the names it would refuse left out, the most it takes. */
121
+ const offeredOf = (values) => values.filter((value) => choiceOf(value) !== null).slice(0, MAX_RUNNER_OFFERS);
122
+ /** A line a runner says of its product as the console takes one, or nothing where it is none. */
123
+ const lineOf = (value) => {
124
+ const line = (value ?? "").trim().slice(0, MAX_RUNNER_LINE_LENGTH).trim();
125
+ return line.length === 0 || hasForbiddenCodePoint(line) ? null : line;
126
+ };
127
+ export function createRunner(options) {
128
+ const { client, driver, config, log } = options;
129
+ const timing = {
130
+ heartbeatMs: HEARTBEAT_MS,
131
+ batchMs: BATCH_MS,
132
+ groupWindowMs: GROUP_WINDOW_MS,
133
+ coolDownMs: COOL_DOWN_MS,
134
+ maxCoolDownMs: MAX_COOL_DOWN_MS,
135
+ reconnectMs: RECONNECT_MS,
136
+ holdMs: HOLD_MS,
137
+ maxHoldMs: MAX_HOLD_MS,
138
+ ...options.timing,
139
+ };
140
+ /** How long what an hour's bound refused is held back: the first time, and twice as long each time after. */
141
+ const heldFor = (before) => before === undefined ? timing.holdMs : Math.min(before.pause * 2, timing.maxHoldMs);
142
+ const bounds = { outboxBytes: MAX_OUTBOX_BYTES, ...options.bounds };
143
+ // What keeps failing is said once, and again when what is said of it changes, so that an hour
144
+ // without the console is a line and not thousands; that it is over is said once too.
145
+ const saidOf = new Map();
146
+ const sayOnce = (about, line) => {
147
+ if (saidOf.get(about) !== line) {
148
+ saidOf.set(about, line);
149
+ log(line);
150
+ }
151
+ };
152
+ const sayOver = (about, line) => {
153
+ if (saidOf.delete(about) && line !== undefined) {
154
+ log(line);
155
+ }
156
+ };
157
+ // The queue: everything that changes what the runner holds, one at a time.
158
+ const queue = [];
159
+ let wake;
160
+ const push = (message) => {
161
+ queue.push(message);
162
+ wake?.();
163
+ };
164
+ const next = async () => {
165
+ for (;;) {
166
+ const message = queue.shift();
167
+ if (message !== undefined) {
168
+ return message;
169
+ }
170
+ await new Promise((resolve) => {
171
+ wake = resolve;
172
+ });
173
+ wake = undefined;
174
+ }
175
+ };
176
+ let stopped = false;
177
+ const abort = new AbortController();
178
+ const timers = new Set();
179
+ const later = (ms, message) => {
180
+ const timer = setTimeout(() => {
181
+ timers.delete(timer);
182
+ push(message);
183
+ }, ms);
184
+ timers.add(timer);
185
+ };
186
+ // What the runner holds.
187
+ /** The number of the last signal delivered, every one before it too: what is kept and said to the console. */
188
+ let delivered = 0;
189
+ /** The signals handed to the product that it has not taken yet, by number: not delivered, and not sent twice. */
190
+ const owed = new Set();
191
+ /** The highest number of a signal the product took, or the record says was never read or let go. */
192
+ let settled = 0;
193
+ /** The number of the last signal the stream gave, and of the last one acted on: the second is behind the first while one waits in the queue. */
194
+ let received = 0;
195
+ let handled = 0;
196
+ let conversation;
197
+ let stands = {
198
+ state: "running",
199
+ reason: "started",
200
+ };
201
+ let said = "";
202
+ let settings = { model: null, effort: null };
203
+ let probe = { version: undefined, models: [], efforts: [] };
204
+ let productVersion = null;
205
+ let session;
206
+ /** What was received and not handed to the product yet, in order. */
207
+ let pending = [];
208
+ /** A supervisor's stop stands: nothing is delivered until a restart or a reset. */
209
+ let held = false;
210
+ /** The agent could not go on: how many tries ended so, and when what arrives by itself goes next. */
211
+ let waits;
212
+ /** The next session begins a new conversation, and says who reset it. */
213
+ let resetBy;
214
+ /** The open session is to end as soon as no turn is under way: what was chosen for it is taken when one starts. */
215
+ let renew = false;
216
+ let deliverAt;
217
+ /** The number of the last signal the runner acted on itself. */
218
+ let acted = 0;
219
+ let flushing = false;
220
+ /** The values of the account's store, by what the store said of them last. */
221
+ let secretsStamp;
222
+ let secretValues = new Map();
223
+ let redact = redactorOf([]);
224
+ let tooOldSaid = false;
225
+ /** The console took as many wakes of this runner's as an hour takes: until when none is raised, and how long that was. */
226
+ let wakesHeld;
227
+ /**
228
+ * The package the runner runs from, where it is another version than the runner's: the one
229
+ * last seen, and the one seen twice running, which the runner ends for.
230
+ */
231
+ let seenVersion;
232
+ let seenAt = 0;
233
+ let updatedTo;
234
+ /**
235
+ * The signals a product was handed and never took, since it did not start or was not signed
236
+ * in, which are kept to be sent again; and, of those, the ones a reset gave one more try: a
237
+ * product that does not start with them again is not sent them a third time.
238
+ */
239
+ const sentBefore = new Set();
240
+ const lastTry = new Set();
241
+ const persist = async () => {
242
+ try {
243
+ await options.state.save({ delivered, conversation, driver: driver.name });
244
+ }
245
+ catch {
246
+ log("the runner's state could not be written; it goes on from the console's record");
247
+ }
248
+ };
249
+ const setState = (state, reason) => {
250
+ stands = { state, reason };
251
+ push({ t: "heartbeat" });
252
+ };
253
+ /** Asks for a delivery after the window that groups what arrives together, or at a moment. */
254
+ const scheduleDeliver = (at = options.now() + timing.groupWindowMs) => {
255
+ if (deliverAt !== undefined && deliverAt <= at) {
256
+ return;
257
+ }
258
+ deliverAt = at;
259
+ later(Math.max(0, at - options.now()), { t: "deliver" });
260
+ };
261
+ const record = (target, kind, text, data, pictures = []) => {
262
+ target.n += 1;
263
+ const event = {
264
+ n: target.n,
265
+ kind,
266
+ at: new Date(options.now()).toISOString(),
267
+ text: boundedText(redact(text), MAX_TRANSCRIPT_TEXT_BYTES),
268
+ data: boundedFacts(data, redact),
269
+ pictures: pictures.slice(0, MAX_TRANSCRIPT_ATTACHMENTS),
270
+ };
271
+ const held = heldBytes(event);
272
+ target.outbox.push({ ...event, held });
273
+ target.waiting += held;
274
+ // A batch on its way names the events at the head: they are let go, where they must be,
275
+ // before the next one is put together.
276
+ if (!flushing) {
277
+ trim(target);
278
+ }
279
+ };
280
+ /**
281
+ * Lets go of the oldest of what waits to be sent once more waits than the runner keeps, and
282
+ * says so on the record: one event stands in the place of the first let go, with how many
283
+ * went, and those after it are numbered on from it, so that the console is sent no gap.
284
+ */
285
+ const trim = (target) => {
286
+ if (target.outbox.length <= MAX_OUTBOX && target.waiting <= bounds.outboxBytes) {
287
+ return;
288
+ }
289
+ let first;
290
+ let gone = 0;
291
+ // Down to nine tenths of each bound, so that the next event does not find it full again.
292
+ while (target.outbox.length > 1 &&
293
+ (target.outbox.length > MAX_OUTBOX * 0.9 || target.waiting > bounds.outboxBytes * 0.9)) {
294
+ const event = target.outbox.shift();
295
+ if (event === undefined) {
296
+ break;
297
+ }
298
+ first ??= event;
299
+ target.waiting -= event.held;
300
+ gone += event.gone ?? 1;
301
+ }
302
+ if (first === undefined) {
303
+ return;
304
+ }
305
+ const said = {
306
+ text: `The runner let go of ${gone} ${gone === 1 ? "event" : "events"} of this session unsent: more waited for the console than the runner keeps.`,
307
+ pictures: [],
308
+ };
309
+ const held = heldBytes(said);
310
+ target.outbox.unshift({
311
+ n: first.n,
312
+ kind: "status",
313
+ at: first.at,
314
+ ...said,
315
+ data: { let_go: gone },
316
+ held,
317
+ gone,
318
+ });
319
+ target.waiting += held;
320
+ target.outbox.forEach((event, index) => {
321
+ event.n = first.n + index;
322
+ });
323
+ target.n = first.n + target.outbox.length - 1;
324
+ sayOnce(`outbox ${target.id}`, `events of session ${target.id} are let go unsent: more waits for the console than the runner keeps`);
325
+ };
326
+ /** Reads the values of the account's store where the store changed, for what a session is handed and for what is taken out of events. */
327
+ const readSecrets = async () => {
328
+ try {
329
+ const listed = await client.secrets(abort.signal);
330
+ const stamp = JSON.stringify(listed.items.map((item) => [item.name, item.setAt]));
331
+ if (stamp === secretsStamp) {
332
+ return;
333
+ }
334
+ const values = new Map();
335
+ for (const item of listed.items) {
336
+ values.set(item.name, (await client.secret(item.name, abort.signal)).value);
337
+ }
338
+ secretValues = values;
339
+ secretsStamp = stamp;
340
+ redact = redactorOf(values.values());
341
+ }
342
+ catch (error) {
343
+ if (!stopped) {
344
+ log(`the account's secrets could not be read (${codeOf(error)})`);
345
+ }
346
+ }
347
+ };
348
+ const codeOf = (error) => error instanceof ApiError ? error.code : error instanceof Error ? error.name : "error";
349
+ /**
350
+ * Sends what the product did, a batch at a time, the pictures first. What the console has
351
+ * already is left there. Nothing is sent while the console holds the session to its hour
352
+ * (ADR-0132), but at the session's end, which is `last`.
353
+ */
354
+ const flush = async (target, last = false) => {
355
+ if (flushing) {
356
+ return;
357
+ }
358
+ if (!last && target.hold !== undefined && options.now() < target.hold.until) {
359
+ return;
360
+ }
361
+ flushing = true;
362
+ const about = `transcript ${target.id}`;
363
+ try {
364
+ // The most events a batch carries: fewer once the console refused one as too large.
365
+ let most = MAX_TRANSCRIPT_BATCH;
366
+ while (target.outbox.length > 0 && !target.refused) {
367
+ trim(target);
368
+ const batch = [];
369
+ let bytes = 0;
370
+ for (const event of target.outbox) {
371
+ // Sized as it is sent: what a text grows by when it is spelled as JSON is counted.
372
+ const size = sentBytes(event);
373
+ if (batch.length >= most || (batch.length > 0 && bytes + size > BATCH_ROOM_BYTES)) {
374
+ break;
375
+ }
376
+ batch.push(event);
377
+ bytes += size;
378
+ }
379
+ const events = [];
380
+ for (const event of batch) {
381
+ if (event.files === undefined) {
382
+ const files = [];
383
+ for (const picture of event.pictures) {
384
+ try {
385
+ files.push((await client.uploadTranscriptFile(target.id, picture)).id);
386
+ }
387
+ catch (error) {
388
+ if (error instanceof ApiError && error.status === 0) {
389
+ throw error;
390
+ }
391
+ log(`a picture of session ${target.id} was not kept (${codeOf(error)})`);
392
+ }
393
+ }
394
+ event.files = files;
395
+ }
396
+ const files = event.files;
397
+ events.push({
398
+ n: event.n,
399
+ kind: event.kind,
400
+ at: event.at,
401
+ text: event.text,
402
+ data: event.data,
403
+ ...(files.length === 0 ? {} : { files: [...files] }),
404
+ });
405
+ }
406
+ try {
407
+ await client.sendTranscript(target.id, { events });
408
+ for (const event of target.outbox.splice(0, batch.length)) {
409
+ target.waiting -= event.held;
410
+ }
411
+ target.hold = undefined;
412
+ target.unsent = undefined;
413
+ sayOver(about, `the transcript of session ${target.id} is sent again`);
414
+ sayOver(`outbox ${target.id}`);
415
+ }
416
+ catch (error) {
417
+ const code = codeOf(error);
418
+ if (code === "transcript.gap") {
419
+ // The console is behind what was sent: it says where it stands, and the rest goes on from there.
420
+ const last = (await client.agentSession("me", target.id, abort.signal)).events;
421
+ target.outbox = target.outbox.filter((event) => event.n > last);
422
+ target.outbox.forEach((event, index) => {
423
+ event.n = last + 1 + index;
424
+ });
425
+ target.n = last + target.outbox.length;
426
+ target.waiting = target.outbox.reduce((sum, event) => sum + event.held, 0);
427
+ continue;
428
+ }
429
+ if (code === "request.too_large") {
430
+ // More than the console takes in one request, whatever the runner counted: sent
431
+ // again in halves, and never the same batch twice. One event alone that is still
432
+ // too large is cut, with a word that says so, then sent without its facts, then
433
+ // let go: nothing waits behind it for ever.
434
+ const [only] = batch;
435
+ if (batch.length > 1 || only === undefined) {
436
+ most = Math.max(1, Math.floor(batch.length / 2));
437
+ continue;
438
+ }
439
+ const before = only.held;
440
+ const size = Buffer.byteLength(only.text);
441
+ if (size > LEAST_CUT_BYTES) {
442
+ only.text = boundedText(only.text, Math.floor(size / 2));
443
+ }
444
+ else if (Object.keys(only.data).length > 0) {
445
+ only.data = {};
446
+ }
447
+ else {
448
+ target.outbox.shift();
449
+ target.outbox.forEach((event) => {
450
+ event.n -= 1;
451
+ });
452
+ target.n -= 1;
453
+ target.waiting -= before;
454
+ log(`an event of session ${target.id} was let go: the console refused it alone as too large`);
455
+ continue;
456
+ }
457
+ only.held = heldBytes(only);
458
+ target.waiting += only.held - before;
459
+ continue;
460
+ }
461
+ if (code === "transcript.too_many") {
462
+ // As many events as the console takes of a session within an hour: what waits is
463
+ // kept, within what the runner keeps, and sent again after a while, longer each
464
+ // time, so that the console is not asked every second for what it will refuse.
465
+ const pause = heldFor(target.hold);
466
+ target.hold = { until: options.now() + pause, pause };
467
+ target.unsent = code;
468
+ // Nothing is kept past the session's end: what is let go then is said there.
469
+ if (!last) {
470
+ sayOnce(about, `the console takes no more events of session ${target.id} within the hour (${code}); they are kept and sent again later`);
471
+ }
472
+ return;
473
+ }
474
+ if (code === "transcript.session_over" ||
475
+ code === "transcript.session_not_found" ||
476
+ code === "transcript.away") {
477
+ log(`the console takes nothing more of session ${target.id} (${code})`);
478
+ target.refused = true;
479
+ target.outbox = [];
480
+ target.waiting = 0;
481
+ return;
482
+ }
483
+ throw error;
484
+ }
485
+ }
486
+ }
487
+ catch (error) {
488
+ // Kept, and sent with the next batch. Said once for as long as it lasts, and when it is
489
+ // over; at the session's end there is no next batch, and what is let go is said there.
490
+ target.unsent = codeOf(error);
491
+ if (!stopped && !last) {
492
+ sayOnce(about, `the transcript of session ${target.id} waits to be sent (${codeOf(error)})`);
493
+ }
494
+ }
495
+ finally {
496
+ flushing = false;
497
+ }
498
+ };
499
+ /** Reads what the product does: the blocks are numbered and kept as they arrive, its own words go to the queue. */
500
+ const pump = async (target) => {
501
+ try {
502
+ for await (const event of target.driver.events) {
503
+ if (event.type === "event") {
504
+ record(target, event.kind, event.text, event.data, event.pictures);
505
+ }
506
+ else {
507
+ push({ t: "driver", session: target, event });
508
+ }
509
+ }
510
+ }
511
+ catch (error) {
512
+ log(`the product's output could not be read (${codeOf(error)})`);
513
+ push({ t: "driver", session: target, event: { type: "ended", how: "crashed" } });
514
+ }
515
+ };
516
+ /** What a session is handed of the account's store: as its environment, and as files under `work/`. */
517
+ const handOver = async () => {
518
+ const env = {};
519
+ const written = [];
520
+ for (const [variable, name] of Object.entries(config.secrets.env)) {
521
+ const value = secretValues.get(name);
522
+ if (value === undefined) {
523
+ log(`runner.json names the secret ${name}, which the account's store does not hold`);
524
+ }
525
+ else {
526
+ env[variable] = value;
527
+ }
528
+ }
529
+ for (const [path, name] of Object.entries(config.secrets.files)) {
530
+ const value = secretValues.get(name);
531
+ if (value === undefined) {
532
+ log(`runner.json names the secret ${name}, which the account's store does not hold`);
533
+ continue;
534
+ }
535
+ await options.files.write(path, value);
536
+ written.push(path);
537
+ }
538
+ return { env, written };
539
+ };
540
+ /** Begins a session: the product started, and the console told. Nothing where either could not be. */
541
+ const begin = async () => {
542
+ await readSecrets();
543
+ const resume = driver.can.resume ? conversation : undefined;
544
+ let handed;
545
+ try {
546
+ handed = await handOver();
547
+ }
548
+ catch (error) {
549
+ log(`a session's files could not be written (${codeOf(error)})`);
550
+ setState("failed", "start");
551
+ return undefined;
552
+ }
553
+ let created;
554
+ try {
555
+ created = await client.beginAgentSession({
556
+ product: productOf(),
557
+ model: settings.model,
558
+ effort: settings.effort,
559
+ resumed: resume !== undefined,
560
+ machine: options.machine,
561
+ });
562
+ }
563
+ catch (error) {
564
+ const code = codeOf(error);
565
+ log(code === "transcript.too_many_sessions"
566
+ ? `the console begins no more sessions of this account within the hour (${code}); tried again after the cool-down`
567
+ : `a session could not be begun at the console (${code})`);
568
+ for (const path of handed.written) {
569
+ await options.files.remove(path).catch(() => undefined);
570
+ }
571
+ return undefined;
572
+ }
573
+ const started = {
574
+ id: created.id,
575
+ mark: makeMark(options.random),
576
+ driver: driver.start({
577
+ cwd: options.cwd,
578
+ env: handed.env,
579
+ model: settings.model,
580
+ effort: settings.effort,
581
+ resume,
582
+ permissions: config.permissions,
583
+ // Whatever the driver shows a part of is cut once the secrets, as the store has them
584
+ // now, are out of it: no part of one stands where a text was cut.
585
+ cut: (text, most, more) => cutShown(redact, text, most, more),
586
+ cutEnd: (text, most, more) => cutShownEnd(redact, text, most, more),
587
+ }),
588
+ startedAt: options.now(),
589
+ first: true,
590
+ turn: false,
591
+ sent: [],
592
+ handed: [],
593
+ took: false,
594
+ n: 0,
595
+ outbox: [],
596
+ waiting: 0,
597
+ refused: false,
598
+ running: { model: settings.model, effort: settings.effort },
599
+ written: handed.written,
600
+ pumped: Promise.resolve(),
601
+ };
602
+ started.pumped = pump(started);
603
+ session = started;
604
+ renew = false;
605
+ log(`session ${started.id} began${resume === undefined ? "" : ", going on with the conversation before"}`);
606
+ push({ t: "heartbeat" });
607
+ return started;
608
+ };
609
+ /** Ends the open session: the product stopped, what is left of it sent, the console told how, its files removed. */
610
+ const end = async (how, already = false) => {
611
+ const target = session;
612
+ if (target === undefined) {
613
+ return;
614
+ }
615
+ session = undefined;
616
+ if (!already) {
617
+ await target.driver.stop().catch(() => undefined);
618
+ }
619
+ await target.pumped;
620
+ await hearLast(target);
621
+ // What the product was handed and never took is sent again, by the console where the
622
+ // runner itself ends and by the runner where the product had not started; else it is on
623
+ // the record as never read.
624
+ await leave(target, stopped ? "after" : already || target.took ? undefined : "next");
625
+ record(target, "status", `The session ended: ${how}.`, { ended: how });
626
+ await flush(target, true);
627
+ // What the console did not take by now goes with the session: nothing of it is sent
628
+ // later, and the runner's line says how many, where an earlier one said they were kept.
629
+ const unsent = target.outbox.length;
630
+ if (unsent > 0) {
631
+ const one = unsent === 1;
632
+ log(`${unsent} ${one ? "event" : "events"} of session ${target.id} ${one ? "was" : "were"} let go unsent when it ended: ${target.unsent === "transcript.too_many"
633
+ ? `the console takes no more of it within the hour (${target.unsent})`
634
+ : `the console did not take ${one ? "it" : "them"} (${target.unsent ?? "error"})`}`);
635
+ }
636
+ saidOf.delete(`transcript ${target.id}`);
637
+ saidOf.delete(`outbox ${target.id}`);
638
+ try {
639
+ await client.endAgentSession(target.id, { how });
640
+ }
641
+ catch (error) {
642
+ log(`the end of session ${target.id} was not told to the console (${codeOf(error)})`);
643
+ }
644
+ for (const path of target.written) {
645
+ await options.files.remove(path).catch(() => undefined);
646
+ }
647
+ log(`session ${target.id} ended: ${how}`);
648
+ await autosave();
649
+ push({ t: "heartbeat" });
650
+ };
651
+ /**
652
+ * The autosave (ADR-0088): the desk committed and pushed at the end of a session, a net under
653
+ * the agent's own commits, where `runner.json` leaves it on and the desk is a repository. A
654
+ * step that does not succeed is said by how git ended, and the runner goes on.
655
+ */
656
+ const autosave = async () => {
657
+ const git = options.git;
658
+ if (!config.autosave || git === undefined) {
659
+ return;
660
+ }
661
+ try {
662
+ if ((await git(["rev-parse", "--is-inside-work-tree"])) !== 0) {
663
+ return;
664
+ }
665
+ // Everything but `work/`, by name: what a supervisor sent and what the runner keeps
666
+ // there stay on the machine, whatever the desk's own list of what git ignores says.
667
+ await git(["add", "--all", "--", ".", ":(exclude)work"]);
668
+ // Ends in 1 where something is staged, and in 0 where nothing is.
669
+ if ((await git(["diff", "--cached", "--quiet"])) === 1) {
670
+ const committed = await git(["commit", "--quiet", "--message", AUTOSAVE_MESSAGE]);
671
+ if (committed !== 0) {
672
+ log(`the desk was not saved: git commit ended in ${committed}`);
673
+ return;
674
+ }
675
+ }
676
+ const pushed = await git(["push", "--quiet"], {
677
+ token: secretValues.get(GIT_TOKEN_SECRET),
678
+ });
679
+ log(pushed === 0
680
+ ? "the desk was saved"
681
+ : `the desk was committed and not pushed: git push ended in ${pushed}`);
682
+ }
683
+ catch (error) {
684
+ log(`the desk was not saved (${codeOf(error)})`);
685
+ }
686
+ };
687
+ /** Hands the product what is pending, as one message; begins a session where none is open. */
688
+ const deliver = async (priority) => {
689
+ deliverAt = undefined;
690
+ // Nothing more is handed over once the runner is to end for an update: what waits is sent
691
+ // again by the console, in order, to the runner started after.
692
+ if (held || updatedTo !== undefined || pending.length === 0) {
693
+ return;
694
+ }
695
+ // An agent that could not go on is tried at once for a supervisor, and after the cool-down for the rest.
696
+ if (waits !== undefined && !pending.some((signal) => signal.source === "person")) {
697
+ if (options.now() < waits.until) {
698
+ scheduleDeliver(waits.until);
699
+ return;
700
+ }
701
+ }
702
+ // A new day or a new week begins a new conversation where `runner.json` says so, when the agent is idle then.
703
+ const anew = pending.some((signal) => signal.kind === "wake" &&
704
+ signal.source === "schedule" &&
705
+ ((config.conversation === "daily" && signal.key === "day") ||
706
+ (config.conversation !== "continuous" && signal.key === "week")));
707
+ if (anew && session?.turn !== true) {
708
+ await end("restarted");
709
+ conversation = undefined;
710
+ }
711
+ const target = session ?? (await begin());
712
+ if (target === undefined) {
713
+ // Tried again when the cool-down ends, or when a supervisor says something.
714
+ waits = cooled(waits);
715
+ scheduleDeliver(waits.until);
716
+ return;
717
+ }
718
+ const batch = pending;
719
+ pending = [];
720
+ // The files a supervisor sent, read from the console and written in the desk, under its
721
+ // ignored work/, for the agent to open; a picture is handed to the product as a picture
722
+ // too, where the product takes one. What could not be handed over is said to the agent in
723
+ // the message, and what a product was not shown as a picture is said on the record.
724
+ const pictures = new Map();
725
+ const attached = new Map();
726
+ const images = [];
727
+ // What the session says of itself where it knows, an agent that said what it takes; else
728
+ // what the driver says of its product.
729
+ const takesPictures = target.driver.images?.() ?? driver.can.images;
730
+ for (const signal of batch) {
731
+ const taken = new Set();
732
+ for (const file of signal.attachments) {
733
+ const picture = PICTURE_TYPES.has(file.contentType);
734
+ let bytes;
735
+ try {
736
+ bytes = (await client.readSignalFile(signal.number, file.id, abort.signal)).bytes;
737
+ }
738
+ catch (error) {
739
+ log(`a file of signal #${signal.number} could not be read (${codeOf(error)})`);
740
+ }
741
+ let path;
742
+ if (bytes !== undefined) {
743
+ const name = deskName(file.name, taken);
744
+ taken.add(name);
745
+ try {
746
+ await options.files.write(`inbox/${signal.number}/${name}`, bytes);
747
+ path = `work/inbox/${signal.number}/${name}`;
748
+ }
749
+ catch (error) {
750
+ log(`a file of signal #${signal.number} could not be written (${codeOf(error)})`);
751
+ }
752
+ }
753
+ // A product that takes a picture as a file takes it by its path in the desk.
754
+ const shown = picture &&
755
+ bytes !== undefined &&
756
+ takesPictures &&
757
+ (driver.can.imageFiles !== true || path !== undefined);
758
+ if (picture && bytes !== undefined) {
759
+ const kept = { name: file.name, contentType: file.contentType, bytes, path };
760
+ pictures.set(signal.number, [...(pictures.get(signal.number) ?? []), kept]);
761
+ if (shown) {
762
+ images.push(kept);
763
+ }
764
+ }
765
+ attached.set(signal.number, [
766
+ ...(attached.get(signal.number) ?? []),
767
+ {
768
+ name: file.name,
769
+ // A picture that could not be written is still handed over where its bytes are taken.
770
+ path: bytes === undefined ? undefined : path,
771
+ contentType: file.contentType,
772
+ size: file.size,
773
+ picture: !picture ? undefined : shown ? "shown" : "unseen",
774
+ },
775
+ ]);
776
+ }
777
+ }
778
+ const composed = composeSignals(batch, {
779
+ mark: target.mark,
780
+ time: options.time,
781
+ attached: (signal) => attached.get(signal.number) ?? [],
782
+ });
783
+ const intro = target.first
784
+ ? introOf(target.mark, resetBy === undefined ? undefined : { by: resetBy })
785
+ : undefined;
786
+ const text = [intro, composed.text].filter((part) => part !== undefined).join("\n\n");
787
+ try {
788
+ await target.driver.send({ text, images, priority });
789
+ }
790
+ catch (error) {
791
+ // Not handed over: kept, and what the product says of itself decides what is next.
792
+ log(`a message could not be handed to the product (${codeOf(error)})`);
793
+ pending = [...batch, ...pending];
794
+ return;
795
+ }
796
+ if (target.first) {
797
+ target.first = false;
798
+ resetBy = undefined;
799
+ }
800
+ const write = (unread, only) => {
801
+ for (const signal of batch) {
802
+ if (only !== undefined && !only(signal)) {
803
+ continue;
804
+ }
805
+ const files = attached.get(signal.number) ?? [];
806
+ const named = (which) => {
807
+ const names = files.filter(which).map((file) => file.name);
808
+ return names.length === 0 ? undefined : names.join(", ");
809
+ };
810
+ // Beside the pictures: the other files by name, and the pictures the product was not
811
+ // handed as pictures, which whoever sent them is told of on the page.
812
+ const others = named((file) => file.picture === undefined);
813
+ const unseen = named((file) => file.picture === "unseen");
814
+ record(target, signal.source === "person" ? "person" : "wake", signal.text, {
815
+ ...signalData(signal),
816
+ ...(others === undefined ? {} : { files: others }),
817
+ ...(unseen === undefined ? {} : { unseen }),
818
+ ...(unread === true ? { unread: true } : {}),
819
+ }, pictures.get(signal.number));
820
+ }
821
+ };
822
+ // A product that waited for a message takes this one at once, and the record says so here.
823
+ // One at a turn takes it when it comes to it, after the tool call under way or after the
824
+ // turn: the record is written then, where the agent read it, and until then the page shows
825
+ // it as waiting.
826
+ const waited = !target.turn;
827
+ if (waited) {
828
+ write();
829
+ }
830
+ target.handed.push({ written: waited, write, signals: batch });
831
+ target.turn = true;
832
+ // Everything handed over since the agent was last done: what a product that took nothing is sent again.
833
+ target.sent = [...target.sent, ...batch];
834
+ // Handed over, and not delivered until the product says it took it: a product that did
835
+ // not start took nothing, and a runner that ends then is sent it again when it is back.
836
+ for (const signal of batch) {
837
+ owed.add(signal.number);
838
+ }
839
+ await account();
840
+ };
841
+ /** Counts signals as delivered: the product took them, or the record says what became of them. */
842
+ const count = async (signals) => {
843
+ for (const signal of signals) {
844
+ owed.delete(signal.number);
845
+ settled = Math.max(settled, signal.number);
846
+ }
847
+ await account();
848
+ };
849
+ /**
850
+ * The product took the oldest message it was handed: on the record now, where it was not
851
+ * written at once, and delivered.
852
+ */
853
+ const take = async (target) => {
854
+ target.took = true;
855
+ const message = target.handed.shift();
856
+ if (message !== undefined) {
857
+ if (!message.written) {
858
+ message.write();
859
+ }
860
+ await count(message.signals);
861
+ }
862
+ };
863
+ /**
864
+ * The agent is done with what it was sent: what the product was handed and did not say it
865
+ * took is taken, on the record where the record does not hold it yet, and delivered.
866
+ */
867
+ const settle = async (target) => {
868
+ target.took = true;
869
+ const counted = [];
870
+ for (const message of target.handed.splice(0)) {
871
+ if (!message.written) {
872
+ message.write();
873
+ }
874
+ counted.push(...message.signals);
875
+ }
876
+ await count(counted);
877
+ };
878
+ /**
879
+ * Hears what the product said as its session was ended. Its words wait in the queue behind
880
+ * whatever ended the session, and are let go there once the session is no longer the open
881
+ * one: a message it said it took then is taken, and not sent again, and the conversation it
882
+ * named is the one to go on with, where that message is.
883
+ */
884
+ const hearLast = async (target) => {
885
+ let named = false;
886
+ for (const message of queue) {
887
+ if (message.t !== "driver" || message.session !== target) {
888
+ continue;
889
+ }
890
+ const { event } = message;
891
+ if (event.type === "conversation") {
892
+ conversation = event.id;
893
+ named = true;
894
+ }
895
+ else if (event.type === "taken") {
896
+ await take(target);
897
+ }
898
+ else if (event.type === "turn_completed") {
899
+ await settle(target);
900
+ record(target, "status", "The turn completed.", { turn: "completed" });
901
+ }
902
+ }
903
+ if (named) {
904
+ await persist();
905
+ }
906
+ };
907
+ /**
908
+ * What becomes of what the product was handed and never took, as its session ends
909
+ * (ADR-0131). Sent again, it is not delivered, and the record holds nothing more of it than
910
+ * a line that says so: by the console to the runner started `after` this one, where the
911
+ * runner itself ends; by this runner in the `next` session, where the product had taken
912
+ * nothing of this one, and so had not started. Else it is let go: on the record as never
913
+ * read, which delivers it; what the record holds since it was handed over, to a product that
914
+ * waited for a message, is said in a line not to have been taken.
915
+ */
916
+ const leave = async (target, again) => {
917
+ const messages = target.handed.splice(0);
918
+ const signals = messages.flatMap((message) => message.signals);
919
+ if (signals.length === 0) {
920
+ return;
921
+ }
922
+ const numbersOf = (some) => some.map((signal) => `#${signal.number}`).join(", ");
923
+ if (again !== undefined) {
924
+ const numbers = numbersOf(signals);
925
+ record(target, "status", `The session ended before the product took what it was sent: ${numbers}. It is sent again.`);
926
+ log(`${numbers} not taken when session ${target.id} ended: sent again`);
927
+ if (again === "next") {
928
+ for (const signal of signals) {
929
+ // Waits to be handed over again: owed no word of the product's, and not delivered.
930
+ owed.delete(signal.number);
931
+ sentBefore.add(signal.number);
932
+ }
933
+ pending = [...signals, ...pending];
934
+ }
935
+ return;
936
+ }
937
+ const written = messages.filter((message) => message.written).flatMap((one) => one.signals);
938
+ if (written.length > 0) {
939
+ record(target, "status", `The session ended before the product took what it was sent: ${numbersOf(written)}. Whoever sent it says it again where it still matters.`);
940
+ }
941
+ for (const message of messages) {
942
+ if (!message.written) {
943
+ message.write(true);
944
+ }
945
+ }
946
+ // On the record as never read: delivered.
947
+ await count(signals);
948
+ };
949
+ /**
950
+ * What the transcript says of a signal beside its words: its number and kind, where it came
951
+ * from, and who sent it, by the login, with the agent they sent it through. One shape for
952
+ * every signal, so that whoever reads a session reads who stopped it as who told it.
953
+ */
954
+ const signalData = (signal) => ({
955
+ signal: signal.number,
956
+ kind: signal.kind,
957
+ source: signal.source,
958
+ key: signal.key,
959
+ by: signal.by?.login ?? null,
960
+ agent: signal.agent,
961
+ });
962
+ /** The next cool-down: the first, or twice the one before, up to the most. */
963
+ const cooled = (before) => {
964
+ const tries = (before?.tries ?? 0) + 1;
965
+ const length = Math.min(timing.coolDownMs * 2 ** (tries - 1), timing.maxCoolDownMs);
966
+ return { tries, until: options.now() + length };
967
+ };
968
+ /**
969
+ * Counts what is delivered: signals in order, up to the last one the product took, the record
970
+ * says was never read or let go, or the runner acted on itself, a stop or a choice of model
971
+ * say, and never past one that still waits to be handed to the product or that the product
972
+ * has not said it took.
973
+ */
974
+ const account = async (signal) => {
975
+ acted = Math.max(acted, signal?.number ?? 0);
976
+ const most = Math.max(settled, acted);
977
+ let least = Number.POSITIVE_INFINITY;
978
+ for (const kept of pending) {
979
+ least = Math.min(least, kept.number);
980
+ }
981
+ for (const number of owed) {
982
+ least = Math.min(least, number);
983
+ }
984
+ const next = Math.min(most, least - 1);
985
+ if (next > delivered) {
986
+ delivered = next;
987
+ await persist();
988
+ }
989
+ };
990
+ /** Stops the turn under way through the driver, and says so where it could not. */
991
+ const stopTurn = async (target) => {
992
+ await target.driver.interrupt().catch((error) => {
993
+ log(`the turn could not be stopped (${codeOf(error)})`);
994
+ });
995
+ };
996
+ const onSignal = async (signal) => {
997
+ handled = Math.max(handled, signal.number);
998
+ if (signal.number <= delivered ||
999
+ owed.has(signal.number) ||
1000
+ pending.some((kept) => kept.number === signal.number)) {
1001
+ return;
1002
+ }
1003
+ const by = signal.by?.name ?? signal.by?.login ?? "a supervisor";
1004
+ switch (signal.kind) {
1005
+ case "stop": {
1006
+ held = true;
1007
+ if (session !== undefined) {
1008
+ // What the product had not taken is on the record before the stop that leaves it
1009
+ // unread; what a product that had not started was handed waits for the restart.
1010
+ await leave(session, session.took ? undefined : "next");
1011
+ record(session, "person", signal.text, signalData(signal));
1012
+ }
1013
+ await end("stopped");
1014
+ await account(signal);
1015
+ setState("stopped", "stop");
1016
+ return;
1017
+ }
1018
+ case "restart":
1019
+ case "reset": {
1020
+ held = false;
1021
+ waits = undefined;
1022
+ await end(signal.kind === "reset" ? "reset" : "restarted");
1023
+ if (signal.kind === "reset") {
1024
+ conversation = undefined;
1025
+ resetBy = by;
1026
+ // What a product was handed and never took is tried once more, in the new
1027
+ // conversation: a product that does not start with it again is not sent it a
1028
+ // third time, so that no signal stands in the way of every one after it for ever.
1029
+ for (const kept of pending) {
1030
+ if (sentBefore.has(kept.number)) {
1031
+ lastTry.add(kept.number);
1032
+ }
1033
+ }
1034
+ }
1035
+ // Its words are the agent's to read, in the first message of the session that follows.
1036
+ if (signal.text.trim().length > 0) {
1037
+ pending.push(signal);
1038
+ }
1039
+ else {
1040
+ await account(signal);
1041
+ }
1042
+ await persist();
1043
+ setState("running", signal.kind === "reset" ? "reset" : "resumed");
1044
+ if (pending.length > 0) {
1045
+ scheduleDeliver();
1046
+ }
1047
+ return;
1048
+ }
1049
+ case "set": {
1050
+ if (signal.set !== null) {
1051
+ settings = signal.set;
1052
+ }
1053
+ await account(signal);
1054
+ if (session !== undefined && !(await session.driver.set(settings))) {
1055
+ // Taken when the product starts: the session ends when no turn is under way, and the next has it.
1056
+ renew = true;
1057
+ if (!session.turn) {
1058
+ await end("restarted");
1059
+ }
1060
+ }
1061
+ push({ t: "heartbeat" });
1062
+ return;
1063
+ }
1064
+ case "interrupt": {
1065
+ // A turn of the session open when it was sent is stopped first; anything else just reads the words.
1066
+ const sent = Date.parse(signal.at);
1067
+ const stops = session?.turn === true && !held && sent >= session.startedAt;
1068
+ if (signal.text.trim().length === 0 && signal.attachments.length === 0) {
1069
+ // No words of its own: the turn is stopped so that what the agent was sent and has
1070
+ // not taken is read now. On the record as who stopped the turn, where one was.
1071
+ if (session !== undefined && stops) {
1072
+ record(session, "person", "", signalData(signal));
1073
+ }
1074
+ // Nothing is handed over once the runner is to end for an update: the turn is
1075
+ // stopped all the same, and what waits is sent again to the runner started after.
1076
+ if (pending.length > 0 && updatedTo === undefined) {
1077
+ if (stops) {
1078
+ await deliver("now");
1079
+ }
1080
+ else {
1081
+ scheduleDeliver();
1082
+ }
1083
+ }
1084
+ else if (session !== undefined && stops) {
1085
+ await stopTurn(session);
1086
+ }
1087
+ await account(signal);
1088
+ return;
1089
+ }
1090
+ pending.push(signal);
1091
+ if (stops && updatedTo !== undefined && session !== undefined) {
1092
+ // Its words are not handed to a runner that is to end: the turn is stopped, the
1093
+ // runner ends for the update, and the console sends them again to the one after.
1094
+ await stopTurn(session);
1095
+ }
1096
+ else if (stops) {
1097
+ await deliver("now");
1098
+ }
1099
+ else {
1100
+ scheduleDeliver();
1101
+ }
1102
+ return;
1103
+ }
1104
+ default: {
1105
+ pending.push(signal);
1106
+ if (waits !== undefined && signal.source === "person") {
1107
+ await deliver("next");
1108
+ }
1109
+ else {
1110
+ scheduleDeliver();
1111
+ }
1112
+ }
1113
+ }
1114
+ };
1115
+ const onDriver = async (target, event) => {
1116
+ if (target !== session) {
1117
+ return;
1118
+ }
1119
+ switch (event.type) {
1120
+ case "running": {
1121
+ // What the product does not say of itself stays what the session was started with.
1122
+ target.running = {
1123
+ model: event.model ?? target.running.model,
1124
+ effort: event.effort ?? target.running.effort,
1125
+ };
1126
+ productVersion = event.version ?? productVersion;
1127
+ push({ t: "heartbeat" });
1128
+ return;
1129
+ }
1130
+ case "conversation": {
1131
+ conversation = event.id;
1132
+ await persist();
1133
+ return;
1134
+ }
1135
+ case "taken": {
1136
+ await take(target);
1137
+ return;
1138
+ }
1139
+ case "turn_completed": {
1140
+ // The agent is done with everything it was sent: what a product did not say it took is taken.
1141
+ await settle(target);
1142
+ // The record says where a turn ends, so that whoever reads it live knows the agent is done.
1143
+ record(target, "status", "The turn completed.", { turn: "completed" });
1144
+ target.turn = false;
1145
+ for (const signal of target.sent) {
1146
+ sentBefore.delete(signal.number);
1147
+ lastTry.delete(signal.number);
1148
+ }
1149
+ target.sent = [];
1150
+ if (waits !== undefined || stands.state !== "running") {
1151
+ waits = undefined;
1152
+ setState("running", "completed");
1153
+ }
1154
+ if (renew) {
1155
+ await end("restarted");
1156
+ }
1157
+ if (pending.length > 0) {
1158
+ scheduleDeliver();
1159
+ }
1160
+ return;
1161
+ }
1162
+ case "cannot_go_on": {
1163
+ // Reported, and nothing restarted: a supervisor's signal is tried at once, the rest after the cool-down.
1164
+ target.turn = false;
1165
+ // A product that did not start, or is not signed in, took nothing: what was sent is tried again.
1166
+ if (event.reason === "start" || event.reason === "sign_in") {
1167
+ // What a reset gave one more try and the product did not start with again is let
1168
+ // go: on the record as never read, with a line that says so, and sent no more.
1169
+ const again = target.sent.filter((signal) => !lastTry.has(signal.number));
1170
+ const gone = target.sent.filter((signal) => lastTry.has(signal.number));
1171
+ if (gone.length > 0) {
1172
+ for (const message of target.handed) {
1173
+ if (!message.written) {
1174
+ message.write(true, (signal) => lastTry.has(signal.number));
1175
+ }
1176
+ }
1177
+ const numbers = gone.map((signal) => `#${signal.number}`).join(", ");
1178
+ record(target, "status", `What the agent was sent before the reset was let go, since the product did not start with it again: ${numbers}. Whoever sent it says it again where it still matters.`, { let_go: numbers });
1179
+ log(`${numbers} let go: the product did not start with what it was sent, after a reset too`);
1180
+ for (const signal of gone) {
1181
+ lastTry.delete(signal.number);
1182
+ sentBefore.delete(signal.number);
1183
+ }
1184
+ }
1185
+ for (const signal of again) {
1186
+ sentBefore.add(signal.number);
1187
+ // Waits to be handed over again: owed no word of the product's, and not delivered.
1188
+ owed.delete(signal.number);
1189
+ }
1190
+ pending = [...again, ...pending];
1191
+ // Sent again, and on the record when it is taken then.
1192
+ target.handed = [];
1193
+ // What was let go is on the record as that, and is delivered: it is sent no more.
1194
+ await count(gone);
1195
+ }
1196
+ target.sent = [];
1197
+ waits = cooled(waits);
1198
+ setState(event.state, event.reason);
1199
+ if (pending.length > 0) {
1200
+ scheduleDeliver(waits.until);
1201
+ }
1202
+ return;
1203
+ }
1204
+ case "ended": {
1205
+ await end(event.how, true);
1206
+ if (pending.length > 0 && waits === undefined) {
1207
+ scheduleDeliver();
1208
+ }
1209
+ return;
1210
+ }
1211
+ default:
1212
+ }
1213
+ };
1214
+ /** The product as the console is told of it: its version as the product said it last, where that is a line the console takes. */
1215
+ const productOf = () => {
1216
+ const version = productVersion ?? probe.version ?? null;
1217
+ return {
1218
+ driver: driver.name,
1219
+ name: driver.product.name,
1220
+ version: lineOf(version),
1221
+ tried: driver.product.tried.includes(version ?? ""),
1222
+ };
1223
+ };
1224
+ /** What the product offers to choose from, as far as the console takes it, and what the driver can do. */
1225
+ const offersOf = () => ({
1226
+ models: offeredOf(probe.models),
1227
+ efforts: offeredOf(probe.efforts),
1228
+ steer: driver.can.steer,
1229
+ images: driver.can.images,
1230
+ resume: driver.can.resume,
1231
+ });
1232
+ const beat = async () => {
1233
+ const now = `${stands.state} ${stands.reason} ${session?.id ?? ""}`;
1234
+ said = now;
1235
+ try {
1236
+ const answer = await client.runnerHeartbeat({
1237
+ state: stands.state,
1238
+ reason: stands.reason,
1239
+ delivered,
1240
+ session: session?.id ?? null,
1241
+ machine: options.machine,
1242
+ runner: options.version,
1243
+ product: productOf(),
1244
+ offers: offersOf(),
1245
+ // What the product says it runs with, where the console takes the name; nothing where
1246
+ // it would refuse it, so that one name does not cost the account its heartbeat.
1247
+ running: {
1248
+ model: choiceOf(session?.running.model),
1249
+ effort: choiceOf(session?.running.effort),
1250
+ },
1251
+ });
1252
+ settings = answer.settings;
1253
+ tooOldSaid = false;
1254
+ sayOver("heartbeat", "the console takes the heartbeat again");
1255
+ // A stop that stands at the console and that this runner did not see: it was sent before
1256
+ // the runner started. One still on its way down the stream is acted on when it arrives,
1257
+ // with its words.
1258
+ if (answer.state === "stopped" && !held && answer.latest <= handled) {
1259
+ held = true;
1260
+ await end("stopped");
1261
+ stands = { state: "stopped", reason: "stop" };
1262
+ }
1263
+ }
1264
+ catch (error) {
1265
+ if (stopped) {
1266
+ return;
1267
+ }
1268
+ if (error instanceof ApiError && error.code === "client.outdated") {
1269
+ if (!tooOldSaid) {
1270
+ tooOldSaid = true;
1271
+ const install = options.updateCommand?.();
1272
+ log(`the console no longer takes this version of the command: ${error.message}${install === undefined ? "" : ` What installs it: ${install}`}`);
1273
+ // The agent is told, once: what installs the version the console takes, which the
1274
+ // runner then sees and starts again for; and whom to tell where it cannot.
1275
+ if (session !== undefined && !session.first) {
1276
+ const target = session;
1277
+ const update = install === undefined
1278
+ ? ""
1279
+ : ` Update it: ${install}\nThe runner sees the update and is started again with it by itself, once no turn is under way.`;
1280
+ try {
1281
+ await target.driver.send({
1282
+ text: `[console ${target.mark} · runner]\nThe console no longer takes this version of the command the runner runs from, and hears nothing of this session until it is updated.${update}\nWhere you cannot update it, say so to whom you report to.`,
1283
+ images: [],
1284
+ priority: "next",
1285
+ });
1286
+ // The agent has a turn's work in it, the install among it: counted as a turn, so
1287
+ // that the update it makes is taken when that turn ends and never inside it. What
1288
+ // the product says it took then is this message, which the record holds nothing
1289
+ // of, and not the message of a signal that waits behind it.
1290
+ target.turn = true;
1291
+ target.handed.push({ written: true, write: () => undefined, signals: [] });
1292
+ }
1293
+ catch {
1294
+ // Not told: the runner's own line above says it to whoever reads its output.
1295
+ }
1296
+ }
1297
+ }
1298
+ return;
1299
+ }
1300
+ sayOnce("heartbeat", `the console did not take the heartbeat (${codeOf(error)})`);
1301
+ }
1302
+ };
1303
+ /**
1304
+ * Looks whether the package the runner runs from was updated (ADR-0101). A version is taken
1305
+ * once it was seen twice, a heartbeat's interval apart, so that an install under way has put
1306
+ * its last file down by the time the runner is started again.
1307
+ */
1308
+ const lookForUpdate = async () => {
1309
+ if (options.installed === undefined || updatedTo !== undefined) {
1310
+ return;
1311
+ }
1312
+ const now = await options.installed().catch(() => undefined);
1313
+ if (now === undefined || now === options.version) {
1314
+ seenVersion = undefined;
1315
+ return;
1316
+ }
1317
+ if (seenVersion !== now) {
1318
+ seenVersion = now;
1319
+ seenAt = options.now();
1320
+ return;
1321
+ }
1322
+ // A heartbeat is said at every change of state too: two looks a moment apart are one
1323
+ // sighting, and the second counts a heartbeat's interval after the first, less what a
1324
+ // timer may be early by.
1325
+ if (options.now() - seenAt < timing.heartbeatMs - Math.min(1000, timing.heartbeatMs / 2)) {
1326
+ return;
1327
+ }
1328
+ updatedTo = now;
1329
+ log(`the command the runner runs from is ${now} now, and was ${options.version} when the runner started: the runner ends once no turn is under way, to be started again`);
1330
+ };
1331
+ /** Waits so long, or until the runner stops. */
1332
+ const pause = (ms, signal = abort.signal) => new Promise((resolve) => {
1333
+ if (signal.aborted) {
1334
+ resolve();
1335
+ return;
1336
+ }
1337
+ const timer = setTimeout(resolve, ms);
1338
+ signal.addEventListener("abort", () => {
1339
+ clearTimeout(timer);
1340
+ resolve();
1341
+ }, { once: true });
1342
+ });
1343
+ /**
1344
+ * Keeps a wake source listening for as long as the runner runs: started again when it ends,
1345
+ * soon where it held for a while, and after longer and longer where it keeps ending at once.
1346
+ * What it raises goes to the console, and comes back in order on the runner's own stream.
1347
+ */
1348
+ const keepSource = async (source) => {
1349
+ const wakes = options.wakes;
1350
+ if (source.run === undefined || wakes === undefined) {
1351
+ return;
1352
+ }
1353
+ const host = {
1354
+ secret: async (name) => secretValues.get(name),
1355
+ raise: async (raised) => {
1356
+ // The console takes so many wakes a runner raises itself within an hour (ADR-0132):
1357
+ // past that, what the sources raise is let go for a while, longer each time, and said
1358
+ // once, so that a source that repeats itself costs the console nothing more.
1359
+ if (wakesHeld !== undefined && options.now() < wakesHeld.until) {
1360
+ return;
1361
+ }
1362
+ try {
1363
+ await client.raiseWake({ source: "messenger", key: raised.key, text: raised.text });
1364
+ }
1365
+ catch (error) {
1366
+ if (codeOf(error) !== "signal.too_many") {
1367
+ throw error;
1368
+ }
1369
+ const pause = heldFor(wakesHeld);
1370
+ wakesHeld = { until: options.now() + pause, pause };
1371
+ sayOnce("wakes", "the console takes no more wakes of this runner's within the hour (signal.too_many); what its wake sources raise is let go for a while");
1372
+ return;
1373
+ }
1374
+ if (wakesHeld !== undefined) {
1375
+ wakesHeld = undefined;
1376
+ sayOver("wakes", "the console takes the wakes of this runner's wake sources again");
1377
+ }
1378
+ },
1379
+ log,
1380
+ fetch: wakes.fetch,
1381
+ socket: wakes.socket,
1382
+ wait: pause,
1383
+ };
1384
+ let wait = timing.reconnectMs;
1385
+ while (!stopped) {
1386
+ const began = options.now();
1387
+ try {
1388
+ await source.run(host, abort.signal);
1389
+ }
1390
+ catch (error) {
1391
+ if (!stopped) {
1392
+ log(`the wake source ${source.name} ended (${codeOf(error)}); started again`);
1393
+ }
1394
+ }
1395
+ wait =
1396
+ options.now() - began >= SOURCE_HELD_MS
1397
+ ? timing.reconnectMs
1398
+ : Math.min(wait * 2, MAX_SOURCE_PAUSE_MS);
1399
+ await pause(wait);
1400
+ }
1401
+ };
1402
+ /** Reads the account's signals as they are sent, from the last one received on, for as long as the runner runs. */
1403
+ const listen = async () => {
1404
+ while (!stopped) {
1405
+ try {
1406
+ for await (const signal of client.signalStream(received, abort.signal)) {
1407
+ received = Math.max(received, signal.number);
1408
+ sayOver("stream");
1409
+ push({ t: "signal", signal });
1410
+ }
1411
+ }
1412
+ catch (error) {
1413
+ if (!stopped) {
1414
+ sayOnce("stream", `the stream of signals ended (${codeOf(error)}); asked for again`);
1415
+ }
1416
+ }
1417
+ if (!stopped) {
1418
+ await new Promise((resolve) => {
1419
+ const timer = setTimeout(resolve, timing.reconnectMs);
1420
+ abort.signal.addEventListener("abort", () => {
1421
+ clearTimeout(timer);
1422
+ resolve();
1423
+ }, { once: true });
1424
+ });
1425
+ }
1426
+ }
1427
+ };
1428
+ return {
1429
+ stop() {
1430
+ if (!stopped) {
1431
+ push({ t: "stop" });
1432
+ }
1433
+ },
1434
+ async run() {
1435
+ const kept = await options.state.load().catch(() => undefined);
1436
+ delivered = kept?.delivered ?? 0;
1437
+ conversation = kept?.conversation;
1438
+ if (conversation !== undefined && kept?.driver !== undefined && kept.driver !== driver.name) {
1439
+ // Another product's conversation is nothing this one can go on with: asked to, it fails
1440
+ // at every turn until a supervisor resets it.
1441
+ conversation = undefined;
1442
+ log(`the conversation kept was begun under the driver ${kept.driver}: a new one begins under ${driver.name}`);
1443
+ }
1444
+ probe = await driver.probe();
1445
+ const left = probe.models.length +
1446
+ probe.efforts.length -
1447
+ offeredOf(probe.models).length -
1448
+ offeredOf(probe.efforts).length;
1449
+ if (left > 0) {
1450
+ log(`${left} of the models and the levels of effort the product offers are not said to the console: it takes a name of letters, digits and . _ : [ ] - and ${MAX_RUNNER_OFFERS} of each`);
1451
+ }
1452
+ await readSecrets();
1453
+ // The first heartbeat says the runner is there, and learns where the console's record stands.
1454
+ try {
1455
+ const first = await client.runnerHeartbeat({
1456
+ state: "running",
1457
+ reason: "started",
1458
+ delivered,
1459
+ session: null,
1460
+ machine: options.machine,
1461
+ runner: options.version,
1462
+ product: productOf(),
1463
+ offers: offersOf(),
1464
+ running: { model: null, effort: null },
1465
+ });
1466
+ settings = first.settings;
1467
+ delivered = Math.max(delivered, first.delivered);
1468
+ if (first.state === "stopped") {
1469
+ held = true;
1470
+ stands = { state: "stopped", reason: "stop" };
1471
+ }
1472
+ }
1473
+ catch (error) {
1474
+ log(`the console did not take the first heartbeat (${codeOf(error)}); the runner goes on`);
1475
+ }
1476
+ received = delivered;
1477
+ handled = delivered;
1478
+ said = `${stands.state} ${stands.reason} `;
1479
+ const listening = listen();
1480
+ const sourcing = (options.wakes?.sources ?? []).map((source) => keepSource(source));
1481
+ const beating = setInterval(() => push({ t: "heartbeat" }), timing.heartbeatMs);
1482
+ const batching = setInterval(() => push({ t: "flush" }), timing.batchMs);
1483
+ log(`the runner is there: ${driver.product.name} ${probe.version ?? "of a version it did not say"}, signals after #${delivered}`);
1484
+ let how = "stopped";
1485
+ try {
1486
+ for (;;) {
1487
+ // An update is taken between two turns, and at once where none is under way.
1488
+ if (updatedTo !== undefined && session?.turn !== true) {
1489
+ how = "updated";
1490
+ if (session !== undefined) {
1491
+ record(session, "status", `The command the runner runs from was updated, from ${options.version} to ${updatedTo}: the runner ends, to be started again with it.`);
1492
+ }
1493
+ break;
1494
+ }
1495
+ const message = await next();
1496
+ if (message.t === "stop") {
1497
+ break;
1498
+ }
1499
+ try {
1500
+ if (message.t === "signal") {
1501
+ await onSignal(message.signal);
1502
+ }
1503
+ else if (message.t === "driver") {
1504
+ await onDriver(message.session, message.event);
1505
+ }
1506
+ else if (message.t === "deliver") {
1507
+ // A timer may fire a moment before the clock says it is time.
1508
+ if (deliverAt !== undefined && options.now() >= deliverAt - 20) {
1509
+ await deliver("next");
1510
+ }
1511
+ }
1512
+ else if (message.t === "flush") {
1513
+ if (session !== undefined) {
1514
+ await flush(session);
1515
+ }
1516
+ }
1517
+ else {
1518
+ await lookForUpdate();
1519
+ // Said every thirty seconds, and at once when how the agent stands changed.
1520
+ const changed = said !== `${stands.state} ${stands.reason} ${session?.id ?? ""}`;
1521
+ if (changed || !queue.some((queued) => queued.t === "heartbeat")) {
1522
+ await beat();
1523
+ await readSecrets();
1524
+ }
1525
+ }
1526
+ }
1527
+ catch (error) {
1528
+ log(`the runner could not do what it was about to (${codeOf(error)}); it goes on`);
1529
+ }
1530
+ }
1531
+ }
1532
+ finally {
1533
+ stopped = true;
1534
+ clearInterval(beating);
1535
+ clearInterval(batching);
1536
+ for (const timer of timers) {
1537
+ clearTimeout(timer);
1538
+ }
1539
+ // A session ended for an update goes on, in the runner started after, as one a
1540
+ // supervisor restarted does: with the same conversation.
1541
+ await end(how === "updated" ? "restarted" : "exited");
1542
+ abort.abort();
1543
+ await listening;
1544
+ await Promise.all(sourcing);
1545
+ log(how === "updated" ? "the runner ended, for the update" : "the runner stopped");
1546
+ }
1547
+ return how;
1548
+ },
1549
+ };
1550
+ }
1551
+ //# sourceMappingURL=runner.js.map