Merge remote-tracking branch 'upstream/dev' into feature/podcast-agent

This commit is contained in:
Anish Sarkar 2025-12-21 20:39:21 +05:30
commit 35463eeab4
11 changed files with 434 additions and 115 deletions

View file

@ -17,6 +17,35 @@ interface NewChatAdapterConfig {
chatId: number;
}
interface ChatMessageForBackend {
role: "user" | "assistant";
content: string;
}
/**
* Converts assistant-ui messages to a simple format for the backend
*/
function convertMessagesToBackendFormat(
messages: ChatModelRunOptions["messages"]
): ChatMessageForBackend[] {
return messages
.filter((m) => m.role === "user" || m.role === "assistant")
.map((m) => {
// Extract text content from the message parts
let content = "";
for (const part of m.content) {
if (part.type === "text") {
content += part.text;
}
}
return {
role: m.role as "user" | "assistant",
content: content.trim(),
};
})
.filter((m) => m.content.length > 0); // Filter out empty messages
}
/**
* Represents an in-progress or completed tool call
*/
@ -38,7 +67,7 @@ const TOOLS_WITH_UI = new Set(["generate_podcast"]);
*
* The backend expects:
* - POST /api/v1/new_chat
* - Body: { chat_id: number, user_query: string, search_space_id: number }
* - Body: { chat_id: number, user_query: string, search_space_id: number, messages: [...] }
* - Returns: SSE stream with Vercel AI SDK Data Stream Protocol
*/
export function createNewChatAdapter(config: NewChatAdapterConfig): ChatModelAdapter {
@ -53,7 +82,7 @@ export function createNewChatAdapter(config: NewChatAdapterConfig): ChatModelAda
throw new Error("No user message found");
}
// Extract text content from the message
// Extract text content from the last user message
let userQuery = "";
for (const part of lastUserMessage.content) {
if (part.type === "text") {
@ -85,6 +114,9 @@ export function createNewChatAdapter(config: NewChatAdapterConfig): ChatModelAda
throw new Error("Not authenticated. Please log in again.");
}
// Convert all messages to backend format for chat history
const messageHistory = convertMessagesToBackendFormat(messages);
const response = await fetch(`${backendUrl}/api/v1/new_chat`, {
method: "POST",
headers: {
@ -95,6 +127,7 @@ export function createNewChatAdapter(config: NewChatAdapterConfig): ChatModelAda
chat_id: config.chatId,
user_query: userQuery.trim(),
search_space_id: config.searchSpaceId,
messages: messageHistory,
}),
signal: abortSignal,
});
@ -309,3 +342,4 @@ export function createNewChatAdapter(config: NewChatAdapterConfig): ChatModelAda
},
};
}