(props: PromptTokenFieldProps)
| 524 | * object references as tokens via autocomplete. |
| 525 | */ |
| 526 | export function PromptTokenField(props: PromptTokenFieldProps) { |
| 527 | let { |
| 528 | completionTrigger, |
| 529 | renderCompletions, |
| 530 | children, |
| 531 | pixelLoader, |
| 532 | shouldAnimatePixelLoader = false, |
| 533 | placeholder, |
| 534 | menuWidth, |
| 535 | onKeyDown: onKeyDownProp, |
| 536 | onKeyUp: onKeyUpProp |
| 537 | } = props; |
| 538 | let { |
| 539 | prompt, |
| 540 | setPrompt, |
| 541 | acceptedAttachmentTypes, |
| 542 | setAttachments, |
| 543 | onAddAttachments, |
| 544 | inputRef, |
| 545 | onSubmit, |
| 546 | isGenerating, |
| 547 | isListening, |
| 548 | size |
| 549 | } = useContext(PromptFieldContext); |
| 550 | let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/ai'); |
| 551 | let [isFocused, setFocused] = useState(false); |
| 552 | |
| 553 | let [filterAnchor, filterValue, filterType] = useMemo(() => { |
| 554 | // If on a placeholder token, show suggestions. |
| 555 | let slice = prompt.slice(prompt.selectedRange.start, prompt.selectedRange.end); |
| 556 | let segment = slice.segments.length === 1 ? slice.segments[0] : null; |
| 557 | if ( |
| 558 | segment?.type === 'token' && |
| 559 | ((segment.value?.type === 'placeholder' && segment.value.placeholderType === 'token') || |
| 560 | segment.value?.type === 'custom') |
| 561 | ) { |
| 562 | return [prompt.selectedRange.start, segment.value.anchor, segment.value.valueType ?? null]; |
| 563 | } |
| 564 | |
| 565 | if (completionTrigger) { |
| 566 | // Find a preceding anchor token. This tells us what kind of object to filter for. |
| 567 | let anchorTokenIndex = -1; |
| 568 | let filterType: string | null = null; |
| 569 | for ( |
| 570 | let index = Math.min(prompt.selectedRange.anchor.index, prompt.segments.length - 1); |
| 571 | index >= 0; |
| 572 | index-- |
| 573 | ) { |
| 574 | let segment = prompt.segments[index]; |
| 575 | if (segment.type === 'token' && segment.value?.type === 'anchor') { |
| 576 | anchorTokenIndex = index; |
| 577 | filterType = segment.value?.valueType; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | let filterAnchor = prompt.findText( |
| 583 | prompt.caretPosition, |
nothing calls this directly
no test coverage detected