@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,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Better Auth catch-all route.
|
|
3
|
+
*
|
|
4
|
+
* Uses Better Auth's fetch-native handler (Request/Response, no framework adapter).
|
|
5
|
+
* Dynamic imports ensure better-auth is never loaded when not in managed mode.
|
|
6
|
+
* Returns 404 for all auth routes when managed mode is not active.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Hono } from "hono";
|
|
10
|
+
import { detectAuthMode } from "@atlas/api/lib/auth/detect";
|
|
11
|
+
import { createLogger } from "@atlas/api/lib/logger";
|
|
12
|
+
|
|
13
|
+
const log = createLogger("auth-route");
|
|
14
|
+
|
|
15
|
+
const auth = new Hono();
|
|
16
|
+
|
|
17
|
+
auth.all("/*", async (c) => {
|
|
18
|
+
if (detectAuthMode() !== "managed") {
|
|
19
|
+
return c.json(
|
|
20
|
+
{ error: "not_found", message: "Auth routes are not enabled" },
|
|
21
|
+
404,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const { getAuthInstance } = await import("@atlas/api/lib/auth/server");
|
|
27
|
+
const authInstance = getAuthInstance();
|
|
28
|
+
const response = await authInstance.handler(c.req.raw);
|
|
29
|
+
return response;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
log.error(
|
|
32
|
+
{
|
|
33
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
34
|
+
url: c.req.url,
|
|
35
|
+
},
|
|
36
|
+
"Auth route handler failed",
|
|
37
|
+
);
|
|
38
|
+
return c.json(
|
|
39
|
+
{
|
|
40
|
+
error: "auth_service_error",
|
|
41
|
+
message: "Authentication service unavailable",
|
|
42
|
+
},
|
|
43
|
+
503,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export { auth };
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat route — accepts a conversation and streams agent responses.
|
|
3
|
+
*
|
|
4
|
+
* Middleware stack:
|
|
5
|
+
* auth → rate limit → withRequestContext → validateEnvironment → conversation persistence → runAgent → stream.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { type UIMessage } from "ai";
|
|
11
|
+
import { APICallError, LoadAPIKeyError, NoSuchModelError } from "ai";
|
|
12
|
+
import { runAgent } from "@atlas/api/lib/agent";
|
|
13
|
+
import { validateEnvironment } from "@atlas/api/lib/startup";
|
|
14
|
+
import { GatewayModelNotFoundError } from "@ai-sdk/gateway";
|
|
15
|
+
import { createLogger, withRequestContext } from "@atlas/api/lib/logger";
|
|
16
|
+
import type { AuthResult } from "@atlas/api/lib/auth/types";
|
|
17
|
+
import {
|
|
18
|
+
authenticateRequest,
|
|
19
|
+
checkRateLimit,
|
|
20
|
+
getClientIP,
|
|
21
|
+
} from "@atlas/api/lib/auth/middleware";
|
|
22
|
+
import { hasInternalDB } from "@atlas/api/lib/db/internal";
|
|
23
|
+
import {
|
|
24
|
+
createConversation,
|
|
25
|
+
addMessage,
|
|
26
|
+
getConversation,
|
|
27
|
+
generateTitle,
|
|
28
|
+
} from "@atlas/api/lib/conversations";
|
|
29
|
+
|
|
30
|
+
const log = createLogger("chat");
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Zod schemas — exported for OpenAPI spec generation
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
const MessagePartSchema = z.object({ type: z.string() }).passthrough();
|
|
37
|
+
|
|
38
|
+
const UIMessageSchema = z.object({
|
|
39
|
+
role: z.enum(["user", "assistant", "system"]),
|
|
40
|
+
parts: z.array(MessagePartSchema),
|
|
41
|
+
id: z.string(),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const ChatRequestSchema = z.object({
|
|
45
|
+
messages: z.array(UIMessageSchema).min(1),
|
|
46
|
+
conversationId: z.string().uuid().optional(),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const chat = new Hono();
|
|
50
|
+
|
|
51
|
+
chat.post("/", async (c) => {
|
|
52
|
+
const req = c.req.raw;
|
|
53
|
+
const requestId = crypto.randomUUID();
|
|
54
|
+
|
|
55
|
+
// Auth check — before context so user identity is available to all downstream logs
|
|
56
|
+
let authResult: AuthResult;
|
|
57
|
+
try {
|
|
58
|
+
authResult = await authenticateRequest(req);
|
|
59
|
+
} catch (err) {
|
|
60
|
+
log.error(
|
|
61
|
+
{
|
|
62
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
63
|
+
requestId,
|
|
64
|
+
},
|
|
65
|
+
"Auth dispatch failed",
|
|
66
|
+
);
|
|
67
|
+
return c.json(
|
|
68
|
+
{ error: "auth_error", message: "Authentication system error" },
|
|
69
|
+
500,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (!authResult.authenticated) {
|
|
73
|
+
log.warn({ requestId, status: authResult.status }, "Authentication failed");
|
|
74
|
+
return c.json(
|
|
75
|
+
{ error: "auth_error", message: authResult.error },
|
|
76
|
+
authResult.status as 401 | 403 | 500,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Rate limit check — after auth so we have user identity
|
|
81
|
+
const ip = getClientIP(req);
|
|
82
|
+
const rateLimitKey = authResult.user?.id ?? (ip ? `ip:${ip}` : "anon");
|
|
83
|
+
const rateCheck = checkRateLimit(rateLimitKey);
|
|
84
|
+
if (!rateCheck.allowed) {
|
|
85
|
+
log.warn(
|
|
86
|
+
{ requestId, rateLimitKey, retryAfterMs: rateCheck.retryAfterMs },
|
|
87
|
+
"Rate limit exceeded",
|
|
88
|
+
);
|
|
89
|
+
const retryAfterSeconds = Math.ceil(
|
|
90
|
+
(rateCheck.retryAfterMs ?? 60000) / 1000,
|
|
91
|
+
);
|
|
92
|
+
return c.json(
|
|
93
|
+
{
|
|
94
|
+
error: "rate_limited",
|
|
95
|
+
message: "Too many requests. Please wait before trying again.",
|
|
96
|
+
retryAfterSeconds,
|
|
97
|
+
},
|
|
98
|
+
{ status: 429, headers: { "Retry-After": String(retryAfterSeconds) } },
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// withRequestContext binds requestId + user to AsyncLocalStorage for the
|
|
103
|
+
// entire async call chain (including logQueryAudit deep inside executeSQL).
|
|
104
|
+
return withRequestContext(
|
|
105
|
+
{ requestId, user: authResult.user },
|
|
106
|
+
async () => {
|
|
107
|
+
// Startup diagnostics — fast-fail with actionable errors
|
|
108
|
+
const diagnostics = await validateEnvironment();
|
|
109
|
+
if (diagnostics.length > 0) {
|
|
110
|
+
return c.json(
|
|
111
|
+
{
|
|
112
|
+
error: "configuration_error",
|
|
113
|
+
message: diagnostics.map((d) => d.message).join("\n\n"),
|
|
114
|
+
diagnostics,
|
|
115
|
+
},
|
|
116
|
+
400,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Datasource guard — diagnostics pass (it's a warning) but chat requires a datasource
|
|
121
|
+
if (!process.env.ATLAS_DATASOURCE_URL) {
|
|
122
|
+
return c.json(
|
|
123
|
+
{
|
|
124
|
+
error: "no_datasource",
|
|
125
|
+
message:
|
|
126
|
+
"No analytics datasource configured. Set ATLAS_DATASOURCE_URL to query your data.",
|
|
127
|
+
},
|
|
128
|
+
400,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Parse request body separately so malformed JSON gets a 400, not 500
|
|
133
|
+
let body: unknown;
|
|
134
|
+
try {
|
|
135
|
+
body = await c.req.json();
|
|
136
|
+
} catch (err) {
|
|
137
|
+
log.warn(
|
|
138
|
+
{ err: err instanceof Error ? err : new Error(String(err)) },
|
|
139
|
+
"Failed to parse request body",
|
|
140
|
+
);
|
|
141
|
+
return c.json(
|
|
142
|
+
{
|
|
143
|
+
error: "invalid_request",
|
|
144
|
+
message: "Invalid JSON body.",
|
|
145
|
+
},
|
|
146
|
+
400,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const parsed = ChatRequestSchema.safeParse(body);
|
|
151
|
+
if (!parsed.success) {
|
|
152
|
+
return c.json(
|
|
153
|
+
{
|
|
154
|
+
error: "validation_error",
|
|
155
|
+
message: "Invalid request body.",
|
|
156
|
+
details: parsed.error.issues,
|
|
157
|
+
},
|
|
158
|
+
422,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const messages = parsed.data.messages as UIMessage[];
|
|
163
|
+
let conversationId = parsed.data.conversationId;
|
|
164
|
+
|
|
165
|
+
// Conversation persistence — Ownership verification blocks here (can 404); message writes are fire-and-forget via internalExecute.
|
|
166
|
+
if (hasInternalDB()) {
|
|
167
|
+
if (conversationId) {
|
|
168
|
+
// Ownership verification — NOT best-effort, this is a security check
|
|
169
|
+
const existing = await getConversation(conversationId, authResult.user?.id);
|
|
170
|
+
if (!existing.ok) {
|
|
171
|
+
return c.json({ error: "not_found", message: "Conversation not found." }, 404);
|
|
172
|
+
}
|
|
173
|
+
// Persist the latest user message
|
|
174
|
+
try {
|
|
175
|
+
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
|
|
176
|
+
if (lastUserMsg) {
|
|
177
|
+
addMessage({ conversationId, role: "user", content: lastUserMsg.parts });
|
|
178
|
+
}
|
|
179
|
+
} catch (err) {
|
|
180
|
+
log.warn({ err: err instanceof Error ? err.message : String(err) }, "Failed to persist user message");
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
try {
|
|
184
|
+
// Create new conversation — best-effort
|
|
185
|
+
const firstUserMsg = messages.find((m) => m.role === "user");
|
|
186
|
+
const title = firstUserMsg
|
|
187
|
+
? generateTitle(
|
|
188
|
+
firstUserMsg.parts
|
|
189
|
+
?.filter((p): p is { type: "text"; text: string } => p.type === "text")
|
|
190
|
+
.map((p) => p.text)
|
|
191
|
+
.join(" ") ?? "",
|
|
192
|
+
)
|
|
193
|
+
: "New conversation";
|
|
194
|
+
const created = await createConversation({
|
|
195
|
+
userId: authResult.user?.id,
|
|
196
|
+
title,
|
|
197
|
+
surface: "web",
|
|
198
|
+
});
|
|
199
|
+
if (created) conversationId = created.id;
|
|
200
|
+
} catch (err) {
|
|
201
|
+
log.warn({ err: err instanceof Error ? err.message : String(err) }, "Failed to create conversation");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
// Build a dynamic registry when Salesforce sources or actions are present
|
|
208
|
+
let toolRegistry;
|
|
209
|
+
const includeActions = process.env.ATLAS_ACTIONS_ENABLED === "true";
|
|
210
|
+
let includeSalesforce = false;
|
|
211
|
+
try {
|
|
212
|
+
const { listSalesforceSources } = await import("@atlas/api/lib/db/salesforce");
|
|
213
|
+
includeSalesforce = listSalesforceSources().length > 0;
|
|
214
|
+
} catch (err) {
|
|
215
|
+
const isModuleNotFound =
|
|
216
|
+
err instanceof Error &&
|
|
217
|
+
(err.message.includes("Cannot find module") ||
|
|
218
|
+
err.message.includes("MODULE_NOT_FOUND"));
|
|
219
|
+
if (!isModuleNotFound) {
|
|
220
|
+
log.error(
|
|
221
|
+
{ err: err instanceof Error ? err : new Error(String(err)) },
|
|
222
|
+
"Failed to initialize Salesforce tool registry — falling back to default tools",
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (includeSalesforce || includeActions) {
|
|
227
|
+
try {
|
|
228
|
+
const { buildRegistry } = await import("@atlas/api/lib/tools/registry");
|
|
229
|
+
toolRegistry = await buildRegistry({ includeSalesforce, includeActions });
|
|
230
|
+
} catch (err) {
|
|
231
|
+
log.error(
|
|
232
|
+
{ err: err instanceof Error ? err : new Error(String(err)) },
|
|
233
|
+
"Failed to build tool registry — falling back to default tools",
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Merge plugin tools (if any) on top of the current registry
|
|
239
|
+
const { getPluginTools } = await import("@atlas/api/lib/plugins/tools");
|
|
240
|
+
const pluginTools = getPluginTools();
|
|
241
|
+
if (pluginTools) {
|
|
242
|
+
const { ToolRegistry, defaultRegistry } = await import("@atlas/api/lib/tools/registry");
|
|
243
|
+
const base = toolRegistry ?? defaultRegistry;
|
|
244
|
+
toolRegistry = ToolRegistry.merge(base, pluginTools);
|
|
245
|
+
toolRegistry.freeze();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const result = await runAgent({ messages, ...(toolRegistry && { tools: toolRegistry }) });
|
|
249
|
+
const streamResponse = result.toUIMessageStreamResponse();
|
|
250
|
+
|
|
251
|
+
// Set conversation ID header so the client can track continuity
|
|
252
|
+
if (conversationId) {
|
|
253
|
+
streamResponse.headers.set("x-conversation-id", conversationId);
|
|
254
|
+
|
|
255
|
+
// Fire-and-forget: persist assistant response after stream completes
|
|
256
|
+
const cid = conversationId;
|
|
257
|
+
void Promise.resolve(result.text)
|
|
258
|
+
.then((text) => {
|
|
259
|
+
addMessage({ conversationId: cid, role: "assistant", content: [{ type: "text", text }] });
|
|
260
|
+
})
|
|
261
|
+
.catch((err: unknown) => {
|
|
262
|
+
log.warn({ err: err instanceof Error ? err.message : String(err) }, "Failed to persist assistant message");
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return streamResponse;
|
|
267
|
+
} catch (err) {
|
|
268
|
+
const message = err instanceof Error ? err.message : "";
|
|
269
|
+
|
|
270
|
+
// --- Structured AI SDK error types (checked first) ---
|
|
271
|
+
|
|
272
|
+
if (GatewayModelNotFoundError.isInstance(err)) {
|
|
273
|
+
log.error(
|
|
274
|
+
{
|
|
275
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
276
|
+
category: "provider_model_not_found",
|
|
277
|
+
},
|
|
278
|
+
"Gateway model not found",
|
|
279
|
+
);
|
|
280
|
+
return c.json(
|
|
281
|
+
{
|
|
282
|
+
error: "provider_model_not_found",
|
|
283
|
+
message:
|
|
284
|
+
"Model not found on the AI Gateway. Check that your ATLAS_MODEL uses the correct provider/model format (e.g., anthropic/claude-sonnet-4.6).",
|
|
285
|
+
},
|
|
286
|
+
400,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (NoSuchModelError.isInstance(err)) {
|
|
291
|
+
log.error(
|
|
292
|
+
{
|
|
293
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
294
|
+
category: "provider_model_not_found",
|
|
295
|
+
},
|
|
296
|
+
"Model not found",
|
|
297
|
+
);
|
|
298
|
+
return c.json(
|
|
299
|
+
{
|
|
300
|
+
error: "provider_model_not_found",
|
|
301
|
+
message:
|
|
302
|
+
"The configured model was not found. Check ATLAS_MODEL and ATLAS_PROVIDER settings.",
|
|
303
|
+
},
|
|
304
|
+
400,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (LoadAPIKeyError.isInstance(err)) {
|
|
309
|
+
log.error(
|
|
310
|
+
{
|
|
311
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
312
|
+
category: "provider_auth_error",
|
|
313
|
+
},
|
|
314
|
+
"API key not loaded",
|
|
315
|
+
);
|
|
316
|
+
return c.json(
|
|
317
|
+
{
|
|
318
|
+
error: "provider_auth_error",
|
|
319
|
+
message:
|
|
320
|
+
"LLM provider API key could not be loaded. Check that the required API key environment variable is set.",
|
|
321
|
+
},
|
|
322
|
+
503,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// APICallError carries the HTTP status code from the provider response
|
|
327
|
+
if (APICallError.isInstance(err)) {
|
|
328
|
+
const status = err.statusCode;
|
|
329
|
+
|
|
330
|
+
if (status === 401 || status === 403) {
|
|
331
|
+
log.error(
|
|
332
|
+
{
|
|
333
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
334
|
+
category: "provider_auth_error",
|
|
335
|
+
statusCode: status,
|
|
336
|
+
},
|
|
337
|
+
"Provider auth error",
|
|
338
|
+
);
|
|
339
|
+
return c.json(
|
|
340
|
+
{
|
|
341
|
+
error: "provider_auth_error",
|
|
342
|
+
message:
|
|
343
|
+
"LLM provider authentication failed. Check that your API key is valid and has not expired.",
|
|
344
|
+
},
|
|
345
|
+
503,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (status === 429) {
|
|
350
|
+
log.error(
|
|
351
|
+
{
|
|
352
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
353
|
+
category: "provider_rate_limit",
|
|
354
|
+
statusCode: status,
|
|
355
|
+
},
|
|
356
|
+
"Provider rate limit",
|
|
357
|
+
);
|
|
358
|
+
return c.json(
|
|
359
|
+
{
|
|
360
|
+
error: "provider_rate_limit",
|
|
361
|
+
message:
|
|
362
|
+
"LLM provider rate limit reached. Wait a moment and try again.",
|
|
363
|
+
},
|
|
364
|
+
503,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (status === 408 || /timeout/i.test(message)) {
|
|
369
|
+
log.error(
|
|
370
|
+
{
|
|
371
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
372
|
+
category: "provider_timeout",
|
|
373
|
+
statusCode: status,
|
|
374
|
+
},
|
|
375
|
+
"Request timed out",
|
|
376
|
+
);
|
|
377
|
+
return c.json(
|
|
378
|
+
{
|
|
379
|
+
error: "provider_timeout",
|
|
380
|
+
message:
|
|
381
|
+
"The request timed out. The LLM provider took too long to respond. " +
|
|
382
|
+
"Try again, or if using a local model, ensure it has sufficient resources.",
|
|
383
|
+
},
|
|
384
|
+
504,
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Catch-all for any other APICallError status codes (5xx, etc.)
|
|
389
|
+
log.error(
|
|
390
|
+
{
|
|
391
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
392
|
+
category: "provider_error",
|
|
393
|
+
statusCode: status,
|
|
394
|
+
},
|
|
395
|
+
"Provider error",
|
|
396
|
+
);
|
|
397
|
+
return c.json(
|
|
398
|
+
{
|
|
399
|
+
error: "provider_error",
|
|
400
|
+
message: `The LLM provider returned an error (HTTP ${status}). This is usually a temporary issue. Try again in a moment.`,
|
|
401
|
+
},
|
|
402
|
+
502,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// --- Regex fallbacks for non-APICallError exceptions ---
|
|
407
|
+
|
|
408
|
+
if (/timeout|timed out|AbortError/i.test(message)) {
|
|
409
|
+
log.error(
|
|
410
|
+
{
|
|
411
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
412
|
+
category: "provider_timeout",
|
|
413
|
+
},
|
|
414
|
+
"Request timed out",
|
|
415
|
+
);
|
|
416
|
+
return c.json(
|
|
417
|
+
{
|
|
418
|
+
error: "provider_timeout",
|
|
419
|
+
message:
|
|
420
|
+
"The request timed out. The LLM provider took too long to respond. " +
|
|
421
|
+
"Try again, or if using a local model, ensure it has sufficient resources.",
|
|
422
|
+
},
|
|
423
|
+
504,
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (/fetch failed|ECONNREFUSED|ENOTFOUND/i.test(message)) {
|
|
428
|
+
log.error(
|
|
429
|
+
{
|
|
430
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
431
|
+
category: "provider_unreachable",
|
|
432
|
+
},
|
|
433
|
+
"Provider unreachable",
|
|
434
|
+
);
|
|
435
|
+
return c.json(
|
|
436
|
+
{
|
|
437
|
+
error: "provider_unreachable",
|
|
438
|
+
message:
|
|
439
|
+
"Could not reach the LLM provider. Check your network connection and provider status.",
|
|
440
|
+
},
|
|
441
|
+
503,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Fallback — safe 500 with requestId for correlation
|
|
446
|
+
log.error(
|
|
447
|
+
{
|
|
448
|
+
err: err instanceof Error ? err : new Error(String(err)),
|
|
449
|
+
category: "internal_error",
|
|
450
|
+
},
|
|
451
|
+
"Unexpected error",
|
|
452
|
+
);
|
|
453
|
+
return c.json(
|
|
454
|
+
{
|
|
455
|
+
error: "internal_error",
|
|
456
|
+
message: `An unexpected error occurred (ref: ${requestId.slice(0, 8)}). If this persists, check the server logs.`,
|
|
457
|
+
},
|
|
458
|
+
500,
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
export { chat };
|