import {
Agent,
registry,
type ModelProvider,
type ChatMessage,
type ModelResponse,
type StreamChunk,
} from "@radaros/core";
class CohereProvider implements ModelProvider {
readonly providerId = "cohere";
readonly modelId: string;
private apiKey: string;
constructor(modelId: string, config: { apiKey: string }) {
this.modelId = modelId;
this.apiKey = config.apiKey;
}
async generate(messages: ChatMessage[]): Promise<ModelResponse> {
const res = await fetch("https://api.cohere.ai/v1/chat", {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: this.modelId,
message: messages[messages.length - 1]?.content,
}),
});
const data = await res.json();
return {
message: { role: "assistant", content: data.text },
usage: data.meta?.tokens ?? { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
finishReason: "stop",
raw: data,
};
}
async *stream(): AsyncGenerator<StreamChunk> {
// Implement streaming if your API supports it
yield { type: "finish", finishReason: "stop", usage: undefined };
}
}
registry.register("cohere", (modelId, config) => {
return new CohereProvider(modelId, config as { apiKey: string });
});
const model = registry.resolve("cohere", "command-r", {
apiKey: process.env.COHERE_API_KEY,
});
const agent = new Agent({ name: "Cohere Agent", model });