Skip to content

🀯 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.

πŸ› fix(task): open task detail portal from onAfterCall instead of render effect (#16297)

Arvin Xu9a24f3ab4503
diff --git a/packages/builtin-tool-task/src/client/Render/CreateTask/index.tsx b/packages/builtin-tool-task/src/client/Render/CreateTask/index.tsx
index 740eeda611d..98cad51866d 100644
--- a/packages/builtin-tool-task/src/client/Render/CreateTask/index.tsx
+++ b/packages/builtin-tool-task/src/client/Render/CreateTask/index.tsx
@@ -4,7 +4,7 @@ import type { BuiltinRenderProps } from '@lobechat/types';
import { ActionIcon, Block, Markdown, Text } from '@lobehub/ui';
import { createStaticStyles, cssVar } from 'antd-style';
import { PanelRight, PanelRightClose } from 'lucide-react';
-import { memo, useEffect, useRef } from 'react';
+import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import TaskPriorityTag from '@/features/AgentTasks/features/TaskPriorityTag';
@@ -14,10 +14,6 @@ import { chatPortalSelectors } from '@/store/chat/selectors';
import type { CreateTaskParams, CreateTaskState } from '../../../types';
-// Tracks task identifiers we've already auto-expanded so re-mounting the card
-// (scrolling the message back into view) doesn't reopen a portal the user closed.
-const autoOpened = new Set<string>();
-
const styles = createStaticStyles(({ css, cssVar }) => ({
description: css`
overflow: hidden;
@@ -50,8 +46,6 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
max-height: 132px;
mask-image: linear-gradient(to bottom, black 78%, transparent);
-
- mask-image: linear-gradient(to bottom, black 78%, transparent);
`,
row: css`
display: flex;
@@ -93,19 +87,9 @@ export const CreateTaskRender = memo<BuiltinRenderProps<CreateTaskParams, Create
);
const identifier = pluginState?.identifier;
- // Identifier present at first render means this card mounted already-resolved
- // (e.g. an old task when reopening the conversation) β€” only the fresh
- // undefined β†’ defined transition counts as "just created".
- const identifierAtMount = useRef(identifier);
-
- // Once the task is freshly created, auto-expand its detail in the right-side
- // portal so the user can review it without leaving the conversation.
- useEffect(() => {
- if (!identifier || identifierAtMount.current === identifier) return;
- if (autoOpened.has(identifier)) return;
- autoOpened.add(identifier);
- openTaskDetail(identifier);
- }, [identifier, openTaskDetail]);
+ // Auto-expanding the freshly created task's detail portal is driven by the
+ // executor's `onAfterCall` hook (gateway `tool_end`), not a render effect β€”
+ // see `client/executor`. The card itself only handles the manual toggle.
// Prefer the resolved task (`pluginState`); fall back to `args` while the
// call is still streaming and no result has landed yet.
diff --git a/packages/builtin-tool-task/src/client/executor/index.ts b/packages/builtin-tool-task/src/client/executor/index.ts
index da57b4c0401..b0ad00c2085 100644
--- a/packages/builtin-tool-task/src/client/executor/index.ts
+++ b/packages/builtin-tool-task/src/client/executor/index.ts
@@ -19,6 +19,7 @@ import { BaseExecutor } from '@lobechat/types';
import debug from 'debug';
import { taskService } from '@/services/task';
+import { getChatStoreState } from '@/store/chat';
import { getTaskStoreState } from '@/store/task';
import { findSubtaskParentId } from '@/store/task/slices/detail/reducer';
@@ -79,6 +80,16 @@ class TaskExecutor extends BaseExecutor<typeof TaskApiName> {
const store = getTaskStoreState();
const identifier = extractIdentifier(params, result);
+ // Auto-expand the freshly created task's detail in the right-side portal so
+ // the user can review it without leaving the conversation. This fires once
+ // per `createTask` tool call (on the gateway `tool_end` event), which is why
+ // it lives here rather than in the renderer: the gateway re-fetches and
+ // remounts the message after `tool_end`, so a render-mount effect never sees
+ // the undefined β†’ defined identifier transition and would never open.
+ if (apiName === TaskApiName.createTask && identifier) {
+ getChatStoreState().openTaskDetail(identifier);
+ }
+
// Build the set of task-detail keys to revalidate. Mirrors the pattern
// used by `updateTask` in the detail slice so subtask deletions / edits
// bubble up: when we mutate a subtask we must also refresh the parent
diff --git a/packages/builtin-tool-task/src/client/executor/onAfterCall.test.ts b/packages/builtin-tool-task/src/client/executor/onAfterCall.test.ts
new file mode 100644
index 00000000000..c839baa370c
--- /dev/null
+++ b/packages/builtin-tool-task/src/client/executor/onAfterCall.test.ts
@@ -0,0 +1,71 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { TaskApiName } from '../../types';
+
+const mocks = vi.hoisted(() => ({
+ getChatStoreState: vi.fn(),
+ getTaskStoreState: vi.fn(),
+ openTaskDetail: vi.fn(),
+}));
+
+vi.mock('@/store/chat', () => ({
+ getChatStoreState: mocks.getChatStoreState,
+}));
+
+vi.mock('@/store/task', () => ({
+ getTaskStoreState: mocks.getTaskStoreState,
+}));
+
+vi.mock('@/store/task/slices/detail/reducer', () => ({
+ findSubtaskParentId: vi.fn(() => undefined),
+}));
+
+vi.mock('@/services/task', () => ({
+ taskService: {},
+}));
+
+// Imported after mocks so the executor module resolves the stubbed stores.
+const { taskExecutor } = await import('./index');
+
+describe('TaskExecutor.onAfterCall β€” portal auto-open', () => {
+ beforeEach(() => {
+ mocks.openTaskDetail.mockClear();
+ mocks.getChatStoreState.mockReturnValue({ openTaskDetail: mocks.openTaskDetail });
+ mocks.getTaskStoreState.mockReturnValue({
+ activeTaskId: undefined,
+ internal_refreshTaskDetail: vi.fn().mockResolvedValue(undefined),
+ refreshTaskList: vi.fn().mockResolvedValue(undefined),
+ taskDetailMap: {},
+ });
+ });
+
+ it('opens the task detail portal after a successful createTask', async () => {
+ await taskExecutor.onAfterCall({
+ apiName: TaskApiName.createTask,
+ params: {},
+ result: { content: '', state: { identifier: 'task-123' }, success: true },
+ } as any);
+
+ expect(mocks.openTaskDetail).toHaveBeenCalledWith('task-123');
+ });
+
+ it('does not open the portal for non-createTask APIs', async () => {
+ await taskExecutor.onAfterCall({
+ apiName: TaskApiName.editTask,
+ params: { identifier: 'task-1' },
+ result: { content: '', state: { identifier: 'task-1' }, success: true },
+ } as any);
+
+ expect(mocks.openTaskDetail).not.toHaveBeenCalled();
+ });
+
+ it('does not open the portal when createTask failed', async () => {
+ await taskExecutor.onAfterCall({
+ apiName: TaskApiName.createTask,
+ params: {},
+ result: { content: '', success: false },
+ } as any);
+
+ expect(mocks.openTaskDetail).not.toHaveBeenCalled();
+ });
+});