Elffuss BraidHub

kikocis/continue public

main · 2.0 KiBRawPermalink
1import {
2 ContextItem,
3 ContextProviderDescription,
4 ContextProviderExtras,
5} from "../../index.js";
6import { BaseContextProvider } from "../index.js";
7
8class GoogleContextProvider extends BaseContextProvider {
9 static description: ContextProviderDescription = {
10 title: "google",
11 displayTitle: "Google",
12 description: "Attach the results of a Google search",
13 type: "query",
14 };
15
16 private _serperApiKey: string;
17
18 constructor(options: { serperApiKey: string }) {
19 super(options);
20 this._serperApiKey = options.serperApiKey;
21 }
22
23 get deprecationMessage() {
24 return "The Google context provider is now deprecated and will be removed in a later version. Please consider using github.com/jae-jae/g-search-mcp instead.";
25 }
26
27 async getContextItems(
28 query: string,
29 extras: ContextProviderExtras,
30 ): Promise<ContextItem[]> {
31 const url = "https://google.serper.dev/search";
32
33 const payload = JSON.stringify({ q: query });
34 const headers = {
35 "X-API-KEY": this._serperApiKey,
36 "Content-Type": "application/json",
37 };
38
39 const response = await extras.fetch(url, {
40 method: "POST",
41 headers: headers,
42 body: payload,
43 });
44
45 if (!response.ok) {
46 throw new Error(
47 `Failed to fetch Google search results: ${response.statusText}`,
48 );
49 }
50 const results = await response.text();
51 try {
52 const parsed = JSON.parse(results);
53 let content = `Google Search: ${query}\n\n`;
54 const answerBox = parsed.answerBox;
55
56 if (answerBox) {
57 content += `Answer Box (${answerBox.title}): ${answerBox.answer}\n\n`;
58 }
59
60 for (const result of parsed.organic) {
61 content += `${result.title}\n${result.link}\n${result.snippet}\n\n`;
62 }
63
64 return [
65 {
66 content,
67 name: "Google Search",
68 description: "Google Search",
69 },
70 ];
71 } catch (e) {
72 throw new Error(`Failed to parse Google search results: ${results}`);
73 }
74 }
75}
76
77export default GoogleContextProvider;