📄 chat-factory.ts • 1952 bytes
/**
* CmdCode V0.5 - 聊天引擎工厂
* 根据配置自动选择 EnhancedChatEngine 或 ChatEngine
*/
import { ChatEngine, type Message } from './chat.js'
import { EnhancedChatEngine, setPAVREnabled, type Message as PAVRMessage } from './chat-enhanced.js'
import type { Config } from './config.js'
export { setPAVREnabled, isPAVREnabled } from './chat-enhanced.js'
/** 全局开关 */
let globalPAVREnabled = true
export function setGlobalPAVREnabled(enabled: boolean): void {
globalPAVREnabled = enabled
setPAVREnabled(enabled)
}
export function isGlobalPAVREnabled(): boolean {
return globalPAVREnabled
}
/**
* 创建聊天引擎(兼容 API)
*
* @param usePAVR - 是否使用 PAVR 循环(默认使用全局设置)
* @param config - 配置
* @param systemPrompt - 系统提示词
* @param workspaceDir - 工作目录
*/
export function createChatEngine(
usePAVR?: boolean,
config?: Config,
systemPrompt?: string,
workspaceDir?: string
): ChatEngine | EnhancedChatEngine {
const shouldUsePAVR = usePAVR ?? globalPAVREnabled
if (shouldUsePAVR) {
setPAVREnabled(true)
return new EnhancedChatEngine(config, systemPrompt, workspaceDir)
} else {
setPAVREnabled(false)
return new ChatEngine(config, systemPrompt, workspaceDir)
}
}
/**
* 从历史记录创建聊天引擎(兼容 API)
*/
export function createChatEngineFromHistory(
messages: Message[],
usePAVR?: boolean,
config?: Config,
workspaceDir?: string
): ChatEngine | EnhancedChatEngine {
const shouldUsePAVR = usePAVR ?? globalPAVREnabled
if (shouldUsePAVR) {
setPAVREnabled(true)
return EnhancedChatEngine.fromHistory(messages, config, workspaceDir)
} else {
setPAVREnabled(false)
return ChatEngine.fromHistory(messages, config, workspaceDir)
}
}
// 导出类型
export type { Message } from './chat.js'
export type { PAVRMessage as EnhancedMessage, PAVRState } from './chat-enhanced.js'