siyuan 1.2.2-alpha.0 → 1.2.2

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.
package/CHANGELOG.md CHANGED
@@ -1,8 +1,16 @@
1
1
  # Changelog
2
2
 
3
- ## v1.2.2 2026
3
+ ## v1.2.3 2026
4
4
 
5
- * [:art: add kernel type definitions and update package exports](https://github.com/siyuan-note/petal/pull/49)
5
+
6
+ ## v1.2.2 2026-6-30
7
+
8
+ * [Align with SiYuan's latest type definitions](https://github.com/siyuan-note/petal/pull/54)
9
+ * [Deprecate updateTransaction and replace with updateTransactionElement](https://github.com/siyuan-note/siyuan/issues/17828)
10
+ * [Improve LocalStorage related APIs](https://github.com/siyuan-note/siyuan/pull/17482)
11
+ * [Add kernel type definitions and update package exports](https://github.com/siyuan-note/petal/pull/49)
12
+ * [Add MCP interface and handler types to kernel definitions](https://github.com/siyuan-note/petal/pull/53)
13
+ * [Provides plugin functions for exporting files](https://github.com/siyuan-note/siyuan/issues/15484)
6
14
 
7
15
  ## v1.2.1 2026-04-21
8
16
 
package/kernel.d.ts CHANGED
@@ -2,8 +2,7 @@
2
2
  /// <reference types="@dop251/types-goja_nodejs-global" />
3
3
  /// <reference types="@dop251/types-goja_nodejs-url" />
4
4
 
5
- export { };
6
-
5
+ import type { JSONSchema } from "zod/v4/core";
7
6
  declare global {
8
7
  const siyuan: ISiyuan;
9
8
  }
@@ -127,6 +126,45 @@ export interface IEventMessage {
127
126
  detail: any;
128
127
  }
129
128
 
129
+ /**
130
+ * Published on the runtime event bus after the plugin starts successfully
131
+ * and enters the running state.
132
+ */
133
+ export interface IStartEventMessage extends IEventMessage {
134
+ type: 'start';
135
+ detail: null;
136
+ }
137
+
138
+ /**
139
+ * Published on the runtime event bus at the beginning of a clean plugin
140
+ * shutdown, before the runtime is torn down.
141
+ */
142
+ export interface IStopEventMessage extends IEventMessage {
143
+ type: 'stop';
144
+ detail: null;
145
+ }
146
+
147
+ /** File-system event kind emitted by the kernel storage watcher. */
148
+ export type TFsNotifyOperation = 'CREATE' | 'WRITE' | 'RENAME' | 'REMOVE';
149
+
150
+ /**
151
+ * Published on the runtime event bus when a watched storage path is
152
+ * created, written, renamed, or removed.
153
+ *
154
+ * Watching is managed via {@link IStorage.watcher}.
155
+ */
156
+ export interface IFsNotifyEventMessage extends IEventMessage {
157
+ type: 'fs-notify';
158
+ detail: {
159
+ /** The type of file-system change that triggered this event. */
160
+ operation: TFsNotifyOperation;
161
+ /** Path relative to the plugin's storage directory. */
162
+ path: string;
163
+ }
164
+ }
165
+
166
+ export type TEventMessage = IStartEventMessage | IStopEventMessage | IFsNotifyEventMessage | IEventMessage;
167
+
130
168
  // ── WebSocket ─────────────────────────────────────────────────────────────────
131
169
 
132
170
  /**
@@ -265,7 +303,7 @@ export interface IWebSocket {
265
303
  /** Negotiated sub-protocol, or an empty string if none was negotiated. */
266
304
  readonly protocol: string;
267
305
  /** Current connection state. See {@link TWebSocketReadyState}. */
268
- readyState: TWebSocketReadyState;
306
+ readonly readyState: TWebSocketReadyState;
269
307
  /** The WebSocket server URL (e.g. `"ws://127.0.0.1:6806/ws/…"`). */
270
308
  readonly url: string;
271
309
  /** Called when the connection is established. */
@@ -327,7 +365,7 @@ export interface IWebSocket {
327
365
  */
328
366
  export interface IEventSource {
329
367
  /** Current connection state. See {@link TEventSourceReadyState}. */
330
- readyState: TEventSourceReadyState;
368
+ readonly readyState: TEventSourceReadyState;
331
369
  /** The original path passed to {@link IClient.event}, e.g. `"/api/…"`. */
332
370
  readonly url: string;
333
371
  /** Called when the connection is established. */
@@ -409,10 +447,8 @@ export interface IPlugin {
409
447
  * advancing to the next lifecycle stage. Unset callbacks (`null`) are skipped.
410
448
  */
411
449
  export interface IPluginLifecycle {
412
- /** Called when the plugin script is first evaluated (before the runtime is fully ready). */
450
+ /** Called when the plugin script is first evaluated (before the `running` state.). */
413
451
  onload: (() => void | Promise<void>) | null;
414
- /** Called after the plugin has fully loaded and its runtime is initialized. */
415
- onloaded: (() => void | Promise<void>) | null;
416
452
  /** Called when the plugin transitions to the `running` state. */
417
453
  onrunning: (() => void | Promise<void>) | null;
418
454
  /** Called when the plugin is being unloaded (e.g. on shutdown or hot-reload). */
@@ -429,11 +465,10 @@ export interface IEvent {
429
465
  /**
430
466
  * Inbound kernel event handler.
431
467
  *
432
- * @remarks Assign a function to receive every kernel broadcast event.
433
- * If the function returns a `Promise`, the kernel awaits its resolution
434
- * before delivering the next event. Set to `null` to stop receiving events.
468
+ * @remarks Assign a function to receive every kernel dispatched event.
469
+ * Set to `null` to stop receiving events.
435
470
  */
436
- handler: ((event: IEventMessage) => void | Promise<void>) | null;
471
+ handler: ((event: TEventMessage) => void | Promise<void>) | null;
437
472
  /**
438
473
  * Publishes an event to the in-process event bus.
439
474
  *
@@ -499,8 +534,32 @@ export interface IStorage {
499
534
  * @returns An array of {@link IStorageEntry} descriptors.
500
535
  */
501
536
  list(path: string): Promise<IStorageEntry[]>;
537
+ readonly watcher: IStorageWatcher;
538
+ }
539
+
540
+ /**
541
+ * Controls which storage paths the plugin's file-system watcher monitors.
542
+ * Changes on watched paths are delivered as {@link IFsNotifyEventMessage}
543
+ * events on the runtime event bus.
544
+ */
545
+ export interface IStorageWatcher {
546
+ /**
547
+ * Resolves `path` and registers it with the file-system watcher.
548
+ * @param path - Path relative to the storage directory to start watching.
549
+ */
550
+ add(path: string): Promise<void>;
551
+ /**
552
+ * Resolves `path` and unregisters it from the file-system watcher.
553
+ * @param path - Path relative to the storage directory to stop watching.
554
+ */
555
+ remove(path: string): Promise<void>;
502
556
  }
503
557
 
558
+ /**
559
+ * RPC / MCP method handler type.
560
+ */
561
+ export type THandler = (...args: any[]) => any | Promise<any>;
562
+
504
563
  /**
505
564
  * JSON-RPC method registry for the plugin.
506
565
  *
@@ -513,12 +572,12 @@ export interface IRpc {
513
572
  * Registers a named RPC method callable by external clients.
514
573
  *
515
574
  * @param name - Unique method name used to dispatch the call.
516
- * @param fn - Handler function; may be async.
575
+ * @param handler - Handler function; may be async.
517
576
  * @param descriptions - Optional human-readable description strings.
518
577
  */
519
578
  bind(
520
579
  name: string,
521
- fn: (...args: any[]) => any | Promise<any>,
580
+ handler: THandler,
522
581
  ...descriptions: string[]
523
582
  ): Promise<void>;
524
583
  /**
@@ -533,7 +592,78 @@ export interface IRpc {
533
592
  * @param method - Notification method name.
534
593
  * @param params - Optional notification parameters.
535
594
  */
536
- broadcast(method: string, params?: any): Promise<void>;
595
+ broadcast(method: string, params?: any[] | Record<string, any>): Promise<void>;
596
+ }
597
+
598
+ /**
599
+ * MCP (Model Context Protocol) interface exposed to plugins.
600
+ *
601
+ * Allows plugins to register and unregister tools that are surfaced to MCP clients
602
+ * (e.g., AI assistants) through the kernel's built-in MCP server.
603
+ */
604
+ export interface IMcp {
605
+ /**
606
+ * Register a tool with the MCP server.
607
+ *
608
+ * The tool name is automatically namespaced to avoid collisions between plugins.
609
+ * The actual registered name follows the pattern `plugin__<plugin-name>__<tool-name>`.
610
+ *
611
+ * @param name - The tool name, local to this plugin (e.g. `"my-tool"`).
612
+ * @param config - Metadata describing the tool to MCP clients.
613
+ * @param handler - The function invoked when an MCP client calls the tool.
614
+ * @returns The registration record, including the fully-qualified tool name.
615
+ */
616
+ registerTool(
617
+ name: string,
618
+ config: IMcpToolConfig,
619
+ handler: THandler,
620
+ ): Promise<IRegisteredTool>;
621
+
622
+ /**
623
+ * Unregister a previously registered tool.
624
+ *
625
+ * Uses the same local name passed to {@link registerTool}; the kernel resolves
626
+ * the fully-qualified name internally.
627
+ *
628
+ * @param name - The local tool name used when the tool was registered.
629
+ */
630
+ unregisterTool(name: string): Promise<void>;
631
+ }
632
+
633
+ /**
634
+ * Metadata describing an MCP tool to clients.
635
+ *
636
+ * All fields are optional; omitting them produces a tool with no description or
637
+ * schema constraints, which is valid but less discoverable.
638
+ */
639
+ export interface IMcpToolConfig {
640
+ /** Human-readable display name shown in MCP client UIs. */
641
+ title?: string;
642
+ /** Natural-language description of what the tool does, used by AI clients for tool selection. */
643
+ description: string;
644
+ /** JSON Schema describing the tool's input parameters. */
645
+ inputSchema: JSONSchema.ObjectSchema;
646
+ /** JSON Schema describing the tool's output. */
647
+ outputSchema?: JSONSchema.Schema;
648
+ }
649
+
650
+ /**
651
+ * The registration record returned by {@link IMcp.registerTool}.
652
+ *
653
+ * Extends {@link IMcpToolConfig} with the resolved fully-qualified name and the
654
+ * handler that was registered.
655
+ */
656
+ export interface IRegisteredTool extends IMcpToolConfig {
657
+ /**
658
+ * The fully-qualified tool name as registered with the MCP server.
659
+ *
660
+ * The kernel namespaces tool names to prevent collisions between plugins.
661
+ * A tool registered as `"my-tool"` in plugin `"my-plugin"` becomes
662
+ * `"plugin__my_plugin__my_tool"`.
663
+ *
664
+ * @example "plugin__plugin_name__tool_name"
665
+ */
666
+ name: string;
537
667
  }
538
668
 
539
669
  // ── Server request types ─────────────────────────────────────────────────────
@@ -878,24 +1008,119 @@ export interface IHttpResponse {
878
1008
 
879
1009
  // ── Server handler interfaces ─────────────────────────────────────────────────
880
1010
 
1011
+ /**
1012
+ * A single Server-Sent Event (SSE) frame sent from the server to the client.
1013
+ *
1014
+ * Each field corresponds to a line prefix defined by the SSE specification
1015
+ * {@link https://html.spec.whatwg.org/multipage/server-sent-events.html | HTML Standard - 9.2 Server-sent events}.
1016
+ */
1017
+ export interface IServerSentEvent {
1018
+ /** `id:` field — sets the event source's last-event-ID, used for reconnection replay. */
1019
+ id?: string;
1020
+ /** `event:` field — custom event type. */
1021
+ event?: string;
1022
+ /** `data:` field — the payload of the event. Multi-line values are joined with `\n`. */
1023
+ data: any;
1024
+ /** `retry:` field — overrides the client's reconnection delay (milliseconds). */
1025
+ retry?: number;
1026
+ }
1027
+
1028
+ /**
1029
+ * Server-side SSE (Server-Sent Events) port provided to
1030
+ * {@link IServerEventSourceRequest.port}.
1031
+ *
1032
+ * @remarks
1033
+ * The kernel opens the SSE response stream before invoking the handler.
1034
+ * Once streaming begins, {@link IEventSourcePort.onopen} fires to signal that
1035
+ * the stream is ready for events. Call {@link IEventSourcePort.send} to push
1036
+ * SSE events to the client and {@link IEventSourcePort.close} to terminate
1037
+ * the stream. The connection stays open until `close()` is called or the
1038
+ * client disconnects.
1039
+ */
1040
+ export interface IEventSourcePort {
1041
+ /** Called once when the SSE stream is ready to accept events. */
1042
+ onopen: ((event: IEventSourceOpenEvent) => void | Promise<void>) | null;
1043
+ /** Called when the client disconnects or after {@link IEventSourcePort.close}. */
1044
+ onclose: ((event: IEventSourceCloseEvent) => void | Promise<void>) | null;
1045
+ /**
1046
+ * Pushes one SSE event to the connected client.
1047
+ *
1048
+ * @remarks
1049
+ * `send` is synchronous — no `await` required. It enqueues the event in
1050
+ * the kernel's SSE write buffer; the actual flush is asynchronous.
1051
+ *
1052
+ * @param event - The SSE event to send.
1053
+ */
1054
+ send(event: IServerSentEvent): void;
1055
+ /** Terminates the SSE stream and closes the response. */
1056
+ close(): void;
1057
+ }
1058
+
1059
+ /**
1060
+ * The request object received by {@link IServerScope.ws | WebSocket server handlers}.
1061
+ *
1062
+ * @remarks
1063
+ * Extends {@link IServerRequest} with a `port` property that mirrors the
1064
+ * {@link IWebSocket} client interface. The kernel upgrades the HTTP connection
1065
+ * to WebSocket before invoking the handler. After the handler returns, the
1066
+ * kernel auto-opens the port's read loop if `port.open()` was not called
1067
+ * explicitly.
1068
+ */
1069
+ export interface IServerWebSocketRequest extends IServerRequest {
1070
+ /**
1071
+ * Bidirectional WebSocket port connected to the client.
1072
+ *
1073
+ * @remarks
1074
+ * Assign event callbacks (`onopen`, `onmessage`, `onping`, `onpong`,
1075
+ * `onclose`, `onerror`) before the handler returns. Calling `port.open()`
1076
+ * is optional — the kernel opens the read loop automatically.
1077
+ */
1078
+ readonly port: Omit<IWebSocket, "extensions" | "url">;
1079
+ }
1080
+
1081
+ /**
1082
+ * The request object received by {@link IServerScope.es | SSE server handlers}.
1083
+ *
1084
+ * @remarks
1085
+ * Extends {@link IServerRequest} with a `port` property for pushing
1086
+ * Server-Sent Events to the client. The kernel opens the SSE response before
1087
+ * the handler is invoked; {@link IEventSourcePort.onopen} fires once streaming
1088
+ * begins.
1089
+ */
1090
+ export interface IServerEventSourceRequest extends IServerRequest {
1091
+ /**
1092
+ * Server-side SSE port for pushing events to the connected client.
1093
+ *
1094
+ * @remarks
1095
+ * Assign `onopen` and `onclose` callbacks in the handler body. Call
1096
+ * `port.send(eventType, data)` inside `onopen` to emit SSE events.
1097
+ */
1098
+ readonly port: IEventSourcePort;
1099
+ }
1100
+
881
1101
  /**
882
1102
  * Handler slot for one request type within a server scope.
883
1103
  *
884
- * @remarks The object is sealed by the kernel; only the `handler` property
885
- * may be reassigned. Set `handler` to `null` to leave the slot empty the
886
- * kernel will return `500 Internal Server Error` for any unhandled request.
1104
+ * @remarks
1105
+ * The object is sealed by the kernel; only the `handler` property may be
1106
+ * reassigned. Set `handler` to `null` to leave the slot empty the kernel
1107
+ * will return `500 Internal Server Error` for any unhandled request.
887
1108
  *
888
1109
  * @typeParam TRes - Expected return type of the handler function.
1110
+ * @typeParam TReq - Request object type passed to the handler. Defaults to
1111
+ * {@link IServerRequest} for the HTTP slot; specialised to
1112
+ * {@link IServerWebSocketRequest} and {@link IServerEventSourceRequest} for the WS and SSE
1113
+ * slots respectively.
889
1114
  */
890
- export interface IServerRequestHandler<TRes> {
1115
+ export interface IServerRequestHandler<TRes, TReq extends IServerRequest = IServerRequest> {
891
1116
  /**
892
1117
  * The function invoked for each incoming request of this type.
893
1118
  *
894
- * @remarks The kernel passes the parsed {@link IServerRequest} as the
895
- * sole argument and awaits any returned `Promise` before writing the
896
- * response.
1119
+ * @remarks
1120
+ * The kernel passes the parsed request as the sole argument and awaits
1121
+ * any returned `Promise` before writing the response.
897
1122
  */
898
- handler: ((request: IServerRequest) => TRes | Promise<TRes>) | null;
1123
+ handler: ((request: TReq) => TRes | Promise<TRes>) | null;
899
1124
  }
900
1125
 
901
1126
  /**
@@ -915,15 +1140,23 @@ export interface IServerScope {
915
1140
  /**
916
1141
  * WebSocket upgrade handler.
917
1142
  *
918
- * @remarks Reserved — not yet implemented by the kernel.
1143
+ * @remarks
1144
+ * The handler receives an {@link IServerWebSocketRequest} that includes
1145
+ * `request.port`, a bidirectional {@link IWebSocket} connected to the
1146
+ * client. Assign event callbacks before the handler returns; the kernel
1147
+ * auto-opens the port's read loop afterwards.
919
1148
  */
920
- readonly ws: IServerRequestHandler<void>;
1149
+ readonly ws: IServerRequestHandler<void, IServerWebSocketRequest>;
921
1150
  /**
922
1151
  * Server-Sent Events handler.
923
1152
  *
924
- * @remarks Reserved — not yet implemented by the kernel.
1153
+ * @remarks
1154
+ * The handler receives an {@link IServerEventSourceRequest} that includes
1155
+ * `request.port`, an {@link IEventSourcePort} for pushing SSE events.
1156
+ * Assign `onopen` / `onclose` callbacks and call `port.send` inside
1157
+ * `onopen`.
925
1158
  */
926
- readonly es: IServerRequestHandler<void>;
1159
+ readonly es: IServerRequestHandler<void, IServerEventSourceRequest>;
927
1160
  }
928
1161
 
929
1162
  /**
@@ -963,6 +1196,8 @@ export interface ISiyuan {
963
1196
  readonly storage: IStorage;
964
1197
  /** JSON-RPC method registry. */
965
1198
  readonly rpc: IRpc;
1199
+ /** MCP method registry. */
1200
+ readonly mcp: IMcp;
966
1201
  /** Network client utilities (HTTP, WebSocket, SSE). */
967
1202
  readonly client: IClient;
968
1203
  /** Web request handler registry. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "siyuan",
3
- "version": "1.2.2-alpha.0",
3
+ "version": "1.2.2",
4
4
  "contributors": [
5
5
  "zuoez02",
6
6
  "Vanessa",
@@ -40,6 +40,7 @@
40
40
  "dependencies": {
41
41
  "@dop251/types-goja_nodejs-buffer": "latest",
42
42
  "@dop251/types-goja_nodejs-global": "latest",
43
- "@dop251/types-goja_nodejs-url": "latest"
43
+ "@dop251/types-goja_nodejs-url": "latest",
44
+ "zod": "latest"
44
45
  }
45
46
  }
package/siyuan.d.ts CHANGED
@@ -1,32 +1,23 @@
1
1
  import type {
2
2
  IGetDocInfo,
3
3
  IGetTreeStat,
4
- IMenuBaseDetail,
4
+ IKernelPlugin,
5
+ IKernelPluginState,
5
6
  IMenu,
7
+ IMenuBaseDetail,
8
+ IMenuItem,
9
+ IModels,
6
10
  IObject,
7
11
  IPosition,
8
- ISiyuan,
9
- IWebSocketData,
10
12
  IProtyle,
11
13
  IProtyleOptions,
12
- TProtyleAction,
13
- IMenuItem,
14
14
  IRefDefs,
15
+ ISiyuan,
16
+ IWebSocketData,
15
17
  TEditorMode,
18
+ TProtyleAction,
16
19
  } from "./types";
17
- import {
18
- Config,
19
- Custom,
20
- Lute,
21
- Protyle,
22
- Toolbar,
23
- subMenu,
24
- App,
25
- Files,
26
- Tab,
27
- Model,
28
- MobileCustom,
29
- } from "./types";
20
+ import {App, Config, Custom, Files, Lute, MobileCustom, Model, Protyle, subMenu, Tab, Toolbar,} from "./types";
30
21
 
31
22
  export * from "./types";
32
23
 
@@ -35,7 +26,7 @@ declare global {
35
26
  }
36
27
  }
37
28
 
38
- export type TDock = "file" | "outline" | "inbox" | "bookmark" | "tag" | "graph" | "globalGraph" | "backlink"
29
+ export type TDock = "file" | "outline" | "inbox" | "bookmark" | "tag" | "graph" | "globalGraph" | "backlink" | "agentChat"
39
30
 
40
31
  export type TTab = "Outline" | "Graph" | "Backlink" | "Asset" | "Editor" | "Search" | "siyuan-card"
41
32
 
@@ -230,6 +221,7 @@ export interface IEventBusMap {
230
221
  languageElements: HTMLElement[],
231
222
  protyle: IProtyle
232
223
  };
224
+ "kernel-plugin-state-change": IKernelPluginState;
233
225
  }
234
226
 
235
227
  export interface IPluginDockTab {
@@ -378,21 +370,11 @@ export function exitSiYuan(): void
378
370
 
379
371
  export function getAllEditor(): Protyle[]
380
372
 
373
+ export function saveExportFile(uri: string, msgId?: string): Promise<void>;
374
+
381
375
  export function getAllTabs(type?: TTab | string): Tab[]
382
376
 
383
- export function getAllModels(): {
384
- editor: [],
385
- graph: [],
386
- asset: [],
387
- outline: [],
388
- backlink: [],
389
- search: [],
390
- inbox: [],
391
- files: [],
392
- bookmark: [],
393
- tag: [],
394
- custom: [],
395
- }
377
+ export function getAllModels(): IModels
396
378
 
397
379
  export function openSetting(app: App): Dialog | undefined;
398
380
 
@@ -443,6 +425,7 @@ export function hideMessage(id?: string): void;
443
425
  export abstract class Plugin {
444
426
  eventBus: EventBus;
445
427
  i18n: IObject;
428
+ kernel: IKernelPlugin;
446
429
  data: any;
447
430
  displayName: string;
448
431
  readonly name: string;
@@ -534,6 +517,24 @@ export abstract class Plugin {
534
517
 
535
518
  addCommand(options: ICommand): void;
536
519
 
520
+ /**
521
+ * 按名称取密钥值(来自「设置 → 密钥和变量」的密钥库)。找不到时返回空字符串。
522
+ * 密钥在内核侧加密存储,此处读到的是运行时明文;仅在本地管理员身份下可用。
523
+ */
524
+ getSecret(name: string): string;
525
+
526
+ /**
527
+ * 按名称取变量值(来自「设置 → 密钥和变量」的变量库)。找不到时返回空字符串。
528
+ * 变量以明文存储,用于非敏感配置。
529
+ */
530
+ getVariable(name: string): string;
531
+
532
+ addAgentAction(options: {
533
+ name: string,
534
+ description: string,
535
+ handler: (args: Record<string, unknown>, app: App) => Promise<{ result?: string, error?: string }>,
536
+ }): string;
537
+
537
538
  addFloatLayer(options: {
538
539
  refDefs: IRefDefs[],
539
540
  x?: number,