@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
109 lines • 4.56 kB
JavaScript
import { createApplyPatchTool } from "./tool.js";
const APPLY_PATCH_FREEFORM_APIS = new Set([
"openai-responses",
"azure-openai-responses",
"openai-codex-responses",
]);
const APPLY_PATCH_JSON_APIS = new Set(["openai-completions"]);
const EDIT_TOOL_NAMES = new Set(["write", "edit"]);
const APPLY_PATCH_NAME = "apply_patch";
function isGptId(model) {
return model !== undefined && /(?:^|[/@:._-])gpt(?:[._-]|\d)/i.test(model.id);
}
export function getApplyPatchWireMode(model) {
if (!isGptId(model))
return "none";
if (APPLY_PATCH_FREEFORM_APIS.has(model.api))
return "freeform";
if (APPLY_PATCH_JSON_APIS.has(model.api))
return "json";
return "none";
}
export function isOpenAIGptModel(model) {
return getApplyPatchWireMode(model) === "freeform";
}
function hasEditTools(toolNames) {
return toolNames.some((toolName) => EDIT_TOOL_NAMES.has(toolName));
}
function withoutApplyPatch(toolNames) {
return toolNames.filter((toolName) => toolName !== APPLY_PATCH_NAME);
}
function replaceEditToolsWithApplyPatch(toolNames) {
const hadApplyPatch = toolNames.includes(APPLY_PATCH_NAME);
const insertIndex = toolNames.findIndex((toolName) => EDIT_TOOL_NAMES.has(toolName) || toolName === APPLY_PATCH_NAME);
const filteredToolNames = withoutApplyPatch(toolNames).filter((toolName) => !EDIT_TOOL_NAMES.has(toolName));
if (!hasEditTools(toolNames) && !hadApplyPatch)
return filteredToolNames;
const at = insertIndex === -1 ? filteredToolNames.length : Math.min(insertIndex, filteredToolNames.length);
return [...filteredToolNames.slice(0, at), APPLY_PATCH_NAME, ...filteredToolNames.slice(at)];
}
function hasApplyPatchFailures(details) {
if (typeof details !== "object" || details === null)
return false;
const result = Reflect.get(details, "result");
if (typeof result !== "object" || result === null)
return false;
const failures = Reflect.get(result, "failures");
return Array.isArray(failures) && failures.length > 0;
}
function syncToolset(pi, model, state, variants) {
const mode = getApplyPatchWireMode(model);
state.wireMode = mode;
const currentToolNames = pi.getActiveTools();
if (mode !== "none") {
if (state.activeVariant !== mode) {
pi.registerTool(variants[mode]);
state.activeVariant = mode;
}
const activeEdit = currentToolNames.filter((toolName) => EDIT_TOOL_NAMES.has(toolName));
if (activeEdit.length > 0)
state.removedEditToolNames = activeEdit;
pi.setActiveTools(replaceEditToolsWithApplyPatch(currentToolNames));
return;
}
if (state.removedEditToolNames.length === 0) {
if (currentToolNames.includes(APPLY_PATCH_NAME))
pi.setActiveTools(withoutApplyPatch(currentToolNames));
return;
}
const registeredNames = new Set(pi.getAllTools().map((tool) => tool.name));
const restorable = state.removedEditToolNames.filter((toolName) => registeredNames.has(toolName));
const restored = [...withoutApplyPatch(currentToolNames), ...restorable];
state.removedEditToolNames = [];
pi.setActiveTools([...new Set(restored)]);
}
function registerApplyPatchLazyActivator(pi, state) {
pi.registerLazyToolActivator?.((toolName) => {
if (toolName !== APPLY_PATCH_NAME || state.wireMode === "none")
return false;
const activeToolNames = pi.getActiveTools();
if (activeToolNames.includes(APPLY_PATCH_NAME))
return false;
pi.setActiveTools([...activeToolNames, APPLY_PATCH_NAME]);
return true;
});
}
export function registerApplyPatchExtension(pi) {
const state = { removedEditToolNames: [], wireMode: "none" };
const variants = {
freeform: createApplyPatchTool("freeform"),
json: createApplyPatchTool("json"),
};
state.activeVariant = "freeform";
pi.registerTool(variants.freeform);
registerApplyPatchLazyActivator(pi, state);
pi.on("tool_result", async (event) => {
if (event.toolName !== APPLY_PATCH_NAME || event.isError || !hasApplyPatchFailures(event.details)) {
return undefined;
}
return { isError: true };
});
pi.on("session_start", async (_event, ctx) => {
syncToolset(pi, ctx.model, state, variants);
});
pi.on("model_select", async (event) => {
syncToolset(pi, event.model, state, variants);
});
}
export default registerApplyPatchExtension;
//# sourceMappingURL=extension.js.map