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