diff --git a/apps/mobile/src/components/ComposerContextRing.tsx b/apps/mobile/src/components/ComposerContextRing.tsx new file mode 100644 index 0000000000000..ed0fa3a5ceae5 --- /dev/null +++ b/apps/mobile/src/components/ComposerContextRing.tsx @@ -0,0 +1,51 @@ +import Svg, { Circle } from "react-native-svg"; +import { withUniwind } from "uniwind"; + +const ThemedCircle = withUniwind(Circle); + +/** + * How full the thread's context window is, drawn as a ring inside a round composer button. + * `percent` is 0-100. The ring takes the colour of `colorClassName`; past 90 the arc turns danger. + */ +export function ComposerContextRing(props: { + readonly size: number; + readonly percent: number; + readonly colorClassName: string; +}) { + const used = Math.max(0, Math.min(100, props.percent)); + const strokeWidth = Math.max(2, props.size * 0.075); + const radius = props.size / 2 - strokeWidth * 1.1; + const circumference = 2 * Math.PI * radius; + const center = props.size / 2; + return ( + + + 90 ? "accent-danger-foreground" : props.colorClassName} + stroke="currentColor" + strokeWidth={strokeWidth} + strokeLinecap="round" + strokeDasharray={`${circumference} ${circumference}`} + strokeDashoffset={circumference * (1 - used / 100)} + /> + + ); +} diff --git a/apps/mobile/src/components/ComposerToolbar.tsx b/apps/mobile/src/components/ComposerToolbar.tsx index a901d6fa60b50..c8191c5dac259 100644 --- a/apps/mobile/src/components/ComposerToolbar.tsx +++ b/apps/mobile/src/components/ComposerToolbar.tsx @@ -18,6 +18,7 @@ import Animated, { FadeIn, useReducedMotion } from "react-native-reanimated"; import { cn } from "../lib/cn"; import { AppText as Text } from "./ui/AppText"; import { SymbolView } from "./ui/AppSymbol"; +import { ComposerContextRing } from "./ComposerContextRing"; import { useAndroidControlSizing } from "./ui/useAndroidControlSizing"; const COMPOSER_TOOLBAR_GAP = 8; @@ -290,6 +291,8 @@ export function ComposerActionButton(props: { readonly icon: ComponentProps["name"]; readonly onPress: () => void; readonly variant?: "primary" | "danger"; + /** Context window used, 0-100, drawn as a ring inside the button; null draws none. */ + readonly contextPercent?: number | null; // Menu injects onLongPress on Android and onTouchStart/onPress on iOS. readonly onLongPress?: PressableProps["onLongPress"]; readonly onTouchStart?: PressableProps["onTouchStart"]; @@ -320,6 +323,19 @@ export function ComposerActionButton(props: { : "bg-primary", )} > + {props.contextPercent != null ? ( + + ) : null} {/* Fade the icon as one layer: a translucent tint, or opacity on a Tabler icon (which copies it onto every path), stacks where the SVG strokes overlap. */} = []; export interface ThreadComposerProps { + /** The thread's context window use, drawn as a ring in the send and stop buttons. */ + readonly contextWindow?: ContextWindowSnapshot | null; readonly isDraft?: boolean; readonly canOperateThread: boolean; readonly placeholder: string; @@ -216,6 +219,7 @@ function SendActionButton(props: { readonly accessibilityLabel: string; readonly presentation: ComposerSendPresentation; readonly disabled: boolean; + readonly contextPercent?: number | null; readonly onSend: (followUp?: ActiveTurnComposerAction) => void; }) { const { presentation } = props; @@ -228,6 +232,7 @@ function SendActionButton(props: { icon={presentation.icon} variant="primary" disabled={props.disabled} + contextPercent={props.contextPercent} onPress={() => props.onSend()} /> ); @@ -258,6 +263,7 @@ function SendActionButton(props: { export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps) { const gt = useGT(); + const contextPercent = props.contextWindow?.usedPercentage ?? null; const messages = useMessages(); const project = useProject(scopeProjectRef(props.environmentId, props.selectedThread.projectId)); const { themeVariables: materialTheme } = useAppearancePreferences(); @@ -1056,6 +1062,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps icon="stop.fill" variant="danger" disabled={!props.canOperateThread} + contextPercent={contextPercent} onPress={props.onStopThread} /> ) : showResumeAction ? ( @@ -1072,6 +1079,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps accessibilityLabel={sendBlockedReason ?? sendLabel} presentation={sendPresentation} disabled={!canSend} + contextPercent={contextPercent} onSend={handleSend} /> )} @@ -1124,6 +1132,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps icon="stop.fill" variant="danger" disabled={!props.canOperateThread} + contextPercent={contextPercent} onPress={props.onStopThread} /> ) : showResumeAction && voicePresentation.showsSend ? ( @@ -1140,6 +1149,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps accessibilityLabel={sendBlockedReason ?? sendLabel} presentation={sendPresentation} disabled={!canSend} + contextPercent={contextPercent} onSend={handleSend} /> ) : null} diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 0a1ceab3ce688..6c86a2bf3ff98 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -15,6 +15,7 @@ import { mobilePreferencesAtom } from "../../state/preferences"; import { parseThreadKey, threadKey } from "@t3tools/client-runtime/state/entities"; import { createThreadVisitTracker } from "@t3tools/client-runtime/state/thread-visits"; import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities"; +import { useThreadContextWindow } from "../../state/use-thread-detail"; import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard"; import { useFocusEffect, useNavigation } from "@react-navigation/native"; import { @@ -434,6 +435,7 @@ function ThreadDetailBody( const openInspector = useOpenThreadInspector(); const { session: voiceInputSession } = useGlobalVoiceInput(); const reportedModelSelection = useThreadReportedModelSelection(props.selectedThreadDetailRef); + const contextWindow = useThreadContextWindow(props.selectedThreadDetailRef); const deviceState = useEnvironmentQuery( deviceEnvironment.state({ environmentId: props.environmentId, input: {} }), ); @@ -1919,6 +1921,7 @@ function ThreadDetailBody( ) : ( <> { + if (structure === null) return null; + const { projection } = structure; + const providerThread = projection.providerThreads.find( + (thread) => thread.id === projection.thread.activeProviderThreadId, + ); + return deriveLatestContextWindowSnapshot( + projection.visibleTurnItems, + latestProviderTurnTokenUsage(projection.providerTurns, projection, providerThread), + providerThread, + ); + }, [structure]); +} + export function useSelectedThreadProjection(): EnvironmentThread | null { const { selectedThreadDetailRef } = useThreadSelection(); return useThreadProjection({ diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 10cbb215c9e0f..8803da403f151 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3821,6 +3821,7 @@ export default function ChatView(props: ChatViewProps) { ) : null} {!composerMounted ? null : (