All files / global/search search-tool-run.mjs

100% Statements 33/33
100% Branches 24/24
100% Functions 6/6
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                    11x 3x                                           29x 1x     28x 6x 6x 1x     5x 5x 3x   2x     22x 21x 21x 21x       17x   21x 7x 15x 15x 7x 7x     14x 14x 2x     12x 12x 1x     11x 11x 11x                         1x    
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import { SEARCH_ALL_KEY } from './constants.mjs';
import {
    getFiltersForEntity,
    getSearchEntitiesList,
    performInEntitySearch,
    performSearchAll
} from './search-perform.mjs';
import { collectRelFields, parseRelTarget } from './search-utils.mjs';
 
function collectRelEntityIds(fullSchema, choice, manifest) {
    return collectRelFields(fullSchema, choice, manifest)
        .map(rel => parseRelTarget(rel.relTarget)?.entityId)
        .filter(Boolean);
}
 
/**
 * Executes a datalake search tool action using shared search helpers.
 * Typical order of calls:
 *   1. action=entities — get the list of available entities.
 *   2. action=filters — get the filters for the selected entity (choice).
 *   3. action=run — perform a search with filters and/or text query.
 *
 * @param {'entities'|'filters'|'run'} action
 * @param {{ choice?: string, filters?: Array, searchQuery?: string }} params
 * @param {{
*   manifest: object,
*   fullSchema: object,
*   provider: { getEntityDataMap: (ids: string[]) => Promise<object> },
*   orgCtx?: string
* }} context
* @returns {Promise<unknown>}
*/
export async function executeSearchToolAction(action, params, { manifest, fullSchema, provider, orgCtx }) {
    if (action === 'entities') {
        return getSearchEntitiesList(manifest);
    }
 
    if (action === 'filters') {
        const choice = params.choice;
        if (choice === SEARCH_ALL_KEY) {
            return [];
        }
        const chosenEntitySchema =
            fullSchema?.properties?.[choice] ?? manifest.entities?.[choice]?.schema;
        if (!chosenEntitySchema) {
            throw new Error(`Can't get schema for entity ${choice}.`);
        }
        return getFiltersForEntity(manifest, choice, fullSchema, chosenEntitySchema);
    }
 
    if (action === 'run') {
        const choice = params.choice;
        const filters = Array.isArray(params.filters) ? params.filters : [];
        const queryWords = (params.searchQuery || '')
            .trim()
            .split(/\s+/)
            .filter(Boolean)
            .map(w => w.trim());
 
        if (choice === SEARCH_ALL_KEY) {
            const searchableIds = getSearchEntitiesList(manifest).entities
                .map(e => e.key)
                .filter(k => k !== SEARCH_ALL_KEY);
            const entityDataMap = await provider.getEntityDataMap(searchableIds);
            return performSearchAll(manifest, queryWords, entityDataMap, orgCtx);
        }
 
        const entityDef = manifest.entities?.[choice];
        if (!entityDef) {
            throw new Error(`Chosen entity ${choice} is not found.`);
        }
        const chosenEntitySchema =
            fullSchema?.properties?.[choice] ?? entityDef?.schema;
        if (!chosenEntitySchema) {
            throw new Error(`Can't get schema for entity ${choice}.`);
        }
 
        const relEntityIds = collectRelEntityIds(fullSchema, choice, manifest);
        const entityDataMap = await provider.getEntityDataMap([choice, ...relEntityIds]);
        return performInEntitySearch(
            manifest,
            fullSchema,
            chosenEntitySchema,
            choice,
            filters,
            queryWords,
            entityDataMap[choice],
            entityDataMap,
            orgCtx
        );
    }
 
    throw new Error(`Unknown search action: ${action}`);
}