Files
bim_engine/src/managers/ai-chat-manager.ts

200 lines
5.5 KiB
TypeScript
Raw Normal View History

/**
* AI
* AI
*/
import { AiChat } from '../components/ai-chat';
import type { Message, QuestionOption, StepStatus } from '../components/ai-chat/types';
import { ManagerRegistry } from '../core/manager-registry';
/**
* AI
* AI
*/
export class AiChatManager {
/** AI 聊天组件实例 */
private aiChat: AiChat | null = null;
/** Manager 注册表 */
private registry: ManagerRegistry;
/** 是否已初始化 */
private initialized = false;
constructor(registry: ManagerRegistry) {
this.registry = registry;
}
/**
* AI
* AiChat
*/
public init(): void {
if (this.initialized) return;
const wrapper = this.registry.wrapper;
if (!wrapper) {
console.warn('[AiChatManager] wrapper 不存在,无法初始化');
return;
}
this.aiChat = new AiChat({
container: wrapper,
width: 440,
title: 'aiChat.title',
placeholder: 'aiChat.placeholder',
quickPrompts: [
{ id: 'summarize', label: 'aiChat.quickPrompt.summarize' },
{ id: 'explain', label: 'aiChat.quickPrompt.explain' },
{ id: 'generate', label: 'aiChat.quickPrompt.generate' }
],
onSend: (message) => {
console.log('[AiChatManager] 用户发送消息:', message);
this.aiChat?.addUserMessage(message);
this.registry.emit('aiChat:message-sent', { message });
},
onQuestionSubmit: (questionId, optionId, customAnswer) => {
console.log('[AiChatManager] 用户回答问题:', { questionId, optionId, customAnswer });
this.registry.emit('aiChat:question-answered', { questionId, optionId, customAnswer });
},
onNewChat: () => {
console.log('[AiChatManager] 新建对话');
this.registry.emit('aiChat:new-chat', {});
},
onHistory: () => {
console.log('[AiChatManager] 打开历史');
this.registry.emit('aiChat:history-opened', {});
},
onSettings: () => {
console.log('[AiChatManager] 打开设置');
this.registry.emit('aiChat:settings-opened', {});
},
onClose: () => {
console.log('[AiChatManager] 关闭 AI 聊天');
this.registry.emit('aiChat:closed', {});
this.registry.toolbar?.setBtnActive('aiChat', false);
}
});
this.initialized = true;
}
/**
* AI
*/
public show(): void {
if (!this.aiChat) {
this.init();
}
this.aiChat?.show();
this.registry.emit('aiChat:opened', {});
this.registry.toolbar?.setBtnActive('aiChat', true);
}
/**
* AI
*/
public hide(): void {
this.aiChat?.hide();
this.registry.emit('aiChat:closed', {});
this.registry.toolbar?.setBtnActive('aiChat', false);
}
/**
* AI
*/
public toggle(): void {
if (this.aiChat?.isVisible()) {
this.hide();
} else {
this.show();
}
}
/**
* AI
*/
public isVisible(): boolean {
return this.aiChat?.isVisible() ?? false;
}
/**
*
* @param content
* @returns ID
*/
public addUserMessage(content: string): string {
return this.aiChat?.addUserMessage(content) ?? '';
}
/**
* AI
* @param content
* @returns ID
*/
public addAiMessage(content: string): string {
return this.aiChat?.addAiMessage(content) ?? '';
}
/**
*
* @param status
* @param content
* @returns ID
*/
public addStepMessage(status: StepStatus, content: string): string {
return this.aiChat?.addStepMessage(status, content) ?? '';
}
/**
*
* @returns ID
*/
public addThinkingMessage(): string {
return this.aiChat?.addThinkingMessage() ?? '';
}
/**
*
* @param title
* @param question
* @param options
* @returns ID
*/
public addQuestionMessage(title: string, question: string, options: QuestionOption[]): string {
return this.aiChat?.addQuestionMessage(title, question, options) ?? '';
}
/**
*
* @param id ID
* @param updates
*/
public updateMessage(id: string, updates: Partial<Message>): void {
this.aiChat?.updateMessage(id, updates);
}
/**
*
* @param id ID
*/
public removeMessage(id: string): void {
this.aiChat?.removeMessage(id);
}
/**
*
*/
public clearMessages(): void {
this.aiChat?.clearMessages();
}
/**
* AI
*/
public destroy(): void {
if (this.aiChat) {
this.aiChat.destroy();
this.aiChat = null;
}
this.initialized = false;
}
}