@temmo/cli 0.0.0 → 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 +112 -1
- 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
package/lib/names.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The product's names, and the aliases kept from its old name ai-collab (docs/rename.md).
|
|
3
|
+
* Env: TEMMO_* is read first and AI_COLLAB_* is the fallback; applyEnvAliases folds the old names into the new
|
|
4
|
+
* ones once per process, so call sites read TEMMO_* only. Dirs: the new XDG dir, or the old one while setup has
|
|
5
|
+
* not yet moved it (setup leaves a symlink at the old path for sessions started before the rename).
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
export const PRODUCT = "Temmo";
|
|
11
|
+
export const NAME = "temmo";
|
|
12
|
+
/** The npm package; npm refused the bare name temmo as too close to existing packages. */
|
|
13
|
+
export const PACKAGE_NAME = "@temmo/cli";
|
|
14
|
+
export const LEGACY_NAME = "ai-collab";
|
|
15
|
+
export const ENV_PREFIX = "TEMMO_";
|
|
16
|
+
export const LEGACY_ENV_PREFIX = "AI_COLLAB_";
|
|
17
|
+
/** The CLI and the npm bin; `ai-collab` stays as a second bin running the same entry point. */
|
|
18
|
+
export const BINS = [NAME, LEGACY_NAME];
|
|
19
|
+
/** systemd user services by role: bus, ui, runner. */
|
|
20
|
+
export const SERVICE_ROLES = ["bus", "ui", "runner"];
|
|
21
|
+
export const serviceName = (role) => `${NAME}-${role}`;
|
|
22
|
+
export const legacyServiceName = (role) => `${LEGACY_NAME}-${role}`;
|
|
23
|
+
// Bus topics keep the old prefix: they key stored history, and sessions started before the rename publish there.
|
|
24
|
+
export const TOPIC_PREFIX = "ai-collab-";
|
|
25
|
+
/** The systemd user unit dir: $XDG_CONFIG_HOME/systemd/user. */
|
|
26
|
+
export function unitsDir(env = process.env) {
|
|
27
|
+
return path.join(xdgBase("config", env), "systemd", "user");
|
|
28
|
+
}
|
|
29
|
+
/** The installed name of each service, in role order: temmo-<role>, or ai-collab-<role> while only that unit exists. */
|
|
30
|
+
export function installedServices(env = process.env, roles = SERVICE_ROLES) {
|
|
31
|
+
const dir = unitsDir(env);
|
|
32
|
+
return roles.map((role) => {
|
|
33
|
+
const next = serviceName(role);
|
|
34
|
+
const old = legacyServiceName(role);
|
|
35
|
+
return !existsSync(path.join(dir, `${next}.service`)) && existsSync(path.join(dir, `${old}.service`)) ? old : next;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** Whether an env key is one of ours, new or old name (child envs strip or pass both). */
|
|
39
|
+
export function isOwnEnvKey(key) {
|
|
40
|
+
return key.startsWith(ENV_PREFIX) || key.startsWith(LEGACY_ENV_PREFIX);
|
|
41
|
+
}
|
|
42
|
+
/** One setting by its bare name (`STATE_DIR`): TEMMO_<name>, else AI_COLLAB_<name>. */
|
|
43
|
+
export function envValue(env, name) {
|
|
44
|
+
return env[ENV_PREFIX + name] ?? env[LEGACY_ENV_PREFIX + name];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Folds every AI_COLLAB_<name> into TEMMO_<name> (an existing TEMMO_<name> wins) and deletes the old key, so a
|
|
48
|
+
* child built from this env never inherits a stale old name that a deleted new one would bring back. Returns env.
|
|
49
|
+
*/
|
|
50
|
+
export function applyEnvAliases(env = process.env) {
|
|
51
|
+
for (const key of Object.keys(env)) {
|
|
52
|
+
if (!key.startsWith(LEGACY_ENV_PREFIX))
|
|
53
|
+
continue;
|
|
54
|
+
const next = ENV_PREFIX + key.slice(LEGACY_ENV_PREFIX.length);
|
|
55
|
+
if (env[next] === undefined)
|
|
56
|
+
env[next] = env[key];
|
|
57
|
+
delete env[key];
|
|
58
|
+
}
|
|
59
|
+
return env;
|
|
60
|
+
}
|
|
61
|
+
const XDG = {
|
|
62
|
+
config: { var: "XDG_CONFIG_HOME", rel: [".config"] },
|
|
63
|
+
state: { var: "XDG_STATE_HOME", rel: [".local", "state"] },
|
|
64
|
+
data: { var: "XDG_DATA_HOME", rel: [".local", "share"] },
|
|
65
|
+
};
|
|
66
|
+
// Every process folds the old env names in as soon as it loads this module; config.ts and each entry script import it.
|
|
67
|
+
applyEnvAliases(process.env);
|
|
68
|
+
/** The XDG base for a kind: $XDG_<KIND>_HOME, else its default under HOME. */
|
|
69
|
+
export function xdgBase(kind, env = process.env) {
|
|
70
|
+
const spec = XDG[kind];
|
|
71
|
+
return env[spec.var] || path.join(env.HOME || homedir(), ...spec.rel);
|
|
72
|
+
}
|
|
73
|
+
/** The product dir under a base: <base>/temmo, or <base>/ai-collab while only the old one exists. */
|
|
74
|
+
export function productDir(base) {
|
|
75
|
+
const next = path.join(base, NAME);
|
|
76
|
+
const old = path.join(base, LEGACY_NAME);
|
|
77
|
+
return !existsSync(next) && existsSync(old) ? old : next;
|
|
78
|
+
}
|
|
79
|
+
/** <XDG base>/temmo for a kind, with the old-dir fallback of productDir. */
|
|
80
|
+
export function appDir(kind, env = process.env) {
|
|
81
|
+
return productDir(xdgBase(kind, env));
|
|
82
|
+
}
|
|
83
|
+
/** The old and new product dir for each XDG kind; setup moves old to new and links old to new. */
|
|
84
|
+
export function renamedDirs(env = process.env) {
|
|
85
|
+
return Object.keys(XDG).map((kind) => ({ kind, from: path.join(xdgBase(kind, env), LEGACY_NAME), to: path.join(xdgBase(kind, env), NAME) }));
|
|
86
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client for the local OpenCode service API (OpenCode 2). The endpoint comes from
|
|
3
|
+
* $XDG_STATE_HOME/opencode/service.json ({url, password, pid}), the same file Pulsar reads; only loopback URLs are used.
|
|
4
|
+
* A `--standalone` OpenCode has no service, so every call then reports ok: false.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
const LOOPBACK = /^http:\/\/(127\.0\.0\.1|localhost|\[::1\]):\d+\/?$/;
|
|
10
|
+
const TIMEOUT_MS = 20_000;
|
|
11
|
+
/** The service's {url, auth} from its state file, or null when no service runs. */
|
|
12
|
+
export function serviceEndpoint(env = process.env) {
|
|
13
|
+
const state = env.XDG_STATE_HOME || join(env.HOME || homedir(), ".local", "state");
|
|
14
|
+
try {
|
|
15
|
+
const s = JSON.parse(readFileSync(join(state, "opencode", "service.json"), "utf8"));
|
|
16
|
+
if (typeof s.url !== "string" || !LOOPBACK.test(s.url))
|
|
17
|
+
return null;
|
|
18
|
+
const auth = typeof s.password === "string" && s.password ? `Basic ${Buffer.from(`opencode:${s.password}`).toString("base64")}` : "";
|
|
19
|
+
return { url: s.url.replace(/\/$/, ""), auth };
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** One request to the service: {ok, status, body} or {ok: false, status, error}. */
|
|
26
|
+
export async function opencodeRequest(method, path, { body, env = process.env, endpoint, fetchFn = fetch, timeoutMs = TIMEOUT_MS } = {}) {
|
|
27
|
+
const ep = endpoint === undefined ? serviceEndpoint(env) : endpoint;
|
|
28
|
+
if (!ep)
|
|
29
|
+
return { ok: false, status: null, error: "OpenCode service is not running" };
|
|
30
|
+
try {
|
|
31
|
+
const res = await fetchFn(ep.url + path, {
|
|
32
|
+
method,
|
|
33
|
+
headers: { ...(ep.auth ? { authorization: ep.auth } : {}), ...(body !== undefined ? { "content-type": "application/json" } : {}) },
|
|
34
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
35
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
36
|
+
});
|
|
37
|
+
const text = await res.text();
|
|
38
|
+
if (!res.ok)
|
|
39
|
+
return { ok: false, status: res.status, error: `${res.status} ${text.slice(0, 200)}` };
|
|
40
|
+
try {
|
|
41
|
+
return { ok: true, status: res.status, body: text ? JSON.parse(text) : null };
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return { ok: true, status: res.status, body: text };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return { ok: false, status: null, error: error.name === "TimeoutError" ? "timed out" : "request failed" };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Session ids with a running turn, or null when the service is unreachable. */
|
|
52
|
+
export async function activeOpencodeSessions(options) {
|
|
53
|
+
const r = await opencodeRequest("GET", "/api/session/active", options);
|
|
54
|
+
if (!r.ok)
|
|
55
|
+
return null;
|
|
56
|
+
return new Set(Object.keys(r.body?.data ?? r.body ?? {}));
|
|
57
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// OpenCode 2 plugin: maps OpenCode hooks and events to scripts/temmo-hook.mjs --host opencode (README, OpenCode).
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
export const PLUGIN_ID = "temmo";
|
|
6
|
+
export const HOOK_SCRIPT = fileURLToPath(new URL("../scripts/temmo-hook.mjs", import.meta.url));
|
|
7
|
+
// The session instruction entry the lesson note rides: a system block, not prompt text (docs/harness-improvement.md).
|
|
8
|
+
export const LESSONS_KEY = "temmo-lessons";
|
|
9
|
+
// Same limits as the Claude registrations in hooks/hooks.json.
|
|
10
|
+
export const HOOK_TIMEOUT_MS = { SessionStart: 20_000, UserPromptSubmit: 10_000, PostToolUse: 10_000, PermissionRequest: 600_000, Stop: 660_000 };
|
|
11
|
+
export const ACTIVITY_TIMEOUT_MS = 5_000;
|
|
12
|
+
export const EVENT_TYPES = ["session.text.ended", "session.step.ended", "session.compaction.started", "session.deleted"];
|
|
13
|
+
const MAX_SESSIONS = 256;
|
|
14
|
+
const MAX_DEPTH = 8;
|
|
15
|
+
const OUTPUT_MAX = 1 << 20;
|
|
16
|
+
const RESOURCES_MAX = 20;
|
|
17
|
+
/** Runs the shared hook once with one JSON input; resolves its JSON output, or {} on any failure, timeout, or abort. */
|
|
18
|
+
export function runHook(input, { activity = false, gate = false, timeoutMs = ACTIVITY_TIMEOUT_MS, signal, node = "node", script = HOOK_SCRIPT, env = process.env, host = "opencode" } = {}) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
let child;
|
|
21
|
+
try {
|
|
22
|
+
const args = [script, ...(gate ? ["--read-gate"] : []), ...(activity ? ["--activity"] : []), "--host", host];
|
|
23
|
+
child = spawn(node, args, { env, cwd: typeof input.cwd === "string" ? input.cwd : undefined, stdio: ["pipe", "pipe", "ignore"], shell: false });
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return resolve({});
|
|
27
|
+
}
|
|
28
|
+
let out = "";
|
|
29
|
+
// SIGTERM lets a waiting approval close its phone request before it exits.
|
|
30
|
+
const stop = () => child.kill("SIGTERM");
|
|
31
|
+
const timer = setTimeout(stop, timeoutMs);
|
|
32
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
33
|
+
if (signal?.aborted)
|
|
34
|
+
stop();
|
|
35
|
+
const done = () => { clearTimeout(timer); signal?.removeEventListener("abort", stop); };
|
|
36
|
+
child.stdout.on("data", (chunk) => { if (out.length < OUTPUT_MAX)
|
|
37
|
+
out += chunk; });
|
|
38
|
+
child.on("error", () => { done(); resolve({}); });
|
|
39
|
+
child.on("close", () => {
|
|
40
|
+
done();
|
|
41
|
+
try {
|
|
42
|
+
const value = JSON.parse(out.trim().split("\n").pop() || "{}");
|
|
43
|
+
resolve(value && typeof value === "object" && !Array.isArray(value) ? value : {});
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
resolve({});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
child.stdin.on("error", () => { });
|
|
50
|
+
child.stdin.end(JSON.stringify(input));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Context text a hook output asks to inject, or "". */
|
|
54
|
+
export function contextText(output) {
|
|
55
|
+
const text = output?.hookSpecificOutput?.additionalContext ?? output?.additional_context;
|
|
56
|
+
return typeof text === "string" ? text : "";
|
|
57
|
+
}
|
|
58
|
+
/** Appends text to a tool result in place; OpenCode takes a string or an array of content parts. */
|
|
59
|
+
export function appendContent(result, text) {
|
|
60
|
+
if (!result || typeof result !== "object" || !text)
|
|
61
|
+
return;
|
|
62
|
+
const content = result.content;
|
|
63
|
+
if (typeof content === "string")
|
|
64
|
+
result.content = `${content}\n\n${text}`;
|
|
65
|
+
else if (Array.isArray(content) && content.length)
|
|
66
|
+
result.content = [...content, { type: "text", text }];
|
|
67
|
+
else
|
|
68
|
+
result.content = [{ type: "text", text: typeof result.output === "string" ? result.output : JSON.stringify(result.output ?? "") }, { type: "text", text }];
|
|
69
|
+
}
|
|
70
|
+
/** A tool result for activity: its output, else its text parts. */
|
|
71
|
+
export function resultOutput(result) {
|
|
72
|
+
if (result?.output !== undefined)
|
|
73
|
+
return result.output;
|
|
74
|
+
const content = result?.content;
|
|
75
|
+
if (typeof content === "string")
|
|
76
|
+
return content;
|
|
77
|
+
return Array.isArray(content) ? content.filter((p) => p?.type === "text").map((p) => p.text).join("\n") : "";
|
|
78
|
+
}
|
|
79
|
+
/** Context tokens of one finished step (input plus cache reads and writes), or undefined. */
|
|
80
|
+
export function stepTokens(tokens) {
|
|
81
|
+
const parts = [tokens?.input, tokens?.cache?.read ?? 0, tokens?.cache?.write ?? 0];
|
|
82
|
+
return parts.every((n) => Number.isSafeInteger(n) && n >= 0) ? parts.reduce((a, b) => a + b, 0) : undefined;
|
|
83
|
+
}
|
|
84
|
+
/** A permission request's resources as tool input: one resource as a command, several as a list. */
|
|
85
|
+
export function permissionInput(ev) {
|
|
86
|
+
const resources = Array.isArray(ev?.resources) ? ev.resources.filter((r) => typeof r === "string").slice(0, RESOURCES_MAX) : [];
|
|
87
|
+
return resources.length === 1 ? { command: resources[0] } : { resources };
|
|
88
|
+
}
|
|
89
|
+
function unwrap(value) {
|
|
90
|
+
return value && typeof value === "object" && value.data && typeof value.data === "object" ? value.data : value;
|
|
91
|
+
}
|
|
92
|
+
/** The plugin object; run is injectable for tests. */
|
|
93
|
+
export function createPlugin({ run = runHook } = {}) {
|
|
94
|
+
async function setup(ctx) {
|
|
95
|
+
const directory = ctx?.location?.directory || process.cwd();
|
|
96
|
+
const sessions = new Map();
|
|
97
|
+
// Session facts (root, agent, title, folder), cached; a parent resolves before its child.
|
|
98
|
+
async function resolve(id, depth = 0) {
|
|
99
|
+
if (typeof id !== "string" || !id)
|
|
100
|
+
return null;
|
|
101
|
+
const known = sessions.get(id);
|
|
102
|
+
if (known)
|
|
103
|
+
return known.foreign ? null : known;
|
|
104
|
+
let info = null;
|
|
105
|
+
try {
|
|
106
|
+
info = unwrap(await ctx.session.get({ sessionID: id }));
|
|
107
|
+
}
|
|
108
|
+
catch { /* unknown: a root */ }
|
|
109
|
+
const parentID = typeof info?.parentID === "string" && info.parentID && depth < MAX_DEPTH ? info.parentID : "";
|
|
110
|
+
const parent = parentID ? await resolve(parentID, depth + 1) : null;
|
|
111
|
+
const entry = {
|
|
112
|
+
id, root: parent ? parent.root : id, child: Boolean(parent), agent: typeof info?.agent === "string" ? info.agent : "",
|
|
113
|
+
title: typeof info?.title === "string" ? info.title : "", cwd: info?.location?.directory || directory,
|
|
114
|
+
started: false, continued: false, tokens: undefined, lastText: "", stopping: null,
|
|
115
|
+
};
|
|
116
|
+
// Each OpenCode location loads its own plugin instance; another folder's session belongs to that instance.
|
|
117
|
+
entry.foreign = path.resolve(entry.cwd) !== path.resolve(directory);
|
|
118
|
+
sessions.set(id, entry);
|
|
119
|
+
while (sessions.size > MAX_SESSIONS)
|
|
120
|
+
sessions.delete(sessions.keys().next().value);
|
|
121
|
+
return entry.foreign ? null : entry;
|
|
122
|
+
}
|
|
123
|
+
// Claude-shaped hook input; a child session reports as an agent of its root.
|
|
124
|
+
function input(s, event, extra = {}) {
|
|
125
|
+
return {
|
|
126
|
+
hook_event_name: event, session_id: s.root, cwd: s.cwd,
|
|
127
|
+
...(s.title && !s.child ? { session_title: s.title } : {}),
|
|
128
|
+
...(s.child ? { agent_id: s.id, agent_type: s.agent } : {}),
|
|
129
|
+
...extra,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const tokens = (s) => (s.tokens !== undefined ? { context_tokens: s.tokens } : {});
|
|
133
|
+
const activity = (s, event, extra) => { void Promise.resolve(run(input(s, event, extra), { activity: true })).catch(() => { }); };
|
|
134
|
+
const call = (s, event, extra, options = {}) => Promise.resolve(run(input(s, event, extra), { timeoutMs: HOOK_TIMEOUT_MS[event], ...options })).catch(() => ({}));
|
|
135
|
+
async function onPrompt(ev) {
|
|
136
|
+
const s = await resolve(ev?.sessionID);
|
|
137
|
+
if (!s)
|
|
138
|
+
return;
|
|
139
|
+
if (s.child) {
|
|
140
|
+
if (!s.started) {
|
|
141
|
+
s.started = true;
|
|
142
|
+
activity(s, "SubagentStart");
|
|
143
|
+
}
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// A prompt typed during an AFK hold ends the hold, like a prompt queued in Claude's terminal.
|
|
147
|
+
s.stopping?.abort();
|
|
148
|
+
const texts = [];
|
|
149
|
+
if (!s.started) {
|
|
150
|
+
s.started = true;
|
|
151
|
+
const out = await call(s, "SessionStart", { source: "startup" });
|
|
152
|
+
texts.push(contextText(out));
|
|
153
|
+
const lessons = typeof out.lessons === "string" ? out.lessons.trim() : "";
|
|
154
|
+
if (lessons) {
|
|
155
|
+
// The lesson note rides the session's instructions, not the prompt; a service without instruction entries falls back to the prompt.
|
|
156
|
+
try {
|
|
157
|
+
await ctx.session.instructions.entry.put({ sessionID: s.id, key: LESSONS_KEY, value: lessons });
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
texts.push(lessons);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
s.continued = false;
|
|
165
|
+
const prompt = typeof ev.prompt?.text === "string" ? ev.prompt.text : "";
|
|
166
|
+
const turn = await call(s, "UserPromptSubmit", { prompt });
|
|
167
|
+
texts.push(contextText(turn));
|
|
168
|
+
// Mid-session harness-surface edits (AGENTS.local.md / memory) arrive in lessons on UserPromptSubmit.
|
|
169
|
+
const turnLessons = typeof turn.lessons === "string" ? turn.lessons.trim() : "";
|
|
170
|
+
if (turnLessons) {
|
|
171
|
+
try {
|
|
172
|
+
await ctx.session.instructions.entry.put({ sessionID: s.id, key: LESSONS_KEY, value: turnLessons });
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
texts.push(turnLessons);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const extra = texts.filter(Boolean).join("\n");
|
|
179
|
+
if (extra && ev.prompt && typeof ev.prompt === "object")
|
|
180
|
+
ev.prompt.text = prompt ? `${prompt}\n\n${extra}` : extra;
|
|
181
|
+
}
|
|
182
|
+
async function onIdle(ev) {
|
|
183
|
+
const s = await resolve(ev?.sessionID);
|
|
184
|
+
if (!s)
|
|
185
|
+
return;
|
|
186
|
+
if (s.child)
|
|
187
|
+
return activity(s, "SubagentStop", { last_assistant_message: s.lastText });
|
|
188
|
+
const stopping = new AbortController();
|
|
189
|
+
s.stopping = stopping;
|
|
190
|
+
let out;
|
|
191
|
+
try {
|
|
192
|
+
out = await call(s, "Stop", { stop_hook_active: s.continued, ...tokens(s) }, { signal: stopping.signal });
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
if (s.stopping === stopping)
|
|
196
|
+
s.stopping = null;
|
|
197
|
+
}
|
|
198
|
+
const reason = !stopping.signal.aborted && out?.decision === "block" && typeof out.reason === "string" ? out.reason : "";
|
|
199
|
+
s.continued = Boolean(reason);
|
|
200
|
+
// OpenCode keeps the session running when input is waiting after this hook.
|
|
201
|
+
if (reason)
|
|
202
|
+
await ctx.session.synthetic({ sessionID: s.id, text: reason, description: "Temmo", metadata: { source: PLUGIN_ID } });
|
|
203
|
+
}
|
|
204
|
+
async function onToolBefore(ev) {
|
|
205
|
+
const s = await resolve(ev?.sessionID);
|
|
206
|
+
if (!s)
|
|
207
|
+
return;
|
|
208
|
+
const extra = { tool_name: ev.tool, tool_input: ev.input, tool_use_id: ev.id };
|
|
209
|
+
activity(s, "PreToolUse", extra);
|
|
210
|
+
const out = await run(input(s, "PreToolUse", extra), { gate: true, timeoutMs: ACTIVITY_TIMEOUT_MS });
|
|
211
|
+
const decision = out?.hookSpecificOutput;
|
|
212
|
+
if (decision?.permissionDecision === "deny") {
|
|
213
|
+
const error = new Error(typeof decision.permissionDecisionReason === "string" ? decision.permissionDecisionReason : "direct read refused");
|
|
214
|
+
error.storeRead = true;
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async function onToolAfter(ev) {
|
|
219
|
+
const s = await resolve(ev?.sessionID);
|
|
220
|
+
if (!s)
|
|
221
|
+
return;
|
|
222
|
+
const base = { tool_name: ev.tool, tool_input: ev.input, tool_use_id: ev.id };
|
|
223
|
+
if (ev.status === "error") {
|
|
224
|
+
const error = ev.error?.message ?? ev.error;
|
|
225
|
+
return activity(s, "PostToolUseFailure", { ...base, error: typeof error === "string" ? error : JSON.stringify(error ?? "tool failed") });
|
|
226
|
+
}
|
|
227
|
+
const extra = { ...base, tool_response: resultOutput(ev.result), ...tokens(s) };
|
|
228
|
+
// A subagent's tool results only publish activity; messages reach the root session.
|
|
229
|
+
if (s.child)
|
|
230
|
+
return activity(s, "PostToolUse", extra);
|
|
231
|
+
const text = contextText(await call(s, "PostToolUse", extra));
|
|
232
|
+
if (text)
|
|
233
|
+
appendContent(ev.result, text);
|
|
234
|
+
}
|
|
235
|
+
async function onPermission(ev) {
|
|
236
|
+
if (ev?.effect !== "ask")
|
|
237
|
+
return;
|
|
238
|
+
const s = await resolve(ev.sessionID);
|
|
239
|
+
if (!s)
|
|
240
|
+
return;
|
|
241
|
+
const out = await call(s, "PermissionRequest", { tool_name: String(ev.action || "permission"), tool_input: permissionInput(ev) });
|
|
242
|
+
const decision = out?.hookSpecificOutput?.decision;
|
|
243
|
+
if (decision?.behavior === "allow")
|
|
244
|
+
ev.effect = "allow";
|
|
245
|
+
else if (decision?.behavior === "deny") {
|
|
246
|
+
ev.effect = "deny";
|
|
247
|
+
if (typeof decision.message === "string")
|
|
248
|
+
ev.message = decision.message;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async function onEvent(event) {
|
|
252
|
+
const where = event?.location?.directory;
|
|
253
|
+
if (where && path.resolve(where) !== path.resolve(directory))
|
|
254
|
+
return;
|
|
255
|
+
const data = event?.data || event?.properties || {};
|
|
256
|
+
const s = event?.type === "session.deleted" ? sessions.get(data.sessionID) : await resolve(data.sessionID);
|
|
257
|
+
if (!s || s.foreign)
|
|
258
|
+
return;
|
|
259
|
+
switch (event.type) {
|
|
260
|
+
case "session.text.ended":
|
|
261
|
+
if (typeof data.text === "string" && data.text.trim()) {
|
|
262
|
+
s.lastText = data.text;
|
|
263
|
+
activity(s, "AfterAgentResponse", { text: data.text });
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
case "session.step.ended":
|
|
267
|
+
s.tokens = stepTokens(data.tokens) ?? s.tokens;
|
|
268
|
+
break;
|
|
269
|
+
case "session.compaction.started":
|
|
270
|
+
activity(s, "PreCompact", { trigger: data.reason, ...tokens(s) });
|
|
271
|
+
break;
|
|
272
|
+
case "session.deleted":
|
|
273
|
+
if (!s.child)
|
|
274
|
+
activity(s, "SessionEnd", { reason: "deleted" });
|
|
275
|
+
sessions.delete(s.id);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const guard = (fn) => async (ev) => { try {
|
|
280
|
+
await fn(ev);
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
if (error?.storeRead)
|
|
284
|
+
throw error;
|
|
285
|
+
} };
|
|
286
|
+
const register = async (domain, name, fn) => {
|
|
287
|
+
try {
|
|
288
|
+
await ctx[domain]?.hook?.(name, guard(fn));
|
|
289
|
+
}
|
|
290
|
+
catch { /* this release lacks the hook */ }
|
|
291
|
+
};
|
|
292
|
+
await register("session", "prompt", onPrompt);
|
|
293
|
+
await register("session", "experimental.idle", onIdle);
|
|
294
|
+
await register("tool", "execute.before", onToolBefore);
|
|
295
|
+
await register("tool", "execute.after", onToolAfter);
|
|
296
|
+
await register("permission", "evaluate", onPermission);
|
|
297
|
+
const controller = new AbortController();
|
|
298
|
+
if (typeof ctx?.event?.subscribe === "function") {
|
|
299
|
+
void (async () => {
|
|
300
|
+
try {
|
|
301
|
+
for await (const event of ctx.event.subscribe({ types: EVENT_TYPES, signal: controller.signal })) {
|
|
302
|
+
if (EVENT_TYPES.includes(event?.type))
|
|
303
|
+
void guard(onEvent)(event);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
catch { /* the stream ended */ }
|
|
307
|
+
})();
|
|
308
|
+
}
|
|
309
|
+
return () => controller.abort();
|
|
310
|
+
}
|
|
311
|
+
return { id: PLUGIN_ID, setup };
|
|
312
|
+
}
|
|
313
|
+
export default createPlugin();
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operator token: a second bus credential that only the UI server (the phone's backend) holds. Agents keep the
|
|
3
|
+
* shared bus token. The bus stamps an event `verified` only when it was posted with this token, so an operator message
|
|
4
|
+
* or answer that reaches an agent as [operator-verified] came through the UI, not from another agent claiming
|
|
5
|
+
* from=operator (docs/security.md#operator-verified-messages).
|
|
6
|
+
*
|
|
7
|
+
* The token lives only in a 0600 file next to bus-token; it is never put in an environment variable, a spawn env, a
|
|
8
|
+
* sandbox upload or an OpenShell policy. isOperatorCredential is the one place that decides "this request is the
|
|
9
|
+
* operator": a GitHub login checked against a trust file can replace its body later.
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
12
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import { configDir } from "./config.js";
|
|
15
|
+
/** The operator token's file: operator-token beside bus-token in the config dir. */
|
|
16
|
+
export function operatorTokenFile(env = process.env) {
|
|
17
|
+
return path.join(configDir(env), "operator-token");
|
|
18
|
+
}
|
|
19
|
+
/** The operator token, or "" when the file is missing or empty. */
|
|
20
|
+
export function readOperatorToken(env = process.env) {
|
|
21
|
+
try {
|
|
22
|
+
return readFileSync(operatorTokenFile(env), "utf8").trim();
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Reads the operator token, creating it (mode 0600, exclusive create) on first use; the bus, the UI and setup all call this. */
|
|
29
|
+
export function ensureOperatorToken(env = process.env) {
|
|
30
|
+
const existing = readOperatorToken(env);
|
|
31
|
+
if (existing)
|
|
32
|
+
return existing;
|
|
33
|
+
const file = operatorTokenFile(env);
|
|
34
|
+
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
35
|
+
try {
|
|
36
|
+
writeFileSync(file, `op_${randomBytes(24).toString("hex")}\n`, { mode: 0o600, flag: "wx" });
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// Another process created it first: read theirs.
|
|
40
|
+
if (error.code !== "EEXIST")
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
const token = readOperatorToken(env);
|
|
44
|
+
if (!token)
|
|
45
|
+
throw new Error(`operator token: ${file} is empty`);
|
|
46
|
+
return token;
|
|
47
|
+
}
|
|
48
|
+
/** The seam that decides "this request is the operator": today, a Bearer header carrying the operator token. */
|
|
49
|
+
export function isOperatorCredential(authorization, operatorToken) {
|
|
50
|
+
if (!operatorToken || typeof authorization !== "string")
|
|
51
|
+
return false;
|
|
52
|
+
const got = Buffer.from(authorization);
|
|
53
|
+
const want = Buffer.from(`Bearer ${operatorToken}`);
|
|
54
|
+
return got.length === want.length && timingSafeEqual(got, want);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The UI server's project configs: a project on the local bus (its token is the shared bus-token from defaults.json)
|
|
58
|
+
* gets operatorToken, which lib/messaging.ts sends in place of token. token stays, so the UI's history and hub keys
|
|
59
|
+
* do not change. Without the operator-token file the configs pass through unchanged and operator posts stay
|
|
60
|
+
* unverified. Hosted projects carry their own tokens and are never passed here.
|
|
61
|
+
*/
|
|
62
|
+
export function withOperatorToken(projects, env = process.env) {
|
|
63
|
+
const operatorToken = readOperatorToken(env);
|
|
64
|
+
const shared = sharedBusToken(env);
|
|
65
|
+
if (!operatorToken || !shared)
|
|
66
|
+
return projects;
|
|
67
|
+
return projects.map((cfg) => (cfg.token === shared ? { ...cfg, operatorToken } : cfg));
|
|
68
|
+
}
|
|
69
|
+
// The shared bus token every agent holds: the tokenFile named in defaults.json.
|
|
70
|
+
function sharedBusToken(env) {
|
|
71
|
+
try {
|
|
72
|
+
const defaults = JSON.parse(readFileSync(path.join(configDir(env), "defaults.json"), "utf8"));
|
|
73
|
+
return typeof defaults?.tokenFile === "string" && path.isAbsolute(defaults.tokenFile) ? readFileSync(defaults.tokenFile, "utf8").trim() : "";
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
}
|