Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the created at field in assistant response and use assistant #3129 #3163

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/ai/streams/assistant-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function AssistantResponse(
textEncoder.encode(
formatStreamPart('assistant_message', {
id: value.data.id,
createdAt: value.data.created_at ? new Date(value.data.created_at * 1000) : undefined,
role: 'assistant',
content: [{ type: 'text', text: { value: '' } }],
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/use-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export function useAssistant({
{
id: value.id,
role: value.role,
createdAt: value.createdAt,
content: value.content[0].text.value,
},
]);
Expand All @@ -201,6 +202,7 @@ export function useAssistant({
{
id: lastMessage.id,
role: lastMessage.role,
createdAt: lastMessage.createdAt,
content: lastMessage.content + value,
},
];
Expand Down
86 changes: 86 additions & 0 deletions packages/react/src/use-assistant.ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('stream data stream', () => {
<div data-testid={`message-${idx}`} key={idx}>
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
{m.createdAt && ` (Created at: ${m.createdAt})`}
</div>
))}

Expand Down Expand Up @@ -85,6 +86,91 @@ describe('stream data stream', () => {
);
});

it('should show streamed response with createdAt', async () => {
const { requestBody } = mockFetchDataStream({
url: 'https://example.com/api/assistant',
chunks: [
formatStreamPart('assistant_control_data', {
threadId: 't0',
messageId: 'm0',
}),
formatStreamPart('assistant_message', {
id: 'm0',
role: 'assistant',
createdAt: new Date(1727703377 * 1000),
content: [{ type: 'text', text: { value: '' } }],
}),
// text parts:
'0:"Hello"\n',
'0:","\n',
'0:" world"\n',
'0:"."\n',
],
});

await userEvent.click(screen.getByTestId('do-append'));

await screen.findByTestId('message-0');
expect(screen.getByTestId('message-0')).toHaveTextContent('User: hi');

await screen.findByTestId('message-1');
const messageContent = screen.getByTestId('message-1').textContent;
console.log(messageContent);

expect(messageContent).toContain('AI: Hello, world.');
expect(messageContent).toContain(`Created at: ${new Date(1727703377 * 1000).toISOString()}`);

// check that correct information was sent to the server:
expect(await requestBody).toStrictEqual(
JSON.stringify({
threadId: null,
message: 'hi',
}),
);
});

it('should show streamed response without createdAt', async () => {
const { requestBody } = mockFetchDataStream({
url: 'https://example.com/api/assistant',
chunks: [
formatStreamPart('assistant_control_data', {
threadId: 't0',
messageId: 'm0',
}),
formatStreamPart('assistant_message', {
id: 'm0',
role: 'assistant',
content: [{ type: 'text', text: { value: '' } }],
}),
// text parts:
'0:"Hello"\n',
'0:","\n',
'0:" world"\n',
'0:"."\n',
],
});

await userEvent.click(screen.getByTestId('do-append'));

await screen.findByTestId('message-0');
expect(screen.getByTestId('message-0')).toHaveTextContent('User: hi');

await screen.findByTestId('message-1');
const messageContent = screen.getByTestId('message-1').textContent;
console.log(messageContent);

expect(messageContent).toContain('AI: Hello, world.');
expect(messageContent).not.toContain('Created at:');

// check that correct information was sent to the server:
expect(await requestBody).toStrictEqual(
JSON.stringify({
threadId: null,
message: 'hi',
}),
);
});

it('should show error response', async () => {
mockFetchError({ statusCode: 500, errorMessage: 'Internal Error' });

Expand Down
2 changes: 1 addition & 1 deletion packages/ui-utils/src/stream-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const assistantMessageStreamPart: StreamPart<

return {
type: 'assistant_message',
value: value as AssistantMessage,
value: value as unknown as AssistantMessage,
};
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/ui-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ export type JSONValue =
export type AssistantMessage = {
id: string;
role: 'assistant';
createdAt?: Date;
content: Array<{
type: 'text';
text: {
Expand Down