|
| 1 | +import { Component, createEffect, createMemo, createSignal, For, Show, type JSX } from "solid-js" |
| 2 | +import { Select } from "@opencode-ai/ui/select" |
| 3 | +import { TextField } from "@opencode-ai/ui/text-field" |
| 4 | +import { showToast } from "@opencode-ai/ui/toast" |
| 5 | +import { useLanguage } from "@/context/language" |
| 6 | +import { useGlobalSync } from "@/context/global-sync" |
| 7 | +import { useModels } from "@/context/models" |
| 8 | +import { SettingsList } from "./settings-list" |
| 9 | + |
| 10 | +const AGENT_NAMES = ["build", "plan", "general", "explore"] as const |
| 11 | +type AgentName = (typeof AGENT_NAMES)[number] |
| 12 | + |
| 13 | +const AGENT_KEYS: Record<AgentName, { title: string; description: string }> = { |
| 14 | + build: { |
| 15 | + title: "settings.agents.agent.build.title", |
| 16 | + description: "settings.agents.agent.build.description", |
| 17 | + }, |
| 18 | + plan: { |
| 19 | + title: "settings.agents.agent.plan.title", |
| 20 | + description: "settings.agents.agent.plan.description", |
| 21 | + }, |
| 22 | + general: { |
| 23 | + title: "settings.agents.agent.general.title", |
| 24 | + description: "settings.agents.agent.general.description", |
| 25 | + }, |
| 26 | + explore: { |
| 27 | + title: "settings.agents.agent.explore.title", |
| 28 | + description: "settings.agents.agent.explore.description", |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +interface SettingsRowProps { |
| 33 | + title: string | JSX.Element |
| 34 | + description: string | JSX.Element |
| 35 | + children: JSX.Element |
| 36 | +} |
| 37 | + |
| 38 | +const SettingsRow: Component<SettingsRowProps> = (props) => ( |
| 39 | + <div class="flex flex-wrap items-center gap-4 py-3 border-b border-border-weak-base last:border-none sm:flex-nowrap"> |
| 40 | + <div class="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 41 | + <span class="text-14-medium text-text-strong">{props.title}</span> |
| 42 | + <span class="text-12-regular text-text-weak">{props.description}</span> |
| 43 | + </div> |
| 44 | + <div class="flex w-full justify-end sm:w-auto sm:shrink-0">{props.children}</div> |
| 45 | + </div> |
| 46 | +) |
| 47 | + |
| 48 | +const AgentSection: Component<{ name: AgentName }> = (props) => { |
| 49 | + const language = useLanguage() |
| 50 | + const globalSync = useGlobalSync() |
| 51 | + const models = useModels() |
| 52 | + |
| 53 | + const agentConfig = createMemo(() => globalSync.data.config.agent?.[props.name]) |
| 54 | + |
| 55 | + const modelOptions = createMemo(() => { |
| 56 | + const none = { value: "", label: language.t("settings.agents.model.none") } |
| 57 | + return [ |
| 58 | + none, |
| 59 | + ...models.list().map((m) => ({ |
| 60 | + value: `${m.provider.id}/${m.id}`, |
| 61 | + label: `${m.provider.name} / ${m.name}`, |
| 62 | + })), |
| 63 | + ] |
| 64 | + }) |
| 65 | + |
| 66 | + const currentModelValue = createMemo(() => agentConfig()?.model ?? "") |
| 67 | + |
| 68 | + const currentModel = createMemo(() => { |
| 69 | + const v = currentModelValue() |
| 70 | + if (!v) return undefined |
| 71 | + const slashIdx = v.indexOf("/") |
| 72 | + const providerID = v.slice(0, slashIdx) |
| 73 | + const modelID = v.slice(slashIdx + 1) |
| 74 | + return models.list().find((m) => m.provider.id === providerID && m.id === modelID) |
| 75 | + }) |
| 76 | + |
| 77 | + const variantOptions = createMemo(() => { |
| 78 | + const m = currentModel() |
| 79 | + if (!m?.variants || Object.keys(m.variants).length === 0) return [] |
| 80 | + const none = { value: "", label: language.t("settings.agents.variant.none") } |
| 81 | + return [none, ...Object.keys(m.variants).map((v) => ({ value: v, label: v }))] |
| 82 | + }) |
| 83 | + |
| 84 | + const currentVariantValue = createMemo(() => { |
| 85 | + if (!currentModel()) return "" |
| 86 | + return agentConfig()?.variant ?? "" |
| 87 | + }) |
| 88 | + |
| 89 | + const [localPrompt, setLocalPrompt] = createSignal(agentConfig()?.prompt ?? "") |
| 90 | + |
| 91 | + createEffect(() => { |
| 92 | + setLocalPrompt(agentConfig()?.prompt ?? "") |
| 93 | + }) |
| 94 | + |
| 95 | + const save = async (patch: { model?: string; variant?: string; prompt?: string }) => { |
| 96 | + const current = agentConfig() ?? {} |
| 97 | + const next: Record<string, unknown> = { ...current, ...patch } |
| 98 | + if (next.model === "") delete next.model |
| 99 | + if (next.variant === "") delete next.variant |
| 100 | + if (next.prompt === "") delete next.prompt |
| 101 | + await globalSync.updateConfig({ agent: { [props.name]: next } }).catch((err: unknown) => { |
| 102 | + const message = err instanceof Error ? err.message : String(err) |
| 103 | + showToast({ title: language.t("common.requestFailed"), description: message }) |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + return ( |
| 108 | + <div class="flex flex-col gap-1"> |
| 109 | + <h3 class="text-14-medium text-text-strong pb-2"> |
| 110 | + {language.t(AGENT_KEYS[props.name].title)} |
| 111 | + </h3> |
| 112 | + <SettingsList> |
| 113 | + <SettingsRow |
| 114 | + title={language.t("settings.agents.row.model.title")} |
| 115 | + description={language.t("settings.agents.row.model.description")} |
| 116 | + > |
| 117 | + <Select |
| 118 | + options={modelOptions()} |
| 119 | + current={modelOptions().find((o) => o.value === currentModelValue())} |
| 120 | + value={(o) => o.value} |
| 121 | + label={(o) => o.label} |
| 122 | + onSelect={(option) => { |
| 123 | + if (option === undefined) return |
| 124 | + void save({ model: option.value, variant: "" }) |
| 125 | + }} |
| 126 | + variant="secondary" |
| 127 | + size="small" |
| 128 | + triggerVariant="settings" |
| 129 | + triggerStyle={{ "min-width": "220px" }} |
| 130 | + /> |
| 131 | + </SettingsRow> |
| 132 | + |
| 133 | + <Show when={variantOptions().length > 0}> |
| 134 | + <SettingsRow |
| 135 | + title={language.t("settings.agents.row.variant.title")} |
| 136 | + description={language.t("settings.agents.row.variant.description")} |
| 137 | + > |
| 138 | + <Select |
| 139 | + options={variantOptions()} |
| 140 | + current={variantOptions().find((o) => o.value === currentVariantValue())} |
| 141 | + value={(o) => o.value} |
| 142 | + label={(o) => o.label} |
| 143 | + onSelect={(option) => { |
| 144 | + if (option === undefined) return |
| 145 | + void save({ variant: option.value }) |
| 146 | + }} |
| 147 | + variant="secondary" |
| 148 | + size="small" |
| 149 | + triggerVariant="settings" |
| 150 | + /> |
| 151 | + </SettingsRow> |
| 152 | + </Show> |
| 153 | + |
| 154 | + <div class="py-3 flex flex-col gap-2 border-b border-border-weak-base last:border-none"> |
| 155 | + <div class="flex min-w-0 flex-col gap-0.5"> |
| 156 | + <span class="text-14-medium text-text-strong"> |
| 157 | + {language.t("settings.agents.row.prompt.title")} |
| 158 | + </span> |
| 159 | + <span class="text-12-regular text-text-weak"> |
| 160 | + {language.t("settings.agents.row.prompt.description")} |
| 161 | + </span> |
| 162 | + </div> |
| 163 | + <TextField |
| 164 | + label={language.t("settings.agents.row.prompt.title")} |
| 165 | + hideLabel |
| 166 | + multiline |
| 167 | + value={localPrompt()} |
| 168 | + onChange={setLocalPrompt} |
| 169 | + onBlur={() => void save({ prompt: localPrompt() })} |
| 170 | + placeholder={language.t("settings.agents.row.prompt.placeholder")} |
| 171 | + class="text-12-regular w-full" |
| 172 | + spellcheck={false} |
| 173 | + autocorrect="off" |
| 174 | + autocomplete="off" |
| 175 | + autocapitalize="off" |
| 176 | + rows={4} |
| 177 | + /> |
| 178 | + </div> |
| 179 | + </SettingsList> |
| 180 | + </div> |
| 181 | + ) |
| 182 | +} |
| 183 | + |
| 184 | +export const SettingsAgents: Component = () => { |
| 185 | + const language = useLanguage() |
| 186 | + |
| 187 | + return ( |
| 188 | + <div class="flex flex-col h-full overflow-y-auto no-scrollbar px-4 pb-10 sm:px-10 sm:pb-10"> |
| 189 | + <div class="sticky top-0 z-10 bg-[linear-gradient(to_bottom,var(--surface-stronger-non-alpha)_calc(100%_-_24px),transparent)]"> |
| 190 | + <div class="flex flex-col gap-1 pt-6 pb-8"> |
| 191 | + <h2 class="text-16-medium text-text-strong">{language.t("settings.agents.title")}</h2> |
| 192 | + <p class="text-12-regular text-text-weak">{language.t("settings.agents.header.description")}</p> |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + |
| 196 | + <div class="flex flex-col gap-8 w-full max-w-[720px]"> |
| 197 | + <For each={AGENT_NAMES}>{(name) => <AgentSection name={name} />}</For> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + ) |
| 201 | +} |
0 commit comments