misoai-web 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +352 -352
- package/bin/midscene-playground +2 -2
- package/dist/es/agent.js +247 -63
- package/dist/es/agent.js.map +1 -1
- package/dist/es/bridge-mode-browser.js.map +1 -1
- package/dist/es/bridge-mode.js +247 -63
- package/dist/es/bridge-mode.js.map +1 -1
- package/dist/es/chrome-extension.js +247 -63
- package/dist/es/chrome-extension.js.map +1 -1
- package/dist/es/index.js +247 -63
- package/dist/es/index.js.map +1 -1
- package/dist/es/midscene-playground.js +247 -63
- package/dist/es/midscene-playground.js.map +1 -1
- package/dist/es/midscene-server.js.map +1 -1
- package/dist/es/playground.js +247 -63
- package/dist/es/playground.js.map +1 -1
- package/dist/es/playwright-report.js.map +1 -1
- package/dist/es/playwright.js +247 -63
- package/dist/es/playwright.js.map +1 -1
- package/dist/es/puppeteer-agent-launcher.js +247 -63
- package/dist/es/puppeteer-agent-launcher.js.map +1 -1
- package/dist/es/puppeteer.js +247 -63
- package/dist/es/puppeteer.js.map +1 -1
- package/dist/es/ui-utils.js.map +1 -1
- package/dist/es/utils.js.map +1 -1
- package/dist/es/yaml.js.map +1 -1
- package/dist/lib/agent.js +247 -63
- package/dist/lib/agent.js.map +1 -1
- package/dist/lib/bridge-mode-browser.js.map +1 -1
- package/dist/lib/bridge-mode.js +247 -63
- package/dist/lib/bridge-mode.js.map +1 -1
- package/dist/lib/chrome-extension.js +247 -63
- package/dist/lib/chrome-extension.js.map +1 -1
- package/dist/lib/index.js +247 -63
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/midscene-playground.js +247 -63
- package/dist/lib/midscene-playground.js.map +1 -1
- package/dist/lib/midscene-server.js.map +1 -1
- package/dist/lib/playground.js +247 -63
- package/dist/lib/playground.js.map +1 -1
- package/dist/lib/playwright-report.js.map +1 -1
- package/dist/lib/playwright.js +247 -63
- package/dist/lib/playwright.js.map +1 -1
- package/dist/lib/puppeteer-agent-launcher.js +247 -63
- package/dist/lib/puppeteer-agent-launcher.js.map +1 -1
- package/dist/lib/puppeteer.js +247 -63
- package/dist/lib/puppeteer.js.map +1 -1
- package/dist/lib/ui-utils.js.map +1 -1
- package/dist/lib/utils.js.map +1 -1
- package/dist/lib/yaml.js.map +1 -1
- package/dist/types/agent.d.ts +19 -4
- package/dist/types/bridge-mode-browser.d.ts +2 -2
- package/dist/types/bridge-mode.d.ts +2 -2
- package/dist/types/{browser-a1877d18.d.ts → browser-aec1055d.d.ts} +1 -1
- package/dist/types/chrome-extension.d.ts +2 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/midscene-server.d.ts +1 -1
- package/dist/types/{page-663ece08.d.ts → page-86ab0fe1.d.ts} +34 -34
- package/dist/types/playground.d.ts +2 -2
- package/dist/types/playwright.d.ts +1 -1
- package/dist/types/puppeteer-agent-launcher.d.ts +1 -1
- package/dist/types/puppeteer.d.ts +1 -1
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/yaml.d.ts +1 -1
- package/package.json +3 -3
package/dist/es/ui-utils.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"mappings":";AAWO,SAAS,QAAQ,MAAqB;AAC3C,SAAO,KAAK,WAAW,KAAK,YAAY,SACpC,GAAG,KAAK,IAAI,MAAM,KAAK,WAAW,EAAE,KACpC,KAAK;AACX;AAEO,SAAS,eACd,OAC0C;AAE1C,QAAM,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK;AAIlD,SAAO,KAAK,OAAO,CAAC,KAA+C,MAAM;AACvE,UAAM,cAAc,KAAK,SAAS,MAAM,KAAK,KAAK,SAAS,SAAS;AACpE,QAAI,gBAAgB,MAAM,OAAO,MAAM,MAAM;AAC3C,aAAO,IAAI,OAAO,CAAC,EAAE,KAAK,GAAG,SAAS,YAAY,CAAC,CAAC;AAAA,IACtD;AACA,QAAI,gBAAgB,MAAM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,QAAI,WAAW;AACb,UAAI,OAAO;AACT,gBAAQ,GAAG,SAAS,MAAM,KAAK;AAAA,MACjC,OAAO;AACL,gBAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AAEA,MAAI,OAAO,UAAU;AAAa,WAAO;AACzC,SAAO,OAAO,UAAU,WACpB,QACA,KAAK,UAAU,OAAO,QAAW,CAAC;AACxC;AAEO,IAAM,wBAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA","names":[],"ignoreList":[],"sources":["../../src/common/ui-utils.ts"],"sourcesContent":["import type {\n DetailedLocateParam,\n ExecutionTask,\n ExecutionTaskAction,\n ExecutionTaskInsightAssertion,\n ExecutionTaskInsightLocate,\n ExecutionTaskInsightQuery,\n ExecutionTaskPlanning,\n PlanningActionParamScroll,\n} from 'misoai-core';\n\nexport function typeStr(task: ExecutionTask) {\n return task.subType && task.subType !== 'Plan'\n ? `${task.type} / ${task.subType || ''}`\n : task.type;\n}\n\nexport function getKeyCommands(\n value: string | string[],\n): Array<{ key: string; command?: string }> {\n // Ensure value is an array of keys\n const keys = Array.isArray(value) ? value : [value];\n\n // Process each key to attach a corresponding command if needed, based on the presence of 'Meta' or 'Control' in the keys array.\n // ref: https://github.com/puppeteer/puppeteer/pull/9357/files#diff-32cf475237b000f980eb214a0a823e45a902bddb7d2426d677cae96397aa0ae4R94\n return keys.reduce((acc: Array<{ key: string; command?: string }>, k) => {\n const includeMeta = keys.includes('Meta') || keys.includes('Control');\n if (includeMeta && (k === 'a' || k === 'A')) {\n return acc.concat([{ key: k, command: 'SelectAll' }]);\n }\n if (includeMeta && (k === 'c' || k === 'C')) {\n return acc.concat([{ key: k, command: 'Copy' }]);\n }\n if (includeMeta && (k === 'v' || k === 'V')) {\n return acc.concat([{ key: k, command: 'Paste' }]);\n }\n return acc.concat([{ key: k }]);\n }, []);\n}\n\nexport function locateParamStr(locate?: DetailedLocateParam) {\n if (!locate) {\n return '';\n }\n\n if (typeof locate === 'string') {\n return locate;\n }\n\n return locate.prompt;\n}\n\nexport function scrollParamStr(scrollParam?: PlanningActionParamScroll) {\n if (!scrollParam) {\n return '';\n }\n return `${scrollParam.direction || 'down'}, ${scrollParam.scrollType || 'once'}, ${scrollParam.distance || 'distance-not-set'}`;\n}\n\nexport function taskTitleStr(\n type:\n | 'Tap'\n | 'Hover'\n | 'Input'\n | 'KeyboardPress'\n | 'Scroll'\n | 'Action'\n | 'Query'\n | 'Assert'\n | 'WaitFor'\n | 'Locate'\n | 'Boolean'\n | 'Number'\n | 'String',\n prompt: string,\n) {\n if (prompt) {\n return `${type} - ${prompt}`;\n }\n return type;\n}\n\nexport function paramStr(task: ExecutionTask) {\n let value: string | undefined | object;\n if (task.type === 'Planning') {\n value = (task as ExecutionTaskPlanning)?.param?.userInstruction;\n }\n\n if (task.type === 'Insight') {\n value =\n (task as ExecutionTaskInsightLocate)?.param?.prompt ||\n (task as ExecutionTaskInsightLocate)?.param?.id ||\n (task as ExecutionTaskInsightQuery)?.param?.dataDemand ||\n (task as ExecutionTaskInsightAssertion)?.param?.assertion;\n }\n\n if (task.type === 'Action') {\n const locate = (task as ExecutionTaskAction)?.locate;\n const locateStr = locate ? locateParamStr(locate) : '';\n\n value = task.thought || '';\n if (typeof (task as ExecutionTaskAction)?.param?.timeMs === 'number') {\n value = `${(task as ExecutionTaskAction)?.param?.timeMs}ms`;\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.scrollType === 'string'\n ) {\n value = scrollParamStr((task as ExecutionTaskAction)?.param);\n } else if (\n typeof (task as ExecutionTaskAction)?.param?.value !== 'undefined'\n ) {\n value = (task as ExecutionTaskAction)?.param?.value;\n }\n\n if (locateStr) {\n if (value) {\n value = `${locateStr} - ${value}`;\n } else {\n value = locateStr;\n }\n }\n }\n\n if (typeof value === 'undefined') return '';\n return typeof value === 'string'\n ? value\n : JSON.stringify(value, undefined, 2);\n}\n\nexport const limitOpenNewTabScript = `\nif (!window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__) {\n window.__MIDSCENE_NEW_TAB_INTERCEPTOR_INITIALIZED__ = true;\n\n // Intercept the window.open method (only once)\n window.open = function(url) {\n console.log('Blocked window.open:', url);\n window.location.href = url;\n return null;\n };\n\n // Block all a tag clicks with target=\"_blank\" (only once)\n document.addEventListener('click', function(e) {\n const target = e.target.closest('a');\n if (target && target.target === '_blank') {\n e.preventDefault();\n console.log('Blocked new tab:', target.href);\n window.location.href = target.href;\n target.removeAttribute('target');\n }\n }, true);\n}\n`;\n"]}
|
package/dist/es/utils.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"mappings":";AAQA,SAAS,wCAAwC;AACjD,SAAS,8BAA8B;AACvC,SAAS,0BAA0B,mBAAmB;AAEtD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,uBAAuB;AAEhC,SAAS,QAAQ,QAAQ,YAAY;AACrC,OAAO,WAAW;;;ACVX,IAAM,iBAAN,MAA4C;AAAA,EAsBjD,YAAY;AAAA,IACV;AAAA,IACA;AAAA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAYG;AACD,SAAK,UAAU;AACf,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,MACZ,KAAK,MAAM,KAAK,OAAO,KAAK,QAAQ,CAAC;AAAA,MACrC,KAAK,MAAM,KAAK,MAAM,KAAK,SAAS,CAAC;AAAA,IACvC;AAEA,SAAK,UAAU;AACf,SAAK,KAAK;AACV,SAAK,aAAa;AAClB,SAAK,UAAU;AACf,SAAK,SAAS;AAAA,EAChB;AACF;;;ADrCA,eAAsB,wBACpB,MACA,MACuB;AACvB,SAAO,MAAM,kBAAkB;AAC/B,MAAK,KAAoB,sBAAsB;AAC7C,WAAO,MAAO,KAAa,qBAAqB;AAA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,YAAQ,IAAI,iBAAiB,GAAG,EAAE;AAClC,QAAI,CAAE,MAAwB,SAAS,GAAG;AACxC,UAAI;AACF,cAAO,MAAwB,MAAM;AAAA,MACvC,SAAS,OAAO;AACd,cAAM,yBAAyB,GAAG,YAAY,KAAK,EAAE;AAAA,MACvD;AAAA,IACF,OAAO;AACL,YAAM,uCAAuC,GAAG,EAAE;AAAA,IACpD;AAEA,QAAI,CAAC,KAAK,SAAS,GAAG;AACpB,UAAI;AACF,cAAM,KAAK,KAAK,GAAG;AAAA,MACrB,SAAS,OAAO;AACd,cAAM,kBAAkB,GAAG,YAAY,KAAK,EAAE;AAAA,MAChD;AAAA,IACF,OAAO;AACL,YAAM,qCAAqC,GAAG,EAAE;AAAA,IAClD;AAAA,EACF,CAAC;AACH;AAEO,SAAS,qBACd,iBACA,MACA;AACA,MAAI,CAAC,iBAAiB;AACpB,WAAO;AAAA,EACT;AACA,MAAI,gBAAgB,IAAI;AACtB,WAAO,qBAAqB,gBAAgB,EAAE;AAAA,EAChD;AAEA,MAAI,gBAAgB,MAAM;AACxB,UAAM,iBAAiB;AAAA,MACrB,GAAG,KAAK,OAAO,gBAAgB,KAAK,CAAC,IAAI,gBAAgB,KAAK,CAAC,KAAK,CAAC;AAAA,MACrE,GAAG,KAAK,OAAO,gBAAgB,KAAK,CAAC,IAAI,gBAAgB,KAAK,CAAC,KAAK,CAAC;AAAA,IACvE;AACA,QAAI,UAAU,iCAAiC,MAAM,cAAc;AAEnE,QAAI,CAAC,SAAS;AACZ,gBAAU,0BAA0B,cAAc;AAAA,IACpD;AAEA,WAAO;AAAA,EACT;AAEA,SAAO;AACT","names":[],"ignoreList":[],"sources":["../../src/common/utils.ts","../../src/web-element.ts"],"sourcesContent":["import type { StaticPage } from '@/playground';\r\nimport type {\r\n BaseElement,\r\n ElementTreeNode,\r\n PlanningLocateParam,\r\n PlaywrightParserOpt,\r\n UIContext,\r\n} from 'misoai-core';\r\nimport { elementByPositionWithElementInfo } from 'misoai-core/ai-model';\r\nimport { uploadTestInfoToServer } from 'misoai-core/utils';\r\nimport { MIDSCENE_REPORT_TAG_NAME, getAIConfig } from 'misoai-shared/env';\r\nimport type { ElementInfo } from 'misoai-shared/extractor';\r\nimport {\r\n generateElementByPosition,\r\n getNodeFromCacheList,\r\n traverseTree,\r\n treeToList,\r\n} from 'misoai-shared/extractor';\r\nimport { resizeImgBase64 } from 'misoai-shared/img';\r\nimport type { DebugFunction } from 'misoai-shared/logger';\r\nimport { assert, logMsg, uuid } from 'misoai-shared/utils';\r\nimport dayjs from 'dayjs';\r\nimport type { Page as PlaywrightPage } from 'playwright';\r\nimport type { Page as PuppeteerPage } from 'puppeteer';\r\nimport { WebElementInfo } from '../web-element';\r\nimport type { WebPage } from './page';\r\n\r\nexport type WebUIContext = UIContext<WebElementInfo> & {\r\n url: string;\r\n};\r\n\r\nexport async function parseContextFromWebPage(\r\n page: WebPage,\r\n _opt?: PlaywrightParserOpt,\r\n): Promise<WebUIContext> {\r\n assert(page, 'page is required');\r\n if ((page as StaticPage)._forceUsePageContext) {\r\n return await (page as any)._forceUsePageContext();\r\n }\r\n const url = await page.url();\r\n uploadTestInfoToServer({ testUrl: url });\r\n\r\n let screenshotBase64: string;\r\n let tree: ElementTreeNode<ElementInfo>;\r\n\r\n await Promise.all([\r\n page.screenshotBase64().then((base64) => {\r\n screenshotBase64 = base64;\r\n }),\r\n page.getElementsNodeTree().then(async (treeRoot) => {\r\n tree = treeRoot;\r\n }),\r\n ]);\r\n\r\n const webTree = traverseTree(tree!, (elementInfo) => {\r\n const { rect, id, content, attributes, locator, indexId } = elementInfo;\r\n return new WebElementInfo({\r\n rect,\r\n locator,\r\n id,\r\n content,\r\n attributes,\r\n indexId,\r\n });\r\n });\r\n\r\n assert(screenshotBase64!, 'screenshotBase64 is required');\r\n\r\n const elementsInfo = treeToList(webTree);\r\n const size = await page.size();\r\n\r\n if (size.dpr && size.dpr > 1) {\r\n // console.time('resizeImgBase64');\r\n screenshotBase64 = await resizeImgBase64(screenshotBase64, {\r\n width: size.width,\r\n height: size.height,\r\n });\r\n // console.timeEnd('resizeImgBase64');\r\n }\r\n\r\n return {\r\n content: elementsInfo!,\r\n tree: webTree,\r\n size,\r\n screenshotBase64: screenshotBase64!,\r\n url,\r\n };\r\n}\r\n\r\nexport function reportFileName(tag = 'web') {\r\n const reportTagName = getAIConfig(MIDSCENE_REPORT_TAG_NAME);\r\n const dateTimeInFileName = dayjs().format('YYYY-MM-DD_HH-mm-ss');\r\n // ensure uniqueness at the same time\r\n const uniqueId = uuid().substring(0, 8);\r\n return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;\r\n}\r\n\r\nexport function printReportMsg(filepath: string) {\r\n logMsg(`Midscene - report file updated: ${filepath}`);\r\n}\r\n\r\n/**\r\n * Get the current execution file name\r\n * @returns The name of the current execution file\r\n */\r\nexport function getCurrentExecutionFile(trace?: string): string | false {\r\n const error = new Error();\r\n const stackTrace = trace || error.stack;\r\n const pkgDir = process.cwd() || '';\r\n if (stackTrace) {\r\n const stackLines = stackTrace.split('\\n');\r\n for (const line of stackLines) {\r\n if (\r\n line.includes('.spec.') ||\r\n line.includes('.test.') ||\r\n line.includes('.ts') ||\r\n line.includes('.js')\r\n ) {\r\n const match = line.match(/(?:at\\s+)?(.*?\\.(?:spec|test)\\.[jt]s)/);\r\n if (match?.[1]) {\r\n const targetFileName = match[1]\r\n .replace(pkgDir, '')\r\n .trim()\r\n .replace('at ', '');\r\n return targetFileName;\r\n }\r\n }\r\n }\r\n }\r\n return false;\r\n}\r\n\r\nconst testFileIndex = new Map<string, number>();\r\n\r\nexport function generateCacheId(fileName?: string): string {\r\n let taskFile = fileName || getCurrentExecutionFile();\r\n if (!taskFile) {\r\n taskFile = uuid();\r\n console.warn(\r\n 'Midscene - using random UUID for cache id. Cache may be invalid.',\r\n );\r\n }\r\n\r\n if (testFileIndex.has(taskFile)) {\r\n const currentIndex = testFileIndex.get(taskFile);\r\n if (currentIndex !== undefined) {\r\n testFileIndex.set(taskFile, currentIndex + 1);\r\n }\r\n } else {\r\n testFileIndex.set(taskFile, 1);\r\n }\r\n return `${taskFile}-${testFileIndex.get(taskFile)}`;\r\n}\r\n\r\nexport const ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED =\r\n 'NOT_IMPLEMENTED_AS_DESIGNED';\r\n\r\nexport function replaceIllegalPathCharsAndSpace(str: string) {\r\n return str.replace(/[/\\\\:*?\"<>| ]/g, '-');\r\n}\r\n\r\nexport function forceClosePopup(\r\n page: PuppeteerPage | PlaywrightPage,\r\n debug: DebugFunction,\r\n) {\r\n page.on('popup', async (popup) => {\r\n if (!popup) {\r\n console.warn('got a popup event, but the popup is not ready yet, skip');\r\n return;\r\n }\r\n const url = await (popup as PuppeteerPage).url();\r\n console.log(`Popup opened: ${url}`);\r\n if (!(popup as PuppeteerPage).isClosed()) {\r\n try {\r\n await (popup as PuppeteerPage).close(); // Close the newly opened TAB\r\n } catch (error) {\r\n debug(`failed to close popup ${url}, error: ${error}`);\r\n }\r\n } else {\r\n debug(`popup is already closed, skip close ${url}`);\r\n }\r\n\r\n if (!page.isClosed()) {\r\n try {\r\n await page.goto(url);\r\n } catch (error) {\r\n debug(`failed to goto ${url}, error: ${error}`);\r\n }\r\n } else {\r\n debug(`page is already closed, skip goto ${url}`);\r\n }\r\n });\r\n}\r\n\r\nexport function matchElementFromPlan(\r\n planLocateParam: PlanningLocateParam,\r\n tree: ElementTreeNode<BaseElement>,\r\n) {\r\n if (!planLocateParam) {\r\n return undefined;\r\n }\r\n if (planLocateParam.id) {\r\n return getNodeFromCacheList(planLocateParam.id);\r\n }\r\n\r\n if (planLocateParam.bbox) {\r\n const centerPosition = {\r\n x: Math.floor((planLocateParam.bbox[0] + planLocateParam.bbox[2]) / 2),\r\n y: Math.floor((planLocateParam.bbox[1] + planLocateParam.bbox[3]) / 2),\r\n };\r\n let element = elementByPositionWithElementInfo(tree, centerPosition);\r\n\r\n if (!element) {\r\n element = generateElementByPosition(centerPosition) as BaseElement;\r\n }\r\n\r\n return element;\r\n }\r\n\r\n return undefined;\r\n}\r\n","import type { BaseElement, Rect } from 'misoai-core';\r\nimport type { NodeType } from 'misoai-shared/constants';\r\nexport interface WebElementInfoType extends BaseElement {\r\n id: string;\r\n locator: string;\r\n attributes: {\r\n nodeType: NodeType;\r\n [key: string]: string;\r\n };\r\n}\r\n\r\nexport class WebElementInfo implements BaseElement {\r\n content: string;\r\n\r\n locator?: string;\r\n\r\n rect: Rect;\r\n\r\n center: [number, number];\r\n\r\n // page: WebPage;\r\n\r\n id: string;\r\n\r\n indexId: number;\r\n\r\n attributes: {\r\n nodeType: NodeType;\r\n [key: string]: string;\r\n };\r\n\r\n xpaths?: string[];\r\n\r\n constructor({\r\n content,\r\n rect,\r\n // page,\r\n locator,\r\n id,\r\n attributes,\r\n indexId,\r\n xpaths,\r\n }: {\r\n content: string;\r\n rect: Rect;\r\n // page: WebPage;\r\n locator?: string;\r\n id: string;\r\n attributes: {\r\n nodeType: NodeType;\r\n [key: string]: string;\r\n };\r\n indexId: number;\r\n xpaths?: string[];\r\n }) {\r\n this.content = content;\r\n this.rect = rect;\r\n this.center = [\r\n Math.floor(rect.left + rect.width / 2),\r\n Math.floor(rect.top + rect.height / 2),\r\n ];\r\n // this.page = page;\r\n this.locator = locator;\r\n this.id = id;\r\n this.attributes = attributes;\r\n this.indexId = indexId;\r\n this.xpaths = xpaths;\r\n }\r\n}\r\n"]}
|
1
|
+
{"version":3,"mappings":";AAQA,SAAS,wCAAwC;AACjD,SAAS,8BAA8B;AACvC,SAAS,0BAA0B,mBAAmB;AAEtD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,uBAAuB;AAEhC,SAAS,QAAQ,QAAQ,YAAY;AACrC,OAAO,WAAW;;;ACVX,IAAM,iBAAN,MAA4C;AAAA,EAsBjD,YAAY;AAAA,IACV;AAAA,IACA;AAAA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA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,OAAO,gBAAgB,KAAK,CAAC,IAAI,gBAAgB,KAAK,CAAC,KAAK,CAAC;AAAA,IACvE;AACA,QAAI,UAAU,iCAAiC,MAAM,cAAc;AAEnE,QAAI,CAAC,SAAS;AACZ,gBAAU,0BAA0B,cAAc;AAAA,IACpD;AAEA,WAAO;AAAA,EACT;AAEA,SAAO;AACT","names":[],"ignoreList":[],"sources":["../../src/common/utils.ts","../../src/web-element.ts"],"sourcesContent":["import type { StaticPage } from '@/playground';\nimport type {\n BaseElement,\n ElementTreeNode,\n PlanningLocateParam,\n PlaywrightParserOpt,\n UIContext,\n} from 'misoai-core';\nimport { elementByPositionWithElementInfo } from 'misoai-core/ai-model';\nimport { uploadTestInfoToServer } from 'misoai-core/utils';\nimport { MIDSCENE_REPORT_TAG_NAME, getAIConfig } from 'misoai-shared/env';\nimport type { ElementInfo } from 'misoai-shared/extractor';\nimport {\n generateElementByPosition,\n getNodeFromCacheList,\n traverseTree,\n treeToList,\n} from 'misoai-shared/extractor';\nimport { resizeImgBase64 } from 'misoai-shared/img';\nimport type { DebugFunction } from 'misoai-shared/logger';\nimport { assert, logMsg, uuid } from 'misoai-shared/utils';\nimport dayjs from 'dayjs';\nimport type { Page as PlaywrightPage } from 'playwright';\nimport type { Page as PuppeteerPage } from 'puppeteer';\nimport { WebElementInfo } from '../web-element';\nimport type { WebPage } from './page';\n\nexport type WebUIContext = UIContext<WebElementInfo> & {\n url: string;\n};\n\nexport async function parseContextFromWebPage(\n page: WebPage,\n _opt?: PlaywrightParserOpt,\n): Promise<WebUIContext> {\n assert(page, 'page is required');\n if ((page as StaticPage)._forceUsePageContext) {\n return await (page as any)._forceUsePageContext();\n }\n const url = await page.url();\n uploadTestInfoToServer({ testUrl: url });\n\n let screenshotBase64: string;\n let tree: ElementTreeNode<ElementInfo>;\n\n await Promise.all([\n page.screenshotBase64().then((base64) => {\n screenshotBase64 = base64;\n }),\n page.getElementsNodeTree().then(async (treeRoot) => {\n tree = treeRoot;\n }),\n ]);\n\n const webTree = traverseTree(tree!, (elementInfo) => {\n const { rect, id, content, attributes, locator, indexId } = elementInfo;\n return new WebElementInfo({\n rect,\n locator,\n id,\n content,\n attributes,\n indexId,\n });\n });\n\n assert(screenshotBase64!, 'screenshotBase64 is required');\n\n const elementsInfo = treeToList(webTree);\n const size = await page.size();\n\n if (size.dpr && size.dpr > 1) {\n // console.time('resizeImgBase64');\n screenshotBase64 = await resizeImgBase64(screenshotBase64, {\n width: size.width,\n height: size.height,\n });\n // console.timeEnd('resizeImgBase64');\n }\n\n return {\n content: elementsInfo!,\n tree: webTree,\n size,\n screenshotBase64: screenshotBase64!,\n url,\n };\n}\n\nexport function reportFileName(tag = 'web') {\n const reportTagName = getAIConfig(MIDSCENE_REPORT_TAG_NAME);\n const dateTimeInFileName = dayjs().format('YYYY-MM-DD_HH-mm-ss');\n // ensure uniqueness at the same time\n const uniqueId = uuid().substring(0, 8);\n return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;\n}\n\nexport function printReportMsg(filepath: string) {\n logMsg(`Midscene - report file updated: ${filepath}`);\n}\n\n/**\n * Get the current execution file name\n * @returns The name of the current execution file\n */\nexport function getCurrentExecutionFile(trace?: string): string | false {\n const error = new Error();\n const stackTrace = trace || error.stack;\n const pkgDir = process.cwd() || '';\n if (stackTrace) {\n const stackLines = stackTrace.split('\\n');\n for (const line of stackLines) {\n if (\n line.includes('.spec.') ||\n line.includes('.test.') ||\n line.includes('.ts') ||\n line.includes('.js')\n ) {\n const match = line.match(/(?:at\\s+)?(.*?\\.(?:spec|test)\\.[jt]s)/);\n if (match?.[1]) {\n const targetFileName = match[1]\n .replace(pkgDir, '')\n .trim()\n .replace('at ', '');\n return targetFileName;\n }\n }\n }\n }\n return false;\n}\n\nconst testFileIndex = new Map<string, number>();\n\nexport function generateCacheId(fileName?: string): string {\n let taskFile = fileName || getCurrentExecutionFile();\n if (!taskFile) {\n taskFile = uuid();\n console.warn(\n 'Midscene - using random UUID for cache id. Cache may be invalid.',\n );\n }\n\n if (testFileIndex.has(taskFile)) {\n const currentIndex = testFileIndex.get(taskFile);\n if (currentIndex !== undefined) {\n testFileIndex.set(taskFile, currentIndex + 1);\n }\n } else {\n testFileIndex.set(taskFile, 1);\n }\n return `${taskFile}-${testFileIndex.get(taskFile)}`;\n}\n\nexport const ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED =\n 'NOT_IMPLEMENTED_AS_DESIGNED';\n\nexport function replaceIllegalPathCharsAndSpace(str: string) {\n return str.replace(/[/\\\\:*?\"<>| ]/g, '-');\n}\n\nexport function forceClosePopup(\n page: PuppeteerPage | PlaywrightPage,\n debug: DebugFunction,\n) {\n page.on('popup', async (popup) => {\n if (!popup) {\n console.warn('got a popup event, but the popup is not ready yet, skip');\n return;\n }\n const url = await (popup as PuppeteerPage).url();\n console.log(`Popup opened: ${url}`);\n if (!(popup as PuppeteerPage).isClosed()) {\n try {\n await (popup as PuppeteerPage).close(); // Close the newly opened TAB\n } catch (error) {\n debug(`failed to close popup ${url}, error: ${error}`);\n }\n } else {\n debug(`popup is already closed, skip close ${url}`);\n }\n\n if (!page.isClosed()) {\n try {\n await page.goto(url);\n } catch (error) {\n debug(`failed to goto ${url}, error: ${error}`);\n }\n } else {\n debug(`page is already closed, skip goto ${url}`);\n }\n });\n}\n\nexport function matchElementFromPlan(\n planLocateParam: PlanningLocateParam,\n tree: ElementTreeNode<BaseElement>,\n) {\n if (!planLocateParam) {\n return undefined;\n }\n if (planLocateParam.id) {\n return getNodeFromCacheList(planLocateParam.id);\n }\n\n if (planLocateParam.bbox) {\n const centerPosition = {\n x: Math.floor((planLocateParam.bbox[0] + planLocateParam.bbox[2]) / 2),\n y: Math.floor((planLocateParam.bbox[1] + planLocateParam.bbox[3]) / 2),\n };\n let element = elementByPositionWithElementInfo(tree, centerPosition);\n\n if (!element) {\n element = generateElementByPosition(centerPosition) as BaseElement;\n }\n\n return element;\n }\n\n return undefined;\n}\n","import type { BaseElement, Rect } from 'misoai-core';\nimport type { NodeType } from 'misoai-shared/constants';\nexport interface WebElementInfoType extends BaseElement {\n id: string;\n locator: string;\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n}\n\nexport class WebElementInfo implements BaseElement {\n content: string;\n\n locator?: string;\n\n rect: Rect;\n\n center: [number, number];\n\n // page: WebPage;\n\n id: string;\n\n indexId: number;\n\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n\n xpaths?: string[];\n\n constructor({\n content,\n rect,\n // page,\n locator,\n id,\n attributes,\n indexId,\n xpaths,\n }: {\n content: string;\n rect: Rect;\n // page: WebPage;\n locator?: string;\n id: string;\n attributes: {\n nodeType: NodeType;\n [key: string]: string;\n };\n indexId: number;\n xpaths?: string[];\n }) {\n this.content = content;\n this.rect = rect;\n this.center = [\n Math.floor(rect.left + rect.width / 2),\n Math.floor(rect.top + rect.height / 2),\n ];\n // this.page = page;\n this.locator = locator;\n this.id = id;\n this.attributes = attributes;\n this.indexId = indexId;\n this.xpaths = xpaths;\n }\n}\n"]}
|
package/dist/es/yaml.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"mappings":";AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,SAAS,MAAM,eAAe;AACvC,SAAS,QAAQ,mBAAmB;AAyBpC,SAAS,4BAA4B;AAE9B,IAAM,eAAN,MAAoD;AAAA,EAWzD,YACU,QACA,YAID,oBACP;AANQ;AACA;AAID;AAfT,SAAO,iBAA2C,CAAC;AACnD,SAAO,SAAkC;AAGzC,SAAQ,qBAAqB;AAG7B,SAAQ,YAA8B;AAUpC,SAAK,SAAS,CAAC;AAEf,QAAI,aAAa;AACf,WAAK,SAAS;AAAA,IAChB,WAAW,OAAO,QAAQ,QAAQ;AAChC,WAAK,SAAS,QAAQ,QAAQ,IAAI,GAAG,OAAO,OAAO,MAAM;AAAA,IAC3D,OAAO;AACL,WAAK,SAAS,KAAK,qBAAqB,QAAQ,GAAG,GAAG,QAAQ,GAAG,OAAO;AAA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E,OAAO,QAAQ,YAAY,OAAO,YAAY;AAAA,QAC9C,kDAAkD,OAAO;AAAA,MAC3D;AAAA,IACF;AAAA,EACF;AAEA,EAAAA,QAAO,IAAI,OAAO,+CAA+C,OAAO,EAAE;AAC1E,EAAAA;AAAA,IACE,MAAM,QAAQ,IAAI,KAAK;AAAA,IACvB,6DAA6D,IAAI,KAAK;AAAA,EACxE;AACA,SAAO;AACT","names":["resolve","assert","yaml"],"ignoreList":[],"sources":["../../src/yaml/player.ts","../../src/yaml/builder.ts","../../src/yaml/utils.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\r\nimport { dirname, join, resolve } from 'node:path';\r\nimport { assert, ifInBrowser } from 'misoai-shared/utils';\r\n\r\nimport type { PageAgent } from '@/common/agent';\r\nimport type {\r\n FreeFn,\r\n MidsceneYamlFlowItemAIAction,\r\n MidsceneYamlFlowItemAIAssert,\r\n MidsceneYamlFlowItemAIBoolean,\r\n MidsceneYamlFlowItemAIHover,\r\n MidsceneYamlFlowItemAIInput,\r\n MidsceneYamlFlowItemAIKeyboardPress,\r\n MidsceneYamlFlowItemAILocate,\r\n MidsceneYamlFlowItemAINString,\r\n MidsceneYamlFlowItemAINumber,\r\n MidsceneYamlFlowItemAIQuery,\r\n MidsceneYamlFlowItemAIScroll,\r\n MidsceneYamlFlowItemAITap,\r\n MidsceneYamlFlowItemAIWaitFor,\r\n MidsceneYamlFlowItemEvaluateJavaScript,\r\n MidsceneYamlFlowItemSleep,\r\n MidsceneYamlScript,\r\n MidsceneYamlScriptEnv,\r\n ScriptPlayerStatusValue,\r\n ScriptPlayerTaskStatus,\r\n} from 'misoai-core';\r\nimport { getMidsceneRunSubDir } from 'misoai-shared/common';\r\n\r\nexport class ScriptPlayer<T extends MidsceneYamlScriptEnv> {\r\n public currentTaskIndex?: number;\r\n public taskStatusList: ScriptPlayerTaskStatus[] = [];\r\n public status: ScriptPlayerStatusValue = 'init';\r\n public reportFile?: string | null;\r\n public result: Record<string, any>;\r\n private unnamedResultIndex = 0;\r\n public output?: string | null;\r\n public errorInSetup?: Error;\r\n private pageAgent: PageAgent | null = null;\r\n public agentStatusTip?: string;\r\n constructor(\r\n private script: MidsceneYamlScript,\r\n private setupAgent: (platform: T) => Promise<{\r\n agent: PageAgent;\r\n freeFn: FreeFn[];\r\n }>,\r\n public onTaskStatusChange?: (taskStatus: ScriptPlayerTaskStatus) => void,\r\n ) {\r\n this.result = {};\r\n\r\n if (ifInBrowser) {\r\n this.output = undefined;\r\n } else if (script.target?.output) {\r\n this.output = resolve(process.cwd(), script.target.output);\r\n } else {\r\n this.output = join(getMidsceneRunSubDir('output'), `${process.pid}.json`);\r\n }\r\n\r\n this.taskStatusList = (script.tasks || []).map((task, taskIndex) => ({\r\n ...task,\r\n index: taskIndex,\r\n status: 'init',\r\n totalSteps: task.flow?.length || 0,\r\n }));\r\n }\r\n\r\n private setResult(key: string | undefined, value: any) {\r\n const keyToUse = key || this.unnamedResultIndex++;\r\n if (this.result[keyToUse]) {\r\n console.warn(`result key ${keyToUse} already exists, will overwrite`);\r\n }\r\n this.result[keyToUse] = value;\r\n\r\n this.flushResult();\r\n }\r\n\r\n private setPlayerStatus(status: ScriptPlayerStatusValue, error?: Error) {\r\n this.status = status;\r\n this.errorInSetup = error;\r\n }\r\n\r\n private notifyCurrentTaskStatusChange(taskIndex?: number) {\r\n const taskIndexToNotify =\r\n typeof taskIndex === 'number' ? taskIndex : this.currentTaskIndex;\r\n\r\n if (typeof taskIndexToNotify !== 'number') {\r\n return;\r\n }\r\n\r\n const taskStatus = this.taskStatusList[taskIndexToNotify];\r\n if (this.onTaskStatusChange) {\r\n this.onTaskStatusChange(taskStatus);\r\n }\r\n }\r\n\r\n private async setTaskStatus(\r\n index: number,\r\n statusValue: ScriptPlayerStatusValue,\r\n error?: Error,\r\n ) {\r\n this.taskStatusList[index].status = statusValue;\r\n if (error) {\r\n this.taskStatusList[index].error = error;\r\n }\r\n\r\n this.notifyCurrentTaskStatusChange(index);\r\n }\r\n\r\n private setTaskIndex(taskIndex: number) {\r\n this.currentTaskIndex = taskIndex;\r\n }\r\n\r\n private flushResult() {\r\n if (Object.keys(this.result).length && this.output) {\r\n const output = resolve(process.cwd(), this.output);\r\n const outputDir = dirname(output);\r\n if (!existsSync(outputDir)) {\r\n mkdirSync(outputDir, { recursive: true });\r\n }\r\n writeFileSync(output, JSON.stringify(this.result, undefined, 2));\r\n }\r\n }\r\n\r\n async playTask(taskStatus: ScriptPlayerTaskStatus, agent: PageAgent) {\r\n const { flow } = taskStatus;\r\n assert(flow, 'missing flow in task');\r\n\r\n for (const flowItemIndex in flow) {\r\n const currentStep = Number.parseInt(flowItemIndex, 10);\r\n taskStatus.currentStep = currentStep;\r\n const flowItem = flow[flowItemIndex];\r\n if (\r\n 'aiAction' in (flowItem as MidsceneYamlFlowItemAIAction) ||\r\n 'ai' in (flowItem as MidsceneYamlFlowItemAIAction)\r\n ) {\r\n const actionTask = flowItem as MidsceneYamlFlowItemAIAction;\r\n const prompt = actionTask.aiAction || actionTask.ai;\r\n assert(prompt, 'missing prompt for ai (aiAction)');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for aiAction must be a string',\r\n );\r\n await agent.aiAction(prompt, {\r\n cacheable: actionTask.cacheable,\r\n });\r\n } else if ('aiAssert' in (flowItem as MidsceneYamlFlowItemAIAssert)) {\r\n const assertTask = flowItem as MidsceneYamlFlowItemAIAssert;\r\n const prompt = assertTask.aiAssert;\r\n assert(prompt, 'missing prompt for aiAssert');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for aiAssert must be a string',\r\n );\r\n await agent.aiAssert(prompt);\r\n } else if ('aiQuery' in (flowItem as MidsceneYamlFlowItemAIQuery)) {\r\n const queryTask = flowItem as MidsceneYamlFlowItemAIQuery;\r\n const prompt = queryTask.aiQuery;\r\n assert(prompt, 'missing prompt for aiQuery');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for aiQuery must be a string',\r\n );\r\n const queryResult = await agent.aiQuery(prompt);\r\n this.setResult(queryTask.name, queryResult);\r\n } else if ('aiNumber' in (flowItem as MidsceneYamlFlowItemAINumber)) {\r\n const numberTask = flowItem as MidsceneYamlFlowItemAINumber;\r\n const prompt = numberTask.aiNumber;\r\n assert(prompt, 'missing prompt for number');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for number must be a string',\r\n );\r\n const numberResult = await agent.aiNumber(prompt);\r\n this.setResult(numberTask.name, numberResult);\r\n } else if ('aiString' in (flowItem as MidsceneYamlFlowItemAINString)) {\r\n const stringTask = flowItem as MidsceneYamlFlowItemAINString;\r\n const prompt = stringTask.aiString;\r\n assert(prompt, 'missing prompt for string');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for string must be a string',\r\n );\r\n const stringResult = await agent.aiString(prompt);\r\n this.setResult(stringTask.name, stringResult);\r\n } else if ('aiBoolean' in (flowItem as MidsceneYamlFlowItemAIBoolean)) {\r\n const booleanTask = flowItem as MidsceneYamlFlowItemAIBoolean;\r\n const prompt = booleanTask.aiBoolean;\r\n assert(prompt, 'missing prompt for boolean');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for boolean must be a string',\r\n );\r\n const booleanResult = await agent.aiBoolean(prompt);\r\n this.setResult(booleanTask.name, booleanResult);\r\n } else if ('aiLocate' in (flowItem as MidsceneYamlFlowItemAILocate)) {\r\n const locateTask = flowItem as MidsceneYamlFlowItemAILocate;\r\n const prompt = locateTask.aiLocate;\r\n assert(prompt, 'missing prompt for aiLocate');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for aiLocate must be a string',\r\n );\r\n const locateResult = await agent.aiLocate(prompt);\r\n this.setResult(locateTask.name, locateResult);\r\n } else if ('aiWaitFor' in (flowItem as MidsceneYamlFlowItemAIWaitFor)) {\r\n const waitForTask = flowItem as MidsceneYamlFlowItemAIWaitFor;\r\n const prompt = waitForTask.aiWaitFor;\r\n assert(prompt, 'missing prompt for aiWaitFor');\r\n assert(\r\n typeof prompt === 'string',\r\n 'prompt for aiWaitFor must be a string',\r\n );\r\n const timeout = waitForTask.timeout;\r\n await agent.aiWaitFor(prompt, { timeoutMs: timeout });\r\n } else if ('sleep' in (flowItem as MidsceneYamlFlowItemSleep)) {\r\n const sleepTask = flowItem as MidsceneYamlFlowItemSleep;\r\n const ms = sleepTask.sleep;\r\n let msNumber = ms;\r\n if (typeof ms === 'string') {\r\n msNumber = Number.parseInt(ms, 10);\r\n }\r\n assert(\r\n msNumber && msNumber > 0,\r\n `ms for sleep must be greater than 0, but got ${ms}`,\r\n );\r\n await new Promise((resolve) => setTimeout(resolve, msNumber));\r\n } else if ('aiTap' in (flowItem as MidsceneYamlFlowItemAITap)) {\r\n const tapTask = flowItem as MidsceneYamlFlowItemAITap;\r\n await agent.aiTap(tapTask.aiTap, tapTask);\r\n } else if ('aiHover' in (flowItem as MidsceneYamlFlowItemAIHover)) {\r\n const hoverTask = flowItem as MidsceneYamlFlowItemAIHover;\r\n await agent.aiHover(hoverTask.aiHover, hoverTask);\r\n } else if ('aiInput' in (flowItem as MidsceneYamlFlowItemAIInput)) {\r\n // may be input empty string ''\r\n const inputTask = flowItem as MidsceneYamlFlowItemAIInput;\r\n await agent.aiInput(inputTask.aiInput, inputTask.locate, inputTask);\r\n } else if (\r\n 'aiKeyboardPress' in (flowItem as MidsceneYamlFlowItemAIKeyboardPress)\r\n ) {\r\n const keyboardPressTask =\r\n flowItem as MidsceneYamlFlowItemAIKeyboardPress;\r\n await agent.aiKeyboardPress(\r\n keyboardPressTask.aiKeyboardPress,\r\n keyboardPressTask.locate,\r\n keyboardPressTask,\r\n );\r\n } else if ('aiScroll' in (flowItem as MidsceneYamlFlowItemAIScroll)) {\r\n const scrollTask = flowItem as MidsceneYamlFlowItemAIScroll;\r\n await agent.aiScroll(scrollTask, scrollTask.locate, scrollTask);\r\n } else if (\r\n 'javascript' in (flowItem as MidsceneYamlFlowItemEvaluateJavaScript)\r\n ) {\r\n const evaluateJavaScriptTask =\r\n flowItem as MidsceneYamlFlowItemEvaluateJavaScript;\r\n\r\n const result = await agent.evaluateJavaScript(\r\n evaluateJavaScriptTask.javascript,\r\n );\r\n this.setResult(evaluateJavaScriptTask.name, result);\r\n } else {\r\n throw new Error(`unknown flowItem: ${JSON.stringify(flowItem)}`);\r\n }\r\n }\r\n this.reportFile = agent.reportFile;\r\n }\r\n\r\n async run() {\r\n const { target, web, android, tasks } = this.script;\r\n const webEnv = web || target;\r\n const androidEnv = android;\r\n const platform = webEnv || androidEnv;\r\n\r\n this.setPlayerStatus('running');\r\n\r\n let agent: PageAgent | null = null;\r\n let freeFn: FreeFn[] = [];\r\n try {\r\n const { agent: newAgent, freeFn: newFreeFn } = await this.setupAgent(\r\n platform as T,\r\n );\r\n agent = newAgent;\r\n const originalOnTaskStartTip = agent.onTaskStartTip;\r\n agent.onTaskStartTip = (tip) => {\r\n if (this.status === 'running') {\r\n this.agentStatusTip = tip;\r\n }\r\n originalOnTaskStartTip?.(tip);\r\n };\r\n freeFn = [\r\n ...(newFreeFn || []),\r\n {\r\n name: 'restore-agent-onTaskStartTip',\r\n fn: () => {\r\n if (agent) {\r\n agent.onTaskStartTip = originalOnTaskStartTip;\r\n }\r\n },\r\n },\r\n ];\r\n } catch (e) {\r\n this.setPlayerStatus('error', e as Error);\r\n return;\r\n }\r\n this.pageAgent = agent;\r\n\r\n let taskIndex = 0;\r\n this.setPlayerStatus('running');\r\n let errorFlag = false;\r\n while (taskIndex < tasks.length) {\r\n const taskStatus = this.taskStatusList[taskIndex];\r\n this.setTaskStatus(taskIndex, 'running' as any);\r\n this.setTaskIndex(taskIndex);\r\n\r\n try {\r\n await this.playTask(taskStatus, this.pageAgent);\r\n this.setTaskStatus(taskIndex, 'done' as any);\r\n } catch (e) {\r\n this.setTaskStatus(taskIndex, 'error' as any, e as Error);\r\n\r\n if (taskStatus.continueOnError) {\r\n // nothing more to do\r\n } else {\r\n this.reportFile = agent.reportFile;\r\n errorFlag = true;\r\n break;\r\n }\r\n }\r\n this.reportFile = agent.reportFile;\r\n taskIndex++;\r\n }\r\n\r\n if (errorFlag) {\r\n this.setPlayerStatus('error');\r\n } else {\r\n this.setPlayerStatus('done');\r\n }\r\n this.agentStatusTip = '';\r\n\r\n // free the resources\r\n for (const fn of freeFn) {\r\n try {\r\n // console.log('freeing', fn.name);\r\n await fn.fn();\r\n // console.log('freed', fn.name);\r\n } catch (e) {\r\n // console.error('error freeing', fn.name, e);\r\n }\r\n }\r\n }\r\n}\r\n","import type {\r\n MidsceneYamlScript,\r\n MidsceneYamlScriptWebEnv,\r\n MidsceneYamlTask,\r\n} from 'misoai-core';\r\nimport yaml from 'js-yaml';\r\n\r\nexport function buildYaml(\r\n env: MidsceneYamlScriptWebEnv,\r\n tasks: MidsceneYamlTask[],\r\n) {\r\n const result: MidsceneYamlScript = {\r\n target: env,\r\n tasks,\r\n };\r\n\r\n return yaml.dump(result, {\r\n indent: 2,\r\n });\r\n}\r\n","import { assert } from 'misoai-shared/utils';\r\nimport yaml from 'js-yaml';\r\n\r\nimport type { MidsceneYamlScript } from 'misoai-core';\r\n\r\nfunction interpolateEnvVars(content: string): string {\r\n return content.replace(/\\$\\{([^}]+)\\}/g, (_, envVar) => {\r\n const value = process.env[envVar.trim()];\r\n if (value === undefined) {\r\n throw new Error(`Environment variable \"${envVar.trim()}\" is not defined`);\r\n }\r\n return value;\r\n });\r\n}\r\n\r\nexport function parseYamlScript(\r\n content: string,\r\n filePath?: string,\r\n ignoreCheckingTarget?: boolean,\r\n): MidsceneYamlScript {\r\n let processedContent = content;\r\n if (content.indexOf('android') !== -1 && content.match(/deviceId:\\s*(\\d+)/)) {\r\n let matchedDeviceId;\r\n processedContent = content.replace(\r\n /deviceId:\\s*(\\d+)/g,\r\n (match, deviceId) => {\r\n matchedDeviceId = deviceId;\r\n return `deviceId: '${deviceId}'`;\r\n },\r\n );\r\n console.warn(\r\n `please use string-style deviceId in yaml script, for example: deviceId: \"${matchedDeviceId}\"`,\r\n );\r\n }\r\n const interpolatedContent = interpolateEnvVars(processedContent);\r\n const obj = yaml.load(interpolatedContent, {\r\n schema: yaml.JSON_SCHEMA,\r\n }) as MidsceneYamlScript;\r\n\r\n const pathTip = filePath ? `, failed to load ${filePath}` : '';\r\n const android =\r\n typeof obj.android !== 'undefined'\r\n ? Object.assign({}, obj.android || {})\r\n : undefined;\r\n const webConfig = obj.web || obj.target; // no need to handle null case, because web has required parameters url\r\n const web =\r\n typeof webConfig !== 'undefined'\r\n ? Object.assign({}, webConfig || {})\r\n : undefined;\r\n\r\n if (!ignoreCheckingTarget) {\r\n // make sure at least one of target/web/android is provided\r\n assert(\r\n web || android,\r\n `at least one of \"target\", \"web\", or \"android\" properties is required in yaml script${pathTip}`,\r\n );\r\n\r\n // make sure only one of target/web/android is provided\r\n assert(\r\n (web && !android) || (!web && android),\r\n `only one of \"target\", \"web\", or \"android\" properties is allowed in yaml script${pathTip}`,\r\n );\r\n\r\n // make sure the config is valid\r\n if (web || android) {\r\n assert(\r\n typeof web === 'object' || typeof android === 'object',\r\n `property \"target/web/android\" must be an object${pathTip}`,\r\n );\r\n }\r\n }\r\n\r\n assert(obj.tasks, `property \"tasks\" is required in yaml script ${pathTip}`);\r\n assert(\r\n Array.isArray(obj.tasks),\r\n `property \"tasks\" must be an array in yaml script, but got ${obj.tasks}`,\r\n );\r\n return obj;\r\n}\r\n"]}
|
1
|
+
{"version":3,"mappings":";AAAA,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,SAAS,MAAM,eAAe;AACvC,SAAS,QAAQ,mBAAmB;AAyBpC,SAAS,4BAA4B;AAE9B,IAAM,eAAN,MAAoD;AAAA,EAWzD,YACU,QACA,YAID,oBACP;AANQ;AACA;AAID;AAfT,SAAO,iBAA2C,CAAC;AACnD,SAAO,SAAkC;AAGzC,SAAQ,qBAAqB;AAG7B,SAAQ,YAA8B;AAUpC,SAAK,SAAS,CAAC;AAEf,QAAI,aAAa;AACf,WAAK,SAAS;AAAA,IAChB,WAAW,OAAO,QAAQ,Q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,OAAO;AAAA,IAC/F;AAGA,IAAAA;AAAA,MACG,OAAO,CAAC,WAAa,CAAC,OAAO;AAAA,MAC9B,iFAAiF,OAAO;AAAA,IAC1F;AAGA,QAAI,OAAO,SAAS;AAClB,MAAAA;AAAA,QACE,OAAO,QAAQ,YAAY,OAAO,YAAY;AAAA,QAC9C,kDAAkD,OAAO;AAAA,MAC3D;AAAA,IACF;AAAA,EACF;AAEA,EAAAA,QAAO,IAAI,OAAO,+CAA+C,OAAO,EAAE;AAC1E,EAAAA;AAAA,IACE,MAAM,QAAQ,IAAI,KAAK;AAAA,IACvB,6DAA6D,IAAI,KAAK;AAAA,EACxE;AACA,SAAO;AACT","names":["resolve","assert","yaml"],"ignoreList":[],"sources":["../../src/yaml/player.ts","../../src/yaml/builder.ts","../../src/yaml/utils.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { dirname, join, resolve } from 'node:path';\nimport { assert, ifInBrowser } from 'misoai-shared/utils';\n\nimport type { PageAgent } from '@/common/agent';\nimport type {\n FreeFn,\n MidsceneYamlFlowItemAIAction,\n MidsceneYamlFlowItemAIAssert,\n MidsceneYamlFlowItemAIBoolean,\n MidsceneYamlFlowItemAIHover,\n MidsceneYamlFlowItemAIInput,\n MidsceneYamlFlowItemAIKeyboardPress,\n MidsceneYamlFlowItemAILocate,\n MidsceneYamlFlowItemAINString,\n MidsceneYamlFlowItemAINumber,\n MidsceneYamlFlowItemAIQuery,\n MidsceneYamlFlowItemAIScroll,\n MidsceneYamlFlowItemAITap,\n MidsceneYamlFlowItemAIWaitFor,\n MidsceneYamlFlowItemEvaluateJavaScript,\n MidsceneYamlFlowItemSleep,\n MidsceneYamlScript,\n MidsceneYamlScriptEnv,\n ScriptPlayerStatusValue,\n ScriptPlayerTaskStatus,\n} from 'misoai-core';\nimport { getMidsceneRunSubDir } from 'misoai-shared/common';\n\nexport class ScriptPlayer<T extends MidsceneYamlScriptEnv> {\n public currentTaskIndex?: number;\n public taskStatusList: ScriptPlayerTaskStatus[] = [];\n public status: ScriptPlayerStatusValue = 'init';\n public reportFile?: string | null;\n public result: Record<string, any>;\n private unnamedResultIndex = 0;\n public output?: string | null;\n public errorInSetup?: Error;\n private pageAgent: PageAgent | null = null;\n public agentStatusTip?: string;\n constructor(\n private script: MidsceneYamlScript,\n private setupAgent: (platform: T) => Promise<{\n agent: PageAgent;\n freeFn: FreeFn[];\n }>,\n public onTaskStatusChange?: (taskStatus: ScriptPlayerTaskStatus) => void,\n ) {\n this.result = {};\n\n if (ifInBrowser) {\n this.output = undefined;\n } else if (script.target?.output) {\n this.output = resolve(process.cwd(), script.target.output);\n } else {\n this.output = join(getMidsceneRunSubDir('output'), `${process.pid}.json`);\n }\n\n this.taskStatusList = (script.tasks || []).map((task, taskIndex) => ({\n ...task,\n index: taskIndex,\n status: 'init',\n totalSteps: task.flow?.length || 0,\n }));\n }\n\n private setResult(key: string | undefined, value: any) {\n const keyToUse = key || this.unnamedResultIndex++;\n if (this.result[keyToUse]) {\n console.warn(`result key ${keyToUse} already exists, will overwrite`);\n }\n this.result[keyToUse] = value;\n\n this.flushResult();\n }\n\n private setPlayerStatus(status: ScriptPlayerStatusValue, error?: Error) {\n this.status = status;\n this.errorInSetup = error;\n }\n\n private notifyCurrentTaskStatusChange(taskIndex?: number) {\n const taskIndexToNotify =\n typeof taskIndex === 'number' ? taskIndex : this.currentTaskIndex;\n\n if (typeof taskIndexToNotify !== 'number') {\n return;\n }\n\n const taskStatus = this.taskStatusList[taskIndexToNotify];\n if (this.onTaskStatusChange) {\n this.onTaskStatusChange(taskStatus);\n }\n }\n\n private async setTaskStatus(\n index: number,\n statusValue: ScriptPlayerStatusValue,\n error?: Error,\n ) {\n this.taskStatusList[index].status = statusValue;\n if (error) {\n this.taskStatusList[index].error = error;\n }\n\n this.notifyCurrentTaskStatusChange(index);\n }\n\n private setTaskIndex(taskIndex: number) {\n this.currentTaskIndex = taskIndex;\n }\n\n private flushResult() {\n if (Object.keys(this.result).length && this.output) {\n const output = resolve(process.cwd(), this.output);\n const outputDir = dirname(output);\n if (!existsSync(outputDir)) {\n mkdirSync(outputDir, { recursive: true });\n }\n writeFileSync(output, JSON.stringify(this.result, undefined, 2));\n }\n }\n\n async playTask(taskStatus: ScriptPlayerTaskStatus, agent: PageAgent) {\n const { flow } = taskStatus;\n assert(flow, 'missing flow in task');\n\n for (const flowItemIndex in flow) {\n const currentStep = Number.parseInt(flowItemIndex, 10);\n taskStatus.currentStep = currentStep;\n const flowItem = flow[flowItemIndex];\n if (\n 'aiAction' in (flowItem as MidsceneYamlFlowItemAIAction) ||\n 'ai' in (flowItem as MidsceneYamlFlowItemAIAction)\n ) {\n const actionTask = flowItem as MidsceneYamlFlowItemAIAction;\n const prompt = actionTask.aiAction || actionTask.ai;\n assert(prompt, 'missing prompt for ai (aiAction)');\n assert(\n typeof prompt === 'string',\n 'prompt for aiAction must be a string',\n );\n await agent.aiAction(prompt, {\n cacheable: actionTask.cacheable,\n });\n } else if ('aiAssert' in (flowItem as MidsceneYamlFlowItemAIAssert)) {\n const assertTask = flowItem as MidsceneYamlFlowItemAIAssert;\n const prompt = assertTask.aiAssert;\n assert(prompt, 'missing prompt for aiAssert');\n assert(\n typeof prompt === 'string',\n 'prompt for aiAssert must be a string',\n );\n await agent.aiAssert(prompt);\n } else if ('aiQuery' in (flowItem as MidsceneYamlFlowItemAIQuery)) {\n const queryTask = flowItem as MidsceneYamlFlowItemAIQuery;\n const prompt = queryTask.aiQuery;\n assert(prompt, 'missing prompt for aiQuery');\n assert(\n typeof prompt === 'string',\n 'prompt for aiQuery must be a string',\n );\n const queryResult = await agent.aiQuery(prompt);\n this.setResult(queryTask.name, queryResult);\n } else if ('aiNumber' in (flowItem as MidsceneYamlFlowItemAINumber)) {\n const numberTask = flowItem as MidsceneYamlFlowItemAINumber;\n const prompt = numberTask.aiNumber;\n assert(prompt, 'missing prompt for number');\n assert(\n typeof prompt === 'string',\n 'prompt for number must be a string',\n );\n const numberResult = await agent.aiNumber(prompt);\n this.setResult(numberTask.name, numberResult);\n } else if ('aiString' in (flowItem as MidsceneYamlFlowItemAINString)) {\n const stringTask = flowItem as MidsceneYamlFlowItemAINString;\n const prompt = stringTask.aiString;\n assert(prompt, 'missing prompt for string');\n assert(\n typeof prompt === 'string',\n 'prompt for string must be a string',\n );\n const stringResult = await agent.aiString(prompt);\n this.setResult(stringTask.name, stringResult);\n } else if ('aiBoolean' in (flowItem as MidsceneYamlFlowItemAIBoolean)) {\n const booleanTask = flowItem as MidsceneYamlFlowItemAIBoolean;\n const prompt = booleanTask.aiBoolean;\n assert(prompt, 'missing prompt for boolean');\n assert(\n typeof prompt === 'string',\n 'prompt for boolean must be a string',\n );\n const booleanResult = await agent.aiBoolean(prompt);\n this.setResult(booleanTask.name, booleanResult);\n } else if ('aiLocate' in (flowItem as MidsceneYamlFlowItemAILocate)) {\n const locateTask = flowItem as MidsceneYamlFlowItemAILocate;\n const prompt = locateTask.aiLocate;\n assert(prompt, 'missing prompt for aiLocate');\n assert(\n typeof prompt === 'string',\n 'prompt for aiLocate must be a string',\n );\n const locateResult = await agent.aiLocate(prompt);\n this.setResult(locateTask.name, locateResult);\n } else if ('aiWaitFor' in (flowItem as MidsceneYamlFlowItemAIWaitFor)) {\n const waitForTask = flowItem as MidsceneYamlFlowItemAIWaitFor;\n const prompt = waitForTask.aiWaitFor;\n assert(prompt, 'missing prompt for aiWaitFor');\n assert(\n typeof prompt === 'string',\n 'prompt for aiWaitFor must be a string',\n );\n const timeout = waitForTask.timeout;\n await agent.aiWaitFor(prompt, { timeoutMs: timeout });\n } else if ('sleep' in (flowItem as MidsceneYamlFlowItemSleep)) {\n const sleepTask = flowItem as MidsceneYamlFlowItemSleep;\n const ms = sleepTask.sleep;\n let msNumber = ms;\n if (typeof ms === 'string') {\n msNumber = Number.parseInt(ms, 10);\n }\n assert(\n msNumber && msNumber > 0,\n `ms for sleep must be greater than 0, but got ${ms}`,\n );\n await new Promise((resolve) => setTimeout(resolve, msNumber));\n } else if ('aiTap' in (flowItem as MidsceneYamlFlowItemAITap)) {\n const tapTask = flowItem as MidsceneYamlFlowItemAITap;\n await agent.aiTap(tapTask.aiTap, tapTask);\n } else if ('aiHover' in (flowItem as MidsceneYamlFlowItemAIHover)) {\n const hoverTask = flowItem as MidsceneYamlFlowItemAIHover;\n await agent.aiHover(hoverTask.aiHover, hoverTask);\n } else if ('aiInput' in (flowItem as MidsceneYamlFlowItemAIInput)) {\n // may be input empty string ''\n const inputTask = flowItem as MidsceneYamlFlowItemAIInput;\n await agent.aiInput(inputTask.aiInput, inputTask.locate, inputTask);\n } else if (\n 'aiKeyboardPress' in (flowItem as MidsceneYamlFlowItemAIKeyboardPress)\n ) {\n const keyboardPressTask =\n flowItem as MidsceneYamlFlowItemAIKeyboardPress;\n await agent.aiKeyboardPress(\n keyboardPressTask.aiKeyboardPress,\n keyboardPressTask.locate,\n keyboardPressTask,\n );\n } else if ('aiScroll' in (flowItem as MidsceneYamlFlowItemAIScroll)) {\n const scrollTask = flowItem as MidsceneYamlFlowItemAIScroll;\n await agent.aiScroll(scrollTask, scrollTask.locate, scrollTask);\n } else if (\n 'javascript' in (flowItem as MidsceneYamlFlowItemEvaluateJavaScript)\n ) {\n const evaluateJavaScriptTask =\n flowItem as MidsceneYamlFlowItemEvaluateJavaScript;\n\n const result = await agent.evaluateJavaScript(\n evaluateJavaScriptTask.javascript,\n );\n this.setResult(evaluateJavaScriptTask.name, result);\n } else {\n throw new Error(`unknown flowItem: ${JSON.stringify(flowItem)}`);\n }\n }\n this.reportFile = agent.reportFile;\n }\n\n async run() {\n const { target, web, android, tasks } = this.script;\n const webEnv = web || target;\n const androidEnv = android;\n const platform = webEnv || androidEnv;\n\n this.setPlayerStatus('running');\n\n let agent: PageAgent | null = null;\n let freeFn: FreeFn[] = [];\n try {\n const { agent: newAgent, freeFn: newFreeFn } = await this.setupAgent(\n platform as T,\n );\n agent = newAgent;\n const originalOnTaskStartTip = agent.onTaskStartTip;\n agent.onTaskStartTip = (tip) => {\n if (this.status === 'running') {\n this.agentStatusTip = tip;\n }\n originalOnTaskStartTip?.(tip);\n };\n freeFn = [\n ...(newFreeFn || []),\n {\n name: 'restore-agent-onTaskStartTip',\n fn: () => {\n if (agent) {\n agent.onTaskStartTip = originalOnTaskStartTip;\n }\n },\n },\n ];\n } catch (e) {\n this.setPlayerStatus('error', e as Error);\n return;\n }\n this.pageAgent = agent;\n\n let taskIndex = 0;\n this.setPlayerStatus('running');\n let errorFlag = false;\n while (taskIndex < tasks.length) {\n const taskStatus = this.taskStatusList[taskIndex];\n this.setTaskStatus(taskIndex, 'running' as any);\n this.setTaskIndex(taskIndex);\n\n try {\n await this.playTask(taskStatus, this.pageAgent);\n this.setTaskStatus(taskIndex, 'done' as any);\n } catch (e) {\n this.setTaskStatus(taskIndex, 'error' as any, e as Error);\n\n if (taskStatus.continueOnError) {\n // nothing more to do\n } else {\n this.reportFile = agent.reportFile;\n errorFlag = true;\n break;\n }\n }\n this.reportFile = agent.reportFile;\n taskIndex++;\n }\n\n if (errorFlag) {\n this.setPlayerStatus('error');\n } else {\n this.setPlayerStatus('done');\n }\n this.agentStatusTip = '';\n\n // free the resources\n for (const fn of freeFn) {\n try {\n // console.log('freeing', fn.name);\n await fn.fn();\n // console.log('freed', fn.name);\n } catch (e) {\n // console.error('error freeing', fn.name, e);\n }\n }\n }\n}\n","import type {\n MidsceneYamlScript,\n MidsceneYamlScriptWebEnv,\n MidsceneYamlTask,\n} from 'misoai-core';\nimport yaml from 'js-yaml';\n\nexport function buildYaml(\n env: MidsceneYamlScriptWebEnv,\n tasks: MidsceneYamlTask[],\n) {\n const result: MidsceneYamlScript = {\n target: env,\n tasks,\n };\n\n return yaml.dump(result, {\n indent: 2,\n });\n}\n","import { assert } from 'misoai-shared/utils';\nimport yaml from 'js-yaml';\n\nimport type { MidsceneYamlScript } from 'misoai-core';\n\nfunction interpolateEnvVars(content: string): string {\n return content.replace(/\\$\\{([^}]+)\\}/g, (_, envVar) => {\n const value = process.env[envVar.trim()];\n if (value === undefined) {\n throw new Error(`Environment variable \"${envVar.trim()}\" is not defined`);\n }\n return value;\n });\n}\n\nexport function parseYamlScript(\n content: string,\n filePath?: string,\n ignoreCheckingTarget?: boolean,\n): MidsceneYamlScript {\n let processedContent = content;\n if (content.indexOf('android') !== -1 && content.match(/deviceId:\\s*(\\d+)/)) {\n let matchedDeviceId;\n processedContent = content.replace(\n /deviceId:\\s*(\\d+)/g,\n (match, deviceId) => {\n matchedDeviceId = deviceId;\n return `deviceId: '${deviceId}'`;\n },\n );\n console.warn(\n `please use string-style deviceId in yaml script, for example: deviceId: \"${matchedDeviceId}\"`,\n );\n }\n const interpolatedContent = interpolateEnvVars(processedContent);\n const obj = yaml.load(interpolatedContent, {\n schema: yaml.JSON_SCHEMA,\n }) as MidsceneYamlScript;\n\n const pathTip = filePath ? `, failed to load ${filePath}` : '';\n const android =\n typeof obj.android !== 'undefined'\n ? Object.assign({}, obj.android || {})\n : undefined;\n const webConfig = obj.web || obj.target; // no need to handle null case, because web has required parameters url\n const web =\n typeof webConfig !== 'undefined'\n ? Object.assign({}, webConfig || {})\n : undefined;\n\n if (!ignoreCheckingTarget) {\n // make sure at least one of target/web/android is provided\n assert(\n web || android,\n `at least one of \"target\", \"web\", or \"android\" properties is required in yaml script${pathTip}`,\n );\n\n // make sure only one of target/web/android is provided\n assert(\n (web && !android) || (!web && android),\n `only one of \"target\", \"web\", or \"android\" properties is allowed in yaml script${pathTip}`,\n );\n\n // make sure the config is valid\n if (web || android) {\n assert(\n typeof web === 'object' || typeof android === 'object',\n `property \"target/web/android\" must be an object${pathTip}`,\n );\n }\n }\n\n assert(obj.tasks, `property \"tasks\" is required in yaml script ${pathTip}`);\n assert(\n Array.isArray(obj.tasks),\n `property \"tasks\" must be an array in yaml script, but got ${obj.tasks}`,\n );\n return obj;\n}\n"]}
|
package/dist/lib/agent.js
CHANGED
@@ -1702,44 +1702,70 @@ var TaskCache = class {
|
|
1702
1702
|
this.cache = cacheContent;
|
1703
1703
|
this.cacheOriginalLength = this.cache.caches.length;
|
1704
1704
|
}
|
1705
|
-
matchCache(prompt, type) {
|
1705
|
+
matchCache(prompt, type, contextData) {
|
1706
|
+
const contextHash = contextData ? this.generateContextHash(contextData) : void 0;
|
1706
1707
|
for (let i = 0; i < this.cacheOriginalLength; i++) {
|
1707
1708
|
const item = this.cache.caches[i];
|
1708
1709
|
const key = `${type}:${prompt}:${i}`;
|
1709
|
-
if (item.type
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
cacheContent: item,
|
1719
|
-
updateFn: (cb) => {
|
1720
|
-
debug3(
|
1721
|
-
"will call updateFn to update cache, type: %s, prompt: %s, index: %d",
|
1722
|
-
type,
|
1723
|
-
prompt,
|
1724
|
-
i
|
1725
|
-
);
|
1726
|
-
cb(item);
|
1727
|
-
debug3(
|
1728
|
-
"cache updated, will flush to file, type: %s, prompt: %s, index: %d",
|
1729
|
-
type,
|
1730
|
-
prompt,
|
1731
|
-
i
|
1732
|
-
);
|
1733
|
-
this.flushCacheToFile();
|
1710
|
+
if (item.type !== type || item.prompt !== prompt || this.matchedCacheIndices.has(key)) {
|
1711
|
+
continue;
|
1712
|
+
}
|
1713
|
+
if (type === "plan" && item.type === "plan") {
|
1714
|
+
const planItem = item;
|
1715
|
+
if (contextHash && planItem.contextHash) {
|
1716
|
+
if (contextHash !== planItem.contextHash) {
|
1717
|
+
debug3("cache context mismatch, type: %s, prompt: %s, index: %d", type, prompt, i);
|
1718
|
+
continue;
|
1734
1719
|
}
|
1735
|
-
}
|
1720
|
+
} else if (contextHash || planItem.contextHash) {
|
1721
|
+
debug3("cache context availability mismatch, type: %s, prompt: %s, index: %d", type, prompt, i);
|
1722
|
+
continue;
|
1723
|
+
}
|
1736
1724
|
}
|
1725
|
+
this.matchedCacheIndices.add(key);
|
1726
|
+
debug3(
|
1727
|
+
"cache found and marked as used, type: %s, prompt: %s, index: %d, contextMatch: %s",
|
1728
|
+
type,
|
1729
|
+
prompt,
|
1730
|
+
i,
|
1731
|
+
contextHash ? "yes" : "no-context"
|
1732
|
+
);
|
1733
|
+
return {
|
1734
|
+
cacheContent: item,
|
1735
|
+
updateFn: (cb) => {
|
1736
|
+
debug3(
|
1737
|
+
"will call updateFn to update cache, type: %s, prompt: %s, index: %d",
|
1738
|
+
type,
|
1739
|
+
prompt,
|
1740
|
+
i
|
1741
|
+
);
|
1742
|
+
cb(item);
|
1743
|
+
debug3(
|
1744
|
+
"cache updated, will flush to file, type: %s, prompt: %s, index: %d",
|
1745
|
+
type,
|
1746
|
+
prompt,
|
1747
|
+
i
|
1748
|
+
);
|
1749
|
+
this.flushCacheToFile();
|
1750
|
+
}
|
1751
|
+
};
|
1737
1752
|
}
|
1738
|
-
debug3("no unused cache found, type: %s, prompt: %s", type, prompt);
|
1753
|
+
debug3("no unused cache found, type: %s, prompt: %s, contextHash: %s", type, prompt, contextHash);
|
1739
1754
|
return void 0;
|
1740
1755
|
}
|
1741
|
-
|
1742
|
-
|
1756
|
+
generateContextHash(contextData) {
|
1757
|
+
const sortedKeys = Object.keys(contextData).sort();
|
1758
|
+
const stableString = sortedKeys.map((key) => `${key}:${JSON.stringify(contextData[key])}`).join("|");
|
1759
|
+
let hash = 0;
|
1760
|
+
for (let i = 0; i < stableString.length; i++) {
|
1761
|
+
const char = stableString.charCodeAt(i);
|
1762
|
+
hash = (hash << 5) - hash + char;
|
1763
|
+
hash = hash & hash;
|
1764
|
+
}
|
1765
|
+
return hash.toString(36);
|
1766
|
+
}
|
1767
|
+
matchPlanCache(prompt, contextData) {
|
1768
|
+
return this.matchCache(prompt, "plan", contextData);
|
1743
1769
|
}
|
1744
1770
|
matchLocateCache(prompt) {
|
1745
1771
|
return this.matchCache(prompt, "locate");
|
@@ -1815,11 +1841,16 @@ cache file: ${cacheFile}`
|
|
1815
1841
|
);
|
1816
1842
|
}
|
1817
1843
|
}
|
1818
|
-
updateOrAppendCacheRecord(newRecord, cachedRecord) {
|
1844
|
+
updateOrAppendCacheRecord(newRecord, cachedRecord, contextData) {
|
1819
1845
|
if (cachedRecord) {
|
1820
1846
|
if (newRecord.type === "plan") {
|
1821
1847
|
cachedRecord.updateFn((cache) => {
|
1822
|
-
|
1848
|
+
const planCache = cache;
|
1849
|
+
planCache.yamlWorkflow = newRecord.yamlWorkflow;
|
1850
|
+
if (contextData) {
|
1851
|
+
planCache.contextHash = this.generateContextHash(contextData);
|
1852
|
+
planCache.contextData = { ...contextData };
|
1853
|
+
}
|
1823
1854
|
});
|
1824
1855
|
} else {
|
1825
1856
|
cachedRecord.updateFn((cache) => {
|
@@ -1827,6 +1858,11 @@ cache file: ${cacheFile}`
|
|
1827
1858
|
});
|
1828
1859
|
}
|
1829
1860
|
} else {
|
1861
|
+
if (newRecord.type === "plan" && contextData) {
|
1862
|
+
const planRecord = newRecord;
|
1863
|
+
planRecord.contextHash = this.generateContextHash(contextData);
|
1864
|
+
planRecord.contextData = { ...contextData };
|
1865
|
+
}
|
1830
1866
|
this.appendCache(newRecord);
|
1831
1867
|
}
|
1832
1868
|
}
|
@@ -2187,34 +2223,35 @@ var PageAgent = class {
|
|
2187
2223
|
};
|
2188
2224
|
}
|
2189
2225
|
async aiAction(taskPrompt, opt) {
|
2226
|
+
const originalPrompt = taskPrompt;
|
2227
|
+
let processedPrompt = taskPrompt;
|
2190
2228
|
if (this.opts.enableCumulativeContext && this.contextStore) {
|
2191
2229
|
try {
|
2192
|
-
const
|
2193
|
-
|
2194
|
-
|
2195
|
-
|
2196
|
-
|
2197
|
-
processed: processedPrompt,
|
2198
|
-
storedData: this.contextStore.getAllData()
|
2230
|
+
const storedData = this.contextStore.getAllData();
|
2231
|
+
if (Object.keys(storedData).length > 0) {
|
2232
|
+
debug4("Available data for aiAction:", {
|
2233
|
+
prompt: taskPrompt,
|
2234
|
+
availableData: storedData
|
2199
2235
|
});
|
2200
2236
|
}
|
2201
|
-
this.contextStore.addStep({
|
2202
|
-
type: "action",
|
2203
|
-
summary: `Action: ${processedPrompt}`,
|
2204
|
-
prompt: processedPrompt
|
2205
|
-
});
|
2206
|
-
debug4("Added action step to context store:", {
|
2207
|
-
stepNumber: this.contextStore.getRecentSteps(1)[0]?.stepNumber,
|
2208
|
-
totalSteps: this.contextStore.getRecentSteps(100).length
|
2209
|
-
});
|
2210
|
-
taskPrompt = processedPrompt;
|
2211
2237
|
} catch (error) {
|
2212
2238
|
debug4("Context store operation failed:", error);
|
2213
2239
|
}
|
2214
2240
|
}
|
2215
2241
|
const cacheable = opt?.cacheable;
|
2216
2242
|
const isVlmUiTars = (0, import_env2.vlLocateMode)() === "vlm-ui-tars";
|
2217
|
-
|
2243
|
+
let contextData;
|
2244
|
+
if (this.opts.enableCumulativeContext && this.contextStore) {
|
2245
|
+
try {
|
2246
|
+
contextData = this.contextStore.getAllData();
|
2247
|
+
if (contextData && Object.keys(contextData).length === 0) {
|
2248
|
+
contextData = void 0;
|
2249
|
+
}
|
2250
|
+
} catch (error) {
|
2251
|
+
debug4("Failed to get context data for cache:", error);
|
2252
|
+
}
|
2253
|
+
}
|
2254
|
+
const matchedCache = isVlmUiTars || cacheable === false ? void 0 : this.taskCache?.matchPlanCache(taskPrompt, contextData);
|
2218
2255
|
if (matchedCache && this.taskCache?.isCacheResultUsed) {
|
2219
2256
|
const { executor: executor2 } = await this.taskExecutor.loadYamlFlowAsPlanning(
|
2220
2257
|
taskPrompt,
|
@@ -2224,6 +2261,28 @@ var PageAgent = class {
|
|
2224
2261
|
debug4("matched cache, will call .runYaml to run the action");
|
2225
2262
|
const yaml5 = matchedCache.cacheContent?.yamlWorkflow;
|
2226
2263
|
const result = await this.runYaml(yaml5);
|
2264
|
+
if (this.opts.enableCumulativeContext && this.contextStore) {
|
2265
|
+
try {
|
2266
|
+
const executionResult = {
|
2267
|
+
success: true,
|
2268
|
+
actionType: "cached",
|
2269
|
+
description: `Executed cached action: ${processedPrompt}`,
|
2270
|
+
timing: result.metadata?.totalTime
|
2271
|
+
};
|
2272
|
+
this.contextStore.addStep({
|
2273
|
+
type: "action",
|
2274
|
+
summary: `Action: ${processedPrompt} (cached)`,
|
2275
|
+
prompt: processedPrompt,
|
2276
|
+
executionResult
|
2277
|
+
});
|
2278
|
+
debug4("Added cached action step to context store:", {
|
2279
|
+
stepNumber: this.contextStore.getRecentSteps(1)[0]?.stepNumber,
|
2280
|
+
totalSteps: this.contextStore.getRecentSteps(100).length
|
2281
|
+
});
|
2282
|
+
} catch (error) {
|
2283
|
+
debug4("Failed to add cached action step:", error);
|
2284
|
+
}
|
2285
|
+
}
|
2227
2286
|
return {
|
2228
2287
|
result: result.result,
|
2229
2288
|
metadata: metadata2
|
@@ -2248,10 +2307,39 @@ var PageAgent = class {
|
|
2248
2307
|
prompt: taskPrompt,
|
2249
2308
|
yamlWorkflow: yamlFlowStr
|
2250
2309
|
},
|
2251
|
-
matchedCache
|
2310
|
+
matchedCache,
|
2311
|
+
contextData
|
2312
|
+
// Pass context data for cache creation
|
2252
2313
|
);
|
2253
2314
|
}
|
2254
2315
|
const metadata = this.afterTaskRunning(executor);
|
2316
|
+
if (this.opts.enableCumulativeContext && this.contextStore) {
|
2317
|
+
try {
|
2318
|
+
const executionResult = this.analyzeExecutionResults(executor, originalPrompt);
|
2319
|
+
this.contextStore.addStep({
|
2320
|
+
type: "action",
|
2321
|
+
summary: `Action: ${processedPrompt}`,
|
2322
|
+
prompt: processedPrompt,
|
2323
|
+
executionResult
|
2324
|
+
});
|
2325
|
+
debug4("Added action step with execution result to context store:", {
|
2326
|
+
stepNumber: this.contextStore.getRecentSteps(1)[0]?.stepNumber,
|
2327
|
+
totalSteps: this.contextStore.getRecentSteps(100).length,
|
2328
|
+
executionResult
|
2329
|
+
});
|
2330
|
+
} catch (error) {
|
2331
|
+
debug4("Failed to analyze execution results, adding step without execution result:", error);
|
2332
|
+
try {
|
2333
|
+
this.contextStore.addStep({
|
2334
|
+
type: "action",
|
2335
|
+
summary: `Action: ${processedPrompt}`,
|
2336
|
+
prompt: processedPrompt
|
2337
|
+
});
|
2338
|
+
} catch (stepError) {
|
2339
|
+
debug4("Failed to add action step:", stepError);
|
2340
|
+
}
|
2341
|
+
}
|
2342
|
+
}
|
2255
2343
|
return {
|
2256
2344
|
result: output,
|
2257
2345
|
metadata
|
@@ -2436,23 +2524,40 @@ var PageAgent = class {
|
|
2436
2524
|
};
|
2437
2525
|
}
|
2438
2526
|
async aiAssert(assertion, msg, opt) {
|
2439
|
-
let
|
2527
|
+
let executionContext = "";
|
2440
2528
|
if (this.opts.enableCumulativeContext && this.contextStore) {
|
2441
2529
|
try {
|
2442
|
-
const
|
2443
|
-
|
2444
|
-
|
2445
|
-
|
2446
|
-
|
2447
|
-
|
2448
|
-
|
2449
|
-
|
2530
|
+
const recentSteps = this.contextStore.getRecentSteps(3);
|
2531
|
+
const stepsWithExecutionResults = recentSteps.filter((step) => step.executionResult);
|
2532
|
+
const storedData = this.contextStore.getAllData();
|
2533
|
+
if (stepsWithExecutionResults.length > 0) {
|
2534
|
+
const recentActions = stepsWithExecutionResults.map((step) => {
|
2535
|
+
const result = step.executionResult;
|
2536
|
+
return `- ${result.description}${result.success ? "" : " (FAILED)"}`;
|
2537
|
+
}).join("\n");
|
2538
|
+
executionContext = `
|
2539
|
+
|
2540
|
+
Recent actions performed:
|
2541
|
+
${recentActions}
|
2542
|
+
|
2543
|
+
This context may help verify the assertion.`;
|
2544
|
+
}
|
2545
|
+
if (storedData && Object.keys(storedData).length > 0) {
|
2546
|
+
executionContext += `
|
2547
|
+
|
2548
|
+
Available data for reference:
|
2549
|
+
${JSON.stringify(storedData, null, 2)}
|
2550
|
+
|
2551
|
+
Note: If the assertion references any data keys or natural language equivalents, consider the stored values when verifying.`;
|
2552
|
+
debug4("Available data for aiAssert:", {
|
2553
|
+
assertion,
|
2554
|
+
availableData: storedData
|
2450
2555
|
});
|
2451
2556
|
}
|
2452
2557
|
this.contextStore.addStep({
|
2453
2558
|
type: "assertion",
|
2454
|
-
summary: `Assertion: ${
|
2455
|
-
prompt:
|
2559
|
+
summary: `Assertion: ${assertion}`,
|
2560
|
+
prompt: assertion
|
2456
2561
|
});
|
2457
2562
|
debug4("Added assertion step to context store:", {
|
2458
2563
|
totalSteps: this.contextStore.getRecentSteps(100).length
|
@@ -2468,7 +2573,13 @@ var PageAgent = class {
|
|
2468
2573
|
} catch (e) {
|
2469
2574
|
}
|
2470
2575
|
}
|
2471
|
-
|
2576
|
+
let assertionWithContext = assertion;
|
2577
|
+
if (currentUrl) {
|
2578
|
+
assertionWithContext = `For the page at URL "${currentUrl}", ${assertion}`;
|
2579
|
+
}
|
2580
|
+
if (executionContext) {
|
2581
|
+
assertionWithContext += executionContext;
|
2582
|
+
}
|
2472
2583
|
const { output, executor } = await this.taskExecutor.assert(assertionWithContext);
|
2473
2584
|
const metadata = this.afterTaskRunning(executor, true);
|
2474
2585
|
if (output && opt?.keepRawResponse) {
|
@@ -2682,6 +2793,79 @@ ${errors}`);
|
|
2682
2793
|
async destroy() {
|
2683
2794
|
await this.page.destroy();
|
2684
2795
|
}
|
2796
|
+
/**
|
2797
|
+
* Analyze execution results from executor to generate meaningful descriptions
|
2798
|
+
*/
|
2799
|
+
analyzeExecutionResults(executor, originalPrompt) {
|
2800
|
+
const tasks = executor.tasks;
|
2801
|
+
const success = !executor.isInErrorState();
|
2802
|
+
if (!success) {
|
2803
|
+
const errorTask = executor.latestErrorTask();
|
2804
|
+
return {
|
2805
|
+
success: false,
|
2806
|
+
actionType: "error",
|
2807
|
+
description: `Failed to execute: ${originalPrompt}`,
|
2808
|
+
error: errorTask?.error
|
2809
|
+
};
|
2810
|
+
}
|
2811
|
+
const actionTasks = tasks.filter((t) => t.type === "Action" && t.status === "finished");
|
2812
|
+
const locateTasks = tasks.filter((t) => t.type === "Insight" && t.subType === "Locate");
|
2813
|
+
const lastAction = actionTasks[actionTasks.length - 1];
|
2814
|
+
const lastLocate = locateTasks[locateTasks.length - 1];
|
2815
|
+
if (!lastAction) {
|
2816
|
+
return {
|
2817
|
+
success: true,
|
2818
|
+
actionType: "unknown",
|
2819
|
+
description: `Completed: ${originalPrompt}`
|
2820
|
+
};
|
2821
|
+
}
|
2822
|
+
const actionType = lastAction.subType || "unknown";
|
2823
|
+
const elementInfo = this.extractElementInfo(lastLocate, lastAction);
|
2824
|
+
const description = this.generateActionDescription(actionType, lastAction.param, elementInfo);
|
2825
|
+
return {
|
2826
|
+
success: true,
|
2827
|
+
actionType,
|
2828
|
+
description,
|
2829
|
+
elementInfo,
|
2830
|
+
timing: lastAction.timing?.cost
|
2831
|
+
};
|
2832
|
+
}
|
2833
|
+
/**
|
2834
|
+
* Extract element information from locate task
|
2835
|
+
*/
|
2836
|
+
extractElementInfo(locateTask, _actionTask) {
|
2837
|
+
if (!locateTask?.output?.element)
|
2838
|
+
return void 0;
|
2839
|
+
const element = locateTask.output.element;
|
2840
|
+
return {
|
2841
|
+
type: element.attributes?.nodeType || "unknown",
|
2842
|
+
text: element.content || element.attributes?.placeholder || element.attributes?.title || "",
|
2843
|
+
location: `(${element.center[0]}, ${element.center[1]})`
|
2844
|
+
};
|
2845
|
+
}
|
2846
|
+
/**
|
2847
|
+
* Generate natural language description for actions
|
2848
|
+
*/
|
2849
|
+
generateActionDescription(actionType, param, elementInfo) {
|
2850
|
+
const elementDesc = elementInfo ? `'${elementInfo.text || elementInfo.type}' element` : "element";
|
2851
|
+
switch (actionType) {
|
2852
|
+
case "Tap":
|
2853
|
+
return `Clicked on ${elementDesc}`;
|
2854
|
+
case "Input":
|
2855
|
+
const inputValue = param?.value || "";
|
2856
|
+
return `Entered "${inputValue}" into ${elementDesc}`;
|
2857
|
+
case "KeyboardPress":
|
2858
|
+
return `Pressed ${param?.value || "key"}`;
|
2859
|
+
case "Scroll":
|
2860
|
+
return `Scrolled ${param?.direction || "on page"}`;
|
2861
|
+
case "Hover":
|
2862
|
+
return `Hovered over ${elementDesc}`;
|
2863
|
+
case "Drag":
|
2864
|
+
return `Dragged ${elementDesc}`;
|
2865
|
+
default:
|
2866
|
+
return `Performed ${actionType} action on ${elementDesc}`;
|
2867
|
+
}
|
2868
|
+
}
|
2685
2869
|
};
|
2686
2870
|
// Annotate the CommonJS export names for ESM import in node:
|
2687
2871
|
0 && (module.exports = {
|