This repository contains the backend service for an AI code generation platform built with Spring Boot. The service uses LangChain4j to interact with AI models (for example, OpenAI) and supports streaming code generation via Server-Sent Events (SSE). It can generate single-file HTML or multi-file project output, save results locally, and deploy generated sites for static preview.
The full-stack web application is deployed here.
- Generate code from user prompts (single-file HTML or multi-file projects) using AI models.
- Stream generation results to clients via SSE so the frontend can render progress and file fragments in real time.
- Save generated code to local directories under
tmp/code_outputand deploy previews totmp/code_deploy/{deployKey}. - Manage users, apps, and chat history with MySQL using MyBatis-Flex.
- Centralized validation and error handling conventions (use
ThrowUtils.throwIf()and returnBaseResponse<T>for APIs).
The project follows a layered architecture and makes use of several design patterns:
-
Major packages and responsibilities:
com.kbc.agent.agent_backend.core— core orchestration (e.g.,AiCodeGeneratorFacade) that coordinates the generation flow.com.kbc.agent.agent_backend.ai— services that integrate with LangChain4j and the AI provider (e.g.,AiCodeGeneratorService), including streaming, parsers, and routing strategies.com.kbc.agent.agent_backend.service— business services for apps, users, chat history, etc.com.kbc.agent.agent_backend.controller— REST controllers, including controllers that return SSE streams.com.kbc.agent.agent_backend.utils— utilities (file operations, screenshots, static resource helpers, etc.).
-
Patterns & conventions:
- Template Method:
CodeFileSaverTemplate<T>provides a unified file save workflow. - Strategy/Router: different parsers/savers are used depending on generation type (single-file vs multi-file).
- SSE streaming: uses
Flux<ServerSentEvent<String>>; chunks are wrapped in JSON (e.g. {"d":"..."}) to avoid whitespace loss in browsers; stream ends withevent: "done". - Two-stage loading: when building view objects, entities are loaded first and related data fetched in batch to avoid N+1 queries.
- Template Method:
-
Prompts and templates:
- System prompts live in
src/main/resources/prompt/(e.g.codegen-html-system-prompt.txt,codegen-multi-file-system-prompt.txt). - AI outputs commonly come in Markdown code fences (
html,css,js); parsers extract code blocks and save them as files.
- System prompts live in
agent-backend/pom.xml— Maven build and dependencies.src/main/java/.../AiCodeGeneratorFacade.java— facade that orchestrates AI-based code generation flows.src/main/java/.../AiCodeGeneratorService.java— integrates with LangChain4j and handles streaming responses.src/main/java/.../CodeFileSaverTemplate.java— template for saving generated files.src/main/resources/prompt/— system prompt templates and instruction files for the AI.tmp/code_output— temporary directory for generated code artifacts.tmp/code_deploy/{deployKey}— deployed preview directories served by the static controller.