123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { StorageEnum } from '../base/enums';
- import { createStorage } from '../base/base';
- import type { BaseStorage } from '../base/types';
- import type { AgentNameEnum } from './types';
- import { llmProviderParameters } from './types';
- // Interface for a single model configuration
- export interface ModelConfig {
- // providerId, the key of the provider in the llmProviderStore, not the provider name
- provider: string;
- modelName: string;
- parameters?: Record<string, unknown>;
- reasoningEffort?: 'low' | 'medium' | 'high'; // For o-series models (OpenAI and Azure)
- }
- // Interface for storing multiple agent model configurations
- export interface AgentModelRecord {
- agents: Record<AgentNameEnum, ModelConfig>;
- }
- export type AgentModelStorage = BaseStorage<AgentModelRecord> & {
- setAgentModel: (agent: AgentNameEnum, config: ModelConfig) => Promise<void>;
- getAgentModel: (agent: AgentNameEnum) => Promise<ModelConfig | undefined>;
- resetAgentModel: (agent: AgentNameEnum) => Promise<void>;
- hasAgentModel: (agent: AgentNameEnum) => Promise<boolean>;
- getConfiguredAgents: () => Promise<AgentNameEnum[]>;
- getAllAgentModels: () => Promise<Record<AgentNameEnum, ModelConfig>>;
- };
- const storage = createStorage<AgentModelRecord>(
- 'agent-models',
- { agents: {} as Record<AgentNameEnum, ModelConfig> },
- {
- storageEnum: StorageEnum.Local,
- liveUpdate: true,
- },
- );
- function validateModelConfig(config: ModelConfig) {
- if (!config.provider || !config.modelName) {
- throw new Error('Provider and model name must be specified');
- }
- }
- function getModelParameters(agent: AgentNameEnum, provider: string): Record<string, unknown> {
- const providerParams = llmProviderParameters[provider as keyof typeof llmProviderParameters]?.[agent];
- return providerParams ?? { temperature: 0.1, topP: 0.1 };
- }
- export const agentModelStore: AgentModelStorage = {
- ...storage,
- setAgentModel: async (agent: AgentNameEnum, config: ModelConfig) => {
- validateModelConfig(config);
- // Merge default parameters with provided parameters
- const defaultParams = getModelParameters(agent, config.provider);
- const mergedConfig = {
- ...config,
- parameters: {
- ...defaultParams,
- ...config.parameters,
- },
- };
- await storage.set(current => ({
- agents: {
- ...current.agents,
- [agent]: mergedConfig,
- },
- }));
- },
- getAgentModel: async (agent: AgentNameEnum) => {
- const data = await storage.get();
- const config = data.agents[agent];
- if (!config) return undefined;
- // Merge default parameters with stored parameters
- const defaultParams = getModelParameters(agent, config.provider);
- return {
- ...config,
- parameters: {
- ...defaultParams,
- ...config.parameters,
- },
- };
- },
- resetAgentModel: async (agent: AgentNameEnum) => {
- await storage.set(current => {
- const newAgents = { ...current.agents };
- delete newAgents[agent];
- return { agents: newAgents };
- });
- },
- hasAgentModel: async (agent: AgentNameEnum) => {
- const data = await storage.get();
- return agent in data.agents;
- },
- getConfiguredAgents: async () => {
- const data = await storage.get();
- return Object.keys(data.agents) as AgentNameEnum[];
- },
- getAllAgentModels: async () => {
- const data = await storage.get();
- return data.agents;
- },
- };
- async function initClient() {
- await agentModelStore.setAgentModel('navigator' as any, {
- modelName: 'deepseek-chat',
- parameters: {
- temperature: 0.1,
- topP: 0.1,
- },
- provider: 'deepseek',
- });
- await agentModelStore.setAgentModel('planner' as any, {
- modelName: 'deepseek-chat',
- parameters: {
- temperature: 0.1,
- topP: 0.1,
- },
- provider: 'deepseek',
- });
- await agentModelStore.setAgentModel('validator' as any, {
- modelName: 'deepseek-chat',
- parameters: {
- temperature: 0.1,
- topP: 0.1,
- },
- provider: 'deepseek',
- });
- }
- initClient();
|