@temmo/cli 0.0.0-stage → 0.19.0
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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +16 -0
- package/.codex-plugin/plugin.json +30 -0
- package/.cursor-plugin/plugin.json +22 -0
- package/.mcp.json +18 -0
- package/LICENSE +202 -0
- package/README.md +82 -2
- package/THIRD_PARTY_NOTICES +32 -0
- package/commands/afk.md +17 -0
- package/deploy/build-ui.mjs +65 -0
- package/deploy/claude/README.md +16 -0
- package/deploy/claude/auto-mode.json +31 -0
- package/deploy/postgres/compose.yaml +26 -0
- package/deploy/temmo-runner.service +24 -0
- package/deploy/verify-ui.mjs +15 -0
- package/harness-core/README.md +27 -0
- package/harness-core/bin/harness.mjs +17 -0
- package/harness-core/package.json +10 -0
- package/harness-core/stage.mjs +108 -0
- package/harness-core/stages/commit.md +7 -0
- package/harness-core/stages/pr.md +7 -0
- package/harness-core/stages/push.md +7 -0
- package/hooks/codex-hooks.json +14 -0
- package/hooks/cursor-hooks.json +17 -0
- package/hooks/hooks.json +27 -0
- package/lib/acp/permission.js +72 -0
- package/lib/acp/sessionConfig.js +109 -0
- package/lib/activity.js +244 -0
- package/lib/agent-guard.js +72 -0
- package/lib/agents-route.js +55 -0
- package/lib/agents.js +213 -0
- package/lib/agui.js +255 -0
- package/lib/alerts.js +36 -0
- package/lib/analyze-otlp.js +46 -0
- package/lib/analyze-run-code.js +397 -0
- package/lib/analyze-sources.js +372 -0
- package/lib/approval-card.js +54 -0
- package/lib/approval-cli.js +53 -0
- package/lib/approval-request.js +81 -0
- package/lib/approval-signer.js +48 -0
- package/lib/approvals.js +295 -0
- package/lib/attachment-cache.js +236 -0
- package/lib/attachment.js +63 -0
- package/lib/blocked.js +20 -0
- package/lib/build-review.js +90 -0
- package/lib/builder/cli.js +90 -0
- package/lib/builder/policy.js +176 -0
- package/lib/builder/recover.js +189 -0
- package/lib/builder/run.js +240 -0
- package/lib/builder/settle.js +198 -0
- package/lib/builder/stage.js +240 -0
- package/lib/builder/state.js +73 -0
- package/lib/builder/supervisor.js +289 -0
- package/lib/builder-clean.js +223 -0
- package/lib/builder-code.cjs +67 -0
- package/lib/builder.js +9 -0
- package/lib/bus-postgres.js +281 -0
- package/lib/bus.js +349 -0
- package/lib/catalog/author.js +278 -0
- package/lib/catalog/dot.js +426 -0
- package/lib/catalog/pattern-model.js +193 -0
- package/lib/catalog/patterns.js +324 -0
- package/lib/catalog/projects.js +83 -0
- package/lib/catalog/records.js +2 -0
- package/lib/catalog/roster.js +70 -0
- package/lib/catalog/team-files.js +49 -0
- package/lib/catalog/teams.js +109 -0
- package/lib/claude-socket.js +333 -0
- package/lib/cli.js +741 -0
- package/lib/code-mode.js +45 -0
- package/lib/code-read-policy.yaml +9 -0
- package/lib/code-run.js +461 -0
- package/lib/codex-compact.js +141 -0
- package/lib/compaction.js +21 -0
- package/lib/config.js +424 -0
- package/lib/cursor-trace.js +248 -0
- package/lib/deep-link.js +20 -0
- package/lib/deliver-push.js +120 -0
- package/lib/delivery.js +28 -0
- package/lib/deploy-tree.js +227 -0
- package/lib/durable.js +404 -0
- package/lib/factory-tick.js +32 -0
- package/lib/family.js +63 -0
- package/lib/fetch-origin.js +53 -0
- package/lib/format.js +133 -0
- package/lib/harness-surface-cli.js +153 -0
- package/lib/harness-surface.js +486 -0
- package/lib/harness.js +271 -0
- package/lib/hook.mjs +291 -0
- package/lib/idle.js +40 -0
- package/lib/install-cursor.js +115 -0
- package/lib/install-opencode.js +105 -0
- package/lib/job.js +82 -0
- package/lib/land-cli.js +319 -0
- package/lib/land-member.js +64 -0
- package/lib/land.js +320 -0
- package/lib/lessons.js +223 -0
- package/lib/limit-alert.js +423 -0
- package/lib/limits.js +284 -0
- package/lib/machine-prompt.js +19 -0
- package/lib/merge-sweep.js +149 -0
- package/lib/messaging.js +1002 -0
- package/lib/mission-cli.js +103 -0
- package/lib/mission.js +241 -0
- package/lib/mode.js +215 -0
- package/lib/models.js +114 -0
- package/lib/names.js +86 -0
- package/lib/opencode-api.js +57 -0
- package/lib/opencode-plugin.mjs +313 -0
- package/lib/operator-token.js +78 -0
- package/lib/operator.js +187 -0
- package/lib/pad-activity.js +143 -0
- package/lib/pad-fresh.js +44 -0
- package/lib/pad.js +198 -0
- package/lib/pattern-cli.js +120 -0
- package/lib/pattern-compiler.js +68 -0
- package/lib/pattern-runs.js +795 -0
- package/lib/pattern-stall.js +58 -0
- package/lib/pattern-tick.js +45 -0
- package/lib/pattern-workflow.js +69 -0
- package/lib/pi-extension.mjs +98 -0
- package/lib/pi.js +57 -0
- package/lib/plugin-copy.js +76 -0
- package/lib/pm-review.js +32 -0
- package/lib/pm-workspace.js +87 -0
- package/lib/postgres.js +115 -0
- package/lib/pr-card.js +34 -0
- package/lib/probe.js +295 -0
- package/lib/process.js +67 -0
- package/lib/project-pause.js +23 -0
- package/lib/prune.js +793 -0
- package/lib/questions.js +250 -0
- package/lib/receipts.js +63 -0
- package/lib/reflect.js +962 -0
- package/lib/rename-migration.js +118 -0
- package/lib/run-terminal.js +437 -0
- package/lib/runner-request.js +33 -0
- package/lib/sandbox-app.js +120 -0
- package/lib/sandbox-claims.js +258 -0
- package/lib/sandbox-cli.js +188 -0
- package/lib/sandbox-hosts-cli.js +68 -0
- package/lib/sandbox-hosts.js +94 -0
- package/lib/sandbox-pr.js +102 -0
- package/lib/sandbox-proposal.js +367 -0
- package/lib/sandbox-tools.js +143 -0
- package/lib/sandbox.js +501 -0
- package/lib/schedule-session.js +68 -0
- package/lib/session-activity.js +67 -0
- package/lib/session-events.js +94 -0
- package/lib/sessions/control.js +244 -0
- package/lib/sessions/git-refs.js +265 -0
- package/lib/sessions/launch.js +359 -0
- package/lib/sessions/registry.js +450 -0
- package/lib/sessions/scope.js +111 -0
- package/lib/setup.js +377 -0
- package/lib/signed-approval.js +225 -0
- package/lib/slash.js +167 -0
- package/lib/spawn-cli.js +45 -0
- package/lib/spawn.js +107 -0
- package/lib/start-limit.js +58 -0
- package/lib/state.js +24 -0
- package/lib/stop.js +308 -0
- package/lib/store-read.js +62 -0
- package/lib/store.js +252 -0
- package/lib/subagent-guard.js +240 -0
- package/lib/surfaces/channel-post.js +43 -0
- package/lib/surfaces/instructions.js +15 -0
- package/lib/surfaces/mcp-reload.js +144 -0
- package/lib/surfaces/ops.js +629 -0
- package/lib/surfaces/render-mcp.js +33 -0
- package/lib/surfaces/run-terminal-ops.js +40 -0
- package/lib/surfaces/sandbox-ops.js +36 -0
- package/lib/surfaces/schema.js +7 -0
- package/lib/surfaces/task-view.js +90 -0
- package/lib/surfaces/types.js +4 -0
- package/lib/task-cli.js +274 -0
- package/lib/task-order.js +34 -0
- package/lib/task-order.mjs +2 -0
- package/lib/task-route.js +125 -0
- package/lib/tasks.js +631 -0
- package/lib/team-permission.js +34 -0
- package/lib/team-templates/blitz/team.dot +9 -0
- package/lib/team-templates/factory/team.dot +26 -0
- package/lib/team-templates/feature/team.dot +14 -0
- package/lib/team-templates/fix-and-review/team.dot +12 -0
- package/lib/team-templates/product/team.dot +16 -0
- package/lib/team-templates/release/team.dot +7 -0
- package/lib/team-templates/review/team.dot +6 -0
- package/lib/terminal-draft.js +42 -0
- package/lib/thread-branch.js +82 -0
- package/lib/thread-channel-store.js +181 -0
- package/lib/thread-channels.js +116 -0
- package/lib/thread-listening.js +20 -0
- package/lib/thread-routes.js +91 -0
- package/lib/threads.js +441 -0
- package/lib/tool-api.js +33 -0
- package/lib/transcript.js +98 -0
- package/lib/ui-build-output.js +19 -0
- package/lib/ui-capabilities.js +90 -0
- package/lib/usage.js +182 -0
- package/lib/vendor/mcp-sdk.d.mts +4 -0
- package/lib/vendor/mcp-sdk.mjs +17046 -0
- package/lib/waiter.js +127 -0
- package/lib/workflows.js +546 -0
- package/lib/worktree-setup.js +86 -0
- package/package.json +110 -4
- package/runner/adapters/claude.js +465 -0
- package/runner/adapters/cli-sandbox.js +277 -0
- package/runner/adapters/codex.js +675 -0
- package/runner/adapters/cursor.mjs +937 -0
- package/runner/adapters/fake.js +119 -0
- package/runner/adapters/opencode-sandbox.js +370 -0
- package/runner/adapters/opencode.js +486 -0
- package/runner/adapters/pi-sandbox.js +134 -0
- package/runner/adapters/pi.js +364 -0
- package/runner/client.js +40 -0
- package/runner/contract.js +129 -0
- package/runner/control.js +178 -0
- package/runner/delivery.js +184 -0
- package/runner/factory-tick.mjs +10 -0
- package/runner/lock.js +41 -0
- package/runner/main.mjs +118 -0
- package/runner/pattern-tick.mjs +16 -0
- package/runner/publish.js +113 -0
- package/runner/runner.js +2612 -0
- package/runner/sandbox-run.mjs +239 -0
- package/runner/schedules.js +294 -0
- package/runner/task-wake.js +59 -0
- package/scripts/ai-collab +3 -0
- package/scripts/ai-collab-bus.mjs +4 -0
- package/scripts/ai-collab-code-mcp.mjs +3 -0
- package/scripts/ai-collab-hook +3 -0
- package/scripts/ai-collab-hook.mjs +3 -0
- package/scripts/ai-collab-mcp-tools.mjs +2 -0
- package/scripts/ai-collab-mcp.mjs +3 -0
- package/scripts/ai-collab.mjs +3 -0
- package/scripts/install-cursor.mjs +11 -0
- package/scripts/install-opencode.mjs +10 -0
- package/scripts/temmo +3 -0
- package/scripts/temmo-bus.mjs +151 -0
- package/scripts/temmo-code-mcp.mjs +63 -0
- package/scripts/temmo-hook +3 -0
- package/scripts/temmo-hook.mjs +201 -0
- package/scripts/temmo-mcp-tools.mjs +22 -0
- package/scripts/temmo-mcp.mjs +27 -0
- package/scripts/temmo.mjs +4 -0
- package/skills/antislop/SKILL.md +701 -0
- package/skills/antislop/VERSION +1 -0
- package/skills/antislop-code/SKILL.md +128 -0
- package/skills/antislop-human/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-human/SKILL.md +204 -0
- package/skills/antislop-human/contrast-check.py +168 -0
- package/skills/antislop-human/contrast-mcp.py +125 -0
- package/skills/antislop-human/references/A11Y-PATTERNS.md +249 -0
- package/skills/antislop-human/references/WCAG.md +127 -0
- package/skills/antislop-layoutmobile/SKILL.md +170 -0
- package/skills/antislop-ui/LICENSE-web-quality-skills +21 -0
- package/skills/antislop-ui/SKILL.md +351 -0
- package/skills/antislop-ui/references/CORE-WEB-VITALS.md +181 -0
- package/skills/antislop-ui/references/WEB-BASELINE.md +28 -0
- package/skills/harness-surface/SKILL.md +42 -0
- package/skills/land/SKILL.md +32 -0
- package/skills/memory-file/SKILL.md +39 -0
- package/skills/temmo/SKILL.md +101 -0
- package/skills/temmo/references/role-programs.md +47 -0
- package/skills/ui-sdk/SKILL.md +56 -0
- package/skills/ui-sdk/references/agent-ready.md +136 -0
- package/skills/ui-sdk/references/build-interface.md +187 -0
- package/skills/ui-sdk/references/create-component.md +88 -0
- package/skills/ui-sdk/references/patterns.md +246 -0
- package/skills/ui-sdk/references/styles.md +66 -0
- package/skills/ui-sdk/references/testing.md +172 -0
- package/skills/ui-sdk/references/web-components.md +78 -0
- package/tools/screen-state.mjs +119 -0
- package/tools/smoke-ui.mjs +540 -0
- package/web/app/activity.jsx +251 -0
- package/web/app/agent-mark.tsx +43 -0
- package/web/app/alert-level.jsx +16 -0
- package/web/app/api/[action]/route.js +8 -0
- package/web/app/api/auth/[step]/route.js +12 -0
- package/web/app/api/events/route.js +8 -0
- package/web/app/api/file/[name]/route.js +8 -0
- package/web/app/api/land/route.js +12 -0
- package/web/app/api/lessons/route.js +8 -0
- package/web/app/api/push/key/route.js +8 -0
- package/web/app/api/push/subscribe/route.js +12 -0
- package/web/app/api/push/test/route.js +8 -0
- package/web/app/api/refs/route.js +8 -0
- package/web/app/api/review-diff/route.js +8 -0
- package/web/app/api/schedules/route.js +8 -0
- package/web/app/api/thread-diff/route.js +8 -0
- package/web/app/api/version/route.js +6 -0
- package/web/app/cards.jsx +189 -0
- package/web/app/chat-api.js +82 -0
- package/web/app/chat-composer.jsx +313 -0
- package/web/app/chat-console.jsx +691 -0
- package/web/app/chat-image.tsx +61 -0
- package/web/app/chat-message.jsx +122 -0
- package/web/app/chat-shared.jsx +59 -0
- package/web/app/chat-sidebar.jsx +25 -0
- package/web/app/chat-stream.js +127 -0
- package/web/app/chat.jsx +31 -0
- package/web/app/compactions.jsx +57 -0
- package/web/app/composer-actions.tsx +87 -0
- package/web/app/decisions.jsx +53 -0
- package/web/app/fonts/OFL-hanken-grotesk.txt +93 -0
- package/web/app/fonts/OFL-ibm-plex-mono.txt +93 -0
- package/web/app/fonts/hanken-grotesk-latin-wght-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/web/app/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
- package/web/app/globals.css +199 -0
- package/web/app/home.jsx +246 -0
- package/web/app/icon.svg +1 -0
- package/web/app/land-panel.jsx +63 -0
- package/web/app/land-reviews.jsx +20 -0
- package/web/app/layout.jsx +17 -0
- package/web/app/lessons.tsx +76 -0
- package/web/app/logo.tsx +13 -0
- package/web/app/logos.jsx +16 -0
- package/web/app/manifest.js +8 -0
- package/web/app/mode-menu.tsx +46 -0
- package/web/app/new-session.jsx +275 -0
- package/web/app/notify.jsx +180 -0
- package/web/app/outcomes.tsx +94 -0
- package/web/app/page.jsx +9 -0
- package/web/app/pattern-graph.jsx +75 -0
- package/web/app/patterns-page.jsx +293 -0
- package/web/app/phone-nav.tsx +36 -0
- package/web/app/progress-feed.tsx +33 -0
- package/web/app/providers.tsx +27 -0
- package/web/app/quote.jsx +15 -0
- package/web/app/run-card.tsx +200 -0
- package/web/app/run-requests.tsx +148 -0
- package/web/app/scheduled.jsx +105 -0
- package/web/app/session-panel.tsx +174 -0
- package/web/app/session-toolbar.tsx +180 -0
- package/web/app/shadcn.css +638 -0
- package/web/app/shipped.jsx +49 -0
- package/web/app/subagent-runs.jsx +31 -0
- package/web/app/task-graph.jsx +67 -0
- package/web/app/tasks-strip.tsx +36 -0
- package/web/app/tasks.jsx +375 -0
- package/web/app/team-author.jsx +145 -0
- package/web/app/team-editor.jsx +88 -0
- package/web/app/thread-agents.tsx +32 -0
- package/web/app/thread-place.tsx +9 -0
- package/web/app/thread-sessions.tsx +149 -0
- package/web/app/thread-sidebar.tsx +418 -0
- package/web/app/thread-switcher.tsx +27 -0
- package/web/app/tool-diff.jsx +94 -0
- package/web/components/a2ui/controls.jsx +36 -0
- package/web/components/a2ui/option-picker.jsx +23 -0
- package/web/components/a2ui/script.tsx +121 -0
- package/web/components/a2ui/surface.jsx +81 -0
- package/web/components/chat/thread.jsx +50 -0
- package/web/components/script-text.tsx +42 -0
- package/web/components/ui/avatar.jsx +115 -0
- package/web/components/ui/badge.jsx +47 -0
- package/web/components/ui/button.jsx +63 -0
- package/web/components/ui/chat-container.jsx +52 -0
- package/web/components/ui/choice-list.tsx +72 -0
- package/web/components/ui/code-block.jsx +60 -0
- package/web/components/ui/copy-button.tsx +41 -0
- package/web/components/ui/dropdown-menu.jsx +257 -0
- package/web/components/ui/markdown.jsx +84 -0
- package/web/components/ui/message.jsx +28 -0
- package/web/components/ui/popover.jsx +33 -0
- package/web/components/ui/scroll-button.jsx +34 -0
- package/web/components/ui/select.jsx +189 -0
- package/web/components/ui/sheet.jsx +152 -0
- package/web/components/ui/sonner.jsx +10 -0
- package/web/components/ui/tabs.jsx +88 -0
- package/web/components/ui/textarea.jsx +20 -0
- package/web/components/ui/tooltip.jsx +56 -0
- package/web/components.json +24 -0
- package/web/instrumentation.js +8 -0
- package/web/lib/a2ui-catalog.js +34 -0
- package/web/lib/a2ui-pr.js +76 -0
- package/web/lib/a2ui-run-catalog.js +18 -0
- package/web/lib/a2ui-run.js +93 -0
- package/web/lib/a2ui.js +262 -0
- package/web/lib/activity-rows.js +275 -0
- package/web/lib/agent-detail.js +5 -0
- package/web/lib/alert-level.js +44 -0
- package/web/lib/approval-mode.js +22 -0
- package/web/lib/chat-turns.js +174 -0
- package/web/lib/child-subagents.js +16 -0
- package/web/lib/compaction-view.js +48 -0
- package/web/lib/composer-actions.js +40 -0
- package/web/lib/context-view.js +57 -0
- package/web/lib/fuzzy.js +26 -0
- package/web/lib/highlight.js +44 -0
- package/web/lib/lessons-view.js +23 -0
- package/web/lib/mentions.js +16 -0
- package/web/lib/message-routing.js +105 -0
- package/web/lib/new-session.js +85 -0
- package/web/lib/operator-messages.js +28 -0
- package/web/lib/pattern-graph.js +165 -0
- package/web/lib/pattern-view.js +33 -0
- package/web/lib/pending-summary.js +18 -0
- package/web/lib/permission-modes.js +30 -0
- package/web/lib/project-feed.js +107 -0
- package/web/lib/quote.js +7 -0
- package/web/lib/reply-quote.js +31 -0
- package/web/lib/scheduled.js +79 -0
- package/web/lib/selection.js +110 -0
- package/web/lib/send-confirmation.js +22 -0
- package/web/lib/session-dead.js +10 -0
- package/web/lib/session-state.js +132 -0
- package/web/lib/sidebar.js +15 -0
- package/web/lib/slash-commands.js +164 -0
- package/web/lib/start-default.js +35 -0
- package/web/lib/start-mode.js +35 -0
- package/web/lib/stream-views.js +49 -0
- package/web/lib/subagent-state.js +20 -0
- package/web/lib/task-board.js +54 -0
- package/web/lib/task-graph.js +183 -0
- package/web/lib/task-tags.js +41 -0
- package/web/lib/team-editor.js +10 -0
- package/web/lib/thread-groups.js +86 -0
- package/web/lib/threads.js +28 -0
- package/web/lib/tool-diff.js +273 -0
- package/web/lib/ui-events.js +58 -0
- package/web/lib/utils.js +5 -0
- package/web/lib/view-memory.js +25 -0
- package/web/next.config.mjs +37 -0
- package/web/postcss.config.mjs +1 -0
- package/web/proxy.js +25 -0
- package/web/public/sw.js +39 -0
- package/web/server/auth.js +178 -0
- package/web/server/dispatch.js +293 -0
- package/web/server/history.js +56 -0
- package/web/server/hosted.js +58 -0
- package/web/server/hub.js +126 -0
- package/web/server/instance.js +5 -0
- package/web/server/land-reviews.js +64 -0
- package/web/server/push.js +148 -0
- package/web/server/run-terminal.js +79 -0
- package/web/server/schedules.js +38 -0
- package/web/server/shipped.js +51 -0
- package/web/server/task-board.js +71 -0
- package/web/server/threads.js +209 -0
- package/web/server/ui.js +2937 -0
- package/web/tsconfig.base.json +16 -0
- package/web/tsconfig.json +9 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push sender: VAPID keys and browser subscriptions live in 0600 files under the state dir, never in the repo.
|
|
3
|
+
* Payloads are encrypted (aes128gcm) for each browser, so the push service (FCM, Mozilla autopush) sees only ciphertext.
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { generateRequestDetails, generateVAPIDKeys } from "web-push";
|
|
8
|
+
import { writeState } from "../../lib/state.js";
|
|
9
|
+
const SENT_TTL_MS = 24 * 60 * 60_000;
|
|
10
|
+
const SENT_MAX = 2000;
|
|
11
|
+
const SUBSCRIPTIONS_MAX = 20;
|
|
12
|
+
const ENDPOINT_MAX = 2048;
|
|
13
|
+
const KEY = /^[A-Za-z0-9_-]{16,128}={0,2}$/;
|
|
14
|
+
const TOPIC = /^[A-Za-z0-9_-]{1,32}$/;
|
|
15
|
+
const TTL_S = 6 * 60 * 60;
|
|
16
|
+
const SEND_TIMEOUT_MS = 15_000;
|
|
17
|
+
const TITLE_MAX = 80;
|
|
18
|
+
const BODY_MAX = 200;
|
|
19
|
+
const GONE = [404, 410];
|
|
20
|
+
const clip = (text, max) => {
|
|
21
|
+
const flat = String(text ?? "").replace(/\s+/g, " ").trim();
|
|
22
|
+
return flat.length > max ? flat.slice(0, max - 1) + "…" : flat;
|
|
23
|
+
};
|
|
24
|
+
function readJson(file, fallback) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return fallback;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** A browser PushSubscription reduced to {endpoint, keys}, or null when the shape is wrong. */
|
|
33
|
+
export function cleanSubscription(raw, { allowHttp = false } = {}) {
|
|
34
|
+
const endpoint = raw?.endpoint;
|
|
35
|
+
if (typeof endpoint !== "string" || endpoint.length > ENDPOINT_MAX)
|
|
36
|
+
return null;
|
|
37
|
+
let url;
|
|
38
|
+
try {
|
|
39
|
+
url = new URL(endpoint);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (url.protocol !== "https:" && !(allowHttp && url.protocol === "http:"))
|
|
45
|
+
return null;
|
|
46
|
+
const { p256dh, auth } = raw.keys || {};
|
|
47
|
+
if (!KEY.test(p256dh || "") || !KEY.test(auth || ""))
|
|
48
|
+
return null;
|
|
49
|
+
return { endpoint, keys: { p256dh, auth } };
|
|
50
|
+
}
|
|
51
|
+
/** Hash route that opens the item's session thread, else the project's session list, in the UI. */
|
|
52
|
+
export function itemUrl(project, sender = "") {
|
|
53
|
+
const [peer, session] = String(sender || "").split("#");
|
|
54
|
+
const route = session ? { project, level: "session", harness: peer, session: sender } : peer ? { project, level: "session" } : { project };
|
|
55
|
+
return "/#" + new URLSearchParams(route).toString();
|
|
56
|
+
}
|
|
57
|
+
/** The notification payload: who asks and a clipped one-line summary, never file contents. */
|
|
58
|
+
export function notice({ project, kind, id, sender, harness, tool, text, important = false }) {
|
|
59
|
+
// oxlint-disable-next-line quality/no-known-value-widening -- looked up by a runtime notice kind
|
|
60
|
+
const titles = {
|
|
61
|
+
approval: `Approve ${tool || "tool"} · ${sender}`, choice: `Choose · ${sender}`,
|
|
62
|
+
question: `${important ? "Important question" : "Question"} · ${sender}`, message: `Message · ${sender}`,
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
title: clip(titles[kind], TITLE_MAX), body: clip(text, BODY_MAX), tag: id, important: important === true,
|
|
66
|
+
kind, project, session: sender, harness: harness || String(sender).split("#")[0], url: itemUrl(project, sender),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function createPush({ dir, subject, fetch: send = globalThis.fetch, now = Date.now, log = console.warn, allowHttp = false }) {
|
|
70
|
+
const files = { vapid: join(dir, "vapid.json"), subscriptions: join(dir, "subscriptions.json"), sent: join(dir, "sent.json") };
|
|
71
|
+
let vapid = null;
|
|
72
|
+
function keys() {
|
|
73
|
+
if (vapid)
|
|
74
|
+
return vapid;
|
|
75
|
+
const stored = readJson(files.vapid, null);
|
|
76
|
+
if (KEY.test(stored?.publicKey || "") && KEY.test(stored?.privateKey || ""))
|
|
77
|
+
vapid = stored;
|
|
78
|
+
else {
|
|
79
|
+
vapid = generateVAPIDKeys();
|
|
80
|
+
writeState(files.vapid, vapid);
|
|
81
|
+
}
|
|
82
|
+
return vapid;
|
|
83
|
+
}
|
|
84
|
+
const list = () => {
|
|
85
|
+
const value = readJson(files.subscriptions, []);
|
|
86
|
+
return Array.isArray(value) ? value.map((item) => cleanSubscription(item, { allowHttp })).filter((item) => Boolean(item)) : [];
|
|
87
|
+
};
|
|
88
|
+
const save = (items) => writeState(files.subscriptions, items);
|
|
89
|
+
function subscribe(raw) {
|
|
90
|
+
const sub = cleanSubscription(raw, { allowHttp });
|
|
91
|
+
if (!sub)
|
|
92
|
+
throw Object.assign(new Error("Invalid push subscription"), { status: 400 });
|
|
93
|
+
keys();
|
|
94
|
+
save([...list().filter(item => item.endpoint !== sub.endpoint), sub].slice(-SUBSCRIPTIONS_MAX));
|
|
95
|
+
return sub;
|
|
96
|
+
}
|
|
97
|
+
function unsubscribe(endpoint) {
|
|
98
|
+
const items = list();
|
|
99
|
+
const kept = items.filter(item => item.endpoint !== endpoint);
|
|
100
|
+
if (kept.length !== items.length)
|
|
101
|
+
save(kept);
|
|
102
|
+
return items.length - kept.length;
|
|
103
|
+
}
|
|
104
|
+
// One encrypted POST to the browser's push service; 404 or 410 means the subscription is gone.
|
|
105
|
+
async function deliver(sub, message) {
|
|
106
|
+
const { publicKey, privateKey } = keys();
|
|
107
|
+
const details = generateRequestDetails(sub, JSON.stringify(message), {
|
|
108
|
+
vapidDetails: { subject, publicKey, privateKey }, TTL: TTL_S,
|
|
109
|
+
urgency: message.important ? "high" : "normal", ...(TOPIC.test(message.tag || "") ? { topic: message.tag } : {}),
|
|
110
|
+
});
|
|
111
|
+
const headers = { ...details.headers };
|
|
112
|
+
delete headers["Content-Length"];
|
|
113
|
+
try {
|
|
114
|
+
const res = await send(details.endpoint, { method: details.method, headers, body: details.body, signal: AbortSignal.timeout(SEND_TIMEOUT_MS) });
|
|
115
|
+
await res.arrayBuffer().catch(() => { });
|
|
116
|
+
if (res.ok)
|
|
117
|
+
return "sent";
|
|
118
|
+
if (GONE.includes(res.status))
|
|
119
|
+
return "gone";
|
|
120
|
+
log(`temmo push: ${new URL(sub.endpoint).host} answered ${res.status}`);
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
log(`temmo push: ${new URL(sub.endpoint).host} unreachable (${err.name})`);
|
|
124
|
+
}
|
|
125
|
+
return "failed";
|
|
126
|
+
}
|
|
127
|
+
/** Sends to every subscription (or the one endpoint given) and drops those the push service reports gone. */
|
|
128
|
+
async function sendAll(message, { endpoint } = {}) {
|
|
129
|
+
const targets = list().filter(item => !endpoint || item.endpoint === endpoint);
|
|
130
|
+
const results = await Promise.all(targets.map(sub => deliver(sub, message)));
|
|
131
|
+
const gone = new Set(targets.filter((_, i) => results[i] === "gone").map(item => item.endpoint));
|
|
132
|
+
if (gone.size)
|
|
133
|
+
save(list().filter(item => !gone.has(item.endpoint)));
|
|
134
|
+
return { sent: results.filter(r => r === "sent").length, gone: gone.size, failed: results.filter(r => r === "failed").length };
|
|
135
|
+
}
|
|
136
|
+
/** Sends once per key: ids stay remembered for a day, across restarts. */
|
|
137
|
+
async function notify(key, message) {
|
|
138
|
+
const at = now();
|
|
139
|
+
const sent = Object.entries(readJson(files.sent, {})).filter(([, t]) => Number.isFinite(t) && at - t < SENT_TTL_MS);
|
|
140
|
+
if (sent.some(([id]) => id === key))
|
|
141
|
+
return { sent: 0, duplicate: true };
|
|
142
|
+
writeState(files.sent, Object.fromEntries([...sent, [key, at]].slice(-SENT_MAX)));
|
|
143
|
+
if (!list().length)
|
|
144
|
+
return { sent: 0 };
|
|
145
|
+
return sendAll(message);
|
|
146
|
+
}
|
|
147
|
+
return { publicKey: () => keys().publicKey, subscribe, unsubscribe, subscriptions: list, send: sendAll, notify };
|
|
148
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Run in terminal on the UI server: watches request folders, lists cards in the snapshot, and runs a script on a Run click.
|
|
2
|
+
import { mkdirSync, watch } from "node:fs";
|
|
3
|
+
import { SHOWN, closeRun, decideRun, listRunRequests, recoverRuns, runInTerminalEnabled, runRoot, runStatus } from "../../lib/run-terminal.js";
|
|
4
|
+
import { runAnswer, runOperations, runSurfaceId } from "../lib/a2ui-run.js";
|
|
5
|
+
const CHANGED = "the request changed after it was shown";
|
|
6
|
+
/**
|
|
7
|
+
* The UI server's run-in-terminal hooks; every hook is inert unless runInTerminal is on and the UI runs on the host
|
|
8
|
+
* (not hosted). notify gets a push notice for each request that appears while the server runs.
|
|
9
|
+
*/
|
|
10
|
+
export function runTerminalUi({ env, hosted, broadcast, notify = () => { } }) {
|
|
11
|
+
const on = () => !hosted && runInTerminalEnabled(env);
|
|
12
|
+
const watchers = [];
|
|
13
|
+
const seen = new Map();
|
|
14
|
+
// The fields of each request as this server first showed them; the card keeps showing them and Run requires them.
|
|
15
|
+
const pins = new Map();
|
|
16
|
+
function listed(cfg) {
|
|
17
|
+
return listRunRequests(cfg.stateDir, cfg.project).map((request) => {
|
|
18
|
+
const key = cfg.project + "/" + request.id;
|
|
19
|
+
const pin = pins.get(key);
|
|
20
|
+
if (!pin)
|
|
21
|
+
pins.set(key, Object.fromEntries(SHOWN.map(field => [field, request[field]])));
|
|
22
|
+
if (!pin || SHOWN.every(field => pin[field] === request[field]))
|
|
23
|
+
return request;
|
|
24
|
+
closeRun(cfg.stateDir, cfg.project, request.id, CHANGED);
|
|
25
|
+
return { ...request, ...runStatus(cfg.stateDir, cfg.project, request.id), ...pin };
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function scan(cfg) {
|
|
29
|
+
const known = seen.get(cfg.project);
|
|
30
|
+
for (const request of listed(cfg)) {
|
|
31
|
+
if (known.has(request.id))
|
|
32
|
+
continue;
|
|
33
|
+
known.add(request.id);
|
|
34
|
+
if (request.status === "pending")
|
|
35
|
+
notify(cfg.project, request);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
/** Settles runs a previous server left running (adopts live ones, closes dead ones as lost), then watches the request folder: a new request pushes the card and a notice. */
|
|
40
|
+
watch(cfg) {
|
|
41
|
+
if (!on())
|
|
42
|
+
return;
|
|
43
|
+
const root = runRoot(cfg.stateDir, cfg.project);
|
|
44
|
+
mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
45
|
+
recoverRuns(cfg.stateDir, cfg.project, broadcast);
|
|
46
|
+
seen.set(cfg.project, new Set(listed(cfg).map(request => request.id)));
|
|
47
|
+
const watcher = watch(root, () => { scan(cfg); broadcast(); });
|
|
48
|
+
watcher.unref();
|
|
49
|
+
watchers.push(watcher);
|
|
50
|
+
},
|
|
51
|
+
/** {runRequests} for the snapshot, only the session's own in a session view; {} while off. */
|
|
52
|
+
snapshot(cfg, session) {
|
|
53
|
+
if (!on())
|
|
54
|
+
return {};
|
|
55
|
+
const list = listed(cfg).filter(request => !session || request.session === session);
|
|
56
|
+
return { runRequests: list };
|
|
57
|
+
},
|
|
58
|
+
/** A2UI surfaces of the snapshot's run cards. */
|
|
59
|
+
surfaces(current, now) {
|
|
60
|
+
return (current.runRequests || []).map((request) => ({ id: runSurfaceId(request.id), operations: runOperations(request, now) }));
|
|
61
|
+
},
|
|
62
|
+
/** The decision for a run card's action, or null when the action belongs to another card. */
|
|
63
|
+
answer(cfg, userAction, by) {
|
|
64
|
+
const answer = runAnswer(userAction);
|
|
65
|
+
if (!answer)
|
|
66
|
+
return null;
|
|
67
|
+
if (!on()) {
|
|
68
|
+
const error = new Error("Run in terminal is off");
|
|
69
|
+
error.status = 403;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
return decideRun({ stateDir: cfg.stateDir, project: cfg.project, ...answer, pinned: pins.get(cfg.project + "/" + answer.id), by: by ? `operator:${by}` : "operator", onUpdate: broadcast, env });
|
|
73
|
+
},
|
|
74
|
+
close() {
|
|
75
|
+
for (const watcher of watchers.splice(0))
|
|
76
|
+
watcher.close();
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Read-only runner schedules for the UI: the control socket's GET routes; offline when no runner answers.
|
|
2
|
+
import { runnerRequest } from "../../runner/client.js";
|
|
3
|
+
import { listProjects } from "../../lib/config.js";
|
|
4
|
+
import { projectOfFolder } from "../../lib/schedule-session.js";
|
|
5
|
+
const READ_TIMEOUT_MS = 2000;
|
|
6
|
+
/** The runner's schedules as {status: "up", schedules, configError}, or {status: "offline"} when its socket does not answer. */
|
|
7
|
+
export async function readSchedules(options = {}) {
|
|
8
|
+
try {
|
|
9
|
+
const { status, body } = await runnerRequest("GET", "/v1/schedules", undefined, { timeoutMs: READ_TIMEOUT_MS, ...options });
|
|
10
|
+
return status === 200 ? { status: "up", ...body } : { status: "offline" };
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return { status: "offline" };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The latest runs of one schedule; rejects with status 503 when the runner is offline. Each run carries the project
|
|
18
|
+
* its schedule's folder belongs to, so the UI opens the timeline where that run's session publishes.
|
|
19
|
+
*/
|
|
20
|
+
export async function readScheduleRuns(name, { env = process.env, ...options } = {}) {
|
|
21
|
+
let response;
|
|
22
|
+
try {
|
|
23
|
+
response = await runnerRequest("GET", `/v1/schedules/${encodeURIComponent(name)}/runs`, undefined, { timeoutMs: READ_TIMEOUT_MS, ...options });
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw Object.assign(new Error("runner offline"), { status: 503 });
|
|
27
|
+
}
|
|
28
|
+
if (response.status !== 200)
|
|
29
|
+
throw Object.assign(new Error(response.body?.error || "Runner error"), { status: response.status });
|
|
30
|
+
const runs = Array.isArray(response.body) ? response.body : [];
|
|
31
|
+
if (!runs.length)
|
|
32
|
+
return runs;
|
|
33
|
+
// The schedule's folder names its project; without one a run still lists, it just links to no timeline.
|
|
34
|
+
const listed = await readSchedules(options);
|
|
35
|
+
const cwd = (listed.schedules || []).find((item) => item.name === name)?.cwd || "";
|
|
36
|
+
const project = projectOfFolder(cwd, listProjects(env));
|
|
37
|
+
return project ? runs.map((run) => ({ ...run, project })) : runs;
|
|
38
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { COMMIT_ID } from "../../lib/build-review.js";
|
|
3
|
+
const checkouts = new Map();
|
|
4
|
+
const options = { encoding: "utf8", timeout: 2000, stdio: ["ignore", "pipe", "ignore"] };
|
|
5
|
+
function mainState(root) {
|
|
6
|
+
const now = Date.now();
|
|
7
|
+
const previous = checkouts.get(root);
|
|
8
|
+
if (previous && now - previous.at < 2000)
|
|
9
|
+
return previous;
|
|
10
|
+
let tip;
|
|
11
|
+
try {
|
|
12
|
+
tip = execFileSync("git", ["rev-parse", "--verify", "refs/remotes/origin/main^{commit}"], { ...options, cwd: root }).trim();
|
|
13
|
+
}
|
|
14
|
+
catch { }
|
|
15
|
+
const state = { tip, at: now, commits: tip === previous?.tip ? previous?.commits ?? new Map() : new Map() };
|
|
16
|
+
if (checkouts.size >= 32)
|
|
17
|
+
checkouts.clear();
|
|
18
|
+
checkouts.set(root, state);
|
|
19
|
+
return state;
|
|
20
|
+
}
|
|
21
|
+
export function shippedTasks(backend, project, root) {
|
|
22
|
+
if (!backend || !root)
|
|
23
|
+
return [];
|
|
24
|
+
let tasks;
|
|
25
|
+
try {
|
|
26
|
+
tasks = backend.list();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const reviewed = tasks.filter(task => COMMIT_ID.test(task.review?.commit));
|
|
32
|
+
if (!reviewed.length)
|
|
33
|
+
return [];
|
|
34
|
+
const { tip, commits } = mainState(root);
|
|
35
|
+
if (!tip)
|
|
36
|
+
return [];
|
|
37
|
+
return reviewed.flatMap(task => {
|
|
38
|
+
const { commit, at, decision } = task.review;
|
|
39
|
+
if (!commits.has(commit)) {
|
|
40
|
+
try {
|
|
41
|
+
execFileSync("git", ["merge-base", "--is-ancestor", commit, tip], { ...options, cwd: root });
|
|
42
|
+
commits.set(commit, true);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (error.status === 1)
|
|
46
|
+
commits.set(commit, false);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return commits.get(commit) ? [{ project, id: task.id, title: task.title, commit, at: decision?.at ?? at }] : [];
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The task board for the UI snapshot: each task with its owner's presence, its last progress line and its review packet.
|
|
2
|
+
import { reviewIsCurrent } from "../../lib/build-review.js";
|
|
3
|
+
// The board renders every card on each push; one branch-tip read per checkout per window is enough for a stale badge.
|
|
4
|
+
const TIP_TTL_MS = 2000;
|
|
5
|
+
/** The review packet a card and its review view read; `stale` and the diff link are derived, never stored. */
|
|
6
|
+
function reviewOf(task, context) {
|
|
7
|
+
const packet = task.review;
|
|
8
|
+
if (!context || !packet)
|
|
9
|
+
return undefined;
|
|
10
|
+
return {
|
|
11
|
+
submission: packet.submission, commit: packet.commit, base: packet.base, branch: packet.branch,
|
|
12
|
+
criteria: packet.criteria, check: packet.check, files: packet.files,
|
|
13
|
+
insertions: packet.insertions, deletions: packet.deletions, more: packet.more, at: packet.at,
|
|
14
|
+
...(packet.decision ? { decision: packet.decision } : {}),
|
|
15
|
+
stale: !reviewIsCurrent(context.root, packet, { ttlMs: TIP_TTL_MS }),
|
|
16
|
+
diff: "/api/review-diff?" + new URLSearchParams({ project: context.project, id: task.id, submission: packet.submission }),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Tasks of a project backend; an unreadable store reports its error instead of tasks. */
|
|
20
|
+
export function taskBoard(backend, agents = [], runs = [], sessions = [], previousBuilder = () => null, assignment, reviewContext) {
|
|
21
|
+
if (!backend)
|
|
22
|
+
return { tasks: [] };
|
|
23
|
+
let list, outcomes;
|
|
24
|
+
try {
|
|
25
|
+
({ tasks: list, outcomes } = backend.projectBoard ? backend.projectBoard() : { tasks: backend.list() });
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
return { tasks: [], error: err.message };
|
|
29
|
+
}
|
|
30
|
+
const owners = new Map(agents.map(agent => [agent.id, agent]));
|
|
31
|
+
const runOf = new Map(runs.map(run => [run.id, run.sessions?.[run.active] || ""]));
|
|
32
|
+
const sessionOf = new Map();
|
|
33
|
+
for (const session of sessions) {
|
|
34
|
+
if (!session.taskId || session.status === "ended")
|
|
35
|
+
continue;
|
|
36
|
+
const previous = sessionOf.get(session.taskId);
|
|
37
|
+
if (!previous || session.createdAt > previous.createdAt)
|
|
38
|
+
sessionOf.set(session.taskId, session);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
...(outcomes ? { outcomes } : {}),
|
|
42
|
+
tasks: list.map(task => {
|
|
43
|
+
const owner = task.owner || sessionOf.get(task.id)?.address;
|
|
44
|
+
const previous = previousBuilder(task);
|
|
45
|
+
const continuation = previous && !sessions.some(session => session.status !== "ended"
|
|
46
|
+
&& (session.cwd === previous.worktree || session.address === owner));
|
|
47
|
+
const review = reviewOf(task, reviewContext);
|
|
48
|
+
return {
|
|
49
|
+
...(continuation ? { previousHarness: (owner || previous.address)?.split("#")[0] || previous.harness } : {}),
|
|
50
|
+
id: task.id,
|
|
51
|
+
title: task.title,
|
|
52
|
+
...(task.priority ? { priority: task.priority } : {}),
|
|
53
|
+
...(owner && !task.owner ? { inferredOwner: true } : {}),
|
|
54
|
+
status: task.status,
|
|
55
|
+
...(task.detail ? { detail: task.detail } : {}),
|
|
56
|
+
blockers: task.blockers,
|
|
57
|
+
...(owner ? { owner, ownerOnline: owners.get(owner)?.online === true } : {}),
|
|
58
|
+
...(task.status === "building" && owner && owners.get(owner)?.runningJobs > 0
|
|
59
|
+
? { ownerRunningJobs: owners.get(owner).runningJobs } : {}),
|
|
60
|
+
...(task.run ? { run: task.run } : {}),
|
|
61
|
+
// The graph opens the run's active session when the task has no owner.
|
|
62
|
+
...(runOf.get(task.run) ? { runSession: runOf.get(task.run) } : {}),
|
|
63
|
+
last: task.progress.at(-1) ?? null,
|
|
64
|
+
...(task.question ? { question: task.question } : {}),
|
|
65
|
+
...(review ? { review } : {}),
|
|
66
|
+
updated: task.updated,
|
|
67
|
+
...assignment?.(task, sessions),
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// The UI server's side of threads (docs/threads.md): a cached list per project, sync of new sessions, and POST /api/thread.
|
|
2
|
+
import { isAbsolute, sep } from "node:path";
|
|
3
|
+
import { isGithubLogin, listProjects } from "../../lib/config.js";
|
|
4
|
+
import { listOwned, listSessions } from "../../lib/sessions/registry.js";
|
|
5
|
+
import { threadClient } from "../../lib/thread-routes.js";
|
|
6
|
+
import { THREAD_ID, UNTITLED, leadOf, nextTitle, sessionTitle, threadPlace, threadSessionsOf } from "../../lib/threads.js";
|
|
7
|
+
import { channelClient } from "../../lib/thread-channel-store.js";
|
|
8
|
+
import { parseEnvelope } from "../../lib/messaging.js";
|
|
9
|
+
import { attachmentOf } from "../../lib/attachment-cache.js";
|
|
10
|
+
const SYNC_DELAY_MS = 1000;
|
|
11
|
+
const OPS = ["list", "get", "create", "rename", "status", "pin", "archive", "attach-pr", "detach-pr", "move-session", "listen", "messages"];
|
|
12
|
+
const ARCHIVE_MAX = 500;
|
|
13
|
+
function reject(status, message) {
|
|
14
|
+
throw Object.assign(new Error(message), { status });
|
|
15
|
+
}
|
|
16
|
+
/** A thread title from a first prompt: its first line, at most 60 characters, as sync gives (docs/threads.md "Naming"). */
|
|
17
|
+
export function promptTitle(prompt) {
|
|
18
|
+
return sessionTitle({ prompt });
|
|
19
|
+
}
|
|
20
|
+
/** One channel post event in the chat message shape (web/app/chat-message.jsx), the thread as addressee; null when it is not a post. */
|
|
21
|
+
function postMessage(ev) {
|
|
22
|
+
const envelope = parseEnvelope(ev?.message);
|
|
23
|
+
if (!envelope || envelope.kind !== "msg" || typeof envelope.body !== "string")
|
|
24
|
+
return null;
|
|
25
|
+
const from = envelope.session ? `${envelope.from}#${envelope.session}` : envelope.from;
|
|
26
|
+
const attachment = attachmentOf(ev);
|
|
27
|
+
return {
|
|
28
|
+
id: String(ev.id), time: Number(ev.time), kind: "Message", sender: from, from, to: "all", body: envelope.body,
|
|
29
|
+
...(attachment ? { attachment } : {}),
|
|
30
|
+
...(envelope.from === "operator" && isGithubLogin(envelope.by) ? { by: envelope.by } : {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Each thread with its stored listening settings, `listeners: [{session, listening}]`; unchanged when they are unknown. */
|
|
34
|
+
function withListeners(threads, listeners) {
|
|
35
|
+
if (!Array.isArray(listeners))
|
|
36
|
+
return threads;
|
|
37
|
+
return threads.map(thread => ({
|
|
38
|
+
...thread,
|
|
39
|
+
listeners: listeners.filter(l => l.thread === thread.id).map(l => ({ session: l.session, listening: l.listening })),
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
/** Threads of one project: list() is the cached snapshot value; sync() posts unassigned sessions to the bus. */
|
|
43
|
+
export function projectThreads(cfg, { env = process.env, broadcast = () => { }, client = { ...threadClient(cfg), ...channelClient(cfg) }, delayMs = SYNC_DELAY_MS, prompts = () => new Map() } = {}) {
|
|
44
|
+
let threads = [];
|
|
45
|
+
let available = true;
|
|
46
|
+
let timer;
|
|
47
|
+
let running = null;
|
|
48
|
+
const root = () => listProjects(env).find(entry => entry.project === cfg.project)?.root || cfg.root || "";
|
|
49
|
+
async function refresh() {
|
|
50
|
+
try {
|
|
51
|
+
// Archived threads stay listed: the sidebar keeps them in its collapsed Archived group.
|
|
52
|
+
const listed = await client.list(cfg.project, { archived: true });
|
|
53
|
+
if (!Array.isArray(listed))
|
|
54
|
+
return;
|
|
55
|
+
available = true;
|
|
56
|
+
const base = root();
|
|
57
|
+
const placed = listed.map(thread => ({ ...thread, place: threadPlace(thread.worktree, base) }));
|
|
58
|
+
const next = withListeners(placed, await client.listeners?.(cfg.project).catch(() => null));
|
|
59
|
+
if (JSON.stringify(next) !== JSON.stringify(threads)) {
|
|
60
|
+
threads = next;
|
|
61
|
+
broadcast();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
// 404: the bus runs a store without threads; other errors keep the last list.
|
|
66
|
+
if (error.status === 404)
|
|
67
|
+
available = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function syncNow() {
|
|
71
|
+
if (!available)
|
|
72
|
+
return;
|
|
73
|
+
try {
|
|
74
|
+
// Sessions of a thread without a worktree go too: the sync gives the thread their folder.
|
|
75
|
+
const known = new Set(threads.filter(thread => thread.worktree).flatMap(thread => thread.sessions));
|
|
76
|
+
const base = root();
|
|
77
|
+
const all = threadSessionsOf({ sessions: listSessions(cfg), owned: listOwned(cfg), root: base, prompts: prompts() });
|
|
78
|
+
// A known lead goes too when its thread's title would change, so the sync renames the thread.
|
|
79
|
+
const byAddress = new Map(all.map(session => [session.address, session]));
|
|
80
|
+
const retitle = new Set(threads.flatMap(thread => {
|
|
81
|
+
const lead = byAddress.get(leadOf(thread));
|
|
82
|
+
return lead && nextTitle(thread, lead, base) !== null ? [lead.address] : [];
|
|
83
|
+
}));
|
|
84
|
+
const sessions = all.filter(session => !known.has(session.address) || retitle.has(session.address));
|
|
85
|
+
if (sessions.length)
|
|
86
|
+
await client.sync({ project: cfg.project, root: base, sessions });
|
|
87
|
+
}
|
|
88
|
+
catch { /* the next registry change retries */ }
|
|
89
|
+
await refresh();
|
|
90
|
+
}
|
|
91
|
+
/** Debounced: hook writes touch the registry on every event, so one sync runs per quiet second. */
|
|
92
|
+
function sync() {
|
|
93
|
+
if (timer)
|
|
94
|
+
return;
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
timer = undefined;
|
|
97
|
+
running = (running || Promise.resolve()).then(syncNow, syncNow);
|
|
98
|
+
}, delayMs);
|
|
99
|
+
timer.unref?.();
|
|
100
|
+
}
|
|
101
|
+
async function own(id) {
|
|
102
|
+
if (typeof id !== "string" || !THREAD_ID.test(id))
|
|
103
|
+
reject(400, "Invalid thread");
|
|
104
|
+
const thread = await client.get(id);
|
|
105
|
+
if (thread.project !== cfg.project)
|
|
106
|
+
reject(404, "Unknown thread");
|
|
107
|
+
return thread;
|
|
108
|
+
}
|
|
109
|
+
/** The thread a start runs in: its worktree as the start folder, or null for a new thread. */
|
|
110
|
+
async function startIn(id) {
|
|
111
|
+
if (id === undefined || id === null || id === "")
|
|
112
|
+
return null;
|
|
113
|
+
const thread = await own(id);
|
|
114
|
+
const base = root();
|
|
115
|
+
if (!thread.worktree || !isAbsolute(thread.worktree) || (thread.worktree !== base && !thread.worktree.startsWith(base + sep))) {
|
|
116
|
+
reject(409, "The thread's worktree is outside the project");
|
|
117
|
+
}
|
|
118
|
+
return thread;
|
|
119
|
+
}
|
|
120
|
+
/** Attaches a started session to its thread, creating one titled from the prompt when none was named. */
|
|
121
|
+
async function claim(session, { thread, prompt } = {}) {
|
|
122
|
+
if (!available || typeof session !== "string")
|
|
123
|
+
return null;
|
|
124
|
+
try {
|
|
125
|
+
const target = thread || await client.create({ project: cfg.project, title: promptTitle(prompt) || UNTITLED });
|
|
126
|
+
const joined = await client.moveSession(target.id, session);
|
|
127
|
+
await refresh();
|
|
128
|
+
return joined;
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// A start never fails because the thread store is down; the next sync assigns the session.
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async function handle(input) {
|
|
136
|
+
const { op, thread: id } = input;
|
|
137
|
+
if (!OPS.includes(op))
|
|
138
|
+
reject(400, "Unknown thread op");
|
|
139
|
+
if (!available)
|
|
140
|
+
reject(503, "Threads need the postgres bus store");
|
|
141
|
+
// A read: the thread's channel posts, oldest first, in the chat message shape; it skips the write ops' refresh.
|
|
142
|
+
if (op === "messages") {
|
|
143
|
+
const thread = await own(id);
|
|
144
|
+
if (!client.messages)
|
|
145
|
+
reject(503, "Threads need the postgres bus store");
|
|
146
|
+
const page = await client.messages(id, {
|
|
147
|
+
...(input.since ? { since: input.since } : {}),
|
|
148
|
+
...(input.limit !== undefined && input.limit !== null ? { limit: input.limit } : {}),
|
|
149
|
+
});
|
|
150
|
+
const messages = (page.events || []).flatMap((ev) => { const item = postMessage(ev); return item ? [item] : []; });
|
|
151
|
+
return { thread: id, title: thread.title, messages, more: page.more === true };
|
|
152
|
+
}
|
|
153
|
+
let result;
|
|
154
|
+
if (op === "list")
|
|
155
|
+
result = { threads: await client.list(cfg.project, { archived: input.archived === true }) };
|
|
156
|
+
else if (op === "create") {
|
|
157
|
+
const { title, worktree, branch, baseRef, parentThreadId } = input;
|
|
158
|
+
if (parentThreadId !== undefined && parentThreadId !== null)
|
|
159
|
+
await own(parentThreadId);
|
|
160
|
+
result = await client.create({ project: cfg.project, title, worktree, branch, baseRef, parentThreadId, ...(title ? { titleSource: "operator" } : {}) });
|
|
161
|
+
}
|
|
162
|
+
else if (op === "archive") {
|
|
163
|
+
const ids = input.threads;
|
|
164
|
+
if (!Array.isArray(ids) || !ids.length || ids.length > ARCHIVE_MAX)
|
|
165
|
+
reject(400, `threads must list 1-${ARCHIVE_MAX} threads`);
|
|
166
|
+
for (const each of ids)
|
|
167
|
+
await own(each);
|
|
168
|
+
for (const each of ids)
|
|
169
|
+
await client.update(each, { status: "archived" });
|
|
170
|
+
result = { archived: ids.length };
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
await own(id);
|
|
174
|
+
if (op === "get")
|
|
175
|
+
result = await client.get(id);
|
|
176
|
+
else if (op === "rename")
|
|
177
|
+
result = await client.update(id, { title: input.title });
|
|
178
|
+
else if (op === "status")
|
|
179
|
+
result = await client.update(id, { status: input.status });
|
|
180
|
+
else if (op === "pin")
|
|
181
|
+
result = await client.update(id, { pinned: input.pinned });
|
|
182
|
+
else if (op === "attach-pr")
|
|
183
|
+
result = await client.attachPr(id, input.pr || {});
|
|
184
|
+
else if (op === "detach-pr")
|
|
185
|
+
result = await client.detachPr(id, input.pr);
|
|
186
|
+
else if (op === "move-session") {
|
|
187
|
+
if (!listSessions(cfg).some(session => session.id === input.session) && !listOwned(cfg).some(record => record.address === input.session)) {
|
|
188
|
+
reject(404, "Unknown session");
|
|
189
|
+
}
|
|
190
|
+
result = await client.moveSession(id, input.session);
|
|
191
|
+
}
|
|
192
|
+
else if (op === "listen") {
|
|
193
|
+
if (input.on !== true && input.on !== false && input.on !== null)
|
|
194
|
+
reject(400, "on must be true, false or null");
|
|
195
|
+
if (!client.setListening)
|
|
196
|
+
reject(503, "Threads need the postgres bus store");
|
|
197
|
+
if (!listSessions(cfg).some(session => session.id === input.session) && !listOwned(cfg).some(record => record.address === input.session)) {
|
|
198
|
+
reject(404, "Unknown session");
|
|
199
|
+
}
|
|
200
|
+
result = await client.setListening(id, input.session, input.on);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
await refresh();
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
// channels: the bus client a thread-channel post resolves its listeners with, or null when the store has none.
|
|
207
|
+
const channels = client.channel && client.setListening && client.messages && client.listeners ? client : null;
|
|
208
|
+
return { list: () => threads, refresh, sync, startIn, claim, handle, channels, close: () => clearTimeout(timer) };
|
|
209
|
+
}
|