(props: PromptTokenFieldProps)
| 470 | * object references as tokens via autocomplete. |
| 471 | */ |
| 472 | export 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, |
nothing calls this directly
no test coverage detected