Skip to content

Commit

Permalink
persist reasoning tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon committed Feb 3, 2025
1 parent b3ee45c commit 0f1b6a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
28 changes: 14 additions & 14 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,24 @@ export async function POST(request: Request) {
dataStream,
}),
},
onFinish: async ({ response }) => {
onFinish: async ({ response, reasoning }) => {
if (session.user?.id) {
try {
const responseMessagesWithoutIncompleteToolCalls =
sanitizeResponseMessages(response.messages);
const sanitizedResponseMessages = sanitizeResponseMessages({
messages: response.messages,
reasoning,
});

await saveMessages({
messages: responseMessagesWithoutIncompleteToolCalls.map(
(message) => {
return {
id: message.id,
chatId: id,
role: message.role,
content: message.content,
createdAt: new Date(),
};
},
),
messages: sanitizedResponseMessages.map((message) => {
return {
id: message.id,
chatId: id,
role: message.role,
content: message.content,
createdAt: new Date(),
};
}),
});
} catch (error) {
console.error('Failed to save chat');
Expand Down
2 changes: 1 addition & 1 deletion components/message-reasoning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function MessageReasoning({
animate="expanded"
exit="collapsed"
variants={variants}
transition={{ duration: 0.3, ease: 'easeInOut' }}
transition={{ duration: 0.2, ease: 'easeInOut' }}
style={{ overflow: 'hidden' }}
className="pl-4 text-zinc-600 dark:text-zinc-400 border-l flex flex-col gap-4"
>
Expand Down
19 changes: 16 additions & 3 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export function convertToUIMessages(
}

let textContent = '';
let reasoning: string | undefined = undefined;
const toolInvocations: Array<ToolInvocation> = [];

if (typeof message.content === 'string') {
Expand All @@ -112,6 +113,8 @@ export function convertToUIMessages(
toolName: content.toolName,
args: content.args,
});
} else if (content.type === 'reasoning') {
reasoning = content.reasoning;
}
}
}
Expand All @@ -120,6 +123,7 @@ export function convertToUIMessages(
id: message.id,
role: message.role as Message['role'],
content: textContent,
reasoning,
toolInvocations,
});

Expand All @@ -130,9 +134,13 @@ export function convertToUIMessages(
type ResponseMessageWithoutId = CoreToolMessage | CoreAssistantMessage;
type ResponseMessage = ResponseMessageWithoutId & { id: string };

export function sanitizeResponseMessages(
messages: Array<ResponseMessage>,
): Array<ResponseMessage> {
export function sanitizeResponseMessages({
messages,
reasoning,
}: {
messages: Array<ResponseMessage>;
reasoning: string | undefined;
}) {
const toolResultIds: Array<string> = [];

for (const message of messages) {
Expand All @@ -158,6 +166,11 @@ export function sanitizeResponseMessages(
: true,
);

if (reasoning) {
// @ts-expect-error: reasoning message parts in sdk is wip
sanitizedContent.push({ type: 'reasoning', reasoning });
}

return {
...message,
content: sanitizedContent,
Expand Down

0 comments on commit 0f1b6a7

Please sign in to comment.