|
1 | 1 | --- |
2 | 2 | description: Architecture of openapi-to-cli (ocli) |
| 3 | +globs: "src/**/*.ts" |
3 | 4 | alwaysApply: true |
4 | 5 | --- |
5 | 6 |
|
6 | | -## Architectural Rules |
| 7 | +# Architecture of openapi-to-cli (ocli) |
7 | 8 |
|
8 | | -### General Architecture |
| 9 | +`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 |
|
10 | | -The `openapi-to-cli` project is a Node.js/TypeScript CLI application that: |
| 11 | +## Data flow |
11 | 12 |
|
12 | | -- reads an OpenAPI/Swagger spec from a URL or file; |
13 | | -- caches the spec in `.ocli/specs`; |
14 | | -- uses profiles to describe connected APIs; |
15 | | -- maps OpenAPI operations to `ocli` subcommands. |
16 | | - |
17 | | -### Data Flow |
18 | | - |
19 | | -```text |
20 | | -ocli onboard --options --> write profile (profiles.ini) --> download OpenAPI --> save to .ocli/specs |
| 13 | +``` |
| 14 | +ocli profiles add <name> --api-base-url ... --openapi-spec ... |
| 15 | + | |
| 16 | + v |
| 17 | +ConfigLocator -> .ocli/ dir (global or local) |
| 18 | + | |
| 19 | + v |
| 20 | +ProfileStore -> profiles.ini (read/write/select) |
| 21 | + | |
| 22 | + v |
| 23 | +OpenapiLoader -> fetches spec, caches under .ocli/specs/<profile>.json |
| 24 | + | |
| 25 | + v |
| 26 | +OpenapiToCommands -> parses spec, applies include/exclude filters, |
| 27 | + builds CliCommand[] (name, method, path, options, body schema) |
21 | 28 | | |
22 | 29 | v |
23 | | -ocli [--profile] <tool> [options] --> load profile + cached spec --> build commands from OpenAPI --> perform HTTP request to API_BASE_URL |
| 30 | +CommandSearch (BM25) -> ranks commands by NL query or regex |
| 31 | + | |
| 32 | + v |
| 33 | +cli.ts (yargs) -> resolves profile + spec, dispatches: profiles | use | commands | <toolName> |
| 34 | + | |
| 35 | + v |
| 36 | +HttpClient (axios) -> performs the real HTTP request to API_BASE_URL |
24 | 37 | ``` |
25 | 38 |
|
26 | | -### Components |
| 39 | +## Components (mapping to `src/`) |
| 40 | + |
| 41 | +- `config.ts` - `ConfigLocator`. Finds `.ocli/` (global `~/.ocli/`, local in CWD), resolves `profiles.ini` paths. |
| 42 | +- `profile-store.ts` - `ProfileStore`, `Profile`. Reads/writes `profiles.ini`, tracks current profile, validates fields. |
| 43 | +- `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. |
| 44 | +- `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`. |
| 45 | +- `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. |
| 46 | +- `bm25.ts` - tokenizer + BM25 scorer, no I/O. |
| 47 | +- `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. |
| 48 | +- `version.ts` - generated by `scripts/generate-version.js` during `prebuild`. Do not edit by hand. |
| 49 | + |
| 50 | +## Design principles |
| 51 | + |
| 52 | +1. **OpenAPI-driven**: commands and their options come from the spec. No hand-maintained registry. |
| 53 | +2. **Profiles**: every API connection is named; `profiles.ini` is the source of truth. Global vs local `.ocli/` priority is decided by `ConfigLocator`. |
| 54 | +3. **Spec cache**: never re-download a spec on every invocation. Refresh is explicit (`onboard`/profile add or refresh flag). |
| 55 | +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. |
| 56 | +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. |
| 57 | +6. **TypeScript strict**: `strict: true` in `tsconfig.json`. Explicit types for exported functions and public interfaces. |
| 58 | +7. **No surprise breaking changes**: every CLI-visible change must be reflected in `README.md`. |
| 59 | + |
| 60 | +## Layers and allowed dependencies |
| 61 | + |
| 62 | +``` |
| 63 | +Layer 0 (pure) bm25.ts, version.ts, types in openapi-to-commands.ts |
| 64 | +Layer 1 (I/O wrappers) config.ts, profile-store.ts, openapi-loader.ts |
| 65 | +Layer 2 (transform) openapi-to-commands.ts (uses Profile), command-search.ts (uses CliCommand + bm25) |
| 66 | +Layer 3 (entry) cli.ts (uses everything above; only this layer talks to yargs/axios/process) |
| 67 | +``` |
27 | 68 |
|
28 | | -- **config** - locate and select `.ocli` directory, resolve `profiles.ini` paths with global and local priority. |
29 | | -- **profile-store** - read and write profile INI files (`profiles.ini`), select current profile. |
30 | | -- **openapi-loader** - load spec from URL or file and cache it into `.ocli/specs/<profile>.json`. |
31 | | -- **openapi-to-commands** - parse OpenAPI, apply include/exclude filters, build command names and option schemas. |
32 | | -- **cli** - entry point, argument parser, command registration, help rendering. |
| 69 | +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`. |
33 | 70 |
|
34 | | -### Design Principles |
| 71 | +## Repository layout |
35 | 72 |
|
36 | | -1. **OpenAPI-driven** - the list of commands and their parameters is defined by the spec. |
37 | | -2. **Profiles** - all API settings are configured via profiles (global or local). |
38 | | -3. **Spec cache** - the spec is cached under `.ocli/specs` to avoid fetching it on every run. |
39 | | -4. **TypeScript strict** - `strict: true`; explicit types for public APIs and profile interfaces. |
40 | | -5. **TDD for core logic** - unit tests for profile parsing, spec loading and command mapping. |
41 | | -6. **Language** - all documentation and code comments for this project must be written in English. |
| 73 | +- `src/` - production code (see components above). |
| 74 | +- `tests/` - Jest test files (`*.test.ts`), fixtures under `tests/fixtures/`, recorded results under `tests/results/`. |
| 75 | +- `examples/skill-ocli-api.md` - example Claude Code skill describing the agent workflow. |
| 76 | +- `skills/ocli-api/SKILL.md` - portable OpenClaw skill. |
| 77 | +- `benchmarks/benchmark.ts` - token-overhead comparison (MCP variants vs CLI). |
| 78 | +- `scripts/generate-version.js` - writes `src/version.ts` before build. |
| 79 | +- `.ocli/` - working dir at runtime (not part of source). Never committed. |
| 80 | +- `dist/` - `tsc` build output. |
42 | 81 |
|
43 | | -### Repository Layout (for the openapi-to-cli directory) |
| 82 | +## When extending the spec parser |
44 | 83 |
|
45 | | -- `README.md` - concept and description of the CLI and profiles. |
46 | | -- `package.json` - npm package with the `ocli` binary. |
47 | | -- `tsconfig.json` - TypeScript config with strict mode. |
48 | | -- `jest.config.js` - Jest configuration. |
49 | | -- `src/`: |
50 | | - - `cli.ts` - `ocli` binary entry point. |
51 | | - - future modules: `config`, `profile-store`, `openapi-loader`, `openapi-to-commands`, etc. |
52 | | -- `tests/` - tests for the modules above. |
| 84 | +Real-world OpenAPI/Swagger documents drift from any single example. Before changing `openapi-to-commands.ts` or `openapi-loader.ts`: |
53 | 85 |
|
| 86 | +- 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). |
| 87 | +- Cover both OAS 3 (`requestBody`, `components/schemas`) and Swagger 2 (`body`/`formData`, `definitions`) when the change affects request building. |
| 88 | +- Mention the new spec feature in the README "Broader spec support" or "Better request generation" section. |
0 commit comments