import type { MemoryProvider } from "../types.js"; import { fetchWithTimeout } from "./_fetch.js"; export class OpenRouterProvider implements MemoryProvider { name: string; private apiKey: string; private model: string; private maxTokens: number; private baseUrl: string; constructor( apiKey: string, model: string, maxTokens: number, baseUrl: string, ) { this.apiKey = apiKey; this.model = model; this.maxTokens = maxTokens; this.baseUrl = baseUrl; this.name = baseUrl.includes("openrouter") ? "openrouter" : "gemini"; } async compress(systemPrompt: string, userPrompt: string): Promise { return this.call(systemPrompt, userPrompt); } async summarize(systemPrompt: string, userPrompt: string): Promise { return this.call(systemPrompt, userPrompt); } private async call( systemPrompt: string, userPrompt: string, ): Promise { const response = await fetchWithTimeout(this.baseUrl, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, ...(this.baseUrl.includes("openrouter") ? { "HTTP-Referer": "https://github.com/rohitg00/agentmemory" } : {}), }, body: JSON.stringify({ model: this.model, max_tokens: this.maxTokens, messages: [ { role: "system", content: systemPrompt }, { role: "user", content: userPrompt }, ], }), }); if (!response.ok) { const text = await response.text(); throw new Error(`${this.name} API error (${response.status}): ${text}`); } const data = (await response.json()) as Record; const choices = data.choices as | Array<{ message: { content: string } }> | undefined; const content = choices?.[0]?.message?.content; if (!content) { throw new Error( `${this.name} returned unexpected response: ${JSON.stringify(data).slice(0, 200)}`, ); } return content; } }