-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
181 lines (151 loc) · 3.97 KB
/
server.js
File metadata and controls
181 lines (151 loc) · 3.97 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
import express from 'express';
import { AiModeClient } from './aimodeClient.js';
const app = express();
app.use(express.json({
limit: '2mb'
}));
app.use(express.static('public'));
const client = new AiModeClient({
headless: process.env.HEADLESS !== 'false',
userDataDir: process.env.USER_DATA_DIR || '/app/data/sessions/google-profile',
locale: process.env.BROWSER_LOCALE || 'it-IT',
timezoneId: process.env.BROWSER_TIMEZONE || 'Europe/Rome',
userAgent: process.env.BROWSER_USER_AGENT || undefined
});
await client.init();
const apiSpec = {
name: 'Google AI Mode API Wrapper',
version: '0.1.0',
endpoints: [
{
method: 'GET',
path: '/health',
description: 'Returns service status and browser environment configuration.'
},
{
method: 'POST',
path: '/chat/new',
description: 'Creates a new AI Mode browser tab/session and returns a chatId.'
},
{
method: 'POST',
path: '/chat/:chatId/message',
description: 'Sends a message to an existing chat and returns the latest assistant response.',
body: {
message: 'string'
}
},
{
method: 'GET',
path: '/chat/:chatId/last',
description: 'Returns the last parsed conversation item for a chat.'
},
{
method: 'GET',
path: '/chat/:chatId/all',
description: 'Returns the full parsed conversation for a chat.'
},
{
method: 'DELETE',
path: '/chat/:chatId',
description: 'Closes the browser tab associated with the chat and removes it from memory.'
}
]
};
app.get('/api', (_req, res) => {
res.json(apiSpec);
});
app.get('/health', (_req, res) => {
res.json({
ok: true,
timestamp: new Date().toISOString(),
browserLocale: process.env.BROWSER_LOCALE || 'it-IT',
browserTimezone: process.env.BROWSER_TIMEZONE || 'Europe/Rome',
headless: process.env.HEADLESS !== 'false',
userAgent: process.env.BROWSER_USER_AGENT || undefined
});
});
app.post('/chat/new', async (_req, res) => {
try {
const result = await client.newChat();
res.json(result);
} catch (error) {
res.status(500).json({
error: error.message
});
}
});
app.post('/chat/:chatId/message', async (req, res) => {
try {
const { chatId } = req.params;
const { message } = req.body;
if (typeof message !== 'string' || message.trim().length === 0) {
return res.status(400).json({
error: 'The "message" field must be a non-empty string'
});
}
const result = await client.sendMessage(chatId, message);
res.json({
chatId,
...result
});
} catch (error) {
res.status(500).json({
error: error.message
});
}
});
app.get('/chat/:chatId/last', async (req, res) => {
try {
const { chatId } = req.params;
const lastMessage = await client.readLastMessage(chatId);
res.json({
chatId,
lastMessage
});
} catch (error) {
res.status(500).json({
error: error.message
});
}
});
app.get('/chat/:chatId/all', async (req, res) => {
try {
const { chatId } = req.params;
const conversation = await client.readConversation(chatId);
res.json({
chatId,
conversation
});
} catch (error) {
res.status(500).json({
error: error.message
});
}
});
app.delete('/chat/:chatId', async (req, res) => {
try {
const { chatId } = req.params;
const result = await client.closeChat(chatId);
res.json(result);
} catch (error) {
res.status(500).json({
error: error.message
});
}
});
const port = Number(process.env.PORT || 3000);
app.listen(port, '0.0.0.0', () => {
console.log(`Google AI Mode API Wrapper listening on port ${port}`);
});
async function shutdown() {
console.log('Shutting down Google AI Mode API Wrapper...');
try {
await client.close();
} catch (error) {
console.error('Error during browser shutdown:', error);
}
process.exit(0);
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);