diff --git a/src/app/api/proxy/route.ts b/src/app/api/proxy/route.ts index ba3bac6..08deb32 100644 --- a/src/app/api/proxy/route.ts +++ b/src/app/api/proxy/route.ts @@ -39,32 +39,55 @@ interface PersonaMatch { } function findTagsBetween(content: string, startMarker: string, endMarker: string): PersonaMatch[] { + console.log("Starting findTagsBetween with markers:", startMarker, endMarker); + console.log("Content length:", content.length); + console.log("First 100 chars of content:", content.substring(0, 100)); + const startMarkerTag = `<${startMarker}>`; const endMarkerTag = `<${endMarker}>`; const startIdx = content.indexOf(startMarkerTag); - if (startIdx === -1) return []; + console.log("Start marker index:", startIdx); + if (startIdx === -1) { + console.log("Start marker not found"); + return []; + } const endIdx = content.indexOf(endMarkerTag); - if (endIdx === -1) return []; + console.log("End marker index:", endIdx); + if (endIdx === -1) { + console.log("End marker not found"); + return []; + } const section = content.slice(startIdx, endIdx); + console.log("Section length:", section.length); console.log("Section found:", section); const matches: PersonaMatch[] = []; const tagPattern = /<([^/>\s][^>]*)>([\s\S]*?)<\/\1>/g; + console.log("Using pattern:", tagPattern); let match; - while ((match = tagPattern.exec(section)) !== null) { - console.log("Found match:", match[1], match[2].substring(0, 50) + "..."); - matches.push({ - tag: match[1].trim(), - content: match[2].trim() - }); + try { + while ((match = tagPattern.exec(section)) !== null) { + console.log("Found match:", { + fullMatch: match[0], + tag: match[1], + contentPreview: match[2].substring(0, 50) + "..." + }); + matches.push({ + tag: match[1].trim(), + content: match[2].trim() + }); + } + } catch (error) { + console.error("Error during regex execution:", error); } console.log("Total matches found:", matches.length); + console.log("Matches:", matches); return matches; } @@ -85,13 +108,24 @@ function safeReplace(text: string, old: string, newStr: string): string { } function extractCardData(messages: Message[]): CardData { + console.log("Starting extractCardData"); + console.log("Messages length:", messages.length); + const content0 = messages[0].content; const content1 = messages[2].content; + + console.log("Content0 length:", content0.length); + console.log("Content1 length:", content1.length); // Find all persona tags between system and scenario, take the last one as character + console.log("Finding personas between system and scenario"); const personas = findTagsBetween(content0, "system", "scenario"); + console.log("Found personas:", personas); + const charPersona = personas[personas.length - 1]; + console.log("Selected char persona:", charPersona); const charName = charPersona?.tag || ""; + console.log("Char name:", charName); // Initialize card data with the character name let cardData: CardData = { @@ -103,6 +137,8 @@ function extractCardData(messages: Message[]): CardData { first_mes: content1, }; + console.log("Initial card data:", cardData); + // Replace character name with placeholder in all fields for (const field in cardData) { if (field !== "name") { @@ -113,6 +149,7 @@ function extractCardData(messages: Message[]): CardData { } } + console.log("Final card data:", cardData); return cardData; } @@ -129,9 +166,12 @@ export async function POST(request: NextRequest) { } try { + console.log("Received POST request"); const body = await request.json(); + console.log("Request body received"); if (!body.messages || body.messages.length < 2) { + console.log("Invalid request - missing messages or insufficient count"); return NextResponse.json( { error: "Missing messages or insufficient message count" }, { @@ -143,7 +183,10 @@ export async function POST(request: NextRequest) { ); } + console.log("Processing card data"); const cardData = extractCardData(body.messages); + console.log("Card data processed"); + extractedCards.push({ ...cardData, timestamp: Date.now(), @@ -162,6 +205,7 @@ export async function POST(request: NextRequest) { ); } catch (error) { console.error("Error processing request:", error); + console.error("Error stack:", error instanceof Error ? error.stack : "No stack trace"); return NextResponse.json( { error: "Internal server error" }, {