diff --git a/apps/mobile/src/components/ComposerModelControl.tsx b/apps/mobile/src/components/ComposerModelControl.tsx index cd018ea70a9f..c395b15b721d 100644 --- a/apps/mobile/src/components/ComposerModelControl.tsx +++ b/apps/mobile/src/components/ComposerModelControl.tsx @@ -1,4 +1,6 @@ import type { ModelSelection, ProviderDriverKind } from "@t3tools/contracts"; +import { useState } from "react"; +import { View } from "react-native"; import { buildTraitsTriggerDisplay, getPromptControlledTraits, @@ -19,6 +21,10 @@ export function ComposerModelControl(props: { readonly disabled?: boolean; readonly size?: "regular" | "large"; readonly iconOnly?: boolean; + /** Effort abbreviation instead of the full traits, for a row shared with the editor. */ + readonly compact?: boolean; + /** Width the control may fill; falls back to the effort abbreviation plus speed when the full traits overflow it. */ + readonly fitWidth?: number; readonly onPress: () => void; }) { const descriptors = resolveProviderOptionDescriptors({ @@ -26,15 +32,19 @@ export function ComposerModelControl(props: { selections: props.selection?.options, }); const promptTraits = getPromptControlledTraits(descriptors, props.prompt); - const summary = props.provider + const traits = props.provider ? buildTraitsTriggerDisplay({ descriptors, primarySelectDescriptorId: promptTraits.primarySelectDescriptor?.id ?? null, ultrathinkPromptControlled: promptTraits.ultrathinkPromptControlled, modelSelection: props.selection, reportedModelSelection: props.reportedSelection, - }).label - : ""; + }) + : null; + const summary = traits?.label ?? ""; + const [fullWidth, setFullWidth] = useState(0); + const overflows = props.fitWidth !== undefined && fullWidth > 0 && fullWidth > props.fitWidth + 1; + const compact = props.compact === true || overflows; const badge = props.modelOption?.providerBadge; const label = props.modelOption?.label ?? props.selection?.model ?? "Choose model"; const providerNameLabel = badge @@ -46,70 +56,91 @@ export function ComposerModelControl(props: { }) : null; const shortLabel = props.modelOption?.shortName ?? label; + const visibleSummary = props.compact + ? (traits?.compactEffortLabel ?? "") + : overflows + ? [traits?.compactEffortLabel, traits?.speedLabel].filter(Boolean).join(" ") + : summary; + const renderControl = (full: boolean) => { + const controlSummary = full ? summary : visibleSummary; + const suffixLabel = providerNameLabel + ? [shortLabel, controlSummary].filter(Boolean).join(" · ") + : controlSummary; + return ( + + badge?.showBadge ? ( + + ) : ( + + ) + } + label={providerNameLabel ?? shortLabel} + labelEllipsizeMode={providerNameLabel ? "middle" : undefined} + iconOnly={props.iconOnly} + suffixWidth={providerNameLabel ? 96 : undefined} + // Only short abbreviations keep their width; an unrecognized effort name truncates. + suffixFixed={!full && compact && (traits?.compactEffortLabel?.length ?? 0) <= 4} + suffix={ + suffixLabel ? ( + + {suffixLabel} + + ) : undefined + } + maxWidth={full ? 10_000 : "100%"} + onPress={props.onPress} + /> + ); + }; + if (props.fitWidth === undefined) return renderControl(false); return ( - - badge?.showBadge ? ( - - ) : ( - - ) - } - label={providerNameLabel ?? shortLabel} - labelEllipsizeMode={providerNameLabel ? "middle" : undefined} - iconOnly={props.iconOnly} - suffixWidth={providerNameLabel ? 96 : undefined} - suffix={ - providerNameLabel ? ( - - {shortLabel} - {summary ? ` · ${summary}` : ""} - - ) : summary ? ( - - {summary} - - ) : undefined - } - maxWidth="100%" - onPress={props.onPress} - /> + <> + {/* Unconstrained copy measures whether the full labels fit. */} + + setFullWidth(event.nativeEvent.layout.width)} + > + {renderControl(true)} + + + {renderControl(false)} + ); } diff --git a/apps/mobile/src/components/ComposerToolbar.tsx b/apps/mobile/src/components/ComposerToolbar.tsx index fdbf27b6f2c2..b2a11e6f6f9f 100644 --- a/apps/mobile/src/components/ComposerToolbar.tsx +++ b/apps/mobile/src/components/ComposerToolbar.tsx @@ -35,6 +35,8 @@ export function ComposerInlineControl(props: { readonly labelEllipsizeMode?: ComponentProps["ellipsizeMode"]; readonly suffix?: ReactNode; readonly suffixWidth?: number; + /** A short suffix keeps its width while the label truncates. */ + readonly suffixFixed?: boolean; /** Show only the icon and chevron; the label still names the control for accessibility. */ readonly iconOnly?: boolean; readonly maxWidth?: ViewStyle["maxWidth"]; @@ -104,7 +106,10 @@ export function ComposerInlineControl(props: { )} {!props.iconOnly && props.suffix ? ( {props.suffix} diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index fdd6b414a46e..b3f7628f8bd5 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -148,7 +148,7 @@ function collapsedEditorInset(text: ScaledTextRole): number { return Math.max(0, Math.round(COLLAPSED_EDITOR_DEFAULT_INSET - shift)); } -function useCompactComposerEditor(text: ScaledTextRole, value: string) { +function useCompactComposerEditor(text: ScaledTextRole, value: string, forceExpanded = false) { const [contentSize, setContentSize] = useState({ width: 0, height: 0 }); const [compactWidth, setCompactWidth] = useState(0); const contentHeight = contentSize.height; @@ -165,9 +165,10 @@ function useCompactComposerEditor(text: ScaledTextRole, value: string) { // A line that fits the expanded editor may still overflow its compact slot. // Compare its measured width too so widening cannot trigger a collapse loop. const isExpanded = - value.length > 0 && - (contentHeight > text.lineHeight * 1.5 || - (compactWidth > 0 && contentSize.width > compactWidth)); + forceExpanded || + (value.length > 0 && + (contentHeight > text.lineHeight * 1.5 || + (compactWidth > 0 && contentSize.width > compactWidth))); const compactRowHeight = Math.max(44, minimum); const isRTL = I18nManager.isRTL; const editorStyle = useAnimatedStyle(() => ({ @@ -301,6 +302,8 @@ const composerHeightTransition: LayoutAnimationFunction = (values) => { const COMPOSER_LAYOUT_TRANSITION = Platform.OS === "android" ? undefined : composerHeightTransition; // Share of the one-row composer the draft keeps; a long model name truncates first. const INLINE_EDITOR_MIN_WIDTH = "40%"; +// Android types in the expanded editor, so the unfocused row only previews the draft. +const ANDROID_PREVIEW_EDITOR_MIN_WIDTH = "25%"; const INLINE_EDITOR_HEIGHT = 36; const COMPOSER_ATTACHMENT_ENTERING = @@ -619,8 +622,14 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps const usesLargeControls = sidebarWorkspaceTabs === "picker" || deviceType === "pad"; const surfacePaddingVertical = usesLargeControls ? 9 : floatingSidebarComposer ? 6 : 2; const [compactRowWidth, setCompactRowWidth] = useState(0); + const [modelToolbarWidth, setModelToolbarWidth] = useState(0); const compactAttachmentLimit = compactRowWidth >= 500 ? 3 : 1; - const compactEditor = useCompactComposerEditor(bodyText, draftMessage); + // Android gives focused typing the full-width editor and a toolbar row for the model labels. + const compactEditor = useCompactComposerEditor( + bodyText, + draftMessage, + Platform.OS === "android" && isExpanded, + ); const showsCompactDictation = isVoiceInputPresented; const isToolbarVisible = isVoiceInputPresented; const hasEditingAttachments = @@ -820,8 +829,19 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps ) : null; + const fitsModelToolbar = Platform.OS === "android" && compactEditor.isExpanded; + if (!fitsModelToolbar && modelToolbarWidth > 0) { + setModelToolbarWidth(0); + } const modelControl = ( - + setModelToolbarWidth(event.nativeEvent.layout.width) + : undefined + } + > 0 ? modelToolbarWidth : undefined} onPress={openSettings} /> @@ -967,10 +989,13 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps onLayout={compactEditor.onEditorLayout} style={[ { - minWidth: - compactEditor.isExpanded || stripAttachments.length > 0 - ? 0 - : INLINE_EDITOR_MIN_WIDTH, + minWidth: compactEditor.isExpanded + ? 0 + : Platform.OS === "android" + ? ANDROID_PREVIEW_EDITOR_MIN_WIDTH + : stripAttachments.length > 0 + ? 0 + : INLINE_EDITOR_MIN_WIDTH, }, compactEditor.containerStyle, compactEditor.isExpanded ? { flex: 0, width: 0 } : undefined, diff --git a/packages/client-runtime/src/modelTraits.test.ts b/packages/client-runtime/src/modelTraits.test.ts index 24da035532a0..d1a70b8868d7 100644 --- a/packages/client-runtime/src/modelTraits.test.ts +++ b/packages/client-runtime/src/modelTraits.test.ts @@ -3,6 +3,34 @@ import { describe, expect, it } from "vite-plus/test"; import { buildTraitsTriggerDisplay, getDescriptorStringValue } from "./modelTraits.ts"; +function effort(currentValue: string): ProviderOptionDescriptor { + return { + id: "reasoningEffort", + label: "Reasoning", + type: "select", + currentValue, + options: [ + { id: "medium", label: "Medium" }, + { id: "high", label: "High" }, + { id: "xhigh", label: "Extra High" }, + { id: "custom", label: "Deep Think" }, + { id: "prototype", label: "__proto__" }, + ], + }; +} + +function fastMode(currentValue: boolean): ProviderOptionDescriptor { + return { id: "fastMode", label: "Fast mode", type: "boolean", currentValue }; +} + +function display(descriptors: ReadonlyArray) { + return buildTraitsTriggerDisplay({ + descriptors, + primarySelectDescriptorId: "reasoningEffort", + ultrathinkPromptControlled: false, + }); +} + describe("buildTraitsTriggerDisplay", () => { it.each([ { currentValue: undefined, defaultValue: undefined, value: null, label: "Thinking" }, @@ -32,4 +60,30 @@ describe("buildTraitsTriggerDisplay", () => { ).toBe(input.label); expect(getDescriptorStringValue(descriptor)).toBe(input.value); }); + + it("abbreviates the selected effort", () => { + expect(display([effort("medium")]).compactEffortLabel).toBe("M"); + expect(display([effort("high")]).compactEffortLabel).toBe("H"); + expect(display([effort("xhigh")]).compactEffortLabel).toBe("XH"); + }); + + it("keeps an unknown effort label whole rather than inventing an abbreviation", () => { + expect(display([effort("custom")]).compactEffortLabel).toBe("Deep Think"); + expect(display([effort("prototype")]).compactEffortLabel).toBe("__proto__"); + }); + + it("reports Fast only while it is enabled and keeps the effort when it is off", () => { + expect(display([effort("high"), fastMode(true)])).toEqual({ + label: "High Fast", + compactEffortLabel: "H", + speedLabel: "Fast", + ultracode: false, + }); + expect(display([effort("high"), fastMode(false)])).toEqual({ + label: "High", + compactEffortLabel: "H", + speedLabel: null, + ultracode: false, + }); + }); }); diff --git a/packages/client-runtime/src/modelTraits.ts b/packages/client-runtime/src/modelTraits.ts index 6e84acf10fc0..2e608ad9999f 100644 --- a/packages/client-runtime/src/modelTraits.ts +++ b/packages/client-runtime/src/modelTraits.ts @@ -105,6 +105,25 @@ export function getTraitSelectChange( }; } +const COMPACT_EFFORT_LABELS: Record = { + none: "None", + minimal: "Min", + low: "L", + medium: "M", + high: "H", + "extra high": "XH", + xhigh: "XH", + max: "Max", +}; + +function compactEffortLabel(label: string): string { + const key = label + .toLowerCase() + .replace(/\s+effort$/, "") + .trim(); + return Object.hasOwn(COMPACT_EFFORT_LABELS, key) ? COMPACT_EFFORT_LABELS[key]! : label; +} + export function buildTraitsTriggerDisplay(input: { descriptors: ReadonlyArray; primarySelectDescriptorId: string | null; @@ -113,6 +132,7 @@ export function buildTraitsTriggerDisplay(input: { reportedModelSelection?: ModelSelection | null | undefined; }): { label: string; + compactEffortLabel: string | null; /** "Fast" or "Ultrafast" when a speed option is on. */ speedLabel: string | null; ultracode: boolean; @@ -173,9 +193,14 @@ export function buildTraitsTriggerDisplay(input: { } } - const ultracode = ultracodeLabelIndex >= 0; + const compact = { + compactEffortLabel: + reasoningLabelIndex >= 0 ? compactEffortLabel(labels[reasoningLabelIndex]!) : null, + speedLabel, + ultracode: ultracodeLabelIndex >= 0, + }; if (labels.length === 0 && fastModeFallbackLabel !== null) { - return { label: fastModeFallbackLabel, speedLabel, ultracode }; + return { label: fastModeFallbackLabel, ...compact }; } if (speedLabel) { if (reasoningLabelIndex >= 0) { @@ -184,5 +209,5 @@ export function buildTraitsTriggerDisplay(input: { labels.push(speedLabel); } } - return { label: labels.join(" · "), speedLabel, ultracode }; + return { label: labels.join(" · "), ...compact }; }