donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
107 lines • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigateWithinStreetViewTool = exports.Action = void 0;
const Tool_1 = require("./Tool");
const ToolCallResult_1 = require("../models/ToolCallResult");
const GoToGoogleMapsStreetViewTool_1 = require("./GoToGoogleMapsStreetViewTool");
const ExtractGoogleStreetviewEntityDataTool_1 = require("./ExtractGoogleStreetviewEntityDataTool");
const PlaywrightUtils_1 = require("../utils/PlaywrightUtils");
const MiscUtils_1 = require("../utils/MiscUtils");
var Action;
(function (Action) {
Action["ROTATE_CLOCKWISE"] = "ROTATE_CLOCKWISE";
Action["ROTATE_COUNTERCLOCKWISE"] = "ROTATE_COUNTERCLOCKWISE";
Action["ZOOM_IN"] = "ZOOM_IN";
Action["ZOOM_OUT"] = "ZOOM_OUT";
})(Action || (exports.Action = Action = {}));
class NavigateWithinStreetViewTool extends Tool_1.Tool {
constructor() {
super(NavigateWithinStreetViewTool.NAME, 'Zoom in or out or rotate clockwise or counterclockwise on Google Street View.', 'NavigateWithinStreetViewToolCoreParameters', 'NavigateWithinStreetViewToolGptParameters');
}
async call(context, parameters) {
const page = context.page;
await NavigateWithinStreetViewTool.removeGoogleMapsSurvey(page);
let isSuccess;
if (parameters.action === Action.ROTATE_CLOCKWISE ||
parameters.action === Action.ROTATE_COUNTERCLOCKWISE) {
// First zoom out before rotating so that we do not miss content.
await NavigateWithinStreetViewTool.zoomStreetView(page, Action.ZOOM_OUT);
isSuccess = await NavigateWithinStreetViewTool.rotateStreetView(page, parameters.action);
}
else {
isSuccess = await NavigateWithinStreetViewTool.zoomStreetView(page, parameters.action);
}
const goToGoogleMapsStreetViewTool = new GoToGoogleMapsStreetViewTool_1.GoToGoogleMapsStreetViewTool();
// Queue a ExtractGoogleStreetviewEntityDataTool call if one is not already planned.
const isExtractToolAlreadyProposed = context.proposedToolCalls.some((call) => call.name === ExtractGoogleStreetviewEntityDataTool_1.ExtractGoogleStreetviewEntityDataTool.NAME);
if (!isExtractToolAlreadyProposed) {
// Extract the target business/entity name from the original GoToGoogleMapsStreetViewTool tool call.
const originalToolCall = context.invokedToolCalls.find((tc) => tc.toolName === goToGoogleMapsStreetViewTool.name);
if (originalToolCall) {
const entityName = originalToolCall.parameters.entityName;
const extractEntityDataProposedToolCall = {
name: ExtractGoogleStreetviewEntityDataTool_1.ExtractGoogleStreetviewEntityDataTool.NAME,
parameters: {
entityName,
},
};
context.proposedToolCalls.push(extractEntityDataProposedToolCall);
}
}
await PlaywrightUtils_1.PlaywrightUtils.waitForPageStability(page);
return isSuccess
? ToolCallResult_1.ToolCallResult.successful()
: {
isSuccessful: false,
forLlm: 'FAILED',
metadata: null,
};
}
callFromGpt(context, parameters) {
return this.call(context, parameters);
}
/**
* Removes the Google Maps survey iframe from the given Page.
* If no survey exists, this call has no effect.
*
* @param page - The Playwright Page object to remove the survey from
*/
static async removeGoogleMapsSurvey(page) {
return page.evaluate(() => {
document.querySelectorAll('#google-hats-survey').forEach((element) => {
element.remove();
});
});
}
static async rotateStreetView(googleMapsPage, direction) {
const rotateDirection = direction === Action.ROTATE_COUNTERCLOCKWISE
? 'Rotate the view counterclockwise'
: 'Rotate the view clockwise';
const rotateStreetViewButton = await googleMapsPage.$(`button[aria-label="${rotateDirection}"]`);
if (rotateStreetViewButton) {
await rotateStreetViewButton.click({
delay: MiscUtils_1.MiscUtils.generateHumanLikeClickDurationInMs(),
});
await googleMapsPage.waitForLoadState();
await googleMapsPage.waitForTimeout(931);
return true;
}
return false;
}
static async zoomStreetView(googleMapsPage, direction) {
const zoomDirection = direction === Action.ZOOM_OUT ? 'Zoom out' : 'Zoom in';
const zoomStreetViewButton = await googleMapsPage.$(`button[aria-label="${zoomDirection}"]`);
if (zoomStreetViewButton && !(await zoomStreetViewButton.isDisabled())) {
await zoomStreetViewButton.click({
delay: MiscUtils_1.MiscUtils.generateHumanLikeClickDurationInMs(),
});
await googleMapsPage.waitForLoadState();
await googleMapsPage.waitForTimeout(931);
return true;
}
return false;
}
}
exports.NavigateWithinStreetViewTool = NavigateWithinStreetViewTool;
NavigateWithinStreetViewTool.NAME = 'navigateWithinStreetView';
//# sourceMappingURL=NavigateWithinStreetView.js.map