Skip to content

Commit

Permalink
sidepanel: Add support for recorded actions that need the exact text …
Browse files Browse the repository at this point in the history
…on element

- Fix typos
- Add support for recorded actions that need the exact text on element
  • Loading branch information
hillary-mutisya committed Mar 8, 2025
1 parent e06948a commit 94fc72e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export async function handleSchemaDiscoveryAction(
...authoredActions.values(),
];

if (typeDefinitions.length === 0) {
console.log("No actions for this schema.");
return;
}

const union = sc.union(
typeDefinitions.map((definition) => sc.ref(definition)),
);
Expand Down Expand Up @@ -383,12 +388,12 @@ export async function handleSchemaDiscoveryAction(
});

const obj: ActionSchemaObject = sc.obj({
actionName: sc.string(userIntentJson.actiontName),
actionName: sc.string(userIntentJson.actionName),
parameters: sc.obj(Object.fromEntries(fields)),
} as const);

const schema = sc.type(
userIntentJson.actiontName,
userIntentJson.actionName,
obj,
actionDescription,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type UserIntentParameter = {

export type UserIntent = {
// a concise name for the action, in camelCase
actiontName: string;
actionName: string;
// a consise list of the parameters that should be captured from the user in order to implenent this action
parameters: UserIntentParameter[];
};
Expand All @@ -49,7 +49,7 @@ export type WebPlan = {
export type SelectElementByText = {
actionName: "selectElementByText";
parameters: {
// the shortName of the UserIntentParameter to use for this value
// IMPORTANT: the shortName of the UserIntentParameter to use for this value
text: string;
elementType?: string;
};
Expand All @@ -58,7 +58,7 @@ export type SelectElementByText = {
export type EnterText = {
actionName: "enterText";
parameters: {
// the shortName of the UserIntentParameter to use for this value
// IMPORTANT: the shortName of the UserIntentParameter to use for this value
textParameter: string;
};
};
Expand All @@ -68,24 +68,32 @@ export type EnterText = {
export type EnterTextAtPageScope = {
actionName: "EnterTextAtPageScope";
parameters: {
// the shortName of the UserIntentParameter to use for this value
// IMPORTANT: the shortName of the UserIntentParameter to use for this value
textParameter: string;
};
};

export type SelectValueFromDropdown = {
actionName: "selectValueFromDropdown";
parameters: {
// the shortName of the UserIntentParameter to use for this value
// IMPORTANT: the shortName of the UserIntentParameter to use for this value
valueTextParameter: string;
};
};

export type ClickOnButton = {
actionName: "clickOnButton";
parameters: {
// the displayed text of the button to click on
buttonText: string;
};
};

export type ClickOnElement = {
actionName: "clickOnElement";
parameters: {
// the shortName of the UserIntentParameter to use for this value
elementTextParameter: string;
// the displayed text of the element to click on
elementText: string;
};
};

Expand All @@ -101,6 +109,7 @@ export type PageManipulationActions =
| SelectElementByText
| EnterText
| SelectValueFromDropdown
| ClickOnButton
| ClickOnElement
| ClickOnLink;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export function createTempAgentForSchema(
selectionCondition?: string,
) {
const htmlFragments = await browser.getHtmlFragments();
const timerName = `getting ${componentType} section`;

console.time(timerName);
const response = await agent.getPageComponentSchema(
componentType,
selectionCondition,
Expand All @@ -79,7 +76,6 @@ export function createTempAgentForSchema(
return;
}

console.timeEnd(timerName);
return response.data;
}

Expand Down Expand Up @@ -141,7 +137,7 @@ export function createTempAgentForSchema(
!actionsJson.has(action.actionName)
) {
console.log(
`Action ${actionsJson} was not found on the list of user-defined actions`,
`Action ${action.actionName} was not found on the list of user-defined actions`,
);
return;
}
Expand Down Expand Up @@ -169,21 +165,27 @@ export function createTempAgentForSchema(
await followLink(link?.linkCssSelector);
break;
case "clickOnElement":
const elementParameter = targetIntent.parameters.find(
(param) =>
param.shortName ==
step.parameters.elementTextParameter,
);
const element = (await getComponentFromPage(
"Element",
`element text ${elementParameter?.name}`,
`element text ${step.parameters?.elementText}`,
)) as Element;
if (element !== undefined) {
await browser.clickOn(element.cssSelector);
await browser.awaitPageInteraction();
await browser.awaitPageLoad();
}
break;
case "clickOnButton":
const button = (await getComponentFromPage(
"Element",
`element text ${step.parameters?.buttonText}`,
)) as Element;
if (button !== undefined) {
await browser.clickOn(button.cssSelector);
await browser.awaitPageInteraction();
await browser.awaitPageLoad();
}
break;
case "enterText":
const textParameter = targetIntent.parameters.find(
(param) =>
Expand Down
11 changes: 6 additions & 5 deletions ts/packages/agents/browser/src/extension/sidepanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async function saveUserAction() {
}, 5000);
}

button.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...`;
button.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Saving...`;
button.disabled = true;

// Get schema based on the recorded action info
Expand All @@ -213,8 +213,9 @@ async function saveUserAction() {
console.error("Error fetching schema:", chrome.runtime.lastError);
showTemporaryStatus("✖ Failed", "btn-outline-danger");
} else {
const processedActionName = response.intentJson.actionName;
await addEntryToStoredPageProperties(actionName!, "userActions", {
name: actionName,
name: processedActionName,
description: actionDescription,
steps,
screenshot,
Expand All @@ -224,17 +225,17 @@ async function saveUserAction() {
});

await addEntryToStoredPageProperties(
actionName!,
processedActionName,
"authoredActionDefinitions",
response.intentTypeDefinition,
);
await addEntryToStoredPageProperties(
actionName!,
processedActionName,
"authoredActionsJson",
response.actions,
);
await addEntryToStoredPageProperties(
actionName!,
processedActionName,
"authoredIntentJson",
response.intentJson,
);
Expand Down

0 comments on commit 94fc72e

Please sign in to comment.