Skip to content

Commit 50855a4

Browse files
committed
Add enum length check and truncate if exceeds limit
1 parent e313d9a commit 50855a4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/actors.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Ajv } from 'ajv';
22
import { ApifyClient } from 'apify-client';
33

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';
55
import { log } from './logger.js';
66
import type { ActorDefinitionPruned, ActorDefinitionWithDesc, IActorInputSchema, ISchemaProperties, Tool } from './types.js';
77

@@ -86,7 +86,27 @@ export function shortenProperties(properties: { [key: string]: ISchemaProperties
8686
if (property.description.length > MAX_DESCRIPTION_LENGTH) {
8787
property.description = `${property.description.slice(0, MAX_DESCRIPTION_LENGTH)}...`;
8888
}
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+
}
89108
}
109+
90110
return properties;
91111
}
92112

@@ -298,13 +318,11 @@ export async function getActorsAsTools(actors: string[]): Promise<Tool[]> {
298318
for (const result of results) {
299319
if (result) {
300320
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);
308326
}
309327
try {
310328
const memoryMbytes = result.defaultRunOptions?.memoryMbytes || defaults.maxMemoryMbytes;

src/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const defaults = {
1616
};
1717

1818
export const ACTOR_README_MAX_LENGTH = 5_000;
19+
export const ACTOR_ENUM_MAX_LENGTH = 200;
1920
export const ACTOR_OUTPUT_MAX_CHARS_PER_ITEM = 5_000;
2021
export const ACTOR_OUTPUT_TRUNCATED_MESSAGE = `Output was truncated because it will not fit into context.`
2122
+ `There is no reason to call this tool again!`;

0 commit comments

Comments
 (0)