Skip to content

Commit dd8bd27

Browse files
committed
fix: generate client-side id for optimistic update, return shallow copy from getUserTemplates
1. New characters now get a 'user-<hex>' id immediately on save, matching the server's convention (uuid.uuid4().hex[:8]). Previously the id was '' which the merge guard in _getCharacterList filtered as falsy. 2. getUserTemplates() now returns [...userTemplates] so callers cannot accidentally mutate module state.
1 parent 937b1d4 commit dd8bd27

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

static/js/presets.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ export async function saveCustomPreset(showToast, showError) {
837837
if (saveName) {
838838
const _existing = userTemplates.find(t => t.name === saveName);
839839
const _entry = {
840-
id: (_existing && _existing.id) || '',
840+
id: (_existing && _existing.id) || ('user-' + Math.random().toString(16).slice(2, 10)),
841841
name: saveName,
842842
system_prompt: system_prompt || '',
843843
temperature: config.temperature,
@@ -906,7 +906,7 @@ export function getAllPresets() {
906906
* Get the in-memory user templates list (may be stale; call loadUserTemplates first if freshness matters).
907907
*/
908908
export function getUserTemplates() {
909-
return userTemplates;
909+
return [...userTemplates];
910910
}
911911

912912
/**

tests/test_group_character_dropdown.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,13 @@ def test_presets_optimistic_update_on_save():
4848

4949

5050
def test_presets_getUserTemplates_returns_array():
51-
"""getUserTemplates should return the userTemplates array directly."""
52-
assert "return userTemplates" in PRESETS_JS
51+
"""getUserTemplates should return a shallow copy of userTemplates."""
52+
assert "return [...userTemplates]" in PRESETS_JS
53+
54+
55+
def test_presets_optimistic_id_not_empty():
56+
"""Optimistic update must generate a client-side id for new characters (not empty string)."""
57+
# The id generation uses 'user-' prefix matching server's uuid convention
58+
assert "user-' + Math.random" in PRESETS_JS
59+
# Must NOT use empty string as fallback (that was the bug)
60+
assert "(_existing && _existing.id) || ''" not in PRESETS_JS

0 commit comments

Comments
 (0)