feat(context): 根据模型动态设置 contextLimit
- AgentConfig 添加 contextWindow 字段 - ProviderRegistry 添加 getModelInfo() 方法查询模型信息 - loadConfig() 从 ProviderRegistry 获取模型的 contextWindow - Agent 构造函数使用 contextWindow 初始化压缩管理器 效果: - Anthropic Claude: 168k (200k - 32k 输出预留) - DeepSeek: 32k (64k - 32k 输出预留) - 未知模型回退到默认 200k
This commit is contained in:
@@ -57,8 +57,16 @@ export class Agent {
|
|||||||
baseUrl: config.baseUrl,
|
baseUrl: config.baseUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 构建压缩配置,使用模型的 contextWindow(如果有)
|
||||||
|
const finalCompressionConfig: Partial<CompressionConfig> = {
|
||||||
|
...compressionConfig,
|
||||||
|
};
|
||||||
|
if (config.contextWindow) {
|
||||||
|
finalCompressionConfig.contextLimit = config.contextWindow;
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化压缩管理器
|
// 初始化压缩管理器
|
||||||
this.compressionManager = new CompressionManager(compressionConfig);
|
this.compressionManager = new CompressionManager(finalCompressionConfig);
|
||||||
// 设置主模型(作为摘要模型的后备)
|
// 设置主模型(作为摘要模型的后备)
|
||||||
this.compressionManager.setModel(this.getModel(config.model));
|
this.compressionManager.setModel(this.getModel(config.model));
|
||||||
|
|
||||||
|
|||||||
@@ -300,6 +300,17 @@ export class ProviderRegistry {
|
|||||||
return [...provider.info.models, ...customModels];
|
return [...provider.info.models, ...customModels];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定模型的详细信息
|
||||||
|
* @param providerId 提供商 ID
|
||||||
|
* @param modelId 模型 ID
|
||||||
|
* @returns 模型信息,未找到时返回 undefined
|
||||||
|
*/
|
||||||
|
getModelInfo(providerId: string, modelId: string): ModelInfo | undefined {
|
||||||
|
const models = this.getModels(providerId);
|
||||||
|
return models.find((m) => m.id === modelId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加自定义模型
|
* 添加自定义模型
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -78,6 +78,8 @@ export interface AgentConfig {
|
|||||||
systemPrompt: string;
|
systemPrompt: string;
|
||||||
/** 自定义 API 基础 URL(用于兼容 OpenAI API 的第三方服务,如阿里云百炼) */
|
/** 自定义 API 基础 URL(用于兼容 OpenAI API 的第三方服务,如阿里云百炼) */
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
|
/** 模型的上下文窗口大小(tokens),用于压缩管理 */
|
||||||
|
contextWindow?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 会话上下文
|
// 会话上下文
|
||||||
|
|||||||
@@ -121,6 +121,10 @@ export function loadConfig(): AgentConfig {
|
|||||||
// 确定 baseUrl
|
// 确定 baseUrl
|
||||||
const finalBaseUrl = storedConfig.baseUrl || providerConfig?.baseUrl;
|
const finalBaseUrl = storedConfig.baseUrl || providerConfig?.baseUrl;
|
||||||
|
|
||||||
|
// 获取模型的 contextWindow(从 ProviderRegistry 查询)
|
||||||
|
const modelInfo = providerRegistry.getModelInfo(finalProvider, finalModel);
|
||||||
|
const contextWindow = modelInfo?.contextWindow;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
provider: finalProvider,
|
provider: finalProvider,
|
||||||
apiKey: finalApiKey,
|
apiKey: finalApiKey,
|
||||||
@@ -128,6 +132,7 @@ export function loadConfig(): AgentConfig {
|
|||||||
maxTokens: storedConfig.maxTokens || 4096,
|
maxTokens: storedConfig.maxTokens || 4096,
|
||||||
systemPrompt: DEFAULT_SYSTEM_PROMPT,
|
systemPrompt: DEFAULT_SYSTEM_PROMPT,
|
||||||
baseUrl: finalBaseUrl,
|
baseUrl: finalBaseUrl,
|
||||||
|
contextWindow,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user