-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(api): add cognima provider support #8868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5e5c5d5
3e179b6
0076e71
253cbb1
ad187f7
ab0d7c2
61546b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { ModelInfo } from "../model.js" | ||
|
|
||
| export type CognimaModelId = string | ||
|
|
||
| export const cognimaDefaultModelId: CognimaModelId = "gpt-4o" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import OpenAI from "openai" | ||
|
|
||
| import { type CognimaModelId, cognimaDefaultModelId } from "@roo-code/types" | ||
|
|
||
| import type { ApiHandlerOptions } from "../../shared/api" | ||
|
|
||
| import { convertToOpenAiMessages } from "../transform/openai-format" | ||
| import { ApiStreamChunk } from "../transform/stream" | ||
| import { RouterProvider } from "./router-provider" | ||
| import { handleOpenAIError } from "./utils/openai-error-handler" | ||
|
|
||
| export class CognimaHandler extends RouterProvider { | ||
| private readonly providerName = "Cognima" | ||
|
|
||
| constructor(options: ApiHandlerOptions) { | ||
| super({ | ||
| options, | ||
| name: "cognima", | ||
| baseURL: "https://cog2.cognima.com.br/openai/v1", | ||
| apiKey: options.cognimaApiKey, | ||
| modelId: options.cognimaModelId, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| defaultModelId: cognimaDefaultModelId, | ||
| defaultModelInfo: { | ||
| maxTokens: 16384, | ||
| contextWindow: 128000, | ||
| supportsImages: true, | ||
| supportsPromptCache: false, | ||
| inputPrice: 2.5, | ||
| outputPrice: 10, | ||
| supportsTemperature: true, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| override async *createMessage( | ||
| systemPrompt: string, | ||
| messages: Anthropic.Messages.MessageParam[], | ||
| ): AsyncGenerator<ApiStreamChunk> { | ||
| const model = await this.fetchModel() | ||
| const modelId = model.id | ||
| const maxTokens = model.info.maxTokens | ||
| const temperature = 0 // Default temperature | ||
|
|
||
| // Convert Anthropic messages to OpenAI format | ||
| const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ | ||
| { role: "system", content: systemPrompt }, | ||
| ...convertToOpenAiMessages(messages), | ||
| ] | ||
|
|
||
| const completionParams: OpenAI.Chat.ChatCompletionCreateParams = { | ||
| model: modelId, | ||
| ...(maxTokens && maxTokens > 0 && { max_tokens: maxTokens }), | ||
| temperature, | ||
| messages: openAiMessages, | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| } | ||
|
|
||
| let stream | ||
| try { | ||
| stream = await this.client.chat.completions.create(completionParams) | ||
| } catch (error) { | ||
| throw handleOpenAIError(error, this.providerName) | ||
| } | ||
|
|
||
| for await (const chunk of stream) { | ||
| // Handle OpenAI error responses | ||
| if ("error" in chunk) { | ||
| const error = chunk.error as { message?: string; code?: number } | ||
| console.error(`Cognima API Error: ${error?.code} - ${error?.message}`) | ||
| throw new Error(`Cognima API Error ${error?.code}: ${error?.message}`) | ||
| } | ||
|
|
||
| const delta = chunk.choices[0]?.delta | ||
|
|
||
| if (delta?.content) { | ||
| yield { type: "text", text: delta.content } | ||
| } | ||
|
|
||
| if (chunk.usage) { | ||
| const usage = chunk.usage | ||
| yield { | ||
| type: "usage", | ||
| inputTokens: usage.prompt_tokens || 0, | ||
| outputTokens: usage.completion_tokens || 0, | ||
| totalCost: 0, // Cognima doesn't provide cost info in usage | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,113 @@ | ||||
| import axios from "axios" | ||||
| import { z } from "zod" | ||||
|
|
||||
| import type { ModelInfo } from "@roo-code/types" | ||||
|
|
||||
| import { parseApiPrice } from "../../../shared/cost" | ||||
|
||||
| import { parseApiPrice } from "../../../shared/cost" |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parseApiPrice import is unused in this file and should be removed to keep the code clean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline at end of file. Add a newline after the last line to follow standard conventions.