@@ -13,38 +13,74 @@ import {
13
13
DeleteAttachmentPayload ,
14
14
TranscriptionPayload ,
15
15
TranscriptionResponse ,
16
+ MultiSourceQueryResponse ,
16
17
} from "@/types/commands" ;
18
+ import { useAppStore } from '@/stores/appStore' ;
19
+
20
+ async function invokeWithErrorHandler < T > (
21
+ command : string ,
22
+ args ?: Record < string , any >
23
+ ) : Promise < T > {
24
+ const addError = useAppStore . getState ( ) . addError ;
25
+ try {
26
+ const result = await invoke < T > ( command , args ) ;
27
+ console . log ( command , result ) ;
28
+
29
+ if ( result && typeof result === 'object' && 'failed' in result ) {
30
+ const failedResult = result as any ;
31
+ if ( failedResult . failed ?. length > 0 ) {
32
+ failedResult . failed . forEach ( ( error : any ) => {
33
+ // addError(error.error, 'error');
34
+ console . log ( error . error ) ;
35
+ } ) ;
36
+ }
37
+ }
38
+
39
+ if ( typeof result === 'string' ) {
40
+ const res = JSON . parse ( result ) ;
41
+ if ( typeof res === 'string' ) {
42
+ throw new Error ( result ) ;
43
+ }
44
+ }
45
+
46
+ return result ;
47
+ } catch ( error : any ) {
48
+ const errorMessage = error || 'Command execution failed' ;
49
+ addError ( errorMessage , 'error' ) ;
50
+ throw error ;
51
+ }
52
+ }
17
53
18
54
export function get_server_token ( id : string ) : Promise < ServerTokenResponse > {
19
- return invoke ( `get_server_token` , { id } ) ;
55
+ return invokeWithErrorHandler ( `get_server_token` , { id } ) ;
20
56
}
21
57
22
58
export function list_coco_servers ( ) : Promise < Server [ ] > {
23
- return invoke ( `list_coco_servers` ) ;
59
+ return invokeWithErrorHandler ( `list_coco_servers` ) ;
24
60
}
25
61
26
62
export function add_coco_server ( endpoint : string ) : Promise < Server > {
27
- return invoke ( `add_coco_server` , { endpoint } ) ;
63
+ return invokeWithErrorHandler ( `add_coco_server` , { endpoint } ) ;
28
64
}
29
65
30
66
export function enable_server ( id : string ) : Promise < void > {
31
- return invoke ( `enable_server` , { id } ) ;
67
+ return invokeWithErrorHandler ( `enable_server` , { id } ) ;
32
68
}
33
69
34
70
export function disable_server ( id : string ) : Promise < void > {
35
- return invoke ( `disable_server` , { id } ) ;
71
+ return invokeWithErrorHandler ( `disable_server` , { id } ) ;
36
72
}
37
73
38
74
export function remove_coco_server ( id : string ) : Promise < void > {
39
- return invoke ( `remove_coco_server` , { id } ) ;
75
+ return invokeWithErrorHandler ( `remove_coco_server` , { id } ) ;
40
76
}
41
77
42
78
export function logout_coco_server ( id : string ) : Promise < void > {
43
- return invoke ( `logout_coco_server` , { id } ) ;
79
+ return invokeWithErrorHandler ( `logout_coco_server` , { id } ) ;
44
80
}
45
81
46
82
export function refresh_coco_server_info ( id : string ) : Promise < Server > {
47
- return invoke ( `refresh_coco_server_info` , { id } ) ;
83
+ return invokeWithErrorHandler ( `refresh_coco_server_info` , { id } ) ;
48
84
}
49
85
50
86
export function handle_sso_callback ( {
@@ -56,27 +92,27 @@ export function handle_sso_callback({
56
92
requestId : string ;
57
93
code : string ;
58
94
} ) : Promise < void > {
59
- return invoke ( `handle_sso_callback` , {
95
+ return invokeWithErrorHandler ( `handle_sso_callback` , {
60
96
serverId,
61
97
requestId,
62
98
code,
63
99
} ) ;
64
100
}
65
101
66
102
export function get_connectors_by_server ( id : string ) : Promise < Connector [ ] > {
67
- return invoke ( `get_connectors_by_server` , { id } ) ;
103
+ return invokeWithErrorHandler ( `get_connectors_by_server` , { id } ) ;
68
104
}
69
105
70
106
export function get_datasources_by_server ( id : string ) : Promise < DataSource [ ] > {
71
- return invoke ( `get_datasources_by_server` , { id } ) ;
107
+ return invokeWithErrorHandler ( `get_datasources_by_server` , { id } ) ;
72
108
}
73
109
74
110
export function connect_to_server ( id : string , clientId : string ) : Promise < void > {
75
- return invoke ( `connect_to_server` , { id, clientId } ) ;
111
+ return invokeWithErrorHandler ( `connect_to_server` , { id, clientId } ) ;
76
112
}
77
113
78
114
export function disconnect ( clientId : string ) : Promise < void > {
79
- return invoke ( `disconnect` , { clientId } ) ;
115
+ return invokeWithErrorHandler ( `disconnect` , { clientId } ) ;
80
116
}
81
117
82
118
export function chat_history ( {
@@ -90,7 +126,7 @@ export function chat_history({
90
126
size ?: number ;
91
127
query ?: string ;
92
128
} ) : Promise < string > {
93
- return invoke ( `chat_history` , {
129
+ return invokeWithErrorHandler ( `chat_history` , {
94
130
serverId,
95
131
from,
96
132
size,
@@ -109,7 +145,7 @@ export function session_chat_history({
109
145
from ?: number ;
110
146
size ?: number ;
111
147
} ) : Promise < string > {
112
- return invoke ( `session_chat_history` , {
148
+ return invokeWithErrorHandler ( `session_chat_history` , {
113
149
serverId,
114
150
sessionId,
115
151
from,
@@ -124,7 +160,7 @@ export function close_session_chat({
124
160
serverId : string ;
125
161
sessionId : string ;
126
162
} ) : Promise < string > {
127
- return invoke ( `close_session_chat` , {
163
+ return invokeWithErrorHandler ( `close_session_chat` , {
128
164
serverId,
129
165
sessionId,
130
166
} ) ;
@@ -137,7 +173,7 @@ export function open_session_chat({
137
173
serverId : string ;
138
174
sessionId : string ;
139
175
} ) : Promise < string > {
140
- return invoke ( `open_session_chat` , {
176
+ return invokeWithErrorHandler ( `open_session_chat` , {
141
177
serverId,
142
178
sessionId,
143
179
} ) ;
@@ -150,7 +186,7 @@ export function cancel_session_chat({
150
186
serverId : string ;
151
187
sessionId : string ;
152
188
} ) : Promise < string > {
153
- return invoke ( `cancel_session_chat` , {
189
+ return invokeWithErrorHandler ( `cancel_session_chat` , {
154
190
serverId,
155
191
sessionId,
156
192
} ) ;
@@ -167,7 +203,7 @@ export function new_chat({
167
203
message : string ;
168
204
queryParams ?: Record < string , any > ;
169
205
} ) : Promise < GetResponse > {
170
- return invoke ( `new_chat` , {
206
+ return invokeWithErrorHandler ( `new_chat` , {
171
207
serverId,
172
208
websocketId,
173
209
message,
@@ -188,7 +224,7 @@ export function send_message({
188
224
message : string ;
189
225
queryParams ?: Record < string , any > ;
190
226
} ) : Promise < string > {
191
- return invoke ( `send_message` , {
227
+ return invokeWithErrorHandler ( `send_message` , {
192
228
serverId,
193
229
websocketId,
194
230
sessionId,
@@ -198,7 +234,7 @@ export function send_message({
198
234
}
199
235
200
236
export const delete_session_chat = ( serverId : string , sessionId : string ) => {
201
- return invoke < boolean > ( `delete_session_chat` , { serverId, sessionId } ) ;
237
+ return invokeWithErrorHandler < boolean > ( `delete_session_chat` , { serverId, sessionId } ) ;
202
238
} ;
203
239
204
240
export const update_session_chat = ( payload : {
@@ -209,11 +245,11 @@ export const update_session_chat = (payload: {
209
245
attachments ?: string [ ] ;
210
246
} ;
211
247
} ) : Promise < boolean > => {
212
- return invoke < boolean > ( "update_session_chat" , payload ) ;
248
+ return invokeWithErrorHandler < boolean > ( "update_session_chat" , payload ) ;
213
249
} ;
214
250
215
251
export const upload_attachment = async ( payload : UploadAttachmentPayload ) => {
216
- const response = await invoke < UploadAttachmentResponse > ( "upload_attachment" , {
252
+ const response = await invokeWithErrorHandler < UploadAttachmentResponse > ( "upload_attachment" , {
217
253
...payload ,
218
254
} ) ;
219
255
@@ -223,13 +259,21 @@ export const upload_attachment = async (payload: UploadAttachmentPayload) => {
223
259
} ;
224
260
225
261
export const get_attachment = ( payload : GetAttachmentPayload ) => {
226
- return invoke < GetAttachmentResponse > ( "get_attachment" , { ...payload } ) ;
262
+ return invokeWithErrorHandler < GetAttachmentResponse > ( "get_attachment" , { ...payload } ) ;
227
263
} ;
228
264
229
265
export const delete_attachment = ( payload : DeleteAttachmentPayload ) => {
230
- return invoke < boolean > ( "delete_attachment" , { ...payload } ) ;
266
+ return invokeWithErrorHandler < boolean > ( "delete_attachment" , { ...payload } ) ;
231
267
} ;
232
268
233
269
export const transcription = ( payload : TranscriptionPayload ) => {
234
- return invoke < TranscriptionResponse > ( "transcription" , { ...payload } ) ;
270
+ return invokeWithErrorHandler < TranscriptionResponse > ( "transcription" , { ...payload } ) ;
271
+ } ;
272
+
273
+ export const query_coco_fusion = ( payload : {
274
+ from : number ;
275
+ size : number ;
276
+ query_strings : Record < string , string > ;
277
+ } ) => {
278
+ return invokeWithErrorHandler < MultiSourceQueryResponse > ( "query_coco_fusion" , { ...payload } ) ;
235
279
} ;
0 commit comments