Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 21 additions & 5 deletions packages/instantsearch.js/src/connectors/chat/connectChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ export type ChatConnectorParams<TUiMessage extends UIMessage = UIMessage> = (
* When `resume` is enabled, this message is not sent.
*/
initialUserMessage?: string;
/**
* Messages to pre-populate the chat with when it is initialized.
*
* These messages are set without triggering an AI response. They are only
* applied when the chat has no existing messages yet. If messages were
* restored or otherwise already exist when the widget starts, these messages
* are not applied.
*
* When `resume` is enabled, these messages are not applied.
*/
initialMessages?: TUiMessage[];
};

export type ChatWidgetDescription<TUiMessage extends UIMessage = UIMessage> = {
Expand Down Expand Up @@ -271,6 +282,7 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
tools = {},
type = 'chat',
initialUserMessage,
initialMessages,
...options
} = widgetParams || {};

Expand Down Expand Up @@ -554,6 +566,14 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
};
}

const hasExistingMessages = _chatInstance.messages.length > 0;

// Set initialMessages before registering callbacks to avoid
// triggering re-renders during init
if (initialMessages && !resume && !hasExistingMessages) {
_chatInstance.messages = initialMessages;
}
Comment thread
aymeric-giraudet marked this conversation as resolved.

_chatInstance['~registerErrorCallback'](render);
_chatInstance['~registerMessagesCallback'](render);
_chatInstance['~registerStatusCallback'](render);
Expand All @@ -562,11 +582,7 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
_chatInstance.resumeStream();
}

if (
initialUserMessage &&
!resume &&
_chatInstance.messages.length === 0
) {
if (initialUserMessage && !resume && !hasExistingMessages) {
_chatInstance.sendMessage({ text: initialUserMessage });
}

Expand Down
122 changes: 122 additions & 0 deletions tests/common/connectors/chat/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,128 @@ export function createOptionsTests(
expect(sendMessageSpy).not.toHaveBeenCalled();
});

test('sets initialMessages on init', async () => {
const chat = new Chat({});

const options: SetupOptions<ChatConnectorSetup> = {
Comment thread
aymeric-giraudet marked this conversation as resolved.
instantSearchOptions: {
indexName: 'indexName',
searchClient: createSearchClient(),
},
widgetParams: {
chat,
agentId: 'agentId',
initialMessages: [
{
id: '1',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
} as any,
};

await setup(options);

await act(async () => {
await wait(0);
});

expect(chat.messages).toHaveLength(1);
expect(chat.messages[0]).toEqual(
expect.objectContaining({
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
})
);
});

test('does not set initialMessages when messages already exist', async () => {
const chat = new Chat({
messages: [
{
id: '1',
role: 'user',
parts: [{ type: 'text', text: 'Previous message' }],
},
],
});

const options: SetupOptions<ChatConnectorSetup> = {
instantSearchOptions: {
indexName: 'indexName',
searchClient: createSearchClient(),
},
widgetParams: {
chat,
agentId: 'agentId',
initialMessages: [
{
id: '2',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
} as any,
};

await setup(options);

await act(async () => {
await wait(0);
});

expect(chat.messages).toHaveLength(1);
expect(chat.messages[0]).toEqual(
expect.objectContaining({
role: 'user',
parts: [{ type: 'text', text: 'Previous message' }],
})
);
});

test('applies initialMessages and sends initialUserMessage together', async () => {
sessionStorage.clear();
const chat = new Chat({});
const sendMessageSpy = jest
.spyOn(chat, 'sendMessage')
.mockResolvedValue(undefined);

const options: SetupOptions<ChatConnectorSetup> = {
instantSearchOptions: {
indexName: 'indexName',
searchClient: createSearchClient(),
},
widgetParams: {
chat,
agentId: 'agentId',
initialMessages: [
{
id: '1',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
initialUserMessage: 'Hello, AI!',
} as any,
};

await setup(options);

await act(async () => {
await wait(0);
});

expect(chat.messages).toHaveLength(1);
expect(chat.messages[0]).toEqual(
expect.objectContaining({
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
})
);
expect(sendMessageSpy).toHaveBeenCalledWith({ text: 'Hello, AI!' });
});

test('provides `input` state to persist text input', async () => {
const options: SetupOptions<ChatConnectorSetup> = {
instantSearchOptions: {
Expand Down
105 changes: 105 additions & 0 deletions tests/common/widgets/chat/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,111 @@ export function createOptionsTests(
expect(sendMessageSpy).not.toHaveBeenCalled();
});

test('sets initialMessages on init', async () => {
sessionStorage.clear();
const searchClient = createSearchClient();

const chat = new Chat({});

await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: {
javascript: {
...createDefaultWidgetParams(chat),
initialMessages: [
{
id: '1',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
},
react: {
...createDefaultWidgetParams(chat),
initialMessages: [
{
id: '1',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
},
vue: {},
},
});

await act(async () => {
await wait(0);
});

expect(chat.messages).toHaveLength(1);
expect(chat.messages[0]).toEqual(
expect.objectContaining({
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
})
);
});

test('does not set initialMessages when messages already exist', async () => {
const searchClient = createSearchClient();

const chat = new Chat({
messages: [
{
id: '1',
role: 'user',
parts: [{ type: 'text', text: 'Previous message' }],
},
],
});

await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: {
javascript: {
...createDefaultWidgetParams(chat),
initialMessages: [
{
id: '2',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
},
react: {
...createDefaultWidgetParams(chat),
initialMessages: [
{
id: '2',
role: 'assistant',
parts: [{ type: 'text', text: 'Welcome! How can I help?' }],
},
],
},
vue: {},
},
});

await act(async () => {
await wait(0);
});

expect(chat.messages).toHaveLength(1);
expect(chat.messages[0]).toEqual(
expect.objectContaining({
role: 'user',
parts: [{ type: 'text', text: 'Previous message' }],
})
);
});

test('sends messages when prompt is submitted', async () => {
const searchClient = createSearchClient();

Expand Down
Loading