Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/server/src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const testLayer = Layer.mergeAll(
Layer.succeed(Open, {
openBrowser: (_target: string) => Effect.void,
openInEditor: () => Effect.void,
openInFileManager: () => Effect.void,
revealInFileManager: () => Effect.void,
} satisfies OpenShape),
AnalyticsService.layerTest,
FetchHttpClient.layer,
Expand Down
41 changes: 41 additions & 0 deletions apps/server/src/open.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
launchDetached,
resolveAvailableEditors,
resolveEditorLaunch,
resolveOpenInFileManagerLaunch,
resolveRevealInFileManagerLaunch,
} from "./open";

it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
Expand Down Expand Up @@ -121,6 +123,45 @@ it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
});
}),
);

it("maps direct file-manager launches for directories", () => {
assert.deepEqual(resolveOpenInFileManagerLaunch({ path: "/tmp/workspace" }, "darwin"), {
command: "open",
args: ["/tmp/workspace"],
});
assert.deepEqual(resolveOpenInFileManagerLaunch({ path: "C:\\workspace" }, "win32"), {
command: "explorer",
args: ["C:\\workspace"],
});
assert.deepEqual(resolveOpenInFileManagerLaunch({ path: "/tmp/workspace" }, "linux"), {
command: "xdg-open",
args: ["/tmp/workspace"],
});
});

it("maps reveal launches to platform-specific file-manager commands", () => {
assert.deepEqual(
resolveRevealInFileManagerLaunch({ path: "/tmp/workspace/file.ts" }, "darwin"),
{
command: "open",
args: ["-R", "/tmp/workspace/file.ts"],
},
);
assert.deepEqual(
resolveRevealInFileManagerLaunch({ path: "C:\\workspace\\file.ts" }, "win32"),
{
command: "explorer",
args: ["/select,", "C:\\workspace\\file.ts"],
},
);
assert.deepEqual(
resolveRevealInFileManagerLaunch({ path: "/tmp/workspace/file.ts" }, "linux"),
{
command: "xdg-open",
args: ["/tmp/workspace"],
},
);
});
});

it.layer(NodeServices.layer)("launchDetached", (it) => {
Expand Down
40 changes: 39 additions & 1 deletion apps/server/src/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import { spawn } from "node:child_process";
import { accessSync, constants, statSync } from "node:fs";
import { extname, join } from "node:path";
import { dirname, extname, join } from "node:path";

import { EDITORS, type EditorId } from "@okcode/contracts";
import { ServiceMap, Schema, Effect, Layer } from "effect";
Expand All @@ -27,6 +27,10 @@ export interface OpenInEditorInput {
readonly editor: EditorId;
}

export interface OpenPathInput {
readonly path: string;
}

interface EditorLaunch {
readonly command: string;
readonly args: ReadonlyArray<string>;
Expand Down Expand Up @@ -192,6 +196,16 @@ export interface OpenShape {
* Launches the editor as a detached process so server startup is not blocked.
*/
readonly openInEditor: (input: OpenInEditorInput) => Effect.Effect<void, OpenError>;

/**
* Open a path in the OS file manager.
*/
readonly openInFileManager: (input: OpenPathInput) => Effect.Effect<void, OpenError>;

/**
* Reveal a path in the OS file manager.
*/
readonly revealInFileManager: (input: OpenPathInput) => Effect.Effect<void, OpenError>;
}

/**
Expand Down Expand Up @@ -225,6 +239,28 @@ export const resolveEditorLaunch = Effect.fnUntraced(function* (
return { command: fileManagerCommandForPlatform(platform), args: [input.cwd] };
});

export const resolveOpenInFileManagerLaunch = (
input: OpenPathInput,
platform: NodeJS.Platform = process.platform,
): EditorLaunch => ({
command: fileManagerCommandForPlatform(platform),
args: [input.path],
});

export const resolveRevealInFileManagerLaunch = (
input: OpenPathInput,
platform: NodeJS.Platform = process.platform,
): EditorLaunch => {
switch (platform) {
case "darwin":
return { command: "open", args: ["-R", input.path] };
case "win32":
return { command: "explorer", args: ["/select,", input.path] };
default:
return { command: "xdg-open", args: [dirname(input.path)] };
}
};

export const launchDetached = (launch: EditorLaunch) =>
Effect.gen(function* () {
if (!isCommandAvailable(launch.command)) {
Expand Down Expand Up @@ -270,6 +306,8 @@ const make = Effect.gen(function* () {
catch: (cause) => new OpenError({ message: "Browser auto-open failed", cause }),
}),
openInEditor: (input) => Effect.flatMap(resolveEditorLaunch(input), launchDetached),
openInFileManager: (input) => launchDetached(resolveOpenInFileManagerLaunch(input)),
revealInFileManager: (input) => launchDetached(resolveRevealInFileManagerLaunch(input)),
} satisfies OpenShape;
});

Expand Down
58 changes: 58 additions & 0 deletions apps/server/src/wsServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const asTurnId = (value: string): TurnId => TurnId.makeUnsafe(value);
const defaultOpenService: OpenShape = {
openBrowser: () => Effect.void,
openInEditor: () => Effect.void,
openInFileManager: () => Effect.void,
revealInFileManager: () => Effect.void,
};

const defaultProviderStatuses: ReadonlyArray<ServerProviderStatus> = [
Expand Down Expand Up @@ -1024,6 +1026,8 @@ describe("WebSocket Server", () => {
openCalls.push({ cwd: input.cwd, editor: input.editor });
return Effect.void;
},
openInFileManager: () => Effect.void,
revealInFileManager: () => Effect.void,
};

server = await createTestServer({ cwd: "/my/workspace", open: openService });
Expand All @@ -1041,6 +1045,58 @@ describe("WebSocket Server", () => {
expect(openCalls).toEqual([{ cwd: "/my/workspace", editor: "cursor" }]);
});

it("routes shell.openInFileManager through the injected open service", async () => {
const openCalls: string[] = [];
const openService: OpenShape = {
openBrowser: () => Effect.void,
openInEditor: () => Effect.void,
openInFileManager: (input) => {
openCalls.push(input.path);
return Effect.void;
},
revealInFileManager: () => Effect.void,
};

server = await createTestServer({ cwd: "/my/workspace", open: openService });
const addr = server.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;

const [ws] = await connectAndAwaitWelcome(port);
connections.push(ws);

const response = await sendRequest(ws, WS_METHODS.shellOpenInFileManager, {
path: "/my/workspace/src",
});
expect(response.error).toBeUndefined();
expect(openCalls).toEqual(["/my/workspace/src"]);
});

it("routes shell.revealInFileManager through the injected open service", async () => {
const revealCalls: string[] = [];
const openService: OpenShape = {
openBrowser: () => Effect.void,
openInEditor: () => Effect.void,
openInFileManager: () => Effect.void,
revealInFileManager: (input) => {
revealCalls.push(input.path);
return Effect.void;
},
};

server = await createTestServer({ cwd: "/my/workspace", open: openService });
const addr = server.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;

const [ws] = await connectAndAwaitWelcome(port);
connections.push(ws);

const response = await sendRequest(ws, WS_METHODS.shellRevealInFileManager, {
path: "/my/workspace/src/index.ts",
});
expect(response.error).toBeUndefined();
expect(revealCalls).toEqual(["/my/workspace/src/index.ts"]);
});

it("reads keybindings from the configured state directory", async () => {
const baseDir = makeTempDir("okcode-state-keybindings-");
const { keybindingsConfigPath: keybindingsPath } = deriveServerPathsSync(baseDir, undefined);
Expand Down Expand Up @@ -1503,6 +1559,8 @@ describe("WebSocket Server", () => {
openBrowser: () => Effect.void,
openInEditor: () =>
Effect.sync(() => BigInt(1)).pipe(Effect.map((result) => result as unknown as void)),
openInFileManager: () => Effect.void,
revealInFileManager: () => Effect.void,
};

try {
Expand Down
12 changes: 11 additions & 1 deletion apps/server/src/wsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
const checkpointDiffQuery = yield* CheckpointDiffQuery;
const orchestrationReactor = yield* OrchestrationReactor;
const prReview = yield* PrReview;
const { openInEditor } = yield* Open;
const { openInEditor, openInFileManager, revealInFileManager } = yield* Open;
const environmentVariables = yield* EnvironmentVariables;
const skillService = yield* SkillService;

Expand Down Expand Up @@ -1010,6 +1010,16 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
return yield* openInEditor(body);
}

case WS_METHODS.shellOpenInFileManager: {
const body = stripRequestTag(request.body);
return yield* openInFileManager(body);
}

case WS_METHODS.shellRevealInFileManager: {
const body = stripRequestTag(request.body);
return yield* revealInFileManager(body);
}

case WS_METHODS.gitStatus: {
const body = stripRequestTag(request.body);
return yield* gitManager.status(body);
Expand Down
Loading
Loading