Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(agent-tools): report engineLoaded the way GET /sessions does

Yudhi Armyndharisde43fe63bb21
diff --git a/src/core/agent-tools/tools/session.tools.spec.ts b/src/core/agent-tools/tools/session.tools.spec.ts
index 3bf016e22..1dda404d6 100644
--- a/src/core/agent-tools/tools/session.tools.spec.ts
+++ b/src/core/agent-tools/tools/session.tools.spec.ts
@@ -24,14 +24,19 @@ async function run(tool: AnyToolDescriptor, input: unknown): Promise<unknown> {
}
describe('sessionTools', () => {
+ // engineLoaded, not isActive: a session a live peer node runs has no engine in this process, and
+ // an MCP call is never forwarded to the owner, so isActive would report it as stopped.
it('SessionFindAll scopes by the key allowedSessions and maps entities to DTOs', async () => {
- const findAll = jest.fn().mockResolvedValue([{ id: 's1', name: 'main', status: 'ready' }]);
- const isActive = jest.fn().mockReturnValue(true);
- const out = (await run(makeTools({ findAll, isActive } as unknown as SessionService).get('SessionFindAll')!, {
- limit: 5,
- })) as Array<{ id: string; engineLoaded: boolean }>;
+ const row = { id: 's1', name: 'main', status: 'ready' };
+ const findAll = jest.fn().mockResolvedValue([row]);
+ const isActive = jest.fn().mockReturnValue(false);
+ const engineLoaded = jest.fn().mockReturnValue(true);
+ const out = (await run(
+ makeTools({ findAll, isActive, engineLoaded } as unknown as SessionService).get('SessionFindAll')!,
+ { limit: 5 },
+ )) as Array<{ id: string; engineLoaded: boolean }>;
expect(findAll).toHaveBeenCalledWith(null, { limit: 5, offset: undefined });
- expect(isActive).toHaveBeenCalledWith('s1');
+ expect(engineLoaded).toHaveBeenCalledWith(row);
expect(out).toEqual([expect.objectContaining({ id: 's1', engineLoaded: true })]);
});
@@ -53,14 +58,18 @@ describe('sessionTools', () => {
});
it('SessionFindOne delegates to findOne and maps to the response DTO', async () => {
- const findOne = jest.fn().mockResolvedValue({ id: 's1', name: 'main', status: 'ready' });
+ const row = { id: 's1', name: 'main', status: 'ready' };
+ const findOne = jest.fn().mockResolvedValue(row);
const isActive = jest.fn().mockReturnValue(false);
- const out = (await run(makeTools({ findOne, isActive } as unknown as SessionService).get('SessionFindOne')!, {
- sessionId: 's1',
- })) as { id: string; engineLoaded: boolean };
+ const engineLoaded = jest.fn().mockReturnValue(true);
+ const out = (await run(
+ makeTools({ findOne, isActive, engineLoaded } as unknown as SessionService).get('SessionFindOne')!,
+ { sessionId: 's1' },
+ )) as { id: string; engineLoaded: boolean };
expect(findOne).toHaveBeenCalledWith('s1');
+ expect(engineLoaded).toHaveBeenCalledWith(row);
expect(out.id).toBe('s1');
- expect(out.engineLoaded).toBe(false);
+ expect(out.engineLoaded).toBe(true);
});
it('SessionGetChats delegates to getChats with paging', async () => {
diff --git a/src/core/agent-tools/tools/session.tools.ts b/src/core/agent-tools/tools/session.tools.ts
index 31f898ea2..2868a71ce 100644
--- a/src/core/agent-tools/tools/session.tools.ts
+++ b/src/core/agent-tools/tools/session.tools.ts
@@ -26,7 +26,7 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
handler: (input, apiKey) =>
session
.findAll(apiKey.allowedSessions, { limit: input.limit, offset: input.offset, name: input.name })
- .then(ss => ss.map(s => SessionResponseDto.fromEntity(s, session.isActive(s.id)))),
+ .then(ss => ss.map(s => SessionResponseDto.fromEntity(s, session.engineLoaded(s)))),
}),
defineTool({
name: 'SessionFindOne',
@@ -35,7 +35,7 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {
sessionScoped: true,
inputSchema: z.object({ sessionId }),
handler: input =>
- session.findOne(input.sessionId).then(s => SessionResponseDto.fromEntity(s, session.isActive(s.id))),
+ session.findOne(input.sessionId).then(s => SessionResponseDto.fromEntity(s, session.engineLoaded(s))),
}),
defineTool({
name: 'SessionGetChats',
diff --git a/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts b/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
index d42ff27e4..bbf497617 100644
--- a/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
+++ b/src/core/agent-tools/tools/tool-output-sanitisation.spec.ts
@@ -126,7 +126,7 @@ describe('FIX 2: session tools strip config + proxyUrl', () => {
it('SessionFindOne output has no config or proxyUrl keys', async () => {
const sessionSvc = {
findOne: jest.fn().mockResolvedValue(entity),
- isActive: jest.fn().mockReturnValue(false),
+ engineLoaded: jest.fn().mockReturnValue(false),
} as unknown as SessionService;
const tool = sessionTools(sessionSvc).find(t => t.name === 'SessionFindOne')!;
@@ -141,7 +141,7 @@ describe('FIX 2: session tools strip config + proxyUrl', () => {
it('SessionFindAll output items have no config or proxyUrl keys', async () => {
const sessionSvc = {
findAll: jest.fn().mockResolvedValue([entity]),
- isActive: jest.fn().mockReturnValue(false),
+ engineLoaded: jest.fn().mockReturnValue(false),
} as unknown as SessionService;
const tool = sessionTools(sessionSvc).find(t => t.name === 'SessionFindAll')!;
@@ -162,7 +162,7 @@ describe('FIX 5: empty sessionId is rejected at validation', () => {
it('a sessionScoped tool rejects sessionId: "" with BadRequestException', async () => {
const sessionSvc = {
findOne: jest.fn().mockResolvedValue(stubSessionEntity()),
- isActive: jest.fn().mockReturnValue(false),
+ engineLoaded: jest.fn().mockReturnValue(false),
} as unknown as SessionService;
const tool = sessionTools(sessionSvc).find(t => t.name === 'SessionFindOne')!;