bailinghub 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +42 -0
- package/.env.example +83 -0
- package/.gitignore +44 -0
- package/.npmignore +56 -0
- package/CODE_OF_CONDUCT.md +24 -0
- package/CONTRIBUTING.md +74 -0
- package/Dockerfile +16 -0
- package/LICENSE +176 -0
- package/LICENSING.md +9 -0
- package/NOTICE +23 -0
- package/README.en.md +264 -0
- package/README.md +279 -0
- package/SECURITY.md +30 -0
- package/THIRD_PARTY_NOTICES.md +254 -0
- package/VISION.md +41 -0
- package/assets/architecture-overview.en-dark.png +0 -0
- package/assets/architecture-overview.en-dark.svg +57 -0
- package/assets/architecture-overview.en.png +0 -0
- package/assets/architecture-overview.en.svg +57 -0
- package/assets/architecture-overview.zh-CN-dark.png +0 -0
- package/assets/architecture-overview.zh-CN-dark.svg +57 -0
- package/assets/architecture-overview.zh-CN.png +0 -0
- package/assets/architecture-overview.zh-CN.svg +57 -0
- package/assets/bailinghub-lockup-dark.png +0 -0
- package/assets/bailinghub-lockup-dark.svg +8 -0
- package/assets/bailinghub-lockup.png +0 -0
- package/assets/bailinghub-lockup.svg +8 -0
- package/brain/README.en.md +23 -0
- package/brain/README.md +45 -0
- package/brain/agents/code-review.md +41 -0
- package/brain/agents/triage.md +37 -0
- package/brain/profiles.json +114 -0
- package/brain/runbooks/ticket-triage.md +24 -0
- package/config.example.json +58 -0
- package/contracts/client-api/consumers.json +28 -0
- package/contracts/client-api/v1/error-response.schema.json +21 -0
- package/contracts/client-api/v1/health-response.schema.json +13 -0
- package/contracts/client-api/v1/job-response.schema.json +49 -0
- package/contracts/client-api/v1/manifest.json +63 -0
- package/contracts/client-api/v1/run-request.schema.json +42 -0
- package/contracts/client-api/v1/submit-response.schema.json +35 -0
- package/contracts/client-api/v1/vectors.json +153 -0
- package/demo/business/Dockerfile +8 -0
- package/demo/business/server.mjs +483 -0
- package/docker-compose.images.yml +67 -0
- package/docker-compose.yml +79 -0
- package/docs/AI_FRIENDLY_TOOLS.en.md +69 -0
- package/docs/AI/345/217/213/345/245/275/345/267/245/345/205/267/350/256/276/350/256/241/346/214/207/345/215/227.md +198 -0
- package/docs/ARCHITECTURE.en.md +81 -0
- package/docs/ARCHITECTURE.md +795 -0
- package/docs/CHANGELOG.en.md +275 -0
- package/docs/CHANGELOG.md +279 -0
- package/docs/CHANNELS.en.md +35 -0
- package/docs/CHANNELS.md +72 -0
- package/docs/CLIENT_API.md +89 -0
- package/docs/COMPATIBILITY.en.md +103 -0
- package/docs/CONTRACT.en.md +254 -0
- package/docs/CONTRACT.md +653 -0
- package/docs/DEMO.en.md +90 -0
- package/docs/DEMO.md +297 -0
- package/docs/ECOSYSTEM.en.md +39 -0
- package/docs/ECOSYSTEM.md +39 -0
- package/docs/INDEPENDENT_VALIDATION.en.md +184 -0
- package/docs/INDEPENDENT_VALIDATION.md +184 -0
- package/docs/INTEGRATION.en.md +120 -0
- package/docs/KERNEL_HOST_API.en.md +59 -0
- package/docs/KERNEL_HOST_API.md +59 -0
- package/docs/OPERATIONS.en.md +182 -0
- package/docs/OPERATIONS.md +231 -0
- package/docs/PIPELINE.en.md +74 -0
- package/docs/PIPELINE.md +67 -0
- package/docs/QUICKSTART.en.md +297 -0
- package/docs/QUICKSTART.md +152 -0
- package/docs/README.en.md +103 -0
- package/docs/README.md +119 -0
- package/docs/RELEASE_NOTES_v0.1.0.en.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.0.md +66 -0
- package/docs/RELEASE_NOTES_v0.1.1.en.md +38 -0
- package/docs/RELEASE_NOTES_v0.1.1.md +38 -0
- package/docs/RELEASE_NOTES_v0.1.10.en.md +102 -0
- package/docs/RELEASE_NOTES_v0.1.10.md +102 -0
- package/docs/RELEASE_NOTES_v0.1.11.en.md +99 -0
- package/docs/RELEASE_NOTES_v0.1.11.md +99 -0
- package/docs/RELEASE_NOTES_v0.1.12.en.md +64 -0
- package/docs/RELEASE_NOTES_v0.1.12.md +64 -0
- package/docs/RELEASE_NOTES_v0.1.13.en.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.13.md +46 -0
- package/docs/RELEASE_NOTES_v0.1.14.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.14.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.15.en.md +50 -0
- package/docs/RELEASE_NOTES_v0.1.15.md +50 -0
- package/docs/RELEASE_NOTES_v0.1.16.en.md +106 -0
- package/docs/RELEASE_NOTES_v0.1.16.md +105 -0
- package/docs/RELEASE_NOTES_v0.1.2.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.2.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.3.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.3.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.4.en.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.4.md +55 -0
- package/docs/RELEASE_NOTES_v0.1.5.en.md +49 -0
- package/docs/RELEASE_NOTES_v0.1.5.md +49 -0
- package/docs/RELEASE_NOTES_v0.1.6.en.md +41 -0
- package/docs/RELEASE_NOTES_v0.1.6.md +41 -0
- package/docs/RELEASE_NOTES_v0.1.7.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.7.md +39 -0
- package/docs/RELEASE_NOTES_v0.1.8.en.md +88 -0
- package/docs/RELEASE_NOTES_v0.1.8.md +88 -0
- package/docs/RELEASE_NOTES_v0.1.9.en.md +127 -0
- package/docs/RELEASE_NOTES_v0.1.9.md +127 -0
- package/docs/RELEASE_NOTES_v0.2.0.en.md +95 -0
- package/docs/RELEASE_NOTES_v0.2.0.md +96 -0
- package/docs/RELEASE_NOTES_v0.3.0.en.md +77 -0
- package/docs/RELEASE_NOTES_v0.3.0.md +77 -0
- package/docs/SDK.en.md +177 -0
- package/docs/STREAMING.en.md +78 -0
- package/docs/STREAMING.md +109 -0
- package/docs/TOOLS.en.md +205 -0
- package/docs/TOOLS_DESIGN.en.md +142 -0
- package/docs/TOOLS_DESIGN.md +466 -0
- package/docs/TOOLS_MODEL.en.md +56 -0
- package/docs/TOOLS_MODEL.md +903 -0
- package/docs/WIDGET_RENDERERS.en.md +133 -0
- package/docs/WIDGET_RENDERERS.md +139 -0
- package/docs/examples/bailing-tool-verify.mjs +94 -0
- package/docs/examples/bailing-tool-verify.php +154 -0
- package/docs/examples/bailing-tool-verify.py +107 -0
- package/docs/examples/custom-streaming-chat-client.mjs +108 -0
- package/docs/examples/widget-chart-renderer-adapter.mjs +44 -0
- package/docs/integrations/bailian/README.en.md +181 -0
- package/docs/integrations/bailian/README.md +165 -0
- package/docs/integrations/bailian/bailian-mcp-config.json +17 -0
- package/docs/integrations/bailian/verify_recipe.py +97 -0
- package/docs/integrations/deepseek/README.en.md +225 -0
- package/docs/integrations/deepseek/README.md +225 -0
- package/docs/integrations/deepseek/verify_e2e.py +131 -0
- package/docs/integrations/dify/README.en.md +131 -0
- package/docs/integrations/dify/README.md +131 -0
- package/docs/integrations/dify/bailinghub-control-plane.openapi.json +129 -0
- package/docs/integrations/dify/verify_contract.py +88 -0
- package/docs/integrations/dify/verify_e2e.py +117 -0
- package/docs/integrations/jeecgboot/README.en.md +193 -0
- package/docs/integrations/jeecgboot/README.md +161 -0
- package/docs/integrations/jeecgboot/adapter-contract.v1.json +94 -0
- package/docs/integrations/jeecgboot/jeecgboot-user-adapter.openapi.json +183 -0
- package/docs/integrations/jeecgboot/verify_recipe.mjs +97 -0
- package/docs/integrations/ruoyi-vue-pro/README.en.md +91 -0
- package/docs/integrations/ruoyi-vue-pro/README.md +155 -0
- package/docs/integrations/ruoyi-vue-pro/adapter-contract.v1.json +71 -0
- package/docs/integrations/ruoyi-vue-pro/ruoyi-vue-pro-trade-adapter.openapi.json +148 -0
- package/docs/integrations/ruoyi-vue-pro/verify_recipe.mjs +71 -0
- package/docs/user-guide/README.en.md +39 -0
- package/docs/user-guide/README.md +40 -0
- package/docs/user-guide/concepts.en.md +69 -0
- package/docs/user-guide/concepts.md +225 -0
- package/docs/user-guide/console-map.en.md +58 -0
- package/docs/user-guide/console-map.md +473 -0
- package/docs/user-guide/overview.en.md +61 -0
- package/docs/user-guide/overview.md +156 -0
- package/docs/user-guide/scenarios.en.md +70 -0
- package/docs/user-guide/scenarios.md +398 -0
- package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +134 -0
- package/docs//347/254/254/344/270/211/346/226/271/345/257/271/346/216/245/346/214/207/345/215/227.md +625 -0
- package/package.json +119 -0
- package/schemas/agent-capability-contract.schema.json +104 -0
- package/schemas/api/approval-decision.schema.json +28 -0
- package/schemas/api/approval-request.schema.json +31 -0
- package/schemas/api/delivery-callback.schema.json +28 -0
- package/schemas/api/job-response.schema.json +6 -0
- package/schemas/api/kb-doc-upsert.schema.json +21 -0
- package/schemas/api/run-request.schema.json +6 -0
- package/schemas/api/run-response.schema.json +6 -0
- package/schemas/api/tool-spec.schema.json +38 -0
- package/schemas/config/alert-rule.schema.json +22 -0
- package/schemas/config/channel.schema.json +91 -0
- package/schemas/config/chat-entry.schema.json +47 -0
- package/schemas/config/client.schema.json +56 -0
- package/schemas/config/common.schema.json +215 -0
- package/schemas/config/credential.schema.json +48 -0
- package/schemas/config/executor-token.schema.json +20 -0
- package/schemas/config/page-context.schema.json +21 -0
- package/schemas/config/route.schema.json +152 -0
- package/schemas/config/storage-bucket.schema.json +31 -0
- package/schemas/config/target.schema.json +17 -0
- package/schemas/config/tool-provider.schema.json +60 -0
- package/schemas/tool-definition.schema.json +188 -0
- package/scripts/check-client-api-ecosystem.mjs +69 -0
- package/scripts/check-doc-links.mjs +136 -0
- package/scripts/check-image-tags.sh +43 -0
- package/scripts/check-package-artifact.mjs +185 -0
- package/scripts/check-release-consistency.mjs +199 -0
- package/scripts/check-release-consistency.test.mjs +63 -0
- package/scripts/client-api-contract-test.ts +127 -0
- package/scripts/create-admin.ts +27 -0
- package/scripts/demo-e2e.ts +133 -0
- package/scripts/docker-entrypoint.sh +33 -0
- package/scripts/doctor.ts +107 -0
- package/scripts/e2e-console.mjs +214 -0
- package/scripts/export-oss.mjs +204 -0
- package/scripts/generate-readme-assets.mjs +181 -0
- package/scripts/generate-third-party-notices.mjs +93 -0
- package/scripts/image-release-policy.mjs +75 -0
- package/scripts/image-release-policy.test.mjs +58 -0
- package/scripts/init-db.ts +21 -0
- package/scripts/install.sh +461 -0
- package/scripts/oss-boundary-check.mjs +183 -0
- package/scripts/publish-images.sh +63 -0
- package/scripts/publish-mysql-image.sh +41 -0
- package/scripts/release-audit.mjs +348 -0
- package/scripts/sdk-contract-test.ts +62 -0
- package/scripts/sdk-p1-check.mjs +58 -0
- package/scripts/sdk-runtime-test.mjs +106 -0
- package/scripts/security-scan.mjs +110 -0
- package/scripts/seed-demo.ts +116 -0
- package/scripts/selftest.ts +226 -0
- package/scripts/smoke.ts +31 -0
- package/scripts/validate-examples.mjs +93 -0
- package/scripts/verify-bootstrap-admin-restart.ts +66 -0
- package/scripts/verify-github-repo.mjs +195 -0
- package/scripts/verify-oss-export.mjs +192 -0
- package/scripts/verify-routing.ts +29 -0
- package/scripts/widget-renderer-test.mjs +206 -0
- package/scripts/write-build-info.mjs +29 -0
- package/sdk/dotnet/Bailing.Connect/Bailing.Connect.csproj +12 -0
- package/sdk/dotnet/Bailing.Connect/BailingConnect.cs +260 -0
- package/sdk/dotnet/README.md +32 -0
- package/sdk/dotnet/examples/BuildSpec/BuildSpec.csproj +11 -0
- package/sdk/dotnet/examples/BuildSpec/Program.cs +36 -0
- package/sdk/go/README.md +33 -0
- package/sdk/go/bailingconnect/connect.go +367 -0
- package/sdk/go/examples/build-spec/main.go +39 -0
- package/sdk/go/go.mod +3 -0
- package/sdk/java/README.md +43 -0
- package/sdk/java/examples/BuildSpec.java +31 -0
- package/sdk/java/src/main/java/com/bailing/connect/BailingConnect.java +376 -0
- package/sdk/node/README.md +107 -0
- package/sdk/node/examples/build-spec.mjs +84 -0
- package/sdk/node/package.json +22 -0
- package/sdk/node/src/index.mjs +250 -0
- package/sdk/php/README.md +204 -0
- package/sdk/php/composer.json +18 -0
- package/sdk/php/examples/DemoStaffController.php +108 -0
- package/sdk/php/examples/build-spec.php +27 -0
- package/sdk/php/examples/thinkphp-integration.php +81 -0
- package/sdk/php/src/Attributes/AiParam.php +40 -0
- package/sdk/php/src/Attributes/AiTool.php +75 -0
- package/sdk/php/src/HubClient.php +122 -0
- package/sdk/php/src/SpecBuilder.php +328 -0
- package/sdk/php/src/SpecServer.php +209 -0
- package/sdk/php/src/Ticket.php +37 -0
- package/sdk/php/src/Verify.php +178 -0
- package/sdk/php7/README.md +191 -0
- package/sdk/php7/composer.json +18 -0
- package/sdk/php7/examples/build-spec.php +68 -0
- package/sdk/php7/examples/well-known.php +33 -0
- package/sdk/php7/src/HubClient.php +111 -0
- package/sdk/php7/src/SpecServer.php +203 -0
- package/sdk/php7/src/Ticket.php +33 -0
- package/sdk/php7/src/ToolDef.php +169 -0
- package/sdk/php7/src/ToolSpec.php +334 -0
- package/sdk/php7/src/Verify.php +165 -0
- package/sdk/python/README.md +107 -0
- package/sdk/python/bailing_connect/__init__.py +307 -0
- package/sdk/python/examples/build_spec.py +90 -0
- package/sdk/python/pyproject.toml +24 -0
- package/sql/001_init_state.sql +66 -0
- package/sql/002_routes.sql +44 -0
- package/sql/003_executor.sql +15 -0
- package/sql/004_clients.sql +22 -0
- package/sql/005_admins.sql +25 -0
- package/sql/006_ledger.sql +57 -0
- package/sql/007_delivery.sql +8 -0
- package/sql/008_kb.sql +66 -0
- package/sql/009_rbac.sql +5 -0
- package/sql/010_targets.sql +28 -0
- package/sql/011_observability.sql +11 -0
- package/sql/012_generic.sql +5 -0
- package/sql/013_tools.sql +24 -0
- package/sql/014_approvals.sql +27 -0
- package/sql/015_chat.sql +21 -0
- package/sql/016_chat_feedback.sql +19 -0
- package/sql/017_provider_autorefresh.sql +6 -0
- package/sql/018_kb_ingest.sql +11 -0
- package/sql/019_kb_datasources.sql +32 -0
- package/sql/022_storage_buckets.sql +25 -0
- package/sql/024_channels.sql +15 -0
- package/sql/025_chat_appearance.sql +7 -0
- package/sql/026_thread_memory.sql +10 -0
- package/sql/027_page_contexts.sql +23 -0
- package/sql/028_executor_capabilities.sql +4 -0
- package/sql/029_executor_tokens.sql +13 -0
- package/sql/030_route_permission.sql +5 -0
- package/sql/031_tool_embeddings.sql +20 -0
- package/sql/032_tool_provider_embed.sql +7 -0
- package/sql/033_alert_rules.sql +21 -0
- package/sql/034_client_allowed_channels.sql +6 -0
- package/sql/035_tool_idempotency.sql +15 -0
- package/sql/036_delivery_dlq.sql +15 -0
- package/sql/038_audit_trace_shape.sql +15 -0
- package/sql/039_runtime_locks.sql +9 -0
- package/sql/040_budget_policy.sql +3 -0
- package/sql/041_approval_intent.sql +14 -0
- package/sql/042_approval_decision_contract.sql +11 -0
- package/sql/043_rate_limits.sql +7 -0
- package/sql/044_job_run_after.sql +3 -0
- package/sql/045_job_claim_lease.sql +7 -0
- package/sql/046_tool_authz_probe.sql +5 -0
- package/sql/047_route_audience.sql +4 -0
- package/sql/048_llm_target.sql +17 -0
- package/sql/049_llm_input_shape.sql +11 -0
- package/sql/050_operational_metrics_indexes.sql +8 -0
- package/sql/051_instance_branding.sql +17 -0
- package/sql/052_tool_execution_journal.sql +19 -0
- package/sql/053_tool_spec_access_policy.sql +9 -0
- package/sql/README.en.md +41 -0
- package/sql/README.md +40 -0
- package/src/adapters/channels/wecom-api.test.ts +77 -0
- package/src/adapters/channels/wecom-api.ts +224 -0
- package/src/adapters/channels/wecom-crypto.test.ts +85 -0
- package/src/adapters/channels/wecom-crypto.ts +77 -0
- package/src/adapters/executors/claudecode.ts +39 -0
- package/src/adapters/executors/profiles.ts +86 -0
- package/src/adapters/executors/report.ts +35 -0
- package/src/adapters/executors/runner.ts +114 -0
- package/src/adapters/executors/wecomnotify.ts +63 -0
- package/src/adapters/llm/file.test.ts +81 -0
- package/src/adapters/llm/file.ts +250 -0
- package/src/adapters/llm/openai-chat-stream.test.ts +74 -0
- package/src/adapters/llm/openai-chat-stream.ts +154 -0
- package/src/adapters/llm/perception.ts +159 -0
- package/src/adapters/llm/speech.test.ts +75 -0
- package/src/adapters/llm/speech.ts +146 -0
- package/src/adapters/storage/object-storage.test.ts +38 -0
- package/src/adapters/storage/object-storage.ts +140 -0
- package/src/adapters/targets/demo-agent.ts +88 -0
- package/src/adapters/targets/llm.test.ts +733 -0
- package/src/adapters/targets/llm.ts +862 -0
- package/src/app/admin-bootstrap.test.ts +112 -0
- package/src/app/admin-bootstrap.ts +48 -0
- package/src/app/auth-default.ts +26 -0
- package/src/app/auth.test.ts +57 -0
- package/src/app/auth.ts +207 -0
- package/src/app/builtin-tools-default.ts +17 -0
- package/src/app/builtin-tools.test.ts +202 -0
- package/src/app/builtin-tools.ts +380 -0
- package/src/app/channels-default.ts +8 -0
- package/src/app/channels.ts +141 -0
- package/src/app/delivery-default.ts +14 -0
- package/src/app/delivery.test.ts +46 -0
- package/src/app/delivery.ts +215 -0
- package/src/app/engine-default.ts +54 -0
- package/src/app/engine.ts +427 -0
- package/src/app/http-mount.test.ts +27 -0
- package/src/app/http-server.test.ts +114 -0
- package/src/app/http-server.ts +127 -0
- package/src/app/http.test.ts +27 -0
- package/src/app/http.ts +118 -0
- package/src/app/kernel.test.ts +191 -0
- package/src/app/kernel.ts +544 -0
- package/src/app/monitor-default.ts +9 -0
- package/src/app/monitor.ts +46 -0
- package/src/app/operational-metrics.test.ts +73 -0
- package/src/app/operational-metrics.ts +124 -0
- package/src/app/oss-edition.test.ts +71 -0
- package/src/app/oss-edition.ts +108 -0
- package/src/app/outbound-default.ts +57 -0
- package/src/app/outbound.ts +133 -0
- package/src/app/readiness.test.ts +37 -0
- package/src/app/readiness.ts +39 -0
- package/src/app/runtime-composition.test.ts +51 -0
- package/src/app/runtime-composition.ts +73 -0
- package/src/app/runtime-context-default.ts +30 -0
- package/src/app/runtime-context.test.ts +60 -0
- package/src/app/runtime-context.ts +79 -0
- package/src/app/runtime-lifecycle-default.ts +48 -0
- package/src/app/runtime-lifecycle.test.ts +322 -0
- package/src/app/runtime-lifecycle.ts +440 -0
- package/src/app/runtime.ts +36 -0
- package/src/app/tool-approvals-default.ts +16 -0
- package/src/app/tool-approvals.ts +214 -0
- package/src/app/tool-assembly-default.ts +11 -0
- package/src/app/tool-assembly.test.ts +112 -0
- package/src/app/tool-assembly.ts +83 -0
- package/src/app/tool-context-default.ts +7 -0
- package/src/app/tool-context.test.ts +82 -0
- package/src/app/tool-context.ts +109 -0
- package/src/app/tool-debug.test.ts +111 -0
- package/src/app/tool-debug.ts +152 -0
- package/src/app/tool-proxy-default.ts +35 -0
- package/src/app/tool-proxy.ts +173 -0
- package/src/app/tool-specs-default.ts +33 -0
- package/src/app/tool-specs.test.ts +322 -0
- package/src/app/tool-specs.ts +555 -0
- package/src/app/tools-runtime-default.ts +14 -0
- package/src/app/tools-runtime.ts +23 -0
- package/src/architecture-boundary.test.ts +119 -0
- package/src/core/config/config-codec.test.ts +105 -0
- package/src/core/config/config-codec.ts +231 -0
- package/src/core/config/config-diagnostics.test.ts +285 -0
- package/src/core/config/config-diagnostics.ts +436 -0
- package/src/core/config/config-models.test.ts +584 -0
- package/src/core/config/config-models.ts +392 -0
- package/src/core/config/config-schema.test.ts +153 -0
- package/src/core/config/config.test.ts +314 -0
- package/src/core/config/config.ts +324 -0
- package/src/core/config/route-config.test.ts +232 -0
- package/src/core/config/route-config.ts +246 -0
- package/src/core/config/target-config.test.ts +139 -0
- package/src/core/config/target-config.ts +219 -0
- package/src/core/config/tools-config.test.ts +124 -0
- package/src/core/config/tools-config.ts +126 -0
- package/src/core/contracts/approval-decision.test.ts +67 -0
- package/src/core/contracts/approval-decision.ts +93 -0
- package/src/core/contracts/openapi-tools.ts +201 -0
- package/src/core/contracts/tool-annotations.ts +121 -0
- package/src/core/contracts/tool-definition.test.ts +147 -0
- package/src/core/contracts/tool-definition.ts +304 -0
- package/src/core/contracts/tools-idempotency.test.ts +435 -0
- package/src/core/contracts/tools.test.ts +487 -0
- package/src/core/contracts/tools.ts +831 -0
- package/src/core/contracts/types.ts +408 -0
- package/src/core/edition/capabilities.ts +6 -0
- package/src/core/edition/context.test.ts +20 -0
- package/src/core/edition/context.ts +78 -0
- package/src/core/edition/index.ts +4 -0
- package/src/core/edition/scope-resolver.ts +24 -0
- package/src/core/edition/store-factory.ts +6 -0
- package/src/core/observability/openmetrics.test.ts +57 -0
- package/src/core/observability/openmetrics.ts +103 -0
- package/src/core/platform/content.test.ts +46 -0
- package/src/core/platform/content.ts +90 -0
- package/src/core/platform/fence.test.ts +31 -0
- package/src/core/platform/fence.ts +11 -0
- package/src/core/platform/instance-branding.test.ts +65 -0
- package/src/core/platform/instance-branding.ts +202 -0
- package/src/core/platform/metrics-token.test.ts +31 -0
- package/src/core/platform/metrics-token.ts +42 -0
- package/src/core/platform/pagecontext.ts +0 -0
- package/src/core/platform/password.test.ts +13 -0
- package/src/core/platform/password.ts +38 -0
- package/src/core/platform/queue.test.ts +59 -0
- package/src/core/platform/queue.ts +75 -0
- package/src/core/platform/serial.test.ts +73 -0
- package/src/core/platform/serial.ts +64 -0
- package/src/core/platform/server-token.test.ts +46 -0
- package/src/core/platform/server-token.ts +60 -0
- package/src/core/platform/signing.test.ts +24 -0
- package/src/core/platform/signing.ts +22 -0
- package/src/core/platform/time.ts +73 -0
- package/src/core/platform/version.test.ts +19 -0
- package/src/core/platform/version.ts +90 -0
- package/src/core/runtime/budget-runtime.test.ts +93 -0
- package/src/core/runtime/budget-runtime.ts +110 -0
- package/src/core/runtime/context-runtime.test.ts +106 -0
- package/src/core/runtime/context-runtime.ts +95 -0
- package/src/core/runtime/credential-resolver.test.ts +129 -0
- package/src/core/runtime/credential-resolver.ts +93 -0
- package/src/core/runtime/credential-verify.test.ts +76 -0
- package/src/core/runtime/credential-verify.ts +161 -0
- package/src/core/runtime/debug-report.test.ts +20 -0
- package/src/core/runtime/debug-report.ts +100 -0
- package/src/core/runtime/execution-runtime.test.ts +350 -0
- package/src/core/runtime/execution-runtime.ts +197 -0
- package/src/core/runtime/finish-runtime.test.ts +168 -0
- package/src/core/runtime/finish-runtime.ts +83 -0
- package/src/core/runtime/identity-runtime.test.ts +38 -0
- package/src/core/runtime/identity-runtime.ts +104 -0
- package/src/core/runtime/inhub-runtime.test.ts +297 -0
- package/src/core/runtime/inhub-runtime.ts +198 -0
- package/src/core/runtime/job-stream.test.ts +55 -0
- package/src/core/runtime/job-stream.ts +187 -0
- package/src/core/runtime/knowledge-runtime.test.ts +120 -0
- package/src/core/runtime/knowledge-runtime.ts +98 -0
- package/src/core/runtime/launch-runtime.test.ts +187 -0
- package/src/core/runtime/launch-runtime.ts +182 -0
- package/src/core/runtime/memory-runtime.test.ts +85 -0
- package/src/core/runtime/memory-runtime.ts +32 -0
- package/src/core/runtime/memory.ts +141 -0
- package/src/core/runtime/redaction-runtime.test.ts +35 -0
- package/src/core/runtime/redaction-runtime.ts +60 -0
- package/src/core/runtime/routing-runtime.test.ts +89 -0
- package/src/core/runtime/routing-runtime.ts +146 -0
- package/src/core/runtime/smoke-runtime.test.ts +65 -0
- package/src/core/runtime/smoke-runtime.ts +237 -0
- package/src/core/runtime/summary-runtime.test.ts +234 -0
- package/src/core/runtime/summary-runtime.ts +153 -0
- package/src/core/runtime/trace-runtime.test.ts +234 -0
- package/src/core/runtime/trace-runtime.ts +330 -0
- package/src/core/state/audit-observability.test.ts +51 -0
- package/src/core/state/audit-observability.ts +74 -0
- package/src/core/state/state-codec.ts +44 -0
- package/src/core/state/state-contracts.ts +59 -0
- package/src/core/targets/adapter.ts +69 -0
- package/src/core/targets/registry.test.ts +65 -0
- package/src/core/targets/registry.ts +153 -0
- package/src/core/targets/resolve.ts +21 -0
- package/src/executor.ts +201 -0
- package/src/extension-api.test.ts +84 -0
- package/src/extension-api.ts +194 -0
- package/src/infrastructure/config/config-admin-repository.test.ts +193 -0
- package/src/infrastructure/config/config-admin-repository.ts +175 -0
- package/src/infrastructure/config/config-alert-rule-repository.ts +38 -0
- package/src/infrastructure/config/config-approval-ledger.ts +74 -0
- package/src/infrastructure/config/config-channel-repository.ts +31 -0
- package/src/infrastructure/config/config-chat-repository.ts +83 -0
- package/src/infrastructure/config/config-client-repository.ts +46 -0
- package/src/infrastructure/config/config-conversation-ledger.ts +222 -0
- package/src/infrastructure/config/config-credential-repository.test.ts +32 -0
- package/src/infrastructure/config/config-credential-repository.ts +47 -0
- package/src/infrastructure/config/config-delivery-dlq-ledger.ts +49 -0
- package/src/infrastructure/config/config-executor-ledger.ts +30 -0
- package/src/infrastructure/config/config-executor-token-repository.ts +44 -0
- package/src/infrastructure/config/config-instance-branding-repository.ts +75 -0
- package/src/infrastructure/config/config-knowledge-repository.ts +249 -0
- package/src/infrastructure/config/config-observability-ledger.ts +262 -0
- package/src/infrastructure/config/config-project-repository.ts +30 -0
- package/src/infrastructure/config/config-rate-limit-ledger.ts +56 -0
- package/src/infrastructure/config/config-route-repository.ts +41 -0
- package/src/infrastructure/config/config-storage-bucket-repository.ts +43 -0
- package/src/infrastructure/config/config-target-repository.ts +25 -0
- package/src/infrastructure/config/config-tool-call-ledger.ts +142 -0
- package/src/infrastructure/config/config-tool-embedding-repository.test.ts +124 -0
- package/src/infrastructure/config/config-tool-embedding-repository.ts +78 -0
- package/src/infrastructure/config/config-tool-provider-repository.test.ts +71 -0
- package/src/infrastructure/config/config-tool-provider-repository.ts +50 -0
- package/src/infrastructure/config/configstore.ts +130 -0
- package/src/infrastructure/config/local-instance-branding-provider.test.ts +94 -0
- package/src/infrastructure/config/local-instance-branding-provider.ts +79 -0
- package/src/infrastructure/mysql/pool-owner.test.ts +36 -0
- package/src/infrastructure/mysql/pool-owner.ts +56 -0
- package/src/infrastructure/schema/core-schema-migrator.test.ts +340 -0
- package/src/infrastructure/schema/core-schema-migrator.ts +461 -0
- package/src/infrastructure/schema/migration-files.test.ts +18 -0
- package/src/infrastructure/schema/migration-files.ts +27 -0
- package/src/infrastructure/schema/retired-migrations.ts +35 -0
- package/src/infrastructure/state/operational-metrics-storage.test.ts +107 -0
- package/src/infrastructure/state/state-jsonl.ts +268 -0
- package/src/infrastructure/state/state-mysql-update-plan.test.ts +54 -0
- package/src/infrastructure/state/state-mysql-update-plan.ts +63 -0
- package/src/infrastructure/state/state-mysql.ts +359 -0
- package/src/infrastructure/state/state.test.ts +319 -0
- package/src/infrastructure/state/state.ts +11 -0
- package/src/kernel-api/v1/artifact.test.ts +46 -0
- package/src/kernel-api/v1/artifact.ts +71 -0
- package/src/kernel-api/v1/contracts.ts +168 -0
- package/src/kernel-api/v1/execution-gate.test.ts +25 -0
- package/src/kernel-api/v1/execution-gate.ts +18 -0
- package/src/kernel-api/v1/index.ts +51 -0
- package/src/openclaw-stdio.test.ts +60 -0
- package/src/routes/admin-access-default.ts +20 -0
- package/src/routes/admin-access.ts +94 -0
- package/src/routes/admin-branding.test.ts +116 -0
- package/src/routes/admin-branding.ts +48 -0
- package/src/routes/admin-chat-default.ts +12 -0
- package/src/routes/admin-chat.ts +84 -0
- package/src/routes/admin-default.ts +35 -0
- package/src/routes/admin-dispatch-config-default.ts +13 -0
- package/src/routes/admin-dispatch-config.ts +121 -0
- package/src/routes/admin-format.ts +12 -0
- package/src/routes/admin-host.test.ts +56 -0
- package/src/routes/admin-infra-default.ts +12 -0
- package/src/routes/admin-infra.ts +142 -0
- package/src/routes/admin-kb-default.ts +13 -0
- package/src/routes/admin-kb.ts +160 -0
- package/src/routes/admin-runtime-default.ts +31 -0
- package/src/routes/admin-runtime.ts +444 -0
- package/src/routes/admin-tool-providers-default.ts +20 -0
- package/src/routes/admin-tool-providers.test.ts +114 -0
- package/src/routes/admin-tool-providers.ts +186 -0
- package/src/routes/admin.ts +246 -0
- package/src/routes/approvals-default.ts +32 -0
- package/src/routes/approvals.ts +120 -0
- package/src/routes/chat-default.ts +61 -0
- package/src/routes/chat-mount.test.ts +58 -0
- package/src/routes/chat.test.ts +222 -0
- package/src/routes/chat.ts +478 -0
- package/src/routes/critical-routes.test.ts +292 -0
- package/src/routes/executor-default.ts +45 -0
- package/src/routes/executor.ts +226 -0
- package/src/routes/kb-default.ts +23 -0
- package/src/routes/kb.ts +92 -0
- package/src/routes/private-default.ts +38 -0
- package/src/routes/private-host-auth.test.ts +84 -0
- package/src/routes/private.ts +210 -0
- package/src/routes/public-default.ts +46 -0
- package/src/routes/public.test.ts +504 -0
- package/src/routes/public.ts +313 -0
- package/src/routes/run-default.ts +25 -0
- package/src/routes/run.ts +188 -0
- package/src/routes/send-default.ts +24 -0
- package/src/routes/send.ts +143 -0
- package/src/routes/wecom-default.ts +27 -0
- package/src/routes/wecom.ts +307 -0
- package/src/server.ts +13 -0
- package/src/services/embedding.test.ts +185 -0
- package/src/services/embedding.ts +138 -0
- package/src/services/kb-repository.ts +79 -0
- package/src/services/kb.ts +328 -0
- package/src/services/kbsync.ts +166 -0
- package/src/services/tool-index-repository.ts +50 -0
- package/src/services/tools-index.test.ts +756 -0
- package/src/services/tools-index.ts +427 -0
- package/tsconfig.json +17 -0
- package/web/connect/bailing-connect-php.tgz +0 -0
- package/web/connect/bailing-connect-php7.tgz +0 -0
- package/web/connect/executor.mjs +152 -0
- package/web/connect/openclaw-stdio.mjs +145 -0
- package/web/connect/skills/connect-bailinghub-executor/SKILL.md +97 -0
- package/web/connect/skills/connect-bailinghub-executor/agents/openai.yaml +4 -0
- package/web/connect/skills/connect-bailinghub-executor/references/direct-protocol.md +99 -0
- package/web/connect/skills/connect-bailinghub-executor/references/openclaw.md +39 -0
- package/web/console/assets/Accounts-B4ePH5jp.css +1 -0
- package/web/console/assets/Accounts-Dwzm4D_V.js +3 -0
- package/web/console/assets/Approvals-BxhuI0ku.js +1 -0
- package/web/console/assets/Approvals-DlTSHl_7.css +1 -0
- package/web/console/assets/Audit-CSu6_3oD.css +1 -0
- package/web/console/assets/Audit-dFAbGynl.js +1 -0
- package/web/console/assets/BrandLockup-BtZyvkM5.js +1 -0
- package/web/console/assets/BrandLockup-DKYBYzKU.css +1 -0
- package/web/console/assets/Channels-CCn0L1jg.css +1 -0
- package/web/console/assets/Channels-Dqg4-_yr.js +105 -0
- package/web/console/assets/ChatEntries-BweMx_CU.css +1 -0
- package/web/console/assets/ChatEntries-CEGZ3NnJ.js +7 -0
- package/web/console/assets/Clients-C0QD1x7g.js +3 -0
- package/web/console/assets/Clients-DcTYN-Js.css +1 -0
- package/web/console/assets/Cost-BAJqLhpK.js +1 -0
- package/web/console/assets/Cost-CjT39DSZ.css +1 -0
- package/web/console/assets/Credentials-D_GDRgWi.js +1 -0
- package/web/console/assets/Credentials-ioLP0qYN.css +1 -0
- package/web/console/assets/Diagnostics-BWYnDxRj.js +1 -0
- package/web/console/assets/Diagnostics-Ya05PTDp.css +1 -0
- package/web/console/assets/Executors-CjhEM54c.css +1 -0
- package/web/console/assets/Executors-Cmryk4Vk.js +1 -0
- package/web/console/assets/HelpTip-CA6Bm7Nx.css +1 -0
- package/web/console/assets/HelpTip-CT07o3kB.js +1 -0
- package/web/console/assets/Kb-BSXQE0Fa.js +5 -0
- package/web/console/assets/Kb-CzIzd9xr.css +1 -0
- package/web/console/assets/Login-BnBPqC8Z.js +1 -0
- package/web/console/assets/Login-xtPwDB5_.css +1 -0
- package/web/console/assets/Projects-Bm8AjsNx.css +1 -0
- package/web/console/assets/Projects-CNHx4L_7.js +1 -0
- package/web/console/assets/Routes-DWGQN5Pv.css +1 -0
- package/web/console/assets/Routes-u5cEAEXQ.js +124 -0
- package/web/console/assets/Runs-BeFUR77_.js +3 -0
- package/web/console/assets/Runs-CUtSsPW-.css +1 -0
- package/web/console/assets/Setup-BjZfrmfN.js +1 -0
- package/web/console/assets/Setup-QjlHZKdB.css +1 -0
- package/web/console/assets/Shell-CbGNTN91.css +1 -0
- package/web/console/assets/Shell-EJuYf3OO.js +1 -0
- package/web/console/assets/StorageBuckets-DMXRJxUA.js +1 -0
- package/web/console/assets/StorageBuckets-GnXnVvF0.css +1 -0
- package/web/console/assets/SystemSettings-B72F-loY.js +1 -0
- package/web/console/assets/SystemSettings-BjiHyrXO.css +1 -0
- package/web/console/assets/SystemStatus-I11I67rw.css +1 -0
- package/web/console/assets/SystemStatus-hOj_TPYE.js +1 -0
- package/web/console/assets/Targets-DTb5tn6z.css +1 -0
- package/web/console/assets/Targets-UVVf7twC.js +1 -0
- package/web/console/assets/Tools-A20oaMaQ.css +1 -0
- package/web/console/assets/Tools-ihkr9R9x.js +2 -0
- package/web/console/assets/_plugin-vue_export-helper-DlAUqK2U.js +1 -0
- package/web/console/assets/docs-DPUOCa7R.js +1 -0
- package/web/console/assets/index-DZEQL_yy.js +1 -0
- package/web/console/assets/index-DxvruykG.js +2 -0
- package/web/console/assets/index-Fi9gkGxV.css +1 -0
- package/web/console/assets/llm-catalog-HwRIrS94.js +1 -0
- package/web/console/assets/request-BisL2T9B.js +1 -0
- package/web/console/assets/schema-CzpUpoLk.js +1 -0
- package/web/console/assets/util-SiQC2MJh.js +1 -0
- package/web/console/assets/vendor-element-D7WRzHhy.js +37 -0
- package/web/console/assets/vendor-mammoth-DvgsHdlx.js +231 -0
- package/web/console/assets/vendor-turndown-kLzRgxYD.js +61 -0
- package/web/console/assets/vendor-vue-B2uhHUfE.js +29 -0
- package/web/console/index.html +16 -0
- package/web/widget/widget.js +1490 -0
- package/web-admin/index.html +13 -0
- package/web-admin/package-lock.json +1854 -0
- package/web-admin/package.json +25 -0
- package/web-admin/src/App.vue +9 -0
- package/web-admin/src/branding.ts +90 -0
- package/web-admin/src/components/BrandLockup.vue +40 -0
- package/web-admin/src/components/HelpTip.vue +51 -0
- package/web-admin/src/components/RichText.vue +35 -0
- package/web-admin/src/docs.ts +16 -0
- package/web-admin/src/layout/Shell.vue +356 -0
- package/web-admin/src/llm-catalog.ts +129 -0
- package/web-admin/src/main.ts +116 -0
- package/web-admin/src/pages/Accounts.vue +162 -0
- package/web-admin/src/pages/Approvals.vue +179 -0
- package/web-admin/src/pages/Audit.vue +110 -0
- package/web-admin/src/pages/Channels.vue +738 -0
- package/web-admin/src/pages/ChatEntries.vue +778 -0
- package/web-admin/src/pages/Clients.vue +353 -0
- package/web-admin/src/pages/Cost.vue +314 -0
- package/web-admin/src/pages/Credentials.vue +418 -0
- package/web-admin/src/pages/Diagnostics.vue +440 -0
- package/web-admin/src/pages/Executors.vue +398 -0
- package/web-admin/src/pages/Kb.vue +698 -0
- package/web-admin/src/pages/Login.vue +314 -0
- package/web-admin/src/pages/Projects.vue +106 -0
- package/web-admin/src/pages/Routes.vue +1661 -0
- package/web-admin/src/pages/Runs.vue +1113 -0
- package/web-admin/src/pages/Setup.vue +354 -0
- package/web-admin/src/pages/StorageBuckets.vue +182 -0
- package/web-admin/src/pages/SystemSettings.vue +281 -0
- package/web-admin/src/pages/SystemStatus.vue +185 -0
- package/web-admin/src/pages/Targets.vue +193 -0
- package/web-admin/src/pages/Tools.vue +1051 -0
- package/web-admin/src/request.ts +18 -0
- package/web-admin/src/router.ts +142 -0
- package/web-admin/src/runtime-path.ts +27 -0
- package/web-admin/src/schema.ts +52 -0
- package/web-admin/src/store.ts +35 -0
- package/web-admin/src/styles/base.css +272 -0
- package/web-admin/src/table-widths.ts +603 -0
- package/web-admin/src/util.ts +16 -0
- package/web-admin/tsconfig.json +15 -0
- package/web-admin/vite.config.ts +35 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
// 内置动作工具(非业务工具源):让大脑/执行器在执行过程中**自己命名收件人**主动发消息——
|
|
2
|
+
// "完成了某件事,自己就知道发给谁"。中枢只持有渠道凭证 + 校验"这条路由准发哪些渠道",
|
|
3
|
+
// 收件人是谁、发几次全由大脑当场决定,中枢不持有任何人↔身份映射(见 docs/CONTRACT.md §2.4c)。
|
|
4
|
+
// 复用 channelSend 出站原语 + /send 的入历史纪律(记 out、scope 共享、逐字投递)。
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import type { Job } from '../core/contracts/types';
|
|
7
|
+
import {
|
|
8
|
+
toolCallIdempotencyKey,
|
|
9
|
+
toolExecutionUncertaintyFromJournal,
|
|
10
|
+
type ToolExecutionJournalEntry,
|
|
11
|
+
type ToolExecutionUncertainty,
|
|
12
|
+
} from '../core/contracts/tools';
|
|
13
|
+
import {
|
|
14
|
+
type ChannelMessage,
|
|
15
|
+
type ChannelSendResult,
|
|
16
|
+
channelScopeKey,
|
|
17
|
+
channelSendFor,
|
|
18
|
+
} from './channels';
|
|
19
|
+
import { sendMessageConfig } from '../core/config/tools-config';
|
|
20
|
+
import { SEND_TOOL_NAME, type BuiltinToolDef } from '../core/targets/adapter';
|
|
21
|
+
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
22
|
+
|
|
23
|
+
export { SEND_MAX_CALLS, SEND_TOOL_NAME } from '../core/targets/adapter';
|
|
24
|
+
|
|
25
|
+
export interface BuiltinSendResult {
|
|
26
|
+
ok: boolean;
|
|
27
|
+
text: string;
|
|
28
|
+
uncertainty?: ToolExecutionUncertainty;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function auditBestEffort(
|
|
32
|
+
audit: ((event: string, detail: Record<string, unknown>) => Promise<void> | void) | undefined,
|
|
33
|
+
event: string,
|
|
34
|
+
detail: Record<string, unknown>,
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
try { await audit?.(event, detail); } catch { /* 后续对账状态优先,补充审计失败不覆盖原始故障 */ }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 路由声明的「大脑可主动发哪些渠道」白名单:`tools.builtin.send_message.channels` = 渠道名数组,`['*']` = 所有启用渠道。
|
|
41
|
+
* 返回**解析后确实存在且启用**的渠道名清单('*' 展开成全部启用渠道);未配置 / 无后端 / 无命中返回 []。
|
|
42
|
+
* 这是唯一的发送闸:路由准发哪些渠道由中枢后台定,收件人由大脑当场给——中枢不枚举任何人。
|
|
43
|
+
*/
|
|
44
|
+
export async function resolveSendChannelsFor(config: ConfigStoreContract | null, tcfg: Record<string, unknown> | undefined): Promise<string[]> {
|
|
45
|
+
const raw = sendMessageConfig(tcfg)?.channels ?? [];
|
|
46
|
+
if (!raw.length || !config) return [];
|
|
47
|
+
const enabled = (await config.channels.list().catch(() => [])).filter((c) => c.enabled).map((c) => c.name);
|
|
48
|
+
if (raw.includes('*')) return enabled;
|
|
49
|
+
return raw.filter((n) => enabled.includes(n));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 生成喂大脑的 send_message 工具定义。channels = 本路由允许发送的渠道清单(已解析)。 */
|
|
53
|
+
export function sendToolDef(channels: string[]): BuiltinToolDef {
|
|
54
|
+
const list = channels.join(' / ');
|
|
55
|
+
const multi = channels.length > 1;
|
|
56
|
+
return {
|
|
57
|
+
type: 'function',
|
|
58
|
+
function: {
|
|
59
|
+
name: SEND_TOOL_NAME,
|
|
60
|
+
description: `主动给指定的人发送一条消息(经渠道送达,如企业微信),可附带文件(如审核报告)。当你完成了某件事、需要把结果或通知/报告发给某个人时调用——收件人由你指定,不是预先设定的。可用渠道:${list}。一次可发给多人。`,
|
|
61
|
+
parameters: {
|
|
62
|
+
type: 'object',
|
|
63
|
+
properties: {
|
|
64
|
+
to: { type: 'string', description: '收件人在该渠道的原生 id(企业微信即成员 UserID);发给多人用竖线分隔,如 "ZhangSan|LiSi|WangWu"' },
|
|
65
|
+
text: { type: 'string', description: '要发送的正文(纯文本;企业微信不渲染 Markdown,请用纯文本)。带附件时这里写一句说明,完整内容放 files。' },
|
|
66
|
+
files: {
|
|
67
|
+
type: 'array',
|
|
68
|
+
description: '可选附件,用于把完整报告/文件发给对方(如代码审核报告)。最多 5 个。每项推荐用 {name, content}:name=带扩展名的文件名(如 "审核报告.md"),content=该文件的完整文本内容(你直接把报告全文放这里,中枢会生成文件发出去,无需你先上传到任何地方)。也可用 {name, url} 发一个已托管的文件。',
|
|
69
|
+
items: { type: 'object', properties: { name: { type: 'string', description: '文件名,带扩展名,如 审核报告.md' }, content: { type: 'string', description: '文件的完整文本内容(生成的报告全文放这里)' }, url: { type: 'string', description: '已托管文件的 http(s) URL(与 content 二选一)' } }, required: ['name'] },
|
|
70
|
+
},
|
|
71
|
+
...(multi ? { channel: { type: 'string', description: `走哪个渠道(只有一个可用渠道时可省略)`, enum: channels } } : {}),
|
|
72
|
+
},
|
|
73
|
+
required: ['to', 'text'],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 受治理执行 send_message:校验渠道白名单 → channelSend 投递 → 给每个收件人各自 thread 记一条 out(与 /send 同纪律)。
|
|
81
|
+
* 返回回流给大脑的文本(成功失败都以文本回流,让大脑据此向用户解释)。永不抛。
|
|
82
|
+
*/
|
|
83
|
+
export async function runSendMessageFor(
|
|
84
|
+
config: ConfigStoreContract | null,
|
|
85
|
+
job: Job,
|
|
86
|
+
allowedChannels: string[],
|
|
87
|
+
args: Record<string, unknown>,
|
|
88
|
+
audit?: (event: string, detail: Record<string, unknown>) => Promise<void> | void,
|
|
89
|
+
sendChannel: (
|
|
90
|
+
config: ConfigStoreContract | null,
|
|
91
|
+
channelName: string,
|
|
92
|
+
recipient: string,
|
|
93
|
+
message: string | ChannelMessage,
|
|
94
|
+
) => Promise<ChannelSendResult> = channelSendFor,
|
|
95
|
+
): Promise<BuiltinSendResult> {
|
|
96
|
+
if (!config) return { ok: false, text: '发送失败:中枢无 mysql 后端。' };
|
|
97
|
+
if (!allowedChannels.length) return { ok: false, text: '本路由未开放任何可主动发送的渠道,无法发送。' };
|
|
98
|
+
// 渠道:显式 channel 必须在白名单内;不给且只有一个允许渠道 → 默认它
|
|
99
|
+
const reqCh = String(args['channel'] ?? '').trim();
|
|
100
|
+
const channelName = reqCh || (allowedChannels.length === 1 ? allowedChannels[0]! : '');
|
|
101
|
+
if (!channelName) return { ok: false, text: `请用 channel 参数指定渠道,可选:${allowedChannels.join(' / ')}。` };
|
|
102
|
+
if (!allowedChannels.includes(channelName)) return { ok: false, text: `渠道 ${channelName} 不在本路由允许发送的范围内(可选:${allowedChannels.join(' / ')})。` };
|
|
103
|
+
// 收件人:string / 数组 / "A|B|C"。去重 + 封顶 1000(企微 touser 上限)。
|
|
104
|
+
const toVal = args['to'];
|
|
105
|
+
const rawList = Array.isArray(toVal) ? toVal.map((x) => String(x)) : String(toVal ?? '').split('|');
|
|
106
|
+
const recipients = [...new Set(rawList.map((x) => x.trim()).filter(Boolean))].slice(0, 1000);
|
|
107
|
+
if (!recipients.length) return { ok: false, text: '请用 to 参数指定收件人 id。' };
|
|
108
|
+
const text = String(args['text'] ?? '').trim();
|
|
109
|
+
// 附件:每项 {name, content} 内联文本(如生成的 .md 报告)或 {name, url} 已托管文件。最多 5 个。
|
|
110
|
+
const files = (Array.isArray(args['files']) ? (args['files'] as unknown[]) : [])
|
|
111
|
+
.filter((f): f is Record<string, unknown> => !!f && typeof f === 'object')
|
|
112
|
+
.map((f) => ({
|
|
113
|
+
name: String(f['name'] ?? 'file').slice(0, 120),
|
|
114
|
+
content: typeof f['content'] === 'string' ? (f['content'] as string) : undefined,
|
|
115
|
+
url: typeof f['url'] === 'string' ? (f['url'] as string).trim() : undefined,
|
|
116
|
+
}))
|
|
117
|
+
.filter((f) => f.content !== undefined || f.url)
|
|
118
|
+
.slice(0, 5);
|
|
119
|
+
if (!text && !files.length) return { ok: false, text: '请用 text 给出正文,或用 files 附上要发送的文件。' };
|
|
120
|
+
const ch = await config.channels.get(channelName);
|
|
121
|
+
if (!ch || !ch.enabled) return { ok: false, text: `渠道 ${channelName} 不存在或已停用。` };
|
|
122
|
+
const label = recipients.length === 1 ? recipients[0]! : `${recipients.length}人`;
|
|
123
|
+
// 附件审计快照:每个文件记 名称 + 字节数 + 全文 sha256(可对账"对方收到的是不是这一份")+ 内容(封顶,超出以 sha256 为准)。
|
|
124
|
+
// 落进 builtin_send 审计(按 job_id 可追溯)——这样从任务详情就能看到本次到底发了什么文件,不会出现"对方说收到另一份说不清"。
|
|
125
|
+
const fileAudit = files.map((f) => f.content !== undefined
|
|
126
|
+
? { name: f.name, bytes: Buffer.byteLength(f.content, 'utf8'), sha256: createHash('sha256').update(f.content, 'utf8').digest('hex'), content: f.content.length > 16000 ? f.content.slice(0, 16000) + '…(已截断,完整内容以 sha256 为准)' : f.content }
|
|
127
|
+
: { name: f.name, url: f.url });
|
|
128
|
+
// 幂等与不确定性边界:同 job 内相同发送共用业务工具的持久化执行日志。
|
|
129
|
+
// 必须在投递前 reserve;成功送达、明确失败、部分送达和网络异常都先落状态,再决定能否重试。
|
|
130
|
+
const idemHash = createHash('sha256').update(JSON.stringify({
|
|
131
|
+
c: channelName, to: [...recipients].sort(), text,
|
|
132
|
+
files: fileAudit.map((f) => ('sha256' in f ? { n: f.name, h: (f as { sha256: string }).sha256 } : { n: f.name, u: (f as { url?: string }).url })),
|
|
133
|
+
})).digest('hex');
|
|
134
|
+
const scope = 'builtin.send_message';
|
|
135
|
+
const idemKey = toolCallIdempotencyKey(job.job_id, SEND_TOOL_NAME, idemHash);
|
|
136
|
+
let prior: ToolExecutionJournalEntry | null;
|
|
137
|
+
try {
|
|
138
|
+
prior = await config.toolCalls.get(job.job_id, SEND_TOOL_NAME, idemHash);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
await auditBestEffort(audit, 'builtin_send_blocked', {
|
|
141
|
+
channel: channelName,
|
|
142
|
+
to: label,
|
|
143
|
+
reason: '执行日志不可用,消息未发送',
|
|
144
|
+
error: String(error).slice(0, 500),
|
|
145
|
+
});
|
|
146
|
+
return { ok: false, text: '发送失败:持久化执行日志当前不可用,消息未发送。' };
|
|
147
|
+
}
|
|
148
|
+
if (prior?.state === 'completed') {
|
|
149
|
+
await auditBestEffort(audit, 'builtin_send_deduped', {
|
|
150
|
+
channel: channelName,
|
|
151
|
+
to: label,
|
|
152
|
+
idempotency_key: prior.idempotencyKey,
|
|
153
|
+
reason: '同 job 已完成相同发送,返回已确认结果',
|
|
154
|
+
});
|
|
155
|
+
return { ok: prior.ok, text: prior.text || '已处理(未重复发送)。' };
|
|
156
|
+
}
|
|
157
|
+
if (prior) {
|
|
158
|
+
const uncertainty = toolExecutionUncertaintyFromJournal(SEND_TOOL_NAME, scope, prior);
|
|
159
|
+
await auditBestEffort(audit, 'builtin_send_reconciliation_required', {
|
|
160
|
+
channel: channelName,
|
|
161
|
+
to: label,
|
|
162
|
+
state: prior.state,
|
|
163
|
+
idempotency_key: prior.idempotencyKey,
|
|
164
|
+
reason: prior.error ?? '检测到未完成的发送执行日志,禁止自动重放',
|
|
165
|
+
});
|
|
166
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
await audit?.('builtin_send_call', {
|
|
171
|
+
channel: channelName,
|
|
172
|
+
to: label,
|
|
173
|
+
recipients: recipients.length,
|
|
174
|
+
chars: text.length,
|
|
175
|
+
files: fileAudit,
|
|
176
|
+
idempotency_key: idemKey,
|
|
177
|
+
});
|
|
178
|
+
} catch (error) {
|
|
179
|
+
return {
|
|
180
|
+
ok: false,
|
|
181
|
+
text: `发送失败:调用审计无法持久化,消息未发送(${String(error).slice(0, 160)})。`,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const reserved = await config.toolCalls.reserve(job.job_id, SEND_TOOL_NAME, scope, idemHash, idemKey);
|
|
187
|
+
if (!reserved.inserted) {
|
|
188
|
+
if (reserved.entry.state === 'completed') {
|
|
189
|
+
return { ok: reserved.entry.ok, text: reserved.entry.text || '已处理(未重复发送)。' };
|
|
190
|
+
}
|
|
191
|
+
const uncertainty = toolExecutionUncertaintyFromJournal(SEND_TOOL_NAME, scope, reserved.entry);
|
|
192
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
193
|
+
}
|
|
194
|
+
} catch (error) {
|
|
195
|
+
await auditBestEffort(audit, 'builtin_send_blocked', {
|
|
196
|
+
channel: channelName,
|
|
197
|
+
to: label,
|
|
198
|
+
reason: '执行日志预留失败,消息未发送',
|
|
199
|
+
error: String(error).slice(0, 500),
|
|
200
|
+
});
|
|
201
|
+
return { ok: false, text: '发送失败:无法预留持久化执行日志,消息未发送。' };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// 投递:多收件人渠道原生合并一次发(企微 touser 支持 "A|B|C");带文件则发对象
|
|
205
|
+
const msg: ChannelMessage = { ...(text ? { text } : {}), ...(files.length ? { files } : {}) };
|
|
206
|
+
let sent: ChannelSendResult;
|
|
207
|
+
try {
|
|
208
|
+
sent = await sendChannel(config, channelName, recipients.join('|'), msg);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
const reason = `发送请求可能已到达渠道,但未取得可信响应:${String(error).slice(0, 300)}`;
|
|
211
|
+
await config.toolCalls.markUncertain(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
212
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
213
|
+
state: 'uncertain',
|
|
214
|
+
tool: SEND_TOOL_NAME,
|
|
215
|
+
scope,
|
|
216
|
+
idempotency_key: idemKey,
|
|
217
|
+
reason,
|
|
218
|
+
message: `消息发送结果无法确认。为避免重复送达,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
219
|
+
};
|
|
220
|
+
await auditBestEffort(audit, 'builtin_send_reconciliation_required', {
|
|
221
|
+
channel: channelName,
|
|
222
|
+
to: label,
|
|
223
|
+
state: uncertainty.state,
|
|
224
|
+
idempotency_key: idemKey,
|
|
225
|
+
reason,
|
|
226
|
+
});
|
|
227
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!sent.ok) {
|
|
231
|
+
if (sent.mayHaveCommitted) {
|
|
232
|
+
const reason = `渠道报告部分内容可能已送达:${sent.error ?? 'unknown_channel_error'}`;
|
|
233
|
+
await config.toolCalls.markUncertain(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
234
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
235
|
+
state: 'uncertain',
|
|
236
|
+
tool: SEND_TOOL_NAME,
|
|
237
|
+
scope,
|
|
238
|
+
idempotency_key: idemKey,
|
|
239
|
+
reason,
|
|
240
|
+
message: `消息可能只完成了部分送达。为避免重复发送,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
241
|
+
};
|
|
242
|
+
await auditBestEffort(audit, 'builtin_send_reconciliation_required', {
|
|
243
|
+
channel: channelName,
|
|
244
|
+
to: label,
|
|
245
|
+
state: uncertainty.state,
|
|
246
|
+
idempotency_key: idemKey,
|
|
247
|
+
reason,
|
|
248
|
+
});
|
|
249
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const resultText = `发送失败:${sent.error}`;
|
|
253
|
+
try {
|
|
254
|
+
await config.toolCalls.recordResponse(
|
|
255
|
+
job.job_id,
|
|
256
|
+
SEND_TOOL_NAME,
|
|
257
|
+
idemHash,
|
|
258
|
+
{ ok: false, status: 502, text: resultText },
|
|
259
|
+
);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
const reason = `渠道明确返回失败,但执行日志写入失败:${String(error).slice(0, 300)}`;
|
|
262
|
+
await config.toolCalls.markUncertain(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
263
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
264
|
+
state: 'uncertain',
|
|
265
|
+
tool: SEND_TOOL_NAME,
|
|
266
|
+
scope,
|
|
267
|
+
idempotency_key: idemKey,
|
|
268
|
+
reason,
|
|
269
|
+
message: `消息发送结果未能可靠入账。为避免重复发送,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
270
|
+
};
|
|
271
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
await audit?.('builtin_send_error', {
|
|
275
|
+
channel: channelName,
|
|
276
|
+
to: label,
|
|
277
|
+
files: fileAudit,
|
|
278
|
+
error: sent.error,
|
|
279
|
+
idempotency_key: idemKey,
|
|
280
|
+
});
|
|
281
|
+
} catch (error) {
|
|
282
|
+
const reason = `渠道失败响应已记录,但结果审计写入失败:${String(error).slice(0, 300)}`;
|
|
283
|
+
await config.toolCalls.markEvidenceDegraded(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
284
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
285
|
+
state: 'evidence_degraded',
|
|
286
|
+
tool: SEND_TOOL_NAME,
|
|
287
|
+
scope,
|
|
288
|
+
idempotency_key: idemKey,
|
|
289
|
+
reason,
|
|
290
|
+
message: `消息发送结果已经返回,但审计证据不完整。为避免错误重放,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
291
|
+
};
|
|
292
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
await config.toolCalls.complete(job.job_id, SEND_TOOL_NAME, idemHash);
|
|
296
|
+
} catch (error) {
|
|
297
|
+
const reason = `失败响应与审计已记录,但执行日志未能确认完成:${String(error).slice(0, 300)}`;
|
|
298
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
299
|
+
state: 'response_recorded',
|
|
300
|
+
tool: SEND_TOOL_NAME,
|
|
301
|
+
scope,
|
|
302
|
+
idempotency_key: idemKey,
|
|
303
|
+
reason,
|
|
304
|
+
message: `消息发送失败已被确认,但执行日志未完成最终确认。为避免错误重放,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
305
|
+
};
|
|
306
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
307
|
+
}
|
|
308
|
+
return { ok: false, text: resultText };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const resultText = `已发送给 ${label}(渠道 ${channelName})${files.length ? `,含 ${files.length} 个附件` : ''}。`;
|
|
312
|
+
try {
|
|
313
|
+
await config.toolCalls.recordResponse(
|
|
314
|
+
job.job_id,
|
|
315
|
+
SEND_TOOL_NAME,
|
|
316
|
+
idemHash,
|
|
317
|
+
{ ok: true, status: 200, text: resultText },
|
|
318
|
+
);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
const reason = `消息已送达,但执行日志写入失败:${String(error).slice(0, 300)}`;
|
|
321
|
+
await config.toolCalls.markUncertain(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
322
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
323
|
+
state: 'uncertain',
|
|
324
|
+
tool: SEND_TOOL_NAME,
|
|
325
|
+
scope,
|
|
326
|
+
idempotency_key: idemKey,
|
|
327
|
+
reason,
|
|
328
|
+
message: `消息可能已经送达,但发送结果未能可靠入账。为避免重复发送,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
329
|
+
};
|
|
330
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// 入历史:给每个收件人各自 thread 记一条 out(channel='brain-send' ≠ 'hub' → 记忆层渲染为「系统通知→用户」)。附件以 [附件:名] 记录。
|
|
334
|
+
const histContent = [text, ...files.map((f) => `[附件:${f.name}]`)].filter(Boolean).join('\n') || '(空)';
|
|
335
|
+
try {
|
|
336
|
+
await Promise.all(recipients.map(async (rcpt) => {
|
|
337
|
+
const scope = channelScopeKey(ch.kind, ch.name, rcpt);
|
|
338
|
+
const pid = (ch.kind === 'wecom' ? `wxuid:${rcpt}` : `${ch.kind}:${rcpt}`).slice(0, 64);
|
|
339
|
+
const tid = await config.conversations.resolveThread(ch.route_key, scope, pid);
|
|
340
|
+
await config.conversations.appendMessage({ thread_id: tid, direction: 'out', channel: 'brain-send', principal_id: pid, job_id: job.job_id, content: histContent });
|
|
341
|
+
}));
|
|
342
|
+
} catch { /* 历史可降级,不影响已送达 */ }
|
|
343
|
+
try {
|
|
344
|
+
await audit?.('builtin_send', {
|
|
345
|
+
channel: channelName,
|
|
346
|
+
to: label,
|
|
347
|
+
recipients: recipients.length,
|
|
348
|
+
chars: text.length,
|
|
349
|
+
files: fileAudit,
|
|
350
|
+
idempotency_key: idemKey,
|
|
351
|
+
});
|
|
352
|
+
} catch (error) {
|
|
353
|
+
const reason = `消息已送达,但结果审计写入失败:${String(error).slice(0, 300)}`;
|
|
354
|
+
await config.toolCalls.markEvidenceDegraded(job.job_id, SEND_TOOL_NAME, idemHash, reason).catch(() => undefined);
|
|
355
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
356
|
+
state: 'evidence_degraded',
|
|
357
|
+
tool: SEND_TOOL_NAME,
|
|
358
|
+
scope,
|
|
359
|
+
idempotency_key: idemKey,
|
|
360
|
+
reason,
|
|
361
|
+
message: `消息已经送达,但审计证据不完整。为避免重复发送,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
362
|
+
};
|
|
363
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
await config.toolCalls.complete(job.job_id, SEND_TOOL_NAME, idemHash);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
const reason = `发送响应与审计已记录,但执行日志未能确认完成:${String(error).slice(0, 300)}`;
|
|
369
|
+
const uncertainty: ToolExecutionUncertainty = {
|
|
370
|
+
state: 'response_recorded',
|
|
371
|
+
tool: SEND_TOOL_NAME,
|
|
372
|
+
scope,
|
|
373
|
+
idempotency_key: idemKey,
|
|
374
|
+
reason,
|
|
375
|
+
message: `消息已经送达,但执行日志未完成最终确认。为避免重复发送,本任务已禁止自动重试;请使用幂等键 ${idemKey} 人工对账。`,
|
|
376
|
+
};
|
|
377
|
+
return { ok: false, text: uncertainty.message, uncertainty };
|
|
378
|
+
}
|
|
379
|
+
return { ok: true, text: resultText };
|
|
380
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// OSS 默认渠道出站包装:这里才绑定 app/runtime 单组织单例。
|
|
2
|
+
// 自定义部署应使用 channels.ts 的 channelSendFor(config, ...)。
|
|
3
|
+
import { cfgStore } from './runtime';
|
|
4
|
+
import { channelSendFor, type ChannelMessage, type ChannelSendResult } from './channels';
|
|
5
|
+
|
|
6
|
+
export async function channelSend(channelName: string, recipient: string, message: string | ChannelMessage): Promise<ChannelSendResult> {
|
|
7
|
+
return channelSendFor(cfgStore, channelName, recipient, message);
|
|
8
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// 渠道出站原语:把一条消息经「渠道注册表(bz_channels)」的凭证推给某收件人,按 channel.kind 分发。
|
|
2
|
+
// 这是中枢「送达」能力的通用底座——系统告警(sendAlert)是第一个调用方;未来业务侧出站(带 token 调"发给渠道A用户A")
|
|
3
|
+
// 是另一个调用方,另加 client↔channel 授权治理即可,共用本原语。接飞书/钉钉=这里加一个 case,调用方零改动。
|
|
4
|
+
// 不建任何 job、不过 LLM、逐字送达——正式通知所见即所得;与「执行器拉取(wecom-notify)」那套解耦(内部告警不再走它)。
|
|
5
|
+
import { sendWecomText, sendWecomMedia, sendWecomCard, uploadWecomMedia, type WecomAccessTokenCache } from '../adapters/channels/wecom-api';
|
|
6
|
+
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
7
|
+
|
|
8
|
+
export interface ChannelSendResult {
|
|
9
|
+
ok: boolean;
|
|
10
|
+
error?: string;
|
|
11
|
+
/** true 表示失败前已有一部分消息成功送达,调用方不得自动重发。 */
|
|
12
|
+
mayHaveCommitted?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 卡片(企微 textcard:标题+描述+跳转+按钮)。仅对支持卡片的渠道(企微)生效;其它渠道回退发 text。 */
|
|
16
|
+
export interface ChannelCard { type?: string; title: string; description?: string; url: string; btntxt?: string }
|
|
17
|
+
/** 出站消息体:纯文本传 string;带图片/附件/卡片传对象。images 是「业务能访问到的 URL」,中枢拉取→上传渠道→投递。
|
|
18
|
+
* files 每项二选一:`{url,name?}`(已托管文件,中枢拉取)或 `{content,name}`(内联文本内容,如执行器生成的 .md 报告,中枢直接成文件,无需先托管)。 */
|
|
19
|
+
export interface ChannelMessage { text?: string; images?: string[]; files?: { url?: string; name?: string; content?: string; mime?: string }[]; card?: ChannelCard }
|
|
20
|
+
|
|
21
|
+
/** 拉取业务给的 URL 成二进制(带超时 + 大小封顶)。失败返回 {error},不抛。 */
|
|
22
|
+
async function fetchBinary(url: string, maxBytes = 20 * 1024 * 1024): Promise<{ buf: Buffer; mime: string } | { error: string }> {
|
|
23
|
+
if (!/^https?:\/\//i.test(url)) return { error: 'URL 必须是 http(s)' };
|
|
24
|
+
try {
|
|
25
|
+
const r = await fetch(url, { signal: AbortSignal.timeout(20000) });
|
|
26
|
+
if (!r.ok) return { error: `拉取失败 HTTP ${r.status}` };
|
|
27
|
+
const ab = await r.arrayBuffer();
|
|
28
|
+
if (!ab.byteLength) return { error: '内容为空' };
|
|
29
|
+
if (ab.byteLength > maxBytes) return { error: `超过 ${Math.round(maxBytes / 1048576)}MB 上限` };
|
|
30
|
+
const mime = ((r.headers.get('content-type') || '').split(';')[0] || '').trim() || 'application/octet-stream';
|
|
31
|
+
return { buf: Buffer.from(ab), mime };
|
|
32
|
+
} catch (e) { return { error: `拉取异常 ${String(e instanceof Error ? e.message : e).slice(0, 80)}` }; }
|
|
33
|
+
}
|
|
34
|
+
/** 给上传素材取个带扩展名的文件名(企微 media/upload 需要):优先 URL 末段,其次按 mime/类型兜底。 */
|
|
35
|
+
function fileNameFor(url: string, mime: string, kind: 'image' | 'file'): string {
|
|
36
|
+
try { const base = (new URL(url).pathname.split('/').pop() || '').trim(); if (base && /\.[a-z0-9]{1,8}$/i.test(base)) return base; } catch { /* ignore */ }
|
|
37
|
+
if (kind === 'image') { const ext = mime.includes('png') ? 'png' : mime.includes('gif') ? 'gif' : mime.includes('webp') ? 'webp' : 'jpg'; return `image.${ext}`; }
|
|
38
|
+
return 'attachment.bin';
|
|
39
|
+
}
|
|
40
|
+
/** 按文件名扩展名猜 mime(内联内容附件用;发 file 主要靠文件名扩展名,mime 给个合理值即可)。 */
|
|
41
|
+
function mimeByName(name: string): string {
|
|
42
|
+
const ext = (name.split('.').pop() || '').toLowerCase();
|
|
43
|
+
const m: Record<string, string> = { md: 'text/markdown', txt: 'text/plain', log: 'text/plain', json: 'application/json', csv: 'text/csv', html: 'text/html', xml: 'application/xml', yaml: 'text/yaml', yml: 'text/yaml', pdf: 'application/pdf' };
|
|
44
|
+
return m[ext] || 'application/octet-stream';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 渠道会话 scope(与各渠道「入站」逐字一致)。主动出站(/send)写历史时必须用它,
|
|
49
|
+
* 才能落到「用户从该渠道发消息进来时会命中的那个 thread」——差一个字符就接不上历史、追问失忆。
|
|
50
|
+
* 当前仅 wecom:与 wecom.ts flushWecom 的 `wecom:${账号}:${用户id}` 完全一致(kind==='wecom')。
|
|
51
|
+
* 单一来源,杜绝入站/出站漂移。
|
|
52
|
+
*/
|
|
53
|
+
export function channelScopeKey(kind: string, channelName: string, recipient: string): string {
|
|
54
|
+
return `${kind}:${channelName}:${recipient}`.slice(0, 191);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 经 channelName 渠道把消息推给 recipient(渠道原生 id)。message 传 string=纯文本,或对象带 images/files。
|
|
58
|
+
* 渠道不存在/停用/类型不支持/凭证缺失/附件拉取失败 → ok:false + 原因(调用方负责审计)。 */
|
|
59
|
+
export async function channelSendFor(
|
|
60
|
+
config: ConfigStoreContract | null,
|
|
61
|
+
channelName: string,
|
|
62
|
+
recipient: string,
|
|
63
|
+
message: string | ChannelMessage,
|
|
64
|
+
wecomAccessTokenCache?: WecomAccessTokenCache,
|
|
65
|
+
): Promise<ChannelSendResult> {
|
|
66
|
+
if (!config) return { ok: false, error: '无 mysql 后端' };
|
|
67
|
+
if (!recipient) return { ok: false, error: '收件人为空' };
|
|
68
|
+
const msg: ChannelMessage = typeof message === 'string' ? { text: message } : (message ?? {});
|
|
69
|
+
const text = (msg.text ?? '').trim();
|
|
70
|
+
const images = (msg.images ?? []).filter(Boolean);
|
|
71
|
+
const files = (msg.files ?? []).filter((f) => f && (f.url || typeof f.content === 'string'));
|
|
72
|
+
const card = msg.card && msg.card.title && msg.card.url ? msg.card : undefined;
|
|
73
|
+
if (!text && !images.length && !files.length && !card) return { ok: false, error: '正文/附件/卡片均为空' };
|
|
74
|
+
const ch = await config.channels.get(channelName);
|
|
75
|
+
if (!ch || !ch.enabled) return { ok: false, error: `渠道 ${channelName} 不存在或已停用` };
|
|
76
|
+
|
|
77
|
+
if (ch.kind === 'wecom') {
|
|
78
|
+
const cc = ch.config ?? {};
|
|
79
|
+
const corpid = String(cc['corpid'] ?? '').trim();
|
|
80
|
+
const secret = String(cc['secret'] ?? '').trim();
|
|
81
|
+
const agentid = String(cc['agentid'] ?? '').trim();
|
|
82
|
+
if (!corpid || !secret || !agentid) {
|
|
83
|
+
return { ok: false, error: `渠道 ${channelName} 缺出站凭证(需 corpid/secret/agentid,主动推必填)` };
|
|
84
|
+
}
|
|
85
|
+
// 先把所有附件 拉取→上传换 media_id(任一失败即整体中止、一条不发,尽量避免「文字发了图没发」的半截投递)。
|
|
86
|
+
const media: { kind: 'image' | 'file'; mediaId: string }[] = [];
|
|
87
|
+
for (const url of images) {
|
|
88
|
+
const got = await fetchBinary(url);
|
|
89
|
+
if ('error' in got) return { ok: false, error: `图片 ${url}:${got.error}` };
|
|
90
|
+
const up = await uploadWecomMedia(corpid, secret, 'image', got.buf, fileNameFor(url, got.mime, 'image'), got.mime, wecomAccessTokenCache);
|
|
91
|
+
if (!up.ok || !up.mediaId) return { ok: false, error: `图片上传失败:${up.error}` };
|
|
92
|
+
media.push({ kind: 'image', mediaId: up.mediaId });
|
|
93
|
+
}
|
|
94
|
+
for (const f of files) {
|
|
95
|
+
let buf: Buffer; let mime: string; let fname: string;
|
|
96
|
+
if (typeof f.content === 'string') { // 内联文本内容(如执行器生成的 .md 报告):直接成字节,无需先托管 URL
|
|
97
|
+
buf = Buffer.from(f.content, 'utf8');
|
|
98
|
+
if (buf.byteLength > 20 * 1024 * 1024) return { ok: false, error: `附件 ${f.name || ''} 超过 20MB 上限` };
|
|
99
|
+
fname = f.name || 'file.txt';
|
|
100
|
+
mime = f.mime || mimeByName(fname);
|
|
101
|
+
} else { // 已托管 URL:中枢拉取
|
|
102
|
+
const got = await fetchBinary(f.url!);
|
|
103
|
+
if ('error' in got) return { ok: false, error: `附件 ${f.url}:${got.error}` };
|
|
104
|
+
buf = got.buf; mime = f.mime || got.mime; fname = f.name || fileNameFor(f.url!, mime, 'file');
|
|
105
|
+
}
|
|
106
|
+
const up = await uploadWecomMedia(corpid, secret, 'file', buf, fname, mime, wecomAccessTokenCache);
|
|
107
|
+
if (!up.ok || !up.mediaId) return { ok: false, error: `附件上传失败:${up.error}` };
|
|
108
|
+
media.push({ kind: 'file', mediaId: up.mediaId });
|
|
109
|
+
}
|
|
110
|
+
// 发送:卡片优先(card 是 text 的富形态,企微支持时发卡片、text 仅作降级/入历史,不重复发)→ 否则文字 → 各图片 → 各文件(企微每条只能一个类型)。
|
|
111
|
+
let delivered = false;
|
|
112
|
+
if (card) {
|
|
113
|
+
const r = await sendWecomCard(corpid, secret, agentid, recipient, card, wecomAccessTokenCache);
|
|
114
|
+
if (!r.ok) return { ok: false, error: `企微 textcard errcode=${r.errcode} ${r.errmsg}` };
|
|
115
|
+
delivered = true;
|
|
116
|
+
} else if (text) {
|
|
117
|
+
const r = await sendWecomText(corpid, secret, agentid, recipient, text, wecomAccessTokenCache);
|
|
118
|
+
if (!r.ok) {
|
|
119
|
+
return {
|
|
120
|
+
ok: false,
|
|
121
|
+
error: `企微 errcode=${r.errcode} ${r.errmsg}`,
|
|
122
|
+
mayHaveCommitted: r.mayHaveCommitted,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
delivered = true;
|
|
126
|
+
}
|
|
127
|
+
for (const m of media) {
|
|
128
|
+
const r = await sendWecomMedia(corpid, secret, agentid, recipient, m.kind, m.mediaId, wecomAccessTokenCache);
|
|
129
|
+
if (!r.ok) {
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
error: `企微${m.kind} errcode=${r.errcode} ${r.errmsg}${delivered ? '(部分内容可能已送达)' : ''}`,
|
|
133
|
+
mayHaveCommitted: delivered,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
delivered = true;
|
|
137
|
+
}
|
|
138
|
+
return { ok: true };
|
|
139
|
+
}
|
|
140
|
+
return { ok: false, error: `渠道类型 ${ch.kind} 暂不支持主动出站` };
|
|
141
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// OSS 默认送达包装:这里才绑定 app/runtime 单组织单例。
|
|
2
|
+
// 自定义部署应使用 delivery.ts 的 spawnDeliveryJobFor(deps, ...)。
|
|
3
|
+
import { cfg, cfgStore, store } from './runtime';
|
|
4
|
+
import { now, sleep } from './http';
|
|
5
|
+
import { spawnDeliveryJobFor, type DeliveryDeps } from './delivery';
|
|
6
|
+
import type { Job } from '../core/contracts/types';
|
|
7
|
+
|
|
8
|
+
export function defaultDeliveryDeps(): DeliveryDeps {
|
|
9
|
+
return { cfg, configStore: cfgStore, stateStore: store, now, sleep };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function spawnDeliveryJob(parent: Job): Promise<void> {
|
|
13
|
+
return spawnDeliveryJobFor(defaultDeliveryDeps(), parent);
|
|
14
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// 覆盖:送达层的结果通知文案渲染。投递副作用走集成链路,这里先钉住纯函数契约。
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
import { renderDeliveryMessage } from './delivery';
|
|
5
|
+
import type { Job } from '../core/contracts/types';
|
|
6
|
+
|
|
7
|
+
function job(result: Record<string, unknown>): Job {
|
|
8
|
+
return {
|
|
9
|
+
job_id: 'job-1',
|
|
10
|
+
request_id: 'req-1',
|
|
11
|
+
status: 'done',
|
|
12
|
+
target: 'llm',
|
|
13
|
+
profile: 'default',
|
|
14
|
+
project: '',
|
|
15
|
+
source: 'test',
|
|
16
|
+
input_preview: 'input',
|
|
17
|
+
result,
|
|
18
|
+
metadata: {},
|
|
19
|
+
dispatch: { route_name: '员工审核' },
|
|
20
|
+
created_at: '2026-06-30T00:00:00.000Z',
|
|
21
|
+
updated_at: '2026-06-30T00:00:01.000Z',
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test('renderDeliveryMessage: report 结果渲染摘要、等级、证据和建议', () => {
|
|
26
|
+
const text = renderDeliveryMessage(job({
|
|
27
|
+
report: {
|
|
28
|
+
summary: '存在高风险变更',
|
|
29
|
+
severity: 'high',
|
|
30
|
+
category: 'security',
|
|
31
|
+
evidence: ['删除鉴权', '暴露密钥'],
|
|
32
|
+
suggested_next_step: '先回滚再复查',
|
|
33
|
+
},
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
assert.match(text, /【员工审核】结果通知/);
|
|
37
|
+
assert.match(text, /结论:存在高风险变更/);
|
|
38
|
+
assert.match(text, /等级:high/);
|
|
39
|
+
assert.match(text, /1\. 删除鉴权/);
|
|
40
|
+
assert.match(text, /建议:先回滚再复查/);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('renderDeliveryMessage: text 结果渲染正文,空结果返回空串', () => {
|
|
44
|
+
assert.match(renderDeliveryMessage(job({ text: '处理完成' })), /处理完成/);
|
|
45
|
+
assert.equal(renderDeliveryMessage(job({})), '');
|
|
46
|
+
});
|