swytchcode-runtime 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -2
- package/dist/exec.d.ts +6 -3
- package/dist/exec.js +12 -1
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,12 +20,30 @@ npm install @swytchcode/runtime
|
|
|
20
20
|
import { exec } from "@swytchcode/runtime";
|
|
21
21
|
|
|
22
22
|
const result = await exec("api.account.create", {
|
|
23
|
-
|
|
23
|
+
body: { name: "my-cluster" },
|
|
24
|
+
Authorization: "Bearer token123",
|
|
24
25
|
});
|
|
25
26
|
// result is parsed JSON (unknown)
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
Equivalent to: `swytchcode exec api.account.create --json` with
|
|
29
|
+
Equivalent to: `swytchcode exec api.account.create --json` with args on stdin.
|
|
30
|
+
|
|
31
|
+
**Request input (args):** The second argument is the kernel **args** object (sent as JSON on stdin). Use this shape so the kernel builds the request correctly:
|
|
32
|
+
- **`body`** — Request body (object).
|
|
33
|
+
- **`params`** — Query/path params (object, e.g. `{ id: "cluster-123" }`).
|
|
34
|
+
- **`Authorization`** — Auth header value (e.g. `"Bearer token123"`).
|
|
35
|
+
- **`headers`** — Additional request headers (e.g. `{ "X-Request-Id": "abc-123" }`).
|
|
36
|
+
- Other top-level keys are passed as query params.
|
|
37
|
+
|
|
38
|
+
Example with body, params, and headers:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
await exec("api.cluster.get", {
|
|
42
|
+
params: { id: "cluster-123" },
|
|
43
|
+
Authorization: "Bearer token123",
|
|
44
|
+
headers: { "X-Request-Id": "abc-123" },
|
|
45
|
+
});
|
|
46
|
+
```
|
|
29
47
|
|
|
30
48
|
### Raw mode
|
|
31
49
|
|
|
@@ -46,8 +64,12 @@ Equivalent to: `swytchcode exec api.report.export --raw` with input on stdin.
|
|
|
46
64
|
- **`env`** – Extra environment variables (merged with `process.env`).
|
|
47
65
|
- **`output`** – `"json"` (default), `"raw"`, or `"stream"`. Default is JSON (stdout must be valid JSON; parse failure throws). Use `"raw"` to get stdout as a string. `"stream"` is not supported and will throw; use the CLI directly for streaming.
|
|
48
66
|
- **`raw`** – If `true`, same as `output: "raw"`. Kept for backward compatibility.
|
|
67
|
+
- **`dryRun`** – If `true`, pass `--dry-run` to the CLI; the CLI outputs request details (method, url, headers, body) instead of calling the server.
|
|
68
|
+
- **`allowRaw`** – If `true`, pass `--allow-raw` to the CLI; required for executing raw methods (kernel has this disabled by default).
|
|
49
69
|
- **`debug`** – If `true`, log spawn args, cwd, exit status, and stdout/stderr lengths to stderr.
|
|
50
70
|
|
|
71
|
+
This runtime invokes `swytchcode exec [canonical_id]` with the flags above. For full exec behavior (exit codes, output format, pipeline), see the [Swytchcode kernel documentation](https://github.com/swytchcode/swytchcode).
|
|
72
|
+
|
|
51
73
|
**Debug logs** are also enabled when `SWYTCHCODE_RUNTIME_DEBUG=1` or `SWYTCHCODE_RUNTIME_DEBUG=true` (no code change):
|
|
52
74
|
|
|
53
75
|
```bash
|
package/dist/exec.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { ExecOptions, ExecResult } from "./types.js";
|
|
1
|
+
import type { ExecArgs, ExecOptions, ExecResult } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Run `swytchcode exec <canonicalId>` with optional JSON
|
|
3
|
+
* Run `swytchcode exec <canonicalId>` with optional JSON args on stdin.
|
|
4
4
|
* Default is JSON mode (stdout must be valid JSON; empty or parse failure throws).
|
|
5
5
|
* Use output: "raw" or raw: true for raw stdout string. Stream mode is not supported.
|
|
6
6
|
*
|
|
7
|
+
* The second argument (input) is the kernel args object sent on stdin: use body, params,
|
|
8
|
+
* Authorization, and headers so the kernel builds the request correctly. See ExecArgs.
|
|
9
|
+
*
|
|
7
10
|
* Enable logs: pass `{ debug: true }` or set env SWYTCHCODE_RUNTIME_DEBUG=1
|
|
8
11
|
*/
|
|
9
|
-
export declare function exec(canonicalId: string, input?: unknown, options?: ExecOptions): Promise<ExecResult>;
|
|
12
|
+
export declare function exec(canonicalId: string, input?: ExecArgs | unknown, options?: ExecOptions): Promise<ExecResult>;
|
package/dist/exec.js
CHANGED
|
@@ -16,10 +16,13 @@ function log(debug, msg, detail) {
|
|
|
16
16
|
process.stderr.write(line);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Run `swytchcode exec <canonicalId>` with optional JSON
|
|
19
|
+
* Run `swytchcode exec <canonicalId>` with optional JSON args on stdin.
|
|
20
20
|
* Default is JSON mode (stdout must be valid JSON; empty or parse failure throws).
|
|
21
21
|
* Use output: "raw" or raw: true for raw stdout string. Stream mode is not supported.
|
|
22
22
|
*
|
|
23
|
+
* The second argument (input) is the kernel args object sent on stdin: use body, params,
|
|
24
|
+
* Authorization, and headers so the kernel builds the request correctly. See ExecArgs.
|
|
25
|
+
*
|
|
23
26
|
* Enable logs: pass `{ debug: true }` or set env SWYTCHCODE_RUNTIME_DEBUG=1
|
|
24
27
|
*/
|
|
25
28
|
function exec(canonicalId, input, options = {}) {
|
|
@@ -36,9 +39,17 @@ function exec(canonicalId, input, options = {}) {
|
|
|
36
39
|
}
|
|
37
40
|
const raw = outputMode === "raw";
|
|
38
41
|
const args = ["exec", canonicalIdTrimmed, raw ? "--raw" : "--json"];
|
|
42
|
+
if (options.dryRun === true)
|
|
43
|
+
args.push("--dry-run");
|
|
44
|
+
if (options.allowRaw === true)
|
|
45
|
+
args.push("--allow-raw");
|
|
39
46
|
const cwd = options.cwd ?? process.cwd();
|
|
40
47
|
const hasInput = input !== undefined && input !== null;
|
|
41
48
|
log(debug, "spawn:", `swytchcode ${args.join(" ")}`);
|
|
49
|
+
if (options.dryRun === true)
|
|
50
|
+
log(debug, "dry-run:", "enabled");
|
|
51
|
+
if (options.allowRaw === true)
|
|
52
|
+
log(debug, "allow-raw:", "enabled");
|
|
42
53
|
log(debug, "cwd:", cwd);
|
|
43
54
|
log(debug, "stdin:", hasInput ? `JSON (${JSON.stringify(input).length} chars)` : "none");
|
|
44
55
|
const result = (0, node_child_process_1.spawnSync)("swytchcode", args, {
|
package/dist/index.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -12,8 +12,27 @@ export interface ExecOptions {
|
|
|
12
12
|
output?: OutputMode;
|
|
13
13
|
/** If true, same as `output: "raw"`. Kept for backward compatibility. */
|
|
14
14
|
raw?: boolean;
|
|
15
|
+
/** If true, pass `--dry-run` to the CLI; request details are output instead of calling the server. */
|
|
16
|
+
dryRun?: boolean;
|
|
17
|
+
/** If true, pass `--allow-raw` to the CLI; required for executing raw methods (disabled by default in kernel). */
|
|
18
|
+
allowRaw?: boolean;
|
|
15
19
|
/** If true, log spawn/capture details to process.stderr (for debugging). */
|
|
16
20
|
debug?: boolean;
|
|
17
21
|
}
|
|
18
22
|
/** Result of `exec()` in JSON mode: parsed stdout. In raw mode the result is a string. */
|
|
19
23
|
export type ExecResult = unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Tool arguments sent to the kernel on stdin (matches `swytchcode exec` JSON stdin).
|
|
26
|
+
* - `body`: Request body (object).
|
|
27
|
+
* - `params`: Query/path params (object).
|
|
28
|
+
* - `Authorization`: Auth header value (e.g. "Bearer token").
|
|
29
|
+
* - `headers`: Additional request headers (map of header name to value).
|
|
30
|
+
* - Other top-level keys are passed as query params.
|
|
31
|
+
*/
|
|
32
|
+
export interface ExecArgs {
|
|
33
|
+
body?: unknown;
|
|
34
|
+
params?: Record<string, string>;
|
|
35
|
+
Authorization?: string;
|
|
36
|
+
headers?: Record<string, string>;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|