Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions apps/web/src/__tests__/agents-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ describe('GET /api/agents', () => {
expect.objectContaining({
where: expect.objectContaining({
OR: expect.arrayContaining([
expect.objectContaining({ name: expect.objectContaining({ contains: 'classifier' }) }),
expect.objectContaining({
name: expect.objectContaining({ contains: 'classifier' }),
}),
]),
}),
}),
Expand Down Expand Up @@ -207,7 +209,9 @@ describe('DELETE /api/agents/[id]', () => {
mockAgentFindUnique.mockResolvedValue(MOCK_AGENT);
mockAgentDelete.mockResolvedValue(MOCK_AGENT);

const req = new NextRequest('http://localhost:3000/api/agents/agent-1', { method: 'DELETE' });
const req = new NextRequest('http://localhost:3000/api/agents/agent-1', {
method: 'DELETE',
});
const res = await DELETE(req as never, { params: Promise.resolve({ id: 'agent-1' }) });
const body = await res.json();

Expand All @@ -231,17 +235,18 @@ describe('POST /api/agents/[id]/run', () => {
mockGetServerSession.mockResolvedValue(userSession('tm-1', 'ADMIN'));
});

it('triggers a run and updates lastRun', async () => {
it('reports that runs are not available instead of a queued run', async () => {
mockAgentFindUnique.mockResolvedValue(MOCK_AGENT);
mockAgentUpdate.mockResolvedValue({ ...MOCK_AGENT, lastRun: new Date() });

const req = makeJsonRequest('http://localhost:3000/api/agents/agent-1/run', {});
const res = await RUN(req as never, { params: Promise.resolve({ id: 'agent-1' }) });
const body = await res.json();

expect(res.status).toBe(202);
expect(body.status).toBe('queued');
expect(mockAgentUpdate).toHaveBeenCalledTimes(1);
expect(res.status).toBe(501);
expect(body.error).toContain('not available yet');
expect(body.status).toBeUndefined();
// Nothing ran, so nothing may claim it did.
expect(mockAgentUpdate).not.toHaveBeenCalled();
});

it('returns 404 for non-existent agent', async () => {
Expand Down Expand Up @@ -308,7 +313,11 @@ describe('Agent form validation', () => {
it('fails when webhook URL is not valid', () => {
const errors = validateAgentForm({
...validData,
config: { triggerType: 'manual', actionType: 'custom_webhook', webhookUrl: 'not-a-url' },
config: {
triggerType: 'manual',
actionType: 'custom_webhook',
webhookUrl: 'not-a-url',
},
});
expect(errors.webhookUrl).toBe('Must be a valid URL');
});
Expand All @@ -328,7 +337,11 @@ describe('Agent form validation', () => {
it('passes for valid interval trigger', () => {
const errors = validateAgentForm({
...validData,
config: { triggerType: 'interval', actionType: 'classify_tickets', intervalMinutes: 15 },
config: {
triggerType: 'interval',
actionType: 'classify_tickets',
intervalMinutes: 15,
},
});
expect(Object.keys(errors)).toHaveLength(0);
});
Expand Down
54 changes: 35 additions & 19 deletions apps/web/src/__tests__/agents-components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ describe('AgentStatusBadge', () => {
describe('AgentActions', () => {
it('renders run, edit, and delete buttons', () => {
render(
<AgentActions
agentId="agent-1"
onRun={vi.fn()}
onEdit={vi.fn()}
onDelete={vi.fn()}
/>,
<AgentActions agentId="agent-1" onRun={vi.fn()} onEdit={vi.fn()} onDelete={vi.fn()} />,
);
expect(screen.getByTestId('agent-run-btn')).toBeTruthy();
expect(screen.getByTestId('agent-edit-btn')).toBeTruthy();
Expand All @@ -68,21 +63,33 @@ describe('AgentActions', () => {
onRun={onRun}
onEdit={vi.fn()}
onDelete={vi.fn()}
runsAvailable
/>,
);
fireEvent.click(screen.getByTestId('agent-run-btn'));
const runButton = screen.getByTestId('agent-run-btn');
expect(runButton).not.toBeDisabled();
fireEvent.click(runButton);
expect(onRun).toHaveBeenCalledWith('agent-1');
});

it('disables the run button when runs are unavailable', () => {
const onRun = vi.fn();
render(
<AgentActions agentId="agent-1" onRun={onRun} onEdit={vi.fn()} onDelete={vi.fn()} />,
);
const runButton = screen.getByTestId('agent-run-btn');
expect(runButton).toBeDisabled();
// A disabled button shows no tooltip of its own, so the wrapper carries
// the reason.
expect(screen.getByTitle('Manual runs are not available yet')).toBeTruthy();
fireEvent.click(runButton);
expect(onRun).not.toHaveBeenCalled();
});

it('calls onEdit with agent ID when edit button clicked', () => {
const onEdit = vi.fn();
render(
<AgentActions
agentId="agent-1"
onRun={vi.fn()}
onEdit={onEdit}
onDelete={vi.fn()}
/>,
<AgentActions agentId="agent-1" onRun={vi.fn()} onEdit={onEdit} onDelete={vi.fn()} />,
);
fireEvent.click(screen.getByTestId('agent-edit-btn'));
expect(onEdit).toHaveBeenCalledWith('agent-1');
Expand All @@ -91,12 +98,7 @@ describe('AgentActions', () => {
it('calls onDelete with agent ID when delete button clicked', () => {
const onDelete = vi.fn();
render(
<AgentActions
agentId="agent-1"
onRun={vi.fn()}
onEdit={vi.fn()}
onDelete={onDelete}
/>,
<AgentActions agentId="agent-1" onRun={vi.fn()} onEdit={vi.fn()} onDelete={onDelete} />,
);
fireEvent.click(screen.getByTestId('agent-delete-btn'));
expect(onDelete).toHaveBeenCalledWith('agent-1');
Expand Down Expand Up @@ -145,6 +147,20 @@ describe('AgentTable', () => {
render(<AgentTable {...defaultProps} agents={[neverRunAgent]} />);
expect(screen.getByText('Never')).toBeTruthy();
});

it('disables the run button unless runs are available', () => {
render(<AgentTable {...defaultProps} />);
for (const button of screen.getAllByTestId('agent-run-btn')) {
expect(button).toBeDisabled();
}
});

it('enables the run button when runs are available', () => {
render(<AgentTable {...defaultProps} runsAvailable />);
for (const button of screen.getAllByTestId('agent-run-btn')) {
expect(button).not.toBeDisabled();
}
});
});

describe('AgentForm', () => {
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/app/agents/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function AgentsPage() {
<div>
<PageHeader
title="Agents"
description="Configure and monitor AI agents for automated support operations."
description="Configure and monitor AI agents for automated support operations. Manual runs are not available yet — see outpost#218."
icon={Bot}
breadcrumbs={[{ label: 'Agents' }]}
/>
Expand Down Expand Up @@ -121,6 +121,10 @@ export default function AgentsPage() {

{/* Table */}
<div className="rounded-lg border border-border bg-card">
{/* Manual runs have no execution framework behind them yet: the
API answers 501 instead of pretending a run was queued, so
the button is disabled rather than offering a run that
cannot happen. */}
{loading ? (
<div className="flex items-center justify-center py-12 text-sm text-muted-foreground">
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Expand All @@ -132,6 +136,7 @@ export default function AgentsPage() {
onRun={handleRun}
onEdit={handleEdit}
onDelete={handleDelete}
runsAvailable={false}
/>
)}
</div>
Expand Down
41 changes: 15 additions & 26 deletions apps/web/src/app/api/agents/[id]/run/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { requireAdmin } from '@/lib/require-admin';
/**
* POST /api/agents/[id]/run
*
* Manually trigger an agent run. Validates the agent exists and updates
* its lastRun timestamp. In a full implementation, this would also
* enqueue a job via the job queue for the configured action type.
* Manually trigger an agent run.
*
* No execution framework is wired up yet: nothing in the queue or the worker
* reads `Agent` rows, so a run cannot be queued. This route used to answer
* 202 with `status: 'queued'` and stamp `lastRun` — three signals that the
* agent ran, none of them true. It now says the run did not happen.
*/
export async function POST(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
export async function POST(_request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { error } = await requireAdmin();
if (error) return error;

Expand All @@ -23,23 +23,12 @@ export async function POST(
return NextResponse.json({ error: 'Agent not found' }, { status: 404 });
}

const config = agent.config as Record<string, unknown> | null;
const actionType = config?.actionType ?? 'unknown';

// Update the agent's lastRun timestamp
await prisma.agent.update({
where: { id },
data: { lastRun: new Date() },
});

const result = {
agentId: agent.id,
agentName: agent.name,
actionType,
status: 'queued',
triggeredAt: new Date().toISOString(),
message: `Agent "${agent.name}" run has been queued. Action: ${actionType}`,
};

return NextResponse.json(result, { status: 202 });
return NextResponse.json(
{
error: 'Agent runs are not available yet: no execution framework is wired up.',
agentId: agent.id,
agentName: agent.name,
},
{ status: 501 },
);
}
46 changes: 37 additions & 9 deletions apps/web/src/components/agents/agent-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,47 @@ interface AgentActionsProps {
onRun: (id: string) => void;
onEdit: (id: string) => void;
onDelete: (id: string) => void;
/**
* Manual runs have no execution framework behind them yet, so the run
* button is disabled rather than offering a run that cannot happen.
* Defaults to unavailable: a run button that cannot run is the worse
* default.
*/
runsAvailable?: boolean;
}

export function AgentActions({ agentId, onRun, onEdit, onDelete }: AgentActionsProps) {
export function AgentActions({
agentId,
onRun,
onEdit,
onDelete,
runsAvailable = false,
}: AgentActionsProps) {
const runButton = (
<button
onClick={() => onRun(agentId)}
disabled={!runsAvailable}
className={
runsAvailable
? 'rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground transition-colors'
: 'rounded-md p-1.5 text-muted-foreground opacity-50 cursor-not-allowed'
}
title={runsAvailable ? 'Run Now' : undefined}
data-testid="agent-run-btn"
>
<Play className="h-4 w-4" />
</button>
);

return (
<div className="flex items-center gap-1">
<button
onClick={() => onRun(agentId)}
className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground transition-colors"
title="Run Now"
data-testid="agent-run-btn"
>
<Play className="h-4 w-4" />
</button>
{/* A disabled button emits no mouse events, so no browser shows a
tooltip for it. The wrapper carries the reason instead. */}
{runsAvailable ? (
runButton
) : (
<span title="Manual runs are not available yet">{runButton}</span>
)}
<button
onClick={() => onEdit(agentId)}
className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground transition-colors"
Expand Down
20 changes: 15 additions & 5 deletions apps/web/src/components/agents/agent-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface AgentTableProps {
onRun: (id: string) => void;
onEdit: (id: string) => void;
onDelete: (id: string) => void;
/** Manual runs are not available yet, so the run button is disabled. */
runsAvailable?: boolean;
}

function formatLastRun(lastRun: string | null): string {
Expand All @@ -33,10 +35,19 @@ function formatLastRun(lastRun: string | null): string {
});
}

export function AgentTable({ agents, onRun, onEdit, onDelete }: AgentTableProps) {
export function AgentTable({
agents,
onRun,
onEdit,
onDelete,
runsAvailable = false,
}: AgentTableProps) {
if (agents.length === 0) {
return (
<div className="flex items-center justify-center py-12 text-sm text-muted-foreground" data-testid="agent-table-empty">
<div
className="flex items-center justify-center py-12 text-sm text-muted-foreground"
data-testid="agent-table-empty"
>
No agents configured. Create an agent to automate support tasks.
</div>
);
Expand All @@ -61,9 +72,7 @@ export function AgentTable({ agents, onRun, onEdit, onDelete }: AgentTableProps)
className="border-b border-border/50 hover:bg-accent/50 transition-colors"
data-testid="agent-row"
>
<td className="px-4 py-3 font-medium text-foreground">
{agent.name}
</td>
<td className="px-4 py-3 font-medium text-foreground">{agent.name}</td>
<td className="px-4 py-3 text-muted-foreground max-w-[300px] truncate">
{agent.description || '--'}
</td>
Expand All @@ -79,6 +88,7 @@ export function AgentTable({ agents, onRun, onEdit, onDelete }: AgentTableProps)
onRun={onRun}
onEdit={onEdit}
onDelete={onDelete}
runsAvailable={runsAvailable}
/>
</td>
</tr>
Expand Down