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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"format": "prettier --write \"src/**/*.ts\"",
"format:fix": "prettier --write \"src/**/*.ts\"",
"format:check": "prettier --check \"src/**/*.ts\"",
"prepare": "husky",
"prepare": "bash -c 'npm run build && (husky || true)'",
"setup": "tsx setup/index.ts",
"auth": "tsx src/whatsapp-auth.ts",
"test:e2e:tasks": "tsx scripts/test-task-sdk-e2e.ts",
Expand Down
155 changes: 154 additions & 1 deletion src/agent/actions-http.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import fs from 'fs';
import os from 'os';
import path from 'path';

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { z } from 'zod';

import { ActionsHttp } from './actions-http.js';
import type { AgentStatus } from './status-writer.js';
import type { ActionContext, RegisteredAction } from '../api/action.js';

// ─── Test helpers ──────────────────────────────────────────────────
Expand Down Expand Up @@ -50,10 +55,24 @@ describe('ActionsHttp', () => {
let server: ActionsHttp;
let baseUrl: string;
let token: string;
let dataDir: string;

function readStatus(): AgentStatus {
return JSON.parse(
fs.readFileSync(path.join(dataDir, 'ipc', 'status.json'), 'utf8'),
) as AgentStatus;
}

beforeEach(async () => {
actions = new Map();
server = new ActionsHttp(() => actions);
dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agentlite-actions-http-'));
server = new ActionsHttp(() => actions, {
agentId: 'agent-1',
agentName: 'Observer',
dataDir,
sessionId: 'session-1',
sessionStartedAt: '2026-04-19T10:00:00.000Z',
});
const info = await server.start();
if (!info) throw new Error('No LAN IP available for tests');
baseUrl = info.url;
Expand All @@ -64,6 +83,7 @@ describe('ActionsHttp', () => {

afterEach(async () => {
await server.stop();
fs.rmSync(dataDir, { force: true, recursive: true });
});

describe('auth', () => {
Expand Down Expand Up @@ -98,6 +118,139 @@ describe('ActionsHttp', () => {
});
});

describe('status instrumentation', () => {
it('writes an idle status file when the server starts', () => {
expect(readStatus()).toEqual({
schemaVersion: 1,
updatedAt: expect.any(String),
agentId: 'agent-1',
agentName: 'Observer',
status: 'idle',
phase: 'idle',
currentTool: null,
toolArgsSummary: null,
lastToolResultSummary: null,
lastToolDurationMs: null,
lastToolResult: null,
turnCount: 0,
currentTaskId: null,
workItemId: null,
workItemTitle: null,
sessionId: 'session-1',
sessionStartedAt: '2026-04-19T10:00:00.000Z',
});
});

it('writes tool start and completion status around a successful action call', async () => {
let statusSeenInsideHandler: AgentStatus | null = null;
actions.set(
'workflow_items_move',
makeAction({
inputSchema: { itemId: z.string() },
handler: async () => {
statusSeenInsideHandler = readStatus();
return { ok: true };
},
}),
);

const res = await post(
baseUrl,
'/call',
{ name: 'workflow_items_move', payload: { itemId: 'item-42' } },
token,
);

expect(res.status).toBe(200);
expect(statusSeenInsideHandler).toEqual({
schemaVersion: 1,
updatedAt: expect.any(String),
agentId: 'agent-1',
agentName: 'Observer',
status: 'tool-calling',
phase: 'tool_call_start',
currentTool: 'workflow_items_move',
toolArgsSummary: 'workflow_items_move',
lastToolResultSummary: null,
lastToolDurationMs: null,
lastToolResult: null,
turnCount: 1,
currentTaskId: null,
workItemId: null,
workItemTitle: null,
sessionId: 'session-1',
sessionStartedAt: '2026-04-19T10:00:00.000Z',
});

expect(readStatus()).toEqual({
schemaVersion: 1,
updatedAt: expect.any(String),
agentId: 'agent-1',
agentName: 'Observer',
status: 'idle',
phase: 'tool_call_done',
currentTool: null,
toolArgsSummary: 'workflow_items_move',
lastToolResultSummary: '{"ok":true}',
lastToolDurationMs: expect.any(Number),
lastToolResult: {
toolName: 'workflow_items_move',
preview: '{"ok":true}',
},
turnCount: 1,
currentTaskId: null,
workItemId: null,
workItemTitle: null,
sessionId: 'session-1',
sessionStartedAt: '2026-04-19T10:00:00.000Z',
});
});

it('writes an error status when an action handler throws', async () => {
actions.set(
'workflow_tasks_update',
makeAction({
inputSchema: { itemId: z.string() },
handler: async () => {
throw new Error('boom');
},
}),
);

const res = await post(
baseUrl,
'/call',
{ name: 'workflow_tasks_update', payload: { itemId: 'item-43' } },
token,
);

expect(res.status).toBe(500);
expect(readStatus()).toEqual({
schemaVersion: 1,
updatedAt: expect.any(String),
agentId: 'agent-1',
agentName: 'Observer',
status: 'error',
phase: 'error',
currentTool: null,
toolArgsSummary: 'workflow_tasks_update',
lastToolResultSummary: null,
lastToolDurationMs: expect.any(Number),
lastToolResult: {
toolName: 'workflow_tasks_update',
preview: 'boom',
isError: true,
},
turnCount: 1,
currentTaskId: null,
workItemId: null,
workItemTitle: null,
sessionId: 'session-1',
sessionStartedAt: '2026-04-19T10:00:00.000Z',
});
});
});

describe('/search query grammar', () => {
beforeEach(() => {
actions.set(
Expand Down
Loading