Skip to content

Commit 6485ee2

Browse files
committed
refactor(ai): define Typesense ask-ai message and config types
1 parent e30d52f commit 6485ee2

3 files changed

Lines changed: 39 additions & 34 deletions

File tree

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
import type { UIMessage } from '@ai-sdk/react';
2-
import type { ToolUIPart, UIDataTypes, UIMessagePart } from 'ai';
1+
import type { MultiSearchRequestSchema } from 'typesense/lib/Typesense/Types';
32

43
export type AskAiState = 'conversation-history' | 'conversation' | 'initial' | 'new-conversation';
54

6-
export interface SearchIndexTool {
7-
input: {
8-
query: string;
9-
};
10-
output: {
11-
query?: string;
12-
hits?: any[];
13-
};
14-
}
5+
export type AskAiStatus = 'error' | 'ready' | 'streaming' | 'submitted';
156

16-
export interface AgentStudioSearchTool {
17-
input: {
18-
index: string;
19-
query: string;
20-
number_of_results: number;
21-
facet_filters: any | null;
22-
};
23-
output: {
24-
hits?: any[];
25-
nbHits?: number;
26-
queryID?: string;
27-
};
7+
export interface AITextPart {
8+
type: 'text';
9+
text: string;
10+
state?: 'done' | 'streaming';
2811
}
2912

30-
type Tools = {
31-
searchIndex: SearchIndexTool;
32-
algolia_search_index: AgentStudioSearchTool;
13+
export type AIMessagePart = AITextPart;
14+
15+
export type AIMessage = {
16+
id: string;
17+
role: 'assistant' | 'user';
18+
parts: AIMessagePart[];
19+
metadata?: {
20+
stopped?: boolean;
21+
conversationId?: string;
22+
};
3323
};
3424

35-
export type AIMessage = UIMessage<{ stopped?: boolean }, UIDataTypes, Tools>;
25+
export type UseAskAiSendMessageOptions = {
26+
suggestedQuestionId?: string;
27+
};
3628

37-
export type AIMessagePart = UIMessagePart<UIDataTypes, Tools>;
29+
export type TypesenseAskAiSearchParameters = Omit<
30+
Partial<MultiSearchRequestSchema<Record<string, unknown>, string>>,
31+
'collection' | 'exclude_fields' | 'q' | 'query_by'
32+
>;
3833

39-
export type AIToolPart = ToolUIPart<Tools>;
34+
export type TypesenseAskAiParams = {
35+
collection: string;
36+
conversationModelId: string;
37+
excludeFields?: string;
38+
queryBy: string;
39+
searchParameters?: TypesenseAskAiSearchParameters;
40+
};

packages/docsearch-react/src/types/StoredDocSearchHit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type StoredAskAiMessage = AIMessage & {
99
};
1010

1111
export type StoredAskAiState = StoredDocSearchHit & {
12+
conversationId?: string;
1213
stopped?: boolean;
1314
messages?: StoredAskAiMessage[];
1415
};

packages/docsearch-react/src/utils/ai.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { TextUIPart } from 'ai';
2-
31
import type { StoredAskAiState } from '../types';
4-
import type { AIMessage } from '../types/AskiAi';
2+
import type { AIMessage, AITextPart } from '../types/AskiAi';
53

64
import { sanitizeUserInput } from './sanitize';
75

@@ -69,11 +67,16 @@ export function extractLinksFromMessage(message: AIMessage | null): ExtractedLin
6967
return links;
7068
}
7169

72-
export const buildDummyAskAiHit = (query: string, messages: AIMessage[]): StoredAskAiState => {
70+
export const buildDummyAskAiHit = (
71+
query: string,
72+
messages: AIMessage[],
73+
conversationId?: string
74+
): StoredAskAiState => {
7375
const textPart = messages[0].parts.find((part) => part.type === 'text');
7476
const sanitizedText = textPart?.text ? sanitizeUserInput(textPart.text) : '';
7577

7678
return {
79+
conversationId,
7780
query,
7881
objectID: sanitizedText,
7982
messages,
@@ -96,7 +99,7 @@ export const buildDummyAskAiHit = (query: string, messages: AIMessage[]): Stored
9699
};
97100
};
98101

99-
export const getMessageContent = (message: AIMessage | null): TextUIPart | undefined =>
102+
export const getMessageContent = (message: AIMessage | null): AITextPart | undefined =>
100103
message?.parts.find((part) => part.type === 'text');
101104

102105
/**

0 commit comments

Comments
 (0)