π€― 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(agent): guard working directory path rendering (#16822)
Arvin Xu5559775286ef
diff --git a/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx b/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsxindex 91e0307b908..b4b8502e6c0 100644--- a/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx+++ b/src/features/ChatInput/ControlBar/WorkingDirectoryPicker.tsx@@ -3,6 +3,7 @@import { isDesktop } from '@lobechat/const';import type { WorkingDirEntry } from '@lobechat/types';import { getWorkingDirEffectivePath } from '@lobechat/types';+import { isRecord } from '@lobechat/utils';import { Flexbox, Icon, Input, Popover, Tooltip } from '@lobehub/ui';import { toast } from '@lobehub/ui/base-ui';import { createStaticStyles, cssVar, cx } from 'antd-style';@@ -24,6 +25,10 @@ import {resolveAgentWorkingDirectory,resolveTargetDeviceId,} from '@/helpers/agentWorkingDirectory';+import {+ getWorkingDirectoryName,+ getWorkingDirectoryPathString,+} from '@/helpers/workingDirectoryPath';import { deviceService } from '@/services/device';import { electronSystemService } from '@/services/electron/system';import { useAgentStore } from '@/store/agent';@@ -209,7 +214,8 @@ const styles = createStaticStyles(({ css }) => ({`,}));-const getDirName = (path: string) => path.split('/').findLast(Boolean) || path;+const isValidWorkingDirEntry = (entry: unknown): entry is WorkingDirEntry =>+ isRecord(entry) && !!getWorkingDirectoryPathString(entry.path);type FolderEntry = { path: string; repoType?: 'git' | 'github' };@@ -317,19 +323,23 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =// The local machine's filesystem is browsable; a remote device's is not.const isLocalDevice = isDesktop && !!targetDeviceId && targetDeviceId === currentDeviceId;- const recents = useDeviceStore(deviceSelectors.getDeviceWorkingDirs(targetDeviceId));- const deviceDefaultCwd = useDeviceStore(deviceSelectors.getDeviceDefaultCwd(targetDeviceId));- const topicWorkingDirectory = useChatStore(topicSelectors.currentTopicWorkingDirectory);+ const rawRecents = useDeviceStore(deviceSelectors.getDeviceWorkingDirs(targetDeviceId));+ const recents = useMemo(() => rawRecents.filter(isValidWorkingDirEntry), [rawRecents]);+ const rawDeviceDefaultCwd = useDeviceStore(deviceSelectors.getDeviceDefaultCwd(targetDeviceId));+ const deviceDefaultCwd = getWorkingDirectoryPathString(rawDeviceDefaultCwd);+ const rawTopicWorkingDirectory = useChatStore(topicSelectors.currentTopicWorkingDirectory);+ const topicWorkingDirectory = getWorkingDirectoryPathString(rawTopicWorkingDirectory);const topicWorkingDirectoryConfig = useChatStore((s) => topicSelectors.currentTopicMetadata(s)?.workingDirectoryConfig,);- const legacyAgentWorkingDirectory = useAgentStore(+ const rawLegacyAgentWorkingDirectory = useAgentStore((s) => s.localAgentWorkingDirectoryMap[agentId],);+ const legacyAgentWorkingDirectory = getWorkingDirectoryPathString(rawLegacyAgentWorkingDirectory);// The explicitly-selected cwd (no home fallback) β drives the active check and// the Reset affordance.- const selectedDir = resolveAgentWorkingDirectory({+ const resolvedSelectedDir = resolveAgentWorkingDirectory({agencyConfig,currentDeviceId,deviceDefaultCwd,@@ -337,6 +347,7 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =topicWorkingDirectory,topicWorkingDirectoryConfig,});+ const selectedDir = getWorkingDirectoryPathString(resolvedSelectedDir);// Reset only makes sense when an agent-level override exists. The device-wide// `deviceDefaultCwd` isn't clearable from here (it's the fallback itself), so@@ -359,7 +370,7 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =if (!query) return recents;return recents.filter((entry) =>- getDirName(entry.path).toLowerCase().includes(query) ||+ getWorkingDirectoryName(entry.path)?.toLowerCase().includes(query) ||entry.path.toLowerCase().includes(query),);}, [recents, search]);@@ -405,7 +416,9 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =variant: 'text',},],- title: t('workingDirectory.removed', { name: getDirName(entry.path) }),+ title: t('workingDirectory.removed', {+ name: getWorkingDirectoryName(entry.path) ?? entry.path,+ }),});};@@ -431,7 +444,9 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =<DirIcon repoType={entry.repoType} /><Flexbox flex={1} style={{ minWidth: 0 }}><Flexbox horizontal align={'center'} gap={6}>- <div className={styles.dirName}>{getDirName(entry.path)}</div>+ <div className={styles.dirName}>+ {getWorkingDirectoryName(entry.path) ?? entry.path}+ </div>{isDefault && (<span className={styles.badge}>{t('workingDirectory.defaultBadge')}</span>)}@@ -517,7 +532,9 @@ const WorkingDirectoryPicker = memo<WorkingDirectoryPickerProps>(({ agentId }) =</Flexbox>);- const displayName = selectedDir ? getDirName(selectedDir) : t('workingDirectory.notSet');+ const displayName = selectedDir+ ? (getWorkingDirectoryName(selectedDir) ?? selectedDir)+ : t('workingDirectory.notSet');const trigger = (<div className={styles.button}>diff --git a/src/features/ChatInput/ControlBar/WorkingDirectorySection.tsx b/src/features/ChatInput/ControlBar/WorkingDirectorySection.tsxindex 5e4b7682622..799f79f7bbf 100644--- a/src/features/ChatInput/ControlBar/WorkingDirectorySection.tsx+++ b/src/features/ChatInput/ControlBar/WorkingDirectorySection.tsx@@ -1,10 +1,12 @@'use client';import { isDesktop } from '@lobechat/const';-import { getWorkingDirEffectivePath } from '@lobechat/types';+import { isRecord } from '@lobechat/utils';import { memo } from 'react';+import SafeBoundary from '@/components/ErrorBoundary';import { resolveTargetDeviceId } from '@/helpers/agentWorkingDirectory';+import { getWorkingDirectoryPathString } from '@/helpers/workingDirectoryPath';import { useEffectiveWorkingDirectory } from '@/hooks/useEffectiveWorkingDirectory';import { useAgentStore } from '@/store/agent';import { agentByIdSelectors } from '@/store/agent/selectors';@@ -19,31 +21,43 @@ interface WorkingDirectorySectionProps {agentId: string;}+const getEntryEffectivePath = (entry: unknown) => {+ if (!isRecord(entry)) return;++ const sourcePath = getWorkingDirectoryPathString(entry.path);+ const git = isRecord(entry.git) ? entry.git : undefined;+ return getWorkingDirectoryPathString(git?.activeWorktree) ?? sourcePath;+};+/*** Working directory + git status, shared by the agent runtime bars. The unified* picker handles local and remote targets alike; git status shows for both β the* local machine probes its own filesystem, a remote device answers over RPC* (read-only) via GitStatus's `deviceId`.*/-const WorkingDirectorySection = memo<WorkingDirectorySectionProps>(({ agentId }) => {+const WorkingDirectorySectionInner = memo<WorkingDirectorySectionProps>(({ agentId }) => {const agencyConfig = useAgentStore(agentByIdSelectors.getAgencyConfigById(agentId));const currentDeviceId = useElectronStore((s) => s.gatewayDeviceInfo?.deviceId);const targetDeviceId = resolveTargetDeviceId(agencyConfig, currentDeviceId);const isLocalDevice = isDesktop && !!targetDeviceId && targetDeviceId === currentDeviceId;- const effectiveWorkingDirectory = useEffectiveWorkingDirectory(agentId);+ const rawEffectiveWorkingDirectory = useEffectiveWorkingDirectory(agentId);+ const effectiveWorkingDirectory = getWorkingDirectoryPathString(rawEffectiveWorkingDirectory);// Local machine probes the filesystem for repoType; a remote device's repoType// comes from the cached `workingDirs` entry (we can't probe a remote fs here).const localRepoType = useRepoType(isLocalDevice ? effectiveWorkingDirectory : undefined);const deviceDirs = useDeviceStore(deviceSelectors.getDeviceWorkingDirs(targetDeviceId));- const currentEntry = deviceDirs.find(- (d) =>- d.path === effectiveWorkingDirectory ||- getWorkingDirEffectivePath(d) === effectiveWorkingDirectory,- );- const sourceWorkingDirectory = currentEntry?.path ?? effectiveWorkingDirectory;- const remoteRepoType = currentEntry?.repoType;+ const currentEntry = effectiveWorkingDirectory+ ? deviceDirs.find((entry) => getEntryEffectivePath(entry) === effectiveWorkingDirectory)+ : undefined;+ const sourceWorkingDirectory =+ (isRecord(currentEntry) ? getWorkingDirectoryPathString(currentEntry.path) : undefined) ??+ effectiveWorkingDirectory;+ const remoteRepoType =+ currentEntry?.repoType === 'git' || currentEntry?.repoType === 'github'+ ? currentEntry.repoType+ : undefined;const repoType = isLocalDevice ? localRepoType : remoteRepoType;return (@@ -62,6 +76,14 @@ const WorkingDirectorySection = memo<WorkingDirectorySectionProps>(({ agentId }));});+WorkingDirectorySectionInner.displayName = 'WorkingDirectorySectionInner';++const WorkingDirectorySection = memo<WorkingDirectorySectionProps>(({ agentId }) => (+ <SafeBoundary minHeight={22} resetKeys={[agentId]}>+ <WorkingDirectorySectionInner agentId={agentId} />+ </SafeBoundary>+));+WorkingDirectorySection.displayName = 'WorkingDirectorySection';export default WorkingDirectorySection;diff --git a/src/helpers/workingDirectoryPath.ts b/src/helpers/workingDirectoryPath.tsnew file mode 100644index 00000000000..18b55cbb78a--- /dev/null+++ b/src/helpers/workingDirectoryPath.ts@@ -0,0 +1,15 @@+import { pickString } from '@lobechat/utils';++export const getWorkingDirectoryPathString = (path: unknown) => {+ const value = pickString(path)?.trim();+ return value || undefined;+};++// Last non-empty path segment β the folder name. Also yields the repo name for+// a web github URL (".../owner/repo" -> "repo").+export const getWorkingDirectoryName = (path: unknown) => {+ const value = getWorkingDirectoryPathString(path);+ if (!value) return;++ return value.replaceAll('\\', '/').split('/').findLast(Boolean) || value;+};diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/index.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/index.tsxindex 6b5c81e53b5..ab97215f1ab 100644--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/index.tsx+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/index.tsx@@ -20,6 +20,7 @@ import DirIcon from '@/features/ChatInput/ControlBar/DirIcon';import { useHasDraft } from '@/features/ChatInput/draftStorage';import NavItem from '@/features/NavPanel/components/NavItem';import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';+import { getWorkingDirectoryName } from '@/helpers/workingDirectoryPath';import { getPlatformIcon } from '@/routes/(main)/agent/channel/const';import { useAgentStore } from '@/store/agent';import { agentSelectors } from '@/store/agent/selectors';@@ -120,20 +121,18 @@ const cancelPendingSingleClick = () => {}};-// Last non-empty path segment β the folder name. Also yields the repo name for-// a web github URL (".../owner/repo" β "repo").-const getDirName = (path: string) => path.split('/').findLast(Boolean) || path;-const getWorkingDirectoryDisplay = (metadata: ChatTopicMetadata | undefined) => {const config = metadata?.workingDirectoryConfig;const workingDirectory = getTopicMetadataWorkingDirectoryEffectivePath(metadata);if (!workingDirectory) return;const branch = config?.git?.branch;- const dirName = getDirName(workingDirectory);+ const dirName = getWorkingDirectoryName(workingDirectory);+ if (!dirName) return;+const sourcePath = getTopicMetadataWorkingDirectorySourcePath(metadata);const sourceName =- sourcePath && sourcePath !== workingDirectory ? getDirName(sourcePath) : undefined;+ sourcePath && sourcePath !== workingDirectory ? getWorkingDirectoryName(sourcePath) : undefined;const pathLabel = sourceName && sourceName !== dirName ? `${sourceName}/${dirName}` : dirName;return {diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/metaCardData.ts b/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/metaCardData.tsindex 97160a60b34..0e1450e1c2b 100644--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/metaCardData.ts+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/List/Item/metaCardData.ts@@ -8,9 +8,7 @@ import type { LucideIcon } from 'lucide-react';import { GitMerge, GitPullRequestArrow, GitPullRequestClosed } from 'lucide-react';import { isDesktop } from '@/const/version';--const getDirName = (path: string) =>- path.replaceAll('\\', '/').split('/').findLast(Boolean) || path;+import { getWorkingDirectoryName } from '@/helpers/workingDirectoryPath';export type PullRequestState = 'open' | 'merged' | 'closed';@@ -59,8 +57,8 @@ export const getTopicMetaCard = (metadata: ChatTopicMetadata | undefined) => {branch: git.branch,detached: git.detached,pullRequest: git.github?.pullRequest ?? undefined,- repoName: sourcePath ? getDirName(sourcePath) : undefined,+ repoName: sourcePath ? getWorkingDirectoryName(sourcePath) : undefined,repoType: config.repoType ?? (isDesktop ? undefined : ('github' as const)),- worktreeName: isWorktree && effectivePath ? getDirName(effectivePath) : undefined,+ worktreeName: isWorktree && effectivePath ? getWorkingDirectoryName(effectivePath) : undefined,};};