Compare commits

..

16 Commits

Author SHA1 Message Date
jiantw83 4a67dec32a feat: 階段三 - AI 語意去重,失敗時降級保留所有問題 2026-05-11 09:40:38 +00:00
jiantw83 5c5660a34b feat: update OpenAI base URL to use OpenRouter API 2026-05-11 09:37:53 +00:00
jiantw83 6ecb018ef4 feat: update OpenAI base URL to use OpenRouter API 2026-05-11 09:37:01 +00:00
jiantw83 02529a4ec9 feat: update OpenAI API key in README.md 2026-05-11 09:34:43 +00:00
jiantw83 624a71836c feat: update AI Code Review step to use OpenAI API key and base URL 2026-05-11 09:31:01 +00:00
jiantw83 fb1254aa32 feat: refactor AI Code Review step to use OLLAMA_BASE_URL and OLLAMA_MODEL
Co-authored-by: Copilot <copilot@github.com>
2026-05-11 09:10:20 +00:00
jiantw83 6eae6eb0ce feat: add OPENAI_MODEL parameter to AI Code Review step 2026-05-11 09:07:21 +00:00
jiantw83 ed1f2bea15 feat: update AI Code Review step to use DeepSeek API and correct API key
Co-authored-by: Copilot <copilot@github.com>
2026-05-11 09:03:03 +00:00
jiantw83 9a11d25c00 revert: 移除 DeepSeek-R1 特別處理 2026-05-11 08:58:59 +00:00
jiantw83 64b904dd07 fix: 支援不接受 system role 的模型(DeepSeek-R1) 2026-05-11 08:56:48 +00:00
jiantw83 73c11129ab feat: update AI Code Review step to use new OpenAI base URL and model 2026-05-11 08:55:53 +00:00
jiantw83 7ba2af3384 feat: update OPENAI_MODEL to use DeepSeek-R1-Distill-Qwen-32B for improved performance 2026-05-11 08:54:13 +00:00
jiantw83 aca76f23af feat: add OPENAI_MODEL parameter to AI Code Review step 2026-05-11 08:53:03 +00:00
jiantw83 bdf8d8a797 feat: update AI Code Review step to use OpenAI API key and base URL 2026-05-11 08:47:34 +00:00
jiantw83 e183e31ce0 fix: 忽略 SSL 憑證驗證(支援自簽憑證的 Ollama/Gitea) 2026-05-11 08:34:04 +00:00
jiantw83 7b5decf46a feat: update AI Code Review step to use vars instead of secrets for improved flexibility 2026-05-11 08:31:02 +00:00
6 changed files with 53 additions and 9 deletions
+2 -2
View File
@@ -28,8 +28,8 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@v${{ needs.version.outputs.version }}
with:
OLLAMA_BASE_URL: ${{ secrets.OLLAMA_BASE_URL }}
OLLAMA_MODEL: ${{ secrets.OLLAMA_MODEL }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_BASE_URL: https://openrouter.ai/api/v1
permissions:
contents: write
pull-requests: write
+3 -1
View File
@@ -41,8 +41,10 @@ jobs:
- name: AI Code Review
uses: https://gitea.jsc.idv.tw/jiantw83/code-review@${{ vars.ACTION_CODE_REVIEW_VERSION }}
with:
# Github (h3285@evertrust.com.tw)
# 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
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_BASE_URL: https://openrouter.ai/api/v1
permissions:
contents: write
pull-requests: write
+32
View File
@@ -61,3 +61,35 @@ export function mergeFindings(oldFindings, newFindings) {
export function sortByLevel(findings) {
return [...findings].sort((a, b) => LEVELS.indexOf(a.level) - LEVELS.indexOf(b.level));
}
/**
* 呼叫 LLM 進行語意去重,回傳去重後的 findings
* 失敗時降級回傳原始 findings
*/
export async function deduplicateWithAI(findings) {
if (findings.length === 0) return findings;
const systemPrompt = `你是一位程式碼審查問題去重專家。
給你一份問題清單(JSON 陣列),請移除語意重複的問題(即使描述文字不同,但指的是同一個問題)。
保留等級較高的版本,優先保留 critical > warning > info。
只回傳去重後的 JSON 陣列,不要有其他文字。`;
const userContent = `以下是問題清單,請去除語意重複的項目:\n\n${JSON.stringify(findings, null, 2)}`;
try {
const result = await chatJSON(systemPrompt, userContent);
if (Array.isArray(result) && result.length > 0) {
console.log(` AI 去重: ${findings.length} -> ${result.length}`);
return result;
}
throw new Error('AI 回傳空陣列');
} catch (e) {
const status = e.response?.status;
if (status === 402 || status === 429) {
console.log(` ⚠️ AI 去重失敗(${status} 額度/限流),降級:保留所有問題`);
} else {
console.log(` ⚠️ AI 去重失敗(${e.message}),降級:保留所有問題`);
}
return findings;
}
}
+4 -2
View File
@@ -1,15 +1,17 @@
import axios from 'axios';
import https from 'https';
import { GITEA_TOKEN, GITEA_SERVER_URL, GITEA_REPOSITORY, PR_NUMBER } from './config.js';
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const headers = () => ({ Authorization: `token ${GITEA_TOKEN}`, 'Content-Type': 'application/json' });
const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`;
export async function getPRDiff() {
const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000 });
const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent });
return resp.data;
}
export async function postComment(body) {
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000 });
const resp = await axios.post(api(`/repos/${GITEA_REPOSITORY}/issues/${PR_NUMBER}/comments`), { body }, { headers: headers(), timeout: 30000, httpsAgent });
return resp.data;
}
+4 -1
View File
@@ -1,6 +1,9 @@
import axios from 'axios';
import https from 'https';
import { getLLMConfig } from './config.js';
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
export async function chat(systemPrompt, userContent) {
const { provider, apiKey, baseURL, model } = getLLMConfig();
if (!provider) throw new Error('未設定任何 LLM API Key');
@@ -16,7 +19,7 @@ export async function chat(systemPrompt, userContent) {
const resp = await axios.post(
`${baseURL.replace(/\/$/, '')}/chat/completions`,
{ model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 },
{ headers, timeout: 120000 }
{ headers, timeout: 120000, httpsAgent }
);
return resp.data.choices[0].message.content;
}
+8 -3
View File
@@ -1,7 +1,7 @@
import { GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH, PR_BASE_BRANCH, getLLMConfig } from './config.js';
import { loadRoles, getRoleIntro } from './roles.js';
import { getPRDiff, postComment } from './gitea.js';
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel } from './findings.js';
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel, deduplicateWithAI } from './findings.js';
const WORKSPACE = process.env.GITHUB_WORKSPACE || '/workspace';
@@ -66,8 +66,13 @@ async function main() {
console.log('\n🔀 Step3: Findings 合併');
const oldFindings = loadOldFindings(WORKSPACE);
const mergedFindings = mergeFindings(oldFindings, newFindings);
const sorted = sortByLevel(mergedFindings);
console.log(` Step3 merged findings total=${sorted.length} (critical=${sorted.filter(f=>f.level==='critical').length} warning=${sorted.filter(f=>f.level==='warning').length} info=${sorted.filter(f=>f.level==='info').length})`);
console.log(` Step3 merged findings total=${mergedFindings.length}`);
// Step3b: AI 語意去重
console.log('\n🤖 Step3b: AI 語意去重');
const deduped = await deduplicateWithAI(mergedFindings);
const sorted = sortByLevel(deduped);
console.log(` Step3b dedup findings total=${sorted.length} (critical=${sorted.filter(f=>f.level==='critical').length} warning=${sorted.filter(f=>f.level==='warning').length} info=${sorted.filter(f=>f.level==='info').length})`);
console.log('\n📝 Step4: Findings 寫入與 Comment 發布(待實作)');
console.log(' [stub] 寫入 findings.json,發布 comment...');