@yh-ui/hooks 0.1.56 → 1.0.4

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.
@@ -3,31 +3,84 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
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
- });
6
+ var _exportNames = {
7
+ useAiChat: true,
8
+ aiChatTestUtils: true,
9
+ useAiStream: true,
10
+ openaiParser: true,
11
+ ernieParser: true,
12
+ qwenParser: true,
13
+ claudeParser: true,
14
+ geminiParser: true,
15
+ plainTextParser: true,
16
+ aiStreamTestUtils: true
17
+ };
18
+ Object.defineProperty(exports, "aiChatTestUtils", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _useAiChat.__test__;
22
+ }
16
23
  });
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
- });
24
+ Object.defineProperty(exports, "aiStreamTestUtils", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _useAiStream.__test__;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "claudeParser", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _useAiStream.claudeParser;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "ernieParser", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _useAiStream.ernieParser;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "geminiParser", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _useAiStream.geminiParser;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "openaiParser", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _useAiStream.openaiParser;
52
+ }
27
53
  });
54
+ Object.defineProperty(exports, "plainTextParser", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _useAiStream.plainTextParser;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "qwenParser", {
61
+ enumerable: true,
62
+ get: function () {
63
+ return _useAiStream.qwenParser;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "useAiChat", {
67
+ enumerable: true,
68
+ get: function () {
69
+ return _useAiChat.useAiChat;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "useAiStream", {
73
+ enumerable: true,
74
+ get: function () {
75
+ return _useAiStream.useAiStream;
76
+ }
77
+ });
78
+ var _useAiChat = require("./use-ai-chat.cjs");
79
+ var _useAiStream = require("./use-ai-stream.cjs");
28
80
  var _useAiConversations = require("./use-ai-conversations.cjs");
29
81
  Object.keys(_useAiConversations).forEach(function (key) {
30
82
  if (key === "default" || key === "__esModule") return;
83
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
31
84
  if (key in exports && exports[key] === _useAiConversations[key]) return;
32
85
  Object.defineProperty(exports, key, {
33
86
  enumerable: true,
@@ -39,6 +92,7 @@ Object.keys(_useAiConversations).forEach(function (key) {
39
92
  var _useAiRequest = require("./use-ai-request.cjs");
40
93
  Object.keys(_useAiRequest).forEach(function (key) {
41
94
  if (key === "default" || key === "__esModule") return;
95
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
42
96
  if (key in exports && exports[key] === _useAiRequest[key]) return;
43
97
  Object.defineProperty(exports, key, {
44
98
  enumerable: true,
@@ -50,6 +104,7 @@ Object.keys(_useAiRequest).forEach(function (key) {
50
104
  var _useAiVoice = require("./use-ai-voice.cjs");
51
105
  Object.keys(_useAiVoice).forEach(function (key) {
52
106
  if (key === "default" || key === "__esModule") return;
107
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
53
108
  if (key in exports && exports[key] === _useAiVoice[key]) return;
54
109
  Object.defineProperty(exports, key, {
55
110
  enumerable: true,
@@ -61,6 +116,7 @@ Object.keys(_useAiVoice).forEach(function (key) {
61
116
  var _useAiPersistence = require("./use-ai-persistence.cjs");
62
117
  Object.keys(_useAiPersistence).forEach(function (key) {
63
118
  if (key === "default" || key === "__esModule") return;
119
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
64
120
  if (key in exports && exports[key] === _useAiPersistence[key]) return;
65
121
  Object.defineProperty(exports, key, {
66
122
  enumerable: true,
@@ -1,5 +1,7 @@
1
- export * from './use-ai-chat';
2
- export * from './use-ai-stream';
1
+ export { useAiChat, type AiChatMessage, type UseAiChatOptions } from './use-ai-chat';
2
+ export { __test__ as aiChatTestUtils } from './use-ai-chat';
3
+ export { useAiStream, openaiParser, ernieParser, qwenParser, claudeParser, geminiParser, plainTextParser, type StreamChunkParser, type AiStreamOptions, type UseAiStreamOptions } from './use-ai-stream';
4
+ export { __test__ as aiStreamTestUtils } from './use-ai-stream';
3
5
  export * from './use-ai-conversations';
4
6
  export * from './use-ai-request';
5
7
  export * from './use-ai-voice';
@@ -1,5 +1,15 @@
1
- export * from "./use-ai-chat.mjs";
2
- export * from "./use-ai-stream.mjs";
1
+ export { useAiChat } from "./use-ai-chat.mjs";
2
+ export { __test__ as aiChatTestUtils } from "./use-ai-chat.mjs";
3
+ export {
4
+ useAiStream,
5
+ openaiParser,
6
+ ernieParser,
7
+ qwenParser,
8
+ claudeParser,
9
+ geminiParser,
10
+ plainTextParser
11
+ } from "./use-ai-stream.mjs";
12
+ export { __test__ as aiStreamTestUtils } from "./use-ai-stream.mjs";
3
13
  export * from "./use-ai-conversations.mjs";
4
14
  export * from "./use-ai-request.mjs";
5
15
  export * from "./use-ai-voice.mjs";
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.__test__ = void 0;
6
7
  exports.useAiChat = useAiChat;
7
8
  var _vue = require("vue");
8
9
  var _useAiStream = require("./use-ai-stream.cjs");
@@ -190,4 +191,7 @@ function useAiChat(options = {}) {
190
191
  /** 重置清空所有会话 */
191
192
  clear
192
193
  };
193
- }
194
+ }
195
+ const __test__ = exports.__test__ = {
196
+ createTypewriter
197
+ };
@@ -60,6 +60,11 @@ export interface UseAiChatOptions {
60
60
  /** 消息发送完成回调 */
61
61
  onFinish?: (message: AiChatMessage) => void;
62
62
  }
63
+ declare function createTypewriter(onChar: (char: string) => void, charsPerFrame: number): {
64
+ push: (text: string) => void;
65
+ flush: () => void;
66
+ cancel: () => void;
67
+ };
63
68
  /**
64
69
  * useAiChat - 核心 AI 会话管理 Hook
65
70
  *
@@ -113,3 +118,7 @@ export declare function useAiChat(options?: UseAiChatOptions): {
113
118
  /** 重置清空所有会话 */
114
119
  clear: () => void;
115
120
  };
121
+ export declare const __test__: {
122
+ createTypewriter: typeof createTypewriter;
123
+ };
124
+ export {};
@@ -180,3 +180,6 @@ export function useAiChat(options = {}) {
180
180
  clear
181
181
  };
182
182
  }
183
+ export const __test__ = {
184
+ createTypewriter
185
+ };
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.qwenParser = exports.plainTextParser = exports.openaiParser = exports.geminiParser = exports.ernieParser = exports.claudeParser = void 0;
6
+ exports.qwenParser = exports.plainTextParser = exports.openaiParser = exports.geminiParser = exports.ernieParser = exports.claudeParser = exports.__test__ = void 0;
7
7
  exports.useAiStream = useAiStream;
8
8
  var _vue = require("vue");
9
9
  const openaiParser = raw => {
@@ -168,7 +168,7 @@ function useAiStream(options) {
168
168
  for await (const chunk of response) {
169
169
  if (abortController.signal.aborted) break;
170
170
  const parsed = parser(chunk);
171
- if (parsed) pushText(parsed);
171
+ if (parsed !== null) pushText(parsed);
172
172
  }
173
173
  } else if (response instanceof Response && response.body) {
174
174
  const reader = response.body.getReader();
@@ -187,7 +187,7 @@ function useAiStream(options) {
187
187
  stream: true
188
188
  });
189
189
  const parsed = parser(chunkStr);
190
- if (parsed) pushText(parsed);
190
+ if (parsed !== null) pushText(parsed);
191
191
  }
192
192
  }
193
193
  if (!abortController.signal.aborted) {
@@ -220,4 +220,7 @@ function useAiStream(options) {
220
220
  plainTextParser
221
221
  }
222
222
  };
223
- }
223
+ }
224
+ const __test__ = exports.__test__ = {
225
+ TypewriterThrottle
226
+ };
@@ -28,6 +28,21 @@ export declare const geminiParser: StreamChunkParser;
28
28
  * 纯文本流解析器(AsyncGenerator 输出的原始字符串)
29
29
  */
30
30
  export declare const plainTextParser: StreamChunkParser;
31
+ /**
32
+ * 基于 requestAnimationFrame 的平滑打字机效果
33
+ * 将一次性的大量文字拆分为每帧若干字符的均匀输出
34
+ */
35
+ declare class TypewriterThrottle {
36
+ private queue;
37
+ private rafId;
38
+ private onUpdate;
39
+ private charsPerFrame;
40
+ constructor(onUpdate: (content: string) => void, charsPerFrame?: number);
41
+ push(text: string): void;
42
+ private schedule;
43
+ flush(): void;
44
+ cancel(): void;
45
+ }
31
46
  export interface AiStreamOptions {
32
47
  /**
33
48
  * 请求适配器,返回 AsyncGenerator 或 fetch Response
@@ -52,6 +67,7 @@ export interface AiStreamOptions {
52
67
  onFinish?: (content: string) => void;
53
68
  onError?: (err: Error) => void;
54
69
  }
70
+ export type UseAiStreamOptions = AiStreamOptions;
55
71
  /**
56
72
  * useAiStream - 多厂商兼容流式请求引擎
57
73
  *
@@ -74,3 +90,7 @@ export declare function useAiStream(options: AiStreamOptions): {
74
90
  plainTextParser: StreamChunkParser;
75
91
  };
76
92
  };
93
+ export declare const __test__: {
94
+ TypewriterThrottle: typeof TypewriterThrottle;
95
+ };
96
+ export {};
@@ -160,7 +160,7 @@ export function useAiStream(options) {
160
160
  for await (const chunk of response) {
161
161
  if (abortController.signal.aborted) break;
162
162
  const parsed = parser(chunk);
163
- if (parsed) pushText(parsed);
163
+ if (parsed !== null) pushText(parsed);
164
164
  }
165
165
  } else if (response instanceof Response && response.body) {
166
166
  const reader = response.body.getReader();
@@ -174,7 +174,7 @@ export function useAiStream(options) {
174
174
  if (done) break;
175
175
  const chunkStr = decoder.decode(value, { stream: true });
176
176
  const parsed = parser(chunkStr);
177
- if (parsed) pushText(parsed);
177
+ if (parsed !== null) pushText(parsed);
178
178
  }
179
179
  }
180
180
  if (!abortController.signal.aborted) {
@@ -208,3 +208,6 @@ export function useAiStream(options) {
208
208
  }
209
209
  };
210
210
  }
211
+ export const __test__ = {
212
+ TypewriterThrottle
213
+ };
@@ -5,9 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.updateDayjsMonths = exports.setDayjsLocaleSync = exports.setDayjsLocale = exports.getDayjsLocale = void 0;
7
7
  var _dayjs = _interopRequireDefault(require("../dayjs.cjs"));
8
- require("dayjs/locale/en");
9
8
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const dayjsLocales = import.meta.glob(["../../../../node_modules/dayjs/locale/*.js", "!../../../../node_modules/dayjs/locale/en.js"], {
9
+ const dayjsLocales = import.meta.glob(["../../../../node_modules/dayjs/locale/*.js"], {
11
10
  eager: false
12
11
  });
13
12
  const loadedLocales = /* @__PURE__ */new Set(["en"]);
@@ -1,4 +1,3 @@
1
- import 'dayjs/locale/en';
2
1
  /**
3
2
  * 获取 dayjs locale code
4
3
  */
@@ -1,7 +1,6 @@
1
1
  import dayjs from "../dayjs.mjs";
2
- import "dayjs/locale/en";
3
2
  const dayjsLocales = import.meta.glob(
4
- ["../../../../node_modules/dayjs/locale/*.js", "!../../../../node_modules/dayjs/locale/en.js"],
3
+ ["../../../../node_modules/dayjs/locale/*.js"],
5
4
  { eager: false }
6
5
  );
7
6
  const loadedLocales = /* @__PURE__ */ new Set(["en"]);
@@ -45,17 +45,25 @@ const useLocale = localeOverrides => {
45
45
  }, {
46
46
  immediate: true
47
47
  });
48
- const t = (path, options) => {
48
+ const resolveLocaleValue = path => {
49
49
  const keys = path.split(".");
50
- let result = locale.value.yh;
51
- for (const key of keys) {
52
- if (result && typeof result === "object") {
53
- result = result[key];
54
- } else {
55
- result = void 0;
50
+ const sources = [locale.value.yh, _locale.zhCn.yh, _locale.en.yh];
51
+ for (const source of sources) {
52
+ let result = source;
53
+ for (const key of keys) {
54
+ if (result && typeof result === "object") {
55
+ result = result[key];
56
+ } else {
57
+ result = void 0;
58
+ break;
59
+ }
56
60
  }
57
- if (result === void 0) return path;
61
+ if (result !== void 0) return result;
58
62
  }
63
+ return void 0;
64
+ };
65
+ const t = (path, options) => {
66
+ const result = resolveLocaleValue(path);
59
67
  if (typeof result !== "string") return path;
60
68
  if (options) {
61
69
  return result.replace(/\{(\w+)\}/g, (_match, key) => {
@@ -66,16 +74,8 @@ const useLocale = localeOverrides => {
66
74
  return result;
67
75
  };
68
76
  const tRaw = path => {
69
- const keys = path.split(".");
70
- let result = locale.value.yh;
71
- for (const key of keys) {
72
- if (result && typeof result === "object") {
73
- result = result[key];
74
- } else {
75
- result = void 0;
76
- }
77
- if (result === void 0) return path;
78
- }
77
+ const result = resolveLocaleValue(path);
78
+ if (result === void 0) return path;
79
79
  return result;
80
80
  };
81
81
  return {
@@ -2,11 +2,6 @@ import type { Ref } from 'vue';
2
2
  import type { Language } from '@yh-ui/locale';
3
3
  export type { Language };
4
4
  export { setDayjsLocale, getDayjsLocale, setDayjsLocaleSync, updateDayjsMonths } from './dayjs-locale';
5
- /**
6
- * useLocale - 国际化
7
- * @param localeOverrides - 可选的自定义语言包
8
- * @returns 国际化相关方法
9
- */
10
5
  export declare const useLocale: (localeOverrides?: Ref<Language>) => {
11
6
  locale: import("vue").ComputedRef<Language>;
12
7
  lang: import("vue").ComputedRef<string>;
@@ -1,5 +1,5 @@
1
1
  import { computed, unref, watch } from "vue";
2
- import { zhCn } from "@yh-ui/locale";
2
+ import { en, zhCn } from "@yh-ui/locale";
3
3
  import { useConfig } from "../use-config/index.mjs";
4
4
  import { setDayjsLocale } from "./dayjs-locale.mjs";
5
5
  export {
@@ -21,17 +21,25 @@ export const useLocale = (localeOverrides) => {
21
21
  },
22
22
  { immediate: true }
23
23
  );
24
- const t = (path, options) => {
24
+ const resolveLocaleValue = (path) => {
25
25
  const keys = path.split(".");
26
- let result = locale.value.yh;
27
- for (const key of keys) {
28
- if (result && typeof result === "object") {
29
- result = result[key];
30
- } else {
31
- result = void 0;
26
+ const sources = [locale.value.yh, zhCn.yh, en.yh];
27
+ for (const source of sources) {
28
+ let result = source;
29
+ for (const key of keys) {
30
+ if (result && typeof result === "object") {
31
+ result = result[key];
32
+ } else {
33
+ result = void 0;
34
+ break;
35
+ }
32
36
  }
33
- if (result === void 0) return path;
37
+ if (result !== void 0) return result;
34
38
  }
39
+ return void 0;
40
+ };
41
+ const t = (path, options) => {
42
+ const result = resolveLocaleValue(path);
35
43
  if (typeof result !== "string") return path;
36
44
  if (options) {
37
45
  return result.replace(/\{(\w+)\}/g, (_match, key) => {
@@ -42,16 +50,8 @@ export const useLocale = (localeOverrides) => {
42
50
  return result;
43
51
  };
44
52
  const tRaw = (path) => {
45
- const keys = path.split(".");
46
- let result = locale.value.yh;
47
- for (const key of keys) {
48
- if (result && typeof result === "object") {
49
- result = result[key];
50
- } else {
51
- result = void 0;
52
- }
53
- if (result === void 0) return path;
54
- }
53
+ const result = resolveLocaleValue(path);
54
+ if (result === void 0) return path;
55
55
  return result;
56
56
  };
57
57
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yh-ui/hooks",
3
- "version": "0.1.56",
3
+ "version": "1.0.4",
4
4
  "description": "YH-UI composition hooks",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@yh-ui/locale": "^0.1.56",
27
- "@yh-ui/utils": "^0.1.56",
26
+ "@yh-ui/locale": "^1.0.4",
27
+ "@yh-ui/utils": "^1.0.4",
28
28
  "dayjs": "^1.11.19"
29
29
  },
30
30
  "devDependencies": {