@lengmoxxl/sdk 0.1.9 → 0.1.11

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
@@ -206,6 +206,7 @@ connection.disconnect();
206
206
  `agentCommand(command, env)` 返回拼接后的 shell 命令:在 PATH 前加入远端 `$HOME/.local/bin`,
207
207
  再带上当前进程的 WANGCAI_HOME 与配置要求的环境;值里的 `$NAME` 留给远端 shell 展开。命令本身
208
208
  不转义,只传可信内容。
209
+
209
210
  ## 壳与内核的约定
210
211
 
211
212
  `@lengmoxxl/sdk/bridge` 是壳(App)与业务内核共享的那份契约:窗口的三个 scheme、桥的两个 channel、
package/dist/bridge.cjs CHANGED
@@ -21,9 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var bridge_exports = {};
22
22
  __export(bridge_exports, {
23
23
  contractVersion: () => contractVersion,
24
- corePageTopic: () => corePageTopic,
24
+ coreId: () => coreId,
25
25
  coreScheme: () => coreScheme,
26
- coreStatusTopic: () => coreStatusTopic,
27
26
  fullscreenTopic: () => fullscreenTopic,
28
27
  installStatusesTopic: () => installStatusesTopic,
29
28
  link: () => link,
@@ -38,7 +37,7 @@ __export(bridge_exports, {
38
37
  uiFont: () => uiFont
39
38
  });
40
39
  module.exports = __toCommonJS(bridge_exports);
41
- var contractVersion = 1;
40
+ var contractVersion = 3;
42
41
  var uiFont = '"DejaVuSansM Nerd Font Mono", monospace';
43
42
  var coreScheme = "wangcai-core";
44
43
  var pluginScheme = "wangcai-plugin";
@@ -50,16 +49,14 @@ var previewMessage = "wangcai-preview";
50
49
  var link = { invoke: "wangcai:invoke", channel: "wangcai:channel" };
51
50
  var pluginPageTopic = "plugin-page";
52
51
  var installStatusesTopic = "install-statuses";
53
- var corePageTopic = "core-page";
54
- var coreStatusTopic = "core-status";
52
+ var coreId = "core";
55
53
  var fullscreenTopic = "fullscreen";
56
54
  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
55
  // Annotate the CommonJS export names for ESM import in node:
58
56
  0 && (module.exports = {
59
57
  contractVersion,
60
- corePageTopic,
58
+ coreId,
61
59
  coreScheme,
62
- coreStatusTopic,
63
60
  fullscreenTopic,
64
61
  installStatusesTopic,
65
62
  link,
package/dist/bridge.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const contractVersion = 1;
1
+ export declare const contractVersion = 3;
2
2
  export declare const uiFont = "\"DejaVuSansM Nerd Font Mono\", monospace";
3
3
  export declare const coreScheme = "wangcai-core";
4
4
  export declare const pluginScheme = "wangcai-plugin";
@@ -13,8 +13,7 @@ export declare const link: {
13
13
  };
14
14
  export declare const pluginPageTopic = "plugin-page";
15
15
  export declare const installStatusesTopic = "install-statuses";
16
- export declare const corePageTopic = "core-page";
17
- export declare const coreStatusTopic = "core-status";
16
+ export declare const coreId = "core";
18
17
  export declare const fullscreenTopic = "fullscreen";
19
18
  /** How a run reaches the network: `proxy` is an http://, https:// or socks5:// address, and `mirror` is a
20
19
  * prefix every https://github.com/ URL goes out through. */
@@ -44,7 +43,6 @@ export interface InstallStatus {
44
43
  stage: InstallStage;
45
44
  message?: string;
46
45
  }
47
- export type CoreStatus = Omit<InstallStatus, 'id'>;
48
46
  export declare const stageNames: Record<InstallStage, string>;
49
47
  export interface WorkspaceGroup {
50
48
  id: string;
@@ -23,6 +23,7 @@ export declare class MachineConnection {
23
23
  private epoch;
24
24
  private serial;
25
25
  private refs;
26
+ private call;
26
27
  constructor(options: ConnectionOptions);
27
28
  onState(callback: (state: MachineState) => void): () => void;
28
29
  private publish;
@@ -50,6 +51,12 @@ export declare class MachineConnection {
50
51
  attach: (id: string) => Promise<Process>;
51
52
  close: (id: string) => Promise<void>;
52
53
  };
54
+ /** The stream a map holds under `id`, or one made and attached now: a failed attach leaves neither. */
55
+ private openStream;
56
+ private closeStream;
57
+ /** A stream lives in the connection that asked for it, so it is attached to the one that is up again; one the
58
+ * node no longer has is released. False once the connection has moved on. */
59
+ private reattachStreams;
53
60
  private cleanup;
54
61
  private fail;
55
62
  private open;
package/dist/index.cjs CHANGED
@@ -41,22 +41,55 @@ var import_node_net = require("node:net");
41
41
  var import_node_util = require("node:util");
42
42
  var import_ws = __toESM(require("ws"));
43
43
 
44
+ // stream.ts
45
+ var Stream = class {
46
+ // What the node sent last, and what no reader has taken yet.
47
+ sequence = -1;
48
+ missed = [];
49
+ readers = /* @__PURE__ */ new Set();
50
+ /** Why the stream was released; a released stream takes no more events. */
51
+ failure;
52
+ /** Takes one event in order, unless a release or a later sequence makes it stale. */
53
+ deliver(event) {
54
+ if (this.failure || event.seq <= this.sequence) return;
55
+ this.sequence = event.seq;
56
+ if (!this.readers.size) this.missed.push(event);
57
+ for (const reader of this.readers) reader(event);
58
+ }
59
+ /** Replaces the order from a snapshot: what came before it is no longer what the stream holds. */
60
+ reset(seq) {
61
+ this.sequence = seq;
62
+ this.missed = [];
63
+ }
64
+ /** Everything from here on, and first what no reader has taken yet. */
65
+ onData(reader) {
66
+ this.readers.add(reader);
67
+ for (const event of this.missed) reader(event);
68
+ this.missed = [];
69
+ return () => {
70
+ this.readers.delete(reader);
71
+ };
72
+ }
73
+ release(error) {
74
+ this.failure = error;
75
+ this.readers.clear();
76
+ this.missed = [];
77
+ }
78
+ };
79
+
44
80
  // process.ts
45
81
  var CHUNK = 32 * 1024;
46
- var Process = class {
82
+ var Process = class extends Stream {
47
83
  constructor(id, request) {
84
+ super();
48
85
  this.id = id;
49
86
  this.request = request;
50
87
  }
51
- sequence = -1;
52
- error;
53
88
  ended = false;
54
89
  code;
55
- buffered = [];
56
- outputs = /* @__PURE__ */ new Set();
57
90
  exits = /* @__PURE__ */ new Set();
58
91
  receive(event) {
59
- if (this.error) return;
92
+ if (this.failure) return;
60
93
  if (event.event === "exit" || event.event === "lost") {
61
94
  this.ended = true;
62
95
  this.code = event.event === "exit" ? event.code : void 0;
@@ -64,19 +97,7 @@ var Process = class {
64
97
  this.exits.clear();
65
98
  return;
66
99
  }
67
- if (event.seq <= this.sequence) return;
68
- this.sequence = event.seq;
69
- const output = { data: event.data, seq: event.seq, stream: event.event };
70
- if (!this.outputs.size) this.buffered.push(output);
71
- for (const callback of this.outputs) callback(output);
72
- }
73
- onData(callback) {
74
- this.outputs.add(callback);
75
- for (const event of this.buffered) callback(event);
76
- this.buffered = [];
77
- return () => {
78
- this.outputs.delete(callback);
79
- };
100
+ this.deliver({ data: event.data, seq: event.seq, stream: event.event });
80
101
  }
81
102
  /** Its exit code, or nothing when the node lost the stream and the process may still be running. */
82
103
  onExit(callback) {
@@ -87,7 +108,7 @@ var Process = class {
87
108
  };
88
109
  }
89
110
  async write(data) {
90
- if (this.error) throw this.error;
111
+ if (this.failure) throw this.failure;
91
112
  const requests = [];
92
113
  for (let offset = 0; offset < data.length; offset += CHUNK) {
93
114
  const chunk = Buffer.from(data.subarray(offset, offset + CHUNK)).toString("base64");
@@ -96,39 +117,30 @@ var Process = class {
96
117
  await Promise.all(requests);
97
118
  }
98
119
  release(error = new Error("Process detached")) {
99
- this.error = error;
100
- this.outputs.clear();
120
+ super.release(error);
101
121
  this.exits.clear();
102
- this.buffered = [];
103
122
  }
104
123
  };
105
124
 
106
125
  // pty.ts
107
- var Pty = class {
126
+ var Pty = class extends Stream {
108
127
  constructor(id, request, remove) {
128
+ super();
109
129
  this.id = id;
110
130
  this.request = request;
111
131
  this.remove = remove;
112
132
  }
113
- sequence = -1;
114
- error;
115
133
  snapshot;
116
- buffered = [];
117
134
  snapshots = /* @__PURE__ */ new Set();
118
- outputs = /* @__PURE__ */ new Set();
119
135
  receive(event) {
120
- if (this.error) return;
136
+ if (this.failure) return;
121
137
  if (event.event === "snapshot") {
122
- this.sequence = event.seq;
123
- this.buffered = [];
124
138
  this.snapshot = event;
139
+ this.reset(event.seq);
125
140
  for (const callback of this.snapshots) callback(event);
126
141
  return;
127
142
  }
128
- if (event.seq <= this.sequence) return;
129
- this.sequence = event.seq;
130
- if (!this.outputs.size) this.buffered.push(event);
131
- for (const callback of this.outputs) callback(event);
143
+ this.deliver(event);
132
144
  }
133
145
  onSnapshot(callback) {
134
146
  this.snapshots.add(callback);
@@ -137,16 +149,8 @@ var Pty = class {
137
149
  this.snapshots.delete(callback);
138
150
  };
139
151
  }
140
- onData(callback) {
141
- this.outputs.add(callback);
142
- for (const event of this.buffered) callback(event);
143
- this.buffered = [];
144
- return () => {
145
- this.outputs.delete(callback);
146
- };
147
- }
148
152
  async write(data) {
149
- if (this.error) throw this.error;
153
+ if (this.failure) throw this.failure;
150
154
  const parts = Array.from(data);
151
155
  const requests = [];
152
156
  for (let offset = 0; offset < parts.length; offset += 8e3) {
@@ -155,21 +159,19 @@ var Pty = class {
155
159
  await Promise.all(requests);
156
160
  }
157
161
  async resize(size) {
158
- if (this.error) throw this.error;
162
+ if (this.failure) throw this.failure;
159
163
  await this.request("resize", { session_id: this.id, ...size });
160
164
  }
161
165
  async detach() {
162
- if (this.error) return;
166
+ if (this.failure) return;
163
167
  this.remove();
164
168
  this.release();
165
169
  await this.request("detach", { session_id: this.id });
166
170
  }
167
171
  release(error = new Error("Terminal detached")) {
168
- this.error = error;
172
+ super.release(error);
169
173
  this.snapshots.clear();
170
- this.outputs.clear();
171
174
  this.snapshot = void 0;
172
- this.buffered = [];
173
175
  }
174
176
  };
175
177
 
@@ -224,6 +226,8 @@ var MachineConnection = class {
224
226
  epoch = 0;
225
227
  serial = 0;
226
228
  refs = 1;
229
+ // What a held stream sends its requests through.
230
+ call = (op, params) => this.request(op, params);
227
231
  onState(callback) {
228
232
  this.listeners.add(callback);
229
233
  callback({ ...this.state });
@@ -261,27 +265,8 @@ var MachineConnection = class {
261
265
  list: () => this.request("list"),
262
266
  cwd: (id) => this.request("cwd", { session_id: id }),
263
267
  create: (size = { rows: 24, cols: 80 }, options = {}) => this.request("create", { ...size, ...options }),
264
- attach: async (id) => {
265
- const existing = this.terminals.get(id);
266
- if (existing) return existing;
267
- const terminal = new Pty(id, (op, params) => this.request(op, params), () => {
268
- this.terminals.delete(id);
269
- });
270
- this.terminals.set(id, terminal);
271
- try {
272
- await this.request("attach", { session_id: id });
273
- } catch (error) {
274
- this.terminals.delete(id);
275
- terminal.release();
276
- throw error;
277
- }
278
- return terminal;
279
- },
280
- close: async (id) => {
281
- await this.request("close", { session_id: id });
282
- this.terminals.get(id)?.release();
283
- this.terminals.delete(id);
284
- }
268
+ attach: (id) => this.openStream(this.terminals, id, (drop) => new Pty(id, this.call, drop), "attach"),
269
+ close: (id) => this.closeStream(this.terminals, id, "close")
285
270
  };
286
271
  fs = {
287
272
  stat: (path) => this.request("stat", { path }),
@@ -313,26 +298,45 @@ var MachineConnection = class {
313
298
  return { stdout: Buffer.from(result.stdout, "base64"), stderr: Buffer.from(result.stderr, "base64"), code: result.code };
314
299
  },
315
300
  spawn: (program, args, options) => this.request("spawn", { program, args, ...options }),
316
- attach: async (id) => {
317
- const existing = this.processes.get(id);
318
- if (existing) return existing;
319
- const process2 = new Process(id, (op, params) => this.request(op, params));
320
- this.processes.set(id, process2);
301
+ attach: (id) => this.openStream(this.processes, id, () => new Process(id, this.call), "process_attach"),
302
+ close: (id) => this.closeStream(this.processes, id, "process_close")
303
+ };
304
+ /** The stream a map holds under `id`, or one made and attached now: a failed attach leaves neither. */
305
+ async openStream(held, id, make, op) {
306
+ const existing = held.get(id);
307
+ if (existing) return existing;
308
+ const entry = make(() => {
309
+ held.delete(id);
310
+ });
311
+ held.set(id, entry);
312
+ try {
313
+ await this.request(op, { session_id: id });
314
+ } catch (error) {
315
+ held.delete(id);
316
+ entry.release();
317
+ throw error;
318
+ }
319
+ return entry;
320
+ }
321
+ async closeStream(held, id, op) {
322
+ await this.request(op, { session_id: id });
323
+ held.get(id)?.release();
324
+ held.delete(id);
325
+ }
326
+ /** A stream lives in the connection that asked for it, so it is attached to the one that is up again; one the
327
+ * node no longer has is released. False once the connection has moved on. */
328
+ async reattachStreams(held, op, current) {
329
+ for (const [id, entry] of held) {
321
330
  try {
322
- await this.request("process_attach", { session_id: id });
331
+ await this.request(op, { session_id: id });
323
332
  } catch (error) {
324
- this.processes.delete(id);
325
- process2.release();
326
- throw error;
333
+ if (!current()) return false;
334
+ entry.release(error);
335
+ held.delete(id);
327
336
  }
328
- return process2;
329
- },
330
- close: async (id) => {
331
- await this.request("process_close", { session_id: id });
332
- this.processes.get(id)?.release();
333
- this.processes.delete(id);
334
337
  }
335
- };
338
+ return true;
339
+ }
336
340
  cleanup() {
337
341
  this.epoch++;
338
342
  clearTimeout(this.retry);
@@ -454,24 +458,8 @@ var MachineConnection = class {
454
458
  this.processes.clear();
455
459
  }
456
460
  this.instance = actual.instance_id;
457
- for (const [id, terminal] of this.terminals) {
458
- try {
459
- await this.request("attach", { session_id: id });
460
- } catch (error) {
461
- if (!current()) return;
462
- terminal.release(error);
463
- this.terminals.delete(id);
464
- }
465
- }
466
- for (const [id, process2] of this.processes) {
467
- try {
468
- await this.request("process_attach", { session_id: id });
469
- } catch (error) {
470
- if (!current()) return;
471
- process2.release(error);
472
- this.processes.delete(id);
473
- }
474
- }
461
+ if (!await this.reattachStreams(this.terminals, "attach", current)) return;
462
+ if (!await this.reattachStreams(this.processes, "process_attach", current)) return;
475
463
  for (const [watchId, watcher] of this.watchers) {
476
464
  try {
477
465
  await this.request("watch", { watch_id: watchId, path: watcher.path, recursive: watcher.recursive });
package/dist/process.d.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  import type { ProcessEvent, ProcessOutput } from './types';
2
- export declare class Process {
2
+ import { Stream } from './stream';
3
+ export declare class Process extends Stream<ProcessOutput> {
3
4
  readonly id: string;
4
5
  private request;
5
- private sequence;
6
- private error?;
7
6
  private ended;
8
7
  private code?;
9
- private buffered;
10
- private outputs;
11
8
  private exits;
12
9
  constructor(id: string, request: (op: string, params: Record<string, unknown>) => Promise<unknown>);
13
10
  receive(event: ProcessEvent): void;
14
- onData(callback: (event: ProcessOutput) => void): () => void;
15
11
  /** Its exit code, or nothing when the node lost the stream and the process may still be running. */
16
12
  onExit(callback: (code?: number) => void): () => void;
17
13
  write(data: Uint8Array): Promise<void>;
package/dist/pty.d.ts CHANGED
@@ -1,18 +1,14 @@
1
1
  import type { Output, Snapshot, TerminalEvent, Size } from './types';
2
- export declare class Pty {
2
+ import { Stream } from './stream';
3
+ export declare class Pty extends Stream<Output> {
3
4
  readonly id: string;
4
5
  private request;
5
6
  private remove;
6
- private sequence;
7
- private error?;
8
7
  private snapshot?;
9
- private buffered;
10
8
  private snapshots;
11
- private outputs;
12
9
  constructor(id: string, request: (op: string, params: Record<string, unknown>) => Promise<unknown>, remove: () => void);
13
10
  receive(event: TerminalEvent): void;
14
11
  onSnapshot(callback: (event: Snapshot) => void): () => void;
15
- onData(callback: (event: Output) => void): () => void;
16
12
  write(data: string): Promise<void>;
17
13
  resize(size: Size): Promise<void>;
18
14
  detach(): Promise<void>;
@@ -0,0 +1,17 @@
1
+ /** One node stream's events, in the order it sent them, kept for a reader that has not asked yet. */
2
+ export declare class Stream<T extends {
3
+ seq: number;
4
+ }> {
5
+ private sequence;
6
+ private missed;
7
+ private readers;
8
+ /** Why the stream was released; a released stream takes no more events. */
9
+ protected failure?: Error;
10
+ /** Takes one event in order, unless a release or a later sequence makes it stale. */
11
+ protected deliver(event: T): void;
12
+ /** Replaces the order from a snapshot: what came before it is no longer what the stream holds. */
13
+ protected reset(seq: number): void;
14
+ /** Everything from here on, and first what no reader has taken yet. */
15
+ onData(reader: (event: T) => void): () => void;
16
+ protected release(error: Error): void;
17
+ }
package/dist/types.d.ts CHANGED
@@ -1,26 +1,5 @@
1
1
  /** What puts the agent on an ssh machine, which the SDK calls before it connects to it. */
2
2
  export type InstallAgent = (host: string, signal?: AbortSignal) => Promise<void>;
3
- /**
4
- * The workspace protocol. A plugin that init.ts lists under `workspaces` is a workspace provider, and
5
- * the app drives it through these `ui` methods:
6
- *
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
9
- * workspace-menu() -> WorkspaceMenuItem[] what the menu offers to open; an entry that carries
10
- * an error stays listed with its reason, and a click retries it
11
- * workspace-create({ key }) -> { id: string } the id of the workspace it made
12
- * workspace-select({ id })
13
- * workspace-close({ id })
14
- * workspace-rename({ id, name }) an empty name gives the workspace back to the name it followed
15
- * workspace-move({ id, before? })
16
- *
17
- * Every view that follows the workspace in front shares these channel topics, whatever the provider
18
- * behind it is:
19
- *
20
- * 'workspaces' a provider published that its rows changed
21
- * 'workspace:active' the workspace in front, or null; published by its provider
22
- * 'workspace:query' a view asks for 'workspace:active' again
23
- */
24
3
  /** What the agent in a workspace is doing, which the app draws at the right end of its row. */
25
4
  export type WorkspaceStatus = 'idle' | 'working' | 'finished';
26
5
  export interface WorkspaceRow {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lengmoxxl/sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
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": {