🤯 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: populate workingDirectory in local agent builtin tool dispatch (#17808)
Northwordeb65d06d0507
diff --git a/src/helpers/__tests__/effectiveWorkingDirectory.test.ts b/src/helpers/__tests__/effectiveWorkingDirectory.test.tsnew file mode 100644index 00000000000..d1836f89ca6--- /dev/null+++ b/src/helpers/__tests__/effectiveWorkingDirectory.test.ts@@ -0,0 +1,107 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { resolveEffectiveWorkingDirectory } from '../effectiveWorkingDirectory';++vi.mock('@lobechat/const', () => ({+ isDesktop: true,+}));++const mockGetElectronStoreState = vi.fn();+vi.mock('@/store/electron', () => ({+ getElectronStoreState: () => mockGetElectronStoreState(),+}));++const mockGetAgentStoreState = vi.fn();+vi.mock('@/store/agent', () => ({+ getAgentStoreState: () => mockGetAgentStoreState(),+}));++const mockCurrentAgentWorkingDirectory = vi.fn();+const mockGetAgentWorkingDirectoryById = vi.fn();+vi.mock('@/store/agent/selectors', () => ({+ agentSelectors: {+ currentAgentWorkingDirectory: (deviceId?: string) => mockCurrentAgentWorkingDirectory(deviceId),+ },+ agentByIdSelectors: {+ getAgentWorkingDirectoryById: (agentId: string, deviceId?: string) => () =>+ mockGetAgentWorkingDirectoryById(agentId, deviceId),+ },+}));++const mockGetTopicWorkingDirectory = vi.fn();+vi.mock('@/store/chat/selectors', () => ({+ topicSelectors: {+ getTopicWorkingDirectory: (topicId?: string | null) => (state: any) =>+ mockGetTopicWorkingDirectory(topicId, state),+ },+}));++describe('resolveEffectiveWorkingDirectory', () => {+ const chatState = {} as any;++ beforeEach(() => {+ vi.clearAllMocks();+ mockGetElectronStoreState.mockReturnValue({+ gatewayDeviceInfo: { deviceId: 'device-1' },+ });+ mockGetAgentStoreState.mockReturnValue({});+ });++ it('returns the topic working directory when one is configured', () => {+ mockGetTopicWorkingDirectory.mockReturnValue('/home/user/project');++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1');++ expect(result).toBe('/home/user/project');+ expect(mockGetTopicWorkingDirectory).toHaveBeenCalledWith('topic-1', chatState);+ });++ it('falls back to the active agent when no topic working directory and no agentId', () => {+ mockGetTopicWorkingDirectory.mockReturnValue(undefined);+ mockCurrentAgentWorkingDirectory.mockReturnValue(() => '/agent/default/repo');++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1');++ expect(result).toBe('/agent/default/repo');+ expect(mockCurrentAgentWorkingDirectory).toHaveBeenCalledWith('device-1');+ });++ it('falls back to the captured agent when agentId is provided', () => {+ mockGetTopicWorkingDirectory.mockReturnValue(undefined);+ mockGetAgentWorkingDirectoryById.mockReturnValue('/agent-captured/repo');++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1', 'agent-42');++ expect(result).toBe('/agent-captured/repo');+ expect(mockGetAgentWorkingDirectoryById).toHaveBeenCalledWith('agent-42', 'device-1');+ expect(mockCurrentAgentWorkingDirectory).not.toHaveBeenCalled();+ });++ it('returns undefined when no topic working directory, no agentId, and active agent has no working directory', () => {+ mockGetTopicWorkingDirectory.mockReturnValue(undefined);+ mockCurrentAgentWorkingDirectory.mockReturnValue(() => undefined);++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1');++ expect(result).toBeUndefined();+ });++ it('returns undefined when no topic working directory, agentId provided but agent has no working directory', () => {+ mockGetTopicWorkingDirectory.mockReturnValue(undefined);+ mockGetAgentWorkingDirectoryById.mockReturnValue(undefined);++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1', 'agent-42');++ expect(result).toBeUndefined();+ });++ it('prefers topic working directory over captured agentId fallback', () => {+ mockGetTopicWorkingDirectory.mockReturnValue('/topic/overrides/everything');+ mockGetAgentWorkingDirectoryById.mockReturnValue('/agent-captured/repo');++ const result = resolveEffectiveWorkingDirectory(chatState, 'topic-1', 'agent-42');++ expect(result).toBe('/topic/overrides/everything');+ expect(mockGetAgentWorkingDirectoryById).not.toHaveBeenCalled();+ });+});diff --git a/src/helpers/effectiveWorkingDirectory.ts b/src/helpers/effectiveWorkingDirectory.tsindex 1bfadb39ff1..bd8ad9261e4 100644--- a/src/helpers/effectiveWorkingDirectory.ts+++ b/src/helpers/effectiveWorkingDirectory.ts@@ -1,7 +1,7 @@import { isDesktop } from '@lobechat/const';import { getAgentStoreState } from '@/store/agent';-import { agentSelectors } from '@/store/agent/selectors';+import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';import { type ChatStoreState } from '@/store/chat/initialState';import { topicSelectors } from '@/store/chat/selectors';import { getElectronStoreState } from '@/store/electron';@@ -25,10 +25,15 @@ import { getElectronStoreState } from '@/store/electron';* bound to the topic that *started* the request, not whatever topic is active* now — the user may switch topics mid-stream. Omit it (prompt-build time) to* resolve against the active topic.+ *+ * Pass `agentId` when the caller has captured a specific agent (e.g. from the+ * operation context) so the agent-level fallback resolves against the+ * request-starting agent, not the currently active one.*/export const resolveEffectiveWorkingDirectory = (chatState: ChatStoreState,topicId?: string | null,+ agentId?: string | null,): string | undefined => {if (!isDesktop) return undefined;@@ -36,6 +41,15 @@ export const resolveEffectiveWorkingDirectory = (if (topicWorkingDir) return topicWorkingDir;const currentDeviceId = getElectronStoreState().gatewayDeviceInfo?.deviceId;+ if (agentId) {+ return (+ agentByIdSelectors.getAgentWorkingDirectoryById(+ agentId,+ currentDeviceId,+ )(getAgentStoreState()) ?? undefined+ );+ }+return (agentSelectors.currentAgentWorkingDirectory(currentDeviceId)(getAgentStoreState()) ?? undefined);diff --git a/src/store/chat/slices/agentRun/actions/transports/client/clientToolExecution.ts b/src/store/chat/slices/agentRun/actions/transports/client/clientToolExecution.tsindex 9f3e14d2d02..218a5d4ee03 100644--- a/src/store/chat/slices/agentRun/actions/transports/client/clientToolExecution.ts+++ b/src/store/chat/slices/agentRun/actions/transports/client/clientToolExecution.ts@@ -186,6 +186,7 @@ export class ClientToolExecutionActionImpl {workingDirectory: resolveEffectiveWorkingDirectory(this.#get(),topicId ?? operation?.context?.topicId,+ agentId ?? operation?.context?.agentId,),};diff --git a/src/store/chat/slices/plugin/actions/pluginTypes.ts b/src/store/chat/slices/plugin/actions/pluginTypes.tsindex d96dff41c3c..a93d1325c4b 100644--- a/src/store/chat/slices/plugin/actions/pluginTypes.ts+++ b/src/store/chat/slices/plugin/actions/pluginTypes.ts@@ -5,6 +5,7 @@ import {} from '@lobechat/types';import debug from 'debug';+import { resolveEffectiveWorkingDirectory } from '@/helpers/effectiveWorkingDirectory';import { type MCPToolCallResult } from '@/libs/mcp';import { mcpService } from '@/services/mcp';import { messageService } from '@/services/message';@@ -217,6 +218,11 @@ export class PluginTypesActionImpl {toolCallId: payload.id,toolMessageId: id,topicId,+ workingDirectory: resolveEffectiveWorkingDirectory(+ this.#get(),+ topicId ?? rootRuntimeOperationContext?.topicId,+ agentId ?? rootRuntimeOperationContext?.agentId,+ ),});log('[BuiltinToolCall] invoke:end', {