-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: Add n1n.ai as a model provider #8659
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,20 @@ | ||
import type { ModelInfo } from "../model.js" | ||
|
||
// n1n.ai is an OpenAI-compatible API that provides access to 400+ models | ||
// Since they have a large and dynamic model list, we'll fetch models dynamically | ||
export type N1nModelId = string | ||
|
||
export const n1nDefaultModelId = "gpt-4o-mini" | ||
|
||
// Default model info for when dynamic fetching isn't available | ||
export const n1nDefaultModelInfo: ModelInfo = { | ||
maxTokens: 16_384, | ||
contextWindow: 128_000, | ||
supportsImages: true, | ||
supportsPromptCache: false, | ||
inputPrice: 0.15, | ||
outputPrice: 0.6, | ||
} | ||
|
||
// Base URL for n1n.ai API | ||
export const N1N_BASE_URL = "https://n1n.ai/v1" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import axios from "axios" | ||
import { z } from "zod" | ||
|
||
import { type ModelInfo, N1N_BASE_URL } from "@roo-code/types" | ||
|
||
import { DEFAULT_HEADERS } from "../constants" | ||
|
||
// n1n models endpoint follows OpenAI /models shape | ||
const N1nModelSchema = z.object({ | ||
id: z.string(), | ||
object: z.literal("model").optional(), | ||
owned_by: z.string().optional(), | ||
created: z.number().optional(), | ||
}) | ||
|
||
const N1nModelsResponseSchema = z.object({ | ||
data: z.array(N1nModelSchema).optional(), | ||
object: z.string().optional(), | ||
}) | ||
|
||
export async function getN1nModels(apiKey: string): Promise<Record<string, ModelInfo>> { | ||
const headers: Record<string, string> = { | ||
...DEFAULT_HEADERS, | ||
Authorization: `Bearer ${apiKey}`, | ||
} | ||
|
||
const url = `${N1N_BASE_URL}/models` | ||
const models: Record<string, ModelInfo> = {} | ||
|
||
try { | ||
const response = await axios.get(url, { headers }) | ||
const parsed = N1nModelsResponseSchema.safeParse(response.data) | ||
const data = parsed.success ? parsed.data.data || [] : response.data?.data || [] | ||
|
||
for (const m of data as Array<z.infer<typeof N1nModelSchema>>) { | ||
// Default model info - n1n doesn't provide detailed metadata in /models endpoint | ||
// These are conservative defaults that should work for most models | ||
const info: ModelInfo = { | ||
maxTokens: 4096, | ||
contextWindow: 16384, | ||
supportsImages: false, // Will be true for vision models like gpt-4-vision | ||
supportsPromptCache: false, | ||
// n1n doesn't expose pricing via API, would need to be hardcoded or fetched separately | ||
inputPrice: undefined, | ||
outputPrice: undefined, | ||
} | ||
|
||
// Check for known vision model patterns | ||
if (m.id.includes("vision") || m.id.includes("gpt-4o") || m.id.includes("claude-3")) { | ||
info.supportsImages = true | ||
} | ||
|
||
// Check for known models with larger contexts | ||
if (m.id.includes("gpt-4-turbo") || m.id.includes("claude-3") || m.id.includes("gpt-4o")) { | ||
info.contextWindow = 128000 | ||
info.maxTokens = 4096 | ||
} else if (m.id.includes("claude-2")) { | ||
info.contextWindow = 100000 | ||
info.maxTokens = 4096 | ||
} else if (m.id.includes("gpt-3.5-turbo-16k")) { | ||
info.contextWindow = 16384 | ||
info.maxTokens = 4096 | ||
} | ||
|
||
models[m.id] = info | ||
} | ||
|
||
return models | ||
} catch (error) { | ||
console.error("Error fetching n1n models:", error) | ||
// Return empty object on error - the handler will use default model | ||
return {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { n1nDefaultModelId, n1nDefaultModelInfo, N1N_BASE_URL } from "@roo-code/types" | ||
|
||
import type { ApiHandlerOptions } from "../../shared/api" | ||
|
||
import { getModelParams } from "../transform/model-params" | ||
|
||
import { OpenAiHandler } from "./openai" | ||
|
||
export class N1nHandler extends OpenAiHandler { | ||
constructor(options: ApiHandlerOptions) { | ||
super({ | ||
...options, | ||
openAiApiKey: options.n1nApiKey ?? "", | ||
openAiModelId: options.n1nModelId ?? n1nDefaultModelId, | ||
openAiBaseUrl: N1N_BASE_URL, | ||
openAiStreamingEnabled: true, | ||
}) | ||
} | ||
|
||
override getModel() { | ||
const id = this.options.n1nModelId ?? n1nDefaultModelId | ||
// Since n1n.ai supports 400+ models dynamically, we use default model info | ||
// unless we implement dynamic model fetching | ||
const info = n1nDefaultModelInfo | ||
const params = getModelParams({ format: "openai", modelId: id, model: info, settings: this.options }) | ||
return { id, info, ...params } | ||
Comment on lines
+20
to
+26
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 The fetcher in const info = this.models[id] ?? deepInfraDefaultModelInfo N1nHandler should follow the same pattern - store fetched models and use them in |
||
} | ||
} |
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.
Consider logging additional details from the Zod safeParse result when parsing fails (e.g. logging parsed.error) to aid in debugging schema mismatches.
This comment was generated because it violated a code review rule: irule_PTI8rjtnhwrWq6jS.