forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiFunctions.js
More file actions
executable file
·521 lines (460 loc) · 12.8 KB
/
apiFunctions.js
File metadata and controls
executable file
·521 lines (460 loc) · 12.8 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
const apiUrl = (path) => path
const addYamlSuffix = (filename) => {
const trimmed = (filename || '').trim()
if (!trimmed) {
return ''
}
if (trimmed.endsWith('.yaml') || trimmed.endsWith('.yml')) {
return trimmed
}
return `${trimmed}.yaml`
}
// Upload a YAML file
export async function postYaml(filename, content) {
try {
const fullFilename = addYamlSuffix(filename)
const response = await fetch(apiUrl('/api/workflows/upload/content'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
filename: fullFilename,
content: content
})
})
// Return 400 when content is invalid
if (response.ok || response.status === 400) {
const data = await response.json()
// If status is missing, content validation failed and detail contains the error
if (data.status) {
return {
success: true,
message: 'YAML file saved successfully!'
}
} else if (data.detail) {
return {
success: false,
detail: data.detail
}
} else {
return {
success: false,
message: 'Unknown error saving YAML file'
}
}
} else {
const errorData = await response.json().catch(() => ({}))
console.error('Error saving YAML file:', errorData)
return {
success: false,
message: `Error saving YAML file: ${errorData.message || response.statusText}`
}
}
} catch (error) {
console.error('Error saving YAML file:', error)
return {
success: false,
message: 'API error'
}
}
}
export async function updateYaml(filename, content) {
try {
const yamlFilename = addYamlSuffix(filename)
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(yamlFilename)}/update`), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content
})
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
filename: data?.filename || yamlFilename,
message: data?.message || 'Workflow updated successfully'
}
}
return {
success: false,
detail: data?.detail,
message: data.error?.message || 'Failed to update workflow',
status: response.status
}
} catch (error) {
console.error('Error updating workflow YAML:', error)
return {
success: false,
message: 'API error'
}
}
}
// Rename YAML file
export async function postYamlNameChange(filename, newFilename) {
try {
const yamlFilename = addYamlSuffix(filename)
const yamlNewFilename = addYamlSuffix(newFilename)
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(yamlFilename)}/rename`), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
new_filename: yamlNewFilename
})
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
filename: data?.filename || yamlNewFilename,
message: data?.message || 'Workflow renamed successfully'
}
}
return {
success: false,
detail: data?.detail,
message: data?.message || 'Failed to rename workflow',
status: response.status
}
} catch (error) {
console.error('Error renaming workflow YAML:', error)
return {
success: false,
message: 'API error'
}
}
}
// Copy YAML file
export async function postYamlCopy(filename, newFilename) {
try {
const yamlFilename = addYamlSuffix(filename)
const yamlNewFilename = addYamlSuffix(newFilename)
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(yamlFilename)}/copy`), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
new_filename: yamlNewFilename
})
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
filename: data?.filename || yamlNewFilename,
message: data?.message || 'Workflow copied successfully'
}
}
return {
success: false,
detail: data?.detail,
message: data?.message || 'Failed to copy workflow',
status: response.status
}
} catch (error) {
console.error('Error copying workflow YAML:', error)
return {
success: false,
message: 'API error'
}
}
}
// Load the YAML file list from the API with names and descriptions
export async function fetchWorkflowsWithDesc() {
try {
const response = await fetch(apiUrl('/api/workflows'))
if (!response.ok) {
throw new Error(`/api/workflows fetch error, status: ${response.status}`)
}
const data = await response.json()
// Fetch YAML descriptions by filename
const filesWithDesc = await Promise.all(
data.workflows.map(async (filename) => {
try {
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(filename)}/desc`))
const fileData = await response.json()
return {
name: filename,
description: fileData.description || 'No description'
}
} catch {
return { name: filename, description: 'No description' }
}
})
)
return {
success: true,
workflows: filesWithDesc
}
} catch (err) {
console.error('Failed to load YAML files:', err)
return {
success: false,
error: 'Failure loading YAML files, please run API service'
}
}
}
// Fetch YAML file content
export async function fetchWorkflowYAML(filename) {
try {
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(filename)}/get`))
if (!response.ok) {
throw new Error(`Failed to load YAML file: ${filename}, status: ${response.status}`)
}
const data = await response.json()
return data.content
} catch (err) {
console.error('Failed to load YAML file:', err)
throw err
}
}
// Fetch YAML for the specified workflow
export async function fetchYaml(filename) {
try {
const yamlFilename = addYamlSuffix(filename)
const response = await fetch(apiUrl(`/api/workflows/${encodeURIComponent(yamlFilename)}/get`))
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
content: data?.content
}
}
return {
success: false,
detail: data?.detail,
message: data?.message || 'Failed to fetch YAML file',
status: response.status
}
} catch (error) {
console.error('Error fetching YAML file:', error)
return {
success: false,
message: 'API error'
}
}
}
// Fetch the VueFlow graph
export async function fetchVueGraph(key) {
try {
const response = await fetch(apiUrl(`/api/vuegraphs/${encodeURIComponent(key)}`))
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
content: data?.content,
status: response.status
}
}
return {
success: false,
status: response.status,
detail: data?.detail,
message: data?.message || 'Failed to fetch VueFlow graph'
}
} catch (error) {
console.error('Error fetching VueFlow graph:', error)
return {
success: false,
message: 'API error'
}
}
}
// Save the VueFlow graph
export async function postVuegraphs({ filename, content }) {
try {
const response = await fetch(apiUrl('/api/vuegraphs/upload/content'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
filename,
content
})
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
message: data?.message || 'VueFlow graph saved successfully'
}
}
return {
success: false,
status: response.status,
detail: data?.detail,
message: data?.message || 'Failed to save VueFlow graph'
}
} catch (error) {
console.error('Error saving VueFlow graph:', error)
return {
success: false,
message: 'API error'
}
}
}
// Fetch the config schema
export async function fetchConfigSchema(breadcrumbs) {
try {
const response = await fetch(apiUrl('/api/config/schema'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
breadcrumbs: breadcrumbs
})
})
if (!response.ok) {
throw new Error(`Failed to fetch config schema: ${response.status} ${response.statusText}`)
}
const data = await response.json()
return data
} catch (error) {
console.error('Failed to fetch config schema:', error)
throw error
}
}
// Download execution logs
export async function fetchLogsZip(sessionId) {
try {
const response = await fetch(apiUrl(`/api/sessions/${encodeURIComponent(sessionId)}/download`))
if (!response.ok) {
throw new Error(`Download failed: ${response.status} ${response.statusText}`)
}
// Get zip file data
const blob = await response.blob()
const url = window.URL.createObjectURL(blob)
// Create a download link and trigger download
const link = document.createElement('a')
link.href = url
link.download = `execution_logs_${sessionId}.zip`
document.body.appendChild(link)
link.click()
// Cleanup
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
return { success: true }
} catch (error) {
console.error('Failed to download execution logs:', error)
throw error
}
}
// Fetch session attachments
export async function getAttachment(sessionId, attachmentId) {
try {
if (!sessionId) {
throw new Error('Missing session id')
}
if (!attachmentId) {
throw new Error('Missing attachment id')
}
const response = await fetch(apiUrl(`/api/sessions/${encodeURIComponent(sessionId)}/artifacts/${encodeURIComponent(attachmentId)}`))
if (!response.ok) {
throw new Error(`Failed to fetch attachment: ${response.status} ${response.statusText}`)
}
const data = await response.json()
return data?.data_uri
} catch (error) {
console.error('Failed to fetch attachment:', error)
throw error
}
}
// Batch workflow execution
export async function postBatchWorkflow({ file, sessionId, yamlFile, maxParallel, logLevel }) {
try {
const formData = new FormData()
// Append required parameters
if (file) {
formData.append('file', file)
}
if (sessionId) {
formData.append('session_id', sessionId)
}
if (yamlFile) {
formData.append('yaml_file', yamlFile)
}
if (maxParallel !== undefined) {
formData.append('max_parallel', maxParallel.toString())
}
if (logLevel) {
formData.append('log_level', logLevel)
}
const response = await fetch(apiUrl('/api/workflows/batch'), {
method: 'POST',
body: formData
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
message: data?.message || 'Batch workflow executed successfully',
...data
}
}
return {
success: false,
detail: data?.detail,
message: data?.message || 'Failed to execute batch workflow',
status: response.status
}
} catch (error) {
console.error('Error executing batch workflow:', error)
return {
success: false,
message: 'API error'
}
}
}
// Upload a binary file
export async function postFile(sessionId, file) {
try {
if (!sessionId) {
throw new Error('Missing session id')
}
if (!file) {
throw new Error('Missing file payload')
}
const formData = new FormData()
let payload = file
let filename = 'upload.bin'
if (typeof file === 'string') {
payload = new Blob([file], { type: 'application/octet-stream' })
} else if (file?.name) {
filename = file.name
}
formData.append('file', payload, filename)
const response = await fetch(apiUrl(`/api/uploads/${encodeURIComponent(sessionId)}`), {
method: 'POST',
body: formData
})
const data = await response.json().catch(() => ({}))
if (response.ok) {
return {
success: true,
message: 'File uploaded successfully',
name: data?.name,
attachmentId: data?.attachment_id,
mimeType: data?.mime_type,
size: data?.size
}
}
return {
success: false,
message: 'Failed to upload file'
}
} catch (error) {
console.error('Error uploading file:', error)
return {
success: false,
message: 'API error'
}
}
}