@@ -11,12 +11,6 @@ const ChatUiComponent = createChatComponent({
11
11
Fragment,
12
12
} ) ;
13
13
14
- export type ChatProps = {
15
- agentId : string ;
16
- itemComponent ?: ( props : { item : Record < string , unknown > } ) => JSX . Element ;
17
- tools ?: Tools ;
18
- } ;
19
-
20
14
const createDefaultTools = (
21
15
itemComponent ?: ( props : { item : Record < string , unknown > } ) => JSX . Element
22
16
) : Tools => {
@@ -46,7 +40,25 @@ const createDefaultTools = (
46
40
] ;
47
41
} ;
48
42
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 ) {
50
62
const { indexUiState, setIndexUiState } = useInstantSearch ( ) ;
51
63
52
64
const [ open , setOpen ] = React . useState ( false ) ;
@@ -56,17 +68,33 @@ export function Chat({ agentId, tools: userTools, itemComponent }: ChatProps) {
56
68
return [ ...createDefaultTools ( itemComponent ) , ...( userTools ?? [ ] ) ] ;
57
69
} , [ userTools ] ) ;
58
70
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
+
59
95
const { messages, sendMessage } = useChat ( {
60
96
// 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,
70
98
onToolCall : ( params ) => {
71
99
tools ?. forEach ( ( tool ) => {
72
100
if ( tool . type === params . toolCall . toolName ) {
0 commit comments