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 #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

akihikokuroda
Copy link
Contributor

@akihikokuroda akihikokuroda commented Dec 11, 2024

Which issue(s) does this pull-request address?

Closes: #105

Description

Checklist

  • I have read the contributor guide
  • Linting passes: yarn lint or yarn lint:fix
  • Formatting is applied: yarn format or yarn format:fix
  • Unit tests pass: yarn test:unit
  • E2E tests pass: yarn test:e2e
  • Tests are included
  • Documentation is changed or added
  • Commit messages and PR title follow conventional commits

@akihikokuroda akihikokuroda requested a review from a team as a code owner December 11, 2024 21:46
description?: string;
openApiSchema?: any;
apiKey?: string;
http_proxy_url?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use camelCase

http_proxy_url?: string;
}

type ToolRunOptions = BaseToolRunOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed.

}
}

export class OpenAPI extends Tool<StringToolOutput, OpenAPIOptions, ToolRunOptions> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToolRunOptions can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to OpenAPITool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response class does not match the reality.
It should be OpenAPIToolOutput, not StringToolOutput.

Comment on lines 112 to 113
//ToolInput<this>,
any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with Record<string, any>

public readonly emitter: ToolEmitter<
//ToolInput<this>,
any,
StringToolOutput,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be OpenAPIToolOutput

this.openApiSchema = parse(openApiSchema);
// TODO: #105 review error codes. Were using APIErrorCode, now ToolError
if (!this.openApiSchema?.paths) {
throw new ValueError("Server is not specified!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the error message more descriptive, please.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole constructor can be simplified to

public constructor(options: OpenAPIOptions) {
    super(options);
    this.openApiSchema = parse(options.openApiSchema);
    if (!this.openApiSchema?.paths) {
      throw new ValueError("Server is not specified!");
    }
  }

// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
const headers: { [key: string]: string } = { Accept: "application/json" };
if (this.apiKey) {
headers["Authorization"] = `Bearer this.apiKey`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work.

You probably wanted to do Bearer ${this.apiKey}.

src/tools/openapi.ts Show resolved Hide resolved
headers: headers,
signal: AbortSignal.any([AbortSignal.timeout(30_000), run.signal]),
});
return new StringToolOutput(await response.text());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong response type.

body: !isEmpty(input.body) ? input.body : undefined,
method: input.method.toLowerCase(),
headers: headers,
signal: AbortSignal.any([AbortSignal.timeout(30_000), run.signal]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the AbortSignal.timeout() because a user can specify it via run parameters.

this.openApiSchema = parse(openApiSchema);
// TODO: #105 review error codes. Were using APIErrorCode, now ToolError
if (!this.openApiSchema?.paths) {
throw new ValueError("Server is not specified!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole constructor can be simplified to

public constructor(options: OpenAPIOptions) {
    super(options);
    this.openApiSchema = parse(options.openApiSchema);
    if (!this.openApiSchema?.paths) {
      throw new ValueError("Server is not specified!");
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing serialization-related methods.

}

protected async _run(
input: any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the error message?

const text = await response.text();
return new OpenAPIToolOutput(response.status, response.statusText, text);
} catch {
throw new ToolError(`Request to ${url} failed.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you throw this error without any context, the agent cannot retry. Do this instead.

} catch (err) {
   throw new ToolError(`Request to ${url} has failed.`, [err]);
}

@akihikokuroda akihikokuroda force-pushed the apitool branch 3 times, most recently from 5058a23 to 1e85cb1 Compare December 16, 2024 20:59
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI tool
2 participants