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,160 @@
|
|
|
1
|
+
// 后台知识库 API:知识库、文档、拉取式数据源、同步和命中测试。
|
|
2
|
+
// 运行时知识注入在 knowledge-runtime;这里仅承接控制台管理面。
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { errMsg, readBody, send } from '../app/http';
|
|
5
|
+
import type { RuntimeStateStore } from '../core/state/state-contracts';
|
|
6
|
+
import type { KbService } from '../services/kb';
|
|
7
|
+
import type { KbSyncService } from '../services/kbsync';
|
|
8
|
+
|
|
9
|
+
export interface AdminKbApiDeps {
|
|
10
|
+
kbService: KbService | null;
|
|
11
|
+
kbSync: KbSyncService | null;
|
|
12
|
+
stateStore: RuntimeStateStore;
|
|
13
|
+
now: () => string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function handleAdminKbApiFor(
|
|
17
|
+
deps: AdminKbApiDeps,
|
|
18
|
+
method: string,
|
|
19
|
+
path: string,
|
|
20
|
+
req: IncomingMessage,
|
|
21
|
+
res: ServerResponse,
|
|
22
|
+
): Promise<boolean> {
|
|
23
|
+
if (!deps.kbService || !(path === '/admin/api/kb' || path.startsWith('/admin/api/kb/'))) return false;
|
|
24
|
+
const knowledge = deps.kbService;
|
|
25
|
+
const sync = deps.kbSync;
|
|
26
|
+
|
|
27
|
+
if (path === '/admin/api/kb') {
|
|
28
|
+
if (method === 'GET') { send(res, 200, await knowledge.listBases()); return true; }
|
|
29
|
+
if (method === 'POST') {
|
|
30
|
+
const b = (await readBody(req)) as Record<string, unknown>;
|
|
31
|
+
const kbId = String(b['kb_id'] ?? '').trim();
|
|
32
|
+
if (!/^[a-z0-9][a-z0-9_-]{1,63}$/.test(kbId)) { send(res, 400, { error: 'kb_id 仅限小写字母/数字/中划线/下划线' }); return true; }
|
|
33
|
+
if (!b['name'] || !b['credential']) { send(res, 400, { error: 'name / credential 必填' }); return true; }
|
|
34
|
+
try {
|
|
35
|
+
await knowledge.upsertBase({
|
|
36
|
+
kb_id: kbId, name: String(b['name']), credential: String(b['credential']).trim(),
|
|
37
|
+
model: String(b['model'] ?? '').trim() || 'text-embedding-v4',
|
|
38
|
+
dim: Number(b['dim'] ?? 1024) || 1024,
|
|
39
|
+
enabled: b['enabled'] !== false, description: b['description'] ? String(b['description']) : undefined,
|
|
40
|
+
writers: Array.isArray(b['writers']) ? (b['writers'] as unknown[]).map(String).filter(Boolean) : [],
|
|
41
|
+
});
|
|
42
|
+
} catch (e) { send(res, 400, { error: errMsg(e) }); return true; }
|
|
43
|
+
send(res, 200, { ok: true });
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const mDocs = path.match(/^\/admin\/api\/kb\/([^/]+)\/docs$/);
|
|
49
|
+
if (mDocs) {
|
|
50
|
+
const kbId = decodeURIComponent(mDocs[1]!);
|
|
51
|
+
if (method === 'GET') { send(res, 200, await knowledge.listDocs(kbId)); return true; }
|
|
52
|
+
if (method === 'POST') {
|
|
53
|
+
const b = (await readBody(req)) as Record<string, unknown>;
|
|
54
|
+
const title = String(b['title'] ?? '').trim();
|
|
55
|
+
const content = String(b['content'] ?? '').trim();
|
|
56
|
+
if (!title || !content) { send(res, 400, { error: 'title / content 必填' }); return true; }
|
|
57
|
+
if (content.length > 500_000) { send(res, 400, { error: '单篇文档上限 50 万字符,请拆分' }); return true; }
|
|
58
|
+
try {
|
|
59
|
+
const docId = await knowledge.addDoc(kbId, title.slice(0, 191), content);
|
|
60
|
+
send(res, 200, { ok: true, doc_id: docId, status: 'embedding' });
|
|
61
|
+
} catch (e) { send(res, 400, { error: errMsg(e) }); }
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const mDoc = path.match(/^\/admin\/api\/kb\/([^/]+)\/docs\/(\d+)$/);
|
|
67
|
+
if (mDoc && method === 'DELETE') {
|
|
68
|
+
await knowledge.deleteDoc(decodeURIComponent(mDoc[1]!), Number(mDoc[2]));
|
|
69
|
+
send(res, 200, { ok: true });
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---- 数据源连接器(拉取式入库)----
|
|
74
|
+
if (sync) {
|
|
75
|
+
const mDsList = path.match(/^\/admin\/api\/kb\/([^/]+)\/datasources$/);
|
|
76
|
+
if (mDsList) {
|
|
77
|
+
const kbId = decodeURIComponent(mDsList[1]!);
|
|
78
|
+
if (!(await knowledge.getBase(kbId))) { send(res, 404, { error: `未知知识库: ${kbId}` }); return true; }
|
|
79
|
+
if (method === 'GET') { send(res, 200, await sync.list(kbId)); return true; }
|
|
80
|
+
if (method === 'POST') {
|
|
81
|
+
const b = (await readBody(req)) as Record<string, unknown>;
|
|
82
|
+
for (const f of ['name', 'db_host', 'db_user', 'db_database', 'query_sql', 'key_field', 'title_field', 'content_template']) {
|
|
83
|
+
if (!String(b[f] ?? '').trim()) { send(res, 400, { error: `${f} 必填` }); return true; }
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const dsId = await sync.upsert({
|
|
87
|
+
ds_id: b['ds_id'] ? Number(b['ds_id']) : undefined, kb_id: kbId,
|
|
88
|
+
name: String(b['name']).trim(), db_host: String(b['db_host']).trim(),
|
|
89
|
+
db_port: Number(b['db_port'] ?? 3306) || 3306, db_user: String(b['db_user']).trim(),
|
|
90
|
+
db_password: String(b['db_password'] ?? ''), db_database: String(b['db_database']).trim(),
|
|
91
|
+
query_sql: String(b['query_sql']).trim(), key_field: String(b['key_field']).trim(),
|
|
92
|
+
title_field: String(b['title_field']).trim(), content_template: String(b['content_template']),
|
|
93
|
+
interval_min: Math.max(Number(b['interval_min'] ?? 60) || 0, 0), enabled: b['enabled'] !== false,
|
|
94
|
+
});
|
|
95
|
+
send(res, 200, { ok: true, ds_id: dsId });
|
|
96
|
+
} catch (e) { send(res, 400, { error: errMsg(e) }); }
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const mDsTest = path.match(/^\/admin\/api\/kb\/([^/]+)\/datasources\/test$/);
|
|
102
|
+
if (mDsTest && method === 'POST') {
|
|
103
|
+
const b = (await readBody(req)) as Record<string, unknown>;
|
|
104
|
+
try {
|
|
105
|
+
// 编辑态密码留空 = 用库里存的那份测
|
|
106
|
+
let password = String(b['db_password'] ?? '');
|
|
107
|
+
if (!password && b['ds_id']) password = (await sync.get(Number(b['ds_id'])))?.db_password ?? '';
|
|
108
|
+
const preview = await sync.preview({
|
|
109
|
+
db_host: String(b['db_host'] ?? '').trim(), db_port: Number(b['db_port'] ?? 3306) || 3306,
|
|
110
|
+
db_user: String(b['db_user'] ?? '').trim(), db_password: password, db_database: String(b['db_database'] ?? '').trim(),
|
|
111
|
+
query_sql: String(b['query_sql'] ?? ''), key_field: String(b['key_field'] ?? '').trim(),
|
|
112
|
+
title_field: String(b['title_field'] ?? '').trim(), content_template: String(b['content_template'] ?? ''),
|
|
113
|
+
});
|
|
114
|
+
send(res, 200, { ok: true, preview });
|
|
115
|
+
} catch (e) { send(res, 400, { error: errMsg(e) }); }
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const mDsSync = path.match(/^\/admin\/api\/kb\/([^/]+)\/datasources\/(\d+)\/sync$/);
|
|
120
|
+
if (mDsSync && method === 'POST') {
|
|
121
|
+
const dsId = Number(mDsSync[2]);
|
|
122
|
+
// 异步跑:首轮全量可能要嵌几百篇,不让 admin 请求挂着等;进度看列表 last_status/last_stats
|
|
123
|
+
void sync.sync(dsId, 'manual')
|
|
124
|
+
.then((stats) => deps.stateStore.appendAudit({ ts: deps.now(), job_id: '-', request_id: 'kb-ds', event: 'kb_ds_sync', detail: { ds_id: dsId, trigger: 'manual', ...stats } }))
|
|
125
|
+
.catch((e) => deps.stateStore.appendAudit({ ts: deps.now(), job_id: '-', request_id: 'kb-ds', event: 'kb_ds_sync_error', detail: { ds_id: dsId, trigger: 'manual', error: errMsg(e) } }))
|
|
126
|
+
.catch(() => undefined);
|
|
127
|
+
send(res, 200, { ok: true, started: true });
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const mDs = path.match(/^\/admin\/api\/kb\/([^/]+)\/datasources\/(\d+)$/);
|
|
132
|
+
if (mDs && method === 'DELETE') {
|
|
133
|
+
const purged = await sync.remove(Number(mDs[2]));
|
|
134
|
+
send(res, 200, { ok: true, purged_docs: purged });
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const mHit = path.match(/^\/admin\/api\/kb\/([^/]+)\/hittest$/);
|
|
140
|
+
if (mHit && method === 'POST') {
|
|
141
|
+
const b = (await readBody(req)) as Record<string, unknown>;
|
|
142
|
+
const query = String(b['query'] ?? '').trim();
|
|
143
|
+
if (!query) { send(res, 400, { error: 'query 必填' }); return true; }
|
|
144
|
+
try {
|
|
145
|
+
// 命中测试不设 min_score:把低分也亮出来,方便后台判断阈值该设多少
|
|
146
|
+
const hits = await knowledge.search(decodeURIComponent(mHit[1]!), query.slice(0, 2000), Number(b['top_k'] ?? 5) || 5, -1);
|
|
147
|
+
send(res, 200, { hits });
|
|
148
|
+
} catch (e) { send(res, 400, { error: errMsg(e) }); }
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const mKb = path.match(/^\/admin\/api\/kb\/([^/]+)$/);
|
|
153
|
+
if (mKb && method === 'DELETE') {
|
|
154
|
+
await knowledge.deleteBase(decodeURIComponent(mKb[1]!));
|
|
155
|
+
send(res, 200, { ok: true });
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// OSS 默认后台运行面包装:这里才绑定 app/runtime 单组织单例。
|
|
2
|
+
// 自定义部署应使用 admin-runtime.ts 的 handleAdminRuntimeApiFor(deps, ...)。
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { channelSend } from '../app/channels-default';
|
|
5
|
+
import { requeueForRerun } from '../app/engine-default';
|
|
6
|
+
import { now } from '../app/http';
|
|
7
|
+
import { cfgStore, isPaused, queue, store } from '../app/runtime';
|
|
8
|
+
import type { Principal } from '../app/auth';
|
|
9
|
+
import { handleAdminRuntimeApiFor, type AdminRuntimeApiDeps } from './admin-runtime';
|
|
10
|
+
|
|
11
|
+
export function defaultAdminRuntimeApiDeps(): AdminRuntimeApiDeps {
|
|
12
|
+
return {
|
|
13
|
+
configStore: cfgStore,
|
|
14
|
+
stateStore: store,
|
|
15
|
+
now,
|
|
16
|
+
isPaused,
|
|
17
|
+
queueStats: () => queue.stats(),
|
|
18
|
+
channelSend,
|
|
19
|
+
engineRuntime: { requeueForRerun },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function handleAdminRuntimeApi(
|
|
24
|
+
method: string,
|
|
25
|
+
path: string,
|
|
26
|
+
req: IncomingMessage,
|
|
27
|
+
res: ServerResponse,
|
|
28
|
+
principal: Principal,
|
|
29
|
+
): Promise<boolean> {
|
|
30
|
+
return handleAdminRuntimeApiFor(defaultAdminRuntimeApiDeps(), method, path, req, res, principal);
|
|
31
|
+
}
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// 后台运行面 API:任务、会话、审批、执行器在线、死信重投、状态与审计。
|
|
2
|
+
// 这里不承载项目/路由/凭证等配置 CRUD,避免 admin.ts 成为所有后台能力的巨型分发器。
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import type { EngineRuntime } from '../app/engine';
|
|
5
|
+
import { send } from '../app/http';
|
|
6
|
+
import { redactValue, redactionSummary } from '../core/runtime/redaction-runtime';
|
|
7
|
+
import { renderDebugReport } from '../core/runtime/debug-report';
|
|
8
|
+
import { buildJobTrace } from '../core/runtime/trace-runtime';
|
|
9
|
+
import type { RuntimeStateStore } from '../core/state/state-contracts';
|
|
10
|
+
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
11
|
+
import type { Principal } from '../app/auth';
|
|
12
|
+
import type { ChannelMessage, ChannelSendResult } from '../app/channels';
|
|
13
|
+
|
|
14
|
+
export type AdminChannelSender = (channelName: string, recipient: string, message: string | ChannelMessage) => Promise<ChannelSendResult>;
|
|
15
|
+
|
|
16
|
+
function textPreview(v: unknown, limit = 2000): string | null {
|
|
17
|
+
if (v == null) return null;
|
|
18
|
+
const s = typeof v === 'string' ? v : JSON.stringify(v);
|
|
19
|
+
return s.length > limit ? `${s.slice(0, limit)}...[truncated ${s.length - limit} chars]` : s;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function routeConfigSnapshot(route: any): Record<string, unknown> | null {
|
|
23
|
+
if (!route) return null;
|
|
24
|
+
return {
|
|
25
|
+
route_key: route.route_key,
|
|
26
|
+
name: route.name,
|
|
27
|
+
enabled: route.enabled,
|
|
28
|
+
target: route.target,
|
|
29
|
+
profile: route.profile,
|
|
30
|
+
project: route.project ?? null,
|
|
31
|
+
session_policy: route.session_policy,
|
|
32
|
+
audience: route.audience ?? null,
|
|
33
|
+
knowledge: route.knowledge ?? null,
|
|
34
|
+
tools: route.tools ?? null,
|
|
35
|
+
delivery: route.delivery ?? null,
|
|
36
|
+
memory: route.memory ?? null,
|
|
37
|
+
budget: route.budget ?? null,
|
|
38
|
+
retry: route.retry ?? null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function approvalSnapshot(a: any): Record<string, unknown> {
|
|
43
|
+
return {
|
|
44
|
+
id: a.id,
|
|
45
|
+
job_id: a.job_id,
|
|
46
|
+
request_id: a.request_id,
|
|
47
|
+
tool: a.tool,
|
|
48
|
+
status: a.status,
|
|
49
|
+
scope: a.scope,
|
|
50
|
+
policy: a.policy,
|
|
51
|
+
reason: a.reason,
|
|
52
|
+
summary: a.summary,
|
|
53
|
+
decided_by: a.decided_by ?? null,
|
|
54
|
+
created_at: a.created_at,
|
|
55
|
+
decided_at: a.decided_at ?? null,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function messageSnapshot(m: any): Record<string, unknown> {
|
|
60
|
+
return {
|
|
61
|
+
id: m.id,
|
|
62
|
+
thread_id: m.thread_id,
|
|
63
|
+
job_id: m.job_id ?? null,
|
|
64
|
+
direction: m.direction,
|
|
65
|
+
channel: m.channel,
|
|
66
|
+
principal_id: m.principal_id,
|
|
67
|
+
content_preview: textPreview(m.content, 1200),
|
|
68
|
+
created_at: m.created_at,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function buildDebugDiagnosis(input: {
|
|
73
|
+
job: any;
|
|
74
|
+
trace: ReturnType<typeof buildJobTrace>;
|
|
75
|
+
approvals: any[];
|
|
76
|
+
deliveryDlq: any[];
|
|
77
|
+
currentRoute: any;
|
|
78
|
+
}): Array<{ severity: 'error' | 'warning' | 'info'; code: string; title: string; detail: string; next_action?: string }> {
|
|
79
|
+
const out: Array<{ severity: 'error' | 'warning' | 'info'; code: string; title: string; detail: string; next_action?: string }> = [];
|
|
80
|
+
const { job, trace, approvals, deliveryDlq, currentRoute } = input;
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
const runAfterMs = job.run_after ? new Date(job.run_after).getTime() : 0;
|
|
83
|
+
const leaseMs = job.lease_until ? new Date(job.lease_until).getTime() : 0;
|
|
84
|
+
const lastError = [...trace.events].reverse().find((e) => e.severity === 'error');
|
|
85
|
+
const pendingApproval = approvals.find((a) => a.status === 'pending');
|
|
86
|
+
|
|
87
|
+
if (job.status === 'queued') {
|
|
88
|
+
if (runAfterMs && runAfterMs > now) {
|
|
89
|
+
out.push({ severity: 'info', code: 'queued_delayed', title: '任务正在等待延迟调度', detail: `run_after=${job.run_after}`, next_action: '等待退避时间到达;若这是非预期延迟,检查 retry/backoff 配置。' });
|
|
90
|
+
} else if (job.thread_id) {
|
|
91
|
+
out.push({ severity: 'warning', code: 'queued_thread_fifo', title: '任务仍在队列,可能受同 thread 串行约束影响', detail: `thread_id=${job.thread_id}`, next_action: '到「执行器 → 调度租约」查看该 thread 是否有在途任务阻塞队头。' });
|
|
92
|
+
} else {
|
|
93
|
+
out.push({ severity: 'warning', code: 'queued_waiting_claim', title: '任务等待执行器或本地调度认领', detail: `target=${job.target ?? '(unknown)'}`, next_action: '检查目标是否启用、执行器是否在线、队列是否积压。' });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if ((job.status === 'running' || job.status === 'dispatched') && leaseMs) {
|
|
97
|
+
if (leaseMs < now) {
|
|
98
|
+
out.push({ severity: 'error', code: 'lease_expired', title: '任务租约已过期', detail: `lease_until=${job.lease_until}`, next_action: '检查执行器是否离线;等待 reaper 回队或手动排查该 executor。' });
|
|
99
|
+
} else {
|
|
100
|
+
out.push({ severity: 'info', code: 'lease_active', title: '任务租约有效', detail: `lease_until=${job.lease_until}`, next_action: '若长时间不收尾,检查执行器日志和目标超时配置。' });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (job.status === 'error' || job.status === 'rejected') {
|
|
104
|
+
out.push({
|
|
105
|
+
severity: 'error',
|
|
106
|
+
code: job.status === 'rejected' ? 'job_rejected' : 'job_error',
|
|
107
|
+
title: job.status === 'rejected' ? '任务被策略拒绝' : '任务执行失败',
|
|
108
|
+
detail: job.error ?? lastError?.summary ?? '无错误摘要',
|
|
109
|
+
next_action: lastError ? `查看 trace 事件 ${lastError.event} 的 detail。` : '查看 job.error 和执行器日志。',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (pendingApproval) {
|
|
113
|
+
out.push({ severity: 'warning', code: 'approval_pending', title: '存在待处理审批意图', detail: `approval_id=${pendingApproval.id} tool=${pendingApproval.tool}`, next_action: '到「审批意图」处理,或确认业务侧审批回调是否已承接。' });
|
|
114
|
+
}
|
|
115
|
+
if (deliveryDlq.length) {
|
|
116
|
+
out.push({ severity: 'error', code: 'delivery_dlq', title: '存在送达死信', detail: `${deliveryDlq.length} 条送达最终失败`, next_action: '到「送达死信」查看失败原因,修复渠道后重投。' });
|
|
117
|
+
}
|
|
118
|
+
if (job.dispatch?.route_key && !currentRoute) {
|
|
119
|
+
out.push({ severity: 'warning', code: 'route_missing_now', title: '当前配置中找不到该任务使用的路由', detail: `route_key=${job.dispatch.route_key}`, next_action: '确认路由是否被删除;排障以 job_snapshot 为准。' });
|
|
120
|
+
} else if (currentRoute && currentRoute.enabled === false) {
|
|
121
|
+
out.push({ severity: 'warning', code: 'route_disabled_now', title: '该任务使用的路由当前已停用', detail: `route_key=${currentRoute.route_key}`, next_action: '确认这是预期停用,或重新启用路由后重试。' });
|
|
122
|
+
}
|
|
123
|
+
if (trace.summary.error_count && lastError && !out.some((d) => d.code === 'job_error')) {
|
|
124
|
+
out.push({ severity: 'error', code: 'trace_error_event', title: 'Trace 中存在错误事件', detail: `${lastError.event}: ${lastError.summary || lastError.title}`, next_action: '展开该错误事件 detail,定位工具、送达或上下文装配失败原因。' });
|
|
125
|
+
}
|
|
126
|
+
if (!out.length) {
|
|
127
|
+
out.push({ severity: 'info', code: 'no_obvious_issue', title: '未发现明确阻断项', detail: '任务状态、租约、审批、送达死信未显示明显异常。', next_action: '继续查看 trace 时间线和模型/工具返回内容。' });
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface AdminRuntimeApiDeps {
|
|
133
|
+
configStore: ConfigStoreContract | null;
|
|
134
|
+
stateStore: RuntimeStateStore;
|
|
135
|
+
now: () => string;
|
|
136
|
+
isPaused: () => boolean;
|
|
137
|
+
queueStats: () => unknown;
|
|
138
|
+
channelSend: AdminChannelSender;
|
|
139
|
+
engineRuntime: Pick<EngineRuntime, 'requeueForRerun'>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function handleAdminRuntimeApiFor(
|
|
143
|
+
deps: AdminRuntimeApiDeps,
|
|
144
|
+
method: string,
|
|
145
|
+
path: string,
|
|
146
|
+
req: IncomingMessage,
|
|
147
|
+
res: ServerResponse,
|
|
148
|
+
principal: Principal,
|
|
149
|
+
): Promise<boolean> {
|
|
150
|
+
if (!deps.configStore) return false;
|
|
151
|
+
const configStore = deps.configStore;
|
|
152
|
+
const stateStore = deps.stateStore;
|
|
153
|
+
|
|
154
|
+
async function tracePayload(jobId: string): Promise<Record<string, unknown> | null> {
|
|
155
|
+
const job = await stateStore.getJob(jobId);
|
|
156
|
+
if (!job) return null;
|
|
157
|
+
const routeKey = String(job.dispatch?.route_key ?? '').trim();
|
|
158
|
+
const [rawInput, audit, approvals, messages, deliveryDlq, currentRoute] = await Promise.all([
|
|
159
|
+
configStore.conversations.rawInputForJob(jobId).catch(() => null),
|
|
160
|
+
configStore.observability.auditForJob(jobId),
|
|
161
|
+
configStore.approvals.forJob(jobId),
|
|
162
|
+
configStore.conversations.messagesForJob(jobId),
|
|
163
|
+
configStore.deliveryDlq.listByParentJob(jobId, true, 100).catch(() => []),
|
|
164
|
+
routeKey ? configStore.routes.get(routeKey).catch(() => null) : Promise.resolve(null),
|
|
165
|
+
]);
|
|
166
|
+
const jobWithRaw = { ...job, raw_input: rawInput };
|
|
167
|
+
const trace = buildJobTrace({ job, audit, approvals, messages });
|
|
168
|
+
const dispatchSnapshot = {
|
|
169
|
+
status: job.status,
|
|
170
|
+
target: job.target ?? null,
|
|
171
|
+
profile: job.profile,
|
|
172
|
+
project: job.project,
|
|
173
|
+
route_key: routeKey || null,
|
|
174
|
+
route_name: job.dispatch?.route_name ?? null,
|
|
175
|
+
run_after: job.run_after ?? null,
|
|
176
|
+
claimed_at: job.claimed_at ?? null,
|
|
177
|
+
lease_until: job.lease_until ?? null,
|
|
178
|
+
executor_id: job.executor_id ?? null,
|
|
179
|
+
dispatched_at: job.dispatched_at ?? null,
|
|
180
|
+
attempts: job.attempts ?? 0,
|
|
181
|
+
has_claim_token: !!job.claim_token,
|
|
182
|
+
updated_at: job.updated_at,
|
|
183
|
+
};
|
|
184
|
+
const debugBundle = redactValue({
|
|
185
|
+
kind: 'bailing-job-debug-bundle',
|
|
186
|
+
version: 1,
|
|
187
|
+
generated_at: new Date().toISOString(),
|
|
188
|
+
redaction: {
|
|
189
|
+
...redactionSummary(),
|
|
190
|
+
note: '凭证、令牌、密钥和常见个人信息会在排障包内脱敏;原始 job/trace 本体仍仅面向授权后台查看。',
|
|
191
|
+
},
|
|
192
|
+
identifiers: {
|
|
193
|
+
job_id: job.job_id,
|
|
194
|
+
request_id: job.request_id,
|
|
195
|
+
client_app_id: job.client_app_id ?? null,
|
|
196
|
+
thread_id: job.thread_id ?? null,
|
|
197
|
+
route_key: routeKey || null,
|
|
198
|
+
principal_id: (job.metadata?.principal as any)?.id ?? job.metadata?.principal_id ?? null,
|
|
199
|
+
},
|
|
200
|
+
principal: job.metadata?.principal ?? null,
|
|
201
|
+
dispatch: dispatchSnapshot,
|
|
202
|
+
route: {
|
|
203
|
+
current_exists: !!currentRoute,
|
|
204
|
+
current: routeConfigSnapshot(currentRoute),
|
|
205
|
+
job_snapshot: {
|
|
206
|
+
target_config: job.dispatch?.target_config ?? null,
|
|
207
|
+
delivery: job.dispatch?.delivery ?? null,
|
|
208
|
+
retry: job.dispatch?.retry ?? null,
|
|
209
|
+
tools: job.dispatch?.tools ?? null,
|
|
210
|
+
memory: job.dispatch?.memory ?? null,
|
|
211
|
+
kb_refs: job.dispatch?.kb_refs ?? [],
|
|
212
|
+
user_images: job.dispatch?.user_images ?? [],
|
|
213
|
+
user_audio: job.dispatch?.user_audio ?? [],
|
|
214
|
+
user_files: job.dispatch?.user_files ?? [],
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
outcome: {
|
|
218
|
+
status: job.status,
|
|
219
|
+
error: job.error ?? null,
|
|
220
|
+
usage: job.usage ?? null,
|
|
221
|
+
result_preview: textPreview(job.result ?? job.report ?? job.raw_result, 2000),
|
|
222
|
+
},
|
|
223
|
+
counts: {
|
|
224
|
+
audit_events: trace.events.length,
|
|
225
|
+
approvals: approvals.length,
|
|
226
|
+
messages: messages.length,
|
|
227
|
+
delivery_dlq: deliveryDlq.length,
|
|
228
|
+
trace_errors: trace.summary.error_count,
|
|
229
|
+
trace_warnings: trace.summary.warning_count,
|
|
230
|
+
},
|
|
231
|
+
approvals: approvals.map(approvalSnapshot),
|
|
232
|
+
delivery_dlq: deliveryDlq,
|
|
233
|
+
messages: messages.map(messageSnapshot),
|
|
234
|
+
trace: trace.summary,
|
|
235
|
+
events: trace.events,
|
|
236
|
+
diagnosis: buildDebugDiagnosis({ job, trace, approvals, deliveryDlq, currentRoute }),
|
|
237
|
+
raw_input_preview: textPreview(rawInput, 2000),
|
|
238
|
+
});
|
|
239
|
+
return {
|
|
240
|
+
job: jobWithRaw,
|
|
241
|
+
trace,
|
|
242
|
+
approvals,
|
|
243
|
+
messages,
|
|
244
|
+
lookup: {
|
|
245
|
+
job_id: job.job_id,
|
|
246
|
+
request_id: job.request_id,
|
|
247
|
+
client_app_id: job.client_app_id ?? null,
|
|
248
|
+
thread_id: job.thread_id ?? null,
|
|
249
|
+
route_key: job.dispatch?.route_key ?? null,
|
|
250
|
+
principal: job.metadata?.principal ?? null,
|
|
251
|
+
},
|
|
252
|
+
debug_bundle: debugBundle,
|
|
253
|
+
debug_report: renderDebugReport(debugBundle as Record<string, any>),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ---- 送达死信队列:最终失败的送达可查可重投(闭环 delivery_failed_* 告警)----
|
|
258
|
+
if (path === '/admin/api/delivery-dlq' && method === 'GET') {
|
|
259
|
+
const u = new URL(req.url ?? '', 'http://x');
|
|
260
|
+
send(res, 200, { items: await configStore.deliveryDlq.list(u.searchParams.get('all') === '1', Number(u.searchParams.get('limit') ?? 100)) });
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
const mDlqResend = path.match(/^\/admin\/api\/delivery-dlq\/(\d+)\/resend$/);
|
|
264
|
+
if (mDlqResend && method === 'POST') {
|
|
265
|
+
const id = Number(mDlqResend[1]);
|
|
266
|
+
const d = await configStore.deliveryDlq.get(id);
|
|
267
|
+
if (!d) { send(res, 404, { error: '死信不存在' }); return true; }
|
|
268
|
+
if (d.resolved) { send(res, 200, { ok: true, note: '已处理过,未重复重投' }); return true; }
|
|
269
|
+
const sent = await deps.channelSend(d.channel, d.recipient, d.content).catch((e) => ({ ok: false as const, error: String(e) }));
|
|
270
|
+
if (sent.ok) await configStore.deliveryDlq.resolve(id);
|
|
271
|
+
await stateStore.appendAudit({ ts: deps.now(), job_id: '-', request_id: 'delivery-dlq', event: sent.ok ? 'dlq_resend_ok' : 'dlq_resend_failed', detail: { id, channel: d.channel, to: d.recipient, ...(sent.ok ? {} : { error: (sent as { error?: string }).error }) } }).catch(() => undefined);
|
|
272
|
+
send(res, sent.ok ? 200 : 502, sent.ok ? { ok: true } : { ok: false, error: (sent as { error?: string }).error ?? '重投失败' });
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
const mDlqResolve = path.match(/^\/admin\/api\/delivery-dlq\/(\d+)\/resolve$/);
|
|
276
|
+
if (mDlqResolve && method === 'POST') {
|
|
277
|
+
await configStore.deliveryDlq.resolve(Number(mDlqResolve[1]));
|
|
278
|
+
send(res, 200, { ok: true });
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (path === '/admin/api/runs' && method === 'GET') {
|
|
283
|
+
const q = new URL(req.url ?? '/', 'http://x').searchParams;
|
|
284
|
+
send(res, 200, await configStore.observability.listRecentJobs(Number(q.get('limit')) || 50, Number(q.get('offset')) || 0));
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (path === '/admin/api/runs/trace' && method === 'GET') {
|
|
289
|
+
const q = new URL(req.url ?? '/', 'http://x').searchParams;
|
|
290
|
+
const jobId = String(q.get('job_id') ?? '').trim();
|
|
291
|
+
if (/^[0-9a-f-]{36}$/i.test(jobId)) {
|
|
292
|
+
const payload = await tracePayload(jobId);
|
|
293
|
+
if (!payload) { send(res, 404, { error: 'job 不存在' }); return true; }
|
|
294
|
+
send(res, 200, payload);
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
const requestId = String(q.get('request_id') ?? '').trim();
|
|
298
|
+
const clientAppId = String(q.get('client_id') ?? q.get('client_app_id') ?? '').trim();
|
|
299
|
+
const principalId = String(q.get('principal_id') ?? '').trim();
|
|
300
|
+
const threadRaw = String(q.get('thread_id') ?? '').trim();
|
|
301
|
+
const threadId = threadRaw && /^\d+$/.test(threadRaw) ? Number(threadRaw) : undefined;
|
|
302
|
+
const matches = await configStore.observability.findJobs({
|
|
303
|
+
...(requestId ? { requestId } : {}),
|
|
304
|
+
...(clientAppId ? { clientAppId } : {}),
|
|
305
|
+
...(principalId ? { principalId } : {}),
|
|
306
|
+
...(threadId !== undefined ? { threadId } : {}),
|
|
307
|
+
limit: Number(q.get('limit')) || 20,
|
|
308
|
+
});
|
|
309
|
+
if (matches.length === 1 && q.get('list') !== '1') {
|
|
310
|
+
const payload = await tracePayload(matches[0]!.job_id);
|
|
311
|
+
if (payload) { send(res, 200, { ...payload, matches }); return true; }
|
|
312
|
+
}
|
|
313
|
+
send(res, 200, { matches, count: matches.length });
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (path === '/admin/api/dispatch-status' && method === 'GET') {
|
|
318
|
+
send(res, 200, await configStore.observability.dispatchStatus());
|
|
319
|
+
return true;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// 成本可观测(先行):近 N 天花费聚合(默认 30 天)。只读,不设预算硬闸——先让花费看得见。
|
|
323
|
+
if (path === '/admin/api/cost' && method === 'GET') {
|
|
324
|
+
const days = Number(new URL(req.url ?? '/', 'http://x').searchParams.get('days')) || 30;
|
|
325
|
+
send(res, 200, await configStore.observability.costSummary(days));
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// 会话视图(调度台「会话」Tab):列线索 / 取单会话全量(消息总账+对齐的 job_id,执行轨迹复用 /runs/:job 接口懒拉)
|
|
330
|
+
if (path === '/admin/api/threads' && method === 'GET') {
|
|
331
|
+
const q = new URL(req.url ?? '/', 'http://x').searchParams;
|
|
332
|
+
send(res, 200, await configStore.conversations.listRecentThreads(Number(q.get('limit')) || 80, Number(q.get('offset')) || 0));
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
const mThread = path.match(/^\/admin\/api\/threads\/(\d+)$/);
|
|
336
|
+
if (mThread && method === 'GET') {
|
|
337
|
+
const d = await configStore.conversations.threadDetail(Number(mThread[1]));
|
|
338
|
+
if (!d) { send(res, 404, { error: '会话不存在' }); return true; }
|
|
339
|
+
send(res, 200, d);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 任务详情 / 审计时间线 / 重跑
|
|
344
|
+
const mRun = path.match(/^\/admin\/api\/runs\/([0-9a-f-]{36})(\/audit|\/rerun|\/trace)?$/);
|
|
345
|
+
if (mRun) {
|
|
346
|
+
const jobId = mRun[1]!;
|
|
347
|
+
const job = await stateStore.getJob(jobId);
|
|
348
|
+
if (!job) { send(res, 404, { error: 'job 不存在' }); return true; }
|
|
349
|
+
if (method === 'GET' && !mRun[2]) {
|
|
350
|
+
// raw_input = 未装配的原始触发输入(总账 in 消息),供审计区分"用户发了啥"与"我们组装成的 input";无总账时前端回落 input_preview
|
|
351
|
+
const rawInput = await configStore.conversations.rawInputForJob(jobId).catch(() => null);
|
|
352
|
+
send(res, 200, { ...job, raw_input: rawInput });
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
if (method === 'GET' && mRun[2] === '/audit') {
|
|
356
|
+
send(res, 200, await configStore.observability.auditForJob(jobId));
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
if (method === 'GET' && mRun[2] === '/trace') {
|
|
360
|
+
// 单 job 全链聚合:把散在 job / 审计 / 工具审批 / 总账 4 处的回放素材一次取齐(控制台原先要 2+ 次调用拼)。
|
|
361
|
+
send(res, 200, await tracePayload(jobId));
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
if (method === 'POST' && mRun[2] === '/rerun') {
|
|
365
|
+
if (job.status === 'queued' || job.status === 'running' || job.status === 'dispatched') {
|
|
366
|
+
send(res, 400, { error: `任务在途(${job.status}),无需重跑` });
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
await deps.engineRuntime.requeueForRerun(job, principal.kind === 'admin' ? principal.username ?? 'token' : '?', 'console');
|
|
370
|
+
send(res, 200, { ok: true, job_id: jobId, status: 'queued' });
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ---- 工具审批(确认车道 B:批准后自动重跑;批准范围锁定当时那个调用快照)----
|
|
376
|
+
if (path === '/admin/api/tool-approvals' && method === 'GET') {
|
|
377
|
+
const status = new URL(req.url ?? '/', 'http://x').searchParams.get('status') ?? 'pending';
|
|
378
|
+
send(res, 200, await configStore.approvals.list(status === 'all' ? undefined : status));
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
const mApproval = path.match(/^\/admin\/api\/tool-approvals\/(\d+)\/(approve|deny)$/);
|
|
382
|
+
if (mApproval && method === 'POST') {
|
|
383
|
+
const id = Number(mApproval[1]);
|
|
384
|
+
const action = mApproval[2] as 'approve' | 'deny';
|
|
385
|
+
const appr = await configStore.approvals.get(id);
|
|
386
|
+
if (!appr) { send(res, 404, { error: '审批单不存在' }); return true; }
|
|
387
|
+
if (appr.status !== 'pending') { send(res, 400, { error: `审批单已是 ${appr.status},不可重复裁决` }); return true; }
|
|
388
|
+
const by = principal.kind === 'admin' ? principal.username ?? 'token' : '?';
|
|
389
|
+
if (!(await configStore.approvals.decide(id, action === 'approve' ? 'approved' : 'denied', by))) {
|
|
390
|
+
send(res, 409, { error: '审批单状态已变化,请刷新后重试' });
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
await stateStore.appendAudit({
|
|
394
|
+
ts: deps.now(), job_id: appr.job_id, request_id: appr.request_id,
|
|
395
|
+
event: action === 'approve' ? 'tool_approved' : 'tool_denied',
|
|
396
|
+
detail: { approval_id: id, tool: appr.tool, scope: appr.scope, by, policy: appr.policy, reason: appr.reason, summary: appr.summary },
|
|
397
|
+
});
|
|
398
|
+
// 批准 → 自动重跑原任务(任务在途则不动:运行中会自然消费批准单)
|
|
399
|
+
let rerun = false;
|
|
400
|
+
if (action === 'approve') {
|
|
401
|
+
const job = await stateStore.getJob(appr.job_id);
|
|
402
|
+
if (job && (job.status === 'done' || job.status === 'error' || job.status === 'rejected')) {
|
|
403
|
+
await deps.engineRuntime.requeueForRerun(job, by, `approval_${id}`);
|
|
404
|
+
rerun = true;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
send(res, 200, { ok: true, id, status: action === 'approve' ? 'approved' : 'denied', rerun });
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// 执行器富列表(控制台「执行器」页):在线状态 + 最后心跳 + 声明的 targets + 自报能力(profiles/claude 版本/标签)。
|
|
412
|
+
// 路由/目标的「服务池与覆盖度」由前端拿这份 + /targets + /routes 做 join 计算(中枢不替前端预算,保持后端简单)。
|
|
413
|
+
if (path === '/admin/api/executors' && method === 'GET') {
|
|
414
|
+
const list = (await configStore.executors.list().catch(() => [])).map((e) => ({
|
|
415
|
+
...e, online: Date.now() - new Date(e.last_seen_at).getTime() < 2 * 60_000, // 与 /status 同口径:2 分钟无心跳=离线
|
|
416
|
+
}));
|
|
417
|
+
send(res, 200, list);
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
// 注销执行器(退役/换名/selftest 清场):删心跳记录,不再参与离线告警
|
|
421
|
+
if (path.startsWith('/admin/api/executors/') && method === 'DELETE') {
|
|
422
|
+
await configStore.executors.delete(decodeURIComponent(path.slice('/admin/api/executors/'.length)));
|
|
423
|
+
send(res, 200, { ok: true });
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 配置变更审计(谁在什么时候改了什么)
|
|
428
|
+
if (path === '/admin/api/config-audit' && method === 'GET') {
|
|
429
|
+
const q = new URL(req.url ?? '/', 'http://x').searchParams;
|
|
430
|
+
send(res, 200, await configStore.observability.recentConfigAudit(Number(q.get('limit')) || 100, Number(q.get('offset')) || 0));
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// 系统状态(执行器在线/队列/暂停位):所有登录角色可见
|
|
435
|
+
if (path === '/admin/api/status' && method === 'GET') {
|
|
436
|
+
const executors = (await configStore.executors.list().catch(() => [])).map((e) => ({
|
|
437
|
+
...e, online: Date.now() - new Date(e.last_seen_at).getTime() < 2 * 60_000, // 执行器 ~12s 一轮 claim,2 分钟没动静=离线
|
|
438
|
+
}));
|
|
439
|
+
send(res, 200, { paused: deps.isPaused(), queue: deps.queueStats(), executors });
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// OSS 默认后台工具源 API 包装:这里才绑定 app/runtime 单组织单例。
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
+
import { now, sleep } from '../app/http';
|
|
4
|
+
import { cfg, cfgStore, store, toolIndex } from '../app/runtime';
|
|
5
|
+
import type { Principal } from '../app/auth';
|
|
6
|
+
import { handleAdminToolProviderApiFor, type AdminToolProviderApiDeps } from './admin-tool-providers';
|
|
7
|
+
|
|
8
|
+
export function defaultAdminToolProviderApiDeps(): AdminToolProviderApiDeps {
|
|
9
|
+
return { cfg, configStore: cfgStore, stateStore: store, toolIndex, now, sleep };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function handleAdminToolProviderApi(
|
|
13
|
+
method: string,
|
|
14
|
+
path: string,
|
|
15
|
+
req: IncomingMessage,
|
|
16
|
+
res: ServerResponse,
|
|
17
|
+
principal: Principal,
|
|
18
|
+
): Promise<boolean> {
|
|
19
|
+
return handleAdminToolProviderApiFor(defaultAdminToolProviderApiDeps(), method, path, req, res, principal);
|
|
20
|
+
}
|