Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
59 lines · 1.5 KB
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
/**
 * Ollama AI Adapter — Local LLM provider
 *
 * Connects to a locally-running Ollama instance for private,
 * cost-free AI code reviews. No data leaves your infrastructure.
 */

import type {
  AIAdapter,
  AICompletionResult,
  AIConfig,
  AIReviewPrompt,
} from "./types";

export class OllamaAdapter implements AIAdapter {
  async complete(
    prompt: AIReviewPrompt,
    config: AIConfig,
  ): Promise<AICompletionResult> {
    const baseUrl =
      config.baseUrl || process.env.OLLAMA_BASE_URL || "http://localhost:11434";
    const model = config.model || process.env.OLLAMA_MODEL || "codellama:13b";

    const response = await fetch(`${baseUrl}/api/chat`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        model,
        messages: [
          { role: "system", content: prompt.system },
          { role: "user", content: prompt.user },
        ],
        stream: false,
        options: {
          temperature: config.temperature ?? 0.1,
          num_predict: config.maxTokens ?? 4096,
        },
        format: "json",
      }),
    });

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(`Ollama error (${response.status}): ${errorText}`);
    }

    const data = await response.json();

    return {
      content: data.message?.content || "",
      usage: {
        inputTokens: data.prompt_eval_count || 0,
        outputTokens: data.eval_count || 0,
        totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0),
      },
    };
  }
}