Skip to content

Commit bf410e9

Browse files
committed
add transport to props
1 parent 4541336 commit bf410e9

File tree

1 file changed

+44
-16
lines changed
  • packages/react-instantsearch/src/widgets/Chat

1 file changed

+44
-16
lines changed

packages/react-instantsearch/src/widgets/Chat/Chat.tsx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ const ChatUiComponent = createChatComponent({
1111
Fragment,
1212
});
1313

14-
export type ChatProps = {
15-
agentId: string;
16-
itemComponent?: (props: { item: Record<string, unknown> }) => JSX.Element;
17-
tools?: Tools;
18-
};
19-
2014
const createDefaultTools = (
2115
itemComponent?: (props: { item: Record<string, unknown> }) => JSX.Element
2216
): Tools => {
@@ -46,7 +40,25 @@ const createDefaultTools = (
4640
];
4741
};
4842

49-
export function Chat({ agentId, tools: userTools, itemComponent }: ChatProps) {
43+
export type ChatProps = {
44+
itemComponent?: (props: { item: Record<string, unknown> }) => JSX.Element;
45+
tools?: Tools;
46+
} & (
47+
| { agentId: string; transport?: never }
48+
| {
49+
transport: NonNullable<
50+
ConstructorParameters<typeof DefaultChatTransport>[0]
51+
>;
52+
agentId?: never;
53+
}
54+
);
55+
56+
export function Chat({
57+
tools: userTools,
58+
agentId,
59+
transport: userTransport,
60+
itemComponent,
61+
}: ChatProps) {
5062
const { indexUiState, setIndexUiState } = useInstantSearch();
5163

5264
const [open, setOpen] = React.useState(false);
@@ -56,17 +68,33 @@ export function Chat({ agentId, tools: userTools, itemComponent }: ChatProps) {
5668
return [...createDefaultTools(itemComponent), ...(userTools ?? [])];
5769
}, [userTools]);
5870

71+
const transport = React.useMemo(() => {
72+
if (!userTransport && !agentId) {
73+
throw new Error(
74+
'You need to provide either an `agentId` or a `transport`.'
75+
);
76+
}
77+
if (userTransport && agentId) {
78+
throw new Error(
79+
'You cannot provide both an `agentId` and a `transport`.'
80+
);
81+
}
82+
if (!userTransport && agentId) {
83+
return new DefaultChatTransport({
84+
api: `https://agent-studio-staging.eu.algolia.com/1/agents/${agentId}/completions?stream=true&compatibilityMode=ai-sdk-5`,
85+
headers: {
86+
'x-algolia-application-id': 'F4T6CUV2AH',
87+
'X-Algolia-API-Key': '93aba0bf5908533b213d93b2410ded0c',
88+
},
89+
});
90+
}
91+
92+
return new DefaultChatTransport(userTransport);
93+
}, [userTransport]);
94+
5995
const { messages, sendMessage } = useChat({
6096
// TODO: loading indicator
61-
// TODO: override transport here from props
62-
transport: new DefaultChatTransport({
63-
// api: `https://generative-eu.algolia.com/1/agents/${agentId}/completions?stream=true&compatibilityMode=ai-sdk-5`,
64-
api: `https://agent-studio-staging.eu.algolia.com/1/agents/${agentId}/completions?stream=true&compatibilityMode=ai-sdk-5`,
65-
headers: {
66-
'x-algolia-application-id': 'F4T6CUV2AH',
67-
'X-Algolia-API-Key': '93aba0bf5908533b213d93b2410ded0c',
68-
},
69-
}),
97+
transport,
7098
onToolCall: (params) => {
7199
tools?.forEach((tool) => {
72100
if (tool.type === params.toolCall.toolName) {

0 commit comments

Comments
 (0)