80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import type { CheckoutStatusPayload } from "@/hooks/use-checkout-status-query";
|
|
import {
|
|
buildHostWorkspaceRoute,
|
|
parseHostAgentRouteFromPathname,
|
|
parseHostWorkspaceTabRouteFromPathname,
|
|
} from "@/utils/host-routes";
|
|
|
|
export function parseAgentKey(
|
|
key: string | null | undefined
|
|
): { serverId: string; agentId: string } | null {
|
|
if (!key) {
|
|
return null;
|
|
}
|
|
const sep = key.lastIndexOf(":");
|
|
if (sep <= 0 || sep >= key.length - 1) {
|
|
return null;
|
|
}
|
|
const serverId = key.slice(0, sep).trim();
|
|
const agentId = key.slice(sep + 1).trim();
|
|
if (!serverId || !agentId) {
|
|
return null;
|
|
}
|
|
return { serverId, agentId };
|
|
}
|
|
|
|
export function resolveSelectedAgentForNewAgent(input: {
|
|
pathname: string;
|
|
selectedAgentId?: string;
|
|
}): { serverId: string; agentId: string } | null {
|
|
const workspaceTabRoute = parseHostWorkspaceTabRouteFromPathname(input.pathname);
|
|
if (workspaceTabRoute?.tabId?.startsWith("agent_")) {
|
|
const agentId = workspaceTabRoute.tabId.slice("agent_".length).trim();
|
|
if (agentId) {
|
|
return { serverId: workspaceTabRoute.serverId, agentId };
|
|
}
|
|
}
|
|
return (
|
|
parseHostAgentRouteFromPathname(input.pathname) ??
|
|
parseAgentKey(input.selectedAgentId)
|
|
);
|
|
}
|
|
|
|
function inferMainRepoRootFromPaseoWorktreePath(cwd: string): string | null {
|
|
const normalizedPath = cwd.replace(/\\/g, "/");
|
|
const marker = "/.paseo/worktrees";
|
|
const markerIndex = normalizedPath.indexOf(marker);
|
|
if (markerIndex <= 0) {
|
|
return null;
|
|
}
|
|
const markerEnd = markerIndex + marker.length;
|
|
const nextChar = normalizedPath[markerEnd];
|
|
if (nextChar && nextChar !== "/") {
|
|
return null;
|
|
}
|
|
const inferred = cwd.slice(0, markerIndex).replace(/[\\/]+$/, "");
|
|
return inferred.trim() ? inferred : null;
|
|
}
|
|
|
|
export function resolveNewAgentWorkingDir(
|
|
cwd: string,
|
|
checkout: CheckoutStatusPayload | null
|
|
): string {
|
|
const explicitMainRepoRoot = checkout?.isPaseoOwnedWorktree
|
|
? checkout.mainRepoRoot?.trim() || null
|
|
: null;
|
|
if (explicitMainRepoRoot) {
|
|
return explicitMainRepoRoot;
|
|
}
|
|
|
|
return inferMainRepoRootFromPaseoWorktreePath(cwd) ?? cwd;
|
|
}
|
|
|
|
export function buildNewAgentRoute(
|
|
serverId: string,
|
|
workingDir?: string | null
|
|
): string {
|
|
const trimmedWorkingDir = workingDir?.trim();
|
|
return buildHostWorkspaceRoute(serverId, trimmedWorkingDir || ".");
|
|
}
|