| 1 | import { BaseContextProvider } from ".."; |
| 2 | import { |
| 3 | ContextItem, |
| 4 | ContextProviderDescription, |
| 5 | ContextProviderExtras, |
| 6 | FetchFunction, |
| 7 | } from "../.."; |
| 8 | import { getHeaders } from "../../continueServer/stubs/headers"; |
| 9 | import { TRIAL_PROXY_URL } from "../../control-plane/client"; |
| 10 | |
| 11 | export const fetchSearchResults = async ( |
| 12 | query: string, |
| 13 | n: number, |
| 14 | fetchFn: FetchFunction, |
| 15 | ): Promise<ContextItem[]> => { |
| 16 | const resp = await fetchFn(WebContextProvider.ENDPOINT, { |
| 17 | method: "POST", |
| 18 | headers: { |
| 19 | "Content-Type": "application/json", |
| 20 | ...(await getHeaders()), |
| 21 | }, |
| 22 | body: JSON.stringify({ |
| 23 | query, |
| 24 | n, |
| 25 | }), |
| 26 | }); |
| 27 | |
| 28 | if (!resp.ok) { |
| 29 | const text = await resp.text(); |
| 30 | throw new Error(`Failed to fetch web context: ${text}`); |
| 31 | } |
| 32 | return await resp.json(); |
| 33 | }; |
| 34 | |
| 35 | export default class WebContextProvider extends BaseContextProvider { |
| 36 | public static ENDPOINT = new URL("web", TRIAL_PROXY_URL); |
| 37 | private static DEFAULT_N = 6; |
| 38 | |
| 39 | static description: ContextProviderDescription = { |
| 40 | title: "web", |
| 41 | displayTitle: "Web", |
| 42 | description: "Search the web", |
| 43 | type: "normal", |
| 44 | renderInlineAs: "", |
| 45 | }; |
| 46 | |
| 47 | async getContextItems( |
| 48 | query: string, |
| 49 | extras: ContextProviderExtras, |
| 50 | ): Promise<ContextItem[]> { |
| 51 | return await fetchSearchResults( |
| 52 | extras.fullInput, |
| 53 | this.options.n ?? WebContextProvider.DEFAULT_N, |
| 54 | extras.fetch, |
| 55 | ); |
| 56 | } |
| 57 | } |