@core-pilot/sdk 0.0.0-beta.3 → 0.0.0-beta.4
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/README.md +625 -66
- package/dist/index.js +303 -285
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createApp, createVNode, defineComponent, onBeforeUnmount, ref } from "vue";
|
|
1
|
+
import { createApp, createVNode, defineComponent, onBeforeUnmount, ref, 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,104 +13,104 @@ 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, c, l) {
|
|
17
|
+
let u = "[CorePilot]", d = [], f = null, p = l?.batchSize ?? 20, m = l?.flushInterval ?? 15e3, h = () => {
|
|
18
|
+
if (!l || d.length === 0) return;
|
|
19
|
+
let c = [...d];
|
|
20
|
+
d = [], e && console.log(`${u} Uploading ${c.length} log(s) to ${l.url}`);
|
|
21
|
+
let f = JSON.stringify(c);
|
|
22
|
+
navigator.sendBeacon ? navigator.sendBeacon(l.url, f) : fetch(l.url, {
|
|
23
23
|
method: "POST",
|
|
24
|
-
body:
|
|
24
|
+
body: f,
|
|
25
25
|
headers: { "Content-Type": "application/json" },
|
|
26
26
|
keepalive: !0
|
|
27
|
-
}).catch((
|
|
28
|
-
e && (console.error(`${
|
|
27
|
+
}).catch((l) => {
|
|
28
|
+
e && (console.error(`${u} Log upload failed:`, l), d.unshift(...c));
|
|
29
29
|
});
|
|
30
|
-
}, g = (e, ...
|
|
31
|
-
|
|
30
|
+
}, g = (e, ...u) => {
|
|
31
|
+
l && (d.push({
|
|
32
32
|
level: e,
|
|
33
33
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
34
|
-
message: formatArgs(
|
|
35
|
-
appId:
|
|
36
|
-
}),
|
|
34
|
+
message: formatArgs(u),
|
|
35
|
+
appId: c
|
|
36
|
+
}), d.length >= p && (f && clearTimeout(f), h(), f = window.setTimeout(h, m)));
|
|
37
37
|
};
|
|
38
|
-
return
|
|
39
|
-
log(...
|
|
40
|
-
e && console.log(
|
|
38
|
+
return l && (f = window.setTimeout(h, m)), {
|
|
39
|
+
log(...c) {
|
|
40
|
+
e && console.log(u, ...c), g("log", ...c);
|
|
41
41
|
},
|
|
42
|
-
error(...
|
|
43
|
-
e && console.error(
|
|
42
|
+
error(...c) {
|
|
43
|
+
e && console.error(u, ...c), g("error", ...c);
|
|
44
44
|
},
|
|
45
45
|
destroy() {
|
|
46
|
-
|
|
46
|
+
f && clearTimeout(f), h();
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function createAuthManager(e,
|
|
51
|
-
let
|
|
50
|
+
function createAuthManager(e, c) {
|
|
51
|
+
let l = "";
|
|
52
52
|
return { async getToken() {
|
|
53
53
|
if (e.mode === "client") {
|
|
54
54
|
if (!e.token) throw Error("AuthManager: Missing token in client mode");
|
|
55
|
-
return
|
|
55
|
+
return l = e.token, l;
|
|
56
56
|
}
|
|
57
|
-
if (e.mode === "server") return
|
|
57
|
+
if (e.mode === "server") return l ||= await new Promise((e) => setTimeout(() => {
|
|
58
58
|
e("9269ca5c-5b35-46de-915f-b2e3ff41da13");
|
|
59
|
-
}, 1e3)),
|
|
59
|
+
}, 1e3)), l;
|
|
60
60
|
throw Error("AuthManager: Invalid configuration or missing token.");
|
|
61
61
|
} };
|
|
62
62
|
}
|
|
63
63
|
var createStoreImpl = (e) => {
|
|
64
|
-
let
|
|
65
|
-
let
|
|
66
|
-
if (!Object.is(
|
|
67
|
-
let e =
|
|
68
|
-
|
|
64
|
+
let c, l = /* @__PURE__ */ new Set(), u = (e, u) => {
|
|
65
|
+
let d = typeof e == "function" ? e(c) : e;
|
|
66
|
+
if (!Object.is(d, c)) {
|
|
67
|
+
let e = c;
|
|
68
|
+
c = u ?? (typeof d != "object" || !d) ? d : Object.assign({}, c, d), l.forEach((l) => l(c, e));
|
|
69
69
|
}
|
|
70
|
-
},
|
|
71
|
-
setState:
|
|
72
|
-
getState:
|
|
73
|
-
getInitialState: () =>
|
|
74
|
-
subscribe: (e) => (
|
|
75
|
-
},
|
|
76
|
-
return
|
|
70
|
+
}, d = () => c, f = {
|
|
71
|
+
setState: u,
|
|
72
|
+
getState: d,
|
|
73
|
+
getInitialState: () => p,
|
|
74
|
+
subscribe: (e) => (l.add(e), () => l.delete(e))
|
|
75
|
+
}, p = c = e(u, d, f);
|
|
76
|
+
return f;
|
|
77
77
|
};
|
|
78
78
|
const pilotStore = ((e) => e ? createStoreImpl(e) : createStoreImpl)((e) => ({
|
|
79
79
|
currentMessage: void 0,
|
|
80
80
|
isLoading: !1,
|
|
81
81
|
messageHistory: [],
|
|
82
|
-
updateMessage: (
|
|
83
|
-
let
|
|
84
|
-
...
|
|
82
|
+
updateMessage: (c) => e((e) => {
|
|
83
|
+
let l = {
|
|
84
|
+
...c,
|
|
85
85
|
role: "ai",
|
|
86
86
|
showActions: !1,
|
|
87
87
|
maxWidth: "100%"
|
|
88
|
-
},
|
|
89
|
-
if (
|
|
88
|
+
}, u = [...e.messageHistory];
|
|
89
|
+
if (u.forEach((e) => {
|
|
90
90
|
e.isHistoryMessage = !0;
|
|
91
91
|
}), e.currentMessage) {
|
|
92
|
-
let
|
|
93
|
-
...
|
|
94
|
-
content: e.currentMessage.content + (
|
|
92
|
+
let d = {
|
|
93
|
+
...l,
|
|
94
|
+
content: e.currentMessage.content + (c.content || "")
|
|
95
95
|
};
|
|
96
|
-
return
|
|
97
|
-
currentMessage:
|
|
98
|
-
messageHistory:
|
|
96
|
+
return u.pop(), u.push(d), {
|
|
97
|
+
currentMessage: d,
|
|
98
|
+
messageHistory: u
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
|
-
return
|
|
102
|
-
currentMessage:
|
|
103
|
-
messageHistory:
|
|
101
|
+
return u.push(l), {
|
|
102
|
+
currentMessage: l,
|
|
103
|
+
messageHistory: u
|
|
104
104
|
};
|
|
105
105
|
}),
|
|
106
|
-
setLoading: (
|
|
106
|
+
setLoading: (c) => e({ isLoading: c }),
|
|
107
107
|
resetCurrentMessage: () => e({ currentMessage: void 0 }),
|
|
108
|
-
resetMessageHistory: (
|
|
109
|
-
updateMessageHistory: (
|
|
110
|
-
let
|
|
111
|
-
return
|
|
108
|
+
resetMessageHistory: (c) => e({ messageHistory: c || [] }),
|
|
109
|
+
updateMessageHistory: (c) => e((e) => {
|
|
110
|
+
let l = [...e.messageHistory];
|
|
111
|
+
return l.forEach((e) => {
|
|
112
112
|
e.isHistoryMessage = !0;
|
|
113
|
-
}), Array.isArray(
|
|
113
|
+
}), Array.isArray(c) ? { messageHistory: [...l, ...c] } : { messageHistory: [...l, c] };
|
|
114
114
|
})
|
|
115
115
|
}));
|
|
116
116
|
var MAX_RETRY_TIME = 0, EventStreamContentType = "text/event-stream", FatalError = class extends Error {
|
|
@@ -118,46 +118,46 @@ var MAX_RETRY_TIME = 0, EventStreamContentType = "text/event-stream", FatalError
|
|
|
118
118
|
super(e), this.name = "FatalError";
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
|
-
const sseRequester = function(e,
|
|
122
|
-
let { url:
|
|
123
|
-
function
|
|
124
|
-
|
|
121
|
+
const sseRequester = function(e, c) {
|
|
122
|
+
let { url: l, config: u = {} } = e, d = new AbortController();
|
|
123
|
+
function f() {
|
|
124
|
+
c.onCancel(pilotStore.getState().currentMessage, d), pilotStore.getState().setLoading(!1), pilotStore.getState().resetCurrentMessage();
|
|
125
125
|
}
|
|
126
|
-
function
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
taskId:
|
|
130
|
-
messageId:
|
|
131
|
-
conversationId:
|
|
126
|
+
function p(e) {
|
|
127
|
+
let l = c.onMessage(e);
|
|
128
|
+
l.event === "message" && pilotStore.getState().updateMessage({
|
|
129
|
+
taskId: l.task_id,
|
|
130
|
+
messageId: l.message_id,
|
|
131
|
+
conversationId: l.conversation_id,
|
|
132
132
|
role: "ai",
|
|
133
|
-
content:
|
|
133
|
+
content: l.answer
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
function m() {
|
|
137
|
-
pilotStore.getState().setLoading(!0),
|
|
137
|
+
pilotStore.getState().setLoading(!0), c.onOpen();
|
|
138
138
|
}
|
|
139
|
-
function
|
|
140
|
-
pilotStore.getState().setLoading(!1), pilotStore.getState().resetCurrentMessage(),
|
|
139
|
+
function g() {
|
|
140
|
+
pilotStore.getState().setLoading(!1), pilotStore.getState().resetCurrentMessage(), c.onClose();
|
|
141
141
|
}
|
|
142
|
-
let
|
|
143
|
-
return fetchEventSource(
|
|
144
|
-
...
|
|
145
|
-
signal:
|
|
142
|
+
let _ = Math.max(MAX_RETRY_TIME - 1, 0);
|
|
143
|
+
return fetchEventSource(l, {
|
|
144
|
+
...u,
|
|
145
|
+
signal: d.signal,
|
|
146
146
|
openWhenHidden: !0,
|
|
147
|
-
onmessage:
|
|
148
|
-
onclose:
|
|
147
|
+
onmessage: p,
|
|
148
|
+
onclose: g,
|
|
149
149
|
onopen: async (e) => {
|
|
150
|
-
let
|
|
151
|
-
if (!
|
|
150
|
+
let c = e.headers.get("content-type");
|
|
151
|
+
if (!c || !c.includes(EventStreamContentType)) throw Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${c}`);
|
|
152
152
|
m();
|
|
153
153
|
},
|
|
154
154
|
onerror: (e) => {
|
|
155
|
-
if (
|
|
156
|
-
|
|
155
|
+
if (_--, _ < 0 && (g(), e instanceof TypeError)) throw new FatalError(e.message);
|
|
156
|
+
c.onError(e);
|
|
157
157
|
}
|
|
158
158
|
}).catch((e) => {
|
|
159
|
-
|
|
160
|
-
}), { cancel:
|
|
159
|
+
g(), c.onError(e);
|
|
160
|
+
}), { cancel: f };
|
|
161
161
|
}, CorePilotHost = /* @__PURE__ */ defineComponent({
|
|
162
162
|
props: {
|
|
163
163
|
adapter: {
|
|
@@ -178,90 +178,107 @@ const sseRequester = function(e, s) {
|
|
|
178
178
|
}
|
|
179
179
|
},
|
|
180
180
|
setup(e) {
|
|
181
|
-
let
|
|
182
|
-
let { isLoading:
|
|
183
|
-
|
|
181
|
+
let c = ref([]), l = ref([]), p = ref(!1), m = ref(), h = ref("ANC"), g = ref(), _ = pilotStore.subscribe((e) => {
|
|
182
|
+
let { isLoading: c, messageHistory: u } = e;
|
|
183
|
+
l.value = u, p.value = c;
|
|
184
184
|
});
|
|
185
|
-
return
|
|
186
|
-
|
|
185
|
+
return watch(() => p.value, (e) => {
|
|
186
|
+
e && (g.value = void 0);
|
|
187
|
+
}), watch(() => g.value, (c, u) => {
|
|
188
|
+
u === void 0 && c && e.hitlManager.submit({ hitlData: {
|
|
189
|
+
action: "cust_ai_output_finish",
|
|
190
|
+
payload: {
|
|
191
|
+
refConfig: g.value,
|
|
192
|
+
conversationId: l.value[l.value.length - 1]?.conversationId
|
|
193
|
+
}
|
|
194
|
+
} });
|
|
195
|
+
}), onBeforeUnmount(() => {
|
|
196
|
+
_?.();
|
|
187
197
|
}), {
|
|
188
|
-
messages:
|
|
189
|
-
isLoading:
|
|
190
|
-
handleSendMessage: async (
|
|
191
|
-
let
|
|
198
|
+
messages: l,
|
|
199
|
+
isLoading: p,
|
|
200
|
+
handleSendMessage: async (c) => {
|
|
201
|
+
let u = {
|
|
192
202
|
messageId: void 0,
|
|
193
|
-
content:
|
|
203
|
+
content: c.text,
|
|
194
204
|
role: "user",
|
|
195
205
|
placement: "end",
|
|
196
206
|
showActions: !1,
|
|
197
207
|
maxWidth: "100%"
|
|
198
|
-
};
|
|
199
|
-
pilotStore.getState().updateMessageHistory([
|
|
200
|
-
text:
|
|
201
|
-
prevMessage:
|
|
202
|
-
inputs: {
|
|
208
|
+
}, d = l.value[l.value.length - 1];
|
|
209
|
+
pilotStore.getState().updateMessageHistory([u]), pilotStore.getState().resetCurrentMessage(), m.value = sseRequester(await e.adapter.formatReuqest({
|
|
210
|
+
text: c.text,
|
|
211
|
+
prevMessage: d,
|
|
212
|
+
inputs: { scence: h.value }
|
|
203
213
|
}), e.adapter).cancel;
|
|
204
214
|
},
|
|
205
215
|
cancelHandler: () => {
|
|
206
|
-
e.logger.log("Cancel handler called"),
|
|
216
|
+
e.logger.log("Cancel handler called"), m.value?.(), pilotStore.getState().resetCurrentMessage();
|
|
207
217
|
},
|
|
208
|
-
|
|
209
|
-
|
|
218
|
+
handleScenceChange: (c) => {
|
|
219
|
+
h.value = c.id, e.hitlManager.submit({ hitlData: {
|
|
220
|
+
action: "cust_scence_change",
|
|
221
|
+
payload: { scence: c.id }
|
|
222
|
+
} });
|
|
210
223
|
},
|
|
211
|
-
tagList:
|
|
212
|
-
coreSubmit: async (
|
|
224
|
+
tagList: c,
|
|
225
|
+
coreSubmit: async (c, l) => {
|
|
213
226
|
pilotStore.getState().resetCurrentMessage(), await e.hitlManager.submit({
|
|
214
|
-
hitlData:
|
|
215
|
-
prevMessage:
|
|
216
|
-
inputs: {
|
|
227
|
+
hitlData: c,
|
|
228
|
+
prevMessage: l,
|
|
229
|
+
inputs: { scence: h.value }
|
|
217
230
|
});
|
|
231
|
+
},
|
|
232
|
+
handleRefConfigUpdate: (e) => {
|
|
233
|
+
g.value = e;
|
|
218
234
|
}
|
|
219
235
|
};
|
|
220
236
|
},
|
|
221
237
|
render() {
|
|
222
238
|
return createVNode("div", { class: "core-pilot-host" }, [createVNode(BubbleList, {
|
|
223
239
|
list: this.messages,
|
|
224
|
-
onCoreSubmit: this.coreSubmit
|
|
240
|
+
onCoreSubmit: this.coreSubmit,
|
|
241
|
+
onRefConfigUpdate: this.handleRefConfigUpdate
|
|
225
242
|
}, null), createVNode(EditorSender, {
|
|
226
243
|
tagList: this.tagList,
|
|
227
244
|
loading: this.isLoading,
|
|
228
245
|
agentName: "",
|
|
229
246
|
onSubmit: this.handleSendMessage,
|
|
230
247
|
onCancel: this.cancelHandler,
|
|
231
|
-
onSceneChange: this.
|
|
248
|
+
onSceneChange: this.handleScenceChange
|
|
232
249
|
}, null)]);
|
|
233
250
|
}
|
|
234
251
|
});
|
|
235
|
-
function createMountManager(
|
|
236
|
-
let
|
|
237
|
-
let e = (
|
|
252
|
+
function createMountManager(c, l, u, d, f) {
|
|
253
|
+
let p = null, m = null, h = () => {
|
|
254
|
+
let e = (c.container || document).querySelector(c.selector);
|
|
238
255
|
if (e && !e.querySelector("[data-corepilot-host]")) {
|
|
239
|
-
let
|
|
240
|
-
return
|
|
256
|
+
let c = document.createElement("div");
|
|
257
|
+
return c.setAttribute("data-corepilot-host", "true"), c.style.width = "100%", c.style.height = "100%", e.appendChild(c), c;
|
|
241
258
|
}
|
|
242
259
|
return e?.querySelector("[data-corepilot-host]");
|
|
243
260
|
};
|
|
244
261
|
return {
|
|
245
262
|
mount() {
|
|
246
|
-
if (
|
|
247
|
-
|
|
263
|
+
if (m = h(), !m) {
|
|
264
|
+
d.error(`Mount point "${c.selector}" not found or already in use.`);
|
|
248
265
|
return;
|
|
249
266
|
}
|
|
250
|
-
|
|
251
|
-
adapter:
|
|
252
|
-
hitlManager:
|
|
253
|
-
logger:
|
|
254
|
-
provider:
|
|
255
|
-
}),
|
|
267
|
+
p && (d.log("An app instance already exists. Unmounting first."), this.unmount()), p = createApp(CorePilotHost, {
|
|
268
|
+
adapter: l,
|
|
269
|
+
hitlManager: u,
|
|
270
|
+
logger: d,
|
|
271
|
+
provider: f
|
|
272
|
+
}), p.mount(m), d.log("Vue CorePilotHost mounted.");
|
|
256
273
|
},
|
|
257
274
|
unmount() {
|
|
258
|
-
|
|
275
|
+
p && (p.unmount(), p = null, d.log("Vue CorePilotHost unmounted.")), m && m.parentElement && (m.parentElement.innerHTML = "");
|
|
259
276
|
}
|
|
260
277
|
};
|
|
261
278
|
}
|
|
262
279
|
var HttpError = class extends Error {
|
|
263
|
-
constructor(e,
|
|
264
|
-
super(e), this.name = "HttpError", this.code =
|
|
280
|
+
constructor(e, c, l, u) {
|
|
281
|
+
super(e), this.name = "HttpError", this.code = c, this.data = l, this.originalError = u;
|
|
265
282
|
}
|
|
266
283
|
}, axios_default = new class {
|
|
267
284
|
constructor(e) {
|
|
@@ -276,72 +293,72 @@ var HttpError = class extends Error {
|
|
|
276
293
|
this.instance.interceptors.response.use((e) => e.data, (e) => Promise.reject(this.handleError(e)));
|
|
277
294
|
}
|
|
278
295
|
handleError(e) {
|
|
279
|
-
let
|
|
280
|
-
if (axios.isAxiosError(e)) if (e.response) switch (
|
|
296
|
+
let c = "请求失败,请稍后重试", l = -1, u = null;
|
|
297
|
+
if (axios.isAxiosError(e)) if (e.response) switch (l = e.response.status, u = e.response.data, l) {
|
|
281
298
|
case 400:
|
|
282
|
-
|
|
299
|
+
c = "请求参数错误";
|
|
283
300
|
break;
|
|
284
301
|
case 401:
|
|
285
|
-
|
|
302
|
+
c = "未授权,请重新登录";
|
|
286
303
|
break;
|
|
287
304
|
case 403:
|
|
288
|
-
|
|
305
|
+
c = "拒绝访问";
|
|
289
306
|
break;
|
|
290
307
|
case 404:
|
|
291
|
-
|
|
308
|
+
c = "请求资源不存在";
|
|
292
309
|
break;
|
|
293
310
|
case 405:
|
|
294
|
-
|
|
311
|
+
c = "请求方法不允许";
|
|
295
312
|
break;
|
|
296
313
|
case 408:
|
|
297
|
-
|
|
314
|
+
c = "请求超时";
|
|
298
315
|
break;
|
|
299
316
|
case 500:
|
|
300
|
-
|
|
317
|
+
c = "服务器内部错误";
|
|
301
318
|
break;
|
|
302
319
|
case 501:
|
|
303
|
-
|
|
320
|
+
c = "服务未实现";
|
|
304
321
|
break;
|
|
305
322
|
case 502:
|
|
306
|
-
|
|
323
|
+
c = "网关错误";
|
|
307
324
|
break;
|
|
308
325
|
case 503:
|
|
309
|
-
|
|
326
|
+
c = "服务不可用";
|
|
310
327
|
break;
|
|
311
328
|
case 504:
|
|
312
|
-
|
|
329
|
+
c = "网关超时";
|
|
313
330
|
break;
|
|
314
|
-
default:
|
|
331
|
+
default: c = e.response.statusText || `连接错误 ${l}`;
|
|
315
332
|
}
|
|
316
|
-
else e.request ? (
|
|
317
|
-
else
|
|
318
|
-
return console.error(`[HttpClient] Error: ${
|
|
319
|
-
code:
|
|
320
|
-
data:
|
|
333
|
+
else e.request ? (c = "网络连接失败,请检查网络设置", l = 0) : c = e.message;
|
|
334
|
+
else c = e.message || "未知错误";
|
|
335
|
+
return console.error(`[HttpClient] Error: ${c}`, {
|
|
336
|
+
code: l,
|
|
337
|
+
data: u,
|
|
321
338
|
original: e
|
|
322
|
-
}), new HttpError(
|
|
339
|
+
}), new HttpError(c, l, u, e);
|
|
323
340
|
}
|
|
324
|
-
get(e,
|
|
325
|
-
return this.instance.get(e,
|
|
341
|
+
get(e, c) {
|
|
342
|
+
return this.instance.get(e, c);
|
|
326
343
|
}
|
|
327
|
-
post(e,
|
|
328
|
-
return this.instance.post(e,
|
|
344
|
+
post(e, c, l) {
|
|
345
|
+
return this.instance.post(e, c, l);
|
|
329
346
|
}
|
|
330
|
-
put(e,
|
|
331
|
-
return this.instance.put(e,
|
|
347
|
+
put(e, c, l) {
|
|
348
|
+
return this.instance.put(e, c, l);
|
|
332
349
|
}
|
|
333
|
-
delete(e,
|
|
334
|
-
return this.instance.delete(e,
|
|
350
|
+
delete(e, c) {
|
|
351
|
+
return this.instance.delete(e, c);
|
|
335
352
|
}
|
|
336
|
-
patch(e,
|
|
337
|
-
return this.instance.patch(e,
|
|
353
|
+
patch(e, c, l) {
|
|
354
|
+
return this.instance.patch(e, c, l);
|
|
338
355
|
}
|
|
339
|
-
upload(e,
|
|
340
|
-
let
|
|
341
|
-
return
|
|
342
|
-
...
|
|
356
|
+
upload(e, c, l = "file", u) {
|
|
357
|
+
let d = new FormData();
|
|
358
|
+
return d.append(l, c), this.instance.post(e, d, {
|
|
359
|
+
...u,
|
|
343
360
|
headers: {
|
|
344
|
-
...
|
|
361
|
+
...u?.headers,
|
|
345
362
|
"Content-Type": "multipart/form-data"
|
|
346
363
|
}
|
|
347
364
|
});
|
|
@@ -350,77 +367,78 @@ var HttpError = class extends Error {
|
|
|
350
367
|
return this.instance;
|
|
351
368
|
}
|
|
352
369
|
}({});
|
|
353
|
-
function createCoreAgentAdapter(e,
|
|
354
|
-
let { appId:
|
|
355
|
-
let
|
|
356
|
-
return await axios_default.post(`/openapi/v1/app/chat-messages/${
|
|
357
|
-
Authorization: `Bearer ${
|
|
358
|
-
appId:
|
|
370
|
+
function createCoreAgentAdapter(e, c, l) {
|
|
371
|
+
let { appId: u, provider: d } = e, { extensions: f } = d, { user: p = "default" } = f || {}, m = async (e, l) => {
|
|
372
|
+
let d = e.taskId, f = await c.getToken();
|
|
373
|
+
return await axios_default.post(`/openapi/v1/app/chat-messages/${d}/stop`, { user: p }, { headers: {
|
|
374
|
+
Authorization: `Bearer ${f}`,
|
|
375
|
+
appId: u
|
|
359
376
|
} }), Promise.resolve();
|
|
360
|
-
}, m = async (e) => {
|
|
361
|
-
let c = e.messageId, u = await s.getToken(), d = await axios_default.post(`/openapi/v1/app/chat-messages/${c}/suggested`, { user: f }, { headers: {
|
|
362
|
-
Authorization: `Bearer ${u}`,
|
|
363
|
-
appId: l
|
|
364
|
-
} });
|
|
365
|
-
return d.result === "success" ? d.data : [];
|
|
366
377
|
}, h = async (e) => {
|
|
367
|
-
let
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
appId: l
|
|
378
|
+
let l = e.messageId, d = await c.getToken(), f = await axios_default.post(`/openapi/v1/app/chat-messages/${l}/suggested`, { user: p }, { headers: {
|
|
379
|
+
Authorization: `Bearer ${d}`,
|
|
380
|
+
appId: u
|
|
371
381
|
} });
|
|
382
|
+
return f.result === "success" ? f.data : [];
|
|
372
383
|
}, g = async (e) => {
|
|
373
|
-
let
|
|
374
|
-
return await axios_default.get(`/openapi/v1/app/
|
|
375
|
-
Authorization: `Bearer ${
|
|
376
|
-
appId:
|
|
384
|
+
let l = await c.getToken();
|
|
385
|
+
return await axios_default.get(`/openapi/v1/app/conversations?${new URLSearchParams(e).toString()}`, { headers: {
|
|
386
|
+
Authorization: `Bearer ${l}`,
|
|
387
|
+
appId: u
|
|
377
388
|
} });
|
|
378
389
|
}, _ = async (e) => {
|
|
379
|
-
let
|
|
380
|
-
|
|
381
|
-
|
|
390
|
+
let l = await c.getToken();
|
|
391
|
+
return await axios_default.get(`/openapi/v1/app/messages?${new URLSearchParams(e).toString()}`, { headers: {
|
|
392
|
+
Authorization: `Bearer ${l}`,
|
|
393
|
+
appId: u
|
|
394
|
+
} });
|
|
395
|
+
}, v = async (e) => {
|
|
396
|
+
let { hitlData: l, prevMessage: d, text: f, inputs: m } = e, h = {
|
|
397
|
+
query: f || JSON.stringify(l || {}),
|
|
398
|
+
inputs: m || {},
|
|
382
399
|
response_mode: "streaming",
|
|
383
|
-
conversation_id:
|
|
384
|
-
|
|
400
|
+
conversation_id: d?.conversationId || null,
|
|
401
|
+
parent_message_id: d?.messageId || null,
|
|
402
|
+
user: p,
|
|
385
403
|
files: []
|
|
386
|
-
},
|
|
404
|
+
}, g = await c.getToken();
|
|
387
405
|
return {
|
|
388
406
|
url: "/openapi/v1/app/chat-messages",
|
|
389
407
|
config: {
|
|
390
408
|
method: "POST",
|
|
391
409
|
headers: {
|
|
392
410
|
"Content-Type": "application/json",
|
|
393
|
-
Authorization: `Bearer ${
|
|
394
|
-
appId:
|
|
411
|
+
Authorization: `Bearer ${g}`,
|
|
412
|
+
appId: u
|
|
395
413
|
},
|
|
396
|
-
body: JSON.stringify(
|
|
414
|
+
body: JSON.stringify(h)
|
|
397
415
|
}
|
|
398
416
|
};
|
|
399
|
-
},
|
|
400
|
-
let { data:
|
|
401
|
-
return JSON.parse(
|
|
402
|
-
},
|
|
403
|
-
|
|
404
|
-
},
|
|
405
|
-
|
|
406
|
-
},
|
|
407
|
-
|
|
408
|
-
}, S = async (e) => {
|
|
409
|
-
sseRequester(await _(e), {
|
|
410
|
-
formatReuqest: _,
|
|
411
|
-
getSuggestionQuestions: m,
|
|
412
|
-
getConversationList: h,
|
|
413
|
-
getMessageHistory: g,
|
|
414
|
-
onMessage: v,
|
|
415
|
-
onClose: y,
|
|
416
|
-
onError: b,
|
|
417
|
-
onOpen: x,
|
|
418
|
-
onCancel: p
|
|
419
|
-
});
|
|
417
|
+
}, y = (e) => {
|
|
418
|
+
let { data: c } = e;
|
|
419
|
+
return JSON.parse(c);
|
|
420
|
+
}, b = async () => {
|
|
421
|
+
l.log("onClose");
|
|
422
|
+
}, x = async (e) => {
|
|
423
|
+
l.log("onError", e);
|
|
424
|
+
}, S = async () => {
|
|
425
|
+
l.log("onOpen");
|
|
420
426
|
}, C = async (e) => {
|
|
421
|
-
|
|
427
|
+
sseRequester(await v(e), {
|
|
428
|
+
formatReuqest: v,
|
|
429
|
+
getSuggestionQuestions: h,
|
|
430
|
+
getConversationList: g,
|
|
431
|
+
getMessageHistory: _,
|
|
432
|
+
onMessage: y,
|
|
433
|
+
onClose: b,
|
|
434
|
+
onError: x,
|
|
435
|
+
onOpen: S,
|
|
436
|
+
onCancel: m
|
|
437
|
+
});
|
|
438
|
+
}, w = async (e) => {
|
|
439
|
+
let c = [], l;
|
|
422
440
|
if (e.data.forEach((e) => {
|
|
423
|
-
e.parent_message_id ===
|
|
441
|
+
e.parent_message_id === l ? (c.pop(), c.push({
|
|
424
442
|
role: "ai",
|
|
425
443
|
showActions: !1,
|
|
426
444
|
content: e.answer,
|
|
@@ -431,7 +449,7 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
431
449
|
name: e.filename
|
|
432
450
|
})),
|
|
433
451
|
placement: "start"
|
|
434
|
-
})) : (
|
|
452
|
+
})) : (l = e.parent_message_id, c.push({
|
|
435
453
|
role: "user",
|
|
436
454
|
messageId: e.id,
|
|
437
455
|
content: e.query,
|
|
@@ -439,7 +457,7 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
439
457
|
maxWidth: "100%",
|
|
440
458
|
placement: "end",
|
|
441
459
|
isHistoryMessage: !0
|
|
442
|
-
}),
|
|
460
|
+
}), c.push({
|
|
443
461
|
role: "ai",
|
|
444
462
|
content: e.answer,
|
|
445
463
|
showActions: !1,
|
|
@@ -453,19 +471,19 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
453
471
|
isHistoryMessage: !0
|
|
454
472
|
}));
|
|
455
473
|
}), e.has_more) {
|
|
456
|
-
let e = await
|
|
457
|
-
conversation_id:
|
|
458
|
-
user:
|
|
459
|
-
first_id:
|
|
474
|
+
let e = await _({
|
|
475
|
+
conversation_id: c[c.length - 1].conversationId,
|
|
476
|
+
user: p,
|
|
477
|
+
first_id: c[0].messageId
|
|
460
478
|
});
|
|
461
479
|
if (e.data) {
|
|
462
|
-
let
|
|
463
|
-
|
|
480
|
+
let l = await w(e);
|
|
481
|
+
c.unshift(...l);
|
|
464
482
|
}
|
|
465
483
|
}
|
|
466
|
-
return
|
|
467
|
-
},
|
|
468
|
-
let
|
|
484
|
+
return c;
|
|
485
|
+
}, T = async () => (pilotStore.getState().resetMessageHistory(), !0), E = async (e) => {
|
|
486
|
+
let c = {
|
|
469
487
|
messageId: void 0,
|
|
470
488
|
content: e.text,
|
|
471
489
|
role: "user",
|
|
@@ -473,123 +491,123 @@ function createCoreAgentAdapter(e, s, c) {
|
|
|
473
491
|
showActions: !1,
|
|
474
492
|
maxWidth: "100%"
|
|
475
493
|
};
|
|
476
|
-
return pilotStore.getState().updateMessageHistory([
|
|
494
|
+
return pilotStore.getState().updateMessageHistory([c]), pilotStore.getState().resetCurrentMessage(), sseRequester(await v({
|
|
477
495
|
text: e.text,
|
|
478
496
|
prevMessage: void 0,
|
|
479
497
|
inputs: e.inputs
|
|
480
498
|
}), {
|
|
481
|
-
formatReuqest:
|
|
482
|
-
getSuggestionQuestions:
|
|
483
|
-
getConversationList:
|
|
484
|
-
getMessageHistory:
|
|
485
|
-
onMessage:
|
|
486
|
-
onClose:
|
|
487
|
-
onError:
|
|
488
|
-
onOpen:
|
|
489
|
-
onCancel:
|
|
499
|
+
formatReuqest: v,
|
|
500
|
+
getSuggestionQuestions: h,
|
|
501
|
+
getConversationList: g,
|
|
502
|
+
getMessageHistory: _,
|
|
503
|
+
onMessage: y,
|
|
504
|
+
onClose: b,
|
|
505
|
+
onError: x,
|
|
506
|
+
onOpen: S,
|
|
507
|
+
onCancel: m
|
|
490
508
|
});
|
|
491
509
|
};
|
|
492
510
|
return {
|
|
493
|
-
formatReuqest:
|
|
494
|
-
getSuggestionQuestions:
|
|
495
|
-
getConversationList:
|
|
496
|
-
getMessageHistory:
|
|
497
|
-
onMessage:
|
|
498
|
-
onClose:
|
|
499
|
-
onError:
|
|
500
|
-
onOpen:
|
|
501
|
-
onCancel:
|
|
511
|
+
formatReuqest: v,
|
|
512
|
+
getSuggestionQuestions: h,
|
|
513
|
+
getConversationList: g,
|
|
514
|
+
getMessageHistory: _,
|
|
515
|
+
onMessage: y,
|
|
516
|
+
onClose: b,
|
|
517
|
+
onError: x,
|
|
518
|
+
onOpen: S,
|
|
519
|
+
onCancel: m,
|
|
502
520
|
triggerEvent: async (e) => {
|
|
503
521
|
switch (e.action) {
|
|
504
|
-
case "get_suggestion_questions": return await
|
|
505
|
-
case "get_conversation_list": return await
|
|
522
|
+
case "get_suggestion_questions": return await h(e.payload);
|
|
523
|
+
case "get_conversation_list": return await g(e.payload);
|
|
506
524
|
case "get_message_history":
|
|
507
|
-
let
|
|
508
|
-
return pilotStore.getState().resetMessageHistory(
|
|
509
|
-
case "submit_core_action": return await
|
|
510
|
-
case "new_message": return await
|
|
511
|
-
case "submit_message": return await
|
|
525
|
+
let c = await w(await _(e.payload));
|
|
526
|
+
return pilotStore.getState().resetMessageHistory(c), c;
|
|
527
|
+
case "submit_core_action": return await C(e.payload);
|
|
528
|
+
case "new_message": return await T();
|
|
529
|
+
case "submit_message": return await E(e.payload);
|
|
512
530
|
default: throw Error("Invalid event action.");
|
|
513
531
|
}
|
|
514
532
|
}
|
|
515
533
|
};
|
|
516
534
|
}
|
|
517
|
-
function createProtocolAdapter(e,
|
|
518
|
-
switch (
|
|
519
|
-
case "coreagent": return createCoreAgentAdapter(e,
|
|
520
|
-
default: throw Error(`Unsupported provider: ${
|
|
535
|
+
function createProtocolAdapter(e, c, l, u) {
|
|
536
|
+
switch (u.log(`Creating adapter for provider: ${c}`), c.name) {
|
|
537
|
+
case "coreagent": return createCoreAgentAdapter(e, l, u);
|
|
538
|
+
default: throw Error(`Unsupported provider: ${c.name}`);
|
|
521
539
|
}
|
|
522
540
|
}
|
|
523
|
-
function createHitlManager(e,
|
|
524
|
-
let
|
|
525
|
-
let
|
|
541
|
+
function createHitlManager(e, c, l) {
|
|
542
|
+
let u = (e) => e.split("_")[0], d = [], f = async (c) => {
|
|
543
|
+
let u = await e.formatReuqest(c);
|
|
526
544
|
sseRequester({
|
|
527
|
-
url:
|
|
528
|
-
config:
|
|
529
|
-
}, e),
|
|
530
|
-
},
|
|
531
|
-
|
|
532
|
-
},
|
|
545
|
+
url: u.url,
|
|
546
|
+
config: u.config
|
|
547
|
+
}, e), l.log("HITL data submitted successfully.");
|
|
548
|
+
}, p = async (e) => {
|
|
549
|
+
d.forEach((c) => c(e.hitlData));
|
|
550
|
+
}, m = async (e) => {};
|
|
533
551
|
return {
|
|
534
552
|
async submit(e) {
|
|
535
553
|
try {
|
|
536
|
-
let
|
|
537
|
-
switch (
|
|
554
|
+
let c = u(e.hitlData?.action ?? "");
|
|
555
|
+
switch (l.log("[HITL] Submitting HITL data...", e), c) {
|
|
538
556
|
case "core":
|
|
539
|
-
await
|
|
557
|
+
await f(e);
|
|
540
558
|
break;
|
|
541
559
|
case "cust":
|
|
542
|
-
await
|
|
560
|
+
await p(e);
|
|
543
561
|
break;
|
|
544
562
|
case "inner":
|
|
545
|
-
await
|
|
563
|
+
await m(e);
|
|
546
564
|
break;
|
|
547
565
|
default: throw Error("Invalid action type.");
|
|
548
566
|
}
|
|
549
567
|
} catch (e) {
|
|
550
|
-
throw
|
|
568
|
+
throw l.error("Error submitting HITL data:", e), e;
|
|
551
569
|
}
|
|
552
570
|
},
|
|
553
571
|
subscribeEvent(e) {
|
|
554
|
-
|
|
572
|
+
d.includes(e) || d.push(e);
|
|
555
573
|
},
|
|
556
574
|
unsubscribeEvent(e) {
|
|
557
|
-
|
|
575
|
+
d.splice(d.indexOf(e), 1);
|
|
558
576
|
}
|
|
559
577
|
};
|
|
560
578
|
}
|
|
561
579
|
function createCorePilot(e) {
|
|
562
580
|
if (!e.appId) throw Error("CorePilot SDK: `appId` is required.");
|
|
563
581
|
if (!e.mount.selector) throw Error("CorePilot SDK: `mount.selector` is required.");
|
|
564
|
-
let
|
|
565
|
-
|
|
566
|
-
let
|
|
582
|
+
let c = createLogger(e.debug ?? !1, e.appId, e.eventTracker);
|
|
583
|
+
c.log("CorePilot SDK Initializing...", e);
|
|
584
|
+
let l = { ...e }, u = createAuthManager(l.auth, l.baseUrl), d = createProtocolAdapter(l, l.provider, u, c), f = createHitlManager(d, u, c), p = createMountManager(l.mount, d, f, c, l.provider);
|
|
567
585
|
return {
|
|
568
586
|
mount() {
|
|
569
|
-
|
|
587
|
+
c.log("Mounting CorePilot UI..."), p.mount();
|
|
570
588
|
},
|
|
571
589
|
unmount() {
|
|
572
|
-
|
|
590
|
+
c.log("Unmounting CorePilot UI..."), p.unmount();
|
|
573
591
|
},
|
|
574
592
|
reload(e) {
|
|
575
|
-
|
|
576
|
-
...
|
|
593
|
+
c.log("Reloading CorePilot instance...", e), p.unmount(), l = {
|
|
594
|
+
...l,
|
|
577
595
|
...e
|
|
578
|
-
},
|
|
579
|
-
let
|
|
580
|
-
|
|
596
|
+
}, d = createProtocolAdapter(l, l.provider, u, c);
|
|
597
|
+
let m = createMountManager(l.mount, d, f, c, l.provider);
|
|
598
|
+
p.mount = m.mount, p.unmount = m.unmount, c.log("CorePilot reloaded. Remounting UI..."), p.mount();
|
|
581
599
|
},
|
|
582
600
|
destroy() {
|
|
583
|
-
|
|
601
|
+
c.log("Destroying CorePilot instance..."), p.unmount(), c.destroy();
|
|
584
602
|
},
|
|
585
603
|
async triggerEvent(e) {
|
|
586
|
-
return await
|
|
604
|
+
return await d.triggerEvent(e);
|
|
587
605
|
},
|
|
588
606
|
subscribeEvent(e) {
|
|
589
|
-
|
|
607
|
+
f.subscribeEvent(e);
|
|
590
608
|
},
|
|
591
609
|
unsubscribeEvent(e) {
|
|
592
|
-
|
|
610
|
+
f.unsubscribeEvent(e);
|
|
593
611
|
}
|
|
594
612
|
};
|
|
595
613
|
}
|