UNPKG

@plastichub/osr-ai-tools

Version:

CLI and library for LLM tools

132 lines (131 loc) 6.42 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tools = void 0; const path = __importStar(require("path")); const primitives_1 = require("@plastichub/core/primitives"); const write_1 = require("@plastichub/fs/write"); const osr_commons_1 = require("@plastichub/osr-commons"); const __1 = require("../.."); const tools = (target, options) => { const logger = (0, __1.toolLogger)(path.parse(__filename).name, options); return [ { type: 'function', function: { name: 'google', description: 'Searches Google for the given query', parameters: { type: 'object', properties: { query: { type: 'string' } }, required: ['query'] }, function: async (params) => { params = await options.collector.onToolBefore('search', 'google', params) || params; const { query } = params; const config = (0, osr_commons_1.CONFIG_DEFAULT)(); let apiKey = config?.google?.api_key; let cse = config?.google?.cse; if (!config || !apiKey || !cse) { logger.debug("Config not found in $HOME/.osr/config.json. " + "Optionally, export OSR_CONFIG with the path to the configuration file " + ""); return undefined; } const res = await fetch(`https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${cse}&q=${encodeURIComponent(query)}`); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const data = await res.json(); let results = data.items?.map((item) => ({ title: item.title, link: item.link, snippet: item.snippet, ...item })) ?? []; (0, write_1.sync)(path.join(target, 'tmp', 'google_last.json'), results); results = await options.collector.onToolAfter('search', 'google', params, results) || results; return JSON.stringify(results); }, parse: JSON.parse } }, { type: 'function', function: { name: 'serpapi', description: 'Searches Serpapi (finds locations (engine:google_local), places on the map (engine:google_maps) ) for the given query', parameters: { type: 'object', properties: { query: { type: 'string' }, engine: { type: 'string', default: 'google' }, }, required: ['query'] }, function: async (params) => { params = await options.collector.onToolBefore('search', 'serpapi', params) || params; const { query, engine } = params; const config = (0, osr_commons_1.CONFIG_DEFAULT)(); let apiKey = config?.serpapi?.key || config?.serpapi?.api_key; if (!config || !apiKey) { logger.debug("Config not found in $HOME/.osr/config.json. " + "Optionally, export OSR_CONFIG with the path to the configuration file " + ""); return undefined; } const url = `https://serpapi.com/search?api_key=${apiKey}&engine=${engine || 'google'}&q=${encodeURIComponent(query)}&google_domain=google.com`; const res = await fetch(url); logger.debug(`Searching ${url}`); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const data = await res.json(); let items = data.organic_results || data.local_results || data.place_results || data.places || data.maps_results; if (items && !(0, primitives_1.isArray)(items)) { items = [items]; } if (!items) { logger.debug(`No results found for ${query}`); return false; } let results = items.map((item) => ({ title: item.title, link: item.link, snippet: item.snippet, ...item })) ?? []; (0, write_1.sync)(path.join(target, 'tmp', 'serp_last.json'), results); results = await options.collector.onToolBefore('search', 'serpapi', params) || results; return JSON.stringify(results); }, parse: JSON.parse } } ]; }; exports.tools = tools;