sandai-react
Version:
React components and utilities for the Sandai 3D AI Characters.
117 lines • 4.99 kB
TypeScript
import { StoppingCriteriaList } from "@huggingface/transformers";
import { DTYPE } from "./types/qantization-types";
import { Message } from "./types/types";
/**
* Web Worker wrapper for a local (or remote) transformers.js text-generation pipeline.
*
* Responsibilities:
* - Initialize the model pipeline and report progress via `postMessage({ type: "progress" })`.
* - Generate text with optional streaming:
* - Emits: `stream-start`, `stream-delta`, `stream-token` (optional), `response`, `stream-end`.
* - Cooperative cancellation:
* - Accepts: `{ type: "cancel", payload: { requestId } }`
* - Emits: `cancel-ack` and a terminal `stream-end` with an error.
*
* Message contracts (to the main thread):
* - Progress: `{ type: "progress", data: any }`
* - Stream start: `{ type: "stream-start", data: { requestId: string } }`
* - Stream delta: `{ type: "stream-delta", data: { requestId: string, delta: string } }`
* - Stream token: `{ type: "stream-token", data: { requestId: string, tokens: string[] } }`
* - Final text: `{ type: "response", data: { requestId: string, text: string } }`
* - Stream end: `{ type: "stream-end", data: { requestId: string, error?: string } }`
* - Errors: `{ type: "error", data: string }`
*/
export declare class LlmWorker {
/** Whether this code runs inside a Worker context */
private static isWorker;
/** Underlying transformers pipeline instance */
private generator;
/** Model instance should the auto pipeline have been chosen for the model */
private autoModel;
private pipelineType;
/** Default local model directory or repo id */
private static llmModelPath;
/** Set of requestIds that have been cancelled (cooperative cancel) */
private cancelledIds;
/** Per-request interrupt handles (hard-cancel via stopping criteria) */
private interrupts;
/**
* Construct and bind the message handler if in a Worker context.
*/
constructor();
/**
* Central message router for the worker.
*
* Accepts:
* - `initialize` → loads the model and posts progress events
* - `generate` → runs text generation (with streaming callbacks)
* - `cancel` → marks a requestId as cancelled and emits terminal events
*
* @param event MessageEvent carrying `{ type, payload }`.
*/
private handleMessage;
/**
* Initialize the transformers.js pipeline.
*
* Side effects:
* - Enables both remote and local model loading.
* - Emits `{ type: "progress", data }` messages while loading.
*
* @param modelPath Repo ID or local path to the model (defaults to `"gemma3-1b"`).
* @param dtype Optional quantization/precision hint (e.g. `"int8"`, `"q4"`).
* @returns A structured response `{ type, data }` indicating success or error.
*/
initialize(modelPath?: string, dtype?: DTYPE, pipelineType?: "text" | "auto"): Promise<{
type: "response" | "error";
data: string;
}>;
/**
* Normalize a raw pipeline output into a plain string.
*
* Accepts several shapes:
* - string
* - array of strings
* - array of objects with a string `content` property
*
* @param llmRes Unknown raw output from pipeline call.
* @returns A string best-effort extraction of the model’s text.
*/
private validateResponse;
generateResponse(messages: Message[], maxTokens: number, opts?: {
stream?: boolean;
requestId?: string;
emitTokens?: boolean;
stopping_criteria?: StoppingCriteriaList;
}): Promise<string>;
/**
* Takes in messages and constructs a prompt based on the chosen model dynamically.
*
* @param messages Chat messages; rendered to a single text prompt.
* @returns
*/
private _getPromptForMessages;
private supportsMessageFormat;
/**
* Generate a completion for a list of chat messages.
*
* Streaming:
* - When `opts.stream` is true, chunks are emitted via `stream-delta`.
* - Token IDs (as strings) are emitted via `stream-token` if `opts.emitTokens` is true.
* - Cooperative cancellation: if `cancelledIds` contains `opts.requestId`, no chunks are emitted.
*
* @param messages Chat messages; rendered to a single text prompt.
* @param maxTokens Maximum new tokens to generate.
* @param opts Streaming and correlation options:
* - `stream`: whether to emit deltas (default true)
* - `requestId`: correlation id for emitted events
* - `emitTokens`: whether to also emit token id arrays
* @returns The final generated text (even if streaming was used).
*/
generateTextResponse(messages: Message[], maxTokens: number, opts?: {
stream?: boolean;
requestId?: string;
emitTokens?: boolean;
stopping_criteria?: StoppingCriteriaList;
}): Promise<string>;
}
//# sourceMappingURL=llm-worker.d.ts.map