Spaces:
Build error
Build error
| ; | |
| var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | |
| var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | |
| if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | |
| else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | |
| return c > 3 && r && Object.defineProperty(target, key, r), r; | |
| }; | |
| var __metadata = (this && this.__metadata) || function (k, v) { | |
| if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | |
| }; | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| exports.BraveSearchExplicitOperatorsDto = exports.BraveSearchService = void 0; | |
| const civkit_1 = require("civkit"); | |
| const tsyringe_1 = require("tsyringe"); | |
| const logger_1 = require("./logger"); | |
| const secrets_1 = require("../shared/services/secrets"); | |
| const brave_search_1 = require("../shared/3rd-party/brave-search"); | |
| const geoip_1 = require("./geoip"); | |
| const async_context_1 = require("./async-context"); | |
| const blackhole_detector_1 = require("./blackhole-detector"); | |
| let BraveSearchService = class BraveSearchService extends civkit_1.AsyncService { | |
| constructor(globalLogger, secretExposer, geoipControl, threadLocal, blackHoleDetector) { | |
| super(...arguments); | |
| this.globalLogger = globalLogger; | |
| this.secretExposer = secretExposer; | |
| this.geoipControl = geoipControl; | |
| this.threadLocal = threadLocal; | |
| this.blackHoleDetector = blackHoleDetector; | |
| this.logger = this.globalLogger.child({ service: this.constructor.name }); | |
| } | |
| async init() { | |
| await this.dependencyReady(); | |
| this.emit('ready'); | |
| this.braveSearchHTTP = new brave_search_1.BraveSearchHTTP(this.secretExposer.BRAVE_SEARCH_API_KEY); | |
| } | |
| async webSearch(query) { | |
| const ip = this.threadLocal.get('ip'); | |
| const extraHeaders = {}; | |
| if (ip) { | |
| const geoip = await this.geoipControl.lookupCity(ip, geoip_1.GEOIP_SUPPORTED_LANGUAGES.EN); | |
| if (geoip?.city) { | |
| extraHeaders['X-Loc-City'] = encodeURIComponent(geoip.city); | |
| } | |
| if (geoip?.country) { | |
| extraHeaders['X-Loc-Country'] = geoip.country.code; | |
| } | |
| if (geoip?.timezone) { | |
| extraHeaders['X-Loc-Timezone'] = geoip.timezone; | |
| } | |
| if (geoip?.coordinates) { | |
| extraHeaders['X-Loc-Lat'] = `${geoip.coordinates[0]}`; | |
| extraHeaders['X-Loc-Long'] = `${geoip.coordinates[1]}`; | |
| } | |
| if (geoip?.subdivisions?.length) { | |
| extraHeaders['X-Loc-State'] = encodeURIComponent(`${geoip.subdivisions[0].code}`); | |
| extraHeaders['X-Loc-State-Name'] = encodeURIComponent(`${geoip.subdivisions[0].name}`); | |
| } | |
| } | |
| if (this.threadLocal.get('userAgent')) { | |
| extraHeaders['User-Agent'] = this.threadLocal.get('userAgent'); | |
| } | |
| const encoded = { ...query }; | |
| if (encoded.q) { | |
| encoded.q = (Buffer.from(encoded.q).toString('ascii') === encoded.q) ? encoded.q : encodeURIComponent(encoded.q); | |
| } | |
| let maxTries = 11; | |
| while (maxTries--) { | |
| try { | |
| const r = await this.braveSearchHTTP.webSearch(encoded, { headers: extraHeaders }); | |
| this.blackHoleDetector.itWorked(); | |
| return r.parsed; | |
| } | |
| catch (err) { | |
| this.logger.error(`Web search failed: ${err?.message}`, { err: (0, civkit_1.marshalErrorLike)(err) }); | |
| if (err?.status === 429) { | |
| await (0, civkit_1.delay)(500 + 1000 * Math.random()); | |
| continue; | |
| } | |
| throw new civkit_1.DownstreamServiceFailureError({ message: `Search failed` }); | |
| } | |
| } | |
| throw new civkit_1.DownstreamServiceFailureError({ message: `Search failed` }); | |
| } | |
| }; | |
| exports.BraveSearchService = BraveSearchService; | |
| exports.BraveSearchService = BraveSearchService = __decorate([ | |
| (0, tsyringe_1.singleton)(), | |
| __metadata("design:paramtypes", [logger_1.GlobalLogger, | |
| secrets_1.SecretExposer, | |
| geoip_1.GeoIPService, | |
| async_context_1.AsyncLocalContext, | |
| blackhole_detector_1.BlackHoleDetector]) | |
| ], BraveSearchService); | |
| class BraveSearchExplicitOperatorsDto extends civkit_1.AutoCastable { | |
| addTo(searchTerm) { | |
| const chunks = []; | |
| for (const [key, value] of Object.entries(this)) { | |
| if (value) { | |
| const values = Array.isArray(value) ? value : [value]; | |
| const textValue = values.map((v) => `${key}:${v}`).join(' OR '); | |
| if (textValue) { | |
| chunks.push(textValue); | |
| } | |
| } | |
| } | |
| const opPart = chunks.length > 1 ? chunks.map((x) => `(${x})`).join(' AND ') : chunks; | |
| if (opPart.length) { | |
| return [searchTerm, opPart].join(' '); | |
| } | |
| return searchTerm; | |
| } | |
| static from(input) { | |
| const instance = super.from(input); | |
| const ctx = Reflect.get(input, civkit_1.RPC_CALL_ENVIRONMENT); | |
| const params = ['ext', 'filetype', 'inbody', 'intitle', 'inpage', 'lang', 'loc', 'site']; | |
| for (const p of params) { | |
| const customValue = ctx?.req.get(`x-${p}`) || ctx?.req.get(`${p}`); | |
| if (!customValue) { | |
| continue; | |
| } | |
| const filtered = customValue.split(', ').filter(Boolean); | |
| if (filtered.length) { | |
| Reflect.set(instance, p, filtered); | |
| } | |
| } | |
| return instance; | |
| } | |
| } | |
| exports.BraveSearchExplicitOperatorsDto = BraveSearchExplicitOperatorsDto; | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages with a specific file extension. Example: to find the Honda GX120 Owner’s manual in PDF, type “Honda GX120 ownners manual ext:pdf”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "ext", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages created in the specified file type. Example: to find a web page created in PDF format about the evaluation of age-related cognitive changes, type “evaluation of age cognitive changes filetype:pdf”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "filetype", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages containing the specified term in the body of the page. Example: to find information about the Nvidia GeForce GTX 1080 Ti, making sure the page contains the keywords “founders edition” in the body, type “nvidia 1080 ti inbody:“founders edition””.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "inbody", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns webpages containing the specified term in the title of the page. Example: to find pages about SEO conferences making sure the results contain 2023 in the title, type “seo conference intitle:2023”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "intitle", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns webpages containing the specified term either in the title or in the body of the page. Example: to find pages about the 2024 Oscars containing the keywords “best costume design” in the page, type “oscars 2024 inpage:“best costume design””.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "inpage", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages written in the specified language. The language code must be in the ISO 639-1 two-letter code format. Example: to find information on visas only in Spanish, type “visas lang:es”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "lang", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages written in the specified language. The language code must be in the ISO 639-1 two-letter code format. Example: to find information on visas only in Spanish, type “visas lang:es”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "loc", void 0); | |
| __decorate([ | |
| (0, civkit_1.Prop)({ | |
| arrayOf: String, | |
| desc: `Returns web pages coming only from a specific web site. Example: to find information about Goggles only on Brave pages, type “goggles site:brave.com”.` | |
| }), | |
| __metadata("design:type", Object) | |
| ], BraveSearchExplicitOperatorsDto.prototype, "site", void 0); | |
| //# sourceMappingURL=brave-search.js.map |