hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
680 stars 52 forks source link

Expose/Publish parsers to npm? #750

Open parhammmm opened 4 days ago

parhammmm commented 4 days ago

Description

Would it be possible to publish/expose the parsers from this library to npm?

https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/openApi/index.ts

That would be very useful for our use case which is for creating "generic tools" for an LLM Agent very similar to OpenAI

Other libraries are outdated or abandoned.

mrlubos commented 4 days ago

@parhammmm Hmmm, maybe? Can you describe in more detail what you need and how you imagine it would work? Why aren't the tools published by README, Redocly, etc. sufficient for that?

parhammmm commented 1 day ago

@mrlubos I couldn't find a parser for Redocly, could you point me to what you're referring to?

The README parser doesn't actually provide a good validator/parser, as an example the servers field is not in the types of their output and it lets any field be in the schema and it accepts it. Their documentation is outdated and referrs to the original fork's implementation which they seem to have diverged from.

The use case is accepting the users' OpenAPI schema which needs certain fields (e.g. servers,...) converting it to json schema and then defining an LLM tool definition from it for OpenAI, Anthropic, Gemini, ...

mrlubos commented 1 day ago

I think this CLI tool could be used at least for validating https://github.com/Redocly/redocly-cli.

Sounds like your use case would require certain hooks? Can you provide at least a pseudo-code of what you'd be looking to use from the existing package?

parhammmm commented 1 day ago

cli won't be usable unfortunately but I can check to see if they expose internal parsers

I wouldn't need any specific hooks, so this would be the usage of it via zod

import { z } from "zod";
import { openapiSchemaToJsonSchema } from "@openapi-contrib/openapi-schema-to-json-schema";

const openApiSchemaValidator = (
  schema: string,
): { valid: boolean; message?: string } => {
  try {
    const parsed = JSON.parse(schema);
    // add validation here e.g. validate(parsed)
    const jsonSchema = openapiSchemaToJsonSchema(parsed);

   // below is current bare bones validation
    if (!jsonSchema.servers?.length)
      return {
        valid: false,
        message: "The schema must include at least one server.",
      };
    if (jsonSchema.servers.length > 2)
      return {
        valid: false,
        message: "The schema must not include more than two servers.",
      };
    return { valid: true };
  } catch (e) {
    return { valid: false, message: "The schema must be a valid JSON." };
  }
};

export const openAPISchema = z.string().superRefine((schema, context) => {
  const result = openApiSchemaValidator(schema);
  if (!result.valid) {
    context.addIssue({
      code: z.ZodIssueCode.custom,
      message: result.message || "The schema must be a valid OpenAPI schema.",
    });
  }
});