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
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,47 @@ describe('connectChat', () => {
}),
});
});

it('uses custom `type` as key in getRenderState', () => {
const render = jest.fn();
const makeWidget = connectChat(render);
const widget = makeWidget({ type: 'customChat', agentId: 'agentId' });

const helper = algoliasearchHelper(createSearchClient(), '');

const instantSearchInstance: Pick<
InstantSearch,
'client' | 'getUiState'
> = {
client: createSearchClient(),
getUiState: () => ({ indexName: {} }),
};
const parent: Pick<IndexWidget, 'getIndexId' | 'setIndexUiState'> = {
getIndexId: () => 'indexName',
setIndexUiState: () => {},
};

const result = widget.getRenderState(
{
// @ts-expect-error
searchBox: {},
},
createInitOptions({
helper,
state: helper.state,
instantSearchInstance: instantSearchInstance as InstantSearch,
parent: parent as IndexWidget,
})
);

expect(result).toHaveProperty('customChat');
// @ts-expect-error access dynamic key
expect(result.customChat).toEqual(
expect.objectContaining({
widgetParams: expect.objectContaining({ type: 'customChat' }),
})
);
});
});

it('renders during init and render', () => {
Expand Down
16 changes: 14 additions & 2 deletions packages/instantsearch.js/src/connectors/chat/connectChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ export type ChatConnectorParams<TUiMessage extends UIMessage = UIMessage> = (
* Configuration for client-side tools.
*/
tools?: Record<string, Omit<UserClientSideTool, 'layoutComponent'>>;
/**
* Identifier of this type of chat widget. This is used for the key in renderState.
* @default 'chat'
*/
type?: string;
};

export type ChatWidgetDescription<TUiMessage extends UIMessage = UIMessage> = {
$$type: 'ais.chat';
renderState: ChatRenderState<TUiMessage>;
indexRenderState: {
// In IndexRenderState, the key is always 'chat', but in the widgetParams you can customize it with the `type` parameter
chat: WidgetRenderState<
ChatRenderState<TUiMessage>,
ChatConnectorParams<TUiMessage>
Expand All @@ -146,7 +152,12 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
) => {
warning(false, 'Chat is not yet stable and will change in the future.');

const { resume = false, tools = {}, ...options } = widgetParams || {};
const {
resume = false,
tools = {},
type = 'chat',
...options
} = widgetParams || {};

let _chatInstance: Chat<TUiMessage>;
let input = '';
Expand Down Expand Up @@ -320,7 +331,8 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
): IndexRenderState & ChatWidgetDescription['indexRenderState'] {
return {
...renderState,
chat: this.getWidgetRenderState(renderOptions),
// Type is casted to 'chat' here, because in the IndexRenderState the key is always 'chat'
[type as 'chat']: this.getWidgetRenderState(renderOptions),
};
},

Expand Down