Skip to content

HikaruEgashira/lm-suggester

Repository files navigation

lm-suggester

Go Reference Test Release

Convert LLM suggestions and external tool outputs to reviewdog JSON format for seamless code review automation.

Agentic PR review prompt

Review provided Pull Request.
1. Checkout code
gh pr checkout 123

2. Analyze the code and generate suggestions in this format
{"file_path":"path/to/file","lm_before":"<exact match>","lm_after":"<replacement>","message":"<reason>"}

Requirements:
- lm_before must match exactly (including whitespace)
- JSONL format also supported

3. Execute this pipeline
echo '<your suggestions here>' | lm-suggester | CI_REPO_OWNER=owner CI_REPO_NAME=repo CI_PULL_REQUEST=123 CI_COMMIT=$(gh pr view 123 -q .headRefOid) REVIEWDOG_GITHUB_API_TOKEN=$(gh auth token) reviewdog -f=rdjson -reporter=github-pr-review

Installation

mise use -g github:HikaruEgashira/lm-suggester

For other installation methods, see the releases page.

Usage

Basic Usage

The CLI reads JSON input and converts it to reviewdog format.

echo '{"file_path":"main.go","lm_after":"fixed code","message":"Fix typo"}' | lm-suggester

Input/Output Format

Input

{
  "file_path": "main.go",
  "lm_before": "print(\"Hello, World!\")",
  "lm_after": "fmt.Println(\"Hello, World!\")",
  "message": "Use fmt.Println instead of print"
}

Output (reviewdog JSON)

{
  "diagnostics": [
    {
      "message": "Use fmt.Println instead of print",
      "location": {
        "path": "main.go",
        "range": {
          "start": {"line": 10, "column": 5},
          "end": {"line": 10, "column": 30}
        }
      },
      "severity": "INFO",
      "source": {"name": "lm-suggester"},
      "suggestions": [
        {
          "range": {
            "start": {"line": 10, "column": 5},
            "end": {"line": 10, "column": 30}
          },
          "text": "fmt.Println(\"Hello, World!\")"
        }
      ]
    }
  ]
}

Pass-through Format Support

lm-suggester supports pass-through of additional fields. Any extra fields in the input JSON are preserved alongside the computed diagnostics.

Input with additional fields

{
  "file_path": "main.go",
  "base_text": "package main\n\nfunc main() {\n\tprint(\"Hello, World!\")\n}",
  "lm_before": "print(\"Hello, World!\")",
  "lm_after": "fmt.Println(\"Hello, World!\")",
  "message": "Use fmt.Println instead of print",
  "ruleId": "go/print-style",
  "level": "warning",
  "properties": {
    "tags": ["style", "best-practice"],
    "category": "code-quality"
  }
}

Output (merged fields with diagnostics)

{
  "file_path": "main.go",
  "base_text": "package main\n\nfunc main() {\n\tprint(\"Hello, World!\")\n}",
  "lm_before": "print(\"Hello, World!\")",
  "lm_after": "fmt.Println(\"Hello, World!\")",
  "message": "Use fmt.Println instead of print",
  "ruleId": "go/print-style",
  "level": "warning",
  "properties": {
    "tags": ["style", "best-practice"],
    "category": "code-quality"
  },
  "diagnostics": [
    {
      "message": "Replace code with suggestion\n```suggestion\nfmt.Println(\"Hello, World!\")\n```",
      "location": {
        "path": "main.go",
        "range": {
          "start": {"line": 4, "column": 2},
          "end": {"line": 4, "column": 24}
        }
      }
    }
  ],
  "source": {
    "name": "reviewdog-converter"
  }
}

This pass-through behavior preserves all input fields, allowing integration with various tools while maintaining reviewdog compatibility.

Integration with reviewdog

Manual Integration

Pipe the output directly to reviewdog.

# Generate suggestion and review
your-llm-tool | lm-suggester | reviewdog -f=rdjson -reporter=github-pr-review

# With GitHub Actions
lm-suggester -i suggestion.json | reviewdog -f=rdjson -reporter=github-pr-check

Automatic Execution

Automatically run reviewdog with the --reviewdog flag.

# Run reviewdog with local reporter
lm-suggester -i suggestion.json --reviewdog

# Specify reporter for CI/CD
lm-suggester -i suggestion.json --reviewdog --reporter=github-pr-review

# With custom options
lm-suggester -i suggestion.json --reviewdog \
  --reporter=github-pr-check \
  --filter-mode=diff_context \
  --fail-on-error

# From stdin
echo '{"file_path":"main.go","llm_after":"fixed","message":"Fix"}' | \
  lm-suggester --reviewdog --reporter=local

For available reviewdog options, see the reviewdog documentation.

Use Cases

CI/CD Integration

# GitHub Actions example
- name: Run LLM Review
  run: |
    llm-tool analyze --output suggestions.json
    lm-suggester -i suggestions.json | \
      reviewdog -f=rdjson -reporter=github-pr-review

Local Development

# Run local code review
git diff HEAD^ | llm-tool suggest | \
  lm-suggester | \
  reviewdog -f=rdjson -reporter=local -diff="git diff HEAD^"

As a Library

// Use as a library
import "github.com/HikaruEgashira/lm-suggester/suggester"

input := suggester.Input{
    FilePath:  "main.go",
    LMAfter:  "improved code",
    Message:   "Optimization suggestion",
}

rdJSON, err := suggester.BuildRDJSON(input)

Command Options

Usage:
  lm-suggester [flags]

Flags:
  -i, --input string         Input JSON file (default: stdin)
  -o, --output string        Output file (default: stdout)
  -p, --pretty               Pretty-print JSON output
      --reviewdog            Run reviewdog with the output
      --reporter string      reviewdog reporter (local, github-pr-review, github-pr-check, etc.) (default: local)
      --filter-mode string   reviewdog filter mode (added, diff_context, file, nofilter) (default: nofilter)
      --fail-on-error        Exit with non-zero code when reviewdog finds errors
  -h, --help                 Help for lm-suggester
      --version              Print version information

License

MIT License - see LICENSE file for details.

Related Projects

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Languages