@@ -4,8 +4,8 @@ import { z } from 'zod';
44import zodToJsonSchema from 'zod-to-json-schema' ;
55
66import { ApifyClient } from '../apify-client.js' ;
7- import { HelperTools } from '../const.js' ;
8- import type { ActorStorePruned , ApifyStorePricingModel , HelperTool , PricingInfo , ToolEntry } from '../types.js' ;
7+ import { ACTOR_SEARCH_ABOVE_LIMIT , HelperTools } from '../const.js' ;
8+ import type { ActorPricingModel , ActorStorePruned , HelperTool , PricingInfo , ToolEntry } from '../types.js' ;
99
1010function pruneActorStoreInfo ( response : ActorStoreList ) : ActorStorePruned {
1111 const stats = response . stats || { } ;
@@ -38,10 +38,9 @@ export async function searchActorsByKeywords(
3838 apifyToken : string ,
3939 limit : number | undefined = undefined ,
4040 offset : number | undefined = undefined ,
41- pricingModel : ApifyStorePricingModel | undefined = undefined ,
4241) : Promise < ActorStorePruned [ ] > {
4342 const client = new ApifyClient ( { token : apifyToken } ) ;
44- const results = await client . store ( ) . list ( { search, limit, offset, pricingModel } ) ;
43+ const results = await client . store ( ) . list ( { search, limit, offset } ) ;
4544 return results . items . map ( ( x ) => pruneActorStoreInfo ( x ) ) ;
4645}
4746
@@ -70,7 +69,9 @@ export const searchActorsArgsSchema = z.object({
7069} ) ;
7170
7271/**
73- * Filters out actors with the 'FLAT_PRICE_PER_MONTH' pricing model (rental actors).
72+ * Filters out actors with the 'FLAT_PRICE_PER_MONTH' pricing model (rental actors)..
73+ *
74+ * Returns new array of Actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model.
7475 *
7576 * @param actors - Array of ActorStorePruned objects to filter.
7677 * @returns Array of actors excluding those with 'FLAT_PRICE_PER_MONTH' pricing model.
@@ -80,48 +81,7 @@ function filterRentalActors(
8081) : ActorStorePruned [ ] {
8182 // Store list API does not support filtering by two pricing models at once,
8283 // so we filter the results manually after fetching them.
83- return actors . filter ( ( actor ) => ( actor . currentPricingInfo . pricingModel as ApifyStorePricingModel ) !== 'FLAT_PRICE_PER_MONTH' ) ;
84- }
85-
86- /**
87- * Fallback function to fetch actors if no rental actors are found.
88- * Fetches both free and pay-per-result actors and merges them in a zig-zag order.
89- *
90- * @param search - Search keywords for actors.
91- * @param apifyToken - Apify API token.
92- * @param limit - Maximum number of actors to return.
93- * @param offset - Number of actors to skip from the start.
94- * @returns Array of ActorStorePruned objects, alternating between free and pay-per-result actors.
95- */
96- async function getFallbackActors (
97- search : string ,
98- apifyToken : string ,
99- limit : number | undefined ,
100- offset : number | undefined ,
101- ) : Promise < ActorStorePruned [ ] > {
102- const freeActors = await searchActorsByKeywords (
103- search ,
104- apifyToken ,
105- limit ,
106- offset ,
107- 'FREE' ,
108- ) ;
109- const payPerResultActors = await searchActorsByKeywords (
110- search ,
111- apifyToken ,
112- limit ,
113- offset ,
114- 'PRICE_PER_DATASET_ITEM' ,
115- ) ;
116- const allActors : ActorStorePruned [ ] = [ ] ;
117- // Push Actors in zig-zag order to ensure that we return all Actors
118- // in relevant order.
119- const maxLength = Math . max ( freeActors ?. length || 0 , payPerResultActors ?. length || 0 ) ;
120- for ( let i = 0 ; i < maxLength ; i ++ ) {
121- if ( freeActors && freeActors [ i ] ) allActors . push ( freeActors [ i ] ) ;
122- if ( payPerResultActors && payPerResultActors [ i ] ) allActors . push ( payPerResultActors [ i ] ) ;
123- }
124- return allActors ;
84+ return actors . filter ( ( actor ) => ( actor . currentPricingInfo . pricingModel as ActorPricingModel ) !== 'FLAT_PRICE_PER_MONTH' ) ;
12585}
12686
12787/**
@@ -147,21 +107,10 @@ export const searchActors: ToolEntry = {
147107 let actors = await searchActorsByKeywords (
148108 parsed . search ,
149109 apifyToken ,
150- parsed . limit ,
110+ parsed . limit + ACTOR_SEARCH_ABOVE_LIMIT ,
151111 parsed . offset ,
152112 ) ;
153- actors = filterRentalActors ( actors || [ ] ) ;
154- if ( actors . length === 0 ) {
155- // If no non-rental actors found, search for free and pay-per-result actors directly
156- // and sort them by total stars.
157- // This is a fallback to ensure we return some results.
158- actors = await getFallbackActors (
159- parsed . search ,
160- apifyToken ,
161- parsed . limit ,
162- parsed . offset ,
163- ) ;
164- }
113+ actors = filterRentalActors ( actors || [ ] ) . slice ( 0 , parsed . limit ) ;
165114
166115 return { content : actors ?. map ( ( item ) => ( { type : 'text' , text : JSON . stringify ( item ) } ) ) } ;
167116 } ,
0 commit comments