@veltra/desktop 1.9.2 → 1.11.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 (110) hide show
  1. package/dist/components/ai-button/ai-button.js +117 -0
  2. package/dist/components/ai-button/ai-button.js.map +1 -0
  3. package/dist/components/ai-button/style.d.ts +1 -0
  4. package/dist/components/ai-button/style.js +3 -0
  5. package/dist/components/ai-button/style2.css +29 -0
  6. package/dist/components/code-editor/basic-setup.js +5 -5
  7. package/dist/components/code-editor/code-editor.js +2 -2
  8. package/dist/components/code-editor/langs/bash/index.js +2 -2
  9. package/dist/components/code-editor/langs/powershell/index.js +2 -2
  10. package/dist/components/code-editor/langs/spel/index.js +1 -1
  11. package/dist/components/code-editor/langs/spel/language.js +1 -1
  12. package/dist/components/code-editor/shell-extension.js +2 -2
  13. package/dist/components/file-viewer/previewers/pdf-worker-wrapper.js.map +1 -1
  14. package/dist/components/file-viewer/previewers/sheet-previewer.js +10 -14
  15. package/dist/components/file-viewer/previewers/sheet-previewer.js.map +1 -1
  16. package/dist/components/file-viewer/style2.css +42 -176
  17. package/dist/components/index.d.ts +2 -1
  18. package/dist/components/index.js +3 -1
  19. package/dist/components/index.js.map +1 -1
  20. package/dist/index.d.ts +108 -106
  21. package/dist/index.js +107 -106
  22. package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js +3 -3
  23. package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -1
  24. package/dist/node_modules/.bun/{@codemirror_commands@6.11.0 → @codemirror_commands@6.11.1}/node_modules/@codemirror/commands/dist/index.js +7 -7
  25. package/dist/node_modules/.bun/@codemirror_commands@6.11.1/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  26. package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js +3 -3
  27. package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js.map +1 -1
  28. package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js +3 -3
  29. package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js.map +1 -1
  30. package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js +2 -2
  31. package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js.map +1 -1
  32. package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js +5 -5
  33. package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js.map +1 -1
  34. package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js +1 -1
  35. package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js.map +1 -1
  36. package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js +5 -5
  37. package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js.map +1 -1
  38. package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js +3 -3
  39. package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js.map +1 -1
  40. package/dist/node_modules/.bun/{@codemirror_language@6.12.4 → @codemirror_language@6.13.1}/node_modules/@codemirror/language/dist/index.js +8 -171
  41. package/dist/node_modules/.bun/@codemirror_language@6.13.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  42. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js +1 -1
  43. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js.map +1 -1
  44. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js +1 -1
  45. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js.map +1 -1
  46. package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js +2 -2
  47. package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js.map +1 -1
  48. package/dist/node_modules/.bun/{@codemirror_search@6.7.1 → @codemirror_search@6.7.2}/node_modules/@codemirror/search/dist/index.js +10 -3
  49. package/dist/node_modules/.bun/@codemirror_search@6.7.2/node_modules/@codemirror/search/dist/index.js.map +1 -0
  50. package/dist/node_modules/.bun/{@codemirror_state@6.7.1 → @codemirror_state@6.7.6}/node_modules/@codemirror/state/dist/index.js +51 -17
  51. package/dist/node_modules/.bun/@codemirror_state@6.7.6/node_modules/@codemirror/state/dist/index.js.map +1 -0
  52. package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js +169 -0
  53. package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js.map +1 -0
  54. package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js +3 -3
  55. package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js.map +1 -1
  56. package/dist/node_modules/.bun/{@codemirror_view@6.43.9 → @codemirror_view@6.43.14}/node_modules/@codemirror/view/dist/index.js +160 -94
  57. package/dist/node_modules/.bun/@codemirror_view@6.43.14/node_modules/@codemirror/view/dist/index.js.map +1 -0
  58. package/dist/node_modules/.bun/{@lezer_common@1.5.2 → @lezer_common@1.5.3}/node_modules/@lezer/common/dist/index.js +4 -4
  59. package/dist/node_modules/.bun/{@lezer_common@1.5.2 → @lezer_common@1.5.3}/node_modules/@lezer/common/dist/index.js.map +1 -1
  60. package/dist/node_modules/.bun/@lezer_css@1.3.9/node_modules/@lezer/css/dist/index.js +278 -0
  61. package/dist/node_modules/.bun/@lezer_css@1.3.9/node_modules/@lezer/css/dist/index.js.map +1 -0
  62. package/dist/node_modules/.bun/{@lezer_highlight@1.2.3 → @lezer_highlight@1.2.5}/node_modules/@lezer/highlight/dist/index.js +4 -4
  63. package/dist/node_modules/.bun/@lezer_highlight@1.2.5/node_modules/@lezer/highlight/dist/index.js.map +1 -0
  64. package/dist/node_modules/.bun/@lezer_html@1.3.13/node_modules/@lezer/html/dist/index.js +3 -3
  65. package/dist/node_modules/.bun/@lezer_html@1.3.13/node_modules/@lezer/html/dist/index.js.map +1 -1
  66. package/dist/node_modules/.bun/@lezer_java@1.1.5/node_modules/@lezer/java/dist/index.js +240 -0
  67. package/dist/node_modules/.bun/@lezer_java@1.1.5/node_modules/@lezer/java/dist/index.js.map +1 -0
  68. package/dist/node_modules/.bun/@lezer_javascript@1.5.6/node_modules/@lezer/javascript/dist/index.js +491 -0
  69. package/dist/node_modules/.bun/@lezer_javascript@1.5.6/node_modules/@lezer/javascript/dist/index.js.map +1 -0
  70. package/dist/node_modules/.bun/@lezer_json@1.0.3/node_modules/@lezer/json/dist/index.js +2 -2
  71. package/dist/node_modules/.bun/@lezer_json@1.0.3/node_modules/@lezer/json/dist/index.js.map +1 -1
  72. package/dist/node_modules/.bun/{@lezer_lr@1.4.10 → @lezer_lr@1.4.11}/node_modules/@lezer/lr/dist/index.js +41 -29
  73. package/dist/node_modules/.bun/@lezer_lr@1.4.11/node_modules/@lezer/lr/dist/index.js.map +1 -0
  74. package/dist/node_modules/.bun/{@lezer_markdown@1.7.2 → @lezer_markdown@1.8.0}/node_modules/@lezer/markdown/dist/index.js +17 -8
  75. package/dist/node_modules/.bun/@lezer_markdown@1.8.0/node_modules/@lezer/markdown/dist/index.js.map +1 -0
  76. package/dist/node_modules/.bun/{@marijn_find-cluster-break@1.0.3 → @marijn_find-cluster-break@1.0.4}/node_modules/@marijn/find-cluster-break/src/index.js +2 -2
  77. package/dist/node_modules/.bun/@marijn_find-cluster-break@1.0.4/node_modules/@marijn/find-cluster-break/src/index.js.map +1 -0
  78. package/dist/node_modules/.bun/crelt@1.0.7/node_modules/crelt/index.js.map +1 -1
  79. package/dist/node_modules/.bun/{style-mod@4.1.3 → style-mod@4.1.4}/node_modules/style-mod/src/style-mod.js +9 -4
  80. package/dist/node_modules/.bun/style-mod@4.1.4/node_modules/style-mod/src/style-mod.js.map +1 -0
  81. package/dist/node_modules/.bun/w3c-keyname@2.2.8/node_modules/w3c-keyname/index.js.map +1 -1
  82. package/dist/types/ai-button.d.ts +42 -0
  83. package/dist/types/index.d.ts +2 -1
  84. package/package.json +16 -22
  85. package/src/components/ai-button/__test__/ai-button.test.ts +375 -0
  86. package/src/components/ai-button/ai-button.vue +87 -0
  87. package/src/components/ai-button/index.ts +1 -0
  88. package/src/components/ai-button/style.scss +19 -0
  89. package/src/components/ai-button/style.ts +3 -0
  90. package/src/components/file-viewer/previewers/sheet-previewer.vue +7 -12
  91. package/src/components/file-viewer/style.scss +52 -233
  92. package/src/components/index.ts +2 -0
  93. package/src/types/ai-button.ts +44 -0
  94. package/src/types/index.ts +2 -0
  95. package/dist/node_modules/.bun/@codemirror_commands@6.11.0/node_modules/@codemirror/commands/dist/index.js.map +0 -1
  96. package/dist/node_modules/.bun/@codemirror_language@6.12.4/node_modules/@codemirror/language/dist/index.js.map +0 -1
  97. package/dist/node_modules/.bun/@codemirror_search@6.7.1/node_modules/@codemirror/search/dist/index.js.map +0 -1
  98. package/dist/node_modules/.bun/@codemirror_state@6.7.1/node_modules/@codemirror/state/dist/index.js.map +0 -1
  99. package/dist/node_modules/.bun/@codemirror_view@6.43.9/node_modules/@codemirror/view/dist/index.js.map +0 -1
  100. package/dist/node_modules/.bun/@lezer_css@1.3.6/node_modules/@lezer/css/dist/index.js +0 -268
  101. package/dist/node_modules/.bun/@lezer_css@1.3.6/node_modules/@lezer/css/dist/index.js.map +0 -1
  102. package/dist/node_modules/.bun/@lezer_highlight@1.2.3/node_modules/@lezer/highlight/dist/index.js.map +0 -1
  103. package/dist/node_modules/.bun/@lezer_java@1.1.3/node_modules/@lezer/java/dist/index.js +0 -235
  104. package/dist/node_modules/.bun/@lezer_java@1.1.3/node_modules/@lezer/java/dist/index.js.map +0 -1
  105. package/dist/node_modules/.bun/@lezer_javascript@1.5.4/node_modules/@lezer/javascript/dist/index.js +0 -491
  106. package/dist/node_modules/.bun/@lezer_javascript@1.5.4/node_modules/@lezer/javascript/dist/index.js.map +0 -1
  107. package/dist/node_modules/.bun/@lezer_lr@1.4.10/node_modules/@lezer/lr/dist/index.js.map +0 -1
  108. package/dist/node_modules/.bun/@lezer_markdown@1.7.2/node_modules/@lezer/markdown/dist/index.js.map +0 -1
  109. package/dist/node_modules/.bun/@marijn_find-cluster-break@1.0.3/node_modules/@marijn/find-cluster-break/src/index.js.map +0 -1
  110. package/dist/node_modules/.bun/style-mod@4.1.3/node_modules/style-mod/src/style-mod.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veltra/desktop",
3
- "version": "1.9.2",
3
+ "version": "1.11.0",
4
4
  "homepage": "https://github.com/cabinet-fe/ultra-ui#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/cabinet-fe/ultra-ui/issues"
@@ -48,33 +48,33 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@lexical/history": "^0.51.0",
52
- "@lexical/html": "^0.51.0",
53
- "@lexical/link": "^0.51.0",
54
- "@lexical/list": "^0.51.0",
55
- "@lexical/rich-text": "^0.51.0",
56
- "@lexical/selection": "^0.51.0",
57
- "@lexical/utils": "^0.51.0",
51
+ "@lexical/history": "^0.52.0",
52
+ "@lexical/html": "^0.52.0",
53
+ "@lexical/link": "^0.52.0",
54
+ "@lexical/list": "^0.52.0",
55
+ "@lexical/rich-text": "^0.52.0",
56
+ "@lexical/selection": "^0.52.0",
57
+ "@lexical/utils": "^0.52.0",
58
58
  "docx-preview": "^0.4.1",
59
- "infinitable": "^0.1.0",
60
- "lexical": "^0.51.0",
59
+ "infinitable": "^0.1.7",
60
+ "lexical": "^0.52.0",
61
61
  "pdfjs-dist": "6.4.299"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@codemirror/autocomplete": "6.20.3",
65
- "@codemirror/commands": "6.11.0",
65
+ "@codemirror/commands": "6.11.1",
66
66
  "@codemirror/lang-java": "6.0.2",
67
67
  "@codemirror/lang-javascript": "6.2.5",
68
68
  "@codemirror/lang-json": "6.0.2",
69
69
  "@codemirror/lang-markdown": "6.5.2",
70
70
  "@codemirror/lang-sql": "6.10.0",
71
- "@codemirror/language": "6.12.4",
72
- "@codemirror/legacy-modes": "6.5.4",
71
+ "@codemirror/language": "6.13.1",
72
+ "@codemirror/legacy-modes": "6.5.5",
73
73
  "@codemirror/lint": "6.9.7",
74
- "@codemirror/search": "6.7.1",
75
- "@codemirror/state": "6.7.1",
74
+ "@codemirror/search": "6.7.2",
75
+ "@codemirror/state": "6.7.6",
76
76
  "@codemirror/theme-one-dark": "6.1.3",
77
- "@codemirror/view": "6.43.9",
77
+ "@codemirror/view": "6.43.14",
78
78
  "@veltra/ofd-core": "1.0.1"
79
79
  },
80
80
  "peerDependencies": {
@@ -83,14 +83,8 @@
83
83
  "@veltra/compositions": "^1.7.14",
84
84
  "@veltra/directives": "^1.7.14",
85
85
  "@veltra/icons": "^1.5.0",
86
- "@veltra/sheet-core": "^2.5.7",
87
86
  "@veltra/styles": "^1.7.14",
88
87
  "@veltra/utils": "^1.7.14",
89
88
  "vue": ">=3.5.43"
90
- },
91
- "peerDependenciesMeta": {
92
- "@veltra/sheet-core": {
93
- "optional": true
94
- }
95
89
  }
96
90
  }
@@ -0,0 +1,375 @@
1
+ import { useConfig } from '@veltra/compositions'
2
+ import type { AiProviderConfig } from '@veltra/utils'
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vite-plus/test'
4
+ import { createApp, h, nextTick } from 'vue'
5
+
6
+ import UAiButton from '../ai-button.vue'
7
+
8
+ const { setConfig } = useConfig()
9
+
10
+ /** useConfig 的 ai 是模块级全局状态,测后复位避免污染其它用例 */
11
+ function resetAiConfig() {
12
+ setConfig({
13
+ ai: {
14
+ url: undefined,
15
+ apiKey: undefined,
16
+ model: undefined,
17
+ reasoningLevel: undefined,
18
+ stream: undefined
19
+ }
20
+ })
21
+ }
22
+
23
+ function provider(overrides: Partial<AiProviderConfig> = {}): Partial<AiProviderConfig> {
24
+ return {
25
+ url: 'https://api.example.com/v1/chat/completions',
26
+ apiKey: 'sk-test',
27
+ model: 'gpt-x',
28
+ ...overrides
29
+ }
30
+ }
31
+
32
+ function jsonResponse(text: string): Response {
33
+ return new Response(
34
+ JSON.stringify({ choices: [{ message: { role: 'assistant', content: text } }] }),
35
+ { status: 200, headers: { 'Content-Type': 'application/json' } }
36
+ )
37
+ }
38
+
39
+ function sseResponse(sse: string): Response {
40
+ return new Response(sse, { status: 200, headers: { 'Content-Type': 'text/event-stream' } })
41
+ }
42
+
43
+ function deferred<T>() {
44
+ let resolve!: (value: T) => void
45
+ const promise = new Promise<T>((r) => {
46
+ resolve = r
47
+ })
48
+ return { promise, resolve }
49
+ }
50
+
51
+ function mountAiButton(
52
+ props: Record<string, unknown> = {},
53
+ slots: Record<string, () => unknown> = {}
54
+ ) {
55
+ const host = document.createElement('div')
56
+ document.body.appendChild(host)
57
+
58
+ const app = createApp({ render: () => h(UAiButton, props, slots) })
59
+ app.mount(host)
60
+
61
+ return {
62
+ host,
63
+ button: host.querySelector('button')!,
64
+ unmount() {
65
+ app.unmount()
66
+ host.remove()
67
+ }
68
+ }
69
+ }
70
+
71
+ function click(el: Element) {
72
+ el.dispatchEvent(new MouseEvent('click', { bubbles: true }))
73
+ }
74
+
75
+ function requestBody(fetchMock: ReturnType<typeof vi.fn>, nth = 1): Record<string, any> {
76
+ return JSON.parse(String(fetchMock.mock.calls[nth - 1]![1]!.body))
77
+ }
78
+
79
+ beforeEach(() => {
80
+ // happy-dom 中 offsetWidth/offsetHeight 恒为 0,模拟尺寸供 usePop 定位
81
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(200)
82
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(200)
83
+
84
+ // usePop 的浮层容器是模块级单例,上一个用例清空 body 后需补回 teleport 目标
85
+ if (!document.getElementById('pop-container')) {
86
+ const popContainer = document.createElement('div')
87
+ popContainer.id = 'pop-container'
88
+ document.body.appendChild(popContainer)
89
+ }
90
+ })
91
+
92
+ afterEach(() => {
93
+ vi.unstubAllGlobals()
94
+ vi.restoreAllMocks()
95
+ resetAiConfig()
96
+ document.body.innerHTML = ''
97
+ })
98
+
99
+ describe('UAiButton 普通变体', () => {
100
+ it('点击按 OpenAI 兼容协议 POST 配置 url,请求体组装 model/stream/messages,成功触发 success 完整文本', async () => {
101
+ const fetchMock = vi.fn(async () => jsonResponse('完整答案'))
102
+ vi.stubGlobal('fetch', fetchMock)
103
+ const onSuccess = vi.fn()
104
+ const onError = vi.fn()
105
+
106
+ const { button, unmount } = mountAiButton({
107
+ provider: provider({ model: 'gpt-x' }),
108
+ systemPrompt: 'sys',
109
+ prompt: 'hi',
110
+ onSuccess,
111
+ onError
112
+ })
113
+
114
+ try {
115
+ click(button)
116
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('完整答案'))
117
+ expect(onError).not.toHaveBeenCalled()
118
+
119
+ expect(fetchMock).toHaveBeenCalledTimes(1)
120
+ expect(fetchMock.mock.calls[0]![0]).toBe('https://api.example.com/v1/chat/completions')
121
+ expect(requestBody(fetchMock)).toMatchObject({
122
+ model: 'gpt-x',
123
+ stream: false,
124
+ messages: [
125
+ { role: 'system', content: 'sys' },
126
+ { role: 'user', content: 'hi' }
127
+ ]
128
+ })
129
+ } finally {
130
+ unmount()
131
+ }
132
+ })
133
+
134
+ it('流式:增量逐段触发 delta,结束后 success 为拼接完整文本', async () => {
135
+ const sse =
136
+ 'data: {"choices":[{"delta":{"content":"He"}}]}\n\n' +
137
+ 'data: {"choices":[{"delta":{"content":"llo"}}]}\n\n' +
138
+ 'data: [DONE]\n\n'
139
+ const fetchMock = vi.fn(async () => sseResponse(sse))
140
+ vi.stubGlobal('fetch', fetchMock)
141
+ const onDelta = vi.fn()
142
+ const onSuccess = vi.fn()
143
+
144
+ const { button, unmount } = mountAiButton({
145
+ provider: provider({ stream: true }),
146
+ prompt: 'hi',
147
+ onDelta,
148
+ onSuccess
149
+ })
150
+
151
+ try {
152
+ click(button)
153
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalled())
154
+
155
+ expect(onDelta).toHaveBeenNthCalledWith(1, 'He')
156
+ expect(onDelta).toHaveBeenNthCalledWith(2, 'llo')
157
+ expect(onSuccess).toHaveBeenCalledWith('Hello')
158
+ expect(requestBody(fetchMock).stream).toBe(true)
159
+ } finally {
160
+ unmount()
161
+ }
162
+ })
163
+
164
+ it('失败触发 error(Error 载荷),不触发 success', async () => {
165
+ vi.stubGlobal(
166
+ 'fetch',
167
+ vi
168
+ .fn()
169
+ .mockRejectedValueOnce(new TypeError('network down'))
170
+ .mockResolvedValueOnce(new Response('boom', { status: 500 }))
171
+ )
172
+ const onSuccess = vi.fn()
173
+ const onError = vi.fn()
174
+
175
+ const { button, unmount } = mountAiButton({
176
+ provider: provider(),
177
+ prompt: 'hi',
178
+ onSuccess,
179
+ onError
180
+ })
181
+
182
+ try {
183
+ click(button)
184
+ await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1))
185
+ expect(onError).toHaveBeenNthCalledWith(1, expect.any(Error))
186
+
187
+ click(button)
188
+ await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(2))
189
+ expect(onSuccess).not.toHaveBeenCalled()
190
+ } finally {
191
+ unmount()
192
+ }
193
+ })
194
+
195
+ it('生成中按钮 loading 并忽略重复点击,结束后恢复可点', async () => {
196
+ const pending = deferred<Response>()
197
+ const fetchMock = vi.fn(() => pending.promise)
198
+ vi.stubGlobal('fetch', fetchMock)
199
+ const onSuccess = vi.fn()
200
+
201
+ const { button, unmount } = mountAiButton({ provider: provider(), prompt: 'hi', onSuccess })
202
+
203
+ try {
204
+ click(button)
205
+ await nextTick()
206
+ expect(fetchMock).toHaveBeenCalledTimes(1)
207
+ expect(button.classList.contains('is-loading')).toBe(true)
208
+
209
+ click(button)
210
+ click(button)
211
+ expect(fetchMock).toHaveBeenCalledTimes(1)
212
+
213
+ pending.resolve(jsonResponse('ok'))
214
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('ok'))
215
+ await nextTick()
216
+ expect(button.classList.contains('is-loading')).toBe(false)
217
+
218
+ click(button)
219
+ expect(fetchMock).toHaveBeenCalledTimes(2)
220
+ } finally {
221
+ unmount()
222
+ }
223
+ })
224
+ })
225
+
226
+ describe('UAiButton 配置优先级', () => {
227
+ it('实例 provider 字段 > 全局 ai 字段 > 默认值(stream 缺省 false)', async () => {
228
+ setConfig({
229
+ ai: {
230
+ url: 'https://global.example.com/v1/chat/completions',
231
+ apiKey: 'sk-global',
232
+ model: 'g-global'
233
+ }
234
+ })
235
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
236
+ vi.stubGlobal('fetch', fetchMock)
237
+ const onSuccess = vi.fn()
238
+
239
+ // 组件 A:无 provider,全量走全局配置
240
+ const a = mountAiButton({ prompt: 'a', onSuccess })
241
+ // 组件 B:共用全局配置,仅实例覆盖 model 并开流式
242
+ const sse = 'data: {"choices":[{"delta":{"content":"B"}}]}\n\ndata: [DONE]\n\n'
243
+ fetchMock.mockResolvedValueOnce(jsonResponse('ok')).mockResolvedValueOnce(sseResponse(sse))
244
+ const onDelta = vi.fn()
245
+ const b = mountAiButton({
246
+ prompt: 'b',
247
+ provider: { model: 'g-instance', stream: true },
248
+ onDelta,
249
+ onSuccess
250
+ })
251
+
252
+ try {
253
+ click(a.button)
254
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
255
+ click(b.button)
256
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(2))
257
+
258
+ const bodyA = requestBody(fetchMock, 1)
259
+ expect(fetchMock.mock.calls[0]![0]).toBe('https://global.example.com/v1/chat/completions')
260
+ expect(bodyA.model).toBe('g-global')
261
+ expect(bodyA.stream).toBe(false)
262
+ expect(fetchMock.mock.calls[0]![1]!.headers).toMatchObject({
263
+ Authorization: 'Bearer sk-global'
264
+ })
265
+
266
+ const bodyB = requestBody(fetchMock, 2)
267
+ expect(bodyB.model).toBe('g-instance')
268
+ expect(bodyB.stream).toBe(true)
269
+ expect(onDelta).toHaveBeenCalledWith('B')
270
+ } finally {
271
+ a.unmount()
272
+ b.unmount()
273
+ }
274
+ })
275
+
276
+ it('无全局配置时仅实例 provider 也可用,stream 默认 false', async () => {
277
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
278
+ vi.stubGlobal('fetch', fetchMock)
279
+ const onSuccess = vi.fn()
280
+
281
+ const { button, unmount } = mountAiButton({ prompt: 'hi', provider: provider(), onSuccess })
282
+
283
+ try {
284
+ click(button)
285
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalled())
286
+ expect(requestBody(fetchMock).stream).toBe(false)
287
+ } finally {
288
+ unmount()
289
+ }
290
+ })
291
+ })
292
+
293
+ describe('UAiButton pre-input 变体', () => {
294
+ function findBubble() {
295
+ return document.body.querySelector<HTMLElement>('.u-ai-button__main')
296
+ }
297
+
298
+ function findActionButtons(): HTMLButtonElement[] {
299
+ return Array.from(
300
+ document.body.querySelectorAll<HTMLButtonElement>('.u-ai-button__action button')
301
+ )
302
+ }
303
+
304
+ function mountPreInput(extra: Record<string, unknown> = {}) {
305
+ return mountAiButton(
306
+ { variant: 'pre-input', prompt: 'hi', provider: provider(), ...extra },
307
+ { 'pre-input': () => h('div', { class: 'pre-slot' }, '补充输入') }
308
+ )
309
+ }
310
+
311
+ /** 等待 usePop 打开气泡时的异步定位(含 resize 触发的一轮)落地,避免关闭后回调访问已销毁元素 */
312
+ async function openBubble(button: HTMLButtonElement) {
313
+ click(button)
314
+ await nextTick()
315
+ await nextTick()
316
+ await new Promise((resolve) => setTimeout(resolve, 20))
317
+ }
318
+
319
+ it('点击展开气泡:#pre-input 插槽与内置确认/取消按钮', async () => {
320
+ const { button, unmount } = mountPreInput()
321
+
322
+ try {
323
+ await openBubble(button)
324
+
325
+ expect(findBubble()).not.toBeNull()
326
+ expect(document.body.querySelector('.pre-slot')?.textContent).toBe('补充输入')
327
+ expect(findActionButtons().map((b) => b.textContent?.trim())).toEqual(['取消', '确认'])
328
+ } finally {
329
+ unmount()
330
+ }
331
+ })
332
+
333
+ it('点击按钮本身不发请求,确认后关闭气泡并发起请求', async () => {
334
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
335
+ vi.stubGlobal('fetch', fetchMock)
336
+ const onConfirm = vi.fn()
337
+ const onSuccess = vi.fn()
338
+
339
+ const { button, unmount } = mountPreInput({ onConfirm, onSuccess })
340
+
341
+ try {
342
+ await openBubble(button)
343
+ expect(fetchMock).not.toHaveBeenCalled()
344
+
345
+ click(findActionButtons()[1]!)
346
+ expect(onConfirm).toHaveBeenCalledTimes(1)
347
+ await vi.waitFor(() => expect(findBubble()).toBeNull())
348
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('ok'))
349
+ expect(fetchMock).toHaveBeenCalledTimes(1)
350
+ } finally {
351
+ unmount()
352
+ }
353
+ })
354
+
355
+ it('取消后关闭气泡且不发请求', async () => {
356
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
357
+ vi.stubGlobal('fetch', fetchMock)
358
+ const onCancel = vi.fn()
359
+ const onSuccess = vi.fn()
360
+
361
+ const { button, unmount } = mountPreInput({ onCancel, onSuccess })
362
+
363
+ try {
364
+ await openBubble(button)
365
+
366
+ click(findActionButtons()[0]!)
367
+ expect(onCancel).toHaveBeenCalledTimes(1)
368
+ await vi.waitFor(() => expect(findBubble()).toBeNull())
369
+ expect(fetchMock).not.toHaveBeenCalled()
370
+ expect(onSuccess).not.toHaveBeenCalled()
371
+ } finally {
372
+ unmount()
373
+ }
374
+ })
375
+ })
@@ -0,0 +1,87 @@
1
+ <template>
2
+ <!-- pre-input 变体:先经气泡收集补充输入,确认后再发请求 -->
3
+ <u-tip
4
+ v-if="variant === 'pre-input'"
5
+ trigger="click"
6
+ direction="bottom"
7
+ v-model:visible="visible"
8
+ >
9
+ <u-button :class="cls.b" :size="size" :icon="Sparkles" :loading="loading" />
10
+
11
+ <template #content>
12
+ <div :class="cls.e('main')">
13
+ <slot name="pre-input" />
14
+ </div>
15
+ <div :class="cls.e('action')">
16
+ <u-button size="small" type="primary" text @click="cancel"> 取消 </u-button>
17
+ <u-button size="small" type="primary" @click="confirm"> 确认 </u-button>
18
+ </div>
19
+ </template>
20
+ </u-tip>
21
+
22
+ <!-- 普通变体:点击即发 -->
23
+ <u-button v-else :class="cls.b" :size="size" :icon="Sparkles" :loading="loading" @click="send" />
24
+ </template>
25
+
26
+ <script lang="ts" setup>
27
+ import { useConfig } from '@veltra/compositions'
28
+ import { Sparkles } from '@veltra/icons'
29
+ import { bem, requestAiText } from '@veltra/utils'
30
+ import type { AiProviderConfig } from '@veltra/utils'
31
+ import { computed, ref } from 'vue'
32
+
33
+ import type { AiButtonEmits, AiButtonProps } from '../../types'
34
+ import { UButton } from '../button'
35
+ import { UTip } from '../tip'
36
+
37
+ defineOptions({ name: 'UAiButton' })
38
+
39
+ const { provider, systemPrompt, prompt, variant = 'default', size } = defineProps<AiButtonProps>()
40
+
41
+ const emit = defineEmits<AiButtonEmits>()
42
+
43
+ const cls = bem('ai-button')
44
+
45
+ const { config } = useConfig()
46
+
47
+ /** 配置优先级:实例 provider 字段 > 全局 ai 字段 > 默认值(stream 缺省 false) */
48
+ const requestConfig = computed<AiProviderConfig>(() => ({
49
+ stream: false,
50
+ ...config.ai,
51
+ ...provider
52
+ }))
53
+
54
+ /** 生成中:按钮 loading 转圈并禁用 */
55
+ const loading = ref(false)
56
+
57
+ /** 发起请求;生成中忽略重复触发 */
58
+ function send() {
59
+ if (loading.value) return
60
+ loading.value = true
61
+
62
+ requestAiText({
63
+ config: requestConfig.value,
64
+ prompt: prompt ?? '',
65
+ systemPrompt,
66
+ onDelta: (chunk) => emit('delta', chunk),
67
+ onSuccess: (text) => emit('success', text),
68
+ onError: (error) => emit('error', error)
69
+ }).finally(() => {
70
+ loading.value = false
71
+ })
72
+ }
73
+
74
+ /** pre-input 气泡可见性 */
75
+ const visible = ref(false)
76
+
77
+ function confirm() {
78
+ emit('confirm')
79
+ visible.value = false
80
+ send()
81
+ }
82
+
83
+ function cancel() {
84
+ emit('cancel')
85
+ visible.value = false
86
+ }
87
+ </script>
@@ -0,0 +1 @@
1
+ export { default as UAiButton } from './ai-button.vue'
@@ -0,0 +1,19 @@
1
+ @use 'pkg:@veltra/styles/mixins' as m;
2
+ @use 'pkg:@veltra/styles/functions' as fn;
3
+ @use 'pkg:@veltra/styles/vars';
4
+
5
+ // 方便拼接
6
+ $root-name: ai-button;
7
+
8
+ @include m.b($root-name) {
9
+ @include m.e(main) {
10
+ max-width: 280px;
11
+ width: max-content;
12
+ }
13
+
14
+ @include m.e(action) {
15
+ margin-top: fn.use-var(gap, default);
16
+ @include m.flex($justify: flex-end);
17
+ gap: fn.use-var(gap, small);
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ import '../tip/style'
2
+ import '../button/style'
3
+ import './style.scss'
@@ -1,13 +1,14 @@
1
1
  <template>
2
2
  <div :class="cls.e('sheet')">
3
3
  <div v-if="unavailable" :class="cls.e('empty')">
4
- <u-empty text="无法预览表格:未安装 @veltra/sheet-core" :size="32" />
4
+ <u-empty text="无法预览表格:未安装 infinitable" :size="32" />
5
5
  </div>
6
6
  <template v-else>
7
7
  <div v-if="truncated" :class="cls.e('sheet-note')">
8
8
  共 {{ totalRows.toLocaleString() }} 行,超出预览上限
9
9
  {{ maxRows.toLocaleString() }} 行,已全量加载
10
10
  </div>
11
+ <div ref="container" :class="cls.e('sheet-stage')" />
11
12
  <div v-if="sheets.length > 1" :class="cls.e('sheet-tabs')">
12
13
  <button
13
14
  v-for="(s, i) in sheets"
@@ -18,7 +19,6 @@
18
19
  {{ s.name }}
19
20
  </button>
20
21
  </div>
21
- <div ref="container" :class="cls.e('sheet-stage')" />
22
22
  <div v-if="loading" :class="cls.e('loading')">加载中…</div>
23
23
  </template>
24
24
  </div>
@@ -34,9 +34,8 @@ import { getExtension, toArrayBuffer } from '../helper'
34
34
 
35
35
  defineOptions({ name: 'UFileViewerSheetPreviewer' })
36
36
 
37
- /** 仅类型查询;运行时通过动态 import 加载,未安装时不炸主入口 */
38
- type SheetCoreModule = typeof import('@veltra/sheet-core') &
39
- typeof import('@veltra/sheet-core/grid')
37
+ /** 仅类型查询;运行时通过动态 import 加载(保持 sheet 层按需分包,不打进主产物) */
38
+ type SheetCoreModule = typeof import('infinitable/sheet')
40
39
  type Sheet = InstanceType<SheetCoreModule['Sheet']>
41
40
  type SheetGrid = InstanceType<SheetCoreModule['SheetGrid']>
42
41
 
@@ -64,15 +63,11 @@ function releaseGrid() {
64
63
  grid = undefined
65
64
  }
66
65
 
67
- /** 动态加载 sheet-core;未安装 optional peer 时返回 undefined */
66
+ /** 动态加载电子表格层(infinitable/sheet);加载失败(缺依赖)时返回 undefined */
68
67
  async function resolveSheetCore(): Promise<SheetCoreModule | undefined> {
69
68
  if (sheetCore) return sheetCore
70
69
  try {
71
- const [core, grid] = await Promise.all([
72
- import('@veltra/sheet-core'),
73
- import('@veltra/sheet-core/grid')
74
- ])
75
- sheetCore = { ...core, ...grid }
70
+ sheetCore = await import('infinitable/sheet')
76
71
  return sheetCore
77
72
  } catch {
78
73
  return undefined
@@ -127,7 +122,7 @@ async function load() {
127
122
  if (token !== loadToken) return
128
123
  if (!core) {
129
124
  unavailable.value = true
130
- emit('error', new Error('未安装 @veltra/sheet-core,无法预览 Excel/CSV'))
125
+ emit('error', new Error('未安装 infinitable,无法预览 Excel/CSV'))
131
126
  return
132
127
  }
133
128
  unavailable.value = false