ContractWise is a modern, full-stack web application built with Next.js, Genkit, and Google's Gemini AI. It provides an intelligent solution for analyzing legal contracts, identifying potential risks, and suggesting optimized terms for all parties involved.
- AI-Powered Risk Assessment: Leverages Google's Gemini model via Genkit to perform deep analysis of contract text, identifying risks across financial, legal, technical, and risk management domains.
- Dual-Party Reporting: Generates distinct, comprehensive reports tailored for both parties (甲方 and 乙方), fostering fairer negotiations.
- Interactive Term Optimization: Provides AI-generated suggestions for improving clauses, complete with explanations. Users can edit these suggestions directly in the UI.
- Smart Document Handling: Supports
.pdf,.docx,.txt, and.mdfile uploads. Includes a pre-check to ensure the uploaded document is a valid contract before performing a full analysis. - Multi-Language Support: Fully localized for both English and Chinese (中文), including UI and AI-generated report content.
- Professional PDF Downloads: Users can download the detailed analysis reports as professionally formatted PDF files, ready for printing or sharing.
- Framework: Next.js (with App Router)
- Language: TypeScript
- AI Integration: Genkit (Google AI Toolkit) with the Gemini 2.0 Flash model.
- UI: React, Tailwind CSS, and ShadCN/UI for components.
- File Parsing:
pdfjs-distfor PDFs,mammothfor DOCX. - PDF Generation:
jspdfandhtml2canvasfor client-side PDF creation. - State Management: React Context API for global state (e.g., language).
- Deployment: Ready for deployment on Firebase App Hosting.
- Node.js (v18 or later recommended)
- npm, yarn, or pnpm
-
Clone the repository:
git clone <repository-url> cd <repository-name>
-
Install dependencies:
npm install # or # yarn install # or # pnpm install
-
Set up environment variables: Create a
.envfile in the root of the project and add your Google Gemini API key:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"You can obtain a key from Google AI Studio.
The application requires two separate processes to run concurrently: the Next.js frontend and the Genkit AI flows.
-
Run the Next.js app: Open a terminal and run:
npm run dev
This will start the frontend on http://localhost:9002.
-
Run the Genkit flows: Open a second terminal and run:
npm run genkit:watch
This starts the Genkit development server, which watches for changes in your AI flows. The Next.js app will communicate with this server for all AI-related tasks.
Now, you can open http://localhost:9002 in your browser to see the application.
.
├── src
│ ├── ai # Genkit AI core
│ │ ├── flows # All Genkit flow definitions
│ │ ├── genkit.ts # Genkit initialization and configuration
│ │ └── dev.ts # Entry point for Genkit dev server
│ ├── app # Next.js App Router
│ │ ├── (pages) # Route groups for pages
│ │ ├── actions.ts # Server Actions for backend logic
│ │ ├── globals.css # Global styles and Tailwind directives
│ │ └── layout.tsx # Root layout
│ ├── components # Reusable React components
│ │ ├── ui # ShadCN/UI components
│ │ └── ...
│ ├── context # React Context providers
│ ├── hooks # Custom React hooks
│ ├── lib # Utility functions and libraries
│ └── locales # Translation files (en.json, zh.json)
├── public # Static assets
└── ... # Config files (next.config.ts, tailwind.config.ts, etc.)
- File Upload: The user uploads a contract document on the homepage (
src/app/page.tsx). - Pre-check: A Server Action (
checkIfContractinsrc/app/actions.ts) sends the first ~2000 characters of the document to theisContractDocumentGenkit flow. This is a quick, cheap way to validate the file. - Full Analysis: If the file is a valid contract, the user can click "Analyze." This triggers the
analyzeContractFromFileServer Action, which extracts the full text and runs three Genkit flows in parallel:analyzeContractRisks: Finds risks for both parties.suggestBetterContractTerms(for 甲方): Suggests improvements for Party A.suggestBetterContractTerms(for 乙方): Suggests improvements for Party B.
- Display Results: The analysis results are stored in
sessionStorageand the user is redirected to the/reportpage. - Report Generation: On the report page, the user can click "Download Report." This calls the
generateHtmlReportServer Action, which uses thegenerateReportGenkit flow to create a professional HTML report. This HTML is then converted to a PDF on the client-side usingjspdfandhtml2canvas.