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,334 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Bailing\Connect;
|
|
4
|
+
|
|
5
|
+
use InvalidArgumentException;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* PHP 7.3 兼容的工具 spec 构建器(fluent builder,无注解无反射)。
|
|
9
|
+
*
|
|
10
|
+
* 与 8.x 注解版(SpecBuilder + #[AiTool])产出**完全一致**的 OpenAPI spec——
|
|
11
|
+
* 中枢不关心 spec 怎么生成的,只认 spec 内容。PHP 7.3/7.4 项目用这个,无需迁 PHP 版本。
|
|
12
|
+
*
|
|
13
|
+
* 设计原则同 8.x 版:**会被中枢跳过/误用的问题在构建期就报错**,而不是发布后才在控制台发现。
|
|
14
|
+
*
|
|
15
|
+
* 用法:
|
|
16
|
+
* $spec = ToolSpec::create('示例商城')
|
|
17
|
+
* ->tool('staff_list', 'GET', '/openapi/staff/list', 'tenant.staff.read', '查询门店员工列表',
|
|
18
|
+
* function (ToolDef $t) {
|
|
19
|
+
* $t->query('dept', 'string', false, '按部门过滤');
|
|
20
|
+
* })
|
|
21
|
+
* ->tool('staff_delete', 'DELETE', '/openapi/staff/delete', 'tenant.staff.delete', '删除门店员工',
|
|
22
|
+
* function (ToolDef $t) {
|
|
23
|
+
* $t->risk('high')->confirm('AI 申请删除员工 #{id}')->requiresSubject();
|
|
24
|
+
* $t->body('id', 'integer', true, '员工 ID');
|
|
25
|
+
* })
|
|
26
|
+
* ->buildJson();
|
|
27
|
+
*/
|
|
28
|
+
final class ToolSpec
|
|
29
|
+
{
|
|
30
|
+
/** @var string */
|
|
31
|
+
private $title;
|
|
32
|
+
/** @var string */
|
|
33
|
+
private $version;
|
|
34
|
+
/** @var ToolDef[] */
|
|
35
|
+
private $tools = array();
|
|
36
|
+
/** @var string[] 构建期警告(不阻塞,CI 里打到 stderr 提醒整改) */
|
|
37
|
+
private $warnings = array();
|
|
38
|
+
/** @var array|null */
|
|
39
|
+
private $authzProbe = null;
|
|
40
|
+
|
|
41
|
+
public function __construct($title = '业务系统', $version = '1.0.0')
|
|
42
|
+
{
|
|
43
|
+
$this->title = $title;
|
|
44
|
+
$this->version = $version;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public static function create($title = '业务系统', $version = '1.0.0')
|
|
48
|
+
{
|
|
49
|
+
return new self($title, $version);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 声明一个工具。$configure 为闭包 function (ToolDef $t),在其中设可选字段与参数。
|
|
54
|
+
* @param callable|null $configure
|
|
55
|
+
*/
|
|
56
|
+
public function tool($name, $method, $path, $scope, $description, $configure = null)
|
|
57
|
+
{
|
|
58
|
+
$def = new ToolDef($name, $method, $path, $scope, $description);
|
|
59
|
+
if ($configure !== null) {
|
|
60
|
+
if (!is_callable($configure)) {
|
|
61
|
+
throw new InvalidArgumentException('tool() 第 6 个参数须为闭包 function (ToolDef $t) {...}');
|
|
62
|
+
}
|
|
63
|
+
call_user_func($configure, $def);
|
|
64
|
+
}
|
|
65
|
+
$this->tools[] = $def;
|
|
66
|
+
return $this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 声明独立授权探针端点。中枢刷新工具源时会探测业务侧授权闸是否 fail-closed。 */
|
|
70
|
+
public function authzProbe($path = '/.well-known/bailing/authz-probe', $method = 'POST')
|
|
71
|
+
{
|
|
72
|
+
$method = strtoupper(trim((string) $method));
|
|
73
|
+
if (!in_array($method, array('GET', 'POST'), true)) {
|
|
74
|
+
throw new InvalidArgumentException("authzProbe method 仅支持 GET/POST,收到 {$method}");
|
|
75
|
+
}
|
|
76
|
+
if (strpos((string) $path, '/') !== 0) {
|
|
77
|
+
throw new InvalidArgumentException("authzProbe path 必须以 / 开头,收到 {$path}");
|
|
78
|
+
}
|
|
79
|
+
$this->authzProbe = array('method' => $method, 'path' => (string) $path);
|
|
80
|
+
return $this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** @return string[] 最近一次 build() 产生的警告 */
|
|
84
|
+
public function warnings()
|
|
85
|
+
{
|
|
86
|
+
return $this->warnings;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @return array openapi 数组 */
|
|
90
|
+
public function build()
|
|
91
|
+
{
|
|
92
|
+
$this->warnings = array();
|
|
93
|
+
$paths = array();
|
|
94
|
+
$seenNames = array();
|
|
95
|
+
$seenRoutes = array();
|
|
96
|
+
|
|
97
|
+
foreach ($this->tools as $d) {
|
|
98
|
+
$where = "工具 {$d->name}";
|
|
99
|
+
|
|
100
|
+
// ---- 构建期体检(与中枢派生规则、8.x SpecBuilder 一致)----
|
|
101
|
+
if (!in_array($d->method, array('GET', 'POST', 'PUT', 'PATCH', 'DELETE'), true)) {
|
|
102
|
+
throw new InvalidArgumentException("{$where}: method 仅支持 GET/POST/PUT/PATCH/DELETE,收到 {$d->method}");
|
|
103
|
+
}
|
|
104
|
+
if (strpos((string) $d->path, '/') !== 0) {
|
|
105
|
+
throw new InvalidArgumentException("{$where}: path 必须以 / 开头,收到 {$d->path}");
|
|
106
|
+
}
|
|
107
|
+
if (trim((string) $d->scope) === '') {
|
|
108
|
+
throw new InvalidArgumentException("{$where}: scope 不能为空(中枢会直接跳过该接口)");
|
|
109
|
+
}
|
|
110
|
+
if (trim((string) $d->name) === '') {
|
|
111
|
+
throw new InvalidArgumentException("path {$d->path}: 工具名(第 1 个参数)不能为空");
|
|
112
|
+
}
|
|
113
|
+
$risk = isset($d->opt['risk']) ? $d->opt['risk'] : 'low';
|
|
114
|
+
if (!in_array($risk, array('low', 'medium', 'high'), true)) {
|
|
115
|
+
throw new InvalidArgumentException("{$where}: risk 仅支持 low/medium/high,收到 {$risk}");
|
|
116
|
+
}
|
|
117
|
+
if (isset($d->opt['rateLimit']) && self::parseRateLimit($d->opt['rateLimit']) === null) {
|
|
118
|
+
throw new InvalidArgumentException("{$where}: rateLimit 格式应为 \"30/min\"、\"600/hour\"、\"10/s\" 或 \"1000/day\",收到 {$d->opt['rateLimit']}");
|
|
119
|
+
}
|
|
120
|
+
if (isset($d->opt['timeoutMs']) && ($d->opt['timeoutMs'] < 1 || $d->opt['timeoutMs'] > 600000)) {
|
|
121
|
+
throw new InvalidArgumentException("{$where}: timeoutMs 取值 1~600000,收到 {$d->opt['timeoutMs']}");
|
|
122
|
+
}
|
|
123
|
+
// 弱校验:工具多时中枢走渐进披露,目录阶段 AI 只看到 description 第一句——首句必须自含语义
|
|
124
|
+
$firstSentence = explode('。', (string) $d->description);
|
|
125
|
+
$firstSentence = $firstSentence[0];
|
|
126
|
+
if (mb_strlen(trim($firstSentence)) < 6) {
|
|
127
|
+
$this->warnings[] = "{$where}: description 首句过短(\"{$firstSentence}\")——工具目录阶段 AI 只看到这一句,请写自含语义的完整说明(如\"查询门店员工列表\")";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (isset($seenNames[$d->name])) {
|
|
131
|
+
throw new InvalidArgumentException("{$where}: 工具名 {$d->name} 与 {$seenNames[$d->name]} 重复(operationId 必须唯一)");
|
|
132
|
+
}
|
|
133
|
+
$seenNames[$d->name] = $where;
|
|
134
|
+
$routeKey = "{$d->method} {$d->path}";
|
|
135
|
+
if (isset($seenRoutes[$routeKey])) {
|
|
136
|
+
throw new InvalidArgumentException("{$where}: {$routeKey} 与 {$seenRoutes[$routeKey]} 重复");
|
|
137
|
+
}
|
|
138
|
+
$seenRoutes[$routeKey] = $where;
|
|
139
|
+
|
|
140
|
+
$isDeprecated = !empty($d->opt['deprecated']);
|
|
141
|
+
if ($d->method !== 'GET' && !$d->params && !$isDeprecated) {
|
|
142
|
+
throw new InvalidArgumentException(
|
|
143
|
+
"{$where}: 写接口({$d->method})必须用 body()/query() 声明至少一个参数——中枢不暴露无参数 schema 的写接口(防 AI 瞎猜参数)"
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
$paths[$d->path][strtolower($d->method)] = $this->buildOperation($d, $where);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!$paths) {
|
|
151
|
+
throw new InvalidArgumentException('没有声明任何工具:请用 ->tool(...) 至少声明一个');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
$spec = array(
|
|
155
|
+
'openapi' => '3.0.0',
|
|
156
|
+
'info' => array('title' => $this->title, 'version' => $this->version),
|
|
157
|
+
'paths' => $paths,
|
|
158
|
+
);
|
|
159
|
+
if ($this->authzProbe !== null) {
|
|
160
|
+
$spec['x-bailing-authz-probe'] = $this->authzProbe;
|
|
161
|
+
}
|
|
162
|
+
return $spec;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** build() 的 JSON 形态(发布到工具源 spec_url 的就是它)。 */
|
|
166
|
+
public function buildJson($pretty = true)
|
|
167
|
+
{
|
|
168
|
+
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ($pretty ? JSON_PRETTY_PRINT : 0);
|
|
169
|
+
$json = json_encode($this->build(), $flags);
|
|
170
|
+
return $json !== false ? $json : '{}';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private static function parseRateLimit($value)
|
|
174
|
+
{
|
|
175
|
+
if ($value === null || $value === '') {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
$text = strtolower(preg_replace('/\s+/', '', (string) $value));
|
|
179
|
+
if (!preg_match('#^(\d+)/(s|sec|second|min|minute|h|hour|d|day)$#', $text, $m)) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
$unit = $m[2];
|
|
183
|
+
if ($unit === 's' || $unit === 'sec' || $unit === 'second') {
|
|
184
|
+
$window = '1s';
|
|
185
|
+
} elseif ($unit === 'h' || $unit === 'hour') {
|
|
186
|
+
$window = '1h';
|
|
187
|
+
} elseif ($unit === 'd' || $unit === 'day') {
|
|
188
|
+
$window = '1d';
|
|
189
|
+
} else {
|
|
190
|
+
$window = '1m';
|
|
191
|
+
}
|
|
192
|
+
return array('count' => (int) $m[1], 'window' => $window);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private function buildCapability(ToolDef $d)
|
|
196
|
+
{
|
|
197
|
+
$o = $d->opt;
|
|
198
|
+
$method = $d->method;
|
|
199
|
+
$capability = array('version' => 1, 'enabled' => true, 'scope' => $d->scope);
|
|
200
|
+
$risk = isset($o['risk']) ? $o['risk'] : 'low';
|
|
201
|
+
if ($risk !== 'low') {
|
|
202
|
+
$capability['risk'] = array('level' => $risk);
|
|
203
|
+
}
|
|
204
|
+
if (!empty($o['confirm']) || !empty($o['confirmWhen']) || isset($o['confirmPrompt'])) {
|
|
205
|
+
$approval = array();
|
|
206
|
+
if (!empty($o['confirm'])) {
|
|
207
|
+
$approval['required'] = true;
|
|
208
|
+
}
|
|
209
|
+
if (!empty($o['confirmWhen'])) {
|
|
210
|
+
$approval['when'] = $o['confirmWhen'];
|
|
211
|
+
}
|
|
212
|
+
if (isset($o['confirmPrompt'])) {
|
|
213
|
+
$approval['prompt'] = $o['confirmPrompt'];
|
|
214
|
+
}
|
|
215
|
+
$capability['approval'] = $approval;
|
|
216
|
+
}
|
|
217
|
+
if (!empty($o['requiresSubject'])) {
|
|
218
|
+
$capability['subject'] = array('required' => true);
|
|
219
|
+
}
|
|
220
|
+
$execution = array();
|
|
221
|
+
$readonly = array_key_exists('readonly', $o) ? $o['readonly'] : ($method === 'GET');
|
|
222
|
+
if ($readonly && $method !== 'GET') {
|
|
223
|
+
$execution['readonly'] = true;
|
|
224
|
+
}
|
|
225
|
+
$idempotent = array_key_exists('idempotent', $o) ? $o['idempotent'] : ($method === 'GET');
|
|
226
|
+
if ($idempotent && $method !== 'GET') {
|
|
227
|
+
$execution['idempotent'] = true;
|
|
228
|
+
}
|
|
229
|
+
if (isset($o['rateLimit'])) {
|
|
230
|
+
$rateLimit = self::parseRateLimit($o['rateLimit']);
|
|
231
|
+
if ($rateLimit !== null) {
|
|
232
|
+
$execution['rate_limit'] = $rateLimit;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (isset($o['timeoutMs'])) {
|
|
236
|
+
$execution['timeout_ms'] = $o['timeoutMs'];
|
|
237
|
+
}
|
|
238
|
+
if ($execution) {
|
|
239
|
+
$capability['execution'] = $execution;
|
|
240
|
+
}
|
|
241
|
+
if (!empty($o['sensitive'])) {
|
|
242
|
+
$capability['audit'] = array('sensitive' => true);
|
|
243
|
+
}
|
|
244
|
+
$guidance = array();
|
|
245
|
+
if (isset($o['whenToUse'])) {
|
|
246
|
+
$guidance['when_to_use'] = $o['whenToUse'];
|
|
247
|
+
}
|
|
248
|
+
if (isset($o['returns'])) {
|
|
249
|
+
$guidance['returns'] = $o['returns'];
|
|
250
|
+
}
|
|
251
|
+
if (!empty($o['examples'])) {
|
|
252
|
+
$guidance['examples'] = $o['examples'];
|
|
253
|
+
}
|
|
254
|
+
if (!empty($o['context'])) {
|
|
255
|
+
$guidance['context'] = array_values(array_map('strval', $o['context']));
|
|
256
|
+
}
|
|
257
|
+
if ($guidance) {
|
|
258
|
+
$capability['guidance'] = $guidance;
|
|
259
|
+
}
|
|
260
|
+
return $capability;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private function buildOperation(ToolDef $d, $where)
|
|
264
|
+
{
|
|
265
|
+
$o = $d->opt;
|
|
266
|
+
$method = $d->method;
|
|
267
|
+
$op = array(
|
|
268
|
+
'operationId' => $d->name,
|
|
269
|
+
'summary' => $d->description,
|
|
270
|
+
'x-agent-capability' => $this->buildCapability($d),
|
|
271
|
+
);
|
|
272
|
+
// 非核心 OpenAPI 字段才继续落到 operation 顶层;治理字段统一放入 ACC。
|
|
273
|
+
if (!empty($o['tags'])) {
|
|
274
|
+
$op['tags'] = array_values(array_map('strval', $o['tags']));
|
|
275
|
+
}
|
|
276
|
+
if (!empty($o['deprecated'])) {
|
|
277
|
+
$op['deprecated'] = true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 参数分流:query 进 parameters、body 聚合成 requestBody schema
|
|
281
|
+
$queryParams = array();
|
|
282
|
+
$bodyProps = array();
|
|
283
|
+
$bodyRequired = array();
|
|
284
|
+
foreach ($d->params as $p) {
|
|
285
|
+
if (!in_array($p['type'], array('string', 'integer', 'number', 'boolean', 'array'), true)) {
|
|
286
|
+
throw new InvalidArgumentException("{$where}: 参数 {$p['name']} type 仅支持 string/integer/number/boolean/array,收到 {$p['type']}");
|
|
287
|
+
}
|
|
288
|
+
$schema = array('type' => $p['type']);
|
|
289
|
+
if (isset($p['description']) && $p['description'] !== '') {
|
|
290
|
+
$schema['description'] = $p['description'];
|
|
291
|
+
}
|
|
292
|
+
if (array_key_exists('enum', $p) && $p['enum'] !== null) {
|
|
293
|
+
$schema['enum'] = array_values($p['enum']);
|
|
294
|
+
}
|
|
295
|
+
if (array_key_exists('default', $p) && $p['default'] !== null) {
|
|
296
|
+
$schema['default'] = $p['default'];
|
|
297
|
+
}
|
|
298
|
+
if (array_key_exists('format', $p) && $p['format'] !== null) {
|
|
299
|
+
$schema['format'] = $p['format'];
|
|
300
|
+
}
|
|
301
|
+
if ($p['type'] === 'array') {
|
|
302
|
+
$schema['items'] = array('type' => isset($p['itemsType']) && $p['itemsType'] !== null ? $p['itemsType'] : 'string');
|
|
303
|
+
}
|
|
304
|
+
$in = isset($p['in']) ? $p['in'] : ($method === 'GET' ? 'query' : 'body');
|
|
305
|
+
if (!in_array($in, array('query', 'body'), true)) {
|
|
306
|
+
throw new InvalidArgumentException("{$where}: 参数 {$p['name']} in 仅支持 query/body,收到 {$in}");
|
|
307
|
+
}
|
|
308
|
+
if ($in === 'query') {
|
|
309
|
+
$queryParams[] = array(
|
|
310
|
+
'name' => $p['name'],
|
|
311
|
+
'in' => 'query',
|
|
312
|
+
'required' => $p['required'],
|
|
313
|
+
'schema' => $schema,
|
|
314
|
+
);
|
|
315
|
+
} else {
|
|
316
|
+
$bodyProps[$p['name']] = $schema;
|
|
317
|
+
if ($p['required']) {
|
|
318
|
+
$bodyRequired[] = $p['name'];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if ($queryParams) {
|
|
323
|
+
$op['parameters'] = $queryParams;
|
|
324
|
+
}
|
|
325
|
+
if ($bodyProps) {
|
|
326
|
+
$bodySchema = array('type' => 'object', 'properties' => $bodyProps);
|
|
327
|
+
if ($bodyRequired) {
|
|
328
|
+
$bodySchema['required'] = $bodyRequired;
|
|
329
|
+
}
|
|
330
|
+
$op['requestBody'] = array('content' => array('application/json' => array('schema' => $bodySchema)));
|
|
331
|
+
}
|
|
332
|
+
return $op;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Bailing\Connect;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 百灵中枢工具调用验签(CONTRACT.md §2.4b)。PHP 7.3 兼容版(签名公式与 8.x 版逐字一致)。
|
|
7
|
+
* 签名方案 sha256=(算法名,非版本号)。
|
|
8
|
+
*
|
|
9
|
+
* 职责边界:验签只回答"真是中枢发的吗";X-Bailing-On-Behalf-Of 是谁、
|
|
10
|
+
* 有没有权限做这件事,由你接着用自己既有的权限体系裁决——
|
|
11
|
+
* AI 调用与人点按钮走同一条裁决路径。
|
|
12
|
+
*/
|
|
13
|
+
final class Verify
|
|
14
|
+
{
|
|
15
|
+
/**
|
|
16
|
+
* 验工具调用签名。任何一步不过都返回 false(调用方应回 401)。
|
|
17
|
+
*
|
|
18
|
+
* @param string $secret 控制台「工具源」登记的签名密钥
|
|
19
|
+
* @param string $method $_SERVER['REQUEST_METHOD']
|
|
20
|
+
* @param string $pathWithQuery 含 query 的请求路径(ThinkPHP: $request->url();裸 PHP: $_SERVER['REQUEST_URI'])
|
|
21
|
+
* @param string $rawBody 原始请求体(GET 为空串):file_get_contents('php://input')
|
|
22
|
+
* @param string $timestamp X-Bailing-Timestamp 头(unix 秒)
|
|
23
|
+
* @param string $signature X-Bailing-Signature 头(形如 sha256=abc...)
|
|
24
|
+
* @param int $windowSec 时间窗(秒),默认 300
|
|
25
|
+
* @return bool
|
|
26
|
+
*/
|
|
27
|
+
public static function toolCall($secret, $method, $pathWithQuery, $rawBody, $timestamp, $signature, $windowSec = 300, $onBehalfOf = '', $jobId = '')
|
|
28
|
+
{
|
|
29
|
+
return self::failureReason($secret, $method, $pathWithQuery, $rawBody, $timestamp, $signature, $windowSec, $onBehalfOf, $jobId) === null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 验签并返回失败原因(null = 通过)。接入期头号坑是服务器时钟偏移——
|
|
34
|
+
* 401 响应里区分 timestamp_out_of_window 与 bad_signature 能省一半联调时间,
|
|
35
|
+
* 且不泄密(时间窗机制本就写在公开契约里)。记得先给服务器对时(ntp/chrony)。
|
|
36
|
+
*
|
|
37
|
+
* @return string|null null / 'timestamp_out_of_window' / 'bad_signature'
|
|
38
|
+
*/
|
|
39
|
+
public static function failureReason($secret, $method, $pathWithQuery, $rawBody, $timestamp, $signature, $windowSec = 300, $onBehalfOf = '', $jobId = '')
|
|
40
|
+
{
|
|
41
|
+
if (abs(time() - (int) $timestamp) >= $windowSec) {
|
|
42
|
+
return 'timestamp_out_of_window';
|
|
43
|
+
}
|
|
44
|
+
$base = $timestamp . '.' . strtoupper($method) . '.' . $pathWithQuery . '.' . hash('sha256', $rawBody);
|
|
45
|
+
// 签名方案 sha256=(算法名,非版本号;GitHub webhook 同款约定):材料含 On-Behalf-Of + Job-Id,
|
|
46
|
+
// 把"谁、为哪个任务"钉进 HMAC,杜绝窗口内重放再篡改这两个头换租户/绕幂等(CONTRACT §2.4b)。
|
|
47
|
+
$expect = 'sha256=' . hash_hmac('sha256', $base . '.' . $onBehalfOf . '.' . $jobId, $secret);
|
|
48
|
+
return hash_equals($expect, $signature) ? null : 'bad_signature'; // 恒时比较,防时序侧信道
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 裸 PHP 便捷入口:直接验当前请求(从超全局取齐材料)。
|
|
53
|
+
*
|
|
54
|
+
* 中枢签名的 path 段是你 spec 里声明的 operation path(不含工具源 base_url 的路径前缀,见 CONTRACT §2.4b)。
|
|
55
|
+
* - base_url 为纯源站(无路径前缀)时:REQUEST_URI 即等于签名 path,$knownPath 留空即可;
|
|
56
|
+
* - base_url 带路径前缀(如 https://shop.com/openapi),或框架会重写 REQUEST_URI(ThinkPHP pathinfo 等)时:
|
|
57
|
+
* 传 $knownPath = 该端点的 spec path(如 '/goods/create')——只借用 REQUEST_URI 的 query 段,路径段用你给的
|
|
58
|
+
* spec path,与 base_url 前缀 / 框架重写彻底解耦(推荐)。
|
|
59
|
+
*
|
|
60
|
+
* @param string $secret
|
|
61
|
+
* @param string|null $knownPath 该端点在 spec 里声明的 path;传了就用它当路径段
|
|
62
|
+
* @return bool
|
|
63
|
+
*/
|
|
64
|
+
public static function currentRequest($secret, $knownPath = null)
|
|
65
|
+
{
|
|
66
|
+
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
|
67
|
+
if ($knownPath !== null) {
|
|
68
|
+
$q = strpos($uri, '?');
|
|
69
|
+
$uri = $knownPath . ($q !== false ? substr($uri, $q) : '');
|
|
70
|
+
}
|
|
71
|
+
return self::toolCall(
|
|
72
|
+
$secret,
|
|
73
|
+
isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '',
|
|
74
|
+
$uri,
|
|
75
|
+
file_get_contents('php://input') ?: '',
|
|
76
|
+
isset($_SERVER['HTTP_X_BAILING_TIMESTAMP']) ? $_SERVER['HTTP_X_BAILING_TIMESTAMP'] : '',
|
|
77
|
+
isset($_SERVER['HTTP_X_BAILING_SIGNATURE']) ? $_SERVER['HTTP_X_BAILING_SIGNATURE'] : '',
|
|
78
|
+
300,
|
|
79
|
+
// sha256 签名材料:原样取这两个头(中枢签的就是它发的,逐字一致)。
|
|
80
|
+
isset($_SERVER['HTTP_X_BAILING_ON_BEHALF_OF']) ? $_SERVER['HTTP_X_BAILING_ON_BEHALF_OF'] : '',
|
|
81
|
+
isset($_SERVER['HTTP_X_BAILING_JOB_ID']) ? $_SERVER['HTTP_X_BAILING_JOB_ID'] : ''
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 工具端点的【推荐】关卡:验签 + 强制授权裁决,一步到位、fail-closed。
|
|
87
|
+
*
|
|
88
|
+
* 为什么不用 currentRequest 然后自己判断权限?因为那会让"授权"沦为可选的、可被忘记/注释掉的一步——
|
|
89
|
+
* 中枢整套安全模型的支柱正是"业务管 authority"。gate() 把 $authorize 做成【必填】回调,不传根本调不了本方法,
|
|
90
|
+
* 从代码层把"这个人此刻能不能做这件事"钉成必填依赖。
|
|
91
|
+
*
|
|
92
|
+
* ⚠️ 切勿写成 function(){ return true; }:那等于把授权整个关掉,洞原样还在、还更隐蔽(看起来像实现了)。
|
|
93
|
+
* 回调拿到 ($operator, $tool, $params) 三个入参,就是要你用【既有权限表】真正裁决——
|
|
94
|
+
* AI 调用与人点按钮必须走同一条裁决路径。盲目返回 true = 在代码上显眼地把这三个参数全扔了。
|
|
95
|
+
*
|
|
96
|
+
* 注意服务器到服务器调用没有用户 session:业务平时基于登录态的鉴权对中枢调用不生效,
|
|
97
|
+
* 所以把 $operator(On-Behalf-Of) 接进你的权限表,是这条链路【唯一】的授权闸,不接 = 认证了但没授权。
|
|
98
|
+
*
|
|
99
|
+
* @param string $secret 控制台「工具源」登记的签名密钥
|
|
100
|
+
* @param callable $authorize function($operator, $tool, $params): bool —— 能否执行($params 是 query+body 合并)。
|
|
101
|
+
* $operator 空串=匿名任务(网页访客等),按你的业务语义决定拒绝或只读放行。
|
|
102
|
+
* @param string $tool 本端点对应的工具名/operationId(拼进回调,便于按工具分流授权)
|
|
103
|
+
* @param string|null $knownPath spec 里声明的 path(base_url 带前缀/框架重写 REQUEST_URI 时传,见 currentRequest)
|
|
104
|
+
* @return array ['ok'=>bool,'code'=>int,'error'=>?string,'operator'=>string,'jobId'=>string]
|
|
105
|
+
* ok=true 放行;否则按 code 回错:401=验签失败(不是中枢发的)/ 403=授权拒绝(这个人不能做)。
|
|
106
|
+
*/
|
|
107
|
+
public static function gate($secret, callable $authorize, $tool = '', $knownPath = null)
|
|
108
|
+
{
|
|
109
|
+
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
|
|
110
|
+
if ($knownPath !== null) {
|
|
111
|
+
$q = strpos($uri, '?');
|
|
112
|
+
$uri = $knownPath . ($q !== false ? substr($uri, $q) : '');
|
|
113
|
+
}
|
|
114
|
+
$rawBody = file_get_contents('php://input') ?: '';
|
|
115
|
+
$operator = isset($_SERVER['HTTP_X_BAILING_ON_BEHALF_OF']) ? $_SERVER['HTTP_X_BAILING_ON_BEHALF_OF'] : '';
|
|
116
|
+
$jobId = isset($_SERVER['HTTP_X_BAILING_JOB_ID']) ? $_SERVER['HTTP_X_BAILING_JOB_ID'] : '';
|
|
117
|
+
$reason = self::failureReason(
|
|
118
|
+
$secret,
|
|
119
|
+
isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '',
|
|
120
|
+
$uri,
|
|
121
|
+
$rawBody,
|
|
122
|
+
isset($_SERVER['HTTP_X_BAILING_TIMESTAMP']) ? $_SERVER['HTTP_X_BAILING_TIMESTAMP'] : '',
|
|
123
|
+
isset($_SERVER['HTTP_X_BAILING_SIGNATURE']) ? $_SERVER['HTTP_X_BAILING_SIGNATURE'] : '',
|
|
124
|
+
300,
|
|
125
|
+
$operator,
|
|
126
|
+
$jobId
|
|
127
|
+
);
|
|
128
|
+
if ($reason !== null) {
|
|
129
|
+
return ['ok' => false, 'code' => 401, 'error' => $reason, 'operator' => $operator, 'jobId' => $jobId];
|
|
130
|
+
}
|
|
131
|
+
// 强制授权:必填回调用既有权限表裁决(不接 authority = 没接好这套安全模型)
|
|
132
|
+
$allowed = (bool) $authorize($operator, $tool, self::paramsFrom($rawBody));
|
|
133
|
+
if (!$allowed) {
|
|
134
|
+
return ['ok' => false, 'code' => 403, 'error' => 'forbidden', 'operator' => $operator, 'jobId' => $jobId];
|
|
135
|
+
}
|
|
136
|
+
return ['ok' => true, 'code' => 200, 'error' => null, 'operator' => $operator, 'jobId' => $jobId];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 合并本次调用参数(query + JSON body),喂给 gate 的 authorize 回调做参数级裁决。 */
|
|
140
|
+
private static function paramsFrom($rawBody)
|
|
141
|
+
{
|
|
142
|
+
$params = isset($_GET) ? $_GET : [];
|
|
143
|
+
if ($rawBody !== '') {
|
|
144
|
+
$body = json_decode($rawBody, true);
|
|
145
|
+
if (is_array($body)) {
|
|
146
|
+
$params = array_merge($params, $body);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return $params;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 当前请求的操作主体(验签通过后再用!);匿名任务(网页访客等)返回 null。 */
|
|
153
|
+
public static function onBehalfOf()
|
|
154
|
+
{
|
|
155
|
+
$v = isset($_SERVER['HTTP_X_BAILING_ON_BEHALF_OF']) ? $_SERVER['HTTP_X_BAILING_ON_BEHALF_OF'] : '';
|
|
156
|
+
return $v !== '' ? $v : null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 当前请求所属的中枢任务号(审计回查用)。 */
|
|
160
|
+
public static function jobId()
|
|
161
|
+
{
|
|
162
|
+
$v = isset($_SERVER['HTTP_X_BAILING_JOB_ID']) ? $_SERVER['HTTP_X_BAILING_JOB_ID'] : '';
|
|
163
|
+
return $v !== '' ? $v : null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# 百灵中枢 Python SDK
|
|
2
|
+
|
|
3
|
+
业务系统侧 SDK,用来生成工具源 OpenAPI、签发聊天访客票据、校验中枢工具调用签名、校验 callback 签名、实现 authorize 探针,并调用中枢 `/run`、`/jobs/{id}`、`/send`。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install bailing-connect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
仓库内本地验证:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
python3 sdk/python/examples/build_spec.py > tools.json
|
|
15
|
+
python3 sdk/python/examples/build_spec.py | npm run sdk:test-python
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 生成工具源
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import json
|
|
22
|
+
from bailing_connect import build_openapi_spec, param, tool
|
|
23
|
+
|
|
24
|
+
spec = build_openapi_spec(
|
|
25
|
+
title="CRM 工具源",
|
|
26
|
+
version="1.0.0",
|
|
27
|
+
authz_probe={"method": "POST", "path": "/.well-known/bailing/authz-probe"},
|
|
28
|
+
tools=[
|
|
29
|
+
tool(
|
|
30
|
+
name="member_query",
|
|
31
|
+
method="GET",
|
|
32
|
+
path="/api/members/{id}",
|
|
33
|
+
description="查询会员基础资料",
|
|
34
|
+
scope="member.read",
|
|
35
|
+
requiresSubject=True,
|
|
36
|
+
params=[
|
|
37
|
+
param("id", **{"in": "path", "required": True, "description": "会员 ID"})
|
|
38
|
+
],
|
|
39
|
+
),
|
|
40
|
+
tool(
|
|
41
|
+
name="refund_request_create",
|
|
42
|
+
method="POST",
|
|
43
|
+
path="/api/refunds/requests",
|
|
44
|
+
description="创建退款申请",
|
|
45
|
+
scope="refund.request",
|
|
46
|
+
risk="medium",
|
|
47
|
+
requiresSubject=True,
|
|
48
|
+
confirmWhen=[{"param": "amount", "op": ">", "value": 500, "label": "超过 500 元退款需人工确认"}],
|
|
49
|
+
params=[
|
|
50
|
+
param("order_id", required=True, description="订单 ID"),
|
|
51
|
+
param("amount", type="number", required=True, description="退款金额,单位元"),
|
|
52
|
+
param("reason", required=True, description="退款原因"),
|
|
53
|
+
],
|
|
54
|
+
)
|
|
55
|
+
],
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
print(json.dumps(spec, ensure_ascii=False, indent=2))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 验签与授权
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from bailing_connect import verify_tool_call
|
|
65
|
+
|
|
66
|
+
raw_body = request.get_data(as_text=True)
|
|
67
|
+
path_with_query = request.full_path.rstrip("?")
|
|
68
|
+
on_behalf_of = request.headers.get("x-bailing-on-behalf-of", "")
|
|
69
|
+
job_id = request.headers.get("x-bailing-job-id", "")
|
|
70
|
+
|
|
71
|
+
ok = verify_tool_call(
|
|
72
|
+
secret=os.environ["BAILING_TOOL_SECRET"],
|
|
73
|
+
method=request.method,
|
|
74
|
+
path_with_query=path_with_query,
|
|
75
|
+
body=raw_body,
|
|
76
|
+
timestamp=request.headers.get("x-bailing-timestamp"),
|
|
77
|
+
signature=request.headers.get("x-bailing-signature"),
|
|
78
|
+
on_behalf_of=on_behalf_of,
|
|
79
|
+
job_id=job_id,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
if not ok:
|
|
83
|
+
abort(401)
|
|
84
|
+
if not can_user_read_member(on_behalf_of):
|
|
85
|
+
abort(403)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
验签只证明请求来自中枢,不代表这个主体有权限执行该动作。业务工具端点必须先验签,再按 `X-Bailing-On-Behalf-Of` 走自身权限表做授权裁决。
|
|
89
|
+
|
|
90
|
+
## 访客票据与 HubClient
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
import os
|
|
94
|
+
from bailing_connect import HubClient, sign_ticket
|
|
95
|
+
|
|
96
|
+
ticket = sign_ticket(os.environ["BAILING_CLIENT_TOKEN"], f"{tenant_id}:{user_id}")
|
|
97
|
+
|
|
98
|
+
hub = HubClient("https://hub.example.com", os.environ["BAILING_CLIENT_TOKEN"])
|
|
99
|
+
job = hub.run(
|
|
100
|
+
request_id=f"crm_{order_id}",
|
|
101
|
+
route="order-support",
|
|
102
|
+
input="查询订单处理建议",
|
|
103
|
+
metadata={"principal": {"id": str(user_id), "tenant": str(tenant_id)}},
|
|
104
|
+
)
|
|
105
|
+
result = hub.get_job(job["job_id"])
|
|
106
|
+
hub.send("notice_1001", "team-im", "user_001", "任务已完成")
|
|
107
|
+
```
|