feat(composer): fold large pastes into text attachments (#11442)

This commit is contained in:
Chris Deeming authored and GitHub committed 2026-09-12 17:28:28 -07:00
1 parent 36caf200c2
commit 68c2277f50
77 files changed
+3278 -465

No files matched your search

@@ -1,3 +1,5 @@
import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/text-paste";
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
import { composerContextEditorTokens } from "../lib/composerContext";
import { requireNativeView } from "expo";
@@ -52,6 +54,13 @@ type NativePasteImagesEvent = NativeSyntheticEvent<{
readonly uris: ReadonlyArray<string>;
}>;
type NativePasteTextEvent = NativeSyntheticEvent<{
readonly value: string;
readonly eventCount: number;
readonly text: string;
readonly selection: ComposerEditorSelection;
}>;
interface NativeComposerEditorRef {
focus: () => Promise<void>;
blur: () => Promise<void>;
@@ -81,8 +90,11 @@ interface NativeComposerEditorProps extends ViewProps {
event: NativeSyntheticEvent<{ source: string; start: number; end: number }>,
) => void;
readonly onComposerPasteContext?: (
event: NativeSyntheticEvent<{ text: string; fragment: string; html: string }>,
event: NativePasteTextEvent & NativeSyntheticEvent<{ fragment: string; html: string }>,
) => void;
readonly textPasteThresholdBytes: number;
readonly maxInputChars: number;
readonly onComposerPasteText?: (event: NativePasteTextEvent) => void;
readonly onComposerFocus?: () => void;
readonly onComposerBlur?: () => void;
readonly onComposerSubmit?: () => void;
@@ -108,6 +120,7 @@ export function ComposerEditor({
onChangeText,
onSelectionChange,
onPasteImages,
onPasteText,
onFocus,
onBlur,
onSubmit,
@@ -282,6 +295,8 @@ export function ComposerEditor({
autoFocus={props.autoFocus ?? false}
autoCorrect={props.autoCorrect ?? true}
spellCheck={props.spellCheck ?? true}
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
style={style as StyleProp<ViewStyle>}
onComposerChange={(event) => {
const acknowledgedEventCount = acceptNativeEvent(
@@ -315,7 +330,36 @@ export function ComposerEditor({
}}
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
onComposerContextPress={(event) => props.onContextPress?.(event.nativeEvent)}
onComposerPasteContext={(event) => props.onPasteContext?.(event.nativeEvent)}
onComposerPasteContext={(event) => {
const paste = event.nativeEvent;
const acknowledgedEventCount = acceptNativeEvent(
paste.eventCount,
paste.value,
paste.selection,
);
if (acknowledgedEventCount === false) return;
onChangeText(paste.value);
onSelectionChange?.(paste.selection);
props.onPasteContext?.(paste);
setMostRecentEventCount(acknowledgedEventCount);
forceNativeEventRender((sequence) => sequence + 1);
}}
onComposerPasteText={(event) => {
const paste = event.nativeEvent;
const acknowledgedEventCount = acceptNativeEvent(
paste.eventCount,
paste.value,
paste.selection,
);
if (acknowledgedEventCount === false) return;
// Synchronize the draft before an async paste captures its insertion target.
// React props can still precede the last native keystroke.
onChangeText(paste.value);
onSelectionChange?.(paste.selection);
onPasteText?.(paste);
setMostRecentEventCount(acknowledgedEventCount);
forceNativeEventRender((sequence) => sequence + 1);
}}
onComposerFocus={onFocus}
onComposerBlur={onBlur}
onComposerSubmit={onSubmit}
@@ -327,4 +371,5 @@ export type {
ComposerEditorHandle,
ComposerEditorProps,
ComposerEditorSelection,
ComposerTextPaste,
} from "./T3ComposerEditor.types";
@@ -1,3 +1,5 @@
import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/text-paste";
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
import { composerContextEditorTokens } from "../lib/composerContext";
import { requireNativeView } from "expo";
@@ -55,6 +57,13 @@ type NativePasteImagesEvent = NativeSyntheticEvent<{
readonly uris: ReadonlyArray<string>;
}>;
type NativePasteTextEvent = NativeSyntheticEvent<{
readonly value: string;
readonly eventCount: number;
readonly text: string;
readonly selection: ComposerEditorSelection;
}>;
interface NativeComposerEditorRef {
focus: () => Promise<void>;
blur: () => Promise<void>;
@@ -85,8 +94,11 @@ interface NativeComposerEditorProps extends ViewProps {
event: NativeSyntheticEvent<{ source: string; start: number; end: number }>,
) => void;
readonly onComposerPasteContext?: (
event: NativeSyntheticEvent<{ text: string; fragment: string; html: string }>,
event: NativePasteTextEvent & NativeSyntheticEvent<{ fragment: string; html: string }>,
) => void;
readonly textPasteThresholdBytes: number;
readonly maxInputChars: number;
readonly onComposerPasteText?: (event: NativePasteTextEvent) => void;
readonly onComposerFocus?: () => void;
readonly onComposerBlur?: () => void;
}
@@ -111,6 +123,7 @@ export function ComposerEditor({
onChangeText,
onSelectionChange,
onPasteImages,
onPasteText,
onFocus,
onBlur,
contentInsetVertical = 0,
@@ -291,6 +304,8 @@ export function ComposerEditor({
autoFocus={props.autoFocus ?? false}
autoCorrect={props.autoCorrect ?? true}
spellCheck={props.spellCheck ?? true}
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
style={{ flex: 1, minHeight: 0 }}
onComposerChange={(event) => {
const acknowledgedEventCount = acceptNativeEvent(
@@ -325,7 +340,36 @@ export function ComposerEditor({
}}
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
onComposerContextPress={(event) => props.onContextPress?.(event.nativeEvent)}
onComposerPasteContext={(event) => props.onPasteContext?.(event.nativeEvent)}
onComposerPasteContext={(event) => {
const paste = event.nativeEvent;
const acknowledgedEventCount = acceptNativeEvent(
paste.eventCount,
paste.value,
paste.selection,
);
if (acknowledgedEventCount === false) return;
onChangeText(paste.value);
onSelectionChange?.(paste.selection);
props.onPasteContext?.(paste);
setMostRecentEventCount(acknowledgedEventCount);
forceNativeEventRender((sequence) => sequence + 1);
}}
onComposerPasteText={(event) => {
const paste = event.nativeEvent;
const acknowledgedEventCount = acceptNativeEvent(
paste.eventCount,
paste.value,
paste.selection,
);
if (acknowledgedEventCount === false) return;
// Synchronize the draft before an async paste captures its insertion target.
// React props can still precede the last native keystroke.
onChangeText(paste.value);
onSelectionChange?.(paste.selection);
onPasteText?.(paste);
setMostRecentEventCount(acknowledgedEventCount);
forceNativeEventRender((sequence) => sequence + 1);
}}
onComposerFocus={onFocus}
onComposerBlur={onBlur}
/>
@@ -337,4 +381,5 @@ export type {
ComposerEditorHandle,
ComposerEditorProps,
ComposerEditorSelection,
ComposerTextPaste,
} from "./T3ComposerEditor.types";
@@ -12,6 +12,7 @@ export function ComposerEditor({
skills: _skills,
selection,
onPasteImages,
onPasteText: _onPasteText,
style,
textStyle,
contentInsetVertical = 0,
@@ -65,4 +66,5 @@ export type {
ComposerEditorHandle,
ComposerEditorProps,
ComposerEditorSelection,
ComposerTextPaste,
} from "./T3ComposerEditor.types";
@@ -7,6 +7,13 @@ export type ComposerEditorSelection = {
readonly end: number;
};
export type ComposerTextPaste = {
readonly value: string;
readonly eventCount: number;
readonly text: string;
readonly selection: ComposerEditorSelection;
};
export interface ComposerEditorHandle {
focus: () => void;
blur: () => void;
@@ -18,11 +25,12 @@ export interface ComposerEditorProps {
readonly value: string;
readonly context?: OrchestrationMessageContext;
readonly clipboardFragment?: string;
readonly onPasteContext?: (clipboard: {
readonly text: string;
readonly fragment: string;
readonly html: string;
}) => void;
readonly onPasteContext?: (
clipboard: ComposerTextPaste & {
readonly fragment: string;
readonly html: string;
},
) => void;
readonly skills?: ReadonlyArray<
Pick<ServerProviderSkill, "name" | "displayName" | "shortDescription" | "description"> &
Partial<Pick<ServerProviderSkill, "path">>
@@ -50,6 +58,7 @@ export interface ComposerEditorProps {
readonly start: number;
readonly end: number;
}) => void;
readonly onPasteText?: (paste: ComposerTextPaste) => void;
readonly onFocus?: () => void;
readonly onBlur?: () => void;
/** Invoked by the native editor when Command-Return is pressed on a hardware keyboard. */
@@ -192,3 +192,25 @@ describe("assumeComposerControlledState", () => {
);
});
});
describe("typing immediately before an intercepted paste", () => {
it("keeps the pre-paste React value behind the native paste revision", () => {
const snapshots = [
{ eventCount: 0, value: "", selection: { start: 0, end: 0 } },
{ eventCount: 1, value: "typed", selection: { start: 5, end: 5 } },
{ eventCount: 2, value: "typed", selection: { start: 0, end: 5 } },
];
// Both native platforms stamp the paste with its current value and selection.
// A render still carrying the typing caret cannot overwrite that selection.
expect(resolveComposerControlledEventCount("typed", { start: 5, end: 5 }, 2, snapshots)).toBe(
1,
);
expect(isComposerNativeEcho("typed", { start: 0, end: 5 }, 2, snapshots)).toBe(true);
// The replacement is a parent edit at the acknowledged paste revision.
expect(resolveComposerControlledEventCount("pasted", { start: 6, end: 6 }, 2, snapshots)).toBe(
2,
);
expect(isComposerNativeEcho("pasted", { start: 6, end: 6 }, 2, snapshots)).toBe(false);
expect(acknowledgeComposerNativeEvent(2, 1)).toBeNull();
});
});