|
| 1 | +--- |
| 2 | +paths: |
| 3 | + - "src/**/*.ts" |
| 4 | +--- |
| 5 | + |
| 6 | +# Architecture of openapi-to-cli (ocli) |
| 7 | + |
| 8 | +`ocli` is a TypeScript/Node CLI that converts OpenAPI/Swagger specs into runtime CLI commands. No code generation: at every invocation it loads a cached spec and builds command definitions on the fly. |
| 9 | + |
| 10 | +## Data flow |
| 11 | + |
| 12 | +``` |
| 13 | +ocli profiles add <name> --api-base-url ... --openapi-spec ... |
| 14 | + | |
| 15 | + v |
| 16 | +ConfigLocator -> .ocli/ dir (global or local) |
| 17 | + | |
| 18 | + v |
| 19 | +ProfileStore -> profiles.ini (read/write/select) |
| 20 | + | |
| 21 | + v |
| 22 | +OpenapiLoader -> fetches spec, caches under .ocli/specs/<profile>.json |
| 23 | + | |
| 24 | + v |
| 25 | +OpenapiToCommands -> parses spec, applies include/exclude filters, |
| 26 | + builds CliCommand[] (name, method, path, options, body schema) |
| 27 | + | |
| 28 | + v |
| 29 | +CommandSearch (BM25) -> ranks commands by NL query or regex |
| 30 | + | |
| 31 | + v |
| 32 | +cli.ts (yargs) -> resolves profile + spec, dispatches: profiles | use | commands | <toolName> |
| 33 | + | |
| 34 | + v |
| 35 | +HttpClient (axios) -> performs the real HTTP request to API_BASE_URL |
| 36 | +``` |
| 37 | + |
| 38 | +## Components (mapping to `src/`) |
| 39 | + |
| 40 | +- `config.ts` - `ConfigLocator`. Finds `.ocli/` (global `~/.ocli/`, local in CWD), resolves `profiles.ini` paths. |
| 41 | +- `profile-store.ts` - `ProfileStore`, `Profile`. Reads/writes `profiles.ini`, tracks current profile, validates fields. |
| 42 | +- `openapi-loader.ts` - `OpenapiLoader`. Loads spec from URL or local file, caches it to `.ocli/specs/<profile>.json`, refreshes on demand. Resolves external `$ref` across multi-file specs. |
| 43 | +- `openapi-to-commands.ts` - `OpenapiToCommands`, `CliCommand`, `CliCommandOption`. Walks the spec, applies include/exclude filters, expands path-level params, resolves local `$ref`, builds command names with optional prefix, expands `enum`/`default`/`nullable`/`oneOf` schema hints for `--help`. |
| 44 | +- `command-search.ts` - `CommandSearch`. BM25 over `(name, method, path, description, options[].name)`, plus regex fallback. Same engine used by both `ocli commands` and any future agent skill. |
| 45 | +- `bm25.ts` - tokenizer + BM25 scorer, no I/O. |
| 46 | +- `cli.ts` - `ocli` entry point. yargs command tree: `profiles add|remove|list`, `use`, `commands`, and dynamic per-spec commands. Builds the `axios` request from a `CliCommand` + parsed args; injects auth, custom headers, server URL overrides. |
| 47 | +- `version.ts` - generated by `scripts/generate-version.js` during `prebuild`. Do not edit by hand. |
| 48 | + |
| 49 | +## Design principles |
| 50 | + |
| 51 | +1. **OpenAPI-driven**: commands and their options come from the spec. No hand-maintained registry. |
| 52 | +2. **Profiles**: every API connection is named; `profiles.ini` is the source of truth. Global vs local `.ocli/` priority is decided by `ConfigLocator`. |
| 53 | +3. **Spec cache**: never re-download a spec on every invocation. Refresh is explicit (`onboard`/profile add or refresh flag). |
| 54 | +4. **Pure transform layer**: `bm25.ts`, `openapi-to-commands.ts`, and `command-search.ts` perform no I/O; they take inputs and return outputs. This keeps them trivially unit-testable. |
| 55 | +5. **Side effects at the edges**: filesystem in `config.ts`/`profile-store.ts`/`openapi-loader.ts`, network in `cli.ts` via `HttpClient`. Inject these via constructors (`fs`, `httpClient`) so tests can swap them. |
| 56 | +6. **TypeScript strict**: `strict: true` in `tsconfig.json`. Explicit types for exported functions and public interfaces. |
| 57 | +7. **No surprise breaking changes**: every CLI-visible change must be reflected in `README.md` and `CHANGELOG.md`. |
| 58 | + |
| 59 | +## Layers and allowed dependencies |
| 60 | + |
| 61 | +``` |
| 62 | +Layer 0 (pure) bm25.ts, version.ts, types in openapi-to-commands.ts |
| 63 | +Layer 1 (I/O wrappers) config.ts, profile-store.ts, openapi-loader.ts |
| 64 | +Layer 2 (transform) openapi-to-commands.ts (uses Profile), command-search.ts (uses CliCommand + bm25) |
| 65 | +Layer 3 (entry) cli.ts (uses everything above; only this layer talks to yargs/axios/process) |
| 66 | +``` |
| 67 | + |
| 68 | +Lower layers must not import from higher layers. New behavior should live in the lowest layer where it makes sense - prefer adding to Layer 2 over expanding `cli.ts`. |
| 69 | + |
| 70 | +## Repository layout |
| 71 | + |
| 72 | +- `src/` - production code (see components above). |
| 73 | +- `tests/` - Jest test files (`*.test.ts`), fixtures under `tests/fixtures/`, recorded results under `tests/results/`. |
| 74 | +- `examples/skill-ocli-api.md` - example Claude Code skill describing the agent workflow. |
| 75 | +- `skills/ocli-api/SKILL.md` - portable OpenClaw skill. |
| 76 | +- `benchmarks/benchmark.ts` - token-overhead comparison (MCP variants vs CLI). |
| 77 | +- `scripts/generate-version.js` - writes `src/version.ts` before build. |
| 78 | +- `.ocli/` - working dir at runtime (not part of source). Never committed. |
| 79 | +- `dist/` - `tsc` build output. |
| 80 | + |
| 81 | +## When extending the spec parser |
| 82 | + |
| 83 | +Real-world OpenAPI/Swagger documents drift from any single example. Before changing `openapi-to-commands.ts` or `openapi-loader.ts`: |
| 84 | + |
| 85 | +- Add a minimal fixture under `tests/fixtures/` that reproduces the case (don't hand-edit `box-api-yaml.test.ts` or `github-api.test.ts` fixtures - those are real specs). |
| 86 | +- Cover both OAS 3 (`requestBody`, `components/schemas`) and Swagger 2 (`body`/`formData`, `definitions`) when the change affects request building. |
| 87 | +- Mention the new spec feature in the README "Broader spec support" or "Better request generation" section. |
0 commit comments