Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions llama_stack/ui/app/api/fetch-url/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
try {
const { url } = await request.json();

if (!url || typeof url !== "string") {
return NextResponse.json({ error: "URL is required" }, { status: 400 });
}

// Fetch the URL content
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const contentType = response.headers.get("content-type") || "";
let content: string;

if (contentType.includes("application/json")) {
const json = await response.json();
content = JSON.stringify(json, null, 2);
} else if (contentType.includes("text/html")) {
const html = await response.text();
// Basic HTML to text conversion - remove tags and decode entities
content = html
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "")
.replace(/<[^>]*>/g, "")
.replace(/&nbsp;/g, " ")
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/\s+/g, " ")
.trim();
} else {
content = await response.text();
}

return NextResponse.json({ content });
} catch (error) {
console.error("Error fetching URL:", error);
return NextResponse.json(
{ error: "Failed to fetch URL content" },
{ status: 500 }
);
}
}
51 changes: 51 additions & 0 deletions llama_stack/ui/app/api/upload-document/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
try {
const formData = await request.formData();
const file = formData.get("file") as File;
const vectorDbId = formData.get("vectorDbId") as string;

if (!file || !vectorDbId) {
return NextResponse.json(
{ error: "File and vectorDbId are required" },
{ status: 400 }
);
}

// Read file content based on type
let content: string;
const mimeType = file.type || "application/octet-stream";

if (mimeType === "text/plain" || mimeType === "text/markdown") {
content = await file.text();
} else if (mimeType === "application/pdf") {
// For PDFs, convert to base64 on the server side
const arrayBuffer = await file.arrayBuffer();
const bytes = new Uint8Array(arrayBuffer);
let binary = "";
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
content = `data:${mimeType};base64,${base64}`;
} else {
// Try to read as text
content = await file.text();
}

// Return the processed content for the client to send to RagTool
return NextResponse.json({
content,
mimeType,
fileName: file.name,
fileSize: file.size,
});
} catch (error) {
console.error("Error processing file upload:", error);
return NextResponse.json(
{ error: "Failed to process file upload" },
{ status: 500 }
);
}
}
Loading
Loading