style: 完成所有文件的lint

This commit is contained in:
claude-code-best
2026-05-01 21:39:30 +08:00
parent d136872cc9
commit 6182015005
1333 changed files with 68255 additions and 77882 deletions

View File

@@ -41,7 +41,7 @@ export function registerEscHotkey(onEscape: () => void): boolean {
export function unregisterEscHotkey(): void {
if (!registered) return
try {
(requireComputerUseSwift() as any).hotkey?.unregister()
;(requireComputerUseSwift() as any).hotkey?.unregister()
} finally {
releasePump()
registered = false
@@ -51,5 +51,5 @@ export function unregisterEscHotkey(): void {
export function notifyExpectedEscape(): void {
if (!registered) return
(requireComputerUseSwift() as any).hotkey?.notifyExpectedEscape()
;(requireComputerUseSwift() as any).hotkey?.notifyExpectedEscape()
}

View File

@@ -69,18 +69,26 @@ function computeTargetDims(
async function readClipboardViaPbpaste(): Promise<string> {
if (process.platform === 'win32') {
const { stdout, code } = await execFileNoThrow('powershell', ['-NoProfile', '-Command', 'Get-Clipboard'], {
useCwd: false,
})
const { stdout, code } = await execFileNoThrow(
'powershell',
['-NoProfile', '-Command', 'Get-Clipboard'],
{
useCwd: false,
},
)
if (code !== 0) {
throw new Error(`PowerShell Get-Clipboard exited with code ${code}`)
}
return stdout
}
if (process.platform === 'linux') {
const { stdout, code } = await execFileNoThrow('xclip', ['-selection', 'clipboard', '-o'], {
useCwd: false,
})
const { stdout, code } = await execFileNoThrow(
'xclip',
['-selection', 'clipboard', '-o'],
{
useCwd: false,
},
)
if (code !== 0) {
throw new Error(`xclip exited with code ${code}`)
}
@@ -97,19 +105,31 @@ async function readClipboardViaPbpaste(): Promise<string> {
async function writeClipboardViaPbcopy(text: string): Promise<void> {
if (process.platform === 'win32') {
const { code } = await execFileNoThrow('powershell', ['-NoProfile', '-Command', `Set-Clipboard -Value '${text.replace(/'/g, "''")}'`], {
useCwd: false,
})
const { code } = await execFileNoThrow(
'powershell',
[
'-NoProfile',
'-Command',
`Set-Clipboard -Value '${text.replace(/'/g, "''")}'`,
],
{
useCwd: false,
},
)
if (code !== 0) {
throw new Error(`PowerShell Set-Clipboard exited with code ${code}`)
}
return
}
if (process.platform === 'linux') {
const { code } = await execFileNoThrow('xclip', ['-selection', 'clipboard'], {
input: text,
useCwd: false,
})
const { code } = await execFileNoThrow(
'xclip',
['-selection', 'clipboard'],
{
input: text,
useCwd: false,
},
)
if (code !== 0) {
throw new Error(`xclip exited with code ${code}`)
}
@@ -301,7 +321,8 @@ export function createCliExecutor(opts: {
// No macOS code paths, no drainRunLoop, no @ant packages.
if (process.platform !== 'darwin') {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createCrossPlatformExecutor } = require('./executorCrossPlatform.js') as typeof import('./executorCrossPlatform.js')
const { createCrossPlatformExecutor } =
require('./executorCrossPlatform.js') as typeof import('./executorCrossPlatform.js')
return createCrossPlatformExecutor(opts)
}
@@ -428,7 +449,10 @@ export function createCliExecutor(opts: {
// Ensure the result has fields expected by toolCalls.ts (hidden, displayId).
// macOS native returns these from Swift; our cross-platform ComputerUseAPI
// returns {base64, width, height} — fill in the missing fields.
const baseResult = raw as Partial<ResolvePrepareCaptureResult> & { width?: number; height?: number }
const baseResult = raw as Partial<ResolvePrepareCaptureResult> & {
width?: number
height?: number
}
return {
...raw,
displayWidth: baseResult.displayWidth ?? baseResult.width,
@@ -436,7 +460,8 @@ export function createCliExecutor(opts: {
originX: baseResult.originX ?? 0,
originY: baseResult.originY ?? 0,
hidden: baseResult.hidden ?? [],
displayId: baseResult.displayId ?? opts.preferredDisplayId ?? d.displayId,
displayId:
baseResult.displayId ?? opts.preferredDisplayId ?? d.displayId,
} as ResolvePrepareCaptureResult
},

View File

@@ -35,7 +35,11 @@ class DebugLogger implements Logger {
function checkAccessibilityJXA(): boolean {
try {
const result = Bun.spawnSync({
cmd: ['osascript', '-e', 'tell application "System Events" to get name of every process whose background only is false'],
cmd: [
'osascript',
'-e',
'tell application "System Events" to get name of every process whose background only is false',
],
stdout: 'pipe',
stderr: 'pipe',
})

View File

@@ -71,7 +71,13 @@ const input: InputPlatform = {
const screenshot: ScreenshotPlatform = {
async captureScreen(displayId) {
const swift = requireComputerUseSwift()
return swift.screenshot.captureExcluding([], undefined, undefined, undefined, displayId)
return swift.screenshot.captureExcluding(
[],
undefined,
undefined,
undefined,
displayId,
)
},
async captureRegion(x, y, w, h) {
@@ -110,7 +116,7 @@ const apps: AppsPlatform = {
const running = swift.apps.listRunning()
return running.map((app: any) => ({
id: app.bundleId ?? '',
pid: 0, // macOS listRunning doesn't expose PID through this API
pid: 0, // macOS listRunning doesn't expose PID through this API
title: app.displayName ?? '',
}))
},

View File

@@ -5,7 +5,13 @@
* Each backend implements the same unified interface.
*/
import type { InputPlatform, ScreenshotPlatform, DisplayPlatform, AppsPlatform, WindowManagementPlatform } from './types.js'
import type {
InputPlatform,
ScreenshotPlatform,
DisplayPlatform,
AppsPlatform,
WindowManagementPlatform,
} from './types.js'
export interface Platform {
input: InputPlatform
@@ -37,5 +43,18 @@ export function loadPlatform(): Platform {
return cached!
}
export type { InputPlatform, ScreenshotPlatform, DisplayPlatform, AppsPlatform, WindowManagementPlatform } from './types.js'
export type { WindowHandle, ScreenshotResult, DisplayInfo, InstalledApp, FrontmostAppInfo, WindowAction } from './types.js'
export type {
InputPlatform,
ScreenshotPlatform,
DisplayPlatform,
AppsPlatform,
WindowManagementPlatform,
} from './types.js'
export type {
WindowHandle,
ScreenshotResult,
DisplayInfo,
InstalledApp,
FrontmostAppInfo,
WindowAction,
} from './types.js'

View File

@@ -41,7 +41,11 @@ async function runAsync(cmd: string[]): Promise<string> {
}
function commandExists(name: string): boolean {
const result = Bun.spawnSync({ cmd: ['which', name], stdout: 'pipe', stderr: 'pipe' })
const result = Bun.spawnSync({
cmd: ['which', name],
stdout: 'pipe',
stderr: 'pipe',
})
return result.exitCode === 0
}
@@ -50,23 +54,75 @@ function commandExists(name: string): boolean {
// ---------------------------------------------------------------------------
const KEY_MAP: Record<string, string> = {
return: 'Return', enter: 'Return', tab: 'Tab', space: 'space',
backspace: 'BackSpace', delete: 'Delete', escape: 'Escape', esc: 'Escape',
left: 'Left', up: 'Up', right: 'Right', down: 'Down',
home: 'Home', end: 'End', pageup: 'Prior', pagedown: 'Next',
f1: 'F1', f2: 'F2', f3: 'F3', f4: 'F4', f5: 'F5', f6: 'F6',
f7: 'F7', f8: 'F8', f9: 'F9', f10: 'F10', f11: 'F11', f12: 'F12',
shift: 'shift', lshift: 'shift', rshift: 'shift',
control: 'ctrl', ctrl: 'ctrl', lcontrol: 'ctrl', rcontrol: 'ctrl',
alt: 'alt', option: 'alt', lalt: 'alt', ralt: 'alt',
win: 'super', meta: 'super', command: 'super', cmd: 'super', super: 'super',
insert: 'Insert', printscreen: 'Print', pause: 'Pause',
numlock: 'Num_Lock', capslock: 'Caps_Lock', scrolllock: 'Scroll_Lock',
return: 'Return',
enter: 'Return',
tab: 'Tab',
space: 'space',
backspace: 'BackSpace',
delete: 'Delete',
escape: 'Escape',
esc: 'Escape',
left: 'Left',
up: 'Up',
right: 'Right',
down: 'Down',
home: 'Home',
end: 'End',
pageup: 'Prior',
pagedown: 'Next',
f1: 'F1',
f2: 'F2',
f3: 'F3',
f4: 'F4',
f5: 'F5',
f6: 'F6',
f7: 'F7',
f8: 'F8',
f9: 'F9',
f10: 'F10',
f11: 'F11',
f12: 'F12',
shift: 'shift',
lshift: 'shift',
rshift: 'shift',
control: 'ctrl',
ctrl: 'ctrl',
lcontrol: 'ctrl',
rcontrol: 'ctrl',
alt: 'alt',
option: 'alt',
lalt: 'alt',
ralt: 'alt',
win: 'super',
meta: 'super',
command: 'super',
cmd: 'super',
super: 'super',
insert: 'Insert',
printscreen: 'Print',
pause: 'Pause',
numlock: 'Num_Lock',
capslock: 'Caps_Lock',
scrolllock: 'Scroll_Lock',
}
const MODIFIER_KEYS = new Set([
'shift', 'lshift', 'rshift', 'control', 'ctrl', 'lcontrol', 'rcontrol',
'alt', 'option', 'lalt', 'ralt', 'win', 'meta', 'command', 'cmd', 'super',
'shift',
'lshift',
'rshift',
'control',
'ctrl',
'lcontrol',
'rcontrol',
'alt',
'option',
'lalt',
'ralt',
'win',
'meta',
'command',
'cmd',
'super',
])
function mapKey(name: string): string {
@@ -83,11 +139,23 @@ function mouseButtonNum(button: 'left' | 'right' | 'middle'): string {
const input: InputPlatform = {
async moveMouse(x, y) {
run(['xdotool', 'mousemove', '--sync', String(Math.round(x)), String(Math.round(y))])
run([
'xdotool',
'mousemove',
'--sync',
String(Math.round(x)),
String(Math.round(y)),
])
},
async click(x, y, button) {
run(['xdotool', 'mousemove', '--sync', String(Math.round(x)), String(Math.round(y))])
run([
'xdotool',
'mousemove',
'--sync',
String(Math.round(x)),
String(Math.round(y)),
])
run(['xdotool', 'click', mouseButtonNum(button)])
},
@@ -125,11 +193,13 @@ const input: InputPlatform = {
if (direction === 'vertical') {
const btn = amount >= 0 ? '5' : '4'
const repeats = Math.abs(Math.round(amount))
if (repeats > 0) run(['xdotool', 'click', '--repeat', String(repeats), btn])
if (repeats > 0)
run(['xdotool', 'click', '--repeat', String(repeats), btn])
} else {
const btn = amount >= 0 ? '7' : '6'
const repeats = Math.abs(Math.round(amount))
if (repeats > 0) run(['xdotool', 'click', '--repeat', String(repeats), btn])
if (repeats > 0)
run(['xdotool', 'click', '--repeat', String(repeats), btn])
}
},
@@ -153,7 +223,11 @@ const input: InputPlatform = {
const SCREENSHOT_TMP = '/tmp/cu-screenshot-tmp.png'
const SCREENSHOT_JPG = '/tmp/cu-screenshot.jpg'
async function pngToJpegBase64(pngPath: string, width: number, height: number): Promise<ScreenshotResult> {
async function pngToJpegBase64(
pngPath: string,
width: number,
height: number,
): Promise<ScreenshotResult> {
// Try ImageMagick convert first
if (commandExists('convert')) {
await runAsync(['convert', pngPath, '-quality', '75', SCREENSHOT_JPG])
@@ -189,7 +263,13 @@ const screenshot: ScreenshotPlatform = {
async captureRegion(x, y, w, h) {
try {
await runAsync(['scrot', '-a', `${x},${y},${w},${h}`, '-o', SCREENSHOT_TMP])
await runAsync([
'scrot',
'-a',
`${x},${y},${w},${h}`,
'-o',
SCREENSHOT_TMP,
])
return pngToJpegBase64(SCREENSHOT_TMP, w, h)
} catch {
return { base64: '', width: w, height: h }
@@ -282,7 +362,9 @@ const apps: AppsPlatform = {
const title = parts.slice(4).join(' ')
let exePath = ''
try { exePath = run(['readlink', '-f', `/proc/${pid}/exe`]) } catch {}
try {
exePath = run(['readlink', '-f', `/proc/${pid}/exe`])
} catch {}
handles.push({
id: windowId ?? '',
@@ -294,11 +376,13 @@ const apps: AppsPlatform = {
// Deduplicate by id
const seen = new Set<string>()
return handles.filter(h => {
if (seen.has(h.id)) return false
seen.add(h.id)
return true
}).slice(0, 50)
return handles
.filter(h => {
if (seen.has(h.id)) return false
seen.add(h.id)
return true
})
.slice(0, 50)
}
// Fallback: xdotool search
@@ -307,7 +391,9 @@ const apps: AppsPlatform = {
for (const windowId of raw.split('\n').filter(Boolean).slice(0, 50)) {
const title = run(['xdotool', 'getwindowname', windowId])
let pid = 0
try { pid = Number(run(['xdotool', 'getwindowpid', windowId])) } catch {}
try {
pid = Number(run(['xdotool', 'getwindowpid', windowId]))
} catch {}
if (title) {
handles.push({ id: windowId, pid, title })
}
@@ -331,7 +417,9 @@ const apps: AppsPlatform = {
let files: string
try {
files = run(['find', dir, '-name', '*.desktop', '-maxdepth', '1'])
} catch { continue }
} catch {
continue
}
for (const filepath of files.split('\n').filter(Boolean)) {
try {
@@ -350,7 +438,9 @@ const apps: AppsPlatform = {
displayName: name,
path: exec.split(/\s+/)[0] ?? '',
})
} catch { /* skip unreadable */ }
} catch {
/* skip unreadable */
}
}
}
@@ -367,7 +457,9 @@ const apps: AppsPlatform = {
await runAsync(['gtk-launch', desktopName])
return
}
} catch { /* fall through */ }
} catch {
/* fall through */
}
await runAsync(['xdg-open', name])
},
@@ -380,12 +472,19 @@ const apps: AppsPlatform = {
if (!pidStr) return null
let exePath = ''
try { exePath = run(['readlink', '-f', `/proc/${pidStr}/exe`]) } catch {}
try {
exePath = run(['readlink', '-f', `/proc/${pidStr}/exe`])
} catch {}
let appName = ''
try { appName = run(['cat', `/proc/${pidStr}/comm`]) } catch {}
try {
appName = run(['cat', `/proc/${pidStr}/comm`])
} catch {}
if (!exePath && !appName) return null
return { id: exePath || `/proc/${pidStr}/exe`, appName: appName || 'unknown' }
return {
id: exePath || `/proc/${pidStr}/exe`,
appName: appName || 'unknown',
}
} catch {
return null
}
@@ -400,7 +499,9 @@ const apps: AppsPlatform = {
const windowTitle = run(['xdotool', 'getwindowname', windowId])
let pid = 0
try { pid = Number(run(['xdotool', 'getwindowpid', windowId])) } catch {}
try {
pid = Number(run(['xdotool', 'getwindowpid', windowId]))
} catch {}
return { id: windowId, pid, title: windowTitle }
} catch {

View File

@@ -269,7 +269,12 @@ const input: InputPlatform = {
button as 'left' | 'right',
)
if (!ok) {
getWm().sendClick(boundHwnd, Math.round(x), Math.round(y), button as 'left' | 'right')
getWm().sendClick(
boundHwnd,
Math.round(x),
Math.round(y),
button as 'left' | 'right',
)
}
return
}

View File

@@ -1,22 +1,22 @@
import * as React from 'react'
import { MessageResponse } from '../../components/MessageResponse.js'
import { Text } from '@anthropic/ink'
import { truncateToWidth } from '../format.js'
import type { MCPToolResult } from '../mcpValidation.js'
import * as React from 'react';
import { MessageResponse } from '../../components/MessageResponse.js';
import { Text } from '@anthropic/ink';
import { truncateToWidth } from '../format.js';
import type { MCPToolResult } from '../mcpValidation.js';
type CuToolInput = Record<string, unknown> & {
coordinate?: [number, number]
start_coordinate?: [number, number]
text?: string
apps?: Array<{ displayName?: string }>
region?: [number, number, number, number]
direction?: string
amount?: number
duration?: number
}
coordinate?: [number, number];
start_coordinate?: [number, number];
text?: string;
apps?: Array<{ displayName?: string }>;
region?: [number, number, number, number];
direction?: string;
amount?: number;
duration?: number;
};
function fmtCoord(c: [number, number] | undefined): string {
return c ? `(${c[0]}, ${c[1]})` : ''
return c ? `(${c[0]}, ${c[1]})` : '';
}
const RESULT_SUMMARY: Readonly<Partial<Record<string, string>>> = {
@@ -34,7 +34,7 @@ const RESULT_SUMMARY: Readonly<Partial<Record<string, string>>> = {
scroll: 'Scrolled',
left_click_drag: 'Dragged',
open_application: 'Opened',
}
};
/**
* Rendering overrides for `mcp__computer-use__*` tools. Spread into the MCP
@@ -42,20 +42,17 @@ const RESULT_SUMMARY: Readonly<Partial<Record<string, string>>> = {
* Mirror of `getClaudeInChromeMCPToolOverrides`.
*/
export function getComputerUseMCPRenderingOverrides(toolName: string): {
userFacingName: () => string
renderToolUseMessage: (
input: Record<string, unknown>,
options: { verbose: boolean },
) => React.ReactNode
userFacingName: () => string;
renderToolUseMessage: (input: Record<string, unknown>, options: { verbose: boolean }) => React.ReactNode;
renderToolResultMessage: (
output: MCPToolResult,
progressMessages: unknown[],
options: { verbose: boolean },
) => React.ReactNode
) => React.ReactNode;
} {
return {
userFacingName() {
return `Computer Use[${toolName}]`
return `Computer Use[${toolName}]`;
},
// AssistantToolUseMessage.tsx contract: null hides the ENTIRE row, '' shows
@@ -69,7 +66,7 @@ export function getComputerUseMCPRenderingOverrides(toolName: string): {
case 'cursor_position':
case 'list_granted_applications':
case 'read_clipboard':
return ''
return '';
case 'left_click':
case 'right_click':
@@ -77,21 +74,19 @@ export function getComputerUseMCPRenderingOverrides(toolName: string): {
case 'double_click':
case 'triple_click':
case 'mouse_move':
return fmtCoord(input.coordinate)
return fmtCoord(input.coordinate);
case 'left_click_drag':
return input.start_coordinate
? `${fmtCoord(input.start_coordinate)}${fmtCoord(input.coordinate)}`
: `to ${fmtCoord(input.coordinate)}`
: `to ${fmtCoord(input.coordinate)}`;
case 'type':
return typeof input.text === 'string'
? `"${truncateToWidth(input.text, 40)}"`
: ''
return typeof input.text === 'string' ? `"${truncateToWidth(input.text, 40)}"` : '';
case 'key':
case 'hold_key':
return typeof input.text === 'string' ? input.text : ''
return typeof input.text === 'string' ? input.text : '';
case 'scroll':
return [
@@ -100,58 +95,50 @@ export function getComputerUseMCPRenderingOverrides(toolName: string): {
input.coordinate && `at ${fmtCoord(input.coordinate)}`,
]
.filter(Boolean)
.join(' ')
.join(' ');
case 'zoom': {
const r = input.region
return Array.isArray(r) && r.length === 4
? `[${r[0]}, ${r[1]}, ${r[2]}, ${r[3]}]`
: ''
const r = input.region;
return Array.isArray(r) && r.length === 4 ? `[${r[0]}, ${r[1]}, ${r[2]}, ${r[3]}]` : '';
}
case 'wait':
return typeof input.duration === 'number' ? `${input.duration}s` : ''
return typeof input.duration === 'number' ? `${input.duration}s` : '';
case 'write_clipboard':
return typeof input.text === 'string'
? `"${truncateToWidth(input.text, 40)}"`
: ''
return typeof input.text === 'string' ? `"${truncateToWidth(input.text, 40)}"` : '';
case 'open_application':
return typeof input.bundle_id === 'string'
? String(input.bundle_id)
: ''
return typeof input.bundle_id === 'string' ? String(input.bundle_id) : '';
case 'request_access': {
const apps = input.apps
if (!Array.isArray(apps)) return ''
const names = apps
.map(a => (typeof a?.displayName === 'string' ? a.displayName : ''))
.filter(Boolean)
return names.join(', ')
const apps = input.apps;
if (!Array.isArray(apps)) return '';
const names = apps.map(a => (typeof a?.displayName === 'string' ? a.displayName : '')).filter(Boolean);
return names.join(', ');
}
case 'computer_batch': {
const actions = input.actions
return Array.isArray(actions) ? `${actions.length} actions` : ''
const actions = input.actions;
return Array.isArray(actions) ? `${actions.length} actions` : '';
}
default:
return ''
return '';
}
},
renderToolResultMessage(output, _progress, { verbose }) {
if (verbose || typeof output !== 'object' || output === null) return null
if (verbose || typeof output !== 'object' || output === null) return null;
// Non-verbose: one-line dim summary, like Chrome's pattern.
const summary = RESULT_SUMMARY[toolName]
if (!summary) return null
const summary = RESULT_SUMMARY[toolName];
if (!summary) return null;
return (
<MessageResponse height={1}>
<Text dimColor>{summary}</Text>
</MessageResponse>
)
);
},
}
};
}

View File

@@ -134,7 +134,7 @@ export async function call<T = unknown>(
const writable = stdin as unknown as Writable
writable.write(JSON.stringify(req) + '\n')
if (typeof (writable as any).flush === 'function') {
(writable as any).flush()
;(writable as any).flush()
}
}
} catch (err) {

View File

@@ -56,7 +56,9 @@ function excelCleanup(hasWorkbook = true): string {
const parts: string[] = []
if (hasWorkbook) parts.push('if ($wb) { $wb.Close($false) }')
parts.push('$excel.Quit()')
parts.push('[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null')
parts.push(
'[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null',
)
return parts.join('\n ')
}
@@ -107,8 +109,12 @@ try {
const parsed = JSON.parse(raw)
// Normalize: PowerShell single-element arrays become objects
const sheets: SheetInfo[] = Array.isArray(parsed.sheets) ? parsed.sheets : [parsed.sheets]
const sheetNames: string[] = Array.isArray(parsed.sheetNames) ? parsed.sheetNames : [parsed.sheetNames]
const sheets: SheetInfo[] = Array.isArray(parsed.sheets)
? parsed.sheets
: [parsed.sheets]
const sheetNames: string[] = Array.isArray(parsed.sheetNames)
? parsed.sheetNames
: [parsed.sheetNames]
return {
sheets: sheets.map((s: any) => ({
@@ -279,9 +285,7 @@ try {
* Save workbook. If savePath is given, SaveAs to that path; otherwise Save in place.
*/
export function saveExcel(filePath: string, savePath?: string): boolean {
const saveCmd = savePath
? `$wb.SaveAs('${escPath(savePath)}')`
: '$wb.Save()'
const saveCmd = savePath ? `$wb.SaveAs('${escPath(savePath)}')` : '$wb.Save()'
const script = `
${EXCEL_INIT}
try {

View File

@@ -192,10 +192,7 @@ export async function openWord(filePath: string): Promise<WordDocInfo> {
// ---------------------------------------------------------------------------
export async function readText(filePath: string): Promise<string> {
const script = wrapWordScript(
`Write-Output $doc.Content.Text`,
filePath,
)
const script = wrapWordScript(`Write-Output $doc.Content.Text`, filePath)
return runPs(script)
}
@@ -210,8 +207,12 @@ export async function appendText(
): Promise<boolean> {
const fontSetup = opts
? [
opts.bold !== undefined ? `$sel.Font.Bold = ${opts.bold ? '-1' : '0'}` : '',
opts.italic !== undefined ? `$sel.Font.Italic = ${opts.italic ? '-1' : '0'}` : '',
opts.bold !== undefined
? `$sel.Font.Bold = ${opts.bold ? '-1' : '0'}`
: '',
opts.italic !== undefined
? `$sel.Font.Italic = ${opts.italic ? '-1' : '0'}`
: '',
opts.fontSize !== undefined ? `$sel.Font.Size = ${opts.fontSize}` : '',
opts.fontName ? `$sel.Font.Name = '${psEscape(opts.fontName)}'` : '',
]
@@ -324,10 +325,7 @@ export async function insertTable(
): Promise<boolean> {
// Build PowerShell array literal for the data
const psData = data
.map(
(row) =>
',@(' + row.map((cell) => `'${psEscape(cell)}'`).join(',') + ')',
)
.map(row => ',@(' + row.map(cell => `'${psEscape(cell)}'`).join(',') + ')')
.join('\n ')
const body = `

View File

@@ -24,28 +24,25 @@ import {
type CuPermissionResponse,
DEFAULT_GRANT_FLAGS,
type ScreenshotDims,
} from '@ant/computer-use-mcp'
import * as React from 'react'
import { getSessionId } from '../../bootstrap/state.js'
import { ComputerUseApproval } from '../../components/permissions/ComputerUseApproval/ComputerUseApproval.js'
import type { Tool, ToolUseContext } from '../../Tool.js'
import { logForDebugging } from '../debug.js'
import { detectImageFormatFromBase64 } from '../imageResizer.js'
import {
checkComputerUseLock,
tryAcquireComputerUseLock,
} from './computerUseLock.js'
import { registerEscHotkey } from './escHotkey.js'
import { getChicagoCoordinateMode } from './gates.js'
import { getComputerUseHostAdapter } from './hostAdapter.js'
import { getComputerUseMCPRenderingOverrides } from './toolRendering.js'
} from '@ant/computer-use-mcp';
import * as React from 'react';
import { getSessionId } from '../../bootstrap/state.js';
import { ComputerUseApproval } from '../../components/permissions/ComputerUseApproval/ComputerUseApproval.js';
import type { Tool, ToolUseContext } from '../../Tool.js';
import { logForDebugging } from '../debug.js';
import { detectImageFormatFromBase64 } from '../imageResizer.js';
import { checkComputerUseLock, tryAcquireComputerUseLock } from './computerUseLock.js';
import { registerEscHotkey } from './escHotkey.js';
import { getChicagoCoordinateMode } from './gates.js';
import { getComputerUseHostAdapter } from './hostAdapter.js';
import { getComputerUseMCPRenderingOverrides } from './toolRendering.js';
type CallOverride = Pick<Tool, 'call'>['call']
type CallOverride = Pick<Tool, 'call'>['call'];
type Binding = {
ctx: ComputerUseSessionContext
dispatch: (name: string, args: unknown) => Promise<CuCallToolResult>
}
ctx: ComputerUseSessionContext;
dispatch: (name: string, args: unknown) => Promise<CuCallToolResult>;
};
/**
* Cached binding — built on first `.call()`, reused for process lifetime.
@@ -60,37 +57,31 @@ type Binding = {
* its internal screenshot blob survives, but `ToolUseContext` is per-call.
* Tests will need to either inject the cache or run serially.
*/
let binding: Binding | undefined
let currentToolUseContext: ToolUseContext | undefined
let binding: Binding | undefined;
let currentToolUseContext: ToolUseContext | undefined;
function tuc(): ToolUseContext {
// Safe: `binding` is only populated when `currentToolUseContext` is set.
// Called only from within `ctx` callbacks, which only fire during dispatch.
return currentToolUseContext!
return currentToolUseContext!;
}
function formatLockHeld(holder: string): string {
return `Computer use is in use by another Claude session (${holder.slice(0, 8)}…). Wait for that session to finish or run /exit there.`
return `Computer use is in use by another Claude session (${holder.slice(0, 8)}…). Wait for that session to finish or run /exit there.`;
}
export function buildSessionContext(): ComputerUseSessionContext {
return {
// ── Read state fresh via the per-call ref ─────────────────────────────
getAllowedApps: () =>
tuc().getAppState().computerUseMcpState?.allowedApps ?? [],
getGrantFlags: () =>
tuc().getAppState().computerUseMcpState?.grantFlags ??
DEFAULT_GRANT_FLAGS,
getAllowedApps: () => tuc().getAppState().computerUseMcpState?.allowedApps ?? [],
getGrantFlags: () => tuc().getAppState().computerUseMcpState?.grantFlags ?? DEFAULT_GRANT_FLAGS,
// cc-2 has no Settings page for user-denied apps yet.
getUserDeniedBundleIds: () => [],
getSelectedDisplayId: () =>
tuc().getAppState().computerUseMcpState?.selectedDisplayId,
getDisplayPinnedByModel: () =>
tuc().getAppState().computerUseMcpState?.displayPinnedByModel ?? false,
getDisplayResolvedForApps: () =>
tuc().getAppState().computerUseMcpState?.displayResolvedForApps,
getSelectedDisplayId: () => tuc().getAppState().computerUseMcpState?.selectedDisplayId,
getDisplayPinnedByModel: () => tuc().getAppState().computerUseMcpState?.displayPinnedByModel ?? false,
getDisplayResolvedForApps: () => tuc().getAppState().computerUseMcpState?.displayResolvedForApps,
getLastScreenshotDims: (): ScreenshotDims | undefined => {
const d = tuc().getAppState().computerUseMcpState?.lastScreenshotDims
const d = tuc().getAppState().computerUseMcpState?.lastScreenshotDims;
return d
? {
...d,
@@ -98,7 +89,7 @@ export function buildSessionContext(): ComputerUseSessionContext {
originX: d.originX ?? 0,
originY: d.originY ?? 0,
}
: undefined
: undefined;
},
// ── Write-backs ────────────────────────────────────────────────────────
@@ -112,16 +103,14 @@ export function buildSessionContext(): ComputerUseSessionContext {
// Package does the merge (dedupe + truthy-only flags). We just persist.
onAllowedAppsChanged: (apps, flags) =>
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
const prevApps = cu?.allowedApps
const prevFlags = cu?.grantFlags
const sameApps =
prevApps?.length === apps.length &&
apps.every((a, i) => prevApps[i]?.bundleId === a.bundleId)
const cu = prev.computerUseMcpState;
const prevApps = cu?.allowedApps;
const prevFlags = cu?.grantFlags;
const sameApps = prevApps?.length === apps.length && apps.every((a, i) => prevApps[i]?.bundleId === a.bundleId);
const sameFlags =
prevFlags?.clipboardRead === flags.clipboardRead &&
prevFlags?.clipboardWrite === flags.clipboardWrite &&
prevFlags?.systemKeyCombos === flags.systemKeyCombos
prevFlags?.systemKeyCombos === flags.systemKeyCombos;
return sameApps && sameFlags
? prev
: {
@@ -131,23 +120,23 @@ export function buildSessionContext(): ComputerUseSessionContext {
allowedApps: [...apps],
grantFlags: flags,
},
}
};
}),
onAppsHidden: ids => {
if (ids.length === 0) return
if (ids.length === 0) return;
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
const existing = cu?.hiddenDuringTurn
if (existing && ids.every(id => existing.has(id))) return prev
const cu = prev.computerUseMcpState;
const existing = cu?.hiddenDuringTurn;
if (existing && ids.every(id => existing.has(id))) return prev;
return {
...prev,
computerUseMcpState: {
...cu,
hiddenDuringTurn: new Set([...(existing ?? []), ...ids]),
},
}
})
};
});
},
// Resolver writeback only fires under a pin when Swift fell back to main
@@ -156,13 +145,9 @@ export function buildSessionContext(): ComputerUseSessionContext {
// was true, onDisplayResolvedForApps re-sets the key in the same tick.
onResolvedDisplayUpdated: id =>
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
if (
cu?.selectedDisplayId === id &&
!cu.displayPinnedByModel &&
cu.displayResolvedForApps === undefined
) {
return prev
const cu = prev.computerUseMcpState;
if (cu?.selectedDisplayId === id && !cu.displayPinnedByModel && cu.displayResolvedForApps === undefined) {
return prev;
}
return {
...prev,
@@ -172,22 +157,22 @@ export function buildSessionContext(): ComputerUseSessionContext {
displayPinnedByModel: false,
displayResolvedForApps: undefined,
},
}
};
}),
// switch_display(name) pins; switch_display("auto") unpins and clears the
// app-set key so the next screenshot auto-resolves fresh.
onDisplayPinned: id =>
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
const pinned = id !== undefined
const nextResolvedFor = pinned ? cu?.displayResolvedForApps : undefined
const cu = prev.computerUseMcpState;
const pinned = id !== undefined;
const nextResolvedFor = pinned ? cu?.displayResolvedForApps : undefined;
if (
cu?.selectedDisplayId === id &&
cu?.displayPinnedByModel === pinned &&
cu?.displayResolvedForApps === nextResolvedFor
) {
return prev
return prev;
}
return {
...prev,
@@ -197,23 +182,23 @@ export function buildSessionContext(): ComputerUseSessionContext {
displayPinnedByModel: pinned,
displayResolvedForApps: nextResolvedFor,
},
}
};
}),
onDisplayResolvedForApps: key =>
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
if (cu?.displayResolvedForApps === key) return prev
const cu = prev.computerUseMcpState;
if (cu?.displayResolvedForApps === key) return prev;
return {
...prev,
computerUseMcpState: { ...cu, displayResolvedForApps: key },
}
};
}),
onScreenshotCaptured: dims =>
tuc().setAppState(prev => {
const cu = prev.computerUseMcpState
const p = cu?.lastScreenshotDims
const cu = prev.computerUseMcpState;
const p = cu?.lastScreenshotDims;
return p?.width === dims.width &&
p?.height === dims.height &&
p?.displayWidth === dims.displayWidth &&
@@ -225,7 +210,7 @@ export function buildSessionContext(): ComputerUseSessionContext {
: {
...prev,
computerUseMcpState: { ...cu, lastScreenshotDims: dims },
}
};
}),
// ── Lock — async, direct file-lock calls ───────────────────────────────
@@ -234,14 +219,14 @@ export function buildSessionContext(): ComputerUseSessionContext {
// awaits `acquireCuLock`. `defersLockAcquire` is the PACKAGE's set —
// the local copy is gone.
checkCuLock: async () => {
const c = await checkComputerUseLock()
const c = await checkComputerUseLock();
switch (c.kind) {
case 'free':
return { holder: undefined, isSelf: false }
return { holder: undefined, isSelf: false };
case 'held_by_self':
return { holder: getSessionId(), isSelf: true }
return { holder: getSessionId(), isSelf: true };
case 'blocked':
return { holder: c.by, isSelf: false }
return { holder: c.by, isSelf: false };
}
},
@@ -252,9 +237,9 @@ export function buildSessionContext(): ComputerUseSessionContext {
// but is possible under parallel tool-use interleaving — don't spam the
// notification in that case.
acquireCuLock: async () => {
const r = await tryAcquireComputerUseLock()
const r = await tryAcquireComputerUseLock();
if (r.kind === 'blocked') {
throw new Error(formatLockHeld(r.by))
throw new Error(formatLockHeld(r.by));
}
if (r.fresh) {
// Global Escape → abort. Consumes the event (PI defense — prompt
@@ -262,34 +247,30 @@ export function buildSessionContext(): ComputerUseSessionContext {
// CFRunLoopSource is processed by the drainRunLoop pump, so this
// holds a pump retain until unregisterEscHotkey() in cleanup.ts.
const escRegistered = registerEscHotkey(() => {
logForDebugging('[cu-esc] user escape, aborting turn')
tuc().abortController.abort()
})
logForDebugging('[cu-esc] user escape, aborting turn');
tuc().abortController.abort();
});
tuc().sendOSNotification?.({
message: escRegistered
? 'Claude is using your computer · press Esc to stop'
: 'Claude is using your computer · press Ctrl+C to stop',
notificationType: 'computer_use_enter',
})
});
}
},
formatLockHeldMessage: formatLockHeld,
}
};
}
function getOrBind(): Binding {
if (binding) return binding
const ctx = buildSessionContext()
if (binding) return binding;
const ctx = buildSessionContext();
binding = {
ctx,
dispatch: bindSessionContext(
getComputerUseHostAdapter(),
getChicagoCoordinateMode(),
ctx,
),
}
return binding
dispatch: bindSessionContext(getComputerUseHostAdapter(), getChicagoCoordinateMode(), ctx),
};
return binding;
}
/**
@@ -297,25 +278,19 @@ function getOrBind(): Binding {
* tool: rendering overrides from `toolRendering.tsx` plus a `.call()` that
* dispatches through the cached binder.
*/
type ComputerUseMCPToolOverrides = ReturnType<
typeof getComputerUseMCPRenderingOverrides
> & {
call: CallOverride
}
type ComputerUseMCPToolOverrides = ReturnType<typeof getComputerUseMCPRenderingOverrides> & {
call: CallOverride;
};
export function getComputerUseMCPToolOverrides(
toolName: string,
): ComputerUseMCPToolOverrides {
export function getComputerUseMCPToolOverrides(toolName: string): ComputerUseMCPToolOverrides {
const call: CallOverride = async (args, context: ToolUseContext) => {
currentToolUseContext = context
const { dispatch } = getOrBind()
currentToolUseContext = context;
const { dispatch } = getOrBind();
const { telemetry, ...result } = await dispatch(toolName, args)
const { telemetry, ...result } = await dispatch(toolName, args);
if (telemetry?.error_kind) {
logForDebugging(
`[Computer Use MCP] ${toolName} error_kind=${telemetry.error_kind}`,
)
logForDebugging(`[Computer Use MCP] ${toolName} error_kind=${telemetry.error_kind}`);
}
// MCP content blocks → Anthropic API blocks. CU only produces text and
@@ -340,14 +315,14 @@ export function getComputerUseMCPToolOverrides(
text: item.type === 'text' ? item.text : '',
},
)
: result.content
return { data }
}
: result.content;
return { data };
};
return {
...getComputerUseMCPRenderingOverrides(toolName),
call,
}
};
}
/**
@@ -357,43 +332,41 @@ export function getComputerUseMCPToolOverrides(
* The merge-into-AppState that used to live here (dedupe + truthy-only flags)
* is now in the package's `bindSessionContext` → `onAllowedAppsChanged`.
*/
async function runPermissionDialog(
req: CuPermissionRequest,
): Promise<CuPermissionResponse> {
const context = tuc()
const setToolJSX = context.setToolJSX
async function runPermissionDialog(req: CuPermissionRequest): Promise<CuPermissionResponse> {
const context = tuc();
const setToolJSX = context.setToolJSX;
if (!setToolJSX) {
// Shouldn't happen — main.tsx gate excludes non-interactive. Fail safe.
return { granted: [], denied: [], flags: DEFAULT_GRANT_FLAGS }
return { granted: [], denied: [], flags: DEFAULT_GRANT_FLAGS };
}
try {
return await new Promise<CuPermissionResponse>((resolve, reject) => {
const signal = context.abortController.signal
const signal = context.abortController.signal;
// If already aborted, addEventListener won't fire — reject now so the
// promise doesn't hang waiting for a user who Ctrl+C'd.
if (signal.aborted) {
reject(new Error('Computer Use permission dialog aborted'))
return
reject(new Error('Computer Use permission dialog aborted'));
return;
}
const onAbort = (): void => {
signal.removeEventListener('abort', onAbort)
reject(new Error('Computer Use permission dialog aborted'))
}
signal.addEventListener('abort', onAbort)
signal.removeEventListener('abort', onAbort);
reject(new Error('Computer Use permission dialog aborted'));
};
signal.addEventListener('abort', onAbort);
setToolJSX({
jsx: React.createElement(ComputerUseApproval, {
request: req,
onDone: (resp: CuPermissionResponse) => {
signal.removeEventListener('abort', onAbort)
resolve(resp)
signal.removeEventListener('abort', onAbort);
resolve(resp);
},
}),
shouldHidePromptInput: true,
})
})
});
});
} finally {
setToolJSX(null)
setToolJSX(null);
}
}