The Compiler Loop
The "magic" of Intend is not just prompting an LLM. It is the loops and validation that occur around it.
The Generation Pipeline
When a cache miss occurs, the AICodeGenerator takes over.
Phase 1: Context & Prompting
The PromptBuilder accepts the AST. It constructs a specialized system prompt that includes:
- Role Definition: "You are an expert TypeScript engineer..."
- Invariants: Constraints are injected as high-priority rules.
- Steps: The logical flow is presented clearly.
- Imports: Type definitions of imported items are resolved and included in the context window.
Phase 2: Generation
We call the provider (Gemini or Ollama). We request JSON or Markdown output depending on the model's capabilities, favoring structured output where possible.
Phase 3: Validation (Self-Healing)
This is the critical step. We assume the AI makes mistakes.
- Extraction: Code is extracted from markdown code blocks.
- Syntax Validation: We use
ts.createSourceFilefrom the TypeScript Compiler API to check for syntax errors. - Compilation Check: We may run a partial type-check (if enabled).
Phase 4: Retry Loop
If Phase 3 finds errors:
- We do not fail the build yet.
- We construct a Diagnostic Prompt.
- "You generated this code: [Code]"
- "It produced these errors: [Error List]"
- "Please fix it."
- We recursively call the generator with this new context.
This repeats up to maxAttempts (default: 3).
Phase 5: Final Output
Only if the code passes all checks is it written to disk and committed to the CAS.