Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions apps/desktop/src/app/chat/composer/controls.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import { KbdCombo } from '@/components/ui/kbd'
import { Switch } from '@/components/ui/switch'
import { Tip } from '@/components/ui/tooltip'
import { useI18n } from '@/i18n'
import { triggerHaptic } from '@/lib/haptics'
Expand All @@ -27,6 +28,11 @@ export const PRIMARY_ICON_BTN = cn(
'disabled:bg-foreground/30 disabled:text-background disabled:opacity-100'
)

const MODE_PILL = cn(
'flex h-(--composer-control-size) shrink-0 items-center gap-1.5 rounded-md px-2 text-[0.6875rem] font-semibold leading-4',
'shadow-[inset_0_0_0_1px_currentColor] transition-colors'
)

interface ConversationProps {
active: boolean
level: number
Expand All @@ -47,10 +53,14 @@ export function ComposerControls({
compactModelPill = false,
conversation,
disabled,
goalActive,
hasComposerPayload,
planMode,
state,
voiceStatus,
onDictate,
onGoal,
onPlanModeChange,
onSteer,
onToggleAutoSpeak
}: {
Expand All @@ -62,10 +72,14 @@ export function ComposerControls({
compactModelPill?: boolean
conversation: ConversationProps
disabled: boolean
goalActive: boolean
hasComposerPayload: boolean
planMode: boolean
state: ChatBarState
voiceStatus: VoiceStatus
onDictate: () => void
onGoal: () => void
onPlanModeChange: (enabled: boolean) => void
onSteer: () => void
onToggleAutoSpeak: () => void
}) {
Expand All @@ -89,6 +103,53 @@ export function ComposerControls({

return (
<div className="ml-auto flex shrink-0 items-center gap-(--composer-control-gap)">
<Tip label={planMode ? c.planModeOn : c.planModeOff}>
<label
aria-label={planMode ? c.planModeOn : c.planModeOff}
className={cn(
MODE_PILL,
'cursor-pointer bg-sky-500/10 text-sky-700 hover:bg-sky-500/15 dark:text-sky-200',
disabled && 'pointer-events-none opacity-50',
planMode && 'bg-sky-500 text-white shadow-none hover:bg-sky-500/90 dark:bg-sky-400 dark:text-sky-950'
)}
>
<Codicon name="checklist" size="0.82rem" />
<span>{c.planModeLabel}</span>
<Switch
checked={planMode}
className={cn(
'ml-0.5 border-sky-500/40',
planMode && 'data-[state=checked]:bg-white/90 [&_[data-slot=switch-thumb]]:bg-sky-600'
)}
disabled={disabled}
onCheckedChange={checked => {
triggerHaptic('selection')
onPlanModeChange(Boolean(checked))
}}
size="xs"
/>
</label>
</Tip>
<Tip label={goalActive ? c.goalStatus : c.startGoal}>
<Button
aria-label={goalActive ? c.goalStatus : c.startGoal}
aria-pressed={goalActive}
className={cn(
MODE_PILL,
'bg-emerald-500/10 text-emerald-700 hover:bg-emerald-500/15 dark:text-emerald-200',
goalActive &&
'bg-emerald-500 text-white shadow-none hover:bg-emerald-500/90 dark:bg-emerald-400 dark:text-emerald-950'
)}
disabled={disabled}
onClick={onGoal}
size="sm"
type="button"
variant="ghost"
>
<Codicon name="target" size="0.86rem" />
<span>{c.goalModeLabel}</span>
</Button>
</Tip>
<ModelPill compact={compactModelPill} disabled={disabled} model={state.model} />
{/* While the agent runs and the user is typing, steer takes over the mic's
slot rather than crowding the row with an extra button. */}
Expand Down
64 changes: 64 additions & 0 deletions apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { triggerHaptic } from '@/lib/haptics'
import { clearComposerAttachments, clearSessionDraft, type ComposerAttachment } from '@/store/composer'
import { resetBrowseState } from '@/store/composer-input-history'
import { enqueueQueuedPrompt, type QueuedPromptEntry } from '@/store/composer-queue'
import { clearPendingPlan, type PendingPlan, PLAN_PENDING_SESSION, setPendingPlan } from '@/store/plans'

import { cloneAttachments, type QueueEditState } from '../composer-utils'
import { onComposerSubmitRequest } from '../focus'
Expand All @@ -29,6 +30,8 @@ interface UseComposerSubmitArgs {
onCancel: ChatBarProps['onCancel']
onSteer: ChatBarProps['onSteer']
onSubmit: ChatBarProps['onSubmit']
pendingPlan: PendingPlan | null
planMode: boolean
queueCurrentDraft: () => boolean
queueEdit: QueueEditState | null
queuedPrompts: QueuedPromptEntry[]
Expand All @@ -37,6 +40,32 @@ interface UseComposerSubmitArgs {
stashAt: (scope: string | null, text?: string, attachments?: ComposerAttachment[]) => void
}

export function planOnlyPrompt(text: string, attachments: readonly ComposerAttachment[]): string {
const task = text.trim() || (attachments.length ? 'Use the attached context.' : 'Plan the next requested task.')

return [
'Plan mode is enabled.',
'Create a concise implementation plan for the task below, then stop.',
'Use the todo tool to write the plan. Do not edit files, run commands, or start implementation yet.',
'Wait for the user to confirm before executing the plan.',
'',
'Task:',
task
].join('\n')
}

export function approvedPlanPrompt(text: string, attachments: readonly ComposerAttachment[]): string {
const task = text.trim() || (attachments.length ? 'Use the attached context.' : 'Continue with the approved plan.')

return [
'[Plan approved]',
'Proceed with the implementation using the todo plan above. Update todo statuses as you work.',
'',
'Original task:',
task
].join('\n')
}

/**
* The composer's submit engine — the orchestration seam where the draft and
* queue meet. `submitDraft` is the one decision tree (queue-edit save · slash-
Expand Down Expand Up @@ -64,6 +93,8 @@ export function useComposerSubmit({
onCancel,
onSteer,
onSubmit,
pendingPlan,
planMode,
queueCurrentDraft,
queueEdit,
queuedPrompts,
Expand All @@ -82,6 +113,39 @@ export function useComposerSubmit({
stashAt(activeQueueSessionKeyRef.current, text, submittedAttachments)
}

const shouldPlanFirst =
planMode &&
!pendingPlan &&
!SLASH_COMMAND_RE.test(text.trim()) &&
(text.trim().length > 0 || submittedAttachments.length > 0)

if (shouldPlanFirst) {
const key = submittedScope ?? PLAN_PENDING_SESSION
const planPrompt = planOnlyPrompt(text, submittedAttachments)

setPendingPlan(key, {
attachments: submittedAttachments,
originalText: text,
state: 'planning'
})

void Promise.resolve(onSubmit(planPrompt, { attachments: submittedAttachments, displayText: text }))
.then(accepted => {
if (accepted === false) {
clearPendingPlan(key)
restore()
} else {
clearSessionDraft(submittedScope)
}
})
.catch(() => {
clearPendingPlan(key)
restore()
})

return
}

void Promise.resolve(attachments ? onSubmit(text, { attachments }) : onSubmit(text))
.then(accepted => void (accepted === false ? restore() : clearSessionDraft(submittedScope)))
.catch(restore)
Expand Down
Loading