This repository has no description
1import type { AgentResponse } from './runtime';
2
3export type OpenVTControlOptions = {
4 host?: string;
5 port?: number;
6};
7
8export class OpenVTControlClient {
9 private readonly host: string;
10 private readonly port: number;
11
12 constructor(options: OpenVTControlOptions = {}) {
13 this.host = options.host ?? '127.0.0.1';
14 this.port = options.port ?? 25733;
15 }
16
17 async sendResponse(response: AgentResponse) {
18 const messages: Array<Record<string, unknown>> = [];
19
20 for (const action of response.actions) {
21 if (action.type === 'talk:start') messages.push({ type: 'talk', state: 'start' });
22 if (action.type === 'talk:stop') messages.push({ type: 'talk', state: 'stop' });
23 if (action.type === 'emotion') messages.push({ type: 'emotion', value: action.value });
24 if (action.type === 'gesture') messages.push({ type: 'gesture', value: action.value });
25 }
26
27 messages.push({ type: 'text', value: response.reply });
28
29 for (const message of messages) {
30 await this.sendMessage(message);
31 }
32 }
33
34 async sendTalkState(state: 'start' | 'stop') {
35 await this.sendMessage({ type: 'talk', state });
36 }
37
38 async sendEmotion(value: 'neutral' | 'happy' | 'excited' | 'curious') {
39 await this.sendMessage({ type: 'emotion', value });
40 }
41
42 async sendText(value: string) {
43 await this.sendMessage({ type: 'text', value });
44 }
45
46 async sendMessage(message: Record<string, unknown>) {
47 const socket = await Bun.connect({
48 hostname: this.host,
49 port: this.port,
50 socket: {
51 open(socket) {
52 socket.write(JSON.stringify(message));
53 socket.end();
54 },
55 data() {},
56 close() {},
57 error() {},
58 },
59 });
60
61 socket.unref?.();
62 }
63}