Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions packages/instantsearch.js/src/connectors/chat/connectChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import type {
IndexUiState,
IndexWidget,
} from '../../types';
import type {
AddToolResultWithOutput,
UserClientSideTool,
} from 'instantsearch-ui-components';

const withUsage = createDocumentationMessageGenerator({
name: 'chat',
Expand Down Expand Up @@ -84,6 +88,10 @@ export type ChatConnectorParams<TUiMessage extends UIMessage = UIMessage> = (
* Whether to resume an ongoing chat generation stream.
*/
resume?: boolean;
/**
* Configuration for client-side tools.
*/
tools?: Record<string, Omit<UserClientSideTool, 'layoutComponent'>>;
};

export type ChatWidgetDescription<TUiMessage extends UIMessage = UIMessage> = {
Expand All @@ -106,7 +114,7 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
return <TUiMessage extends UIMessage = UIMessage>(
widgetParams: TWidgetParams & ChatConnectorParams<TUiMessage>
) => {
const { resume = false, ...options } = widgetParams || {};
const { resume = false, tools = {}, ...options } = widgetParams || {};

let _chatInstance: Chat<TUiMessage>;
let input = '';
Expand Down Expand Up @@ -153,17 +161,35 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
);
}

const optionsWithTransport =
'chat' in options
? options
: {
...options,
transport,
};

return 'chat' in optionsWithTransport
? optionsWithTransport.chat
: new Chat(optionsWithTransport);
if ('chat' in options) {
return options.chat;
}

return new Chat({
...options,
transport,
onToolCall({ toolCall }) {
const tool = tools[toolCall.toolName];

if (!tool?.onToolCall) {
return Promise.resolve();
}

const addToolResult: AddToolResultWithOutput = ({ output }) =>
Promise.resolve(
_chatInstance.addToolResult({
output,
tool: toolCall.toolName,
toolCallId: toolCall.toolCallId,
})
);

return tool.onToolCall({
...toolCall,
addToolResult,
});
},
});
};

return {
Expand Down
28 changes: 5 additions & 23 deletions packages/instantsearch.js/src/widgets/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import type {
IndexWidget,
} from '../../types';
import type {
AddToolResultWithOutput,
ChatClassNames,
ChatHeaderProps,
ChatHeaderTranslations,
Expand Down Expand Up @@ -806,29 +805,12 @@ export default (function chat<
render(null, containerNode)
);

const { chatInstance, ...rest } = makeWidget({
resume,
...options,
onToolCall: ({ toolCall }) => {
const tool = tools[toolCall.toolName];

if (tool && tool.onToolCall) {
const scopedAddToolResult: AddToolResultWithOutput = ({ output }) => {
return Promise.resolve(
chatInstance.addToolResult({
output,
tool: toolCall.toolName,
toolCallId: toolCall.toolCallId,
})
);
};
tool.onToolCall({ ...toolCall, addToolResult: scopedAddToolResult });
}
},
});

return {
...rest,
...makeWidget({
resume,
tools,
...options,
}),
$$widgetType: 'ais.chat',
};
} satisfies ChatWidget);
Expand Down