diff --git a/renderer/src/components/main-panel/chat/task-loop.ts b/renderer/src/components/main-panel/chat/task-loop.ts index 7a5a6b8..f3125be 100644 --- a/renderer/src/components/main-panel/chat/task-loop.ts +++ b/renderer/src/components/main-panel/chat/task-loop.ts @@ -10,7 +10,12 @@ export class TaskLoop { private readonly streamingToolCalls: Ref, private readonly messages: ChatMessage[], private readonly onError: (msg: string) => void - ) {} + ) { + if (!onError) { + this.onError = (msg) => { + } + } + } private handleToolCalls(toolCalls: ToolCall[]) { // 这里预留给调用方实现工具执行逻辑 @@ -22,6 +27,7 @@ export class TaskLoop { const chunkHandler = this.bridge.addCommandListener('llm/chat/completions/chunk', data => { if (data.code !== 200) { this.onError(data.msg || '请求模型服务时发生错误'); + reject(new Error(data.msg)); return; } @@ -113,4 +119,10 @@ export class TaskLoop { this.onError(error instanceof Error ? error.message : '未知错误'); } } + + + // bridge api + public async name() { + + } } \ No newline at end of file diff --git a/renderer/src/components/main-panel/tool/tool-executor.vue b/renderer/src/components/main-panel/tool/tool-executor.vue index a2c6931..ce75e8b 100644 --- a/renderer/src/components/main-panel/tool/tool-executor.vue +++ b/renderer/src/components/main-panel/tool/tool-executor.vue @@ -51,7 +51,7 @@ import { defineComponent, defineProps, watch, ref, computed } from 'vue'; import { useI18n } from 'vue-i18n'; import type { FormInstance, FormRules } from 'element-plus'; import { tabs } from '../panel'; -import { toolsManager, ToolStorage } from './tools'; +import { callTool, toolsManager, ToolStorage } from './tools'; import { CasualRestAPI, ToolCallResponse } from '@/hook/type'; import { useMessageBridge } from '@/api/message-bridge'; @@ -111,24 +111,11 @@ const resetForm = () => { tabStorage.lastToolCallResponse = undefined; }; -function handleExecute() { +async function handleExecute() { if (!currentTool.value) return; - - const bridge = useMessageBridge(); - bridge.addCommandListener('tools/call', (data: CasualRestAPI) => { - console.log(data.msg); - - tabStorage.lastToolCallResponse = data.msg; - }, { once: true }); - - bridge.postMessage({ - command: 'tools/call', - data: { - toolName: tabStorage.currentToolName, - toolArgs: formData.value - } - }); + const toolResponse = await callTool(tabStorage.currentToolName, formData.value); + tabStorage.lastToolCallResponse = toolResponse; } watch(() => tabStorage.currentToolName, () => { diff --git a/renderer/src/components/main-panel/tool/tools.ts b/renderer/src/components/main-panel/tool/tools.ts index d16bf07..f42c24e 100644 --- a/renderer/src/components/main-panel/tool/tools.ts +++ b/renderer/src/components/main-panel/tool/tools.ts @@ -1,4 +1,5 @@ -import { ToolsListResponse, ToolCallResponse } from '@/hook/type'; +import { useMessageBridge } from '@/api/message-bridge'; +import { ToolsListResponse, ToolCallResponse, CasualRestAPI } from '@/hook/type'; import { reactive } from 'vue'; export const toolsManager = reactive<{ @@ -10,4 +11,25 @@ export const toolsManager = reactive<{ export interface ToolStorage { currentToolName: string; lastToolCallResponse?: ToolCallResponse; +} + +const bridge = useMessageBridge(); + +export function callTool(toolName: string, toolArgs: Record) { + return new Promise((resolve, reject) => { + bridge.addCommandListener('tools/call', (data: CasualRestAPI) => { + console.log(data.msg); + + if (data.code !== 200) { + reject(new Error(data.msg + '')); + } else { + resolve(data.msg); + } + }, { once: true }); + + bridge.postMessage({ + command: 'tools/call', + data: { toolName, toolArgs } + }); + }); } \ No newline at end of file diff --git a/service/tabs.json b/service/tabs.json index 9d0a345..1da574f 100644 --- a/service/tabs.json +++ b/service/tabs.json @@ -1,5 +1,5 @@ { - "currentIndex": 0, + "currentIndex": 1, "tabs": [ { "name": "交互测试", @@ -43,6 +43,24 @@ "systemPrompt": "" } } + }, + { + "name": "工具", + "icon": "icon-tool", + "type": "blank", + "componentIndex": 2, + "storage": { + "lastToolCallResponse": { + "content": [ + { + "type": "text", + "text": "4" + } + ], + "isError": false + }, + "currentToolName": "add" + } } ] } \ No newline at end of file