111 lines
2.8 KiB
JavaScript
111 lines
2.8 KiB
JavaScript
import { cookies } from "next/headers";
|
|
import AgentChatClient from "../AgentChatClient";
|
|
|
|
/**
|
|
* Server component — fetches both agentDetails and initialHistory
|
|
* from the /api/agents proxy using the muapi_key cookie, then renders
|
|
* the client chat component with existing conversation messages pre-loaded.
|
|
*
|
|
* URL: /agents/[agent_id]/[conversation_id]
|
|
*/
|
|
export async function generateMetadata({ params }) {
|
|
return {
|
|
title: `Agent Chat — Open Generative AI`,
|
|
};
|
|
}
|
|
|
|
const BASE_URL = 'https://api.muapi.ai';
|
|
|
|
async function fetchAgentDetails(agentId, apiKey) {
|
|
if (!apiKey) return null;
|
|
try {
|
|
const res = await fetch(
|
|
`${BASE_URL}/agents/by-slug/${agentId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: { "x-api-key": apiKey },
|
|
}
|
|
);
|
|
if (res.ok) return await res.json();
|
|
|
|
if (agentId.length > 20) {
|
|
const resId = await fetch(
|
|
`${BASE_URL}/agents/${agentId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: { "x-api-key": apiKey },
|
|
}
|
|
);
|
|
if (resId.ok) return await resId.json();
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function fetchHistory(agentId, conversationId, apiKey) {
|
|
if (!apiKey) return null;
|
|
try {
|
|
// Try by slug first
|
|
const res = await fetch(
|
|
`${BASE_URL}/agents/by-slug/${agentId}/${conversationId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: { "x-api-key": apiKey },
|
|
}
|
|
);
|
|
if (res.ok) return await res.json();
|
|
|
|
// Fallback to direct agent ID if needed
|
|
if (agentId.length > 20) {
|
|
const resId = await fetch(
|
|
`${BASE_URL}/agents/${agentId}/${conversationId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: { "x-api-key": apiKey },
|
|
}
|
|
);
|
|
if (resId.ok) return await resId.json();
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function fetchUserData(apiKey) {
|
|
if (!apiKey) return null;
|
|
try {
|
|
const res = await fetch(`${BASE_URL}/api/v1/account/balance`, {
|
|
cache: "no-store",
|
|
headers: { "x-api-key": apiKey },
|
|
});
|
|
if (!res.ok) return null;
|
|
return await res.json();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default async function AgentConversationPage({ params }) {
|
|
const { agent_id, conversation_id } = await params;
|
|
const cookieStore = await cookies();
|
|
const apiKey = cookieStore.get("muapi_key")?.value;
|
|
|
|
console.log(`[ConvPage] Loading for agent: ${agent_id}, conv: ${conversation_id}, hasKey: ${!!apiKey}`);
|
|
|
|
const [agentDetails, initialHistory, userData] = await Promise.all([
|
|
fetchAgentDetails(agent_id, apiKey),
|
|
fetchHistory(agent_id, conversation_id, apiKey),
|
|
fetchUserData(apiKey)
|
|
]);
|
|
|
|
return (
|
|
<AgentChatClient
|
|
agentDetails={agentDetails}
|
|
initialHistory={initialHistory}
|
|
userData={userData}
|
|
/>
|
|
);
|
|
}
|