Skip to Content

Setup

Terminal
npm install @autobe/agent @autobe/compiler @autobe/interface npm install @autobe/rpc tgrid

To develop NodeJS WebSocket server of @autobe, you need to install these packages.

At first, install @autobe/agent, @autobe/compiler and @autobe/interface packages, which are required for chatbot. And then, install @autobe/rpc and tgrid packages.

tgrid is a TypeScript based RPC (Remote Procedure Call) framework supporting WebSocket protocol, and @autobe/rpc is a wrapper module of @autobe/agent following the WebSocket RPC.

Development

nodejs/main.ts
import { AutoBeAgent } from "@autobe/agent"; import { AutoBeCompiler } from "@autobe/compiler"; import { IAutoBeRpcListener, IAutoBeRpcService } from "@autobe/interface"; import { AutoBeRpcService } from "@autobe/rpc"; import OpenAI from "openai"; import { WebSocketServer } from "tgrid"; const server: WebSocketServer<null, IAutoBeRpcService, IAutoBeRpcListener> = new WebSocketServer(); await server.open(3_001, async (acceptor) => { const agent: AutoBeAgent<"chatgpt"> = new AutoBeAgent({ model: "chatgpt", vendor: { api: new OpenAI({ apiKey: "********" }), model: "gpt-4.1", }, compiler: new AutoBeCompiler(), }); const service: AutoBeRpcService<"chatgpt"> = new AutoBeRpcService({ agent, listener: acceptor.getDriver(), }); await acceptor.accept(service); });

You can develop WebSocket server application like above.

At first, create an WebSocketServer instance with IAutoBeRpcService and IAutoBeRpcListener type specifiactions, and open the server with a port number and a callback function that is called whenever a client is connected.

And in the callback function, create an AutoBeAgent instance and wrap it into a new AutoBeRpcService instance. And then accept the client connection by calling the WebSocketAcceptor.accept() function with the AutoBeRpcService instance.

When you’ve completed the acceptance, everything is completed. When client calls the IAutoBeRpcService.conversate() function remotely, server will response to the client by calling the IAutoBeRpcListener functions remotely too.

Last updated on