toolCall 修改

This commit is contained in:
锦恢 2025-04-10 13:33:05 +08:00
parent 0670cf8318
commit 515a19f322
4 changed files with 59 additions and 20 deletions

View File

@ -10,7 +10,12 @@ export class TaskLoop {
private readonly streamingToolCalls: Ref<ToolCall[]>, private readonly streamingToolCalls: Ref<ToolCall[]>,
private readonly messages: ChatMessage[], private readonly messages: ChatMessage[],
private readonly onError: (msg: string) => void private readonly onError: (msg: string) => void
) {} ) {
if (!onError) {
this.onError = (msg) => {
}
}
}
private handleToolCalls(toolCalls: ToolCall[]) { private handleToolCalls(toolCalls: ToolCall[]) {
// 这里预留给调用方实现工具执行逻辑 // 这里预留给调用方实现工具执行逻辑
@ -22,6 +27,7 @@ export class TaskLoop {
const chunkHandler = this.bridge.addCommandListener('llm/chat/completions/chunk', data => { const chunkHandler = this.bridge.addCommandListener('llm/chat/completions/chunk', data => {
if (data.code !== 200) { if (data.code !== 200) {
this.onError(data.msg || '请求模型服务时发生错误'); this.onError(data.msg || '请求模型服务时发生错误');
reject(new Error(data.msg)); reject(new Error(data.msg));
return; return;
} }
@ -113,4 +119,10 @@ export class TaskLoop {
this.onError(error instanceof Error ? error.message : '未知错误'); this.onError(error instanceof Error ? error.message : '未知错误');
} }
} }
// bridge api
public async name() {
}
} }

View File

@ -51,7 +51,7 @@ import { defineComponent, defineProps, watch, ref, computed } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import type { FormInstance, FormRules } from 'element-plus'; import type { FormInstance, FormRules } from 'element-plus';
import { tabs } from '../panel'; import { tabs } from '../panel';
import { toolsManager, ToolStorage } from './tools'; import { callTool, toolsManager, ToolStorage } from './tools';
import { CasualRestAPI, ToolCallResponse } from '@/hook/type'; import { CasualRestAPI, ToolCallResponse } from '@/hook/type';
import { useMessageBridge } from '@/api/message-bridge'; import { useMessageBridge } from '@/api/message-bridge';
@ -111,24 +111,11 @@ const resetForm = () => {
tabStorage.lastToolCallResponse = undefined; tabStorage.lastToolCallResponse = undefined;
}; };
function handleExecute() { async function handleExecute() {
if (!currentTool.value) return; if (!currentTool.value) return;
const bridge = useMessageBridge(); const toolResponse = await callTool(tabStorage.currentToolName, formData.value);
tabStorage.lastToolCallResponse = toolResponse;
bridge.addCommandListener('tools/call', (data: CasualRestAPI<ToolCallResponse>) => {
console.log(data.msg);
tabStorage.lastToolCallResponse = data.msg;
}, { once: true });
bridge.postMessage({
command: 'tools/call',
data: {
toolName: tabStorage.currentToolName,
toolArgs: formData.value
}
});
} }
watch(() => tabStorage.currentToolName, () => { watch(() => tabStorage.currentToolName, () => {

View File

@ -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'; import { reactive } from 'vue';
export const toolsManager = reactive<{ export const toolsManager = reactive<{
@ -11,3 +12,24 @@ export interface ToolStorage {
currentToolName: string; currentToolName: string;
lastToolCallResponse?: ToolCallResponse; lastToolCallResponse?: ToolCallResponse;
} }
const bridge = useMessageBridge();
export function callTool(toolName: string, toolArgs: Record<string, any>) {
return new Promise<ToolCallResponse>((resolve, reject) => {
bridge.addCommandListener('tools/call', (data: CasualRestAPI<ToolCallResponse>) => {
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 }
});
});
}

View File

@ -1,5 +1,5 @@
{ {
"currentIndex": 0, "currentIndex": 1,
"tabs": [ "tabs": [
{ {
"name": "交互测试", "name": "交互测试",
@ -43,6 +43,24 @@
"systemPrompt": "" "systemPrompt": ""
} }
} }
},
{
"name": "工具",
"icon": "icon-tool",
"type": "blank",
"componentIndex": 2,
"storage": {
"lastToolCallResponse": {
"content": [
{
"type": "text",
"text": "4"
}
],
"isError": false
},
"currentToolName": "add"
}
} }
] ]
} }