改善生成代码逻辑,覆盖各种情况,改用asyncMode存储promise状态

This commit is contained in:
fofolee
2025-01-26 12:05:56 +08:00
parent 208a6a08d9
commit 365964fc02
22 changed files with 387 additions and 237 deletions

View File

@@ -133,7 +133,7 @@ export const audioCommands = {
value: "quickcomposer.audio.media.play",
label: "音频播放",
icon: "music_note",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.audio.media.play",
@@ -175,7 +175,7 @@ export const audioCommands = {
value: "quickcomposer.audio.record",
label: "音频录制",
icon: "mic",
isAsync: true,
asyncMode: "await",
config: [
{
label: "录制时长(ms)",
@@ -211,7 +211,7 @@ export const audioCommands = {
{
value: "quickcomposer.audio.media.beep",
label: "系统音效",
isAsync: true,
asyncMode: "await",
icon: "notifications_active",
config: [
{
@@ -238,7 +238,7 @@ export const audioCommands = {
value: "quickcomposer.audio.media.analyze",
label: "音频信息",
icon: "analytics",
isAsync: true,
asyncMode: "await",
config: [
{
label: "音频文件",

View File

@@ -36,7 +36,7 @@ export const browserCommands = {
value: "quickcomposer.browser.startClient",
label: "浏览器实例管理",
icon: "launch",
isAsync: true,
asyncMode: "await",
config: [],
subCommands: [
{
@@ -168,7 +168,7 @@ export const browserCommands = {
value: "quickcomposer.browser.getUrl",
label: "获取/设置网址",
icon: "link",
isAsync: true,
asyncMode: "await",
config: [tabConfig],
subCommands: [
{
@@ -196,7 +196,7 @@ export const browserCommands = {
value: "quickcomposer.browser.getTabs",
label: "标签操作",
icon: "tab",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.browser.getTabs",
@@ -240,7 +240,7 @@ export const browserCommands = {
value: "quickcomposer.browser.captureScreenshot",
label: "捕获截图",
icon: "screenshot",
isAsync: true,
asyncMode: "await",
config: [
tabConfig,
{
@@ -305,7 +305,7 @@ export const browserCommands = {
value: "quickcomposer.browser.executeScript",
label: "执行脚本",
icon: "code",
isAsync: true,
asyncMode: "await",
config: [
tabConfig,
{
@@ -328,7 +328,7 @@ export const browserCommands = {
value: "quickcomposer.browser.injectRemoteScript",
label: "注入脚本/样式",
icon: "style",
isAsync: true,
asyncMode: "await",
config: [tabConfig],
subCommands: [
{
@@ -396,7 +396,7 @@ export const browserCommands = {
value: "quickcomposer.browser.setCookie",
label: "Cookie操作",
icon: "cookie",
isAsync: true,
asyncMode: "await",
config: [tabConfig],
subCommands: [
{
@@ -486,7 +486,7 @@ export const browserCommands = {
value: "quickcomposer.browser.clickElement",
label: "元素操作",
icon: "web",
isAsync: true,
asyncMode: "await",
config: [
tabConfig,
{
@@ -591,7 +591,7 @@ export const browserCommands = {
value: "quickcomposer.browser.scrollTo",
label: "滚动及页面尺寸",
icon: "open_in_full",
isAsync: true,
asyncMode: "await",
config: [tabConfig],
subCommands: [
{
@@ -631,8 +631,8 @@ export const browserCommands = {
value: "quickcomposer.browser.network.setRequestInterception",
label: "修改请求/响应",
icon: "network",
isAsync: true,
isAsync: true,
asyncMode: "await",
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.browser.network.setRequestInterception",
@@ -753,7 +753,7 @@ export const browserCommands = {
value: "quickcomposer.browser.device.setDevice",
label: "设备模拟",
icon: "devices",
isAsync: true,
asyncMode: "await",
config: [tabConfig],
subCommands: [
{

View File

@@ -709,7 +709,7 @@ export const dataCommands = {
label: "数据压缩解压",
component: "ZlibEditor",
icon: "compress",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.data.htmlParser",

View File

@@ -7,7 +7,7 @@ export const fileCommands = {
value: "quickcomposer.file.operation",
label: "文件/文件夹操作",
component: "FileOperationEditor",
isAsync: true,
asyncMode: "await",
},
{
value: "utools.shellOpenItem",
@@ -67,7 +67,7 @@ export const fileCommands = {
value: "quickcomposer.file.archive",
label: "文件归档",
icon: "archive",
isAsync: true,
asyncMode: "await",
config: [
{
label: "操作类型",

View File

@@ -25,7 +25,7 @@ export const imageCommands = {
value: "quickcomposer.image.analyze",
label: "图片信息",
icon: "analytics",
isAsync: true,
asyncMode: "await",
config: [
{
label: "图片文件",
@@ -54,7 +54,7 @@ export const imageCommands = {
value: "quickcomposer.image.resize",
label: "调整大小",
icon: "aspect_ratio",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入文件",
@@ -140,7 +140,7 @@ export const imageCommands = {
value: "quickcomposer.image.rotate",
label: "旋转图片",
icon: "rotate_right",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入文件",
@@ -207,7 +207,7 @@ export const imageCommands = {
value: "quickcomposer.image.crop",
label: "裁剪图片",
icon: "crop",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入文件",
@@ -302,7 +302,7 @@ export const imageCommands = {
value: "quickcomposer.image.watermark",
label: "添加水印",
icon: "format_color_text",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入文件",
@@ -409,7 +409,7 @@ export const imageCommands = {
value: "quickcomposer.image.convert",
label: "格式转换",
icon: "transform",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入文件",

View File

@@ -7,7 +7,7 @@ export const macosCommands = {
value: "quickcomposer.macos.app.getFrontmost",
label: "应用及窗口控制",
icon: "apps",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.macos.app.getFrontmost",
@@ -15,29 +15,32 @@ export const macosCommands = {
icon: "front_hand",
outputs: {
label: "前台应用信息",
name: { label: "应用名称" },
displayedName: { label: "应用显示名称" },
path: { label: "应用路径" },
version: { label: "应用版本" },
pid: { label: "应用进程ID" },
backgroundOnly: { label: "是否后台运行" },
visible: { label: "是否可见" },
frontmost: { label: "是否前台运行" },
window: {
label: "窗口信息",
name: { label: "窗口名称" },
title: { label: "窗口标题" },
index: { label: "窗口索引" },
position: {
label: "窗口位置",
placeholder: "数组, 第一个元素是 x 坐标,第二个元素是 y 坐标",
structure: {
name: { label: "应用名称" },
displayedName: { label: "应用显示名称" },
path: { label: "应用路径" },
version: { label: "应用版本" },
pid: { label: "应用进程ID" },
backgroundOnly: { label: "是否后台运行" },
visible: { label: "是否可见" },
frontmost: { label: "是否前台运行" },
window: {
label: "窗口信息",
name: { label: "窗口名称" },
title: { label: "窗口标题" },
index: { label: "窗口索引" },
position: {
label: "窗口位置",
placeholder:
"数组, 第一个元素是 x 坐标,第二个元素是 y 坐标",
},
size: {
label: "窗口大小",
placeholder: "数组, 第一个元素是宽度,第二个元素是高度",
},
minimized: { label: "是否最小化" },
fullscreen: { label: "是否全屏" },
},
size: {
label: "窗口大小",
placeholder: "数组, 第一个元素是宽度,第二个元素是高度",
},
minimized: { label: "是否最小化" },
fullscreen: { label: "是否全屏" },
},
},
},
@@ -164,7 +167,7 @@ export const macosCommands = {
value: "quickcomposer.macos.system.setVolume",
label: "系统管理",
icon: "settings",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.macos.system.setVolume",
@@ -284,7 +287,7 @@ export const macosCommands = {
value: "quickcomposer.macos.finder.getSelection",
label: "访达管理",
icon: "folder",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.macos.finder.getSelection",

View File

@@ -26,14 +26,14 @@ export const networkCommands = {
icon: "public",
},
],
isAsync: true,
asyncMode: "await",
},
{
value: "ubrowser",
label: "ubrowser浏览器操作",
config: [],
component: "UBrowserEditor",
isAsync: true,
asyncMode: "await",
icon: "public",
},
{
@@ -41,7 +41,7 @@ export const networkCommands = {
label: "HTTP请求(Axios)",
config: [],
component: "AxiosConfigEditor",
isAsync: true,
asyncMode: "await",
icon: "http",
},
{
@@ -246,7 +246,7 @@ export const networkCommands = {
value: "quickcomposer.network.dns.lookupHost",
label: "DNS操作",
icon: "dns",
isAsync: true,
asyncMode: "await",
config: [],
subCommands: [
{
@@ -389,7 +389,7 @@ export const networkCommands = {
value: "quickcommand.downloadFile",
label: "下载文件",
icon: "file_download",
isAsync: true,
asyncMode: "await",
config: [
{
label: "文件URL",
@@ -411,7 +411,7 @@ export const networkCommands = {
value: "quickcommand.uploadFile",
label: "上传文件",
icon: "file_upload",
isAsync: true,
asyncMode: "await",
config: [
{
label: "上传接口地址",

View File

@@ -24,7 +24,7 @@ export const scriptCommands = {
label: "运行脚本",
component: "ScriptEditor",
desc: "运行各种编程语言的代码",
isAsync: true,
asyncMode: "await",
},
{
value: "return",

View File

@@ -216,19 +216,19 @@ export const simulateCommands = {
label: "屏幕找图",
component: "ImageSearchEditor",
config: [],
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.simulate.screenColorPick",
label: "屏幕取色",
icon: "colorize",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.simulate.captureScreen",
label: "屏幕截图",
icon: "screenshot_monitor",
isAsync: true,
asyncMode: "await",
config: [
{
label: "截图范围",

View File

@@ -6,31 +6,31 @@ export const statusCommands = {
value: "utools.readCurrentFolderPath",
label: "获取当前文件管理器路径",
icon: "folder",
isAsync: true,
asyncMode: "await",
},
{
value: "utools.readCurrentBrowserUrl",
label: "获取当前浏览器地址",
icon: "language",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.status.getSelectedText",
label: "获取选中文本",
icon: "text_fields",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.status.getSelectedImage",
label: "获取选中的图片",
icon: "image",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcomposer.status.getSelectedFiles",
label: "获取选中的文件",
icon: "file_present",
isAsync: true,
asyncMode: "await",
},
],
};

View File

@@ -105,7 +105,7 @@ export const systemCommands = {
label: "执行系统命令",
component: "SystemCommandEditor",
icon: "terminal",
isAsync: true,
asyncMode: "await",
},
{
value: "utools.getPath",

View File

@@ -111,7 +111,7 @@ export const uiCommands = {
{
value: "quickcommand.showMessageBox",
label: "消息提示",
isAsync: true,
asyncMode: "await",
config: [
{
label: "提示内容",
@@ -168,7 +168,7 @@ export const uiCommands = {
{
value: "quickcommand.showConfirmBox",
label: "确认框",
isAsync: true,
asyncMode: "await",
outputs: {
label: "是否确认",
},
@@ -219,7 +219,7 @@ export const uiCommands = {
{
value: "quickcommand.showButtonBox",
label: "按钮组",
isAsync: true,
asyncMode: "await",
width: 12,
config: [
{
@@ -254,7 +254,7 @@ export const uiCommands = {
{
value: "quickcommand.showInputBox",
label: "输入框",
isAsync: true,
asyncMode: "await",
config: [
{
label: "输入框",
@@ -293,12 +293,12 @@ export const uiCommands = {
value: "quickcommand.showSelectList",
label: "选择列表",
component: "SelectListEditor",
isAsync: true,
asyncMode: "await",
},
{
value: "quickcommand.showTextArea",
label: "文本框",
isAsync: true,
asyncMode: "await",
config: [
{
label: "文本框占位符",
@@ -329,7 +329,7 @@ export const uiCommands = {
{
value: "quickcommand.showSystemWaitButton",
label: "等待操作按钮",
isAsync: true,
asyncMode: "await",
config: [
{
component: "OptionEditor",

View File

@@ -199,7 +199,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.window.getWindowInfo",
label: "搜索/选择窗口",
icon: "window",
isAsync: true,
asyncMode: "await",
config: [],
subCommands: [
{
@@ -207,6 +207,22 @@ export const windowsCommands = {
value: "quickcomposer.windows.window.getWindowInfo",
label: "搜索窗口",
icon: "search",
outputs: {
label: "窗口信息(数组)",
structure: [
{
handle: { label: "窗口句柄" },
title: { label: "窗口标题" },
class: { label: "窗口类名" },
x: { label: "窗口X坐标" },
y: { label: "窗口Y坐标" },
width: { label: "窗口宽度" },
height: { label: "窗口高度" },
processName: { label: "窗口进程名" },
processPath: { label: "窗口进程路径" },
},
],
},
},
{
value: "quickcomposer.windows.automation.inspect",
@@ -381,14 +397,14 @@ export const windowsCommands = {
],
},
],
isAsync: true,
asyncMode: "await",
},
// automation
{
value: "quickcomposer.windows.automation.click",
label: "界面自动化",
icon: "smart_button",
isAsync: true,
asyncMode: "await",
config: searchElementConfig,
subCommands: [
{
@@ -611,7 +627,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.sendmessage.listControls",
label: "发送控制消息",
icon: "smart_button",
isAsync: true,
asyncMode: "await",
config: windowHandleConfig,
subCommands: [
{
@@ -804,7 +820,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.monitor.watchClipboard",
label: "剪贴板/文件监控",
icon: "monitor_heart",
isAsync: true,
asyncMode: "await",
showLoading: true,
subCommands: [
{
@@ -866,7 +882,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.process.listProcesses",
label: "进程管理",
icon: "memory",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.windows.process.listProcesses",
@@ -928,7 +944,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.registry.listKeys",
label: "注册表管理",
icon: "settings",
isAsync: true,
asyncMode: "await",
config: [
{
label: "注册表路径",
@@ -1053,7 +1069,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.service.listServices",
label: "服务管理",
icon: "miscellaneous_services",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.windows.service.listServices",
@@ -1095,7 +1111,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.software.listSoftware",
label: "软件管理",
icon: "apps",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.windows.software.listSoftware",
@@ -1124,7 +1140,7 @@ export const windowsCommands = {
value: "quickcomposer.windows.utils.setWallpaper",
label: "系统工具",
icon: "build",
isAsync: true,
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.windows.utils.setWallpaper",

View File

@@ -103,7 +103,7 @@ const commonComponentGuide = {
},
optionalFields: {
desc: "命令描述",
isAsync: "是否异步命令",
asyncMode: "async模式可选值为await/then",
icon: "命令图标",
},
},

View File

@@ -270,7 +270,7 @@ const customComponentGuide = {
},
optionalFields: {
desc: "命令描述",
isAsync: "是否异步命令",
asyncMode: "async模式可选值为await/then",
isControlFlow: "是否控制流命令",
},
},

View File

@@ -1,10 +1,28 @@
export function generateCode(flow) {
let usedVarNames = {};
// 获取变量赋值代码,如果变量已经存在,则直接赋值,否则声明并赋值
const getVarAssignCode = (varName, varValue, funcName) => {
if (!usedVarNames[funcName]) {
usedVarNames[funcName] = [];
}
if (usedVarNames[funcName].includes(varName)) {
return `${varName} = ${varValue};`;
}
usedVarNames[funcName].push(varName);
return `let ${varName} = ${varValue};`;
};
const getVarByPath = (name, path) => {
return `${name}${path.startsWith("[") ? "" : "."}${path}`;
};
const { commands, name, label, customVariables = [] } = flow;
const params = customVariables.filter((v) => v.type === "param") || [];
const manualVars = customVariables.filter((v) => v.type === "var") || [];
// 检查是否包含异步函数
const hasAsyncFunction = commands.some((cmd) => cmd.isAsync);
const hasAsyncFunction = commands.some((cmd) => cmd.asyncMode);
let code = [];
const funcName = name || "func_" + new Date().getTime();
@@ -17,9 +35,13 @@ export function generateCode(flow) {
.join(", ")}) {`
);
code.push(manualVars.map((v) => ` let ${v.name} = ${v.value};`).join("\n"));
const indent = " ";
// 局部变量赋值
manualVars.forEach((v) => {
code.push(indent + getVarAssignCode(v.name, v.value, flow.name));
});
commands.forEach((cmd) => {
// 跳过禁用的命令
if (cmd.disabled) return;
@@ -29,36 +51,71 @@ export function generateCode(flow) {
// 处理输出变量
if (cmd.outputVariable) {
const { name, details } = cmd.outputVariable;
if (cmd.isAsync) {
if (cmd.asyncMode === "then") {
// 使用回调函数模式
if (cmd.callbackFunc) {
// 使用回调函数模式
cmdCode = `${cmdCode}.then(${cmd.callbackFunc})`;
} else {
// 使用 await 模式
cmdCode = `const ${name} = await ${cmdCode}`;
code.push(indent + cmdCode);
// 处理详细变量
if (details) {
Object.entries(details).forEach(([path, varName]) => {
code.push(`${indent}let ${varName} = ${name}.${path};`);
});
// 如果回调函数存在,则使用回调函数模式,否则保持原样
if (!details) {
cmdCode = `${cmdCode}.then(${cmd.callbackFunc})`;
} else {
// 如果输出变量有详细变量,则需要为每个变量赋值
const promiseName = name || "result";
const extractVarCode = Object.entries(details)
.map(
([path, varName]) =>
`let ${varName} = ${getVarByPath(promiseName, path)}`
)
.join("\n");
const funcName = cmd.callbackFunc;
const funcParams =
(name ? `${name},` : "") + Object.values(details).join(",");
cmdCode = `${cmdCode}.then((${promiseName})=>{
${extractVarCode}
${funcName}(${funcParams})
})`;
}
return;
}
} else {
cmdCode = `const ${name} = ${cmdCode}`;
code.push(indent + cmdCode);
} else if (cmd.asyncMode === "await") {
// 使用 await 模式
const promiseName = name || "result";
cmdCode = getVarAssignCode(promiseName, `await ${cmdCode}`);
code.push(indent + cmdCode);
// 处理详细变量
if (details) {
Object.entries(details).forEach(([path, varName]) => {
code.push(`${indent}let ${varName} = ${name}.${path};`);
code.push(
`${indent}${getVarAssignCode(
varName,
getVarByPath(promiseName, path)
)}`
);
});
}
} else {
// 非Async命令
cmdCode = getVarAssignCode(name, `${cmdCode}`);
code.push(indent + cmdCode);
// 处理详细变量
if (details) {
Object.entries(details).forEach(([path, varName]) => {
code.push(
`${indent}${getVarAssignCode(varName, getVarByPath(name, path))}`
);
});
}
return;
}
} else {
if (cmd.asyncMode === "await") {
cmdCode = `await ${cmdCode}`;
}
code.push(indent + cmdCode);
}
code.push(indent + cmdCode);
});
code.push("}"); // Close the function

View File

@@ -7,12 +7,7 @@ import { validateVariableName } from "js/common/variableValidator";
*/
function generateRandomSuffix(varCount, withPrefix = true) {
// 根据变量数量决定后缀长度
let length = 1; // 默认1位
if (varCount >= 100) {
length = 3;
} else if (varCount >= 10) {
length = 2;
}
let length = varCount > 100 ? 3 : 2;
return (
(withPrefix ? "_" : "") +
@@ -100,7 +95,9 @@ export function processVariable({ value, existingVars, baseName = "var" }) {
isValid: true,
processedValue: processedVar,
warning:
processedVar !== value ? `输入值非法,已被修改为: ${processedVar}` : null,
processedVar !== value
? `输入值非法,已被修改为: ${processedVar}`
: null,
};
}