@mujian/js-sdk 0.0.6-beta.8 → 0.0.6-beta.9
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.js
CHANGED
|
@@ -113,9 +113,14 @@ class MujianSdk {
|
|
|
113
113
|
pendingRequests = new Map();
|
|
114
114
|
async init() {
|
|
115
115
|
const handshake = new postmate.Model({
|
|
116
|
-
reply: ({ id, complete, data })=>{
|
|
116
|
+
reply: ({ id, complete, data, error })=>{
|
|
117
117
|
const call = this.pendingRequests.get(id);
|
|
118
118
|
if (!call) return;
|
|
119
|
+
if (error) {
|
|
120
|
+
call.reject(error);
|
|
121
|
+
this.pendingRequests.delete(id);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
119
124
|
call.onData?.(data);
|
|
120
125
|
if (complete) {
|
|
121
126
|
call.onComplete?.();
|
|
@@ -8,6 +8,7 @@ interface ChatStreamingProps {
|
|
|
8
8
|
setMessages: Dispatch<SetStateAction<Message[]>>;
|
|
9
9
|
mujian: MujianSdk;
|
|
10
10
|
}
|
|
11
|
+
export declare const NOT_SAVED_MSG_ID_PREFIX = "not_saved";
|
|
11
12
|
export declare const useChatStreaming: ({ common, setError, setMessages, mujian, }: ChatStreamingProps) => {
|
|
12
13
|
chatStreaming: ({ query, regenerate, is_continue, signal, }: {
|
|
13
14
|
/** 用户输入 */
|
package/dist/react.js
CHANGED
|
@@ -234,37 +234,8 @@ function messageFormatting(mes, sanitizerOverrides = {}) {
|
|
|
234
234
|
});
|
|
235
235
|
return mes;
|
|
236
236
|
}
|
|
237
|
-
const unescapeHTML = (str)=>{
|
|
238
|
-
const named = {
|
|
239
|
-
amp: "&",
|
|
240
|
-
lt: "<",
|
|
241
|
-
gt: ">",
|
|
242
|
-
quot: '"',
|
|
243
|
-
apos: "'",
|
|
244
|
-
nbsp: "\u00A0"
|
|
245
|
-
};
|
|
246
|
-
return str.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (_m, body)=>{
|
|
247
|
-
if ("#" === body[0]) {
|
|
248
|
-
const isHex = body[1]?.toLowerCase() === "x";
|
|
249
|
-
const numStr = isHex ? body.slice(2) : body.slice(1);
|
|
250
|
-
const codePoint = parseInt(numStr, isHex ? 16 : 10);
|
|
251
|
-
if (Number.isFinite(codePoint)) try {
|
|
252
|
-
return String.fromCodePoint(codePoint);
|
|
253
|
-
} catch {}
|
|
254
|
-
return _m;
|
|
255
|
-
}
|
|
256
|
-
const lower = body.toLowerCase();
|
|
257
|
-
return Object.hasOwn(named, lower) ? named[lower] : _m;
|
|
258
|
-
});
|
|
259
|
-
};
|
|
260
237
|
const MdRendererBase = ({ content })=>{
|
|
261
|
-
|
|
262
|
-
mes = mes.replace(/<code(.*)>[\s\S]*?<\/code>/g, (match)=>{
|
|
263
|
-
const code = match.replace("<code>", "").replace("</code>", "");
|
|
264
|
-
const unescapedCode = unescapeHTML(code);
|
|
265
|
-
const srcdoc = unescapedCode.replace(/'/g, "'");
|
|
266
|
-
return "<iframe sandbox='allow-scripts' loading='lazy' referrerpolicy='no-referrer' style='width: 100%; height: 600px; border: none;' srcdoc='" + srcdoc + "'></iframe>";
|
|
267
|
-
});
|
|
238
|
+
const mes = messageFormatting(content);
|
|
268
239
|
return /*#__PURE__*/ jsx("div", {
|
|
269
240
|
className: "mes_text",
|
|
270
241
|
dangerouslySetInnerHTML: {
|
|
@@ -273,8 +244,8 @@ const MdRendererBase = ({ content })=>{
|
|
|
273
244
|
});
|
|
274
245
|
};
|
|
275
246
|
const MdRenderer = /*#__PURE__*/ react.memo(MdRendererBase, (prev, next)=>prev.content === next.content);
|
|
276
|
-
MdRendererBase.displayName =
|
|
277
|
-
MdRenderer.displayName =
|
|
247
|
+
MdRendererBase.displayName = 'MdRenderer';
|
|
248
|
+
MdRenderer.displayName = 'MdRenderer';
|
|
278
249
|
const chat_complete = async function(message, onData, signal) {
|
|
279
250
|
return await this.call("mujian:ai:chat:complete", {
|
|
280
251
|
content: message
|
|
@@ -370,9 +341,14 @@ class MujianSdk {
|
|
|
370
341
|
pendingRequests = new Map();
|
|
371
342
|
async init() {
|
|
372
343
|
const handshake = new postmate.Model({
|
|
373
|
-
reply: ({ id, complete, data })=>{
|
|
344
|
+
reply: ({ id, complete, data, error })=>{
|
|
374
345
|
const call = this.pendingRequests.get(id);
|
|
375
346
|
if (!call) return;
|
|
347
|
+
if (error) {
|
|
348
|
+
call.reject(error);
|
|
349
|
+
this.pendingRequests.delete(id);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
376
352
|
call.onData?.(data);
|
|
377
353
|
if (complete) {
|
|
378
354
|
call.onComplete?.();
|
|
@@ -603,6 +579,7 @@ async function* callSdk(mujian, content, type = 'generate', signal) {
|
|
|
603
579
|
resolveWait = resolve;
|
|
604
580
|
});
|
|
605
581
|
}
|
|
582
|
+
const NOT_SAVED_MSG_ID_PREFIX = 'not_saved';
|
|
606
583
|
const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
|
|
607
584
|
const { body } = common;
|
|
608
585
|
const [isStreaming, setIsStreaming] = useState(false);
|
|
@@ -611,7 +588,7 @@ const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
|
|
|
611
588
|
setError(void 0);
|
|
612
589
|
setIsStreaming(true);
|
|
613
590
|
const userMessage = {
|
|
614
|
-
id: Date.now()
|
|
591
|
+
id: `${NOT_SAVED_MSG_ID_PREFIX}_user_${Date.now()}`,
|
|
615
592
|
content: query || '',
|
|
616
593
|
role: 'user',
|
|
617
594
|
swipes: [],
|
|
@@ -620,7 +597,7 @@ const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
|
|
|
620
597
|
sendAt: new Date()
|
|
621
598
|
};
|
|
622
599
|
const aiMessage = {
|
|
623
|
-
id: Date.now()
|
|
600
|
+
id: `${NOT_SAVED_MSG_ID_PREFIX}_assistant_${Date.now()}`,
|
|
624
601
|
content: '',
|
|
625
602
|
role: 'assistant',
|
|
626
603
|
swipes: [],
|
|
@@ -832,7 +809,7 @@ const useChat = ({ body, onError, onFinish })=>{
|
|
|
832
809
|
setMessages((prev)=>prev.map((msg)=>msg.id === messageId ? patchMessage(msg) : msg));
|
|
833
810
|
};
|
|
834
811
|
const deleteMessage = async (messageId)=>{
|
|
835
|
-
await mujian.ai.chat.message.deleteOne(messageId);
|
|
812
|
+
if (!messageId.startsWith(NOT_SAVED_MSG_ID_PREFIX)) await mujian.ai.chat.message.deleteOne(messageId);
|
|
836
813
|
setMessages((prev)=>prev.filter((msg)=>msg.id !== messageId));
|
|
837
814
|
};
|
|
838
815
|
return {
|