@useatlas/create 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/README.md +231 -0
- package/index.ts +829 -0
- package/package.json +38 -0
- package/templates/docker/.env.example +67 -0
- package/templates/docker/Dockerfile +52 -0
- package/templates/docker/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/docker/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/docker/bin/__tests__/eval.test.ts +434 -0
- package/templates/docker/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/docker/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/docker/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/docker/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/docker/bin/__tests__/query.test.ts +240 -0
- package/templates/docker/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/docker/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/docker/bin/atlas.ts +5044 -0
- package/templates/docker/bin/benchmark.ts +695 -0
- package/templates/docker/bin/enrich.ts +559 -0
- package/templates/docker/bin/eval.ts +770 -0
- package/templates/docker/bin/smoke.ts +438 -0
- package/templates/docker/data/.gitkeep +0 -0
- package/templates/docker/data/cybersec.sql +1961 -0
- package/templates/docker/data/demo-semantic/catalog.yml +40 -0
- package/templates/docker/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/docker/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/docker/data/demo-semantic/entities/people.yml +145 -0
- package/templates/docker/data/demo-semantic/glossary.yml +22 -0
- package/templates/docker/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/docker/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/docker/data/demo.sql +373 -0
- package/templates/docker/data/ecommerce.sql +1690 -0
- package/templates/docker/data/init-demo-db.sql +8 -0
- package/templates/docker/docker-compose.yml +34 -0
- package/templates/docker/docs/deploy.md +390 -0
- package/templates/docker/eslint.config.mjs +18 -0
- package/templates/docker/gitignore +5 -0
- package/templates/docker/next.config.ts +9 -0
- package/templates/docker/package.json +59 -0
- package/templates/docker/postcss.config.mjs +8 -0
- package/templates/docker/public/.gitkeep +0 -0
- package/templates/docker/public/favicon.svg +4 -0
- package/templates/docker/railway.json +13 -0
- package/templates/docker/render.yaml +34 -0
- package/templates/docker/semantic/catalog.yml +5 -0
- package/templates/docker/semantic/entities/.gitkeep +0 -0
- package/templates/docker/semantic/glossary.yml +6 -0
- package/templates/docker/semantic/metrics/.gitkeep +0 -0
- package/templates/docker/sidecar/Dockerfile +28 -0
- package/templates/docker/sidecar/railway.json +14 -0
- package/templates/docker/sidecar/server.ts +188 -0
- package/templates/docker/src/api/__tests__/actions.test.ts +683 -0
- package/templates/docker/src/api/__tests__/admin.test.ts +820 -0
- package/templates/docker/src/api/__tests__/auth.test.ts +165 -0
- package/templates/docker/src/api/__tests__/chat.test.ts +376 -0
- package/templates/docker/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/docker/src/api/__tests__/cors.test.ts +135 -0
- package/templates/docker/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/docker/src/api/__tests__/health.test.ts +261 -0
- package/templates/docker/src/api/__tests__/query.test.ts +891 -0
- package/templates/docker/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/docker/src/api/__tests__/slack.test.ts +847 -0
- package/templates/docker/src/api/index.ts +117 -0
- package/templates/docker/src/api/routes/actions.ts +274 -0
- package/templates/docker/src/api/routes/admin.ts +757 -0
- package/templates/docker/src/api/routes/auth.ts +48 -0
- package/templates/docker/src/api/routes/chat.ts +465 -0
- package/templates/docker/src/api/routes/conversations.ts +266 -0
- package/templates/docker/src/api/routes/health.ts +287 -0
- package/templates/docker/src/api/routes/openapi.ts +390 -0
- package/templates/docker/src/api/routes/query.ts +318 -0
- package/templates/docker/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/docker/src/api/routes/slack.ts +611 -0
- package/templates/docker/src/api/server.ts +226 -0
- package/templates/docker/src/app/api/[...route]/route.ts +33 -0
- package/templates/docker/src/app/error.tsx +24 -0
- package/templates/docker/src/app/globals.css +126 -0
- package/templates/docker/src/app/layout.tsx +19 -0
- package/templates/docker/src/app/page.tsx +14 -0
- package/templates/docker/src/global.d.ts +1 -0
- package/templates/docker/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/docker/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/docker/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/docker/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/docker/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/docker/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/docker/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/docker/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/docker/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/docker/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/docker/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/docker/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/docker/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/docker/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/docker/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/docker/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/docker/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/docker/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/docker/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/docker/src/lib/action-types.ts +95 -0
- package/templates/docker/src/lib/agent-query.ts +178 -0
- package/templates/docker/src/lib/agent.ts +505 -0
- package/templates/docker/src/lib/api-url.ts +2 -0
- package/templates/docker/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/docker/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/docker/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/docker/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/docker/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/docker/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/docker/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/docker/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/docker/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/docker/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/docker/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/docker/src/lib/auth/audit.ts +89 -0
- package/templates/docker/src/lib/auth/byot.ts +158 -0
- package/templates/docker/src/lib/auth/client.ts +35 -0
- package/templates/docker/src/lib/auth/detect.ts +83 -0
- package/templates/docker/src/lib/auth/managed.ts +73 -0
- package/templates/docker/src/lib/auth/middleware.ts +208 -0
- package/templates/docker/src/lib/auth/migrate.ts +111 -0
- package/templates/docker/src/lib/auth/permissions.ts +156 -0
- package/templates/docker/src/lib/auth/server.ts +142 -0
- package/templates/docker/src/lib/auth/simple-key.ts +92 -0
- package/templates/docker/src/lib/auth/types.ts +49 -0
- package/templates/docker/src/lib/config.ts +704 -0
- package/templates/docker/src/lib/conversation-types.ts +29 -0
- package/templates/docker/src/lib/conversations.ts +270 -0
- package/templates/docker/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/docker/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/docker/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/docker/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/docker/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/docker/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/docker/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/docker/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/docker/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/docker/src/lib/db/connection.ts +753 -0
- package/templates/docker/src/lib/db/duckdb.ts +122 -0
- package/templates/docker/src/lib/db/internal.ts +273 -0
- package/templates/docker/src/lib/db/salesforce.ts +342 -0
- package/templates/docker/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/docker/src/lib/errors.ts +154 -0
- package/templates/docker/src/lib/logger.ts +98 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/docker/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/docker/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/docker/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/docker/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/docker/src/lib/plugins/hooks.ts +162 -0
- package/templates/docker/src/lib/plugins/index.ts +9 -0
- package/templates/docker/src/lib/plugins/migrate.ts +309 -0
- package/templates/docker/src/lib/plugins/registry.ts +231 -0
- package/templates/docker/src/lib/plugins/tools.ts +39 -0
- package/templates/docker/src/lib/plugins/wiring.ts +291 -0
- package/templates/docker/src/lib/providers.ts +102 -0
- package/templates/docker/src/lib/rls.ts +321 -0
- package/templates/docker/src/lib/scheduled-task-types.ts +132 -0
- package/templates/docker/src/lib/scheduled-tasks.ts +475 -0
- package/templates/docker/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/docker/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/delivery.ts +248 -0
- package/templates/docker/src/lib/scheduler/engine.ts +317 -0
- package/templates/docker/src/lib/scheduler/executor.ts +73 -0
- package/templates/docker/src/lib/scheduler/format-email.ts +109 -0
- package/templates/docker/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/docker/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/docker/src/lib/scheduler/index.ts +7 -0
- package/templates/docker/src/lib/security.ts +11 -0
- package/templates/docker/src/lib/semantic-index.ts +503 -0
- package/templates/docker/src/lib/semantic.ts +387 -0
- package/templates/docker/src/lib/sidecar-types.ts +16 -0
- package/templates/docker/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/docker/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/docker/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/docker/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/docker/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/docker/src/lib/slack/api.ts +102 -0
- package/templates/docker/src/lib/slack/format.ts +209 -0
- package/templates/docker/src/lib/slack/store.ts +107 -0
- package/templates/docker/src/lib/slack/threads.ts +64 -0
- package/templates/docker/src/lib/slack/verify.ts +71 -0
- package/templates/docker/src/lib/startup.ts +730 -0
- package/templates/docker/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/docker/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/docker/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/docker/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/docker/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/docker/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/docker/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/docker/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/docker/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/docker/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/docker/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/docker/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/docker/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/docker/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/docker/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/docker/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/docker/src/lib/tools/actions/audit.ts +47 -0
- package/templates/docker/src/lib/tools/actions/email.ts +191 -0
- package/templates/docker/src/lib/tools/actions/handler.ts +591 -0
- package/templates/docker/src/lib/tools/actions/index.ts +23 -0
- package/templates/docker/src/lib/tools/actions/jira.ts +220 -0
- package/templates/docker/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/docker/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/docker/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/docker/src/lib/tools/explore.ts +379 -0
- package/templates/docker/src/lib/tools/registry.ts +221 -0
- package/templates/docker/src/lib/tools/salesforce.ts +138 -0
- package/templates/docker/src/lib/tools/soql-validation.ts +172 -0
- package/templates/docker/src/lib/tools/sql.ts +680 -0
- package/templates/docker/src/lib/tracing.ts +40 -0
- package/templates/docker/src/lib/utils.ts +6 -0
- package/templates/docker/src/test-setup.ts +38 -0
- package/templates/docker/src/types/vercel-sandbox.d.ts +54 -0
- package/templates/docker/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/docker/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/docker/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/docker/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/docker/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/docker/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/docker/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/docker/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/docker/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/docker/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/docker/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/docker/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/docker/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/docker/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/docker/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/docker/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/docker/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/docker/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/docker/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/docker/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/docker/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/docker/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/docker/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/docker/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/docker/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/docker/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/docker/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/docker/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/docker/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/docker/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/docker/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/docker/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/docker/src/ui/context.tsx +78 -0
- package/templates/docker/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/docker/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/docker/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/docker/src/ui/lib/action-types.ts +63 -0
- package/templates/docker/src/ui/lib/helpers.ts +104 -0
- package/templates/docker/src/ui/lib/types.ts +145 -0
- package/templates/docker/tsconfig.json +41 -0
- package/templates/docker/vercel.json +3 -0
- package/templates/nextjs-standalone/.env.example +68 -0
- package/templates/nextjs-standalone/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/nextjs-standalone/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/nextjs-standalone/bin/__tests__/eval.test.ts +434 -0
- package/templates/nextjs-standalone/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/nextjs-standalone/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/nextjs-standalone/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/nextjs-standalone/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/nextjs-standalone/bin/__tests__/query.test.ts +240 -0
- package/templates/nextjs-standalone/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/nextjs-standalone/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/nextjs-standalone/bin/atlas.ts +5044 -0
- package/templates/nextjs-standalone/bin/benchmark.ts +695 -0
- package/templates/nextjs-standalone/bin/enrich.ts +559 -0
- package/templates/nextjs-standalone/bin/eval.ts +770 -0
- package/templates/nextjs-standalone/bin/smoke.ts +438 -0
- package/templates/nextjs-standalone/data/.gitkeep +0 -0
- package/templates/nextjs-standalone/data/cybersec.sql +1961 -0
- package/templates/nextjs-standalone/data/demo-semantic/catalog.yml +40 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/people.yml +145 -0
- package/templates/nextjs-standalone/data/demo-semantic/glossary.yml +22 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/nextjs-standalone/data/demo.sql +373 -0
- package/templates/nextjs-standalone/data/ecommerce.sql +1690 -0
- package/templates/nextjs-standalone/data/init-demo-db.sql +8 -0
- package/templates/nextjs-standalone/docs/deploy.md +390 -0
- package/templates/nextjs-standalone/eslint.config.mjs +18 -0
- package/templates/nextjs-standalone/gitignore +5 -0
- package/templates/nextjs-standalone/next.config.ts +10 -0
- package/templates/nextjs-standalone/package.json +63 -0
- package/templates/nextjs-standalone/postcss.config.mjs +8 -0
- package/templates/nextjs-standalone/semantic/catalog.yml +5 -0
- package/templates/nextjs-standalone/semantic/entities/.gitkeep +0 -0
- package/templates/nextjs-standalone/semantic/glossary.yml +6 -0
- package/templates/nextjs-standalone/semantic/metrics/.gitkeep +0 -0
- package/templates/nextjs-standalone/src/api/__tests__/actions.test.ts +683 -0
- package/templates/nextjs-standalone/src/api/__tests__/admin.test.ts +820 -0
- package/templates/nextjs-standalone/src/api/__tests__/auth.test.ts +165 -0
- package/templates/nextjs-standalone/src/api/__tests__/chat.test.ts +376 -0
- package/templates/nextjs-standalone/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/nextjs-standalone/src/api/__tests__/cors.test.ts +135 -0
- package/templates/nextjs-standalone/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/nextjs-standalone/src/api/__tests__/health.test.ts +261 -0
- package/templates/nextjs-standalone/src/api/__tests__/query.test.ts +891 -0
- package/templates/nextjs-standalone/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/nextjs-standalone/src/api/__tests__/slack.test.ts +847 -0
- package/templates/nextjs-standalone/src/api/index.ts +117 -0
- package/templates/nextjs-standalone/src/api/routes/actions.ts +274 -0
- package/templates/nextjs-standalone/src/api/routes/admin.ts +757 -0
- package/templates/nextjs-standalone/src/api/routes/auth.ts +48 -0
- package/templates/nextjs-standalone/src/api/routes/chat.ts +465 -0
- package/templates/nextjs-standalone/src/api/routes/conversations.ts +266 -0
- package/templates/nextjs-standalone/src/api/routes/health.ts +287 -0
- package/templates/nextjs-standalone/src/api/routes/openapi.ts +390 -0
- package/templates/nextjs-standalone/src/api/routes/query.ts +318 -0
- package/templates/nextjs-standalone/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/nextjs-standalone/src/api/routes/slack.ts +611 -0
- package/templates/nextjs-standalone/src/api/server.ts +226 -0
- package/templates/nextjs-standalone/src/app/api/[...route]/route.ts +33 -0
- package/templates/nextjs-standalone/src/app/error.tsx +24 -0
- package/templates/nextjs-standalone/src/app/global-error.tsx +68 -0
- package/templates/nextjs-standalone/src/app/globals.css +126 -0
- package/templates/nextjs-standalone/src/app/layout.tsx +19 -0
- package/templates/nextjs-standalone/src/app/page.tsx +14 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/nextjs-standalone/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/nextjs-standalone/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/nextjs-standalone/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/nextjs-standalone/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/nextjs-standalone/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/nextjs-standalone/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/nextjs-standalone/src/lib/action-types.ts +95 -0
- package/templates/nextjs-standalone/src/lib/agent-query.ts +178 -0
- package/templates/nextjs-standalone/src/lib/agent.ts +505 -0
- package/templates/nextjs-standalone/src/lib/api-url.ts +3 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/nextjs-standalone/src/lib/auth/audit.ts +89 -0
- package/templates/nextjs-standalone/src/lib/auth/byot.ts +158 -0
- package/templates/nextjs-standalone/src/lib/auth/client.ts +23 -0
- package/templates/nextjs-standalone/src/lib/auth/detect.ts +83 -0
- package/templates/nextjs-standalone/src/lib/auth/managed.ts +73 -0
- package/templates/nextjs-standalone/src/lib/auth/middleware.ts +208 -0
- package/templates/nextjs-standalone/src/lib/auth/migrate.ts +111 -0
- package/templates/nextjs-standalone/src/lib/auth/permissions.ts +156 -0
- package/templates/nextjs-standalone/src/lib/auth/server.ts +142 -0
- package/templates/nextjs-standalone/src/lib/auth/simple-key.ts +92 -0
- package/templates/nextjs-standalone/src/lib/auth/types.ts +49 -0
- package/templates/nextjs-standalone/src/lib/config.ts +704 -0
- package/templates/nextjs-standalone/src/lib/conversation-types.ts +29 -0
- package/templates/nextjs-standalone/src/lib/conversations.ts +270 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/nextjs-standalone/src/lib/db/connection.ts +753 -0
- package/templates/nextjs-standalone/src/lib/db/duckdb.ts +122 -0
- package/templates/nextjs-standalone/src/lib/db/internal.ts +273 -0
- package/templates/nextjs-standalone/src/lib/db/salesforce.ts +342 -0
- package/templates/nextjs-standalone/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/nextjs-standalone/src/lib/errors.ts +154 -0
- package/templates/nextjs-standalone/src/lib/logger.ts +98 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/nextjs-standalone/src/lib/plugins/hooks.ts +162 -0
- package/templates/nextjs-standalone/src/lib/plugins/index.ts +9 -0
- package/templates/nextjs-standalone/src/lib/plugins/migrate.ts +309 -0
- package/templates/nextjs-standalone/src/lib/plugins/registry.ts +231 -0
- package/templates/nextjs-standalone/src/lib/plugins/tools.ts +39 -0
- package/templates/nextjs-standalone/src/lib/plugins/wiring.ts +291 -0
- package/templates/nextjs-standalone/src/lib/providers.ts +102 -0
- package/templates/nextjs-standalone/src/lib/rls.ts +321 -0
- package/templates/nextjs-standalone/src/lib/scheduled-task-types.ts +132 -0
- package/templates/nextjs-standalone/src/lib/scheduled-tasks.ts +475 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/delivery.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/engine.ts +317 -0
- package/templates/nextjs-standalone/src/lib/scheduler/executor.ts +73 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-email.ts +109 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/nextjs-standalone/src/lib/scheduler/index.ts +7 -0
- package/templates/nextjs-standalone/src/lib/security.ts +11 -0
- package/templates/nextjs-standalone/src/lib/semantic-index.ts +503 -0
- package/templates/nextjs-standalone/src/lib/semantic.ts +387 -0
- package/templates/nextjs-standalone/src/lib/sidecar-types.ts +16 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/nextjs-standalone/src/lib/slack/api.ts +102 -0
- package/templates/nextjs-standalone/src/lib/slack/format.ts +209 -0
- package/templates/nextjs-standalone/src/lib/slack/store.ts +107 -0
- package/templates/nextjs-standalone/src/lib/slack/threads.ts +64 -0
- package/templates/nextjs-standalone/src/lib/slack/verify.ts +71 -0
- package/templates/nextjs-standalone/src/lib/startup.ts +730 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/audit.ts +47 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/email.ts +191 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/handler.ts +591 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/index.ts +23 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/jira.ts +220 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/nextjs-standalone/src/lib/tools/explore.ts +379 -0
- package/templates/nextjs-standalone/src/lib/tools/registry.ts +221 -0
- package/templates/nextjs-standalone/src/lib/tools/salesforce.ts +138 -0
- package/templates/nextjs-standalone/src/lib/tools/soql-validation.ts +172 -0
- package/templates/nextjs-standalone/src/lib/tools/sql.ts +680 -0
- package/templates/nextjs-standalone/src/lib/tracing.ts +40 -0
- package/templates/nextjs-standalone/src/lib/utils.ts +6 -0
- package/templates/nextjs-standalone/src/test-setup.ts +38 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/nextjs-standalone/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/nextjs-standalone/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/nextjs-standalone/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/nextjs-standalone/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/nextjs-standalone/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/nextjs-standalone/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/nextjs-standalone/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/nextjs-standalone/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/nextjs-standalone/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/nextjs-standalone/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/nextjs-standalone/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/nextjs-standalone/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/nextjs-standalone/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/nextjs-standalone/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/nextjs-standalone/src/ui/context.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/nextjs-standalone/src/ui/lib/action-types.ts +63 -0
- package/templates/nextjs-standalone/src/ui/lib/helpers.ts +104 -0
- package/templates/nextjs-standalone/src/ui/lib/types.ts +145 -0
- package/templates/nextjs-standalone/tsconfig.json +32 -0
- package/templates/nextjs-standalone/vercel.json +4 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action framework handler.
|
|
3
|
+
*
|
|
4
|
+
* Core logic for the action approval workflow:
|
|
5
|
+
* - handleAction: persist request → check approval mode → auto-execute or pend
|
|
6
|
+
* - approveAction / denyAction: CAS via PostgreSQL WHERE status = 'pending' RETURNING *; in-memory path uses non-atomic check-then-update
|
|
7
|
+
* - getAction / listPendingActions: read-only queries
|
|
8
|
+
* - registerActionExecutor / getActionExecutor: deferred execution registry
|
|
9
|
+
* - getActionConfig: resolve per-action config from atlas.config.ts / defaults
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type {
|
|
13
|
+
ActionLogEntry,
|
|
14
|
+
ActionRequest,
|
|
15
|
+
ActionToolResult,
|
|
16
|
+
ActionApprovalMode,
|
|
17
|
+
ActionStatus,
|
|
18
|
+
} from "@atlas/api/lib/action-types";
|
|
19
|
+
import type { AtlasRole } from "@atlas/api/lib/auth/types";
|
|
20
|
+
import { hasInternalDB, internalQuery } from "@atlas/api/lib/db/internal";
|
|
21
|
+
import { createLogger, getRequestContext } from "@atlas/api/lib/logger";
|
|
22
|
+
import { getConfig, type ActionsConfig, type PerActionConfig } from "@atlas/api/lib/config";
|
|
23
|
+
import { parseRole } from "@atlas/api/lib/auth/permissions";
|
|
24
|
+
import { logActionAudit } from "./audit";
|
|
25
|
+
|
|
26
|
+
const log = createLogger("action-handler");
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// In-memory fallback store (when DATABASE_URL is not set)
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
const memoryStore = new Map<string, ActionLogEntry>();
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Executor registry (for deferred approval)
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
type ActionExecutor = (payload: Record<string, unknown>) => Promise<unknown>;
|
|
39
|
+
const executorRegistry = new Map<string, ActionExecutor>();
|
|
40
|
+
|
|
41
|
+
export function registerActionExecutor(actionId: string, fn: ActionExecutor): void {
|
|
42
|
+
executorRegistry.set(actionId, fn);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getActionExecutor(actionId: string): ActionExecutor | undefined {
|
|
46
|
+
return executorRegistry.get(actionId);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
// Config resolution
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolve the effective approval mode for an action type.
|
|
55
|
+
* Priority: per-action override > config defaults > action's defaultApproval > "manual".
|
|
56
|
+
*/
|
|
57
|
+
// Note: timeout and maxPerConversation are resolved here but not yet enforced — reserved for future implementation
|
|
58
|
+
export function getActionConfig(
|
|
59
|
+
actionType: string,
|
|
60
|
+
defaultApproval?: ActionApprovalMode,
|
|
61
|
+
): { approval: ActionApprovalMode; requiredRole?: AtlasRole; timeout?: number; maxPerConversation?: number } {
|
|
62
|
+
const config = getConfig();
|
|
63
|
+
const actionsConfig = config?.actions as ActionsConfig | undefined;
|
|
64
|
+
|
|
65
|
+
let approval: ActionApprovalMode = defaultApproval ?? "manual";
|
|
66
|
+
let requiredRole: AtlasRole | undefined;
|
|
67
|
+
let timeout: number | undefined;
|
|
68
|
+
let maxPerConversation: number | undefined;
|
|
69
|
+
|
|
70
|
+
// Layer 1: config defaults
|
|
71
|
+
if (actionsConfig?.defaults) {
|
|
72
|
+
if (actionsConfig.defaults.approval) approval = actionsConfig.defaults.approval;
|
|
73
|
+
if (actionsConfig.defaults.timeout) timeout = actionsConfig.defaults.timeout;
|
|
74
|
+
if (actionsConfig.defaults.maxPerConversation) maxPerConversation = actionsConfig.defaults.maxPerConversation;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Layer 2: per-action override
|
|
78
|
+
const perAction = actionsConfig?.[actionType] as PerActionConfig | undefined;
|
|
79
|
+
if (perAction) {
|
|
80
|
+
if (perAction.approval) approval = perAction.approval;
|
|
81
|
+
if (perAction.requiredRole) {
|
|
82
|
+
const validated = parseRole(perAction.requiredRole as string);
|
|
83
|
+
if (validated) {
|
|
84
|
+
requiredRole = validated;
|
|
85
|
+
} else {
|
|
86
|
+
log.warn({ actionType, value: perAction.requiredRole }, "Per-action requiredRole is not a valid Atlas role — ignoring override");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { approval, requiredRole, timeout, maxPerConversation };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Build action request helper
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
export function buildActionRequest(params: {
|
|
99
|
+
actionType: string;
|
|
100
|
+
target: string;
|
|
101
|
+
summary: string;
|
|
102
|
+
payload: Record<string, unknown>;
|
|
103
|
+
reversible: boolean;
|
|
104
|
+
}): ActionRequest {
|
|
105
|
+
return {
|
|
106
|
+
id: crypto.randomUUID(),
|
|
107
|
+
...params,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// Persist helpers
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
async function persistAction(entry: ActionLogEntry): Promise<void> {
|
|
116
|
+
memoryStore.set(entry.id, entry);
|
|
117
|
+
if (hasInternalDB()) {
|
|
118
|
+
try {
|
|
119
|
+
await internalQuery(
|
|
120
|
+
`INSERT INTO action_log (id, requested_by, approved_by, auth_mode, action_type, target, summary, payload, status, result, error, rollback_info, conversation_id, request_id)
|
|
121
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)`,
|
|
122
|
+
[
|
|
123
|
+
entry.id,
|
|
124
|
+
entry.requested_by,
|
|
125
|
+
entry.approved_by,
|
|
126
|
+
entry.auth_mode,
|
|
127
|
+
entry.action_type,
|
|
128
|
+
entry.target,
|
|
129
|
+
entry.summary,
|
|
130
|
+
JSON.stringify(entry.payload),
|
|
131
|
+
entry.status,
|
|
132
|
+
entry.result ? JSON.stringify(entry.result) : null,
|
|
133
|
+
entry.error,
|
|
134
|
+
entry.rollback_info ? JSON.stringify(entry.rollback_info) : null,
|
|
135
|
+
entry.conversation_id,
|
|
136
|
+
entry.request_id,
|
|
137
|
+
],
|
|
138
|
+
);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
log.error({ err, actionId: entry.id }, "Failed to persist action to DB — stored in memory only");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const COLUMN_MAP: Record<string, string> = {
|
|
146
|
+
status: "status",
|
|
147
|
+
resolved_at: "resolved_at",
|
|
148
|
+
executed_at: "executed_at",
|
|
149
|
+
approved_by: "approved_by",
|
|
150
|
+
result: "result",
|
|
151
|
+
error: "error",
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
async function updateActionStatus(
|
|
155
|
+
id: string,
|
|
156
|
+
updates: Partial<Pick<ActionLogEntry, "status" | "resolved_at" | "executed_at" | "approved_by" | "result" | "error">>,
|
|
157
|
+
): Promise<void> {
|
|
158
|
+
// Update memory store first
|
|
159
|
+
const existing = memoryStore.get(id);
|
|
160
|
+
if (existing) {
|
|
161
|
+
memoryStore.set(id, { ...existing, ...updates });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const setClauses: string[] = [];
|
|
165
|
+
const params: unknown[] = [];
|
|
166
|
+
let paramIdx = 1;
|
|
167
|
+
|
|
168
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
169
|
+
if (value !== undefined) {
|
|
170
|
+
const colName = COLUMN_MAP[key];
|
|
171
|
+
if (!colName) throw new Error(`Unknown action_log column key: ${key}`);
|
|
172
|
+
setClauses.push(`${colName} = $${paramIdx}`);
|
|
173
|
+
params.push(colName === "result" ? JSON.stringify(value) : value);
|
|
174
|
+
paramIdx++;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (hasInternalDB() && setClauses.length > 0) {
|
|
179
|
+
params.push(id);
|
|
180
|
+
try {
|
|
181
|
+
await internalQuery(
|
|
182
|
+
`UPDATE action_log SET ${setClauses.join(", ")} WHERE id = $${paramIdx}`,
|
|
183
|
+
params,
|
|
184
|
+
);
|
|
185
|
+
} catch (err) {
|
|
186
|
+
log.error({ err, actionId: id }, "Failed to update action status in DB — memory store updated");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Core handler
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
export interface HandleActionOptions {
|
|
196
|
+
conversationId?: string;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Main entry point: persist pending action → check approval mode → if auto: execute immediately.
|
|
201
|
+
*/
|
|
202
|
+
export async function handleAction(
|
|
203
|
+
request: ActionRequest,
|
|
204
|
+
executeFn: (payload: Record<string, unknown>) => Promise<unknown>,
|
|
205
|
+
opts?: HandleActionOptions,
|
|
206
|
+
): Promise<ActionToolResult> {
|
|
207
|
+
const ctx = getRequestContext();
|
|
208
|
+
const userId = ctx?.user?.id;
|
|
209
|
+
const authMode = ctx?.user?.mode ?? "none";
|
|
210
|
+
const requestId = ctx?.requestId ?? null;
|
|
211
|
+
const now = new Date().toISOString();
|
|
212
|
+
|
|
213
|
+
const entry: ActionLogEntry = {
|
|
214
|
+
id: request.id,
|
|
215
|
+
requested_at: now,
|
|
216
|
+
resolved_at: null,
|
|
217
|
+
executed_at: null,
|
|
218
|
+
requested_by: userId ?? null,
|
|
219
|
+
approved_by: null,
|
|
220
|
+
auth_mode: authMode,
|
|
221
|
+
action_type: request.actionType,
|
|
222
|
+
target: request.target,
|
|
223
|
+
summary: request.summary,
|
|
224
|
+
payload: request.payload,
|
|
225
|
+
status: "pending",
|
|
226
|
+
result: null,
|
|
227
|
+
error: null,
|
|
228
|
+
rollback_info: null,
|
|
229
|
+
conversation_id: opts?.conversationId ?? null,
|
|
230
|
+
request_id: requestId,
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
await persistAction(entry);
|
|
234
|
+
logActionAudit({
|
|
235
|
+
actionId: request.id,
|
|
236
|
+
actionType: request.actionType,
|
|
237
|
+
status: "pending",
|
|
238
|
+
userId,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// Register executor for deferred approval (keyed by actionId so each request gets its own executor)
|
|
242
|
+
registerActionExecutor(request.id, executeFn);
|
|
243
|
+
|
|
244
|
+
// Resolve approval mode
|
|
245
|
+
const actionConfig = getActionConfig(request.actionType);
|
|
246
|
+
|
|
247
|
+
if (actionConfig.approval === "auto") {
|
|
248
|
+
// Execute immediately
|
|
249
|
+
const startMs = Date.now();
|
|
250
|
+
try {
|
|
251
|
+
const result = await executeFn(request.payload);
|
|
252
|
+
const latencyMs = Date.now() - startMs;
|
|
253
|
+
|
|
254
|
+
await updateActionStatus(request.id, {
|
|
255
|
+
status: "auto_approved",
|
|
256
|
+
resolved_at: new Date().toISOString(),
|
|
257
|
+
executed_at: new Date().toISOString(),
|
|
258
|
+
approved_by: "system:auto",
|
|
259
|
+
result,
|
|
260
|
+
});
|
|
261
|
+
logActionAudit({
|
|
262
|
+
actionId: request.id,
|
|
263
|
+
actionType: request.actionType,
|
|
264
|
+
status: "auto_approved",
|
|
265
|
+
latencyMs,
|
|
266
|
+
userId,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return { status: "auto_approved", actionId: request.id, result };
|
|
270
|
+
} catch (err) {
|
|
271
|
+
const latencyMs = Date.now() - startMs;
|
|
272
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
273
|
+
|
|
274
|
+
await updateActionStatus(request.id, {
|
|
275
|
+
status: "failed",
|
|
276
|
+
resolved_at: new Date().toISOString(),
|
|
277
|
+
error: errorMsg,
|
|
278
|
+
});
|
|
279
|
+
logActionAudit({
|
|
280
|
+
actionId: request.id,
|
|
281
|
+
actionType: request.actionType,
|
|
282
|
+
status: "failed",
|
|
283
|
+
latencyMs,
|
|
284
|
+
userId,
|
|
285
|
+
error: errorMsg,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
return { status: "error", actionId: request.id, error: errorMsg };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Manual or admin-only: pend for approval
|
|
293
|
+
return { status: "pending_approval", actionId: request.id, summary: request.summary };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
// Approval / denial (CAS via PostgreSQL WHERE status = 'pending' RETURNING *; in-memory path uses non-atomic check-then-update)
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Approve a pending action. Returns the updated entry, or null if CAS failed
|
|
302
|
+
* (action already resolved — 409 scenario).
|
|
303
|
+
*/
|
|
304
|
+
export async function approveAction(
|
|
305
|
+
actionId: string,
|
|
306
|
+
approverId: string,
|
|
307
|
+
executeFn?: ActionExecutor,
|
|
308
|
+
): Promise<ActionLogEntry | null> {
|
|
309
|
+
const resolveFn = executeFn ?? getActionExecutor(actionId);
|
|
310
|
+
|
|
311
|
+
// CAS in DB (atomic via WHERE status = 'pending' RETURNING *)
|
|
312
|
+
if (hasInternalDB()) {
|
|
313
|
+
const rows = await internalQuery(
|
|
314
|
+
`UPDATE action_log
|
|
315
|
+
SET status = 'approved', resolved_at = now(), approved_by = $1
|
|
316
|
+
WHERE id = $2 AND status = 'pending'
|
|
317
|
+
RETURNING *`,
|
|
318
|
+
[approverId, actionId],
|
|
319
|
+
) as unknown as ActionLogEntry[];
|
|
320
|
+
if (rows.length === 0) return null;
|
|
321
|
+
|
|
322
|
+
const entry = rows[0];
|
|
323
|
+
memoryStore.set(actionId, entry);
|
|
324
|
+
|
|
325
|
+
logActionAudit({
|
|
326
|
+
actionId,
|
|
327
|
+
actionType: entry.action_type,
|
|
328
|
+
status: "approved",
|
|
329
|
+
approverId,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Execute the action
|
|
333
|
+
if (resolveFn) {
|
|
334
|
+
const startMs = Date.now();
|
|
335
|
+
try {
|
|
336
|
+
const result = await resolveFn(entry.payload);
|
|
337
|
+
const latencyMs = Date.now() - startMs;
|
|
338
|
+
|
|
339
|
+
const execRows = await internalQuery(
|
|
340
|
+
`UPDATE action_log SET status = 'executed', executed_at = now(), result = $1 WHERE id = $2 RETURNING *`,
|
|
341
|
+
[JSON.stringify(result), actionId],
|
|
342
|
+
) as unknown as ActionLogEntry[];
|
|
343
|
+
const updated = execRows[0] ?? { ...entry, status: "executed" as ActionStatus, executed_at: new Date().toISOString(), result };
|
|
344
|
+
memoryStore.set(actionId, updated);
|
|
345
|
+
|
|
346
|
+
logActionAudit({
|
|
347
|
+
actionId,
|
|
348
|
+
actionType: entry.action_type,
|
|
349
|
+
status: "executed",
|
|
350
|
+
latencyMs,
|
|
351
|
+
approverId,
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
return updated;
|
|
355
|
+
} catch (err) {
|
|
356
|
+
const latencyMs = Date.now() - startMs;
|
|
357
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
358
|
+
|
|
359
|
+
const failRows = await internalQuery(
|
|
360
|
+
`UPDATE action_log SET status = 'failed', error = $1 WHERE id = $2 RETURNING *`,
|
|
361
|
+
[errorMsg, actionId],
|
|
362
|
+
) as unknown as ActionLogEntry[];
|
|
363
|
+
const failed = failRows[0] ?? { ...entry, status: "failed" as ActionStatus, error: errorMsg };
|
|
364
|
+
memoryStore.set(actionId, failed);
|
|
365
|
+
|
|
366
|
+
logActionAudit({
|
|
367
|
+
actionId,
|
|
368
|
+
actionType: entry.action_type,
|
|
369
|
+
status: "failed",
|
|
370
|
+
latencyMs,
|
|
371
|
+
approverId,
|
|
372
|
+
error: errorMsg,
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
return failed;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
log.warn({ actionId, actionType: entry.action_type }, "Action approved but no executor available — will not execute");
|
|
380
|
+
return entry;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Memory-only fallback
|
|
384
|
+
const entry = memoryStore.get(actionId);
|
|
385
|
+
if (!entry || entry.status !== "pending") return null;
|
|
386
|
+
|
|
387
|
+
const approved: ActionLogEntry = {
|
|
388
|
+
...entry,
|
|
389
|
+
status: "approved",
|
|
390
|
+
resolved_at: new Date().toISOString(),
|
|
391
|
+
approved_by: approverId,
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
logActionAudit({
|
|
395
|
+
actionId,
|
|
396
|
+
actionType: entry.action_type,
|
|
397
|
+
status: "approved",
|
|
398
|
+
approverId,
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
if (resolveFn) {
|
|
402
|
+
const startMs = Date.now();
|
|
403
|
+
try {
|
|
404
|
+
const result = await resolveFn(entry.payload);
|
|
405
|
+
const latencyMs = Date.now() - startMs;
|
|
406
|
+
const executed: ActionLogEntry = {
|
|
407
|
+
...approved,
|
|
408
|
+
status: "executed",
|
|
409
|
+
executed_at: new Date().toISOString(),
|
|
410
|
+
result,
|
|
411
|
+
};
|
|
412
|
+
memoryStore.set(actionId, executed);
|
|
413
|
+
|
|
414
|
+
logActionAudit({
|
|
415
|
+
actionId,
|
|
416
|
+
actionType: entry.action_type,
|
|
417
|
+
status: "executed",
|
|
418
|
+
latencyMs,
|
|
419
|
+
approverId,
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return executed;
|
|
423
|
+
} catch (err) {
|
|
424
|
+
const latencyMs = Date.now() - startMs;
|
|
425
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
426
|
+
const failed: ActionLogEntry = {
|
|
427
|
+
...approved,
|
|
428
|
+
status: "failed",
|
|
429
|
+
error: errorMsg,
|
|
430
|
+
};
|
|
431
|
+
memoryStore.set(actionId, failed);
|
|
432
|
+
|
|
433
|
+
logActionAudit({
|
|
434
|
+
actionId,
|
|
435
|
+
actionType: entry.action_type,
|
|
436
|
+
status: "failed",
|
|
437
|
+
latencyMs,
|
|
438
|
+
approverId,
|
|
439
|
+
error: errorMsg,
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
return failed;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
log.warn({ actionId, actionType: entry.action_type }, "Action approved but no executor available — will not execute");
|
|
447
|
+
memoryStore.set(actionId, approved);
|
|
448
|
+
return approved;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Deny a pending action. Returns the updated entry, or null if CAS failed.
|
|
453
|
+
*/
|
|
454
|
+
export async function denyAction(
|
|
455
|
+
actionId: string,
|
|
456
|
+
denierId: string,
|
|
457
|
+
reason?: string,
|
|
458
|
+
): Promise<ActionLogEntry | null> {
|
|
459
|
+
if (hasInternalDB()) {
|
|
460
|
+
const rows = await internalQuery(
|
|
461
|
+
`UPDATE action_log
|
|
462
|
+
-- approved_by is overloaded: stores approver for approved actions, denier for denied actions
|
|
463
|
+
SET status = 'denied', resolved_at = now(), approved_by = $1, error = $2
|
|
464
|
+
WHERE id = $3 AND status = 'pending'
|
|
465
|
+
RETURNING *`,
|
|
466
|
+
[denierId, reason ?? null, actionId],
|
|
467
|
+
) as unknown as ActionLogEntry[];
|
|
468
|
+
if (rows.length === 0) return null;
|
|
469
|
+
|
|
470
|
+
const entry = rows[0];
|
|
471
|
+
memoryStore.set(actionId, entry);
|
|
472
|
+
|
|
473
|
+
logActionAudit({
|
|
474
|
+
actionId,
|
|
475
|
+
actionType: entry.action_type,
|
|
476
|
+
status: "denied",
|
|
477
|
+
approverId: denierId,
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
return entry;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Memory-only fallback
|
|
484
|
+
const entry = memoryStore.get(actionId);
|
|
485
|
+
if (!entry || entry.status !== "pending") return null;
|
|
486
|
+
|
|
487
|
+
const denied: ActionLogEntry = {
|
|
488
|
+
...entry,
|
|
489
|
+
status: "denied",
|
|
490
|
+
resolved_at: new Date().toISOString(),
|
|
491
|
+
approved_by: denierId, // approved_by is overloaded: stores approver for approved actions, denier for denied actions
|
|
492
|
+
error: reason ?? null,
|
|
493
|
+
};
|
|
494
|
+
memoryStore.set(actionId, denied);
|
|
495
|
+
|
|
496
|
+
logActionAudit({
|
|
497
|
+
actionId,
|
|
498
|
+
actionType: entry.action_type,
|
|
499
|
+
status: "denied",
|
|
500
|
+
approverId: denierId,
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return denied;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// ---------------------------------------------------------------------------
|
|
507
|
+
// Read operations
|
|
508
|
+
// ---------------------------------------------------------------------------
|
|
509
|
+
|
|
510
|
+
export async function getAction(actionId: string): Promise<ActionLogEntry | null> {
|
|
511
|
+
if (hasInternalDB()) {
|
|
512
|
+
const rows = await internalQuery(
|
|
513
|
+
`SELECT * FROM action_log WHERE id = $1`,
|
|
514
|
+
[actionId],
|
|
515
|
+
) as unknown as ActionLogEntry[];
|
|
516
|
+
return rows[0] ?? null;
|
|
517
|
+
}
|
|
518
|
+
return memoryStore.get(actionId) ?? null;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export interface ListActionsOptions {
|
|
522
|
+
status?: ActionStatus;
|
|
523
|
+
userId?: string;
|
|
524
|
+
conversationId?: string;
|
|
525
|
+
limit?: number;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Despite the name, supports filtering by any ActionStatus via opts.status.
|
|
530
|
+
* Defaults to "pending" when no status filter is provided.
|
|
531
|
+
*/
|
|
532
|
+
export async function listPendingActions(opts?: ListActionsOptions): Promise<ActionLogEntry[]> {
|
|
533
|
+
const limit = Math.min(opts?.limit ?? 50, 100);
|
|
534
|
+
|
|
535
|
+
if (hasInternalDB()) {
|
|
536
|
+
const conditions: string[] = [];
|
|
537
|
+
const params: unknown[] = [];
|
|
538
|
+
let paramIdx = 1;
|
|
539
|
+
|
|
540
|
+
if (opts?.status) {
|
|
541
|
+
conditions.push(`status = $${paramIdx++}`);
|
|
542
|
+
params.push(opts.status);
|
|
543
|
+
} else {
|
|
544
|
+
conditions.push(`status = $${paramIdx++}`);
|
|
545
|
+
params.push("pending");
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (opts?.userId) {
|
|
549
|
+
conditions.push(`requested_by = $${paramIdx++}`);
|
|
550
|
+
params.push(opts.userId);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (opts?.conversationId) {
|
|
554
|
+
conditions.push(`conversation_id = $${paramIdx++}`);
|
|
555
|
+
params.push(opts.conversationId);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
params.push(limit);
|
|
559
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
560
|
+
const rows = await internalQuery(
|
|
561
|
+
`SELECT * FROM action_log ${where} ORDER BY requested_at DESC LIMIT $${paramIdx}`,
|
|
562
|
+
params,
|
|
563
|
+
) as unknown as ActionLogEntry[];
|
|
564
|
+
return rows;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// Memory-only fallback
|
|
568
|
+
const targetStatus = opts?.status ?? "pending";
|
|
569
|
+
let results = Array.from(memoryStore.values())
|
|
570
|
+
.filter((e) => e.status === targetStatus);
|
|
571
|
+
|
|
572
|
+
if (opts?.userId) {
|
|
573
|
+
results = results.filter((e) => e.requested_by === opts.userId);
|
|
574
|
+
}
|
|
575
|
+
if (opts?.conversationId) {
|
|
576
|
+
results = results.filter((e) => e.conversation_id === opts.conversationId);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
return results
|
|
580
|
+
.sort((a, b) => b.requested_at.localeCompare(a.requested_at))
|
|
581
|
+
.slice(0, limit);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// ---------------------------------------------------------------------------
|
|
585
|
+
// Test helpers
|
|
586
|
+
// ---------------------------------------------------------------------------
|
|
587
|
+
|
|
588
|
+
export function _resetActionStore(): void {
|
|
589
|
+
memoryStore.clear();
|
|
590
|
+
executorRegistry.clear();
|
|
591
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action framework — barrel exports.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
handleAction,
|
|
7
|
+
approveAction,
|
|
8
|
+
denyAction,
|
|
9
|
+
getAction,
|
|
10
|
+
listPendingActions,
|
|
11
|
+
buildActionRequest,
|
|
12
|
+
getActionConfig,
|
|
13
|
+
registerActionExecutor,
|
|
14
|
+
getActionExecutor,
|
|
15
|
+
_resetActionStore,
|
|
16
|
+
type HandleActionOptions,
|
|
17
|
+
type ListActionsOptions,
|
|
18
|
+
} from "./handler";
|
|
19
|
+
|
|
20
|
+
export { logActionAudit, type ActionAuditEntry } from "./audit";
|
|
21
|
+
|
|
22
|
+
export { createJiraTicket } from "./jira";
|
|
23
|
+
export { sendEmailReport } from "./email";
|