|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { useAPI } from "@/browser/contexts/API"; |
| 3 | +import { Input } from "@/browser/components/ui/input"; |
| 4 | +import { |
| 5 | + DEFAULT_TASK_SETTINGS, |
| 6 | + TASK_SETTINGS_LIMITS, |
| 7 | + normalizeTaskSettings, |
| 8 | + type TaskSettings, |
| 9 | + type SubagentAiDefaults, |
| 10 | +} from "@/common/types/tasks"; |
| 11 | +import { BUILT_IN_SUBAGENTS } from "@/common/constants/agents"; |
| 12 | +import type { ThinkingLevel } from "@/common/types/thinking"; |
| 13 | +import { useModelsFromSettings } from "@/browser/hooks/useModelsFromSettings"; |
| 14 | +import { |
| 15 | + Select, |
| 16 | + SelectContent, |
| 17 | + SelectItem, |
| 18 | + SelectTrigger, |
| 19 | + SelectValue, |
| 20 | +} from "@/browser/components/ui/select"; |
| 21 | + |
| 22 | +export function TasksSection() { |
| 23 | + const { api } = useAPI(); |
| 24 | + const [taskSettings, setTaskSettings] = useState<TaskSettings>(DEFAULT_TASK_SETTINGS); |
| 25 | + const [subagentAiDefaults, setSubagentAiDefaults] = useState<SubagentAiDefaults>({}); |
| 26 | + const [loaded, setLoaded] = useState(false); |
| 27 | + const [loadFailed, setLoadFailed] = useState(false); |
| 28 | + const [saveError, setSaveError] = useState<string | null>(null); |
| 29 | + const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 30 | + const savingRef = useRef(false); |
| 31 | + |
| 32 | + const { models } = useModelsFromSettings(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (!api) return; |
| 36 | + |
| 37 | + setLoaded(false); |
| 38 | + setLoadFailed(false); |
| 39 | + setSaveError(null); |
| 40 | + |
| 41 | + void api.config |
| 42 | + .getConfig() |
| 43 | + .then((cfg) => { |
| 44 | + setTaskSettings(normalizeTaskSettings(cfg.taskSettings)); |
| 45 | + setSubagentAiDefaults(cfg.subagentAiDefaults ?? {}); |
| 46 | + setLoadFailed(false); |
| 47 | + setLoaded(true); |
| 48 | + }) |
| 49 | + .catch((error: unknown) => { |
| 50 | + setSaveError(error instanceof Error ? error.message : String(error)); |
| 51 | + setLoadFailed(true); |
| 52 | + setLoaded(true); |
| 53 | + }); |
| 54 | + }, [api]); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (!api) return; |
| 58 | + if (!loaded) return; |
| 59 | + if (loadFailed) return; |
| 60 | + if (savingRef.current) return; |
| 61 | + |
| 62 | + if (saveTimerRef.current) { |
| 63 | + clearTimeout(saveTimerRef.current); |
| 64 | + saveTimerRef.current = null; |
| 65 | + } |
| 66 | + |
| 67 | + saveTimerRef.current = setTimeout(() => { |
| 68 | + savingRef.current = true; |
| 69 | + void api.config |
| 70 | + .saveConfig({ taskSettings, subagentAiDefaults }) |
| 71 | + .catch((error: unknown) => { |
| 72 | + setSaveError(error instanceof Error ? error.message : String(error)); |
| 73 | + }) |
| 74 | + .finally(() => { |
| 75 | + savingRef.current = false; |
| 76 | + }); |
| 77 | + }, 400); |
| 78 | + |
| 79 | + return () => { |
| 80 | + if (saveTimerRef.current) { |
| 81 | + clearTimeout(saveTimerRef.current); |
| 82 | + saveTimerRef.current = null; |
| 83 | + } |
| 84 | + }; |
| 85 | + }, [api, loaded, loadFailed, subagentAiDefaults, taskSettings]); |
| 86 | + |
| 87 | + const setMaxParallelAgentTasks = (rawValue: string) => { |
| 88 | + const parsed = Number(rawValue); |
| 89 | + setTaskSettings((prev) => normalizeTaskSettings({ ...prev, maxParallelAgentTasks: parsed })); |
| 90 | + }; |
| 91 | + |
| 92 | + const setMaxTaskNestingDepth = (rawValue: string) => { |
| 93 | + const parsed = Number(rawValue); |
| 94 | + setTaskSettings((prev) => normalizeTaskSettings({ ...prev, maxTaskNestingDepth: parsed })); |
| 95 | + }; |
| 96 | + |
| 97 | + const INHERIT = "__inherit__"; |
| 98 | + |
| 99 | + const setSubagentModel = (agentType: string, value: string) => { |
| 100 | + setSubagentAiDefaults((prev) => { |
| 101 | + const next = { ...prev }; |
| 102 | + const existing = next[agentType] ?? {}; |
| 103 | + const updated = { ...existing }; |
| 104 | + |
| 105 | + if (value === INHERIT) { |
| 106 | + delete updated.modelString; |
| 107 | + } else { |
| 108 | + updated.modelString = value; |
| 109 | + } |
| 110 | + |
| 111 | + if (!updated.modelString && !updated.thinkingLevel) { |
| 112 | + delete next[agentType]; |
| 113 | + } else { |
| 114 | + next[agentType] = updated; |
| 115 | + } |
| 116 | + |
| 117 | + return next; |
| 118 | + }); |
| 119 | + }; |
| 120 | + |
| 121 | + const setSubagentThinking = (agentType: string, value: string) => { |
| 122 | + setSubagentAiDefaults((prev) => { |
| 123 | + const next = { ...prev }; |
| 124 | + const existing = next[agentType] ?? {}; |
| 125 | + const updated = { ...existing }; |
| 126 | + |
| 127 | + if (value === INHERIT) { |
| 128 | + delete updated.thinkingLevel; |
| 129 | + } else { |
| 130 | + updated.thinkingLevel = value as ThinkingLevel; |
| 131 | + } |
| 132 | + |
| 133 | + if (!updated.modelString && !updated.thinkingLevel) { |
| 134 | + delete next[agentType]; |
| 135 | + } else { |
| 136 | + next[agentType] = updated; |
| 137 | + } |
| 138 | + |
| 139 | + return next; |
| 140 | + }); |
| 141 | + }; |
| 142 | + |
| 143 | + return ( |
| 144 | + <div className="space-y-6"> |
| 145 | + <div> |
| 146 | + <h3 className="text-foreground mb-4 text-sm font-medium">Agents</h3> |
| 147 | + <div className="space-y-4"> |
| 148 | + <div className="flex items-center justify-between gap-4"> |
| 149 | + <div className="flex-1"> |
| 150 | + <div className="text-foreground text-sm">Max Parallel Agent Tasks</div> |
| 151 | + <div className="text-muted text-xs"> |
| 152 | + Default {TASK_SETTINGS_LIMITS.maxParallelAgentTasks.default}, range{" "} |
| 153 | + {TASK_SETTINGS_LIMITS.maxParallelAgentTasks.min}– |
| 154 | + {TASK_SETTINGS_LIMITS.maxParallelAgentTasks.max} |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + <Input |
| 158 | + type="number" |
| 159 | + value={taskSettings.maxParallelAgentTasks} |
| 160 | + min={TASK_SETTINGS_LIMITS.maxParallelAgentTasks.min} |
| 161 | + max={TASK_SETTINGS_LIMITS.maxParallelAgentTasks.max} |
| 162 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => |
| 163 | + setMaxParallelAgentTasks(e.target.value) |
| 164 | + } |
| 165 | + className="border-border-medium bg-background-secondary h-9 w-28" |
| 166 | + /> |
| 167 | + </div> |
| 168 | + |
| 169 | + <div className="flex items-center justify-between gap-4"> |
| 170 | + <div className="flex-1"> |
| 171 | + <div className="text-foreground text-sm">Max Task Nesting Depth</div> |
| 172 | + <div className="text-muted text-xs"> |
| 173 | + Default {TASK_SETTINGS_LIMITS.maxTaskNestingDepth.default}, range{" "} |
| 174 | + {TASK_SETTINGS_LIMITS.maxTaskNestingDepth.min}– |
| 175 | + {TASK_SETTINGS_LIMITS.maxTaskNestingDepth.max} |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + <Input |
| 179 | + type="number" |
| 180 | + value={taskSettings.maxTaskNestingDepth} |
| 181 | + min={TASK_SETTINGS_LIMITS.maxTaskNestingDepth.min} |
| 182 | + max={TASK_SETTINGS_LIMITS.maxTaskNestingDepth.max} |
| 183 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => |
| 184 | + setMaxTaskNestingDepth(e.target.value) |
| 185 | + } |
| 186 | + className="border-border-medium bg-background-secondary h-9 w-28" |
| 187 | + /> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + |
| 191 | + {saveError && <div className="text-danger-light mt-4 text-xs">{saveError}</div>} |
| 192 | + </div> |
| 193 | + |
| 194 | + <div> |
| 195 | + <h3 className="text-foreground mb-4 text-sm font-medium">Sub-agents</h3> |
| 196 | + <div className="space-y-4"> |
| 197 | + {BUILT_IN_SUBAGENTS.map((preset) => { |
| 198 | + const agentType = preset.agentType; |
| 199 | + const entry = subagentAiDefaults[agentType]; |
| 200 | + const modelValue = entry?.modelString ?? INHERIT; |
| 201 | + const thinkingValue = entry?.thinkingLevel ?? INHERIT; |
| 202 | + |
| 203 | + return ( |
| 204 | + <div |
| 205 | + key={agentType} |
| 206 | + className="border-border-medium bg-background-secondary rounded-md border p-3" |
| 207 | + > |
| 208 | + <div className="text-foreground text-sm font-medium">{preset.label}</div> |
| 209 | + |
| 210 | + <div className="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2"> |
| 211 | + <div className="space-y-1"> |
| 212 | + <div className="text-muted text-xs">Model</div> |
| 213 | + <Select |
| 214 | + value={modelValue} |
| 215 | + onValueChange={(value) => setSubagentModel(agentType, value)} |
| 216 | + > |
| 217 | + <SelectTrigger className="border-border-medium bg-modal-bg h-9"> |
| 218 | + <SelectValue /> |
| 219 | + </SelectTrigger> |
| 220 | + <SelectContent> |
| 221 | + <SelectItem value={INHERIT}>Inherit</SelectItem> |
| 222 | + {models.map((model) => ( |
| 223 | + <SelectItem key={model} value={model}> |
| 224 | + {model} |
| 225 | + </SelectItem> |
| 226 | + ))} |
| 227 | + </SelectContent> |
| 228 | + </Select> |
| 229 | + </div> |
| 230 | + |
| 231 | + <div className="space-y-1"> |
| 232 | + <div className="text-muted text-xs">Reasoning</div> |
| 233 | + <Select |
| 234 | + value={thinkingValue} |
| 235 | + onValueChange={(value) => setSubagentThinking(agentType, value)} |
| 236 | + > |
| 237 | + <SelectTrigger className="border-border-medium bg-modal-bg h-9"> |
| 238 | + <SelectValue /> |
| 239 | + </SelectTrigger> |
| 240 | + <SelectContent> |
| 241 | + <SelectItem value={INHERIT}>Inherit</SelectItem> |
| 242 | + {(["off", "low", "medium", "high", "xhigh"] as const).map((level) => ( |
| 243 | + <SelectItem key={level} value={level}> |
| 244 | + {level} |
| 245 | + </SelectItem> |
| 246 | + ))} |
| 247 | + </SelectContent> |
| 248 | + </Select> |
| 249 | + </div> |
| 250 | + </div> |
| 251 | + </div> |
| 252 | + ); |
| 253 | + })} |
| 254 | + </div> |
| 255 | + </div> |
| 256 | + </div> |
| 257 | + ); |
| 258 | +} |
0 commit comments