Skip to content
Open
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
165 changes: 98 additions & 67 deletions apps/mobile/src/components/ComposerModelControl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ModelSelection, ProviderDriverKind } from "@t3tools/contracts";
import { useState } from "react";
import { View } from "react-native";
import {
buildTraitsTriggerDisplay,
getPromptControlledTraits,
Expand All @@ -19,22 +21,30 @@ 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({
capabilities: props.modelOption?.capabilities,
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
Expand All @@ -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 (
<ComposerInlineControl
accessibilityLabel={[
"Model and reasoning settings",
label,
badge?.showBadge ? badge.displayName : null,
summary,
]
.filter(Boolean)
.join(", ")}
size={props.size}
disabled={props.disabled}
emphasized
renderIcon={(size) =>
badge?.showBadge ? (
<ProviderInstanceIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
displayName={badge.displayName}
accentColor={badge.accentColor}
showBadge
surfaceColor={props.surfaceColor}
size={size}
/>
) : (
<ProviderIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
size={size}
/>
)
}
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 ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{suffixLabel}
</Text>
) : undefined
}
maxWidth={full ? 10_000 : "100%"}
onPress={props.onPress}
/>
);
};
if (props.fitWidth === undefined) return renderControl(false);
return (
<ComposerInlineControl
accessibilityLabel={[
"Model and reasoning settings",
label,
badge?.showBadge ? badge.displayName : null,
summary,
]
.filter(Boolean)
.join(", ")}
size={props.size}
disabled={props.disabled}
emphasized
renderIcon={(size) =>
badge?.showBadge ? (
<ProviderInstanceIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
displayName={badge.displayName}
accentColor={badge.accentColor}
showBadge
surfaceColor={props.surfaceColor}
size={size}
/>
) : (
<ProviderIcon
iconUrl={props.modelOption?.providerIconUrl}
provider={props.modelOption?.providerDriver}
size={size}
/>
)
}
label={providerNameLabel ?? shortLabel}
labelEllipsizeMode={providerNameLabel ? "middle" : undefined}
iconOnly={props.iconOnly}
suffixWidth={providerNameLabel ? 96 : undefined}
suffix={
providerNameLabel ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{shortLabel}
{summary ? ` · ${summary}` : ""}
</Text>
) : summary ? (
<Text
className={
props.size === "large"
? "text-sm text-foreground-muted"
: "text-xs text-foreground-muted"
}
numberOfLines={1}
>
{summary}
</Text>
) : undefined
}
maxWidth="100%"
onPress={props.onPress}
/>
<>
{/* Unconstrained copy measures whether the full labels fit. */}
<View
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
style={{ position: "absolute", left: 0, top: 0, width: 10_000, opacity: 0 }}
>
<View
className="self-start"
onLayout={(event) => setFullWidth(event.nativeEvent.layout.width)}
>
{renderControl(true)}
</View>
</View>
{renderControl(false)}
</>
);
}
7 changes: 6 additions & 1 deletion apps/mobile/src/components/ComposerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function ComposerInlineControl(props: {
readonly labelEllipsizeMode?: ComponentProps<typeof Text>["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"];
Expand Down Expand Up @@ -104,7 +106,10 @@ export function ComposerInlineControl(props: {
)}
{!props.iconOnly && props.suffix ? (
<View
className={cn("min-w-0", props.suffixWidth === undefined ? "shrink" : "shrink-0")}
className={cn(
"min-w-0",
props.suffixFixed || props.suffixWidth !== undefined ? "shrink-0" : "shrink",
)}
style={{ width: props.suffixWidth }}
>
{props.suffix}
Expand Down
47 changes: 36 additions & 11 deletions apps/mobile/src/features/threads/ThreadComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() => ({
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 = (
<View className="min-w-0 shrink">
<View
className={fitsModelToolbar ? "min-w-0 flex-1 flex-row justify-end" : "min-w-0 shrink"}
onLayout={
fitsModelToolbar
? (event) => setModelToolbarWidth(event.nativeEvent.layout.width)
: undefined
}
>
<ComposerModelControl
size={usesLargeControls ? "large" : "regular"}
modelOption={currentModelOption}
Expand All @@ -830,7 +850,9 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
reportedSelection={props.reportedModelSelection}
prompt={draftMessage}
surfaceColor={composerPanel}
iconOnly={!compactEditor.isExpanded}
iconOnly={Platform.OS !== "android" && !compactEditor.isExpanded}
compact={Platform.OS === "android" && !isExpanded}
fitWidth={fitsModelToolbar && modelToolbarWidth > 0 ? modelToolbarWidth : undefined}
onPress={openSettings}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/>
</View>
Expand Down Expand Up @@ -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,
Expand Down
54 changes: 54 additions & 0 deletions packages/client-runtime/src/modelTraits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderOptionDescriptor>) {
return buildTraitsTriggerDisplay({
descriptors,
primarySelectDescriptorId: "reasoningEffort",
ultrathinkPromptControlled: false,
});
}

describe("buildTraitsTriggerDisplay", () => {
it.each([
{ currentValue: undefined, defaultValue: undefined, value: null, label: "Thinking" },
Expand Down Expand Up @@ -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,
});
});
});
Loading
Loading