@kivacore/cli-browser
v1.1.0
Published
KIVA API CLI for the browser: run kiva API commands from a web page
Readme
@kivacore/cli-browser
The API commands of the kiva CLI, packaged to run in a web page. Use it to
build a terminal-style console or a CLI simulator on top of the Kiva API: the commands, flags and output are the ones of the
real CLI, because they are built from the same source.
One ES module, no dependencies, no Node APIs.
import { createKivaCli } from "@kivacore/cli-browser";
const cli = createKivaCli({ accessToken });
const apps = await cli.run("apps list");
console.log(apps.stdout);
const who = await cli.run("auth who --json");
console.log(JSON.parse(who.stdout).data.email);Signing in
There is no auth login. The page signs the user in and hands the tokens to the CLI:
const cli = createKivaCli({
accessToken,
refreshToken, // optional: lets the CLI renew an expired access token
clientId, // the OAuth2 client the tokens were issued to; needed with refreshToken
apiBaseUrl: "https://api.kiva.app", // the default
onTokensChange: ({ accessToken, refreshToken, expiresAt }) => saveTokens({ accessToken, refreshToken }),
});The first command that needs a session asks the API who the token belongs to (GET /users/me), so results carry the same
context (email and tenant) as the real CLI. When the user signs in again or switches tenant, pass the new tokens with
cli.setTokens({ accessToken, refreshToken, clientId }).
Without a refresh token, commands fail once the access token expires, until setTokens is called.
The API must allow cross-origin requests from the page's origin.
Running commands
run takes a command line or an argument list. A leading kiva is optional, and quoting follows shell rules, so JSON
arguments can be written as in a terminal:
await cli.run(`ds create crm contacts --data '{"name":"Ada"}'`);
await cli.run(["ds", "create", "crm", "contacts", "--data", JSON.stringify({ name: "Ada" })]);It resolves to { argv, exitCode, stdout, stderr } and does not reject when a command fails: check exitCode. As in the
real CLI, a failing command prints its error to stdout; stderr holds what the browser build itself refuses.
Options for a single command:
| Option | Purpose |
| ---------------------- | ----------------------------------------------------------------------------- |
| stdin | Text piped to the command, like echo '{…}' \| kiva ds create crm contacts. |
| env | Extra environment variables for this command. |
| tty | false formats output for a program, as when the real CLI's output is piped. |
| onStdout, onStderr | Receive output while the command runs. |
Every global flag of the CLI works: --json, --format <toon|json|yaml|md|jsonl>, --filter-output, --help, --schema,
--llms, and the rest listed by kiva --help.
Commands run one at a time; calls made while another command is running wait their turn.
Files
Options that read a file (--from-file, uploads) read from an in-memory filesystem that belongs to the CLI instance:
const cli = createKivaCli({ accessToken, files: { "contacts.csv": csvText } });
cli.files.write("logo.png", pngBytes);
await cli.run("ds create crm contacts --from-file contacts.csv");cli.files has write, read, readBytes, remove and list. Relative paths resolve against /. Files the commands
write, such as the OpenAPI spec discover downloads, appear there too. Nothing is persisted: a new instance starts empty.
The tenant's OpenAPI spec is the exception. discover keeps it for every instance on the page for ten minutes, so later
instances do not download it again, and discover api downloads a fresh one. Since the page cannot read an instance's
files, discover results do not name the spec's file.
What is included
auth (who, tenant, accounts list, tenants list, sso-session), apps, ds, dsql, discover, studio,
automation, agent, acl, account, messaging, connect and curl.
Left out, because they need a terminal, local files, git or an interactive sign-in: the agents (work, code, server),
env, browser, knowledge, project-plan, release, agent-skills, update, auth login and the other
session-changing auth commands, apps clone, and apps create --local-development. Running one returns exit code 1
with an explanation.
The package version is the version of the kiva CLI it was built from; cli.version returns it.
