Allow passing WebSocket in directly (#4)

This commit is contained in:
Jacob Lee
2024-10-04 10:13:44 -07:00
committed by GitHub
parent 5bec194aa0
commit 2474ddc43f
2 changed files with 10 additions and 8 deletions
+2 -6
View File
@@ -1,15 +1,14 @@
import "dotenv/config";
import { type WebSocket } from "ws";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { createNodeWebSocket } from "@hono/node-ws";
import { serveStatic } from "@hono/node-server/serve-static";
import { WebSocket } from "ws";
import { OpenAIVoiceReactAgent } from "./lib/langchain_openai_voice";
import { INSTRUCTIONS } from "./prompt";
import { TOOLS } from "./tools";
import { createStreamFromWebsocket } from "./lib/utils";
const app = new Hono();
@@ -30,10 +29,7 @@ app.get(
tools: TOOLS,
model: "gpt-4o-realtime-preview",
});
await agent.connect(
createStreamFromWebsocket(ws.raw as WebSocket),
ws.send.bind(ws)
);
await agent.connect(ws.raw as WebSocket, ws.send.bind(ws));
},
onClose: () => {
console.log("CLOSING");
+8 -2
View File
@@ -198,13 +198,19 @@ export class OpenAIVoiceReactAgent {
/**
* Connect to the OpenAI API and send and receive messages.
* @param inputStream
* @param websocketOrStream
* @param sendOutputChunk
*/
async connect(
inputStream: AsyncGenerator<string>,
websocketOrStream: AsyncGenerator<string> | WebSocket,
sendOutputChunk: (chunk: string) => void | Promise<void>
) {
let inputStream;
if ("next" in websocketOrStream) {
inputStream = websocketOrStream;
} else {
inputStream = createStreamFromWebsocket(websocketOrStream);
}
const toolsByName = this.tools.reduce(
(toolsByName: Record<string, StructuredTool>, tool) => {
toolsByName[tool.name] = tool;