-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Expand file tree
/
Copy pathexportImport.js
More file actions
151 lines (139 loc) · 4.8 KB
/
exportImport.js
File metadata and controls
151 lines (139 loc) · 4.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
import { getErrorMessage } from './errorHandler'
import { generateExportFlowData } from './genericHelper'
const sanitizeTool = (Tool) => {
try {
return Tool.map((tool) => {
return {
id: tool.id,
name: tool.name,
description: tool.description,
color: tool.color,
iconSrc: tool.iconSrc,
schema: tool.schema,
func: tool.func
}
})
} catch (error) {
throw new Error(`exportImport.sanitizeTool ${getErrorMessage(error)}`)
}
}
const CHATFLOW_FIELDS_TO_PRESERVE = [
'chatbotConfig',
'category',
'speechToText',
'textToSpeech',
'followUpPrompts',
'apiConfig',
'analytic',
'isPublic',
'apikeyid',
'mcpServerConfig'
]
const sanitizeChatflow = (ChatFlow) => {
try {
return ChatFlow.map((chatFlow) => {
const sanitizeFlowData = generateExportFlowData(JSON.parse(chatFlow.flowData))
const sanitized = {
id: chatFlow.id,
name: chatFlow.name,
flowData: stringify(sanitizeFlowData),
type: chatFlow.type
}
CHATFLOW_FIELDS_TO_PRESERVE.forEach((field) => {
if (chatFlow[field] != null) {
sanitized[field] = chatFlow[field]
}
})
return sanitized
})
} catch (error) {
throw new Error(`exportImport.sanitizeChatflow ${getErrorMessage(error)}`)
}
}
const sanitizeVariable = (Variable) => {
try {
return Variable.map((variable) => {
return {
id: variable.id,
name: variable.name,
value: variable.value,
type: variable.type
}
})
} catch (error) {
throw new Error(`exportImport.sanitizeVariable ${getErrorMessage(error)}`)
}
}
const sanitizeAssistant = (Assistant) => {
try {
return Assistant.map((assistant) => {
return {
id: assistant.id,
details: assistant.details,
credential: assistant.credential,
iconSrc: assistant.iconSrc,
type: assistant.type
}
})
} catch (error) {
throw new Error(`exportImport.sanitizeAssistant ${getErrorMessage(error)}`)
}
}
const sanitizeCustomTemplate = (CustomTemplate) => {
try {
return CustomTemplate.map((customTemplate) => {
return { ...customTemplate, usecases: JSON.stringify(customTemplate.usecases), workspaceId: undefined }
})
} catch (error) {
throw new Error(`exportImport.sanitizeCustomTemplate ${getErrorMessage(error)}`)
}
}
const sanitizeDocumentStore = (DocumentStore) => {
try {
return DocumentStore.map((documentStore) => {
return { ...documentStore, workspaceId: undefined }
})
} catch (error) {
throw new Error(`exportImport.sanitizeDocumentStore ${getErrorMessage(error)}`)
}
}
const sanitizeExecution = (Execution) => {
try {
return Execution.map((execution) => {
if (execution.agentflow) execution.agentflow.workspaceId = undefined
return { ...execution, workspaceId: undefined }
})
} catch (error) {
throw new Error(`exportImport.sanitizeExecution ${getErrorMessage(error)}`)
}
}
export const stringify = (object) => {
try {
return JSON.stringify(object, null, 2)
} catch (error) {
throw new Error(`exportImport.stringify ${getErrorMessage(error)}`)
}
}
export const exportData = (exportAllData) => {
try {
return {
AgentFlow: sanitizeChatflow(exportAllData.AgentFlow),
AgentFlowV2: sanitizeChatflow(exportAllData.AgentFlowV2),
AssistantFlow: sanitizeChatflow(exportAllData.AssistantFlow),
AssistantCustom: sanitizeAssistant(exportAllData.AssistantCustom),
AssistantOpenAI: sanitizeAssistant(exportAllData.AssistantOpenAI),
AssistantAzure: sanitizeAssistant(exportAllData.AssistantAzure),
ChatFlow: sanitizeChatflow(exportAllData.ChatFlow),
ChatMessage: exportAllData.ChatMessage,
ChatMessageFeedback: exportAllData.ChatMessageFeedback,
CustomTemplate: sanitizeCustomTemplate(exportAllData.CustomTemplate),
DocumentStore: sanitizeDocumentStore(exportAllData.DocumentStore),
DocumentStoreFileChunk: exportAllData.DocumentStoreFileChunk,
Execution: sanitizeExecution(exportAllData.Execution),
Tool: sanitizeTool(exportAllData.Tool),
Variable: sanitizeVariable(exportAllData.Variable)
}
} catch (error) {
throw new Error(`exportImport.exportData ${getErrorMessage(error)}`)
}
}