import { GoogleGenAI } from "@google/genai";
import fs from "fs";
import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
const FILE_NAME = "frontracer.md";
const MIME_TYPE = "text/markdown";
const CUSTOM_METADATA = [
{ key: "doc_type", stringValue: "portfolio_project" },
{ key: "project", stringValue: "FrontRacer" },
{
key: "tags",
stringListValue: {
values: [
"frontracer",
"social voting platform",
"next.js 15",
"typescript",
"payload cms",
"postgresql",
"cloudflare r2",
"pairing algorithm",
"vote session tracking",
"stats aggregation"
]
}
},
{ key: "author", stringValue: "Jakkrit Turner" },
];
const STORE_ID = process.env.FSS_ID;
const genAI = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
async function uploadDocumentToStore() {
try {
if (!STORE_ID) {
console.error("ERROR: FSS_ID environment variable is not set.");
console.error("Please set it in your .env.local file to the store's unique ID.");
return;
}
console.log("--- Uploading Document to File Search Store ---");
console.log(`Target Store ID: ${STORE_ID}`);
const filePath = `./ffs-files/${FILE_NAME}`;
if (fs.existsSync(filePath)) {
console.log(`Uploading file '${filePath}' and indexing it...`);
const res = await genAI.fileSearchStores.uploadToFileSearchStore({
fileSearchStoreName: STORE_ID,
file: filePath,
config: {
mimeType: MIME_TYPE,
displayName: FILE_NAME,
customMetadata: CUSTOM_METADATA,
}
});
console.log("File uploaded and indexed successfully.")
console.log(`Operation Name (to track indexing status): ${res.name}`);
console.log("Indexing is now running in the background. It may take a few minutes.");
} else {
console.error(`ERROR: File not found at path: ${filePath}`);
}
} catch (error) {
console.error("Setup failed. An API error occurred.", error);
}
}
uploadDocumentToStore();