Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): review follow-ups on load retry PR

nnlgsakib6e8007f47524
diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.ts
index 9f15c622..df0df997 100644
--- a/laya-ts/src/providers.ts
+++ b/laya-ts/src/providers.ts
@@ -168,6 +168,8 @@ export interface ProviderOptions {
numThreads?: number;
/** Opt-in {artifact name: SHA-256 hexdigest} check for fetched ONNX files (web). */
expectedSha256?: Record<string, string>;
+ signal?: AbortSignal | null;
+ onProgress?: ((done: number, total: number, file: string) => void) | null;
}
function applyNumThreads(ort: any, numThreads?: number): void {
@@ -188,7 +190,7 @@ function isOomError(e: unknown): boolean {
return m.includes("memory") || m.includes("cuda") || m.includes("out of memory") || m.includes("oom");
}
-const CACHE_KEY = "laya-ts-v1";
+const CACHE_KEY = "laya-ts";
const TOKENIZER_CANDIDATES = ["tokenizer.json", "tokenizer/tokenizer.json"];
function sleep(ms: number): Promise<void> {
@@ -451,6 +453,7 @@ export async function loadWebBundle(
cfg = await fetchVerifiedJson("rl_agent_config.json");
} catch (e) {
if (e instanceof Error && e.message.startsWith("laya-ts: SHA-256 mismatch")) throw e;
+ if ((e as Error)?.name === "AbortError") throw e;
throw new Error(`Incompatible model: ${JSON.stringify(repoOrUrl)} does not contain 'rl_agent_config.json'.`);
}
opts?.onProgress?.(1, 2, "rl_agent_config.json");
@@ -572,16 +575,20 @@ export async function createWebProvider(
const headUrl = `${base}/head.onnx`;
let encBuf: ArrayBuffer;
try {
- encBuf = await fetchArrayBuffer(encUrl);
- } catch {
+ encBuf = await fetchArrayBuffer(encUrl, { signal: opts?.signal ?? undefined });
+ } catch (e) {
+ if ((e as Error)?.name === "AbortError") throw e;
throw new Error(`Incompatible model: 'encoder.onnx' not found (expected ${encUrl}).`);
}
+ opts?.onProgress?.(1, 2, "encoder.onnx");
let headBuf: ArrayBuffer;
try {
- headBuf = await fetchArrayBuffer(headUrl);
- } catch {
+ headBuf = await fetchArrayBuffer(headUrl, { signal: opts?.signal ?? undefined });
+ } catch (e) {
+ if ((e as Error)?.name === "AbortError") throw e;
throw new Error(`Incompatible model: 'head.onnx' not found (expected ${headUrl}).`);
}
+ opts?.onProgress?.(2, 2, "head.onnx");
// Verify before the bytes reach the runtime: a tampered ONNX never becomes a session.
if (opts?.expectedSha256) {
await expectDigest("encoder.onnx", encBuf, opts.expectedSha256);
diff --git a/laya-ts/tests/load-robustness.test.ts b/laya-ts/tests/load-robustness.test.ts
index 7526b2b0..fc2716b1 100644
--- a/laya-ts/tests/load-robustness.test.ts
+++ b/laya-ts/tests/load-robustness.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it, vi, afterEach } from "vitest";
-import { loadWebBundle } from "../src/providers.js";
+import { createWebProvider, loadWebBundle } from "../src/providers.js";
afterEach(() => vi.unstubAllGlobals());
@@ -39,3 +39,52 @@ describe("load robustness", () => {
await expect(loadWebBundle("https://example.com/m", { signal: c.signal } as any)).rejects.toThrow();
});
});
+
+describe("web provider downloads", () => {
+ function onnxBytes() {
+ return {
+ ok: true,
+ status: 200,
+ clone: () => ({}),
+ arrayBuffer: async () => new Uint8Array([0, 1, 2, 3]).buffer as ArrayBuffer,
+ };
+ }
+ it("passes signal to the model fetches and reports per-file progress", async () => {
+ const seen: Array<{ url: string; signal: unknown }> = [];
+ vi.stubGlobal("fetch", vi.fn(async (url: string, init: any) => {
+ seen.push({ url, signal: init?.signal ?? null });
+ return onnxBytes();
+ }));
+ const progress: Array<[number, number, string]> = [];
+ const c = new AbortController();
+ // Garbage bytes: the real runtime rejects at session creation, after the
+ // fetches already carried the signal and the progress fired.
+ await expect(
+ createWebProvider("https://example.com/m", {
+ signal: c.signal,
+ onProgress: (d: number, t: number, f: string) => progress.push([d, t, f]),
+ } as any),
+ ).rejects.toThrow();
+ const encFetch = seen.find((s) => s.url.endsWith("encoder.onnx"));
+ const headFetch = seen.find((s) => s.url.endsWith("head.onnx"));
+ expect(encFetch?.signal).toBe(c.signal);
+ expect(headFetch?.signal).toBe(c.signal);
+ expect(progress).toEqual([
+ [1, 2, "encoder.onnx"],
+ [2, 2, "head.onnx"],
+ ]);
+ });
+});
+
+describe("load abort semantics", () => {
+ it("aborted signal surfaces AbortError, not Incompatible model", async () => {
+ const c = new AbortController();
+ c.abort();
+ vi.stubGlobal("fetch", vi.fn(async (_u: any, init: any) => {
+ if (init?.signal?.aborted ?? c.signal.aborted) throw new DOMException("aborted", "AbortError");
+ return jsonResponse({});
+ }));
+ const err = await loadWebBundle("https://example.com/m", { signal: c.signal } as any).catch((e) => e);
+ expect(err?.name).toBe("AbortError");
+ });
+});