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