/** * Edgework SDK Types * * Core type definitions for client-side AI inference with D1/Dash storage. */ type ModelArchitecture = 'llama' | 'mistral' | 'qwen' | 'smollm' | 'phi'; type QuantizationType = 'f32' | 'f16' | 'q8_0' | 'q4_k' | 'q4_0' | 'q4_1'; /** * Model metadata stored in D1 */ interface ModelMeta { id: string; name: string; architecture: ModelArchitecture; version: string; vocabSize: number; hiddenDim: number; numLayers: number; numHeads: number; numKvHeads?: number; intermediateDim?: number; maxSeqLength: number; ropeTheta?: number; createdAt: string; } /** * Tensor chunk stored in D1 */ interface TensorChunk { id: string; modelId: string; tensorName: string; layerIndex: number | null; chunkIndex: number; totalChunks: number; dtype: QuantizationType; shape: number[]; offset: number; data: ArrayBuffer; hash: string; } /** * Tokenizer data stored in D1 */ interface TokenizerData { modelId: string; vocab: ArrayBuffer; merges?: ArrayBuffer; specialTokens: Record; chatTemplate?: string; } /** * Download/sync progress */ interface SyncProgress { modelId: string; tensorName: string; chunksDownloaded: number; totalChunks: number; lastSync?: string; } /** * User adapter for on-device RLHF */ interface UserAdapter { id: string; modelId: string; userId: string; adapterType: 'reward_head' | 'style_adapter' | 'lora'; weights: ArrayBuffer; trainingExamples: number; lastUpdated: string; } /** * Training log entry for RLHF */ interface TrainingLogEntry { id: number; adapterId: string; messageHash: string; feedback: number; createdAt: string; } /** * Edgework initialization options */ interface EdgeworkOptions { /** Model to load (e.g., 'cyrano-360m', 'cog-360m') */ model: string; /** Sync server URL for model download */ syncUrl?: string; /** Progress callback for model download */ onProgress?: (progress: DownloadProgress) => void; /** Enable on-device RLHF */ enableRLHF?: boolean; /** Enable community aggregation sync (opt-in) */ communityParticipation?: boolean; /** Federated aggregation hub URL (optional) */ federatedSyncUrl?: string; /** Micro-batch size for on-device training (default: 16) */ trainingBatchSize?: number; /** Idle flush timeout in milliseconds (default: 45000) */ trainingIdleFlushMs?: number; /** Local cache directory (Node.js only) */ cacheDir?: string; /** Anonymous user ID for RLHF */ userId?: string; /** Storage backend preference */ storageBackend?: 'opfs' | 'indexeddb' | 'memory'; /** Inference backend preference */ inferenceBackend?: 'webgpu' | 'webnn' | 'wasm' | 'cpu'; } /** * Download progress info */ interface DownloadProgress { modelId: string; percent: number; bytesDownloaded: number; totalBytes: number; currentLayer?: string; layersComplete: number; totalLayers: number; status: 'downloading' | 'verifying' | 'ready' | 'error'; error?: string; } /** * Generation options */ interface GenerateOptions { maxTokens?: number; temperature?: number; topP?: number; topK?: number; stopSequences?: string[]; stream?: boolean; } /** * Generation result */ interface GenerateResult { text: string; tokens: number[]; tokenCount: number; durationMs: number; tokensPerSecond: number; } /** * Chat message */ interface ChatMessage { role: 'system' | 'user' | 'assistant'; content: string; } /** * RLHF feedback */ interface RLHFFeedback { messageHash: string; feedback: number; hiddenState?: Float32Array; } /** * Supported multimodal inference domains */ type Modality = 'text' | 'stt' | 'tts' | 'translation' | 'embeddings' | 'classification' | 'image'; /** * Structured inference error codes */ type InferenceErrorCode = 'MODEL_NOT_READY' | 'MODEL_UNAVAILABLE' | 'QUALITY_GATE_FAILED' | 'LOCAL_TRAINING_REQUIRED'; /** * Structured multimodal inference error payload */ interface InferenceError { code: InferenceErrorCode; message: string; modality: Modality; modelId?: string; modelVersion?: string; retryAfterMs?: number; } /** * Generic multimodal inference result */ interface ModalityInferenceResult { modality: Modality; modelId: string; modelVersion?: string; output: unknown; durationMs: number; } /** * On-device training signal captured per modality */ interface ModalityTrainingSignal { messageHash?: string; feedback?: number; hiddenState?: Float32Array; metadata?: Record; } /** * Result from flushing accumulated local training signals */ interface TrainingFlushResult { modality: Modality | 'all'; trainedExamples: number; pendingExamples: number; communitySyncAttempted: boolean; communitySyncApplied: boolean; flushedAt: string; } /** * Distributed model manifest for sync */ interface ModelManifest { modelId: string; modality: Modality; baseVersion: string; adapterVersion: string; sha256: string; chunkRefs: string[]; minSdkVersion: string; } /** * Differentially-private gradient envelope */ interface GradientEnvelope { modality: Modality; modelId: string; baseVersion: string; adapterBaseVersion: string; clippedDelta: ArrayBuffer; dpNoiseSeedId: string; deviceProof: string; createdAt: string; } /** * Model status */ type ModelStatus = 'not_downloaded' | 'downloading' | 'ready' | 'offline' | 'error'; /** * Inference backend */ type InferenceBackend = 'webgpu' | 'webnn' | 'wasm' | 'cpu'; /** * Storage backend */ type StorageBackend = 'opfs' | 'indexeddb' | 'memory'; /** * Engine info */ interface EngineInfo { modelId: string; status: ModelStatus; backend: InferenceBackend; storageBackend: StorageBackend; progress?: DownloadProgress; config: ModelMeta; } /** * Layer weights for streaming inference */ interface LayerWeights { layerIndex: number; attnQ: Float32Array; attnK: Float32Array; attnV: Float32Array; attnOut: Float32Array; attnNorm: Float32Array; ffnGate: Float32Array; ffnUp: Float32Array; ffnDown: Float32Array; ffnNorm: Float32Array; } /** * KV cache for autoregressive generation */ interface KVCache { keys: Float32Array[]; values: Float32Array[]; position: number; maxLength: number; } /** * Storage interface for model weights */ interface ModelStorage { /** Get model metadata */ getModelMeta(modelId: string): Promise; /** Get tensor chunks for a layer */ getTensorChunks(modelId: string, layerIndex: number): Promise; /** Get tokenizer data */ getTokenizer(modelId: string): Promise; /** Get sync progress */ getSyncProgress(modelId: string): Promise; /** Store tensor chunk */ storeTensorChunk(chunk: TensorChunk): Promise; /** Update sync progress */ updateSyncProgress(progress: SyncProgress): Promise; /** Check if model is fully downloaded */ isModelComplete(modelId: string): Promise; /** Get total storage used */ getStorageUsed(): Promise; } /** * Sync service interface */ interface SyncService { /** Start syncing a model */ startSync(modelId: string): Promise; /** Pause syncing */ pauseSync(): Promise; /** Resume syncing */ resumeSync(): Promise; /** Cancel sync and clean up */ cancelSync(): Promise; /** Get current sync progress */ getProgress(): DownloadProgress | null; /** Check if syncing */ isSyncing(): boolean; } /** * Inference engine interface */ interface InferenceEngine { /** Initialize the engine */ initialize(): Promise; /** Generate text from prompt */ generate(prompt: string, options?: GenerateOptions): Promise; /** Stream tokens from prompt */ stream(prompt: string, options?: GenerateOptions): AsyncGenerator; /** Chat with conversation history */ chat(messages: ChatMessage[], options?: GenerateOptions): Promise; /** Get embeddings for text */ embed(text: string): Promise; /** Get last hidden state (for RLHF) */ getLastHiddenState(): Float32Array | null; /** Get engine info */ getInfo(): EngineInfo; } /** * Base Storage Class * * Abstract implementation of ModelStorage interface. */ declare abstract class BaseStorage { abstract readonly backend: StorageBackend; abstract initialize(): Promise; abstract getModelMeta(modelId: string): Promise; abstract setModelMeta(meta: ModelMeta): Promise; abstract deleteModelMeta(modelId: string): Promise; abstract listModels(): Promise; abstract getTensorChunks(modelId: string, layerIndex: number): Promise; abstract getTensorChunk(modelId: string, tensorName: string, chunkIndex: number): Promise; abstract storeTensorChunk(chunk: TensorChunk): Promise; abstract deleteTensorChunks(modelId: string): Promise; abstract getTokenizer(modelId: string): Promise; abstract setTokenizer(data: TokenizerData): Promise; abstract getSyncProgress(modelId: string): Promise; abstract updateSyncProgress(progress: SyncProgress): Promise; abstract clearSyncProgress(modelId: string): Promise; abstract getUserAdapter(modelId: string, userId: string, adapterId?: string): Promise; abstract setUserAdapter(adapter: UserAdapter): Promise; abstract deleteUserAdapter(adapterId: string): Promise; abstract addTrainingLogEntry(entry: Omit): Promise; abstract getTrainingLog(adapterId: string, limit?: number): Promise; abstract isModelComplete(modelId: string): Promise; abstract getStorageUsed(): Promise; abstract clear(): Promise; /** * Load all tensor chunks for a layer and combine into Float32Arrays */ loadLayerWeights(modelId: string, layerIndex: number): Promise>; /** * Get download progress percentage */ getDownloadProgress(modelId: string): Promise; } export { BaseStorage as B, type ChatMessage as C, type DownloadProgress as D, type EdgeworkOptions as E, type GenerateResult as G, type InferenceError as I, type KVCache as K, type LayerWeights as L, type Modality as M, type QuantizationType as Q, type RLHFFeedback as R, type StorageBackend as S, type TrainingFlushResult as T, type UserAdapter as U, type GenerateOptions as a, type ModalityInferenceResult as b, type ModalityTrainingSignal as c, type EngineInfo as d, type GradientEnvelope as e, type InferenceBackend as f, type InferenceEngine as g, type InferenceErrorCode as h, type ModelArchitecture as i, type ModelManifest as j, type ModelMeta as k, type ModelStatus as l, type ModelStorage as m, type SyncProgress as n, type SyncService as o, type TensorChunk as p, type TokenizerData as q, type TrainingLogEntry as r };