Add filesystem browse API and command palette project picker (#2024)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
35 files changed
+2604
-322
No files matched your search
@@ -18,7 +18,7 @@ import {
|
||||
safeStorage,
|
||||
shell,
|
||||
} from "electron";
|
||||
import type { MenuItemConstructorOptions } from "electron";
|
||||
import type { MenuItemConstructorOptions, OpenDialogOptions } from "electron";
|
||||
import type {
|
||||
ClientSettings,
|
||||
DesktopTheme,
|
||||
@@ -126,6 +126,32 @@ const APP_RUN_ID = Crypto.randomBytes(6).toString("hex");
|
||||
const SERVER_SETTINGS_PATH = Path.join(STATE_DIR, "settings.json");
|
||||
const AUTO_UPDATE_STARTUP_DELAY_MS = 15_000;
|
||||
const AUTO_UPDATE_POLL_INTERVAL_MS = 4 * 60 * 60 * 1000;
|
||||
|
||||
function resolvePickFolderDefaultPath(rawOptions: unknown): string | undefined {
|
||||
if (typeof rawOptions !== "object" || rawOptions === null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { initialPath } = rawOptions as { initialPath?: unknown };
|
||||
if (typeof initialPath !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const trimmedPath = initialPath.trim();
|
||||
if (trimmedPath.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (trimmedPath === "~") {
|
||||
return OS.homedir();
|
||||
}
|
||||
|
||||
if (trimmedPath.startsWith("~/") || trimmedPath.startsWith("~\\")) {
|
||||
return Path.join(OS.homedir(), trimmedPath.slice(2));
|
||||
}
|
||||
|
||||
return Path.resolve(trimmedPath);
|
||||
}
|
||||
const DESKTOP_LOOPBACK_HOST = "127.0.0.1";
|
||||
const DESKTOP_REQUIRED_PORT_PROBE_HOSTS = ["0.0.0.0", "::"] as const;
|
||||
const TITLEBAR_HEIGHT = 40;
|
||||
@@ -1641,15 +1667,16 @@ function registerIpcHandlers(): void {
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(PICK_FOLDER_CHANNEL);
|
||||
ipcMain.handle(PICK_FOLDER_CHANNEL, async () => {
|
||||
ipcMain.handle(PICK_FOLDER_CHANNEL, async (_event, rawOptions: unknown) => {
|
||||
const owner = BrowserWindow.getFocusedWindow() ?? mainWindow;
|
||||
const defaultPath = resolvePickFolderDefaultPath(rawOptions);
|
||||
const openDialogOptions: OpenDialogOptions = {
|
||||
properties: ["openDirectory", "createDirectory"],
|
||||
...(defaultPath ? { defaultPath } : {}),
|
||||
};
|
||||
const result = owner
|
||||
? await dialog.showOpenDialog(owner, {
|
||||
properties: ["openDirectory", "createDirectory"],
|
||||
})
|
||||
: await dialog.showOpenDialog({
|
||||
properties: ["openDirectory", "createDirectory"],
|
||||
});
|
||||
? await dialog.showOpenDialog(owner, openDialogOptions)
|
||||
: await dialog.showOpenDialog(openDialogOptions);
|
||||
if (result.canceled) return null;
|
||||
return result.filePaths[0] ?? null;
|
||||
});
|
||||
|
||||
@@ -53,7 +53,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
ipcRenderer.invoke(REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId),
|
||||
getServerExposureState: () => ipcRenderer.invoke(GET_SERVER_EXPOSURE_STATE_CHANNEL),
|
||||
setServerExposureMode: (mode) => ipcRenderer.invoke(SET_SERVER_EXPOSURE_MODE_CHANNEL, mode),
|
||||
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
|
||||
pickFolder: (options) => ipcRenderer.invoke(PICK_FOLDER_CHANNEL, options),
|
||||
confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
|
||||
setTheme: (theme) => ipcRenderer.invoke(SET_THEME_CHANNEL, theme),
|
||||
showContextMenu: (items, position) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items, position),
|
||||
|
||||
@@ -28,10 +28,31 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
|
||||
),
|
||||
);
|
||||
|
||||
const normalizeProjectWorkspaceRootForCreate = (
|
||||
workspaceRoot: string,
|
||||
createIfMissing: boolean | undefined,
|
||||
) =>
|
||||
workspacePaths
|
||||
.normalizeWorkspaceRoot(workspaceRoot, {
|
||||
createIfMissing: createIfMissing === true,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestrationDispatchCommandError({
|
||||
message: cause.message,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (command.type === "project.create") {
|
||||
return {
|
||||
...command,
|
||||
workspaceRoot: yield* normalizeProjectWorkspaceRoot(command.workspaceRoot),
|
||||
workspaceRoot: yield* normalizeProjectWorkspaceRootForCreate(
|
||||
command.workspaceRoot,
|
||||
command.createWorkspaceRootIfMissing,
|
||||
),
|
||||
createWorkspaceRootIfMissing: command.createWorkspaceRootIfMissing === true,
|
||||
} satisfies OrchestrationCommand;
|
||||
}
|
||||
|
||||
|
||||
@@ -2039,6 +2039,40 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("creates a missing workspace root during websocket project.create dispatch", () =>
|
||||
Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ws-project-create-" });
|
||||
const missingWorkspaceRoot = path.join(parentDir, "nested", "new-project");
|
||||
|
||||
yield* buildAppUnderTest();
|
||||
|
||||
const wsUrl = yield* getWsServerUrl("/ws");
|
||||
const response = yield* Effect.scoped(
|
||||
withWsRpcClient(wsUrl, (client) =>
|
||||
client[ORCHESTRATION_WS_METHODS.dispatchCommand]({
|
||||
type: "project.create",
|
||||
commandId: CommandId.make("cmd-project-create-missing-root"),
|
||||
projectId: ProjectId.make("project-create-missing-root"),
|
||||
title: "New Project",
|
||||
workspaceRoot: missingWorkspaceRoot,
|
||||
createWorkspaceRootIfMissing: true,
|
||||
defaultModelSelection: {
|
||||
provider: "codex",
|
||||
model: "gpt-5-codex",
|
||||
},
|
||||
createdAt: new Date().toISOString(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
const stat = yield* fs.stat(missingWorkspaceRoot);
|
||||
|
||||
assert.isAtLeast(response.sequence, 0);
|
||||
assert.equal(stat.type, "Directory");
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("routes websocket rpc projects.writeFile errors", () =>
|
||||
Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
|
||||
@@ -176,12 +176,14 @@ it.layer(NodeServices.layer)("server settings", (it) => {
|
||||
const serverSettings = yield* ServerSettingsService;
|
||||
|
||||
const next = yield* serverSettings.updateSettings({
|
||||
addProjectBaseDirectory: " ~/Development ",
|
||||
observability: {
|
||||
otlpTracesUrl: " http://localhost:4318/v1/traces ",
|
||||
otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(next.addProjectBaseDirectory, "~/Development");
|
||||
assert.deepEqual(next.observability, {
|
||||
otlpTracesUrl: "http://localhost:4318/v1/traces",
|
||||
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
|
||||
@@ -215,6 +217,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
|
||||
const serverConfig = yield* ServerConfig;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const next = yield* serverSettings.updateSettings({
|
||||
addProjectBaseDirectory: "~/Development",
|
||||
observability: {
|
||||
otlpTracesUrl: "http://localhost:4318/v1/traces",
|
||||
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
|
||||
@@ -230,6 +233,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
|
||||
|
||||
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
|
||||
assert.deepEqual(JSON.parse(raw), {
|
||||
addProjectBaseDirectory: "~/Development",
|
||||
observability: {
|
||||
otlpTracesUrl: "http://localhost:4318/v1/traces",
|
||||
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
|
||||
|
||||
@@ -68,6 +68,11 @@ const searchWorkspaceEntries = (input: { cwd: string; query: string; limit: numb
|
||||
return yield* workspaceEntries.search(input);
|
||||
});
|
||||
|
||||
const appendSeparator = (input: string) =>
|
||||
input.endsWith("/") || input.endsWith("\\")
|
||||
? input
|
||||
: `${input}${process.platform === "win32" ? "\\" : "/"}`;
|
||||
|
||||
it.layer(TestLayer)("WorkspaceEntriesLive", (it) => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
@@ -275,4 +280,85 @@ it.layer(TestLayer)("WorkspaceEntriesLive", (it) => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("browse", () => {
|
||||
it.effect("returns matching directories and excludes files", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspaceEntries = yield* WorkspaceEntries;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-prefix-" });
|
||||
yield* writeTextFile(cwd, "alphabet.txt", "ignore me");
|
||||
yield* writeTextFile(cwd, "alpha/index.ts", "export {};\n");
|
||||
yield* writeTextFile(cwd, "alpine/index.ts", "export {};\n");
|
||||
|
||||
const result = yield* workspaceEntries.browse({
|
||||
partialPath: path.join(cwd, "alp"),
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
parentPath: cwd,
|
||||
entries: [
|
||||
{ name: "alpha", fullPath: path.join(cwd, "alpha") },
|
||||
{ name: "alpine", fullPath: path.join(cwd, "alpine") },
|
||||
],
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("shows dot directories in directory mode and hidden-prefix mode", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspaceEntries = yield* WorkspaceEntries;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-hidden-" });
|
||||
yield* writeTextFile(cwd, ".config/settings.json", "{}");
|
||||
yield* writeTextFile(cwd, "config/settings.json", "{}");
|
||||
|
||||
const directoryResult = yield* workspaceEntries.browse({
|
||||
partialPath: appendSeparator(cwd),
|
||||
});
|
||||
const hiddenPrefixResult = yield* workspaceEntries.browse({
|
||||
partialPath: `${appendSeparator(cwd)}.c`,
|
||||
});
|
||||
|
||||
expect(directoryResult.entries.map((entry) => entry.name)).toEqual([".config", "config"]);
|
||||
expect(hiddenPrefixResult).toEqual({
|
||||
parentPath: cwd,
|
||||
entries: [{ name: ".config", fullPath: path.join(cwd, ".config") }],
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("supports relative paths when cwd is provided", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspaceEntries = yield* WorkspaceEntries;
|
||||
const path = yield* Path.Path;
|
||||
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-relative-" });
|
||||
yield* writeTextFile(cwd, "packages/pkg.json", "{}");
|
||||
|
||||
const result = yield* workspaceEntries.browse({
|
||||
cwd,
|
||||
partialPath: "./pack",
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
parentPath: cwd,
|
||||
entries: [{ name: "packages", fullPath: path.join(cwd, "packages") }],
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects relative paths without cwd", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspaceEntries = yield* WorkspaceEntries;
|
||||
|
||||
const error = yield* workspaceEntries
|
||||
.browse({
|
||||
partialPath: "./src",
|
||||
})
|
||||
.pipe(Effect.flip);
|
||||
|
||||
expect(error.detail).toBe("Relative filesystem browse paths require a current project.");
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,11 @@
|
||||
import * as OS from "node:os";
|
||||
import fsPromises from "node:fs/promises";
|
||||
import type { Dirent } from "node:fs";
|
||||
|
||||
import { Cache, Duration, Effect, Exit, Layer, Option, Path } from "effect";
|
||||
|
||||
import { type ProjectEntry } from "@t3tools/contracts";
|
||||
import { type FilesystemBrowseInput, type ProjectEntry } from "@t3tools/contracts";
|
||||
import { isExplicitRelativePath, isWindowsAbsolutePath } from "@t3tools/shared/path";
|
||||
import {
|
||||
insertRankedSearchResult,
|
||||
normalizeSearchQuery,
|
||||
@@ -14,6 +16,7 @@ import {
|
||||
import { GitCore } from "../../git/Services/GitCore.ts";
|
||||
import {
|
||||
WorkspaceEntries,
|
||||
WorkspaceEntriesBrowseError,
|
||||
WorkspaceEntriesError,
|
||||
type WorkspaceEntriesShape,
|
||||
} from "../Services/WorkspaceEntries.ts";
|
||||
@@ -52,6 +55,16 @@ function toPosixPath(input: string): string {
|
||||
return input.replaceAll("\\", "/");
|
||||
}
|
||||
|
||||
function expandHomePath(input: string, path: Path.Path): string {
|
||||
if (input === "~") {
|
||||
return OS.homedir();
|
||||
}
|
||||
if (input.startsWith("~/") || input.startsWith("~\\")) {
|
||||
return path.join(OS.homedir(), input.slice(2));
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
function parentPathOf(input: string): string | undefined {
|
||||
const separatorIndex = input.lastIndexOf("/");
|
||||
if (separatorIndex === -1) {
|
||||
@@ -129,6 +142,36 @@ function directoryAncestorsOf(relativePath: string): string[] {
|
||||
return directories;
|
||||
}
|
||||
|
||||
const resolveBrowseTarget = (
|
||||
input: FilesystemBrowseInput,
|
||||
pathService: Path.Path,
|
||||
): Effect.Effect<string, WorkspaceEntriesBrowseError> =>
|
||||
Effect.gen(function* () {
|
||||
if (process.platform !== "win32" && isWindowsAbsolutePath(input.partialPath)) {
|
||||
return yield* new WorkspaceEntriesBrowseError({
|
||||
cwd: input.cwd,
|
||||
partialPath: input.partialPath,
|
||||
operation: "workspaceEntries.resolveBrowseTarget",
|
||||
detail: "Windows-style paths are only supported on Windows.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!isExplicitRelativePath(input.partialPath)) {
|
||||
return pathService.resolve(expandHomePath(input.partialPath, pathService));
|
||||
}
|
||||
|
||||
if (!input.cwd) {
|
||||
return yield* new WorkspaceEntriesBrowseError({
|
||||
cwd: input.cwd,
|
||||
partialPath: input.partialPath,
|
||||
operation: "workspaceEntries.resolveBrowseTarget",
|
||||
detail: "Relative filesystem browse paths require a current project.",
|
||||
});
|
||||
}
|
||||
|
||||
return pathService.resolve(expandHomePath(input.cwd, pathService), input.partialPath);
|
||||
});
|
||||
|
||||
export const makeWorkspaceEntries = Effect.gen(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const gitOption = yield* Effect.serviceOption(GitCore);
|
||||
@@ -379,6 +422,46 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
|
||||
},
|
||||
);
|
||||
|
||||
const browse: WorkspaceEntriesShape["browse"] = Effect.fn("WorkspaceEntries.browse")(
|
||||
function* (input) {
|
||||
const resolvedInputPath = yield* resolveBrowseTarget(input, path);
|
||||
const endsWithSeparator = /[\\/]$/.test(input.partialPath) || input.partialPath === "~";
|
||||
const parentPath = endsWithSeparator ? resolvedInputPath : path.dirname(resolvedInputPath);
|
||||
const prefix = endsWithSeparator ? "" : path.basename(resolvedInputPath);
|
||||
|
||||
const dirents = yield* Effect.tryPromise({
|
||||
try: () => fsPromises.readdir(parentPath, { withFileTypes: true }),
|
||||
catch: (cause) =>
|
||||
new WorkspaceEntriesBrowseError({
|
||||
cwd: input.cwd,
|
||||
partialPath: input.partialPath,
|
||||
operation: "workspaceEntries.browse.readDirectory",
|
||||
detail: `Unable to browse '${parentPath}': ${cause instanceof Error ? cause.message : String(cause)}`,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
|
||||
const showHidden = endsWithSeparator || prefix.startsWith(".");
|
||||
const lowerPrefix = prefix.toLowerCase();
|
||||
|
||||
return {
|
||||
parentPath,
|
||||
entries: dirents
|
||||
.filter(
|
||||
(dirent) =>
|
||||
dirent.isDirectory() &&
|
||||
dirent.name.toLowerCase().startsWith(lowerPrefix) &&
|
||||
(showHidden || !dirent.name.startsWith(".")),
|
||||
)
|
||||
.map((dirent) => ({
|
||||
name: dirent.name,
|
||||
fullPath: path.join(parentPath, dirent.name),
|
||||
}))
|
||||
.toSorted((left, right) => left.name.localeCompare(right.name)),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const search: WorkspaceEntriesShape["search"] = Effect.fn("WorkspaceEntries.search")(
|
||||
function* (input) {
|
||||
const normalizedCwd = yield* normalizeWorkspaceRoot(input.cwd);
|
||||
@@ -415,6 +498,7 @@ export const makeWorkspaceEntries = Effect.gen(function* () {
|
||||
);
|
||||
|
||||
return {
|
||||
browse,
|
||||
invalidate,
|
||||
search,
|
||||
} satisfies WorkspaceEntriesShape;
|
||||
|
||||
@@ -58,6 +58,24 @@ it.layer(TestLayer)("WorkspacePathsLive", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("creates missing directories when createIfMissing is enabled", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspacePaths = yield* WorkspacePaths;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* makeTempDir();
|
||||
const path = yield* Path.Path;
|
||||
const missingPath = path.join(cwd, "nested", "new-project");
|
||||
|
||||
const resolved = yield* workspacePaths.normalizeWorkspaceRoot(missingPath, {
|
||||
createIfMissing: true,
|
||||
});
|
||||
const stat = yield* fileSystem.stat(resolved);
|
||||
|
||||
expect(resolved).toBe(missingPath);
|
||||
expect(stat.type).toBe("Directory");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects file paths", () =>
|
||||
Effect.gen(function* () {
|
||||
const workspacePaths = yield* WorkspacePaths;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Effect, FileSystem, Layer, Path } from "effect";
|
||||
import {
|
||||
WorkspacePaths,
|
||||
WorkspacePathOutsideRootError,
|
||||
WorkspaceRootCreateFailedError,
|
||||
WorkspaceRootNotDirectoryError,
|
||||
WorkspaceRootNotExistsError,
|
||||
type WorkspacePathsShape,
|
||||
@@ -29,11 +30,25 @@ export const makeWorkspacePaths = Effect.gen(function* () {
|
||||
|
||||
const normalizeWorkspaceRoot: WorkspacePathsShape["normalizeWorkspaceRoot"] = Effect.fn(
|
||||
"WorkspacePaths.normalizeWorkspaceRoot",
|
||||
)(function* (workspaceRoot) {
|
||||
)(function* (workspaceRoot, options) {
|
||||
const normalizedWorkspaceRoot = path.resolve(expandHomePath(workspaceRoot.trim(), path));
|
||||
const workspaceStat = yield* fileSystem
|
||||
let workspaceStat = yield* fileSystem
|
||||
.stat(normalizedWorkspaceRoot)
|
||||
.pipe(Effect.catch(() => Effect.succeed(null)));
|
||||
if (!workspaceStat && options?.createIfMissing) {
|
||||
yield* fileSystem.makeDirectory(normalizedWorkspaceRoot, { recursive: true }).pipe(
|
||||
Effect.mapError(
|
||||
() =>
|
||||
new WorkspaceRootCreateFailedError({
|
||||
workspaceRoot,
|
||||
normalizedWorkspaceRoot,
|
||||
}),
|
||||
),
|
||||
);
|
||||
workspaceStat = yield* fileSystem
|
||||
.stat(normalizedWorkspaceRoot)
|
||||
.pipe(Effect.catch(() => Effect.succeed(null)));
|
||||
}
|
||||
if (!workspaceStat) {
|
||||
return yield* new WorkspaceRootNotExistsError({
|
||||
workspaceRoot,
|
||||
|
||||
@@ -9,7 +9,12 @@
|
||||
import { Schema, Context } from "effect";
|
||||
import type { Effect } from "effect";
|
||||
|
||||
import type { ProjectSearchEntriesInput, ProjectSearchEntriesResult } from "@t3tools/contracts";
|
||||
import type {
|
||||
FilesystemBrowseInput,
|
||||
FilesystemBrowseResult,
|
||||
ProjectSearchEntriesInput,
|
||||
ProjectSearchEntriesResult,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export class WorkspaceEntriesError extends Schema.TaggedErrorClass<WorkspaceEntriesError>()(
|
||||
"WorkspaceEntriesError",
|
||||
@@ -21,11 +26,29 @@ export class WorkspaceEntriesError extends Schema.TaggedErrorClass<WorkspaceEntr
|
||||
},
|
||||
) {}
|
||||
|
||||
export class WorkspaceEntriesBrowseError extends Schema.TaggedErrorClass<WorkspaceEntriesBrowseError>()(
|
||||
"WorkspaceEntriesBrowseError",
|
||||
{
|
||||
cwd: Schema.optional(Schema.String),
|
||||
partialPath: Schema.String,
|
||||
operation: Schema.String,
|
||||
detail: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
},
|
||||
) {}
|
||||
|
||||
/**
|
||||
* WorkspaceEntriesShape - Service API for workspace entry search and cache
|
||||
* invalidation.
|
||||
*/
|
||||
export interface WorkspaceEntriesShape {
|
||||
/**
|
||||
* Browse matching directories for the provided partial path.
|
||||
*/
|
||||
readonly browse: (
|
||||
input: FilesystemBrowseInput,
|
||||
) => Effect.Effect<FilesystemBrowseResult, WorkspaceEntriesBrowseError>;
|
||||
|
||||
/**
|
||||
* Search indexed workspace entries for files and directories matching the
|
||||
* provided query.
|
||||
|
||||
@@ -21,6 +21,18 @@ export class WorkspaceRootNotExistsError extends Schema.TaggedErrorClass<Workspa
|
||||
}
|
||||
}
|
||||
|
||||
export class WorkspaceRootCreateFailedError extends Schema.TaggedErrorClass<WorkspaceRootCreateFailedError>()(
|
||||
"WorkspaceRootCreateFailedError",
|
||||
{
|
||||
workspaceRoot: Schema.String,
|
||||
normalizedWorkspaceRoot: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to create workspace root: ${this.normalizedWorkspaceRoot}`;
|
||||
}
|
||||
}
|
||||
|
||||
export class WorkspaceRootNotDirectoryError extends Schema.TaggedErrorClass<WorkspaceRootNotDirectoryError>()(
|
||||
"WorkspaceRootNotDirectoryError",
|
||||
{
|
||||
@@ -47,6 +59,7 @@ export class WorkspacePathOutsideRootError extends Schema.TaggedErrorClass<Works
|
||||
|
||||
export const WorkspacePathsError = Schema.Union([
|
||||
WorkspaceRootNotExistsError,
|
||||
WorkspaceRootCreateFailedError,
|
||||
WorkspaceRootNotDirectoryError,
|
||||
WorkspacePathOutsideRootError,
|
||||
]);
|
||||
@@ -61,7 +74,11 @@ export interface WorkspacePathsShape {
|
||||
*/
|
||||
readonly normalizeWorkspaceRoot: (
|
||||
workspaceRoot: string,
|
||||
) => Effect.Effect<string, WorkspaceRootNotExistsError | WorkspaceRootNotDirectoryError>;
|
||||
options?: { readonly createIfMissing?: boolean },
|
||||
) => Effect.Effect<
|
||||
string,
|
||||
WorkspaceRootNotExistsError | WorkspaceRootCreateFailedError | WorkspaceRootNotDirectoryError
|
||||
>;
|
||||
|
||||
/**
|
||||
* Resolve a relative path within a validated workspace root.
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
ProjectSearchEntriesError,
|
||||
ProjectWriteFileError,
|
||||
OrchestrationReplayEventsError,
|
||||
FilesystemBrowseError,
|
||||
ThreadId,
|
||||
type TerminalEvent,
|
||||
WS_METHODS,
|
||||
@@ -768,6 +769,20 @@ const makeWsRpcLayer = (currentSessionId: AuthSessionId) =>
|
||||
observeRpcEffect(WS_METHODS.shellOpenInEditor, open.openInEditor(input), {
|
||||
"rpc.aggregate": "workspace",
|
||||
}),
|
||||
[WS_METHODS.filesystemBrowse]: (input) =>
|
||||
observeRpcEffect(
|
||||
WS_METHODS.filesystemBrowse,
|
||||
workspaceEntries.browse(input).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new FilesystemBrowseError({
|
||||
message: cause.detail,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
{ "rpc.aggregate": "workspace" },
|
||||
),
|
||||
[WS_METHODS.subscribeGitStatus]: (input) =>
|
||||
observeRpcStream(
|
||||
WS_METHODS.subscribeGitStatus,
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
interface CommandPaletteOpenIntent {
|
||||
kind: "add-project";
|
||||
requestId: number;
|
||||
}
|
||||
|
||||
interface CommandPaletteStore {
|
||||
open: boolean;
|
||||
openIntent: CommandPaletteOpenIntent | null;
|
||||
setOpen: (open: boolean) => void;
|
||||
toggleOpen: () => void;
|
||||
openAddProject: () => void;
|
||||
clearOpenIntent: () => void;
|
||||
}
|
||||
|
||||
export const useCommandPaletteStore = create<CommandPaletteStore>((set) => ({
|
||||
open: false,
|
||||
setOpen: (open) => set({ open }),
|
||||
toggleOpen: () => set((state) => ({ open: !state.open })),
|
||||
openIntent: null,
|
||||
setOpen: (open) => set({ open, ...(open ? {} : { openIntent: null }) }),
|
||||
toggleOpen: () =>
|
||||
set((state) => ({ open: !state.open, ...(state.open ? { openIntent: null } : {}) })),
|
||||
openAddProject: () =>
|
||||
set((state) => ({
|
||||
open: true,
|
||||
openIntent: {
|
||||
kind: "add-project",
|
||||
requestId: (state.openIntent?.requestId ?? 0) + 1,
|
||||
},
|
||||
})),
|
||||
clearOpenIntent: () => set({ openIntent: null }),
|
||||
}));
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
EventId,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
EnvironmentId,
|
||||
type EnvironmentApi,
|
||||
type MessageId,
|
||||
type OrchestrationReadModel,
|
||||
type ProjectId,
|
||||
@@ -31,6 +32,16 @@ import { render } from "vitest-browser-react";
|
||||
|
||||
import { useCommandPaletteStore } from "../commandPaletteStore";
|
||||
import { useComposerDraftStore, DraftId } from "../composerDraftStore";
|
||||
import {
|
||||
__resetEnvironmentApiOverridesForTests,
|
||||
__setEnvironmentApiOverrideForTests,
|
||||
} from "../environmentApi";
|
||||
import {
|
||||
resetSavedEnvironmentRegistryStoreForTests,
|
||||
resetSavedEnvironmentRuntimeStoreForTests,
|
||||
useSavedEnvironmentRegistryStore,
|
||||
useSavedEnvironmentRuntimeStore,
|
||||
} from "../environments/runtime";
|
||||
import {
|
||||
INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
|
||||
removeInlineTerminalContextPlaceholder,
|
||||
@@ -62,6 +73,7 @@ const ARCHIVED_SECONDARY_THREAD_ID = "thread-secondary-project-archived" as Thre
|
||||
const PROJECT_ID = "project-1" as ProjectId;
|
||||
const SECOND_PROJECT_ID = "project-2" as ProjectId;
|
||||
const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
|
||||
const REMOTE_ENVIRONMENT_ID = EnvironmentId.make("environment-remote");
|
||||
const THREAD_REF = scopeThreadRef(LOCAL_ENVIRONMENT_ID, THREAD_ID);
|
||||
const THREAD_KEY = scopedThreadKey(THREAD_REF);
|
||||
const UUID_ROUTE_RE = /^\/draft\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
@@ -70,6 +82,7 @@ const PROJECT_KEY = scopedProjectKey(scopeProjectRef(LOCAL_ENVIRONMENT_ID, PROJE
|
||||
const NOW_ISO = "2026-03-04T12:00:00.000Z";
|
||||
const BASE_TIME_MS = Date.parse(NOW_ISO);
|
||||
const ATTACHMENT_SVG = "<svg xmlns='http://www.w3.org/2000/svg' width='120' height='120'></svg>";
|
||||
const ADD_PROJECT_SUBMENU_PLACEHOLDER = "Enter path (e.g. ~/projects/my-app)";
|
||||
|
||||
interface TestFixture {
|
||||
snapshot: OrchestrationReadModel;
|
||||
@@ -172,6 +185,32 @@ function createBaseServerConfig(): ServerConfig {
|
||||
};
|
||||
}
|
||||
|
||||
function createMockEnvironmentApi(input: {
|
||||
browse: EnvironmentApi["filesystem"]["browse"];
|
||||
dispatchCommand: EnvironmentApi["orchestration"]["dispatchCommand"];
|
||||
}): EnvironmentApi {
|
||||
return {
|
||||
terminal: {} as EnvironmentApi["terminal"],
|
||||
projects: {} as EnvironmentApi["projects"],
|
||||
filesystem: {
|
||||
browse: input.browse,
|
||||
},
|
||||
git: {} as EnvironmentApi["git"],
|
||||
orchestration: {
|
||||
dispatchCommand: input.dispatchCommand,
|
||||
getTurnDiff: (() => {
|
||||
throw new Error("Not implemented in browser test.");
|
||||
}) as EnvironmentApi["orchestration"]["getTurnDiff"],
|
||||
getFullThreadDiff: (() => {
|
||||
throw new Error("Not implemented in browser test.");
|
||||
}) as EnvironmentApi["orchestration"]["getFullThreadDiff"],
|
||||
subscribeShell: (() => () => undefined) as EnvironmentApi["orchestration"]["subscribeShell"],
|
||||
subscribeThread: (() => () =>
|
||||
undefined) as EnvironmentApi["orchestration"]["subscribeThread"],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function createUserMessage(options: {
|
||||
id: MessageId;
|
||||
text: string;
|
||||
@@ -1383,6 +1422,44 @@ async function waitForCommandPaletteShortcutLabel(): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForCommandPaletteInput(placeholder: string): Promise<HTMLInputElement> {
|
||||
return waitForElement(
|
||||
() => document.querySelector(`input[placeholder="${placeholder}"]`) as HTMLInputElement | null,
|
||||
`Command palette input with placeholder "${placeholder}" did not render.`,
|
||||
);
|
||||
}
|
||||
|
||||
function getCommandPaletteLegendEntries(): string[] {
|
||||
const footer = document.querySelector('[data-slot="command-footer"]');
|
||||
if (!footer) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Array.from(footer.querySelectorAll('[data-slot="kbd-group"]'))
|
||||
.map((group) =>
|
||||
Array.from(group.children)
|
||||
.map((child) => child.textContent?.trim() ?? "")
|
||||
.filter((value) => value.length > 0)
|
||||
.join(" "),
|
||||
)
|
||||
.filter((value) => value.length > 0);
|
||||
}
|
||||
|
||||
async function dispatchInputKey(
|
||||
input: HTMLInputElement,
|
||||
init: Pick<KeyboardEventInit, "key" | "metaKey" | "ctrlKey" | "shiftKey" | "altKey">,
|
||||
): Promise<void> {
|
||||
input.focus();
|
||||
input.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
...init,
|
||||
}),
|
||||
);
|
||||
await waitForLayout();
|
||||
}
|
||||
|
||||
async function mountChatView(options: {
|
||||
viewport: ViewportSpec;
|
||||
snapshot: OrchestrationReadModel;
|
||||
@@ -1524,6 +1601,10 @@ describe("ChatView timeline estimator parity (full app)", () => {
|
||||
document.body.innerHTML = "";
|
||||
wsRequests.length = 0;
|
||||
customWsRpcResolver = null;
|
||||
__resetEnvironmentApiOverridesForTests();
|
||||
resetSavedEnvironmentRegistryStoreForTests();
|
||||
resetSavedEnvironmentRuntimeStoreForTests();
|
||||
Reflect.deleteProperty(window, "desktopBridge");
|
||||
useComposerDraftStore.setState({
|
||||
draftsByThreadKey: {},
|
||||
draftThreadsByThreadKey: {},
|
||||
@@ -1533,6 +1614,7 @@ describe("ChatView timeline estimator parity (full app)", () => {
|
||||
});
|
||||
useCommandPaletteStore.setState({
|
||||
open: false,
|
||||
openIntent: null,
|
||||
});
|
||||
useStore.setState({
|
||||
activeEnvironmentId: null,
|
||||
@@ -4079,6 +4161,746 @@ describe("ChatView timeline estimator parity (full app)", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("adds a project from browse mode with Enter when no directory is highlighted", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-add-project-enter" as MessageId,
|
||||
targetText: "command palette add project enter",
|
||||
}),
|
||||
configureFixture: (nextFixture) => {
|
||||
nextFixture.serverConfig = {
|
||||
...nextFixture.serverConfig,
|
||||
keybindings: [
|
||||
{
|
||||
command: "commandPalette.toggle",
|
||||
shortcut: {
|
||||
key: "k",
|
||||
metaKey: false,
|
||||
ctrlKey: false,
|
||||
shiftKey: false,
|
||||
altKey: false,
|
||||
modKey: true,
|
||||
},
|
||||
whenAst: {
|
||||
type: "not",
|
||||
node: { type: "identifier", name: "terminalFocus" },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Development/") {
|
||||
return {
|
||||
parentPath: "~/Development/",
|
||||
entries: [
|
||||
{ name: "alpha", fullPath: "~/Development/alpha" },
|
||||
{ name: "beta", fullPath: "~/Development/beta" },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Development", fullPath: "~/Development" }],
|
||||
};
|
||||
}
|
||||
|
||||
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
|
||||
return {
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
await waitForCommandPaletteShortcutLabel();
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await openCommandPaletteFromTrigger();
|
||||
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
await palette.getByText("Add project", { exact: true }).click();
|
||||
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await page.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER).fill("~/Development/");
|
||||
await expect.element(palette.getByText("alpha", { exact: true })).toBeInTheDocument();
|
||||
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: "Add (Enter)" }))
|
||||
.toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, { key: "Enter" });
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const dispatchRequest = wsRequests.find(
|
||||
(request) =>
|
||||
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
|
||||
request.type === "project.create",
|
||||
) as
|
||||
| {
|
||||
_tag: string;
|
||||
type?: string;
|
||||
workspaceRoot?: string;
|
||||
title?: string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
expect(dispatchRequest).toMatchObject({
|
||||
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
|
||||
type: "project.create",
|
||||
workspaceRoot: "~/Development",
|
||||
title: "Development",
|
||||
});
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await waitForURL(
|
||||
mounted.router,
|
||||
(path) => UUID_ROUTE_RE.test(path),
|
||||
"Route should have changed to a new draft thread after adding a project with Enter.",
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("opens add project browse mode from the sidebar add button", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-sidebar-add-project-trigger" as MessageId,
|
||||
targetText: "sidebar add project trigger",
|
||||
}),
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Development", fullPath: "~/Development" }],
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
|
||||
await page.getByTestId("sidebar-add-project-trigger").click();
|
||||
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await expect.element(browseInput).toHaveValue("~/");
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(
|
||||
wsRequests.some(
|
||||
(request) =>
|
||||
request._tag === WS_METHODS.filesystemBrowse && request.partialPath === "~/",
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("starts add project browse mode from the configured base directory", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-sidebar-add-project-custom-base-dir" as MessageId,
|
||||
targetText: "sidebar add project custom base directory",
|
||||
}),
|
||||
configureFixture: (nextFixture) => {
|
||||
nextFixture.serverConfig = {
|
||||
...nextFixture.serverConfig,
|
||||
settings: {
|
||||
...nextFixture.serverConfig.settings,
|
||||
addProjectBaseDirectory: "~/Development",
|
||||
},
|
||||
};
|
||||
},
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Development/") {
|
||||
return {
|
||||
parentPath: "~/Development/",
|
||||
entries: [{ name: "codething", fullPath: "~/Development/codething" }],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Development", fullPath: "~/Development" }],
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
|
||||
await page.getByTestId("sidebar-add-project-trigger").click();
|
||||
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await expect.element(browseInput).toHaveValue("~/Development/");
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(
|
||||
wsRequests.some(
|
||||
(request) =>
|
||||
request._tag === WS_METHODS.filesystemBrowse &&
|
||||
request.partialPath === "~/Development/",
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("shows create-folder affordances for missing project paths", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-create-missing-project" as MessageId,
|
||||
targetText: "command palette create missing project",
|
||||
}),
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Desktop/") {
|
||||
return {
|
||||
parentPath: "~/Desktop/",
|
||||
entries: [{ name: "existing", fullPath: "~/Desktop/existing" }],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Desktop", fullPath: "~/Desktop" }],
|
||||
};
|
||||
}
|
||||
|
||||
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
|
||||
return {
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await page.getByTestId("sidebar-add-project-trigger").click();
|
||||
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await page.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER).fill("~/Desktop/fresh-project");
|
||||
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: "Create & Add (Enter)" }))
|
||||
.toBeInTheDocument();
|
||||
await expect.element(palette.getByText("Will create this folder")).not.toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, { key: "Enter" });
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const dispatchRequest = wsRequests.find(
|
||||
(request) =>
|
||||
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
|
||||
request.type === "project.create",
|
||||
) as
|
||||
| {
|
||||
_tag: string;
|
||||
type?: string;
|
||||
workspaceRoot?: string;
|
||||
title?: string;
|
||||
createWorkspaceRootIfMissing?: boolean;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
expect(dispatchRequest).toMatchObject({
|
||||
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
|
||||
type: "project.create",
|
||||
workspaceRoot: "~/Desktop/fresh-project",
|
||||
title: "fresh-project",
|
||||
createWorkspaceRootIfMissing: true,
|
||||
});
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not show create affordances for an existing directory with a trailing slash", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-existing-trailing-directory" as MessageId,
|
||||
targetText: "command palette existing trailing directory",
|
||||
}),
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Development/codex/") {
|
||||
return {
|
||||
parentPath: "~/Development/codex/",
|
||||
entries: [{ name: "Codex.app", fullPath: "~/Development/codex/Codex.app" }],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Development", fullPath: "~/Development" }],
|
||||
};
|
||||
}
|
||||
|
||||
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
|
||||
return {
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await page.getByTestId("sidebar-add-project-trigger").click();
|
||||
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await page.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER).fill("~/Development/codex/");
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(
|
||||
wsRequests.some(
|
||||
(request) =>
|
||||
request._tag === WS_METHODS.filesystemBrowse &&
|
||||
request.partialPath === "~/Development/codex/",
|
||||
),
|
||||
).toBe(true);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: "Add (Enter)" }))
|
||||
.toBeInTheDocument();
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: "Create & Add (Enter)" }))
|
||||
.not.toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, { key: "Enter" });
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const dispatchRequest = wsRequests.find(
|
||||
(request) =>
|
||||
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
|
||||
request.type === "project.create",
|
||||
) as
|
||||
| {
|
||||
_tag: string;
|
||||
type?: string;
|
||||
workspaceRoot?: string;
|
||||
title?: string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
expect(dispatchRequest).toMatchObject({
|
||||
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
|
||||
type: "project.create",
|
||||
workspaceRoot: "~/Development/codex",
|
||||
title: "codex",
|
||||
});
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("selects an environment before browsing when multiple environments are available", async () => {
|
||||
const remoteBrowseMock = vi.fn(async ({ partialPath }: { partialPath: string }) => {
|
||||
if (partialPath === "~/workspaces/") {
|
||||
return {
|
||||
parentPath: "~/workspaces/",
|
||||
entries: [{ name: "codething", fullPath: "~/workspaces/codething" }],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "workspaces", fullPath: "~/workspaces" }],
|
||||
};
|
||||
});
|
||||
const remoteDispatchMock = vi.fn(async () => ({
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
}));
|
||||
|
||||
__setEnvironmentApiOverrideForTests(
|
||||
REMOTE_ENVIRONMENT_ID,
|
||||
createMockEnvironmentApi({
|
||||
browse: remoteBrowseMock,
|
||||
dispatchCommand: remoteDispatchMock,
|
||||
}),
|
||||
);
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-add-project-multi-env" as MessageId,
|
||||
targetText: "command palette add project multi env",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
useSavedEnvironmentRegistryStore.getState().upsert({
|
||||
environmentId: REMOTE_ENVIRONMENT_ID,
|
||||
label: "Staging",
|
||||
httpBaseUrl: "https://staging.example.test",
|
||||
wsBaseUrl: "wss://staging.example.test/ws",
|
||||
createdAt: NOW_ISO,
|
||||
lastConnectedAt: NOW_ISO,
|
||||
});
|
||||
useSavedEnvironmentRuntimeStore.getState().patch(REMOTE_ENVIRONMENT_ID, {
|
||||
connectionState: "connected",
|
||||
authState: "authenticated",
|
||||
descriptor: {
|
||||
...fixture.serverConfig.environment,
|
||||
environmentId: REMOTE_ENVIRONMENT_ID,
|
||||
label: "Staging",
|
||||
},
|
||||
serverConfig: {
|
||||
...fixture.serverConfig,
|
||||
environment: {
|
||||
...fixture.serverConfig.environment,
|
||||
environmentId: REMOTE_ENVIRONMENT_ID,
|
||||
label: "Staging",
|
||||
},
|
||||
settings: {
|
||||
...fixture.serverConfig.settings,
|
||||
addProjectBaseDirectory: "~/workspaces",
|
||||
},
|
||||
},
|
||||
connectedAt: NOW_ISO,
|
||||
});
|
||||
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await openCommandPaletteFromTrigger();
|
||||
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
await palette.getByText("Add project", { exact: true }).click();
|
||||
await expect.element(palette.getByText("Environments", { exact: true })).toBeInTheDocument();
|
||||
await expect
|
||||
.element(palette.getByText("This device", { exact: true }).first())
|
||||
.toBeInTheDocument();
|
||||
await palette.getByText("Staging", { exact: true }).click();
|
||||
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await expect.element(browseInput).toHaveValue("~/workspaces/");
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(remoteBrowseMock).toHaveBeenCalledWith({ partialPath: "~/workspaces/" });
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await page.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER).fill("~/workspaces/");
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(remoteBrowseMock).toHaveBeenCalledWith({ partialPath: "~/workspaces/" });
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
await expect.element(palette.getByText("codething", { exact: true })).toBeInTheDocument();
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: "Add (Enter)" }))
|
||||
.toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, { key: "Enter" });
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(remoteDispatchMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: "project.create",
|
||||
workspaceRoot: "~/workspaces",
|
||||
title: "workspaces",
|
||||
}),
|
||||
);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await waitForURL(
|
||||
mounted.router,
|
||||
(path) => UUID_ROUTE_RE.test(path),
|
||||
"Route should have changed to a new draft thread after adding a remote project.",
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("picks a local project from the native file manager", async () => {
|
||||
const pickFolder = vi.fn().mockResolvedValue("/Users/julius/Projects/finder-picked");
|
||||
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-add-project-file-manager" as MessageId,
|
||||
targetText: "command palette add project file manager",
|
||||
}),
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Applications/") {
|
||||
return {
|
||||
parentPath: "~/Applications/",
|
||||
entries: [{ name: "Utilities", fullPath: "~/Applications/Utilities" }],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Applications", fullPath: "~/Applications" }],
|
||||
};
|
||||
}
|
||||
|
||||
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
|
||||
return {
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
window.desktopBridge = {
|
||||
pickFolder,
|
||||
setTheme: vi.fn().mockResolvedValue(undefined),
|
||||
} as unknown as NonNullable<typeof window.desktopBridge>;
|
||||
|
||||
await page.getByTestId("sidebar-add-project-trigger").click();
|
||||
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
const browseInput = palette.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await browseInput.fill("~/Applications/access");
|
||||
|
||||
const fileManagerLabel = isMacPlatform(navigator.platform)
|
||||
? "Open in Finder"
|
||||
: navigator.platform.toLowerCase().startsWith("win")
|
||||
? "Open in Explorer"
|
||||
: "Open in Files";
|
||||
await palette.getByRole("button", { name: fileManagerLabel }).click();
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(pickFolder).toHaveBeenCalledWith({ initialPath: "~/Applications" });
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const dispatchRequest = wsRequests.find(
|
||||
(request) =>
|
||||
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
|
||||
request.type === "project.create",
|
||||
) as
|
||||
| {
|
||||
_tag: string;
|
||||
type?: string;
|
||||
workspaceRoot?: string;
|
||||
title?: string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
expect(dispatchRequest).toMatchObject({
|
||||
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
|
||||
type: "project.create",
|
||||
workspaceRoot: "/Users/julius/Projects/finder-picked",
|
||||
title: "finder-picked",
|
||||
});
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await waitForURL(
|
||||
mounted.router,
|
||||
(path) => UUID_ROUTE_RE.test(path),
|
||||
"Route should have changed to a new draft thread after adding a project from the native file manager.",
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("adds a project from browse mode with Mod+Enter when a directory is highlighted", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-command-palette-add-project-mod-enter" as MessageId,
|
||||
targetText: "command palette add project mod enter",
|
||||
}),
|
||||
configureFixture: (nextFixture) => {
|
||||
nextFixture.serverConfig = {
|
||||
...nextFixture.serverConfig,
|
||||
keybindings: [
|
||||
{
|
||||
command: "commandPalette.toggle",
|
||||
shortcut: {
|
||||
key: "k",
|
||||
metaKey: false,
|
||||
ctrlKey: false,
|
||||
shiftKey: false,
|
||||
altKey: false,
|
||||
modKey: true,
|
||||
},
|
||||
whenAst: {
|
||||
type: "not",
|
||||
node: { type: "identifier", name: "terminalFocus" },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
resolveRpc: (body) => {
|
||||
if (body._tag === WS_METHODS.filesystemBrowse) {
|
||||
if (body.partialPath === "~/Development/") {
|
||||
return {
|
||||
parentPath: "~/Development/",
|
||||
entries: [
|
||||
{ name: "alpha", fullPath: "~/Development/alpha" },
|
||||
{ name: "beta", fullPath: "~/Development/beta" },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
parentPath: "~/",
|
||||
entries: [{ name: "Development", fullPath: "~/Development" }],
|
||||
};
|
||||
}
|
||||
|
||||
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
|
||||
return {
|
||||
sequence: fixture.snapshot.snapshotSequence + 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServerConfigToApply();
|
||||
await waitForCommandPaletteShortcutLabel();
|
||||
const palette = page.getByTestId("command-palette");
|
||||
await openCommandPaletteFromTrigger();
|
||||
|
||||
await expect.element(palette).toBeInTheDocument();
|
||||
await palette.getByText("Add project", { exact: true }).click();
|
||||
|
||||
const browseInput = await waitForCommandPaletteInput(ADD_PROJECT_SUBMENU_PLACEHOLDER);
|
||||
await page.getByPlaceholder(ADD_PROJECT_SUBMENU_PLACEHOLDER).fill("~/Development/");
|
||||
await expect.element(palette.getByText("alpha", { exact: true })).toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, { key: "ArrowDown" });
|
||||
|
||||
const addButtonLabel = isMacPlatform(navigator.platform)
|
||||
? "Add (\u2318 Enter)"
|
||||
: "Add (Ctrl Enter)";
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const legendEntries = getCommandPaletteLegendEntries();
|
||||
expect(legendEntries).toContain("Enter Select");
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
await expect
|
||||
.element(palette.getByRole("button", { name: addButtonLabel }))
|
||||
.toBeInTheDocument();
|
||||
|
||||
await dispatchInputKey(browseInput, {
|
||||
key: "Enter",
|
||||
metaKey: isMacPlatform(navigator.platform),
|
||||
ctrlKey: !isMacPlatform(navigator.platform),
|
||||
});
|
||||
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const dispatchRequest = wsRequests.find(
|
||||
(request) =>
|
||||
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
|
||||
request.type === "project.create",
|
||||
) as
|
||||
| {
|
||||
_tag: string;
|
||||
type?: string;
|
||||
workspaceRoot?: string;
|
||||
title?: string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
expect(dispatchRequest).toMatchObject({
|
||||
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
|
||||
type: "project.create",
|
||||
workspaceRoot: "~/Development",
|
||||
title: "Development",
|
||||
});
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
|
||||
await waitForURL(
|
||||
mounted.router,
|
||||
(path) => UUID_ROUTE_RE.test(path),
|
||||
"Route should have changed to a new draft thread after adding a project with Mod+Enter.",
|
||||
);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps project-context thread matches available when searching by project name", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type KeybindingCommand } from "@t3tools/contracts";
|
||||
import { type KeybindingCommand, type FilesystemBrowseEntry } from "@t3tools/contracts";
|
||||
import type { SidebarThreadSortOrder } from "@t3tools/contracts/settings";
|
||||
import { type ReactNode } from "react";
|
||||
import { sortThreads } from "../lib/threadSort";
|
||||
@@ -45,7 +45,39 @@ export interface CommandPaletteView {
|
||||
readonly initialQuery?: string;
|
||||
}
|
||||
|
||||
export type CommandPaletteMode = "root" | "submenu";
|
||||
export type CommandPaletteMode = "root" | "root-browse" | "submenu" | "submenu-browse";
|
||||
|
||||
export function filterBrowseEntries(input: {
|
||||
browseEntries: ReadonlyArray<FilesystemBrowseEntry>;
|
||||
browseFilterQuery: string;
|
||||
highlightedItemValue: string | null;
|
||||
}): {
|
||||
filteredEntries: FilesystemBrowseEntry[];
|
||||
highlightedEntry: FilesystemBrowseEntry | null;
|
||||
exactEntry: FilesystemBrowseEntry | null;
|
||||
} {
|
||||
const lowerFilter = input.browseFilterQuery.toLowerCase();
|
||||
const showHidden = input.browseFilterQuery.startsWith(".");
|
||||
|
||||
const filteredEntries = input.browseEntries.filter(
|
||||
(entry) =>
|
||||
entry.name.toLowerCase().startsWith(lowerFilter) &&
|
||||
(showHidden || !entry.name.startsWith(".")),
|
||||
);
|
||||
|
||||
let highlightedEntry: FilesystemBrowseEntry | null = null;
|
||||
if (input.highlightedItemValue?.startsWith("browse:")) {
|
||||
const highlightedPath = input.highlightedItemValue.slice("browse:".length);
|
||||
highlightedEntry = filteredEntries.find((entry) => entry.fullPath === highlightedPath) ?? null;
|
||||
}
|
||||
|
||||
const exactEntry =
|
||||
input.browseFilterQuery.length > 0
|
||||
? (filteredEntries.find((entry) => entry.name === input.browseFilterQuery) ?? null)
|
||||
: null;
|
||||
|
||||
return { filteredEntries, highlightedEntry, exactEntry };
|
||||
}
|
||||
|
||||
export function normalizeSearchText(value: string): string {
|
||||
return value.trim().toLowerCase().replace(/\s+/g, " ");
|
||||
@@ -230,10 +262,56 @@ export function filterCommandPaletteGroups(input: {
|
||||
});
|
||||
}
|
||||
|
||||
export function buildBrowseGroups(input: {
|
||||
browseEntries: ReadonlyArray<FilesystemBrowseEntry>;
|
||||
browseQuery: string;
|
||||
canBrowseUp: boolean;
|
||||
upIcon: ReactNode;
|
||||
directoryIcon: ReactNode;
|
||||
browseUp: () => void;
|
||||
browseTo: (name: string) => void;
|
||||
}): CommandPaletteGroup[] {
|
||||
const items: CommandPaletteActionItem[] = [];
|
||||
|
||||
if (input.canBrowseUp) {
|
||||
items.push({
|
||||
kind: "action",
|
||||
value: "browse:up",
|
||||
searchTerms: [input.browseQuery, ".."],
|
||||
title: "..",
|
||||
icon: input.upIcon,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
input.browseUp();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const entry of input.browseEntries) {
|
||||
items.push({
|
||||
kind: "action",
|
||||
value: `browse:${entry.fullPath}`,
|
||||
searchTerms: [input.browseQuery, entry.fullPath, entry.name],
|
||||
title: entry.name,
|
||||
icon: input.directoryIcon,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
input.browseTo(entry.name);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return [{ value: "directories", label: "Directories", items }];
|
||||
}
|
||||
|
||||
export function getCommandPaletteMode(input: {
|
||||
currentView: CommandPaletteView | null;
|
||||
isBrowsing: boolean;
|
||||
}): CommandPaletteMode {
|
||||
return input.currentView ? "submenu" : "root";
|
||||
if (input.currentView) {
|
||||
return input.isBrowsing ? "submenu-browse" : "submenu";
|
||||
}
|
||||
return input.isBrowsing ? "root-browse" : "root";
|
||||
}
|
||||
|
||||
export function buildRootGroups(input: {
|
||||
@@ -258,7 +336,11 @@ export function getCommandPaletteInputPlaceholder(mode: CommandPaletteMode): str
|
||||
switch (mode) {
|
||||
case "root":
|
||||
return "Search commands, projects, and threads...";
|
||||
case "root-browse":
|
||||
return "Enter project path (e.g. ~/projects/my-app)";
|
||||
case "submenu":
|
||||
return "Search...";
|
||||
case "submenu-browse":
|
||||
return "Enter path (e.g. ~/projects/my-app)";
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,27 @@
|
||||
"use client";
|
||||
|
||||
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime";
|
||||
import type { ProjectId } from "@t3tools/contracts";
|
||||
import {
|
||||
DEFAULT_MODEL_BY_PROVIDER,
|
||||
type EnvironmentId,
|
||||
type FilesystemBrowseResult,
|
||||
type ProjectId,
|
||||
} from "@t3tools/contracts";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useNavigate, useParams } from "@tanstack/react-router";
|
||||
import {
|
||||
ArrowDownIcon,
|
||||
ArrowLeftIcon,
|
||||
ArrowUpIcon,
|
||||
CornerLeftUpIcon,
|
||||
FolderIcon,
|
||||
FolderPlusIcon,
|
||||
MessageSquareIcon,
|
||||
SettingsIcon,
|
||||
SquarePenIcon,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
useCallback,
|
||||
useDeferredValue,
|
||||
useEffect,
|
||||
useMemo,
|
||||
@@ -22,15 +32,37 @@ import {
|
||||
} from "react";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import { useCommandPaletteStore } from "../commandPaletteStore";
|
||||
import { readEnvironmentApi } from "../environmentApi";
|
||||
import { readPrimaryEnvironmentDescriptor, usePrimaryEnvironmentId } from "../environments/primary";
|
||||
import {
|
||||
useSavedEnvironmentRegistryStore,
|
||||
useSavedEnvironmentRuntimeStore,
|
||||
} from "../environments/runtime";
|
||||
import { useHandleNewThread } from "../hooks/useHandleNewThread";
|
||||
import { useSettings } from "../hooks/useSettings";
|
||||
import { readLocalApi } from "../localApi";
|
||||
import {
|
||||
startNewThreadInProjectFromContext,
|
||||
startNewThreadFromContext,
|
||||
} from "../lib/chatThreadActions";
|
||||
import {
|
||||
appendBrowsePathSegment,
|
||||
canNavigateUp,
|
||||
ensureBrowseDirectoryPath,
|
||||
findProjectByPath,
|
||||
getBrowseDirectoryPath,
|
||||
getBrowseLeafPathSegment,
|
||||
getBrowseParentPath,
|
||||
hasTrailingPathSeparator,
|
||||
inferProjectTitleFromPath,
|
||||
isExplicitRelativeProjectPath,
|
||||
isFilesystemBrowseQuery,
|
||||
isUnsupportedWindowsProjectPath,
|
||||
resolveProjectPathForDispatch,
|
||||
} from "../lib/projectPaths";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { getLatestThreadForProject } from "../lib/threadSort";
|
||||
import { cn } from "../lib/utils";
|
||||
import { cn, isMacPlatform, isWindowsPlatform, newCommandId, newProjectId } from "../lib/utils";
|
||||
import {
|
||||
selectProjectsAcrossEnvironments,
|
||||
selectSidebarThreadsAcrossEnvironments,
|
||||
@@ -40,18 +72,21 @@ import { selectThreadTerminalState, useTerminalStateStore } from "../terminalSta
|
||||
import { buildThreadRouteParams, resolveThreadRouteTarget } from "../threadRoutes";
|
||||
import {
|
||||
ADDON_ICON_CLASS,
|
||||
buildBrowseGroups,
|
||||
buildProjectActionItems,
|
||||
buildRootGroups,
|
||||
buildThreadActionItems,
|
||||
type CommandPaletteActionItem,
|
||||
type CommandPaletteSubmenuItem,
|
||||
type CommandPaletteView,
|
||||
filterBrowseEntries,
|
||||
filterCommandPaletteGroups,
|
||||
getCommandPaletteInputPlaceholder,
|
||||
getCommandPaletteMode,
|
||||
ITEM_ICON_CLASS,
|
||||
RECENT_THREAD_LIMIT,
|
||||
} from "./CommandPalette.logic";
|
||||
import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
|
||||
import { CommandPaletteResults } from "./CommandPaletteResults";
|
||||
import { ProjectFavicon } from "./ProjectFavicon";
|
||||
import { useServerKeybindings } from "../rpc/serverState";
|
||||
@@ -64,11 +99,44 @@ import {
|
||||
CommandInput,
|
||||
CommandPanel,
|
||||
} from "./ui/command";
|
||||
import { Button } from "./ui/button";
|
||||
import { Kbd, KbdGroup } from "./ui/kbd";
|
||||
import { toastManager } from "./ui/toast";
|
||||
import { ComposerHandleContext, useComposerHandleContext } from "../composerHandleContext";
|
||||
import type { ChatComposerHandle } from "./chat/ChatComposer";
|
||||
|
||||
const EMPTY_BROWSE_ENTRIES: FilesystemBrowseResult["entries"] = [];
|
||||
const BROWSE_STALE_TIME_MS = 30_000;
|
||||
|
||||
function getLocalFileManagerName(platform: string): string {
|
||||
if (isMacPlatform(platform)) {
|
||||
return "Finder";
|
||||
}
|
||||
if (isWindowsPlatform(platform)) {
|
||||
return "Explorer";
|
||||
}
|
||||
return "Files";
|
||||
}
|
||||
|
||||
function getEnvironmentBrowsePlatform(os: string | null | undefined): string {
|
||||
if (os === "windows") {
|
||||
return "Win32";
|
||||
}
|
||||
if (os === "darwin") {
|
||||
return "MacIntel";
|
||||
}
|
||||
if (os === "linux") {
|
||||
return "Linux";
|
||||
}
|
||||
return typeof navigator === "undefined" ? "" : navigator.platform;
|
||||
}
|
||||
|
||||
interface AddProjectEnvironmentOption {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly label: string;
|
||||
readonly isPrimary: boolean;
|
||||
}
|
||||
|
||||
export function CommandPalette({ children }: { children: ReactNode }) {
|
||||
const open = useCommandPaletteStore((store) => store.open);
|
||||
const setOpen = useCommandPaletteStore((store) => store.setOpen);
|
||||
@@ -136,10 +204,14 @@ function CommandPaletteDialog() {
|
||||
function OpenCommandPaletteDialog() {
|
||||
const navigate = useNavigate();
|
||||
const setOpen = useCommandPaletteStore((store) => store.setOpen);
|
||||
const openIntent = useCommandPaletteStore((store) => store.openIntent);
|
||||
const clearOpenIntent = useCommandPaletteStore((store) => store.clearOpenIntent);
|
||||
const composerHandleRef = useComposerHandleContext();
|
||||
const [query, setQuery] = useState("");
|
||||
const deferredQuery = useDeferredValue(query);
|
||||
const isActionsOnly = deferredQuery.startsWith(">");
|
||||
const queryClient = useQueryClient();
|
||||
const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
|
||||
const settings = useSettings();
|
||||
const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread } =
|
||||
useHandleNewThread();
|
||||
@@ -148,15 +220,199 @@ function OpenCommandPaletteDialog() {
|
||||
const keybindings = useServerKeybindings();
|
||||
const [viewStack, setViewStack] = useState<CommandPaletteView[]>([]);
|
||||
const currentView = viewStack.at(-1) ?? null;
|
||||
const paletteMode = getCommandPaletteMode({ currentView });
|
||||
const [browseGeneration, setBrowseGeneration] = useState(0);
|
||||
const [addProjectEnvironmentId, setAddProjectEnvironmentId] = useState<EnvironmentId | null>(
|
||||
null,
|
||||
);
|
||||
const [isPickingProjectFolder, setIsPickingProjectFolder] = useState(false);
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const primaryEnvironmentLabel = readPrimaryEnvironmentDescriptor()?.label ?? null;
|
||||
const savedEnvironmentRegistry = useSavedEnvironmentRegistryStore((state) => state.byId);
|
||||
const savedEnvironmentRuntimeById = useSavedEnvironmentRuntimeStore((state) => state.byId);
|
||||
|
||||
const addProjectEnvironmentOptions = useMemo(() => {
|
||||
const options: AddProjectEnvironmentOption[] = [];
|
||||
const seenEnvironmentIds = new Set<EnvironmentId>();
|
||||
|
||||
if (primaryEnvironmentId) {
|
||||
seenEnvironmentIds.add(primaryEnvironmentId);
|
||||
options.push({
|
||||
environmentId: primaryEnvironmentId,
|
||||
label: resolveEnvironmentOptionLabel({
|
||||
isPrimary: true,
|
||||
environmentId: primaryEnvironmentId,
|
||||
runtimeLabel: primaryEnvironmentLabel,
|
||||
}),
|
||||
isPrimary: true,
|
||||
});
|
||||
}
|
||||
|
||||
for (const record of Object.values(savedEnvironmentRegistry)) {
|
||||
if (seenEnvironmentIds.has(record.environmentId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const runtimeState = savedEnvironmentRuntimeById[record.environmentId];
|
||||
options.push({
|
||||
environmentId: record.environmentId,
|
||||
label: resolveEnvironmentOptionLabel({
|
||||
isPrimary: false,
|
||||
environmentId: record.environmentId,
|
||||
runtimeLabel: runtimeState?.descriptor?.label ?? null,
|
||||
savedLabel: record.label,
|
||||
}),
|
||||
isPrimary: false,
|
||||
});
|
||||
}
|
||||
|
||||
options.sort((left, right) => {
|
||||
if (left.isPrimary !== right.isPrimary) {
|
||||
return left.isPrimary ? -1 : 1;
|
||||
}
|
||||
return left.label.localeCompare(right.label);
|
||||
});
|
||||
|
||||
return options;
|
||||
}, [
|
||||
primaryEnvironmentId,
|
||||
primaryEnvironmentLabel,
|
||||
savedEnvironmentRegistry,
|
||||
savedEnvironmentRuntimeById,
|
||||
]);
|
||||
const defaultAddProjectEnvironmentId = addProjectEnvironmentOptions[0]?.environmentId ?? null;
|
||||
const browseEnvironmentId = addProjectEnvironmentId ?? defaultAddProjectEnvironmentId;
|
||||
const browseEnvironmentPlatform = useMemo(() => {
|
||||
const os =
|
||||
browseEnvironmentId && primaryEnvironmentId && browseEnvironmentId === primaryEnvironmentId
|
||||
? (readPrimaryEnvironmentDescriptor()?.platform.os ?? null)
|
||||
: browseEnvironmentId
|
||||
? (savedEnvironmentRuntimeById[browseEnvironmentId]?.descriptor?.platform.os ??
|
||||
savedEnvironmentRuntimeById[browseEnvironmentId]?.serverConfig?.environment.platform
|
||||
.os ??
|
||||
null)
|
||||
: null;
|
||||
return getEnvironmentBrowsePlatform(os);
|
||||
}, [browseEnvironmentId, primaryEnvironmentId, savedEnvironmentRuntimeById]);
|
||||
const isBrowsing = isFilesystemBrowseQuery(query, browseEnvironmentPlatform);
|
||||
const paletteMode = getCommandPaletteMode({ currentView, isBrowsing });
|
||||
const getAddProjectInitialQueryForEnvironment = useCallback(
|
||||
(environmentId: EnvironmentId | null): string => {
|
||||
const environmentSettings =
|
||||
environmentId && primaryEnvironmentId && environmentId === primaryEnvironmentId
|
||||
? settings
|
||||
: environmentId
|
||||
? savedEnvironmentRuntimeById[environmentId]?.serverConfig?.settings
|
||||
: null;
|
||||
const baseDirectory = environmentSettings?.addProjectBaseDirectory?.trim() ?? "";
|
||||
if (baseDirectory.length === 0) {
|
||||
return "~/";
|
||||
}
|
||||
return ensureBrowseDirectoryPath(baseDirectory);
|
||||
},
|
||||
[primaryEnvironmentId, savedEnvironmentRuntimeById, settings],
|
||||
);
|
||||
|
||||
const projectCwdById = useMemo(
|
||||
() => new Map<ProjectId, string>(projects.map((project) => [project.id, project.cwd])),
|
||||
[projects],
|
||||
);
|
||||
const projectTitleById = useMemo(
|
||||
() => new Map<ProjectId, string>(projects.map((project) => [project.id, project.name])),
|
||||
[projects],
|
||||
);
|
||||
|
||||
const activeThreadId = activeThread?.id;
|
||||
const currentProjectEnvironmentId =
|
||||
activeThread?.environmentId ?? activeDraftThread?.environmentId ?? null;
|
||||
const currentProjectId = activeThread?.projectId ?? activeDraftThread?.projectId ?? null;
|
||||
const currentProjectCwd = currentProjectId
|
||||
? (projectCwdById.get(currentProjectId) ?? null)
|
||||
: null;
|
||||
const currentProjectCwdForBrowse =
|
||||
browseEnvironmentId && currentProjectEnvironmentId === browseEnvironmentId
|
||||
? currentProjectCwd
|
||||
: null;
|
||||
const relativePathNeedsActiveProject =
|
||||
isExplicitRelativeProjectPath(query.trim()) && currentProjectCwdForBrowse === null;
|
||||
const browseDirectoryPath = isBrowsing ? getBrowseDirectoryPath(query) : "";
|
||||
const browseFilterQuery =
|
||||
isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : "";
|
||||
|
||||
const fetchBrowseResult = useCallback(
|
||||
async (partialPath: string): Promise<FilesystemBrowseResult | null> => {
|
||||
if (!browseEnvironmentId) return null;
|
||||
const api = readEnvironmentApi(browseEnvironmentId);
|
||||
if (!api) return null;
|
||||
return api.filesystem.browse({
|
||||
partialPath,
|
||||
...(currentProjectCwdForBrowse ? { cwd: currentProjectCwdForBrowse } : {}),
|
||||
});
|
||||
},
|
||||
[browseEnvironmentId, currentProjectCwdForBrowse],
|
||||
);
|
||||
|
||||
const { data: browseResult, isPending: isBrowsePending } = useQuery({
|
||||
queryKey: [
|
||||
"filesystemBrowse",
|
||||
browseEnvironmentId,
|
||||
browseDirectoryPath,
|
||||
currentProjectCwdForBrowse,
|
||||
],
|
||||
queryFn: () => fetchBrowseResult(browseDirectoryPath),
|
||||
staleTime: BROWSE_STALE_TIME_MS,
|
||||
enabled:
|
||||
isBrowsing &&
|
||||
browseDirectoryPath.length > 0 &&
|
||||
browseEnvironmentId !== null &&
|
||||
!relativePathNeedsActiveProject,
|
||||
});
|
||||
const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES;
|
||||
const {
|
||||
filteredEntries: filteredBrowseEntries,
|
||||
highlightedEntry: highlightedBrowseEntry,
|
||||
exactEntry: exactBrowseEntry,
|
||||
} = useMemo(
|
||||
() => filterBrowseEntries({ browseEntries, browseFilterQuery, highlightedItemValue }),
|
||||
[browseEntries, browseFilterQuery, highlightedItemValue],
|
||||
);
|
||||
|
||||
const prefetchBrowsePath = useCallback(
|
||||
(partialPath: string) => {
|
||||
void queryClient.prefetchQuery({
|
||||
queryKey: [
|
||||
"filesystemBrowse",
|
||||
browseEnvironmentId,
|
||||
partialPath,
|
||||
currentProjectCwdForBrowse,
|
||||
],
|
||||
queryFn: () => fetchBrowseResult(partialPath),
|
||||
staleTime: BROWSE_STALE_TIME_MS,
|
||||
});
|
||||
},
|
||||
[browseEnvironmentId, currentProjectCwdForBrowse, fetchBrowseResult, queryClient],
|
||||
);
|
||||
|
||||
// Prefetch the parent and the most likely next child so browse navigation
|
||||
// stays warm without scanning every child directory in large trees.
|
||||
useEffect(() => {
|
||||
if (!isBrowsing || filteredBrowseEntries.length === 0) return;
|
||||
|
||||
if (canNavigateUp(query)) {
|
||||
prefetchBrowsePath(getBrowseParentPath(query)!);
|
||||
}
|
||||
|
||||
const nextChild = highlightedBrowseEntry ?? exactBrowseEntry;
|
||||
if (nextChild) {
|
||||
prefetchBrowsePath(appendBrowsePathSegment(query, nextChild.name));
|
||||
}
|
||||
}, [
|
||||
exactBrowseEntry,
|
||||
filteredBrowseEntries.length,
|
||||
highlightedBrowseEntry,
|
||||
isBrowsing,
|
||||
prefetchBrowsePath,
|
||||
query,
|
||||
]);
|
||||
|
||||
const openProjectFromSearch = useMemo(
|
||||
() => async (project: (typeof projects)[number]) => {
|
||||
@@ -259,30 +515,117 @@ function OpenCommandPaletteDialog() {
|
||||
);
|
||||
const recentThreadItems = allThreadItems.slice(0, RECENT_THREAD_LIMIT);
|
||||
|
||||
function pushView(item: CommandPaletteSubmenuItem): void {
|
||||
function pushPaletteView(view: CommandPaletteView): void {
|
||||
setViewStack((previousViews) => [
|
||||
...previousViews,
|
||||
{
|
||||
addonIcon: item.addonIcon,
|
||||
groups: item.groups,
|
||||
...(item.initialQuery ? { initialQuery: item.initialQuery } : {}),
|
||||
addonIcon: view.addonIcon,
|
||||
groups: view.groups,
|
||||
...(view.initialQuery ? { initialQuery: view.initialQuery } : {}),
|
||||
},
|
||||
]);
|
||||
setQuery(item.initialQuery ?? "");
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(view.initialQuery ?? "");
|
||||
}
|
||||
|
||||
function pushView(item: CommandPaletteSubmenuItem): void {
|
||||
pushPaletteView({
|
||||
addonIcon: item.addonIcon,
|
||||
groups: item.groups,
|
||||
...(item.initialQuery ? { initialQuery: item.initialQuery } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
function popView(): void {
|
||||
if (viewStack.length <= 1) {
|
||||
setAddProjectEnvironmentId(null);
|
||||
}
|
||||
setViewStack((previousViews) => previousViews.slice(0, -1));
|
||||
setHighlightedItemValue(null);
|
||||
setQuery("");
|
||||
}
|
||||
|
||||
function handleQueryChange(nextQuery: string): void {
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(nextQuery);
|
||||
if (nextQuery === "" && currentView?.initialQuery) {
|
||||
popView();
|
||||
}
|
||||
}
|
||||
|
||||
const startAddProjectBrowse = useCallback(
|
||||
(environmentId: EnvironmentId): void => {
|
||||
setAddProjectEnvironmentId(environmentId);
|
||||
pushPaletteView({
|
||||
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
|
||||
groups: [],
|
||||
initialQuery: getAddProjectInitialQueryForEnvironment(environmentId),
|
||||
});
|
||||
},
|
||||
[getAddProjectInitialQueryForEnvironment],
|
||||
);
|
||||
|
||||
const addProjectEnvironmentItems: CommandPaletteActionItem[] = addProjectEnvironmentOptions.map(
|
||||
(option) => ({
|
||||
kind: "action",
|
||||
value: `action:add-project:environment:${option.environmentId}`,
|
||||
searchTerms: [option.label, option.environmentId, option.isPrimary ? "this device" : ""],
|
||||
title: option.label,
|
||||
description: option.isPrimary ? "This device" : option.environmentId,
|
||||
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
startAddProjectBrowse(option.environmentId);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const addProjectEnvironmentGroups = useMemo<CommandPaletteView["groups"]>(
|
||||
() => [
|
||||
{
|
||||
value: "environments",
|
||||
label: "Environments",
|
||||
items: addProjectEnvironmentItems,
|
||||
},
|
||||
],
|
||||
[addProjectEnvironmentItems],
|
||||
);
|
||||
|
||||
const openAddProjectFlow = useCallback(() => {
|
||||
if (addProjectEnvironmentOptions.length > 1) {
|
||||
pushPaletteView({
|
||||
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
|
||||
groups: addProjectEnvironmentGroups,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const environmentId = defaultAddProjectEnvironmentId;
|
||||
if (!environmentId) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Unable to browse projects",
|
||||
description: "No environment is available.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
startAddProjectBrowse(environmentId);
|
||||
}, [
|
||||
addProjectEnvironmentGroups,
|
||||
addProjectEnvironmentOptions.length,
|
||||
defaultAddProjectEnvironmentId,
|
||||
startAddProjectBrowse,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (openIntent?.kind !== "add-project") {
|
||||
return;
|
||||
}
|
||||
clearOpenIntent();
|
||||
openAddProjectFlow();
|
||||
}, [clearOpenIntent, openAddProjectFlow, openIntent]);
|
||||
|
||||
const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
|
||||
|
||||
if (projects.length > 0) {
|
||||
@@ -325,6 +668,30 @@ function OpenCommandPaletteDialog() {
|
||||
});
|
||||
}
|
||||
|
||||
if (addProjectEnvironmentOptions.length > 1) {
|
||||
actionItems.push({
|
||||
kind: "submenu",
|
||||
value: "action:add-project",
|
||||
searchTerms: ["add project", "folder", "directory", "browse", "environment"],
|
||||
title: "Add project",
|
||||
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
|
||||
addonIcon: <FolderPlusIcon className={ADDON_ICON_CLASS} />,
|
||||
groups: addProjectEnvironmentGroups,
|
||||
});
|
||||
} else {
|
||||
actionItems.push({
|
||||
kind: "action",
|
||||
value: "action:add-project",
|
||||
searchTerms: ["add project", "folder", "directory", "browse"],
|
||||
title: "Add project",
|
||||
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
|
||||
keepOpen: true,
|
||||
run: async () => {
|
||||
openAddProjectFlow();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
actionItems.push({
|
||||
kind: "action",
|
||||
value: "action:settings",
|
||||
@@ -339,7 +706,7 @@ function OpenCommandPaletteDialog() {
|
||||
const rootGroups = buildRootGroups({ actionItems, recentThreadItems });
|
||||
const activeGroups = currentView ? currentView.groups : rootGroups;
|
||||
|
||||
const displayedGroups = filterCommandPaletteGroups({
|
||||
const filteredGroups = filterCommandPaletteGroups({
|
||||
activeGroups,
|
||||
query: deferredQuery,
|
||||
isInSubmenu: currentView !== null,
|
||||
@@ -347,10 +714,206 @@ function OpenCommandPaletteDialog() {
|
||||
threadSearchItems: allThreadItems,
|
||||
});
|
||||
|
||||
const handleAddProject = useCallback(
|
||||
async (rawCwd: string) => {
|
||||
if (!browseEnvironmentId) return;
|
||||
const api = readEnvironmentApi(browseEnvironmentId);
|
||||
if (!api) return;
|
||||
|
||||
if (isUnsupportedWindowsProjectPath(rawCwd.trim(), browseEnvironmentPlatform)) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Failed to add project",
|
||||
description: "Windows-style paths are only supported on Windows.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (isExplicitRelativeProjectPath(rawCwd.trim()) && !currentProjectCwdForBrowse) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Failed to add project",
|
||||
description: "Relative paths require an active project.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const cwd = resolveProjectPathForDispatch(rawCwd, currentProjectCwdForBrowse);
|
||||
if (cwd.length === 0) return;
|
||||
|
||||
const existing = findProjectByPath(
|
||||
projects.filter((project) => project.environmentId === browseEnvironmentId),
|
||||
cwd,
|
||||
);
|
||||
if (existing) {
|
||||
const latestThread = getLatestThreadForProject(
|
||||
threads.filter((thread) => thread.environmentId === existing.environmentId),
|
||||
existing.id,
|
||||
settings.sidebarThreadSortOrder,
|
||||
);
|
||||
if (latestThread) {
|
||||
await navigate({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(
|
||||
scopeThreadRef(latestThread.environmentId, latestThread.id),
|
||||
),
|
||||
});
|
||||
} else {
|
||||
await handleNewThread(scopeProjectRef(existing.environmentId, existing.id), {
|
||||
envMode: settings.defaultThreadEnvMode,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
setOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const projectId = newProjectId();
|
||||
await api.orchestration.dispatchCommand({
|
||||
type: "project.create",
|
||||
commandId: newCommandId(),
|
||||
projectId,
|
||||
title: inferProjectTitleFromPath(cwd),
|
||||
workspaceRoot: cwd,
|
||||
createWorkspaceRootIfMissing: true,
|
||||
defaultModelSelection: {
|
||||
provider: "codex",
|
||||
model: DEFAULT_MODEL_BY_PROVIDER.codex,
|
||||
},
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
await handleNewThread(scopeProjectRef(browseEnvironmentId, projectId), {
|
||||
envMode: settings.defaultThreadEnvMode,
|
||||
}).catch(() => undefined);
|
||||
setOpen(false);
|
||||
} catch (error) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Failed to add project",
|
||||
description: error instanceof Error ? error.message : "An error occurred.",
|
||||
});
|
||||
}
|
||||
},
|
||||
[
|
||||
browseEnvironmentId,
|
||||
browseEnvironmentPlatform,
|
||||
currentProjectCwdForBrowse,
|
||||
handleNewThread,
|
||||
navigate,
|
||||
projects,
|
||||
setOpen,
|
||||
settings.defaultThreadEnvMode,
|
||||
settings.sidebarThreadSortOrder,
|
||||
threads,
|
||||
],
|
||||
);
|
||||
|
||||
function browseTo(name: string): void {
|
||||
const nextQuery = appendBrowsePathSegment(query, name);
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(nextQuery);
|
||||
setBrowseGeneration((generation) => generation + 1);
|
||||
}
|
||||
|
||||
function browseUp(): void {
|
||||
const parentPath = getBrowseParentPath(query);
|
||||
if (parentPath === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
setHighlightedItemValue(null);
|
||||
setQuery(parentPath);
|
||||
setBrowseGeneration((generation) => generation + 1);
|
||||
}
|
||||
|
||||
// Resolve the add-project path from browse data when available. When the
|
||||
// query has a trailing separator (e.g. "~/projects/foo/"), parentPath is the
|
||||
// directory itself. Otherwise the user typed a partial leaf name, so we need
|
||||
// the exact browse entry's fullPath or fall back to the raw query.
|
||||
const resolvedAddProjectPath = hasTrailingPathSeparator(query)
|
||||
? (browseResult?.parentPath ?? query.trim())
|
||||
: (exactBrowseEntry?.fullPath ?? query.trim());
|
||||
|
||||
const canBrowseUp =
|
||||
isBrowsing && !relativePathNeedsActiveProject && canNavigateUp(browseDirectoryPath);
|
||||
|
||||
const browseGroups = buildBrowseGroups({
|
||||
browseEntries: filteredBrowseEntries,
|
||||
browseQuery: query,
|
||||
canBrowseUp,
|
||||
upIcon: <CornerLeftUpIcon className={ITEM_ICON_CLASS} />,
|
||||
directoryIcon: <FolderIcon className={ITEM_ICON_CLASS} />,
|
||||
browseUp,
|
||||
browseTo,
|
||||
});
|
||||
|
||||
let displayedGroups = filteredGroups;
|
||||
if (isBrowsing) {
|
||||
displayedGroups = relativePathNeedsActiveProject ? [] : browseGroups;
|
||||
}
|
||||
|
||||
const inputPlaceholder = getCommandPaletteInputPlaceholder(paletteMode);
|
||||
const isSubmenu = paletteMode === "submenu";
|
||||
const isSubmenu = paletteMode === "submenu" || paletteMode === "submenu-browse";
|
||||
const hasHighlightedBrowseItem = highlightedItemValue?.startsWith("browse:") ?? false;
|
||||
const canSubmitBrowsePath = isBrowsing && !relativePathNeedsActiveProject;
|
||||
const willCreateProjectPath =
|
||||
canSubmitBrowsePath &&
|
||||
!isBrowsePending &&
|
||||
query.trim().length > 0 &&
|
||||
!hasHighlightedBrowseItem &&
|
||||
(hasTrailingPathSeparator(query) ? !browseResult : exactBrowseEntry === null);
|
||||
const useMetaForMod = isMacPlatform(navigator.platform);
|
||||
const submitModifierLabel = useMetaForMod ? "\u2318" : "Ctrl";
|
||||
const submitActionLabel = willCreateProjectPath ? "Create & Add" : "Add";
|
||||
const addShortcutLabel = hasHighlightedBrowseItem ? `${submitModifierLabel} Enter` : "Enter";
|
||||
const fileManagerName = getLocalFileManagerName(navigator.platform);
|
||||
const canOpenProjectFromFileManager =
|
||||
isBrowsing &&
|
||||
browseEnvironmentId !== null &&
|
||||
primaryEnvironmentId !== null &&
|
||||
browseEnvironmentId === primaryEnvironmentId &&
|
||||
typeof window !== "undefined" &&
|
||||
window.desktopBridge !== undefined;
|
||||
const fileManagerInitialPath = useMemo(() => {
|
||||
if (!canOpenProjectFromFileManager) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const trimmedQuery = query.trim();
|
||||
if (trimmedQuery.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const initialPath = hasTrailingPathSeparator(query)
|
||||
? (browseResult?.parentPath ?? trimmedQuery)
|
||||
: browseDirectoryPath || trimmedQuery;
|
||||
|
||||
const resolvedPath = resolveProjectPathForDispatch(initialPath, currentProjectCwdForBrowse);
|
||||
return resolvedPath.length > 0 ? resolvedPath : undefined;
|
||||
}, [
|
||||
browseDirectoryPath,
|
||||
browseResult?.parentPath,
|
||||
canOpenProjectFromFileManager,
|
||||
currentProjectCwdForBrowse,
|
||||
query,
|
||||
]);
|
||||
|
||||
function isPrimaryModifierPressed(event: KeyboardEvent<HTMLInputElement>): boolean {
|
||||
return useMetaForMod ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
|
||||
}
|
||||
|
||||
function handleKeyDown(event: KeyboardEvent<HTMLInputElement>): void {
|
||||
const shouldSubmitBrowsePath =
|
||||
canSubmitBrowsePath &&
|
||||
event.key === "Enter" &&
|
||||
(!hasHighlightedBrowseItem || isPrimaryModifierPressed(event));
|
||||
|
||||
if (shouldSubmitBrowsePath) {
|
||||
event.preventDefault();
|
||||
void handleAddProject(resolvedAddProjectPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key === "Backspace" && query === "" && isSubmenu) {
|
||||
event.preventDefault();
|
||||
popView();
|
||||
@@ -376,6 +939,38 @@ function OpenCommandPaletteDialog() {
|
||||
});
|
||||
}
|
||||
|
||||
const handleOpenProjectFromFileManager = useCallback(async () => {
|
||||
if (!canOpenProjectFromFileManager || isPickingProjectFolder) {
|
||||
return;
|
||||
}
|
||||
const api = readLocalApi();
|
||||
if (!api) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsPickingProjectFolder(true);
|
||||
let pickedPath: string | null = null;
|
||||
try {
|
||||
pickedPath = await api.dialogs.pickFolder(
|
||||
fileManagerInitialPath ? { initialPath: fileManagerInitialPath } : undefined,
|
||||
);
|
||||
} catch {
|
||||
// Ignore picker failures and leave the palette open.
|
||||
setIsPickingProjectFolder(false);
|
||||
return;
|
||||
}
|
||||
setIsPickingProjectFolder(false);
|
||||
if (!pickedPath) {
|
||||
return;
|
||||
}
|
||||
await handleAddProject(pickedPath);
|
||||
}, [
|
||||
canOpenProjectFromFileManager,
|
||||
fileManagerInitialPath,
|
||||
handleAddProject,
|
||||
isPickingProjectFolder,
|
||||
]);
|
||||
|
||||
return (
|
||||
<CommandDialogPopup
|
||||
aria-label="Command palette"
|
||||
@@ -387,42 +982,86 @@ function OpenCommandPaletteDialog() {
|
||||
}}
|
||||
>
|
||||
<Command
|
||||
key={viewStack.length}
|
||||
key={`${viewStack.length}-${browseGeneration}-${isBrowsing}`}
|
||||
aria-label="Command palette"
|
||||
autoHighlight="always"
|
||||
autoHighlight={isBrowsing ? false : "always"}
|
||||
mode="none"
|
||||
onItemHighlighted={(value) => {
|
||||
setHighlightedItemValue(typeof value === "string" ? value : null);
|
||||
}}
|
||||
onValueChange={handleQueryChange}
|
||||
value={query}
|
||||
>
|
||||
<CommandInput
|
||||
placeholder={inputPlaceholder}
|
||||
wrapperClassName={
|
||||
isSubmenu
|
||||
? "[&_[data-slot=autocomplete-start-addon]]:pointer-events-auto [&_[data-slot=autocomplete-start-addon]]:cursor-pointer"
|
||||
: undefined
|
||||
}
|
||||
{...(isSubmenu
|
||||
? {
|
||||
startAddon: (
|
||||
<button
|
||||
type="button"
|
||||
className="flex cursor-pointer items-center"
|
||||
aria-label="Back"
|
||||
onClick={popView}
|
||||
>
|
||||
<ArrowLeftIcon />
|
||||
</button>
|
||||
),
|
||||
}
|
||||
: {})}
|
||||
onKeyDown={handleKeyDown}
|
||||
/>
|
||||
<div className="relative">
|
||||
<CommandInput
|
||||
className={isBrowsing ? (willCreateProjectPath ? "pe-36" : "pe-16") : undefined}
|
||||
placeholder={inputPlaceholder}
|
||||
wrapperClassName={
|
||||
isSubmenu ? "[&_[data-slot=autocomplete-start-addon]]:pointer-events-auto" : undefined
|
||||
}
|
||||
{...(isSubmenu
|
||||
? {
|
||||
startAddon: (
|
||||
<button
|
||||
type="button"
|
||||
className="flex cursor-pointer items-center"
|
||||
aria-label="Back"
|
||||
onClick={popView}
|
||||
>
|
||||
<ArrowLeftIcon />
|
||||
</button>
|
||||
),
|
||||
}
|
||||
: isBrowsing && !isSubmenu
|
||||
? {
|
||||
startAddon: <FolderPlusIcon />,
|
||||
}
|
||||
: {})}
|
||||
onKeyDown={handleKeyDown}
|
||||
/>
|
||||
{isBrowsing ? (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="xs"
|
||||
tabIndex={-1}
|
||||
className={cn(
|
||||
"absolute end-2.5 top-1/2 pe-1 ps-2 -translate-y-1/2",
|
||||
hasHighlightedBrowseItem ? "gap-1" : "gap-1.5",
|
||||
)}
|
||||
aria-label={`${submitActionLabel} (${addShortcutLabel})`}
|
||||
disabled={relativePathNeedsActiveProject}
|
||||
onMouseDown={(event) => {
|
||||
event.preventDefault();
|
||||
}}
|
||||
onClick={() => {
|
||||
if (relativePathNeedsActiveProject) {
|
||||
return;
|
||||
}
|
||||
void handleAddProject(resolvedAddProjectPath);
|
||||
}}
|
||||
title={`${submitActionLabel} (${addShortcutLabel})`}
|
||||
>
|
||||
<span>{submitActionLabel}</span>
|
||||
<KbdGroup className="pointer-events-none -me-0.5 items-center gap-1">
|
||||
<Kbd>{hasHighlightedBrowseItem ? `${submitModifierLabel} Enter` : "Enter"}</Kbd>
|
||||
</KbdGroup>
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
<CommandPanel className="max-h-[min(28rem,70vh)]">
|
||||
<CommandPaletteResults
|
||||
groups={displayedGroups}
|
||||
highlightedItemValue={highlightedItemValue}
|
||||
isActionsOnly={isActionsOnly}
|
||||
keybindings={keybindings}
|
||||
onExecuteItem={executeItem}
|
||||
{...(relativePathNeedsActiveProject
|
||||
? { emptyStateMessage: "Relative paths require an active project." }
|
||||
: willCreateProjectPath
|
||||
? {
|
||||
emptyStateMessage: "Press Enter to create this folder and add it as a project.",
|
||||
}
|
||||
: {})}
|
||||
/>
|
||||
</CommandPanel>
|
||||
<CommandFooter className="gap-3 max-sm:flex-col max-sm:items-start">
|
||||
@@ -436,10 +1075,12 @@ function OpenCommandPaletteDialog() {
|
||||
</Kbd>
|
||||
<span className={cn("text-muted-foreground/80")}>Navigate</span>
|
||||
</KbdGroup>
|
||||
<KbdGroup className="items-center gap-1.5">
|
||||
<Kbd>Enter</Kbd>
|
||||
<span className={cn("text-muted-foreground/80")}>Select</span>
|
||||
</KbdGroup>
|
||||
{!canSubmitBrowsePath || hasHighlightedBrowseItem ? (
|
||||
<KbdGroup className="items-center gap-1.5">
|
||||
<Kbd>Enter</Kbd>
|
||||
<span className={cn("text-muted-foreground/80")}>Select</span>
|
||||
</KbdGroup>
|
||||
) : null}
|
||||
{isSubmenu ? (
|
||||
<KbdGroup className="items-center gap-1.5">
|
||||
<Kbd>Backspace</Kbd>
|
||||
@@ -451,6 +1092,19 @@ function OpenCommandPaletteDialog() {
|
||||
<span className={cn("text-muted-foreground/80")}>Close</span>
|
||||
</KbdGroup>
|
||||
</div>
|
||||
{canOpenProjectFromFileManager ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="xs"
|
||||
className="h-auto px-2 text-xs text-muted-foreground/80 hover:bg-transparent hover:text-foreground"
|
||||
disabled={isPickingProjectFolder}
|
||||
onClick={() => {
|
||||
void handleOpenProjectFromFileManager();
|
||||
}}
|
||||
>
|
||||
{`Open in ${fileManagerName}`}
|
||||
</Button>
|
||||
) : null}
|
||||
</CommandFooter>
|
||||
</Command>
|
||||
</CommandDialogPopup>
|
||||
|
||||
@@ -14,10 +14,12 @@ import {
|
||||
CommandList,
|
||||
CommandShortcut,
|
||||
} from "./ui/command";
|
||||
import { cn } from "~/lib/utils";
|
||||
|
||||
interface CommandPaletteResultsProps {
|
||||
emptyStateMessage?: string;
|
||||
groups: ReadonlyArray<CommandPaletteGroup>;
|
||||
highlightedItemValue?: string | null;
|
||||
isActionsOnly: boolean;
|
||||
keybindings: ResolvedKeybindingsConfig;
|
||||
onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
|
||||
@@ -46,6 +48,7 @@ export function CommandPaletteResults(props: CommandPaletteResultsProps) {
|
||||
item={item}
|
||||
key={item.value}
|
||||
keybindings={props.keybindings}
|
||||
isActive={props.highlightedItemValue === item.value}
|
||||
onExecuteItem={props.onExecuteItem}
|
||||
/>
|
||||
)}
|
||||
@@ -58,6 +61,7 @@ export function CommandPaletteResults(props: CommandPaletteResultsProps) {
|
||||
|
||||
function CommandPaletteResultRow(props: {
|
||||
item: CommandPaletteActionItem | CommandPaletteSubmenuItem;
|
||||
isActive: boolean;
|
||||
keybindings: ResolvedKeybindingsConfig;
|
||||
onExecuteItem: (item: CommandPaletteActionItem | CommandPaletteSubmenuItem) => void;
|
||||
}) {
|
||||
@@ -68,7 +72,10 @@ function CommandPaletteResultRow(props: {
|
||||
return (
|
||||
<CommandItem
|
||||
value={props.item.value}
|
||||
className="cursor-pointer gap-2"
|
||||
className={cn(
|
||||
"cursor-pointer gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit data-selected:bg-transparent data-selected:text-inherit [&[data-highlighted][data-selected]]:bg-transparent [&[data-highlighted][data-selected]]:text-inherit",
|
||||
props.isActive && "bg-accent! text-accent-foreground!",
|
||||
)}
|
||||
onMouseDown={(event) => {
|
||||
event.preventDefault();
|
||||
}}
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
ArrowUpDownIcon,
|
||||
ChevronRightIcon,
|
||||
CloudIcon,
|
||||
FolderIcon,
|
||||
GitPullRequestIcon,
|
||||
PlusIcon,
|
||||
SearchIcon,
|
||||
@@ -32,9 +31,7 @@ import { SortableContext, useSortable, verticalListSortingStrategy } from "@dnd-
|
||||
import { restrictToFirstScrollableAncestor, restrictToVerticalAxis } from "@dnd-kit/modifiers";
|
||||
import { CSS } from "@dnd-kit/utilities";
|
||||
import {
|
||||
DEFAULT_MODEL_BY_PROVIDER,
|
||||
type DesktopUpdateState,
|
||||
type EnvironmentId,
|
||||
ProjectId,
|
||||
type ScopedProjectRef,
|
||||
type ScopedThreadRef,
|
||||
@@ -58,7 +55,7 @@ import { usePrimaryEnvironmentId } from "../environments/primary";
|
||||
import { isElectron } from "../env";
|
||||
import { APP_STAGE_LABEL, APP_VERSION } from "../branding";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils";
|
||||
import { isMacPlatform, newCommandId } from "../lib/utils";
|
||||
import {
|
||||
selectProjectByRef,
|
||||
selectProjectsAcrossEnvironments,
|
||||
@@ -122,7 +119,7 @@ import {
|
||||
SidebarTrigger,
|
||||
} from "./ui/sidebar";
|
||||
import { useThreadSelectionStore } from "../threadSelectionStore";
|
||||
import { isNonEmpty as isNonEmptyString } from "effect/String";
|
||||
import { useCommandPaletteStore } from "../commandPaletteStore";
|
||||
import {
|
||||
getSidebarThreadIdsToPrewarm,
|
||||
resolveAdjacentThreadId,
|
||||
@@ -2033,20 +2030,7 @@ interface SidebarProjectsContentProps {
|
||||
projectSortOrder: SidebarProjectSortOrder;
|
||||
threadSortOrder: SidebarThreadSortOrder;
|
||||
updateSettings: ReturnType<typeof useUpdateSettings>["updateSettings"];
|
||||
shouldShowProjectPathEntry: boolean;
|
||||
handleStartAddProject: () => void;
|
||||
isElectron: boolean;
|
||||
isPickingFolder: boolean;
|
||||
isAddingProject: boolean;
|
||||
handlePickFolder: () => Promise<void>;
|
||||
addProjectInputRef: React.RefObject<HTMLInputElement | null>;
|
||||
addProjectError: string | null;
|
||||
newCwd: string;
|
||||
setNewCwd: React.Dispatch<React.SetStateAction<string>>;
|
||||
setAddProjectError: React.Dispatch<React.SetStateAction<string | null>>;
|
||||
handleAddProject: () => void;
|
||||
setAddingProject: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
canAddProject: boolean;
|
||||
openAddProject: () => void;
|
||||
isManualProjectSorting: boolean;
|
||||
projectDnDSensors: ReturnType<typeof useSensors>;
|
||||
projectCollisionDetection: CollisionDetection;
|
||||
@@ -2085,20 +2069,7 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
|
||||
projectSortOrder,
|
||||
threadSortOrder,
|
||||
updateSettings,
|
||||
shouldShowProjectPathEntry,
|
||||
handleStartAddProject,
|
||||
isElectron,
|
||||
isPickingFolder,
|
||||
isAddingProject,
|
||||
handlePickFolder,
|
||||
addProjectInputRef,
|
||||
addProjectError,
|
||||
newCwd,
|
||||
setNewCwd,
|
||||
setAddProjectError,
|
||||
handleAddProject,
|
||||
setAddingProject,
|
||||
canAddProject,
|
||||
openAddProject,
|
||||
isManualProjectSorting,
|
||||
projectDnDSensors,
|
||||
projectCollisionDetection,
|
||||
@@ -2137,26 +2108,6 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
|
||||
},
|
||||
[updateSettings],
|
||||
);
|
||||
const handleAddProjectInputChange = useCallback(
|
||||
(event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setNewCwd(event.target.value);
|
||||
setAddProjectError(null);
|
||||
},
|
||||
[setAddProjectError, setNewCwd],
|
||||
);
|
||||
const handleAddProjectInputKeyDown = useCallback(
|
||||
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === "Enter") handleAddProject();
|
||||
if (event.key === "Escape") {
|
||||
setAddingProject(false);
|
||||
setAddProjectError(null);
|
||||
}
|
||||
},
|
||||
[handleAddProject, setAddProjectError, setAddingProject],
|
||||
);
|
||||
const handleBrowseForFolderClick = useCallback(() => {
|
||||
void handlePickFolder();
|
||||
}, [handlePickFolder]);
|
||||
|
||||
return (
|
||||
<SidebarContent className="gap-0">
|
||||
@@ -2223,68 +2174,19 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
|
||||
render={
|
||||
<button
|
||||
type="button"
|
||||
aria-label={shouldShowProjectPathEntry ? "Cancel add project" : "Add project"}
|
||||
aria-pressed={shouldShowProjectPathEntry}
|
||||
aria-label="Add project"
|
||||
data-testid="sidebar-add-project-trigger"
|
||||
className="inline-flex size-5 cursor-pointer items-center justify-center rounded-md text-muted-foreground/60 transition-colors hover:bg-accent hover:text-foreground"
|
||||
onClick={handleStartAddProject}
|
||||
onClick={openAddProject}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<PlusIcon
|
||||
className={`size-3.5 transition-transform duration-150 ${
|
||||
shouldShowProjectPathEntry ? "rotate-45" : "rotate-0"
|
||||
}`}
|
||||
/>
|
||||
<PlusIcon className="size-3.5" />
|
||||
</TooltipTrigger>
|
||||
<TooltipPopup side="right">
|
||||
{shouldShowProjectPathEntry ? "Cancel add project" : "Add project"}
|
||||
</TooltipPopup>
|
||||
<TooltipPopup side="right">Add project</TooltipPopup>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
{shouldShowProjectPathEntry && (
|
||||
<div className="mb-2 px-1">
|
||||
{isElectron && (
|
||||
<button
|
||||
type="button"
|
||||
className="mb-1.5 flex w-full items-center justify-center gap-2 rounded-md border border-border bg-secondary py-1.5 text-xs text-foreground/80 transition-colors duration-150 hover:bg-accent hover:text-foreground disabled:cursor-not-allowed disabled:opacity-60"
|
||||
onClick={handleBrowseForFolderClick}
|
||||
disabled={isPickingFolder || isAddingProject}
|
||||
>
|
||||
<FolderIcon className="size-3.5" />
|
||||
{isPickingFolder ? "Picking folder..." : "Browse for folder"}
|
||||
</button>
|
||||
)}
|
||||
<div className="flex gap-1.5">
|
||||
<input
|
||||
ref={addProjectInputRef}
|
||||
className={`min-w-0 flex-1 rounded-md border bg-secondary px-2 py-1 font-mono text-xs text-foreground placeholder:text-muted-foreground/40 focus:outline-none ${
|
||||
addProjectError
|
||||
? "border-red-500/70 focus:border-red-500"
|
||||
: "border-border focus:border-ring"
|
||||
}`}
|
||||
placeholder="/path/to/project"
|
||||
value={newCwd}
|
||||
onChange={handleAddProjectInputChange}
|
||||
onKeyDown={handleAddProjectInputKeyDown}
|
||||
autoFocus
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="shrink-0 rounded-md bg-primary px-2.5 py-1 text-xs font-medium text-primary-foreground transition-colors duration-150 hover:bg-primary/90 disabled:opacity-60"
|
||||
onClick={handleAddProject}
|
||||
disabled={!canAddProject}
|
||||
>
|
||||
{isAddingProject ? "Adding..." : "Add"}
|
||||
</button>
|
||||
</div>
|
||||
{addProjectError && (
|
||||
<p className="mt-1 px-0.5 text-[11px] leading-tight text-red-400">
|
||||
{addProjectError}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isManualProjectSorting ? (
|
||||
<DndContext
|
||||
@@ -2359,7 +2261,7 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
|
||||
</SidebarMenu>
|
||||
)}
|
||||
|
||||
{projectsLength === 0 && !shouldShowProjectPathEntry && (
|
||||
{projectsLength === 0 && (
|
||||
<div className="px-2 pt-4 text-center text-xs text-muted-foreground/60">
|
||||
No projects yet
|
||||
</div>
|
||||
@@ -2372,7 +2274,6 @@ const SidebarProjectsContent = memo(function SidebarProjectsContent(
|
||||
export default function Sidebar() {
|
||||
const projects = useStore(useShallow(selectProjectsAcrossEnvironments));
|
||||
const sidebarThreads = useStore(useShallow(selectSidebarThreadsAcrossEnvironments));
|
||||
const activeEnvironmentId = useStore((store) => store.activeEnvironmentId);
|
||||
const projectExpandedById = useUiStateStore((store) => store.projectExpandedById);
|
||||
const projectOrder = useUiStateStore((store) => store.projectOrder);
|
||||
const reorderProjects = useUiStateStore((store) => store.reorderProjects);
|
||||
@@ -2381,7 +2282,6 @@ export default function Sidebar() {
|
||||
const isOnSettings = pathname.startsWith("/settings");
|
||||
const sidebarThreadSortOrder = useSettings((s) => s.sidebarThreadSortOrder);
|
||||
const sidebarProjectSortOrder = useSettings((s) => s.sidebarProjectSortOrder);
|
||||
const defaultThreadEnvMode = useSettings((s) => s.defaultThreadEnvMode);
|
||||
const { updateSettings } = useUpdateSettings();
|
||||
const { handleNewThread } = useNewThreadHandler();
|
||||
const { archiveThread, deleteThread } = useThreadActions();
|
||||
@@ -2391,12 +2291,7 @@ export default function Sidebar() {
|
||||
});
|
||||
const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null;
|
||||
const keybindings = useServerKeybindings();
|
||||
const [addingProject, setAddingProject] = useState(false);
|
||||
const [newCwd, setNewCwd] = useState("");
|
||||
const [isPickingFolder, setIsPickingFolder] = useState(false);
|
||||
const [isAddingProject, setIsAddingProject] = useState(false);
|
||||
const [addProjectError, setAddProjectError] = useState<string | null>(null);
|
||||
const addProjectInputRef = useRef<HTMLInputElement | null>(null);
|
||||
const openAddProjectCommandPalette = useCommandPaletteStore((store) => store.openAddProject);
|
||||
const [expandedThreadListsByProject, setExpandedThreadListsByProject] = useState<
|
||||
ReadonlySet<string>
|
||||
>(() => new Set());
|
||||
@@ -2408,10 +2303,7 @@ export default function Sidebar() {
|
||||
const selectedThreadCount = useThreadSelectionStore((s) => s.selectedThreadKeys.size);
|
||||
const clearSelection = useThreadSelectionStore((s) => s.clearSelection);
|
||||
const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor);
|
||||
const isLinuxDesktop = isElectron && isLinuxPlatform(navigator.platform);
|
||||
const platform = navigator.platform;
|
||||
const shouldBrowseForProjectImmediately = isElectron && !isLinuxDesktop;
|
||||
const shouldShowProjectPathEntry = addingProject && !shouldBrowseForProjectImmediately;
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const savedEnvironmentRegistry = useSavedEnvironmentRegistryStore((s) => s.byId);
|
||||
const savedEnvironmentRuntimeById = useSavedEnvironmentRuntimeStore((s) => s.byId);
|
||||
@@ -2574,131 +2466,6 @@ export default function Sidebar() {
|
||||
const newThreadShortcutLabel =
|
||||
shortcutLabelForCommand(keybindings, "chat.newLocal", newThreadShortcutLabelOptions) ??
|
||||
shortcutLabelForCommand(keybindings, "chat.new", newThreadShortcutLabelOptions);
|
||||
const focusMostRecentThreadForProject = useCallback(
|
||||
(projectRef: { environmentId: EnvironmentId; projectId: ProjectId }) => {
|
||||
const physicalKey = scopedProjectKey(
|
||||
scopeProjectRef(projectRef.environmentId, projectRef.projectId),
|
||||
);
|
||||
const logicalKey = physicalToLogicalKey.get(physicalKey) ?? physicalKey;
|
||||
const latestThread = sortThreads(
|
||||
(threadsByProjectKey.get(logicalKey) ?? []).filter((thread) => thread.archivedAt === null),
|
||||
sidebarThreadSortOrder,
|
||||
)[0];
|
||||
if (!latestThread) return;
|
||||
|
||||
void navigate({
|
||||
to: "/$environmentId/$threadId",
|
||||
params: buildThreadRouteParams(scopeThreadRef(latestThread.environmentId, latestThread.id)),
|
||||
});
|
||||
},
|
||||
[sidebarThreadSortOrder, navigate, threadsByProjectKey, physicalToLogicalKey],
|
||||
);
|
||||
|
||||
const addProjectFromInput = useCallback(
|
||||
async (rawCwd: string) => {
|
||||
const cwd = rawCwd.trim();
|
||||
if (!cwd || isAddingProject) return;
|
||||
const api = activeEnvironmentId ? readEnvironmentApi(activeEnvironmentId) : undefined;
|
||||
if (!api) return;
|
||||
|
||||
setIsAddingProject(true);
|
||||
const finishAddingProject = () => {
|
||||
setIsAddingProject(false);
|
||||
setNewCwd("");
|
||||
setAddProjectError(null);
|
||||
setAddingProject(false);
|
||||
};
|
||||
|
||||
const existing = projects.find((project) => project.cwd === cwd);
|
||||
if (existing) {
|
||||
focusMostRecentThreadForProject({
|
||||
environmentId: existing.environmentId,
|
||||
projectId: existing.id,
|
||||
});
|
||||
finishAddingProject();
|
||||
return;
|
||||
}
|
||||
|
||||
const projectId = newProjectId();
|
||||
const title = cwd.split(/[/\\]/).findLast(isNonEmptyString) ?? cwd;
|
||||
try {
|
||||
await api.orchestration.dispatchCommand({
|
||||
type: "project.create",
|
||||
commandId: newCommandId(),
|
||||
projectId,
|
||||
title,
|
||||
workspaceRoot: cwd,
|
||||
defaultModelSelection: {
|
||||
provider: "codex",
|
||||
model: DEFAULT_MODEL_BY_PROVIDER.codex,
|
||||
},
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
if (activeEnvironmentId !== null) {
|
||||
await handleNewThread(scopeProjectRef(activeEnvironmentId, projectId), {
|
||||
envMode: defaultThreadEnvMode,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
} catch (error) {
|
||||
const description =
|
||||
error instanceof Error ? error.message : "An error occurred while adding the project.";
|
||||
setIsAddingProject(false);
|
||||
if (shouldBrowseForProjectImmediately) {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Failed to add project",
|
||||
description,
|
||||
});
|
||||
} else {
|
||||
setAddProjectError(description);
|
||||
}
|
||||
return;
|
||||
}
|
||||
finishAddingProject();
|
||||
},
|
||||
[
|
||||
focusMostRecentThreadForProject,
|
||||
activeEnvironmentId,
|
||||
handleNewThread,
|
||||
isAddingProject,
|
||||
projects,
|
||||
shouldBrowseForProjectImmediately,
|
||||
defaultThreadEnvMode,
|
||||
],
|
||||
);
|
||||
|
||||
const handleAddProject = () => {
|
||||
void addProjectFromInput(newCwd);
|
||||
};
|
||||
|
||||
const canAddProject = newCwd.trim().length > 0 && !isAddingProject;
|
||||
|
||||
const handlePickFolder = async () => {
|
||||
const api = readLocalApi();
|
||||
if (!api || isPickingFolder) return;
|
||||
setIsPickingFolder(true);
|
||||
let pickedPath: string | null = null;
|
||||
try {
|
||||
pickedPath = await api.dialogs.pickFolder();
|
||||
} catch {
|
||||
// Ignore picker failures and leave the current thread selection unchanged.
|
||||
}
|
||||
if (pickedPath) {
|
||||
await addProjectFromInput(pickedPath);
|
||||
} else if (!shouldBrowseForProjectImmediately) {
|
||||
addProjectInputRef.current?.focus();
|
||||
}
|
||||
setIsPickingFolder(false);
|
||||
};
|
||||
|
||||
const handleStartAddProject = () => {
|
||||
setAddProjectError(null);
|
||||
if (shouldBrowseForProjectImmediately) {
|
||||
void handlePickFolder();
|
||||
return;
|
||||
}
|
||||
setAddingProject((prev) => !prev);
|
||||
};
|
||||
|
||||
const navigateToThread = useCallback(
|
||||
(threadRef: ScopedThreadRef) => {
|
||||
@@ -3212,20 +2979,7 @@ export default function Sidebar() {
|
||||
projectSortOrder={sidebarProjectSortOrder}
|
||||
threadSortOrder={sidebarThreadSortOrder}
|
||||
updateSettings={updateSettings}
|
||||
shouldShowProjectPathEntry={shouldShowProjectPathEntry}
|
||||
handleStartAddProject={handleStartAddProject}
|
||||
isElectron={isElectron}
|
||||
isPickingFolder={isPickingFolder}
|
||||
isAddingProject={isAddingProject}
|
||||
handlePickFolder={handlePickFolder}
|
||||
addProjectInputRef={addProjectInputRef}
|
||||
addProjectError={addProjectError}
|
||||
newCwd={newCwd}
|
||||
setNewCwd={setNewCwd}
|
||||
setAddProjectError={setAddProjectError}
|
||||
handleAddProject={handleAddProject}
|
||||
setAddingProject={setAddingProject}
|
||||
canAddProject={canAddProject}
|
||||
openAddProject={openAddProjectCommandPalette}
|
||||
isManualProjectSorting={isManualProjectSorting}
|
||||
projectDnDSensors={projectDnDSensors}
|
||||
projectCollisionDetection={projectCollisionDetection}
|
||||
|
||||
@@ -444,6 +444,9 @@ export function useSettingsRestore(onRestored?: () => void) {
|
||||
...(settings.defaultThreadEnvMode !== DEFAULT_UNIFIED_SETTINGS.defaultThreadEnvMode
|
||||
? ["New thread mode"]
|
||||
: []),
|
||||
...(settings.addProjectBaseDirectory !== DEFAULT_UNIFIED_SETTINGS.addProjectBaseDirectory
|
||||
? ["Add project base directory"]
|
||||
: []),
|
||||
...(settings.confirmThreadArchive !== DEFAULT_UNIFIED_SETTINGS.confirmThreadArchive
|
||||
? ["Archive confirmation"]
|
||||
: []),
|
||||
@@ -458,6 +461,7 @@ export function useSettingsRestore(onRestored?: () => void) {
|
||||
isGitWritingModelDirty,
|
||||
settings.confirmThreadArchive,
|
||||
settings.confirmThreadDelete,
|
||||
settings.addProjectBaseDirectory,
|
||||
settings.defaultThreadEnvMode,
|
||||
settings.diffWordWrap,
|
||||
settings.enableAssistantStreaming,
|
||||
@@ -919,6 +923,34 @@ export function GeneralSettingsPanel() {
|
||||
}
|
||||
/>
|
||||
|
||||
<SettingsRow
|
||||
title="Add project starts in"
|
||||
description='Leave empty to use "~/" when the Add Project browser opens.'
|
||||
resetAction={
|
||||
settings.addProjectBaseDirectory !==
|
||||
DEFAULT_UNIFIED_SETTINGS.addProjectBaseDirectory ? (
|
||||
<SettingResetButton
|
||||
label="add project base directory"
|
||||
onClick={() =>
|
||||
updateSettings({
|
||||
addProjectBaseDirectory: DEFAULT_UNIFIED_SETTINGS.addProjectBaseDirectory,
|
||||
})
|
||||
}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
control={
|
||||
<Input
|
||||
className="w-full sm:w-72"
|
||||
value={settings.addProjectBaseDirectory}
|
||||
onChange={(event) => updateSettings({ addProjectBaseDirectory: event.target.value })}
|
||||
placeholder="~/"
|
||||
spellCheck={false}
|
||||
aria-label="Add project base directory"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
<SettingsRow
|
||||
title="Archive confirmation"
|
||||
description="Require a second click on the inline archive action before a thread is archived."
|
||||
|
||||
@@ -3,6 +3,8 @@ import type { EnvironmentId, EnvironmentApi } from "@t3tools/contracts";
|
||||
import type { WsRpcClient } from "./rpc/wsRpcClient";
|
||||
import { readEnvironmentConnection } from "./environments/runtime";
|
||||
|
||||
const environmentApiOverridesForTests = new Map<EnvironmentId, EnvironmentApi>();
|
||||
|
||||
export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi {
|
||||
return {
|
||||
terminal: {
|
||||
@@ -18,6 +20,9 @@ export function createEnvironmentApi(rpcClient: WsRpcClient): EnvironmentApi {
|
||||
searchEntries: rpcClient.projects.searchEntries,
|
||||
writeFile: rpcClient.projects.writeFile,
|
||||
},
|
||||
filesystem: {
|
||||
browse: rpcClient.filesystem.browse,
|
||||
},
|
||||
git: {
|
||||
pull: rpcClient.git.pull,
|
||||
refreshStatus: rpcClient.git.refreshStatus,
|
||||
@@ -52,6 +57,11 @@ export function readEnvironmentApi(environmentId: EnvironmentId): EnvironmentApi
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const overriddenApi = environmentApiOverridesForTests.get(environmentId);
|
||||
if (overriddenApi) {
|
||||
return overriddenApi;
|
||||
}
|
||||
|
||||
const connection = readEnvironmentConnection(environmentId);
|
||||
return connection ? createEnvironmentApi(connection.client) : undefined;
|
||||
}
|
||||
@@ -63,3 +73,14 @@ export function ensureEnvironmentApi(environmentId: EnvironmentId): EnvironmentA
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
export function __setEnvironmentApiOverrideForTests(
|
||||
environmentId: EnvironmentId,
|
||||
api: EnvironmentApi,
|
||||
): void {
|
||||
environmentApiOverridesForTests.set(environmentId, api);
|
||||
}
|
||||
|
||||
export function __resetEnvironmentApiOverridesForTests(): void {
|
||||
environmentApiOverridesForTests.clear();
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
appendBrowsePathSegment,
|
||||
canNavigateUp,
|
||||
getBrowseDirectoryPath,
|
||||
findProjectByPath,
|
||||
getBrowseLeafPathSegment,
|
||||
getBrowseParentPath,
|
||||
hasTrailingPathSeparator,
|
||||
inferProjectTitleFromPath,
|
||||
isExplicitRelativeProjectPath,
|
||||
isFilesystemBrowseQuery,
|
||||
normalizeProjectPathForComparison,
|
||||
normalizeProjectPathForDispatch,
|
||||
isUnsupportedWindowsProjectPath,
|
||||
resolveProjectPathForDispatch,
|
||||
} from "./projectPaths";
|
||||
|
||||
describe("projectPaths", () => {
|
||||
it("normalizes trailing separators for dispatch and comparison", () => {
|
||||
expect(normalizeProjectPathForDispatch(" /repo/app/ ")).toBe("/repo/app");
|
||||
expect(normalizeProjectPathForComparison("/repo/app/")).toBe("/repo/app");
|
||||
});
|
||||
|
||||
it("normalizes windows-style paths for comparison", () => {
|
||||
expect(normalizeProjectPathForComparison("C:/Work/Repo/")).toBe("c:\\work\\repo");
|
||||
expect(normalizeProjectPathForComparison("C:\\Work\\Repo\\")).toBe("c:\\work\\repo");
|
||||
});
|
||||
|
||||
it("finds existing projects even when the input formatting differs", () => {
|
||||
const existing = findProjectByPath(
|
||||
[
|
||||
{ id: "project-1", cwd: "/repo/app" },
|
||||
{ id: "project-2", cwd: "C:\\Work\\Repo" },
|
||||
],
|
||||
"C:/Work/Repo/",
|
||||
);
|
||||
|
||||
expect(existing?.id).toBe("project-2");
|
||||
});
|
||||
|
||||
it("infers project titles from normalized paths", () => {
|
||||
expect(inferProjectTitleFromPath("/repo/app/")).toBe("app");
|
||||
expect(inferProjectTitleFromPath("C:\\Work\\Repo\\")).toBe("Repo");
|
||||
expect(inferProjectTitleFromPath("/home/user\\project/")).toBe("user\\project");
|
||||
});
|
||||
|
||||
it("detects browse queries across supported path styles", () => {
|
||||
expect(isFilesystemBrowseQuery(".")).toBe(false);
|
||||
expect(isFilesystemBrowseQuery("..")).toBe(false);
|
||||
expect(isFilesystemBrowseQuery("./")).toBe(true);
|
||||
expect(isFilesystemBrowseQuery("../")).toBe(true);
|
||||
expect(isFilesystemBrowseQuery("~/projects")).toBe(true);
|
||||
expect(isFilesystemBrowseQuery("..\\docs")).toBe(true);
|
||||
expect(isFilesystemBrowseQuery("notes")).toBe(false);
|
||||
});
|
||||
|
||||
it("only treats windows-style paths as browse queries on windows", () => {
|
||||
expect(isFilesystemBrowseQuery("C:\\Work\\Repo\\", "MacIntel")).toBe(false);
|
||||
expect(isFilesystemBrowseQuery("C:\\Work\\Repo\\", "Win32")).toBe(true);
|
||||
expect(isUnsupportedWindowsProjectPath("C:\\Work\\Repo\\", "MacIntel")).toBe(true);
|
||||
expect(isUnsupportedWindowsProjectPath("C:\\Work\\Repo\\", "Win32")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects explicit relative project paths", () => {
|
||||
expect(isExplicitRelativeProjectPath(".")).toBe(true);
|
||||
expect(isExplicitRelativeProjectPath("..")).toBe(true);
|
||||
expect(isExplicitRelativeProjectPath("./docs")).toBe(true);
|
||||
expect(isExplicitRelativeProjectPath("..\\docs")).toBe(true);
|
||||
expect(isExplicitRelativeProjectPath("/repo/docs")).toBe(false);
|
||||
});
|
||||
|
||||
it("resolves explicit relative paths against the current project", () => {
|
||||
expect(resolveProjectPathForDispatch(".", "/repo/app")).toBe("/repo/app");
|
||||
expect(resolveProjectPathForDispatch("..", "/repo/app")).toBe("/repo");
|
||||
expect(resolveProjectPathForDispatch("./docs", "/repo/app")).toBe("/repo/app/docs");
|
||||
expect(resolveProjectPathForDispatch("../docs", "/repo/app")).toBe("/repo/docs");
|
||||
expect(resolveProjectPathForDispatch("./Repo", "C:\\Work")).toBe("C:\\Work\\Repo");
|
||||
expect(resolveProjectPathForDispatch("./docs", "/home/user\\project")).toBe(
|
||||
"/home/user\\project/docs",
|
||||
);
|
||||
});
|
||||
|
||||
it("navigates browse paths with matching separators", () => {
|
||||
expect(appendBrowsePathSegment("/repo/", "src")).toBe("/repo/src/");
|
||||
expect(appendBrowsePathSegment("C:\\Work\\", "Repo")).toBe("C:\\Work\\Repo\\");
|
||||
expect(appendBrowsePathSegment("/home/user\\project/", "docs")).toBe(
|
||||
"/home/user\\project/docs/",
|
||||
);
|
||||
expect(getBrowseParentPath("/repo/src/")).toBe("/repo/");
|
||||
expect(getBrowseParentPath("C:\\Work\\Repo\\")).toBe("C:\\Work\\");
|
||||
expect(getBrowseParentPath("\\\\server\\share\\")).toBeNull();
|
||||
expect(getBrowseParentPath("\\\\server\\share\\repo\\")).toBe("\\\\server\\share\\");
|
||||
expect(getBrowseParentPath("C:\\")).toBeNull();
|
||||
expect(getBrowseParentPath("/home/user\\project/docs/")).toBe("/home/user\\project/");
|
||||
});
|
||||
|
||||
it("detects browse path boundaries", () => {
|
||||
expect(hasTrailingPathSeparator("/repo/src/")).toBe(true);
|
||||
expect(hasTrailingPathSeparator("/repo/src")).toBe(false);
|
||||
expect(getBrowseDirectoryPath("/repo/src")).toBe("/repo/");
|
||||
expect(getBrowseDirectoryPath("/repo/src/")).toBe("/repo/src/");
|
||||
expect(getBrowseLeafPathSegment("/repo/src")).toBe("src");
|
||||
expect(getBrowseLeafPathSegment("C:\\Work\\Repo\\Docs")).toBe("Docs");
|
||||
expect(getBrowseDirectoryPath("/home/user\\project/docs")).toBe("/home/user\\project/");
|
||||
expect(getBrowseLeafPathSegment("/home/user\\project/docs")).toBe("docs");
|
||||
});
|
||||
|
||||
it("only allows browse-up after entering a directory", () => {
|
||||
expect(canNavigateUp("~/repo")).toBe(false);
|
||||
expect(canNavigateUp("~/a")).toBe(false);
|
||||
expect(canNavigateUp("~/repo/")).toBe(true);
|
||||
expect(canNavigateUp("\\\\server\\share\\")).toBe(false);
|
||||
expect(canNavigateUp("\\\\server\\share\\repo\\")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,259 @@
|
||||
import {
|
||||
isExplicitRelativePath,
|
||||
isUncPath,
|
||||
isWindowsAbsolutePath,
|
||||
isWindowsDrivePath,
|
||||
} from "@t3tools/shared/path";
|
||||
import { isWindowsPlatform } from "./utils";
|
||||
|
||||
function isRootPath(value: string): boolean {
|
||||
return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]?$/.test(value);
|
||||
}
|
||||
|
||||
function getAbsolutePathKind(value: string): "unix" | "windows" | null {
|
||||
if (isWindowsDrivePath(value) || isUncPath(value)) {
|
||||
return "windows";
|
||||
}
|
||||
|
||||
if (value.startsWith("/")) {
|
||||
return "unix";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function trimTrailingPathSeparators(value: string): string {
|
||||
if (value.length === 0 || isRootPath(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const trimmed =
|
||||
getAbsolutePathKind(value) === "unix"
|
||||
? value.replace(/\/+$/g, "")
|
||||
: value.replace(/[\\/]+$/g, "");
|
||||
if (trimmed.length === 0) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
|
||||
}
|
||||
|
||||
function preferredPathSeparator(value: string): "/" | "\\" {
|
||||
const absolutePathKind = getAbsolutePathKind(value);
|
||||
if (absolutePathKind === "windows") {
|
||||
return "\\";
|
||||
}
|
||||
if (absolutePathKind === "unix") {
|
||||
return "/";
|
||||
}
|
||||
|
||||
return value.includes("\\") ? "\\" : "/";
|
||||
}
|
||||
|
||||
export function hasTrailingPathSeparator(value: string): boolean {
|
||||
return (getAbsolutePathKind(value) === "unix" ? /\/$/ : /[\\/]$/).test(value);
|
||||
}
|
||||
|
||||
export { isExplicitRelativePath as isExplicitRelativeProjectPath };
|
||||
|
||||
function splitPathSegments(value: string, separator: "/" | "\\"): string[] {
|
||||
return value.split(separator === "/" ? /\/+/ : /[\\/]+/).filter(Boolean);
|
||||
}
|
||||
|
||||
function getLastPathSeparatorIndex(value: string): number {
|
||||
if (getAbsolutePathKind(value) === "unix") {
|
||||
return value.lastIndexOf("/");
|
||||
}
|
||||
|
||||
return Math.max(value.lastIndexOf("/"), value.lastIndexOf("\\"));
|
||||
}
|
||||
|
||||
function splitAbsolutePath(value: string): {
|
||||
root: string;
|
||||
separator: "/" | "\\";
|
||||
segments: string[];
|
||||
} | null {
|
||||
if (isWindowsDrivePath(value)) {
|
||||
const root = `${value.slice(0, 2)}\\`;
|
||||
const segments = splitPathSegments(value.slice(root.length), "\\");
|
||||
return { root, separator: "\\", segments };
|
||||
}
|
||||
if (isUncPath(value)) {
|
||||
const segments = splitPathSegments(value, "\\");
|
||||
const [server, share, ...rest] = segments;
|
||||
if (!server || !share) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
root: `\\\\${server}\\${share}\\`,
|
||||
separator: "\\",
|
||||
segments: rest,
|
||||
};
|
||||
}
|
||||
if (value.startsWith("/")) {
|
||||
return {
|
||||
root: "/",
|
||||
separator: "/",
|
||||
segments: splitPathSegments(value.slice(1), "/"),
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isFilesystemBrowseQuery(
|
||||
value: string,
|
||||
platform = typeof navigator === "undefined" ? "" : navigator.platform,
|
||||
): boolean {
|
||||
const allowWindowsPaths = isWindowsPlatform(platform);
|
||||
return (
|
||||
value.startsWith("./") ||
|
||||
value.startsWith("../") ||
|
||||
value.startsWith(".\\") ||
|
||||
value.startsWith("..\\") ||
|
||||
value.startsWith("/") ||
|
||||
value.startsWith("~/") ||
|
||||
(allowWindowsPaths && isWindowsAbsolutePath(value))
|
||||
);
|
||||
}
|
||||
|
||||
export function isUnsupportedWindowsProjectPath(value: string, platform: string): boolean {
|
||||
return isWindowsAbsolutePath(value) && !isWindowsPlatform(platform);
|
||||
}
|
||||
|
||||
export function normalizeProjectPathForDispatch(value: string): string {
|
||||
return trimTrailingPathSeparators(value.trim());
|
||||
}
|
||||
|
||||
export function resolveProjectPathForDispatch(value: string, cwd?: string | null): string {
|
||||
const trimmedValue = value.trim();
|
||||
if (!isExplicitRelativePath(trimmedValue) || !cwd) {
|
||||
return normalizeProjectPathForDispatch(trimmedValue);
|
||||
}
|
||||
|
||||
const absoluteBase = splitAbsolutePath(normalizeProjectPathForDispatch(cwd));
|
||||
if (!absoluteBase) {
|
||||
return normalizeProjectPathForDispatch(trimmedValue);
|
||||
}
|
||||
|
||||
const nextSegments = [...absoluteBase.segments];
|
||||
for (const segment of trimmedValue.split(/[\\/]+/)) {
|
||||
if (segment.length === 0 || segment === ".") {
|
||||
continue;
|
||||
}
|
||||
if (segment === "..") {
|
||||
nextSegments.pop();
|
||||
continue;
|
||||
}
|
||||
nextSegments.push(segment);
|
||||
}
|
||||
|
||||
const joinedPath = nextSegments.join(absoluteBase.separator);
|
||||
if (joinedPath.length === 0) {
|
||||
return normalizeProjectPathForDispatch(absoluteBase.root);
|
||||
}
|
||||
|
||||
return normalizeProjectPathForDispatch(`${absoluteBase.root}${joinedPath}`);
|
||||
}
|
||||
|
||||
export function normalizeProjectPathForComparison(value: string): string {
|
||||
const normalized = normalizeProjectPathForDispatch(value);
|
||||
if (isWindowsDrivePath(normalized) || normalized.startsWith("\\\\")) {
|
||||
return normalized.replaceAll("/", "\\").toLowerCase();
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function findProjectByPath<T extends { cwd: string }>(
|
||||
projects: ReadonlyArray<T>,
|
||||
candidatePath: string,
|
||||
): T | undefined {
|
||||
const normalizedCandidate = normalizeProjectPathForComparison(candidatePath);
|
||||
if (normalizedCandidate.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return projects.find(
|
||||
(project) => normalizeProjectPathForComparison(project.cwd) === normalizedCandidate,
|
||||
);
|
||||
}
|
||||
|
||||
export function inferProjectTitleFromPath(value: string): string {
|
||||
const normalized = normalizeProjectPathForDispatch(value);
|
||||
const absolutePath = splitAbsolutePath(normalized);
|
||||
if (absolutePath) {
|
||||
return absolutePath.segments.findLast(Boolean) ?? normalized;
|
||||
}
|
||||
|
||||
const segments = normalized.split(/[/\\]/);
|
||||
return segments.findLast(Boolean) ?? normalized;
|
||||
}
|
||||
|
||||
export function appendBrowsePathSegment(currentPath: string, segment: string): string {
|
||||
const separator = preferredPathSeparator(currentPath);
|
||||
return `${getBrowseDirectoryPath(currentPath)}${segment}${separator}`;
|
||||
}
|
||||
|
||||
export function getBrowseLeafPathSegment(currentPath: string): string {
|
||||
const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
|
||||
return currentPath.slice(lastSeparatorIndex + 1);
|
||||
}
|
||||
|
||||
export function getBrowseDirectoryPath(currentPath: string): string {
|
||||
if (hasTrailingPathSeparator(currentPath)) {
|
||||
return currentPath;
|
||||
}
|
||||
|
||||
const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
|
||||
if (lastSeparatorIndex < 0) {
|
||||
return currentPath;
|
||||
}
|
||||
|
||||
return currentPath.slice(0, lastSeparatorIndex + 1);
|
||||
}
|
||||
|
||||
export function ensureBrowseDirectoryPath(currentPath: string): string {
|
||||
const trimmed = currentPath.trim();
|
||||
if (trimmed.length === 0) {
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
if (hasTrailingPathSeparator(trimmed)) {
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
return `${trimmed}${preferredPathSeparator(trimmed)}`;
|
||||
}
|
||||
|
||||
export function getBrowseParentPath(currentPath: string): string | null {
|
||||
const trimmed = trimTrailingPathSeparators(currentPath);
|
||||
const absolutePath = splitAbsolutePath(trimmed);
|
||||
if (absolutePath) {
|
||||
if (absolutePath.segments.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (absolutePath.segments.length === 1) {
|
||||
return absolutePath.root;
|
||||
}
|
||||
|
||||
const parentSegments = absolutePath.segments.slice(0, -1).join(absolutePath.separator);
|
||||
return `${absolutePath.root}${parentSegments}${absolutePath.separator}`;
|
||||
}
|
||||
|
||||
const separator = preferredPathSeparator(currentPath);
|
||||
const lastSeparatorIndex = getLastPathSeparatorIndex(trimmed);
|
||||
|
||||
if (lastSeparatorIndex < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lastSeparatorIndex === 2 && /^[a-zA-Z]:/.test(trimmed)) {
|
||||
return `${trimmed.slice(0, 2)}${separator}`;
|
||||
}
|
||||
|
||||
return trimmed.slice(0, lastSeparatorIndex + 1);
|
||||
}
|
||||
|
||||
export function canNavigateUp(currentPath: string): boolean {
|
||||
return hasTrailingPathSeparator(currentPath) && getBrowseParentPath(currentPath) !== null;
|
||||
}
|
||||
@@ -51,6 +51,9 @@ const rpcClientMock = {
|
||||
searchEntries: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
},
|
||||
filesystem: {
|
||||
browse: vi.fn(),
|
||||
},
|
||||
shell: {
|
||||
openInEditor: vi.fn(),
|
||||
},
|
||||
@@ -419,6 +422,25 @@ describe("wsApi", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("forwards filesystem browse requests to the RPC client", async () => {
|
||||
rpcClientMock.filesystem.browse.mockResolvedValue({
|
||||
parentPath: "/tmp/project/",
|
||||
entries: [],
|
||||
});
|
||||
const { createEnvironmentApi } = await import("./environmentApi");
|
||||
|
||||
const api = createEnvironmentApi(rpcClientMock as never);
|
||||
await api.filesystem.browse({
|
||||
partialPath: "/tmp/project/",
|
||||
cwd: "/tmp/project",
|
||||
});
|
||||
|
||||
expect(rpcClientMock.filesystem.browse).toHaveBeenCalledWith({
|
||||
partialPath: "/tmp/project/",
|
||||
cwd: "/tmp/project",
|
||||
});
|
||||
});
|
||||
|
||||
it("forwards full-thread diff requests to the orchestration RPC", async () => {
|
||||
rpcClientMock.orchestration.getFullThreadDiff.mockResolvedValue({ diff: "patch" });
|
||||
const { createEnvironmentApi } = await import("./environmentApi");
|
||||
@@ -481,6 +503,19 @@ describe("wsApi", () => {
|
||||
expect(showContextMenu).toHaveBeenCalledWith(items, undefined);
|
||||
});
|
||||
|
||||
it("forwards folder picker options to the desktop bridge", async () => {
|
||||
const pickFolder = vi.fn().mockResolvedValue("/tmp/project");
|
||||
getWindowForTest().desktopBridge = makeDesktopBridge({ pickFolder });
|
||||
|
||||
const { createLocalApi } = await import("./localApi");
|
||||
const api = createLocalApi(rpcClientMock as never);
|
||||
|
||||
await expect(api.dialogs.pickFolder({ initialPath: "/tmp/workspace" })).resolves.toBe(
|
||||
"/tmp/project",
|
||||
);
|
||||
expect(pickFolder).toHaveBeenCalledWith({ initialPath: "/tmp/workspace" });
|
||||
});
|
||||
|
||||
it("falls back to the browser context menu helper when the desktop bridge is missing", async () => {
|
||||
showContextMenuFallbackMock.mockResolvedValue("rename");
|
||||
const { createLocalApi } = await import("./localApi");
|
||||
|
||||
@@ -29,9 +29,9 @@ let cachedApi: LocalApi | undefined;
|
||||
export function createLocalApi(rpcClient: WsRpcClient): LocalApi {
|
||||
return {
|
||||
dialogs: {
|
||||
pickFolder: async () => {
|
||||
pickFolder: async (options) => {
|
||||
if (!window.desktopBridge) return null;
|
||||
return window.desktopBridge.pickFolder();
|
||||
return window.desktopBridge.pickFolder(options);
|
||||
},
|
||||
confirm: async (message) => {
|
||||
if (window.desktopBridge) {
|
||||
|
||||
@@ -68,6 +68,9 @@ export interface WsRpcClient {
|
||||
readonly searchEntries: RpcUnaryMethod<typeof WS_METHODS.projectsSearchEntries>;
|
||||
readonly writeFile: RpcUnaryMethod<typeof WS_METHODS.projectsWriteFile>;
|
||||
};
|
||||
readonly filesystem: {
|
||||
readonly browse: RpcUnaryMethod<typeof WS_METHODS.filesystemBrowse>;
|
||||
};
|
||||
readonly shell: {
|
||||
readonly openInEditor: (input: {
|
||||
readonly cwd: Parameters<LocalApi["shell"]["openInEditor"]>[0];
|
||||
@@ -145,6 +148,9 @@ export function createWsRpcClient(transport: WsTransport): WsRpcClient {
|
||||
writeFile: (input) =>
|
||||
transport.request((client) => client[WS_METHODS.projectsWriteFile](input)),
|
||||
},
|
||||
filesystem: {
|
||||
browse: (input) => transport.request((client) => client[WS_METHODS.filesystemBrowse](input)),
|
||||
},
|
||||
shell: {
|
||||
openInEditor: (input) =>
|
||||
transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)),
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Schema } from "effect";
|
||||
import { TrimmedNonEmptyString } from "./baseSchemas";
|
||||
|
||||
const FILESYSTEM_PATH_MAX_LENGTH = 512;
|
||||
|
||||
export const FilesystemBrowseInput = Schema.Struct({
|
||||
partialPath: TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH)),
|
||||
cwd: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(FILESYSTEM_PATH_MAX_LENGTH))),
|
||||
});
|
||||
export type FilesystemBrowseInput = typeof FilesystemBrowseInput.Type;
|
||||
|
||||
export const FilesystemBrowseEntry = Schema.Struct({
|
||||
name: TrimmedNonEmptyString,
|
||||
fullPath: TrimmedNonEmptyString,
|
||||
});
|
||||
export type FilesystemBrowseEntry = typeof FilesystemBrowseEntry.Type;
|
||||
|
||||
export const FilesystemBrowseResult = Schema.Struct({
|
||||
parentPath: TrimmedNonEmptyString,
|
||||
entries: Schema.Array(FilesystemBrowseEntry),
|
||||
});
|
||||
export type FilesystemBrowseResult = typeof FilesystemBrowseResult.Type;
|
||||
|
||||
export class FilesystemBrowseError extends Schema.TaggedErrorClass<FilesystemBrowseError>()(
|
||||
"FilesystemBrowseError",
|
||||
{
|
||||
message: TrimmedNonEmptyString,
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
},
|
||||
) {}
|
||||
@@ -13,4 +13,5 @@ export * from "./git";
|
||||
export * from "./orchestration";
|
||||
export * from "./editor";
|
||||
export * from "./project";
|
||||
export * from "./filesystem";
|
||||
export * from "./rpc";
|
||||
@@ -18,6 +18,7 @@ import type {
|
||||
GitStatusResult,
|
||||
GitCreateBranchResult,
|
||||
} from "./git";
|
||||
import type { FilesystemBrowseInput, FilesystemBrowseResult } from "./filesystem";
|
||||
import type {
|
||||
ProjectSearchEntriesInput,
|
||||
ProjectSearchEntriesResult,
|
||||
@@ -140,6 +141,10 @@ export interface DesktopServerExposureState {
|
||||
advertisedHost: string | null;
|
||||
}
|
||||
|
||||
export interface PickFolderOptions {
|
||||
initialPath?: string | null;
|
||||
}
|
||||
|
||||
export interface DesktopBridge {
|
||||
getAppBranding: () => DesktopAppBranding | null;
|
||||
getLocalEnvironmentBootstrap: () => DesktopEnvironmentBootstrap | null;
|
||||
@@ -154,7 +159,7 @@ export interface DesktopBridge {
|
||||
removeSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise<void>;
|
||||
getServerExposureState: () => Promise<DesktopServerExposureState>;
|
||||
setServerExposureMode: (mode: DesktopServerExposureMode) => Promise<DesktopServerExposureState>;
|
||||
pickFolder: () => Promise<string | null>;
|
||||
pickFolder: (options?: PickFolderOptions) => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
setTheme: (theme: DesktopTheme) => Promise<void>;
|
||||
showContextMenu: <T extends string>(
|
||||
@@ -183,7 +188,7 @@ export interface DesktopBridge {
|
||||
*/
|
||||
export interface LocalApi {
|
||||
dialogs: {
|
||||
pickFolder: () => Promise<string | null>;
|
||||
pickFolder: (options?: PickFolderOptions) => Promise<string | null>;
|
||||
confirm: (message: string) => Promise<boolean>;
|
||||
};
|
||||
shell: {
|
||||
@@ -239,6 +244,9 @@ export interface EnvironmentApi {
|
||||
searchEntries: (input: ProjectSearchEntriesInput) => Promise<ProjectSearchEntriesResult>;
|
||||
writeFile: (input: ProjectWriteFileInput) => Promise<ProjectWriteFileResult>;
|
||||
};
|
||||
filesystem: {
|
||||
browse: (input: FilesystemBrowseInput) => Promise<FilesystemBrowseResult>;
|
||||
};
|
||||
git: {
|
||||
listBranches: (input: GitListBranchesInput) => Promise<GitListBranchesResult>;
|
||||
createWorktree: (input: GitCreateWorktreeInput) => Promise<GitCreateWorktreeResult>;
|
||||
|
||||
@@ -95,6 +95,7 @@ it.effect("trims branded ids and command string fields at decode boundaries", ()
|
||||
assert.strictEqual(parsed.projectId, "project-1");
|
||||
assert.strictEqual(parsed.title, "Project Title");
|
||||
assert.strictEqual(parsed.workspaceRoot, "/tmp/workspace");
|
||||
assert.strictEqual(parsed.createWorkspaceRootIfMissing, undefined);
|
||||
assert.deepStrictEqual(parsed.defaultModelSelection, {
|
||||
provider: "codex",
|
||||
model: "gpt-5.2",
|
||||
@@ -102,6 +103,22 @@ it.effect("trims branded ids and command string fields at decode boundaries", ()
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("decodes project.create with createWorkspaceRootIfMissing enabled", () =>
|
||||
Effect.gen(function* () {
|
||||
const parsed = yield* decodeProjectCreateCommand({
|
||||
type: "project.create",
|
||||
commandId: "cmd-1",
|
||||
projectId: "project-1",
|
||||
title: "Project Title",
|
||||
workspaceRoot: "/tmp/workspace",
|
||||
createWorkspaceRootIfMissing: true,
|
||||
createdAt: "2026-01-01T00:00:00.000Z",
|
||||
});
|
||||
|
||||
assert.strictEqual(parsed.createWorkspaceRootIfMissing, true);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("decodes historical project.created payloads with a default provider", () =>
|
||||
Effect.gen(function* () {
|
||||
const parsed = yield* decodeProjectCreatedPayload({
|
||||
|
||||
@@ -402,6 +402,7 @@ export const ProjectCreateCommand = Schema.Struct({
|
||||
projectId: ProjectId,
|
||||
title: TrimmedNonEmptyString,
|
||||
workspaceRoot: TrimmedNonEmptyString,
|
||||
createWorkspaceRootIfMissing: Schema.optional(Schema.Boolean),
|
||||
defaultModelSelection: Schema.optional(Schema.NullOr(ModelSelection)),
|
||||
createdAt: IsoDateTime,
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as RpcGroup from "effect/unstable/rpc/RpcGroup";
|
||||
|
||||
import { OpenError, OpenInEditorInput } from "./editor";
|
||||
import { AuthAccessStreamEvent } from "./auth";
|
||||
import { FilesystemBrowseInput, FilesystemBrowseResult, FilesystemBrowseError } from "./filesystem";
|
||||
import {
|
||||
GitActionProgressEvent,
|
||||
GitCheckoutInput,
|
||||
@@ -83,6 +84,9 @@ export const WS_METHODS = {
|
||||
// Shell methods
|
||||
shellOpenInEditor: "shell.openInEditor",
|
||||
|
||||
// Filesystem methods
|
||||
filesystemBrowse: "filesystem.browse",
|
||||
|
||||
// Git methods
|
||||
gitPull: "git.pull",
|
||||
gitRefreshStatus: "git.refreshStatus",
|
||||
@@ -165,6 +169,12 @@ export const WsShellOpenInEditorRpc = Rpc.make(WS_METHODS.shellOpenInEditor, {
|
||||
error: OpenError,
|
||||
});
|
||||
|
||||
export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, {
|
||||
payload: FilesystemBrowseInput,
|
||||
success: FilesystemBrowseResult,
|
||||
error: FilesystemBrowseError,
|
||||
});
|
||||
|
||||
export const WsSubscribeGitStatusRpc = Rpc.make(WS_METHODS.subscribeGitStatus, {
|
||||
payload: GitStatusInput,
|
||||
success: GitStatusStreamEvent,
|
||||
@@ -350,6 +360,7 @@ export const WsRpcGroup = RpcGroup.make(
|
||||
WsProjectsSearchEntriesRpc,
|
||||
WsProjectsWriteFileRpc,
|
||||
WsShellOpenInEditorRpc,
|
||||
WsFilesystemBrowseRpc,
|
||||
WsSubscribeGitStatusRpc,
|
||||
WsGitPullRpc,
|
||||
WsGitRefreshStatusRpc,
|
||||
|
||||
@@ -84,6 +84,7 @@ export const ServerSettings = Schema.Struct({
|
||||
defaultThreadEnvMode: ThreadEnvMode.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed("local" as const satisfies ThreadEnvMode)),
|
||||
),
|
||||
addProjectBaseDirectory: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
|
||||
textGenerationModelSelection: ModelSelection.pipe(
|
||||
Schema.withDecodingDefault(
|
||||
Effect.succeed({
|
||||
@@ -168,6 +169,7 @@ const ClaudeSettingsPatch = Schema.Struct({
|
||||
export const ServerSettingsPatch = Schema.Struct({
|
||||
enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
|
||||
defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
|
||||
addProjectBaseDirectory: Schema.optionalKey(Schema.String),
|
||||
textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
|
||||
observability: Schema.optionalKey(
|
||||
Schema.Struct({
|
||||
|
||||
@@ -59,6 +59,10 @@
|
||||
"./qrCode": {
|
||||
"types": "./src/qrCode.ts",
|
||||
"import": "./src/qrCode.ts"
|
||||
},
|
||||
"./path": {
|
||||
"types": "./src/path.ts",
|
||||
"import": "./src/path.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
isExplicitRelativePath,
|
||||
isUncPath,
|
||||
isWindowsAbsolutePath,
|
||||
isWindowsDrivePath,
|
||||
} from "./path";
|
||||
|
||||
describe("path helpers", () => {
|
||||
it("detects windows drive paths", () => {
|
||||
expect(isWindowsDrivePath("C:\\repo")).toBe(true);
|
||||
expect(isWindowsDrivePath("D:/repo")).toBe(true);
|
||||
expect(isWindowsDrivePath("/repo")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects UNC paths", () => {
|
||||
expect(isUncPath("\\\\server\\share\\repo")).toBe(true);
|
||||
expect(isUncPath("C:\\repo")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects windows absolute paths", () => {
|
||||
expect(isWindowsAbsolutePath("C:\\repo")).toBe(true);
|
||||
expect(isWindowsAbsolutePath("\\\\server\\share\\repo")).toBe(true);
|
||||
expect(isWindowsAbsolutePath("./repo")).toBe(false);
|
||||
});
|
||||
|
||||
it("detects explicit relative paths", () => {
|
||||
expect(isExplicitRelativePath(".")).toBe(true);
|
||||
expect(isExplicitRelativePath("..")).toBe(true);
|
||||
expect(isExplicitRelativePath("./repo")).toBe(true);
|
||||
expect(isExplicitRelativePath("..\\repo")).toBe(true);
|
||||
expect(isExplicitRelativePath("~/repo")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
export function isWindowsDrivePath(value: string): boolean {
|
||||
return /^[a-zA-Z]:([/\\]|$)/.test(value);
|
||||
}
|
||||
|
||||
export function isUncPath(value: string): boolean {
|
||||
return value.startsWith("\\\\");
|
||||
}
|
||||
|
||||
export function isWindowsAbsolutePath(value: string): boolean {
|
||||
return isUncPath(value) || isWindowsDrivePath(value);
|
||||
}
|
||||
|
||||
export function isExplicitRelativePath(value: string): boolean {
|
||||
return (
|
||||
value === "." ||
|
||||
value === ".." ||
|
||||
value.startsWith("./") ||
|
||||
value.startsWith("../") ||
|
||||
value.startsWith(".\\") ||
|
||||
value.startsWith("..\\")
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user