@mbws/plugin-sdk 0.3.21 → 0.4.0
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 +54 -1
- package/dist/client.d.ts +73 -3
- package/dist/client.js +124 -3
- package/dist/config.d.ts +51 -0
- package/dist/dev-mock.js +70 -5
- package/dist/manifest.d.ts +3 -0
- package/dist/manifest.js +10 -0
- package/dist/protocol.d.ts +34 -1
- package/dist/protocol.js +67 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -180,10 +180,15 @@ mbws.panel.onBeforeClose(async () => ({
|
|
|
180
180
|
|
|
181
181
|
```ts
|
|
182
182
|
// ① spawn 前:预占产物名(冲突缺省 auto-rename,返回最终可用名)+ 占位符
|
|
183
|
+
// 落点两形态(dir 与 jobId 至少其一,0.3.22 起与宿主契约对齐):
|
|
184
|
+
// - 绝对 dir:须在本会话 pickDirectory/pickFile 选中范围内——spawn 前预占的唯一途径
|
|
185
|
+
// - 相对 dir / 缺省:解析进 jobId 对应 job 沙箱的 out/,jobId 必填且 job 须已 spawn
|
|
186
|
+
// (编码中途补占用;产物写沙箱 out/ 的插件通常无需预占——每个 spawn 全新沙箱,out/ 空)
|
|
183
187
|
const { filename, placeholder } = await mbws.caps.exec.reserveOutput({
|
|
184
188
|
filename: "cut1_JJD巨人S4.mp4",
|
|
185
|
-
dir: outputDir, //
|
|
189
|
+
dir: outputDir, // pick 过的绝对路径
|
|
186
190
|
});
|
|
191
|
+
// 或 spawn 之后补占:reserveOutput({ filename, jobId, onConflict? })
|
|
187
192
|
// 用返回的 filename 组装 spawn 的输出路径(冲突时已是 `名 (1).ext`)
|
|
188
193
|
|
|
189
194
|
// ①' 占位符先行(0.3.10+):预占即发——编码开始前就把字段值 commit 成占位符,
|
|
@@ -280,6 +285,36 @@ viewer 容器 grants 还含 caps(有桥时)——`caps.exec.mediaUrl(local:
|
|
|
280
285
|
|
|
281
286
|
- **生产(宿主 sandbox iframe 内)**:等待宿主经 `window.postMessage` 递来的
|
|
282
287
|
`MessagePort`,完成 init 握手后按授权构造命名空间。此后所有调用都走端口消息。
|
|
288
|
+
生产又分两种装载形态(init 的 `mode` 字段声明,0.4.0 起):
|
|
289
|
+
- **panel(缺省)**:交互面板——工人打开工具弹窗的常规形态;
|
|
290
|
+
- **auto**:无头自动执行——宿主无人值守驱动(「本地工具批量预跑」)。注册
|
|
291
|
+
`mbws.auto?.onRun(async ({ params, signal, timeoutMs }) => { ... })` 即支持:
|
|
292
|
+
SDK 发 `auto:ready` 握手后回调被调一次。收尾两条路径任一到达即结束:
|
|
293
|
+
1. `data.apply/commit` 写回(等价 succeeded);
|
|
294
|
+
2. `mbws.auto.done({ status })` 显式声明——`skipped` 是一等态(如人脸检测
|
|
295
|
+
0 张脸:不算失败、不写值、留给人工复核),`succeeded` 用于「成功但无需写值」,
|
|
296
|
+
`failed` 携带 message。回调既不写回也不 done 就返回 → SDK 代发
|
|
297
|
+
done(failed)(视为插件缺陷,明确报错优于挂到超时)。
|
|
298
|
+
面板形态 `mbws.auto === undefined`——按它分支即可;建议 panel 与 onRun 共用同一份
|
|
299
|
+
核心逻辑(脚手架 starter 即此结构)。要求 manifest `automation: "batchable"`。
|
|
300
|
+
|
|
301
|
+
**运行参数(声明式参数通道)**:插件需要可调参数(打码强度、检测阈值等)时在
|
|
302
|
+
mbws.config.ts 声明 `auto: { params: <JSON Schema> }`(object 形态,默认值内嵌
|
|
303
|
+
`properties.*.default`)——宿主批量预跑入队时按 schema 弹表单收集一次性参数
|
|
304
|
+
(不声明则不弹表单,零成本),收集值(默认值 ← 表单值合并)经 init 下发,
|
|
305
|
+
`onRun` 回调 `ctx.params` 原样收到(未声明恒空对象)。与顶层 `paramsSchema`
|
|
306
|
+
(spec 侧参数位)互不相干。进度上报用既有 `mbws.runtime.progress(ratio, message)`
|
|
307
|
+
——auto 模式照常可用,宿主转发到批量队列逐条展示。
|
|
308
|
+
|
|
309
|
+
**auto 模式的三条边界约定**:
|
|
310
|
+
- **不保证 DOM**:当前宿主实现是隐藏 iframe,但协议按「与容器无关」规范——auto
|
|
311
|
+
回调里不要依赖 `document`/`window` 上任何 UI(将来换 worker/轻量 core 运行时,
|
|
312
|
+
同一份插件代码不改);
|
|
313
|
+
- **不授予 ui**:auto 形态 `mbws.ui === undefined`(宿主 grants 不含 ui,客户端
|
|
314
|
+
不构造该面)——无头代码不要调用 confirm/toast;
|
|
315
|
+
- **超时可协商**:宿主 init 带 `autoTimeoutMs`(ctx.timeoutMs 同源);插件需要更长
|
|
316
|
+
时间时在 onRun 里……见下——目前协议面是插件经 `auto:ready` 声明(SDK 按需扩展
|
|
317
|
+
透传,宿主取 max(声明值, 自身配置),只升不降)。
|
|
283
318
|
- **开发(本地 `pnpm dev` / 顶窗口 / 端口等待超时)**:自动降级为 dev mock——
|
|
284
319
|
`data.inputs` / `viewer.inputs` 读工程内 `samples/inputs.json`、`viewer.outputs` /
|
|
285
320
|
`data.outputs` 读 `samples/outputs.json`(均原样返回,缺省回落 per-key 合并);
|
|
@@ -311,6 +346,24 @@ dev 面模拟:`MBWS_DEV_HOST_FACE=<ver>` 环境变量让 dev mock 按
|
|
|
311
346
|
`host.capabilities()` 一致生效)——降级分支在浏览器 dev 即可测试,如
|
|
312
347
|
`MBWS_DEV_HOST_FACE=0.3.9 pnpm dev`。
|
|
313
348
|
|
|
349
|
+
**auto 无头链路的 dev 工作流(0.4.0 起,不装桌面端即可 e2e)**:
|
|
350
|
+
`pnpm dev` 起 panel(dev mock 形态),控制台调 `await window.__mbwsDev.autoRun(params?)`
|
|
351
|
+
——它以 `{ params: params ?? {}, signal, timeoutMs: 300_000 }` 调一次你注册的
|
|
352
|
+
`auto.onRun` 回调(params 即宿主按 `auto.params` 声明收集下发的运行参数,模拟传参),
|
|
353
|
+
与宿主无头装载同一入口签名;输入读 `samples/inputs.json` 夹具、`data.apply` 写回
|
|
354
|
+
`samples/outputs.json`(与面板一致)。模拟各结局:
|
|
355
|
+
|
|
356
|
+
| 想模拟的结局 | 做法 |
|
|
357
|
+
| --- | --- |
|
|
358
|
+
| skipped(业务性跳过) | 回调里 `mbws.auto.done({ status: "skipped", message: "原因" })` |
|
|
359
|
+
| failed(自判失败) | 回调里 `mbws.auto.done({ status: "failed", message: "原因" })` |
|
|
360
|
+
| failed(抛错,含 stderr 尾巴) | 回调里 `throw jobFailedError(...)` 或任意 Error |
|
|
361
|
+
| 取消 | devRun 前拿不到 signal 句柄——直接在回调里检查传入的 `ctx.signal` 分支覆盖 |
|
|
362
|
+
|
|
363
|
+
`window.__mbwsDev.autoDone()` 返回最近一次 `auto.done` 的 `{ status, message }`
|
|
364
|
+
(harness 断言用;未调用过为 null)。会话级单执行与端口客户端一致(autoRun 只生效
|
|
365
|
+
首次)。
|
|
366
|
+
|
|
314
367
|
dev 故障模拟:`MBWS_DEV_SIMULATE=<token>[,<token>...]` 往 happy path 里注入指定
|
|
315
368
|
故障,插件的降级/重试分支浏览器 dev 即可覆盖(语义与宿主对齐):
|
|
316
369
|
|
package/dist/client.d.ts
CHANGED
|
@@ -191,18 +191,29 @@ export interface MbwsClient<C extends MbwsContractTypes = MbwsContractTypes> {
|
|
|
191
191
|
*/
|
|
192
192
|
jobResult(jobId: string): Promise<JobResultPayload>;
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
194
|
+
* 产物名预占 + 覆盖预检(编码几分钟,跑完才发现同名冲突太浪费)。
|
|
195
|
+
* 冲突源:目标目录内既有同名文件 / 上传队列同路径条目。onConflict 缺省
|
|
196
196
|
* auto-rename(返回可用最终名,`名 (1).ext` 递增);reject 返回 conflict 由插件处置。
|
|
197
|
-
* dir 缺省 = job 沙箱 out/;绝对路径须在本会话 pickDirectory/pickFile 选中范围内。
|
|
198
197
|
* 预占会在上传中心立一条 reserved 条目(编码中可见),registerOutput 到达转正。
|
|
199
198
|
*
|
|
199
|
+
* 落点两形态(与宿主 reserveTargetDir 同源,dir 与 jobId 至少其一):
|
|
200
|
+
* - dir 为绝对路径:须在本会话 pickDirectory/pickFile 选中范围内(防外泄闸,
|
|
201
|
+
* 与 registerOutput 同源)——spawn 之前想预占只有这一种途径;
|
|
202
|
+
* - dir 为相对路径或缺省:解析进 jobId 对应 job 沙箱的 out/(缺省)或沙箱内
|
|
203
|
+
* 该相对目录;jobId 必填且 job 必须已存在(job 沙箱是 spawn 时才创建的,
|
|
204
|
+
* 即只能 spawn 之后补占,用于编码中途预占/上传中心可见)。产物写 job 沙箱
|
|
205
|
+
* out/ 的插件通常不需要预占——每个 spawn 一个全新沙箱,out/ 初始为空,
|
|
206
|
+
* 不存在同名冲突。
|
|
207
|
+
* (0.3.22 修正:此前文档写「dir 缺省 = job 沙箱 out/」但类型没有 jobId,
|
|
208
|
+
* 按旧文档写的不带 dir/jobId 调用在真宿主上必 E_INVALID_ARGS。)
|
|
209
|
+
*
|
|
200
210
|
* 占位符先行(0.3.10+):返回带 placeholder(与后续 registerOutput 返回同一值),
|
|
201
211
|
* 编码开始前即可 apply/commit 字段值;旧宿主不返回该字段(探测降级回编码后登记)
|
|
202
212
|
*/
|
|
203
213
|
reserveOutput(req: {
|
|
204
214
|
filename: string;
|
|
205
215
|
dir?: string;
|
|
216
|
+
jobId?: string;
|
|
206
217
|
onConflict?: "auto-rename" | "reject";
|
|
207
218
|
}): Promise<ReservedOutput>;
|
|
208
219
|
/**
|
|
@@ -294,6 +305,36 @@ export interface MbwsClient<C extends MbwsContractTypes = MbwsContractTypes> {
|
|
|
294
305
|
outputs(): Promise<Partial<MbwsContractOutputs<C>>>;
|
|
295
306
|
onChange(cb: (outputs: Partial<MbwsContractOutputs<C>>) => void): () => void;
|
|
296
307
|
};
|
|
308
|
+
/**
|
|
309
|
+
* auto 无头执行面(0.4.0 起;宿主 init `mode:"auto"` 装载形态才构造——面板形态
|
|
310
|
+
* undefined,插件按 `mbws.auto?.onRun` 判断分支。auto 模式宿主不授 ui 面权限,
|
|
311
|
+
* `mbws.ui` 为 undefined——无头代码不要调用 ui.confirm/toast):
|
|
312
|
+
*
|
|
313
|
+
* - `onRun(cb)`:注册即握手——SDK 发 auto:ready 告知宿主「本插件支持自动执行」,
|
|
314
|
+
* 随后回调被调一次(ctx 见 AutoRunContext:params/signal/timeoutMs 定型)。
|
|
315
|
+
* 回调内两条收尾路径任一到达即结束:
|
|
316
|
+
* 1. `data.apply/commit` 写回(兼容旧写法,等价 succeeded);
|
|
317
|
+
* 2. `done({ status })` 显式声明结局——skipped 是一等态(如人脸检测 0 张脸:
|
|
318
|
+
* 不算失败、不写值、留给人工复核),succeeded 用于「成功但无需写值」,
|
|
319
|
+
* failed 携带 message。回调既不写回也不 done 就返回 → SDK 代发
|
|
320
|
+
* done(failed)(视为插件缺陷,明确报错优于挂到超时)。
|
|
321
|
+
* - 回调抛错 → SDK 代发 auto:error(message 200 字符截断;MbwsInvokeError 的
|
|
322
|
+
* data(如 stderrTail)随消息透传,宿主可展示失败原因尾巴)。
|
|
323
|
+
*/
|
|
324
|
+
auto?: {
|
|
325
|
+
/**
|
|
326
|
+
* 注册自动执行回调(opts.timeoutMs 声明本次执行的建议超时上限,随 auto:ready
|
|
327
|
+
* 上报——宿主取 max(声明值, 自身配置) 只升不降;视频类长任务按需协商)
|
|
328
|
+
*/
|
|
329
|
+
onRun(cb: (ctx: AutoRunContext) => Promise<void> | void, opts?: {
|
|
330
|
+
timeoutMs?: number;
|
|
331
|
+
}): void;
|
|
332
|
+
/** 显式声明结局(见上;fire-and-forget,调用后不要再发 apply/done) */
|
|
333
|
+
done(result: {
|
|
334
|
+
status: "succeeded" | "skipped" | "failed";
|
|
335
|
+
message?: string;
|
|
336
|
+
}): void;
|
|
337
|
+
};
|
|
297
338
|
/**
|
|
298
339
|
* 数据结构(契约)面:拉关联任务的最新契约。不经宿主端口——服务端按插件编号
|
|
299
340
|
* (manifest.id)公开派生,dev 预览/宿主内同一调用;故不随 grants 增减、恒在。
|
|
@@ -308,6 +349,19 @@ export interface MbwsClient<C extends MbwsContractTypes = MbwsContractTypes> {
|
|
|
308
349
|
allows(outputKey: string, fieldPath: string): Promise<boolean>;
|
|
309
350
|
};
|
|
310
351
|
}
|
|
352
|
+
/** auto 无头执行回调上下文(onRun 定型签名;字段随版本只加不减) */
|
|
353
|
+
export interface AutoRunContext {
|
|
354
|
+
/**
|
|
355
|
+
* 运行参数(0.4.0 声明式参数通道):宿主按 manifest.auto.params 声明收集的
|
|
356
|
+
* 一次性参数(schema 默认值 ← 入队表单值合并后下发);未声明/未下发 → 空对象。
|
|
357
|
+
* dev 预览经 window.__mbwsDev.autoRun(params) 手动传入。
|
|
358
|
+
*/
|
|
359
|
+
params: Record<string, unknown>;
|
|
360
|
+
/** 宿主取消信号(abort 后应尽快收尾/丢弃写回) */
|
|
361
|
+
signal: AbortSignal;
|
|
362
|
+
/** 本次执行总超时(宿主下发;长任务据此设 checkpoint) */
|
|
363
|
+
timeoutMs: number;
|
|
364
|
+
}
|
|
311
365
|
/** 调用被拒时抛出的规范错误(error.code 是 9 错误码之一) */
|
|
312
366
|
export declare class MbwsInvokeError extends Error {
|
|
313
367
|
readonly code: MbwsErrorCode;
|
|
@@ -333,6 +387,19 @@ export declare function writeFileInvalid(req: {
|
|
|
333
387
|
path?: unknown;
|
|
334
388
|
content?: unknown;
|
|
335
389
|
}): MbwsInvokeError | null;
|
|
390
|
+
/**
|
|
391
|
+
* caps.exec.reserveOutput 入参校验(端口客户端与 dev-mock 共用;与宿主
|
|
392
|
+
* apps/desktop plugin-caps.ts 的 handler + reserveTargetDir 同源同形):
|
|
393
|
+
* - filename 必须是非空纯文件名(不含路径分隔符);
|
|
394
|
+
* - 落点二选一:绝对 dir(宿主侧另有本会话 pick 范围闸,客户端判不了)或 jobId
|
|
395
|
+
* (相对 dir / 缺省都解析进 job 沙箱,无 jobId 无从定位——相对 dir 不带 jobId
|
|
396
|
+
* 同样拒,宿主相对分支需要 jobDir(jobId))。返回 null = 通过。
|
|
397
|
+
*/
|
|
398
|
+
export declare function reserveOutputInvalid(req: {
|
|
399
|
+
filename?: unknown;
|
|
400
|
+
dir?: unknown;
|
|
401
|
+
jobId?: unknown;
|
|
402
|
+
}): MbwsInvokeError | null;
|
|
336
403
|
export interface CreateMbwsOptions {
|
|
337
404
|
/** 等宿主 init 端口的超时(ms),超时走 dev mock。默认 3000。 */
|
|
338
405
|
timeoutMs?: number;
|
|
@@ -346,6 +413,9 @@ export interface CreateMbwsOptions {
|
|
|
346
413
|
/** 分层可测:纯端口逻辑(不碰 window),单测用 Node MessageChannel 直连 broker。 */
|
|
347
414
|
export declare function connectMbwsPort<C extends MbwsContractTypes = MbwsContractTypes>(port: MessagePort, init: {
|
|
348
415
|
grants: string[];
|
|
416
|
+
mode?: "panel" | "auto";
|
|
417
|
+
autoTimeoutMs?: number;
|
|
418
|
+
autoParams?: Record<string, unknown>;
|
|
349
419
|
theme?: Record<string, string>;
|
|
350
420
|
attachments?: AttachmentState[];
|
|
351
421
|
}, opts?: Pick<CreateMbwsOptions, "invokeTimeoutMs">): MbwsClient<C>;
|
package/dist/client.js
CHANGED
|
@@ -2,7 +2,7 @@ import { asMbwsRef, CAPS_WRITE_FILE_MAX_BYTES, mergeAttachmentEvent, mergeUpload
|
|
|
2
2
|
import { contractFieldAllowed, fetchContract, } from "./contract.js";
|
|
3
3
|
import { createDevMockMbws } from "./dev-mock.js";
|
|
4
4
|
import { MBWS_ERROR_CODES } from "./errors.js";
|
|
5
|
-
import { parseMbwsMessage } from "./protocol.js";
|
|
5
|
+
import { AUTO_ERROR_MESSAGE_MAX, parseMbwsMessage } from "./protocol.js";
|
|
6
6
|
import { uuid } from "./uuid.js";
|
|
7
7
|
import { PROTOCOL_VERSION, SDK_VERSION } from "./version.js";
|
|
8
8
|
/** 调用被拒时抛出的规范错误(error.code 是 9 错误码之一) */
|
|
@@ -62,6 +62,33 @@ export function writeFileInvalid(req) {
|
|
|
62
62
|
}
|
|
63
63
|
return null;
|
|
64
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* caps.exec.reserveOutput 入参校验(端口客户端与 dev-mock 共用;与宿主
|
|
67
|
+
* apps/desktop plugin-caps.ts 的 handler + reserveTargetDir 同源同形):
|
|
68
|
+
* - filename 必须是非空纯文件名(不含路径分隔符);
|
|
69
|
+
* - 落点二选一:绝对 dir(宿主侧另有本会话 pick 范围闸,客户端判不了)或 jobId
|
|
70
|
+
* (相对 dir / 缺省都解析进 job 沙箱,无 jobId 无从定位——相对 dir 不带 jobId
|
|
71
|
+
* 同样拒,宿主相对分支需要 jobDir(jobId))。返回 null = 通过。
|
|
72
|
+
*/
|
|
73
|
+
export function reserveOutputInvalid(req) {
|
|
74
|
+
const filename = typeof req.filename === "string" ? req.filename : "";
|
|
75
|
+
if (!filename || /[\\/]/.test(filename)) {
|
|
76
|
+
return new MbwsInvokeError({
|
|
77
|
+
code: MBWS_ERROR_CODES.E_INVALID_ARGS,
|
|
78
|
+
message: "filename 必须是非空纯文件名(不含路径分隔符)",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const dir = typeof req.dir === "string" ? req.dir : "";
|
|
82
|
+
const hasJobId = typeof req.jobId === "string" && req.jobId !== "";
|
|
83
|
+
// 浏览器侧无 node:path:绝对路径按三形态判(与 writeFileInvalid 同款)
|
|
84
|
+
if (!/^(?:[a-zA-Z]:[\\/]|\\\\|\/)/.test(dir) && !hasJobId) {
|
|
85
|
+
return new MbwsInvokeError({
|
|
86
|
+
code: MBWS_ERROR_CODES.E_INVALID_ARGS,
|
|
87
|
+
message: "dir/jobId 缺一:无 dir 时需 jobId 定位 job 沙箱",
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
65
92
|
/**
|
|
66
93
|
* callId 生成走专用 uuid()(getRandomValues 直拼)——randomUUID 是 secure-context-only,
|
|
67
94
|
* 插件面板经 http 的 registry 代理装载(dev)时不存在,历史上已踩坑。
|
|
@@ -170,6 +197,72 @@ export function connectMbwsPort(port, init, opts = {}) {
|
|
|
170
197
|
};
|
|
171
198
|
/** 已结算 job 的 wait() 结果:exit 0 resolve,非零 reject(与 dev-mock 同一构造函数) */
|
|
172
199
|
const settleJobWait = (result) => result.exitCode === 0 ? Promise.resolve(result) : Promise.reject(jobFailedError(result));
|
|
200
|
+
// auto 无头执行(mode=auto 装载形态):onRun 注册即发 auto:ready 并执行一次(会话
|
|
201
|
+
// 内只执行首个注册的回调,重复注册忽略);收尾三路——data.apply/commit(等价
|
|
202
|
+
// succeeded,旧写法兼容)、显式 done()(succeeded/skipped/failed 三态)、回调抛错
|
|
203
|
+
// (代发 auto:error,MbwsInvokeError 的 data 如 stderrTail 透传)。回调正常返回但
|
|
204
|
+
// 既未写回也未 done → 代发 done(failed)(插件缺陷明确报错,优于挂到超时)。未注册
|
|
205
|
+
// 不发 ready——宿主按 no-auto 快速失败,不耗整条超时额度去猜
|
|
206
|
+
let autoStarted = false;
|
|
207
|
+
let autoApplyInitiated = false;
|
|
208
|
+
let autoDoneSent = false;
|
|
209
|
+
const autoCtx = {
|
|
210
|
+
params: init.autoParams ?? {},
|
|
211
|
+
signal: abortController.signal,
|
|
212
|
+
timeoutMs: init.autoTimeoutMs ?? 300_000,
|
|
213
|
+
};
|
|
214
|
+
const postAutoError = (error) => {
|
|
215
|
+
console.warn("[mbws] auto.onRun 回调抛错", error);
|
|
216
|
+
const text = (error instanceof Error ? error.message : String(error)).slice(0, AUTO_ERROR_MESSAGE_MAX);
|
|
217
|
+
const data = error?.data;
|
|
218
|
+
post({
|
|
219
|
+
v: PROTOCOL_VERSION,
|
|
220
|
+
type: "auto:error",
|
|
221
|
+
message: text,
|
|
222
|
+
...(data !== undefined ? { data } : {}),
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
const runAuto = (cb, opts) => {
|
|
226
|
+
if (autoStarted)
|
|
227
|
+
return;
|
|
228
|
+
autoStarted = true;
|
|
229
|
+
const declared = typeof opts?.timeoutMs === "number" && opts.timeoutMs > 0 ? opts.timeoutMs : undefined;
|
|
230
|
+
post({ v: PROTOCOL_VERSION, type: "auto:ready", ...(declared ? { timeoutMs: declared } : {}) });
|
|
231
|
+
// 异步链包一层:回调同步抛错同样落进 catch → auto:error(不冒泡到插件入口)
|
|
232
|
+
void Promise.resolve()
|
|
233
|
+
.then(() => cb(autoCtx))
|
|
234
|
+
.then(() => {
|
|
235
|
+
if (autoApplyInitiated || autoDoneSent)
|
|
236
|
+
return;
|
|
237
|
+
autoDoneSent = true;
|
|
238
|
+
post({
|
|
239
|
+
v: PROTOCOL_VERSION,
|
|
240
|
+
type: "auto:done",
|
|
241
|
+
status: "failed",
|
|
242
|
+
message: "onRun 回调既未写回(data.apply/commit)也未声明完成(auto.done)",
|
|
243
|
+
});
|
|
244
|
+
})
|
|
245
|
+
.catch((error) => {
|
|
246
|
+
if (autoDoneSent || autoApplyInitiated)
|
|
247
|
+
return;
|
|
248
|
+
postAutoError(error);
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
const autoDone = (result) => {
|
|
252
|
+
if (autoDoneSent || autoApplyInitiated) {
|
|
253
|
+
console.warn("[mbws] auto.done 在已收尾后再次调用,忽略");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
autoDoneSent = true;
|
|
257
|
+
post({
|
|
258
|
+
v: PROTOCOL_VERSION,
|
|
259
|
+
type: "auto:done",
|
|
260
|
+
status: result.status,
|
|
261
|
+
...(result.message !== undefined
|
|
262
|
+
? { message: result.message.slice(0, AUTO_ERROR_MESSAGE_MAX) }
|
|
263
|
+
: {}),
|
|
264
|
+
});
|
|
265
|
+
};
|
|
173
266
|
/** spawn 返回的裸 jobId 包装成 ExecJob(wait 以 exit 事件结算,断线走 jobResult 兜底) */
|
|
174
267
|
const makeExecJob = (jobId) => {
|
|
175
268
|
jobSessionOf(jobId);
|
|
@@ -274,8 +367,18 @@ export function connectMbwsPort(port, init, opts = {}) {
|
|
|
274
367
|
inputs: () => invoke("data", "inputs"),
|
|
275
368
|
outputs: () => invoke("data", "outputs"),
|
|
276
369
|
blob: (key) => invoke("data", "blob", { key }),
|
|
277
|
-
apply: (outputs) =>
|
|
278
|
-
|
|
370
|
+
apply: (outputs) => {
|
|
371
|
+
// auto 模式:apply 发起即视为收尾信号(宿主收到 outputs 立即结案,
|
|
372
|
+
// 插件随后 return 不触发 done(failed) 兜底)
|
|
373
|
+
if (init.mode === "auto")
|
|
374
|
+
autoApplyInitiated = true;
|
|
375
|
+
return invoke("data", "apply", { outputs });
|
|
376
|
+
},
|
|
377
|
+
commit: (outputs) => {
|
|
378
|
+
if (init.mode === "auto")
|
|
379
|
+
autoApplyInitiated = true;
|
|
380
|
+
return invoke("data", "commit", { outputs });
|
|
381
|
+
},
|
|
279
382
|
},
|
|
280
383
|
}
|
|
281
384
|
: {}),
|
|
@@ -343,6 +446,11 @@ export function connectMbwsPort(port, init, opts = {}) {
|
|
|
343
446
|
return invoke("caps.exec", "writeFile", req);
|
|
344
447
|
},
|
|
345
448
|
reserveOutput: async (req) => {
|
|
449
|
+
// 落点契约(绝对 dir 或 jobId 二选一)客户端先拦一道——与宿主
|
|
450
|
+
// reserveTargetDir 同形,省一次注定被拒的往返(见 reserveOutputInvalid)
|
|
451
|
+
const invalid = reserveOutputInvalid(req);
|
|
452
|
+
if (invalid)
|
|
453
|
+
throw invalid;
|
|
346
454
|
const reserved = (await invoke("caps.exec", "reserveOutput", req));
|
|
347
455
|
// 占位符先行:预占即发——拿到即可 apply/commit 字段值
|
|
348
456
|
if (reserved.placeholder)
|
|
@@ -491,6 +599,16 @@ export function connectMbwsPort(port, init, opts = {}) {
|
|
|
491
599
|
},
|
|
492
600
|
}
|
|
493
601
|
: {}),
|
|
602
|
+
// auto 无头执行面:mode=auto 装载形态才构造(面板形态 undefined,插件按
|
|
603
|
+
// mbws.auto?.onRun 分支);dev mock 同形(dev 触发口手动驱动)
|
|
604
|
+
...(init.mode === "auto"
|
|
605
|
+
? {
|
|
606
|
+
auto: {
|
|
607
|
+
onRun: (cb, opts) => runAuto(cb, opts),
|
|
608
|
+
done: autoDone,
|
|
609
|
+
},
|
|
610
|
+
}
|
|
611
|
+
: {}),
|
|
494
612
|
// 契约面与端口无关(HTTP 直连),端口客户端/dev mock 同形,插件代码零分支
|
|
495
613
|
contract: {
|
|
496
614
|
get: (opts = {}) => fetchContract(opts),
|
|
@@ -539,6 +657,9 @@ export function bootstrapFromWindow(win, opts = {}) {
|
|
|
539
657
|
}
|
|
540
658
|
finish(connectMbwsPort(port, {
|
|
541
659
|
grants: message.grants,
|
|
660
|
+
...(message.mode ? { mode: message.mode } : {}),
|
|
661
|
+
...(message.autoTimeoutMs ? { autoTimeoutMs: message.autoTimeoutMs } : {}),
|
|
662
|
+
...(message.autoParams ? { autoParams: message.autoParams } : {}),
|
|
542
663
|
theme: message.theme,
|
|
543
664
|
...(message.attachments
|
|
544
665
|
? { attachments: message.attachments }
|
package/dist/config.d.ts
CHANGED
|
@@ -19,6 +19,57 @@ import { type AttachmentDecl, type AttachmentRef, type Manifest } from "./manife
|
|
|
19
19
|
*/
|
|
20
20
|
declare const UserConfigSchema: z.ZodObject<{
|
|
21
21
|
version: z.ZodString;
|
|
22
|
+
auto: z.ZodOptional<z.ZodObject<{
|
|
23
|
+
params: z.ZodUnion<readonly [z.ZodBoolean, z.ZodRecord<z.ZodString, z.ZodType<string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | {
|
|
24
|
+
[key: string]: string | number | boolean | /*elided*/ any | /*elided*/ any | null;
|
|
25
|
+
} | null)[] | {
|
|
26
|
+
[key: string]: string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
27
|
+
} | null)[] | {
|
|
28
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
29
|
+
} | null)[] | {
|
|
30
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
31
|
+
} | null)[] | {
|
|
32
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
33
|
+
} | null)[] | {
|
|
34
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
35
|
+
} | null)[] | {
|
|
36
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
37
|
+
} | null)[] | {
|
|
38
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
39
|
+
} | null)[] | {
|
|
40
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
41
|
+
} | null)[] | {
|
|
42
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
43
|
+
} | null)[] | {
|
|
44
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
45
|
+
} | null)[] | {
|
|
46
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
47
|
+
} | null, unknown, z.core.$ZodTypeInternals<string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | {
|
|
48
|
+
[key: string]: string | number | boolean | /*elided*/ any | /*elided*/ any | null;
|
|
49
|
+
} | null)[] | {
|
|
50
|
+
[key: string]: string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
51
|
+
} | null)[] | {
|
|
52
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
53
|
+
} | null)[] | {
|
|
54
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
55
|
+
} | null)[] | {
|
|
56
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
57
|
+
} | null)[] | {
|
|
58
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
59
|
+
} | null)[] | {
|
|
60
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
61
|
+
} | null)[] | {
|
|
62
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
63
|
+
} | null)[] | {
|
|
64
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
65
|
+
} | null)[] | {
|
|
66
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
67
|
+
} | null)[] | {
|
|
68
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
69
|
+
} | null)[] | {
|
|
70
|
+
[key: string]: string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | (string | number | boolean | /*elided*/ any | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null)[] | /*elided*/ any | null;
|
|
71
|
+
} | null, unknown>>>]>;
|
|
72
|
+
}, z.core.$strip>>;
|
|
22
73
|
kind: z.ZodEnum<{
|
|
23
74
|
producer: "producer";
|
|
24
75
|
validator: "validator";
|
package/dist/dev-mock.js
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* (.mbws/attachments/ 缓存命中 = ready,否则 missing),attachmentStatus 本地读。
|
|
24
24
|
*/
|
|
25
25
|
import { asMbwsRef, CAPS_EXEC_METHODS, CAPS_UPLOADS_METHODS, METHOD_INTRODUCED_AT, mergeAttachmentEvent, mergeUploadEvent, sdkVersionAtLeast, } from "./caps.js";
|
|
26
|
-
import { jobFailedError, MbwsInvokeError, writeFileInvalid } from "./client.js";
|
|
26
|
+
import { jobFailedError, MbwsInvokeError, reserveOutputInvalid, writeFileInvalid, } from "./client.js";
|
|
27
27
|
import { contractFieldAllowed, fetchContract } from "./contract.js";
|
|
28
28
|
import { isStorageKeyValid, MemoryDevStorage, utf8ByteLength } from "./dev-storage.js";
|
|
29
29
|
import { MBWS_ERROR_CODES } from "./errors.js";
|
|
@@ -640,6 +640,15 @@ function storageKeyInvalid(key) {
|
|
|
640
640
|
export function createDevMockMbws() {
|
|
641
641
|
// 无 dev server 时的 storage 兜底(每个客户端一份 = 页面生命周期)
|
|
642
642
|
const memoryStorage = new MemoryDevStorage();
|
|
643
|
+
// dev auto 面(0.4.0):与端口客户端同形——注册只存回调,不自动触发(dev 无宿主
|
|
644
|
+
// 发 mode=auto 的 init);window.__mbwsDev.autoRun(params?) 手动驱动(harness/开发者
|
|
645
|
+
// 手测 auto 分支用,params 即宿主按 manifest.auto.params 收集后下发的运行参数——
|
|
646
|
+
// 缺省空对象,与端口客户端 init.autoParams 缺省回落同形)。会话级单执行:与端口
|
|
647
|
+
// 客户端 autoStarted 同语义。
|
|
648
|
+
let devAutoStarted = false;
|
|
649
|
+
const devAutoController = new AbortController();
|
|
650
|
+
let devAutoRunCb = null;
|
|
651
|
+
let devAutoDoneResult = null;
|
|
643
652
|
const client = {
|
|
644
653
|
data: {
|
|
645
654
|
inputs: fetchDevSamples,
|
|
@@ -786,11 +795,29 @@ export function createDevMockMbws() {
|
|
|
786
795
|
attachJob: (jobId) => Promise.resolve(attachDevJob(jobId)),
|
|
787
796
|
jobResult: (jobId) => Promise.resolve(devJobResult(jobId)),
|
|
788
797
|
writeFile: (req) => writeDevFile(req),
|
|
789
|
-
//
|
|
790
|
-
//
|
|
791
|
-
//
|
|
792
|
-
//
|
|
798
|
+
// 落点契约与宿主 reserveTargetDir 同形(共享 reserveOutputInvalid):无绝对
|
|
799
|
+
// dir 且无 jobId 拒 E_INVALID_ARGS——job 沙箱是 spawn 时才建的,spawn 前不
|
|
800
|
+
// 存在,缺省落点本就无从解析(真宿主必拒,dev 不能静默放过:face-mosaic
|
|
801
|
+
// 曾因此 dev 全绿、上桌面端即失败)。dev 差异:无 pick 范围闸(dev 没有
|
|
802
|
+
// pickDirectory/pickFile 会话概念),绝对 dir 一律放行不做范围校验;相对
|
|
803
|
+
// dir 也不落真沙箱(无盘可写),只校验 job 存在。
|
|
804
|
+
// 队列侧:dev 无宿主队列可查,恒无冲突返回原名 + 假占位(占位符先行协议
|
|
805
|
+
// 同构:预占即立 reserved 态镜像,编码完成前 mediaUrl 该占位抛 E_NOT_READY)。
|
|
806
|
+
// reserve-conflict 模拟:已预占名视为冲突——reject 同宿主返回 conflict 不
|
|
807
|
+
// 预占,auto-rename 同宿主 ` (i)` 方案改名后预占。
|
|
793
808
|
reserveOutput: async (req) => {
|
|
809
|
+
const invalid = reserveOutputInvalid(req);
|
|
810
|
+
if (invalid)
|
|
811
|
+
throw invalid;
|
|
812
|
+
// 相对 dir / 缺省落点:jobId 必须命中已 spawn 的 dev job——与宿主
|
|
813
|
+
// jobDir(jobId) 为空同形同 message(宿主对「带 jobId 但 job 不存在」也走这条)
|
|
814
|
+
const dir = req.dir ?? "";
|
|
815
|
+
if (!/^(?:[a-zA-Z]:[\\/]|\\\\|\/)/.test(dir) && !devJobSessions.has(req.jobId ?? "")) {
|
|
816
|
+
throw new MbwsInvokeError({
|
|
817
|
+
code: MBWS_ERROR_CODES.E_INVALID_ARGS,
|
|
818
|
+
message: "dir/jobId 缺一:无 dir 时需 jobId 定位 job 沙箱",
|
|
819
|
+
});
|
|
820
|
+
}
|
|
794
821
|
const conflictSim = readDevSimulate().has("reserve-conflict");
|
|
795
822
|
let finalName = req.filename;
|
|
796
823
|
if (conflictSim && devReservedNames.has(req.filename)) {
|
|
@@ -979,7 +1006,45 @@ export function createDevMockMbws() {
|
|
|
979
1006
|
return contractFieldAllowed(devContractSnapshot.outputs, outputKey, fieldPath);
|
|
980
1007
|
},
|
|
981
1008
|
},
|
|
1009
|
+
// auto 无头执行面 dev mock:注册只存回调(dev 无宿主发 mode=auto 的 init,不会
|
|
1010
|
+
// 自动进 auto 形态);经 window.__mbwsDev.autoRun() 手动驱动——面板/自动两分支
|
|
1011
|
+
// 同一份插件代码在浏览器 dev 即可联调。回调抛错 console.warn(dev 无宿主可发
|
|
1012
|
+
// auto:error),单执行语义与端口客户端一致
|
|
1013
|
+
auto: {
|
|
1014
|
+
onRun: (cb, _opts) => {
|
|
1015
|
+
// opts.timeoutMs(超时协商)dev 无宿主可发 auto:ready——收下不动作(dev 无超时)
|
|
1016
|
+
devAutoRunCb = cb;
|
|
1017
|
+
},
|
|
1018
|
+
// dev 同形:done 只记状态(无宿主可发 auto:done,控制台留痕);重复调用忽略
|
|
1019
|
+
done: (result) => {
|
|
1020
|
+
console.info("[mbws:dev] auto.done", result.status, result.message ?? "");
|
|
1021
|
+
devAutoDoneResult = { ...result };
|
|
1022
|
+
},
|
|
1023
|
+
},
|
|
982
1024
|
};
|
|
1025
|
+
// dev 触发口:autoRun() 手动驱动 auto 分支(幂等——会话内只执行首个注册的回调)
|
|
1026
|
+
if (typeof window !== "undefined") {
|
|
1027
|
+
const win = window;
|
|
1028
|
+
const devFace = win.__mbwsDev ?? {};
|
|
1029
|
+
devFace.autoRun = async (params) => {
|
|
1030
|
+
if (devAutoStarted || !devAutoRunCb)
|
|
1031
|
+
return;
|
|
1032
|
+
devAutoStarted = true;
|
|
1033
|
+
try {
|
|
1034
|
+
await devAutoRunCb({
|
|
1035
|
+
params: params ?? {},
|
|
1036
|
+
signal: devAutoController.signal,
|
|
1037
|
+
timeoutMs: 300_000,
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
catch (error) {
|
|
1041
|
+
console.warn("[mbws:dev] auto.onRun 回调抛错", error);
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
// 上一次 auto.done 的结果(harness/开发者断言 skipped/failed 分支用)
|
|
1045
|
+
devFace.autoDone = () => devAutoDoneResult;
|
|
1046
|
+
win.__mbwsDev = devFace;
|
|
1047
|
+
}
|
|
983
1048
|
// 面模拟收尾:不满足 face 的方法属性从对象上真删(capabilities 列表已过滤,
|
|
984
1049
|
// 这里让 typeof 探测的降级分支也真实成立)
|
|
985
1050
|
if (readDevHostFace() && client.caps) {
|
package/dist/manifest.d.ts
CHANGED
|
@@ -89,6 +89,9 @@ export declare const ManifestSchema: z.ZodObject<{
|
|
|
89
89
|
batchable: "batchable";
|
|
90
90
|
"interactive-only": "interactive-only";
|
|
91
91
|
}>;
|
|
92
|
+
auto: z.ZodOptional<z.ZodObject<{
|
|
93
|
+
params: z.ZodUnion<readonly [z.ZodBoolean, z.ZodRecord<z.ZodString, z.ZodType<Json, unknown, z.core.$ZodTypeInternals<Json, unknown>>>]>;
|
|
94
|
+
}, z.core.$strip>>;
|
|
92
95
|
components: z.ZodOptional<z.ZodObject<{
|
|
93
96
|
panel: z.ZodOptional<z.ZodString>;
|
|
94
97
|
viewer: z.ZodOptional<z.ZodString>;
|
package/dist/manifest.js
CHANGED
|
@@ -113,6 +113,16 @@ export const ManifestSchema = z.object({
|
|
|
113
113
|
permissions: z.array(z.string().regex(PERMISSION_RE)).default([]),
|
|
114
114
|
/** interactive-only 不进入批量/自动驱动 */
|
|
115
115
|
automation: z.enum(["batchable", "interactive-only"]),
|
|
116
|
+
/**
|
|
117
|
+
* auto 无头执行声明(0.4.0 起,可选):
|
|
118
|
+
* - `params`:批量运行参数的 JSON Schema(object 形态,默认值内嵌
|
|
119
|
+
* `properties.*.default`)——宿主「本地工具批量预跑」入队时按它收集一次性参数
|
|
120
|
+
* (缺省不弹参数表单,零成本),收集值经 init.autoParams 下发、插件在
|
|
121
|
+
* `auto.onRun` 回调的 `ctx.params` 原样收到。与顶层 paramsSchema(spec 侧
|
|
122
|
+
* 参数位)互不相干:auto.params 只服务无头批量这一条通道;
|
|
123
|
+
* - 面板形态不消费本字段。
|
|
124
|
+
*/
|
|
125
|
+
auto: z.object({ params: JsonSchemaValue }).optional(),
|
|
116
126
|
/** 产物组件的 html 相对路径,缺省 `panel.html` / `viewer.html` */
|
|
117
127
|
components: z
|
|
118
128
|
.object({
|
package/dist/protocol.d.ts
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
* 与 PROTOCOL_VERSION 不符的消息按协议违规直接拒绝(不静默兼容)。
|
|
6
6
|
*
|
|
7
7
|
* 方向约定(防线在宿主 broker 对每条进入消息独立校验,本模块只是契约形状):
|
|
8
|
-
* - plugin → host:调用(mbws:invoke)、生命周期上报(panel:ready / panel:resize / panel:before-close-reply / mbws:init-ack)
|
|
8
|
+
* - plugin → host:调用(mbws:invoke)、生命周期上报(panel:ready / panel:resize / panel:before-close-reply / mbws:init-ack / auto:ready / auto:error / auto:done)
|
|
9
|
+
*
|
|
10
|
+
* auto 形态(mode=auto,0.4.0 起)的协议边界:**与容器无关**——本层消息族不依赖
|
|
11
|
+
* 「隐藏 iframe」这一宿主实现(将来可换 worker / 轻量 core 运行时,插件代码不改);
|
|
12
|
+
* 插件 auto 回调里不应依赖 DOM 可用(不保证 document/window 的 UI)。
|
|
9
13
|
* - host → plugin:握手(mbws:init)、调用结果(mbws:resolve / mbws:reject)、事件(mbws:event)、
|
|
10
14
|
* 生命周期询问(panel:before-close)
|
|
11
15
|
*/
|
|
@@ -24,6 +28,8 @@ export declare const MBWS_NAMESPACES: readonly ["data", "runtime", "storage", "u
|
|
|
24
28
|
* payload 形状见 caps.ts。
|
|
25
29
|
*/
|
|
26
30
|
export declare const MBWS_EVENT_KINDS: readonly ["progress", "log", "cancel", "theme", "job", "attachment", "upload", "viewer"];
|
|
31
|
+
/** auto:error message 的截断上限(与 panel:before-close-reply message 同口径) */
|
|
32
|
+
export declare const AUTO_ERROR_MESSAGE_MAX = 200;
|
|
27
33
|
/**
|
|
28
34
|
* 关闭询问等待插件应答的超时(ms):超时按「无未保存」放行(防插件锁死面板)。
|
|
29
35
|
* 宿主 broker 的缺省值与插件文档的口径都以本常量为准。
|
|
@@ -41,6 +47,12 @@ export declare const MbwsMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
41
47
|
type: z.ZodLiteral<"mbws:init">;
|
|
42
48
|
protocolVersion: z.ZodLiteral<1>;
|
|
43
49
|
grants: z.ZodArray<z.ZodString>;
|
|
50
|
+
mode: z.ZodOptional<z.ZodEnum<{
|
|
51
|
+
panel: "panel";
|
|
52
|
+
auto: "auto";
|
|
53
|
+
}>>;
|
|
54
|
+
autoTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
55
|
+
autoParams: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
44
56
|
theme: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
45
57
|
attachments: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
46
58
|
name: z.ZodString;
|
|
@@ -110,6 +122,24 @@ export declare const MbwsMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
110
122
|
}, z.core.$strip>, z.ZodObject<{
|
|
111
123
|
v: z.ZodLiteral<1>;
|
|
112
124
|
type: z.ZodLiteral<"panel:close">;
|
|
125
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
126
|
+
v: z.ZodLiteral<1>;
|
|
127
|
+
type: z.ZodLiteral<"auto:ready">;
|
|
128
|
+
timeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
129
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
130
|
+
v: z.ZodLiteral<1>;
|
|
131
|
+
type: z.ZodLiteral<"auto:error">;
|
|
132
|
+
message: z.ZodString;
|
|
133
|
+
data: z.ZodOptional<z.ZodUnknown>;
|
|
134
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
135
|
+
v: z.ZodLiteral<1>;
|
|
136
|
+
type: z.ZodLiteral<"auto:done">;
|
|
137
|
+
status: z.ZodEnum<{
|
|
138
|
+
failed: "failed";
|
|
139
|
+
succeeded: "succeeded";
|
|
140
|
+
skipped: "skipped";
|
|
141
|
+
}>;
|
|
142
|
+
message: z.ZodOptional<z.ZodString>;
|
|
113
143
|
}, z.core.$strip>], "type">;
|
|
114
144
|
export type MbwsMessage = z.output<typeof MbwsMessageSchema>;
|
|
115
145
|
/** 消息方向表:契约形状层即可判定,broker 在此之上再做权限/配额校验 */
|
|
@@ -125,6 +155,9 @@ export declare const MESSAGE_DIRECTION: {
|
|
|
125
155
|
readonly "panel:before-close": "host-to-plugin";
|
|
126
156
|
readonly "panel:before-close-reply": "plugin-to-host";
|
|
127
157
|
readonly "panel:close": "plugin-to-host";
|
|
158
|
+
readonly "auto:ready": "plugin-to-host";
|
|
159
|
+
readonly "auto:error": "plugin-to-host";
|
|
160
|
+
readonly "auto:done": "plugin-to-host";
|
|
128
161
|
};
|
|
129
162
|
export type MessageDirection = (typeof MESSAGE_DIRECTION)[keyof typeof MESSAGE_DIRECTION];
|
|
130
163
|
export declare function isPluginToHost(message: MbwsMessage): boolean;
|
package/dist/protocol.js
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
* 与 PROTOCOL_VERSION 不符的消息按协议违规直接拒绝(不静默兼容)。
|
|
6
6
|
*
|
|
7
7
|
* 方向约定(防线在宿主 broker 对每条进入消息独立校验,本模块只是契约形状):
|
|
8
|
-
* - plugin → host:调用(mbws:invoke)、生命周期上报(panel:ready / panel:resize / panel:before-close-reply / mbws:init-ack)
|
|
8
|
+
* - plugin → host:调用(mbws:invoke)、生命周期上报(panel:ready / panel:resize / panel:before-close-reply / mbws:init-ack / auto:ready / auto:error / auto:done)
|
|
9
|
+
*
|
|
10
|
+
* auto 形态(mode=auto,0.4.0 起)的协议边界:**与容器无关**——本层消息族不依赖
|
|
11
|
+
* 「隐藏 iframe」这一宿主实现(将来可换 worker / 轻量 core 运行时,插件代码不改);
|
|
12
|
+
* 插件 auto 回调里不应依赖 DOM 可用(不保证 document/window 的 UI)。
|
|
9
13
|
* - host → plugin:握手(mbws:init)、调用结果(mbws:resolve / mbws:reject)、事件(mbws:event)、
|
|
10
14
|
* 生命周期询问(panel:before-close)
|
|
11
15
|
*/
|
|
@@ -52,6 +56,27 @@ const InitMessage = z.object({
|
|
|
52
56
|
protocolVersion: z.literal(PROTOCOL_VERSION),
|
|
53
57
|
/** 本次授予的能力清单:客户端只构造已授予的命名空间(按授权构造) */
|
|
54
58
|
grants: z.array(z.string()),
|
|
59
|
+
/**
|
|
60
|
+
* 宿主装载形态(0.4.0 起可选):
|
|
61
|
+
* - panel(缺省):交互面板——用户打开工具弹窗的常规形态;
|
|
62
|
+
* - auto:无头自动执行(批量预跑)——宿主开隐藏 iframe 无人值守驱动,
|
|
63
|
+
* 插件注册 client.auto.onRun 后 SDK 发 auto:ready 握手。
|
|
64
|
+
* 旧宿主 init 不带 mode(zod 缺省)——插件侧 auto 面缺席即面板形态,零分支兼容。
|
|
65
|
+
*/
|
|
66
|
+
mode: z.enum(["panel", "auto"]).optional(),
|
|
67
|
+
/**
|
|
68
|
+
* auto 形态的总超时(ms,mode=auto 时宿主下发;插件可经 auto:ready.timeoutMs
|
|
69
|
+
* 协商上调,最终值 = max(宿主配置, 插件声明))。onRun 回调 ctx.timeoutMs 同源——
|
|
70
|
+
* 长任务据此自行设 checkpoint。旧宿主不带 → SDK 用缺省 300_000。
|
|
71
|
+
*/
|
|
72
|
+
autoTimeoutMs: z.number().positive().optional(),
|
|
73
|
+
/**
|
|
74
|
+
* auto 形态的运行参数(mode=auto 时宿主下发;形状 = manifest.auto.params 收集值,
|
|
75
|
+
* 平铺键值)。插件侧 SDK 原样放进 onRun 回调的 ctx.params——插件按声明自己的
|
|
76
|
+
* schema 消费,协议层不做二次校验(收集侧按 schema 生成表单,天然合形)。
|
|
77
|
+
* 旧宿主/面板形态不带——ctx.params 回落空对象。
|
|
78
|
+
*/
|
|
79
|
+
autoParams: z.record(z.string(), z.unknown()).optional(),
|
|
55
80
|
/** 主题 token 表(CSS 自定义属性 → 值),插件侧写入 :root 跟随宿主主题 */
|
|
56
81
|
theme: z.record(z.string(), z.string()).optional(),
|
|
57
82
|
/**
|
|
@@ -123,6 +148,41 @@ const PanelCloseMessage = z.object({
|
|
|
123
148
|
v: z.literal(PROTOCOL_VERSION),
|
|
124
149
|
type: z.literal("panel:close"),
|
|
125
150
|
});
|
|
151
|
+
/**
|
|
152
|
+
* auto 模式握手(0.4.0 起,mode=auto 装载形态专用):
|
|
153
|
+
* 插件注册 client.auto.onRun 后 SDK 发 auto:ready——宿主据此确认「本插件支持
|
|
154
|
+
* 无头自动执行」(旧 SDK 插件不发 → 宿主按 no-auto 快速失败,不耗整条超时猜)。
|
|
155
|
+
* timeoutMs(可选,正数):插件声明本次执行的建议超时上限——宿主取
|
|
156
|
+
* max(声明值, 自身配置) 计算总超时(视频类长任务按需协商,缺省 300s)。
|
|
157
|
+
*/
|
|
158
|
+
const AutoReadyMessage = z.object({
|
|
159
|
+
v: z.literal(PROTOCOL_VERSION),
|
|
160
|
+
type: z.literal("auto:ready"),
|
|
161
|
+
timeoutMs: z.number().positive().optional(),
|
|
162
|
+
});
|
|
163
|
+
/** auto 模式执行失败上报(onRun 回调抛错时 SDK 代发;200 字符截断防滥用;data 可带 stderrTail 等结构化尾巴) */
|
|
164
|
+
const AutoErrorMessage = z.object({
|
|
165
|
+
v: z.literal(PROTOCOL_VERSION),
|
|
166
|
+
type: z.literal("auto:error"),
|
|
167
|
+
message: z.string().max(200),
|
|
168
|
+
data: z.unknown().optional(),
|
|
169
|
+
});
|
|
170
|
+
/**
|
|
171
|
+
* auto 模式显式完成(0.4.0 起):onRun 回调内调 client.auto.done() 声明结局——
|
|
172
|
+
* 三态有别于「apply=成功 / error=失败」的隐式两分:
|
|
173
|
+
* - succeeded:成功(未 apply 的「无需写值」完成也走这里);
|
|
174
|
+
* - skipped:业务性跳过(如人脸检测 0 张脸:不算失败、不写值、留给人工);
|
|
175
|
+
* - failed:插件自判失败(message 写原因)。
|
|
176
|
+
* 兼容:不调 done 而直接 data.apply/commit 的旧写法仍等价 succeeded(apply 触发即收)。
|
|
177
|
+
*/
|
|
178
|
+
const AutoDoneMessage = z.object({
|
|
179
|
+
v: z.literal(PROTOCOL_VERSION),
|
|
180
|
+
type: z.literal("auto:done"),
|
|
181
|
+
status: z.enum(["succeeded", "skipped", "failed"]),
|
|
182
|
+
message: z.string().max(200).optional(),
|
|
183
|
+
});
|
|
184
|
+
/** auto:error message 的截断上限(与 panel:before-close-reply message 同口径) */
|
|
185
|
+
export const AUTO_ERROR_MESSAGE_MAX = 200;
|
|
126
186
|
/**
|
|
127
187
|
* 关闭询问等待插件应答的超时(ms):超时按「无未保存」放行(防插件锁死面板)。
|
|
128
188
|
* 宿主 broker 的缺省值与插件文档的口径都以本常量为准。
|
|
@@ -140,6 +200,9 @@ export const MbwsMessageSchema = z.discriminatedUnion("type", [
|
|
|
140
200
|
PanelBeforeCloseMessage,
|
|
141
201
|
PanelBeforeCloseReplyMessage,
|
|
142
202
|
PanelCloseMessage,
|
|
203
|
+
AutoReadyMessage,
|
|
204
|
+
AutoErrorMessage,
|
|
205
|
+
AutoDoneMessage,
|
|
143
206
|
]);
|
|
144
207
|
/** 消息方向表:契约形状层即可判定,broker 在此之上再做权限/配额校验 */
|
|
145
208
|
export const MESSAGE_DIRECTION = {
|
|
@@ -154,6 +217,9 @@ export const MESSAGE_DIRECTION = {
|
|
|
154
217
|
"panel:before-close": "host-to-plugin",
|
|
155
218
|
"panel:before-close-reply": "plugin-to-host",
|
|
156
219
|
"panel:close": "plugin-to-host",
|
|
220
|
+
"auto:ready": "plugin-to-host",
|
|
221
|
+
"auto:error": "plugin-to-host",
|
|
222
|
+
"auto:done": "plugin-to-host",
|
|
157
223
|
};
|
|
158
224
|
export function isPluginToHost(message) {
|
|
159
225
|
return MESSAGE_DIRECTION[message.type] === "plugin-to-host";
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED