mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-27 20:00:51 +00:00
* feat(tabs): move a tab to its own window without reconnecting (#4201) Adds "Move Tab to New Window" to the tab context menu. Instead of opening a new connection like "Copy Tab to New Window", the tab is hidden from the tab bar and an attach popup takes over the same live PTY, so bastion sessions are not re-authenticated and no extra seat is used. Closing the popup returns the tab (with its screen state) to the window it came from. - popup lifecycle notifies the home window via terminal-popup:attach-closed - new runtime poppedOut flag; popped-out tabs still persist for restore and are not labelled as AI silent sessions in the tray * fix(tabs): keep moved tabs alive when their source window closes - A moved-tab popup registers as the lifetime heir of its session. If the source window is destroyed, ownership passes to the popup instead of the session being closed (worker and in-process paths). AI observe popups are unchanged. - If the source window is gone when the popup closes, skip the hand-back so the popup's ownership closes the session. - Only offer/allow Move Tab to New Window for connected sessions; attach mode cannot connect or reconnect a backend. * fix(tabs): expose Move Tab to New Window through TerminalHost The handler was registered but not passed into the terminal domain bag, so AppView received undefined and the menu item never rendered. * fix(tabs): address move-to-window focus, crash ownership, live count, history, and AI context review findings * fix(tabs): activate moved-tab lifetime heir only after output rebind succeeds * fix(tabs): wait for popup attachment before hiding tab and exclude plugin sessions * fix(tabs): confirm renderer reattach before resolving move and await output restore before unhiding tab * fix(tabs): preserve detached output across popup crashes and avoid focusing main window from tray * fix(tabs): deliver attach snapshots and output restore directly to peer session windows * fix(tabs): schedule automatic output restore retries when popup recovery fails * fix(tabs): handle reopen replay preservation, stale restore cancellation, startup workspace move, and shutdown log capture * fix(tabs): reassert popped-out state after tray reopen * test(terminal): provide log finalizer stubs in popup snapshot harness --------- Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
205 lines
6.4 KiB
TypeScript
205 lines
6.4 KiB
TypeScript
import type { ToolCall, ToolResult, AIPermissionMode, WebSearchConfig } from '../types';
|
|
import type {
|
|
TerminalContextReader,
|
|
} from '../../../domain/terminalContextRead';
|
|
import {
|
|
executeTerminalExecute,
|
|
executeWorkspaceGetInfo,
|
|
executeWorkspaceGetSessionInfo,
|
|
executeWebSearch,
|
|
executeUrlFetch,
|
|
type ToolDeps,
|
|
type ToolExecResult,
|
|
} from '../shared/toolExecutors';
|
|
import { fitTerminalExecuteResultForModel } from '../harness/terminalCompression';
|
|
|
|
/**
|
|
* Bridge interface for Catty Agent to interact with the Electron main process.
|
|
* This mirrors the AI-related subset of window.netcatty from electron/preload.cjs.
|
|
*/
|
|
export interface NetcattyBridge {
|
|
aiExec(
|
|
sessionId: string,
|
|
command: string,
|
|
chatSessionId?: string,
|
|
delegationTag?: string,
|
|
): Promise<{
|
|
ok: boolean;
|
|
stdout?: string;
|
|
stderr?: string;
|
|
exitCode?: number;
|
|
error?: string;
|
|
code?: string;
|
|
}>;
|
|
/**
|
|
* Cancel any in-flight Catty Agent command execution scoped to the
|
|
* given chat session. Idempotent — safe to call when nothing is
|
|
* running. Used by tools to re-issue cancel during the IPC transit
|
|
* window if the user clicks Stop after we've already dispatched
|
|
* `aiExec` but before the main process has registered it.
|
|
*/
|
|
aiCattyCancelExec?(chatSessionId: string, delegationTag?: string): Promise<unknown>;
|
|
aiSetChatSessionCancelled?(chatSessionId: string, cancelled?: boolean, delegationTag?: string): Promise<{ ok: boolean; error?: string }>;
|
|
aiCapability?(
|
|
rpcMethod: string,
|
|
params: Record<string, unknown>,
|
|
chatSessionId?: string,
|
|
delegationTag?: string,
|
|
): Promise<unknown>;
|
|
}
|
|
|
|
// Workspace context provided to the executor
|
|
export interface ExecutorContext {
|
|
// Available sessions in scope
|
|
sessions: Array<{
|
|
sessionId: string;
|
|
hostId: string;
|
|
hostname: string;
|
|
label: string;
|
|
os?: string;
|
|
username?: string;
|
|
protocol?: string;
|
|
shellType?: string;
|
|
deviceType?: string;
|
|
connected: boolean;
|
|
}>;
|
|
// Workspace info
|
|
workspaceId?: string;
|
|
workspaceName?: string;
|
|
/** Sessions currently rendered in a detached/attach popup rather than the home renderer. */
|
|
detachedSessionIds?: ReadonlySet<string>;
|
|
readTerminalContext?: TerminalContextReader;
|
|
}
|
|
|
|
/** Convert a shared ToolExecResult into the executor's ToolResult format. */
|
|
function toToolResult(toolCallId: string, r: ToolExecResult): ToolResult {
|
|
if (r.ok === false) {
|
|
// #4195: an unrecognized prompt on a live shell is an actionable,
|
|
// non-terminal state, not a fatal tool error. The exec gate already refused
|
|
// to send the command; surface the honest + viable-path hint as a normal
|
|
// tool result (no isError) so the agent inspects / re-checks / confirms.
|
|
if (r.code === 'SHELL_READINESS_UNKNOWN') {
|
|
return { toolCallId, content: r.error };
|
|
}
|
|
if (
|
|
typeof r.data === 'object'
|
|
&& r.data !== null
|
|
&& 'stdout' in r.data
|
|
&& 'stderr' in r.data
|
|
&& 'exitCode' in r.data
|
|
) {
|
|
const fitted = fitTerminalExecuteResultForModel(r.data as {
|
|
stdout: string;
|
|
stderr: string;
|
|
exitCode: number | null;
|
|
});
|
|
const output = [
|
|
r.error,
|
|
fitted.stdout ? `Partial output:\n${fitted.stdout}` : '',
|
|
fitted.stderr ? `Stderr:\n${fitted.stderr}` : '',
|
|
].filter(Boolean).join('\n\n');
|
|
return { toolCallId, content: output, isError: true };
|
|
}
|
|
return { toolCallId, content: r.error, isError: true };
|
|
}
|
|
// For terminal_execute, format as the legacy STDOUT/STDERR/exitCode text block
|
|
if (
|
|
typeof r.data === 'object' &&
|
|
r.data !== null &&
|
|
'stdout' in r.data &&
|
|
'stderr' in r.data &&
|
|
'exitCode' in r.data
|
|
) {
|
|
const d = r.data as { stdout: string; stderr: string; exitCode: number };
|
|
const output = [
|
|
d.stdout ? `STDOUT:\n${d.stdout}` : '',
|
|
d.stderr ? `STDERR:\n${d.stderr}` : '',
|
|
`Exit code: ${d.exitCode === -1 ? 'unknown' : d.exitCode}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join('\n\n');
|
|
return { toolCallId, content: output || 'Command completed (no output)' };
|
|
}
|
|
// Default: JSON-serialize the data
|
|
return { toolCallId, content: JSON.stringify(r.data, null, 2) };
|
|
}
|
|
|
|
/**
|
|
* Create a tool executor function for the Catty Agent.
|
|
* This bridges tool calls to the netcatty Electron IPC layer.
|
|
*/
|
|
export function createToolExecutor(
|
|
bridge: NetcattyBridge | undefined,
|
|
context: ExecutorContext,
|
|
commandBlocklist?: string[],
|
|
permissionMode: AIPermissionMode = 'confirm',
|
|
webSearchConfig?: WebSearchConfig,
|
|
chatSessionId?: string,
|
|
): (toolCall: ToolCall) => Promise<ToolResult> {
|
|
return async (toolCall: ToolCall): Promise<ToolResult> => {
|
|
if (!bridge) {
|
|
return {
|
|
toolCallId: toolCall.id,
|
|
content: 'Netcatty bridge is not available',
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const deps: ToolDeps = { bridge, context, commandBlocklist, permissionMode, webSearchConfig, chatSessionId };
|
|
const args = toolCall.arguments;
|
|
|
|
try {
|
|
switch (toolCall.name) {
|
|
case 'terminal_execute': {
|
|
const r = await executeTerminalExecute(deps, {
|
|
sessionId: String(args.sessionId || ''),
|
|
command: String(args.command || ''),
|
|
});
|
|
return toToolResult(toolCall.id, r);
|
|
}
|
|
|
|
case 'workspace_get_info': {
|
|
const r = executeWorkspaceGetInfo(deps);
|
|
return toToolResult(toolCall.id, r);
|
|
}
|
|
|
|
case 'workspace_get_session_info': {
|
|
const r = executeWorkspaceGetSessionInfo(deps, {
|
|
sessionId: String(args.sessionId || ''),
|
|
});
|
|
return toToolResult(toolCall.id, r);
|
|
}
|
|
|
|
case 'web_search': {
|
|
const r = await executeWebSearch(deps, {
|
|
query: String(args.query || ''),
|
|
maxResults: Number(args.maxResults) || 5,
|
|
});
|
|
return toToolResult(toolCall.id, r);
|
|
}
|
|
|
|
case 'url_fetch': {
|
|
const r = await executeUrlFetch(deps, {
|
|
url: String(args.url || ''),
|
|
maxLength: Number(args.maxLength) || 50000,
|
|
});
|
|
return toToolResult(toolCall.id, r);
|
|
}
|
|
|
|
default:
|
|
return {
|
|
toolCallId: toolCall.id,
|
|
content: `Unknown tool: ${toolCall.name}`,
|
|
isError: true,
|
|
};
|
|
}
|
|
} catch (err) {
|
|
return {
|
|
toolCallId: toolCall.id,
|
|
content: `Tool execution error: ${err instanceof Error ? err.message : String(err)}`,
|
|
isError: true,
|
|
};
|
|
}
|
|
};
|
|
}
|