MCPcopy Create free account
hub / github.com/adobe/react-spectrum / PromptTokenField

Function PromptTokenField

packages/@react-spectrum/ai/src/PromptField.tsx:472–764  ·  view source on GitHub ↗
(props: PromptTokenFieldProps)

Source from the content-addressed store, hash-verified

470 * object references as tokens via autocomplete.
471 */
472export function PromptTokenField(props: PromptTokenFieldProps) {
473 let {
474 completionTrigger,
475 renderCompletions,
476 children,
477 pixelLoader,
478 shouldAnimatePixelLoader = false,
479 placeholder,
480 menuWidth,
481 onKeyDown: onKeyDownProp,
482 onKeyUp: onKeyUpProp
483 } = props;
484 let {
485 prompt,
486 setPrompt,
487 acceptedAttachmentTypes,
488 setAttachments,
489 onAddAttachments,
490 inputRef,
491 onSubmit,
492 isGenerating,
493 isListening,
494 size
495 } = useContext(PromptFieldContext);
496 let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/ai');
497 let [isFocused, setFocused] = useState(false);
498
499 let [filterAnchor, filterValue, filterType] = useMemo(() => {
500 // If on a placeholder token, show suggestions.
501 let slice = prompt.slice(prompt.selectedRange.start, prompt.selectedRange.end);
502 let segment = slice.segments.length === 1 ? slice.segments[0] : null;
503 if (
504 segment?.type === 'token' &&
505 ((segment.value?.type === 'placeholder' && segment.value.placeholderType === 'token') ||
506 segment.value?.type === 'custom')
507 ) {
508 return [prompt.selectedRange.start, segment.value.anchor, segment.value.valueType ?? null];
509 }
510
511 if (completionTrigger) {
512 // Find a preceding anchor token. This tells us what kind of object to filter for.
513 let anchorTokenIndex = -1;
514 let filterType: string | null = null;
515 for (
516 let index = Math.min(prompt.selectedRange.anchor.index, prompt.segments.length - 1);
517 index >= 0;
518 index--
519 ) {
520 let segment = prompt.segments[index];
521 if (segment.type === 'token' && segment.value?.type === 'anchor') {
522 anchorTokenIndex = index;
523 filterType = segment.value?.valueType;
524 break;
525 }
526 }
527
528 let filterAnchor = prompt.findText(
529 prompt.caretPosition,

Callers

nothing calls this directly

Calls 15

useKeyboardFunction · 0.90
spaceFunction · 0.90
getInteractionModalityFunction · 0.90
matchMimeTypeFunction · 0.90
cssFunction · 0.90
scrollFadeFunction · 0.90
childrenFunction · 0.85
sliceMethod · 0.80
findTextMethod · 0.80
withSelectedRangeMethod · 0.80
getAsFileMethod · 0.80

Tested by

no test coverage detected