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