chatbot-template/lib/utils.ts

166 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-11-15 12:18:17 -05:00
import type {
2024-10-30 16:01:24 +05:30
CoreAssistantMessage,
CoreToolMessage,
Message,
TextStreamPart,
ToolInvocation,
ToolSet,
UIMessage,
2024-10-30 16:01:24 +05:30
} from 'ai';
2024-11-15 12:18:17 -05:00
import { type ClassValue, clsx } from 'clsx';
2024-10-30 16:01:24 +05:30
import { twMerge } from 'tailwind-merge';
2023-05-19 12:33:56 -04:00
import type { DBMessage, Document } from '@/lib/db/schema';
2023-05-19 12:33:56 -04:00
export function cn(...inputs: ClassValue[]) {
2024-10-11 18:00:22 +05:30
return twMerge(clsx(inputs));
2023-05-19 12:33:56 -04:00
}
2024-10-11 18:00:22 +05:30
interface ApplicationError extends Error {
info: string;
status: number;
2023-05-19 12:33:56 -04:00
}
2023-06-16 15:20:42 +04:00
2024-10-11 18:00:22 +05:30
export const fetcher = async (url: string) => {
const res = await fetch(url);
2024-03-14 20:00:52 +03:00
2024-10-11 18:00:22 +05:30
if (!res.ok) {
const error = new Error(
2024-11-15 13:00:15 -05:00
'An error occurred while fetching the data.',
2024-10-11 18:00:22 +05:30
) as ApplicationError;
2024-03-19 04:37:30 +03:00
2024-10-11 18:00:22 +05:30
error.info = await res.json();
error.status = res.status;
2024-03-19 04:37:30 +03:00
2024-10-11 18:00:22 +05:30
throw error;
2024-03-19 04:37:30 +03:00
}
2024-08-24 23:23:37 -05:00
2024-10-11 18:00:22 +05:30
return res.json();
};
2024-08-24 23:23:37 -05:00
2024-10-11 18:00:22 +05:30
export function getLocalStorage(key: string) {
2024-10-30 16:01:24 +05:30
if (typeof window !== 'undefined') {
return JSON.parse(localStorage.getItem(key) || '[]');
2024-10-11 18:00:22 +05:30
}
return [];
2024-08-24 23:23:37 -05:00
}
2024-10-11 18:00:22 +05:30
export function generateUUID(): string {
2024-11-15 12:18:17 -05:00
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
2024-10-11 18:00:22 +05:30
const r = (Math.random() * 16) | 0;
2024-10-30 16:01:24 +05:30
const v = c === 'x' ? r : (r & 0x3) | 0x8;
2024-10-11 18:00:22 +05:30
return v.toString(16);
});
2024-08-24 23:23:37 -05:00
}
function addToolMessageToChat({
toolMessage,
messages,
}: {
toolMessage: CoreToolMessage;
messages: Array<Message>;
}): Array<Message> {
return messages.map((message) => {
if (message.toolInvocations) {
return {
...message,
toolInvocations: message.toolInvocations.map((toolInvocation) => {
const toolResult = toolMessage.content.find(
2024-11-15 13:00:15 -05:00
(tool) => tool.toolCallId === toolInvocation.toolCallId,
);
if (toolResult) {
return {
...toolInvocation,
2024-10-30 16:01:24 +05:30
state: 'result',
result: toolResult.result,
};
}
return toolInvocation;
}),
};
}
return message;
});
}
type ResponseMessageWithoutId = CoreToolMessage | CoreAssistantMessage;
type ResponseMessage = ResponseMessageWithoutId & { id: string };
export function sanitizeResponseMessages({
messages,
reasoning,
}: {
messages: Array<ResponseMessage>;
reasoning: string | undefined;
}) {
2024-11-15 12:18:17 -05:00
const toolResultIds: Array<string> = [];
2024-10-30 16:01:24 +05:30
for (const message of messages) {
if (message.role === 'tool') {
for (const content of message.content) {
if (content.type === 'tool-result') {
toolResultIds.push(content.toolCallId);
}
}
}
}
const messagesBySanitizedContent = messages.map((message) => {
if (message.role !== 'assistant') return message;
if (typeof message.content === 'string') return message;
const sanitizedContent = message.content.filter((content) =>
content.type === 'tool-call'
? toolResultIds.includes(content.toolCallId)
: content.type === 'text'
? content.text.length > 0
2024-11-15 13:00:15 -05:00
: true,
2024-10-30 16:01:24 +05:30
);
if (reasoning) {
// @ts-expect-error: reasoning message parts in sdk is wip
sanitizedContent.push({ type: 'reasoning', reasoning });
}
2024-10-30 16:01:24 +05:30
return {
...message,
content: sanitizedContent,
};
});
return messagesBySanitizedContent.filter(
2024-11-15 13:00:15 -05:00
(message) => message.content.length > 0,
2024-10-30 16:01:24 +05:30
);
}
export function getMostRecentUserMessage(messages: Array<UIMessage>) {
2024-11-05 17:15:51 +03:00
const userMessages = messages.filter((message) => message.role === 'user');
return userMessages.at(-1);
}
export function getDocumentTimestampByIndex(
documents: Array<Document>,
2024-11-15 13:00:15 -05:00
index: number,
) {
if (!documents) return new Date();
if (index > documents.length) return new Date();
return documents[index].createdAt;
}
export function getTrailingMessageId({
messages,
}: {
messages: Array<ResponseMessage>;
}): string | null {
const trailingMessage = messages.at(-1);
if (!trailingMessage) return null;
return trailingMessage.id;
}