diff --git a/.changeset/generate-spec-command.md b/.changeset/generate-spec-command.md new file mode 100644 index 0000000000..d2cf3c6a4d --- /dev/null +++ b/.changeset/generate-spec-command.md @@ -0,0 +1,5 @@ +--- +'@redocly/cli': minor +--- + +Added an experimental `generate-spec` command that infers an OpenAPI description from recorded HTTP traffic. diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index a516482b11..e7a9e9b595 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -5,4 +5,5 @@ docs/ @Redocly/technical-writers packages/cli/src/utils/otel.ts @Redocly/hot-dogs packages/cli/src/utils/telemetry.ts @Redocly/hot-dogs packages/cli/src/commands/drift/ @Redocly/cyberpunks +packages/cli/src/commands/generate-spec/ @Redocly/cyberpunks packages/cli/src/commands/proxy/ @Redocly/cyberpunks \ No newline at end of file diff --git a/docs/@v2/commands/drift.md b/docs/@v2/commands/drift.md index 36e0f3a9f4..edd522ff5a 100644 --- a/docs/@v2/commands/drift.md +++ b/docs/@v2/commands/drift.md @@ -114,3 +114,4 @@ redocly drift ./traffic.har --api ./openapi.yaml --format json -o ./drift-report ## Related commands - [`proxy`](./proxy.md) captures live HTTP traffic into a HAR file that can be replayed through `drift`. +- [`generate-spec`](./generate-spec.md) infers an OpenAPI description from the same traffic formats. diff --git a/docs/@v2/commands/generate-spec.md b/docs/@v2/commands/generate-spec.md new file mode 100644 index 0000000000..482c6cdc62 --- /dev/null +++ b/docs/@v2/commands/generate-spec.md @@ -0,0 +1,119 @@ +# `generate-spec` + +The `generate-spec` command infers an OpenAPI description from recorded HTTP traffic. +The command reads a traffic log (or a folder of logs), builds a deterministic baseline description from the observed exchanges, and can optionally refine that baseline with an AI provider. + +{% admonition type="warning" name="Experimental" %} +This is an experimental feature. +Its behavior, command, flags, and output may change in future releases. + +The `generate-spec` command generates OpenAPI 3.1 descriptions only. +{% /admonition %} + +## Supported traffic formats + +The traffic input can be provided in any of the following formats. +By default the format is detected automatically from the file contents: + +- HAR +- Kong +- Nginx JSON +- Apache JSON +- NDJSON + +Traffic parsing is shared with the [`drift`](./drift.md) command, so any log that works with `drift` works here too. + +## How it works + +The baseline description is inferred directly from the recorded exchanges: identifier-like path segments become named path parameters, request and response schemas are merged across all observations, repeated object shapes are extracted into `components/schemas`, and consistently formatted string values get `format` or `enum` hints. + +A description inferred from traffic alone is a hypothesis: types are coarse and there are no descriptions, enums, or examples beyond what was observed. +With `--with-ai`, the command sends each operation together with sample exchanges to an AI provider, which narrows types and adds formats, enums, descriptions, and examples. + +The AI's answer is never trusted blindly: a refined operation is only accepted when it keeps the operation's path, method, and observed response status codes, and passes validation with the `spec` ruleset. +Rejected refinements keep their baseline version, and if refinement fails entirely the command falls back to the baseline description. +The validation uses the built-in `spec` ruleset, not your project's `redocly.yaml` — lint the generated description with your own configuration afterward. + +{% admonition type="warning" name="Data sharing" %} +`--with-ai` sends samples of the recorded traffic (URLs, query strings, request and response bodies) to the selected AI provider. +Make sure the traffic contains no secrets or personal data you are not allowed to share. +{% /admonition %} + +### AI providers + +Refinement runs a locally installed AI CLI in non-interactive mode: `claude` (Claude Code), `codex` (Codex CLI), or `cursor` (Cursor CLI). +The selected CLI must be installed and authenticated on the machine running the command. + +The provider runs in isolation: project context the CLIs normally load (such as `CLAUDE.md`, `AGENTS.md`, or `.cursor/rules`) and settings like a configured model do not apply. +Use `--ai-model` to choose a model, or the provider's default is used. + +## Usage + +```bash +redocly generate-spec +redocly generate-spec [--traffic-format=