All files / providers LMStudioProvider.ts

47.36% Statements 9/19
0% Branches 0/16
50% Functions 2/4
47.36% Lines 9/19

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    1x 4x 4x 4x     4x 4x       4x 4x     4x                                                                                                
import { AIProvider, AIResponse, ChatOptions, Message } from '../core/interfaces';
 
export class LMStudioProvider implements AIProvider {
  readonly name = 'LM Studio (Local)';
  readonly type = 'lmstudio' as const;
  readonly modelName = 'local-model';
 
  constructor(
    private readonly endpoint: string,
    private readonly timeout: number
  ) {}
 
  async isAvailable(): Promise<boolean> {
    try {
      const resp = await fetch(`${this.endpoint}/v1/models`, {
        signal: AbortSignal.timeout(2000),
      });
      return resp.ok;
    } catch {
      return false;
    }
  }
 
  async chat(messages: Message[], options: ChatOptions = {}): Promise<AIResponse> {
    const body = {
      messages: options.systemPrompt
        ? [{ role: 'system', content: options.systemPrompt }, ...messages]
        : messages,
      max_tokens: options.maxTokens ?? 4096,
      temperature: options.temperature ?? 0.1,
    };
 
    const resp = await fetch(`${this.endpoint}/v1/chat/completions`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(this.timeout),
    });
 
    Iif (!resp.ok) {
      const err = await resp.text();
      throw new Error(`LM Studio ${resp.status}: ${err}`);
    }
 
    const data = await resp.json() as {
      choices: Array<{ message: { content: string } }>;
      model?: string;
      usage?: { prompt_tokens: number; completion_tokens: number };
    };
 
    return {
      content: data.choices[0]?.message?.content ?? '',
      model: data.model ?? 'local',
      usage: {
        promptTokens: data.usage?.prompt_tokens ?? 0,
        completionTokens: data.usage?.completion_tokens ?? 0,
      },
    };
  }
 
  async complete(prompt: string, options?: ChatOptions): Promise<string> {
    const resp = await this.chat([{ role: 'user', content: prompt }], options);
    return resp.content;
  }
}