-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add Tetrate Agent Router Service (TARS) as provider #6832
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
Closed
arifsetiawan
wants to merge
17
commits into
RooCodeInc:main
from
arifsetiawan:feature/add-tars-as-provider
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6f0ad59
Add TARS as provider
arifsetiawan a1e3535
fix translation
arifsetiawan 04579e2
Merge remote-tracking branch 'upstream/main' into feature/add-tars-as…
arifsetiawan 1e84fcb
update tars page
arifsetiawan 07a7877
fix: add type assertions for tars provider compatibility
arifsetiawan 8119b11
Merge remote-tracking branch 'upstream/main' into feature/add-tars-as…
arifsetiawan a23fb19
add TARS provider translations for all locales
arifsetiawan 8e7fa56
fix test
arifsetiawan 87d4d00
fix: remove any types from tars provider and CustomModesManager
daniel-lxs 1052b20
revert changes
arifsetiawan 9a289fd
revert changes
arifsetiawan 7d6a6da
revert changes
arifsetiawan 1718264
revert changes
arifsetiawan 202ae37
Merge remote-tracking branch 'upstream/main' into feature/add-tars-as…
arifsetiawan bf9b87d
fix import
arifsetiawan 00fa9ae
revert changes
arifsetiawan 05f3b52
revert changes
arifsetiawan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { ModelInfo } from "../model.js" | ||
|
|
||
| export const tarsDefaultModelId = "claude-3-5-haiku-20241022" | ||
|
|
||
| export const tarsDefaultModelInfo: ModelInfo = { | ||
| maxTokens: 8192, | ||
| contextWindow: 200000, | ||
| supportsImages: true, | ||
| supportsComputerUse: false, | ||
| supportsPromptCache: true, | ||
| inputPrice: 0.8, | ||
| outputPrice: 4.0, | ||
| cacheWritesPrice: 1.0, | ||
| cacheReadsPrice: 0.08, | ||
| description: | ||
| "Claude 3.5 Haiku - Fast and cost-effective with excellent coding capabilities. Ideal for development tasks with 200k context window", | ||
| } | ||
|
|
||
| export const tarsModels = { | ||
| [tarsDefaultModelId]: tarsDefaultModelInfo, | ||
| } as const satisfies Record<string, ModelInfo> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| // npx vitest run api/providers/__tests__/tars.spec.ts | ||
|
|
||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import OpenAI from "openai" | ||
|
|
||
| import { TarsHandler } from "../tars" | ||
| import { ApiHandlerOptions } from "../../../shared/api" | ||
| import { Package } from "../../../shared/package" | ||
|
|
||
| const mockCreate = vitest.fn() | ||
|
|
||
| vitest.mock("openai", () => { | ||
| return { | ||
| default: vitest.fn().mockImplementation(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })), | ||
| } | ||
| }) | ||
|
|
||
| vitest.mock("delay", () => ({ default: vitest.fn(() => Promise.resolve()) })) | ||
|
|
||
| vitest.mock("../fetchers/modelCache", () => ({ | ||
| getModels: vitest.fn().mockImplementation(() => { | ||
| return Promise.resolve({ | ||
| "gpt-4o": { | ||
| maxTokens: 16384, | ||
| contextWindow: 128000, | ||
| supportsImages: true, | ||
| supportsPromptCache: true, | ||
| supportsComputerUse: false, | ||
| inputPrice: 2.5, | ||
| outputPrice: 10.0, | ||
| cacheWritesPrice: 0, | ||
| cacheReadsPrice: 0, | ||
| description: | ||
| "OpenAI GPT-4o model routed through TARS for optimal performance and reliability. TARS automatically selects the best available provider.", | ||
| }, | ||
| }) | ||
| }), | ||
| })) | ||
|
|
||
| describe("TarsHandler", () => { | ||
| const mockOptions: ApiHandlerOptions = { | ||
| tarsApiKey: "test-key", | ||
| tarsModelId: "gpt-4o", | ||
| } | ||
|
|
||
| beforeEach(() => vitest.clearAllMocks()) | ||
|
|
||
| it("initializes with correct options", () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| expect(handler).toBeInstanceOf(TarsHandler) | ||
|
|
||
| expect(OpenAI).toHaveBeenCalledWith({ | ||
| baseURL: "https://api.router.tetrate.ai/v1", | ||
| apiKey: mockOptions.tarsApiKey, | ||
| defaultHeaders: { | ||
| "HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline", | ||
| "X-Title": "Roo Code", | ||
| "User-Agent": `RooCode/${Package.version}`, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| describe("fetchModel", () => { | ||
| it("returns correct model info when options are provided", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| const result = await handler.fetchModel() | ||
|
|
||
| expect(result).toMatchObject({ | ||
| id: mockOptions.tarsModelId, | ||
| info: { | ||
| maxTokens: 16384, | ||
| contextWindow: 128000, | ||
| supportsImages: true, | ||
| supportsPromptCache: true, | ||
| supportsComputerUse: false, | ||
| inputPrice: 2.5, | ||
| outputPrice: 10.0, | ||
| cacheWritesPrice: 0, | ||
| cacheReadsPrice: 0, | ||
| description: | ||
| "OpenAI GPT-4o model routed through TARS for optimal performance and reliability. TARS automatically selects the best available provider.", | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it("returns default model info when options are not provided", async () => { | ||
| const handler = new TarsHandler({}) | ||
| const result = await handler.fetchModel() | ||
|
|
||
| expect(result).toMatchObject({ | ||
| id: "claude-3-5-haiku-20241022", | ||
| info: { | ||
| maxTokens: 8192, | ||
| contextWindow: 200000, | ||
| supportsImages: true, | ||
| supportsPromptCache: true, | ||
| supportsComputerUse: false, | ||
| inputPrice: 0.8, | ||
| outputPrice: 4.0, | ||
| cacheWritesPrice: 1.0, | ||
| cacheReadsPrice: 0.08, | ||
| description: | ||
| "Claude 3.5 Haiku - Fast and cost-effective with excellent coding capabilities. Ideal for development tasks with 200k context window", | ||
| }, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("createMessage", () => { | ||
| it("generates correct stream chunks", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
|
|
||
| const mockStream = { | ||
| async *[Symbol.asyncIterator]() { | ||
| yield { | ||
| id: mockOptions.tarsModelId, | ||
| choices: [{ delta: { content: "test response" } }], | ||
| } | ||
| yield { | ||
| id: "test-id", | ||
| choices: [{ delta: { reasoning_content: "test reasoning" } }], | ||
| } | ||
| yield { | ||
| id: "test-id", | ||
| choices: [{ delta: {} }], | ||
| usage: { | ||
| prompt_tokens: 10, | ||
| completion_tokens: 20, | ||
| prompt_tokens_details: { | ||
| caching_tokens: 5, | ||
| cached_tokens: 2, | ||
| }, | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| mockCreate.mockResolvedValue(mockStream) | ||
|
|
||
| const systemPrompt = "test system prompt" | ||
| const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "test message" }] | ||
| const metadata = { taskId: "test-task-id", mode: "test-mode" } | ||
|
|
||
| const generator = handler.createMessage(systemPrompt, messages, metadata) | ||
| const chunks = [] | ||
|
|
||
| for await (const chunk of generator) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // Verify stream chunks | ||
| expect(chunks).toHaveLength(3) // text, reasoning, and usage chunks | ||
| expect(chunks[0]).toEqual({ type: "text", text: "test response" }) | ||
| expect(chunks[1]).toEqual({ type: "reasoning", text: "test reasoning" }) | ||
| expect(chunks[2]).toEqual({ | ||
| type: "usage", | ||
| inputTokens: 10, | ||
| outputTokens: 20, | ||
| cacheWriteTokens: 5, | ||
| cacheReadTokens: 2, | ||
| totalCost: expect.any(Number), | ||
| }) | ||
|
|
||
| // Verify OpenAI client was called with correct parameters | ||
| expect(mockCreate).toHaveBeenCalledWith({ | ||
| max_tokens: 16384, | ||
| messages: [ | ||
| { | ||
| role: "system", | ||
| content: "test system prompt", | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: "test message", | ||
| }, | ||
| ], | ||
| model: "gpt-4o", | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| temperature: 0, | ||
| }) | ||
| }) | ||
|
|
||
| it("handles API errors", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| const mockError = new Error("API Error") | ||
| mockCreate.mockRejectedValue(mockError) | ||
|
|
||
| const generator = handler.createMessage("test", []) | ||
| await expect(generator.next()).rejects.toThrow("API Error") | ||
| }) | ||
| }) | ||
|
|
||
| describe("completePrompt", () => { | ||
| it("returns correct response", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| const mockResponse = { choices: [{ message: { content: "test completion" } }] } | ||
|
|
||
| mockCreate.mockResolvedValue(mockResponse) | ||
|
|
||
| const result = await handler.completePrompt("test prompt") | ||
|
|
||
| expect(result).toBe("test completion") | ||
|
|
||
| expect(mockCreate).toHaveBeenCalledWith({ | ||
| model: mockOptions.tarsModelId, | ||
| max_tokens: 16384, | ||
| messages: [{ role: "system", content: "test prompt" }], | ||
| temperature: 0, | ||
| }) | ||
| }) | ||
|
|
||
| it("handles API errors", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| const mockError = new Error("API Error") | ||
| mockCreate.mockRejectedValue(mockError) | ||
|
|
||
| await expect(handler.completePrompt("test prompt")).rejects.toThrow("API Error") | ||
| }) | ||
|
|
||
| it("handles unexpected errors", async () => { | ||
| const handler = new TarsHandler(mockOptions) | ||
| mockCreate.mockRejectedValue(new Error("Unexpected error")) | ||
|
|
||
| await expect(handler.completePrompt("test prompt")).rejects.toThrow("Unexpected error") | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 test coverage looks good! Consider adding a few more edge case tests:
These additional tests would help ensure robustness of the implementation.