create-windy 0.3.2 → 0.3.5
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 +27 -9
- package/dist/cli.js +825 -117
- package/package.json +1 -1
- package/template/.agents/skills/windy-business-module/SKILL.md +16 -3
- package/template/.windy-template.json +2 -2
- package/template/AGENTS.md +4 -0
- package/template/README.md +18 -11
- package/template/_gitignore +1 -0
- package/template/apps/agent-server/README.md +2 -0
- package/template/apps/agent-server/pyproject.toml +8 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +87 -10
- package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +18 -3
- package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +78 -11
- package/template/apps/agent-server/src/southwind_agent_server/app.py +120 -115
- package/template/apps/agent-server/src/southwind_agent_server/artifact_input.py +153 -0
- package/template/apps/agent-server/src/southwind_agent_server/config.py +29 -0
- package/template/apps/agent-server/src/southwind_agent_server/contracts.py +81 -1
- package/template/apps/agent-server/src/southwind_agent_server/deterministic.py +8 -0
- package/template/apps/agent-server/src/southwind_agent_server/engine.py +28 -0
- package/template/apps/agent-server/src/southwind_agent_server/fingerprint.py +41 -0
- package/template/apps/agent-server/src/southwind_agent_server/http_helpers.py +62 -0
- package/template/apps/agent-server/src/southwind_agent_server/lease.py +15 -0
- package/template/apps/agent-server/src/southwind_agent_server/multimodal.py +76 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +112 -20
- package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +14 -1
- package/template/apps/agent-server/src/southwind_agent_server/postgres_store.py +329 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_store_backend.py +332 -0
- package/template/apps/agent-server/src/southwind_agent_server/provider_governance.py +64 -3
- package/template/apps/agent-server/src/southwind_agent_server/runtime_factory.py +68 -0
- package/template/apps/agent-server/src/southwind_agent_server/service.py +218 -56
- package/template/apps/agent-server/src/southwind_agent_server/signal_events.py +36 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_catalog.py +107 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_schema.py +65 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_serialization.py +16 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_store.py +89 -57
- package/template/apps/agent-server/src/southwind_agent_server/store.py +105 -2
- package/template/apps/agent-server/src/southwind_agent_server/store_cipher.py +40 -0
- package/template/apps/agent-server/src/southwind_agent_server/telemetry.py +89 -0
- package/template/apps/agent-server/src/southwind_agent_server/tool_host.py +72 -0
- package/template/apps/agent-server/tests/test_adk_adapter.py +6 -2
- package/template/apps/agent-server/tests/test_api.py +24 -1
- package/template/apps/agent-server/tests/test_artifact_input.py +110 -0
- package/template/apps/agent-server/tests/test_config.py +4 -0
- package/template/apps/agent-server/tests/test_fingerprint.py +10 -0
- package/template/apps/agent-server/tests/test_multimodal.py +51 -0
- package/template/apps/agent-server/tests/test_openai_provider.py +6 -2
- package/template/apps/agent-server/tests/test_postgres_store.py +131 -0
- package/template/apps/agent-server/tests/test_provider_governance.py +61 -0
- package/template/apps/agent-server/tests/test_sqlite_store.py +95 -10
- package/template/apps/agent-server/tests/test_telemetry.py +25 -0
- package/template/apps/agent-server/tests/test_tool_approval.py +28 -0
- package/template/apps/agent-server/uv.lock +165 -0
- package/template/apps/document-server/Dockerfile +19 -0
- package/template/apps/document-server/pyproject.toml +43 -0
- package/template/apps/document-server/src/southwind_document_server/__init__.py +1 -0
- package/template/apps/document-server/src/southwind_document_server/adapter.py +337 -0
- package/template/apps/document-server/src/southwind_document_server/app.py +151 -0
- package/template/apps/document-server/src/southwind_document_server/config.py +94 -0
- package/template/apps/document-server/src/southwind_document_server/contracts.py +103 -0
- package/template/apps/document-server/src/southwind_document_server/service.py +149 -0
- package/template/apps/document-server/src/southwind_document_server/store.py +185 -0
- package/template/apps/document-server/tests/__init__.py +1 -0
- package/template/apps/document-server/tests/test_config.py +42 -0
- package/template/apps/document-server/tests/test_service.py +140 -0
- package/template/apps/document-server/uv.lock +430 -0
- package/template/apps/embedding-server/Dockerfile +17 -0
- package/template/apps/embedding-server/pyproject.toml +44 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/__init__.py +1 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/adapter.py +254 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/app.py +119 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/config.py +120 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/contracts.py +106 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/error.py +6 -0
- package/template/apps/embedding-server/src/southwind_embedding_server/image_input.py +87 -0
- package/template/apps/embedding-server/tests/__init__.py +1 -0
- package/template/apps/embedding-server/tests/test_adapter.py +92 -0
- package/template/apps/embedding-server/tests/test_config.py +38 -0
- package/template/apps/embedding-server/uv.lock +449 -0
- package/template/apps/server/src/agent/background-operation-port.ts +37 -0
- package/template/apps/server/src/agent/background-operation.test.ts +231 -0
- package/template/apps/server/src/agent/background-operation.ts +252 -0
- package/template/apps/server/src/agent/drizzle-run-binding-repository.ts +114 -0
- package/template/apps/server/src/agent/execution-grants.ts +7 -1
- package/template/apps/server/src/agent/fake-runtime.test-support.ts +43 -0
- package/template/apps/server/src/agent/idempotency-key.test.ts +28 -0
- package/template/apps/server/src/agent/idempotency-key.ts +11 -0
- package/template/apps/server/src/agent/remote-runtime.test.ts +115 -1
- package/template/apps/server/src/agent/remote-runtime.ts +173 -5
- package/template/apps/server/src/agent/run-authorization.test-support.ts +9 -0
- package/template/apps/server/src/agent/run-authorization.test.ts +126 -0
- package/template/apps/server/src/agent/run-authorization.ts +180 -0
- package/template/apps/server/src/agent/run-binding-repository.ts +93 -0
- package/template/apps/server/src/agent/run-contracts.ts +88 -1
- package/template/apps/server/src/agent/run-definition-support.ts +58 -0
- package/template/apps/server/src/agent/run-errors.ts +25 -0
- package/template/apps/server/src/agent/run-facade.test.ts +57 -46
- package/template/apps/server/src/agent/run-facade.ts +185 -65
- package/template/apps/server/src/agent/run-release.ts +78 -0
- package/template/apps/server/src/agent/run-route-input.ts +169 -0
- package/template/apps/server/src/agent/run-routes.ts +63 -76
- package/template/apps/server/src/agent/runtime-bootstrap.ts +169 -13
- package/template/apps/server/src/agent/sandbox-client.ts +200 -0
- package/template/apps/server/src/agent/sandbox-protocol.ts +76 -0
- package/template/apps/server/src/agent/sandbox-registry.ts +205 -0
- package/template/apps/server/src/agent/sandbox-runtime.test.ts +137 -0
- package/template/apps/server/src/agent/sandbox-worker-server.ts +195 -0
- package/template/apps/server/src/agent/sandbox-worker.ts +12 -0
- package/template/apps/server/src/agent/tool-approval.test.ts +75 -0
- package/template/apps/server/src/agent/tool-approval.ts +115 -0
- package/template/apps/server/src/agent/tool-audit.ts +62 -0
- package/template/apps/server/src/agent/tool-host.test.ts +39 -11
- package/template/apps/server/src/agent/tool-host.ts +184 -17
- package/template/apps/server/src/agent/tool-registry.ts +52 -47
- package/template/apps/server/src/agent/vision-facade.test.ts +256 -0
- package/template/apps/server/src/agent/vision-input.ts +120 -0
- package/template/apps/server/src/ai-artifact/codec.test.ts +44 -0
- package/template/apps/server/src/ai-artifact/codec.ts +137 -0
- package/template/apps/server/src/ai-artifact/routes.ts +37 -0
- package/template/apps/server/src/ai-artifact/runtime.ts +205 -0
- package/template/apps/server/src/application-services.ts +62 -2
- package/template/apps/server/src/auth/reauthentication.test.ts +40 -0
- package/template/apps/server/src/auth/reauthentication.ts +19 -0
- package/template/apps/server/src/auth/runtime.ts +9 -1
- package/template/apps/server/src/data-access/context.ts +6 -4
- package/template/apps/server/src/data-access/scope-snapshot.ts +27 -0
- package/template/apps/server/src/data-governance/export/approval-port.ts +6 -3
- package/template/apps/server/src/data-governance/export/authorization-port.test.ts +23 -4
- package/template/apps/server/src/data-governance/export/authorization-port.ts +89 -25
- package/template/apps/server/src/data-governance/export/drizzle-repository.integration.test.ts +2 -0
- package/template/apps/server/src/data-governance/export/drizzle-repository.ts +1 -0
- package/template/apps/server/src/data-governance/export/job-runtime.test.ts +15 -4
- package/template/apps/server/src/data-governance/export/job-runtime.ts +68 -15
- package/template/apps/server/src/data-governance/export/ndjson-stream.ts +25 -6
- package/template/apps/server/src/data-governance/export/postgres-recovery.integration.test.ts +4 -2
- package/template/apps/server/src/data-governance/export/renderer-job-runtime.test.ts +235 -0
- package/template/apps/server/src/data-governance/export/renderer-runtime.ts +293 -0
- package/template/apps/server/src/data-governance/export/renderer-selection.test.ts +67 -0
- package/template/apps/server/src/data-governance/export/repository.test.ts +2 -0
- package/template/apps/server/src/data-governance/export/request-fingerprint.test.ts +13 -0
- package/template/apps/server/src/data-governance/export/request-fingerprint.ts +2 -0
- package/template/apps/server/src/data-governance/export/routes.ts +11 -1
- package/template/apps/server/src/data-governance/export/scope-snapshot.ts +6 -18
- package/template/apps/server/src/data-governance/export/service-support.ts +188 -0
- package/template/apps/server/src/data-governance/export/service.ts +90 -113
- package/template/apps/server/src/data-governance/export/source.ts +101 -0
- package/template/apps/server/src/data-governance/export/types.ts +27 -0
- package/template/apps/server/src/document/contracts.ts +76 -0
- package/template/apps/server/src/document/facade.ts +339 -0
- package/template/apps/server/src/document/remote-runtime.test.ts +96 -0
- package/template/apps/server/src/document/remote-runtime.ts +223 -0
- package/template/apps/server/src/document/routes.ts +193 -0
- package/template/apps/server/src/embedding/contracts.ts +83 -0
- package/template/apps/server/src/embedding/error.ts +10 -0
- package/template/apps/server/src/embedding/facade.ts +217 -0
- package/template/apps/server/src/embedding/input.ts +172 -0
- package/template/apps/server/src/embedding/remote-runtime.test.ts +66 -0
- package/template/apps/server/src/embedding/remote-runtime.ts +218 -0
- package/template/apps/server/src/embedding/routes.ts +152 -0
- package/template/apps/server/src/index.ts +40 -43
- package/template/apps/server/src/jobs/artifact-service.test.ts +14 -2
- package/template/apps/server/src/jobs/artifact-service.ts +10 -0
- package/template/apps/server/src/jobs/drizzle-execution-repository.ts +17 -18
- package/template/apps/server/src/jobs/drizzle-execution.integration.test.ts +19 -1
- package/template/apps/server/src/jobs/drizzle-mappers.ts +4 -1
- package/template/apps/server/src/jobs/drizzle-recovery-repository.ts +49 -2
- package/template/apps/server/src/jobs/drizzle-repository.ts +1 -0
- package/template/apps/server/src/jobs/execution-repository.ts +17 -0
- package/template/apps/server/src/jobs/execution.test.ts +34 -0
- package/template/apps/server/src/jobs/in-memory-control.ts +135 -0
- package/template/apps/server/src/jobs/in-memory-execution.ts +34 -65
- package/template/apps/server/src/jobs/legacy-service-runtime.ts +5 -2
- package/template/apps/server/src/jobs/repository.ts +3 -0
- package/template/apps/server/src/jobs/worker.ts +80 -41
- package/template/apps/server/src/module-host/ai-operation-port.ts +170 -0
- package/template/apps/server/src/module-host/durable-context.test.ts +312 -0
- package/template/apps/server/src/module-host/durable-context.ts +124 -0
- package/template/apps/server/src/module-host/durable-handler-registration.ts +72 -0
- package/template/apps/server/src/module-host/governed-export-route-support.ts +171 -0
- package/template/apps/server/src/module-host/governed-export-routes.test.ts +327 -0
- package/template/apps/server/src/module-host/governed-export-routes.ts +238 -0
- package/template/apps/server/src/module-host/governed-renderer-registration.test.ts +247 -0
- package/template/apps/server/src/module-host/governed-resource-routes.ts +331 -0
- package/template/apps/server/src/module-host/governed-resources.test.ts +344 -0
- package/template/apps/server/src/module-host/governed-resources.ts +203 -0
- package/template/apps/server/src/module-host/http-runtime.ts +95 -0
- package/template/apps/server/src/module-host/initialize-contracts.ts +14 -0
- package/template/apps/server/src/module-host/initialize.test.ts +49 -1
- package/template/apps/server/src/module-host/initialize.ts +39 -16
- package/template/apps/server/src/module-host/job-port.test.ts +262 -0
- package/template/apps/server/src/module-host/job-port.ts +334 -0
- package/template/apps/server/src/module-host/request-context.test.ts +88 -2
- package/template/apps/server/src/module-host/request-context.ts +56 -22
- package/template/apps/server/src/module-host/route-installer.test.ts +10 -0
- package/template/apps/server/src/module-host/route-installer.ts +36 -1
- package/template/apps/server/src/module-host/sandbox-tool-registration.ts +42 -0
- package/template/apps/server/src/module-host/tool-handlers.ts +67 -31
- package/template/apps/server/src/module-host.ts +1 -0
- package/template/apps/server/src/module-runtime-validation.ts +7 -3
- package/template/apps/server/src/speech/contracts.ts +76 -0
- package/template/apps/server/src/speech/facade.ts +316 -0
- package/template/apps/server/src/speech/remote-runtime.test.ts +88 -0
- package/template/apps/server/src/speech/remote-runtime.ts +227 -0
- package/template/apps/server/src/speech/routes.ts +180 -0
- package/template/apps/server/src/system/actor.ts +39 -0
- package/template/apps/server/src/system/route-helpers.ts +1 -0
- package/template/apps/server/src/workflow/service.ts +2 -1
- package/template/apps/speech-server/Dockerfile +22 -0
- package/template/apps/speech-server/pyproject.toml +43 -0
- package/template/apps/speech-server/src/southwind_speech_server/__init__.py +1 -0
- package/template/apps/speech-server/src/southwind_speech_server/adapter.py +249 -0
- package/template/apps/speech-server/src/southwind_speech_server/app.py +183 -0
- package/template/apps/speech-server/src/southwind_speech_server/config.py +100 -0
- package/template/apps/speech-server/src/southwind_speech_server/contracts.py +103 -0
- package/template/apps/speech-server/src/southwind_speech_server/errors.py +6 -0
- package/template/apps/speech-server/src/southwind_speech_server/normalizer.py +108 -0
- package/template/apps/speech-server/src/southwind_speech_server/output_validation.py +128 -0
- package/template/apps/speech-server/src/southwind_speech_server/service.py +162 -0
- package/template/apps/speech-server/src/southwind_speech_server/store.py +209 -0
- package/template/apps/speech-server/tests/__init__.py +1 -0
- package/template/apps/speech-server/tests/test_config.py +33 -0
- package/template/apps/speech-server/tests/test_service.py +154 -0
- package/template/apps/speech-server/uv.lock +430 -0
- package/template/apps/web/src/app/business-routes.ts +22 -0
- package/template/apps/web/src/components/common/AccessSnapshotBoundary.vue +112 -0
- package/template/apps/web/src/components/common/AccessSnapshotBoundary.webtest.ts +130 -0
- package/template/apps/web/src/components/common/AccessSnapshotNotice.vue +117 -0
- package/template/apps/web/src/components/common/FormDialog.vue +6 -0
- package/template/apps/web/src/components/common/ModalLayout.webtest.ts +13 -0
- package/template/apps/web/src/composables/access-snapshot-change.ts +131 -0
- package/template/apps/web/src/composables/access-snapshot-change.webtest.ts +103 -0
- package/template/apps/web/src/composables/useAccessSnapshot.ts +48 -2
- package/template/apps/web/src/composables/useAccessSnapshot.webtest.ts +54 -0
- package/template/apps/web/src/composables/useAccessSnapshotMonitor.ts +42 -0
- package/template/apps/web/src/layout/AppShell.vue +28 -25
- package/template/apps/web/src/layout/AppShell.webtest.ts +1 -0
- package/template/apps/web/src/router/business-routes.ts +68 -0
- package/template/apps/web/src/router/business-routes.webtest.ts +85 -0
- package/template/apps/web/src/router/index.ts +7 -0
- package/template/apps/web/src/router/shared-scaffold-neutrality.webtest.ts +12 -0
- package/template/apps/web/src/services/access-api.ts +5 -1
- package/template/apps/web/src/services/access-api.webtest.ts +35 -0
- package/template/docker-compose.yml +127 -0
- package/template/docs/architecture/ai-egress.md +12 -9
- package/template/docs/architecture/ai-runtime.md +23 -11
- package/template/docs/architecture/durable-jobs.md +25 -3
- package/template/docs/architecture/foundation-infrastructure.md +163 -0
- package/template/docs/architecture/module-host.md +115 -0
- package/template/docs/architecture/server-module-interface.md +39 -0
- package/template/docs/architecture/web-access-snapshot-resilience.md +60 -0
- package/template/docs/development/business-web-routes.md +66 -0
- package/template/docs/development/custom-login-page.md +3 -3
- package/template/docs/development/module-lifecycle.md +71 -0
- package/template/docs/operations/observability.md +5 -1
- package/template/docs/platform/agent-runtime.md +21 -11
- package/template/docs/platform/agent-tool-sandbox.md +58 -0
- package/template/docs/platform/agent-tools.md +5 -0
- package/template/docs/platform/document-ai-runtime.md +204 -0
- package/template/docs/platform/governed-export-renderers.md +132 -0
- package/template/docs/platform/navigation-registry.md +5 -0
- package/template/docs/platform/organization-membership.md +2 -1
- package/template/docs/platform/speech-ai-runtime.md +156 -0
- package/template/docs/platform/system-crud-api.md +7 -1
- package/template/docs/platform/vision-ai-runtime.md +97 -0
- package/template/docs/platform/web-system-crud.md +6 -0
- package/template/package.json +16 -2
- package/template/packages/ai-client/index.ts +4 -0
- package/template/packages/ai-client/package.json +5 -2
- package/template/packages/ai-client/src/agent/client/artifact.ts +26 -0
- package/template/packages/ai-client/src/agent/client/attach.test.ts +124 -0
- package/template/packages/ai-client/src/agent/client/client.ts +119 -7
- package/template/packages/ai-client/src/agent/client/run.ts +14 -0
- package/template/packages/ai-client/src/agent/client/types.ts +18 -0
- package/template/packages/ai-client/src/agent/client/vision.test.ts +53 -0
- package/template/packages/ai-client/src/agent/contracts/contracts.test.ts +1 -0
- package/template/packages/ai-client/src/agent/contracts/events.ts +33 -0
- package/template/packages/ai-client/src/agent/contracts/runs.ts +71 -0
- package/template/packages/ai-client/src/agent/index.ts +1 -0
- package/template/packages/ai-client/src/artifact-ref.ts +8 -0
- package/template/packages/ai-client/src/document/client.test.ts +114 -0
- package/template/packages/ai-client/src/document/client.ts +134 -0
- package/template/packages/ai-client/src/document/contracts.ts +152 -0
- package/template/packages/ai-client/src/document/error.ts +43 -0
- package/template/packages/ai-client/src/document/index.ts +3 -0
- package/template/packages/ai-client/src/document/run.ts +104 -0
- package/template/packages/ai-client/src/document/validation.ts +115 -0
- package/template/packages/ai-client/src/embedding/client.test.ts +77 -0
- package/template/packages/ai-client/src/embedding/client.ts +96 -0
- package/template/packages/ai-client/src/embedding/contracts.ts +46 -0
- package/template/packages/ai-client/src/embedding/error.ts +61 -0
- package/template/packages/ai-client/src/embedding/index.ts +3 -0
- package/template/packages/ai-client/src/embedding/validation.ts +117 -0
- package/template/packages/ai-client/src/speech/client.test.ts +135 -0
- package/template/packages/ai-client/src/speech/client.ts +104 -0
- package/template/packages/ai-client/src/speech/contracts.ts +129 -0
- package/template/packages/ai-client/src/speech/error.ts +43 -0
- package/template/packages/ai-client/src/speech/index.ts +3 -0
- package/template/packages/ai-client/src/speech/run.ts +133 -0
- package/template/packages/ai-client/src/speech/validation.ts +132 -0
- package/template/packages/config/package.json +2 -2
- package/template/packages/database/drizzle/0039_early_ezekiel_stane.sql +1 -0
- package/template/packages/database/drizzle/0040_colossal_baron_strucker.sql +63 -0
- package/template/packages/database/drizzle/0041_yummy_prowler.sql +21 -0
- package/template/packages/database/drizzle/0042_puzzling_rumiko_fujikawa.sql +27 -0
- package/template/packages/database/drizzle/meta/0039_snapshot.json +7755 -0
- package/template/packages/database/drizzle/meta/0040_snapshot.json +8150 -0
- package/template/packages/database/drizzle/meta/0041_snapshot.json +8323 -0
- package/template/packages/database/drizzle/meta/0042_snapshot.json +8541 -0
- package/template/packages/database/drizzle/meta/_journal.json +28 -0
- package/template/packages/database/package.json +2 -2
- package/template/packages/database/src/agent-runtime-schema.test.ts +54 -0
- package/template/packages/database/src/schema/agent-runtime.ts +149 -0
- package/template/packages/database/src/schema/common.ts +2 -14
- package/template/packages/database/src/schema/durable-jobs.ts +5 -0
- package/template/packages/database/src/schema/governed-exports.ts +17 -5
- package/template/packages/database/src/schema/index.ts +3 -0
- package/template/packages/database/src/schema-durable-recovery.test.ts +31 -0
- package/template/packages/database/src/schema-governed-exports.test.ts +21 -1
- package/template/packages/database/src/schema.test.ts +6 -23
- package/template/packages/jobs/package.json +3 -2
- package/template/packages/jobs/src/handlers.test.ts +13 -0
- package/template/packages/jobs/src/handlers.ts +7 -1
- package/template/packages/jobs/src/types.ts +4 -0
- package/template/packages/modules/package.json +2 -2
- package/template/packages/modules/src/ai-operation-composition.test-support.ts +165 -0
- package/template/packages/modules/src/ai-operation-composition.test.ts +91 -127
- package/template/packages/modules/src/ai-operation-composition.ts +59 -1
- package/template/packages/modules/src/manifest.ts +25 -1
- package/template/packages/modules/src/tool-composition.ts +18 -0
- package/template/packages/server-sdk/index.ts +50 -0
- package/template/packages/server-sdk/package.json +10 -4
- package/template/packages/server-sdk/src/ai-artifact-port.ts +34 -0
- package/template/packages/server-sdk/src/ai-operation-port.ts +65 -0
- package/template/packages/server-sdk/src/ai-operation-registration.ts +42 -1
- package/template/packages/server-sdk/src/background-ai-port.ts +43 -0
- package/template/packages/server-sdk/src/data-governance-registration.ts +116 -0
- package/template/packages/server-sdk/src/durable-handler.ts +29 -0
- package/template/packages/server-sdk/src/jobs-port.ts +69 -0
- package/template/packages/server-sdk/src/module-host.ts +16 -2
- package/template/packages/server-sdk/src/request-context.ts +6 -2
- package/template/packages/server-sdk/src/sandbox-tool.ts +21 -0
- package/template/packages/server-sdk/src/tool-registration.ts +2 -0
- package/template/packages/shared/package.json +1 -1
- package/template/packages/shared/src/rbac.ts +30 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModuleBackgroundAiOperationInput,
|
|
3
|
+
ModuleBackgroundAiPort,
|
|
4
|
+
} from "@southwind-ai/server-sdk";
|
|
5
|
+
import { ModuleBackgroundAiError } from "@southwind-ai/server-sdk";
|
|
6
|
+
import type { RequestGuardContext } from "../guards.js";
|
|
7
|
+
import type { AgentBackgroundOperationExecutor } from "./background-operation.js";
|
|
8
|
+
|
|
9
|
+
export class DeferredAgentBackgroundOperationPortFactory {
|
|
10
|
+
#executor?: AgentBackgroundOperationExecutor;
|
|
11
|
+
|
|
12
|
+
bind(executor: AgentBackgroundOperationExecutor): void {
|
|
13
|
+
if (this.#executor) {
|
|
14
|
+
throw new Error("后台 AI Operation Executor 已绑定");
|
|
15
|
+
}
|
|
16
|
+
this.#executor = executor;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
create(
|
|
20
|
+
moduleName: string,
|
|
21
|
+
context: RequestGuardContext,
|
|
22
|
+
signal: AbortSignal,
|
|
23
|
+
): ModuleBackgroundAiPort {
|
|
24
|
+
return {
|
|
25
|
+
enqueueOperation: (input: ModuleBackgroundAiOperationInput) => {
|
|
26
|
+
if (!this.#executor) {
|
|
27
|
+
throw new ModuleBackgroundAiError(
|
|
28
|
+
"AI_OPERATION_UNAVAILABLE",
|
|
29
|
+
"后台 AI Operation Runtime 未启用",
|
|
30
|
+
true,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return this.#executor.execute(moduleName, input, context, signal);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { ModuleManifest } from "@southwind-ai/modules";
|
|
3
|
+
import { buildScopeContext } from "../data-access/context.js";
|
|
4
|
+
import { createRequestGuardContext } from "../guards.js";
|
|
5
|
+
import type { InitializedModuleHosts } from "../module-host.js";
|
|
6
|
+
import { AgentBackgroundOperationExecutor } from "./background-operation.js";
|
|
7
|
+
|
|
8
|
+
describe("Actor-aware 后台 Agent Operation", () => {
|
|
9
|
+
test("只执行当前模块声明且不含 Tool 的 Operation", async () => {
|
|
10
|
+
const fixture = createFixture();
|
|
11
|
+
|
|
12
|
+
await expect(
|
|
13
|
+
fixture.executor.execute(
|
|
14
|
+
"other",
|
|
15
|
+
request(),
|
|
16
|
+
fixture.context,
|
|
17
|
+
new AbortController().signal,
|
|
18
|
+
),
|
|
19
|
+
).rejects.toMatchObject({
|
|
20
|
+
code: "AI_OPERATION_DENIED",
|
|
21
|
+
retryable: false,
|
|
22
|
+
});
|
|
23
|
+
expect(fixture.started).toEqual([]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("等待既有 Run 终态并读取受控结果", async () => {
|
|
27
|
+
const fixture = createFixture();
|
|
28
|
+
|
|
29
|
+
await expect(
|
|
30
|
+
fixture.executor.execute(
|
|
31
|
+
"business",
|
|
32
|
+
request(),
|
|
33
|
+
fixture.context,
|
|
34
|
+
new AbortController().signal,
|
|
35
|
+
),
|
|
36
|
+
).resolves.toEqual({
|
|
37
|
+
runId: "run-background",
|
|
38
|
+
output: { summary: "滚动摘要" },
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(fixture.started).toEqual([
|
|
42
|
+
{
|
|
43
|
+
operationKey: "business.summarize",
|
|
44
|
+
idempotencyKey: "session-1-sequence-8",
|
|
45
|
+
actorId: "actor-1",
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
expect(fixture.attachCalls()).toBe(2);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("Durable Job 取消会停止等待并请求取消远端 Run", async () => {
|
|
52
|
+
const controller = new AbortController();
|
|
53
|
+
const fixture = createFixture({ waitUntilAbort: true });
|
|
54
|
+
const pending = fixture.executor.execute(
|
|
55
|
+
"business",
|
|
56
|
+
request(),
|
|
57
|
+
fixture.context,
|
|
58
|
+
controller.signal,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
await Promise.resolve();
|
|
62
|
+
controller.abort("cancelled");
|
|
63
|
+
|
|
64
|
+
await expect(pending).rejects.toMatchObject({
|
|
65
|
+
code: "AI_OPERATION_CANCELLED",
|
|
66
|
+
retryable: false,
|
|
67
|
+
});
|
|
68
|
+
expect(fixture.cancelled).toEqual(["run-background"]);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
function createFixture(options: { waitUntilAbort?: boolean } = {}) {
|
|
73
|
+
const module = manifest();
|
|
74
|
+
const hosts = {
|
|
75
|
+
aiOperationHandlers: [
|
|
76
|
+
{
|
|
77
|
+
moduleName: "business",
|
|
78
|
+
registration: {
|
|
79
|
+
operationKey: "business.summarize",
|
|
80
|
+
prepare: () => ({
|
|
81
|
+
instructions: "压缩历史上下文。",
|
|
82
|
+
userPrompt: "生成滚动摘要。",
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
} as unknown as InitializedModuleHosts;
|
|
88
|
+
const started: unknown[] = [];
|
|
89
|
+
const cancelled: string[] = [];
|
|
90
|
+
let attaches = 0;
|
|
91
|
+
const facade = {
|
|
92
|
+
async start(
|
|
93
|
+
operationKey: string,
|
|
94
|
+
_input: unknown,
|
|
95
|
+
idempotencyKey: string,
|
|
96
|
+
context: ReturnType<typeof guardContext>,
|
|
97
|
+
) {
|
|
98
|
+
started.push({
|
|
99
|
+
operationKey,
|
|
100
|
+
idempotencyKey,
|
|
101
|
+
actorId: context.actor.id,
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
runId: "run-background",
|
|
105
|
+
status: "accepted" as const,
|
|
106
|
+
eventsUrl: "/events",
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
async attach(
|
|
110
|
+
_operationKey: string,
|
|
111
|
+
runId: string,
|
|
112
|
+
_cursor: number,
|
|
113
|
+
_context: ReturnType<typeof guardContext>,
|
|
114
|
+
signal?: AbortSignal,
|
|
115
|
+
) {
|
|
116
|
+
attaches += 1;
|
|
117
|
+
if (options.waitUntilAbort) {
|
|
118
|
+
await new Promise((_, reject) => {
|
|
119
|
+
signal?.addEventListener("abort", () => reject(signal.reason), {
|
|
120
|
+
once: true,
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
runId,
|
|
126
|
+
status: attaches === 1 ? ("running" as const) : ("completed" as const),
|
|
127
|
+
lastSequence: attaches,
|
|
128
|
+
eventsUrl: "/events",
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
const remote = {
|
|
133
|
+
result: () =>
|
|
134
|
+
Promise.resolve(
|
|
135
|
+
Response.json({
|
|
136
|
+
runId: "run-background",
|
|
137
|
+
status: "completed",
|
|
138
|
+
output: { summary: "滚动摘要" },
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
cancel: (runId: string) => {
|
|
142
|
+
cancelled.push(runId);
|
|
143
|
+
return Promise.resolve(new Response());
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
executor: new AgentBackgroundOperationExecutor(
|
|
148
|
+
[module],
|
|
149
|
+
hosts,
|
|
150
|
+
facade,
|
|
151
|
+
remote,
|
|
152
|
+
0,
|
|
153
|
+
),
|
|
154
|
+
context: guardContext(),
|
|
155
|
+
started,
|
|
156
|
+
cancelled,
|
|
157
|
+
attachCalls: () => attaches,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function request() {
|
|
162
|
+
return {
|
|
163
|
+
operationKey: "business.summarize",
|
|
164
|
+
input: { messages: ["a", "b"] },
|
|
165
|
+
idempotencyKey: "session-1-sequence-8",
|
|
166
|
+
timeoutSeconds: 30,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function guardContext() {
|
|
171
|
+
return createRequestGuardContext(new Request("http://durable.internal"), {
|
|
172
|
+
resolveActor: () => ({
|
|
173
|
+
id: "actor-1",
|
|
174
|
+
type: "user",
|
|
175
|
+
name: "测试用户",
|
|
176
|
+
roleCodes: ["user"],
|
|
177
|
+
permissionKeys: ["business.ai.execute"],
|
|
178
|
+
scopeContext: buildScopeContext("actor-1", undefined, [
|
|
179
|
+
{
|
|
180
|
+
id: "role-user",
|
|
181
|
+
code: "user",
|
|
182
|
+
dataScope: "self",
|
|
183
|
+
departmentIds: [],
|
|
184
|
+
},
|
|
185
|
+
]),
|
|
186
|
+
}),
|
|
187
|
+
resolveLicense: () => ({ status: "valid" }),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function manifest(): ModuleManifest {
|
|
192
|
+
return {
|
|
193
|
+
name: "business",
|
|
194
|
+
version: "1.0.0",
|
|
195
|
+
label: "业务",
|
|
196
|
+
description: "业务",
|
|
197
|
+
menus: [],
|
|
198
|
+
adminRoutes: [],
|
|
199
|
+
permissions: [],
|
|
200
|
+
apiPermissions: [],
|
|
201
|
+
features: [],
|
|
202
|
+
migrations: [],
|
|
203
|
+
schemaExports: [],
|
|
204
|
+
jsonSchemas: [],
|
|
205
|
+
tasks: [],
|
|
206
|
+
tools: [],
|
|
207
|
+
providers: [],
|
|
208
|
+
auditActions: [],
|
|
209
|
+
aiOperations: [
|
|
210
|
+
{
|
|
211
|
+
key: "business.summarize",
|
|
212
|
+
kind: "agent",
|
|
213
|
+
featureKey: "business.ai",
|
|
214
|
+
permissionKey: "business.ai.execute",
|
|
215
|
+
auditAction: "business.ai.execute",
|
|
216
|
+
inputSchemaKey: "business.summary.input.v1",
|
|
217
|
+
outputSchemaKey: "business.summary.output.v1",
|
|
218
|
+
requiredCapabilities: ["text-generation"],
|
|
219
|
+
allowedToolKeys: [],
|
|
220
|
+
dataClassification: "internal",
|
|
221
|
+
execution: "streaming",
|
|
222
|
+
publicNetworkAccess: "forbidden",
|
|
223
|
+
limits: {
|
|
224
|
+
maxDurationSeconds: 60,
|
|
225
|
+
maxInputBytes: 16_384,
|
|
226
|
+
maxOutputBytes: 4_096,
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
};
|
|
231
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModuleAiOperationDefinition,
|
|
3
|
+
ModuleManifest,
|
|
4
|
+
} from "@southwind-ai/modules";
|
|
5
|
+
import type {
|
|
6
|
+
ModuleBackgroundAiOperationInput,
|
|
7
|
+
ModuleBackgroundAiOperationResult,
|
|
8
|
+
ModuleAiJsonValue,
|
|
9
|
+
} from "@southwind-ai/server-sdk";
|
|
10
|
+
import { ModuleBackgroundAiError } from "@southwind-ai/server-sdk";
|
|
11
|
+
import type { RequestGuardContext } from "../guards.js";
|
|
12
|
+
import type { InitializedModuleHosts } from "../module-host.js";
|
|
13
|
+
import type { AgentRuntimePort } from "./run-contracts.js";
|
|
14
|
+
import type { AgentRunFacade } from "./run-facade.js";
|
|
15
|
+
import { AgentRunFacadeError } from "./run-errors.js";
|
|
16
|
+
|
|
17
|
+
export class AgentBackgroundOperationExecutor {
|
|
18
|
+
readonly #operations: Map<string, BackgroundOperation>;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
modules: readonly ModuleManifest[],
|
|
22
|
+
hosts: InitializedModuleHosts,
|
|
23
|
+
private readonly facade: Pick<AgentRunFacade, "start" | "attach">,
|
|
24
|
+
private readonly remote: Pick<AgentRuntimePort, "result" | "cancel">,
|
|
25
|
+
private readonly pollIntervalMs = 250,
|
|
26
|
+
) {
|
|
27
|
+
const registered = new Set(
|
|
28
|
+
hosts.aiOperationHandlers.map(
|
|
29
|
+
({ registration }) => registration.operationKey,
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
this.#operations = new Map(
|
|
33
|
+
modules.flatMap((module) =>
|
|
34
|
+
(module.aiOperations ?? [])
|
|
35
|
+
.filter(({ key }) => registered.has(key))
|
|
36
|
+
.map((definition) => [
|
|
37
|
+
definition.key,
|
|
38
|
+
{ moduleName: module.name, definition },
|
|
39
|
+
]),
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async execute(
|
|
45
|
+
moduleName: string,
|
|
46
|
+
input: ModuleBackgroundAiOperationInput,
|
|
47
|
+
context: RequestGuardContext,
|
|
48
|
+
outerSignal: AbortSignal,
|
|
49
|
+
): Promise<ModuleBackgroundAiOperationResult> {
|
|
50
|
+
this.assertAllowed(moduleName, input);
|
|
51
|
+
const timeoutSeconds = validateTimeout(input.timeoutSeconds);
|
|
52
|
+
const timeout = AbortSignal.timeout(timeoutSeconds * 1_000);
|
|
53
|
+
const signal = AbortSignal.any([outerSignal, timeout]);
|
|
54
|
+
let runId: string | undefined;
|
|
55
|
+
try {
|
|
56
|
+
const created = await this.facade.start(
|
|
57
|
+
input.operationKey,
|
|
58
|
+
input.input,
|
|
59
|
+
input.idempotencyKey,
|
|
60
|
+
context,
|
|
61
|
+
[],
|
|
62
|
+
signal,
|
|
63
|
+
);
|
|
64
|
+
runId = created.runId;
|
|
65
|
+
while (true) {
|
|
66
|
+
const status = await this.facade.attach(
|
|
67
|
+
input.operationKey,
|
|
68
|
+
runId,
|
|
69
|
+
0,
|
|
70
|
+
context,
|
|
71
|
+
signal,
|
|
72
|
+
);
|
|
73
|
+
if (status.status === "completed") {
|
|
74
|
+
return {
|
|
75
|
+
runId,
|
|
76
|
+
output: await this.readResult(runId, signal),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (status.status === "failed") {
|
|
80
|
+
throw new ModuleBackgroundAiError(
|
|
81
|
+
"AI_OPERATION_FAILED",
|
|
82
|
+
status.terminalEvent?.type === "run.failed"
|
|
83
|
+
? status.terminalEvent.error.message
|
|
84
|
+
: "后台 AI Operation 执行失败",
|
|
85
|
+
status.terminalEvent?.type === "run.failed"
|
|
86
|
+
? status.terminalEvent.error.retryable
|
|
87
|
+
: true,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (status.status === "cancelled") {
|
|
91
|
+
throw new ModuleBackgroundAiError(
|
|
92
|
+
"AI_OPERATION_CANCELLED",
|
|
93
|
+
"后台 AI Operation 已取消",
|
|
94
|
+
false,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
await wait(this.pollIntervalMs, signal);
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (signal.aborted) {
|
|
101
|
+
const timedOut = timeout.aborted || outerSignal.reason === "timeout";
|
|
102
|
+
const cancelled = outerSignal.reason === "cancelled";
|
|
103
|
+
if (runId && (timedOut || cancelled)) {
|
|
104
|
+
await this.cancel(runId);
|
|
105
|
+
}
|
|
106
|
+
throw new ModuleBackgroundAiError(
|
|
107
|
+
timedOut
|
|
108
|
+
? "AI_OPERATION_TIMEOUT"
|
|
109
|
+
: cancelled
|
|
110
|
+
? "AI_OPERATION_CANCELLED"
|
|
111
|
+
: "AI_OPERATION_FAILED",
|
|
112
|
+
timedOut
|
|
113
|
+
? "后台 AI Operation 执行超时"
|
|
114
|
+
: cancelled
|
|
115
|
+
? "后台 AI Operation 已取消"
|
|
116
|
+
: "后台 AI Operation 执行中断,可由任务重试恢复",
|
|
117
|
+
!cancelled,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (error instanceof ModuleBackgroundAiError) throw error;
|
|
121
|
+
if (error instanceof AgentRunFacadeError) {
|
|
122
|
+
throw new ModuleBackgroundAiError(
|
|
123
|
+
error.status === 403 ? "AI_OPERATION_DENIED" : "AI_OPERATION_FAILED",
|
|
124
|
+
error.message,
|
|
125
|
+
error.status >= 500,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
throw new ModuleBackgroundAiError(
|
|
129
|
+
"AI_OPERATION_FAILED",
|
|
130
|
+
"后台 AI Operation 暂不可用",
|
|
131
|
+
true,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private assertAllowed(
|
|
137
|
+
moduleName: string,
|
|
138
|
+
input: ModuleBackgroundAiOperationInput,
|
|
139
|
+
) {
|
|
140
|
+
const operation = this.#operations.get(input.operationKey);
|
|
141
|
+
if (
|
|
142
|
+
!operation ||
|
|
143
|
+
operation.moduleName !== moduleName ||
|
|
144
|
+
operation.definition.kind !== "agent"
|
|
145
|
+
) {
|
|
146
|
+
throw new ModuleBackgroundAiError(
|
|
147
|
+
"AI_OPERATION_DENIED",
|
|
148
|
+
"后台 AI Operation 未在当前模块声明",
|
|
149
|
+
false,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (operation.definition.allowedToolKeys.length) {
|
|
153
|
+
throw new ModuleBackgroundAiError(
|
|
154
|
+
"AI_OPERATION_DENIED",
|
|
155
|
+
"后台 AI Operation 禁止调用 Tool",
|
|
156
|
+
false,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (!input.idempotencyKey.trim() || input.idempotencyKey.length > 200) {
|
|
160
|
+
throw new ModuleBackgroundAiError(
|
|
161
|
+
"AI_OPERATION_INVALID",
|
|
162
|
+
"后台 AI Operation 幂等键无效",
|
|
163
|
+
false,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private async readResult(
|
|
169
|
+
runId: string,
|
|
170
|
+
signal: AbortSignal,
|
|
171
|
+
): Promise<ModuleAiJsonValue> {
|
|
172
|
+
const response = await this.remote.result(runId, signal);
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
throw new ModuleBackgroundAiError(
|
|
175
|
+
"AI_OPERATION_FAILED",
|
|
176
|
+
"后台 AI Operation 结果不可用",
|
|
177
|
+
response.status >= 500,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const value: unknown = await response.json();
|
|
181
|
+
if (
|
|
182
|
+
!isRecord(value) ||
|
|
183
|
+
value.runId !== runId ||
|
|
184
|
+
value.status !== "completed" ||
|
|
185
|
+
!Object.hasOwn(value, "output") ||
|
|
186
|
+
!isJson(value.output)
|
|
187
|
+
) {
|
|
188
|
+
throw new ModuleBackgroundAiError(
|
|
189
|
+
"AI_OPERATION_FAILED",
|
|
190
|
+
"后台 AI Operation 结果格式无效",
|
|
191
|
+
false,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return value.output;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private async cancel(runId: string) {
|
|
198
|
+
try {
|
|
199
|
+
await this.remote.cancel(
|
|
200
|
+
runId,
|
|
201
|
+
"background-operation-aborted",
|
|
202
|
+
AbortSignal.timeout(5_000),
|
|
203
|
+
);
|
|
204
|
+
} catch {
|
|
205
|
+
// 原始 Durable Job 终止优先,远端 Run 仍受自身时长上限约束。
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
interface BackgroundOperation {
|
|
211
|
+
readonly moduleName: string;
|
|
212
|
+
readonly definition: ModuleAiOperationDefinition;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function validateTimeout(value: number | undefined) {
|
|
216
|
+
if (value === undefined) return 300;
|
|
217
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > 900) {
|
|
218
|
+
throw new ModuleBackgroundAiError(
|
|
219
|
+
"AI_OPERATION_INVALID",
|
|
220
|
+
"后台 AI Operation 超时时间无效",
|
|
221
|
+
false,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function wait(milliseconds: number, signal: AbortSignal) {
|
|
228
|
+
return new Promise<void>((resolve, reject) => {
|
|
229
|
+
if (signal.aborted) return reject(signal.reason);
|
|
230
|
+
const timer = setTimeout(resolve, milliseconds);
|
|
231
|
+
signal.addEventListener(
|
|
232
|
+
"abort",
|
|
233
|
+
() => {
|
|
234
|
+
clearTimeout(timer);
|
|
235
|
+
reject(signal.reason);
|
|
236
|
+
},
|
|
237
|
+
{ once: true },
|
|
238
|
+
);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
243
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function isJson(value: unknown): value is ModuleAiJsonValue {
|
|
247
|
+
try {
|
|
248
|
+
return JSON.stringify(value) !== undefined;
|
|
249
|
+
} catch {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { agentRunBindings } from "@southwind-ai/database";
|
|
2
|
+
import { and, eq, isNull, lte, or } from "drizzle-orm";
|
|
3
|
+
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
|
+
import type {
|
|
5
|
+
AgentRunBinding,
|
|
6
|
+
AgentRunBindingRepository,
|
|
7
|
+
} from "./run-binding-repository.js";
|
|
8
|
+
|
|
9
|
+
export class DrizzleAgentRunBindingRepository implements AgentRunBindingRepository {
|
|
10
|
+
constructor(private readonly db: NodePgDatabase) {}
|
|
11
|
+
|
|
12
|
+
async createOrGet(binding: AgentRunBinding) {
|
|
13
|
+
const [created] = await this.db
|
|
14
|
+
.insert(agentRunBindings)
|
|
15
|
+
.values(values(binding))
|
|
16
|
+
.onConflictDoNothing({
|
|
17
|
+
target: agentRunBindings.idempotencyHash,
|
|
18
|
+
})
|
|
19
|
+
.returning();
|
|
20
|
+
if (created) return map(created);
|
|
21
|
+
const [existing] = await this.db
|
|
22
|
+
.select()
|
|
23
|
+
.from(agentRunBindings)
|
|
24
|
+
.where(eq(agentRunBindings.idempotencyHash, binding.idempotencyHash))
|
|
25
|
+
.limit(1);
|
|
26
|
+
if (!existing) throw new Error("Agent Run Binding 幂等创建后无法读取");
|
|
27
|
+
return map(existing);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async bind(bindingId: string, runId: string, boundAt: number) {
|
|
31
|
+
const [bound] = await this.db
|
|
32
|
+
.update(agentRunBindings)
|
|
33
|
+
.set({ runId, boundAt })
|
|
34
|
+
.where(
|
|
35
|
+
and(
|
|
36
|
+
eq(agentRunBindings.bindingId, bindingId),
|
|
37
|
+
or(isNull(agentRunBindings.runId), eq(agentRunBindings.runId, runId)),
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
.returning();
|
|
41
|
+
return bound ? map(bound) : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async findByRunId(runId: string) {
|
|
45
|
+
const [binding] = await this.db
|
|
46
|
+
.select()
|
|
47
|
+
.from(agentRunBindings)
|
|
48
|
+
.where(eq(agentRunBindings.runId, runId))
|
|
49
|
+
.limit(1);
|
|
50
|
+
return binding ? map(binding) : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async findByTokenHash(tokenHash: string) {
|
|
54
|
+
const [binding] = await this.db
|
|
55
|
+
.select()
|
|
56
|
+
.from(agentRunBindings)
|
|
57
|
+
.where(eq(agentRunBindings.tokenHash, tokenHash))
|
|
58
|
+
.limit(1);
|
|
59
|
+
return binding ? map(binding) : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async deleteUnbound(bindingId: string) {
|
|
63
|
+
await this.db
|
|
64
|
+
.delete(agentRunBindings)
|
|
65
|
+
.where(
|
|
66
|
+
and(
|
|
67
|
+
eq(agentRunBindings.bindingId, bindingId),
|
|
68
|
+
isNull(agentRunBindings.runId),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async prune(expiredBefore: number) {
|
|
74
|
+
await this.db
|
|
75
|
+
.delete(agentRunBindings)
|
|
76
|
+
.where(lte(agentRunBindings.expiresAt, expiredBefore));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function values(binding: AgentRunBinding) {
|
|
81
|
+
return {
|
|
82
|
+
bindingId: binding.bindingId,
|
|
83
|
+
runId: binding.runId,
|
|
84
|
+
operationKey: binding.operationKey,
|
|
85
|
+
actorId: binding.actorId,
|
|
86
|
+
idempotencyHash: binding.idempotencyHash,
|
|
87
|
+
tokenHash: binding.tokenHash,
|
|
88
|
+
scopeHash: binding.scopeHash,
|
|
89
|
+
scopeSnapshot: binding.scopeSnapshot,
|
|
90
|
+
authorizationSnapshot: binding.authorizationSnapshot,
|
|
91
|
+
releaseId: binding.releaseId,
|
|
92
|
+
createdAt: binding.createdAt,
|
|
93
|
+
boundAt: binding.boundAt,
|
|
94
|
+
expiresAt: binding.expiresAt,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function map(row: typeof agentRunBindings.$inferSelect): AgentRunBinding {
|
|
99
|
+
return {
|
|
100
|
+
bindingId: row.bindingId,
|
|
101
|
+
runId: row.runId ?? undefined,
|
|
102
|
+
operationKey: row.operationKey,
|
|
103
|
+
actorId: row.actorId,
|
|
104
|
+
idempotencyHash: row.idempotencyHash,
|
|
105
|
+
tokenHash: row.tokenHash,
|
|
106
|
+
scopeHash: row.scopeHash,
|
|
107
|
+
scopeSnapshot: row.scopeSnapshot,
|
|
108
|
+
authorizationSnapshot: row.authorizationSnapshot,
|
|
109
|
+
releaseId: row.releaseId,
|
|
110
|
+
createdAt: row.createdAt,
|
|
111
|
+
boundAt: row.boundAt ?? undefined,
|
|
112
|
+
expiresAt: row.expiresAt,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -22,6 +22,7 @@ export class ExecutionGrantStore {
|
|
|
22
22
|
issue(
|
|
23
23
|
operation: ModuleAiOperationDefinition,
|
|
24
24
|
context: RequestGuardContext,
|
|
25
|
+
maximumLifetimeSeconds = 300,
|
|
25
26
|
): ExecutionGrant {
|
|
26
27
|
this.prune();
|
|
27
28
|
if (this.#grants.size >= this.maxActiveGrants) {
|
|
@@ -29,7 +30,12 @@ export class ExecutionGrantStore {
|
|
|
29
30
|
}
|
|
30
31
|
const token = `grant_${randomUUIDv7().replaceAll("-", "")}`;
|
|
31
32
|
const expiresAt =
|
|
32
|
-
this.now() +
|
|
33
|
+
this.now() +
|
|
34
|
+
Math.min(
|
|
35
|
+
Math.max(operation.limits.maxDurationSeconds, maximumLifetimeSeconds),
|
|
36
|
+
7 * 24 * 60 * 60,
|
|
37
|
+
) *
|
|
38
|
+
1_000;
|
|
33
39
|
const grant = { token, operation, context, expiresAt };
|
|
34
40
|
this.#grants.set(token, grant);
|
|
35
41
|
return grant;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentRuntimeOperation,
|
|
3
|
+
AgentRuntimePort,
|
|
4
|
+
} from "./run-contracts.js";
|
|
5
|
+
|
|
6
|
+
export class FakeAgentRuntime implements AgentRuntimePort {
|
|
7
|
+
readonly started: AgentRuntimeOperation[] = [];
|
|
8
|
+
readonly startedIdempotencyKeys: string[] = [];
|
|
9
|
+
|
|
10
|
+
start(request: AgentRuntimeOperation, idempotencyKey: string) {
|
|
11
|
+
this.started.push(request);
|
|
12
|
+
this.startedIdempotencyKeys.push(idempotencyKey);
|
|
13
|
+
return Promise.resolve({
|
|
14
|
+
runId: "run_1",
|
|
15
|
+
status: "accepted" as const,
|
|
16
|
+
eventsUrl: "/internal/events",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
status(runId: string) {
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
runId,
|
|
23
|
+
status: "running" as const,
|
|
24
|
+
lastSequence: 0,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
events() {
|
|
29
|
+
return Promise.resolve(new Response());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
result() {
|
|
33
|
+
return Promise.resolve(new Response());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
cancel() {
|
|
37
|
+
return Promise.resolve(new Response());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
health() {
|
|
41
|
+
return Promise.resolve(new Response());
|
|
42
|
+
}
|
|
43
|
+
}
|