Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPI tool to framework #111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/tools/search/duckDuckGoSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { HeaderGenerator } from "header-generator";
import type { NeedleOptions } from "needle";
import { z } from "zod";
import { Cache } from "@/cache/decoratorCache.js";
import { HttpsProxyAgent } from "https-proxy-agent";

export { SafeSearchType as DuckDuckGoSearchToolSearchType };

Expand All @@ -36,6 +37,8 @@ export interface DuckDuckGoSearchToolOptions extends SearchToolOptions {
throttle?: ThrottleOptions | false;
httpClientOptions?: NeedleOptions;
maxResultsPerPage: number;
apiKey?: string;
http_proxy_url?: string;
}

export interface DuckDuckGoSearchToolRunOptions extends SearchToolRunOptions {
Expand Down Expand Up @@ -121,6 +124,19 @@ export class DuckDuckGoSearchTool extends Tool<
) {
const headers = new HeaderGenerator().getHeaders();

if (this.options.apiKey) {
headers["Authorization"] = `Bearer ${this.options.apiKey}`;
}

const httpClientOptions = {
...this.options?.httpClientOptions,
...options?.httpClientOptions,
};

if (this.options.http_proxy_url) {
httpClientOptions.agent = new HttpsProxyAgent(this.options.http_proxy_url);
}

const { results } = await this.client(
input,
{
Expand All @@ -132,8 +148,7 @@ export class DuckDuckGoSearchTool extends Tool<
{
headers,
user_agent: headers["user-agent"],
...this.options?.httpClientOptions,
...options?.httpClientOptions,
...httpClientOptions,
},
);

Expand Down
29 changes: 17 additions & 12 deletions src/tools/search/googleSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import { ValueError } from "@/errors.js";
import { ValidationError } from "ajv";
import { parseEnv } from "@/internals/env.js";
import { RunContext } from "@/context.js";
import { HttpsProxyAgent } from "https-proxy-agent";

export interface GoogleSearchToolOptions extends SearchToolOptions {
apiKey?: string;
cseId?: string;
maxResultsPerPage: number;
http_proxy_url?: string;
}

type GoogleSearchToolRunOptions = SearchToolRunOptions;
Expand Down Expand Up @@ -129,18 +131,21 @@ export class GoogleSearchTool extends Tool<
run: RunContext<this>,
) {
const startIndex = (page - 1) * this.options.maxResultsPerPage + 1;
const response = await this.client.cse.list(
{
cx: this.cseId,
q: input,
num: this.options.maxResultsPerPage,
start: startIndex,
safe: "active",
},
{
signal: run.signal,
},
);
const requestOptions: GoogleSearchAPI.Params$Resource$Cse$List = {
cx: this.cseId,
q: input,
num: this.options.maxResultsPerPage,
start: startIndex,
safe: "active",
};

if (this.options.http_proxy_url) {
requestOptions.agent = new HttpsProxyAgent(this.options.http_proxy_url);
}

const response = await this.client.cse.list(requestOptions, {
signal: run.signal,
});

const results = response.data.items || [];

Expand Down
28 changes: 21 additions & 7 deletions src/tools/weather/openMeteo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { createURLParams } from "@/internals/fetcher.js";
import { isNullish, pick, pickBy } from "remeda";
import { Cache } from "@/cache/decoratorCache.js";
import { RunContext } from "@/context.js";
import { HttpsProxyAgent } from "https-proxy-agent";

type ToolOptions = { apiKey?: string } & BaseToolOptions;
type ToolOptions = { apiKey?: string; http_proxy_url?: string } & BaseToolOptions;
type ToolRunOptions = BaseToolRunOptions;

interface Location {
Expand Down Expand Up @@ -107,7 +108,7 @@ export class OpenMeteoTool extends Tool<
_options: BaseToolRunOptions | undefined,
run: RunContext<this>,
) {
const { apiKey } = this.options;
const { apiKey, http_proxy_url } = this.options;

const prepareParams = async () => {
const extractLocation = async (): Promise<Location> => {
Expand Down Expand Up @@ -141,14 +142,20 @@ export class OpenMeteoTool extends Tool<
};

const params = await prepareParams();
const response = await fetch(`https://api.open-meteo.com/v1/forecast?${params}`, {
const fetchOptions: RequestInit = {
headers: {
...(apiKey && {
Authorization: `Bearer ${apiKey}`,
}),
},
signal: run.signal,
});
};

if (http_proxy_url) {
fetchOptions.agent = new HttpsProxyAgent(http_proxy_url);
}

const response = await fetch(`https://api.open-meteo.com/v1/forecast?${params}`, fetchOptions);

if (!response.ok) {
throw new ToolError("Request to OpenMeteo API has failed!", [
Expand All @@ -162,7 +169,7 @@ export class OpenMeteoTool extends Tool<

@Cache()
protected async _geocode(location: LocationSearch, signal: AbortSignal) {
const { apiKey } = this.options;
const { apiKey, http_proxy_url } = this.options;

const params = createURLParams({
name: location.name,
Expand All @@ -171,14 +178,21 @@ export class OpenMeteoTool extends Tool<
format: "json",
count: 1,
});
const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?${params}`, {

const fetchOptions: RequestInit = {
headers: {
...(apiKey && {
Authorization: `Bearer ${apiKey}`,
}),
},
signal,
});
};

if (http_proxy_url) {
fetchOptions.agent = new HttpsProxyAgent(http_proxy_url);
}

const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?${params}`, fetchOptions);
if (!response.ok) {
throw new ToolError(`Failed to GeoCode provided location (${location.name}).`, [
new Error(await response.text()),
Expand Down