@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
158 lines • 6.63 kB
JavaScript
export const SDK_TO_PI_TOOL_NAME = {
read: "read",
write: "write",
edit: "edit",
bash: "bash",
grep: "grep",
glob: "find",
};
/**
* Senpi tools served to Claude Code as its built-ins. `write` and `edit` are deliberately absent:
* Claude Code runs its own read-before-write validator on built-in Write/Edit BEFORE any
* PreToolUse hook, and its read state is always empty here (senpi denies the SDK's Read), so the
* SDK answered those calls itself while senpi still executed them (#2401). They go through the
* custom-tools MCP server instead, where the host denial always answers and senpi alone executes.
*/
export const PI_TO_SDK_TOOL_NAME = {
read: "Read",
bash: "Bash",
grep: "Grep",
find: "Glob",
glob: "Glob",
};
/**
* Versioned host-tool denial policy. `configFingerprint` in session-sync.ts hashes this as
* `hostToolPolicy` into `toolsetHash`; a mismatch is `toolset_changed` and retires the live
* resident query (reattach with the current hooks). The same hash is persisted in the
* restart sidecar, so the first admission after an upgrade also reattaches with
* `toolset_changed` - the resumed query carries the current hooks, so the pre-bump
* denial text is never served again. Bump when denial copy or hooks change so
* wording-only edits cannot keep serving the old reason.
*/
export const HOST_TOOL_POLICY_FINGERPRINT = "host-tool-denial-v4";
export const BUILTIN_SDK_TOOLS = ["Read", "Bash", "Grep", "Glob"];
/**
* The reason the host deny hook gives. Claude Code itself labels every PreToolUse deny as
* "PreToolUse:<Tool> hook error: <reason>" with is_error true, and that label cannot be changed, so the
* reason has to say first that it is the expected handoff and must not be reported (senpi#3417).
*/
export const TOOL_EXECUTION_DENIED_MESSAGE = "Not an error: this is the normal host handoff, so ignore the 'hook error' label. " +
"Senpi runs this tool on the host and returns its result as the next user message. " +
"Wait for that result, and do not mention this message to the user.";
export const HOST_TOOL_EXECUTION_DENIED_MESSAGE = TOOL_EXECUTION_DENIED_MESSAGE;
export const CUSTOM_TOOLS_MCP_SERVER_NAME = "custom-tools";
export const CUSTOM_TOOLS_MCP_PREFIX = `mcp__${CUSTOM_TOOLS_MCP_SERVER_NAME}__`;
export const HOST_CAPTURED_SDK_TOOL_MATCHER = "Bash|Write|Edit|Read|Grep|Glob|mcp__custom-tools__.*";
export const HOST_TOOL_DENIAL_HOOKS = {
PreToolUse: [
{
matcher: HOST_CAPTURED_SDK_TOOL_MATCHER,
hooks: [
async () => ({
continue: false,
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: HOST_TOOL_EXECUTION_DENIED_MESSAGE,
},
}),
],
},
],
};
function pascalCase(value) {
return value
.split(/[^a-zA-Z0-9]+/)
.filter(Boolean)
.map((part) => `${part[0]?.toUpperCase() ?? ""}${part.slice(1)}`)
.join("");
}
export function mapPiToolNameToSdk(name, customToolNameToSdk) {
const normalized = name.toLowerCase();
return (customToolNameToSdk?.get(name) ??
customToolNameToSdk?.get(normalized) ??
PI_TO_SDK_TOOL_NAME[normalized] ??
pascalCase(name));
}
export function mapSdkToolNameToPi(name, customToolNameToPi) {
const normalized = name.toLowerCase();
return (SDK_TO_PI_TOOL_NAME[normalized] ??
customToolNameToPi?.get(name) ??
customToolNameToPi?.get(normalized) ??
(normalized.startsWith(CUSTOM_TOOLS_MCP_PREFIX) ? name.slice(CUSTOM_TOOLS_MCP_PREFIX.length) : name));
}
/** Translate Claude Code's built-in-tool inputs into senpi's active tool schemas. */
export function mapToolArgs(toolName, args) {
const input = args ?? {};
switch (toolName.toLowerCase()) {
case "read":
return { path: input.file_path ?? input.path, offset: input.offset, limit: input.limit };
case "write":
return { path: input.file_path ?? input.path, content: input.content };
case "edit":
return {
path: input.file_path ?? input.path,
edits: Array.isArray(input.edits)
? input.edits
: [
{
oldText: input.old_string ?? input.oldText ?? input.old_text,
newText: input.new_string ?? input.newText ?? input.new_text,
},
],
};
case "bash":
return {
command: input.command,
timeout: typeof input.timeout === "number" ? input.timeout / 1_000 : input.timeout,
};
case "grep":
return {
pattern: input.pattern,
path: input.path,
glob: input.glob,
ignoreCase: input["-i"] ?? input.ignoreCase,
context: input.context ?? input["-C"],
limit: input.head_limit ?? input.limit,
};
case "glob":
case "find":
return { pattern: input.pattern, path: input.path, limit: input.limit };
default:
return { ...input };
}
}
export function resolveSdkTools(context) {
if (!context.tools) {
return {
sdkTools: [...BUILTIN_SDK_TOOLS],
customTools: [],
customToolNameToSdk: new Map(),
customToolNameToPi: new Map(),
};
}
const sdkTools = new Set();
const customTools = [];
const customToolNameToSdk = new Map();
const customToolNameToPi = new Map();
for (const tool of context.tools) {
const normalized = tool.name.toLowerCase();
const sdkName = PI_TO_SDK_TOOL_NAME[normalized];
if (sdkName) {
sdkTools.add(sdkName);
continue;
}
const customSdkName = `${CUSTOM_TOOLS_MCP_PREFIX}${tool.name}`;
customTools.push(tool);
customToolNameToSdk.set(tool.name, customSdkName);
customToolNameToSdk.set(normalized, customSdkName);
customToolNameToPi.set(customSdkName, tool.name);
customToolNameToPi.set(customSdkName.toLowerCase(), tool.name);
}
return { sdkTools: [...sdkTools], customTools, customToolNameToSdk, customToolNameToPi };
}
export const canUseTool = async () => ({
behavior: "deny",
message: TOOL_EXECUTION_DENIED_MESSAGE,
});
//# sourceMappingURL=tools.js.map