allow provider / model config

This commit is contained in:
Ramnique Singh 2025-11-14 09:13:28 +05:30
parent 62caa0c8b6
commit 6251c8f007
9 changed files with 140 additions and 12 deletions

View file

@ -1,14 +1,13 @@
import { Message, MessageList } from "../entities/message.js";
import { z } from "zod";
import { Step, StepInputT, StepOutputT } from "./step.js";
import { openai } from "@ai-sdk/openai";
import { google } from "@ai-sdk/google";
import { generateText, ModelMessage, stepCountIs, streamText, tool, Tool, ToolSet, jsonSchema } from "ai";
import { ModelMessage, stepCountIs, streamText, tool, Tool, ToolSet, jsonSchema } from "ai";
import { Agent, AgentTool } from "../entities/agent.js";
import { WorkDir } from "../config/config.js";
import { DefaultModel, WorkDir } from "../config/config.js";
import fs from "fs";
import path from "path";
import { loadWorkflow } from "./utils.js";
import { getProvider } from "./models.js";
const BashTool = tool({
description: "Run a command in the shell",
@ -157,9 +156,9 @@ export class AgentNode implements Step {
// console.log("\n\n\t>>>>\t\ttools", JSON.stringify(tools, null, 2));
const provider = getProvider(this.agent.provider);
const { fullStream } = streamText({
model: openai("gpt-4.1"),
// model: google("gemini-2.5-flash"),
model: provider(this.agent.model || DefaultModel),
messages: convertFromMessages(input),
system: this.agent.instructions,
stopWhen: stepCountIs(1),

View file

@ -0,0 +1,40 @@
import { createOpenAI, OpenAIProvider } from "@ai-sdk/openai";
import { createGoogleGenerativeAI, GoogleGenerativeAIProvider } from "@ai-sdk/google";
import { AnthropicProvider, createAnthropic } from "@ai-sdk/anthropic";
import { DefaultModel, DefaultProvider, Providers } from "../config/config.js";
const providerMap: Record<string, OpenAIProvider | GoogleGenerativeAIProvider | AnthropicProvider> = {};
export function getProvider(name: string = "") {
if (!name) {
name = DefaultProvider;
}
if (providerMap[name]) {
return providerMap[name];
}
const providerConfig = Providers[name];
if (!providerConfig) {
throw new Error(`Provider ${name} not found`);
}
switch (providerConfig.flavor) {
case "openai":
providerMap[name] = createOpenAI({
apiKey: providerConfig.apiKey,
baseURL: providerConfig.baseURL,
});
break;
case "anthropic":
providerMap[name] = createAnthropic({
apiKey: providerConfig.apiKey,
baseURL: providerConfig.baseURL,
});
break;
case "google":
providerMap[name] = createGoogleGenerativeAI({
apiKey: providerConfig.apiKey,
baseURL: providerConfig.baseURL,
});
break;
}
return providerMap[name];
}