@lengmoxxl/sdk 0.1.8 → 0.1.9

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/README.md CHANGED
@@ -22,25 +22,24 @@ SDK 连接本机或 SSH 机器上的旺财后台进程,提供终端、文件
22
22
  `ui.handle` 注册的方法名不能含 `:`;`ui.publish` 与 `ui.subscribe` 的主题由宿主加插件 id 前缀,
23
23
  `global` 用原始主题,自定义主题要自己加命名空间(如 `directory-demo:opened`)。
24
24
 
25
- `connect({})` 连接本机,`connect({ host })` 连接 SSH;`env` 是那台机器上节点启动时的环境。
26
-
27
- 宿主为相同机器复用连接并计数,每次取得连接对应一次 `disconnect()`。
25
+ `Bus`:`publish(topic, data): Promise<void>`、`subscribe<T>(topic, callback): () => void`。
28
26
 
29
27
  `Dispose = () => void | Promise<void>`,卸载时清理资源,不要关闭用户仍要保留的 PTY。
30
28
 
31
- `Bus`:`publish(topic, data): Promise<void>`、`subscribe<T>(topic, callback): () => void`。
29
+ `connect({})` 连接本机,`connect({ host })` 连接 SSH;`env` 是那台机器上节点启动时的环境。
30
+ 宿主为相同机器复用连接并计数,每次取得连接对应一次 `disconnect()`。
32
31
 
33
32
  `host.dataDirectory` 默认 `~/.local/share/wangcai/data/<plugin-id>`。
34
33
 
35
- `host.config` 是浅合并,避免把自定义字段命名为 `theme`,配色键见
36
- [README 主题参数](../README.md#主题参数)。
37
-
38
34
  ## 标签页
39
35
 
40
- `host.tabs({ id, title, tooltip?, workspaceId?, onClose?, mount })`:`id`、`title` 必填,
41
- `tooltip?` 是标签上的悬停提示(缺省用 `title`),`workspaceId` 缺省用当前工作区;
42
- `mount(container)` 返回的 `TabContent.dispose` 必填,`onSelect?` 在选中、工作区刷新或侧栏重新显示时
43
- 都可能调用,`onClose?` 在移除标签页时先于 `dispose` 调用。
36
+ `host.tabs({ id, title, tooltip?, workspaceId?, onClose?, mount })`:`id`、`title` 必填。
37
+ `tooltip` 是标签上的悬停提示,缺省用 `title`;`workspaceId` 缺省用当前工作区。
38
+
39
+ `mount(container)` 返回的 `TabContent.dispose` 必填,用于清理标签页资源。
40
+ `onClose?` 在移除标签页时先于 `dispose` 调用。
41
+
42
+ `onSelect?` 在选中、工作区刷新或侧栏重新显示时都可能调用。
44
43
 
45
44
  相同插件 id、工作区 id、标签 id 再次调用 `host.tabs` 会复用并选中;尚无工作区时打开的标签页
46
45
  会在第一个工作区激活时绑定到它。
@@ -56,34 +55,51 @@ SDK 连接本机或 SSH 机器上的旺财后台进程,提供终端、文件
56
55
 
57
56
  ## 插件配置
58
57
 
58
+ `host.config` 是浅合并,避免把自定义字段命名为 `theme`,配色键见
59
+ [README 主题参数](../README.md#主题参数)。
60
+
59
61
  插件 `config` 是嵌套对象,叶子声明 `type` 与可选 `default`;宿主只保留 schema 声明的键,
60
62
  缺失或类型不符时取 default,`array` 内部条目由插件自己验证。
61
63
 
62
64
  ## 工作区协议
63
65
 
64
- 放入 init.ts `workspaces` 的插件用 `ui.handle` 提供:`workspaces()`(`WorkspaceRow[]`,
65
- 按显示顺序;宿主可能把行画进它自己的分组,组内仍按此顺序)、
66
- `workspace-menu()`(`WorkspaceMenuItem[]`)、`workspace-create({ key })`(返回 `{ id }`)、
67
- `workspace-select({ id })`、`workspace-close({ id })`、
68
- `workspace-rename({ id, name })`(名字为空表示交回 provider 自己跟随的名字)、
69
- `workspace-move({ id, before? })`(`before` 是目标工作区 id,不是索引;省略为末尾,且只
70
- 允许同一 provider 内拖动)。
66
+ 放入 init.ts `workspaces` 的插件用 `ui.handle` 提供以下方法:
67
+
68
+ | 方法 | 返回值或约定 |
69
+ | --- | --- |
70
+ | `workspaces()` | 返回 `WorkspaceRow[]`,按显示顺序;宿主分组后,组内仍保持此顺序 |
71
+ | `workspace-menu()` | 返回 `WorkspaceMenuItem[]` |
72
+ | `workspace-create({ key })` | 返回 `{ id }` |
73
+ | `workspace-select({ id })` | 选中工作区 |
74
+ | `workspace-close({ id })` | 关闭工作区 |
75
+ | `workspace-rename({ id, name })` | 名字为空表示交回 provider 自己跟随的名字 |
76
+ | `workspace-move({ id, before? })` | `before` 是目标工作区 id,不是索引;省略为末尾,只允许同一 provider 内拖动 |
77
+
78
+ provider 要保证工作区 id 在所有 provider 的行中唯一识别。
71
79
 
72
- provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error` 由 provider 给出并在重试成功后清除,
73
- 宿主不禁用带 `error` 的菜单项。
80
+ `error` 由 provider 给出并在重试成功后清除,宿主不禁用带 `error` 的菜单项。
74
81
 
75
- `WorkspaceRow.status` 是那行里 agent 的状态,写 `'idle'`、`'working'` 或 `'finished'`,缺省按
76
- `'idle'` 画;宿主把它在行末画成一个图标,点 `'finished'` 的图标只在窗口里清成 `'idle'`(provider
77
- 自己那份不变),那一行报出别的状态之后再报 `'finished'` 才会重新画出来。
82
+ `WorkspaceRow.status` 是工作区里 agent 的状态,取 `'idle'`、`'working'` 或 `'finished'`,
83
+ 缺省为 `'idle'`,宿主在行末显示状态图标。
78
84
 
79
- 约定的公共主题:`workspaces`(行变化,App 重新请求列表)、`workspace:active`
80
- (`WorkspaceActive | null`,provider 发布)、`workspace:query`(视图请求 provider 再发布
81
- 当前工作区)、`onclick`(`FileClick`)。
85
+ 点击工作区行或行末图标,只会在窗口里将状态清成 `'idle'`,不修改 provider 的状态。
86
+ 该工作区报出其他状态后,再报 `'finished'` 才会重新显示完成状态。
87
+
88
+ 公共主题约定如下:
89
+
90
+ | 主题 | 数据或约定 |
91
+ | --- | --- |
92
+ | `workspaces` | 行变化时发布,App 重新请求列表 |
93
+ | `workspace:active` | provider 发布 `WorkspaceActive \| null` |
94
+ | `workspace:query` | 视图请求 provider 再发布当前工作区 |
95
+ | `onclick` | `FileClick` |
82
96
 
83
97
  字段类型见 SDK 导出的 `WorkspaceRow`、`WorkspaceStatus`、`WorkspaceMenuItem`、`Machine`、
84
98
  `WorkspaceActive` 与 `FileClick`,定义在 `sdk/types.ts`。
85
99
 
86
- ## 机器、终端与子进程
100
+ ## 机器接口
101
+
102
+ ### 连接与重连
87
103
 
88
104
  `MachineConnection`:`ready`(首次连接就绪)、`state`、`port`(本地 loopback 端口,SSH 时
89
105
  为转发端口)、`onState(callback)`、`retain()`(连接层使用)、`disconnect()`,以及 `pty`、`fs`、
@@ -94,11 +110,22 @@ provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error
94
110
  断线会自动重连并重新 attach 已挂载的终端、进程与文件订阅,失败的请求与输入不自动重放,后台进程重启时
95
111
  旧 Pty 与进程失效。
96
112
 
97
- `connection.pty`:`list()`、`cwd(id)`、`create(size?, options?)`(默认 24 行 80 列;`options.cwd` 用绝对
98
- 目录,省略时在后台进程的 HOME 下启动;`options.env` 是额外给这个 shell 的环境变量,值都是字符串)、
99
- `attach(id)`(复用句柄)、`close(id)`(结束 shell 与后台子孙进程)。
113
+ ### 终端(PTY)
100
114
 
101
- `Size` 用整数:rows 2–300,cols 2–500。
115
+ `connection.pty` 提供以下方法:
116
+
117
+ | 方法 | 作用 |
118
+ | --- | --- |
119
+ | `list()` | 列出会话 |
120
+ | `cwd(id)` | 查询会话工作目录 |
121
+ | `create(size?, options?)` | 创建终端会话 |
122
+ | `attach(id)` | 挂载会话,复用句柄 |
123
+ | `close(id)` | 结束 shell 与后台子孙进程 |
124
+
125
+ `create` 默认使用 24 行 80 列。`size` 使用 `Size`,字段为整数:rows 2–300,cols 2–500。
126
+
127
+ `options.cwd` 使用绝对目录,省略时在后台进程的 HOME 下启动;
128
+ `options.env` 是额外给这个 shell 的环境变量,值都是字符串。
102
129
 
103
130
  `Session` 有 `id, title`、`pid, rows, cols`、`exit_code: number | null`。
104
131
 
@@ -111,27 +138,41 @@ provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error
111
138
 
112
139
  `receive` 与 `release(error?)` 由连接层使用。
113
140
 
141
+ ### 文件
142
+
114
143
  `connection.fs`:`stat(path)` 返回 `{ isDirectory: boolean } | null`、`readDirectory(path)` 返回
115
144
  `DirectoryEntry[]`(`{ name, isDirectory }`)、`readFile(path)` 返回原始字节。
116
145
 
117
146
  路径为绝对路径,`readFile` 上限 16 MiB;SDK 不提供写入、删除或上传接口。
118
147
 
119
- `connection.fs.watch(path, options, callback)` 订阅该路径的变化,`options.recursive` 决定是否含子目录,
120
- 返回的 `() => void` 关闭订阅。回调收到的是 `FileChanges = { created, modified, removed }`,一个安静窗口
121
- (约 200ms 没新事件,最长 2 秒)一批,路径按订阅时给的根路径书写;连接彻底关闭(最后一个持有者
122
- `disconnect()`)时一并结束。macOS 走 FSEvents,创建与删除多数时候报成 `modified`;Linux 走 inotify,
123
- 三类齐全。
148
+ `connection.fs.watch(path, options, callback)` 订阅该路径的变化,`options.recursive` 决定是否含子目录。
149
+ 返回的 `() => void` 关闭订阅;连接彻底关闭(最后一个持有者 `disconnect()`)时,订阅也随之结束。
124
150
 
125
- `connection.subprocess.exec(program, args, options)`:`args` 是独立 argv(不经 shell 展开),
126
- `options.cwd` 是绝对工作目录,`options.env` 覆盖继承的环境变量;返回
127
- `{ stdout: Uint8Array; stderr: Uint8Array; code: number }`。非零退出码仍返回结果,无法启动、
128
- 超时、输出超限或被信号终止时 reject;stdin 关闭,长时间或交互用 `subprocess.spawn` 或 PTY。
151
+ 回调收到 `FileChanges = { created, modified, removed }`,路径按订阅时给的根路径书写。
152
+ 事件按安静窗口批量投递:约 200ms 没有新事件时发出一批,最长等待 2 秒。
129
153
 
130
- 执行上限 15 秒,SDK 等待响应上限 20 秒,stdout 与 stderr 各最多 8 MiB,断线取消未完成的 exec。
154
+ macOS 使用 FSEvents,创建与删除多数时候报成 `modified`;Linux 使用 inotify,三类事件齐全。
155
+
156
+ ### 短期命令(exec)
157
+
158
+ `connection.subprocess.exec(program, args, options)`:`args` 是独立 argv,不经 shell 展开;
159
+ `options.cwd` 是绝对工作目录,`options.env` 覆盖继承的环境变量。
160
+
161
+ 返回 `{ stdout: Uint8Array; stderr: Uint8Array; code: number }`,非零退出码仍返回结果。
162
+ 无法启动、超时、输出超限或被信号终止时 reject。
163
+
164
+ stdin 关闭,执行上限 15 秒,SDK 等待响应上限 20 秒,stdout 与 stderr 各最多 8 MiB;
165
+ 断线取消未完成的 exec。长时间或交互操作用 `subprocess.spawn` 或 PTY。
166
+
167
+ ### 长期进程(spawn)
131
168
 
132
169
  `connection.subprocess.spawn(program, args, options)`(`options` 同 `exec`)启动一个
133
- 长期存活的进程,stdin、stdout、stderr 都是管道,返回 `ProcessInfo = { id, pid, exit_code }`;
134
- `attach(id)` 复用句柄(未 attach 前不接收输出),`close(id)` 结束进程及其进程组;进程归后台进程所有,除 `close` 外,只有起它或最后 `attach` 它的那条连接断开后 60 秒内没有连接再 `attach`,节点才会自己收回。
170
+ 长期存活的进程,stdin、stdout、stderr 都是管道,返回 `ProcessInfo = { id, pid, exit_code }`。
171
+
172
+ `attach(id)` 复用句柄,未 attach 前不接收输出;`close(id)` 结束进程及其进程组。
173
+
174
+ 进程归后台进程所有。除显式 `close` 外,只有启动它或最后 attach 它的那条连接断开后,
175
+ 60 秒内没有任何连接再 attach,该进程才会被节点自动回收。
135
176
 
136
177
  `Process`:`id`、`onData(callback)`、`onExit(callback)`、`write(data)`。
137
178
 
@@ -165,3 +206,9 @@ connection.disconnect();
165
206
  `agentCommand(command, env)` 返回拼接后的 shell 命令:在 PATH 前加入远端 `$HOME/.local/bin`,
166
207
  再带上当前进程的 WANGCAI_HOME 与配置要求的环境;值里的 `$NAME` 留给远端 shell 展开。命令本身
167
208
  不转义,只传可信内容。
209
+ ## 壳与内核的约定
210
+
211
+ `@lengmoxxl/sdk/bridge` 是壳(App)与业务内核共享的那份契约:窗口的三个 scheme、桥的两个 channel、
212
+ 壳向窗口发的 topic、界面字体、安装阶段与跨桥的类型,以及 `contractVersion` —— 内核启动时把它报给壳,
213
+ 壳据此拒绝一个面向旧契约的构建。壳与内核各取一份 SDK,所以改这份契约要发新版本,两边对不上时靠这个
214
+ 版本号发现。插件不需要它:插件用到的是上文列出的上下文。
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // bridge.ts
21
+ var bridge_exports = {};
22
+ __export(bridge_exports, {
23
+ contractVersion: () => contractVersion,
24
+ corePageTopic: () => corePageTopic,
25
+ coreScheme: () => coreScheme,
26
+ coreStatusTopic: () => coreStatusTopic,
27
+ fullscreenTopic: () => fullscreenTopic,
28
+ installStatusesTopic: () => installStatusesTopic,
29
+ link: () => link,
30
+ pluginPageTopic: () => pluginPageTopic,
31
+ pluginScheme: () => pluginScheme,
32
+ pluginsHost: () => pluginsHost,
33
+ pluginsUrl: () => pluginsUrl,
34
+ previewMessage: () => previewMessage,
35
+ previewScheme: () => previewScheme,
36
+ previewUrl: () => previewUrl,
37
+ stageNames: () => stageNames,
38
+ uiFont: () => uiFont
39
+ });
40
+ module.exports = __toCommonJS(bridge_exports);
41
+ var contractVersion = 1;
42
+ var uiFont = '"DejaVuSansM Nerd Font Mono", monospace';
43
+ var coreScheme = "wangcai-core";
44
+ var pluginScheme = "wangcai-plugin";
45
+ var previewScheme = "wangcai-preview";
46
+ var pluginsHost = "plugins";
47
+ var pluginsUrl = `${pluginScheme}://${pluginsHost}/`;
48
+ var previewUrl = `${previewScheme}://preview/`;
49
+ var previewMessage = "wangcai-preview";
50
+ var link = { invoke: "wangcai:invoke", channel: "wangcai:channel" };
51
+ var pluginPageTopic = "plugin-page";
52
+ var installStatusesTopic = "install-statuses";
53
+ var corePageTopic = "core-page";
54
+ var coreStatusTopic = "core-status";
55
+ var fullscreenTopic = "fullscreen";
56
+ var stageNames = { cloning: "\u514B\u9686\u4E2D", updating: "\u540C\u6B65\u4E2D", installing: "\u5B89\u88C5\u4F9D\u8D56", building: "\u6784\u5EFA\u4E2D", ready: "\u5C31\u7EEA", failed: "\u5931\u8D25" };
57
+ // Annotate the CommonJS export names for ESM import in node:
58
+ 0 && (module.exports = {
59
+ contractVersion,
60
+ corePageTopic,
61
+ coreScheme,
62
+ coreStatusTopic,
63
+ fullscreenTopic,
64
+ installStatusesTopic,
65
+ link,
66
+ pluginPageTopic,
67
+ pluginScheme,
68
+ pluginsHost,
69
+ pluginsUrl,
70
+ previewMessage,
71
+ previewScheme,
72
+ previewUrl,
73
+ stageNames,
74
+ uiFont
75
+ });
@@ -0,0 +1,60 @@
1
+ export declare const contractVersion = 1;
2
+ export declare const uiFont = "\"DejaVuSansM Nerd Font Mono\", monospace";
3
+ export declare const coreScheme = "wangcai-core";
4
+ export declare const pluginScheme = "wangcai-plugin";
5
+ export declare const previewScheme = "wangcai-preview";
6
+ export declare const pluginsHost = "plugins";
7
+ export declare const pluginsUrl = "wangcai-plugin://plugins/";
8
+ export declare const previewUrl = "wangcai-preview://preview/";
9
+ export declare const previewMessage = "wangcai-preview";
10
+ export declare const link: {
11
+ readonly invoke: "wangcai:invoke";
12
+ readonly channel: "wangcai:channel";
13
+ };
14
+ export declare const pluginPageTopic = "plugin-page";
15
+ export declare const installStatusesTopic = "install-statuses";
16
+ export declare const corePageTopic = "core-page";
17
+ export declare const coreStatusTopic = "core-status";
18
+ export declare const fullscreenTopic = "fullscreen";
19
+ /** How a run reaches the network: `proxy` is an http://, https:// or socks5:// address, and `mirror` is a
20
+ * prefix every https://github.com/ URL goes out through. */
21
+ export interface Network {
22
+ proxy?: string;
23
+ mirror?: string;
24
+ }
25
+ /** A plugin that is built and ready: where its files are, and how init.ts named it. */
26
+ export interface PluginEntry {
27
+ id: string;
28
+ workspaces: boolean;
29
+ directory: string;
30
+ config?: Record<string, unknown>;
31
+ }
32
+ /** What the window is told about a plugin that was loaded: its settings, its files, and why it failed. */
33
+ export interface PluginInfo {
34
+ id: string;
35
+ config: Record<string, unknown>;
36
+ ui?: string;
37
+ css?: string;
38
+ error?: string;
39
+ workspaces?: boolean;
40
+ }
41
+ export type InstallStage = 'cloning' | 'updating' | 'installing' | 'building' | 'ready' | 'failed';
42
+ export interface InstallStatus {
43
+ id: string;
44
+ stage: InstallStage;
45
+ message?: string;
46
+ }
47
+ export type CoreStatus = Omit<InstallStatus, 'id'>;
48
+ export declare const stageNames: Record<InstallStage, string>;
49
+ export interface WorkspaceGroup {
50
+ id: string;
51
+ name: string;
52
+ collapsed: boolean;
53
+ workspaces: string[];
54
+ after?: number;
55
+ }
56
+ /** What the shell's preload hands the window. */
57
+ export interface PluginBridge {
58
+ call(method: string, ...args: unknown[]): Promise<any>;
59
+ subscribe(event: string, callback: (data: unknown) => void): () => void;
60
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lengmoxxl/sdk",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Client SDK for WangCai: connect to a local or remote machine and drive its pty, files and git",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,6 +29,10 @@
29
29
  "types": "./dist/index.d.ts",
30
30
  "default": "./dist/index.cjs"
31
31
  },
32
+ "./bridge": {
33
+ "types": "./dist/bridge.d.ts",
34
+ "default": "./dist/bridge.cjs"
35
+ },
32
36
  "./channel": {
33
37
  "types": "./dist/channel.d.ts"
34
38
  },
@@ -39,7 +43,7 @@
39
43
  "registry": "https://registry.npmjs.org/"
40
44
  },
41
45
  "scripts": {
42
- "build": "esbuild index.ts --outfile=dist/index.cjs --bundle --platform=node --target=node22 --packages=external && tsc",
46
+ "build": "esbuild index.ts --outfile=dist/index.cjs --bundle --platform=node --target=node22 --packages=external && esbuild bridge.ts --outfile=dist/bridge.cjs --bundle --platform=node --target=node22 --packages=external && tsc",
43
47
  "prepack": "npm run build",
44
48
  "typecheck": "tsc --noEmit"
45
49
  },