interactive / services /geminiService.ts
caustino's picture
Upload 20 files
bb8c446 verified
Raw
History Blame Contribute Delete
4.17 kB
import { GoogleGenAI, Type } from "@google/genai";
import type { ActionPlan, CaseData } from '../types';
if (!process.env.API_KEY) {
throw new Error("API_KEY environment variable is not set");
}
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const responseSchema = {
type: Type.OBJECT,
properties: {
title: { type: Type.STRING, description: "A concise title for the action plan." },
summary: { type: Type.STRING, description: "A brief summary of the action plan and its goals." },
categories: {
type: Type.ARRAY,
description: "A list of categories for action items, e.g., 'Medical Documentation', 'Work History Evidence'.",
items: {
type: Type.OBJECT,
properties: {
category: { type: Type.STRING, description: "The name of the category." },
description: { type: Type.STRING, description: "A short description of this category's purpose." },
items: {
type: Type.ARRAY,
description: "A list of specific action items within this category.",
items: {
type: Type.OBJECT,
properties: {
item: { type: Type.STRING, description: "The specific action item or task to complete." },
description: { type: Type.STRING, description: "A detailed explanation of why this item is important and how to complete it." },
status: { type: Type.STRING, description: "The initial status, which should always be 'Not Started'." }
},
required: ["item", "description", "status"]
}
}
},
required: ["category", "description", "items"]
}
}
},
required: ["title", "summary", "categories"]
};
const createPrompt = (data: CaseData): string => {
return `
Analyze the following Social Security Disability Insurance (SSDI) case details and generate a comprehensive, step-by-step action plan for the applicant.
**Applicant's Case Details:**
- **Age:** ${data.age}
- **Diagnosed Medical Conditions:** ${data.medicalConditions}
- **Summary of Work History (last 15 years):** ${data.workHistory}
- **Daily Limitations and Difficulties:** ${data.limitations}
**Your Task:**
Create a detailed action plan in JSON format. The plan should be structured to guide the applicant in gathering the necessary evidence to build a strong SSDI claim.
**Key areas to cover in the plan:**
1. **Medical Documentation:** Gathering records, doctor's statements, test results, etc.
2. **Work History Evidence:** Documenting past jobs, job duties, and why they can no longer be performed.
3. **Activities of Daily Living (ADL):** Creating logs or journals that detail daily struggles.
4. **Third-Party Evidence:** Obtaining statements from family, friends, or former colleagues.
For each action item, provide a clear task ("item") and a detailed "description" explaining its importance and providing guidance. Every item's initial "status" must be "Not Started".
Generate the output strictly following the provided JSON schema.
`;
};
export const generateActionPlan = async (data: CaseData): Promise<ActionPlan> => {
const prompt = createPrompt(data);
try {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseSchema: responseSchema,
temperature: 0.5,
},
});
const jsonString = response.text.trim();
const plan: ActionPlan = JSON.parse(jsonString);
return plan;
} catch (error) {
console.error("Error calling Gemini API:", error);
throw new Error("Failed to generate action plan from Gemini API.");
}
};