58 lines
2.3 KiB
JavaScript
58 lines
2.3 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth, getSessionCredentials } from "../../auth/index.js";
|
|
import { handleToolError } from "../helpers/errorHandler.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
import { LOCAL_SERVER_URL } from "../../config/index.js";
|
|
import axios from "axios";
|
|
|
|
export function registerNavigateBrowserTool(server) {
|
|
server.tool(
|
|
"navigate_browser",
|
|
`Navigate the user's browser preview to a specific page URL. Use this after creating or modifying a page to show the result to the user. The enlace should be a path like "/servicios/" or "/blog/my-post/".`,
|
|
withAuthParams({
|
|
enlace: z.string().describe("The URL path to navigate to, e.g. '/servicios/' or '/contacto/'"),
|
|
}),
|
|
{ readOnlyHint: true, destructiveHint: false },
|
|
withAuth(async ({ enlace }, extra) => {
|
|
try {
|
|
if (!enlace) {
|
|
return {
|
|
content: [{ type: "text", text: "Error: enlace is required" }],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
// Ensure enlace starts with /
|
|
if (!enlace.startsWith("/")) {
|
|
enlace = "/" + enlace;
|
|
}
|
|
|
|
const credentials = await getSessionCredentials(extra.sessionId);
|
|
const project = credentials.website || process.env.ACAI_WEBSITE || "";
|
|
|
|
// POST to Python server to set pending navigation
|
|
await axios.post(`${LOCAL_SERVER_URL}/api/browser/navigate`, {
|
|
project: project,
|
|
enlace: enlace,
|
|
}, {
|
|
headers: { "Content-Type": "application/json" },
|
|
timeout: 5000,
|
|
});
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: true,
|
|
message: `Browser navigated to ${enlace}`,
|
|
enlace: enlace,
|
|
})
|
|
}],
|
|
};
|
|
} catch (error) {
|
|
return handleToolError(error, "navigate_browser", { enlace });
|
|
}
|
|
})
|
|
);
|
|
}
|