@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,370 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useChat } from "@ai-sdk/react";
|
|
4
|
+
import { DefaultChatTransport, isToolUIPart } from "ai";
|
|
5
|
+
import { useState, useRef, useEffect, useMemo, useCallback } from "react";
|
|
6
|
+
import { AUTH_MODES, type AuthMode } from "../lib/types";
|
|
7
|
+
import { useAtlasConfig, ActionAuthProvider } from "../context";
|
|
8
|
+
import { DarkModeContext } from "../hooks/use-dark-mode";
|
|
9
|
+
import { useDarkMode } from "../hooks/use-dark-mode";
|
|
10
|
+
import { useConversations } from "../hooks/use-conversations";
|
|
11
|
+
import { ErrorBanner } from "./chat/error-banner";
|
|
12
|
+
import { ApiKeyBar } from "./chat/api-key-bar";
|
|
13
|
+
import { ManagedAuthCard } from "./chat/managed-auth-card";
|
|
14
|
+
import { TypingIndicator } from "./chat/typing-indicator";
|
|
15
|
+
import { ToolPart } from "./chat/tool-part";
|
|
16
|
+
import { Markdown } from "./chat/markdown";
|
|
17
|
+
import { STARTER_PROMPTS } from "./chat/starter-prompts";
|
|
18
|
+
import { ConversationSidebar } from "./conversations/conversation-sidebar";
|
|
19
|
+
|
|
20
|
+
const API_KEY_STORAGE_KEY = "atlas-api-key";
|
|
21
|
+
|
|
22
|
+
export function AtlasChat() {
|
|
23
|
+
const { apiUrl, isCrossOrigin, authClient } = useAtlasConfig();
|
|
24
|
+
const dark = useDarkMode();
|
|
25
|
+
const [input, setInput] = useState("");
|
|
26
|
+
const [authMode, setAuthMode] = useState<AuthMode>("none");
|
|
27
|
+
const [healthWarning, setHealthWarning] = useState("");
|
|
28
|
+
const [apiKey, setApiKey] = useState("");
|
|
29
|
+
const [conversationId, setConversationId] = useState<string | null>(null);
|
|
30
|
+
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
|
|
31
|
+
const [loadingConversation, setLoadingConversation] = useState(false);
|
|
32
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
33
|
+
|
|
34
|
+
const managedSession = authClient.useSession();
|
|
35
|
+
const isManaged = authMode === "managed";
|
|
36
|
+
const isSignedIn = isManaged && !!managedSession.data?.user;
|
|
37
|
+
|
|
38
|
+
const getHeaders = useCallback(() => {
|
|
39
|
+
const headers: Record<string, string> = {};
|
|
40
|
+
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
|
41
|
+
return headers;
|
|
42
|
+
}, [apiKey]);
|
|
43
|
+
|
|
44
|
+
const getCredentials = useCallback((): RequestCredentials => {
|
|
45
|
+
return isCrossOrigin ? "include" : "same-origin";
|
|
46
|
+
}, [isCrossOrigin]);
|
|
47
|
+
|
|
48
|
+
const convos = useConversations({
|
|
49
|
+
apiUrl,
|
|
50
|
+
enabled: true,
|
|
51
|
+
getHeaders,
|
|
52
|
+
getCredentials,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const refreshConvosRef = useRef(convos.refresh);
|
|
56
|
+
refreshConvosRef.current = convos.refresh;
|
|
57
|
+
|
|
58
|
+
// Load API key from sessionStorage on mount + fetch auth mode + conversations
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
try {
|
|
61
|
+
const stored = sessionStorage.getItem(API_KEY_STORAGE_KEY);
|
|
62
|
+
if (stored) setApiKey(stored);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.warn("Cannot read API key from sessionStorage:", err);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function fetchHealth(attempt: number): Promise<void> {
|
|
68
|
+
try {
|
|
69
|
+
const res = await fetch(`${apiUrl}/api/health`, {
|
|
70
|
+
credentials: isCrossOrigin ? "include" : "same-origin",
|
|
71
|
+
});
|
|
72
|
+
if (!res.ok) {
|
|
73
|
+
console.warn(`Health check returned ${res.status}`);
|
|
74
|
+
if (attempt < 2) {
|
|
75
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
76
|
+
return fetchHealth(attempt + 1);
|
|
77
|
+
}
|
|
78
|
+
setHealthWarning("Health check failed — check server logs. Try refreshing the page.");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const data = await res.json();
|
|
82
|
+
const mode = data?.checks?.auth?.mode;
|
|
83
|
+
if (typeof mode === "string" && AUTH_MODES.includes(mode as AuthMode)) {
|
|
84
|
+
setAuthMode(mode as AuthMode);
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.warn("Health endpoint unavailable:", err);
|
|
88
|
+
if (attempt < 2) {
|
|
89
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
90
|
+
return fetchHealth(attempt + 1);
|
|
91
|
+
}
|
|
92
|
+
setHealthWarning("Unable to reach the API server. Try refreshing the page.");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
fetchHealth(1);
|
|
96
|
+
}, [apiUrl]);
|
|
97
|
+
|
|
98
|
+
// Fetch conversation list after auth is resolved
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
convos.fetchList();
|
|
101
|
+
}, [authMode, convos.fetchList]);
|
|
102
|
+
|
|
103
|
+
const handleSaveApiKey = useCallback((key: string) => {
|
|
104
|
+
setApiKey(key);
|
|
105
|
+
try {
|
|
106
|
+
sessionStorage.setItem(API_KEY_STORAGE_KEY, key);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.warn("Could not persist API key to sessionStorage:", err);
|
|
109
|
+
}
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
// Dynamic transport — captures x-conversation-id from response
|
|
113
|
+
const transport = useMemo(() => {
|
|
114
|
+
const headers: Record<string, string> = {};
|
|
115
|
+
if (apiKey) {
|
|
116
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
117
|
+
}
|
|
118
|
+
return new DefaultChatTransport({
|
|
119
|
+
api: `${apiUrl}/api/chat`,
|
|
120
|
+
headers,
|
|
121
|
+
credentials: isCrossOrigin ? "include" : undefined,
|
|
122
|
+
body: conversationId ? { conversationId } : undefined,
|
|
123
|
+
fetch: (async (input, init) => {
|
|
124
|
+
const response = await globalThis.fetch(input, init);
|
|
125
|
+
const convId = response.headers.get("x-conversation-id");
|
|
126
|
+
if (convId && convId !== conversationId) {
|
|
127
|
+
setConversationId(convId);
|
|
128
|
+
setTimeout(() => {
|
|
129
|
+
refreshConvosRef.current().catch((err) => {
|
|
130
|
+
console.warn("Sidebar refresh failed:", err);
|
|
131
|
+
});
|
|
132
|
+
}, 500);
|
|
133
|
+
}
|
|
134
|
+
return response;
|
|
135
|
+
}) as typeof fetch,
|
|
136
|
+
});
|
|
137
|
+
}, [apiKey, authMode, apiUrl, isCrossOrigin, conversationId]);
|
|
138
|
+
|
|
139
|
+
const { messages, setMessages, sendMessage, status, error } = useChat({ transport });
|
|
140
|
+
|
|
141
|
+
const isLoading = status === "streaming" || status === "submitted";
|
|
142
|
+
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
const el = scrollRef.current;
|
|
145
|
+
if (!el) return;
|
|
146
|
+
const isNearBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 100;
|
|
147
|
+
if (isNearBottom) el.scrollTop = el.scrollHeight;
|
|
148
|
+
}, [messages, status]);
|
|
149
|
+
|
|
150
|
+
function handleSend(text: string) {
|
|
151
|
+
if (!text.trim()) return;
|
|
152
|
+
const saved = text;
|
|
153
|
+
setInput("");
|
|
154
|
+
sendMessage({ text: saved }).catch((err) => {
|
|
155
|
+
console.error("Failed to send message:", err);
|
|
156
|
+
setInput(saved);
|
|
157
|
+
setHealthWarning("Failed to send message. Please try again.");
|
|
158
|
+
setTimeout(() => setHealthWarning(""), 5000);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function handleSelectConversation(id: string) {
|
|
163
|
+
if (loadingConversation) return;
|
|
164
|
+
setLoadingConversation(true);
|
|
165
|
+
try {
|
|
166
|
+
const uiMessages = await convos.loadConversation(id);
|
|
167
|
+
if (uiMessages) {
|
|
168
|
+
setMessages(uiMessages);
|
|
169
|
+
setConversationId(id);
|
|
170
|
+
convos.setSelectedId(id);
|
|
171
|
+
setMobileMenuOpen(false);
|
|
172
|
+
} else {
|
|
173
|
+
setHealthWarning("Could not load conversation. It may have been deleted.");
|
|
174
|
+
setTimeout(() => setHealthWarning(""), 5000);
|
|
175
|
+
}
|
|
176
|
+
} finally {
|
|
177
|
+
setLoadingConversation(false);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function handleNewChat() {
|
|
182
|
+
setMessages([]);
|
|
183
|
+
setConversationId(null);
|
|
184
|
+
convos.setSelectedId(null);
|
|
185
|
+
setInput("");
|
|
186
|
+
setMobileMenuOpen(false);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<DarkModeContext.Provider value={dark}>
|
|
191
|
+
<div className="flex h-dvh">
|
|
192
|
+
{convos.available && (
|
|
193
|
+
<ConversationSidebar
|
|
194
|
+
conversations={convos.conversations}
|
|
195
|
+
selectedId={convos.selectedId}
|
|
196
|
+
loading={convos.loading}
|
|
197
|
+
onSelect={handleSelectConversation}
|
|
198
|
+
onDelete={(id) => convos.deleteConversation(id)}
|
|
199
|
+
onStar={(id, starred) => convos.starConversation(id, starred)}
|
|
200
|
+
onNewChat={handleNewChat}
|
|
201
|
+
mobileOpen={mobileMenuOpen}
|
|
202
|
+
onMobileClose={() => setMobileMenuOpen(false)}
|
|
203
|
+
/>
|
|
204
|
+
)}
|
|
205
|
+
|
|
206
|
+
<main className="flex flex-1 flex-col overflow-hidden">
|
|
207
|
+
<div className="mx-auto flex w-full max-w-4xl flex-1 flex-col p-4">
|
|
208
|
+
<header className="mb-4 flex-none border-b border-zinc-100 pb-3 dark:border-zinc-800">
|
|
209
|
+
<div className="flex items-center justify-between">
|
|
210
|
+
<div className="flex items-center gap-3">
|
|
211
|
+
{convos.available && (
|
|
212
|
+
<button
|
|
213
|
+
onClick={() => setMobileMenuOpen(true)}
|
|
214
|
+
className="rounded p-1 text-zinc-400 hover:text-zinc-700 md:hidden dark:hover:text-zinc-200"
|
|
215
|
+
aria-label="Open conversation history"
|
|
216
|
+
>
|
|
217
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" className="h-5 w-5">
|
|
218
|
+
<path fillRule="evenodd" d="M2 4.75A.75.75 0 0 1 2.75 4h14.5a.75.75 0 0 1 0 1.5H2.75A.75.75 0 0 1 2 4.75ZM2 10a.75.75 0 0 1 .75-.75h14.5a.75.75 0 0 1 0 1.5H2.75A.75.75 0 0 1 2 10Zm0 5.25a.75.75 0 0 1 .75-.75h14.5a.75.75 0 0 1 0 1.5H2.75a.75.75 0 0 1-.75-.75Z" clipRule="evenodd" />
|
|
219
|
+
</svg>
|
|
220
|
+
</button>
|
|
221
|
+
)}
|
|
222
|
+
<div className="flex items-center gap-2.5">
|
|
223
|
+
<svg viewBox="0 0 256 256" fill="none" className="h-7 w-7 shrink-0" aria-hidden="true">
|
|
224
|
+
<path d="M128 24 L232 208 L24 208 Z" stroke="#23CE9E" strokeWidth="14" fill="none" strokeLinejoin="round"/>
|
|
225
|
+
<circle cx="128" cy="28" r="16" fill="#23CE9E"/>
|
|
226
|
+
</svg>
|
|
227
|
+
<div>
|
|
228
|
+
<h1 className="text-xl font-semibold tracking-tight">Atlas</h1>
|
|
229
|
+
<p className="text-sm text-zinc-500">Ask your data anything</p>
|
|
230
|
+
</div>
|
|
231
|
+
</div>
|
|
232
|
+
</div>
|
|
233
|
+
{isSignedIn && (
|
|
234
|
+
<div className="flex items-center gap-3">
|
|
235
|
+
<span className="text-xs text-zinc-500 dark:text-zinc-400">
|
|
236
|
+
{managedSession.data?.user?.email}
|
|
237
|
+
</span>
|
|
238
|
+
<button
|
|
239
|
+
onClick={() => {
|
|
240
|
+
authClient.signOut().catch((err: unknown) => {
|
|
241
|
+
console.error("Sign out failed:", err);
|
|
242
|
+
setHealthWarning("Sign out failed. Please try again.");
|
|
243
|
+
setTimeout(() => setHealthWarning(""), 5000);
|
|
244
|
+
});
|
|
245
|
+
}}
|
|
246
|
+
className="rounded border border-zinc-200 px-2 py-1 text-xs text-zinc-500 transition-colors hover:border-zinc-400 hover:text-zinc-800 dark:border-zinc-700 dark:text-zinc-400 dark:hover:border-zinc-500 dark:hover:text-zinc-200"
|
|
247
|
+
>
|
|
248
|
+
Sign out
|
|
249
|
+
</button>
|
|
250
|
+
</div>
|
|
251
|
+
)}
|
|
252
|
+
</div>
|
|
253
|
+
</header>
|
|
254
|
+
|
|
255
|
+
{healthWarning && (
|
|
256
|
+
<p className="mb-2 text-xs text-zinc-400 dark:text-zinc-500">{healthWarning}</p>
|
|
257
|
+
)}
|
|
258
|
+
|
|
259
|
+
{isManaged && !isSignedIn ? (
|
|
260
|
+
<ManagedAuthCard />
|
|
261
|
+
) : (
|
|
262
|
+
<ActionAuthProvider getHeaders={getHeaders} getCredentials={getCredentials}>
|
|
263
|
+
{authMode === "simple-key" && (
|
|
264
|
+
<div className="mb-3 flex-none">
|
|
265
|
+
<ApiKeyBar apiKey={apiKey} onSave={handleSaveApiKey} />
|
|
266
|
+
</div>
|
|
267
|
+
)}
|
|
268
|
+
|
|
269
|
+
<div ref={scrollRef} className="flex-1 space-y-4 overflow-y-auto pb-4">
|
|
270
|
+
{messages.length === 0 && !error && (
|
|
271
|
+
<div className="flex h-full flex-col items-center justify-center gap-6">
|
|
272
|
+
<div className="text-center">
|
|
273
|
+
<p className="text-lg font-medium text-zinc-500 dark:text-zinc-400">
|
|
274
|
+
What would you like to know?
|
|
275
|
+
</p>
|
|
276
|
+
<p className="mt-1 text-sm text-zinc-400 dark:text-zinc-600">
|
|
277
|
+
Ask a question about your data to get started
|
|
278
|
+
</p>
|
|
279
|
+
</div>
|
|
280
|
+
<div className="grid w-full max-w-lg grid-cols-2 gap-2">
|
|
281
|
+
{STARTER_PROMPTS.map((prompt) => (
|
|
282
|
+
<button
|
|
283
|
+
key={prompt}
|
|
284
|
+
onClick={() => handleSend(prompt)}
|
|
285
|
+
className="rounded-lg border border-zinc-200 bg-zinc-50 px-3 py-2.5 text-left text-sm text-zinc-500 transition-colors hover:border-zinc-400 hover:bg-zinc-100 hover:text-zinc-800 dark:border-zinc-800 dark:bg-zinc-900 dark:text-zinc-400 dark:hover:border-zinc-600 dark:hover:bg-zinc-800 dark:hover:text-zinc-200"
|
|
286
|
+
>
|
|
287
|
+
{prompt}
|
|
288
|
+
</button>
|
|
289
|
+
))}
|
|
290
|
+
</div>
|
|
291
|
+
</div>
|
|
292
|
+
)}
|
|
293
|
+
|
|
294
|
+
{messages.map((m) => {
|
|
295
|
+
if (m.role === "user") {
|
|
296
|
+
return (
|
|
297
|
+
<div key={m.id} className="flex justify-end">
|
|
298
|
+
<div className="max-w-[85%] rounded-xl bg-blue-600 px-4 py-3 text-sm text-white">
|
|
299
|
+
{m.parts?.map((part, i) =>
|
|
300
|
+
part.type === "text" ? (
|
|
301
|
+
<p key={i} className="whitespace-pre-wrap">
|
|
302
|
+
{part.text}
|
|
303
|
+
</p>
|
|
304
|
+
) : null,
|
|
305
|
+
)}
|
|
306
|
+
</div>
|
|
307
|
+
</div>
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return (
|
|
312
|
+
<div key={m.id} className="space-y-2">
|
|
313
|
+
{m.parts?.map((part, i) => {
|
|
314
|
+
if (part.type === "text" && part.text.trim()) {
|
|
315
|
+
return (
|
|
316
|
+
<div key={i} className="max-w-[90%]">
|
|
317
|
+
<div className="rounded-xl bg-zinc-100 px-4 py-3 text-sm text-zinc-800 dark:bg-zinc-800 dark:text-zinc-200">
|
|
318
|
+
<Markdown content={part.text} />
|
|
319
|
+
</div>
|
|
320
|
+
</div>
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
if (isToolUIPart(part)) {
|
|
324
|
+
return (
|
|
325
|
+
<div key={i} className="max-w-[95%]">
|
|
326
|
+
<ToolPart part={part} />
|
|
327
|
+
</div>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
return null;
|
|
331
|
+
})}
|
|
332
|
+
</div>
|
|
333
|
+
);
|
|
334
|
+
})}
|
|
335
|
+
|
|
336
|
+
{isLoading && messages.length > 0 && <TypingIndicator />}
|
|
337
|
+
</div>
|
|
338
|
+
|
|
339
|
+
{error && <ErrorBanner error={error} authMode={authMode} />}
|
|
340
|
+
|
|
341
|
+
<form
|
|
342
|
+
onSubmit={(e) => {
|
|
343
|
+
e.preventDefault();
|
|
344
|
+
handleSend(input);
|
|
345
|
+
}}
|
|
346
|
+
className="flex flex-none gap-2 border-t border-zinc-100 pt-4 dark:border-zinc-800"
|
|
347
|
+
>
|
|
348
|
+
<input
|
|
349
|
+
value={input}
|
|
350
|
+
onChange={(e) => setInput(e.target.value)}
|
|
351
|
+
placeholder="Ask a question about your data..."
|
|
352
|
+
className="flex-1 rounded-lg border border-zinc-200 bg-zinc-50 px-4 py-3 text-sm text-zinc-900 placeholder-zinc-400 outline-none focus:border-blue-500 dark:border-zinc-700 dark:bg-zinc-900 dark:text-zinc-100 dark:placeholder-zinc-600"
|
|
353
|
+
disabled={isLoading}
|
|
354
|
+
/>
|
|
355
|
+
<button
|
|
356
|
+
type="submit"
|
|
357
|
+
disabled={isLoading || !input.trim()}
|
|
358
|
+
className="rounded-lg bg-blue-600 px-5 py-3 text-sm font-medium text-white transition-colors hover:bg-blue-500 disabled:opacity-40"
|
|
359
|
+
>
|
|
360
|
+
Ask
|
|
361
|
+
</button>
|
|
362
|
+
</form>
|
|
363
|
+
</ActionAuthProvider>
|
|
364
|
+
)}
|
|
365
|
+
</div>
|
|
366
|
+
</main>
|
|
367
|
+
</div>
|
|
368
|
+
</DarkModeContext.Provider>
|
|
369
|
+
);
|
|
370
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/* Chart detection — pure functions, zero React deps. Kept framework-agnostic for direct unit testing. */
|
|
2
|
+
|
|
3
|
+
export type ColumnType = "numeric" | "date" | "categorical" | "unknown";
|
|
4
|
+
|
|
5
|
+
export type ClassifiedColumn = {
|
|
6
|
+
index: number;
|
|
7
|
+
header: string;
|
|
8
|
+
type: ColumnType;
|
|
9
|
+
uniqueCount: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ChartType = "bar" | "line" | "pie";
|
|
13
|
+
|
|
14
|
+
export type ChartRecommendation = {
|
|
15
|
+
type: ChartType;
|
|
16
|
+
categoryColumn: ClassifiedColumn;
|
|
17
|
+
valueColumns: [ClassifiedColumn, ...ClassifiedColumn[]];
|
|
18
|
+
reason: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type RechartsRow = Record<string, string | number>;
|
|
22
|
+
|
|
23
|
+
type NonChartableResult = {
|
|
24
|
+
chartable: false;
|
|
25
|
+
columns: ClassifiedColumn[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type ChartableResult = {
|
|
29
|
+
chartable: true;
|
|
30
|
+
columns: ClassifiedColumn[];
|
|
31
|
+
recommendations: [ChartRecommendation, ...ChartRecommendation[]];
|
|
32
|
+
data: RechartsRow[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ChartDetectionResult = NonChartableResult | ChartableResult;
|
|
36
|
+
|
|
37
|
+
/* ------------------------------------------------------------------ */
|
|
38
|
+
/* Color palettes (Tailwind weights) */
|
|
39
|
+
/* ------------------------------------------------------------------ */
|
|
40
|
+
|
|
41
|
+
export const CHART_COLORS_LIGHT = [
|
|
42
|
+
"#3b82f6", // blue-500
|
|
43
|
+
"#10b981", // emerald-500
|
|
44
|
+
"#f59e0b", // amber-500
|
|
45
|
+
"#ef4444", // red-500
|
|
46
|
+
"#8b5cf6", // violet-500
|
|
47
|
+
"#06b6d4", // cyan-500
|
|
48
|
+
"#f97316", // orange-500
|
|
49
|
+
"#ec4899", // pink-500
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
export const CHART_COLORS_DARK = [
|
|
53
|
+
"#60a5fa", // blue-400
|
|
54
|
+
"#34d399", // emerald-400
|
|
55
|
+
"#fbbf24", // amber-400
|
|
56
|
+
"#f87171", // red-400
|
|
57
|
+
"#a78bfa", // violet-400
|
|
58
|
+
"#22d3ee", // cyan-400
|
|
59
|
+
"#fb923c", // orange-400
|
|
60
|
+
"#f472b6", // pink-400
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/* ------------------------------------------------------------------ */
|
|
64
|
+
/* Column classification */
|
|
65
|
+
/* ------------------------------------------------------------------ */
|
|
66
|
+
|
|
67
|
+
const DATE_HEADER_HINTS = /^(date|month|year|quarter|week|day|period|time|timestamp)$/i;
|
|
68
|
+
const CATEGORICAL_HEADER_HINTS = /^(name|type|category|status|region|country|industry|department|plan|tier|segment|group|label|source|channel)$/i;
|
|
69
|
+
const SKIP_HEADER_HINTS = /^(id|uuid|_id|pk|key)$/i;
|
|
70
|
+
|
|
71
|
+
const ISO_DATE_RE = /^\d{4}-\d{2}/;
|
|
72
|
+
const MONTH_NAME_RE = /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i;
|
|
73
|
+
const YEAR_ONLY_RE = /^(19|20)\d{2}$/;
|
|
74
|
+
const QUARTER_RE = /^Q[1-4]\s*\d{4}$/i;
|
|
75
|
+
|
|
76
|
+
export function classifyColumn(header: string, values: string[]): ColumnType {
|
|
77
|
+
const nonEmpty = values.filter((v) => v !== "" && v != null);
|
|
78
|
+
if (nonEmpty.length === 0) return "unknown";
|
|
79
|
+
|
|
80
|
+
// Header hint: skip ID-like columns
|
|
81
|
+
if (SKIP_HEADER_HINTS.test(header)) return "unknown";
|
|
82
|
+
|
|
83
|
+
// Numeric check: >80% parse as finite numbers (date check takes priority for overlapping values)
|
|
84
|
+
const numericCount = nonEmpty.filter((v) => {
|
|
85
|
+
const n = Number(v.replace(/,/g, ""));
|
|
86
|
+
return isFinite(n);
|
|
87
|
+
}).length;
|
|
88
|
+
const numericRatio = numericCount / nonEmpty.length;
|
|
89
|
+
|
|
90
|
+
// Date check: >70% match date patterns (>30% when header hints match)
|
|
91
|
+
const dateCount = nonEmpty.filter(
|
|
92
|
+
(v) => ISO_DATE_RE.test(v) || MONTH_NAME_RE.test(v) || YEAR_ONLY_RE.test(v) || QUARTER_RE.test(v),
|
|
93
|
+
).length;
|
|
94
|
+
const dateRatio = dateCount / nonEmpty.length;
|
|
95
|
+
|
|
96
|
+
// Header hint tiebreaker: if header matches date keywords...
|
|
97
|
+
// (a) ...and at least some values look date-like, trust the header
|
|
98
|
+
// (b) ...and values aren't overwhelmingly numeric (catches year-only values)
|
|
99
|
+
if (DATE_HEADER_HINTS.test(header) && dateRatio > 0.3) return "date";
|
|
100
|
+
if (DATE_HEADER_HINTS.test(header) && numericRatio < 0.9) return "date";
|
|
101
|
+
|
|
102
|
+
if (dateRatio > 0.7) return "date";
|
|
103
|
+
if (numericRatio > 0.8) return "numeric";
|
|
104
|
+
|
|
105
|
+
// Categorical header hint
|
|
106
|
+
if (CATEGORICAL_HEADER_HINTS.test(header)) return "categorical";
|
|
107
|
+
|
|
108
|
+
// Categorical fallback: text values with <50 unique entries (higher cardinality suggests free-text or IDs)
|
|
109
|
+
const unique = new Set(nonEmpty);
|
|
110
|
+
if (unique.size < 50) return "categorical";
|
|
111
|
+
|
|
112
|
+
return "unknown";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* ------------------------------------------------------------------ */
|
|
116
|
+
/* Chart recommendation engine */
|
|
117
|
+
/* ------------------------------------------------------------------ */
|
|
118
|
+
|
|
119
|
+
export function detectCharts(headers: string[], rows: string[][]): ChartDetectionResult {
|
|
120
|
+
if (headers.length === 0 || rows.length < 2) {
|
|
121
|
+
return { chartable: false, columns: [] };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Deduplicate headers so chart dataKey matches transformed data keys
|
|
125
|
+
const seen = new Map<string, number>();
|
|
126
|
+
const dedupedHeaders = headers.map((h) => {
|
|
127
|
+
const count = seen.get(h) ?? 0;
|
|
128
|
+
seen.set(h, count + 1);
|
|
129
|
+
return count > 0 ? `${h}_${count + 1}` : h;
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const columns: ClassifiedColumn[] = dedupedHeaders.map((header, index) => {
|
|
133
|
+
const values = rows.map((r) => r[index] ?? "");
|
|
134
|
+
const type = classifyColumn(header, values);
|
|
135
|
+
const uniqueCount = new Set(values.filter((v) => v !== "")).size;
|
|
136
|
+
return { index, header, type, uniqueCount };
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const dateColumns = columns.filter((c) => c.type === "date");
|
|
140
|
+
const numericColumns = columns.filter((c) => c.type === "numeric");
|
|
141
|
+
const categoricalColumns = columns.filter((c) => c.type === "categorical");
|
|
142
|
+
|
|
143
|
+
if (numericColumns.length === 0) {
|
|
144
|
+
return { chartable: false, columns };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const recommendations: ChartRecommendation[] = [];
|
|
148
|
+
|
|
149
|
+
// Line: date + numeric (time-series, highest priority)
|
|
150
|
+
if (dateColumns.length >= 1 && numericColumns.length >= 1) {
|
|
151
|
+
recommendations.push({
|
|
152
|
+
type: "line",
|
|
153
|
+
categoryColumn: dateColumns[0],
|
|
154
|
+
valueColumns: numericColumns as [ClassifiedColumn, ...ClassifiedColumn[]],
|
|
155
|
+
reason: `Time-series: ${dateColumns[0].header} vs ${numericColumns.map((c) => c.header).join(", ")}`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Bar: categorical + numeric
|
|
160
|
+
if (categoricalColumns.length >= 1 && numericColumns.length >= 1) {
|
|
161
|
+
recommendations.push({
|
|
162
|
+
type: "bar",
|
|
163
|
+
categoryColumn: categoricalColumns[0],
|
|
164
|
+
valueColumns: numericColumns as [ClassifiedColumn, ...ClassifiedColumn[]],
|
|
165
|
+
reason: `Comparison: ${numericColumns.map((c) => c.header).join(", ")} by ${categoricalColumns[0].header}`,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Pie: first categorical column (2-7 unique values) + first numeric column
|
|
170
|
+
if (categoricalColumns.length >= 1 && numericColumns.length >= 1) {
|
|
171
|
+
const cat = categoricalColumns[0];
|
|
172
|
+
if (cat.uniqueCount >= 2 && cat.uniqueCount <= 7) {
|
|
173
|
+
recommendations.push({
|
|
174
|
+
type: "pie",
|
|
175
|
+
categoryColumn: cat,
|
|
176
|
+
valueColumns: [numericColumns[0]],
|
|
177
|
+
reason: `Distribution: ${numericColumns[0].header} by ${cat.header}`,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Fallback: when all columns are numeric, treat first as category axis (often an index or bucket label)
|
|
183
|
+
if (recommendations.length === 0 && numericColumns.length >= 2) {
|
|
184
|
+
const first = columns[0];
|
|
185
|
+
const rest = numericColumns.filter((c) => c.index !== first.index);
|
|
186
|
+
if (rest.length >= 1) {
|
|
187
|
+
recommendations.push({
|
|
188
|
+
type: "bar",
|
|
189
|
+
categoryColumn: first,
|
|
190
|
+
valueColumns: rest as [ClassifiedColumn, ...ClassifiedColumn[]],
|
|
191
|
+
reason: `Fallback: ${rest.map((c) => c.header).join(", ")} by ${first.header}`,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Also allow bar for date columns (as a secondary option after line)
|
|
197
|
+
if (dateColumns.length >= 1 && numericColumns.length >= 1 && !recommendations.some((r) => r.type === "bar")) {
|
|
198
|
+
recommendations.push({
|
|
199
|
+
type: "bar",
|
|
200
|
+
categoryColumn: dateColumns[0],
|
|
201
|
+
valueColumns: numericColumns as [ClassifiedColumn, ...ClassifiedColumn[]],
|
|
202
|
+
reason: `Comparison: ${numericColumns.map((c) => c.header).join(", ")} by ${dateColumns[0].header}`,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (recommendations.length === 0) {
|
|
207
|
+
return { chartable: false, columns };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const data = transformData(rows, recommendations[0]);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
chartable: true,
|
|
214
|
+
columns,
|
|
215
|
+
recommendations: recommendations as [ChartRecommendation, ...ChartRecommendation[]],
|
|
216
|
+
data,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* ------------------------------------------------------------------ */
|
|
221
|
+
/* Data transform */
|
|
222
|
+
/* ------------------------------------------------------------------ */
|
|
223
|
+
|
|
224
|
+
function parseNumericValue(raw: string): number {
|
|
225
|
+
const cleaned = raw.replace(/[$%,\s]/g, "");
|
|
226
|
+
if (cleaned === "" || cleaned === "-") return 0;
|
|
227
|
+
const num = Number(cleaned);
|
|
228
|
+
return isFinite(num) ? num : 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function transformData(
|
|
232
|
+
rows: string[][],
|
|
233
|
+
recommendation: ChartRecommendation,
|
|
234
|
+
): RechartsRow[] {
|
|
235
|
+
const catIdx = recommendation.categoryColumn.index;
|
|
236
|
+
const catHeader = recommendation.categoryColumn.header;
|
|
237
|
+
const valIdxs = recommendation.valueColumns.map((c) => c.index);
|
|
238
|
+
|
|
239
|
+
// Cap rows for bar charts with many categories
|
|
240
|
+
let effectiveRows = rows;
|
|
241
|
+
if (recommendation.type === "bar" && rows.length > 30) {
|
|
242
|
+
// Sort by first value column descending, take top 20
|
|
243
|
+
const valIdx = valIdxs[0];
|
|
244
|
+
effectiveRows = [...rows]
|
|
245
|
+
.sort((a, b) => {
|
|
246
|
+
const av = parseNumericValue(a[valIdx] ?? "0");
|
|
247
|
+
const bv = parseNumericValue(b[valIdx] ?? "0");
|
|
248
|
+
return bv - av;
|
|
249
|
+
})
|
|
250
|
+
.slice(0, 20);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return effectiveRows.map((row) => {
|
|
254
|
+
const record: RechartsRow = {};
|
|
255
|
+
record[catHeader] = row[catIdx] ?? "";
|
|
256
|
+
for (const vc of recommendation.valueColumns) {
|
|
257
|
+
record[vc.header] = parseNumericValue(row[vc.index] ?? "0");
|
|
258
|
+
}
|
|
259
|
+
return record;
|
|
260
|
+
});
|
|
261
|
+
}
|