|
import fetch from 'node-fetch'; |
|
import { D as DEFAULT_SEARCH_INPUT_PARAMS } from './constants-PEBBwOxo.js'; |
|
import { d as private_env } from './shared-server-49TKSBDM.js'; |
|
import dns from 'node:dns'; |
|
|
|
function createSearchInputParams(overrides = {}) { |
|
return { |
|
...DEFAULT_SEARCH_INPUT_PARAMS, |
|
...overrides, |
|
categories: { |
|
...DEFAULT_SEARCH_INPUT_PARAMS.categories, |
|
...overrides.categories || {} |
|
} |
|
}; |
|
} |
|
dns.setDefaultResultOrder("ipv4first"); |
|
class FullTextSearchService { |
|
url = ""; |
|
constructor(url) { |
|
this.url = url; |
|
} |
|
async health() { |
|
try { |
|
const r = await fetch(`${this.url}/health`, { |
|
method: "GET", |
|
headers: { |
|
"Content-Type": "application/json" |
|
} |
|
}); |
|
const data = await r.json(); |
|
if (data.status === "ok") { |
|
return data.status; |
|
} |
|
} catch (error) { |
|
console.log(JSON.parse(JSON.stringify(error))); |
|
} |
|
return "unavailable"; |
|
} |
|
async search(params, abortController) { |
|
const timeoutId = setTimeout(() => { |
|
abortController.abort(); |
|
}, 6e4 * Number(private_env.SEARCH_API_REQUEST_TIMEOUT)); |
|
const startTime = Date.now(); |
|
const data = createSearchInputParams(params); |
|
console.info("Search request sent with parameters:"); |
|
console.info(data); |
|
try { |
|
const r = await fetch(`${this.url}/search`, { |
|
method: "POST", |
|
headers: { |
|
"Content-Type": "application/json" |
|
}, |
|
body: JSON.stringify(data), |
|
signal: abortController.signal |
|
}); |
|
if (!r.ok) { |
|
console.error(`Failed to load search results: ${await r.text()}`); |
|
throw new Error(`Не удалось загрузить результаты поиска. Возможно, поиск ещё не запустился. Попробуйте отправить запрос ещё раз.`); |
|
} |
|
const response = await r.json(); |
|
if (params.find_transaction_maps_by_question || params.find_transaction_maps_by_operation) { |
|
return { |
|
transactionMapResults: response.transaction_maps_results, |
|
results: null, |
|
explanations: response.explanations || null, |
|
consultations: null, |
|
metrics: response.metrics |
|
}; |
|
} else { |
|
return { |
|
transactionMapResults: null, |
|
results: response.results, |
|
explanations: response.explanations || null, |
|
consultations: response.consultations, |
|
query: response.query, |
|
metrics: response.metrics, |
|
llmResponses: response.llm_responses |
|
}; |
|
} |
|
} catch (error) { |
|
if (abortController.signal.aborted || error.name === "AbortError") { |
|
throw new Error(`Сервис поиска не отвечает. Попробуйте изменить и повторить запрос ${JSON.stringify(error)}`); |
|
} |
|
console.log(JSON.parse(JSON.stringify(error))); |
|
throw error; |
|
} finally { |
|
const endTime = Date.now(); |
|
const difference = Math.abs(endTime - startTime) / 1e3; |
|
console.log("Search response time:", difference); |
|
clearTimeout(timeoutId); |
|
} |
|
} |
|
} |
|
|
|
export { FullTextSearchService as F }; |
|
|
|
|