feat: add reasoning model (#750)

Co-authored-by: Matt Apperson <me@mattapperson.com>
This commit is contained in:
Jeremy 2025-02-03 20:33:15 +05:30 committed by GitHub
parent 76804269c4
commit c61d4f91d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 342 additions and 209 deletions

View file

@ -1,9 +1,10 @@
import type {
CoreAssistantMessage,
CoreMessage,
CoreToolMessage,
Message,
TextStreamPart,
ToolInvocation,
ToolSet,
} from 'ai';
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
@ -96,6 +97,7 @@ export function convertToUIMessages(
}
let textContent = '';
let reasoning: string | undefined = undefined;
const toolInvocations: Array<ToolInvocation> = [];
if (typeof message.content === 'string') {
@ -111,6 +113,8 @@ export function convertToUIMessages(
toolName: content.toolName,
args: content.args,
});
} else if (content.type === 'reasoning') {
reasoning = content.reasoning;
}
}
}
@ -119,6 +123,7 @@ export function convertToUIMessages(
id: message.id,
role: message.role as Message['role'],
content: textContent,
reasoning,
toolInvocations,
});
@ -129,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) {
@ -157,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,