forked from sugarforever/chat-ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseTools.ts
55 lines (48 loc) · 1.31 KB
/
useTools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { ref, type Ref } from 'vue'
export interface Tool {
type: 'function'
name: string
description: string
parameters?: {
type: 'object'
properties: Record<string, {
type: string
description: string
}>
}
handler: (args: any) => Promise<any>
}
const globalTools: Ref<Tool[]> = ref([])
export function useTools() {
const registerTool = (tool: Tool) => {
console.log('Registering tool:', tool)
const existingIndex = globalTools.value.findIndex(t => t.name === tool.name)
if (existingIndex >= 0) {
globalTools.value[existingIndex] = tool
} else {
globalTools.value.push(tool)
}
}
const unregisterTool = (toolName: string) => {
console.log('Unregistering tool:', toolName)
const index = globalTools.value.findIndex(t => t.name === toolName)
if (index >= 0) {
globalTools.value.splice(index, 1)
}
}
const getTools = () => globalTools.value
const executeToolHandler = async (toolName: string, args: any) => {
console.log('Executing tool handler:', toolName, args)
const tool = globalTools.value.find(t => t.name === toolName)
if (!tool) {
throw new Error(`Tool ${toolName} not found`)
}
return await tool.handler(args)
}
return {
registerTool,
unregisterTool,
getTools,
executeToolHandler
}
}