@yh-ui/ai-sdk 1.0.53 → 1.0.54

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.
@@ -432,7 +432,9 @@ function useAIChat(options) {
432
432
  onToolCall,
433
433
  onToolResult,
434
434
  onFinish,
435
- onError
435
+ onError,
436
+ enableFallback = false,
437
+ fallbackContent = "\u62B1\u6B49\uFF0C\u670D\u52A1\u6682\u65F6\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002"
436
438
  } = options;
437
439
  const messages = (0, _vue.ref)([...initialMessages]);
438
440
  const input = (0, _vue.ref)("");
@@ -695,6 +697,14 @@ function useAIChat(options) {
695
697
  const errorObj = err instanceof Error ? err : new Error(String(err));
696
698
  error.value = errorObj;
697
699
  onError?.(errorObj);
700
+ if (enableFallback) {
701
+ updateLastMessage({
702
+ content: fallbackContent
703
+ });
704
+ if (currentMessage.value) {
705
+ currentMessage.value.content = fallbackContent;
706
+ }
707
+ }
698
708
  }
699
709
  } finally {
700
710
  isLoading.value = false;
@@ -791,10 +801,24 @@ function useAIChat(options) {
791
801
  const errorObj = err instanceof Error ? err : new Error(String(err));
792
802
  error.value = errorObj;
793
803
  onError?.(errorObj);
804
+ if (enableFallback) {
805
+ append(fallbackContent, "assistant");
806
+ }
794
807
  } finally {
795
808
  isLoading.value = false;
796
809
  }
797
810
  };
811
+ const resend = async () => {
812
+ const userMessages = messages.value.filter(m => m.role === "user");
813
+ if (userMessages.length === 0) return;
814
+ const lastUserMessage = userMessages[userMessages.length - 1];
815
+ const lastUserIndex = messages.value.lastIndexOf(lastUserMessage);
816
+ if (lastUserIndex > -1) {
817
+ const previousMessages = messages.value.slice(0, lastUserIndex);
818
+ messages.value = previousMessages;
819
+ await sendMessage(lastUserMessage.content);
820
+ }
821
+ };
798
822
  return {
799
823
  messages,
800
824
  input,
@@ -807,7 +831,8 @@ function useAIChat(options) {
807
831
  stop,
808
832
  append,
809
833
  updateLastMessage,
810
- reload
834
+ reload,
835
+ resend
811
836
  };
812
837
  }
813
838
  function useAIStream(options) {
@@ -401,6 +401,10 @@ export interface UseAIChatOptions {
401
401
  onFinish?: (message: ConversationMessage) => void;
402
402
  /** 回调:出错 */
403
403
  onError?: (error: Error) => void;
404
+ /** 是否启用本地兜底内容 */
405
+ enableFallback?: boolean;
406
+ /** 兜底回复内容 */
407
+ fallbackContent?: string;
404
408
  }
405
409
  export interface UseAIChatReturn {
406
410
  /** 消息列表 */
@@ -427,6 +431,8 @@ export interface UseAIChatReturn {
427
431
  updateLastMessage: (updates: Partial<ConversationMessage>) => void;
428
432
  /** 重置 */
429
433
  reload: () => void;
434
+ /** 重新发送最后一条用户消息 */
435
+ resend: () => Promise<void>;
430
436
  }
431
437
  /**
432
438
  * AI 对话 hook (增强版)
@@ -378,7 +378,9 @@ export function useAIChat(options) {
378
378
  onToolCall,
379
379
  onToolResult,
380
380
  onFinish,
381
- onError
381
+ onError,
382
+ enableFallback = false,
383
+ fallbackContent = "\u62B1\u6B49\uFF0C\u670D\u52A1\u6682\u65F6\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002"
382
384
  } = options;
383
385
  const messages = ref([...initialMessages]);
384
386
  const input = ref("");
@@ -612,6 +614,12 @@ export function useAIChat(options) {
612
614
  const errorObj = err instanceof Error ? err : new Error(String(err));
613
615
  error.value = errorObj;
614
616
  onError?.(errorObj);
617
+ if (enableFallback) {
618
+ updateLastMessage({ content: fallbackContent });
619
+ if (currentMessage.value) {
620
+ currentMessage.value.content = fallbackContent;
621
+ }
622
+ }
615
623
  }
616
624
  } finally {
617
625
  isLoading.value = false;
@@ -698,10 +706,24 @@ export function useAIChat(options) {
698
706
  const errorObj = err instanceof Error ? err : new Error(String(err));
699
707
  error.value = errorObj;
700
708
  onError?.(errorObj);
709
+ if (enableFallback) {
710
+ append(fallbackContent, "assistant");
711
+ }
701
712
  } finally {
702
713
  isLoading.value = false;
703
714
  }
704
715
  };
716
+ const resend = async () => {
717
+ const userMessages = messages.value.filter((m) => m.role === "user");
718
+ if (userMessages.length === 0) return;
719
+ const lastUserMessage = userMessages[userMessages.length - 1];
720
+ const lastUserIndex = messages.value.lastIndexOf(lastUserMessage);
721
+ if (lastUserIndex > -1) {
722
+ const previousMessages = messages.value.slice(0, lastUserIndex);
723
+ messages.value = previousMessages;
724
+ await sendMessage(lastUserMessage.content);
725
+ }
726
+ };
705
727
  return {
706
728
  messages,
707
729
  input,
@@ -714,7 +736,8 @@ export function useAIChat(options) {
714
736
  stop,
715
737
  append,
716
738
  updateLastMessage,
717
- reload
739
+ reload,
740
+ resend
718
741
  };
719
742
  }
720
743
  export function useAIStream(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/ai-sdk",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "description": "YH-UI AI SDK integration for Vercel AI SDK and LangChain",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "peerDependencies": {
47
47
  "vue": "^3.5.35",
48
- "@yh-ui/components": "^1.0.53",
48
+ "@yh-ui/components": "^1.0.54",
49
49
  "@langchain/core": ">=0.3.0"
50
50
  },
51
51
  "peerDependenciesMeta": {
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@langchain/core": ">=0.3.0",
58
- "@yh-ui/components": "^1.0.53",
58
+ "@yh-ui/components": "^1.0.54",
59
59
  "typescript": "^5.7.3",
60
60
  "unbuild": "^3.3.1",
61
61
  "vitest": "^4.0.18",