Files
orca/src/shared/source-control-ai-recipe-save.ts
T
JinjingandOrca 006a4ef3a5 Customizable Source Control AI action recipes with per-action agent and command templates (#4868)
* WIP: Changes before auto-review fixes

* Customize Source Control AI action recipes

- Add per-action CLI arguments for generation and launch flows so saved
  recipes can select model flags without putting prompts in argv.
- Share the text-generation dialog between commit messages and hosted-review
  details, with first-run defaults and repo/global recipe support.
- Launch fix-check agents directly from saved recipes and harden prompt/template
  handling for invalid args, blank prompts, and inherited variables.

* Allow custom commands for source control action recipes

- Let text action recipes save and resolve the custom-command sentinel
- Add settings UI for custom command recipes and preserve per-action defaults
- Split large source-control dialogs and direct launch helpers into focused modules
- Keep launch actions from treating custom text agents as runnable TUI agents

* Add per-repo Source Control AI enablement, custom command, and save-targ

- Repo overrides now support `enabled` and `customAgentCommand`, letting
  repositories opt in/out of Source Control AI independently and supply a
  repo-scoped custom command that takes precedence over the global one.
- Recipe-save dialogs gained a save-target selector ("Don't save / Save for
  this repo / Save as global default") replacing the old boolean checkbox,
  routing saves through the new `saveSourceControlActionRecipe` helper in
  `source-control-ai-recipe-save.ts`.
- `normalizeRepoSourceControlAiOverrides` now returns `undefined` for empty
  objects and passes the `null` sentinel through the IPC/RPC layer so the
  persistence layer can clear repo overrides cleanly.
- `resolveSourceControlLaunchPlatform` resolves the correct shell platform
  for SSH and WSL worktrees so agent launch commands are built correctly.
- Settings UI gained `RepositorySourceControlAiEnablement` and
  `RepositorySourceControlAiCustomCommand` rows; draft/label logic was
  extracted into focused modules to stay within lint line limits.

* Extract action recipe defaults into own component and use id-prefixed wo

- Move action recipe draft state and UI out of CommitMessageAiPane into SourceControlAiActionRecipeDefaults and source-control-ai-action-recipe-draft.ts to respect the max-lines lint rule
- Use toRuntimeWorktreeSelector() across all runtime git RPC calls so the runtime can resolve worktrees by ID rather than path
- Fix SSH launch platform resolution to use the repo's connection when the newly created worktree isn't hydrated yet
- Add edit and delete handlers for PR conversation comments with confirmation dialog
- Use text-status-success design token instead of hardcoded text-emerald-500

* add more search keyword

* Rename "Enable Source Control AI defaults" to "Show Source Control AI ac

* fix test

* Remove unused imports and variable assignment in launch-work-item-direct

* Fix test mocks to use `mocks.store` instead of `storeState.value` for di

* Extract Source Control AI logic into focused modules with fix-checks dia

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-08 14:08:35 -07:00

223 lines
7.4 KiB
TypeScript

import {
normalizeRepoSourceControlAiOverrides,
normalizeSourceControlAiSettings
} from './source-control-ai'
import {
DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES,
SOURCE_CONTROL_ACTION_IDS,
SOURCE_CONTROL_TEXT_ACTION_IDS,
type SourceControlActionId,
type SourceControlActionRecipe
} from './source-control-ai-actions'
import type {
CompleteSourceControlActionRecipe,
RepoSourceControlAiOverrides,
SourceControlAiOperation,
SourceControlAiSettings,
WritableRepoSourceControlAiOverrides
} from './source-control-ai-types'
import type { GlobalSettings, Repo } from './types'
export type SourceControlAiWriteTarget = { type: 'repo'; repoId: string } | { type: 'global' }
export type SourceControlAiRepoUpdate =
| { sourceControlAi: WritableRepoSourceControlAiOverrides }
| { sourceControlAi: null }
export type SourceControlActionRecipeSaveResult =
| { target: { type: 'global' }; sourceControlAi: SourceControlAiSettings }
| { target: { type: 'repo'; repoId: string }; update: SourceControlAiRepoUpdate }
type SaveSourceControlActionRecipeInput = {
target: SourceControlAiWriteTarget
settings: Pick<GlobalSettings, 'sourceControlAi' | 'commitMessageAi'>
repo?: Pick<Repo, 'sourceControlAi'> | null
actionId: SourceControlActionId
recipe: SourceControlActionRecipe
customAgentCommand?: string
}
type ReadCompatibleActionRecipe = {
agentId?: SourceControlActionRecipe['agentId']
commandInputTemplate?: string | null
agentArgs?: string | null
}
const TEXT_ACTION_ID_SET = new Set<SourceControlActionId>(SOURCE_CONTROL_TEXT_ACTION_IDS)
function hasEntries(value: Record<string, unknown> | null | undefined): boolean {
return Object.keys(value ?? {}).length > 0
}
function normalizeStringRecord<T extends string>(
value: Partial<Record<T, string | null | undefined>> | undefined
): Partial<Record<T, string>> | undefined {
const normalized: Partial<Record<T, string>> = {}
for (const [key, item] of Object.entries(value ?? {}) as [T, string | null | undefined][]) {
if (typeof item === 'string') {
normalized[key] = item
}
}
return hasEntries(normalized) ? normalized : undefined
}
function normalizeBooleanRecord<T extends string>(
value: Partial<Record<T, boolean | null | undefined>> | undefined
): Partial<Record<T, boolean>> | undefined {
const normalized: Partial<Record<T, boolean>> = {}
for (const [key, item] of Object.entries(value ?? {}) as [T, boolean | null | undefined][]) {
if (typeof item === 'boolean') {
normalized[key] = item
}
}
return hasEntries(normalized) ? normalized : undefined
}
function normalizeCompleteRecipe(
actionId: SourceControlActionId,
recipe: ReadCompatibleActionRecipe | undefined
): CompleteSourceControlActionRecipe | undefined {
if (!recipe) {
return undefined
}
const commandInputTemplate =
typeof recipe.commandInputTemplate === 'string'
? recipe.commandInputTemplate
: DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES[actionId]
const rawAgentArgs = recipe.agentArgs
const agentArgs = typeof rawAgentArgs === 'string' ? rawAgentArgs.trim() : undefined
return {
agentId: recipe.agentId ?? null,
commandInputTemplate,
...(agentArgs !== undefined ? { agentArgs } : {})
}
}
function normalizeActionOverrides(
overrides: RepoSourceControlAiOverrides['actionOverrides']
): WritableRepoSourceControlAiOverrides['actionOverrides'] {
const normalized: WritableRepoSourceControlAiOverrides['actionOverrides'] = {}
for (const actionId of SOURCE_CONTROL_ACTION_IDS) {
const recipe = normalizeCompleteRecipe(actionId, overrides?.[actionId])
if (recipe) {
normalized[actionId] = recipe
}
}
return hasEntries(normalized) ? normalized : undefined
}
export function normalizeWritableRepoSourceControlAiOverrides(
value: RepoSourceControlAiOverrides | null | undefined
): WritableRepoSourceControlAiOverrides | undefined {
const readCompatible = normalizeRepoSourceControlAiOverrides(value)
if (!readCompatible) {
return undefined
}
const writable: WritableRepoSourceControlAiOverrides = {}
if (typeof readCompatible.enabled === 'boolean') {
writable.enabled = readCompatible.enabled
}
if (typeof readCompatible.customAgentCommand === 'string') {
const customAgentCommand = readCompatible.customAgentCommand.trim()
if (customAgentCommand) {
writable.customAgentCommand = customAgentCommand
}
}
if (readCompatible.modelOverridesByOperation) {
writable.modelOverridesByOperation = readCompatible.modelOverridesByOperation
}
const instructionsByOperation = normalizeStringRecord(readCompatible.instructionsByOperation)
if (instructionsByOperation) {
writable.instructionsByOperation = instructionsByOperation
}
const actionOverrides = normalizeActionOverrides(readCompatible.actionOverrides)
if (actionOverrides) {
writable.actionOverrides = actionOverrides
}
const prCreationDefaults = normalizeBooleanRecord(readCompatible.prCreationDefaults)
if (prCreationDefaults) {
writable.prCreationDefaults = prCreationDefaults
}
return Object.keys(writable).length > 0 ? writable : undefined
}
export function toSourceControlAiRepoUpdate(
value: RepoSourceControlAiOverrides | null | undefined
): SourceControlAiRepoUpdate {
const sourceControlAi = normalizeWritableRepoSourceControlAiOverrides(value)
return sourceControlAi ? { sourceControlAi } : { sourceControlAi: null }
}
function dropLegacyInstructionForAction(
value: WritableRepoSourceControlAiOverrides,
actionId: SourceControlActionId
): WritableRepoSourceControlAiOverrides {
if (!TEXT_ACTION_ID_SET.has(actionId) || !value.instructionsByOperation) {
return value
}
const instructionsByOperation = { ...value.instructionsByOperation }
delete instructionsByOperation[actionId as SourceControlAiOperation]
return {
...value,
instructionsByOperation: hasEntries(instructionsByOperation)
? instructionsByOperation
: undefined
}
}
function normalizeRecipeForSave(
actionId: SourceControlActionId,
recipe: SourceControlActionRecipe
): CompleteSourceControlActionRecipe {
return (
normalizeCompleteRecipe(actionId, recipe) ?? {
agentId: null,
commandInputTemplate: DEFAULT_SOURCE_CONTROL_ACTION_COMMAND_TEMPLATES[actionId]
}
)
}
export function saveSourceControlActionRecipe(
input: SaveSourceControlActionRecipeInput
): SourceControlActionRecipeSaveResult {
const savedRecipe = normalizeRecipeForSave(input.actionId, input.recipe)
if (input.target.type === 'global') {
const current = normalizeSourceControlAiSettings(
input.settings.sourceControlAi,
input.settings.commitMessageAi
)
return {
target: { type: 'global' },
sourceControlAi: {
...current,
...(typeof input.customAgentCommand === 'string'
? { customAgentCommand: input.customAgentCommand }
: {}),
actions: {
...current.actions,
[input.actionId]: savedRecipe
}
}
}
}
const currentRepoAi = normalizeWritableRepoSourceControlAiOverrides(input.repo?.sourceControlAi)
const next = dropLegacyInstructionForAction(
{
...currentRepoAi,
...(typeof input.customAgentCommand === 'string'
? { customAgentCommand: input.customAgentCommand }
: {}),
actionOverrides: {
...currentRepoAi?.actionOverrides,
[input.actionId]: savedRecipe
}
},
input.actionId
)
return {
target: input.target,
update: toSourceControlAiRepoUpdate(next)
}
}