feat(core): 将 Tavily API Key 从环境变量迁移到系统配置
- 新增 ServiceConfig 类型和 services 配置字段 - 添加 getServiceApiKey/saveServiceConfig/deleteServiceConfig API - 更新 web_search 和 web_extract 工具使用配置系统 - 新增 /api/services REST 端点管理第三方服务配置
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import type { ProvidersConfigFile, CustomProviderDefinition, ProviderConfig } from './types.js';
|
||||
import type { ProvidersConfigFile, CustomProviderDefinition, ProviderConfig, ServiceConfig, ServiceType } from './types.js';
|
||||
import { getConfigDir as getGlobalConfigDir } from '../constants/paths.js';
|
||||
|
||||
/** 配置文件名 */
|
||||
@@ -28,7 +28,7 @@ export async function loadProvidersConfig(): Promise<ProvidersConfigFile> {
|
||||
const configPath = getConfigPath();
|
||||
|
||||
if (!existsSync(configPath)) {
|
||||
return { providers: {}, configs: {} };
|
||||
return { providers: {}, configs: {}, services: {} };
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -37,10 +37,11 @@ export async function loadProvidersConfig(): Promise<ProvidersConfigFile> {
|
||||
return {
|
||||
providers: config.providers ?? {},
|
||||
configs: config.configs ?? {},
|
||||
services: config.services ?? {},
|
||||
};
|
||||
} catch {
|
||||
// 解析失败返回空配置
|
||||
return { providers: {}, configs: {} };
|
||||
return { providers: {}, configs: {}, services: {} };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,3 +84,43 @@ export function mergeProviderConfig(
|
||||
enabled: config?.enabled ?? true,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务配置
|
||||
*/
|
||||
export async function getServiceConfig(serviceId: ServiceType): Promise<ServiceConfig | undefined> {
|
||||
const config = await loadProvidersConfig();
|
||||
return config.services?.[serviceId];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务 API Key
|
||||
*/
|
||||
export async function getServiceApiKey(serviceId: ServiceType): Promise<string | undefined> {
|
||||
const serviceConfig = await getServiceConfig(serviceId);
|
||||
if (serviceConfig?.enabled === false) {
|
||||
return undefined;
|
||||
}
|
||||
return serviceConfig?.apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存服务配置
|
||||
*/
|
||||
export async function saveServiceConfig(serviceId: ServiceType, serviceConfig: Omit<ServiceConfig, 'id'>): Promise<void> {
|
||||
const config = await loadProvidersConfig();
|
||||
config.services = config.services ?? {};
|
||||
config.services[serviceId] = { ...serviceConfig, id: serviceId };
|
||||
await saveProvidersConfig(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除服务配置
|
||||
*/
|
||||
export async function deleteServiceConfig(serviceId: ServiceType): Promise<void> {
|
||||
const config = await loadProvidersConfig();
|
||||
if (config.services) {
|
||||
delete config.services[serviceId];
|
||||
await saveProvidersConfig(config);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user