From f3d87193a41668e9045c7b48321c4721076374a6 Mon Sep 17 00:00:00 2001 From: RutvikGhaskataEalf Date: Thu, 20 Feb 2025 09:56:25 +0530 Subject: [PATCH 1/3] feat:introduce AI agent and frontend code setup for aelf-code-generator --- .gitignore | 101 +++ agent/README.md | 21 + agent/aelf_code_generator/__init__.py | 9 + agent/aelf_code_generator/__main__.py | 146 ++++ agent/aelf_code_generator/agent.py | 747 +++++++++++++++++ agent/aelf_code_generator/demo.py | 59 ++ agent/aelf_code_generator/model.py | 39 + agent/aelf_code_generator/types.py | 51 ++ agent/langgraph.json | 17 + agent/pyproject.toml | 33 + ui/.env.local | 5 + ui/eslint.config.mjs | 16 + ui/next.config.ts | 18 + ui/package.json | 64 ++ ui/postcss.config.mjs | 8 + ui/public/file.svg | 1 + ui/public/globe.svg | 1 + ui/public/next.svg | 1 + ui/public/vercel.svg | 1 + ui/public/window.svg | 1 + ui/src/app/[workplaceId]/page.tsx | 20 + ui/src/app/api/build/route.ts | 116 +++ ui/src/app/api/copilotkit/route.ts | 33 + ui/src/app/contract-viewer/page.tsx | 35 + ui/src/app/contract-viewer/style.css | 17 + ui/src/app/deployments/page.tsx | 48 ++ ui/src/app/favicon.ico | Bin 0 -> 25931 bytes ui/src/app/globals.css | 40 + ui/src/app/layout.tsx | 32 + ui/src/app/page.tsx | 139 ++++ ui/src/app/projects/page.tsx | 34 + ui/src/app/wallet/page.tsx | 69 ++ ui/src/components/chat/ChatHeader.tsx | 14 + ui/src/components/chat/ChatInput.tsx | 146 ++++ ui/src/components/chat/ChatMessage.tsx | 40 + ui/src/components/chat/ChatSuggestions.tsx | 48 ++ ui/src/components/chat/index.tsx | 109 +++ ui/src/components/editor/CodeEditor.tsx | 84 ++ ui/src/components/editor/EditorHeader.tsx | 768 ++++++++++++++++++ ui/src/components/editor/EditorTabs.tsx | 56 ++ ui/src/components/editor/Progressbar.tsx | 49 ++ .../editor/editor-header/Progressbar.tsx | 49 ++ .../editor/editor-header/dailogs.tsx | 469 +++++++++++ .../editor/editor-header/header-buttons.tsx | 25 + .../components/editor/editor-header/index.tsx | 264 ++++++ .../components/editor/editor-header/style.css | 24 + .../components/file-explorer/context-menu.tsx | 87 ++ ui/src/components/file-explorer/delete.tsx | 78 ++ .../file-explorer/file-explorer.tsx | 156 ++++ .../file-explorer/new-file-form.tsx | 98 +++ ui/src/components/file-explorer/new-file.tsx | 35 + .../components/file-explorer/rename-form.tsx | 118 +++ ui/src/components/file-explorer/rename.tsx | 39 + ui/src/components/header/index.tsx | 58 ++ ui/src/components/layout/MainLayout.tsx | 13 + ui/src/components/ui/badge.tsx | 36 + ui/src/components/ui/button.tsx | 45 + ui/src/components/ui/context-menu.tsx | 200 +++++ ui/src/components/ui/deployment-card.tsx | 61 ++ ui/src/components/ui/dialog.tsx | 122 +++ ui/src/components/ui/dropdown-menu.tsx | 79 ++ ui/src/components/ui/form.tsx | 178 ++++ ui/src/components/ui/icons.tsx | 231 ++++++ ui/src/components/ui/input.tsx | 25 + ui/src/components/ui/label.tsx | 26 + ui/src/components/ui/project-card.tsx | 71 ++ ui/src/components/ui/table.tsx | 117 +++ ui/src/components/ui/tabs.tsx | 52 ++ ui/src/context/ContractContext.tsx | 100 +++ ui/src/context/EditorContext.tsx | 101 +++ ui/src/data/audit.ts | 128 +++ ui/src/data/client.ts | 139 ++++ ui/src/data/db.ts | 64 ++ ui/src/data/env.ts | 8 + ui/src/data/graphql.ts | 94 +++ ui/src/data/wallet.ts | 101 +++ ui/src/hooks/useChat.ts | 91 +++ ui/src/hooks/useCliActions.ts | 271 ++++++ ui/src/hooks/useFileSystem.ts | 67 ++ ui/src/hooks/useWorkspaces.ts | 10 + ui/src/lib/constants.ts | 87 ++ ui/src/lib/file-content-to-zip.ts | 13 + ui/src/lib/file.tsx | 60 ++ ui/src/lib/format-errors.tsx | 114 +++ ui/src/lib/model-selector-provider.tsx | 63 ++ ui/src/lib/process-test-output.ts | 45 + ui/src/lib/types.ts | 9 + ui/src/lib/utils.ts | 36 + ui/src/providers/apollo-wrapper.tsx | 34 + ui/src/providers/index.tsx | 13 + ui/src/types/index.ts | 29 + ui/tailwind.config.ts | 26 + ui/tsconfig.json | 27 + 93 files changed, 7722 insertions(+) create mode 100644 .gitignore create mode 100644 agent/README.md create mode 100644 agent/aelf_code_generator/__init__.py create mode 100644 agent/aelf_code_generator/__main__.py create mode 100644 agent/aelf_code_generator/agent.py create mode 100644 agent/aelf_code_generator/demo.py create mode 100644 agent/aelf_code_generator/model.py create mode 100644 agent/aelf_code_generator/types.py create mode 100644 agent/langgraph.json create mode 100644 agent/pyproject.toml create mode 100644 ui/.env.local create mode 100644 ui/eslint.config.mjs create mode 100644 ui/next.config.ts create mode 100644 ui/package.json create mode 100644 ui/postcss.config.mjs create mode 100644 ui/public/file.svg create mode 100644 ui/public/globe.svg create mode 100644 ui/public/next.svg create mode 100644 ui/public/vercel.svg create mode 100644 ui/public/window.svg create mode 100644 ui/src/app/[workplaceId]/page.tsx create mode 100644 ui/src/app/api/build/route.ts create mode 100644 ui/src/app/api/copilotkit/route.ts create mode 100644 ui/src/app/contract-viewer/page.tsx create mode 100644 ui/src/app/contract-viewer/style.css create mode 100644 ui/src/app/deployments/page.tsx create mode 100644 ui/src/app/favicon.ico create mode 100644 ui/src/app/globals.css create mode 100644 ui/src/app/layout.tsx create mode 100644 ui/src/app/page.tsx create mode 100644 ui/src/app/projects/page.tsx create mode 100644 ui/src/app/wallet/page.tsx create mode 100644 ui/src/components/chat/ChatHeader.tsx create mode 100644 ui/src/components/chat/ChatInput.tsx create mode 100644 ui/src/components/chat/ChatMessage.tsx create mode 100644 ui/src/components/chat/ChatSuggestions.tsx create mode 100644 ui/src/components/chat/index.tsx create mode 100644 ui/src/components/editor/CodeEditor.tsx create mode 100644 ui/src/components/editor/EditorHeader.tsx create mode 100644 ui/src/components/editor/EditorTabs.tsx create mode 100644 ui/src/components/editor/Progressbar.tsx create mode 100644 ui/src/components/editor/editor-header/Progressbar.tsx create mode 100644 ui/src/components/editor/editor-header/dailogs.tsx create mode 100644 ui/src/components/editor/editor-header/header-buttons.tsx create mode 100644 ui/src/components/editor/editor-header/index.tsx create mode 100644 ui/src/components/editor/editor-header/style.css create mode 100644 ui/src/components/file-explorer/context-menu.tsx create mode 100644 ui/src/components/file-explorer/delete.tsx create mode 100644 ui/src/components/file-explorer/file-explorer.tsx create mode 100644 ui/src/components/file-explorer/new-file-form.tsx create mode 100644 ui/src/components/file-explorer/new-file.tsx create mode 100644 ui/src/components/file-explorer/rename-form.tsx create mode 100644 ui/src/components/file-explorer/rename.tsx create mode 100644 ui/src/components/header/index.tsx create mode 100644 ui/src/components/layout/MainLayout.tsx create mode 100644 ui/src/components/ui/badge.tsx create mode 100644 ui/src/components/ui/button.tsx create mode 100644 ui/src/components/ui/context-menu.tsx create mode 100644 ui/src/components/ui/deployment-card.tsx create mode 100644 ui/src/components/ui/dialog.tsx create mode 100644 ui/src/components/ui/dropdown-menu.tsx create mode 100644 ui/src/components/ui/form.tsx create mode 100644 ui/src/components/ui/icons.tsx create mode 100644 ui/src/components/ui/input.tsx create mode 100644 ui/src/components/ui/label.tsx create mode 100644 ui/src/components/ui/project-card.tsx create mode 100644 ui/src/components/ui/table.tsx create mode 100644 ui/src/components/ui/tabs.tsx create mode 100644 ui/src/context/ContractContext.tsx create mode 100644 ui/src/context/EditorContext.tsx create mode 100644 ui/src/data/audit.ts create mode 100644 ui/src/data/client.ts create mode 100644 ui/src/data/db.ts create mode 100644 ui/src/data/env.ts create mode 100644 ui/src/data/graphql.ts create mode 100644 ui/src/data/wallet.ts create mode 100644 ui/src/hooks/useChat.ts create mode 100644 ui/src/hooks/useCliActions.ts create mode 100644 ui/src/hooks/useFileSystem.ts create mode 100644 ui/src/hooks/useWorkspaces.ts create mode 100644 ui/src/lib/constants.ts create mode 100644 ui/src/lib/file-content-to-zip.ts create mode 100644 ui/src/lib/file.tsx create mode 100644 ui/src/lib/format-errors.tsx create mode 100644 ui/src/lib/model-selector-provider.tsx create mode 100644 ui/src/lib/process-test-output.ts create mode 100644 ui/src/lib/types.ts create mode 100644 ui/src/lib/utils.ts create mode 100644 ui/src/providers/apollo-wrapper.tsx create mode 100644 ui/src/providers/index.tsx create mode 100644 ui/src/types/index.ts create mode 100644 ui/tailwind.config.ts create mode 100644 ui/tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f5809c --- /dev/null +++ b/.gitignore @@ -0,0 +1,101 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +**/node_modules/ +ui/node_modules/ +/.pnp +.pnp.* +.yarn/* +!.yarn/patches +!.yarn/plugins +!.yarn/releases +!.yarn/versions + +# Package lock files +package-lock.json +**/package-lock.json +ui/package-lock.json +yarn.lock +**/yarn.lock + +# testing +/coverage + +# next.js +/.next/ +**/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# env files (except .env.local) +.env +.env.development +.env.test +.env.production +**/.env +**/.env.development +**/.env.test +**/.env.production +!.env.local +!**/.env.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts + +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# Virtual Environment +venv/ +ENV/ +env/ +.env + +# Agent specific +agent/.env +agent/poetry.lock +agent/.pytest_cache/ +agent/.coverage +agent/htmlcov/ +agent/logs/ +agent/.vscode/ +agent/.idea/ +agent/*.log +agent/tmp/ +agent/temp/ +agent/cache/ +agent/.cache/ diff --git a/agent/README.md b/agent/README.md new file mode 100644 index 0000000..6db8521 --- /dev/null +++ b/agent/README.md @@ -0,0 +1,21 @@ +# AELF Code Generator Agent + +This agent generates AELF smart contract code based on natural language descriptions. + +## Installation + +```bash +# Create virtual environment +python3 -m venv venv +source venv/bin/activate + +# Install dependencies +pip install -e . +``` + +## Usage + +```bash +# Start the agent +python3 -m aelf_code_generator +``` \ No newline at end of file diff --git a/agent/aelf_code_generator/__init__.py b/agent/aelf_code_generator/__init__.py new file mode 100644 index 0000000..1f58259 --- /dev/null +++ b/agent/aelf_code_generator/__init__.py @@ -0,0 +1,9 @@ +""" +AELF Smart Contract Code Generator. +""" + +from aelf_code_generator.types import AgentState, get_default_state +from aelf_code_generator.agent import graph +from aelf_code_generator.demo import app + +__all__ = ["graph", "AgentState", "get_default_state", "app"] \ No newline at end of file diff --git a/agent/aelf_code_generator/__main__.py b/agent/aelf_code_generator/__main__.py new file mode 100644 index 0000000..05be0fa --- /dev/null +++ b/agent/aelf_code_generator/__main__.py @@ -0,0 +1,146 @@ +"""Main entry point for the AELF code generator agent.""" + +import os +import json +import uvicorn +import logging +from typing import Dict, Any, List +from contextlib import asynccontextmanager +from dotenv import load_dotenv +from fastapi import FastAPI, Request, HTTPException +from fastapi.responses import StreamingResponse, JSONResponse +from fastapi.middleware.cors import CORSMiddleware +from copilotkit import CopilotKitSDK, LangGraphAgent +from copilotkit.integrations.fastapi import add_fastapi_endpoint +from aelf_code_generator.agent import create_agent, graph + +# Configure logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +# Initialize FastAPI app with lifespan +@asynccontextmanager +async def lifespan(app: FastAPI): + """Lifespan events for the FastAPI application.""" + # Initialize on startup + logger.info("Starting up the application...") + yield + # Cleanup on shutdown + logger.info("Shutting down the application...") + +app = FastAPI(lifespan=lifespan) + +# Configure CORS +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], # In production, replace with your frontend URL + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# Initialize CopilotKit SDK with our agent +sdk = CopilotKitSDK( + agents=[ + LangGraphAgent( + name="aelf_code_generator", + description="Generates AELF smart contract code based on natural language descriptions.", + graph=graph, + ) + ], +) + +# Custom message handler +async def handle_messages(messages: List[Dict[str, str]]) -> JSONResponse: + """Handle incoming messages and return a JSON response.""" + try: + # Log incoming messages + logger.info(f"Received messages: {messages}") + + # Create initial state + state = { + "input": messages[-1]["content"] if messages else "", + "messages": messages + } + + # Accumulate all events + events = [] + try: + async for event in graph.astream(state): + logger.info(f"Generated event: {event}") + events.append(event) + except Exception as e: + logger.error(f"Error generating response: {str(e)}") + return JSONResponse( + status_code=500, + content={"error": str(e)} + ) + + # Return the last event as the final response + if events: + last_event = events[-1] + # Extract the internal output from the last event + if "_internal" in last_event.get("analyze", {}): + internal_output = last_event["analyze"]["_internal"]["output"] + # Format response for frontend + response = { + "analyze": { + "contract": internal_output.get("contract", {}).get("content", ""), + "proto": internal_output.get("proto", {}).get("content", ""), + "state": internal_output.get("state", {}).get("content", ""), + "project": internal_output.get("project", {}).get("content", ""), + "reference": internal_output.get("reference", {}).get("content", ""), + "analysis": internal_output.get("analysis", ""), + "metadata": internal_output.get("metadata", []) + } + } + return JSONResponse(content=response) + else: + # Fallback if output structure is different + return JSONResponse(content=last_event) + else: + return JSONResponse( + status_code=500, + content={"error": "No response generated"} + ) + except Exception as e: + logger.error(f"Error in handle_messages: {str(e)}") + raise HTTPException(status_code=500, detail=str(e)) + +# Add custom endpoint handlers +@app.post("/copilotkit") +@app.post("/copilotkit/generate") +async def copilotkit_endpoint(request: Request): + """Handle requests to the copilotkit endpoint.""" + try: + body = await request.json() + messages = body.get("messages", []) + return await handle_messages(messages) + except Exception as e: + logger.error(f"Error in copilotkit_endpoint: {str(e)}") + return JSONResponse( + status_code=500, + content={"error": f"Failed to process request: {str(e)}"} + ) + +# Health check endpoint +@app.get("/health") +def health(): + """Health check.""" + return {"status": "ok"} + +def main(): + """Start the FastAPI server.""" + load_dotenv() + + # Start the server + logger.info("Starting the FastAPI server...") + uvicorn.run( + app, + host="0.0.0.0", + port=3001, + log_level="info" + ) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/agent/aelf_code_generator/agent.py b/agent/aelf_code_generator/agent.py new file mode 100644 index 0000000..93411f3 --- /dev/null +++ b/agent/aelf_code_generator/agent.py @@ -0,0 +1,747 @@ +""" +This module defines the main agent workflow for AELF smart contract code generation. +""" + +import os +from typing import Dict, List, Any, Annotated, Literal +from langchain_core.messages import HumanMessage, AIMessage, BaseMessage, SystemMessage +from langgraph.graph import StateGraph, END +from langgraph.prebuilt.tool_executor import ToolExecutor +from langgraph.graph.message import add_messages +from langgraph.types import Command +from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper +from aelf_code_generator.model import get_model +from aelf_code_generator.types import AgentState, ContractOutput, CodebaseInsight, get_default_state + +ANALYSIS_PROMPT = """You are an expert AELF smart contract developer. Your task is to analyze the dApp description and provide a detailed analysis. + +Analyze the requirements and identify: +- Contract type and purpose +- Core features and functionality +- Required methods and their specifications +- State variables and storage needs +- Events and their parameters +- Access control and security requirements + +Provide a structured analysis that will be used to generate the smart contract code in the next step. +Do not generate any code in this step, focus only on the analysis.""" + +CODEBASE_ANALYSIS_PROMPT = """You are an expert AELF smart contract developer. Based on the provided analysis and sample codebase insights, analyze and extract best practices and patterns. + +Focus on: +1. Project structure and organization +2. Common coding patterns in AELF smart contracts +3. Implementation guidelines specific to the requirements +4. Relevant sample contracts that can be used as reference + +Provide structured insights that will guide the code generation process.""" + +CODE_GENERATION_PROMPT = """You are an expert AELF smart contract developer. Based on the provided analysis and codebase insights, generate a complete smart contract implementation following AELF's standard project structure. + +Follow these implementation guidelines: +{implementation_guidelines} + +Common coding patterns to use: +{coding_patterns} + +Project structure to follow: +{project_structure} + +Relevant sample references: +{relevant_samples} + +Generate the following files with proper implementations: + +1. Main Contract File (src/ContractName.cs): +- Inherit from ContractNameContainer.ContractNameBase +- Implement all contract methods +- Use proper state management +- Include XML documentation +- Add proper access control +- Include input validation +- Emit events for state changes + +2. State Class File (src/ContractState.cs): +- Define all state variables using proper AELF state types +- Use MappedState for collections +- Use SingletonState for single values +- Include XML documentation + +3. Proto File (src/Protobuf/contract/contract_name.proto): +- Define all messages and services +- Use proper protobuf types +- Include method definitions +- Define events +- Add proper comments + +4. Reference Contract File (src/ContractReference.cs): +- Define contract reference state +- Include necessary contract references +- Add helper methods + +5. Project File (ContractName.csproj): +- Include necessary AELF package references +- Set proper SDK version +- Configure protobuf generation + +Format each file in a separate code block with proper file path comment: +```csharp +// src/ContractName.cs +... contract implementation ... +``` + +```csharp +// src/ContractState.cs +... state class implementation ... +``` + +```protobuf +// src/Protobuf/contract/contract_name.proto +... proto definitions ... +``` + +```csharp +// src/ContractReference.cs +... contract references ... +``` + +```xml +// ContractName.csproj +... project configuration ... +``` + +Ensure all files follow AELF conventions and best practices.""" + +async def analyze_requirements(state: AgentState) -> Command[Literal["analyze_codebase", "__end__"]]: + """Analyze the dApp description and provide detailed requirements analysis.""" + try: + # Initialize internal state if not present + if "_internal" not in state: + state["_internal"] = { + "analysis": "", + "codebase_insights": { + "project_structure": "", + "coding_patterns": "", + "relevant_samples": [], + "implementation_guidelines": "" + }, + "output": { + "contract": "", + "state": "", + "proto": "", + "analysis": "" + } + } + + # Get model with state + model = get_model(state) + + # Generate analysis + messages = [ + SystemMessage(content=ANALYSIS_PROMPT), + HumanMessage(content=state["input"]) + ] + + response = await model.ainvoke(messages) + analysis = response.content.strip() + + if not analysis: + raise ValueError("Analysis generation failed - empty response") + + # Return command to move to next state + return Command( + goto="analyze_codebase", + update={ + "_internal": { + "analysis": analysis, + "codebase_insights": state["_internal"]["codebase_insights"], + "output": { + "contract": "", + "state": "", + "proto": "", + "analysis": analysis + } + } + } + ) + + except Exception as e: + error_msg = f"Error analyzing requirements: {str(e)}" + return Command( + goto="__end__", + update={ + "_internal": { + "analysis": error_msg, + "codebase_insights": { + "project_structure": "", + "coding_patterns": "", + "relevant_samples": [], + "implementation_guidelines": "" + }, + "output": { + "contract": "", + "state": "", + "proto": "", + "analysis": error_msg + } + } + } + ) + +async def analyze_codebase(state: AgentState) -> Command[Literal["generate", "__end__"]]: + """Analyze AELF sample codebases to gather implementation insights.""" + try: + # First try to determine the type of contract from analysis + analysis = state["_internal"]["analysis"] + contract_type = "smart contract" # default + + # Identify relevant sample based on contract type + relevant_samples = [] + if "NFT" in analysis or "token" in analysis.lower(): + contract_type = "NFT contract" + relevant_samples = ["nft"] + elif "DAO" in analysis.lower(): + contract_type = "DAO contract" + relevant_samples = ["simple-dao"] + elif "game" in analysis.lower(): + contract_type = "game contract" + relevant_samples = ["lottery-game", "tic-tac-toe"] + elif "todo" in analysis.lower(): + contract_type = "todo contract" + relevant_samples = ["todo"] + elif "vote" in analysis.lower(): + contract_type = "voting contract" + relevant_samples = ["vote"] + elif "allowance" in analysis.lower() or "spending" in analysis.lower(): + contract_type = "allowance contract" + relevant_samples = ["allowance"] + elif "staking" in analysis.lower(): + contract_type = "staking contract" + relevant_samples = ["staking"] + elif "donation" in analysis.lower(): + contract_type = "donation contract" + relevant_samples = ["donation"] + elif "expense" in analysis.lower() or "tracking" in analysis.lower(): + contract_type = "expense tracking contract" + relevant_samples = ["expense-tracker"] + else: + # For basic contracts, look at hello-world + relevant_samples = ["hello-world"] + + # Get model to analyze requirements + model = get_model(state) + + # Generate codebase insights with improved prompt + messages = [ + SystemMessage(content="""You are an expert AELF smart contract developer. Based on the contract requirements and AELF sample contracts, provide implementation insights and patterns. +Focus on practical, concrete patterns that can be directly applied to smart contract development. +For each pattern you identify, include a brief explanation of why it's important and how it should be used. + +Your response should be structured in these sections: +1. Project Structure - How the contract files should be organized +2. Coding Patterns - Common patterns and practices to use +3. Implementation Guidelines - Specific guidance for this contract type +4. Relevant Samples - Which sample contracts to reference + +Be specific and detailed in your guidance."""), + HumanMessage(content=f""" +Based on the following contract requirements and type, provide implementation insights and patterns from AELF sample contracts. + +Contract Requirements: +{analysis} + +Contract Type: {contract_type} +Relevant Sample(s): {', '.join(relevant_samples)} + +Please provide structured insights focusing on: + +1. Project Structure and Organization + - Required contract files and their purpose + - State variables and their types + - Events and their parameters + - Contract references needed + +2. Smart Contract Patterns + - State management patterns for this type + - Access control patterns needed + - Event handling patterns + - Common utility functions + - Error handling strategies + +3. Implementation Guidelines + - Best practices for this contract type + - Security considerations + - Performance optimizations + - Testing approaches + +4. Code Examples + - Key methods to implement + - Common features needed + - Pitfalls to avoid + +Your insights will guide the code generation process.""") + ] + + try: + response = await model.ainvoke(messages, timeout=150) + insights = response.content.strip() + + if not insights: + raise ValueError("Codebase analysis failed - empty response") + + # Split insights into sections + sections = insights.split("\n\n") + + # Extract sections based on headers + project_structure = "" + coding_patterns = "" + implementation_guidelines = insights # Keep full response as guidelines + + for i, section in enumerate(sections): + section_lower = section.lower() + if any(header in section_lower for header in ["project structure", "file structure", "organization"]): + project_structure = section + # Look ahead for subsections + for next_section in sections[i+1:]: + if not any(header in next_section.lower() for header in ["pattern", "guideline", "implementation"]): + project_structure += "\n\n" + next_section + else: + break + elif any(header in section_lower for header in ["pattern", "practice", "common"]): + coding_patterns = section + # Look ahead for subsections + for next_section in sections[i+1:]: + if not any(header in next_section.lower() for header in ["guideline", "implementation", "structure"]): + coding_patterns += "\n\n" + next_section + else: + break + + # Ensure we have content for each section + if not project_structure: + project_structure = """Standard AELF project structure: +1. Main Contract Implementation (ContractName.cs) + - Inherits from ContractBase + - Contains contract logic + - Uses state management + - Includes documentation + +2. Contract State (ContractState.cs) + - Defines state variables + - Uses proper AELF types + - Includes documentation + +3. Protobuf Definitions (Protobuf/) + - contract/ - Interface + - message/ - Messages + - reference/ - References + +4. Contract References (ContractReferences.cs) + - Reference declarations + - Helper methods""" + + if not coding_patterns: + coding_patterns = """Common AELF patterns: +1. State Management + - MapState for collections + - SingletonState for values + - State initialization + - Access patterns + +2. Access Control + - Context.Sender checks + - Ownership patterns + - Authorization + - Least privilege + +3. Event Handling + - Event definitions + - State change events + - Event parameters + - Documentation + +4. Input Validation + - Parameter validation + - State validation + - Error messages + - Fail-fast approach + +5. Error Handling + - Exception types + - Error messages + - Edge cases + - AELF patterns""" + + # Create insights dictionary with extracted sections + insights_dict = { + "project_structure": project_structure, + "coding_patterns": coding_patterns, + "relevant_samples": relevant_samples, + "implementation_guidelines": implementation_guidelines + } + + except Exception as e: + print(f"Error analyzing requirements: {str(e)}") + raise + + # Return command to move to next state + return Command( + goto="generate", + update={ + "_internal": { + **state["_internal"], + "codebase_insights": insights_dict + } + } + ) + + except Exception as e: + error_msg = f"Error analyzing codebase: {str(e)}" + print(f"Codebase analysis error: {error_msg}") + return Command( + goto="generate", # Continue to generate even if codebase analysis fails + update={ + "_internal": { + **state["_internal"], + "codebase_insights": { + "project_structure": """Standard AELF project structure: +1. Contract class inheriting from AElfContract +2. State class for data storage +3. Proto files for interface definition +4. Project configuration in .csproj""", + "coding_patterns": """Common AELF patterns: +1. State management using MapState/SingletonState +2. Event emission for status changes +3. Authorization checks using Context.Sender +4. Input validation with proper error handling""", + "relevant_samples": ["hello-world"], + "implementation_guidelines": """Follow AELF best practices: +1. Use proper base classes and inheritance +2. Implement robust state management +3. Add proper access control checks +4. Include comprehensive input validation +5. Emit events for important state changes +6. Follow proper error handling patterns +7. Add XML documentation for all public members""" + } + } + } + ) + +async def generate_contract(state: AgentState) -> Command[Literal["__end__"]]: + """Generate smart contract code based on analysis and codebase insights.""" + try: + # Get analysis and insights + internal = state["_internal"] + analysis = internal["analysis"] + insights = internal["codebase_insights"] + + if not analysis or not insights["implementation_guidelines"]: + raise ValueError("Missing analysis or codebase insights") + + # Get model with state + model = get_model(state) + + # Generate code based on analysis and insights + messages = [ + SystemMessage(content=CODE_GENERATION_PROMPT.format( + implementation_guidelines=insights["implementation_guidelines"], + coding_patterns=insights["coding_patterns"], + project_structure=insights["project_structure"], + relevant_samples="\n".join(insights["relevant_samples"]) + )), + HumanMessage(content=f"Analysis:\n{analysis}\n\nPlease generate the complete smart contract implementation following AELF's project structure.") + ] + + try: + # Set a longer timeout for code generation + response = await model.ainvoke(messages, timeout=150) # 5 minutes timeout + content = response.content + + if not content: + raise ValueError("Code generation failed - empty response") + except TimeoutError: + print("Code generation timed out, using partial response if available") + content = getattr(response, 'content', '') or "" + if not content: + raise ValueError("Code generation timed out and no partial response available") + + # Initialize components with empty CodeFile structures + empty_code_file = {"content": "", "file_type": "", "path": ""} + components = { + "contract": dict(empty_code_file), + "state": dict(empty_code_file), + "proto": dict(empty_code_file), + "reference": dict(empty_code_file), + "project": dict(empty_code_file) + } + additional_files = [] # List to store additional files + + # Parse code blocks + current_component = None + current_content = [] + in_code_block = False + current_file_type = "" + + # Track if we're inside XML documentation + in_xml_doc = False + xml_doc_content = [] + + for line in content.split("\n"): + # Handle XML documentation + if "///" in line or "/<" in line: + in_xml_doc = True + xml_doc_content.append(line) + continue + elif in_xml_doc and (">" in line or line.strip().endswith("/")): + in_xml_doc = False + xml_doc_content.append(line) + if current_content: + current_content.extend(xml_doc_content) + xml_doc_content = [] + continue + elif in_xml_doc: + xml_doc_content.append(line) + continue + + # Handle code block markers + if "```" in line: + if not in_code_block: + # Start of code block - detect language + if "csharp" in line.lower(): + current_file_type = "csharp" + elif "protobuf" in line.lower() or "proto" in line.lower(): + current_file_type = "proto" + elif "xml" in line.lower(): + current_file_type = "xml" + else: + current_file_type = "text" + else: + # End of code block + if current_component and current_content: + code_content = "\n".join(current_content).strip() + if current_component in components: + components[current_component]["content"] = code_content + components[current_component]["file_type"] = current_file_type + else: + # Only store non-empty additional files + if code_content and current_file_type: + additional_files.append({ + "content": code_content, + "file_type": current_file_type, + "path": current_component if current_component != "additional" else "" + }) + current_content = [] + current_component = None + current_file_type = "" + in_code_block = not in_code_block + continue + + # Handle file path markers + if ("// src/" in line or "// " in line or "", "") + .strip() + ) + + # First check for project file since it has a different comment style + if ".csproj" in line: + current_component = "project" + components[current_component]["path"] = file_path + # Check for main contract file - should be the one with main contract logic + elif (file_path.endswith(".cs") and + not any(x in file_path.lower() for x in ["reference", "state", "test"]) and + "src" in file_path.lower()): + # Main contract file should be in src directory and not be a reference/state file + if not components["contract"]["content"]: # Only set if not already set + current_component = "contract" + components[current_component]["path"] = file_path + else: + # If we already have a main contract, this is an additional file + current_component = file_path + additional_files.append({ + "content": "", + "file_type": "csharp", + "path": file_path + }) + # Check for state file + elif "State.cs" in file_path or "ContractState.cs" in file_path: + current_component = "state" + components[current_component]["path"] = file_path + # Check for proto file in contract directory + elif ".proto" in file_path and "contract" in file_path.lower(): + current_component = "proto" + components[current_component]["path"] = file_path + # Check for reference file + elif ("Reference" in file_path and file_path.endswith(".cs")) or "ContractReference" in file_path: + current_component = "reference" + components[current_component]["path"] = file_path + # Handle any other files + else: + # Only create additional file entry if it's a real file path + if "." in file_path and "/" in file_path: + current_component = file_path + # Set file type based on extension + file_type = "" + if file_path.endswith(".cs"): + file_type = "csharp" + elif file_path.endswith(".proto"): + file_type = "proto" + elif file_path.endswith(".xml") or file_path.endswith(".csproj"): + file_type = "xml" + + additional_files.append({ + "content": "", # Will be filled when code block ends + "file_type": file_type, # Set based on extension + "path": file_path + }) + continue + + # Collect content if in a code block and have a current component + if in_code_block and current_component: + current_content.append(line) + + # Add last component if any + if current_component and current_content: + code_content = "\n".join(current_content).strip() + if current_component in components: + components[current_component]["content"] = code_content + components[current_component]["file_type"] = current_file_type + else: + # Only store non-empty additional files + if code_content and current_file_type: + additional_files.append({ + "content": code_content, + "file_type": current_file_type, + "path": current_component if current_component != "additional" else "" + }) + + # Filter out empty or invalid additional files + additional_files = [ + f for f in additional_files + if f["content"] and f["file_type"] and not any( + invalid in f["path"].lower() + for invalid in ["", "", "", "", ""] + ) + ] + + # Move main contract files from additional_files to components if needed + for file in additional_files[:]: # Use slice to allow modification during iteration + # Check for main contract file if not already set + if (not components["contract"]["content"] and + file["path"].endswith(".cs") and + "src" in file["path"].lower() and + not any(x in file["path"].lower() for x in ["reference", "state", "test"])): + components["contract"] = file + additional_files.remove(file) + # Check for state file if not already set + elif (not components["state"]["content"] and + ("State.cs" in file["path"] or "ContractState.cs" in file["path"])): + components["state"] = file + additional_files.remove(file) + # Check for reference file if not already set + elif (not components["reference"]["content"] and + (("Reference" in file["path"] and file["path"].endswith(".cs")) or + "ContractReference" in file["path"])): + components["reference"] = file + additional_files.remove(file) + # Check for proto file if not already set + elif (not components["proto"]["content"] and + ".proto" in file["path"] and + "contract" in file["path"].lower()): + components["proto"] = file + additional_files.remove(file) + + # Ensure we have the main contract file + if not components["contract"]["content"]: + # Try to find the most likely main contract file from additional_files + main_candidates = [ + f for f in additional_files + if f["file_type"] == "csharp" and + "src" in f["path"].lower() and + not any(x in f["path"].lower() for x in ["reference", "state", "test"]) + ] + if main_candidates: + # Use the first candidate as main contract + components["contract"] = main_candidates[0] + additional_files.remove(main_candidates[0]) + + # Ensure all components have content + if not components["contract"]["content"]: + raise ValueError("No main contract implementation found") + + # Create the output structure + output = { + "contract": components["contract"], + "state": components["state"], + "proto": components["proto"], + "reference": components["reference"], + "project": components["project"], + "metadata": additional_files, + "analysis": analysis + } + + # Return command with results + return Command( + goto="__end__", + update={ + "_internal": { + **state["_internal"], + "output": output + } + } + ) + + except Exception as e: + error_msg = f"Error generating contract: {str(e)}" + print(f"Generation error: {error_msg}") # Add logging + empty_code_file = {"content": "", "file_type": "", "path": ""} + return Command( + goto="__end__", + update={ + "_internal": { + **state["_internal"], + "output": { + "contract": empty_code_file, + "state": empty_code_file, + "proto": empty_code_file, + "reference": empty_code_file, + "project": empty_code_file, + "metadata": [], + "analysis": error_msg + } + } + } + ) + +def create_agent(): + """Create the agent workflow.""" + workflow = StateGraph(AgentState) + + # Add nodes for each step + workflow.add_node("analyze", analyze_requirements) + workflow.add_node("analyze_codebase", analyze_codebase) + workflow.add_node("generate", generate_contract) + + # Set entry point and connect nodes + workflow.set_entry_point("analyze") + workflow.add_edge("analyze", "analyze_codebase") + workflow.add_edge("analyze", END) # In case of analysis error + workflow.add_edge("analyze_codebase", "generate") + workflow.add_edge("analyze_codebase", END) # In case of codebase analysis error + workflow.add_edge("generate", END) + + # Compile workflow + return workflow.compile() + +# Create the graph +graph = create_agent() + +# Export +__all__ = ["graph", "get_default_state"] \ No newline at end of file diff --git a/agent/aelf_code_generator/demo.py b/agent/aelf_code_generator/demo.py new file mode 100644 index 0000000..839d4d7 --- /dev/null +++ b/agent/aelf_code_generator/demo.py @@ -0,0 +1,59 @@ +""" +This module provides a demo interface for the AELF smart contract code generator. +""" + +import os +from dotenv import load_dotenv +load_dotenv() # This loads the environment variables from .env + +from fastapi import FastAPI, Body, HTTPException +import uvicorn +from aelf_code_generator.agent import graph, get_default_state + +app = FastAPI() + +@app.post("/generate") +async def generate_contract(description: str = Body(..., description="Describe your smart contract requirements in plain text. For example:\n- I need a voting contract where users can create proposals and vote\n- Create an NFT marketplace with listing and bidding features\n- Token contract with mint, burn, and transfer functions\n- DAO governance contract with proposal voting and treasury management")): + """Generate smart contract from text description.""" + try: + # Create initial state with description + state = get_default_state() + state["input"] = description + + # Run the graph + result = await graph.ainvoke(state) + + # Check for errors + if not any(result["output"].values()): + raise HTTPException( + status_code=500, + detail="Failed to generate contract. Please try again with a more detailed description." + ) + + # Return the generated outputs + return result["output"] + + except Exception as e: + raise HTTPException( + status_code=500, + detail=str(e) + ) + +@app.get("/health") +def health(): + """Health check.""" + return {"status": "ok"} + +def main(): + """Run the uvicorn server.""" + port = int(os.getenv("PORT", "8000")) + uvicorn.run( + "aelf_code_generator.demo:app", + host="0.0.0.0", + port=port, + reload=True, + reload_dirs=["."] + ) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/agent/aelf_code_generator/model.py b/agent/aelf_code_generator/model.py new file mode 100644 index 0000000..ea4f294 --- /dev/null +++ b/agent/aelf_code_generator/model.py @@ -0,0 +1,39 @@ +""" +This module provides model configuration for the AELF smart contract code generator. +""" + +import os +from typing import cast, Any +from langchain_core.language_models.chat_models import BaseChatModel +from aelf_code_generator.types import AgentState + +def get_model(state: AgentState) -> BaseChatModel: + """ + Get a model based on the environment variable or state configuration. + """ + state_model = state.get("model") + model = os.getenv("MODEL", state_model) + + print(f"Using model: {model}") + + if model == "openai": + from langchain_openai import ChatOpenAI + return ChatOpenAI(temperature=0, model="gpt-4") + if model == "anthropic": + from langchain_anthropic import ChatAnthropic + return ChatAnthropic( + temperature=0, + model_name="claude-3-sonnet-20240229", + timeout=None, + stop=None + ) + if model == "google_genai": + from langchain_google_genai import ChatGoogleGenerativeAI + return ChatGoogleGenerativeAI( + temperature=0, + model="gemini-1.5-flash", + api_key=cast(Any, os.getenv("GOOGLE_API_KEY")) or None, + convert_system_message_to_human=True + ) + + raise ValueError("Invalid model specified") \ No newline at end of file diff --git a/agent/aelf_code_generator/types.py b/agent/aelf_code_generator/types.py new file mode 100644 index 0000000..4e92637 --- /dev/null +++ b/agent/aelf_code_generator/types.py @@ -0,0 +1,51 @@ +""" +This module defines the state types for the AELF code generator agent. +""" + +from typing import TypedDict, List, Optional, NotRequired + +class CodebaseInsight(TypedDict, total=False): + """ + Represents insights gathered from analyzing sample codebases + """ + project_structure: str + coding_patterns: str + relevant_samples: List[str] + implementation_guidelines: str + +class CodeFile(TypedDict): + """ + Represents a code file with its content and metadata + """ + content: str # The actual code content + file_type: str # File type (e.g., "csharp", "proto", "xml") + path: str # Path in project structure (e.g., "src/ContractName.cs") + +class ContractOutput(TypedDict, total=False): + """ + Represents the generated smart contract components + """ + contract: CodeFile # Main contract implementation + state: CodeFile # State class implementation + proto: CodeFile # Protobuf definitions + reference: CodeFile # Contract references + project: CodeFile # Project configuration + metadata: List[CodeFile] # Additional files generated by LLM + analysis: str # Requirements analysis + +class InternalState(TypedDict, total=False): + """Internal state for agent workflow.""" + analysis: str + codebase_insights: CodebaseInsight + output: ContractOutput + +class AgentState(TypedDict, total=False): + """State type for the agent workflow.""" + input: str # Original dApp description + _internal: NotRequired[InternalState] # Internal state management (not shown in UI) + +def get_default_state() -> AgentState: + """Initialize default state.""" + return { + "input": "" + } \ No newline at end of file diff --git a/agent/langgraph.json b/agent/langgraph.json new file mode 100644 index 0000000..499de33 --- /dev/null +++ b/agent/langgraph.json @@ -0,0 +1,17 @@ +{ + "python_version": "3.12", + "dockerfile_lines": [ + "RUN pip install aiohttp==3.11.11", + "RUN pip install --no-cache-dir poetry", + "WORKDIR /api", + "COPY . .", + "RUN poetry config virtualenvs.create false", + "RUN poetry install --no-interaction --no-ansi --no-root", + "RUN poetry install --no-interaction --no-ansi" + ], + "dependencies": ["."], + "graphs": { + "aelf_code_generator": "./aelf_code_generator/agent.py:graph" + }, + "env": ".env" +} diff --git a/agent/pyproject.toml b/agent/pyproject.toml new file mode 100644 index 0000000..778d171 --- /dev/null +++ b/agent/pyproject.toml @@ -0,0 +1,33 @@ +[tool.poetry] +name = "aelf-code-generator" +version = "0.1.0" +description = "AELF Smart Contract Code Generator" +authors = ["Your Name "] +readme = "README.md" +packages = [ + { include = "aelf_code_generator", from = "." } +] + +[tool.poetry.dependencies] +python = "^3.12" +langchain = "0.3.4" +langchain-openai = "0.2.3" +langchain-anthropic = "0.3.1" +langchain-google-genai = "2.0.5" +langgraph-cli = {version = "^0.1.64", extras = ["inmem"]} +langgraph = {version = "^0.2.50", extras = ["api"]} +copilotkit = "0.1.33" +fastapi = "^0.115.0" +uvicorn = "^0.31.0" +pydantic = "^2.6.1" +python-dotenv = "^1.0.1" +tavily-python = "^0.5.0" +html2text = "^2024.2.26" +aiohttp = "^3.11.11" + +[tool.poetry.scripts] +demo = "aelf_code_generator.demo:main" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" \ No newline at end of file diff --git a/ui/.env.local b/ui/.env.local new file mode 100644 index 0000000..a078696 --- /dev/null +++ b/ui/.env.local @@ -0,0 +1,5 @@ +NEXT_PUBLIC_RUNTIME_URL=http://localhost:3000/api/copilotkit +AGENT_URL=http://localhost:3001/copilotkit/generate +GROQ_API_KEY= +NEXT_PUBLIC_FAUCET_API_URL= +NEXT_PUBLIC_GOOGLE_CAPTCHA_SITEKEY= \ No newline at end of file diff --git a/ui/eslint.config.mjs b/ui/eslint.config.mjs new file mode 100644 index 0000000..c85fb67 --- /dev/null +++ b/ui/eslint.config.mjs @@ -0,0 +1,16 @@ +import { dirname } from "path"; +import { fileURLToPath } from "url"; +import { FlatCompat } from "@eslint/eslintrc"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const compat = new FlatCompat({ + baseDirectory: __dirname, +}); + +const eslintConfig = [ + ...compat.extends("next/core-web-vitals", "next/typescript"), +]; + +export default eslintConfig; diff --git a/ui/next.config.ts b/ui/next.config.ts new file mode 100644 index 0000000..0026498 --- /dev/null +++ b/ui/next.config.ts @@ -0,0 +1,18 @@ +/** @type {import('next').NextConfig} */ +const nextConfig = { + experimental: { + serverActions: { + bodySizeLimit: '2mb', + }, + }, + async rewrites() { + return [ + { + source: '/api/:path*', + destination: `https://playground.aelf.com/:path*`, + }, + ] + }, +}; + +export default nextConfig; diff --git a/ui/package.json b/ui/package.json new file mode 100644 index 0000000..bd3b47c --- /dev/null +++ b/ui/package.json @@ -0,0 +1,64 @@ +{ + "name": "aelf-code-generator", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "@apollo/client": "^3.13.1", + "@copilotkit/backend": "^0.37.0", + "@copilotkit/react-core": "^1.5.18", + "@copilotkit/react-textarea": "^1.5.18", + "@copilotkit/react-ui": "^1.5.18", + "@copilotkit/runtime": "^1.5.11", + "@copilotkit/runtime-client-gql": "^1.5.18", + "@copilotkit/shared": "^1.5.11", + "@hookform/resolvers": "^4.1.0", + "@monaco-editor/react": "^4.6.0", + "@radix-ui/react-context-menu": "^2.2.6", + "@radix-ui/react-dropdown-menu": "^2.1.6", + "@radix-ui/react-tabs": "^1.1.2", + "aelf-sdk": "^3.4.15", + "aelf-smartcontract-viewer": "^1.1.1", + "ai": "^4.0.33", + "class-variance-authority": "^0.7.1", + "clsx": "^2.1.1", + "dexie": "^4.0.11", + "fflate": "^0.8.2", + "file-saver": "^2.0.5", + "framer-motion": "^12.4.2", + "graphql": "^16.10.0", + "jszip": "^3.10.1", + "langchain": "^0.3.11", + "langsmith": "^0.2.15", + "next": "15.1.4", + "openai": "^4.78.1", + "openai-edge": "^1.2.2", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-google-recaptcha": "^3.1.0", + "react-hook-form": "^7.54.2", + "react-syntax-highlighter": "^15.6.1", + "tailwind-merge": "^2.6.0", + "tailwind-scrollbar": "^4.0.0", + "uuid": "^11.0.5" + }, + "devDependencies": { + "@eslint/eslintrc": "^3", + "@types/file-saver": "^2.0.7", + "@types/node": "^20.17.12", + "@types/react": "^19.0.5", + "@types/react-dom": "^19", + "@types/react-syntax-highlighter": "^15.5.13", + "@types/webpack": "^5.28.5", + "eslint": "^9", + "eslint-config-next": "15.1.4", + "postcss": "^8", + "tailwindcss": "^3.4.1", + "typescript": "^5" + } +} diff --git a/ui/postcss.config.mjs b/ui/postcss.config.mjs new file mode 100644 index 0000000..1a69fd2 --- /dev/null +++ b/ui/postcss.config.mjs @@ -0,0 +1,8 @@ +/** @type {import('postcss-load-config').Config} */ +const config = { + plugins: { + tailwindcss: {}, + }, +}; + +export default config; diff --git a/ui/public/file.svg b/ui/public/file.svg new file mode 100644 index 0000000..004145c --- /dev/null +++ b/ui/public/file.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/public/globe.svg b/ui/public/globe.svg new file mode 100644 index 0000000..567f17b --- /dev/null +++ b/ui/public/globe.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/public/next.svg b/ui/public/next.svg new file mode 100644 index 0000000..5174b28 --- /dev/null +++ b/ui/public/next.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/public/vercel.svg b/ui/public/vercel.svg new file mode 100644 index 0000000..7705396 --- /dev/null +++ b/ui/public/vercel.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/public/window.svg b/ui/public/window.svg new file mode 100644 index 0000000..b2b2a44 --- /dev/null +++ b/ui/public/window.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/src/app/[workplaceId]/page.tsx b/ui/src/app/[workplaceId]/page.tsx new file mode 100644 index 0000000..66da12f --- /dev/null +++ b/ui/src/app/[workplaceId]/page.tsx @@ -0,0 +1,20 @@ +"use client"; + +import { MainLayout } from "@/components/layout/MainLayout"; +import { FileExplorer } from "@/components/file-explorer/file-explorer"; +import { CodeEditor } from "@/components/editor/CodeEditor"; +import { ChatWindow } from "@/components/chat"; + +export default function Workplace() { + return ( + +
+ + +
+ +
+
+
+ ); +} diff --git a/ui/src/app/api/build/route.ts b/ui/src/app/api/build/route.ts new file mode 100644 index 0000000..871d485 --- /dev/null +++ b/ui/src/app/api/build/route.ts @@ -0,0 +1,116 @@ +import { NextResponse } from 'next/server'; +import { exec } from 'child_process'; +import { promisify } from 'util'; +import fs from 'fs/promises'; +import path from 'path'; +import os from 'os'; + +const execAsync = promisify(exec); + +export async function POST(req: Request) { + try { + const { folderStructure } = await req.json(); + + // Create a temporary directory with a unique name + const tempDir = path.join(os.tmpdir(), 'aelf-contract-' + Date.now()); + await fs.mkdir(tempDir, { recursive: true }); + + // Write files to temp directory + const writeFiles = async (structure: any, currentPath: string) => { + for (const [name, content] of Object.entries(structure)) { + const filePath = path.join(currentPath, name); + if (typeof content === 'string') { + // Create directories if they don't exist + await fs.mkdir(path.dirname(filePath), { recursive: true }); + await fs.writeFile(filePath, content); + } else { + await fs.mkdir(filePath, { recursive: true }); + await writeFiles(content, filePath); + } + } + }; + + await writeFiles(folderStructure, tempDir); + + // Find the .csproj file + let csprojFile = ''; + const findCsprojFile = async (dir: string): Promise => { + const files = await fs.readdir(dir, { withFileTypes: true }); + + for (const file of files) { + const fullPath = path.join(dir, file.name); + if (file.isDirectory()) { + const found = await findCsprojFile(fullPath); + if (found) return found; + } else if (file.name.endsWith('.csproj')) { + return fullPath; + } + } + return ''; + }; + + csprojFile = await findCsprojFile(tempDir); + + if (!csprojFile) { + throw new Error('No .csproj file found in the project'); + } + + // Get the directory containing the .csproj file + const projectDir = path.dirname(csprojFile); + + // Run dotnet restore first + try { + const { stdout: restoreOutput, stderr: restoreError } = await execAsync('dotnet restore', { + cwd: projectDir, + env: { ...process.env, DOTNET_CLI_HOME: tempDir } + }); + console.log('Restore output:', restoreOutput); + if (restoreError) console.error('Restore error:', restoreError); + } catch (error) { + console.error('Restore failed:', error); + throw new Error(`Restore failed: ${(error as Error).message}`); + } + + // Then run dotnet build + try { + const { stdout, stderr } = await execAsync('dotnet build', { + cwd: projectDir, + env: { ...process.env, DOTNET_CLI_HOME: tempDir } + }); + + // Clean up + await fs.rm(tempDir, { recursive: true, force: true }); + + if (stderr) { + console.error('Build stderr:', stderr); + return NextResponse.json({ + success: false, + error: stderr + }, { status: 400 }); + } + + return NextResponse.json({ + success: true, + output: stdout + }); + + } catch (error) { + console.error('Build error:', error); + // Clean up even if build fails + await fs.rm(tempDir, { recursive: true, force: true }); + + throw error; + } + + } catch (error) { + console.error('Process error:', error); + return NextResponse.json( + { + success: false, + error: 'Build failed: ' + (error as Error).message, + details: (error as any).stderr || '' + }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/ui/src/app/api/copilotkit/route.ts b/ui/src/app/api/copilotkit/route.ts new file mode 100644 index 0000000..660d31e --- /dev/null +++ b/ui/src/app/api/copilotkit/route.ts @@ -0,0 +1,33 @@ +import { NextRequest, NextResponse } from "next/server"; + +export const POST = async (req: NextRequest) => { + console.log("Received request to /copilotkit endpoint"); + + try { + const body = await req.json(); + console.log("Request body:", body); + + const response = await fetch(process.env.AGENT_URL || "http://localhost:3001/generate", { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }); + + if (!response.ok) { + throw new Error(`Agent responded with status: ${response.status}`); + } + + const data = await response.json(); + console.log("Agent response:", data); + + return NextResponse.json(data); + } catch (error: any) { + console.error("Error in /copilotkit endpoint:", error); + return NextResponse.json( + { error: error.message || "Internal Server Error" }, + { status: error.status || 500 } + ); + } +}; diff --git a/ui/src/app/contract-viewer/page.tsx b/ui/src/app/contract-viewer/page.tsx new file mode 100644 index 0000000..735d34b --- /dev/null +++ b/ui/src/app/contract-viewer/page.tsx @@ -0,0 +1,35 @@ +"use client"; + +import React from "react"; +import { MainLayout } from "@/components/layout/MainLayout"; +import { useWallet } from "@/data/wallet"; +import { useSearchParams } from "next/navigation"; +// @ts-ignore +import { ContractView } from "aelf-smartcontract-viewer"; +import "./style.css"; + +const sideChainTestnetRpc = "https://tdvw-test-node.aelf.io"; + +const ContractViewer = () => { + const wallet = useWallet(); + const searchParams = useSearchParams(); + const contractViewerAddress = searchParams.get("address"); + + return ( + +
+ {wallet && ( + + )} +
+
+ ); +}; + +export default ContractViewer; diff --git a/ui/src/app/contract-viewer/style.css b/ui/src/app/contract-viewer/style.css new file mode 100644 index 0000000..eca43d4 --- /dev/null +++ b/ui/src/app/contract-viewer/style.css @@ -0,0 +1,17 @@ +.contract-view-container{ + background-color: transparent !important; + border: 1px solid #fff3; + max-height: calc(100vh - 120px); + scrollbar-width: thin; + scrollbar-color: #374151 #111827; +} + +.contract-view-container header { + border-bottom-width: 1px !important; +} + +#_rht_toaster *{ + color: black !important; + font-weight: 600; + font-size: 14px; +} \ No newline at end of file diff --git a/ui/src/app/deployments/page.tsx b/ui/src/app/deployments/page.tsx new file mode 100644 index 0000000..d29cbdc --- /dev/null +++ b/ui/src/app/deployments/page.tsx @@ -0,0 +1,48 @@ +"use client"; + +import React, { useMemo } from "react"; +import { MainLayout } from "@/components/layout/MainLayout"; +import { useContractList } from "@/data/graphql"; +import { useWallet } from "@/data/wallet"; +import DeploymentCard from "@/components/ui/deployment-card"; + +const Deployments = () => { + const wallet = useWallet(); + const { data, loading } = useContractList(wallet?.wallet.address); + + const deployments = useMemo(() => { + if (data) { + return data.contractList.items.map((i) => ({ + time: i.metadata.block.blockTime, + address: i.address, + })); + } + return null; + }, [data]); + + return ( + +
+

+ Deployment History +

+ + {loading ? ( +
Loading...
+ ) : ( + deployments && ( +
+
+ {deployments.map((deployment, index) => ( + + ))} +
+
+ ) + )} +
+
+ ); +}; + +export default Deployments; diff --git a/ui/src/app/favicon.ico b/ui/src/app/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000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zIkR2~Q}>X=Ar8W|^Ms41Fm8o6IB2_j60eOeBB1Br!boW7JnoeX6Gs)?7rW0^5psc- zjS16yb>dFn>KPOF;imD}e!enuIniFzv}n$m2#gCCv4jM#ArwlzZ$7@9&XkFxZ4n!V zj3dyiwW4Ki2QG{@i>yuZXQizw_OkZI^-3otXC{!(lUpJF33gI60ak;Uqitp74|B6I zgg{b=Iz}WkhCGj1M=hu4#Aw173YxIVbISaoc z-nLZC*6Tgivd5V`K%GxhBsp@SUU60-rfc$=wb>zdJzXS&-5(NRRodFk;Kxk!S(O(a0e7oY=E( zAyS;Ow?6Q&XA+cnkCb{28_1N8H#?J!*$MmIwLq^*T_9-z^&UE@A(z9oGYtFy6EZef LrJugUA?W`A8`#=m literal 0 HcmV?d00001 diff --git a/ui/src/app/globals.css b/ui/src/app/globals.css new file mode 100644 index 0000000..52c1311 --- /dev/null +++ b/ui/src/app/globals.css @@ -0,0 +1,40 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +.copilotKitInput { + border-radius: 0; +} + +.main-content { + width: calc(100vw - 500px); +} + +.file-result-container { + height: 100%; + max-height: calc(100vh - 120px); +} + +.monaco-editor .margin, +.monaco-editor-background { + /* background-color: #111827 !important */ + background-color: rgb(31 41 55 / 1) !important; +} + +.editor-container { + max-width: calc(100vw - 730px); +} + +.private-key { + width: max-content; + > p { + filter: blur(8px); + user-select: none; + + &.key-visible { + filter: none; + user-select: auto; + } + } + } + \ No newline at end of file diff --git a/ui/src/app/layout.tsx b/ui/src/app/layout.tsx new file mode 100644 index 0000000..bd6f2aa --- /dev/null +++ b/ui/src/app/layout.tsx @@ -0,0 +1,32 @@ +import { ReactNode } from "react"; +import { Open_Sans } from "next/font/google"; +import { CopilotKit } from "@copilotkit/react-core"; +import "@copilotkit/react-ui/styles.css"; + +import { ModelSelectorProvider } from "@/lib/model-selector-provider"; +import { Header } from "@/components/header"; +import Providers from "@/providers"; +import "./globals.css"; + + +const openSans = Open_Sans({ subsets: ["latin"] }); + +export const metadata = { + title: "AElf Code Generator", + description: "AI-powered code generator for AElf ecosystem", +}; + +export default function RootLayout({ children }: { children: ReactNode }) { + return ( + + + + +
+ {children} + + + + + ); +} diff --git a/ui/src/app/page.tsx b/ui/src/app/page.tsx new file mode 100644 index 0000000..8567520 --- /dev/null +++ b/ui/src/app/page.tsx @@ -0,0 +1,139 @@ +"use client"; + +import { useEffect } from "react"; +import { motion, AnimatePresence } from "framer-motion"; + +import { MainLayout } from "@/components/layout/MainLayout"; +import { ChatWindow } from "@/components/chat"; +import { db } from "@/data/db"; +import { benifits } from "@/lib/constants"; +import { Header } from "@/components/header"; +import { useRouter, useSearchParams } from "next/navigation"; +import { Loader } from "@/components/ui/icons"; +import { useShare } from "@/data/client"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; + +export default function Home() { + const searchParams = useSearchParams(); + const shareId = searchParams.get("share"); + const { data, isLoading } = useShare(shareId || ""); + + const router = useRouter(); + + useEffect(() => { + if (shareId) { + const importWorkspace = async () => { + if (!data?.files || !shareId) return; + + const existing = await db.workspaces.get(shareId); + + if (existing) { + router.push(`/${shareId}`); + } else { + await db.workspaces.add({ + name: shareId, + template: shareId, + dll: "", + }); + + await db.files.bulkAdd( + data.files.map(({ path, contents }) => ({ + path: `/workspace/${shareId}/${path}`, + contents, + })) + ); + router.push(`/${shareId}`); + } + }; + + importWorkspace(); + } + }, [shareId, data?.files]); + + if (shareId) { + if (data?.success === false) { + return ( + router.push("/")}> + + + Error + {data.message} + + + + ); + } + if (isLoading) { + return ( + +
+

+ Getting Shared Details... +

+
+
+ ); + } + } + + return ( + +
+ + + +

+ Generate smart contracts effortlessly using C# language. Describe + your requirements or choose from our templates below. +

+
+ + + + + + {/* Optional: Add some features or benefits section */} + + {benifits.map((feature, index) => ( +
+
{feature.icon}
+

{feature.title}

+

{feature.description}

+
+ ))} +
+
+
+ + ); +} diff --git a/ui/src/app/projects/page.tsx b/ui/src/app/projects/page.tsx new file mode 100644 index 0000000..da19416 --- /dev/null +++ b/ui/src/app/projects/page.tsx @@ -0,0 +1,34 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { db, Workspace } from "@/data/db"; // Ensure you have the db instance imported +import ProjectCard from "@/components/ui/project-card"; +import { MainLayout } from "@/components/layout/MainLayout"; + +export default function Workplace() { + const [projects, setProjects] = useState([]); + + const fetchProjects = async () => { + const allProjects = await db.workspaces.toArray(); // Fetch all projects from the database + allProjects && setProjects(allProjects); // Directly set the state with the array of projects + }; + + useEffect(() => { + fetchProjects(); + }, []); + + return ( + +
+

+ Your Workspaces +

+
+ {projects.map((project: Workspace, index) => ( + + ))} +
+
+
+ ); +} diff --git a/ui/src/app/wallet/page.tsx b/ui/src/app/wallet/page.tsx new file mode 100644 index 0000000..688ffb0 --- /dev/null +++ b/ui/src/app/wallet/page.tsx @@ -0,0 +1,69 @@ +"use client"; + +import { useState } from "react"; +import { EyeIcon, EyeOffIcon } from "lucide-react"; +import Link from "next/link"; + +import { useWallet } from "@/data/wallet"; +import { Loader } from "@/components/ui/icons"; +import { MainLayout } from "@/components/layout/MainLayout"; + +export default function Wallet() { + const wallet = useWallet(); + + return ( + +
+

Wallet information

+ {wallet ? ( +
+

+ Wallet address:{" "} + +

+

Privatekey:

+ +
+ ) : ( +
+ Loading... +
+ )} +
+
+ ); +} + +function ViewPrivatekey({ privateKey }: { privateKey: string }) { + const [isVisibleKey, setIsVisibleKey] = useState(false); + + const toggleKey = () => setIsVisibleKey((prev: boolean) => !prev); + + return ( +
+

{privateKey}

+ + +
+ ); +} + +function ViewAddressOnExplorer({ address }: { address: string }) { + return ( + + AELF_{address}_tDVW + + ); +} diff --git a/ui/src/components/chat/ChatHeader.tsx b/ui/src/components/chat/ChatHeader.tsx new file mode 100644 index 0000000..6499493 --- /dev/null +++ b/ui/src/components/chat/ChatHeader.tsx @@ -0,0 +1,14 @@ +interface ChatHeaderProps { + fullScreen?: boolean; +} + +export const ChatHeader = ({ fullScreen = false }: ChatHeaderProps) => { + return ( +
+

Chat Bot

+ {fullScreen && ( +

Start by describing your smart contract requirements or choose a template below

+ )} +
+ ); +}; \ No newline at end of file diff --git a/ui/src/components/chat/ChatInput.tsx b/ui/src/components/chat/ChatInput.tsx new file mode 100644 index 0000000..0ed3a97 --- /dev/null +++ b/ui/src/components/chat/ChatInput.tsx @@ -0,0 +1,146 @@ +import { useRef, useEffect } from 'react'; + +interface ChatInputProps { + value: string; + onChange: (value: string) => void; + onSubmit: (e: React.FormEvent) => void; + loading: boolean; + fullScreen?: boolean; +} + +export const ChatInput = ({ + value, + onChange, + onSubmit, + loading, + fullScreen = false, +}: ChatInputProps) => { + const textareaRef = useRef(null); + + // Auto-resize textarea + useEffect(() => { + const textarea = textareaRef.current; + if (textarea) { + textarea.style.height = 'auto'; + textarea.style.height = `${textarea.scrollHeight}px`; + } + }, [value]); + + // Handle form submission + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + if (!loading && value.trim()) { + onSubmit(e); + } + }; + + // Handle key press + const handleKeyPress = (e: React.KeyboardEvent) => { + if (e.key === 'Enter' && !e.shiftKey) { + e.preventDefault(); + handleSubmit(e); + } + }; + + return ( +
+
+
+