-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): export puppeteer integration (#11)
* feat: puppeteer integration * feat: export puppeteer agent * fix: bug of insight visualizer
- Loading branch information
1 parent
bb27afe
commit beeebba
Showing
40 changed files
with
303 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,10 +119,8 @@ | |
} | ||
} | ||
|
||
.context { | ||
pre { | ||
text-wrap: balance; | ||
} | ||
pre { | ||
text-wrap: balance; | ||
} | ||
|
||
.item-list-space-up { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ExecutionDump, GroupedActionDump } from '@midscene/core'; | ||
import { groupedActionDumpFileExt, writeDumpFile } from '@midscene/core/utils'; | ||
import { PageTaskExecutor } from '../common/tasks'; | ||
import { WebPage } from '@/common/page'; | ||
|
||
export class PageAgent { | ||
page: WebPage; | ||
|
||
dumps: GroupedActionDump[]; | ||
|
||
constructor(page: WebPage) { | ||
this.page = page; | ||
this.dumps = []; | ||
} | ||
|
||
appendDump(groupName: string, execution: ExecutionDump) { | ||
let currentDump = this.dumps.find((dump) => dump.groupName === groupName); | ||
if (!currentDump) { | ||
currentDump = { | ||
groupName, | ||
executions: [], | ||
}; | ||
this.dumps.push(currentDump); | ||
} | ||
currentDump.executions.push(execution); | ||
} | ||
|
||
writeOutActionDumps() { | ||
writeDumpFile(`playwright-${process.pid}`, groupedActionDumpFileExt, JSON.stringify(this.dumps)); | ||
} | ||
|
||
async aiAction(taskPrompt: string, dumpCaseName = 'AI Action', dumpGroupName = 'MidScene / Web') { | ||
const actionAgent = new PageTaskExecutor(this.page, { taskName: dumpCaseName }); | ||
let error: Error | undefined; | ||
try { | ||
await actionAgent.action(taskPrompt); | ||
} catch (e: any) { | ||
error = e; | ||
} | ||
if (actionAgent.executionDump) { | ||
this.appendDump(dumpGroupName, actionAgent.executionDump); | ||
this.writeOutActionDumps(); | ||
} | ||
if (error) { | ||
// playwright cli won't print error cause, so we print it here | ||
console.error(error); | ||
throw new Error(error.message, { cause: error }); | ||
} | ||
} | ||
|
||
async aiQuery(demand: any, dumpCaseName = 'AI Query', dumpGroupName = 'MidScene / Web') { | ||
const actionAgent = new PageTaskExecutor(this.page, { taskName: dumpCaseName }); | ||
let error: Error | undefined; | ||
let result: any; | ||
try { | ||
result = await actionAgent.query(demand); | ||
} catch (e: any) { | ||
error = e; | ||
} | ||
if (actionAgent.executionDump) { | ||
this.appendDump(dumpGroupName, actionAgent.executionDump); | ||
this.writeOutActionDumps(); | ||
} | ||
if (error) { | ||
// playwright cli won't print error cause, so we print it here | ||
console.error(error); | ||
throw new Error(error.message, { cause: error }); | ||
} | ||
return result; | ||
} | ||
|
||
async ai(taskPrompt: string, type = 'action', dumpCaseName = 'AI', dumpGroupName = 'MidScene / Web') { | ||
if (type === 'action') { | ||
return this.aiAction(taskPrompt, dumpCaseName, dumpGroupName); | ||
} else if (type === 'query') { | ||
return this.aiQuery(taskPrompt, dumpCaseName, dumpGroupName); | ||
} | ||
throw new Error(`Unknown or Unsupported task type: ${type}, only support 'action' or 'query'`); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Page as PlaywrightPage } from 'playwright'; | ||
import type { Page as PuppeteerPage, KeyInput } from 'puppeteer'; | ||
|
||
export type WebPage = PlaywrightPage | PuppeteerPage; | ||
export type WebKeyInput = KeyInput; |
Oops, something went wrong.