|
| 1 | +import { CODEX_BACKEND, CODEX_DEFAULT_STAGE_MODELS } from "adapters/codex"; |
| 2 | +import { DEFAULT_REASONING_EFFORTS } from "devos/features/onboard/constants"; |
| 3 | +import { |
| 4 | + loadInstanceConfig, |
| 5 | + saveInstanceConfig, |
| 6 | +} from "devos/features/onboard/instance-config"; |
| 7 | +import type { OnboardInstanceConfig } from "devos/features/onboard/types/instance-config.types"; |
| 8 | +import type { AgentUpdatePayload } from "../routes/types/entity-crud.types"; |
| 9 | +import { |
| 10 | + applySettingsModelStageUpdate, |
| 11 | + findSettingsModelStage, |
| 12 | + pruneEmptyCodexSettings, |
| 13 | +} from "../settings/settings-models-config"; |
| 14 | +import type { |
| 15 | + SettingsModelConfigKey, |
| 16 | + SettingsModelStageId, |
| 17 | + SettingsModelStageUpdate, |
| 18 | +} from "../settings/types/settings-models.types"; |
| 19 | +import type { |
| 20 | + AgentReasoningEffort, |
| 21 | + AgentRecord, |
| 22 | +} from "../types/repositories.types"; |
| 23 | +import type { DefaultAgentDefinition } from "./types/default-agents.types"; |
| 24 | + |
| 25 | +const DEFAULT_AGENT_DETAILS: Record< |
| 26 | + SettingsModelStageId, |
| 27 | + Pick<DefaultAgentDefinition, "description" | "instructions"> |
| 28 | +> = { |
| 29 | + brainstorm: { |
| 30 | + description: "Explores task context and shapes the first workflow plan.", |
| 31 | + instructions: "Clarify intent, constraints, and success criteria.", |
| 32 | + }, |
| 33 | + plan: { |
| 34 | + description: "Turns scoped work into an implementation plan.", |
| 35 | + instructions: "Produce a concise plan with verification steps.", |
| 36 | + }, |
| 37 | + implement: { |
| 38 | + description: "Applies code changes for approved workflow tasks.", |
| 39 | + instructions: "Make focused edits and keep package boundaries intact.", |
| 40 | + }, |
| 41 | + testing: { |
| 42 | + description: "Reviews and validates the completed implementation.", |
| 43 | + instructions: "Run relevant checks and report evidence before completion.", |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +const CONFIG_AGENT_UPDATE_FIELDS = new Set(["model", "reasoningEffort"]); |
| 48 | + |
| 49 | +export class DefaultAgentsError extends Error { |
| 50 | + constructor( |
| 51 | + readonly status: number, |
| 52 | + message: string, |
| 53 | + ) { |
| 54 | + super(message); |
| 55 | + this.name = "DefaultAgentsError"; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export async function listDefaultAgents( |
| 60 | + workspacePath: string, |
| 61 | +): Promise<AgentRecord[]> { |
| 62 | + const config = await readInstanceConfig(workspacePath); |
| 63 | + return defaultAgentDefinitions().map((definition) => |
| 64 | + renderDefaultAgent(definition, config), |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +export async function getDefaultAgent( |
| 69 | + workspacePath: string, |
| 70 | + id: string, |
| 71 | +): Promise<AgentRecord | null> { |
| 72 | + const definition = findDefaultAgentDefinition(id); |
| 73 | + if (!definition) return null; |
| 74 | + return renderDefaultAgent( |
| 75 | + definition, |
| 76 | + await readInstanceConfig(workspacePath), |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export async function updateDefaultAgent( |
| 81 | + workspacePath: string, |
| 82 | + id: string, |
| 83 | + input: AgentUpdatePayload, |
| 84 | +): Promise<AgentRecord | null> { |
| 85 | + const definition = findDefaultAgentDefinition(id); |
| 86 | + if (!definition) return null; |
| 87 | + assertConfigAgentUpdate(input); |
| 88 | + const update = toSettingsUpdate(definition.id, input); |
| 89 | + const config = await readInstanceConfig(workspacePath); |
| 90 | + applySettingsModelStageUpdate(config, update, definition.configKey); |
| 91 | + pruneEmptyCodexSettings(config); |
| 92 | + await saveInstanceConfig(config); |
| 93 | + return renderDefaultAgent(definition, config); |
| 94 | +} |
| 95 | + |
| 96 | +async function readInstanceConfig( |
| 97 | + workspacePath: string, |
| 98 | +): Promise<OnboardInstanceConfig> { |
| 99 | + const result = await loadInstanceConfig(workspacePath); |
| 100 | + if (!result.ok) { |
| 101 | + throw new DefaultAgentsError(404, result.message); |
| 102 | + } |
| 103 | + return result.config; |
| 104 | +} |
| 105 | + |
| 106 | +function defaultAgentDefinitions(): DefaultAgentDefinition[] { |
| 107 | + return ["brainstorm", "plan", "implement", "testing"].map((id) => { |
| 108 | + const stage = findSettingsModelStage(id); |
| 109 | + if (!stage) { |
| 110 | + throw new DefaultAgentsError(500, `Default agent '${id}' is not mapped`); |
| 111 | + } |
| 112 | + return { |
| 113 | + ...stage, |
| 114 | + ...DEFAULT_AGENT_DETAILS[stage.id], |
| 115 | + }; |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +function findDefaultAgentDefinition( |
| 120 | + id: string, |
| 121 | +): DefaultAgentDefinition | undefined { |
| 122 | + return defaultAgentDefinitions().find((definition) => definition.id === id); |
| 123 | +} |
| 124 | + |
| 125 | +function renderDefaultAgent( |
| 126 | + definition: DefaultAgentDefinition, |
| 127 | + config: OnboardInstanceConfig, |
| 128 | +): AgentRecord { |
| 129 | + const updatedAt = config.$meta.updatedAt; |
| 130 | + return { |
| 131 | + id: definition.id, |
| 132 | + name: definition.label, |
| 133 | + description: definition.description, |
| 134 | + logo: "", |
| 135 | + runtime: CODEX_BACKEND, |
| 136 | + backend: CODEX_BACKEND, |
| 137 | + model: modelForStage(definition.configKey, config), |
| 138 | + reasoningEffort: reasoningForStage(definition.configKey, config), |
| 139 | + status: "online", |
| 140 | + concurrency: 1, |
| 141 | + owner: config.workspace.id, |
| 142 | + createdAt: updatedAt, |
| 143 | + updatedAt, |
| 144 | + skills: [], |
| 145 | + recentWork: [], |
| 146 | + activity: [], |
| 147 | + instructions: definition.instructions, |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +function modelForStage( |
| 152 | + configKey: SettingsModelConfigKey, |
| 153 | + config: OnboardInstanceConfig, |
| 154 | +): string { |
| 155 | + return ( |
| 156 | + config.codex?.models?.[configKey] ?? |
| 157 | + CODEX_DEFAULT_STAGE_MODELS[configKey] ?? |
| 158 | + "gpt-5.5" |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +function reasoningForStage( |
| 163 | + configKey: SettingsModelConfigKey, |
| 164 | + config: OnboardInstanceConfig, |
| 165 | +): AgentReasoningEffort { |
| 166 | + return ( |
| 167 | + config.codex?.reasoningEfforts?.[configKey] ?? |
| 168 | + DEFAULT_REASONING_EFFORTS[configKey] |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +function assertConfigAgentUpdate(input: AgentUpdatePayload): void { |
| 173 | + const unsupportedFields = Object.keys(input).filter( |
| 174 | + (field) => !CONFIG_AGENT_UPDATE_FIELDS.has(field), |
| 175 | + ); |
| 176 | + if (unsupportedFields.length > 0) { |
| 177 | + throw new DefaultAgentsError( |
| 178 | + 400, |
| 179 | + "Config-backed agents only support model and reasoningEffort updates", |
| 180 | + ); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +function toSettingsUpdate( |
| 185 | + id: SettingsModelStageId, |
| 186 | + input: AgentUpdatePayload, |
| 187 | +): SettingsModelStageUpdate { |
| 188 | + return { |
| 189 | + id, |
| 190 | + ...(input.model !== undefined ? { model: input.model } : {}), |
| 191 | + ...(input.reasoningEffort !== undefined |
| 192 | + ? { reasoningEffort: input.reasoningEffort } |
| 193 | + : {}), |
| 194 | + }; |
| 195 | +} |
0 commit comments