initial commit
This commit is contained in:
68
services/gemini.ts
Normal file
68
services/gemini.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { GoogleGenAI, Type } from "@google/genai";
|
||||
import { FileItem, FileType } from "../types";
|
||||
|
||||
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY || '' });
|
||||
|
||||
export const generateRemoteFileList = async (host: string, path: string): Promise<FileItem[]> => {
|
||||
try {
|
||||
const model = 'gemini-2.5-flash-latest';
|
||||
const prompt = `
|
||||
You are simulating an FTP server file listing for the host: "${host}" at path: "${path}".
|
||||
Generate a realistic list of 8-15 files and folders that might exist on this specific type of server.
|
||||
If the host sounds corporate, use corporate files. If it sounds like a game server, use game files.
|
||||
If it sounds like NASA, use space data files.
|
||||
|
||||
Return a JSON array of objects.
|
||||
`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model: model,
|
||||
contents: prompt,
|
||||
config: {
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
name: { type: Type.STRING },
|
||||
type: { type: Type.STRING, enum: ["FILE", "FOLDER"] },
|
||||
size: { type: Type.INTEGER, description: "Size in bytes. Folders should be 0 or 4096." },
|
||||
},
|
||||
required: ["name", "type", "size"]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const data = JSON.parse(response.text || '[]');
|
||||
|
||||
return data.map((item: any, index: number) => ({
|
||||
id: `gen-${Date.now()}-${index}`,
|
||||
name: item.name,
|
||||
type: item.type === 'FOLDER' ? FileType.FOLDER : FileType.FILE,
|
||||
size: item.size,
|
||||
date: new Date(Date.now() - Math.floor(Math.random() * 10000000000)).toISOString(),
|
||||
permissions: item.type === 'FOLDER' ? 'drwxr-xr-x' : '-rw-r--r--'
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("Gemini generation failed", error);
|
||||
// Fallback if API fails
|
||||
return [
|
||||
{ id: 'err1', name: 'connection_retry.log', type: FileType.FILE, size: 1024, date: new Date().toISOString(), permissions: '-rw-r--r--' },
|
||||
{ id: 'err2', name: 'backup', type: FileType.FOLDER, size: 0, date: new Date().toISOString(), permissions: 'drwxr-xr-x' }
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
export const generateServerMessage = async (host: string): Promise<string> => {
|
||||
try {
|
||||
const response = await ai.models.generateContent({
|
||||
model: 'gemini-3-flash-preview',
|
||||
contents: `Write a short, single-line FTP welcome message (220 Service ready) for a server named "${host}". Make it sound authentic to the domain info.`,
|
||||
});
|
||||
return response.text?.trim() || `220 ${host} FTP Server ready.`;
|
||||
} catch (e) {
|
||||
return `220 ${host} FTP Server ready.`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user