corporateai 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +10 -0
- package/.env.example +3 -0
- package/.github/workflows/publish-cli.yml +49 -0
- package/.mailmap +1 -0
- package/AGENTS.md +148 -0
- package/CONTRIBUTING.md +75 -0
- package/Dockerfile +59 -0
- package/Dockerfile.onboard-smoke +42 -0
- package/LICENSE +21 -0
- package/README.md +93 -0
- package/cli/esbuild.config.mjs +11 -0
- package/cli/package.json +24 -0
- package/cli/scripts/build-cli.mjs +5 -0
- package/cli/src/index.ts +27 -0
- package/docker-compose.quickstart.yml +18 -0
- package/docker-compose.untrusted-review.yml +33 -0
- package/docker-compose.yml +38 -0
- package/package.json +56 -0
- package/patches/embedded-postgres@18.1.0-beta.16.patch +0 -0
- package/pnpm-workspace.yaml +4 -0
- package/releases/.gitkeep +0 -0
- package/releases/v0.0.1.md +36 -0
- package/releases/v0.2.7.md +15 -0
- package/releases/v0.3.0.md +54 -0
- package/releases/v0.3.1.md +55 -0
- package/releases/v2026.318.0.md +66 -0
- package/releases/v2026.325.0.md +78 -0
- package/report/2026-03-13-08-46-token-optimization-implementation.md +48 -0
- package/scripts/backup-db.sh +17 -0
- package/scripts/build-npm.sh +80 -0
- package/scripts/check-forbidden-tokens.mjs +115 -0
- package/scripts/clean-onboard-git.sh +14 -0
- package/scripts/clean-onboard-npm.sh +13 -0
- package/scripts/clean-onboard-ref.sh +86 -0
- package/scripts/create-github-release.sh +99 -0
- package/scripts/dev-runner-paths.mjs +38 -0
- package/scripts/dev-runner.mjs +606 -0
- package/scripts/docker-onboard-smoke.sh +306 -0
- package/scripts/ensure-plugin-build-deps.mjs +47 -0
- package/scripts/generate-company-assets.ts +365 -0
- package/scripts/generate-npm-package-json.mjs +113 -0
- package/scripts/generate-org-chart-images.ts +694 -0
- package/scripts/generate-org-chart-satori-comparison.ts +225 -0
- package/scripts/generate-ui-package-json.mjs +31 -0
- package/scripts/kill-dev.sh +71 -0
- package/scripts/migrate-inline-env-secrets.ts +126 -0
- package/scripts/prepare-server-ui-dist.sh +22 -0
- package/scripts/provision-worktree.sh +333 -0
- package/scripts/release-lib.sh +306 -0
- package/scripts/release-package-map.mjs +169 -0
- package/scripts/release.sh +312 -0
- package/scripts/rollback-latest.sh +111 -0
- package/scripts/smoke/openclaw-docker-ui.sh +329 -0
- package/scripts/smoke/openclaw-gateway-e2e.sh +954 -0
- package/scripts/smoke/openclaw-join.sh +295 -0
- package/scripts/smoke/openclaw-sse-standalone.sh +146 -0
- package/scripts/workspace-compat.mjs +60 -0
- package/server/CHANGELOG.md +130 -0
- package/server/package.json +96 -0
- package/server/scripts/copy-onboarding-assets.mjs +10 -0
- package/server/scripts/dev-watch.ts +33 -0
- package/server/src/__tests__/activity-routes.test.ts +70 -0
- package/server/src/__tests__/adapter-models.test.ts +105 -0
- package/server/src/__tests__/adapter-session-codecs.test.ts +194 -0
- package/server/src/__tests__/agent-auth-jwt.test.ts +79 -0
- package/server/src/__tests__/agent-instructions-routes.test.ts +318 -0
- package/server/src/__tests__/agent-instructions-service.test.ts +361 -0
- package/server/src/__tests__/agent-permissions-routes.test.ts +275 -0
- package/server/src/__tests__/agent-shortname-collision.test.ts +69 -0
- package/server/src/__tests__/agent-skill-contract.test.ts +50 -0
- package/server/src/__tests__/agent-skills-routes.test.ts +462 -0
- package/server/src/__tests__/app-hmr-port.test.ts +19 -0
- package/server/src/__tests__/approval-routes-idempotency.test.ts +110 -0
- package/server/src/__tests__/approvals-service.test.ts +107 -0
- package/server/src/__tests__/assets.test.ts +250 -0
- package/server/src/__tests__/attachment-types.test.ts +97 -0
- package/server/src/__tests__/board-mutation-guard.test.ts +105 -0
- package/server/src/__tests__/budgets-service.test.ts +311 -0
- package/server/src/__tests__/claude-local-adapter-environment.test.ts +92 -0
- package/server/src/__tests__/claude-local-adapter.test.ts +31 -0
- package/server/src/__tests__/claude-local-skill-sync.test.ts +111 -0
- package/server/src/__tests__/cli-auth-routes.test.ts +230 -0
- package/server/src/__tests__/codex-local-adapter-environment.test.ts +143 -0
- package/server/src/__tests__/codex-local-adapter.test.ts +253 -0
- package/server/src/__tests__/codex-local-execute.test.ts +391 -0
- package/server/src/__tests__/codex-local-skill-injection.test.ts +175 -0
- package/server/src/__tests__/codex-local-skill-sync.test.ts +123 -0
- package/server/src/__tests__/companies-route-path-guard.test.ts +56 -0
- package/server/src/__tests__/company-branding-route.test.ts +196 -0
- package/server/src/__tests__/company-portability-routes.test.ts +175 -0
- package/server/src/__tests__/company-portability.test.ts +2186 -0
- package/server/src/__tests__/company-skills-routes.test.ts +113 -0
- package/server/src/__tests__/company-skills.test.ts +229 -0
- package/server/src/__tests__/costs-service.test.ts +226 -0
- package/server/src/__tests__/cursor-local-adapter-environment.test.ts +196 -0
- package/server/src/__tests__/cursor-local-adapter.test.ts +406 -0
- package/server/src/__tests__/cursor-local-execute.test.ts +263 -0
- package/server/src/__tests__/cursor-local-skill-injection.test.ts +104 -0
- package/server/src/__tests__/cursor-local-skill-sync.test.ts +145 -0
- package/server/src/__tests__/dev-runner-paths.test.ts +25 -0
- package/server/src/__tests__/dev-server-status.test.ts +66 -0
- package/server/src/__tests__/dev-watch-ignore.test.ts +42 -0
- package/server/src/__tests__/documents.test.ts +29 -0
- package/server/src/__tests__/error-handler.test.ts +53 -0
- package/server/src/__tests__/execution-workspace-policy.test.ts +170 -0
- package/server/src/__tests__/forbidden-tokens.test.ts +77 -0
- package/server/src/__tests__/gemini-local-adapter-environment.test.ts +135 -0
- package/server/src/__tests__/gemini-local-adapter.test.ts +190 -0
- package/server/src/__tests__/gemini-local-execute.test.ts +172 -0
- package/server/src/__tests__/gemini-local-skill-sync.test.ts +90 -0
- package/server/src/__tests__/health.test.ts +16 -0
- package/server/src/__tests__/heartbeat-process-recovery.test.ts +256 -0
- package/server/src/__tests__/heartbeat-run-summary.test.ts +33 -0
- package/server/src/__tests__/heartbeat-workspace-session.test.ts +334 -0
- package/server/src/__tests__/helpers/embedded-postgres.ts +7 -0
- package/server/src/__tests__/hire-hook.test.ts +181 -0
- package/server/src/__tests__/instance-settings-routes.test.ts +156 -0
- package/server/src/__tests__/invite-accept-gateway-defaults.test.ts +119 -0
- package/server/src/__tests__/invite-accept-replay.test.ts +92 -0
- package/server/src/__tests__/invite-expiry.test.ts +10 -0
- package/server/src/__tests__/invite-join-grants.test.ts +57 -0
- package/server/src/__tests__/invite-join-manager.test.ts +33 -0
- package/server/src/__tests__/invite-onboarding-text.test.ts +116 -0
- package/server/src/__tests__/issue-comment-reopen-routes.test.ts +146 -0
- package/server/src/__tests__/issue-goal-fallback.test.ts +99 -0
- package/server/src/__tests__/issues-checkout-wakeup.test.ts +48 -0
- package/server/src/__tests__/issues-goal-context-routes.test.ts +187 -0
- package/server/src/__tests__/issues-service.test.ts +317 -0
- package/server/src/__tests__/issues-user-context.test.ts +113 -0
- package/server/src/__tests__/log-redaction.test.ts +74 -0
- package/server/src/__tests__/monthly-spend-service.test.ts +90 -0
- package/server/src/__tests__/normalize-agent-mention-token.test.ts +41 -0
- package/server/src/__tests__/openclaw-gateway-adapter.test.ts +626 -0
- package/server/src/__tests__/openclaw-invite-prompt-route.test.ts +192 -0
- package/server/src/__tests__/opencode-local-adapter-environment.test.ts +97 -0
- package/server/src/__tests__/opencode-local-adapter.test.ts +226 -0
- package/server/src/__tests__/opencode-local-skill-sync.test.ts +91 -0
- package/server/src/__tests__/paperclip-env.test.ts +58 -0
- package/server/src/__tests__/paperclip-skill-utils.test.ts +63 -0
- package/server/src/__tests__/pi-local-adapter-environment.test.ts +102 -0
- package/server/src/__tests__/pi-local-skill-sync.test.ts +95 -0
- package/server/src/__tests__/plugin-dev-watcher.test.ts +68 -0
- package/server/src/__tests__/plugin-worker-manager.test.ts +43 -0
- package/server/src/__tests__/private-hostname-guard.test.ts +56 -0
- package/server/src/__tests__/project-shortname-resolution.test.ts +45 -0
- package/server/src/__tests__/quota-windows-service.test.ts +56 -0
- package/server/src/__tests__/quota-windows.test.ts +1109 -0
- package/server/src/__tests__/redaction.test.ts +66 -0
- package/server/src/__tests__/routines-e2e.test.ts +276 -0
- package/server/src/__tests__/routines-routes.test.ts +271 -0
- package/server/src/__tests__/routines-service.test.ts +424 -0
- package/server/src/__tests__/storage-local-provider.test.ts +78 -0
- package/server/src/__tests__/ui-branding.test.ts +82 -0
- package/server/src/__tests__/work-products.test.ts +95 -0
- package/server/src/__tests__/workspace-runtime.test.ts +1131 -0
- package/server/src/__tests__/worktree-config.test.ts +426 -0
- package/server/src/adapters/codex-models.ts +105 -0
- package/server/src/adapters/cursor-models.ts +171 -0
- package/server/src/adapters/http/execute.ts +42 -0
- package/server/src/adapters/http/index.ts +21 -0
- package/server/src/adapters/http/test.ts +116 -0
- package/server/src/adapters/index.ts +18 -0
- package/server/src/adapters/process/execute.ts +77 -0
- package/server/src/adapters/process/index.ts +24 -0
- package/server/src/adapters/process/test.ts +89 -0
- package/server/src/adapters/registry.ts +225 -0
- package/server/src/adapters/server-utils-compat.ts +57 -0
- package/server/src/adapters/types.ts +30 -0
- package/server/src/adapters/utils.ts +48 -0
- package/server/src/agent-auth-jwt.ts +141 -0
- package/server/src/app.ts +321 -0
- package/server/src/attachment-types.ts +74 -0
- package/server/src/auth/better-auth.ts +148 -0
- package/server/src/board-claim.ts +150 -0
- package/server/src/config-file.ts +17 -0
- package/server/src/config.ts +260 -0
- package/server/src/dev-server-status.ts +103 -0
- package/server/src/dev-watch-ignore.ts +36 -0
- package/server/src/errors.ts +34 -0
- package/server/src/home-paths.ts +95 -0
- package/server/src/index.ts +799 -0
- package/server/src/log-redaction.ts +146 -0
- package/server/src/middleware/auth.ts +178 -0
- package/server/src/middleware/board-mutation-guard.ts +66 -0
- package/server/src/middleware/error-handler.ts +71 -0
- package/server/src/middleware/index.ts +3 -0
- package/server/src/middleware/logger.ts +90 -0
- package/server/src/middleware/private-hostname-guard.ts +92 -0
- package/server/src/middleware/validate.ts +9 -0
- package/server/src/onboarding-assets/ceo/AGENTS.md +54 -0
- package/server/src/onboarding-assets/ceo/HEARTBEAT.md +72 -0
- package/server/src/onboarding-assets/ceo/SOUL.md +33 -0
- package/server/src/onboarding-assets/ceo/TOOLS.md +3 -0
- package/server/src/onboarding-assets/default/AGENTS.md +3 -0
- package/server/src/paths.ts +34 -0
- package/server/src/realtime/live-events-ws.ts +274 -0
- package/server/src/redaction.ts +59 -0
- package/server/src/routes/access.ts +2888 -0
- package/server/src/routes/activity.ts +89 -0
- package/server/src/routes/agents.ts +2313 -0
- package/server/src/routes/approvals.ts +346 -0
- package/server/src/routes/assets.ts +341 -0
- package/server/src/routes/authz.ts +52 -0
- package/server/src/routes/companies.ts +343 -0
- package/server/src/routes/company-skills.ts +300 -0
- package/server/src/routes/costs.ts +335 -0
- package/server/src/routes/dashboard.ts +19 -0
- package/server/src/routes/execution-workspaces.ts +182 -0
- package/server/src/routes/goals.ts +107 -0
- package/server/src/routes/health.ts +94 -0
- package/server/src/routes/index.ts +17 -0
- package/server/src/routes/instance-settings.ts +95 -0
- package/server/src/routes/issues-checkout-wakeup.ts +14 -0
- package/server/src/routes/issues.ts +1680 -0
- package/server/src/routes/llms.ts +86 -0
- package/server/src/routes/org-chart-svg.ts +777 -0
- package/server/src/routes/plugin-ui-static.ts +497 -0
- package/server/src/routes/plugins.ts +2220 -0
- package/server/src/routes/projects.ts +295 -0
- package/server/src/routes/routines.ts +300 -0
- package/server/src/routes/secrets.ts +166 -0
- package/server/src/routes/sidebar-badges.ts +52 -0
- package/server/src/secrets/external-stub-providers.ts +32 -0
- package/server/src/secrets/local-encrypted-provider.ts +135 -0
- package/server/src/secrets/provider-registry.ts +31 -0
- package/server/src/secrets/types.ts +23 -0
- package/server/src/services/access.ts +381 -0
- package/server/src/services/activity-log.ts +95 -0
- package/server/src/services/activity.ts +164 -0
- package/server/src/services/agent-instructions.ts +735 -0
- package/server/src/services/agent-permissions.ts +27 -0
- package/server/src/services/agents.ts +694 -0
- package/server/src/services/approvals.ts +273 -0
- package/server/src/services/assets.ts +23 -0
- package/server/src/services/board-auth.ts +355 -0
- package/server/src/services/budgets.ts +959 -0
- package/server/src/services/companies.ts +313 -0
- package/server/src/services/company-export-readme.ts +173 -0
- package/server/src/services/company-portability.ts +4263 -0
- package/server/src/services/company-skills.ts +2356 -0
- package/server/src/services/costs.ts +365 -0
- package/server/src/services/cron.ts +373 -0
- package/server/src/services/dashboard.ts +110 -0
- package/server/src/services/default-agent-instructions.ts +27 -0
- package/server/src/services/documents.ts +434 -0
- package/server/src/services/execution-workspace-policy.ts +210 -0
- package/server/src/services/execution-workspaces.ts +100 -0
- package/server/src/services/finance.ts +135 -0
- package/server/src/services/goals.ts +81 -0
- package/server/src/services/heartbeat-run-summary.ts +35 -0
- package/server/src/services/heartbeat.ts +3863 -0
- package/server/src/services/hire-hook.ts +114 -0
- package/server/src/services/index.ts +32 -0
- package/server/src/services/instance-settings.ts +138 -0
- package/server/src/services/issue-approvals.ts +175 -0
- package/server/src/services/issue-assignment-wakeup.ts +48 -0
- package/server/src/services/issue-goal-fallback.ts +56 -0
- package/server/src/services/issues.ts +1828 -0
- package/server/src/services/live-events.ts +55 -0
- package/server/src/services/plugin-capability-validator.ts +450 -0
- package/server/src/services/plugin-config-validator.ts +55 -0
- package/server/src/services/plugin-dev-watcher.ts +339 -0
- package/server/src/services/plugin-event-bus.ts +413 -0
- package/server/src/services/plugin-host-service-cleanup.ts +59 -0
- package/server/src/services/plugin-host-services.ts +1132 -0
- package/server/src/services/plugin-job-coordinator.ts +261 -0
- package/server/src/services/plugin-job-scheduler.ts +753 -0
- package/server/src/services/plugin-job-store.ts +466 -0
- package/server/src/services/plugin-lifecycle.ts +822 -0
- package/server/src/services/plugin-loader.ts +1955 -0
- package/server/src/services/plugin-log-retention.ts +87 -0
- package/server/src/services/plugin-manifest-validator.ts +164 -0
- package/server/src/services/plugin-registry.ts +683 -0
- package/server/src/services/plugin-runtime-sandbox.ts +222 -0
- package/server/src/services/plugin-secrets-handler.ts +355 -0
- package/server/src/services/plugin-state-store.ts +238 -0
- package/server/src/services/plugin-stream-bus.ts +81 -0
- package/server/src/services/plugin-tool-dispatcher.ts +449 -0
- package/server/src/services/plugin-tool-registry.ts +450 -0
- package/server/src/services/plugin-worker-manager.ts +1343 -0
- package/server/src/services/projects.ts +860 -0
- package/server/src/services/quota-windows.ts +65 -0
- package/server/src/services/routines.ts +1269 -0
- package/server/src/services/run-log-store.ts +156 -0
- package/server/src/services/secrets.ts +370 -0
- package/server/src/services/sidebar-badges.ts +56 -0
- package/server/src/services/work-products.ts +124 -0
- package/server/src/services/workspace-operation-log-store.ts +156 -0
- package/server/src/services/workspace-operations.ts +262 -0
- package/server/src/services/workspace-runtime.ts +1565 -0
- package/server/src/startup-banner.ts +176 -0
- package/server/src/storage/index.ts +35 -0
- package/server/src/storage/local-disk-provider.ts +89 -0
- package/server/src/storage/provider-registry.ts +18 -0
- package/server/src/storage/s3-provider.ts +153 -0
- package/server/src/storage/service.ts +131 -0
- package/server/src/storage/types.ts +63 -0
- package/server/src/ui-branding.ts +217 -0
- package/server/src/version.ts +10 -0
- package/server/src/worktree-config.ts +468 -0
- package/server/tsconfig.json +9 -0
- package/server/vitest.config.ts +7 -0
- package/skills/paperclip/SKILL.md +365 -0
- package/skills/paperclip/references/api-reference.md +647 -0
- package/skills/paperclip/references/company-skills.md +193 -0
- package/skills/paperclip-create-agent/SKILL.md +142 -0
- package/skills/paperclip-create-agent/references/api-reference.md +105 -0
- package/skills/paperclip-create-plugin/SKILL.md +102 -0
- package/skills/para-memory-files/SKILL.md +104 -0
- package/skills/para-memory-files/references/schemas.md +35 -0
- package/tests/e2e/onboarding.spec.ts +142 -0
- package/tests/e2e/playwright.config.ts +35 -0
- package/tests/release-smoke/docker-auth-onboarding.spec.ts +141 -0
- package/tests/release-smoke/playwright.config.ts +28 -0
- package/tsconfig.base.json +18 -0
- package/tsconfig.json +18 -0
- package/ui/README.md +12 -0
- package/ui/components.json +21 -0
- package/ui/index.html +47 -0
- package/ui/package.json +73 -0
- package/ui/public/android-chrome-192x192.png +0 -0
- package/ui/public/android-chrome-512x512.png +0 -0
- package/ui/public/apple-touch-icon.png +0 -0
- package/ui/public/brands/opencode-logo-dark-square.svg +18 -0
- package/ui/public/brands/opencode-logo-light-square.svg +18 -0
- package/ui/public/favicon-16x16.png +0 -0
- package/ui/public/favicon-32x32.png +0 -0
- package/ui/public/favicon.ico +0 -0
- package/ui/public/favicon.svg +9 -0
- package/ui/public/site.webmanifest +30 -0
- package/ui/public/sprites/1-D-1.png +0 -0
- package/ui/public/sprites/1-D-2.png +0 -0
- package/ui/public/sprites/1-D-3.png +0 -0
- package/ui/public/sprites/1-L-1.png +0 -0
- package/ui/public/sprites/1-R-1.png +0 -0
- package/ui/public/sprites/10-D-1.png +0 -0
- package/ui/public/sprites/10-D-2.png +0 -0
- package/ui/public/sprites/10-D-3.png +0 -0
- package/ui/public/sprites/10-L-1.png +0 -0
- package/ui/public/sprites/10-R-1.png +0 -0
- package/ui/public/sprites/11-D-1.png +0 -0
- package/ui/public/sprites/11-D-2.png +0 -0
- package/ui/public/sprites/11-D-3.png +0 -0
- package/ui/public/sprites/11-L-1.png +0 -0
- package/ui/public/sprites/11-R-1.png +0 -0
- package/ui/public/sprites/12-D-1.png +0 -0
- package/ui/public/sprites/12-D-2.png +0 -0
- package/ui/public/sprites/12-D-3.png +0 -0
- package/ui/public/sprites/12-L-1.png +0 -0
- package/ui/public/sprites/12-R-1.png +0 -0
- package/ui/public/sprites/13-D-1.png +0 -0
- package/ui/public/sprites/13-D-2.png +0 -0
- package/ui/public/sprites/13-D-3.png +0 -0
- package/ui/public/sprites/13-L-1.png +0 -0
- package/ui/public/sprites/13-R-1.png +0 -0
- package/ui/public/sprites/14-D-1.png +0 -0
- package/ui/public/sprites/14-D-2.png +0 -0
- package/ui/public/sprites/14-D-3.png +0 -0
- package/ui/public/sprites/14-L-1.png +0 -0
- package/ui/public/sprites/14-R-1.png +0 -0
- package/ui/public/sprites/2-D-1.png +0 -0
- package/ui/public/sprites/2-D-2.png +0 -0
- package/ui/public/sprites/2-D-3.png +0 -0
- package/ui/public/sprites/2-L-1.png +0 -0
- package/ui/public/sprites/2-R-1.png +0 -0
- package/ui/public/sprites/3-D-1.png +0 -0
- package/ui/public/sprites/3-D-2.png +0 -0
- package/ui/public/sprites/3-D-3.png +0 -0
- package/ui/public/sprites/3-L-1.png +0 -0
- package/ui/public/sprites/3-R-1.png +0 -0
- package/ui/public/sprites/4-D-1.png +0 -0
- package/ui/public/sprites/4-D-2.png +0 -0
- package/ui/public/sprites/4-D-3.png +0 -0
- package/ui/public/sprites/4-L-1.png +0 -0
- package/ui/public/sprites/4-R-1.png +0 -0
- package/ui/public/sprites/5-D-1.png +0 -0
- package/ui/public/sprites/5-D-2.png +0 -0
- package/ui/public/sprites/5-D-3.png +0 -0
- package/ui/public/sprites/5-L-1.png +0 -0
- package/ui/public/sprites/5-R-1.png +0 -0
- package/ui/public/sprites/6-D-1.png +0 -0
- package/ui/public/sprites/6-D-2.png +0 -0
- package/ui/public/sprites/6-D-3.png +0 -0
- package/ui/public/sprites/6-L-1.png +0 -0
- package/ui/public/sprites/6-R-1.png +0 -0
- package/ui/public/sprites/7-D-1.png +0 -0
- package/ui/public/sprites/7-D-2.png +0 -0
- package/ui/public/sprites/7-D-3.png +0 -0
- package/ui/public/sprites/7-L-1.png +0 -0
- package/ui/public/sprites/7-R-1.png +0 -0
- package/ui/public/sprites/8-D-1.png +0 -0
- package/ui/public/sprites/8-D-2.png +0 -0
- package/ui/public/sprites/8-D-3.png +0 -0
- package/ui/public/sprites/8-L-1.png +0 -0
- package/ui/public/sprites/8-R-1.png +0 -0
- package/ui/public/sprites/9-D-1.png +0 -0
- package/ui/public/sprites/9-D-2.png +0 -0
- package/ui/public/sprites/9-D-3.png +0 -0
- package/ui/public/sprites/9-L-1.png +0 -0
- package/ui/public/sprites/9-R-1.png +0 -0
- package/ui/public/sprites/ceo-lobster.png +0 -0
- package/ui/public/sw.js +42 -0
- package/ui/public/worktree-favicon-16x16.png +0 -0
- package/ui/public/worktree-favicon-32x32.png +0 -0
- package/ui/public/worktree-favicon.ico +0 -0
- package/ui/public/worktree-favicon.svg +9 -0
- package/ui/src/App.tsx +354 -0
- package/ui/src/adapters/claude-local/config-fields.tsx +138 -0
- package/ui/src/adapters/claude-local/index.ts +13 -0
- package/ui/src/adapters/codex-local/config-fields.tsx +104 -0
- package/ui/src/adapters/codex-local/index.ts +13 -0
- package/ui/src/adapters/cursor/config-fields.tsx +49 -0
- package/ui/src/adapters/cursor/index.ts +13 -0
- package/ui/src/adapters/gemini-local/config-fields.tsx +51 -0
- package/ui/src/adapters/gemini-local/index.ts +13 -0
- package/ui/src/adapters/http/build-config.ts +9 -0
- package/ui/src/adapters/http/config-fields.tsx +38 -0
- package/ui/src/adapters/http/index.ts +12 -0
- package/ui/src/adapters/http/parse-stdout.ts +5 -0
- package/ui/src/adapters/index.ts +9 -0
- package/ui/src/adapters/local-workspace-runtime-fields.tsx +5 -0
- package/ui/src/adapters/openclaw-gateway/config-fields.tsx +237 -0
- package/ui/src/adapters/openclaw-gateway/index.ts +13 -0
- package/ui/src/adapters/opencode-local/config-fields.tsx +72 -0
- package/ui/src/adapters/opencode-local/index.ts +13 -0
- package/ui/src/adapters/pi-local/config-fields.tsx +49 -0
- package/ui/src/adapters/pi-local/index.ts +13 -0
- package/ui/src/adapters/process/build-config.ts +18 -0
- package/ui/src/adapters/process/config-fields.tsx +77 -0
- package/ui/src/adapters/process/index.ts +12 -0
- package/ui/src/adapters/process/parse-stdout.ts +5 -0
- package/ui/src/adapters/registry.ts +34 -0
- package/ui/src/adapters/runtime-json-fields.tsx +122 -0
- package/ui/src/adapters/transcript.test.ts +30 -0
- package/ui/src/adapters/transcript.ts +62 -0
- package/ui/src/adapters/types.ts +34 -0
- package/ui/src/api/access.ts +160 -0
- package/ui/src/api/activity.ts +37 -0
- package/ui/src/api/agents.ts +194 -0
- package/ui/src/api/approvals.ts +25 -0
- package/ui/src/api/assets.ts +30 -0
- package/ui/src/api/auth.ts +74 -0
- package/ui/src/api/budgets.ts +21 -0
- package/ui/src/api/client.ts +50 -0
- package/ui/src/api/companies.ts +59 -0
- package/ui/src/api/companySkills.ts +55 -0
- package/ui/src/api/costs.ts +60 -0
- package/ui/src/api/dashboard.ts +7 -0
- package/ui/src/api/execution-workspaces.ts +27 -0
- package/ui/src/api/goals.ts +12 -0
- package/ui/src/api/health.ts +41 -0
- package/ui/src/api/heartbeats.ts +62 -0
- package/ui/src/api/index.ts +18 -0
- package/ui/src/api/instanceSettings.ts +19 -0
- package/ui/src/api/issues.ts +115 -0
- package/ui/src/api/plugins.ts +424 -0
- package/ui/src/api/projects.ts +34 -0
- package/ui/src/api/routines.ts +59 -0
- package/ui/src/api/secrets.ts +26 -0
- package/ui/src/api/sidebarBadges.ts +7 -0
- package/ui/src/components/AccountingModelCard.tsx +69 -0
- package/ui/src/components/ActiveAgentsPanel.tsx +157 -0
- package/ui/src/components/ActivityCharts.tsx +264 -0
- package/ui/src/components/ActivityRow.tsx +147 -0
- package/ui/src/components/AgentActionButtons.tsx +51 -0
- package/ui/src/components/AgentConfigForm.tsx +1468 -0
- package/ui/src/components/AgentIconPicker.tsx +81 -0
- package/ui/src/components/AgentProperties.tsx +107 -0
- package/ui/src/components/ApprovalCard.tsx +107 -0
- package/ui/src/components/ApprovalPayload.tsx +134 -0
- package/ui/src/components/AsciiArtAnimation.tsx +355 -0
- package/ui/src/components/BillerSpendCard.tsx +146 -0
- package/ui/src/components/BreadcrumbBar.tsx +113 -0
- package/ui/src/components/BudgetIncidentCard.tsx +101 -0
- package/ui/src/components/BudgetPolicyCard.tsx +220 -0
- package/ui/src/components/BudgetSidebarMarker.tsx +13 -0
- package/ui/src/components/ClaudeSubscriptionPanel.tsx +141 -0
- package/ui/src/components/CodexSubscriptionPanel.tsx +158 -0
- package/ui/src/components/CommandPalette.tsx +239 -0
- package/ui/src/components/CommentThread.tsx +503 -0
- package/ui/src/components/CompanyPatternIcon.tsx +212 -0
- package/ui/src/components/CompanyRail.tsx +329 -0
- package/ui/src/components/CompanySwitcher.tsx +81 -0
- package/ui/src/components/CopyText.tsx +56 -0
- package/ui/src/components/DevRestartBanner.tsx +89 -0
- package/ui/src/components/EmptyState.tsx +27 -0
- package/ui/src/components/EntityRow.tsx +69 -0
- package/ui/src/components/FilterBar.tsx +39 -0
- package/ui/src/components/FinanceBillerCard.tsx +45 -0
- package/ui/src/components/FinanceKindCard.tsx +44 -0
- package/ui/src/components/FinanceTimelineCard.tsx +72 -0
- package/ui/src/components/GoalProperties.tsx +165 -0
- package/ui/src/components/GoalTree.tsx +118 -0
- package/ui/src/components/Identity.tsx +39 -0
- package/ui/src/components/InlineEditor.tsx +248 -0
- package/ui/src/components/InlineEntitySelector.tsx +206 -0
- package/ui/src/components/InstanceSidebar.tsx +53 -0
- package/ui/src/components/IssueDocumentsSection.tsx +892 -0
- package/ui/src/components/IssueProperties.tsx +621 -0
- package/ui/src/components/IssueRow.tsx +149 -0
- package/ui/src/components/IssueWorkspaceCard.tsx +404 -0
- package/ui/src/components/IssuesList.tsx +889 -0
- package/ui/src/components/JsonSchemaForm.tsx +1048 -0
- package/ui/src/components/KanbanBoard.tsx +275 -0
- package/ui/src/components/Layout.tsx +441 -0
- package/ui/src/components/LiveRunWidget.tsx +160 -0
- package/ui/src/components/MarkdownBody.test.tsx +50 -0
- package/ui/src/components/MarkdownBody.tsx +152 -0
- package/ui/src/components/MarkdownEditor.tsx +622 -0
- package/ui/src/components/MetricCard.tsx +53 -0
- package/ui/src/components/MobileBottomNav.tsx +123 -0
- package/ui/src/components/NewAgentDialog.tsx +223 -0
- package/ui/src/components/NewGoalDialog.tsx +283 -0
- package/ui/src/components/NewIssueDialog.tsx +1473 -0
- package/ui/src/components/NewProjectDialog.tsx +451 -0
- package/ui/src/components/OnboardingWizard.tsx +1392 -0
- package/ui/src/components/OpenCodeLogoIcon.tsx +22 -0
- package/ui/src/components/PackageFileTree.tsx +318 -0
- package/ui/src/components/PageSkeleton.tsx +180 -0
- package/ui/src/components/PageTabBar.tsx +45 -0
- package/ui/src/components/PathInstructionsModal.tsx +143 -0
- package/ui/src/components/PriorityIcon.tsx +77 -0
- package/ui/src/components/ProjectProperties.tsx +1127 -0
- package/ui/src/components/PropertiesPanel.tsx +29 -0
- package/ui/src/components/ProviderQuotaCard.tsx +417 -0
- package/ui/src/components/QuotaBar.tsx +65 -0
- package/ui/src/components/ReportsToPicker.tsx +127 -0
- package/ui/src/components/ScheduleEditor.tsx +344 -0
- package/ui/src/components/ScrollToBottom.tsx +79 -0
- package/ui/src/components/Sidebar.tsx +130 -0
- package/ui/src/components/SidebarAgents.tsx +146 -0
- package/ui/src/components/SidebarNavItem.tsx +92 -0
- package/ui/src/components/SidebarProjects.tsx +234 -0
- package/ui/src/components/SidebarSection.tsx +17 -0
- package/ui/src/components/StatusBadge.tsx +15 -0
- package/ui/src/components/StatusIcon.tsx +71 -0
- package/ui/src/components/SwipeToArchive.tsx +152 -0
- package/ui/src/components/ToastViewport.tsx +99 -0
- package/ui/src/components/WorktreeBanner.tsx +25 -0
- package/ui/src/components/agent-config-defaults.ts +31 -0
- package/ui/src/components/agent-config-primitives.tsx +476 -0
- package/ui/src/components/transcript/RunTranscriptView.test.tsx +84 -0
- package/ui/src/components/transcript/RunTranscriptView.tsx +1015 -0
- package/ui/src/components/transcript/useLiveRunTranscripts.ts +297 -0
- package/ui/src/components/ui/avatar.tsx +107 -0
- package/ui/src/components/ui/badge.tsx +48 -0
- package/ui/src/components/ui/breadcrumb.tsx +109 -0
- package/ui/src/components/ui/button.tsx +64 -0
- package/ui/src/components/ui/card.tsx +92 -0
- package/ui/src/components/ui/checkbox.tsx +32 -0
- package/ui/src/components/ui/collapsible.tsx +33 -0
- package/ui/src/components/ui/command.tsx +194 -0
- package/ui/src/components/ui/dialog.tsx +156 -0
- package/ui/src/components/ui/dropdown-menu.tsx +257 -0
- package/ui/src/components/ui/input.tsx +21 -0
- package/ui/src/components/ui/label.tsx +22 -0
- package/ui/src/components/ui/popover.tsx +88 -0
- package/ui/src/components/ui/scroll-area.tsx +56 -0
- package/ui/src/components/ui/select.tsx +188 -0
- package/ui/src/components/ui/separator.tsx +28 -0
- package/ui/src/components/ui/sheet.tsx +143 -0
- package/ui/src/components/ui/skeleton.tsx +13 -0
- package/ui/src/components/ui/tabs.tsx +89 -0
- package/ui/src/components/ui/textarea.tsx +18 -0
- package/ui/src/components/ui/tooltip.tsx +57 -0
- package/ui/src/components/visual-office/AgentAvatar.tsx +99 -0
- package/ui/src/components/visual-office/OfficeViewExact.tsx +417 -0
- package/ui/src/components/visual-office/i18n.ts +52 -0
- package/ui/src/components/visual-office/office-view/CliUsagePanel.tsx +240 -0
- package/ui/src/components/visual-office/office-view/VirtualPadOverlay.tsx +104 -0
- package/ui/src/components/visual-office/office-view/buildScene-break-room.ts +248 -0
- package/ui/src/components/visual-office/office-view/buildScene-ceo-hallway.ts +345 -0
- package/ui/src/components/visual-office/office-view/buildScene-department-agent.ts +242 -0
- package/ui/src/components/visual-office/office-view/buildScene-departments.ts +360 -0
- package/ui/src/components/visual-office/office-view/buildScene-final-layers.ts +113 -0
- package/ui/src/components/visual-office/office-view/buildScene-types.ts +91 -0
- package/ui/src/components/visual-office/office-view/buildScene.ts +232 -0
- package/ui/src/components/visual-office/office-view/drawing-core.ts +374 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-a.ts +338 -0
- package/ui/src/components/visual-office/office-view/drawing-furniture-b.ts +241 -0
- package/ui/src/components/visual-office/office-view/model.ts +301 -0
- package/ui/src/components/visual-office/office-view/officeTicker.ts +455 -0
- package/ui/src/components/visual-office/office-view/officeTickerRoomAndDelivery.ts +133 -0
- package/ui/src/components/visual-office/office-view/themes-locale.ts +460 -0
- package/ui/src/components/visual-office/office-view/useCliUsage.ts +37 -0
- package/ui/src/components/visual-office/office-view/useOfficeDeliveryEffects.ts +465 -0
- package/ui/src/components/visual-office/office-view/useOfficePixiRuntime.ts +282 -0
- package/ui/src/components/visual-office/types.ts +123 -0
- package/ui/src/context/BreadcrumbContext.tsx +44 -0
- package/ui/src/context/CompanyContext.tsx +151 -0
- package/ui/src/context/DialogContext.tsx +135 -0
- package/ui/src/context/LiveUpdatesProvider.test.ts +119 -0
- package/ui/src/context/LiveUpdatesProvider.tsx +760 -0
- package/ui/src/context/PanelContext.tsx +73 -0
- package/ui/src/context/SidebarContext.tsx +43 -0
- package/ui/src/context/ThemeContext.tsx +83 -0
- package/ui/src/context/ToastContext.tsx +172 -0
- package/ui/src/fixtures/runTranscriptFixtures.ts +226 -0
- package/ui/src/hooks/useAgentOrder.ts +105 -0
- package/ui/src/hooks/useAutosaveIndicator.ts +72 -0
- package/ui/src/hooks/useCompanyPageMemory.test.ts +90 -0
- package/ui/src/hooks/useCompanyPageMemory.ts +79 -0
- package/ui/src/hooks/useDateRange.ts +120 -0
- package/ui/src/hooks/useInboxBadge.ts +132 -0
- package/ui/src/hooks/useKeyboardShortcuts.ts +40 -0
- package/ui/src/hooks/useProjectOrder.ts +106 -0
- package/ui/src/index.css +770 -0
- package/ui/src/lib/agent-icons.ts +99 -0
- package/ui/src/lib/agent-order.ts +107 -0
- package/ui/src/lib/agent-skills-state.test.ts +90 -0
- package/ui/src/lib/agent-skills-state.ts +41 -0
- package/ui/src/lib/assignees.test.ts +92 -0
- package/ui/src/lib/assignees.ts +82 -0
- package/ui/src/lib/color-contrast.ts +107 -0
- package/ui/src/lib/company-export-selection.test.ts +41 -0
- package/ui/src/lib/company-export-selection.ts +57 -0
- package/ui/src/lib/company-page-memory.ts +65 -0
- package/ui/src/lib/company-portability-sidebar.test.ts +101 -0
- package/ui/src/lib/company-portability-sidebar.ts +62 -0
- package/ui/src/lib/company-routes.ts +88 -0
- package/ui/src/lib/company-selection.test.ts +34 -0
- package/ui/src/lib/company-selection.ts +18 -0
- package/ui/src/lib/groupBy.ts +11 -0
- package/ui/src/lib/inbox.test.ts +404 -0
- package/ui/src/lib/inbox.ts +292 -0
- package/ui/src/lib/instance-settings.test.ts +26 -0
- package/ui/src/lib/instance-settings.ts +25 -0
- package/ui/src/lib/issueDetailBreadcrumb.ts +24 -0
- package/ui/src/lib/legacy-agent-config.test.ts +40 -0
- package/ui/src/lib/legacy-agent-config.ts +17 -0
- package/ui/src/lib/mention-aware-link-node.test.ts +50 -0
- package/ui/src/lib/mention-aware-link-node.ts +67 -0
- package/ui/src/lib/mention-chips.ts +168 -0
- package/ui/src/lib/mention-deletion.test.ts +87 -0
- package/ui/src/lib/mention-deletion.ts +143 -0
- package/ui/src/lib/model-utils.ts +16 -0
- package/ui/src/lib/onboarding-goal.test.ts +22 -0
- package/ui/src/lib/onboarding-goal.ts +18 -0
- package/ui/src/lib/onboarding-launch.test.ts +131 -0
- package/ui/src/lib/onboarding-launch.ts +54 -0
- package/ui/src/lib/onboarding-route.test.ts +80 -0
- package/ui/src/lib/onboarding-route.ts +51 -0
- package/ui/src/lib/portable-files.ts +42 -0
- package/ui/src/lib/project-order.ts +71 -0
- package/ui/src/lib/queryKeys.ts +140 -0
- package/ui/src/lib/recent-assignees.ts +36 -0
- package/ui/src/lib/router.tsx +76 -0
- package/ui/src/lib/routine-trigger-patch.test.ts +72 -0
- package/ui/src/lib/routine-trigger-patch.ts +31 -0
- package/ui/src/lib/status-colors.ts +108 -0
- package/ui/src/lib/timeAgo.ts +31 -0
- package/ui/src/lib/utils.ts +168 -0
- package/ui/src/lib/worktree-branding.ts +65 -0
- package/ui/src/lib/zip.test.ts +289 -0
- package/ui/src/lib/zip.ts +284 -0
- package/ui/src/main.tsx +67 -0
- package/ui/src/pages/Activity.tsx +141 -0
- package/ui/src/pages/AgentDetail.tsx +4053 -0
- package/ui/src/pages/Agents.tsx +415 -0
- package/ui/src/pages/ApprovalDetail.tsx +369 -0
- package/ui/src/pages/Approvals.tsx +132 -0
- package/ui/src/pages/Auth.tsx +180 -0
- package/ui/src/pages/BoardClaim.tsx +125 -0
- package/ui/src/pages/CliAuth.tsx +184 -0
- package/ui/src/pages/Companies.tsx +297 -0
- package/ui/src/pages/CompanyExport.tsx +1019 -0
- package/ui/src/pages/CompanyImport.tsx +1355 -0
- package/ui/src/pages/CompanySettings.tsx +661 -0
- package/ui/src/pages/CompanySkills.tsx +1171 -0
- package/ui/src/pages/Costs.tsx +1103 -0
- package/ui/src/pages/Dashboard.tsx +388 -0
- package/ui/src/pages/DesignGuide.tsx +1330 -0
- package/ui/src/pages/ExecutionWorkspaceDetail.tsx +82 -0
- package/ui/src/pages/GoalDetail.tsx +197 -0
- package/ui/src/pages/Goals.tsx +63 -0
- package/ui/src/pages/Inbox.tsx +1291 -0
- package/ui/src/pages/InstanceExperimentalSettings.tsx +139 -0
- package/ui/src/pages/InstanceGeneralSettings.tsx +104 -0
- package/ui/src/pages/InstanceSettings.tsx +284 -0
- package/ui/src/pages/InviteLanding.tsx +320 -0
- package/ui/src/pages/IssueDetail.tsx +1201 -0
- package/ui/src/pages/Issues.tsx +116 -0
- package/ui/src/pages/MyIssues.tsx +72 -0
- package/ui/src/pages/NewAgent.tsx +353 -0
- package/ui/src/pages/NotFound.tsx +66 -0
- package/ui/src/pages/Org.tsx +132 -0
- package/ui/src/pages/OrgChart.tsx +447 -0
- package/ui/src/pages/PluginManager.tsx +510 -0
- package/ui/src/pages/PluginPage.tsx +156 -0
- package/ui/src/pages/PluginSettings.tsx +836 -0
- package/ui/src/pages/ProjectDetail.tsx +633 -0
- package/ui/src/pages/Projects.tsx +87 -0
- package/ui/src/pages/RoutineDetail.tsx +1022 -0
- package/ui/src/pages/Routines.tsx +661 -0
- package/ui/src/pages/RunTranscriptUxLab.tsx +334 -0
- package/ui/src/pages/VisualOffice.tsx +243 -0
- package/ui/src/plugins/bridge-init.ts +69 -0
- package/ui/src/plugins/bridge.ts +476 -0
- package/ui/src/plugins/launchers.tsx +834 -0
- package/ui/src/plugins/slots.tsx +855 -0
- package/ui/tsconfig.json +21 -0
- package/ui/vite.config.ts +23 -0
- package/ui/vitest.config.ts +14 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { and, desc, eq, gte, inArray, lt, ne, sql } from "drizzle-orm";
|
|
3
|
+
import type { Db } from "@corporateai/db";
|
|
4
|
+
import {
|
|
5
|
+
agents,
|
|
6
|
+
agentConfigRevisions,
|
|
7
|
+
agentApiKeys,
|
|
8
|
+
agentRuntimeState,
|
|
9
|
+
agentTaskSessions,
|
|
10
|
+
agentWakeupRequests,
|
|
11
|
+
costEvents,
|
|
12
|
+
heartbeatRunEvents,
|
|
13
|
+
heartbeatRuns,
|
|
14
|
+
} from "@corporateai/db";
|
|
15
|
+
import { isUuidLike, normalizeAgentUrlKey } from "@corporateai/shared";
|
|
16
|
+
import { conflict, notFound, unprocessable } from "../errors.js";
|
|
17
|
+
import { normalizeAgentPermissions } from "./agent-permissions.js";
|
|
18
|
+
import { REDACTED_EVENT_VALUE, sanitizeRecord } from "../redaction.js";
|
|
19
|
+
|
|
20
|
+
function hashToken(token: string) {
|
|
21
|
+
return createHash("sha256").update(token).digest("hex");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createToken() {
|
|
25
|
+
return `pcp_${randomBytes(24).toString("hex")}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const CONFIG_REVISION_FIELDS = [
|
|
29
|
+
"name",
|
|
30
|
+
"role",
|
|
31
|
+
"title",
|
|
32
|
+
"reportsTo",
|
|
33
|
+
"capabilities",
|
|
34
|
+
"adapterType",
|
|
35
|
+
"adapterConfig",
|
|
36
|
+
"runtimeConfig",
|
|
37
|
+
"budgetMonthlyCents",
|
|
38
|
+
"metadata",
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
type ConfigRevisionField = (typeof CONFIG_REVISION_FIELDS)[number];
|
|
42
|
+
type AgentConfigSnapshot = Pick<typeof agents.$inferSelect, ConfigRevisionField>;
|
|
43
|
+
|
|
44
|
+
interface RevisionMetadata {
|
|
45
|
+
createdByAgentId?: string | null;
|
|
46
|
+
createdByUserId?: string | null;
|
|
47
|
+
source?: string;
|
|
48
|
+
rolledBackFromRevisionId?: string | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface UpdateAgentOptions {
|
|
52
|
+
recordRevision?: RevisionMetadata;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface AgentShortnameRow {
|
|
56
|
+
id: string;
|
|
57
|
+
name: string;
|
|
58
|
+
status: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface AgentShortnameCollisionOptions {
|
|
62
|
+
excludeAgentId?: string | null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
66
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function jsonEqual(left: unknown, right: unknown): boolean {
|
|
70
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildConfigSnapshot(
|
|
74
|
+
row: Pick<typeof agents.$inferSelect, ConfigRevisionField>,
|
|
75
|
+
): AgentConfigSnapshot {
|
|
76
|
+
const adapterConfig =
|
|
77
|
+
typeof row.adapterConfig === "object" && row.adapterConfig !== null && !Array.isArray(row.adapterConfig)
|
|
78
|
+
? sanitizeRecord(row.adapterConfig as Record<string, unknown>)
|
|
79
|
+
: {};
|
|
80
|
+
const runtimeConfig =
|
|
81
|
+
typeof row.runtimeConfig === "object" && row.runtimeConfig !== null && !Array.isArray(row.runtimeConfig)
|
|
82
|
+
? sanitizeRecord(row.runtimeConfig as Record<string, unknown>)
|
|
83
|
+
: {};
|
|
84
|
+
const metadata =
|
|
85
|
+
typeof row.metadata === "object" && row.metadata !== null && !Array.isArray(row.metadata)
|
|
86
|
+
? sanitizeRecord(row.metadata as Record<string, unknown>)
|
|
87
|
+
: row.metadata ?? null;
|
|
88
|
+
return {
|
|
89
|
+
name: row.name,
|
|
90
|
+
role: row.role,
|
|
91
|
+
title: row.title,
|
|
92
|
+
reportsTo: row.reportsTo,
|
|
93
|
+
capabilities: row.capabilities,
|
|
94
|
+
adapterType: row.adapterType,
|
|
95
|
+
adapterConfig,
|
|
96
|
+
runtimeConfig,
|
|
97
|
+
budgetMonthlyCents: row.budgetMonthlyCents,
|
|
98
|
+
metadata,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function containsRedactedMarker(value: unknown): boolean {
|
|
103
|
+
if (value === REDACTED_EVENT_VALUE) return true;
|
|
104
|
+
if (Array.isArray(value)) return value.some((item) => containsRedactedMarker(item));
|
|
105
|
+
if (typeof value !== "object" || value === null) return false;
|
|
106
|
+
return Object.values(value as Record<string, unknown>).some((entry) => containsRedactedMarker(entry));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function hasConfigPatchFields(data: Partial<typeof agents.$inferInsert>) {
|
|
110
|
+
return CONFIG_REVISION_FIELDS.some((field) => Object.prototype.hasOwnProperty.call(data, field));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function diffConfigSnapshot(
|
|
114
|
+
before: AgentConfigSnapshot,
|
|
115
|
+
after: AgentConfigSnapshot,
|
|
116
|
+
): string[] {
|
|
117
|
+
return CONFIG_REVISION_FIELDS.filter((field) => !jsonEqual(before[field], after[field]));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function configPatchFromSnapshot(snapshot: unknown): Partial<typeof agents.$inferInsert> {
|
|
121
|
+
if (!isPlainRecord(snapshot)) throw unprocessable("Invalid revision snapshot");
|
|
122
|
+
|
|
123
|
+
if (typeof snapshot.name !== "string" || snapshot.name.length === 0) {
|
|
124
|
+
throw unprocessable("Invalid revision snapshot: name");
|
|
125
|
+
}
|
|
126
|
+
if (typeof snapshot.role !== "string" || snapshot.role.length === 0) {
|
|
127
|
+
throw unprocessable("Invalid revision snapshot: role");
|
|
128
|
+
}
|
|
129
|
+
if (typeof snapshot.adapterType !== "string" || snapshot.adapterType.length === 0) {
|
|
130
|
+
throw unprocessable("Invalid revision snapshot: adapterType");
|
|
131
|
+
}
|
|
132
|
+
if (typeof snapshot.budgetMonthlyCents !== "number" || !Number.isFinite(snapshot.budgetMonthlyCents)) {
|
|
133
|
+
throw unprocessable("Invalid revision snapshot: budgetMonthlyCents");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
name: snapshot.name,
|
|
138
|
+
role: snapshot.role,
|
|
139
|
+
title: typeof snapshot.title === "string" || snapshot.title === null ? snapshot.title : null,
|
|
140
|
+
reportsTo:
|
|
141
|
+
typeof snapshot.reportsTo === "string" || snapshot.reportsTo === null ? snapshot.reportsTo : null,
|
|
142
|
+
capabilities:
|
|
143
|
+
typeof snapshot.capabilities === "string" || snapshot.capabilities === null
|
|
144
|
+
? snapshot.capabilities
|
|
145
|
+
: null,
|
|
146
|
+
adapterType: snapshot.adapterType,
|
|
147
|
+
adapterConfig: isPlainRecord(snapshot.adapterConfig) ? snapshot.adapterConfig : {},
|
|
148
|
+
runtimeConfig: isPlainRecord(snapshot.runtimeConfig) ? snapshot.runtimeConfig : {},
|
|
149
|
+
budgetMonthlyCents: Math.max(0, Math.floor(snapshot.budgetMonthlyCents)),
|
|
150
|
+
metadata: isPlainRecord(snapshot.metadata) || snapshot.metadata === null ? snapshot.metadata : null,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function hasAgentShortnameCollision(
|
|
155
|
+
candidateName: string,
|
|
156
|
+
existingAgents: AgentShortnameRow[],
|
|
157
|
+
options?: AgentShortnameCollisionOptions,
|
|
158
|
+
): boolean {
|
|
159
|
+
const candidateShortname = normalizeAgentUrlKey(candidateName);
|
|
160
|
+
if (!candidateShortname) return false;
|
|
161
|
+
|
|
162
|
+
return existingAgents.some((agent) => {
|
|
163
|
+
if (agent.status === "terminated") return false;
|
|
164
|
+
if (options?.excludeAgentId && agent.id === options.excludeAgentId) return false;
|
|
165
|
+
return normalizeAgentUrlKey(agent.name) === candidateShortname;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function deduplicateAgentName(
|
|
170
|
+
candidateName: string,
|
|
171
|
+
existingAgents: AgentShortnameRow[],
|
|
172
|
+
): string {
|
|
173
|
+
if (!hasAgentShortnameCollision(candidateName, existingAgents)) {
|
|
174
|
+
return candidateName;
|
|
175
|
+
}
|
|
176
|
+
for (let i = 2; i <= 100; i++) {
|
|
177
|
+
const suffixed = `${candidateName} ${i}`;
|
|
178
|
+
if (!hasAgentShortnameCollision(suffixed, existingAgents)) {
|
|
179
|
+
return suffixed;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return `${candidateName} ${Date.now()}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function agentService(db: Db) {
|
|
186
|
+
function currentUtcMonthWindow(now = new Date()) {
|
|
187
|
+
const year = now.getUTCFullYear();
|
|
188
|
+
const month = now.getUTCMonth();
|
|
189
|
+
return {
|
|
190
|
+
start: new Date(Date.UTC(year, month, 1, 0, 0, 0, 0)),
|
|
191
|
+
end: new Date(Date.UTC(year, month + 1, 1, 0, 0, 0, 0)),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function withUrlKey<T extends { id: string; name: string }>(row: T) {
|
|
196
|
+
return {
|
|
197
|
+
...row,
|
|
198
|
+
urlKey: normalizeAgentUrlKey(row.name) ?? row.id,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function normalizeAgentRow(row: typeof agents.$inferSelect) {
|
|
203
|
+
return withUrlKey({
|
|
204
|
+
...row,
|
|
205
|
+
permissions: normalizeAgentPermissions(row.permissions, row.role),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function getMonthlySpendByAgentIds(companyId: string, agentIds: string[]) {
|
|
210
|
+
if (agentIds.length === 0) return new Map<string, number>();
|
|
211
|
+
const { start, end } = currentUtcMonthWindow();
|
|
212
|
+
const rows = await db
|
|
213
|
+
.select({
|
|
214
|
+
agentId: costEvents.agentId,
|
|
215
|
+
spentMonthlyCents: sql<number>`coalesce(sum(${costEvents.costCents}), 0)::int`,
|
|
216
|
+
})
|
|
217
|
+
.from(costEvents)
|
|
218
|
+
.where(
|
|
219
|
+
and(
|
|
220
|
+
eq(costEvents.companyId, companyId),
|
|
221
|
+
inArray(costEvents.agentId, agentIds),
|
|
222
|
+
gte(costEvents.occurredAt, start),
|
|
223
|
+
lt(costEvents.occurredAt, end),
|
|
224
|
+
),
|
|
225
|
+
)
|
|
226
|
+
.groupBy(costEvents.agentId);
|
|
227
|
+
return new Map(rows.map((row) => [row.agentId, Number(row.spentMonthlyCents ?? 0)]));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function hydrateAgentSpend<T extends { id: string; companyId: string; spentMonthlyCents: number }>(rows: T[]) {
|
|
231
|
+
const agentIds = rows.map((row) => row.id);
|
|
232
|
+
const companyId = rows[0]?.companyId;
|
|
233
|
+
if (!companyId || agentIds.length === 0) return rows;
|
|
234
|
+
const spendByAgentId = await getMonthlySpendByAgentIds(companyId, agentIds);
|
|
235
|
+
return rows.map((row) => ({
|
|
236
|
+
...row,
|
|
237
|
+
spentMonthlyCents: spendByAgentId.get(row.id) ?? 0,
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function getById(id: string) {
|
|
242
|
+
const row = await db
|
|
243
|
+
.select()
|
|
244
|
+
.from(agents)
|
|
245
|
+
.where(eq(agents.id, id))
|
|
246
|
+
.then((rows) => rows[0] ?? null);
|
|
247
|
+
if (!row) return null;
|
|
248
|
+
const [hydrated] = await hydrateAgentSpend([row]);
|
|
249
|
+
return normalizeAgentRow(hydrated);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function ensureManager(companyId: string, managerId: string) {
|
|
253
|
+
const manager = await getById(managerId);
|
|
254
|
+
if (!manager) throw notFound("Manager not found");
|
|
255
|
+
if (manager.companyId !== companyId) {
|
|
256
|
+
throw unprocessable("Manager must belong to same company");
|
|
257
|
+
}
|
|
258
|
+
return manager;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function assertNoCycle(agentId: string, reportsTo: string | null | undefined) {
|
|
262
|
+
if (!reportsTo) return;
|
|
263
|
+
if (reportsTo === agentId) throw unprocessable("Agent cannot report to itself");
|
|
264
|
+
|
|
265
|
+
let cursor: string | null = reportsTo;
|
|
266
|
+
while (cursor) {
|
|
267
|
+
if (cursor === agentId) throw unprocessable("Reporting relationship would create cycle");
|
|
268
|
+
const next = await getById(cursor);
|
|
269
|
+
cursor = next?.reportsTo ?? null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function assertCompanyShortnameAvailable(
|
|
274
|
+
companyId: string,
|
|
275
|
+
candidateName: string,
|
|
276
|
+
options?: AgentShortnameCollisionOptions,
|
|
277
|
+
) {
|
|
278
|
+
const candidateShortname = normalizeAgentUrlKey(candidateName);
|
|
279
|
+
if (!candidateShortname) return;
|
|
280
|
+
|
|
281
|
+
const existingAgents = await db
|
|
282
|
+
.select({
|
|
283
|
+
id: agents.id,
|
|
284
|
+
name: agents.name,
|
|
285
|
+
status: agents.status,
|
|
286
|
+
})
|
|
287
|
+
.from(agents)
|
|
288
|
+
.where(eq(agents.companyId, companyId));
|
|
289
|
+
|
|
290
|
+
const hasCollision = hasAgentShortnameCollision(candidateName, existingAgents, options);
|
|
291
|
+
if (hasCollision) {
|
|
292
|
+
throw conflict(
|
|
293
|
+
`Agent shortname '${candidateShortname}' is already in use in this company`,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function updateAgent(
|
|
299
|
+
id: string,
|
|
300
|
+
data: Partial<typeof agents.$inferInsert>,
|
|
301
|
+
options?: UpdateAgentOptions,
|
|
302
|
+
) {
|
|
303
|
+
const existing = await getById(id);
|
|
304
|
+
if (!existing) return null;
|
|
305
|
+
|
|
306
|
+
if (existing.status === "terminated" && data.status && data.status !== "terminated") {
|
|
307
|
+
throw conflict("Terminated agents cannot be resumed");
|
|
308
|
+
}
|
|
309
|
+
if (
|
|
310
|
+
existing.status === "pending_approval" &&
|
|
311
|
+
data.status &&
|
|
312
|
+
data.status !== "pending_approval" &&
|
|
313
|
+
data.status !== "terminated"
|
|
314
|
+
) {
|
|
315
|
+
throw conflict("Pending approval agents cannot be activated directly");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (data.reportsTo !== undefined) {
|
|
319
|
+
if (data.reportsTo) {
|
|
320
|
+
await ensureManager(existing.companyId, data.reportsTo);
|
|
321
|
+
}
|
|
322
|
+
await assertNoCycle(id, data.reportsTo);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (data.name !== undefined) {
|
|
326
|
+
const previousShortname = normalizeAgentUrlKey(existing.name);
|
|
327
|
+
const nextShortname = normalizeAgentUrlKey(data.name);
|
|
328
|
+
if (previousShortname !== nextShortname) {
|
|
329
|
+
await assertCompanyShortnameAvailable(existing.companyId, data.name, { excludeAgentId: id });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const normalizedPatch = { ...data } as Partial<typeof agents.$inferInsert>;
|
|
334
|
+
if (data.permissions !== undefined) {
|
|
335
|
+
const role = (data.role ?? existing.role) as string;
|
|
336
|
+
normalizedPatch.permissions = normalizeAgentPermissions(data.permissions, role);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const shouldRecordRevision = Boolean(options?.recordRevision) && hasConfigPatchFields(normalizedPatch);
|
|
340
|
+
const beforeConfig = shouldRecordRevision ? buildConfigSnapshot(existing) : null;
|
|
341
|
+
|
|
342
|
+
const updated = await db
|
|
343
|
+
.update(agents)
|
|
344
|
+
.set({ ...normalizedPatch, updatedAt: new Date() })
|
|
345
|
+
.where(eq(agents.id, id))
|
|
346
|
+
.returning()
|
|
347
|
+
.then((rows) => rows[0] ?? null);
|
|
348
|
+
const normalizedUpdated = updated ? normalizeAgentRow(updated) : null;
|
|
349
|
+
|
|
350
|
+
if (normalizedUpdated && shouldRecordRevision && beforeConfig) {
|
|
351
|
+
const afterConfig = buildConfigSnapshot(normalizedUpdated);
|
|
352
|
+
const changedKeys = diffConfigSnapshot(beforeConfig, afterConfig);
|
|
353
|
+
if (changedKeys.length > 0) {
|
|
354
|
+
await db.insert(agentConfigRevisions).values({
|
|
355
|
+
companyId: normalizedUpdated.companyId,
|
|
356
|
+
agentId: normalizedUpdated.id,
|
|
357
|
+
createdByAgentId: options?.recordRevision?.createdByAgentId ?? null,
|
|
358
|
+
createdByUserId: options?.recordRevision?.createdByUserId ?? null,
|
|
359
|
+
source: options?.recordRevision?.source ?? "patch",
|
|
360
|
+
rolledBackFromRevisionId: options?.recordRevision?.rolledBackFromRevisionId ?? null,
|
|
361
|
+
changedKeys,
|
|
362
|
+
beforeConfig: beforeConfig as unknown as Record<string, unknown>,
|
|
363
|
+
afterConfig: afterConfig as unknown as Record<string, unknown>,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return normalizedUpdated;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
list: async (companyId: string, options?: { includeTerminated?: boolean }) => {
|
|
373
|
+
const conditions = [eq(agents.companyId, companyId)];
|
|
374
|
+
if (!options?.includeTerminated) {
|
|
375
|
+
conditions.push(ne(agents.status, "terminated"));
|
|
376
|
+
}
|
|
377
|
+
const rows = await db.select().from(agents).where(and(...conditions));
|
|
378
|
+
const hydrated = await hydrateAgentSpend(rows);
|
|
379
|
+
return hydrated.map(normalizeAgentRow);
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
getById,
|
|
383
|
+
|
|
384
|
+
create: async (companyId: string, data: Omit<typeof agents.$inferInsert, "companyId">) => {
|
|
385
|
+
if (data.reportsTo) {
|
|
386
|
+
await ensureManager(companyId, data.reportsTo);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const existingAgents = await db
|
|
390
|
+
.select({ id: agents.id, name: agents.name, status: agents.status })
|
|
391
|
+
.from(agents)
|
|
392
|
+
.where(eq(agents.companyId, companyId));
|
|
393
|
+
const uniqueName = deduplicateAgentName(data.name, existingAgents);
|
|
394
|
+
|
|
395
|
+
const role = data.role ?? "general";
|
|
396
|
+
const normalizedPermissions = normalizeAgentPermissions(data.permissions, role);
|
|
397
|
+
const created = await db
|
|
398
|
+
.insert(agents)
|
|
399
|
+
.values({ ...data, name: uniqueName, companyId, role, permissions: normalizedPermissions })
|
|
400
|
+
.returning()
|
|
401
|
+
.then((rows) => rows[0]);
|
|
402
|
+
|
|
403
|
+
return normalizeAgentRow(created);
|
|
404
|
+
},
|
|
405
|
+
|
|
406
|
+
update: updateAgent,
|
|
407
|
+
|
|
408
|
+
pause: async (id: string, reason: "manual" | "budget" | "system" = "manual") => {
|
|
409
|
+
const existing = await getById(id);
|
|
410
|
+
if (!existing) return null;
|
|
411
|
+
if (existing.status === "terminated") throw conflict("Cannot pause terminated agent");
|
|
412
|
+
|
|
413
|
+
const updated = await db
|
|
414
|
+
.update(agents)
|
|
415
|
+
.set({
|
|
416
|
+
status: "paused",
|
|
417
|
+
pauseReason: reason,
|
|
418
|
+
pausedAt: new Date(),
|
|
419
|
+
updatedAt: new Date(),
|
|
420
|
+
})
|
|
421
|
+
.where(eq(agents.id, id))
|
|
422
|
+
.returning()
|
|
423
|
+
.then((rows) => rows[0] ?? null);
|
|
424
|
+
return updated ? normalizeAgentRow(updated) : null;
|
|
425
|
+
},
|
|
426
|
+
|
|
427
|
+
resume: async (id: string) => {
|
|
428
|
+
const existing = await getById(id);
|
|
429
|
+
if (!existing) return null;
|
|
430
|
+
if (existing.status === "terminated") throw conflict("Cannot resume terminated agent");
|
|
431
|
+
if (existing.status === "pending_approval") {
|
|
432
|
+
throw conflict("Pending approval agents cannot be resumed");
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const updated = await db
|
|
436
|
+
.update(agents)
|
|
437
|
+
.set({
|
|
438
|
+
status: "idle",
|
|
439
|
+
pauseReason: null,
|
|
440
|
+
pausedAt: null,
|
|
441
|
+
updatedAt: new Date(),
|
|
442
|
+
})
|
|
443
|
+
.where(eq(agents.id, id))
|
|
444
|
+
.returning()
|
|
445
|
+
.then((rows) => rows[0] ?? null);
|
|
446
|
+
return updated ? normalizeAgentRow(updated) : null;
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
terminate: async (id: string) => {
|
|
450
|
+
const existing = await getById(id);
|
|
451
|
+
if (!existing) return null;
|
|
452
|
+
|
|
453
|
+
await db
|
|
454
|
+
.update(agents)
|
|
455
|
+
.set({
|
|
456
|
+
status: "terminated",
|
|
457
|
+
pauseReason: null,
|
|
458
|
+
pausedAt: null,
|
|
459
|
+
updatedAt: new Date(),
|
|
460
|
+
})
|
|
461
|
+
.where(eq(agents.id, id));
|
|
462
|
+
|
|
463
|
+
await db
|
|
464
|
+
.update(agentApiKeys)
|
|
465
|
+
.set({ revokedAt: new Date() })
|
|
466
|
+
.where(eq(agentApiKeys.agentId, id));
|
|
467
|
+
|
|
468
|
+
return getById(id);
|
|
469
|
+
},
|
|
470
|
+
|
|
471
|
+
remove: async (id: string) => {
|
|
472
|
+
const existing = await getById(id);
|
|
473
|
+
if (!existing) return null;
|
|
474
|
+
|
|
475
|
+
return db.transaction(async (tx) => {
|
|
476
|
+
await tx.update(agents).set({ reportsTo: null }).where(eq(agents.reportsTo, id));
|
|
477
|
+
await tx.delete(heartbeatRunEvents).where(eq(heartbeatRunEvents.agentId, id));
|
|
478
|
+
await tx.delete(agentTaskSessions).where(eq(agentTaskSessions.agentId, id));
|
|
479
|
+
await tx.delete(heartbeatRuns).where(eq(heartbeatRuns.agentId, id));
|
|
480
|
+
await tx.delete(agentWakeupRequests).where(eq(agentWakeupRequests.agentId, id));
|
|
481
|
+
await tx.delete(agentApiKeys).where(eq(agentApiKeys.agentId, id));
|
|
482
|
+
await tx.delete(agentRuntimeState).where(eq(agentRuntimeState.agentId, id));
|
|
483
|
+
const deleted = await tx
|
|
484
|
+
.delete(agents)
|
|
485
|
+
.where(eq(agents.id, id))
|
|
486
|
+
.returning()
|
|
487
|
+
.then((rows) => rows[0] ?? null);
|
|
488
|
+
return deleted ? normalizeAgentRow(deleted) : null;
|
|
489
|
+
});
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
activatePendingApproval: async (id: string) => {
|
|
493
|
+
const existing = await getById(id);
|
|
494
|
+
if (!existing) return null;
|
|
495
|
+
if (existing.status !== "pending_approval") return existing;
|
|
496
|
+
|
|
497
|
+
const updated = await db
|
|
498
|
+
.update(agents)
|
|
499
|
+
.set({ status: "idle", updatedAt: new Date() })
|
|
500
|
+
.where(eq(agents.id, id))
|
|
501
|
+
.returning()
|
|
502
|
+
.then((rows) => rows[0] ?? null);
|
|
503
|
+
|
|
504
|
+
return updated ? normalizeAgentRow(updated) : null;
|
|
505
|
+
},
|
|
506
|
+
|
|
507
|
+
updatePermissions: async (id: string, permissions: { canCreateAgents: boolean }) => {
|
|
508
|
+
const existing = await getById(id);
|
|
509
|
+
if (!existing) return null;
|
|
510
|
+
|
|
511
|
+
const updated = await db
|
|
512
|
+
.update(agents)
|
|
513
|
+
.set({
|
|
514
|
+
permissions: normalizeAgentPermissions(permissions, existing.role),
|
|
515
|
+
updatedAt: new Date(),
|
|
516
|
+
})
|
|
517
|
+
.where(eq(agents.id, id))
|
|
518
|
+
.returning()
|
|
519
|
+
.then((rows) => rows[0] ?? null);
|
|
520
|
+
|
|
521
|
+
return updated ? normalizeAgentRow(updated) : null;
|
|
522
|
+
},
|
|
523
|
+
|
|
524
|
+
listConfigRevisions: async (id: string) =>
|
|
525
|
+
db
|
|
526
|
+
.select()
|
|
527
|
+
.from(agentConfigRevisions)
|
|
528
|
+
.where(eq(agentConfigRevisions.agentId, id))
|
|
529
|
+
.orderBy(desc(agentConfigRevisions.createdAt)),
|
|
530
|
+
|
|
531
|
+
getConfigRevision: async (id: string, revisionId: string) =>
|
|
532
|
+
db
|
|
533
|
+
.select()
|
|
534
|
+
.from(agentConfigRevisions)
|
|
535
|
+
.where(and(eq(agentConfigRevisions.agentId, id), eq(agentConfigRevisions.id, revisionId)))
|
|
536
|
+
.then((rows) => rows[0] ?? null),
|
|
537
|
+
|
|
538
|
+
rollbackConfigRevision: async (
|
|
539
|
+
id: string,
|
|
540
|
+
revisionId: string,
|
|
541
|
+
actor: { agentId?: string | null; userId?: string | null },
|
|
542
|
+
) => {
|
|
543
|
+
const revision = await db
|
|
544
|
+
.select()
|
|
545
|
+
.from(agentConfigRevisions)
|
|
546
|
+
.where(and(eq(agentConfigRevisions.agentId, id), eq(agentConfigRevisions.id, revisionId)))
|
|
547
|
+
.then((rows) => rows[0] ?? null);
|
|
548
|
+
if (!revision) return null;
|
|
549
|
+
if (containsRedactedMarker(revision.afterConfig)) {
|
|
550
|
+
throw unprocessable("Cannot roll back a revision that contains redacted secret values");
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const patch = configPatchFromSnapshot(revision.afterConfig);
|
|
554
|
+
return updateAgent(id, patch, {
|
|
555
|
+
recordRevision: {
|
|
556
|
+
createdByAgentId: actor.agentId ?? null,
|
|
557
|
+
createdByUserId: actor.userId ?? null,
|
|
558
|
+
source: "rollback",
|
|
559
|
+
rolledBackFromRevisionId: revision.id,
|
|
560
|
+
},
|
|
561
|
+
});
|
|
562
|
+
},
|
|
563
|
+
|
|
564
|
+
createApiKey: async (id: string, name: string) => {
|
|
565
|
+
const existing = await getById(id);
|
|
566
|
+
if (!existing) throw notFound("Agent not found");
|
|
567
|
+
if (existing.status === "pending_approval") {
|
|
568
|
+
throw conflict("Cannot create keys for pending approval agents");
|
|
569
|
+
}
|
|
570
|
+
if (existing.status === "terminated") {
|
|
571
|
+
throw conflict("Cannot create keys for terminated agents");
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const token = createToken();
|
|
575
|
+
const keyHash = hashToken(token);
|
|
576
|
+
const created = await db
|
|
577
|
+
.insert(agentApiKeys)
|
|
578
|
+
.values({
|
|
579
|
+
agentId: id,
|
|
580
|
+
companyId: existing.companyId,
|
|
581
|
+
name,
|
|
582
|
+
keyHash,
|
|
583
|
+
})
|
|
584
|
+
.returning()
|
|
585
|
+
.then((rows) => rows[0]);
|
|
586
|
+
|
|
587
|
+
return {
|
|
588
|
+
id: created.id,
|
|
589
|
+
name: created.name,
|
|
590
|
+
token,
|
|
591
|
+
createdAt: created.createdAt,
|
|
592
|
+
};
|
|
593
|
+
},
|
|
594
|
+
|
|
595
|
+
listKeys: (id: string) =>
|
|
596
|
+
db
|
|
597
|
+
.select({
|
|
598
|
+
id: agentApiKeys.id,
|
|
599
|
+
name: agentApiKeys.name,
|
|
600
|
+
createdAt: agentApiKeys.createdAt,
|
|
601
|
+
revokedAt: agentApiKeys.revokedAt,
|
|
602
|
+
})
|
|
603
|
+
.from(agentApiKeys)
|
|
604
|
+
.where(eq(agentApiKeys.agentId, id)),
|
|
605
|
+
|
|
606
|
+
revokeKey: async (keyId: string) => {
|
|
607
|
+
const rows = await db
|
|
608
|
+
.update(agentApiKeys)
|
|
609
|
+
.set({ revokedAt: new Date() })
|
|
610
|
+
.where(eq(agentApiKeys.id, keyId))
|
|
611
|
+
.returning();
|
|
612
|
+
return rows[0] ?? null;
|
|
613
|
+
},
|
|
614
|
+
|
|
615
|
+
orgForCompany: async (companyId: string) => {
|
|
616
|
+
const rows = await db
|
|
617
|
+
.select()
|
|
618
|
+
.from(agents)
|
|
619
|
+
.where(and(eq(agents.companyId, companyId), ne(agents.status, "terminated")));
|
|
620
|
+
const normalizedRows = rows.map(normalizeAgentRow);
|
|
621
|
+
const byManager = new Map<string | null, typeof normalizedRows>();
|
|
622
|
+
for (const row of normalizedRows) {
|
|
623
|
+
const key = row.reportsTo ?? null;
|
|
624
|
+
const group = byManager.get(key) ?? [];
|
|
625
|
+
group.push(row);
|
|
626
|
+
byManager.set(key, group);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const build = (managerId: string | null): Array<Record<string, unknown>> => {
|
|
630
|
+
const members = byManager.get(managerId) ?? [];
|
|
631
|
+
return members.map((member) => ({
|
|
632
|
+
...member,
|
|
633
|
+
reports: build(member.id),
|
|
634
|
+
}));
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
return build(null);
|
|
638
|
+
},
|
|
639
|
+
|
|
640
|
+
getChainOfCommand: async (agentId: string) => {
|
|
641
|
+
const chain: { id: string; name: string; role: string; title: string | null }[] = [];
|
|
642
|
+
const visited = new Set<string>([agentId]);
|
|
643
|
+
const start = await getById(agentId);
|
|
644
|
+
let currentId = start?.reportsTo ?? null;
|
|
645
|
+
while (currentId && !visited.has(currentId) && chain.length < 50) {
|
|
646
|
+
visited.add(currentId);
|
|
647
|
+
const mgr = await getById(currentId);
|
|
648
|
+
if (!mgr) break;
|
|
649
|
+
chain.push({ id: mgr.id, name: mgr.name, role: mgr.role, title: mgr.title ?? null });
|
|
650
|
+
currentId = mgr.reportsTo ?? null;
|
|
651
|
+
}
|
|
652
|
+
return chain;
|
|
653
|
+
},
|
|
654
|
+
|
|
655
|
+
runningForAgent: (agentId: string) =>
|
|
656
|
+
db
|
|
657
|
+
.select()
|
|
658
|
+
.from(heartbeatRuns)
|
|
659
|
+
.where(and(eq(heartbeatRuns.agentId, agentId), inArray(heartbeatRuns.status, ["queued", "running"]))),
|
|
660
|
+
|
|
661
|
+
resolveByReference: async (companyId: string, reference: string) => {
|
|
662
|
+
const raw = reference.trim();
|
|
663
|
+
if (raw.length === 0) {
|
|
664
|
+
return { agent: null, ambiguous: false } as const;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (isUuidLike(raw)) {
|
|
668
|
+
const byId = await getById(raw);
|
|
669
|
+
if (!byId || byId.companyId !== companyId) {
|
|
670
|
+
return { agent: null, ambiguous: false } as const;
|
|
671
|
+
}
|
|
672
|
+
return { agent: byId, ambiguous: false } as const;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const urlKey = normalizeAgentUrlKey(raw);
|
|
676
|
+
if (!urlKey) {
|
|
677
|
+
return { agent: null, ambiguous: false } as const;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const rows = await db.select().from(agents).where(eq(agents.companyId, companyId));
|
|
681
|
+
const matches = rows
|
|
682
|
+
.map(normalizeAgentRow)
|
|
683
|
+
.filter((agent) => agent.urlKey === urlKey && agent.status !== "terminated");
|
|
684
|
+
if (matches.length === 1) {
|
|
685
|
+
return { agent: matches[0] ?? null, ambiguous: false } as const;
|
|
686
|
+
}
|
|
687
|
+
if (matches.length > 1) {
|
|
688
|
+
return { agent: null, ambiguous: true } as const;
|
|
689
|
+
}
|
|
690
|
+
return { agent: null, ambiguous: false } as const;
|
|
691
|
+
},
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
|