@@ -9,6 +9,24 @@ import log from '@apify/log';
99
1010import type { Input , ToolSelector } from './types.js' ;
1111
12+ // Helpers
13+ // Normalize booleans that may arrive as strings or be undefined.
14+ function toBoolean ( value : unknown , defaultValue : boolean ) : boolean {
15+ if ( value === undefined ) return defaultValue ;
16+ if ( typeof value === 'boolean' ) return value ;
17+ if ( typeof value === 'string' ) return value . toLowerCase ( ) === 'true' ;
18+ return defaultValue ;
19+ }
20+
21+ // Normalize lists from comma-separated strings or arrays.
22+ function normalizeList ( value : string | string [ ] | undefined ) : string [ ] | undefined {
23+ if ( value === undefined ) return undefined ;
24+ if ( Array . isArray ( value ) ) return value . map ( ( s ) => String ( s ) . trim ( ) ) . filter ( ( s ) => s !== '' ) ;
25+ const trimmed = String ( value ) . trim ( ) ;
26+ if ( trimmed === '' ) return [ ] ;
27+ return trimmed . split ( ',' ) . map ( ( s ) => s . trim ( ) ) . filter ( ( s ) => s !== '' ) ;
28+ }
29+
1230/**
1331 * Normalize user-provided input into a canonical `Input`.
1432 *
@@ -21,53 +39,40 @@ import type { Input, ToolSelector } from './types.js';
2139 * - `undefined` → use defaults; `[]` → explicitly none.
2240 */
2341export function processInput ( originalInput : Partial < Input > ) : Input {
24- const input = { ...originalInput } as Input ;
25-
26- // Helpers
27- // Normalize booleans that may arrive as strings or be undefined.
28- const toBoolean = ( value : unknown , defaultValue : boolean ) : boolean => {
29- if ( value === undefined ) return defaultValue ;
30- if ( typeof value === 'boolean' ) return value ;
31- if ( typeof value === 'string' ) return value . toLowerCase ( ) === 'true' ;
32- return defaultValue ;
33- } ;
34- // Normalize lists from comma-separated strings or arrays.
35- const normalizeList = ( value : string | string [ ] | undefined ) : string [ ] | undefined => {
36- if ( value === undefined ) return undefined ;
37- if ( Array . isArray ( value ) ) return value . map ( ( s ) => String ( s ) . trim ( ) ) . filter ( ( s ) => s !== '' ) ;
38- const trimmed = String ( value ) . trim ( ) ;
39- if ( trimmed === '' ) return [ ] ;
40- return trimmed . split ( ',' ) . map ( ( s ) => s . trim ( ) ) . filter ( ( s ) => s !== '' ) ;
41- } ;
42-
4342 // Normalize actors (strings and arrays) to a clean array or undefined
44- input . actors = normalizeList ( input . actors ) as unknown as string [ ] | undefined ;
43+ const actors = normalizeList ( originalInput . actors ) as unknown as string [ ] | undefined ;
4544
4645 // Map deprecated flag to the new one and normalize both to boolean.
47- if ( input . enableAddingActors === undefined && input . enableActorAutoLoading !== undefined ) {
46+ let enableAddingActors : boolean ;
47+ if ( originalInput . enableAddingActors === undefined && originalInput . enableActorAutoLoading !== undefined ) {
4848 log . warning ( 'enableActorAutoLoading is deprecated, use enableAddingActors instead' ) ;
49- input . enableAddingActors = toBoolean ( input . enableActorAutoLoading , false ) ;
49+ enableAddingActors = toBoolean ( originalInput . enableActorAutoLoading , false ) ;
5050 } else {
51- input . enableAddingActors = toBoolean ( input . enableAddingActors , false ) ;
51+ enableAddingActors = toBoolean ( originalInput . enableAddingActors , false ) ;
5252 }
5353
5454 // Normalize tools (strings/arrays) to a clean array or undefined
55- input . tools = normalizeList ( input . tools as string | string [ ] | undefined ) as unknown as ToolSelector [ ] | undefined ;
55+ let tools = normalizeList ( originalInput . tools as string | string [ ] | undefined ) as unknown as ToolSelector [ ] | undefined ;
5656
5757 // Merge actors into tools. If tools undefined → tools = actors, then remove actors;
5858 // otherwise append actors to tools.
5959 // NOTE (future): Actor names contain '/', unlike internal tool names or categories. We could use that to differentiate between the two.
60- if ( Array . isArray ( input . actors ) && input . actors . length > 0 ) {
61- if ( input . tools === undefined ) {
62- input . tools = [ ...input . actors ] as ToolSelector [ ] ;
63- // Treat as if only tools were specified; clear actors to avoid duplicate semantics
64- delete ( input as Record < string , unknown > ) . actors ;
60+ if ( Array . isArray ( actors ) && actors . length > 0 ) {
61+ if ( tools === undefined ) {
62+ tools = [ ...actors ] as ToolSelector [ ] ;
6563 } else {
66- const currentTools : ToolSelector [ ] = Array . isArray ( input . tools )
67- ? input . tools
68- : [ input . tools as ToolSelector ] ;
69- input . tools = [ ...currentTools , ... input . actors ] as ToolSelector [ ] ;
64+ const currentTools : ToolSelector [ ] = Array . isArray ( tools )
65+ ? tools
66+ : [ tools as ToolSelector ] ;
67+ tools = [ ...currentTools , ...actors ] as ToolSelector [ ] ;
7068 }
7169 }
72- return input ;
70+
71+ // Return a new object with all properties explicitly defined
72+ return {
73+ ...originalInput ,
74+ actors : Array . isArray ( actors ) && actors . length > 0 && tools !== undefined ? undefined : actors ,
75+ enableAddingActors,
76+ tools,
77+ } ;
7378}
0 commit comments