@marimo-team/islands 0.19.7-dev10 → 0.19.7-dev15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.19.7-dev10",
3
+ "version": "0.19.7-dev15",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -61,14 +61,14 @@ describe("ai-utils", () => {
61
61
  describe("getConfiguredProvider", () => {
62
62
  it("should return undefined when no AI config", () => {
63
63
  const config: UserConfig = {} as UserConfig;
64
- expect(getConfiguredProvider(config)).toBeUndefined();
64
+ expect(getConfiguredProvider(config.ai)).toBeUndefined();
65
65
  });
66
66
 
67
67
  it("should return undefined when AI config has no credentials", () => {
68
68
  const config: UserConfig = {
69
69
  ai: {},
70
70
  } as UserConfig;
71
- expect(getConfiguredProvider(config)).toBeUndefined();
71
+ expect(getConfiguredProvider(config.ai)).toBeUndefined();
72
72
  });
73
73
 
74
74
  it("should return openai when OpenAI API key is set", () => {
@@ -77,7 +77,7 @@ describe("ai-utils", () => {
77
77
  open_ai: { api_key: "sk-test" },
78
78
  },
79
79
  } as UserConfig;
80
- expect(getConfiguredProvider(config)).toBe("openai");
80
+ expect(getConfiguredProvider(config.ai)).toBe("openai");
81
81
  });
82
82
 
83
83
  it("should return anthropic when Anthropic API key is set", () => {
@@ -86,7 +86,7 @@ describe("ai-utils", () => {
86
86
  anthropic: { api_key: "sk-ant-test" },
87
87
  },
88
88
  } as UserConfig;
89
- expect(getConfiguredProvider(config)).toBe("anthropic");
89
+ expect(getConfiguredProvider(config.ai)).toBe("anthropic");
90
90
  });
91
91
 
92
92
  it("should return google when Google API key is set", () => {
@@ -95,7 +95,7 @@ describe("ai-utils", () => {
95
95
  google: { api_key: "google-key" },
96
96
  },
97
97
  } as UserConfig;
98
- expect(getConfiguredProvider(config)).toBe("google");
98
+ expect(getConfiguredProvider(config.ai)).toBe("google");
99
99
  });
100
100
 
101
101
  it("should return ollama when Ollama base URL is set", () => {
@@ -104,7 +104,7 @@ describe("ai-utils", () => {
104
104
  ollama: { base_url: "http://localhost:11434" },
105
105
  },
106
106
  } as UserConfig;
107
- expect(getConfiguredProvider(config)).toBe("ollama");
107
+ expect(getConfiguredProvider(config.ai)).toBe("ollama");
108
108
  });
109
109
 
110
110
  it("should return azure only when both API key and base URL are set", () => {
@@ -113,7 +113,7 @@ describe("ai-utils", () => {
113
113
  azure: { api_key: "azure-key", base_url: "https://azure.com" },
114
114
  },
115
115
  } as UserConfig;
116
- expect(getConfiguredProvider(config)).toBe("azure");
116
+ expect(getConfiguredProvider(config.ai)).toBe("azure");
117
117
  });
118
118
 
119
119
  it("should return undefined for azure with only API key", () => {
@@ -122,7 +122,7 @@ describe("ai-utils", () => {
122
122
  azure: { api_key: "azure-key" },
123
123
  },
124
124
  } as UserConfig;
125
- expect(getConfiguredProvider(config)).toBeUndefined();
125
+ expect(getConfiguredProvider(config.ai)).toBeUndefined();
126
126
  });
127
127
 
128
128
  it("should return custom provider when configured", () => {
@@ -133,14 +133,14 @@ describe("ai-utils", () => {
133
133
  },
134
134
  },
135
135
  } as unknown as UserConfig;
136
- expect(getConfiguredProvider(config)).toBe("my_provider");
136
+ expect(getConfiguredProvider(config.ai)).toBe("my_provider");
137
137
  });
138
138
  });
139
139
 
140
140
  describe("getRecommendedModel", () => {
141
141
  it("should return undefined when no provider is configured", () => {
142
142
  const config: UserConfig = {} as UserConfig;
143
- expect(getRecommendedModel(config)).toBeUndefined();
143
+ expect(getRecommendedModel(config.ai)).toBeUndefined();
144
144
  });
145
145
 
146
146
  it("should return openai model when OpenAI is configured", () => {
@@ -149,7 +149,7 @@ describe("ai-utils", () => {
149
149
  open_ai: { api_key: "sk-test" },
150
150
  },
151
151
  } as UserConfig;
152
- expect(getRecommendedModel(config)).toBe("openai/gpt-4");
152
+ expect(getRecommendedModel(config.ai)).toBe("openai/gpt-4");
153
153
  });
154
154
 
155
155
  it("should return anthropic model when Anthropic is configured", () => {
@@ -158,7 +158,7 @@ describe("ai-utils", () => {
158
158
  anthropic: { api_key: "sk-ant-test" },
159
159
  },
160
160
  } as UserConfig;
161
- expect(getRecommendedModel(config)).toBe("anthropic/claude-3-sonnet");
161
+ expect(getRecommendedModel(config.ai)).toBe("anthropic/claude-3-sonnet");
162
162
  });
163
163
 
164
164
  it("should return google model when Google is configured", () => {
@@ -167,7 +167,7 @@ describe("ai-utils", () => {
167
167
  google: { api_key: "google-key" },
168
168
  },
169
169
  } as UserConfig;
170
- expect(getRecommendedModel(config)).toBe("google/gemini-pro");
170
+ expect(getRecommendedModel(config.ai)).toBe("google/gemini-pro");
171
171
  });
172
172
 
173
173
  it("should return ollama model when Ollama is configured", () => {
@@ -176,7 +176,7 @@ describe("ai-utils", () => {
176
176
  ollama: { base_url: "http://localhost:11434" },
177
177
  },
178
178
  } as UserConfig;
179
- expect(getRecommendedModel(config)).toBe("ollama/llama2");
179
+ expect(getRecommendedModel(config.ai)).toBe("ollama/llama2");
180
180
  });
181
181
  });
182
182
 
@@ -194,7 +194,7 @@ describe("ai-utils", () => {
194
194
  },
195
195
  } as unknown as UserConfig;
196
196
 
197
- const result = autoPopulateModels(values);
197
+ const result = autoPopulateModels(values.ai);
198
198
 
199
199
  expect(result.chatModel).toBeUndefined();
200
200
  expect(result.editModel).toBeUndefined();
@@ -205,7 +205,7 @@ describe("ai-utils", () => {
205
205
  ai: {},
206
206
  } as UserConfig;
207
207
 
208
- const result = autoPopulateModels(values);
208
+ const result = autoPopulateModels(values.ai);
209
209
 
210
210
  expect(result.chatModel).toBeUndefined();
211
211
  expect(result.editModel).toBeUndefined();
@@ -218,7 +218,7 @@ describe("ai-utils", () => {
218
218
  },
219
219
  } as UserConfig;
220
220
 
221
- const result = autoPopulateModels(values);
221
+ const result = autoPopulateModels(values.ai);
222
222
 
223
223
  expect(result.chatModel).toBe("openai/gpt-4");
224
224
  expect(result.editModel).toBe("openai/gpt-4");
@@ -236,7 +236,7 @@ describe("ai-utils", () => {
236
236
  },
237
237
  } as unknown as UserConfig;
238
238
 
239
- const result = autoPopulateModels(values);
239
+ const result = autoPopulateModels(values.ai);
240
240
 
241
241
  expect(result.chatModel).toBe("openai/gpt-4");
242
242
  expect(result.editModel).toBeUndefined();
@@ -254,7 +254,7 @@ describe("ai-utils", () => {
254
254
  },
255
255
  } as unknown as UserConfig;
256
256
 
257
- const result = autoPopulateModels(values);
257
+ const result = autoPopulateModels(values.ai);
258
258
 
259
259
  expect(result.chatModel).toBeUndefined();
260
260
  expect(result.editModel).toBe("openai/gpt-4");
@@ -267,7 +267,7 @@ describe("ai-utils", () => {
267
267
  },
268
268
  } as UserConfig;
269
269
 
270
- const result = autoPopulateModels(values);
270
+ const result = autoPopulateModels(values.ai);
271
271
 
272
272
  expect(result.chatModel).toBe("anthropic/claude-3-sonnet");
273
273
  expect(result.editModel).toBe("anthropic/claude-3-sonnet");
@@ -19,6 +19,7 @@ import {
19
19
  } from "@/core/ai/ids/ids";
20
20
  import { type AiModel, AiModelRegistry } from "@/core/ai/model-registry";
21
21
  import { aiAtom, completionAtom } from "@/core/config/config";
22
+ import { useOpenSettingsToTab } from "../app-config/state";
22
23
  import {
23
24
  DropdownMenu,
24
25
  DropdownMenuContent,
@@ -62,6 +63,7 @@ export const AIModelDropdown = ({
62
63
  const ai = useAtomValue(aiAtom);
63
64
  const completion = useAtomValue(completionAtom);
64
65
  const { saveModelChange } = useModelChange();
66
+ const { handleClick } = useOpenSettingsToTab();
65
67
 
66
68
  // Only include autocompleteModel if copilot is set to "custom"
67
69
  const autocompleteModel =
@@ -188,16 +190,12 @@ export const AIModelDropdown = ({
188
190
  {showAddCustomModelDocs && (
189
191
  <>
190
192
  <DropdownMenuSeparator />
191
- <DropdownMenuItem className="flex items-center gap-2">
192
- <a
193
- className="flex items-center gap-1"
194
- href="https://links.marimo.app/custom-models"
195
- target="_blank"
196
- rel="noreferrer"
197
- >
198
- <CircleHelpIcon className="h-3 w-3" />
199
- <span>How to add a custom model</span>
200
- </a>
193
+ <DropdownMenuItem
194
+ className="h-7 flex items-center gap-2"
195
+ onClick={() => handleClick("ai", "ai-models")}
196
+ >
197
+ <CircleHelpIcon className="h-3 w-3" />
198
+ <span className="cursor-pointer text-link">Add custom model</span>
201
199
  </DropdownMenuItem>
202
200
  </>
203
201
  )}
@@ -32,18 +32,16 @@ const CREDENTIAL_CHECKERS: Record<KnownProviderId, CredentialChecker> = {
32
32
  * Returns the first configured provider based on credentials.
33
33
  */
34
34
  export function getConfiguredProvider(
35
- config: UserConfig,
35
+ config: UserConfig["ai"],
36
36
  ): ProviderId | undefined {
37
- const ai = config.ai;
38
-
39
37
  for (const provider of KNOWN_PROVIDERS) {
40
- if (CREDENTIAL_CHECKERS[provider](ai)) {
38
+ if (CREDENTIAL_CHECKERS[provider](config)) {
41
39
  return provider;
42
40
  }
43
41
  }
44
42
 
45
43
  // Check custom providers
46
- const customProviders = ai?.custom_providers;
44
+ const customProviders = config?.custom_providers;
47
45
  if (customProviders) {
48
46
  const firstCustomProvider = Object.entries(customProviders).find(
49
47
  ([_, providerConfig]) => providerConfig?.base_url,
@@ -54,7 +52,9 @@ export function getConfiguredProvider(
54
52
  }
55
53
  }
56
54
 
57
- export function getRecommendedModel(config: UserConfig): string | undefined {
55
+ export function getRecommendedModel(
56
+ config: UserConfig["ai"],
57
+ ): string | undefined {
58
58
  const provider = getConfiguredProvider(config);
59
59
  if (!provider) {
60
60
  return undefined;
@@ -73,14 +73,16 @@ export interface AutoPopulateResult {
73
73
  *
74
74
  * @param values - The full form values
75
75
  */
76
- export function autoPopulateModels(values: UserConfig): AutoPopulateResult {
76
+ export function autoPopulateModels(
77
+ values: UserConfig["ai"],
78
+ ): AutoPopulateResult {
77
79
  const result: AutoPopulateResult = {
78
80
  chatModel: undefined,
79
81
  editModel: undefined,
80
82
  };
81
83
 
82
- const needsChatModel = !values.ai?.models?.chat_model;
83
- const needsEditModel = !values.ai?.models?.edit_model;
84
+ const needsChatModel = !values?.models?.chat_model;
85
+ const needsEditModel = !values?.models?.edit_model;
84
86
 
85
87
  if (!needsChatModel && !needsEditModel) {
86
88
  return result;
@@ -1,7 +1,8 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
 
3
3
  import { describe, expect, test } from "vitest";
4
- import { getDirtyValues } from "../user-config-form";
4
+ import type { UserConfig } from "@/core/config/config-schema";
5
+ import { applyManualInjections, getDirtyValues } from "../user-config-form";
5
6
 
6
7
  describe("getDirtyValues", () => {
7
8
  test("extracts only dirty fields", () => {
@@ -71,4 +72,49 @@ describe("getDirtyValues", () => {
71
72
  expect(result).toEqual({ display: { theme: "dark" } });
72
73
  expect(result).not.toHaveProperty("runtime");
73
74
  });
75
+
76
+ test("applyManualInjections injects touched ai model fields", () => {
77
+ const values = {
78
+ ai: {
79
+ models: {
80
+ displayed_models: ["openai/gpt-4"],
81
+ custom_models: ["openai/custom-model"],
82
+ },
83
+ },
84
+ } as UserConfig;
85
+ const dirtyValues: Partial<UserConfig> = {};
86
+ const touchedFields = {
87
+ ai: { models: { displayed_models: true } },
88
+ };
89
+
90
+ applyManualInjections({ values, dirtyValues, touchedFields });
91
+
92
+ expect(dirtyValues).toEqual({
93
+ ai: {
94
+ models: {
95
+ displayed_models: ["openai/gpt-4"],
96
+ custom_models: ["openai/custom-model"],
97
+ },
98
+ },
99
+ });
100
+ });
101
+
102
+ test("applyManualInjections skips when field not touched", () => {
103
+ const values = {
104
+ ai: {
105
+ models: {
106
+ displayed_models: ["openai/gpt-4"],
107
+ custom_models: ["openai/custom-model"],
108
+ },
109
+ },
110
+ } as UserConfig;
111
+ const dirtyValues: Partial<UserConfig> = {};
112
+ const touchedFields = {
113
+ ai: { models: { displayed_models: false } },
114
+ };
115
+
116
+ applyManualInjections({ values, dirtyValues, touchedFields });
117
+
118
+ expect(dirtyValues).toEqual({});
119
+ });
74
120
  });
@@ -1,10 +1,10 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
 
3
+ import { useAtom } from "jotai";
3
4
  import {
4
5
  BotIcon,
5
6
  BrainIcon,
6
7
  ChevronRightIcon,
7
- InfoIcon,
8
8
  PlusIcon,
9
9
  Trash2Icon,
10
10
  } from "lucide-react";
@@ -61,7 +61,6 @@ import {
61
61
  } from "../ui/accordion";
62
62
  import { Button } from "../ui/button";
63
63
  import { Checkbox } from "../ui/checkbox";
64
- import { DropdownMenuSeparator } from "../ui/dropdown-menu";
65
64
  import { Label } from "../ui/label";
66
65
  import { ExternalLink } from "../ui/links";
67
66
  import {
@@ -79,6 +78,7 @@ import { AWS_REGIONS } from "./constants";
79
78
  import { IncorrectModelId } from "./incorrect-model-id";
80
79
  import { IsOverridden } from "./is-overridden";
81
80
  import { MCPConfig } from "./mcp-config";
81
+ import { aiSettingsSubTabAtom } from "./state";
82
82
 
83
83
  interface AiConfigProps {
84
84
  form: UseFormReturn<UserConfig>;
@@ -234,9 +234,7 @@ interface ModelSelectorProps {
234
234
  config: UserConfig;
235
235
  name: FieldPath<UserConfig>;
236
236
  placeholder: string;
237
- testId: string;
238
237
  description?: React.ReactNode;
239
- disabled?: boolean;
240
238
  label: string;
241
239
  forRole: SupportedRole;
242
240
  onSubmit: (values: UserConfig) => void;
@@ -247,9 +245,7 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({
247
245
  config,
248
246
  name,
249
247
  placeholder,
250
- testId,
251
248
  description,
252
- disabled = false,
253
249
  label,
254
250
  forRole,
255
251
  onSubmit,
@@ -276,34 +272,6 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({
276
272
  placeholder={placeholder}
277
273
  onSelect={selectModel}
278
274
  triggerClassName="text-sm"
279
- customDropdownContent={
280
- <>
281
- <DropdownMenuSeparator />
282
- <p className="px-2 py-1.5 text-sm text-muted-secondary flex items-center gap-1">
283
- Enter a custom model
284
- <Tooltip content="Models should include the provider prefix, e.g. 'openai/gpt-4o'">
285
- <InfoIcon className="h-3 w-3" />
286
- </Tooltip>
287
- </p>
288
- <div className="px-2 py-1">
289
- <Input
290
- data-testid={testId}
291
- className="w-full border-border shadow-none focus-visible:shadow-xs"
292
- placeholder={placeholder}
293
- {...field}
294
- value={asStringOrEmpty(field.value)}
295
- disabled={disabled}
296
- onKeyDown={Events.stopPropagation()}
297
- />
298
- {value && (
299
- <IncorrectModelId
300
- value={value}
301
- includeSuggestion={false}
302
- />
303
- )}
304
- </div>
305
- </>
306
- }
307
275
  forRole={forRole}
308
276
  />
309
277
  </FormControl>
@@ -432,7 +400,6 @@ const renderCopilotProvider = ({
432
400
  config={config}
433
401
  name="ai.models.autocomplete_model"
434
402
  placeholder="ollama/qwen2.5-coder:1.5b"
435
- testId="custom-model-input"
436
403
  description="Model to use for code completion when using a custom provider."
437
404
  onSubmit={onSubmit}
438
405
  forRole="autocomplete"
@@ -1209,8 +1176,6 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
1209
1176
  config,
1210
1177
  onSubmit,
1211
1178
  }) => {
1212
- const isWasmRuntime = isWasm();
1213
-
1214
1179
  return (
1215
1180
  <SettingGroup>
1216
1181
  <SettingSubtitle>AI Assistant</SettingSubtitle>
@@ -1244,8 +1209,6 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
1244
1209
  config={config}
1245
1210
  name="ai.models.chat_model"
1246
1211
  placeholder={DEFAULT_AI_MODEL}
1247
- testId="ai-chat-model-input"
1248
- disabled={isWasmRuntime}
1249
1212
  description={
1250
1213
  <span>Model to use for chat conversations in the Chat panel.</span>
1251
1214
  }
@@ -1258,8 +1221,6 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
1258
1221
  config={config}
1259
1222
  name="ai.models.edit_model"
1260
1223
  placeholder={DEFAULT_AI_MODEL}
1261
- testId="ai-edit-model-input"
1262
- disabled={isWasmRuntime}
1263
1224
  description={
1264
1225
  <span>
1265
1226
  Model to use for code editing with the{" "}
@@ -1270,18 +1231,6 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
1270
1231
  onSubmit={onSubmit}
1271
1232
  />
1272
1233
 
1273
- <ul className="bg-muted p-2 rounded-md list-disc space-y-1 pl-6">
1274
- <li className="text-xs text-muted-secondary">
1275
- Models should include the provider name and model name separated by a
1276
- slash. For example, "anthropic/claude-3-5-sonnet-latest" or
1277
- "google/gemini-2.0-flash-exp"
1278
- </li>
1279
- <li className="text-xs text-muted-secondary">
1280
- Depending on the provider, we will use the respective API key and
1281
- additional configuration.
1282
- </li>
1283
- </ul>
1284
-
1285
1234
  <FormField
1286
1235
  control={form.control}
1287
1236
  name="ai.rules"
@@ -1434,6 +1383,7 @@ export const AiModelDisplayConfig: React.FC<AiConfigProps> = ({
1434
1383
 
1435
1384
  form.setValue("ai.models.displayed_models", newModels, {
1436
1385
  shouldDirty: true,
1386
+ shouldTouch: true,
1437
1387
  });
1438
1388
  onSubmit(form.getValues());
1439
1389
  });
@@ -1453,6 +1403,7 @@ export const AiModelDisplayConfig: React.FC<AiConfigProps> = ({
1453
1403
 
1454
1404
  form.setValue("ai.models.displayed_models", newModels, {
1455
1405
  shouldDirty: true,
1406
+ shouldTouch: true,
1456
1407
  });
1457
1408
  onSubmit(form.getValues());
1458
1409
  },
@@ -1466,9 +1417,11 @@ export const AiModelDisplayConfig: React.FC<AiConfigProps> = ({
1466
1417
  );
1467
1418
  form.setValue("ai.models.displayed_models", newDisplayedModels, {
1468
1419
  shouldDirty: true,
1420
+ shouldTouch: true,
1469
1421
  });
1470
1422
  form.setValue("ai.models.custom_models", newModels, {
1471
1423
  shouldDirty: true,
1424
+ shouldTouch: true,
1472
1425
  });
1473
1426
  onSubmit(form.getValues());
1474
1427
  });
@@ -1548,6 +1501,7 @@ export const AddModelForm: React.FC<{
1548
1501
 
1549
1502
  form.setValue("ai.models.custom_models", [newModel.id, ...customModels], {
1550
1503
  shouldDirty: true,
1504
+ shouldTouch: true,
1551
1505
  });
1552
1506
  onSubmit(form.getValues());
1553
1507
  resetForm();
@@ -1740,6 +1694,12 @@ const AddButton = ({
1740
1694
  );
1741
1695
  };
1742
1696
 
1697
+ export type AiSettingsSubTab =
1698
+ | "ai-features"
1699
+ | "ai-providers"
1700
+ | "ai-models"
1701
+ | "mcp";
1702
+
1743
1703
  export const AiConfig: React.FC<AiConfigProps> = ({
1744
1704
  form,
1745
1705
  config,
@@ -1747,8 +1707,14 @@ export const AiConfig: React.FC<AiConfigProps> = ({
1747
1707
  }) => {
1748
1708
  // MCP is not supported in WASM
1749
1709
  const wasm = isWasm();
1710
+ const [activeTab, setActiveTab] = useAtom(aiSettingsSubTabAtom);
1711
+
1750
1712
  return (
1751
- <Tabs defaultValue="ai-features" className="flex-1">
1713
+ <Tabs
1714
+ value={activeTab}
1715
+ onValueChange={(value) => setActiveTab(value as AiSettingsSubTab)}
1716
+ className="flex-1"
1717
+ >
1752
1718
  <TabsList className="mb-2">
1753
1719
  <TabsTrigger value="ai-features">AI Features</TabsTrigger>
1754
1720
  <TabsTrigger value="ai-providers">AI Providers</TabsTrigger>
@@ -1,17 +1,27 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
2
  import { atom, useSetAtom } from "jotai";
3
+ import type { AiSettingsSubTab } from "./ai-config";
3
4
  import {
4
5
  activeUserConfigCategoryAtom,
5
6
  type SettingCategoryId,
6
7
  } from "./user-config-form";
7
8
 
9
+ export const aiSettingsSubTabAtom = atom<AiSettingsSubTab>("ai-features");
10
+
8
11
  export const settingDialogAtom = atom<boolean>(false);
9
12
 
10
13
  export function useOpenSettingsToTab() {
11
14
  const setActiveCategory = useSetAtom(activeUserConfigCategoryAtom);
12
15
  const setSettingsDialog = useSetAtom(settingDialogAtom);
13
- const handleClick = (tab: SettingCategoryId) => {
16
+ const setAiSubTab = useSetAtom(aiSettingsSubTabAtom);
17
+
18
+ // Note: If more settings categories need sub-tabs or deep-linking is required,
19
+ // consider using a different strategy like query params
20
+ const handleClick = (tab: SettingCategoryId, subTab?: AiSettingsSubTab) => {
14
21
  setActiveCategory(tab);
22
+ if (tab === "ai") {
23
+ setAiSubTab(subTab ?? "ai-features");
24
+ }
15
25
  setSettingsDialog(true);
16
26
  };
17
27
  return { handleClick };
@@ -14,7 +14,7 @@ import {
14
14
  } from "lucide-react";
15
15
  import React, { useId, useRef } from "react";
16
16
  import { useLocale } from "react-aria";
17
- import type { FieldValues } from "react-hook-form";
17
+ import type { FieldPath, FieldValues } from "react-hook-form";
18
18
  import { useForm } from "react-hook-form";
19
19
  import type z from "zod";
20
20
  import { Button } from "@/components/ui/button";
@@ -95,6 +95,68 @@ export function getDirtyValues<T extends FieldValues>(
95
95
  return result;
96
96
  }
97
97
 
98
+ type ManualInjector = (
99
+ values: UserConfig,
100
+ dirtyValues: Partial<UserConfig>,
101
+ ) => void;
102
+
103
+ const modelsAiInjection = (
104
+ values: UserConfig,
105
+ dirtyValues: Partial<UserConfig>,
106
+ ) => {
107
+ dirtyValues.ai = {
108
+ ...dirtyValues.ai,
109
+ models: {
110
+ ...dirtyValues.ai?.models,
111
+ displayed_models: values.ai?.models?.displayed_models ?? [],
112
+ custom_models: values.ai?.models?.custom_models ?? [],
113
+ },
114
+ };
115
+ };
116
+
117
+ // Some fields (like AI model lists) have empty arrays as default values.
118
+ // If a user explicitly clears them, RHF won't mark them dirty, so we use
119
+ // touchedFields to force-include those values in the payload.
120
+ const MANUAL_INJECT_ENTRIES = [
121
+ ["ai.models.displayed_models", modelsAiInjection],
122
+ ["ai.models.custom_models", modelsAiInjection],
123
+ ] as const satisfies readonly (readonly [
124
+ FieldPath<UserConfig>,
125
+ ManualInjector,
126
+ ])[];
127
+
128
+ const MANUAL_INJECT_FIELDS = new Map(MANUAL_INJECT_ENTRIES);
129
+
130
+ const isTouchedPath = (
131
+ touched: unknown,
132
+ path: FieldPath<UserConfig>,
133
+ ): boolean => {
134
+ if (!touched) {
135
+ return false;
136
+ }
137
+ let current: unknown = touched;
138
+ for (const segment of path.split(".")) {
139
+ if (typeof current !== "object" || current === null) {
140
+ return false;
141
+ }
142
+ current = (current as Record<string, unknown>)[segment];
143
+ }
144
+ return current === true;
145
+ };
146
+
147
+ export const applyManualInjections = (opts: {
148
+ values: UserConfig;
149
+ dirtyValues: Partial<UserConfig>;
150
+ touchedFields: unknown;
151
+ }) => {
152
+ const { values, dirtyValues, touchedFields } = opts;
153
+ for (const [fieldPath, injector] of MANUAL_INJECT_FIELDS) {
154
+ if (isTouchedPath(touchedFields, fieldPath)) {
155
+ injector(values, dirtyValues);
156
+ }
157
+ }
158
+ };
159
+
98
160
  const categories = [
99
161
  {
100
162
  id: "editor",
@@ -181,7 +243,10 @@ export const UserConfigForm: React.FC = () => {
181
243
  defaultValues: config,
182
244
  });
183
245
 
184
- const setAiModels = (values: UserConfig, dirtyAiConfig: UserConfig["ai"]) => {
246
+ const setAiModels = (
247
+ values: UserConfig["ai"],
248
+ dirtyAiConfig: UserConfig["ai"],
249
+ ) => {
185
250
  const { chatModel, editModel } = autoPopulateModels(values);
186
251
  if (chatModel || editModel) {
187
252
  dirtyAiConfig = {
@@ -207,13 +272,18 @@ export const UserConfigForm: React.FC = () => {
207
272
  // Only send values that were actually changed to avoid
208
273
  // overwriting backend values the form doesn't manage
209
274
  const dirtyValues = getDirtyValues(values, form.formState.dirtyFields);
275
+ applyManualInjections({
276
+ values,
277
+ dirtyValues,
278
+ touchedFields: form.formState.touchedFields,
279
+ });
210
280
  if (Object.keys(dirtyValues).length === 0) {
211
281
  return; // Nothing changed
212
282
  }
213
283
 
214
284
  // Auto-populate AI models when credentials are set, makes it easier to get started
215
285
  if (dirtyValues.ai) {
216
- dirtyValues.ai = setAiModels(values, dirtyValues.ai);
286
+ dirtyValues.ai = setAiModels(values.ai, dirtyValues.ai);
217
287
  }
218
288
 
219
289
  await saveUserConfig({ config: dirtyValues }).then(() => {
@@ -435,7 +435,11 @@ const ChatPanel = () => {
435
435
  title="Chat with AI"
436
436
  description="No AI provider configured or model selected"
437
437
  action={
438
- <Button variant="outline" size="sm" onClick={() => handleClick("ai")}>
438
+ <Button
439
+ variant="outline"
440
+ size="sm"
441
+ onClick={() => handleClick("ai", "ai-providers")}
442
+ >
439
443
  Edit AI settings
440
444
  </Button>
441
445
  }