
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="9d8d24d6-b5e0-53dd-b442-73c12caa8e4e")}catch(e){}}();
import { getActorMCPServerPath, getActorMCPServerURL } from '../mcp/actors.js';
import { mcpServerCache } from '../state.js';
import { getActorDefinition } from '../tools/build.js';
import { getValuesByDotKeys } from './generic.js';
/**
 * Resolve and cache the MCP server URL for the given Actor.
 * - Returns a string URL when the Actor exposes an MCP server
 * - Returns false when the Actor is not an MCP server
 * Uses a TTL LRU cache to avoid repeated API calls.
 */
export async function getActorMcpUrlCached(actorIdOrName, apifyClient) {
    const cached = mcpServerCache.get(actorIdOrName);
    if (cached !== null && cached !== undefined) {
        return cached;
    }
    try {
        const actorDefinitionWithInfo = await getActorDefinition(actorIdOrName, apifyClient);
        const definition = actorDefinitionWithInfo === null || actorDefinitionWithInfo === void 0 ? void 0 : actorDefinitionWithInfo.definition;
        const mcpPath = definition && getActorMCPServerPath(definition);
        if (mcpPath) {
            const url = await getActorMCPServerURL(definition.id, mcpPath);
            mcpServerCache.set(actorIdOrName, url);
            return url;
        }
        mcpServerCache.set(actorIdOrName, false);
        return false;
    }
    catch (error) {
        // Check if it's a "not found" error (404 or 400 status codes)
        const isNotFound = typeof error === 'object'
            && error !== null
            && 'statusCode' in error
            && (error.statusCode === 404 || error.statusCode === 400);
        if (isNotFound) {
            // Actor doesn't exist - cache false and return false
            mcpServerCache.set(actorIdOrName, false);
            return false;
        }
        // Real server error - don't cache, let it propagate
        throw error;
    }
}
/**
 * Returns an array of all field names mentioned in the display.properties
 * of all views in the given ActorDefinitionStorage object.
 */
export function getActorDefinitionStorageFieldNames(storage) {
    const fieldSet = new Set();
    if ('views' in storage && typeof storage.views === 'object' && storage.views !== null) {
        for (const view of Object.values(storage.views)) {
            // Collect from display.properties
            if (view.display && view.display.properties) {
                Object.keys(view.display.properties).forEach((field) => fieldSet.add(field));
            }
            // Collect from transformation.fields
            if (view.transformation && Array.isArray(view.transformation.fields)) {
                view.transformation.fields.forEach((field) => {
                    if (typeof field === 'string')
                        fieldSet.add(field);
                });
            }
        }
    }
    return Array.from(fieldSet);
}
/**
 * Ensures the Actor output items are within the character limit.
 *
 * First checks if all items fit into the limit, then tries only the important fields and as a last resort
 * starts removing items until within the limit. In worst scenario return empty array.
 *
 * This is primarily used to ensure the tool output does not exceed the LLM context length or tool output limit.
 */
export function ensureOutputWithinCharLimit(items, importantFields, charLimit) {
    // Check if all items fit into the limit
    const allItemsString = JSON.stringify(items);
    if (allItemsString.length <= charLimit) {
        return items;
    }
    /**
     * Items used for the final fallback - removing items until within the limit.
     * If important fields are defined, use only those fields for that fallback step.
     */
    let sourceItems = items;
    // Try keeping only the important fields
    if (importantFields.length > 0) {
        const importantItems = items.map((item) => getValuesByDotKeys(item, importantFields));
        const importantItemsString = JSON.stringify(importantItems);
        if (importantItemsString.length <= charLimit) {
            return importantItems;
        }
        sourceItems = importantItems;
    }
    // Start removing items until within the limit
    const result = [];
    for (const item of sourceItems) {
        if (JSON.stringify(result.concat(item)).length > charLimit) {
            break;
        }
        result.push(item);
    }
    return result;
}
//# sourceMappingURL=actor.js.map
//# debugId=9d8d24d6-b5e0-53dd-b442-73c12caa8e4e
