I know this is redundant and might not be useful to many, but I needed to build a custom search tool for an agent I'm working on.
I set this up to test the functionality and figured someone might find it useful.
/*
# Google Search
Example of leveraging Google's Custom Search Engine API to search the web
*/
// Name: Google Search
// Description: Leverage Google's Custom Search Engine API to search the web
// Author: Josh Mabry
// Twitter: @AI_Citizen
import "@johnlindquist/kit";
let GOOGLE_API_KEY = await env("GOOGLE_API_KEY", {
shortcuts: [
{
name: "Google API Key",
key: `${cmd}+o`,
bar: "right",
onPress: () => {
open("https://developers.google.com/custom-search/v1/introduction");
},
},
],
ignoreBlur: true,
secret: true,
height: PROMPT.HEIGHT.INPUT_ONLY,
});
let GOOGLE_CSE_KEY = await env("GOOGLE_CSE_KEY", {
shortcuts: [
{
name: "Google Custom Search Engine Key",
key: `${cmd}+o`,
bar: "right",
onPress: () => {
open("https://programmablesearchengine.google.com/");
},
},
],
ignoreBlur: true,
secret: true,
height: PROMPT.HEIGHT.INPUT_ONLY,
});
let query = await arg(
{
placeholder: "Search Query",
strict: false,
},
[
{
name: "Send a search query to Google",
info: "always",
},
]
);
let search = (q) =>
`https://www.googleapis.com/customsearch/v1?key=${GOOGLE_API_KEY}&cx=${GOOGLE_CSE_KEY}&q=${q}&sort=date`;
let response = await get(search(query));
let items = response?.data?.items;
if (items) {
let choices = items.map((item) => ({
name: item.title,
value: item.link,
}));
let link = await arg("Choose a link to view", choices);
open(link);
}