berry-agent 0.1.0-alpha.1

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 (401) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +96 -0
  3. package/dist/.api-emit.stamp +1 -0
  4. package/dist/.build-meta.json +6 -0
  5. package/dist/agent/events.js +1 -0
  6. package/dist/agent/index.js +2 -0
  7. package/dist/agent/loop.js +139 -0
  8. package/dist/agent/queue.js +119 -0
  9. package/dist/agent/stream.js +62 -0
  10. package/dist/agent/tools-batch.js +221 -0
  11. package/dist/agent/types.js +8 -0
  12. package/dist/api/berry-agent-llm.d.ts +18 -0
  13. package/dist/api/berry-agent.d.ts +9 -0
  14. package/dist/api/surface.json +1632 -0
  15. package/dist/api/tsconfig.paths.json +12 -0
  16. package/dist/api/typebox-compile.d.ts +2 -0
  17. package/dist/api/typebox-value.d.ts +2 -0
  18. package/dist/api/typebox.d.ts +2 -0
  19. package/dist/browser/cdp.js +256 -0
  20. package/dist/browser/codes.js +31 -0
  21. package/dist/browser/discover.js +107 -0
  22. package/dist/browser/engine.js +169 -0
  23. package/dist/browser/index.js +17 -0
  24. package/dist/browser/install.js +342 -0
  25. package/dist/browser/page.js +321 -0
  26. package/dist/browser/service.js +158 -0
  27. package/dist/browser/tools.js +198 -0
  28. package/dist/browser/types.js +81 -0
  29. package/dist/channels/ask-queue.js +82 -0
  30. package/dist/channels/commands.js +129 -0
  31. package/dist/channels/engine/cell.js +143 -0
  32. package/dist/channels/engine/diff.js +143 -0
  33. package/dist/channels/engine/engine.js +416 -0
  34. package/dist/channels/engine/index.js +8 -0
  35. package/dist/channels/engine/input-keys.js +86 -0
  36. package/dist/channels/engine/input.js +596 -0
  37. package/dist/channels/engine/memory-io.js +71 -0
  38. package/dist/channels/engine/process-io.js +38 -0
  39. package/dist/channels/engine/types.js +17 -0
  40. package/dist/channels/engine/width.js +122 -0
  41. package/dist/channels/index.js +15 -0
  42. package/dist/channels/registry.js +56 -0
  43. package/dist/channels/sdk/admit.js +29 -0
  44. package/dist/channels/sdk/backend.js +102 -0
  45. package/dist/channels/sdk/cursor.js +56 -0
  46. package/dist/channels/sdk/index.js +20 -0
  47. package/dist/channels/sdk/jsonl.js +208 -0
  48. package/dist/channels/sdk/protocol.js +24 -0
  49. package/dist/channels/sdk/schema.js +91 -0
  50. package/dist/channels/sdk/wire-core.js +446 -0
  51. package/dist/channels/service.js +169 -0
  52. package/dist/channels/tui/autocomplete/autocomplete.js +42 -0
  53. package/dist/channels/tui/autocomplete/file-mentions.js +106 -0
  54. package/dist/channels/tui/autocomplete/popup.js +148 -0
  55. package/dist/channels/tui/autocomplete/provider.js +7 -0
  56. package/dist/channels/tui/autocomplete/token.js +68 -0
  57. package/dist/channels/tui/backend/ansi-rows.js +211 -0
  58. package/dist/channels/tui/backend/main-screen.js +207 -0
  59. package/dist/channels/tui/backend/osc.js +91 -0
  60. package/dist/channels/tui/backend/transcript.js +230 -0
  61. package/dist/channels/tui/backend/tui-backend.js +915 -0
  62. package/dist/channels/tui/editor/editor-model.js +543 -0
  63. package/dist/channels/tui/editor/editor-view.js +140 -0
  64. package/dist/channels/tui/editor/editor.js +275 -0
  65. package/dist/channels/tui/editor/undo-stack.js +35 -0
  66. package/dist/channels/tui/editor/visual-lines.js +126 -0
  67. package/dist/channels/tui/editor/word-nav.js +106 -0
  68. package/dist/channels/tui/history/history-viewer.js +403 -0
  69. package/dist/channels/tui/index.js +3 -0
  70. package/dist/channels/tui/layout.js +167 -0
  71. package/dist/channels/tui/markdown/blocks.js +111 -0
  72. package/dist/channels/tui/markdown/inline.js +75 -0
  73. package/dist/channels/tui/markdown/markdown.js +182 -0
  74. package/dist/channels/tui/memory/memory-viewer.js +540 -0
  75. package/dist/channels/tui/overlay/alt-screen.js +105 -0
  76. package/dist/channels/tui/overlay/overlay.js +57 -0
  77. package/dist/channels/tui/overlay/select-confirm.js +156 -0
  78. package/dist/channels/tui/panels/todo-panel.js +54 -0
  79. package/dist/channels/tui/panels/tool-progress-panel.js +93 -0
  80. package/dist/channels/tui/scroll/scroll-view.js +254 -0
  81. package/dist/channels/tui/status/status-line.js +76 -0
  82. package/dist/channels/tui/text.js +38 -0
  83. package/dist/channels/tui/theme.js +41 -0
  84. package/dist/channels/types.js +11 -0
  85. package/dist/channels/ui-core.js +209 -0
  86. package/dist/checkpoint/capture.js +45 -0
  87. package/dist/checkpoint/codes.js +33 -0
  88. package/dist/checkpoint/command.js +82 -0
  89. package/dist/checkpoint/gate.js +48 -0
  90. package/dist/checkpoint/index.js +20 -0
  91. package/dist/checkpoint/restore.js +172 -0
  92. package/dist/checkpoint/store.js +247 -0
  93. package/dist/checkpoint/types.js +4 -0
  94. package/dist/checkpoint/walk.js +146 -0
  95. package/dist/compaction/ccr-tools.js +112 -0
  96. package/dist/compaction/ccr.js +84 -0
  97. package/dist/compaction/codes.js +21 -0
  98. package/dist/compaction/index.js +18 -0
  99. package/dist/compaction/policy.js +200 -0
  100. package/dist/compaction/service.js +422 -0
  101. package/dist/compaction/slots.js +121 -0
  102. package/dist/compaction/types.js +10 -0
  103. package/dist/context/codes.js +40 -0
  104. package/dist/context/events.js +160 -0
  105. package/dist/context/index.js +13 -0
  106. package/dist/context/logger.js +136 -0
  107. package/dist/context/scope.js +182 -0
  108. package/dist/context/workspace.js +104 -0
  109. package/dist/contracts/agent-events.d.ts +73 -0
  110. package/dist/contracts/agent-events.js +16 -0
  111. package/dist/contracts/api.d.ts +249 -0
  112. package/dist/contracts/api.js +299 -0
  113. package/dist/contracts/approval.d.ts +37 -0
  114. package/dist/contracts/approval.js +9 -0
  115. package/dist/contracts/env-ref.d.ts +41 -0
  116. package/dist/contracts/env-ref.js +32 -0
  117. package/dist/contracts/errors.d.ts +56 -0
  118. package/dist/contracts/errors.js +266 -0
  119. package/dist/contracts/events.d.ts +70 -0
  120. package/dist/contracts/events.js +329 -0
  121. package/dist/contracts/index.d.ts +32 -0
  122. package/dist/contracts/index.js +26 -0
  123. package/dist/contracts/llm.d.ts +275 -0
  124. package/dist/contracts/llm.js +12 -0
  125. package/dist/contracts/messages.d.ts +63 -0
  126. package/dist/contracts/messages.js +65 -0
  127. package/dist/contracts/redact.d.ts +46 -0
  128. package/dist/contracts/redact.js +223 -0
  129. package/dist/contracts/tools.d.ts +197 -0
  130. package/dist/contracts/tools.js +33 -0
  131. package/dist/contracts/types.d.ts +302 -0
  132. package/dist/contracts/types.js +50 -0
  133. package/dist/contracts/ui.d.ts +122 -0
  134. package/dist/contracts/ui.js +15 -0
  135. package/dist/conversation/agent-service.js +61 -0
  136. package/dist/conversation/approval-wiring.js +82 -0
  137. package/dist/conversation/backoff.js +38 -0
  138. package/dist/conversation/codes.js +47 -0
  139. package/dist/conversation/control-tools.js +102 -0
  140. package/dist/conversation/control.js +210 -0
  141. package/dist/conversation/driver.js +922 -0
  142. package/dist/conversation/index.js +12 -0
  143. package/dist/conversation/model-visible.js +169 -0
  144. package/dist/conversation/open-tools.js +126 -0
  145. package/dist/conversation/reseed.js +132 -0
  146. package/dist/conversation/sessions.js +215 -0
  147. package/dist/conversation/todo.js +203 -0
  148. package/dist/conversation/types.js +35 -0
  149. package/dist/conversation/wiring.js +204 -0
  150. package/dist/credentials/codes.js +59 -0
  151. package/dist/credentials/commands.js +186 -0
  152. package/dist/credentials/env-ref.js +34 -0
  153. package/dist/credentials/index.js +28 -0
  154. package/dist/credentials/migration.js +18 -0
  155. package/dist/credentials/oauth.js +266 -0
  156. package/dist/credentials/refresh.js +152 -0
  157. package/dist/credentials/secrets.js +118 -0
  158. package/dist/credentials/types.js +30 -0
  159. package/dist/exec/bash.js +279 -0
  160. package/dist/exec/codes.js +37 -0
  161. package/dist/exec/env.js +99 -0
  162. package/dist/exec/environment.js +39 -0
  163. package/dist/exec/git-guard.js +623 -0
  164. package/dist/exec/index.js +24 -0
  165. package/dist/exec/registry.js +120 -0
  166. package/dist/exec/spawn.js +281 -0
  167. package/dist/exec/tail.js +77 -0
  168. package/dist/exec/types.js +5 -0
  169. package/dist/goal/codes.js +33 -0
  170. package/dist/goal/command.js +175 -0
  171. package/dist/goal/fold.js +121 -0
  172. package/dist/goal/gates.js +122 -0
  173. package/dist/goal/index.js +22 -0
  174. package/dist/goal/migration.js +48 -0
  175. package/dist/goal/service.js +501 -0
  176. package/dist/goal/todo-tool.js +174 -0
  177. package/dist/goal/types.js +1 -0
  178. package/dist/goal/update-tool.js +46 -0
  179. package/dist/host/approval-cmd.js +276 -0
  180. package/dist/host/assembly.js +1146 -0
  181. package/dist/host/boot-failures.js +104 -0
  182. package/dist/host/budget-advisory.js +74 -0
  183. package/dist/host/budget-broadcast.js +50 -0
  184. package/dist/host/builtins.js +54 -0
  185. package/dist/host/cli.js +544 -0
  186. package/dist/host/codes.js +120 -0
  187. package/dist/host/config-schema.js +195 -0
  188. package/dist/host/conversation-stack.js +654 -0
  189. package/dist/host/core-plugins.js +1685 -0
  190. package/dist/host/credentials-cmd.js +42 -0
  191. package/dist/host/disclosure.js +30 -0
  192. package/dist/host/dispatch.js +105 -0
  193. package/dist/host/doors-cmd.js +154 -0
  194. package/dist/host/dump-config.js +44 -0
  195. package/dist/host/hook-dispatch-guard.js +27 -0
  196. package/dist/host/import-gate.js +153 -0
  197. package/dist/host/index.js +54 -0
  198. package/dist/host/issue-session.js +241 -0
  199. package/dist/host/loader.js +372 -0
  200. package/dist/host/main.js +161 -0
  201. package/dist/host/manifest.js +381 -0
  202. package/dist/host/mcp-entry.js +53 -0
  203. package/dist/host/plugin-boot.js +952 -0
  204. package/dist/host/plugin-context.js +672 -0
  205. package/dist/host/plugin-install.js +480 -0
  206. package/dist/host/plugin-reload.js +124 -0
  207. package/dist/host/plugin-store.js +462 -0
  208. package/dist/host/plugin-tools.js +359 -0
  209. package/dist/host/plugin-uninstall.js +257 -0
  210. package/dist/host/plugins-cmd.js +302 -0
  211. package/dist/host/plugins-command.js +130 -0
  212. package/dist/host/plugins-config.js +195 -0
  213. package/dist/host/prompt-sections.js +133 -0
  214. package/dist/host/run-entry.js +544 -0
  215. package/dist/host/runtime.js +209 -0
  216. package/dist/host/scheduler-tick.js +294 -0
  217. package/dist/host/serve-daemon.js +501 -0
  218. package/dist/host/serve-entry.js +279 -0
  219. package/dist/host/session-anchor.js +41 -0
  220. package/dist/host/sessions-cmd.js +219 -0
  221. package/dist/host/sessions-face.js +47 -0
  222. package/dist/host/settings-store.js +112 -0
  223. package/dist/host/signals.js +69 -0
  224. package/dist/host/single-instance.js +105 -0
  225. package/dist/host/subagent-factory.js +291 -0
  226. package/dist/host/testkit/harness.js +283 -0
  227. package/dist/host/testkit/index.js +14 -0
  228. package/dist/host/testkit/matrix.js +302 -0
  229. package/dist/host/tool-policy-store.js +177 -0
  230. package/dist/host/triggers.js +212 -0
  231. package/dist/host/tui-entry.js +216 -0
  232. package/dist/host/webui-bridge.js +165 -0
  233. package/dist/issue/codes.js +38 -0
  234. package/dist/issue/filter.js +146 -0
  235. package/dist/issue/github.js +116 -0
  236. package/dist/issue/index.js +17 -0
  237. package/dist/issue/mount.js +70 -0
  238. package/dist/issue/poll.js +58 -0
  239. package/dist/issue/service.js +341 -0
  240. package/dist/issue/tools.js +70 -0
  241. package/dist/issue/types.js +34 -0
  242. package/dist/issue/webhook.js +126 -0
  243. package/dist/llm/codes.js +77 -0
  244. package/dist/llm/complete.js +196 -0
  245. package/dist/llm/events.js +15 -0
  246. package/dist/llm/index.js +24 -0
  247. package/dist/llm/inflight.js +65 -0
  248. package/dist/llm/model-id.js +58 -0
  249. package/dist/llm/provider-face.d.ts +32 -0
  250. package/dist/llm/provider-face.js +34 -0
  251. package/dist/llm/recovery.js +146 -0
  252. package/dist/llm/runtime.js +46 -0
  253. package/dist/llm/stream-fn.js +147 -0
  254. package/dist/lsp/codes.js +31 -0
  255. package/dist/lsp/connection.js +147 -0
  256. package/dist/lsp/frame.js +110 -0
  257. package/dist/lsp/index.js +16 -0
  258. package/dist/lsp/inject.js +58 -0
  259. package/dist/lsp/instance.js +264 -0
  260. package/dist/lsp/service.js +522 -0
  261. package/dist/lsp/tools.js +53 -0
  262. package/dist/lsp/types.js +201 -0
  263. package/dist/mcp/bridge.js +222 -0
  264. package/dist/mcp/codes.js +21 -0
  265. package/dist/mcp/index.js +19 -0
  266. package/dist/mcp/jsonrpc.js +230 -0
  267. package/dist/mcp/service.js +125 -0
  268. package/dist/mcp/tools.js +145 -0
  269. package/dist/mcp/types.js +108 -0
  270. package/dist/memory/cite.js +131 -0
  271. package/dist/memory/codes.js +50 -0
  272. package/dist/memory/command.js +71 -0
  273. package/dist/memory/consolidate.js +219 -0
  274. package/dist/memory/cycle.js +109 -0
  275. package/dist/memory/dao.js +878 -0
  276. package/dist/memory/diff.js +238 -0
  277. package/dist/memory/extract.js +181 -0
  278. package/dist/memory/fts.js +58 -0
  279. package/dist/memory/index.js +42 -0
  280. package/dist/memory/inject.js +354 -0
  281. package/dist/memory/merge.js +178 -0
  282. package/dist/memory/migration.js +132 -0
  283. package/dist/memory/pollution.js +59 -0
  284. package/dist/memory/port.js +385 -0
  285. package/dist/memory/review.js +201 -0
  286. package/dist/memory/scan.js +89 -0
  287. package/dist/memory/tools.js +508 -0
  288. package/dist/memory/types.js +140 -0
  289. package/dist/obs/codes.js +38 -0
  290. package/dist/obs/db.js +115 -0
  291. package/dist/obs/index.js +20 -0
  292. package/dist/obs/rollup.js +61 -0
  293. package/dist/obs/service.js +363 -0
  294. package/dist/obs/session-tools.js +282 -0
  295. package/dist/obs/session-view.js +225 -0
  296. package/dist/obs/tool.js +97 -0
  297. package/dist/obs/types.js +1 -0
  298. package/dist/persist/audit.js +63 -0
  299. package/dist/persist/aux.js +39 -0
  300. package/dist/persist/codes.js +35 -0
  301. package/dist/persist/index.js +30 -0
  302. package/dist/persist/load-history.js +50 -0
  303. package/dist/persist/migrations.js +37 -0
  304. package/dist/persist/paths.js +108 -0
  305. package/dist/persist/persistence.js +229 -0
  306. package/dist/persist/schema.js +99 -0
  307. package/dist/persist/secret-box.js +95 -0
  308. package/dist/persist/store.js +706 -0
  309. package/dist/persist/write-behind.js +238 -0
  310. package/dist/safety/approval.js +171 -0
  311. package/dist/safety/bwrap.js +123 -0
  312. package/dist/safety/codes.js +57 -0
  313. package/dist/safety/danger.js +505 -0
  314. package/dist/safety/gate.js +226 -0
  315. package/dist/safety/index.js +24 -0
  316. package/dist/safety/presets.js +73 -0
  317. package/dist/safety/roots.js +139 -0
  318. package/dist/safety/sandbox.js +214 -0
  319. package/dist/safety/seatbelt.js +88 -0
  320. package/dist/safety/sensitive.js +34 -0
  321. package/dist/safety/tool-policy.js +203 -0
  322. package/dist/safety/types.js +13 -0
  323. package/dist/scheduler/codes.js +54 -0
  324. package/dist/scheduler/cron-backend.js +135 -0
  325. package/dist/scheduler/engine.js +280 -0
  326. package/dist/scheduler/gates.js +60 -0
  327. package/dist/scheduler/index.js +25 -0
  328. package/dist/scheduler/migration.js +22 -0
  329. package/dist/scheduler/runner.js +171 -0
  330. package/dist/scheduler/schedule.js +193 -0
  331. package/dist/scheduler/service.js +283 -0
  332. package/dist/scheduler/tick.js +139 -0
  333. package/dist/scheduler/types.js +1 -0
  334. package/dist/sdk/http.js +865 -0
  335. package/dist/sdk/index.js +22 -0
  336. package/dist/sdk/mcp.js +289 -0
  337. package/dist/sdk/plugin-route-registry.js +126 -0
  338. package/dist/sdk/plugin-routes.js +149 -0
  339. package/dist/sdk/security.js +116 -0
  340. package/dist/sdk/types.js +32 -0
  341. package/dist/session/budget.js +159 -0
  342. package/dist/session/codes.js +40 -0
  343. package/dist/session/derive.js +180 -0
  344. package/dist/session/event-data.js +1 -0
  345. package/dist/session/fork.js +72 -0
  346. package/dist/session/import-gates.js +148 -0
  347. package/dist/session/index.js +18 -0
  348. package/dist/session/recover.js +103 -0
  349. package/dist/session/session.js +279 -0
  350. package/dist/session/snapshot.js +107 -0
  351. package/dist/skills/agents.js +242 -0
  352. package/dist/skills/codes.js +51 -0
  353. package/dist/skills/discovery.js +266 -0
  354. package/dist/skills/frontmatter.js +215 -0
  355. package/dist/skills/index.js +28 -0
  356. package/dist/skills/load.js +118 -0
  357. package/dist/skills/manage.js +201 -0
  358. package/dist/skills/registry.js +138 -0
  359. package/dist/skills/render.js +124 -0
  360. package/dist/skills/sections.js +171 -0
  361. package/dist/skills/types.js +21 -0
  362. package/dist/subagent/codes.js +50 -0
  363. package/dist/subagent/declarative.js +55 -0
  364. package/dist/subagent/index.js +19 -0
  365. package/dist/subagent/notify.js +51 -0
  366. package/dist/subagent/provide.js +7 -0
  367. package/dist/subagent/registry.js +149 -0
  368. package/dist/subagent/service.js +338 -0
  369. package/dist/subagent/surface.js +44 -0
  370. package/dist/subagent/tool.js +153 -0
  371. package/dist/subagent/types.js +25 -0
  372. package/dist/tools/apply-patch.js +164 -0
  373. package/dist/tools/codes.js +109 -0
  374. package/dist/tools/fs.js +501 -0
  375. package/dist/tools/index.js +17 -0
  376. package/dist/tools/observed.js +99 -0
  377. package/dist/tools/pipeline.js +176 -0
  378. package/dist/tools/protected-read.js +54 -0
  379. package/dist/tools/registry.js +250 -0
  380. package/dist/tools/search.js +451 -0
  381. package/dist/tools/worktree.js +327 -0
  382. package/dist/web/codes.js +37 -0
  383. package/dist/web/gate.js +43 -0
  384. package/dist/web/hygiene.js +195 -0
  385. package/dist/web/index.js +16 -0
  386. package/dist/web/service.js +207 -0
  387. package/dist/web/ssrf-guard.js +44 -0
  388. package/dist/web/tool.js +76 -0
  389. package/dist/web/types.js +10 -0
  390. package/dist/webui/assets/index-B-t2O8gh.js +22 -0
  391. package/dist/webui/assets/index-DOlgSZ9Q.css +2 -0
  392. package/dist/webui/index.html +14 -0
  393. package/dist/webui/index.js +14 -0
  394. package/dist/webui/server.js +549 -0
  395. package/dist/webui/types.js +38 -0
  396. package/examples/README.md +26 -0
  397. package/examples/minimal-code-plugin/entry.js +43 -0
  398. package/examples/minimal-code-plugin/package.json +12 -0
  399. package/examples/pure-skill-pack/package.json +14 -0
  400. package/examples/pure-skill-pack/skills/markdown-table/SKILL.md +32 -0
  401. package/package.json +86 -0
@@ -0,0 +1,329 @@
1
+ import { BaseError } from './errors.js';
2
+ /** 事件类别四分法闭集(类别列口径与 05 §3.1 投影执法对齐) */
3
+ export const EVENT_CATEGORIES = ['surface', 'snapshot', 'log-only', 'structure'];
4
+ /**
5
+ * 核心事件类型 32 词(05 §1.1 表格逐条转录;owner 归属按表注:
6
+ * gate/decision 归 tools、llm/usage 归 llm、llm/retry 注册走 session
7
+ * 核心词汇(llm 模块不知道驱动存在)、plugin/uninstalled 宿主写点 host、
8
+ * approval/* 与 sandbox/mode 归 safety 域、todo/write 与
9
+ * session/thinking-level 归 conversation〔05 行 66:写入者 = conversation
10
+ * 件档位切换面——agent 件零存储感知、不写本词〕)。
11
+ * tier 全 stable(核心词汇 = 已收口契约面,03 §8.3 零隐式载体)。
12
+ * 核心词计数 16→19:compaction/start|surface|end 随 compaction 纵切批入表;
13
+ * 19→20:session/thinking-level 随遗漏审计批回填(05 §1.1 行 66 agent 纵切
14
+ * 批已定名,注册表漏登——批 11 conversation 写入将撞词汇闸,先回填止血);
15
+ * 20→22:plugin/opens·capability/used 随 U3 落码批入册(05 §1.1 行 70-71
16
+ * 开门制两审计词——载体 = 进程级 audit_events 审计流〔05 §9〕非会话流,
17
+ * 入册值 = 核心词身份双闸 + 目录单一真源;写入面随 U3-2 persist 笔落码);
18
+ * 22→23:compaction/fallback 随 U4 落码批入册(05 §1.1 行 77 回落三律第 3
19
+ * 律审计词——2026-09-07 双轴二轮评估批已定词形、随 U4 落码批注册兑现);
20
+ * 23→28:plugin/installed·mounted·unmounted·toggled·updated 随生命周期
21
+ * 五词 audit 落账批入册(05 §1.1 生命周期归因面行——uninstall 词已随装机
22
+ * 面落码批先行落 audit_events 载体〔CLI 人面无会话场景唯一可达 durable
23
+ * 载体〕,本五词与之同载体并列成族——六词同面;词形单源本表:
24
+ * installed {id,source,version} / mounted·unmounted {id} /
25
+ * toggled {id,disabled 双态} / updated {id,from?,to});
26
+ * 28→29:doors/updated 随开门制扩展批入册(05 §1.1 行 72——进程级开门位
27
+ * 授予面切换事实:doors 段〔模型道高危面授予载体,03 §4.6 双源并集律第二
28
+ * 源〕的审计腿,boot diff 幂等同 plugin/opens 律;载体 = audit_events);
29
+ * 29→31:hook/registered·kv/written 随 T9 案一批入册(05 §1.1 常规行为
30
+ * 归因两词——钩子监听者受理账与键值写历史;载体 = audit_events。前者
31
+ * 载荷 {pluginId, hook} 无 rank 位〔03 §2.4 ctx.on 两参签名恒定、注册序由
32
+ * 受理时序承载〕;后者载荷 {pluginId, key, action} 值与元数据恒不入、
33
+ * 发射位随 ctx.sessions 受理制写面批兑现〔规范已裁代码未落——词先锚定,
34
+ * compaction/fallback 同律〕)。
35
+ * 31→32:preset/applied 随审批分档批 ap-3 入册(05 §1.1 权限预设切换审计词
36
+ * ——载体 = audit_events;写点 = TUI /approval preset 执行尾恰一笔,CLI
37
+ * --preset 逐次形零审计——04 §9 ⑥ ap-3 定形补充)。
38
+ * 32→33:session/paused 随无人值守深化批 u-3 入册(05 §1.1 会话停靠词
39
+ * ——04 §5 停靠升格定形注②;载体 = 会话流;写入者 = 编排层停靠登记位
40
+ * 〔issue 悬置/goal 停靠——run 在飞时先 driver.abort() 协作中止、收口后
41
+ * 落词〕;fold 语义 = 尾条即停靠〔SessionLiveState 推导判据〕;恢复不设
42
+ * 对称词——唤醒消息落账尾条翻位即恢复,resumed 词不立)。
43
+ * 34→35:compaction/skip 随 obs-b 压缩判据观测批入册(05 §1.1 五门
44
+ * skip 词 + §2.1 判序定形注——fire 而被门挡才落:判序 = 阈值评估先行
45
+ * 〔below 不落——判据素材可后算,不为 below 防 durable 膨胀落账〕→
46
+ * no-channel〔首触一条后静默,与 warn-once 同锚〕→ pending → cooldown
47
+ * 〔+remainMs 窗余〕,锁内复评已不 fire = retracted〔幻影触发可查〕、
48
+ * 区间规划无合法段 = no-segment;basis 五件同 compaction/start〔fire
49
+ * 判据快照〕;写入者 = 宿主 compaction 件;载体 = 会话流)。
50
+ */
51
+ const CORE_EVENT_TYPES = [
52
+ {
53
+ type: 'turn/start',
54
+ category: 'structure',
55
+ owner: 'session',
56
+ tier: 'stable',
57
+ description: '一轮(用户输入 → 停止)开始',
58
+ },
59
+ {
60
+ type: 'turn/end',
61
+ category: 'structure',
62
+ owner: 'session',
63
+ tier: 'stable',
64
+ description: '一轮结束;reason = completed/aborted/blocked/error/max-tokens/interrupted(可扩展)',
65
+ },
66
+ {
67
+ type: 'user/message',
68
+ category: 'surface',
69
+ owner: 'session',
70
+ tier: 'stable',
71
+ description: '用户输入(string 或 text/image 块);source 归因词汇闭集见 types.ts',
72
+ },
73
+ {
74
+ type: 'assistant/message',
75
+ category: 'surface',
76
+ owner: 'session',
77
+ tier: 'stable',
78
+ description: '组装完成后的消息级事件;toolCall 块不内联 content(由 tool/call 唯一承载);errorMessage 腿独立 2KiB 小帽',
79
+ },
80
+ {
81
+ type: 'tool/call',
82
+ category: 'surface',
83
+ owner: 'session',
84
+ tier: 'stable',
85
+ description: '工具调用(arguments 存原始未解析字符串——审计保真)',
86
+ },
87
+ { type: 'tool/result', category: 'surface', owner: 'session', tier: 'stable', description: '一调用一结果' },
88
+ {
89
+ type: 'todo/write',
90
+ category: 'surface',
91
+ owner: 'conversation',
92
+ tier: 'stable',
93
+ description: '轮内清单全量快照(last-write-wins);fold = 日志倒扫最后一条 user/message 之后的最后一条本事件',
94
+ },
95
+ {
96
+ type: 'request/header',
97
+ category: 'snapshot',
98
+ owner: 'session',
99
+ tier: 'stable',
100
+ description: '完整请求信封快照;reason = initial/resume/change,重建请求取最后一条为基准',
101
+ },
102
+ {
103
+ type: 'session/end-seed',
104
+ category: 'structure',
105
+ owner: 'session',
106
+ tier: 'stable',
107
+ description: 'fork 种子边界标记(data 为空对象,边界即事件自身 seq)',
108
+ },
109
+ {
110
+ type: 'approval/asked',
111
+ category: 'log-only',
112
+ owner: 'safety',
113
+ tier: 'stable',
114
+ description: '审批提问(决策对完整内容;turn 内闭合可回放)',
115
+ },
116
+ {
117
+ type: 'approval/decided',
118
+ category: 'log-only',
119
+ owner: 'safety',
120
+ tier: 'stable',
121
+ description: '审批决策(决策对完整内容;turn 内闭合可回放)',
122
+ },
123
+ {
124
+ type: 'gate/decision',
125
+ category: 'log-only',
126
+ owner: 'tools',
127
+ tier: 'stable',
128
+ description: '守门段决策(toolCallId/decision(allow|block|mutate)/reason)——「守门不可绕」不变式的断言对象',
129
+ },
130
+ {
131
+ type: 'sandbox/mode',
132
+ category: 'log-only',
133
+ owner: 'safety',
134
+ tier: 'stable',
135
+ description: '会话级沙箱状态 = fold(events),append 即切换、重放即恢复(无独立配置存储)',
136
+ },
137
+ {
138
+ type: 'session/thinking-level',
139
+ category: 'log-only',
140
+ owner: 'conversation',
141
+ tier: 'stable',
142
+ description: '会话级思考档位 = fold(events),append 即切换、重放即恢复(与 sandbox/mode 同形态;04 §5「thinkingLevel 是会话态不是 run 态」的 durable 落账词;写入者 = conversation 件档位切换面)',
143
+ },
144
+ {
145
+ type: 'llm/usage',
146
+ category: 'log-only',
147
+ owner: 'llm',
148
+ tier: 'stable',
149
+ description: 'complete 单发补全通道的计量事实(token 原始值入账,货币折算在投影查询做)',
150
+ },
151
+ {
152
+ type: 'llm/retry',
153
+ category: 'log-only',
154
+ owner: 'session',
155
+ tier: 'stable',
156
+ description: 'turn 级 auto-retry 的 durable 事实(attempt/phase scheduled|aborted|exhausted;成功不落)',
157
+ },
158
+ {
159
+ type: 'plugin/uninstalled',
160
+ category: 'log-only',
161
+ owner: 'host',
162
+ tier: 'stable',
163
+ description: '卸载四段成功尾落账(id/source/dataAction/affected?;核心词身份拒装载面注册)',
164
+ },
165
+ {
166
+ type: 'plugin/opens',
167
+ category: 'log-only',
168
+ owner: 'host',
169
+ tier: 'stable',
170
+ description: '高危面开门授予面切换事实(boot 装载序对每插件 grantedOpens 与审计流尾最近一条本词 diff——有变才落幂等记账;撤位落 opens:[] 空数组形收口;载体 = 进程级 durable 审计流 audit_events〔05 §9〕非会话流,fold = 审计流尾条 = 该插件当前有效授予面)',
171
+ },
172
+ {
173
+ type: 'doors/updated',
174
+ category: 'log-only',
175
+ owner: 'host',
176
+ tier: 'stable',
177
+ description: '进程级开门位授予面切换事实(开门制扩展批 2026-09-09——doors 段〔模型道高危面授予载体,03 §4.6 双源并集律第二源〕的审计腿,与 plugin/opens 分立成对:行 opens 授予跟插件 id 走、doors 段授予跟进程走):boot 装载序以 doors 段现值与审计流尾最近一条本词 diff——有变才落幂等记账(空面首记不落),撤位落 doors:[] 空数组形收口;载荷 {doors 全量清单快照, origin(boot-diff|tui-cmd)};载体 = audit_events',
178
+ },
179
+ {
180
+ type: 'capability/used',
181
+ category: 'log-only',
182
+ owner: 'host',
183
+ tier: 'stable',
184
+ description: '高危面开门后的每次换装/注册使用事实(宿主门检接线位在门检通过、注册动词受理成功后落;载体 = 进程级 durable 审计流 audit_events〔05 §9〕;v1 射程 = channels.ui-backend + triggers.start-run 逐次 fire(triggerName 归因腿)+ credentials.read-cross 逐次越域读(core: 直开豁免照记——豁免免的是门不是账,c-3),sdk.register-route 随 U5)',
185
+ },
186
+ {
187
+ type: 'plugin/installed',
188
+ category: 'log-only',
189
+ owner: 'host',
190
+ tier: 'stable',
191
+ description: '生命周期归因面——装机成功事实(id/source/version;写点 = CLI 人面 install 成功尾;载体 = 进程级 durable 审计流 audit_events〔05 §9〕非会话流,05 §1.1 生命周期归因面行)',
192
+ },
193
+ {
194
+ type: 'plugin/mounted',
195
+ category: 'log-only',
196
+ owner: 'host',
197
+ tier: 'stable',
198
+ description: '生命周期归因面——挂载事实(id;写点两路 = CLI 人面 mount 成功尾 + boot 装载序手编 enabled.yaml 漂移 diff 补播;载体 = audit_events;core: 内置基线态不造首记噪声——diff 基线 core: 前缀按启用算)',
199
+ },
200
+ {
201
+ type: 'plugin/unmounted',
202
+ category: 'log-only',
203
+ owner: 'host',
204
+ tier: 'stable',
205
+ description: '生命周期归因面——卸下事实(id;装机保留行移除;写点两路同 mounted;载体 = audit_events——与 plugin/uninstalled 同面(装机面落码批已先行,CLI 人面无会话恒此载体);卸下/卸载两动词分立——卸下保留装机、卸载清算资产)',
206
+ },
207
+ {
208
+ type: 'plugin/toggled',
209
+ category: 'log-only',
210
+ owner: 'host',
211
+ tier: 'stable',
212
+ description: '生命周期归因面——禁用态翻转事实(id/disabled 双态 true|false——审计词形独立于 enabled.yaml 行形〔行内 absent 即启用、无 false 键〕,翻回启用需 false 位表达;写点两路 = CLI toggle 成功尾 + boot diff 补播;载体 = audit_events)',
213
+ },
214
+ {
215
+ type: 'plugin/updated',
216
+ category: 'log-only',
217
+ owner: 'host',
218
+ tier: 'stable',
219
+ description: '生命周期归因面——换装成功事实(id/from?/to——from 缺席 = 旧账本无版本位;local 源 no-op 不落〔无变更不造账〕;写点 = CLI 人面 update 成功尾;载体 = audit_events)',
220
+ },
221
+ {
222
+ type: 'compaction/start',
223
+ category: 'log-only',
224
+ owner: 'compaction',
225
+ tier: 'stable',
226
+ description: '压缩意图与判据快照(reason=threshold/overflow + willRetry + basis?;05 §2.1 五步之一)',
227
+ },
228
+ {
229
+ type: 'compaction/surface',
230
+ category: 'log-only',
231
+ owner: 'compaction',
232
+ tier: 'stable',
233
+ description: '遮蔽指令事件(信封 surfaceOp 载体本尊;data=summarySeq/规模审计;经 appendWithSurfaceOp 正门)',
234
+ },
235
+ {
236
+ type: 'compaction/end',
237
+ category: 'log-only',
238
+ owner: 'compaction',
239
+ tier: 'stable',
240
+ description: '压缩收尾标记(reason=completed/failed/vetoed/aborted;failed=摘要通道失败即时闭段防孤 start 悬挂;vetoed=钩子否决收形〔U4——start 后即 end 无摘要无遮蔽〕;aborted=§4 恢复协议合成形非 live 写入位)',
241
+ },
242
+ {
243
+ type: 'compaction/fallback',
244
+ category: 'log-only',
245
+ owner: 'compaction',
246
+ tier: 'stable',
247
+ description: '接管/算法槽失败回落事实(05 §2.1 回落三律第 3 律——回落非静默换算法;source=plugin:<id>、stage=throw|timeout|rejected〔抛错/超预算/产物被拒——空文本同宿主通道失败律〕、error? 截断、circuit?=true 末次记三振停用〔进程级,复位走重启〕;U4 调整途非法调整同 stage=rejected 记账)',
248
+ },
249
+ {
250
+ type: 'compaction/skip',
251
+ category: 'log-only',
252
+ owner: 'compaction',
253
+ tier: 'stable',
254
+ description: '压缩判据观测五门 skip(obs-b 2026-09-13——05 §1.1 行 82 + §2.1 判序定形注:fire 而被门挡才落,below 恒不落):gate=pending〔防重入挡〕|cooldown〔冷却窗挡,+remainMs 窗余〕|no-segment〔fire 但区间规划无合法段〕|retracted〔排队锁内复评已不 fire——幻影触发可查〕|no-channel〔摘要通道缺席——首触落一条后静默,与 warn-once 同锚〕;basis 五件同 compaction/start(fire 判据快照——触发时刻判据素材,非门检时刻);五门均不动冷却锚(skip 非 completed);写入者 = 宿主 compaction 件 handleRunSettled',
255
+ },
256
+ {
257
+ type: 'hook/registered',
258
+ category: 'log-only',
259
+ owner: 'host',
260
+ tier: 'stable',
261
+ description: '钩子监听者受理账(T9 案一批 2026-09-09——context_transform 瀑布等钩子注册全隐身的 durable 归因:「谁在改我的上下文」有账):载荷 {pluginId, hook}(无 rank——03 §2.4 ctx.on 两参签名恒定、注册序由受理时序承载);写点 = ctx.on 受理壳成功尾逐笔(waterfall/notify 两腿返回位;含 §2.1 回调窗延伸内的受理;拒路径零审计〔PLUGIN_HOOK_UNKNOWN/PLUGIN_WINDOW_CLOSED——被拒的注册不是行为〕);/reload 换代新受理新账(受理制非 boot diff——钩子注册无 fold 语义系行为流,每代如实记);载体 = audit_events',
262
+ },
263
+ {
264
+ type: 'kv/written',
265
+ category: 'log-only',
266
+ owner: 'host',
267
+ tier: 'stable',
268
+ description: '键值写历史(T9 案一批 2026-09-09——store_state LRU+ttl 只有终态、写无历史的 durable 归因):载荷 {pluginId, key 裸键名, action: set|delete}(值与 ttl/kind 元数据恒不入载荷——credentials/changed 同律;读腿零落账;persist 层 LRU/ttl 治理写零落账〔治理/受理分层〕);写点 = ctx.sessions store_state 受理面 set/delete 成功尾逐笔——**规范已裁、代码未落**(词先锚定,发射位随受理制写面落码批兑现,compaction/fallback 同律);载体 = audit_events',
269
+ },
270
+ {
271
+ type: 'preset/applied',
272
+ category: 'log-only',
273
+ owner: 'host',
274
+ tier: 'stable',
275
+ description: '权限预设切换审计(审批分档批 ap-3 2026-09-11——04 §9 定形块⑥「预设切换落审计」兑现:用户主权动作留痕,/danger approve 同形):载荷 {preset: conservative|balanced|open, sandboxMode, approvalPolicy, appended}(appended = 建议集去重后真追加数——balanced/conservative 恒 0);写点 = TUI /approval preset 执行尾恰一笔(写盘成功后;CLI --preset 逐次形零审计、失败路径零落账);载体 = audit_events',
276
+ },
277
+ {
278
+ type: 'credentials/changed',
279
+ category: 'log-only',
280
+ owner: 'host',
281
+ tier: 'stable',
282
+ description: '凭证治理记账(凭证代管批 c-1 立词 2026-09-08——05 §1.1 词条单源;ix-4 补注册:规范立词而核心词表漏行的 c 批真遗漏,audit 词汇闸 fail-loud 抓出):载荷 {namespace, name, action: add|remove|rotate, origin: human|oauth-flow}(值恒不入载荷——模型可见性铁律 03 §10.9);写点 = 宿主受理位(人面 /credentials add/rm + oauth 流写/轮换 + /plugins config 表单 secret 入盒——装配根单写者律);载体 = audit_events',
283
+ },
284
+ {
285
+ type: 'session/paused',
286
+ category: 'log-only',
287
+ owner: 'host',
288
+ tier: 'stable',
289
+ description: '会话停靠(无人值守深化批 u-3 2026-09-11——04 §5 停靠升格定形注②;05 §1.1 词行):载荷 {reason}(v1 单值 budget——预算语境停靠;触发形态非穷举:记账刹停/budgetExceeded 复验/起跑前池检拒/issue 悬置同词承载);写入者 = 编排层停靠登记位(run 在飞时先 driver.abort() 协作中止、收口后落词);fold 语义 = 尾条即停靠(SessionLiveState paused 档推导判据);恢复不设对称词——唤醒消息(user/message source=budget-extended)落账尾条翻位即恢复 running,resumed 词不立;载体 = 会话流',
290
+ },
291
+ ];
292
+ /** 核心词类型名清单(核心词保护判据——registerEventType 拒收) */
293
+ export const CORE_EVENT_TYPE_NAMES = CORE_EVENT_TYPES.map((m) => m.type);
294
+ /** 注册表本体(type → 目录条目);模块加载时灌入核心词 */
295
+ const registry = new Map();
296
+ for (const meta of CORE_EVENT_TYPES)
297
+ registry.set(meta.type, meta);
298
+ /**
299
+ * 注册事件类型(插件扩展唯一入口;宿主件自管类型已列核心表)。
300
+ *
301
+ * 两类拒收(均 fail-loud):
302
+ * - 核心词身份:SESSION_CORE_TYPE_FORBIDDEN(防装载面伪造宿主词汇——
303
+ * 与 session append 侧词汇检查同判据双闸);
304
+ * - 注册冲突:PLUGIN_EVENT_TYPE_CONFLICT(03 §2.7 指派——装载面统一
305
+ * 插件域码:core 件与用户插件同走 registerSessionEventType 动词,
306
+ * 撞名按动词域拒收不分调用方身份)。
307
+ */
308
+ export function registerEventType(meta) {
309
+ if (CORE_EVENT_TYPE_NAMES.includes(meta.type)) {
310
+ throw new BaseError('SESSION_CORE_TYPE_FORBIDDEN', `事件类型 ${meta.type} 是核心词汇——拒绝装载面注册/伪造(宿主身份词)`);
311
+ }
312
+ const existing = registry.get(meta.type);
313
+ if (existing) {
314
+ throw new BaseError('PLUGIN_EVENT_TYPE_CONFLICT', `事件类型 ${meta.type} 重复注册:${existing.owner}(彼) vs ${meta.owner}(此)`);
315
+ }
316
+ registry.set(meta.type, meta);
317
+ }
318
+ /** 判别事件类型是否已注册(session append 词汇检查的判据源) */
319
+ export function isKnownEventType(type) {
320
+ return registry.has(type);
321
+ }
322
+ /** 事件类型目录条目查询(未注册返回 undefined;类别/归属消费面用) */
323
+ export function getEventTypeMeta(type) {
324
+ return registry.get(type);
325
+ }
326
+ /** 全量目录枚举(生成目录 / CI 校验用) */
327
+ export function listEventTypes() {
328
+ return [...registry.values()];
329
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * contracts 模块公开面(L0 公共契约——模块间唯一共边;02 篇 §4.1)。
3
+ *
4
+ * 再导出面 = 错误码注册表 + 事件词汇注册表 + 会话事件信封类型 + LLM 边界
5
+ * 消息基础件(第五批)。工具/插件/子代理/Job 类型随对应模块落码批进本面。
6
+ */
7
+ export * from './errors.js';
8
+ export * from './agent-events.js';
9
+ export * from './ui.js';
10
+ /**
11
+ * api.ts 分桶收面(03 篇 §8.3 internal 行 + §8.4 公开根分桶——API 治理批 2):
12
+ * api.ts 顶层导出分两桶,本处只转出**可见桶**六名(四型 + 两纯函数——插件作者
13
+ * 可消费面);internal 机制桶八符号(VIRTUAL_API_KEYS / SERVICE_CATALOG /
14
+ * CAPABILITIES / API_ENFORCEMENT_IGNITED / adjudicateApiGate / ApiGateResult /
15
+ * assertExperimentalDeclared / materializeHostFace)不进公开根——它们是宿主
16
+ * 治理机制非插件 API,内核消费全深导 contracts/api.js。
17
+ * 本桶面即插件虚拟模块 `berry-agent` 的运行时面(loader 注入物),分桶 =
18
+ * 真实运行时收面(星出时代机制符号实测进面——内部重构将判伪 MAJOR)。
19
+ * 分桶不变式由抽取器 assertApiBucketPartition fail-loud 执法:api.ts 新顶层
20
+ * 导出未分桶即炸(白名单单点 tools/extract-api-surface.mjs INTERNAL_API_EXPORTS)。
21
+ * 类型四名走 export type(verbatimModuleSyntax 纪律——值面只含两纯函数)。
22
+ */
23
+ export type { ApiTier, ApiBlock, HostFace, HostFaceInput } from './api.js';
24
+ export { compareApiVersions, isValidApiVersion } from './api.js';
25
+ export * from './events.js';
26
+ export * from './types.js';
27
+ export * from './env-ref.js';
28
+ export * from './redact.js';
29
+ export * from './llm.js';
30
+ export * from './messages.js';
31
+ export * from './approval.js';
32
+ export * from './tools.js';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * contracts 模块公开面(L0 公共契约——模块间唯一共边;02 篇 §4.1)。
3
+ *
4
+ * 再导出面 = 错误码注册表 + 事件词汇注册表 + 会话事件信封类型 + LLM 边界
5
+ * 消息基础件(第五批)。工具/插件/子代理/Job 类型随对应模块落码批进本面。
6
+ */
7
+ export * from './errors.js';
8
+ // AgentEvent 活体事件族 + UI 通道后端契约族(2026-09-08 U3 落码批归位——
9
+ // 插件侧类型可达路径定形:虚拟主键 `berry-agent` 面只达 contracts,registerUiBackend
10
+ // 的 backend 形〔UiBackend<never>〕与 onEnvelope 活体流词汇经此两件可达;
11
+ // agent/channels 两侧公开面 re-export 维持不变〔批 11b ApprovalAsk 同款先例〕)
12
+ export * from './agent-events.js';
13
+ export * from './ui.js';
14
+ export { compareApiVersions, isValidApiVersion } from './api.js';
15
+ export * from './events.js';
16
+ export * from './types.js';
17
+ // 凭证 env 引用形词面单源(03 §10.9 注入腿——c-4):exec 执法位与 credentials
18
+ // resolver 两方消费,放公开根使两方零新 DAG 边(三名律——跨模块走公开面)
19
+ export * from './env-ref.js';
20
+ // 出口治理③ 凭据消毒纯函数族(04 §7 执行段 2026-09-08 落码定形):tools 管道
21
+ // 链尾(模式+值基两腿)与 agent 错误包装位(纯模式腿)两方消费,同律零新边
22
+ export * from './redact.js';
23
+ export * from './llm.js';
24
+ export * from './messages.js';
25
+ export * from './approval.js';
26
+ export * from './tools.js';
@@ -0,0 +1,275 @@
1
+ /**
2
+ * L0 contracts — LLM 边界消息基础件(04 篇 §3 StreamFn 契约 + §5 模型层)。
3
+ *
4
+ * 承 berry 同款关键决策:agent loop 不 import llm(02 篇 §4.2 关键不依赖第
5
+ * 一条),StreamFn 签名与消息形状收进 contracts——pi-ai 产生的消息值可原样
6
+ * 赋给这里的类型(标准三角色零转换直通)。
7
+ *
8
+ * 兼容策略:字段取 pi-ai 0.84.4 对应接口的**超集兼容子集**——本文件要求的
9
+ * 必填字段在 pi-ai 中全部存在且同名同型;pi-ai 独有的字段(api/responseModel/
10
+ * deferred 等)不在此收口,多出的字段对结构化赋值透明。
11
+ */
12
+ /** 文本块(user/assistant/toolResult 通用) */
13
+ export interface TextContent {
14
+ type: 'text';
15
+ text: string;
16
+ /** 供应商侧文本签名(回放用;透传) */
17
+ textSignature?: string;
18
+ }
19
+ /** 思考块(仅 assistant;供应商推理内容回放) */
20
+ export interface ThinkingContent {
21
+ type: 'thinking';
22
+ thinking: string;
23
+ /** 供应商不透明签名(推理上下文复用;透传) */
24
+ thinkingSignature?: string;
25
+ /** 安全过滤已遮蔽的思考(加密载荷存 thinkingSignature 透传回 API) */
26
+ redacted?: boolean;
27
+ }
28
+ /** 图片块(多模态附件;base64 数据) */
29
+ export interface ImageContent {
30
+ type: 'image';
31
+ data: string;
32
+ mimeType: string;
33
+ }
34
+ /** 工具调用块(仅 assistant 内联;arguments 已是解析后的对象) */
35
+ export interface ToolCallBlock {
36
+ type: 'toolCall';
37
+ id: string;
38
+ name: string;
39
+ arguments: Record<string, unknown>;
40
+ }
41
+ /**
42
+ * 一次 LLM 调用的 token 用量(pi-ai 同构):cacheRead/cacheWrite 独立拆桶,
43
+ * cacheWrite1h(仅 Anthropic 上报拆分)与 reasoning(已含于 output,供应商
44
+ * 不报则缺省)为可选子集。
45
+ */
46
+ export interface Usage {
47
+ input: number;
48
+ output: number;
49
+ cacheRead: number;
50
+ cacheWrite: number;
51
+ /** cacheWrite 中 1h 保留的子集(仅 Anthropic 上报拆分) */
52
+ cacheWrite1h?: number;
53
+ /** 推理 token 子集(已含于 output;供应商不报则缺省) */
54
+ reasoning?: number;
55
+ totalTokens: number;
56
+ /** 费用(供应商可解析时填充) */
57
+ cost?: {
58
+ total: number;
59
+ input?: number;
60
+ output?: number;
61
+ currency?: string;
62
+ };
63
+ }
64
+ /**
65
+ * 计量四桶形(05 篇 §1.1 llm/usage 事件载荷 usage 字段形):input/output/
66
+ * cacheRead/cacheWrite 四桶必落(供应商恒报),cacheWrite1h/reasoning 上报才落;
67
+ * totalTokens(派生)与 cost(折算)**不入账**——token 原始值入账、货币折算
68
+ * 在投影查询做(价格表更新不回改历史)。
69
+ */
70
+ export interface UsageBuckets {
71
+ input: number;
72
+ output: number;
73
+ cacheRead: number;
74
+ cacheWrite: number;
75
+ /** cacheWrite 中 1h 保留的子集(仅 Anthropic 上报拆分;上报才落) */
76
+ cacheWrite1h?: number;
77
+ /** 推理 token 子集(已含于 output;上报才落) */
78
+ reasoning?: number;
79
+ }
80
+ /**
81
+ * LLM 调用终态(pi-ai 同构七值)。
82
+ * loop 只消费 error/aborted(终态短路)与 length(截断防御);
83
+ * deferred 是 pi-ai 原生延迟工具装载透传值,v1 不产生但词汇保留。
84
+ */
85
+ export type StopReason = 'pending' | 'stop' | 'length' | 'toolUse' | 'error' | 'aborted' | 'deferred';
86
+ /**
87
+ * 错误四桶(04 §3.5 表)。词汇归 contracts 单源(批 11b 契约先行):唯一实现
88
+ * 在 llm(classifyError 纯函数表),conversation(runTurns 重试循环与溢出
89
+ * 兜底)经装配注入消费同形——两侧零词汇漂移(04 §3.5 消费通路条款)。
90
+ * transient/non-retryable/quota 是消费动作桶(每桶一个动作无歧义态),
91
+ * overflow 只分类不消费(动作挂溢出兜底纵切 04 §3.4)。
92
+ */
93
+ export type ErrorBucket = 'transient' | 'non-retryable' | 'quota' | 'overflow';
94
+ /**
95
+ * 用户消息(content 允许纯文本或图文块数组)。
96
+ * source 归因复用会话事件词汇闭集 EventSource(05 §3.1 单源——durable 落账
97
+ * 与 LLM 边界同一名;pi-ai UserMessage 不带此字段,多出字段对直通透明)。
98
+ */
99
+ export interface UserMessage {
100
+ role: 'user';
101
+ content: string | (TextContent | ImageContent)[];
102
+ /** Unix 毫秒时间戳 */
103
+ timestamp: number;
104
+ /** 输入归因(缺省视为 'user';投影带出、durable 原样落账) */
105
+ source?: import('./types.js').EventSource;
106
+ /**
107
+ * 幂等去重键(05 §1.1 user/message 行:调用方自选、可选字段——serve 面
108
+ * 幂等 admit 语义真源 05 §3.5;UI 侧不带零影响)。子代理审批挂起通知等
109
+ * 「恰一条」机器注入以本键承载去重判据(04 §10)。
110
+ */
111
+ dedupeKey?: string;
112
+ }
113
+ /** 助手消息(流式组装终值;stopReason=error/aborted 时错误即数据,见 AssistantStream) */
114
+ export interface AssistantMessage {
115
+ role: 'assistant';
116
+ content: (TextContent | ThinkingContent | ToolCallBlock)[];
117
+ usage: Usage;
118
+ stopReason: StopReason;
119
+ /** Unix 毫秒时间戳 */
120
+ timestamp: number;
121
+ /** 模型标识(provider 内模型 id;llm 模块解析) */
122
+ model?: string;
123
+ /** 供应商标识 */
124
+ provider?: string;
125
+ /** stopReason=error/aborted 时的错误说明(错误是数据契约之一) */
126
+ errorMessage?: string;
127
+ /**
128
+ * 错误码(04 §3.5 判定序的机器判定位):宿主合成错误携带 LLM_ 码(如
129
+ * LLM_INFLIGHT_LIMIT 帽拒绝);provider 真错误码归一挂 provider 钩子纵切。
130
+ * classifyError 判定时在场码优先、文案正则兜底。
131
+ */
132
+ errorCode?: string;
133
+ /** 脱敏诊断(恢复与审计用) */
134
+ diagnostics?: unknown[];
135
+ }
136
+ /** 工具结果消息(与 assistant 内 toolCall 按 toolCallId 配对) */
137
+ export interface ToolResultMessage {
138
+ role: 'toolResult';
139
+ toolCallId: string;
140
+ toolName: string;
141
+ content: (TextContent | ImageContent)[];
142
+ isError: boolean;
143
+ /** Unix 毫秒时间戳 */
144
+ timestamp: number;
145
+ /** 供日志/UI 的结构化明细(不进主上下文计费) */
146
+ details?: unknown;
147
+ /** 工具执行自身的用量(若可得上报;不进主上下文计费) */
148
+ usage?: Usage;
149
+ /** 本次结果后新可用的工具名(延迟装载透传) */
150
+ addedToolNames?: string[];
151
+ }
152
+ /** LLM 边界标准三角色(convertToLlm 透传的标准半边) */
153
+ export type Message = UserMessage | AssistantMessage | ToolResultMessage;
154
+ /** LLM 工具描述(parameters 为 JSON Schema——typebox 产物即此形状) */
155
+ export interface LlmTool {
156
+ name: string;
157
+ description: string;
158
+ /** JSON Schema 参数描述(typebox 产物或等价 JSON Schema 对象;宽收 object 兼容构建器类型) */
159
+ parameters: object;
160
+ }
161
+ /** 单次 LLM 请求上下文(StreamFn 第一参数;04 §3.1) */
162
+ export interface LlmContext {
163
+ systemPrompt?: string;
164
+ messages: Message[];
165
+ tools?: LlmTool[];
166
+ }
167
+ /**
168
+ * 模型目录只读投影(listModels()/getModel() 返回形):pi-ai Model 的应用友好
169
+ * 子集——枚举/展示/能力判别所需字段直通,传输与 provider 配置面(baseUrl/
170
+ * headers/samplingParams/compat)不披露(宿主数据只经服务面,03 篇 §3.2
171
+ * 「防双实例」纪律的读侧配套)。
172
+ */
173
+ export interface ModelInfo {
174
+ /** 模型标识("provider/model-id" 全形——resolveModel 可解析的同一名) */
175
+ readonly id: string;
176
+ /** 展示名(人类可读) */
177
+ readonly name: string;
178
+ /** 所属 provider id(目录分组用) */
179
+ readonly provider: string;
180
+ /** 是否推理模型(思考档位面可用性判据) */
181
+ readonly reasoning: boolean;
182
+ /** 输入模态清单 */
183
+ readonly input: readonly ('text' | 'image')[];
184
+ /** 上下文窗口(tokens) */
185
+ readonly contextWindow: number;
186
+ /** 单请求输出上限(tokens) */
187
+ readonly maxTokens: number;
188
+ }
189
+ /** 思考档位(pi-ai 同构七值;xhigh/max 仅部分模型家族支持——会话态非 run 态,04 §5) */
190
+ export type ThinkingLevel = 'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max';
191
+ /** StreamFn 每次调用的选项(model 为模型 id 字符串——解析归 llm 模块,loop 不持模型对象) */
192
+ export interface StreamFnOptions {
193
+ /** 模型 id(llm 模块解析为具体 provider/model;可逐轮替换) */
194
+ model: string;
195
+ thinkingLevel?: ThinkingLevel;
196
+ /** 凭证(缺省 undefined 由 llm 层走持久化凭证链) */
197
+ apiKey?: string;
198
+ }
199
+ /**
200
+ * 模型层注入签名(llm 模块整体替换位——agent 与 llm 两模块的唯一会合点,故落在
201
+ * contracts)。契约:**永不抛错**(04 §3)——一切失败编码为返回流的 error 终止
202
+ * 事件 + 最终消息 stopReason='error'|'aborted' + errorMessage(AssistantStream
203
+ * 契约注释)。这是 loop 零 try/catch 的契约根基:错误是数据不是异常。
204
+ */
205
+ export type StreamFn = (context: LlmContext, options: StreamFnOptions, signal?: AbortSignal) => AssistantStream | Promise<AssistantStream>;
206
+ /**
207
+ * 流式事件(pi-ai AssistantMessageEvent 同构 12 型)。
208
+ * 协议:`start` 先行 → 各内容块 start/delta/end 交错(partial 携带累计快照)→
209
+ * 以 `done`(成功)或 `error`(stopReason=error/aborted)收尾。
210
+ */
211
+ export type AssistantStreamEvent = {
212
+ type: 'start';
213
+ partial: AssistantMessage;
214
+ } | {
215
+ type: 'text_start';
216
+ contentIndex: number;
217
+ partial: AssistantMessage;
218
+ } | {
219
+ type: 'text_delta';
220
+ contentIndex: number;
221
+ delta: string;
222
+ partial: AssistantMessage;
223
+ } | {
224
+ type: 'text_end';
225
+ contentIndex: number;
226
+ content: string;
227
+ partial: AssistantMessage;
228
+ } | {
229
+ type: 'thinking_start';
230
+ contentIndex: number;
231
+ partial: AssistantMessage;
232
+ } | {
233
+ type: 'thinking_delta';
234
+ contentIndex: number;
235
+ delta: string;
236
+ partial: AssistantMessage;
237
+ } | {
238
+ type: 'thinking_end';
239
+ contentIndex: number;
240
+ partial: AssistantMessage;
241
+ } | {
242
+ type: 'toolcall_start';
243
+ contentIndex: number;
244
+ partial: AssistantMessage;
245
+ } | {
246
+ type: 'toolcall_delta';
247
+ contentIndex: number;
248
+ delta: string;
249
+ partial: AssistantMessage;
250
+ } | {
251
+ type: 'toolcall_end';
252
+ contentIndex: number;
253
+ toolCall: ToolCallBlock;
254
+ partial: AssistantMessage;
255
+ } | {
256
+ type: 'done';
257
+ reason: Extract<StopReason, 'stop' | 'length' | 'toolUse' | 'deferred'>;
258
+ message: AssistantMessage;
259
+ } | {
260
+ type: 'error';
261
+ reason: Extract<StopReason, 'aborted' | 'error'>;
262
+ error: AssistantMessage;
263
+ };
264
+ /**
265
+ * 流式响应:异步迭代事件 + 最终消息取值口。
266
+ * 契约(04 §3「永不抛」,loop 零 try/catch 的根基):**永不抛错**——一切失败
267
+ * 编码为流内 `error` 终止事件 + 最终 AssistantMessage 的 stopReason='error'|
268
+ * 'aborted' + errorMessage。错误是数据不是异常(金样回放轨的替换点 seam)。
269
+ */
270
+ export interface AssistantStream {
271
+ /** 迭代流事件(以 done/error 收尾后结束) */
272
+ [Symbol.asyncIterator](): AsyncIterator<AssistantStreamEvent>;
273
+ /** 取最终 AssistantMessage(流耗尽后 resolve;失败编码在 stopReason/errorMessage 上) */
274
+ result(): Promise<AssistantMessage>;
275
+ }