import type OpenAI from "openai"; import type { Result } from "./types"; type ToolCall = OpenAI.Chat.Completions.ChatCompletionMessageToolCall; type Tool = OpenAI.Chat.Completions.ChatCompletionTool; type ToolMsg = OpenAI.Chat.Completions.ChatCompletionToolMessageParam; type Message = OpenAI.Chat.Completions.ChatCompletionMessage; export default class OpenAIToolUse { api; model; socket; tools; message; calls; res: ToolMsg | null = null; constructor( api: OpenAI, model: string, tools: Tool[], message: Message, calls: ToolCall[], ) { this.api = api; this.model = model; this.socket = new WebSocket("http://localhost:8900"); this.tools = tools; this.message = message; this.calls = calls; for (let c of calls) { console.log({ c }); } this.wsHandlers(); } wsHandlers() { this.socket.addEventListener("open", (_data) => { this.handleToolCalls(); }); this.socket.addEventListener("message", (ev) => { const j = JSON.parse(ev.data); if ("functionRes" in j) this.handleRes(j.functionRes); }); } handleToolCalls() { for (let c of this.calls) this.socket.send(JSON.stringify({ call: c })); } async handleRes(res: Result) { if ("error" in res) { console.log("TODO"); return; } this.res = res.ok; const messages = [this.message, res.ok]; console.log({ messages }, "almost there"); const completion = await this.api.chat.completions.create({ model: this.model, messages, tools: this.tools, }); console.log({ completion }); for (let choice of completion.choices) { console.log({ choice }); } } }