Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(plugins): save untouched json settings without stringifying them

Michel Ruffieuxf0a5f562f53f
diff --git a/apps/desktop/src/components/plugins/PluginSettingsSheet.tsx b/apps/desktop/src/components/plugins/PluginSettingsSheet.tsx
index c9da32d3b..9d86e4d78 100644
--- a/apps/desktop/src/components/plugins/PluginSettingsSheet.tsx
+++ b/apps/desktop/src/components/plugins/PluginSettingsSheet.tsx
@@ -67,6 +67,18 @@ function serializeJson(value: unknown): string {
}
}
+/**
+ * A json setting's draft is the stored value until the user edits its
+ * textarea, and the textarea text afterwards. Only text needs parsing:
+ * `String({})` is `"[object Object]"`, so parsing an untouched object made
+ * every save fail with "invalid JSON" for the first json setting.
+ */
+export function parseJsonSettingDraft(draft: unknown): unknown {
+ if (typeof draft !== "string") return draft ?? {};
+ const text = draft.trim();
+ return JSON.parse(text === "" ? "{}" : text);
+}
+
export function PluginSettingsSheet({ plugin, platform, onClose, onSaved }: Props) {
// Native plugin views composite above the renderer; hide them while this
// host sheet is open so the right edge of the dialog stays clickable.
@@ -124,7 +136,7 @@ export function PluginSettingsSheet({ plugin, platform, onClose, onSaved }: Prop
for (const setting of settings) {
if (setting.type !== "json") continue;
try {
- payload[setting.key] = JSON.parse(String(payload[setting.key] ?? "{}"));
+ payload[setting.key] = parseJsonSettingDraft(payload[setting.key]);
} catch {
setError(t("plugins.settingsInvalidJson", { name: setting.title }));
return;
diff --git a/apps/desktop/test/plugin-settings.test.mjs b/apps/desktop/test/plugin-settings.test.mjs
index 45bd77f45..7200daa65 100644
--- a/apps/desktop/test/plugin-settings.test.mjs
+++ b/apps/desktop/test/plugin-settings.test.mjs
@@ -28,6 +28,25 @@ test("plugin settings expose generated fields and plugin-local shortcut metadata
assert.match(sheet, /api\.setPluginSettings/);
});
+test("saving parses only json drafts the user edited; untouched objects pass through", async () => {
+ // Regression: the sheet stringified every json draft before parsing, so an
+ // untouched object became "[object Object]" and the save was refused.
+ assert.match(sheet, /payload\[setting\.key\] = parseJsonSettingDraft\(payload\[setting\.key\]\)/);
+ assert.doesNotMatch(sheet, /JSON\.parse\(String\(/);
+ const source = sheet.match(/export function parseJsonSettingDraft[\s\S]*?\n\}\n/)[0];
+ const { parseJsonSettingDraft } = await import(
+ `data:text/javascript,${encodeURIComponent(source.replace(/\(draft: unknown\): unknown/, "(draft)"))}`
+ );
+ assert.deepEqual(parseJsonSettingDraft({}), {});
+ assert.deepEqual(parseJsonSettingDraft({ mode: "auto" }), { mode: "auto" });
+ assert.deepEqual(parseJsonSettingDraft(["a"]), ["a"]);
+ assert.deepEqual(parseJsonSettingDraft(undefined), {});
+ assert.deepEqual(parseJsonSettingDraft(null), {});
+ assert.deepEqual(parseJsonSettingDraft(""), {});
+ assert.deepEqual(parseJsonSettingDraft('{"rules": []}'), { rules: [] });
+ assert.throws(() => parseJsonSettingDraft("{nope"), SyntaxError);
+});
+
test("generated plugin settings stay author-language; plugins localize from the host locale", () => {
assert.match(sdk, /export type PluginSettingContrib = \{[\s\S]*?title: string;/);
assert.doesNotMatch(