π€― 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
import { getWorkingDirEffectivePath } from '@lobechat/types';
import isEqual from 'fast-deep-equal';
import { electronGitService } from '@/services/electron/git';
import { gitService } from '@/services/git';
import type { ChatStore } from '@/store/chat/store';
import {
canReadTopicGitTransport,
mergeWorkingDirGitBranch,
resolveTopicGitTransport,
} from '@/store/chat/utils/topicWorkingDirGit';
import { deviceSelectors, getDeviceStoreState } from '@/store/device';
import { topicSelectors } from '../../../topic/selectors';
/**
* Resolve the working directory's repo type the SAME way the ControlBar's
* `useRepoType` does β from the device's persisted `workingDirs[].repoType`, then
* (local machine only) a filesystem probe. Deliberately NOT read off the topic's
* stored `workingDirectoryConfig.repoType`: legacy topics persist only
* `workingDirectory` with no config, so a config-based gate would skip them and
* never refresh their branch snapshot.
*/
const resolveRepoType = async (params: {
isLocalDevice: boolean;
path: string;
targetDeviceId?: string;
}): Promise<'git' | 'github' | undefined> => {
const { isLocalDevice, path, targetDeviceId } = params;
const fromDevice = deviceSelectors
.getDeviceWorkingDirs(targetDeviceId)(getDeviceStoreState())
.find((d) => d.path === path || getWorkingDirEffectivePath(d) === path)?.repoType;
if (fromDevice) return fromDevice;
// A remote device's filesystem isn't reachable here β only probe the local one.
if (isLocalDevice) return electronGitService.detectRepoType(path);
return undefined;
};
/**
* Capture the working directory's live branch and its remote ref onto the
* topic's `workingDirectoryConfig` at SEND time.
*
* This is the sole send-time writer of the branch snapshot into topic metadata. It used
* to live as a reactive effect in the ControlBar's `GitStatus`, but that fired on
* every mount β so merely OPENING a topic re-stamped it with whatever branch the
* shared working directory happened to be on, clobbering the historical snapshot.
* Anchoring it to `afterUserMessagePersisted` matches the product contract ("the
* chat branch reflects the last-used active branch; sending a message updates
* it") and leaves `GitStatus` a pure live display of the current directory.
*
* Fire-and-forget: the device reads may be slow, so callers must not await it on
* the send path. It only ever patches topic metadata, idempotently.
*/
export const snapshotTopicWorkingDirGit = async (
get: () => ChatStore,
{ agentId, topicId }: { agentId: string; topicId: string },
): Promise<void> => {
const topic = topicSelectors.getTopicById(topicId)(get());
if (!topic) return;
const currentConfig = topic.metadata?.workingDirectoryConfig;
const path = getWorkingDirEffectivePath(currentConfig) ?? topic.metadata?.workingDirectory;
if (!path) return;
const { deviceId, isLocalDevice, targetDeviceId } = resolveTopicGitTransport(agentId);
// Same transport gate as `gitHooks.isEnabled`: a local read needs `isDesktop`,
// a remote read needs a `deviceId`. Neither β nothing to probe.
if (!canReadTopicGitTransport({ deviceId })) return;
// The branch snapshot is only kept for a GitHub repo β mirror GitStatus's
// `isGithub` gate. Resolve it live (the
// config's own `repoType` may be absent on legacy topics); the stored value is
// still a valid fast path when present.
const repoType =
currentConfig?.repoType ?? (await resolveRepoType({ isLocalDevice, path, targetDeviceId }));
if (repoType !== 'github') return;
const branchInfo = await gitService.getGitBranch({ deviceId, path });
const branch = branchInfo?.branch;
const detached = branchInfo?.detached;
// No branch ref to query (unprobed / detached HEAD) β leave the prior snapshot.
if (!branch || detached) return;
// PRs reach the topic only through the agent's own commands (server Work
// rows), so the send records the branch and its remote ref, never which PR
// `gh` guesses for the branch.
const nextConfig = mergeWorkingDirGitBranch({
branch,
currentConfig,
path,
upstream: branchInfo?.upstream,
});
if (isEqual(currentConfig, nextConfig)) return;
await get().updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
};