@yh-ui/ai-sdk 1.0.53 → 1.0.55
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/vue/index.cjs +36 -6
- package/dist/vue/index.d.ts +6 -0
- package/dist/vue/index.mjs +34 -5
- package/package.json +3 -3
package/dist/vue/index.cjs
CHANGED
|
@@ -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)("");
|
|
@@ -655,6 +657,9 @@ function useAIChat(options) {
|
|
|
655
657
|
for (const result of toolResults) {
|
|
656
658
|
messages.value.push(result);
|
|
657
659
|
}
|
|
660
|
+
if (!abortController) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
658
663
|
const finalMessages = messages.value.map(m => ({
|
|
659
664
|
role: m.role,
|
|
660
665
|
content: m.content,
|
|
@@ -674,27 +679,37 @@ function useAIChat(options) {
|
|
|
674
679
|
body: JSON.stringify({
|
|
675
680
|
messages: finalMessages,
|
|
676
681
|
...body
|
|
677
|
-
})
|
|
682
|
+
}),
|
|
683
|
+
signal: abortController.signal
|
|
678
684
|
});
|
|
679
685
|
if (finalResponse.ok) {
|
|
680
686
|
const finalData = await finalResponse.json();
|
|
681
687
|
const finalContent = finalData.content || finalData.message?.content || "";
|
|
682
|
-
|
|
688
|
+
messages.value = messages.value.map(m => m.id === assistantMessage.id ? {
|
|
689
|
+
...m,
|
|
683
690
|
content: finalContent
|
|
684
|
-
});
|
|
691
|
+
} : m);
|
|
685
692
|
currentMessage.value = {
|
|
686
693
|
...assistantMessage,
|
|
687
694
|
content: finalContent
|
|
688
695
|
};
|
|
689
696
|
}
|
|
690
697
|
}
|
|
691
|
-
const finalMessage = messages.value
|
|
698
|
+
const finalMessage = messages.value.find(m => m.id === assistantMessage.id) || assistantMessage;
|
|
692
699
|
onFinish?.(finalMessage);
|
|
693
700
|
} catch (err) {
|
|
694
701
|
if (err.name !== "AbortError") {
|
|
695
702
|
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
696
703
|
error.value = errorObj;
|
|
697
704
|
onError?.(errorObj);
|
|
705
|
+
if (enableFallback) {
|
|
706
|
+
updateLastMessage({
|
|
707
|
+
content: fallbackContent
|
|
708
|
+
});
|
|
709
|
+
if (currentMessage.value) {
|
|
710
|
+
currentMessage.value.content = fallbackContent;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
698
713
|
}
|
|
699
714
|
} finally {
|
|
700
715
|
isLoading.value = false;
|
|
@@ -791,10 +806,24 @@ function useAIChat(options) {
|
|
|
791
806
|
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
792
807
|
error.value = errorObj;
|
|
793
808
|
onError?.(errorObj);
|
|
809
|
+
if (enableFallback) {
|
|
810
|
+
append(fallbackContent, "assistant");
|
|
811
|
+
}
|
|
794
812
|
} finally {
|
|
795
813
|
isLoading.value = false;
|
|
796
814
|
}
|
|
797
815
|
};
|
|
816
|
+
const resend = async () => {
|
|
817
|
+
const userMessages = messages.value.filter(m => m.role === "user");
|
|
818
|
+
if (userMessages.length === 0) return;
|
|
819
|
+
const lastUserMessage = userMessages[userMessages.length - 1];
|
|
820
|
+
const lastUserIndex = messages.value.lastIndexOf(lastUserMessage);
|
|
821
|
+
if (lastUserIndex > -1) {
|
|
822
|
+
const previousMessages = messages.value.slice(0, lastUserIndex);
|
|
823
|
+
messages.value = previousMessages;
|
|
824
|
+
await sendMessage(lastUserMessage.content);
|
|
825
|
+
}
|
|
826
|
+
};
|
|
798
827
|
return {
|
|
799
828
|
messages,
|
|
800
829
|
input,
|
|
@@ -807,7 +836,8 @@ function useAIChat(options) {
|
|
|
807
836
|
stop,
|
|
808
837
|
append,
|
|
809
838
|
updateLastMessage,
|
|
810
|
-
reload
|
|
839
|
+
reload,
|
|
840
|
+
resend
|
|
811
841
|
};
|
|
812
842
|
}
|
|
813
843
|
function useAIStream(options) {
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -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 (增强版)
|
package/dist/vue/index.mjs
CHANGED
|
@@ -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("");
|
|
@@ -581,6 +583,9 @@ export function useAIChat(options) {
|
|
|
581
583
|
for (const result of toolResults) {
|
|
582
584
|
messages.value.push(result);
|
|
583
585
|
}
|
|
586
|
+
if (!abortController) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
584
589
|
const finalMessages = messages.value.map((m) => ({
|
|
585
590
|
role: m.role,
|
|
586
591
|
content: m.content,
|
|
@@ -596,22 +601,31 @@ export function useAIChat(options) {
|
|
|
596
601
|
body: JSON.stringify({
|
|
597
602
|
messages: finalMessages,
|
|
598
603
|
...body
|
|
599
|
-
})
|
|
604
|
+
}),
|
|
605
|
+
signal: abortController.signal
|
|
600
606
|
});
|
|
601
607
|
if (finalResponse.ok) {
|
|
602
608
|
const finalData = await finalResponse.json();
|
|
603
609
|
const finalContent = finalData.content || finalData.message?.content || "";
|
|
604
|
-
|
|
610
|
+
messages.value = messages.value.map(
|
|
611
|
+
(m) => m.id === assistantMessage.id ? { ...m, content: finalContent } : m
|
|
612
|
+
);
|
|
605
613
|
currentMessage.value = { ...assistantMessage, content: finalContent };
|
|
606
614
|
}
|
|
607
615
|
}
|
|
608
|
-
const finalMessage = messages.value
|
|
616
|
+
const finalMessage = messages.value.find((m) => m.id === assistantMessage.id) || assistantMessage;
|
|
609
617
|
onFinish?.(finalMessage);
|
|
610
618
|
} catch (err) {
|
|
611
619
|
if (err.name !== "AbortError") {
|
|
612
620
|
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
613
621
|
error.value = errorObj;
|
|
614
622
|
onError?.(errorObj);
|
|
623
|
+
if (enableFallback) {
|
|
624
|
+
updateLastMessage({ content: fallbackContent });
|
|
625
|
+
if (currentMessage.value) {
|
|
626
|
+
currentMessage.value.content = fallbackContent;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
615
629
|
}
|
|
616
630
|
} finally {
|
|
617
631
|
isLoading.value = false;
|
|
@@ -698,10 +712,24 @@ export function useAIChat(options) {
|
|
|
698
712
|
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
699
713
|
error.value = errorObj;
|
|
700
714
|
onError?.(errorObj);
|
|
715
|
+
if (enableFallback) {
|
|
716
|
+
append(fallbackContent, "assistant");
|
|
717
|
+
}
|
|
701
718
|
} finally {
|
|
702
719
|
isLoading.value = false;
|
|
703
720
|
}
|
|
704
721
|
};
|
|
722
|
+
const resend = async () => {
|
|
723
|
+
const userMessages = messages.value.filter((m) => m.role === "user");
|
|
724
|
+
if (userMessages.length === 0) return;
|
|
725
|
+
const lastUserMessage = userMessages[userMessages.length - 1];
|
|
726
|
+
const lastUserIndex = messages.value.lastIndexOf(lastUserMessage);
|
|
727
|
+
if (lastUserIndex > -1) {
|
|
728
|
+
const previousMessages = messages.value.slice(0, lastUserIndex);
|
|
729
|
+
messages.value = previousMessages;
|
|
730
|
+
await sendMessage(lastUserMessage.content);
|
|
731
|
+
}
|
|
732
|
+
};
|
|
705
733
|
return {
|
|
706
734
|
messages,
|
|
707
735
|
input,
|
|
@@ -714,7 +742,8 @@ export function useAIChat(options) {
|
|
|
714
742
|
stop,
|
|
715
743
|
append,
|
|
716
744
|
updateLastMessage,
|
|
717
|
-
reload
|
|
745
|
+
reload,
|
|
746
|
+
resend
|
|
718
747
|
};
|
|
719
748
|
}
|
|
720
749
|
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.
|
|
3
|
+
"version": "1.0.55",
|
|
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.
|
|
48
|
+
"@yh-ui/components": "^1.0.55",
|
|
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.
|
|
58
|
+
"@yh-ui/components": "^1.0.55",
|
|
59
59
|
"typescript": "^5.7.3",
|
|
60
60
|
"unbuild": "^3.3.1",
|
|
61
61
|
"vitest": "^4.0.18",
|