-
Notifications
You must be signed in to change notification settings - Fork 554
feat(autocomplete): add prompt suggestions #6900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0721d3
poc(autocomplete): add prompt suggestions
FabienMotte 2d62200
poc: rename to `showQuerySuggestions` + add `searchParameters`
FabienMotte 0b2f016
wip
Haroenv e7b0a7a
abracadabra
Haroenv aa8c808
lock
Haroenv 40734dd
finalize implementation
aymeric-giraudet 99305c2
Merge branch 'master' into poc/autocomplete-prompt-suggestions
aymeric-giraudet e90875d
fix CI
aymeric-giraudet d0ed37c
revert ask AI fallback
aymeric-giraudet 8618fd9
bundlesize again
aymeric-giraudet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../instantsearch-ui-components/src/components/autocomplete/AutocompletePromptSuggestion.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** @jsx createElement */ | ||
|
|
||
| import { cx } from '../../lib'; | ||
| import { SparklesIcon } from '../chat/icons'; | ||
|
|
||
| import type { ComponentChildren, Renderer } from '../../types'; | ||
|
|
||
| export type AutocompletePromptSuggestionProps< | ||
| T = { prompt: string; label?: string } & Record<string, unknown> | ||
| > = { | ||
| item: T; | ||
| onSelect: () => void; | ||
| children?: ComponentChildren; | ||
| classNames?: Partial<AutocompletePromptSuggestionClassNames>; | ||
| }; | ||
|
|
||
| export type AutocompletePromptSuggestionClassNames = { | ||
| /** | ||
| * Class names to apply to the root element. | ||
| */ | ||
| root: string | string[]; | ||
| /** | ||
| * Class names to apply to the content element. | ||
| */ | ||
| content: string | string[]; | ||
| /** | ||
| * Class names to apply to the icon element. | ||
| */ | ||
| icon: string | string[]; | ||
| /** | ||
| * Class names to apply to the body element. | ||
| */ | ||
| body: string | string[]; | ||
| }; | ||
|
|
||
| export function createAutocompletePromptSuggestionComponent({ | ||
| createElement, | ||
| }: Renderer) { | ||
| return function AutocompletePromptSuggestion( | ||
| userProps: AutocompletePromptSuggestionProps | ||
| ) { | ||
| const { item, onSelect, children, classNames = {} } = userProps; | ||
| const label = item.label || item.prompt; | ||
|
|
||
| return ( | ||
| <div | ||
| onClick={onSelect} | ||
| className={cx( | ||
| 'ais-AutocompleteItemWrapper', | ||
| 'ais-AutocompletePromptSuggestionWrapper', | ||
| classNames.root | ||
| )} | ||
| > | ||
| <div | ||
| className={cx( | ||
| 'ais-AutocompleteItemContent', | ||
| 'ais-AutocompletePromptSuggestionItemContent', | ||
| classNames.content | ||
| )} | ||
| > | ||
| <div | ||
| className={cx( | ||
| 'ais-AutocompleteItemIcon', | ||
| 'ais-AutocompletePromptSuggestionItemIcon', | ||
| classNames.icon | ||
| )} | ||
| > | ||
| <SparklesIcon createElement={createElement} /> | ||
| </div> | ||
| <div | ||
| className={cx( | ||
| 'ais-AutocompleteItemContentBody', | ||
| 'ais-AutocompletePromptSuggestionItemContentBody', | ||
| classNames.body | ||
| )} | ||
| > | ||
| {children ?? label} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/instantsearch-ui-components/src/components/autocomplete/promptSuggestionUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| export const PROMPT_SUGGESTION_FLAG = '__isPromptSuggestion' as const; | ||
| export const PROMPT_SUGGESTION_FALLBACK_FLAG = | ||
| '__isPromptSuggestionFallback' as const; | ||
|
|
||
| export function getPromptSuggestionHits({ | ||
| hits, | ||
| query, | ||
| limit, | ||
| }: { | ||
| hits: Array<{ objectID: string } & Record<string, unknown>>; | ||
| query: string; | ||
| limit: number; | ||
| }): Array<{ objectID: string } & Record<string, unknown>> { | ||
| const promptHits = hits.slice(0, limit).map((hit) => ({ | ||
| ...hit, | ||
| [PROMPT_SUGGESTION_FLAG]: true, | ||
| })); | ||
|
|
||
| if (promptHits.length > 0 || query.trim().length === 0) { | ||
| return promptHits; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| objectID: `ask-about:${encodeURIComponent(query)}`, | ||
| prompt: query, | ||
| label: `Ask about "${query}"`, | ||
| [PROMPT_SUGGESTION_FLAG]: true, | ||
| [PROMPT_SUGGESTION_FALLBACK_FLAG]: true, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| export function isPromptSuggestion(item: unknown): item is { | ||
| prompt: string; | ||
| [PROMPT_SUGGESTION_FLAG]: true; | ||
| } { | ||
| return Boolean( | ||
| item && | ||
| typeof item === 'object' && | ||
| (item as Record<string, unknown>)[PROMPT_SUGGESTION_FLAG] | ||
| ); | ||
| } | ||
|
|
||
| export function isPromptSuggestionFallback(item: unknown): item is { | ||
| prompt: string; | ||
| label?: string; | ||
| [PROMPT_SUGGESTION_FALLBACK_FLAG]: true; | ||
| } { | ||
| return Boolean( | ||
| item && | ||
| typeof item === 'object' && | ||
| (item as Record<string, unknown>)[PROMPT_SUGGESTION_FALLBACK_FLAG] | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.