Rename .js to .mjs, extract helpers module

Move resolvePath and exec out of index.mjs into server/helpers.mjs so
actions can import them directly rather than receiving them as arguments.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 20:06:29 +00:00
parent 5a557a642a
commit f02e2a746d
7 changed files with 43 additions and 42 deletions

View File

@@ -1,7 +1,7 @@
// Conduit client module — import this when using conduit programmatically. // Conduit client module — import this when using conduit programmatically.
// Designed for use by Claude or other tools that want to call conduit actions. // Designed for use by Claude or other tools that want to call conduit actions.
const BASE_URL = process.env.CONDUIT_URL || "http://localhost:3333"; const BASE_URL = process.env.CONDUIT_URL || "http://localhost:3015";
export async function callAction(action, params = {}) { export async function callAction(action, params = {}) {
const res = await fetch(`${BASE_URL}/action`, { const res = await fetch(`${BASE_URL}/action`, {

View File

@@ -1,11 +1,11 @@
#!/usr/bin/env node #!/usr/bin/env node
// Conduit client — thin CLI wrapper for Claude to call the conduit server. // Conduit client — thin CLI wrapper for Claude to call the conduit server.
// Usage: // Usage:
// node client/index.js <action> [key=value ...] // node client/index.mjs <action> [key=value ...]
// node client/index.js list-actions // node client/index.mjs list-actions
// node client/index.js edit-file filename=/workspace/foo.js // node client/index.mjs edit-file filename=/workspace/foo.js
const BASE_URL = process.env.CONDUIT_URL || "http://localhost:3333"; const BASE_URL = process.env.CONDUIT_URL || "http://localhost:3015";
async function callAction(action, params = {}) { async function callAction(action, params = {}) {
const res = await fetch(`${BASE_URL}/action`, { const res = await fetch(`${BASE_URL}/action`, {

View File

@@ -4,8 +4,8 @@
"description": "A supervised action bridge between Claude Code and the host system", "description": "A supervised action bridge between Claude Code and the host system",
"type": "module", "type": "module",
"scripts": { "scripts": {
"server": "node server/index.js", "server": "node server/index.mjs",
"client": "node client/index.js" "client": "node client/index.mjs"
}, },
"dependencies": { "dependencies": {
"express": "^5.2.1" "express": "^5.2.1"

View File

@@ -1,6 +1,8 @@
// Action registry — defines all available actions, their parameters, and policies. // Action registry — defines all available actions, their parameters, and policies.
// policy: "auto-accept" | "auto-deny" | "queue" // policy: "auto-accept" | "auto-deny" | "queue"
import { resolvePath, exec } from "./helpers.mjs";
export const actions = { export const actions = {
"list-actions": { "list-actions": {
description: "List all available actions and their definitions", description: "List all available actions and their definitions",
@@ -20,7 +22,7 @@ export const actions = {
description: "Open a file in the editor", description: "Open a file in the editor",
params: [{ name: "filename", required: true, type: "path" }], params: [{ name: "filename", required: true, type: "path" }],
policy: "auto-accept", policy: "auto-accept",
handler: async ({ filename }, { resolvePath, exec }) => { handler: async ({ filename }) => {
const resolved = resolvePath(filename); const resolved = resolvePath(filename);
await exec("xdg-open", [resolved]); await exec("xdg-open", [resolved]);
return { opened: resolved }; return { opened: resolved };
@@ -31,7 +33,7 @@ export const actions = {
description: "Open a directory in the file manager", description: "Open a directory in the file manager",
params: [{ name: "path", required: true, type: "path" }], params: [{ name: "path", required: true, type: "path" }],
policy: "auto-accept", policy: "auto-accept",
handler: async ({ path }, { resolvePath, exec }) => { handler: async ({ path }) => {
const resolved = resolvePath(path); const resolved = resolvePath(path);
await exec("xdg-open", [resolved]); await exec("xdg-open", [resolved]);
return { opened: resolved }; return { opened: resolved };
@@ -42,7 +44,7 @@ export const actions = {
description: "Open a URL in the web browser", description: "Open a URL in the web browser",
params: [{ name: "url", required: true, type: "string" }], params: [{ name: "url", required: true, type: "string" }],
policy: "queue", policy: "queue",
handler: async ({ url }, { exec }) => { handler: async ({ url }) => {
await exec("xdg-open", [url]); await exec("xdg-open", [url]);
return { opened: url }; return { opened: url };
}, },
@@ -52,7 +54,7 @@ export const actions = {
description: "Open a terminal in a given directory", description: "Open a terminal in a given directory",
params: [{ name: "path", required: false, type: "path" }], params: [{ name: "path", required: false, type: "path" }],
policy: "queue", policy: "queue",
handler: async ({ path }, { resolvePath, exec }) => { handler: async ({ path }) => {
const resolved = path ? resolvePath(path) : process.env.HOME; const resolved = path ? resolvePath(path) : process.env.HOME;
await exec("xdg-open", [resolved]); await exec("xdg-open", [resolved]);
return { opened: resolved }; return { opened: resolved };

25
server/helpers.mjs Normal file
View File

@@ -0,0 +1,25 @@
import { spawnSync } from "child_process";
import path from "path";
const WORKSPACE_ROOT = process.env.CONDUIT_ROOT || "/workspace";
// Resolve a path param relative to WORKSPACE_ROOT, preventing traversal.
export function resolvePath(userPath) {
const resolved = path.resolve(WORKSPACE_ROOT, userPath.replace(/^\//, ""));
if (!resolved.startsWith(WORKSPACE_ROOT)) {
throw new Error(`Path escapes workspace root: ${userPath}`);
}
return resolved;
}
// Execute a binary with an argument list — no shell interpolation.
export function exec(bin, args = []) {
return new Promise((resolve, reject) => {
const result = spawnSync(bin, args, { stdio: "inherit" });
if (result.error) {
reject(result.error);
} else {
resolve(result.status);
}
});
}

View File

@@ -1,38 +1,12 @@
import express from "express"; import express from "express";
import { spawnSync } from "child_process"; import { actions } from "./actions.mjs";
import path from "path"; import { enqueue, getEntry, listPending, resolve } from "./queue.mjs";
import { actions } from "./actions.js";
import { enqueue, getEntry, listPending, resolve } from "./queue.js";
const PORT = process.env.CONDUIT_PORT || 3015; const PORT = process.env.CONDUIT_PORT || 3015;
const WORKSPACE_ROOT = process.env.CONDUIT_ROOT || "/workspace";
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
// Resolve a path param relative to WORKSPACE_ROOT, preventing traversal.
function resolvePath(userPath) {
const resolved = path.resolve(WORKSPACE_ROOT, userPath.replace(/^\//, ""));
if (!resolved.startsWith(WORKSPACE_ROOT)) {
throw new Error(`Path escapes workspace root: ${userPath}`);
}
return resolved;
}
// Execute a binary with an argument list — no shell interpolation.
function exec(bin, args = []) {
return new Promise((resolve, reject) => {
const result = spawnSync(bin, args, { stdio: "inherit" });
if (result.error) {
reject(result.error);
} else {
resolve(result.status);
}
});
}
const helpers = { resolvePath, exec };
// Validate that all required params are present. // Validate that all required params are present.
function validateParams(actionDef, params) { function validateParams(actionDef, params) {
const errors = []; const errors = [];
@@ -68,7 +42,7 @@ app.post("/action", async (req, res) => {
if (def.policy === "auto-accept") { if (def.policy === "auto-accept") {
try { try {
const result = await def.handler(params, helpers); const result = await def.handler(params);
return res.json({ status: "accepted", result }); return res.json({ status: "accepted", result });
} catch (err) { } catch (err) {
return res.status(500).json({ status: "error", error: err.message }); return res.status(500).json({ status: "error", error: err.message });
@@ -100,7 +74,7 @@ app.post("/queue/:id/approve", async (req, res) => {
const def = actions[entry.action]; const def = actions[entry.action];
try { try {
const result = await def.handler(entry.params, helpers); const result = await def.handler(entry.params);
res.json({ status: "approved", result }); res.json({ status: "approved", result });
} catch (err) { } catch (err) {
res.status(500).json({ status: "error", error: err.message }); res.status(500).json({ status: "error", error: err.message });
@@ -123,5 +97,5 @@ app.post("/queue/:id/deny", (req, res) => {
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`claude-code-conduit server running on port ${PORT}`); console.log(`claude-code-conduit server running on port ${PORT}`);
console.log(`Workspace root: ${WORKSPACE_ROOT}`); console.log(`Workspace root: ${process.env.CONDUIT_ROOT || "/workspace"}`);
}); });