接续L4-TOOL-DB浏览器Provider协议

结论:浏览器 Worker 已支持可插拔 Python runtime provider 的 line I/O,缺真实 Python/WASM runtime 时仍保持阻塞不解锁。
This commit is contained in:
2026-06-19 07:13:52 +08:00
parent 63870fd6d7
commit db33224dd0
6 changed files with 159 additions and 4 deletions

View File

@@ -80,6 +80,66 @@ class FakeBlockedWorker {
terminate() {}
}
class FakeReadyWorker {
constructor() {
this.listeners = new Map();
this.lines = [
"v2.1",
"T10 P110 D0.10 Z0.10",
"FINI (get_cmd)",
"FINI (update recvd) t11 p111 d0.33 z0.11",
"FINI (update recvd) t14 p0",
"FINI (update recvd) t0 p0",
];
}
addEventListener(type, listener) {
this.listeners.set(type, listener);
}
postMessage(message) {
queueMicrotask(() => {
if (message.type === "start") {
this.reply(message, {
runtimeExecutionReady: true,
status: "browser_python_wasm_runtime_ready",
executionEnabled: false,
promotionAllowed: false,
});
return;
}
if (message.type === "writeLine") {
this.reply(message, { executionEnabled: false, promotionAllowed: false });
return;
}
if (message.type === "readLine") {
this.reply(message, {
line: this.lines.shift(),
executionEnabled: false,
promotionAllowed: false,
});
return;
}
if (message.type === "close") {
this.reply(message, { executionEnabled: false, promotionAllowed: false });
}
});
}
reply(message, payload) {
this.listeners.get("message")?.({
data: {
id: message.id,
type: message.type,
ok: true,
...payload,
},
});
}
terminate() {}
}
const browserWorkerAdapter = createLinuxCncToolDbBrowserWorkerAdapter({
workerUrl: "./tool-db-python-worker.js",
workerFactory: () => new FakeBlockedWorker(),
@@ -99,6 +159,25 @@ assert.equal(browserWorkerBlockedPlan.runtimeExecutionReady, false);
assert.equal(browserWorkerBlockedPlan.promotionAllowed, false);
await browserWorkerPort.close();
const readyWorkerAdapter = createLinuxCncToolDbBrowserWorkerAdapter({
workerUrl: "./tool-db-python-worker.js",
workerFactory: () => new FakeReadyWorker(),
});
const readyWorkerPort = createLinuxCncToolDbProcessPort({
dbProgramPath: "./db_nonran.py",
runtimeMode: readyWorkerAdapter.runtimeMode,
runtimeAdapter: readyWorkerAdapter,
});
const readyWorkerStart = await readyWorkerPort.start();
assert.equal(readyWorkerStart.runtimeExecutionReady, true);
assert.equal(readyWorkerStart.status, "browser_python_wasm_runtime_ready");
const readyWorkerPlan = await readyWorkerPort.runTransactionPlan();
assert.equal(readyWorkerPlan.status, "runtime_adapter_transaction_plan_executed");
assert.equal(readyWorkerPlan.runtimeExecutionReady, true);
assert.equal(readyWorkerPlan.promotionAllowed, false);
assert.equal(validateToolDbTranscript(readyWorkerPort.exportTranscript()).ready, true);
await readyWorkerPort.close();
const transcript = [
{ direction: "read", line: "v2.1" },
{ direction: "write", line: "g" },