zen-gitsync 2.15.34 → 2.16.2

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 (92) hide show
  1. package/README.md +37 -3
  2. package/package.json +1 -1
  3. package/src/cli/ai/agent.js +493 -83
  4. package/src/cli/ai/agent.test.js +190 -0
  5. package/src/cli/ai/images.js +211 -0
  6. package/src/cli/ai/images.test.js +98 -0
  7. package/src/cli/ai/modelSetup.js +594 -0
  8. package/src/cli/ai/modelSetup.test.js +496 -0
  9. package/src/cli/ai/platformGuard.js +200 -0
  10. package/src/cli/ai/platformGuard.test.js +253 -0
  11. package/src/cli/ai/streamFilter.js +82 -0
  12. package/src/cli/ai/streamFilter.test.js +88 -0
  13. package/src/cli/ai/termui.js +453 -0
  14. package/src/cli/ai/termui.test.js +253 -0
  15. package/src/cli/ai/tools.js +54 -18
  16. package/src/cli/ai/tools.test.js +28 -0
  17. package/src/ui/public/assets/AppVersionBadge-BTznOVSJ.js +2 -0
  18. package/src/ui/public/assets/AppVersionBadge-CS7Trvp-.css +1 -0
  19. package/src/ui/public/assets/BranchSelector-B0eLkAdv.css +1 -0
  20. package/src/ui/public/assets/BranchSelector-BISqgXoZ.js +1 -0
  21. package/src/ui/public/assets/CommitForm-C7FmI0Sl.js +6 -0
  22. package/src/ui/public/assets/CommitForm-DdK8dymc.css +1 -0
  23. package/src/ui/public/assets/CommonDialog-BA4xg5l3.js +1 -0
  24. package/src/ui/public/assets/CommonDialog-ChSKsEz2.css +1 -0
  25. package/src/ui/public/assets/EditorView-BVod-rjn.css +1 -0
  26. package/src/ui/public/assets/EditorView-CSWy4q7G.js +0 -0
  27. package/src/ui/public/assets/FlowExecutionViewer-CZdF1f1e.js +1 -0
  28. package/src/ui/public/assets/FlowExecutionViewer-jNHI5zdm.css +1 -0
  29. package/src/ui/public/assets/FlowOrchestrationWorkspace-BECFOAcQ.js +21 -0
  30. package/src/ui/public/assets/FlowOrchestrationWorkspace-CYGp-PqS.css +1 -0
  31. package/src/ui/public/assets/LogList-CeFFytjB.css +1 -0
  32. package/src/ui/public/assets/LogList-DYoGtOou.js +15 -0
  33. package/src/ui/public/assets/MindmapView-3rMfOoNm.css +1 -0
  34. package/src/ui/public/assets/MindmapView-CU3tXgbR.js +1 -0
  35. package/src/ui/public/assets/MonitorView-CSAqU7oV.js +1 -0
  36. package/src/ui/public/assets/MonitorView-TaS4kL0Y.css +1 -0
  37. package/src/ui/public/assets/ProjectStartupButton-2By0-fuq.js +1 -0
  38. package/src/ui/public/assets/ProjectStartupButton-QLPltCK5.css +1 -0
  39. package/src/ui/public/assets/RemoteRepoCard-BRGNlbeO.css +1 -0
  40. package/src/ui/public/assets/RemoteRepoCard-DHsJwTEX.js +5 -0
  41. package/src/ui/public/assets/SourceMapView-BNRjvgzH.css +1 -0
  42. package/src/ui/public/assets/SourceMapView-DskfUia5.js +3 -0
  43. package/src/ui/public/assets/SvgIcon-BqWp2WS8.js +1 -0
  44. package/src/ui/public/assets/{SvgIcon-BQ6bR7l0.css → SvgIcon-CDNsk4-Q.css} +1 -1
  45. package/src/ui/public/assets/UserInputNode-BVgQkB2l.css +1 -0
  46. package/src/ui/public/assets/UserInputNode-Ccnh0e-0.js +1 -0
  47. package/src/ui/public/assets/WorkbenchView-1q0xaqyV.js +28 -0
  48. package/src/ui/public/assets/{WorkbenchView-CQfjF0Gr.css → WorkbenchView-DbF4hH5s.css} +1 -1
  49. package/src/ui/public/assets/{_plugin-vue_export-helper-B7s-K_Aj.js → _plugin-vue_export-helper-BQRl7WAH.js} +1 -1
  50. package/src/ui/public/assets/{configStore-DPGao81A.js → configStore-BkxDLXGa.js} +1 -1
  51. package/src/ui/public/assets/element-plus-DNnePjco.js +32 -0
  52. package/src/ui/public/assets/{flow-mindmap-5SDymYbY.js → flow-mindmap-DRmzYE4F.js} +1 -1
  53. package/src/ui/public/assets/index-Br2VZQBT.js +68 -0
  54. package/src/ui/public/assets/{index-CWoujklW.css → index-CaIu73UM.css} +1 -1
  55. package/src/ui/public/assets/socket-io-pmu5tRPo.js +1 -0
  56. package/src/ui/public/assets/{vendor-BteTDIoK.js → vendor-CcK8n8T4.js} +44 -37
  57. package/src/ui/public/assets/vue-flow-pAz--378.js +39 -0
  58. package/src/ui/public/index.html +12 -12
  59. package/src/ui/public/assets/AppVersionBadge-DL0Y81fU.js +0 -2
  60. package/src/ui/public/assets/AppVersionBadge-Dj6H729N.css +0 -1
  61. package/src/ui/public/assets/BranchSelector-B_VMrSKv.js +0 -1
  62. package/src/ui/public/assets/BranchSelector-T_rJm-JY.css +0 -1
  63. package/src/ui/public/assets/CommitForm-BNuwp3ha.js +0 -6
  64. package/src/ui/public/assets/CommitForm-CUvl0VcF.css +0 -1
  65. package/src/ui/public/assets/CommonDialog-C2dbNSrw.js +0 -1
  66. package/src/ui/public/assets/CommonDialog-Cyh3xu0p.css +0 -1
  67. package/src/ui/public/assets/EditorView-BdTQsQw7.js +0 -0
  68. package/src/ui/public/assets/EditorView-CRYGVw_r.css +0 -1
  69. package/src/ui/public/assets/FlowExecutionViewer-B5kh7VxS.css +0 -1
  70. package/src/ui/public/assets/FlowExecutionViewer-D5DBcy8S.js +0 -1
  71. package/src/ui/public/assets/FlowOrchestrationWorkspace-BAQDU9Jd.css +0 -1
  72. package/src/ui/public/assets/FlowOrchestrationWorkspace-omHN8HaT.js +0 -21
  73. package/src/ui/public/assets/LogList-DJYpDUTZ.css +0 -1
  74. package/src/ui/public/assets/LogList-Dc1RMEFq.js +0 -7
  75. package/src/ui/public/assets/MindmapView-BPTh-XAf.js +0 -1
  76. package/src/ui/public/assets/MindmapView-CWoqfZRL.css +0 -1
  77. package/src/ui/public/assets/MonitorView-DxFogQIt.css +0 -1
  78. package/src/ui/public/assets/MonitorView-RTbnLZlO.js +0 -1
  79. package/src/ui/public/assets/ProjectStartupButton-B7zvqRj_.css +0 -1
  80. package/src/ui/public/assets/ProjectStartupButton-CcQ8SaPb.js +0 -1
  81. package/src/ui/public/assets/RemoteRepoCard-Cr1yQ4pF.js +0 -1
  82. package/src/ui/public/assets/RemoteRepoCard-d0klonVb.css +0 -1
  83. package/src/ui/public/assets/SourceMapView-BbiYmCHB.js +0 -3
  84. package/src/ui/public/assets/SourceMapView-DG_a_076.css +0 -1
  85. package/src/ui/public/assets/SvgIcon-D3jL_b3c.js +0 -1
  86. package/src/ui/public/assets/UserInputNode-DCk_cvaT.js +0 -1
  87. package/src/ui/public/assets/UserInputNode-DDtvv9Iq.css +0 -1
  88. package/src/ui/public/assets/WorkbenchView-KX3nKGZc.js +0 -12
  89. package/src/ui/public/assets/element-plus-BAcS68uS.js +0 -24
  90. package/src/ui/public/assets/index-B8oXZO4w.js +0 -38
  91. package/src/ui/public/assets/socket-io-BC2cap_-.js +0 -1
  92. package/src/ui/public/assets/vue-flow-CtZ_ZWKZ.js +0 -39
@@ -0,0 +1,253 @@
1
+ // Copyright 2026 xz333221
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // src/cli/ai/platformGuard.js 单元测试
16
+ // 覆盖纯函数(splitCommandSegments / extractCommandName / detectUnixOnlyCommands)
17
+ // 和异步主接口(guardCommand,注入 mock exec 避免 PATH 依赖)。
18
+ import { test, beforeEach } from 'node:test'
19
+ import assert from 'node:assert/strict'
20
+ import {
21
+ splitCommandSegments,
22
+ extractCommandName,
23
+ detectUnixOnlyCommands,
24
+ guardCommand,
25
+ checkCommandAvailability,
26
+ _clearAvailabilityCache,
27
+ UNIX_ONLY_COMMANDS,
28
+ } from './platformGuard.js'
29
+
30
+ // ── splitCommandSegments ──────────────────────
31
+
32
+ test('splitCommandSegments: 按管道切分', () => {
33
+ assert.deepEqual(
34
+ splitCommandSegments('npm test | tail -60'),
35
+ ['npm test', 'tail -60'],
36
+ )
37
+ })
38
+
39
+ test('splitCommandSegments: 按 && 切分', () => {
40
+ assert.deepEqual(
41
+ splitCommandSegments('echo a && echo b'),
42
+ ['echo a', 'echo b'],
43
+ )
44
+ })
45
+
46
+ test('splitCommandSegments: 按 || 和 ; 切分', () => {
47
+ assert.deepEqual(
48
+ splitCommandSegments('echo a || echo b ; echo c'),
49
+ ['echo a', 'echo b', 'echo c'],
50
+ )
51
+ })
52
+
53
+ test('splitCommandSegments: 空串/纯空白返回空数组', () => {
54
+ assert.deepEqual(splitCommandSegments(''), [])
55
+ assert.deepEqual(splitCommandSegments(' '), [])
56
+ })
57
+
58
+ test('splitCommandSegments: 无操作符的命令返回单段', () => {
59
+ assert.deepEqual(splitCommandSegments('echo hello'), ['echo hello'])
60
+ })
61
+
62
+ // ── extractCommandName ────────────────────────
63
+
64
+ test('extractCommandName: 提取第一个 token 并小写', () => {
65
+ assert.equal(extractCommandName('TAIL -60'), 'tail')
66
+ assert.equal(extractCommandName('node -e "code"'), 'node')
67
+ })
68
+
69
+ test('extractCommandName: 跳过 sudo/doas 前缀', () => {
70
+ assert.equal(extractCommandName('sudo tail -f log'), 'tail')
71
+ assert.equal(extractCommandName('doas grep pattern'), 'grep')
72
+ })
73
+
74
+ test('extractCommandName: 空串返回空', () => {
75
+ assert.equal(extractCommandName(''), '')
76
+ })
77
+
78
+ // ── detectUnixOnlyCommands ────────────────────
79
+
80
+ test('detectUnixOnlyCommands: 检测管道中的 Unix 命令', () => {
81
+ // 复现用户踩坑场景:npm test | tail -60
82
+ assert.deepEqual(detectUnixOnlyCommands('npm test | tail -60'), ['tail'])
83
+ })
84
+
85
+ test('detectUnixOnlyCommands: 检测多个不同的 Unix 命令', () => {
86
+ assert.deepEqual(
87
+ detectUnixOnlyCommands('cat file.txt | grep foo | wc -l'),
88
+ ['cat', 'grep', 'wc'],
89
+ )
90
+ })
91
+
92
+ test('detectUnixOnlyCommands: 同一命令去重', () => {
93
+ assert.deepEqual(
94
+ detectUnixOnlyCommands('grep a && grep b'),
95
+ ['grep'],
96
+ )
97
+ })
98
+
99
+ test('detectUnixOnlyCommands: 没有 Unix 命令时返回空数组', () => {
100
+ assert.deepEqual(detectUnixOnlyCommands('npm test'), [])
101
+ assert.deepEqual(detectUnixOnlyCommands('node -e "console.log(1)"'), [])
102
+ assert.deepEqual(detectUnixOnlyCommands('git status'), [])
103
+ })
104
+
105
+ test('detectUnixOnlyCommands: 带路径前缀的命令也能检测', () => {
106
+ // /usr/bin/tail → tail
107
+ assert.deepEqual(detectUnixOnlyCommands('/usr/bin/tail -f log'), ['tail'])
108
+ })
109
+
110
+ test('detectUnixOnlyCommands: 空输入返回空数组', () => {
111
+ assert.deepEqual(detectUnixOnlyCommands(''), [])
112
+ assert.deepEqual(detectUnixOnlyCommands(null), [])
113
+ assert.deepEqual(detectUnixOnlyCommands(undefined), [])
114
+ })
115
+
116
+ test('detectUnixOnlyCommands: 不误判 Windows 自带命令', () => {
117
+ // sort/find/where 在 Windows 上存在(行为不同但不是 Unix-only)
118
+ assert.deepEqual(detectUnixOnlyCommands('sort file.txt'), [])
119
+ assert.deepEqual(detectUnixOnlyCommands('where node'), [])
120
+ })
121
+
122
+ // ── guardCommand:非 Windows 平台 ─────────────
123
+
124
+ test('guardCommand: 非 Windows 平台直接放行', async () => {
125
+ const r = await guardCommand('tail -f log', { platform: 'linux' })
126
+ assert.equal(r.blocked, false)
127
+ assert.equal(r.reason, null)
128
+ })
129
+
130
+ test('guardCommand: macOS 也放行', async () => {
131
+ const r = await guardCommand('cat file | grep foo', { platform: 'darwin' })
132
+ assert.equal(r.blocked, false)
133
+ })
134
+
135
+ test('guardCommand: 空/非字符串命令放行', async () => {
136
+ assert.equal((await guardCommand('', { platform: 'win32' })).blocked, false)
137
+ assert.equal((await guardCommand(null, { platform: 'win32' })).blocked, false)
138
+ })
139
+
140
+ // ── guardCommand:Windows + mock exec ──────────
141
+ //
142
+ // 注入 mock execFn 模拟 `where` 的成功/失败,避免依赖真实 PATH。
143
+ // 每个测试前清空缓存,确保 mock 不互相污染。
144
+
145
+ // mock exec:commands 里列出的命令名返回"找到"(无 err),其余返回"找不到"(err)
146
+ function createMockExec(notFoundCommands = []) {
147
+ const notFound = new Set(notFoundCommands)
148
+ return (cmd, _opts, cb) => {
149
+ // 提取 `where <cmd>` 中的 cmd 名
150
+ const m = cmd.match(/^where\s+(\S+)/)
151
+ const name = m?.[1] || ''
152
+ if (notFound.has(name)) {
153
+ cb(new Error('not found'), '')
154
+ } else {
155
+ cb(null, `C:\\fake\\${name}.exe`)
156
+ }
157
+ }
158
+ }
159
+
160
+ beforeEach(() => {
161
+ _clearAvailabilityCache()
162
+ })
163
+
164
+ test('guardCommand: Windows 上 tail 不存在 → 拦截并给出替代方案', async () => {
165
+ const r = await guardCommand('npm test | tail -60', {
166
+ platform: 'win32',
167
+ execFn: createMockExec(['tail']),
168
+ })
169
+ assert.equal(r.blocked, true)
170
+ assert.match(r.reason, /tail/)
171
+ assert.match(r.reason, /Select-Object/)
172
+ assert.match(r.reason, /npm test \| tail -60/)
173
+ })
174
+
175
+ test('guardCommand: Windows 上 tail 存在(如 Git Bash) → 放行', async () => {
176
+ const r = await guardCommand('npm test | tail -60', {
177
+ platform: 'win32',
178
+ execFn: createMockExec([]), // 所有命令都"找到"
179
+ })
180
+ assert.equal(r.blocked, false)
181
+ })
182
+
183
+ test('guardCommand: Windows 上无 Unix 命令 → 放行', async () => {
184
+ const r = await guardCommand('npm test', {
185
+ platform: 'win32',
186
+ execFn: createMockExec(),
187
+ })
188
+ assert.equal(r.blocked, false)
189
+ })
190
+
191
+ test('guardCommand: 多个 Unix 命令都不存在 → 全部列出', async () => {
192
+ const r = await guardCommand('cat file | grep foo | wc -l', {
193
+ platform: 'win32',
194
+ execFn: createMockExec(['cat', 'grep', 'wc']),
195
+ })
196
+ assert.equal(r.blocked, true)
197
+ assert.match(r.reason, /cat/)
198
+ assert.match(r.reason, /grep/)
199
+ assert.match(r.reason, /wc/)
200
+ })
201
+
202
+ test('guardCommand: 多个 Unix 命令,部分存在部分不存在 → 只拦截不存在的', async () => {
203
+ // grep 存在(Git Bash),cat/wc 不存在
204
+ const r = await guardCommand('cat file | grep foo | wc -l', {
205
+ platform: 'win32',
206
+ execFn: createMockExec(['cat', 'wc']),
207
+ })
208
+ assert.equal(r.blocked, true)
209
+ assert.match(r.reason, /cat/)
210
+ assert.doesNotMatch(r.reason, /• grep/)
211
+ assert.match(r.reason, /wc/)
212
+ })
213
+
214
+ test('guardCommand: 拦截原因包含原始命令', async () => {
215
+ const cmd = 'echo hello | tail -5'
216
+ const r = await guardCommand(cmd, {
217
+ platform: 'win32',
218
+ execFn: createMockExec(['tail']),
219
+ })
220
+ assert.match(r.reason, new RegExp(cmd.replace(/\|/g, '\\|')))
221
+ })
222
+
223
+ // ── checkCommandAvailability:缓存行为 ────────
224
+
225
+ test('checkCommandAvailability: 缓存结果,第二次不调 exec', async () => {
226
+ let callCount = 0
227
+ const execFn = (cmd, _opts, cb) => {
228
+ callCount++
229
+ cb(new Error('not found'), '')
230
+ }
231
+ _clearAvailabilityCache()
232
+ const a1 = await checkCommandAvailability('tail', { execFn })
233
+ const a2 = await checkCommandAvailability('tail', { execFn })
234
+ assert.equal(a1, false)
235
+ assert.equal(a2, false)
236
+ assert.equal(callCount, 1, '第二次应命中缓存,不调 exec')
237
+ })
238
+
239
+ // ── 数据完整性 ────────────────────────────────
240
+
241
+ test('UNIX_ONLY_COMMANDS: 每个条目都有非空的替代方案说明', () => {
242
+ for (const [name, hint] of Object.entries(UNIX_ONLY_COMMANDS)) {
243
+ assert.ok(typeof name === 'string' && name.length > 0)
244
+ assert.ok(typeof hint === 'string' && hint.length > 10, `${name} 的替代方案说明过短`)
245
+ }
246
+ })
247
+
248
+ test('UNIX_ONLY_COMMANDS: 不包含 Windows 自带命令(防误杀)', () => {
249
+ const windowsBuiltins = ['sort', 'find', 'where', 'dir', 'type', 'copy', 'move', 'del']
250
+ for (const w of windowsBuiltins) {
251
+ assert.ok(!UNIX_ONLY_COMMANDS[w], `${w} 是 Windows 自带命令,不应在 Unix-only 清单中`)
252
+ }
253
+ })
@@ -0,0 +1,82 @@
1
+ // Copyright 2026 xz333221
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // MiniMax 系模型的 <think> 标签流式过滤器。
16
+ //
17
+ // 背景:MiniMax-M2/M3 等模型不走 reasoning_content 字段,而是把思考过程
18
+ // 以 <think>...</think> 标签**内联在 content 流**里推送,且标签本身会被
19
+ // SSE 分片切碎(如 '<thi' + 'nk>')。直接透传到终端会看到裸标签。
20
+ //
21
+ // 本模块提供一个增量状态机:feed() 接收任意分片的 content 字符串,
22
+ // 返回 [{content}|{thinking}] 段数组,调用方按类型分别渲染
23
+ // (content 正常显示,thinking 置灰)。标签本身被剥离,不进显示。
24
+ //
25
+ // 注意:这里只影响**终端显示**;写回对话历史的仍是模型原始 content
26
+ // (含标签),保证上下文对模型保真。
27
+
28
+ const OPEN = '<think>'
29
+ const CLOSE = '</think>'
30
+ // 保留尾部长度:开标签 7 字符 / 闭标签 8 字符,跨分片的半个标签不能提前吐出
31
+ const KEEP_OPEN = OPEN.length - 1 // 6
32
+ const KEEP_CLOSE = CLOSE.length - 1 // 7
33
+
34
+ /**
35
+ * 创建一个增量 think 标签过滤器。
36
+ * @returns {{ feed: (chunk: string) => Array<object>, flush: () => Array<object> }}
37
+ */
38
+ export function createThinkFilter() {
39
+ let mode = 'normal' // 'normal' | 'think'
40
+ let pending = ''
41
+
42
+ function feed(chunk) {
43
+ pending += String(chunk || '')
44
+ const out = []
45
+ while (pending.length > 0) {
46
+ const tag = mode === 'normal' ? OPEN : CLOSE
47
+ const keep = mode === 'normal' ? KEEP_OPEN : KEEP_CLOSE
48
+ const i = pending.indexOf(tag)
49
+ if (i === -1) {
50
+ // 没找到完整标签:吐出除"可能是半拉标签"的尾部以外的内容
51
+ const safe = pending.length - keep
52
+ if (safe > 0) {
53
+ out.push({ [mode === 'normal' ? 'content' : 'thinking']: pending.slice(0, safe) })
54
+ pending = pending.slice(safe)
55
+ }
56
+ break
57
+ }
58
+ if (i > 0) {
59
+ // 标签前有正文,先吐出
60
+ out.push({ [mode === 'normal' ? 'content' : 'thinking']: pending.slice(0, i) })
61
+ pending = pending.slice(i)
62
+ continue
63
+ }
64
+ // 命中完整标签:切换模式,吃掉标签
65
+ mode = mode === 'normal' ? 'think' : 'normal'
66
+ pending = pending.slice(tag.length)
67
+ }
68
+ return out
69
+ }
70
+
71
+ // 流结束时冲刷剩余 buffer(含未闭合的 think 块,按当前模式吐出)
72
+ function flush() {
73
+ if (!pending) return []
74
+ const seg = { [mode === 'normal' ? 'content' : 'thinking']: pending }
75
+ pending = ''
76
+ return [seg]
77
+ }
78
+
79
+ return { feed, flush }
80
+ }
81
+
82
+ export default { createThinkFilter }
@@ -0,0 +1,88 @@
1
+ // Copyright 2026 xz333221
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // src/cli/ai/streamFilter.js 单元测试。
16
+ // 重点覆盖 SSE 分片把 <think>/</think> 标签切碎的场景 —— 这正是
17
+ // MiniMax 模型裸标签问题的根源,整标签输入反而没啥可测的。
18
+ import { test } from 'node:test'
19
+ import assert from 'node:assert/strict'
20
+ import { createThinkFilter } from './streamFilter.js'
21
+
22
+ // 辅助:把一串分片喂给过滤器,返回渲染后的 {content, thinking} 全文
23
+ function runChunks(chunks) {
24
+ const f = createThinkFilter()
25
+ const segs = []
26
+ for (const c of chunks) segs.push(...f.feed(c))
27
+ segs.push(...f.flush())
28
+ return {
29
+ segs,
30
+ content: segs.map(s => s.content || '').join(''),
31
+ thinking: segs.map(s => s.thinking || '').join(''),
32
+ }
33
+ }
34
+
35
+ test('纯文本无标签原样通过', () => {
36
+ const r = runChunks(['你好,', '这是正文。'])
37
+ assert.equal(r.content, '你好,这是正文。')
38
+ assert.equal(r.thinking, '')
39
+ })
40
+
41
+ test('完整 think 块:标签剥离,内容归类 thinking', () => {
42
+ const r = runChunks(['<think>让我想想</think>答案是 42。'])
43
+ assert.equal(r.content, '答案是 42。')
44
+ assert.equal(r.thinking, '让我想想')
45
+ })
46
+
47
+ test('开标签被分片切碎(<thi + nk>)也能识别', () => {
48
+ const r = runChunks(['<thi', 'nk>思考中</thi', 'nk>正文'])
49
+ assert.equal(r.content, '正文')
50
+ assert.equal(r.thinking, '思考中')
51
+ })
52
+
53
+ test('闭标签跨 3 个分片也能识别', () => {
54
+ const r = runChunks(['<think>想</', 'th', 'ink>做完'])
55
+ assert.equal(r.content, '做完')
56
+ assert.equal(r.thinking, '想')
57
+ })
58
+
59
+ test('正文与 think 块交替多次', () => {
60
+ const r = runChunks(['先想<think>一</think>再说<think>二</think>完'])
61
+ assert.equal(r.content, '先想再说完')
62
+ assert.equal(r.thinking, '一二')
63
+ })
64
+
65
+ test('未闭合的 think 块在 flush 时按 thinking 吐出', () => {
66
+ const r = runChunks(['前置<think>想到一半被截断'])
67
+ assert.equal(r.content, '前置')
68
+ assert.equal(r.thinking, '想到一半被截断')
69
+ })
70
+
71
+ test('空 think 块不产生空段干扰', () => {
72
+ const r = runChunks(['<think></think>正文'])
73
+ assert.equal(r.content, '正文')
74
+ assert.equal(r.thinking, '')
75
+ })
76
+
77
+ test('逐字符投喂的极端分片', () => {
78
+ const full = '<think>深</think>答'
79
+ const r = runChunks([...full])
80
+ assert.equal(r.content, '答')
81
+ assert.equal(r.thinking, '深')
82
+ })
83
+
84
+ test('文末残留的半个开标签在 flush 时按正文吐出(不是标签)', () => {
85
+ const r = runChunks(['正常结尾<thi'])
86
+ assert.equal(r.content, '正常结尾<thi')
87
+ assert.equal(r.thinking, '')
88
+ })