Add suggested changes

This commit is contained in:
Kevin Taylor 2025-07-29 16:36:09 -07:00
parent e2af6818d7
commit f9f619bf1f
21 changed files with 232 additions and 34 deletions

View file

@ -35,7 +35,7 @@ export const cerebrasModels = {
},
"qwen-3-235b-a22b-instruct-2507": {
maxTokens: 64000,
contextWindow: 640000,
contextWindow: 64000,
supportsImages: false,
supportsPromptCache: false,
inputPrice: 0,

View file

@ -0,0 +1,152 @@
import { describe, it, expect, vi, beforeEach } from "vitest"
import { CerebrasHandler } from "../cerebras"
import { cerebrasModels, type CerebrasModelId } from "@roo-code/types"
// Mock fetch globally
global.fetch = vi.fn()
describe("CerebrasHandler", () => {
let handler: CerebrasHandler
const mockOptions = {
cerebrasApiKey: "test-api-key",
apiModelId: "llama-3.3-70b" as CerebrasModelId,
}
beforeEach(() => {
vi.clearAllMocks()
handler = new CerebrasHandler(mockOptions)
})
describe("constructor", () => {
it("should throw error when API key is missing", () => {
expect(() => new CerebrasHandler({ cerebrasApiKey: "" })).toThrow("Cerebras API key is required")
})
it("should initialize with valid API key", () => {
expect(() => new CerebrasHandler(mockOptions)).not.toThrow()
})
})
describe("getModel", () => {
it("should return correct model info", () => {
const { id, info } = handler.getModel()
expect(id).toBe("llama-3.3-70b")
expect(info).toEqual(cerebrasModels["llama-3.3-70b"])
})
it("should fallback to default model when apiModelId is not provided", () => {
const handlerWithoutModel = new CerebrasHandler({ cerebrasApiKey: "test" })
const { id } = handlerWithoutModel.getModel()
expect(id).toBe("qwen-3-235b-a22b-instruct-2507") // cerebrasDefaultModelId
})
})
describe("message conversion", () => {
it("should strip thinking tokens from assistant messages", () => {
// This would test the stripThinkingTokens function
// Implementation details would test the regex functionality
})
it("should flatten complex message content to strings", () => {
// This would test the flattenMessageContent function
// Test various content types: strings, arrays, image objects
})
it("should convert OpenAI messages to Cerebras format", () => {
// This would test the convertToCerebrasMessages function
// Ensure all messages have string content and proper role/content structure
})
})
describe("createMessage", () => {
it("should make correct API request", async () => {
// Mock successful API response
const mockResponse = {
ok: true,
body: {
getReader: () => ({
read: vi.fn().mockResolvedValueOnce({ done: true, value: new Uint8Array() }),
releaseLock: vi.fn(),
}),
},
}
vi.mocked(fetch).mockResolvedValueOnce(mockResponse as any)
const generator = handler.createMessage("System prompt", [])
// Test that fetch was called with correct parameters
expect(fetch).toHaveBeenCalledWith(
"https://api.cerebras.ai/v1/chat/completions",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
"Content-Type": "application/json",
Authorization: "Bearer test-api-key",
"User-Agent": "roo-cline/1.0.0",
}),
}),
)
})
it("should handle API errors properly", async () => {
const mockErrorResponse = {
ok: false,
status: 400,
text: () => Promise.resolve('{"error": "Bad Request"}'),
}
vi.mocked(fetch).mockResolvedValueOnce(mockErrorResponse as any)
const generator = handler.createMessage("System prompt", [])
await expect(generator.next()).rejects.toThrow("Cerebras API Error: 400")
})
it("should parse streaming responses correctly", async () => {
// Test streaming response parsing
// Mock ReadableStream with various data chunks
// Verify thinking token extraction and usage tracking
})
it("should handle temperature clamping", async () => {
const handlerWithTemp = new CerebrasHandler({
...mockOptions,
modelTemperature: 2.0, // Above Cerebras max of 1.5
})
vi.mocked(fetch).mockResolvedValueOnce({
ok: true,
body: { getReader: () => ({ read: () => Promise.resolve({ done: true }), releaseLock: vi.fn() }) },
} as any)
await handlerWithTemp.createMessage("test", []).next()
const requestBody = JSON.parse(vi.mocked(fetch).mock.calls[0][1]?.body as string)
expect(requestBody.temperature).toBe(1.5) // Should be clamped
})
})
describe("completePrompt", () => {
it("should handle non-streaming completion", async () => {
const mockResponse = {
ok: true,
json: () =>
Promise.resolve({
choices: [{ message: { content: "Test response" } }],
}),
}
vi.mocked(fetch).mockResolvedValueOnce(mockResponse as any)
const result = await handler.completePrompt("Test prompt")
expect(result).toBe("Test response")
})
})
describe("token usage and cost calculation", () => {
it("should track token usage properly", () => {
// Test that lastUsage is updated correctly
// Test getApiCost returns calculated cost based on actual usage
})
it("should provide usage estimates when API doesn't return usage", () => {
// Test fallback token estimation logic
})
})
})

View file

@ -134,22 +134,6 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
: {}),
}
console.log("[CEREBRAS DEBUG] Request URL:", `${CEREBRAS_BASE_URL}/chat/completions`)
console.log("[CEREBRAS DEBUG] Request body:", JSON.stringify(requestBody, null, 2))
console.log("[CEREBRAS DEBUG] API key present:", !!this.apiKey)
console.log("[CEREBRAS DEBUG] Message conversion:")
console.log(" - Original messages:", messages.length)
console.log(" - OpenAI messages:", openaiMessages.length)
console.log(" - Cerebras messages:", cerebrasMessages.length)
console.log(
" - All content is strings:",
cerebrasMessages.every((msg) => typeof msg.content === "string"),
)
console.log(
" - Thinking tokens stripped from assistant messages:",
cerebrasMessages.filter((msg) => msg.role === "assistant").length > 0 ? "✅" : "N/A",
)
try {
const response = await fetch(`${CEREBRAS_BASE_URL}/chat/completions`, {
method: "POST",
@ -161,26 +145,33 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
body: JSON.stringify(requestBody),
})
console.log("[CEREBRAS DEBUG] Response status:", response.status)
const headersObj: Record<string, string> = {}
response.headers.forEach((value, key) => {
headersObj[key] = value
})
console.log("[CEREBRAS DEBUG] Response headers:", headersObj)
if (!response.ok) {
const errorText = await response.text()
console.error("[CEREBRAS DEBUG] Error response body:", errorText)
let errorDetails = "Unknown error"
let errorMessage = "Unknown error"
try {
const errorJson = JSON.parse(errorText)
errorDetails = JSON.stringify(errorJson, null, 2)
errorMessage = errorJson.error?.message || errorJson.message || JSON.stringify(errorJson, null, 2)
} catch {
errorDetails = errorText || `HTTP ${response.status}`
errorMessage = errorText || `HTTP ${response.status}`
}
throw new Error(`Cerebras API Error: ${response.status} - ${errorDetails}`)
// Provide more actionable error messages
if (response.status === 401) {
throw new Error(
`Cerebras API authentication failed. Please check your API key is valid and not expired.`,
)
} else if (response.status === 403) {
throw new Error(
`Cerebras API access forbidden. Your API key may not have access to the requested model or feature.`,
)
} else if (response.status === 429) {
throw new Error(`Cerebras API rate limit exceeded. Please wait before making another request.`)
} else if (response.status >= 500) {
throw new Error(`Cerebras API server error (${response.status}). Please try again later.`)
} else {
throw new Error(`Cerebras API Error (${response.status}): ${errorMessage}`)
}
}
if (!response.body) {
@ -241,7 +232,7 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
}
}
} catch (error) {
console.error("[CEREBRAS DEBUG] Failed to parse streaming data:", error, "Line:", line)
// Silently ignore malformed streaming data lines
}
}
}
@ -270,8 +261,6 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
outputTokens,
}
} catch (error) {
console.error("[CEREBRAS] Streaming error:", error)
if (error instanceof Error) {
throw new Error(`Cerebras API error: ${error.message}`)
}
@ -302,7 +291,23 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
if (!response.ok) {
const errorText = await response.text()
throw new Error(`Cerebras API Error: ${response.status} - ${errorText}`)
// Provide consistent error handling with createMessage
if (response.status === 401) {
throw new Error(
`Cerebras API authentication failed. Please check your API key is valid and not expired.`,
)
} else if (response.status === 403) {
throw new Error(
`Cerebras API access forbidden. Your API key may not have access to the requested model or feature.`,
)
} else if (response.status === 429) {
throw new Error(`Cerebras API rate limit exceeded. Please wait before making another request.`)
} else if (response.status >= 500) {
throw new Error(`Cerebras API server error (${response.status}). Please try again later.`)
} else {
throw new Error(`Cerebras API Error (${response.status}): ${errorText}`)
}
}
const result = await response.json()

View file

@ -6,6 +6,8 @@ import {
anthropicModels,
bedrockDefaultModelId,
bedrockModels,
cerebrasDefaultModelId,
cerebrasModels,
deepSeekDefaultModelId,
deepSeekModels,
moonshotDefaultModelId,
@ -224,11 +226,16 @@ function getSelectedModel({
const info = claudeCodeModels[id as keyof typeof claudeCodeModels]
return { id, info: { ...openAiModelInfoSaneDefaults, ...info } }
}
case "cerebras": {
const id = apiConfiguration.apiModelId ?? cerebrasDefaultModelId
const info = cerebrasModels[id as keyof typeof cerebrasModels]
return { id, info }
}
// case "anthropic":
// case "human-relay":
// case "fake-ai":
default: {
provider satisfies "anthropic" | "gemini-cli" | "human-relay" | "fake-ai" | "cerebras"
provider satisfies "anthropic" | "gemini-cli" | "human-relay" | "fake-ai"
const id = apiConfiguration.apiModelId ?? anthropicDefaultModelId
const info = anthropicModels[id as keyof typeof anthropicModels]
return { id, info }

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Clau API d'Anthropic",
"getAnthropicApiKey": "Obtenir clau API d'Anthropic",
"anthropicUseAuthToken": "Passar la clau API d'Anthropic com a capçalera d'autorització en lloc de X-Api-Key",
"cerebrasApiKey": "Clau API de Cerebras",
"getCerebrasApiKey": "Obtenir clau API de Cerebras",
"chutesApiKey": "Clau API de Chutes",
"getChutesApiKey": "Obtenir clau API de Chutes",
"deepSeekApiKey": "Clau API de DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API-Schlüssel",
"getAnthropicApiKey": "Anthropic API-Schlüssel erhalten",
"anthropicUseAuthToken": "Anthropic API-Schlüssel als Authorization-Header anstelle von X-Api-Key übergeben",
"cerebrasApiKey": "Cerebras API-Schlüssel",
"getCerebrasApiKey": "Cerebras API-Schlüssel erhalten",
"chutesApiKey": "Chutes API-Schlüssel",
"getChutesApiKey": "Chutes API-Schlüssel erhalten",
"deepSeekApiKey": "DeepSeek API-Schlüssel",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Clave API de Anthropic",
"getAnthropicApiKey": "Obtener clave API de Anthropic",
"anthropicUseAuthToken": "Pasar la clave API de Anthropic como encabezado de autorización en lugar de X-Api-Key",
"cerebrasApiKey": "Clave API de Cerebras",
"getCerebrasApiKey": "Obtener clave API de Cerebras",
"chutesApiKey": "Clave API de Chutes",
"getChutesApiKey": "Obtener clave API de Chutes",
"deepSeekApiKey": "Clave API de DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Clé API Anthropic",
"getAnthropicApiKey": "Obtenir la clé API Anthropic",
"anthropicUseAuthToken": "Passer la clé API Anthropic comme en-tête d'autorisation au lieu de X-Api-Key",
"cerebrasApiKey": "Clé API Cerebras",
"getCerebrasApiKey": "Obtenir la clé API Cerebras",
"chutesApiKey": "Clé API Chutes",
"getChutesApiKey": "Obtenir la clé API Chutes",
"deepSeekApiKey": "Clé API DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API कुंजी",
"getAnthropicApiKey": "Anthropic API कुंजी प्राप्त करें",
"anthropicUseAuthToken": "X-Api-Key के बजाय Anthropic API कुंजी को Authorization हेडर के रूप में पास करें",
"cerebrasApiKey": "Cerebras API कुंजी",
"getCerebrasApiKey": "Cerebras API कुंजी प्राप्त करें",
"chutesApiKey": "Chutes API कुंजी",
"getChutesApiKey": "Chutes API कुंजी प्राप्त करें",
"deepSeekApiKey": "DeepSeek API कुंजी",

View file

@ -253,6 +253,8 @@
"anthropicApiKey": "Anthropic API Key",
"getAnthropicApiKey": "Dapatkan Anthropic API Key",
"anthropicUseAuthToken": "Kirim Anthropic API Key sebagai Authorization header alih-alih X-Api-Key",
"cerebrasApiKey": "Cerebras API Key",
"getCerebrasApiKey": "Dapatkan Cerebras API Key",
"chutesApiKey": "Chutes API Key",
"getChutesApiKey": "Dapatkan Chutes API Key",
"deepSeekApiKey": "DeepSeek API Key",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Chiave API Anthropic",
"getAnthropicApiKey": "Ottieni chiave API Anthropic",
"anthropicUseAuthToken": "Passa la chiave API Anthropic come header di autorizzazione invece di X-Api-Key",
"cerebrasApiKey": "Chiave API Cerebras",
"getCerebrasApiKey": "Ottieni chiave API Cerebras",
"chutesApiKey": "Chiave API Chutes",
"getChutesApiKey": "Ottieni chiave API Chutes",
"deepSeekApiKey": "Chiave API DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic APIキー",
"getAnthropicApiKey": "Anthropic APIキーを取得",
"anthropicUseAuthToken": "Anthropic APIキーをX-Api-Keyの代わりにAuthorizationヘッダーとして渡す",
"cerebrasApiKey": "Cerebras APIキー",
"getCerebrasApiKey": "Cerebras APIキーを取得",
"chutesApiKey": "Chutes APIキー",
"getChutesApiKey": "Chutes APIキーを取得",
"deepSeekApiKey": "DeepSeek APIキー",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API 키",
"getAnthropicApiKey": "Anthropic API 키 받기",
"anthropicUseAuthToken": "X-Api-Key 대신 Authorization 헤더로 Anthropic API 키 전달",
"cerebrasApiKey": "Cerebras API 키",
"getCerebrasApiKey": "Cerebras API 키 가져오기",
"chutesApiKey": "Chutes API 키",
"getChutesApiKey": "Chutes API 키 받기",
"deepSeekApiKey": "DeepSeek API 키",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API-sleutel",
"getAnthropicApiKey": "Anthropic API-sleutel ophalen",
"anthropicUseAuthToken": "Anthropic API-sleutel als Authorization-header doorgeven in plaats van X-Api-Key",
"cerebrasApiKey": "Cerebras API-sleutel",
"getCerebrasApiKey": "Cerebras API-sleutel verkrijgen",
"chutesApiKey": "Chutes API-sleutel",
"getChutesApiKey": "Chutes API-sleutel ophalen",
"deepSeekApiKey": "DeepSeek API-sleutel",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Klucz API Anthropic",
"getAnthropicApiKey": "Uzyskaj klucz API Anthropic",
"anthropicUseAuthToken": "Przekaż klucz API Anthropic jako nagłówek Authorization zamiast X-Api-Key",
"cerebrasApiKey": "Klucz API Cerebras",
"getCerebrasApiKey": "Pobierz klucz API Cerebras",
"chutesApiKey": "Klucz API Chutes",
"getChutesApiKey": "Uzyskaj klucz API Chutes",
"deepSeekApiKey": "Klucz API DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Chave de API Anthropic",
"getAnthropicApiKey": "Obter chave de API Anthropic",
"anthropicUseAuthToken": "Passar a chave de API Anthropic como cabeçalho Authorization em vez de X-Api-Key",
"cerebrasApiKey": "Chave de API Cerebras",
"getCerebrasApiKey": "Obter chave de API Cerebras",
"chutesApiKey": "Chave de API Chutes",
"getChutesApiKey": "Obter chave de API Chutes",
"deepSeekApiKey": "Chave de API DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API-ключ",
"getAnthropicApiKey": "Получить Anthropic API-ключ",
"anthropicUseAuthToken": "Передавать Anthropic API-ключ как Authorization-заголовок вместо X-Api-Key",
"cerebrasApiKey": "Cerebras API-ключ",
"getCerebrasApiKey": "Получить Cerebras API-ключ",
"chutesApiKey": "Chutes API-ключ",
"getChutesApiKey": "Получить Chutes API-ключ",
"deepSeekApiKey": "DeepSeek API-ключ",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API Anahtarı",
"getAnthropicApiKey": "Anthropic API Anahtarı Al",
"anthropicUseAuthToken": "Anthropic API Anahtarını X-Api-Key yerine Authorization başlığı olarak geçir",
"cerebrasApiKey": "Cerebras API Anahtarı",
"getCerebrasApiKey": "Cerebras API Anahtarını Al",
"chutesApiKey": "Chutes API Anahtarı",
"getChutesApiKey": "Chutes API Anahtarı Al",
"deepSeekApiKey": "DeepSeek API Anahtarı",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Khóa API Anthropic",
"getAnthropicApiKey": "Lấy khóa API Anthropic",
"anthropicUseAuthToken": "Truyền khóa API Anthropic dưới dạng tiêu đề Authorization thay vì X-Api-Key",
"cerebrasApiKey": "Khóa API Cerebras",
"getCerebrasApiKey": "Lấy khóa API Cerebras",
"chutesApiKey": "Khóa API Chutes",
"getChutesApiKey": "Lấy khóa API Chutes",
"deepSeekApiKey": "Khóa API DeepSeek",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API 密钥",
"getAnthropicApiKey": "获取 Anthropic API 密钥",
"anthropicUseAuthToken": "将 Anthropic API 密钥作为 Authorization 标头传递,而不是 X-Api-Key",
"cerebrasApiKey": "Cerebras API 密钥",
"getCerebrasApiKey": "获取 Cerebras API 密钥",
"chutesApiKey": "Chutes API 密钥",
"getChutesApiKey": "获取 Chutes API 密钥",
"deepSeekApiKey": "DeepSeek API 密钥",

View file

@ -249,6 +249,8 @@
"anthropicApiKey": "Anthropic API 金鑰",
"getAnthropicApiKey": "取得 Anthropic API 金鑰",
"anthropicUseAuthToken": "將 Anthropic API 金鑰作為 Authorization 標頭傳遞,而非使用 X-Api-Key",
"cerebrasApiKey": "Cerebras API 金鑰",
"getCerebrasApiKey": "取得 Cerebras API 金鑰",
"chutesApiKey": "Chutes API 金鑰",
"getChutesApiKey": "取得 Chutes API 金鑰",
"deepSeekApiKey": "DeepSeek API 金鑰",