xonecas.io
5875
Os

ollama-space-molt-player / files

ollama-space-molt-player / packages / client / src / ollama.ts

2.9 KB
1import type { OllamaTool } from "./mcp-client.ts";
2
3export interface OllamaConfig {
4	baseUrl: string;
5	model: string;
6	options: Record<string, unknown>;
7	timeout: number;
8}
9
10export interface ChatMessage {
11	role: "system" | "user" | "assistant" | "tool";
12	content: string;
13	tool_calls?: ToolCall[];
14	name?: string;
15}
16
17export interface ToolCall {
18	function: {
19		name: string;
20		arguments: Record<string, unknown>;
21	};
22}
23
24interface OllamaChatRequest {
25	model: string;
26	messages: ChatMessage[];
27	tools?: OllamaTool[];
28	stream: boolean;
29	options?: Record<string, unknown>;
30}
31
32interface OllamaChatResponse {
33	model: string;
34	created_at: string;
35	message: {
36		role: string;
37		content: string;
38		tool_calls?: ToolCall[];
39	};
40	done: boolean;
41	done_reason?: string;
42}
43
44export interface ChatResult {
45	content: string;
46	toolCalls: ToolCall[];
47	thinking?: string;
48}
49
50export class OllamaClient {
51	private config: OllamaConfig;
52
53	constructor(config: OllamaConfig) {
54		this.config = config;
55	}
56
57	async chat(messages: ChatMessage[], tools?: OllamaTool[]): Promise<ChatResult> {
58		const url = `${this.config.baseUrl}/api/chat`;
59
60		const request: OllamaChatRequest = {
61			model: this.config.model,
62			messages,
63			stream: false,
64			options: this.config.options,
65		};
66
67		if (tools && tools.length > 0) {
68			request.tools = tools;
69		}
70
71		try {
72			const response = await fetch(url, {
73				method: "POST",
74				headers: {
75					"Content-Type": "application/json",
76				},
77				body: JSON.stringify(request),
78				signal: AbortSignal.timeout(this.config.timeout),
79			});
80
81			if (!response.ok) {
82				throw new Error(`Ollama request failed with status ${response.status}`);
83			}
84
85			const data = (await response.json()) as OllamaChatResponse;
86
87			const content = data.message?.content || "";
88			const toolCalls = data.message?.tool_calls || [];
89
90			const { cleanContent, thinking } = this.extractThinking(content);
91
92			return {
93				content: cleanContent,
94				toolCalls,
95				thinking,
96			};
97		} catch (error) {
98			if (error instanceof Error) {
99				if (error.name === "AbortError") {
100					throw new Error(`Ollama request timed out after ${this.config.timeout}ms`);
101				}
102				throw new Error(`Ollama request failed: ${error.message}`);
103			}
104			throw new Error("Ollama request failed with unknown error");
105		}
106	}
107
108	private extractThinking(content: string): { cleanContent: string; thinking?: string } {
109		const thinkPattern = /<think>([\s\S]*?)<\/think>/g;
110		const matches = Array.from(content.matchAll(thinkPattern));
111		const blocks = matches
112			.map((match) => match[1]?.trim() || "")
113			.filter((block) => block.length > 0);
114		const thinking = blocks.length > 0 ? blocks.join("\n") : undefined;
115		const cleanContent = content.replace(thinkPattern, "").trim();
116		return { cleanContent, thinking };
117	}
118
119	getConfig(): OllamaConfig {
120		return { ...this.config };
121	}
122
123	updateConfig(config: Partial<OllamaConfig>): void {
124		this.config = { ...this.config, ...config };
125	}
126}