接续L4-TOOL-DB浏览器Provider协议
结论:浏览器 Worker 已支持可插拔 Python runtime provider 的 line I/O,缺真实 Python/WASM runtime 时仍保持阻塞不解锁。
This commit is contained in:
@@ -15,12 +15,21 @@ function pythonRuntimeAvailable() {
|
||||
return typeof self.loadPyodide === "function" || Boolean(self.pyodide?.runPythonAsync);
|
||||
}
|
||||
|
||||
function getRuntimeProvider() {
|
||||
return self.linuxCncToolDbPythonRuntimeProvider ?? null;
|
||||
}
|
||||
|
||||
self.addEventListener("message", async (event) => {
|
||||
const message = event.data ?? {};
|
||||
const { id, type } = message;
|
||||
const { id, type, line } = message;
|
||||
try {
|
||||
if (type === "start") {
|
||||
const ready = pythonRuntimeAvailable();
|
||||
const provider = getRuntimeProvider();
|
||||
const providerReady = Boolean(provider?.start && provider?.writeLine && provider?.readLine);
|
||||
const ready = providerReady || pythonRuntimeAvailable();
|
||||
if (providerReady) {
|
||||
await provider.start(message);
|
||||
}
|
||||
postReply(id, type, {
|
||||
runtimeMode: "browser-python-wasm-worker",
|
||||
runtimeExecutionReady: ready,
|
||||
@@ -32,6 +41,25 @@ self.addEventListener("message", async (event) => {
|
||||
});
|
||||
return;
|
||||
}
|
||||
const provider = getRuntimeProvider();
|
||||
if (type === "writeLine" && provider?.writeLine) {
|
||||
await provider.writeLine(line);
|
||||
postReply(id, type, { executionEnabled: false, promotionAllowed: false });
|
||||
return;
|
||||
}
|
||||
if (type === "readLine" && provider?.readLine) {
|
||||
postReply(id, type, {
|
||||
line: await provider.readLine(),
|
||||
executionEnabled: false,
|
||||
promotionAllowed: false,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (type === "close" && provider?.close) {
|
||||
await provider.close();
|
||||
postReply(id, type, { executionEnabled: false, promotionAllowed: false });
|
||||
return;
|
||||
}
|
||||
throw new Error("blocked_browser_python_wasm_runtime_missing");
|
||||
} catch (error) {
|
||||
postError(id, type, error);
|
||||
|
||||
Reference in New Issue
Block a user