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,312 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { DurableHandlerContext } from "@southwind-ai/jobs";
|
|
3
|
+
import { featureKey } from "@southwind-ai/shared";
|
|
4
|
+
import { buildActorScopeContexts } from "../data-access/context.js";
|
|
5
|
+
import { captureAuthorizationScope } from "../data-access/scope-snapshot.js";
|
|
6
|
+
import type { RequestActor } from "../guards.js";
|
|
7
|
+
import { createServerRuntime } from "../runtime.js";
|
|
8
|
+
import { createStorageRuntime } from "../storage/runtime.js";
|
|
9
|
+
import { createModuleDurableContextualizer } from "./durable-context.js";
|
|
10
|
+
import { initializeModuleHosts } from "./initialize.js";
|
|
11
|
+
import { manifest } from "./test-fixtures.js";
|
|
12
|
+
|
|
13
|
+
const evidenceFeature = {
|
|
14
|
+
key: featureKey("evidence.catalog"),
|
|
15
|
+
label: "电子物证",
|
|
16
|
+
module: "evidence",
|
|
17
|
+
visible: "visible" as const,
|
|
18
|
+
enabled: true,
|
|
19
|
+
stage: "stable" as const,
|
|
20
|
+
};
|
|
21
|
+
const evidenceManifest = manifest("evidence", {
|
|
22
|
+
features: [evidenceFeature],
|
|
23
|
+
permissions: [
|
|
24
|
+
{
|
|
25
|
+
key: "evidence.read",
|
|
26
|
+
label: "读取物证",
|
|
27
|
+
module: "evidence",
|
|
28
|
+
moduleLabel: "物证",
|
|
29
|
+
resource: "file",
|
|
30
|
+
resourceLabel: "文件",
|
|
31
|
+
action: "read",
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
auditActions: ["evidence.file.read"],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("模块 Durable Handler 授权上下文", () => {
|
|
38
|
+
test("后台 AI Port 固定当前模块与重建后的 Actor", async () => {
|
|
39
|
+
const calls: unknown[] = [];
|
|
40
|
+
const fixture = await setup(currentActor, {
|
|
41
|
+
create(moduleName, context) {
|
|
42
|
+
return {
|
|
43
|
+
async enqueueOperation(input) {
|
|
44
|
+
calls.push({
|
|
45
|
+
moduleName,
|
|
46
|
+
actorId: context.actor.id,
|
|
47
|
+
operationKey: input.operationKey,
|
|
48
|
+
});
|
|
49
|
+
return { runId: "run-1", output: { summary: "完成" } };
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const context = await fixture.contextualize({
|
|
55
|
+
claim: { job: job(fixture.actor) },
|
|
56
|
+
moduleName: "evidence",
|
|
57
|
+
context: baseContext(1),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await (
|
|
61
|
+
context as typeof context & {
|
|
62
|
+
ai: {
|
|
63
|
+
enqueueOperation(input: {
|
|
64
|
+
operationKey: string;
|
|
65
|
+
input: null;
|
|
66
|
+
idempotencyKey: string;
|
|
67
|
+
}): Promise<unknown>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
).ai.enqueueOperation({
|
|
71
|
+
operationKey: "evidence.summarize",
|
|
72
|
+
input: null,
|
|
73
|
+
idempotencyKey: "summary-1",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(calls).toEqual([
|
|
77
|
+
{
|
|
78
|
+
moduleName: "evidence",
|
|
79
|
+
actorId: "user-reader",
|
|
80
|
+
operationKey: "evidence.summarize",
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("重启后按 requestedBy 重建 Actor 并允许同案跨上传者读取", async () => {
|
|
86
|
+
const { contextualize, storage, actor, resolverCalls, runtime } =
|
|
87
|
+
await setup();
|
|
88
|
+
const file = await seedFile(storage);
|
|
89
|
+
const persistedJob = job(actor);
|
|
90
|
+
|
|
91
|
+
const first = await contextualize({
|
|
92
|
+
claim: { job: persistedJob },
|
|
93
|
+
moduleName: "evidence",
|
|
94
|
+
context: baseContext(1),
|
|
95
|
+
});
|
|
96
|
+
const restarted = await contextualize({
|
|
97
|
+
claim: { job: persistedJob },
|
|
98
|
+
moduleName: "evidence",
|
|
99
|
+
context: baseContext(2),
|
|
100
|
+
});
|
|
101
|
+
const opened = await (
|
|
102
|
+
restarted as typeof restarted & {
|
|
103
|
+
storage: {
|
|
104
|
+
openResourceFile(input: {
|
|
105
|
+
fileId: string;
|
|
106
|
+
purpose: string;
|
|
107
|
+
resourceType: string;
|
|
108
|
+
resourceId: string;
|
|
109
|
+
}): Promise<{ id: string; body: ReadableStream<Uint8Array> }>;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
).storage.openResourceFile({
|
|
113
|
+
fileId: file.id,
|
|
114
|
+
purpose: "evidence.raw",
|
|
115
|
+
resourceType: "evidence.evidence-file",
|
|
116
|
+
resourceId: "evidence-1",
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(first).toMatchObject({
|
|
120
|
+
actor: { id: "user-reader" },
|
|
121
|
+
requestId: expect.any(String),
|
|
122
|
+
});
|
|
123
|
+
expect(opened.id).toBe(file.id);
|
|
124
|
+
expect("artifactRef" in opened).toBe(false);
|
|
125
|
+
expect(resolverCalls()).toBe(2);
|
|
126
|
+
expect(runtime.auditSink.list()).toContainEqual(
|
|
127
|
+
expect.objectContaining({
|
|
128
|
+
action: "evidence.file.read",
|
|
129
|
+
outcome: "success",
|
|
130
|
+
target: { type: "evidence.evidence-file", id: "evidence-1" },
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("Scope 变化、权限撤销与资源绑定漂移均 fail-closed", async () => {
|
|
136
|
+
const stable = currentActor();
|
|
137
|
+
const scopeChanged = currentActor("self");
|
|
138
|
+
const setupScopeChanged = await setup(() => scopeChanged);
|
|
139
|
+
await expect(
|
|
140
|
+
setupScopeChanged.contextualize({
|
|
141
|
+
claim: { job: job(stable) },
|
|
142
|
+
moduleName: "evidence",
|
|
143
|
+
context: baseContext(1),
|
|
144
|
+
}),
|
|
145
|
+
).rejects.toMatchObject({
|
|
146
|
+
code: "MODULE_AUTHORIZATION_SCOPE_CHANGED",
|
|
147
|
+
retryable: false,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const permissionRevoked = { ...stable, permissionKeys: [] };
|
|
151
|
+
const revoked = await setup(() => permissionRevoked);
|
|
152
|
+
const file = await seedFile(revoked.storage);
|
|
153
|
+
const context = await revoked.contextualize({
|
|
154
|
+
claim: { job: job(stable) },
|
|
155
|
+
moduleName: "evidence",
|
|
156
|
+
context: baseContext(1),
|
|
157
|
+
});
|
|
158
|
+
const storage = (
|
|
159
|
+
context as typeof context & {
|
|
160
|
+
storage: {
|
|
161
|
+
openResourceFile(input: {
|
|
162
|
+
fileId: string;
|
|
163
|
+
purpose: string;
|
|
164
|
+
resourceType: string;
|
|
165
|
+
resourceId: string;
|
|
166
|
+
}): Promise<unknown>;
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
).storage;
|
|
170
|
+
await expect(
|
|
171
|
+
storage.openResourceFile({
|
|
172
|
+
fileId: file.id,
|
|
173
|
+
purpose: "evidence.raw",
|
|
174
|
+
resourceType: "evidence.evidence-file",
|
|
175
|
+
resourceId: "evidence-1",
|
|
176
|
+
}),
|
|
177
|
+
).rejects.toThrow("资源文件访问未授权");
|
|
178
|
+
expect(revoked.runtime.auditSink.list()).toContainEqual(
|
|
179
|
+
expect.objectContaining({
|
|
180
|
+
action: "evidence.file.read",
|
|
181
|
+
outcome: "denied",
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
async function setup(
|
|
188
|
+
resolve: () => RequestActor = currentActor,
|
|
189
|
+
backgroundAi?: Parameters<
|
|
190
|
+
typeof createModuleDurableContextualizer
|
|
191
|
+
>[0]["backgroundAi"],
|
|
192
|
+
) {
|
|
193
|
+
const hosts = await initializeModuleHosts({
|
|
194
|
+
modules: [evidenceManifest],
|
|
195
|
+
installations: [
|
|
196
|
+
{
|
|
197
|
+
manifest: evidenceManifest,
|
|
198
|
+
setup(host) {
|
|
199
|
+
host.registerUploadPolicy({
|
|
200
|
+
purpose: "evidence.raw",
|
|
201
|
+
maxByteSize: 1024,
|
|
202
|
+
maxPartByteSize: 512,
|
|
203
|
+
sessionTtlMs: 60_000,
|
|
204
|
+
allowedTypes: { "text/plain": ["txt"] },
|
|
205
|
+
});
|
|
206
|
+
host.registerResourceFileAccessPolicies([
|
|
207
|
+
{
|
|
208
|
+
purpose: "evidence.raw",
|
|
209
|
+
resourceType: "evidence.evidence-file",
|
|
210
|
+
featureKey: "evidence.catalog",
|
|
211
|
+
permissionKey: "evidence.read",
|
|
212
|
+
auditAction: "evidence.file.read",
|
|
213
|
+
authorize: ({ fileId, resourceId }, context) =>
|
|
214
|
+
fileId.startsWith("file_") &&
|
|
215
|
+
resourceId === "evidence-1" &&
|
|
216
|
+
context.actor.id === "user-reader",
|
|
217
|
+
},
|
|
218
|
+
]);
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
});
|
|
223
|
+
const storage = createStorageRuntime({}, undefined, hosts.uploadPolicies);
|
|
224
|
+
const runtime = createServerRuntime(
|
|
225
|
+
{ NODE_ENV: "test" },
|
|
226
|
+
{ featureCatalog: [evidenceFeature] },
|
|
227
|
+
);
|
|
228
|
+
let calls = 0;
|
|
229
|
+
return {
|
|
230
|
+
actor: currentActor(),
|
|
231
|
+
storage,
|
|
232
|
+
runtime,
|
|
233
|
+
resolverCalls: () => calls,
|
|
234
|
+
contextualize: createModuleDurableContextualizer({
|
|
235
|
+
hosts,
|
|
236
|
+
runtime,
|
|
237
|
+
features: [evidenceFeature],
|
|
238
|
+
storage,
|
|
239
|
+
backgroundAi,
|
|
240
|
+
async resolveActor() {
|
|
241
|
+
calls += 1;
|
|
242
|
+
return resolve();
|
|
243
|
+
},
|
|
244
|
+
}),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function currentActor(scope: "all" | "self" = "all"): RequestActor {
|
|
249
|
+
return {
|
|
250
|
+
id: "user-reader",
|
|
251
|
+
type: "user",
|
|
252
|
+
name: "物证读取者",
|
|
253
|
+
roleCodes: ["investigator"],
|
|
254
|
+
permissionKeys: ["evidence.read"],
|
|
255
|
+
...buildActorScopeContexts("user-reader", "dept-case", [
|
|
256
|
+
{
|
|
257
|
+
id: "role-investigator",
|
|
258
|
+
code: "investigator",
|
|
259
|
+
dataScope: scope,
|
|
260
|
+
departmentIds: [],
|
|
261
|
+
},
|
|
262
|
+
]),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function job(actor: RequestActor) {
|
|
267
|
+
return {
|
|
268
|
+
id: "job-evidence",
|
|
269
|
+
requestedBy: actor.id,
|
|
270
|
+
authorizationScope: captureAuthorizationScope(actor.scopeContext).snapshot,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function baseContext(attemptNumber: number): DurableHandlerContext {
|
|
275
|
+
return {
|
|
276
|
+
jobId: "job-evidence",
|
|
277
|
+
stageKey: "main",
|
|
278
|
+
attemptNumber,
|
|
279
|
+
signal: new AbortController().signal,
|
|
280
|
+
heartbeat: async () => undefined,
|
|
281
|
+
progress: async () => undefined,
|
|
282
|
+
publishArtifact: async () => undefined,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function seedFile(storage: ReturnType<typeof createStorageRuntime>) {
|
|
287
|
+
const bytes = new TextEncoder().encode("evidence");
|
|
288
|
+
const sha256 = new Bun.CryptoHasher("sha256").update(bytes).digest("hex");
|
|
289
|
+
const session = await storage.uploads.create({
|
|
290
|
+
ownerId: "user-uploader",
|
|
291
|
+
purpose: "evidence.raw",
|
|
292
|
+
filename: "evidence.txt",
|
|
293
|
+
mimeType: "text/plain",
|
|
294
|
+
expectedByteSize: bytes.length,
|
|
295
|
+
expectedSha256: sha256,
|
|
296
|
+
});
|
|
297
|
+
await storage.uploads.appendPart({
|
|
298
|
+
sessionId: session.id,
|
|
299
|
+
ownerId: "user-uploader",
|
|
300
|
+
partNumber: 0,
|
|
301
|
+
byteOffset: 0,
|
|
302
|
+
byteSize: bytes.length,
|
|
303
|
+
sha256,
|
|
304
|
+
body: new ReadableStream({
|
|
305
|
+
start(controller) {
|
|
306
|
+
controller.enqueue(bytes);
|
|
307
|
+
controller.close();
|
|
308
|
+
},
|
|
309
|
+
}),
|
|
310
|
+
});
|
|
311
|
+
return storage.uploads.complete(session.id, "user-uploader");
|
|
312
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DurableHandlerError,
|
|
3
|
+
type DurableHandlerContext,
|
|
4
|
+
} from "@southwind-ai/jobs";
|
|
5
|
+
import type {
|
|
6
|
+
AuthorizationScopeSnapshot,
|
|
7
|
+
FeatureFlagDefinition,
|
|
8
|
+
} from "@southwind-ai/shared";
|
|
9
|
+
import type {
|
|
10
|
+
ModuleBackgroundAiPort,
|
|
11
|
+
ModuleDurableHandlerContext,
|
|
12
|
+
} from "@southwind-ai/server-sdk";
|
|
13
|
+
import { ModuleBackgroundAiError } from "@southwind-ai/server-sdk";
|
|
14
|
+
import {
|
|
15
|
+
captureAuthorizationScope,
|
|
16
|
+
hashAuthorizationScope,
|
|
17
|
+
} from "../data-access/scope-snapshot.js";
|
|
18
|
+
import type { RequestActor, RequestGuardContext } from "../guards.js";
|
|
19
|
+
import type { createServerRuntime } from "../runtime.js";
|
|
20
|
+
import type { InitializedModuleHosts } from "./initialize.js";
|
|
21
|
+
import {
|
|
22
|
+
createModuleStoragePort,
|
|
23
|
+
type ModuleStorageServices,
|
|
24
|
+
} from "./storage-port.js";
|
|
25
|
+
import { createModuleStorageScope } from "./storage-scope.js";
|
|
26
|
+
import { toModuleActor } from "./request-context.js";
|
|
27
|
+
|
|
28
|
+
export interface ModuleDurableContextServices {
|
|
29
|
+
readonly hosts: InitializedModuleHosts;
|
|
30
|
+
readonly runtime: ReturnType<typeof createServerRuntime>;
|
|
31
|
+
readonly features: readonly FeatureFlagDefinition[];
|
|
32
|
+
readonly storage: ModuleStorageServices;
|
|
33
|
+
readonly backgroundAi?: {
|
|
34
|
+
create(
|
|
35
|
+
moduleName: string,
|
|
36
|
+
context: RequestGuardContext,
|
|
37
|
+
signal: AbortSignal,
|
|
38
|
+
): ModuleBackgroundAiPort;
|
|
39
|
+
};
|
|
40
|
+
resolveActor(actorId: string): Promise<RequestActor | undefined>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function createModuleDurableContextualizer(
|
|
44
|
+
services: ModuleDurableContextServices,
|
|
45
|
+
) {
|
|
46
|
+
return async (input: {
|
|
47
|
+
claim: {
|
|
48
|
+
job: {
|
|
49
|
+
id: string;
|
|
50
|
+
requestedBy: string;
|
|
51
|
+
authorizationScope?: AuthorizationScopeSnapshot;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
moduleName?: string;
|
|
55
|
+
context: DurableHandlerContext;
|
|
56
|
+
}): Promise<DurableHandlerContext> => {
|
|
57
|
+
if (!input.moduleName) return input.context;
|
|
58
|
+
const snapshot = input.claim.job.authorizationScope;
|
|
59
|
+
if (!snapshot || snapshot.actorId !== input.claim.job.requestedBy) {
|
|
60
|
+
throw denied("MODULE_AUTHORIZATION_SCOPE_MISSING");
|
|
61
|
+
}
|
|
62
|
+
const actor = await services.resolveActor(input.claim.job.requestedBy);
|
|
63
|
+
if (!actor?.scopeContext) {
|
|
64
|
+
throw denied("MODULE_ACTOR_UNAVAILABLE");
|
|
65
|
+
}
|
|
66
|
+
const current = captureAuthorizationScope(actor.scopeContext);
|
|
67
|
+
if (current.hash !== hashAuthorizationScope(snapshot)) {
|
|
68
|
+
throw denied("MODULE_AUTHORIZATION_SCOPE_CHANGED");
|
|
69
|
+
}
|
|
70
|
+
const request = new Request(
|
|
71
|
+
`http://durable.internal/jobs/${encodeURIComponent(input.claim.job.id)}`,
|
|
72
|
+
{ signal: input.context.signal },
|
|
73
|
+
);
|
|
74
|
+
const guardContext = await services.runtime.createGuardContextForActor(
|
|
75
|
+
request,
|
|
76
|
+
actor,
|
|
77
|
+
);
|
|
78
|
+
const storage = createModuleStoragePort(
|
|
79
|
+
services.storage,
|
|
80
|
+
createModuleStorageScope({
|
|
81
|
+
moduleName: input.moduleName,
|
|
82
|
+
purposes: services.hosts.uploadPurposesOf(input.moduleName),
|
|
83
|
+
resourceFileAccessPolicies: services.hosts.resourceFileAccessPoliciesOf(
|
|
84
|
+
input.moduleName,
|
|
85
|
+
),
|
|
86
|
+
guardContext,
|
|
87
|
+
runtime: services.runtime,
|
|
88
|
+
features: services.features,
|
|
89
|
+
signal: input.context.signal,
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
const moduleContext: ModuleDurableHandlerContext = {
|
|
93
|
+
...input.context,
|
|
94
|
+
actor: toModuleActor(actor),
|
|
95
|
+
requestId: guardContext.requestId,
|
|
96
|
+
storage: {
|
|
97
|
+
openResourceFile: (request) => storage.openResourceFile(request),
|
|
98
|
+
},
|
|
99
|
+
ai:
|
|
100
|
+
services.backgroundAi?.create(
|
|
101
|
+
input.moduleName,
|
|
102
|
+
guardContext,
|
|
103
|
+
input.context.signal,
|
|
104
|
+
) ?? unavailableBackgroundAi(input.moduleName),
|
|
105
|
+
};
|
|
106
|
+
return moduleContext;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function unavailableBackgroundAi(moduleName: string): ModuleBackgroundAiPort {
|
|
111
|
+
return {
|
|
112
|
+
async enqueueOperation() {
|
|
113
|
+
throw new ModuleBackgroundAiError(
|
|
114
|
+
"AI_OPERATION_UNAVAILABLE",
|
|
115
|
+
`模块 ${moduleName} 的后台 AI Operation Port 未挂载`,
|
|
116
|
+
true,
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function denied(code: string) {
|
|
123
|
+
return new DurableHandlerError(code, "模块后台任务授权已失效", false);
|
|
124
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DurableHandlerError,
|
|
3
|
+
type DurableHandlerContext,
|
|
4
|
+
type DurableHandlerDefinition,
|
|
5
|
+
type DurableHandlerRegistry,
|
|
6
|
+
} from "@southwind-ai/jobs";
|
|
7
|
+
import type {
|
|
8
|
+
ModuleDurableHandlerContext,
|
|
9
|
+
ModuleDurableHandlerDefinition,
|
|
10
|
+
} from "@southwind-ai/server-sdk";
|
|
11
|
+
import { ModuleBackgroundAiError } from "@southwind-ai/server-sdk";
|
|
12
|
+
|
|
13
|
+
export interface DurableHandlerRegistrationState {
|
|
14
|
+
readonly handlerKeys: Set<string>;
|
|
15
|
+
readonly handlerInstallers: Array<(registry: DurableHandlerRegistry) => void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function registerModuleDurableHandler<T>(
|
|
19
|
+
moduleName: string,
|
|
20
|
+
state: DurableHandlerRegistrationState,
|
|
21
|
+
definition: ModuleDurableHandlerDefinition<T>,
|
|
22
|
+
) {
|
|
23
|
+
if (state.handlerKeys.has(definition.key)) {
|
|
24
|
+
throw new Error(`任务 handler 重复注册:${definition.key}`);
|
|
25
|
+
}
|
|
26
|
+
state.handlerKeys.add(definition.key);
|
|
27
|
+
state.handlerInstallers.push((registry) => {
|
|
28
|
+
registry.register(
|
|
29
|
+
{
|
|
30
|
+
key: definition.key,
|
|
31
|
+
payload: definition.payload,
|
|
32
|
+
async execute(payload, context) {
|
|
33
|
+
try {
|
|
34
|
+
await definition.execute(
|
|
35
|
+
payload,
|
|
36
|
+
requireModuleDurableContext(context, moduleName),
|
|
37
|
+
);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof ModuleBackgroundAiError) {
|
|
40
|
+
throw new DurableHandlerError(
|
|
41
|
+
error.code,
|
|
42
|
+
error.message,
|
|
43
|
+
error.retryable,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{ moduleName },
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function requireModuleDurableContext(
|
|
56
|
+
context: Parameters<DurableHandlerDefinition<unknown>["execute"]>[1],
|
|
57
|
+
moduleName: string,
|
|
58
|
+
): ModuleDurableHandlerContext {
|
|
59
|
+
if (
|
|
60
|
+
!("actor" in context) ||
|
|
61
|
+
!("requestId" in context) ||
|
|
62
|
+
!("storage" in context) ||
|
|
63
|
+
!("ai" in context)
|
|
64
|
+
) {
|
|
65
|
+
throw new DurableHandlerError(
|
|
66
|
+
"MODULE_DURABLE_CONTEXT_UNAVAILABLE",
|
|
67
|
+
`模块 ${moduleName} 的后台授权上下文不可用`,
|
|
68
|
+
false,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return context as DurableHandlerContext & ModuleDurableHandlerContext;
|
|
72
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DataGovernancePolicyDefinition,
|
|
3
|
+
JsonValue,
|
|
4
|
+
} from "@southwind-ai/shared";
|
|
5
|
+
import type { ModuleGovernedResourceRegistration } from "@southwind-ai/server-sdk";
|
|
6
|
+
import { DurableArtifactError } from "../jobs/artifact-service.js";
|
|
7
|
+
import {
|
|
8
|
+
GovernedExportServiceError,
|
|
9
|
+
type GovernedExportTarget,
|
|
10
|
+
} from "../data-governance/export/service.js";
|
|
11
|
+
import type { GovernedExportRequest } from "../data-governance/export/types.js";
|
|
12
|
+
import type { InitializedModuleHosts } from "./initialize.js";
|
|
13
|
+
|
|
14
|
+
export function exportTarget(
|
|
15
|
+
registration: ModuleGovernedResourceRegistration,
|
|
16
|
+
policy: DataGovernancePolicyDefinition,
|
|
17
|
+
): GovernedExportTarget {
|
|
18
|
+
return {
|
|
19
|
+
resourceType: policy.resourceType,
|
|
20
|
+
policyKey: policy.key,
|
|
21
|
+
policyVersion: policy.version,
|
|
22
|
+
fieldKeys: policy.fields.map(({ fieldKey }) => fieldKey),
|
|
23
|
+
approvalRequired: registration.export?.approvalRequired,
|
|
24
|
+
retentionSeconds: registration.export?.retentionSeconds,
|
|
25
|
+
...(registration.export?.renderers
|
|
26
|
+
? {
|
|
27
|
+
renderers: registration.export.renderers.map((renderer) => ({
|
|
28
|
+
formatKey: renderer.formatKey,
|
|
29
|
+
templateKey: renderer.templateKey,
|
|
30
|
+
templateVersion: renderer.templateVersion,
|
|
31
|
+
inputSchemaKey: renderer.inputSchemaKey,
|
|
32
|
+
contentType: renderer.contentType,
|
|
33
|
+
filenameExtension: renderer.filenameExtension,
|
|
34
|
+
...renderer.limits,
|
|
35
|
+
})),
|
|
36
|
+
}
|
|
37
|
+
: {}),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function parseCreate(
|
|
42
|
+
body: unknown,
|
|
43
|
+
registration: ModuleGovernedResourceRegistration,
|
|
44
|
+
) {
|
|
45
|
+
const value = strictObject(body, [
|
|
46
|
+
"idempotencyKey",
|
|
47
|
+
"reason",
|
|
48
|
+
"query",
|
|
49
|
+
"formatKey",
|
|
50
|
+
]);
|
|
51
|
+
try {
|
|
52
|
+
const query = registration.export!.validateQuery(value.query ?? {});
|
|
53
|
+
if (!isJsonValue(query) || JSON.stringify(query).length > 64_000) {
|
|
54
|
+
throw new Error("invalid query");
|
|
55
|
+
}
|
|
56
|
+
const formatKey = optionalString(value.formatKey);
|
|
57
|
+
return {
|
|
58
|
+
idempotencyKey: requiredString(value.idempotencyKey),
|
|
59
|
+
reason: requiredString(value.reason),
|
|
60
|
+
query,
|
|
61
|
+
...(formatKey === undefined ? {} : { formatKey }),
|
|
62
|
+
};
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error instanceof GovernedExportServiceError) throw error;
|
|
65
|
+
throw new GovernedExportServiceError("EXPORT_QUERY_INVALID", 400);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isJsonValue(value: unknown, depth = 0): value is JsonValue {
|
|
70
|
+
if (depth > 10) return false;
|
|
71
|
+
if (
|
|
72
|
+
value === null ||
|
|
73
|
+
typeof value === "string" ||
|
|
74
|
+
typeof value === "boolean"
|
|
75
|
+
) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
return (
|
|
81
|
+
value.length <= 1_000 &&
|
|
82
|
+
value.every((item) => isJsonValue(item, depth + 1))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (
|
|
86
|
+
typeof value !== "object" ||
|
|
87
|
+
Object.getPrototypeOf(value) !== Object.prototype
|
|
88
|
+
) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const entries = Object.entries(value);
|
|
92
|
+
return (
|
|
93
|
+
entries.length <= 100 &&
|
|
94
|
+
entries.every(
|
|
95
|
+
([key, item]) => key.length <= 160 && isJsonValue(item, depth + 1),
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function strictObject(value: unknown, keys: readonly string[]) {
|
|
101
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
102
|
+
throw new GovernedExportServiceError("EXPORT_INPUT_INVALID", 400);
|
|
103
|
+
}
|
|
104
|
+
const object = value as Record<string, unknown>;
|
|
105
|
+
if (Object.keys(object).some((key) => !keys.includes(key))) {
|
|
106
|
+
throw new GovernedExportServiceError("EXPORT_INPUT_INVALID", 400);
|
|
107
|
+
}
|
|
108
|
+
return object;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function required(value: string | undefined) {
|
|
112
|
+
if (!value) throw new GovernedExportServiceError("EXPORT_NOT_FOUND", 404);
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function requiredString(value: unknown) {
|
|
117
|
+
if (typeof value !== "string") {
|
|
118
|
+
throw new GovernedExportServiceError("EXPORT_INPUT_INVALID", 400);
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function optionalString(value: unknown) {
|
|
124
|
+
return value === undefined ? undefined : requiredString(value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function project(request: GovernedExportRequest) {
|
|
128
|
+
return {
|
|
129
|
+
id: request.id,
|
|
130
|
+
resourceType: request.resourceType,
|
|
131
|
+
status: request.status,
|
|
132
|
+
approvalRequired: request.approvalRequired,
|
|
133
|
+
approvalId: request.approvalId,
|
|
134
|
+
jobId: request.durableJobId,
|
|
135
|
+
expiresAt: request.expiresAt,
|
|
136
|
+
createdAt: request.createdAt,
|
|
137
|
+
updatedAt: request.updatedAt,
|
|
138
|
+
formatKey: request.renderer.formatKey,
|
|
139
|
+
templateKey: request.renderer.templateKey,
|
|
140
|
+
templateVersion: request.renderer.templateVersion,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function knownError(error: unknown): Response {
|
|
145
|
+
if (error instanceof GovernedExportServiceError) {
|
|
146
|
+
return Response.json({ error: error.code }, { status: error.status });
|
|
147
|
+
}
|
|
148
|
+
if (error instanceof DurableArtifactError) {
|
|
149
|
+
return Response.json(
|
|
150
|
+
{ error: error.code },
|
|
151
|
+
{ status: error.code === "ARTIFACT_EXPIRED" ? 410 : 409 },
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function requirePolicy(
|
|
158
|
+
hosts: InitializedModuleHosts,
|
|
159
|
+
moduleName: string,
|
|
160
|
+
policyKey: string,
|
|
161
|
+
) {
|
|
162
|
+
const policy = hosts
|
|
163
|
+
.manifestOf(moduleName)
|
|
164
|
+
.dataPolicies?.find(({ key }) => key === policyKey);
|
|
165
|
+
if (!policy) throw new Error(`模块数据治理策略不存在:${policyKey}`);
|
|
166
|
+
return policy;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function stripApiPrefix(path: string) {
|
|
170
|
+
return path.slice("/api".length);
|
|
171
|
+
}
|