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: Organize API contract documentation by module
Daniel4d066f428d7b
diff --git a/docs/API-CONTRACTS.md b/docs/API-CONTRACTS.mdindex 48fdc57a98..758320fac2 100644--- a/docs/API-CONTRACTS.md+++ b/docs/API-CONTRACTS.md@@ -44,6 +44,10 @@ System contracts retain complete configuration, workspace management, uploads, aTransaction contracts discriminate all 98 known operation actions and exclude those names from the explicit unknown-action compatibility branch. The frontend uses the same finite operation union. Private raw input preserves historical fields, ignored values, asynchronous model errors, and echoed payloads; schema declarations do not move model validation into HTTP admission. Undo, redo, heading operations, response leases, and transaction persistence formats retain their existing behavior.+The editor submits a standalone `swapBlockRef` transaction with the reference in `id`, the definition in `blockID`, and the boolean options `includeChildren` and `originalToEmbed` in `data`. The kernel returns affected document IDs in `retData` and records an inverse operation with a private in-memory snapshot of affected top-level blocks. Snapshots are never serialized to clients or accepted from requests. Replay preserves block IDs and database bindings, rejects changed content or positions, and compensates document writes after a commit failure. `TestBlockSwapTransaction` covers persisted undo/redo, cross-document history, conflict rejection, and write-failure recovery; it runs in the full kernel command below. Run it separately with `go test -tags "fts5 sqlcipher" ./model -run 'Test(SwapBlockRefNodes|BlockSwapTransaction)' -count=1`.++Block-swap undo and redo hold notebook leases for all affected documents through response serialization. `TestContractBlockSwapReplayNotebookResponseLease` verifies that locking waits for the response to finish and is included in the full kernel command.+Extension clipping retains first-value multipart handling, dynamically named uploaded files, partial results, original messages, and encrypted-notebook admission. Dynamic icons retain SVG bytes, security and cache headers, and empty failure responses. Broadcast streams declare raw SSE bytes with dynamic event names, IDs, and retry values, and raw WebSocket frames with their upgrade errors; payloads are not converted to JSON or Base64, and channel cleanup retains its original scope.Plugin private services declare their existing serialization modes, raw files, redirects, proxy responses, SSE events, and WebSocket frames. Plugin-defined payloads remain protocol extension data; branch-specific validation distinguishes serialized formats from arbitrary bytes. Request bodies, explicit response headers, admission failures, and cancellation retain the plugin service lifecycle.@@ -52,6 +56,8 @@ Network contracts retain request bytes, multipart fields, headers, URL and TLS dAttribute-view contracts declare table, gallery, and kanban results separately and retain missing base fields, cell patches, and null values. 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. `TestAVContractPublishRenderDoesNotWrite` covers explicit view switching, unchanged database and carrier files, denied access, and missing databases. It runs in the full kernel CI suite; run the focused checks with `go test -tags "fts5 sqlcipher" ./api -run 'TestAVContract(PublishRenderDoesNotWrite|RenderWire)$' -count=1` and `go test ./apicontract/...`. 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.+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.AI providers accept `anthropic-messages` alongside `openai` and `openai-responses`; omitted protocols retain the Chat Completions default. Native Messages calls cover model discovery, model tests, editor generation, agent tools, titles, and summary compaction. An automatic output limit uses 4096 tokens because Messages requires `max_tokens`. Assistant entries may carry `nativeContent` with a protocol, version, and opaque content blocks; version 1 preserves Anthropic thinking signatures and block ordering across runtime recovery and session commits. Other protocols project the visible conversation without sending these native blocks. Unknown native versions fail before sending a Messages request, retaining the original session. Stream usage is emitted once after `message_stop`, and incomplete tool responses cannot reach tool execution. `TestAnthropic*` and `TestAPIContractAnthropic*` run in the full kernel CI suite; run the affected subset with `go test -tags "fts5 sqlcipher" ./conf ./util ./agent ./api ./apicontract/... -run 'Test(Anthropic|APIContractAnthropic|APIContractAI|AIInput|AISession|RouteCoverage)' -count=1`, plus `go test ./apicontract/...`. The existing frontend test discovery includes `aiProviderPresets.test.ts`.@@ -62,8 +68,20 @@ Setting contracts retain partial configuration merging, existing defaults, case-Document-tree contracts retain conditional parameter validation, path and sorting semantics, omitted callbacks, pagination defaults, and document response variants. Publish authentication preserves HTTP 429 and `Retry-After` through explicitly declared additional error statuses. Publish and encrypted-notebook admission remain before deferred field validation, and document leases cover response serialization.+`POST /api/filetree/duplicateDocTree` copies one document and all descendants within the same notebook and parent. It accepts a document `id` and returns the new root's `id`, `notebook`, `path`, and `hPath`; the existing single-document endpoint remains unchanged. Internal block references, block links, and explicit block IDs in query embeds are remapped across the copied tree, while database definitions and row bindings remain shared mirrors. Source snapshots are authenticated without repair writes, and detected runtime failures remove new documents and restore sorting. This is runtime compensation, not a crash-atomic transaction. `TestDuplicateDocTree*` covers hierarchy, ordering, references, rich cells, mirrors, invalid sources, and rollback; `TestAPIContractDuplicateDocTree*` covers actual responses, authorization, encrypted source preservation, failed authentication, lock/unlock, and response leases. These tests are included in the full kernel CI command; run the focused checks with `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(DuplicateDocTree|APIContractDuplicateDocTree)' -count=1` and `go test ./apicontract/...`.++`TestAPIContractFileTreeMissingDocuments` tests actual HTTP responses for missing documents during path lookup, removal, renaming, duplication, and moving. It checks business errors and preserved `closeTimeout` values against the response contracts and runs in the full kernel command below. Run it separately with `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` from `kernel/`.+Asset contracts retain nullable result lists, per-file upload order and duplicate names, successful partial-upload messages, and local-insertion failure payloads. OCR columns remain string-valued. Annotation validation, published-file admission, encrypted reads and writes, deferred downloads, and upload target selection keep their existing behavior. The non-API upload entry uses the same typed model operation.+Unused-asset listing and cleanup return the standard `code: -1`, `data: null` error when document loading or reference-directory traversal fails, including unsupported document specs. Single-asset cleanup uses the same complete scan; bulk cleanup does not start history creation or deletion before scanning succeeds. Successful response shapes and encrypted-notebook exclusions remain unchanged. `TestAssetUnusedScanFailureContract`, `TestUnusedAssets*`, `TestHTMLAssetLinkDests`, `TestPagedPathsErrorModes`, `TestExportAssetEntityRename`, and `TestExportHTMLMultipleAssetReferences` cover HTTP errors, source preservation, HTML references, URL suffixes, legacy traversal behavior, and exported resources and links. They run in the full kernel CI selection; run the focused checks with `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(AssetUnusedScanFailureContract|UnusedAssets|HTMLAssetLinkDests|PagedPathsErrorModes|ExportAssetEntityRename|ExportHTMLMultipleAssetReferences)' -count=1` and `go test ./apicontract/...`.++`/api/asset/resolveAssetPath` preserves its path-string response and administrator authorization. Unlocked encrypted assets return an authenticated temporary plaintext copy with the original filename; ordinary assets retain their existing paths. The request holds the encrypted notebook lease through response serialization. External edits do not write back to the encrypted source. `TestAPIContractNotebookSystemLock` includes external-asset response validation, original-name preservation, source preservation, authentication-failure cleanup, locked rejection, and lock cleanup; it runs in the full kernel CI selection. Run `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(APIContractNotebookSystemLock|AssetAdminEndpointsRejectReader|MobileExportLease)' -count=1` and `go test ./apicontract/...` for the affected behavior and route coverage.++Asset-reference scans report missing attribute-view definitions and invalid definition IDs in the optional `unavailableAttributeViews` list without blocking queries, previews, or replacement. Each entry identifies the notebook ID/name, document ID/path/human-readable path, block ID, view ID, and reason. Existing unreadable or corrupt definitions remain errors. Missing-file snapshots are revalidated before writing so a definition restored during scanning cannot be silently omitted. Attribute-view definitions are not deferred assets under the current download-path policy; the regression suite checks this assumption. The unavailable-definition regressions run through the model and actual HTTP contracts, including closed notebooks and shared definitions.++Asset-reference query and replacement contracts preserve scalar requests and also accept batches. Batch results retain input order and report each mapping's status, reason, references, and changed-file count; the top-level count includes each shared file once. Empty batches, duplicate sources, and chained or cyclic mappings are rejected. Independent mappings may succeed when another mapping fails; a shared-file write failure belongs to every affected mapping. Scans allow editing and validate the workspace snapshot before saving; cancellation and unchanged retries preserve source data. `TestAssetRelink` regressions cover scalar compatibility, batch validation, shared document/database/OCR persistence, history, and concurrent edits and are included in the full kernel command below.+Export contracts retain Markdown option defaults and numeric truncation, notebook-list filtering, ignored title-option types, optional HTML folders, and file-upload field selection. Error responses preserve message durations and empty-string resource payloads. Publish filtering, encrypted-notebook admission, response-held leases, and temporary export cleanup remain in the existing lifecycle.Repository contracts retain key encoding, snapshot metadata, numeric truncation and retention defaults, cloud pagination, and file access leases. Repository-file reads retain their media type and bytes; empty files retain the success envelope. Both file success and JSON failure use HTTP 200. For this explicitly declared shared status, `ValidateHTTPResponse` accepts raw file bytes and `ValidateErrorResponse` separately verifies known error payloads. No key material, encrypted file format, or snapshot recovery behavior changes.@@ -78,8 +96,12 @@ Marketplace contracts retain required-field ordering, whitespace handling, themePlugin information queries retain path, query-string, and JSON-body name precedence, including whitespace and business error codes 1 through 4. URL parameters skip body decoding, and list queries ignore the body. Plugin and RPC-method lists retain nullable arrays and entries. HTTP JSON-RPC has a separate contract for single and batch requests, success and error replies, and notification-only HTTP 204 responses. Plugin admission occurs before body reads, batch errors retain their order, and arbitrary JSON is limited to RPC parameters, results, and error details. RPC WebSocket routes declare HTTP 101 upgrades, HTTP 404 plugin admission errors, HTTP 400 text rejections, and separate incoming calls and outgoing replies or notifications. Origin authorization and connection cleanup remain in the existing upgrade lifecycle.+Plugin publishing contracts separate administrator-only grants and snapshot writes from authenticated public reads. Public data is limited to declared scalar fields; grants and snapshots use a local versioned store outside synced private plugin data. Static and file reads share plugin availability and secure file-opening rules, while publish loading excludes kernel code. `TestAPIContractPluginPublish`, `TestPluginPublishContracts`, `TestPluginPublish*`, and `TestPublishFile*` cover actual responses, admission, scope changes, revocation, reinstall, corruption, and path boundaries. They run in the full kernel suite; run the affected subset with `go test -tags "fts5 sqlcipher" ./api ./model ./server ./util ./apicontract -run 'Test(APIContractPluginPublish|PluginPublish|PublishFile|RouteCoverage)' -count=1`. The path-fixture CI selection also includes the model, static, and file-boundary regressions. See [Plugin publishing](PLUGIN-PUBLISH.md) for public API documentation and migration examples.+Search 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. `TestCustomBlockSearch` regressions cover configuration compatibility, saved criteria, filtering, and FTS updates; `TestAPIContractSettingConfigCompatibility` and `TestAPIContractSettingCompletePayloads` cover the setting contract. All are included in the full kernel command below.+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.Import contracts preserve archive cleanup, first-upload selection, untrimmed Markdown paths, and staged-token trimming and lifetime. Automatic SiYuan imports declare document, token, notebook, and notebook-collection results; mount failures retain the document payload. Obsidian task cancellation retains its task snapshot on failure. Notebook mounting, encrypted import handling, and creation notifications remain in the existing business operations.@@ -120,6 +142,10 @@ Heading transaction queries return `BlockTransaction` with typed operations andAll `/api/block/` routes use contracts. Heading-level queries retain batch-ID precedence, deduplication, fractional-level truncation, document struct binding, and message display durations. Document conversion results include the six heading counts and typed transactions. Reference checks validate only the fields used by the selected scope, preserve ignored fields and notebook trimming rules, and retain boolean error payloads, publish filtering, and notebook leases through response serialization. These exceptional input rules use private, endpoint-specific typed decoders; ordinary endpoints continue to use field declarations. Recent-update results use a recursive `SearchBlock` payload, including nullable references, children, and card metadata.+HTML clipboard conversion preserves literal backslashes and marker characters while retaining HTML formatting when Markdown syntax is disabled. `TestHTML2BlockDOMContractEscapedText` covers both source-format modes, actual response schemas, editing round trips, HTML entities, and style/link boundaries. Run it with the related conversion regressions using `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(.*HTML.*|.*Clipboard.*|.*IFrame.*|SpinBlockDOM.*|WPSPresentation.*|NormalizeMSWord.*|NormalizeWPS.*)' -count=1`. The full kernel command below includes these tests, and `app/tests/luteHtmlEscapes.test.js` checks the generated Lute JavaScript through the full frontend suite. Lute source changes also require `go test ./...` in the Lute repository and regeneration of the bundled JavaScript.++`swapBlockRef` accepts the optional boolean `originalToEmbed`. Omission or `false` keeps the reference at the original definition position; `true` replaces it with an embed querying the moved definition ID. `includeChildren` retains its heading and list behavior. `TestSwapBlockRefContractCompatibility` and `TestSwapBlockRefNodes` cover request defaults, invalid options, and both replacement modes across document, heading, and list cases; they run in the full kernel command below.+Storage contracts keep arbitrary JSON limited to storage values; keys, recent documents, search criteria, inline styles, and attribute-view palettes have structured types. Recent-document mutations retain their read-only no-op before parsing the body. An optional typed `beforeDecode` callback on `contractHandler` preserves this ordering and may return a response before decoding; route checks still require an explicit endpoint binding. Inline-style version 1 updates preserve existing built-in configuration, while version 2 and palette requests retain their struct-decoding compatibility.`DirectJSONOutput` preserves protocols that return their own JSON objects or arrays without the kernel envelope. Use `SuccessDirectJSON` for these payloads. Endpoints that also support empty notification responses explicitly declare `NoContent` and return `SuccessNoContent`; HTTP validation requires status 204 and an empty body. However, authentication and read-only failures retain the kernel error envelope. Generated declarations record the direct output mode and optional empty-response support.@@ -160,32 +186,6 @@ Dynamic multipart endpoints use `MultipartFields` to retain every text value and## Generation and verification-`POST /api/filetree/duplicateDocTree` copies one document and all descendants within the same notebook and parent. It accepts a document `id` and returns the new root's `id`, `notebook`, `path`, and `hPath`; the existing single-document endpoint remains unchanged. Internal block references, block links, and explicit block IDs in query embeds are remapped across the copied tree, while database definitions and row bindings remain shared mirrors. Source snapshots are authenticated without repair writes, and detected runtime failures remove new documents and restore sorting. This is runtime compensation, not a crash-atomic transaction. `TestDuplicateDocTree*` covers hierarchy, ordering, references, rich cells, mirrors, invalid sources, and rollback; `TestAPIContractDuplicateDocTree*` covers actual responses, authorization, encrypted source preservation, failed authentication, lock/unlock, and response leases. These tests are included in the full kernel CI command; run the focused checks with `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(DuplicateDocTree|APIContractDuplicateDocTree)' -count=1` and `go test ./apicontract/...`.--Unused-asset listing and cleanup return the standard `code: -1`, `data: null` error when document loading or reference-directory traversal fails, including unsupported document specs. Single-asset cleanup uses the same complete scan; bulk cleanup does not start history creation or deletion before scanning succeeds. Successful response shapes and encrypted-notebook exclusions remain unchanged. `TestAssetUnusedScanFailureContract`, `TestUnusedAssets*`, `TestHTMLAssetLinkDests`, `TestPagedPathsErrorModes`, `TestExportAssetEntityRename`, and `TestExportHTMLMultipleAssetReferences` cover HTTP errors, source preservation, HTML references, URL suffixes, legacy traversal behavior, and exported resources and links. They run in the full kernel CI selection; run the focused checks with `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(AssetUnusedScanFailureContract|UnusedAssets|HTMLAssetLinkDests|PagedPathsErrorModes|ExportAssetEntityRename|ExportHTMLMultipleAssetReferences)' -count=1` and `go test ./apicontract/...`.--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. `TestAVContractPublishRenderDoesNotWrite` covers explicit view switching, unchanged database and carrier files, denied access, and missing databases. It runs in the full kernel CI suite; run the focused checks with `go test -tags "fts5 sqlcipher" ./api -run 'TestAVContract(PublishRenderDoesNotWrite|RenderWire)$' -count=1` and `go test ./apicontract/...`. 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.--`/api/asset/resolveAssetPath` preserves its path-string response and administrator authorization. Unlocked encrypted assets return an authenticated temporary plaintext copy with the original filename; ordinary assets retain their existing paths. The request holds the encrypted notebook lease through response serialization. External edits do not write back to the encrypted source. `TestAPIContractNotebookSystemLock` includes external-asset response validation, original-name preservation, source preservation, authentication-failure cleanup, locked rejection, and lock cleanup; it runs in the full kernel CI selection. Run `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(APIContractNotebookSystemLock|AssetAdminEndpointsRejectReader|MobileExportLease)' -count=1` and `go test ./apicontract/...` for the affected behavior and route coverage.--HTML clipboard conversion preserves literal backslashes and marker characters while retaining HTML formatting when Markdown syntax is disabled. `TestHTML2BlockDOMContractEscapedText` covers both source-format modes, actual response schemas, editing round trips, HTML entities, and style/link boundaries. Run it with the related conversion regressions using `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(.*HTML.*|.*Clipboard.*|.*IFrame.*|SpinBlockDOM.*|WPSPresentation.*|NormalizeMSWord.*|NormalizeWPS.*)' -count=1`. The full kernel command below includes these tests, and `app/tests/luteHtmlEscapes.test.js` checks the generated Lute JavaScript through the full frontend suite. Lute source changes also require `go test ./...` in the Lute repository and regeneration of the bundled JavaScript.--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. `TestCustomBlockSearch` regressions cover configuration compatibility, saved criteria, filtering, and FTS updates; `TestAPIContractSettingConfigCompatibility` and `TestAPIContractSettingCompletePayloads` cover the setting contract. All are included in the full kernel command below.--`TestAPIContractFileTreeMissingDocuments` tests actual HTTP responses for missing documents during path lookup, removal, renaming, duplication, and moving. It checks business errors and preserved `closeTimeout` values against the response contracts and runs in the full kernel command below. Run it separately with `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` from `kernel/`.--`swapBlockRef` accepts the optional boolean `originalToEmbed`. Omission or `false` keeps the reference at the original definition position; `true` replaces it with an embed querying the moved definition ID. `includeChildren` retains its heading and list behavior. `TestSwapBlockRefContractCompatibility` and `TestSwapBlockRefNodes` cover request defaults, invalid options, and both replacement modes across document, heading, and list cases; they run in the full kernel command below.--The editor submits a standalone `swapBlockRef` transaction with the reference in `id`, the definition in `blockID`, and the boolean options `includeChildren` and `originalToEmbed` in `data`. The kernel returns affected document IDs in `retData` and records an inverse operation with a private in-memory snapshot of affected top-level blocks. Snapshots are never serialized to clients or accepted from requests. Replay preserves block IDs and database bindings, rejects changed content or positions, and compensates document writes after a commit failure. `TestBlockSwapTransaction` covers persisted undo/redo, cross-document history, conflict rejection, and write-failure recovery; it runs in the full kernel command below. Run it separately with `go test -tags "fts5 sqlcipher" ./model -run 'Test(SwapBlockRefNodes|BlockSwapTransaction)' -count=1`.--Block-swap undo and redo hold notebook leases for all affected documents through response serialization. `TestContractBlockSwapReplayNotebookResponseLease` verifies that locking waits for the response to finish and is included in the full kernel command.--Asset-reference scans report missing attribute-view definitions and invalid definition IDs in the optional `unavailableAttributeViews` list without blocking queries, previews, or replacement. Each entry identifies the notebook ID/name, document ID/path/human-readable path, block ID, view ID, and reason. Existing unreadable or corrupt definitions remain errors. Missing-file snapshots are revalidated before writing so a definition restored during scanning cannot be silently omitted. Attribute-view definitions are not deferred assets under the current download-path policy; the regression suite checks this assumption. The unavailable-definition regressions run through the model and actual HTTP contracts, including closed notebooks and shared definitions.--Asset-reference query and replacement contracts preserve scalar requests and also accept batches. Batch results retain input order and report each mapping's status, reason, references, and changed-file count; the top-level count includes each shared file once. Empty batches, duplicate sources, and chained or cyclic mappings are rejected. Independent mappings may succeed when another mapping fails; a shared-file write failure belongs to every affected mapping. Scans allow editing and validate the workspace snapshot before saving; cancellation and unchanged retries preserve source data. `TestAssetRelink` regressions cover scalar compatibility, batch validation, shared document/database/OCR persistence, history, and concurrent edits and are included in the full kernel command below.--Plugin publishing contracts separate administrator-only grants and snapshot writes from authenticated public reads. Public data is limited to declared scalar fields; grants and snapshots use a local versioned store outside synced private plugin data. Static and file reads share plugin availability and secure file-opening rules, while publish loading excludes kernel code. `TestAPIContractPluginPublish`, `TestPluginPublishContracts`, `TestPluginPublish*`, and `TestPublishFile*` cover actual responses, admission, scope changes, revocation, reinstall, corruption, and path boundaries. They run in the full kernel suite; run the affected subset with `go test -tags "fts5 sqlcipher" ./api ./model ./server ./util ./apicontract -run 'Test(APIContractPluginPublish|PluginPublish|PublishFile|RouteCoverage)' -count=1`. The path-fixture CI selection also includes the model, static, and file-boundary regressions. See [Plugin publishing](PLUGIN-PUBLISH.md) for public API documentation and migration examples.-Run from `app/`:```textdiff --git a/docs/API-CONTRACTS.zh-CN.md b/docs/API-CONTRACTS.zh-CN.mdindex a7e57252e3..88576c9afa 100644--- a/docs/API-CONTRACTS.zh-CN.md+++ b/docs/API-CONTRACTS.zh-CN.md@@ -42,6 +42,10 @@事务契约按动作区分全部 98 个已知操作,并在显式未知操作兼容分支中排除这些名称。前端使用同一组有限操作联合。私有原始输入保留历史字段、被忽略的值、模型异步错误和回传载荷,类型声明不把模型校验提前到 HTTP 准入阶段。撤销、重做、标题操作、响应租约及事务持久化格式保持原有行为。+编辑器通过独立的 `swapBlockRef` 事务转换块引用:`id` 指定引用块,`blockID` 指定定义块,`data` 包含布尔选项 `includeChildren` 和 `originalToEmbed`。内核在 `retData` 中返回受影响的文档 ID,并以受影响顶层块的私有内存快照生成逆向操作。快照不传给客户端,也不接受客户端提交。重放保留块 ID 和数据库绑定;内容或位置已改变时拒绝覆盖;提交失败时补偿已写入的文档。`TestBlockSwapTransaction` 覆盖落盘后的撤销和重做、跨文档历史、冲突拒绝及写入失败恢复,包含在下方完整内核测试命令中,也可在 `kernel/` 下运行 `go test -tags "fts5 sqlcipher" ./model -run 'Test(SwapBlockRefNodes|BlockSwapTransaction)' -count=1` 单独验证++块交换的撤销和重做为所有受影响文档持有笔记本租约,直到响应序列化完成。`TestContractBlockSwapReplayNotebookResponseLease` 验证锁定操作会等待响应结束,包含在完整内核测试命令中。+扩展剪藏保留多段表单首值处理、动态名称上传文件、部分结果、原有消息及加密笔记本准入。动态图标保留 SVG 字节、安全和缓存响应头以及空错误响应。广播流声明原始 SSE 字节、动态事件名、ID 和重试值,以及原始 WebSocket 帧和升级错误;载荷不转换为 JSON 或 Base64,频道清理保持原有作用范围。插件私有服务声明现有序列化模式、原始文件、重定向、代理响应、SSE 事件和 WebSocket 帧。插件自行定义的载荷保留为协议扩展数据,并按实际分支分别校验序列化格式与任意字节。请求体、显式响应头、准入错误和取消处理保持插件服务原有生命周期。@@ -58,8 +62,16 @@ AI 契约保留供应商配置、模型发现与匹配、确认结果、会话文档树契约保留条件参数校验、路径与排序语义、回调省略规则、分页默认值及文档响应变体。发布认证通过显式声明的额外错误状态保留 HTTP 429 和 `Retry-After`。发布及加密笔记本准入仍先于延迟字段校验,文档租约覆盖响应序列化阶段。+`POST /api/filetree/duplicateDocTree` 将单篇文档及全部子文档复制到同一笔记本的原父目录。请求为文档 `id`,返回副本根文档的 `id`、`notebook`、`path` 和 `hPath`;原有单文档复制接口保持不变。副本中的内部块引用、块链接和查询嵌入的显式块 ID 统一重映射,数据库定义与行绑定保持为共享镜像。源快照经过认证读取,不执行修复写回;运行期间检测到失败时清理新建文档并恢复排序。这是运行时补偿,不保证进程崩溃时的事务原子性。`TestDuplicateDocTree*` 覆盖层级、排序、引用、富文本单元格、镜像、无效源及回滚;`TestAPIContractDuplicateDocTree*` 覆盖实际响应、授权、加密源数据保留、认证失败、锁定与解锁及响应租约。测试已纳入内核全量持续集成命令,可使用 `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(DuplicateDocTree|APIContractDuplicateDocTree)' -count=1` 和 `go test ./apicontract/...` 单独验证。++`TestAPIContractFileTreeMissingDocuments` 通过实际 HTTP 响应覆盖查询路径、删除、重命名、复制和移动不存在的文档,按响应契约检查业务错误及保留的 `closeTimeout`,已包含在下方的内核全量测试命令中。可在 `kernel/` 中执行 `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` 单独验证。+附件契约保留结果列表的空值、逐文件上传顺序与重名文件、部分上传成功时的提示及本地插入失败载荷。OCR 列保持字符串值。标注校验、发布文件准入、加密读写、延迟下载和上传目标选择保持既有行为。非 API 上传入口复用同一个有类型的模型操作。+资源引用扫描将定义文件缺失或定义 ID 无效的数据库记录到可选的 `unavailableAttributeViews` 列表,不阻断查询、预演或替换。每项包含笔记本 ID 与名称、文档 ID 与路径及人类可读路径、块 ID、数据库 ID 和原因。已存在但无法读取或内容损坏的定义仍返回错误。保存前重新校验缺失文件快照,避免遗漏扫描期间恢复的定义。当前按需下载规则不包含数据库定义,回归测试会检查这一前提。缺失定义回归覆盖模型和实际 HTTP 契约,包括关闭的笔记本和共享定义。++资源引用查询和替换契约保留单资源请求,同时支持批量参数。批量结果按输入顺序返回每项状态、原因、引用和改动文件数;顶层计数对共享文件去重。空批次、重复源路径、链式和循环映射会被拒绝。独立映射可以在其他项失败时完成,共享文件写入失败归属所有受影响映射。扫描期间允许编辑,保存前校验工作区快照;取消和无变化重试保留源数据。`TestAssetRelink` 回归覆盖单资源兼容、批量校验、共享文档、数据库及 OCR 保存、历史和并发编辑,已包含在下方完整内核测试命令中。+导出契约保留 Markdown 选项默认值与数字截断、笔记本列表过滤、标题选项类型忽略规则、可省略的 HTML 目录字段及上传字段选择规则。错误响应保留消息显示时长与资源错误中的空字符串载荷。发布过滤、加密笔记本准入、覆盖响应阶段的租约及临时导出清理保持既有生命周期。仓库契约保留密钥编码、快照元数据、数值截断与保留期限默认值、云端分页及文件访问租约。仓库文件读取保留媒体类型和原始字节,空文件保留成功信封。文件成功与 JSON 失败均返回 HTTP 200。对于显式声明的共用状态,`ValidateHTTPResponse` 接受文件原始字节,`ValidateErrorResponse` 单独验证已知错误载荷。密钥材料、加密文件格式及快照恢复行为保持不变。@@ -74,8 +86,12 @@ AI 契约保留供应商配置、模型发现与匹配、确认结果、会话插件信息查询保留路径参数、查询参数和 JSON 请求体中名称的优先级,包括空白及业务错误码 1 至 4。命中 URL 参数时不解析请求体,列表查询忽略请求体。插件列表与 RPC 方法列表保留数组及数组元素的空值语义。HTTP JSON-RPC 使用独立契约描述单次与批量请求、成功与错误回复,以及纯通知请求的 HTTP 204 响应。插件准入先于请求体读取,批量错误保留原有顺序,任意 JSON 仅用于 RPC 参数、返回值和错误详情。RPC WebSocket 路由声明 HTTP 101 升级、HTTP 404 插件准入错误、HTTP 400 文本拒绝,以及独立的入站调用和出站回复或通知。Origin 授权与连接清理保留在既有升级生命周期中。+插件发布契约将仅管理员可用的授权、快照写入与已认证访问者的公开读取分离。公开数据仅包含声明的标量字段,授权和快照使用独立于同步插件私有存储的本地版本化文件。静态路由与文件接口共用插件状态和安全文件打开规则,发布加载响应排除内核代码。`TestAPIContractPluginPublish`、`TestPluginPublishContracts`、`TestPluginPublish*` 和 `TestPublishFile*` 覆盖实际响应、准入、范围变化、撤销、重装、损坏及路径边界,均包含在完整内核测试中。可运行 `go test -tags "fts5 sqlcipher" ./api ./model ./server ./util ./apicontract -run 'Test(APIContractPluginPublish|PluginPublish|PublishFile|RouteCoverage)' -count=1` 单独验证。路径测试的 CI 筛选也包含模型、静态路由与文件边界回归。接口说明和迁移示例见[插件发布](PLUGIN-PUBLISH.zh-CN.md)。+搜索契约保留分页默认值与小数截断、路径校验与去重、历史子类型筛选的忽略规则,以及空值与空数组的区别。引用搜索区分仅回传请求标识和完整块结果,保留笔记本准入先于延迟参数校验的顺序。SQL 搜索权限、发布过滤、加密笔记本租约、取消请求的响应和只读嵌入块更新的空操作保持原有顺序。桌面端与移动端调用使用生成的请求类型。+自定义块搜索使用可选的 `customBlock` 类型筛选字段。API 显式传入类型映射时仅搜索其中启用的类型;省略类型映射时使用搜索设置,默认启用自定义块。保存的搜索条件区分字段缺失与 `false`,使旧版前端搜索配置能够继承设置。`TestCustomBlockSearch` 回归覆盖配置兼容、搜索条件保存、类型筛选和全文索引更新;`TestAPIContractSettingConfigCompatibility` 与 `TestAPIContractSettingCompletePayloads` 覆盖设置契约,均包含在下方完整内核测试命令中。+历史契约保留路径去空白、可选高亮默认值、历史类型的小数截断,以及空值与空数组的区别。版本对比先检查两个引用对象,再检查对象字段,并按排序后的笔记本 ID 获取租约。内容读取及文档、资源和数据库回滚保留历史路径租约检查,笔记本回滚沿用模型层的恢复行为。导入契约保留压缩包清理、首个上传文件选择、Markdown 路径空白,以及暂存令牌去空白和有效期。思源自动导入声明文档、令牌、笔记本和笔记本集合结果,挂载失败时保留文档载荷。Obsidian 任务取消失败时保留任务快照。笔记本挂载、加密导入处理和创建通知仍由既有业务操作负责。@@ -116,6 +132,8 @@ SQL 查询契约保留成功信封顶层的 `limit` 和 `truncated`。`SuccessSQ全部 `/api/block/` 路由已使用契约。标题级别查询保留批量 ID 优先、去重、小数级别截断、文档结构体绑定和消息展示时长。文档转换结果包含六级标题计数和有类型的事务。引用检查仅校验所选范围使用的字段,保留忽略字段及笔记本参数去空白规则,并保留布尔错误载荷、发布过滤和持续到响应序列化完成的笔记本租约。这些特殊输入规则使用私有的入口专用类型解码函数,而普通接口继续通过字段声明解码。最近更新结果使用递归的 `SearchBlock` 载荷,包含可为空的引用、子块和卡片元数据。+HTML 剪贴板转换保留字面反斜杠和标记字符,并在关闭 Markdown 语法时保留 HTML 格式。`TestHTML2BlockDOMContractEscapedText` 覆盖两种源格式模式、实际响应模式校验、编辑往返、HTML 实体以及样式和链接边界。可使用 `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(.*HTML.*|.*Clipboard.*|.*IFrame.*|SpinBlockDOM.*|WPSPresentation.*|NormalizeMSWord.*|NormalizeWPS.*)' -count=1` 运行相关转换回归。下方内核全量命令已包含这些测试,`app/tests/luteHtmlEscapes.test.js` 通过前端全量测试检查生成的 Lute JavaScript。修改 Lute 源码后,还需在 Lute 仓库运行 `go test ./...` 并重新生成内置 JavaScript。+存储契约仅在存储值中允许任意 JSON,键、最近文档、搜索条件、行内样式和属性视图调色板均使用结构化类型。最近文档写入接口保留只读角色在解析请求体前直接返回成功的行为。`contractHandler` 可选的类型化 `beforeDecode` 回调用于保留这一执行顺序,允许在解码前返回响应;路由检查仍要求显式绑定端点。行内样式版本 1 的更新保留已有内置配置,版本 2 和调色板请求保留结构体解码的兼容行为。`DirectJSONOutput` 保留直接返回 JSON 对象或数组的独立协议,不添加内核信封,此类载荷使用 `SuccessDirectJSON` 返回。支持通知空响应的端点显式声明 `NoContent` 并返回 `SuccessNoContent`;HTTP 校验要求状态码为 204 且响应体为空。鉴权和只读错误仍保留内核错误信封。生成声明记录直接输出模式及可选的空响应支持。@@ -156,24 +174,6 @@ JSON SSE 接口通过 `SSEOptions` 和 `SSEEvent` 声明各事件名称及载荷## 生成与验证-`POST /api/filetree/duplicateDocTree` 将单篇文档及全部子文档复制到同一笔记本的原父目录。请求为文档 `id`,返回副本根文档的 `id`、`notebook`、`path` 和 `hPath`;原有单文档复制接口保持不变。副本中的内部块引用、块链接和查询嵌入的显式块 ID 统一重映射,数据库定义与行绑定保持为共享镜像。源快照经过认证读取,不执行修复写回;运行期间检测到失败时清理新建文档并恢复排序。这是运行时补偿,不保证进程崩溃时的事务原子性。`TestDuplicateDocTree*` 覆盖层级、排序、引用、富文本单元格、镜像、无效源及回滚;`TestAPIContractDuplicateDocTree*` 覆盖实际响应、授权、加密源数据保留、认证失败、锁定与解锁及响应租约。测试已纳入内核全量持续集成命令,可使用 `go test -tags "fts5 sqlcipher" ./model ./api -run 'Test(DuplicateDocTree|APIContractDuplicateDocTree)' -count=1` 和 `go test ./apicontract/...` 单独验证。--HTML 剪贴板转换保留字面反斜杠和标记字符,并在关闭 Markdown 语法时保留 HTML 格式。`TestHTML2BlockDOMContractEscapedText` 覆盖两种源格式模式、实际响应模式校验、编辑往返、HTML 实体以及样式和链接边界。可使用 `go test -tags "fts5 sqlcipher" ./api ./model -run 'Test(.*HTML.*|.*Clipboard.*|.*IFrame.*|SpinBlockDOM.*|WPSPresentation.*|NormalizeMSWord.*|NormalizeWPS.*)' -count=1` 运行相关转换回归。下方内核全量命令已包含这些测试,`app/tests/luteHtmlEscapes.test.js` 通过前端全量测试检查生成的 Lute JavaScript。修改 Lute 源码后,还需在 Lute 仓库运行 `go test ./...` 并重新生成内置 JavaScript。--自定义块搜索使用可选的 `customBlock` 类型筛选字段。API 显式传入类型映射时仅搜索其中启用的类型;省略类型映射时使用搜索设置,默认启用自定义块。保存的搜索条件区分字段缺失与 `false`,使旧版前端搜索配置能够继承设置。`TestCustomBlockSearch` 回归覆盖配置兼容、搜索条件保存、类型筛选和全文索引更新;`TestAPIContractSettingConfigCompatibility` 与 `TestAPIContractSettingCompletePayloads` 覆盖设置契约,均包含在下方完整内核测试命令中。--编辑器通过独立的 `swapBlockRef` 事务转换块引用:`id` 指定引用块,`blockID` 指定定义块,`data` 包含布尔选项 `includeChildren` 和 `originalToEmbed`。内核在 `retData` 中返回受影响的文档 ID,并以受影响顶层块的私有内存快照生成逆向操作。快照不传给客户端,也不接受客户端提交。重放保留块 ID 和数据库绑定;内容或位置已改变时拒绝覆盖;提交失败时补偿已写入的文档。`TestBlockSwapTransaction` 覆盖落盘后的撤销和重做、跨文档历史、冲突拒绝及写入失败恢复,包含在下方完整内核测试命令中,也可在 `kernel/` 下运行 `go test -tags "fts5 sqlcipher" ./model -run 'Test(SwapBlockRefNodes|BlockSwapTransaction)' -count=1` 单独验证--块交换的撤销和重做为所有受影响文档持有笔记本租约,直到响应序列化完成。`TestContractBlockSwapReplayNotebookResponseLease` 验证锁定操作会等待响应结束,包含在完整内核测试命令中。--`TestAPIContractFileTreeMissingDocuments` 通过实际 HTTP 响应覆盖查询路径、删除、重命名、复制和移动不存在的文档,按响应契约检查业务错误及保留的 `closeTimeout`,已包含在下方的内核全量测试命令中。可在 `kernel/` 中执行 `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` 单独验证。--资源引用扫描将定义文件缺失或定义 ID 无效的数据库记录到可选的 `unavailableAttributeViews` 列表,不阻断查询、预演或替换。每项包含笔记本 ID 与名称、文档 ID 与路径及人类可读路径、块 ID、数据库 ID 和原因。已存在但无法读取或内容损坏的定义仍返回错误。保存前重新校验缺失文件快照,避免遗漏扫描期间恢复的定义。当前按需下载规则不包含数据库定义,回归测试会检查这一前提。缺失定义回归覆盖模型和实际 HTTP 契约,包括关闭的笔记本和共享定义。--资源引用查询和替换契约保留单资源请求,同时支持批量参数。批量结果按输入顺序返回每项状态、原因、引用和改动文件数;顶层计数对共享文件去重。空批次、重复源路径、链式和循环映射会被拒绝。独立映射可以在其他项失败时完成,共享文件写入失败归属所有受影响映射。扫描期间允许编辑,保存前校验工作区快照;取消和无变化重试保留源数据。`TestAssetRelink` 回归覆盖单资源兼容、批量校验、共享文档、数据库及 OCR 保存、历史和并发编辑,已包含在下方完整内核测试命令中。--插件发布契约将仅管理员可用的授权、快照写入与已认证访问者的公开读取分离。公开数据仅包含声明的标量字段,授权和快照使用独立于同步插件私有存储的本地版本化文件。静态路由与文件接口共用插件状态和安全文件打开规则,发布加载响应排除内核代码。`TestAPIContractPluginPublish`、`TestPluginPublishContracts`、`TestPluginPublish*` 和 `TestPublishFile*` 覆盖实际响应、准入、范围变化、撤销、重装、损坏及路径边界,均包含在完整内核测试中。可运行 `go test -tags "fts5 sqlcipher" ./api ./model ./server ./util ./apicontract -run 'Test(APIContractPluginPublish|PluginPublish|PublishFile|RouteCoverage)' -count=1` 单独验证。路径测试的 CI 筛选也包含模型、静态路由与文件边界回归。接口说明和迁移示例见[插件发布](PLUGIN-PUBLISH.zh-CN.md)。-在 `app/` 下运行:```text