@core-pilot/sdk 0.0.0-beta.9 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.css +1 -1
- package/dist/index.js +592 -337
- package/dist/index.umd.cjs +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createApp, createVNode, defineComponent, onBeforeUnmount, ref, watch } from "vue";
|
|
1
|
+
import { createApp, createVNode, defineComponent, onBeforeUnmount, ref, useTemplateRef, watch } from "vue";
|
|
2
2
|
import { BubbleList, EditorSender } from "@core-pilot/client-vue";
|
|
3
3
|
import { fetchEventSource } from "@microsoft/fetch-event-source";
|
|
4
4
|
import axios from "axios";
|
|
@@ -13,153 +13,193 @@ function formatArgs(e) {
|
|
|
13
13
|
}
|
|
14
14
|
}).join(" ");
|
|
15
15
|
}
|
|
16
|
-
function createLogger(e,
|
|
17
|
-
let
|
|
18
|
-
if (!
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
navigator.sendBeacon ? navigator.sendBeacon(
|
|
16
|
+
function createLogger(e, o, s) {
|
|
17
|
+
let c = "[CorePilot]", l = [], u = null, d = s?.batchSize ?? 20, f = s?.flushInterval ?? 15e3, p = () => {
|
|
18
|
+
if (!s || l.length === 0) return;
|
|
19
|
+
let o = [...l];
|
|
20
|
+
l = [], e && console.log(`${c} Uploading ${o.length} log(s) to ${s.url}`);
|
|
21
|
+
let u = JSON.stringify(o);
|
|
22
|
+
navigator.sendBeacon ? navigator.sendBeacon(s.url, u) : fetch(s.url, {
|
|
23
23
|
method: "POST",
|
|
24
|
-
body:
|
|
24
|
+
body: u,
|
|
25
25
|
headers: { "Content-Type": "application/json" },
|
|
26
26
|
keepalive: !0
|
|
27
|
-
}).catch((
|
|
28
|
-
e && (console.error(`${
|
|
27
|
+
}).catch((s) => {
|
|
28
|
+
e && (console.error(`${c} Log upload failed:`, s), l.unshift(...o));
|
|
29
29
|
});
|
|
30
|
-
},
|
|
31
|
-
|
|
30
|
+
}, m = (e, ...c) => {
|
|
31
|
+
s && (l.push({
|
|
32
32
|
level: e,
|
|
33
33
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
34
|
-
message: formatArgs(
|
|
35
|
-
appId:
|
|
36
|
-
}),
|
|
34
|
+
message: formatArgs(c),
|
|
35
|
+
appId: o
|
|
36
|
+
}), l.length >= d && (u && clearTimeout(u), p(), u = window.setTimeout(p, f)));
|
|
37
37
|
};
|
|
38
|
-
return
|
|
39
|
-
log(...
|
|
40
|
-
e && console.log(
|
|
38
|
+
return s && (u = window.setTimeout(p, f)), {
|
|
39
|
+
log(...o) {
|
|
40
|
+
e && console.log(c, ...o), m("log", ...o);
|
|
41
41
|
},
|
|
42
|
-
error(...
|
|
43
|
-
e && console.error(
|
|
42
|
+
error(...o) {
|
|
43
|
+
e && console.error(c, ...o), m("error", ...o);
|
|
44
44
|
},
|
|
45
45
|
destroy() {
|
|
46
|
-
|
|
46
|
+
u && clearTimeout(u), p();
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function createAuthManager(e,
|
|
51
|
-
let
|
|
52
|
-
return {
|
|
53
|
-
|
|
54
|
-
if (!
|
|
55
|
-
return
|
|
50
|
+
function createAuthManager(e, o) {
|
|
51
|
+
let s = e.token || "", c = null;
|
|
52
|
+
return {
|
|
53
|
+
async getToken() {
|
|
54
|
+
if (!s) throw Error("AuthManager: Missing token");
|
|
55
|
+
return s;
|
|
56
|
+
},
|
|
57
|
+
async setToken(e) {
|
|
58
|
+
return e ? (s = e, c && c(), !0) : !1;
|
|
59
|
+
},
|
|
60
|
+
onTokenRefreshed(e) {
|
|
61
|
+
c = e;
|
|
62
|
+
},
|
|
63
|
+
isServerMode() {
|
|
64
|
+
return e.mode === "server";
|
|
65
|
+
},
|
|
66
|
+
getAppId() {
|
|
67
|
+
if (!o) throw Error("AuthManager: Missing appId");
|
|
68
|
+
return o;
|
|
56
69
|
}
|
|
57
|
-
|
|
58
|
-
e("9269ca5c-5b35-46de-915f-b2e3ff41da13");
|
|
59
|
-
}, 1e3)), c;
|
|
60
|
-
throw Error("AuthManager: Invalid configuration or missing token.");
|
|
61
|
-
} };
|
|
70
|
+
};
|
|
62
71
|
}
|
|
63
72
|
var createStoreImpl = (e) => {
|
|
64
|
-
let
|
|
65
|
-
let
|
|
66
|
-
if (!Object.is(
|
|
67
|
-
let e =
|
|
68
|
-
|
|
73
|
+
let o, s = /* @__PURE__ */ new Set(), c = (e, c) => {
|
|
74
|
+
let l = typeof e == "function" ? e(o) : e;
|
|
75
|
+
if (!Object.is(l, o)) {
|
|
76
|
+
let e = o;
|
|
77
|
+
o = c ?? (typeof l != "object" || !l) ? l : Object.assign({}, o, l), s.forEach((s) => s(o, e));
|
|
69
78
|
}
|
|
70
|
-
},
|
|
71
|
-
setState:
|
|
72
|
-
getState:
|
|
73
|
-
getInitialState: () =>
|
|
74
|
-
subscribe: (e) => (
|
|
75
|
-
},
|
|
76
|
-
return
|
|
79
|
+
}, l = () => o, u = {
|
|
80
|
+
setState: c,
|
|
81
|
+
getState: l,
|
|
82
|
+
getInitialState: () => d,
|
|
83
|
+
subscribe: (e) => (s.add(e), () => s.delete(e))
|
|
84
|
+
}, d = o = e(c, l, u);
|
|
85
|
+
return u;
|
|
77
86
|
};
|
|
78
87
|
const pilotStore = ((e) => e ? createStoreImpl(e) : createStoreImpl)((e) => ({
|
|
79
88
|
currentMessage: void 0,
|
|
80
89
|
isLoading: !1,
|
|
81
90
|
messageHistory: [],
|
|
82
91
|
activeScence: "ANC",
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
pendingRetryRequest: null,
|
|
93
|
+
setActiveScence: (o) => e({ activeScence: o }),
|
|
94
|
+
updateMessage: (o) => e((e) => {
|
|
95
|
+
let s = {
|
|
96
|
+
...o,
|
|
87
97
|
role: "ai",
|
|
88
98
|
showActions: !1,
|
|
89
99
|
maxWidth: "100%"
|
|
90
|
-
},
|
|
91
|
-
if (
|
|
100
|
+
}, c = [...e.messageHistory];
|
|
101
|
+
if (c.forEach((e) => {
|
|
92
102
|
e.isHistoryMessage = !0;
|
|
93
103
|
}), e.currentMessage) {
|
|
94
|
-
let
|
|
95
|
-
...
|
|
96
|
-
content: e.currentMessage.content + (
|
|
104
|
+
let l = {
|
|
105
|
+
...s,
|
|
106
|
+
content: e.currentMessage.content + (o.content || "")
|
|
97
107
|
};
|
|
98
|
-
return
|
|
99
|
-
currentMessage:
|
|
100
|
-
messageHistory:
|
|
108
|
+
return c.pop(), c.push(l), {
|
|
109
|
+
currentMessage: l,
|
|
110
|
+
messageHistory: c
|
|
101
111
|
};
|
|
102
112
|
}
|
|
103
|
-
return
|
|
104
|
-
currentMessage:
|
|
105
|
-
messageHistory:
|
|
113
|
+
return c.push(s), {
|
|
114
|
+
currentMessage: s,
|
|
115
|
+
messageHistory: c
|
|
106
116
|
};
|
|
107
117
|
}),
|
|
108
|
-
setLoading: (
|
|
118
|
+
setLoading: (o) => e({ isLoading: o }),
|
|
109
119
|
resetCurrentMessage: () => e({ currentMessage: void 0 }),
|
|
110
|
-
resetMessageHistory: (
|
|
111
|
-
|
|
112
|
-
let
|
|
113
|
-
return
|
|
114
|
-
e.
|
|
115
|
-
}),
|
|
116
|
-
})
|
|
120
|
+
resetMessageHistory: (o) => e({ messageHistory: o || [] }),
|
|
121
|
+
resetQuestions: () => e((e) => {
|
|
122
|
+
let o = [...e.messageHistory];
|
|
123
|
+
return o.forEach((e) => {
|
|
124
|
+
e.questions = [];
|
|
125
|
+
}), { messageHistory: o };
|
|
126
|
+
}),
|
|
127
|
+
updateMessageHistory: (o) => e((e) => {
|
|
128
|
+
let s = [...e.messageHistory];
|
|
129
|
+
return s.forEach((e) => {
|
|
130
|
+
e.isHistoryMessage = !0, e.questions = [];
|
|
131
|
+
}), Array.isArray(o) ? { messageHistory: [...s, ...o] } : { messageHistory: [...s, o] };
|
|
132
|
+
}),
|
|
133
|
+
updateSuggestionQuestions: (o) => e((e) => {
|
|
134
|
+
let s = [...e.messageHistory];
|
|
135
|
+
return s[s.length - 1].questions = o, { messageHistory: s };
|
|
136
|
+
}),
|
|
137
|
+
setPendingRetryRequest: (o) => {
|
|
138
|
+
e({ pendingRetryRequest: o });
|
|
139
|
+
},
|
|
140
|
+
clearPendingRetryRequest: () => {
|
|
141
|
+
e({ pendingRetryRequest: null });
|
|
142
|
+
}
|
|
117
143
|
}));
|
|
118
|
-
var MAX_RETRY_TIME = 0,
|
|
144
|
+
var MAX_RETRY_TIME = 0, FatalError = class extends Error {
|
|
119
145
|
constructor(e) {
|
|
120
146
|
super(e), this.name = "FatalError";
|
|
121
147
|
}
|
|
122
148
|
};
|
|
123
|
-
const sseRequester = function(e, s) {
|
|
124
|
-
let { url: c, config: l = {} } = e, u = new AbortController();
|
|
125
|
-
function
|
|
126
|
-
|
|
149
|
+
const sseRequester = function(e, o, s) {
|
|
150
|
+
let { url: c, config: l = {} } = e, u = new AbortController(), d = !1, f = !1;
|
|
151
|
+
function p() {
|
|
152
|
+
u.abort(), o.onCancel(pilotStore.getState().currentMessage, u), pilotStore.getState().setLoading(!1), pilotStore.getState().resetCurrentMessage();
|
|
127
153
|
}
|
|
128
|
-
function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
154
|
+
function h(e) {
|
|
155
|
+
try {
|
|
156
|
+
let s = o.onMessage(e);
|
|
157
|
+
if (!s) return;
|
|
158
|
+
s.event === "message" && (pilotStore.getState().updateMessage({
|
|
159
|
+
taskId: s.task_id,
|
|
160
|
+
messageId: s.message_id,
|
|
161
|
+
conversationId: s.conversation_id,
|
|
162
|
+
role: "ai",
|
|
163
|
+
content: s.answer
|
|
164
|
+
}), pilotStore.getState().resetQuestions());
|
|
165
|
+
} catch (e) {
|
|
166
|
+
throw _(), e;
|
|
167
|
+
}
|
|
137
168
|
}
|
|
138
|
-
function
|
|
139
|
-
|
|
169
|
+
async function g(e) {
|
|
170
|
+
try {
|
|
171
|
+
await o.onOpen(e), pilotStore.getState().setLoading(!0);
|
|
172
|
+
} catch (o) {
|
|
173
|
+
throw (e.status === 500 || o instanceof Error && o.message === "Token expired") && await _(), o;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async function _() {
|
|
177
|
+
d || (d = !0, pilotStore.getState().setLoading(!1), await o.onClose());
|
|
140
178
|
}
|
|
141
|
-
function
|
|
142
|
-
|
|
179
|
+
async function v(e) {
|
|
180
|
+
f || (f = !0, d || await o.onError(e));
|
|
143
181
|
}
|
|
144
|
-
let
|
|
145
|
-
return fetchEventSource(c, {
|
|
182
|
+
let y = MAX_RETRY_TIME, S = {
|
|
146
183
|
...l,
|
|
147
184
|
signal: u.signal,
|
|
148
185
|
openWhenHidden: !0,
|
|
149
|
-
onmessage:
|
|
150
|
-
onclose:
|
|
186
|
+
onmessage: h,
|
|
187
|
+
onclose: async () => await _(),
|
|
151
188
|
onopen: async (e) => {
|
|
152
|
-
|
|
153
|
-
if (!s || !s.includes(EventStreamContentType)) throw Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${s}`);
|
|
154
|
-
p();
|
|
189
|
+
await g(e);
|
|
155
190
|
},
|
|
156
191
|
onerror: (e) => {
|
|
157
|
-
if (
|
|
158
|
-
|
|
192
|
+
if (e instanceof FatalError) throw _(), e;
|
|
193
|
+
if (y--, y < 0) {
|
|
194
|
+
if (_(), e instanceof TypeError) throw new FatalError(e.message);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
v(e);
|
|
159
198
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
199
|
+
};
|
|
200
|
+
return s?.fetchFn && (S.fetch = s.fetchFn), fetchEventSource(c, S).catch((e) => {
|
|
201
|
+
_(), v(e);
|
|
202
|
+
}), { cancel: p };
|
|
163
203
|
}, CorePilotHost = /* @__PURE__ */ defineComponent({
|
|
164
204
|
props: {
|
|
165
205
|
adapter: {
|
|
@@ -177,82 +217,133 @@ const sseRequester = function(e, s) {
|
|
|
177
217
|
provider: {
|
|
178
218
|
type: Object,
|
|
179
219
|
required: !0
|
|
220
|
+
},
|
|
221
|
+
authManager: {
|
|
222
|
+
type: Object,
|
|
223
|
+
required: !0
|
|
224
|
+
},
|
|
225
|
+
appId: {
|
|
226
|
+
type: String,
|
|
227
|
+
required: !0
|
|
180
228
|
}
|
|
181
229
|
},
|
|
182
230
|
setup(e) {
|
|
183
|
-
let s = ref([]),
|
|
184
|
-
|
|
185
|
-
|
|
231
|
+
let o = useTemplateRef("bubbleList"), s = ref([]), f = ref([]), p = ref(!1), m = ref(), h = ref("ANC"), g = ref(), _ = ref("");
|
|
232
|
+
watch(() => e.authManager, async (e) => {
|
|
233
|
+
_.value = await e.getToken();
|
|
234
|
+
}, { immediate: !0 });
|
|
235
|
+
let v = pilotStore.subscribe((e) => {
|
|
236
|
+
let { isLoading: o, messageHistory: s } = e;
|
|
237
|
+
f.value = s, p.value = o, h.value = e.activeScence;
|
|
186
238
|
});
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
239
|
+
watch(() => p.value, (o) => {
|
|
240
|
+
o && (g.value = void 0, e.hitlManager.submit({ hitlData: {
|
|
241
|
+
action: "cust_ai_output_start",
|
|
242
|
+
payload: {}
|
|
243
|
+
} }));
|
|
244
|
+
}), watch(() => g.value, (o, s) => {
|
|
245
|
+
o && f.value[f.value.length - 1]?.role === "ai" && e.hitlManager.submit({ hitlData: {
|
|
191
246
|
action: "cust_ai_output_finish",
|
|
192
247
|
payload: {
|
|
193
|
-
refConfig:
|
|
194
|
-
conversationId:
|
|
248
|
+
refConfig: g.value,
|
|
249
|
+
conversationId: f.value[f.value.length - 1]?.conversationId,
|
|
250
|
+
messageId: [...f.value].reverse().find((e) => e.messageId)?.messageId
|
|
195
251
|
}
|
|
196
252
|
} });
|
|
197
|
-
})
|
|
198
|
-
|
|
253
|
+
});
|
|
254
|
+
let y = async (o, s = !1) => {
|
|
255
|
+
let c = {
|
|
256
|
+
messageId: void 0,
|
|
257
|
+
content: o.text,
|
|
258
|
+
role: "user",
|
|
259
|
+
placement: "end",
|
|
260
|
+
showActions: !1,
|
|
261
|
+
maxWidth: "100%"
|
|
262
|
+
};
|
|
263
|
+
C();
|
|
264
|
+
let l = f.value[f.value.length - 1];
|
|
265
|
+
s || (pilotStore.getState().updateMessageHistory([c]), pilotStore.getState().setPendingRetryRequest({
|
|
266
|
+
type: "submit_message",
|
|
267
|
+
payload: {
|
|
268
|
+
text: o.text,
|
|
269
|
+
inputs: { scence: h.value },
|
|
270
|
+
file: o.file?.[0]
|
|
271
|
+
}
|
|
272
|
+
})), pilotStore.getState().resetCurrentMessage();
|
|
273
|
+
let u = await e.adapter.formatRequest({
|
|
274
|
+
text: o.text,
|
|
275
|
+
prevMessage: l,
|
|
276
|
+
inputs: { scence: h.value },
|
|
277
|
+
files: o.file
|
|
278
|
+
}), d = e.authManager.isServerMode() ? e.adapter.createSseFetch?.() : void 0;
|
|
279
|
+
m.value = sseRequester(u, e.adapter, {
|
|
280
|
+
fetchFn: d,
|
|
281
|
+
isRetry: s
|
|
282
|
+
}).cancel;
|
|
283
|
+
}, b = () => {
|
|
284
|
+
e.logger.log("Cancel handler called"), m.value?.(), pilotStore.getState().resetCurrentMessage();
|
|
285
|
+
}, x = async (o, s) => {
|
|
286
|
+
pilotStore.getState().resetCurrentMessage(), pilotStore.getState().updateMessageHistory([]), C(), await e.hitlManager.submit({
|
|
287
|
+
hitlData: o,
|
|
288
|
+
prevMessage: s,
|
|
289
|
+
inputs: { scence: h.value }
|
|
290
|
+
});
|
|
291
|
+
}, C = () => {
|
|
292
|
+
o.value?.scrollToBottom();
|
|
293
|
+
};
|
|
294
|
+
return onBeforeUnmount(() => {
|
|
295
|
+
v?.();
|
|
199
296
|
}), {
|
|
200
|
-
messages:
|
|
201
|
-
isLoading:
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
showActions: !1,
|
|
209
|
-
maxWidth: "100%"
|
|
210
|
-
}, u = c.value[c.value.length - 1];
|
|
211
|
-
pilotStore.getState().updateMessageHistory([l]), pilotStore.getState().resetCurrentMessage(), p.value = sseRequester(await e.adapter.formatReuqest({
|
|
212
|
-
text: s.text,
|
|
213
|
-
prevMessage: u,
|
|
214
|
-
inputs: { scence: m.value }
|
|
215
|
-
}), e.adapter).cancel;
|
|
216
|
-
},
|
|
217
|
-
cancelHandler: () => {
|
|
218
|
-
e.logger.log("Cancel handler called"), p.value?.(), pilotStore.getState().resetCurrentMessage();
|
|
219
|
-
},
|
|
220
|
-
handleScenceChange: (s) => {
|
|
297
|
+
messages: f,
|
|
298
|
+
isLoading: p,
|
|
299
|
+
authToken: _,
|
|
300
|
+
activeScence: h,
|
|
301
|
+
tagList: s,
|
|
302
|
+
handleSendMessage: y,
|
|
303
|
+
cancelHandler: b,
|
|
304
|
+
handleScenceChange: (o) => {
|
|
221
305
|
e.hitlManager.submit({ hitlData: {
|
|
222
306
|
action: "cust_scence_change",
|
|
223
|
-
payload: { scence:
|
|
307
|
+
payload: { scence: o.id }
|
|
224
308
|
} });
|
|
225
309
|
},
|
|
226
|
-
|
|
227
|
-
coreSubmit: async (s, c) => {
|
|
228
|
-
pilotStore.getState().resetCurrentMessage(), await e.hitlManager.submit({
|
|
229
|
-
hitlData: s,
|
|
230
|
-
prevMessage: c,
|
|
231
|
-
inputs: { scence: m.value }
|
|
232
|
-
});
|
|
233
|
-
},
|
|
310
|
+
coreSubmit: x,
|
|
234
311
|
handleRefConfigUpdate: (e) => {
|
|
235
|
-
|
|
312
|
+
g.value = e;
|
|
236
313
|
},
|
|
237
|
-
activeScence: m,
|
|
238
314
|
handleSceneUpdate: (e) => {
|
|
239
315
|
pilotStore.getState().setActiveScence(e);
|
|
316
|
+
},
|
|
317
|
+
handleQuestionClick: (e) => {
|
|
318
|
+
y({
|
|
319
|
+
text: e,
|
|
320
|
+
file: []
|
|
321
|
+
});
|
|
240
322
|
}
|
|
241
323
|
};
|
|
242
324
|
},
|
|
243
325
|
render() {
|
|
244
326
|
return createVNode("div", { class: "core-pilot-host" }, [createVNode(BubbleList, {
|
|
327
|
+
ref: "bubbleList",
|
|
245
328
|
class: "bubble-list",
|
|
246
329
|
list: this.messages,
|
|
247
330
|
maxHeight: "100%",
|
|
331
|
+
user: this.provider.extensions.user,
|
|
332
|
+
authToken: this.authManager.getToken(),
|
|
333
|
+
appId: this.appId,
|
|
248
334
|
btnLoading: this.isLoading,
|
|
249
335
|
onCoreSubmit: this.coreSubmit,
|
|
250
|
-
onRefConfigUpdate: this.handleRefConfigUpdate
|
|
336
|
+
onRefConfigUpdate: this.handleRefConfigUpdate,
|
|
337
|
+
onQuestionClick: this.handleQuestionClick
|
|
251
338
|
}, null), createVNode(EditorSender, {
|
|
252
339
|
tagList: this.tagList,
|
|
253
340
|
loading: this.isLoading,
|
|
254
341
|
agentName: "",
|
|
255
342
|
scene: this.activeScence,
|
|
343
|
+
user: this.provider.extensions.user,
|
|
344
|
+
authToken: this.authToken,
|
|
345
|
+
appId: this.appId,
|
|
346
|
+
showUpload: !1,
|
|
256
347
|
onSubmit: this.handleSendMessage,
|
|
257
348
|
onCancel: this.cancelHandler,
|
|
258
349
|
onSceneChange: this.handleScenceChange,
|
|
@@ -260,36 +351,38 @@ const sseRequester = function(e, s) {
|
|
|
260
351
|
}, null)]);
|
|
261
352
|
}
|
|
262
353
|
});
|
|
263
|
-
function createMountManager(s, c, l, u
|
|
264
|
-
let f = null, p =
|
|
265
|
-
let e = (
|
|
354
|
+
function createMountManager(o, s, c, l, u) {
|
|
355
|
+
let d = null, f = null, p = { ...o.mount }, m = { ...o.provider }, h = o.appId, g = () => {
|
|
356
|
+
let e = (p.container || document).querySelector(p.selector);
|
|
266
357
|
if (e && !e.querySelector("[data-corepilot-host]")) {
|
|
267
|
-
let
|
|
268
|
-
return
|
|
358
|
+
let o = document.createElement("div");
|
|
359
|
+
return o.setAttribute("data-corepilot-host", "true"), o.style.width = "100%", o.style.height = "100%", e.appendChild(o), o;
|
|
269
360
|
}
|
|
270
361
|
return e?.querySelector("[data-corepilot-host]");
|
|
271
362
|
};
|
|
272
363
|
return {
|
|
273
364
|
mount() {
|
|
274
|
-
if (
|
|
275
|
-
|
|
365
|
+
if (f = g(), !f) {
|
|
366
|
+
l.error(`Mount point "${p.selector}" not found or already in use.`);
|
|
276
367
|
return;
|
|
277
368
|
}
|
|
278
|
-
|
|
279
|
-
adapter:
|
|
280
|
-
hitlManager:
|
|
281
|
-
logger:
|
|
282
|
-
provider:
|
|
283
|
-
|
|
369
|
+
d && (l.log("An app instance already exists. Unmounting first."), this.unmount()), d = createApp(CorePilotHost, {
|
|
370
|
+
adapter: s,
|
|
371
|
+
hitlManager: c,
|
|
372
|
+
logger: l,
|
|
373
|
+
provider: m,
|
|
374
|
+
authManager: u,
|
|
375
|
+
appId: h
|
|
376
|
+
}), d.mount(f), l.log("Vue CorePilotHost mounted.");
|
|
284
377
|
},
|
|
285
378
|
unmount() {
|
|
286
|
-
|
|
379
|
+
d && (d.unmount(), d = null, l.log("Vue CorePilotHost unmounted.")), f && f.parentElement && (f.parentElement.innerHTML = "");
|
|
287
380
|
}
|
|
288
381
|
};
|
|
289
382
|
}
|
|
290
383
|
var HttpError = class extends Error {
|
|
291
|
-
constructor(e, s, c
|
|
292
|
-
super(e), this.name = "HttpError", this.code =
|
|
384
|
+
constructor(e, o, s, c) {
|
|
385
|
+
super(e), this.name = "HttpError", this.code = o, this.data = s, this.originalError = c;
|
|
293
386
|
}
|
|
294
387
|
}, axios_default = new class {
|
|
295
388
|
constructor(e) {
|
|
@@ -304,72 +397,72 @@ var HttpError = class extends Error {
|
|
|
304
397
|
this.instance.interceptors.response.use((e) => e.data, (e) => Promise.reject(this.handleError(e)));
|
|
305
398
|
}
|
|
306
399
|
handleError(e) {
|
|
307
|
-
let
|
|
308
|
-
if (axios.isAxiosError(e)) if (e.response) switch (
|
|
400
|
+
let o = "请求失败,请稍后重试", s = -1, c = null;
|
|
401
|
+
if (axios.isAxiosError(e)) if (e.response) switch (s = e.response.status, c = e.response.data, s) {
|
|
309
402
|
case 400:
|
|
310
|
-
|
|
403
|
+
o = "请求参数错误";
|
|
311
404
|
break;
|
|
312
405
|
case 401:
|
|
313
|
-
|
|
406
|
+
o = "未授权,请重新登录";
|
|
314
407
|
break;
|
|
315
408
|
case 403:
|
|
316
|
-
|
|
409
|
+
o = "拒绝访问";
|
|
317
410
|
break;
|
|
318
411
|
case 404:
|
|
319
|
-
|
|
412
|
+
o = "请求资源不存在";
|
|
320
413
|
break;
|
|
321
414
|
case 405:
|
|
322
|
-
|
|
415
|
+
o = "请求方法不允许";
|
|
323
416
|
break;
|
|
324
417
|
case 408:
|
|
325
|
-
|
|
418
|
+
o = "请求超时";
|
|
326
419
|
break;
|
|
327
420
|
case 500:
|
|
328
|
-
|
|
421
|
+
o = "服务器内部错误";
|
|
329
422
|
break;
|
|
330
423
|
case 501:
|
|
331
|
-
|
|
424
|
+
o = "服务未实现";
|
|
332
425
|
break;
|
|
333
426
|
case 502:
|
|
334
|
-
|
|
427
|
+
o = "网关错误";
|
|
335
428
|
break;
|
|
336
429
|
case 503:
|
|
337
|
-
|
|
430
|
+
o = "服务不可用";
|
|
338
431
|
break;
|
|
339
432
|
case 504:
|
|
340
|
-
|
|
433
|
+
o = "网关超时";
|
|
341
434
|
break;
|
|
342
|
-
default:
|
|
435
|
+
default: o = e.response.statusText || `连接错误 ${s}`;
|
|
343
436
|
}
|
|
344
|
-
else e.request ? (
|
|
345
|
-
else
|
|
346
|
-
return console.error(`[HttpClient] Error: ${
|
|
347
|
-
code:
|
|
348
|
-
data:
|
|
437
|
+
else e.request ? (o = "网络连接失败,请检查网络设置", s = 0) : o = e.message;
|
|
438
|
+
else o = e.message || "未知错误";
|
|
439
|
+
return console.error(`[HttpClient] Error: ${o}`, {
|
|
440
|
+
code: s,
|
|
441
|
+
data: c,
|
|
349
442
|
original: e
|
|
350
|
-
}), new HttpError(s, c,
|
|
443
|
+
}), new HttpError(o, s, c, e);
|
|
351
444
|
}
|
|
352
|
-
get(e,
|
|
353
|
-
return this.instance.get(e,
|
|
445
|
+
get(e, o) {
|
|
446
|
+
return this.instance.get(e, o);
|
|
354
447
|
}
|
|
355
|
-
post(e,
|
|
356
|
-
return this.instance.post(e,
|
|
448
|
+
post(e, o, s) {
|
|
449
|
+
return this.instance.post(e, o, s);
|
|
357
450
|
}
|
|
358
|
-
put(e,
|
|
359
|
-
return this.instance.put(e,
|
|
451
|
+
put(e, o, s) {
|
|
452
|
+
return this.instance.put(e, o, s);
|
|
360
453
|
}
|
|
361
|
-
delete(e,
|
|
362
|
-
return this.instance.delete(e,
|
|
454
|
+
delete(e, o) {
|
|
455
|
+
return this.instance.delete(e, o);
|
|
363
456
|
}
|
|
364
|
-
patch(e,
|
|
365
|
-
return this.instance.patch(e,
|
|
457
|
+
patch(e, o, s) {
|
|
458
|
+
return this.instance.patch(e, o, s);
|
|
366
459
|
}
|
|
367
|
-
upload(e,
|
|
368
|
-
let
|
|
369
|
-
return
|
|
370
|
-
...
|
|
460
|
+
upload(e, o, s = "file", c) {
|
|
461
|
+
let l = new FormData();
|
|
462
|
+
return l.append(s, o), this.instance.post(e, l, {
|
|
463
|
+
...c,
|
|
371
464
|
headers: {
|
|
372
|
-
...
|
|
465
|
+
...c?.headers,
|
|
373
466
|
"Content-Type": "multipart/form-data"
|
|
374
467
|
}
|
|
375
468
|
});
|
|
@@ -378,84 +471,181 @@ var HttpError = class extends Error {
|
|
|
378
471
|
return this.instance;
|
|
379
472
|
}
|
|
380
473
|
}({});
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
let
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
474
|
+
const eventHub = new class {
|
|
475
|
+
constructor() {
|
|
476
|
+
this.listeners = [];
|
|
477
|
+
}
|
|
478
|
+
subscribeEvent(e) {
|
|
479
|
+
this.listeners.includes(e) || this.listeners.push(e);
|
|
480
|
+
}
|
|
481
|
+
unsubscribeEvent(e) {
|
|
482
|
+
let o = this.listeners.indexOf(e);
|
|
483
|
+
o > -1 && this.listeners.splice(o, 1);
|
|
484
|
+
}
|
|
485
|
+
emitEvent(e) {
|
|
486
|
+
this.listeners.forEach((o) => {
|
|
487
|
+
try {
|
|
488
|
+
o(e);
|
|
489
|
+
} catch (e) {
|
|
490
|
+
console.error("[EventHub] Event callback error:", e);
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}();
|
|
495
|
+
var TOKEN_EXPIRED_CODE = 40100, APPID_INVALID_CODE = 403;
|
|
496
|
+
function createCoreAgentAdapter(e, o, s) {
|
|
497
|
+
let { appId: c, provider: l, baseUrl: u } = e, { extensions: d } = l, { user: f = "default", accessToken: p = "" } = d || {}, m = () => {
|
|
498
|
+
eventHub.emitEvent({
|
|
499
|
+
action: "cust_token_expired",
|
|
500
|
+
code: TOKEN_EXPIRED_CODE,
|
|
501
|
+
message: "凭证已过期"
|
|
502
|
+
});
|
|
503
|
+
}, h = (e) => {
|
|
504
|
+
eventHub.emitEvent({
|
|
505
|
+
action: "cust_request_error",
|
|
506
|
+
code: e.code,
|
|
507
|
+
message: e.message || "请求失败"
|
|
508
|
+
});
|
|
509
|
+
}, g = () => o.isServerMode() ? u + "/api/proxy/ca" : u, _ = async () => ({
|
|
510
|
+
Authorization: `Bearer ${await o.getToken()}`,
|
|
511
|
+
appId: c
|
|
512
|
+
}), v = async (e, o) => {
|
|
513
|
+
let s = e.taskId, c = g(), l = await _();
|
|
514
|
+
return await axios_default.post(`${c}/openapi/v1/app/chat-messages/${s}/stop`, { user: f }, { headers: l }), Promise.resolve();
|
|
515
|
+
}, y = async (e, s, c) => {
|
|
516
|
+
e.messageId;
|
|
517
|
+
let l = g(), u = await o.getToken(), d = {
|
|
518
|
+
query: JSON.stringify(s || []),
|
|
519
|
+
inputs: { scence: c },
|
|
520
|
+
response_mode: "blocking",
|
|
521
|
+
conversation_id: null,
|
|
522
|
+
parent_message_id: null,
|
|
413
523
|
user: f,
|
|
414
524
|
files: []
|
|
415
|
-
},
|
|
525
|
+
}, p = { Authorization: `Bearer ${u}` };
|
|
526
|
+
p.appId = "b7886345-d200-486d-8318-8c234a19ccb9";
|
|
527
|
+
let m = await axios_default.post(`${l}/openapi/v1/app/chat-messages`, d, { headers: p });
|
|
528
|
+
try {
|
|
529
|
+
let e = JSON.parse(m.answer || "[]");
|
|
530
|
+
return console.log("data", e), e;
|
|
531
|
+
} catch {
|
|
532
|
+
return [];
|
|
533
|
+
}
|
|
534
|
+
}, b = async (e) => {
|
|
535
|
+
let o = g(), s = await _();
|
|
536
|
+
try {
|
|
537
|
+
return await axios_default.get(`${o}/openapi/v1/app/conversations?${new URLSearchParams(e).toString()}`, { headers: s });
|
|
538
|
+
} catch (e) {
|
|
539
|
+
throw e;
|
|
540
|
+
}
|
|
541
|
+
}, x = async (e) => {
|
|
542
|
+
let o = g(), s = await _();
|
|
543
|
+
try {
|
|
544
|
+
return await axios_default.get(`${o}/openapi/v1/app/messages?${new URLSearchParams(e).toString()}`, { headers: s });
|
|
545
|
+
} catch (e) {
|
|
546
|
+
throw e;
|
|
547
|
+
}
|
|
548
|
+
}, C = (e) => {
|
|
549
|
+
switch (e.toLowerCase()) {
|
|
550
|
+
case "txt":
|
|
551
|
+
case "doc":
|
|
552
|
+
case "docx":
|
|
553
|
+
case "pdf":
|
|
554
|
+
case "ppt":
|
|
555
|
+
case "pptx":
|
|
556
|
+
case "xls":
|
|
557
|
+
case "xlsx":
|
|
558
|
+
case "csv": return "document";
|
|
559
|
+
case "jpg":
|
|
560
|
+
case "jpeg":
|
|
561
|
+
case "png":
|
|
562
|
+
case "gif":
|
|
563
|
+
case "webp":
|
|
564
|
+
case "svg": return "image";
|
|
565
|
+
case "mp3":
|
|
566
|
+
case "wav": return "audio";
|
|
567
|
+
case "mp4":
|
|
568
|
+
case "mov":
|
|
569
|
+
case "ogg": return "video";
|
|
570
|
+
default: return "custom";
|
|
571
|
+
}
|
|
572
|
+
}, w = async (e) => {
|
|
573
|
+
let { hitlData: o, prevMessage: s, text: c, inputs: l, files: u } = e, d = o?.payload?.__files__ || {}, m = [];
|
|
574
|
+
Object.values(d).flat().forEach((e) => {
|
|
575
|
+
m.push({
|
|
576
|
+
transfer_method: "local_file",
|
|
577
|
+
type: C(e.extension || ""),
|
|
578
|
+
upload_file_id: e.id,
|
|
579
|
+
url: e.url || ""
|
|
580
|
+
});
|
|
581
|
+
}), u?.forEach((e) => {
|
|
582
|
+
m.push({
|
|
583
|
+
transfer_method: "local_file",
|
|
584
|
+
type: C(e.extension || ""),
|
|
585
|
+
upload_file_id: e.id,
|
|
586
|
+
url: e.url || ""
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
let h = {
|
|
590
|
+
query: c || JSON.stringify(o || {}),
|
|
591
|
+
inputs: {
|
|
592
|
+
...l || {},
|
|
593
|
+
accessToken: p
|
|
594
|
+
},
|
|
595
|
+
response_mode: "streaming",
|
|
596
|
+
conversation_id: s?.conversationId || null,
|
|
597
|
+
parent_message_id: s?.messageId || null,
|
|
598
|
+
user: f,
|
|
599
|
+
files: m
|
|
600
|
+
}, v = g(), y = await _();
|
|
416
601
|
return {
|
|
417
|
-
url:
|
|
602
|
+
url: `${v}/openapi/v1/app/chat-messages`,
|
|
418
603
|
config: {
|
|
419
604
|
method: "POST",
|
|
420
605
|
headers: {
|
|
421
606
|
"Content-Type": "application/json",
|
|
422
|
-
|
|
423
|
-
appId: l
|
|
607
|
+
...y
|
|
424
608
|
},
|
|
425
|
-
body: JSON.stringify(
|
|
609
|
+
body: JSON.stringify(h)
|
|
426
610
|
}
|
|
427
611
|
};
|
|
428
|
-
},
|
|
429
|
-
let { data:
|
|
430
|
-
return
|
|
431
|
-
},
|
|
432
|
-
|
|
433
|
-
},
|
|
434
|
-
|
|
435
|
-
},
|
|
436
|
-
|
|
437
|
-
},
|
|
612
|
+
}, T = (e) => {
|
|
613
|
+
let { data: o } = e, s = JSON.parse(o || "{}");
|
|
614
|
+
return s.code === TOKEN_EXPIRED_CODE ? (m(), null) : s.code === APPID_INVALID_CODE ? (h(s), null) : s;
|
|
615
|
+
}, E = async () => {
|
|
616
|
+
s.log("onClose");
|
|
617
|
+
}, D = async (e) => {
|
|
618
|
+
s.log("onError", e);
|
|
619
|
+
}, O = async (e) => {
|
|
620
|
+
s.log("onOpen");
|
|
621
|
+
}, k = (e) => {
|
|
438
622
|
try {
|
|
439
623
|
return JSON.parse(e || "{}")?.action?.startsWith("core_") || !1;
|
|
440
624
|
} catch {
|
|
441
625
|
return !1;
|
|
442
626
|
}
|
|
443
|
-
},
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
627
|
+
}, A = async (e, s = !1) => {
|
|
628
|
+
let { inputs: c } = e, l = c?.scence;
|
|
629
|
+
l && pilotStore.getState().setActiveScence(l);
|
|
630
|
+
let u = await w(e), d = o.isServerMode() ? N() : void 0;
|
|
631
|
+
sseRequester(u, {
|
|
632
|
+
formatRequest: w,
|
|
633
|
+
getSuggestionQuestions: y,
|
|
634
|
+
getConversationList: b,
|
|
635
|
+
getMessageHistory: x,
|
|
636
|
+
onMessage: T,
|
|
637
|
+
onClose: E,
|
|
638
|
+
onError: D,
|
|
639
|
+
onOpen: O,
|
|
640
|
+
onCancel: v
|
|
641
|
+
}, {
|
|
642
|
+
fetchFn: d,
|
|
643
|
+
isRetry: s
|
|
454
644
|
});
|
|
455
|
-
},
|
|
456
|
-
let
|
|
645
|
+
}, j = async (e) => {
|
|
646
|
+
let o = [], s;
|
|
457
647
|
if (e.data.forEach((e) => {
|
|
458
|
-
if (e.parent_message_id ===
|
|
648
|
+
if (e.parent_message_id === s) o.pop(), o.push({
|
|
459
649
|
role: "ai",
|
|
460
650
|
showActions: !1,
|
|
461
651
|
content: e.answer,
|
|
@@ -465,26 +655,29 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
465
655
|
extension: e.type,
|
|
466
656
|
name: e.filename
|
|
467
657
|
})),
|
|
468
|
-
placement: "start"
|
|
658
|
+
placement: "start",
|
|
659
|
+
conversationId: e.conversation_id,
|
|
660
|
+
messageId: e.id
|
|
469
661
|
});
|
|
470
662
|
else {
|
|
471
|
-
if (
|
|
663
|
+
if (s = e.parent_message_id, !k(e.query)) o.push({
|
|
472
664
|
role: "user",
|
|
473
665
|
messageId: e.id,
|
|
474
666
|
content: e.query,
|
|
475
667
|
showActions: !1,
|
|
476
668
|
maxWidth: "100%",
|
|
477
669
|
placement: "end",
|
|
478
|
-
isHistoryMessage: !0
|
|
670
|
+
isHistoryMessage: !0,
|
|
671
|
+
conversationId: e.conversation_id
|
|
479
672
|
});
|
|
480
673
|
else {
|
|
481
|
-
let
|
|
482
|
-
if (
|
|
483
|
-
let e =
|
|
484
|
-
e && (e.historyInputs =
|
|
674
|
+
let s = JSON.parse(e.query || "{}");
|
|
675
|
+
if (s?.payload) {
|
|
676
|
+
let e = o[o.length - 1];
|
|
677
|
+
e && (e.historyInputs = s.payload);
|
|
485
678
|
}
|
|
486
679
|
}
|
|
487
|
-
|
|
680
|
+
o.push({
|
|
488
681
|
role: "ai",
|
|
489
682
|
content: e.answer,
|
|
490
683
|
showActions: !1,
|
|
@@ -495,23 +688,35 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
495
688
|
extension: e.type,
|
|
496
689
|
name: e.filename
|
|
497
690
|
})),
|
|
498
|
-
isHistoryMessage: !0
|
|
691
|
+
isHistoryMessage: !0,
|
|
692
|
+
conversationId: e.conversation_id,
|
|
693
|
+
messageId: e.id
|
|
499
694
|
});
|
|
500
695
|
}
|
|
501
696
|
}), e.has_more) {
|
|
502
|
-
let e = await
|
|
503
|
-
conversation_id:
|
|
697
|
+
let e = await x({
|
|
698
|
+
conversation_id: o[o.length - 1].conversationId,
|
|
504
699
|
user: f,
|
|
505
|
-
first_id:
|
|
700
|
+
first_id: o[0].messageId
|
|
506
701
|
});
|
|
507
702
|
if (e.data) {
|
|
508
|
-
let
|
|
509
|
-
|
|
703
|
+
let s = await j(e);
|
|
704
|
+
o.unshift(...s);
|
|
510
705
|
}
|
|
511
706
|
}
|
|
512
|
-
return
|
|
513
|
-
},
|
|
514
|
-
let
|
|
707
|
+
return o;
|
|
708
|
+
}, M = async () => (pilotStore.getState().resetMessageHistory(), !0), N = () => async (e, s) => {
|
|
709
|
+
let c = await o.getToken(), l = o.getAppId(), u = {
|
|
710
|
+
...s?.headers || {},
|
|
711
|
+
Authorization: `Bearer ${c}`,
|
|
712
|
+
...l ? { appId: l } : {}
|
|
713
|
+
};
|
|
714
|
+
return await fetch(e, {
|
|
715
|
+
...s,
|
|
716
|
+
headers: u
|
|
717
|
+
});
|
|
718
|
+
}, P = async (e, s = !1) => {
|
|
719
|
+
let c = {
|
|
515
720
|
messageId: void 0,
|
|
516
721
|
content: e.text,
|
|
517
722
|
role: "user",
|
|
@@ -519,125 +724,175 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
519
724
|
showActions: !1,
|
|
520
725
|
maxWidth: "100%"
|
|
521
726
|
};
|
|
522
|
-
pilotStore.getState().updateMessageHistory([
|
|
523
|
-
let
|
|
727
|
+
s || pilotStore.getState().updateMessageHistory([c]), pilotStore.getState().resetCurrentMessage();
|
|
728
|
+
let l = await w({
|
|
524
729
|
text: e.text,
|
|
525
730
|
prevMessage: void 0,
|
|
526
731
|
inputs: e.inputs
|
|
527
732
|
});
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
733
|
+
pilotStore.getState().setActiveScence(e.inputs?.scence);
|
|
734
|
+
let u = o.isServerMode() ? N() : void 0;
|
|
735
|
+
return sseRequester(l, {
|
|
736
|
+
formatRequest: w,
|
|
737
|
+
getSuggestionQuestions: y,
|
|
738
|
+
getConversationList: b,
|
|
739
|
+
getMessageHistory: x,
|
|
740
|
+
onMessage: T,
|
|
741
|
+
onClose: E,
|
|
742
|
+
onError: D,
|
|
743
|
+
onOpen: O,
|
|
744
|
+
onCancel: v
|
|
745
|
+
}, {
|
|
746
|
+
fetchFn: u,
|
|
747
|
+
isRetry: s
|
|
538
748
|
});
|
|
749
|
+
}, F = async () => {
|
|
750
|
+
let e = pilotStore.getState().pendingRetryRequest;
|
|
751
|
+
if (!e) return;
|
|
752
|
+
pilotStore.getState().clearPendingRetryRequest();
|
|
753
|
+
let s = o.isServerMode() ? N() : void 0;
|
|
754
|
+
if (e.type === "submit_message") {
|
|
755
|
+
let o = e.payload;
|
|
756
|
+
sseRequester(await w({
|
|
757
|
+
text: o.text,
|
|
758
|
+
prevMessage: void 0,
|
|
759
|
+
inputs: o.inputs
|
|
760
|
+
}), {
|
|
761
|
+
formatRequest: w,
|
|
762
|
+
getSuggestionQuestions: y,
|
|
763
|
+
getConversationList: b,
|
|
764
|
+
getMessageHistory: x,
|
|
765
|
+
onMessage: T,
|
|
766
|
+
onClose: E,
|
|
767
|
+
onError: D,
|
|
768
|
+
onOpen: O,
|
|
769
|
+
onCancel: v
|
|
770
|
+
}, {
|
|
771
|
+
fetchFn: s,
|
|
772
|
+
isRetry: !0
|
|
773
|
+
});
|
|
774
|
+
} else e.type === "submit_core_action" && await A(e.payload, !0);
|
|
539
775
|
};
|
|
540
|
-
return {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
776
|
+
return o.onTokenRefreshed(() => {
|
|
777
|
+
setTimeout(() => {
|
|
778
|
+
F();
|
|
779
|
+
}, 0);
|
|
780
|
+
}), {
|
|
781
|
+
formatRequest: w,
|
|
782
|
+
getSuggestionQuestions: y,
|
|
783
|
+
getConversationList: b,
|
|
784
|
+
getMessageHistory: x,
|
|
785
|
+
onMessage: T,
|
|
786
|
+
onClose: E,
|
|
787
|
+
onError: D,
|
|
788
|
+
onOpen: O,
|
|
789
|
+
onCancel: v,
|
|
550
790
|
triggerEvent: async (e) => {
|
|
551
791
|
switch (e.action) {
|
|
552
|
-
case "get_suggestion_questions": return await
|
|
553
|
-
case "get_conversation_list": return await
|
|
792
|
+
case "get_suggestion_questions": return await y(e.payload);
|
|
793
|
+
case "get_conversation_list": return await b(e.payload);
|
|
554
794
|
case "get_message_history":
|
|
555
|
-
let
|
|
556
|
-
return pilotStore.getState().resetMessageHistory(
|
|
557
|
-
case "submit_core_action": return
|
|
558
|
-
|
|
559
|
-
|
|
795
|
+
let o = await j(await x(e.payload));
|
|
796
|
+
return pilotStore.getState().resetMessageHistory(o), o;
|
|
797
|
+
case "submit_core_action": return pilotStore.getState().setPendingRetryRequest({
|
|
798
|
+
type: "submit_core_action",
|
|
799
|
+
payload: e.payload
|
|
800
|
+
}), await A(e.payload);
|
|
801
|
+
case "new_message": return await M();
|
|
802
|
+
case "submit_message": return pilotStore.getState().setPendingRetryRequest({
|
|
803
|
+
type: "submit_message",
|
|
804
|
+
payload: e.payload
|
|
805
|
+
}), await P(e.payload);
|
|
560
806
|
default: throw Error("Invalid event action.");
|
|
561
807
|
}
|
|
562
|
-
}
|
|
808
|
+
},
|
|
809
|
+
createSseFetch: N
|
|
563
810
|
};
|
|
564
811
|
}
|
|
565
|
-
function createProtocolAdapter(e, s, c
|
|
566
|
-
switch (
|
|
567
|
-
case "coreagent": return createCoreAgentAdapter(e,
|
|
568
|
-
default: throw Error(`Unsupported provider: ${
|
|
812
|
+
function createProtocolAdapter(e, o, s, c) {
|
|
813
|
+
switch (c.log(`Creating adapter for provider: ${o}`), o.name) {
|
|
814
|
+
case "coreagent": return createCoreAgentAdapter(e, s, c);
|
|
815
|
+
default: throw Error(`Unsupported provider: ${o.name}`);
|
|
569
816
|
}
|
|
570
817
|
}
|
|
571
|
-
function createHitlManager(e,
|
|
572
|
-
let
|
|
573
|
-
let
|
|
818
|
+
function createHitlManager(e, o, s) {
|
|
819
|
+
let c = (e) => e.split("_")[0], l = [], u = async (o) => {
|
|
820
|
+
let c = await e.formatRequest(o);
|
|
574
821
|
sseRequester({
|
|
575
|
-
url:
|
|
576
|
-
config:
|
|
577
|
-
}, e),
|
|
578
|
-
},
|
|
579
|
-
|
|
580
|
-
},
|
|
822
|
+
url: c.url,
|
|
823
|
+
config: c.config
|
|
824
|
+
}, e), s.log("HITL data submitted successfully.");
|
|
825
|
+
}, d = async (e) => {
|
|
826
|
+
l.forEach((o) => o(e.hitlData));
|
|
827
|
+
}, f = async (e) => {};
|
|
581
828
|
return {
|
|
582
829
|
async submit(e) {
|
|
583
830
|
try {
|
|
584
|
-
let
|
|
585
|
-
switch (
|
|
831
|
+
let o = c(e.hitlData?.action ?? "");
|
|
832
|
+
switch (s.log("[HITL] Submitting HITL data...", e), o) {
|
|
586
833
|
case "core":
|
|
587
|
-
await
|
|
834
|
+
await u(e);
|
|
588
835
|
break;
|
|
589
836
|
case "cust":
|
|
590
|
-
await
|
|
837
|
+
await d(e);
|
|
591
838
|
break;
|
|
592
839
|
case "inner":
|
|
593
|
-
await
|
|
840
|
+
await f(e);
|
|
594
841
|
break;
|
|
595
842
|
default: throw Error("Invalid action type.");
|
|
596
843
|
}
|
|
597
844
|
} catch (e) {
|
|
598
|
-
throw
|
|
845
|
+
throw s.error("Error submitting HITL data:", e), e;
|
|
599
846
|
}
|
|
600
847
|
},
|
|
601
848
|
subscribeEvent(e) {
|
|
602
|
-
|
|
849
|
+
l.includes(e) || l.push(e);
|
|
603
850
|
},
|
|
604
851
|
unsubscribeEvent(e) {
|
|
605
|
-
|
|
852
|
+
l.splice(l.indexOf(e), 1);
|
|
606
853
|
}
|
|
607
854
|
};
|
|
608
855
|
}
|
|
609
856
|
function createCorePilot(e) {
|
|
610
|
-
if (!e.appId) throw Error("CorePilot SDK: `appId` is required.");
|
|
857
|
+
if (e.auth.mode, !e.appId) throw Error("CorePilot SDK: `appId` is required.");
|
|
611
858
|
if (!e.mount.selector) throw Error("CorePilot SDK: `mount.selector` is required.");
|
|
612
|
-
let
|
|
613
|
-
|
|
614
|
-
let
|
|
859
|
+
let o = createLogger(e.debug ?? !1, e.appId || "", e.eventTracker);
|
|
860
|
+
o.log("CorePilot SDK Initializing...", e);
|
|
861
|
+
let s = { ...e }, c = createAuthManager(s.auth, s.appId), l = createProtocolAdapter(s, s.provider, c, o), u = createHitlManager(l, c, o), d = createMountManager(s, l, u, o, c), f = {
|
|
862
|
+
inner_set_token: async (e) => (await c.setToken(e), !0),
|
|
863
|
+
inner_get_token: async () => await c.getToken()
|
|
864
|
+
};
|
|
615
865
|
return {
|
|
616
866
|
mount() {
|
|
617
|
-
|
|
867
|
+
o.log("Mounting CorePilot UI..."), d.mount();
|
|
618
868
|
},
|
|
619
869
|
unmount() {
|
|
620
|
-
|
|
870
|
+
o.log("Unmounting CorePilot UI..."), d.unmount();
|
|
621
871
|
},
|
|
622
872
|
reload(e) {
|
|
623
|
-
|
|
624
|
-
...
|
|
873
|
+
o.log("Reloading CorePilot instance...", e), d.unmount(), s = {
|
|
874
|
+
...s,
|
|
625
875
|
...e
|
|
626
|
-
},
|
|
627
|
-
let
|
|
628
|
-
|
|
876
|
+
}, l = createProtocolAdapter(s, s.provider, c, o);
|
|
877
|
+
let f = createMountManager(s, l, u, o, c);
|
|
878
|
+
d.mount = f.mount, d.unmount = f.unmount, o.log("CorePilot reloaded. Remounting UI..."), d.mount();
|
|
629
879
|
},
|
|
630
880
|
destroy() {
|
|
631
|
-
|
|
881
|
+
o.log("Destroying CorePilot instance..."), d.unmount(), o.destroy();
|
|
632
882
|
},
|
|
633
883
|
async triggerEvent(e) {
|
|
634
|
-
|
|
884
|
+
let o = f[e.action];
|
|
885
|
+
if (o) {
|
|
886
|
+
let s = e.payload;
|
|
887
|
+
return await o(s);
|
|
888
|
+
}
|
|
889
|
+
return await l.triggerEvent(e);
|
|
635
890
|
},
|
|
636
891
|
subscribeEvent(e) {
|
|
637
|
-
|
|
892
|
+
u.subscribeEvent(e), eventHub.subscribeEvent(e);
|
|
638
893
|
},
|
|
639
894
|
unsubscribeEvent(e) {
|
|
640
|
-
|
|
895
|
+
u.unsubscribeEvent(e), eventHub.unsubscribeEvent(e);
|
|
641
896
|
}
|
|
642
897
|
};
|
|
643
898
|
}
|