-
Notifications
You must be signed in to change notification settings - Fork 1
Test/diff actions ai #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
dc9b935
1306ce7
b49b937
31adb7c
7499491
eae76dd
76bbfbe
2349847
fe0a136
adeeb4c
eba1226
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,12 +12,12 @@ const App = () => { | |
| <p>Start building amazing things with Rsbuild.</p> | ||
|
|
||
| <div className="button-container"> | ||
| <button onClick={handleClick} className="primary-button"> | ||
| {/* <button onClick={handleClick} className="primary-button"> | ||
| Click Me! | ||
| </button> | ||
| <button onClick={() => console.log('Secondary button')} className="secondary-button"> | ||
| Secondary Action | ||
| </button> | ||
| </button> */} | ||
|
Comment on lines
+15
to
+20
|
||
| </div> | ||
|
|
||
| <div className="button-container"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import * as fs from 'fs'; | ||
|
|
||
| export interface AIAnalysisResult { | ||
| analysis: string; | ||
| provider: string; | ||
| model: string; | ||
| } | ||
|
|
||
| function detectProvider(model: string): 'anthropic' | 'openai' { | ||
| return model.toLowerCase().startsWith('claude') ? 'anthropic' : 'openai'; | ||
| } | ||
|
|
||
| async function callAnthropicAPI(prompt: string, token: string, model: string): Promise<string> { | ||
| const response = await fetch('https://api.anthropic.com/v1/messages', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'x-api-key': token, | ||
| 'anthropic-version': '2023-06-01', | ||
| }, | ||
| body: JSON.stringify({ | ||
| model, | ||
| max_tokens: 2048, | ||
| messages: [{ role: 'user', content: prompt }], | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const error = await response.text(); | ||
| throw new Error(`Anthropic API error ${response.status}: ${error}`); | ||
| } | ||
|
|
||
| const data = await response.json() as any; | ||
| return data.content[0].text as string; | ||
| } | ||
|
Comment on lines
+37
to
+39
|
||
|
|
||
| async function callOpenAIAPI(prompt: string, token: string, model: string): Promise<string> { | ||
| const response = await fetch('https://api.openai.com/v1/chat/completions', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${token}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| model, | ||
| max_tokens: 2048, | ||
| messages: [{ role: 'user', content: prompt }], | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const error = await response.text(); | ||
| throw new Error(`OpenAI API error ${response.status}: ${error}`); | ||
| } | ||
|
|
||
| const data = await response.json() as any; | ||
| return data.choices[0].message.content as string; | ||
| } | ||
|
Comment on lines
+60
to
+62
|
||
|
|
||
| function buildPrompt(diffData: unknown): string { | ||
| // Truncate large diff data to avoid token limits (~50k chars) | ||
| const MAX_CHARS = 50000; | ||
| let diffStr = JSON.stringify(diffData, null, 2); | ||
| if (diffStr.length > MAX_CHARS) { | ||
| diffStr = diffStr.substring(0, MAX_CHARS) + '\n... (truncated due to size)'; | ||
| } | ||
|
|
||
| return `You are a frontend performance expert analyzing a JavaScript bundle size diff report generated by Rsdoctor (a Webpack/Rspack bundle analyzer). | ||
|
|
||
| Please analyze the following bundle diff JSON data and provide a concise report covering: | ||
|
|
||
| 1. **Size Regression Summary**: Which assets/chunks increased significantly in size | ||
| 2. **Root Cause Analysis**: Likely causes of size increases based on the diff data | ||
| 3. **Risk Assessment**: Overall severity — Low / Medium / High — with a brief justification | ||
| 4. **Optimization Recommendations**: Specific, actionable steps to reduce the regressions | ||
|
|
||
| Focus especially on: | ||
| - Assets or chunks with >5% or >10 KB size increase | ||
| - Newly added large assets or modules | ||
| - Changes to initial/entry chunks (highest priority) | ||
| - Potential duplicate dependencies | ||
|
|
||
| Bundle diff data: | ||
| \`\`\`json | ||
| ${diffStr} | ||
| \`\`\` | ||
|
|
||
| Respond in concise GitHub-flavored Markdown suitable for a PR comment. If there are no regressions, say so clearly.`; | ||
| } | ||
|
|
||
| /** | ||
| * Run AI degradation analysis on a bundle-diff JSON file. | ||
| * | ||
| * @param diffJsonPath Path to the JSON file produced by `rsdoctor bundle-diff --json` | ||
| * @param token AI API key (Anthropic or OpenAI) | ||
| * @param model Model name — auto-detects provider from prefix (default: claude-3-5-haiku-latest) | ||
| */ | ||
| export async function analyzeWithAI( | ||
| diffJsonPath: string, | ||
| token: string, | ||
| model = 'claude-3-5-haiku-latest', | ||
| ): Promise<AIAnalysisResult | null> { | ||
| if (!token) { | ||
| console.log('ℹ️ No AI token provided, skipping AI analysis'); | ||
| return null; | ||
| } | ||
|
Comment on lines
+103
to
+112
|
||
|
|
||
| if (!fs.existsSync(diffJsonPath)) { | ||
| console.log(`⚠️ Bundle diff JSON not found at ${diffJsonPath}, skipping AI analysis`); | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| const diffData: unknown = JSON.parse(fs.readFileSync(diffJsonPath, 'utf8')); | ||
| const prompt = buildPrompt(diffData); | ||
| const provider = detectProvider(model); | ||
|
|
||
| console.log(`🤖 Running AI analysis with ${provider} (${model})...`); | ||
|
|
||
| const analysis = | ||
| provider === 'anthropic' | ||
| ? await callAnthropicAPI(prompt, token, model) | ||
| : await callOpenAIAPI(prompt, token, model); | ||
|
|
||
| console.log('✅ AI analysis completed'); | ||
| return { analysis, provider, model }; | ||
| } catch (error) { | ||
| console.warn(`⚠️ AI analysis failed: ${error}`); | ||
| return null; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ai_modelis configurable, but enabling the feature requires settingAI_TOKENvia environment variables; that requirement isn’t discoverable fromaction.yml. Consider adding an explicitai_tokeninput (intended forsecrets.*) or at least documenting in the input description that an environment variable is required and that bundle diff JSON will be sent to a third-party AI provider when enabled.