@allbluecn/web-app 0.28.0 → 0.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/components/harness/__tests__/dock-registry.test.ts +43 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/__tests__/use-harness-chat-adapter.test.ts +20 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/use-agent-terminals.ts +116 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/use-harness-chat-adapter.ts +17 -5
- package/src/components/harness/conversation-panel/messages/harness-chain/use-harness-session.ts +4 -1
- package/src/components/harness/conversation-panel/messages/tools/__tests__/tool-run.test.ts +26 -1
- package/src/components/harness/conversation-panel/messages/tools/tool-run.ts +11 -0
- package/src/components/harness/extension-panel/dock/__tests__/tab-strip.test.tsx +45 -0
- package/src/components/harness/extension-panel/dock/dock-registry.ts +34 -0
- package/src/components/harness/extension-panel/dock/dock-surface.tsx +33 -0
- package/src/components/harness/extension-panel/dock/guide-page.tsx +4 -1
- package/src/components/harness/extension-panel/dock/tab-strip.tsx +9 -1
- package/src/components/harness/extension-panel/panels/terminal/__tests__/kind.test.tsx +219 -2
- package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx +322 -19
- package/src/components/harness/extension-panel/panels/terminal/__tests__/use-agent-terminal-roster.test.tsx +393 -0
- package/src/components/harness/extension-panel/panels/terminal/__tests__/ws-transport.test.ts +25 -0
- package/src/components/harness/extension-panel/panels/terminal/kind.tsx +55 -1
- package/src/components/harness/extension-panel/panels/terminal/shell-pref.ts +7 -4
- package/src/components/harness/extension-panel/panels/terminal/sse-transport.ts +26 -6
- package/src/components/harness/extension-panel/panels/terminal/tab-control-state.tsx +114 -0
- package/src/components/harness/extension-panel/panels/terminal/terminal-instance.tsx +190 -96
- package/src/components/harness/extension-panel/panels/terminal/terminal-theme.ts +113 -0
- package/src/components/harness/extension-panel/panels/terminal/transport.ts +28 -6
- package/src/components/harness/extension-panel/panels/terminal/use-agent-terminal-roster.ts +107 -0
- package/src/components/harness/extension-panel/panels/terminal/ws-transport.ts +7 -5
- package/src/components/harness/extension-panel/panels/terminal/xterm-terminal.tsx +5 -67
- package/src/lib/changelog/sdk-versions.ts +2 -2
- package/src/lib/queries/harness-terminal.ts +77 -0
- package/src/paraglide/messages/_index.js +48 -0
- package/src/paraglide/messages/en.js +15 -0
- package/src/paraglide/messages/zh.js +15 -0
- package/src/routes/api/harness/$.ts +19 -2
- package/src/routes/api/harness/terminal/$.ts +37 -6
- package/src/routes/api/harness/terminal/__tests__/route.test.ts +120 -0
- package/src/server/harness/__tests__/agent-terminal.server.test.ts +448 -0
- package/src/server/harness/__tests__/builtin-tools-invariant.test.ts +22 -12
- package/src/server/harness/__tests__/first-contact-fakes.test.ts +103 -3
- package/src/server/harness/__tests__/terminal-manager.server.test.ts +761 -2
- package/src/server/harness/agent-terminal.server.ts +631 -0
- package/src/server/harness/first-contact-fakes.ts +115 -4
- package/src/server/harness/host.server.ts +28 -1
- package/src/server/harness/persistence/__tests__/session-index-store.server.test.ts +83 -0
- package/src/server/harness/persistence/prisma-persistence.server.ts +12 -3
- package/src/server/harness/persistence/session-index-store.server.ts +26 -0
- package/src/server/harness/plugins/__tests__/assemble-disable.test.ts +25 -17
- package/src/server/harness/plugins/__tests__/tool-risk-audit.test.ts +16 -6
- package/src/server/harness/plugins/assemble-app.ts +12 -0
- package/src/server/harness/plugins/base-patch.ts +9 -2
- package/src/server/harness/plugins/builtin/__tests__/agent-terminal.plugin.test.ts +702 -0
- package/src/server/harness/plugins/builtin/agent-terminal.plugin.ts +518 -0
- package/src/server/harness/terminal-frame-builder.server.ts +5 -3
- package/src/server/harness/terminal-manager.server.ts +772 -35
- package/src/server/harness/terminal-ws-handler.server.ts +8 -2
- package/src/server/harness/tool-risk.ts +20 -4
- package/src/components/harness/extension-panel/panels/terminal/__tests__/xterm-webgl.test.ts +0 -68
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// T13 roster 消费 + dock 动态 reconcile 单测(设计 §7.2):
|
|
19
|
+
// - reconcileAgentTerminalTabs 纯函数四则(①开 ②关 ③control 翻转不关 ④user 终端不参与)
|
|
20
|
+
// + exited/消失细分与 tab 过滤边界;
|
|
21
|
+
// - useAgentTerminalRoster hook(threadId 就绪拉取解析 / 空态不发请求 / 失败静默);
|
|
22
|
+
// - wrapClientWithAgentTerminals 旁路事件(Opened/Closed → invalidate roster query,
|
|
23
|
+
// 无关事件不 invalidate,原事件照旧透传给 view);
|
|
24
|
+
// - DockSurface 挂载点冒烟(roster 到达 → openKind 开 agent tab / 消失 → closeTab)。
|
|
25
|
+
|
|
26
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
27
|
+
import { act, cleanup, render, renderHook, waitFor } from '@testing-library/react'
|
|
28
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
29
|
+
import { HotkeysProvider } from '@tanstack/react-hotkeys'
|
|
30
|
+
import type { SessionEvent } from '@tanstack/ai-harness'
|
|
31
|
+
import type { SessionViewSource } from '@tanstack/ai-harness/view'
|
|
32
|
+
|
|
33
|
+
// wrap 内部经 query-client-seam 单例 invalidate roster query——mock 拦截断言
|
|
34
|
+
//(真单例在此无 QueryClientProvider 上下文,mock 后 wrap 测试自洽)。
|
|
35
|
+
const invalidateSpy = vi.hoisted(() => vi.fn())
|
|
36
|
+
vi.mock('@/lib/query-client-seam', () => ({
|
|
37
|
+
getQueryClient: () => ({ invalidateQueries: invalidateSpy }),
|
|
38
|
+
}))
|
|
39
|
+
|
|
40
|
+
// DockSurface 挂载用例需要 terminal kind availability 为 'on'(vitest env 默认
|
|
41
|
+
// VITE_HARNESS_DEV='0' → hostEnabled=false → openKind 被拦),stub 门控为开。
|
|
42
|
+
vi.mock('@/lib/harness-gate', () => ({
|
|
43
|
+
isHarnessClientEnabled: () => true,
|
|
44
|
+
}))
|
|
45
|
+
// reconcile 开 tab 后 PaneTree 渲染 terminal kind → TerminalInstance 消费
|
|
46
|
+
// useThemeMode(无 provider 会抛)→ mock 成固定值(kind.test.tsx 同款)。
|
|
47
|
+
vi.mock('@/providers/theme-mode-provider', () => ({
|
|
48
|
+
useThemeMode: () => ({
|
|
49
|
+
mode: 'dark',
|
|
50
|
+
resolved: 'dark',
|
|
51
|
+
mounted: true,
|
|
52
|
+
setMode: () => {},
|
|
53
|
+
toggle: () => {},
|
|
54
|
+
}),
|
|
55
|
+
}))
|
|
56
|
+
|
|
57
|
+
import {
|
|
58
|
+
HARNESS_TERMINAL_ROSTER_QUERY_KEY,
|
|
59
|
+
harnessTerminalRosterQueryOptions,
|
|
60
|
+
type HarnessTerminalRosterEntry,
|
|
61
|
+
} from '@/lib/queries/harness-terminal'
|
|
62
|
+
import {
|
|
63
|
+
reconcileAgentTerminalTabs,
|
|
64
|
+
useAgentTerminalRoster,
|
|
65
|
+
} from '../use-agent-terminal-roster'
|
|
66
|
+
import { readAgentTerminalEvent, wrapClientWithAgentTerminals } from '@/components/harness/conversation-panel/messages/harness-chain/use-agent-terminals'
|
|
67
|
+
import { dockRegistry } from '@/components/harness/extension-panel/dock/dock-registry'
|
|
68
|
+
import { DockSurface } from '@/components/harness/extension-panel/dock/dock-surface'
|
|
69
|
+
import { appHotkeys } from '@/lib/hotkeys/registry'
|
|
70
|
+
|
|
71
|
+
// ── fixture ─────────────────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
/** roster 条目构造(缺省 = agent 出生 + agent 控制 + running)。 */
|
|
74
|
+
function rosterEntry(over: Partial<HarnessTerminalRosterEntry> & { id: string }): HarnessTerminalRosterEntry {
|
|
75
|
+
return {
|
|
76
|
+
title: 'zsh',
|
|
77
|
+
shell: '/bin/zsh',
|
|
78
|
+
cwd: '/tmp/ws',
|
|
79
|
+
cols: 80,
|
|
80
|
+
rows: 24,
|
|
81
|
+
state: 'running',
|
|
82
|
+
exitCode: null,
|
|
83
|
+
origin: 'agent',
|
|
84
|
+
control: 'agent',
|
|
85
|
+
...over,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** dock tab 构造(terminal kind + instanceId 形态,与 openKind 产出一致)。 */
|
|
90
|
+
function terminalTab(id: string, instanceId: string) {
|
|
91
|
+
return { id, kind: 'terminal', title: 'zsh', instanceId }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 构造一条 CUSTOM harness.plugin.event 信封(shape 对齐 toolcall-diff / use-file-changes 口径)。 */
|
|
95
|
+
function pluginEnvelope(name: string, value: Record<string, unknown>): SessionEvent {
|
|
96
|
+
return {
|
|
97
|
+
event: {
|
|
98
|
+
type: 'CUSTOM',
|
|
99
|
+
name: 'harness.plugin.event',
|
|
100
|
+
value: { plugin: 'allblue/agent-terminal', name, value },
|
|
101
|
+
timestamp: Date.now(),
|
|
102
|
+
},
|
|
103
|
+
} as unknown as SessionEvent
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 最小 SessionViewSource fake:按序 yield 事件(收窄到 events() 面即可)。 */
|
|
107
|
+
function fakeSource(events: SessionEvent[]): SessionViewSource {
|
|
108
|
+
return {
|
|
109
|
+
events: async function* () {
|
|
110
|
+
for (const e of events) yield e
|
|
111
|
+
},
|
|
112
|
+
} as unknown as SessionViewSource
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 收干事件流(驱动 wrap 副作用执行)。 */
|
|
116
|
+
async function drain(source: SessionViewSource): Promise<SessionEvent[]> {
|
|
117
|
+
const seen: SessionEvent[] = []
|
|
118
|
+
for await (const entry of source.events({})) seen.push(entry)
|
|
119
|
+
return seen
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── reconcile 纯函数 ────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
describe('reconcileAgentTerminalTabs(§7.2 规则)', () => {
|
|
125
|
+
it('① roster agent+running 且无对应 tab → open 指令(instanceId+title 取 roster title)', () => {
|
|
126
|
+
const plan = reconcileAgentTerminalTabs(
|
|
127
|
+
// term-other 已有 tab(保持零指令),term-1 未开(产生 open)
|
|
128
|
+
[rosterEntry({ id: 'term-other' }), rosterEntry({ id: 'term-1', title: 'build-watch' })],
|
|
129
|
+
[terminalTab('terminal-9', 'term-other')],
|
|
130
|
+
)
|
|
131
|
+
expect(plan.open).toEqual([{ instanceId: 'term-1', title: 'build-watch' }])
|
|
132
|
+
expect(plan.close).toEqual([])
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('② roster 消失的 agent 终端(此前有 tab)→ close 指令(tabId)', () => {
|
|
136
|
+
const plan = reconcileAgentTerminalTabs(
|
|
137
|
+
// term-gone 不在 roster(kill/exit 后 listAll 移除);term-alive 在册 running 保持
|
|
138
|
+
[rosterEntry({ id: 'term-alive' })],
|
|
139
|
+
[terminalTab('terminal-3', 'term-gone'), terminalTab('terminal-4', 'term-alive')],
|
|
140
|
+
)
|
|
141
|
+
expect(plan.close).toEqual(['terminal-3'])
|
|
142
|
+
expect(plan.open).toEqual([])
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('③ control 翻转(agent→user)同 id 同 state → 零指令(reconcile 不看 control)', () => {
|
|
146
|
+
const plan = reconcileAgentTerminalTabs(
|
|
147
|
+
[rosterEntry({ id: 'term-1', control: 'user' })],
|
|
148
|
+
[terminalTab('terminal-9', 'term-1')],
|
|
149
|
+
)
|
|
150
|
+
expect(plan.open).toEqual([])
|
|
151
|
+
expect(plan.close).toEqual([])
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('④ user origin 终端不参与 reconcile:running 无 tab 不开;在册 exited 有 tab 不关', () => {
|
|
155
|
+
const plan = reconcileAgentTerminalTabs(
|
|
156
|
+
[
|
|
157
|
+
rosterEntry({ id: 'term-user', origin: 'user', control: 'user' }),
|
|
158
|
+
rosterEntry({ id: 'term-user-2', origin: 'user', control: 'user', state: 'exited', exitCode: 0 }),
|
|
159
|
+
],
|
|
160
|
+
[terminalTab('terminal-7', 'term-user-2')],
|
|
161
|
+
)
|
|
162
|
+
// user running 无 tab → 不 open;user exited 有 tab → 不关(用户终端完全不参与)
|
|
163
|
+
expect(plan.open).toEqual([])
|
|
164
|
+
expect(plan.close).toEqual([])
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('agent 终端在册但已 exited → 关对应 tab(退出即视为消失,§7.2「agent 关闭/退出」)', () => {
|
|
168
|
+
const plan = reconcileAgentTerminalTabs(
|
|
169
|
+
[rosterEntry({ id: 'term-dead', state: 'exited', exitCode: 1 })],
|
|
170
|
+
[terminalTab('terminal-5', 'term-dead')],
|
|
171
|
+
)
|
|
172
|
+
expect(plan.close).toEqual(['terminal-5'])
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('非 terminal kind / 无 instanceId 的 tab 不参与 close', () => {
|
|
176
|
+
const plan = reconcileAgentTerminalTabs(
|
|
177
|
+
[],
|
|
178
|
+
[
|
|
179
|
+
{ id: 'guide-1', kind: 'guide', title: 'Guide' },
|
|
180
|
+
{ id: 'terminal-2', kind: 'terminal', title: 'shell' }, // 用户 tab:openKind 不传 instanceId
|
|
181
|
+
],
|
|
182
|
+
)
|
|
183
|
+
expect(plan.close).toEqual([])
|
|
184
|
+
expect(plan.open).toEqual([])
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('空 roster + 无 tab → 空计划(no-op)', () => {
|
|
188
|
+
const plan = reconcileAgentTerminalTabs([], [])
|
|
189
|
+
expect(plan.open).toEqual([])
|
|
190
|
+
expect(plan.close).toEqual([])
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// ── roster query hook ───────────────────────────────────────────────────────
|
|
195
|
+
|
|
196
|
+
describe('useAgentTerminalRoster(query hook)', () => {
|
|
197
|
+
beforeEach(() => {
|
|
198
|
+
vi.unstubAllGlobals()
|
|
199
|
+
})
|
|
200
|
+
afterEach(() => {
|
|
201
|
+
cleanup()
|
|
202
|
+
vi.unstubAllGlobals()
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
function makeWrapper() {
|
|
206
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
|
207
|
+
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
208
|
+
<QueryClientProvider client={qc}>{children}</QueryClientProvider>
|
|
209
|
+
)
|
|
210
|
+
return { qc, Wrapper }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
it('threadId 就绪 → GET roster 并解析 terminals', async () => {
|
|
214
|
+
const fetchMock = vi.fn(async () =>
|
|
215
|
+
new Response(JSON.stringify({ terminals: [rosterEntry({ id: 'term-1' })] }), { status: 200 }),
|
|
216
|
+
)
|
|
217
|
+
vi.stubGlobal('fetch', fetchMock)
|
|
218
|
+
const { Wrapper } = makeWrapper()
|
|
219
|
+
const { result } = renderHook(() => useAgentTerminalRoster('t1'), { wrapper: Wrapper })
|
|
220
|
+
await waitFor(() => expect(result.current.data?.terminals).toHaveLength(1))
|
|
221
|
+
expect(result.current.data?.terminals[0]?.id).toBe('term-1')
|
|
222
|
+
expect(fetchMock).toHaveBeenCalledWith(
|
|
223
|
+
expect.stringContaining('/api/harness/terminal/roster?threadId=t1'),
|
|
224
|
+
)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
it('threadId 空 → query 不启用、零请求', async () => {
|
|
228
|
+
const fetchMock = vi.fn()
|
|
229
|
+
vi.stubGlobal('fetch', fetchMock)
|
|
230
|
+
const { Wrapper } = makeWrapper()
|
|
231
|
+
const { result } = renderHook(() => useAgentTerminalRoster(null), { wrapper: Wrapper })
|
|
232
|
+
await act(async () => {
|
|
233
|
+
await Promise.resolve()
|
|
234
|
+
})
|
|
235
|
+
expect(fetchMock).not.toHaveBeenCalled()
|
|
236
|
+
expect(result.current.fetchStatus).toBe('idle')
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('端点失败(404 gate 关)→ 静默进 error 态,不 throw 到调用方', async () => {
|
|
240
|
+
const fetchMock = vi.fn(async () => new Response('Not Found', { status: 404 }))
|
|
241
|
+
vi.stubGlobal('fetch', fetchMock)
|
|
242
|
+
const { Wrapper } = makeWrapper()
|
|
243
|
+
const { result } = renderHook(() => useAgentTerminalRoster('t1'), { wrapper: Wrapper })
|
|
244
|
+
await waitFor(() => expect(result.current.status).toBe('error'))
|
|
245
|
+
expect(result.current.data).toBeUndefined()
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('queryOptions 工厂:key 逐字 = [harnessTerminalRoster, threadId](对账)', () => {
|
|
249
|
+
const opts = harnessTerminalRosterQueryOptions('t9')
|
|
250
|
+
expect(opts.queryKey).toEqual(['harnessTerminalRoster', 't9'])
|
|
251
|
+
expect(opts.queryKey[0]).toBe(HARNESS_TERMINAL_ROSTER_QUERY_KEY)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
// ── 旁路事件 wrap(harness-chain/use-agent-terminals)────────────────────────
|
|
256
|
+
|
|
257
|
+
describe('wrapClientWithAgentTerminals(旁路事件 invalidate)', () => {
|
|
258
|
+
beforeEach(() => {
|
|
259
|
+
invalidateSpy.mockClear()
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('④ AgentTerminalOpened 事件 → invalidate roster query(key 前缀)', async () => {
|
|
263
|
+
const wrapped = wrapClientWithAgentTerminals(
|
|
264
|
+
fakeSource([pluginEnvelope('allblue/agent-terminal:opened', { terminalId: 'term-1', title: 'zsh' })]),
|
|
265
|
+
)
|
|
266
|
+
await drain(wrapped!)
|
|
267
|
+
expect(invalidateSpy).toHaveBeenCalledTimes(1)
|
|
268
|
+
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: [HARNESS_TERMINAL_ROSTER_QUERY_KEY] })
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('④ AgentTerminalClosed 事件 → 同样 invalidate', async () => {
|
|
272
|
+
const wrapped = wrapClientWithAgentTerminals(
|
|
273
|
+
fakeSource([pluginEnvelope('allblue/agent-terminal:closed', { terminalId: 'term-1' })]),
|
|
274
|
+
)
|
|
275
|
+
await drain(wrapped!)
|
|
276
|
+
expect(invalidateSpy).toHaveBeenCalledTimes(1)
|
|
277
|
+
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: [HARNESS_TERMINAL_ROSTER_QUERY_KEY] })
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
it('无关事件(FileChanged / 其他 CUSTOM)→ 不 invalidate', async () => {
|
|
281
|
+
const wrapped = wrapClientWithAgentTerminals(
|
|
282
|
+
fakeSource([
|
|
283
|
+
pluginEnvelope('FileChanged', { path: '/a.ts', change: 'updated' }),
|
|
284
|
+
{ event: { type: 'TEXT_MESSAGE_START', timestamp: Date.now() } } as unknown as SessionEvent,
|
|
285
|
+
]),
|
|
286
|
+
)
|
|
287
|
+
await drain(wrapped!)
|
|
288
|
+
expect(invalidateSpy).not.toHaveBeenCalled()
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
it('原事件照旧透传给 view(wrap 不吞事件)', async () => {
|
|
292
|
+
const opened = pluginEnvelope('allblue/agent-terminal:opened', { terminalId: 'term-1', title: 'zsh' })
|
|
293
|
+
const wrapped = wrapClientWithAgentTerminals(fakeSource([opened]))
|
|
294
|
+
const seen = await drain(wrapped!)
|
|
295
|
+
expect(seen).toHaveLength(1)
|
|
296
|
+
expect(seen[0]).toBe(opened)
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
it('source 为 null(空态)→ 原样返回 null', () => {
|
|
300
|
+
expect(wrapClientWithAgentTerminals(null)).toBeNull()
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it('readAgentTerminalEvent:结构守卫(非法形状 → null,合法 → 判别联合)', () => {
|
|
304
|
+
expect(
|
|
305
|
+
readAgentTerminalEvent(pluginEnvelope('allblue/agent-terminal:opened', { terminalId: 'x', title: 't' }).event),
|
|
306
|
+
).toEqual({ kind: 'opened', terminalId: 'x', title: 't' })
|
|
307
|
+
expect(
|
|
308
|
+
readAgentTerminalEvent(pluginEnvelope('allblue/agent-terminal:closed', { terminalId: 'x' }).event),
|
|
309
|
+
).toEqual({ kind: 'closed', terminalId: 'x' })
|
|
310
|
+
// opened 缺 terminalId / closed 缺 terminalId → fail-closed
|
|
311
|
+
expect(
|
|
312
|
+
readAgentTerminalEvent(pluginEnvelope('allblue/agent-terminal:opened', { title: 't' }).event),
|
|
313
|
+
).toBeNull()
|
|
314
|
+
expect(readAgentTerminalEvent(pluginEnvelope('allblue/agent-terminal:closed', {}).event)).toBeNull()
|
|
315
|
+
// 非 CUSTOM / 非插件信封 / 非终端事件 → null
|
|
316
|
+
expect(readAgentTerminalEvent({ type: 'TEXT_MESSAGE_START' } as unknown as SessionEvent['event'])).toBeNull()
|
|
317
|
+
expect(
|
|
318
|
+
readAgentTerminalEvent(pluginEnvelope('FileChanged', { path: '/a.ts', change: 'updated' }).event),
|
|
319
|
+
).toBeNull()
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
// ── DockSurface 挂载点冒烟(reconcile 消费执行)──────────────────────────────
|
|
324
|
+
|
|
325
|
+
describe('DockSurface reconcile 挂载(roster → dock 开合 tab)', () => {
|
|
326
|
+
// fetch stub 共享的 roster 响应体(每用例改写;staleTime 0 → 挂载即重取,
|
|
327
|
+
// stub 必须返回 roster 载荷而非任意 JSON,否则 data.terminals 被 covers 掉)
|
|
328
|
+
let rosterResponse: { terminals: HarnessTerminalRosterEntry[] }
|
|
329
|
+
|
|
330
|
+
beforeEach(() => {
|
|
331
|
+
window.localStorage.clear()
|
|
332
|
+
rosterResponse = { terminals: [] }
|
|
333
|
+
// onTabClose 会发 kill 请求(关 agent tab 场景)+ roster query 挂载重取——同一 stub
|
|
334
|
+
vi.stubGlobal(
|
|
335
|
+
'fetch',
|
|
336
|
+
vi.fn(async () => new Response(JSON.stringify(rosterResponse), { status: 200 })),
|
|
337
|
+
)
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
afterEach(() => {
|
|
341
|
+
cleanup()
|
|
342
|
+
vi.unstubAllGlobals()
|
|
343
|
+
// 回落空线程布局,防串染其他用例
|
|
344
|
+
act(() => {
|
|
345
|
+
dockRegistry.switchThread(null)
|
|
346
|
+
})
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
function renderSurface(threadId: string, terminals: HarnessTerminalRosterEntry[]) {
|
|
350
|
+
rosterResponse = { terminals }
|
|
351
|
+
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
|
352
|
+
qc.setQueryData(['harnessTerminalRoster', threadId], { terminals })
|
|
353
|
+
return render(
|
|
354
|
+
<QueryClientProvider client={qc}>
|
|
355
|
+
<HotkeysProvider appHotkeys={appHotkeys}>
|
|
356
|
+
<DockSurface mediaThreadId={threadId} panelExpanded onSetPanelExpanded={() => {}} />
|
|
357
|
+
</HotkeysProvider>
|
|
358
|
+
</QueryClientProvider>,
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
it('roster 到达:agent running 终端 → openKind 自动开 tab(instanceId 携带)', async () => {
|
|
363
|
+
act(() => {
|
|
364
|
+
dockRegistry.switchThread('t-recon-open')
|
|
365
|
+
})
|
|
366
|
+
renderSurface('t-recon-open', [rosterEntry({ id: 'term-1', title: 'build-watch' })])
|
|
367
|
+
await waitFor(() => {
|
|
368
|
+
const tabs = Object.values(dockRegistry.getState().tabs)
|
|
369
|
+
expect(tabs.some((t) => t.kind === 'terminal' && t.instanceId === 'term-1')).toBe(true)
|
|
370
|
+
})
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
it('roster 消失的 agent 终端:对应 tab 被关闭', async () => {
|
|
374
|
+
act(() => {
|
|
375
|
+
dockRegistry.switchThread('t-recon-close')
|
|
376
|
+
})
|
|
377
|
+
// 直接种一个带 instanceId 的 agent tab(openTab 兼容入口——本测试文件的 registry
|
|
378
|
+
// 实例未注册 terminal kind,openKind 会在 kind 查表处拦截;reconcile 关闭路径
|
|
379
|
+
// 只按 kind 字段 + instanceId 匹配,与 tab 来源无关)
|
|
380
|
+
act(() => {
|
|
381
|
+
dockRegistry.openTab({ id: 'terminal-x', kind: 'terminal', title: 'zsh', instanceId: 'term-gone' })
|
|
382
|
+
})
|
|
383
|
+
expect(
|
|
384
|
+
Object.values(dockRegistry.getState().tabs).some((t) => t.instanceId === 'term-gone'),
|
|
385
|
+
).toBe(true)
|
|
386
|
+
renderSurface('t-recon-close', [])
|
|
387
|
+
await waitFor(() => {
|
|
388
|
+
expect(
|
|
389
|
+
Object.values(dockRegistry.getState().tabs).some((t) => t.instanceId === 'term-gone'),
|
|
390
|
+
).toBe(false)
|
|
391
|
+
})
|
|
392
|
+
})
|
|
393
|
+
})
|
package/src/components/harness/extension-panel/panels/terminal/__tests__/ws-transport.test.ts
CHANGED
|
@@ -153,6 +153,31 @@ describe('createWsTransport', () => {
|
|
|
153
153
|
t.close()
|
|
154
154
|
})
|
|
155
155
|
|
|
156
|
+
// 任务 5 typed 透传(管道,手势识别在 T12):write 帧携带 typed 字段原样上行;
|
|
157
|
+
// 不带 typed 时帧无 typed 键(wire 精简,不发 undefined)。
|
|
158
|
+
it('send write 帧携带 typed 字段;缺省 typed 时无该键', async () => {
|
|
159
|
+
vi.useFakeTimers()
|
|
160
|
+
const { createWsTransport } = await import('../ws-transport')
|
|
161
|
+
const t = createWsTransport({ threadId: 'th1' })
|
|
162
|
+
t.connect('term1')
|
|
163
|
+
|
|
164
|
+
const ws = instances[0]!
|
|
165
|
+
ws.__open()
|
|
166
|
+
|
|
167
|
+
t.send({ type: 'write', sequence: 3, data: 'hi', typed: true })
|
|
168
|
+
expect(ws.sent).toHaveLength(1)
|
|
169
|
+
const sent = JSON.parse(ws.sent[0]!) as { type: string; seq: number; data: string; typed?: boolean }
|
|
170
|
+
expect(sent.type).toBe('write')
|
|
171
|
+
expect(sent.seq).toBe(3)
|
|
172
|
+
expect(sent.data).toBe('hi')
|
|
173
|
+
expect(sent.typed).toBe(true)
|
|
174
|
+
|
|
175
|
+
t.send({ type: 'write', sequence: 4, data: 'yo' })
|
|
176
|
+
const sent2 = JSON.parse(ws.sent[1]!) as { typed?: boolean }
|
|
177
|
+
expect('typed' in sent2).toBe(false)
|
|
178
|
+
t.close()
|
|
179
|
+
})
|
|
180
|
+
|
|
156
181
|
it('scheduleAck 空实现(WS 模式无 ack 帧)', async () => {
|
|
157
182
|
const { createWsTransport } = await import('../ws-transport')
|
|
158
183
|
const t = createWsTransport({ threadId: 'th1' })
|
|
@@ -26,8 +26,9 @@
|
|
|
26
26
|
|
|
27
27
|
import { SquareTerminal } from "lucide-react"
|
|
28
28
|
import { TerminalInstance } from "./terminal-instance"
|
|
29
|
+
import { createTerminalControlStore, TerminalTabTitle } from "./tab-control-state"
|
|
29
30
|
import type { DockTab } from "@/components/harness/extension-panel/dock/dock-layout"
|
|
30
|
-
import type
|
|
31
|
+
import { dockRegistry, type DockPanelKind } from "@/components/harness/extension-panel/dock/dock-registry"
|
|
31
32
|
|
|
32
33
|
export interface TerminalKindDeps {
|
|
33
34
|
title: string
|
|
@@ -37,7 +38,14 @@ export interface TerminalKindDeps {
|
|
|
37
38
|
description: string
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
/** binding localStorage 键前缀(与 terminal-instance.tsx 的 BINDING_PREFIX 同源,勿单方改名)。 */
|
|
42
|
+
const TERMINAL_BINDING_PREFIX = "allblue.harness.terminal-binding.v1"
|
|
43
|
+
|
|
40
44
|
export function createTerminalKind(deps: TerminalKindDeps): DockPanelKind {
|
|
45
|
+
// 控制权轻量注册表(T14):tabId → 最近 state 帧 {origin, control}。TerminalInstance
|
|
46
|
+
// onControlStateChange 写入;renderTabTitle 的 TerminalTabTitle 自订阅(tab-strip 零
|
|
47
|
+
// 感知)。见 tab-control-state.tsx 文件头的设计取舍。
|
|
48
|
+
const controlStore = createTerminalControlStore()
|
|
41
49
|
return {
|
|
42
50
|
kind: "terminal",
|
|
43
51
|
title: deps.title,
|
|
@@ -55,10 +63,56 @@ export function createTerminalKind(deps: TerminalKindDeps): DockPanelKind {
|
|
|
55
63
|
dropdown: "shell",
|
|
56
64
|
},
|
|
57
65
|
availability: ({ hostEnabled }) => (hostEnabled ? "on" : "disabled"),
|
|
66
|
+
// 关 tab → 主动杀 PTY 进程 + 清 binding(DSH 对标;服务端 idle 回收兜底 kill 失败)。
|
|
67
|
+
// threadId 未就绪(ref 空)直接跳过:kill URL 缺 ?threadId= 服务端 400,清了也无从 kill。
|
|
68
|
+
onTabClose: (tab: DockTab) => {
|
|
69
|
+
// 控制权 entry 一并清(T14):防同 tabId 复用(多实例 id 自增不复用,防御性)+ 泄漏。
|
|
70
|
+
controlStore.clear(tab.id)
|
|
71
|
+
const threadId = deps.threadIdRef.current
|
|
72
|
+
if (!threadId) return
|
|
73
|
+
const key = `${TERMINAL_BINDING_PREFIX}.${threadId}.${tab.id}`
|
|
74
|
+
// terminalId:优先 tab.instanceId(openKind 显式携带),回落 binding localStorage
|
|
75
|
+
// (现网 openKind 不传 instanceId,binding 是主路径)。
|
|
76
|
+
let terminalId = tab.instanceId ?? null
|
|
77
|
+
if (!terminalId) {
|
|
78
|
+
try {
|
|
79
|
+
terminalId = window.localStorage.getItem(key)
|
|
80
|
+
} catch {
|
|
81
|
+
terminalId = null
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// binding 必须清(与 kill 成败无关)——防重挂载/恢复布局时 attach 到已关实例。
|
|
85
|
+
try {
|
|
86
|
+
window.localStorage.removeItem(key)
|
|
87
|
+
} catch {
|
|
88
|
+
/* localStorage 不可用(隐私模式)→ 忽略 */
|
|
89
|
+
}
|
|
90
|
+
if (!terminalId) return
|
|
91
|
+
// 不 await:关闭即返回不等网络;失败静默(idle 回收兜底)。keepalive 保证关 tab 后
|
|
92
|
+
// 整页卸载场景请求仍能发出。
|
|
93
|
+
void fetch(
|
|
94
|
+
`/api/harness/terminal/${encodeURIComponent(terminalId)}/kill?threadId=${encodeURIComponent(threadId)}`,
|
|
95
|
+
{ method: "POST", keepalive: true },
|
|
96
|
+
).catch(() => {
|
|
97
|
+
/* kill 失败静默:进程残留由服务端 idle 回收兜底 */
|
|
98
|
+
})
|
|
99
|
+
},
|
|
100
|
+
// T14 tab 标题:TerminalTabTitle 自订阅 controlStore——state 帧到达徽章自行出现/
|
|
101
|
+
// 消失(tab-strip 只渲染返回的 ReactNode,无需重渲染整个 strip)。
|
|
102
|
+
renderTabTitle: (tab: DockTab) => <TerminalTabTitle tab={tab} store={controlStore} />,
|
|
58
103
|
render: (tab: DockTab) => (
|
|
59
104
|
<TerminalInstance
|
|
60
105
|
threadId={deps.threadIdRef.current ?? ""}
|
|
61
106
|
tabId={tab.id}
|
|
107
|
+
// T14 instanceId 优先 attach:reconcile 开的 agent tab(带 instanceId)直接
|
|
108
|
+
// attach 该终端,跳过 binding/create;用户 tab 无 instanceId(T13 裁决不变量:
|
|
109
|
+
// 带 instanceId ⟺ agent 终端 tab),走原 binding → create 链。
|
|
110
|
+
initialTerminalId={tab.instanceId}
|
|
111
|
+
onControlStateChange={(s) => controlStore.set(tab.id, s)}
|
|
112
|
+
// T14-I1 reopen 接缝:agent tab 重开后回归「用户 tab 无 instanceId」——组件内
|
|
113
|
+
// 只能清自身覆盖,tab 元数据的清除归 kind(与 onTabClose 元数据清理同责)。
|
|
114
|
+
// 残留死 agent id 会让 T13 reconcile 强制关掉刚重开的新终端。
|
|
115
|
+
onReopen={() => dockRegistry.clearTabInstanceId(tab.id)}
|
|
62
116
|
/>
|
|
63
117
|
),
|
|
64
118
|
}
|
|
@@ -21,12 +21,15 @@
|
|
|
21
21
|
|
|
22
22
|
const TERMINAL_SHELL_PREF_KEY = "allblue.harness.terminal.shell"
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
24
|
+
/** 默认 shell(用户裁决:新建终端默认 zsh,无偏好/未选时统一 zsh)。 */
|
|
25
|
+
const DEFAULT_SHELL = "zsh"
|
|
26
|
+
|
|
27
|
+
/** 读取终端 shell 偏好(无偏好 → 默认 zsh;create 与 shell 下拉选中显示共用)。 */
|
|
28
|
+
export function readTerminalShellPref(): string {
|
|
26
29
|
try {
|
|
27
|
-
return window.localStorage.getItem(TERMINAL_SHELL_PREF_KEY)
|
|
30
|
+
return window.localStorage.getItem(TERMINAL_SHELL_PREF_KEY) ?? DEFAULT_SHELL
|
|
28
31
|
} catch {
|
|
29
|
-
return
|
|
32
|
+
return DEFAULT_SHELL
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
35
|
|
|
@@ -69,16 +69,34 @@ export function createSseTransport(options: TerminalTransportOptions): TerminalT
|
|
|
69
69
|
| null
|
|
70
70
|
return { ok: false, error: errBody?.message ?? null, errorCode: errBody?.error }
|
|
71
71
|
}
|
|
72
|
-
const data = (await res.json()) as { terminalId: string; shell: string }
|
|
73
|
-
return { ok: true, terminalId: data.terminalId, shell: data.shell }
|
|
72
|
+
const data = (await res.json()) as { terminalId: string; shell: string; serverEpoch?: string }
|
|
73
|
+
return { ok: true, terminalId: data.terminalId, shell: data.shell, serverEpoch: data.serverEpoch }
|
|
74
74
|
} catch {
|
|
75
75
|
return { ok: false, error: null }
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
78
|
|
|
79
|
-
connect(termId):
|
|
79
|
+
async connect(termId): Promise<{ ok: boolean; notFound?: boolean; serverEpoch?: string }> {
|
|
80
80
|
terminalId = termId
|
|
81
|
-
const
|
|
81
|
+
const eventUrl = apiBase(`${termId}/events?threadId=${encodeURIComponent(threadId)}`)
|
|
82
|
+
// EventSource 读不到 HTTP 状态码(404/503 一律 onerror)——先探测一次区分
|
|
83
|
+
// 「终端已不存在」(死 binding/instanceId,服务端重启或 idle 回收后)与普通网络断。
|
|
84
|
+
// 404 → 返回 notFound + serverEpoch(方案 C:客户端比对 binding 存的代际区分重启/回收)
|
|
85
|
+
// 让 instance 走 create 重建;否则开 EventSource 正常下行。
|
|
86
|
+
try {
|
|
87
|
+
const probe = await fetch(eventUrl)
|
|
88
|
+
if (probe.status === 404) {
|
|
89
|
+
terminalId = null
|
|
90
|
+
const body = (await probe.json().catch(() => null)) as { serverEpoch?: string } | null
|
|
91
|
+
return { ok: false, notFound: true, serverEpoch: body?.serverEpoch }
|
|
92
|
+
}
|
|
93
|
+
// 非 404(200 SSE 流 / 401 / 429 等)→ 交由 EventSource 处理;探测响应体不消费,
|
|
94
|
+
// 直接取消(EventSource 会重新发起真正的流连接)。
|
|
95
|
+
probe.body?.cancel().catch(() => {})
|
|
96
|
+
} catch {
|
|
97
|
+
// 探测网络异常 → 仍开 EventSource,由其 onerror 呈现断线态(不误判 notFound)
|
|
98
|
+
}
|
|
99
|
+
const source = new EventSource(eventUrl)
|
|
82
100
|
es = source
|
|
83
101
|
source.onmessage = (ev) => {
|
|
84
102
|
try {
|
|
@@ -96,6 +114,7 @@ export function createSseTransport(options: TerminalTransportOptions): TerminalT
|
|
|
96
114
|
if (es === source) es = null
|
|
97
115
|
errorCb?.()
|
|
98
116
|
}
|
|
117
|
+
return { ok: true }
|
|
99
118
|
},
|
|
100
119
|
|
|
101
120
|
onFrame(cb): void {
|
|
@@ -108,9 +127,10 @@ export function createSseTransport(options: TerminalTransportOptions): TerminalT
|
|
|
108
127
|
|
|
109
128
|
send(cmd: TerminalTransportCmd): void {
|
|
110
129
|
if (!terminalId) return
|
|
111
|
-
// 上行 wire 格式:write → { writes: [{ seq, data }] }
|
|
130
|
+
// 上行 wire 格式:write → { writes: [{ seq, data, typed? }] }(typed 缺省不发键,
|
|
131
|
+
// wire 精简);resize → { resize: { seq, cols, rows } }
|
|
112
132
|
const body = cmd.type === "write"
|
|
113
|
-
? { writes: [{ seq: cmd.sequence, data: cmd.data }] }
|
|
133
|
+
? { writes: [{ seq: cmd.sequence, data: cmd.data, ...(cmd.typed !== undefined ? { typed: cmd.typed } : {}) }] }
|
|
114
134
|
: { resize: { seq: cmd.sequence, cols: cmd.cols!, rows: cmd.rows! } }
|
|
115
135
|
// Promise 链串行保序(防 write/resize 请求乱序触发服务端 invalid_sequence 409)
|
|
116
136
|
inputChain = inputChain.then(async () => {
|