Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(shell): 切换器增强——断开动作/重连倒计时/凭据类型/活动标识

Menghuan19188525c5643d41
diff --git a/src/i18n/locales/en-US.json b/src/i18n/locales/en-US.json
index 716f3b51..a621dbb2 100644
--- a/src/i18n/locales/en-US.json
+++ b/src/i18n/locales/en-US.json
@@ -355,6 +355,11 @@
"remote.local": "Local",
"remote.manage": "Manage machines",
"remote.manage_hint": "Add or manage machines in the web app: open Settings → SSH Remote Machines.",
+ "remote.disconnect_active": "Disconnect current remote",
+ "remote.retry_in": " (retry in {{seconds}}s)",
+ "remote.auth.agent": "agent",
+ "remote.auth.key": "key",
+ "remote.auth.password": "password",
"remote.empty": "No machines yet — add one from the web Settings → SSH Remote Machines panel.",
"remote.degraded": "Local instance unreachable — retrying",
"remote.state.disconnected": "Disconnected",
diff --git a/src/i18n/locales/zh-CN.json b/src/i18n/locales/zh-CN.json
index a2a717d3..037e3b65 100644
--- a/src/i18n/locales/zh-CN.json
+++ b/src/i18n/locales/zh-CN.json
@@ -355,6 +355,11 @@
"remote.local": "本地",
"remote.manage": "管理机器",
"remote.manage_hint": "在网页端「设置 → SSH 远端机器」面板中添加或管理机器。",
+ "remote.disconnect_active": "断开当前远端连接",
+ "remote.retry_in": "({{seconds}}s 后重试)",
+ "remote.auth.agent": "agent",
+ "remote.auth.key": "密钥",
+ "remote.auth.password": "密码",
"remote.empty": "还没有机器——在网页端「设置 → SSH 远端机器」面板添加。",
"remote.degraded": "本地实例不可达,正在重试",
"remote.state.disconnected": "未连接",
diff --git a/src/layout/components/remote-switcher.test.tsx b/src/layout/components/remote-switcher.test.tsx
index d84913c3..4e6da5c7 100644
--- a/src/layout/components/remote-switcher.test.tsx
+++ b/src/layout/components/remote-switcher.test.tsx
@@ -32,7 +32,10 @@ let engineMachines: SshMachineRow[] = []
/** 是否模拟本地实例不可达(/api-ssh 抛错 → 降级态)。 */
let engineUnreachable = false
+let disconnectSpy = vi.fn(async () => undefined)
+
function bindEngine() {
+ disconnectSpy = vi.fn(async () => undefined)
bindSshApiForTests({
listMachines: vi.fn(async () => {
if (engineUnreachable)
@@ -40,7 +43,7 @@ function bindEngine() {
return engineMachines
}),
connect: vi.fn(async () => ({ tunnelBaseUrl: 'http://127.0.0.1:4001' })),
- disconnect: vi.fn(async () => undefined),
+ disconnect: disconnectSpy,
})
}
@@ -182,3 +185,43 @@ describe('remoteSwitcher 交互与降级', () => {
})
})
})
+
+describe('remoteSwitcher 增强(4.4)', () => {
+ it('活动机器:状态点强制绿色 + 底部「断开当前连接」一键断开并回本地', async () => {
+ seedMachines([machineOf({ id: 'm1', name: 'alpha', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' })])
+ remote.activeId = 'm1'
+ remote.activeTunnelUrl = 'http://127.0.0.1:4001'
+ render(<RemoteSwitcher />)
+ const menu = await openMenu()
+
+ const dot = within(menu).getByText('alpha').closest('[class*="flex"]')?.querySelector('span')
+ expect(dot?.className).toContain('bg-success')
+
+ fireEvent.click(within(menu).getByText('remote.disconnect_active'))
+ await waitFor(() => {
+ expect(disconnectSpy).toHaveBeenCalledWith('m1')
+ })
+ expect(remote.activeId).toBeNull()
+ expect(remote.activeTunnelUrl).toBe('')
+ })
+
+ it('无活动机器时不出现断开项', async () => {
+ seedMachines([machineOf({ id: 'm1', name: 'alpha', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' })])
+ render(<RemoteSwitcher />)
+ const menu = await openMenu()
+ expect(within(menu).queryByText('remote.disconnect_active')).toBeNull()
+ })
+
+ it('重连机器显示重试倒计时;已知凭据类型缀在状态后', async () => {
+ seedMachines([
+ machineOf({ id: 'm1', name: 'alpha', state: 'reconnecting', nextRetryAt: Date.now() + 42_000 }),
+ machineOf({ id: 'm2', name: 'beta', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4002', authMethod: 'key' }),
+ ])
+ render(<RemoteSwitcher />)
+ const menu = await openMenu()
+ const alpha = within(menu).getByText('alpha').closest('[class*="flex"]')
+ expect(alpha?.textContent).toContain('remote.retry_in')
+ const beta = within(menu).getByText('beta').closest('[class*="flex"]')
+ expect(beta?.textContent).toContain('remote.auth.key')
+ })
+})
diff --git a/src/layout/components/remote-switcher.tsx b/src/layout/components/remote-switcher.tsx
index ddb1f699..ccb3b42f 100644
--- a/src/layout/components/remote-switcher.tsx
+++ b/src/layout/components/remote-switcher.tsx
@@ -9,6 +9,24 @@ import { store } from '@/store'
import { dotClassOf, dotStyleOf } from '@/store/modules/remote'
import { toast } from '@/utils/toast'
+/** 机器行的状态描述:pending 立即「连接中」;重连带倒计时;已知凭据类型缀后。 */
+function stateTextOf(
+ machine: { state: string, nextRetryAt?: number, authMethod?: 'agent' | 'key' | 'password' },
+ pending: boolean,
+ t: (key: string, params?: Record<string, unknown>) => string,
+): string {
+ if (pending)
+ return t('remote.state.connecting')
+ let text = t(`remote.state.${machine.state}`)
+ if (machine.state === 'reconnecting' && machine.nextRetryAt !== undefined) {
+ const seconds = Math.max(0, Math.ceil((machine.nextRetryAt - Date.now()) / 1000))
+ text += t('remote.retry_in', { seconds })
+ }
+ if (machine.authMethod !== undefined)
+ text += ` · ${t(`remote.auth.${machine.authMethod}`)}`
+ return text
+}
+
/**
* 导航栏远端机器切换器:本地实例 ↔ 各远端机器。
*
@@ -97,18 +115,32 @@ export function RemoteSwitcher() {
>
<span
aria-hidden="true"
- className={cn('size-1.5 shrink-0 rounded-full', dotClassOf(machine))}
- style={dotStyleOf(machine)}
+ className={cn('size-1.5 shrink-0 rounded-full', machine.id === activeId ? 'bg-success' : dotClassOf(machine))}
+ style={machine.id === activeId ? undefined : dotStyleOf(machine)}
/>
<Label className="min-w-0 truncate">{machine.name}</Label>
{/* 待切换的机器在首轮轮询回报前尚无 connecting 状态:由
pendingId 立即给出「连接中」反馈,避免点了没反应 */}
<Description className={cn('ml-auto shrink-0', pendingId === machine.id && 'text-warning')}>
- {pendingId === machine.id ? t('remote.state.connecting') : t(`remote.state.${machine.state}`)}
+ {stateTextOf(machine, pendingId === machine.id, t)}
</Description>
</span>
</Dropdown.Item>
))}
+ {/* 活动远端连接的一键断开(视图先回本地,再向引擎发断开) */}
+ <If cond={activeId !== null}>
+ <Dropdown.Item
+ className="rounded-md"
+ id="remote-disconnect-active"
+ textValue={t('remote.disconnect_active')}
+ onAction={() => {
+ if (activeId !== null)
+ void store.remote.disconnect(activeId)
+ }}
+ >
+ <Label className="text-warning">{t('remote.disconnect_active')}</Label>
+ </Dropdown.Item>
+ </If>
<Dropdown.Item
className="rounded-md"
id="remote-manage"
diff --git a/src/store/modules/remote/api.test.ts b/src/store/modules/remote/api.test.ts
index edde6883..78890e8f 100644
--- a/src/store/modules/remote/api.test.ts
+++ b/src/store/modules/remote/api.test.ts
@@ -11,7 +11,7 @@ function fakeFetch(envelope: unknown, status = 200): SshFetchFn {
const listValue: SshMachineListValue = {
items: [
{ id: 'b', name: 'beta', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4002', color: '#123456', tintBorder: true },
- { id: 'a', name: 'alpha', state: 'disconnected', lastError: 'boom' },
+ { id: 'a', name: 'alpha', state: 'disconnected', lastError: 'boom', authMethod: 'key', nextRetryAt: 1725000000000 },
],
discovered: [
{ id: 'alias', name: 'alias-host', state: 'disconnected' },
@@ -32,6 +32,8 @@ describe('createSshApiClient', () => {
// 按 name 排序:alias-host < alpha < beta
expect(rows.map(row => row.id)).toEqual(['alias', 'a', 'b'])
expect(rows[2]).toMatchObject({ color: '#123456', tintBorder: true, tunnelBaseUrl: 'http://127.0.0.1:4002' })
+ // 增量投影:authMethod 与 nextRetryAt 随行透出(切换器倒计时/凭据后缀用)
+ expect(rows[1]).toMatchObject({ authMethod: 'key', nextRetryAt: 1725000000000 })
// 请求形状:POST /api-ssh + machine.list 信封
expect(fetchFn).toHaveBeenCalledWith(
'http://127.0.0.1:3081/api-ssh',
diff --git a/src/store/modules/remote/api.ts b/src/store/modules/remote/api.ts
index 779723ff..826f760e 100644
--- a/src/store/modules/remote/api.ts
+++ b/src/store/modules/remote/api.ts
@@ -58,6 +58,9 @@ function machineRowOf(raw: unknown): SshMachineRow | undefined {
...typeof value.tunnelBaseUrl === 'string' ? { tunnelBaseUrl: value.tunnelBaseUrl } : {},
...typeof value.lastError === 'string' ? { lastError: value.lastError } : {},
...typeof value.nextRetryAt === 'number' ? { nextRetryAt: value.nextRetryAt } : {},
+ ...value.authMethod === 'agent' || value.authMethod === 'key' || value.authMethod === 'password'
+ ? { authMethod: value.authMethod }
+ : {},
}
}
diff --git a/src/store/modules/remote/store.test.ts b/src/store/modules/remote/store.test.ts
index e37a24e4..047cf86b 100644
--- a/src/store/modules/remote/store.test.ts
+++ b/src/store/modules/remote/store.test.ts
@@ -186,4 +186,22 @@ describe('remote store 切换语义', () => {
remote.switchTo('m1')
expect(remote.pendingId).toBeNull()
})
+
+ it('disconnect:活动机器先回本地视图再向引擎发断开,随后 refresh 落定', async () => {
+ const disconnectSpy = vi.fn(async () => undefined)
+ bindSshApiForTests({
+ listMachines: vi.fn(async () => [machineOf({ id: 'm1', state: 'disconnected' })]),
+ connect: vi.fn(async () => ({ tunnelBaseUrl: 'http://127.0.0.1:4001' })),
+ disconnect: disconnectSpy,
+ })
+ remote.machines = [machineOf({ id: 'm1', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' })]
+ remote.activeId = 'm1'
+ remote.activeTunnelUrl = 'http://127.0.0.1:4001'
+ await remote.disconnect('m1')
+ expect(disconnectSpy).toHaveBeenCalledWith('m1')
+ // 视图立即回本地(不等 refresh),refresh 后引擎落定 disconnected
+ expect(remote.activeId).toBeNull()
+ expect(remote.activeTunnelUrl).toBe('')
+ expect(remote.machines[0]?.state).toBe('disconnected')
+ })
})
diff --git a/src/store/modules/remote/store.ts b/src/store/modules/remote/store.ts
index 3e8d1486..81f99388 100644
--- a/src/store/modules/remote/store.ts
+++ b/src/store/modules/remote/store.ts
@@ -148,6 +148,23 @@ export const remote = defineStore({
this.activeTunnelUrl = ''
this.pendingId = null
},
+
+ /** 断开一台机器:活动机器先退回本地视图,再向引擎发断开。 */
+ async disconnect(machineId: string) {
+ if (this.activeId === machineId)
+ this.backToLocal()
+ if (this.pendingId === machineId)
+ this.pendingId = null
+ try {
+ await api.disconnect(machineId)
+ }
+ catch (err) {
+ console.warn('[remote] disconnect failed:', err)
+ }
+ finally {
+ void this.refresh()
+ }
+ },
},
})
diff --git a/src/store/modules/remote/types.ts b/src/store/modules/remote/types.ts
index 19a5a6f2..b8f01c98 100644
--- a/src/store/modules/remote/types.ts
+++ b/src/store/modules/remote/types.ts
@@ -30,6 +30,8 @@ export interface SshMachineRow {
lastError?: string
/** 下次重连重试时间(epoch ms,reconnecting 时出现)。 */
nextRetryAt?: number
+ /** 当前(或最近一次成功)连接使用的凭据类型。 */
+ authMethod?: 'agent' | 'key' | 'password'
}
/** `machine.list` 的应答信封(items = 手动机器,discovered = ~/.ssh/config 别名)。 */