@xiuchang-midscene/shared 2.0.2
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/README.md +9 -0
- package/dist/es/baseDB.mjs +109 -0
- package/dist/es/baseDB.mjs.bak +109 -0
- package/dist/es/build/copy-static.mjs +31 -0
- package/dist/es/build/copy-static.mjs.bak +31 -0
- package/dist/es/build/rspack-config.mjs +4 -0
- package/dist/es/build/rspack-config.mjs.bak +4 -0
- package/dist/es/cli/cli-runner.mjs +140 -0
- package/dist/es/cli/cli-runner.mjs.bak +140 -0
- package/dist/es/cli/index.mjs +2 -0
- package/dist/es/cli/index.mjs.bak +2 -0
- package/dist/es/common.mjs +37 -0
- package/dist/es/common.mjs.bak +37 -0
- package/dist/es/constants/example-code.mjs +223 -0
- package/dist/es/constants/example-code.mjs.bak +223 -0
- package/dist/es/constants/index.mjs +23 -0
- package/dist/es/constants/index.mjs.bak +23 -0
- package/dist/es/env/basic.mjs +6 -0
- package/dist/es/env/basic.mjs.bak +6 -0
- package/dist/es/env/constants.mjs +70 -0
- package/dist/es/env/constants.mjs.bak +70 -0
- package/dist/es/env/global-config-manager.mjs +94 -0
- package/dist/es/env/global-config-manager.mjs.bak +94 -0
- package/dist/es/env/helper.mjs +43 -0
- package/dist/es/env/helper.mjs.bak +43 -0
- package/dist/es/env/index.mjs +5 -0
- package/dist/es/env/index.mjs.bak +5 -0
- package/dist/es/env/init-debug.mjs +18 -0
- package/dist/es/env/init-debug.mjs.bak +18 -0
- package/dist/es/env/model-config-manager.mjs +79 -0
- package/dist/es/env/model-config-manager.mjs.bak +79 -0
- package/dist/es/env/parse-model-config.mjs +132 -0
- package/dist/es/env/parse-model-config.mjs.bak +132 -0
- package/dist/es/env/types.mjs +220 -0
- package/dist/es/env/types.mjs.bak +220 -0
- package/dist/es/env/utils.mjs +26 -0
- package/dist/es/env/utils.mjs.bak +26 -0
- package/dist/es/extractor/constants.mjs +2 -0
- package/dist/es/extractor/constants.mjs.bak +2 -0
- package/dist/es/extractor/debug.mjs +6 -0
- package/dist/es/extractor/debug.mjs.bak +6 -0
- package/dist/es/extractor/dom-util.mjs +92 -0
- package/dist/es/extractor/dom-util.mjs.bak +92 -0
- package/dist/es/extractor/index.mjs +5 -0
- package/dist/es/extractor/index.mjs.bak +5 -0
- package/dist/es/extractor/locator.mjs +250 -0
- package/dist/es/extractor/locator.mjs.bak +250 -0
- package/dist/es/extractor/tree.mjs +78 -0
- package/dist/es/extractor/tree.mjs.bak +78 -0
- package/dist/es/extractor/util.mjs +245 -0
- package/dist/es/extractor/util.mjs.bak +245 -0
- package/dist/es/extractor/web-extractor.mjs +303 -0
- package/dist/es/extractor/web-extractor.mjs.bak +303 -0
- package/dist/es/img/box-select.mjs +824 -0
- package/dist/es/img/box-select.mjs.bak +824 -0
- package/dist/es/img/canvas-fallback.mjs +238 -0
- package/dist/es/img/canvas-fallback.mjs.bak +238 -0
- package/dist/es/img/get-photon.mjs +45 -0
- package/dist/es/img/get-photon.mjs.bak +45 -0
- package/dist/es/img/get-sharp.mjs +11 -0
- package/dist/es/img/get-sharp.mjs.bak +11 -0
- package/dist/es/img/index.mjs +4 -0
- package/dist/es/img/index.mjs.bak +4 -0
- package/dist/es/img/info.mjs +29 -0
- package/dist/es/img/info.mjs.bak +29 -0
- package/dist/es/img/transform.mjs +295 -0
- package/dist/es/img/transform.mjs.bak +295 -0
- package/dist/es/index.mjs +4 -0
- package/dist/es/index.mjs.bak +4 -0
- package/dist/es/logger.mjs +64 -0
- package/dist/es/logger.mjs.bak +64 -0
- package/dist/es/mcp/base-server.mjs +281 -0
- package/dist/es/mcp/base-server.mjs.bak +281 -0
- package/dist/es/mcp/base-tools.mjs +91 -0
- package/dist/es/mcp/base-tools.mjs.bak +91 -0
- package/dist/es/mcp/chrome-path.mjs +35 -0
- package/dist/es/mcp/chrome-path.mjs.bak +35 -0
- package/dist/es/mcp/index.mjs +7 -0
- package/dist/es/mcp/index.mjs.bak +7 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
- package/dist/es/mcp/inject-report-html-plugin.mjs.bak +53 -0
- package/dist/es/mcp/launcher-helper.mjs +52 -0
- package/dist/es/mcp/launcher-helper.mjs.bak +52 -0
- package/dist/es/mcp/tool-generator.mjs +297 -0
- package/dist/es/mcp/tool-generator.mjs.bak +297 -0
- package/dist/es/mcp/types.mjs +3 -0
- package/dist/es/mcp/types.mjs.bak +3 -0
- package/dist/es/node/fs.mjs +44 -0
- package/dist/es/node/fs.mjs.bak +44 -0
- package/dist/es/node/index.mjs +2 -0
- package/dist/es/node/index.mjs.bak +2 -0
- package/dist/es/node/port.mjs +24 -0
- package/dist/es/node/port.mjs.bak +24 -0
- package/dist/es/oss/demo.mjs +30 -0
- package/dist/es/oss/demo.mjs.bak +30 -0
- package/dist/es/oss/index.mjs +90 -0
- package/dist/es/oss/index.mjs.bak +90 -0
- package/dist/es/polyfills/async-hooks.mjs +2 -0
- package/dist/es/polyfills/async-hooks.mjs.bak +2 -0
- package/dist/es/polyfills/index.mjs +1 -0
- package/dist/es/polyfills/index.mjs.bak +1 -0
- package/dist/es/types/index.mjs +3 -0
- package/dist/es/types/index.mjs.bak +3 -0
- package/dist/es/us-keyboard-layout.mjs +1414 -0
- package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
- package/dist/es/us-keyboard-layout.mjs.bak +1414 -0
- package/dist/es/utils.mjs +72 -0
- package/dist/es/utils.mjs.bak +72 -0
- package/dist/es/zod-schema-utils.mjs +54 -0
- package/dist/es/zod-schema-utils.mjs.bak +54 -0
- package/dist/lib/baseDB.js +149 -0
- package/dist/lib/baseDB.js.bak +149 -0
- package/dist/lib/build/copy-static.js +79 -0
- package/dist/lib/build/copy-static.js.bak +79 -0
- package/dist/lib/build/rspack-config.js +38 -0
- package/dist/lib/build/rspack-config.js.bak +38 -0
- package/dist/lib/cli/cli-runner.js +196 -0
- package/dist/lib/cli/cli-runner.js.bak +196 -0
- package/dist/lib/cli/index.js +48 -0
- package/dist/lib/cli/index.js.bak +48 -0
- package/dist/lib/common.js +93 -0
- package/dist/lib/common.js.bak +93 -0
- package/dist/lib/constants/example-code.js +260 -0
- package/dist/lib/constants/example-code.js.bak +260 -0
- package/dist/lib/constants/index.js +96 -0
- package/dist/lib/constants/index.js.bak +96 -0
- package/dist/lib/env/basic.js +40 -0
- package/dist/lib/env/basic.js.bak +40 -0
- package/dist/lib/env/constants.js +113 -0
- package/dist/lib/env/constants.js.bak +113 -0
- package/dist/lib/env/global-config-manager.js +128 -0
- package/dist/lib/env/global-config-manager.js.bak +128 -0
- package/dist/lib/env/helper.js +80 -0
- package/dist/lib/env/helper.js.bak +80 -0
- package/dist/lib/env/index.js +90 -0
- package/dist/lib/env/index.js.bak +90 -0
- package/dist/lib/env/init-debug.js +52 -0
- package/dist/lib/env/init-debug.js.bak +52 -0
- package/dist/lib/env/model-config-manager.js +113 -0
- package/dist/lib/env/model-config-manager.js.bak +113 -0
- package/dist/lib/env/parse-model-config.js +178 -0
- package/dist/lib/env/parse-model-config.js.bak +178 -0
- package/dist/lib/env/types.js +554 -0
- package/dist/lib/env/types.js.bak +554 -0
- package/dist/lib/env/utils.js +72 -0
- package/dist/lib/env/utils.js.bak +72 -0
- package/dist/lib/extractor/constants.js +42 -0
- package/dist/lib/extractor/constants.js.bak +42 -0
- package/dist/lib/extractor/debug.js +12 -0
- package/dist/lib/extractor/debug.js.bak +12 -0
- package/dist/lib/extractor/dom-util.js +153 -0
- package/dist/lib/extractor/dom-util.js.bak +153 -0
- package/dist/lib/extractor/index.js +81 -0
- package/dist/lib/extractor/index.js.bak +81 -0
- package/dist/lib/extractor/locator.js +296 -0
- package/dist/lib/extractor/locator.js.bak +296 -0
- package/dist/lib/extractor/tree.js +124 -0
- package/dist/lib/extractor/tree.js.bak +124 -0
- package/dist/lib/extractor/util.js +336 -0
- package/dist/lib/extractor/util.js.bak +336 -0
- package/dist/lib/extractor/web-extractor.js +349 -0
- package/dist/lib/extractor/web-extractor.js.bak +349 -0
- package/dist/lib/img/box-select.js +875 -0
- package/dist/lib/img/box-select.js.bak +875 -0
- package/dist/lib/img/canvas-fallback.js +305 -0
- package/dist/lib/img/canvas-fallback.js.bak +305 -0
- package/dist/lib/img/get-photon.js +82 -0
- package/dist/lib/img/get-photon.js.bak +82 -0
- package/dist/lib/img/get-sharp.js +45 -0
- package/dist/lib/img/get-sharp.js.bak +45 -0
- package/dist/lib/img/index.js +95 -0
- package/dist/lib/img/index.js.bak +95 -0
- package/dist/lib/img/info.js +83 -0
- package/dist/lib/img/info.js.bak +83 -0
- package/dist/lib/img/transform.js +387 -0
- package/dist/lib/img/transform.js.bak +387 -0
- package/dist/lib/index.js +47 -0
- package/dist/lib/index.js.bak +47 -0
- package/dist/lib/logger.js +114 -0
- package/dist/lib/logger.js.bak +114 -0
- package/dist/lib/mcp/base-server.js +331 -0
- package/dist/lib/mcp/base-server.js.bak +331 -0
- package/dist/lib/mcp/base-tools.js +125 -0
- package/dist/lib/mcp/base-tools.js.bak +125 -0
- package/dist/lib/mcp/chrome-path.js +72 -0
- package/dist/lib/mcp/chrome-path.js.bak +72 -0
- package/dist/lib/mcp/index.js +100 -0
- package/dist/lib/mcp/index.js.bak +100 -0
- package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
- package/dist/lib/mcp/inject-report-html-plugin.js.bak +98 -0
- package/dist/lib/mcp/launcher-helper.js +86 -0
- package/dist/lib/mcp/launcher-helper.js.bak +86 -0
- package/dist/lib/mcp/tool-generator.js +334 -0
- package/dist/lib/mcp/tool-generator.js.bak +334 -0
- package/dist/lib/mcp/types.js +40 -0
- package/dist/lib/mcp/types.js.bak +40 -0
- package/dist/lib/node/fs.js +97 -0
- package/dist/lib/node/fs.js.bak +97 -0
- package/dist/lib/node/index.js +65 -0
- package/dist/lib/node/index.js.bak +65 -0
- package/dist/lib/node/port.js +61 -0
- package/dist/lib/node/port.js.bak +61 -0
- package/dist/lib/oss/demo.js +36 -0
- package/dist/lib/oss/demo.js.bak +36 -0
- package/dist/lib/oss/index.js +138 -0
- package/dist/lib/oss/index.js.bak +138 -0
- package/dist/lib/polyfills/async-hooks.js +36 -0
- package/dist/lib/polyfills/async-hooks.js.bak +36 -0
- package/dist/lib/polyfills/index.js +58 -0
- package/dist/lib/polyfills/index.js.bak +58 -0
- package/dist/lib/types/index.js +37 -0
- package/dist/lib/types/index.js.bak +37 -0
- package/dist/lib/us-keyboard-layout.js +1457 -0
- package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
- package/dist/lib/us-keyboard-layout.js.bak +1457 -0
- package/dist/lib/utils.js +148 -0
- package/dist/lib/utils.js.bak +148 -0
- package/dist/lib/zod-schema-utils.js +97 -0
- package/dist/lib/zod-schema-utils.js.bak +97 -0
- package/dist/types/baseDB.d.ts +25 -0
- package/dist/types/baseDB.d.ts.bak +25 -0
- package/dist/types/build/copy-static.d.ts +31 -0
- package/dist/types/build/copy-static.d.ts.bak +31 -0
- package/dist/types/build/rspack-config.d.ts +8 -0
- package/dist/types/build/rspack-config.d.ts.bak +8 -0
- package/dist/types/cli/cli-runner.d.ts +14 -0
- package/dist/types/cli/cli-runner.d.ts.bak +14 -0
- package/dist/types/cli/index.d.ts +2 -0
- package/dist/types/cli/index.d.ts.bak +2 -0
- package/dist/types/common.d.ts +12 -0
- package/dist/types/common.d.ts.bak +12 -0
- package/dist/types/constants/example-code.d.ts +2 -0
- package/dist/types/constants/example-code.d.ts.bak +2 -0
- package/dist/types/constants/index.d.ts +21 -0
- package/dist/types/constants/index.d.ts.bak +21 -0
- package/dist/types/env/basic.d.ts +6 -0
- package/dist/types/env/basic.d.ts.bak +6 -0
- package/dist/types/env/constants.d.ts +40 -0
- package/dist/types/env/constants.d.ts.bak +40 -0
- package/dist/types/env/global-config-manager.d.ts +32 -0
- package/dist/types/env/global-config-manager.d.ts.bak +32 -0
- package/dist/types/env/helper.d.ts +4 -0
- package/dist/types/env/helper.d.ts.bak +4 -0
- package/dist/types/env/index.d.ts +4 -0
- package/dist/types/env/index.d.ts.bak +4 -0
- package/dist/types/env/init-debug.d.ts +1 -0
- package/dist/types/env/init-debug.d.ts.bak +1 -0
- package/dist/types/env/model-config-manager.d.ts +25 -0
- package/dist/types/env/model-config-manager.d.ts.bak +25 -0
- package/dist/types/env/parse-model-config.d.ts +31 -0
- package/dist/types/env/parse-model-config.d.ts.bak +31 -0
- package/dist/types/env/types.d.ts +318 -0
- package/dist/types/env/types.d.ts.bak +318 -0
- package/dist/types/env/utils.d.ts +38 -0
- package/dist/types/env/utils.d.ts.bak +38 -0
- package/dist/types/extractor/constants.d.ts +1 -0
- package/dist/types/extractor/constants.d.ts.bak +1 -0
- package/dist/types/extractor/debug.d.ts +1 -0
- package/dist/types/extractor/debug.d.ts.bak +1 -0
- package/dist/types/extractor/dom-util.d.ts +56 -0
- package/dist/types/extractor/dom-util.d.ts.bak +56 -0
- package/dist/types/extractor/index.d.ts +32 -0
- package/dist/types/extractor/index.d.ts.bak +32 -0
- package/dist/types/extractor/locator.d.ts +9 -0
- package/dist/types/extractor/locator.d.ts.bak +9 -0
- package/dist/types/extractor/tree.d.ts +6 -0
- package/dist/types/extractor/tree.d.ts.bak +6 -0
- package/dist/types/extractor/util.d.ts +47 -0
- package/dist/types/extractor/util.d.ts.bak +47 -0
- package/dist/types/extractor/web-extractor.d.ts +19 -0
- package/dist/types/extractor/web-extractor.d.ts.bak +19 -0
- package/dist/types/img/box-select.d.ts +26 -0
- package/dist/types/img/box-select.d.ts.bak +26 -0
- package/dist/types/img/canvas-fallback.d.ts +105 -0
- package/dist/types/img/canvas-fallback.d.ts.bak +105 -0
- package/dist/types/img/get-photon.d.ts +19 -0
- package/dist/types/img/get-photon.d.ts.bak +19 -0
- package/dist/types/img/get-sharp.d.ts +3 -0
- package/dist/types/img/get-sharp.d.ts.bak +3 -0
- package/dist/types/img/index.d.ts +3 -0
- package/dist/types/img/index.d.ts.bak +3 -0
- package/dist/types/img/info.d.ts +29 -0
- package/dist/types/img/info.d.ts.bak +29 -0
- package/dist/types/img/transform.d.ts +107 -0
- package/dist/types/img/transform.d.ts.bak +107 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.bak +4 -0
- package/dist/types/logger.d.ts +5 -0
- package/dist/types/logger.d.ts.bak +5 -0
- package/dist/types/mcp/base-server.d.ts +93 -0
- package/dist/types/mcp/base-server.d.ts.bak +93 -0
- package/dist/types/mcp/base-tools.d.ts +79 -0
- package/dist/types/mcp/base-tools.d.ts.bak +79 -0
- package/dist/types/mcp/chrome-path.d.ts +2 -0
- package/dist/types/mcp/chrome-path.d.ts.bak +2 -0
- package/dist/types/mcp/index.d.ts +7 -0
- package/dist/types/mcp/index.d.ts.bak +7 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
- package/dist/types/mcp/inject-report-html-plugin.d.ts.bak +18 -0
- package/dist/types/mcp/launcher-helper.d.ts +94 -0
- package/dist/types/mcp/launcher-helper.d.ts.bak +94 -0
- package/dist/types/mcp/tool-generator.d.ts +10 -0
- package/dist/types/mcp/tool-generator.d.ts.bak +10 -0
- package/dist/types/mcp/types.d.ts +103 -0
- package/dist/types/mcp/types.d.ts.bak +103 -0
- package/dist/types/node/fs.d.ts +15 -0
- package/dist/types/node/fs.d.ts.bak +15 -0
- package/dist/types/node/index.d.ts +2 -0
- package/dist/types/node/index.d.ts.bak +2 -0
- package/dist/types/node/port.d.ts +8 -0
- package/dist/types/node/port.d.ts.bak +8 -0
- package/dist/types/oss/demo.d.ts +1 -0
- package/dist/types/oss/demo.d.ts.bak +1 -0
- package/dist/types/oss/index.d.ts +34 -0
- package/dist/types/oss/index.d.ts.bak +34 -0
- package/dist/types/polyfills/async-hooks.d.ts +6 -0
- package/dist/types/polyfills/async-hooks.d.ts.bak +6 -0
- package/dist/types/polyfills/index.d.ts +4 -0
- package/dist/types/polyfills/index.d.ts.bak +4 -0
- package/dist/types/types/index.d.ts +34 -0
- package/dist/types/types/index.d.ts.bak +34 -0
- package/dist/types/us-keyboard-layout.d.ts +32 -0
- package/dist/types/us-keyboard-layout.d.ts.bak +32 -0
- package/dist/types/utils.d.ts +34 -0
- package/dist/types/utils.d.ts.bak +34 -0
- package/dist/types/zod-schema-utils.d.ts +23 -0
- package/dist/types/zod-schema-utils.d.ts.bak +23 -0
- package/package.json +132 -0
- package/src/baseDB.ts +158 -0
- package/src/build/copy-static.ts +68 -0
- package/src/build/rspack-config.ts +12 -0
- package/src/cli/cli-runner.ts +224 -0
- package/src/cli/index.ts +8 -0
- package/src/common.ts +67 -0
- package/src/constants/example-code.ts +223 -0
- package/src/constants/index.ts +29 -0
- package/src/env/basic.ts +12 -0
- package/src/env/constants.ts +234 -0
- package/src/env/global-config-manager.ts +191 -0
- package/src/env/helper.ts +58 -0
- package/src/env/index.ts +4 -0
- package/src/env/init-debug.ts +34 -0
- package/src/env/model-config-manager.ts +149 -0
- package/src/env/parse-model-config.ts +294 -0
- package/src/env/types.ts +547 -0
- package/src/env/utils.ts +89 -0
- package/src/extractor/constants.ts +5 -0
- package/src/extractor/debug.ts +10 -0
- package/src/extractor/dom-util.ts +226 -0
- package/src/extractor/index.ts +48 -0
- package/src/extractor/locator.ts +469 -0
- package/src/extractor/tree.ts +179 -0
- package/src/extractor/util.ts +482 -0
- package/src/extractor/web-extractor.ts +481 -0
- package/src/img/box-select.ts +588 -0
- package/src/img/canvas-fallback.ts +393 -0
- package/src/img/get-photon.ts +108 -0
- package/src/img/get-sharp.ts +18 -0
- package/src/img/index.ts +26 -0
- package/src/img/info.ts +75 -0
- package/src/img/transform.ts +594 -0
- package/src/index.ts +8 -0
- package/src/logger.ts +96 -0
- package/src/mcp/base-server.ts +502 -0
- package/src/mcp/base-tools.ts +185 -0
- package/src/mcp/chrome-path.ts +48 -0
- package/src/mcp/index.ts +7 -0
- package/src/mcp/inject-report-html-plugin.ts +119 -0
- package/src/mcp/launcher-helper.ts +200 -0
- package/src/mcp/tool-generator.ts +429 -0
- package/src/mcp/types.ts +112 -0
- package/src/node/fs.ts +84 -0
- package/src/node/index.ts +2 -0
- package/src/node/port.ts +37 -0
- package/src/oss/demo.ts +61 -0
- package/src/oss/index.ts +187 -0
- package/src/polyfills/async-hooks.ts +6 -0
- package/src/polyfills/index.ts +4 -0
- package/src/types/index.ts +52 -0
- package/src/us-keyboard-layout.ts +723 -0
- package/src/utils.ts +149 -0
- package/src/zod-schema-utils.ts +133 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
export const PLAYWRIGHT_EXAMPLE_CODE = `
|
|
2
|
+
// Reference the following code to generate Midscene test cases
|
|
3
|
+
// The following is test code for Midscene AI, for reference
|
|
4
|
+
// The following is Playwright syntax, you can use Playwright to assist in test generation
|
|
5
|
+
IMPORTANT: Follow these exact type signatures for AI functions:
|
|
6
|
+
|
|
7
|
+
// Type signatures for AI functions:
|
|
8
|
+
aiAct(prompt: string, options?: { cacheable?: boolean, deepThink?: 'unset' | true | false }): Promise<void>
|
|
9
|
+
aiInput(text: string, locate: string, options?: { deepLocate?: boolean, xpath?: string, cacheable?: boolean }): Promise<void>
|
|
10
|
+
aiTap(locate: string, options?: { deepLocate?: boolean, xpath?: string, cacheable?: boolean }): Promise<void>
|
|
11
|
+
aiHover(locate: string, options?: { deepLocate?: boolean, xpath?: string, cacheable?: boolean }): Promise<void>
|
|
12
|
+
aiDoubleClick(locate: string, options?: { deepLocate?: boolean, xpath?: string, cacheable?: boolean }): Promise<void>
|
|
13
|
+
aiKeyboardPress(key: string, locate?: string, options?: { deepLocate?: boolean, xpath?: string, cacheable?: boolean }): Promise<void>
|
|
14
|
+
aiScroll(locate: string | undefined, options: {
|
|
15
|
+
direction?: 'up' | 'down' | 'left' | 'right',
|
|
16
|
+
scrollType?: 'singleAction' | 'scrollToBottom' | 'scrollToTop' | 'scrollToRight' | 'scrollToLeft',
|
|
17
|
+
distance?: number | null,
|
|
18
|
+
deepLocate?: boolean,
|
|
19
|
+
xpath?: string,
|
|
20
|
+
cacheable?: boolean
|
|
21
|
+
}): Promise<void>
|
|
22
|
+
aiAssert(assertion: string, options?: { errorMessage?: string }): Promise<void>
|
|
23
|
+
aiWaitFor(prompt: string, options?: { timeout?: number }): Promise<void>
|
|
24
|
+
aiQuery<T>(queryObject: Record<string, string>): Promise<T> // Extracts data from page based on descriptions
|
|
25
|
+
|
|
26
|
+
// examples:
|
|
27
|
+
// Reference the following code to generate Midscene test cases
|
|
28
|
+
// The following is test code for Midscene AI, for reference
|
|
29
|
+
// The following is Playwright syntax, you can use Playwright to assist in test generation
|
|
30
|
+
import { test as base } from '@playwright/test';
|
|
31
|
+
import type { PlayWrightAiFixtureType } from '@midscene/web/playwright';
|
|
32
|
+
import { PlaywrightAiFixture } from '@midscene/web/playwright';
|
|
33
|
+
|
|
34
|
+
const test = base.extend<PlayWrightAiFixtureType>(PlaywrightAiFixture({
|
|
35
|
+
waitForNetworkIdleTimeout: 2000, // optional, the timeout for waiting for network idle between each action, default is 2000ms
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
test.beforeEach(async ({ page }) => {
|
|
40
|
+
await page.goto('https://www.xxx.com/');
|
|
41
|
+
await page.setViewportSize({ width: 1920, height: 1080 });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('ai shop', async ({
|
|
45
|
+
aiAct,
|
|
46
|
+
aiInput,
|
|
47
|
+
aiAssert,
|
|
48
|
+
aiQuery,
|
|
49
|
+
aiKeyboardPress,
|
|
50
|
+
aiHover,
|
|
51
|
+
aiTap,
|
|
52
|
+
aiWaitFor,
|
|
53
|
+
agentForPage,
|
|
54
|
+
page,
|
|
55
|
+
}) => {
|
|
56
|
+
// login
|
|
57
|
+
await aiAssert('The page shows the login interface');
|
|
58
|
+
await aiInput('user_name', 'in user name input');
|
|
59
|
+
await aiInput('password', 'in password input');
|
|
60
|
+
await aiKeyboardPress('Enter', 'Login Button');
|
|
61
|
+
|
|
62
|
+
// check the login success
|
|
63
|
+
await aiWaitFor('The page shows that the loading is complete');
|
|
64
|
+
await aiAssert('The current page shows the product detail page');
|
|
65
|
+
|
|
66
|
+
// check the product info
|
|
67
|
+
const dataA = await aiQuery({
|
|
68
|
+
userInfo: 'User information in the format {name: string}',
|
|
69
|
+
theFirstProductInfo: 'The first product info in the format {name: string, price: number}',
|
|
70
|
+
});
|
|
71
|
+
expect(dataA.theFirstProductInfo.name).toBe('xxx');
|
|
72
|
+
expect(dataA.theFirstProductInfo.price).toBe(100);
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
// add to cart
|
|
76
|
+
await aiTap('click add to cart button');
|
|
77
|
+
|
|
78
|
+
await aiTap('click right top cart icon');
|
|
79
|
+
await aiAssert('The cart icon shows the number 1');
|
|
80
|
+
});
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
export const YAML_EXAMPLE_CODE = `
|
|
84
|
+
1. Format:
|
|
85
|
+
|
|
86
|
+
web:
|
|
87
|
+
url: "starting_url"
|
|
88
|
+
viewportWidth: 1280
|
|
89
|
+
viewportHeight: 960
|
|
90
|
+
|
|
91
|
+
tasks:
|
|
92
|
+
- name: "descriptive task name"
|
|
93
|
+
flow:
|
|
94
|
+
- aiTap: "element description"
|
|
95
|
+
- aiInput: 'text value'
|
|
96
|
+
locate: 'input field description'
|
|
97
|
+
- aiScroll:
|
|
98
|
+
direction: down/up
|
|
99
|
+
scrollType: scrollToBottom/scrollToTop/singleAction
|
|
100
|
+
- aiAssert: "expected state"
|
|
101
|
+
- sleep: milliseconds
|
|
102
|
+
|
|
103
|
+
2. Action Types:
|
|
104
|
+
- aiTap: for clicks (natural language targeting)
|
|
105
|
+
- aiInput: for text input with 'locate' field
|
|
106
|
+
- aiScroll: with direction and scrollType
|
|
107
|
+
- aiAssert: for validations
|
|
108
|
+
- sleep: for delays (milliseconds)
|
|
109
|
+
|
|
110
|
+
3. Best Practices:
|
|
111
|
+
- Group related actions into logical tasks
|
|
112
|
+
- Use natural language descriptions
|
|
113
|
+
- Add deepLocate: true for complex interactions
|
|
114
|
+
- Keep task names concise but descriptive
|
|
115
|
+
|
|
116
|
+
4. CRITICAL - YAML Indentation Rules:
|
|
117
|
+
- For actions with additional parameters (aiScroll, aiInput, aiKeyboardPress), the parameters must be SIBLING keys, NOT nested children
|
|
118
|
+
- Parameters like direction, scrollType, locate must align with the action key, not indented further
|
|
119
|
+
- CORRECT indentation example:
|
|
120
|
+
- aiScroll:
|
|
121
|
+
direction: down
|
|
122
|
+
scrollType: singleAction
|
|
123
|
+
- WRONG indentation (DO NOT do this):
|
|
124
|
+
- aiScroll:
|
|
125
|
+
direction: down
|
|
126
|
+
scrollType: singleAction
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
YAML type
|
|
131
|
+
tasks:
|
|
132
|
+
- name: <name>
|
|
133
|
+
continueOnError: <boolean> # Optional, whether to continue to the next task on error, defaults to false.
|
|
134
|
+
flow:
|
|
135
|
+
# Auto Planning (.ai)
|
|
136
|
+
# ----------------
|
|
137
|
+
|
|
138
|
+
# Perform an interaction. \`ai\` is a shorthand for \`aiAct\`.
|
|
139
|
+
- ai: <prompt>
|
|
140
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
141
|
+
|
|
142
|
+
# This usage is the same as \`ai\`.
|
|
143
|
+
- aiAct: <prompt>
|
|
144
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
145
|
+
|
|
146
|
+
# Instant Action (.aiTap, .aiDoubleClick, .aiHover, .aiInput, .aiKeyboardPress, .aiScroll)
|
|
147
|
+
# ----------------
|
|
148
|
+
|
|
149
|
+
# Tap an element described by a prompt.
|
|
150
|
+
- aiTap: <prompt>
|
|
151
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
152
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
153
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
154
|
+
|
|
155
|
+
# Double click an element described by a prompt.
|
|
156
|
+
- aiDoubleClick: <prompt>
|
|
157
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
158
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
159
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
160
|
+
|
|
161
|
+
# Hover over an element described by a prompt.
|
|
162
|
+
- aiHover: <prompt>
|
|
163
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
164
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
165
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
166
|
+
|
|
167
|
+
# Input text into an element described by a prompt.
|
|
168
|
+
- aiInput: <final text content of the input>
|
|
169
|
+
locate: <prompt>
|
|
170
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
171
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
172
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
173
|
+
|
|
174
|
+
# Press a key (e.g., Enter, Tab, Escape) on an element described by a prompt.
|
|
175
|
+
- aiKeyboardPress: <key>
|
|
176
|
+
locate: <prompt>
|
|
177
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
178
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
179
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
180
|
+
|
|
181
|
+
# Scroll globally or on an element described by a prompt.
|
|
182
|
+
- aiScroll:
|
|
183
|
+
direction: 'up' # or 'down' | 'left' | 'right'
|
|
184
|
+
scrollType: 'singleAction' # or 'scrollToTop' | 'scrollToBottom' | 'scrollToLeft' | 'scrollToRight'
|
|
185
|
+
distance: <number> # Optional, the scroll distance in pixels.
|
|
186
|
+
locate: <prompt> # Optional, the element to scroll on.
|
|
187
|
+
deepLocate: <boolean> # Optional, whether to use deepLocate to precisely locate the element. Defaults to False.
|
|
188
|
+
xpath: <xpath> # Optional, the xpath of the target element for the operation. If provided, Midscene will prioritize this xpath to find the element before using the cache and the AI model. Defaults to empty.
|
|
189
|
+
cacheable: <boolean> # Optional, whether to cache the result of this API call when the [caching feature](./caching.mdx) is enabled. Defaults to True.
|
|
190
|
+
|
|
191
|
+
# Record the current screenshot with a description in the report file.
|
|
192
|
+
- recordToReport: <title> # Optional, the title of the screenshot. If not provided, the title will be 'untitled'.
|
|
193
|
+
content: <content> # Optional, the description of the screenshot.
|
|
194
|
+
|
|
195
|
+
# Data Extraction
|
|
196
|
+
# ----------------
|
|
197
|
+
|
|
198
|
+
# Perform a query that returns a JSON object.
|
|
199
|
+
- aiQuery: <prompt> # Remember to describe the format of the result in the prompt.
|
|
200
|
+
name: <name> # The key for the query result in the JSON output.
|
|
201
|
+
|
|
202
|
+
# More APIs
|
|
203
|
+
# ----------------
|
|
204
|
+
|
|
205
|
+
# Wait for a condition to be met, with a timeout (in ms, optional, defaults to 30000).
|
|
206
|
+
- aiWaitFor: <prompt>
|
|
207
|
+
timeout: <ms>
|
|
208
|
+
|
|
209
|
+
# Perform an assertion.
|
|
210
|
+
- aiAssert: <prompt>
|
|
211
|
+
errorMessage: <error-message> # Optional, the error message to print if the assertion fails.
|
|
212
|
+
|
|
213
|
+
# Wait for a specified amount of time.
|
|
214
|
+
- sleep: <ms>
|
|
215
|
+
|
|
216
|
+
# Execute a piece of JavaScript code in the web page context.
|
|
217
|
+
- javascript: <javascript>
|
|
218
|
+
name: <name> # Optional, assign a name to the return value, which will be used as a key in the JSON output.
|
|
219
|
+
|
|
220
|
+
- name: <name>
|
|
221
|
+
flow:
|
|
222
|
+
# ...
|
|
223
|
+
`;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const TEXT_SIZE_THRESHOLD = 9;
|
|
2
|
+
|
|
3
|
+
export const TEXT_MAX_SIZE = 40;
|
|
4
|
+
|
|
5
|
+
export const CONTAINER_MINI_HEIGHT = 3;
|
|
6
|
+
export const CONTAINER_MINI_WIDTH = 3;
|
|
7
|
+
|
|
8
|
+
export enum NodeType {
|
|
9
|
+
CONTAINER = 'CONTAINER Node',
|
|
10
|
+
FORM_ITEM = 'FORM_ITEM Node',
|
|
11
|
+
BUTTON = 'BUTTON Node',
|
|
12
|
+
A = 'Anchor Node',
|
|
13
|
+
IMG = 'IMG Node',
|
|
14
|
+
TEXT = 'TEXT Node',
|
|
15
|
+
POSITION = 'POSITION Node',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const PLAYGROUND_SERVER_PORT = 5800;
|
|
19
|
+
export const SCRCPY_SERVER_PORT = 5700;
|
|
20
|
+
|
|
21
|
+
// WebDriver constants
|
|
22
|
+
export const WEBDRIVER_ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf';
|
|
23
|
+
export const DEFAULT_WDA_PORT = 8100;
|
|
24
|
+
|
|
25
|
+
export const DEFAULT_WAIT_FOR_NAVIGATION_TIMEOUT = 5000;
|
|
26
|
+
export const DEFAULT_WAIT_FOR_NETWORK_IDLE_TIMEOUT = 2000;
|
|
27
|
+
export const DEFAULT_WAIT_FOR_NETWORK_IDLE_CONCURRENCY = 2;
|
|
28
|
+
|
|
29
|
+
export { PLAYWRIGHT_EXAMPLE_CODE, YAML_EXAMPLE_CODE } from './example-code';
|
package/src/env/basic.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BASIC_ENV_KEYS } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* get basic env value from process.env
|
|
5
|
+
* use a single file to avoid circular dependency
|
|
6
|
+
*/
|
|
7
|
+
export const getBasicEnvValue = (key: (typeof BASIC_ENV_KEYS)[number]) => {
|
|
8
|
+
if (!BASIC_ENV_KEYS.includes(key)) {
|
|
9
|
+
throw new Error(`getBasicEnvValue with key ${key} is not supported.`);
|
|
10
|
+
}
|
|
11
|
+
return process.env[key];
|
|
12
|
+
};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MIDSCENE_INSIGHT_MODEL_API_KEY,
|
|
3
|
+
MIDSCENE_INSIGHT_MODEL_BASE_URL,
|
|
4
|
+
MIDSCENE_INSIGHT_MODEL_EXTRA_BODY_JSON,
|
|
5
|
+
MIDSCENE_INSIGHT_MODEL_FAMILY,
|
|
6
|
+
MIDSCENE_INSIGHT_MODEL_HTTP_PROXY,
|
|
7
|
+
MIDSCENE_INSIGHT_MODEL_INIT_CONFIG_JSON,
|
|
8
|
+
MIDSCENE_INSIGHT_MODEL_NAME,
|
|
9
|
+
MIDSCENE_INSIGHT_MODEL_REASONING_BUDGET,
|
|
10
|
+
MIDSCENE_INSIGHT_MODEL_REASONING_EFFORT,
|
|
11
|
+
MIDSCENE_INSIGHT_MODEL_REASONING_ENABLED,
|
|
12
|
+
MIDSCENE_INSIGHT_MODEL_RETRY_COUNT,
|
|
13
|
+
MIDSCENE_INSIGHT_MODEL_RETRY_INTERVAL,
|
|
14
|
+
MIDSCENE_INSIGHT_MODEL_SOCKS_PROXY,
|
|
15
|
+
MIDSCENE_INSIGHT_MODEL_TEMPERATURE,
|
|
16
|
+
MIDSCENE_INSIGHT_MODEL_TIMEOUT,
|
|
17
|
+
MIDSCENE_MODEL_API_KEY,
|
|
18
|
+
MIDSCENE_MODEL_BASE_URL,
|
|
19
|
+
MIDSCENE_MODEL_EXTRA_BODY_JSON,
|
|
20
|
+
MIDSCENE_MODEL_FAMILY,
|
|
21
|
+
MIDSCENE_MODEL_HTTP_PROXY,
|
|
22
|
+
MIDSCENE_MODEL_INIT_CONFIG_JSON,
|
|
23
|
+
MIDSCENE_MODEL_NAME,
|
|
24
|
+
MIDSCENE_MODEL_REASONING_BUDGET,
|
|
25
|
+
MIDSCENE_MODEL_REASONING_EFFORT,
|
|
26
|
+
MIDSCENE_MODEL_REASONING_ENABLED,
|
|
27
|
+
MIDSCENE_MODEL_RETRY_COUNT,
|
|
28
|
+
MIDSCENE_MODEL_RETRY_INTERVAL,
|
|
29
|
+
MIDSCENE_MODEL_SOCKS_PROXY,
|
|
30
|
+
MIDSCENE_MODEL_TEMPERATURE,
|
|
31
|
+
MIDSCENE_MODEL_TIMEOUT,
|
|
32
|
+
MIDSCENE_OPENAI_HTTP_PROXY,
|
|
33
|
+
MIDSCENE_OPENAI_INIT_CONFIG_JSON,
|
|
34
|
+
MIDSCENE_OPENAI_SOCKS_PROXY,
|
|
35
|
+
MIDSCENE_PLANNING_MODEL_API_KEY,
|
|
36
|
+
MIDSCENE_PLANNING_MODEL_BASE_URL,
|
|
37
|
+
MIDSCENE_PLANNING_MODEL_EXTRA_BODY_JSON,
|
|
38
|
+
MIDSCENE_PLANNING_MODEL_FAMILY,
|
|
39
|
+
MIDSCENE_PLANNING_MODEL_HTTP_PROXY,
|
|
40
|
+
MIDSCENE_PLANNING_MODEL_INIT_CONFIG_JSON,
|
|
41
|
+
MIDSCENE_PLANNING_MODEL_NAME,
|
|
42
|
+
MIDSCENE_PLANNING_MODEL_REASONING_BUDGET,
|
|
43
|
+
MIDSCENE_PLANNING_MODEL_REASONING_EFFORT,
|
|
44
|
+
MIDSCENE_PLANNING_MODEL_REASONING_ENABLED,
|
|
45
|
+
MIDSCENE_PLANNING_MODEL_RETRY_COUNT,
|
|
46
|
+
MIDSCENE_PLANNING_MODEL_RETRY_INTERVAL,
|
|
47
|
+
MIDSCENE_PLANNING_MODEL_SOCKS_PROXY,
|
|
48
|
+
MIDSCENE_PLANNING_MODEL_TEMPERATURE,
|
|
49
|
+
MIDSCENE_PLANNING_MODEL_TIMEOUT,
|
|
50
|
+
OPENAI_API_KEY,
|
|
51
|
+
OPENAI_BASE_URL,
|
|
52
|
+
} from './types';
|
|
53
|
+
|
|
54
|
+
interface IModelConfigKeys {
|
|
55
|
+
modelName: string;
|
|
56
|
+
/**
|
|
57
|
+
* proxy
|
|
58
|
+
*/
|
|
59
|
+
socksProxy: string;
|
|
60
|
+
httpProxy: string;
|
|
61
|
+
/**
|
|
62
|
+
* OpenAI
|
|
63
|
+
*/
|
|
64
|
+
openaiBaseURL: string;
|
|
65
|
+
openaiApiKey: string;
|
|
66
|
+
openaiExtraConfig: string;
|
|
67
|
+
extraBody: string;
|
|
68
|
+
/**
|
|
69
|
+
* Extra
|
|
70
|
+
*/
|
|
71
|
+
modelFamily: string;
|
|
72
|
+
/**
|
|
73
|
+
* Timeout
|
|
74
|
+
*/
|
|
75
|
+
timeout: string;
|
|
76
|
+
/**
|
|
77
|
+
* Temperature
|
|
78
|
+
*/
|
|
79
|
+
temperature: string;
|
|
80
|
+
/**
|
|
81
|
+
* Retry
|
|
82
|
+
*/
|
|
83
|
+
retryCount: string;
|
|
84
|
+
retryInterval: string;
|
|
85
|
+
reasoningEffort: string;
|
|
86
|
+
reasoningEnabled: string;
|
|
87
|
+
reasoningBudget: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const INSIGHT_MODEL_CONFIG_KEYS: IModelConfigKeys = {
|
|
91
|
+
modelName: MIDSCENE_INSIGHT_MODEL_NAME,
|
|
92
|
+
/**
|
|
93
|
+
* proxy
|
|
94
|
+
*/
|
|
95
|
+
socksProxy: MIDSCENE_INSIGHT_MODEL_SOCKS_PROXY,
|
|
96
|
+
httpProxy: MIDSCENE_INSIGHT_MODEL_HTTP_PROXY,
|
|
97
|
+
/**
|
|
98
|
+
* OpenAI
|
|
99
|
+
*/
|
|
100
|
+
openaiBaseURL: MIDSCENE_INSIGHT_MODEL_BASE_URL,
|
|
101
|
+
openaiApiKey: MIDSCENE_INSIGHT_MODEL_API_KEY,
|
|
102
|
+
openaiExtraConfig: MIDSCENE_INSIGHT_MODEL_INIT_CONFIG_JSON,
|
|
103
|
+
extraBody: MIDSCENE_INSIGHT_MODEL_EXTRA_BODY_JSON,
|
|
104
|
+
/**
|
|
105
|
+
* Extra
|
|
106
|
+
*/
|
|
107
|
+
modelFamily: MIDSCENE_INSIGHT_MODEL_FAMILY,
|
|
108
|
+
/**
|
|
109
|
+
* Timeout
|
|
110
|
+
*/
|
|
111
|
+
timeout: MIDSCENE_INSIGHT_MODEL_TIMEOUT,
|
|
112
|
+
/**
|
|
113
|
+
* Temperature
|
|
114
|
+
*/
|
|
115
|
+
temperature: MIDSCENE_INSIGHT_MODEL_TEMPERATURE,
|
|
116
|
+
/**
|
|
117
|
+
* Retry
|
|
118
|
+
*/
|
|
119
|
+
retryCount: MIDSCENE_INSIGHT_MODEL_RETRY_COUNT,
|
|
120
|
+
retryInterval: MIDSCENE_INSIGHT_MODEL_RETRY_INTERVAL,
|
|
121
|
+
reasoningEffort: MIDSCENE_INSIGHT_MODEL_REASONING_EFFORT,
|
|
122
|
+
reasoningEnabled: MIDSCENE_INSIGHT_MODEL_REASONING_ENABLED,
|
|
123
|
+
reasoningBudget: MIDSCENE_INSIGHT_MODEL_REASONING_BUDGET,
|
|
124
|
+
} as const;
|
|
125
|
+
|
|
126
|
+
export const PLANNING_MODEL_CONFIG_KEYS: IModelConfigKeys = {
|
|
127
|
+
modelName: MIDSCENE_PLANNING_MODEL_NAME,
|
|
128
|
+
/**
|
|
129
|
+
* proxy
|
|
130
|
+
*/
|
|
131
|
+
socksProxy: MIDSCENE_PLANNING_MODEL_SOCKS_PROXY,
|
|
132
|
+
httpProxy: MIDSCENE_PLANNING_MODEL_HTTP_PROXY,
|
|
133
|
+
/**
|
|
134
|
+
* OpenAI
|
|
135
|
+
*/
|
|
136
|
+
openaiBaseURL: MIDSCENE_PLANNING_MODEL_BASE_URL,
|
|
137
|
+
openaiApiKey: MIDSCENE_PLANNING_MODEL_API_KEY,
|
|
138
|
+
openaiExtraConfig: MIDSCENE_PLANNING_MODEL_INIT_CONFIG_JSON,
|
|
139
|
+
extraBody: MIDSCENE_PLANNING_MODEL_EXTRA_BODY_JSON,
|
|
140
|
+
/**
|
|
141
|
+
* Extra
|
|
142
|
+
*/
|
|
143
|
+
modelFamily: MIDSCENE_PLANNING_MODEL_FAMILY,
|
|
144
|
+
/**
|
|
145
|
+
* Timeout
|
|
146
|
+
*/
|
|
147
|
+
timeout: MIDSCENE_PLANNING_MODEL_TIMEOUT,
|
|
148
|
+
/**
|
|
149
|
+
* Temperature
|
|
150
|
+
*/
|
|
151
|
+
temperature: MIDSCENE_PLANNING_MODEL_TEMPERATURE,
|
|
152
|
+
/**
|
|
153
|
+
* Retry
|
|
154
|
+
*/
|
|
155
|
+
retryCount: MIDSCENE_PLANNING_MODEL_RETRY_COUNT,
|
|
156
|
+
retryInterval: MIDSCENE_PLANNING_MODEL_RETRY_INTERVAL,
|
|
157
|
+
reasoningEffort: MIDSCENE_PLANNING_MODEL_REASONING_EFFORT,
|
|
158
|
+
reasoningEnabled: MIDSCENE_PLANNING_MODEL_REASONING_ENABLED,
|
|
159
|
+
reasoningBudget: MIDSCENE_PLANNING_MODEL_REASONING_BUDGET,
|
|
160
|
+
} as const;
|
|
161
|
+
|
|
162
|
+
// modelConfig return default
|
|
163
|
+
export const DEFAULT_MODEL_CONFIG_KEYS: IModelConfigKeys = {
|
|
164
|
+
modelName: MIDSCENE_MODEL_NAME,
|
|
165
|
+
/**
|
|
166
|
+
* proxy
|
|
167
|
+
*/
|
|
168
|
+
socksProxy: MIDSCENE_MODEL_SOCKS_PROXY,
|
|
169
|
+
httpProxy: MIDSCENE_MODEL_HTTP_PROXY,
|
|
170
|
+
/**
|
|
171
|
+
* OpenAI
|
|
172
|
+
*/
|
|
173
|
+
openaiBaseURL: MIDSCENE_MODEL_BASE_URL,
|
|
174
|
+
openaiApiKey: MIDSCENE_MODEL_API_KEY,
|
|
175
|
+
openaiExtraConfig: MIDSCENE_MODEL_INIT_CONFIG_JSON,
|
|
176
|
+
extraBody: MIDSCENE_MODEL_EXTRA_BODY_JSON,
|
|
177
|
+
/**
|
|
178
|
+
* Extra
|
|
179
|
+
*/
|
|
180
|
+
modelFamily: MIDSCENE_MODEL_FAMILY,
|
|
181
|
+
/**
|
|
182
|
+
* Timeout
|
|
183
|
+
*/
|
|
184
|
+
timeout: MIDSCENE_MODEL_TIMEOUT,
|
|
185
|
+
/**
|
|
186
|
+
* Temperature
|
|
187
|
+
*/
|
|
188
|
+
temperature: MIDSCENE_MODEL_TEMPERATURE,
|
|
189
|
+
/**
|
|
190
|
+
* Retry
|
|
191
|
+
*/
|
|
192
|
+
retryCount: MIDSCENE_MODEL_RETRY_COUNT,
|
|
193
|
+
retryInterval: MIDSCENE_MODEL_RETRY_INTERVAL,
|
|
194
|
+
reasoningEffort: MIDSCENE_MODEL_REASONING_EFFORT,
|
|
195
|
+
reasoningEnabled: MIDSCENE_MODEL_REASONING_ENABLED,
|
|
196
|
+
reasoningBudget: MIDSCENE_MODEL_REASONING_BUDGET,
|
|
197
|
+
} as const;
|
|
198
|
+
|
|
199
|
+
// read from process.env
|
|
200
|
+
export const DEFAULT_MODEL_CONFIG_KEYS_LEGACY: IModelConfigKeys = {
|
|
201
|
+
modelName: MIDSCENE_MODEL_NAME,
|
|
202
|
+
/**
|
|
203
|
+
* proxy - Uses legacy MIDSCENE_OPENAI_* variables for backward compatibility
|
|
204
|
+
*/
|
|
205
|
+
socksProxy: MIDSCENE_OPENAI_SOCKS_PROXY,
|
|
206
|
+
httpProxy: MIDSCENE_OPENAI_HTTP_PROXY,
|
|
207
|
+
/**
|
|
208
|
+
* Model API - Uses legacy OPENAI_* variables for backward compatibility
|
|
209
|
+
*/
|
|
210
|
+
openaiBaseURL: OPENAI_BASE_URL,
|
|
211
|
+
openaiApiKey: OPENAI_API_KEY,
|
|
212
|
+
openaiExtraConfig: MIDSCENE_OPENAI_INIT_CONFIG_JSON,
|
|
213
|
+
extraBody: MIDSCENE_MODEL_EXTRA_BODY_JSON,
|
|
214
|
+
/**
|
|
215
|
+
* Extra
|
|
216
|
+
*/
|
|
217
|
+
modelFamily: 'DEFAULT_MODEL_CONFIG_KEYS has no modelFamily key',
|
|
218
|
+
/**
|
|
219
|
+
* Timeout - use the new key for legacy mode too
|
|
220
|
+
*/
|
|
221
|
+
timeout: MIDSCENE_MODEL_TIMEOUT,
|
|
222
|
+
/**
|
|
223
|
+
* Temperature - use the new key for legacy mode too
|
|
224
|
+
*/
|
|
225
|
+
temperature: MIDSCENE_MODEL_TEMPERATURE,
|
|
226
|
+
/**
|
|
227
|
+
* Retry - use the new key for legacy mode too
|
|
228
|
+
*/
|
|
229
|
+
retryCount: MIDSCENE_MODEL_RETRY_COUNT,
|
|
230
|
+
retryInterval: MIDSCENE_MODEL_RETRY_INTERVAL,
|
|
231
|
+
reasoningEffort: MIDSCENE_MODEL_REASONING_EFFORT,
|
|
232
|
+
reasoningEnabled: MIDSCENE_MODEL_REASONING_ENABLED,
|
|
233
|
+
reasoningBudget: MIDSCENE_MODEL_REASONING_BUDGET,
|
|
234
|
+
} as const;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { initDebugConfig } from './init-debug';
|
|
2
|
+
import type { ModelConfigManager } from './model-config-manager';
|
|
3
|
+
import {
|
|
4
|
+
BOOLEAN_ENV_KEYS,
|
|
5
|
+
GLOBAL_ENV_KEYS,
|
|
6
|
+
NUMBER_ENV_KEYS,
|
|
7
|
+
STRING_ENV_KEYS,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
ALL_ENV_KEYS,
|
|
12
|
+
MATCH_BY_POSITION,
|
|
13
|
+
MODEL_ENV_KEYS,
|
|
14
|
+
type TGlobalConfig,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Collect global configs from process.env, overrideAIConfig, etc.
|
|
19
|
+
* And provider methods to get merged config value
|
|
20
|
+
*/
|
|
21
|
+
export class GlobalConfigManager {
|
|
22
|
+
private override:
|
|
23
|
+
| {
|
|
24
|
+
newConfig?: Partial<TGlobalConfig>;
|
|
25
|
+
extendMode?: boolean;
|
|
26
|
+
}
|
|
27
|
+
| undefined;
|
|
28
|
+
|
|
29
|
+
private keysHaveBeenRead: Record<string, boolean> = {};
|
|
30
|
+
|
|
31
|
+
private globalModelConfigManager: ModelConfigManager | undefined = undefined;
|
|
32
|
+
|
|
33
|
+
constructor() {
|
|
34
|
+
initDebugConfig();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* recalculate allEnvConfig every time because process.env can be updated any time
|
|
39
|
+
*/
|
|
40
|
+
public getAllEnvConfig() {
|
|
41
|
+
const envConfig = ALL_ENV_KEYS.reduce(
|
|
42
|
+
(p, name) => {
|
|
43
|
+
p[name] = process.env[name];
|
|
44
|
+
return p;
|
|
45
|
+
},
|
|
46
|
+
Object.create(null) as Record<string, string | undefined>,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (this.override) {
|
|
50
|
+
const { newConfig, extendMode } = this.override;
|
|
51
|
+
if (extendMode) {
|
|
52
|
+
return { ...envConfig, ...newConfig };
|
|
53
|
+
} else {
|
|
54
|
+
return { ...newConfig };
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
return envConfig;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getEnvConfigValue(key: (typeof STRING_ENV_KEYS)[number]) {
|
|
62
|
+
const allConfig = this.getAllEnvConfig();
|
|
63
|
+
|
|
64
|
+
if (key === MATCH_BY_POSITION) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
'MATCH_BY_POSITION is discarded, use MIDSCENE_MODEL_FAMILY instead',
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!STRING_ENV_KEYS.includes(key)) {
|
|
71
|
+
throw new Error(`getEnvConfigValue with key ${key} is not supported.`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const value = allConfig[key];
|
|
75
|
+
this.keysHaveBeenRead[key] = true;
|
|
76
|
+
if (typeof value === 'string') {
|
|
77
|
+
return value.trim();
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* read boolean only from process.env
|
|
84
|
+
*/
|
|
85
|
+
getEnvConfigInBoolean(key: (typeof BOOLEAN_ENV_KEYS)[number]): boolean {
|
|
86
|
+
const allConfig = this.getAllEnvConfig();
|
|
87
|
+
|
|
88
|
+
if (!BOOLEAN_ENV_KEYS.includes(key)) {
|
|
89
|
+
throw new Error(`getEnvConfigInBoolean with key ${key} is not supported`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const value = allConfig[key];
|
|
93
|
+
this.keysHaveBeenRead[key] = true;
|
|
94
|
+
|
|
95
|
+
if (!value) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (/^(true|1)$/i.test(value)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (/^(false|0)$/i.test(value)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return !!value.trim();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Read environment variable value and convert it to number.
|
|
109
|
+
* Returns undefined if the value is not set or cannot be converted to a valid number.
|
|
110
|
+
*/
|
|
111
|
+
getEnvConfigValueAsNumber(
|
|
112
|
+
key: (typeof STRING_ENV_KEYS)[number] | (typeof NUMBER_ENV_KEYS)[number],
|
|
113
|
+
): number | undefined {
|
|
114
|
+
if (
|
|
115
|
+
!STRING_ENV_KEYS.includes(key as never) &&
|
|
116
|
+
!NUMBER_ENV_KEYS.includes(key as never)
|
|
117
|
+
) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`getEnvConfigValueAsNumber with key ${key} is not supported.`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const allConfig = this.getAllEnvConfig();
|
|
124
|
+
const value = allConfig[key];
|
|
125
|
+
this.keysHaveBeenRead[key] = true;
|
|
126
|
+
|
|
127
|
+
if (typeof value !== 'string') {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const trimmed = value.trim();
|
|
131
|
+
if (!trimmed) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
const numValue = Number(trimmed);
|
|
135
|
+
return Number.isNaN(numValue) ? undefined : numValue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
registerModelConfigManager(globalModelConfigManager: ModelConfigManager) {
|
|
139
|
+
this.globalModelConfigManager = globalModelConfigManager;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @deprecated use the modelConfig param in Agent constructor instead
|
|
144
|
+
*/
|
|
145
|
+
overrideAIConfig(
|
|
146
|
+
newConfig: Partial<
|
|
147
|
+
Record<
|
|
148
|
+
(typeof GLOBAL_ENV_KEYS)[number] | (typeof MODEL_ENV_KEYS)[number],
|
|
149
|
+
string
|
|
150
|
+
>
|
|
151
|
+
>,
|
|
152
|
+
extendMode = false, // true: merge with global config, false: override global config
|
|
153
|
+
) {
|
|
154
|
+
for (const key in newConfig) {
|
|
155
|
+
if (![...GLOBAL_ENV_KEYS, ...MODEL_ENV_KEYS].includes(key as never)) {
|
|
156
|
+
throw new Error(`Failed to override AI config, invalid key: ${key}`);
|
|
157
|
+
}
|
|
158
|
+
const value = newConfig[key as keyof typeof newConfig];
|
|
159
|
+
if (typeof value !== 'string') {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Failed to override AI config, value for key ${key} must be a string, but got with type ${typeof value}`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (this.keysHaveBeenRead[key]) {
|
|
165
|
+
console.warn(
|
|
166
|
+
`Warning: try to override AI config with key ${key} ,but it has been read.`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const savedNewConfig = extendMode
|
|
171
|
+
? {
|
|
172
|
+
...this.override?.newConfig,
|
|
173
|
+
...newConfig,
|
|
174
|
+
}
|
|
175
|
+
: newConfig;
|
|
176
|
+
|
|
177
|
+
this.override = {
|
|
178
|
+
newConfig: {
|
|
179
|
+
...savedNewConfig,
|
|
180
|
+
},
|
|
181
|
+
extendMode,
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
if (!this.globalModelConfigManager) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
'globalModelConfigManager is not registered, which should not happen',
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
this.globalModelConfigManager.clearModelConfigMap();
|
|
190
|
+
}
|
|
191
|
+
}
|