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,81 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* ThinkPHP 8 接入示例(复制改造,不是可直接运行的文件)。
|
|
7
|
+
* 三件事:①spec 发布路由 ②验签中间件 ③(可选)聊天票据签发。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// ============================================================
|
|
11
|
+
// ① spec 发布路由 —— route/app.php
|
|
12
|
+
// ============================================================
|
|
13
|
+
/*
|
|
14
|
+
use think\facade\Route;
|
|
15
|
+
|
|
16
|
+
Route::get('.well-known/bailing/tools.json', function () {
|
|
17
|
+
$secret = config('bailing.tool_secret');
|
|
18
|
+
$spec = (new \Bailing\Connect\SpecBuilder(title: '你的业务系统'))
|
|
19
|
+
->addClass(\app\opentenantapi\controller\StaffController::class)
|
|
20
|
+
->addClass(\app\opentenantapi\controller\OrderController::class);
|
|
21
|
+
// 传 secret = 只对中枢开放(中枢拉取带 sha256= 签名)。
|
|
22
|
+
// 确需公开时显式使用 handlePublic(),并在控制台选择 public_allowed。
|
|
23
|
+
[$status, $body] = \Bailing\Connect\SpecServer::handle(
|
|
24
|
+
$spec,
|
|
25
|
+
$secret,
|
|
26
|
+
request()->method(),
|
|
27
|
+
request()->url(),
|
|
28
|
+
request()->header(),
|
|
29
|
+
);
|
|
30
|
+
$response = response($body, $status);
|
|
31
|
+
$response->header(\Bailing\Connect\SpecServer::responseHeaders($secret));
|
|
32
|
+
return $response;
|
|
33
|
+
});
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
// ============================================================
|
|
37
|
+
// ② 验签中间件 —— app/common/middleware/BailingVerify.php
|
|
38
|
+
// 挂在暴露给 AI 的路由分组上;验签通过后主体放进 request,
|
|
39
|
+
// 控制器里照常走你自己的权限体系裁决。
|
|
40
|
+
// ============================================================
|
|
41
|
+
/*
|
|
42
|
+
namespace app\common\middleware;
|
|
43
|
+
|
|
44
|
+
use Bailing\Connect\Verify;
|
|
45
|
+
use Closure;
|
|
46
|
+
use think\Request;
|
|
47
|
+
use think\Response;
|
|
48
|
+
|
|
49
|
+
class BailingVerify
|
|
50
|
+
{
|
|
51
|
+
public function handle(Request $request, Closure $next): Response
|
|
52
|
+
{
|
|
53
|
+
$ok = Verify::toolCall(
|
|
54
|
+
config('bailing.tool_secret'), // 与中枢「工具源」登记一致
|
|
55
|
+
$request->method(),
|
|
56
|
+
$request->url(), // 含 query
|
|
57
|
+
$request->getInput(), // 原始 body
|
|
58
|
+
$request->header('x-bailing-timestamp', ''),
|
|
59
|
+
$request->header('x-bailing-signature', ''),
|
|
60
|
+
);
|
|
61
|
+
if (!$ok) {
|
|
62
|
+
return json(['error' => 'bad signature'], 401);
|
|
63
|
+
}
|
|
64
|
+
// 操作主体(可能为空 = 匿名任务):用你既有的权限体系裁决,
|
|
65
|
+
// AI 调用与人点按钮走同一条路径
|
|
66
|
+
$request->aiOperator = $request->header('x-bailing-on-behalf-of', '');
|
|
67
|
+
return $next($request);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
// ============================================================
|
|
73
|
+
// ③ 聊天组件带登录身份 —— 页面渲染处
|
|
74
|
+
// ============================================================
|
|
75
|
+
/*
|
|
76
|
+
$ticket = \Bailing\Connect\Ticket::sign(
|
|
77
|
+
config('bailing.client_token'), // 你在中枢「接入方」的 token,永不进前端
|
|
78
|
+
(string) $user->id,
|
|
79
|
+
);
|
|
80
|
+
// <script src="https://中枢域名/widget.js" data-entry="pub_xxx" data-ticket="<?= $ticket ?>"></script>
|
|
81
|
+
*/
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Bailing\Connect\Attributes;
|
|
6
|
+
|
|
7
|
+
use Attribute;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 声明工具的一个参数(可重复标注)。参数定义越细,AI 调用越准——
|
|
11
|
+
* description / enum / format / default 都会原样喂给模型。
|
|
12
|
+
*
|
|
13
|
+
* 写接口(非 GET)必须至少声明一个参数,否则中枢不暴露该工具(不让 AI 瞎猜参数),
|
|
14
|
+
* SpecBuilder 在构建期就会报错拦下。
|
|
15
|
+
*/
|
|
16
|
+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
|
17
|
+
final class AiParam
|
|
18
|
+
{
|
|
19
|
+
public function __construct(
|
|
20
|
+
/** 参数名 */
|
|
21
|
+
public string $name,
|
|
22
|
+
/** 给 AI 看的参数说明,务必写清语义与取值习惯 */
|
|
23
|
+
public string $description = '',
|
|
24
|
+
/** JSON Schema 类型:string / integer / number / boolean / array */
|
|
25
|
+
public string $type = 'string',
|
|
26
|
+
/** 是否必填 */
|
|
27
|
+
public bool $required = false,
|
|
28
|
+
/** 参数位置:query / body;缺省 GET→query、其他→body */
|
|
29
|
+
public ?string $in = null,
|
|
30
|
+
/** 枚举取值(强约束,AI 只会传这些值) */
|
|
31
|
+
public ?array $enum = null,
|
|
32
|
+
/** 默认值(提示性,AI 可见) */
|
|
33
|
+
public mixed $default = null,
|
|
34
|
+
/** 格式提示,如 date / date-time / email */
|
|
35
|
+
public ?string $format = null,
|
|
36
|
+
/** type=array 时的元素类型 */
|
|
37
|
+
public ?string $itemsType = null,
|
|
38
|
+
) {
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Bailing\Connect\Attributes;
|
|
6
|
+
|
|
7
|
+
use Attribute;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 把一个控制器方法声明为 Agent 可调工具(字段遵循 ACC,接入约定见百灵中枢 CONTRACT.md §2.4a)。
|
|
11
|
+
*
|
|
12
|
+
* 字段按"宁可全不可缺"原则一次定稿——按本注解标注的接口无需回头补字段;
|
|
13
|
+
* 中枢对不认识的字段一律忽略(前向兼容),可放心全量标注。
|
|
14
|
+
*
|
|
15
|
+
* 示例:
|
|
16
|
+
* #[AiTool(
|
|
17
|
+
* description: '查询门店员工列表',
|
|
18
|
+
* scope: 'tenant.staff.read',
|
|
19
|
+
* path: '/opentenantapi/staff/list',
|
|
20
|
+
* whenToUse: '用户问员工、排班、人事相关问题时',
|
|
21
|
+
* returns: '{code:1, data:[{id,name,role,dept}]}',
|
|
22
|
+
* examples: [['dept' => '前厅']],
|
|
23
|
+
* )]
|
|
24
|
+
* #[AiParam('dept', description: '按部门过滤,如 前厅')]
|
|
25
|
+
* public function list() { ... }
|
|
26
|
+
*/
|
|
27
|
+
#[Attribute(Attribute::TARGET_METHOD)]
|
|
28
|
+
final class AiTool
|
|
29
|
+
{
|
|
30
|
+
public function __construct(
|
|
31
|
+
/** 工具一句话说明(必填)——AI 判断何时调用的首要依据,务必写人话 */
|
|
32
|
+
public string $description,
|
|
33
|
+
/** 权限标识(必填),如 tenant.staff.read;中枢路由白名单按它放行(支持前缀通配) */
|
|
34
|
+
public string $scope,
|
|
35
|
+
/** 接口路径(必填),如 /opentenantapi/staff/list——中枢调用时拼在 base_url 后 */
|
|
36
|
+
public string $path,
|
|
37
|
+
/** HTTP 方法,默认 GET */
|
|
38
|
+
public string $method = 'GET',
|
|
39
|
+
/** 工具名(operationId),缺省 = 蛇形(类名去Controller)_蛇形(方法名),如 StaffController::list → staff_list。建议显式指定;定了就别改:改名会让 AI 认为这是另一个新工具 */
|
|
40
|
+
public ?string $name = null,
|
|
41
|
+
/** 风险级:low(默认,直接放行)/ medium(放行留痕)/ high(先进人工审批) */
|
|
42
|
+
public string $risk = 'low',
|
|
43
|
+
/** true = 不论风险级一律先人工审批(批准后任务自动重跑执行) */
|
|
44
|
+
public bool $confirm = false,
|
|
45
|
+
/** 参数级确认规则,如 [['param' => 'amount', 'op' => '>', 'value' => 500, 'label' => '大额退款需确认']] */
|
|
46
|
+
public array $confirmWhen = [],
|
|
47
|
+
/** 语义只读声明:GET 默认只读;POST 实现的查询接口务必标 true,AI 才敢放心调用 */
|
|
48
|
+
public ?bool $readonly = null,
|
|
49
|
+
/** true = 必须有操作主体(X-Bailing-On-Behalf-Of)才有意义;匿名任务(网页访客)直接看不到该工具 */
|
|
50
|
+
public bool $requiresSubject = false,
|
|
51
|
+
/** 可安全重试(GET 默认 true);网络抖动时大脑可凭此决定是否自动重发 */
|
|
52
|
+
public ?bool $idempotent = null,
|
|
53
|
+
/** true = 参数含敏感数据(手机号/身份证等):中枢审计只记参数键名不记值 */
|
|
54
|
+
public bool $sensitive = false,
|
|
55
|
+
/** 单工具限流,如 "30/min"、"600/hour",中枢侧执行 */
|
|
56
|
+
public ?string $rateLimit = null,
|
|
57
|
+
/** 慢接口(报表等)单独覆盖超时(1~600000 毫秒),缺省用工具源超时 */
|
|
58
|
+
public ?int $timeoutMs = null,
|
|
59
|
+
/** 何时该用/不该用的补充提示(拼进工具描述),如 "问工资别用本工具,走 salary_query" */
|
|
60
|
+
public ?string $whenToUse = null,
|
|
61
|
+
/** 返回结构的人话说明(拼进工具描述),如 "返回 {code, data: 员工数组}" */
|
|
62
|
+
public ?string $returns = null,
|
|
63
|
+
/** 示例参数数组(如 [['dept' => '前厅']]),首例拼进工具描述做示范 */
|
|
64
|
+
public array $examples = [],
|
|
65
|
+
/** 审批通知的人话模板,{参数名} 占位,如 "AI 申请删除员工 #{id}"——审批人不用读 JSON */
|
|
66
|
+
public ?string $confirmPrompt = null,
|
|
67
|
+
/** 业务自定义字符串数组,中枢原样透传——注册表没有的私有标记放这里(扩展阀门) */
|
|
68
|
+
public array $context = [],
|
|
69
|
+
/** 分组标签(控制台工具清单导航用,预留) */
|
|
70
|
+
public array $tags = [],
|
|
71
|
+
/** true = 弃用:不再暴露给 Agent;接口本身是否继续服务由业务侧决定 */
|
|
72
|
+
public bool $deprecated = false,
|
|
73
|
+
) {
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Bailing\Connect;
|
|
6
|
+
|
|
7
|
+
use RuntimeException;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 中枢主动 API 客户端:封装 POST /run、GET /jobs/{id}、POST /send。
|
|
11
|
+
*
|
|
12
|
+
* 这只是薄封装,稳定契约仍是 HTTP + Bearer token。适合业务后端在事件点触发任务、
|
|
13
|
+
* 查询结果,或经入站渠道主动给用户推消息。
|
|
14
|
+
*/
|
|
15
|
+
final class HubClient
|
|
16
|
+
{
|
|
17
|
+
private string $baseUrl;
|
|
18
|
+
private string $token;
|
|
19
|
+
private int $timeoutSeconds;
|
|
20
|
+
|
|
21
|
+
public function __construct(string $baseUrl, string $token, int $timeoutSeconds = 8)
|
|
22
|
+
{
|
|
23
|
+
if ($baseUrl === '') {
|
|
24
|
+
throw new RuntimeException('baseUrl 必填');
|
|
25
|
+
}
|
|
26
|
+
if ($token === '') {
|
|
27
|
+
throw new RuntimeException('token 必填');
|
|
28
|
+
}
|
|
29
|
+
$this->baseUrl = rtrim($baseUrl, '/');
|
|
30
|
+
$this->token = $token;
|
|
31
|
+
$this->timeoutSeconds = $timeoutSeconds;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @param array<string,mixed> $metadata @return array<string,mixed> */
|
|
35
|
+
public function run(string $requestId, string $route, string $input, array $metadata = [], ?string $callbackUrl = null, ?int $waitMs = null): array
|
|
36
|
+
{
|
|
37
|
+
$body = ['request_id' => $requestId, 'route' => $route, 'input' => $input, 'metadata' => $metadata];
|
|
38
|
+
if ($callbackUrl !== null && $callbackUrl !== '') {
|
|
39
|
+
$body['callback_url'] = $callbackUrl;
|
|
40
|
+
}
|
|
41
|
+
if ($waitMs !== null) {
|
|
42
|
+
$body['wait_ms'] = $waitMs;
|
|
43
|
+
}
|
|
44
|
+
return $this->post('/run', $body);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @return array<string,mixed> */
|
|
48
|
+
public function getJob(string $jobId): array
|
|
49
|
+
{
|
|
50
|
+
return $this->get('/jobs/' . rawurlencode($jobId));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param string|array<int,string> $to
|
|
55
|
+
* @param array<int,string> $images
|
|
56
|
+
* @param array<int,array<string,mixed>> $files
|
|
57
|
+
* @param array<string,mixed>|null $card
|
|
58
|
+
* @return array<string,mixed>
|
|
59
|
+
*/
|
|
60
|
+
public function send(string $requestId, string $channel, string|array $to, string $text, array $images = [], array $files = [], ?array $card = null): array
|
|
61
|
+
{
|
|
62
|
+
$body = ['request_id' => $requestId, 'channel' => $channel, 'to' => $to, 'text' => $text];
|
|
63
|
+
if ($images !== []) {
|
|
64
|
+
$body['images'] = $images;
|
|
65
|
+
}
|
|
66
|
+
if ($files !== []) {
|
|
67
|
+
$body['files'] = $files;
|
|
68
|
+
}
|
|
69
|
+
if ($card !== null) {
|
|
70
|
+
$body['card'] = $card;
|
|
71
|
+
}
|
|
72
|
+
return $this->post('/send', $body);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @return array<string,mixed> */
|
|
76
|
+
public function get(string $path): array
|
|
77
|
+
{
|
|
78
|
+
return $this->request('GET', $path);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @param array<string,mixed> $body @return array<string,mixed> */
|
|
82
|
+
public function post(string $path, array $body): array
|
|
83
|
+
{
|
|
84
|
+
return $this->request('POST', $path, $body);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @param array<string,mixed>|null $body @return array<string,mixed> */
|
|
88
|
+
public function request(string $method, string $path, ?array $body = null): array
|
|
89
|
+
{
|
|
90
|
+
$headers = ['Authorization: Bearer ' . $this->token];
|
|
91
|
+
$content = '';
|
|
92
|
+
if ($body !== null) {
|
|
93
|
+
$content = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}';
|
|
94
|
+
$headers[] = 'Content-Type: application/json';
|
|
95
|
+
}
|
|
96
|
+
$http = [
|
|
97
|
+
'method' => strtoupper($method),
|
|
98
|
+
'header' => implode("\r\n", $headers),
|
|
99
|
+
'timeout' => $this->timeoutSeconds,
|
|
100
|
+
'ignore_errors' => true,
|
|
101
|
+
];
|
|
102
|
+
if ($body !== null) {
|
|
103
|
+
$http['content'] = $content;
|
|
104
|
+
}
|
|
105
|
+
$raw = file_get_contents($this->baseUrl . $path, false, stream_context_create(['http' => $http]));
|
|
106
|
+
$status = 0;
|
|
107
|
+
foreach ($http_response_header ?? [] as $line) {
|
|
108
|
+
if (preg_match('/^HTTP\/\S+\s+(\d+)/', $line, $m)) {
|
|
109
|
+
$status = (int) $m[1];
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
$data = json_decode($raw !== false ? $raw : '{}', true);
|
|
114
|
+
if (!is_array($data)) {
|
|
115
|
+
$data = ['raw' => $raw];
|
|
116
|
+
}
|
|
117
|
+
if ($status < 200 || $status >= 300) {
|
|
118
|
+
throw new RuntimeException((string) ($data['error'] ?? $data['message'] ?? ('HTTP ' . $status)));
|
|
119
|
+
}
|
|
120
|
+
return $data;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
namespace Bailing\Connect;
|
|
6
|
+
|
|
7
|
+
use Bailing\Connect\Attributes\AiParam;
|
|
8
|
+
use Bailing\Connect\Attributes\AiTool;
|
|
9
|
+
use InvalidArgumentException;
|
|
10
|
+
use ReflectionClass;
|
|
11
|
+
use ReflectionMethod;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 反射扫描 #[AiTool] 注解 → 构建带 x-agent-capability 的 OpenAPI 3.0 spec。
|
|
15
|
+
*
|
|
16
|
+
* 设计原则:**会被中枢跳过/误用的问题在构建期报错**,而不是发布后才在控制台
|
|
17
|
+
* 「工具清单·被跳过」里发现——CI 跑构建即完成体检。
|
|
18
|
+
*
|
|
19
|
+
* 用法:
|
|
20
|
+
* $spec = (new SpecBuilder(title: '某某业务系统'))
|
|
21
|
+
* ->addClass(StaffController::class)
|
|
22
|
+
* ->addClass(OrderController::class)
|
|
23
|
+
* ->build();
|
|
24
|
+
*/
|
|
25
|
+
final class SpecBuilder
|
|
26
|
+
{
|
|
27
|
+
/** @var array<class-string> */
|
|
28
|
+
private array $classes = [];
|
|
29
|
+
|
|
30
|
+
/** @var string[] 构建期警告(不阻塞构建;CI 里打到 stderr 提醒整改) */
|
|
31
|
+
private array $warnings = [];
|
|
32
|
+
|
|
33
|
+
/** @var array{method:string,path:string}|null */
|
|
34
|
+
private ?array $authzProbe = null;
|
|
35
|
+
|
|
36
|
+
public function __construct(
|
|
37
|
+
private readonly string $title = '业务系统',
|
|
38
|
+
private readonly string $version = '1.0.0',
|
|
39
|
+
) {
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @return string[] 最近一次 build() 产生的警告 */
|
|
43
|
+
public function warnings(): array
|
|
44
|
+
{
|
|
45
|
+
return $this->warnings;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 登记一个含 #[AiTool] 方法的类(通常是控制器)。 */
|
|
49
|
+
public function addClass(string $class): static
|
|
50
|
+
{
|
|
51
|
+
if (!class_exists($class)) {
|
|
52
|
+
throw new InvalidArgumentException("类不存在:{$class}");
|
|
53
|
+
}
|
|
54
|
+
$this->classes[] = $class;
|
|
55
|
+
return $this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 声明独立授权探针端点。中枢刷新工具源时会用不存在的主体探测业务侧授权闸是否 fail-closed。
|
|
60
|
+
*/
|
|
61
|
+
public function authzProbe(string $path = '/.well-known/bailing/authz-probe', string $method = 'POST'): static
|
|
62
|
+
{
|
|
63
|
+
$method = strtoupper(trim($method));
|
|
64
|
+
if (!in_array($method, ['GET', 'POST'], true)) {
|
|
65
|
+
throw new InvalidArgumentException("authzProbe method 仅支持 GET/POST,收到 {$method}");
|
|
66
|
+
}
|
|
67
|
+
if (!str_starts_with($path, '/')) {
|
|
68
|
+
throw new InvalidArgumentException("authzProbe path 必须以 / 开头,收到 {$path}");
|
|
69
|
+
}
|
|
70
|
+
$this->authzProbe = ['method' => $method, 'path' => $path];
|
|
71
|
+
return $this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @return array<string, mixed> openapi 数组 */
|
|
75
|
+
public function build(): array
|
|
76
|
+
{
|
|
77
|
+
$this->warnings = [];
|
|
78
|
+
$paths = [];
|
|
79
|
+
$seenNames = []; // operationId 去重
|
|
80
|
+
$seenRoutes = []; // method+path 去重
|
|
81
|
+
|
|
82
|
+
foreach ($this->classes as $class) {
|
|
83
|
+
$ref = new ReflectionClass($class);
|
|
84
|
+
foreach ($ref->getMethods(ReflectionMethod::IS_PUBLIC) as $m) {
|
|
85
|
+
$toolAttrs = $m->getAttributes(AiTool::class);
|
|
86
|
+
if (!$toolAttrs) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
/** @var AiTool $t */
|
|
90
|
+
$t = $toolAttrs[0]->newInstance();
|
|
91
|
+
$where = "{$class}::{$m->getName()}()";
|
|
92
|
+
|
|
93
|
+
// ---- 构建期体检(中枢派生规则的前移)----
|
|
94
|
+
$method = strtoupper(trim($t->method));
|
|
95
|
+
if (!in_array($method, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], true)) {
|
|
96
|
+
throw new InvalidArgumentException("{$where}: method 仅支持 GET/POST/PUT/PATCH/DELETE,收到 {$t->method}");
|
|
97
|
+
}
|
|
98
|
+
if (!str_starts_with($t->path, '/')) {
|
|
99
|
+
throw new InvalidArgumentException("{$where}: path 必须以 / 开头,收到 {$t->path}");
|
|
100
|
+
}
|
|
101
|
+
if (trim($t->scope) === '') {
|
|
102
|
+
throw new InvalidArgumentException("{$where}: scope 不能为空(中枢会直接跳过该接口)");
|
|
103
|
+
}
|
|
104
|
+
if (!in_array($t->risk, ['low', 'medium', 'high'], true)) {
|
|
105
|
+
throw new InvalidArgumentException("{$where}: risk 仅支持 low/medium/high,收到 {$t->risk}");
|
|
106
|
+
}
|
|
107
|
+
if ($t->rateLimit !== null && self::parseRateLimit($t->rateLimit) === null) {
|
|
108
|
+
throw new InvalidArgumentException("{$where}: rateLimit 格式应为 \"30/min\"、\"600/hour\"、\"10/s\" 或 \"1000/day\",收到 {$t->rateLimit}");
|
|
109
|
+
}
|
|
110
|
+
if ($t->timeoutMs !== null && ($t->timeoutMs < 1 || $t->timeoutMs > 600000)) {
|
|
111
|
+
throw new InvalidArgumentException("{$where}: timeoutMs 取值 1~600000,收到 {$t->timeoutMs}");
|
|
112
|
+
}
|
|
113
|
+
// 弱校验:工具多时中枢走渐进披露,目录阶段 AI 只看到 description 第一句——首句必须自含语义
|
|
114
|
+
$firstSentence = explode('。', $t->description)[0];
|
|
115
|
+
if (mb_strlen(trim($firstSentence)) < 6) {
|
|
116
|
+
$this->warnings[] = "{$where}: description 首句过短(\"{$firstSentence}\")——工具目录阶段 AI 只看到这一句,请写自含语义的完整说明(如\"查询门店员工列表\")";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 默认工具名 = 蛇形(类名去 Controller) + 蛇形(方法名):StaffController::list → staff_list。
|
|
120
|
+
// 裸方法名(list/delete)对 AI 是无上下文的烂名字;显式 name 仍是最佳实践(定了就别改)。
|
|
121
|
+
$name = $t->name ?? self::snake(preg_replace('/Controller$/', '', $ref->getShortName()) ?? '') . '_' . self::snake($m->getName());
|
|
122
|
+
if (isset($seenNames[$name])) {
|
|
123
|
+
throw new InvalidArgumentException("{$where}: 工具名 {$name} 与 {$seenNames[$name]} 重复(operationId 必须唯一)");
|
|
124
|
+
}
|
|
125
|
+
$seenNames[$name] = $where;
|
|
126
|
+
$routeKey = "{$method} {$t->path}";
|
|
127
|
+
if (isset($seenRoutes[$routeKey])) {
|
|
128
|
+
throw new InvalidArgumentException("{$where}: {$routeKey} 与 {$seenRoutes[$routeKey]} 重复");
|
|
129
|
+
}
|
|
130
|
+
$seenRoutes[$routeKey] = $where;
|
|
131
|
+
|
|
132
|
+
// ---- 参数 ----
|
|
133
|
+
$params = array_map(
|
|
134
|
+
static fn ($a) => $a->newInstance(),
|
|
135
|
+
$m->getAttributes(AiParam::class),
|
|
136
|
+
);
|
|
137
|
+
if ($method !== 'GET' && !$params && !$t->deprecated) {
|
|
138
|
+
throw new InvalidArgumentException(
|
|
139
|
+
"{$where}: 写接口({$method})必须用 #[AiParam] 声明至少一个参数——中枢不暴露无参数 schema 的写接口(防 AI 瞎猜参数)"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
$op = $this->buildOperation($t, $name, $method, $params, $where);
|
|
144
|
+
$paths[$t->path][strtolower($method)] = $op;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!$paths) {
|
|
149
|
+
throw new InvalidArgumentException('没有发现任何 #[AiTool] 注解:请确认 addClass 的类里有公开方法标注了 AiTool');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
$spec = [
|
|
153
|
+
'openapi' => '3.0.0',
|
|
154
|
+
'info' => ['title' => $this->title, 'version' => $this->version],
|
|
155
|
+
'paths' => $paths,
|
|
156
|
+
];
|
|
157
|
+
if ($this->authzProbe !== null) {
|
|
158
|
+
$spec['x-bailing-authz-probe'] = $this->authzProbe;
|
|
159
|
+
}
|
|
160
|
+
return $spec;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** build() 的 JSON 形态(发布到 /.well-known/bailing/tools.json 的就是它)。 */
|
|
164
|
+
public function buildJson(bool $pretty = true): string
|
|
165
|
+
{
|
|
166
|
+
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ($pretty ? JSON_PRETTY_PRINT : 0);
|
|
167
|
+
return json_encode($this->build(), $flags) ?: '{}';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 驼峰/帕斯卡 → 蛇形:DemoStaff → demo_staff、memberQuery → member_query */
|
|
171
|
+
private static function snake(string $s): string
|
|
172
|
+
{
|
|
173
|
+
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $s) ?? $s);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** @return array{count:int,window:string}|null */
|
|
177
|
+
private static function parseRateLimit(?string $value): ?array
|
|
178
|
+
{
|
|
179
|
+
if ($value === null) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
$text = strtolower(preg_replace('/\s+/', '', $value) ?? '');
|
|
183
|
+
if (!preg_match('#^(\d+)/(s|sec|second|min|minute|h|hour|d|day)$#', $text, $m)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
$unit = $m[2];
|
|
187
|
+
$window = match ($unit) {
|
|
188
|
+
's', 'sec', 'second' => '1s',
|
|
189
|
+
'h', 'hour' => '1h',
|
|
190
|
+
'd', 'day' => '1d',
|
|
191
|
+
default => '1m',
|
|
192
|
+
};
|
|
193
|
+
return ['count' => (int) $m[1], 'window' => $window];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** @return array<string,mixed> */
|
|
197
|
+
private function buildCapability(AiTool $t, string $method): array
|
|
198
|
+
{
|
|
199
|
+
$capability = ['version' => 1, 'enabled' => true, 'scope' => $t->scope];
|
|
200
|
+
if ($t->risk !== 'low') {
|
|
201
|
+
$capability['risk'] = ['level' => $t->risk];
|
|
202
|
+
}
|
|
203
|
+
if ($t->confirm || $t->confirmWhen || $t->confirmPrompt !== null) {
|
|
204
|
+
$approval = [];
|
|
205
|
+
if ($t->confirm) {
|
|
206
|
+
$approval['required'] = true;
|
|
207
|
+
}
|
|
208
|
+
if ($t->confirmWhen) {
|
|
209
|
+
$approval['when'] = $t->confirmWhen;
|
|
210
|
+
}
|
|
211
|
+
if ($t->confirmPrompt !== null) {
|
|
212
|
+
$approval['prompt'] = $t->confirmPrompt;
|
|
213
|
+
}
|
|
214
|
+
$capability['approval'] = $approval;
|
|
215
|
+
}
|
|
216
|
+
if ($t->requiresSubject) {
|
|
217
|
+
$capability['subject'] = ['required' => true];
|
|
218
|
+
}
|
|
219
|
+
$execution = [];
|
|
220
|
+
if (($t->readonly ?? ($method === 'GET')) && $method !== 'GET') {
|
|
221
|
+
$execution['readonly'] = true;
|
|
222
|
+
}
|
|
223
|
+
if (($t->idempotent ?? ($method === 'GET')) && $method !== 'GET') {
|
|
224
|
+
$execution['idempotent'] = true;
|
|
225
|
+
}
|
|
226
|
+
$rateLimit = self::parseRateLimit($t->rateLimit);
|
|
227
|
+
if ($rateLimit !== null) {
|
|
228
|
+
$execution['rate_limit'] = $rateLimit;
|
|
229
|
+
}
|
|
230
|
+
if ($t->timeoutMs !== null) {
|
|
231
|
+
$execution['timeout_ms'] = $t->timeoutMs;
|
|
232
|
+
}
|
|
233
|
+
if ($execution) {
|
|
234
|
+
$capability['execution'] = $execution;
|
|
235
|
+
}
|
|
236
|
+
if ($t->sensitive) {
|
|
237
|
+
$capability['audit'] = ['sensitive' => true];
|
|
238
|
+
}
|
|
239
|
+
$guidance = [];
|
|
240
|
+
if ($t->whenToUse !== null) {
|
|
241
|
+
$guidance['when_to_use'] = $t->whenToUse;
|
|
242
|
+
}
|
|
243
|
+
if ($t->returns !== null) {
|
|
244
|
+
$guidance['returns'] = $t->returns;
|
|
245
|
+
}
|
|
246
|
+
if ($t->examples) {
|
|
247
|
+
$guidance['examples'] = $t->examples;
|
|
248
|
+
}
|
|
249
|
+
if ($t->context) {
|
|
250
|
+
$guidance['context'] = array_values(array_map('strval', $t->context));
|
|
251
|
+
}
|
|
252
|
+
if ($guidance) {
|
|
253
|
+
$capability['guidance'] = $guidance;
|
|
254
|
+
}
|
|
255
|
+
return $capability;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** @param AiParam[] $params */
|
|
259
|
+
private function buildOperation(AiTool $t, string $name, string $method, array $params, string $where): array
|
|
260
|
+
{
|
|
261
|
+
$op = [
|
|
262
|
+
'operationId' => $name,
|
|
263
|
+
'summary' => $t->description,
|
|
264
|
+
'x-agent-capability' => $this->buildCapability($t, $method),
|
|
265
|
+
];
|
|
266
|
+
// 非核心 OpenAPI 字段才继续落到 operation 顶层;治理字段统一放入 ACC。
|
|
267
|
+
if ($t->tags) {
|
|
268
|
+
$op['tags'] = array_values(array_map('strval', $t->tags));
|
|
269
|
+
}
|
|
270
|
+
if ($t->deprecated) {
|
|
271
|
+
$op['deprecated'] = true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// 参数分流:query 进 parameters、body 聚合成 requestBody schema
|
|
275
|
+
$queryParams = [];
|
|
276
|
+
$bodyProps = [];
|
|
277
|
+
$bodyRequired = [];
|
|
278
|
+
foreach ($params as $p) {
|
|
279
|
+
if (!in_array($p->type, ['string', 'integer', 'number', 'boolean', 'array'], true)) {
|
|
280
|
+
throw new InvalidArgumentException("{$where}: 参数 {$p->name} type 仅支持 string/integer/number/boolean/array,收到 {$p->type}");
|
|
281
|
+
}
|
|
282
|
+
$schema = ['type' => $p->type];
|
|
283
|
+
if ($p->description !== '') {
|
|
284
|
+
$schema['description'] = $p->description;
|
|
285
|
+
}
|
|
286
|
+
if ($p->enum !== null) {
|
|
287
|
+
$schema['enum'] = array_values($p->enum);
|
|
288
|
+
}
|
|
289
|
+
if ($p->default !== null) {
|
|
290
|
+
$schema['default'] = $p->default;
|
|
291
|
+
}
|
|
292
|
+
if ($p->format !== null) {
|
|
293
|
+
$schema['format'] = $p->format;
|
|
294
|
+
}
|
|
295
|
+
if ($p->type === 'array') {
|
|
296
|
+
$schema['items'] = ['type' => $p->itemsType ?? 'string'];
|
|
297
|
+
}
|
|
298
|
+
$in = $p->in ?? ($method === 'GET' ? 'query' : 'body');
|
|
299
|
+
if (!in_array($in, ['query', 'body'], true)) {
|
|
300
|
+
throw new InvalidArgumentException("{$where}: 参数 {$p->name} in 仅支持 query/body,收到 {$in}");
|
|
301
|
+
}
|
|
302
|
+
if ($in === 'query') {
|
|
303
|
+
$queryParams[] = [
|
|
304
|
+
'name' => $p->name,
|
|
305
|
+
'in' => 'query',
|
|
306
|
+
'required' => $p->required,
|
|
307
|
+
'schema' => $schema,
|
|
308
|
+
];
|
|
309
|
+
} else {
|
|
310
|
+
$bodyProps[$p->name] = $schema;
|
|
311
|
+
if ($p->required) {
|
|
312
|
+
$bodyRequired[] = $p->name;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if ($queryParams) {
|
|
317
|
+
$op['parameters'] = $queryParams;
|
|
318
|
+
}
|
|
319
|
+
if ($bodyProps) {
|
|
320
|
+
$op['requestBody'] = ['content' => ['application/json' => ['schema' => [
|
|
321
|
+
'type' => 'object',
|
|
322
|
+
'properties' => $bodyProps,
|
|
323
|
+
...($bodyRequired ? ['required' => $bodyRequired] : []),
|
|
324
|
+
]]]];
|
|
325
|
+
}
|
|
326
|
+
return $op;
|
|
327
|
+
}
|
|
328
|
+
}
|