import type { BackendObservability, GenerateRequestId, GenerateResponse, NativeRuntimeConfig, SamplingRuntimeOverride, RequestObservabilityMetrics, ChatMessage } from '../engine/inference-types.js'; import type { ClassifiedAsset, ModelDetectionResult, PairingPlan, RuntimePairingErrorCode } from '../models/types.js'; import { QueryError, type AssetRecord, type ModelInfo, type ObservabilityEvent, type ObservabilityEventType, type ObservabilitySnapshot, type QueryErrorCode, type RegistryManifest } from '../models/types.js'; import type { ChatBoundaryInfo } from '../engine/chat-boundary-sanitizer.js'; import { EngineModule } from './engine-module.js'; import type { SharedTokenRingDescriptor } from '../runtime/shared-token-ring.js'; export declare const COMPLETED_REQUEST_STATUS_PENDING = 0; export declare const COMPLETED_REQUEST_STATUS_COMPLETED = 1; interface WasmTextRequestOptions { grammar?: string; stop?: readonly string[]; sampling?: SamplingRuntimeOverride; emitTokens?: boolean; } export type WasmSchedulerProgressResult = { stepResult: number; completedResponseCount: number; }; export type BrowserCacheLayout = 'single-file' | 'split-gguf'; interface PairingValidationResponse { ok: boolean; plan?: PairingPlan; error?: { code: RuntimePairingErrorCode | string; message: string; }; } export interface RustLifecycleError { readonly code: QueryErrorCode | string; readonly message: string; readonly status?: number; readonly retryAfterMs?: number; } interface RustLifecycleResponse { ok: boolean; value?: T; error?: RustLifecycleError; } type RustLifecycleHandle = number; type RustLifecycleBackendPreference = 'auto' | 'cpu' | 'webgpu'; interface RustLifecycleCreateValue { handle: RustLifecycleHandle; manifest: RegistryManifest; snapshot: ObservabilitySnapshot; } export interface RustLifecycleLoadSource { modelId: string; } export interface RustLifecycleInstallSource { assets: AssetRecord[]; classified: ClassifiedAsset[]; } export interface RustRemoteMetadata { readonly url: string; readonly name: string; readonly bytes: number; readonly etag?: string; readonly lastModified?: string; } export interface RustRemoteCacheCandidate { readonly candidateId: string; readonly assetIds: readonly string[]; readonly metadata: RustRemoteMetadata; } export type RustRemoteAction = { readonly kind: 'fetch_metadata'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly url: string; } | { readonly kind: 'wait'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly delayMs: number; } | { readonly kind: 'validate_cache'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly candidate: RustRemoteCacheCandidate; } | { readonly kind: 'download'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly metadata: RustRemoteMetadata; } | { readonly kind: 'cleanup'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly assetIds: readonly string[]; }; export type RustRemoteFailure = { readonly phase: 'metadata' | 'download' | 'cache_validation' | 'cleanup'; readonly kind: 'transport' | 'http' | 'invalid_response' | 'integrity' | 'storage'; readonly status?: number; readonly retryAfter?: string; readonly reason: string; }; export type RustRemoteEvent = { readonly kind: 'metadata_succeeded'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly headers: { readonly contentLength?: number; readonly linkedSize?: number; readonly etag?: string; readonly linkedEtag?: string; readonly lastModified?: string; }; } | { readonly kind: 'operation_failed'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly failure: RustRemoteFailure; readonly createdAssetIds: readonly string[]; } | { readonly kind: 'wait_completed'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; } | { readonly kind: 'cache_validated'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly assetIds: readonly string[]; } | { readonly kind: 'download_succeeded'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; readonly assetIds: readonly string[]; readonly createdAssetIds: readonly string[]; } | { readonly kind: 'cleanup_succeeded'; readonly acquisitionId: string; readonly memberId: number; readonly attempt: number; }; export type RustRemoteCommand = { readonly command: 'begin'; readonly urls: readonly string[]; } | { readonly command: 'advance'; readonly event: RustRemoteEvent; readonly assets?: readonly AssetRecord[]; readonly classified?: readonly ClassifiedAsset[]; } | { readonly command: 'cancel'; readonly acquisitionId: string; }; export type RustRemoteCommandValue = { readonly kind: 'action'; readonly action: RustRemoteAction; } | { readonly kind: 'installed'; readonly installed: RustLifecycleInstallValue; } | { readonly kind: 'cancelled'; readonly snapshot: ObservabilitySnapshot; } | { readonly kind: 'failed'; readonly error: RustLifecycleError; }; interface RustLifecycleLoadOptions { backend?: RustLifecycleBackendPreference; runtime?: NativeRuntimeConfig; observability?: 'off' | 'runtime' | 'profile'; } interface RustLifecyclePlannedAsset { assetId: string; kind: AssetRecord['kind']; storagePath: string; mountName: string; bytes: number; } export interface RustLifecyclePrepareLoadValue { loadId: string; model: ModelInfo; runtimeFingerprint: string; runtimeConfig: NativeRuntimeConfig; loadRequired: boolean; assets: RustLifecyclePlannedAsset[]; projector?: RustLifecyclePlannedAsset | null; manifest: RegistryManifest; snapshot: ObservabilitySnapshot; events: ObservabilityEvent[]; } export interface RustLifecycleInstallValue { model: ModelInfo; manifest: RegistryManifest; snapshot: ObservabilitySnapshot; events: ObservabilityEvent[]; } interface RustLifecycleCommitLoad { loadId: string; modelId: string; runtimeFingerprint: string; chatTemplate?: string | null; bosText?: string; eosText?: string; mediaMarker?: string | null; runtime?: unknown; profile?: unknown; } interface RustLifecycleCommitLoadValue { model: ModelInfo; manifest: RegistryManifest; snapshot: ObservabilitySnapshot; events: ObservabilityEvent[]; } interface RustLifecycleRemoveValue { removed: unknown; orphanedAssets: AssetRecord[]; manifest: RegistryManifest; snapshot: ObservabilitySnapshot; events: ObservabilityEvent[]; } export interface GgufSplitStreamCallbacks { readAt(offset: number, target: Uint8Array): number | void; openShard(path: string, index: number, count: number): number | void; writeShard(bytes: Uint8Array): number | void; closeShard(): number | void; } export interface GgufReadAtCallbacks { readAt(offset: number, target: Uint8Array): number | void; } export declare class RustLifecycleBridge { private readonly bridge; private readonly handle; private closed; private constructor(); static create(bridge: WasmBridge, manifest: RegistryManifest): RustLifecycleBridge; list(): ModelInfo[]; current(): ModelInfo | null; manifest(): RegistryManifest; prepareLoad(source: RustLifecycleLoadSource, options: RustLifecycleLoadOptions): RustLifecyclePrepareLoadValue; install(source: RustLifecycleInstallSource): RustLifecycleInstallValue; remoteAcquisition(command: RustRemoteCommand): RustRemoteCommandValue; commitLoad(commit: RustLifecycleCommitLoad): RustLifecycleCommitLoadValue; abortLoad(error: { message?: string; }): ObservabilitySnapshot; remove(modelId: string): RustLifecycleRemoveValue; unload(): ObservabilitySnapshot; snapshot(): ObservabilitySnapshot; drainEvents(): ObservabilityEvent[]; recordEvent(type: ObservabilityEventType, patch: Record): ObservabilitySnapshot; close(): void; } export declare function unwrapLifecycleResponse(response: RustLifecycleResponse, label: string): T; export declare function queryErrorFromLifecycleError(error: RustLifecycleError | undefined, fallbackMessage: string): QueryError; export declare class WasmBridge { readonly module: EngineModule; private _cachedDataView; constructor(module: EngineModule); private ensureHeapView; private byteOffset; private heapIndex; callNumber(ident: string, argTypes?: string[], args?: unknown[]): number; callNumberAsync(ident: string, argTypes?: string[], args?: unknown[]): Promise; loadRuntimeModel(modelPath: string, config?: NativeRuntimeConfig): Promise; readLastEngineError(): string; close(): void; startTextRequest(contextKey: string, promptText: string, maxOutputTokens: number, options?: WasmTextRequestOptions): GenerateRequestId; startMediaRequest(contextKey: string, promptText: string, maxOutputTokens: number, media: Uint8Array[], options?: WasmTextRequestOptions): GenerateRequestId; startChatRequest(contextKey: string, messages: readonly ChatMessage[], maxOutputTokens: number, media?: Uint8Array[], options?: WasmTextRequestOptions): GenerateRequestId; startEmbeddingRequest(contextKey: string, input: string, normalize: boolean): GenerateRequestId; readMediaMarker(): string | null; readNativeChatTemplate(): string | null; getBosText(): string; getEosText(): string; /** * Applies llama.cpp's native chat template (via common_chat_format_single) * to a set of OpenAI-style chat messages and returns the formatted prompt * text. Returns '' when the model has no embedded chat template. */ probeChatTemplateBoundaryInfo(): ChatBoundaryInfo; validatePairing(classified: readonly ClassifiedAsset[]): PairingValidationResponse; cancelQuery(requestId: GenerateRequestId): Promise; getCompletedRequestStatus(requestId: GenerateRequestId): number; consumeCompletedRequest(requestId: GenerateRequestId): boolean; consumeCompletedResponseIfPresent(requestId: GenerateRequestId): boolean; getBackendObservabilityJson(): Promise; rustBrowserEngineAbiVersion(): number; modelServiceCreate(config?: { manifest?: RegistryManifest | null; }): RustLifecycleResponse; modelServiceClose(handle: RustLifecycleHandle): boolean; modelServiceList(handle: RustLifecycleHandle): RustLifecycleResponse; modelServiceCurrent(handle: RustLifecycleHandle): RustLifecycleResponse; modelServiceManifest(handle: RustLifecycleHandle): RustLifecycleResponse; modelServicePrepareLoad(handle: RustLifecycleHandle, source: RustLifecycleLoadSource, options?: RustLifecycleLoadOptions): RustLifecycleResponse; modelServiceInstall(handle: RustLifecycleHandle, source: RustLifecycleInstallSource): RustLifecycleResponse; modelServiceRemoteAcquisitionCommand(handle: RustLifecycleHandle, command: RustRemoteCommand): RustLifecycleResponse; modelServiceCommitLoad(handle: RustLifecycleHandle, commit: RustLifecycleCommitLoad): RustLifecycleResponse; modelServiceAbortLoad(handle: RustLifecycleHandle, error?: { message?: string; }): RustLifecycleResponse; modelServiceRemove(handle: RustLifecycleHandle, modelId: string): RustLifecycleResponse; modelServiceUnload(handle: RustLifecycleHandle): RustLifecycleResponse; modelServiceSnapshot(handle: RustLifecycleHandle): RustLifecycleResponse; modelServiceDrainEvents(handle: RustLifecycleHandle): RustLifecycleResponse; modelServiceRecordEvent(handle: RustLifecycleHandle, type: ObservabilityEventType, patch: Record): RustLifecycleResponse; sha256Text(value: string): string; sha256Blob(blob: Blob, signal?: AbortSignal): Promise; browserCacheLayout(sourceBytes: number, sourceBytesKnown: boolean, directLoadMaxBytes: number, shardMaxBytes: number): BrowserCacheLayout; detectModelFromGgufFile(file: Blob & { name?: string; }, signal?: AbortSignal): Promise; planGgufSplitCount(sourceBytes: number, shardMaxBytes: number, callbacks: GgufReadAtCallbacks): number; splitGgufStream(sourceBytes: number, outputPrefix: string, shardMaxBytes: number, callbacks: GgufSplitStreamCallbacks): void; readRuntimeObservability(): RequestObservabilityMetrics | null; readCompletedRequestRuntimeObservability(requestId: GenerateRequestId): RequestObservabilityMetrics | null; takeCompletedResponse(requestId: GenerateRequestId): GenerateResponse; private completedResponseBase; runInferenceLoop(maxTicks: number, maxCompletedResponses: number, maxGeneratedTokens: number, options?: { maxDurationUs?: number; }): Promise; getSharedTokenRingDescriptor(): SharedTokenRingDescriptor; releaseReusableBuffers(): void; private allocate; private free; private readGgufMetadataPrefix; private withWasmBytes; private ggufCallbackError; private withWasmMediaBuffers; private callOwnedString; private readUtf8String; private unwrapGgufResponse; private callLifecycleJson; private withSha256; private updateSha256; private reusableLoopResultPtr; private ensureLoopResultBuffer; private readSchedulerLoopResult; private readRuntimeObservabilityViaCall; private readCompletedEmbedding; private copyText; } export declare function parseBackendObservabilityJson(raw: string): BackendObservability; export {};