Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:memo: Clarify API contract maintenance workflow https://github.com/siyuan-note/siyuan/issues/19378

Daniel08ea37d5be8f
diff --git a/docs/API-CONTRACTS.md b/docs/API-CONTRACTS.md
index 17443ebbf4..1134b37c97 100644
--- a/docs/API-CONTRACTS.md
+++ b/docs/API-CONTRACTS.md
@@ -4,11 +4,21 @@
## Scope
-This document defines type declarations, compatibility requirements, generated artifacts, and verification for kernel HTTP APIs. Endpoint definitions are maintained in `kernel/apicontract/contracts.go`. The migration is complete and `kernel/apicontract/legacy_routes.json` is empty. New endpoints must define type contracts and must not be added to the legacy list.
+This document defines type declarations, compatibility requirements, generated artifacts, and verification for kernel HTTP APIs. Transport types and endpoint definitions are maintained in the module files under `kernel/apicontract/`; `contracts.go` collects the endpoint registry. The migration is complete and `kernel/apicontract/legacy_routes.json` is empty. New endpoints must define type contracts and must not be added to the legacy list.
+
+## Endpoint maintenance
+
+1. Define or update transport types and endpoints in the contract package, specifying request bodies, error codes, null values, defaults, and historical input compatibility
+2. Bind business entry points through `contractHandler`, preserving route middleware order, authorization, and lease scope
+3. Keep `legacy_routes.json` empty; when deleting an endpoint, remove both its route registration and contract definition
+4. Update actual-response, input-compatibility, and strict type tests; run generation and correct calls identified by the compiler
+5. Synchronize generated and related public declarations in `petal`; update API documentation for public endpoints
+
+Generation checks inspect actual route and handler declarations to verify methods, paths, handlers, and contract adapters. CI compares the legacy list with the pre-change list and prevents additional records. Do not bypass contract checks with `any`, type assertions, or changes to the legacy list.
## Contracts and implementation
-`kernel/apicontract/contracts.go` defines requests, responses, and endpoints. 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`. Existing helpers continue to validate business rules, and `contractFailure` preserves their error codes, messages, and supported error payloads.
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.
@@ -84,11 +94,11 @@ Endpoint-specific behavior is recorded jointly in contract definitions, compatib
`ignoretype` and `filterstrings` apply only to explicitly declared historical parameter compatibility. Generated request types describe canonical calls; compatibility decoding may accept and ignore a wider set of old inputs, with tests covering those exceptions. There is no global switch to fall back to old parsing after binding fails.
-Read-only middleware may still return a prompt object containing `closeTimeout`. Ordinary `fetchPost` callbacks receive only nonnegative codes retained after message processing; block-info code `3` still requires handling. `fetchSyncPost` and `fetchGet` preserve complete responses. Dynamic URLs retain existing signatures. Static POST paths must come from contracts or recorded legacy routes, and invalid parameters cannot fall back through another overload. Use an explicit `string` variable when constructing a template URL with an open-ended range.
+Read-only middleware may still return a prompt object containing `closeTimeout`. Ordinary `fetchPost` callbacks receive only nonnegative codes retained after message processing; block-info code `3` still requires handling. `fetchSyncPost` and `fetchGet` preserve complete responses. Dynamic URLs retain existing signatures. Static POST paths must come from contracts, and invalid parameters cannot fall back through another overload. Use an explicit `string` variable when constructing a template URL with an open-ended range.
Use `FailureWithTimeout` when a business error must preserve its message display duration. Contract-based block queries use `holdContractBlockRequest` to retain lease checks for explicit notebooks and accompanying IDs. Individual entry points still specify whether state queries permit deleted IDs.
-`StructJSONBody` is reserved for endpoints that already use Go JSON struct binding. It preserves case-insensitive field matching, null handling, and parser errors; required business fields are validated by the handler. It must not be used to relax a migrated endpoint's request rules. Endpoints that return their result payload on failure explicitly set `DataOnError` and use the endpoint's typed `FailureWithData` method.
+`StructJSONBody` is reserved for endpoints that already use Go JSON struct binding. It preserves case-insensitive field matching, null handling, and parser errors; required business fields are validated by the handler. It must not be used to relax an existing endpoint's request rules. Endpoints that return their result payload on failure explicitly set `DataOnError` and use the endpoint's typed `FailureWithData` method.
Notebook configuration updates use a typed partial object. The `legacyobject` field option preserves the existing JSON round-trip's numeric normalization and case-insensitive struct binding; optional pointer fields leave existing values unchanged when omitted or null. Encryption fields are decoded for input compatibility but never applied by the configuration patch. `Base64Bytes` explicitly models historical byte-slice inputs as Base64 strings or byte arrays.
@@ -124,33 +134,22 @@ Use `MultipartBody` for file uploads. Request structs declare string fields and
Fixed fields declared as `[]*multipart.FileHeader` receive all files in their original order and generate `Array<Blob>`. An optional absent file list remains nil. Text and single-file fields still select the first value. `SuccessWithMessage` retains nonempty messages on successful responses, including partial batch uploads.
-Frontend callers construct `ContractFormData` from typed fields before passing it to the existing fetch functions. The generated signatures require the endpoint's fields and distinguish file values from strings; raw `FormData` cannot satisfy a migrated upload contract. Optional fields are omitted and string values are not trimmed. Plugin callers can implement the generated `APIFormData<Request>` interface when constructing their forms.
-
-## Endpoint maintenance
+Frontend callers construct `ContractFormData` from typed fields before passing it to the existing fetch functions. The generated signatures require the endpoint's fields and distinguish file values from strings; raw `FormData` cannot satisfy an upload contract. Optional fields are omitted and string values are not trimmed. Plugin callers can implement the generated `APIFormData<Request>` interface when constructing their forms.
Dynamic multipart endpoints use `MultipartFields` to retain every text value and file under each field name. Its request schema maps field names to arrays of text or binary values; `ContractFormData` appends each array item as a repeated form field. This is distinct from fixed-field uploads, which continue to bind the first value. Broadcast publication preserves text-before-file processing and its per-message error results. Endpoint-specific `DecodeFailure` handling preserves existing parsing error codes and payloads.
-1. Define or update transport types and endpoints in the contract package, specifying request bodies, error codes, null values, defaults, and historical input compatibility
-2. Bind business entry points through `contractHandler`, preserving route middleware order, authorization, and lease scope
-3. Routes with type contracts must not also appear in `legacy_routes.json`; remove records for deleted endpoints, and never add new endpoints to the list
-4. Update actual-response, input-compatibility, and strict type tests; run generation and correct calls identified by the compiler
-5. Synchronize generated and related public declarations in `petal`; update API documentation for public endpoints
-
-Generation checks inspect actual route and handler declarations to verify methods, paths, handlers, and contract adapters. CI compares the legacy list with the pre-change list and prevents additional records. Do not bypass contract checks with `any`, type assertions, or changes to the legacy list.
-
## Generation and verification
Run from `app/`:
```text
-pnpm run api:generate
pnpm run api:generate --petal ../../petal
pnpm run api:check --petal ../../petal
pnpm run lint
pnpm exec tsx --test src/util/fetch.test.ts src/util/fetchTimeout.test.ts src/util/contractFormData.test.ts src/config/systemConfig.test.ts src/util/keymapBindings.test.ts src/config/tabs/cloudUser.test.ts src/protyle/util/transactionContract.test.ts
```
-The `--petal` path is relative to the generator's working directory, `kernel/`; the example refers to a sibling repository. CI checks only this repository's artifacts. Local synchronization across repositories uses this option to verify plugin declarations.
+The generation command updates both this repository and `petal`; a separate generation run without `--petal` is unnecessary. The `--petal` path is relative to the generator's working directory, `kernel/`; the example refers to a sibling repository. CI checks only this repository's artifacts. Local synchronization across repositories uses this option to verify plugin declarations.
Run from `kernel/`:
diff --git a/docs/API-CONTRACTS.zh-CN.md b/docs/API-CONTRACTS.zh-CN.md
index d371c8fef8..6246b3fa7e 100644
--- a/docs/API-CONTRACTS.zh-CN.md
+++ b/docs/API-CONTRACTS.zh-CN.md
@@ -4,11 +4,21 @@
## 适用范围
-本文规定内核 HTTP 接口的类型定义、兼容要求、生成产物和验证流程。端点定义以 `kernel/apicontract/contracts.go` 为准;迁移已完成,`kernel/apicontract/legacy_routes.json` 清单为空。新增接口必须定义类型契约,不得加入该清单。
+本文规定内核 HTTP 接口的类型定义、兼容要求、生成产物和验证流程。传输类型与端点定义位于 `kernel/apicontract/` 下的各模块文件,`contracts.go` 汇总端点注册表;迁移已完成,`kernel/apicontract/legacy_routes.json` 清单为空。新增接口必须定义类型契约,不得加入该清单。
+
+## 接口维护流程
+
+1. 在契约包中定义或更新传输类型与端点,明确请求体、错误码、空值、默认值和历史输入兼容规则
+2. 用 `contractHandler` 绑定业务入口,保持路由中间件顺序、授权和租约范围
+3. 保持 `legacy_routes.json` 为空;删除接口时同步移除路由注册和契约定义
+4. 更新实际响应、兼容输入和严格类型测试,运行生成器,再修正编译器指出的调用问题
+5. 同步 `petal` 的生成声明和相关公共声明;公开接口同步更新接口文档
+
+生成检查读取实际路由与处理函数声明,验证方法、路径、处理函数和契约适配器的对应关系。CI 对照变更前的清单阻止增加存量记录。不得通过 `any`、类型断言或修改存量清单绕过契约检查。
## 契约与实现
-`kernel/apicontract/contracts.go` 定义请求、响应和端点。契约包独立于内核启动、数据库和持久化模型,生成器可以单独运行。API 入口通过 `contractHandler` 绑定端点,请求参数和成功返回值受到 Go 泛型签名约束;响应载荷通过构造函数设置,不能直接给通用 `ret.Data` 赋值。业务校验继续使用现有辅助函数,`contractFailure` 保留其错误码、消息和已支持的错误载荷。
+`kernel/apicontract/` 下的各模块文件定义请求、响应和端点,`contracts.go` 将其汇总到端点注册表。契约包独立于内核启动、数据库和持久化模型,生成器可以单独运行。API 入口通过 `contractHandler` 绑定端点,请求参数和成功返回值受到 Go 泛型签名约束;响应载荷通过构造函数设置,不能直接给通用 `ret.Data` 赋值。业务校验继续使用现有辅助函数,`contractFailure` 保留其错误码、消息和已支持的错误载荷。
生成器从同一组 Go 类型生成 `app/src/types/api/index.d.ts` 和 `kernel/apicontract/schema.json`。后者包含共享的 `$defs` 和每个端点的请求、响应 schema,测试使用同一套 schema 检查实际 HTTP 响应。类型声明不会在运行时验证 JSON,CI 中的处理函数测试负责验证序列化结果。
@@ -84,11 +94,11 @@ SQL 查询契约保留成功信封顶层的 `limit` 和 `truncated`。`SuccessSQ
`ignoretype` 和 `filterstrings` 仅用于声明过的旧参数兼容行为。生成的请求类型描述规范调用形式;兼容解码可能接受并忽略更宽的旧输入,兼容测试明确覆盖这些例外。不存在全局「绑定失败后回退旧解析」的开关。
-只读中间件仍可返回带 `closeTimeout` 的提示对象。`fetchPost` 的普通回调只接收消息处理后保留的非负错误码,块信息接口的 `3` 仍须处理;`fetchSyncPost` 和 `fetchGet` 保留完整响应。动态 URL 保留存量签名;静态 POST 路径必须来自契约或存量路由,错误参数不能通过重载回退。拼接出开放范围的模板 URL 时使用显式 `string` 变量。
+只读中间件仍可返回带 `closeTimeout` 的提示对象。`fetchPost` 的普通回调只接收消息处理后保留的非负错误码,块信息接口的 `3` 仍须处理;`fetchSyncPost` 和 `fetchGet` 保留完整响应。动态 URL 保留存量签名;静态 POST 路径必须来自契约,错误参数不能通过重载回退。拼接出开放范围的模板 URL 时使用显式 `string` 变量。
业务错误需要保留提示显示时长时使用 `FailureWithTimeout`。使用契约的块查询通过 `holdContractBlockRequest` 保留显式笔记本及附带 ID 的租约检查;状态查询允许已删除 ID 的行为仍由对应入口明确指定。
-`StructJSONBody` 用于已经采用 Go JSON 结构体绑定的接口,保留字段名大小写兼容、空值处理和解析错误,业务必填字段继续由处理函数校验。不得用它放宽已迁移接口的请求规则。失败时仍返回业务结果的端点显式设置 `DataOnError`,并调用端点自身的类型化 `FailureWithData` 方法。
+`StructJSONBody` 用于已经采用 Go JSON 结构体绑定的接口,保留字段名大小写兼容、空值处理和解析错误,业务必填字段继续由处理函数校验。不得用它放宽现有接口的请求规则。失败时仍返回业务结果的端点显式设置 `DataOnError`,并调用端点自身的类型化 `FailureWithData` 方法。
笔记本配置更新使用类型化的部分对象。字段选项 `legacyobject` 保留既有 JSON 往返转换中的数字归一化和结构体字段名大小写兼容;可缺省指针字段在缺失或为 null 时保留原值。加密字段仅为输入兼容而解码,配置补丁不会应用这些字段。`Base64Bytes` 显式描述字节切片的 Base64 字符串或字节数组输入。
@@ -102,8 +112,6 @@ SQL 查询契约保留成功信封顶层的 `limit` 和 `truncated`。`SuccessSQ
`DirectJSONOutput` 保留直接返回 JSON 对象或数组的独立协议,不添加内核信封,此类载荷使用 `SuccessDirectJSON` 返回。支持通知空响应的端点显式声明 `NoContent` 并返回 `SuccessNoContent`;HTTP 校验要求状态码为 204 且响应体为空。鉴权和只读错误仍保留内核错误信封。生成声明记录直接输出模式及可选的空响应支持。
-`WebSocketOutput` 通过 `WebSocketOptions` 声明入站和出站消息类型及插件准入失败状态。`UpgradeWebSocket` 在 `contractHandler` 的响应阶段将写入器交给连接生命周期;`RejectWebSocket` 序列化声明的拒绝载荷。生成的路由元数据包含双向消息模式,`ValidateWebSocketMessage` 单独校验连接内消息,避免与握手响应或中间件信封混淆。握手校验检查 HTTP 状态和响应体,网络回归验证升级头、Origin 拒绝、消息交互和取消后的连接关闭。
-
## 文件与流式协议
`RawSSEOptions` 和 `RawWebSocketOptions` 声明以字节为载荷的广播协议,通过 `ValidateRawSSEEvent` 和 `ValidateRawWebSocketFrame` 单独校验事件及帧元数据;JSON 事件与 RPC 消息保留各自既有校验。原始 WebSocket 的错误由升级器写出,不使用 `RejectWebSocket`。
@@ -114,6 +122,8 @@ JSON SSE 接口通过 `SSEOptions` 和 `SSEEvent` 声明各事件名称及载荷
页面响应通过 `HTTPContentOptions` 声明允许的 HTTP 状态与媒体类型组合,通过 `SuccessHTTPContent` 保留原始字节,复用既有二进制传输并单独处理 JSON 中间件错误。`FastJSON` 为指定的大体量响应保留快速 JSON 编码,不改变有类型载荷及响应信封;编码失败时仍回退到标准编码器。
+`WebSocketOutput` 通过 `WebSocketOptions` 声明入站和出站消息类型及插件准入失败状态。`UpgradeWebSocket` 在 `contractHandler` 的响应阶段将写入器交给连接生命周期;`RejectWebSocket` 序列化声明的拒绝载荷。生成的路由元数据包含双向消息模式,`ValidateWebSocketMessage` 单独校验连接内消息,避免与握手响应或中间件信封混淆。握手校验检查 HTTP 状态和响应体,网络回归验证升级头、Origin 拒绝、消息交互和取消后的连接关闭。
+
可选的 `*string` 表单字段区分未传字段与显式空字符串,并使用 `nonnullable`,因为表单文本不能包含 JSON 空值。导入处理器据此保留默认值和延后校验的行为。上传进度在解析表单前启动,解析失败时先清理进度再返回;类型绑定复用 Gin 缓存的表单。
`BinaryOutput` 通过 `BinaryContent` 和 `SuccessBinary` 声明原始文件响应。适配器保留字节和媒体类型,`ErrorStatus` 声明 JSON 失败响应使用的独立 HTTP 状态(`getFile` 使用 202)。schema 记录二进制成功响应和类型化 JSON 错误;`ValidateHTTPResponse` 先检查状态和媒体类型,再验证错误信封。生成的路由响应使用 `Blob`,现有 fetch 函数则使用 `JSONValue`,因为它们保留将文件内容解析为文本或 JSON 的行为。JSON 文件内容可以是任意 JSON,但结构化错误契约不因此放宽。
@@ -124,33 +134,22 @@ JSON SSE 接口通过 `SSEOptions` 和 `SSEEvent` 声明各事件名称及载荷
声明为 `[]*multipart.FileHeader` 的固定字段按原顺序接收全部文件,生成 `Array<Blob>`。可选文件列表缺省时保留 nil;文本与单文件字段仍取首值。`SuccessWithMessage` 保留成功响应中的非空提示,包括批量上传部分成功的情况。
-前端从类型化字段构造 `ContractFormData`,再交给现有请求函数。生成签名检查端点所需字段并区分文件与字符串,普通 `FormData` 不能满足已迁移上传接口的契约。可缺省字段不写入表单,字符串不裁剪空白。插件调用方构造表单时可实现生成的 `APIFormData<Request>` 接口。
-
-## 接口维护流程
+前端从类型化字段构造 `ContractFormData`,再交给现有请求函数。生成签名检查端点所需字段并区分文件与字符串,普通 `FormData` 不能满足上传接口的契约。可缺省字段不写入表单,字符串不裁剪空白。插件调用方构造表单时可实现生成的 `APIFormData<Request>` 接口。
动态上传接口使用 `MultipartFields` 保留每个字段名对应的全部文本值和文件。请求模式将字段名映射为文本或二进制值的数组,`ContractFormData` 将数组中的每一项追加为同名表单字段。固定字段上传仍绑定首个值。广播发布保留先处理文本、再处理文件的顺序及逐条消息的错误结果。端点专用的 `DecodeFailure` 处理保留原有解析错误码和载荷。
-1. 在契约包中定义或更新传输类型与端点,明确请求体、错误码、空值、默认值和历史输入兼容规则
-2. 用 `contractHandler` 绑定业务入口,保持路由中间件顺序、授权和租约范围
-3. 使用类型契约的路由不得同时出现在 `legacy_routes.json`;删除接口时同步移除对应记录,新增接口不得加入该清单
-4. 更新实际响应、兼容输入和严格类型测试,运行生成器,再修正编译器指出的调用问题
-5. 同步 `petal` 的生成声明和相关公共声明;公开接口同步更新接口文档
-
-生成检查读取实际路由与处理函数声明,验证方法、路径、处理函数和契约适配器的对应关系。CI 对照变更前的清单阻止增加存量记录。不得通过 `any`、类型断言或修改存量清单绕过契约检查。
-
## 生成与验证
在 `app/` 下运行:
```text
-pnpm run api:generate
pnpm run api:generate --petal ../../petal
pnpm run api:check --petal ../../petal
pnpm run lint
pnpm exec tsx --test src/util/fetch.test.ts src/util/fetchTimeout.test.ts src/util/contractFormData.test.ts src/config/systemConfig.test.ts src/util/keymapBindings.test.ts src/config/tabs/cloudUser.test.ts src/protyle/util/transactionContract.test.ts
```
-`--petal` 路径相对于生成器的工作目录 `kernel/`,示例对应同级仓库。CI 只检查本仓库产物,本地跨仓库同步须使用该参数核对插件声明。
+上述生成命令同时更新本仓库与 `petal`,无须再单独执行不带 `--petal` 的生成命令。`--petal` 路径相对于生成器的工作目录 `kernel/`,示例对应同级仓库。CI 只检查本仓库产物,本地跨仓库同步须使用该参数核对插件声明。
在 `kernel/` 下运行: