@dsh-workbench/adapter-runtime 1.1.14-beta.2 → 1.1.15

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencli-plugin-workbench",
3
- "version": "1.1.14-beta.2",
3
+ "version": "1.1.15",
4
4
  "private": true,
5
5
  "type": "module",
6
6
  "description": "DSH 工作台的公共通道:带浏览器登录态的 Node 侧 fetch,只服务 cookie / cookieValue 两种凭据来源",
@@ -1,4 +1,4 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
1
+ import { type Seam } from '@dsh-workbench/shared/host';
2
2
  export interface BrowserFindEntry {
3
3
  readonly text: string;
4
4
  readonly tag: string;
@@ -16,7 +16,7 @@ export interface BrowserFindResult {
16
16
  readonly helpUrl?: string;
17
17
  }
18
18
  export interface BrowserFindOptions {
19
- readonly shell: DshShell;
19
+ readonly seam: Seam;
20
20
  readonly css: string;
21
21
  readonly url?: string;
22
22
  readonly command?: string;
@@ -1,7 +1,8 @@
1
- import { parseJsonLoose } from '@dsh-workbench/shared/host';
1
+ import { now, parseJsonLoose } from '@dsh-workbench/shared/host';
2
2
  import { BROWSER_READ_SESSION } from './browser-read.js';
3
3
  import { backgroundLine, OPENCLI_WINDOW_ENV } from '../cli/index.js';
4
- import { runShell } from '@dsh-workbench/shared/host';
4
+ import { noWrite } from '@dsh-workbench/shared/host';
5
+ import { asRecord, textOf } from './page-value.js';
5
6
  const DEFAULT_TIMEOUT_MS = 25000;
6
7
  const DEFAULT_SETTLE_MS = 2500;
7
8
  const URL_TIMEOUT_MS = 8000;
@@ -26,12 +27,6 @@ export function cellRows(text) {
26
27
  .map((one) => one.trim())
27
28
  .filter((one) => one !== '');
28
29
  }
29
- function asRecord(value) {
30
- return typeof value === 'object' && value !== null ? value : undefined;
31
- }
32
- function textOf(value) {
33
- return typeof value === 'string' ? value.trim() : '';
34
- }
35
30
  export function entriesOf(value) {
36
31
  const record = asRecord(value);
37
32
  const raw = record === undefined ? undefined : record['entries'];
@@ -89,10 +84,16 @@ export function urlOf(stdout) {
89
84
  }
90
85
  return bare;
91
86
  }
92
- async function currentUrl(shell, command, session) {
87
+ async function currentUrl(seam, command, session) {
93
88
  const line = backgroundLine(command, ['browser', session, 'get', 'url', '--window', 'background']);
94
89
  try {
95
- const result = await runShell(shell, line, { timeoutMs: URL_TIMEOUT_MS, env: OPENCLI_WINDOW_ENV });
90
+ const result = await seam.run({
91
+ command: line,
92
+ intent: noWrite(),
93
+ retry: 'safe',
94
+ timeoutMs: URL_TIMEOUT_MS,
95
+ env: OPENCLI_WINDOW_ENV,
96
+ });
96
97
  if (result.exitCode !== 0)
97
98
  return '';
98
99
  return urlOf(result.stdout);
@@ -104,7 +105,7 @@ async function currentUrl(shell, command, session) {
104
105
  function sleep(ms) {
105
106
  return new Promise((resolve) => setTimeout(resolve, ms));
106
107
  }
107
- async function findOnce(shell, command, session, css, limit, textMax, timeoutMs) {
108
+ async function findOnce(seam, command, session, css, limit, textMax, timeoutMs) {
108
109
  const line = backgroundLine(command, [
109
110
  'browser',
110
111
  session,
@@ -118,7 +119,13 @@ async function findOnce(shell, command, session, css, limit, textMax, timeoutMs)
118
119
  '--window',
119
120
  'background',
120
121
  ]);
121
- const result = await runShell(shell, line, { timeoutMs, env: OPENCLI_WINDOW_ENV });
122
+ const result = await seam.run({
123
+ command: line,
124
+ intent: noWrite(),
125
+ retry: 'safe',
126
+ timeoutMs,
127
+ env: OPENCLI_WINDOW_ENV,
128
+ });
122
129
  if (result.timedOut) {
123
130
  return { kind: 'timeout', detail: `没在 ${Math.round(timeoutMs / 1000)} 秒内返回` };
124
131
  }
@@ -132,35 +139,39 @@ async function findOnce(shell, command, session, css, limit, textMax, timeoutMs)
132
139
  const entries = entriesOf(parseJsonLoose(result.stdout));
133
140
  return { kind: 'ok', entries, stdout: result.stdout };
134
141
  }
135
- async function openOnce(shell, command, session, url, timeoutMs) {
142
+ async function openOnce(seam, command, session, url, timeoutMs) {
136
143
  const line = backgroundLine(command, ['browser', session, 'open', url, '--window', 'background']);
137
- let result;
138
144
  try {
139
- result = await runShell(shell, line, { timeoutMs, env: OPENCLI_WINDOW_ENV });
145
+ const result = await seam.run({
146
+ command: line,
147
+ intent: noWrite(),
148
+ timeoutMs,
149
+ env: OPENCLI_WINDOW_ENV,
150
+ });
151
+ if (result.timedOut)
152
+ return `没在 ${Math.round(timeoutMs / 1000)} 秒内打开`;
153
+ if (result.exitCode !== 0) {
154
+ const reason = reasonOf(result.stdout, result.stderr);
155
+ return reason === '' ? `opencli 打开失败(退出码 ${String(result.exitCode ?? '?')})` : reason;
156
+ }
157
+ return undefined;
140
158
  }
141
159
  catch (error) {
142
160
  return error instanceof Error ? error.message : String(error);
143
161
  }
144
- if (result.timedOut)
145
- return `没在 ${Math.round(timeoutMs / 1000)} 秒内打开`;
146
- if (result.exitCode !== 0) {
147
- const reason = reasonOf(result.stdout, result.stderr);
148
- return reason === '' ? `opencli 打开失败(退出码 ${String(result.exitCode ?? '?')})` : reason;
149
- }
150
- return undefined;
151
162
  }
152
163
  export async function browserFind(options) {
153
- const { shell, css, url, open = true } = options;
164
+ const { seam, css, url, open = true } = options;
154
165
  const command = options.command ?? 'opencli';
155
166
  const session = options.session ?? BROWSER_READ_SESSION;
156
167
  const limit = options.limit ?? DEFAULT_LIMIT;
157
168
  const textMax = options.textMax ?? DEFAULT_TEXT_MAX;
158
169
  const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
159
170
  const settleMs = options.settleMs ?? DEFAULT_SETTLE_MS;
160
- const at = Date.now();
171
+ const at = now();
161
172
  const find = async () => {
162
173
  try {
163
- return await findOnce(shell, command, session, css, limit, textMax, timeoutMs);
174
+ return await findOnce(seam, command, session, css, limit, textMax, timeoutMs);
164
175
  }
165
176
  catch (error) {
166
177
  return {
@@ -172,9 +183,9 @@ export async function browserFind(options) {
172
183
  };
173
184
  const canMove = open && url !== undefined;
174
185
  if (canMove) {
175
- const here = await currentUrl(shell, command, session);
186
+ const here = await currentUrl(seam, command, session);
176
187
  if (here !== '' && !sameOrigin(here, url)) {
177
- const opened = await openOnce(shell, command, session, url, timeoutMs);
188
+ const opened = await openOnce(seam, command, session, url, timeoutMs);
178
189
  if (opened !== undefined) {
179
190
  return {
180
191
  ok: false,
@@ -196,7 +207,7 @@ export async function browserFind(options) {
196
207
  return { ok: false, code: 'cli-exec-failed', at, entries: [], count: 0, message: '没找到元素' };
197
208
  }
198
209
  if (outcome.kind === 'failed' && canMove) {
199
- const opened = await openOnce(shell, command, session, url, timeoutMs);
210
+ const opened = await openOnce(seam, command, session, url, timeoutMs);
200
211
  if (opened === undefined) {
201
212
  await sleep(settleMs);
202
213
  const second = await find();
@@ -1,4 +1,4 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
1
+ import { type Seam } from '@dsh-workbench/shared/host';
2
2
  import { BROWSER_READ_SESSION, BROWSER_RECORD_SESSION, BROWSER_SCREEN_SESSION } from '@dsh-workbench/shared/host';
3
3
  export { BROWSER_READ_SESSION, BROWSER_RECORD_SESSION, BROWSER_SCREEN_SESSION };
4
4
  export interface BrowserReadResult {
@@ -13,7 +13,7 @@ export interface BrowserReadResult {
13
13
  readonly helpUrl?: string;
14
14
  }
15
15
  export interface BrowserReadOptions {
16
- readonly shell: DshShell;
16
+ readonly seam: Seam;
17
17
  readonly script: string;
18
18
  readonly url?: string;
19
19
  readonly loginUrl?: string;
@@ -23,5 +23,7 @@ export interface BrowserReadOptions {
23
23
  readonly timeoutMs?: number;
24
24
  readonly retryMs?: number;
25
25
  readonly settleMs?: number;
26
+ readonly platform?: NodeJS.Platform;
26
27
  }
28
+ export declare function evalScriptArgOf(script: string, platform?: NodeJS.Platform): string;
27
29
  export declare function browserRead(options: BrowserReadOptions): Promise<BrowserReadResult>;
@@ -1,7 +1,8 @@
1
- import { parseJsonLoose } from '@dsh-workbench/shared/host';
1
+ import { now, parseJsonLoose } from '@dsh-workbench/shared/host';
2
2
  import { tailLines } from '@dsh-workbench/shared/host';
3
3
  import { backgroundLine, OPENCLI_WINDOW_ENV } from '../cli/index.js';
4
- import { runShell } from '@dsh-workbench/shared/host';
4
+ import { noWrite } from '@dsh-workbench/shared/host';
5
+ import { asRecord, textOf } from './page-value.js';
5
6
  import { BROWSER_READ_SESSION, BROWSER_RECORD_SESSION, BROWSER_SCREEN_SESSION } from '@dsh-workbench/shared/host';
6
7
  export { BROWSER_READ_SESSION, BROWSER_RECORD_SESSION, BROWSER_SCREEN_SESSION };
7
8
  const DEFAULT_TIMEOUT_MS = 25000;
@@ -17,12 +18,6 @@ const AUTH_CODES = new Set([
17
18
  'TOKEN_EXPIRED',
18
19
  ]);
19
20
  const PERMISSION_CODES = new Set(['FORBIDDEN', 'PERMISSION_DENIED', 'NO_PERMISSION', 'EACCES']);
20
- function asRecord(value) {
21
- return typeof value === 'object' && value !== null ? value : undefined;
22
- }
23
- function textOf(value) {
24
- return typeof value === 'string' ? value.trim() : '';
25
- }
26
21
  function codeOf(page) {
27
22
  const record = asRecord(page);
28
23
  return record === undefined ? '' : textOf(record['code']);
@@ -54,8 +49,20 @@ function blockerFor(code) {
54
49
  function sleep(ms) {
55
50
  return new Promise((resolve) => setTimeout(resolve, ms));
56
51
  }
57
- async function shellRun(shell, command, argv, timeoutMs) {
58
- const result = await runShell(shell, backgroundLine(command, argv), { timeoutMs, env: OPENCLI_WINDOW_ENV });
52
+ export function evalScriptArgOf(script, platform = process.platform) {
53
+ if (platform !== 'win32')
54
+ return script;
55
+ const bytes = Buffer.from(script, 'utf8').toString('base64');
56
+ return `eval(new TextDecoder().decode(Uint8Array.from(atob('${bytes}'), (one) => one.charCodeAt(0))))`;
57
+ }
58
+ async function shellRun(seam, command, argv, timeoutMs, platform) {
59
+ const result = await seam.run({
60
+ command: backgroundLine(command, argv, platform),
61
+ intent: noWrite(),
62
+ retry: 'safe',
63
+ timeoutMs,
64
+ env: OPENCLI_WINDOW_ENV,
65
+ });
59
66
  if (result.timedOut) {
60
67
  return { kind: 'timeout', detail: `没在 ${Math.round(timeoutMs / 1000)} 秒内返回`, stderr: result.stderr };
61
68
  }
@@ -64,11 +71,11 @@ async function shellRun(shell, command, argv, timeoutMs) {
64
71
  }
65
72
  return { kind: 'ok', stdout: result.stdout };
66
73
  }
67
- async function evalOnce(shell, command, session, script, timeoutMs, retryMs) {
74
+ async function evalOnce(seam, command, session, script, timeoutMs, retryMs, platform) {
68
75
  const run = async () => {
69
76
  let attempt;
70
77
  try {
71
- attempt = await shellRun(shell, command, ['browser', session, 'eval', script, '--window', 'background'], timeoutMs);
78
+ attempt = await shellRun(seam, command, ['browser', session, 'eval', evalScriptArgOf(script, platform), '--window', 'background'], timeoutMs, platform);
72
79
  }
73
80
  catch (error) {
74
81
  return {
@@ -131,10 +138,10 @@ async function evalOnce(shell, command, session, script, timeoutMs, retryMs) {
131
138
  await sleep(retryMs);
132
139
  return run();
133
140
  }
134
- async function openOnce(shell, command, session, url, timeoutMs) {
141
+ async function openOnce(seam, command, session, url, timeoutMs, platform) {
135
142
  let attempt;
136
143
  try {
137
- attempt = await shellRun(shell, command, ['browser', session, 'open', url, '--window', 'background'], timeoutMs);
144
+ attempt = await shellRun(seam, command, ['browser', session, 'open', url, '--window', 'background'], timeoutMs, platform);
138
145
  }
139
146
  catch (error) {
140
147
  return {
@@ -158,14 +165,15 @@ async function openOnce(shell, command, session, url, timeoutMs) {
158
165
  };
159
166
  }
160
167
  export async function browserRead(options) {
161
- const { shell, script, url, loginUrl, open = true } = options;
168
+ const { seam, script, url, loginUrl, open = true } = options;
162
169
  const command = options.command ?? 'opencli';
163
170
  const session = options.session ?? BROWSER_READ_SESSION;
164
171
  const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
165
172
  const retryMs = options.retryMs ?? DEFAULT_RETRY_MS;
166
173
  const settleMs = options.settleMs ?? DEFAULT_SETTLE_MS;
167
- const at = Date.now();
168
- const first = await evalOnce(shell, command, session, script, timeoutMs, retryMs);
174
+ const platform = options.platform ?? process.platform;
175
+ const at = now();
176
+ const first = await evalOnce(seam, command, session, script, timeoutMs, retryMs, platform);
169
177
  if (first.kind === 'failure')
170
178
  return { ...first.result, at };
171
179
  let page = first.value;
@@ -181,11 +189,11 @@ export async function browserRead(options) {
181
189
  ...(url === undefined ? {} : { helpUrl: url }),
182
190
  };
183
191
  }
184
- const opened = await openOnce(shell, command, session, url, timeoutMs);
192
+ const opened = await openOnce(seam, command, session, url, timeoutMs, platform);
185
193
  if (opened !== undefined)
186
194
  return { ...opened, at };
187
195
  await sleep(settleMs);
188
- const second = await evalOnce(shell, command, session, script, timeoutMs, retryMs);
196
+ const second = await evalOnce(seam, command, session, script, timeoutMs, retryMs, platform);
189
197
  if (second.kind === 'failure')
190
198
  return { ...second.result, at };
191
199
  page = second.value;
@@ -0,0 +1,2 @@
1
+ export declare function asRecord(value: unknown): Record<string, unknown> | undefined;
2
+ export declare function textOf(value: unknown): string;
@@ -0,0 +1,6 @@
1
+ export function asRecord(value) {
2
+ return typeof value === 'object' && value !== null ? value : undefined;
3
+ }
4
+ export function textOf(value) {
5
+ return typeof value === 'string' ? value.trim() : '';
6
+ }
@@ -8,7 +8,7 @@ export interface OpencliResolution {
8
8
  readonly isolated: string | null;
9
9
  }
10
10
  export declare const OPENCLI_PATH_FALLBACK = "opencli";
11
- export declare function isolatedOpencliPathOf(ctx: HostContextLike): string | null;
11
+ export declare function isolatedOpencliPathOf(_ctx: HostContextLike): string | null;
12
12
  export declare function opencliCommandOf(ctx: HostContextLike): OpencliResolution;
13
13
  export * from './opencli.js';
14
14
  export * from './opencli-runtime.js';
@@ -1,19 +1,13 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
+ import { runtimeLayoutOf } from '@dsh-workbench/shared/host';
4
+ import { OPENCLI_RUNTIME_ID } from '../foundation/paths.js';
3
5
  export const OPENCLI_PATH_FALLBACK = 'opencli';
4
6
  function binName(platform = process.platform) {
5
7
  return platform === 'win32' ? 'opencli.cmd' : 'opencli';
6
8
  }
7
- export function isolatedOpencliPathOf(ctx) {
8
- const homePath = ctx.get('dshHomePath');
9
- if (typeof homePath !== 'function')
10
- return null;
11
- try {
12
- return join(homePath('workbench/opencli/bin'), binName());
13
- }
14
- catch {
15
- return null;
16
- }
9
+ export function isolatedOpencliPathOf(_ctx) {
10
+ return join(runtimeLayoutOf(OPENCLI_RUNTIME_ID).root, 'bin', binName());
17
11
  }
18
12
  export function opencliCommandOf(ctx) {
19
13
  const isolated = isolatedOpencliPathOf(ctx);
@@ -1,8 +1,8 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
1
+ import { type Seam } from '@dsh-workbench/shared/host';
2
2
  import type { Opencli, OpencliSession } from './opencli-types.js';
3
3
  export declare function opencliCommandLine(command: string, argv: readonly string[], session?: OpencliSession, platform?: NodeJS.Platform): string;
4
4
  export declare function createOpencli(options: {
5
- shell: DshShell;
5
+ seam: Seam;
6
6
  plugin: string;
7
7
  logger?: {
8
8
  warn?(message: string): void;
@@ -1,9 +1,9 @@
1
- import { OPENCLI_MAX_CONCURRENT, OPENCLI_TIMEOUT_MS } from '@dsh-workbench/shared/host';
1
+ import { OPENCLI_MAX_CONCURRENT, OPENCLI_TIMEOUT_MS, now } from '@dsh-workbench/shared/host';
2
2
  import { parseJsonLoose } from '@dsh-workbench/shared/host';
3
- import { quoteShellArgFor, tailLines } from '@dsh-workbench/shared/host';
3
+ import { productWrite, quoteShellArgFor, tailLines } from '@dsh-workbench/shared/host';
4
4
  import { helpUrlOf, parseCliErrorEnvelope } from '@dsh-workbench/shared/host';
5
5
  import { OPENCLI_WINDOW_ENV } from './opencli-window.js';
6
- import { runShell } from '@dsh-workbench/shared/host';
6
+ import { SeamError } from '@dsh-workbench/shared/host';
7
7
  import { WorkbenchDataError } from '@dsh-workbench/shared/host';
8
8
  const FORBIDDEN_CODES = new Set(['FORBIDDEN', 'PERMISSION_DENIED']);
9
9
  const SITE_SESSION_ARGS = ['--site-session', 'persistent'];
@@ -17,7 +17,7 @@ export function opencliCommandLine(command, argv, session = 'persistent', platfo
17
17
  ].join(' ');
18
18
  }
19
19
  export function createOpencli(options) {
20
- const { shell, plugin, logger, command = 'opencli', timeoutMs = OPENCLI_TIMEOUT_MS, maxConcurrent = OPENCLI_MAX_CONCURRENT, } = options;
20
+ const { seam, plugin, logger, command = 'opencli', timeoutMs = OPENCLI_TIMEOUT_MS, maxConcurrent = OPENCLI_MAX_CONCURRENT, } = options;
21
21
  const inflight = new Map();
22
22
  const MAX_CONCURRENT = maxConcurrent;
23
23
  let running = 0;
@@ -48,13 +48,20 @@ export function createOpencli(options) {
48
48
  async function call(commandLine, workdir) {
49
49
  let result;
50
50
  try {
51
- result = await runShell(shell, commandLine, {
51
+ result = await seam.run({
52
+ command: commandLine,
53
+ intent: productWrite(),
52
54
  ...(workdir === undefined ? {} : { workdir }),
53
55
  timeoutMs,
54
56
  env: OPENCLI_WINDOW_ENV,
55
57
  });
56
58
  }
57
59
  catch (error) {
60
+ if (error instanceof SeamError) {
61
+ throw new WorkbenchDataError('cli-exec-failed', error.message, {
62
+ hint: '这条命令被沙箱挡住了:把命令与写入域记下来交给维护者。',
63
+ });
64
+ }
58
65
  throw new WorkbenchDataError('cli-exec-failed', `执行 opencli 失败:${String(error)}`, {
59
66
  hint: '确认 opencli 已安装,并且在 PATH 里。',
60
67
  });
@@ -107,7 +114,7 @@ export function createOpencli(options) {
107
114
  const commandLine = opencliCommandLine(command, [plugin, ...args], session);
108
115
  const once = async () => {
109
116
  const value = await call(commandLine, workdir);
110
- return { value, at: Date.now() };
117
+ return { value, at: now() };
111
118
  };
112
119
  const task = runLimited(async () => {
113
120
  try {
@@ -1,7 +1,7 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
2
1
  import type { CommandReader } from '@dsh-workbench/shared';
2
+ import { type Seam } from '@dsh-workbench/shared/host';
3
3
  export interface CommandReaderOptions {
4
- readonly shell: DshShell;
4
+ readonly seam: Seam;
5
5
  readonly tmpRoot?: string | undefined;
6
6
  }
7
7
  export declare function createCommandReader(options: CommandReaderOptions): CommandReader;
@@ -1,7 +1,7 @@
1
- import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
1
+ import { mkdtempSync, rmSync } from 'node:fs';
2
2
  import { tmpdir } from 'node:os';
3
3
  import { join } from 'node:path';
4
- import { quoteShellArg, runShell } from '@dsh-workbench/shared/host';
4
+ import { noWrite, quoteShellArg, writeTextFile } from '@dsh-workbench/shared/host';
5
5
  function quoteAll(argv) {
6
6
  return argv.map((arg) => quoteShellArg(arg)).join(' ');
7
7
  }
@@ -12,13 +12,13 @@ function writeFiles(request, tmpRoot) {
12
12
  const paths = request.files.map((file, index) => {
13
13
  const suffix = file.suffix === undefined || file.suffix === '' ? '' : `.${file.suffix}`;
14
14
  const path = join(dir, `file-${String(index)}${suffix}`);
15
- writeFileSync(path, file.content, 'utf8');
15
+ writeTextFile(path, file.content);
16
16
  return path;
17
17
  });
18
18
  return { dir, paths };
19
19
  }
20
20
  export function createCommandReader(options) {
21
- const { shell } = options;
21
+ const { seam } = options;
22
22
  const tmpRoot = options.tmpRoot ?? tmpdir();
23
23
  return {
24
24
  async run(request) {
@@ -30,7 +30,9 @@ export function createCommandReader(options) {
30
30
  for (const path of temp.paths)
31
31
  argv.push(request.appendFileArgs, path);
32
32
  }
33
- const result = await runShell(shell, quoteAll(argv), {
33
+ const result = await seam.run({
34
+ command: quoteAll(argv),
35
+ intent: noWrite(),
34
36
  ...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
35
37
  ...(request.cwd === undefined ? {} : { workdir: request.cwd }),
36
38
  ...(request.env === undefined ? {} : { env: request.env }),
@@ -1,4 +1,4 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
1
+ import { type Seam } from '@dsh-workbench/shared/host';
2
2
  import type { CallOverrides, RequestRecipe } from '@dsh-workbench/shared/host';
3
3
  export declare const CHANNEL_DIR: string;
4
4
  export type ChannelState = 'unknown' | 'ready' | 'unavailable';
@@ -13,7 +13,7 @@ export declare function wireBodyOf(recipe: RequestRecipe, overrides: CallOverrid
13
13
  };
14
14
  export declare function channelCallOf(recipe: RequestRecipe, overrides: CallOverrides, command: string): ChannelCall | undefined;
15
15
  export declare function channelAlreadyInstalled(): boolean;
16
- export declare function ensureChannel(shell: DshShell, command: string, state: {
16
+ export declare function ensureChannel(seam: Seam, command: string, state: {
17
17
  value: ChannelState;
18
18
  }, ready?: () => boolean): Promise<boolean>;
19
19
  export declare function channelCommandLine(call: ChannelCall): string;
@@ -1,5 +1,5 @@
1
1
  import { fileURLToPath } from 'node:url';
2
- import { DEFAULT_RECIPE_MAX_BYTES, quoteShellArg, runShell, } from '@dsh-workbench/shared/host';
2
+ import { DEFAULT_RECIPE_MAX_BYTES, productWrite, quoteShellArg, } from '@dsh-workbench/shared/host';
3
3
  import { channelInstalledVersion, channelPluginPlan, channelSiteInstalled, channelVersionOf, } from './channel-plugin.js';
4
4
  export const CHANNEL_DIR = fileURLToPath(new URL('../../channel/', import.meta.url));
5
5
  function authOf(recipe) {
@@ -99,7 +99,7 @@ export function channelAlreadyInstalled() {
99
99
  const shipped = channelVersionOf(CHANNEL_DIR);
100
100
  return shipped !== undefined && channelInstalledVersion() === shipped && channelSiteInstalled();
101
101
  }
102
- export async function ensureChannel(shell, command, state, ready = channelAlreadyInstalled) {
102
+ export async function ensureChannel(seam, command, state, ready = channelAlreadyInstalled) {
103
103
  if (state.value === 'ready')
104
104
  return true;
105
105
  if (state.value === 'unavailable')
@@ -113,7 +113,7 @@ export async function ensureChannel(shell, command, state, ready = channelAlread
113
113
  state.value = 'unavailable';
114
114
  return false;
115
115
  }
116
- const result = await runShell(shell, plan.line, { timeoutMs: 120_000 });
116
+ const result = await seam.run({ command: plan.line, intent: productWrite(), timeoutMs: 120_000 });
117
117
  state.value = result.exitCode === 0 && !result.timedOut ? 'ready' : 'unavailable';
118
118
  return state.value === 'ready';
119
119
  }
@@ -1,8 +1,8 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
1
+ import { type Seam } from '@dsh-workbench/shared/host';
2
2
  import type { DataReader } from '@dsh-workbench/shared/host';
3
3
  export * from './channel-plugin.js';
4
4
  export interface DataReaderOptions {
5
- readonly shell?: DshShell | undefined;
5
+ readonly seam?: Seam | undefined;
6
6
  readonly command?: string | undefined;
7
7
  readonly fetch?: typeof fetch | undefined;
8
8
  readonly channel?: boolean | undefined;
@@ -1,4 +1,4 @@
1
- import { callsProblemOf, recipeProblemOf, runShell, scriptOf, scriptOfMany } from '@dsh-workbench/shared/host';
1
+ import { callsProblemOf, noWrite, recipeProblemOf, scriptOf, scriptOfMany } from '@dsh-workbench/shared/host';
2
2
  import { browserRead } from '../browser/index.js';
3
3
  import { createOpencli } from '../cli/index.js';
4
4
  import { WorkbenchDataError } from '@dsh-workbench/shared/host';
@@ -127,10 +127,10 @@ export function createDataReader(options = {}) {
127
127
  }
128
128
  }
129
129
  async function readBrowser(request) {
130
- if (options.shell === undefined)
131
- return failure('invalid-request', '没有 shell,读不了浏览器');
130
+ if (options.seam === undefined)
131
+ return failure('invalid-request', '没有执行缝,读不了浏览器');
132
132
  const result = await browserRead({
133
- shell: options.shell,
133
+ seam: options.seam,
134
134
  script: request.script,
135
135
  command,
136
136
  ...(request.url === undefined ? {} : { url: request.url }),
@@ -144,8 +144,18 @@ export function createDataReader(options = {}) {
144
144
  return { ok: true, data: result.value };
145
145
  }
146
146
  const channelState = { value: 'unknown' };
147
- async function readChannel(shell, call, recipe) {
148
- const result = await runShell(shell, channelCommandLine(call), { timeoutMs: call.timeoutMs + 5000 });
147
+ async function readChannel(seam, call, recipe) {
148
+ const result = await seam
149
+ .run({
150
+ command: channelCommandLine(call),
151
+ intent: noWrite(),
152
+ retry: recipe.request.method === 'GET' ? 'safe' : 'never',
153
+ timeoutMs: call.timeoutMs + 5000,
154
+ bestEffort: true,
155
+ })
156
+ .catch(() => undefined);
157
+ if (result === undefined)
158
+ return undefined;
149
159
  if (result.timedOut)
150
160
  return failure('timeout', `通道读超时(${call.timeoutMs}ms)`);
151
161
  if (result.exitCode !== 0)
@@ -187,12 +197,12 @@ export function createDataReader(options = {}) {
187
197
  if (mode === 'request' && recipe.request.method !== 'GET') {
188
198
  return failure('invalid-request', 'request 只读 GET;写走 submit');
189
199
  }
190
- if (options.shell === undefined)
191
- return failure('invalid-request', '没有 shell,读不了浏览器');
200
+ if (options.seam === undefined)
201
+ return failure('invalid-request', '没有执行缝,读不了浏览器');
202
+ const seam = options.seam;
192
203
  const call = options.channel === false ? undefined : channelCallOf(recipe, overrides ?? {}, command);
193
- if (call !== undefined &&
194
- (await ensureChannel(options.shell, command, channelState, options.channelInstalled))) {
195
- const viaChannel = await readChannel(options.shell, call, recipe);
204
+ if (call !== undefined && (await ensureChannel(seam, command, channelState, options.channelInstalled))) {
205
+ const viaChannel = await readChannel(seam, call, recipe);
196
206
  if (viaChannel !== undefined)
197
207
  return viaChannel;
198
208
  channelState.value = 'unavailable';
@@ -205,7 +215,7 @@ export function createDataReader(options = {}) {
205
215
  return failure('invalid-request', error instanceof Error ? error.message : String(error));
206
216
  }
207
217
  const result = await browserRead({
208
- shell: options.shell,
218
+ seam,
209
219
  script,
210
220
  command,
211
221
  url: recipe.origin,
@@ -234,8 +244,9 @@ export function createDataReader(options = {}) {
234
244
  if (recipe.request.method !== 'GET') {
235
245
  return allFailure(names, 'invalid-request', 'requestMany 只读 GET;写走 submit');
236
246
  }
237
- if (options.shell === undefined)
238
- return allFailure(names, 'invalid-request', '没有 shell,读不了浏览器');
247
+ if (options.seam === undefined)
248
+ return allFailure(names, 'invalid-request', '没有执行缝,读不了浏览器');
249
+ const seam = options.seam;
239
250
  let script;
240
251
  try {
241
252
  script = scriptOfMany(recipe, calls);
@@ -244,7 +255,7 @@ export function createDataReader(options = {}) {
244
255
  return allFailure(names, 'invalid-request', error instanceof Error ? error.message : String(error));
245
256
  }
246
257
  const result = await browserRead({
247
- shell: options.shell,
258
+ seam,
248
259
  script,
249
260
  command,
250
261
  url: recipe.origin,
@@ -297,7 +308,7 @@ export function createDataReader(options = {}) {
297
308
  if (cached !== undefined)
298
309
  return cached;
299
310
  const created = createOpencli({
300
- shell: options.shell,
311
+ seam: options.seam,
301
312
  plugin,
302
313
  command,
303
314
  timeoutMs,
@@ -306,8 +317,8 @@ export function createDataReader(options = {}) {
306
317
  return created;
307
318
  }
308
319
  async function readCommand(request) {
309
- if (options.shell === undefined)
310
- return failure('invalid-request', '没有 shell,跑不了命令');
320
+ if (options.seam === undefined)
321
+ return failure('invalid-request', '没有执行缝,跑不了命令');
311
322
  if (request.adapter !== 'opencli') {
312
323
  return failure('invalid-request', `不认识的命令适配器:${request.adapter}`);
313
324
  }
@@ -0,0 +1,10 @@
1
+ import type { DshShell } from '@dsh-workbench/shared';
2
+ import type { ExtensionReader } from '@dsh-workbench/shared/host';
3
+ export interface ExtensionReaderOptions {
4
+ readonly shell?: DshShell | undefined;
5
+ readonly command?: string | undefined;
6
+ readonly logger?: {
7
+ warn?(message: string): void;
8
+ } | undefined;
9
+ }
10
+ export declare function createExtensionReader(options: ExtensionReaderOptions): ExtensionReader;
@@ -0,0 +1,36 @@
1
+ import { WorkbenchDataError } from '@dsh-workbench/shared/host';
2
+ import { createOpencli } from '../cli/index.js';
3
+ export function createExtensionReader(options) {
4
+ const readers = new Map();
5
+ function readerOf(need) {
6
+ if (need.runtime !== 'opencli') {
7
+ throw new WorkbenchDataError('unsupported-runtime', `没有能跑 ${need.runtime} 扩展的适配器:这个模块要的是 ${need.name}`, { hint: '换一个带这种运行时的适配器集合,或把这条需要改成 opencli 扩展。' });
8
+ }
9
+ if (options.shell === undefined) {
10
+ throw new WorkbenchDataError('cli-exec-failed', `读不了:这台机器上没有 shell 服务(${need.name})`, {
11
+ hint: '工作台的 shell 服务没起来,重启一下工作台再试',
12
+ });
13
+ }
14
+ const cached = readers.get(need.name);
15
+ if (cached !== undefined)
16
+ return cached;
17
+ const created = createOpencli({
18
+ shell: options.shell,
19
+ plugin: need.name,
20
+ ...(options.command === undefined ? {} : { command: options.command }),
21
+ ...(options.logger === undefined ? {} : { logger: options.logger }),
22
+ });
23
+ readers.set(need.name, created);
24
+ return created;
25
+ }
26
+ return {
27
+ async json(need, args, callOptions = {}) {
28
+ const result = await readerOf(need).json(args, {
29
+ ...(callOptions.key === undefined ? {} : { key: callOptions.key }),
30
+ ...(callOptions.workdir === undefined ? {} : { workdir: callOptions.workdir }),
31
+ ...(callOptions.session === undefined ? {} : { session: callOptions.session }),
32
+ });
33
+ return result;
34
+ },
35
+ };
36
+ }
@@ -1,7 +1,7 @@
1
- import type { DshShell } from '@dsh-workbench/shared';
2
1
  import type { GitReader } from '@dsh-workbench/shared';
2
+ import { type Seam } from '@dsh-workbench/shared/host';
3
3
  export interface GitReaderOptions {
4
- readonly shell: DshShell;
4
+ readonly seam: Seam;
5
5
  readonly logger?: {
6
6
  warn?(message: string): void;
7
7
  } | undefined;
package/dist/git/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { quoteShellArg, runShell, tailLines } from '@dsh-workbench/shared/host';
1
+ import { noWrite, quoteShellArg, tailLines } from '@dsh-workbench/shared/host';
2
2
  function keyOf(path) {
3
3
  return String(path ?? '').trim();
4
4
  }
5
5
  export function createGitReader(options) {
6
- const { shell, logger } = options;
6
+ const { seam, logger } = options;
7
7
  return {
8
8
  async origin(path) {
9
9
  const key = keyOf(path);
@@ -11,7 +11,11 @@ export function createGitReader(options) {
11
11
  return { kind: 'unknown', reason: 'no-path' };
12
12
  let result;
13
13
  try {
14
- result = await runShell(shell, `git -C ${quoteShellArg(key)} remote get-url origin`);
14
+ result = await seam.run({
15
+ command: `git -C ${quoteShellArg(key)} remote get-url origin`,
16
+ intent: noWrite(),
17
+ retry: 'safe',
18
+ });
15
19
  }
16
20
  catch (error) {
17
21
  logger?.warn?.(`[dsh-workbench] git remote 执行失败:${String(error)}`);
@@ -27,7 +31,11 @@ export function createGitReader(options) {
27
31
  return { kind: 'unknown', reason: 'no-path' };
28
32
  let result;
29
33
  try {
30
- result = await runShell(shell, `git -C ${quoteShellArg(key)} rev-parse --abbrev-ref HEAD`);
34
+ result = await seam.run({
35
+ command: `git -C ${quoteShellArg(key)} rev-parse --abbrev-ref HEAD`,
36
+ intent: noWrite(),
37
+ retry: 'safe',
38
+ });
31
39
  }
32
40
  catch (error) {
33
41
  logger?.warn?.(`[dsh-workbench] git rev-parse --abbrev-ref 执行失败:${String(error)}`);
@@ -46,7 +54,11 @@ export function createGitReader(options) {
46
54
  return { kind: 'unknown', reason: 'no-path' };
47
55
  let result;
48
56
  try {
49
- result = await runShell(shell, `git -C ${quoteShellArg(key)} rev-parse HEAD`);
57
+ result = await seam.run({
58
+ command: `git -C ${quoteShellArg(key)} rev-parse HEAD`,
59
+ intent: noWrite(),
60
+ retry: 'safe',
61
+ });
50
62
  }
51
63
  catch (error) {
52
64
  logger?.warn?.(`[dsh-workbench] git rev-parse 执行失败:${String(error)}`);
@@ -0,0 +1,23 @@
1
+ import type { BinNeed, LibNeed, SupplySource } from '@dsh-workbench/shared';
2
+ import type { HostOs } from '@dsh-workbench/shared';
3
+ export type NpmSource = Extract<SupplySource, {
4
+ kind: 'npm';
5
+ }>;
6
+ export declare function libInstallCommand(options: {
7
+ readonly need: LibNeed;
8
+ readonly source: NpmSource;
9
+ readonly version?: string | undefined;
10
+ readonly env?: NodeJS.ProcessEnv | undefined;
11
+ }): string;
12
+ /**
13
+ * 这个 bin 在这台机器上怎么装:作者写了 `install` 且平台对得上才给出那条命令。
14
+ * `command` 原样照抄;`npm` 装进隔离运行时的共用依赖前缀(那份包里的 bin 会自动上 PATH)。
15
+ * @param need 一条 bin 需要。
16
+ * @param os 宿主平台(由平台适配器给出)。
17
+ * @param env 取 registry 环境变量的地方。
18
+ * @returns 一条能直接粘进终端的命令;没声明或平台不对就是 undefined。
19
+ */
20
+ export declare function libUninstallCommand(options: {
21
+ readonly need: LibNeed;
22
+ }): string;
23
+ export declare function binInstallCommand(need: BinNeed, os: HostOs, env?: NodeJS.ProcessEnv): string | undefined;
@@ -0,0 +1,45 @@
1
+ import { npmRegistryOf } from '@dsh-workbench/shared/host';
2
+ import { quoteShellArg } from '@dsh-workbench/shared/host';
3
+ import { libsPrefix, npmCacheDir } from './paths.js';
4
+ export function libInstallCommand(options) {
5
+ const { need, source, version } = options;
6
+ const registry = npmRegistryOf(source, options.env ?? process.env);
7
+ const spec = version ?? need.version ?? 'latest';
8
+ const registryFlag = registry.ok ? ` --registry ${quoteShellArg(registry.value)}` : '';
9
+ return `npm i --prefix ${quoteShellArg(libsPrefix())} ${quoteShellArg(`${need.name}@${spec}`)}${registryFlag} --cache ${quoteShellArg(npmCacheDir())}`;
10
+ }
11
+ /**
12
+ * 这个 bin 在这台机器上怎么装:作者写了 `install` 且平台对得上才给出那条命令。
13
+ * `command` 原样照抄;`npm` 装进隔离运行时的共用依赖前缀(那份包里的 bin 会自动上 PATH)。
14
+ * @param need 一条 bin 需要。
15
+ * @param os 宿主平台(由平台适配器给出)。
16
+ * @param env 取 registry 环境变量的地方。
17
+ * @returns 一条能直接粘进终端的命令;没声明或平台不对就是 undefined。
18
+ */
19
+ export function libUninstallCommand(options) {
20
+ return `npm uninstall --prefix ${quoteShellArg(libsPrefix())} ${quoteShellArg(options.need.name)}`;
21
+ }
22
+ export function binInstallCommand(need, os, env = process.env) {
23
+ const install = need.install;
24
+ if (install === undefined)
25
+ return undefined;
26
+ if (install.platforms !== undefined && !install.platforms.includes(os))
27
+ return undefined;
28
+ if (install.kind === 'command') {
29
+ const line = install.line.trim();
30
+ return line === '' ? undefined : line;
31
+ }
32
+ const packageName = install.package.trim();
33
+ if (packageName === '')
34
+ return undefined;
35
+ return libInstallCommand({
36
+ need: { kind: 'lib', name: packageName, purpose: need.purpose },
37
+ source: {
38
+ kind: 'npm',
39
+ package: packageName,
40
+ ...(install.registryEnv === undefined ? {} : { registryEnv: install.registryEnv }),
41
+ ...(install.registryDefault === undefined ? {} : { registryDefault: install.registryDefault }),
42
+ },
43
+ env,
44
+ });
45
+ }
@@ -1,17 +1,14 @@
1
1
  export * from './links.js';
2
2
  export * from './runner.js';
3
3
  import { existsSync } from 'node:fs';
4
- import { homedir } from 'node:os';
5
4
  import { join, normalize } from 'node:path';
6
5
  import { pathToFileURL } from 'node:url';
7
- import { WorkbenchDataError } from '@dsh-workbench/shared/host';
6
+ import { runtimeLayoutOf, WorkbenchDataError } from '@dsh-workbench/shared/host';
8
7
  import { libInstallCommand } from '../foundation/install.js';
9
- function rootsOf(env) {
10
- const home = env['DSH_HOME'] ?? join(homedir(), '.dsh');
11
- return [
12
- join(home, 'workbench', 'opencli', 'libs', 'node_modules'),
13
- join(home, 'workbench', 'opencli', 'toolchain', 'node_modules'),
14
- ];
8
+ import { OPENCLI_RUNTIME_ID } from '../foundation/paths.js';
9
+ function rootsOf() {
10
+ const layout = runtimeLayoutOf(OPENCLI_RUNTIME_ID);
11
+ return [join(layout.libsPrefix, 'node_modules'), join(layout.root, 'toolchain', 'node_modules')];
15
12
  }
16
13
  function entryProblem(entry) {
17
14
  if (entry.trim() === '')
@@ -23,8 +20,7 @@ function entryProblem(entry) {
23
20
  return null;
24
21
  }
25
22
  export function createLibReader(options = {}) {
26
- const env = options.env ?? process.env;
27
- const roots = options.roots ?? rootsOf(env);
23
+ const roots = options.roots ?? rootsOf();
28
24
  return {
29
25
  async load(need, entry) {
30
26
  const problem = entryProblem(entry);
@@ -0,0 +1,14 @@
1
+ export declare const OPENCLI_RUNTIME_ID = "opencli";
2
+ export declare const OPENCLI_NPM_PACKAGE = "@jackwener/opencli";
3
+ export declare function isolatedRoot(): string;
4
+ export declare function isolatedHome(): string;
5
+ export declare function vendorPrefix(): string;
6
+ export declare function vendorPluginsDir(): string;
7
+ export declare function libsPrefix(): string;
8
+ export declare function libsBinDir(): string;
9
+ export declare function npmCacheDir(): string;
10
+ export declare function toolchainOpencliDir(): string;
11
+ export declare function vendorPeerLink(): {
12
+ readonly dir: string;
13
+ readonly parent: string;
14
+ };
package/dist/paths.js ADDED
@@ -0,0 +1,33 @@
1
+ import { runtimeLayoutOf } from '@dsh-workbench/shared/host';
2
+ export const OPENCLI_RUNTIME_ID = 'opencli';
3
+ export const OPENCLI_NPM_PACKAGE = '@jackwener/opencli';
4
+ const layout = () => runtimeLayoutOf(OPENCLI_RUNTIME_ID);
5
+ export function isolatedRoot() {
6
+ return layout().root;
7
+ }
8
+ export function isolatedHome() {
9
+ return layout().home;
10
+ }
11
+ export function vendorPrefix() {
12
+ return layout().vendorPrefix;
13
+ }
14
+ export function vendorPluginsDir() {
15
+ return layout().vendorPluginsDir;
16
+ }
17
+ export function libsPrefix() {
18
+ return layout().libsPrefix;
19
+ }
20
+ export function libsBinDir() {
21
+ return layout().libsBinDir;
22
+ }
23
+ export function npmCacheDir() {
24
+ return layout().npmCacheDir;
25
+ }
26
+ export function toolchainOpencliDir() {
27
+ return `${isolatedRoot()}/toolchain/node_modules/${OPENCLI_NPM_PACKAGE}`;
28
+ }
29
+ export function vendorPeerLink() {
30
+ const base = `${vendorPrefix()}/node_modules`;
31
+ const [scope] = OPENCLI_NPM_PACKAGE.split('/');
32
+ return { dir: `${base}/${OPENCLI_NPM_PACKAGE}`, parent: `${base}/${scope}` };
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dsh-workbench/adapter-runtime",
3
- "version": "1.1.14-beta.2",
3
+ "version": "1.1.15",
4
4
  "description": "Opencli, browser and data runtime adapters.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,8 +17,7 @@
17
17
  "bin"
18
18
  ],
19
19
  "dependencies": {
20
- "execa": "^10.2.0",
21
- "@dsh-workbench/shared": "1.1.14-beta.2"
20
+ "@dsh-workbench/shared": "1.1.15"
22
21
  },
23
22
  "devDependencies": {
24
23
  "@types/node": "^24.19.0",