nemu-cli 1.0.0
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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +170 -0
- package/dist/args.js +77 -0
- package/dist/commands/account.js +187 -0
- package/dist/commands/login.js +84 -0
- package/dist/commands/logout.js +19 -0
- package/dist/commands/resources.js +318 -0
- package/dist/commands/whoami.js +35 -0
- package/dist/commands/workspaces.js +115 -0
- package/dist/config.js +58 -0
- package/dist/index.js +155 -0
- package/dist/output.js +64 -0
- package/dist/session.js +52 -0
- package/dist/ui.js +55 -0
- package/package.json +40 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
First real release. `login` signs in with the device grant, so nothing is pasted
|
|
6
|
+
into the terminal and the package carries no secret.
|
|
7
|
+
|
|
8
|
+
Commands for everything the management API exposes: workspaces, members,
|
|
9
|
+
budgets, providers, models, prompts, mappings, indexes, quotas, metrics and
|
|
10
|
+
workflows. `nemu api` calls any endpoint directly for the things that have no
|
|
11
|
+
command of their own.
|
|
12
|
+
|
|
13
|
+
Every command takes `--json` and exits non zero on failure, so a script or an
|
|
14
|
+
agent can drive it without scraping tables.
|
|
15
|
+
|
|
16
|
+
Destructive commands need `--yes`. There is no interactive prompt anywhere.
|
|
17
|
+
|
|
18
|
+
## 0.1.0
|
|
19
|
+
|
|
20
|
+
Unpublished. login, logout and whoami only.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hiraeeth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# nemu-cli
|
|
2
|
+
|
|
3
|
+
Command line access to Nemu. The package is `nemu-cli` and the command it installs is `nemu`,
|
|
4
|
+
because `nemu` itself was taken on npm in 2016.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
bunx nemu-cli login
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Or install it once and drop the package name:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bun add -g nemu-cli
|
|
14
|
+
nemu login
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Signing in
|
|
18
|
+
|
|
19
|
+
`nemu login` uses the OAuth device authorization grant, so nothing is typed into the terminal and no
|
|
20
|
+
secret is baked into this package.
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
$ nemu login
|
|
24
|
+
|
|
25
|
+
Open https://app.nemu.cc/device
|
|
26
|
+
Code BDXW-KRTQ
|
|
27
|
+
|
|
28
|
+
Waiting for approval...
|
|
29
|
+
✓ Logged in as hiraeeth
|
|
30
|
+
Token saved to ~/.nemu/auth.json
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The browser opens on its own. Pass `--no-browser` when there is no browser to open, which is the case
|
|
34
|
+
over SSH and inside a container, and open the printed link from any other device. The code expires
|
|
35
|
+
after 15 minutes. `--force` signs in again when a session already exists.
|
|
36
|
+
|
|
37
|
+
## Commands
|
|
38
|
+
|
|
39
|
+
Every command takes `--json`, which is what you want when something other than a person is reading
|
|
40
|
+
the output. Failures exit non zero and print `{"ok": false, "error": "..."}` under `--json`.
|
|
41
|
+
|
|
42
|
+
Anything workspace scoped takes `-w` or `--workspace`, by id or by name. Left off, it uses your first
|
|
43
|
+
workspace.
|
|
44
|
+
|
|
45
|
+
### Account
|
|
46
|
+
|
|
47
|
+
| Command | Does |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| `nemu login` | Sign in on this machine |
|
|
50
|
+
| `nemu logout` | Delete the stored token |
|
|
51
|
+
| `nemu whoami` | Show the signed in account |
|
|
52
|
+
| `nemu user` | Full account record |
|
|
53
|
+
| `nemu scopes` | Every scope and the actions it covers |
|
|
54
|
+
| `nemu health` | Service health |
|
|
55
|
+
| `nemu api-version` | Supported API versions |
|
|
56
|
+
|
|
57
|
+
### Workspaces and members
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
nemu workspaces list
|
|
61
|
+
nemu workspaces get <id>
|
|
62
|
+
nemu workspaces create "My workspace"
|
|
63
|
+
nemu workspaces rename <id> "New name"
|
|
64
|
+
nemu workspaces delete <id> --yes
|
|
65
|
+
nemu workspaces budget
|
|
66
|
+
nemu workspaces set-budget <id> 50
|
|
67
|
+
|
|
68
|
+
nemu members list
|
|
69
|
+
nemu members invite --email a@b.com,c@d.com --max-uses 5 --expires-in-days 30
|
|
70
|
+
nemu members role <member-id> ADMIN
|
|
71
|
+
nemu members remove <member-id>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Providers and models
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
nemu providers list
|
|
78
|
+
nemu providers create myprovider --priority 1 \
|
|
79
|
+
--base-url https://api.openai.com/v1 \
|
|
80
|
+
--api-key sk-... \
|
|
81
|
+
--compatibility OPENAI_COMPATIBLE
|
|
82
|
+
nemu providers update <id> --priority 5 --enabled false
|
|
83
|
+
nemu providers delete <id> --yes
|
|
84
|
+
|
|
85
|
+
nemu models list --provider <provider-id> --type chat
|
|
86
|
+
nemu models create --name gpt-5 --display-name "GPT 5" \
|
|
87
|
+
--provider <provider-id> --type chat \
|
|
88
|
+
--input-cost 0.00001 --output-cost 0.00003 --context-window 128000
|
|
89
|
+
nemu models update <id> --output-cost 0.00004
|
|
90
|
+
nemu models delete <id> --yes
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`--compatibility` is `OPENAI_COMPATIBLE`, `ANTHROPIC` or `OPENAI_RESPONSES`. Model `--type` is
|
|
94
|
+
`chat`, `embedding`, `moderation`, `vision`, `audio` or `image`.
|
|
95
|
+
|
|
96
|
+
### Prompts and mappings
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
nemu prompts create --name house-style --content "Answer plainly."
|
|
100
|
+
nemu prompts list --type SYSTEM
|
|
101
|
+
nemu prompts update <id> --content "Answer very plainly."
|
|
102
|
+
nemu prompts delete <id> --yes
|
|
103
|
+
|
|
104
|
+
nemu mappings create --source "gpt-4*" --target myprovider/gpt-5 --priority 10
|
|
105
|
+
nemu mappings list
|
|
106
|
+
nemu mappings delete <id> --yes
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Reading
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
nemu metrics list --limit 20 --start 2026-09-01T00:00:00Z
|
|
113
|
+
nemu metrics get <request-id>
|
|
114
|
+
nemu quotas list
|
|
115
|
+
nemu quotas update <id> --limit 5000
|
|
116
|
+
nemu indexes create https://example.com --max-pages 20
|
|
117
|
+
nemu workflows list
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Anything else
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
nemu api GET api/v1/workspaces
|
|
124
|
+
nemu api POST api/v1/providers --data '{"name":"x","priority":1}'
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`api` signs the request with your token and prints the raw JSON, so an endpoint with no dedicated
|
|
128
|
+
command is still one line away.
|
|
129
|
+
|
|
130
|
+
## Deleting
|
|
131
|
+
|
|
132
|
+
Every destructive command needs `--yes`. Without it the CLI refuses and tells you what it was about
|
|
133
|
+
to remove. There is no interactive prompt, because this is meant to be scriptable.
|
|
134
|
+
|
|
135
|
+
Some things the CLI deliberately cannot do. It never asks for `workspaces:delete` or `keys:write`, so
|
|
136
|
+
it cannot delete a workspace you reached through it or mint a gateway key. Workflows and metrics are
|
|
137
|
+
read only through the API. Quota rules can be read, changed and removed but not created.
|
|
138
|
+
|
|
139
|
+
## Where the token lives
|
|
140
|
+
|
|
141
|
+
`~/.nemu/auth.json`. On Linux and macOS it is written `0600`, readable only by you. Windows has no
|
|
142
|
+
equivalent mode, so it inherits the permissions of your user profile directory.
|
|
143
|
+
|
|
144
|
+
`nemu logout` deletes the file from this machine. It does not revoke anything on the server, so if the
|
|
145
|
+
machine is not yours, revoke the grant in your account settings as well.
|
|
146
|
+
|
|
147
|
+
## Environment
|
|
148
|
+
|
|
149
|
+
| Variable | Default | Purpose |
|
|
150
|
+
| --- | --- | --- |
|
|
151
|
+
| `NEMU_API_URL` | `https://app.nemu.cc` | Point the CLI at another console |
|
|
152
|
+
| `NEMU_CLIENT_ID` | `nemu_cli` | Use a different OAuth client |
|
|
153
|
+
| `NEMU_CONFIG_DIR` | `~/.nemu` | Store the token somewhere else |
|
|
154
|
+
| `NO_COLOR` | unset | Turn off colour |
|
|
155
|
+
|
|
156
|
+
## If something fails
|
|
157
|
+
|
|
158
|
+
`This build is not a recognised client` means the console you are pointed at does not know
|
|
159
|
+
`nemu_cli`. Check `NEMU_API_URL`.
|
|
160
|
+
|
|
161
|
+
`The code expired before it was approved` means more than 15 minutes passed. Run `nemu login` again.
|
|
162
|
+
|
|
163
|
+
`The request was rejected in the browser` means Reject was pressed. Nothing was authorized.
|
|
164
|
+
|
|
165
|
+
On Git Bash for Windows, a path beginning with `/` is rewritten into a Windows path before the CLI
|
|
166
|
+
sees it, so `nemu api GET /api/v1/user` fails. Drop the leading slash and use `api/v1/user`.
|
|
167
|
+
|
|
168
|
+
## Licence
|
|
169
|
+
|
|
170
|
+
MIT
|
package/dist/args.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export function parse_args(argv) {
|
|
2
|
+
const positional = [];
|
|
3
|
+
const flags = {};
|
|
4
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
5
|
+
const token = argv[i];
|
|
6
|
+
if (token === "--") {
|
|
7
|
+
positional.push(...argv.slice(i + 1));
|
|
8
|
+
break;
|
|
9
|
+
}
|
|
10
|
+
if (token.startsWith("--")) {
|
|
11
|
+
const body = token.slice(2);
|
|
12
|
+
const equals = body.indexOf("=");
|
|
13
|
+
if (equals !== -1) {
|
|
14
|
+
flags[body.slice(0, equals)] = body.slice(equals + 1);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const next = argv[i + 1];
|
|
18
|
+
if (next === undefined || next.startsWith("-")) {
|
|
19
|
+
flags[body] = true;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
flags[body] = next;
|
|
23
|
+
i += 1;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (token.startsWith("-") && token.length > 1) {
|
|
27
|
+
flags[token.slice(1)] = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
positional.push(token);
|
|
31
|
+
}
|
|
32
|
+
return { positional, flags };
|
|
33
|
+
}
|
|
34
|
+
export function flag(args, ...names) {
|
|
35
|
+
for (const name of names) {
|
|
36
|
+
const value = args.flags[name];
|
|
37
|
+
if (typeof value === "string")
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
export function bool_flag(args, ...names) {
|
|
43
|
+
for (const name of names) {
|
|
44
|
+
if (args.flags[name] === true)
|
|
45
|
+
return true;
|
|
46
|
+
const value = args.flags[name];
|
|
47
|
+
if (value === "true")
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
export function number_flag(args, ...names) {
|
|
53
|
+
const raw = flag(args, ...names);
|
|
54
|
+
if (raw === undefined)
|
|
55
|
+
return undefined;
|
|
56
|
+
const value = Number(raw);
|
|
57
|
+
if (!Number.isFinite(value)) {
|
|
58
|
+
throw new Error(`--${names[0]} must be a number, got "${raw}"`);
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
export function list_flag(args, ...names) {
|
|
63
|
+
const raw = flag(args, ...names);
|
|
64
|
+
if (raw === undefined)
|
|
65
|
+
return undefined;
|
|
66
|
+
return raw
|
|
67
|
+
.split(",")
|
|
68
|
+
.map((entry) => entry.trim())
|
|
69
|
+
.filter((entry) => entry !== "");
|
|
70
|
+
}
|
|
71
|
+
export function require_positional(args, index, name) {
|
|
72
|
+
const value = args.positional[index];
|
|
73
|
+
if (value === undefined || value === "") {
|
|
74
|
+
throw new Error(`Missing required argument: ${name}`);
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { ALL_SCOPES, SCOPE_ACTIONS, } from "@nemu-ai/sdk";
|
|
2
|
+
import { bool_flag, flag, number_flag, require_positional, } from "../args.js";
|
|
3
|
+
import { client } from "../session.js";
|
|
4
|
+
import { detail, done, emit, is_json_mode, table } from "../output.js";
|
|
5
|
+
import { api_url, API_VERSION } from "../config.js";
|
|
6
|
+
import { out } from "../ui.js";
|
|
7
|
+
export async function user() {
|
|
8
|
+
const nemu = await client();
|
|
9
|
+
const account = await nemu.user.get();
|
|
10
|
+
detail({ ...account }, "Account");
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
export async function health() {
|
|
14
|
+
const nemu = await client();
|
|
15
|
+
const status = await nemu.health.check();
|
|
16
|
+
detail({ ...status }, "Health");
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
export async function version_info() {
|
|
20
|
+
const nemu = await client();
|
|
21
|
+
const info = await nemu.version();
|
|
22
|
+
detail({ ...info }, "API version");
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
export function scopes() {
|
|
26
|
+
if (is_json_mode()) {
|
|
27
|
+
emit({ scopes: ALL_SCOPES, resources: SCOPE_ACTIONS });
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
table(Object.entries(SCOPE_ACTIONS).map(([resource, actions]) => ({
|
|
31
|
+
resource,
|
|
32
|
+
actions: actions.join(" "),
|
|
33
|
+
})), ["resource", "actions"]);
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
export async function indexes(args) {
|
|
37
|
+
const action = args.positional[0] ?? "list";
|
|
38
|
+
const nemu = await client();
|
|
39
|
+
if (action === "list") {
|
|
40
|
+
const rows = await nemu.indexes.list();
|
|
41
|
+
table(rows.map((row) => ({ ...row })), ["id", "base_url", "title", "page_count", "status", "created_at"], "No indexes.");
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
if (action === "get") {
|
|
45
|
+
const index = await nemu.indexes.get(require_positional(args, 1, "index id"));
|
|
46
|
+
detail({ ...index }, index.title ?? index.base_url);
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
if (action === "create") {
|
|
50
|
+
const url = flag(args, "url") ?? require_positional(args, 1, "url");
|
|
51
|
+
const created = await nemu.indexes.create({
|
|
52
|
+
url,
|
|
53
|
+
title: flag(args, "title"),
|
|
54
|
+
max_pages: number_flag(args, "max-pages"),
|
|
55
|
+
});
|
|
56
|
+
done(`Indexing ${url}`, { ...created });
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
if (action === "delete") {
|
|
60
|
+
const id = require_positional(args, 1, "index id");
|
|
61
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
62
|
+
throw new Error("Pass --yes to confirm deleting an index.");
|
|
63
|
+
}
|
|
64
|
+
await nemu.indexes.delete(id);
|
|
65
|
+
done(`Deleted index ${id}`);
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
throw new Error(`Unknown indexes action "${action}".`);
|
|
69
|
+
}
|
|
70
|
+
export async function quotas(args) {
|
|
71
|
+
const action = args.positional[0] ?? "list";
|
|
72
|
+
const nemu = await client();
|
|
73
|
+
if (action === "list") {
|
|
74
|
+
const rows = await nemu.quotas.list();
|
|
75
|
+
table(rows.map((row) => ({ ...row })), ["id", "name", "type", "limit", "period", "model_id", "enabled"], "No quota rules.");
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (action === "get") {
|
|
79
|
+
const quota = await nemu.quotas.get(require_positional(args, 1, "quota id"));
|
|
80
|
+
detail({ ...quota }, quota.name);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
if (action === "update") {
|
|
84
|
+
const id = require_positional(args, 1, "quota id");
|
|
85
|
+
if (id.startsWith("system_")) {
|
|
86
|
+
throw new Error("System quotas come from your plan and cannot be edited here.");
|
|
87
|
+
}
|
|
88
|
+
const quota = await nemu.quotas.get(id);
|
|
89
|
+
const period = flag(args, "period");
|
|
90
|
+
if (quota.model_id === null) {
|
|
91
|
+
throw new Error("This quota rule has no model and cannot be updated.");
|
|
92
|
+
}
|
|
93
|
+
await quota.update({
|
|
94
|
+
name: flag(args, "name") ?? quota.name,
|
|
95
|
+
type: quota.type,
|
|
96
|
+
model_id: flag(args, "model", "model-id") ?? quota.model_id,
|
|
97
|
+
limit: number_flag(args, "limit") ?? quota.limit,
|
|
98
|
+
...(period !== undefined ? { period: period } : {}),
|
|
99
|
+
...(args.flags.enabled !== undefined
|
|
100
|
+
? { enabled: args.flags.enabled !== "false" }
|
|
101
|
+
: {}),
|
|
102
|
+
});
|
|
103
|
+
done(`Updated quota ${quota.id}`, { ...quota });
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
if (action === "delete") {
|
|
107
|
+
const quota = await nemu.quotas.get(require_positional(args, 1, "quota id"));
|
|
108
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
109
|
+
throw new Error("Pass --yes to confirm deleting a quota rule.");
|
|
110
|
+
}
|
|
111
|
+
await quota.delete();
|
|
112
|
+
done(`Deleted quota ${quota.id}`);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
throw new Error(`Unknown quotas action "${action}". The API cannot create quota rules, only read, update and delete them.`);
|
|
116
|
+
}
|
|
117
|
+
export async function metrics(args) {
|
|
118
|
+
const action = args.positional[0] ?? "list";
|
|
119
|
+
const nemu = await client();
|
|
120
|
+
if (action === "list") {
|
|
121
|
+
const response = await nemu.metrics.list({
|
|
122
|
+
page: number_flag(args, "page"),
|
|
123
|
+
limit: number_flag(args, "limit"),
|
|
124
|
+
key_id: flag(args, "key", "key-id"),
|
|
125
|
+
start_date: flag(args, "start", "start-date"),
|
|
126
|
+
end_date: flag(args, "end", "end-date"),
|
|
127
|
+
});
|
|
128
|
+
const rows = response.data ?? [];
|
|
129
|
+
table(rows.map((row) => ({ ...row })), [
|
|
130
|
+
"id",
|
|
131
|
+
"model",
|
|
132
|
+
"status",
|
|
133
|
+
"total_tokens",
|
|
134
|
+
"cost",
|
|
135
|
+
"latency_ms",
|
|
136
|
+
"created_at",
|
|
137
|
+
], "No requests recorded.");
|
|
138
|
+
if (!is_json_mode() && response.pagination) {
|
|
139
|
+
const { page, pages, total } = response.pagination;
|
|
140
|
+
out(` page ${page} of ${pages}, ${total} total`);
|
|
141
|
+
}
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
if (action === "get") {
|
|
145
|
+
const metric = await nemu.metrics.get(require_positional(args, 1, "request id"));
|
|
146
|
+
detail({ ...metric }, "Request");
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
throw new Error(`Unknown metrics action "${action}".`);
|
|
150
|
+
}
|
|
151
|
+
export async function raw_api(args) {
|
|
152
|
+
const method = (args.positional[0] ?? require_positional(args, 0, "method")).toUpperCase();
|
|
153
|
+
const path = require_positional(args, 1, "path");
|
|
154
|
+
const body = flag(args, "data", "d");
|
|
155
|
+
const { read_credentials } = await import("../config.js");
|
|
156
|
+
const credentials = await read_credentials();
|
|
157
|
+
if (credentials === null)
|
|
158
|
+
throw new Error("Not signed in. Run nemu login.");
|
|
159
|
+
const base = (credentials.api_url ?? api_url()).replace(/\/+$/, "");
|
|
160
|
+
const url = path.startsWith("http")
|
|
161
|
+
? path
|
|
162
|
+
: `${base}${path.startsWith("/") ? "" : "/"}${path}`;
|
|
163
|
+
let response;
|
|
164
|
+
try {
|
|
165
|
+
response = await fetch(url, {
|
|
166
|
+
method,
|
|
167
|
+
headers: {
|
|
168
|
+
authorization: `Bearer ${credentials.access_token}`,
|
|
169
|
+
"content-type": "application/json",
|
|
170
|
+
"Nemu-Api-Version": API_VERSION,
|
|
171
|
+
},
|
|
172
|
+
body: body === undefined ? undefined : body,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
177
|
+
throw new Error(`${method} ${url} could not be reached: ${reason}`);
|
|
178
|
+
}
|
|
179
|
+
const text = await response.text();
|
|
180
|
+
try {
|
|
181
|
+
emit(JSON.parse(text));
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
out(text);
|
|
185
|
+
}
|
|
186
|
+
return response.ok ? 0 : 1;
|
|
187
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { NemuDeviceError } from "@nemu-ai/sdk";
|
|
2
|
+
import { api_url, is_expired, read_credentials, write_credentials, } from "../config.js";
|
|
3
|
+
import { device_client } from "../session.js";
|
|
4
|
+
import { bool_flag } from "../args.js";
|
|
5
|
+
import { done, is_json_mode } from "../output.js";
|
|
6
|
+
import { bold, cyan, dim, open_browser, out, spinner, success, } from "../ui.js";
|
|
7
|
+
async function account_name(nemu) {
|
|
8
|
+
try {
|
|
9
|
+
const account = await nemu.user.get();
|
|
10
|
+
return account.username ?? account.email ?? null;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export async function login(args) {
|
|
17
|
+
const url = api_url();
|
|
18
|
+
const force = bool_flag(args, "force", "f");
|
|
19
|
+
const no_browser = bool_flag(args, "no-browser");
|
|
20
|
+
const existing = await read_credentials();
|
|
21
|
+
if (existing !== null && !is_expired(existing) && !force) {
|
|
22
|
+
const who = existing.account ?? "an existing session";
|
|
23
|
+
if (is_json_mode()) {
|
|
24
|
+
done("Already signed in", { account: existing.account });
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
out(`Already signed in as ${bold(who)}.`);
|
|
28
|
+
out(dim("Run nemu login --force to sign in again."));
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
const device = device_client(url);
|
|
32
|
+
const authorization = await device.request_code();
|
|
33
|
+
if (is_json_mode()) {
|
|
34
|
+
out(JSON.stringify({
|
|
35
|
+
verification_uri: authorization.verification_uri,
|
|
36
|
+
user_code: authorization.user_code,
|
|
37
|
+
expires_in: authorization.expires_in,
|
|
38
|
+
}, null, 2));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
out();
|
|
42
|
+
out(` Open ${cyan(authorization.verification_uri)}`);
|
|
43
|
+
out(` Code ${bold(authorization.user_code)}`);
|
|
44
|
+
out();
|
|
45
|
+
if (!no_browser) {
|
|
46
|
+
open_browser(authorization.verification_uri_complete);
|
|
47
|
+
out(dim(" Opening your browser. Use the link above if it does not."));
|
|
48
|
+
out();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const wait = is_json_mode()
|
|
52
|
+
? { stop: () => { } }
|
|
53
|
+
: spinner("Waiting for approval...");
|
|
54
|
+
let nemu;
|
|
55
|
+
try {
|
|
56
|
+
nemu = await device.poll(authorization);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
wait.stop();
|
|
60
|
+
throw error instanceof NemuDeviceError
|
|
61
|
+
? new Error(error.message)
|
|
62
|
+
: new Error("Sign in failed.");
|
|
63
|
+
}
|
|
64
|
+
wait.stop();
|
|
65
|
+
const tokens = nemu.tokens;
|
|
66
|
+
if (tokens === undefined)
|
|
67
|
+
throw new Error("No token was issued.");
|
|
68
|
+
const account = await account_name(nemu);
|
|
69
|
+
const path = await write_credentials({
|
|
70
|
+
access_token: tokens.access_token,
|
|
71
|
+
refresh_token: tokens.refresh_token,
|
|
72
|
+
expires_at: tokens.expires_at ?? Date.now() + 3_600_000,
|
|
73
|
+
scope: tokens.scope ?? "",
|
|
74
|
+
api_url: url,
|
|
75
|
+
account: account ?? undefined,
|
|
76
|
+
});
|
|
77
|
+
if (is_json_mode()) {
|
|
78
|
+
done("Logged in", { account, path });
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
success(`Logged in${account !== null ? ` as ${bold(account)}` : ""}`);
|
|
82
|
+
out(dim(` Token saved to ${path}`));
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { clear_credentials, read_credentials } from "../config.js";
|
|
2
|
+
import { bold, dim, out, success } from "../ui.js";
|
|
3
|
+
export async function logout() {
|
|
4
|
+
const existing = await read_credentials();
|
|
5
|
+
if (existing === null) {
|
|
6
|
+
out("You are not signed in.");
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
const removed = await clear_credentials();
|
|
10
|
+
if (!removed) {
|
|
11
|
+
out("You are not signed in.");
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
const who = existing.account;
|
|
15
|
+
success(`Signed out${who !== undefined ? ` of ${bold(who)}` : ""}`);
|
|
16
|
+
out(dim(" The stored token has been deleted from this machine."));
|
|
17
|
+
out(dim(" Revoke it for good in your account settings."));
|
|
18
|
+
return 0;
|
|
19
|
+
}
|