Repository navigation
Expand file tree
/
Copy pathagentHttp.js
More file actions
364 lines (336 loc) · 12.7 KB
/
Copy pathagentHttp.js
File metadata and controls
364 lines (336 loc) · 12.7 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
const agentChatStore = require('./agentChatStore')
const agentKnowledge = require('./agentKnowledge')
const composioBridge = require('./composioBridge')
const path = require('path')
const fs = require('fs')
const { extractTextFromBuffer, inferMediaType } = require('./agentDocumentExtract')
const { getGeneratedDir } = require('./agentDocumentGenerator')
function getRequestPath(req) {
try {
return new URL(req.url, 'http://localhost').pathname
} catch {
return String(req.url || '').split('?')[0]
}
}
function handleAgentGet(req, res, appSettings, deps = {}) {
const url = new URL(req.url, 'http://localhost')
if (url.pathname === '/api/agent/chats') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(agentChatStore.listChats()))
return true
}
if (url.pathname === '/api/agent/chats/search') {
const q = url.searchParams.get('q') || ''
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(agentChatStore.searchChats(q)))
return true
}
if (url.pathname === '/api/agent/chat') {
const id = url.searchParams.get('id')
const chat = id ? agentChatStore.readChat(id) : null
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(chat))
return true
}
if (url.pathname === '/api/agent/kb-status') {
agentKnowledge.getIndexStatus()
.then((status) => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(status))
})
.catch((error) => {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
})
return true
}
if (url.pathname === '/api/agent/composio-toolkits') {
composioBridge.listAvailableToolkits(appSettings?.agent)
.then((toolkits) => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(toolkits))
})
.catch((error) => {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
})
return true
}
if (url.pathname === '/api/agent/composio/connected') {
composioBridge.getConnectedToolkits(appSettings?.agent)
.then((connected) => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(connected))
})
.catch((error) => {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
})
return true
}
if (url.pathname === '/api/agent/notes-save-context') {
try {
if (!deps.getAgentNotesSaveContext) throw new Error('Notes save not available')
const ctx = deps.getAgentNotesSaveContext()
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(ctx))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (url.pathname === '/api/agent/generated') {
try {
const name = path.basename(url.searchParams.get('name') || '')
if (!name) throw new Error('name required')
const generatedDir = path.resolve(getGeneratedDir())
const filePath = path.resolve(generatedDir, name)
if (!filePath.startsWith(`${generatedDir}${path.sep}`)) throw new Error('Invalid file path')
if (!fs.existsSync(filePath)) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'File not found' }))
return true
}
const data = fs.readFileSync(filePath)
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="${name}"`
})
res.end(data)
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
return false
}
async function handleAgentPost(req, res, body, appSettings, deps = {}) {
const pathname = getRequestPath(req)
if (pathname === '/api/agent/chats') {
try {
const payload = JSON.parse(body || '{}')
const chat = agentChatStore.createChat({
title: payload.title,
capabilities: payload.capabilities || {
knowledgeBase: false,
deskMasterTools: false,
composioIntegrations: false
},
provider: payload.provider || appSettings?.agent?.defaultProvider,
model: payload.model || appSettings?.agent?.defaultModel
})
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(chat))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/chat/delete') {
try {
const payload = JSON.parse(body || '{}')
agentChatStore.deleteChat(payload.sessionId)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ success: true }))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/chat/update') {
try {
const payload = JSON.parse(body || '{}')
if (!payload.sessionId) throw new Error('sessionId required')
const patch = {}
if (payload.capabilities) patch.capabilities = payload.capabilities
if (payload.provider !== undefined) patch.provider = payload.provider
if (payload.model !== undefined) patch.model = payload.model
const chat = agentChatStore.updateChatMeta(payload.sessionId, patch)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(chat))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/chat/replace-messages') {
try {
const payload = JSON.parse(body || '{}')
if (!payload.sessionId) throw new Error('sessionId required')
if (!Array.isArray(payload.messages)) throw new Error('messages array required')
const chat = agentChatStore.replaceMessages(payload.sessionId, payload.messages)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(chat))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/kb-reindex') {
try {
const result = await agentKnowledge.reindexAll(appSettings, appSettings?.agent?.knowledgeBase)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/chat') {
try {
const payload = JSON.parse(body || '{}')
const agentOrchestrator = require('./agentOrchestrator')
const result = await agentOrchestrator.runChatTurn({
appSettings,
sessionId: payload.sessionId,
message: payload.message,
images: payload.images,
files: payload.files,
attachments: payload.attachments,
capabilities: payload.capabilities,
provider: payload.provider,
model: payload.model,
confirmedToolIds: payload.confirmedToolIds || [],
webContents: null
})
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/test-provider') {
try {
const payload = JSON.parse(body || '{}')
const agentProviders = require('./agentProviders')
const result = await agentProviders.testProvider(
require('./agentOrchestrator').buildAgentSettings(appSettings),
payload.providerId
)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/extract-document') {
try {
const payload = JSON.parse(body || '{}')
if (!payload?.name || !payload?.base64) throw new Error('name and base64 required')
const buffer = Buffer.from(payload.base64, 'base64')
const extractedText = await extractTextFromBuffer(buffer, payload.name, payload.mediaType)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({
kind: 'document',
name: payload.name,
mediaType: inferMediaType(payload.name, payload.mediaType),
extractedText,
size: buffer.length
}))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/composio/connect') {
try {
const payload = JSON.parse(body || '{}')
const toolkitSlug = payload.toolkitSlug
if (!toolkitSlug) throw new Error('toolkitSlug required')
const result = await composioBridge.initiateToolkitConnection(
appSettings?.agent,
toolkitSlug,
null
)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/composio/wait') {
try {
const payload = JSON.parse(body || '{}')
const toolkitSlug = payload.toolkitSlug
if (!toolkitSlug) throw new Error('toolkitSlug required')
const connected = await composioBridge.waitForConnection(appSettings?.agent, toolkitSlug, {
knownAccountIds: payload.knownAccountIds || [],
connectionRequestId: payload.connectionRequestId || null
})
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(connected))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/composio/cancel-wait') {
try {
const payload = JSON.parse(body || '{}')
const toolkitSlug = payload.toolkitSlug
if (!toolkitSlug) throw new Error('toolkitSlug required')
const cancelled = composioBridge.cancelWaitForConnection(toolkitSlug)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ cancelled }))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/composio/disconnect') {
try {
const payload = JSON.parse(body || '{}')
const result = await composioBridge.disconnectToolkit(appSettings?.agent, payload.accountId)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/set-notes-save-parent') {
try {
const payload = JSON.parse(body || '{}')
if (!deps.setAgentNotesSaveParent) throw new Error('Notes save not available')
const result = deps.setAgentNotesSaveParent(payload.parentId)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
if (pathname === '/api/agent/save-to-notes') {
try {
const payload = JSON.parse(body || '{}')
if (!deps.saveAgentResponseToNotes) throw new Error('Notes save not available')
const result = deps.saveAgentResponseToNotes(payload)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} catch (error) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: error.message }))
}
return true
}
return false
}
module.exports = { handleAgentGet, handleAgentPost }