|
1 | 1 | import { AIClient, type AIModelConfig } from '@opentiny/tiny-robot-kit' |
2 | 2 | import { OpenAICompatibleProvider } from './OpenAICompatibleProvider' |
3 | | -import useMcp from '../composables/useMcp' |
4 | | -import useRobot from '../js/useRobot' |
5 | | -import type { LLMMessage } from '../types/mcp-types' |
6 | | -import { getAgentSystemPrompt } from '../js/prompts' |
7 | | -import { utils } from '@opentiny/tiny-engine-utils' |
8 | | -import { getMetaApi, META_SERVICE, useCanvas } from '@opentiny/tiny-engine-meta-register' |
9 | | - |
10 | | -const { deepClone } = utils |
11 | | -const { loadRobotSettingState, EXISTING_MODELS, aiMode, CHAT_MODE } = useRobot() |
12 | | -const { activeName, existModel, customizeModel } = loadRobotSettingState() || {} |
13 | | - |
14 | | -const storageSettingState = (activeName === EXISTING_MODELS ? existModel : customizeModel) || {} |
15 | | - |
16 | | -const config: Omit<AIModelConfig, 'provider' | 'providerImplementation'> = { |
17 | | - apiKey: storageSettingState.apiKey || '', |
18 | | - apiUrl: aiMode.value === CHAT_MODE.Agent ? '/app-center/api/ai/chat' : '/app-center/api/chat/completions', |
19 | | - defaultModel: storageSettingState.model || 'deepseek-v3' |
20 | | -} |
21 | | - |
22 | | -let provider: OpenAICompatibleProvider | null = null |
23 | | - |
24 | | -const addSystemPrompt = (messages: LLMMessage[], prompt: string = '') => { |
25 | | - if (!messages.length || messages[0].role !== 'system') { |
26 | | - messages.unshift({ role: 'system', content: prompt }) |
27 | | - } else if (messages[0].role === 'system' && messages[0].content !== prompt) { |
28 | | - messages[0].content = prompt |
29 | | - } |
30 | | -} |
31 | | - |
32 | | -export const search = async (content: string) => { |
33 | | - let result = '' |
34 | | - const MAX_SEARCH_LENGTH = 8000 |
35 | | - try { |
36 | | - const res = await getMetaApi(META_SERVICE.Http).post('/app-center/api/ai/search', { content }) |
37 | | - |
38 | | - res.forEach((item: { content: string }) => { |
39 | | - if (result.length + item.content.length > MAX_SEARCH_LENGTH) { |
40 | | - return |
41 | | - } |
42 | | - result += item.content |
43 | | - }) |
44 | | - } catch (error) { |
45 | | - // error |
46 | | - } |
47 | | - return result |
48 | | -} |
49 | | - |
50 | | -const beforeRequest = async (requestParams: any) => { |
51 | | - const { aiMode, CHAT_MODE, robotSettingState } = useRobot() |
52 | | - const pageSchema = deepClone(useCanvas().pageState.pageSchema) |
53 | | - const isAgentMode = aiMode.value === CHAT_MODE.Agent |
54 | | - const tools = await useMcp().getLLMTools() |
55 | | - if (!requestParams.tools && tools?.length && !isAgentMode) { |
56 | | - Object.assign(requestParams, { tools }) |
57 | | - } |
58 | | - if (isAgentMode) { |
59 | | - requestParams.apiKey = robotSettingState.selectedModel.apiKey |
60 | | - // let referenceContext = '' |
61 | | - // if (requestParams.messages?.[0].role && requestParams.messages?.[0].role !== 'system') { |
62 | | - // referenceContext = await search(requestParams.messages?.at(-1)?.content) |
63 | | - // } |
64 | | - addSystemPrompt(requestParams.messages, getAgentSystemPrompt(pageSchema, '')) |
65 | | - } |
66 | | - requestParams.baseUrl = robotSettingState.selectedModel.baseUrl |
67 | | - requestParams.model = robotSettingState.selectedModel.model |
68 | | - if (config.apiKey !== robotSettingState.selectedModel.apiKey) { |
69 | | - provider?.updateConfig({ apiKey: robotSettingState.selectedModel.apiKey }) |
70 | | - config.apiKey = robotSettingState.selectedModel.apiKey |
71 | | - } |
72 | | - return requestParams |
| 3 | +interface ClientOptions { |
| 4 | + config: Omit<AIModelConfig, 'provider' | 'providerImplementation'> |
| 5 | + beforeRequest: () => object |
73 | 6 | } |
74 | 7 |
|
75 | | -provider = new OpenAICompatibleProvider(config, { beforeRequest }) |
| 8 | +const createClient = ({ config, beforeRequest }: ClientOptions) => { |
| 9 | + const provider: OpenAICompatibleProvider = new OpenAICompatibleProvider(config, { beforeRequest }) |
76 | 10 |
|
77 | | -const client = new AIClient({ |
78 | | - ...config, |
79 | | - provider: 'custom', |
80 | | - providerImplementation: provider |
81 | | -}) |
| 11 | + const client = new AIClient({ |
| 12 | + ...config, |
| 13 | + provider: 'custom', |
| 14 | + providerImplementation: provider |
| 15 | + }) |
82 | 16 |
|
83 | | -const updateLLMConfig = (newConfig: Omit<AIModelConfig, 'provider' | 'providerImplementation'>) => { |
84 | | - provider?.updateConfig(newConfig) |
85 | | - Object.assign(config, newConfig) |
| 17 | + return { client, provider } |
86 | 18 | } |
87 | 19 |
|
88 | | -export { client, updateLLMConfig } |
| 20 | +export { createClient } |
0 commit comments