forked from apify/apify-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.call_actor_common.test.ts
More file actions
414 lines (368 loc) · 17.9 KB
/
Copy pathtools.call_actor_common.test.ts
File metadata and controls
414 lines (368 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { ApifyApiError } from 'apify-client';
import type { AxiosResponse } from 'axios';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
APIFY_ERROR_TYPE_MEMORY_LIMIT_EXCEEDED,
FAILURE_CATEGORY,
HELPER_TOOLS,
TOOL_STATUS,
} from '../../src/const.js';
import * as mcpClient from '../../src/mcp/client.js';
import { EXTERNAL_TOOL_CALL_TIMEOUT_MSEC } from '../../src/mcp/const.js';
import {
buildCallActorAppsDescription,
buildCallActorDescription,
buildCallActorErrorResponse,
callActorArgs,
handleMcpToolCall,
resolveAndValidateActor,
} from '../../src/tools/actors/call_actor.js';
import type { InternalToolArgs, ToolEntry } from '../../src/types.js';
import { TOOL_TYPE } from '../../src/types.js';
import { textOf, type TextToolResult } from './helpers/tool_context.js';
vi.mock('../../src/tools/actors/actor_tools_factory.js', async () => {
const actual = await vi.importActual<Record<string, unknown>>('../../src/tools/actors/actor_tools_factory.js');
return { ...actual, getActorsAsTools: vi.fn() };
});
const { getActorsAsTools } = await import('../../src/tools/actors/actor_tools_factory.js');
describe('call_actor_common', () => {
describe('buildCallActorDescription', () => {
it('builds the description with public helper tools and waitSecs guidance', () => {
const description = buildCallActorDescription();
expect(description).toContain(`Use ${HELPER_TOOLS.ACTOR_GET_DETAILS} to get the Actor's input schema`);
expect(description).toContain(
`${HELPER_TOOLS.STORE_SEARCH} is available in this session, use it to resolve the correct Actor first`,
);
expect(description).toContain('waitSecs');
expect(description).toContain(HELPER_TOOLS.DATASET_GET_ITEMS);
expect(description).not.toContain('always runs asynchronously');
expect(description).not.toContain(HELPER_TOOLS.ACTOR_CALL_WIDGET);
});
});
describe('buildCallActorAppsDescription', () => {
it('appends widget guidance to the shared description', () => {
const description = buildCallActorAppsDescription();
expect(description).toContain(HELPER_TOOLS.ACTOR_CALL_WIDGET);
expect(description).toContain(HELPER_TOOLS.STORE_SEARCH_WIDGET);
expect(description).toContain('waitSecs');
expect(description).toContain(HELPER_TOOLS.DATASET_GET_ITEMS);
});
});
describe('buildCallActorErrorResponse', () => {
it('uses public helper tool names and preserves telemetry fields', () => {
const error = Object.assign(new Error('Actor not found'), { statusCode: 404 });
const response = buildCallActorErrorResponse({
actorName: 'apify/rag-web-browser',
error,
actorId: 'actor-123',
mcpSessionId: 'session-123',
actorGetDetailsTool: HELPER_TOOLS.ACTOR_GET_DETAILS,
});
expect(response.isError).toBe(true);
const allText = (response.content ?? []).map(textOf).join('\n');
expect(allText).toContain(`If ${HELPER_TOOLS.STORE_SEARCH} is available in this session`);
expect(allText).toContain(`using: ${HELPER_TOOLS.ACTOR_GET_DETAILS}`);
expect(response.toolTelemetry).toEqual(
expect.objectContaining({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
failureHttpStatus: 404,
failureDetail: 'Actor not found',
actorId: 'actor-123',
}),
);
});
it('returns approval URL for full-permission-actor-not-approved error', () => {
const approvalUrl = 'https://console.apify.com/actors/abc123?approvePermissions=true';
const error = new ApifyApiError(
{
data: {
error: {
type: 'full-permission-actor-not-approved',
message:
'This Actor requires full access to your account. You must approve its permissions before running it.',
data: { approvalUrl },
},
},
status: 403,
} as AxiosResponse,
1,
);
const response = buildCallActorErrorResponse({
actorName: 'apify/some-actor',
error,
actorId: 'actor-456',
actorGetDetailsTool: HELPER_TOOLS.ACTOR_GET_DETAILS,
});
expect(response.isError).toBe(true);
const allText = (response.content ?? []).map(textOf).join('\n');
expect(allText).toContain('This Actor requires full access to your account');
expect(allText).toContain(approvalUrl);
expect(response.toolTelemetry).toEqual(
expect.objectContaining({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.PERMISSION_APPROVAL_REQUIRED,
failureHttpStatus: 403,
actorId: 'actor-456',
}),
);
});
it('uses public search helper name for generic errors', () => {
const response = buildCallActorErrorResponse({
actorName: 'apify/rag-web-browser',
error: new Error('boom'),
actorGetDetailsTool: HELPER_TOOLS.ACTOR_GET_DETAILS,
});
const allText = (response.content ?? []).map(textOf).join('\n');
expect(allText).toContain(`If ${HELPER_TOOLS.STORE_SEARCH} is available in this session`);
expect(allText).toContain(`using: ${HELPER_TOOLS.ACTOR_GET_DETAILS}`);
expect(response.toolTelemetry).toEqual(
expect.objectContaining({
toolStatus: TOOL_STATUS.FAILED,
failureCategory: FAILURE_CATEGORY.INTERNAL_ERROR,
failureDetail: 'boom',
}),
);
});
it('returns memory-quota recovery hint for HTTP 402 memory-limit errors', () => {
const error = new ApifyApiError(
{
data: {
error: {
type: APIFY_ERROR_TYPE_MEMORY_LIMIT_EXCEEDED,
message:
'By launching this job you will exceed the memory limit of 8192MB for all your Actor runs and builds.',
},
},
status: 402,
} as AxiosResponse,
1,
);
const response = buildCallActorErrorResponse({
actorName: 'compass/crawler-google-places',
error,
actorId: 'actor-789',
actorGetDetailsTool: HELPER_TOOLS.ACTOR_GET_DETAILS,
});
expect(response.isError).toBe(true);
const allText = (response.content ?? []).map(textOf).join('\n');
expect(allText).toContain('memory limit of 8192MB');
expect(allText).toContain('Account memory quota exceeded');
expect(allText).toContain('callOptions.memory');
// Regression: must not nudge the LLM toward aborting unrelated runs to free capacity.
expect(allText).not.toContain(HELPER_TOOLS.ACTOR_RUNS_ABORT);
expect(allText).not.toContain('verify the Actor name');
// 402 memory-limit derives SOFT_FAIL (4xx) with the memory-limit failureDetail.
expect(response.toolTelemetry).toEqual(
expect.objectContaining({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
failureHttpStatus: 402,
failureDetail: APIFY_ERROR_TYPE_MEMORY_LIMIT_EXCEEDED,
actorId: 'actor-789',
}),
);
});
it('returns the concurrent-run-limit billing hint for cannot-start-actor-runs errors', () => {
const error = new ApifyApiError(
{
data: {
error: {
type: 'cannot-start-actor-runs',
message:
'Cannot start new Actor runs. Underlying error: By launching this job you will exceed your limit of 25 concurrent Actor runs.',
},
},
status: 402,
} as AxiosResponse,
1,
);
const response = buildCallActorErrorResponse({
actorName: 'apify/instagram-scraper',
error,
actorId: 'actor-999',
actorGetDetailsTool: HELPER_TOOLS.ACTOR_GET_DETAILS,
});
expect(response.isError).toBe(true);
const allText = (response.content ?? []).map(textOf).join('\n');
expect(allText).toContain('account limit for concurrent Actor runs');
expect(allText).toContain('console.apify.com/billing/subscription');
// Run-limit must not fall through to the generic "verify the Actor name" hint.
expect(allText).not.toContain('verify the Actor name');
// Run-limit derives SOFT_FAIL even though it arrives as 402/5xx (billing condition).
expect(response.toolTelemetry).toEqual(
expect.objectContaining({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
failureDetail: 'cannot-start-actor-runs',
actorId: 'actor-999',
}),
);
});
});
describe('callActorArgs.callOptions', () => {
const baseArgs = { actor: 'apify/rag-web-browser', input: { query: 'hello' } };
it.each([
['memory', { memory: 1024 }],
['timeout', { timeout: 60 }],
['build', { build: 'latest' }],
['maxItems', { maxItems: 3 }],
['maxTotalChargeUsd', { maxTotalChargeUsd: 1.5 }],
['memory + build', { memory: 1024, build: 'latest' }],
])('accepts %s', (_name, callOptions) => {
const result = callActorArgs.safeParse({ ...baseArgs, callOptions });
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.callOptions).toEqual(callOptions);
}
});
it('rejects negative maxItems', () => {
const result = callActorArgs.safeParse({
...baseArgs,
callOptions: { maxItems: -1 },
});
expect(result.success).toBe(false);
});
});
describe('resolveAndValidateActor', () => {
const INPUT_SCHEMA = { type: 'object', properties: { query: { type: 'string' } } };
const stubToolArgs = { apifyClient: {}, mcpSessionId: 'session-1' } as unknown as InternalToolArgs;
beforeEach(() => {
vi.mocked(getActorsAsTools).mockReset();
});
function mockActorTool(ajvValidate: ((input: unknown) => boolean) & { errors?: unknown }): void {
const tool = {
type: TOOL_TYPE.ACTOR,
name: 'apify--rag-web-browser',
actorId: 'actor-id-rag',
actorFullName: 'apify/rag-web-browser',
inputSchema: INPUT_SCHEMA,
ajvValidate,
} as unknown as ToolEntry;
vi.mocked(getActorsAsTools).mockResolvedValue({ tools: [tool], errors: [] });
}
it('returns a SOFT_FAIL/404 not-found error when the Actor is missing', async () => {
vi.mocked(getActorsAsTools).mockResolvedValue({ tools: [], errors: [] });
const resolution = await resolveAndValidateActor({
actorName: 'apify/missing',
input: { query: 'x' },
toolArgs: stubToolArgs,
});
expect('error' in resolution).toBe(true);
const { error } = resolution as { error: TextToolResult & { toolTelemetry?: unknown } };
expect(error.isError).toBe(true);
expect(error.content[0].text).toContain("Actor 'apify/missing' was not found");
expect(error.toolTelemetry).toEqual({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
failureHttpStatus: 404,
failureDetail: "Actor 'apify/missing' was not found",
});
});
// Byte-identity guard for #937: the input-required error embeds the input schema in a
// ```json fence built via wrapJsonText — the exact bytes of the former hand-rolled fence.
it('embeds the input schema in an unchanged ```json fence for the input-required error', async () => {
mockActorTool((() => true) as never);
const resolution = await resolveAndValidateActor({
actorName: 'apify/rag-web-browser',
input: null as unknown as Record<string, unknown>,
toolArgs: stubToolArgs,
});
const { error } = resolution as { error: TextToolResult & { toolTelemetry?: Record<string, unknown> } };
expect(error.content[2].text).toBe(`\`\`\`json\n${JSON.stringify(INPUT_SCHEMA)}\n\`\`\``);
expect(error.toolTelemetry).toEqual({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
failureDetail: 'input is required',
actorId: 'actor-id-rag',
});
});
// Byte-identity guard for #937: the validation-failed error keeps the `Input schema:\n`
// prefix before the wrapJsonText fence.
it('embeds the input schema in an unchanged ```json fence for the validation-failed error', async () => {
const ajvValidate = Object.assign(() => false, {
errors: [{ message: 'must have required property query' }],
});
mockActorTool(ajvValidate as never);
const resolution = await resolveAndValidateActor({
actorName: 'apify/rag-web-browser',
input: { wrong: 1 },
toolArgs: stubToolArgs,
});
const { error } = resolution as { error: TextToolResult & { toolTelemetry?: Record<string, unknown> } };
expect(error.content[1].text).toBe(`Input schema:\n\`\`\`json\n${JSON.stringify(INPUT_SCHEMA)}\n\`\`\``);
expect(error.toolTelemetry).toMatchObject({
toolStatus: TOOL_STATUS.SOFT_FAIL,
failureCategory: FAILURE_CATEGORY.INVALID_INPUT,
actorId: 'actor-id-rag',
});
});
});
describe('handleMcpToolCall()', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('forwards signal and timeout into client.callTool options', async () => {
const callTool = vi.fn().mockResolvedValue({
content: [{ type: 'text', text: 'remote ok' }],
isError: false,
});
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue({
callTool,
close: vi.fn().mockResolvedValue(undefined),
} as unknown as Client);
const controller = new AbortController();
const result = await handleMcpToolCall({
baseActorName: 'apify/mcp-demo',
mcpToolName: 'search',
input: { q: 'x' },
isActorMcpServer: true,
mcpServerUrl: 'https://example.invalid/mcp',
apifyToken: 'token',
signal: controller.signal,
});
expect(result?.isError).not.toBe(true);
expect(callTool).toHaveBeenCalledTimes(1);
const options = callTool.mock.calls[0][2] as { signal?: AbortSignal; timeout?: number };
expect(options.signal).toBe(controller.signal);
expect(options.timeout).toBe(EXTERNAL_TOOL_CALL_TIMEOUT_MSEC);
});
it('returns aborted when the signal is already aborted before the remote call', async () => {
const connectSpy = vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(null);
const controller = new AbortController();
controller.abort();
const result = await handleMcpToolCall({
baseActorName: 'apify/mcp-demo',
mcpToolName: 'search',
input: { q: 'x' },
isActorMcpServer: true,
mcpServerUrl: 'https://example.invalid/mcp',
apifyToken: 'token',
signal: controller.signal,
});
expect(result).toEqual({});
expect(connectSpy).not.toHaveBeenCalled();
});
it('returns aborted when the remote call rejects after the signal aborts', async () => {
const controller = new AbortController();
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue({
callTool: vi.fn().mockImplementation(async () => {
controller.abort();
throw new Error('aborted');
}),
close: vi.fn().mockResolvedValue(undefined),
} as unknown as Client);
const result = await handleMcpToolCall({
baseActorName: 'apify/mcp-demo',
mcpToolName: 'search',
input: { q: 'x' },
isActorMcpServer: true,
mcpServerUrl: 'https://example.invalid/mcp',
apifyToken: 'token',
signal: controller.signal,
});
expect(result).toEqual({});
});
});
});