-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathpromptSuggestionUtils.ts
More file actions
55 lines (50 loc) · 1.34 KB
/
promptSuggestionUtils.ts
File metadata and controls
55 lines (50 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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]
);
}