@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,324 @@
|
|
|
1
|
+
/** Patterns: storage, listing and saving. Sources merge as built-in, global, other project, project; legacy teams fill the rest. */
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { configDir, isValidName } from "../config.js";
|
|
6
|
+
import { readJson, writeFileAtomic } from "../store.js";
|
|
7
|
+
import { parseDot, serializeDot } from "./dot.js";
|
|
8
|
+
import { deleteTeam, listTeams, validateMember } from "./teams.js";
|
|
9
|
+
import { entryPatterns, entryTeams, findProject, readProjectsFile, requireProject, updateProject, updateProjects } from "./projects.js";
|
|
10
|
+
import { globalTeamsDir, isRegularFile, migratedNames, projectTeamsDir, readTeamFiles, recordMigrated, saveTeamFile, teamFilePath } from "./team-files.js";
|
|
11
|
+
import { checkNodePrompts, legacyPattern, patternFromTeam, reservedNames, teamFromPattern, validatePattern, withExtras } from "./pattern-model.js";
|
|
12
|
+
const BUILT_IN_DIR = fileURLToPath(new URL("../team-templates/", import.meta.url));
|
|
13
|
+
// Shipped team templates, read from lib/team-templates/<name>/team.dot. Harnesses and models are defaults; `pattern create` with the same name overrides them.
|
|
14
|
+
// Test and tool use only: callers list patterns through listPatterns.
|
|
15
|
+
export const BUILT_IN_PATTERNS = Object.fromEntries(readTeamFiles(BUILT_IN_DIR).map(({ name, text }) => {
|
|
16
|
+
const team = parseDot(text);
|
|
17
|
+
return [name, withExtras(validatePattern(patternFromTeam(team)), team)];
|
|
18
|
+
}));
|
|
19
|
+
function patternsFile(env) {
|
|
20
|
+
return join(configDir(env), "patterns.json");
|
|
21
|
+
}
|
|
22
|
+
function readGlobalPatterns(env) {
|
|
23
|
+
const value = readJson(patternsFile(env));
|
|
24
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
25
|
+
}
|
|
26
|
+
/** The entry under name, or undefined. Own keys only, so "constructor" and "__proto__" find nothing. */
|
|
27
|
+
function entryOf(map, name) {
|
|
28
|
+
return Object.hasOwn(map, name) ? map[name] : undefined;
|
|
29
|
+
}
|
|
30
|
+
/** Team files in other registered projects: this project's own files are not included. */
|
|
31
|
+
function otherTeamFiles(cfg, env) {
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const entry of readProjectsFile(env)) {
|
|
34
|
+
if (!entry || entry.project === cfg.project || typeof entry.root !== "string" || !entry.root)
|
|
35
|
+
continue;
|
|
36
|
+
out.push(...readTeamFiles(projectTeamsDir(entry.root)));
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
/** Another project's team, included only when exactly one other project has that name. */
|
|
41
|
+
function uniqueOtherTeams(cfg, env) {
|
|
42
|
+
const grouped = new Map();
|
|
43
|
+
for (const item of otherTeamFiles(cfg, env)) {
|
|
44
|
+
const list = grouped.get(item.name) ?? [];
|
|
45
|
+
list.push(item);
|
|
46
|
+
grouped.set(item.name, list);
|
|
47
|
+
}
|
|
48
|
+
return [...grouped.values()].filter((items) => items.length === 1).map((items) => items[0]);
|
|
49
|
+
}
|
|
50
|
+
/** Every pattern name visible to a project, valid or not; teams check against this. */
|
|
51
|
+
export function allPatternNames(cfg, env = process.env) {
|
|
52
|
+
return [...new Set([
|
|
53
|
+
...Object.keys(BUILT_IN_PATTERNS),
|
|
54
|
+
...Object.keys(readGlobalPatterns(env)),
|
|
55
|
+
...Object.keys(entryPatterns(findProject(env, cfg.project))),
|
|
56
|
+
...teamFilesOf(cfg, env).map((item) => item.name),
|
|
57
|
+
...otherTeamFiles(cfg, env).map((item) => item.name),
|
|
58
|
+
])];
|
|
59
|
+
}
|
|
60
|
+
/** This project's root from projects.json, or "" when the project has none. */
|
|
61
|
+
function projectRoot(env, project) {
|
|
62
|
+
const root = findProject(env, project)?.root;
|
|
63
|
+
return typeof root === "string" ? root : "";
|
|
64
|
+
}
|
|
65
|
+
/** Team files a project sees, global then project. */
|
|
66
|
+
function teamFilesOf(cfg, env) {
|
|
67
|
+
const root = projectRoot(env, cfg.project);
|
|
68
|
+
return [
|
|
69
|
+
...readTeamFiles(globalTeamsDir(env)).map((item) => ({ ...item, source: "global" })),
|
|
70
|
+
...(root ? readTeamFiles(projectTeamsDir(root)).map((item) => ({ ...item, source: "project" })) : []),
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
/** Converts each global patterns.json entry to a team.dot once. patterns.json stays; a converted name is never converted again. */
|
|
74
|
+
function migrateGlobalPatterns(env) {
|
|
75
|
+
const dir = globalTeamsDir(env);
|
|
76
|
+
const done = migratedNames(dir);
|
|
77
|
+
const pending = Object.entries(readGlobalPatterns(env)).filter(([name]) => !done.includes(name));
|
|
78
|
+
if (!pending.length)
|
|
79
|
+
return;
|
|
80
|
+
const names = [...done];
|
|
81
|
+
for (const [name, raw] of pending) {
|
|
82
|
+
names.push(name);
|
|
83
|
+
const file = teamFilePath(dir, name);
|
|
84
|
+
if (existsSync(file))
|
|
85
|
+
continue;
|
|
86
|
+
let pattern;
|
|
87
|
+
try {
|
|
88
|
+
pattern = validatePattern({ ...raw, name });
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
writeFileAtomic(file, serializeDot(teamFromPattern(pattern)));
|
|
94
|
+
}
|
|
95
|
+
recordMigrated(dir, names);
|
|
96
|
+
}
|
|
97
|
+
/** A team file as a pattern entry; a file that fails to parse or validate carries `invalid` with its line. */
|
|
98
|
+
function checkedTeamEntry({ name, file, text, source }, reserved) {
|
|
99
|
+
try {
|
|
100
|
+
const team = parseDot(text);
|
|
101
|
+
if (team.name !== name)
|
|
102
|
+
throw new Error(`digraph name "${team.name}" must match the folder "${name}"`);
|
|
103
|
+
checkNodePrompts(team);
|
|
104
|
+
return { ...withExtras(validatePattern(patternFromTeam(team), reserved), team), source, file };
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
return { name, source, file, invalid: error.message, roles: [], handoffs: [] };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function checkedEntry(name, raw, source, reserved) {
|
|
111
|
+
try {
|
|
112
|
+
return { ...validatePattern({ ...raw, name }, reserved), source };
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return { name, source, invalid: error.message, roles: [], handoffs: [] };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Merged map: built-in, then global, then another project's team, then this project's patterns and team files. Legacy teams fill names no pattern uses. Invalid saved patterns carry `invalid`. */
|
|
119
|
+
export function listPatterns(cfg, env = process.env) {
|
|
120
|
+
try {
|
|
121
|
+
migrateGlobalPatterns(env);
|
|
122
|
+
}
|
|
123
|
+
catch { /* a failed conversion leaves patterns.json as the source; the next call retries */ }
|
|
124
|
+
const reserved = reservedNames(cfg);
|
|
125
|
+
const files = teamFilesOf(cfg, env);
|
|
126
|
+
const out = {};
|
|
127
|
+
for (const [name, pattern] of Object.entries(BUILT_IN_PATTERNS))
|
|
128
|
+
out[name] = { ...pattern, source: "built-in" };
|
|
129
|
+
for (const [name, raw] of Object.entries(readGlobalPatterns(env)))
|
|
130
|
+
out[name] = checkedEntry(name, raw, "global", reserved);
|
|
131
|
+
for (const item of files)
|
|
132
|
+
if (item.source === "global")
|
|
133
|
+
out[item.name] = checkedTeamEntry({ ...item, source: "global" }, reserved);
|
|
134
|
+
// Another project's team is copyable here. A team file in this project, written below, shadows it.
|
|
135
|
+
for (const item of uniqueOtherTeams(cfg, env)) {
|
|
136
|
+
if (!Object.hasOwn(out, item.name))
|
|
137
|
+
out[item.name] = checkedTeamEntry({ ...item, source: "other" }, reserved);
|
|
138
|
+
}
|
|
139
|
+
for (const [name, raw] of Object.entries(entryPatterns(findProject(env, cfg.project))))
|
|
140
|
+
out[name] = checkedEntry(name, raw, "project", reserved);
|
|
141
|
+
for (const item of files)
|
|
142
|
+
if (item.source === "project")
|
|
143
|
+
out[item.name] = checkedTeamEntry({ ...item, source: "project" }, reserved);
|
|
144
|
+
for (const [name, members] of Object.entries(listTeams(cfg, env))) {
|
|
145
|
+
if (!out[name])
|
|
146
|
+
out[name] = { ...legacyPattern(name, members), source: "team" };
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
export function getPattern(cfg, name, env = process.env) {
|
|
151
|
+
const pattern = entryOf(listPatterns(cfg, env), name);
|
|
152
|
+
if (!pattern)
|
|
153
|
+
throw new Error(`unknown pattern "${name}"`);
|
|
154
|
+
if (pattern.invalid)
|
|
155
|
+
throw new Error(`pattern "${name}" is invalid: ${pattern.invalid}`);
|
|
156
|
+
return pattern;
|
|
157
|
+
}
|
|
158
|
+
/** Checks every role's harness and model against the allowlists (lib/models.ts output). */
|
|
159
|
+
export async function validatePatternModels(pattern, cfg, env = process.env, models) {
|
|
160
|
+
for (const role of pattern.roles) {
|
|
161
|
+
await validateMember(role.model ? `${role.harness}:${role.model}` : role.harness, cfg, env, models);
|
|
162
|
+
}
|
|
163
|
+
return pattern;
|
|
164
|
+
}
|
|
165
|
+
/** Saves a pattern to the project's projects.json entry; the same name as a team is refused. */
|
|
166
|
+
export async function createPattern(cfg, raw, env = process.env, models) {
|
|
167
|
+
const pattern = validatePattern(raw, reservedNames(cfg));
|
|
168
|
+
if (entryOf(listTeams(cfg, env), pattern.name))
|
|
169
|
+
throw new Error(`"${pattern.name}" is a team name; pick another`);
|
|
170
|
+
await validatePatternModels(pattern, cfg, env, models);
|
|
171
|
+
return updateProject(env, cfg.project, (entry) => {
|
|
172
|
+
if (teamFilesOf(cfg, env).some((item) => item.source === "project" && item.name === pattern.name)) {
|
|
173
|
+
throw new Error(`"${pattern.name}" is a project team file; edit its team.dot instead`);
|
|
174
|
+
}
|
|
175
|
+
return { entry: { ...entry, patterns: { ...entryPatterns(entry), [pattern.name]: pattern } }, result: pattern };
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/** Parses team.dot text as the team named name: the parsed team and its checked pattern. Throws with the line of a DOT error. */
|
|
179
|
+
function teamOf(cfg, name, text) {
|
|
180
|
+
if (!isValidName(name))
|
|
181
|
+
throw new Error("pattern name must be 1-24 chars of a-z, 0-9 and single hyphens");
|
|
182
|
+
const team = parseDot(text);
|
|
183
|
+
if (team.name !== name)
|
|
184
|
+
throw new Error(`digraph name "${team.name}" must match "${name}"`);
|
|
185
|
+
checkNodePrompts(team);
|
|
186
|
+
return { team, pattern: withExtras(validatePattern(patternFromTeam(team), reservedNames(cfg)), team) };
|
|
187
|
+
}
|
|
188
|
+
/** Checks team.dot text without saving it, for the editor's live preview. */
|
|
189
|
+
export function checkTeamText(cfg, name, text) {
|
|
190
|
+
try {
|
|
191
|
+
const { team, pattern } = teamOf(cfg, name, text);
|
|
192
|
+
return { ok: true, pattern, warnings: team.warnings };
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
return { ok: false, error: error.message, line: error.line ?? null };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Saves team.dot text as a project team: parses and checks it, then writes the canonical text under the file's lock.
|
|
200
|
+
* The team's name is its folder and its digraph name; a project team shadows a global or built-in pattern of the same name.
|
|
201
|
+
* Lock order: projects.json first, then the team file.
|
|
202
|
+
*/
|
|
203
|
+
export async function saveTeamText(cfg, name, text, env = process.env, models, { create = false } = {}) {
|
|
204
|
+
const root = projectRoot(env, cfg.project);
|
|
205
|
+
if (!root)
|
|
206
|
+
throw new Error("Project is not registered in projects.json");
|
|
207
|
+
const { pattern } = teamOf(cfg, name, text);
|
|
208
|
+
if (entryOf(listTeams(cfg, env), name))
|
|
209
|
+
throw new Error(`"${name}" is a team name; pick another`);
|
|
210
|
+
await validatePatternModels(pattern, cfg, env, models);
|
|
211
|
+
const file = teamFilePath(projectTeamsDir(root), name);
|
|
212
|
+
// The text is written as the operator wrote it, comments and layout included; the parse above checked it.
|
|
213
|
+
const body = text.endsWith("\n") ? text : `${text}\n`;
|
|
214
|
+
// The name check and the file write share the projects.json lock, so two creates of one name cannot both pass the check.
|
|
215
|
+
updateProjects(env, (projects) => {
|
|
216
|
+
if (create && entryOf(listPatterns(cfg, env), name))
|
|
217
|
+
throw new Error(`"${name}" already exists; pick another name or edit it`);
|
|
218
|
+
saveTeamFile(file, body);
|
|
219
|
+
// A JSON pattern of the same name is replaced by this file; drop its entry so it is not orphaned.
|
|
220
|
+
const current = entryPatterns(projects.find((item) => item?.project === cfg.project));
|
|
221
|
+
if (!entryOf(current, name))
|
|
222
|
+
return { result: undefined };
|
|
223
|
+
const patterns = { ...current };
|
|
224
|
+
delete patterns[name];
|
|
225
|
+
return { projects: projects.map((item) => (item?.project === cfg.project ? { ...item, patterns } : item)), result: undefined };
|
|
226
|
+
});
|
|
227
|
+
return { ...pattern, source: "project", file };
|
|
228
|
+
}
|
|
229
|
+
/** The shipped team templates the editor starts a new team from: [{ name, text }]. */
|
|
230
|
+
export function teamTemplates() {
|
|
231
|
+
return readTeamFiles(BUILT_IN_DIR).map(({ name, text }) => ({ name, text }));
|
|
232
|
+
}
|
|
233
|
+
/** The DOT text an editor shows for a listed pattern: a team file's own text, else the canonical text of the pattern. "" when neither exists. */
|
|
234
|
+
export function teamTextOf(entry) {
|
|
235
|
+
if (entry.file)
|
|
236
|
+
return isRegularFile(entry.file) ? readFileSync(entry.file, "utf8") : "";
|
|
237
|
+
if (entry.invalid || !entry.roles?.length)
|
|
238
|
+
return "";
|
|
239
|
+
return serializeDot(teamFromPattern(entry));
|
|
240
|
+
}
|
|
241
|
+
/** Deletes a project pattern; global and built-in patterns stay. */
|
|
242
|
+
export function deletePattern(cfg, name, env = process.env) {
|
|
243
|
+
const pattern = entryOf(listPatterns(cfg, env), name);
|
|
244
|
+
if (!pattern)
|
|
245
|
+
throw new Error(`unknown pattern "${name}"`);
|
|
246
|
+
if (pattern.file)
|
|
247
|
+
throw new Error(`team "${name}" is a team file; delete ${pattern.file} by hand`);
|
|
248
|
+
if (pattern.source !== "project")
|
|
249
|
+
throw new Error(`pattern "${name}" is ${pattern.source}; remove it from its file`);
|
|
250
|
+
return updateProject(env, cfg.project, (entry) => {
|
|
251
|
+
const local = { ...entryPatterns(entry) };
|
|
252
|
+
delete local[name];
|
|
253
|
+
return { entry: { ...entry, patterns: local }, result: { deleted: name } };
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Saves a pattern from the builder. With no `replace`, creates it (createPattern). With `replace` (the name the editor opened),
|
|
258
|
+
* updates that project pattern in place, or converts that project team to a pattern; a new name renames it in the same write.
|
|
259
|
+
*/
|
|
260
|
+
export async function savePattern(cfg, raw, { replace = "" } = {}, env = process.env, models) {
|
|
261
|
+
if (!replace)
|
|
262
|
+
return createPattern(cfg, raw, env, models);
|
|
263
|
+
const pattern = validatePattern(raw, reservedNames(cfg));
|
|
264
|
+
const all = listPatterns(cfg, env);
|
|
265
|
+
const old = entryOf(all, replace);
|
|
266
|
+
if (!old)
|
|
267
|
+
throw new Error(`unknown pattern "${replace}"`);
|
|
268
|
+
if (old.source === "built-in" || old.source === "global")
|
|
269
|
+
throw new Error(`${old.source} pattern "${replace}" is read-only; copy it to the project`);
|
|
270
|
+
if (old.file)
|
|
271
|
+
throw new Error(`team "${replace}" is a team file; save its team.dot text with saveTeamText`);
|
|
272
|
+
const entry = requireProject(env, cfg.project);
|
|
273
|
+
if (old.source === "team" && !Object.hasOwn(entryTeams(entry), replace))
|
|
274
|
+
throw new Error(`team "${replace}" is global; edit it in teams.json`);
|
|
275
|
+
if (pattern.name !== replace && entryOf(all, pattern.name))
|
|
276
|
+
throw new Error(`"${pattern.name}" already exists; pick another name`);
|
|
277
|
+
await validatePatternModels(pattern, cfg, env, models);
|
|
278
|
+
return updateProject(env, cfg.project, (current) => {
|
|
279
|
+
const patterns = { ...entryPatterns(current) };
|
|
280
|
+
const teams = { ...entryTeams(current) };
|
|
281
|
+
if (old.source === "project")
|
|
282
|
+
delete patterns[replace];
|
|
283
|
+
else
|
|
284
|
+
delete teams[replace];
|
|
285
|
+
patterns[pattern.name] = pattern;
|
|
286
|
+
return { entry: { ...current, patterns, teams }, result: pattern };
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/** Names removePattern accepts in this project: its patterns and its teams (global teams live in teams.json). */
|
|
290
|
+
export function removableNames(cfg, env = process.env) {
|
|
291
|
+
const entry = findProject(env, cfg.project);
|
|
292
|
+
return new Set([...Object.keys(entryPatterns(entry)), ...Object.keys(entryTeams(entry))]);
|
|
293
|
+
}
|
|
294
|
+
/** Deletes a project pattern or project team; built-in and global entries stay read-only. */
|
|
295
|
+
export function removePattern(cfg, name, env = process.env) {
|
|
296
|
+
const pattern = entryOf(listPatterns(cfg, env), name);
|
|
297
|
+
if (pattern?.source === "team")
|
|
298
|
+
return deleteTeam(cfg, name, env);
|
|
299
|
+
return deletePattern(cfg, name, env);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Copies a built-in, global, or another project's team into this project under the same name.
|
|
303
|
+
* The copy is a team file in this project. The source file is read and not written.
|
|
304
|
+
*/
|
|
305
|
+
export async function copyPattern(cfg, name, env = process.env, models) {
|
|
306
|
+
let source = entryOf(listPatterns(cfg, env), name);
|
|
307
|
+
if (!source) {
|
|
308
|
+
const found = otherTeamFiles(cfg, env).filter((item) => item.name === name);
|
|
309
|
+
if (found.length > 1)
|
|
310
|
+
throw new Error(`pattern "${name}" is in more than one project`);
|
|
311
|
+
if (found.length === 1)
|
|
312
|
+
source = checkedTeamEntry({ ...found[0], source: "other" }, reservedNames(cfg));
|
|
313
|
+
}
|
|
314
|
+
if (!source)
|
|
315
|
+
throw new Error(`unknown pattern "${name}"`);
|
|
316
|
+
if (source.source !== "built-in" && source.source !== "global" && source.source !== "other")
|
|
317
|
+
throw new Error(`pattern "${name}" is already ${source.source}`);
|
|
318
|
+
if (source.invalid)
|
|
319
|
+
throw new Error(`pattern "${name}" is invalid: ${source.invalid}`);
|
|
320
|
+
// A team file copies as its DOT text, which keeps every field; a JSON pattern copies its shape.
|
|
321
|
+
if (source.file)
|
|
322
|
+
return saveTeamText(cfg, name, readFileSync(source.file, "utf8"), env, models);
|
|
323
|
+
return createPattern(cfg, { name, ...(source.description ? { description: source.description } : {}), roles: source.roles, handoffs: source.handoffs }, env, models);
|
|
324
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** projects.json: the registered projects with their teams and patterns. Every write holds the projects.json lock. */
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { configDir } from "../config.js";
|
|
4
|
+
import { readJson, withLockSync, writeJson } from "../store.js";
|
|
5
|
+
const NOT_REGISTERED = "Project is not registered in projects.json";
|
|
6
|
+
const LOCK_WAIT_MS = 2000;
|
|
7
|
+
function projectsFile(env) {
|
|
8
|
+
return join(configDir(env), "projects.json");
|
|
9
|
+
}
|
|
10
|
+
/** The object at value, or {} when it is missing, an array, or not an object. */
|
|
11
|
+
export function plainMap(value) {
|
|
12
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
13
|
+
}
|
|
14
|
+
/** The entry's team map: name to member strings. */
|
|
15
|
+
export function entryTeams(entry) {
|
|
16
|
+
return plainMap(entry?.teams);
|
|
17
|
+
}
|
|
18
|
+
/** The entry's pattern map: name to pattern. */
|
|
19
|
+
export function entryPatterns(entry) {
|
|
20
|
+
return plainMap(entry?.patterns);
|
|
21
|
+
}
|
|
22
|
+
export function readProjectsFile(env) {
|
|
23
|
+
const value = readJson(projectsFile(env));
|
|
24
|
+
return Array.isArray(value) ? value : [];
|
|
25
|
+
}
|
|
26
|
+
/** Writes projects.json through a temp file and rename, indented two spaces. Callers hold the lock. */
|
|
27
|
+
export function writeProjectsFile(env, projects) {
|
|
28
|
+
writeJson(projectsFile(env), projects, { indent: 2 });
|
|
29
|
+
}
|
|
30
|
+
/** Runs fn holding the projects.json lock; throws when another writer holds it for 2 s. */
|
|
31
|
+
function underProjectsLockSync(env, fn) {
|
|
32
|
+
const locked = withLockSync(projectsFile(env), fn, { waitMs: LOCK_WAIT_MS });
|
|
33
|
+
if (!locked.locked)
|
|
34
|
+
throw new Error("projects.json is busy; retry");
|
|
35
|
+
return locked.value;
|
|
36
|
+
}
|
|
37
|
+
/** Read-modify-write of the whole list under the lock: fn returns the next list (omit it to skip the write) and a result. */
|
|
38
|
+
export function updateProjects(env, fn) {
|
|
39
|
+
return underProjectsLockSync(env, () => {
|
|
40
|
+
const change = fn(readProjectsFile(env));
|
|
41
|
+
if (change.projects)
|
|
42
|
+
writeProjectsFile(env, change.projects);
|
|
43
|
+
return change.result;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/** Read-modify-write of one registered entry under the lock; a missing entry throws, with `status` when given. */
|
|
47
|
+
export function updateProject(env, project, change, { status } = {}) {
|
|
48
|
+
return updateProjects(env, (projects) => {
|
|
49
|
+
const index = projects.findIndex((entry) => entry?.project === project);
|
|
50
|
+
if (index < 0)
|
|
51
|
+
throw Object.assign(new Error(NOT_REGISTERED), status ? { status } : {});
|
|
52
|
+
const { entry, result } = change(projects[index]);
|
|
53
|
+
const next = projects.slice();
|
|
54
|
+
next[index] = entry;
|
|
55
|
+
return { projects: next, result };
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
export function findProject(env, project) {
|
|
59
|
+
return readProjectsFile(env).find((entry) => entry?.project === project);
|
|
60
|
+
}
|
|
61
|
+
/** The registered entry; throws when the project is not in projects.json. */
|
|
62
|
+
export function requireProject(env, project) {
|
|
63
|
+
const entry = findProject(env, project);
|
|
64
|
+
if (!entry)
|
|
65
|
+
throw new Error(NOT_REGISTERED);
|
|
66
|
+
return entry;
|
|
67
|
+
}
|
|
68
|
+
/** Marks a registered project closed or open; its repo, worktrees and bus history stay. */
|
|
69
|
+
export function setProjectClosed(env, project, closed) {
|
|
70
|
+
updateProject(env, project, (entry) => ({ entry: { ...entry, closed }, result: undefined }), { status: 404 });
|
|
71
|
+
}
|
|
72
|
+
/** Removes a project's projects.json entry; its repo, worktrees and bus history stay. */
|
|
73
|
+
export function removeProject(env, project) {
|
|
74
|
+
updateProjects(env, (projects) => {
|
|
75
|
+
if (!projects.some((entry) => entry?.project === project))
|
|
76
|
+
throw Object.assign(new Error(NOT_REGISTERED), { status: 404 });
|
|
77
|
+
return { projects: projects.filter((entry) => entry?.project !== project), result: undefined };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/** Names of the registered projects marked closed. */
|
|
81
|
+
export function closedProjectNames(env) {
|
|
82
|
+
return readProjectsFile(env).filter((entry) => entry?.closed === true).map((entry) => entry.project);
|
|
83
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** Team operations that also read patterns: create a team and start one. Patterns import teams, never the reverse. */
|
|
2
|
+
import { isValidName } from "../config.js";
|
|
3
|
+
import { checkPrompt, sessionStartPlace, startSession } from "../sessions/launch.js";
|
|
4
|
+
import { parseHarnessModel } from "../harness.js";
|
|
5
|
+
import { entryTeams, updateProject } from "./projects.js";
|
|
6
|
+
import { allPatternNames, getPattern } from "./patterns.js";
|
|
7
|
+
import { listTeams, recordTeamSession, validateMember, validateMembers } from "./teams.js";
|
|
8
|
+
const UNKNOWN_PATTERN = "unknown pattern ";
|
|
9
|
+
const RESERVED_TEAM_NAMES = ["all", "operator"];
|
|
10
|
+
/** The pattern a role name belongs to when `member` names one of its roles, else null. Legacy rosters return null. */
|
|
11
|
+
function patternWithRole(cfg, name, member, env) {
|
|
12
|
+
let pattern;
|
|
13
|
+
try {
|
|
14
|
+
pattern = getPattern(cfg, name, env);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (String(error.message).startsWith(UNKNOWN_PATTERN))
|
|
18
|
+
return null;
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
if (pattern.legacy || !pattern.roles.some((role) => role.id === member))
|
|
22
|
+
return null;
|
|
23
|
+
return pattern;
|
|
24
|
+
}
|
|
25
|
+
/** Saves a team in the project's projects.json entry; pattern names and invalid members are refused. */
|
|
26
|
+
export async function createTeam(cfg, name, members, env = process.env, models, { edit = false } = {}) {
|
|
27
|
+
if (!isValidName(name))
|
|
28
|
+
throw new Error("team name must be 1-24 chars of a-z, 0-9 and single hyphens");
|
|
29
|
+
if (RESERVED_TEAM_NAMES.includes(name))
|
|
30
|
+
throw new Error(`team name cannot be "${name}"`);
|
|
31
|
+
if (allPatternNames(cfg, env).includes(name))
|
|
32
|
+
throw new Error(`"${name}" is a pattern name; pick another`);
|
|
33
|
+
const roster = await validateMembers(members, cfg, env, models);
|
|
34
|
+
return updateProject(env, cfg.project, (entry) => {
|
|
35
|
+
const teams = { ...entryTeams(entry) };
|
|
36
|
+
if (edit && (!Object.hasOwn(teams, name) || !Array.isArray(teams[name]) || !teams[name].length)) {
|
|
37
|
+
throw new Error(`"${name}" is not a project team`);
|
|
38
|
+
}
|
|
39
|
+
teams[name] = roster;
|
|
40
|
+
return { entry: { ...entry, teams }, result: { name, members: roster } };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Starts a team member, or a pattern run when `member` is a role of a pattern named `name`. */
|
|
44
|
+
// oxlint-disable-next-line quality/no-chained-type-assertions -- startSession is untyped JS; StartFn is its contract
|
|
45
|
+
export async function teamStart(cfg, name, member, task, env = process.env, { start = startSession, models, permission } = {}) {
|
|
46
|
+
if (patternWithRole(cfg, name, member, env)) {
|
|
47
|
+
if (permission !== undefined)
|
|
48
|
+
return { team: name, member, status: "failed", detail: "A team.dot team's runs take its permission attribute; set it with /team-permission" };
|
|
49
|
+
// oxlint-disable-next-line quality/no-chained-type-assertions -- pattern-runs takes a full Cfg; StartRunFn is the catalog-config contract
|
|
50
|
+
const { startRun } = await import("../pattern-runs.js");
|
|
51
|
+
const result = await startRun(cfg, name, task, env, { start, models });
|
|
52
|
+
return { team: name, member, status: result.status, session: result.session || "", detail: result.detail || "", ...(result.id ? { id: result.id } : {}) };
|
|
53
|
+
}
|
|
54
|
+
const teams = listTeams(cfg, env);
|
|
55
|
+
if (!teams[name])
|
|
56
|
+
throw new Error(`unknown team "${name}"`);
|
|
57
|
+
if (!teams[name].includes(member))
|
|
58
|
+
throw new Error(`"${member}" is not on team "${name}"`);
|
|
59
|
+
await validateMember(member, cfg, env, models);
|
|
60
|
+
const checked = checkPrompt(task);
|
|
61
|
+
if (checked.error)
|
|
62
|
+
throw new Error(checked.error);
|
|
63
|
+
const parsed = parseHarnessModel(member);
|
|
64
|
+
if (!parsed)
|
|
65
|
+
throw new Error(`"${member}" is not a harness or harness:model`);
|
|
66
|
+
const result = await start(cfg, { harness: parsed.harness, prompt: checked.text, system: true, env, place: sessionStartPlace(cfg, parsed.harness, env), managed: true, claudePermission: permission ?? "auto", ...(parsed.model ? { model: parsed.model } : {}) });
|
|
67
|
+
if (typeof result.session === "string")
|
|
68
|
+
await recordTeamSession(cfg, name, member, result.session);
|
|
69
|
+
return { team: name, member, ...result };
|
|
70
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Team files: one team.dot per team, in <root>/.ai-collab/teams/<name>/ or <config>/temmo/teams/<name>/. The folder name is the team name. */
|
|
2
|
+
import { lstatSync, readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDir, isValidName } from "../config.js";
|
|
5
|
+
import { readJson, withLockSync, writeFileAtomic, writeJson } from "../store.js";
|
|
6
|
+
const TEAM_FILE = "team.dot";
|
|
7
|
+
const MIGRATED = ".migrated.json";
|
|
8
|
+
// Waits for a save's lock this long before failing; the lock guards one file write.
|
|
9
|
+
const SAVE_WAIT_MS = 500;
|
|
10
|
+
export const globalTeamsDir = (env) => join(configDir(env), "teams");
|
|
11
|
+
export const projectTeamsDir = (root) => join(root, ".ai-collab", "teams");
|
|
12
|
+
export const teamFilePath = (dir, name) => join(dir, name, TEAM_FILE);
|
|
13
|
+
/** True for a regular file. A symlink is not one: a cloned repo could point team.dot at any file on the machine. */
|
|
14
|
+
export function isRegularFile(file) {
|
|
15
|
+
try {
|
|
16
|
+
return lstatSync(file).isFile();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Every valid team folder under dir that holds a regular team.dot, sorted by name. */
|
|
23
|
+
export function readTeamFiles(dir) {
|
|
24
|
+
let names;
|
|
25
|
+
try {
|
|
26
|
+
names = readdirSync(dir, { withFileTypes: true });
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
return names
|
|
32
|
+
.filter((entry) => entry.isDirectory() && isValidName(entry.name) && isRegularFile(teamFilePath(dir, entry.name)))
|
|
33
|
+
.map((entry) => ({ name: entry.name, file: teamFilePath(dir, entry.name), text: readFileSync(teamFilePath(dir, entry.name), "utf8") }))
|
|
34
|
+
.sort((a, b) => (a.name < b.name ? -1 : 1));
|
|
35
|
+
}
|
|
36
|
+
/** Saves a team file under its lock; fails when another writer holds the lock past 500 ms. */
|
|
37
|
+
export function saveTeamFile(file, text) {
|
|
38
|
+
const locked = withLockSync(file, () => writeFileAtomic(file, text), { waitMs: SAVE_WAIT_MS });
|
|
39
|
+
if (!locked.locked)
|
|
40
|
+
throw new Error(`${file} is busy; retry`);
|
|
41
|
+
}
|
|
42
|
+
/** Names the one-time migration already converted (or skipped), read from the marker in the global teams folder. */
|
|
43
|
+
export function migratedNames(dir) {
|
|
44
|
+
const value = readJson(join(dir, MIGRATED));
|
|
45
|
+
return Array.isArray(value) ? value.filter((name) => typeof name === "string") : [];
|
|
46
|
+
}
|
|
47
|
+
export function recordMigrated(dir, names) {
|
|
48
|
+
writeJson(join(dir, MIGRATED), names);
|
|
49
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** Teams: named member lists from teams.json (global) and projects.json (per project), with member checks and team sessions. */
|
|
2
|
+
import { mkdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDir } from "../config.js";
|
|
5
|
+
import { listModels } from "../models.js";
|
|
6
|
+
import { listSessions } from "../sessions/registry.js";
|
|
7
|
+
import { formatHarnessModel, parseHarnessModel } from "../harness.js";
|
|
8
|
+
import { readJson, withLock, writeJson } from "../store.js";
|
|
9
|
+
import { entryTeams, findProject, plainMap, updateProjects } from "./projects.js";
|
|
10
|
+
function globalTeamsFile(env) {
|
|
11
|
+
return join(configDir(env), "teams.json");
|
|
12
|
+
}
|
|
13
|
+
function teamStateFile(cfg, name) {
|
|
14
|
+
return join(cfg.stateDir, cfg.project, "teams", `${name}.json`);
|
|
15
|
+
}
|
|
16
|
+
export function readGlobalTeams(env = process.env) {
|
|
17
|
+
return plainMap(readJson(globalTeamsFile(env)));
|
|
18
|
+
}
|
|
19
|
+
export function writeGlobalTeams(env, teams) {
|
|
20
|
+
writeJson(globalTeamsFile(env), teams, { indent: 2 });
|
|
21
|
+
}
|
|
22
|
+
/** Merged team map for a project; project teams override global names. */
|
|
23
|
+
export function listTeams(cfg, env = process.env) {
|
|
24
|
+
const global = readGlobalTeams(env);
|
|
25
|
+
const project = entryTeams(findProject(env, cfg.project));
|
|
26
|
+
const names = new Set([...Object.keys(global), ...Object.keys(project)]);
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const name of names) {
|
|
29
|
+
const members = project[name] ?? global[name];
|
|
30
|
+
if (Array.isArray(members) && members.length)
|
|
31
|
+
out[name] = members.map(String);
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** The harness or harness:model text a member string normalizes to, or null when it is neither. */
|
|
36
|
+
export function parseMember(raw) {
|
|
37
|
+
const parsed = parseHarnessModel(String(raw).trim());
|
|
38
|
+
return parsed ? formatHarnessModel(parsed) : null;
|
|
39
|
+
}
|
|
40
|
+
/** Checks one member against the project's peers and the model list; returns the normalized member. */
|
|
41
|
+
export async function validateMember(member, cfg, env = process.env, models) {
|
|
42
|
+
const normalized = parseMember(member);
|
|
43
|
+
if (!normalized)
|
|
44
|
+
throw new Error(`invalid member "${member}"`);
|
|
45
|
+
const split = normalized.indexOf(":");
|
|
46
|
+
const harness = split < 0 ? normalized : normalized.slice(0, split);
|
|
47
|
+
const model = split < 0 ? undefined : normalized.slice(split + 1);
|
|
48
|
+
if (cfg.peers.length && !cfg.peers.includes(harness))
|
|
49
|
+
throw new Error(`harness "${harness}" is not in TEMMO_PEERS`);
|
|
50
|
+
if (model) {
|
|
51
|
+
const lists = models ?? await listModels(cfg.stateDir, env, { peers: cfg.peers });
|
|
52
|
+
if (!lists[harness]?.includes(model))
|
|
53
|
+
throw new Error(`unknown model "${model}" for ${harness}`);
|
|
54
|
+
}
|
|
55
|
+
return normalized;
|
|
56
|
+
}
|
|
57
|
+
/** Checks every member and rejects a duplicate; returns the normalized members in order. */
|
|
58
|
+
export async function validateMembers(members, cfg, env = process.env, models) {
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const member of members)
|
|
61
|
+
out.push(await validateMember(member, cfg, env, models));
|
|
62
|
+
if (new Set(out).size !== out.length)
|
|
63
|
+
throw new Error("duplicate members");
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
function readTeamSessions(cfg, name) {
|
|
67
|
+
return plainMap(readJson(teamStateFile(cfg, name)));
|
|
68
|
+
}
|
|
69
|
+
/** Records the session address a member started with, under the team's state lock. */
|
|
70
|
+
export async function recordTeamSession(cfg, name, member, address) {
|
|
71
|
+
const file = teamStateFile(cfg, name);
|
|
72
|
+
mkdirSync(join(cfg.stateDir, cfg.project, "teams"), { recursive: true, mode: 0o700 });
|
|
73
|
+
const prev = readTeamSessions(cfg, name);
|
|
74
|
+
await withLock(file, async () => {
|
|
75
|
+
writeJson(file, { ...prev, [member]: address });
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/** Each member of a team with the session it started and whether that session is online. */
|
|
79
|
+
export function teamStatus(cfg, name, env = process.env) {
|
|
80
|
+
const teams = listTeams(cfg, env);
|
|
81
|
+
if (!teams[name])
|
|
82
|
+
throw new Error(`unknown team "${name}"`);
|
|
83
|
+
const sessions = readTeamSessions(cfg, name);
|
|
84
|
+
const online = new Set(listSessions(cfg).filter((s) => s.online).map((s) => s.id));
|
|
85
|
+
return teams[name].map((member) => {
|
|
86
|
+
const address = typeof sessions[member] === "string" ? sessions[member] : "";
|
|
87
|
+
return { member, address, online: address ? online.has(address) : false };
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** Removes a project-level team; a global team stays in teams.json and throws. */
|
|
91
|
+
export function deleteTeam(cfg, name, env = process.env) {
|
|
92
|
+
return updateProjects(env, (projects) => {
|
|
93
|
+
const teams = listTeams(cfg, env);
|
|
94
|
+
if (!teams[name])
|
|
95
|
+
throw new Error(`unknown team "${name}"`);
|
|
96
|
+
const entry = projects.find((item) => item?.project === cfg.project);
|
|
97
|
+
if (!entry?.teams?.[name])
|
|
98
|
+
throw new Error(`team "${name}" is global; remove it from teams.json`);
|
|
99
|
+
const local = { ...entryTeams(entry) };
|
|
100
|
+
delete local[name];
|
|
101
|
+
const next = projects.map((item) => (item?.project === cfg.project ? { ...item, teams: local } : item));
|
|
102
|
+
return { projects: next, result: { deleted: name } };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** Task text after an @team tag in a post. */
|
|
106
|
+
export function teamTask(body, team) {
|
|
107
|
+
const match = new RegExp(`(?:^|[^\\w#-])@${team}\\s+([\\s\\S]*)`).exec(String(body));
|
|
108
|
+
return match?.[1]?.trim() || String(body).trim();
|
|
109
|
+
}
|