Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

improve: Add security and reliability improvements to AI code reviewer

LearningCircuit7e24bd467a9c
diff --git a/.github/workflows/ai-code-reviewer.yml b/.github/workflows/ai-code-reviewer.yml
index 1ae0f9d88b..414139ce0a 100644
--- a/.github/workflows/ai-code-reviewer.yml
+++ b/.github/workflows/ai-code-reviewer.yml
@@ -32,9 +32,14 @@ jobs:
- name: AI Code Review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
+ AI_MODEL: z-ai/glm-4.6
+ AI_TEMPERATURE: 0.1
+ AI_MAX_TOKENS: 2000
+ MAX_DIFF_SIZE: 100000
run: |
# Run the AI reviewer and post the comment directly via stdin
- cat diff.txt | bash github/scripts/ai-reviewer.sh "${{ secrets.OPENROUTER_API_KEY }}" | gh pr comment ${{ github.event.pull_request.number }} --repo ${{ github.repository }} -F -
+ cat diff.txt | bash github/scripts/ai-reviewer.sh | gh pr comment ${{ github.event.pull_request.number }} --repo ${{ github.repository }} -F -
- name: Cleanup
if: always()
diff --git a/github/scripts/ai-reviewer.sh b/github/scripts/ai-reviewer.sh
index bc7479758c..9a08bb3548 100755
--- a/github/scripts/ai-reviewer.sh
+++ b/github/scripts/ai-reviewer.sh
@@ -1,19 +1,26 @@
#!/bin/bash
# AI Code Reviewer Script - Simple Comment Version
-# Usage: echo "DIFF_CONTENT" | ./ai-reviewer.sh <API_KEY>
+# Usage: OPENROUTER_API_KEY=xxx AI_MODEL=model AI_TEMPERATURE=0.1 AI_MAX_TOKENS=2000 echo "DIFF_CONTENT" | ./ai-reviewer.sh
set -e
-API_KEY="$1"
+# Get API key from environment variable
+API_KEY="${OPENROUTER_API_KEY}"
if [ -z "$API_KEY" ]; then
echo "## 🤖 AI Code Review
-❌ **Error**: Missing API key parameter"
+❌ **Error**: Missing OPENROUTER_API_KEY environment variable"
exit 1
fi
+# Configuration with defaults
+AI_MODEL="${AI_MODEL:-z-ai/glm-4.6}"
+AI_TEMPERATURE="${AI_TEMPERATURE:-0.1}"
+AI_MAX_TOKENS="${AI_MAX_TOKENS:-2000}"
+MAX_DIFF_SIZE="${MAX_DIFF_SIZE:-100000}" # 100KB default limit
+
# Read diff content from stdin
DIFF_CONTENT=$(cat)
@@ -24,6 +31,16 @@ if [ -z "$DIFF_CONTENT" ]; then
exit 1
fi
+# Validate diff size to prevent excessive API usage
+DIFF_SIZE=${#DIFF_CONTENT}
+if [ "$DIFF_SIZE" -gt "$MAX_DIFF_SIZE" ]; then
+ echo "## 🤖 AI Code Review
+
+❌ **Error**: Diff is too large ($DIFF_SIZE bytes, max: $MAX_DIFF_SIZE bytes)
+Please split this PR into smaller changes for review."
+ exit 1
+fi
+
# Create the JSON request with proper escaping using jq
USER_PROMPT="Please analyze this code diff and provide a comprehensive review in markdown format:
@@ -40,8 +57,11 @@ $DIFF_CONTENT"
jq -n \
--arg system_content "You are a helpful code reviewer analyzing pull requests. Provide a comprehensive review covering security, performance, code quality, and best practices. Respond with a clear, human-readable comment in markdown format." \
--arg user_content "$USER_PROMPT" \
+ --arg model "$AI_MODEL" \
+ --argjson temperature "$AI_TEMPERATURE" \
+ --argjson max_tokens "$AI_MAX_TOKENS" \
'{
- "model": "z-ai/glm-4.6",
+ "model": $model,
"messages": [
{
"role": "system",
@@ -52,8 +72,8 @@ jq -n \
"content": $user_content
}
],
- "temperature": 0.1,
- "max_tokens": 2000
+ "temperature": $temperature,
+ "max_tokens": $max_tokens
}' > request.json
# Make API call to OpenRouter
@@ -78,9 +98,20 @@ fi
CONTENT=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // "error"')
if [ "$CONTENT" = "error" ]; then
+ # Try to extract error details from the API response
+ ERROR_MSG=$(echo "$RESPONSE" | jq -r '.error.message // "Invalid API response format"')
+ ERROR_CODE=$(echo "$RESPONSE" | jq -r '.error.code // ""')
+
echo "## 🤖 AI Code Review
-❌ **Error**: Invalid API response format"
+❌ **Error**: $ERROR_MSG"
+
+ if [ -n "$ERROR_CODE" ]; then
+ echo "Error code: \`$ERROR_CODE\`"
+ fi
+
+ # Log full response for debugging (will appear in GitHub Actions logs)
+ echo "Full API response: $RESPONSE" >&2
exit 1
fi