103 lines
4.1 KiB
JavaScript
103 lines
4.1 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth } from "../../auth/index.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
import { handleToolError } from "../helpers/errorHandler.js";
|
|
import { pythonGet, pythonPost } from "../helpers/pythonServerClient.js";
|
|
import { getCurrentProjectInfo } from "../files/helpers.js";
|
|
|
|
// Tool: remove_global_library
|
|
// Removes a URL from a section (top or bottom) of the project's global
|
|
// libraries. Idempotent: if the URL isn't present, the list is left untouched
|
|
// and removed:false is returned.
|
|
|
|
export function registerRemoveGlobalLibraryTool(server) {
|
|
server.tool(
|
|
"remove_global_library",
|
|
`Remove a URL from the project's global libraries. Idempotent — if the URL isn't present, returns removed:false.`,
|
|
withAuthParams({
|
|
section: z.enum(["top", "bottom"]).describe("Target section: 'top' = <head>, 'bottom' = before </body>"),
|
|
url: z.string().min(1).describe("Absolute URL or project-relative path to remove"),
|
|
}),
|
|
{ readOnlyHint: false, destructiveHint: false },
|
|
withAuth(async ({ section, url }, _extra) => {
|
|
try {
|
|
const { projectSlug } = getCurrentProjectInfo();
|
|
|
|
// 1. Read current state from Python.
|
|
const current = await pythonGet("/api/project/libraries", {
|
|
project: projectSlug,
|
|
});
|
|
if (!current?.success) {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: false,
|
|
error: current?.error || "Could not read current libraries",
|
|
}),
|
|
}],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const sectionList = Array.isArray(current[section]) ? current[section] : [];
|
|
const trimmedUrl = String(url).trim();
|
|
|
|
// 2. Filter out entries matching the URL (trim-compare).
|
|
const nextList = sectionList.filter(
|
|
(entry) => String(entry?.url || "").trim() !== trimmedUrl
|
|
);
|
|
|
|
// 3. If nothing changed → not found, no-op.
|
|
if (nextList.length === sectionList.length) {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: true,
|
|
removed: false,
|
|
reason: "not found",
|
|
section,
|
|
entries: sectionList,
|
|
}, null, 2),
|
|
}],
|
|
};
|
|
}
|
|
|
|
// 4. Persist via save endpoint.
|
|
const saveResult = await pythonPost("/api/project/libraries/save", {
|
|
project: projectSlug,
|
|
section,
|
|
libraries: nextList,
|
|
});
|
|
if (!saveResult?.success) {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: false,
|
|
error: saveResult?.error || "Could not save libraries",
|
|
}),
|
|
}],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: true,
|
|
removed: true,
|
|
section,
|
|
entries: saveResult.libraries || [],
|
|
}, null, 2),
|
|
}],
|
|
};
|
|
} catch (error) {
|
|
return handleToolError(error, "remove_global_library", { section, url });
|
|
}
|
|
})
|
|
);
|
|
}
|