Skip to content

Commit

Permalink
added-functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
ARYPROGRAMMER committed Oct 25, 2024
1 parent c32ce13 commit 61f40c4
Show file tree
Hide file tree
Showing 14 changed files with 1,165 additions and 156 deletions.
85 changes: 80 additions & 5 deletions app/api/analyzer/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,96 @@
// import { model } from "@/lib/groq";
// import { DocAnalyzerSchema } from "@/lib/doc-analyzer.types";
// import { generateObject } from "ai";
// import { NextRequest, NextResponse } from "next/server";

// export async function POST(req: NextRequest) {
// try {
// const body = await req.json();
// const prompt = `You are an Legal Consulting Advisor. Analyze the document given and help users understand and improve their legal documents. The Document is title is ${body.title} and other requirements are as follows:
// - The Severities, Risks and the Description of the Document.
// - A complete breakdown of all changes in the document to make it more legally sound in markdown format.
// - Tips(atmost 5) to add in the document to make it more legally sound`;

// const result = await generateObject({
// model,
// schema: DocAnalyzerSchema,
// prompt,
// });

// return NextResponse.json(result.object);
// } catch (error) {
// console.error("Error analyzing document:", error);
// return NextResponse.json({ error: "Failed to analyze document." });
// }
// }

import { model } from "@/lib/groq";
import { DocAnalyzerSchema } from "@/lib/doc-analyzer.types";
import { DocAnalyzerSchema, DocPlan } from "@/lib/doc-analyzer.types";
import { generateObject } from "ai";
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
try {
const body = await req.json();
const prompt = `You are an Legal Consulting Advisor. Analyze the document given and help users understand and improve their legal documents. The Document is ${body.description}.`;

if (!body.title) {
return NextResponse.json(
{ error: "Title is required" },
{ status: 400 }
);
}

const prompt = `As a Legal Consulting Advisor, analyze the following document titled "${body.title}". Provide:
1. A comprehensive description that outlines:
- Key legal risks and their severity levels
- Potential compliance issues
- Areas requiring immediate attention
2. A detailed markdown-formatted breakdown of required changes, including:
- Section-by-section analysis
- Specific recommendations for improvement
- Legal implications of each change
3. A markdown-formatted list of 5 essential tips to enhance the document's legal soundness, focusing on:
- Risk mitigation
- Compliance improvement
- Legal protection enhancement
Format the response maintaining clear structure and using markdown for better readability.`;

// Generate the analysis
const result = await generateObject({
model,
schema: DocAnalyzerSchema,
prompt,
});

return NextResponse.json(result.object);
// Create a complete DocPlan object
const docPlan: DocPlan = {
id: Date.now(), // Generate a unique ID
title: body.title,
description: result.object.description,
studyPlan: result.object.studyPlan,
tips: result.object.tips,
starred: false
};

return NextResponse.json(docPlan);
} catch (error) {
console.error("Error analyzing document:", error);
return NextResponse.json({ error: "Failed to analyze document." });

// More detailed error handling
if (error instanceof Error) {
return NextResponse.json(
{ error: error.message },
{ status: 500 }
);
}

return NextResponse.json(
{ error: "Failed to analyze document" },
{ status: 500 }
);
}
}
}
155 changes: 106 additions & 49 deletions components/add-doc-analyze.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useState } from "react";

import { Plus } from "lucide-react";

import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import {
Dialog,
Expand All @@ -11,10 +9,17 @@ import {
DialogHeader,
DialogTitle,
DialogTrigger,
DialogFooter,
} from "@/components/ui/dialog";

import { Button } from "./ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
import { Button } from "@/components/ui/button";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
TooltipProvider,
} from "@/components/ui/tooltip";
import { Card, CardContent } from "@/components/ui/card";
import { Alert, AlertDescription } from "@/components/ui/alert";

export default function AddStudyPlan({
onAdd,
Expand All @@ -23,55 +28,107 @@ export default function AddStudyPlan({
}) {
const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false);
const [newPlanTitle, setNewPlanTitle] = useState<string>("");
const [isError, setIsError] = useState<boolean>(false);

const generateNewPlan = () => {
if (newPlanTitle.trim().length < 10) {
setIsError(true);
return;
}
setIsError(false);
onAdd(newPlanTitle);
setNewPlanTitle("");
setIsDialogOpen(false);
};

const handleClose = () => {
setNewPlanTitle("");
setIsError(false);
setIsDialogOpen(false);
};

return (
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<Tooltip>
<TooltipTrigger asChild>
<DialogTrigger asChild>
<Button
className="fixed bottom-20 right-6 rounded-full"
size="icon"
>
<Plus className="h-4 w-4" />
</Button>
</DialogTrigger>
</TooltipTrigger>
<TooltipContent side="left">
<p>Add a Document</p>
</TooltipContent>
</Tooltip>
<DialogContent
className="sm:max-w-xl w-full h-[70vh] max-h-screen overflow-y-auto p-6"
>
<DialogHeader>
<DialogTitle>Analyze Document</DialogTitle>
<DialogDescription>
Enter The Legal Document Content
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="title" className="text-right">
Text Input:
</Label>
<Input
id="title"
value={newPlanTitle}
onChange={(e) => setNewPlanTitle(e.target.value)}
className="col-span-3 w-full h-24 min-h-[6rem] max-h-[50vh] resize-y border border-gray-300 p-2 rounded-md"
/>
</div>
<TooltipProvider>
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<div className="fixed bottom-20 right-6">
<Tooltip>
<TooltipTrigger asChild>
<Button
onClick={() => setIsDialogOpen(true)}
className="rounded-full shadow-lg hover:shadow-xl transition-all"
size="icon"
>
<Plus className="h-5 w-5" />
</Button>
</TooltipTrigger>
<TooltipContent side="left">
<p>Add a Document</p>
</TooltipContent>
</Tooltip>
</div>
<div className="flex justify-end">
<Button onClick={generateNewPlan}>Start Review</Button>
</div>
</DialogContent>
</Dialog>

<DialogContent className="sm:max-w-2xl w-full">
<DialogHeader>
<DialogTitle className="text-xl font-semibold">
Analyze Document
</DialogTitle>
<DialogDescription className="text-sm text-muted-foreground">
Enter the legal document content below for analysis
</DialogDescription>
</DialogHeader>

<Card className="mt-4">
<CardContent className="p-4">
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="documentContent" className="text-sm font-medium">
Document Content
</Label>
<Textarea
id="documentContent"
placeholder="Paste or type your legal document content here..."
value={newPlanTitle}
onChange={(e) => {
setNewPlanTitle(e.target.value);
setIsError(false);
}}
className="min-h-[300px] resize-y leading-relaxed"
/>
</div>

{isError && (
<Alert variant="destructive">
<AlertDescription>
Please enter at least 10 characters of document content
</AlertDescription>
</Alert>
)}

<div className="text-xs text-muted-foreground">
Tip: For best results, ensure the document is properly formatted
and contains clear legal language
</div>
</div>
</CardContent>
</Card>

<DialogFooter className="flex justify-between items-center mt-6 sm:justify-between">
<Button variant="outline" onClick={handleClose}>
Cancel
</Button>
<div className="flex items-center gap-2">
<Button
variant="default"
onClick={generateNewPlan}
disabled={newPlanTitle.trim().length === 0}
className="px-6"
>
Start Review
</Button>
</div>
</DialogFooter>
</DialogContent>
</Dialog>
</TooltipProvider>
);
}
}
Loading

0 comments on commit 61f40c4

Please sign in to comment.