-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathchat.ts
More file actions
341 lines (331 loc) · 9.29 KB
/
chat.ts
File metadata and controls
341 lines (331 loc) · 9.29 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
import { request } from '@/utils/request'
import { getDate } from '@/utils/utils.ts'
export const questionApi = {
pager: (pageNumber: number, pageSize: number) =>
request.get(`/chat/question/pager/${pageNumber}/${pageSize}`),
/* add: (data: any) => new Promise((resolve, reject) => {
request.post('/chat/question', data, { responseType: 'stream', timeout: 0, onDownloadProgress: p => {
resolve(p)
}}).catch(e => reject(e))
}), */
// add: (data: any) => request.post('/chat/question', data),
add: (data: any, controller?: AbortController) =>
request.fetchStream('/chat/question', data, controller),
edit: (data: any) => request.put('/chat/question', data),
delete: (id: number) => request.delete(`/chat/question/${id}`),
query: (id: number) => request.get(`/chat/question/${id}`),
}
export interface ChatMessage {
role: 'user' | 'assistant'
create_time?: Date | string
content?: string | number
record?: ChatRecord
isTyping?: boolean
first_chat?: boolean
recommended_question?: string
index: number
}
export class ChatRecord {
id?: number
chat_id?: number
create_time?: Date | string
finish_time?: Date | string
question?: string
sql_answer?: string
sql?: string
data?: string | any
chart_answer?: string
chart?: string
analysis?: string
analysis_thinking?: string
predict?: string
predict_content?: string
predict_data?: string | any
finish?: boolean = false
error?: string
run_time: number = 0
first_chat: boolean = false
recommended_question?: string
analysis_record_id?: number
predict_record_id?: number
sql_prase?: string
constructor()
constructor(
id: number,
chat_id: number,
create_time: Date | string,
finish_time: Date | string | undefined,
question: string,
sql_answer: string | undefined,
sql: string | undefined,
data: string | any | undefined,
chart_answer: string | undefined,
chart: string | undefined,
analysis: string | undefined,
analysis_thinking: string | undefined,
predict: string | undefined,
predict_content: string | undefined,
predict_data: string | any | undefined,
finish: boolean,
error: string | undefined,
run_time: number,
first_chat: boolean,
recommended_question: string | undefined,
analysis_record_id: number | undefined,
predict_record_id: number | undefined
)
constructor(
id?: number,
chat_id?: number,
create_time?: Date | string,
finish_time?: Date | string,
question?: string,
sql_answer?: string,
sql?: string,
data?: string | any,
chart_answer?: string,
chart?: string,
analysis?: string,
analysis_thinking?: string,
predict?: string,
predict_content?: string,
predict_data?: string | any,
finish?: boolean,
error?: string,
run_time?: number,
first_chat?: boolean,
recommended_question?: string,
analysis_record_id?: number,
predict_record_id?: number
) {
this.id = id
this.chat_id = chat_id
this.create_time = getDate(create_time)
this.finish_time = getDate(finish_time)
this.question = question
this.sql_answer = sql_answer
this.sql = sql
this.data = data
this.chart_answer = chart_answer
this.chart = chart
this.analysis = analysis
this.analysis_thinking = analysis_thinking
this.predict = predict
this.predict_content = predict_content
this.predict_data = predict_data
this.finish = !!finish
this.error = error
this.run_time = run_time ?? 0
this.first_chat = !!first_chat
this.recommended_question = recommended_question
this.analysis_record_id = analysis_record_id
this.predict_record_id = predict_record_id
}
}
export class Chat {
id?: number
create_time?: Date | string
create_by?: number
brief?: string
chat_type?: string
datasource?: number
engine_type?: string
ds_type?: string
constructor()
constructor(
id: number,
create_time: Date | string,
create_by: number,
brief: string,
chat_type: string,
datasource: number,
engine_type: string
)
constructor(
id?: number,
create_time?: Date | string,
create_by?: number,
brief?: string,
chat_type?: string,
datasource?: number,
engine_type?: string
) {
this.id = id
this.create_time = getDate(create_time)
this.create_by = create_by
this.brief = brief
this.chat_type = chat_type
this.datasource = datasource
this.engine_type = engine_type
}
}
export class ChatInfo extends Chat {
datasource_name?: string
datasource_exists: boolean = true
records: Array<ChatRecord> = []
constructor()
constructor(chat: Chat)
constructor(
id: number,
create_time: Date | string,
create_by: number,
brief: string,
chat_type: string,
datasource: number,
engine_type: string,
ds_type: string,
datasource_name: string,
datasource_exists: boolean,
records: Array<ChatRecord>
)
constructor(
param1?: number | Chat,
create_time?: Date | string,
create_by?: number,
brief?: string,
chat_type?: string,
datasource?: number,
engine_type?: string,
ds_type?: string,
datasource_name?: string,
datasource_exists: boolean = true,
records: Array<ChatRecord> = []
) {
super()
if (param1 !== undefined) {
if (param1 instanceof Chat) {
this.id = param1.id
this.create_time = getDate(param1.create_time)
this.create_by = param1.create_by
this.brief = param1.brief
this.chat_type = param1.chat_type
this.datasource = param1.datasource
this.engine_type = param1.engine_type
this.ds_type = param1.ds_type
} else {
this.id = param1
this.create_time = getDate(create_time)
this.create_by = create_by
this.brief = brief
this.chat_type = chat_type
this.datasource = datasource
this.engine_type = engine_type
this.ds_type = ds_type
}
}
this.datasource_name = datasource_name
this.datasource_exists = datasource_exists
this.records = records
}
}
const toChatRecord = (data?: any): ChatRecord | undefined => {
if (!data) {
return undefined
}
return new ChatRecord(
data.id,
data.chat_id,
data.create_time,
data.finish_time,
data.question,
data.sql_answer,
data.sql,
data.data,
data.chart_answer,
data.chart,
data.analysis,
data.analysis_thinking,
data.predict,
data.predict_content,
data.predict_data,
data.finish,
data.error,
data.run_time,
data.first_chat,
data.recommended_question,
data.analysis_record_id,
data.predict_record_id
)
}
const toChatRecordList = (list: any = []): ChatRecord[] => {
const records: Array<ChatRecord> = []
for (let i = 0; i < list.length; i++) {
const record = toChatRecord(list[i])
if (record) {
records.push(record)
}
}
return records
}
export const chatApi = {
toChatInfo: (data?: any): ChatInfo | undefined => {
if (!data) {
return undefined
}
return new ChatInfo(
data.id,
data.create_time,
data.create_by,
data.brief,
data.chat_type,
data.datasource,
data.engine_type,
data.ds_type,
data.datasource_name,
data.datasource_exists,
toChatRecordList(data.records)
)
},
toChatInfoList: (list: any[] = []): ChatInfo[] => {
const infos: Array<ChatInfo> = []
for (let i = 0; i < list.length; i++) {
const chatInfo = chatApi.toChatInfo(list[i])
if (chatInfo) {
infos.push(chatInfo)
}
}
return infos
},
list: (): Promise<Array<ChatInfo>> => {
return request.get('/chat/list')
},
get: (id: number): Promise<ChatInfo> => {
return request.get(`/chat/${id}`)
},
get_with_Data: (id: number): Promise<ChatInfo> => {
return request.get(`/chat/${id}/with_data`)
},
get_chart_data: (record_id?: number): Promise<any> => {
return request.get(`/chat/record/${record_id}/data`)
},
get_chart_predict_data: (record_id?: number): Promise<any> => {
return request.get(`/chat/record/${record_id}/predict_data`)
},
startChat: (data: any): Promise<ChatInfo> => {
return request.post('/chat/start', data)
},
startAssistantChat: (): Promise<ChatInfo> => {
return request.post('/chat/assistant/start')
},
renameChat: (chat_id: number | undefined, brief: string): Promise<string> => {
return request.post('/chat/rename', { id: chat_id, brief: brief })
},
deleteChat: (id: number | undefined): Promise<string> => {
return request.delete(`/chat/${id}`)
},
analysis: (record_id: number | undefined, controller?: AbortController) => {
return request.fetchStream(`/chat/record/${record_id}/analysis`, {}, controller)
},
predict: (record_id: number | undefined, controller?: AbortController) => {
return request.fetchStream(`/chat/record/${record_id}/predict`, {}, controller)
},
recommendQuestions: (record_id: number | undefined, controller?: AbortController) => {
return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}, controller)
},
checkLLMModel: () => request.get('/system/aimodel/default', { requestOptions: { silent: true } }),
export2Excel: (data: any) =>
request.post('/chat/excel/export', data, {
responseType: 'blob',
requestOptions: { customError: true },
}),
}