import type { Tool, ToolAvailability, ToolCallContext } from '../types.js'; import type { ToolGate } from '../toolGate.js'; import { resolveOutputContract, type OutputContract } from './outputContract.js'; export { WEB_OUTPUT, resolveOutputContract, type OutputContract } from './outputContract.js'; import { isMemoryToolName } from '../tools/capabilities.js'; import type { ComputerAutomationIntent } from '../tools/computeruse/computerIntent.js'; import { normalizeBrowserRuntime, type BrowserRuntimeContext } from './browserRuntimeContext.js'; export type { BrowserRuntimeContext } from './browserRuntimeContext.js'; export type AgentSurface = | 'desktop' | 'tui' | 'cli' | 'headless' | 'workflow' | 'subagent'; export type AgentProfile = 'main' | 'subagent-explore' | 'subagent-general'; export type AgentOwner = 'local-owner' | 'untrusted'; export type ConversationMode = 'private' | 'group'; export type MemoryAccess = 'read-write' | 'read-only' | 'denied'; export type MemoryAccessInput = MemoryAccess | 'local-owner'; /** * Host-issued trust context for one agent run. Nothing in this object may be * inferred from model output, project files, tools, or web content. */ export interface AgentRuntimeContext { readonly surface: AgentSurface; readonly output?: OutputContract; /** Concrete frontend identity; surface remains the execution capability class. */ readonly host?: 'desktop' | 'tui' | 'cli' | 'stream-json' | 'workflow'; readonly profile: AgentProfile; /** Host-issued per-turn audience. It is never inferred from message text. */ readonly conversationMode?: ConversationMode; readonly cwd: string; readonly workspaceId?: string; readonly sessionId?: string; readonly owner: AgentOwner; readonly memoryAccess: MemoryAccess; readonly interactiveAsk: boolean; readonly browser?: BrowserRuntimeContext; readonly computer?: ToolCallContext['computerRun']; /** Host-derived user intent; helper availability alone never exposes ComputerUse. */ readonly computerIntent?: ComputerAutomationIntent; readonly computerIntervention?: ToolCallContext['computerIntervention']; readonly toolGate?: ToolGate; } export interface AgentRuntimeContextInput extends Omit< AgentRuntimeContext, 'owner' | 'memoryAccess' | 'conversationMode' | 'browser' > { owner?: AgentOwner; memoryAccess?: MemoryAccessInput; conversationMode?: ConversationMode; host?: AgentRuntimeContext['host']; browser?: ToolCallContext['browserRun']; } export function normalizeMemoryAccess( access: MemoryAccessInput | undefined, ): MemoryAccess { return access === 'local-owner' ? 'read-write' : (access ?? 'denied'); } /** Only a local owner can receive memory access; untrusted hosts are always denied. */ export function createAgentRuntimeContext( input: AgentRuntimeContextInput, ): Readonly { const owner = input.owner ?? 'untrusted'; const memoryAccess = owner === 'local-owner' ? normalizeMemoryAccess(input.memoryAccess) : 'denied'; const { browser: rawBrowser, ...rest } = input; return Object.freeze({ ...rest, output: resolveOutputContract(input.surface, input.output), ...(rawBrowser ? { browser: normalizeBrowserRuntime(rawBrowser), } : {}), conversationMode: input.conversationMode ?? 'private', owner, memoryAccess, }); } export function filterToolsForRuntime( tools: readonly Tool[], runtime: Pick< AgentRuntimeContext, | 'surface' | 'profile' | 'conversationMode' | 'cwd' | 'memoryAccess' | 'browser' | 'computer' | 'computerIntent' >, resolvedAvailability?: ReadonlyMap, ): Tool[] { return tools.filter( (tool) => (resolvedAvailability?.get(tool)?.available ?? tool.resolveAvailability?.(runtime as AgentRuntimeContext).available ?? true) && memoryToolAllowed(tool.name, runtime.memoryAccess) && (tool.availableInModes === undefined || tool.availableInModes.includes( runtime.conversationMode ?? 'private', )) && (tool.availableOnSurfaces === undefined || tool.availableOnSurfaces.includes(runtime.surface)) && (!BROWSER_TOOL_NAMES.has(tool.name) || webBrowserAllowed(runtime)) && (tool.name !== 'ComputerUse' || (runtime.computer !== undefined && runtime.computerIntent?.needed === true)), ); } /** All browser automation entry points share a host-issued local browser boundary. */ export const BROWSER_TOOL_NAMES = new Set([ 'WebBrowser', 'BrowserBatch', 'BrowserCDP', ]); /** Browser automation is main-agent only; a visible Desktop viewer is optional. */ export function webBrowserAllowed( runtime: Pick, ): boolean { return ( runtime.profile === 'main' && runtime.browser !== undefined && ['desktop', 'tui', 'cli', 'headless', 'workflow'].includes(runtime.surface) ); } const READ_ONLY_MEMORY_TOOLS = new Set([ 'RecallMemory', 'ListMemories', 'ReviewMemories', 'SearchConversations', 'MemoryMaintenance', ]); export function memoryToolAllowed(name: string, access: MemoryAccess): boolean { if (!isMemoryToolName(name)) return true; if (access === 'read-write') return true; return access === 'read-only' && READ_ONLY_MEMORY_TOOLS.has(name); } export function isMemoryTool(name: string): boolean { return isMemoryToolName(name); } export function memoryContextFromRuntime(runtime: AgentRuntimeContext): { access: MemoryAccess; workspaceId?: string; sessionId?: string; } { return { access: runtime.memoryAccess, ...(runtime.workspaceId ? { workspaceId: runtime.workspaceId } : {}), ...(runtime.sessionId ? { sessionId: runtime.sessionId } : {}), }; }