refactor: replace agent_list polling with request/response RPC pattern

This commit is contained in:
Mohamed Boudra
2026-02-01 22:37:32 +07:00
parent 8971295c4d
commit bf9555829b
59 changed files with 2979 additions and 3637 deletions
+3 -23
View File
@@ -1,6 +1,5 @@
import type { Command } from 'commander'
import type { AgentSnapshotPayload } from '@paseo/server'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
/** Result type for agent archive command */
@@ -58,27 +57,7 @@ export async function runArchiveCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID (supports prefix matching)
const agentId = resolveAgentId(agentIdArg, agents)
if (!agentId) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found: ${agentIdArg}`,
details: 'Use "paseo ls" to list available agents',
}
throw error
}
// Get the agent snapshot to check status
const agent = agents.find((a: AgentSnapshotPayload) => a.id === agentId)
const agent = await client.fetchAgent(agentIdArg)
if (!agent) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
@@ -87,6 +66,7 @@ export async function runArchiveCommand(
}
throw error
}
const agentId = agent.id
// Check if agent is already archived
if (agent.archivedAt) {
+4 -18
View File
@@ -1,5 +1,5 @@
import type { Command } from 'commander'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type {
DaemonClientV2,
AgentStreamMessage,
@@ -118,28 +118,14 @@ export async function runAttachCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait for agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
const resolvedId = resolveAgentId(id, agents)
if (!resolvedId) {
const agent = await client.fetchAgent(id)
if (!agent) {
console.error(`Error: No agent found matching: ${id}`)
console.error('Use `paseo ls` to list available agents')
await client.close()
process.exit(1)
}
const agent = agents.find((a) => a.id === resolvedId)
if (!agent) {
console.error(`Error: Agent not found: ${resolvedId}`)
await client.close()
process.exit(1)
}
const resolvedId = agent.id
// Print header
console.log(`Attaching to agent ${resolvedId.substring(0, 7)}...`)
+2 -22
View File
@@ -1,6 +1,6 @@
import type { Command } from 'commander'
import type { AgentSnapshotPayload } from '@paseo/server'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, ListResult, OutputSchema, CommandError } from '../../output/index.js'
/** Agent inspect data for display (matches CLI spec format) */
@@ -217,27 +217,7 @@ export async function runInspectCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID (supports prefix matching)
const agentId = resolveAgentId(agentIdArg, agents)
if (!agentId) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found: ${agentIdArg}`,
details: 'Use "paseo ls" to list available agents',
}
throw error
}
// Get the full agent snapshot
const snapshot = agents.find((a) => a.id === agentId)
const snapshot = await client.fetchAgent(agentIdArg)
if (!snapshot) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
+4 -35
View File
@@ -1,5 +1,5 @@
import type { Command } from 'commander'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions } from '../../output/index.js'
import type {
DaemonClientV2,
@@ -67,16 +67,6 @@ function extractTimelineFromSnapshot(message: AgentStreamSnapshotMessage): Agent
return items
}
/**
* Extract a timeline item from an agent_stream message
*/
function extractTimelineFromStream(message: AgentStreamMessage): AgentTimelineItem | null {
if (message.payload.event.type === 'timeline') {
return message.payload.event.item
}
return null
}
export async function runLogsCommand(
id: string,
options: AgentLogsOptions,
@@ -101,21 +91,14 @@ export async function runLogsCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait for agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
const resolvedId = resolveAgentId(id, agents)
if (!resolvedId) {
const agent = await client.fetchAgent(id)
if (!agent) {
console.error(`Error: No agent found matching: ${id}`)
console.error('Use `paseo ls` to list available agents')
await client.close()
process.exit(1)
}
const resolvedId = agent.id
// For follow mode, we stream events continuously
if (options.follow) {
@@ -155,20 +138,6 @@ export async function runLogsCommand(
// Get timeline from snapshot
let timelineItems = await snapshotPromise
// Also check message queue for any stream events
const queue = client.getMessageQueue()
for (const msg of queue) {
if (msg.type === 'agent_stream') {
const streamMsg = msg as AgentStreamMessage
if (streamMsg.payload.agentId === resolvedId) {
const item = extractTimelineFromStream(streamMsg)
if (item) {
timelineItems.push(item)
}
}
}
}
// Apply filter
if (options.filter) {
timelineItems = timelineItems.filter((item) => matchesFilter(item, options.filter))
+4 -8
View File
@@ -115,10 +115,7 @@ export async function runLsCommand(
}
try {
// Request and wait for agent list
await client.waitForAgentList()
let agents = client.listAgents()
let agents = await client.fetchAgents()
// Status filtering:
// By default, only show running/idle agents (not error, archived, etc.)
@@ -173,9 +170,9 @@ export async function runLsCommand(
if (Object.keys(labelFilters).length > 0) {
// Filter to agents that have ALL specified labels (AND semantics)
agents = agents.filter((a) => {
const agentLabels = (a as any).labels as Record<string, string> | undefined
const agentLabels = a.labels
for (const [key, value] of Object.entries(labelFilters)) {
if (!agentLabels || agentLabels[key] !== value) {
if (agentLabels[key] !== value) {
return false
}
}
@@ -184,8 +181,7 @@ export async function runLsCommand(
} else {
// Default: show background agents only (those without ui=true)
agents = agents.filter((a) => {
const agentLabels = (a as any).labels as Record<string, string> | undefined
return !agentLabels || agentLabels['ui'] !== 'true'
return a.labels['ui'] !== 'true'
})
}
+4 -21
View File
@@ -1,5 +1,5 @@
import type { Command } from 'commander'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type {
CommandOptions,
OutputSchema,
@@ -72,17 +72,8 @@ export async function runModeCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID
const resolvedId = resolveAgentId(id, agents)
if (!resolvedId) {
const agent = await client.fetchAgent(id)
if (!agent) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `No agent found matching: ${id}`,
@@ -90,15 +81,7 @@ export async function runModeCommand(
}
throw error
}
const agent = agents.find((a) => a.id === resolvedId)
if (!agent) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found after resolution: ${resolvedId}`,
}
throw error
}
const resolvedId = agent.id
if (options.list) {
// List available modes for this agent
+42 -47
View File
@@ -1,5 +1,4 @@
import type { Command } from 'commander'
import type { AgentSnapshotPayload } from '@paseo/server'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
import { readFile } from 'node:fs/promises'
@@ -8,7 +7,7 @@ import { extname } from 'node:path'
/** Result type for agent send command */
export interface AgentSendResult {
agentId: string
status: 'sent' | 'completed'
status: 'sent' | 'completed' | 'timeout' | 'permission' | 'error'
message: string
}
@@ -27,27 +26,6 @@ export interface AgentSendOptions extends CommandOptions {
image?: string[]
}
/**
* Resolve agent ID from prefix or full ID.
* Supports exact match and prefix matching.
*/
function resolveAgentId(agents: AgentSnapshotPayload[], 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
}
/**
* Read image files and convert them to base64 data URIs
*/
@@ -140,32 +118,13 @@ export async function runSendCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list 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 ls" to list available agents',
}
throw error
}
// Read image files if provided
const images = options.image && options.image.length > 0
? await readImageFiles(options.image)
: undefined
// Send the message
await client.sendAgentMessage(agentId, prompt, { images })
await client.sendAgentMessage(agentIdArg, prompt, { images })
// If --no-wait, return immediately
if (options.noWait) {
@@ -174,7 +133,7 @@ export async function runSendCommand(
return {
type: 'single',
data: {
agentId,
agentId: agentIdArg,
status: 'sent',
message: 'Message sent, not waiting for completion',
},
@@ -183,16 +142,52 @@ export async function runSendCommand(
}
// Wait for agent to finish
const state = await client.waitForFinish(agentId, 600000) // 10 minute timeout
const state = await client.waitForFinish(agentIdArg, 600000) // 10 minute timeout
await client.close()
if (state.status === 'timeout') {
return {
type: 'single',
data: {
agentId: state.final?.id ?? agentIdArg,
status: 'timeout',
message: 'Timed out waiting for agent to finish',
},
schema: agentSendSchema,
}
}
if (state.status === 'permission') {
return {
type: 'single',
data: {
agentId: state.final?.id ?? agentIdArg,
status: 'permission',
message: 'Agent is waiting for permission',
},
schema: agentSendSchema,
}
}
if (state.status === 'error') {
return {
type: 'single',
data: {
agentId: state.final?.id ?? agentIdArg,
status: 'error',
message: state.error ?? 'Agent finished with error',
},
schema: agentSendSchema,
}
}
return {
type: 'single',
data: {
agentId,
agentId: state.final?.id ?? agentIdArg,
status: 'completed',
message: state.status === 'error' ? 'Agent finished with error' : 'Agent completed processing the message',
message: 'Agent completed processing the message',
},
schema: agentSendSchema,
}
+5 -11
View File
@@ -1,5 +1,5 @@
import type { Command } from 'commander'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
/** Result type for agent stop command */
@@ -53,13 +53,7 @@ export async function runStopCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
let agents = client.listAgents()
let agents = await client.fetchAgents()
const stoppedIds: string[] = []
if (options.all) {
@@ -76,8 +70,8 @@ export async function runStopCommand(
})
} else if (id) {
// Stop specific agent
const resolvedId = resolveAgentId(id, agents)
if (!resolvedId) {
const agent = await client.fetchAgent(id)
if (!agent) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `No agent found matching: ${id}`,
@@ -85,7 +79,7 @@ export async function runStopCommand(
}
throw error
}
agents = agents.filter((a) => a.id === resolvedId)
agents = [agent]
}
// Stop each agent
+37 -46
View File
@@ -1,11 +1,11 @@
import type { Command } from 'commander'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
/** Result type for agent wait command */
export interface AgentWaitResult {
agentId: string
status: 'idle' | 'timeout' | 'permission'
status: 'idle' | 'timeout' | 'permission' | 'error'
message: string
}
@@ -121,52 +121,56 @@ export async function runWaitCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID (supports prefix matching)
const agentId = resolveAgentId(agentIdArg, agents)
if (!agentId) {
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found: ${agentIdArg}`,
details: 'Use "paseo ls" to list available agents',
}
throw error
}
// Wait for agent to finish (idle, error, or permission)
try {
const state = await client.waitForFinish(agentId, timeoutMs)
const state = await client.waitForFinish(agentIdArg, timeoutMs)
await client.close()
// Check if agent has pending permissions
if (state.pendingPermissions && state.pendingPermissions.length > 0) {
const permission = state.pendingPermissions[0]
if (state.status === 'timeout') {
return {
type: 'single',
data: {
agentId,
status: 'permission',
message: `Agent is waiting for permission: ${permission.kind}`,
agentId: agentIdArg,
status: 'timeout',
message: `Timed out waiting for agent after ${timeoutSeconds} seconds`,
},
schema: agentWaitSchema,
}
}
// Agent is idle or error
if (state.status === 'permission') {
const permission = state.final?.pendingPermissions?.[0]
return {
type: 'single',
data: {
agentId: state.final?.id ?? agentIdArg,
status: 'permission',
message: permission
? `Agent is waiting for permission: ${permission.kind}`
: 'Agent is waiting for permission',
},
schema: agentWaitSchema,
}
}
if (state.status === 'error') {
return {
type: 'single',
data: {
agentId: state.final?.id ?? agentIdArg,
status: 'error',
message: state.error ?? 'Agent finished with error',
},
schema: agentWaitSchema,
}
}
// Agent is idle
return {
type: 'single',
data: {
agentId,
agentId: state.final?.id ?? agentIdArg,
status: 'idle',
message: state.status === 'error' ? 'Agent finished with error' : 'Agent is now idle',
message: 'Agent is now idle',
},
schema: agentWaitSchema,
}
@@ -175,19 +179,6 @@ export async function runWaitCommand(
const waitMessage = waitErr instanceof Error ? waitErr.message : String(waitErr)
// Check if it's a timeout error
if (waitMessage.toLowerCase().includes('timeout')) {
return {
type: 'single',
data: {
agentId,
status: 'timeout',
message: `Timed out waiting for agent after ${timeoutSeconds} seconds`,
},
schema: agentWaitSchema,
}
}
// Other errors
const error: CommandError = {
code: 'WAIT_FAILED',
+1 -7
View File
@@ -72,13 +72,7 @@ export async function runStatusCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
const agents = await client.fetchAgents()
const runningAgents = agents.filter((a) => a.status === 'running')
const idleAgents = agents.filter((a) => a.status === 'idle')
+4 -21
View File
@@ -1,6 +1,6 @@
import type { Command } from 'commander'
import type { AgentPermissionRequest } from '@paseo/server'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, ListResult, OutputSchema, CommandError } from '../../output/index.js'
/** Permission response item for display */
@@ -79,15 +79,8 @@ export async function runAllowCommand(
}
try {
// Request agent list
client.requestAgentList()
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID
const resolvedAgentId = resolveAgentId(agentIdOrPrefix, agents)
if (!resolvedAgentId) {
const agent = await client.fetchAgent(agentIdOrPrefix)
if (!agent) {
await client.close()
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
@@ -96,17 +89,7 @@ export async function runAllowCommand(
}
throw error
}
// Find the agent
const agent = agents.find((a) => a.id === resolvedAgentId)
if (!agent) {
await client.close()
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found: ${resolvedAgentId}`,
}
throw error
}
const resolvedAgentId = agent.id
// Get pending permissions for this agent
const pendingPermissions = agent.pendingPermissions || []
+4 -21
View File
@@ -1,6 +1,6 @@
import type { Command } from 'commander'
import type { AgentPermissionRequest } from '@paseo/server'
import { connectToDaemon, getDaemonHost, resolveAgentId } from '../../utils/client.js'
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
import type { CommandOptions, ListResult, CommandError } from '../../output/index.js'
import { permitResponseSchema, type PermissionResponseItem } from './allow.js'
@@ -45,15 +45,8 @@ export async function runDenyCommand(
}
try {
// Request agent list
client.requestAgentList()
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
// Resolve agent ID
const resolvedAgentId = resolveAgentId(agentIdOrPrefix, agents)
if (!resolvedAgentId) {
const agent = await client.fetchAgent(agentIdOrPrefix)
if (!agent) {
await client.close()
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
@@ -62,17 +55,7 @@ export async function runDenyCommand(
}
throw error
}
// Find the agent
const agent = agents.find((a) => a.id === resolvedAgentId)
if (!agent) {
await client.close()
const error: CommandError = {
code: 'AGENT_NOT_FOUND',
message: `Agent not found: ${resolvedAgentId}`,
}
throw error
}
const resolvedAgentId = agent.id
// Get pending permissions for this agent
const pendingPermissions = agent.pendingPermissions || []
+1 -7
View File
@@ -57,13 +57,7 @@ export async function runLsCommand(options: PermitLsOptions, _command: Command):
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
const agents = await client.fetchAgents()
await client.close()
// Collect all pending permissions from all agents
+1 -7
View File
@@ -63,13 +63,7 @@ export async function runLsCommand(
}
try {
// Request agent list
client.requestAgentList()
// Wait a moment for the agent list to be populated
await new Promise((resolve) => setTimeout(resolve, 500))
const agents = client.listAgents()
const agents = await client.fetchAgents()
// Get worktree list from daemon
const response = await client.getPaseoWorktreeList({})