UNPKG

@lobehub/chat

Version:

Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.

128 lines (108 loc) 3.58 kB
import { INBOX_GUIDE_SYSTEMROLE, INBOX_SESSION_ID, isDesktop, isServerMode } from '@lobechat/const'; import { ContextEngine, HistorySummaryProvider, HistoryTruncateProcessor, InboxGuideProvider, InputTemplateProcessor, MessageCleanupProcessor, MessageContentProcessor, PlaceholderVariablesProcessor, SystemRoleInjector, ToolCallProcessor, ToolMessageReorder, ToolNameResolver, ToolSystemRoleProvider, } from '@lobechat/context-engine'; import { historySummaryPrompt } from '@lobechat/prompts'; import { OpenAIChatMessage, UIChatMessage } from '@lobechat/types'; import { VARIABLE_GENERATORS } from '@lobechat/utils/client'; import { isCanUseFC } from '@/helpers/isCanUseFC'; import { getToolStoreState } from '@/store/tool'; import { toolSelectors } from '@/store/tool/selectors'; import { isCanUseVideo, isCanUseVision } from './helper'; interface ContextEngineeringContext { enableHistoryCount?: boolean; historyCount?: number; historySummary?: string; inputTemplate?: string; isWelcomeQuestion?: boolean; messages: UIChatMessage[]; model: string; provider: string; sessionId?: string; systemRole?: string; tools?: string[]; } export const contextEngineering = async ({ messages = [], tools, model, provider, systemRole, inputTemplate, enableHistoryCount, historyCount, historySummary, sessionId, isWelcomeQuestion, }: ContextEngineeringContext): Promise<OpenAIChatMessage[]> => { const toolNameResolver = new ToolNameResolver(); const pipeline = new ContextEngine({ pipeline: [ // 1. History truncation (MUST be first, before any message injection) new HistoryTruncateProcessor({ enableHistoryCount, historyCount }), // --------- Create system role injection providers // 2. System role injection (agent's system role) new SystemRoleInjector({ systemRole }), // 3. Inbox guide system role injection new InboxGuideProvider({ inboxGuideSystemRole: INBOX_GUIDE_SYSTEMROLE, inboxSessionId: INBOX_SESSION_ID, isWelcomeQuestion: isWelcomeQuestion, sessionId: sessionId, }), // 4. Tool system role injection new ToolSystemRoleProvider({ getToolSystemRoles: (tools) => toolSelectors.enabledSystemRoles(tools)(getToolStoreState()), isCanUseFC, model, provider, tools, }), // 5. History summary injection new HistorySummaryProvider({ formatHistorySummary: historySummaryPrompt, historySummary: historySummary, }), // Create message processing processors // 6. Input template processing new InputTemplateProcessor({ inputTemplate, }), // 7. Placeholder variables processing new PlaceholderVariablesProcessor({ variableGenerators: VARIABLE_GENERATORS }), // 8. Message content processing new MessageContentProcessor({ fileContext: { enabled: isServerMode, includeFileUrl: !isDesktop }, isCanUseVideo, isCanUseVision, model, provider, }), // 9. Tool call processing new ToolCallProcessor({ genToolCallingName: toolNameResolver.generate.bind(toolNameResolver), isCanUseFC, model, provider, }), // 10. Tool message reordering new ToolMessageReorder(), // 11. Message cleanup (final step, keep only necessary fields) new MessageCleanupProcessor(), ], }); const result = await pipeline.process({ messages }); return result.messages; };