mirror of
https://github.com/getpaseo/paseo.git
synced 2026-07-29 12:01:31 +00:00
feat(cli): add agent send command
Implements the `agent send` command for sending messages to existing agents. Features: - Send follow-up tasks to running agents - Support for ID prefix matching (e.g., "a1b2" matches "a1b2c3d4") - --no-wait flag to return immediately without waiting for completion - Default behavior waits for agent to become idle Usage: paseo agent send <id> <prompt> paseo agent send --no-wait a1b2c3d "Run the linter"
This commit is contained in:
@@ -2,6 +2,7 @@ import { Command } from 'commander'
|
||||
import { runPsCommand } from './ps.js'
|
||||
import { runRunCommand } from './run.js'
|
||||
import { runSendCommand } from './send.js'
|
||||
import { runStopCommand } from './stop.js'
|
||||
import { withOutput } from '../../output/index.js'
|
||||
|
||||
export function createAgentCommand(): Command {
|
||||
@@ -37,5 +38,14 @@ export function createAgentCommand(): Command {
|
||||
.option('--host <host>', 'Daemon host:port (default: localhost:6767)')
|
||||
.action(withOutput(runSendCommand))
|
||||
|
||||
agent
|
||||
.command('stop')
|
||||
.description('Stop an agent (cancel if running, then terminate)')
|
||||
.argument('[id]', 'Agent ID (or prefix) - optional if --all or --cwd specified')
|
||||
.option('--all', 'Stop all agents')
|
||||
.option('--cwd <path>', 'Stop all agents in directory')
|
||||
.option('--host <host>', 'Daemon host:port (default: localhost:6767)')
|
||||
.action(withOutput(runStopCommand))
|
||||
|
||||
return agent
|
||||
}
|
||||
|
||||
164
packages/cli/src/commands/agent/send.ts
Normal file
164
packages/cli/src/commands/agent/send.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
import type { Command } from 'commander'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
|
||||
/** Minimal agent snapshot type (from daemon client) */
|
||||
interface AgentSnapshot {
|
||||
id: string
|
||||
provider: string
|
||||
cwd: string
|
||||
createdAt: string
|
||||
status: string
|
||||
title: string | null
|
||||
}
|
||||
|
||||
/** Result type for agent send command */
|
||||
export interface AgentSendResult {
|
||||
agentId: string
|
||||
status: 'sent' | 'completed'
|
||||
message: string
|
||||
}
|
||||
|
||||
/** Schema for agent send output */
|
||||
export const agentSendSchema: OutputSchema<AgentSendResult> = {
|
||||
idField: 'agentId',
|
||||
columns: [
|
||||
{ header: 'AGENT ID', field: 'agentId', width: 12 },
|
||||
{ header: 'STATUS', field: 'status', width: 12 },
|
||||
{ header: 'MESSAGE', field: 'message', width: 40 },
|
||||
],
|
||||
}
|
||||
|
||||
export interface AgentSendOptions extends CommandOptions {
|
||||
noWait?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve agent ID from prefix or full ID.
|
||||
* Supports exact match and prefix matching.
|
||||
*/
|
||||
function resolveAgentId(agents: AgentSnapshot[], idOrPrefix: string): string | null {
|
||||
// Exact match first
|
||||
const exact = agents.find((a) => a.id === idOrPrefix)
|
||||
if (exact) return exact.id
|
||||
|
||||
// Prefix match
|
||||
const matches = agents.filter((a) => a.id.startsWith(idOrPrefix))
|
||||
if (matches.length === 1 && matches[0]) return matches[0].id
|
||||
if (matches.length > 1) {
|
||||
throw new Error(
|
||||
`Ambiguous ID prefix '${idOrPrefix}': matches ${matches.length} agents (${matches.map((a) => a.id.slice(0, 7)).join(', ')})`
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export async function runSendCommand(
|
||||
agentIdArg: string,
|
||||
prompt: string,
|
||||
options: AgentSendOptions,
|
||||
_command: Command
|
||||
): Promise<SingleResult<AgentSendResult>> {
|
||||
const host = getDaemonHost({ host: options.host as string | undefined })
|
||||
|
||||
// Validate arguments
|
||||
if (!agentIdArg || agentIdArg.trim().length === 0) {
|
||||
const error: CommandError = {
|
||||
code: 'MISSING_AGENT_ID',
|
||||
message: 'Agent ID is required',
|
||||
details: 'Usage: paseo agent send [options] <id> <prompt>',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
if (!prompt || prompt.trim().length === 0) {
|
||||
const error: CommandError = {
|
||||
code: 'MISSING_PROMPT',
|
||||
message: 'A prompt is required',
|
||||
details: 'Usage: paseo agent send [options] <id> <prompt>',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
let client
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host as string | undefined })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
try {
|
||||
// Request session state to get agent information
|
||||
client.requestSessionState()
|
||||
|
||||
// Wait a moment for the session state to be populated
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
|
||||
const agents = client.listAgents()
|
||||
|
||||
// Resolve agent ID (supports prefix matching)
|
||||
const agentId = resolveAgentId(agents, agentIdArg)
|
||||
if (!agentId) {
|
||||
const error: CommandError = {
|
||||
code: 'AGENT_NOT_FOUND',
|
||||
message: `Agent not found: ${agentIdArg}`,
|
||||
details: 'Use "paseo agent ps" to list available agents',
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
// Send the message
|
||||
await client.sendAgentMessage(agentId, prompt)
|
||||
|
||||
// If --no-wait, return immediately
|
||||
if (options.noWait) {
|
||||
await client.close()
|
||||
|
||||
return {
|
||||
type: 'single',
|
||||
data: {
|
||||
agentId,
|
||||
status: 'sent',
|
||||
message: 'Message sent, not waiting for completion',
|
||||
},
|
||||
schema: agentSendSchema,
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for agent to become idle
|
||||
await client.waitForAgentIdle(agentId, 600000) // 10 minute timeout
|
||||
|
||||
await client.close()
|
||||
|
||||
return {
|
||||
type: 'single',
|
||||
data: {
|
||||
agentId,
|
||||
status: 'completed',
|
||||
message: 'Agent completed processing the message',
|
||||
},
|
||||
schema: agentSendSchema,
|
||||
}
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const error: CommandError = {
|
||||
code: 'SEND_FAILED',
|
||||
message: `Failed to send message: ${message}`,
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
168
packages/cli/tests/06-agent-send.test.mts
Normal file
168
packages/cli/tests/06-agent-send.test.mts
Normal file
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env npx tsx
|
||||
|
||||
/**
|
||||
* Phase 5: Agent Send Command Tests
|
||||
*
|
||||
* Tests the agent send command - sending messages to existing agents.
|
||||
* Since daemon may not be running, we test both:
|
||||
* - Help and argument parsing
|
||||
* - Graceful error handling when daemon not running
|
||||
* - All flags are accepted
|
||||
*
|
||||
* Tests:
|
||||
* - agent send --help shows options
|
||||
* - agent send requires id and prompt arguments
|
||||
* - agent send handles daemon not running
|
||||
* - agent send --no-wait flag is accepted
|
||||
* - agent shows send in subcommands
|
||||
*/
|
||||
|
||||
import assert from 'node:assert'
|
||||
import { $ } from 'zx'
|
||||
import { mkdtemp, rm } from 'fs/promises'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
|
||||
$.verbose = false
|
||||
|
||||
console.log('=== Agent Send Command Tests ===\n')
|
||||
|
||||
// Get random port that's definitely not in use (never 6767)
|
||||
const port = 10000 + Math.floor(Math.random() * 50000)
|
||||
const paseoHome = await mkdtemp(join(tmpdir(), 'paseo-test-home-'))
|
||||
|
||||
try {
|
||||
// Test 1: agent send --help shows options
|
||||
{
|
||||
console.log('Test 1: agent send --help shows options')
|
||||
const result = await $`npx paseo agent send --help`.nothrow()
|
||||
assert.strictEqual(result.exitCode, 0, 'agent send --help should exit 0')
|
||||
assert(result.stdout.includes('--no-wait'), 'help should mention --no-wait flag')
|
||||
assert(result.stdout.includes('--host'), 'help should mention --host option')
|
||||
assert(result.stdout.includes('<id>'), 'help should mention id argument')
|
||||
assert(result.stdout.includes('<prompt>'), 'help should mention prompt argument')
|
||||
console.log(' help should mention --no-wait flag')
|
||||
console.log(' help should mention --host option')
|
||||
console.log(' help should mention <id> argument')
|
||||
console.log(' help should mention <prompt> argument')
|
||||
console.log('✓ agent send --help shows options\n')
|
||||
}
|
||||
|
||||
// Test 2: agent send requires id argument
|
||||
{
|
||||
console.log('Test 2: agent send requires id argument')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo agent send`.nothrow()
|
||||
assert.notStrictEqual(result.exitCode, 0, 'should fail without id')
|
||||
const output = result.stdout + result.stderr
|
||||
// Commander should complain about missing argument
|
||||
const hasMissingArg =
|
||||
output.toLowerCase().includes('missing') ||
|
||||
output.toLowerCase().includes('required') ||
|
||||
output.toLowerCase().includes('argument')
|
||||
assert(hasMissingArg, 'error should mention missing argument')
|
||||
console.log('✓ agent send requires id argument\n')
|
||||
}
|
||||
|
||||
// Test 3: agent send requires prompt argument
|
||||
{
|
||||
console.log('Test 3: agent send requires prompt argument')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo agent send abc123`.nothrow()
|
||||
assert.notStrictEqual(result.exitCode, 0, 'should fail without prompt')
|
||||
const output = result.stdout + result.stderr
|
||||
// Commander should complain about missing argument
|
||||
const hasMissingArg =
|
||||
output.toLowerCase().includes('missing') ||
|
||||
output.toLowerCase().includes('required') ||
|
||||
output.toLowerCase().includes('argument')
|
||||
assert(hasMissingArg, 'error should mention missing argument')
|
||||
console.log('✓ agent send requires prompt argument\n')
|
||||
}
|
||||
|
||||
// Test 4: agent send handles daemon not running
|
||||
{
|
||||
console.log('Test 4: agent send handles daemon not running')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo agent send abc123 "test prompt"`.nothrow()
|
||||
// Should fail because daemon not running
|
||||
assert.notStrictEqual(result.exitCode, 0, 'should fail when daemon not running')
|
||||
const output = result.stdout + result.stderr
|
||||
const hasError =
|
||||
output.toLowerCase().includes('daemon') ||
|
||||
output.toLowerCase().includes('connect') ||
|
||||
output.toLowerCase().includes('cannot')
|
||||
assert(hasError, 'error message should mention connection issue')
|
||||
console.log('✓ agent send handles daemon not running\n')
|
||||
}
|
||||
|
||||
// Test 5: agent send --no-wait flag is accepted
|
||||
{
|
||||
console.log('Test 5: agent send --no-wait flag is accepted')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo agent send --no-wait abc123 "test prompt"`.nothrow()
|
||||
const output = result.stdout + result.stderr
|
||||
assert(!output.includes('unknown option'), 'should accept --no-wait flag')
|
||||
assert(!output.includes('error: option'), 'should not have option parsing error')
|
||||
console.log('✓ agent send --no-wait flag is accepted\n')
|
||||
}
|
||||
|
||||
// Test 6: agent send --host flag is accepted
|
||||
{
|
||||
console.log('Test 6: agent send --host flag is accepted')
|
||||
const result =
|
||||
await $`PASEO_HOME=${paseoHome} npx paseo agent send --host localhost:${port} abc123 "test prompt"`.nothrow()
|
||||
const output = result.stdout + result.stderr
|
||||
assert(!output.includes('unknown option'), 'should accept --host flag')
|
||||
assert(!output.includes('error: option'), 'should not have option parsing error')
|
||||
console.log('✓ agent send --host flag is accepted\n')
|
||||
}
|
||||
|
||||
// Test 7: -q (quiet) flag is accepted with agent send
|
||||
{
|
||||
console.log('Test 7: -q (quiet) flag is accepted with agent send')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo -q agent send --no-wait abc123 "test prompt"`.nothrow()
|
||||
const output = result.stdout + result.stderr
|
||||
assert(!output.includes('unknown option'), 'should accept -q flag')
|
||||
assert(!output.includes('error: option'), 'should not have option parsing error')
|
||||
console.log('✓ -q (quiet) flag is accepted with agent send\n')
|
||||
}
|
||||
|
||||
// Test 8: Combined flags work together
|
||||
{
|
||||
console.log('Test 8: Combined flags work together')
|
||||
const result =
|
||||
await $`PASEO_HOST=localhost:${port} PASEO_HOME=${paseoHome} npx paseo -q agent send --no-wait abc123 "Run the linter"`.nothrow()
|
||||
const output = result.stdout + result.stderr
|
||||
assert(!output.includes('unknown option'), 'should accept all combined flags')
|
||||
assert(!output.includes('error: option'), 'should not have option parsing error')
|
||||
console.log('✓ Combined flags work together\n')
|
||||
}
|
||||
|
||||
// Test 9: agent --help shows send subcommand
|
||||
{
|
||||
console.log('Test 9: agent --help shows send subcommand')
|
||||
const result = await $`npx paseo agent --help`.nothrow()
|
||||
assert.strictEqual(result.exitCode, 0, 'agent --help should exit 0')
|
||||
assert(result.stdout.includes('send'), 'help should mention send subcommand')
|
||||
console.log('✓ agent --help shows send subcommand\n')
|
||||
}
|
||||
|
||||
// Test 10: ID prefix syntax is mentioned in help
|
||||
{
|
||||
console.log('Test 10: send command description mentions ID')
|
||||
const result = await $`npx paseo agent send --help`.nothrow()
|
||||
assert.strictEqual(result.exitCode, 0, 'agent send --help should exit 0')
|
||||
const hasIdMention =
|
||||
result.stdout.toLowerCase().includes('id') ||
|
||||
result.stdout.toLowerCase().includes('prefix')
|
||||
assert(hasIdMention, 'help should mention ID or prefix')
|
||||
console.log('✓ send command description mentions ID\n')
|
||||
}
|
||||
} finally {
|
||||
// Clean up temp directory
|
||||
await rm(paseoHome, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
console.log('=== All agent send tests passed ===')
|
||||
Reference in New Issue
Block a user