Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·506 lines (444 loc) · 17.3 KB
/
Copy pathcli.js
File metadata and controls
executable file
·506 lines (444 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env node
/**
* Gemini Bridge - CLI Orchestrator and Universal OpenAI-Compatible Proxy
* Connects to active browser webchat (Gemini, ChatGPT, Claude) via WebSocket on port 8765.
* Provides OpenAI API proxy (/v1/chat/completions, /v1/models) and interactive agent loop.
*/
const { WebSocketServer } = require('ws');
const readline = require('readline');
const http = require('http');
const tools = require('./tools');
// CLI Argument Parsing
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
Gemini Bridge - Universal Webchat Agent Orchestrator & OpenAI Proxy
USAGE:
node cli.js [OPTIONS]
npx gemini-bridge [OPTIONS]
OPTIONS:
-p, --port <number> Port for WebSocket server and OpenAI HTTP proxy (default: 8765)
--safe Enable humanized pacing and safety delays (default: enabled)
--fast Disable safety pacing delays for faster execution
-t, --test Verify syntax, test configuration, and exit cleanly
-h, --help Show this help manual
ENDPOINTS:
GET /v1/models OpenAI-compatible models list
POST /v1/chat/completions OpenAI-compatible chat completion (streaming & non-streaming)
WS ws://localhost:<port> Bi-directional communication with Chrome extension
`);
process.exit(0);
}
if (args.includes('--test')) {
console.log('\x1b[32m[Gemini Bridge Test]\x1b[0m Syntax and configuration valid.');
process.exit(0);
}
const isSafeMode = !args.includes('--fast');
let configuredPort = process.env.PORT || process.env.WS_PORT || 8765;
const portArgIndex = args.findIndex((arg) => arg === '-p' || arg === '--port');
if (portArgIndex !== -1 && args[portArgIndex + 1]) {
configuredPort = parseInt(args[portArgIndex + 1], 10) || configuredPort;
}
const WS_PORT = configuredPort;
const PHONE_ENDPOINT = process.env.PHONE_ENDPOINT || 'http://localhost:8000';
let activeSocket = null;
let pendingResponseResolver = null;
let activePlatform = 'Gemini';
let activeModel = 'gemini-web';
const SYSTEM_TOOL_SPEC = `
You have access to the following local developer tools on the user's laptop.
To invoke a tool, output a single JSON block wrapped inside a \`\`\`tool_call markdown fence.
Available tools:
1. run_command: Execute shell commands locally.
Format:
\`\`\`tool_call
{ "tool": "run_command", "args": { "command": "npm test" } }
\`\`\`
2. read_file: Read file contents with optional line slice.
Format:
\`\`\`tool_call
{ "tool": "read_file", "args": { "path": "src/index.js", "startLine": 1, "endLine": 50 } }
\`\`\`
3. write_file: Write or create a file.
Format:
\`\`\`tool_call
{ "tool": "write_file", "args": { "path": "src/index.js", "content": "..." } }
\`\`\`
Rules:
- Make one tool call per turn when you need environment inspection or code edits.
- When your task is complete and you do not need more tools, reply with your final response without any \`\`\`tool_call block.
`;
function parseToolCall(responseText) {
if (!responseText) return null;
// 1. Markdown code blocks: ```tool_call ... ``` or ```json ... ```
const toolCallRegex = /```(?:tool_call|json|bash)?\s*([\s\S]*?)```/g;
let match;
while ((match = toolCallRegex.exec(responseText)) !== null) {
const rawContent = match[1].trim();
try {
const parsed = JSON.parse(rawContent);
if (parsed.tool && parsed.args) return parsed;
if (parsed.name && (parsed.arguments || parsed.args)) {
return { tool: parsed.name, args: parsed.arguments || parsed.args };
}
} catch {}
}
// 2. Direct JSON object containing "tool" and "args"
const jsonBraceRegex = /\{[\s\S]*?"tool"\s*:\s*"[^"]+"[\s\S]*?"args"\s*:\s*\{[\s\S]*?\}\s*\}/;
const directMatch = responseText.match(jsonBraceRegex);
if (directMatch) {
try {
const parsed = JSON.parse(directMatch[0]);
if (parsed.tool && parsed.args) return parsed;
} catch {}
}
// 3. OpenAI format { "name": "...", "arguments": {...} }
const openaiStyleRegex = /\{[\s\S]*?"name"\s*:\s*"[^"]+"[\s\S]*?"arguments"\s*:\s*\{[\s\S]*?\}\s*\}/;
const openaiMatch = responseText.match(openaiStyleRegex);
if (openaiMatch) {
try {
const parsed = JSON.parse(openaiMatch[0]);
return { tool: parsed.name, args: parsed.arguments };
} catch {}
}
return null;
}
/**
* Checks phone System 1 microservice with ultra-short timeout and zero-crash fallback
*/
function queryPhoneSystem1(pathEndpoint, payload) {
return new Promise((resolve) => {
try {
const data = JSON.stringify(payload);
const url = new URL(pathEndpoint, PHONE_ENDPOINT);
const req = http.request(
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
},
timeout: 400
},
(res) => {
let body = '';
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {
try {
resolve(JSON.parse(body));
} catch {
resolve(null);
}
});
}
);
req.on('error', () => resolve(null));
req.on('timeout', () => {
req.destroy();
resolve(null);
});
req.write(data);
req.end();
} catch {
resolve(null);
}
});
}
function sendPromptToChrome(promptText) {
return new Promise((resolve, reject) => {
if (!activeSocket) {
return reject(
new Error(`No browser extension connected. Open ${activePlatform} in Chrome with the extension active.`)
);
}
pendingResponseResolver = resolve;
activeSocket.send(JSON.stringify({ type: 'PROMPT', text: promptText, safeMode: isSafeMode }));
});
}
async function runAgentLoop(userGoal) {
let promptToSend = `${SYSTEM_TOOL_SPEC}\n\nUser Task: ${userGoal}`;
let turn = 1;
const maxTurns = 25;
while (turn <= maxTurns) {
console.log(`\n\x1b[36m[Turn ${turn}] Routing prompt to ${activePlatform} (${activeModel})...\x1b[0m`);
let responseText;
try {
responseText = await sendPromptToChrome(promptToSend);
} catch (err) {
console.error(`\x1b[31m[Bridge Error]\x1b[0m ${err.message}`);
break;
}
const toolCall = parseToolCall(responseText);
if (!toolCall) {
console.log(`\n\x1b[32m=== Final Assistant Response ===\x1b[0m\n${responseText}\n`);
break;
}
console.log(`\n\x1b[33m[Tool Call]\x1b[0m ${toolCall.tool}`);
console.log(JSON.stringify(toolCall.args, null, 2));
if (toolCall.tool === 'run_command') {
const riskCheck = await queryPhoneSystem1('/risk', { command: toolCall.args.command });
if (riskCheck && riskCheck.requires_confirmation) {
console.log(
`\x1b[31m[Phone System 1 Warning]\x1b[0m Command flagged high-risk: ${riskCheck.reason || 'Destructive potential'}`
);
}
}
let toolResult;
if (toolCall.tool === 'run_command') {
toolResult = await tools.run_command(toolCall.args);
} else if (toolCall.tool === 'read_file') {
toolResult = await tools.read_file(toolCall.args);
} else if (toolCall.tool === 'write_file') {
toolResult = await tools.write_file(toolCall.args);
} else {
toolResult = { error: `Unknown tool: ${toolCall.tool}` };
}
const pruned = await queryPhoneSystem1('/prune', { output: JSON.stringify(toolResult, null, 2) });
const formattedOutput = pruned && pruned.output ? pruned.output : JSON.stringify(toolResult, null, 2);
promptToSend = `[Tool Result: ${toolCall.tool}]\n\`\`\`json\n${formattedOutput}\n\`\`\`\nPlease inspect this result and proceed with your next tool call or your final response.`;
turn++;
// Safety pacing: 1500ms human reading pace pause before sending subsequent automated prompts
if (isSafeMode) {
console.log(`\x1b[90m[Safety Mode] Human reading pace pause (1500ms)...\x1b[0m`);
await new Promise((resolve) => setTimeout(resolve, 1500));
}
}
if (turn > maxTurns) {
console.log('\x1b[31m[Agent Notice]\x1b[0m Reached maximum conversation turns.');
}
}
function formatOpenAIMessagesToPrompt(messages, toolsList) {
let prompt = '';
if (Array.isArray(toolsList) && toolsList.length > 0) {
prompt += `You have access to the following developer tools on the user's laptop:\n`;
for (const t of toolsList) {
if (t.function) {
prompt += `- ${t.function.name}: ${t.function.description || ''}\n Parameters: ${JSON.stringify(
t.function.parameters || {}
)}\n`;
}
}
prompt += `\nTo invoke a tool, output a single JSON block wrapped inside a \`\`\`tool_call markdown fence:
\`\`\`tool_call
{ "tool": "tool_name", "args": { ... } }
\`\`\`
If no tool is needed, simply write your direct response.\n\n`;
}
if (Array.isArray(messages)) {
prompt += messages
.map((m) => {
const roleLabel = m.role === 'system' ? 'System' : m.role === 'user' ? 'User' : 'Assistant';
const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content);
if (m.tool_call_id || m.role === 'tool') {
return `[Tool Result (${m.name || 'output'})]:\n${content}`;
}
return `[${roleLabel}]:\n${content}`;
})
.join('\n\n');
}
return prompt;
}
function start() {
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', '*');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (req.method === 'GET' && req.url === '/v1/models') {
res.writeHead(200, { 'Content-Type': 'application/json' });
const now = Math.floor(Date.now() / 1000);
res.end(
JSON.stringify({
object: 'list',
data: [
{ id: activeModel, object: 'model', created: now, owned_by: activePlatform.toLowerCase() },
{ id: 'argon', object: 'model', created: now, owned_by: 'google' },
{ id: 'gemini-4-argon', object: 'model', created: now, owned_by: 'google' },
{ id: 'gpt-6.1-sol', object: 'model', created: now, owned_by: 'openai' },
{ id: 'gpt-6-astra', object: 'model', created: now, owned_by: 'openai' },
{ id: 'fable-5.5', object: 'model', created: now, owned_by: 'anthropic' },
{ id: 'fable-5.1', object: 'model', created: now, owned_by: 'anthropic' },
{ id: 'claude-opus-5-5', object: 'model', created: now, owned_by: 'anthropic' },
{ id: 'gemini-web', object: 'model', created: now, owned_by: 'google' }
]
})
);
return;
}
if (req.method === 'POST' && req.url === '/v1/chat/completions') {
let body = '';
req.on('data', (chunk) => (body += chunk));
req.on('end', async () => {
try {
const payload = JSON.parse(body || '{}');
const stream = payload.stream === true;
const requestedModel = payload.model || activeModel;
const promptText = formatOpenAIMessagesToPrompt(payload.messages || [], payload.tools);
if (!activeSocket) {
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
error: {
message: `Browser extension is not connected on ws://localhost:${WS_PORT}. Please open ${activePlatform} in Chrome.`
}
})
);
return;
}
console.log(`\n\x1b[36m[OpenAI Proxy] Routing request for '${requestedModel}' to ${activePlatform}...\x1b[0m`);
const responseText = await sendPromptToChrome(promptText);
const toolCall = parseToolCall(responseText);
if (stream) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive'
});
const chunkId = 'chatcmpl-' + Date.now();
const chunk = {
id: chunkId,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: requestedModel,
choices: [
{
index: 0,
delta: toolCall
? {
tool_calls: [
{
index: 0,
id: 'call_' + Math.random().toString(36).substring(2, 9),
type: 'function',
function: {
name: toolCall.tool,
arguments: JSON.stringify(toolCall.args)
}
}
]
}
: { content: responseText },
finish_reason: toolCall ? 'tool_calls' : 'stop'
}
]
};
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
res.write('data: [DONE]\n\n');
res.end();
} else {
let messageObj = { role: 'assistant', content: responseText };
let finishReason = 'stop';
if (toolCall) {
const toolCallId = 'call_' + Math.random().toString(36).substring(2, 9);
messageObj = {
role: 'assistant',
content: responseText.replace(/```(?:tool_call|json)?\s*[\s\S]*?```/g, '').trim(),
tool_calls: [
{
id: toolCallId,
type: 'function',
function: {
name: toolCall.tool,
arguments: JSON.stringify(toolCall.args)
}
}
]
};
finishReason = 'tool_calls';
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
id: 'chatcmpl-' + Date.now(),
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: requestedModel,
choices: [
{
index: 0,
message: messageObj,
finish_reason: finishReason
}
]
})
);
}
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: err.message } }));
}
});
return;
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: { message: 'Not found' } }));
});
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
activeSocket = ws;
console.log(`\n\x1b[32m[Bridge Connected]\x1b[0m Browser extension attached on ws://localhost:${WS_PORT}`);
// Send initial configuration to extension
ws.send(JSON.stringify({ type: 'CONFIG', safeMode: isSafeMode }));
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.type === 'HELLO' || data.type === 'STATUS') {
if (data.platform) activePlatform = data.platform;
if (data.model) activeModel = data.model;
console.log(`\x1b[35m[Active Session]\x1b[0m Platform: ${activePlatform} | Model: ${activeModel}`);
} else if (data.type === 'RESPONSE' && pendingResponseResolver) {
const resolve = pendingResponseResolver;
pendingResponseResolver = null;
resolve(data.text);
} else if (data.type === 'ERROR') {
console.error(`\x1b[31m[Extension Error]\x1b[0m ${data.message}`);
}
} catch (err) {
console.error('[CLI] Failed to parse message from extension:', err);
}
});
ws.on('close', () => {
console.log('\n\x1b[31m[Bridge Disconnected]\x1b[0m Browser extension detached.');
activeSocket = null;
});
});
server.listen(WS_PORT, () => {
console.log(`\x1b[35m=== Gemini Bridge Orchestrator & Universal Proxy ===\x1b[0m`);
console.log(`HTTP/OpenAI API: http://localhost:${WS_PORT}/v1`);
console.log(`WebSocket Bridge: ws://localhost:${WS_PORT}`);
console.log(`Safety Mode: ${isSafeMode ? '\x1b[32mEnabled (1500ms pacing + humanized jitter)\x1b[0m' : '\x1b[33mDisabled (--fast active)\x1b[0m'}`);
console.log(`Waiting for browser connection (Gemini / ChatGPT / Claude)...`);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '\ngemini-bridge> '
});
rl.prompt();
rl.on('line', async (line) => {
const input = line.trim();
if (!input) {
rl.prompt();
return;
}
if (input === 'exit' || input === 'quit') {
console.log('Exiting Gemini Bridge...');
process.exit(0);
}
if (!activeSocket) {
console.log('\x1b[33mNotice: Browser extension is not connected yet.\x1b[0m');
console.log('1. Open Chrome at https://gemini.google.com, https://chatgpt.com, or https://claude.ai');
console.log('2. Ensure the unpacked Gemini Bridge extension is loaded and active.');
rl.prompt();
return;
}
await runAgentLoop(input);
rl.prompt();
});
}
start();