@agentlang/agentmanager 0.0.3
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 +122 -0
- package/apps/agentcraft/README.md +237 -0
- package/apps/agentcraft/src/agentcraft-core.module.ts +40 -0
- package/apps/agentcraft/src/app.module.ts +28 -0
- package/apps/agentcraft/src/architect/architect.controller.ts +69 -0
- package/apps/agentcraft/src/architect/architect.module.ts +11 -0
- package/apps/agentcraft/src/architect/architect.service.ts +193 -0
- package/apps/agentcraft/src/architect/architect.store.ts +63 -0
- package/apps/agentcraft/src/architect/dto/ingest-signal.dto.ts +20 -0
- package/apps/agentcraft/src/architect/interfaces/architect.interfaces.ts +20 -0
- package/apps/agentcraft/src/autonomous/autonomous.controller.ts +92 -0
- package/apps/agentcraft/src/autonomous/autonomous.module.ts +9 -0
- package/apps/agentcraft/src/autonomous/autonomous.service.ts +160 -0
- package/apps/agentcraft/src/autonomous/dto/run-autonomous.dto.ts +16 -0
- package/apps/agentcraft/src/autonomous/dto/watch-kb.dto.ts +16 -0
- package/apps/agentcraft/src/config/configuration.ts +74 -0
- package/apps/agentcraft/src/health/health.controller.ts +22 -0
- package/apps/agentcraft/src/health/health.module.ts +9 -0
- package/apps/agentcraft/src/main.ts +53 -0
- package/apps/agentcraft/src/resolver/dto/create-resolver.dto.ts +14 -0
- package/apps/agentcraft/src/resolver/dto/install-registry.dto.ts +10 -0
- package/apps/agentcraft/src/resolver/dto/search-registry-query.dto.ts +10 -0
- package/apps/agentcraft/src/resolver/dto/submit-action.dto.ts +4 -0
- package/apps/agentcraft/src/resolver/resolver-session.store.ts +39 -0
- package/apps/agentcraft/src/resolver/resolver.controller.ts +86 -0
- package/apps/agentcraft/src/resolver/resolver.module.ts +10 -0
- package/apps/agentcraft/src/resolver/resolver.service.ts +128 -0
- package/apps/agentcraft/src/sessions/dto/create-session.dto.ts +12 -0
- package/apps/agentcraft/src/sessions/dto/submit-action.dto.ts +1 -0
- package/apps/agentcraft/src/sessions/dto/submit-idea.dto.ts +8 -0
- package/apps/agentcraft/src/sessions/session.store.ts +39 -0
- package/apps/agentcraft/src/sessions/sessions.controller.ts +87 -0
- package/apps/agentcraft/src/sessions/sessions.module.ts +10 -0
- package/apps/agentcraft/src/sessions/sessions.service.ts +277 -0
- package/apps/agentcraft/tsconfig.app.json +8 -0
- package/apps/agentmanager/README.md +141 -0
- package/apps/agentmanager/src/agentmanager-core.module.ts +45 -0
- package/apps/agentmanager/src/app.module.ts +26 -0
- package/apps/agentmanager/src/config/configuration.ts +63 -0
- package/apps/agentmanager/src/deployments/deployments.controller.ts +21 -0
- package/apps/agentmanager/src/deployments/deployments.module.ts +10 -0
- package/apps/agentmanager/src/deployments/deployments.service.ts +166 -0
- package/apps/agentmanager/src/deployments/errors/deployment.errors.ts +13 -0
- package/apps/agentmanager/src/deployments/interfaces/deployment.interfaces.ts +25 -0
- package/apps/agentmanager/src/discovery/discovery.controller.ts +14 -0
- package/apps/agentmanager/src/discovery/discovery.module.ts +36 -0
- package/apps/agentmanager/src/discovery/discovery.service.ts +174 -0
- package/apps/agentmanager/src/discovery/dto/discover.dto.ts +8 -0
- package/apps/agentmanager/src/discovery/interfaces/discovery.interfaces.ts +22 -0
- package/apps/agentmanager/src/discovery/llm/anthropic-llm.service.ts +67 -0
- package/apps/agentmanager/src/discovery/llm/llm-provider.interface.ts +17 -0
- package/apps/agentmanager/src/discovery/llm/llm.errors.ts +7 -0
- package/apps/agentmanager/src/discovery/llm/openai-llm.service.ts +67 -0
- package/apps/agentmanager/src/employee-code/dto/put-config.dto.ts +8 -0
- package/apps/agentmanager/src/employee-code/dto/put-module.dto.ts +8 -0
- package/apps/agentmanager/src/employee-code/dto/put-package.dto.ts +13 -0
- package/apps/agentmanager/src/employee-code/employee-code.controller.ts +114 -0
- package/apps/agentmanager/src/employee-code/employee-code.module.ts +10 -0
- package/apps/agentmanager/src/employee-code/employee-code.service.ts +55 -0
- package/apps/agentmanager/src/employees/dto/register-employee.dto.ts +9 -0
- package/apps/agentmanager/src/employees/employees.controller.ts +53 -0
- package/apps/agentmanager/src/employees/employees.module.ts +10 -0
- package/apps/agentmanager/src/employees/employees.service.ts +29 -0
- package/apps/agentmanager/src/health/health.controller.ts +22 -0
- package/apps/agentmanager/src/health/health.module.ts +9 -0
- package/apps/agentmanager/src/inbox/dto/list-messages-query.dto.ts +12 -0
- package/apps/agentmanager/src/inbox/dto/push-message.dto.ts +55 -0
- package/apps/agentmanager/src/inbox/dto/respond-message.dto.ts +9 -0
- package/apps/agentmanager/src/inbox/inbox.controller.ts +87 -0
- package/apps/agentmanager/src/inbox/inbox.module.ts +10 -0
- package/apps/agentmanager/src/inbox/inbox.service.ts +209 -0
- package/apps/agentmanager/src/main.ts +53 -0
- package/apps/agentmanager/src/projects/dto/create-project.dto.ts +10 -0
- package/apps/agentmanager/src/projects/projects.controller.ts +47 -0
- package/apps/agentmanager/src/projects/projects.module.ts +10 -0
- package/apps/agentmanager/src/projects/projects.service.ts +24 -0
- package/apps/agentmanager/tsconfig.app.json +8 -0
- package/apps/fabric/Dockerfile +22 -0
- package/apps/fabric/docker-compose.yml +14 -0
- package/apps/fabric/ecosystem.config.cjs +17 -0
- package/apps/fabric/src/ai-agent/ai-agent.module.ts +43 -0
- package/apps/fabric/src/ai-agent/ai-agent.service.ts +374 -0
- package/apps/fabric/src/ai-agent/probe-investigation.types.ts +24 -0
- package/apps/fabric/src/app.module.ts +40 -0
- package/apps/fabric/src/auth/current-user.decorator.ts +11 -0
- package/apps/fabric/src/auth/request-user.ts +4 -0
- package/apps/fabric/src/config/configuration.ts +266 -0
- package/apps/fabric/src/database/entities/deployment-event.entity.ts +33 -0
- package/apps/fabric/src/database/entities/environment.entity.ts +25 -0
- package/apps/fabric/src/database/entities/index.ts +25 -0
- package/apps/fabric/src/database/entities/project.entity.ts +25 -0
- package/apps/fabric/src/database/entities/runtime.entity.ts +55 -0
- package/apps/fabric/src/database/entities/user-config.entity.ts +22 -0
- package/apps/fabric/src/database/entities/version.entity.ts +22 -0
- package/apps/fabric/src/database/migrations-data-source.ts +27 -0
- package/apps/fabric/src/deployment-events/deployment-events.controller.ts +27 -0
- package/apps/fabric/src/deployment-events/deployment-events.module.ts +11 -0
- package/apps/fabric/src/deployment-events/deployment-events.service.ts +58 -0
- package/apps/fabric/src/deployment-events/deployment-events.store.ts +162 -0
- package/apps/fabric/src/deployment-events/deployment-events.types.ts +24 -0
- package/apps/fabric/src/employees/dto/deploy.dto.ts +71 -0
- package/apps/fabric/src/employees/employees.controller.ts +217 -0
- package/apps/fabric/src/employees/employees.module.ts +32 -0
- package/apps/fabric/src/employees/employees.service.ts +1333 -0
- package/apps/fabric/src/employees/employees.types.ts +69 -0
- package/apps/fabric/src/employees/errors/employee-deployment.errors.ts +80 -0
- package/apps/fabric/src/employees/runtime.store.ts +304 -0
- package/apps/fabric/src/environments/dto/environment.dto.ts +26 -0
- package/apps/fabric/src/environments/environments.controller.ts +48 -0
- package/apps/fabric/src/environments/environments.module.ts +11 -0
- package/apps/fabric/src/environments/environments.service.ts +92 -0
- package/apps/fabric/src/environments/environments.store.ts +146 -0
- package/apps/fabric/src/environments/environments.types.ts +11 -0
- package/apps/fabric/src/environments/errors/environment.errors.ts +14 -0
- package/apps/fabric/src/fabric-core.module.ts +48 -0
- package/apps/fabric/src/health/health.controller.ts +19 -0
- package/apps/fabric/src/health/health.module.ts +12 -0
- package/apps/fabric/src/health/provider-health.indicator.ts +33 -0
- package/apps/fabric/src/healthcheck/healthcheck.module.ts +24 -0
- package/apps/fabric/src/healthcheck/healthcheck.service.ts +494 -0
- package/apps/fabric/src/logs/logs.module.ts +10 -0
- package/apps/fabric/src/logs/logs.service.ts +149 -0
- package/apps/fabric/src/main.ts +57 -0
- package/apps/fabric/src/notifications/notifications.module.ts +23 -0
- package/apps/fabric/src/notifications/notifications.service.ts +141 -0
- package/apps/fabric/src/project-source/errors/project-source.errors.ts +8 -0
- package/apps/fabric/src/project-source/project-source.module.ts +8 -0
- package/apps/fabric/src/project-source/project-source.service.ts +75 -0
- package/apps/fabric/src/project-source/project-source.types.ts +5 -0
- package/apps/fabric/src/projects/dto/project.dto.ts +25 -0
- package/apps/fabric/src/projects/errors/project.errors.ts +17 -0
- package/apps/fabric/src/projects/projects-data.service.ts +60 -0
- package/apps/fabric/src/projects/projects.controller.ts +92 -0
- package/apps/fabric/src/projects/projects.module.ts +20 -0
- package/apps/fabric/src/projects/projects.service.ts +323 -0
- package/apps/fabric/src/projects/projects.store.ts +142 -0
- package/apps/fabric/src/projects/projects.types.ts +9 -0
- package/apps/fabric/src/providers/docker-provider.ts +141 -0
- package/apps/fabric/src/providers/eks-provider.ts +279 -0
- package/apps/fabric/src/providers/pm2-provider.ts +205 -0
- package/apps/fabric/src/providers/port-allocator.ts +57 -0
- package/apps/fabric/src/providers/provider-factory.ts +32 -0
- package/apps/fabric/src/providers/provider.interface.ts +50 -0
- package/apps/fabric/src/providers/providers.module.ts +26 -0
- package/apps/fabric/src/provisioners/cognito-provisioner.service.ts +168 -0
- package/apps/fabric/src/provisioners/postgres-provisioner.service.ts +124 -0
- package/apps/fabric/src/provisioners/provisioners.module.ts +40 -0
- package/apps/fabric/src/user-config/user-config.module.ts +9 -0
- package/apps/fabric/src/user-config/user-config.service.ts +54 -0
- package/apps/fabric/src/user-config/user-config.store.ts +120 -0
- package/apps/fabric/src/user-config/user-config.types.ts +11 -0
- package/apps/fabric/src/versions/versions.controller.ts +24 -0
- package/apps/fabric/src/versions/versions.module.ts +11 -0
- package/apps/fabric/src/versions/versions.service.ts +65 -0
- package/apps/fabric/src/versions/versions.store.ts +160 -0
- package/apps/fabric/src/versions/versions.types.ts +12 -0
- package/apps/fabric/src/watcher/deployment-watcher.module.ts +22 -0
- package/apps/fabric/src/watcher/deployment-watcher.service.ts +92 -0
- package/apps/fabric/src/watcher/pod-probe.service.ts +231 -0
- package/apps/fabric/tsconfig.app.json +8 -0
- package/apps/integrations/prisma/.integrations/integrations.sqlite +0 -0
- package/apps/integrations/prisma/prisma.config.ts +8 -0
- package/apps/integrations/prisma/prisma.sqlite.config.ts +8 -0
- package/apps/integrations/prisma/schema.postgres.prisma +27 -0
- package/apps/integrations/prisma/schema.sqlite.prisma +27 -0
- package/apps/integrations/scripts/prepare-db.cjs +92 -0
- package/apps/integrations/src/api/auth-flow.controller.ts +39 -0
- package/apps/integrations/src/api/connections.controller.ts +26 -0
- package/apps/integrations/src/api/health.controller.ts +34 -0
- package/apps/integrations/src/api/providers.controller.ts +60 -0
- package/apps/integrations/src/api/tenant-config.controller.ts +62 -0
- package/apps/integrations/src/app.module.ts +38 -0
- package/apps/integrations/src/config/configuration.ts +134 -0
- package/apps/integrations/src/generated/prisma-postgres/browser.ts +24 -0
- package/apps/integrations/src/generated/prisma-postgres/client.ts +46 -0
- package/apps/integrations/src/generated/prisma-postgres/commonInputTypes.ts +297 -0
- package/apps/integrations/src/generated/prisma-postgres/enums.ts +15 -0
- package/apps/integrations/src/generated/prisma-postgres/internal/class.ts +204 -0
- package/apps/integrations/src/generated/prisma-postgres/internal/prismaNamespace.ts +794 -0
- package/apps/integrations/src/generated/prisma-postgres/internal/prismaNamespaceBrowser.ts +126 -0
- package/apps/integrations/src/generated/prisma-postgres/models/IntegrationEntity.ts +1273 -0
- package/apps/integrations/src/generated/prisma-postgres/models.ts +12 -0
- package/apps/integrations/src/generated/prisma-sqlite/browser.ts +24 -0
- package/apps/integrations/src/generated/prisma-sqlite/client.ts +46 -0
- package/apps/integrations/src/generated/prisma-sqlite/commonInputTypes.ts +278 -0
- package/apps/integrations/src/generated/prisma-sqlite/enums.ts +15 -0
- package/apps/integrations/src/generated/prisma-sqlite/internal/class.ts +204 -0
- package/apps/integrations/src/generated/prisma-sqlite/internal/prismaNamespace.ts +770 -0
- package/apps/integrations/src/generated/prisma-sqlite/internal/prismaNamespaceBrowser.ts +123 -0
- package/apps/integrations/src/generated/prisma-sqlite/models/IntegrationEntity.ts +1271 -0
- package/apps/integrations/src/generated/prisma-sqlite/models.ts +12 -0
- package/apps/integrations/src/integrations-core.module.ts +52 -0
- package/apps/integrations/src/main.ts +63 -0
- package/apps/integrations/src/modules/auth-flow/auth-flow.module.ts +21 -0
- package/apps/integrations/src/modules/auth-flow/auth-flow.service.ts +275 -0
- package/apps/integrations/src/modules/auth-flow/dto/auth-flow.dto.ts +36 -0
- package/apps/integrations/src/modules/auth-flow/scheduler.service.ts +73 -0
- package/apps/integrations/src/modules/connections/connections.module.ts +11 -0
- package/apps/integrations/src/modules/connections/connections.service.ts +109 -0
- package/apps/integrations/src/modules/credentials/credentials.module.ts +11 -0
- package/apps/integrations/src/modules/credentials/encryption.service.ts +71 -0
- package/apps/integrations/src/modules/credentials/token-store.service.ts +82 -0
- package/apps/integrations/src/modules/discovery/discovery.module.ts +8 -0
- package/apps/integrations/src/modules/discovery/oidc-discovery.service.ts +114 -0
- package/apps/integrations/src/modules/platform/metadata-store.service.ts +337 -0
- package/apps/integrations/src/modules/platform/platform.module.ts +8 -0
- package/apps/integrations/src/modules/platform/platform.types.ts +10 -0
- package/apps/integrations/src/modules/platform/resilient-fetch.ts +53 -0
- package/apps/integrations/src/modules/providers/base/api-key.provider.ts +43 -0
- package/apps/integrations/src/modules/providers/base/basic-auth.provider.ts +44 -0
- package/apps/integrations/src/modules/providers/base/oauth2.provider.ts +233 -0
- package/apps/integrations/src/modules/providers/base/oidc.provider.ts +87 -0
- package/apps/integrations/src/modules/providers/builtin/google-drive.provider.ts +40 -0
- package/apps/integrations/src/modules/providers/dto/provider.dto.ts +53 -0
- package/apps/integrations/src/modules/providers/provider.factory.ts +68 -0
- package/apps/integrations/src/modules/providers/provider.registry.ts +123 -0
- package/apps/integrations/src/modules/providers/provider.types.ts +185 -0
- package/apps/integrations/src/modules/providers/providers.module.ts +34 -0
- package/apps/integrations/src/modules/tenant-config/dto/tenant-config.dto.ts +28 -0
- package/apps/integrations/src/modules/tenant-config/tenant-config.module.ts +11 -0
- package/apps/integrations/src/modules/tenant-config/tenant-config.service.ts +212 -0
- package/apps/integrations/tsconfig.app.json +8 -0
- package/apps/knowledge/prisma/prisma.config.ts +8 -0
- package/apps/knowledge/prisma/schema.postgres.prisma +29 -0
- package/apps/knowledge/prisma/schema.sqlite.prisma +29 -0
- package/apps/knowledge/scripts/prepare-db.cjs +72 -0
- package/apps/knowledge/src/api/auth.controller.ts +54 -0
- package/apps/knowledge/src/api/connections.controller.ts +99 -0
- package/apps/knowledge/src/api/documents.controller.ts +45 -0
- package/apps/knowledge/src/api/graph.controller.ts +67 -0
- package/apps/knowledge/src/api/health.controller.ts +39 -0
- package/apps/knowledge/src/api/ingestion.controller.ts +45 -0
- package/apps/knowledge/src/api/knowledge-query.controller.ts +27 -0
- package/apps/knowledge/src/api/oauth.controller.ts +38 -0
- package/apps/knowledge/src/api/recommendations.controller.ts +63 -0
- package/apps/knowledge/src/api/scheduler.controller.ts +32 -0
- package/apps/knowledge/src/api/topics.controller.ts +46 -0
- package/apps/knowledge/src/app.module.ts +68 -0
- package/apps/knowledge/src/config/configuration.ts +149 -0
- package/apps/knowledge/src/generated/prisma-postgres/browser.ts +24 -0
- package/apps/knowledge/src/generated/prisma-postgres/client.ts +46 -0
- package/apps/knowledge/src/generated/prisma-postgres/commonInputTypes.ts +297 -0
- package/apps/knowledge/src/generated/prisma-postgres/enums.ts +15 -0
- package/apps/knowledge/src/generated/prisma-postgres/internal/class.ts +204 -0
- package/apps/knowledge/src/generated/prisma-postgres/internal/prismaNamespace.ts +795 -0
- package/apps/knowledge/src/generated/prisma-postgres/internal/prismaNamespaceBrowser.ts +127 -0
- package/apps/knowledge/src/generated/prisma-postgres/models/KnowledgeEntity.ts +1301 -0
- package/apps/knowledge/src/generated/prisma-postgres/models.ts +12 -0
- package/apps/knowledge/src/generated/prisma-sqlite/browser.ts +24 -0
- package/apps/knowledge/src/generated/prisma-sqlite/client.ts +46 -0
- package/apps/knowledge/src/generated/prisma-sqlite/commonInputTypes.ts +278 -0
- package/apps/knowledge/src/generated/prisma-sqlite/enums.ts +15 -0
- package/apps/knowledge/src/generated/prisma-sqlite/internal/class.ts +204 -0
- package/apps/knowledge/src/generated/prisma-sqlite/internal/prismaNamespace.ts +771 -0
- package/apps/knowledge/src/generated/prisma-sqlite/internal/prismaNamespaceBrowser.ts +124 -0
- package/apps/knowledge/src/generated/prisma-sqlite/models/KnowledgeEntity.ts +1299 -0
- package/apps/knowledge/src/generated/prisma-sqlite/models.ts +12 -0
- package/apps/knowledge/src/main.ts +48 -0
- package/apps/knowledge/src/modules/auth/auth.module.ts +8 -0
- package/apps/knowledge/src/modules/auth/auth.service.ts +290 -0
- package/apps/knowledge/src/modules/documents/documents.module.ts +12 -0
- package/apps/knowledge/src/modules/documents/documents.service.ts +179 -0
- package/apps/knowledge/src/modules/documents/dto/document.dto.ts +68 -0
- package/apps/knowledge/src/modules/graph/graph.module.ts +10 -0
- package/apps/knowledge/src/modules/graph/graph.service.ts +223 -0
- package/apps/knowledge/src/modules/ingestion/dto/ingestion.dto.ts +58 -0
- package/apps/knowledge/src/modules/ingestion/ingestion-workflows.service.ts +246 -0
- package/apps/knowledge/src/modules/ingestion/ingestion.module.ts +14 -0
- package/apps/knowledge/src/modules/ingestion/ingestion.service.ts +619 -0
- package/apps/knowledge/src/modules/oauth-bridge/oauth-bridge.module.ts +8 -0
- package/apps/knowledge/src/modules/oauth-bridge/oauth-bridge.service.ts +135 -0
- package/apps/knowledge/src/modules/platform/metadata-store.service.ts +314 -0
- package/apps/knowledge/src/modules/platform/platform.module.ts +9 -0
- package/apps/knowledge/src/modules/platform/platform.types.ts +23 -0
- package/apps/knowledge/src/modules/platform/vector-store.service.ts +235 -0
- package/apps/knowledge/src/modules/recommendations/recommendations.module.ts +10 -0
- package/apps/knowledge/src/modules/recommendations/recommendations.service.ts +121 -0
- package/apps/knowledge/src/modules/storage/storage.module.ts +8 -0
- package/apps/knowledge/src/modules/storage/storage.service.ts +150 -0
- package/apps/knowledge/src/modules/sync/scheduler.service.ts +175 -0
- package/apps/knowledge/src/modules/sync/sync.module.ts +11 -0
- package/apps/knowledge/src/modules/topics/dto/topic.dto.ts +50 -0
- package/apps/knowledge/src/modules/topics/topics.module.ts +10 -0
- package/apps/knowledge/src/modules/topics/topics.service.ts +86 -0
- package/apps/knowledge/tsconfig.app.json +8 -0
- package/apps/monolith/src/app.module.ts +75 -0
- package/apps/monolith/src/config/configuration.ts +93 -0
- package/apps/monolith/src/health/health.controller.ts +22 -0
- package/apps/monolith/src/health/health.module.ts +9 -0
- package/apps/monolith/src/main.ts +56 -0
- package/apps/monolith/tsconfig.app.json +8 -0
- package/libs/agentcraft/src/agentcraft.module.d.ts +12 -0
- package/libs/agentcraft/src/agentcraft.module.js +142 -0
- package/libs/agentcraft/src/agentcraft.module.js.map +1 -0
- package/libs/agentcraft/src/agentcraft.module.ts +161 -0
- package/libs/agentcraft/src/constants.d.ts +2 -0
- package/libs/agentcraft/src/constants.js +6 -0
- package/libs/agentcraft/src/constants.js.map +1 -0
- package/libs/agentcraft/src/constants.ts +2 -0
- package/libs/agentcraft/src/dto/submit-action.dto.ts +20 -0
- package/libs/agentcraft/src/errors/domain.error.d.ts +8 -0
- package/libs/agentcraft/src/errors/domain.error.js +27 -0
- package/libs/agentcraft/src/errors/domain.error.js.map +1 -0
- package/libs/agentcraft/src/errors/domain.error.ts +14 -0
- package/libs/agentcraft/src/errors/llm.errors.d.ts +10 -0
- package/libs/agentcraft/src/errors/llm.errors.js +26 -0
- package/libs/agentcraft/src/errors/llm.errors.js.map +1 -0
- package/libs/agentcraft/src/errors/llm.errors.ts +19 -0
- package/libs/agentcraft/src/index.d.ts +64 -0
- package/libs/agentcraft/src/index.js +195 -0
- package/libs/agentcraft/src/index.js.map +1 -0
- package/libs/agentcraft/src/index.ts +171 -0
- package/libs/agentcraft/src/instructions/step6-data-model.ts +181 -0
- package/libs/agentcraft/src/instructions/step7-workflows.ts +544 -0
- package/libs/agentcraft/src/instructions/step8-agents.ts +114 -0
- package/libs/agentcraft/src/interfaces/llm-provider.interface.d.ts +16 -0
- package/libs/agentcraft/src/interfaces/llm-provider.interface.js +3 -0
- package/libs/agentcraft/src/interfaces/llm-provider.interface.js.map +1 -0
- package/libs/agentcraft/src/interfaces/llm-provider.interface.ts +18 -0
- package/libs/agentcraft/src/prompts/assembly.prompt.ts +104 -0
- package/libs/agentcraft/src/prompts/codegen.prompt.ts +162 -0
- package/libs/agentcraft/src/prompts/decomposition.prompt.ts +32 -0
- package/libs/agentcraft/src/prompts/examples-provider.ts +63 -0
- package/libs/agentcraft/src/prompts/planning.prompt.ts +52 -0
- package/libs/agentcraft/src/prompts/resolver.prompt.ts +364 -0
- package/libs/agentcraft/src/prompts/semantics-guide.ts +134 -0
- package/libs/agentcraft/src/services/anthropic-craft-llm.service.ts +186 -0
- package/libs/agentcraft/src/services/anthropic-llm.service.d.ts +9 -0
- package/libs/agentcraft/src/services/anthropic-llm.service.js +90 -0
- package/libs/agentcraft/src/services/anthropic-llm.service.js.map +1 -0
- package/libs/agentcraft/src/services/anthropic-llm.service.ts +61 -0
- package/libs/agentcraft/src/services/api-adapter.service.d.ts +65 -0
- package/libs/agentcraft/src/services/api-adapter.service.js +167 -0
- package/libs/agentcraft/src/services/api-adapter.service.js.map +1 -0
- package/libs/agentcraft/src/services/api-adapter.service.ts +186 -0
- package/libs/agentcraft/src/services/architect-loop.service.d.ts +16 -0
- package/libs/agentcraft/src/services/architect-loop.service.js +92 -0
- package/libs/agentcraft/src/services/architect-loop.service.js.map +1 -0
- package/libs/agentcraft/src/services/architect-loop.service.ts +407 -0
- package/libs/agentcraft/src/services/autonomous-runner.service.ts +178 -0
- package/libs/agentcraft/src/services/craft-config.service.d.ts +11 -0
- package/libs/agentcraft/src/services/craft-config.service.js +127 -0
- package/libs/agentcraft/src/services/craft-config.service.js.map +1 -0
- package/libs/agentcraft/src/services/craft-config.service.ts +92 -0
- package/libs/agentcraft/src/services/craft-llm-adapter.service.d.ts +12 -0
- package/libs/agentcraft/src/services/craft-llm-adapter.service.js +73 -0
- package/libs/agentcraft/src/services/craft-llm-adapter.service.js.map +1 -0
- package/libs/agentcraft/src/services/craft-llm-adapter.service.ts +52 -0
- package/libs/agentcraft/src/services/decomposer.service.ts +115 -0
- package/libs/agentcraft/src/services/design-generator.service.ts +672 -0
- package/libs/agentcraft/src/services/executor.service.ts +452 -0
- package/libs/agentcraft/src/services/kb-watcher.service.ts +150 -0
- package/libs/agentcraft/src/services/openai-llm.service.d.ts +9 -0
- package/libs/agentcraft/src/services/openai-llm.service.js +91 -0
- package/libs/agentcraft/src/services/openai-llm.service.js.map +1 -0
- package/libs/agentcraft/src/services/openai-llm.service.ts +61 -0
- package/libs/agentcraft/src/services/output-parser.service.d.ts +9 -0
- package/libs/agentcraft/src/services/output-parser.service.js +80 -0
- package/libs/agentcraft/src/services/output-parser.service.js.map +1 -0
- package/libs/agentcraft/src/services/output-parser.service.ts +68 -0
- package/libs/agentcraft/src/services/pipeline-state.service.d.ts +19 -0
- package/libs/agentcraft/src/services/pipeline-state.service.js +134 -0
- package/libs/agentcraft/src/services/pipeline-state.service.js.map +1 -0
- package/libs/agentcraft/src/services/pipeline-state.service.ts +108 -0
- package/libs/agentcraft/src/services/pipeline-steps.service.d.ts +10 -0
- package/libs/agentcraft/src/services/pipeline-steps.service.js +147 -0
- package/libs/agentcraft/src/services/pipeline-steps.service.js.map +1 -0
- package/libs/agentcraft/src/services/pipeline-steps.service.ts +126 -0
- package/libs/agentcraft/src/services/planner.service.ts +189 -0
- package/libs/agentcraft/src/services/readme-generator.service.ts +229 -0
- package/libs/agentcraft/src/services/registry-client.service.d.ts +14 -0
- package/libs/agentcraft/src/services/registry-client.service.js +158 -0
- package/libs/agentcraft/src/services/registry-client.service.js.map +1 -0
- package/libs/agentcraft/src/services/registry-client.service.ts +139 -0
- package/libs/agentcraft/src/services/requirements-generator.service.ts +238 -0
- package/libs/agentcraft/src/services/spec-fetcher.service.d.ts +3 -0
- package/libs/agentcraft/src/services/spec-fetcher.service.js +46 -0
- package/libs/agentcraft/src/services/spec-fetcher.service.js.map +1 -0
- package/libs/agentcraft/src/services/spec-fetcher.service.ts +21 -0
- package/libs/agentcraft/src/services/ui-blueprint-generator.service.ts +387 -0
- package/libs/agentcraft/src/types/architect.types.ts +156 -0
- package/libs/agentcraft/src/types/autonomous.types.ts +71 -0
- package/libs/agentcraft/src/types/llm.types.d.ts +26 -0
- package/libs/agentcraft/src/types/llm.types.js +3 -0
- package/libs/agentcraft/src/types/llm.types.js.map +1 -0
- package/libs/agentcraft/src/types/llm.types.ts +60 -0
- package/libs/agentcraft/src/types/pipeline.types.d.ts +55 -0
- package/libs/agentcraft/src/types/pipeline.types.js +37 -0
- package/libs/agentcraft/src/types/pipeline.types.js.map +1 -0
- package/libs/agentcraft/src/types/pipeline.types.ts +61 -0
- package/libs/agentcraft/src/types/resolver.types.d.ts +27 -0
- package/libs/agentcraft/src/types/resolver.types.js +3 -0
- package/libs/agentcraft/src/types/resolver.types.js.map +1 -0
- package/libs/agentcraft/src/types/resolver.types.ts +22 -0
- package/libs/agentcraft/src/types/signal.types.d.ts +16 -0
- package/libs/agentcraft/src/types/signal.types.js +3 -0
- package/libs/agentcraft/src/types/signal.types.js.map +1 -0
- package/libs/agentcraft/src/types/signal.types.ts +18 -0
- package/libs/agentcraft/src/utils/kb-loader.ts +67 -0
- package/libs/agentcraft/src/utils/output-writer.ts +71 -0
- package/libs/agentcraft/src/utils/spec-fetcher.ts +20 -0
- package/libs/agentcraft/src/utils/spec-trimmer.ts +280 -0
- package/libs/agentcraft/src/utils/text-utils.ts +49 -0
- package/libs/agentcraft/src/utils/validator.ts +231 -0
- package/libs/agentcraft/tsconfig.lib.json +9 -0
- package/libs/auth/src/auth.guard.ts +188 -0
- package/libs/auth/src/auth.module.ts +11 -0
- package/libs/auth/src/index.ts +3 -0
- package/libs/auth/src/public.decorator.ts +5 -0
- package/libs/auth/tsconfig.lib.json +9 -0
- package/libs/common/src/database/data-source.service.ts +75 -0
- package/libs/common/src/database/database-connection.holder.ts +41 -0
- package/libs/common/src/database/database.config.ts +14 -0
- package/libs/common/src/database/database.module.ts +42 -0
- package/libs/common/src/database/embedded-database.service.ts +120 -0
- package/libs/common/src/database/file-store.factory.ts +20 -0
- package/libs/common/src/database/file-store.ts +47 -0
- package/libs/common/src/errors/domain.error.ts +11 -0
- package/libs/common/src/filters/domain-exception.filter.ts +62 -0
- package/libs/common/src/index.ts +15 -0
- package/libs/common/src/interceptors/logging.interceptor.ts +60 -0
- package/libs/common/tsconfig.lib.json +9 -0
- package/libs/project-provider/src/constants.d.ts +7 -0
- package/libs/project-provider/src/constants.js +18 -0
- package/libs/project-provider/src/constants.js.map +1 -0
- package/libs/project-provider/src/constants.ts +7 -0
- package/libs/project-provider/src/errors/domain.error.d.ts +4 -0
- package/libs/project-provider/src/errors/domain.error.js +13 -0
- package/libs/project-provider/src/errors/domain.error.js.map +1 -0
- package/libs/project-provider/src/errors/domain.error.ts +1 -0
- package/libs/project-provider/src/errors/employee-code.errors.d.ts +13 -0
- package/libs/project-provider/src/errors/employee-code.errors.js +42 -0
- package/libs/project-provider/src/errors/employee-code.errors.js.map +1 -0
- package/libs/project-provider/src/errors/employee-code.errors.ts +37 -0
- package/libs/project-provider/src/errors/inbox.errors.d.ts +13 -0
- package/libs/project-provider/src/errors/inbox.errors.js +36 -0
- package/libs/project-provider/src/errors/inbox.errors.js.map +1 -0
- package/libs/project-provider/src/errors/inbox.errors.ts +31 -0
- package/libs/project-provider/src/errors/registry.errors.d.ts +16 -0
- package/libs/project-provider/src/errors/registry.errors.js +46 -0
- package/libs/project-provider/src/errors/registry.errors.js.map +1 -0
- package/libs/project-provider/src/errors/registry.errors.ts +40 -0
- package/libs/project-provider/src/index.d.ts +67 -0
- package/libs/project-provider/src/index.js +220 -0
- package/libs/project-provider/src/index.js.map +1 -0
- package/libs/project-provider/src/index.ts +82 -0
- package/libs/project-provider/src/interfaces/employee-code-provider.interface.d.ts +36 -0
- package/libs/project-provider/src/interfaces/employee-code-provider.interface.js +3 -0
- package/libs/project-provider/src/interfaces/employee-code-provider.interface.js.map +1 -0
- package/libs/project-provider/src/interfaces/employee-code-provider.interface.ts +37 -0
- package/libs/project-provider/src/interfaces/inbox-client.interface.d.ts +3 -0
- package/libs/project-provider/src/interfaces/inbox-client.interface.js +3 -0
- package/libs/project-provider/src/interfaces/inbox-client.interface.js.map +1 -0
- package/libs/project-provider/src/interfaces/inbox-client.interface.ts +3 -0
- package/libs/project-provider/src/interfaces/inbox-handler.interface.ts +10 -0
- package/libs/project-provider/src/interfaces/knowledge-base.interface.d.ts +5 -0
- package/libs/project-provider/src/interfaces/knowledge-base.interface.js +3 -0
- package/libs/project-provider/src/interfaces/knowledge-base.interface.js.map +1 -0
- package/libs/project-provider/src/interfaces/knowledge-base.interface.ts +6 -0
- package/libs/project-provider/src/interfaces/registry-provider.interface.d.ts +24 -0
- package/libs/project-provider/src/interfaces/registry-provider.interface.js +3 -0
- package/libs/project-provider/src/interfaces/registry-provider.interface.js.map +1 -0
- package/libs/project-provider/src/interfaces/registry-provider.interface.ts +36 -0
- package/libs/project-provider/src/project-provider.module.d.ts +10 -0
- package/libs/project-provider/src/project-provider.module.js +137 -0
- package/libs/project-provider/src/project-provider.module.js.map +1 -0
- package/libs/project-provider/src/project-provider.module.ts +119 -0
- package/libs/project-provider/src/services/al-parser.service.d.ts +6 -0
- package/libs/project-provider/src/services/al-parser.service.js +103 -0
- package/libs/project-provider/src/services/al-parser.service.js.map +1 -0
- package/libs/project-provider/src/services/al-parser.service.ts +92 -0
- package/libs/project-provider/src/services/direct-inbox-client.service.ts +24 -0
- package/libs/project-provider/src/services/file-knowledge-base.service.d.ts +8 -0
- package/libs/project-provider/src/services/file-knowledge-base.service.js +158 -0
- package/libs/project-provider/src/services/file-knowledge-base.service.js.map +1 -0
- package/libs/project-provider/src/services/file-knowledge-base.service.ts +81 -0
- package/libs/project-provider/src/services/filesystem-employee-code.service.d.ts +49 -0
- package/libs/project-provider/src/services/filesystem-employee-code.service.js +227 -0
- package/libs/project-provider/src/services/filesystem-employee-code.service.js.map +1 -0
- package/libs/project-provider/src/services/filesystem-employee-code.service.ts +220 -0
- package/libs/project-provider/src/services/filesystem-registry.service.d.ts +34 -0
- package/libs/project-provider/src/services/filesystem-registry.service.js +231 -0
- package/libs/project-provider/src/services/filesystem-registry.service.js.map +1 -0
- package/libs/project-provider/src/services/filesystem-registry.service.ts +271 -0
- package/libs/project-provider/src/services/http-inbox-client.service.d.ts +7 -0
- package/libs/project-provider/src/services/http-inbox-client.service.js +66 -0
- package/libs/project-provider/src/services/http-inbox-client.service.js.map +1 -0
- package/libs/project-provider/src/services/http-inbox-client.service.ts +29 -0
- package/libs/project-provider/src/services/noop-inbox-client.service.d.ts +4 -0
- package/libs/project-provider/src/services/noop-inbox-client.service.js +32 -0
- package/libs/project-provider/src/services/noop-inbox-client.service.js.map +1 -0
- package/libs/project-provider/src/services/noop-inbox-client.service.ts +9 -0
- package/libs/project-provider/src/types/messaging.types.d.ts +49 -0
- package/libs/project-provider/src/types/messaging.types.js +11 -0
- package/libs/project-provider/src/types/messaging.types.js.map +1 -0
- package/libs/project-provider/src/types/messaging.types.ts +59 -0
- package/libs/project-provider/src/types/project.types.d.ts +44 -0
- package/libs/project-provider/src/types/project.types.js +3 -0
- package/libs/project-provider/src/types/project.types.js.map +1 -0
- package/libs/project-provider/src/types/project.types.ts +58 -0
- package/libs/project-provider/tsconfig.lib.json +9 -0
- package/nest-cli.json +114 -0
- package/package.json +206 -0
- package/scripts/agentmanager.mjs +1126 -0
- package/scripts/monolith-daemon-run.mjs +77 -0
- package/scripts/monolith-daemon-run.sh +27 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { parseAssemblyOutput } from '../utils/output-writer';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a resolver name to a camelCase import alias.
|
|
5
|
+
* e.g. "github" -> "githubResolver", "github-issues" -> "githubIssuesResolver"
|
|
6
|
+
*/
|
|
7
|
+
function toResolverAlias(name: string): string {
|
|
8
|
+
const camelCase = name.replace(/-([a-z])/g, (_, c: string) =>
|
|
9
|
+
c.toUpperCase(),
|
|
10
|
+
);
|
|
11
|
+
return camelCase + 'Resolver';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Builds the system prompt for resolver generation.
|
|
16
|
+
*/
|
|
17
|
+
export function buildResolverSystemPrompt(
|
|
18
|
+
name: string,
|
|
19
|
+
description: string,
|
|
20
|
+
specContent: string,
|
|
21
|
+
): string {
|
|
22
|
+
const filePrefix = name.replace(/-/g, '_') + '_resolver';
|
|
23
|
+
const jsFileName = filePrefix + '.js';
|
|
24
|
+
const alFileName = filePrefix + '.al';
|
|
25
|
+
const importAlias = toResolverAlias(name);
|
|
26
|
+
|
|
27
|
+
return `You are a resolver generator for agentlang applications. Your task is to generate a complete resolver package that allows agentlang entities to interact with an external API.
|
|
28
|
+
|
|
29
|
+
## Resolver Name
|
|
30
|
+
${name}
|
|
31
|
+
|
|
32
|
+
## Description
|
|
33
|
+
${description}
|
|
34
|
+
|
|
35
|
+
## API Specification
|
|
36
|
+
<api-spec>
|
|
37
|
+
${specContent}
|
|
38
|
+
</api-spec>
|
|
39
|
+
|
|
40
|
+
## Agentlang Resolver Syntax Reference
|
|
41
|
+
|
|
42
|
+
### Entity Definition (.al file)
|
|
43
|
+
Entities are defined using the \`entity\` keyword inside a \`module\` block. Each entity has typed attributes and a \`resolver\` block that maps CRUD operations to functions imported from a JavaScript file. Each resolver block MUST have a unique name (NOT the entity name itself) and a **paths vector** \`[ModuleName/EntityName]\` that specifies which entity it resolves.
|
|
44
|
+
|
|
45
|
+
Attribute types: \`String\`, \`Int\`, \`Float\`, \`Boolean\`, \`UUID\`, \`DateTime\`, \`Map\`
|
|
46
|
+
Attribute properties: \`@id\` (primary key), \`@unique\`, \`@optional\`, \`@default(value)\`
|
|
47
|
+
|
|
48
|
+
Attributes may also be declared as arrays, e.g String[].
|
|
49
|
+
|
|
50
|
+
**Structures with no attributes MUST be defined with \`{}\`**. For example: \`@public event getAllPets {}\`, \`record EmptyPayload {}\`. Never omit the braces.
|
|
51
|
+
|
|
52
|
+
**IMPORTANT — Resolver binding syntax:**
|
|
53
|
+
|
|
54
|
+
The .al file MUST import the JavaScript file using an \`import\` statement with \`@as\` to create an alias. Resolver methods are then bound using \`alias.functionName\` (dot notation, NO quotes).
|
|
55
|
+
|
|
56
|
+
\`\`\`
|
|
57
|
+
module Resolvers.GitHub
|
|
58
|
+
|
|
59
|
+
import "github_resolver.js" @as githubResolver
|
|
60
|
+
|
|
61
|
+
entity Issue {
|
|
62
|
+
id Int @id,
|
|
63
|
+
title String,
|
|
64
|
+
body String @optional,
|
|
65
|
+
state String @optional,
|
|
66
|
+
number Int @optional,
|
|
67
|
+
repo String,
|
|
68
|
+
owner String
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
resolver IssueResolver [Resolvers.GitHub/Issue] {
|
|
72
|
+
create githubResolver.createIssue,
|
|
73
|
+
query githubResolver.queryIssue,
|
|
74
|
+
update githubResolver.updateIssue,
|
|
75
|
+
delete githubResolver.deleteIssue
|
|
76
|
+
}
|
|
77
|
+
\`\`\`
|
|
78
|
+
|
|
79
|
+
CRITICAL syntax rules:
|
|
80
|
+
- Do NOT use the string-path format like \`"resolver.js/functionName"\`. That is INVALID. Always use \`import "file.js" @as alias\` at the top of the module, then \`alias.functionName\` in resolver blocks.
|
|
81
|
+
- The resolver name MUST be a unique identifier (e.g. \`IssueResolver\`), NOT the entity name itself. \`resolver Issue { ... }\` is WRONG — use \`resolver IssueResolver [GitHub/Issue] { ... }\`.
|
|
82
|
+
- The paths vector \`[ModuleName/EntityName]\` is REQUIRED. It tells agentlang which entity this resolver handles. The ModuleName is the last segment of the module name (e.g. for \`module Resolvers.GitHub\`, use \`GitHub/Issue\`).
|
|
83
|
+
- Resolver method bindings MUST be separated by commas.
|
|
84
|
+
|
|
85
|
+
### Resolver JS Implementation (${jsFileName})
|
|
86
|
+
The resolver JS file exports functions for each CRUD operation. Each function receives an \`attributes\` object.
|
|
87
|
+
|
|
88
|
+
- Use \`instance.attributes.get("fieldName")\` to read entity attribute values
|
|
89
|
+
- Use \`instance.queryAttributes.get("fieldName")\` to get values to query entity instances
|
|
90
|
+
- Use \`process.env.ENV_VAR_NAME\` to read environment variables (for auth tokens, API keys, etc.). Do NOT import \`getLocalEnv\` or similar abstractions from agentlang.
|
|
91
|
+
- Use the global \`fetch\` API for HTTP calls
|
|
92
|
+
- Return result objects matching the entity shape
|
|
93
|
+
- Throw errors with descriptive messages on failure
|
|
94
|
+
|
|
95
|
+
\`\`\`javascript
|
|
96
|
+
async function createIssue(_, instance) { // the first argument '_' is always required for resolver methods
|
|
97
|
+
const token = process.env.GITHUB_TOKEN;
|
|
98
|
+
const owner = instance.attributes.get("owner");
|
|
99
|
+
const repo = instance.attributes.get("repo");
|
|
100
|
+
const title = instance.attributes.get("title");
|
|
101
|
+
const body = instance.attributes.get("body");
|
|
102
|
+
|
|
103
|
+
const response = await fetch(
|
|
104
|
+
\`https://api.github.com/repos/\${owner}/\${repo}/issues\`,
|
|
105
|
+
{
|
|
106
|
+
method: "POST",
|
|
107
|
+
headers: {
|
|
108
|
+
"Authorization": \`Bearer \${token}\`,
|
|
109
|
+
"Accept": "application/vnd.github+json",
|
|
110
|
+
"Content-Type": "application/json"
|
|
111
|
+
},
|
|
112
|
+
body: JSON.stringify({ title, body })
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
const error = await response.text();
|
|
118
|
+
throw new Error(\`GitHub API error: \${response.status} \${error}\`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const issue = await response.json();
|
|
122
|
+
return {
|
|
123
|
+
id: issue.id,
|
|
124
|
+
number: issue.number,
|
|
125
|
+
title: issue.title,
|
|
126
|
+
body: issue.body,
|
|
127
|
+
state: issue.state,
|
|
128
|
+
repo,
|
|
129
|
+
owner
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function queryIssue(_, instance) {
|
|
134
|
+
const token = process.env.GITHUB_TOKEN;
|
|
135
|
+
const owner = instance.queryAttributes.get("owner");
|
|
136
|
+
const repo = instance.queryAttributes.get("repo");
|
|
137
|
+
|
|
138
|
+
const response = await fetch(
|
|
139
|
+
\`https://api.github.com/repos/\${owner}/\${repo}/issues\`,
|
|
140
|
+
{
|
|
141
|
+
headers: {
|
|
142
|
+
"Authorization": \`Bearer \${token}\`,
|
|
143
|
+
"Accept": "application/vnd.github+json"
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
const error = await response.text();
|
|
150
|
+
throw new Error(\`GitHub API error: \${response.status} \${error}\`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const issues = await response.json();
|
|
154
|
+
return issues.map(issue => ({
|
|
155
|
+
id: issue.id,
|
|
156
|
+
number: issue.number,
|
|
157
|
+
title: issue.title,
|
|
158
|
+
body: issue.body,
|
|
159
|
+
state: issue.state,
|
|
160
|
+
repo,
|
|
161
|
+
owner
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function updateIssue(_, instance) {
|
|
166
|
+
const token = process.env.GITHUB_TOKEN;
|
|
167
|
+
const owner = instance.attributes.get("owner");
|
|
168
|
+
const repo = instance.attributes.get("repo");
|
|
169
|
+
const number = instance.attributes.get("number");
|
|
170
|
+
const title = instance.attributes.get("title");
|
|
171
|
+
const body = instance.attributes.get("body");
|
|
172
|
+
const state = instance.attributes.get("state");
|
|
173
|
+
|
|
174
|
+
const updates = {};
|
|
175
|
+
if (title) updates.title = title;
|
|
176
|
+
if (body) updates.body = body;
|
|
177
|
+
if (state) updates.state = state;
|
|
178
|
+
|
|
179
|
+
const response = await fetch(
|
|
180
|
+
\`https://api.github.com/repos/\${owner}/\${repo}/issues/\${number}\`,
|
|
181
|
+
{
|
|
182
|
+
method: "PATCH",
|
|
183
|
+
headers: {
|
|
184
|
+
"Authorization": \`Bearer \${token}\`,
|
|
185
|
+
"Accept": "application/vnd.github+json",
|
|
186
|
+
"Content-Type": "application/json"
|
|
187
|
+
},
|
|
188
|
+
body: JSON.stringify(updates)
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
if (!response.ok) {
|
|
193
|
+
const error = await response.text();
|
|
194
|
+
throw new Error(\`GitHub API error: \${response.status} \${error}\`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const issue = await response.json();
|
|
198
|
+
return {
|
|
199
|
+
id: issue.id,
|
|
200
|
+
number: issue.number,
|
|
201
|
+
title: issue.title,
|
|
202
|
+
body: issue.body,
|
|
203
|
+
state: issue.state,
|
|
204
|
+
repo,
|
|
205
|
+
owner
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function deleteIssue(_, instance) {
|
|
210
|
+
const token = process.env.GITHUB_TOKEN;
|
|
211
|
+
const owner = instance.attributes.get("owner");
|
|
212
|
+
const repo = instance.attributes.get("repo");
|
|
213
|
+
const number = instance.attributes.get("number");
|
|
214
|
+
|
|
215
|
+
// GitHub API doesn't support deleting issues, so we close it instead
|
|
216
|
+
const response = await fetch(
|
|
217
|
+
\`https://api.github.com/repos/\${owner}/\${repo}/issues/\${number}\`,
|
|
218
|
+
{
|
|
219
|
+
method: "PATCH",
|
|
220
|
+
headers: {
|
|
221
|
+
"Authorization": \`Bearer \${token}\`,
|
|
222
|
+
"Accept": "application/vnd.github+json",
|
|
223
|
+
"Content-Type": "application/json"
|
|
224
|
+
},
|
|
225
|
+
body: JSON.stringify({ state: "closed" })
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
if (!response.ok) {
|
|
230
|
+
const error = await response.text();
|
|
231
|
+
throw new Error(\`GitHub API error: \${response.status} \${error}\`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return { id: 0, title: "", repo, owner };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export { createIssue, queryIssue, updateIssue, deleteIssue };
|
|
238
|
+
\`\`\`
|
|
239
|
+
|
|
240
|
+
### package.json
|
|
241
|
+
\`\`\`json
|
|
242
|
+
{
|
|
243
|
+
"name": "${name}-resolver",
|
|
244
|
+
"version": "0.1.0",
|
|
245
|
+
"type": "module"
|
|
246
|
+
}
|
|
247
|
+
\`\`\`
|
|
248
|
+
|
|
249
|
+
## Output Format
|
|
250
|
+
|
|
251
|
+
Output exactly three files using this delimiter format:
|
|
252
|
+
|
|
253
|
+
\`\`\`
|
|
254
|
+
--- FILE: src/${alFileName} ---
|
|
255
|
+
<module definition with import statement and entities with resolver mappings>
|
|
256
|
+
|
|
257
|
+
--- FILE: src/${jsFileName} ---
|
|
258
|
+
<JavaScript implementation with CRUD functions>
|
|
259
|
+
|
|
260
|
+
--- FILE: package.json ---
|
|
261
|
+
<package.json with name, version, type>
|
|
262
|
+
\`\`\`
|
|
263
|
+
|
|
264
|
+
## Reserved Words — Do NOT Use as Names
|
|
265
|
+
|
|
266
|
+
Agentlang reserves certain keywords. You MUST NOT use any of the following as entity names or attribute names. If your domain has a concept that matches a reserved word, rename it (e.g. \`Event\` → \`CalendarEvent\`, \`Flow\` → \`AppFlow\`, \`Agent\` → \`AIAgent\`, \`import\` → \`importItem\`, \`delete\` → \`isDeleted\`, \`record\` → \`entry\`).
|
|
267
|
+
|
|
268
|
+
**Definition keywords:** \`module\`, \`import\`, \`entity\`, \`event\`, \`record\`, \`relationship\`, \`workflow\`, \`agent\`, \`resolver\`, \`flow\`, \`decision\`, \`scenario\`, \`directive\`, \`glossaryEntry\`, \`eval\`
|
|
269
|
+
|
|
270
|
+
**Control flow and operators:** \`if\`, \`else\`, \`for\`, \`in\`, \`return\`, \`throw\`, \`await\`, \`delete\`, \`purge\`, \`case\`, \`not\`, \`true\`, \`false\`, \`or\`, \`and\`, \`like\`, \`between\`
|
|
271
|
+
|
|
272
|
+
**CRUD and schema keywords:** \`create\`, \`update\`, \`query\`, \`upsert\`, \`contains\`, \`extends\`, \`where\`, \`roles\`, \`allow\`, \`subscribe\`, \`backoff\`, \`attempts\`
|
|
273
|
+
|
|
274
|
+
## Guidelines
|
|
275
|
+
- Focus on the described functionality: "${description}"
|
|
276
|
+
- Handle authentication via environment variables using \`process.env\` (e.g. \`process.env.PETSTORE_API_KEY\`). Do NOT import \`getLocalEnv\` or similar abstractions from agentlang.
|
|
277
|
+
- Use the global \`fetch\` API for all HTTP requests
|
|
278
|
+
- Include proper error handling with descriptive error messages
|
|
279
|
+
- Map API responses back to entity attribute shapes
|
|
280
|
+
- Module name should follow the pattern \`Resolvers.<PascalCaseName>\`
|
|
281
|
+
- Each entity needs a matching resolver block with a unique resolver name (e.g. \`PetResolver\`, \`OrderResolver\`) and a paths vector \`[ModuleName/EntityName]\`. Resolver method bindings must be comma-separated
|
|
282
|
+
- Export all resolver functions from ${jsFileName}
|
|
283
|
+
- The .al file MUST start with \`import "${jsFileName}" @as ${importAlias}\` right after the module declaration
|
|
284
|
+
- Resolver methods MUST use \`${importAlias}.functionName\` (dot notation), NEVER \`"file/function"\` string syntax`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export interface PipelineResolverResult {
|
|
288
|
+
al: string;
|
|
289
|
+
js: string;
|
|
290
|
+
meta: string;
|
|
291
|
+
rawOutput: string;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Parses LLM output with `--- FILE: ---` delimiters into resolver content.
|
|
296
|
+
* Extracts .al and .js files, and builds metadata from the .al content.
|
|
297
|
+
*/
|
|
298
|
+
export function parseResolverOutput(
|
|
299
|
+
rawOutput: string,
|
|
300
|
+
): PipelineResolverResult | null {
|
|
301
|
+
const files = parseAssemblyOutput(rawOutput);
|
|
302
|
+
|
|
303
|
+
let al = '';
|
|
304
|
+
let js = '';
|
|
305
|
+
|
|
306
|
+
for (const file of files) {
|
|
307
|
+
if (file.filePath.endsWith('.al')) {
|
|
308
|
+
al = file.content;
|
|
309
|
+
} else if (file.filePath.endsWith('.js')) {
|
|
310
|
+
js = file.content;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (!al && !js) {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const meta = buildResolverMeta(al);
|
|
319
|
+
|
|
320
|
+
return { al, js, meta, rawOutput };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Builds structured metadata from .al content for LLM prompts.
|
|
325
|
+
* Extracts module name, entity names, and their attributes.
|
|
326
|
+
*/
|
|
327
|
+
export function buildResolverMeta(alContent: string): string {
|
|
328
|
+
const lines: string[] = ['# Resolver Metadata', ''];
|
|
329
|
+
|
|
330
|
+
// Extract module name
|
|
331
|
+
const moduleMatch = alContent.match(/^module\s+(\S+)/m);
|
|
332
|
+
if (moduleMatch) {
|
|
333
|
+
lines.push(`Module: ${moduleMatch[1]}`);
|
|
334
|
+
lines.push('');
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Extract entities and their attributes
|
|
338
|
+
const entityRe = /entity\s+(\w+)\s*\{([^}]*)\}/g;
|
|
339
|
+
let match;
|
|
340
|
+
while ((match = entityRe.exec(alContent)) !== null) {
|
|
341
|
+
const entityName = match[1];
|
|
342
|
+
const body = match[2];
|
|
343
|
+
lines.push(`## Entity: ${entityName}`);
|
|
344
|
+
|
|
345
|
+
// Extract attributes: name Type [properties]
|
|
346
|
+
const attrRe =
|
|
347
|
+
/(\w+)\s+(String|Int|Float|Boolean|UUID|DateTime|Map|Any|Number|Decimal|Email|Date|Time|URL|Password)(?:\[\])?\s*([^,\n]*)/g;
|
|
348
|
+
let attrMatch;
|
|
349
|
+
const attrs: string[] = [];
|
|
350
|
+
while ((attrMatch = attrRe.exec(body)) !== null) {
|
|
351
|
+
attrs.push(
|
|
352
|
+
`- ${attrMatch[1]}: ${attrMatch[2]}${attrMatch[3].trim() ? ' ' + attrMatch[3].trim() : ''}`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
if (attrs.length > 0) {
|
|
356
|
+
lines.push('Attributes:');
|
|
357
|
+
lines.push(...attrs);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
lines.push('');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return lines.join('\n').trim();
|
|
364
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agentlang semantics guide — a focused conceptual reference for the LLM
|
|
3
|
+
* during the design phase. Explains what each construct means, when to use
|
|
4
|
+
* it, and how constructs compose into a system.
|
|
5
|
+
*
|
|
6
|
+
* This is NOT a syntax reference — no code examples, no grammar rules.
|
|
7
|
+
* The goal is to teach the LLM to make correct architectural decisions.
|
|
8
|
+
*/
|
|
9
|
+
export const SEMANTICS_GUIDE = `## Agentlang Semantics Guide
|
|
10
|
+
|
|
11
|
+
Agentlang is a declarative language for building AI-powered applications. An application is a **module** containing entities, relationships, events, workflows, and agents. Understanding when to use each construct is critical for correct design.
|
|
12
|
+
|
|
13
|
+
### Entity
|
|
14
|
+
|
|
15
|
+
An entity is a **persistent, database-backed domain object**. Every noun in your domain that needs to be stored, queried, or referenced is an entity. Examples: User, Product, Order, Lead, Ticket.
|
|
16
|
+
|
|
17
|
+
At design time, define entities with a name and description only — attributes are added during code generation. Focus on identifying the right set of entities: too few leads to overloaded entities; too many leads to unnecessary complexity.
|
|
18
|
+
|
|
19
|
+
**When to define an entity:** Any domain concept that has its own identity and lifecycle, needs CRUD operations, or is referenced by other constructs.
|
|
20
|
+
|
|
21
|
+
**When NOT to define an entity:** Temporary data, computed values, or concepts that only exist within a single workflow step.
|
|
22
|
+
|
|
23
|
+
**Inheritance:** An entity can extend a record or another entity to inherit its attributes. Use when multiple entities share a common set of fields.
|
|
24
|
+
|
|
25
|
+
**Entity-level features** (important design decisions — mark which ones apply to each entity):
|
|
26
|
+
|
|
27
|
+
- **@meta {"audit": true}** — Enable audit trail. Use when you need to track who changed what and when (e.g., compliance-sensitive entities like Orders, Payments, MedicalRecords).
|
|
28
|
+
- **@meta {"fullTextSearch": "\*"}** — Enable full-text search. Use when users need to search entity instances by free-text queries (e.g., Tickets, Articles, Products).
|
|
29
|
+
- **@rbac** — Role-based access control at the entity level. Define which roles can create/read/update/delete, and optional row-level filters (e.g., "users can only read their own records"). Use when different user roles need different access levels.
|
|
30
|
+
- **@before/@after triggers** — Lifecycle hooks that fire before or after create/update/delete operations. Use when an entity change should automatically trigger side effects (e.g., after creating a Ticket, auto-assign it; before updating an Order, validate inventory). Triggers reference events/workflows by name and can be @async.
|
|
31
|
+
- **@with_unique(attr1, attr2)** — Composite unique constraint across multiple attributes. Use when no single attribute is unique but the combination must be (e.g., unique email per organization, unique document per version).
|
|
32
|
+
- **@actions** — Bind named actions to events on an entity. Use when an entity needs custom operations beyond standard CRUD.
|
|
33
|
+
- **@expr** — Computed attributes whose values are automatically derived from other fields. Use when a field's value can always be calculated from other attributes (e.g., totalPrice = quantity * unitPrice). User-provided values override the computed expression.
|
|
34
|
+
|
|
35
|
+
### Relationship
|
|
36
|
+
|
|
37
|
+
Relationships connect entities. There are exactly two kinds — choosing the right one is a critical design decision.
|
|
38
|
+
|
|
39
|
+
**contains (parent-child ownership):**
|
|
40
|
+
- The parent **owns** the child. The child's identity is scoped within the parent.
|
|
41
|
+
- Deleting the parent cascades to delete all children.
|
|
42
|
+
- The child has no meaning outside its parent.
|
|
43
|
+
- Use for: Orders containing OrderItems, Users containing Posts, Projects containing Tasks.
|
|
44
|
+
|
|
45
|
+
**between (cross-reference association):**
|
|
46
|
+
- Both entities have **independent lifecycles**. Neither owns the other.
|
|
47
|
+
- Deleting one side does NOT delete the other.
|
|
48
|
+
- Supports cardinality: one_one, one_many (default is many-to-many).
|
|
49
|
+
- Use for: Students enrolled in Courses, Employees assigned to Departments, Tags on Articles.
|
|
50
|
+
|
|
51
|
+
**Decision framework:** Ask "If I delete entity A, should entity B also be deleted?" If yes → contains. If no → between.
|
|
52
|
+
|
|
53
|
+
**Cardinality for between relationships:**
|
|
54
|
+
- one_one: Each A has exactly one B, and vice versa (e.g., User ↔ Profile).
|
|
55
|
+
- one_many: Each A has many Bs, but each B belongs to one A (e.g., Department → Employees).
|
|
56
|
+
- many_many (default): Each A can relate to many Bs and vice versa (e.g., Students ↔ Courses).
|
|
57
|
+
|
|
58
|
+
**Role aliases (@as):** When a relationship connects an entity to itself (self-referencing), use @as to distinguish the two roles. Example: Employee @as manager ↔ Employee @as report. Use when modeling hierarchies, mentorship, or any self-referential relationship.
|
|
59
|
+
|
|
60
|
+
**Relationship schemas:** A relationship can carry its own attributes (e.g., Enrollment between Student and Course might have enrolledOn and grade). Use when the association itself has data beyond just linking two entities.
|
|
61
|
+
|
|
62
|
+
### Event
|
|
63
|
+
|
|
64
|
+
An event is a **command or action** that triggers a workflow. It defines the input data schema for that action.
|
|
65
|
+
|
|
66
|
+
- @public events expose a REST API endpoint (POST /api/Module/EventName).
|
|
67
|
+
- Events can extend a record or another event to inherit attributes.
|
|
68
|
+
- Events represent user actions (SubmitOrder, ApproveApplication) or system triggers (ProcessPayment, SendNotification).
|
|
69
|
+
- Event names should clearly describe the action being performed.
|
|
70
|
+
|
|
71
|
+
**When to define an event:** Any action that initiates a process, changes state, or triggers side effects.
|
|
72
|
+
|
|
73
|
+
### Workflow
|
|
74
|
+
|
|
75
|
+
A workflow is the **procedural handler** for an event or a lifecycle trigger. It performs CRUD operations on entities, invokes agents, and orchestrates multi-step logic.
|
|
76
|
+
|
|
77
|
+
**Two forms:**
|
|
78
|
+
|
|
79
|
+
1. **Event-driven (named):** The workflow name matches the event name. When the event fires, the workflow executes.
|
|
80
|
+
2. **Lifecycle trigger:** Fires automatically before or after an entity's create, update, or delete operation. Use for automatic side effects (e.g., after creating a User, create a default Profile; before deleting an Order, check it's not shipped).
|
|
81
|
+
|
|
82
|
+
**Access control:** @withRole restricts who can invoke a workflow (e.g., only admins can run an approval workflow).
|
|
83
|
+
|
|
84
|
+
**What workflows can do:**
|
|
85
|
+
- **CRUD operations** — Create, query (with ? suffix), update, upsert, delete, and purge entity instances.
|
|
86
|
+
- **Sequential binding** — Chain operations with @as to bind results to variables for subsequent steps.
|
|
87
|
+
- **Control flow** — if/else conditionals and for-each loops over collections.
|
|
88
|
+
- **Error handling** — @catch for not_found and error cases; @empty for handling empty query results with fallback logic.
|
|
89
|
+
- **Side effects** — @then for post-operation logic (run additional statements after a create/update).
|
|
90
|
+
- **Joins and analytics** — Query across entities with @join/@left_join/@right_join/@full_join, project results with @into, filter with @where, aggregate with @count/@sum/@avg/@min/@max, group with @groupBy, sort with @orderBy, paginate with @limit/@offset, deduplicate with @distinct.
|
|
91
|
+
- **Upsert** — @upsert for create-or-update semantics.
|
|
92
|
+
- **Full-text search** — Query entities using free-text when @meta fullTextSearch is enabled.
|
|
93
|
+
- **Return and throw** — Return results or throw errors to abort with a message.
|
|
94
|
+
|
|
95
|
+
**When to define a workflow:** Every event needs a corresponding workflow. Lifecycle triggers are for automatic side effects on entity operations. The workflow describes WHAT happens — which entities are read, created, updated, or deleted.
|
|
96
|
+
|
|
97
|
+
### Agent
|
|
98
|
+
|
|
99
|
+
An agent is an **AI-powered component** using an LLM. Agents understand natural language, make decisions, classify data, generate content, and interact with entities via tools.
|
|
100
|
+
|
|
101
|
+
- Tools are the entities, events, and workflows an agent can call.
|
|
102
|
+
- @public agents expose a REST chat endpoint.
|
|
103
|
+
- Each agent should have a focused role — one agent per domain area.
|
|
104
|
+
|
|
105
|
+
**When to define an agent:** Any task requiring natural language understanding, classification, content generation, or intelligent decision-making. Agents are NOT needed for simple CRUD — workflows handle that directly.
|
|
106
|
+
|
|
107
|
+
**Agent goal:** Each agent should have a clear high-level objective explaining its purpose in the system. The goal answers "why does this agent exist and what value does it provide?"
|
|
108
|
+
|
|
109
|
+
**Key agent capabilities** (design decisions — note which ones each agent needs):
|
|
110
|
+
|
|
111
|
+
- **responseSchema** — Force the agent to return structured output matching a record type. Use when downstream code needs to process the agent's output programmatically (e.g., a classification agent returning a typed result).
|
|
112
|
+
- **documents** — Attach documents for RAG (retrieval-augmented generation). Use when the agent needs access to reference material (e.g., product manuals, pricing lists, policy documents).
|
|
113
|
+
- **validate** — Run a validation workflow on the agent's output before accepting it. Use when the agent's output must meet specific business rules.
|
|
114
|
+
- **retry** — Retry policy for failed agent invocations. Use when the agent's task is critical and transient failures are possible.
|
|
115
|
+
- **stateless** — If true, the agent has no conversation history. Use for one-shot classification or transformation tasks that don't need context from previous interactions.
|
|
116
|
+
- **scratch** — Temporary mutable variables available during the agent's execution. Use when the agent needs working memory across tool calls within a single interaction.
|
|
117
|
+
- **compact** — Number of conversation turns before older messages are summarized and discarded. Use for long-running agents to prevent context overflow.
|
|
118
|
+
|
|
119
|
+
### Flow (Advanced)
|
|
120
|
+
|
|
121
|
+
A flow is an **orchestration pipeline** connecting multiple agents and decision points. Use when a complex process requires sequential agent invocations with routing logic.
|
|
122
|
+
|
|
123
|
+
**When to define a flow:** Multi-step AI processes where one agent's output feeds another, with conditional routing between them. Most applications don't need flows — they are for complex orchestration scenarios.
|
|
124
|
+
|
|
125
|
+
### How Constructs Compose
|
|
126
|
+
|
|
127
|
+
The typical pattern is:
|
|
128
|
+
1. **Entities** hold the data (the nouns)
|
|
129
|
+
2. **Relationships** define how entities connect
|
|
130
|
+
3. **Events** represent actions users or systems can take (the verbs)
|
|
131
|
+
4. **Workflows** handle events by reading/writing entities
|
|
132
|
+
5. **Agents** add intelligence — they are invoked by workflows when AI reasoning is needed
|
|
133
|
+
6. **Flows** orchestrate multiple agents for complex multi-step AI processes
|
|
134
|
+
`;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
import { CraftLlmProvider } from '../interfaces/llm-provider.interface';
|
|
4
|
+
import { ChatRequest, ChatResponse, ToolCall } from '../types/llm.types';
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
export class AnthropicCraftLlmService implements CraftLlmProvider {
|
|
8
|
+
private client: Anthropic;
|
|
9
|
+
private model: string;
|
|
10
|
+
private maxRetries = 3;
|
|
11
|
+
private baseDelay = 1000;
|
|
12
|
+
|
|
13
|
+
constructor(model: string, apiKey: string) {
|
|
14
|
+
this.client = new Anthropic({ apiKey });
|
|
15
|
+
this.model = model;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async chat(request: ChatRequest): Promise<ChatResponse> {
|
|
19
|
+
return this.withRetry(async () => {
|
|
20
|
+
const useCache = request.cacheControl ?? false;
|
|
21
|
+
|
|
22
|
+
const params: Anthropic.MessageCreateParams = {
|
|
23
|
+
model: this.model,
|
|
24
|
+
max_tokens: request.maxTokens ?? 16384,
|
|
25
|
+
system: useCache
|
|
26
|
+
? [
|
|
27
|
+
{
|
|
28
|
+
type: 'text' as const,
|
|
29
|
+
text: request.system,
|
|
30
|
+
cache_control: { type: 'ephemeral' as const },
|
|
31
|
+
},
|
|
32
|
+
]
|
|
33
|
+
: request.system,
|
|
34
|
+
messages: request.messages.map((m) => ({
|
|
35
|
+
role: m.role,
|
|
36
|
+
content:
|
|
37
|
+
typeof m.content === 'string'
|
|
38
|
+
? m.content
|
|
39
|
+
: m.content
|
|
40
|
+
.filter(
|
|
41
|
+
(b): b is { type: 'text'; text: string } =>
|
|
42
|
+
b.type === 'text',
|
|
43
|
+
)
|
|
44
|
+
.map((b) => b.text)
|
|
45
|
+
.join(''),
|
|
46
|
+
})),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (request.tools?.length) {
|
|
50
|
+
params.tools = request.tools.map((t, i) => ({
|
|
51
|
+
name: t.name,
|
|
52
|
+
description: t.description ?? '',
|
|
53
|
+
input_schema: t.inputSchema as Anthropic.Tool.InputSchema,
|
|
54
|
+
...(useCache && i === request.tools!.length - 1
|
|
55
|
+
? { cache_control: { type: 'ephemeral' as const } }
|
|
56
|
+
: {}),
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (request.toolChoice) {
|
|
61
|
+
params.tool_choice = {
|
|
62
|
+
type: 'tool' as const,
|
|
63
|
+
name: request.toolChoice.name,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Use streaming to avoid the SDK's 10-minute timeout guard
|
|
68
|
+
const stream = this.client.messages.stream(params);
|
|
69
|
+
for await (const event of stream) {
|
|
70
|
+
// consume events to drive the stream
|
|
71
|
+
void event;
|
|
72
|
+
}
|
|
73
|
+
const response = await stream.finalMessage();
|
|
74
|
+
|
|
75
|
+
const content = response.content
|
|
76
|
+
.filter((block) => block.type === 'text')
|
|
77
|
+
.map((block) => (block as Anthropic.TextBlock).text)
|
|
78
|
+
.join('');
|
|
79
|
+
|
|
80
|
+
const toolCalls: ToolCall[] = response.content
|
|
81
|
+
.filter(
|
|
82
|
+
(block): block is Anthropic.ToolUseBlock => block.type === 'tool_use',
|
|
83
|
+
)
|
|
84
|
+
.map((block) => ({
|
|
85
|
+
id: block.id,
|
|
86
|
+
name: block.name,
|
|
87
|
+
input: block.input,
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
content: [{ type: 'text', text: content }],
|
|
92
|
+
stopReason:
|
|
93
|
+
response.stop_reason === 'max_tokens' ? 'max_tokens' : 'end_turn',
|
|
94
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
95
|
+
truncated: response.stop_reason === 'max_tokens',
|
|
96
|
+
usage: {
|
|
97
|
+
inputTokens: response.usage.input_tokens,
|
|
98
|
+
outputTokens: response.usage.output_tokens,
|
|
99
|
+
cacheCreationInputTokens:
|
|
100
|
+
response.usage.cache_creation_input_tokens ?? undefined,
|
|
101
|
+
cacheReadInputTokens:
|
|
102
|
+
response.usage.cache_read_input_tokens ?? undefined,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async chatStream(
|
|
109
|
+
request: ChatRequest,
|
|
110
|
+
onChunk: (text: string) => void,
|
|
111
|
+
): Promise<ChatResponse> {
|
|
112
|
+
return this.withRetry(async () => {
|
|
113
|
+
const stream = this.client.messages.stream({
|
|
114
|
+
model: this.model,
|
|
115
|
+
max_tokens: request.maxTokens ?? 16384,
|
|
116
|
+
system: request.system,
|
|
117
|
+
messages: request.messages.map((m) => ({
|
|
118
|
+
role: m.role,
|
|
119
|
+
content:
|
|
120
|
+
typeof m.content === 'string'
|
|
121
|
+
? m.content
|
|
122
|
+
: m.content
|
|
123
|
+
.filter(
|
|
124
|
+
(b): b is { type: 'text'; text: string } =>
|
|
125
|
+
b.type === 'text',
|
|
126
|
+
)
|
|
127
|
+
.map((b) => b.text)
|
|
128
|
+
.join(''),
|
|
129
|
+
})),
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
let fullContent = '';
|
|
133
|
+
for await (const event of stream) {
|
|
134
|
+
if (
|
|
135
|
+
event.type === 'content_block_delta' &&
|
|
136
|
+
event.delta.type === 'text_delta'
|
|
137
|
+
) {
|
|
138
|
+
onChunk(event.delta.text);
|
|
139
|
+
fullContent += event.delta.text;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const finalMessage = await stream.finalMessage();
|
|
144
|
+
return {
|
|
145
|
+
content: [{ type: 'text', text: fullContent }],
|
|
146
|
+
stopReason:
|
|
147
|
+
finalMessage.stop_reason === 'max_tokens' ? 'max_tokens' : 'end_turn',
|
|
148
|
+
usage: {
|
|
149
|
+
inputTokens: finalMessage.usage.input_tokens,
|
|
150
|
+
outputTokens: finalMessage.usage.output_tokens,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private async withRetry<T>(fn: () => Promise<T>): Promise<T> {
|
|
157
|
+
let lastError: Error | undefined;
|
|
158
|
+
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
|
|
159
|
+
try {
|
|
160
|
+
return await fn();
|
|
161
|
+
} catch (err) {
|
|
162
|
+
lastError = err as Error;
|
|
163
|
+
if (!this.isRetryable(lastError) || attempt === this.maxRetries - 1) {
|
|
164
|
+
throw lastError;
|
|
165
|
+
}
|
|
166
|
+
const delay = this.baseDelay * Math.pow(2, attempt);
|
|
167
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
throw lastError;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private isRetryable(error: Error): boolean {
|
|
174
|
+
const message = error.message.toLowerCase();
|
|
175
|
+
return (
|
|
176
|
+
message.includes('rate limit') ||
|
|
177
|
+
message.includes('429') ||
|
|
178
|
+
message.includes('500') ||
|
|
179
|
+
message.includes('502') ||
|
|
180
|
+
message.includes('503') ||
|
|
181
|
+
message.includes('network') ||
|
|
182
|
+
message.includes('timeout') ||
|
|
183
|
+
message.includes('econnreset')
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|