mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-22 00:14:30 +00:00
436 lines
17 KiB
TypeScript
436 lines
17 KiB
TypeScript
import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@hanzodev/client-runtime/text-paste";
|
|
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@hanzodev/contracts";
|
|
import { collectComposerInlineTokens } from "@hanzodev/shared/composerInlineTokens";
|
|
import { composerContextEditorTokens, composerSkillLabels } from "../lib/composerContext";
|
|
import { DEFAULT_COMPOSER_ENTER_BEHAVIOR } from "../lib/composerEnterBehavior";
|
|
import { useComposerModelTokens } from "./useComposerModelTokens";
|
|
import { requireNativeView } from "expo";
|
|
import { TextInputWrapper } from "expo-paste-input";
|
|
import {
|
|
useCallback,
|
|
useEffect,
|
|
useImperativeHandle,
|
|
useMemo,
|
|
useRef,
|
|
useState,
|
|
type Ref,
|
|
} from "react";
|
|
import type { NativeSyntheticEvent, ViewProps } from "react-native";
|
|
import { Platform, StyleSheet } from "react-native";
|
|
|
|
import {
|
|
composerChipSizeSuffix,
|
|
contextChipIcon,
|
|
contextChipAccent,
|
|
contextChipPresentation,
|
|
contextRecordFileName,
|
|
} from "../lib/composerChipPresentation";
|
|
import { contextChipIconUri, modelChipIcon, modelChipIconUri } from "../lib/contextChipIconSource";
|
|
import { MOBILE_TYPOGRAPHY } from "../lib/typography";
|
|
import { useNativePaste } from "../lib/useNativePaste";
|
|
import { FONT_FAMILIES } from "../lib/fontFamilies";
|
|
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
|
|
import { useUniwindTheme } from "../lib/useUniwindTheme";
|
|
import { createNativeComposerTheme } from "../lib/nativeComposerTheme";
|
|
import {
|
|
acknowledgeComposerNativeEvent,
|
|
assumeComposerControlledState,
|
|
isComposerNativeEcho,
|
|
pruneAcknowledgedComposerNativeEvents,
|
|
resolveComposerControlledEventCount,
|
|
type ComposerNativeEventSnapshot,
|
|
} from "./composerEditorRevision";
|
|
import type { ComposerEditorProps, ComposerEditorSelection } from "./HanzoComposerEditor.types";
|
|
|
|
const NATIVE_MODULE_NAME = "HanzoComposerEditor";
|
|
const EMPTY_SKILLS: NonNullable<ComposerEditorProps["skills"]> = [];
|
|
const EMPTY_MODELS: NonNullable<ComposerEditorProps["models"]> = [];
|
|
|
|
type NativeEditorEvent = NativeSyntheticEvent<{
|
|
readonly value: string;
|
|
readonly selection: ComposerEditorSelection;
|
|
readonly eventCount: number;
|
|
}>;
|
|
|
|
type NativeSelectionEvent = NativeSyntheticEvent<{
|
|
readonly value: string;
|
|
readonly selection: ComposerEditorSelection;
|
|
readonly eventCount: number;
|
|
}>;
|
|
|
|
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>;
|
|
setSelection: (start: number, end: number) => Promise<void>;
|
|
}
|
|
|
|
interface NativeComposerEditorProps extends ViewProps {
|
|
readonly ref?: Ref<NativeComposerEditorRef>;
|
|
readonly controlledDocumentJson: string;
|
|
readonly clipboardFragment: string;
|
|
readonly themeJson: string;
|
|
readonly placeholder: string;
|
|
readonly fontFamily: string;
|
|
readonly fontSize: number;
|
|
readonly lineHeight: number;
|
|
readonly contentInsetVertical: number;
|
|
readonly singleLineCentered: boolean;
|
|
readonly editable: boolean;
|
|
readonly readOnly: boolean;
|
|
readonly scrollEnabled: boolean;
|
|
readonly autoFocus: boolean;
|
|
readonly autoCorrect: boolean;
|
|
readonly spellCheck: boolean;
|
|
readonly enterBehavior: string;
|
|
readonly submitEnabled: boolean;
|
|
readonly onComposerChange: (event: NativeEditorEvent) => void;
|
|
readonly onComposerSelectionChange?: (event: NativeSelectionEvent) => void;
|
|
readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
|
|
readonly onComposerContextPress?: (
|
|
event: NativeSyntheticEvent<{ source: string; start: number; end: number }>,
|
|
) => void;
|
|
readonly onComposerPasteContext?: (
|
|
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 onComposerContentSizeChange?: (
|
|
event: NativeSyntheticEvent<{ readonly width: number; readonly height: number }>,
|
|
) => void;
|
|
readonly onComposerSubmit?: (event: NativeSyntheticEvent<{ alternate: boolean }>) => void;
|
|
}
|
|
|
|
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
|
|
|
|
// A view command rejects with "Unable to find the view" when the composer's native view is
|
|
// unmounted, which can happen while an open question hides the composer slot. There is nothing
|
|
// to act on then.
|
|
function settleViewCommand(command: Promise<void> | undefined): void {
|
|
command?.catch(() => undefined);
|
|
}
|
|
|
|
function basename(path: string): string {
|
|
const separator = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
return separator >= 0 ? path.slice(separator + 1) : path;
|
|
}
|
|
|
|
export function ComposerEditor({
|
|
ref,
|
|
skills = EMPTY_SKILLS,
|
|
models = EMPTY_MODELS,
|
|
selection,
|
|
style,
|
|
textStyle,
|
|
onChangeText,
|
|
onSelectionChange,
|
|
onPasteImages,
|
|
onPasteText,
|
|
onFocus,
|
|
onBlur,
|
|
onContentSizeChange,
|
|
onSubmit,
|
|
contentInsetVertical = 0,
|
|
...props
|
|
}: ComposerEditorProps) {
|
|
const nativeRef = useRef<NativeComposerEditorRef>(null);
|
|
const mostRecentEventCountRef = useRef(0);
|
|
const [mostRecentEventCount, setMostRecentEventCount] = useState(0);
|
|
const [, forceNativeEventRender] = useState(0);
|
|
// Start history empty so restored drafts or recycled views receive a non-echo controlled payload.
|
|
const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
|
|
// Tokens advance from the previous committed value; render-phase state keeps
|
|
// an abandoned render from leaking a trailing token into the next one.
|
|
const [confirmedTokens, setConfirmedTokens] = useState(() => ({
|
|
value: props.value,
|
|
tokens: collectComposerInlineTokens(props.value),
|
|
}));
|
|
let tokens = confirmedTokens.tokens;
|
|
if (confirmedTokens.value !== props.value) {
|
|
tokens = collectComposerInlineTokens(props.value, {
|
|
preserveTrailingFrom: confirmedTokens.tokens,
|
|
});
|
|
setConfirmedTokens({ value: props.value, tokens });
|
|
}
|
|
const theme = useUniwindTheme();
|
|
const handlePaste = useNativePaste((uris) => onPasteImages?.(uris));
|
|
|
|
useImperativeHandle(
|
|
ref,
|
|
() => ({
|
|
focus: () => settleViewCommand(nativeRef.current?.focus()),
|
|
blur: () => settleViewCommand(nativeRef.current?.blur()),
|
|
setSelection: (nextSelection) =>
|
|
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
|
|
flushPendingInput: async () => {},
|
|
}),
|
|
[],
|
|
);
|
|
|
|
const skillLabels = useMemo(() => composerSkillLabels(skills), [skills]);
|
|
const { modelsBySource, modelTokens } = useComposerModelTokens(props.value, models);
|
|
const chipTextColor = createNativeComposerTheme(theme).text;
|
|
const tokensJson = useMemo(() => {
|
|
return JSON.stringify(
|
|
composerContextEditorTokens(props.value, tokens, skillLabels, modelTokens).map((token) => {
|
|
const model = token.type === "model" ? modelsBySource.get(token.source) : undefined;
|
|
const record =
|
|
token.type === "context"
|
|
? props.context?.records.find((record) => record.contextId === token.contextId)
|
|
: undefined;
|
|
const presentation = contextChipPresentation(
|
|
token.type === "context" ? token.kind : token.type,
|
|
record,
|
|
);
|
|
const icon = contextChipIcon(
|
|
presentation,
|
|
token.type === "mention" ? token.value : contextRecordFileName(record),
|
|
);
|
|
return {
|
|
// Android taps only known token types; a model chip opens like a skill chip.
|
|
type:
|
|
token.type === "model" ? "skill" : token.type === "citation" ? "context" : token.type,
|
|
source: token.source,
|
|
start: token.start,
|
|
end: token.end,
|
|
accent: contextChipAccent(
|
|
model ? modelChipIcon(model).accent : presentation.accent,
|
|
theme,
|
|
chipTextColor,
|
|
),
|
|
label:
|
|
token.type === "skill"
|
|
? (skillLabels.get(token.value) ?? token.value)
|
|
: model
|
|
? model.label
|
|
: token.type === "context"
|
|
? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}`
|
|
: token.type === "citation"
|
|
? token.value
|
|
: basename(token.value),
|
|
detail: token.type === "context" ? composerChipSizeSuffix(record) : "",
|
|
iconUri: model ? modelChipIconUri(model) : contextChipIconUri(icon),
|
|
tintsIcon: model ? modelChipIcon(model).tintsIcon : icon.kind === "chip",
|
|
};
|
|
}),
|
|
);
|
|
}, [
|
|
props.value,
|
|
tokens,
|
|
props.context,
|
|
skillLabels,
|
|
modelsBySource,
|
|
modelTokens,
|
|
theme,
|
|
chipTextColor,
|
|
]);
|
|
// Every render resolves against the snapshot history, so a render whose
|
|
// (value, selection) lags the acknowledged native state is stamped behind
|
|
// the native revision and rejected by the editor instead of re-applying a
|
|
// stale caret or stale text mid-typing.
|
|
/* oxlint-disable react/refs -- Native controlled edits must reconcile against the last acknowledged native event before sending the next payload. */
|
|
const controlledEventCount = resolveComposerControlledEventCount(
|
|
props.value,
|
|
selection ?? null,
|
|
mostRecentEventCount,
|
|
nativeEventSnapshotsRef.current,
|
|
);
|
|
const acknowledgesLatestNativeEvent = isComposerNativeEcho(
|
|
props.value,
|
|
selection ?? null,
|
|
mostRecentEventCount,
|
|
nativeEventSnapshotsRef.current,
|
|
);
|
|
/* oxlint-enable react/refs */
|
|
const isNativeEcho =
|
|
controlledEventCount === mostRecentEventCount && acknowledgesLatestNativeEvent;
|
|
const controlledDocumentJson = JSON.stringify({
|
|
value: props.value,
|
|
selection: isNativeEcho ? null : (selection ?? null),
|
|
tokensJson,
|
|
mostRecentEventCount: controlledEventCount,
|
|
isNativeEcho,
|
|
});
|
|
// oxlint-disable-next-line hanzo/no-unreasoned-state-sync -- prunes native editor event snapshots once a committed payload acknowledges them
|
|
useEffect(() => {
|
|
if (!acknowledgesLatestNativeEvent) return;
|
|
nativeEventSnapshotsRef.current = pruneAcknowledgedComposerNativeEvents(
|
|
nativeEventSnapshotsRef.current,
|
|
mostRecentEventCount,
|
|
);
|
|
}, [acknowledgesLatestNativeEvent, mostRecentEventCount, nativeEventSnapshotsRef]);
|
|
const assumedValue = props.value;
|
|
// oxlint-disable-next-line hanzo/no-unreasoned-state-sync -- records the committed controlled payload as the native editor's assumed state
|
|
useEffect(() => {
|
|
// Newer native events make this payload stale; do not record it as applied.
|
|
if (isNativeEcho || controlledEventCount !== mostRecentEventCountRef.current) return;
|
|
nativeEventSnapshotsRef.current = assumeComposerControlledState(
|
|
nativeEventSnapshotsRef.current,
|
|
controlledEventCount,
|
|
assumedValue,
|
|
);
|
|
}, [
|
|
assumedValue,
|
|
controlledEventCount,
|
|
isNativeEcho,
|
|
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- Every changed native payload, including selection-only changes, must update the assumed native snapshot.
|
|
controlledDocumentJson,
|
|
nativeEventSnapshotsRef,
|
|
]);
|
|
const acceptNativeEvent = useCallback(
|
|
(eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {
|
|
const acknowledgedEventCount = acknowledgeComposerNativeEvent(
|
|
mostRecentEventCountRef.current,
|
|
eventCount,
|
|
);
|
|
if (acknowledgedEventCount === null) {
|
|
return false;
|
|
}
|
|
mostRecentEventCountRef.current = acknowledgedEventCount;
|
|
nativeEventSnapshotsRef.current.push({
|
|
eventCount: acknowledgedEventCount,
|
|
value,
|
|
selection: nextSelection,
|
|
});
|
|
return acknowledgedEventCount;
|
|
},
|
|
[nativeEventSnapshotsRef],
|
|
);
|
|
const { systemColorsActive } = useAppearancePreferences();
|
|
const themeJson = JSON.stringify({
|
|
...createNativeComposerTheme(theme),
|
|
selection: Platform.OS === "android" || systemColorsActive ? theme["--color-focus"] : null,
|
|
});
|
|
const resolvedTextStyle = StyleSheet.flatten(textStyle) ?? {};
|
|
const regularFontFamily = FONT_FAMILIES.regular;
|
|
return (
|
|
<TextInputWrapper onPaste={handlePaste} style={[{ minHeight: 0 }, style]}>
|
|
<NativeView
|
|
ref={nativeRef}
|
|
controlledDocumentJson={controlledDocumentJson}
|
|
clipboardFragment={props.clipboardFragment ?? ""}
|
|
themeJson={themeJson}
|
|
placeholder={props.placeholder ?? ""}
|
|
fontFamily={
|
|
typeof resolvedTextStyle.fontFamily === "string"
|
|
? resolvedTextStyle.fontFamily
|
|
: regularFontFamily
|
|
}
|
|
fontSize={
|
|
typeof resolvedTextStyle.fontSize === "number"
|
|
? resolvedTextStyle.fontSize
|
|
: MOBILE_TYPOGRAPHY.body.fontSize
|
|
}
|
|
lineHeight={
|
|
typeof resolvedTextStyle.lineHeight === "number"
|
|
? resolvedTextStyle.lineHeight
|
|
: MOBILE_TYPOGRAPHY.body.lineHeight
|
|
}
|
|
contentInsetVertical={contentInsetVertical}
|
|
singleLineCentered={props.singleLineCentered ?? false}
|
|
editable={props.editable ?? true}
|
|
readOnly={props.readOnly ?? false}
|
|
scrollEnabled={props.scrollEnabled ?? true}
|
|
autoFocus={props.autoFocus ?? false}
|
|
autoCorrect={props.autoCorrect ?? true}
|
|
spellCheck={props.spellCheck ?? true}
|
|
enterBehavior={props.enterBehavior ?? DEFAULT_COMPOSER_ENTER_BEHAVIOR}
|
|
submitEnabled={onSubmit !== undefined}
|
|
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(
|
|
event.nativeEvent.eventCount,
|
|
event.nativeEvent.value,
|
|
event.nativeEvent.selection,
|
|
);
|
|
if (acknowledgedEventCount === false) return;
|
|
onChangeText(event.nativeEvent.value);
|
|
onSelectionChange?.(event.nativeEvent.selection);
|
|
setMostRecentEventCount(acknowledgedEventCount);
|
|
forceNativeEventRender((sequence) => sequence + 1);
|
|
}}
|
|
onComposerSelectionChange={(event) => {
|
|
const acknowledgedEventCount = acceptNativeEvent(
|
|
event.nativeEvent.eventCount,
|
|
event.nativeEvent.value,
|
|
event.nativeEvent.selection,
|
|
);
|
|
if (acknowledgedEventCount === false) return;
|
|
// Android selection events may precede text changes; publish text and revision together to reject stale drafts.
|
|
if (event.nativeEvent.value !== props.value) {
|
|
onChangeText(event.nativeEvent.value);
|
|
}
|
|
onSelectionChange?.(event.nativeEvent.selection);
|
|
setMostRecentEventCount(acknowledgedEventCount);
|
|
forceNativeEventRender((sequence) => sequence + 1);
|
|
}}
|
|
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
|
|
onComposerContextPress={(event) => props.onContextPress?.(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;
|
|
// Sync before capturing async paste targets; props may lag the last native keystroke.
|
|
onChangeText(paste.value);
|
|
onSelectionChange?.(paste.selection);
|
|
onPasteText?.(paste);
|
|
setMostRecentEventCount(acknowledgedEventCount);
|
|
forceNativeEventRender((sequence) => sequence + 1);
|
|
}}
|
|
onComposerFocus={onFocus}
|
|
onComposerBlur={onBlur}
|
|
onComposerContentSizeChange={
|
|
onContentSizeChange === undefined
|
|
? undefined
|
|
: (event) => onContentSizeChange(event.nativeEvent)
|
|
}
|
|
onComposerSubmit={
|
|
onSubmit === undefined
|
|
? undefined
|
|
: (event) => onSubmit(event.nativeEvent.alternate === true)
|
|
}
|
|
/>
|
|
</TextInputWrapper>
|
|
);
|
|
}
|
|
|
|
export type {
|
|
ComposerEditorHandle,
|
|
ComposerEditorProps,
|
|
ComposerEditorSelection,
|
|
ComposerTextPaste,
|
|
} from "./HanzoComposerEditor.types";
|