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,334 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { randomUUIDv7 } from "bun";
|
|
3
|
+
import type { DurableJobSnapshot, DurableJobs } from "@southwind-ai/jobs";
|
|
4
|
+
import type {
|
|
5
|
+
ModuleJobPort,
|
|
6
|
+
ModuleJobView,
|
|
7
|
+
} from "@southwind-ai/server-sdk";
|
|
8
|
+
import {
|
|
9
|
+
ModuleJobControlError,
|
|
10
|
+
requireModuleNamespace,
|
|
11
|
+
} from "@southwind-ai/server-sdk";
|
|
12
|
+
import type { AuditEvent } from "@southwind-ai/shared";
|
|
13
|
+
import { auditActor, type RequestGuardContext } from "../guards.js";
|
|
14
|
+
import type { createServerRuntime } from "../runtime.js";
|
|
15
|
+
import {
|
|
16
|
+
captureAuthorizationScope,
|
|
17
|
+
hashAuthorizationScope,
|
|
18
|
+
} from "../data-access/scope-snapshot.js";
|
|
19
|
+
|
|
20
|
+
type ServerRuntime = ReturnType<typeof createServerRuntime>;
|
|
21
|
+
|
|
22
|
+
export interface ModuleJobControlRuntime {
|
|
23
|
+
readonly worker: {
|
|
24
|
+
controlCancel(input: {
|
|
25
|
+
jobId: string;
|
|
26
|
+
expectedStatus?: ModuleJobView["status"];
|
|
27
|
+
expectedVersion?: string;
|
|
28
|
+
}): Promise<{
|
|
29
|
+
outcome: "changed" | "unchanged" | "not-found" | "conflict";
|
|
30
|
+
}>;
|
|
31
|
+
recover(input: {
|
|
32
|
+
jobId: string;
|
|
33
|
+
requestedBy: string;
|
|
34
|
+
reason: string;
|
|
35
|
+
idempotencyKey: string;
|
|
36
|
+
expectedAttempt: number;
|
|
37
|
+
expectedStatus: ModuleJobView["status"];
|
|
38
|
+
expectedVersion: string;
|
|
39
|
+
}): Promise<unknown>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ModuleJobPortInput {
|
|
44
|
+
readonly moduleName: string;
|
|
45
|
+
readonly guardContext: RequestGuardContext;
|
|
46
|
+
readonly runtime: ServerRuntime;
|
|
47
|
+
readonly jobs?: DurableJobs;
|
|
48
|
+
readonly control?: ModuleJobControlRuntime;
|
|
49
|
+
readonly reauthorize?: () => Promise<boolean>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createModuleJobPort(input: ModuleJobPortInput): ModuleJobPort {
|
|
53
|
+
return {
|
|
54
|
+
async submit(job) {
|
|
55
|
+
requireModuleNamespace("任务 handler", job.handlerKey, input.moduleName);
|
|
56
|
+
const jobs = requiredJobs(input);
|
|
57
|
+
const result = await jobs.submit({
|
|
58
|
+
...job,
|
|
59
|
+
queue: input.moduleName,
|
|
60
|
+
requestedBy: input.guardContext.actor.id,
|
|
61
|
+
authorizationScope: currentScope(input),
|
|
62
|
+
});
|
|
63
|
+
return { jobId: result.job.id, created: result.created };
|
|
64
|
+
},
|
|
65
|
+
async get(jobId) {
|
|
66
|
+
return toAuthorizedView(input, jobId);
|
|
67
|
+
},
|
|
68
|
+
async cancel(request) {
|
|
69
|
+
const reason = sanitizeReason(request.reason);
|
|
70
|
+
const current = await requireAuthorizedSnapshot(input, request.jobId);
|
|
71
|
+
const control = requiredControl(input);
|
|
72
|
+
const result = await control.worker.controlCancel({
|
|
73
|
+
jobId: current.job.id,
|
|
74
|
+
expectedStatus: request.expectedStatus,
|
|
75
|
+
expectedVersion: request.expectedVersion,
|
|
76
|
+
});
|
|
77
|
+
if (result.outcome === "conflict" || result.outcome === "not-found") {
|
|
78
|
+
await auditControl(
|
|
79
|
+
input,
|
|
80
|
+
"cancel",
|
|
81
|
+
"failure",
|
|
82
|
+
current,
|
|
83
|
+
reason,
|
|
84
|
+
result.outcome,
|
|
85
|
+
);
|
|
86
|
+
throw conflict();
|
|
87
|
+
}
|
|
88
|
+
const updated = await requireAuthorizedSnapshot(input, request.jobId);
|
|
89
|
+
await auditControl(
|
|
90
|
+
input,
|
|
91
|
+
"cancel",
|
|
92
|
+
"success",
|
|
93
|
+
updated,
|
|
94
|
+
reason,
|
|
95
|
+
result.outcome,
|
|
96
|
+
);
|
|
97
|
+
return toView(updated);
|
|
98
|
+
},
|
|
99
|
+
async recover(request) {
|
|
100
|
+
const reason = sanitizeReason(request.reason);
|
|
101
|
+
const current = await requireAuthorizedSnapshot(input, request.jobId);
|
|
102
|
+
const control = requiredControl(input);
|
|
103
|
+
try {
|
|
104
|
+
await control.worker.recover({
|
|
105
|
+
jobId: current.job.id,
|
|
106
|
+
requestedBy: input.guardContext.actor.id,
|
|
107
|
+
reason,
|
|
108
|
+
idempotencyKey: recoveryKey(input, request),
|
|
109
|
+
expectedAttempt: request.expectedAttempt,
|
|
110
|
+
expectedStatus: request.expectedStatus,
|
|
111
|
+
expectedVersion: request.expectedVersion,
|
|
112
|
+
});
|
|
113
|
+
} catch {
|
|
114
|
+
await auditControl(
|
|
115
|
+
input,
|
|
116
|
+
"recover",
|
|
117
|
+
"failure",
|
|
118
|
+
current,
|
|
119
|
+
reason,
|
|
120
|
+
"conflict",
|
|
121
|
+
);
|
|
122
|
+
throw conflict();
|
|
123
|
+
}
|
|
124
|
+
const updated = await requireAuthorizedSnapshot(input, request.jobId);
|
|
125
|
+
await auditControl(input, "recover", "success", updated, reason, "changed");
|
|
126
|
+
return toView(updated);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function toAuthorizedView(
|
|
132
|
+
input: ModuleJobPortInput,
|
|
133
|
+
jobId: string,
|
|
134
|
+
): Promise<ModuleJobView | undefined> {
|
|
135
|
+
if (input.reauthorize && !(await input.reauthorize())) return undefined;
|
|
136
|
+
const snapshot = await requiredJobs(input).get(jobId);
|
|
137
|
+
return snapshot && authorized(input, snapshot) ? toView(snapshot) : undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function requireAuthorizedSnapshot(
|
|
141
|
+
input: ModuleJobPortInput,
|
|
142
|
+
jobId: string,
|
|
143
|
+
) {
|
|
144
|
+
const snapshot = await toAuthorizedSnapshot(input, jobId);
|
|
145
|
+
if (snapshot) return snapshot;
|
|
146
|
+
await auditUnavailable(input, jobId);
|
|
147
|
+
throw new ModuleJobControlError("JOB_NOT_AVAILABLE", "任务不可用");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function toAuthorizedSnapshot(
|
|
151
|
+
input: ModuleJobPortInput,
|
|
152
|
+
jobId: string,
|
|
153
|
+
) {
|
|
154
|
+
if (input.reauthorize && !(await input.reauthorize())) return undefined;
|
|
155
|
+
const snapshot = await requiredJobs(input).get(jobId);
|
|
156
|
+
return snapshot && authorized(input, snapshot) ? snapshot : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function authorized(input: ModuleJobPortInput, snapshot: DurableJobSnapshot) {
|
|
160
|
+
const job = snapshot.job;
|
|
161
|
+
const submittedScope = job.authorizationScope;
|
|
162
|
+
return (
|
|
163
|
+
job.queue === input.moduleName &&
|
|
164
|
+
job.handlerKey.startsWith(`${input.moduleName}.`) &&
|
|
165
|
+
job.requestedBy === input.guardContext.actor.id &&
|
|
166
|
+
Boolean(submittedScope) &&
|
|
167
|
+
hashAuthorizationScope(submittedScope!) ===
|
|
168
|
+
hashAuthorizationScope(currentScope(input))
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toView(snapshot: DurableJobSnapshot): ModuleJobView {
|
|
173
|
+
const attempt = Math.max(
|
|
174
|
+
0,
|
|
175
|
+
...snapshot.stages.map((stage) => stage.currentAttempt),
|
|
176
|
+
);
|
|
177
|
+
const latestProgress = snapshot.progress
|
|
178
|
+
.slice()
|
|
179
|
+
.sort(
|
|
180
|
+
(left, right) =>
|
|
181
|
+
left.recordedAt.localeCompare(right.recordedAt) ||
|
|
182
|
+
left.sequence - right.sequence,
|
|
183
|
+
)
|
|
184
|
+
.at(-1);
|
|
185
|
+
const latestFailure = snapshot.failures.at(-1);
|
|
186
|
+
const activeLease = snapshot.leases.some((lease) => lease.status === "active");
|
|
187
|
+
return {
|
|
188
|
+
jobId: snapshot.job.id,
|
|
189
|
+
handlerKey: snapshot.job.handlerKey,
|
|
190
|
+
status: snapshot.job.status,
|
|
191
|
+
progress: latestProgress
|
|
192
|
+
? {
|
|
193
|
+
completedUnits: latestProgress.completedUnits,
|
|
194
|
+
totalUnits: latestProgress.totalUnits,
|
|
195
|
+
}
|
|
196
|
+
: undefined,
|
|
197
|
+
attempt,
|
|
198
|
+
maxAttempts: snapshot.job.maxAttempts,
|
|
199
|
+
createdAt: snapshot.job.createdAt,
|
|
200
|
+
startedAt: snapshot.job.startedAt,
|
|
201
|
+
finishedAt: snapshot.job.finishedAt,
|
|
202
|
+
failureCode: safeFailureCode(latestFailure?.code),
|
|
203
|
+
canCancel:
|
|
204
|
+
["queued", "running"].includes(snapshot.job.status) &&
|
|
205
|
+
!snapshot.job.cancelRequestedAt,
|
|
206
|
+
canRecover:
|
|
207
|
+
["failed", "cancelled"].includes(snapshot.job.status) && !activeLease,
|
|
208
|
+
version: snapshot.job.updatedAt,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function sanitizeReason(value: string) {
|
|
213
|
+
const reason = value
|
|
214
|
+
.trim()
|
|
215
|
+
.replace(/\s+/g, " ")
|
|
216
|
+
.replace(/[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}/g, "[邮箱]")
|
|
217
|
+
.replace(/\b\d{7,}\b/g, "[号码]")
|
|
218
|
+
.replace(/\b[A-Za-z0-9_-]{24,}\b/g, "[敏感内容]")
|
|
219
|
+
.slice(0, 200);
|
|
220
|
+
if (reason.length < 5) {
|
|
221
|
+
throw new ModuleJobControlError(
|
|
222
|
+
"JOB_REASON_INVALID",
|
|
223
|
+
"任务控制理由至少 5 个字符",
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return reason;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function safeFailureCode(code: string | undefined) {
|
|
230
|
+
if (!code) return undefined;
|
|
231
|
+
return /^[A-Z][A-Z0-9_.-]{0,79}$/.test(code) ? code : "JOB_FAILED";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function currentScope(input: ModuleJobPortInput) {
|
|
235
|
+
return captureAuthorizationScope(
|
|
236
|
+
input.guardContext.actor.scopeContext,
|
|
237
|
+
).snapshot;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function requiredJobs(input: ModuleJobPortInput) {
|
|
241
|
+
if (input.jobs) return input.jobs;
|
|
242
|
+
throw unavailable();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function requiredControl(input: ModuleJobPortInput) {
|
|
246
|
+
if (input.control) return input.control;
|
|
247
|
+
throw unavailable();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function unavailable() {
|
|
251
|
+
return new ModuleJobControlError(
|
|
252
|
+
"JOB_CONTROL_UNAVAILABLE",
|
|
253
|
+
"Durable Jobs 运行时未挂载",
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function conflict() {
|
|
258
|
+
return new ModuleJobControlError("JOB_STATE_CONFLICT", "任务状态已变化");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function recoveryKey(
|
|
262
|
+
input: ModuleJobPortInput,
|
|
263
|
+
request: {
|
|
264
|
+
jobId: string;
|
|
265
|
+
expectedAttempt: number;
|
|
266
|
+
expectedStatus: string;
|
|
267
|
+
expectedVersion: string;
|
|
268
|
+
},
|
|
269
|
+
) {
|
|
270
|
+
return createHash("sha256")
|
|
271
|
+
.update(
|
|
272
|
+
[
|
|
273
|
+
input.moduleName,
|
|
274
|
+
input.guardContext.actor.id,
|
|
275
|
+
request.jobId,
|
|
276
|
+
request.expectedAttempt,
|
|
277
|
+
request.expectedStatus,
|
|
278
|
+
request.expectedVersion,
|
|
279
|
+
].join(":"),
|
|
280
|
+
)
|
|
281
|
+
.digest("hex");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function auditUnavailable(input: ModuleJobPortInput, jobId: string) {
|
|
285
|
+
await input.runtime.auditSink.recordCriticalEvent(
|
|
286
|
+
auditEvent(input, "access", "denied", jobId),
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function auditControl(
|
|
291
|
+
input: ModuleJobPortInput,
|
|
292
|
+
operation: "cancel" | "recover",
|
|
293
|
+
outcome: "success" | "failure",
|
|
294
|
+
snapshot: DurableJobSnapshot,
|
|
295
|
+
reason: string,
|
|
296
|
+
result: string,
|
|
297
|
+
) {
|
|
298
|
+
await input.runtime.auditSink.recordCriticalEvent({
|
|
299
|
+
...auditEvent(input, operation, outcome, snapshot.job.id),
|
|
300
|
+
metadata: {
|
|
301
|
+
handlerKey: snapshot.job.handlerKey,
|
|
302
|
+
status: snapshot.job.status,
|
|
303
|
+
attempt: Math.max(
|
|
304
|
+
0,
|
|
305
|
+
...snapshot.stages.map((stage) => stage.currentAttempt),
|
|
306
|
+
),
|
|
307
|
+
attemptCount: snapshot.attempts.length,
|
|
308
|
+
failureCount: snapshot.failures.length,
|
|
309
|
+
failureCode: safeFailureCode(snapshot.failures.at(-1)?.code),
|
|
310
|
+
recoveryCount: snapshot.recoveries.length,
|
|
311
|
+
version: snapshot.job.updatedAt,
|
|
312
|
+
reason,
|
|
313
|
+
result,
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function auditEvent(
|
|
319
|
+
input: ModuleJobPortInput,
|
|
320
|
+
operation: string,
|
|
321
|
+
outcome: AuditEvent["outcome"],
|
|
322
|
+
jobId: string,
|
|
323
|
+
): AuditEvent {
|
|
324
|
+
return {
|
|
325
|
+
id: randomUUIDv7(),
|
|
326
|
+
action: `module.job.${operation}`,
|
|
327
|
+
outcome,
|
|
328
|
+
actor: auditActor(input.guardContext),
|
|
329
|
+
target: { type: "durable-job", id: jobId },
|
|
330
|
+
module: input.moduleName,
|
|
331
|
+
requestId: input.guardContext.requestId,
|
|
332
|
+
occurredAt: new Date().toISOString(),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
buildActorScopeContexts,
|
|
4
|
+
buildScopeContext,
|
|
5
|
+
} from "../data-access/context.js";
|
|
3
6
|
import type { RequestActor } from "../guards.js";
|
|
4
|
-
import {
|
|
7
|
+
import { createServerRuntime } from "../runtime.js";
|
|
8
|
+
import { manifest } from "./test-fixtures.js";
|
|
9
|
+
import {
|
|
10
|
+
buildModuleRequestContext,
|
|
11
|
+
requestActorFromModule,
|
|
12
|
+
toModuleActor,
|
|
13
|
+
} from "./request-context.js";
|
|
5
14
|
|
|
6
15
|
describe("模块 Actor 运行时边界", () => {
|
|
7
16
|
test("只下发 SDK 白名单字段并保留宿主内部身份映射", () => {
|
|
@@ -63,4 +72,81 @@ describe("模块 Actor 运行时边界", () => {
|
|
|
63
72
|
}),
|
|
64
73
|
).toThrow("不是由当前宿主颁发");
|
|
65
74
|
});
|
|
75
|
+
|
|
76
|
+
test("模块提交任务时由宿主持久化 Actor 与 nominal Scope 快照", async () => {
|
|
77
|
+
const submissions: unknown[] = [];
|
|
78
|
+
const actor: RequestActor = {
|
|
79
|
+
id: "user-job",
|
|
80
|
+
type: "user",
|
|
81
|
+
name: "任务用户",
|
|
82
|
+
roleCodes: ["operator"],
|
|
83
|
+
permissionKeys: ["evidence.read"],
|
|
84
|
+
...buildActorScopeContexts("user-job", "dept-evidence", [
|
|
85
|
+
{
|
|
86
|
+
id: "role-evidence",
|
|
87
|
+
code: "operator",
|
|
88
|
+
dataScope: "department",
|
|
89
|
+
departmentIds: [],
|
|
90
|
+
},
|
|
91
|
+
]),
|
|
92
|
+
};
|
|
93
|
+
const context = buildModuleRequestContext({
|
|
94
|
+
moduleName: "evidence",
|
|
95
|
+
manifest: manifest("evidence"),
|
|
96
|
+
guardContext: {
|
|
97
|
+
requestId: "request-job",
|
|
98
|
+
actor,
|
|
99
|
+
startedAt: "2026-07-27T00:00:00.000Z",
|
|
100
|
+
},
|
|
101
|
+
params: {},
|
|
102
|
+
query: {},
|
|
103
|
+
runtime: createServerRuntime(),
|
|
104
|
+
jobs: {
|
|
105
|
+
async get() {
|
|
106
|
+
return undefined;
|
|
107
|
+
},
|
|
108
|
+
async list() {
|
|
109
|
+
return [];
|
|
110
|
+
},
|
|
111
|
+
async submit(input) {
|
|
112
|
+
submissions.push(input);
|
|
113
|
+
return {
|
|
114
|
+
job: {
|
|
115
|
+
...input,
|
|
116
|
+
id: "job-1",
|
|
117
|
+
queue: input.queue || "default",
|
|
118
|
+
status: "queued",
|
|
119
|
+
priority: 0,
|
|
120
|
+
maxAttempts: 3,
|
|
121
|
+
timeoutSeconds: 900,
|
|
122
|
+
availableAt: "2026-07-27T00:00:00.000Z",
|
|
123
|
+
createdAt: "2026-07-27T00:00:00.000Z",
|
|
124
|
+
updatedAt: "2026-07-27T00:00:00.000Z",
|
|
125
|
+
},
|
|
126
|
+
created: true,
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await context.jobs.submit({
|
|
133
|
+
handlerKey: "evidence.parse",
|
|
134
|
+
idempotencyKey: "parse-1",
|
|
135
|
+
payload: { evidenceFileId: "evidence-1" },
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(submissions).toEqual([
|
|
139
|
+
expect.objectContaining({
|
|
140
|
+
requestedBy: "user-job",
|
|
141
|
+
queue: "evidence",
|
|
142
|
+
authorizationScope: {
|
|
143
|
+
tenantId: "single-tenant",
|
|
144
|
+
actorId: "user-job",
|
|
145
|
+
departmentId: "dept-evidence",
|
|
146
|
+
grants: [{ scope: "department", departmentId: "dept-evidence" }],
|
|
147
|
+
roleIds: ["role-evidence"],
|
|
148
|
+
},
|
|
149
|
+
}),
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
66
152
|
});
|
|
@@ -2,16 +2,18 @@ import { randomUUIDv7 } from "bun";
|
|
|
2
2
|
import type { DurableJobs } from "@southwind-ai/jobs";
|
|
3
3
|
import type {
|
|
4
4
|
ModuleActor,
|
|
5
|
+
ModuleAiArtifactPort,
|
|
6
|
+
ModuleAiOperationPort,
|
|
5
7
|
ModuleAuditEmitInput,
|
|
6
8
|
ModuleAuditEmitter,
|
|
7
|
-
ModuleJobSubmitter,
|
|
8
9
|
ModuleRequestContext,
|
|
9
10
|
ModuleStoragePort,
|
|
10
11
|
ModuleUserDirectoryPort,
|
|
11
12
|
} from "@southwind-ai/server-sdk";
|
|
12
13
|
import {
|
|
14
|
+
ModuleAiArtifactError,
|
|
15
|
+
ModuleAiOperationError,
|
|
13
16
|
ModuleUserDirectoryError,
|
|
14
|
-
requireModuleNamespace,
|
|
15
17
|
} from "@southwind-ai/server-sdk";
|
|
16
18
|
import type { AuditEvent } from "@southwind-ai/shared";
|
|
17
19
|
import type { ModuleManifest } from "@southwind-ai/modules";
|
|
@@ -22,6 +24,10 @@ import {
|
|
|
22
24
|
} from "../guards.js";
|
|
23
25
|
import type { createServerRuntime } from "../runtime.js";
|
|
24
26
|
import { unavailableModuleStoragePort } from "./storage-port.js";
|
|
27
|
+
import {
|
|
28
|
+
createModuleJobPort,
|
|
29
|
+
type ModuleJobControlRuntime,
|
|
30
|
+
} from "./job-port.js";
|
|
25
31
|
|
|
26
32
|
type ServerRuntime = ReturnType<typeof createServerRuntime>;
|
|
27
33
|
|
|
@@ -66,8 +72,12 @@ export interface ModuleRequestContextInput {
|
|
|
66
72
|
body?: unknown;
|
|
67
73
|
runtime: ServerRuntime;
|
|
68
74
|
jobs?: DurableJobs;
|
|
75
|
+
jobControl?: ModuleJobControlRuntime;
|
|
76
|
+
reauthorizeJobs?: () => Promise<boolean>;
|
|
69
77
|
storage?: ModuleStoragePort;
|
|
70
78
|
userDirectory?: ModuleUserDirectoryPort;
|
|
79
|
+
aiArtifacts?: ModuleAiArtifactPort;
|
|
80
|
+
aiOperations?: ModuleAiOperationPort;
|
|
71
81
|
}
|
|
72
82
|
|
|
73
83
|
export function buildModuleRequestContext(
|
|
@@ -80,13 +90,56 @@ export function buildModuleRequestContext(
|
|
|
80
90
|
query: input.query,
|
|
81
91
|
body: input.body,
|
|
82
92
|
audit: createModuleAuditEmitter(input),
|
|
83
|
-
|
|
93
|
+
aiArtifacts:
|
|
94
|
+
input.aiArtifacts ?? unavailableModuleAiArtifactPort(input.moduleName),
|
|
95
|
+
aiOperations:
|
|
96
|
+
input.aiOperations ?? unavailableModuleAiOperationPort(input.moduleName),
|
|
97
|
+
jobs: createModuleJobPort({
|
|
98
|
+
moduleName: input.moduleName,
|
|
99
|
+
guardContext: input.guardContext,
|
|
100
|
+
runtime: input.runtime,
|
|
101
|
+
jobs: input.jobs,
|
|
102
|
+
control: input.jobControl,
|
|
103
|
+
reauthorize: input.reauthorizeJobs,
|
|
104
|
+
}),
|
|
84
105
|
storage: input.storage ?? unavailableModuleStoragePort(input.moduleName),
|
|
85
106
|
userDirectory:
|
|
86
107
|
input.userDirectory ?? unavailableModuleUserDirectory(input.moduleName),
|
|
87
108
|
};
|
|
88
109
|
}
|
|
89
110
|
|
|
111
|
+
function unavailableModuleAiOperationPort(
|
|
112
|
+
moduleName: string,
|
|
113
|
+
): ModuleAiOperationPort {
|
|
114
|
+
const unavailable = (): never => {
|
|
115
|
+
throw new ModuleAiOperationError(
|
|
116
|
+
"AI_OPERATION_UNAVAILABLE",
|
|
117
|
+
`模块 ${moduleName} 的 AI Operation Port 未挂载`,
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
startAgent: async () => unavailable(),
|
|
122
|
+
startDocument: async () => unavailable(),
|
|
123
|
+
startSpeech: async () => unavailable(),
|
|
124
|
+
embed: async () => unavailable(),
|
|
125
|
+
result: async () => unavailable(),
|
|
126
|
+
cancel: async () => unavailable(),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function unavailableModuleAiArtifactPort(
|
|
131
|
+
moduleName: string,
|
|
132
|
+
): ModuleAiArtifactPort {
|
|
133
|
+
return {
|
|
134
|
+
async issue() {
|
|
135
|
+
throw new ModuleAiArtifactError(
|
|
136
|
+
"AI_ARTIFACT_UNAVAILABLE",
|
|
137
|
+
`模块 ${moduleName} 的 AI Artifact Port 未挂载`,
|
|
138
|
+
);
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
90
143
|
function unavailableModuleUserDirectory(
|
|
91
144
|
moduleName: string,
|
|
92
145
|
): ModuleUserDirectoryPort {
|
|
@@ -141,22 +194,3 @@ function buildAuditEvent(
|
|
|
141
194
|
occurredAt: new Date().toISOString(),
|
|
142
195
|
};
|
|
143
196
|
}
|
|
144
|
-
|
|
145
|
-
function createModuleJobSubmitter(
|
|
146
|
-
input: ModuleRequestContextInput,
|
|
147
|
-
): ModuleJobSubmitter {
|
|
148
|
-
return {
|
|
149
|
-
async submit(job) {
|
|
150
|
-
requireModuleNamespace("任务 handler", job.handlerKey, input.moduleName);
|
|
151
|
-
if (!input.jobs) {
|
|
152
|
-
throw new Error("Durable Jobs 运行时未挂载");
|
|
153
|
-
}
|
|
154
|
-
const result = await input.jobs.submit({
|
|
155
|
-
...job,
|
|
156
|
-
queue: input.moduleName,
|
|
157
|
-
requestedBy: input.guardContext.actor.id,
|
|
158
|
-
});
|
|
159
|
-
return { jobId: result.job.id, created: result.created };
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
}
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ModuleRequestContext,
|
|
7
7
|
} from "@southwind-ai/server-sdk";
|
|
8
8
|
import { featureKey, licenseVersionKey } from "@southwind-ai/shared";
|
|
9
|
+
import { buildScopeContext } from "../data-access/context.js";
|
|
9
10
|
import type { RequestActor } from "../guards.js";
|
|
10
11
|
import { createServerRuntime } from "../runtime.js";
|
|
11
12
|
import { createStorageRuntime } from "../storage/runtime.js";
|
|
@@ -241,6 +242,7 @@ async function createFixture(options: FixtureOptions = {}) {
|
|
|
241
242
|
invoke(method: string, path: string, params = {}, body?: unknown) {
|
|
242
243
|
return app.invoke(method, path, {
|
|
243
244
|
guardContext,
|
|
245
|
+
request: new Request(`http://localhost${path}`),
|
|
244
246
|
params,
|
|
245
247
|
query: {},
|
|
246
248
|
body,
|
|
@@ -258,6 +260,14 @@ function actor(
|
|
|
258
260
|
name: "测试管理员",
|
|
259
261
|
roleCodes: ["developer"],
|
|
260
262
|
permissionKeys,
|
|
263
|
+
scopeContext: buildScopeContext("user_admin", undefined, [
|
|
264
|
+
{
|
|
265
|
+
id: "developer",
|
|
266
|
+
code: "developer",
|
|
267
|
+
dataScope: "self",
|
|
268
|
+
departmentIds: [],
|
|
269
|
+
},
|
|
270
|
+
]),
|
|
261
271
|
};
|
|
262
272
|
}
|
|
263
273
|
|
|
@@ -5,6 +5,10 @@ import { forbidden, routeContext } from "../system/route-helpers.js";
|
|
|
5
5
|
import type { RouteApp } from "../system/route-types.js";
|
|
6
6
|
import type { createServerRuntime } from "../runtime.js";
|
|
7
7
|
import type { InitializedModuleHosts } from "./initialize.js";
|
|
8
|
+
import type {
|
|
9
|
+
ModuleAiArtifactPort,
|
|
10
|
+
ModuleAiOperationPort,
|
|
11
|
+
} from "@southwind-ai/server-sdk";
|
|
8
12
|
import { buildModuleRequestContext } from "./request-context.js";
|
|
9
13
|
import {
|
|
10
14
|
createModuleStoragePort,
|
|
@@ -15,6 +19,7 @@ import {
|
|
|
15
19
|
type ModuleUserDirectoryServices,
|
|
16
20
|
} from "./user-directory-port.js";
|
|
17
21
|
import { createModuleStorageScope } from "./storage-scope.js";
|
|
22
|
+
import type { ModuleJobControlRuntime } from "./job-port.js";
|
|
18
23
|
|
|
19
24
|
type ServerRuntime = ReturnType<typeof createServerRuntime>;
|
|
20
25
|
|
|
@@ -24,10 +29,21 @@ export interface ModuleRouteInstallContext {
|
|
|
24
29
|
runtime: ServerRuntime;
|
|
25
30
|
features: readonly FeatureFlagDefinition[];
|
|
26
31
|
jobs?: DurableJobs;
|
|
32
|
+
jobControl?: ModuleJobControlRuntime;
|
|
27
33
|
/** 平台存储服务;提供后模块获得受限 Upload/BlobRead Port。 */
|
|
28
34
|
storage?: ModuleStorageServices;
|
|
29
35
|
/** 平台身份目录;Adapter 会绑定当前请求 Actor 与服务端数据范围。 */
|
|
30
36
|
userDirectory?: Omit<ModuleUserDirectoryServices, "runtime" | "features">;
|
|
37
|
+
aiArtifacts?: (
|
|
38
|
+
moduleName: string,
|
|
39
|
+
guardContext: ReturnType<typeof routeContext>["guardContext"],
|
|
40
|
+
signal: AbortSignal,
|
|
41
|
+
) => ModuleAiArtifactPort;
|
|
42
|
+
aiOperations?: (
|
|
43
|
+
moduleName: string,
|
|
44
|
+
guardContext: ReturnType<typeof routeContext>["guardContext"],
|
|
45
|
+
signal: AbortSignal,
|
|
46
|
+
) => ModuleAiOperationPort;
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
/**
|
|
@@ -73,7 +89,26 @@ export function installModuleRoutes(
|
|
|
73
89
|
query: route.query,
|
|
74
90
|
body: route.body,
|
|
75
91
|
runtime: context.runtime,
|
|
92
|
+
aiArtifacts: context.aiArtifacts?.(
|
|
93
|
+
moduleName,
|
|
94
|
+
route.guardContext,
|
|
95
|
+
route.request.signal,
|
|
96
|
+
),
|
|
97
|
+
aiOperations: context.aiOperations?.(
|
|
98
|
+
moduleName,
|
|
99
|
+
route.guardContext,
|
|
100
|
+
route.request.signal,
|
|
101
|
+
),
|
|
76
102
|
jobs: context.jobs,
|
|
103
|
+
jobControl: context.jobControl,
|
|
104
|
+
reauthorizeJobs: async () =>
|
|
105
|
+
(
|
|
106
|
+
await evaluateRouteGuards(
|
|
107
|
+
context.runtime,
|
|
108
|
+
route.guardContext,
|
|
109
|
+
{ permission: binding, feature },
|
|
110
|
+
)
|
|
111
|
+
).allowed,
|
|
77
112
|
storage: context.storage
|
|
78
113
|
? createModuleStoragePort(context.storage, {
|
|
79
114
|
...createModuleStorageScope({
|
|
@@ -84,7 +119,7 @@ export function installModuleRoutes(
|
|
|
84
119
|
guardContext: route.guardContext,
|
|
85
120
|
runtime: context.runtime,
|
|
86
121
|
features: context.features,
|
|
87
|
-
signal:
|
|
122
|
+
signal: route.request.signal,
|
|
88
123
|
}),
|
|
89
124
|
})
|
|
90
125
|
: undefined,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ModuleManifest } from "@southwind-ai/modules";
|
|
2
|
+
import {
|
|
3
|
+
requireModuleNamespace,
|
|
4
|
+
type ModuleSandboxToolHost,
|
|
5
|
+
} from "@southwind-ai/server-sdk";
|
|
6
|
+
import type { RegisteredModuleSandboxToolHandler } from "./initialize-contracts.js";
|
|
7
|
+
|
|
8
|
+
export function createScopedSandboxHost(
|
|
9
|
+
manifest: ModuleManifest,
|
|
10
|
+
state: {
|
|
11
|
+
toolKeys: Set<string>;
|
|
12
|
+
sandboxToolHandlers: RegisteredModuleSandboxToolHandler[];
|
|
13
|
+
},
|
|
14
|
+
): ModuleSandboxToolHost {
|
|
15
|
+
return {
|
|
16
|
+
moduleName: manifest.name,
|
|
17
|
+
registerToolHandlers(registrations) {
|
|
18
|
+
for (const registration of registrations) {
|
|
19
|
+
requireModuleNamespace(
|
|
20
|
+
"Sandbox Agent Tool",
|
|
21
|
+
registration.toolKey,
|
|
22
|
+
manifest.name,
|
|
23
|
+
);
|
|
24
|
+
if (!manifest.tools.some(({ key }) => key === registration.toolKey)) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Sandbox Agent Tool Handler 未在 Manifest 声明:${registration.toolKey}`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
if (state.toolKeys.has(registration.toolKey)) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Agent Tool Handler 重复注册:${registration.toolKey}`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
state.toolKeys.add(registration.toolKey);
|
|
35
|
+
state.sandboxToolHandlers.push({
|
|
36
|
+
moduleName: manifest.name,
|
|
37
|
+
registration,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|