Micro Agent

repository·main·Indexed 26 days ago

https://github.com/builderio/micro-agent

An AI-driven CLI tool (@builder.io/micro-agent) that writes and fixes code by generating tests and iterating on implementations until they pass. It supports unit test matching, experimental visual matching via screenshots, and integrates with Claude, OpenAI, Ollama, and other OpenAI-compatible providers.

Tokens
3K
Snippets
6
Records
24
Agent score
87%

What's inside micro-agent

  1. Use Unit Test Matching mode

    main

    Run Micro Agent on a specific file and iterate until a provided test script passes.

    Default behavior: Micro Agent looks for a test file with the same name as the target file but with .test.ts appended (e.g., ./file.ts looks for ./file.test.ts).

    Flags:

    • -t, --test <string>: The test script to run (e.g., npm test).
    • -f, --test-file <string>: Specify a custom test file path.
    • -p, --prompt <string>: Specify a custom prompt file path (defaults to <filename>.prompt.md).
  2. Configure LLM API keys and models

    main

    Micro Agent supports Claude, OpenAI, Ollama, or any OpenAI compatible provider (like Groq). Use the config set command to configure your environment.

    For OpenAI/Compatible providers:

    • Set API key: OPENAI_KEY
    • Set model: MODEL (defaults to gpt-4o)
    • Set custom endpoint: OPENAI_API_ENDPOINT

    For Claude:

    • Set API key: ANTHROPIC_KEY
    • Set model: MODEL (use claude as an alias for claude-3-5-sonnet-20241022)
    # OpenAI configuration
    micro-agent config set OPENAI_KEY=<your token>
    micro-agent config set MODEL=gpt-4o
    micro-agent config set OPENAI_API_ENDPOINT=https://api.groq.com/openai/v1
    
    # Claude configuration
    micro-agent config set ANTHROPIC_KEY=<your token>
    micro-agent config set MODEL=claude
  3. Use Visual Matching mode (experimental)

    main

    Match a design by providing a design image and a local URL to your rendered code. Micro Agent will iterate until the rendered output matches the screenshot.

    Requirements:

    • Requires an Anthropic API key (recommended for visual matching).
    • A screenshot file must exist next to the code being edited (e.g., ./app/about/page.png for ./app/about/page.tsx).

    Flags:

    • --visual <string>: The local URL of your rendered code (e.g., localhost:3000/about).
    micro-agent ./app/about/page.tsx --visual localhost:3000/about
  4. Configure Micro Agent via CLI or UI

    main

    You can manage settings using the config command.

    • Interactive UI: Run micro-agent config to open a visual interface for setting keys and models.
    • Environment Variables: All config options can be overridden by environment variables (e.g., MODEL=gpt-3.5-turbo micro-agent ...).
    • Max Runs: Use the -m flag to change the maximum number of iteration attempts (default is 10).
  5. Micro Agent CLI Reference

    main

    Full list of available commands and flags:

    Commands:

    • config: Configure the CLI
    • update: Update Micro Agent to the latest version

    Flags:

    • -h, --help: Show help
    • -m, --max-runs <number>: The maximum number of runs to attempt
    • -p, --prompt <string>: Prompt to run
    • -t, --test <string>: The test script to run
    • -f, --test-file <string>: The test file to run
    • -v, --visual <string>: Visual matching URL
    • --thread <string>: Thread ID to resume
    • --version: Show version
  6. Run Micro Agent in interactive mode

    main

    The interactiveMode function starts a CLI-based interactive session. It guides the user through:

    1. Providing an OPENAI_KEY if one is not already configured.
    2. Entering a natural language prompt describing the task (e.g., "A function that...").
    3. Selecting a target file to create or edit (it uses an LLM to suggest a file path based on your prompt).
    4. Generating a unit test file if one doesn't exist, allowing for iterative feedback.
    5. Confirming the test command to run.
    6. Executing the agent loop to fulfill the prompt.

    It accepts a Partial<RunOptions> object to configure the session.

  7. Retrieve configuration with getConfig()

    main

    The getConfig function retrieves the current configuration. It follows a priority order:

    1. Values passed via cliConfig (as a RawConfig object).
    2. Values stored in the local configuration file (~/.micro-agent).
    3. Environment variables (which are merged into the final object).

    Returns a ValidConfig object where all values have been parsed according to their specific types (e.g., booleans are converted from strings, models have defaults).

  8. Generate a CLI command string with createCommandString()

    main
    Use createCommandString() to generate a formatted shell command string based on your current RunOptions. This is useful for providing a 'resume' command to the user if the agent reaches the maximum number of runs without passing tests.
  9. Detect invalid test commands

    main

    The isInvalidCommand function identifies if a command execution failed because the command itself was not found or the script is missing. It checks for the following substrings in the output:

    • command not found:
    • command_not_found:
    • npm ERR! Missing script: