ysagc-agent 0.9.15 → 0.9.16

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.
@@ -0,0 +1,722 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.computerStart = computerStart;
37
+ exports.computerStop = computerStop;
38
+ exports.computerScreenshot = computerScreenshot;
39
+ exports.computerMove = computerMove;
40
+ exports.computerClick = computerClick;
41
+ exports.computerDoubleClick = computerDoubleClick;
42
+ exports.computerDrag = computerDrag;
43
+ exports.computerScroll = computerScroll;
44
+ exports.computerType = computerType;
45
+ exports.computerKey = computerKey;
46
+ exports.computerClipboardRead = computerClipboardRead;
47
+ exports.computerClipboardWrite = computerClipboardWrite;
48
+ // ============================================================================
49
+ // computer:让模型操作「本机桌面」——截屏、移动、点击、拖拽、滚轮、输入、按键、剪贴板
50
+ // ----------------------------------------------------------------------------
51
+ // 和 browser 的区别:
52
+ // - browser 操作的是「网页」(DOM / 选择器 / 无头或真实浏览器);
53
+ // - computer 操作的是「操作系统桌面」:原生应用、文件管理器、微信、MES、ERP 等。
54
+ // 它按屏幕绝对坐标点击,所以模型必须先 computer_screenshot 看清画面再动手。
55
+ //
56
+ // 安全边界:
57
+ // 1. 必须显式 computer_start,并且平台会先让用户确认(needsApproval);
58
+ // 2. 会话默认 5 分钟无操作自动结束,最长 10 分钟强制结束;
59
+ // 3. 操作期间屏幕右上角显示「AI 正在操作你的电脑」,结束/停止即消失;
60
+ // 4. 只读权限(read-only)下整组工具被 preExecute 拦截;
61
+ // 5. 目前桌面输入控制只实现 Windows;macOS/Linux 返回明确提示,不静默失败。
62
+ // ============================================================================
63
+ const fs = __importStar(require("node:fs"));
64
+ const os = __importStar(require("node:os"));
65
+ const path = __importStar(require("node:path"));
66
+ const node_child_process_1 = require("node:child_process");
67
+ const screenshot_1 = require("./screenshot");
68
+ const SESSION_IDLE_MS = Math.max(30000, Number(process.env.YSAGC_COMPUTER_IDLE_MS || 5 * 60 * 1000));
69
+ const SESSION_MAX_MS = Math.max(60000, Number(process.env.YSAGC_COMPUTER_MAX_MS || 10 * 60 * 1000));
70
+ const PS_TIMEOUT_MS = Math.max(3000, Number(process.env.YSAGC_COMPUTER_PS_TIMEOUT_MS || 20000));
71
+ const state = { active: false, startedAt: 0 };
72
+ function isWindows() {
73
+ return process.platform === 'win32';
74
+ }
75
+ function platformError() {
76
+ return {
77
+ ok: false,
78
+ error: '电脑操作目前只支持 Windows 本机。macOS/Linux 需要额外的辅助功能授权与输入工具,暂未开放。',
79
+ };
80
+ }
81
+ function argInt(args, key, fallback, min = -100000, max = 100000) {
82
+ const raw = args[key];
83
+ if (raw === undefined || raw === null || raw === '') {
84
+ if (fallback === undefined)
85
+ throw new Error(`缺少参数 ${key}`);
86
+ return fallback;
87
+ }
88
+ const n = typeof raw === 'number' ? raw : Number(raw);
89
+ if (!Number.isFinite(n))
90
+ throw new Error(`${key} 必须是数字`);
91
+ return Math.min(max, Math.max(min, Math.round(n)));
92
+ }
93
+ function argStr(args, key, fallback = '') {
94
+ const raw = args[key];
95
+ if (raw === undefined || raw === null)
96
+ return fallback;
97
+ return String(raw);
98
+ }
99
+ function shellQuote(p) {
100
+ return `'${p.replace(/'/g, "''")}'`;
101
+ }
102
+ function runPowerShell(script, timeoutMs = PS_TIMEOUT_MS) {
103
+ return new Promise((resolve) => {
104
+ let stdout = '';
105
+ let stderr = '';
106
+ let settled = false;
107
+ const finish = (code) => {
108
+ if (settled)
109
+ return;
110
+ settled = true;
111
+ resolve({ code, stdout, stderr });
112
+ };
113
+ let child;
114
+ try {
115
+ child = (0, node_child_process_1.spawn)('powershell', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-STA', '-Command', script], {
116
+ windowsHide: true,
117
+ });
118
+ }
119
+ catch (e) {
120
+ finish(-1);
121
+ stderr = String(e?.message || e);
122
+ return;
123
+ }
124
+ const timer = setTimeout(() => {
125
+ try {
126
+ child.kill('SIGKILL');
127
+ }
128
+ catch { /* ignore */ }
129
+ finish(-2);
130
+ }, timeoutMs);
131
+ timer.unref?.();
132
+ child.stdout?.on('data', (c) => { stdout += c.toString('utf8'); });
133
+ child.stderr?.on('data', (c) => { stderr += c.toString('utf8'); });
134
+ child.on('error', (e) => {
135
+ clearTimeout(timer);
136
+ stderr += String(e?.message || e);
137
+ finish(-1);
138
+ });
139
+ child.on('close', (code) => {
140
+ clearTimeout(timer);
141
+ finish(code ?? -1);
142
+ });
143
+ });
144
+ }
145
+ function psResult(r, okNote) {
146
+ if (r.code !== 0) {
147
+ return { ok: false, error: `本机操作失败(退出码 ${r.code}):${(r.stderr || r.stdout || '').trim().slice(0, 500) || '无输出'}` };
148
+ }
149
+ return { ok: true, result: { ok: true, note: okNote } };
150
+ }
151
+ // ============================================================================
152
+ // 屏幕右上角「AI 正在操作你的电脑」浮层
153
+ // ----------------------------------------------------------------------------
154
+ // 用 WPF 做无边框、置顶、鼠标穿透的小窗口;PowerShell 进程常驻,读取一个状态文件:
155
+ // { active: true } → 显示
156
+ // { active: false } → 隐藏
157
+ // { closed: true } → 退出进程
158
+ // 这样不依赖 stdin/线程,Agent 退出或会话结束都能可靠收掉提示。
159
+ // ============================================================================
160
+ const OVERLAY_TEXT = 'AI 正在操作你的电脑';
161
+ const OVERLAY_SUBTEXT = '操作期间请勿手动操作鼠标键盘';
162
+ function base64Utf8(s) {
163
+ return Buffer.from(s, 'utf8').toString('base64');
164
+ }
165
+ function overlayScript(statePath) {
166
+ return `
167
+ Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase
168
+ $ErrorActionPreference = 'SilentlyContinue'
169
+ $statePath = ${shellQuote(statePath)}
170
+ $window = New-Object System.Windows.Window
171
+ $window.WindowStyle = 'None'
172
+ $window.AllowsTransparency = $true
173
+ $window.Background = 'Transparent'
174
+ $window.Topmost = $true
175
+ $window.ShowInTaskbar = $false
176
+ $window.ShowActivated = $false
177
+ $window.Focusable = $false
178
+ $window.IsHitTestVisible = $false
179
+ $window.ResizeMode = 'NoResize'
180
+ $window.Width = 330
181
+ $window.Height = 64
182
+ $work = [System.Windows.SystemParameters]::WorkArea
183
+ $window.Left = $work.Right - $window.Width - 24
184
+ $window.Top = $work.Top + 24
185
+
186
+ $border = New-Object System.Windows.Controls.Border
187
+ $border.Background = New-Object System.Windows.Media.SolidColorBrush ([System.Windows.Media.ColorConverter]::ConvertFromString('#E61F2937'))
188
+ $border.CornerRadius = New-Object System.Windows.CornerRadius 16
189
+ $border.Padding = New-Object System.Windows.Thickness 14,0,14,0
190
+ $shadow = New-Object System.Windows.Media.Effects.DropShadowEffect
191
+ $shadow.BlurRadius = 18
192
+ $shadow.Opacity = 0.38
193
+ $shadow.ShadowDepth = 4
194
+ $border.Effect = $shadow
195
+
196
+ $stack = New-Object System.Windows.Controls.StackPanel
197
+ $stack.Orientation = 'Horizontal'
198
+ $stack.VerticalAlignment = 'Center'
199
+ $dot = New-Object System.Windows.Shapes.Ellipse
200
+ $dot.Width = 10
201
+ $dot.Height = 10
202
+ $dot.Fill = New-Object System.Windows.Media.SolidColorBrush ([System.Windows.Media.ColorConverter]::ConvertFromString('#34D399'))
203
+ $dot.Margin = New-Object System.Windows.Thickness 0,0,12,0
204
+ $dot.VerticalAlignment = 'Center'
205
+ $textStack = New-Object System.Windows.Controls.StackPanel
206
+ $textStack.VerticalAlignment = 'Center'
207
+ $title = New-Object System.Windows.Controls.TextBlock
208
+ $title.Text = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${base64Utf8(OVERLAY_TEXT)}'))
209
+ $title.Foreground = New-Object System.Windows.Media.SolidColorBrush ([System.Windows.Media.ColorConverter]::ConvertFromString('#FFFFFF'))
210
+ $title.FontSize = 13.5
211
+ $title.FontWeight = 'SemiBold'
212
+ $sub = New-Object System.Windows.Controls.TextBlock
213
+ $sub.Text = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${base64Utf8(OVERLAY_SUBTEXT)}'))
214
+ $sub.Foreground = New-Object System.Windows.Media.SolidColorBrush ([System.Windows.Media.ColorConverter]::ConvertFromString('#9CA3AF'))
215
+ $sub.FontSize = 10.5
216
+ $sub.Margin = New-Object System.Windows.Thickness 0,3,0,0
217
+ $textStack.Children.Add($title) | Out-Null
218
+ $textStack.Children.Add($sub) | Out-Null
219
+ $stack.Children.Add($dot) | Out-Null
220
+ $stack.Children.Add($textStack) | Out-Null
221
+ $border.Child = $stack
222
+ $window.Content = $border
223
+
224
+ $anim = New-Object System.Windows.Media.Animation.DoubleAnimation
225
+ $anim.From = 1.0
226
+ $anim.To = 0.35
227
+ $anim.Duration = New-Object System.Windows.Duration ([TimeSpan]::FromMilliseconds(900))
228
+ $anim.AutoReverse = $true
229
+ $anim.RepeatBehavior = [System.Windows.Media.Animation.RepeatBehavior]::Forever
230
+ $dot.BeginAnimation([System.Windows.Shapes.Shape]::OpacityProperty, $anim)
231
+
232
+ # 先直接显示:不依赖定时器首次触发,避免某些会话里 DispatcherTimer 首帧不跑导致提示永远不出现。
233
+ $window.Show()
234
+ $window.Topmost = $true
235
+
236
+ $timer = New-Object System.Windows.Threading.DispatcherTimer
237
+ $timer.Interval = [TimeSpan]::FromMilliseconds(350)
238
+ $timer.Add_Tick({
239
+ $s = $null
240
+ if (Test-Path -LiteralPath $statePath) {
241
+ try { $s = Get-Content -LiteralPath $statePath -Raw -ErrorAction Stop | ConvertFrom-Json } catch { $s = $null }
242
+ }
243
+ if ($null -eq $s) {
244
+ if ($window.IsVisible) { $window.Hide() }
245
+ } elseif ($s.closed -eq $true) {
246
+ $window.Close()
247
+ } elseif ($s.active -eq $true) {
248
+ if (-not $window.IsVisible) { $window.Show(); $window.Topmost = $true }
249
+ } else {
250
+ if ($window.IsVisible) { $window.Hide() }
251
+ }
252
+ })
253
+ $timer.Start()
254
+ [System.Windows.Threading.Dispatcher]::Run()
255
+ `;
256
+ }
257
+ function writeOverlayState(handle, active, closed = false) {
258
+ try {
259
+ fs.writeFileSync(handle.statePath, JSON.stringify({ active, closed, ts: Date.now() }), 'utf8');
260
+ }
261
+ catch { /* ignore */ }
262
+ }
263
+ function startOverlay(logger) {
264
+ if (!isWindows())
265
+ return undefined;
266
+ if (state.overlay)
267
+ return state.overlay;
268
+ try {
269
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ysagc-overlay-'));
270
+ const scriptPath = path.join(dir, 'overlay.ps1');
271
+ const statePath = path.join(dir, 'state.json');
272
+ fs.writeFileSync(scriptPath, '\uFEFF' + overlayScript(statePath), 'utf8');
273
+ fs.writeFileSync(statePath, JSON.stringify({ active: true, closed: false, ts: Date.now() }), 'utf8');
274
+ // 关键:由「短命 launcher」调用 Start-Process 把 WPF 浮层拉起来。
275
+ // 直接 spawn powershell -File 时,WPF 窗口在部分 Windows 会话里建不出来
276
+ // (进程活着但 MainWindowHandle=0);Start-Process 走的是交互式桌面,能正常显示。
277
+ const command = `$p = Start-Process powershell -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-STA','-File',${shellQuote(scriptPath)}) -WindowStyle Hidden -PassThru; [Console]::Out.Write($p.Id)`;
278
+ const launcher = (0, node_child_process_1.spawn)('powershell', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', command], {
279
+ windowsHide: true,
280
+ stdio: ['ignore', 'pipe', 'pipe'],
281
+ });
282
+ const handle = { launcher, scriptPath, statePath };
283
+ state.overlay = handle;
284
+ launcher.stdout?.on('data', (c) => {
285
+ const text = c.toString('utf8').trim();
286
+ const n = Number(text);
287
+ if (Number.isInteger(n) && n > 0) {
288
+ handle.pid = n;
289
+ logger?.info?.('computer overlay started', { pid: n, scriptPath, statePath });
290
+ }
291
+ else if (text) {
292
+ logger?.info?.('computer overlay launcher output', { text: text.slice(0, 300) });
293
+ }
294
+ });
295
+ launcher.stderr?.on('data', (c) => {
296
+ const text = c.toString('utf8').trim();
297
+ if (text)
298
+ logger?.warn?.('computer overlay launcher stderr', { text: text.slice(0, 800) });
299
+ });
300
+ launcher.on('error', (e) => logger?.warn?.('computer overlay launcher failed', { error: String(e?.message || e) }));
301
+ launcher.on('close', (code) => {
302
+ if (!handle.pid)
303
+ logger?.warn?.('computer overlay launcher exited without pid', { code });
304
+ });
305
+ return handle;
306
+ }
307
+ catch (e) {
308
+ logger?.warn?.('computer overlay create failed', { error: String(e?.message || e) });
309
+ return undefined;
310
+ }
311
+ }
312
+ function killPid(pid) {
313
+ if (!pid)
314
+ return;
315
+ try {
316
+ (0, node_child_process_1.spawn)('taskkill', ['/PID', String(pid), '/F'], { windowsHide: true, stdio: 'ignore' });
317
+ }
318
+ catch { /* ignore */ }
319
+ }
320
+ function stopOverlay() {
321
+ const handle = state.overlay;
322
+ if (!handle)
323
+ return;
324
+ state.overlay = undefined;
325
+ writeOverlayState(handle, false, true);
326
+ if (handle.closeTimer)
327
+ clearTimeout(handle.closeTimer);
328
+ handle.closeTimer = setTimeout(() => {
329
+ killPid(handle.pid);
330
+ try {
331
+ handle.launcher.kill();
332
+ }
333
+ catch { /* ignore */ }
334
+ try {
335
+ fs.rmSync(path.dirname(handle.scriptPath), { recursive: true, force: true });
336
+ }
337
+ catch { /* ignore */ }
338
+ }, 1200);
339
+ handle.closeTimer.unref?.();
340
+ }
341
+ process.once('exit', () => {
342
+ const handle = state.overlay;
343
+ if (!handle)
344
+ return;
345
+ writeOverlayState(handle, false, true);
346
+ killPid(handle.pid);
347
+ try {
348
+ handle.launcher.kill();
349
+ }
350
+ catch { /* ignore */ }
351
+ try {
352
+ fs.rmSync(path.dirname(handle.scriptPath), { recursive: true, force: true });
353
+ }
354
+ catch { /* ignore */ }
355
+ });
356
+ // ============================================================================
357
+ // 会话生命周期
358
+ // ============================================================================
359
+ function scheduleIdleStop() {
360
+ if (state.idleTimer)
361
+ clearTimeout(state.idleTimer);
362
+ state.idleTimer = setTimeout(() => stopSession('idle'), SESSION_IDLE_MS);
363
+ state.idleTimer.unref?.();
364
+ }
365
+ function touchSession() {
366
+ if (!state.active)
367
+ return;
368
+ scheduleIdleStop();
369
+ }
370
+ function startSession(ctx) {
371
+ if (!isWindows())
372
+ return platformError();
373
+ if (state.active) {
374
+ scheduleIdleStop();
375
+ return { ok: true, result: { active: true, note: '电脑操作会话已在进行中。' } };
376
+ }
377
+ state.active = true;
378
+ state.startedAt = Date.now();
379
+ state.logger = ctx.logger;
380
+ startOverlay(ctx.logger);
381
+ scheduleIdleStop();
382
+ if (state.maxTimer)
383
+ clearTimeout(state.maxTimer);
384
+ state.maxTimer = setTimeout(() => stopSession('max_duration'), SESSION_MAX_MS);
385
+ state.maxTimer.unref?.();
386
+ ctx.logger?.info?.('computer session started', { idleMs: SESSION_IDLE_MS, maxMs: SESSION_MAX_MS });
387
+ return {
388
+ ok: true,
389
+ result: {
390
+ active: true,
391
+ screen: state.screen,
392
+ note: '已获得电脑操作授权。屏幕右上角会显示操作提示。先 computer_screenshot 看清屏幕,再按坐标操作;完成后必须 computer_stop。',
393
+ },
394
+ };
395
+ }
396
+ function stopSession(reason) {
397
+ const wasActive = state.active;
398
+ state.active = false;
399
+ if (state.idleTimer) {
400
+ clearTimeout(state.idleTimer);
401
+ state.idleTimer = undefined;
402
+ }
403
+ if (state.maxTimer) {
404
+ clearTimeout(state.maxTimer);
405
+ state.maxTimer = undefined;
406
+ }
407
+ stopOverlay();
408
+ if (wasActive)
409
+ state.logger?.info?.('computer session stopped', { reason });
410
+ return { ok: true, result: { active: false, reason, note: reason === 'user' ? '电脑操作会话已结束。' : `电脑操作会话已自动结束(${reason})。` } };
411
+ }
412
+ function requireActive() {
413
+ if (!isWindows())
414
+ return platformError();
415
+ if (!state.active) {
416
+ return { ok: false, error: '电脑操作会话未开启:请先调用 computer_start,并等待用户确认授权。' };
417
+ }
418
+ touchSession();
419
+ return null;
420
+ }
421
+ // ============================================================================
422
+ // Windows 输入原语
423
+ // ============================================================================
424
+ const MOUSE_TYPE = `
425
+ using System;
426
+ using System.Runtime.InteropServices;
427
+ using System.Threading;
428
+ public class YsagcMouse {
429
+ [DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
430
+ [DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
431
+ [DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
432
+ public const uint LEFTDOWN=0x0002, LEFTUP=0x0004, RIGHTDOWN=0x0008, RIGHTUP=0x0010, MIDDLEDOWN=0x0020, MIDDLEUP=0x0040, WHEEL=0x0800, HWHEEL=0x01000;
433
+ static uint Down(int b){ return b==2?RIGHTDOWN:(b==3?MIDDLEDOWN:LEFTDOWN); }
434
+ static uint Up(int b){ return b==2?RIGHTUP:(b==3?MIDDLEUP:LEFTUP); }
435
+ public static void Init(){ SetProcessDPIAware(); }
436
+ public static void Move(int x,int y){ SetCursorPos(x,y); }
437
+ public static void Click(int x,int y,int button,int clicks){
438
+ SetCursorPos(x,y); Thread.Sleep(60);
439
+ for(int i=0;i<clicks;i++){ mouse_event(Down(button),0,0,0,UIntPtr.Zero); Thread.Sleep(35); mouse_event(Up(button),0,0,0,UIntPtr.Zero); Thread.Sleep(70); }
440
+ }
441
+ public static void Drag(int x1,int y1,int x2,int y2,int button){
442
+ SetCursorPos(x1,y1); Thread.Sleep(100);
443
+ mouse_event(Down(button),0,0,0,UIntPtr.Zero); Thread.Sleep(100);
444
+ int steps=24;
445
+ for(int i=1;i<=steps;i++){ int x=x1+(x2-x1)*i/steps; int y=y1+(y2-y1)*i/steps; SetCursorPos(x,y); Thread.Sleep(16); }
446
+ Thread.Sleep(80); mouse_event(Up(button),0,0,0,UIntPtr.Zero);
447
+ }
448
+ public static void Scroll(int x,int y,int dx,int dy){
449
+ SetCursorPos(x,y); Thread.Sleep(60);
450
+ if(dy!=0) mouse_event(WHEEL,0,0,unchecked((uint)dy),UIntPtr.Zero);
451
+ if(dx!=0) mouse_event(HWHEEL,0,0,unchecked((uint)dx),UIntPtr.Zero);
452
+ }
453
+ }`;
454
+ const KEYS_TYPE = `
455
+ using System;
456
+ using System.Runtime.InteropServices;
457
+ using System.Threading;
458
+ public class YsagcKeys {
459
+ [DllImport("user32.dll")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
460
+ public static void Press(int[] mods, int key, bool extended){
461
+ foreach(int m in mods){ keybd_event((byte)m,0,0,UIntPtr.Zero); Thread.Sleep(25); }
462
+ keybd_event((byte)key,0,extended?1u:0u,UIntPtr.Zero);
463
+ Thread.Sleep(45);
464
+ keybd_event((byte)key,0,(extended?1u:0u)|2u,UIntPtr.Zero);
465
+ for(int i=mods.Length-1;i>=0;i--) keybd_event((byte)mods[i],0,2u,UIntPtr.Zero);
466
+ }
467
+ }`;
468
+ const PASTE_TYPE = `
469
+ using System;
470
+ using System.Runtime.InteropServices;
471
+ public class YsagcPaste {
472
+ [DllImport("user32.dll")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
473
+ public static void Paste(){
474
+ keybd_event(0x11,0,0,UIntPtr.Zero);
475
+ keybd_event(0x56,0,0,UIntPtr.Zero);
476
+ keybd_event(0x56,0,2,UIntPtr.Zero);
477
+ keybd_event(0x11,0,2,UIntPtr.Zero);
478
+ }
479
+ }`;
480
+ function mouseScript(call) {
481
+ return `Add-Type -TypeDefinition @'\n${MOUSE_TYPE}\n'@\n[YsagcMouse]::Init()\n${call}`;
482
+ }
483
+ function buttonCode(name) {
484
+ const n = name.toLowerCase();
485
+ if (n === 'right')
486
+ return 2;
487
+ if (n === 'middle')
488
+ return 3;
489
+ return 1;
490
+ }
491
+ function psArray(nums) {
492
+ return `@(${nums.join(',')})`;
493
+ }
494
+ const VK = {
495
+ ctrl: 0x11, control: 0x11, shift: 0x10, alt: 0x12, win: 0x5B, meta: 0x5B,
496
+ enter: 0x0D, return: 0x0D, tab: 0x09, esc: 0x1B, escape: 0x1B, space: 0x20,
497
+ backspace: 0x08, delete: 0x2E, del: 0x2E, insert: 0x2D, ins: 0x2D,
498
+ home: 0x24, end: 0x23, pageup: 0x21, pgup: 0x21, pagedown: 0x22, pgdn: 0x22,
499
+ left: 0x25, up: 0x26, right: 0x27, down: 0x28, arrowleft: 0x25, arrowup: 0x26,
500
+ arrowright: 0x27, arrowdown: 0x28,
501
+ '-': 0xBD, '=': 0xBB, ',': 0xBC, '.': 0xBE, '/': 0xBF, ';': 0xBA, "'": 0xDE,
502
+ '[': 0xDB, ']': 0xDD, '\\\\': 0xDC, '`': 0xC0,
503
+ };
504
+ const EXTENDED_KEYS = new Set(['delete', 'del', 'insert', 'ins', 'home', 'end', 'pageup', 'pgup', 'pagedown', 'pgdn',
505
+ 'left', 'up', 'right', 'down', 'arrowleft', 'arrowup', 'arrowright', 'arrowdown']);
506
+ function keyCode(token) {
507
+ const t = token.trim().toLowerCase();
508
+ if (!t)
509
+ return null;
510
+ if (/^[a-z]$/.test(t))
511
+ return { code: t.toUpperCase().charCodeAt(0), extended: false };
512
+ if (/^[0-9]$/.test(t))
513
+ return { code: t.charCodeAt(0), extended: false };
514
+ if (/^f([1-9]|1[0-2])$/.test(t))
515
+ return { code: 0x70 + Number(t.slice(1)) - 1, extended: false };
516
+ if (t in VK)
517
+ return { code: VK[t], extended: EXTENDED_KEYS.has(t) };
518
+ return null;
519
+ }
520
+ function parseKeys(spec) {
521
+ const parts = spec.split('+').map((s) => s.trim()).filter(Boolean);
522
+ if (!parts.length)
523
+ throw new Error('keys 不能为空');
524
+ const last = parts.pop();
525
+ const key = keyCode(last);
526
+ if (!key)
527
+ throw new Error(`不认识的按键:${last}`);
528
+ const mods = [];
529
+ for (const p of parts) {
530
+ const m = keyCode(p);
531
+ if (!m || ![0x11, 0x10, 0x12, 0x5B].includes(m.code))
532
+ throw new Error(`不支持的组合键修饰符:${p}`);
533
+ mods.push(m.code);
534
+ }
535
+ return { mods, key: key.code, extended: key.extended };
536
+ }
537
+ // ============================================================================
538
+ // Tool executors
539
+ // ============================================================================
540
+ async function computerStart(ctx, _args, _signal, opts) {
541
+ if (!isWindows())
542
+ return platformError();
543
+ if (!opts?.preApproved) {
544
+ return {
545
+ ok: false,
546
+ needsApproval: true,
547
+ error: 'AI 请求操作你的电脑:接管鼠标、键盘与剪贴板。操作期间屏幕右上角会显示提示,可随时在工作台停止。',
548
+ };
549
+ }
550
+ return startSession(ctx);
551
+ }
552
+ async function computerStop(_ctx) {
553
+ return stopSession('user');
554
+ }
555
+ async function computerScreenshot(ctx, args) {
556
+ const blocked = requireActive();
557
+ if (blocked)
558
+ return blocked;
559
+ const shot = await (0, screenshot_1.screenshotTool)(ctx, {
560
+ quality: args.quality ?? 70,
561
+ ...(args.path ? { path: args.path } : {}),
562
+ });
563
+ if (!shot.ok)
564
+ return shot;
565
+ const size = await getScreenSize();
566
+ if (size) {
567
+ const result = shot.result && typeof shot.result === 'object' ? shot.result : {};
568
+ shot.result = {
569
+ ...result,
570
+ screen_width: size.width,
571
+ screen_height: size.height,
572
+ coordinate_note: `坐标以 ${size.width}x${size.height} 屏幕为准;如果图片被压缩,按 screen_width / 图片 width 的比例换算。`,
573
+ };
574
+ }
575
+ return shot;
576
+ }
577
+ async function computerMove(_ctx, args) {
578
+ const blocked = requireActive();
579
+ if (blocked)
580
+ return blocked;
581
+ const x = argInt(args, 'x');
582
+ const y = argInt(args, 'y');
583
+ const r = await runPowerShell(mouseScript(`[YsagcMouse]::Move(${x},${y})`));
584
+ return psResult(r, `鼠标已移动到 (${x}, ${y})`);
585
+ }
586
+ async function computerClick(_ctx, args) {
587
+ const blocked = requireActive();
588
+ if (blocked)
589
+ return blocked;
590
+ const x = argInt(args, 'x');
591
+ const y = argInt(args, 'y');
592
+ const button = buttonCode(argStr(args, 'button', 'left'));
593
+ const clicks = argInt(args, 'clicks', 1, 1, 3);
594
+ const r = await runPowerShell(mouseScript(`[YsagcMouse]::Click(${x},${y},${button},${clicks})`));
595
+ return psResult(r, `已在 (${x}, ${y}) ${clicks === 1 ? '单击' : `${clicks} 连击`}(${argStr(args, 'button', 'left')}键)`);
596
+ }
597
+ async function computerDoubleClick(_ctx, args) {
598
+ const blocked = requireActive();
599
+ if (blocked)
600
+ return blocked;
601
+ const x = argInt(args, 'x');
602
+ const y = argInt(args, 'y');
603
+ const button = buttonCode(argStr(args, 'button', 'left'));
604
+ const r = await runPowerShell(mouseScript(`[YsagcMouse]::Click(${x},${y},${button},2)`));
605
+ return psResult(r, `已在 (${x}, ${y}) 双击`);
606
+ }
607
+ async function computerDrag(_ctx, args) {
608
+ const blocked = requireActive();
609
+ if (blocked)
610
+ return blocked;
611
+ const x1 = argInt(args, 'from_x');
612
+ const y1 = argInt(args, 'from_y');
613
+ const x2 = argInt(args, 'to_x');
614
+ const y2 = argInt(args, 'to_y');
615
+ const button = buttonCode(argStr(args, 'button', 'left'));
616
+ const r = await runPowerShell(mouseScript(`[YsagcMouse]::Drag(${x1},${y1},${x2},${y2},${button})`));
617
+ return psResult(r, `已从 (${x1}, ${y1}) 拖拽到 (${x2}, ${y2})`);
618
+ }
619
+ async function computerScroll(_ctx, args) {
620
+ const blocked = requireActive();
621
+ if (blocked)
622
+ return blocked;
623
+ const x = argInt(args, 'x', 0);
624
+ const y = argInt(args, 'y', 0);
625
+ // 模型语义:delta_y 正数向下。Windows 滚轮正数向上,所以取反;一格 120。
626
+ const deltaY = argInt(args, 'delta_y', 600, -100000, 100000);
627
+ const deltaX = argInt(args, 'delta_x', 0, -100000, 100000);
628
+ const wheelY = -Math.round(deltaY / 120) * 120;
629
+ const wheelX = -Math.round(deltaX / 120) * 120;
630
+ const r = await runPowerShell(mouseScript(`[YsagcMouse]::Scroll(${x},${y},${wheelX},${wheelY})`));
631
+ return psResult(r, `已在 (${x}, ${y}) 滚动(纵向 ${deltaY}px,横向 ${deltaX}px)`);
632
+ }
633
+ async function computerType(_ctx, args) {
634
+ const blocked = requireActive();
635
+ if (blocked)
636
+ return blocked;
637
+ const text = argStr(args, 'text');
638
+ if (!text)
639
+ return { ok: false, error: '缺少要输入的内容(text)' };
640
+ if (text.length > 20000)
641
+ return { ok: false, error: '单次输入不能超过 20000 字符' };
642
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ysagc-type-'));
643
+ const file = path.join(dir, 'text.txt');
644
+ try {
645
+ fs.writeFileSync(file, text, 'utf8');
646
+ const script = `Add-Type -TypeDefinition @'\n${PASTE_TYPE}\n'@\n$old = Get-Clipboard -Raw -ErrorAction SilentlyContinue\n$text = [IO.File]::ReadAllText(${shellQuote(file)}, [Text.Encoding]::UTF8)\nSet-Clipboard -Value $text\nStart-Sleep -Milliseconds 120\n[YsagcPaste]::Paste()\nStart-Sleep -Milliseconds 320\nif ($old -is [string]) { Set-Clipboard -Value $old }`;
647
+ const r = await runPowerShell(script);
648
+ return psResult(r, `已输入 ${text.length} 个字符(通过剪贴板粘贴,兼容中文/表情)`);
649
+ }
650
+ finally {
651
+ try {
652
+ fs.rmSync(dir, { recursive: true, force: true });
653
+ }
654
+ catch { /* ignore */ }
655
+ }
656
+ }
657
+ async function computerKey(_ctx, args) {
658
+ const blocked = requireActive();
659
+ if (blocked)
660
+ return blocked;
661
+ const spec = argStr(args, 'keys');
662
+ if (!spec)
663
+ return { ok: false, error: '缺少按键(keys),如 enter、ctrl+c、ctrl+shift+t' };
664
+ let parsed;
665
+ try {
666
+ parsed = parseKeys(spec);
667
+ }
668
+ catch (e) {
669
+ return { ok: false, error: e?.message || String(e) };
670
+ }
671
+ const script = `Add-Type -TypeDefinition @'\n${KEYS_TYPE}\n'@\n[YsagcKeys]::Press(${psArray(parsed.mods)}, ${parsed.key}, $${parsed.extended ? 'true' : 'false'})`;
672
+ const r = await runPowerShell(script);
673
+ return psResult(r, `已按下 ${spec}`);
674
+ }
675
+ async function computerClipboardRead(_ctx) {
676
+ const blocked = requireActive();
677
+ if (blocked)
678
+ return blocked;
679
+ const r = await runPowerShell(`$t = Get-Clipboard -Raw -ErrorAction SilentlyContinue; if ($null -eq $t) { $t = '' }; [Console]::Out.Write($t)`);
680
+ if (r.code !== 0)
681
+ return psResult(r, '');
682
+ return { ok: true, result: { text: r.stdout, chars: r.stdout.length } };
683
+ }
684
+ async function computerClipboardWrite(_ctx, args) {
685
+ const blocked = requireActive();
686
+ if (blocked)
687
+ return blocked;
688
+ const text = argStr(args, 'text');
689
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ysagc-clip-'));
690
+ const file = path.join(dir, 'text.txt');
691
+ try {
692
+ fs.writeFileSync(file, text, 'utf8');
693
+ const r = await runPowerShell(`$text = [IO.File]::ReadAllText(${shellQuote(file)}, [Text.Encoding]::UTF8); Set-Clipboard -Value $text`);
694
+ return psResult(r, `已写入剪贴板(${text.length} 字符)`);
695
+ }
696
+ finally {
697
+ try {
698
+ fs.rmSync(dir, { recursive: true, force: true });
699
+ }
700
+ catch { /* ignore */ }
701
+ }
702
+ }
703
+ async function getScreenSize() {
704
+ if (!isWindows())
705
+ return null;
706
+ if (state.screen && Date.now() - state.screen.at < 60000)
707
+ return state.screen;
708
+ const r = await runPowerShell(`Add-Type -AssemblyName System.Windows.Forms\n` +
709
+ `Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class YsagcDpi { [DllImport("user32.dll")] public static extern bool SetProcessDPIAware(); }'\n` +
710
+ `[YsagcDpi]::SetProcessDPIAware() | Out-Null\n` +
711
+ `$b=[System.Windows.Forms.Screen]::PrimaryScreen.Bounds\n` +
712
+ `[Console]::Out.Write("$($b.Width)x$($b.Height)")`);
713
+ if (r.code !== 0)
714
+ return null;
715
+ const m = /^(\d+)x(\d+)$/.exec(r.stdout.trim());
716
+ if (!m)
717
+ return null;
718
+ const size = { width: Number(m[1]), height: Number(m[2]), at: Date.now() };
719
+ state.screen = size;
720
+ return size;
721
+ }
722
+ //# sourceMappingURL=computer.js.map