bailinghub 0.3.4 → 0.5.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.
Files changed (169) hide show
  1. package/.gitignore +5 -0
  2. package/README.en.md +6 -1
  3. package/README.md +6 -0
  4. package/SECURITY.md +11 -0
  5. package/docker-compose.images.yml +2 -2
  6. package/docs/AGENT_AUTH_API.en.md +175 -0
  7. package/docs/AGENT_AUTH_API.md +190 -0
  8. package/docs/AGENT_CLIENT_QUICKSTART.en.md +212 -0
  9. package/docs/AGENT_CLIENT_QUICKSTART.md +206 -0
  10. package/docs/AGENT_CLIENT_RUNTIME_API.en.md +157 -0
  11. package/docs/AGENT_CLIENT_RUNTIME_API.md +107 -0
  12. package/docs/CHANGELOG.en.md +59 -1
  13. package/docs/CHANGELOG.md +27 -1
  14. package/docs/COMPATIBILITY.en.md +25 -0
  15. package/docs/CONTRACT.en.md +79 -8
  16. package/docs/CONTRACT.md +36 -6
  17. package/docs/INDEPENDENT_VALIDATION.en.md +4 -4
  18. package/docs/INDEPENDENT_VALIDATION.md +4 -4
  19. package/docs/QUICKSTART.en.md +6 -0
  20. package/docs/QUICKSTART.md +1 -1
  21. package/docs/README.en.md +8 -0
  22. package/docs/README.md +8 -0
  23. package/docs/RELEASE_NOTES_v0.4.0.en.md +39 -0
  24. package/docs/RELEASE_NOTES_v0.4.0.md +39 -0
  25. package/docs/RELEASE_NOTES_v0.5.0.en.md +41 -0
  26. package/docs/RELEASE_NOTES_v0.5.0.md +41 -0
  27. package/docs/STREAMING.en.md +32 -0
  28. package/docs/STREAMING.md +8 -2
  29. package/docs/WIDGET_RENDERERS.en.md +123 -6
  30. package/docs/WIDGET_RENDERERS.md +75 -6
  31. package/docs/user-guide/console-map.en.md +1 -2
  32. package/docs/user-guide/console-map.md +2 -2
  33. package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +9 -0
  34. package/package.json +3 -2
  35. package/schemas/config/chat-entry.schema.json +2 -1
  36. package/schemas/config/client.schema.json +7 -0
  37. package/schemas/config/common.schema.json +30 -0
  38. package/schemas/config/route.schema.json +11 -0
  39. package/scripts/check-doc-links.mjs +1 -0
  40. package/scripts/check-image-tags.sh +1 -1
  41. package/scripts/check-release-consistency.test.mjs +5 -5
  42. package/scripts/e2e-console.mjs +144 -0
  43. package/scripts/export-oss.mjs +5 -12
  44. package/scripts/install.sh +1 -1
  45. package/scripts/public-content-denylist.mjs +82 -0
  46. package/scripts/public-content-denylist.test.mjs +53 -0
  47. package/scripts/publish-images.sh +1 -1
  48. package/scripts/release-audit.mjs +8 -12
  49. package/scripts/sdk-runtime-test.mjs +171 -7
  50. package/scripts/security-scan.mjs +8 -5
  51. package/scripts/validate-examples.mjs +3 -1
  52. package/scripts/verify-github-repo.mjs +7 -11
  53. package/scripts/verify-oss-export.mjs +6 -12
  54. package/scripts/widget-renderer-test.mjs +244 -16
  55. package/sdk/php/README.md +27 -1
  56. package/sdk/php/src/AgentAuth.php +78 -0
  57. package/sdk/php/src/HubClient.php +3 -0
  58. package/sdk/php7/README.md +32 -2
  59. package/sdk/php7/src/AgentAuth.php +77 -0
  60. package/sdk/php7/src/HubClient.php +3 -0
  61. package/sql/055_agent_auth.sql +72 -0
  62. package/sql/056_agent_client_runtime.sql +60 -0
  63. package/src/adapters/channels/wecom-api.ts +1 -1
  64. package/src/app/agent-client-runtime.test.ts +221 -0
  65. package/src/app/agent-client-runtime.ts +408 -0
  66. package/src/app/agent-tool-invocations.test.ts +462 -0
  67. package/src/app/agent-tool-invocations.ts +795 -0
  68. package/src/app/agent-tool-job.test.ts +62 -0
  69. package/src/app/agent-tool-job.ts +41 -0
  70. package/src/app/auth.ts +2 -1
  71. package/src/app/kernel.ts +2 -0
  72. package/src/app/runtime-context.ts +8 -0
  73. package/src/app/tool-approvals.ts +5 -1
  74. package/src/app/tool-assembly.ts +19 -1
  75. package/src/app/tool-context.ts +2 -1
  76. package/src/core/config/config-codec.ts +2 -1
  77. package/src/core/config/config-models.test.ts +28 -1
  78. package/src/core/config/config-models.ts +46 -3
  79. package/src/core/config/config-schema.test.ts +8 -0
  80. package/src/core/config/route-config.test.ts +15 -0
  81. package/src/core/config/route-config.ts +36 -0
  82. package/src/core/config/tools-config.test.ts +58 -1
  83. package/src/core/config/tools-config.ts +84 -0
  84. package/src/core/contracts/tools.ts +162 -29
  85. package/src/core/contracts/types.ts +38 -1
  86. package/src/core/platform/chat-form.test.ts +147 -0
  87. package/src/core/platform/chat-form.ts +402 -0
  88. package/src/core/platform/version.ts +1 -1
  89. package/src/core/runtime/agent-client-trace.test.ts +154 -0
  90. package/src/core/runtime/agent-client-trace.ts +288 -0
  91. package/src/core/runtime/execution-runtime.test.ts +61 -0
  92. package/src/core/runtime/execution-runtime.ts +17 -0
  93. package/src/core/runtime/launch-runtime.ts +13 -0
  94. package/src/core/runtime/trace-runtime.test.ts +18 -1
  95. package/src/core/runtime/trace-runtime.ts +28 -3
  96. package/src/core/state/state-codec.ts +2 -0
  97. package/src/infrastructure/config/config-agent-auth-repository.test.ts +147 -0
  98. package/src/infrastructure/config/config-agent-auth-repository.ts +356 -0
  99. package/src/infrastructure/config/config-agent-client-runtime-repository.ts +253 -0
  100. package/src/infrastructure/config/config-approval-ledger.test.ts +32 -0
  101. package/src/infrastructure/config/config-approval-ledger.ts +16 -0
  102. package/src/infrastructure/config/config-client-repository.ts +3 -3
  103. package/src/infrastructure/config/config-conversation-ledger.test.ts +69 -0
  104. package/src/infrastructure/config/config-conversation-ledger.ts +8 -7
  105. package/src/infrastructure/config/config-observability-ledger.test.ts +50 -0
  106. package/src/infrastructure/config/config-observability-ledger.ts +65 -10
  107. package/src/infrastructure/config/config-route-repository.ts +4 -3
  108. package/src/infrastructure/config/configstore.ts +14 -3
  109. package/src/infrastructure/state/state-mysql.ts +2 -2
  110. package/src/kernel-api/v1/contracts.ts +8 -0
  111. package/src/routes/admin-runtime.test.ts +214 -0
  112. package/src/routes/admin-runtime.ts +82 -2
  113. package/src/routes/agent-api.test.ts +379 -0
  114. package/src/routes/agent-api.ts +251 -0
  115. package/src/routes/agent-auth.test.ts +276 -0
  116. package/src/routes/agent-auth.ts +395 -0
  117. package/src/routes/approvals.ts +4 -1
  118. package/src/routes/chat.test.ts +319 -7
  119. package/src/routes/chat.ts +166 -21
  120. package/src/routes/critical-routes.test.ts +235 -1
  121. package/src/routes/private-default.ts +2 -1
  122. package/src/routes/private.ts +16 -1
  123. package/src/routes/run.ts +79 -13
  124. package/web/connect/bailing-connect-php.tgz +0 -0
  125. package/web/connect/bailing-connect-php7.tgz +0 -0
  126. package/web/console/assets/{Accounts-CAwrtBiS.js → Accounts-D4RRm5D1.js} +1 -1
  127. package/web/console/assets/{Approvals-C2BZ86T3.js → Approvals-CsTTKb8o.js} +1 -1
  128. package/web/console/assets/{Audit-B-E4Y_Up.js → Audit-BzvZ7JZw.js} +1 -1
  129. package/web/console/assets/{BrandLockup-C18u8Lx5.js → BrandLockup-CYcA93wh.js} +1 -1
  130. package/web/console/assets/{Channels-DTy8ylH-.js → Channels-DRQZggqM.js} +1 -1
  131. package/web/console/assets/ChatEntries-BUB7BWKU.js +7 -0
  132. package/web/console/assets/ChatEntries-DsYtcXN6.css +1 -0
  133. package/web/console/assets/Clients-CQ_MonGL.css +1 -0
  134. package/web/console/assets/Clients-HhCyrxfz.js +3 -0
  135. package/web/console/assets/{Cost-2SSYs1AT.js → Cost-BZgFf1yM.js} +1 -1
  136. package/web/console/assets/{Credentials-B79LtxCE.js → Credentials-BTe2Q07j.js} +1 -1
  137. package/web/console/assets/{Diagnostics-BakcRYvb.js → Diagnostics-WL-buYli.js} +1 -1
  138. package/web/console/assets/{Executors-BXoyo28u.js → Executors-CGZ-7Xmp.js} +1 -1
  139. package/web/console/assets/{Kb-ISbzquab.js → Kb-VVw8oGsT.js} +1 -1
  140. package/web/console/assets/{Login-Dlu6Wgcf.js → Login-rwvmBBPl.js} +1 -1
  141. package/web/console/assets/{Projects-DFzmtnqg.js → Projects-CexaCEY1.js} +1 -1
  142. package/web/console/assets/Routes-B9ZHbQAp.js +124 -0
  143. package/web/console/assets/Routes-BbkFpi6Y.css +1 -0
  144. package/web/console/assets/Runs-4zDD9VOC.css +1 -0
  145. package/web/console/assets/Runs-BzFNPTwD.js +3 -0
  146. package/web/console/assets/{Setup-CrBAV-b9.js → Setup-Djh5yadQ.js} +1 -1
  147. package/web/console/assets/{Shell-B1a8DvXQ.js → Shell-kn763smL.js} +1 -1
  148. package/web/console/assets/{StorageBuckets-BRfL_eHb.js → StorageBuckets-CQBR41W1.js} +1 -1
  149. package/web/console/assets/{SystemSettings-CyirVLbq.js → SystemSettings-DgnopekD.js} +1 -1
  150. package/web/console/assets/{SystemStatus-DZ_OR7sk.js → SystemStatus-Bj8-kPt2.js} +1 -1
  151. package/web/console/assets/{Targets-CtIMa66c.js → Targets-BAfCt_nH.js} +1 -1
  152. package/web/console/assets/{Tools-BfDZHt01.js → Tools-xjd0NZdE.js} +1 -1
  153. package/web/console/assets/{index-DBiByFiO.js → index-DfJA7iJS.js} +2 -2
  154. package/web/console/assets/{request-DY2cxQqZ.js → request-DkNPOweH.js} +1 -1
  155. package/web/console/assets/{schema-6mCQ7rR5.js → schema-CgPWAu1_.js} +1 -1
  156. package/web/console/index.html +1 -1
  157. package/web/widget/widget.js +493 -14
  158. package/web-admin/src/pages/ChatEntries.vue +18 -6
  159. package/web-admin/src/pages/Clients.vue +11 -3
  160. package/web-admin/src/pages/Routes.vue +282 -16
  161. package/web-admin/src/pages/Runs.vue +159 -38
  162. package/web/console/assets/ChatEntries-9xbRiQov.css +0 -1
  163. package/web/console/assets/ChatEntries-DvD-4IeE.js +0 -7
  164. package/web/console/assets/Clients--D7NQJKk.css +0 -1
  165. package/web/console/assets/Clients-DWs9JFr0.js +0 -3
  166. package/web/console/assets/Routes-B-hm9HGY.js +0 -124
  167. package/web/console/assets/Routes-DWGQN5Pv.css +0 -1
  168. package/web/console/assets/Runs-CUtSsPW-.css +0 -1
  169. package/web/console/assets/Runs-hMRu1im3.js +0 -3
@@ -0,0 +1,212 @@
1
+ # Agent Client v1 Integration Guide
2
+
3
+ English | [简体中文](AGENT_CLIENT_QUICKSTART.md)
4
+
5
+ Agent Client lets a local Agent host such as DeepSeek Harness perform reasoning, planning, and
6
+ multi-step tool selection while BailingHub continues to own trusted business identity,
7
+ capability filtering, approvals, idempotency, business invocation, and audit. It is not the
8
+ existing executor protocol, and it does not give local models business passwords, tokens, or
9
+ direct business API endpoints.
10
+
11
+ This guide separates the responsibilities of the BailingHub deployer, the business-system
12
+ developer, and the local Agent user. They do not share one credential or configuration file.
13
+
14
+ ## 1. Component relationship
15
+
16
+ ```text
17
+ Local Agent host (for example, DSH)
18
+ └─ dsh-bailinghub: host lifecycle, dynamic tools, and visible results
19
+ └─ bailinghub-mcp-server/sdk: PKCE, browser login, credential storage, Runtime DTOs
20
+ └─ BailingHub Core: identity revalidation, capability filtering, approval,
21
+ execution, audit, and the visible conversation ledger
22
+ ├─ business authorization page: binds the current business login
23
+ └─ Tool Provider: ACC declarations, signature verification, final authorization
24
+ ```
25
+
26
+ The dependency direction is host to generic SDK to BailingHub Core. The business system never
27
+ sends its login cookie, password, or API secret to DSH, and DSH never calls business APIs directly.
28
+
29
+ ## 2. Configuration ownership
30
+
31
+ | Configuration | Owner | Storage | Secret |
32
+ |---|---|---|---|
33
+ | `hubUrl` | host deployer | host plugin settings | No; public BailingHub HTTPS origin |
34
+ | `clientAppId` | host deployer | host plugin settings | No; BailingHub client `app_id` |
35
+ | `workspace` | host deployer | host plugin settings | No; an allowed BailingHub `route_key` |
36
+ | `connectionName` | local user | local SDK registry | No; local readable alias only |
37
+ | Agent authorization URL | business developer / Hub admin | BailingHub client config | No, but users do not enter it |
38
+ | business Client Token | business backend | server-side secret store | **Yes; never browser or DSH config** |
39
+ | Tool Provider Secret | business backend and BailingHub | both secret stores | **Yes; never DSH** |
40
+ | Agent access/refresh token | generic SDK | Keychain or explicit secure store | **Yes; never plugin settings** |
41
+ | model API key | local Agent host | DSH model-provider credential store | **Yes; independent of Hub auth** |
42
+ | BailingHub admin token | Hub operator | Hub server secret store | **Yes; never any Agent client** |
43
+
44
+ `clientAppId` is a public identifier, not a Client Token. A native Agent Client setup must not ask
45
+ the end user to paste a BailingHub Client Token, Tool Provider Secret, or business password.
46
+
47
+ ## 3. BailingHub deployer: expose one Agent Client workspace
48
+
49
+ ### 3.1 Deploy Core and apply migrations
50
+
51
+ Use a BailingHub release that contains Agent Auth v1, Agent Client Runtime v1, and migrations 055
52
+ and 056. Confirm that health and readiness pass, no migration is pending, the console can edit
53
+ Agent Client settings, and production traffic uses HTTPS.
54
+
55
+ Never copy databases, tokens, business domains, or route configuration from a maintainer's
56
+ self-use instance into a public deployment.
57
+
58
+ ### 3.2 Connect business tools
59
+
60
+ The business system still publishes ACC/OpenAPI capability declarations and performs signature
61
+ verification plus final authorization at the real endpoint. Use `bailing/connect` or implement
62
+ the public HTTP/HMAC contract:
63
+
64
+ - [Business integration guide](INTEGRATION.en.md)
65
+ - [Tool governance design](TOOLS_DESIGN.en.md)
66
+ - [PHP SDK](../sdk/php/README.md)
67
+
68
+ Register the business `base_url`, `spec_url`, and a Hub-side credential reference in the Tool
69
+ Provider. The Tool Provider Secret must never enter a route, knowledge base, prompt, or Agent
70
+ Client setting.
71
+
72
+ ### 3.3 Configure a route
73
+
74
+ 1. Set `tools.agent_direct.enabled=true`.
75
+ 2. Add only explicitly allowed write operation IDs to `write_tools`; read-only tools do not need
76
+ to be listed there.
77
+ 3. Inherit approval requirements from the ACC declaration. Use `force_approval_tools` only to
78
+ make selected writes stricter.
79
+ 4. Enable Local Agent Runtime and optionally set host-specific instructions and an active tool
80
+ limit from 1 to 12 (default 8).
81
+ 5. Configure audience, tenant, and role boundaries instead of using `*` as production design.
82
+
83
+ The Agent Client never receives model credentials, Tool Provider Secrets, business API URLs, or
84
+ the complete `target_config`.
85
+
86
+ ### 3.4 Create a client application
87
+
88
+ In Clients, create one Agent Client application:
89
+
90
+ - a stable public `app_id`, for example `merchant-agent`;
91
+ - the minimum `allowed_routes` set;
92
+ - the HTTPS business authorization page, for example
93
+ `https://business.example.com/agent/authorize`;
94
+ - a reasonable rate limit and `enabled=true`.
95
+
96
+ BailingHub displays the Client Token once. Give it only to the business backend that implements
97
+ the authorization page. Never put it in DSH settings, browser JavaScript, a URL, documentation,
98
+ or screenshots.
99
+
100
+ ## 4. Business developer: bind trusted identity
101
+
102
+ The BailingHub SDK provides server-side Agent Auth methods; it does not inject one universal UI
103
+ into every business system. The business system owns a page that follows its normal login and
104
+ permission model. The backend derives `principal`, `on_behalf_of`, and allowed routes from the
105
+ current authenticated server session.
106
+
107
+ 1. BailingHub appends only `authorization_id` to the configured page URL.
108
+ 2. The page sends only that ID to its own backend.
109
+ 3. The backend uses its server-side Client Token to read the authorization context.
110
+ 4. After user confirmation, the backend derives trusted identity and approves the request.
111
+ 5. The page navigates only to the `redirect_uri` returned by BailingHub.
112
+ 6. Deny or revoke the session when the user rejects, leaves the organization, or loses a device.
113
+
114
+ PHP example:
115
+
116
+ ```php
117
+ use Bailing\Connect\AgentAuth;
118
+
119
+ $agentAuth = new AgentAuth(
120
+ getenv('BAILINGHUB_BASE_URL'),
121
+ getenv('BAILINGHUB_CLIENT_TOKEN')
122
+ );
123
+
124
+ $context = $agentAuth->context($authorizationId);
125
+
126
+ // Both values come from the authenticated backend session, never from browser claims.
127
+ $result = $agentAuth->approve(
128
+ $authorizationId,
129
+ [
130
+ 'id' => (string) $currentUser->id,
131
+ 'tenant' => (string) $tenantId,
132
+ 'roles' => $currentUser->roles,
133
+ ],
134
+ "tenant_{$tenantId}:user_{$currentUser->id}",
135
+ $context['requested_routes']
136
+ );
137
+
138
+ return redirect($result['redirect_uri']);
139
+ ```
140
+
141
+ Other languages can implement the Agent Auth v1 HTTP contract directly. The Client Token stays
142
+ in the backend `Authorization: Bearer <BUSINESS_CLIENT_TOKEN>` header and is never sent to the
143
+ browser.
144
+
145
+ ## 5. Local Agent user: install and authorize
146
+
147
+ For DSH, follow the versioned installation and compatibility matrix in the
148
+ [dsh-bailinghub repository](https://github.com/bailinghub/bailinghub-dsh-plugin). The native
149
+ plugin configuration contains only:
150
+
151
+ ```text
152
+ hubUrl=https://hub.example.com
153
+ clientAppId=merchant-agent
154
+ workspace=order-assistant
155
+ connectionName=default
156
+ ```
157
+
158
+ Equivalent environment variables are:
159
+
160
+ ```bash
161
+ export BAILINGHUB_HUB_URL='https://hub.example.com'
162
+ export BAILINGHUB_CLIENT_APP_ID='merchant-agent'
163
+ export BAILINGHUB_WORKSPACE='order-assistant'
164
+ export BAILINGHUB_CONNECTION_NAME='default'
165
+ ```
166
+
167
+ `hubUrl` is the Hub origin without `/console`, `/agent-api`, or a business path. `workspace` is a
168
+ route key, not a business domain, tenant ID, or chat-entry ID.
169
+
170
+ Run in DSH:
171
+
172
+ ```text
173
+ /bailinghub login
174
+ /bailinghub status
175
+ ```
176
+
177
+ Login creates a random loopback PKCE callback and opens the business authorization page. The SDK
178
+ uses macOS Keychain; a Linux/POSIX file fallback is explicit opt-in and requires mode `0600`.
179
+ Agent Session fails closed on Windows until a native secure store is available.
180
+
181
+ Configure the model provider and model API key separately in DSH. The BailingHub plugin neither
182
+ reads nor manages model-provider keys.
183
+
184
+ ## 6. Multiple Hubs and workspaces
185
+
186
+ A connection is uniquely bound to `Hub + clientAppId + workspace`; `connectionName` is only its
187
+ local alias. The first public version authorizes one workspace per connection for least
188
+ privilege. Use a different alias and a new browser authorization for another Hub or route. Never
189
+ copy access/refresh-token files between connections.
190
+
191
+ ## 7. Minimum acceptance
192
+
193
+ 1. Install a clean DSH profile using public packages only, without local paths or tarballs.
194
+ 2. Confirm `/bailinghub login` opens the configured business authorization domain.
195
+ 3. Confirm `/bailinghub status` exposes non-secret session metadata only.
196
+ 4. Run one read-only business capability.
197
+ 5. Run one reversible write that follows ACC/route approval semantics and is never duplicated.
198
+ 6. Confirm BailingHub shows local-orchestration and Hub-governance trace boundaries.
199
+ 7. Confirm logs, pages, and package artifacts contain no Client Token, Agent token, model key,
200
+ business cookie, tool argument value, or response body.
201
+
202
+ ## 8. Troubleshooting
203
+
204
+ - **`invalid_client`:** check `clientAppId`, enabled state, authorization URL, and allowed route.
205
+ - **Authorization page does not open:** production requires HTTPS; only loopback development may
206
+ use port-qualified `127.0.0.1` or `::1` HTTP.
207
+ - **Authorized but no tools:** check the Tool Provider, `tools.agent_direct.enabled`, audience
208
+ policy, and Local Agent Runtime switch.
209
+ - **A write requires approval:** inspect the ACC declaration first. `force_approval_tools` may
210
+ only make policy stricter.
211
+ - **Missing SDK:** the host adapter and `bailinghub-mcp-server/sdk` were not installed at compatible
212
+ versions. Fix the public package dependency; do not copy SDK source or add a local absolute path.
@@ -0,0 +1,206 @@
1
+ # Agent Client v1 接入指南
2
+
3
+ [English](AGENT_CLIENT_QUICKSTART.en.md) | 简体中文
4
+
5
+ Agent Client 让 DeepSeek Harness 等本地智能体负责理解、规划和多步工具选择,同时继续由
6
+ BailingHub 负责可信业务身份、能力裁剪、审批、幂等、业务调用和审计。它不是现有“执行器”,
7
+ 也不是把业务系统的账号、Token 或接口地址交给本地模型。
8
+
9
+ 本指南面向三类角色:BailingHub 部署者、业务系统开发者和本地智能体使用者。三者需要填写的
10
+ 配置不同,不应共享同一份密钥或配置文件。
11
+
12
+ ## 1. 组件关系
13
+
14
+ ```text
15
+ 本地智能体宿主(例如 DSH)
16
+ └─ dsh-bailinghub:接入宿主生命周期、动态工具和可见结果
17
+ └─ bailinghub-mcp-server/sdk:PKCE、浏览器登录、凭据存储、Runtime DTO
18
+ └─ BailingHub Core:身份重验、路由/能力裁剪、审批、执行、审计、会话总账
19
+ ├─ 业务系统授权页:复用业务登录态确认当前用户和允许的 workspace
20
+ └─ Tool Provider:业务侧 ACC 能力声明、验签和最终业务授权
21
+ ```
22
+
23
+ 依赖方向始终从宿主指向通用 SDK,再指向 BailingHub Core。业务系统不会把自己的登录 Cookie、
24
+ 密码或业务 API Secret 发给 DSH;DSH 也不会直接调用业务 API。
25
+
26
+ ## 2. 谁配置什么
27
+
28
+ | 配置 | 谁填写 | 存放位置 | 是否秘密 |
29
+ |---|---|---|---|
30
+ | `hubUrl` | DSH/宿主部署者 | 宿主插件配置 | 否;只填中枢公开 HTTPS 根地址 |
31
+ | `clientAppId` | DSH/宿主部署者 | 宿主插件配置 | 否;对应中枢“接入方”的 `app_id` |
32
+ | `workspace` | DSH/宿主部署者 | 宿主插件配置 | 否;对应允许的 BailingHub `route_key` |
33
+ | `connectionName` | 最终用户或宿主部署者 | 本机 SDK 连接注册表 | 否;只是本机可读别名 |
34
+ | Agent 授权页 URL | 业务开发者/中枢管理员 | BailingHub 接入方配置 | 否,但不应让最终用户手填 |
35
+ | 接入方 Client Token | 业务后端 | 服务端 Secret/环境变量 | **是;不得进入浏览器、DSH 或公开配置** |
36
+ | Tool Provider Secret | 业务后端与 BailingHub | 两端 Secret/凭据管理 | **是;不得进入 DSH** |
37
+ | Agent access/refresh token | 通用 SDK | Keychain 或显式启用的安全存储 | **是;不得显示或写入插件配置** |
38
+ | 模型 API Key | 本地智能体宿主 | DSH 模型提供方凭据 | **是;与 BailingHub 授权完全独立** |
39
+ | BailingHub 管理 Token | 中枢管理员 | 中枢服务端 Secret | **是;不得进入任何 Agent 客户端** |
40
+
41
+ `clientAppId` 是公开客户端标识,不是 Client Token。插件安装完成后不应出现要求用户粘贴
42
+ BailingHub Client Token、Tool Provider Secret 或业务系统密码的步骤。
43
+
44
+ ## 3. BailingHub 部署者:开放一条 Agent Client workspace
45
+
46
+ ### 3.1 部署 Core 并执行数据库迁移
47
+
48
+ 使用包含 Agent Auth v1、Agent Client Runtime v1、migration 055 和 056 的 BailingHub 版本。
49
+ 升级后先确认:
50
+
51
+ - `/health` 和 `/health/ready` 正常;
52
+ - 数据库迁移没有 pending;
53
+ - 管理后台可以编辑“接入方”和“路由”的 Agent Client 配置;
54
+ - 生产环境使用 HTTPS。
55
+
56
+ 不要从自用实例复制数据库、Token、业务域名或 route 配置到公开部署。
57
+
58
+ ### 3.2 接入业务工具
59
+
60
+ 业务侧仍按既有工具接入流程发布 ACC/OpenAPI 能力声明,并在真实调用端执行验签与最终授权。
61
+ 可使用现有 `bailing/connect` SDK,也可以按公开 HTTP/HMAC 契约自行实现:
62
+
63
+ - [业务侧接入指南](第三方对接指南.md)
64
+ - [工具治理设计](TOOLS_DESIGN.md)
65
+ - [PHP SDK](../sdk/php/README.md)
66
+
67
+ 在 BailingHub“工具源”中登记业务侧 `base_url`、`spec_url` 和中枢侧凭据引用。Tool Provider
68
+ Secret 只存在于业务后端与中枢凭据管理中,不进入 route、知识库、提示词或 Agent Client 配置。
69
+
70
+ ### 3.3 配置 route
71
+
72
+ 在“路由”中选择刚才的工具源,并配置:
73
+
74
+ 1. `tools.agent_direct.enabled=true`,显式开放本地 Agent 工具面;
75
+ 2. 只把允许本地 Agent 调用的写操作精确列入 `write_tools`;只读能力无需逐项加入该数组;
76
+ 3. 工具审批默认继承业务侧 ACC 声明;只有确需额外收紧时才加入 `force_approval_tools`;
77
+ 4. 开启“本地 Agent Runtime”,按需填写补充指令和每轮主动工具数(1–12,默认 8);
78
+ 5. 配置 route 的受众、租户和角色边界,不使用 `*` 代替生产授权设计。
79
+
80
+ Agent Client 不会收到模型凭据、Tool Provider Secret、业务 API 地址或完整 `target_config`。
81
+
82
+ ### 3.4 创建接入方
83
+
84
+ 在“接入方”中新建一个用于 Agent Client 的应用:
85
+
86
+ - `app_id`:稳定的公开 `clientAppId`,例如 `merchant-agent`;
87
+ - `allowed_routes`:只选择要开放的 route;
88
+ - `agent_authorize_url`:业务系统实现的 HTTPS 授权页面,例如
89
+ `https://business.example.com/agent/authorize`;
90
+ - `enabled=true`,并设置合理限速。
91
+
92
+ 中枢创建接入方时会显示一次 Client Token。它只交给业务后端,用于业务授权页服务端调用
93
+ BailingHub Agent Auth 接口。不要把它写入 DSH 配置、网页 JavaScript、URL、README 或截图。
94
+
95
+ ## 4. 业务开发者:建立可信身份授权页
96
+
97
+ BailingHub SDK 提供的是服务端 Agent Auth 方法,不会在任意业务系统中自动生成统一样式的页面。
98
+ 业务系统负责提供一个符合自身登录和权限体系的页面;页面必须先要求用户登录,再由业务后端从
99
+ 当前服务端登录态推导 `principal`、`on_behalf_of` 和可授权 route。
100
+
101
+ ### 4.1 浏览器与后端边界
102
+
103
+ 1. BailingHub 在授权页 URL 后只追加 `authorization_id`;
104
+ 2. 授权页前端只把该 ID 发送给自己的业务后端;
105
+ 3. 业务后端携带服务端保存的 Client Token 调用 BailingHub 查询授权上下文;
106
+ 4. 用户确认后,业务后端从当前登录态生成可信身份并批准;
107
+ 5. 前端只跳转 BailingHub 返回的 `redirect_uri`,不得接受任意回调地址;
108
+ 6. 用户拒绝、员工离职或设备丢失时,调用 deny/revoke。
109
+
110
+ PHP 示例:
111
+
112
+ ```php
113
+ use Bailing\Connect\AgentAuth;
114
+
115
+ $agentAuth = new AgentAuth(
116
+ getenv('BAILINGHUB_BASE_URL'),
117
+ getenv('BAILINGHUB_CLIENT_TOKEN')
118
+ );
119
+
120
+ $context = $agentAuth->context($authorizationId);
121
+
122
+ // $currentUser 与 $tenantId 必须来自业务服务端登录态,不能相信前端提交值。
123
+ $result = $agentAuth->approve(
124
+ $authorizationId,
125
+ [
126
+ 'id' => (string) $currentUser->id,
127
+ 'tenant' => (string) $tenantId,
128
+ 'roles' => $currentUser->roles,
129
+ ],
130
+ "tenant_{$tenantId}:user_{$currentUser->id}",
131
+ $context['requested_routes']
132
+ );
133
+
134
+ return redirect($result['redirect_uri']);
135
+ ```
136
+
137
+ 非 PHP 项目可直接实现相同的 Agent Auth v1 HTTP 契约。Client Token 始终放在服务端
138
+ `Authorization: Bearer <BUSINESS_CLIENT_TOKEN>` 请求头中,不得下发到浏览器。
139
+
140
+ ## 5. 本地智能体使用者:安装并授权
141
+
142
+ 以 DSH 原生插件为例,安装步骤、目标版本和兼容矩阵以
143
+ [dsh-bailinghub 仓库](https://github.com/bailinghub/bailinghub-dsh-plugin)为准。插件配置只包含:
144
+
145
+ ```text
146
+ hubUrl=https://hub.example.com
147
+ clientAppId=merchant-agent
148
+ workspace=order-assistant
149
+ connectionName=default
150
+ ```
151
+
152
+ 对应环境变量为:
153
+
154
+ ```bash
155
+ export BAILINGHUB_HUB_URL='https://hub.example.com'
156
+ export BAILINGHUB_CLIENT_APP_ID='merchant-agent'
157
+ export BAILINGHUB_WORKSPACE='order-assistant'
158
+ export BAILINGHUB_CONNECTION_NAME='default'
159
+ ```
160
+
161
+ `hubUrl` 只填中枢根地址,不带 `/console`、`/agent-api` 或业务接口路径;`workspace` 填 route
162
+ 标识,不填业务域名、租户 ID 或聊天入口 ID。
163
+
164
+ 在 DSH 中执行:
165
+
166
+ ```text
167
+ /bailinghub login
168
+ /bailinghub status
169
+ ```
170
+
171
+ 登录命令会在本机随机 `127.0.0.1` 端口建立 PKCE 回调并打开业务系统授权页。授权成功后,SDK
172
+ 在 macOS 使用 Keychain 保存凭据;Linux/POSIX 文件回退必须显式启用且文件权限为 `0600`;
173
+ Windows 在具备原生安全存储前对 Agent Session 失败关闭。
174
+
175
+ DSH 的模型提供方和模型 API Key 需要在 DSH 自己的模型设置中单独配置。BailingHub 插件既不读取
176
+ 也不代管模型 Key。
177
+
178
+ ## 6. 多 Hub 与多 workspace
179
+
180
+ 一条连接由 `Hub + clientAppId + workspace` 唯一绑定,`connectionName` 只是该绑定的本机别名。
181
+ 首个公开版本按最小权限原则每次授权一个 workspace。需要连接另一套中枢或另一条 route 时,使用
182
+ 另一个 `connectionName` 并重新完成浏览器授权;不要复用或复制 access/refresh token 文件。
183
+
184
+ ## 7. 最小验收
185
+
186
+ 发布或接入完成后,至少验证:
187
+
188
+ 1. 全新 DSH Profile 只通过公开包安装,不引用本机路径或本地 tgz;
189
+ 2. `/bailinghub login` 打开的域名是开发者配置的业务授权页;
190
+ 3. `/bailinghub status` 只显示非秘密会话元数据;
191
+ 4. 本地 Agent 能查询一项只读能力;
192
+ 5. 一项可回滚写操作遵循 ACC/route 审批语义,且不会重复调用;
193
+ 6. BailingHub 会话页能看到本地编排边界和中枢治理轨迹;
194
+ 7. 日志、页面和包制品中没有 Client Token、Agent token、模型 Key、业务 Cookie、工具参数值或响应正文。
195
+
196
+ ## 8. 常见问题
197
+
198
+ - **`invalid_client`**:检查 `clientAppId`、接入方是否启用、是否配置授权页,以及 workspace 是否在
199
+ `allowed_routes` 中。
200
+ - **授权页打不开**:生产授权页必须是 HTTPS;仅本机开发允许带端口的 `127.0.0.1`/`::1` HTTP。
201
+ - **登录成功但没有业务工具**:检查 route 的工具源、`tools.agent_direct.enabled`、受众策略和
202
+ “本地 Agent Runtime”开关。
203
+ - **写操作要求审批**:先查看业务侧 ACC 声明;route 的 `force_approval_tools` 只能额外收紧,不能
204
+ 降低高风险或 ACC 明示审批。
205
+ - **提示缺少 SDK**:说明宿主适配器与 `bailinghub-mcp-server/sdk` 没有按兼容版本安装;不要通过
206
+ 复制 SDK 源码或改成本机绝对路径绕过,应修正公开包依赖。
@@ -0,0 +1,157 @@
1
+ # Agent Client Runtime v1
2
+
3
+ English | [简体中文](AGENT_CLIENT_RUNTIME_API.md)
4
+
5
+ This versioned API lets a browser-authorized local Agent plan locally while reusing BailingHub
6
+ identity revalidation, tool governance, approvals, audit, and the visible conversation ledger. It
7
+ does not replace `/run`, chat entries, or the executor protocol. See the
8
+ [Agent Client Integration Guide](AGENT_CLIENT_QUICKSTART.en.md) and
9
+ [Agent Auth v1](AGENT_AUTH_API.en.md) for deployment and authorization.
10
+
11
+ Every endpoint requires `Authorization: Bearer <Agent access token>` and returns
12
+ `Cache-Control: no-store`. The workspace is the intersection of the Client `allowed_routes`, Agent
13
+ Session `allowed_routes`, enabled and audience-compatible routes, and `tools.agent_direct`.
14
+ `agent_client.enabled=false` disables this Runtime only; it does not disable the older
15
+ `/agent-api/v1/tools` surface.
16
+
17
+ ## Endpoints
18
+
19
+ - `GET /agent-api/v1/workspaces`
20
+ - `GET /agent-api/v1/workspaces/:route/bootstrap`
21
+ - `POST /agent-api/v1/workspaces/:route/turns`
22
+ - `POST /agent-api/v1/workspaces/:route/capabilities/search`
23
+ - `POST /agent-api/v1/runs/:run_id/complete`
24
+ - Tool execution continues to use `POST /agent-api/v1/tool-invocations` and the existing resume
25
+ endpoint.
26
+
27
+ Raw JSON for `turns` and `complete` is limited to 512 KiB. Parsed values still have strict field
28
+ limits; the service rejects invalid input instead of truncating it silently.
29
+
30
+ ## Write and approval semantics
31
+
32
+ `tools.agent_direct.write_tools` exposes exact write operation IDs to the local Agent. Approval
33
+ requirements continue to come from the business ACC `risk`, `approval.required`, and
34
+ `approval.when` declarations. A route may add stricter approval by listing an exact operation ID
35
+ under `tools.agent_direct.force_approval_tools`; it cannot weaken ACC policy.
36
+
37
+ The deprecated private-candidate field `unattended_write_tools` remains readable for migration.
38
+ Do not configure it together with `force_approval_tools`. When converting old configuration, put
39
+ the difference between the old `write_tools` and `unattended_write_tools` sets into
40
+ `force_approval_tools`, rather than copying the old list unchanged.
41
+
42
+ ## Bootstrap
43
+
44
+ ```json
45
+ {
46
+ "schema_version": "bailing.agent-runtime-profile.v1",
47
+ "workspace": {
48
+ "route": "order-assistant",
49
+ "name": "Order assistant",
50
+ "description": ""
51
+ },
52
+ "profile": {
53
+ "revision": "<sha256>",
54
+ "instructions": "...",
55
+ "memory": {
56
+ "recent_messages": 12,
57
+ "recent_budget_chars": 8000,
58
+ "per_message_chars": 2000,
59
+ "summary_enabled": false,
60
+ "summary_max_chars": 1200
61
+ },
62
+ "knowledge": {
63
+ "enabled": true,
64
+ "sources": 1,
65
+ "mode": "chunk",
66
+ "top_k": 5,
67
+ "max_docs": 4,
68
+ "page_boost": false
69
+ },
70
+ "governance": {
71
+ "planner": "local_agent",
72
+ "execution": "bailinghub_governed",
73
+ "authorization": "server_revalidated",
74
+ "knowledge_trust": "reference_only",
75
+ "tool_results_trust": "untrusted_data",
76
+ "hidden_reasoning_sync": false,
77
+ "max_tool_calls": 5,
78
+ "permission": "full"
79
+ }
80
+ },
81
+ "capabilities": {
82
+ "revision": "<sha256>",
83
+ "authorized_total": 312,
84
+ "active_limit": 8,
85
+ "readonly": 311,
86
+ "writes": 1,
87
+ "approval_required": 0
88
+ }
89
+ }
90
+ ```
91
+
92
+ Bootstrap extracts only `target_config.system_prompt` field by field and appends optional
93
+ `agent_client.instructions`. It never returns credentials, model configuration, other
94
+ `target_config`, Tool Provider base URLs/secrets, or business egress configuration.
95
+
96
+ ## Turn
97
+
98
+ ```json
99
+ {
100
+ "client_conversation_id": "local-conversation-1",
101
+ "client_turn_id": "turn-1",
102
+ "user_message_id": "message-1",
103
+ "user_input": "Find the employee named Ada",
104
+ "page_context": { "url": "/staff", "title": "Staff" },
105
+ "renderers": ["markdown", "bailing-form.v1"]
106
+ }
107
+ ```
108
+
109
+ The four identifiers/input fields are required. `page_context` and `renderers` are optional and
110
+ unknown fields are rejected. `user_input` is limited to 64,000 characters; no more than 20 unique,
111
+ valid renderer names are accepted.
112
+
113
+ The response uses `bailing.agent-turn-context.v1` and contains a stable `run_id`, profile and
114
+ capability revisions, safe instructions, memory, reference-only knowledge, governance, and the
115
+ active typed tool set. The same Agent Session + route + conversation + turn, and the same
116
+ `user_message_id`, resolve to the same frozen run/context. A changed payload under the same key
117
+ returns `409`. Core reads prior memory before it appends the current visible user message.
118
+
119
+ Capability search returns no more than 12 complete typed tools under
120
+ `bailing.agent-capability-search.v1`, always within the authorized set. If semantic retrieval for
121
+ any source is unavailable, the whole search deterministically falls back to lexical ordering. A
122
+ search needs a non-empty query or a valid `run_id` from the same Agent Session and route.
123
+
124
+ ## Tool invocation and recovery
125
+
126
+ Invoke only a tool projected by the current bootstrap/search result. Core revalidates the Agent
127
+ Session, route, operation ID, ACC declaration, approval state, limits, and business authorization
128
+ on every invocation. When the result is pending, in progress, or uncertain, resume the original
129
+ `invocation_id`; do not create a replacement write call.
130
+
131
+ The Hub records visible governance milestones, operation IDs, parameter key names, approval state,
132
+ and business status. It does not receive model hidden reasoning, complete sensitive arguments, or
133
+ raw business response bodies.
134
+
135
+ ## Complete
136
+
137
+ ```json
138
+ {
139
+ "assistant_message_id": "assistant-message-1",
140
+ "status": "completed",
141
+ "content": "Done.",
142
+ "model": "optional-model-name",
143
+ "runtime": "optional-runtime-name",
144
+ "usage": {
145
+ "input_tokens": 120,
146
+ "cached_input_tokens": 80,
147
+ "output_tokens": 30,
148
+ "total_tokens": 150,
149
+ "tool_calls": 1
150
+ }
151
+ }
152
+ ```
153
+
154
+ The assistant message ID, status, visible content, model, runtime, and public usage form one
155
+ idempotency fingerprint. An identical replay returns `200`; any changed field returns `409`.
156
+ Hidden reasoning is neither accepted nor stored. Content is limited to 64,000 characters and
157
+ model/runtime to 191 characters each; invalid values are rejected rather than normalized silently.