Files
opencode-manager/backend/src/services/git/commit-message-prompt.ts
T
Chris Scott b36d412ca2 feat: add multi-run fusion and session change walkthroughs
Fuse two to five completed multi-run entries into one synthesized session,
and explain a session's diff as ordered walkthrough stops cached by diff hash.

- Multi-run fusion service, persistence, routes and FuseRunDialog
- Change walkthrough service, persistence, routes and ChangesWalkthroughDialog
- Shared unified-diff parser plus bounded text generation, json-extract,
  text-truncate and service-error helpers
2026-10-06 15:27:14 -04:00

47 lines
1.6 KiB
TypeScript

import { truncateText } from '../../utils/text-truncate'
export const MAX_COMMIT_PROMPT_DIFF_CHARS = 60_000
const COMMIT_PROMPT_DIFF_TRUNCATION_MARKER = '\n[diff truncated]'
export interface CommitMessageContext {
stagedStat: string
stagedDiff: string
recentSubjects: string[]
}
export function buildCommitMessagePrompt({ stagedStat, stagedDiff, recentSubjects }: CommitMessageContext): string {
const subjects = recentSubjects.length > 0
? recentSubjects.map(subject => `- ${subject}`).join('\n')
: '(no recent commits)'
return [
'Write a single commit message for the staged changes below.',
'Match the style of the recent commit subjects when they establish a convention, such as a conventional-commit prefix.',
'Use a subject line of 72 characters or fewer, followed by an optional short body separated by a blank line.',
'Return only the commit message text. Do not wrap it in code fences and do not prefix it with a label.',
'',
'Recent commit subjects:',
subjects,
'',
'Staged changes (stat):',
stagedStat.trim(),
'',
'Staged diff:',
truncateText(stagedDiff, MAX_COMMIT_PROMPT_DIFF_CHARS, COMMIT_PROMPT_DIFF_TRUNCATION_MARKER).text,
].join('\n')
}
const COMMIT_MESSAGE_LABEL = /^(?:commit message|message)\s*:\s*/i
export function normalizeGeneratedCommitMessage(text: string): string {
let normalized = text.trim().replace(COMMIT_MESSAGE_LABEL, '')
const fenced = normalized.match(/^```[^\n]*\n?([\s\S]*?)\n?```$/)
if (fenced) {
normalized = (fenced[1] ?? '').trim()
}
return normalized.replace(COMMIT_MESSAGE_LABEL, '').trim()
}