dsh-zcode-cua 0.0.0-stage → 1.3.1
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 +133 -0
- package/LICENSE +21 -0
- package/README.md +208 -2
- package/README.zh.md +209 -0
- package/client.js +209 -0
- package/cordis.patch.yml +43 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +51 -0
- package/icon.svg +28 -0
- package/index.js +226 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +73 -6
- package/server/access.js +94 -0
- package/server/helper-launcher.js +64 -0
- package/server/index.js +498 -0
- package/server/overlay/Overlay.cs +777 -0
- package/server/overlay/host.ps1 +130 -0
- package/server/overlay.js +374 -0
- package/server/paths.js +231 -0
- package/skills/zcode-computer-use/SKILL.md +304 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Resident overlay host: one PowerShell process, one JSON command per line.
|
|
2
|
+
#
|
|
3
|
+
# The Node bridge cannot create a layered window itself, and spawning a fresh
|
|
4
|
+
# PowerShell per show/hide would cost ~200 ms each time — far too slow for
|
|
5
|
+
# something that must appear the instant a tool call starts and vanish before a
|
|
6
|
+
# screenshot. So this process is spawned once and kept alive, reading stdin.
|
|
7
|
+
#
|
|
8
|
+
# Protocol (one JSON object per line on stdin, one line per reply on stdout):
|
|
9
|
+
# {"cmd":"show","phase":"working"|"waiting","text":"..."} -> {"ok":true,"detail":"ok:6"}
|
|
10
|
+
# {"cmd":"hide"} -> {"ok":true}
|
|
11
|
+
# {"cmd":"diagnose"} -> {"ok":true,"detail":"visible:..."}
|
|
12
|
+
# {"cmd":"quit"} -> exits
|
|
13
|
+
#
|
|
14
|
+
# `text` is the action line under the title — whatever the current tool call
|
|
15
|
+
# said it was doing. It is optional; without it the banner shows a generic line.
|
|
16
|
+
#
|
|
17
|
+
# Compilation is done in-process with Add-Type. Nothing is written to disk, so
|
|
18
|
+
# the plugin ships no binaries and needs no build step. If compilation fails the
|
|
19
|
+
# host reports it on stderr and exits non-zero; the Node side treats the overlay
|
|
20
|
+
# as unavailable and keeps working, because the indicator is decoration.
|
|
21
|
+
|
|
22
|
+
param(
|
|
23
|
+
[Parameter(Mandatory = $true)][string]$SourcePath
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
$ErrorActionPreference = 'Stop'
|
|
27
|
+
|
|
28
|
+
# No console window, ever: this runs as a child of the MCP bridge, and a stray
|
|
29
|
+
# console would steal focus and appear in screenshots.
|
|
30
|
+
try {
|
|
31
|
+
Add-Type -Namespace DshZcodeCua -Name ConsoleHide -MemberDefinition @'
|
|
32
|
+
[DllImport("kernel32.dll")] public static extern System.IntPtr GetConsoleWindow();
|
|
33
|
+
[DllImport("kernel32.dll")] public static extern bool FreeConsole();
|
|
34
|
+
public static void Hide() { FreeConsole(); }
|
|
35
|
+
'@
|
|
36
|
+
[DshZcodeCua.ConsoleHide]::Hide()
|
|
37
|
+
} catch {
|
|
38
|
+
# Not fatal: worst case a console flashes.
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
$source = [System.IO.File]::ReadAllText($SourcePath, [System.Text.Encoding]::UTF8)
|
|
42
|
+
|
|
43
|
+
# Windows PowerShell 5.1 runs on .NET Framework, where GDI+ and WinForms are
|
|
44
|
+
# separate assemblies that are NOT loaded by default — referencing a type before
|
|
45
|
+
# loading its assembly fails with "Unable to find type". Load them first, then
|
|
46
|
+
# hand their locations to the compiler.
|
|
47
|
+
foreach ($name in @('System.Drawing', 'System.Windows.Forms')) {
|
|
48
|
+
try { Add-Type -AssemblyName $name -ErrorAction Stop } catch {
|
|
49
|
+
[Console]::Error.WriteLine("overlay: cannot load $name — $($_.Exception.Message)")
|
|
50
|
+
exit 2
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
$refs = @(
|
|
55
|
+
[System.Drawing.Bitmap].Assembly.Location,
|
|
56
|
+
[System.Drawing.Graphics].Assembly.Location,
|
|
57
|
+
[System.Windows.Forms.Cursor].Assembly.Location
|
|
58
|
+
) | Where-Object { $_ } | Select-Object -Unique
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
Add-Type -TypeDefinition $source -ReferencedAssemblies $refs -Language CSharp
|
|
62
|
+
} catch {
|
|
63
|
+
[Console]::Error.WriteLine("overlay compile failed: $($_.Exception.Message)")
|
|
64
|
+
if ($_.Exception.InnerException) {
|
|
65
|
+
[Console]::Error.WriteLine($_.Exception.InnerException.Message)
|
|
66
|
+
}
|
|
67
|
+
exit 2
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
[DshZcodeCua.Overlay]::Init()
|
|
71
|
+
|
|
72
|
+
# The Node bridge writes UTF-8. `[Console]::In` would decode it with the console
|
|
73
|
+
# output code page instead (936/GBK on a Chinese Windows), turning every Chinese
|
|
74
|
+
# action line into mojibake, so the channel is opened as explicit UTF-8 streams.
|
|
75
|
+
# `$false` = no BOM: the parent parses each line as JSON and a BOM would break it.
|
|
76
|
+
$utf8 = New-Object System.Text.UTF8Encoding($false)
|
|
77
|
+
$stdin = New-Object System.IO.StreamReader([Console]::OpenStandardInput(), $utf8)
|
|
78
|
+
$stdout = New-Object System.IO.StreamWriter([Console]::OpenStandardOutput(), $utf8)
|
|
79
|
+
$stdout.AutoFlush = $true
|
|
80
|
+
|
|
81
|
+
function Reply([bool]$ok, [string]$detail) {
|
|
82
|
+
$payload = @{ ok = $ok }
|
|
83
|
+
if ($detail) { $payload.detail = $detail }
|
|
84
|
+
$json = $payload | ConvertTo-Json -Compress
|
|
85
|
+
$stdout.WriteLine($json)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
Reply $true 'ready'
|
|
89
|
+
|
|
90
|
+
# Blocking read loop. `ReadLine` returning null means stdin closed: the parent
|
|
91
|
+
# died or shut down, so this process must not outlive it.
|
|
92
|
+
while ($true) {
|
|
93
|
+
$line = $stdin.ReadLine()
|
|
94
|
+
if ($null -eq $line) { break }
|
|
95
|
+
$line = $line.Trim()
|
|
96
|
+
if ($line -eq '') { continue }
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
$cmd = $line | ConvertFrom-Json
|
|
100
|
+
} catch {
|
|
101
|
+
Reply $false "bad-json"
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
switch ($cmd.cmd) {
|
|
106
|
+
'show' {
|
|
107
|
+
$phase = if ($cmd.phase) { [string]$cmd.phase } else { 'working' }
|
|
108
|
+
$text = if ($null -ne $cmd.text) { [string]$cmd.text } else { $null }
|
|
109
|
+
$detail = [DshZcodeCua.Overlay]::Show($phase, $true, $text)
|
|
110
|
+
Reply ($detail -notlike 'error:*') $detail
|
|
111
|
+
}
|
|
112
|
+
'hide' {
|
|
113
|
+
[DshZcodeCua.Overlay]::Hide()
|
|
114
|
+
Reply $true 'hidden'
|
|
115
|
+
}
|
|
116
|
+
'diagnose' {
|
|
117
|
+
Reply $true ([DshZcodeCua.Overlay]::Diagnose())
|
|
118
|
+
}
|
|
119
|
+
'quit' {
|
|
120
|
+
[DshZcodeCua.Overlay]::Hide()
|
|
121
|
+
Reply $true 'bye'
|
|
122
|
+
break
|
|
123
|
+
}
|
|
124
|
+
default {
|
|
125
|
+
Reply $false "unknown-cmd"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
[DshZcodeCua.Overlay]::Hide()
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host for the "an AI is driving this computer" indicator.
|
|
3
|
+
*
|
|
4
|
+
* The indicator itself is a Win32 layered window built by `Overlay.cs` and driven
|
|
5
|
+
* by `host.ps1`; this module is the Node side: it owns that child process, turns
|
|
6
|
+
* tool calls into show/hide commands, and guarantees the indicator is gone before
|
|
7
|
+
* any screenshot the model could take.
|
|
8
|
+
*
|
|
9
|
+
* ## Why a resident child process
|
|
10
|
+
*
|
|
11
|
+
* Compiling the C# and creating six windows costs a few hundred milliseconds.
|
|
12
|
+
* That is fine ONCE, and unacceptable per tool call: the indicator has to appear
|
|
13
|
+
* when the call starts and disappear before `capture_app`, both of which happen
|
|
14
|
+
* inside a single turn. So the host is spawned on first use, compiles once, and
|
|
15
|
+
* then answers one JSON line per command.
|
|
16
|
+
*
|
|
17
|
+
* ## Why exclusion needs no cooperation from the capture path
|
|
18
|
+
*
|
|
19
|
+
* The affinity alone is enough: Windows omits the window from every capture,
|
|
20
|
+
* including the GDI/DWM paths the CUA helper uses. That matters because the
|
|
21
|
+
* bridge never sees a `capture_app` request — it is a socket message between the
|
|
22
|
+
* upstream and the helper, not an MCP call — so a hide-before-capture scheme
|
|
23
|
+
* could not be wired up from here even if it were wanted. Verified on this
|
|
24
|
+
* machine: a full-screen capture taken mid-tool-call contains no trace of the
|
|
25
|
+
* overlay, while the same capture with the affinity cleared shows the full
|
|
26
|
+
* artwork.
|
|
27
|
+
*
|
|
28
|
+
* ## Failure policy
|
|
29
|
+
*
|
|
30
|
+
* The indicator is decoration. Every path here degrades to "no indicator" and
|
|
31
|
+
* never blocks, delays or fails a tool call: a missing PowerShell, a compile
|
|
32
|
+
* error, a dead host process — all of it is logged once and ignored. A computer
|
|
33
|
+
* use plugin that stops working because its status badge broke would be a bad
|
|
34
|
+
* trade.
|
|
35
|
+
*
|
|
36
|
+
* @module dsh-zcode-cua/overlay
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { spawn } from "node:child_process";
|
|
40
|
+
import { existsSync } from "node:fs";
|
|
41
|
+
import { dirname, join } from "node:path";
|
|
42
|
+
import { fileURLToPath } from "node:url";
|
|
43
|
+
|
|
44
|
+
/** Absolute directory holding Overlay.cs and host.ps1. */
|
|
45
|
+
const OVERLAY_DIR = join(dirname(fileURLToPath(import.meta.url)), "overlay");
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Tools whose execution should raise the indicator.
|
|
49
|
+
*
|
|
50
|
+
* Only `js` is exposed by the upstream, and every Computer Use action happens
|
|
51
|
+
* inside one. Listing it explicitly rather than "any tool call" keeps the
|
|
52
|
+
* indicator honest: a future non-desktop tool added upstream must not light up a
|
|
53
|
+
* badge claiming the computer is being driven.
|
|
54
|
+
*/
|
|
55
|
+
const CUA_TOOL_NAMES = new Set(["js"]);
|
|
56
|
+
|
|
57
|
+
/** How long a single command may take before the host is considered wedged. */
|
|
58
|
+
const COMMAND_TIMEOUT_MS = 4000;
|
|
59
|
+
|
|
60
|
+
/** Delay before a respawn attempt, so a crash loop cannot spin the CPU. */
|
|
61
|
+
const RESPAWN_DELAY_MS = 5000;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* How long the indicator stays up at minimum, even if the call finished sooner.
|
|
65
|
+
*
|
|
66
|
+
* First use compiles the C# (~1 s), so a call that takes 300 ms would otherwise
|
|
67
|
+
* raise the windows and tear them down within a few frames — technically
|
|
68
|
+
* working, but the user sees nothing and concludes the indicator is broken.
|
|
69
|
+
*/
|
|
70
|
+
const MIN_VISIBLE_MS = 900;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resolve the PowerShell executable.
|
|
74
|
+
*
|
|
75
|
+
* `powershell.exe` (5.1) is used rather than `pwsh` on purpose: 5.1 is present on
|
|
76
|
+
* every supported Windows, while PowerShell 7 is an optional install. The C# in
|
|
77
|
+
* Overlay.cs targets .NET Framework 4.x, which is exactly what 5.1 provides.
|
|
78
|
+
*/
|
|
79
|
+
function powershellPath() {
|
|
80
|
+
const root = process.env.SystemRoot ?? "C:\\Windows";
|
|
81
|
+
const full = join(root, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
82
|
+
return existsSync(full) ? full : "powershell.exe";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Line-delimited JSON channel to one `host.ps1` process.
|
|
87
|
+
*
|
|
88
|
+
* Commands are serialized by a promise chain rather than queued with ids: the
|
|
89
|
+
* protocol is strictly one command, one reply, in order, and keeping it that way
|
|
90
|
+
* removes any need for correlation. A command that times out resolves as a
|
|
91
|
+
* failure instead of rejecting, so a wedged host cannot produce an unhandled
|
|
92
|
+
* rejection from a fire-and-forget caller.
|
|
93
|
+
*/
|
|
94
|
+
class OverlayHost {
|
|
95
|
+
constructor(options) {
|
|
96
|
+
this.log = options.log;
|
|
97
|
+
this.child = undefined;
|
|
98
|
+
this.pending = undefined;
|
|
99
|
+
this.buffer = "";
|
|
100
|
+
this.chain = Promise.resolve();
|
|
101
|
+
this.failed = false;
|
|
102
|
+
this.spawnedAt = 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether the indicator is worth trying at all. */
|
|
106
|
+
get usable() {
|
|
107
|
+
return !this.failed && existsSync(join(OVERLAY_DIR, "host.ps1")) && existsSync(join(OVERLAY_DIR, "Overlay.cs"));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
start() {
|
|
111
|
+
if (this.child !== undefined || this.failed) return;
|
|
112
|
+
const script = join(OVERLAY_DIR, "host.ps1");
|
|
113
|
+
const source = join(OVERLAY_DIR, "Overlay.cs");
|
|
114
|
+
let child;
|
|
115
|
+
try {
|
|
116
|
+
child = spawn(
|
|
117
|
+
powershellPath(),
|
|
118
|
+
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", script, "-SourcePath", source],
|
|
119
|
+
{ stdio: ["pipe", "pipe", "pipe"], windowsHide: true },
|
|
120
|
+
);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
this.fail(`spawn failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.child = child;
|
|
126
|
+
this.spawnedAt = Date.now();
|
|
127
|
+
this.buffer = "";
|
|
128
|
+
|
|
129
|
+
child.stdout.setEncoding("utf8");
|
|
130
|
+
child.stdout.on("data", (chunk) => this.onStdout(chunk));
|
|
131
|
+
child.stderr.setEncoding("utf8");
|
|
132
|
+
child.stderr.on("data", (chunk) => {
|
|
133
|
+
const text = String(chunk).trim();
|
|
134
|
+
// stderr carries compile errors and the one-time "peer identity" notices
|
|
135
|
+
// from the CUA helper; both are worth one line each, not a flood.
|
|
136
|
+
if (text !== "") this.log(`overlay: ${text.split("\n").slice(-3).join(" ")}`);
|
|
137
|
+
});
|
|
138
|
+
child.on("error", (error) => this.fail(`process error: ${error.message}`));
|
|
139
|
+
child.on("exit", (code, signal) => {
|
|
140
|
+
this.child = undefined;
|
|
141
|
+
const reason = signal === null ? `exit ${code}` : `signal ${signal}`;
|
|
142
|
+
if (this.pending !== undefined) {
|
|
143
|
+
const settle = this.pending;
|
|
144
|
+
this.pending = undefined;
|
|
145
|
+
settle({ ok: false, detail: `host exited: ${reason}` });
|
|
146
|
+
}
|
|
147
|
+
// A host that dies within a second of spawning is broken (compile error,
|
|
148
|
+
// missing assembly); retrying it on every tool call would just spam.
|
|
149
|
+
if (Date.now() - this.spawnedAt < 1000) {
|
|
150
|
+
this.fail(`host died immediately (${reason})`);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this.log(`overlay: host exited (${reason}); will respawn on next use`);
|
|
154
|
+
// Deliberately not respawning here: the next show() will, and a turn that
|
|
155
|
+
// never asks again should not keep a process alive.
|
|
156
|
+
this.respawnAllowedAt = Date.now() + RESPAWN_DELAY_MS;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
onStdout(chunk) {
|
|
161
|
+
this.buffer += chunk;
|
|
162
|
+
for (;;) {
|
|
163
|
+
const newline = this.buffer.indexOf("\n");
|
|
164
|
+
if (newline < 0) return;
|
|
165
|
+
const line = this.buffer.slice(0, newline).trim();
|
|
166
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
167
|
+
if (line === "") continue;
|
|
168
|
+
const settle = this.pending;
|
|
169
|
+
if (settle === undefined) continue;
|
|
170
|
+
this.pending = undefined;
|
|
171
|
+
let reply;
|
|
172
|
+
try {
|
|
173
|
+
reply = JSON.parse(line);
|
|
174
|
+
} catch {
|
|
175
|
+
settle({ ok: false, detail: `unparsable reply: ${line.slice(0, 120)}` });
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
settle({ ok: reply?.ok === true, detail: typeof reply?.detail === "string" ? reply.detail : "" });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fail(reason) {
|
|
183
|
+
if (this.failed) return;
|
|
184
|
+
this.failed = true;
|
|
185
|
+
this.log(`overlay: disabled — ${reason}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Send one command, awaiting its reply. Never throws. */
|
|
189
|
+
command(name, extra) {
|
|
190
|
+
const run = async () => {
|
|
191
|
+
if (!this.usable) return { ok: false, detail: "unavailable" };
|
|
192
|
+
if (this.child === undefined) {
|
|
193
|
+
if (this.respawnAllowedAt !== undefined && Date.now() < this.respawnAllowedAt) {
|
|
194
|
+
return { ok: false, detail: "cooling down" };
|
|
195
|
+
}
|
|
196
|
+
this.start();
|
|
197
|
+
if (this.child === undefined) return { ok: false, detail: "not started" };
|
|
198
|
+
// The first line the host prints is `{"ok":true,"detail":"ready"}`; it
|
|
199
|
+
// arrives as a normal reply, so the first real command simply queues
|
|
200
|
+
// behind it. Nothing to do here.
|
|
201
|
+
}
|
|
202
|
+
return await this.send(name, extra);
|
|
203
|
+
};
|
|
204
|
+
// Chain, so two concurrent tool calls cannot interleave half-lines.
|
|
205
|
+
this.chain = this.chain.then(run, run);
|
|
206
|
+
return this.chain;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
send(name, extra) {
|
|
210
|
+
return new Promise((resolve) => {
|
|
211
|
+
const child = this.child;
|
|
212
|
+
if (child === undefined || child.stdin === undefined) {
|
|
213
|
+
resolve({ ok: false, detail: "no host" });
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
let settled = false;
|
|
217
|
+
const finish = (value) => {
|
|
218
|
+
if (settled) return;
|
|
219
|
+
settled = true;
|
|
220
|
+
if (this.pending === finish) this.pending = undefined;
|
|
221
|
+
resolve(value);
|
|
222
|
+
};
|
|
223
|
+
this.pending = finish;
|
|
224
|
+
const timer = setTimeout(() => {
|
|
225
|
+
finish({ ok: false, detail: `timeout after ${COMMAND_TIMEOUT_MS}ms` });
|
|
226
|
+
}, COMMAND_TIMEOUT_MS);
|
|
227
|
+
timer.unref?.();
|
|
228
|
+
const wrapped = finish;
|
|
229
|
+
this.pending = (value) => {
|
|
230
|
+
clearTimeout(timer);
|
|
231
|
+
wrapped(value);
|
|
232
|
+
};
|
|
233
|
+
try {
|
|
234
|
+
child.stdin.write(`${JSON.stringify({ cmd: name, ...(extra ?? {}) })}\n`);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
finish({ ok: false, detail: `write failed: ${error instanceof Error ? error.message : String(error)}` });
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
dispose() {
|
|
242
|
+
const child = this.child;
|
|
243
|
+
this.child = undefined;
|
|
244
|
+
if (child === undefined) return;
|
|
245
|
+
try {
|
|
246
|
+
child.stdin.write(`${JSON.stringify({ cmd: "quit" })}\n`);
|
|
247
|
+
child.stdin.end();
|
|
248
|
+
} catch {
|
|
249
|
+
// Already gone.
|
|
250
|
+
}
|
|
251
|
+
// Give it a moment to destroy its windows, then make sure.
|
|
252
|
+
const timer = setTimeout(() => {
|
|
253
|
+
try {
|
|
254
|
+
child.kill();
|
|
255
|
+
} catch {
|
|
256
|
+
// Already gone.
|
|
257
|
+
}
|
|
258
|
+
}, 500);
|
|
259
|
+
timer.unref?.();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The indicator as the bridge uses it.
|
|
265
|
+
*
|
|
266
|
+
* Tracks nesting so that two overlapping tool calls do not hide the indicator
|
|
267
|
+
* from under each other: the first call raises it, the last one lowers it.
|
|
268
|
+
*/
|
|
269
|
+
export function createOverlay(options = {}) {
|
|
270
|
+
const log = options.log ?? ((message) => process.stderr.write(`[dsh-zcode-cua] ${message}\n`));
|
|
271
|
+
const enabled = options.enabled !== false;
|
|
272
|
+
const host = new OverlayHost({ log });
|
|
273
|
+
let depth = 0;
|
|
274
|
+
let shown = false;
|
|
275
|
+
// The reply to a `show` that has not come back yet. First use pays for the
|
|
276
|
+
// C# compile (~1-2 s), so a short tool call can finish while the windows are
|
|
277
|
+
// still being created; `leave` must wait for that reply before hiding, or the
|
|
278
|
+
// overlay never gets a chance to appear at all.
|
|
279
|
+
let showing;
|
|
280
|
+
let raisedAt = 0;
|
|
281
|
+
// A hide waiting out MIN_VISIBLE_MS. A new call arriving in that window must
|
|
282
|
+
// cancel it, otherwise the stale hide fires in the middle of the next call and
|
|
283
|
+
// blanks the indicator while the computer is still being driven.
|
|
284
|
+
let hideTimer;
|
|
285
|
+
|
|
286
|
+
const show = (phase, text) => {
|
|
287
|
+
if (!enabled) return;
|
|
288
|
+
const extra = { phase };
|
|
289
|
+
if (typeof text === "string" && text.trim() !== "") extra.text = text.trim();
|
|
290
|
+
raisedAt = Date.now();
|
|
291
|
+
showing = host.command("show", extra).then((reply) => {
|
|
292
|
+
showing = undefined;
|
|
293
|
+
if (!reply.ok) log(`overlay: show failed — ${reply.detail}`);
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
const hide = () => {
|
|
297
|
+
if (!enabled) return;
|
|
298
|
+
void host.command("hide").then((reply) => {
|
|
299
|
+
if (!reply.ok) log(`overlay: hide failed — ${reply.detail}`);
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
/** Whether this tool call should raise the indicator. */
|
|
305
|
+
handlesTool(name) {
|
|
306
|
+
return enabled && CUA_TOOL_NAMES.has(name);
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* A tool call started.
|
|
311
|
+
*
|
|
312
|
+
* `text` is what the call said it was doing — the `js` tool's required
|
|
313
|
+
* `title` argument — and becomes the banner's second line. Empty is fine:
|
|
314
|
+
* the banner then keeps a generic line instead of showing a blank row.
|
|
315
|
+
*/
|
|
316
|
+
enter(text) {
|
|
317
|
+
if (!enabled) return;
|
|
318
|
+
depth += 1;
|
|
319
|
+
if (hideTimer !== undefined) {
|
|
320
|
+
clearTimeout(hideTimer);
|
|
321
|
+
hideTimer = undefined;
|
|
322
|
+
}
|
|
323
|
+
if (depth === 1 && !shown) {
|
|
324
|
+
shown = true;
|
|
325
|
+
show("working", text);
|
|
326
|
+
} else if (depth === 1 && shown) {
|
|
327
|
+
// Already up (the previous call was still inside its minimum-visible
|
|
328
|
+
// window, or two calls overlap). Repaint so the banner names THIS call
|
|
329
|
+
// rather than the one before it.
|
|
330
|
+
show("working", text);
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
/** A tool call finished, failed or was cancelled. */
|
|
335
|
+
leave() {
|
|
336
|
+
if (!enabled) return;
|
|
337
|
+
depth = Math.max(0, depth - 1);
|
|
338
|
+
if (depth > 0 || !shown) return;
|
|
339
|
+
shown = false;
|
|
340
|
+
// Two things have to be true before tearing the windows down: the show
|
|
341
|
+
// must have been answered (otherwise the hide jumps ahead of the windows
|
|
342
|
+
// and the indicator never appears), and it must have been up long enough
|
|
343
|
+
// to be seen. Both are cheap to wait for and both fix a real complaint.
|
|
344
|
+
const wait = Math.max(0, MIN_VISIBLE_MS - (Date.now() - raisedAt));
|
|
345
|
+
const pending = showing;
|
|
346
|
+
const teardown = () => {
|
|
347
|
+
if (wait > 0) {
|
|
348
|
+
hideTimer = setTimeout(() => {
|
|
349
|
+
hideTimer = undefined;
|
|
350
|
+
// Only lower it if nothing took it back up meanwhile.
|
|
351
|
+
if (!shown) hide();
|
|
352
|
+
}, wait);
|
|
353
|
+
hideTimer.unref?.();
|
|
354
|
+
} else {
|
|
355
|
+
hide();
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
if (pending !== undefined) void pending.then(teardown, teardown);
|
|
359
|
+
else teardown();
|
|
360
|
+
},
|
|
361
|
+
|
|
362
|
+
/** Diagnostic snapshot for the smoke test. */
|
|
363
|
+
async diagnose() {
|
|
364
|
+
if (!enabled) return { ok: true, detail: "disabled" };
|
|
365
|
+
return await host.command("diagnose");
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
dispose() {
|
|
369
|
+
host.dispose();
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export const OVERLAY_CUA_TOOLS = CUA_TOOL_NAMES;
|