Feat add autocomplete (#18)

Co-authored-by: Shubham Naik <shub@memgpt.ai>
This commit is contained in:
Shubham Naik
2025-10-28 14:24:56 -07:00
committed by GitHub
parent 4ac4412fcc
commit 948684dfac
9 changed files with 711 additions and 28 deletions

View File

@@ -20,6 +20,8 @@ interface PasteAwareTextInputProps {
onSubmit?: (value: string) => void;
placeholder?: string;
focus?: boolean;
cursorPosition?: number;
onCursorMove?: (position: number) => void;
}
function countLines(text: string): number {
@@ -32,6 +34,8 @@ export function PasteAwareTextInput({
onSubmit,
placeholder,
focus = true,
cursorPosition,
onCursorMove,
}: PasteAwareTextInputProps) {
const [displayValue, setDisplayValue] = useState(value);
const [actualValue, setActualValue] = useState(value);
@@ -41,6 +45,20 @@ export function PasteAwareTextInput({
const [nudgeCursorOffset, setNudgeCursorOffset] = useState<
number | undefined
>(undefined);
// Apply cursor position from parent
useEffect(() => {
if (typeof cursorPosition === "number") {
setNudgeCursorOffset(cursorPosition);
caretOffsetRef.current = cursorPosition;
}
}, [cursorPosition]);
// Notify parent of cursor position changes
// Default assumption: cursor is at the end when typing
useEffect(() => {
onCursorMove?.(displayValue.length);
}, [displayValue, onCursorMove]);
const TextInputAny = RawTextInput as unknown as React.ComponentType<{
value: string;
onChange: (value: string) => void;
@@ -218,7 +236,7 @@ export function PasteAwareTextInput({
const resolved = resolvePlaceholders(newValue);
setActualValue(resolved);
onChange(newValue);
// Default caret behavior on typing/appends: move to end
// Default: cursor moves to end (most common case)
caretOffsetRef.current = newValue.length;
};