Skip to content

Commit 06d2d52

Browse files
ThinkOffAppclaude
andcommitted
Add OpenRoom bridge plugin
Connects OpenClaw agents to MiniMax OpenRoom (AI browser desktop). Translates room messages (@openroom app action params) to OpenRoom CharacterAppAction protocol. Forwards lifecycle events back to rooms. Supports WebSocket and HTTP API connections to OpenRoom. Requested in MiniMax-AI/OpenRoom#27. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 34f2e03 commit 06d2d52

1 file changed

Lines changed: 224 additions & 0 deletions

File tree

‎src/openroom-bridge.mjs‎

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/**
2+
* OpenRoom Bridge for IDE Agent Kit
3+
*
4+
* Connects OpenClaw agents to MiniMax OpenRoom (browser-based AI desktop).
5+
* Agents can send actions to OpenRoom apps (Music, Chess, Email, etc.)
6+
* and receive lifecycle events back through GroupMind rooms.
7+
*
8+
* Protocol: OpenRoom uses CharacterAppAction for bidirectional communication.
9+
* This bridge translates between room messages and OpenRoom actions.
10+
*
11+
* @see https://github.com/MiniMax-AI/OpenRoom
12+
*/
13+
14+
import { EventEmitter } from 'node:events';
15+
16+
// OpenRoom action trigger sources
17+
const TRIGGER_BY = { User: 1, Agent: 2, System: 3 };
18+
19+
// Known OpenRoom app IDs (from their built-in apps)
20+
const APP_IDS = {
21+
music: 1,
22+
chess: 2,
23+
gomoku: 3,
24+
freecell: 4,
25+
email: 5,
26+
diary: 6,
27+
twitter: 7,
28+
album: 8,
29+
cybernews: 9,
30+
};
31+
32+
/**
33+
* OpenRoom Bridge
34+
*
35+
* Usage:
36+
* const bridge = new OpenRoomBridge({ openroomUrl: 'http://localhost:3000', roomApi, roomSlug });
37+
* bridge.start();
38+
*
39+
* Room messages like "@openroom play jazz" get translated to OpenRoom actions.
40+
* OpenRoom lifecycle events get posted back to the room.
41+
*/
42+
export class OpenRoomBridge extends EventEmitter {
43+
constructor(config = {}) {
44+
super();
45+
this.openroomUrl = config.openroomUrl || 'http://localhost:3000';
46+
this.wsUrl = config.wsUrl || this.openroomUrl.replace(/^http/, 'ws') + '/ws';
47+
this.roomApi = config.roomApi; // { baseUrl, apiKey, room }
48+
this.pollInterval = config.pollInterval || 5000;
49+
this.ws = null;
50+
this.connected = false;
51+
this.actionCounter = 0;
52+
}
53+
54+
/**
55+
* Send an action to OpenRoom
56+
* @param {string} appName - App name (music, chess, email, etc.)
57+
* @param {string} actionType - Action type (e.g. PLAY_TRACK, CREATE_POST)
58+
* @param {Record<string, string>} params - Action parameters
59+
*/
60+
async sendAction(appName, actionType, params = {}) {
61+
const appId = APP_IDS[appName.toLowerCase()];
62+
if (!appId) {
63+
throw new Error(`Unknown OpenRoom app: ${appName}. Known: ${Object.keys(APP_IDS).join(', ')}`);
64+
}
65+
66+
const action = {
67+
app_id: appId,
68+
action_id: ++this.actionCounter,
69+
action_type: actionType,
70+
params,
71+
timestamp_ms: Date.now(),
72+
trigger_by: TRIGGER_BY.Agent,
73+
};
74+
75+
// Send via WebSocket if connected
76+
if (this.ws && this.connected) {
77+
this.ws.send(JSON.stringify({
78+
type: 'agent_action',
79+
action,
80+
}));
81+
this.emit('action_sent', action);
82+
return action;
83+
}
84+
85+
// Fallback: send via HTTP API if available
86+
try {
87+
const resp = await fetch(`${this.openroomUrl}/api/agent/action`, {
88+
method: 'POST',
89+
headers: { 'Content-Type': 'application/json' },
90+
body: JSON.stringify(action),
91+
});
92+
if (resp.ok) {
93+
this.emit('action_sent', action);
94+
return action;
95+
}
96+
} catch (e) {
97+
// HTTP fallback failed
98+
}
99+
100+
throw new Error('Not connected to OpenRoom');
101+
}
102+
103+
/**
104+
* Parse a room message into an OpenRoom action
105+
* Messages like "@openroom music play jazz" or "@openroom email compose to:bob subject:hello"
106+
*/
107+
parseRoomMessage(body) {
108+
const match = body.match(/@openroom\s+(\w+)\s+(\w+)\s*(.*)/i);
109+
if (!match) return null;
110+
111+
const [, appName, actionType, paramStr] = match;
112+
const params = {};
113+
114+
// Parse key:value params or treat remainder as a single "query" param
115+
if (paramStr.includes(':')) {
116+
paramStr.split(/\s+/).forEach(kv => {
117+
const [k, ...v] = kv.split(':');
118+
if (k && v.length) params[k] = v.join(':');
119+
});
120+
} else if (paramStr.trim()) {
121+
params.query = paramStr.trim();
122+
}
123+
124+
return { appName, actionType: actionType.toUpperCase(), params };
125+
}
126+
127+
/**
128+
* Post an OpenRoom event back to the GroupMind room
129+
*/
130+
async postToRoom(message) {
131+
if (!this.roomApi) return;
132+
try {
133+
await fetch(`${this.roomApi.baseUrl}/messages`, {
134+
method: 'POST',
135+
headers: {
136+
'X-API-Key': this.roomApi.apiKey,
137+
'Content-Type': 'application/json',
138+
},
139+
body: JSON.stringify({
140+
room: this.roomApi.room,
141+
body: message,
142+
}),
143+
});
144+
} catch (e) {
145+
this.emit('error', e);
146+
}
147+
}
148+
149+
/**
150+
* Handle incoming room messages and translate to OpenRoom actions
151+
*/
152+
async handleRoomMessage(msg) {
153+
const parsed = this.parseRoomMessage(msg.body || '');
154+
if (!parsed) return;
155+
156+
try {
157+
const action = await this.sendAction(parsed.appName, parsed.actionType, parsed.params);
158+
this.emit('action_executed', { msg, action });
159+
} catch (e) {
160+
await this.postToRoom(`[OpenRoom] Failed to execute: ${e.message}`);
161+
}
162+
}
163+
164+
/**
165+
* Connect to OpenRoom WebSocket for real-time events
166+
*/
167+
async connect() {
168+
try {
169+
const WebSocket = (await import('ws')).default;
170+
this.ws = new WebSocket(this.wsUrl);
171+
172+
this.ws.on('open', () => {
173+
this.connected = true;
174+
this.emit('connected');
175+
});
176+
177+
this.ws.on('message', (data) => {
178+
try {
179+
const event = JSON.parse(data.toString());
180+
this.emit('openroom_event', event);
181+
182+
// Forward lifecycle events to room
183+
if (event.type === 'app_lifecycle' && this.roomApi) {
184+
this.postToRoom(`[OpenRoom] ${event.app || 'App'}: ${event.status || event.type}`);
185+
}
186+
} catch (e) {
187+
// ignore parse errors
188+
}
189+
});
190+
191+
this.ws.on('close', () => {
192+
this.connected = false;
193+
this.emit('disconnected');
194+
// Reconnect after 5s
195+
setTimeout(() => this.connect(), 5000);
196+
});
197+
198+
this.ws.on('error', (e) => {
199+
this.emit('error', e);
200+
});
201+
} catch (e) {
202+
this.emit('error', e);
203+
}
204+
}
205+
206+
/**
207+
* Start the bridge (connect to OpenRoom + listen for room messages)
208+
*/
209+
async start() {
210+
await this.connect();
211+
this.emit('started');
212+
}
213+
214+
stop() {
215+
if (this.ws) {
216+
this.ws.close();
217+
this.ws = null;
218+
}
219+
this.connected = false;
220+
this.emit('stopped');
221+
}
222+
}
223+
224+
export default OpenRoomBridge;

0 commit comments

Comments
 (0)