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