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.

101 lines (86 loc) 3.42 kB
/** * Tools Engineering - Unified tools processing using ToolsEngine */ import { ToolsEngine } from '@lobechat/context-engine'; import type { PluginEnableChecker } from '@lobechat/context-engine'; import { ChatCompletionTool, WorkingModel } from '@lobechat/types'; import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk'; import { getToolStoreState } from '@/store/tool'; import { pluginSelectors } from '@/store/tool/selectors'; import { WebBrowsingManifest } from '@/tools/web-browsing'; import { getSearchConfig } from '../getSearchConfig'; import { isCanUseFC } from '../isCanUseFC'; import { shouldEnableTool } from '../toolFilters'; /** * Tools engine configuration options */ export interface ToolsEngineConfig { /** Additional manifests to include beyond the standard ones */ additionalManifests?: LobeChatPluginManifest[]; /** Default tool IDs that will always be added to the end of the tools list */ defaultToolIds?: string[]; /** Custom enable checker for plugins */ enableChecker?: PluginEnableChecker; } /** * Initialize ToolsEngine with current manifest schemas and configurable options */ export const createToolsEngine = (config: ToolsEngineConfig = {}): ToolsEngine => { const { enableChecker, additionalManifests = [], defaultToolIds } = config; const toolStoreState = getToolStoreState(); // Get all available plugin manifests const pluginManifests = pluginSelectors.installedPluginManifestList(toolStoreState); // Get all builtin tool manifests const builtinManifests = toolStoreState.builtinTools.map( (tool) => tool.manifest as LobeChatPluginManifest, ); // Combine all manifests const allManifests = [...pluginManifests, ...builtinManifests, ...additionalManifests]; return new ToolsEngine({ defaultToolIds, enableChecker, functionCallChecker: isCanUseFC, manifestSchemas: allManifests, }); }; export const createChatToolsEngine = (workingModel: WorkingModel) => createToolsEngine({ // Add WebBrowsingManifest as default tool defaultToolIds: [WebBrowsingManifest.identifier], // Create search-aware enableChecker for this request enableChecker: ({ pluginId }) => { // Check platform-specific constraints (e.g., LocalSystem desktop-only) if (!shouldEnableTool(pluginId)) { return false; } // For WebBrowsingManifest, apply search logic if (pluginId === WebBrowsingManifest.identifier) { const searchConfig = getSearchConfig(workingModel.model, workingModel.provider); return searchConfig.useApplicationBuiltinSearchTool; } // For all other plugins, enable by default return true; }, }); /** * Provides the same functionality using ToolsEngine with enhanced capabilities * * @param toolIds - Array of tool IDs to generate tools for * @param model - Model name for function calling compatibility check (optional) * @param provider - Provider name for function calling compatibility check (optional) * @returns Array of ChatCompletionTool objects */ export const getEnabledTools = ( toolIds: string[] = [], model: string, provider: string, ): ChatCompletionTool[] => { const toolsEngine = createToolsEngine(); return ( toolsEngine.generateTools({ model: model, // Use provided model or fallback provider: provider, // Use provided provider or fallback toolIds, }) || [] ); };