mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 13:55:50 +00:00
更新大量 tsx 原始文件; 已经迁移 login panel; 部分 (#121)
* style(B1-1): 格式化 ink/buddy/cli/context/screens/tasks/services/keybindings/state (43 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 修复了 Box.tsx 和 ScrollBox.tsx 中无效的 global.d.ts import。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style(B1-2): 格式化 commands (79 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style(B1-3): 格式化 components/messages,permissions,mcp,sandbox,shell (104 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style(B1-4): 格式化 components/PromptInput,FeedbackSurvey,tasks,agents,skills,design-system,wizard (73 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style(B1-5): 格式化 components其余 + hooks + tools (232 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style(B1-6): 格式化 main/entrypoints/utils/moreright (21 files) 纯格式化:移除分号、React Compiler import、import 多行展开。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 README,新增 Run.ps1/TODO.md,删除 V6.md - README.md: 大幅重写,更详细版本历史和配置示例 - Run.ps1: 新增 Windows 启动脚本 - TODO.md: 新增包完成清单 - V6.md: 删除(架构重构规划已不适用) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复以前的问题 * fix: 修复 login 面板的问题 --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
import * as React from 'react';
|
||||
import { MessageResponse } from '../../components/MessageResponse.js';
|
||||
import { Text } from '../../ink.js';
|
||||
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 '../../ink.js'
|
||||
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;
|
||||
};
|
||||
function fmtCoord(c: [number, number] | undefined): string {
|
||||
return c ? `(${c[0]}, ${c[1]})` : '';
|
||||
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]})` : ''
|
||||
}
|
||||
|
||||
const RESULT_SUMMARY: Readonly<Partial<Record<string, string>>> = {
|
||||
screenshot: 'Captured',
|
||||
zoom: 'Captured',
|
||||
@@ -32,8 +33,8 @@ const RESULT_SUMMARY: Readonly<Partial<Record<string, string>>> = {
|
||||
hold_key: 'Pressed',
|
||||
scroll: 'Scrolled',
|
||||
left_click_drag: 'Dragged',
|
||||
open_application: 'Opened'
|
||||
};
|
||||
open_application: 'Opened',
|
||||
}
|
||||
|
||||
/**
|
||||
* Rendering overrides for `mcp__computer-use__*` tools. Spread into the MCP
|
||||
@@ -41,18 +42,22 @@ 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;
|
||||
renderToolResultMessage: (output: MCPToolResult, progressMessages: 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
|
||||
} {
|
||||
return {
|
||||
userFacingName() {
|
||||
return `Computer Use[${toolName}]`;
|
||||
return `Computer Use[${toolName}]`
|
||||
},
|
||||
|
||||
// AssistantToolUseMessage.tsx contract: null hides the ENTIRE row, '' shows
|
||||
// the tool name without "(args)". Every path below returns '' when there's
|
||||
// nothing to show — never null.
|
||||
@@ -64,61 +69,89 @@ export function getComputerUseMCPRenderingOverrides(toolName: string): {
|
||||
case 'cursor_position':
|
||||
case 'list_granted_applications':
|
||||
case 'read_clipboard':
|
||||
return '';
|
||||
return ''
|
||||
|
||||
case 'left_click':
|
||||
case 'right_click':
|
||||
case 'middle_click':
|
||||
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)}`;
|
||||
return input.start_coordinate
|
||||
? `${fmtCoord(input.start_coordinate)} → ${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 [input.direction, input.amount && `×${input.amount}`, input.coordinate && `at ${fmtCoord(input.coordinate)}`].filter(Boolean).join(' ');
|
||||
case 'zoom':
|
||||
{
|
||||
const r = input.region;
|
||||
return Array.isArray(r) && r.length === 4 ? `[${r[0]}, ${r[1]}, ${r[2]}, ${r[3]}]` : '';
|
||||
}
|
||||
return [
|
||||
input.direction,
|
||||
input.amount && `×${input.amount}`,
|
||||
input.coordinate && `at ${fmtCoord(input.coordinate)}`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')
|
||||
|
||||
case 'zoom': {
|
||||
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) : '';
|
||||
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(', ');
|
||||
}
|
||||
case 'computer_batch':
|
||||
{
|
||||
const actions = input.actions;
|
||||
return Array.isArray(actions) ? `${actions.length} actions` : '';
|
||||
}
|
||||
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(', ')
|
||||
}
|
||||
|
||||
case 'computer_batch': {
|
||||
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;
|
||||
|
||||
renderToolResultMessage(output, _progress, { verbose }) {
|
||||
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;
|
||||
return <MessageResponse height={1}>
|
||||
const summary = RESULT_SUMMARY[toolName]
|
||||
if (!summary) return null
|
||||
return (
|
||||
<MessageResponse height={1}>
|
||||
<Text dimColor>{summary}</Text>
|
||||
</MessageResponse>;
|
||||
}
|
||||
};
|
||||
</MessageResponse>
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,22 +16,35 @@
|
||||
* GrowthBook gate `tengu_malort_pedway` (see gates.ts).
|
||||
*/
|
||||
|
||||
import { bindSessionContext, type ComputerUseSessionContext, type CuCallToolResult, type CuPermissionRequest, 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 { 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'];
|
||||
import {
|
||||
bindSessionContext,
|
||||
type ComputerUseSessionContext,
|
||||
type CuCallToolResult,
|
||||
type CuPermissionRequest,
|
||||
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 {
|
||||
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 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.
|
||||
@@ -46,35 +59,47 @@ 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;
|
||||
return d ? {
|
||||
...d,
|
||||
displayId: d.displayId ?? 0,
|
||||
originX: d.originX ?? 0,
|
||||
originY: d.originY ?? 0
|
||||
} : undefined;
|
||||
const d = tuc().getAppState().computerUseMcpState?.lastScreenshotDims
|
||||
return d
|
||||
? {
|
||||
...d,
|
||||
displayId: d.displayId ?? 0,
|
||||
originX: d.originX ?? 0,
|
||||
originY: d.originY ?? 0,
|
||||
}
|
||||
: undefined
|
||||
},
|
||||
|
||||
// ── Write-backs ────────────────────────────────────────────────────────
|
||||
// `setToolJSX` is guaranteed present — the gate in `main.tsx` excludes
|
||||
// non-interactive sessions. The package's `_dialogSignal` (tool-finished
|
||||
@@ -82,122 +107,143 @@ export function buildSessionContext(): ComputerUseSessionContext {
|
||||
// the dialog can't outlive it. Ctrl+C is what matters, and
|
||||
// `runPermissionDialog` wires that from the per-call ref's abortController.
|
||||
onPermissionRequest: (req, _dialogSignal) => runPermissionDialog(req),
|
||||
|
||||
// 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 sameFlags = prevFlags?.clipboardRead === flags.clipboardRead && prevFlags?.clipboardWrite === flags.clipboardWrite && prevFlags?.systemKeyCombos === flags.systemKeyCombos;
|
||||
return sameApps && sameFlags ? prev : {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
allowedApps: [...apps],
|
||||
grantFlags: flags
|
||||
}
|
||||
};
|
||||
}),
|
||||
onAppsHidden: ids => {
|
||||
if (ids.length === 0) return;
|
||||
onAllowedAppsChanged: (apps, flags) =>
|
||||
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 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
|
||||
return sameApps && sameFlags
|
||||
? prev
|
||||
: {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
allowedApps: [...apps],
|
||||
grantFlags: flags,
|
||||
},
|
||||
}
|
||||
}),
|
||||
|
||||
onAppsHidden: ids => {
|
||||
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
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
hiddenDuringTurn: new Set([...(existing ?? []), ...ids])
|
||||
}
|
||||
};
|
||||
});
|
||||
hiddenDuringTurn: new Set([...(existing ?? []), ...ids]),
|
||||
},
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// Resolver writeback only fires under a pin when Swift fell back to main
|
||||
// (pinned display unplugged) — the pin is semantically dead, so clear it
|
||||
// and the app-set key so the chase chain runs next time. When autoResolve
|
||||
// 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;
|
||||
}
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
selectedDisplayId: id,
|
||||
displayPinnedByModel: false,
|
||||
displayResolvedForApps: undefined
|
||||
onResolvedDisplayUpdated: id =>
|
||||
tuc().setAppState(prev => {
|
||||
const cu = prev.computerUseMcpState
|
||||
if (
|
||||
cu?.selectedDisplayId === id &&
|
||||
!cu.displayPinnedByModel &&
|
||||
cu.displayResolvedForApps === undefined
|
||||
) {
|
||||
return prev
|
||||
}
|
||||
};
|
||||
}),
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
selectedDisplayId: id,
|
||||
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;
|
||||
if (cu?.selectedDisplayId === id && cu?.displayPinnedByModel === pinned && cu?.displayResolvedForApps === nextResolvedFor) {
|
||||
return prev;
|
||||
}
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
selectedDisplayId: id,
|
||||
displayPinnedByModel: pinned,
|
||||
displayResolvedForApps: nextResolvedFor
|
||||
onDisplayPinned: id =>
|
||||
tuc().setAppState(prev => {
|
||||
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
|
||||
}
|
||||
};
|
||||
}),
|
||||
onDisplayResolvedForApps: key => tuc().setAppState(prev => {
|
||||
const cu = prev.computerUseMcpState;
|
||||
if (cu?.displayResolvedForApps === key) return prev;
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
displayResolvedForApps: key
|
||||
return {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
selectedDisplayId: id,
|
||||
displayPinnedByModel: pinned,
|
||||
displayResolvedForApps: nextResolvedFor,
|
||||
},
|
||||
}
|
||||
};
|
||||
}),
|
||||
onScreenshotCaptured: dims => tuc().setAppState(prev => {
|
||||
const cu = prev.computerUseMcpState;
|
||||
const p = cu?.lastScreenshotDims;
|
||||
return p?.width === dims.width && p?.height === dims.height && p?.displayWidth === dims.displayWidth && p?.displayHeight === dims.displayHeight && p?.displayId === dims.displayId && p?.originX === dims.originX && p?.originY === dims.originY ? prev : {
|
||||
...prev,
|
||||
computerUseMcpState: {
|
||||
...cu,
|
||||
lastScreenshotDims: dims
|
||||
}),
|
||||
|
||||
onDisplayResolvedForApps: key =>
|
||||
tuc().setAppState(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
|
||||
return p?.width === dims.width &&
|
||||
p?.height === dims.height &&
|
||||
p?.displayWidth === dims.displayWidth &&
|
||||
p?.displayHeight === dims.displayHeight &&
|
||||
p?.displayId === dims.displayId &&
|
||||
p?.originX === dims.originX &&
|
||||
p?.originY === dims.originY
|
||||
? prev
|
||||
: {
|
||||
...prev,
|
||||
computerUseMcpState: { ...cu, lastScreenshotDims: dims },
|
||||
}
|
||||
}),
|
||||
|
||||
// ── Lock — async, direct file-lock calls ───────────────────────────────
|
||||
// No `lockHolderForGate` dance: the package's gate is async now. It
|
||||
// awaits `checkCuLock`, and on `holder: undefined` + non-deferring tool
|
||||
// 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 }
|
||||
}
|
||||
},
|
||||
|
||||
// Called only when checkCuLock returned `holder: undefined`. The O_EXCL
|
||||
// acquire is atomic — if another process grabbed it in the gap (rare),
|
||||
// throw so the tool fails instead of proceeding without the lock.
|
||||
@@ -205,9 +251,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
|
||||
@@ -215,26 +261,34 @@ 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'
|
||||
});
|
||||
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
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,21 +296,25 @@ 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;
|
||||
};
|
||||
export function getComputerUseMCPToolOverrides(toolName: string): ComputerUseMCPToolOverrides {
|
||||
type ComputerUseMCPToolOverrides = ReturnType<
|
||||
typeof getComputerUseMCPRenderingOverrides
|
||||
> & {
|
||||
call: CallOverride
|
||||
}
|
||||
|
||||
export function getComputerUseMCPToolOverrides(
|
||||
toolName: string,
|
||||
): ComputerUseMCPToolOverrides {
|
||||
const call: CallOverride = async (args, context: ToolUseContext) => {
|
||||
currentToolUseContext = context;
|
||||
const {
|
||||
dispatch
|
||||
} = getOrBind();
|
||||
const {
|
||||
telemetry,
|
||||
...result
|
||||
} = await dispatch(toolName, args);
|
||||
currentToolUseContext = context
|
||||
const { dispatch } = getOrBind()
|
||||
|
||||
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
|
||||
@@ -265,25 +323,30 @@ export function getComputerUseMCPToolOverrides(toolName: string): ComputerUseMCP
|
||||
// shape just maps to the API's base64-source shape. The package's result
|
||||
// type admits audio/resource too, but CU's handleToolCall never emits
|
||||
// those; the fallthrough coerces them to empty text.
|
||||
const data = Array.isArray(result.content) ? result.content.map(item => item.type === 'image' ? {
|
||||
type: 'image' as const,
|
||||
source: {
|
||||
type: 'base64' as const,
|
||||
media_type: item.mimeType ?? 'image/jpeg',
|
||||
data: item.data
|
||||
}
|
||||
} : {
|
||||
type: 'text' as const,
|
||||
text: item.type === 'text' ? item.text : ''
|
||||
}) : result.content;
|
||||
return {
|
||||
data
|
||||
};
|
||||
};
|
||||
const data = Array.isArray(result.content)
|
||||
? result.content.map(item =>
|
||||
item.type === 'image'
|
||||
? {
|
||||
type: 'image' as const,
|
||||
source: {
|
||||
type: 'base64' as const,
|
||||
media_type: item.mimeType ?? 'image/jpeg',
|
||||
data: item.data,
|
||||
},
|
||||
}
|
||||
: {
|
||||
type: 'text' as const,
|
||||
text: item.type === 'text' ? item.text : '',
|
||||
},
|
||||
)
|
||||
: result.content
|
||||
return { data }
|
||||
}
|
||||
|
||||
return {
|
||||
...getComputerUseMCPRenderingOverrides(toolName),
|
||||
call
|
||||
};
|
||||
call,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,43 +356,43 @@ export function getComputerUseMCPToolOverrides(toolName: string): ComputerUseMCP
|
||||
* 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
|
||||
});
|
||||
});
|
||||
shouldHidePromptInput: true,
|
||||
})
|
||||
})
|
||||
} finally {
|
||||
setToolJSX(null);
|
||||
setToolJSX(null)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user