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.

Merge pull request #723 from Swatantra-66/feat/ts-router-sha256-digests

Nandakishore3e0dee75de2
diff --cc laya-ts/src/index.ts
index 78dbff0e,b07f39e9..a79e5443
--- a/laya-ts/src/index.ts
+++ b/laya-ts/src/index.ts
@@@ -15,8 -15,8 +15,8 @@@ export type
} from "./agent.js";
export { createNodeProvider, createWebProvider, feed, feedHead, loadNodeBundle, loadWebBundle, PINNED_REVISIONS, resolveRevision } from "./providers.js";
export type { Batch, SessionProvider, ProviderOptions, NodeBundle, WebBundle } from "./providers.js";
- export { Router, normaliseName, DEFAULT_MODELS } from "./router.js";
- export type { RoutedResult, RouteDecision, ModelName, ModelSpec, BatchRequest } from "./router.js";
+ export { Router, normaliseName, DEFAULT_MODELS, digestsFromEnv, _digestsFromEnv } from "./router.js";
-export type { RoutedResult, RouteDecision, ModelName, ModelSpec, RouterOptions, RouteOptions } from "./router.js";
++export type { RoutedResult, RouteDecision, ModelName, ModelSpec, BatchRequest, RouterOptions, RouteOptions } from "./router.js";
export { shortlistChoice, predictShortlist, embedFnFromAgent, DEFAULT_SHORTLIST_K } from "./shortlist.js";
export type { EmbedFn, ShortlistMeta } from "./shortlist.js";
export { analyse, isEnglish, guessLatinLanguage, detectScript } from "./lang.js";
diff --cc laya-ts/src/router.ts
index 615436de,121f22dc..8e93fe2a
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@@ -149,30 -152,47 +153,71 @@@ export interface RouteOptions
hooksRaise?: boolean;
}
+/** One item of a Router.predictBatch/routeBatch batch: state, questions, route overrides. */
+export interface BatchRequest {
+ state: unknown;
+ questions: Record<string, QuestionDef>;
+ model?: string | null;
+ task?: string | null;
+ lang?: string | null;
+ langGuess?: LangGuess;
+ lang_guess?: LangGuess;
+}
+
+function questionSchema(questions: Record<string, unknown>): string {
+ // Python groups on json.dumps(sort_keys=False, default=str): insertion order is significant
+ // at every nesting level because options are positional in the rendered sequence, so two
+ // equal schemas with different key orders must not share a forward pass. JSON.stringify
+ // preserves insertion order for string keys; the replacer stands in for default=str on
+ // the few value types JSON cannot represent.
+ return JSON.stringify(questions, (_key, value) =>
+ typeof value === "function" || typeof value === "symbol" || typeof value === "bigint"
+ ? String(value)
+ : value,
+ );
+}
+
+ /** Turn `LAYA_SHA256_DIGESTS` into per-checkpoint digest maps when it names models. */
+ export function digestsFromEnv(
+ models: Record<string, unknown>,
+ ): Record<string, Record<string, string> | null> {
+ if (typeof process === "undefined" || !process.env) return {};
+ const raw = (process.env["LAYA_SHA256_DIGESTS"] ?? "").trim();
+ if (!raw) return {};
+ let data: unknown;
+ try {
+ data = JSON.parse(raw);
+ } catch {
+ return {};
+ }
+ if (typeof data !== "object" || data === null || Array.isArray(data)) return {};
+ const entries = Object.entries(data);
+ if (entries.length === 0) return {};
+ const values = entries.map(([, v]) => v);
+ if (values.every((v) => typeof v === "string")) {
+ return {}; // flat: providers already applies it
+ }
+ if (!values.every((v) => typeof v === "object" && v !== null && !Array.isArray(v))) {
+ throw new Error(
+ `LAYA_SHA256_DIGESTS must be either {artifact: digest} for every checkpoint or {model: {artifact: digest}} per checkpoint; ${JSON.stringify(Object.keys(data).sort())} mixes the two or holds a value that is neither`,
+ );
+ }
+ const perModel: Record<string, Record<string, string> | null> = {};
+ for (const [k, v] of entries) {
+ perModel[normaliseName(k)] = v as Record<string, string>;
+ }
+ for (const name of Object.keys(models)) {
+ const norm = normaliseName(name);
+ if (!(norm in perModel)) {
+ perModel[norm] = {};
+ }
+ }
+ return perModel;
+ }
+
+ export const _digestsFromEnv = digestsFromEnv;
+ export const _digests_from_env = digestsFromEnv;
+
function toSpec(spec: string | ModelSpec | [string, string | null]): ModelSpec {
if (typeof spec === "string") return { repo: spec, subfolder: null };
if (Array.isArray(spec)) {