|
| 1 | +# @thingweb/code-gen |
| 2 | + |
| 3 | +Generates code snippets for interacting with [Thing Descriptions (TD)](https://www.w3.org/TR/wot-thing-description11/). Supports multiple programming languages and libraries. For unsupported language/library combinations, it generates a prompt for an LLM to complete the task. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### As a Library |
| 8 | + |
| 9 | +```js |
| 10 | +import { generateCode, isProtocolSupported } from "@thingweb/code-gen"; |
| 11 | + |
| 12 | +const result = generateCode({ |
| 13 | + td, |
| 14 | + affordanceType: "properties", |
| 15 | + affordanceKey: "temperature", |
| 16 | + operation: "readproperty", |
| 17 | + language: "javascript", |
| 18 | + library: "fetch", |
| 19 | +}); |
| 20 | + |
| 21 | +if ("code" in result) { |
| 22 | + console.log(result.code); |
| 23 | +} else { |
| 24 | + console.log(result.prompt); // LLM prompt for unsupported combinations |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### CLI |
| 29 | + |
| 30 | +The package provides both interactive and non-interactive CLI modes. |
| 31 | + |
| 32 | +**Interactive mode** — run without arguments to be guided through prompts: |
| 33 | + |
| 34 | +```bash |
| 35 | +npm run cli |
| 36 | +``` |
| 37 | + |
| 38 | +**Non-interactive mode** — pass all options as flags. When running the CLI through the npm script, parameters are added after `--`: |
| 39 | + |
| 40 | +```bash |
| 41 | +npm run cli -- --td ./my-thing.td.jsonld --affordance-type properties --affordance-key temperature --operation readproperty --language javascript --library fetch --output ./output |
| 42 | +``` |
| 43 | + |
| 44 | +#### CLI Options |
| 45 | + |
| 46 | +| Flag | Description | |
| 47 | +| ----------------------- | ---------------------------------------------------------- | |
| 48 | +| `-t, --td` | Path to the TD JSON file (relative or absolute) | |
| 49 | +| `-a, --affordance-type` | Affordance type: `properties`, `actions`, or `events` | |
| 50 | +| `-k, --affordance-key` | Key name of the affordance | |
| 51 | +| `-o, --operation` | Operation to perform (e.g. `readproperty`, `invokeaction`) | |
| 52 | +| `-l, --language` | Programming language | |
| 53 | +| `-b, --library` | Library to use | |
| 54 | +| `-O, --output` | Output folder path (relative or absolute, default: `./`) | |
| 55 | + |
| 56 | +## Supported Languages and Libraries |
| 57 | + |
| 58 | +| Language | Libraries | |
| 59 | +| ---------- | --------------------------------------------- | |
| 60 | +| JavaScript | fetch, node-wot, webthing, modbus-serial | |
| 61 | +| Python | requests, wotpy, PyModbus | |
| 62 | +| Java | httpclient, wot-servient, digitalpetri/modbus | |
| 63 | +| Rust | reqwest | |
| 64 | +| Go | net-http | |
| 65 | +| C# | httpclient, WoT.Net | |
| 66 | +| PHP | curl | |
| 67 | +| Ruby | net-http | |
| 68 | +| Dart | dart-wot, http | |
| 69 | + |
| 70 | +## Scripts |
| 71 | + |
| 72 | +- `npm run build` — Compile TypeScript to `dist/` |
| 73 | +- `npm run cli` — Run the CLI |
| 74 | +- `npm test` — Run tests |
| 75 | + |
| 76 | +## License |
| 77 | + |
| 78 | +Licensed under the MIT license, see [License](../../LICENSE.md). |
0 commit comments