-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add OrcaRouter as a named model catalog source #190
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
Open
kuswardhanietidims-svg
wants to merge
23
commits into
trycompai:main
Choose a base branch
from
kuswardhanietidims-svg:feat/orcarouter-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
52a57ed
Merge pull request #74 from trycompai/main
carhartlewis 1561073
chore: release release
github-actions[bot] 5074fc4
Merge pull request #75 from trycompai/release-please--branches--release
carhartlewis 3c20d80
Merge pull request #77 from trycompai/main
carhartlewis 808b835
Merge pull request #79 from trycompai/main
carhartlewis 407280a
Merge pull request #81 from trycompai/main
carhartlewis c26a08d
Merge pull request #84 from trycompai/main
carhartlewis d585dc3
Merge pull request #90 from trycompai/main
carhartlewis d0299d9
Merge pull request #98 from trycompai/main
carhartlewis 7d4a573
Merge pull request #107 from trycompai/main
carhartlewis 56f4eeb
Merge pull request #116 from trycompai/main
github-actions[bot] 57001e6
Merge pull request #119 from trycompai/main
github-actions[bot] ad1d702
Merge pull request #122 from trycompai/main
github-actions[bot] fc0c594
Merge pull request #127 from trycompai/main
github-actions[bot] 4ffe150
Merge pull request #130 from trycompai/main
github-actions[bot] 14cd220
Merge pull request #135 from trycompai/main
github-actions[bot] f2484fb
Merge pull request #141 from trycompai/main
github-actions[bot] bb63520
Merge pull request #161 from trycompai/main
github-actions[bot] 517d859
Merge pull request #165 from trycompai/main
github-actions[bot] b842bd6
Merge pull request #168 from trycompai/main
github-actions[bot] 77089e4
Merge pull request #172 from trycompai/main
github-actions[bot] 6d4793d
Merge pull request #177 from trycompai/main
github-actions[bot] 0e4dbbe
feat: add OrcaRouter as a named model catalog provider
kuswardhanietidims-svg 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 |
|---|---|---|
|
|
@@ -3,14 +3,22 @@ import { Inject, Injectable, Logger } from "@nestjs/common"; | |
| import type { Cache } from "cache-manager"; | ||
| import { z } from "zod"; | ||
|
|
||
| const CATALOG_URL = "https://ai-gateway.vercel.sh/v1/models"; | ||
| const VERCEL_CATALOG_URL = "https://ai-gateway.vercel.sh/v1/models"; | ||
|
|
||
| const ORCAROUTER_CATALOG_URL = "https://api.orcarouter.ai/v1/models"; | ||
|
|
||
| const ORCAROUTER_API_KEY_ENV = "ORCAROUTER_API_KEY"; | ||
|
|
||
| const CATALOG_TTL_MS = 30 * 60_000; | ||
|
|
||
| const CATALOG_KEY = "settings:model-catalog"; | ||
|
|
||
| const CATALOG_TIMEOUT_MS = 5_000; | ||
|
|
||
| const DEFAULT_ORCAROUTER_CONTEXT_WINDOW_TOKENS = 128_000; | ||
|
|
||
| export type ModelCatalogSource = "vercel" | "orcarouter"; | ||
|
|
||
| export interface CatalogModel { | ||
| id: string; | ||
| name: string; | ||
|
|
@@ -45,6 +53,21 @@ const gatewayCatalog = z | |
| .object({ data: z.array(z.json()).catch([]) }) | ||
| .catch({ data: [] }); | ||
|
|
||
| const orcaModel = z.object({ | ||
| id: z.string(), | ||
| name: z.string().catch(""), | ||
| owned_by: z.string().catch(""), | ||
| context_length: z.number().nullable().catch(null), | ||
| pricing: z | ||
| .object({ prompt: gatewayRate, completion: gatewayRate }) | ||
| .nullable() | ||
| .catch(null), | ||
| }); | ||
|
|
||
| const orcaCatalog = z | ||
| .object({ data: z.array(z.json()).catch([]) }) | ||
| .catch({ data: [] }); | ||
|
|
||
| function usable(model: GatewayModel): boolean { | ||
| return model.type === "language" && model.tags.includes("tool-use"); | ||
| } | ||
|
|
@@ -62,20 +85,84 @@ function toCatalogModel(model: GatewayModel): CatalogModel { | |
| }; | ||
| } | ||
|
|
||
| function parseVercelCatalog(body: unknown): CatalogModel[] { | ||
| const parsed = gatewayCatalog.safeParse(body); | ||
| const models: CatalogModel[] = []; | ||
|
|
||
| if (parsed.success) { | ||
| for (const entry of parsed.data.data) { | ||
| const model = gatewayModel.safeParse(entry); | ||
| if (model.success && usable(model.data)) { | ||
| models.push(toCatalogModel(model.data)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| models.sort( | ||
| (a, b) => | ||
| a.provider.localeCompare(b.provider) || a.name.localeCompare(b.name), | ||
| ); | ||
|
|
||
| return models; | ||
| } | ||
|
|
||
| function parseOrcaCatalog(body: unknown): CatalogModel[] { | ||
| const parsed = orcaCatalog.safeParse(body); | ||
| const models: CatalogModel[] = []; | ||
|
|
||
| if (parsed.success) { | ||
| for (const entry of parsed.data.data) { | ||
| const model = orcaModel.safeParse(entry); | ||
| if (!model.success) continue; | ||
|
|
||
| const input = model.data.pricing?.prompt ?? null; | ||
| const output = model.data.pricing?.completion ?? null; | ||
|
|
||
| models.push({ | ||
|
Contributor
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. P1: When OrcaRouter is selected, this adds every Prompt for AI agents |
||
| id: model.data.id, | ||
| name: model.data.name || model.data.id, | ||
| provider: | ||
| model.data.owned_by || (model.data.id.split("/")[0] ?? model.data.id), | ||
| contextWindowTokens: | ||
| model.data.context_length ?? DEFAULT_ORCAROUTER_CONTEXT_WINDOW_TOKENS, | ||
| pricing: input !== null && output !== null ? { input, output } : null, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| models.sort( | ||
| (a, b) => | ||
| a.provider.localeCompare(b.provider) || a.name.localeCompare(b.name), | ||
| ); | ||
|
|
||
| return models; | ||
| } | ||
|
|
||
| export function catalogSource(): ModelCatalogSource { | ||
| return process.env[ORCAROUTER_API_KEY_ENV]?.trim() ? "orcarouter" : "vercel"; | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class ModelCatalogService { | ||
| private readonly logger = new Logger(ModelCatalogService.name); | ||
|
|
||
| constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {} | ||
|
|
||
| source(): ModelCatalogSource { | ||
| return catalogSource(); | ||
| } | ||
|
|
||
| async models(): Promise<CatalogModel[] | null> { | ||
| const cached = await this.cache.get<CatalogModel[]>(CATALOG_KEY); | ||
| const source = catalogSource(); | ||
| const cacheKey = `${CATALOG_KEY}:${source}`; | ||
|
|
||
| const cached = await this.cache.get<CatalogModel[]>(cacheKey); | ||
| if (cached) return cached; | ||
|
|
||
| const models = await this.fetchCatalog(); | ||
| if (!models) return null; | ||
|
|
||
| await this.cache.set(CATALOG_KEY, models, CATALOG_TTL_MS); | ||
| await this.cache.set(cacheKey, models, CATALOG_TTL_MS); | ||
| return models; | ||
| } | ||
|
|
||
|
|
@@ -85,43 +172,48 @@ export class ModelCatalogService { | |
| } | ||
|
|
||
| private async fetchCatalog(): Promise<CatalogModel[] | null> { | ||
| const source = catalogSource(); | ||
| const url = | ||
| source === "orcarouter" ? ORCAROUTER_CATALOG_URL : VERCEL_CATALOG_URL; | ||
|
|
||
| try { | ||
| const response = await fetch(CATALOG_URL, { | ||
| headers: { accept: "application/json" }, | ||
| const headers: Record<string, string> = { accept: "application/json" }; | ||
|
|
||
| if (source === "orcarouter") { | ||
| const apiKey = process.env[ORCAROUTER_API_KEY_ENV]?.trim(); | ||
| if (apiKey) headers.authorization = `Bearer ${apiKey}`; | ||
| } | ||
|
|
||
| const response = await fetch(url, { | ||
| headers, | ||
| signal: AbortSignal.timeout(CATALOG_TIMEOUT_MS), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| this.logger.warn({ | ||
| message: "Model catalog request failed", | ||
| source, | ||
| status: response.status, | ||
| }); | ||
| return null; | ||
| } | ||
|
|
||
| const body = gatewayCatalog.parse(await response.json()); | ||
|
|
||
| const models = body.data.flatMap((entry) => { | ||
| const parsed = gatewayModel.safeParse(entry); | ||
| return parsed.success && usable(parsed.data) | ||
| ? [toCatalogModel(parsed.data)] | ||
| : []; | ||
| }); | ||
|
|
||
| models.sort( | ||
| (a, b) => | ||
| a.provider.localeCompare(b.provider) || a.name.localeCompare(b.name), | ||
| ); | ||
| const models = | ||
| source === "orcarouter" | ||
| ? parseOrcaCatalog(await response.json()) | ||
| : parseVercelCatalog(await response.json()); | ||
|
|
||
| this.logger.log({ | ||
| message: "Model catalog loaded", | ||
| source, | ||
| models: models.length, | ||
| }); | ||
|
|
||
| return models; | ||
| } catch (error) { | ||
| this.logger.warn({ | ||
| message: "Model catalog unavailable", | ||
| source, | ||
| reason: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| return null; | ||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P3: This tunable default is defined at the top of the service file, but AGENTS.md "Constants in One File per Area" requires tunable numbers to live in a named config module for their area, never at the top of the file that first needed them. Move the 128_000 default into the settings area config and import it.
Prompt for AI agents