mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-24 00:50:44 +00:00
* feat: add deviceType field to Host model for network device support Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: pass deviceType through session metadata pipeline Thread deviceType from Host model through AITerminalSessionInfo, IPC types, and mcpServerBridge so AI agents can inspect device type per session. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat: route network device SSH sessions to raw PTY execution When deviceType === 'network', handleExec now uses execViaRawPty instead of execViaPty so vendor CLIs (Huawei VRP, Cisco IOS, etc.) receive commands as-is without POSIX shell wrapping or markers. The command blocklist is also skipped for network devices, consistent with the existing serial session bypass. AI context description updated to document the raw-execution behaviour for network device sessions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat: add network device mode toggle to host settings UI Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add network device awareness to Catty Agent system prompt Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: extend network device mode to Catty Agent exec path and host context - Add network device detection and raw execution routing to aiBridge.cjs (the primary Catty Agent command path), not just the MCP bridge - Export getSessionMeta from mcpServerBridge for reuse in aiBridge - Surface deviceType in Catty Agent system prompt host list so the AI can identify which sessions are network devices - Pass deviceType through buildSystemPrompt context Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: exempt network device sessions from client-side blocklist and update ACP context - Add deviceType to ExecutorContext sessions type - Skip renderer-side command blocklist for deviceType=network sessions in shared toolExecutors.ts (not just main-process side) - Update ACP agent context hint to mention network device sessions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: only show network device mode toggle for SSH hosts Telnet and local hosts don't support the network device execution path, so hiding the toggle prevents users from enabling a broken configuration. Serial hosts already use raw mode by default. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: exclude Mosh sessions from network device raw execution path Mosh uses a shell-backed PTY and cannot connect to vendor CLIs, so network device mode should only apply to SSH and serial sessions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: prefer session.protocol over metadata for Mosh detection Mosh tabs report protocol:"ssh" in renderer metadata but "mosh" in the main-process session object. Prioritize session.protocol (runtime truth) to correctly exclude Mosh from network device raw execution. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: suppress deviceType metadata for Mosh sessions Mosh requires a shell-backed PTY and cannot connect to vendor CLIs, so omit deviceType from AI-facing metadata when session is Mosh-backed. This prevents the AI from being told to use vendor CLI syntax when the actual execution path uses normal shell wrapping. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use exit code 0 for network device sessions and hide toggle for Mosh - Network device / serial sessions return exitCode: null from vendor CLIs. Default to 0 instead of -1 so the AI doesn't misinterpret successful commands as failures. - Hide the network device mode toggle when Mosh is enabled, since the setting is suppressed at runtime for Mosh sessions anyway. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: preserve null exit codes and restrict raw mode to SSH/serial - Preserve exitCode: null for network device sessions instead of coercing to 0, so the AI knows exit status is unavailable rather than seeing a misleading success code. - Explicitly whitelist SSH/serial protocols for network device mode instead of just excluding mosh, preventing local/telnet sessions from accidentally entering raw execution. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use UTF-8 encoding for SSH network device raw execution execViaRawPty hardcodes latin1 for serial port data decoding. Add an encoding option (default: latin1) and pass utf8 from SSH network device call sites so multi-byte characters aren't corrupted. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use host charset for serial port decoding instead of hardcoded latin1 - Extract charsetToNodeEncoding() to module scope in terminalBridge - Serial sessions now read options.charset (from Host.charset) for both terminal display decoding and AI command output - Store serialEncoding on session object so exec paths can use it - Pass encoding through all execViaRawPty call sites - Default encoding changed from latin1 to utf8 (matches most modern network equipment and is the safer default for CJK environments) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move serialEncoding declaration before session object creation serialEncoding was referenced in the session object literal before its const declaration, causing a TDZ ReferenceError that would crash every serial connection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: tighten isNetworkDevice logic and clean up edge cases - Align toolExecutors isNetworkDevice check with bridge logic: require explicit SSH/serial protocol match instead of trusting deviceType alone - Remove empty-string protocol match from isSshOrSerial in both bridges to prevent local/unknown sessions from being treated as network devices - Widen exitCode return type to `number | null` to match actual behavior - Clear deviceType when enabling Mosh (incompatible combination) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update MCP server tool descriptions for network device sessions The get_environment and terminal_execute tool descriptions only mentioned serial/raw sessions for network devices. Updated to also reference deviceType: network SSH sessions so external AI agents (Claude, Codex) know about the new execution mode. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: include deviceType in get_environment and guard execViaChannel fallback - Add deviceType to executeWorkspaceGetInfo session mapping and return type so Catty Agent's get_environment tool matches MCP bridge output - Guard both aiBridge and mcpServerBridge against falling through to execViaChannel for network device sessions — network devices require an interactive PTY and exec channels would produce broken behavior Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add charset setting to serial host configuration UI Serial hosts now have a charset input in the Advanced section, defaulting to UTF-8. The value is saved to Host.charset and used by the serial decoder in terminalBridge. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add charset to serial quick-connect modal with full pipeline - Add charset input to SerialConnectModal (Advanced section) - Thread charset through onConnect callback → handleConnectSerial → createSerialSession → TerminalSession.charset - Add charset field to TerminalSession interface - Include charset in fallback host builder for quick-connect sessions so createTerminalSessionStarters can pass it to startSerialSession - Saved hosts also store charset via onSaveHost Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: constrain serial connect modal height with scrollable content Modal content could overflow the viewport when Advanced section was expanded. Add max-h-[85vh] to DialogContent with flex layout so the content area scrolls while header and footer buttons stay visible. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: propagate charset through all serial session creation paths - Add charset to startSerialSession type in global.d.ts - Copy host.charset to TerminalSession in connectToHost serial path - Copy host.charset in createWorkspaceWithHosts serial path - Propagate session.charset in splitSession (both workspace and standalone) - Propagate session.charset in copySession Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: propagate charset in remaining session creation paths Add host.charset to connectToHost (non-serial), createWorkspaceWithHosts (non-serial), and runSnippet session creation for consistency. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
240 lines
8.8 KiB
TypeScript
240 lines
8.8 KiB
TypeScript
/**
|
|
* Shared tool execution logic used by both the Catty Agent executor (switch/case)
|
|
* and the Vercel AI SDK tool wrappers.
|
|
*
|
|
* Each function encapsulates the core business logic for a tool — validation,
|
|
* safety checks, bridge calls, and result formatting — so callers only need to
|
|
* adapt the return value to their own response shape.
|
|
*/
|
|
|
|
import type { NetcattyBridge, ExecutorContext } from '../cattyAgent/executor';
|
|
import type { AIPermissionMode, WebSearchConfig } from '../types';
|
|
import { checkCommandSafety } from '../cattyAgent/safety';
|
|
import { executeWebSearchProvider } from './webSearchProviders';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Shared result types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** Discriminated union returned by every shared executor. */
|
|
export type ToolExecResult<T = unknown> =
|
|
| { ok: true; data: T }
|
|
| { ok: false; error: string };
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Dependencies bundle
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export interface ToolDeps {
|
|
bridge: NetcattyBridge;
|
|
context: ExecutorContext | (() => ExecutorContext);
|
|
commandBlocklist?: string[];
|
|
permissionMode: AIPermissionMode;
|
|
webSearchConfig?: WebSearchConfig;
|
|
chatSessionId?: string;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function resolveContext(ctx: ToolDeps['context']): ExecutorContext {
|
|
return typeof ctx === 'function' ? ctx() : ctx;
|
|
}
|
|
|
|
function validSessionIds(ctx: ToolDeps['context']): Set<string> {
|
|
const resolved = resolveContext(ctx);
|
|
return new Set(resolved.sessions.map(s => s.sessionId));
|
|
}
|
|
|
|
function validateSessionScope(ctx: ToolDeps['context'], sessionId: string): string | null {
|
|
const ids = validSessionIds(ctx);
|
|
if (!ids.has(sessionId)) {
|
|
return `Session "${sessionId}" is not in the current scope. Available sessions: ${[...ids].join(', ')}`;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function isObserver(mode: AIPermissionMode): boolean {
|
|
return mode === 'observer';
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tool executors
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function executeTerminalExecute(
|
|
deps: ToolDeps,
|
|
args: { sessionId: string; command: string },
|
|
): Promise<ToolExecResult<{ stdout: string; stderr: string; exitCode: number | null }>> {
|
|
const { bridge, context, commandBlocklist, permissionMode } = deps;
|
|
const { sessionId, command } = args;
|
|
|
|
if (!sessionId || !command) {
|
|
return { ok: false, error: 'Missing sessionId or command' };
|
|
}
|
|
const scopeErr = validateSessionScope(context, sessionId);
|
|
if (scopeErr) return { ok: false, error: scopeErr };
|
|
if (isObserver(permissionMode)) {
|
|
return { ok: false, error: 'Observer mode: command execution is disabled. Switch to Confirm or Auto mode to execute commands.' };
|
|
}
|
|
// Shell blocklist is meaningless on network device CLIs (e.g. "shutdown"
|
|
// disables an interface on Cisco). Skip for serial and network device sessions.
|
|
// The bridge layer (handleExec / netcatty:ai:exec) also has its own session-aware check.
|
|
const resolved = resolveContext(context);
|
|
const targetSession = resolved.sessions.find(s => s.sessionId === sessionId);
|
|
const proto = targetSession?.protocol || '';
|
|
const isSshOrSerial = proto === 'ssh' || proto === 'serial';
|
|
const isNetworkDevice = proto === 'serial' || (targetSession?.deviceType === 'network' && isSshOrSerial);
|
|
if (!isNetworkDevice) {
|
|
const safety = checkCommandSafety(command, commandBlocklist);
|
|
if (safety.blocked) {
|
|
return { ok: false, error: `Command blocked by safety policy. Matched pattern: ${safety.matchedPattern}` };
|
|
}
|
|
}
|
|
|
|
const result = await bridge.aiExec(sessionId, command, deps.chatSessionId);
|
|
// Real execution failures (timeout, disconnect, no stream) have an `error` field
|
|
if (!result.ok && result.error) {
|
|
const parts = [result.error];
|
|
if (result.stdout) parts.push(`Partial output:\n${result.stdout}`);
|
|
if (result.stderr) parts.push(`Stderr:\n${result.stderr}`);
|
|
return { ok: false, error: parts.join('\n\n') };
|
|
}
|
|
// Command ran (even if exit code is non-zero) — always return stdout+exitCode for LLM to judge.
|
|
// Network device / serial sessions return exitCode: null because vendor CLIs don't expose
|
|
// exit codes. Preserve null so the model knows exit status is unavailable rather than
|
|
// seeing a misleading 0 (success) or -1 (failure).
|
|
return {
|
|
ok: true,
|
|
data: {
|
|
stdout: result.stdout || '',
|
|
stderr: result.stderr || '',
|
|
exitCode: isNetworkDevice ? (result.exitCode ?? null) : (result.exitCode ?? -1),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function executeWorkspaceGetInfo(
|
|
deps: ToolDeps,
|
|
): ToolExecResult<{
|
|
workspaceId: string | null;
|
|
workspaceName: string | null;
|
|
sessions: Array<{
|
|
sessionId: string;
|
|
hostname: string;
|
|
label: string;
|
|
os?: string;
|
|
username?: string;
|
|
protocol?: string;
|
|
shellType?: string;
|
|
deviceType?: string;
|
|
connected: boolean;
|
|
}>;
|
|
}> {
|
|
const context = resolveContext(deps.context);
|
|
return {
|
|
ok: true,
|
|
data: {
|
|
workspaceId: context.workspaceId || null,
|
|
workspaceName: context.workspaceName || null,
|
|
sessions: context.sessions.map(s => ({
|
|
sessionId: s.sessionId,
|
|
hostname: s.hostname,
|
|
label: s.label,
|
|
os: s.os,
|
|
username: s.username,
|
|
protocol: s.protocol,
|
|
shellType: s.shellType,
|
|
deviceType: s.deviceType,
|
|
connected: s.connected,
|
|
})),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function executeWorkspaceGetSessionInfo(
|
|
deps: ToolDeps,
|
|
args: { sessionId: string },
|
|
): ToolExecResult<ExecutorContext['sessions'][number]> {
|
|
const context = resolveContext(deps.context);
|
|
const session = context.sessions.find(s => s.sessionId === args.sessionId);
|
|
if (!session) {
|
|
return { ok: false, error: `Session not found: ${args.sessionId}` };
|
|
}
|
|
return { ok: true, data: session };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Web Search & URL Fetch (read-only, no permission check needed)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function executeWebSearch(
|
|
deps: ToolDeps,
|
|
args: { query: string; maxResults?: number },
|
|
): Promise<ToolExecResult<{ results: Array<{ title: string; url: string; content: string }> }>> {
|
|
const { bridge, webSearchConfig } = deps;
|
|
|
|
if (!webSearchConfig?.enabled) {
|
|
return { ok: false, error: 'Web search is not enabled. Please configure a search provider in Settings → AI.' };
|
|
}
|
|
if (!args.query) {
|
|
return { ok: false, error: 'Missing search query' };
|
|
}
|
|
|
|
try {
|
|
const maxResults = Math.max(1, Math.min(20, args.maxResults ?? webSearchConfig.maxResults ?? 5));
|
|
const results = await executeWebSearchProvider(bridge, webSearchConfig, args.query, maxResults);
|
|
// Enforce maxResults after provider normalization (some providers ignore the limit)
|
|
return { ok: true, data: { results: results.slice(0, maxResults) } };
|
|
} catch (err) {
|
|
return { ok: false, error: `Web search failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
}
|
|
}
|
|
|
|
interface BridgeFetchResponse {
|
|
ok: boolean;
|
|
status?: number;
|
|
data?: string;
|
|
error?: string;
|
|
}
|
|
|
|
export async function executeUrlFetch(
|
|
deps: ToolDeps,
|
|
args: { url: string; maxLength?: number },
|
|
): Promise<ToolExecResult<{ url: string; content: string; status: number }>> {
|
|
const { bridge } = deps;
|
|
const { url } = args;
|
|
|
|
if (!url || !url.startsWith('https://')) {
|
|
return { ok: false, error: 'Invalid URL. Must start with https://' };
|
|
}
|
|
|
|
const aiFetch = (bridge as unknown as Record<string, (...a: unknown[]) => Promise<unknown>>).aiFetch;
|
|
if (!aiFetch) {
|
|
return { ok: false, error: 'aiFetch is not available on the bridge' };
|
|
}
|
|
|
|
try {
|
|
// skipHostCheck=true, followRedirects=true: url_fetch targets user-provided URLs
|
|
const resp = await aiFetch(url, 'GET', {
|
|
'User-Agent': 'Netcatty-AI/1.0',
|
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.7',
|
|
}, undefined, undefined, true, true) as BridgeFetchResponse;
|
|
|
|
if (!resp.ok) {
|
|
return { ok: false, error: resp.error || `HTTP ${resp.status}` };
|
|
}
|
|
|
|
const maxLength = Math.max(1, Math.min(200000, args.maxLength ?? 50000));
|
|
let content = resp.data || '';
|
|
if (content.length > maxLength) {
|
|
content = content.slice(0, maxLength) + '\n\n[Content truncated]';
|
|
}
|
|
|
|
return { ok: true, data: { url, content, status: resp.status || 200 } };
|
|
} catch (err) {
|
|
return { ok: false, error: `URL fetch failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
}
|
|
}
|