Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions mobile/src/session/MobileSessionSheets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function MobileSessionSheets({ controller }: { controller: MobileSessionC
setShowDictationSetup,
browserScreencastSupported,
quickCommandsSupported,
quickCommandAgentPromptMaxLength,
showToast,
nativeChatTranscriptIsLocalReadable,
nativeChatController,
Expand Down Expand Up @@ -111,6 +112,7 @@ export function MobileSessionSheets({ controller }: { controller: MobileSessionC
: getRepoIdFromMobileWorktreeId(worktreeId) || null
}
repoName={worktreeName || null}
agentPromptMaxLength={quickCommandAgentPromptMaxLength}
onLaunch={launchQuickCommand}
/>
<ActionSheetModal
Expand Down
38 changes: 31 additions & 7 deletions mobile/src/session/QuickCommandEditorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { TEXT_INPUT_FONT_SIZE } from '../platform/text-input-font-size'
import { MobileAgentIcon } from '../components/MobileAgentIcon'
import {
getQuickCommandAgentLabel,
MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH,
getTerminalQuickCommandBodySize,
formatKilobytes,
MAX_QUICK_COMMAND_LABEL_LENGTH,
MAX_QUICK_COMMAND_TERMINAL_TEXT_LENGTH
} from '../terminal/quick-commands'
import type { QuickCommandDraft } from './quick-command-draft'
import { isQuickCommandDraftValid } from './quick-command-draft'
import { isQuickCommandDraftPromptTooLong, isQuickCommandDraftValid } from './quick-command-draft'

type Props = {
draft: QuickCommandDraft
Expand All @@ -22,6 +23,9 @@ type Props = {
// worktree's repo — no cross-repo picker like desktop.
repoId: string | null
repoName: string | null
/** The host's agent-prompt cap. */
/** An older host's agent-prompt character cap; `null` means none. */
agentPromptMaxLength: number | null
onChange: (patch: Partial<QuickCommandDraft>) => void
onOpenAgentPicker: () => void
onCancel: () => void
Expand Down Expand Up @@ -68,6 +72,7 @@ export function QuickCommandEditorForm({
error,
repoId,
repoName,
agentPromptMaxLength,
onChange,
onOpenAgentPicker,
onCancel,
Expand All @@ -76,7 +81,13 @@ export function QuickCommandEditorForm({
const hasRepoScope = repoId !== null
const [advancedOpen, setAdvancedOpen] = useState(draft.scope.type === 'repo')
const isAgent = draft.action === 'agent-prompt'
const canSave = isQuickCommandDraftValid(draft) && !saving
const canSave = isQuickCommandDraftValid(draft, agentPromptMaxLength) && !saving
const promptTooLong = isQuickCommandDraftPromptTooLong(draft, agentPromptMaxLength)
const promptSize = getTerminalQuickCommandBodySize(
'agent-prompt',
draft.prompt,
agentPromptMaxLength
)

return (
<View style={styles.form}>
Expand Down Expand Up @@ -131,11 +142,24 @@ export function QuickCommandEditorForm({
autoCapitalize="none"
autoCorrect={false}
multiline
maxLength={
isAgent ? MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH : MAX_QUICK_COMMAND_TERMINAL_TEXT_LENGTH
}
// Why: maxLength drops the tail of a paste silently, so prompts show a refusal instead.
maxLength={isAgent ? undefined : MAX_QUICK_COMMAND_TERMINAL_TEXT_LENGTH}
/>
{isAgent ? (
{promptTooLong ? (
<View>
{/* Why static text in the alert: a live count would re-announce on every keystroke. */}
<Text style={styles.error} accessibilityRole="alert" accessibilityLiveRegion="polite">
{promptSize.olderHostCap
? 'Too long for this host. Update Orca on the host to save longer prompts.'
: 'Too long to save.'}
</Text>
<Text style={styles.hint}>
{promptSize.unit === 'bytes'
? `${formatKilobytes(promptSize.used)} / ${formatKilobytes(promptSize.max)}`
: `${promptSize.used.toLocaleString()} / ${promptSize.max.toLocaleString()} characters`}
</Text>
</View>
) : isAgent ? (
<Text style={styles.hint}>Supports skills, file paths, and built-in commands.</Text>
) : null}
</View>
Expand Down
5 changes: 5 additions & 0 deletions mobile/src/session/QuickCommandsSheet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('QuickCommandsSheet', () => {
client: {} as RpcClient,
repoId: 'repo-1',
repoName: 'Repo',
agentPromptMaxLength: null,
onLaunch
})
)
Expand All @@ -109,6 +110,7 @@ describe('QuickCommandsSheet', () => {
client: {} as RpcClient,
repoId: 'repo-1',
repoName: 'Repo',
agentPromptMaxLength: null,
onLaunch: () => true
})
)
Expand Down Expand Up @@ -146,6 +148,7 @@ describe('QuickCommandsSheet', () => {
client: {} as RpcClient,
repoId: 'repo-1',
repoName: 'Repo',
agentPromptMaxLength: null,
onLaunch: () => true
})
)
Expand All @@ -172,6 +175,7 @@ describe('QuickCommandsSheet', () => {
client: {} as RpcClient,
repoId: null,
repoName: 'Folder workspace',
agentPromptMaxLength: null,
onLaunch: () => true
})
)
Expand All @@ -195,6 +199,7 @@ describe('QuickCommandsSheet', () => {
client: {} as RpcClient,
repoId: 'repo-1',
repoName: 'Repo',
agentPromptMaxLength: null,
onLaunch: () => true
})
)
Expand Down
8 changes: 6 additions & 2 deletions mobile/src/session/QuickCommandsSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Props = {
client: RpcClient | null
repoId: string | null
repoName: string | null
/** The host's agent-prompt cap. */
agentPromptMaxLength: number | null
onLaunch: (command: TerminalQuickCommand) => boolean
}

Expand All @@ -37,6 +39,7 @@ export function QuickCommandsSheet({
client,
repoId,
repoName,
agentPromptMaxLength,
onLaunch
}: Props) {
const { commands, loading, ready, error, persist } = useQuickCommands({
Expand All @@ -59,7 +62,7 @@ export function QuickCommandsSheet({
}
}

// Why: prompt bodies can total ~240 KB. Lowercase them once per settings
// Why: prompt bodies can total megabytes. Lowercase them once per settings
// update instead of allocating the same search text on every keystroke.
const searchableCommands = useMemo(() => {
return commands
Expand Down Expand Up @@ -123,7 +126,7 @@ export function QuickCommandsSheet({
if (!draft || savingRef.current) {
return
}
const built = draftToQuickCommand(draft)
const built = draftToQuickCommand(draft, agentPromptMaxLength)
if (!built) {
return
}
Expand Down Expand Up @@ -204,6 +207,7 @@ export function QuickCommandsSheet({
error={error}
repoId={repoId}
repoName={repoName}
agentPromptMaxLength={agentPromptMaxLength}
onChange={(patch) =>
setDraft((current) => (current ? { ...current, ...patch } : current))
}
Expand Down
57 changes: 57 additions & 0 deletions mobile/src/session/quick-command-draft.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest'
import {
LEGACY_MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH,
MAX_QUICK_COMMAND_AGENT_PROMPT_MESSAGE_BYTES
} from '../../../src/shared/terminal-quick-command-prompt-limit'
import { terminalQuickCommandAgentPromptMaxLength } from '../terminal/quick-commands'
import { TERMINAL_QUICK_COMMAND_LONG_PROMPTS_RUNTIME_CAPABILITY } from '../../../src/shared/terminal-quick-command-capabilities'
import {
createEmptyQuickCommandDraft,
draftToQuickCommand,
isQuickCommandDraftPromptTooLong,
type QuickCommandDraft
} from './quick-command-draft'

function agentDraft(prompt: string): QuickCommandDraft {
return {
...createEmptyQuickCommandDraft({ type: 'global' }),
label: 'Review',
action: 'agent-prompt',
agent: 'claude',
prompt
}
}

describe('quick command drafts', () => {
it('saves a long prompt whole on a host that stores long prompts', () => {
const prompt = 'x'.repeat(100_000)

expect(draftToQuickCommand(agentDraft(prompt))).toMatchObject({ prompt })
// Past what a chat message carries, measured on the prompt's real size.
expect(
draftToQuickCommand(agentDraft('x'.repeat(MAX_QUICK_COMMAND_AGENT_PROMPT_MESSAGE_BYTES)))
).toBeNull()
})

it('reads the host cap from its capabilities', () => {
expect(terminalQuickCommandAgentPromptMaxLength(['terminal.quick-commands.v1'])).toBe(
LEGACY_MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH
)
expect(
terminalQuickCommandAgentPromptMaxLength([
TERMINAL_QUICK_COMMAND_LONG_PROMPTS_RUNTIME_CAPABILITY
])
).toBeNull()
// Unknown capabilities must not block a save; an older host refuses visibly itself.
expect(terminalQuickCommandAgentPromptMaxLength(undefined)).toBeNull()
})

it('refuses rather than trims a prompt over an older host cap', () => {
const draft = agentDraft('x'.repeat(LEGACY_MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH + 1))

expect(
isQuickCommandDraftPromptTooLong(draft, LEGACY_MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH)
).toBe(true)
expect(draftToQuickCommand(draft, LEGACY_MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH)).toBeNull()
})
})
31 changes: 25 additions & 6 deletions mobile/src/session/quick-command-draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
} from '../../../src/shared/terminal-quick-command-types'
import type { TuiAgent } from '../../../src/shared/tui-agent'
import {
getTerminalQuickCommandBodySize,
isAgentQuickCommand,
MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH,
MAX_QUICK_COMMAND_LABEL_LENGTH,
MAX_QUICK_COMMAND_TERMINAL_TEXT_LENGTH,
supportsTerminalAgentQuickCommand
Expand Down Expand Up @@ -68,15 +68,31 @@ export function quickCommandToDraft(command: TerminalQuickCommand): QuickCommand
}
}

export function isQuickCommandDraftValid(draft: QuickCommandDraft): boolean {
/** Whether the draft's prompt is over its limit; `agentPromptMaxLength` is an older host's cap. */
export function isQuickCommandDraftPromptTooLong(
draft: QuickCommandDraft,
agentPromptMaxLength: number | null
): boolean {
if (draft.action !== 'agent-prompt') {
return false
}
const size = getTerminalQuickCommandBodySize('agent-prompt', draft.prompt, agentPromptMaxLength)
return size.used > size.max
}

export function isQuickCommandDraftValid(
draft: QuickCommandDraft,
agentPromptMaxLength: number | null = null
): boolean {
if (!draft.label.trim()) {
return false
}
if (draft.action === 'agent-prompt') {
return Boolean(
draft.agent &&
supportsTerminalAgentQuickCommand(draft.agent) &&
draft.prompt.trim().length > 0
draft.prompt.trim().length > 0 &&
!isQuickCommandDraftPromptTooLong(draft, agentPromptMaxLength)
)
}
return draft.command.trim().length > 0
Expand All @@ -85,8 +101,11 @@ export function isQuickCommandDraftValid(draft: QuickCommandDraft): boolean {
// Build the persisted command from a draft. Returns null when incomplete so the
// caller can keep the editor open. The server re-normalizes, but we trim/cap
// here so optimistic local state matches what will be saved.
export function draftToQuickCommand(draft: QuickCommandDraft): TerminalQuickCommand | null {
if (!isQuickCommandDraftValid(draft)) {
export function draftToQuickCommand(
draft: QuickCommandDraft,
agentPromptMaxLength: number | null = null
): TerminalQuickCommand | null {
if (!isQuickCommandDraftValid(draft, agentPromptMaxLength)) {
return null
}
// Why: timestamps alone collide when desktop and mobile add commands in the
Expand All @@ -101,7 +120,7 @@ export function draftToQuickCommand(draft: QuickCommandDraft): TerminalQuickComm
label,
action: 'agent-prompt',
agent: draft.agent,
prompt: draft.prompt.trimEnd().slice(0, MAX_QUICK_COMMAND_AGENT_PROMPT_LENGTH),
prompt: draft.prompt.trimEnd(),
scope: draft.scope
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function useMobileSessionFeedbackCapabilities(scope: MobileSessionTermina
null
)
const [quickCommandsSupported, setQuickCommandsSupported] = useState<boolean | null>(null)
// Why: an older host's character cap; null until a host says it has one.
const [quickCommandAgentPromptMaxLength, setQuickCommandAgentPromptMaxLength] = useState<
number | null
>(null)
// Structured-session features are negotiated with the same host capability probe as
// the other session surfaces; consumers never maintain a second status cache.
const [agentSessionHostSupport, setAgentSessionHostSupport] =
Expand Down Expand Up @@ -120,6 +124,8 @@ export function useMobileSessionFeedbackCapabilities(scope: MobileSessionTermina
setAgentSessionHistorySupported,
quickCommandsSupported,
setQuickCommandsSupported,
quickCommandAgentPromptMaxLength,
setQuickCommandAgentPromptMaxLength,
agentSessionHostSupport,
setAgentSessionHostSupport,
browserScreencastSupportedRef,
Expand Down
9 changes: 8 additions & 1 deletion mobile/src/session/use-mobile-session-tab-reconciliation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useEffect, useRef, useCallback, useMemo, useState } from 'react'
import { startRuntimeCapabilityProbe } from '../transport/runtime-capability-probe'
import { supportsMobileQuickCommands } from '../terminal/quick-commands'
import {
supportsMobileQuickCommands,
terminalQuickCommandAgentPromptMaxLength
} from '../terminal/quick-commands'
import { MOBILE_AI_VAULT_CAPABILITY } from '../agent-history/agent-history-capability'
import { TERMINAL_QUERY_REPLY_INPUT_RUNTIME_CAPABILITY } from '../../../src/shared/protocol-version'
import { structuredAgentSessionHostSupport } from './mobile-structured-agent-session-host-support'
Expand Down Expand Up @@ -34,6 +37,7 @@ export function useMobileSessionTabReconciliation(scope: MobileSessionMarkdownAc
setAgentSessionHistorySupported,
setAgentSessionHostSupport,
setQuickCommandsSupported,
setQuickCommandAgentPromptMaxLength,
nativeChatStream,
fetchTerminals,
applySessionTabs,
Expand Down Expand Up @@ -152,6 +156,7 @@ export function useMobileSessionTabReconciliation(scope: MobileSessionMarkdownAc
setAgentSessionHistorySupported(null)
setAgentSessionHostSupport(null)
setQuickCommandsSupported(null)
setQuickCommandAgentPromptMaxLength(null)
setShowQuickCommands(false)
hostQueryReplyInputSupportedRef.current = false
return
Expand All @@ -162,6 +167,7 @@ export function useMobileSessionTabReconciliation(scope: MobileSessionMarkdownAc
setAgentSessionHistorySupported(null)
setAgentSessionHostSupport(null)
setQuickCommandsSupported(null)
setQuickCommandAgentPromptMaxLength(null)
setShowQuickCommands(false)
hostQueryReplyInputSupportedRef.current = false
// Why: the probe retries — a relay→direct cutover or request timeout rejects
Expand All @@ -171,6 +177,7 @@ export function useMobileSessionTabReconciliation(scope: MobileSessionMarkdownAc
setAgentSessionHistorySupported(capabilities.includes(MOBILE_AI_VAULT_CAPABILITY))
setAgentSessionHostSupport(structuredAgentSessionHostSupport(capabilities))
setQuickCommandsSupported(supportsMobileQuickCommands(capabilities))
setQuickCommandAgentPromptMaxLength(terminalQuickCommandAgentPromptMaxLength(capabilities))
// Why: hosts without this capability strip inputKind from terminal.send,
// so a forwarded xterm reply would become floor-stealing shell input.
hostQueryReplyInputSupportedRef.current = capabilities.includes(
Expand Down
Loading
Loading