@mujian/js-sdk 0.0.6-beta.26 → 0.0.6-beta.28

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.
@@ -10,7 +10,7 @@ export type { Message };
10
10
  */
11
11
  export type UseChatProps = {
12
12
  body?: object;
13
- onError?: (e: Error) => void;
13
+ onError?: (e: unknown) => void;
14
14
  onFinish?: (message: Message) => void;
15
15
  };
16
16
  /**
@@ -24,7 +24,7 @@ export type UseChatProps = {
24
24
  export type UseChatReturn = {
25
25
  messages: Message[];
26
26
  status: 'uninitialized' | 'ready' | 'streaming' | 'error';
27
- error?: Error;
27
+ error?: unknown;
28
28
  /** 提交用户输入,让AI回答 */
29
29
  append: (query: string) => Promise<void>;
30
30
  /** 重新生成最后一条回答 */
package/dist/react.js CHANGED
@@ -96,7 +96,7 @@ function addDOMPurifyHooks() {
96
96
  * @returns {string} Encoded message text
97
97
  * @copyright https://github.com/kwaroran/risuAI
98
98
  */ function encodeStyleTags(text) {
99
- const styleRegex = /<style>(.+?)<\/style>/gi;
99
+ const styleRegex = /<style>([\s\S]+?)<\/style>/gi;
100
100
  return text.replaceAll(styleRegex, (_, match)=>`<custom-style>${encodeURIComponent(match)}</custom-style>`);
101
101
  }
102
102
  /**
@@ -947,39 +947,67 @@ const useChat = ({ body, onError, onFinish })=>{
947
947
  }, [
948
948
  isStreaming
949
949
  ]);
950
- const append = async (query)=>{
950
+ const append = useCallback(async (query)=>{
951
951
  if (isStreaming) throw new Error('useChat is streaming already.');
952
952
  const controller = new AbortController();
953
953
  setAbortController(controller);
954
- await chatStreaming({
955
- query,
956
- signal: controller.signal
957
- });
958
- setAbortController(void 0);
959
- };
960
- const regenerate = async ()=>{
954
+ try {
955
+ await chatStreaming({
956
+ query,
957
+ signal: controller.signal
958
+ });
959
+ } catch (e) {
960
+ setError(e);
961
+ throw e;
962
+ } finally{
963
+ setAbortController(void 0);
964
+ }
965
+ }, [
966
+ isStreaming,
967
+ chatStreaming
968
+ ]);
969
+ const regenerate = useCallback(async ()=>{
961
970
  if (isStreaming) throw new Error('useChat is streaming already.');
962
971
  const controller = new AbortController();
963
972
  setAbortController(controller);
964
973
  const lastMessage = messages.findLast((message)=>'user' === message.role);
965
- await chatStreaming({
966
- query: lastMessage?.content || '',
967
- regenerate: true,
968
- signal: controller.signal
969
- });
970
- setAbortController(void 0);
971
- };
972
- const continueGenerate = async ()=>{
974
+ try {
975
+ await chatStreaming({
976
+ query: lastMessage?.content || '',
977
+ regenerate: true,
978
+ signal: controller.signal
979
+ });
980
+ } catch (e) {
981
+ setError(e);
982
+ throw e;
983
+ } finally{
984
+ setAbortController(void 0);
985
+ }
986
+ }, [
987
+ isStreaming,
988
+ messages,
989
+ chatStreaming
990
+ ]);
991
+ const continueGenerate = useCallback(async ()=>{
973
992
  if (isStreaming) throw new Error('useChat is streaming already.');
974
993
  const controller = new AbortController();
975
994
  setAbortController(controller);
976
- await chatStreaming({
977
- query: '',
978
- is_continue: true,
979
- signal: controller.signal
980
- });
981
- setAbortController(void 0);
982
- };
995
+ try {
996
+ await chatStreaming({
997
+ query: '',
998
+ is_continue: true,
999
+ signal: controller.signal
1000
+ });
1001
+ } catch (e) {
1002
+ setError(e);
1003
+ throw e;
1004
+ } finally{
1005
+ setAbortController(void 0);
1006
+ }
1007
+ }, [
1008
+ isStreaming,
1009
+ chatStreaming
1010
+ ]);
983
1011
  const stop = ()=>{
984
1012
  abortController?.abort();
985
1013
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mujian/js-sdk",
3
- "version": "0.0.6-beta.26",
3
+ "version": "0.0.6-beta.28",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {