diff --git a/packages/rpa-core/src/capabilities/desktop.ts b/packages/rpa-core/src/capabilities/desktop.ts index 4c14823..6e7a828 100644 --- a/packages/rpa-core/src/capabilities/desktop.ts +++ b/packages/rpa-core/src/capabilities/desktop.ts @@ -151,7 +151,10 @@ const clipboardSet: CapabilityHandler = async (_ctx, _node, args) => { const notice: CapabilityHandler = async (_ctx, _node, args) => { const title = text(args, ["title"], "提醒"); const content = text(args, ["text", "message"]); - await powershell(`Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show(${psString(content)},${psString(title)}) | Out-Null`); + // MessageBox 阻塞等用户点确定,同样不能吃 powershell() 默认 30s 超时;且 MB_TOPMOST 保证弹窗不被主窗口盖住 + const waitSeconds = number(args, ["wait_time", "waitTime", "timeout"], 600); + const timeoutMs = Math.max(10000, Math.round(waitSeconds * 1000)); + await powershell(`Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show(${psString(content)},${psString(title)},'OK','Information','Button1','HelpButton',1) | Out-Null`, timeoutMs); return true; }; @@ -204,26 +207,73 @@ register("Dialog_CsMsgBox", async (_ctx, _node, args) => { register("Dialog_OpenFile", async (_ctx, _node, args) => { const title = text(args, ["title"], "选择文件"); const wildcard = text(args, ["wildcard"], "所有文件|*.*"); - return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.OpenFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileName } else { '' }"); + // ShowDialog 阻塞等用户选择,等待上限给到 30 分钟(文件挑选通常耗时更久) + return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.OpenFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileName } else { '' }", 1800000); }); register("Dialog_OpenFiles", async (_ctx, _node, args) => { const title = text(args, ["title"], "选择文件"); const wildcard = text(args, ["wildcard"], "所有文件|*.*"); - return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.OpenFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; $dlg.Multiselect = $true; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileNames -join ';' } else { '' }"); + return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.OpenFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; $dlg.Multiselect = $true; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileNames -join ';' } else { '' }", 1800000); }); register("Dialog_SaveFile", async (_ctx, _node, args) => { const title = text(args, ["title"], "保存文件"); const wildcard = text(args, ["wildcard"], "所有文件|*.*"); - return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.SaveFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileName } else { '' }"); + return powershell("Add-Type -AssemblyName System.Windows.Forms | Out-Null; $dlg = New-Object System.Windows.Forms.SaveFileDialog; $dlg.Filter = " + psString(wildcard) + "; $dlg.Title = " + psString(title) + "; if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileName } else { '' }", 1800000); }); register(["Dialog_InputBox", "Dialog_CsInputBox"], async (_ctx, _node, args) => { const title = text(args, ["title", "input_title"], "请输入"); const description = text(args, ["description", "txt_content"], ""); const defaultValue = text(args, ["text"], ""); - return powershell("Add-Type -AssemblyName Microsoft.VisualBasic | Out-Null; [Microsoft.VisualBasic.Interaction]::InputBox(" + psString(description) + ", " + psString(title) + ", " + psString(defaultValue) + ")"); + // 交互对话框会一直阻塞等用户输入,必须给 powershell() 传足够长的超时(默认 30s 会把还在等人的对话框进程杀掉,报 Command failed)。 + // 默认 10 分钟,可用 wait_time(秒)自定义;仍超时则报「用户未及时输入」而非笼统的 Command failed。 + const waitSeconds = number(args, ["wait_time", "waitTime", "timeout"], 600); + const timeoutMs = Math.max(10000, Math.round(waitSeconds * 1000)); + // VB InputBox 不置顶也不在任务栏显示,容易被主窗口盖住导致"看不见弹窗→干等超时"。 + // 改用 WinForms 窗口:Topmost 强制置顶 + ShowInTaskbar + StartPosition 屏幕中央,保证用户一定看得见。 + const script = [ + "Add-Type -AssemblyName System.Windows.Forms | Out-Null", + "Add-Type -AssemblyName System.Drawing | Out-Null", + "$form = New-Object System.Windows.Forms.Form", + "$form.Text = " + psString(title), + "$form.Size = New-Object System.Drawing.Size(460, 240)", + "$form.StartPosition = 'CenterScreen'", + "$form.TopMost = $true", + "$form.ShowInTaskbar = $true", + "$form.FormBorderStyle = 'FixedDialog'", + "$form.MaximizeBox = $false", + "$label = New-Object System.Windows.Forms.Label", + "$label.Text = " + psString(description), + "$label.Location = New-Object System.Drawing.Point(14, 12)", + "$label.Size = New-Object System.Drawing.Size(416, 60)", + "$box = New-Object System.Windows.Forms.TextBox", + "$box.Text = " + psString(defaultValue), + "$box.Location = New-Object System.Drawing.Point(14, 78)", + "$box.Size = New-Object System.Drawing.Size(416, 26)", + "$ok = New-Object System.Windows.Forms.Button", + "$ok.Text = '确定'", + "$ok.DialogResult = [System.Windows.Forms.DialogResult]::OK", + "$ok.Location = New-Object System.Drawing.Point(256, 150)", + "$ok.Size = New-Object System.Drawing.Size(84, 30)", + "$form.AcceptButton = $ok", + "$cancel = New-Object System.Windows.Forms.Button", + "$cancel.Text = '取消'", + "$cancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel", + "$cancel.Location = New-Object System.Drawing.Point(346, 150)", + "$cancel.Size = New-Object System.Drawing.Size(84, 30)", + "$form.Controls.AddRange(@($label, $box, $ok, $cancel))", + "$form.Add_Shown({ $form.Activate() })", + "$result = $form.ShowDialog()", + "if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $box.Text } else { '' }" + ].join("; "); + return powershell(script, timeoutMs).then((value) => ({ value, canceled: value === "" })).catch((error) => { + if (error instanceof RuntimeError && /Command failed/.test(error.message) && !error.message.includes("RPA_ERROR")) { + throw new RuntimeError("dialog_timeout", `输入对话框等待超时(${Math.round(timeoutMs / 1000)} 秒)用户未完成输入`, { nodeId: _node.id, componentId: _node.componentId, recommendation: "置顶输入框应显示在屏幕最前;若仍超时请调大节点的超时时间或 wait_time 参数" }); + } + throw error; + }); }); register("Dialog_Notice", notice);