Skip to content

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

feat(dsh-tauri-ui): offer resume in the composer after an interrupted turn

hairyfceea485f61ef
diff --git a/packages/dsh-tauri-ui/src/client/apis/index.ts b/packages/dsh-tauri-ui/src/client/apis/index.ts
new file mode 100644
index 00000000..8097e91a
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/apis/index.ts
@@ -0,0 +1,16 @@
+/*
+ * @title dsh-tauri-ui
+ * @swagger 2.0
+ * @version 0.0.0
+ */
+
+import type { FetchOptions } from "dsh-tauri/client";
+import { ofetch } from "dsh-tauri/client";
+import type * as Types from "./index.type";
+
+export const baseURL = "/api/desktop/dsh-tauri-ui";
+
+/** @method post */
+export function postSessionResume(body: Types.PostSessionResumeBody, options?: FetchOptions) {
+ return ofetch<Types.SessionResumeResponse>("/session/resume", { baseURL, method: "post", body, ...options });
+}
diff --git a/packages/dsh-tauri-ui/src/client/apis/index.type.ts b/packages/dsh-tauri-ui/src/client/apis/index.type.ts
new file mode 100644
index 00000000..107f3914
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/apis/index.type.ts
@@ -0,0 +1,8 @@
+export type SessionResumeResponse = {
+ ok?: boolean;
+ error?: string;
+};
+
+export interface PostSessionResumeBody {
+ sessionId?: string;
+}
diff --git a/packages/dsh-tauri-ui/src/client/index.ts b/packages/dsh-tauri-ui/src/client/index.ts
index 76ac2535..d144f069 100644
--- a/packages/dsh-tauri-ui/src/client/index.ts
+++ b/packages/dsh-tauri-ui/src/client/index.ts
@@ -1,15 +1,9 @@
import type {} from '@deepseek-ai/dsh-client-ui-layout/client'
import type { ClientContext } from 'dsh-tauri/client'
import { PLUGIN_ID } from '../shared/constants'
-import {
- LOCALE_EFFECT,
- OBSTRUCTIONS_EFFECT,
- SEAT_EFFECT,
- SECTIONS_EFFECT,
- SETTINGS_EFFECT,
- STYLES_EFFECT,
-} from './constants'
+import { LOCALE_EFFECT, OBSTRUCTIONS_EFFECT, SEAT_EFFECT, SECTIONS_EFFECT, SETTINGS_EFFECT, STYLES_EFFECT } from './constants'
import { locale } from './locales'
+import { composerResumeFeature } from './register/composer-resume'
import { registerSettingsObstructions } from './register/obstructions'
import { registerShellSeat } from './register/seat'
import { registerSettingsSections } from './register/sections'
@@ -30,7 +24,9 @@ export * from './utils/cssr'
export * from './utils/style'
export const name = PLUGIN_ID
-export const inject = ['slots', 'layout', 'locale']
+export const inject = ['slots', 'layout', 'locale', 'sessions']
+
+const COMPOSER_RESUME_EFFECT = `${PLUGIN_ID}: composer resume`
export function apply(ctx: ClientContext): void {
ctx.effect(locale.registerLocale, LOCALE_EFFECT)
@@ -39,4 +35,5 @@ export function apply(ctx: ClientContext): void {
ctx.effect(registerSettingsSections, SECTIONS_EFFECT)
ctx.effect(registerSettings, SETTINGS_EFFECT)
ctx.effect(registerSettingsObstructions, OBSTRUCTIONS_EFFECT)
+ ctx.effect(composerResumeFeature, COMPOSER_RESUME_EFFECT)
}
diff --git a/packages/dsh-tauri-ui/src/client/locales/index.ts b/packages/dsh-tauri-ui/src/client/locales/index.ts
index 9db05480..baa79146 100644
--- a/packages/dsh-tauri-ui/src/client/locales/index.ts
+++ b/packages/dsh-tauri-ui/src/client/locales/index.ts
@@ -7,11 +7,13 @@ export const locale = defineLocale(PLUGIN_ID, {
search: '搜索设置…',
settings: '设置',
noResults: '没有匹配的设置项',
+ resumeTask: '继续任务',
},
en: {
back: 'Back to app',
search: 'Search settings…',
settings: 'Settings',
noResults: 'No matching settings',
+ resumeTask: 'Resume task',
},
})
diff --git a/packages/dsh-tauri-ui/src/client/register/composer-resume.register.test.ts b/packages/dsh-tauri-ui/src/client/register/composer-resume.register.test.ts
new file mode 100644
index 00000000..53066a14
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/composer-resume.register.test.ts
@@ -0,0 +1,308 @@
+import type { ComposerSessionEventEntry, ComposerSessionSnapshot } from './composer-resume.types'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { composerResumeFeature } from './composer-resume'
+
+const ARROW_PATH = 'M8.3125 0.980183C8.66767 1.0531'
+const PLAY_FILL_PATH = 'M14.642 6.285c1.294.777 1.294 2.653 0 3.43l-9.113 5.468c-1.333.8-3.028-.16-3.029-1.715V2.532C2.5.978 4.196.018 5.53.818z'
+
+const RESUME_LABEL = 'resumeTask'
+
+interface ControllerStub {
+ triggerObserve: () => void
+ click: (event: unknown) => void
+ dispose: () => void
+}
+
+const mocks = vi.hoisted(() => ({
+ resumeComposer: vi.fn(async () => ({ ok: true })),
+ adapter: {} as Record<string, unknown>,
+ controller: undefined as unknown,
+}))
+
+vi.mock('../service/composer-resume', () => ({ resumeComposer: mocks.resumeComposer }))
+
+vi.mock('dsh-tauri/client', () => {
+ const defineLocale = (namespace: string) => ({
+ NS: namespace,
+ text: (key: string) => key,
+ activeLocale: () => 'en',
+ isEnglishLocale: () => true,
+ useLocale: () => 'en',
+ registerLocale: () => () => {},
+ })
+ const createLifecycleController = () => {
+ const disposers: Array<() => void> = []
+ let clickHandler: ((event: unknown) => void) | undefined
+ let observerCallback: (() => void) | undefined
+ return {
+ add: (disposer: () => void) => {
+ disposers.push(disposer)
+ },
+ listen: (_type: string, handler: (event: unknown) => void) => {
+ clickHandler = handler
+ return () => {}
+ },
+ observe: (_node: unknown, callback: () => void) => {
+ observerCallback = callback
+ return { disconnect: () => {} }
+ },
+ isDisposed: () => false,
+ dispose: () => {
+ for (const disposer of [...disposers])
+ disposer()
+ disposers.length = 0
+ },
+ triggerObserve: () => observerCallback?.(),
+ click: (event: unknown) => clickHandler?.(event),
+ }
+ }
+ return {
+ ofetch: vi.fn(),
+ defineLocale,
+ createLifecycleController,
+ defineRegister: (ctxOrSetup: unknown, maybeSetup?: unknown) => {
+ const setup = (typeof maybeSetup === 'function' ? maybeSetup : ctxOrSetup) as
+ (controller: unknown, ctx: unknown, adapter: unknown) => void
+ return function registerEffect(this: unknown) {
+ const controller = createLifecycleController()
+ mocks.controller = controller
+ setup(controller, this, mocks.adapter)
+ return () => controller.dispose()
+ }
+ },
+ }
+})
+
+class FakeElement {
+ closest: (selector: string) => unknown = () => null
+}
+
+class FakeButtonElement extends FakeElement {}
+
+interface FakeIcon {
+ d: string | null
+}
+
+interface FakeIconElement {
+ getAttribute: (name: string) => string | null
+ setAttribute: (name: string, value: string) => void
+}
+
+interface FakeButton {
+ disabled: boolean
+ querySelector: (selector: string) => FakeIconElement | null
+ getAttribute: (name: string) => string | null
+ setAttribute: (name: string, value: string) => void
+ removeAttribute: (name: string) => void
+}
+
+interface FakeCard {
+ querySelector: (selector: string) => unknown
+ querySelectorAll: (selector: string) => { length: number, item: (index: number) => FakeButton | null }
+}
+
+function snapshotSource<T>(initial: T) {
+ let state = initial
+ const listeners = new Set<() => void>()
+ return {
+ getSnapshot: () => state,
+ subscribe: (listener: () => void) => {
+ listeners.add(listener)
+ return () => listeners.delete(listener)
+ },
+ publish: (next: T) => {
+ state = next
+ for (const listener of [...listeners])
+ listener()
+ },
+ setSilently: (next: T) => {
+ state = next
+ },
+ }
+}
+
+function turnEnd(kind: string): ComposerSessionEventEntry {
+ return { type: 'event', event: { type: 'turn/end', data: { reason: { kind } } } }
+}
+
+function turnStart(): ComposerSessionEventEntry {
+ return { type: 'event', event: { type: 'turn/start' } }
+}
+
+function createButton(options: { path: string | null, label?: string, disabled?: boolean }) {
+ const attributes = new Map<string, string>()
+ if (options.label !== undefined)
+ attributes.set('aria-label', options.label)
+ const icon: FakeIcon = { d: options.path }
+ const iconElement: FakeIconElement = {
+ getAttribute: (name: string) => name === 'd' ? icon.d : null,
+ setAttribute: (name: string, value: string) => {
+ if (name === 'd')
+ icon.d = value
+ },
+ }
+ let disabled = options.disabled ?? true
+ const button: FakeButton = {
+ get disabled() {
+ return disabled
+ },
+ set disabled(value: boolean) {
+ disabled = value
+ },
+ querySelector: (selector: string) => selector === 'svg path' ? iconElement : null,
+ getAttribute: (name: string) => attributes.get(name) ?? null,
+ setAttribute: (name: string, value: string) => void attributes.set(name, value),
+ removeAttribute: (name: string) => void attributes.delete(name),
+ }
+ Object.setPrototypeOf(button, FakeButtonElement.prototype)
+ return {
+ button,
+ icon: () => icon.d,
+ label: () => attributes.get('aria-label') ?? null,
+ }
+}
+
+interface HarnessOptions {
+ empty?: boolean
+ running?: boolean
+ entries?: ComposerSessionEventEntry[]
+ path?: string | null
+ buttonDisabled?: boolean
+}
+
+function harness(options: HarnessOptions = {}) {
+ const sessionId = 's-1'
+ const primary = createButton({
+ path: options.path === undefined ? ARROW_PATH : options.path,
+ label: 'Send message',
+ disabled: options.buttonDisabled ?? true,
+ })
+ const card: FakeCard = {
+ querySelector: (selector: string) =>
+ selector === '[data-composer-placeholder]' && (options.empty ?? true) ? {} : null,
+ querySelectorAll: () => ({ length: 1, item: () => primary.button }),
+ }
+ const session = snapshotSource<ComposerSessionSnapshot>({
+ running: options.running ?? false,
+ removed: false,
+ subagent: null,
+ })
+ const events = snapshotSource({ entries: options.entries ?? [turnEnd('aborted')] })
+ const list = snapshotSource({ current: sessionId as string | undefined })
+
+ mocks.adapter.sessions = {
+ list,
+ binding: (id: string) => id === sessionId ? { session, eventSource: events } : undefined,
+ }
+
+ Object.assign(globalThis, {
+ document: {
+ body: {},
+ querySelector: (selector: string) => selector === '[data-composer-card]' ? card : null,
+ },
+ Element: FakeElement,
+ HTMLButtonElement: FakeButtonElement,
+ })
+
+ const ctx = { locale: { subscribe: () => () => {} } }
+ const register = composerResumeFeature as unknown as (this: unknown) => () => void
+ const cleanup = register.call(ctx)
+ const controller = mocks.controller as ControllerStub
+
+ return {
+ sessionId,
+ button: primary.button,
+ icon: primary.icon,
+ label: primary.label,
+ disabled: () => primary.button.disabled,
+ setIcon: (d: string | null) => {
+ ;(primary.button.querySelector('svg path') as FakeIconElement).setAttribute('d', d ?? '')
+ },
+ setRunning: (running: boolean) => session.publish({ running, removed: false, subagent: null }),
+ setRunningSilently: (running: boolean) => session.setSilently({ running, removed: false, subagent: null }),
+ setEntries: (entries: ComposerSessionEventEntry[]) => events.publish({ entries }),
+ triggerObserve: () => controller.triggerObserve(),
+ click: (event: unknown) => controller.click(event),
+ cleanup,
+ }
+}
+
+beforeEach(() => {
+ mocks.resumeComposer.mockClear()
+ mocks.resumeComposer.mockResolvedValue({ ok: true })
+ delete mocks.adapter.sessions
+})
+
+describe('composerResumeFeature', () => {
+ it('回合中断后把主按钮改写成可点的 PlayFill', () => {
+ const h = harness({ entries: [turnStart(), turnEnd('aborted')] })
+ expect(h.icon()).toBe(PLAY_FILL_PATH)
+ expect(h.disabled()).toBe(false)
+ expect(h.label()).toBe(RESUME_LABEL)
+ h.cleanup()
+ })
+
+ it('同一按钮被 React 重渲染(新 path + disabled)后再次改写', () => {
+ const h = harness({ entries: [turnStart(), turnEnd('aborted')] })
+ expect(h.icon()).toBe(PLAY_FILL_PATH)
+
+ h.setIcon(ARROW_PATH)
+ h.button.disabled = true
+ h.triggerObserve()
+
+ expect(h.icon()).toBe(PLAY_FILL_PATH)
+ expect(h.disabled()).toBe(false)
+ h.cleanup()
+ })
+
+ it('二次中断:running 尚未发布给订阅者时也能判定可继续', () => {
+ const h = harness({ running: true, entries: [turnStart()] })
+ expect(h.icon()).toBe(ARROW_PATH)
+
+ h.setEntries([turnStart(), turnEnd('aborted')])
+ expect(h.icon()).toBe(ARROW_PATH)
+
+ h.setRunningSilently(false)
+ h.button.disabled = true
+ h.triggerObserve()
+
+ expect(h.icon()).toBe(PLAY_FILL_PATH)
+ expect(h.disabled()).toBe(false)
+ h.cleanup()
+ })
+
+ it('任务进行中不改写(stop 方块阶段不被接管)', () => {
+ const h = harness({ running: true, path: null, entries: [turnStart()] })
+ expect(h.icon()).toBe(null)
+ h.cleanup()
+ })
+
+ it('任务正常结束时不改写', () => {
+ const h = harness({ entries: [turnStart(), turnEnd('completed')] })
+ expect(h.icon()).toBe(ARROW_PATH)
+ expect(h.disabled()).toBe(true)
+ h.cleanup()
+ })
+
+ it('草稿非空时还原内核图标与禁用态', () => {
+ const h = harness({ empty: false, entries: [turnStart(), turnEnd('aborted')] })
+ expect(h.icon()).toBe(ARROW_PATH)
+ expect(h.disabled()).toBe(true)
+ h.cleanup()
+ })
+
+ it('点击只请求继续,不发送草稿文本', () => {
+ const h = harness({ entries: [turnStart(), turnEnd('aborted')] })
+ const preventDefault = vi.fn()
+ const stopPropagation = vi.fn()
+ const target = new FakeElement()
+ target.closest = () => h.button
+
+ h.click({ target, preventDefault, stopPropagation })
+
+ expect(preventDefault).toHaveBeenCalledTimes(1)
+ expect(stopPropagation).toHaveBeenCalledTimes(1)
+ expect(mocks.resumeComposer).toHaveBeenCalledWith({ sessionId: 's-1' })
+ h.cleanup()
+ })
+})
diff --git a/packages/dsh-tauri-ui/src/client/register/composer-resume.ts b/packages/dsh-tauri-ui/src/client/register/composer-resume.ts
new file mode 100644
index 00000000..4d6824c8
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/composer-resume.ts
@@ -0,0 +1,149 @@
+import type { ClientContext } from 'dsh-tauri/client'
+import type { ComposerIconState, ComposerSessionBinding, ComposerSessionSnapshot, ComposerSessionsRuntime } from './composer-resume.types'
+import { defineRegister } from 'dsh-tauri/client'
+import { PLUGIN_ID } from '../../shared/constants'
+import { locale } from '../locales'
+import { resumeComposer } from '../service/composer-resume'
+import {
+ isComposerEmpty,
+ paintResumeIcon,
+ primaryButtonOf,
+ readIconPath,
+ restoreDisabled,
+ restorePrimaryIcon,
+ shouldOfferResume,
+} from './composer-resume.utils'
+
+const COMPOSER_CARD_SELECTOR = '[data-composer-card]'
+
+export const composerResumeFeature = defineRegister<ClientContext>((controller, ctx, adapter) => {
+ if (typeof document === 'undefined')
+ return
+
+ const sessions = adapter.sessions as ComposerSessionsRuntime
+ const sessionsList = sessions.list
+ const sessionsBinding = sessions.binding
+ if (sessionsList === undefined || sessionsBinding === undefined)
+ return
+
+ let binding: ComposerSessionBinding | undefined
+ let watchedSessionId: string | undefined
+ let unwatchEvents: (() => void) | undefined
+ let unwatchSession: (() => void) | undefined
+ let patch: { button: HTMLButtonElement, icon: ComposerIconState } | undefined
+ let pending = false
+
+ const resumeLabel = (): string => locale.text('resumeTask')
+
+ const snapshotNow = (): ComposerSessionSnapshot | undefined => binding?.session?.getSnapshot?.()
+
+ const sessionIdNow = (): string | undefined => sessionsList.getSnapshot().current
+
+ const bindingOf = (sessionId: string): ComposerSessionBinding | undefined => {
+ const resolved = sessionsBinding(sessionId)
+ return typeof resolved === 'object' && resolved !== null ? resolved as ComposerSessionBinding : undefined
+ }
+
+ function restore(card?: Element | null): void {
+ const current = patch
+ if (current === undefined)
+ return
+ patch = undefined
+ const anchor = card ?? document.querySelector(COMPOSER_CARD_SELECTOR)
+ const snapshot = snapshotNow()
+ const disabled = anchor !== null && restoreDisabled(
+ isComposerEmpty(anchor),
+ snapshot?.running === true,
+ snapshot?.subagent !== null && snapshot?.subagent !== undefined,
+ )
+ restorePrimaryIcon(current.button, current.icon, { label: resumeLabel(), disabled })
+ }
+
+ function paint(button: HTMLButtonElement): void {
+ if (patch?.button !== button) {
+ const path = readIconPath(button)
+ if (path === null) {
+ button.disabled = false
+ return
+ }
+ restore()
+ patch = { button, icon: { path, ariaLabel: button.getAttribute('aria-label') } }
+ }
+ paintResumeIcon(button, resumeLabel())
+ }
+
+ function reconcile(): void {
+ if (controller.isDisposed())
+ return
+ const card = document.querySelector(COMPOSER_CARD_SELECTOR)
+ const entries = binding?.eventSource?.getSnapshot?.().entries
+ if (card !== null && isComposerEmpty(card) && shouldOfferResume({ session: snapshotNow(), entries })) {
+ const button = primaryButtonOf(card)
+ if (button !== null) {
+ paint(button)
+ return
+ }
+ }
+ restore(card)
+ }
+
+ function unwatch(): void {
+ unwatchEvents?.()
+ unwatchEvents = undefined
+ unwatchSession?.()
+ unwatchSession = undefined
+ }
+
+ function refresh(): void {
+ const sessionId = sessionIdNow()
+ if (sessionId !== watchedSessionId) {
+ watchedSessionId = sessionId
+ unwatch()
+ const next = sessionId === undefined ? undefined : bindingOf(sessionId)
+ if (typeof next?.eventSource?.subscribe === 'function')
+ unwatchEvents = next.eventSource.subscribe(reconcile)
+ if (typeof next?.session?.subscribe === 'function')
+ unwatchSession = next.session.subscribe(reconcile)
+ }
+ binding = sessionId === undefined ? undefined : bindingOf(sessionId)
+ reconcile()
+ }
+
+ async function resume(): Promise<void> {
+ const sessionId = sessionIdNow()
+ if (sessionId === undefined || pending)
+ return
+ pending = true
+ const outcome = await resumeComposer({ sessionId })
+ pending = false
+ if (!outcome.ok) {
+ console.warn(`[${PLUGIN_ID}] 会话继续失败: ${outcome.error ?? 'unknown'}`)
+ reconcile()
+ }
+ }
+
+ controller.add(sessionsList.subscribe(refresh))
+ controller.add(ctx.locale.subscribe(() => {
+ if (patch !== undefined)
+ paintResumeIcon(patch.button, resumeLabel())
+ }))
+ controller.add(unwatch)
+ controller.listen('click', (event) => {
+ if (patch === undefined)
+ return
+ const button = event.target instanceof Element ? event.target.closest('button') : null
+ if (button !== patch.button)
+ return
+ event.preventDefault()
+ event.stopPropagation()
+ void resume()
+ }, { capture: true })
+ controller.observe(document.body, () => reconcile(), {
+ attributeFilter: ['d', 'disabled'],
+ attributes: true,
+ childList: true,
+ subtree: true,
+ })
+
+ refresh()
+})
diff --git a/packages/dsh-tauri-ui/src/client/register/composer-resume.types.ts b/packages/dsh-tauri-ui/src/client/register/composer-resume.types.ts
new file mode 100644
index 00000000..d7bf80a0
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/composer-resume.types.ts
@@ -0,0 +1,43 @@
+export interface ComposerListProjection {
+ subscribe: (listener: () => void) => () => void
+ getSnapshot: () => { current?: string }
+}
+
+export interface ComposerSessionSnapshot {
+ running?: boolean
+ removed?: boolean
+ subagent?: unknown
+}
+
+export interface ComposerSessionEventEntry {
+ type?: string
+ event?: {
+ type?: string
+ data?: { reason?: { kind?: string } }
+ }
+}
+
+export interface ComposerSessionEventSource {
+ subscribe: (listener: () => void) => () => void
+ getSnapshot: () => { entries?: readonly ComposerSessionEventEntry[] }
+}
+
+export interface ComposerSession {
+ subscribe?: (listener: () => void) => () => void
+ getSnapshot?: () => ComposerSessionSnapshot
+}
+
+export interface ComposerSessionBinding {
+ session?: ComposerSession
+ eventSource?: ComposerSessionEventSource
+}
+
+export interface ComposerSessionsRuntime {
+ list?: ComposerListProjection
+ binding?: (sessionId: string) => unknown
+}
+
+export interface ComposerIconState {
+ path: string | null
+ ariaLabel: string | null
+}
diff --git a/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.test.ts b/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.test.ts
new file mode 100644
index 00000000..320bd10b
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.test.ts
@@ -0,0 +1,173 @@
+import type { ComposerSessionEventEntry } from './composer-resume.types'
+import { describe, expect, it } from 'vitest'
+import { isComposerEmpty, lastTurnEndKind, paintResumeIcon, readIconPath, restoreDisabled, restorePrimaryIcon, shouldOfferResume } from './composer-resume.utils'
+
+const ARROW_PATH = 'M8.3125 0.980183C8.66767 1.0531'
+const PLAY_FILL_PATH = 'M14.642 6.285c1.294.777 1.294 2.653 0 3.43l-9.113 5.468c-1.333.8-3.028-.16-3.029-1.715V2.532C2.5.978 4.196.018 5.53.818z'
+
+interface ButtonStub {
+ button: HTMLButtonElement
+ attributes: Map<string, string>
+ iconPath: () => string | null
+ pathWrites: () => number
+ disabledWrites: () => number
+}
+
+function stubButton(options: { path?: string, ariaLabel?: string, noPath?: boolean, disabled?: boolean } = {}): ButtonStub {
+ const attributes = new Map<string, string>()
+ if (options.path !== undefined)
+ attributes.set('d', options.path)
+ if (options.ariaLabel !== undefined)
+ attributes.set('aria-label', options.ariaLabel)
+
+ let pathWrites = 0
+ let disabledWrites = 0
+ let disabled = options.disabled ?? true
+
+ const pathElement = {
+ getAttribute: (name: string) => attributes.get(name) ?? null,
+ setAttribute: (name: string, value: string) => {
+ pathWrites += 1
+ attributes.set(name, value)
+ },
+ }
+ const button = {
+ get disabled() {
+ return disabled
+ },
+ set disabled(value: boolean) {
+ disabledWrites += 1
+ disabled = value
+ },
+ querySelector: (selector: string) => selector === 'svg path' && options.noPath !== true ? pathElement : null,
+ getAttribute: (name: string) => attributes.get(name) ?? null,
+ setAttribute: (name: string, value: string) => void attributes.set(name, value),
+ removeAttribute: (name: string) => void attributes.delete(name),
+ }
+ return {
+ button: button as unknown as HTMLButtonElement,
+ attributes,
+ iconPath: () => attributes.get('d') ?? null,
+ pathWrites: () => pathWrites,
+ disabledWrites: () => disabledWrites,
+ }
+}
+
+function stubCard(placeholder: boolean): Element {
+ return { querySelector: () => (placeholder ? {} : null) } as unknown as Element
+}
+
+describe('isComposerEmpty', () => {
+ it('reads the kernel placeholder as the empty-draft signal', () => {
+ expect(isComposerEmpty(stubCard(true))).toBe(true)
+ expect(isComposerEmpty(stubCard(false))).toBe(false)
+ })
+})
+
+describe('lastTurnEndKind', () => {
+ function entry(type: string, kind?: string): ComposerSessionEventEntry {
+ return { type: 'event', event: { type, data: kind === undefined ? {} : { reason: { kind } } } }
+ }
+
+ it('returns the newest turn/end reason kind', () => {
+ expect(lastTurnEndKind([
+ entry('turn/start'),
+ entry('turn/end', 'completed'),
+ entry('turn/start'),
+ entry('turn/end', 'aborted'),
+ ])).toBe('aborted')
+ })
+
+ it('reads the interrupted kind synthesized by crash-tail recovery', () => {
+ expect(lastTurnEndKind([entry('turn/end', 'interrupted')])).toBe('interrupted')
+ })
+
+ it('ignores an open turn, transient entries and missing windows', () => {
+ expect(lastTurnEndKind([entry('turn/end', 'error'), entry('turn/start')])).toBe('error')
+ expect(lastTurnEndKind([{ type: 'transient', event: { type: 'assistant/live-chunk' } }])).toBeUndefined()
+ expect(lastTurnEndKind([])).toBeUndefined()
+ expect(lastTurnEndKind(undefined)).toBeUndefined()
+ })
+
+ it('returns the raw kind without narrowing it', () => {
+ expect(lastTurnEndKind([entry('turn/end', 'max-tokens')])).toBe('max-tokens')
+ })
+})
+
+describe('paintResumeIcon / restorePrimaryIcon', () => {
+ it('paints the play icon, enables the button and restores the original state', () => {
+ const stub = stubButton({ ariaLabel: 'Send message', path: ARROW_PATH })
+ expect(readIconPath(stub.button)).toBe(ARROW_PATH)
+
+ paintResumeIcon(stub.button, 'Resume task')
+ expect(stub.iconPath()).toBe(PLAY_FILL_PATH)
+ expect(stub.button.disabled).toBe(false)
+ expect(stub.attributes.get('aria-label')).toBe('Resume task')
+
+ restorePrimaryIcon(stub.button, { path: ARROW_PATH, ariaLabel: 'Send message' }, { label: 'Resume task', disabled: true })
+ expect(stub.iconPath()).toBe(ARROW_PATH)
+ expect(stub.attributes.get('aria-label')).toBe('Send message')
+ expect(stub.button.disabled).toBe(true)
+ })
+
+ it('leaves a kernel re-rendered icon and label alone', () => {
+ const stub = stubButton({ ariaLabel: 'Steer the running turn', path: 'M8 0.75 0 0 1', disabled: false })
+ restorePrimaryIcon(stub.button, { path: ARROW_PATH, ariaLabel: 'Send message' }, { label: 'Resume task', disabled: false })
+ expect(stub.iconPath()).toBe('M8 0.75 0 0 1')
+ expect(stub.attributes.get('aria-label')).toBe('Steer the running turn')
+ expect(stub.button.disabled).toBe(false)
+ })
+
+ it('keeps the button enabled when the draft is no longer empty', () => {
+ const stub = stubButton({ ariaLabel: 'Send message', path: ARROW_PATH, disabled: false })
+ paintResumeIcon(stub.button, 'Resume task')
+ restorePrimaryIcon(stub.button, { path: ARROW_PATH, ariaLabel: 'Send message' }, { label: 'Resume task', disabled: false })
+ expect(stub.button.disabled).toBe(false)
+ })
+
+ it('drops an aria-label the kernel never set', () => {
+ const stub = stubButton({ path: ARROW_PATH })
+ paintResumeIcon(stub.button, 'Resume task')
+ restorePrimaryIcon(stub.button, { path: ARROW_PATH, ariaLabel: null }, { label: 'Resume task', disabled: false })
+ expect(stub.attributes.has('aria-label')).toBe(false)
+ })
+
+ it('re-applies the play icon without writing the same value twice', () => {
+ const stub = stubButton({ ariaLabel: 'Send message', path: ARROW_PATH, disabled: false })
+ paintResumeIcon(stub.button, 'Resume task')
+ const firstWrites = stub.pathWrites()
+ const firstDisabled = stub.disabledWrites()
+
+ paintResumeIcon(stub.button, 'Resume task')
+ paintResumeIcon(stub.button, 'Resume task')
+
+ expect(firstWrites).toBe(1)
+ expect(stub.pathWrites()).toBe(firstWrites)
+ expect(stub.disabledWrites()).toBe(firstDisabled)
+ expect(stub.iconPath()).toBe(PLAY_FILL_PATH)
+ })
+
+ it('tolerates a button whose icon node is not mounted yet', () => {
+ const stub = stubButton({ ariaLabel: 'Send message', noPath: true, disabled: true })
+ paintResumeIcon(stub.button, 'Resume task')
+ expect(stub.iconPath()).toBe(null)
+ expect(stub.button.disabled).toBe(false)
+ expect(stub.attributes.get('aria-label')).toBe('Resume task')
+ })
+
+ it('settles the resume offer from the live snapshot, never from a cached flag', () => {
+ const entries: ComposerSessionEventEntry[] = [{ type: 'event', event: { type: 'turn/end', data: { reason: { kind: 'aborted' } } } }]
+ expect(shouldOfferResume({ session: { running: false, removed: false }, entries })).toBe(true)
+ expect(shouldOfferResume({ session: { running: true, removed: false }, entries })).toBe(false)
+ expect(shouldOfferResume({ session: { running: false, removed: true }, entries })).toBe(false)
+ expect(shouldOfferResume({ session: undefined, entries })).toBe(false)
+ expect(shouldOfferResume({ session: { running: false }, entries: [] })).toBe(false)
+ })
+
+ it('leaves the kernel stop button enabled while the turn runs', () => {
+ expect(restoreDisabled(true, false, false)).toBe(true)
+ expect(restoreDisabled(true, true, false)).toBe(false)
+ expect(restoreDisabled(true, true, true)).toBe(true)
+ expect(restoreDisabled(false, false, false)).toBe(false)
+ })
+})
diff --git a/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.ts b/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.ts
new file mode 100644
index 00000000..af235f42
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/register/composer-resume.utils.ts
@@ -0,0 +1,79 @@
+import type { ComposerIconState, ComposerSessionEventEntry, ComposerSessionSnapshot } from './composer-resume.types'
+
+const PLAY_FILL_PATH = 'M14.642 6.285c1.294.777 1.294 2.653 0 3.43l-9.113 5.468c-1.333.8-3.028-.16-3.029-1.715V2.532C2.5.978 4.196.018 5.53.818z'
+
+const COMPOSER_PLACEHOLDER_SELECTOR = '[data-composer-placeholder]'
+
+const RESUMABLE_TURN_END_KINDS = ['aborted', 'error', 'interrupted']
+
+export function primaryButtonOf(card: Element): HTMLButtonElement | null {
+ const buttons = card.querySelectorAll('button')
+ const last = buttons.item(buttons.length - 1)
+ return last instanceof HTMLButtonElement ? last : null
+}
+
+export function isComposerEmpty(card: Element): boolean {
+ return card.querySelector(COMPOSER_PLACEHOLDER_SELECTOR) !== null
+}
+
+export function lastTurnEndKind(entries: readonly ComposerSessionEventEntry[] | undefined): string | undefined {
+ if (entries === undefined)
+ return undefined
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
+ const event = entries[index]?.event
+ if (event?.type !== 'turn/end')
+ continue
+ return event.data?.reason?.kind
+ }
+ return undefined
+}
+
+export function readIconPath(button: HTMLButtonElement): string | null {
+ return button.querySelector('svg path')?.getAttribute('d') ?? null
+}
+
+export function isResumableTurnEnd(kind: string | undefined): boolean {
+ return kind !== undefined && RESUMABLE_TURN_END_KINDS.includes(kind)
+}
+
+export function shouldOfferResume(input: {
+ session?: ComposerSessionSnapshot
+ entries?: readonly ComposerSessionEventEntry[]
+}): boolean {
+ const session = input.session
+ if (session === undefined || session.running === true || session.removed === true)
+ return false
+ return isResumableTurnEnd(lastTurnEndKind(input.entries))
+}
+
+export function restoreDisabled(composerEmpty: boolean, running: boolean, hasSubagent: boolean): boolean {
+ return composerEmpty && !(running && !hasSubagent)
+}
+
+export function paintResumeIcon(button: HTMLButtonElement, label: string): void {
+ const path = button.querySelector('svg path')
+ if (path !== null && path.getAttribute('d') !== PLAY_FILL_PATH)
+ path.setAttribute('d', PLAY_FILL_PATH)
+ if (button.disabled)
+ button.disabled = false
+ if (button.getAttribute('aria-label') !== label)
+ button.setAttribute('aria-label', label)
+}
+
+export function restorePrimaryIcon(
+ button: HTMLButtonElement,
+ state: ComposerIconState,
+ options: { label: string, disabled: boolean },
+): void {
+ const path = button.querySelector('svg path')
+ if (state.path !== null && path !== null && path.getAttribute('d') === PLAY_FILL_PATH)
+ path.setAttribute('d', state.path)
+ if (button.getAttribute('aria-label') === options.label) {
+ if (state.ariaLabel === null)
+ button.removeAttribute('aria-label')
+ else
+ button.setAttribute('aria-label', state.ariaLabel)
+ }
+ if (options.disabled)
+ button.disabled = true
+}
diff --git a/packages/dsh-tauri-ui/src/client/service/composer-resume.ts b/packages/dsh-tauri-ui/src/client/service/composer-resume.ts
new file mode 100644
index 00000000..b2d39180
--- /dev/null
+++ b/packages/dsh-tauri-ui/src/client/service/composer-resume.ts
@@ -0,0 +1,11 @@
+import { postSessionResume } from '../apis'
+
+export async function resumeComposer(input: { sessionId: string }): Promise<{ ok: boolean, error?: string }> {
+ try {
+ await postSessionResume({ sessionId: input.sessionId })
+ return { ok: true }
+ }
+ catch (error) {
+ return { ok: false, error: error instanceof Error ? error.message : String(error) }
+ }
+}