Switch to double quotes and reformat codebase with Biome
This commit is contained in:
@@ -1,193 +1,198 @@
|
||||
import type { Command } from 'commander'
|
||||
import { connectToDaemon, getDaemonHost } from '../../utils/client.js'
|
||||
import type { CommandOptions, SingleResult, OutputSchema, CommandError } from '../../output/index.js'
|
||||
import { fetchAgentTimelineItems, formatAgentActivityTranscript } from './logs.js'
|
||||
import { parseDuration } from '../../utils/duration.js'
|
||||
import type { Command } from "commander";
|
||||
import { connectToDaemon, getDaemonHost } from "../../utils/client.js";
|
||||
import type {
|
||||
CommandOptions,
|
||||
SingleResult,
|
||||
OutputSchema,
|
||||
CommandError,
|
||||
} from "../../output/index.js";
|
||||
import { fetchAgentTimelineItems, formatAgentActivityTranscript } from "./logs.js";
|
||||
import { parseDuration } from "../../utils/duration.js";
|
||||
|
||||
/** Result type for agent wait command */
|
||||
export interface AgentWaitResult {
|
||||
agentId: string
|
||||
status: 'idle' | 'timeout' | 'permission' | 'error'
|
||||
message: string
|
||||
agentId: string;
|
||||
status: "idle" | "timeout" | "permission" | "error";
|
||||
message: string;
|
||||
}
|
||||
|
||||
/** Schema for agent wait output */
|
||||
export const agentWaitSchema: OutputSchema<AgentWaitResult> = {
|
||||
idField: 'agentId',
|
||||
idField: "agentId",
|
||||
columns: [
|
||||
{ header: 'AGENT ID', field: 'agentId', width: 12 },
|
||||
{ header: 'STATUS', field: 'status', width: 12 },
|
||||
{ header: 'MESSAGE', field: 'message', width: 40 },
|
||||
{ header: "AGENT ID", field: "agentId", width: 12 },
|
||||
{ header: "STATUS", field: "status", width: 12 },
|
||||
{ header: "MESSAGE", field: "message", width: 40 },
|
||||
],
|
||||
}
|
||||
};
|
||||
|
||||
export interface AgentWaitOptions extends CommandOptions {
|
||||
timeout?: string
|
||||
host?: string
|
||||
timeout?: string;
|
||||
host?: string;
|
||||
}
|
||||
|
||||
const WAIT_ACTIVITY_PREVIEW_COUNT = 5
|
||||
const WAIT_ACTIVITY_PREVIEW_COUNT = 5;
|
||||
|
||||
function appendRecentActivity(message: string, transcript: string | null): string {
|
||||
if (!transcript || transcript.trim().length === 0) {
|
||||
return message
|
||||
return message;
|
||||
}
|
||||
|
||||
return `${message}\nLast ${WAIT_ACTIVITY_PREVIEW_COUNT} activity items:\n${transcript}`
|
||||
return `${message}\nLast ${WAIT_ACTIVITY_PREVIEW_COUNT} activity items:\n${transcript}`;
|
||||
}
|
||||
|
||||
async function getRecentActivityTranscript(
|
||||
client: Awaited<ReturnType<typeof connectToDaemon>>,
|
||||
agentId: string
|
||||
agentId: string,
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const timelineItems = await fetchAgentTimelineItems(client, agentId)
|
||||
return formatAgentActivityTranscript(timelineItems, WAIT_ACTIVITY_PREVIEW_COUNT)
|
||||
const timelineItems = await fetchAgentTimelineItems(client, agentId);
|
||||
return formatAgentActivityTranscript(timelineItems, WAIT_ACTIVITY_PREVIEW_COUNT);
|
||||
} catch {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function runWaitCommand(
|
||||
agentIdArg: string,
|
||||
options: AgentWaitOptions,
|
||||
_command: Command
|
||||
_command: Command,
|
||||
): Promise<SingleResult<AgentWaitResult>> {
|
||||
const host = getDaemonHost({ host: options.host as string | undefined })
|
||||
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 wait <id>',
|
||||
}
|
||||
throw error
|
||||
code: "MISSING_AGENT_ID",
|
||||
message: "Agent ID is required",
|
||||
details: "Usage: paseo agent wait <id>",
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Parse timeout (no limit unless explicitly provided)
|
||||
let timeoutMs = 0
|
||||
let timeoutLabel: string | null = null
|
||||
let timeoutMs = 0;
|
||||
let timeoutLabel: string | null = null;
|
||||
if (options.timeout) {
|
||||
try {
|
||||
timeoutMs = parseDuration(options.timeout)
|
||||
timeoutMs = parseDuration(options.timeout);
|
||||
if (timeoutMs <= 0) {
|
||||
throw new Error('Timeout must be positive')
|
||||
throw new Error("Timeout must be positive");
|
||||
}
|
||||
const timeoutSeconds = Math.floor(timeoutMs / 1000)
|
||||
timeoutLabel = `${timeoutSeconds} second${timeoutSeconds === 1 ? '' : 's'}`
|
||||
const timeoutSeconds = Math.floor(timeoutMs / 1000);
|
||||
timeoutLabel = `${timeoutSeconds} second${timeoutSeconds === 1 ? "" : "s"}`;
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'INVALID_TIMEOUT',
|
||||
message: 'Invalid timeout value',
|
||||
code: "INVALID_TIMEOUT",
|
||||
message: "Invalid timeout value",
|
||||
details: message,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
let client
|
||||
let client;
|
||||
try {
|
||||
client = await connectToDaemon({ host: options.host as string | undefined })
|
||||
client = await connectToDaemon({ host: options.host as string | undefined });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'DAEMON_NOT_RUNNING',
|
||||
code: "DAEMON_NOT_RUNNING",
|
||||
message: `Cannot connect to daemon at ${host}: ${message}`,
|
||||
details: 'Start the daemon with: paseo daemon start',
|
||||
}
|
||||
throw error
|
||||
details: "Start the daemon with: paseo daemon start",
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
const state = await client.waitForFinish(agentIdArg, timeoutMs)
|
||||
const resolvedAgentId = state.final?.id ?? agentIdArg
|
||||
const state = await client.waitForFinish(agentIdArg, timeoutMs);
|
||||
const resolvedAgentId = state.final?.id ?? agentIdArg;
|
||||
const recentActivity =
|
||||
state.status === 'timeout' || state.status === 'idle'
|
||||
state.status === "timeout" || state.status === "idle"
|
||||
? await getRecentActivityTranscript(client, resolvedAgentId)
|
||||
: null
|
||||
: null;
|
||||
|
||||
await client.close()
|
||||
await client.close();
|
||||
|
||||
if (state.status === 'timeout') {
|
||||
if (state.status === "timeout") {
|
||||
const timeoutMessage = timeoutLabel
|
||||
? `Agent did not finish within ${timeoutLabel}. Run \`paseo wait ${resolvedAgentId}\` again to keep waiting.`
|
||||
: `Agent wait timed out. Run \`paseo wait ${resolvedAgentId}\` again to keep waiting.`
|
||||
: `Agent wait timed out. Run \`paseo wait ${resolvedAgentId}\` again to keep waiting.`;
|
||||
return {
|
||||
type: 'single',
|
||||
type: "single",
|
||||
data: {
|
||||
agentId: resolvedAgentId,
|
||||
status: 'timeout',
|
||||
status: "timeout",
|
||||
message: appendRecentActivity(timeoutMessage, recentActivity),
|
||||
},
|
||||
schema: agentWaitSchema,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (state.status === 'permission') {
|
||||
const permission = state.final?.pendingPermissions?.[0]
|
||||
if (state.status === "permission") {
|
||||
const permission = state.final?.pendingPermissions?.[0];
|
||||
return {
|
||||
type: 'single',
|
||||
type: "single",
|
||||
data: {
|
||||
agentId: resolvedAgentId,
|
||||
status: 'permission',
|
||||
status: "permission",
|
||||
message: permission
|
||||
? `Agent is waiting for permission: ${permission.kind}`
|
||||
: 'Agent is waiting for permission',
|
||||
: "Agent is waiting for permission",
|
||||
},
|
||||
schema: agentWaitSchema,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (state.status === 'error') {
|
||||
if (state.status === "error") {
|
||||
return {
|
||||
type: 'single',
|
||||
type: "single",
|
||||
data: {
|
||||
agentId: resolvedAgentId,
|
||||
status: 'error',
|
||||
message: state.error ?? 'Agent finished with error',
|
||||
status: "error",
|
||||
message: state.error ?? "Agent finished with error",
|
||||
},
|
||||
schema: agentWaitSchema,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Agent is idle
|
||||
return {
|
||||
type: 'single',
|
||||
type: "single",
|
||||
data: {
|
||||
agentId: resolvedAgentId,
|
||||
status: 'idle',
|
||||
message: appendRecentActivity('Agent is idle.', recentActivity),
|
||||
status: "idle",
|
||||
message: appendRecentActivity("Agent is idle.", recentActivity),
|
||||
},
|
||||
schema: agentWaitSchema,
|
||||
}
|
||||
};
|
||||
} catch (waitErr) {
|
||||
await client.close().catch(() => {})
|
||||
await client.close().catch(() => {});
|
||||
|
||||
const waitMessage = waitErr instanceof Error ? waitErr.message : String(waitErr)
|
||||
const waitMessage = waitErr instanceof Error ? waitErr.message : String(waitErr);
|
||||
|
||||
// Other errors
|
||||
const error: CommandError = {
|
||||
code: 'WAIT_FAILED',
|
||||
code: "WAIT_FAILED",
|
||||
message: `Failed to wait for agent: ${waitMessage}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
} catch (err) {
|
||||
await client.close().catch(() => {})
|
||||
await client.close().catch(() => {});
|
||||
|
||||
// Re-throw CommandError as-is
|
||||
if (err && typeof err === 'object' && 'code' in err) {
|
||||
throw err
|
||||
if (err && typeof err === "object" && "code" in err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const error: CommandError = {
|
||||
code: 'WAIT_FAILED',
|
||||
code: "WAIT_FAILED",
|
||||
message: `Failed to wait for agent: ${message}`,
|
||||
}
|
||||
throw error
|
||||
};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user