Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): the saved model pick was read before its own key existed

yhh0173f7dd20f3
diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.ts
index c827a8718..5d146c98c 100644
--- a/desktop/src/store/AgentStore.ts
+++ b/desktop/src/store/AgentStore.ts
@@ -256,6 +256,27 @@ export interface ConversationSummary {
turns: number;
}
+const SELECTION_KEY = 'estella.agent.selection';
+
+export interface AgentSelection {
+ providerId: string;
+ model: string;
+}
+
+/**
+ * The pick this window starts on. Declared ABOVE the store because the store's
+ * initializer calls it: a `const` read from below is in its dead zone there,
+ * and the throw lands in the catch that is here for a browser refusing storage.
+ */
+function loadSelection(): AgentSelection | null {
+ try {
+ const raw = localStorage.getItem(SELECTION_KEY);
+ return raw ? (JSON.parse(raw) as AgentSelection) : null;
+ } catch {
+ return null;
+ }
+}
+
export const useAgent = create<AgentState>(() => ({
status: IDLE, turns: [], driving: false, peeked: [], queued: [], checkpointDone: null,
draft: '', attachments: [], selection: loadSelection(), conversations: [], historyOpen: false,
@@ -307,22 +328,6 @@ export const removeAgentAttachment = (id: string): void =>
// Persisted here rather than as a registered setting: it is picked from the
// composer, and a settings row for it would be a second control for one choice.
-const SELECTION_KEY = 'estella.agent.selection';
-
-export interface AgentSelection {
- providerId: string;
- model: string;
-}
-
-function loadSelection(): AgentSelection | null {
- try {
- const raw = localStorage.getItem(SELECTION_KEY);
- return raw ? (JSON.parse(raw) as AgentSelection) : null;
- } catch {
- return null;
- }
-}
-
/**
* A provider, with the one part of it that cannot be shipped filled in: the
* vendor whose ADDRESS holds still while its model names do not. Providers the
diff --git a/desktop/tests/agent-selection-hydrate.test.ts b/desktop/tests/agent-selection-hydrate.test.ts
new file mode 100644
index 000000000..7297fcf63
--- /dev/null
+++ b/desktop/tests/agent-selection-hydrate.test.ts
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The model pick a window starts on.
+ *
+ * It is read once, while the store is being constructed, out of a `try` that is
+ * there for a browser refusing storage. Anything else thrown in that read —
+ * including a `const` touched before its declaration ran — comes back as "no
+ * pick saved", and the composer silently offers the default instead.
+ */
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+
+const FLASH = { providerId: 'deepseek', model: 'deepseek-v4-flash' };
+
+/** A window that already has a pick saved, as a returning one does. */
+function windowWith(entries: Record<string, string>): void {
+ const store = new Map(Object.entries(entries));
+ vi.stubGlobal('localStorage', {
+ getItem: (k: string) => store.get(k) ?? null,
+ setItem: (k: string, v: string) => { store.set(k, v); },
+ removeItem: (k: string) => { store.delete(k); },
+ clear: () => { store.clear(); },
+ });
+}
+
+beforeEach(() => {
+ vi.resetModules();
+ vi.unstubAllGlobals();
+});
+
+describe('the pick a returning window starts on', () => {
+ it('is the one that was saved', async () => {
+ windowWith({ 'estella.agent.selection': JSON.stringify(FLASH) });
+ const { useAgent } = await import('@/store/AgentStore');
+ expect(useAgent.getState().selection).toEqual(FLASH);
+ });
+
+ it('is nothing when nothing was saved', async () => {
+ windowWith({});
+ const { useAgent } = await import('@/store/AgentStore');
+ expect(useAgent.getState().selection).toBeNull();
+ });
+
+ it('is nothing, rather than a crash, when the saved value is torn', async () => {
+ windowWith({ 'estella.agent.selection': '{not json' });
+ const { useAgent } = await import('@/store/AgentStore');
+ expect(useAgent.getState().selection).toBeNull();
+ });
+
+ it('is nothing where the browser refuses storage at all', async () => {
+ vi.stubGlobal('localStorage', {
+ getItem: () => { throw new Error('access denied'); },
+ });
+ const { useAgent } = await import('@/store/AgentStore');
+ expect(useAgent.getState().selection).toBeNull();
+ });
+});