@mujian/js-sdk 0.0.6-beta.7 → 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
  /** 用户输入 */
@@ -1,5 +1,6 @@
1
- import React from "react";
2
- import "./MdTheme.css";
1
+ /** biome-ignore-all lint/security/noDangerouslySetInnerHtml: <explanation> */
2
+ import React from 'react';
3
+ import './MdTheme.css';
3
4
  export type MdRendererProps = {
4
5
  /**
5
6
  * The markdown content to render
package/dist/react.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { useMount, useRequest, useUpdateEffect } from "ahooks";
2
- import react, { createContext, forwardRef, useCallback, useContext, useEffect, useRef, useState } from "react";
2
+ import react, { createContext, forwardRef, useCallback, useContext, useEffect, useState } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  import css_tools from "@adobe/css-tools";
5
5
  import dompurify from "dompurify";
@@ -236,49 +236,16 @@ function messageFormatting(mes, sanitizerOverrides = {}) {
236
236
  }
237
237
  const MdRendererBase = ({ content })=>{
238
238
  const mes = messageFormatting(content);
239
- const _mes = mes.replace(/<code(.*)>[\s\S]*?<\/code>/g, (match)=>{
240
- const code = match.replace("<code>", "").replace("</code>", "");
241
- const unescapeHTML = (str)=>{
242
- const named = {
243
- amp: "&",
244
- lt: "<",
245
- gt: ">",
246
- quot: '"',
247
- apos: "'",
248
- nbsp: "\u00A0"
249
- };
250
- return str.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (_m, body)=>{
251
- if ('#' === body[0]) {
252
- const isHex = body[1]?.toLowerCase() === 'x';
253
- const numStr = isHex ? body.slice(2) : body.slice(1);
254
- const codePoint = parseInt(numStr, isHex ? 16 : 10);
255
- if (Number.isFinite(codePoint)) try {
256
- return String.fromCodePoint(codePoint);
257
- } catch {}
258
- return _m;
259
- }
260
- const lower = body.toLowerCase();
261
- return Object.hasOwn(named, lower) ? named[lower] : _m;
262
- });
263
- };
264
- const unescapedCode = unescapeHTML(code);
265
- const srcdoc = unescapedCode.replace(/'/g, "&#39;");
266
- return "<iframe sandbox='allow-scripts' loading='lazy' referrerpolicy='no-referrer' style='width: 100%; height: 600px; border: none;' srcdoc='" + srcdoc + "'></iframe>";
267
- });
268
- const containerRef = useRef(null);
269
- useEffect(()=>{
270
- if (containerRef.current) containerRef.current.innerHTML = _mes;
271
- }, [
272
- _mes
273
- ]);
274
239
  return /*#__PURE__*/ jsx("div", {
275
240
  className: "mes_text",
276
- ref: containerRef
241
+ dangerouslySetInnerHTML: {
242
+ __html: mes
243
+ }
277
244
  });
278
245
  };
279
246
  const MdRenderer = /*#__PURE__*/ react.memo(MdRendererBase, (prev, next)=>prev.content === next.content);
280
- MdRendererBase.displayName = "MdRenderer";
281
- MdRenderer.displayName = "MdRenderer";
247
+ MdRendererBase.displayName = 'MdRenderer';
248
+ MdRenderer.displayName = 'MdRenderer';
282
249
  const chat_complete = async function(message, onData, signal) {
283
250
  return await this.call("mujian:ai:chat:complete", {
284
251
  content: message
@@ -374,9 +341,14 @@ class MujianSdk {
374
341
  pendingRequests = new Map();
375
342
  async init() {
376
343
  const handshake = new postmate.Model({
377
- reply: ({ id, complete, data })=>{
344
+ reply: ({ id, complete, data, error })=>{
378
345
  const call = this.pendingRequests.get(id);
379
346
  if (!call) return;
347
+ if (error) {
348
+ call.reject(error);
349
+ this.pendingRequests.delete(id);
350
+ return;
351
+ }
380
352
  call.onData?.(data);
381
353
  if (complete) {
382
354
  call.onComplete?.();
@@ -607,6 +579,7 @@ async function* callSdk(mujian, content, type = 'generate', signal) {
607
579
  resolveWait = resolve;
608
580
  });
609
581
  }
582
+ const NOT_SAVED_MSG_ID_PREFIX = 'not_saved';
610
583
  const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
611
584
  const { body } = common;
612
585
  const [isStreaming, setIsStreaming] = useState(false);
@@ -615,7 +588,7 @@ const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
615
588
  setError(void 0);
616
589
  setIsStreaming(true);
617
590
  const userMessage = {
618
- id: Date.now().toString(),
591
+ id: `${NOT_SAVED_MSG_ID_PREFIX}_user_${Date.now()}`,
619
592
  content: query || '',
620
593
  role: 'user',
621
594
  swipes: [],
@@ -624,7 +597,7 @@ const useChatStreaming = ({ common, setError, setMessages, mujian })=>{
624
597
  sendAt: new Date()
625
598
  };
626
599
  const aiMessage = {
627
- id: Date.now().toString() + '1',
600
+ id: `${NOT_SAVED_MSG_ID_PREFIX}_assistant_${Date.now()}`,
628
601
  content: '',
629
602
  role: 'assistant',
630
603
  swipes: [],
@@ -836,7 +809,7 @@ const useChat = ({ body, onError, onFinish })=>{
836
809
  setMessages((prev)=>prev.map((msg)=>msg.id === messageId ? patchMessage(msg) : msg));
837
810
  };
838
811
  const deleteMessage = async (messageId)=>{
839
- await mujian.ai.chat.message.deleteOne(messageId);
812
+ if (!messageId.startsWith(NOT_SAVED_MSG_ID_PREFIX)) await mujian.ai.chat.message.deleteOne(messageId);
840
813
  setMessages((prev)=>prev.filter((msg)=>msg.id !== messageId));
841
814
  };
842
815
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mujian/js-sdk",
3
- "version": "0.0.6-beta.7",
3
+ "version": "0.0.6-beta.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {