import type { CheckpointId, PrefixHash, ProfileId, ProposalId, RequestId, SessionEpoch, } from './ids.ts'; export interface EngineIdentity { modelHash: string; tokenizerHash: string; templateHash: string; numericalProfile: ProfileId; stateSchema: number; } export interface PrefixState { processedLength: number; prefixHash: PrefixHash; pendingToken?: number; samplerPhase: 'beforeDraw' | 'afterDraw'; } export interface CheckpointRef { id: CheckpointId; identity: EngineIdentity; prefix: PrefixState; canResampleNext: boolean; } export interface GenerateRequest { epoch: SessionEpoch; requestId: RequestId; samplerProfile: ProfileId; seed: bigint; maxNewTokens: number; stopPolicy: string; } export type StopReason = 'eos' | 'length' | 'cancelled' | 'tool' | 'grammar' | 'fault' | 'window'; export type EngineEvent = | { type: 'preparation'; requestId: RequestId; stage: PrepareStage; completed: number; total: number } | { type: 'prefill'; requestId: RequestId; processed: number; total: number } | { type: 'token'; requestId: RequestId; tokenId: number; index: number } | { type: 'channel'; requestId: RequestId; channel: 'reasoning' | 'answer'; text: string; tokenStart: number; tokenEnd: number; } | { type: 'toolProposal'; requestId: RequestId; proposal: ToolProposal } | { type: 'completion'; requestId: RequestId; reason: StopReason; processed: number; emitted: number } | { type: 'cancelled'; requestId: RequestId; boundary: PrefixState } | { type: 'error'; requestId: RequestId | null; recoverable: boolean; code: string; message: string } | { type: 'checkpoint'; requestId: RequestId; checkpoint: CheckpointRef }; export type PrepareStage = | 'manifest' | 'download' | 'verify' | 'upload' | 'compile' | 'calibrate' | 'ready'; export interface ToolCall { name: string; arguments: Record; } export interface ToolProposal { proposalId: ProposalId; call: ToolCall; rawText: string; complete: boolean; parseError?: string; } export interface InferenceEngine { load(manifestUrl: string): Promise; prefill(tokens: Uint32Array): Promise; generate(req: GenerateRequest): AsyncIterable; checkpoint(): Promise; restore(ref: CheckpointRef): Promise; cancel(requestId: number): Promise; dispose(): Promise; } export interface DeviceCapabilities { webgpu: boolean; secureContext: boolean; crossOriginIsolated: boolean; adapter: AdapterReport | null; features: string[]; wgslLanguageFeatures: string[]; limits: Record; timestampQuery: boolean; subgroups: boolean; subgroupSizeControl: boolean; immediateAddressSpace: boolean; bufferView: boolean; subgroupUniformity: boolean; subgroupId: boolean; shaderF16: boolean; opfs: boolean; indexedDb: boolean; sharedArrayBuffer: boolean; webLocks: boolean; broadcastChannel: boolean; serviceWorker: boolean; screenWakeLock: boolean; deviceMemoryGiB: number | null; storageQuotaBytes: number | null; storageUsageBytes: number | null; } export interface AdapterReport { vendor: string; architecture: string; device: string; description: string; subgroupMinSize: number; subgroupMaxSize: number; isFallbackAdapter: boolean; } export interface CapabilityFailure { missing: string[]; detail: string; }