An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:memo: Correct endpoint contract documentation https://github.com/siyuan-note/siyuan/pull/21037
Daniel2572ee2be09d
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.tsindex ce6e5e9e89..3c02412cb7 100644--- a/app/src/types/api/index.d.ts+++ b/app/src/types/api/index.d.ts@@ -4908,12 +4908,9 @@ export interface APIPOSTRoutes {authorization?: readonly ["authenticated","admin","writable"];};/**- * 编辑器通过独立事务转换块引用:id 指定引用块,blockID 指定定义块,data 携带布尔选项 includeChildren- * 和 originalToEmbed。内核在 retData 中返回受影响的文档 ID,并以受影响顶层块的私有内存快照生成逆向操作;- * 快照不传给客户端,也不接受客户端提交。重放保留块 ID 和数据库绑定,内容或位置已改变时拒绝覆盖,- * originalToEmbed 省略或为 false 时在原定义位置保留引用,为 true 时替换为查询已移动定义 ID 的嵌入块;- * includeChildren 保留标题和列表行为。- * 提交失败时补偿已写入的文档。撤销与重放期间为所有受影响文档持有笔记本租约,直至响应序列化完成。+ * refID 指定引用块,defID 指定定义块;includeChildren 控制标题或列表的子块范围。+ * originalToEmbed 省略或为 false 时在原定义位置保留引用,为 true 时替换为查询已移动定义 ID 的嵌入块。+ * 成功响应的 data 为 null;可撤销的编辑器操作使用 /api/transactions 的 swapBlockRef 动作。*/"/api/block/swapBlockRef": {request: SwapBlockRefRequestInput;@@ -5515,6 +5512,7 @@ export interface APIPOSTRoutes {};/*** 将单篇文档及全部子文档复制到同一笔记本的原父目录,返回副本根文档的 id、notebook、path 和 hPath。+ * 为副本文档和块执行块收集规则,仅初始化新行。* 副本内部块引用、块链接和查询嵌入的显式块 ID 统一重映射,数据库定义与行绑定保持为共享镜像。* 源快照经过认证读取且不执行修复写回;运行期失败时清理新建文档并恢复排序,属运行时补偿,* 不保证进程崩溃时的事务原子性。@@ -8695,6 +8693,10 @@ export interface APIPOSTRoutes {* 字段失效或目标数据库被删除时,后续条目写入停用规则并保留配置,源修改仍提交;提交后提示失效引用。* 修复后需手动启用,撤销源修改不重新启用规则;锁定、认证失败、损坏及实际写入失败仍一起回滚。* 跨库动作限于同一加密边界,要求目标可访问;单笔事务最多执行 1000 个自动操作。+ * swapBlockRef 必须独立提交:id 指定引用块,blockID 指定定义块,data 携带 includeChildren 和 originalToEmbed,+ * retData 返回受影响的文档 ID。逆向操作使用受影响顶层块的私有内存快照;快照不传给客户端,也不接受客户端提交。+ * 重放保留块 ID 和数据库绑定,内容或位置已改变时拒绝覆盖;提交失败时补偿已写入的文档。+ * 撤销与重放为所有受影响文档持有笔记本租约,直至响应序列化完成。*/"/api/transactions": {request: PerformTransactionsRequestInput;diff --git a/docs/API-CONTRACTS.md b/docs/API-CONTRACTS.mdindex 4987f83286..ec0a0bbd05 100644--- a/docs/API-CONTRACTS.md+++ b/docs/API-CONTRACTS.md@@ -22,7 +22,7 @@ Generation checks inspect actual route and handler declarations to verify methodFor JSON routes subject to `ControlConcurrency`, requests pass authorization and admission checks, then `contractHandler` completes decoding and drains the remaining request body before acquiring the per-path business lock. This prevents queued bodies from exhausting an HTTP/2 connection's receive window. The outer middleware releases the lock after response handling and notebook lease cleanup. Preserve this ordering when adding adapters; raw streams and form uploads retain their existing concurrency paths.-Module files under `kernel/apicontract/` define requests, responses, and endpoints; `contracts.go` collects them in the endpoint registry. The contract package is independent of kernel startup, databases, and persistence models, so the generator runs independently. API entry points bind endpoints through `contractHandler`; Go generic signatures constrain request parameters and successful return values. Constructors set response payloads rather than assigning directly to generic `ret.Data`. Existing helpers continue to validate business rules, and `contractFailure` preserves their error codes, messages, and supported error payloads.+Module files under `kernel/apicontract/` define requests, responses, and endpoints; `contracts.go` collects them in the endpoint registry. The contract package is independent of kernel startup, databases, and persistence models, so the generator runs independently. API entry points bind endpoints through `contractHandler`; Go generic signatures constrain request parameters and successful return values. Constructors set response payloads rather than assigning directly to generic `ret.Data`. Validation helpers return errors; `FailureFrom` preserves the code and message of a `Rejection` and maps other errors to code `-1`. Use endpoint constructors for supported error payloads and `SuccessWithoutData` for success responses with null data.The generator produces `app/src/types/api/index.d.ts` and `kernel/apicontract/schema.json` from the same Go types. The schema contains shared `$defs` and each endpoint's request and response schemas. Tests validate actual HTTP responses against those same schemas. Type declarations do not validate JSON at runtime; handler tests in CI validate serialized results.@@ -54,9 +54,9 @@ Plugin private services declare their existing serialization modes, raw files, rNetwork contracts retain request bytes, multipart fields, headers, URL and TLS diagnostics, including complete certificate public-key structures and large integers. Forward proxy options preserve validation order, numeric truncation, response encodings, and protocol-defined JSON payloads. HTTP, EventSource, and WebSocket proxies retain upstream statuses and bytes, repeated-header behavior, security headers, stream cancellation, and connection cleanup. Echo wildcard paths bind their own adapter while sharing the same handler behavior.-Attribute-view contracts declare table, gallery, and kanban result shapes and retain missing base fields, cell patches, and null values. The independent `list` layout uses the table-shaped `columns`/`rows`/`rowCount` payload and stores its layout settings under `list`; `viewType` identifies it as `list`. Known patch fields retain their supplied presence without populating omitted fields with zero values. Current, history, and snapshot rendering use their corresponding request types; callers handle error payloads before updating views. Row sorting, publish admission, encrypted-notebook leases, and fast JSON rendering preserve their existing behavior. Shared frontend and plugin declarations reflect the fields actually present in each view and cell. These tests are included in the full kernel CI suite.+Attribute-view contracts declare table, gallery, and kanban result shapes and retain missing base fields, cell patches, and null values. The independent `list` layout uses the table-shaped `columns`/`rows`/`rowCount` payload and stores its layout settings under `list`; `viewType` identifies it as `list`. Known patch fields retain their supplied presence without populating omitted fields with zero values. Current, history, and snapshot rendering use their corresponding request types; callers handle error payloads before updating views. Row sorting, publish admission, encrypted-notebook leases, and fast JSON rendering preserve their existing behavior. Shared frontend and plugin declarations reflect the fields actually present in each view and cell.-Publish readers of attribute-view rendering endpoints use a non-persistent rendering path. Requests retain their existing schemas, carrier authorization, content filtering, and notebook leases; even `createIfNotExist: true` cannot create a missing database for a reader. Compatibility normalization, group generation, and missing-field corrections remain in memory. Frontend session-state and DOM regressions run through `node --import tsx --test src/protyle/render/av/publishState.test.ts tests/publishReadonlyFold.test.js` and the full frontend CI suite.+Publish readers of attribute-view rendering endpoints use a non-persistent rendering path. Requests retain their existing schemas, carrier authorization, content filtering, and notebook leases; even `createIfNotExist: true` cannot create a missing database for a reader. Compatibility normalization, group generation, and missing-field corrections remain in memory.AI contracts retain provider configuration, model discovery and matching, confirmation decisions, session extensions, and numeric and omission semantics. Editor and agent streams declare their actual SSE events; disconnection closes upstream requests, and stream lifetimes remain within the request. OAuth pages retain their media types, HTTP statuses, and security headers. Session permission notifications use `WithAfterWrite` to preserve response-before-broadcast ordering. Arbitrary JSON remains limited to protocol extension fields and tool results.@@ -90,7 +90,7 @@ Plugin publishing contracts separate administrator-only grants and snapshot writSearch contracts retain pagination defaults and numeric truncation, path validation and deduplication, ignored historical subtype filters, and null versus empty arrays. Reference search distinguishes correlation-only responses from block results and retains notebook admission before deferred parameter validation. SQL search authorization, publish filtering, encrypted notebook leases, cancellation responses, and read-only embed-update no-ops remain in their original order. Desktop and mobile callers share generated request types.-Custom block search uses the optional `customBlock` type filter. Explicit API type maps select only their enabled types; omitted type maps use search settings, which enable custom blocks by default. Saved criteria preserve omission separately from `false` so older frontend search configurations can inherit the setting. All are included in the full kernel command below.+Custom block search uses the optional `customBlock` type filter. Explicit API type maps select only their enabled types; omitted type maps use search settings, which enable custom blocks by default. Saved criteria preserve omission separately from `false` so older frontend search configurations can inherit the setting.History contracts retain path trimming, optional highlight defaults, fractional history-type truncation, and null versus empty result arrays. Version comparison checks both reference objects before their fields and acquires notebook leases in sorted order. Content reads and document, asset, and attribute-view rollbacks retain their history-path lease checks; notebook rollback keeps its existing model-level recovery behavior.diff --git a/docs/API-CONTRACTS.zh-CN.md b/docs/API-CONTRACTS.zh-CN.mdindex 761dc89be3..a7c570d899 100644--- a/docs/API-CONTRACTS.zh-CN.md+++ b/docs/API-CONTRACTS.zh-CN.md@@ -22,7 +22,7 @@对于受 `ControlConcurrency` 控制的 JSON 接口,请求先通过鉴权和准入检查,再由 `contractHandler` 完成解码并接收剩余请求体,之后才获取按路径划分的业务锁,避免排队请求耗尽 HTTP/2 连接的接收窗口。外层中间件在响应处理和笔记本租约清理完成后释放锁。新增适配器必须保持这一顺序;原始数据流和表单上传保留既有的并发控制路径。-`kernel/apicontract/` 下的各模块文件定义请求、响应和端点,`contracts.go` 将其汇总到端点注册表。契约包独立于内核启动、数据库和持久化模型,因此生成器可以单独运行。API 入口通过 `contractHandler` 绑定端点,请求参数和成功返回值受到 Go 泛型签名约束;响应载荷通过构造函数设置,不能直接给通用 `ret.Data` 赋值。业务校验继续使用现有辅助函数,`contractFailure` 保留其错误码、消息和已支持的错误载荷。+`kernel/apicontract/` 下的各模块文件定义请求、响应和端点,`contracts.go` 将其汇总到端点注册表。契约包独立于内核启动、数据库和持久化模型,因此生成器可以单独运行。API 入口通过 `contractHandler` 绑定端点,请求参数和成功返回值受到 Go 泛型签名约束;响应载荷通过构造函数设置,不能直接给通用 `ret.Data` 赋值。校验辅助函数返回错误;`FailureFrom` 保留 `Rejection` 的错误码和消息,其他错误使用 `-1`。支持的错误载荷通过端点构造函数设置,数据为 null 的成功响应使用 `SuccessWithoutData`。生成器从同一组 Go 类型生成 `app/src/types/api/index.d.ts` 和 `kernel/apicontract/schema.json`。后者包含共享的 `$defs` 和每个端点的请求、响应 schema,测试使用同一套 schema 检查实际 HTTP 响应。类型声明不会在运行时验证 JSON,CI 中的处理函数测试负责验证序列化结果。@@ -54,9 +54,9 @@网络契约保留请求字节、表单字段、请求头、URL 和 TLS 诊断信息,包括完整证书公钥结构与大整数。转发代理选项保留校验顺序、数字截断、响应编码及协议定义的 JSON 载荷。HTTP、EventSource 和 WebSocket 代理保留上游状态与字节、重复请求头行为、安全响应头、流取消和连接清理。回显通配路径绑定独立适配器,并共用相同业务处理。-数据库契约分别声明表格、画廊和看板的结果结构,保留基础字段缺失、单元格局部修改及 null 值。独立的 `list` 布局复用表格的 `columns`/`rows`/`rowCount` 载荷,布局配置保存在 `list` 下,`viewType` 标识为 `list`。已知修改字段保留传入状态,不以零值填充未提供的字段。当前、历史和快照渲染使用对应请求类型,调用端先处理错误载荷再更新视图。行排序、发布准入、加密笔记本租约和快速 JSON 渲染保留既有行为。前端和插件的共享声明按视图及单元格实际提供的字段同步。这些测试已包含在内核完整 CI 测试中。+数据库契约分别声明表格、画廊和看板的结果结构,保留基础字段缺失、单元格局部修改及 null 值。独立的 `list` 布局复用表格的 `columns`/`rows`/`rowCount` 载荷,布局配置保存在 `list` 下,`viewType` 标识为 `list`。已知修改字段保留传入状态,不以零值填充未提供的字段。当前、历史和快照渲染使用对应请求类型,调用端先处理错误载荷再更新视图。行排序、发布准入、加密笔记本租约和快速 JSON 渲染保留既有行为。前端和插件的共享声明按视图及单元格实际提供的字段同步。-数据库渲染接口对发布阅读者使用不落盘的渲染路径。请求保留现有模式、载体授权、内容过滤及笔记本租约;即使传入 `createIfNotExist: true`,也不能为阅读者创建缺失的数据库。兼容性归一化、分组生成和缺失字段修正均仅在内存中完成。前端会话状态和 DOM 回归通过 `node --import tsx --test src/protyle/render/av/publishState.test.ts tests/publishReadonlyFold.test.js` 及前端全量持续集成测试执行。+数据库渲染接口对发布阅读者使用不落盘的渲染路径。请求保留现有模式、载体授权、内容过滤及笔记本租约;即使传入 `createIfNotExist: true`,也不能为阅读者创建缺失的数据库。兼容性归一化、分组生成和缺失字段修正均仅在内存中完成。AI 契约保留供应商配置、模型发现与匹配、确认结果、会话扩展字段,以及数值和省略语义。编辑器和智能体流声明实际 SSE 事件,断开连接时关闭上游请求,流生命周期仍位于请求内部。OAuth 页面保留媒体类型、HTTP 状态和安全响应头。会话权限通知使用 `WithAfterWrite`,保留先响应再广播的顺序。任意 JSON 仅用于协议扩展字段和工具结果。diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.goindex 82d73b1812..dfdbce023e 100644--- a/kernel/apicontract/contracts.go+++ b/kernel/apicontract/contracts.go@@ -434,12 +434,9 @@ var GetTreeStat = define[TreeStatRequest, TreeStatData]("getTreeStat", "/api/blovar TransferBlockRef = define[TransferBlockRefRequest, Null]("transferBlockRef", "/api/block/transferBlockRef", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")-// 编辑器通过独立事务转换块引用:id 指定引用块,blockID 指定定义块,data 携带布尔选项 includeChildren-// 和 originalToEmbed。内核在 retData 中返回受影响的文档 ID,并以受影响顶层块的私有内存快照生成逆向操作;-// 快照不传给客户端,也不接受客户端提交。重放保留块 ID 和数据库绑定,内容或位置已改变时拒绝覆盖,-// originalToEmbed 省略或为 false 时在原定义位置保留引用,为 true 时替换为查询已移动定义 ID 的嵌入块;-// includeChildren 保留标题和列表行为。-// 提交失败时补偿已写入的文档。撤销与重放期间为所有受影响文档持有笔记本租约,直至响应序列化完成。+// refID 指定引用块,defID 指定定义块;includeChildren 控制标题或列表的子块范围。+// originalToEmbed 省略或为 false 时在原定义位置保留引用,为 true 时替换为查询已移动定义 ID 的嵌入块。+// 成功响应的 data 为 null;可撤销的编辑器操作使用 /api/transactions 的 swapBlockRef 动作。var SwapBlockRef = define[SwapBlockRefRequest, Null]("swapBlockRef", "/api/block/swapBlockRef", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")var SetBlockReminder = define[BlockReminderRequest, Null]("setBlockReminder", "/api/block/setBlockReminder", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")@@ -1189,6 +1186,7 @@ var RenameDocByID = define[FileTreeRenameIDRequest, Null]("renameDocByID", "/apivar DuplicateDoc = define[FileTreeIDRequest, FileTreeDuplicateData]("duplicateDoc", "/api/filetree/duplicateDoc", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")// 将单篇文档及全部子文档复制到同一笔记本的原父目录,返回副本根文档的 id、notebook、path 和 hPath。+// 为副本文档和块执行块收集规则,仅初始化新行。// 副本内部块引用、块链接和查询嵌入的显式块 ID 统一重映射,数据库定义与行绑定保持为共享镜像。// 源快照经过认证读取且不执行修复写回;运行期失败时清理新建文档并恢复排序,属运行时补偿,// 不保证进程崩溃时的事务原子性。@@ -1841,6 +1839,10 @@ var SystemAddUIProcess = define[EmptyRequest, Null]("addUIProcess", "/api/system// 字段失效或目标数据库被删除时,后续条目写入停用规则并保留配置,源修改仍提交;提交后提示失效引用。// 修复后需手动启用,撤销源修改不重新启用规则;锁定、认证失败、损坏及实际写入失败仍一起回滚。// 跨库动作限于同一加密边界,要求目标可访问;单笔事务最多执行 1000 个自动操作。+// swapBlockRef 必须独立提交:id 指定引用块,blockID 指定定义块,data 携带 includeChildren 和 originalToEmbed,+// retData 返回受影响的文档 ID。逆向操作使用受影响顶层块的私有内存快照;快照不传给客户端,也不接受客户端提交。+// 重放保留块 ID 和数据库绑定,内容或位置已改变时拒绝覆盖;提交失败时补偿已写入的文档。+// 撤销与重放为所有受影响文档持有笔记本租约,直至响应序列化完成。var PerformTransactions = define[PerformTransactionsRequest, []*Transaction]("performTransactions", "/api/transactions", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")var UndoState = define[TransactionUndoStateRequest, TransactionUndoState]("undoState", "/api/transactions/undoState", AuthenticatedAccess, JSONBody, ResponseOptions{}, "POST")var PerformUndo = define[TransactionHistoryRequest, TransactionHistoryResult]("performUndo", "/api/transactions/undo", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")