@lengmoxxl/sdk 0.1.6 → 0.1.8

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
@@ -1,8 +1,7 @@
1
1
  # @lengmoxxl/sdk:接口参考
2
2
 
3
- 本参考对应 SDK `0.1.5`:连接本机或 SSH 机器上的旺财后台进程,提供终端、文件与子进程
4
- 操作。插件入口与生命周期见 [插件开发手册](../docs/plugins.md),进程关系见
5
- [架构说明](../docs/architecture.md)。
3
+ SDK 连接本机或 SSH 机器上的旺财后台进程,提供终端、文件与子进程操作。插件入口与生命周期见
4
+ [插件开发手册](../docs/plugins.md),进程关系见 [架构说明](../docs/architecture.md)。
6
5
 
7
6
  ## 插件上下文
8
7
 
@@ -63,8 +62,9 @@
63
62
  ## 工作区协议
64
63
 
65
64
  放入 init.ts `workspaces` 的插件用 `ui.handle` 提供:`workspaces()`(`WorkspaceRow[]`,
66
- 按显示顺序)、`workspace-menu()`(`WorkspaceMenuItem[]`)、`workspace-create({ key })`
67
- (返回 `{ id }`)、`workspace-select({ id })`、`workspace-close({ id })`、
65
+ 按显示顺序;宿主可能把行画进它自己的分组,组内仍按此顺序)、
66
+ `workspace-menu()`(`WorkspaceMenuItem[]`)、`workspace-create({ key })`(返回 `{ id }`)、
67
+ `workspace-select({ id })`、`workspace-close({ id })`、
68
68
  `workspace-rename({ id, name })`(名字为空表示交回 provider 自己跟随的名字)、
69
69
  `workspace-move({ id, before? })`(`before` 是目标工作区 id,不是索引;省略为末尾,且只
70
70
  允许同一 provider 内拖动)。
@@ -72,12 +72,16 @@
72
72
  provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error` 由 provider 给出并在重试成功后清除,
73
73
  宿主不禁用带 `error` 的菜单项。
74
74
 
75
+ `WorkspaceRow.status` 是那行里 agent 的状态,写 `'idle'`、`'working'` 或 `'finished'`,缺省按
76
+ `'idle'` 画;宿主把它在行末画成一个图标,点 `'finished'` 的图标只在窗口里清成 `'idle'`(provider
77
+ 自己那份不变),那一行报出别的状态之后再报 `'finished'` 才会重新画出来。
78
+
75
79
  约定的公共主题:`workspaces`(行变化,App 重新请求列表)、`workspace:active`
76
80
  (`WorkspaceActive | null`,provider 发布)、`workspace:query`(视图请求 provider 再发布
77
81
  当前工作区)、`onclick`(`FileClick`)。
78
82
 
79
- 字段类型见 SDK 导出的 `WorkspaceRow`、`WorkspaceMenuItem`、`Machine`、`WorkspaceActive`
80
- 与 `FileClick`,定义在 `sdk/types.ts`。
83
+ 字段类型见 SDK 导出的 `WorkspaceRow`、`WorkspaceStatus`、`WorkspaceMenuItem`、`Machine`、
84
+ `WorkspaceActive` 与 `FileClick`,定义在 `sdk/types.ts`。
81
85
 
82
86
  ## 机器、终端与子进程
83
87
 
@@ -87,10 +91,12 @@ provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error
87
91
 
88
92
  `MachineState` 含 `status`、`error?`、`sessions`、`generation`。
89
93
 
90
- 断线会自动重连并重新 attach 已挂载的终端与进程,失败的请求与输入不自动重放,后台进程重启时旧 Pty 与进程失效。
94
+ 断线会自动重连并重新 attach 已挂载的终端、进程与文件订阅,失败的请求与输入不自动重放,后台进程重启时
95
+ 旧 Pty 与进程失效。
91
96
 
92
- `connection.pty`:`list()`、`cwd(id)`、`create(size?, cwd?)`(默认 24 行 80 列;cwd 用绝对目录,
93
- 省略时在后台进程的 HOME 下启动)、`attach(id)`(复用句柄)、`close(id)`(结束 shell 与后台子孙进程)。
97
+ `connection.pty`:`list()`、`cwd(id)`、`create(size?, options?)`(默认 24 行 80 列;`options.cwd` 用绝对
98
+ 目录,省略时在后台进程的 HOME 下启动;`options.env` 是额外给这个 shell 的环境变量,值都是字符串)、
99
+ `attach(id)`(复用句柄)、`close(id)`(结束 shell 与后台子孙进程)。
94
100
 
95
101
  `Size` 用整数:rows 2–300,cols 2–500。
96
102
 
@@ -110,6 +116,12 @@ provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error
110
116
 
111
117
  路径为绝对路径,`readFile` 上限 16 MiB;SDK 不提供写入、删除或上传接口。
112
118
 
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
+ 三类齐全。
124
+
113
125
  `connection.subprocess.exec(program, args, options)`:`args` 是独立 argv(不经 shell 展开),
114
126
  `options.cwd` 是绝对工作目录,`options.env` 覆盖继承的环境变量;返回
115
127
  `{ stdout: Uint8Array; stderr: Uint8Array; code: number }`。非零退出码仍返回结果,无法启动、
@@ -119,7 +131,7 @@ provider 要保证工作区 id 在所有 provider 的行中唯一识别;`error
119
131
 
120
132
  `connection.subprocess.spawn(program, args, options)`(`options` 同 `exec`)启动一个
121
133
  长期存活的进程,stdin、stdout、stderr 都是管道,返回 `ProcessInfo = { id, pid, exit_code }`;
122
- `attach(id)` 复用句柄(未 attach 前不接收输出),`close(id)` 结束进程及其进程组;进程归后台进程所有,不 `close` 就一直留着。
134
+ `attach(id)` 复用句柄(未 attach 前不接收输出),`close(id)` 结束进程及其进程组;进程归后台进程所有,除 `close` 外,只有起它或最后 `attach` 它的那条连接断开后 60 秒内没有连接再 `attach`,节点才会自己收回。
123
135
 
124
136
  `Process`:`id`、`onData(callback)`、`onExit(callback)`、`write(data)`。
125
137
 
@@ -1,4 +1,4 @@
1
- import type { ConnectionOptions, MachineState, Session, Size, DirectoryEntry, ExecOptions, ExecResult, ProcessInfo } from './types';
1
+ import type { ConnectionOptions, MachineState, Session, Size, PtyOptions, DirectoryEntry, ExecOptions, ExecResult, ProcessInfo, FileChanges, WatchOptions } from './types';
2
2
  import { Process } from './process';
3
3
  import { Pty } from './pty';
4
4
  export declare class MachineConnection {
@@ -6,6 +6,7 @@ export declare class MachineConnection {
6
6
  private listeners;
7
7
  private terminals;
8
8
  private processes;
9
+ private watchers;
9
10
  port?: number;
10
11
  private instance?;
11
12
  readonly ready: Promise<void>;
@@ -31,7 +32,7 @@ export declare class MachineConnection {
31
32
  pty: {
32
33
  list: () => Promise<Session[]>;
33
34
  cwd: (id: string) => Promise<string>;
34
- create: (size?: Size, cwd?: string) => Promise<Session>;
35
+ create: (size?: Size, options?: PtyOptions) => Promise<Session>;
35
36
  attach: (id: string) => Promise<Pty>;
36
37
  close: (id: string) => Promise<void>;
37
38
  };
@@ -41,6 +42,7 @@ export declare class MachineConnection {
41
42
  } | null>;
42
43
  readDirectory: (path: string) => Promise<DirectoryEntry[]>;
43
44
  readFile: (path: string) => Promise<Uint8Array>;
45
+ watch: (path: string, options: WatchOptions, callback: (changes: FileChanges) => void) => Promise<() => void>;
44
46
  };
45
47
  subprocess: {
46
48
  exec: (program: string, args: string[], options: ExecOptions) => Promise<ExecResult>;
package/dist/index.cjs CHANGED
@@ -206,6 +206,7 @@ var MachineConnection = class {
206
206
  listeners = /* @__PURE__ */ new Set();
207
207
  terminals = /* @__PURE__ */ new Map();
208
208
  processes = /* @__PURE__ */ new Map();
209
+ watchers = /* @__PURE__ */ new Map();
209
210
  // Where the daemon listens: the window reaches it with this.
210
211
  port;
211
212
  instance;
@@ -250,6 +251,7 @@ var MachineConnection = class {
250
251
  this.terminals.clear();
251
252
  for (const process2 of this.processes.values()) process2.release();
252
253
  this.processes.clear();
254
+ this.watchers.clear();
253
255
  this.state.status = "disconnected";
254
256
  this.state.error = void 0;
255
257
  this.publish();
@@ -258,7 +260,7 @@ var MachineConnection = class {
258
260
  pty = {
259
261
  list: () => this.request("list"),
260
262
  cwd: (id) => this.request("cwd", { session_id: id }),
261
- create: (size = { rows: 24, cols: 80 }, cwd) => this.request("create", { ...size, cwd }),
263
+ create: (size = { rows: 24, cols: 80 }, options = {}) => this.request("create", { ...size, ...options }),
262
264
  attach: async (id) => {
263
265
  const existing = this.terminals.get(id);
264
266
  if (existing) return existing;
@@ -287,6 +289,22 @@ var MachineConnection = class {
287
289
  readFile: async (path) => {
288
290
  const result = await this.request("read_file", { path });
289
291
  return Buffer.from(result.data, "base64");
292
+ },
293
+ watch: async (path, options, callback) => {
294
+ const watchId = `watch-${++this.serial}`;
295
+ const recursive = options.recursive ?? false;
296
+ this.watchers.set(watchId, { recursive, path, callback });
297
+ try {
298
+ await this.request("watch", { watch_id: watchId, path, recursive });
299
+ } catch (error) {
300
+ this.watchers.delete(watchId);
301
+ throw error;
302
+ }
303
+ return () => {
304
+ this.watchers.delete(watchId);
305
+ void this.request("unwatch", { watch_id: watchId }).catch(() => {
306
+ });
307
+ };
290
308
  }
291
309
  };
292
310
  subprocess = {
@@ -403,6 +421,9 @@ var MachineConnection = class {
403
421
  } else if (message.event === "sessions_changed") {
404
422
  void this.refresh().catch(() => {
405
423
  });
424
+ } else if (message.event === "fs_changed") {
425
+ const watcher = this.watchers.get(message.watch_id);
426
+ if (watcher) watcher.callback({ created: message.created, modified: message.modified, removed: message.removed });
406
427
  }
407
428
  }
408
429
  } catch (error) {
@@ -451,6 +472,13 @@ var MachineConnection = class {
451
472
  this.processes.delete(id);
452
473
  }
453
474
  }
475
+ for (const [watchId, watcher] of this.watchers) {
476
+ try {
477
+ await this.request("watch", { watch_id: watchId, path: watcher.path, recursive: watcher.recursive });
478
+ } catch {
479
+ if (!current()) return;
480
+ }
481
+ }
454
482
  if (!current()) return;
455
483
  this.state.status = "connected";
456
484
  this.state.error = void 0;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { MachineConnection } from './connection';
2
2
  import type { ConnectionOptions } from './types';
3
3
  export { agentCommand } from './agent';
4
- export type { ConnectionOptions, FileClick, Machine, MachineState, Session, Size, Output, Snapshot, TerminalEvent, DirectoryEntry, ExecOptions, ExecResult, ProcessEvent, ProcessInfo, ProcessOutput, InstallAgent, Profile, Theme, WorkspaceActive, WorkspaceRow, WorkspaceMenuItem } from './types';
4
+ export type { ConnectionOptions, FileClick, Machine, MachineState, Session, Size, PtyOptions, Output, Snapshot, TerminalEvent, DirectoryEntry, FileChanges, WatchOptions, ExecOptions, ExecResult, ProcessEvent, ProcessInfo, ProcessOutput, InstallAgent, Profile, Theme, WorkspaceActive, WorkspaceRow, WorkspaceStatus, WorkspaceMenuItem } from './types';
5
5
  export type { MachineConnection } from './connection';
6
6
  export type { Process } from './process';
7
7
  export type { Pty } from './pty';
package/dist/types.d.ts CHANGED
@@ -4,7 +4,8 @@ export type InstallAgent = (host: string, signal?: AbortSignal) => Promise<void>
4
4
  * The workspace protocol. A plugin that init.ts lists under `workspaces` is a workspace provider, and
5
5
  * the app drives it through these `ui` methods:
6
6
  *
7
- * workspaces() -> WorkspaceRow[] one flat row per workspace, in draw order
7
+ * workspaces() -> WorkspaceRow[] one flat row per workspace, in draw order; the window may
8
+ * draw rows under groups of its own, whose rows keep this order
8
9
  * workspace-menu() -> WorkspaceMenuItem[] what the menu offers to open; an entry that carries
9
10
  * an error stays listed with its reason, and a click retries it
10
11
  * workspace-create({ key }) -> { id: string } the id of the workspace it made
@@ -20,11 +21,14 @@ export type InstallAgent = (host: string, signal?: AbortSignal) => Promise<void>
20
21
  * 'workspace:active' the workspace in front, or null; published by its provider
21
22
  * 'workspace:query' a view asks for 'workspace:active' again
22
23
  */
24
+ /** What the agent in a workspace is doing, which the app draws at the right end of its row. */
25
+ export type WorkspaceStatus = 'idle' | 'working' | 'finished';
23
26
  export interface WorkspaceRow {
24
27
  id: string;
25
28
  label: string;
26
29
  machine: string;
27
30
  running: boolean;
31
+ status?: WorkspaceStatus;
28
32
  }
29
33
  export interface WorkspaceMenuItem {
30
34
  key: string;
@@ -67,6 +71,11 @@ export interface Size {
67
71
  rows: number;
68
72
  cols: number;
69
73
  }
74
+ /** What a session is created with: the directory its shell opens in, and what it is told besides the node's environment. */
75
+ export interface PtyOptions {
76
+ cwd?: string;
77
+ env?: Record<string, string>;
78
+ }
70
79
  export interface Session extends Size {
71
80
  id: string;
72
81
  title: string;
@@ -116,6 +125,14 @@ export interface DirectoryEntry {
116
125
  name: string;
117
126
  isDirectory: boolean;
118
127
  }
128
+ export interface FileChanges {
129
+ created: string[];
130
+ modified: string[];
131
+ removed: string[];
132
+ }
133
+ export interface WatchOptions {
134
+ recursive?: boolean;
135
+ }
119
136
  export interface ExecOptions {
120
137
  cwd: string;
121
138
  env?: Record<string, string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lengmoxxl/sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
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": {