feat(ai): add streaming SSE support and new prompt templates
- Stream AI responses token-by-token via SSE for instant feedback - Add EXPLAIN, REVIEW, CHANGELOG, SUGGEST system prompts - Add buildExplainPrompt, buildReviewPrompt, buildChangelogPrompt, buildSuggestBranchPrompt, buildSuggestTypePrompt functions - Review prompt supports strict/lenient/normal modes
This commit is contained in:
+149
-37
@@ -1,5 +1,7 @@
|
||||
import type { PRContext, ProjectContext } from "./types";
|
||||
|
||||
// ── Commit System Prompt ──────────────────────────────────────────────
|
||||
|
||||
export const SYSTEM_PROMPT = `You are an expert at writing concise, meaningful git commit messages following the Conventional Commits specification.
|
||||
|
||||
Format: <type>(<scope>): <description>
|
||||
@@ -19,29 +21,17 @@ Rules:
|
||||
export function buildPrompt(context: ProjectContext): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (
|
||||
context.packageDescription ||
|
||||
context.readme ||
|
||||
context.structure
|
||||
) {
|
||||
if (context.packageDescription || context.readme || context.structure) {
|
||||
parts.push("## Project Context");
|
||||
if (context.packageDescription) {
|
||||
parts.push(`Description: ${context.packageDescription}`);
|
||||
}
|
||||
if (context.structure) {
|
||||
parts.push(`Structure: ${context.structure}`);
|
||||
}
|
||||
if (context.readme) {
|
||||
parts.push(`README:\n${context.readme}`);
|
||||
}
|
||||
if (context.packageDescription) parts.push(`Description: ${context.packageDescription}`);
|
||||
if (context.structure) parts.push(`Structure: ${context.structure}`);
|
||||
if (context.readme) parts.push(`README:\n${context.readme}`);
|
||||
parts.push("");
|
||||
}
|
||||
|
||||
if (context.recentCommits.length > 0) {
|
||||
parts.push("## Recent Commits (for style reference)");
|
||||
for (const c of context.recentCommits) {
|
||||
parts.push(c);
|
||||
}
|
||||
for (const c of context.recentCommits) parts.push(c);
|
||||
parts.push("");
|
||||
}
|
||||
|
||||
@@ -55,6 +45,8 @@ export function buildPrompt(context: ProjectContext): string {
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// ── PR System Prompt ───────────────────────────────────────────────────
|
||||
|
||||
export const PR_SYSTEM_PROMPT = `You are an expert at writing clear, concise pull request titles and descriptions.
|
||||
|
||||
Format:
|
||||
@@ -74,21 +66,11 @@ Rules:
|
||||
export function buildPRPrompt(context: PRContext): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (
|
||||
context.packageDescription ||
|
||||
context.readme ||
|
||||
context.structure
|
||||
) {
|
||||
if (context.packageDescription || context.readme || context.structure) {
|
||||
parts.push("## Project Context");
|
||||
if (context.packageDescription) {
|
||||
parts.push(`Description: ${context.packageDescription}`);
|
||||
}
|
||||
if (context.structure) {
|
||||
parts.push(`Structure: ${context.structure}`);
|
||||
}
|
||||
if (context.readme) {
|
||||
parts.push(`README:\n${context.readme}`);
|
||||
}
|
||||
if (context.packageDescription) parts.push(`Description: ${context.packageDescription}`);
|
||||
if (context.structure) parts.push(`Structure: ${context.structure}`);
|
||||
if (context.readme) parts.push(`README:\n${context.readme}`);
|
||||
parts.push("");
|
||||
}
|
||||
|
||||
@@ -99,9 +81,7 @@ export function buildPRPrompt(context: PRContext): string {
|
||||
|
||||
if (context.branchCommits.length > 0) {
|
||||
parts.push("## Commits on This Branch");
|
||||
for (const c of context.branchCommits) {
|
||||
parts.push(c);
|
||||
}
|
||||
for (const c of context.branchCommits) parts.push(c);
|
||||
parts.push("");
|
||||
}
|
||||
|
||||
@@ -110,9 +90,141 @@ export function buildPRPrompt(context: PRContext): string {
|
||||
parts.push(context.diff);
|
||||
parts.push("```");
|
||||
parts.push("");
|
||||
parts.push(
|
||||
"Generate a pull request title and brief body for the above changes.",
|
||||
);
|
||||
parts.push("Generate a pull request title and brief body for the above changes.");
|
||||
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// ── Explain Prompt ─────────────────────────────────────────────────────
|
||||
|
||||
export const EXPLAIN_SYSTEM_PROMPT = `You are an expert software engineer explaining code changes in plain, accessible language.
|
||||
|
||||
Given a git diff, explain:
|
||||
1. WHAT changed at a high level (1 sentence summary)
|
||||
2. WHY these changes matter (what problem they solve or what they enable)
|
||||
3. A brief breakdown of the key changes (bullet points, one per file/module)
|
||||
|
||||
Rules:
|
||||
- Be concise but thorough
|
||||
- Use plain language suitable for both junior and senior engineers
|
||||
- Focus on the intent and impact, not just restating the diff
|
||||
- Do NOT use markdown headings (no ##, ###). Use bold text markers like **Section:** instead.
|
||||
- Keep each bullet point to 1-2 lines
|
||||
- If the diff is trivial, say so and keep the explanation short`;
|
||||
|
||||
export function buildExplainPrompt(diff: string): string {
|
||||
const parts: string[] = [];
|
||||
parts.push("## Changes to Explain");
|
||||
parts.push("```diff");
|
||||
parts.push(diff);
|
||||
parts.push("```");
|
||||
parts.push("");
|
||||
parts.push("Explain these changes in plain language as described.");
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// ── Review Prompt ──────────────────────────────────────────────────────
|
||||
|
||||
export const REVIEW_SYSTEM_PROMPT = `You are a senior software engineer performing a thorough but friendly code review.
|
||||
|
||||
Review the following code changes and provide feedback in these categories:
|
||||
1. **Bugs & Logic Errors** — actual bugs, off-by-one errors, null safety, edge cases
|
||||
2. **Code Quality** — readability, naming, duplication, complexity
|
||||
3. **Performance** — inefficient patterns, unnecessary allocations, N+1 queries
|
||||
4. **Security** — injection risks, exposed secrets, unsafe operations
|
||||
5. **Suggestions** — concrete improvements with code snippets where helpful
|
||||
|
||||
Rules:
|
||||
- Be constructive, not harsh. Use "consider" and "suggest" instead of "you should".
|
||||
- Prioritize by severity. Mention critical issues first.
|
||||
- If the code looks great, say so! Don't fabricate issues.
|
||||
- Keep feedback actionable — every issue should have a clear suggestion.
|
||||
- Use **bold** for section headers and \`code\` for code references.
|
||||
- Do NOT output a concluding summary paragraph. End with the last suggestion.`;
|
||||
|
||||
export function buildReviewPrompt(diff: string, strictness: "lenient" | "normal" | "strict"): string {
|
||||
const strictnessHints: Record<string, string> = {
|
||||
lenient: "Focus only on major issues. Skip minor style nits.",
|
||||
normal: "Provide balanced feedback covering all categories.",
|
||||
strict: "Be thorough. Flag even minor issues and style inconsistencies.",
|
||||
};
|
||||
|
||||
const parts: string[] = [];
|
||||
parts.push(`Review strictness: ${strictnessHints[strictness]}`);
|
||||
parts.push("");
|
||||
parts.push("## Code Changes to Review");
|
||||
parts.push("```diff");
|
||||
parts.push(diff);
|
||||
parts.push("```");
|
||||
parts.push("");
|
||||
parts.push("Please review the above changes.");
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// ── Changelog Prompt ───────────────────────────────────────────────────
|
||||
|
||||
export const CHANGELOG_SYSTEM_PROMPT = `You are an expert at writing clear, user-facing changelogs from git commit history.
|
||||
|
||||
Given a list of commits, generate a changelog organized by type:
|
||||
- **Features** (feat commits)
|
||||
- **Bug Fixes** (fix commits)
|
||||
- **Improvements** (refactor, perf, style commits)
|
||||
- **Documentation** (docs commits)
|
||||
- **Chores & Maintenance** (chore, build, ci, test commits)
|
||||
|
||||
Rules:
|
||||
- Group by type, with the heading in **bold**
|
||||
- Each entry should be a single line describing the change in user-friendly language
|
||||
- Translate technical commit messages into language a user would understand
|
||||
- Skip merge commits and trivial chore commits if they don't add value
|
||||
- If a type has no entries, omit that section
|
||||
- Output ONLY the changelog text, no preamble or markdown code blocks`;
|
||||
|
||||
export function buildChangelogPrompt(commits: string[], from?: string, to?: string): string {
|
||||
const parts: string[] = [];
|
||||
const range = from ? `from ${from}${to ? ` to ${to}` : " to HEAD"}` : "";
|
||||
parts.push(range ? `Generate a changelog for commits ${range}.` : "Generate a changelog from the following commits.");
|
||||
parts.push("");
|
||||
parts.push("## Commits");
|
||||
for (const c of commits) parts.push(c);
|
||||
parts.push("");
|
||||
parts.push("Generate a changelog from these commits.");
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// ── Suggest Prompt ─────────────────────────────────────────────────────
|
||||
|
||||
export const SUGGEST_SYSTEM_PROMPT = `You are an expert at suggesting git branch names and commit types based on code changes.
|
||||
|
||||
For branch name suggestions:
|
||||
- Use format: <type>/<short-description>
|
||||
- Types: feat, fix, refactor, docs, chore, perf, test
|
||||
- Description should be 2-4 hyphenated words
|
||||
- Provide exactly 3 suggestions, one per line
|
||||
|
||||
For commit type suggestions:
|
||||
- Return exactly one Conventional Commit type that best matches the changes
|
||||
- Valid types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
|
||||
- Output ONLY the type name`;
|
||||
|
||||
export function buildSuggestBranchPrompt(diff: string): string {
|
||||
const parts: string[] = [];
|
||||
parts.push("## Changes");
|
||||
parts.push("```diff");
|
||||
parts.push(diff);
|
||||
parts.push("```");
|
||||
parts.push("");
|
||||
parts.push("Suggest 3 branch names for these changes. Output one per line, no numbering.");
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
export function buildSuggestTypePrompt(diff: string): string {
|
||||
const parts: string[] = [];
|
||||
parts.push("## Changes");
|
||||
parts.push("```diff");
|
||||
parts.push(diff);
|
||||
parts.push("```");
|
||||
parts.push("");
|
||||
parts.push("What Conventional Commit type best describes these changes? Output ONLY the type name.");
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user