|
1 | 1 | import { Ajv } from 'ajv'; |
2 | 2 | import { ApifyClient } from 'apify-client'; |
3 | 3 |
|
4 | | -import { ACTOR_ADDITIONAL_INSTRUCTIONS, defaults, MAX_DESCRIPTION_LENGTH, ACTOR_README_MAX_LENGTH } from './const.js'; |
| 4 | +import { ACTOR_ADDITIONAL_INSTRUCTIONS, defaults, MAX_DESCRIPTION_LENGTH, ACTOR_README_MAX_LENGTH, ACTOR_ENUM_MAX_LENGTH } from './const.js'; |
5 | 5 | import { log } from './logger.js'; |
6 | 6 | import type { ActorDefinitionPruned, ActorDefinitionWithDesc, IActorInputSchema, ISchemaProperties, Tool } from './types.js'; |
7 | 7 |
|
@@ -86,7 +86,27 @@ export function shortenProperties(properties: { [key: string]: ISchemaProperties |
86 | 86 | if (property.description.length > MAX_DESCRIPTION_LENGTH) { |
87 | 87 | property.description = `${property.description.slice(0, MAX_DESCRIPTION_LENGTH)}...`; |
88 | 88 | } |
| 89 | + |
| 90 | + if (property.enum && property.enum?.length > 0) { |
| 91 | + let charCount = 0; |
| 92 | + let maxEnumCount = 0; |
| 93 | + for (let i = 0; i < property.enum.length; i++) { |
| 94 | + charCount += property.enum[i].length; |
| 95 | + if (charCount > ACTOR_ENUM_MAX_LENGTH) { |
| 96 | + maxEnumCount = i; |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (maxEnumCount > 0) { |
| 102 | + property.enum = property.enum.slice(0, maxEnumCount); |
| 103 | + } else { |
| 104 | + // If the enum is too long, we remove it |
| 105 | + property.enum = undefined; |
| 106 | + } |
| 107 | + } |
89 | 108 | } |
| 109 | + |
90 | 110 | return properties; |
91 | 111 | } |
92 | 112 |
|
@@ -298,13 +318,11 @@ export async function getActorsAsTools(actors: string[]): Promise<Tool[]> { |
298 | 318 | for (const result of results) { |
299 | 319 | if (result) { |
300 | 320 | if (result.input && 'properties' in result.input && result.input) { |
301 | | - // TODO let us also refactor this to use properties only |
302 | | - // We should be able to comment/uncomment any of the following lines and it should work |
303 | | - const propertiesMarkedAsRequired = markInputPropertiesAsRequired(result.input); |
304 | | - const propertiesObjectsBuilt = buildNestedProperties(propertiesMarkedAsRequired); |
305 | | - const propertiesFiltered = filterSchemaProperties(propertiesObjectsBuilt); |
306 | | - const propertiesShortened = shortenProperties(propertiesFiltered); |
307 | | - result.input.properties = addEnumsToDescriptionsWithExamples(propertiesShortened); |
| 321 | + result.input.properties = markInputPropertiesAsRequired(result.input); |
| 322 | + result.input.properties = buildNestedProperties(result.input.properties); |
| 323 | + result.input.properties = filterSchemaProperties(result.input.properties); |
| 324 | + result.input.properties = shortenProperties(result.input.properties); |
| 325 | + result.input.properties = addEnumsToDescriptionsWithExamples(result.input.properties); |
308 | 326 | } |
309 | 327 | try { |
310 | 328 | const memoryMbytes = result.defaultRunOptions?.memoryMbytes || defaults.maxMemoryMbytes; |
|
0 commit comments