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
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { bool_flag, flag, number_flag, require_positional, } from "../args.js";
|
|
2
|
+
import { client, resolve_workspace } from "../session.js";
|
|
3
|
+
import { detail, done, table } from "../output.js";
|
|
4
|
+
function optional_bool(args, name) {
|
|
5
|
+
const raw = flag(args, name);
|
|
6
|
+
if (raw === undefined) {
|
|
7
|
+
return args.flags[name] === true ? true : undefined;
|
|
8
|
+
}
|
|
9
|
+
if (raw === "true")
|
|
10
|
+
return true;
|
|
11
|
+
if (raw === "false")
|
|
12
|
+
return false;
|
|
13
|
+
throw new Error(`--${name} must be true or false, got "${raw}"`);
|
|
14
|
+
}
|
|
15
|
+
function strip_undefined(value) {
|
|
16
|
+
const result = {};
|
|
17
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
18
|
+
if (entry !== undefined)
|
|
19
|
+
result[key] = entry;
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
const PROVIDER_COLUMNS = [
|
|
24
|
+
"id",
|
|
25
|
+
"name",
|
|
26
|
+
"compatibility",
|
|
27
|
+
"priority",
|
|
28
|
+
"base_url",
|
|
29
|
+
"enabled",
|
|
30
|
+
];
|
|
31
|
+
export async function providers(args) {
|
|
32
|
+
const action = args.positional[0] ?? "list";
|
|
33
|
+
const nemu = await client();
|
|
34
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
35
|
+
if (action === "list") {
|
|
36
|
+
const rows = await workspace.providers.list(strip_undefined({
|
|
37
|
+
name: flag(args, "name"),
|
|
38
|
+
compatibility: flag(args, "compatibility"),
|
|
39
|
+
}));
|
|
40
|
+
table(rows.map((row) => ({ ...row })), PROVIDER_COLUMNS, "No providers.");
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
if (action === "get") {
|
|
44
|
+
const provider = await workspace.providers.get(require_positional(args, 1, "provider id"));
|
|
45
|
+
detail({ ...provider }, provider.name);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
if (action === "create") {
|
|
49
|
+
const name = flag(args, "name") ?? require_positional(args, 1, "name");
|
|
50
|
+
const provider = await workspace.providers.create(strip_undefined({
|
|
51
|
+
name,
|
|
52
|
+
priority: number_flag(args, "priority") ?? 1,
|
|
53
|
+
base_url: flag(args, "base-url"),
|
|
54
|
+
api_key: flag(args, "api-key"),
|
|
55
|
+
compatibility: flag(args, "compatibility"),
|
|
56
|
+
models_dev_id: flag(args, "models-dev-id"),
|
|
57
|
+
user_agent: flag(args, "user-agent"),
|
|
58
|
+
source: flag(args, "source"),
|
|
59
|
+
}));
|
|
60
|
+
done(`Created provider ${provider.name}`, { ...provider });
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (action === "update") {
|
|
64
|
+
const provider = await workspace.providers.get(require_positional(args, 1, "provider id"));
|
|
65
|
+
await provider.update(strip_undefined({
|
|
66
|
+
name: flag(args, "name") ?? provider.name,
|
|
67
|
+
priority: number_flag(args, "priority") ?? provider.priority,
|
|
68
|
+
base_url: flag(args, "base-url") ?? provider.base_url ?? undefined,
|
|
69
|
+
api_key: flag(args, "api-key"),
|
|
70
|
+
compatibility: flag(args, "compatibility") ??
|
|
71
|
+
provider.compatibility,
|
|
72
|
+
user_agent: flag(args, "user-agent"),
|
|
73
|
+
enabled: optional_bool(args, "enabled"),
|
|
74
|
+
}));
|
|
75
|
+
done(`Updated provider ${provider.id}`, { ...provider });
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (action === "delete") {
|
|
79
|
+
const provider = await workspace.providers.get(require_positional(args, 1, "provider id"));
|
|
80
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
81
|
+
throw new Error("Pass --yes to confirm deleting a provider.");
|
|
82
|
+
}
|
|
83
|
+
await provider.delete();
|
|
84
|
+
done(`Deleted provider ${provider.id}`);
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`Unknown providers action "${action}".`);
|
|
88
|
+
}
|
|
89
|
+
const MODEL_COLUMNS = [
|
|
90
|
+
"id",
|
|
91
|
+
"name",
|
|
92
|
+
"display_name",
|
|
93
|
+
"type",
|
|
94
|
+
"provider_id",
|
|
95
|
+
"input_cost",
|
|
96
|
+
"output_cost",
|
|
97
|
+
"context_window",
|
|
98
|
+
"enabled",
|
|
99
|
+
];
|
|
100
|
+
export async function models(args) {
|
|
101
|
+
const action = args.positional[0] ?? "list";
|
|
102
|
+
const nemu = await client();
|
|
103
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
104
|
+
if (action === "list") {
|
|
105
|
+
const rows = await workspace.models.list(strip_undefined({
|
|
106
|
+
type: flag(args, "type"),
|
|
107
|
+
provider_id: flag(args, "provider", "provider-id"),
|
|
108
|
+
name: flag(args, "name"),
|
|
109
|
+
}));
|
|
110
|
+
table(rows.map((row) => ({ ...row })), MODEL_COLUMNS, "No models.");
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
if (action === "get") {
|
|
114
|
+
const model = await workspace.models.get(require_positional(args, 1, "model id"));
|
|
115
|
+
detail({ ...model }, model.display_name ?? model.name);
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
if (action === "create") {
|
|
119
|
+
const model_name = flag(args, "name") ?? require_positional(args, 1, "name");
|
|
120
|
+
const provider_id = flag(args, "provider", "provider-id");
|
|
121
|
+
if (provider_id === undefined) {
|
|
122
|
+
throw new Error("--provider <id> is required. Run: nemu providers list");
|
|
123
|
+
}
|
|
124
|
+
const model = await workspace.models.create(strip_undefined({
|
|
125
|
+
name: model_name,
|
|
126
|
+
display_name: flag(args, "display-name") ?? model_name,
|
|
127
|
+
api_name: flag(args, "api-name"),
|
|
128
|
+
provider_id,
|
|
129
|
+
type: (flag(args, "type") ?? "chat"),
|
|
130
|
+
input_cost: number_flag(args, "input-cost") ?? 0,
|
|
131
|
+
output_cost: number_flag(args, "output-cost") ?? 0,
|
|
132
|
+
context_window: number_flag(args, "context-window") ?? 8192,
|
|
133
|
+
cache_read_cost: number_flag(args, "cache-read-cost"),
|
|
134
|
+
cache_write_cost: number_flag(args, "cache-write-cost"),
|
|
135
|
+
max_output: number_flag(args, "max-output"),
|
|
136
|
+
per_image_cost: number_flag(args, "per-image-cost"),
|
|
137
|
+
per_minute_cost: number_flag(args, "per-minute-cost"),
|
|
138
|
+
supports_vision: optional_bool(args, "supports-vision"),
|
|
139
|
+
supports_audio_input: optional_bool(args, "supports-audio-input"),
|
|
140
|
+
supports_audio_output: optional_bool(args, "supports-audio-output"),
|
|
141
|
+
max_images_per_request: number_flag(args, "max-images-per-request"),
|
|
142
|
+
}));
|
|
143
|
+
done(`Created model ${model.name}`, { ...model });
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
if (action === "update") {
|
|
147
|
+
const model = await workspace.models.get(require_positional(args, 1, "model id"));
|
|
148
|
+
await model.update(strip_undefined({
|
|
149
|
+
name: model.name,
|
|
150
|
+
provider_id: model.provider_id,
|
|
151
|
+
type: model.type,
|
|
152
|
+
display_name: flag(args, "display-name") ?? model.display_name ?? model.name,
|
|
153
|
+
api_name: flag(args, "api-name") ?? model.api_name ?? undefined,
|
|
154
|
+
input_cost: number_flag(args, "input-cost") ?? model.input_cost ?? 0,
|
|
155
|
+
output_cost: number_flag(args, "output-cost") ?? model.output_cost ?? 0,
|
|
156
|
+
context_window: number_flag(args, "context-window") ?? model.context_window ?? 8192,
|
|
157
|
+
cache_read_cost: number_flag(args, "cache-read-cost"),
|
|
158
|
+
cache_write_cost: number_flag(args, "cache-write-cost"),
|
|
159
|
+
max_output: number_flag(args, "max-output"),
|
|
160
|
+
enabled: optional_bool(args, "enabled"),
|
|
161
|
+
}));
|
|
162
|
+
done(`Updated model ${model.id}`, { ...model });
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
if (action === "delete") {
|
|
166
|
+
const model = await workspace.models.get(require_positional(args, 1, "model id"));
|
|
167
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
168
|
+
throw new Error("Pass --yes to confirm deleting a model.");
|
|
169
|
+
}
|
|
170
|
+
await model.delete();
|
|
171
|
+
done(`Deleted model ${model.id}`);
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
throw new Error(`Unknown models action "${action}".`);
|
|
175
|
+
}
|
|
176
|
+
const PROMPT_COLUMNS = [
|
|
177
|
+
"id",
|
|
178
|
+
"name",
|
|
179
|
+
"type",
|
|
180
|
+
"environment",
|
|
181
|
+
"position",
|
|
182
|
+
"is_global",
|
|
183
|
+
"enabled",
|
|
184
|
+
];
|
|
185
|
+
export async function prompts(args) {
|
|
186
|
+
const action = args.positional[0] ?? "list";
|
|
187
|
+
const nemu = await client();
|
|
188
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
189
|
+
if (action === "list") {
|
|
190
|
+
const rows = await workspace.prompts.list(strip_undefined({
|
|
191
|
+
name: flag(args, "name"),
|
|
192
|
+
type: flag(args, "type"),
|
|
193
|
+
environment: flag(args, "environment"),
|
|
194
|
+
}));
|
|
195
|
+
table(rows.map((row) => ({ ...row })), PROMPT_COLUMNS, "No prompts.");
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
if (action === "get") {
|
|
199
|
+
const prompt = await workspace.prompts.get(require_positional(args, 1, "prompt id"));
|
|
200
|
+
detail({ ...prompt }, prompt.name);
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
if (action === "create") {
|
|
204
|
+
const prompt = await workspace.prompts.create(strip_undefined({
|
|
205
|
+
name: flag(args, "name") ?? require_positional(args, 1, "name"),
|
|
206
|
+
content: flag(args, "content") ?? require_positional(args, 2, "content"),
|
|
207
|
+
type: flag(args, "type"),
|
|
208
|
+
model_id: flag(args, "model", "model-id"),
|
|
209
|
+
is_global: optional_bool(args, "global"),
|
|
210
|
+
enabled: optional_bool(args, "enabled"),
|
|
211
|
+
position: number_flag(args, "position"),
|
|
212
|
+
environment: flag(args, "environment"),
|
|
213
|
+
}));
|
|
214
|
+
done(`Created prompt ${prompt.name}`, { ...prompt });
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
if (action === "update") {
|
|
218
|
+
const prompt = await workspace.prompts.get(require_positional(args, 1, "prompt id"));
|
|
219
|
+
await prompt.update(strip_undefined({
|
|
220
|
+
name: flag(args, "name") ?? prompt.name,
|
|
221
|
+
content: flag(args, "content") ?? prompt.content,
|
|
222
|
+
type: flag(args, "type") ?? prompt.type,
|
|
223
|
+
position: number_flag(args, "position") ?? prompt.position,
|
|
224
|
+
environment: flag(args, "environment"),
|
|
225
|
+
enabled: optional_bool(args, "enabled"),
|
|
226
|
+
is_global: optional_bool(args, "global"),
|
|
227
|
+
}));
|
|
228
|
+
done(`Updated prompt ${prompt.id}`, { ...prompt });
|
|
229
|
+
return 0;
|
|
230
|
+
}
|
|
231
|
+
if (action === "delete") {
|
|
232
|
+
const prompt = await workspace.prompts.get(require_positional(args, 1, "prompt id"));
|
|
233
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
234
|
+
throw new Error("Pass --yes to confirm deleting a prompt.");
|
|
235
|
+
}
|
|
236
|
+
await prompt.delete();
|
|
237
|
+
done(`Deleted prompt ${prompt.id}`);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
240
|
+
throw new Error(`Unknown prompts action "${action}".`);
|
|
241
|
+
}
|
|
242
|
+
const MAPPING_COLUMNS = [
|
|
243
|
+
"id",
|
|
244
|
+
"source_name",
|
|
245
|
+
"target_model",
|
|
246
|
+
"priority",
|
|
247
|
+
"is_pattern",
|
|
248
|
+
"enabled",
|
|
249
|
+
];
|
|
250
|
+
export async function mappings(args) {
|
|
251
|
+
const action = args.positional[0] ?? "list";
|
|
252
|
+
const nemu = await client();
|
|
253
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
254
|
+
if (action === "list") {
|
|
255
|
+
const rows = await workspace.mappings.list(strip_undefined({
|
|
256
|
+
source_name: flag(args, "source", "source-name"),
|
|
257
|
+
target_model: flag(args, "target", "target-model"),
|
|
258
|
+
}));
|
|
259
|
+
table(rows.map((row) => ({ ...row })), MAPPING_COLUMNS, "No mappings.");
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
if (action === "get") {
|
|
263
|
+
const mapping = await workspace.mappings.get(require_positional(args, 1, "mapping id"));
|
|
264
|
+
detail({ ...mapping }, mapping.source_name);
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
if (action === "create") {
|
|
268
|
+
const mapping = await workspace.mappings.create(strip_undefined({
|
|
269
|
+
source_name: flag(args, "source", "source-name") ??
|
|
270
|
+
require_positional(args, 1, "source name"),
|
|
271
|
+
target_model: flag(args, "target", "target-model") ??
|
|
272
|
+
require_positional(args, 2, "target model"),
|
|
273
|
+
priority: number_flag(args, "priority"),
|
|
274
|
+
enabled: optional_bool(args, "enabled"),
|
|
275
|
+
is_pattern: optional_bool(args, "pattern"),
|
|
276
|
+
}));
|
|
277
|
+
done(`Created mapping ${mapping.source_name}`, { ...mapping });
|
|
278
|
+
return 0;
|
|
279
|
+
}
|
|
280
|
+
if (action === "update") {
|
|
281
|
+
const mapping = await workspace.mappings.get(require_positional(args, 1, "mapping id"));
|
|
282
|
+
await mapping.update(strip_undefined({
|
|
283
|
+
source_name: flag(args, "source", "source-name") ?? mapping.source_name,
|
|
284
|
+
target_model: flag(args, "target", "target-model") ?? mapping.target_model,
|
|
285
|
+
priority: number_flag(args, "priority") ?? mapping.priority,
|
|
286
|
+
enabled: optional_bool(args, "enabled"),
|
|
287
|
+
is_pattern: optional_bool(args, "pattern"),
|
|
288
|
+
}));
|
|
289
|
+
done(`Updated mapping ${mapping.id}`, { ...mapping });
|
|
290
|
+
return 0;
|
|
291
|
+
}
|
|
292
|
+
if (action === "delete") {
|
|
293
|
+
const mapping = await workspace.mappings.get(require_positional(args, 1, "mapping id"));
|
|
294
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
295
|
+
throw new Error("Pass --yes to confirm deleting a mapping.");
|
|
296
|
+
}
|
|
297
|
+
await mapping.delete();
|
|
298
|
+
done(`Deleted mapping ${mapping.id}`);
|
|
299
|
+
return 0;
|
|
300
|
+
}
|
|
301
|
+
throw new Error(`Unknown mappings action "${action}".`);
|
|
302
|
+
}
|
|
303
|
+
export async function workflows(args) {
|
|
304
|
+
const action = args.positional[0] ?? "list";
|
|
305
|
+
const nemu = await client();
|
|
306
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
307
|
+
if (action === "list") {
|
|
308
|
+
const rows = await workspace.workflows.list(strip_undefined({ name: flag(args, "name") }));
|
|
309
|
+
table(rows.map((row) => ({ ...row })), ["id", "name", "enabled", "created_at"], "No workflows.");
|
|
310
|
+
return 0;
|
|
311
|
+
}
|
|
312
|
+
if (action === "get") {
|
|
313
|
+
const workflow = await workspace.workflows.get(require_positional(args, 1, "workflow id"));
|
|
314
|
+
detail({ ...workflow }, workflow.name);
|
|
315
|
+
return 0;
|
|
316
|
+
}
|
|
317
|
+
throw new Error(`Workflows are read only through the API. Known actions: list, get.`);
|
|
318
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { is_expired, read_credentials } from "../config.js";
|
|
2
|
+
import { client } from "../session.js";
|
|
3
|
+
import { detail, is_json_mode } from "../output.js";
|
|
4
|
+
import { bold, dim, out } from "../ui.js";
|
|
5
|
+
export async function whoami() {
|
|
6
|
+
const credentials = await read_credentials();
|
|
7
|
+
if (credentials === null) {
|
|
8
|
+
if (is_json_mode()) {
|
|
9
|
+
detail({ signed_in: false });
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
out("Not signed in. Run nemu login.");
|
|
13
|
+
return 1;
|
|
14
|
+
}
|
|
15
|
+
if (is_expired(credentials) && credentials.refresh_token === undefined) {
|
|
16
|
+
out("Your session has expired. Run nemu login.");
|
|
17
|
+
return 1;
|
|
18
|
+
}
|
|
19
|
+
const nemu = await client();
|
|
20
|
+
const account = await nemu.user.get();
|
|
21
|
+
if (is_json_mode()) {
|
|
22
|
+
detail({
|
|
23
|
+
signed_in: true,
|
|
24
|
+
username: account.username,
|
|
25
|
+
email: account.email,
|
|
26
|
+
api_url: credentials.api_url,
|
|
27
|
+
scope: credentials.scope,
|
|
28
|
+
});
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
out(bold(account.username ?? account.email ?? "unknown"));
|
|
32
|
+
out(dim(` ${credentials.api_url}`));
|
|
33
|
+
out(dim(` scope ${credentials.scope || "none"}`));
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { bool_flag, flag, number_flag, require_positional, } from "../args.js";
|
|
2
|
+
import { client, resolve_workspace } from "../session.js";
|
|
3
|
+
import { detail, done, table } from "../output.js";
|
|
4
|
+
const COLUMNS = ["id", "name", "budget", "created_at"];
|
|
5
|
+
export async function workspaces(args) {
|
|
6
|
+
const action = args.positional[0] ?? "list";
|
|
7
|
+
const nemu = await client();
|
|
8
|
+
if (action === "list") {
|
|
9
|
+
const rows = await nemu.workspaces.list();
|
|
10
|
+
table(rows.map((row) => ({ ...row })), bool_flag(args, "stats") ? [...COLUMNS, "stats"] : COLUMNS, "No workspaces.");
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
if (action === "get") {
|
|
14
|
+
const id = require_positional(args, 1, "workspace id");
|
|
15
|
+
const workspace = await nemu.workspaces.get(id);
|
|
16
|
+
detail({ ...workspace }, workspace.name);
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
if (action === "create") {
|
|
20
|
+
const name = flag(args, "name") ?? require_positional(args, 1, "name");
|
|
21
|
+
const workspace = await nemu.workspaces.create({ name });
|
|
22
|
+
done(`Created workspace ${workspace.name}`, { ...workspace });
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
if (action === "rename" || action === "update") {
|
|
26
|
+
const id = require_positional(args, 1, "workspace id");
|
|
27
|
+
const name = flag(args, "name") ?? require_positional(args, 2, "new name");
|
|
28
|
+
const workspace = await nemu.workspaces.get(id);
|
|
29
|
+
await workspace.update({ name });
|
|
30
|
+
done(`Renamed to ${name}`, { ...workspace });
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
if (action === "delete") {
|
|
34
|
+
const id = require_positional(args, 1, "workspace id");
|
|
35
|
+
if (!bool_flag(args, "yes", "y")) {
|
|
36
|
+
throw new Error("Deleting a workspace removes everything in it. Pass --yes to confirm.");
|
|
37
|
+
}
|
|
38
|
+
const workspace = await nemu.workspaces.get(id);
|
|
39
|
+
await workspace.delete();
|
|
40
|
+
done(`Deleted workspace ${id}`);
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
if (action === "budget") {
|
|
44
|
+
const workspace = await resolve_workspace(nemu, args.positional[1] ?? flag(args, "workspace", "w"));
|
|
45
|
+
const budget = await workspace.get_budget();
|
|
46
|
+
detail({ workspace: workspace.name, ...budget }, "Budget");
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
if (action === "set-budget") {
|
|
50
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w") ?? args.positional[1]);
|
|
51
|
+
const amount = number_flag(args, "amount") ??
|
|
52
|
+
Number(args.positional[2] ?? args.positional[1]);
|
|
53
|
+
if (!Number.isFinite(amount)) {
|
|
54
|
+
throw new Error("Pass the new budget, for example: set-budget <id> 50");
|
|
55
|
+
}
|
|
56
|
+
const applied = await workspace.set_budget(amount);
|
|
57
|
+
done(`Budget for ${workspace.name} is now ${applied}`, {
|
|
58
|
+
workspace_id: workspace.id,
|
|
59
|
+
budget: applied,
|
|
60
|
+
});
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (action === "gateway-key") {
|
|
64
|
+
throw new Error("Minting a gateway key needs a confidential client, so the CLI cannot do it. Use the console.");
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Unknown workspaces action "${action}".`);
|
|
67
|
+
}
|
|
68
|
+
const MEMBER_COLUMNS = ["id", "user_id", "email", "role", "created_at"];
|
|
69
|
+
export async function members(args) {
|
|
70
|
+
const action = args.positional[0] ?? "list";
|
|
71
|
+
const nemu = await client();
|
|
72
|
+
const workspace = await resolve_workspace(nemu, flag(args, "workspace", "w"));
|
|
73
|
+
if (action === "list") {
|
|
74
|
+
const rows = await workspace.members.list();
|
|
75
|
+
table(rows.map((row) => ({ ...row })), MEMBER_COLUMNS, "No members.");
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (action === "invite") {
|
|
79
|
+
const emails = flag(args, "email", "emails");
|
|
80
|
+
const invitation = await workspace.members.invite({
|
|
81
|
+
restricted_to: emails === undefined
|
|
82
|
+
? undefined
|
|
83
|
+
: emails.split(",").map((entry) => entry.trim()),
|
|
84
|
+
max_uses: number_flag(args, "max-uses"),
|
|
85
|
+
expires_in_days: number_flag(args, "expires-in-days", "expires"),
|
|
86
|
+
});
|
|
87
|
+
detail({ ...invitation }, "Invitation");
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
if (action === "role") {
|
|
91
|
+
const member_id = require_positional(args, 1, "member id");
|
|
92
|
+
const role = (flag(args, "role") ?? require_positional(args, 2, "role")).toUpperCase();
|
|
93
|
+
if (role !== "ADMIN" && role !== "MEMBER") {
|
|
94
|
+
throw new Error('Role must be ADMIN or MEMBER.');
|
|
95
|
+
}
|
|
96
|
+
const list = await workspace.members.list();
|
|
97
|
+
const member = list.find((entry) => entry.id === member_id);
|
|
98
|
+
if (!member)
|
|
99
|
+
throw new Error(`No member ${member_id} in ${workspace.name}.`);
|
|
100
|
+
await member.update_role(role);
|
|
101
|
+
done(`${member_id} is now ${role}`);
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
if (action === "remove") {
|
|
105
|
+
const member_id = require_positional(args, 1, "member id");
|
|
106
|
+
const list = await workspace.members.list();
|
|
107
|
+
const member = list.find((entry) => entry.id === member_id);
|
|
108
|
+
if (!member)
|
|
109
|
+
throw new Error(`No member ${member_id} in ${workspace.name}.`);
|
|
110
|
+
await member.remove();
|
|
111
|
+
done(`Removed ${member_id} from ${workspace.name}`);
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
throw new Error(`Unknown members action "${action}".`);
|
|
115
|
+
}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { mkdir, readFile, writeFile, rm, chmod } from "node:fs/promises";
|
|
4
|
+
export const DEFAULT_API_URL = "https://app.nemu.cc";
|
|
5
|
+
export const DEFAULT_CLIENT_ID = "nemu_cli";
|
|
6
|
+
export const API_VERSION = "2026-07-14";
|
|
7
|
+
export function api_url() {
|
|
8
|
+
return process.env.NEMU_API_URL ?? DEFAULT_API_URL;
|
|
9
|
+
}
|
|
10
|
+
export function client_id() {
|
|
11
|
+
return process.env.NEMU_CLIENT_ID ?? DEFAULT_CLIENT_ID;
|
|
12
|
+
}
|
|
13
|
+
export function config_dir() {
|
|
14
|
+
return process.env.NEMU_CONFIG_DIR ?? join(homedir(), ".nemu");
|
|
15
|
+
}
|
|
16
|
+
function credentials_path() {
|
|
17
|
+
return join(config_dir(), "auth.json");
|
|
18
|
+
}
|
|
19
|
+
export async function read_credentials() {
|
|
20
|
+
try {
|
|
21
|
+
const raw = await readFile(credentials_path(), "utf-8");
|
|
22
|
+
const parsed = JSON.parse(raw);
|
|
23
|
+
if (typeof parsed.access_token !== "string")
|
|
24
|
+
return null;
|
|
25
|
+
return parsed;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function write_credentials(credentials) {
|
|
32
|
+
const directory = config_dir();
|
|
33
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
34
|
+
const path = credentials_path();
|
|
35
|
+
await writeFile(path, JSON.stringify(credentials, null, 2), {
|
|
36
|
+
encoding: "utf-8",
|
|
37
|
+
mode: 0o600,
|
|
38
|
+
});
|
|
39
|
+
try {
|
|
40
|
+
await chmod(path, 0o600);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return path;
|
|
44
|
+
}
|
|
45
|
+
return path;
|
|
46
|
+
}
|
|
47
|
+
export async function clear_credentials() {
|
|
48
|
+
try {
|
|
49
|
+
await rm(credentials_path());
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function is_expired(credentials) {
|
|
57
|
+
return Date.now() >= credentials.expires_at;
|
|
58
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parse_args, bool_flag } from "./args.js";
|
|
3
|
+
import { set_json_mode, is_json_mode, emit } from "./output.js";
|
|
4
|
+
import { login } from "./commands/login.js";
|
|
5
|
+
import { logout } from "./commands/logout.js";
|
|
6
|
+
import { whoami } from "./commands/whoami.js";
|
|
7
|
+
import { members, workspaces } from "./commands/workspaces.js";
|
|
8
|
+
import { mappings, models, prompts, providers, workflows, } from "./commands/resources.js";
|
|
9
|
+
import { health, indexes, metrics, quotas, raw_api, scopes, user, version_info, } from "./commands/account.js";
|
|
10
|
+
import { bold, dim, fail, out } from "./ui.js";
|
|
11
|
+
const VERSION = "1.0.0";
|
|
12
|
+
const COMMANDS = {
|
|
13
|
+
login,
|
|
14
|
+
logout: () => logout(),
|
|
15
|
+
whoami: () => whoami(),
|
|
16
|
+
workspaces,
|
|
17
|
+
ws: workspaces,
|
|
18
|
+
members,
|
|
19
|
+
providers,
|
|
20
|
+
models,
|
|
21
|
+
prompts,
|
|
22
|
+
mappings,
|
|
23
|
+
workflows,
|
|
24
|
+
indexes,
|
|
25
|
+
quotas,
|
|
26
|
+
metrics,
|
|
27
|
+
user: () => user(),
|
|
28
|
+
account: () => user(),
|
|
29
|
+
health: () => health(),
|
|
30
|
+
"api-version": () => version_info(),
|
|
31
|
+
scopes: () => scopes(),
|
|
32
|
+
api: raw_api,
|
|
33
|
+
};
|
|
34
|
+
const HELP = `
|
|
35
|
+
${bold("nemu")} ${dim(VERSION)}
|
|
36
|
+
|
|
37
|
+
${bold("Account")}
|
|
38
|
+
login [--force] [--no-browser] Sign in with the device flow
|
|
39
|
+
logout Delete the stored token
|
|
40
|
+
whoami Show the signed in account
|
|
41
|
+
user Full account record
|
|
42
|
+
scopes Every scope and its actions
|
|
43
|
+
|
|
44
|
+
${bold("Workspaces")}
|
|
45
|
+
workspaces list [--stats]
|
|
46
|
+
workspaces get <id>
|
|
47
|
+
workspaces create <name>
|
|
48
|
+
workspaces rename <id> <name>
|
|
49
|
+
workspaces delete <id> --yes
|
|
50
|
+
workspaces budget [id]
|
|
51
|
+
workspaces set-budget <id> <amount>
|
|
52
|
+
|
|
53
|
+
${bold("Members")}
|
|
54
|
+
members list [-w <workspace>]
|
|
55
|
+
members invite [--email a,b] [--max-uses n] [--expires-in-days n]
|
|
56
|
+
members role <member-id> <ADMIN|MEMBER>
|
|
57
|
+
members remove <member-id>
|
|
58
|
+
|
|
59
|
+
${bold("Providers")}
|
|
60
|
+
providers list [--name] [--compatibility]
|
|
61
|
+
providers get <id>
|
|
62
|
+
providers create <name> --priority 1 [--base-url] [--api-key]
|
|
63
|
+
[--compatibility OPENAI_COMPATIBLE|ANTHROPIC|OPENAI_RESPONSES]
|
|
64
|
+
providers update <id> [--name] [--priority] [--base-url] [--api-key] [--enabled]
|
|
65
|
+
providers delete <id> --yes
|
|
66
|
+
|
|
67
|
+
${bold("Models")}
|
|
68
|
+
models list [--provider <id>] [--type chat] [--name]
|
|
69
|
+
models get <id>
|
|
70
|
+
models create --name --display-name --provider <id> --type chat
|
|
71
|
+
--input-cost --output-cost --context-window
|
|
72
|
+
[--api-name] [--max-output] [--cache-read-cost] [--cache-write-cost]
|
|
73
|
+
models update <id> [--display-name] [--input-cost] [--output-cost] [--enabled]
|
|
74
|
+
models delete <id> --yes
|
|
75
|
+
|
|
76
|
+
${bold("Prompts")}
|
|
77
|
+
prompts list [--name] [--type] [--environment]
|
|
78
|
+
prompts get <id>
|
|
79
|
+
prompts create --name --content [--type SYSTEM] [--model <id>] [--position n]
|
|
80
|
+
prompts update <id> [--name] [--content] [--enabled]
|
|
81
|
+
prompts delete <id> --yes
|
|
82
|
+
|
|
83
|
+
${bold("Mappings")}
|
|
84
|
+
mappings list [--source] [--target]
|
|
85
|
+
mappings get <id>
|
|
86
|
+
mappings create --source <name> --target <model> [--priority n] [--pattern true]
|
|
87
|
+
mappings update <id> [--source] [--target] [--priority] [--enabled]
|
|
88
|
+
mappings delete <id> --yes
|
|
89
|
+
|
|
90
|
+
${bold("Read only")}
|
|
91
|
+
workflows list | get <id>
|
|
92
|
+
metrics list [--page n] [--limit n] [--key <id>] [--start <iso>] [--end <iso>]
|
|
93
|
+
metrics get <request-id>
|
|
94
|
+
indexes list | get <id> | create <url> [--max-pages n] | delete <id> --yes
|
|
95
|
+
quotas list | get <id> | update <id> [--limit n] | delete <id> --yes
|
|
96
|
+
health
|
|
97
|
+
api-version
|
|
98
|
+
|
|
99
|
+
${bold("Escape hatch")}
|
|
100
|
+
api <METHOD> <path> [--data '<json>'] Call any endpoint directly
|
|
101
|
+
|
|
102
|
+
${bold("Global")}
|
|
103
|
+
--json Machine readable output on every command
|
|
104
|
+
-w, --workspace Workspace id or name, defaults to the first
|
|
105
|
+
--version, -v Print the CLI version
|
|
106
|
+
--help, -h Print this message
|
|
107
|
+
|
|
108
|
+
${dim("Docs https://docs.nemu.cc")}
|
|
109
|
+
`;
|
|
110
|
+
function usage() {
|
|
111
|
+
out(HELP);
|
|
112
|
+
}
|
|
113
|
+
async function main() {
|
|
114
|
+
const argv = process.argv.slice(2);
|
|
115
|
+
const command = argv[0];
|
|
116
|
+
const args = parse_args(argv.slice(1));
|
|
117
|
+
set_json_mode(bool_flag(args, "json"));
|
|
118
|
+
if (command === undefined || command === "--help" || command === "-h") {
|
|
119
|
+
usage();
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
if (command === "--version" || command === "-v") {
|
|
123
|
+
if (is_json_mode()) {
|
|
124
|
+
emit({ version: VERSION });
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
out(VERSION);
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
const handler = COMMANDS[command];
|
|
131
|
+
if (handler === undefined) {
|
|
132
|
+
fail(`Unknown command "${command}".`);
|
|
133
|
+
usage();
|
|
134
|
+
return 1;
|
|
135
|
+
}
|
|
136
|
+
if (bool_flag(args, "help", "h")) {
|
|
137
|
+
usage();
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
return await handler(args);
|
|
141
|
+
}
|
|
142
|
+
main()
|
|
143
|
+
.then((code) => {
|
|
144
|
+
process.exitCode = code;
|
|
145
|
+
})
|
|
146
|
+
.catch((error) => {
|
|
147
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
148
|
+
if (is_json_mode()) {
|
|
149
|
+
emit({ ok: false, error: message });
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
fail(message);
|
|
153
|
+
}
|
|
154
|
+
process.exitCode = 1;
|
|
155
|
+
});
|