mirror of
https://github.com/langchain-ai/react-voice-agent.git
synced 2026-07-01 15:35:18 -04:00
Allow passing WebSocket in directly (#4)
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user