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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
feat(ts): retrying loads with abort and progress
nnlgsakib060fbd10f024
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.tsindex f5bc8e69..069182ec 100644--- a/laya-ts/src/agent.ts+++ b/laya-ts/src/agent.ts@@ -556,6 +556,8 @@ export class Agent extends HookRegistry {* or executed. A missing artifact or digest mismatch throws and loading is refused.*/expectedSha256?: Record<string, string>;+ signal?: AbortSignal | null;+ onProgress?: ((done: number, total: number, file: string) => void) | null;},): Promise<Agent> {const sub = opts?.subfolder ?? null;@@ -572,6 +574,8 @@ export class Agent extends HookRegistry {subfolder: sub,revision: opts?.revision,expectedSha256: opts?.expectedSha256,+ signal: opts?.signal,+ onProgress: opts?.onProgress,});cfg = bundle.cfg;tokenizerJson = bundle.tokenizerJson;@@ -589,6 +593,8 @@ export class Agent extends HookRegistry {token: opts?.token,revision: opts?.revision,expectedSha256: opts?.expectedSha256,+ signal: opts?.signal,+ onProgress: opts?.onProgress,});cfg = bundle.cfg;tokenizerJson = bundle.tokenizerJson;diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.tsindex d423ae85..9f15c622 100644--- a/laya-ts/src/providers.ts+++ b/laya-ts/src/providers.ts@@ -188,10 +188,43 @@ 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 TOKENIZER_CANDIDATES = ["tokenizer.json", "tokenizer/tokenizer.json"];++function sleep(ms: number): Promise<void> {+ return new Promise((r) => setTimeout(r, ms));+}++/** fetch with 2 retries on network errors + 429/5xx; 404s fail fast. */+async function fetchWithRetry(url: string, init?: RequestInit, retries = 2): Promise<Response> {+ let last: unknown = null;+ for (let attempt = 0; attempt <= retries; attempt++) {+ if (init?.signal?.aborted) throw new DOMException("aborted", "AbortError");+ try {+ const res = await fetch(url, init);+ if (res.ok) return res;+ if ((res.status === 429 || res.status >= 500) && attempt < retries) {+ await sleep(100 * (attempt + 1));+ continue;+ }+ return res;+ } catch (e) {+ last = e;+ if ((e as any)?.name === "AbortError") throw e;+ if (attempt < retries) {+ await sleep(100 * (attempt + 1));+ continue;+ }+ throw e;+ }+ }+ throw last instanceof Error ? last : new Error(`fetch failed for ${url}`);+}+/** Online-first fetch: try network, cache on success, fall back to CacheStorage. */async function fetchArrayBuffer(url: string,- onHeaders?: (response: Response) => void,+ opts?: { signal?: AbortSignal | null; onHeaders?: (response: Response) => void },): Promise<ArrayBuffer> {const g = globalThis as unknown as { caches?: any };let cache: any = null;@@ -199,7 +232,7 @@ async function fetchArrayBuffer(try {if (g.caches && typeof g.caches.open === "function") {try {- cache = await g.caches.open("laya-ts");+ cache = await g.caches.open(CACHE_KEY);try {hit = await cache.match(url);} catch {@@ -214,8 +247,8 @@ async function fetchArrayBuffer(}if (cache) {try {- const res = await fetch(url);- onHeaders?.(res);+ const res = await fetchWithRetry(url, { signal: opts?.signal ?? undefined });+ opts?.onHeaders?.(res);if (res.ok) {try {await cache.put(url, res.clone());@@ -243,8 +276,8 @@ async function fetchArrayBuffer(}throw new Error(`fetch failed for ${url}`);}- const res = await fetch(url);- onHeaders?.(res);+ const res = await fetchWithRetry(url, { signal: opts?.signal ?? undefined });+ opts?.onHeaders?.(res);if (!res.ok) throw new Error(`fetch failed for ${url}: ${res.status}`);return await res.arrayBuffer();}@@ -265,6 +298,8 @@ export async function loadNodeBundle(token?: string | null;revision?: string | null;expectedSha256?: Record<string, string>;+ signal?: AbortSignal | null;+ onProgress?: ((done: number, total: number, file: string) => void) | null;},): Promise<NodeBundle> {const fs: typeof import("node:fs/promises") = await import("node:fs/promises");@@ -294,12 +329,20 @@ export async function loadNodeBundle(await fs.mkdir(cache, { recursive: true });const token =opts?.token ?? (typeof process !== "undefined" ? (process as any).env?.["HF_TOKEN"] : undefined);- for (const f of ["rl_agent_config.json", "tokenizer.json", "tokenizer/tokenizer.json", "encoder.onnx", "head.onnx"]) {+ const files = ["rl_agent_config.json", "tokenizer.json", "tokenizer/tokenizer.json", "encoder.onnx", "head.onnx"];+ let done = 0;+ for (const f of files) {try {await fs.stat(path.join(cache, f));} catch {const url = `https://huggingface.co/${modelDirOrRepo}/resolve/${revision ?? "main"}/${sub ? sub + "/" : ""}${f}`;- const res = await fetch(url, token ? { headers: { Authorization: `Bearer ${token}` } } : undefined);+ const res = await fetchWithRetry(+ url,+ {+ ...(token ? { headers: { Authorization: `Bearer ${token}` } } : {}),+ signal: opts?.signal ?? undefined,+ },+ );const commit = res.headers?.get?.("x-repo-commit");if (commit) resolvedRevision = commit;if (!res.ok) {@@ -318,6 +361,8 @@ export async function loadNodeBundle(await fs.writeFile(tmp, new Uint8Array(await res.arrayBuffer()));await fs.rename(tmp, target);}+ done++;+ opts?.onProgress?.(done, files.length, f);}dir = cache;}@@ -350,7 +395,7 @@ export async function loadNodeBundle();}let tokenizerJson: unknown | null = null;- for (const candidate of ["tokenizer.json", "tokenizer/tokenizer.json"]) {+ for (const candidate of TOKENIZER_CANDIDATES) {try {tokenizerJson = JSON.parse(await fs.readFile(path.join(dir, candidate), "utf8"));break;@@ -383,15 +428,20 @@ export async function loadWebBundle(subfolder?: string | null;revision?: string | null;expectedSha256?: Record<string, string>;+ signal?: AbortSignal | null;+ onProgress?: ((done: number, total: number, file: string) => void) | null;},): Promise<WebBundle> {const revision = resolveRevision(repoOrUrl, opts?.revision);const base = baseUrlFor(repoOrUrl, opts?.subfolder ?? null, revision);let reportedRevision = revision;const fetchVerifiedJson = async (rel: string): Promise<unknown> => {- const buf = await fetchArrayBuffer(`${base}/${rel}`, (response) => {- const commit = response.headers?.get?.("x-repo-commit");- if (commit) reportedRevision = commit;+ const buf = await fetchArrayBuffer(`${base}/${rel}`, {+ signal: opts?.signal ?? undefined,+ onHeaders: (response) => {+ const commit = response.headers?.get?.("x-repo-commit");+ if (commit) reportedRevision = commit;+ },});if (opts?.expectedSha256) await expectDigest(rel, buf, opts.expectedSha256);return JSON.parse(new TextDecoder().decode(buf));@@ -403,8 +453,9 @@ export async function loadWebBundle(if (e instanceof Error && e.message.startsWith("laya-ts: SHA-256 mismatch")) 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");let tokenizerJson: unknown | null = null;- for (const candidate of ["tokenizer.json", "tokenizer/tokenizer.json"]) {+ for (const candidate of TOKENIZER_CANDIDATES) {try {tokenizerJson = await fetchVerifiedJson(candidate);break;@@ -413,6 +464,7 @@ export async function loadWebBundle(// Try the next supported Hugging Face layout.}}+ opts?.onProgress?.(2, 2, "tokenizer.json");return { dir: base, cfg, tokenizerJson, revision: reportedRevision };}diff --git a/laya-ts/tests/load-robustness.test.ts b/laya-ts/tests/load-robustness.test.tsnew file mode 100644index 00000000..7526b2b0--- /dev/null+++ b/laya-ts/tests/load-robustness.test.ts@@ -0,0 +1,41 @@+import { describe, expect, it, vi, afterEach } from "vitest";+import { loadWebBundle } from "../src/providers.js";++afterEach(() => vi.unstubAllGlobals());++function jsonResponse(obj: unknown, ok = true, status = 200) {+ const text = JSON.stringify(obj);+ const buf = new TextEncoder().encode(text).buffer as ArrayBuffer;+ return {+ ok,+ status,+ clone: () => jsonResponse(obj, ok, status),+ arrayBuffer: async () => buf.slice(0),+ };+}++describe("load robustness", () => {+ it("retries transient 503 then succeeds", async () => {+ const fetchMock = vi.fn()+ .mockResolvedValueOnce({ ok: false, status: 503, arrayBuffer: async () => new ArrayBuffer(0), clone: () => ({}) })+ .mockResolvedValueOnce(jsonResponse({ max_len: 512 }))+ .mockResolvedValueOnce(jsonResponse({ model: { vocab: {}, merges: [] }, added_tokens: [] }));+ vi.stubGlobal("fetch", fetchMock);+ const progress: string[] = [];+ const bundle = await loadWebBundle("https://example.com/m", {+ onProgress: (_d: number, _t: number, f: string) => progress.push(f),+ } as any);+ expect(bundle.cfg).toEqual({ max_len: 512 });+ expect(fetchMock.mock.calls.length).toBeGreaterThanOrEqual(3);+ expect(progress.length).toBeGreaterThan(0);+ });+ it("aborted signal throws without retry", 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({});+ }));+ await expect(loadWebBundle("https://example.com/m", { signal: c.signal } as any)).rejects.toThrow();+ });+});