|
1 | 1 | import type { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
2 | | -import type { ValidateFunction } from 'ajv'; |
3 | 2 | import { Ajv } from 'ajv'; |
4 | 3 | import type { ActorCallOptions, ActorRun, Dataset, PaginatedList } from 'apify-client'; |
5 | 4 | import { z } from 'zod'; |
@@ -27,32 +26,14 @@ import { |
27 | 26 | addEnumsToDescriptionsWithExamples, |
28 | 27 | buildNestedProperties, |
29 | 28 | filterSchemaProperties, |
| 29 | + fixedAjvCompile, |
30 | 30 | getToolSchemaID, |
31 | 31 | markInputPropertiesAsRequired, |
32 | 32 | shortenProperties, |
33 | 33 | } from './utils.js'; |
34 | 34 |
|
35 | 35 | const ajv = new Ajv({ coerceTypes: 'array', strict: false }); |
36 | 36 |
|
37 | | -// source https://github.com/ajv-validator/ajv/issues/1413#issuecomment-867064234 |
38 | | -function fixedCompile(schema: object): ValidateFunction<unknown> { |
39 | | - const validate = ajv.compile(schema); |
40 | | - ajv.removeSchema(schema); |
41 | | - |
42 | | - // Force reset values that aren't reset with removeSchema |
43 | | - /* eslint-disable no-underscore-dangle */ |
44 | | - /* eslint-disable @typescript-eslint/no-explicit-any */ |
45 | | - (ajv.scope as any)._values.schema!.delete(schema); |
46 | | - (ajv.scope as any)._values.validate!.delete(validate); |
47 | | - const schemaIdx = (ajv.scope as any)._scope.schema.indexOf(schema); |
48 | | - const validateIdx = (ajv.scope as any)._scope.validate.indexOf(validate); |
49 | | - if (schemaIdx !== -1) (ajv.scope as any)._scope.schema.splice(schemaIdx, 1); |
50 | | - if (validateIdx !== -1) (ajv.scope as any)._scope.validate.splice(validateIdx, 1); |
51 | | - /* eslint-enable @typescript-eslint/no-explicit-any */ |
52 | | - /* eslint-enable no-underscore-dangle */ |
53 | | - return validate; |
54 | | -} |
55 | | - |
56 | 37 | // Define a named return type for callActorGetDataset |
57 | 38 | export type CallActorGetDatasetResult = { |
58 | 39 | actorRun: ActorRun; |
@@ -182,7 +163,7 @@ export async function getNormalActorsAsTools( |
182 | 163 | actorFullName: result.actorFullName, |
183 | 164 | description: `${result.description} Instructions: ${ACTOR_ADDITIONAL_INSTRUCTIONS}`, |
184 | 165 | inputSchema: result.input || {}, |
185 | | - ajvValidate: fixedCompile(result.input || {}), |
| 166 | + ajvValidate: fixedAjvCompile(ajv, result.input || {}), |
186 | 167 | memoryMbytes: memoryMbytes > ACTOR_MAX_MEMORY_MBYTES ? ACTOR_MAX_MEMORY_MBYTES : memoryMbytes, |
187 | 168 | }, |
188 | 169 | }; |
|
0 commit comments