Code Mode for Agent Automation
Relevant source files
The following files were used as context for generating this wiki page:
Code Mode executes TypeScript toolchains to automate multimodal source investigation. It enables agents to run arbitrary TypeScript code or specific script files that interact with Scoutline's internal capabilities, such as web searching or repository exploration.
The system uses a dedicated client to manage execution, handle timeouts, and provide type-safe interfaces. It provides a standardized command-line interface for executing code snippets, running script files, and retrieving metadata required for agent prompting.
Architecture and Client Implementation
The ZaiCodeModeClient acts as the primary interface for executing TypeScript toolchains. It encapsulates the environment configuration and manages the lifecycle of code execution.
Key Components
- ZaiCodeModeClient: Manages the execution environment and communication with the toolchain engine.
- Code Mode Commands: CLI-level functions that bridge user input to the client logic.
- CommandContext: Provides the execution environment, including input/output streams and credentials.
Sources: packages/scoutline/src/commands/code.ts:18-20, packages/scoutline/src/lib/code-mode.ts
Execution Flow
The following sequence diagram illustrates the execution of a TypeScript snippet via the eval command.
The evalCode function initializes a new client with the provided environment variables, calls the toolchain, and ensures the client closes even if errors occur.
Sources: packages/scoutline/src/commands/code.ts:41-55
Command Surface
Code Mode provides several subcommands to facilitate script execution and agent integration. These commands return a CommandResult containing data or error information.
CLI Usage Table
| Command | Argument | Description |
|---|---|---|
run | <file> | Executes a specific TypeScript file. |
eval | <code> | Executes a string containing TypeScript code. |
interfaces | N/A | Prints available type definitions for tool integration. |
prompt | N/A | Returns a template for guiding agents in Code Mode usage. |
Sources: packages/scoutline/src/commands/code.ts:69-80
Configuration Options
Execution behavior is modified through the CodeRunOptions interface:
- timeout: Sets the maximum execution time in milliseconds (default: 30000).
- includeLogs: Boolean flag to include console logs in the output data.
- env: Forwards resolved environment variables (including API keys) to the client.
Sources: packages/scoutline/src/commands/code.ts:22-31
Toolchain Integration
Code Mode allows scripts to access Scoutline's core providers. For instance, scripts can perform web searches through integrated providers like Z.ai.
Code Snippet: Inline Evaluation
// Example from help text
const r = await scoutline.zai.search.webSearchPrime({
search_query: 'ZAI'
});
return r;
This snippet demonstrates the use of webSearchPrime within the zai namespace, allowing programmatic research.
Sources: packages/scoutline/src/commands/code.ts:83
Metadata and Discovery
To assist agents in generating valid code, Code Mode provides mechanisms to discover available interfaces and prompt templates.
Interface Retrieval
The printInterfaces function retrieves all type definitions known to the ZaiCodeModeClient. This allows agents to understand the structure of the scoutline global object and its available methods.
Sources: packages/scoutline/src/commands/code.ts:57-67
Prompting
The printPromptTemplate function returns a static template from the client class. This template defines how an agent should structure its thoughts and code when interacting with the system.
Sources: packages/scoutline/src/commands/code.ts:69-71
Conclusion
Code Mode provides a powerful TypeScript-based automation layer for Scoutline. By exposing internal toolchains to programmatic execution, it enables complex research workflows that transcend single CLI command invocations. Its design prioritizes agent compatibility through comprehensive interface discovery and standardized prompt templates.