@yh-ui/hooks 0.1.7 → 0.1.12
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.cjs +623 -12
- package/dist/index.d.cts +333 -7
- package/dist/index.d.mts +333 -7
- package/dist/index.d.ts +333 -7
- package/dist/index.mjs +615 -13
- package/dist/use-ai/index.cjs +38 -0
- package/dist/use-ai/index.d.ts +3 -0
- package/dist/use-ai/index.mjs +3 -0
- package/dist/use-ai/use-ai-chat.cjs +193 -0
- package/dist/use-ai/use-ai-chat.d.ts +115 -0
- package/dist/use-ai/use-ai-chat.mjs +182 -0
- package/dist/use-ai/use-ai-conversations.cjs +254 -0
- package/dist/use-ai/use-ai-conversations.d.ts +148 -0
- package/dist/use-ai/use-ai-conversations.mjs +241 -0
- package/dist/use-ai/use-ai-stream.cjs +190 -0
- package/dist/use-ai/use-ai-stream.d.ts +64 -0
- package/dist/use-ai/use-ai-stream.mjs +172 -0
- package/dist/use-form-item/index.d.ts +1 -1
- package/dist/use-locale/dayjs-locale.cjs +19 -12
- package/dist/use-locale/dayjs-locale.d.ts +1 -8
- package/dist/use-locale/dayjs-locale.mjs +20 -12
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
|
@@ -2,10 +2,6 @@ import { inject, ref, unref, computed, watch, useId as useId$1, shallowRef, onMo
|
|
|
2
2
|
import { zhCn } from '@yh-ui/locale';
|
|
3
3
|
import * as _dayjs from 'dayjs';
|
|
4
4
|
import 'dayjs/locale/en';
|
|
5
|
-
import 'dayjs/locale/zh-cn';
|
|
6
|
-
import 'dayjs/locale/zh-tw';
|
|
7
|
-
import 'dayjs/locale/ja';
|
|
8
|
-
import 'dayjs/locale/ko';
|
|
9
5
|
|
|
10
6
|
const defaultNamespace = "yh";
|
|
11
7
|
const statePrefix = "is-";
|
|
@@ -145,13 +141,16 @@ const useConfig = () => {
|
|
|
145
141
|
};
|
|
146
142
|
|
|
147
143
|
const dayjs = _dayjs.default || _dayjs;
|
|
148
|
-
const
|
|
144
|
+
const dayjsLocales = import.meta.glob(
|
|
145
|
+
["../../../../node_modules/dayjs/locale/*.js", "!../../../../node_modules/dayjs/locale/en.js"],
|
|
146
|
+
{ eager: false }
|
|
147
|
+
);
|
|
148
|
+
const loadedLocales = /* @__PURE__ */ new Set(["en"]);
|
|
149
149
|
const localeMapping = {
|
|
150
150
|
"zh-cn": "zh-cn",
|
|
151
151
|
"zh-tw": "zh-tw",
|
|
152
152
|
"zh-hk": "zh-hk",
|
|
153
153
|
"zh-mo": "zh-tw",
|
|
154
|
-
// 澳门使用繁体
|
|
155
154
|
en: "en",
|
|
156
155
|
ja: "ja",
|
|
157
156
|
ko: "ko",
|
|
@@ -225,12 +224,21 @@ const setDayjsLocale = async (localeCode) => {
|
|
|
225
224
|
dayjs.locale(dayjsLocale);
|
|
226
225
|
return;
|
|
227
226
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
227
|
+
if (dayjsLocale === "en") {
|
|
228
|
+
dayjs.locale("en");
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const path = `../../../../node_modules/dayjs/locale/${dayjsLocale}.js`;
|
|
232
|
+
const loader = dayjsLocales[path];
|
|
233
|
+
if (loader) {
|
|
234
|
+
try {
|
|
235
|
+
await loader();
|
|
236
|
+
loadedLocales.add(dayjsLocale);
|
|
237
|
+
dayjs.locale(dayjsLocale);
|
|
238
|
+
} catch {
|
|
239
|
+
dayjs.locale("en");
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
234
242
|
dayjs.locale("en");
|
|
235
243
|
}
|
|
236
244
|
};
|
|
@@ -539,4 +547,598 @@ function useClickOutside(target, handler) {
|
|
|
539
547
|
useEventListener(window, "touchstart", listener, true);
|
|
540
548
|
}
|
|
541
549
|
|
|
542
|
-
|
|
550
|
+
const openaiParser = (raw) => {
|
|
551
|
+
const lines = raw.split("\n");
|
|
552
|
+
let text = "";
|
|
553
|
+
for (const line of lines) {
|
|
554
|
+
if (!line.startsWith("data: ")) continue;
|
|
555
|
+
const data = line.slice(6).trim();
|
|
556
|
+
if (data === "[DONE]") break;
|
|
557
|
+
try {
|
|
558
|
+
const json = JSON.parse(data);
|
|
559
|
+
const delta = json?.choices?.[0]?.delta?.content;
|
|
560
|
+
if (delta) text += delta;
|
|
561
|
+
} catch {
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return text || null;
|
|
565
|
+
};
|
|
566
|
+
const ernieParser = (raw) => {
|
|
567
|
+
const lines = raw.split("\n");
|
|
568
|
+
let text = "";
|
|
569
|
+
for (const line of lines) {
|
|
570
|
+
if (!line.startsWith("data: ")) continue;
|
|
571
|
+
const data = line.slice(6).trim();
|
|
572
|
+
try {
|
|
573
|
+
const json = JSON.parse(data);
|
|
574
|
+
if (json?.result) text += json.result;
|
|
575
|
+
} catch {
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return text || null;
|
|
579
|
+
};
|
|
580
|
+
const qwenParser = (raw) => {
|
|
581
|
+
const lines = raw.split("\n");
|
|
582
|
+
let text = "";
|
|
583
|
+
for (const line of lines) {
|
|
584
|
+
if (!line.startsWith("data: ")) continue;
|
|
585
|
+
const data = line.slice(6).trim();
|
|
586
|
+
try {
|
|
587
|
+
const json = JSON.parse(data);
|
|
588
|
+
const t = json?.output?.text;
|
|
589
|
+
if (t) text += t;
|
|
590
|
+
} catch {
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return text || null;
|
|
594
|
+
};
|
|
595
|
+
const plainTextParser = (raw) => raw || null;
|
|
596
|
+
class TypewriterThrottle {
|
|
597
|
+
queue = [];
|
|
598
|
+
rafId = null;
|
|
599
|
+
onUpdate;
|
|
600
|
+
charsPerFrame;
|
|
601
|
+
constructor(onUpdate, charsPerFrame = 3) {
|
|
602
|
+
this.onUpdate = onUpdate;
|
|
603
|
+
this.charsPerFrame = charsPerFrame;
|
|
604
|
+
}
|
|
605
|
+
push(text) {
|
|
606
|
+
this.queue.push(...text.split(""));
|
|
607
|
+
if (this.rafId === null) {
|
|
608
|
+
this.schedule();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
schedule() {
|
|
612
|
+
this.rafId = requestAnimationFrame(() => {
|
|
613
|
+
this.rafId = null;
|
|
614
|
+
if (this.queue.length === 0) return;
|
|
615
|
+
const batch = this.queue.splice(0, this.charsPerFrame).join("");
|
|
616
|
+
this.onUpdate(batch);
|
|
617
|
+
if (this.queue.length > 0) {
|
|
618
|
+
this.schedule();
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
flush() {
|
|
623
|
+
if (this.rafId !== null) {
|
|
624
|
+
cancelAnimationFrame(this.rafId);
|
|
625
|
+
this.rafId = null;
|
|
626
|
+
}
|
|
627
|
+
if (this.queue.length > 0) {
|
|
628
|
+
const remaining = this.queue.splice(0).join("");
|
|
629
|
+
this.onUpdate(remaining);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
cancel() {
|
|
633
|
+
if (this.rafId !== null) {
|
|
634
|
+
cancelAnimationFrame(this.rafId);
|
|
635
|
+
this.rafId = null;
|
|
636
|
+
}
|
|
637
|
+
this.queue = [];
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
function useAiStream(options) {
|
|
641
|
+
const isStreaming = ref(false);
|
|
642
|
+
const currentContent = ref("");
|
|
643
|
+
let abortController = new AbortController();
|
|
644
|
+
let typewriter = null;
|
|
645
|
+
const parser = options.parser ?? plainTextParser;
|
|
646
|
+
const enableTypewriter = options.typewriter !== false;
|
|
647
|
+
const charsPerFrame = options.charsPerFrame ?? 3;
|
|
648
|
+
const stop = () => {
|
|
649
|
+
if (isStreaming.value) {
|
|
650
|
+
abortController.abort();
|
|
651
|
+
isStreaming.value = false;
|
|
652
|
+
typewriter?.flush();
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
const fetchStream = async (query, ...args) => {
|
|
656
|
+
isStreaming.value = true;
|
|
657
|
+
currentContent.value = "";
|
|
658
|
+
abortController = new AbortController();
|
|
659
|
+
if (enableTypewriter) {
|
|
660
|
+
typewriter = new TypewriterThrottle((chunk) => {
|
|
661
|
+
currentContent.value += chunk;
|
|
662
|
+
options.onUpdate?.(chunk, currentContent.value);
|
|
663
|
+
}, charsPerFrame);
|
|
664
|
+
}
|
|
665
|
+
const pushText = (text) => {
|
|
666
|
+
if (!text) return;
|
|
667
|
+
if (enableTypewriter && typewriter) {
|
|
668
|
+
typewriter.push(text);
|
|
669
|
+
} else {
|
|
670
|
+
currentContent.value += text;
|
|
671
|
+
options.onUpdate?.(text, currentContent.value);
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
try {
|
|
675
|
+
const response = await options.request(query, ...args);
|
|
676
|
+
if (typeof response === "object" && response !== null && Symbol.asyncIterator in response) {
|
|
677
|
+
for await (const chunk of response) {
|
|
678
|
+
if (abortController.signal.aborted) break;
|
|
679
|
+
const parsed = parser(chunk);
|
|
680
|
+
if (parsed) pushText(parsed);
|
|
681
|
+
}
|
|
682
|
+
} else if (response instanceof Response && response.body) {
|
|
683
|
+
const reader = response.body.getReader();
|
|
684
|
+
const decoder = new TextDecoder("utf-8");
|
|
685
|
+
while (true) {
|
|
686
|
+
if (abortController.signal.aborted) {
|
|
687
|
+
reader.cancel();
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
const { done, value } = await reader.read();
|
|
691
|
+
if (done) break;
|
|
692
|
+
const chunkStr = decoder.decode(value, { stream: true });
|
|
693
|
+
const parsed = parser(chunkStr);
|
|
694
|
+
if (parsed) pushText(parsed);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
if (!abortController.signal.aborted) {
|
|
698
|
+
if (enableTypewriter && typewriter) {
|
|
699
|
+
typewriter.flush();
|
|
700
|
+
}
|
|
701
|
+
isStreaming.value = false;
|
|
702
|
+
options.onFinish?.(currentContent.value);
|
|
703
|
+
}
|
|
704
|
+
} catch (e) {
|
|
705
|
+
if (e.name !== "AbortError") {
|
|
706
|
+
options.onError?.(e);
|
|
707
|
+
}
|
|
708
|
+
typewriter?.cancel();
|
|
709
|
+
isStreaming.value = false;
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
return {
|
|
713
|
+
isStreaming,
|
|
714
|
+
currentContent,
|
|
715
|
+
fetchStream,
|
|
716
|
+
stop,
|
|
717
|
+
// 暴露解析器供测试/自定义使用
|
|
718
|
+
parsers: { openaiParser, ernieParser, qwenParser, plainTextParser }
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function createTypewriter(onChar, charsPerFrame) {
|
|
723
|
+
const queue = [];
|
|
724
|
+
let rafId = null;
|
|
725
|
+
const schedule = () => {
|
|
726
|
+
rafId = requestAnimationFrame(() => {
|
|
727
|
+
rafId = null;
|
|
728
|
+
if (queue.length === 0) return;
|
|
729
|
+
const batch = queue.splice(0, charsPerFrame).join("");
|
|
730
|
+
onChar(batch);
|
|
731
|
+
if (queue.length > 0) schedule();
|
|
732
|
+
});
|
|
733
|
+
};
|
|
734
|
+
return {
|
|
735
|
+
push(text) {
|
|
736
|
+
queue.push(...text.split(""));
|
|
737
|
+
if (rafId === null) schedule();
|
|
738
|
+
},
|
|
739
|
+
flush() {
|
|
740
|
+
if (rafId !== null) {
|
|
741
|
+
cancelAnimationFrame(rafId);
|
|
742
|
+
rafId = null;
|
|
743
|
+
}
|
|
744
|
+
if (queue.length > 0) {
|
|
745
|
+
onChar(queue.splice(0).join(""));
|
|
746
|
+
}
|
|
747
|
+
},
|
|
748
|
+
cancel() {
|
|
749
|
+
if (rafId !== null) {
|
|
750
|
+
cancelAnimationFrame(rafId);
|
|
751
|
+
rafId = null;
|
|
752
|
+
}
|
|
753
|
+
queue.length = 0;
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
function useAiChat(options = {}) {
|
|
758
|
+
const {
|
|
759
|
+
idGenerator = () => Math.random().toString(36).substring(2, 9),
|
|
760
|
+
parser = plainTextParser,
|
|
761
|
+
typewriter: enableTypewriter = true,
|
|
762
|
+
charsPerFrame = 3,
|
|
763
|
+
systemPrompt
|
|
764
|
+
} = options;
|
|
765
|
+
const messages = ref(options.initialMessages ?? []);
|
|
766
|
+
const isGenerating = ref(false);
|
|
767
|
+
const isSending = computed(() => isGenerating.value);
|
|
768
|
+
let abortController = null;
|
|
769
|
+
const stop = () => {
|
|
770
|
+
if (abortController && isGenerating.value) {
|
|
771
|
+
abortController.abort();
|
|
772
|
+
isGenerating.value = false;
|
|
773
|
+
const lastMsg = messages.value[messages.value.length - 1];
|
|
774
|
+
if (lastMsg?.role === "assistant" && lastMsg.status === "generating") {
|
|
775
|
+
lastMsg.status = "stopped";
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
const clear = () => {
|
|
780
|
+
stop();
|
|
781
|
+
messages.value = [];
|
|
782
|
+
};
|
|
783
|
+
const removeMessage = (id) => {
|
|
784
|
+
const idx = messages.value.findIndex((m) => m.id === id);
|
|
785
|
+
if (idx !== -1) messages.value.splice(idx, 1);
|
|
786
|
+
};
|
|
787
|
+
const updateMessage = (id, patch) => {
|
|
788
|
+
const msg = messages.value.find((m) => m.id === id);
|
|
789
|
+
if (msg) Object.assign(msg, patch);
|
|
790
|
+
};
|
|
791
|
+
const sendMessage = async (content) => {
|
|
792
|
+
if (!content.trim() || isGenerating.value) return;
|
|
793
|
+
messages.value.push({
|
|
794
|
+
id: idGenerator(),
|
|
795
|
+
role: "user",
|
|
796
|
+
content,
|
|
797
|
+
createAt: Date.now(),
|
|
798
|
+
status: "success"
|
|
799
|
+
});
|
|
800
|
+
if (!options.request) return;
|
|
801
|
+
const assId = idGenerator();
|
|
802
|
+
const assistantMsg = {
|
|
803
|
+
id: assId,
|
|
804
|
+
role: "assistant",
|
|
805
|
+
content: "",
|
|
806
|
+
createAt: Date.now(),
|
|
807
|
+
status: "loading"
|
|
808
|
+
};
|
|
809
|
+
messages.value.push(assistantMsg);
|
|
810
|
+
isGenerating.value = true;
|
|
811
|
+
abortController = new AbortController();
|
|
812
|
+
const history = [];
|
|
813
|
+
if (systemPrompt) {
|
|
814
|
+
history.push({
|
|
815
|
+
id: "system",
|
|
816
|
+
role: "system",
|
|
817
|
+
content: systemPrompt,
|
|
818
|
+
createAt: 0,
|
|
819
|
+
status: "success"
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
history.push(...messages.value.slice(0, -2));
|
|
823
|
+
try {
|
|
824
|
+
const response = await options.request(content, history, abortController.signal);
|
|
825
|
+
const targetMsg = messages.value.find((m) => m.id === assId);
|
|
826
|
+
targetMsg.status = "generating";
|
|
827
|
+
let typewriterInstance = null;
|
|
828
|
+
if (enableTypewriter && typeof requestAnimationFrame !== "undefined") {
|
|
829
|
+
typewriterInstance = createTypewriter((chars) => {
|
|
830
|
+
targetMsg.content += chars;
|
|
831
|
+
}, charsPerFrame);
|
|
832
|
+
}
|
|
833
|
+
const pushChunk = (text) => {
|
|
834
|
+
if (!text) return;
|
|
835
|
+
if (typewriterInstance) {
|
|
836
|
+
typewriterInstance.push(text);
|
|
837
|
+
} else {
|
|
838
|
+
targetMsg.content += text;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
if (typeof response === "object" && response !== null && Symbol.asyncIterator in response) {
|
|
842
|
+
for await (const chunk of response) {
|
|
843
|
+
if (abortController.signal.aborted) break;
|
|
844
|
+
const parsed = parser(chunk);
|
|
845
|
+
if (parsed) pushChunk(parsed);
|
|
846
|
+
}
|
|
847
|
+
} else if (response instanceof Response && response.body) {
|
|
848
|
+
const reader = response.body.getReader();
|
|
849
|
+
const decoder = new TextDecoder("utf-8");
|
|
850
|
+
while (true) {
|
|
851
|
+
if (abortController.signal.aborted) {
|
|
852
|
+
reader.cancel();
|
|
853
|
+
break;
|
|
854
|
+
}
|
|
855
|
+
const { done, value } = await reader.read();
|
|
856
|
+
if (done) break;
|
|
857
|
+
const chunkStr = decoder.decode(value, { stream: true });
|
|
858
|
+
const parsed = parser(chunkStr);
|
|
859
|
+
if (parsed) pushChunk(parsed);
|
|
860
|
+
}
|
|
861
|
+
} else if (typeof response === "string") {
|
|
862
|
+
pushChunk(response);
|
|
863
|
+
}
|
|
864
|
+
if (typewriterInstance) {
|
|
865
|
+
typewriterInstance.flush();
|
|
866
|
+
}
|
|
867
|
+
if (!abortController.signal.aborted) {
|
|
868
|
+
targetMsg.status = "success";
|
|
869
|
+
options.onFinish?.(targetMsg);
|
|
870
|
+
}
|
|
871
|
+
} catch (e) {
|
|
872
|
+
if (e.name !== "AbortError") {
|
|
873
|
+
const targetMsg = messages.value.find((m) => m.id === assId);
|
|
874
|
+
if (targetMsg) targetMsg.status = "error";
|
|
875
|
+
options.onError?.(e);
|
|
876
|
+
}
|
|
877
|
+
} finally {
|
|
878
|
+
if (isGenerating.value) {
|
|
879
|
+
isGenerating.value = false;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
return {
|
|
884
|
+
/** 会话历史 */
|
|
885
|
+
messages,
|
|
886
|
+
/** 是否正在生成(等同 isSending,别名友好) */
|
|
887
|
+
isGenerating,
|
|
888
|
+
/** 同 isGenerating,语义别名 */
|
|
889
|
+
isSending,
|
|
890
|
+
/** 触发发送(自动处理流、打字机) */
|
|
891
|
+
sendMessage,
|
|
892
|
+
/** 停止/中断当前生成 */
|
|
893
|
+
stop,
|
|
894
|
+
/** 移除单条消息 */
|
|
895
|
+
removeMessage,
|
|
896
|
+
/** 修改单条消息内容 */
|
|
897
|
+
updateMessage,
|
|
898
|
+
/** 重置清空所有会话 */
|
|
899
|
+
clear
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const localStorageAdapter = {
|
|
904
|
+
getItem: (key) => {
|
|
905
|
+
try {
|
|
906
|
+
return localStorage.getItem(key);
|
|
907
|
+
} catch {
|
|
908
|
+
return null;
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
setItem: (key, value) => {
|
|
912
|
+
try {
|
|
913
|
+
localStorage.setItem(key, value);
|
|
914
|
+
} catch {
|
|
915
|
+
}
|
|
916
|
+
},
|
|
917
|
+
removeItem: (key) => {
|
|
918
|
+
try {
|
|
919
|
+
localStorage.removeItem(key);
|
|
920
|
+
} catch {
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
class IndexedDBAdapter {
|
|
925
|
+
db = null;
|
|
926
|
+
dbName;
|
|
927
|
+
storeName = "ai_conversations";
|
|
928
|
+
ready;
|
|
929
|
+
constructor(dbName = "yh-ui-ai") {
|
|
930
|
+
this.dbName = dbName;
|
|
931
|
+
this.ready = this.init();
|
|
932
|
+
}
|
|
933
|
+
init() {
|
|
934
|
+
return new Promise((resolve, reject) => {
|
|
935
|
+
if (typeof indexedDB === "undefined") {
|
|
936
|
+
resolve();
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
const req = indexedDB.open(this.dbName, 1);
|
|
940
|
+
req.onupgradeneeded = () => {
|
|
941
|
+
req.result.createObjectStore(this.storeName);
|
|
942
|
+
};
|
|
943
|
+
req.onsuccess = () => {
|
|
944
|
+
this.db = req.result;
|
|
945
|
+
resolve();
|
|
946
|
+
};
|
|
947
|
+
req.onerror = () => reject(req.error);
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
async getItem(key) {
|
|
951
|
+
await this.ready;
|
|
952
|
+
if (!this.db) return null;
|
|
953
|
+
return new Promise((resolve) => {
|
|
954
|
+
const tx = this.db.transaction(this.storeName, "readonly");
|
|
955
|
+
const req = tx.objectStore(this.storeName).get(key);
|
|
956
|
+
req.onsuccess = () => resolve(req.result ?? null);
|
|
957
|
+
req.onerror = () => resolve(null);
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
async setItem(key, value) {
|
|
961
|
+
await this.ready;
|
|
962
|
+
if (!this.db) return;
|
|
963
|
+
return new Promise((resolve) => {
|
|
964
|
+
const tx = this.db.transaction(this.storeName, "readwrite");
|
|
965
|
+
tx.objectStore(this.storeName).put(value, key);
|
|
966
|
+
tx.oncomplete = () => resolve();
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
async removeItem(key) {
|
|
970
|
+
await this.ready;
|
|
971
|
+
if (!this.db) return;
|
|
972
|
+
return new Promise((resolve) => {
|
|
973
|
+
const tx = this.db.transaction(this.storeName, "readwrite");
|
|
974
|
+
tx.objectStore(this.storeName).delete(key);
|
|
975
|
+
tx.oncomplete = () => resolve();
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
function getGroupLabel(updatedAt) {
|
|
980
|
+
const now = Date.now();
|
|
981
|
+
const diff = now - updatedAt;
|
|
982
|
+
const oneDay = 864e5;
|
|
983
|
+
if (diff < oneDay) return "today";
|
|
984
|
+
if (diff < 7 * oneDay) return "last7Days";
|
|
985
|
+
if (diff < 30 * oneDay) return "last30Days";
|
|
986
|
+
return "earlier";
|
|
987
|
+
}
|
|
988
|
+
const GROUP_ORDER = ["today", "last7Days", "last30Days", "earlier"];
|
|
989
|
+
function useAiConversations(options = {}) {
|
|
990
|
+
const {
|
|
991
|
+
idGenerator = () => Math.random().toString(36).substring(2, 9),
|
|
992
|
+
storage = "localStorage",
|
|
993
|
+
storageKey = "yh-ui-ai-conversations",
|
|
994
|
+
pageSize = 20
|
|
995
|
+
} = options;
|
|
996
|
+
let adapter = null;
|
|
997
|
+
if (storage === "localStorage") {
|
|
998
|
+
adapter = localStorageAdapter;
|
|
999
|
+
} else if (storage === "indexedDB") {
|
|
1000
|
+
adapter = new IndexedDBAdapter();
|
|
1001
|
+
} else if (storage && typeof storage === "object") {
|
|
1002
|
+
adapter = storage;
|
|
1003
|
+
}
|
|
1004
|
+
const conversations = ref([]);
|
|
1005
|
+
const page = ref(1);
|
|
1006
|
+
const isLoadingMore = ref(false);
|
|
1007
|
+
const initPromise = (async () => {
|
|
1008
|
+
let stored = [];
|
|
1009
|
+
if (adapter) {
|
|
1010
|
+
try {
|
|
1011
|
+
const raw = await adapter.getItem(storageKey);
|
|
1012
|
+
if (raw) stored = JSON.parse(raw);
|
|
1013
|
+
} catch {
|
|
1014
|
+
stored = [];
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
const init = options.initialConversations ?? [];
|
|
1018
|
+
const merged = [...init];
|
|
1019
|
+
for (const s of stored) {
|
|
1020
|
+
if (!merged.find((c) => c.id === s.id)) {
|
|
1021
|
+
merged.push(s);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
conversations.value = merged.sort((a, b) => {
|
|
1025
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
|
|
1026
|
+
return b.updatedAt - a.updatedAt;
|
|
1027
|
+
});
|
|
1028
|
+
})();
|
|
1029
|
+
const persist = async () => {
|
|
1030
|
+
if (!adapter) return;
|
|
1031
|
+
try {
|
|
1032
|
+
await adapter.setItem(storageKey, JSON.stringify(conversations.value));
|
|
1033
|
+
} catch {
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
const groupedConversations = computed(() => {
|
|
1037
|
+
const groups = {
|
|
1038
|
+
today: [],
|
|
1039
|
+
last7Days: [],
|
|
1040
|
+
last30Days: [],
|
|
1041
|
+
earlier: []
|
|
1042
|
+
};
|
|
1043
|
+
for (const c of conversations.value) {
|
|
1044
|
+
if (c.pinned) continue;
|
|
1045
|
+
const key = getGroupLabel(c.updatedAt);
|
|
1046
|
+
groups[key].push(c);
|
|
1047
|
+
}
|
|
1048
|
+
const result = [];
|
|
1049
|
+
const pinned = conversations.value.filter((c) => c.pinned);
|
|
1050
|
+
if (pinned.length > 0) {
|
|
1051
|
+
result.push({ label: "pinned", items: pinned });
|
|
1052
|
+
}
|
|
1053
|
+
for (const key of GROUP_ORDER) {
|
|
1054
|
+
if (groups[key].length > 0) {
|
|
1055
|
+
result.push({ label: key, items: groups[key] });
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
return result;
|
|
1059
|
+
});
|
|
1060
|
+
const pagedConversations = computed(() => {
|
|
1061
|
+
return conversations.value.slice(0, page.value * pageSize);
|
|
1062
|
+
});
|
|
1063
|
+
const hasMore = computed(() => {
|
|
1064
|
+
return pagedConversations.value.length < conversations.value.length;
|
|
1065
|
+
});
|
|
1066
|
+
const loadMore = async () => {
|
|
1067
|
+
if (!hasMore.value || isLoadingMore.value) return;
|
|
1068
|
+
isLoadingMore.value = true;
|
|
1069
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
1070
|
+
page.value++;
|
|
1071
|
+
isLoadingMore.value = false;
|
|
1072
|
+
};
|
|
1073
|
+
const createConversation = async (title, meta) => {
|
|
1074
|
+
const newConv = {
|
|
1075
|
+
id: idGenerator(),
|
|
1076
|
+
title,
|
|
1077
|
+
updatedAt: Date.now(),
|
|
1078
|
+
meta
|
|
1079
|
+
};
|
|
1080
|
+
conversations.value.unshift(newConv);
|
|
1081
|
+
await persist();
|
|
1082
|
+
return newConv;
|
|
1083
|
+
};
|
|
1084
|
+
const removeConversation = async (id) => {
|
|
1085
|
+
const idx = conversations.value.findIndex((c) => c.id === id);
|
|
1086
|
+
if (idx !== -1) {
|
|
1087
|
+
conversations.value.splice(idx, 1);
|
|
1088
|
+
await persist();
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
const updateConversation = async (id, updates) => {
|
|
1092
|
+
const idx = conversations.value.findIndex((c) => c.id === id);
|
|
1093
|
+
if (idx !== -1) {
|
|
1094
|
+
conversations.value[idx] = {
|
|
1095
|
+
...conversations.value[idx],
|
|
1096
|
+
...updates,
|
|
1097
|
+
updatedAt: Date.now()
|
|
1098
|
+
};
|
|
1099
|
+
await persist();
|
|
1100
|
+
}
|
|
1101
|
+
};
|
|
1102
|
+
const pinConversation = async (id, pinned = true) => {
|
|
1103
|
+
await updateConversation(id, { pinned });
|
|
1104
|
+
conversations.value.sort((a, b) => {
|
|
1105
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
|
|
1106
|
+
return b.updatedAt - a.updatedAt;
|
|
1107
|
+
});
|
|
1108
|
+
await persist();
|
|
1109
|
+
};
|
|
1110
|
+
const clear = async () => {
|
|
1111
|
+
conversations.value = [];
|
|
1112
|
+
if (adapter) {
|
|
1113
|
+
await adapter.removeItem(storageKey);
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
return {
|
|
1117
|
+
/** 完整会话列表 */
|
|
1118
|
+
conversations,
|
|
1119
|
+
/** 按时间分组后的列表(置顶 / 今天 / 最近 7 天 / 更早) */
|
|
1120
|
+
groupedConversations,
|
|
1121
|
+
/** 分页后的列表 */
|
|
1122
|
+
pagedConversations,
|
|
1123
|
+
/** 是否还有更多数据 */
|
|
1124
|
+
hasMore,
|
|
1125
|
+
/** 加载更多 */
|
|
1126
|
+
loadMore,
|
|
1127
|
+
/** 加载更多状态 */
|
|
1128
|
+
isLoadingMore,
|
|
1129
|
+
/** 等待初始化完成(SSR 场景使用) */
|
|
1130
|
+
ready: initPromise,
|
|
1131
|
+
/** 新建会话 */
|
|
1132
|
+
createConversation,
|
|
1133
|
+
/** 删除会话 */
|
|
1134
|
+
removeConversation,
|
|
1135
|
+
/** 更新会话属性 */
|
|
1136
|
+
updateConversation,
|
|
1137
|
+
/** 置顶/取消置顶 */
|
|
1138
|
+
pinConversation,
|
|
1139
|
+
/** 清空全部 */
|
|
1140
|
+
clear
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
export { FormContextKey, FormItemContextKey, IndexedDBAdapter, configProviderContextKey, createZIndexCounter, defaultNamespace, ernieParser, getDayjsLocale, getNextZIndex, idInjectionKey, localStorageAdapter, namespaceContextKey, openaiParser, plainTextParser, qwenParser, resetZIndex, setDayjsLocale, setDayjsLocaleSync, updateDayjsMonths, useAiChat, useAiConversations, useAiStream, useCache, useClickOutside, useConfig, useEventListener, useFormItem, useGlobalNamespace, useId, useIdInjection, useLocale, useNamespace, useScrollLock, useVirtualScroll, useZIndex, zIndexContextKey, zIndexCounterKey };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _useAiChat = require("./use-ai-chat.cjs");
|
|
7
|
+
Object.keys(_useAiChat).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _useAiChat[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _useAiChat[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _useAiStream = require("./use-ai-stream.cjs");
|
|
18
|
+
Object.keys(_useAiStream).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _useAiStream[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _useAiStream[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _useAiConversations = require("./use-ai-conversations.cjs");
|
|
29
|
+
Object.keys(_useAiConversations).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _useAiConversations[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _useAiConversations[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|