🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(chat-input): load an unlisted composer topic before resolving its model pin (#20968)
Mocha8e731cdb344e
diff --git a/src/features/ChatInput/ActionBar/Model/useTopicModelSwitch.test.ts b/src/features/ChatInput/ActionBar/Model/useTopicModelSwitch.test.tsindex e4272037f38..9fe16bb673c 100644--- a/src/features/ChatInput/ActionBar/Model/useTopicModelSwitch.test.ts+++ b/src/features/ChatInput/ActionBar/Model/useTopicModelSwitch.test.ts@@ -28,6 +28,7 @@ vi.mock('@/store/chat', () => ({selector({activeTopicId: state.globalActiveTopicId,topicDataMap: {},+ useFetchTopicDetail: () => {},topicDetailMap: Object.fromEntries(Object.entries(state.topicModels).map(([id, pin]) => [id, { id, ...pin }]),),diff --git a/src/features/ChatInput/ActionBar/Params/useParamsModelConfig.test.ts b/src/features/ChatInput/ActionBar/Params/useParamsModelConfig.test.tsindex 60932992107..f9651514afd 100644--- a/src/features/ChatInput/ActionBar/Params/useParamsModelConfig.test.ts+++ b/src/features/ChatInput/ActionBar/Params/useParamsModelConfig.test.ts@@ -44,6 +44,7 @@ vi.mock('@/store/chat', () => ({selector({activeTopicId: testState.chat.activeTopicId,topicDataMap: {},+ useFetchTopicDetail: () => {},topicDetailMap: Object.fromEntries(Object.entries(testState.chat.topicModels).map(([id, pin]) => [id, { id, ...pin }]),),diff --git a/src/features/ChatInput/ActionBar/Token/useShowContextWindow.test.ts b/src/features/ChatInput/ActionBar/Token/useShowContextWindow.test.tsindex f3d0043f093..3e4c9c4697c 100644--- a/src/features/ChatInput/ActionBar/Token/useShowContextWindow.test.ts+++ b/src/features/ChatInput/ActionBar/Token/useShowContextWindow.test.ts@@ -13,8 +13,11 @@ interface EnabledAiModel extends ModelPin {}const state = vi.hoisted(() => ({+ /** An active topic that no list or detail holds yet. */+ coldTopicId: undefined as string | undefined,enabledAiModels: [] as { contextWindowTokens?: number; id: string; providerId: string }[],topicModel: undefined as ModelPin | undefined,+ useFetchTopicDetail: vi.fn(),}));vi.mock('../../hooks/useAgentId', () => ({ useAgentId: () => 'agent-1' }));@@ -35,8 +38,9 @@ vi.mock('@/store/agent', () => ({vi.mock('@/store/chat', () => ({useChatStore: (selector: (s: object) => unknown) =>selector({- activeTopicId: state.topicModel ? 'topic-1' : undefined,+ activeTopicId: state.topicModel ? 'topic-1' : state.coldTopicId,topicDataMap: {},+ useFetchTopicDetail: state.useFetchTopicDetail,topicDetailMap: state.topicModel ? { 'topic-1': { id: 'topic-1', ...state.topicModel } } : {},}),}));@@ -58,7 +62,9 @@ const setEnabledModels = (...models: EnabledAiModel[]) => {const topicModel = { model: 'topic-model', provider: 'topic-provider' };beforeEach(() => {+ state.coldTopicId = undefined;state.topicModel = undefined;+ state.useFetchTopicDetail.mockReset();setEnabledModels({ contextWindowTokens: 8000, model: 'agent-model', provider: 'agent-provider' });});@@ -92,4 +98,24 @@ describe('useShowContextWindow', () => {expect(renderHook(() => useShowContextWindow()).result.current).toBe(false);});++ // `Token` returns before `TokenTag` (and its token-breakdown fetch) mounts+ // while this is false, so the gate itself has to load the topic it judges.+ it('loads a cold unlisted topic by id so its pinned model can unlock the ring', () => {+ state.coldTopicId = 'cold-topic';+ setEnabledModels(+ { model: 'agent-model', provider: 'agent-provider' },+ { contextWindowTokens: 16_000, ...topicModel },+ );++ const { rerender, result } = renderHook(() => useShowContextWindow());++ expect(state.useFetchTopicDetail).toHaveBeenLastCalledWith('cold-topic');+ expect(result.current).toBe(false);++ state.topicModel = topicModel;+ rerender();++ expect(result.current).toBe(true);+ });});diff --git a/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.test.ts b/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.test.tsindex 599e2e5df6b..a5e75e51d8b 100644--- a/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.test.ts+++ b/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.test.ts@@ -8,7 +8,6 @@ const state = vi.hoisted(() => ({/** `undefined`: the host publishes no topic; `null`: its conversation has none. */composerTopicId: undefined as string | null | undefined,summaries: {} as Record<string, string>,- useFetchTopicDetail: vi.fn(),}));// One token per character, so a count names the text it was taken from.@@ -22,6 +21,8 @@ vi.mock('@/helpers/toolEngineering', () => ({}));vi.mock('../../hooks/useAgentId', () => ({ useAgentId: () => 'agent-1' }));+// `useEffectiveModel` owns loading the composer topic by id (see `useTopicModel`);+// here the topic lands in `topicDetailMap` the way that fetch would put it.vi.mock('../../hooks/useEffectiveModel', () => ({useEffectiveModel: () => ({ model: 'model-1', provider: 'provider-1' }),}));@@ -74,7 +75,6 @@ vi.mock('@/store/chat', () => ({selector({activeTopicId: state.activeTopicId,topicDataMap: {},- useFetchTopicDetail: state.useFetchTopicDetail,topicDetailMap: Object.fromEntries(Object.entries(state.summaries).map(([id, historySummary]) => [id, { historySummary, id }]),),@@ -86,7 +86,6 @@ beforeEach(() => {state.activeTopicId = 'main-topic';state.composerTopicId = undefined;state.summaries = { 'main-topic': 'main column summary', 'pane-topic': 'pane' };- state.useFetchTopicDetail.mockReset();});describe('useTokenBreakdown', () => {@@ -114,12 +113,12 @@ describe('useTokenBreakdown', () => {// A document's chat panel runs on a `trigger='document'` system topic that// never reaches the loaded lists, yet the run includes its persisted summary.- it('loads an unlisted composer topic by id and counts its summary once it arrives', () => {+ it('counts an unlisted composer topic summary once its detail arrives', () => {state.composerTopicId = 'doc-topic';const { rerender, result } = renderHook(() => useTokenBreakdown());- expect(state.useFetchTopicDetail).toHaveBeenLastCalledWith('doc-topic');+ expect(result.current.historySummaryToken).toBe(0);state.summaries['doc-topic'] = 'doc summary';rerender();@@ -128,14 +127,13 @@ describe('useTokenBreakdown', () => {});// `/goal/:goalId` makes a sidebar-excluded supervision topic globally active- // with no `useFetchActiveTopicDetail` mounted, so the composer must load it.- it('loads the global active topic by id when no list or detail holds it', () => {+ // with no `useFetchActiveTopicDetail` mounted.+ it('counts the global active topic summary once its detail arrives', () => {state.composerTopicId = 'main-topic';delete state.summaries['main-topic'];const { rerender, result } = renderHook(() => useTokenBreakdown());- expect(state.useFetchTopicDetail).toHaveBeenLastCalledWith('main-topic');expect(result.current.historySummaryToken).toBe(0);state.summaries['main-topic'] = 'goal summary';@@ -143,17 +141,4 @@ describe('useTokenBreakdown', () => {expect(result.current.historySummaryToken).toBe('goal summary'.length);});-- it('requests no detail for a topic that is already loaded or when there is none', () => {- state.composerTopicId = 'pane-topic';- const { rerender } = renderHook(() => useTokenBreakdown());-- state.composerTopicId = null;- rerender();-- state.composerTopicId = undefined;- rerender();-- for (const [id] of state.useFetchTopicDetail.mock.calls) expect(id).toBeUndefined();- });});diff --git a/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.ts b/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.tsindex 7013dbed54f..77010fa94de 100644--- a/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.ts+++ b/src/features/ChatInput/ActionBar/Token/useTokenBreakdown.ts@@ -87,17 +87,11 @@ export const useTokenBreakdown = (): TokenBreakdown => {// the global active topic's: an embedded composer would otherwise count the// outer chat's history against its context window.const topicId = useTopicId();- const [historySummary, isUnlistedTopic, useFetchTopicDetail] = useChatStore((s) => {- const topic = topicId ? topicSelectors.getTopicById(topicId)(s) : undefined;- return [topic?.historySummary || '', !!topicId && !topic, s.useFetchTopicDetail] as const;- });- // A topic that never reaches the loaded lists (a document's chat panel runs on- // a `trigger='document'` system topic, a goal's supervision topic is excluded- // from the sidebar) still has a persisted summary the run includes, so load it- // by id instead of counting it as empty. This holds for the global active topic- // too: not every route mounts `useFetchActiveTopicDetail`, and the by-id fetch- // dedupes with it where it does.- useFetchTopicDetail(isUnlistedTopic ? topicId : undefined);+ // The topic itself is loaded by id in `useTopicModel` (via `useEffectiveModel`+ // below), so a sidebar-excluded topic's persisted summary arrives here too.+ const historySummary = useChatStore((s) =>+ topicId ? topicSelectors.getTopicById(topicId)(s)?.historySummary || '' : '',+ );const agentId = useAgentId();const { model, provider } = useEffectiveModel(agentId);diff --git a/src/features/ChatInput/InputEditor/index.test.tsx b/src/features/ChatInput/InputEditor/index.test.tsxindex a6dc45ddda2..8687fb9112c 100644--- a/src/features/ChatInput/InputEditor/index.test.tsx+++ b/src/features/ChatInput/InputEditor/index.test.tsx@@ -159,9 +159,12 @@ vi.mock('@/services/aiChat', () => ({},}));vi.mock('@/store/chat', () => ({- useChatStore: Object.assign(<T,>(selector: StoreSelector<T>) => selector({}), {- getState: () => ({ activeTopicId: undefined }),- }),+ useChatStore: Object.assign(+ <T,>(selector: StoreSelector<T>) => selector({ useFetchTopicDetail: () => {} }),+ {+ getState: () => ({ activeTopicId: undefined }),+ },+ ),}));vi.mock('@/store/serverConfig', () => ({useServerConfigStore: <T,>(selector: StoreSelector<T>) =>diff --git a/src/features/ChatInput/hooks/useEffectiveAgentMode.test.ts b/src/features/ChatInput/hooks/useEffectiveAgentMode.test.tsindex 14a9dacc766..dea5f193152 100644--- a/src/features/ChatInput/hooks/useEffectiveAgentMode.test.ts+++ b/src/features/ChatInput/hooks/useEffectiveAgentMode.test.ts@@ -25,6 +25,7 @@ const testState = vi.hoisted(() => ({toolUseByModel: {} as Record<string, boolean>,topic: {pinned: undefined as { model: string; provider: string } | undefined,+ useFetchTopicDetail: vi.fn(),},user: {fetchedPreference: undefined as@@ -79,6 +80,7 @@ vi.mock('@/store/chat', () => ({vi.mock('@/store/chat/slices/topic/selectors', () => ({topicSelectors: {+ getTopicById: () => (s: typeof testState.topic) => s.pinned,getTopicModelById: () => (s: typeof testState.topic) => s.pinned,},}));diff --git a/src/features/ChatInput/hooks/useEffectiveModel.test.ts b/src/features/ChatInput/hooks/useEffectiveModel.test.tsindex f8b7a2b27d1..b0f326cfda3 100644--- a/src/features/ChatInput/hooks/useEffectiveModel.test.ts+++ b/src/features/ChatInput/hooks/useEffectiveModel.test.ts@@ -13,6 +13,7 @@ const testState = vi.hoisted(() => ({composerTopicId: undefined as string | null | undefined,globalActiveTopicId: undefined as string | undefined,topicModels: {} as Record<string, ModelPin>,+ useFetchTopicDetail: vi.fn(),}));vi.mock('../store', () => ({@@ -33,6 +34,7 @@ vi.mock('@/store/chat', () => ({selector({activeTopicId: testState.globalActiveTopicId,topicDataMap: {},+ useFetchTopicDetail: testState.useFetchTopicDetail,topicDetailMap: Object.fromEntries(Object.entries(testState.topicModels).map(([id, pin]) => [id, { id, ...pin }]),),@@ -46,6 +48,7 @@ beforeEach(() => {testState.composerTopicId = undefined;testState.globalActiveTopicId = 'outer-topic';testState.topicModels = { 'outer-topic': outerPin, 'own-topic': ownPin };+ testState.useFetchTopicDetail.mockReset();});describe('useEffectiveModel', () => {@@ -80,4 +83,39 @@ describe('useEffectiveModel', () => {expect(result.current).toEqual({ model: 'agent-model', provider: 'agent-provider' });});++ // A document's chat panel runs on a `trigger='document'` system topic and a+ // goal's supervision topic is sidebar-excluded: neither reaches the loaded+ // lists, yet the run reads the persisted pin.+ it('loads a cold unlisted composer topic by id and switches to its pin once it lands', () => {+ testState.composerTopicId = 'cold-topic';++ const { rerender, result } = renderHook(() => useEffectiveModel('agent-1'));++ expect(testState.useFetchTopicDetail).toHaveBeenLastCalledWith('cold-topic');+ expect(result.current).toEqual({ model: 'agent-model', provider: 'agent-provider' });++ testState.topicModels['cold-topic'] = ownPin;+ rerender();++ expect(result.current).toEqual(ownPin);+ });++ it('loads the global active topic by id when no list or detail holds it', () => {+ testState.globalActiveTopicId = 'cold-topic';++ renderHook(() => useEffectiveModel('agent-1'));++ expect(testState.useFetchTopicDetail).toHaveBeenLastCalledWith('cold-topic');+ });++ it('requests no detail for a topic that is already loaded or when there is none', () => {+ testState.composerTopicId = 'own-topic';+ const { rerender } = renderHook(() => useEffectiveModel('agent-1'));++ testState.composerTopicId = null;+ rerender();++ for (const [id] of testState.useFetchTopicDetail.mock.calls) expect(id).toBeUndefined();+ });});diff --git a/src/features/ChatInput/hooks/useEffectiveModel.ts b/src/features/ChatInput/hooks/useEffectiveModel.tsindex 3e066c56ac3..01bbac739f9 100644--- a/src/features/ChatInput/hooks/useEffectiveModel.ts+++ b/src/features/ChatInput/hooks/useEffectiveModel.ts@@ -19,11 +19,29 @@ interface ModelAndProvider {* The model pinned to this composer's own topic (see `useTopicId`), never the* global active topic's: a page copilot embedded beside another chat has* `topicId: null` and must not pick up the outer chat's pin.+ *+ * Also the single place the composer loads its topic by id. A topic that never+ * reaches the loaded lists (a document's chat panel runs on a+ * `trigger='document'` system topic, a goal's supervision topic is excluded from+ * the sidebar) still has a persisted pin and summary the run uses, and not every+ * route mounts `useFetchActiveTopicDetail`. Doing it here, rather than in a+ * leaf like the token tag, means every consumer of the model (and the gates that+ * decide whether such a leaf mounts at all) triggers the load; the by-id fetch+ * dedupes across callers and with `useFetchActiveTopicDetail`.*/export const useTopicModel = (): ModelAndProvider | undefined => {const topicId = useTopicId();- return useChatStore((s) => (topicId ? topicSelectors.getTopicModelById(topicId)(s) : undefined));+ const topicModel = useChatStore((s) =>+ topicId ? topicSelectors.getTopicModelById(topicId)(s) : undefined,+ );+ const isUnlistedTopic = useChatStore(+ (s) => !!topicId && !topicSelectors.getTopicById(topicId)(s),+ );+ const useFetchTopicDetail = useChatStore((s) => s.useFetchTopicDetail);+ useFetchTopicDetail(isUnlistedTopic ? topicId : undefined);++ return topicModel;};/**