mcoda 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +9 -0
- package/.eslintrc.cjs +12 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
- package/.github/ISSUE_TEMPLATE/config.yml +5 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
- package/.github/workflows/ci.yml +37 -0
- package/.github/workflows/nightly.yml +38 -0
- package/.github/workflows/release-dry-run.yml +40 -0
- package/.github/workflows/release-please.yml +22 -0
- package/.github/workflows/release.yml +149 -0
- package/.prettierrc +5 -0
- package/.release-please-manifest.json +8 -0
- package/CHANGELOG.md +7 -0
- package/CLA.md +42 -0
- package/CONTRIBUTING.md +38 -0
- package/LICENSE +21 -0
- package/README.md +314 -0
- package/docs/oss_publishing_plan.md +41 -0
- package/docs/pdr/.gitkeep +0 -0
- package/docs/quality_gates.md +32 -0
- package/docs/rfp/.gitkeep +0 -0
- package/docs/sds/sds.md +11963 -0
- package/docs/usage.md +72 -0
- package/openapi/gen-openapi.ts +1 -0
- package/openapi/generated/clients/.gitkeep +0 -0
- package/openapi/generated/types/.gitkeep +0 -0
- package/openapi/generated/types/index.ts +118 -0
- package/openapi/mcoda.yaml +2063 -0
- package/pack-mcoda.sh +88 -0
- package/package.json +46 -0
- package/packages/agents/CHANGELOG.md +7 -0
- package/packages/agents/LICENSE +21 -0
- package/packages/agents/README.md +9 -0
- package/packages/agents/package.json +41 -0
- package/packages/agents/src/AgentService/.gitkeep +0 -0
- package/packages/agents/src/AgentService/AgentService.d.ts +21 -0
- package/packages/agents/src/AgentService/AgentService.d.ts.map +1 -0
- package/packages/agents/src/AgentService/AgentService.js +141 -0
- package/packages/agents/src/AgentService/AgentService.ts +308 -0
- package/packages/agents/src/__tests__/AgentService.test.ts +284 -0
- package/packages/agents/src/adapters/AdapterTypes.d.ts +29 -0
- package/packages/agents/src/adapters/AdapterTypes.d.ts.map +1 -0
- package/packages/agents/src/adapters/AdapterTypes.js +1 -0
- package/packages/agents/src/adapters/AdapterTypes.ts +32 -0
- package/packages/agents/src/adapters/codex/.gitkeep +0 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.js +43 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.ts +63 -0
- package/packages/agents/src/adapters/codex/CodexCliRunner.ts +154 -0
- package/packages/agents/src/adapters/gemini/.gitkeep +0 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.js +42 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.ts +58 -0
- package/packages/agents/src/adapters/gemini/GeminiCliRunner.ts +75 -0
- package/packages/agents/src/adapters/local/.gitkeep +0 -0
- package/packages/agents/src/adapters/local/LocalAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/local/LocalAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/local/LocalAdapter.js +38 -0
- package/packages/agents/src/adapters/local/LocalAdapter.ts +43 -0
- package/packages/agents/src/adapters/ollama/OllamaCliAdapter.ts +58 -0
- package/packages/agents/src/adapters/ollama/OllamaCliRunner.ts +70 -0
- package/packages/agents/src/adapters/ollama/OllamaRemoteAdapter.ts +205 -0
- package/packages/agents/src/adapters/openai/.gitkeep +0 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.js +51 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.ts +56 -0
- package/packages/agents/src/adapters/openai/OpenAiCliAdapter.ts +62 -0
- package/packages/agents/src/adapters/qa/.gitkeep +0 -0
- package/packages/agents/src/adapters/qa/QaAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/qa/QaAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/qa/QaAdapter.js +37 -0
- package/packages/agents/src/adapters/qa/QaAdapter.ts +42 -0
- package/packages/agents/src/adapters/zhipu/ZhipuApiAdapter.ts +273 -0
- package/packages/agents/src/index.d.ts +8 -0
- package/packages/agents/src/index.d.ts.map +1 -0
- package/packages/agents/src/index.js +7 -0
- package/packages/agents/src/index.ts +11 -0
- package/packages/agents/tsconfig.json +14 -0
- package/packages/cli/CHANGELOG.md +7 -0
- package/packages/cli/LICENSE +21 -0
- package/packages/cli/README.md +23 -0
- package/packages/cli/package.json +61 -0
- package/packages/cli/src/__tests__/AgentsCommands.test.ts +137 -0
- package/packages/cli/src/__tests__/BacklogCommands.test.ts +40 -0
- package/packages/cli/src/__tests__/CodeReviewCommand.test.ts +594 -0
- package/packages/cli/src/__tests__/CreateTasksCommand.test.ts +40 -0
- package/packages/cli/src/__tests__/DocsCommands.test.ts +41 -0
- package/packages/cli/src/__tests__/EstimateCommands.test.ts +54 -0
- package/packages/cli/src/__tests__/JobsCommands.behavior.test.ts +311 -0
- package/packages/cli/src/__tests__/JobsCommands.test.ts +49 -0
- package/packages/cli/src/__tests__/MigrateTasksCommand.test.ts +36 -0
- package/packages/cli/src/__tests__/OpenapiCommands.test.ts +34 -0
- package/packages/cli/src/__tests__/OrderTasksCommand.test.ts +150 -0
- package/packages/cli/src/__tests__/PlanningCommands.test.ts +9 -0
- package/packages/cli/src/__tests__/QaTasksCommand.test.ts +58 -0
- package/packages/cli/src/__tests__/RefineTasksCommand.test.ts +63 -0
- package/packages/cli/src/__tests__/RoutingCommands.test.ts +302 -0
- package/packages/cli/src/__tests__/SetWorkspaceCommand.test.ts +18 -0
- package/packages/cli/src/__tests__/TaskShowCommands.test.ts +130 -0
- package/packages/cli/src/__tests__/TelemetryCommands.test.ts +35 -0
- package/packages/cli/src/__tests__/TestAgentCommand.test.ts +41 -0
- package/packages/cli/src/__tests__/UpdateCommands.test.ts +292 -0
- package/packages/cli/src/__tests__/WorkOnTasksCommand.test.ts +42 -0
- package/packages/cli/src/bin/.gitkeep +0 -0
- package/packages/cli/src/bin/McodaEntrypoint.ts +180 -0
- package/packages/cli/src/commands/agents/.gitkeep +0 -0
- package/packages/cli/src/commands/agents/AgentsCommands.ts +374 -0
- package/packages/cli/src/commands/agents/GatewayAgentCommand.ts +621 -0
- package/packages/cli/src/commands/agents/TestAgentCommand.ts +63 -0
- package/packages/cli/src/commands/backlog/.gitkeep +0 -0
- package/packages/cli/src/commands/backlog/BacklogCommands.ts +286 -0
- package/packages/cli/src/commands/backlog/OrderTasksCommand.ts +237 -0
- package/packages/cli/src/commands/backlog/TaskShowCommands.ts +289 -0
- package/packages/cli/src/commands/docs/.gitkeep +0 -0
- package/packages/cli/src/commands/docs/DocsCommands.ts +413 -0
- package/packages/cli/src/commands/estimate/EstimateCommands.ts +290 -0
- package/packages/cli/src/commands/jobs/.gitkeep +0 -0
- package/packages/cli/src/commands/jobs/JobsCommands.ts +595 -0
- package/packages/cli/src/commands/openapi/OpenapiCommands.ts +167 -0
- package/packages/cli/src/commands/planning/.gitkeep +0 -0
- package/packages/cli/src/commands/planning/CreateTasksCommand.ts +149 -0
- package/packages/cli/src/commands/planning/MigrateTasksCommand.ts +105 -0
- package/packages/cli/src/commands/planning/PlanningCommands.ts +1 -0
- package/packages/cli/src/commands/planning/QaTasksCommand.ts +320 -0
- package/packages/cli/src/commands/planning/RefineTasksCommand.ts +408 -0
- package/packages/cli/src/commands/review/CodeReviewCommand.ts +262 -0
- package/packages/cli/src/commands/routing/.gitkeep +0 -0
- package/packages/cli/src/commands/routing/RoutingCommands.ts +554 -0
- package/packages/cli/src/commands/telemetry/.gitkeep +0 -0
- package/packages/cli/src/commands/telemetry/TelemetryCommands.ts +348 -0
- package/packages/cli/src/commands/update/.gitkeep +0 -0
- package/packages/cli/src/commands/update/UpdateCommands.ts +301 -0
- package/packages/cli/src/commands/work/WorkOnTasksCommand.ts +264 -0
- package/packages/cli/src/commands/workspace/SetWorkspaceCommand.ts +132 -0
- package/packages/cli/src/index.ts +18 -0
- package/packages/cli/test/packaging_guardrails.test.js +75 -0
- package/packages/cli/tsconfig.json +20 -0
- package/packages/core/CHANGELOG.md +7 -0
- package/packages/core/LICENSE +21 -0
- package/packages/core/README.md +9 -0
- package/packages/core/package.json +45 -0
- package/packages/core/src/__tests__/SmokeClasses.test.ts +32 -0
- package/packages/core/src/api/AgentsApi.ts +219 -0
- package/packages/core/src/api/QaTasksApi.ts +38 -0
- package/packages/core/src/api/TasksApi.ts +35 -0
- package/packages/core/src/api/__tests__/AgentsApi.test.ts +203 -0
- package/packages/core/src/api/__tests__/QaTasksApi.test.ts +51 -0
- package/packages/core/src/api/__tests__/TasksApi.test.ts +56 -0
- package/packages/core/src/config/.gitkeep +0 -0
- package/packages/core/src/config/ConfigService.ts +1 -0
- package/packages/core/src/domain/dependencies/.gitkeep +0 -0
- package/packages/core/src/domain/dependencies/Dependency.ts +1 -0
- package/packages/core/src/domain/epics/.gitkeep +0 -0
- package/packages/core/src/domain/epics/Epic.ts +1 -0
- package/packages/core/src/domain/projects/.gitkeep +0 -0
- package/packages/core/src/domain/projects/Project.ts +1 -0
- package/packages/core/src/domain/tasks/.gitkeep +0 -0
- package/packages/core/src/domain/tasks/Task.ts +1 -0
- package/packages/core/src/domain/userStories/.gitkeep +0 -0
- package/packages/core/src/domain/userStories/UserStory.ts +1 -0
- package/packages/core/src/index.ts +27 -0
- package/packages/core/src/prompts/.gitkeep +0 -0
- package/packages/core/src/prompts/PdrPrompts.ts +23 -0
- package/packages/core/src/prompts/PromptLoader.ts +1 -0
- package/packages/core/src/prompts/SdsPrompts.ts +47 -0
- package/packages/core/src/services/agents/.gitkeep +0 -0
- package/packages/core/src/services/agents/AgentManagementService.ts +1 -0
- package/packages/core/src/services/agents/GatewayAgentService.ts +956 -0
- package/packages/core/src/services/agents/RoutingService.ts +461 -0
- package/packages/core/src/services/agents/__tests__/GatewayAgentService.test.ts +72 -0
- package/packages/core/src/services/agents/__tests__/RoutingService.test.ts +267 -0
- package/packages/core/src/services/agents/generated/RoutingApiClient.ts +89 -0
- package/packages/core/src/services/backlog/.gitkeep +0 -0
- package/packages/core/src/services/backlog/BacklogService.ts +580 -0
- package/packages/core/src/services/backlog/TaskOrderingService.ts +868 -0
- package/packages/core/src/services/backlog/__tests__/BacklogService.test.ts +219 -0
- package/packages/core/src/services/backlog/__tests__/TaskOrderingService.test.ts +268 -0
- package/packages/core/src/services/docs/.gitkeep +0 -0
- package/packages/core/src/services/docs/DocsService.ts +1913 -0
- package/packages/core/src/services/docs/__tests__/DocsService.test.ts +350 -0
- package/packages/core/src/services/estimate/EstimateService.ts +111 -0
- package/packages/core/src/services/estimate/VelocityService.ts +272 -0
- package/packages/core/src/services/estimate/__tests__/VelocityAndEstimate.test.ts +209 -0
- package/packages/core/src/services/estimate/types.ts +41 -0
- package/packages/core/src/services/execution/.gitkeep +0 -0
- package/packages/core/src/services/execution/ExecutionService.ts +1 -0
- package/packages/core/src/services/execution/QaFollowupService.ts +289 -0
- package/packages/core/src/services/execution/QaProfileService.ts +160 -0
- package/packages/core/src/services/execution/QaTasksService.ts +1303 -0
- package/packages/core/src/services/execution/TaskSelectionService.ts +362 -0
- package/packages/core/src/services/execution/TaskStateService.ts +64 -0
- package/packages/core/src/services/execution/WorkOnTasksService.ts +2023 -0
- package/packages/core/src/services/execution/__tests__/QaFollowupService.test.ts +58 -0
- package/packages/core/src/services/execution/__tests__/QaProfileService.test.ts +49 -0
- package/packages/core/src/services/execution/__tests__/QaTasksService.test.ts +157 -0
- package/packages/core/src/services/execution/__tests__/TaskSelectionService.test.ts +179 -0
- package/packages/core/src/services/execution/__tests__/TaskStateService.test.ts +51 -0
- package/packages/core/src/services/execution/__tests__/WorkOnTasksService.test.ts +285 -0
- package/packages/core/src/services/jobs/.gitkeep +0 -0
- package/packages/core/src/services/jobs/JobInsightsService.ts +355 -0
- package/packages/core/src/services/jobs/JobResumeService.ts +119 -0
- package/packages/core/src/services/jobs/JobService.ts +648 -0
- package/packages/core/src/services/jobs/JobsApiClient.ts +113 -0
- package/packages/core/src/services/jobs/__tests__/JobInsightsService.test.ts +17 -0
- package/packages/core/src/services/jobs/__tests__/JobResumeService.test.ts +45 -0
- package/packages/core/src/services/jobs/__tests__/JobService.test.ts +44 -0
- package/packages/core/src/services/openapi/OpenApiService.ts +558 -0
- package/packages/core/src/services/openapi/__tests__/OpenApiService.test.ts +57 -0
- package/packages/core/src/services/planning/.gitkeep +0 -0
- package/packages/core/src/services/planning/CreateTasksService.ts +1280 -0
- package/packages/core/src/services/planning/KeyHelpers.ts +80 -0
- package/packages/core/src/services/planning/PlanningService.ts +1 -0
- package/packages/core/src/services/planning/RefineTasksService.ts +1552 -0
- package/packages/core/src/services/planning/__tests__/CreateTasksService.test.ts +288 -0
- package/packages/core/src/services/planning/__tests__/KeyHelpers.test.ts +16 -0
- package/packages/core/src/services/planning/__tests__/RefineTasksService.test.ts +172 -0
- package/packages/core/src/services/review/CodeReviewService.ts +1386 -0
- package/packages/core/src/services/review/__tests__/CodeReviewService.test.ts +89 -0
- package/packages/core/src/services/system/SystemUpdateService.ts +177 -0
- package/packages/core/src/services/system/__tests__/SystemUpdateService.test.ts +40 -0
- package/packages/core/src/services/tasks/TaskApiResolver.ts +37 -0
- package/packages/core/src/services/tasks/TaskDetailService.ts +494 -0
- package/packages/core/src/services/tasks/__tests__/TaskApiResolver.test.ts +41 -0
- package/packages/core/src/services/tasks/__tests__/TaskDetailService.test.ts +178 -0
- package/packages/core/src/services/telemetry/.gitkeep +0 -0
- package/packages/core/src/services/telemetry/TelemetryService.ts +515 -0
- package/packages/core/src/services/telemetry/__tests__/TelemetryService.test.ts +160 -0
- package/packages/core/src/workspace/.gitkeep +0 -0
- package/packages/core/src/workspace/WorkspaceManager.ts +234 -0
- package/packages/core/tsconfig.json +20 -0
- package/packages/db/CHANGELOG.md +7 -0
- package/packages/db/LICENSE +21 -0
- package/packages/db/README.md +9 -0
- package/packages/db/package.json +42 -0
- package/packages/db/src/__tests__/GlobalRepository.test.ts +109 -0
- package/packages/db/src/__tests__/SchemaAlignment.test.ts +80 -0
- package/packages/db/src/__tests__/WorkspaceRepository.test.ts +19 -0
- package/packages/db/src/index.d.ts +6 -0
- package/packages/db/src/index.d.ts.map +1 -0
- package/packages/db/src/index.js +5 -0
- package/packages/db/src/index.ts +6 -0
- package/packages/db/src/migrations/global/.gitkeep +0 -0
- package/packages/db/src/migrations/global/GlobalMigrations.d.ts +9 -0
- package/packages/db/src/migrations/global/GlobalMigrations.d.ts.map +1 -0
- package/packages/db/src/migrations/global/GlobalMigrations.js +68 -0
- package/packages/db/src/migrations/global/GlobalMigrations.ts +336 -0
- package/packages/db/src/migrations/workspace/.gitkeep +0 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.d.ts +9 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.d.ts.map +1 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.js +251 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.ts +248 -0
- package/packages/db/src/repositories/global/.gitkeep +0 -0
- package/packages/db/src/repositories/global/GlobalRepository.d.ts +30 -0
- package/packages/db/src/repositories/global/GlobalRepository.d.ts.map +1 -0
- package/packages/db/src/repositories/global/GlobalRepository.js +209 -0
- package/packages/db/src/repositories/global/GlobalRepository.ts +492 -0
- package/packages/db/src/repositories/workspace/.gitkeep +0 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.d.ts +282 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.d.ts.map +1 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.js +773 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.ts +1511 -0
- package/packages/db/src/sqlite/connection.d.ts +11 -0
- package/packages/db/src/sqlite/connection.d.ts.map +1 -0
- package/packages/db/src/sqlite/connection.js +31 -0
- package/packages/db/src/sqlite/connection.ts +35 -0
- package/packages/db/src/sqlite/pragmas.d.ts +5 -0
- package/packages/db/src/sqlite/pragmas.d.ts.map +1 -0
- package/packages/db/src/sqlite/pragmas.js +6 -0
- package/packages/db/src/sqlite/pragmas.ts +10 -0
- package/packages/db/tsconfig.json +13 -0
- package/packages/generators/package.json +21 -0
- package/packages/generators/src/__tests__/Generators.test.ts +19 -0
- package/packages/generators/src/index.ts +1 -0
- package/packages/generators/src/openapi/generateTypes.ts +1 -0
- package/packages/generators/src/openapi/validateSchema.ts +1 -0
- package/packages/generators/src/scaffolding/docs/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/docs/DocsScaffolder.ts +1 -0
- package/packages/generators/src/scaffolding/global/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/global/GlobalScaffolder.ts +1 -0
- package/packages/generators/src/scaffolding/workspace/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/workspace/WorkspaceScaffolder.ts +1 -0
- package/packages/generators/tsconfig.json +10 -0
- package/packages/integrations/CHANGELOG.md +7 -0
- package/packages/integrations/LICENSE +21 -0
- package/packages/integrations/README.md +9 -0
- package/packages/integrations/package.json +47 -0
- package/packages/integrations/src/docdex/.gitkeep +0 -0
- package/packages/integrations/src/docdex/DocdexClient.d.ts +50 -0
- package/packages/integrations/src/docdex/DocdexClient.d.ts.map +1 -0
- package/packages/integrations/src/docdex/DocdexClient.js +216 -0
- package/packages/integrations/src/docdex/DocdexClient.ts +261 -0
- package/packages/integrations/src/docdex/__tests__/DocdexClient.test.ts +29 -0
- package/packages/integrations/src/index.d.ts +2 -0
- package/packages/integrations/src/index.d.ts.map +1 -0
- package/packages/integrations/src/index.js +4 -0
- package/packages/integrations/src/index.ts +5 -0
- package/packages/integrations/src/issues/.gitkeep +0 -0
- package/packages/integrations/src/issues/IssuesClient.ts +1 -0
- package/packages/integrations/src/issues/__tests__/IssuesClient.test.ts +10 -0
- package/packages/integrations/src/qa/.gitkeep +0 -0
- package/packages/integrations/src/qa/ChromiumQaAdapter.ts +89 -0
- package/packages/integrations/src/qa/CliQaAdapter.ts +95 -0
- package/packages/integrations/src/qa/MaestroQaAdapter.ts +91 -0
- package/packages/integrations/src/qa/QaAdapter.ts +7 -0
- package/packages/integrations/src/qa/QaClient.ts +1 -0
- package/packages/integrations/src/qa/QaTypes.ts +26 -0
- package/packages/integrations/src/qa/__tests__/ChromiumQaAdapter.test.ts +30 -0
- package/packages/integrations/src/qa/__tests__/CliQaAdapter.test.ts +33 -0
- package/packages/integrations/src/qa/__tests__/MaestroQaAdapter.test.ts +30 -0
- package/packages/integrations/src/qa/index.ts +5 -0
- package/packages/integrations/src/system/SystemClient.ts +50 -0
- package/packages/integrations/src/system/__tests__/SystemClient.test.ts +40 -0
- package/packages/integrations/src/telemetry/TelemetryClient.ts +139 -0
- package/packages/integrations/src/telemetry/__tests__/TelemetryClient.test.ts +41 -0
- package/packages/integrations/src/vcs/.gitkeep +0 -0
- package/packages/integrations/src/vcs/VcsClient.ts +211 -0
- package/packages/integrations/src/vcs/__tests__/VcsClient.test.ts +26 -0
- package/packages/integrations/tsconfig.json +14 -0
- package/packages/shared/CHANGELOG.md +7 -0
- package/packages/shared/LICENSE +21 -0
- package/packages/shared/README.md +9 -0
- package/packages/shared/package.json +40 -0
- package/packages/shared/src/__tests__/CommandMetadata.test.ts +15 -0
- package/packages/shared/src/__tests__/ServiceShells.test.ts +16 -0
- package/packages/shared/src/crypto/.gitkeep +0 -0
- package/packages/shared/src/crypto/CryptoHelper.d.ts +15 -0
- package/packages/shared/src/crypto/CryptoHelper.d.ts.map +1 -0
- package/packages/shared/src/crypto/CryptoHelper.js +54 -0
- package/packages/shared/src/crypto/CryptoHelper.ts +57 -0
- package/packages/shared/src/errors/.gitkeep +0 -0
- package/packages/shared/src/errors/ErrorFactory.ts +1 -0
- package/packages/shared/src/index.d.ts +6 -0
- package/packages/shared/src/index.d.ts.map +1 -0
- package/packages/shared/src/index.js +4 -0
- package/packages/shared/src/index.ts +35 -0
- package/packages/shared/src/logging/.gitkeep +0 -0
- package/packages/shared/src/logging/Logger.ts +1 -0
- package/packages/shared/src/metadata/CommandMetadata.ts +165 -0
- package/packages/shared/src/openapi/.gitkeep +0 -0
- package/packages/shared/src/openapi/OpenApiTypes.d.ts +216 -0
- package/packages/shared/src/openapi/OpenApiTypes.d.ts.map +1 -0
- package/packages/shared/src/openapi/OpenApiTypes.js +1 -0
- package/packages/shared/src/openapi/OpenApiTypes.ts +312 -0
- package/packages/shared/src/paths/.gitkeep +0 -0
- package/packages/shared/src/paths/PathHelper.d.ts +12 -0
- package/packages/shared/src/paths/PathHelper.d.ts.map +1 -0
- package/packages/shared/src/paths/PathHelper.js +24 -0
- package/packages/shared/src/paths/PathHelper.ts +29 -0
- package/packages/shared/src/qa/QaProfile.ts +14 -0
- package/packages/shared/src/utils/.gitkeep +0 -0
- package/packages/shared/src/utils/UtilityService.ts +1 -0
- package/packages/shared/tsconfig.json +10 -0
- package/packages/testing/package.json +26 -0
- package/packages/testing/src/__tests__/TestingFakes.test.ts +15 -0
- package/packages/testing/src/cli/e2e/.gitkeep +0 -0
- package/packages/testing/src/cli/e2e/E2eSuite.ts +1 -0
- package/packages/testing/src/fakes/agents/.gitkeep +0 -0
- package/packages/testing/src/fakes/agents/FakeAgents.ts +1 -0
- package/packages/testing/src/fakes/docdex/.gitkeep +0 -0
- package/packages/testing/src/fakes/docdex/FakeDocdexClient.ts +1 -0
- package/packages/testing/src/fakes/qa/.gitkeep +0 -0
- package/packages/testing/src/fakes/qa/FakeQaClient.ts +1 -0
- package/packages/testing/src/fakes/vcs/.gitkeep +0 -0
- package/packages/testing/src/fakes/vcs/FakeVcsClient.ts +1 -0
- package/packages/testing/src/fixtures/db/.gitkeep +0 -0
- package/packages/testing/src/fixtures/db/DbFixtures.ts +1 -0
- package/packages/testing/src/fixtures/workspaces/.gitkeep +0 -0
- package/packages/testing/src/fixtures/workspaces/WorkspaceFixtures.ts +1 -0
- package/packages/testing/src/index.ts +1 -0
- package/packages/testing/tsconfig.json +10 -0
- package/pnpm-workspace.yaml +2 -0
- package/prompts/README.md +5 -0
- package/prompts/code-reviewer.md +23 -0
- package/prompts/code-writer.md +35 -0
- package/prompts/gateway-agent.md +27 -0
- package/prompts/qa-agent.md +21 -0
- package/release-please-config.json +39 -0
- package/scripts/build-all.ts +1 -0
- package/scripts/dev.ts +1 -0
- package/scripts/install-local-cli.sh +28 -0
- package/scripts/pack-npm-tarballs.js +63 -0
- package/scripts/release.ts +1 -0
- package/scripts/run-node-tests.js +37 -0
- package/tests/all.js +127 -0
- package/tests/api/openapi_spec.test.js +21 -0
- package/tests/artifacts.md +31 -0
- package/tests/component/cli_version.test.js +38 -0
- package/tests/integration/workspace_resolver.test.js +44 -0
- package/tests/unit/crypto_helper.test.js +36 -0
- package/tests/unit/path_helper.test.js +20 -0
- package/tsconfig.base.json +32 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { Connection, GlobalRepository, type Database } from "@mcoda/db";
|
|
4
|
+
import { PathHelper } from "@mcoda/shared";
|
|
5
|
+
import { TelemetryClient, TokenUsageRow, TokenUsageSummaryRow, type TelemetryConfig as ApiTelemetryConfig } from "@mcoda/integrations";
|
|
6
|
+
import { WorkspaceResolution } from "../../workspace/WorkspaceManager.js";
|
|
7
|
+
|
|
8
|
+
export type { TokenUsageRow, TokenUsageSummaryRow } from "@mcoda/integrations";
|
|
9
|
+
|
|
10
|
+
type GroupByDimension = "project" | "agent" | "command" | "day" | "model" | "job" | "action";
|
|
11
|
+
|
|
12
|
+
export interface TelemetryConfigState extends ApiTelemetryConfig {
|
|
13
|
+
configPath: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface TelemetrySummaryOptions {
|
|
17
|
+
projectKey?: string;
|
|
18
|
+
agent?: string;
|
|
19
|
+
command?: string;
|
|
20
|
+
jobId?: string;
|
|
21
|
+
since?: string;
|
|
22
|
+
until?: string;
|
|
23
|
+
groupBy?: GroupByDimension[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface TokenUsageQueryOptions {
|
|
27
|
+
projectKey?: string;
|
|
28
|
+
agent?: string;
|
|
29
|
+
command?: string;
|
|
30
|
+
jobId?: string;
|
|
31
|
+
since?: string;
|
|
32
|
+
until?: string;
|
|
33
|
+
page?: number;
|
|
34
|
+
pageSize?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface DbTokenUsageRow {
|
|
38
|
+
workspace_id: string;
|
|
39
|
+
agent_id: string | null;
|
|
40
|
+
model_name: string | null;
|
|
41
|
+
job_id: string | null;
|
|
42
|
+
command_run_id: string | null;
|
|
43
|
+
task_run_id: string | null;
|
|
44
|
+
task_id: string | null;
|
|
45
|
+
project_id: string | null;
|
|
46
|
+
epic_id: string | null;
|
|
47
|
+
user_story_id: string | null;
|
|
48
|
+
tokens_prompt: number | null;
|
|
49
|
+
tokens_completion: number | null;
|
|
50
|
+
tokens_total: number | null;
|
|
51
|
+
cost_estimate: number | null;
|
|
52
|
+
duration_seconds: number | null;
|
|
53
|
+
timestamp: string;
|
|
54
|
+
metadata_json?: string | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const hasTables = async (db: Database, required: string[]): Promise<boolean> => {
|
|
58
|
+
const rows = await db.all<{ name: string }[]>(`SELECT name FROM sqlite_master WHERE type='table'`);
|
|
59
|
+
const names = new Set(rows.map((r) => r.name));
|
|
60
|
+
return required.every((name) => names.has(name));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const parseMetadata = (raw?: string | null): Record<string, unknown> => {
|
|
64
|
+
if (!raw) return {};
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(raw) as Record<string, unknown>;
|
|
67
|
+
} catch {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const addCost = (current: number | null, value: number | null | undefined): number | null => {
|
|
73
|
+
if (value === null || value === undefined) return current;
|
|
74
|
+
return (current ?? 0) + value;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const parseDurationMs = (input: string): number | undefined => {
|
|
78
|
+
const match = input.trim().match(/^(\d+)([smhdw])$/i);
|
|
79
|
+
if (!match) return undefined;
|
|
80
|
+
const amount = Number(match[1]);
|
|
81
|
+
const unit = match[2].toLowerCase();
|
|
82
|
+
const multipliers: Record<string, number> = {
|
|
83
|
+
s: 1000,
|
|
84
|
+
m: 60 * 1000,
|
|
85
|
+
h: 60 * 60 * 1000,
|
|
86
|
+
d: 24 * 60 * 60 * 1000,
|
|
87
|
+
w: 7 * 24 * 60 * 60 * 1000,
|
|
88
|
+
};
|
|
89
|
+
return multipliers[unit] ? amount * multipliers[unit] : undefined;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const parseTimeInput = (input?: string): string | undefined => {
|
|
93
|
+
if (!input) return undefined;
|
|
94
|
+
const duration = parseDurationMs(input);
|
|
95
|
+
if (duration !== undefined) {
|
|
96
|
+
return new Date(Date.now() - duration).toISOString();
|
|
97
|
+
}
|
|
98
|
+
const parsed = Date.parse(input);
|
|
99
|
+
if (!Number.isNaN(parsed)) {
|
|
100
|
+
return new Date(parsed).toISOString();
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const normalizeGroupBy = (groupBy?: GroupByDimension[]): GroupByDimension[] => {
|
|
106
|
+
if (!groupBy || groupBy.length === 0) {
|
|
107
|
+
return ["project", "command", "agent"];
|
|
108
|
+
}
|
|
109
|
+
const seen = new Set<GroupByDimension>();
|
|
110
|
+
for (const dim of groupBy) {
|
|
111
|
+
if (["project", "agent", "command", "day", "model", "job", "action"].includes(dim)) {
|
|
112
|
+
seen.add(dim);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return Array.from(seen);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const dayFromTimestamp = (timestamp: string | undefined): string | null => {
|
|
119
|
+
if (!timestamp) return null;
|
|
120
|
+
const parsed = Date.parse(timestamp);
|
|
121
|
+
if (Number.isNaN(parsed)) return null;
|
|
122
|
+
return new Date(parsed).toISOString().slice(0, 10);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export class TelemetryService {
|
|
126
|
+
private globalRepo?: GlobalRepository;
|
|
127
|
+
private client?: TelemetryClient;
|
|
128
|
+
private db?: Database;
|
|
129
|
+
private connection?: Connection;
|
|
130
|
+
|
|
131
|
+
private constructor(private workspace: WorkspaceResolution, deps: { db?: Database; connection?: Connection; client?: TelemetryClient }) {
|
|
132
|
+
this.db = deps.db;
|
|
133
|
+
this.connection = deps.connection;
|
|
134
|
+
this.client = deps.client;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static async create(
|
|
138
|
+
workspace: WorkspaceResolution,
|
|
139
|
+
options: { allowMissingTelemetry?: boolean; requireApi?: boolean } = {},
|
|
140
|
+
): Promise<TelemetryService> {
|
|
141
|
+
const baseUrl = workspace.config?.telemetry?.endpoint ?? process.env.MCODA_TELEMETRY_API;
|
|
142
|
+
const authToken = workspace.config?.telemetry?.authToken ?? process.env.MCODA_TELEMETRY_TOKEN;
|
|
143
|
+
if (baseUrl) {
|
|
144
|
+
return new TelemetryService(workspace, { client: new TelemetryClient({ baseUrl, authToken }) });
|
|
145
|
+
}
|
|
146
|
+
if (options.requireApi) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
"Telemetry API is not configured (set MCODA_TELEMETRY_API/MCODA_TELEMETRY_TOKEN or telemetry.endpoint in workspace config).",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const dbPath = PathHelper.getWorkspaceDbPath(workspace.workspaceRoot);
|
|
153
|
+
try {
|
|
154
|
+
await fs.access(dbPath);
|
|
155
|
+
} catch {
|
|
156
|
+
if (!options.allowMissingTelemetry) {
|
|
157
|
+
throw new Error(`No workspace DB found at ${dbPath}. Run mcoda init or create-tasks first.`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const connection = await Connection.open(dbPath);
|
|
161
|
+
const ok = await hasTables(connection.db, ["token_usage"]);
|
|
162
|
+
if (!ok && !options.allowMissingTelemetry) {
|
|
163
|
+
await connection.close();
|
|
164
|
+
throw new Error("Workspace DB is missing telemetry tables (token_usage). Run create-tasks to initialize it.");
|
|
165
|
+
}
|
|
166
|
+
return new TelemetryService(workspace, { db: connection.db, connection });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async close(): Promise<void> {
|
|
170
|
+
if (this.connection) {
|
|
171
|
+
await this.connection.close();
|
|
172
|
+
}
|
|
173
|
+
if (this.globalRepo) {
|
|
174
|
+
await this.globalRepo.close();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private get configPath(): string {
|
|
179
|
+
return path.join(this.workspace.mcodaDir, "config.json");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private async readConfigFile(): Promise<Record<string, any>> {
|
|
183
|
+
try {
|
|
184
|
+
const raw = await fs.readFile(this.configPath, "utf8");
|
|
185
|
+
return JSON.parse(raw) as Record<string, any>;
|
|
186
|
+
} catch {
|
|
187
|
+
return {};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private async writeConfigFile(config: Record<string, any>): Promise<void> {
|
|
192
|
+
await fs.mkdir(this.workspace.mcodaDir, { recursive: true });
|
|
193
|
+
await fs.writeFile(this.configPath, JSON.stringify(config, null, 2), "utf8");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private async resolveProjectId(projectKey?: string): Promise<string | undefined> {
|
|
197
|
+
if (!projectKey || !this.db) return undefined;
|
|
198
|
+
const row = await this.db.get<{ id: string } | undefined>(`SELECT id FROM projects WHERE key = ?`, projectKey);
|
|
199
|
+
return row?.id;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private async getGlobalRepo(): Promise<GlobalRepository> {
|
|
203
|
+
if (!this.globalRepo) {
|
|
204
|
+
this.globalRepo = await GlobalRepository.create();
|
|
205
|
+
}
|
|
206
|
+
return this.globalRepo;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private async resolveAgentId(agent?: string): Promise<string | undefined> {
|
|
210
|
+
if (!agent) return undefined;
|
|
211
|
+
try {
|
|
212
|
+
const repo = await this.getGlobalRepo();
|
|
213
|
+
const asId = await repo.getAgentById(agent);
|
|
214
|
+
if (asId) return asId.id;
|
|
215
|
+
const bySlug = await repo.getAgentBySlug(agent);
|
|
216
|
+
if (bySlug) return bySlug.id;
|
|
217
|
+
} catch {
|
|
218
|
+
// ignore lookup failures; fall through to raw value
|
|
219
|
+
}
|
|
220
|
+
return agent;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private filterRows(
|
|
224
|
+
rows: DbTokenUsageRow[],
|
|
225
|
+
options: { command?: string },
|
|
226
|
+
): { row: DbTokenUsageRow; metadata: Record<string, unknown> }[] {
|
|
227
|
+
const filtered: { row: DbTokenUsageRow; metadata: Record<string, unknown> }[] = [];
|
|
228
|
+
for (const row of rows) {
|
|
229
|
+
const metadata = parseMetadata(row.metadata_json);
|
|
230
|
+
const commandName =
|
|
231
|
+
(metadata.commandName as string | undefined) ??
|
|
232
|
+
(metadata.command_name as string | undefined) ??
|
|
233
|
+
(metadata.command as string | undefined);
|
|
234
|
+
if (options.command && commandName && options.command !== commandName) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
filtered.push({ row, metadata });
|
|
238
|
+
}
|
|
239
|
+
return filtered;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private mapConfig(apiConfig: ApiTelemetryConfig): TelemetryConfigState {
|
|
243
|
+
return {
|
|
244
|
+
...apiConfig,
|
|
245
|
+
localRecording: apiConfig.localRecording ?? true,
|
|
246
|
+
remoteExport: apiConfig.remoteExport ?? true,
|
|
247
|
+
optOut: apiConfig.optOut ?? false,
|
|
248
|
+
strict: apiConfig.strict ?? false,
|
|
249
|
+
configPath: this.configPath,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async getSummary(options: TelemetrySummaryOptions = {}): Promise<TokenUsageSummaryRow[]> {
|
|
254
|
+
const groupBy = normalizeGroupBy(options.groupBy);
|
|
255
|
+
const since = parseTimeInput(options.since ?? "7d") ?? undefined;
|
|
256
|
+
const until = parseTimeInput(options.until);
|
|
257
|
+
const projectId = options.projectKey ? await this.resolveProjectId(options.projectKey) : undefined;
|
|
258
|
+
if (options.projectKey && !projectId && !this.client) {
|
|
259
|
+
throw new Error(`Unknown project key: ${options.projectKey}`);
|
|
260
|
+
}
|
|
261
|
+
const agentId = await this.resolveAgentId(options.agent);
|
|
262
|
+
|
|
263
|
+
if (this.client) {
|
|
264
|
+
return this.client.getSummary({
|
|
265
|
+
workspaceId: this.workspace.workspaceId,
|
|
266
|
+
projectId: projectId ?? options.projectKey,
|
|
267
|
+
agentId: agentId ?? options.agent,
|
|
268
|
+
commandName: options.command,
|
|
269
|
+
jobId: options.jobId,
|
|
270
|
+
from: since,
|
|
271
|
+
to: until,
|
|
272
|
+
groupBy,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!this.db) {
|
|
277
|
+
throw new Error("Telemetry DB not available and no telemetry client configured.");
|
|
278
|
+
}
|
|
279
|
+
const clauses = ["workspace_id = ?"];
|
|
280
|
+
const params: any[] = [this.workspace.workspaceId];
|
|
281
|
+
if (projectId) {
|
|
282
|
+
clauses.push("project_id = ?");
|
|
283
|
+
params.push(projectId);
|
|
284
|
+
}
|
|
285
|
+
if (agentId) {
|
|
286
|
+
clauses.push("agent_id = ?");
|
|
287
|
+
params.push(agentId);
|
|
288
|
+
}
|
|
289
|
+
if (options.jobId) {
|
|
290
|
+
clauses.push("job_id = ?");
|
|
291
|
+
params.push(options.jobId);
|
|
292
|
+
}
|
|
293
|
+
if (since) {
|
|
294
|
+
clauses.push("timestamp >= ?");
|
|
295
|
+
params.push(since);
|
|
296
|
+
}
|
|
297
|
+
if (until) {
|
|
298
|
+
clauses.push("timestamp <= ?");
|
|
299
|
+
params.push(until);
|
|
300
|
+
}
|
|
301
|
+
const query = `SELECT * FROM token_usage ${clauses.length ? `WHERE ${clauses.join(" AND ")}` : ""}`;
|
|
302
|
+
const rows = await this.db.all<DbTokenUsageRow[]>(query, ...params);
|
|
303
|
+
const filtered = this.filterRows(rows, { command: options.command });
|
|
304
|
+
|
|
305
|
+
const summary = new Map<string, TokenUsageSummaryRow>();
|
|
306
|
+
for (const { row, metadata } of filtered) {
|
|
307
|
+
const commandName =
|
|
308
|
+
(metadata.commandName as string | undefined) ??
|
|
309
|
+
(metadata.command_name as string | undefined) ??
|
|
310
|
+
(metadata.command as string | undefined) ??
|
|
311
|
+
null;
|
|
312
|
+
const action = (metadata.action as string | undefined) ?? (metadata.phase as string | undefined) ?? null;
|
|
313
|
+
const keyParts = groupBy.map((dim) => {
|
|
314
|
+
switch (dim) {
|
|
315
|
+
case "project":
|
|
316
|
+
return row.project_id ?? "";
|
|
317
|
+
case "agent":
|
|
318
|
+
return row.agent_id ?? "";
|
|
319
|
+
case "model":
|
|
320
|
+
return row.model_name ?? "";
|
|
321
|
+
case "command":
|
|
322
|
+
return commandName ?? "";
|
|
323
|
+
case "day":
|
|
324
|
+
return dayFromTimestamp(row.timestamp) ?? "";
|
|
325
|
+
case "job":
|
|
326
|
+
return row.job_id ?? "";
|
|
327
|
+
case "action":
|
|
328
|
+
return action ?? "";
|
|
329
|
+
default:
|
|
330
|
+
return "";
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
const key = keyParts.join("|");
|
|
334
|
+
let record = summary.get(key);
|
|
335
|
+
if (!record) {
|
|
336
|
+
record = {
|
|
337
|
+
workspace_id: this.workspace.workspaceId,
|
|
338
|
+
project_id: groupBy.includes("project") ? row.project_id ?? null : undefined,
|
|
339
|
+
agent_id: groupBy.includes("agent") ? row.agent_id ?? null : undefined,
|
|
340
|
+
model_name: groupBy.includes("model") ? row.model_name ?? null : undefined,
|
|
341
|
+
command_name: groupBy.includes("command") ? commandName : undefined,
|
|
342
|
+
action: groupBy.includes("action") ? action : undefined,
|
|
343
|
+
job_id: groupBy.includes("job") ? row.job_id ?? null : undefined,
|
|
344
|
+
day: groupBy.includes("day") ? dayFromTimestamp(row.timestamp) : undefined,
|
|
345
|
+
calls: 0,
|
|
346
|
+
tokens_prompt: 0,
|
|
347
|
+
tokens_completion: 0,
|
|
348
|
+
tokens_total: 0,
|
|
349
|
+
cost_estimate: null,
|
|
350
|
+
};
|
|
351
|
+
summary.set(key, record);
|
|
352
|
+
}
|
|
353
|
+
record.calls += 1;
|
|
354
|
+
record.tokens_prompt += row.tokens_prompt ?? 0;
|
|
355
|
+
record.tokens_completion += row.tokens_completion ?? 0;
|
|
356
|
+
record.tokens_total += row.tokens_total ?? 0;
|
|
357
|
+
record.cost_estimate = addCost(record.cost_estimate, row.cost_estimate);
|
|
358
|
+
}
|
|
359
|
+
return Array.from(summary.values());
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async getTokenUsage(options: TokenUsageQueryOptions = {}): Promise<TokenUsageRow[]> {
|
|
363
|
+
const since = parseTimeInput(options.since);
|
|
364
|
+
const until = parseTimeInput(options.until);
|
|
365
|
+
const projectId = options.projectKey ? await this.resolveProjectId(options.projectKey) : undefined;
|
|
366
|
+
if (options.projectKey && !projectId && !this.client) {
|
|
367
|
+
throw new Error(`Unknown project key: ${options.projectKey}`);
|
|
368
|
+
}
|
|
369
|
+
const agentId = await this.resolveAgentId(options.agent);
|
|
370
|
+
|
|
371
|
+
if (this.client) {
|
|
372
|
+
const rows = await this.client.getTokenUsage({
|
|
373
|
+
workspaceId: this.workspace.workspaceId,
|
|
374
|
+
projectId: projectId ?? options.projectKey,
|
|
375
|
+
agentId: agentId ?? options.agent,
|
|
376
|
+
commandName: options.command,
|
|
377
|
+
jobId: options.jobId,
|
|
378
|
+
from: since,
|
|
379
|
+
to: until,
|
|
380
|
+
page: options.page,
|
|
381
|
+
pageSize: options.pageSize,
|
|
382
|
+
sort: "timestamp:asc",
|
|
383
|
+
});
|
|
384
|
+
return rows.sort((a, b) => Date.parse(a.timestamp) - Date.parse(b.timestamp));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (!this.db) {
|
|
388
|
+
throw new Error("Telemetry DB not available and no telemetry client configured.");
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const clauses = ["workspace_id = ?"];
|
|
392
|
+
const params: any[] = [this.workspace.workspaceId];
|
|
393
|
+
if (projectId) {
|
|
394
|
+
clauses.push("project_id = ?");
|
|
395
|
+
params.push(projectId);
|
|
396
|
+
}
|
|
397
|
+
if (agentId) {
|
|
398
|
+
clauses.push("agent_id = ?");
|
|
399
|
+
params.push(agentId);
|
|
400
|
+
}
|
|
401
|
+
if (options.jobId) {
|
|
402
|
+
clauses.push("job_id = ?");
|
|
403
|
+
params.push(options.jobId);
|
|
404
|
+
}
|
|
405
|
+
if (since) {
|
|
406
|
+
clauses.push("timestamp >= ?");
|
|
407
|
+
params.push(since);
|
|
408
|
+
}
|
|
409
|
+
if (until) {
|
|
410
|
+
clauses.push("timestamp <= ?");
|
|
411
|
+
params.push(until);
|
|
412
|
+
}
|
|
413
|
+
const query = `SELECT * FROM token_usage ${clauses.length ? `WHERE ${clauses.join(" AND ")}` : ""}`;
|
|
414
|
+
const rows = await this.db.all<DbTokenUsageRow[]>(query, ...params);
|
|
415
|
+
const filtered = this.filterRows(rows, { command: options.command });
|
|
416
|
+
const mapped: TokenUsageRow[] = filtered
|
|
417
|
+
.map(({ row, metadata }) => {
|
|
418
|
+
const commandName =
|
|
419
|
+
(metadata.commandName as string | undefined) ??
|
|
420
|
+
(metadata.command_name as string | undefined) ??
|
|
421
|
+
(metadata.command as string | undefined) ??
|
|
422
|
+
null;
|
|
423
|
+
const action = (metadata.action as string | undefined) ?? (metadata.phase as string | undefined) ?? null;
|
|
424
|
+
const errorKind =
|
|
425
|
+
(metadata.error_kind as string | undefined) ??
|
|
426
|
+
(metadata.errorKind as string | undefined) ??
|
|
427
|
+
(metadata.error as string | undefined) ??
|
|
428
|
+
null;
|
|
429
|
+
return {
|
|
430
|
+
workspace_id: row.workspace_id,
|
|
431
|
+
agent_id: row.agent_id,
|
|
432
|
+
model_name: row.model_name,
|
|
433
|
+
job_id: row.job_id,
|
|
434
|
+
command_run_id: row.command_run_id,
|
|
435
|
+
task_run_id: row.task_run_id,
|
|
436
|
+
task_id: row.task_id,
|
|
437
|
+
project_id: row.project_id,
|
|
438
|
+
epic_id: row.epic_id,
|
|
439
|
+
user_story_id: row.user_story_id,
|
|
440
|
+
tokens_prompt: row.tokens_prompt,
|
|
441
|
+
tokens_completion: row.tokens_completion,
|
|
442
|
+
tokens_total: row.tokens_total,
|
|
443
|
+
cost_estimate: row.cost_estimate,
|
|
444
|
+
duration_seconds: row.duration_seconds,
|
|
445
|
+
timestamp: row.timestamp,
|
|
446
|
+
command_name: commandName,
|
|
447
|
+
action,
|
|
448
|
+
error_kind: errorKind,
|
|
449
|
+
metadata,
|
|
450
|
+
};
|
|
451
|
+
})
|
|
452
|
+
.sort((a, b) => {
|
|
453
|
+
const aTs = Date.parse(a.timestamp);
|
|
454
|
+
const bTs = Date.parse(b.timestamp);
|
|
455
|
+
if (Number.isNaN(aTs) || Number.isNaN(bTs)) return 0;
|
|
456
|
+
return aTs - bTs;
|
|
457
|
+
});
|
|
458
|
+
if (options.page && options.pageSize) {
|
|
459
|
+
const start = (options.page - 1) * options.pageSize;
|
|
460
|
+
return mapped.slice(start, start + options.pageSize);
|
|
461
|
+
}
|
|
462
|
+
return mapped;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async getConfig(): Promise<TelemetryConfigState> {
|
|
466
|
+
if (this.client) {
|
|
467
|
+
const config = await this.client.getConfig(this.workspace.workspaceId);
|
|
468
|
+
return this.mapConfig(config);
|
|
469
|
+
}
|
|
470
|
+
const config = await this.readConfigFile();
|
|
471
|
+
const telemetry = config.telemetry ?? {};
|
|
472
|
+
const envOptOut = (process.env.MCODA_TELEMETRY ?? "").toLowerCase() === "off";
|
|
473
|
+
const optOut = telemetry.optOut === true || telemetry.optedOut === true || false;
|
|
474
|
+
const strict = telemetry.strict === true || false;
|
|
475
|
+
const remoteExport = !optOut && !envOptOut;
|
|
476
|
+
const localRecording = !strict;
|
|
477
|
+
return {
|
|
478
|
+
localRecording,
|
|
479
|
+
remoteExport,
|
|
480
|
+
optOut,
|
|
481
|
+
strict,
|
|
482
|
+
configPath: this.configPath,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async optOut(strict = false): Promise<TelemetryConfigState> {
|
|
487
|
+
if (this.client) {
|
|
488
|
+
const config = await this.client.optOut(this.workspace.workspaceId, strict);
|
|
489
|
+
return this.mapConfig(config);
|
|
490
|
+
}
|
|
491
|
+
const config = await this.readConfigFile();
|
|
492
|
+
config.telemetry = {
|
|
493
|
+
...(config.telemetry ?? {}),
|
|
494
|
+
optOut: true,
|
|
495
|
+
strict: strict || (config.telemetry?.strict ?? false),
|
|
496
|
+
};
|
|
497
|
+
await this.writeConfigFile(config);
|
|
498
|
+
return this.getConfig();
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
async optIn(): Promise<TelemetryConfigState> {
|
|
502
|
+
if (this.client) {
|
|
503
|
+
const config = await this.client.optIn(this.workspace.workspaceId);
|
|
504
|
+
return this.mapConfig(config);
|
|
505
|
+
}
|
|
506
|
+
const config = await this.readConfigFile();
|
|
507
|
+
config.telemetry = {
|
|
508
|
+
...(config.telemetry ?? {}),
|
|
509
|
+
optOut: false,
|
|
510
|
+
strict: false,
|
|
511
|
+
};
|
|
512
|
+
await this.writeConfigFile(config);
|
|
513
|
+
return this.getConfig();
|
|
514
|
+
}
|
|
515
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import fs from "node:fs/promises";
|
|
6
|
+
import { Connection, WorkspaceMigrations, WorkspaceRepository } from "@mcoda/db";
|
|
7
|
+
import { PathHelper } from "@mcoda/shared";
|
|
8
|
+
import { TelemetryService } from "../TelemetryService.js";
|
|
9
|
+
import { WorkspaceResolution } from "../../../workspace/WorkspaceManager.js";
|
|
10
|
+
|
|
11
|
+
const workspaceFromRoot = (workspaceRoot: string): WorkspaceResolution => ({
|
|
12
|
+
workspaceRoot,
|
|
13
|
+
workspaceId: workspaceRoot,
|
|
14
|
+
mcodaDir: path.join(workspaceRoot, ".mcoda"),
|
|
15
|
+
id: workspaceRoot,
|
|
16
|
+
legacyWorkspaceIds: [],
|
|
17
|
+
workspaceDbPath: PathHelper.getWorkspaceDbPath(workspaceRoot),
|
|
18
|
+
globalDbPath: PathHelper.getGlobalDbPath(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("TelemetryService", () => {
|
|
22
|
+
let workspaceRoot: string;
|
|
23
|
+
let connection: Connection;
|
|
24
|
+
let repo: WorkspaceRepository;
|
|
25
|
+
let workspace: WorkspaceResolution;
|
|
26
|
+
let projectId: string;
|
|
27
|
+
let job1: string;
|
|
28
|
+
let job2: string;
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
workspaceRoot = await fs.mkdtemp(path.join(os.tmpdir(), "mcoda-telemetry-"));
|
|
32
|
+
await fs.mkdir(path.join(workspaceRoot, ".mcoda"), { recursive: true });
|
|
33
|
+
const dbPath = PathHelper.getWorkspaceDbPath(workspaceRoot);
|
|
34
|
+
connection = await Connection.open(dbPath);
|
|
35
|
+
await WorkspaceMigrations.run(connection.db);
|
|
36
|
+
repo = new WorkspaceRepository(connection.db, connection);
|
|
37
|
+
const project = await repo.createProjectIfMissing({ key: "PROJ", name: "Project" });
|
|
38
|
+
projectId = project.id;
|
|
39
|
+
workspace = workspaceFromRoot(workspaceRoot);
|
|
40
|
+
const jobRow1 = await repo.createJob({ workspaceId: workspace.workspaceId, type: "work-on-tasks", state: "running", commandName: "work-on-tasks" });
|
|
41
|
+
const jobRow2 = await repo.createJob({ workspaceId: workspace.workspaceId, type: "docs", state: "running", commandName: "docs-pdr-generate" });
|
|
42
|
+
job1 = jobRow1.id;
|
|
43
|
+
job2 = jobRow2.id;
|
|
44
|
+
|
|
45
|
+
const now = new Date().toISOString();
|
|
46
|
+
const recent = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString();
|
|
47
|
+
const old = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000).toISOString();
|
|
48
|
+
|
|
49
|
+
await repo.recordTokenUsage({
|
|
50
|
+
workspaceId: workspace.workspaceId,
|
|
51
|
+
projectId,
|
|
52
|
+
agentId: "agent-1",
|
|
53
|
+
modelName: "gpt-4",
|
|
54
|
+
jobId: job1,
|
|
55
|
+
tokensPrompt: 10,
|
|
56
|
+
tokensCompletion: 5,
|
|
57
|
+
tokensTotal: 15,
|
|
58
|
+
costEstimate: 0.5,
|
|
59
|
+
durationSeconds: 2,
|
|
60
|
+
timestamp: now,
|
|
61
|
+
metadata: { commandName: "work-on-tasks", action: "plan" },
|
|
62
|
+
});
|
|
63
|
+
await repo.recordTokenUsage({
|
|
64
|
+
workspaceId: workspace.workspaceId,
|
|
65
|
+
projectId,
|
|
66
|
+
agentId: "agent-1",
|
|
67
|
+
modelName: "gpt-4",
|
|
68
|
+
jobId: job1,
|
|
69
|
+
tokensPrompt: 6,
|
|
70
|
+
tokensCompletion: 4,
|
|
71
|
+
tokensTotal: 10,
|
|
72
|
+
costEstimate: 0.25,
|
|
73
|
+
durationSeconds: 1,
|
|
74
|
+
timestamp: recent,
|
|
75
|
+
metadata: { commandName: "work-on-tasks", action: "draft" },
|
|
76
|
+
});
|
|
77
|
+
await repo.recordTokenUsage({
|
|
78
|
+
workspaceId: workspace.workspaceId,
|
|
79
|
+
projectId,
|
|
80
|
+
agentId: "agent-2",
|
|
81
|
+
modelName: "gpt-3",
|
|
82
|
+
jobId: job2,
|
|
83
|
+
tokensPrompt: 3,
|
|
84
|
+
tokensCompletion: 2,
|
|
85
|
+
tokensTotal: 5,
|
|
86
|
+
costEstimate: null,
|
|
87
|
+
durationSeconds: 0.5,
|
|
88
|
+
timestamp: old,
|
|
89
|
+
metadata: { commandName: "docs-pdr-generate", action: "context" },
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
afterEach(async () => {
|
|
94
|
+
await repo.close();
|
|
95
|
+
await fs.rm(workspaceRoot, { recursive: true, force: true });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("aggregates token usage with default grouping and window", async () => {
|
|
99
|
+
const service = await TelemetryService.create(workspace);
|
|
100
|
+
try {
|
|
101
|
+
const summary = await service.getSummary();
|
|
102
|
+
assert.equal(summary.length, 1);
|
|
103
|
+
const row = summary[0];
|
|
104
|
+
assert.equal(row.calls, 2);
|
|
105
|
+
assert.equal(row.tokens_total, 25);
|
|
106
|
+
assert.equal(row.tokens_prompt, 16);
|
|
107
|
+
assert.equal(row.tokens_completion, 9);
|
|
108
|
+
assert.equal(row.cost_estimate, 0.75);
|
|
109
|
+
assert.equal(row.command_name, "work-on-tasks");
|
|
110
|
+
assert.equal(row.agent_id, "agent-1");
|
|
111
|
+
assert.equal(row.project_id, projectId);
|
|
112
|
+
} finally {
|
|
113
|
+
await service.close();
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("supports grouping by day", async () => {
|
|
118
|
+
const service = await TelemetryService.create(workspace);
|
|
119
|
+
try {
|
|
120
|
+
const summary = await service.getSummary({ groupBy: ["day"] });
|
|
121
|
+
assert.equal(summary.length, 1);
|
|
122
|
+
assert.ok(summary[0].day);
|
|
123
|
+
assert.equal(summary[0].calls, 2);
|
|
124
|
+
} finally {
|
|
125
|
+
await service.close();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("filters raw token usage by job id and since", async () => {
|
|
130
|
+
const service = await TelemetryService.create(workspace);
|
|
131
|
+
try {
|
|
132
|
+
const rows = await service.getTokenUsage({ jobId: job1 });
|
|
133
|
+
assert.equal(rows.length, 2);
|
|
134
|
+
assert.equal(rows[0].job_id, job1);
|
|
135
|
+
const oldRows = await service.getTokenUsage({ jobId: job2, since: "7d" });
|
|
136
|
+
assert.equal(oldRows.length, 0);
|
|
137
|
+
} finally {
|
|
138
|
+
await service.close();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("manages telemetry config opt-out and opt-in", async () => {
|
|
143
|
+
const service = await TelemetryService.create(workspace, { allowMissingTelemetry: true });
|
|
144
|
+
try {
|
|
145
|
+
const initial = await service.getConfig();
|
|
146
|
+
assert.equal(initial.optOut, false);
|
|
147
|
+
assert.equal(initial.strict, false);
|
|
148
|
+
const optedOut = await service.optOut(true);
|
|
149
|
+
assert.equal(optedOut.optOut, true);
|
|
150
|
+
assert.equal(optedOut.strict, true);
|
|
151
|
+
assert.equal(optedOut.localRecording, false);
|
|
152
|
+
const optedIn = await service.optIn();
|
|
153
|
+
assert.equal(optedIn.optOut, false);
|
|
154
|
+
assert.equal(optedIn.strict, false);
|
|
155
|
+
assert.equal(optedIn.localRecording, true);
|
|
156
|
+
} finally {
|
|
157
|
+
await service.close();
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
File without changes
|