Skip to content
TypeScript

perf(web): ChatComposer compiles with no manual memoization (#20397)

Julius Marmingee925fe754e8e
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index e664744b105..feffd5d48f5 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -7,6 +7,7 @@
export const REACT_COMPILED_FILES = [
"apps/web/src/components/PersistentThreadTerminals.tsx",
"apps/web/src/components/Sidebar.tsx",
+ "apps/web/src/components/chat/ChatComposer.tsx",
"apps/web/src/components/chat/usePendingThreadRequests.ts",
"apps/web/src/components/chat/composerFocusRedirect.ts",
"apps/web/src/components/chat/useChatShortcuts.ts",
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx
index d46eabae8f2..120b3677608 100644
--- a/apps/web/src/components/chat/ChatComposer.tsx
+++ b/apps/web/src/components/chat/ChatComposer.tsx
@@ -21,7 +21,6 @@ import {
changeQuestionAttachmentPreparation,
} from "../../questionAttachments";
import type {
- ApprovalRequestId,
KeybindingCommand,
AssistantCitation,
ChatAttachment as ContractChatAttachment,
@@ -53,7 +52,7 @@ import {
PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS,
} from "@t3tools/contracts";
import type { EnvironmentConnectionPresentation } from "@t3tools/client-runtime/connection";
-import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
+import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import * as Schema from "effect/Schema";
import {
isPasteAsTextShortcut,
@@ -69,13 +68,11 @@ import {
memo,
type ComponentProps,
type ReactNode,
- useCallback,
useEffect,
useEffectEvent,
useId,
useImperativeHandle,
useLayoutEffect,
- useMemo,
useRef,
useState,
useSyncExternalStore,
@@ -362,13 +359,10 @@ import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";
import { observeResize } from "~/lib/observeResize";
function ComposerVideoThumbnail({ file }: { file: File }) {
- const setVideo = useCallback(
- (video: HTMLVideoElement | null) => {
- if (!video) return;
- return attachVideoThumbnail(video, file);
- },
- [file],
- );
+ const setVideo = (video: HTMLVideoElement | null) => {
+ if (!video) return;
+ return attachVideoThumbnail(video, file);
+ };
return (
<video
@@ -480,7 +474,7 @@ function useComposerRestingTransition(
const transitionLayoutRequestRef = useRef(0);
const hasCompletedInitialLayoutRef = useRef(false);
- const clearOverlayPin = useCallback(() => {
+ const clearOverlayPin = () => {
// The overlay belongs to the chat view and outlives this composer, so it
// is remembered from pin time rather than re-resolved through a ref that
// React may already have detached during unmount.
@@ -490,9 +484,9 @@ function useComposerRestingTransition(
overlay?.style.removeProperty("display");
overlay?.style.removeProperty("flex-direction");
overlay?.style.removeProperty("justify-content");
- }, []);
+ };
- const clearTransitionStyles = useCallback(() => {
+ const clearTransitionStyles = () => {
const element = elementRef.current;
const footer = element?.querySelector<HTMLElement>('[data-chat-composer-footer="true"]');
element?.style.removeProperty("overflow");
@@ -524,361 +518,351 @@ function useComposerRestingTransition(
modelStrip?.style.removeProperty(property);
}
clearOverlayPin();
- }, [clearOverlayPin]);
+ };
- isCollapsedRef.current = isCollapsed;
+ useLayoutEffect(() => {
+ isCollapsedRef.current = isCollapsed;
+ });
- const transitionToCurrentGeometry = useCallback(
- (stateChanged: boolean) => {
- const element = elementRef.current;
- const surface = element?.querySelector<HTMLElement>('[data-chat-composer-surface="true"]');
- if (!element || !surface) return;
+ const transitionToCurrentGeometry = (stateChanged: boolean) => {
+ const element = elementRef.current;
+ const surface = element?.querySelector<HTMLElement>('[data-chat-composer-surface="true"]');
+ if (!element || !surface) return;
- const nextIsCollapsed = isCollapsedRef.current;
+ const nextIsCollapsed = isCollapsedRef.current;
- const visibleTransitionElement = (selector: string) =>
- Array.from(element.querySelectorAll<HTMLElement>(selector)).find(
- (candidate) => candidate.getClientRects().length > 0,
- ) ?? null;
- const prompt = visibleTransitionElement(
- '[data-composer-prompt-surface="true"], [data-chat-composer-transition-prompt="true"]',
- );
- const action = visibleTransitionElement('[data-chat-composer-transition-actions="true"]');
- const footer = element.querySelector<HTMLElement>('[data-chat-composer-footer="true"]');
- const continuousControls =
- element.dataset.inlineRestingControls === "true" ||
- element.dataset.modelOnlyStrip === "true";
- const controls = nextIsCollapsed
- ? restingControlsRef.current
- : element.querySelector<HTMLElement>('[data-chat-composer-controls="left"]');
-
- const shell = element.closest<HTMLElement>('[data-slot="composer-shell"]');
- const modelStrip = shell?.querySelector<HTMLElement>('[data-composer-model-strip="true"]');
- const interruptedStripHeight =
- animationRef.current && modelStrip ? modelStrip.getBoundingClientRect().height : null;
- const interruptedAnimation = animationRef.current;
- const interruptedPromptTop = interruptedAnimation
- ? (prompt?.getBoundingClientRect().top ?? null)
- : null;
- const interruptedActionTop = interruptedAnimation
- ? (action?.getBoundingClientRect().top ?? null)
- : null;
- const interruptedControlsLeft = interruptedAnimation
- ? (controls?.getBoundingClientRect().left ?? null)
- : null;
- const interruptedControlsTop = interruptedAnimation
- ? (controls?.getBoundingClientRect().top ?? null)
- : null;
- const interruptedHeight = interruptedAnimation
- ? element.getBoundingClientRect().height
+ const visibleTransitionElement = (selector: string) =>
+ Array.from(element.querySelectorAll<HTMLElement>(selector)).find(
+ (candidate) => candidate.getClientRects().length > 0,
+ ) ?? null;
+ const prompt = visibleTransitionElement(
+ '[data-composer-prompt-surface="true"], [data-chat-composer-transition-prompt="true"]',
+ );
+ const action = visibleTransitionElement('[data-chat-composer-transition-actions="true"]');
+ const footer = element.querySelector<HTMLElement>('[data-chat-composer-footer="true"]');
+ const continuousControls =
+ element.dataset.inlineRestingControls === "true" || element.dataset.modelOnlyStrip === "true";
+ const controls = nextIsCollapsed
+ ? restingControlsRef.current
+ : element.querySelector<HTMLElement>('[data-chat-composer-controls="left"]');
+
+ const shell = element.closest<HTMLElement>('[data-slot="composer-shell"]');
+ const modelStrip = shell?.querySelector<HTMLElement>('[data-composer-model-strip="true"]');
+ const interruptedStripHeight =
+ animationRef.current && modelStrip ? modelStrip.getBoundingClientRect().height : null;
+ const interruptedAnimation = animationRef.current;
+ const interruptedPromptTop = interruptedAnimation
+ ? (prompt?.getBoundingClientRect().top ?? null)
+ : null;
+ const interruptedActionTop = interruptedAnimation
+ ? (action?.getBoundingClientRect().top ?? null)
+ : null;
+ const interruptedControlsLeft = interruptedAnimation
+ ? (controls?.getBoundingClientRect().left ?? null)
+ : null;
+ const interruptedControlsTop = interruptedAnimation
+ ? (controls?.getBoundingClientRect().top ?? null)
+ : null;
+ const interruptedHeight = interruptedAnimation ? element.getBoundingClientRect().height : null;
+ const interruptedTargetHeight = animationTargetHeightRef.current;
+ const interruptedCurrentTime =
+ typeof interruptedAnimation?.currentTime === "number"
+ ? interruptedAnimation.currentTime
: null;
- const interruptedTargetHeight = animationTargetHeightRef.current;
- const interruptedCurrentTime =
- typeof interruptedAnimation?.currentTime === "number"
- ? interruptedAnimation.currentTime
- : null;
- const interruptedDuration = interruptedAnimation?.effect?.getComputedTiming().duration;
- if (transitionCleanupTimeoutRef.current !== null) {
- window.clearTimeout(transitionCleanupTimeoutRef.current);
- transitionCleanupTimeoutRef.current = null;
- }
- interruptedAnimation?.cancel();
- animationRef.current = null;
- for (const animation of contentAnimationsRef.current) animation.cancel();
- contentAnimationsRef.current = [];
- if (stateChanged) {
- for (const animation of stateChangeAnimationsRef.current) animation.cancel();
- stateChangeAnimationsRef.current = [];
- }
- clearTransitionStyles();
+ const interruptedDuration = interruptedAnimation?.effect?.getComputedTiming().duration;
+ if (transitionCleanupTimeoutRef.current !== null) {
+ window.clearTimeout(transitionCleanupTimeoutRef.current);
+ transitionCleanupTimeoutRef.current = null;
+ }
+ interruptedAnimation?.cancel();
+ animationRef.current = null;
+ for (const animation of contentAnimationsRef.current) animation.cancel();
+ contentAnimationsRef.current = [];
+ if (stateChanged) {
+ for (const animation of stateChangeAnimationsRef.current) animation.cancel();
+ stateChangeAnimationsRef.current = [];
+ }
+ clearTransitionStyles();
+
+ const nextRect = element.getBoundingClientRect();
+ const nextHeight = nextRect.height;
+ const nextModelStripHeight = modelStrip?.getBoundingClientRect().height ?? null;
+ // The chat view resize-observes the overlay to place the timeline
+ // inset, the scroll-to-end pill, and the mini player. Publishing the
+ // destination height here turns that feedback into one update instead
+ // of a ChatView re-render on every animation frame.
+ const overlay = element.closest<HTMLElement>('[data-chat-composer-overlay="true"]');
+ const overlayRect = overlay?.getBoundingClientRect();
+ const overlayHeight = overlayRect?.height ?? null;
+ if (overlayHeight !== null) {
+ onOverlayHeightChange(overlayHeight);
+ }
+ const nextPromptRect = prompt?.getBoundingClientRect() ?? null;
+ const nextPromptTop = nextPromptRect?.top ?? null;
+ const nextActionTop = action?.getBoundingClientRect().top ?? null;
+ const nextControlsRect = controls?.getBoundingClientRect() ?? null;
+ const nextControlsTop = nextControlsRect?.top ?? null;
+ const footerBottom = footer ? nextRect.bottom - footer.getBoundingClientRect().bottom : 1;
+ const previousHeight = interruptedHeight ?? previousHeightRef.current;
+ const targetChanged =
+ interruptedTargetHeight === null || Math.abs(interruptedTargetHeight - nextHeight) >= 0.5;
+ const shouldAnimate = shouldAnimateComposerRestingTransition({
+ hasCompletedInitialLayout: hasCompletedInitialLayoutRef.current,
+ stateChanged,
+ hasInterruptedAnimation: interruptedHeight !== null,
+ });
- const nextRect = element.getBoundingClientRect();
- const nextHeight = nextRect.height;
- const nextModelStripHeight = modelStrip?.getBoundingClientRect().height ?? null;
- // The chat view resize-observes the overlay to place the timeline
- // inset, the scroll-to-end pill, and the mini player. Publishing the
- // destination height here turns that feedback into one update instead
- // of a ChatView re-render on every animation frame.
- const overlay = element.closest<HTMLElement>('[data-chat-composer-overlay="true"]');
- const overlayRect = overlay?.getBoundingClientRect();
- const overlayHeight = overlayRect?.height ?? null;
- if (overlayHeight !== null) {
- onOverlayHeightChange(overlayHeight);
+ if (
+ shouldAnimate &&
+ animationsActive &&
+ previousHeight !== null &&
+ Math.abs(previousHeight - nextHeight) >= 0.5
+ ) {
+ const remainingDuration =
+ typeof interruptedDuration === "number" && interruptedCurrentTime !== null
+ ? Math.max(1, interruptedDuration - interruptedCurrentTime)
+ : animationDurationMs;
+ const duration =
+ interruptedHeight !== null && !targetChanged ? remainingDuration : animationDurationMs;
+ element.style.overflow = "clip";
+ if (modelStrip) {
+ const controlsClearance = Math.max(
+ nextModelStripHeight ?? 0,
+ previousModelStripHeightRef.current ?? 0,
+ );
+ element.style.overflowClipMargin = `${controlsClearance}px`;
+ element.style.clipPath = `inset(0 0 -${controlsClearance}px 0)`;
+ }
+ surface.style.height = "100%";
+
+ if (overlay && overlayHeight !== null) {
+ overlay.style.height = `${String(overlayHeight)}px`;
+ overlay.style.display = "flex";
+ overlay.style.flexDirection = "column";
+ overlay.style.justifyContent = "flex-end";
+ pinnedOverlayRef.current = overlay;
}
- const nextPromptRect = prompt?.getBoundingClientRect() ?? null;
- const nextPromptTop = nextPromptRect?.top ?? null;
- const nextActionTop = action?.getBoundingClientRect().top ?? null;
- const nextControlsRect = controls?.getBoundingClientRect() ?? null;
- const nextControlsTop = nextControlsRect?.top ?? null;
- const footerBottom = footer ? nextRect.bottom - footer.getBoundingClientRect().bottom : 1;
- const previousHeight = interruptedHeight ?? previousHeightRef.current;
- const targetChanged =
- interruptedTargetHeight === null || Math.abs(interruptedTargetHeight - nextHeight) >= 0.5;
- const shouldAnimate = shouldAnimateComposerRestingTransition({
- hasCompletedInitialLayout: hasCompletedInitialLayoutRef.current,
- stateChanged,
- hasInterruptedAnimation: interruptedHeight !== null,
- });
-
- if (
- shouldAnimate &&
- animationsActive &&
- previousHeight !== null &&
- Math.abs(previousHeight - nextHeight) >= 0.5
- ) {
- const remainingDuration =
- typeof interruptedDuration === "number" && interruptedCurrentTime !== null
- ? Math.max(1, interruptedDuration - interruptedCurrentTime)
- : animationDurationMs;
- const duration =
- interruptedHeight !== null && !targetChanged ? remainingDuration : animationDurationMs;
- element.style.overflow = "clip";
- if (modelStrip) {
- const controlsClearance = Math.max(
- nextModelStripHeight ?? 0,
- previousModelStripHeightRef.current ?? 0,
- );
- element.style.overflowClipMargin = `${controlsClearance}px`;
- element.style.clipPath = `inset(0 0 -${controlsClearance}px 0)`;
- }
- surface.style.height = "100%";
-
- if (overlay && overlayHeight !== null) {
- overlay.style.height = `${String(overlayHeight)}px`;
- overlay.style.display = "flex";
- overlay.style.flexDirection = "column";
- overlay.style.justifyContent = "flex-end";
- pinnedOverlayRef.current = overlay;
- }
- if (footer) {
- footer.style.position = "absolute";
- footer.style.top = "auto";
- footer.style.bottom = `${String(footerBottom)}px`;
- footer.style.height = "3rem";
- if (nextIsCollapsed) {
- footer.style.left = "auto";
- footer.style.right = "1px";
- } else {
- footer.style.left = "1px";
- footer.style.right = "1px";
- }
+ if (footer) {
+ footer.style.position = "absolute";
+ footer.style.top = "auto";
+ footer.style.bottom = `${String(footerBottom)}px`;
+ footer.style.height = "3rem";
+ if (nextIsCollapsed) {
+ footer.style.left = "auto";
+ footer.style.right = "1px";
+ } else {
+ footer.style.left = "1px";
+ footer.style.right = "1px";
}
+ }
- let stripAnimation: Animation | null = null;
- if (modelStrip) {
- const stripHeight = modelStrip.getBoundingClientRect().height;
- const stripOverlap = -Number.parseFloat(getComputedStyle(modelStrip).marginTop);
- const fromHeight =
- interruptedStripHeight ??
- (previousCollapsedRef.current
- ? (previousModelStripHeightRef.current ?? stripHeight)
- : stripOverlap);
- const toHeight = nextIsCollapsed ? stripHeight : stripOverlap;
- modelStrip.style.position = "relative";
- modelStrip.style.top = "auto";
- modelStrip.style.visibility = "visible";
- modelStrip.style.minHeight = "0";
- modelStrip.style.paddingTop = "0";
- modelStrip.style.alignItems = "flex-end";
- modelStrip.style.pointerEvents = "none";
- modelStrip.style.zIndex = "20";
- shell?.setAttribute("data-model-strip-transition", "true");
- stripAnimation = modelStrip.animate(
- [{ height: `${fromHeight}px` }, { height: `${toHeight}px` }],
- { duration, easing: COMPOSER_RESTING_TRANSITION_EASING, fill: "both" },
- );
- }
+ let stripAnimation: Animation | null = null;
+ if (modelStrip) {
+ const stripHeight = modelStrip.getBoundingClientRect().height;
+ const stripOverlap = -Number.parseFloat(getComputedStyle(modelStrip).marginTop);
+ const fromHeight =
+ interruptedStripHeight ??
+ (previousCollapsedRef.current
+ ? (previousModelStripHeightRef.current ?? stripHeight)
+ : stripOverlap);
+ const toHeight = nextIsCollapsed ? stripHeight : stripOverlap;
+ modelStrip.style.position = "relative";
+ modelStrip.style.top = "auto";
+ modelStrip.style.visibility = "visible";
+ modelStrip.style.minHeight = "0";
+ modelStrip.style.paddingTop = "0";
+ modelStrip.style.alignItems = "flex-end";
+ modelStrip.style.pointerEvents = "none";
+ modelStrip.style.zIndex = "20";
+ shell?.setAttribute("data-model-strip-transition", "true");
+ stripAnimation = modelStrip.animate(
+ [{ height: `${fromHeight}px` }, { height: `${toHeight}px` }],
+ { duration, easing: COMPOSER_RESTING_TRANSITION_EASING, fill: "both" },
+ );
+ }
- const animation = element.animate(
- [{ height: `${previousHeight}px` }, { height: `${nextHeight}px` }],
- {
- duration,
- easing: COMPOSER_RESTING_TRANSITION_EASING,
- fill: "both",
- },
+ const animation = element.animate(
+ [{ height: `${previousHeight}px` }, { height: `${nextHeight}px` }],
+ {
+ duration,
+ easing: COMPOSER_RESTING_TRANSITION_EASING,
+ fill: "both",
+ },
+ );
+ animationRef.current = animation;
+ animationTargetHeightRef.current = nextHeight;
+
+ const animatedRect = element.getBoundingClientRect();
+ const previousBottom =
+ overlayRect && previousContentOffsetsRef.current.surfaceBottomInset !== null
+ ? overlayRect.bottom - previousContentOffsetsRef.current.surfaceBottomInset
+ : animatedRect.bottom;
+ const bottomShift = previousBottom - animatedRect.bottom;
+ const previousPromptTop =
+ interruptedPromptTop ??
+ (previousContentOffsetsRef.current.promptFromTop === null
+ ? null
+ : animatedRect.top + bottomShift + previousContentOffsetsRef.current.promptFromTop);
+ const previousActionTop =
+ interruptedActionTop ??
+ (previousContentOffsetsRef.current.actionFromBottom === null
+ ? null
+ : previousBottom - previousContentOffsetsRef.current.actionFromBottom);
+ const contentAnimations: Animation[] = stripAnimation ? [stripAnimation] : [];
+ const animateContentPosition = (
+ content: HTMLElement | null,
+ previousTop: number | null,
+ previousLeft: number | null = null,
+ ) => {
+ if (!content || previousTop === null) return;
+ const rect = content.getBoundingClientRect();
+ const offset = previousTop - rect.top;
+ const offsetX = previousLeft === null ? 0 : previousLeft - rect.left;
+ if (Math.abs(offset) < 0.5 && Math.abs(offsetX) < 0.5) return;
+ contentAnimations.push(
+ content.animate(
+ [{ transform: `translate(${offsetX}px, ${offset}px)` }, { transform: "none" }],
+ {
+ duration,
+ easing: COMPOSER_RESTING_TRANSITION_EASING,
+ },
+ ),
);
- animationRef.current = animation;
- animationTargetHeightRef.current = nextHeight;
-
- const animatedRect = element.getBoundingClientRect();
- const previousBottom =
- overlayRect && previousContentOffsetsRef.current.surfaceBottomInset !== null
- ? overlayRect.bottom - previousContentOffsetsRef.current.surfaceBottomInset
- : animatedRect.bottom;
- const bottomShift = previousBottom - animatedRect.bottom;
- const previousPromptTop =
- interruptedPromptTop ??
- (previousContentOffsetsRef.current.promptFromTop === null
+ };
+ animateContentPosition(prompt, previousPromptTop);
+ animateContentPosition(action, previousActionTop);
+ if (continuousControls) {
+ const previousControlsTop =
+ interruptedControlsTop ??
+ (previousContentOffsetsRef.current.controlsFromBottom === null
? null
- : animatedRect.top + bottomShift + previousContentOffsetsRef.current.promptFromTop);
- const previousActionTop =
- interruptedActionTop ??
- (previousContentOffsetsRef.current.actionFromBottom === null
+ : previousBottom - previousContentOffsetsRef.current.controlsFromBottom);
+ const previousControlsLeft =
+ interruptedControlsLeft ??
+ (previousContentOffsetsRef.current.controlsFromLeft === null
? null
- : previousBottom - previousContentOffsetsRef.current.actionFromBottom);
- const contentAnimations: Animation[] = stripAnimation ? [stripAnimation] : [];
- const animateContentPosition = (
- content: HTMLElement | null,
- previousTop: number | null,
- previousLeft: number | null = null,
- ) => {
- if (!content || previousTop === null) return;
- const rect = content.getBoundingClientRect();
- const offset = previousTop - rect.top;
- const offsetX = previousLeft === null ? 0 : previousLeft - rect.left;
- if (Math.abs(offset) < 0.5 && Math.abs(offsetX) < 0.5) return;
- contentAnimations.push(
- content.animate(
- [{ transform: `translate(${offsetX}px, ${offset}px)` }, { transform: "none" }],
+ : animatedRect.left + previousContentOffsetsRef.current.controlsFromLeft);
+ animateContentPosition(controls, previousControlsTop, previousControlsLeft);
+ }
+ contentAnimationsRef.current = contentAnimations;
+
+ if (stateChanged) {
+ const stateChangeAnimations: Animation[] = [];
+
+ const previousPromptHeight = previousContentOffsetsRef.current.promptHeight;
+ if (
+ !nextIsCollapsed &&
+ prompt &&
+ nextPromptRect &&
+ previousPromptHeight !== null &&
+ nextPromptRect.height - previousPromptHeight >= 0.5
+ ) {
+ const hiddenHeight = nextPromptRect.height - previousPromptHeight;
+ stateChangeAnimations.push(
+ prompt.animate(
+ [
+ { clipPath: `inset(0 0 ${String(hiddenHeight)}px 0)` },
+ { clipPath: "inset(0 0 0 0)" },
+ ],
{
duration,
easing: COMPOSER_RESTING_TRANSITION_EASING,
},
),
);
- };
- animateContentPosition(prompt, previousPromptTop);
- animateContentPosition(action, previousActionTop);
- if (continuousControls) {
- const previousControlsTop =
- interruptedControlsTop ??
- (previousContentOffsetsRef.current.controlsFromBottom === null
- ? null
- : previousBottom - previousContentOffsetsRef.current.controlsFromBottom);
- const previousControlsLeft =
- interruptedControlsLeft ??
- (previousContentOffsetsRef.current.controlsFromLeft === null
- ? null
- : animatedRect.left + previousContentOffsetsRef.current.controlsFromLeft);
- animateContentPosition(controls, previousControlsTop, previousControlsLeft);
}
- contentAnimationsRef.current = contentAnimations;
-
- if (stateChanged) {
- const stateChangeAnimations: Animation[] = [];
-
- const previousPromptHeight = previousContentOffsetsRef.current.promptHeight;
- if (
- !nextIsCollapsed &&
- prompt &&
- nextPromptRect &&
- previousPromptHeight !== null &&
- nextPromptRect.height - previousPromptHeight >= 0.5
- ) {
- const hiddenHeight = nextPromptRect.height - previousPromptHeight;
- stateChangeAnimations.push(
- prompt.animate(
- [
- { clipPath: `inset(0 0 ${String(hiddenHeight)}px 0)` },
- { clipPath: "inset(0 0 0 0)" },
- ],
- {
- duration,
- easing: COMPOSER_RESTING_TRANSITION_EASING,
- },
- ),
- );
- }
-
- const arrivingControls = nextIsCollapsed
- ? restingControlsRef.current
- : element.querySelector<HTMLElement>('[data-chat-composer-controls="left"]');
- if (arrivingControls && !continuousControls) {
- const drift = nextIsCollapsed
- ? -COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX
- : COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX;
- stateChangeAnimations.push(
- arrivingControls.animate(
- [
- { opacity: 0, transform: `translateY(${String(drift)}px)` },
- { opacity: 1, transform: "none" },
- ],
- {
- duration: nextIsCollapsed ? duration : duration / 2,
- delay: nextIsCollapsed ? 0 : duration / 2,
- fill: "backwards",
- easing: COMPOSER_RESTING_TRANSITION_EASING,
- },
- ),
- );
- }
- const arrivingImagePreviews = nextIsCollapsed
- ? Array.from(
- element.querySelectorAll<HTMLElement>('[data-chat-composer-resting-images="true"]'),
- )
- : Array.from(
- element.querySelectorAll<HTMLElement>('[data-chat-composer-expanded-image="true"]'),
- );
- for (const imagePreview of arrivingImagePreviews) {
- stateChangeAnimations.push(
- imagePreview.animate([{ opacity: 0 }, { opacity: 1 }], {
+ const arrivingControls = nextIsCollapsed
+ ? restingControlsRef.current
+ : element.querySelector<HTMLElement>('[data-chat-composer-controls="left"]');
+ if (arrivingControls && !continuousControls) {
+ const drift = nextIsCollapsed
+ ? -COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX
+ : COMPOSER_RESTING_CONTROLS_ARRIVAL_DRIFT_PX;
+ stateChangeAnimations.push(
+ arrivingControls.animate(
+ [
+ { opacity: 0, transform: `translateY(${String(drift)}px)` },
+ { opacity: 1, transform: "none" },
+ ],
+ {
duration: nextIsCollapsed ? duration : duration / 2,
delay: nextIsCollapsed ? 0 : duration / 2,
fill: "backwards",
easing: COMPOSER_RESTING_TRANSITION_EASING,
- }),
+ },
+ ),
+ );
+ }
+
+ const arrivingImagePreviews = nextIsCollapsed
+ ? Array.from(
+ element.querySelectorAll<HTMLElement>('[data-chat-composer-resting-images="true"]'),
+ )
+ : Array.from(
+ element.querySelectorAll<HTMLElement>('[data-chat-composer-expanded-image="true"]'),
);
- }
- stateChangeAnimationsRef.current = stateChangeAnimations;
+ for (const imagePreview of arrivingImagePreviews) {
+ stateChangeAnimations.push(
+ imagePreview.animate([{ opacity: 0 }, { opacity: 1 }], {
+ duration: nextIsCollapsed ? duration : duration / 2,
+ delay: nextIsCollapsed ? 0 : duration / 2,
+ fill: "backwards",
+ easing: COMPOSER_RESTING_TRANSITION_EASING,
+ }),
+ );
}
+ stateChangeAnimationsRef.current = stateChangeAnimations;
+ }
- const finishTransition = (cancelAnimations: boolean) => {
- if (animationRef.current !== animation) return;
- if (transitionCleanupTimeoutRef.current !== null) {
- window.clearTimeout(transitionCleanupTimeoutRef.current);
- transitionCleanupTimeoutRef.current = null;
+ const finishTransition = (cancelAnimations: boolean) => {
+ if (animationRef.current !== animation) return;
+ if (transitionCleanupTimeoutRef.current !== null) {
+ window.clearTimeout(transitionCleanupTimeoutRef.current);
+ transitionCleanupTimeoutRef.current = null;
+ }
+ if (cancelAnimations) {
+ animation.cancel();
+ for (const contentAnimation of contentAnimationsRef.current) {
+ contentAnimation.cancel();
}
- if (cancelAnimations) {
- animation.cancel();
- for (const contentAnimation of contentAnimationsRef.current) {
- contentAnimation.cancel();
- }
- for (const stateChangeAnimation of stateChangeAnimationsRef.current) {
- stateChangeAnimation.cancel();
- }
+ for (const stateChangeAnimation of stateChangeAnimationsRef.current) {
+ stateChangeAnimation.cancel();
}
- animationRef.current = null;
- animationTargetHeightRef.current = null;
- contentAnimationsRef.current = [];
- stateChangeAnimationsRef.current = [];
- animation.cancel();
- stripAnimation?.cancel();
- clearTransitionStyles();
- };
- void animation.finished.catch(() => undefined).then(() => finishTransition(false));
- // A suspended document timeline can leave `finished` pending while
- // these measurement styles remain active. Wall-clock cleanup makes
- // the natural layout the eventual source of truth in that case.
- transitionCleanupTimeoutRef.current = window.setTimeout(
- () => finishTransition(true),
- duration + COMPOSER_RESTING_TRANSITION_CLEANUP_BUFFER_MS,
- );
- } else {
+ }
+ animationRef.current = null;
animationTargetHeightRef.current = null;
- }
-
- previousCollapsedRef.current = nextIsCollapsed;
- previousHeightRef.current = nextHeight;
- previousModelStripHeightRef.current = nextModelStripHeight;
- previousContentOffsetsRef.current = {
- promptFromTop: nextPromptTop === null ? null : nextPromptTop - nextRect.top,
- promptHeight: nextPromptRect?.height ?? null,
- actionFromBottom: nextActionTop === null ? null : nextRect.bottom - nextActionTop,
- controlsFromBottom: nextControlsTop === null ? null : nextRect.bottom - nextControlsTop,
- controlsFromLeft: nextControlsRect === null ? null : nextControlsRect.left - nextRect.left,
- surfaceBottomInset: overlayRect ? overlayRect.bottom - nextRect.bottom : null,
+ contentAnimationsRef.current = [];
+ stateChangeAnimationsRef.current = [];
+ animation.cancel();
+ stripAnimation?.cancel();
+ clearTransitionStyles();
};
- },
- [
- animationDurationMs,
- animationsActive,
- clearTransitionStyles,
- onOverlayHeightChange,
- restingControlsRef,
- ],
- );
+ void animation.finished.catch(() => undefined).then(() => finishTransition(false));
+ // A suspended document timeline can leave `finished` pending while
+ // these measurement styles remain active. Wall-clock cleanup makes
+ // the natural layout the eventual source of truth in that case.
+ transitionCleanupTimeoutRef.current = window.setTimeout(
+ () => finishTransition(true),
+ duration + COMPOSER_RESTING_TRANSITION_CLEANUP_BUFFER_MS,
+ );
+ } else {
+ animationTargetHeightRef.current = null;
+ }
+
+ previousCollapsedRef.current = nextIsCollapsed;
+ previousHeightRef.current = nextHeight;
+ previousModelStripHeightRef.current = nextModelStripHeight;
+ previousContentOffsetsRef.current = {
+ promptFromTop: nextPromptTop === null ? null : nextPromptTop - nextRect.top,
+ promptHeight: nextPromptRect?.height ?? null,
+ actionFromBottom: nextActionTop === null ? null : nextRect.bottom - nextActionTop,
+ controlsFromBottom: nextControlsTop === null ? null : nextRect.bottom - nextControlsTop,
+ controlsFromLeft: nextControlsRect === null ? null : nextControlsRect.left - nextRect.left,
+ surfaceBottomInset: overlayRect ? overlayRect.bottom - nextRect.bottom : null,
+ };
+ };
useLayoutEffect(() => {
const requestId = transitionLayoutRequestRef.current + 1;
@@ -985,10 +969,7 @@ function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children:
useLayoutEffect(() => {
const anchor = props.anchor;
- if (!anchor) {
- setPosition(null);
- return;
- }
+ if (!anchor) return;
const updatePosition = () => {
const form = anchor.closest<HTMLElement>('[data-chat-composer-form="true"]');
@@ -1036,7 +1017,7 @@ function ComposerCommandMenuLayer(props: { anchor: HTMLElement | null; children:
};
}, [props.anchor]);
- if (!position) return null;
+ if (!props.anchor || !position) return null;
return createPortal(
<div
@@ -1059,7 +1040,6 @@ import { Select, SelectItem, SelectPopup, SelectValue } from "../ui/select";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { toastManager } from "../ui/toast";
import {
- FileIcon,
BotIcon,
CircleAlertIcon,
PaperclipIcon,
@@ -1123,6 +1103,22 @@ import type { ReviewCommentContext } from "../../reviewCommentContext";
const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000;
+/** Reads and disarms a paste-as-text deadline: the shortcut applies to one paste only. */
+function consumePasteAsTextShortcut(deadline: { current: number }): boolean {
+ const armed = Date.now() <= deadline.current;
+ deadline.current = 0;
+ return armed;
+}
+
+async function downloadAttachmentBlob(url: string): Promise<Blob | null> {
+ try {
+ const response = await fetch(url, { signal: AbortSignal.timeout(60_000) });
+ return response.ok ? await response.blob() : null;
+ } catch {
+ return null;
+ }
+}
+
const extendReplacementRangeForTrailingSpace = (
text: string,
rangeEnd: number,
@@ -1137,20 +1133,18 @@ const extendReplacementRangeForTrailingSpace = (
function useRestingComposerControlsLayout(host: HTMLDivElement | null, useControlsAsHost = false) {
const [controls, setControls] = useState<HTMLDivElement | null>(null);
const controlsRef = useRef<HTMLDivElement>(null);
- const attachControls = useCallback((element: HTMLDivElement | null) => {
+ const attachControls = (element: HTMLDivElement | null) => {
controlsRef.current = element;
setControls(element);
- }, []);
- const hostRef = useRef(host);
- hostRef.current = host;
+ };
const [layout, setLayout] = useState<{
hiddenCount: number;
iconOnlyCount?: number;
visible: boolean;
}>({ hiddenCount: 0, visible: true });
- const measure = useCallback(() => {
- const currentHost = useControlsAsHost ? controls : hostRef.current;
+ const measure = () => {
+ const currentHost = useControlsAsHost ? controls : host;
if (currentHost === null || !controls) return;
const measurement = measureRestingComposerControls(controls);
@@ -1173,10 +1167,9 @@ function useRestingComposerControlsLayout(host: HTMLDivElement | null, useContro
? current
: next;
});
- }, [controls, useControlsAsHost]);
+ };
- useLayoutEffect(measure, [measure, host]);
- useEffect(() => {
+ useLayoutEffect(() => {
const currentHost = useControlsAsHost ? controls : host;
if (!currentHost || !controls) return;
let stopObserving = () => {};
@@ -1648,6 +1641,213 @@ export interface ChatComposerProps {
onFileOpen: (attachment: ChatFileAttachment) => void;
}
+/**
+ * Keeps the selected provider's workspace snapshot fresh for the composer's
+ * cwd. Returns a check to run when the user types.
+ */
+function useProviderWorkspaceSnapshotRefresh(input: {
+ readonly environmentId: EnvironmentId;
+ readonly gitCwd: string | null;
+ readonly selectedProviderEntry: ProviderInstanceEntry | undefined;
+ readonly selectedProviderStatus: ServerProvider | null;
+}) {
+ const { environmentId, gitCwd, selectedProviderEntry, selectedProviderStatus } = input;
+ const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
+ reportFailure: false,
+ });
+ // The last scan this composer asked for. A request inside the TTL is not
+ // repeated, so a client clock ahead of the server's cannot loop rescans.
+ const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null);
+ const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{
+ key: string;
+ notBefore: number;
+ } | null>(null);
+ const workspaceRefreshScopeKey =
+ gitCwd && selectedProviderEntry
+ ? `${environmentId}:${selectedProviderEntry.instanceId}:${gitCwd}`
+ : null;
+ const workspaceSlashCommandsPending =
+ selectedProviderStatus?.workspaceSnapshots?.some(
+ (snapshot) => snapshot.cwd === gitCwd && snapshot.slashCommandsPending === true,
+ ) ?? false;
+ useEffect(() => {
+ if (
+ !workspaceSlashCommandsPending ||
+ !workspaceRefreshRetry ||
+ workspaceRefreshRetry.key !== workspaceRefreshScopeKey
+ )
+ return;
+ const timeout = setTimeout(
+ () => {
+ setWorkspaceRefreshRetry((current) => (current === workspaceRefreshRetry ? null : current));
+ },
+ Math.max(0, workspaceRefreshRetry.notBefore - Date.now()),
+ );
+ return () => clearTimeout(timeout);
+ }, [workspaceRefreshRetry, workspaceRefreshScopeKey, workspaceSlashCommandsPending]);
+ // Losing a snapshot (the server dropped it) lifts both the TTL and the retry
+ // cooldown so the next check rescans at once.
+ const hasWorkspaceSnapshot = hasCompleteProviderWorkspaceSnapshot(selectedProviderStatus, gitCwd);
+ const [hadWorkspaceSnapshot, setHadWorkspaceSnapshot] = useState(hasWorkspaceSnapshot);
+ const [workspaceSnapshotLossCount, setWorkspaceSnapshotLossCount] = useState(0);
+ if (hadWorkspaceSnapshot !== hasWorkspaceSnapshot) {
+ setHadWorkspaceSnapshot(hasWorkspaceSnapshot);
+ if (!hasWorkspaceSnapshot) {
+ setWorkspaceSnapshotLossCount((count) => count + 1);
+ setWorkspaceRefreshRetry(null);
+ }
+ }
+ const lastWorkspaceSnapshotLossRef = useRef(workspaceSnapshotLossCount);
+ const refreshWorkspaceSnapshotIfStale = (
+ now: number,
+ key: string | null,
+ retry: { key: string; notBefore: number } | null,
+ snapshotLossCount: number,
+ ) => {
+ if (lastWorkspaceSnapshotLossRef.current !== snapshotLossCount) {
+ lastWorkspaceSnapshotLossRef.current = snapshotLossCount;
+ workspaceRefreshKeyRef.current = null;
+ }
+ if (key === null || !gitCwd || !selectedProviderEntry) return;
+ const lastRequest = workspaceRefreshKeyRef.current;
+ if (
+ lastRequest?.key === key &&
+ now - lastRequest.requestedAt < PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS
+ )
+ return;
+ // A pending retry needs no clearing here: its cooldown lapses long before the TTL.
+ if (hasCurrentProviderWorkspaceSnapshot(selectedProviderStatus, gitCwd, now)) return;
+ if (retry?.key === key && now < retry.notBefore) return;
+ const request = { key, requestedAt: now };
+ workspaceRefreshKeyRef.current = request;
+ const retryLater = () => {
+ if (workspaceRefreshKeyRef.current !== request) return;
+ workspaceRefreshKeyRef.current = null;
+ setWorkspaceRefreshRetry({
+ key,
+ notBefore: Date.now() + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS,
+ });
+ };
+ void refreshProviders({
+ environmentId,
+ input: { instanceId: selectedProviderEntry.instanceId, cwd: gitCwd },
+ }).then((result) => {
+ const refreshedSnapshot =
+ result._tag === "Success" &&
+ hasCompleteProviderWorkspaceSnapshot(
+ result.value.providers.find(
+ (provider) => provider.instanceId === selectedProviderEntry.instanceId,
+ ),
+ gitCwd,
+ );
+ if (!refreshedSnapshot) retryLater();
+ }, retryLater);
+ };
+ const refreshWorkspaceSnapshotOnChange = useEffectEvent(refreshWorkspaceSnapshotIfStale);
+ useEffect(() => {
+ refreshWorkspaceSnapshotOnChange(
+ Date.now(),
+ workspaceRefreshScopeKey,
+ workspaceRefreshRetry,
+ workspaceSnapshotLossCount,
+ );
+ }, [workspaceRefreshScopeKey, workspaceRefreshRetry, workspaceSnapshotLossCount]);
+ return useStableCallback(() =>
+ refreshWorkspaceSnapshotIfStale(
+ Date.now(),
+ workspaceRefreshScopeKey,
+ workspaceRefreshRetry,
+ workspaceSnapshotLossCount,
+ ),
+ );
+}
+
+/**
+ * The provider instances the composer offers and which one is selected.
+ * A hook so its results reach the composer frozen: they are passed to plain
+ * functions across later hook calls, which would otherwise keep the compiler
+ * from caching them.
+ */
+function useComposerProviderSelection(
+ input: Omit<Parameters<typeof resolveComposerProviderSelection>[0], "entries"> & {
+ readonly providerStatuses: ReadonlyArray<ServerProvider>;
+ readonly settings: UnifiedSettings;
+ readonly activeProjectId: ProjectId | null;
+ },
+) {
+ // Calls no hooks, so the compiler needs the directive to memoize it.
+ "use memo";
+ const { providerStatuses, settings, activeProjectId, ...selection } = input;
+ const providerInstanceEntries: ReadonlyArray<ProviderInstanceEntry> = sortProviderInstanceEntries(
+ applyProviderInstanceSettings(
+ deriveProviderInstanceEntries(providerStatuses),
+ settings,
+ activeProjectId,
+ ),
+ );
+ return {
+ providerInstanceEntries,
+ ...resolveComposerProviderSelection({ ...selection, entries: providerInstanceEntries }),
+ };
+}
+
+function useModelOptionsByInstance(
+ settings: UnifiedSettings,
+ entries: ReadonlyArray<ProviderInstanceEntry>,
+ selectedInstanceId: ProviderInstanceId,
+ selectedModel: string | null,
+): ReadonlyMap<ProviderInstanceId, ReadonlyArray<AppModelOption>> {
+ // Calls no hooks, so the compiler needs the directive to memoize it.
+ "use memo";
+ const out = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
+ for (const entry of entries) {
+ out.set(
+ entry.instanceId,
+ getAppModelOptionsForInstance(
+ settings,
+ entry,
+ entry.instanceId === selectedInstanceId ? selectedModel : null,
+ ),
+ );
+ }
+ return out;
+}
+
+/** Splits the draft's images into pending SnapShots and standalone images. */
+function useComposerImageGroups(
+ pendingSnapShotAnimations: ReturnType<typeof getPendingSnapShotAnimations>,
+ composerDraftTarget: ScopedThreadRef | DraftId,
+ composerImages: ReadonlyArray<ComposerImageAttachment>,
+ composerPreviewAnnotations: ReadonlyArray<{ readonly id: string }>,
+) {
+ // Calls no hooks, so the compiler needs the directive to memoize it.
+ "use memo";
+ const pendingSnapShotIds = pendingSnapShotAnimationIdsForTarget(
+ pendingSnapShotAnimations,
+ composerDraftTarget,
+ );
+ const previewAnnotationIds = new Set(
+ composerPreviewAnnotations.map((annotation) => annotation.id),
+ );
+ return {
+ pendingSnapShotIds,
+ pendingSnapShotIdSet: new Set(pendingSnapShotIds),
+ uncommittedSnapShotIds: pendingSnapShotIds.filter(
+ (id) => !composerImages.some((image) => image.id === id),
+ ),
+ standaloneComposerImages: composerImages.filter((image) => !previewAnnotationIds.has(image.id)),
+ };
+}
+
+const NO_PROVIDER_SKILLS: ServerProvider["skills"] = [];
+const NO_PROVIDER_SLASH_COMMANDS: ServerProvider["slashCommands"] = [];
+
+function useComposerProviderState(input: Parameters<typeof getComposerProviderState>[0]) {
+ // Calls no hooks, so the compiler needs the directive to memoize it.
+ "use memo";
+ return getComposerProviderState(input);
+}
+
export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) {
const {
composerDraftTarget,
@@ -1783,7 +1983,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const isEditingQueuedMessage = editingQueuedAttachments !== null;
const composerDraft = useComposerThreadDraft(composerDraftTarget);
const composerDraftTargetKeyRef = useRef("");
- composerDraftTargetKeyRef.current = composerDraftTargetKey;
+ useLayoutEffect(() => {
+ composerDraftTargetKeyRef.current = composerDraftTargetKey;
+ });
const questionAttachmentTarget =
pendingUserInputs[0] && activePendingProgress?.activeQuestion
? questionAttachmentDraftId(
@@ -1799,12 +2001,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// An import that finishes after a draft change must compare against the draft open *now*, not
// the one captured in the closure that started it.
const attachmentTargetKeyRef = useRef(attachmentTargetKey);
- attachmentTargetKeyRef.current = attachmentTargetKey;
+ useLayoutEffect(() => {
+ attachmentTargetKeyRef.current = attachmentTargetKey;
+ });
const questionPreparations = useQuestionAttachmentPreparation((state) => state.counts);
const prompt = composerDraft.prompt;
const composerImages = attachmentDraft.images;
const composerFiles = attachmentDraft.files;
- const inlineFileIdSet = useMemo(() => {
+ const inlineFileIdSet = (() => {
if (questionAttachmentTarget) return new Set<string>();
const contextIds = new Set(collectInlineContextIds(prompt));
return new Set(
@@ -1812,7 +2016,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
.filter((file) => contextIds.has(toKindScopedComposerContextId("file", file.id)))
.map((file) => file.id),
);
- }, [composerFiles, prompt, questionAttachmentTarget]);
+ })();
const composerVideos = composerFiles.filter((file) =>
isPreviewableComposerVideo(file, environmentId),
);
@@ -1830,90 +2034,64 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
getPendingSnapShotAnimations,
getPendingSnapShotAnimations,
);
- const pendingSnapShotIds = useMemo(
- () => pendingSnapShotAnimationIdsForTarget(pendingSnapShotAnimations, composerDraftTarget),
- [composerDraftTarget, pendingSnapShotAnimations],
- );
- const pendingSnapShotIdSet = useMemo(() => new Set(pendingSnapShotIds), [pendingSnapShotIds]);
- const uncommittedSnapShotIds = pendingSnapShotIds.filter(
- (id) => !composerImages.some((image) => image.id === id),
+ const {
+ pendingSnapShotIds,
+ pendingSnapShotIdSet,
+ uncommittedSnapShotIds,
+ standaloneComposerImages,
+ } = useComposerImageGroups(
+ pendingSnapShotAnimations,
+ composerDraftTarget,
+ composerImages,
+ composerPreviewAnnotations,
);
- const standaloneComposerImages = useMemo(() => {
- const previewAnnotationIds = new Set(
- composerPreviewAnnotations.map((annotation) => annotation.id),
- );
- return composerImages.filter((image) => !previewAnnotationIds.has(image.id));
- }, [composerImages, composerPreviewAnnotations]);
const nonPersistedComposerImageIds = attachmentDraft.nonPersistedImageIds;
const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
const openPrLink = useOpenPrLink(routeThreadRef);
const [previewFileId, setPreviewFileId] = useState<string | null>(null);
const previewFile = composerFiles.find((file) => file.id === previewFileId);
- const composerContextActions = useMemo(
- () => ({
- environmentId,
- cwd: gitCwd ?? undefined,
- expandImage: (imageId: string) => {
- const preview = buildExpandedImagePreview(composerImages, imageId);
- if (preview) onExpandImage(preview);
- },
- openFile: setPreviewFileId,
- openMention: (path: string) => useRightPanelStore.getState().openFile(routeThreadRef, path),
- expandVideo: (fileId: string) => {
- const file = composerFiles.find((candidate) => candidate.id === fileId);
- if (!file || !isVideoAttachment(file)) return;
- const localPreview = buildExpandedImagePreview([file], file.id);
- if (localPreview) {
- onExpandImage(localPreview);
- return;
- }
- if (file.uploadedAttachmentId === undefined || file.uploadEnvironmentId !== environmentId) {
- return;
- }
- const persistedPreview = buildAttachmentVideoPreview(environmentId, {
- type: "file",
- id: file.uploadedAttachmentId,
- name: file.name,
- mimeType: file.mimeType,
- sizeBytes: file.sizeBytes,
- });
- if (persistedPreview) onExpandImage(persistedPreview);
- },
- openPullRequest: (event: React.MouseEvent<HTMLElement>, url: string) => {
- openPrLink(event, url);
- },
- }),
- [
- composerFiles,
- composerImages,
- gitCwd,
- environmentId,
- onExpandImage,
- openPrLink,
- routeThreadRef,
- ],
- );
- const composerContextRecords = useMemo(
- () =>
- composerContextRecordsFromDraft({
- terminalContexts: composerTerminalContexts,
- reviewComments: composerReviewComments,
- previewAnnotations: composerPreviewAnnotations,
- threadContexts: composerThreadContexts,
- images: composerImages,
- files: composerFiles,
- uploadsByImageId,
- }),
- [
- composerFiles,
- composerImages,
- composerPreviewAnnotations,
- composerReviewComments,
- composerTerminalContexts,
- composerThreadContexts,
- uploadsByImageId,
- ],
- );
+ const composerContextActions = {
+ environmentId,
+ cwd: gitCwd ?? undefined,
+ expandImage: (imageId: string) => {
+ const preview = buildExpandedImagePreview(composerImages, imageId);
+ if (preview) onExpandImage(preview);
+ },
+ openFile: setPreviewFileId,
+ openMention: (path: string) => useRightPanelStore.getState().openFile(routeThreadRef, path),
+ expandVideo: (fileId: string) => {
+ const file = composerFiles.find((candidate) => candidate.id === fileId);
+ if (!file || !isVideoAttachment(file)) return;
+ const localPreview = buildExpandedImagePreview([file], file.id);
+ if (localPreview) {
+ onExpandImage(localPreview);
+ return;
+ }
+ if (file.uploadedAttachmentId === undefined || file.uploadEnvironmentId !== environmentId) {
+ return;
+ }
+ const persistedPreview = buildAttachmentVideoPreview(environmentId, {
+ type: "file",
+ id: file.uploadedAttachmentId,
+ name: file.name,
+ mimeType: file.mimeType,
+ sizeBytes: file.sizeBytes,
+ });
+ if (persistedPreview) onExpandImage(persistedPreview);
+ },
+ openPullRequest: (event: React.MouseEvent<HTMLElement>, url: string) => {
+ openPrLink(event, url);
+ },
+ };
+ const composerContextRecords = composerContextRecordsFromDraft({
+ terminalContexts: composerTerminalContexts,
+ reviewComments: composerReviewComments,
+ previewAnnotations: composerPreviewAnnotations,
+ threadContexts: composerThreadContexts,
+ images: composerImages,
+ files: composerFiles,
+ uploadsByImageId,
+ });
const needsReattachFileCount = composerFiles.filter(composerFileNeedsReattach).length;
const fileStagingLimit = fileAttachmentStagingLimit({
attachmentUploadsCapabilityKnown,
@@ -2057,46 +2235,26 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
uploadsByImageId,
]);
- const providerInstanceEntries = useMemo<ReadonlyArray<ProviderInstanceEntry>>(
- () =>
- sortProviderInstanceEntries(
- applyProviderInstanceSettings(
- deriveProviderInstanceEntries(providerStatuses),
- settings,
- activeProjectId,
- ),
- ),
- [providerStatuses, settings, activeProjectId],
- );
- const selectedProviderByThreadId = composerDraft.activeProvider ?? null;
const {
+ providerInstanceEntries,
selectedProviderEntry,
requestedDriverKind,
lockedContinuationGroupKey,
unavailableProviderInstanceId,
- } = useMemo(
- () =>
- resolveComposerProviderSelection({
- entries: providerInstanceEntries,
- candidateInstanceIds: [
- selectedProviderByThreadId,
- activeThread?.runtime?.providerInstanceId,
- activeThreadModelSelection?.instanceId,
- activeProjectDefaultModelSelection?.instanceId,
- ],
- lockedProvider,
- lockedInstanceId:
- activeThread?.runtime?.providerInstanceId ?? activeThreadModelSelection?.instanceId,
- }),
- [
- activeProjectDefaultModelSelection?.instanceId,
+ } = useComposerProviderSelection({
+ providerStatuses,
+ settings,
+ activeProjectId,
+ candidateInstanceIds: [
+ composerDraft.activeProvider ?? null,
activeThread?.runtime?.providerInstanceId,
activeThreadModelSelection?.instanceId,
- selectedProviderByThreadId,
- lockedProvider,
- providerInstanceEntries,
+ activeProjectDefaultModelSelection?.instanceId,
],
- );
+ lockedProvider,
+ lockedInstanceId:
+ activeThread?.runtime?.providerInstanceId ?? activeThreadModelSelection?.instanceId,
+ });
const selectedInstanceId =
selectedProviderEntry?.instanceId ?? NO_PROVIDER_MODEL_SELECTION.instanceId;
const noProviderAvailable =
@@ -2145,145 +2303,32 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
: (attachmentBlockReason ??
(multipleModelSelections === null ? providerSendBlockReason : null)));
const isSendDisabled = sendDisabledReason !== null;
- const selectedProviderStatus = useMemo(
- () => selectedProviderEntry?.snapshot ?? null,
- [selectedProviderEntry],
- );
+ const selectedProviderStatus = selectedProviderEntry?.snapshot ?? null;
const compactCommandAvailable = providerSupportsManualCompaction(selectedProviderEntry);
- const selectedProviderSkills = useMemo(
- () =>
- selectedProviderStatus ? resolveProviderSkillsForCwd(selectedProviderStatus, gitCwd) : [],
- [gitCwd, selectedProviderStatus],
- );
- const selectedProviderSlashCommands = useMemo(
- () =>
- selectedProviderStatus
- ? resolveProviderSlashCommandsForCwd(selectedProviderStatus, gitCwd)
- : [],
- [gitCwd, selectedProviderStatus],
- );
- const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
- reportFailure: false,
- });
- // The last scan this composer asked for. A request inside the TTL is not
- // repeated, so a client clock ahead of the server's cannot loop rescans.
- const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null);
- const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{
- key: string;
- notBefore: number;
- } | null>(null);
- const workspaceRefreshScopeKey =
- gitCwd && selectedProviderEntry
- ? `${environmentId}:${selectedProviderEntry.instanceId}:${gitCwd}`
- : null;
- const workspaceSlashCommandsPending =
- selectedProviderStatus?.workspaceSnapshots?.some(
- (snapshot) => snapshot.cwd === gitCwd && snapshot.slashCommandsPending === true,
- ) ?? false;
- useEffect(() => {
- if (
- !workspaceSlashCommandsPending ||
- !workspaceRefreshRetry ||
- workspaceRefreshRetry.key !== workspaceRefreshScopeKey
- )
- return;
- const timeout = setTimeout(
- () => {
- setWorkspaceRefreshRetry((current) => (current === workspaceRefreshRetry ? null : current));
- },
- Math.max(0, workspaceRefreshRetry.notBefore - Date.now()),
- );
- return () => clearTimeout(timeout);
- }, [workspaceRefreshRetry, workspaceRefreshScopeKey, workspaceSlashCommandsPending]);
- const hadWorkspaceSnapshotRef = useRef(false);
- useEffect(() => {
- const hasWorkspaceSnapshot = hasCompleteProviderWorkspaceSnapshot(
- selectedProviderStatus,
- gitCwd,
- );
- if (hadWorkspaceSnapshotRef.current && !hasWorkspaceSnapshot) {
- workspaceRefreshKeyRef.current = null;
- setWorkspaceRefreshRetry(null);
- }
- hadWorkspaceSnapshotRef.current = hasWorkspaceSnapshot;
- }, [gitCwd, selectedProviderStatus]);
- useEffect(() => {
- if (!gitCwd || !selectedProviderEntry) return;
- const key = `${environmentId}:${selectedProviderEntry.instanceId}:${gitCwd}`;
- const now = Date.now();
- const lastRequest = workspaceRefreshKeyRef.current;
- if (
- lastRequest?.key === key &&
- now - lastRequest.requestedAt < PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS
- )
- return;
- if (hasCurrentProviderWorkspaceSnapshot(selectedProviderStatus, gitCwd, now)) {
- setWorkspaceRefreshRetry(null);
- return;
- }
- const retry = workspaceRefreshRetry;
- if (retry?.key === key && now < retry.notBefore) return;
- const request = { key, requestedAt: now };
- workspaceRefreshKeyRef.current = request;
- const retryLater = () => {
- if (workspaceRefreshKeyRef.current !== request) return;
- workspaceRefreshKeyRef.current = null;
- setWorkspaceRefreshRetry({
- key,
- notBefore: Date.now() + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS,
- });
- };
- void refreshProviders({
- environmentId,
- input: { instanceId: selectedProviderEntry.instanceId, cwd: gitCwd },
- }).then((result) => {
- const hasWorkspaceSnapshot =
- result._tag === "Success" &&
- hasCompleteProviderWorkspaceSnapshot(
- result.value.providers.find(
- (provider) => provider.instanceId === selectedProviderEntry.instanceId,
- ),
- gitCwd,
- );
- if (!hasWorkspaceSnapshot) retryLater();
- }, retryLater);
- }, [
+ const selectedProviderSkills = selectedProviderStatus
+ ? resolveProviderSkillsForCwd(selectedProviderStatus, gitCwd)
+ : NO_PROVIDER_SKILLS;
+ const selectedProviderSlashCommands = selectedProviderStatus
+ ? resolveProviderSlashCommandsForCwd(selectedProviderStatus, gitCwd)
+ : NO_PROVIDER_SLASH_COMMANDS;
+ const refreshWorkspaceSnapshotOnTyping = useProviderWorkspaceSnapshotRefresh({
environmentId,
gitCwd,
- prompt,
- refreshProviders,
selectedProviderEntry,
- workspaceRefreshRetry,
- ]);
- const selectedProviderModels = useMemo<ReadonlyArray<ServerProvider["models"][number]>>(
- () => selectedProviderEntry?.models ?? [],
- [selectedProviderEntry],
- );
+ selectedProviderStatus,
+ });
+ const selectedProviderModels: ReadonlyArray<ServerProvider["models"][number]> =
+ selectedProviderEntry?.models ?? [];
- const composerPromptInjectionState = useMemo(
- () => getComposerPromptInjectionState(prompt),
- [prompt],
- );
- const composerProviderState = useMemo(
- () =>
- getComposerProviderState({
- provider: selectedProvider,
- model: selectedModel,
- models: selectedProviderModels,
- promptInjectionState: composerPromptInjectionState,
- modelOptions: composerModelOptions?.[selectedInstanceId],
- planModeEnabled: settings.planModeEnabled,
- }),
- [
- composerModelOptions,
- composerPromptInjectionState,
- selectedInstanceId,
- selectedModel,
- selectedProvider,
- selectedProviderModels,
- settings.planModeEnabled,
- ],
- );
+ const composerPromptInjectionState = getComposerPromptInjectionState(prompt);
+ const composerProviderState = useComposerProviderState({
+ provider: selectedProvider,
+ model: selectedModel,
+ models: selectedProviderModels,
+ promptInjectionState: composerPromptInjectionState,
+ modelOptions: composerModelOptions?.[selectedInstanceId],
+ planModeEnabled: settings.planModeEnabled,
+ });
const selectedPromptEffort = composerProviderState.promptEffort;
const selectedModelOptionsForDispatch = composerProviderState.modelOptionsForDispatch;
@@ -2292,37 +2337,28 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
provider: selectedProviderStatus,
interactionMode: requestedInteractionMode,
});
- const selectedModelSelection = useMemo<ModelSelection>(
- () => createModelSelection(selectedInstanceId, selectedModel, selectedModelOptionsForDispatch),
- [selectedInstanceId, selectedModel, selectedModelOptionsForDispatch],
+ const selectedModelSelection: ModelSelection = createModelSelection(
+ selectedInstanceId,
+ selectedModel,
+ selectedModelOptionsForDispatch,
);
const selectedModelForPicker = selectedModel;
- const modelOptionsByInstance = useMemo<
- ReadonlyMap<ProviderInstanceId, ReadonlyArray<AppModelOption>>
- >(() => {
- const out = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
- for (const entry of providerInstanceEntries) {
- out.set(
- entry.instanceId,
- getAppModelOptionsForInstance(
- settings,
- entry,
- entry.instanceId === selectedInstanceId ? selectedModelForPicker : null,
- ),
- );
- }
- return out;
- }, [providerInstanceEntries, selectedInstanceId, selectedModelForPicker, settings]);
- const selectedModelForPickerWithCustomFallback = useMemo(() => {
+ const modelOptionsByInstance = useModelOptionsByInstance(
+ settings,
+ providerInstanceEntries,
+ selectedInstanceId,
+ selectedModelForPicker,
+ );
+ const selectedModelForPickerWithCustomFallback = (() => {
const currentOptions = modelOptionsByInstance.get(selectedInstanceId) ?? [];
return currentOptions.some((option) => option.slug === selectedModelForPicker)
? selectedModelForPicker
: (normalizeModelSlug(selectedModelForPicker, selectedProvider) ?? selectedModelForPicker);
- }, [modelOptionsByInstance, selectedInstanceId, selectedModelForPicker, selectedProvider]);
+ })();
- const activeThreadModelDisplayName = useMemo(
- () => resolveContextWindowModelDisplayName(activeThreadModelSelection, modelOptionsByInstance),
- [activeThreadModelSelection, modelOptionsByInstance],
+ const activeThreadModelDisplayName = resolveContextWindowModelDisplayName(
+ activeThreadModelSelection,
+ modelOptionsByInstance,
);
const reserveContextWindowMeter = shouldReserveContextWindowMeter({
meterEnabled: settings.contextWindowMeterEnabled,
@@ -2349,7 +2385,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const [composerHighlightedItemId, setComposerHighlightedItemId] = useState<string | null>(null);
const composerSuggestionId = useId();
const composerSuggestionListId = `${composerSuggestionId}-${encodeURIComponent(draftId ?? activeThreadId ?? "new")}-suggestions`;
- const promptHistoryPositionRef = useRef<ComposerPromptHistoryPosition | null>(null);
+ const promptHistoryPositionRef = useRef<{
+ targetKey: string;
+ position: ComposerPromptHistoryPosition;
+ } | null>(null);
const [composerHighlightedSearchKey, setComposerHighlightedSearchKey] = useState<string | null>(
null,
);
@@ -2409,7 +2448,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());
const pendingThreadImportsRef = useRef<Map<string, number>>(new Map());
const isRevertingCheckpointRef = useRef(isRevertingCheckpoint);
- isRevertingCheckpointRef.current = isRevertingCheckpoint;
+ useLayoutEffect(() => {
+ isRevertingCheckpointRef.current = isRevertingCheckpoint;
+ });
useEffect(() => {
const armPasteAsTextShortcut = () => {
@@ -2454,23 +2495,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
};
}, []);
- const composerSendState = useMemo(
- () =>
- deriveComposerSendState({
- prompt,
- imageCount: composerImages.length + composerFiles.length,
- terminalContexts: composerTerminalContexts,
- elementContextCount: composerPreviewAnnotations.length + composerReviewComments.length,
- }),
- [
- composerFiles.length,
- composerImages.length,
- composerPreviewAnnotations.length,
- composerReviewComments.length,
- composerTerminalContexts,
- prompt,
- ],
- );
+ const composerSendState = deriveComposerSendState({
+ prompt,
+ imageCount: composerImages.length + composerFiles.length,
+ terminalContexts: composerTerminalContexts,
+ elementContextCount: composerPreviewAnnotations.length + composerReviewComments.length,
+ });
const composerTriggerKind = composerTrigger?.kind ?? null;
const pathTriggerQuery = composerTrigger?.kind === "path" ? composerTrigger.query : "";
const pullRequestTriggerQuery =
@@ -2502,41 +2532,32 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
composerPreviewAnnotations.length === 0 &&
composerReviewComments.length === 0;
- const pullRequestListTargets = useMemo(
- () =>
- composerTriggerKind !== "pull-request" ||
- pullRequestProjectId === null ||
- (pullRequestTextQuery !== null && settledPullRequestTextQuery === null)
- ? EMPTY_PULL_REQUEST_LIST_TARGETS
- : [
- {
- environmentId,
- input: {
- state: "all" as const,
- projectId: pullRequestProjectId,
- limit: COMPOSER_PULL_REQUEST_LIST_LIMIT,
- ...(settledPullRequestTextQuery === null
- ? {}
- : { query: settledPullRequestTextQuery }),
- },
+ const pullRequestListTargets =
+ composerTriggerKind !== "pull-request" ||
+ pullRequestProjectId === null ||
+ (pullRequestTextQuery !== null && settledPullRequestTextQuery === null)
+ ? EMPTY_PULL_REQUEST_LIST_TARGETS
+ : [
+ {
+ environmentId,
+ input: {
+ state: "all" as const,
+ projectId: pullRequestProjectId,
+ limit: COMPOSER_PULL_REQUEST_LIST_LIMIT,
+ ...(settledPullRequestTextQuery === null
+ ? {}
+ : { query: settledPullRequestTextQuery }),
},
- ],
- [
- composerTriggerKind,
- environmentId,
- pullRequestProjectId,
- pullRequestTextQuery,
- settledPullRequestTextQuery,
- ],
- );
+ },
+ ];
const pullRequestLookup = usePullRequestList(pullRequestListTargets);
- const pullRequestTriggerNumber = useMemo(() => {
+ const pullRequestTriggerNumber = (() => {
if (composerTrigger?.kind !== "pull-request" || composerTrigger.query.length === 0) {
return null;
}
const number = Number(composerTrigger.query);
return Number.isSafeInteger(number) && number > 0 ? number : null;
- }, [composerTrigger]);
+ })();
const debouncedPullRequestNumber = useDebouncedValue(pullRequestTriggerNumber, 180);
const settledPullRequestNumber =
pullRequestTriggerNumber === debouncedPullRequestNumber ? pullRequestTriggerNumber : null;
@@ -2564,15 +2585,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
}),
);
- const linkedPullRequests = useMemo(
- () =>
- pullRequestProjectId === null
- ? []
- : composerPullRequestsFromLinks(threadPullRequests ?? [], pullRequestProjectId),
- [pullRequestProjectId, threadPullRequests],
- );
+ const linkedPullRequests =
+ pullRequestProjectId === null
+ ? []
+ : composerPullRequestsFromLinks(threadPullRequests ?? [], pullRequestProjectId);
- const composerMenuItems = useMemo<ComposerCommandItem[]>(() => {
+ const composerMenuItems = ((): ComposerCommandItem[] => {
if (!composerTrigger) return [];
if (composerTrigger.kind === "path") {
return [
@@ -2725,54 +2743,28 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
}));
}
return [];
- }, [
- activeThreadId,
- compactSlashCommandAvailable,
- composerTrigger,
- environmentId,
- environmentThreadShells,
- exactPullRequestLookup.data,
- linkedPullRequests,
- planModeUiEnabled,
- pullRequestLookup.data,
- pullRequestProjectId,
- pullRequestRepository,
- pullRequestTriggerNumber,
- selectedProvider,
- selectedProviderSkills,
- selectedProviderSlashCommands,
- selectedProviderStatus,
- settings.showSkillsInSlashMenu,
- workspaceEntries.entries,
- ]);
+ })();
const composerMenuOpen = Boolean(composerTrigger);
const composerMenuSearchKey = composerTrigger
? `${composerSuggestionListId}:${composerTrigger.kind}:${composerTrigger.query.trim().toLowerCase()}`
: null;
- const activeComposerMenuItem = useMemo(() => {
- const activeItemId = resolveComposerMenuActiveItemId({
- items: composerMenuItems,
- highlightedItemId: composerHighlightedItemId,
- currentSearchKey: composerMenuSearchKey,
- highlightedSearchKey: composerHighlightedSearchKey,
- });
- return composerMenuItems.find((item) => item.id === activeItemId) ?? null;
- }, [
- composerHighlightedItemId,
- composerHighlightedSearchKey,
- composerMenuItems,
- composerMenuSearchKey,
- ]);
+ const activeComposerMenuItemId = resolveComposerMenuActiveItemId({
+ items: composerMenuItems,
+ highlightedItemId: composerHighlightedItemId,
+ currentSearchKey: composerMenuSearchKey,
+ highlightedSearchKey: composerHighlightedSearchKey,
+ });
+ const activeComposerMenuItem =
+ composerMenuItems.find((item) => item.id === activeComposerMenuItemId) ?? null;
- composerMenuOpenRef.current = composerMenuOpen;
- composerMenuItemsRef.current = composerMenuItems;
- activeComposerMenuItemRef.current = activeComposerMenuItem;
+ useLayoutEffect(() => {
+ composerMenuOpenRef.current = composerMenuOpen;
+ composerMenuItemsRef.current = composerMenuItems;
+ activeComposerMenuItemRef.current = activeComposerMenuItem;
+ });
- const nonPersistedComposerImageIdSet = useMemo(
- () => new Set(nonPersistedComposerImageIds),
- [nonPersistedComposerImageIds],
- );
+ const nonPersistedComposerImageIdSet = new Set(nonPersistedComposerImageIds);
const isComposerApprovalState = activePendingApproval !== null;
const composerSuggestionsVisible = composerMenuOpen && !isComposerApprovalState;
@@ -2792,28 +2784,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
(supportsQuestionAttachments &&
activePendingProgress.activeQuestion?.allowCustomAnswer !== false));
const composerFooterHasWideActions = showPlanFollowUpPrompt || activePendingProgress !== null;
- const composerFooterActionLayoutKey = useMemo(() => {
- if (activePendingProgress) {
- return `pending:${activePendingProgress.questionIndex}:${activePendingProgress.isLastQuestion}:${activePendingIsResponding}`;
- }
- if (phase === "running") {
- return "running";
- }
- if (showPlanFollowUpPrompt) {
- return prompt.trim().length > 0 ? "plan:refine" : "plan:implement";
- }
- return `idle:${composerSendState.hasSendableContent}:${isSendBusy}:${isConnecting}:${isPreparingWorktree}`;
- }, [
- activePendingIsResponding,
- activePendingProgress,
- composerSendState.hasSendableContent,
- isConnecting,
- isPreparingWorktree,
- isSendBusy,
- phase,
- prompt,
- showPlanFollowUpPrompt,
- ]);
const isComposerMenuLoading =
(composerTriggerKind === "path" && pathTriggerQuery.length > 0 && workspaceEntries.isPending) ||
@@ -2824,7 +2794,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
pullRequestTextQuery !== debouncedPullRequestTextQuery ||
pullRequestTriggerNumber !== debouncedPullRequestNumber ||
exactPullRequestLookup.isPending));
- const composerMenuEmptyState = useMemo(() => {
+ const composerMenuEmptyState = (() => {
if (composerTriggerKind === "skill") {
return "No skills found. Try / to browse provider commands.";
}
@@ -2845,14 +2815,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
return composerTriggerKind === "path"
? "No matching files or folders."
: "No matching command.";
- }, [
- composerTrigger,
- composerTriggerKind,
- pullRequestLookup.data?.errors,
- pullRequestLookup.error,
- pullRequestProjectId,
- pullRequestRepository,
- ]);
+ })();
const setPromptFromTraits = (nextPrompt: string) => {
if (nextPrompt === promptRef.current) {
@@ -2906,30 +2869,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
iconOnlyBlockCount: restingControlsIconOnlyBlockCount,
controlsVisible: restingControlsVisible,
} = useRestingComposerControlsLayout(restingControlsHost ?? inlineRestingControlsHost);
- const expandedControlsLayout = useRestingComposerControlsLayout(null, true);
- const pendingPrimaryAction = useMemo(
- () =>
- activePendingProgress
- ? {
- questionIndex: activePendingProgress.questionIndex,
- isLastQuestion: activePendingProgress.isLastQuestion,
- canAdvance:
- activePendingProgress.canAdvance &&
- !attachmentBlockReason &&
- !(questionPreparations[attachmentTargetKey] ?? 0),
- isResponding: activePendingIsResponding,
- isComplete: Boolean(activePendingResolvedAnswers),
- }
- : null,
- [
- activePendingIsResponding,
- activePendingProgress,
- activePendingResolvedAnswers,
- attachmentBlockReason,
- questionPreparations,
- attachmentTargetKey,
- ],
- );
+ const {
+ attachControls: attachExpandedComposerControls,
+ hiddenBlockCount: expandedControlsHiddenBlockCount,
+ iconOnlyBlockCount: expandedControlsIconOnlyBlockCount,
+ } = useRestingComposerControlsLayout(null, true);
+ const pendingPrimaryAction = activePendingProgress
+ ? {
+ questionIndex: activePendingProgress.questionIndex,
+ isLastQuestion: activePendingProgress.isLastQuestion,
+ canAdvance:
+ activePendingProgress.canAdvance &&
+ !attachmentBlockReason &&
+ !(questionPreparations[attachmentTargetKey] ?? 0),
+ isResponding: activePendingIsResponding,
+ isComplete: Boolean(activePendingResolvedAnswers),
+ }
+ : null;
const showResumeAction =
canResume && !composerDraftHasUserContent(composerDraft) && !isEditingQueuedMessage;
const collapsedComposerPrimaryActionDisabled =
@@ -3103,12 +3059,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
fail("The original attachment is no longer available.");
return;
}
- let blob: Blob;
- try {
- const response = await fetch(url, { signal: AbortSignal.timeout(60_000) });
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
- blob = await response.blob();
- } catch {
+ const blob = await downloadAttachmentBlob(url);
+ if (!blob) {
fail("Downloading it from the source failed.");
return;
}
@@ -3172,11 +3124,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// attachment behind it, and so bytes for an abandoned draft never enter the next one.
const importTargetKey = attachmentTargetKey;
pendingDraftWork.begin(importTargetKey);
- try {
- await runAttachmentImport(record, localId, sourceEnvironmentId, importTargetKey);
- } finally {
- pendingDraftWork.end(importTargetKey);
- }
+ await runAttachmentImport(record, localId, sourceEnvironmentId, importTargetKey).finally(() =>
+ pendingDraftWork.end(importTargetKey),
+ );
};
const importContextRecords = (
records: ReadonlyArray<ComposerContextRecord>,
@@ -3297,20 +3247,27 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
useEffect(() => {
if (isLiteralPendingAnswer) return;
promptRef.current = prompt;
- setComposerCursor((existing) => clampCollapsedComposerCursor(prompt, existing));
}, [isLiteralPendingAnswer, prompt, promptRef]);
+ const [cursorClampedFor, setCursorClampedFor] = useState({ prompt, isLiteralPendingAnswer });
+ if (
+ cursorClampedFor.prompt !== prompt ||
+ cursorClampedFor.isLiteralPendingAnswer !== isLiteralPendingAnswer
+ ) {
+ setCursorClampedFor({ prompt, isLiteralPendingAnswer });
+ if (!isLiteralPendingAnswer) {
+ setComposerCursor((existing) => clampCollapsedComposerCursor(prompt, existing));
+ }
+ }
- useEffect(() => {
- if (composerSubmissionError === null) return;
+ // A shown length error follows the prompt so it clears once the prompt fits.
+ if (composerSubmissionError !== null) {
const nextError = getComposerPromptLengthValidationMessage(prompt);
if (nextError !== composerSubmissionError) {
setComposerSubmissionError(nextError);
}
- }, [composerSubmissionError, prompt]);
+ }
- useEffect(() => {
- setProviderInputSubmissionError(null);
- }, [
+ const providerInputInputs = [
composerPreviewAnnotations,
composerReviewComments,
composerTerminalContexts,
@@ -3318,7 +3275,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
selectedModel,
selectedPromptEffort,
selectedProvider,
- ]);
+ ] as const;
+ const [providerInputValidatedFor, setProviderInputValidatedFor] = useState(providerInputInputs);
+ if (providerInputInputs.some((value, index) => value !== providerInputValidatedFor[index])) {
+ setProviderInputValidatedFor(providerInputInputs);
+ setProviderInputSubmissionError(null);
+ }
useEffect(() => {
composerImagesRef.current = composerImages;
@@ -3332,31 +3294,16 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
composerTerminalContextsRef.current = composerTerminalContexts;
}, [composerTerminalContexts, composerTerminalContextsRef]);
- useEffect(() => {
- if (!composerMenuOpen) {
- setComposerHighlightedItemId(null);
- setComposerHighlightedSearchKey(null);
- return;
- }
- const nextActiveItemId = resolveComposerMenuActiveItemId({
- items: composerMenuItems,
- highlightedItemId: composerHighlightedItemId,
- currentSearchKey: composerMenuSearchKey,
- highlightedSearchKey: composerHighlightedSearchKey,
- });
- setComposerHighlightedItemId((existing) =>
- existing === nextActiveItemId ? existing : nextActiveItemId,
- );
- setComposerHighlightedSearchKey((existing) =>
- existing === composerMenuSearchKey ? existing : composerMenuSearchKey,
- );
- }, [
- composerHighlightedItemId,
- composerHighlightedSearchKey,
- composerMenuItems,
- composerMenuOpen,
- composerMenuSearchKey,
- ]);
+ // The highlight is stored resolved, so keyboard handlers and the next
+ // search compare against the item that is actually shown as active.
+ const resolvedHighlightedItemId = composerMenuOpen ? activeComposerMenuItemId : null;
+ const resolvedHighlightedSearchKey = composerMenuOpen ? composerMenuSearchKey : null;
+ if (composerHighlightedItemId !== resolvedHighlightedItemId) {
+ setComposerHighlightedItemId(resolvedHighlightedItemId);
+ }
+ if (composerHighlightedSearchKey !== resolvedHighlightedSearchKey) {
+ setComposerHighlightedSearchKey(resolvedHighlightedSearchKey);
+ }
const lastSyncedPendingInputRef = useRef<{
requestId: string | null;
@@ -3407,24 +3354,24 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
resetComposerTrigger,
]);
- useEffect(() => {
+ const composerSessionKey = `${draftId ?? ""}:${activeThreadId ?? ""}:${isLiteralPendingAnswer}`;
+ const [shownComposerSessionKey, setShownComposerSessionKey] = useState(composerSessionKey);
+ if (shownComposerSessionKey !== composerSessionKey) {
+ setShownComposerSessionKey(composerSessionKey);
setComposerHighlightedItemId(null);
setComposerHighlightedSearchKey(null);
setComposerSubmissionError(null);
setProviderInputSubmissionError(null);
- const { cursor, trigger } = composerStateAtPromptEnd(promptRef.current, isLiteralPendingAnswer);
+ const pendingAnswer = activePendingProgress?.customAnswer;
+ const { cursor, trigger } = composerStateAtPromptEnd(
+ typeof pendingAnswer === "string" ? pendingAnswer : prompt,
+ isLiteralPendingAnswer,
+ );
setComposerCursor(cursor);
resetComposerTrigger(trigger);
setIsDragOverComposer(false);
setIsComposerScrollCollapsed(false);
- }, [
- draftId,
- activeThreadId,
- isLiteralPendingAnswer,
- promptRef,
- resetComposerTrigger,
- setIsComposerScrollCollapsed,
- ]);
+ }
useLayoutEffect(() => {
const composerForm = composerFormRef.current;
@@ -3460,13 +3407,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
previous === nextCompactness.footerCompact ? previous : nextCompactness.footerCompact,
);
});
- }, [
- activeThreadId,
- composerFooterActionLayoutKey,
- composerFooterHasWideActions,
- isComposerApprovalState,
- isComposerCollapsedMobile,
- ]);
+ }, [composerFooterHasWideActions]);
useEffect(() => {
let cancelled = false;
@@ -3567,6 +3508,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
contextIds: string[],
) => {
expandComposerForEditorChange();
+ // Typing is when the menu is about to need skills, so it re-checks a
+ // snapshot that has aged past its TTL.
+ refreshWorkspaceSnapshotOnTyping();
if (activePendingProgress?.activeQuestion && pendingUserInputs.length > 0) {
if (activePendingProgress.activeQuestion.allowCustomAnswer === false) return;
setComposerCursor(nextCursor);
@@ -3585,7 +3529,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
onTypingGuardDraftChange();
promptRef.current = nextPrompt;
setPrompt(nextPrompt);
- if (promptHistoryPositionRef.current?.recalled !== nextPrompt) {
+ if (promptHistoryPositionRef.current?.position.recalled !== nextPrompt) {
promptHistoryPositionRef.current = null;
}
const referenced = new Set(contextIds);
@@ -4119,15 +4063,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// Entries are built on the keypress, not per render: the timeline changes
// on every streamed delta and ArrowUp is rare.
const promptHistoryMessagesRef = useRef(promptHistoryMessages);
- promptHistoryMessagesRef.current = promptHistoryMessages;
+ useLayoutEffect(() => {
+ promptHistoryMessagesRef.current = promptHistoryMessages;
+ });
// The composer persists across threads. A recall from thread A must not
// be treated as active in thread B, where the text-match fallback could
// otherwise turn B's own draft into a browsing position.
const promptHistoryTargetKey = composerTargetKey(composerDraftTarget);
- useEffect(() => {
- promptHistoryPositionRef.current = null;
- }, [promptHistoryTargetKey]);
const replacePromptFromHistory = (nextPrompt: string) => {
promptRef.current = nextPrompt;
@@ -4154,7 +4097,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
) {
return false;
}
- if (promptHistoryPositionRef.current === null && promptRef.current.length > 0) {
+ const recalled = promptHistoryPositionRef.current;
+ const position = recalled?.targetKey === promptHistoryTargetKey ? recalled.position : null;
+ if (position === null && promptRef.current.length > 0) {
return false;
}
const editor = composerEditorRef.current;
@@ -4164,11 +4109,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const step = stepComposerPromptHistory({
direction,
entries: buildComposerPromptHistoryEntries(promptHistoryMessagesRef.current),
- position: promptHistoryPositionRef.current,
+ position,
currentPrompt: promptRef.current,
});
if (!step) return false;
- promptHistoryPositionRef.current = step.position;
+ promptHistoryPositionRef.current = step.position
+ ? { targetKey: promptHistoryTargetKey, position: step.position }
+ : null;
replacePromptFromHistory(step.prompt);
return true;
};
@@ -4650,7 +4597,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const stashTarget = composerDraftTarget;
const entryId = randomUUID();
- try {
+ const stashAndEncode = async () => {
// Ordering matters in both directions: writing before clearing means a
// crash or closed tab mid-encode still leaves the prompt recoverable,
// while clearing before the async image work means edits typed during
@@ -4774,9 +4721,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
data: { hideCopyButton: true },
});
}
- } finally {
- stashInFlightRef.current.delete(snapshotKey);
- }
+ };
+ await stashAndEncode().finally(() => stashInFlightRef.current.delete(snapshotKey));
};
const stashCurrentPromptStable = useStableCallback(stashCurrentPrompt);
@@ -4953,7 +4899,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
!hasMultilinePrompt &&
!composerHasExpandedChrome &&
!showInlineTasksBadge;
- composerScrollCollapseEligibleRef.current = canScrollCollapseComposer && !isComposerResting;
+ const composerScrollCollapseEligible = canScrollCollapseComposer && !isComposerResting;
+ useLayoutEffect(() => {
+ composerScrollCollapseEligibleRef.current = composerScrollCollapseEligible;
+ });
useEffect(() => {
if (!canScrollCollapseComposer) {
@@ -5048,7 +4997,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
finishScrollGesture();
};
}, [
- activeThreadId,
canTrackComposerScrollGesture,
getTimelineScrollableNode,
isTimelineAtLogicalEnd,
@@ -5057,10 +5005,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const restingHiddenBlockCount = composerControlsCollapsed
? restingControlsHiddenBlockCount
- : expandedControlsLayout.hiddenBlockCount;
+ : expandedControlsHiddenBlockCount;
const iconOnlyBlockCount = composerControlsCollapsed
? restingControlsIconOnlyBlockCount
- : expandedControlsLayout.iconOnlyBlockCount;
+ : expandedControlsIconOnlyBlockCount;
const restingProviderTraitsPicker = renderProviderTraitsPicker({
...providerTraitsPickerInput,
size: composerControlsCollapsed ? "xs" : "sm",
@@ -5295,30 +5243,26 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const bannerStackItems = activityStackItem
? [activityStackItem, ...props.bannerItems]
: props.bannerItems;
- useEffect(() => {
- if (activeTasksProgress === null || activeTaskSteps === null) {
- setIsTasksDrawerOpen(false);
- }
- }, [activeTaskSteps, activeTasksProgress]);
-
- useEffect(() => {
- if (hasBlockingComposerTopDrawer) {
- setIsTasksDrawerOpen(false);
- }
- }, [hasBlockingComposerTopDrawer]);
-
- useEffect(() => {
+ const [tasksDrawerThreadId, setTasksDrawerThreadId] = useState(activeThreadId);
+ if (
+ isTasksDrawerOpen &&
+ (activeTasksProgress === null ||
+ activeTaskSteps === null ||
+ hasBlockingComposerTopDrawer ||
+ tasksDrawerThreadId !== activeThreadId)
+ ) {
setIsTasksDrawerOpen(false);
- }, [activeThreadId]);
-
- useEffect(() => {
- if (composerMenuOpen) {
- setIsStashMenuOpen(false);
- }
- }, [composerMenuOpen]);
- useEffect(() => {
- setIsStashMenuOpen(false);
- }, [prompt]);
+ }
+ if (tasksDrawerThreadId !== activeThreadId) setTasksDrawerThreadId(activeThreadId);
+
+ const [stashMenuClosesOn, setStashMenuClosesOn] = useState({ prompt, composerMenuOpen });
+ if (
+ stashMenuClosesOn.prompt !== prompt ||
+ stashMenuClosesOn.composerMenuOpen !== composerMenuOpen
+ ) {
+ setStashMenuClosesOn({ prompt, composerMenuOpen });
+ if (stashMenuClosesOn.prompt !== prompt || composerMenuOpen) setIsStashMenuOpen(false);
+ }
useEffect(() => {
const handler = (event: globalThis.KeyboardEvent) => {
@@ -5501,7 +5445,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
);
if (questionAttachmentTarget)
changeQuestionAttachmentPreparation(questionAttachmentTarget, acceptedImages.length);
- try {
+ const compressAndStoreImages = async (): Promise<boolean> => {
const nextImages: ComposerImageAttachment[] = [];
let compressionError: string | null = null;
for (const file of acceptedImages) {
@@ -5554,7 +5498,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
if (compressionError !== null) {
setThreadError(threadId, compressionError);
}
- } finally {
+ return insertedAny;
+ };
+ return await compressAndStoreImages().finally(() => {
if (questionAttachmentTarget)
changeQuestionAttachmentPreparation(questionAttachmentTarget, -acceptedImages.length);
const remaining =
@@ -5564,8 +5510,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
} else {
pendingImageCompressionsRef.current.delete(attachmentTargetKey);
}
- }
- return insertedAny;
+ });
};
const insertAttachmentReferences = (
@@ -5690,8 +5635,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const onComposerPaste = (event: React.ClipboardEvent<HTMLElement>) => {
const files = Array.from(event.clipboardData.files);
const plainText = event.clipboardData.getData("text/plain");
- const bypassAutoAttachment = Date.now() <= pasteAsTextShortcutUntilRef.current;
- pasteAsTextShortcutUntilRef.current = 0;
+ const bypassAutoAttachment = consumePasteAsTextShortcut(pasteAsTextShortcutUntilRef);
if (
files.length > 0 &&
activeThreadId &&
@@ -5796,10 +5740,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// editor never sees the drop; the load-bearing rules (native stop, "move"
// effect, no eager focus) live in makeComposerMentionDragHandlers.
const composerMentionDragHandlers = makeComposerMentionDragHandlers({
- insertMentionAtEnd: (text) => insertComposerTextAtEnd(text, { ensureLeadingBoundary: true }),
+ insertMentionAtEnd: (text) =>
+ insertComposerTextAtEndStable(text, { ensureLeadingBoundary: true }),
insertPullRequestAtEnd: (pullRequest) => {
const comment = buildPullRequestReferenceContext(pullRequest);
- const inserted = insertComposerTextAtEnd(
+ const inserted = insertComposerTextAtEndStable(
`${formatInlineContextReference(reviewCommentContextReference(comment))} `,
{ ensureLeadingBoundary: true },
);
@@ -5829,8 +5774,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
timeout: 0,
data: { threadRef: routeThreadRef, hideCopyButton: true },
});
- try {
- await importComposerThreadAttachment({
+ const importResult = await settlePromise(() =>
+ importComposerThreadAttachment({
targetKey,
pendingImports: pendingThreadImportsRef.current,
countReservedAttachments: countReservedAttachmentsStable,
@@ -5854,9 +5799,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
);
}
},
- });
- } catch (error) {
- if (!isActive()) return;
+ }),
+ );
+ if (importResult._tag === "Failure" && isActive()) {
+ const error = squashAtomCommandFailure(importResult);
toastManager.add({
type: "error",
title: "Unable to read the dropped thread",
@@ -5866,10 +5812,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
? "This thread exceeds the attachment size limit. Try a shorter thread."
: "Check that its environment is connected and up to date, then try again.",
});
- } finally {
- toastManager.close(loadingToast);
- pendingDraftWork.end(targetKey);
}
+ toastManager.close(loadingToast);
+ pendingDraftWork.end(targetKey);
},
);
@@ -6074,10 +6019,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
pendingDraftWork.has(attachmentTargetKey),
insertTextAtEnd: insertComposerTextAtEndStable,
pasteTextAtEnd: (text: string, options) => {
- const bypassAutoAttachment =
- options?.bypassAutoAttachment === true ||
- Date.now() <= pasteAsTextShortcutUntilRef.current;
- pasteAsTextShortcutUntilRef.current = 0;
+ const shortcutArmed = consumePasteAsTextShortcut(pasteAsTextShortcutUntilRef);
+ const bypassAutoAttachment = options?.bypassAutoAttachment === true || shortcutArmed;
const promptLength = promptRef.current.length;
if (
!foldPastedText(text, bypassAutoAttachment, {
@@ -7226,7 +7169,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
)}
>
<div
- ref={expandedControlsLayout.attachControls}
+ ref={attachExpandedComposerControls}
data-chat-composer-controls="left"
data-chat-composer-footer-controls="true"
className={cn(