UNPKG

termcode

Version:

Superior terminal AI coding agent with enterprise-grade security, intelligent error recovery, performance monitoring, and plugin system - Advanced Claude Code alternative

109 lines (108 loc) 3.92 kB
import { KeyStore } from "../state/keystore.js"; export class CohereProvider { id = "cohere"; name = "Cohere"; supportsTools = true; maxContext = 128000; requiresKey = true; async chat(messages, opts) { const apiKey = await KeyStore.getProviderKey("cohere"); if (!apiKey) { throw new Error("Cohere API key not found. Run 'termcode /keys' to add it."); } // Convert messages to Cohere chat format const chatHistory = messages.slice(0, -1).map(m => ({ role: m.role === "system" ? "SYSTEM" : m.role === "user" ? "USER" : "CHATBOT", message: m.content })); const lastMessage = messages[messages.length - 1]; const response = await fetch("https://api.cohere.ai/v1/chat", { method: "POST", headers: { "Authorization": `Bearer ${apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: opts.model, message: lastMessage.content, chat_history: chatHistory, temperature: opts.temperature ?? 0.7, max_tokens: opts.maxTokens ?? 4000 }) }); if (!response.ok) { throw new Error(`Cohere API error: ${response.statusText}`); } const data = await response.json(); return data.text || ""; } async embed(texts, opts) { const apiKey = await KeyStore.getProviderKey("cohere"); if (!apiKey) { throw new Error("Cohere API key not found. Run 'termcode /keys' to add it."); } const response = await fetch("https://api.cohere.ai/v1/embed", { method: "POST", headers: { "Authorization": `Bearer ${apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: opts.model, texts: texts, input_type: "search_document" }) }); if (!response.ok) { throw new Error(`Cohere API error: ${response.statusText}`); } const data = await response.json(); return data.embeddings; } async listModels() { return [ // Chat models { id: "command-r-plus", type: "chat", context: 128000, costPer1kTokens: 0.003 }, { id: "command-r", type: "chat", context: 128000, costPer1kTokens: 0.0005 }, { id: "command", type: "chat", context: 4096, costPer1kTokens: 0.001 }, // Embedding models { id: "embed-english-v3.0", type: "embed", context: 512, costPer1kTokens: 0.0001 }, { id: "embed-multilingual-v3.0", type: "embed", context: 512, costPer1kTokens: 0.0001 } ]; } async healthCheck() { try { const apiKey = await KeyStore.getProviderKey("cohere"); if (!apiKey) { return { status: "error", error: "No API key configured" }; } // Return healthy with main chat models const models = ["command-r-plus", "command-r", "command"]; return { status: "healthy", models }; } catch (error) { return { status: "error", error: error.message }; } } estimateCost(tokens, model, type) { const models = { "command-r-plus": 0.003, "command-r": 0.0005, "command": 0.001, "embed-english-v3.0": 0.0001, "embed-multilingual-v3.0": 0.0001 }; const costPer1k = models[model] || 0.002; return (tokens / 1000) * costPer1k; } } export const cohereProvider = new CohereProvider();