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