@chenzy-design/core 0.2.1 → 0.4.0

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.
Files changed (283) hide show
  1. package/dist/ai-chat-dialogue.d.ts +300 -0
  2. package/dist/ai-chat-dialogue.d.ts.map +1 -0
  3. package/dist/ai-chat-dialogue.js +699 -0
  4. package/dist/ai-chat-dialogue.js.map +1 -0
  5. package/dist/ai-chat-dialogue.test.d.ts +2 -0
  6. package/dist/ai-chat-dialogue.test.d.ts.map +1 -0
  7. package/dist/ai-chat-dialogue.test.js +338 -0
  8. package/dist/ai-chat-dialogue.test.js.map +1 -0
  9. package/dist/ai-chat-input.d.ts +191 -0
  10. package/dist/ai-chat-input.d.ts.map +1 -0
  11. package/dist/ai-chat-input.js +274 -0
  12. package/dist/ai-chat-input.js.map +1 -0
  13. package/dist/ai-chat-input.test.d.ts +2 -0
  14. package/dist/ai-chat-input.test.d.ts.map +1 -0
  15. package/dist/ai-chat-input.test.js +339 -0
  16. package/dist/ai-chat-input.test.js.map +1 -0
  17. package/dist/audio-player.d.ts +154 -0
  18. package/dist/audio-player.d.ts.map +1 -0
  19. package/dist/audio-player.js +254 -0
  20. package/dist/audio-player.js.map +1 -0
  21. package/dist/audio-player.test.d.ts +2 -0
  22. package/dist/audio-player.test.d.ts.map +1 -0
  23. package/dist/audio-player.test.js +275 -0
  24. package/dist/audio-player.test.js.map +1 -0
  25. package/dist/back-top.d.ts +4 -1
  26. package/dist/back-top.d.ts.map +1 -1
  27. package/dist/back-top.js +5 -2
  28. package/dist/back-top.js.map +1 -1
  29. package/dist/back-top.test.js +3 -2
  30. package/dist/back-top.test.js.map +1 -1
  31. package/dist/calendar.d.ts +132 -44
  32. package/dist/calendar.d.ts.map +1 -1
  33. package/dist/calendar.js +413 -70
  34. package/dist/calendar.js.map +1 -1
  35. package/dist/calendar.test.js +160 -76
  36. package/dist/calendar.test.js.map +1 -1
  37. package/dist/chat.d.ts +172 -0
  38. package/dist/chat.d.ts.map +1 -0
  39. package/dist/chat.js +210 -0
  40. package/dist/chat.js.map +1 -0
  41. package/dist/chat.test.d.ts +2 -0
  42. package/dist/chat.test.d.ts.map +1 -0
  43. package/dist/chat.test.js +181 -0
  44. package/dist/chat.test.js.map +1 -0
  45. package/dist/code-highlight.d.ts +30 -0
  46. package/dist/code-highlight.d.ts.map +1 -0
  47. package/dist/code-highlight.js +37 -0
  48. package/dist/code-highlight.js.map +1 -0
  49. package/dist/code-highlight.test.d.ts +2 -0
  50. package/dist/code-highlight.test.d.ts.map +1 -0
  51. package/dist/code-highlight.test.js +36 -0
  52. package/dist/code-highlight.test.js.map +1 -0
  53. package/dist/collapsible.d.ts +41 -0
  54. package/dist/collapsible.d.ts.map +1 -0
  55. package/dist/collapsible.js +33 -0
  56. package/dist/collapsible.js.map +1 -0
  57. package/dist/collapsible.test.d.ts +2 -0
  58. package/dist/collapsible.test.d.ts.map +1 -0
  59. package/dist/collapsible.test.js +57 -0
  60. package/dist/collapsible.test.js.map +1 -0
  61. package/dist/color-value.d.ts +60 -0
  62. package/dist/color-value.d.ts.map +1 -0
  63. package/dist/color-value.js +165 -0
  64. package/dist/color-value.js.map +1 -0
  65. package/dist/color-value.test.d.ts +2 -0
  66. package/dist/color-value.test.d.ts.map +1 -0
  67. package/dist/color-value.test.js +104 -0
  68. package/dist/color-value.test.js.map +1 -0
  69. package/dist/config-provider.d.ts +76 -37
  70. package/dist/config-provider.d.ts.map +1 -1
  71. package/dist/config-provider.js +107 -32
  72. package/dist/config-provider.js.map +1 -1
  73. package/dist/config-provider.test.js +123 -60
  74. package/dist/config-provider.test.js.map +1 -1
  75. package/dist/cropper.d.ts +164 -0
  76. package/dist/cropper.d.ts.map +1 -0
  77. package/dist/cropper.js +841 -0
  78. package/dist/cropper.js.map +1 -0
  79. package/dist/cropper.test.d.ts +2 -0
  80. package/dist/cropper.test.d.ts.map +1 -0
  81. package/dist/cropper.test.js +259 -0
  82. package/dist/cropper.test.js.map +1 -0
  83. package/dist/drag-move.d.ts +119 -0
  84. package/dist/drag-move.d.ts.map +1 -0
  85. package/dist/drag-move.js +237 -0
  86. package/dist/drag-move.js.map +1 -0
  87. package/dist/drag-move.test.d.ts +2 -0
  88. package/dist/drag-move.test.d.ts.map +1 -0
  89. package/dist/drag-move.test.js +304 -0
  90. package/dist/drag-move.test.js.map +1 -0
  91. package/dist/editable.d.ts +24 -4
  92. package/dist/editable.d.ts.map +1 -1
  93. package/dist/editable.js +18 -0
  94. package/dist/editable.js.map +1 -1
  95. package/dist/editable.test.js +55 -2
  96. package/dist/editable.test.js.map +1 -1
  97. package/dist/ellipsis.d.ts +13 -0
  98. package/dist/ellipsis.d.ts.map +1 -1
  99. package/dist/ellipsis.js +36 -0
  100. package/dist/ellipsis.js.map +1 -1
  101. package/dist/ellipsis.test.js +37 -1
  102. package/dist/ellipsis.test.js.map +1 -1
  103. package/dist/form-path.d.ts +19 -0
  104. package/dist/form-path.d.ts.map +1 -0
  105. package/dist/form-path.js +97 -0
  106. package/dist/form-path.js.map +1 -0
  107. package/dist/form-path.test.d.ts +2 -0
  108. package/dist/form-path.test.d.ts.map +1 -0
  109. package/dist/form-path.test.js +57 -0
  110. package/dist/form-path.test.js.map +1 -0
  111. package/dist/form-validate.d.ts +13 -0
  112. package/dist/form-validate.d.ts.map +1 -0
  113. package/dist/form-validate.js +204 -0
  114. package/dist/form-validate.js.map +1 -0
  115. package/dist/form.d.ts +91 -18
  116. package/dist/form.d.ts.map +1 -1
  117. package/dist/form.js +170 -115
  118. package/dist/form.js.map +1 -1
  119. package/dist/form.test.js +349 -76
  120. package/dist/form.test.js.map +1 -1
  121. package/dist/highlight.d.ts +42 -32
  122. package/dist/highlight.d.ts.map +1 -1
  123. package/dist/highlight.js +67 -41
  124. package/dist/highlight.js.map +1 -1
  125. package/dist/highlight.test.js +73 -32
  126. package/dist/highlight.test.js.map +1 -1
  127. package/dist/hotkeys.d.ts +151 -0
  128. package/dist/hotkeys.d.ts.map +1 -0
  129. package/dist/hotkeys.js +188 -0
  130. package/dist/hotkeys.js.map +1 -0
  131. package/dist/hotkeys.test.d.ts +2 -0
  132. package/dist/hotkeys.test.d.ts.map +1 -0
  133. package/dist/hotkeys.test.js +170 -0
  134. package/dist/hotkeys.test.js.map +1 -0
  135. package/dist/index.d.ts +33 -17
  136. package/dist/index.d.ts.map +1 -1
  137. package/dist/index.js +32 -13
  138. package/dist/index.js.map +1 -1
  139. package/dist/input.d.ts +34 -0
  140. package/dist/input.d.ts.map +1 -0
  141. package/dist/input.js +54 -0
  142. package/dist/input.js.map +1 -0
  143. package/dist/input.test.d.ts +2 -0
  144. package/dist/input.test.d.ts.map +1 -0
  145. package/dist/input.test.js +56 -0
  146. package/dist/input.test.js.map +1 -0
  147. package/dist/markdown-render.d.ts +49 -0
  148. package/dist/markdown-render.d.ts.map +1 -0
  149. package/dist/markdown-render.js +61 -0
  150. package/dist/markdown-render.js.map +1 -0
  151. package/dist/markdown-render.test.d.ts +2 -0
  152. package/dist/markdown-render.test.d.ts.map +1 -0
  153. package/dist/markdown-render.test.js +106 -0
  154. package/dist/markdown-render.test.js.map +1 -0
  155. package/dist/notification.d.ts +40 -31
  156. package/dist/notification.d.ts.map +1 -1
  157. package/dist/notification.js +86 -70
  158. package/dist/notification.js.map +1 -1
  159. package/dist/notification.test.js +62 -39
  160. package/dist/notification.test.js.map +1 -1
  161. package/dist/numeral.d.ts +32 -0
  162. package/dist/numeral.d.ts.map +1 -0
  163. package/dist/numeral.js +67 -0
  164. package/dist/numeral.js.map +1 -0
  165. package/dist/numeral.test.d.ts +2 -0
  166. package/dist/numeral.test.d.ts.map +1 -0
  167. package/dist/numeral.test.js +59 -0
  168. package/dist/numeral.test.js.map +1 -0
  169. package/dist/overflow-list.d.ts +9 -40
  170. package/dist/overflow-list.d.ts.map +1 -1
  171. package/dist/overflow-list.js +9 -70
  172. package/dist/overflow-list.js.map +1 -1
  173. package/dist/overflow-list.test.js +1 -122
  174. package/dist/overflow-list.test.js.map +1 -1
  175. package/dist/pagination.d.ts +17 -0
  176. package/dist/pagination.d.ts.map +1 -1
  177. package/dist/pagination.js +48 -0
  178. package/dist/pagination.js.map +1 -1
  179. package/dist/pagination.test.js +40 -1
  180. package/dist/pagination.test.js.map +1 -1
  181. package/dist/pincode.d.ts +79 -0
  182. package/dist/pincode.d.ts.map +1 -0
  183. package/dist/pincode.js +119 -0
  184. package/dist/pincode.js.map +1 -0
  185. package/dist/pincode.test.d.ts +2 -0
  186. package/dist/pincode.test.d.ts.map +1 -0
  187. package/dist/pincode.test.js +151 -0
  188. package/dist/pincode.test.js.map +1 -0
  189. package/dist/progress.d.ts +32 -30
  190. package/dist/progress.d.ts.map +1 -1
  191. package/dist/progress.js +257 -64
  192. package/dist/progress.js.map +1 -1
  193. package/dist/progress.test.js +50 -47
  194. package/dist/progress.test.js.map +1 -1
  195. package/dist/resizable.d.ts +200 -0
  196. package/dist/resizable.d.ts.map +1 -0
  197. package/dist/resizable.js +329 -0
  198. package/dist/resizable.js.map +1 -0
  199. package/dist/resizable.test.d.ts +2 -0
  200. package/dist/resizable.test.d.ts.map +1 -0
  201. package/dist/resizable.test.js +302 -0
  202. package/dist/resizable.test.js.map +1 -0
  203. package/dist/scroll-list.d.ts +69 -62
  204. package/dist/scroll-list.d.ts.map +1 -1
  205. package/dist/scroll-list.js +103 -144
  206. package/dist/scroll-list.js.map +1 -1
  207. package/dist/scroll-list.test.js +100 -110
  208. package/dist/scroll-list.test.js.map +1 -1
  209. package/dist/sidebar.d.ts +46 -0
  210. package/dist/sidebar.d.ts.map +1 -0
  211. package/dist/sidebar.js +65 -0
  212. package/dist/sidebar.js.map +1 -0
  213. package/dist/sidebar.test.d.ts +2 -0
  214. package/dist/sidebar.test.d.ts.map +1 -0
  215. package/dist/sidebar.test.js +88 -0
  216. package/dist/sidebar.test.js.map +1 -0
  217. package/dist/spin.d.ts +5 -5
  218. package/dist/spin.d.ts.map +1 -1
  219. package/dist/spin.js +13 -39
  220. package/dist/spin.js.map +1 -1
  221. package/dist/spin.test.js +2 -26
  222. package/dist/spin.test.js.map +1 -1
  223. package/dist/toast.d.ts +33 -30
  224. package/dist/toast.d.ts.map +1 -1
  225. package/dist/toast.js +77 -72
  226. package/dist/toast.js.map +1 -1
  227. package/dist/toast.test.js +63 -37
  228. package/dist/toast.test.js.map +1 -1
  229. package/dist/tree.d.ts +22 -5
  230. package/dist/tree.d.ts.map +1 -1
  231. package/dist/tree.js +61 -9
  232. package/dist/tree.js.map +1 -1
  233. package/dist/tree.test.js +72 -1
  234. package/dist/tree.test.js.map +1 -1
  235. package/dist/user-guide.d.ts +73 -0
  236. package/dist/user-guide.d.ts.map +1 -0
  237. package/dist/user-guide.js +95 -0
  238. package/dist/user-guide.js.map +1 -0
  239. package/dist/user-guide.test.d.ts +2 -0
  240. package/dist/user-guide.test.d.ts.map +1 -0
  241. package/dist/user-guide.test.js +109 -0
  242. package/dist/user-guide.test.js.map +1 -0
  243. package/dist/video-player.d.ts +196 -0
  244. package/dist/video-player.d.ts.map +1 -0
  245. package/dist/video-player.js +461 -0
  246. package/dist/video-player.js.map +1 -0
  247. package/dist/video-player.test.d.ts +2 -0
  248. package/dist/video-player.test.d.ts.map +1 -0
  249. package/dist/video-player.test.js +435 -0
  250. package/dist/video-player.test.js.map +1 -0
  251. package/package.json +17 -1
  252. package/dist/banner.d.ts +0 -19
  253. package/dist/banner.d.ts.map +0 -1
  254. package/dist/banner.js +0 -20
  255. package/dist/banner.js.map +0 -1
  256. package/dist/banner.test.d.ts +0 -2
  257. package/dist/banner.test.d.ts.map +0 -1
  258. package/dist/banner.test.js +0 -29
  259. package/dist/banner.test.js.map +0 -1
  260. package/dist/breakpoints.d.ts +0 -30
  261. package/dist/breakpoints.d.ts.map +0 -1
  262. package/dist/breakpoints.js +0 -45
  263. package/dist/breakpoints.js.map +0 -1
  264. package/dist/breakpoints.test.d.ts +0 -2
  265. package/dist/breakpoints.test.d.ts.map +0 -1
  266. package/dist/breakpoints.test.js +0 -46
  267. package/dist/breakpoints.test.js.map +0 -1
  268. package/dist/lottie-icon.d.ts +0 -88
  269. package/dist/lottie-icon.d.ts.map +0 -1
  270. package/dist/lottie-icon.js +0 -84
  271. package/dist/lottie-icon.js.map +0 -1
  272. package/dist/lottie-icon.test.d.ts +0 -2
  273. package/dist/lottie-icon.test.d.ts.map +0 -1
  274. package/dist/lottie-icon.test.js +0 -88
  275. package/dist/lottie-icon.test.js.map +0 -1
  276. package/dist/sider.d.ts +0 -26
  277. package/dist/sider.d.ts.map +0 -1
  278. package/dist/sider.js +0 -55
  279. package/dist/sider.js.map +0 -1
  280. package/dist/sider.test.d.ts +0 -2
  281. package/dist/sider.test.d.ts.map +0 -1
  282. package/dist/sider.test.js +0 -29
  283. package/dist/sider.test.js.map +0 -1
@@ -0,0 +1,274 @@
1
+ /**
2
+ * 判定一次 Enter 键是否应触发发送(而非换行)。
3
+ * - sendHotKey='enter':裸 Enter 发送,Shift+Enter 换行。
4
+ * - sendHotKey='shift+enter':Shift+Enter 发送,裸 Enter 换行。
5
+ * IME 组字中(composing)永不发送,交由渲染层前置拦截。
6
+ */
7
+ export function isSendHotKey(key, shiftKey, sendHotKey) {
8
+ if (key !== 'Enter')
9
+ return false;
10
+ return sendHotKey === 'enter' ? !shiftKey : shiftKey;
11
+ }
12
+ /**
13
+ * 解析当前是否可发送。
14
+ * - 显式传入 canSend(受控)时直接返回它。
15
+ * - 否则:富文本非空 或 有附件 即可发送。
16
+ */
17
+ export function resolveCanSend(params) {
18
+ const { canSend, isEmpty, attachments } = params;
19
+ if (canSend !== undefined)
20
+ return canSend;
21
+ const validRichText = !isEmpty;
22
+ const validAttachment = Array.isArray(attachments) && attachments.length > 0;
23
+ return validRichText || validAttachment;
24
+ }
25
+ /** 组装 onMessageSend 载荷。空字段省略,保持载荷精简。 */
26
+ export function buildMessageContent(params) {
27
+ const { inputContents, attachments, references, setup } = params;
28
+ const msg = {};
29
+ if (inputContents && inputContents.length > 0)
30
+ msg.inputContents = inputContents;
31
+ if (attachments && attachments.length > 0)
32
+ msg.attachments = attachments;
33
+ if (references && references.length > 0)
34
+ msg.references = references;
35
+ if (setup && Object.keys(setup).length > 0)
36
+ msg.setup = setup;
37
+ return msg;
38
+ }
39
+ /**
40
+ * 把 tiptap 文档 JSON 归一为 AIChatInputContent[](阶段 1)。
41
+ * 阶段 1 只有段落文本:每个顶层块产出一段 `{ type:'text', text }`,空段丢弃。
42
+ * transformer(Map<nodeType, fn>)可覆盖特定节点的转换(对齐 Semi transformer)。
43
+ * 阶段 2+ 的 input-slot/select-slot 节点在此扩展。
44
+ */
45
+ export function transformDocToContents(json, transformer) {
46
+ const doc = json;
47
+ if (!doc || !Array.isArray(doc.content))
48
+ return [];
49
+ const out = [];
50
+ for (const block of doc.content) {
51
+ const t = block?.type ?? '';
52
+ const override = transformer?.get(t);
53
+ if (override) {
54
+ out.push(override(block));
55
+ continue;
56
+ }
57
+ const text = extractText(block);
58
+ if (text.length > 0)
59
+ out.push({ type: 'text', text });
60
+ }
61
+ return out;
62
+ }
63
+ /** 零宽字符(对齐 Semi ZERO_WIDTH_CHAR):inputSlot 空态占位锚点,归一时剔除。 */
64
+ export const AI_CHAT_INPUT_ZERO_WIDTH = '';
65
+ /**
66
+ * 递归抽取一个块内的纯文本(含 hardBreak → 换行)。内联自定义 slot 节点归一:
67
+ * - selectSlot:取 attrs.value(用户选中的选项值)。
68
+ * - skillSlot:取 attrs.label ?? attrs.value(技能显示文本)。
69
+ * - inputSlot:取可编辑内容文本(剔零宽字符);为空回退 attrs.placeholder。
70
+ * 对齐 Semi transformSelectSlot / transformSkillSlot / transformInputSlot,使 slot 内容开箱进 content。
71
+ */
72
+ function extractText(node) {
73
+ const n = node;
74
+ if (!n)
75
+ return '';
76
+ if (n.type === 'text') {
77
+ const t = n.text ?? '';
78
+ return t === AI_CHAT_INPUT_ZERO_WIDTH ? '' : t;
79
+ }
80
+ if (n.type === 'hardBreak')
81
+ return '\n';
82
+ if (n.type === 'selectSlot') {
83
+ const v = n.attrs?.value;
84
+ return typeof v === 'string' ? v : '';
85
+ }
86
+ if (n.type === 'skillSlot') {
87
+ const label = n.attrs?.label ?? n.attrs?.value;
88
+ return typeof label === 'string' ? label : '';
89
+ }
90
+ if (n.type === 'inputSlot') {
91
+ const inner = Array.isArray(n.content) ? n.content.map(extractText).join('') : '';
92
+ if (inner.length > 0)
93
+ return inner;
94
+ const ph = n.attrs?.placeholder;
95
+ return typeof ph === 'string' ? ph : '';
96
+ }
97
+ if (Array.isArray(n.content))
98
+ return n.content.map(extractText).join('');
99
+ return '';
100
+ }
101
+ /** 取建议项的显示文本(string 直接返回,对象取 content)。 */
102
+ export function suggestionContent(suggestion) {
103
+ return typeof suggestion === 'string' ? suggestion : (suggestion?.content ?? '');
104
+ }
105
+ /**
106
+ * 建议面板键盘导航:从 current 沿 dir(-1=上 / +1=下)环绕移动,返回新 activeIndex。
107
+ * - len<=0 返回 -1(无项)。
108
+ * - current<0(未选中)时:向下从 0 开始、向上从末项开始。
109
+ */
110
+ export function nextSuggestionIndex(current, len, dir) {
111
+ if (len <= 0)
112
+ return -1;
113
+ if (current < 0)
114
+ return dir === 1 ? 0 : len - 1;
115
+ return (current + dir + len) % len;
116
+ }
117
+ /**
118
+ * 取引用项的显示文本:type='text' 用 content,否则用 name(缺省回退到 id)。
119
+ */
120
+ export function referenceLabel(ref) {
121
+ if (ref.type === 'text')
122
+ return ref.content ?? '';
123
+ return ref.name ?? ref.id;
124
+ }
125
+ /** 该引用是否应按图片渲染(type='image' 或 url 以常见图片扩展名结尾)。 */
126
+ export function isImageReference(ref) {
127
+ if (ref.type === 'image')
128
+ return true;
129
+ const url = ref.url ?? '';
130
+ return /\.(png|jpe?g|gif|bmp|webp|svg)(\?|#|$)/i.test(url);
131
+ }
132
+ /** 取技能项的显示文本(label 优先,回退 value,再回退空串)。 */
133
+ export function skillLabel(skill) {
134
+ return skill.label ?? skill.value ?? '';
135
+ }
136
+ /**
137
+ * 生成 skillSlot 节点的 HTML(供 editor.setContent 插入)。对齐 Semi getSkillSlotString:
138
+ * `<skill-slot data-label data-value data-template>`。属性值做 HTML 转义防注入。
139
+ */
140
+ export function getSkillSlotHTML(skill) {
141
+ const attrs = [];
142
+ if (skill.label)
143
+ attrs.push(`data-label="${escapeAttr(skill.label)}"`);
144
+ if (skill.value)
145
+ attrs.push(`data-value="${escapeAttr(skill.value)}"`);
146
+ if (typeof skill.hasTemplate === 'boolean')
147
+ attrs.push(`data-template="${skill.hasTemplate}"`);
148
+ return `<skill-slot ${attrs.join(' ')}></skill-slot>`;
149
+ }
150
+ /**
151
+ * 生成 selectSlot 节点的 HTML(供 editor.setContent 插入,通常用于 renderTemplate 模版填空)。
152
+ * 对齐 Semi selectSlot:`<select-slot options='["a","b"]' value="a">`。options 为 JSON 字符串。
153
+ * @param options 可选项(string[])
154
+ * @param value 默认选中值(缺省空)
155
+ */
156
+ export function getSelectSlotHTML(options, value = '') {
157
+ const optionsJson = escapeAttr(JSON.stringify(options));
158
+ const attrs = [`options="${optionsJson}"`];
159
+ if (value)
160
+ attrs.push(`value="${escapeAttr(value)}"`);
161
+ return `<select-slot ${attrs.join(' ')}></select-slot>`;
162
+ }
163
+ /**
164
+ * 生成 inputSlot 节点的 HTML(供 editor.setContent 插入,用于 renderTemplate 模版填空的可编辑空格)。
165
+ * 对齐 Semi inputSlot:`<input-slot placeholder="...">` 内含零宽字符作为空态光标锚点。
166
+ * @param placeholder 空态占位提示
167
+ * @param value 初始内容(缺省仅零宽锚点)
168
+ */
169
+ export function getInputSlotHTML(placeholder = '', value = '') {
170
+ const ph = placeholder ? ` placeholder="${escapeAttr(placeholder)}"` : '';
171
+ const inner = value ? escapeHTML(value) : AI_CHAT_INPUT_ZERO_WIDTH;
172
+ return `<input-slot${ph}>${inner}</input-slot>`;
173
+ }
174
+ /** HTML 文本内容转义(元素内容上下文)。 */
175
+ function escapeHTML(value) {
176
+ return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
177
+ }
178
+ /** HTML 属性值转义(双引号上下文)。 */
179
+ function escapeAttr(value) {
180
+ return value
181
+ .replace(/&/g, '&amp;')
182
+ .replace(/"/g, '&quot;')
183
+ .replace(/</g, '&lt;')
184
+ .replace(/>/g, '&gt;');
185
+ }
186
+ /**
187
+ * 是否应触发技能面板:编辑区为空、按下的键等于 skillHotKey、且有技能项。
188
+ * 对齐 Semi:`oldValue === '' && e.key === skillHotKey && skills.length`。
189
+ */
190
+ export function shouldOpenSkillPanel(params) {
191
+ const { key, skillHotKey, isEmpty, skillCount } = params;
192
+ return isEmpty && key === skillHotKey && skillCount > 0;
193
+ }
194
+ /**
195
+ * 配置区状态计算(纯函数,框架无关)。渲染层持有 value($state),
196
+ * 用这些函数算下一个 value —— 对齐 Semi Configure 的 onChange(obj)/onRemove(field)。
197
+ */
198
+ /** 合并一个字段补丁到 value,返回新对象(不可变)。 */
199
+ export function setConfigureField(value, patch) {
200
+ return { ...value, ...patch };
201
+ }
202
+ /** 从 value 移除一个字段,返回新对象(不可变;对齐 Semi onRemove)。 */
203
+ export function removeConfigureField(value, field) {
204
+ const next = {};
205
+ for (const key of Object.keys(value)) {
206
+ if (key !== field)
207
+ next[key] = value[key];
208
+ }
209
+ return next;
210
+ }
211
+ // ————————————————————————————————————————————————————————————————
212
+ // 阶段 5 · Adapter 桥(AIChatInput → AIChatDialogue / OpenAI API)
213
+ // ————————————————————————————————————————————————————————————————
214
+ /**
215
+ * 附件是否应按图片处理。Attachment.type 是 'file'|'directory'(非 mime),故按
216
+ * name/url 的图片扩展名判定。
217
+ */
218
+ function isImageAttachment(att) {
219
+ const s = `${att.url ?? ''} ${att.name ?? ''}`;
220
+ return /\.(png|jpe?g|gif|bmp|webp|svg)(\?|#|\s|$)/i.test(s);
221
+ }
222
+ /**
223
+ * messageToChatInput —— 把 AIChatInput 的 onMessageSend 载荷转成一条 AIDialogueMessage(user 角色),
224
+ * 供直接 push 进 AIChatDialogue 的 chats 展示。对齐 OpenAI Response 输入消息形态:
225
+ * content 为单个 InputMessage 块,其 content 数组含 input_text / input_image / input_file。
226
+ *
227
+ * @param message AIChatInput onMessageSend 载荷
228
+ * @param opts.id 消息 id(AIChatDialogue 需唯一 id;调用方应提供,缺省 '')
229
+ * @param opts.model 可选模型标记
230
+ */
231
+ export function messageToChatInput(message, opts = {}) {
232
+ const parts = [];
233
+ for (const c of message.inputContents ?? []) {
234
+ const text = typeof c.text === 'string' ? c.text : '';
235
+ if (text.length > 0)
236
+ parts.push({ type: 'input_text', text });
237
+ }
238
+ for (const att of message.attachments ?? []) {
239
+ if (isImageAttachment(att)) {
240
+ parts.push({ type: 'input_image', image_url: att.url, file_id: att.uid });
241
+ }
242
+ else {
243
+ parts.push({ type: 'input_file', filename: att.name, file_url: att.url, file_id: att.uid });
244
+ }
245
+ }
246
+ const inputMessage = { type: 'message', role: 'user', content: parts };
247
+ const msg = { id: opts.id ?? '', role: 'user', content: [inputMessage] };
248
+ if (opts.model !== undefined)
249
+ msg.model = opts.model;
250
+ return msg;
251
+ }
252
+ /**
253
+ * chatInputToChatCompletion —— 把 onMessageSend 载荷转成 OpenAI ChatCompletion 请求的 user message,
254
+ * content 为多模态 parts(text / image_url / file)。供直接放进 messages 数组喂 API。
255
+ * 纯文本可由调用方按需扁平化;这里统一产出 parts 数组,保真多模态。
256
+ */
257
+ export function chatInputToChatCompletion(message) {
258
+ const content = [];
259
+ for (const c of message.inputContents ?? []) {
260
+ const text = typeof c.text === 'string' ? c.text : '';
261
+ if (text.length > 0)
262
+ content.push({ type: 'text', text });
263
+ }
264
+ for (const att of message.attachments ?? []) {
265
+ if (isImageAttachment(att)) {
266
+ content.push({ type: 'image_url', image_url: { url: att.url } });
267
+ }
268
+ else {
269
+ content.push({ type: 'file', file: { filename: att.name, file_data: att.url } });
270
+ }
271
+ }
272
+ return { role: 'user', content };
273
+ }
274
+ //# sourceMappingURL=ai-chat-input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-chat-input.js","sourceRoot":"","s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,OAAO,EAAE,CAAC;AACnC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ai-chat-input.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-chat-input.test.d.ts","sourceRoot":"","sources":["../src/ai-chat-input.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,339 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { isSendHotKey, resolveCanSend, buildMessageContent, transformDocToContents, suggestionContent, nextSuggestionIndex, referenceLabel, isImageReference, skillLabel, getSkillSlotHTML, getSelectSlotHTML, getInputSlotHTML, AI_CHAT_INPUT_ZERO_WIDTH as ZW, shouldOpenSkillPanel, setConfigureField, removeConfigureField, messageToChatInput, chatInputToChatCompletion, } from './ai-chat-input.js';
3
+ describe('ai-chat-input · isSendHotKey', () => {
4
+ it("sendHotKey='enter':裸 Enter 发送、Shift+Enter 换行", () => {
5
+ expect(isSendHotKey('Enter', false, 'enter')).toBe(true);
6
+ expect(isSendHotKey('Enter', true, 'enter')).toBe(false);
7
+ });
8
+ it("sendHotKey='shift+enter':Shift+Enter 发送、裸 Enter 换行", () => {
9
+ expect(isSendHotKey('Enter', true, 'shift+enter')).toBe(true);
10
+ expect(isSendHotKey('Enter', false, 'shift+enter')).toBe(false);
11
+ });
12
+ it('非 Enter 键永不发送', () => {
13
+ expect(isSendHotKey('a', false, 'enter')).toBe(false);
14
+ expect(isSendHotKey('ArrowDown', true, 'shift+enter')).toBe(false);
15
+ });
16
+ });
17
+ describe('ai-chat-input · resolveCanSend', () => {
18
+ it('显式 canSend 优先(受控)', () => {
19
+ expect(resolveCanSend({ canSend: true, isEmpty: true })).toBe(true);
20
+ expect(resolveCanSend({ canSend: false, isEmpty: false, attachments: [{ uid: '1' }] })).toBe(false);
21
+ });
22
+ it('非受控:富文本非空即可发送', () => {
23
+ expect(resolveCanSend({ isEmpty: false })).toBe(true);
24
+ });
25
+ it('非受控:空文本但有附件即可发送', () => {
26
+ expect(resolveCanSend({ isEmpty: true, attachments: [{ uid: '1' }] })).toBe(true);
27
+ });
28
+ it('非受控:空文本且无附件不可发送', () => {
29
+ expect(resolveCanSend({ isEmpty: true, attachments: [] })).toBe(false);
30
+ expect(resolveCanSend({ isEmpty: true })).toBe(false);
31
+ });
32
+ });
33
+ describe('ai-chat-input · buildMessageContent', () => {
34
+ it('省略空字段,只带非空内容', () => {
35
+ const msg = buildMessageContent({ inputContents: [{ type: 'text', text: 'hi' }] });
36
+ expect(msg).toEqual({ inputContents: [{ type: 'text', text: 'hi' }] });
37
+ expect(msg.attachments).toBeUndefined();
38
+ expect(msg.references).toBeUndefined();
39
+ expect(msg.setup).toBeUndefined();
40
+ });
41
+ it('全字段均带上', () => {
42
+ const msg = buildMessageContent({
43
+ inputContents: [{ type: 'text', text: 'hi' }],
44
+ attachments: [{ uid: '1', name: 'a.png' }],
45
+ references: [{ type: 'file', id: 'r1' }],
46
+ setup: { model: 'gpt-5' },
47
+ });
48
+ expect(msg.inputContents).toHaveLength(1);
49
+ expect(msg.attachments).toHaveLength(1);
50
+ expect(msg.references).toHaveLength(1);
51
+ expect(msg.setup).toEqual({ model: 'gpt-5' });
52
+ });
53
+ it('空数组与空对象视作无内容而省略', () => {
54
+ expect(buildMessageContent({ inputContents: [], attachments: [], setup: {} })).toEqual({});
55
+ });
56
+ });
57
+ describe('ai-chat-input · transformDocToContents', () => {
58
+ it('段落文本 → text content,空段丢弃', () => {
59
+ const doc = {
60
+ type: 'doc',
61
+ content: [
62
+ { type: 'paragraph', content: [{ type: 'text', text: '你好' }] },
63
+ { type: 'paragraph' }, // 空段
64
+ { type: 'paragraph', content: [{ type: 'text', text: 'world' }] },
65
+ ],
66
+ };
67
+ expect(transformDocToContents(doc)).toEqual([
68
+ { type: 'text', text: '你好' },
69
+ { type: 'text', text: 'world' },
70
+ ]);
71
+ });
72
+ it('hardBreak → 换行;嵌套 marks 文本连接', () => {
73
+ const doc = {
74
+ type: 'doc',
75
+ content: [
76
+ {
77
+ type: 'paragraph',
78
+ content: [{ type: 'text', text: 'a' }, { type: 'hardBreak' }, { type: 'text', text: 'b' }],
79
+ },
80
+ ],
81
+ };
82
+ expect(transformDocToContents(doc)).toEqual([{ type: 'text', text: 'a\nb' }]);
83
+ });
84
+ it('transformer 覆盖特定节点转换', () => {
85
+ const doc = {
86
+ type: 'doc',
87
+ content: [{ type: 'inputSlot', attrs: { value: 'x' } }],
88
+ };
89
+ const transformer = new Map([
90
+ ['inputSlot', (n) => ({ type: 'slot', raw: n })],
91
+ ]);
92
+ expect(transformDocToContents(doc, transformer)).toEqual([
93
+ { type: 'slot', raw: { type: 'inputSlot', attrs: { value: 'x' } } },
94
+ ]);
95
+ });
96
+ it('非法/空 JSON → 空数组', () => {
97
+ expect(transformDocToContents(undefined)).toEqual([]);
98
+ expect(transformDocToContents({})).toEqual([]);
99
+ expect(transformDocToContents({ content: null })).toEqual([]);
100
+ });
101
+ });
102
+ describe('ai-chat-input · suggestionContent', () => {
103
+ it('string 直接返回', () => {
104
+ expect(suggestionContent('帮我写代码')).toBe('帮我写代码');
105
+ });
106
+ it('对象取 content', () => {
107
+ expect(suggestionContent({ content: '翻译', lang: 'en' })).toBe('翻译');
108
+ });
109
+ });
110
+ describe('ai-chat-input · nextSuggestionIndex', () => {
111
+ it('len<=0 返回 -1', () => {
112
+ expect(nextSuggestionIndex(0, 0, 1)).toBe(-1);
113
+ expect(nextSuggestionIndex(2, -1, -1)).toBe(-1);
114
+ });
115
+ it('未选中(current<0):向下从 0、向上从末项', () => {
116
+ expect(nextSuggestionIndex(-1, 3, 1)).toBe(0);
117
+ expect(nextSuggestionIndex(-1, 3, -1)).toBe(2);
118
+ });
119
+ it('向下环绕', () => {
120
+ expect(nextSuggestionIndex(0, 3, 1)).toBe(1);
121
+ expect(nextSuggestionIndex(2, 3, 1)).toBe(0);
122
+ });
123
+ it('向上环绕', () => {
124
+ expect(nextSuggestionIndex(1, 3, -1)).toBe(0);
125
+ expect(nextSuggestionIndex(0, 3, -1)).toBe(2);
126
+ });
127
+ });
128
+ describe('ai-chat-input · referenceLabel', () => {
129
+ it("type='text' 用 content", () => {
130
+ expect(referenceLabel({ type: 'text', id: '1', content: '引用文本' })).toBe('引用文本');
131
+ });
132
+ it('非文本用 name,缺省回退 id', () => {
133
+ expect(referenceLabel({ type: 'file', id: '1', name: 'a.pdf' })).toBe('a.pdf');
134
+ expect(referenceLabel({ type: 'file', id: 'fallback-id' })).toBe('fallback-id');
135
+ });
136
+ });
137
+ describe('ai-chat-input · isImageReference', () => {
138
+ it("type='image' 判图", () => {
139
+ expect(isImageReference({ type: 'image', id: '1' })).toBe(true);
140
+ });
141
+ it('url 图片扩展名判图(含 query/hash)', () => {
142
+ expect(isImageReference({ type: 'file', id: '1', url: 'a.png' })).toBe(true);
143
+ expect(isImageReference({ type: 'file', id: '1', url: 'https://x/y.JPG?v=2' })).toBe(true);
144
+ expect(isImageReference({ type: 'file', id: '1', url: 'doc.pdf' })).toBe(false);
145
+ expect(isImageReference({ type: 'file', id: '1' })).toBe(false);
146
+ });
147
+ });
148
+ describe('ai-chat-input · skillLabel', () => {
149
+ it('label 优先,回退 value,再回退空串', () => {
150
+ expect(skillLabel({ label: '总结', value: 'summarize' })).toBe('总结');
151
+ expect(skillLabel({ value: 'summarize' })).toBe('summarize');
152
+ expect(skillLabel({})).toBe('');
153
+ });
154
+ });
155
+ describe('ai-chat-input · getSkillSlotHTML', () => {
156
+ it('生成 skill-slot 节点 HTML,带 data 属性', () => {
157
+ const html = getSkillSlotHTML({ label: '总结', value: 'sum', hasTemplate: true });
158
+ expect(html).toBe('<skill-slot data-label="总结" data-value="sum" data-template="true"></skill-slot>');
159
+ });
160
+ it('省略未提供的属性', () => {
161
+ expect(getSkillSlotHTML({ label: '仅标签' })).toBe('<skill-slot data-label="仅标签"></skill-slot>');
162
+ });
163
+ it('转义属性值防注入', () => {
164
+ const html = getSkillSlotHTML({ label: '"><img>&' });
165
+ expect(html).toContain('data-label="&quot;&gt;&lt;img&gt;&amp;"');
166
+ expect(html).not.toContain('<img>');
167
+ });
168
+ });
169
+ describe('ai-chat-input · shouldOpenSkillPanel', () => {
170
+ it('空编辑区 + 命中 skillHotKey + 有技能 → true', () => {
171
+ expect(shouldOpenSkillPanel({ key: '/', skillHotKey: '/', isEmpty: true, skillCount: 2 })).toBe(true);
172
+ });
173
+ it('非空编辑区不触发', () => {
174
+ expect(shouldOpenSkillPanel({ key: '/', skillHotKey: '/', isEmpty: false, skillCount: 2 })).toBe(false);
175
+ });
176
+ it('键不匹配不触发', () => {
177
+ expect(shouldOpenSkillPanel({ key: 'a', skillHotKey: '/', isEmpty: true, skillCount: 2 })).toBe(false);
178
+ });
179
+ it('无技能不触发', () => {
180
+ expect(shouldOpenSkillPanel({ key: '/', skillHotKey: '/', isEmpty: true, skillCount: 0 })).toBe(false);
181
+ });
182
+ });
183
+ describe('ai-chat-input · setConfigureField', () => {
184
+ it('合并字段补丁,不可变', () => {
185
+ const v = { model: 'gpt-5' };
186
+ const next = setConfigureField(v, { web: true });
187
+ expect(next).toEqual({ model: 'gpt-5', web: true });
188
+ expect(v).toEqual({ model: 'gpt-5' }); // 原对象不变
189
+ });
190
+ it('同字段覆盖', () => {
191
+ expect(setConfigureField({ model: 'a' }, { model: 'b' })).toEqual({ model: 'b' });
192
+ });
193
+ });
194
+ describe('ai-chat-input · removeConfigureField', () => {
195
+ it('移除字段,不可变', () => {
196
+ const v = { model: 'gpt-5', web: true };
197
+ const next = removeConfigureField(v, 'web');
198
+ expect(next).toEqual({ model: 'gpt-5' });
199
+ expect(v).toEqual({ model: 'gpt-5', web: true });
200
+ });
201
+ it('移除不存在的字段无副作用', () => {
202
+ expect(removeConfigureField({ a: 1 }, 'b')).toEqual({ a: 1 });
203
+ });
204
+ });
205
+ describe('ai-chat-input · messageToChatInput', () => {
206
+ it('inputContents → input_text,附件按类型 → input_image/input_file', () => {
207
+ const msg = messageToChatInput({
208
+ inputContents: [{ type: 'text', text: '你好' }],
209
+ attachments: [
210
+ { uid: '1', name: 'a.png', url: 'https://x/a.png' },
211
+ { uid: '2', name: 'doc.pdf', url: 'https://x/doc.pdf' },
212
+ ],
213
+ }, { id: 'm1', model: 'gpt-5' });
214
+ expect(msg.role).toBe('user');
215
+ expect(msg.id).toBe('m1');
216
+ expect(msg.model).toBe('gpt-5');
217
+ const inner = msg.content[0].content;
218
+ expect(inner).toEqual([
219
+ { type: 'input_text', text: '你好' },
220
+ { type: 'input_image', image_url: 'https://x/a.png', file_id: '1' },
221
+ { type: 'input_file', filename: 'doc.pdf', file_url: 'https://x/doc.pdf', file_id: '2' },
222
+ ]);
223
+ });
224
+ it('缺 id 默认空串;空文本段丢弃', () => {
225
+ const msg = messageToChatInput({ inputContents: [{ type: 'text', text: '' }] });
226
+ expect(msg.id).toBe('');
227
+ const inner = msg.content[0].content;
228
+ expect(inner).toEqual([]);
229
+ });
230
+ it('按 url 图片扩展名判图(attachment.type 是 file/directory 非 mime)', () => {
231
+ const msg = messageToChatInput({ attachments: [{ uid: '1', url: 'https://x/pic.webp' }] });
232
+ const inner = msg.content[0].content;
233
+ expect(inner[0].type).toBe('input_image');
234
+ });
235
+ });
236
+ describe('ai-chat-input · chatInputToChatCompletion', () => {
237
+ it('转成 OpenAI user message 多模态 parts', () => {
238
+ const m = chatInputToChatCompletion({
239
+ inputContents: [{ type: 'text', text: 'hi' }],
240
+ attachments: [{ uid: '1', name: 'a.jpg', url: 'https://x/a.jpg' }],
241
+ });
242
+ expect(m.role).toBe('user');
243
+ expect(m.content).toEqual([
244
+ { type: 'text', text: 'hi' },
245
+ { type: 'image_url', image_url: { url: 'https://x/a.jpg' } },
246
+ ]);
247
+ });
248
+ it('非图附件 → file part', () => {
249
+ const m = chatInputToChatCompletion({
250
+ attachments: [{ uid: '1', name: 'r.pdf', url: 'https://x/r.pdf' }],
251
+ });
252
+ expect(m.content).toEqual([
253
+ { type: 'file', file: { filename: 'r.pdf', file_data: 'https://x/r.pdf' } },
254
+ ]);
255
+ });
256
+ it('空载荷 → 空 content', () => {
257
+ expect(chatInputToChatCompletion({}).content).toEqual([]);
258
+ });
259
+ });
260
+ describe('ai-chat-input · getSelectSlotHTML', () => {
261
+ it('生成 select-slot 节点 HTML,options 为转义 JSON', () => {
262
+ const html = getSelectSlotHTML(['北京', '上海'], '北京');
263
+ expect(html).toContain('<select-slot ');
264
+ expect(html).toContain('value="北京"');
265
+ // options 是 JSON 且引号被转义
266
+ expect(html).toContain('options="[&quot;北京&quot;,&quot;上海&quot;]"');
267
+ });
268
+ it('无默认值时省略 value', () => {
269
+ const html = getSelectSlotHTML(['a']);
270
+ expect(html).not.toContain('value=');
271
+ });
272
+ });
273
+ describe('ai-chat-input · transformDocToContents · 内联 slot 归一', () => {
274
+ it('selectSlot 取 attrs.value,skillSlot 取 label', () => {
275
+ const doc = {
276
+ type: 'doc',
277
+ content: [
278
+ {
279
+ type: 'paragraph',
280
+ content: [
281
+ { type: 'text', text: '去 ' },
282
+ { type: 'selectSlot', attrs: { value: '上海' } },
283
+ { type: 'text', text: ' 出差,用 ' },
284
+ { type: 'skillSlot', attrs: { label: '总结', value: 'sum' } },
285
+ ],
286
+ },
287
+ ],
288
+ };
289
+ expect(transformDocToContents(doc)).toEqual([{ type: 'text', text: '去 上海 出差,用 总结' }]);
290
+ });
291
+ it('skillSlot 无 label 回退 value', () => {
292
+ const doc = {
293
+ type: 'doc',
294
+ content: [{ type: 'paragraph', content: [{ type: 'skillSlot', attrs: { value: 'sk' } }] }],
295
+ };
296
+ expect(transformDocToContents(doc)).toEqual([{ type: 'text', text: 'sk' }]);
297
+ });
298
+ });
299
+ describe('ai-chat-input · getInputSlotHTML', () => {
300
+ it('生成 input-slot 节点,带 placeholder + 零宽锚点', () => {
301
+ const html = getInputSlotHTML('填城市');
302
+ expect(html).toBe(`<input-slot placeholder="填城市">${ZW}</input-slot>`);
303
+ });
304
+ it('有 value 时用转义内容', () => {
305
+ expect(getInputSlotHTML('', '<b>x</b>')).toBe('<input-slot>&lt;b&gt;x&lt;/b&gt;</input-slot>');
306
+ });
307
+ });
308
+ describe('ai-chat-input · transformDocToContents · inputSlot 归一', () => {
309
+ it('inputSlot 有内容取内容,剔零宽字符', () => {
310
+ const doc = {
311
+ type: 'doc',
312
+ content: [
313
+ {
314
+ type: 'paragraph',
315
+ content: [
316
+ { type: 'text', text: '去 ' },
317
+ { type: 'inputSlot', attrs: { placeholder: '城市' }, content: [{ type: 'text', text: '北京' }] },
318
+ ],
319
+ },
320
+ ],
321
+ };
322
+ expect(transformDocToContents(doc)).toEqual([{ type: 'text', text: '去 北京' }]);
323
+ });
324
+ it('inputSlot 空(仅零宽)回退 placeholder', () => {
325
+ const doc = {
326
+ type: 'doc',
327
+ content: [
328
+ {
329
+ type: 'paragraph',
330
+ content: [
331
+ { type: 'inputSlot', attrs: { placeholder: '请输入城市' }, content: [{ type: 'text', text: ZW }] },
332
+ ],
333
+ },
334
+ ],
335
+ };
336
+ expect(transformDocToContents(doc)).toEqual([{ type: 'text', text: '请输入城市' }]);
337
+ });
338
+ });
339
+ //# sourceMappingURL=ai-chat-input.test.js.map