xstudio-linux 0.0.0-stage → 0.2.3
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/CHANGELOG.md +443 -0
- package/LICENSE +31 -0
- package/README.md +527 -2
- package/dist/adapter-cli/cli.d.ts +28 -0
- package/dist/adapter-cli/cli.js +235 -0
- package/dist/adapter-cli/cli.js.map +1 -0
- package/dist/adapter-mcp/server.d.ts +81 -0
- package/dist/adapter-mcp/server.js +477 -0
- package/dist/adapter-mcp/server.js.map +1 -0
- package/dist/config.d.ts +45 -0
- package/dist/config.js +78 -0
- package/dist/config.js.map +1 -0
- package/dist/core/catalog/pronunciation.d.ts +47 -0
- package/dist/core/catalog/pronunciation.js +402 -0
- package/dist/core/catalog/pronunciation.js.map +1 -0
- package/dist/core/catalog/singers.d.ts +46 -0
- package/dist/core/catalog/singers.js +122 -0
- package/dist/core/catalog/singers.js.map +1 -0
- package/dist/core/contracts.d.ts +348 -0
- package/dist/core/contracts.js +17 -0
- package/dist/core/contracts.js.map +1 -0
- package/dist/core/degrade/chain.d.ts +80 -0
- package/dist/core/degrade/chain.js +137 -0
- package/dist/core/degrade/chain.js.map +1 -0
- package/dist/core/diagnose/error-resolver.d.ts +51 -0
- package/dist/core/diagnose/error-resolver.js +202 -0
- package/dist/core/diagnose/error-resolver.js.map +1 -0
- package/dist/core/guard/skill-gate.d.ts +89 -0
- package/dist/core/guard/skill-gate.js +121 -0
- package/dist/core/guard/skill-gate.js.map +1 -0
- package/dist/core/midi/convert.d.ts +31 -0
- package/dist/core/midi/convert.js +125 -0
- package/dist/core/midi/convert.js.map +1 -0
- package/dist/core/midi/smf.d.ts +37 -0
- package/dist/core/midi/smf.js +422 -0
- package/dist/core/midi/smf.js.map +1 -0
- package/dist/core/render/artifacts.d.ts +37 -0
- package/dist/core/render/artifacts.js +417 -0
- package/dist/core/render/artifacts.js.map +1 -0
- package/dist/core/render/orchestrator.d.ts +110 -0
- package/dist/core/render/orchestrator.js +974 -0
- package/dist/core/render/orchestrator.js.map +1 -0
- package/dist/core/svip/codec.d.ts +30 -0
- package/dist/core/svip/codec.js +664 -0
- package/dist/core/svip/codec.js.map +1 -0
- package/dist/core/svip/document.d.ts +31 -0
- package/dist/core/svip/document.js +158 -0
- package/dist/core/svip/document.js.map +1 -0
- package/dist/core/svip/editor.d.ts +71 -0
- package/dist/core/svip/editor.js +245 -0
- package/dist/core/svip/editor.js.map +1 -0
- package/dist/core/svip/model.d.ts +74 -0
- package/dist/core/svip/model.js +158 -0
- package/dist/core/svip/model.js.map +1 -0
- package/dist/core/svip/tick.d.ts +21 -0
- package/dist/core/svip/tick.js +59 -0
- package/dist/core/svip/tick.js.map +1 -0
- package/dist/core/svip/validator.d.ts +39 -0
- package/dist/core/svip/validator.js +267 -0
- package/dist/core/svip/validator.js.map +1 -0
- package/dist/host/linux.d.ts +15 -0
- package/dist/host/linux.js +42 -0
- package/dist/host/linux.js.map +1 -0
- package/dist/host/sidecar.d.ts +61 -0
- package/dist/host/sidecar.js +365 -0
- package/dist/host/sidecar.js.map +1 -0
- package/dist/host/wine-launch.d.ts +60 -0
- package/dist/host/wine-launch.js +77 -0
- package/dist/host/wine-launch.js.map +1 -0
- package/dist/host/wine-paths.d.ts +30 -0
- package/dist/host/wine-paths.js +103 -0
- package/dist/host/wine-paths.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/services.d.ts +55 -0
- package/dist/services.js +78 -0
- package/dist/services.js.map +1 -0
- package/dist/shared/errors.d.ts +61 -0
- package/dist/shared/errors.js +125 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/shared/paths.d.ts +8 -0
- package/dist/shared/paths.js +30 -0
- package/dist/shared/paths.js.map +1 -0
- package/dist/shared/schema.d.ts +26 -0
- package/dist/shared/schema.js +68 -0
- package/dist/shared/schema.js.map +1 -0
- package/dist/shared/types.d.ts +43 -0
- package/dist/shared/types.js +19 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/tests/unit/artifacts.test.d.ts +1 -0
- package/dist/tests/unit/artifacts.test.js +243 -0
- package/dist/tests/unit/artifacts.test.js.map +1 -0
- package/dist/tests/unit/data-plane.test.d.ts +1 -0
- package/dist/tests/unit/data-plane.test.js +153 -0
- package/dist/tests/unit/data-plane.test.js.map +1 -0
- package/dist/tests/unit/degrade.test.d.ts +1 -0
- package/dist/tests/unit/degrade.test.js +133 -0
- package/dist/tests/unit/degrade.test.js.map +1 -0
- package/dist/tests/unit/diagnosis.test.d.ts +1 -0
- package/dist/tests/unit/diagnosis.test.js +66 -0
- package/dist/tests/unit/diagnosis.test.js.map +1 -0
- package/dist/tests/unit/errors.test.d.ts +1 -0
- package/dist/tests/unit/errors.test.js +83 -0
- package/dist/tests/unit/errors.test.js.map +1 -0
- package/dist/tests/unit/mcp-server.test.d.ts +1 -0
- package/dist/tests/unit/mcp-server.test.js +267 -0
- package/dist/tests/unit/mcp-server.test.js.map +1 -0
- package/dist/tests/unit/mcp-smoke.d.ts +6 -0
- package/dist/tests/unit/mcp-smoke.js +7 -0
- package/dist/tests/unit/mcp-smoke.js.map +1 -0
- package/dist/tests/unit/mcp-smoke.test.d.ts +1 -0
- package/dist/tests/unit/mcp-smoke.test.js +196 -0
- package/dist/tests/unit/mcp-smoke.test.js.map +1 -0
- package/dist/tests/unit/midi.test.d.ts +1 -0
- package/dist/tests/unit/midi.test.js +255 -0
- package/dist/tests/unit/midi.test.js.map +1 -0
- package/dist/tests/unit/pronunciation.test.d.ts +1 -0
- package/dist/tests/unit/pronunciation.test.js +83 -0
- package/dist/tests/unit/pronunciation.test.js.map +1 -0
- package/dist/tests/unit/registry-contract.test.d.ts +1 -0
- package/dist/tests/unit/registry-contract.test.js +114 -0
- package/dist/tests/unit/registry-contract.test.js.map +1 -0
- package/dist/tests/unit/render-orchestrator.test.d.ts +1 -0
- package/dist/tests/unit/render-orchestrator.test.js +548 -0
- package/dist/tests/unit/render-orchestrator.test.js.map +1 -0
- package/dist/tests/unit/sidecar.test.d.ts +1 -0
- package/dist/tests/unit/sidecar.test.js +299 -0
- package/dist/tests/unit/sidecar.test.js.map +1 -0
- package/dist/tests/unit/singers.test.d.ts +1 -0
- package/dist/tests/unit/singers.test.js +73 -0
- package/dist/tests/unit/singers.test.js.map +1 -0
- package/dist/tests/unit/skill-gate.test.d.ts +1 -0
- package/dist/tests/unit/skill-gate.test.js +182 -0
- package/dist/tests/unit/skill-gate.test.js.map +1 -0
- package/dist/tests/unit/svip-codec.test.d.ts +1 -0
- package/dist/tests/unit/svip-codec.test.js +182 -0
- package/dist/tests/unit/svip-codec.test.js.map +1 -0
- package/dist/tests/unit/svip-document.test.d.ts +1 -0
- package/dist/tests/unit/svip-document.test.js +137 -0
- package/dist/tests/unit/svip-document.test.js.map +1 -0
- package/dist/tests/unit/svip-editor.test.d.ts +1 -0
- package/dist/tests/unit/svip-editor.test.js +275 -0
- package/dist/tests/unit/svip-editor.test.js.map +1 -0
- package/dist/tests/unit/svip-tick.test.d.ts +1 -0
- package/dist/tests/unit/svip-tick.test.js +66 -0
- package/dist/tests/unit/svip-tick.test.js.map +1 -0
- package/dist/tools/common.d.ts +80 -0
- package/dist/tools/common.js +167 -0
- package/dist/tools/common.js.map +1 -0
- package/dist/tools/control.d.ts +21 -0
- package/dist/tools/control.js +376 -0
- package/dist/tools/control.js.map +1 -0
- package/dist/tools/data.d.ts +37 -0
- package/dist/tools/data.js +680 -0
- package/dist/tools/data.js.map +1 -0
- package/dist/tools/definition.d.ts +60 -0
- package/dist/tools/definition.js +44 -0
- package/dist/tools/definition.js.map +1 -0
- package/dist/tools/degrade.d.ts +3 -0
- package/dist/tools/degrade.js +30 -0
- package/dist/tools/degrade.js.map +1 -0
- package/dist/tools/guide.d.ts +10 -0
- package/dist/tools/guide.js +66 -0
- package/dist/tools/guide.js.map +1 -0
- package/dist/tools/open.d.ts +3 -0
- package/dist/tools/open.js +74 -0
- package/dist/tools/open.js.map +1 -0
- package/dist/tools/probe.d.ts +8 -0
- package/dist/tools/probe.js +132 -0
- package/dist/tools/probe.js.map +1 -0
- package/dist/tools/registry.d.ts +10 -0
- package/dist/tools/registry.js +16 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/version.d.ts +4 -0
- package/dist/version.js +19 -0
- package/dist/version.js.map +1 -0
- package/docs/DESIGN.md +553 -0
- package/docs/RECON-GUI.md +271 -0
- package/docs/RECON.md +339 -0
- package/docs/SPEC.md +786 -0
- package/docs/WINE.md +192 -0
- package/examples/.scratch/01-midi-to-wudi/wudi-demo.svip3 +0 -0
- package/examples/.scratch/01-midi-to-wudi/wudi-melody.mid +0 -0
- package/examples/.scratch/01-midi-to-wudi/wudi-roundtrip.mid +0 -0
- package/examples/.scratch/02-three-singers/melody.chenziyu.svip3 +0 -0
- package/examples/.scratch/02-three-singers/melody.hechang.svip3 +0 -0
- package/examples/.scratch/02-three-singers/melody.svip3 +0 -0
- package/examples/.scratch/02-three-singers/melody.wudi.svip3 +0 -0
- package/examples/.scratch/03-velocity-diction/phrase.svip3 +0 -0
- package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-200 +0 -0
- package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-203 +0 -0
- package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-204 +0 -0
- package/examples/.scratch/04-unknown-dialog-abort/dialog-demo.svip3 +0 -0
- package/examples/01-midi-to-wudi.mjs +144 -0
- package/examples/02-three-singers-compare.mjs +140 -0
- package/examples/03-velocity-diction-rerender.mjs +160 -0
- package/examples/04-unknown-dialog-abort.mjs +126 -0
- package/examples/README.md +101 -0
- package/examples/lib/harness.mjs +274 -0
- package/fixtures/midi/melody-480.mid +0 -0
- package/fixtures/midi/melody-96.mid +0 -0
- package/fixtures/svip/1.svip3 +0 -0
- package/fixtures/svip/3.svip3 +0 -0
- package/fixtures/uia/README.md +37 -0
- package/fixtures/uia/export-dialog.snapshot.json +446 -0
- package/fixtures/uia/main-window.snapshot.json +2997 -0
- package/fixtures/uia/open-dialog.snapshot.json +103 -0
- package/icon.svg +10 -0
- package/package.json +84 -4
- package/proto/xstudio.proto +128 -0
- package/scripts/check-contract.mjs +184 -0
- package/scripts/entry-smoke.mjs +26 -0
- package/scripts/inspect-svip.mjs +161 -0
- package/scripts/setup-venv.sh +93 -0
- package/scripts/wine-gui-probe.py +302 -0
- package/sidecar/xstudio_uia.py +1478 -0
- package/skills/xstudio-gui/SKILL.md +194 -0
- package/skills/xstudio-gui/assertions.md +103 -0
- package/skills/xstudio-gui/dialogs.md +90 -0
- package/skills/xstudio-gui/gestures.md +158 -0
- package/skills/xstudio-gui/ui-map.json +242 -0
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render orchestration for the F layer: open a project in X Studio, start the
|
|
3
|
+
* cloud render/playback, wait for it, and walk the export dialog.
|
|
4
|
+
*
|
|
5
|
+
* Everything goes through the {@link GuiSidecar} interface (UIA only, never
|
|
6
|
+
* coordinates) and every step is budgeted with `config.gui.*`:
|
|
7
|
+
* `maxSteps` (actions), `maxWallClockMs` (wall clock) and
|
|
8
|
+
* `maxConsecutiveFailures` (abort after repeated GUI failures).
|
|
9
|
+
*
|
|
10
|
+
* The orchestrator is deliberately conservative:
|
|
11
|
+
* - a destructive dialog is never confirmed; it aborts and hands control back;
|
|
12
|
+
* - an unknown modal dialog aborts with `DIALOG_UNKNOWN` so the adapter can
|
|
13
|
+
* diagnose it with Tavily / answer it explicitly;
|
|
14
|
+
* - cloud/network error text is mapped to `CLOUD_UNREACHABLE`;
|
|
15
|
+
* - export paths that the current export dialog cannot represent (e.g. an
|
|
16
|
+
* unverified folder-picker flow) fail with `EXPORT_FAILED` and a manual
|
|
17
|
+
* instruction instead of silently exporting to the wrong directory.
|
|
18
|
+
* @module xstudio-linux/core/render/orchestrator
|
|
19
|
+
*/
|
|
20
|
+
import { basename, extname, normalize } from 'node:path';
|
|
21
|
+
import { ErrorCode, XStudioError } from '../../shared/errors.js';
|
|
22
|
+
import { createWinePathConverter } from '../../host/wine-paths.js';
|
|
23
|
+
import { launchOpenProject } from '../../host/wine-launch.js';
|
|
24
|
+
import { findNewestArtifact } from './artifacts.js';
|
|
25
|
+
/** Default limits when the config object is absent/partial. */
|
|
26
|
+
const DEFAULT_LIMITS = { maxSteps: 60, maxWallClockMs: 180_000, maxConsecutiveFailures: 2, requireForeground: true };
|
|
27
|
+
/** Read `config.gui.*` defensively (the constructor receives `unknown`). */
|
|
28
|
+
function readLimits(config) {
|
|
29
|
+
const record = config;
|
|
30
|
+
const gui = record?.gui ?? {};
|
|
31
|
+
const positive = (value, fallback) => (typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : fallback);
|
|
32
|
+
return {
|
|
33
|
+
maxSteps: positive(gui.maxSteps, DEFAULT_LIMITS.maxSteps),
|
|
34
|
+
maxWallClockMs: positive(gui.maxWallClockMs, DEFAULT_LIMITS.maxWallClockMs),
|
|
35
|
+
maxConsecutiveFailures: Math.max(1, Math.floor(positive(gui.maxConsecutiveFailures, DEFAULT_LIMITS.maxConsecutiveFailures))),
|
|
36
|
+
requireForeground: typeof gui.requireForeground === 'boolean' ? gui.requireForeground : DEFAULT_LIMITS.requireForeground,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Text that means the cloud render backend cannot be reached. */
|
|
40
|
+
const CLOUD_ERROR_PATTERN = /网络|连接失败|无法连接|连接不上|服务器|云端|断网|离线|超时|network|connect|offline|server/i;
|
|
41
|
+
/** Text that means the dialog wants a human to sign in. */
|
|
42
|
+
const LOGIN_PATTERN = /登录|登陆|扫码|二维码|账号|sign\s*in|log\s*in|login|password|验证码/i;
|
|
43
|
+
/** Text of a dialog that must never be confirmed automatically. */
|
|
44
|
+
const DESTRUCTIVE_DIALOG_PATTERN = /保存|覆盖|删除|退出|卸载|save|overwrite|delete|quit|exit/i;
|
|
45
|
+
/** Button names that answer a dialog safely / affirmatively. */
|
|
46
|
+
const CANCEL_BUTTONS = /取消|关闭|否|cancel|close|no/i;
|
|
47
|
+
/** Detect the "project already open" state from the window title. */
|
|
48
|
+
function titleShowsProject(title, projectPath) {
|
|
49
|
+
if (title === null || title === '')
|
|
50
|
+
return false;
|
|
51
|
+
const projectName = basename(projectPath);
|
|
52
|
+
return title.toLowerCase().includes(projectName.toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
/** Normalize a path for display/compare (case-insensitive, forward slashes). */
|
|
55
|
+
function normalizeComparable(value) {
|
|
56
|
+
return normalize(value).replace(/\\/g, '/').replace(/\/+$/, '').toLowerCase();
|
|
57
|
+
}
|
|
58
|
+
/** True when `element` (or the snapshot) contains the given text. */
|
|
59
|
+
function elementText(element) {
|
|
60
|
+
return `${element.name ?? ''} ${element.value ?? ''} ${element.automationId ?? ''}`;
|
|
61
|
+
}
|
|
62
|
+
/** Errors raised by the sidecar are re-classified inside the orchestrator. */
|
|
63
|
+
function asGuiError(error) {
|
|
64
|
+
if (error instanceof XStudioError)
|
|
65
|
+
return error;
|
|
66
|
+
const candidate = error;
|
|
67
|
+
if (candidate !== null && candidate !== undefined && typeof candidate.code === 'string' && typeof candidate.message === 'string') {
|
|
68
|
+
return new XStudioError(candidate.code, candidate.message, {
|
|
69
|
+
...(typeof candidate.hint === 'string' ? { hint: candidate.hint } : {}),
|
|
70
|
+
cause: error,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
/** Per-orchestration budget guard. */
|
|
76
|
+
class RunBudget {
|
|
77
|
+
limits;
|
|
78
|
+
now;
|
|
79
|
+
startedAt;
|
|
80
|
+
steps = 0;
|
|
81
|
+
consecutiveFailures = 0;
|
|
82
|
+
constructor(limits, now) {
|
|
83
|
+
this.limits = limits;
|
|
84
|
+
this.now = now;
|
|
85
|
+
this.startedAt = now();
|
|
86
|
+
}
|
|
87
|
+
/** Elapsed milliseconds since the orchestration started. */
|
|
88
|
+
elapsedMs() {
|
|
89
|
+
return Math.max(0, this.now() - this.startedAt);
|
|
90
|
+
}
|
|
91
|
+
/** Remaining wall-clock budget in milliseconds. */
|
|
92
|
+
remainingMs() {
|
|
93
|
+
return Math.max(0, this.limits.maxWallClockMs - this.elapsedMs());
|
|
94
|
+
}
|
|
95
|
+
/** Throw `GUI_BUDGET_EXCEEDED` when the wall clock is exhausted. */
|
|
96
|
+
checkClock() {
|
|
97
|
+
if (this.elapsedMs() >= this.limits.maxWallClockMs) {
|
|
98
|
+
throw new XStudioError(ErrorCode.GUI_BUDGET_EXCEEDED, `GUI orchestration exceeded gui.maxWallClockMs (${this.limits.maxWallClockMs}ms).`, {
|
|
99
|
+
hint: 'The render/export may still be running; call xstudio_preview_wait or xstudio_export_run again (the file may already exist).',
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Check the wall clock and count one mutating GUI action. */
|
|
104
|
+
step(label) {
|
|
105
|
+
this.checkClock();
|
|
106
|
+
this.steps += 1;
|
|
107
|
+
if (this.steps > this.limits.maxSteps) {
|
|
108
|
+
throw new XStudioError(ErrorCode.GUI_BUDGET_EXCEEDED, `GUI orchestration exceeded gui.maxSteps (${this.limits.maxSteps}) while running '${label}'.`, {
|
|
109
|
+
hint: 'Split the task into smaller steps, or raise gui.maxSteps in the plugin config.',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Record a GUI failure; throws when the consecutive-failure budget is spent. */
|
|
114
|
+
recordFailure(detail) {
|
|
115
|
+
this.consecutiveFailures += 1;
|
|
116
|
+
if (this.consecutiveFailures >= this.limits.maxConsecutiveFailures) {
|
|
117
|
+
throw new XStudioError(ErrorCode.GUI_BUDGET_EXCEEDED, `GUI orchestration aborted after ${this.consecutiveFailures} consecutive failures (last: ${detail}).`, {
|
|
118
|
+
hint: 'Close unexpected dialogs manually, then retry with a fresh snapshot.',
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Reset the consecutive-failure counter after a successful step. */
|
|
123
|
+
recordSuccess() {
|
|
124
|
+
this.consecutiveFailures = 0;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Classify a dialog by its text. */
|
|
128
|
+
function classifyDialog(dialog) {
|
|
129
|
+
const text = `${dialog.title} ${dialog.text}`;
|
|
130
|
+
if (DESTRUCTIVE_DIALOG_PATTERN.test(text))
|
|
131
|
+
return 'destructive';
|
|
132
|
+
if (CLOUD_ERROR_PATTERN.test(text))
|
|
133
|
+
return 'cloud';
|
|
134
|
+
if (LOGIN_PATTERN.test(text))
|
|
135
|
+
return 'login';
|
|
136
|
+
if (/下载|资源|音色|歌手模型|更新|安装|初始化|加载/i.test(text))
|
|
137
|
+
return 'download';
|
|
138
|
+
return 'unknown';
|
|
139
|
+
}
|
|
140
|
+
/** Find one element by predicate. */
|
|
141
|
+
function findElement(snapshot, predicate) {
|
|
142
|
+
return snapshot.elements.find(predicate);
|
|
143
|
+
}
|
|
144
|
+
/** Find all elements matching a predicate. */
|
|
145
|
+
function filterElements(snapshot, predicate) {
|
|
146
|
+
return snapshot.elements.filter(predicate);
|
|
147
|
+
}
|
|
148
|
+
/** Find the main window root element of a snapshot. */
|
|
149
|
+
function mainRoot(snapshot) {
|
|
150
|
+
return snapshot.elements[0];
|
|
151
|
+
}
|
|
152
|
+
/** Find a top-level window element by title (dialogs, popups). */
|
|
153
|
+
function findWindowRoot(snapshot, title) {
|
|
154
|
+
const elements = snapshot.elements;
|
|
155
|
+
const fromAdditional = snapshot.additionalWindows?.find((window) => window.title === title);
|
|
156
|
+
if (fromAdditional !== undefined) {
|
|
157
|
+
return elements.find((element) => element.ref === fromAdditional.ref);
|
|
158
|
+
}
|
|
159
|
+
return elements.find((element) => element.depth === 0 && element.controlType === 'Window' && element.name === title);
|
|
160
|
+
}
|
|
161
|
+
/** Elements belonging to one top-level window. */
|
|
162
|
+
function elementsOfWindow(snapshot, windowRef) {
|
|
163
|
+
const elements = snapshot.elements;
|
|
164
|
+
const scoped = elements.filter((element) => element.windowRef === windowRef);
|
|
165
|
+
if (scoped.length > 1)
|
|
166
|
+
return scoped;
|
|
167
|
+
// Fallback when the sidecar did not attach windowRef: search by dialog
|
|
168
|
+
// AutomationId prefix (Qt dialogs are named after their parent window).
|
|
169
|
+
const root = elements.find((element) => element.ref === windowRef);
|
|
170
|
+
if (root === undefined || root.automationId === '')
|
|
171
|
+
return elements;
|
|
172
|
+
return elements.filter((element) => element.ref === windowRef || element.automationId.startsWith(`${root.automationId}.`));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Orchestrates the X Studio GUI through a {@link GuiSidecar}.
|
|
176
|
+
*
|
|
177
|
+
* One instance is shared by `xstudio_preview_trigger`, `xstudio_preview_wait`
|
|
178
|
+
* and `xstudio_export_run` (see `src/services.ts`).
|
|
179
|
+
*/
|
|
180
|
+
export class RenderOrchestrator {
|
|
181
|
+
sidecar;
|
|
182
|
+
limits;
|
|
183
|
+
now;
|
|
184
|
+
sleep;
|
|
185
|
+
pollIntervalMs;
|
|
186
|
+
artifactWaitMs;
|
|
187
|
+
/** Converts host paths to the drive-letter form X Studio sees under Wine. */
|
|
188
|
+
winePaths;
|
|
189
|
+
readTitle;
|
|
190
|
+
wineBin;
|
|
191
|
+
installDir;
|
|
192
|
+
winePrefix;
|
|
193
|
+
constructor(deps) {
|
|
194
|
+
this.sidecar = deps.sidecar;
|
|
195
|
+
this.limits = readLimits(deps.config);
|
|
196
|
+
this.now = deps.now ?? (() => Date.now());
|
|
197
|
+
this.sleep = deps.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
198
|
+
this.pollIntervalMs = Math.max(10, deps.pollIntervalMs ?? 500);
|
|
199
|
+
this.artifactWaitMs = Math.max(1_000, deps.artifactWaitMs ?? 60_000);
|
|
200
|
+
this.winePaths = deps.winePaths ?? createWinePathConverter();
|
|
201
|
+
this.readTitle = deps.readTitle ?? (async () => {
|
|
202
|
+
try {
|
|
203
|
+
return (await this.sidecar.probe()).windowTitle;
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
this.wineBin = deps.wineBin ?? process.env.XSTUDIO_WINE ?? 'wine';
|
|
210
|
+
// installDir/prefix come from the config object when present (unknown-config callers get '').
|
|
211
|
+
const configRecord = deps.config;
|
|
212
|
+
this.installDir = typeof configRecord?.xstudioInstallDir === 'string' && configRecord.xstudioInstallDir !== '' ? configRecord.xstudioInstallDir : process.env.XSTUDIO_INSTALL_DIR ?? '';
|
|
213
|
+
this.winePrefix = typeof configRecord?.wine?.prefix === 'string' && configRecord.wine.prefix !== '' ? configRecord.wine.prefix : '~/.wine';
|
|
214
|
+
}
|
|
215
|
+
// ------------------------------------------------------------------
|
|
216
|
+
// Budgeted primitive wrappers
|
|
217
|
+
// ------------------------------------------------------------------
|
|
218
|
+
async snapshotOrThrow(budget) {
|
|
219
|
+
budget.checkClock();
|
|
220
|
+
try {
|
|
221
|
+
const snapshot = (await this.sidecar.snapshot({ maxElements: 600 }));
|
|
222
|
+
budget.recordSuccess();
|
|
223
|
+
return snapshot;
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
budget.recordFailure(String(error?.message ?? error));
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async actOrThrow(budget, evidence, ref, op, value, label) {
|
|
231
|
+
budget.step(`${op} ${label ?? ref}`);
|
|
232
|
+
try {
|
|
233
|
+
const result = await this.sidecar.act({ ref, op, ...(value === undefined ? {} : { value }) });
|
|
234
|
+
if (!result.ok)
|
|
235
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, result.detail ?? `UIA action '${op}' on ${ref} failed.`);
|
|
236
|
+
budget.recordSuccess();
|
|
237
|
+
evidence.push(`${op}:${label ?? ref}${result.detail === undefined ? '' : ` (${result.detail})`}`);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
budget.recordFailure(`${op} ${label ?? ref}: ${String(error?.message ?? error)}`);
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async assertOrThrow(budget, evidence, assertion, label) {
|
|
245
|
+
budget.step(`assert ${label}`);
|
|
246
|
+
try {
|
|
247
|
+
const result = await this.sidecar.assert(assertion);
|
|
248
|
+
if (!result.ok)
|
|
249
|
+
throw new XStudioError(ErrorCode.GUI_ASSERT_FAILED, result.detail ?? `UI assertion '${label}' failed.`, { hint: result.actual === undefined ? undefined : `Actual: ${result.actual}` });
|
|
250
|
+
budget.recordSuccess();
|
|
251
|
+
evidence.push(`assert:${label}`);
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
budget.recordFailure(`assert ${label}: ${String(error?.message ?? error)}`);
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async readDialogOrNull(budget) {
|
|
259
|
+
budget.checkClock();
|
|
260
|
+
try {
|
|
261
|
+
const dialog = await this.sidecar.readDialog();
|
|
262
|
+
budget.recordSuccess();
|
|
263
|
+
return dialog;
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
budget.recordFailure(String(error?.message ?? error));
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/** Cancel any dialogs left open by a failed orchestration (best effort). */
|
|
271
|
+
async cancelDialogsQuietly() {
|
|
272
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
273
|
+
try {
|
|
274
|
+
const dialog = await this.sidecar.readDialog();
|
|
275
|
+
if (dialog === null)
|
|
276
|
+
return;
|
|
277
|
+
await this.sidecar.respondDialog('cancel');
|
|
278
|
+
await this.sleep(Math.min(200, this.pollIntervalMs));
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/** Poll `readDialog()` until it satisfies `done`, or the timeout elapses. */
|
|
286
|
+
async waitForDialog(budget, done, timeoutMs, evidence) {
|
|
287
|
+
const startedAt = this.now();
|
|
288
|
+
let last = null;
|
|
289
|
+
for (;;) {
|
|
290
|
+
this.checkOrThrow(budget);
|
|
291
|
+
last = await this.readDialogOrNull(budget);
|
|
292
|
+
if (done(last))
|
|
293
|
+
return last;
|
|
294
|
+
if (this.now() - startedAt >= timeoutMs)
|
|
295
|
+
return last;
|
|
296
|
+
await this.sleep(this.pollIntervalMs);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
checkOrThrow(budget) {
|
|
300
|
+
budget.checkClock();
|
|
301
|
+
}
|
|
302
|
+
/** Probe the app and enforce the foreground rule for mutating flows. */
|
|
303
|
+
async probeForAction(budget, evidence, options = {}) {
|
|
304
|
+
budget.checkClock();
|
|
305
|
+
const probe = await this.sidecar.probe(options);
|
|
306
|
+
if (!probe.running) {
|
|
307
|
+
throw new XStudioError(ErrorCode.XSTUDIO_NOT_RUNNING, 'X Studio is not running; GUI orchestration cannot act.', {
|
|
308
|
+
hint: 'Start X Studio manually, or use xstudio_guide_print for the manual steps.',
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
evidence.push(`probe: pid=${probe.pid ?? '?'} foreground=${probe.foreground} title=${probe.windowTitle ?? '?'}`);
|
|
312
|
+
if (this.limits.requireForeground && !probe.foreground) {
|
|
313
|
+
throw new XStudioError(ErrorCode.GUI_FOCUS_LOST, 'X Studio does not own the foreground window, so the plugin will not steal focus.', {
|
|
314
|
+
hint: 'Bring the X Studio window to the foreground and retry, or set gui.requireForeground=false when running unattended.',
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return probe;
|
|
318
|
+
}
|
|
319
|
+
// ------------------------------------------------------------------
|
|
320
|
+
// Menu / dialog discovery helpers
|
|
321
|
+
// ------------------------------------------------------------------
|
|
322
|
+
findMenuBarItem(snapshot, name) {
|
|
323
|
+
return findElement(snapshot, (element) => element.controlType === 'MenuItem' && element.name === name);
|
|
324
|
+
}
|
|
325
|
+
findSubmenuItem(snapshot, name) {
|
|
326
|
+
// The popup items become descendants of the expanded menu item; the parent
|
|
327
|
+
// ref field the sidecar attaches makes this unambiguous when present.
|
|
328
|
+
const candidates = filterElements(snapshot, (element) => element.controlType === 'MenuItem' && element.name === name);
|
|
329
|
+
if (candidates.length === 0)
|
|
330
|
+
return undefined;
|
|
331
|
+
const menuBarRef = findElement(snapshot, (element) => element.controlType === 'MenuBar')?.ref;
|
|
332
|
+
const nonTopLevel = candidates.filter((element) => element.parentRef !== undefined && element.parentRef !== menuBarRef);
|
|
333
|
+
return nonTopLevel[0] ?? candidates[0];
|
|
334
|
+
}
|
|
335
|
+
findDialogEdit(snapshot, windowRef) {
|
|
336
|
+
const scoped = elementsOfWindow(snapshot, windowRef);
|
|
337
|
+
const byAutomationId = scoped.find((element) => element.automationId === '1148');
|
|
338
|
+
if (byAutomationId !== undefined)
|
|
339
|
+
return byAutomationId;
|
|
340
|
+
const editLike = scoped.filter((element) => element.className === 'Edit' || element.controlType === 'Edit');
|
|
341
|
+
const writable = editLike.find((element) => element.patterns.includes('Value'));
|
|
342
|
+
return writable ?? editLike[0];
|
|
343
|
+
}
|
|
344
|
+
findDialogButton(snapshot, windowRef, pattern) {
|
|
345
|
+
const scoped = elementsOfWindow(snapshot, windowRef);
|
|
346
|
+
const buttonLike = scoped.filter((element) => element.className === 'Button' || element.className === 'QPushButton' || element.controlType === 'Button' || element.controlType === 'RadioButton');
|
|
347
|
+
return buttonLike.find((element) => pattern.test(element.name));
|
|
348
|
+
}
|
|
349
|
+
findComboLabel(snapshot, windowRef, suffix) {
|
|
350
|
+
return elementsOfWindow(snapshot, windowRef).find((element) => element.automationId.endsWith(suffix));
|
|
351
|
+
}
|
|
352
|
+
// ------------------------------------------------------------------
|
|
353
|
+
// Project opening / render start
|
|
354
|
+
// ------------------------------------------------------------------
|
|
355
|
+
/** Make sure `projectPath` is the project open in X Studio. */
|
|
356
|
+
async ensureProjectOpen(projectPath, budget, evidence) {
|
|
357
|
+
// Ask the window manager to raise X Studio first: cheap, tree-free, and it
|
|
358
|
+
// fixes both the iconic (minimized) and focus-lost failure modes.
|
|
359
|
+
const probe = await this.probeForAction(budget, evidence, { activate: true });
|
|
360
|
+
if (titleShowsProject(probe.windowTitle, projectPath)) {
|
|
361
|
+
evidence.push(`project-open: ${basename(projectPath)} is already open`);
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
// Wine fast path (verified live): relaunching the exe with the file as an
|
|
365
|
+
// argument makes the single-instance app open it — no keyboard, no tree.
|
|
366
|
+
// Only when the install dir is resolvable; otherwise skip silently.
|
|
367
|
+
if (probe.backend !== 'atspi' && this.installDir !== '') {
|
|
368
|
+
budget.step('wine-launch open');
|
|
369
|
+
try {
|
|
370
|
+
const launch = await launchOpenProject({
|
|
371
|
+
installDir: this.installDir,
|
|
372
|
+
winePrefix: this.winePrefix,
|
|
373
|
+
projectPath,
|
|
374
|
+
wineBin: this.wineBin,
|
|
375
|
+
titleTimeoutMs: 30_000,
|
|
376
|
+
readTitle: this.readTitle,
|
|
377
|
+
sleep: this.sleep,
|
|
378
|
+
});
|
|
379
|
+
evidence.push(...launch.steps);
|
|
380
|
+
if (launch.opened) {
|
|
381
|
+
budget.recordSuccess();
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
budget.recordFailure('wine-launch fallback did not open the project');
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
budget.recordFailure(`wine-launch fallback failed: ${String(error?.message ?? error)}`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const menuSnapshot = await this.snapshotOrThrow(budget);
|
|
391
|
+
const fileMenu = this.findMenuBarItem(menuSnapshot, '文件');
|
|
392
|
+
if (fileMenu === undefined) {
|
|
393
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'The X Studio File menu (文件) was not found in the UIA tree.', {
|
|
394
|
+
hint: 'Check skills/xstudio-gui/ui-map.json for the current build; the app may still be starting.',
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
await this.actOrThrow(budget, evidence, fileMenu.ref, 'expand', undefined, 'file-menu');
|
|
398
|
+
const expandedSnapshot = await this.snapshotOrThrow(budget);
|
|
399
|
+
const openProjectItem = this.findSubmenuItem(expandedSnapshot, '打开工程');
|
|
400
|
+
if (openProjectItem === undefined) {
|
|
401
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'File > Open project (打开工程) was not found after expanding the File menu.', {
|
|
402
|
+
hint: 'The menu layout may differ; inspect the UI with xstudio_ui_snapshot and update the skill ui-map.',
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
await this.actOrThrow(budget, evidence, openProjectItem.ref, 'invoke_pattern', undefined, 'open-project-menu');
|
|
406
|
+
const dialog = await this.waitForDialog(budget, (current) => current !== null, 15_000, evidence);
|
|
407
|
+
if (dialog === null) {
|
|
408
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'The open-project dialog did not appear after choosing 打开工程.', {
|
|
409
|
+
hint: 'Take xstudio_ui_snapshot to inspect the app state; a modal dialog may be blocking the menu.',
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
evidence.push(`dialog-open: ${dialog.title} (${dialog.windowClass})`);
|
|
413
|
+
const dialogSnapshot = await this.snapshotOrThrow(budget);
|
|
414
|
+
const dialogRoot = findWindowRoot(dialogSnapshot, dialog.title);
|
|
415
|
+
if (dialogRoot === undefined) {
|
|
416
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `The open-project dialog '${dialog.title}' was not present in the UIA snapshot.`, {
|
|
417
|
+
hint: 'Handle the dialog manually, or report this as a GUI regression.',
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
const edit = this.findDialogEdit(dialogSnapshot, dialogRoot.ref);
|
|
421
|
+
if (edit === undefined) {
|
|
422
|
+
await this.cancelDialogsQuietly();
|
|
423
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `No filename edit was reachable in the '${dialog.title}' file dialog.`, {
|
|
424
|
+
hint: 'Use xstudio_guide_print: open the project with File > Open project (Ctrl+O) manually.',
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
// Typing the absolute path makes the dialog independent of the current
|
|
428
|
+
// folder shown in the left tree.
|
|
429
|
+
await this.actOrThrow(budget, evidence, edit.ref, 'set_text', this.winePaths.toWine(projectPath), 'project-path');
|
|
430
|
+
const updatedSnapshot = await this.snapshotOrThrow(budget);
|
|
431
|
+
const openButton = this.findDialogButton(updatedSnapshot, dialogRoot.ref, /^(打开|open)/i);
|
|
432
|
+
if (openButton === undefined) {
|
|
433
|
+
await this.cancelDialogsQuietly();
|
|
434
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `The '${dialog.title}' dialog has no Open (打开) button.`, { hint: 'Handle the dialog manually.' });
|
|
435
|
+
}
|
|
436
|
+
await this.actOrThrow(budget, evidence, openButton.ref, 'invoke_pattern', undefined, 'confirm-open');
|
|
437
|
+
const opened = await this.waitUntil(budget, async () => {
|
|
438
|
+
const current = await this.readDialogOrNull(budget);
|
|
439
|
+
if (current !== null)
|
|
440
|
+
return false;
|
|
441
|
+
const currentProbe = await this.sidecar.probe();
|
|
442
|
+
return titleShowsProject(currentProbe.windowTitle, projectPath);
|
|
443
|
+
}, 20_000);
|
|
444
|
+
if (!opened) {
|
|
445
|
+
await this.cancelDialogsQuietly();
|
|
446
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `X Studio did not open '${basename(projectPath)}' within 20s.`, {
|
|
447
|
+
hint: 'Check that the project path exists and is a .svip3 file; a dialog may still be open.',
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
evidence.push(`project-open: ${basename(projectPath)}`);
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
/** Poll an arbitrary condition until it is true or the timeout elapses. */
|
|
454
|
+
async waitUntil(budget, condition, timeoutMs) {
|
|
455
|
+
const startedAt = this.now();
|
|
456
|
+
for (;;) {
|
|
457
|
+
if (await condition())
|
|
458
|
+
return true;
|
|
459
|
+
if (this.now() - startedAt >= timeoutMs)
|
|
460
|
+
return false;
|
|
461
|
+
this.checkOrThrow(budget);
|
|
462
|
+
await this.sleep(this.pollIntervalMs);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** Start playback (which triggers the cloud render). */
|
|
466
|
+
/** True when a toast window is visible (window-level completion/error signal). */
|
|
467
|
+
async readTitleHasToast() {
|
|
468
|
+
try {
|
|
469
|
+
const probe = (await this.sidecar.probe());
|
|
470
|
+
return probe.toastSeen === true;
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
async startPlayback(budget, evidence) {
|
|
477
|
+
let snapshot;
|
|
478
|
+
try {
|
|
479
|
+
snapshot = await this.snapshotOrThrow(budget);
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
// 树不可达(Wine 默认):Space 是应用级播放/暂停键——send_keys 直接发送。
|
|
483
|
+
budget.recordFailure(`snapshot unavailable for playback: ${String(error?.message ?? error)}`);
|
|
484
|
+
evidence.push('playback: tree unavailable; falling back to the keyboard Space key');
|
|
485
|
+
budget.step('send_keys space');
|
|
486
|
+
const result = await this.sidecar.act({ ref: 'unused', op: 'send_keys', value: ' ' });
|
|
487
|
+
if (!result.ok)
|
|
488
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, result.detail ?? 'The keyboard playback fallback failed.', {
|
|
489
|
+
hint: 'The keyboard channel could not reach X Studio; press Space manually or use xstudio_guide_print.',
|
|
490
|
+
});
|
|
491
|
+
budget.recordSuccess();
|
|
492
|
+
evidence.push(`render: playback via keyboard (${result.detail ?? 'sent'})`);
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
const playButton = findElement(snapshot, (element) => element.automationId.endsWith('play_or_pause_btn_')) ??
|
|
496
|
+
findElement(snapshot, (element) => /^(播放|play|暂停|pause)$/i.test(element.name));
|
|
497
|
+
if (playButton === undefined) {
|
|
498
|
+
// 元素在树里找不到时同样退回键盘 Space(树可达但布局变化的场景)。
|
|
499
|
+
evidence.push('playback: play button not found in the tree; falling back to the keyboard Space key');
|
|
500
|
+
budget.step('send_keys space');
|
|
501
|
+
const result = await this.sidecar.act({ ref: 'unused', op: 'send_keys', value: ' ' });
|
|
502
|
+
if (!result.ok)
|
|
503
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'The playback (render) button was not found in the UIA tree, and the keyboard fallback failed.', {
|
|
504
|
+
hint: 'Inspect the window with xstudio_ui_snapshot; the transport bar layout may have changed.',
|
|
505
|
+
});
|
|
506
|
+
evidence.push(`render: playback via keyboard (${result.detail ?? 'sent'})`);
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
if (playButton.toggleState === 'On') {
|
|
510
|
+
evidence.push('render: playback is already running; not toggling it off');
|
|
511
|
+
return true;
|
|
512
|
+
}
|
|
513
|
+
await this.actOrThrow(budget, evidence, playButton.ref, 'invoke_pattern', undefined, 'play-render');
|
|
514
|
+
evidence.push('render: playback invoked (cloud render/playback requested)');
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
// ------------------------------------------------------------------
|
|
518
|
+
// Public API
|
|
519
|
+
// ------------------------------------------------------------------
|
|
520
|
+
/**
|
|
521
|
+
* Open `projectPath` in X Studio and start the cloud render (playback).
|
|
522
|
+
* Returns as soon as the action is dispatched; use {@link wait} to observe
|
|
523
|
+
* completion.
|
|
524
|
+
*/
|
|
525
|
+
async trigger(projectPath) {
|
|
526
|
+
const budget = new RunBudget(this.limits, this.now);
|
|
527
|
+
const evidence = [];
|
|
528
|
+
try {
|
|
529
|
+
const opened = await this.ensureProjectOpen(projectPath, budget, evidence);
|
|
530
|
+
const started = await this.startPlayback(budget, evidence);
|
|
531
|
+
return { opened, started, steps: evidence };
|
|
532
|
+
}
|
|
533
|
+
catch (error) {
|
|
534
|
+
await this.cancelDialogsQuietly();
|
|
535
|
+
const structured = asGuiError(error);
|
|
536
|
+
if (structured !== undefined)
|
|
537
|
+
throw structured;
|
|
538
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `Opening/starting the render failed: ${String(error?.message ?? error)}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Wait for the render started by {@link trigger} to finish.
|
|
543
|
+
* Returns `completed: false` with evidence on a plain timeout; unknown or
|
|
544
|
+
* destructive dialogs abort instead of being confirmed.
|
|
545
|
+
*/
|
|
546
|
+
async wait(timeoutMs) {
|
|
547
|
+
const budget = new RunBudget(this.limits, this.now);
|
|
548
|
+
const evidence = [];
|
|
549
|
+
const startedAt = this.now();
|
|
550
|
+
const requested = Math.max(1_000, Math.min(600_000, timeoutMs));
|
|
551
|
+
const effective = Math.min(requested, budget.remainingMs());
|
|
552
|
+
let sawPlaying = false;
|
|
553
|
+
let lastState = '';
|
|
554
|
+
// 树不可达(Wine 默认):渲染已由 trigger 启动,但进度无法自动观察。
|
|
555
|
+
// 轮询窗口标题/Toast(X 层可见)直到超时,证据里如实说明观察通道受限。
|
|
556
|
+
try {
|
|
557
|
+
const firstSnapshot = await this.snapshotOrThrow(budget);
|
|
558
|
+
if (firstSnapshot.treeAvailable === false) {
|
|
559
|
+
throw new XStudioError(ErrorCode.GUI_REF_NOT_FOUND, 'The accessibility tree is not exposed for this session.');
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
catch (error) {
|
|
563
|
+
budget.recordFailure(`wait: tree unavailable (${String(error?.message ?? error)})`);
|
|
564
|
+
const deadline = startedAt + effective;
|
|
565
|
+
let toastsSeen = 0;
|
|
566
|
+
for (;;) {
|
|
567
|
+
this.checkOrThrow(budget);
|
|
568
|
+
const probe = await this.sidecar.probe().catch(() => null);
|
|
569
|
+
if (probe !== null) {
|
|
570
|
+
evidence.push(`poll: title=${probe.windowTitle ?? '?'}`);
|
|
571
|
+
}
|
|
572
|
+
// Toast 弹出通常意味着完成或错误提示(窗口层唯一可见信号)。
|
|
573
|
+
const toastSeen = await this.readTitleHasToast();
|
|
574
|
+
if (toastSeen)
|
|
575
|
+
toastsSeen += 1;
|
|
576
|
+
if (this.now() >= deadline) {
|
|
577
|
+
evidence.push('window-level wait: no accessibility tree, playback progress is not machine-observable; confirm audibly or manually');
|
|
578
|
+
return { completed: false, elapsedMs: this.now() - startedAt, evidence };
|
|
579
|
+
}
|
|
580
|
+
await this.sleep(this.pollIntervalMs);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
try {
|
|
584
|
+
for (;;) {
|
|
585
|
+
this.checkOrThrow(budget);
|
|
586
|
+
const elapsed = this.now() - startedAt;
|
|
587
|
+
const dialog = await this.readDialogOrNull(budget);
|
|
588
|
+
if (dialog !== null) {
|
|
589
|
+
const dialogClass = classifyDialog(dialog);
|
|
590
|
+
evidence.push(`dialog: ${dialog.title} -> ${dialogClass}`);
|
|
591
|
+
if (dialogClass === 'destructive') {
|
|
592
|
+
await this.cancelDialogsQuietly();
|
|
593
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `A destructive dialog was detected ('${dialog.title}': ${dialog.text}). Refusing to confirm it.`, {
|
|
594
|
+
hint: 'Answer the dialog manually; the plugin never confirms save/overwrite/delete/quit dialogs.',
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
if (dialogClass === 'cloud') {
|
|
598
|
+
throw new XStudioError(ErrorCode.CLOUD_UNREACHABLE, `X Studio reports a cloud/network problem: ${dialog.text}`, {
|
|
599
|
+
hint: 'Check the network connection; X Studio renders on the vendor cloud.',
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
if (dialogClass === 'login') {
|
|
603
|
+
throw new XStudioError(ErrorCode.DIALOG_UNKNOWN, `A login dialog is blocking the render: ${dialog.text}`, {
|
|
604
|
+
hint: 'Sign in to X Studio manually, then retry.',
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
if (dialogClass === 'unknown') {
|
|
608
|
+
await this.cancelDialogsQuietly();
|
|
609
|
+
throw new XStudioError(ErrorCode.DIALOG_UNKNOWN, `An unknown dialog is blocking the render ('${dialog.title}': ${dialog.text}).`, {
|
|
610
|
+
hint: 'Call xstudio_ui_snapshot, then xstudio_ui_dialog_respond to inspect and answer it explicitly.',
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
// 'download': wait for it to finish on its own.
|
|
614
|
+
}
|
|
615
|
+
const snapshot = await this.snapshotOrThrow(budget);
|
|
616
|
+
const playButton = findElement(snapshot, (element) => element.automationId.endsWith('play_or_pause_btn_'));
|
|
617
|
+
const playing = playButton?.toggleState === 'On';
|
|
618
|
+
if (playing)
|
|
619
|
+
sawPlaying = true;
|
|
620
|
+
const progress = this.readProgress(snapshot);
|
|
621
|
+
const busyText = this.readBusyText(snapshot);
|
|
622
|
+
const toastError = this.readToastError(snapshot);
|
|
623
|
+
if (toastError !== undefined) {
|
|
624
|
+
throw new XStudioError(ErrorCode.CLOUD_UNREACHABLE, `X Studio reported a network/cloud error: ${toastError}`, {
|
|
625
|
+
hint: 'Check the network connection; X Studio renders on the vendor cloud.',
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
const state = `playing=${String(playing)}${progress === undefined ? '' : ` progress=${progress}`}${busyText === undefined ? '' : ` state=${busyText}`}`;
|
|
629
|
+
if (state !== lastState) {
|
|
630
|
+
evidence.push(`state: ${state}`);
|
|
631
|
+
lastState = state;
|
|
632
|
+
}
|
|
633
|
+
if (sawPlaying && !playing) {
|
|
634
|
+
return { completed: true, elapsedMs: this.now() - startedAt, evidence };
|
|
635
|
+
}
|
|
636
|
+
if (progress !== undefined && progress >= 1) {
|
|
637
|
+
return { completed: true, elapsedMs: this.now() - startedAt, evidence };
|
|
638
|
+
}
|
|
639
|
+
if (this.now() - startedAt >= effective) {
|
|
640
|
+
evidence.push(`timeout: ${effective}ms${effective < requested ? ' (capped by gui.maxWallClockMs)' : ''}`);
|
|
641
|
+
return { completed: false, elapsedMs: this.now() - startedAt, evidence };
|
|
642
|
+
}
|
|
643
|
+
await this.sleep(this.pollIntervalMs);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
catch (error) {
|
|
647
|
+
const structured = asGuiError(error);
|
|
648
|
+
if (structured !== undefined)
|
|
649
|
+
throw structured;
|
|
650
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `Waiting for the render failed: ${String(error?.message ?? error)}`);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/** Read a numeric progress value from the snapshot, when the app exposes one. */
|
|
654
|
+
readProgress(snapshot) {
|
|
655
|
+
const progressElement = findElement(snapshot, (element) => element.controlType === 'ProgressBar' || /progress/i.test(element.className));
|
|
656
|
+
if (progressElement === undefined)
|
|
657
|
+
return undefined;
|
|
658
|
+
const raw = progressElement.value ?? '';
|
|
659
|
+
const percent = Number.parseFloat(raw);
|
|
660
|
+
if (!Number.isFinite(percent))
|
|
661
|
+
return undefined;
|
|
662
|
+
// QProgressBar exposes 0..100; RangeValue patterns expose 0..1. Accept both.
|
|
663
|
+
if (percent > 1)
|
|
664
|
+
return Math.min(1, percent / 100);
|
|
665
|
+
return Math.max(0, percent);
|
|
666
|
+
}
|
|
667
|
+
/** Read a human "busy" hint from the snapshot (render/loading text, toast). */
|
|
668
|
+
readBusyText(snapshot) {
|
|
669
|
+
const candidate = findElement(snapshot, (element) => /渲染|合成|生成|处理中|加载中|等待|队列|下载/i.test(elementText(element)));
|
|
670
|
+
if (candidate === undefined)
|
|
671
|
+
return undefined;
|
|
672
|
+
const text = `${candidate.name ?? ''}${candidate.value === undefined ? '' : ` ${candidate.value}`}`.trim();
|
|
673
|
+
return text === '' ? undefined : text.slice(0, 120);
|
|
674
|
+
}
|
|
675
|
+
/** Scan toast windows for cloud/network error text. */
|
|
676
|
+
readToastError(snapshot) {
|
|
677
|
+
const toasts = (snapshot.additionalWindows ?? []).filter((window) => /toast/i.test(window.className) || /toast/i.test(window.title));
|
|
678
|
+
for (const toast of toasts) {
|
|
679
|
+
const scoped = elementsOfWindow(snapshot, toast.ref);
|
|
680
|
+
for (const element of scoped) {
|
|
681
|
+
const text = elementText(element);
|
|
682
|
+
if (CLOUD_ERROR_PATTERN.test(text)) {
|
|
683
|
+
const label = `${element.name ?? ''} ${element.value ?? ''}`.trim();
|
|
684
|
+
return (label === '' ? text : label).slice(0, 200);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return undefined;
|
|
689
|
+
}
|
|
690
|
+
// ------------------------------------------------------------------
|
|
691
|
+
// Export
|
|
692
|
+
// ------------------------------------------------------------------
|
|
693
|
+
/** Map a requested format to the label X Studio shows in the dialog. */
|
|
694
|
+
formatLabel(format) {
|
|
695
|
+
return format.toUpperCase();
|
|
696
|
+
}
|
|
697
|
+
/** Map a requested sample rate to the dialog label, e.g. "48.000 kHz". */
|
|
698
|
+
sampleRateLabel(sampleRate) {
|
|
699
|
+
return `${(sampleRate / 1000).toFixed(3)} kHz`;
|
|
700
|
+
}
|
|
701
|
+
/** Map a requested bitrate to the dialog label, e.g. "320k bps". */
|
|
702
|
+
bitrateLabel(bitrate) {
|
|
703
|
+
const numeric = Number.parseFloat(bitrate.replace(/[^0-9.]/g, ''));
|
|
704
|
+
return Number.isFinite(numeric) && numeric > 0 ? `${numeric}k bps` : bitrate;
|
|
705
|
+
}
|
|
706
|
+
/** Click a custom combo box and choose the item whose label matches. */
|
|
707
|
+
async selectComboOption(budget, evidence, comboRef, comboName, desired, labelSuffix) {
|
|
708
|
+
await this.actOrThrow(budget, evidence, comboRef, 'click', undefined, `combo:${comboName}`);
|
|
709
|
+
const startedAt = this.now();
|
|
710
|
+
while (this.now() - startedAt < 3_000) {
|
|
711
|
+
const snapshot = await this.snapshotOrThrow(budget);
|
|
712
|
+
const item = findElement(snapshot, (element) => element.controlType === 'MenuItem' && element.name.toLowerCase().includes(desired.toLowerCase()));
|
|
713
|
+
if (item !== undefined) {
|
|
714
|
+
await this.actOrThrow(budget, evidence, item.ref, 'invoke_pattern', undefined, `combo-item:${desired}`);
|
|
715
|
+
const after = await this.snapshotOrThrow(budget);
|
|
716
|
+
const label = findElement(after, (element) => element.automationId.endsWith(labelSuffix));
|
|
717
|
+
return (label?.name ?? '').toLowerCase().includes(desired.toLowerCase());
|
|
718
|
+
}
|
|
719
|
+
await this.sleep(Math.min(200, this.pollIntervalMs));
|
|
720
|
+
}
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
/** Find the open export-settings dialog, or throw. */
|
|
724
|
+
async openExportDialog(budget, evidence) {
|
|
725
|
+
const menuSnapshot = await this.snapshotOrThrow(budget);
|
|
726
|
+
const fileMenu = this.findMenuBarItem(menuSnapshot, '文件');
|
|
727
|
+
if (fileMenu === undefined)
|
|
728
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'The X Studio File menu (文件) was not found in the UIA tree.');
|
|
729
|
+
await this.actOrThrow(budget, evidence, fileMenu.ref, 'expand', undefined, 'file-menu');
|
|
730
|
+
const expanded = await this.snapshotOrThrow(budget);
|
|
731
|
+
const exportMenu = this.findSubmenuItem(expanded, '导出');
|
|
732
|
+
if (exportMenu === undefined) {
|
|
733
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'File > Export (导出) was not found in the File menu.', { hint: 'Use the transport Export button (导出) instead.' });
|
|
734
|
+
}
|
|
735
|
+
if (exportMenu.patterns.includes('ExpandCollapse')) {
|
|
736
|
+
await this.actOrThrow(budget, evidence, exportMenu.ref, 'expand', undefined, 'export-menu');
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
await this.actOrThrow(budget, evidence, exportMenu.ref, 'invoke_pattern', undefined, 'export-menu');
|
|
740
|
+
}
|
|
741
|
+
const submenu = await this.snapshotOrThrow(budget);
|
|
742
|
+
const exportAudio = this.findSubmenuItem(submenu, '导出音频');
|
|
743
|
+
if (exportAudio === undefined) {
|
|
744
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'File > Export > Export audio (导出音频) was not found.', { hint: 'Use the transport Export button (导出) instead.' });
|
|
745
|
+
}
|
|
746
|
+
await this.actOrThrow(budget, evidence, exportAudio.ref, 'invoke_pattern', undefined, 'export-audio-menu');
|
|
747
|
+
const dialog = await this.waitForDialog(budget, (current) => current !== null, 15_000, evidence);
|
|
748
|
+
if (dialog === null) {
|
|
749
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'The export settings dialog did not appear.', { hint: 'Take xstudio_ui_snapshot to inspect the app.' });
|
|
750
|
+
}
|
|
751
|
+
const dialogSnapshot = await this.snapshotOrThrow(budget);
|
|
752
|
+
const root = findWindowRoot(dialogSnapshot, dialog.title) ?? findWindowRoot(dialogSnapshot, 'ExportAudioSettingDialog');
|
|
753
|
+
if (root === undefined) {
|
|
754
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, `The export dialog '${dialog.title}' was not present in the UIA snapshot.`);
|
|
755
|
+
}
|
|
756
|
+
evidence.push(`dialog-export: ${dialog.title} (${dialog.windowClass})`);
|
|
757
|
+
return { dialog, root, snapshot: dialogSnapshot };
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Walk the export dialog and produce an audio file. Verifies the artifact on
|
|
761
|
+
* disk before reporting success.
|
|
762
|
+
*/
|
|
763
|
+
async runExport(request) {
|
|
764
|
+
const budget = new RunBudget(this.limits, this.now);
|
|
765
|
+
const evidence = [];
|
|
766
|
+
const startedAt = this.now();
|
|
767
|
+
try {
|
|
768
|
+
await this.probeForAction(budget, evidence);
|
|
769
|
+
await this.ensureProjectOpen(request.projectPath, budget, evidence);
|
|
770
|
+
let state = await this.openExportDialog(budget, evidence);
|
|
771
|
+
// File name: use the project name so the export is easy to associate.
|
|
772
|
+
const fileNameEdit = elementsOfWindow(state.snapshot, state.root.ref).find((element) => element.automationId.endsWith('file_name_edit'));
|
|
773
|
+
if (fileNameEdit !== undefined) {
|
|
774
|
+
const baseName = basename(request.projectPath, extname(request.projectPath));
|
|
775
|
+
await this.actOrThrow(budget, evidence, fileNameEdit.ref, 'set_text', baseName, 'export-file-name');
|
|
776
|
+
}
|
|
777
|
+
// Output directory: only adjustable through the (unverified) folder
|
|
778
|
+
// picker. If it already points at the requested directory we continue;
|
|
779
|
+
// otherwise we fail honestly instead of writing somewhere unexpected.
|
|
780
|
+
const pathLabel = elementsOfWindow(state.snapshot, state.root.ref).find((element) => element.automationId.endsWith('file_path_label'));
|
|
781
|
+
const currentDirectory = (pathLabel?.name ?? pathLabel?.value ?? '').trim();
|
|
782
|
+
if (currentDirectory !== '' && normalizeComparable(this.winePaths.toLinux(currentDirectory)) !== normalizeComparable(request.outDir)) {
|
|
783
|
+
const changed = await this.changeExportDirectory(budget, evidence, request.outDir);
|
|
784
|
+
if (!changed) {
|
|
785
|
+
await this.cancelDialogsQuietly();
|
|
786
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `The export directory is '${currentDirectory}', not the requested '${request.outDir}', and the folder-picker flow could not be driven automatically.`, { hint: 'Open the export dialog manually, pick the output folder, then retry; or set gui.exportDirApproved for a directory the dialog already shows.' });
|
|
787
|
+
}
|
|
788
|
+
state = await this.refreshExportDialog(budget, evidence, state.dialog.title);
|
|
789
|
+
}
|
|
790
|
+
// Format.
|
|
791
|
+
const formatLabel = this.findComboLabel(state.snapshot, state.root.ref, 'cbox1_text1_label');
|
|
792
|
+
const desiredFormat = this.formatLabel(request.format);
|
|
793
|
+
if (formatLabel !== undefined && !formatLabel.name.toLowerCase().includes(desiredFormat.toLowerCase())) {
|
|
794
|
+
const combo = elementsOfWindow(state.snapshot, state.root.ref).find((element) => element.automationId.endsWith('cbox1_parent_layout'));
|
|
795
|
+
if (combo === undefined)
|
|
796
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, 'The export format combo box is not reachable through UIA.');
|
|
797
|
+
const selected = await this.selectComboOption(budget, evidence, combo.ref, 'format', desiredFormat, 'cbox1_text1_label');
|
|
798
|
+
if (!selected) {
|
|
799
|
+
await this.cancelDialogsQuietly();
|
|
800
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `Could not switch the export format to ${desiredFormat} (combo popup items were not reachable).`, {
|
|
801
|
+
hint: 'Set the format manually in the export dialog; the combo popup is unverified in this build.',
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
state = await this.refreshExportDialog(budget, evidence, state.dialog.title);
|
|
805
|
+
}
|
|
806
|
+
// Sample rate (optional).
|
|
807
|
+
if (request.sampleRate !== undefined) {
|
|
808
|
+
const desiredRate = this.sampleRateLabel(request.sampleRate);
|
|
809
|
+
const rateLabel = this.findComboLabel(state.snapshot, state.root.ref, 'cbox3_text1_label');
|
|
810
|
+
if (rateLabel !== undefined && !rateLabel.name.toLowerCase().includes(desiredRate.toLowerCase())) {
|
|
811
|
+
const combo = elementsOfWindow(state.snapshot, state.root.ref).find((element) => element.automationId.endsWith('cbox3_parent_layout'));
|
|
812
|
+
if (combo !== undefined) {
|
|
813
|
+
const selected = await this.selectComboOption(budget, evidence, combo.ref, 'sample-rate', desiredRate, 'cbox3_text1_label');
|
|
814
|
+
if (!selected) {
|
|
815
|
+
await this.cancelDialogsQuietly();
|
|
816
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `Could not switch the sample rate to ${desiredRate}.`, { hint: 'Set the sample rate manually in the export dialog.' });
|
|
817
|
+
}
|
|
818
|
+
state = await this.refreshExportDialog(budget, evidence, state.dialog.title);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
// Bitrate (optional, lossy formats only).
|
|
823
|
+
if (request.bitrate !== undefined) {
|
|
824
|
+
const desiredBitrate = this.bitrateLabel(request.bitrate);
|
|
825
|
+
const bitrateLabel = this.findComboLabel(state.snapshot, state.root.ref, 'cbox2_text1_label');
|
|
826
|
+
if (bitrateLabel !== undefined && !bitrateLabel.name.toLowerCase().includes(desiredBitrate.toLowerCase())) {
|
|
827
|
+
const combo = elementsOfWindow(state.snapshot, state.root.ref).find((element) => element.automationId.endsWith('cbox2_parent_layout'));
|
|
828
|
+
if (combo !== undefined) {
|
|
829
|
+
const selected = await this.selectComboOption(budget, evidence, combo.ref, 'bitrate', desiredBitrate, 'cbox2_text1_label');
|
|
830
|
+
if (!selected) {
|
|
831
|
+
await this.cancelDialogsQuietly();
|
|
832
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `Could not switch the bitrate to ${desiredBitrate}.`, { hint: 'Set the bitrate manually in the export dialog.' });
|
|
833
|
+
}
|
|
834
|
+
state = await this.refreshExportDialog(budget, evidence, state.dialog.title);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
// Final pre-confirm checks: the format must match and no destructive
|
|
839
|
+
// dialog may be in front of us.
|
|
840
|
+
state = await this.refreshExportDialog(budget, evidence, state.dialog.title);
|
|
841
|
+
this.assertConfirmationSafe(state.snapshot, state.root.ref);
|
|
842
|
+
const confirmButton = this.findDialogButton(state.snapshot, state.root.ref, /^(确认|确定|ok|export|导出)/i);
|
|
843
|
+
if (confirmButton === undefined) {
|
|
844
|
+
await this.cancelDialogsQuietly();
|
|
845
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, 'The export dialog has no confirm (确认) button reachable through UIA.');
|
|
846
|
+
}
|
|
847
|
+
await this.actOrThrow(budget, evidence, confirmButton.ref, 'invoke_pattern', undefined, 'export-confirm');
|
|
848
|
+
const artifact = await this.awaitArtifact(budget, request, startedAt, evidence);
|
|
849
|
+
evidence.push(`artifact: ${artifact.path} (${artifact.bytes} bytes, ${artifact.format})`);
|
|
850
|
+
return { path: artifact.path, bytes: artifact.bytes, format: artifact.format };
|
|
851
|
+
}
|
|
852
|
+
catch (error) {
|
|
853
|
+
await this.cancelDialogsQuietly();
|
|
854
|
+
const structured = asGuiError(error);
|
|
855
|
+
if (structured !== undefined)
|
|
856
|
+
throw structured;
|
|
857
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `Export failed: ${String(error?.message ?? error)}`);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
/** Re-snapshot and re-find the export dialog root. */
|
|
861
|
+
async refreshExportDialog(budget, evidence, title) {
|
|
862
|
+
const dialog = await this.waitForDialog(budget, (current) => current !== null, 5_000, evidence);
|
|
863
|
+
if (dialog === null) {
|
|
864
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, 'The export dialog closed unexpectedly while it was being configured.');
|
|
865
|
+
}
|
|
866
|
+
const snapshot = await this.snapshotOrThrow(budget);
|
|
867
|
+
const root = findWindowRoot(snapshot, dialog.title) ?? findWindowRoot(snapshot, title);
|
|
868
|
+
if (root === undefined) {
|
|
869
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `The export dialog '${dialog.title}' could not be found in a fresh UIA snapshot.`);
|
|
870
|
+
}
|
|
871
|
+
return { dialog, root, snapshot };
|
|
872
|
+
}
|
|
873
|
+
/** Throw when a destructive dialog (or wrong format) is in front of the export confirm. */
|
|
874
|
+
assertConfirmationSafe(snapshot, windowRef) {
|
|
875
|
+
const scoped = elementsOfWindow(snapshot, windowRef);
|
|
876
|
+
// Only visible labels/values matter here: automation ids legitimately contain
|
|
877
|
+
// words like "file_save_bt" that must not be mistaken for a destructive prompt.
|
|
878
|
+
const text = scoped.map((element) => `${element.name ?? ''} ${element.value ?? ''}`).join(' ');
|
|
879
|
+
if (/保存|覆盖|save|overwrite/i.test(text)) {
|
|
880
|
+
throw new XStudioError(ErrorCode.GUI_ACTION_FAILED, 'A destructive prompt is present in the export dialog stack; refusing to confirm it.', {
|
|
881
|
+
hint: 'Resolve the prompt manually, then retry the export.',
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Try to change the export directory through the dialog's folder button.
|
|
887
|
+
* Unverified in this build: returns false instead of guessing.
|
|
888
|
+
*/
|
|
889
|
+
async changeExportDirectory(budget, evidence, outDir) {
|
|
890
|
+
let snapshot = await this.snapshotOrThrow(budget);
|
|
891
|
+
const dialog = await this.readDialogOrNull(budget);
|
|
892
|
+
if (dialog === null)
|
|
893
|
+
return false;
|
|
894
|
+
const root = findWindowRoot(snapshot, dialog.title);
|
|
895
|
+
if (root === undefined)
|
|
896
|
+
return false;
|
|
897
|
+
const saveButton = elementsOfWindow(snapshot, root.ref).find((element) => element.automationId.endsWith('file_save_bt'));
|
|
898
|
+
if (saveButton === undefined)
|
|
899
|
+
return false;
|
|
900
|
+
await this.actOrThrow(budget, evidence, saveButton.ref, 'invoke_pattern', undefined, 'choose-export-dir');
|
|
901
|
+
const picker = await this.waitForDialog(budget, (current) => current !== null && current.title !== dialog.title, 10_000, evidence);
|
|
902
|
+
if (picker === null)
|
|
903
|
+
return false;
|
|
904
|
+
const pickerSnapshot = await this.snapshotOrThrow(budget);
|
|
905
|
+
const pickerRoot = findWindowRoot(pickerSnapshot, picker.title);
|
|
906
|
+
if (pickerRoot === undefined) {
|
|
907
|
+
await this.cancelDialogsQuietly();
|
|
908
|
+
return false;
|
|
909
|
+
}
|
|
910
|
+
const scoped = elementsOfWindow(pickerSnapshot, pickerRoot.ref);
|
|
911
|
+
const addressEdit = scoped.find((element) => element.automationId === '1152') ??
|
|
912
|
+
scoped.find((element) => element.className === 'Edit' || element.controlType === 'Edit');
|
|
913
|
+
if (addressEdit === undefined) {
|
|
914
|
+
await this.cancelDialogsQuietly();
|
|
915
|
+
return false;
|
|
916
|
+
}
|
|
917
|
+
await this.actOrThrow(budget, evidence, addressEdit.ref, 'set_text', this.winePaths.toWine(outDir), 'export-dir-path');
|
|
918
|
+
const confirm = this.findDialogButton(pickerSnapshot, pickerRoot.ref, /选择文件夹|确定|打开|select|ok/i);
|
|
919
|
+
if (confirm === undefined) {
|
|
920
|
+
await this.cancelDialogsQuietly();
|
|
921
|
+
return false;
|
|
922
|
+
}
|
|
923
|
+
await this.actOrThrow(budget, evidence, confirm.ref, 'invoke_pattern', undefined, 'export-dir-confirm');
|
|
924
|
+
const closed = await this.waitForDialog(budget, (current) => current === null || current.title === dialog.title, 8_000, evidence);
|
|
925
|
+
if (closed === null)
|
|
926
|
+
return false;
|
|
927
|
+
snapshot = await this.snapshotOrThrow(budget);
|
|
928
|
+
const refreshedDialog = await this.readDialogOrNull(budget);
|
|
929
|
+
if (refreshedDialog === null)
|
|
930
|
+
return false;
|
|
931
|
+
const refreshedRoot = findWindowRoot(snapshot, refreshedDialog.title);
|
|
932
|
+
if (refreshedRoot === undefined)
|
|
933
|
+
return false;
|
|
934
|
+
const label = elementsOfWindow(snapshot, refreshedRoot.ref).find((element) => element.automationId.endsWith('file_path_label'));
|
|
935
|
+
const value = (label?.name ?? label?.value ?? '').trim();
|
|
936
|
+
evidence.push(`export-dir: ${value}`);
|
|
937
|
+
return value !== '' && normalizeComparable(this.winePaths.toLinux(value)) === normalizeComparable(outDir);
|
|
938
|
+
}
|
|
939
|
+
/** Wait for a new artifact to appear and be parsable. */
|
|
940
|
+
async awaitArtifact(budget, request, sinceMs, evidence) {
|
|
941
|
+
const startedAt = this.now();
|
|
942
|
+
const waitMs = Math.min(this.artifactWaitMs, Math.max(1_000, budget.remainingMs()));
|
|
943
|
+
let lastDialogChecked = 0;
|
|
944
|
+
while (this.now() - startedAt < waitMs) {
|
|
945
|
+
this.checkOrThrow(budget);
|
|
946
|
+
const artifact = await findNewestArtifact(request.outDir, request.format, sinceMs - 2_000);
|
|
947
|
+
if (artifact !== null)
|
|
948
|
+
return artifact;
|
|
949
|
+
// Surface export error dialogs instead of polling until the timeout.
|
|
950
|
+
if (this.now() - lastDialogChecked > this.pollIntervalMs * 2) {
|
|
951
|
+
lastDialogChecked = this.now();
|
|
952
|
+
const dialog = await this.readDialogOrNull(budget);
|
|
953
|
+
if (dialog !== null) {
|
|
954
|
+
const dialogClass = classifyDialog(dialog);
|
|
955
|
+
evidence.push(`dialog-after-export: ${dialog.title} -> ${dialogClass}`);
|
|
956
|
+
if (dialogClass === 'cloud') {
|
|
957
|
+
throw new XStudioError(ErrorCode.CLOUD_UNREACHABLE, `The export failed with a network/cloud error: ${dialog.text}`, {
|
|
958
|
+
hint: 'Check the network connection and retry the export.',
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
if (dialogClass !== 'download') {
|
|
962
|
+
await this.cancelDialogsQuietly();
|
|
963
|
+
throw new XStudioError(ErrorCode.EXPORT_FAILED, `The export failed: ${dialog.text}`, { hint: 'Inspect the dialog with xstudio_ui_snapshot and retry.' });
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
await this.sleep(this.pollIntervalMs);
|
|
968
|
+
}
|
|
969
|
+
throw new XStudioError(ErrorCode.ARTIFACT_MISSING, `No ${request.format.toUpperCase()} artifact appeared in ${request.outDir} within ${waitMs}ms.`, {
|
|
970
|
+
hint: 'The export may still be rendering on the cloud; check the directory and the dialog, then retry.',
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
//# sourceMappingURL=orchestrator.js.map
|