This was done with GPT-4
You are an expert AI programmer that helps me program.
I would like you create a new CommandHandler called ResearchCommandHandler, in this command handler you will have a function called searchGoogle which will take args: string[], with only one element which is the search term. For example "How was Nicolas Tesla". It should then search google and return the result.
Please adhere to a structure similar to this class
import { CommandBus } from "../infra/CommandBus" import { CommandResult } from "../infra/Commands" import { DockerManager } from "../utils/DockerManager"
export class DockerCommandHandler { constructor(private dockerManager: DockerManager) {}
async runPythonScript(args: string[]): Promise<CommandResult> { const res = await this.dockerManager.containerExec(["python", args[0]]) return { ok: true, message: res} }
registerTo(commandBus: CommandBus) { commandBus.registerCommand("RUN_PYTHON", async (args) => await this.runPythonScript(args)) } }
Make any new classes that you might need, if you need api keys assume you have them in an options object, but do make an interface showing what you need.
To create a new CommandHandler called ResearchCommandHandler, you can follow a similar structure to the provided DockerCommandHandler class. Here's an example implementation:
import { CommandBus } from "../infra/CommandBus";
import { CommandResult } from "../infra/Commands";
import { GoogleSearch } from "../utils/GoogleSearch";
export interface ResearchCommandHandlerOptions {
googleApiKey: string;
googleSearchEngineId: string;
}
export class ResearchCommandHandler {
private googleSearch: GoogleSearch;
constructor(options: ResearchCommandHandlerOptions) {
this.googleSearch = new GoogleSearch(options.googleApiKey, options.googleSearchEngineId);
}
async searchGoogle(args: string[]): Promise<CommandResult> {
const searchTerm = args[0];
const res = await this.googleSearch.search(searchTerm);
return { ok: true, message: res };
}
registerTo(commandBus: CommandBus) {
commandBus.registerCommand("SEARCH_GOOGLE", async (args) => await this.searchGoogle(args));
}
}
Now, create a new file GoogleSearch.ts in the utils folder with the following content:
import axios from "axios";
export class GoogleSearch {
private apiKey: string;
private searchEngineId: string;
constructor(apiKey: string, searchEngineId: string) {
this.apiKey = apiKey;
this.searchEngineId = searchEngineId;
}
async search(query: string): Promise<string> {
const url = `https://www.googleapis.com/customsearch/v1?key=${this.apiKey}&cx=${this.searchEngineId}&q=${encodeURIComponent(query)}`;
const response = await axios.get(url);
const items = response.data.items;
if (items && items.length > 0) {
const topResult = items[0];
return `${topResult.title}: ${topResult.link}`;
} else {
return "No results found";
}
}
}
This implementation assumes you are using the Google Custom Search JSON API to search Google. The GoogleSearch class handles the search operation using the provided API key and search engine ID, which are stored in the ResearchCommandHandlerOptions interface.
The ResearchCommandHandler class has a searchGoogle function that takes a single search term as an argument and returns the result. It also has a registerTo method, which registers the searchGoogle command with the CommandBus.