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): resolve TypeScript compilation errors in hooks timeout runner

naveenprasaath23af44369c25a9
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts
index a895ff12..dfa8077e 100644
--- a/laya-ts/src/agent.ts
+++ b/laya-ts/src/agent.ts
@@ -516,6 +516,8 @@ export class Agent extends HookRegistry {
? [...this.hooks, ...normaliseHooks(opts.hooks, opts.onPredictStart, opts.onPredictEnd)]
: composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);
const raiseErrors = opts.hooksRaise ?? this.hooksRaise;
+ const hooksTimeout = opts.hooksTimeout ?? this.hooksTimeout;
+ const timeoutMs = typeof hooksTimeout === "number" ? hooksTimeout * 1000 : null;
const ctx = new PredictContext({
states,
questions: questions as Record<string, unknown>,
@@ -524,7 +526,7 @@ export class Agent extends HookRegistry {
headMaxLen: opts.headMaxLen ?? null,
});
try {
- await dispatchAsync(active, "onPredictStart", ctx, { raiseErrors, timeoutMs: this.hooksTimeout !== null ? this.hooksTimeout * 1000 : null });
+ await dispatchAsync(active, "onPredictStart", ctx, { raiseErrors, timeoutMs });
if (ctx.results === null) {
if (!Array.isArray(ctx.states)) {
throw new TypeError(
@@ -575,7 +577,7 @@ export class Agent extends HookRegistry {
}
}
try {
- await dispatchAsync(active, "onPredictEnd", ctx, { raiseErrors, timeoutMs: this.hooksTimeout !== null ? this.hooksTimeout * 1000 : null });
+ await dispatchAsync(active, "onPredictEnd", ctx, { raiseErrors, timeoutMs });
} catch (hookErr) {
// End hooks run on the failure path too; do not let one mask the real error.
if (ctx.error === null) throw hookErr;
diff --git a/laya-ts/src/hooks.ts b/laya-ts/src/hooks.ts
index de475e2a..412c9f78 100644
--- a/laya-ts/src/hooks.ts
+++ b/laya-ts/src/hooks.ts
@@ -335,22 +335,28 @@ function reportHookFailure(hook: Hook, event: HookEvent, err: unknown): void {
*/
/** Run a promise with a timeout, returning void if it completes or throwing on timeout. */
-async function runWithTimeout<T>(
- promise: Promise<T>,
+async function runWithTimeout(
+ promise: unknown,
timeoutMs: number | null,
operationName: string,
-): Promise<T> {
- if (timeoutMs === null || timeoutMs === undefined) return promise;
+): Promise<void> {
+ if (timeoutMs === null || timeoutMs === undefined) {
+ await promise;
+ return;
+ }
const abortController = new AbortController();
const timeoutId = setTimeout(() => abortController.abort(), timeoutMs);
try {
- return await Promise.race([promise, new Promise<never>((_, reject) => {
- abortController.signal.addEventListener("abort", () => {
- reject(new Error(`hook ${operationName} timed out after ${timeoutMs}ms")));
- });
- })]);
+ await Promise.race([
+ Promise.resolve(promise),
+ new Promise<never>((_, reject) => {
+ abortController.signal.addEventListener("abort", () => {
+ reject(new Error(`hook ${operationName} timed out after ${timeoutMs}ms`));
+ });
+ }),
+ ]);
} finally {
clearTimeout(timeoutId);
}
@@ -360,10 +366,10 @@ export function dispatch(
hooks: Hook[],
event: HookEvent,
ctx: PredictContext,
- opts: { raiseErrors?: boolean; timeoutMs?: number | null } = {},
+ opts: { raiseErrors?: boolean; timeoutMs?: number | null; hook?: Hook } = {},
): void {
const timeoutMs = opts.timeoutMs ?? null;
- const operationName = `${(opts.hook?.constructor?.name ?? "hook")}.`;
+ const operationName = `${((opts.hook as object | undefined)?.constructor?.name ?? "hook")}.`;
const raiseErrors = opts.raiseErrors ?? true;
for (const hook of hooks) {
const method = hook?.[event];
@@ -389,7 +395,7 @@ export async function dispatchAsync(
opts: { raiseErrors?: boolean; timeoutMs?: number | null; hook?: Hook } = {},
): Promise<void> {
const timeoutMs = opts.timeoutMs ?? null;
- const operationName = `${(opts.hook?.constructor?.name ?? "hook")}.`;
+ const operationName = `${((opts.hook as object | undefined)?.constructor?.name ?? "hook")}.`;
const raiseErrors = opts.raiseErrors ?? true;
for (const hook of hooks) {
const method = hook?.[event];