File size: 4,165 Bytes
bb8c446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

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.");
    }
};