|
| 1 | +import { cookies } from "next/headers"; |
| 2 | +import AgentChatClient from "../AgentChatClient"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Server component — fetches both agentDetails and initialHistory |
| 6 | + * from the /api/agents proxy using the muapi_key cookie, then renders |
| 7 | + * the client chat component with existing conversation messages pre-loaded. |
| 8 | + * |
| 9 | + * URL: /agents/[agent_id]/[conversation_id] |
| 10 | + */ |
| 11 | +export async function generateMetadata({ params }) { |
| 12 | + return { |
| 13 | + title: `Agent Chat — Open Generative AI`, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +const BASE_URL = 'https://api.muapi.ai'; |
| 18 | + |
| 19 | +async function fetchAgentDetails(agentId, apiKey) { |
| 20 | + if (!apiKey) return null; |
| 21 | + try { |
| 22 | + const res = await fetch( |
| 23 | + `${BASE_URL}/agents/by-slug/${agentId}`, |
| 24 | + { |
| 25 | + cache: "no-store", |
| 26 | + headers: { "x-api-key": apiKey }, |
| 27 | + } |
| 28 | + ); |
| 29 | + if (res.ok) return await res.json(); |
| 30 | + |
| 31 | + if (agentId.length > 20) { |
| 32 | + const resId = await fetch( |
| 33 | + `${BASE_URL}/agents/${agentId}`, |
| 34 | + { |
| 35 | + cache: "no-store", |
| 36 | + headers: { "x-api-key": apiKey }, |
| 37 | + } |
| 38 | + ); |
| 39 | + if (resId.ok) return await resId.json(); |
| 40 | + } |
| 41 | + return null; |
| 42 | + } catch { |
| 43 | + return null; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function fetchHistory(agentId, conversationId, apiKey) { |
| 48 | + if (!apiKey) return null; |
| 49 | + try { |
| 50 | + // Try by slug first |
| 51 | + const res = await fetch( |
| 52 | + `${BASE_URL}/agents/by-slug/${agentId}/${conversationId}`, |
| 53 | + { |
| 54 | + cache: "no-store", |
| 55 | + headers: { "x-api-key": apiKey }, |
| 56 | + } |
| 57 | + ); |
| 58 | + if (res.ok) return await res.json(); |
| 59 | + |
| 60 | + // Fallback to direct agent ID if needed |
| 61 | + if (agentId.length > 20) { |
| 62 | + const resId = await fetch( |
| 63 | + `${BASE_URL}/agents/${agentId}/${conversationId}`, |
| 64 | + { |
| 65 | + cache: "no-store", |
| 66 | + headers: { "x-api-key": apiKey }, |
| 67 | + } |
| 68 | + ); |
| 69 | + if (resId.ok) return await resId.json(); |
| 70 | + } |
| 71 | + return null; |
| 72 | + } catch { |
| 73 | + return null; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +async function fetchUserData(apiKey) { |
| 78 | + if (!apiKey) return null; |
| 79 | + try { |
| 80 | + const res = await fetch(`${BASE_URL}/api/v1/account/balance`, { |
| 81 | + cache: "no-store", |
| 82 | + headers: { "x-api-key": apiKey }, |
| 83 | + }); |
| 84 | + if (!res.ok) return null; |
| 85 | + return await res.json(); |
| 86 | + } catch { |
| 87 | + return null; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export default async function AgentConversationPage({ params }) { |
| 92 | + const { agent_id, conversation_id } = await params; |
| 93 | + const cookieStore = await cookies(); |
| 94 | + const apiKey = cookieStore.get("muapi_key")?.value; |
| 95 | + |
| 96 | + console.log(`[ConvPage] Loading for agent: ${agent_id}, conv: ${conversation_id}, hasKey: ${!!apiKey}`); |
| 97 | + |
| 98 | + const [agentDetails, initialHistory, userData] = await Promise.all([ |
| 99 | + fetchAgentDetails(agent_id, apiKey), |
| 100 | + fetchHistory(agent_id, conversation_id, apiKey), |
| 101 | + fetchUserData(apiKey) |
| 102 | + ]); |
| 103 | + |
| 104 | + return ( |
| 105 | + <AgentChatClient |
| 106 | + agentDetails={agentDetails} |
| 107 | + initialHistory={initialHistory} |
| 108 | + userData={userData} |
| 109 | + /> |
| 110 | + ); |
| 111 | +} |
0 commit comments