AI Agent
TL;DR
The AI agent service runs tasks on remote machines with structured, Zod-typed output. Define a Zod schema and the agent returns fully typed results. Supports streaming with progress events, three agent modes (auto, terminal-only, readonly), and complex nested schemas. Use run() for one-shot tasks or stream() for real-time progress updates with cancellation support.
Run AI tasks on remote machines with structured, typed output via Zod schemas.
How do I set up the agent service?
import { CMDOPClient } from '@cmdop/node';
import { z } from 'zod';
// Connect and select the target machine for agent tasks
const client = await CMDOPClient.remote({ apiKey: 'cmdop_xxx' });
await client.agent.setMachine('my-server');How do I run an agent with a Zod schema?
// Define a Zod schema for the expected structured output
const ServerHealth = z.object({
hostname: z.string(),
cpuPercent: z.number(),
memoryPercent: z.number(),
issues: z.array(z.string()),
});
// Run the agent -- it executes the prompt and returns data matching the schema
const result = await client.agent.run({
prompt: 'Check server health and report issues',
outputSchema: ServerHealth,
});
// result.data is fully typed as { hostname: string; cpuPercent: number; ... }
console.log(result.data.cpuPercent);How do I run an agent without a schema?
// Run without a schema -- returns unstructured text output
const result = await client.agent.run({
prompt: 'Describe the current server state',
});
console.log(result.text);How do I stream agent progress?
// Start a streaming agent task with real-time progress events
const stream = client.agent.stream({
prompt: 'Analyze disk usage and suggest cleanup',
outputSchema: DiskReport,
});
// Listen for progress events as the agent works
stream.onProgress((event) => {
console.log(event.type, event.message);
});
// Await the final structured result when the agent completes
const result = await stream.completed;
console.log(result.data);What agent modes are available?
// Auto mode (default) -- agent decides which tools to use
const result = await client.agent.run({
prompt: 'Check server health',
mode: 'auto',
});
// Terminal mode -- agent can only execute shell commands
const result = await client.agent.run({
prompt: 'Find large files',
mode: 'terminal',
});
// Readonly mode -- agent can read files/state but cannot modify anything
const result = await client.agent.run({
prompt: 'Audit the configuration',
mode: 'readonly',
});How do I use complex nested schemas?
// Define a deeply nested schema with enums, optionals, and descriptions
const DeployReport = z.object({
success: z.boolean(),
version: z.string(),
services: z.array(z.object({
name: z.string(),
status: z.enum(['running', 'stopped', 'error']),
port: z.number().optional(), // Port is optional for non-networked services
})),
errors: z.array(z.string()),
duration: z.number().describe('Duration in seconds'), // .describe() adds context for the AI
});
// The agent interprets the schema and returns matching structured data
const result = await client.agent.run({
prompt: 'Deploy the latest version and report status',
outputSchema: DeployReport,
});What parameters does run() accept?
run(options)
| Parameter | Type | Description |
|---|---|---|
options.prompt | string | Task description for the agent |
options.outputSchema | ZodType | Zod schema for typed output |
options.mode | 'auto' | 'terminal' | 'readonly' | Agent mode (default: 'auto') |
options.timeout | number | Timeout in milliseconds |
stream(options)
Same parameters as run(). Returns a stream object with:
| Method | Description |
|---|---|
onProgress(cb) | Progress events |
completed | Promise that resolves to the final result |
cancel() | Cancel the running agent |
Last updated on