|
| 1 | +import { |
| 2 | + AggregationResult, |
| 3 | + PropertyAggregation, |
| 4 | + PropertyFilter, |
| 5 | + QueryAgentResponse, |
| 6 | + SearchResult, |
| 7 | + Source, |
| 8 | + Usage, |
| 9 | +} from "./response.js"; |
| 10 | + |
| 11 | +import { |
| 12 | + ApiAggregationResult, |
| 13 | + ApiPropertyAggregation, |
| 14 | + ApiPropertyFilter, |
| 15 | + ApiQueryAgentResponse, |
| 16 | + ApiSearchResult, |
| 17 | + ApiSource, |
| 18 | + ApiUsage, |
| 19 | +} from "./api-response.js"; |
| 20 | + |
| 21 | +export const mapApiResponse = ( |
| 22 | + response: QueryAgentResponse |
| 23 | +): ApiQueryAgentResponse => ({ |
| 24 | + original_query: response.originalQuery, |
| 25 | + collection_names: response.collectionNames, |
| 26 | + searches: mapApiSearches(response.searches), |
| 27 | + aggregations: mapApiAggregations(response.aggregations), |
| 28 | + usage: mapApiUsage(response.usage), |
| 29 | + total_time: response.totalTime, |
| 30 | + aggregation_answer: response.aggregationAnswer, |
| 31 | + has_aggregation_answer: response.hasAggregationAnswer, |
| 32 | + has_search_answer: response.hasSearchAnswer, |
| 33 | + is_partial_answer: response.isPartialAnswer, |
| 34 | + missing_information: response.missingInformation, |
| 35 | + final_answer: response.finalAnswer, |
| 36 | + sources: mapApiSources(response.sources), |
| 37 | +}); |
| 38 | + |
| 39 | +const mapApiSearches = (searches: SearchResult[][]): ApiSearchResult[][] => |
| 40 | + searches.map((searchGroup) => |
| 41 | + searchGroup.map((result) => ({ |
| 42 | + collection: result.collection, |
| 43 | + queries: result.queries, |
| 44 | + filters: result.filters.map(mapApiPropertyFilters), |
| 45 | + filter_operators: result.filterOperators, |
| 46 | + })) |
| 47 | + ); |
| 48 | + |
| 49 | +const mapApiPropertyFilters = ( |
| 50 | + filters: PropertyFilter[] |
| 51 | +): ApiPropertyFilter[] => |
| 52 | + filters.map((filter) => ({ |
| 53 | + property_name: filter.propertyName, |
| 54 | + operator: filter.operator, |
| 55 | + value: filter.value, |
| 56 | + })); |
| 57 | + |
| 58 | +const mapApiAggregations = ( |
| 59 | + aggregations: AggregationResult[][] |
| 60 | +): ApiAggregationResult[][] => |
| 61 | + aggregations.map((aggregationGroup) => |
| 62 | + aggregationGroup.map((result) => ({ |
| 63 | + collection: result.collection, |
| 64 | + search_query: result.searchQuery, |
| 65 | + groupby_property: result.groupbyProperty, |
| 66 | + aggregations: result.aggregations.map(mapApiPropertyAggregation), |
| 67 | + filters: mapApiPropertyFilters(result.filters), |
| 68 | + })) |
| 69 | + ); |
| 70 | + |
| 71 | +const mapApiPropertyAggregation = ( |
| 72 | + aggregation: PropertyAggregation |
| 73 | +): ApiPropertyAggregation => ({ |
| 74 | + property_name: aggregation.propertyName, |
| 75 | + metrics: aggregation.metrics, |
| 76 | + top_occurrences_limit: |
| 77 | + "topOccurrencesLimit" in aggregation |
| 78 | + ? aggregation.topOccurrencesLimit |
| 79 | + : undefined, |
| 80 | +}); |
| 81 | + |
| 82 | +const mapApiUsage = (usage: Usage): ApiUsage => ({ |
| 83 | + requests: usage.requests, |
| 84 | + request_tokens: usage.requestTokens, |
| 85 | + response_tokens: usage.responseTokens, |
| 86 | + total_tokens: usage.totalTokens, |
| 87 | + details: usage.details, |
| 88 | +}); |
| 89 | + |
| 90 | +const mapApiSources = (sources: Source[]): ApiSource[] => |
| 91 | + sources.map((source) => ({ |
| 92 | + object_id: source.objectId, |
| 93 | + collection: source.collection, |
| 94 | + })); |
0 commit comments