All files webllm-provider.ts

100% Statements 42/42
100% Branches 30/30
100% Functions 9/9
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                    28x           28x 28x 28x     28x           28x         28x                           28x 17x   16x 16x       16x 16x   16x   1x                   15x   15x 1x     1x               16x               1x             9x 9x   9x   9x                     9x 9x             8x 9x   9x                         1x                       1x   1x 1x                       5x 2x 2x         5x 5x 5x      
import type {
  LLMProvider,
  LLMCapabilities,
  LLMPrompt,
  LLMGenerateOptions,
  LLMResponse,
  LLMStreamChunk,
} from '@agentix-e/nl2spel';
import { MODEL_CONFIGS, type ModelConfig } from './model-configs.js';
 
export interface WebLLMConfig {
  /** Model ID */
  model: string;
 
  /** Custom model config (optional override) */
  customModelConfig?: Partial<ModelConfig>;
 
  /** Load progress callback */
  onProgress?: (progress: ModelLoadProgress) => void;
 
  /** Whether to enable GBNF grammar constraints (default true) */
  enableGrammar?: boolean;
 
  /** Whether to output logs to console */
  debug?: boolean;
}
 
export interface ModelLoadProgress {
  progress: number; // 0-1
  loaded: number; // bytes
  total: number; // bytes
  text: string; // Current phase description
}
 
/**
 * WebLLMProvider — browser-side local LLM provider.
 *
 * Uses @mlc-ai/web-llm to run local models in the browser.
 * WebLLM's CreateMLCEngine handles all GPU detection and error reporting internally.
 * This provider simply delegates to WebLLM's native capabilities.
 */
export class WebLLMProvider implements LLMProvider {
  public readonly name = 'webllm';
  public readonly capabilities: LLMCapabilities;
 
  private readonly config: WebLLMConfig;
  private readonly modelConfig: ModelConfig;
 
  private _engine: unknown = null;
  private _initialized = false;
  private _initPromise: Promise<void> | null = null;
 
  constructor(config: WebLLMConfig) {
    this.config = {
      enableGrammar: true,
      debug: false,
      ...config,
    };
 
    this.modelConfig = {
      ...(MODEL_CONFIGS[config.model] ?? MODEL_CONFIGS['gemma-2-2b-it']!),
      ...config.customModelConfig,
    };
 
    this.capabilities = {
      maxContextTokens: this.modelConfig.maxContextTokens,
      supportsGrammarConstraint: true,
      supportsStreaming: true,
      supportsStructuredOutput: false,
      offlineAvailable: true,
    };
  }
 
  /**
   * Initialize — download and load the model via WebLLM.
   * WebLLM's CreateMLCEngine handles GPU detection natively.
   */
  async initialize(): Promise<void> {
    if (this._initialized) return;
    if (this._initPromise) return this._initPromise;
 
    this._initPromise = this._doInitialize();
    return this._initPromise;
  }
 
  private async _doInitialize(): Promise<void> {
    try {
      const { CreateMLCEngine } = await this.importWebLLM();
 
      this._engine = await CreateMLCEngine(this.modelConfig.modelId, {
        initProgressCallback: (report: { progress: number; timeElapsed: number; text: string }) => {
          this.config.onProgress?.({
            progress: report.progress,
            loaded: 0,
            total: 0,
            text: report.text,
          });
        },
        logLevel: this.config.debug ? 'INFO' : 'WARN',
      });
 
      this._initialized = true;
 
      if (this.config.debug) {
        console.log(`[WebLLM] Model ${this.modelConfig.displayName} loaded successfully`);
      }
    } catch (err) {
      throw new Error(`WebLLM initialization failed: ${(err as Error).message}`);
    }
  }
 
  /**
   * Dynamic import of WebLLM (separated for test mocking)
   */
  private async importWebLLM(): Promise<any> {
    return import('@mlc-ai/web-llm');
  }
 
  /**
   * Check if the provider is available.
   * Simply returns true — actual GPU availability is determined by WebLLM at initialization.
   */
  async isAvailable(): Promise<boolean> {
    return true;
  }
 
  /**
   * Generate a SpEL expression
   */
  async generate(prompt: LLMPrompt, options?: LLMGenerateOptions): Promise<LLMResponse> {
    await this.initialize();
    const engine = this._engine as any;
 
    const startTime = Date.now();
 
    const messages = [
      { role: 'system' as const, content: prompt.system },
      { role: 'user' as const, content: prompt.user },
    ];
 
    // GBNF grammar generation is available via GBNFGenerator for manual use.
    // Auto-injection into WebLLM requests is not yet supported by @mlc-ai/web-llm.
    // if (this.config.enableGrammar) {
    //   this.gbnfGenerator.generate(prompt.contextSchema);
    // }
 
    try {
      const completion = await engine.chat.completions.create({
        messages,
        temperature: options?.temperature ?? 0.1,
        max_tokens: options?.maxTokens ?? 512,
        top_p: options?.topP ?? 0.9,
      });
 
      const text = completion.choices?.[0]?.message?.content?.trim() ?? '';
      const usage = completion.usage;
 
      return {
        text,
        model: this.modelConfig.displayName,
        usage: {
          promptTokens: usage?.prompt_tokens ?? 0,
          completionTokens: usage?.completion_tokens ?? 0,
          totalTokens: usage?.total_tokens ?? 0,
        },
        latencyMs: Date.now() - startTime,
        finishReason: completion.choices?.[0]?.finish_reason ?? 'stop',
        providerName: 'webllm',
      };
    } catch (err) {
      throw new Error(`WebLLM generation failed: ${(err as Error).message}`);
    }
  }
 
  /**
   * Streaming generation — falls back to generate() since WebLLM stream API
   * requires extra handling not yet implemented.
   */
  async *generateStream(
    prompt: LLMPrompt,
    options?: LLMGenerateOptions,
  ): AsyncIterable<LLMStreamChunk> {
    await this.initialize();
 
    const result = await this.generate(prompt, options);
    yield {
      delta: result.text,
      accumulated: result.text,
      done: true,
      finishReason: result.finishReason,
    };
  }
 
  /**
   * Release resources
   */
  async dispose(): Promise<void> {
    if (this._engine && typeof (this._engine as any).unload === 'function') {
      try {
        (this._engine as any).unload();
      } catch {
        // Ignore unload errors
      }
    }
    this._engine = null;
    this._initialized = false;
    this._initPromise = null;
  }
}