@hive3/cli 0.3.0 → 0.5.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/README.md +314 -83
- package/bin/hive.js +8 -0
- package/package.json +6 -4
- package/skills/hive3/SKILL.md +253 -0
- package/skills/hive3/agents/openai.yaml +4 -0
- package/src/api-client.js +79 -11
- package/src/area-commands.js +348 -0
- package/src/commands.js +107 -59
- package/src/config.js +56 -11
- package/src/conversations-client.js +18 -0
- package/src/conversations-commands.js +103 -0
- package/src/extended-commands.js +15 -44
- package/src/feedback-client.js +15 -0
- package/src/feedback-commands.js +148 -0
- package/src/feedback-replay.js +43 -0
- package/src/login.js +93 -16
- package/src/payments-client.js +13 -0
- package/src/payments-commands.js +127 -0
- package/src/selling-client.js +24 -0
- package/src/selling-commands.js +157 -0
- package/src/shared.js +102 -0
- package/src/skill.js +212 -0
- package/src/stripe-client.js +16 -0
- package/src/stripe-commands.js +63 -0
- package/src/team-client.js +11 -0
- package/src/team-commands.js +84 -0
- package/src/update.js +30 -17
- package/src/usage.js +305 -12
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { stat } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { HiveApiError } from "./api-client.js";
|
|
7
|
+
import { authenticatedClient, parseBoolean, postFileToUploadUrl, readJsonObject, requiredFlagValue, writeJson } from "./shared.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{ key: string, type: "string" | "boolean" | "switch" | "limit" | "choice" | "iso-date", choices?: string[], max?: number }} FlagRule
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @type {Record<string, FlagRule>} */
|
|
14
|
+
const ACCOUNT = { "--account": { key: "account", type: "string" } };
|
|
15
|
+
/** @type {Record<string, FlagRule>} */
|
|
16
|
+
const PAGING = {
|
|
17
|
+
"--limit": { key: "limit", type: "limit" },
|
|
18
|
+
"--cursor": { key: "cursor", type: "string" },
|
|
19
|
+
};
|
|
20
|
+
/** @type {Record<string, FlagRule>} */
|
|
21
|
+
const IDEMPOTENCY = { "--idempotency-key": { key: "idempotencyKey", type: "string" } };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Small declarative flag parser shared by the newer command groups. Positional
|
|
25
|
+
* arguments are anything that does not start with `--`.
|
|
26
|
+
* @param {string[]} args
|
|
27
|
+
* @param {Record<string, FlagRule>} spec
|
|
28
|
+
* @param {string} label
|
|
29
|
+
* @param {number} maxPositionals
|
|
30
|
+
*/
|
|
31
|
+
function parseArgs(args, spec, label, maxPositionals = 0) {
|
|
32
|
+
/** @type {Record<string, any>} */
|
|
33
|
+
const values = {};
|
|
34
|
+
/** @type {string[]} */
|
|
35
|
+
const positionals = [];
|
|
36
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
37
|
+
const token = args[index];
|
|
38
|
+
if (!token.startsWith("--")) {
|
|
39
|
+
positionals.push(token);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const rule = spec[token];
|
|
43
|
+
if (!rule) throw new Error(`Unknown ${label} option: ${token}`);
|
|
44
|
+
if (rule.type === "switch") {
|
|
45
|
+
values[rule.key] = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (values[rule.key] !== undefined) throw new Error(`${token} may only be given once`);
|
|
49
|
+
index += 1;
|
|
50
|
+
values[rule.key] = convertValue(token, rule, requiredFlagValue(token, args[index]));
|
|
51
|
+
}
|
|
52
|
+
if (positionals.length > maxPositionals) throw new Error(`Unknown ${label} option: ${positionals[maxPositionals]}`);
|
|
53
|
+
return { positionals, values };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {string} flag
|
|
58
|
+
* @param {FlagRule} rule
|
|
59
|
+
* @param {string} raw
|
|
60
|
+
*/
|
|
61
|
+
function convertValue(flag, rule, raw) {
|
|
62
|
+
if (rule.type === "boolean") return parseBoolean(raw, flag);
|
|
63
|
+
if (rule.type === "limit") {
|
|
64
|
+
const value = Number(raw);
|
|
65
|
+
const max = rule.max ?? 100;
|
|
66
|
+
if (!Number.isInteger(value) || value < 1 || value > max) throw new Error(`${flag} must be a whole number from 1 to ${max}`);
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
if (rule.type === "choice") {
|
|
70
|
+
if (!rule.choices?.includes(raw)) throw new Error(`${flag} must be ${rule.choices?.join(", ")}`);
|
|
71
|
+
return raw;
|
|
72
|
+
}
|
|
73
|
+
if (rule.type === "iso-date") {
|
|
74
|
+
if (Number.isNaN(Date.parse(raw))) throw new Error(`${flag} must be an ISO-8601 date-time such as 2026-12-01T15:00:00Z`);
|
|
75
|
+
return raw;
|
|
76
|
+
}
|
|
77
|
+
return raw;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @param {string | undefined} value @param {string} name */
|
|
81
|
+
function requiredId(name, value) {
|
|
82
|
+
return requiredFlagValue(name, value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @param {Record<string, any>} values @param {string[]} keys */
|
|
86
|
+
function pickDefined(values, keys) {
|
|
87
|
+
/** @type {Record<string, any>} */
|
|
88
|
+
const payload = {};
|
|
89
|
+
for (const key of keys) {
|
|
90
|
+
if (values[key] !== undefined) payload[key] = values[key];
|
|
91
|
+
}
|
|
92
|
+
return payload;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function requireChanges(payload, label) {
|
|
96
|
+
if (Object.keys(payload).length === 0) throw new Error(`${label} needs at least one field to change`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function requireExactConfirmation(values, id, noun) {
|
|
100
|
+
if (values.confirm !== id) throw new Error(`--confirm must exactly match the ${noun} id`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --- search ---------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
const SEARCH_TYPES = ["all", "accounts", "events", "courses", "content", "quests", "jobs"];
|
|
106
|
+
|
|
107
|
+
async function runSearch(args, deps) {
|
|
108
|
+
const { positionals, values } = parseArgs(args, {
|
|
109
|
+
"--type": { choices: SEARCH_TYPES, key: "type", type: "choice" },
|
|
110
|
+
"--limit": { key: "limit", max: 50, type: "limit" },
|
|
111
|
+
"--cursor": { key: "cursor", type: "string" },
|
|
112
|
+
}, "search", Number.POSITIVE_INFINITY);
|
|
113
|
+
const q = positionals.join(" ").trim();
|
|
114
|
+
if (!q) throw new Error("search query requires a value");
|
|
115
|
+
if (q.length > 100) throw new Error("search query must be 1-100 characters");
|
|
116
|
+
writeJson(deps.stdout, await (await authenticatedClient(deps)).search({ ...values, q }));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// --- broadcasts -----------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
/** @type {Record<string, FlagRule>} */
|
|
122
|
+
const BROADCAST_FIELDS = {
|
|
123
|
+
"--subject": { key: "subject", type: "string" },
|
|
124
|
+
"--body-text": { key: "bodyText", type: "string" },
|
|
125
|
+
"--body-html": { key: "bodyHtml", type: "string" },
|
|
126
|
+
"--body-mode": { choices: ["editor", "html"], key: "bodyMode", type: "choice" },
|
|
127
|
+
"--hero-image-url": { key: "heroImageUrl", type: "string" },
|
|
128
|
+
"--file": { key: "file", type: "string" },
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
async function broadcastPayload(values) {
|
|
132
|
+
const fromFile = values.file ? await readJsonObject(values.file) : {};
|
|
133
|
+
return {
|
|
134
|
+
...fromFile,
|
|
135
|
+
...pickDefined(values, ["subject", "bodyText", "bodyHtml", "bodyMode", "heroImageUrl"]),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function runBroadcasts(args, deps) {
|
|
140
|
+
const [action, ...rest] = args;
|
|
141
|
+
const client = await authenticatedClient(deps);
|
|
142
|
+
if (action === "list") {
|
|
143
|
+
const { values } = parseArgs(rest, { ...ACCOUNT, ...PAGING }, "broadcasts list");
|
|
144
|
+
writeJson(deps.stdout, await client.listBroadcasts(values));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (action === "audience") {
|
|
148
|
+
const { values } = parseArgs(rest, ACCOUNT, "broadcasts audience");
|
|
149
|
+
writeJson(deps.stdout, await client.getBroadcastAudience(values.account));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (action === "get" || action === "test" || action === "cancel") {
|
|
153
|
+
const { positionals } = parseArgs(rest, {}, `broadcasts ${action}`, 1);
|
|
154
|
+
const id = requiredId("broadcast id", positionals[0]);
|
|
155
|
+
const result = action === "get"
|
|
156
|
+
? await client.getBroadcast(id)
|
|
157
|
+
: action === "test" ? await client.sendBroadcastTest(id) : await client.cancelBroadcast(id);
|
|
158
|
+
writeJson(deps.stdout, result);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (action === "create") {
|
|
162
|
+
const { values } = parseArgs(rest, { ...ACCOUNT, ...IDEMPOTENCY, ...BROADCAST_FIELDS }, "broadcasts create");
|
|
163
|
+
const payload = await broadcastPayload(values);
|
|
164
|
+
if (!payload.subject || !payload.bodyText) throw new Error("--subject and --body-text (or --file with both) are required");
|
|
165
|
+
writeJson(deps.stdout, await client.createBroadcast(payload, values.account, values.idempotencyKey));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (action === "update") {
|
|
169
|
+
const { positionals, values } = parseArgs(rest, {
|
|
170
|
+
...BROADCAST_FIELDS,
|
|
171
|
+
"--remove-hero-image": { key: "removeHeroImage", type: "switch" },
|
|
172
|
+
}, "broadcasts update", 1);
|
|
173
|
+
const id = requiredId("broadcast id", positionals[0]);
|
|
174
|
+
if (values.removeHeroImage && values.heroImageUrl !== undefined) {
|
|
175
|
+
throw new Error("--remove-hero-image cannot be combined with --hero-image-url");
|
|
176
|
+
}
|
|
177
|
+
const payload = await broadcastPayload(values);
|
|
178
|
+
if (values.removeHeroImage) payload.heroImageUrl = null;
|
|
179
|
+
requireChanges(payload, "broadcasts update");
|
|
180
|
+
writeJson(deps.stdout, await client.updateBroadcast(id, payload));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (action === "send") {
|
|
184
|
+
const { positionals, values } = parseArgs(rest, {
|
|
185
|
+
"--at": { key: "scheduledAt", type: "iso-date" },
|
|
186
|
+
"--yes": { key: "yes", type: "switch" },
|
|
187
|
+
}, "broadcasts send", 1);
|
|
188
|
+
const id = requiredId("broadcast id", positionals[0]);
|
|
189
|
+
if (!values.yes) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
"Sending a broadcast emails opted-in members and can't be undone once it starts. "
|
|
192
|
+
+ "Re-run with --yes to confirm (use `hive3 broadcasts test <id>` first to email only yourself).",
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
writeJson(deps.stdout, await client.sendBroadcast(id, pickDefined(values, ["scheduledAt"])));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
throw new Error(`Unknown broadcasts action: ${action || ""}`.trim());
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// --- AI assistant ---------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
const ASSISTANT_DOCUMENT_TYPES = {
|
|
204
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
205
|
+
".pdf": "application/pdf",
|
|
206
|
+
".txt": "text/plain",
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
async function addAssistantDocument(client, rest, deps) {
|
|
210
|
+
const { positionals, values } = parseArgs(rest, {
|
|
211
|
+
...ACCOUNT,
|
|
212
|
+
...IDEMPOTENCY,
|
|
213
|
+
"--content-type": { key: "contentType", type: "string" },
|
|
214
|
+
}, "assistant documents add", 1);
|
|
215
|
+
const filePath = requiredId("file", positionals[0]);
|
|
216
|
+
const contentType = values.contentType ?? /** @type {Record<string, string>} */ (ASSISTANT_DOCUMENT_TYPES)[path.extname(filePath).toLowerCase()];
|
|
217
|
+
if (!contentType) throw new Error("Could not infer content type; documents must be PDF, DOCX or plain text (pass --content-type)");
|
|
218
|
+
const fileStat = await stat(filePath);
|
|
219
|
+
const issued = await client.createAssistantUploadUrl(values.account);
|
|
220
|
+
const { uploadUrl, maxBytes } = issued.data;
|
|
221
|
+
if (typeof maxBytes === "number" && fileStat.size > maxBytes) {
|
|
222
|
+
throw new Error(`${path.basename(filePath)} is ${fileStat.size} bytes; assistant documents can be at most ${maxBytes} bytes`);
|
|
223
|
+
}
|
|
224
|
+
const upload = await postFileToUploadUrl(client, deps, uploadUrl, filePath, contentType);
|
|
225
|
+
const storageId = upload.parsed?.data?.storageId ?? upload.parsed?.storageId;
|
|
226
|
+
if (!upload.ok || !storageId) {
|
|
227
|
+
throw new HiveApiError(`Upload failed with status ${upload.status}`, { code: "upload_failed", status: upload.status });
|
|
228
|
+
}
|
|
229
|
+
writeJson(deps.stdout, await client.createAssistantDocument({
|
|
230
|
+
fileName: path.basename(filePath),
|
|
231
|
+
fileSize: fileStat.size,
|
|
232
|
+
mimeType: contentType,
|
|
233
|
+
storageId,
|
|
234
|
+
}, values.account, values.idempotencyKey));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function runAssistant(args, deps) {
|
|
238
|
+
const [action, ...rest] = args;
|
|
239
|
+
const client = await authenticatedClient(deps);
|
|
240
|
+
if (action === "get") {
|
|
241
|
+
const { values } = parseArgs(rest, ACCOUNT, "assistant get");
|
|
242
|
+
writeJson(deps.stdout, await client.getAssistant(values.account));
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (action === "update") {
|
|
246
|
+
const { values } = parseArgs(rest, {
|
|
247
|
+
...ACCOUNT,
|
|
248
|
+
"--name": { key: "name", type: "string" },
|
|
249
|
+
"--description": { key: "description", type: "string" },
|
|
250
|
+
"--avatar-url": { key: "avatarUrl", type: "string" },
|
|
251
|
+
"--visibility": { choices: ["public", "protected", "private"], key: "visibility", type: "choice" },
|
|
252
|
+
}, "assistant update");
|
|
253
|
+
const payload = pickDefined(values, ["name", "description", "avatarUrl", "visibility"]);
|
|
254
|
+
requireChanges(payload, "assistant update");
|
|
255
|
+
writeJson(deps.stdout, await client.updateAssistant(payload, values.account));
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (action === "documents") {
|
|
259
|
+
const implicitList = rest[0] === undefined || rest[0].startsWith("--");
|
|
260
|
+
const [documentAction, ...documentArgs] = implicitList ? ["list", ...rest] : rest;
|
|
261
|
+
if (documentAction === "list") {
|
|
262
|
+
const { values } = parseArgs(documentArgs, ACCOUNT, "assistant documents list");
|
|
263
|
+
writeJson(deps.stdout, await client.listAssistantDocuments(values.account));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (documentAction === "add") {
|
|
267
|
+
await addAssistantDocument(client, documentArgs, deps);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (documentAction === "delete") {
|
|
271
|
+
const { positionals, values } = parseArgs(documentArgs, { "--confirm": { key: "confirm", type: "string" } }, "assistant documents delete", 1);
|
|
272
|
+
const id = requiredId("document id", positionals[0]);
|
|
273
|
+
requireExactConfirmation(values, id, "document");
|
|
274
|
+
writeJson(deps.stdout, await client.deleteAssistantDocument(id));
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
throw new Error(`Unknown assistant documents action: ${documentAction}`);
|
|
278
|
+
}
|
|
279
|
+
throw new Error(`Unknown assistant action: ${action || ""}`.trim());
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// --- credentials ----------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
async function runCredentials(args, deps) {
|
|
285
|
+
const [action, ...rest] = args;
|
|
286
|
+
const client = await authenticatedClient(deps);
|
|
287
|
+
if (action === "list") {
|
|
288
|
+
const { values } = parseArgs(rest, { ...ACCOUNT, ...PAGING }, "credentials list");
|
|
289
|
+
writeJson(deps.stdout, await client.listCredentials(values));
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (action === "get") {
|
|
293
|
+
const { positionals } = parseArgs(rest, {}, "credentials get", 1);
|
|
294
|
+
writeJson(deps.stdout, await client.getCredential(requiredId("credential id", positionals[0])));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (action === "mine") {
|
|
298
|
+
parseArgs(rest, {}, "credentials mine");
|
|
299
|
+
writeJson(deps.stdout, await client.listMyCredentials());
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (action === "claimable") {
|
|
303
|
+
const { values } = parseArgs(rest, PAGING, "credentials claimable");
|
|
304
|
+
writeJson(deps.stdout, await client.listClaimableCredentials(values));
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (action === "award") {
|
|
308
|
+
const { positionals, values } = parseArgs(rest, {
|
|
309
|
+
"--handles": { key: "handles", type: "string" },
|
|
310
|
+
"--yes": { key: "yes", type: "switch" },
|
|
311
|
+
}, "credentials award", 1);
|
|
312
|
+
const id = requiredId("credential id", positionals[0]);
|
|
313
|
+
if (values.handles === undefined) throw new Error("--handles requires a value (comma-separated, up to 50)");
|
|
314
|
+
const handles = values.handles.split(",").map((handle) => handle.trim()).filter(Boolean);
|
|
315
|
+
if (handles.length < 1 || handles.length > 50) throw new Error("--handles must list between 1 and 50 handles");
|
|
316
|
+
if (!values.yes) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Awarding a credential gives it to ${handles.length} account(s) and can't be undone through the CLI. Re-run with --yes to confirm.`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
writeJson(deps.stdout, await client.awardCredential(id, handles));
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
throw new Error(`Unknown credentials action: ${action || ""}`.trim());
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// --- worlds ---------------------------------------------------------------
|
|
328
|
+
|
|
329
|
+
/** Lists the worlds an account's events may book; pass one's `id` to `events create --world`. */
|
|
330
|
+
async function runWorlds(args, deps) {
|
|
331
|
+
const { values } = parseArgs(args, ACCOUNT, "worlds");
|
|
332
|
+
const client = await authenticatedClient(deps);
|
|
333
|
+
writeJson(deps.stdout, await client.listWorlds(values.account));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Dispatch for the search, broadcasts, assistant, credentials and worlds commands.
|
|
338
|
+
* @returns {Promise<boolean>} true when the command belonged to this module
|
|
339
|
+
*/
|
|
340
|
+
export async function runAreaCommand(command, args, deps) {
|
|
341
|
+
if (command === "search") await runSearch(args, deps);
|
|
342
|
+
else if (command === "broadcasts") await runBroadcasts(args, deps);
|
|
343
|
+
else if (command === "assistant") await runAssistant(args, deps);
|
|
344
|
+
else if (command === "credentials") await runCredentials(args, deps);
|
|
345
|
+
else if (command === "worlds") await runWorlds(args, deps);
|
|
346
|
+
else return false;
|
|
347
|
+
return true;
|
|
348
|
+
}
|
package/src/commands.js
CHANGED
|
@@ -7,10 +7,23 @@ import { createApiClient, HiveApiError, isResourceKind } from "./api-client.js";
|
|
|
7
7
|
import { clearConfig, loadConfig } from "./config.js";
|
|
8
8
|
import { runExtendedCommand } from "./extended-commands.js";
|
|
9
9
|
import { login, loginUsage, parseLoginArgs } from "./login.js";
|
|
10
|
-
import {
|
|
10
|
+
import { runSkill } from "./skill.js";
|
|
11
|
+
import { authenticatedClient, parseBoolean, postFileToUploadUrl, requiredFlagValue, terminalText, writeJson, writeLine } from "./shared.js";
|
|
12
|
+
import { CURRENT_VERSION, updateUsage } from "./update.js";
|
|
13
|
+
import { commandUsage, resourceUsage, usage } from "./usage.js";
|
|
14
|
+
import { runTeamCommand } from "./team-commands.js";
|
|
15
|
+
import { runStripeCommand } from "./stripe-commands.js";
|
|
16
|
+
import { runFeedbackCommand } from "./feedback-commands.js";
|
|
17
|
+
import { runPaymentsCommand } from "./payments-commands.js";
|
|
18
|
+
import { runConversationsCommand } from "./conversations-commands.js";
|
|
19
|
+
import { runSellingCommand } from "./selling-commands.js";
|
|
20
|
+
|
|
21
|
+
// Commands that run with no arguments.
|
|
22
|
+
const SIMPLE_COMMANDS = new Set(["login", "logout", "update", "whoami", "capabilities", "worlds"]);
|
|
11
23
|
|
|
12
24
|
function defaultDeps(overrides = {}) {
|
|
13
25
|
return {
|
|
26
|
+
cwd: process.cwd(),
|
|
14
27
|
env: process.env,
|
|
15
28
|
fetch: globalThis.fetch,
|
|
16
29
|
homeDir: undefined,
|
|
@@ -29,6 +42,7 @@ function loginDeps(deps) {
|
|
|
29
42
|
env: deps.env,
|
|
30
43
|
fetch: deps.fetch,
|
|
31
44
|
platform: deps.platform,
|
|
45
|
+
stderr: deps.stderr,
|
|
32
46
|
stdout: deps.stdout,
|
|
33
47
|
};
|
|
34
48
|
if (deps.homeDir !== undefined) next.homeDir = deps.homeDir;
|
|
@@ -38,19 +52,6 @@ function loginDeps(deps) {
|
|
|
38
52
|
return next;
|
|
39
53
|
}
|
|
40
54
|
|
|
41
|
-
function writeLine(stream, message = "") {
|
|
42
|
-
stream.write(`${message}\n`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function writeJson(stream, value) {
|
|
46
|
-
writeLine(stream, JSON.stringify(value, null, 2));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function requiredFlagValue(flag, value) {
|
|
50
|
-
if (!value || value.startsWith("--")) throw new Error(`${flag} requires a value`);
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
55
|
function takeAccountFlag(args) {
|
|
55
56
|
let account;
|
|
56
57
|
const rest = [];
|
|
@@ -97,7 +98,7 @@ function setPayloadField(payload, key, value) {
|
|
|
97
98
|
if (value !== undefined) payload[key] = value;
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
async function parseResourcePayload(args, includeOwner) {
|
|
101
|
+
async function parseResourcePayload(kind, args, includeOwner) {
|
|
101
102
|
let payload = {};
|
|
102
103
|
let idempotencyKey;
|
|
103
104
|
let ownerType;
|
|
@@ -122,6 +123,9 @@ async function parseResourcePayload(args, includeOwner) {
|
|
|
122
123
|
} else if (flag === "--visibility") {
|
|
123
124
|
index += 1;
|
|
124
125
|
setPayloadField(payload, "visibility", requiredFlagValue(flag, args[index]));
|
|
126
|
+
} else if (kind === "events" && flag === "--world") {
|
|
127
|
+
index += 1;
|
|
128
|
+
setPayloadField(payload, "worldId", requiredFlagValue(flag, args[index]));
|
|
125
129
|
} else if (flag === "--file") {
|
|
126
130
|
index += 1;
|
|
127
131
|
const filePayload = await readJsonFile(requiredFlagValue(flag, args[index]));
|
|
@@ -152,12 +156,6 @@ async function parseResourcePayload(args, includeOwner) {
|
|
|
152
156
|
if (Object.keys(payload).length === 0) throw new Error("At least one resource field is required");
|
|
153
157
|
return { idempotencyKey, payload };
|
|
154
158
|
}
|
|
155
|
-
function parseBoolean(value, flag) {
|
|
156
|
-
if (value === "true" || value === "1" || value === "yes") return true;
|
|
157
|
-
if (value === "false" || value === "0" || value === "no") return false;
|
|
158
|
-
throw new Error(`${flag} must be true or false`);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
159
|
function parseCsv(value) {
|
|
162
160
|
return value.split(",").map((item) => item.trim()).filter(Boolean);
|
|
163
161
|
}
|
|
@@ -234,12 +232,6 @@ function mimeFromPath(filePath) {
|
|
|
234
232
|
}[ext] || "";
|
|
235
233
|
}
|
|
236
234
|
|
|
237
|
-
async function authenticatedClient(deps) {
|
|
238
|
-
const config = await loadConfig({ env: deps.env, homeDir: deps.homeDir, platform: deps.platform });
|
|
239
|
-
if (!config) throw new HiveApiError("Not logged in. Run hive login first.", { code: "auth_required", status: 401 });
|
|
240
|
-
return createApiClient({ apiBaseUrl: config.apiBaseUrl, apiKey: config.apiKey, fetch: deps.fetch });
|
|
241
|
-
}
|
|
242
|
-
|
|
243
235
|
function capabilityLabels(capabilities) {
|
|
244
236
|
return Object.entries(capabilities)
|
|
245
237
|
.filter(([, enabled]) => enabled)
|
|
@@ -250,12 +242,12 @@ async function runWhoami(deps) {
|
|
|
250
242
|
const result = await (await authenticatedClient(deps)).whoami();
|
|
251
243
|
const data = result.data;
|
|
252
244
|
const person = data.person;
|
|
253
|
-
writeLine(deps.stdout, `
|
|
254
|
-
if (person.handle) writeLine(deps.stdout, `Handle: @${person.handle}`);
|
|
255
|
-
writeLine(deps.stdout, `Scopes: ${data.credential.scopes.join(", ") || "none"}`);
|
|
256
|
-
writeLine(deps.stdout, `
|
|
245
|
+
writeLine(deps.stdout, terminalText(`Account: ${person.displayName || person.handle || person.id}`));
|
|
246
|
+
if (person.handle) writeLine(deps.stdout, terminalText(`Handle: @${person.handle}`));
|
|
247
|
+
writeLine(deps.stdout, terminalText(`Scopes: ${data.credential.scopes.join(", ") || "none"}`));
|
|
248
|
+
writeLine(deps.stdout, `Accounts this key can act on: ${data.communities.length}`);
|
|
257
249
|
for (const community of data.communities) {
|
|
258
|
-
writeLine(deps.stdout, `- ${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`);
|
|
250
|
+
writeLine(deps.stdout, terminalText(`- ${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`));
|
|
259
251
|
}
|
|
260
252
|
}
|
|
261
253
|
|
|
@@ -263,11 +255,11 @@ async function runCapabilities(deps) {
|
|
|
263
255
|
const result = await (await authenticatedClient(deps)).capabilities();
|
|
264
256
|
const communities = result.data.communities;
|
|
265
257
|
if (communities.length === 0) {
|
|
266
|
-
writeLine(deps.stdout, "No
|
|
258
|
+
writeLine(deps.stdout, "No accounts are available to this key.");
|
|
267
259
|
return;
|
|
268
260
|
}
|
|
269
261
|
for (const community of communities) {
|
|
270
|
-
writeLine(deps.stdout, `${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`);
|
|
262
|
+
writeLine(deps.stdout, terminalText(`${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`));
|
|
271
263
|
}
|
|
272
264
|
}
|
|
273
265
|
|
|
@@ -284,13 +276,13 @@ async function runResource(kind, args, deps) {
|
|
|
284
276
|
return;
|
|
285
277
|
}
|
|
286
278
|
if (action === "create") {
|
|
287
|
-
const { idempotencyKey, payload } = await parseResourcePayload(rest, true);
|
|
279
|
+
const { idempotencyKey, payload } = await parseResourcePayload(kind, rest, true);
|
|
288
280
|
writeJson(deps.stdout, await client.createResource(kind, payload, idempotencyKey));
|
|
289
281
|
return;
|
|
290
282
|
}
|
|
291
283
|
if (action === "update") {
|
|
292
284
|
const id = requiredFlagValue("resource id", rest[0]);
|
|
293
|
-
const { payload } = await parseResourcePayload(rest.slice(1), false);
|
|
285
|
+
const { payload } = await parseResourcePayload(kind, rest.slice(1), false);
|
|
294
286
|
writeJson(deps.stdout, await client.updateResource(kind, id, payload));
|
|
295
287
|
return;
|
|
296
288
|
}
|
|
@@ -300,6 +292,12 @@ async function runResource(kind, args, deps) {
|
|
|
300
292
|
writeJson(deps.stdout, await client.archiveResource(kind, id));
|
|
301
293
|
return;
|
|
302
294
|
}
|
|
295
|
+
if (action === "get") {
|
|
296
|
+
const id = requiredFlagValue("resource id", rest[0]);
|
|
297
|
+
if (rest.length > 1) throw new Error(`Unknown ${kind} get option: ${rest[1]}`);
|
|
298
|
+
writeJson(deps.stdout, await client.getResource(kind, id));
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
303
301
|
throw new Error(`Unknown ${kind} action: ${action}`);
|
|
304
302
|
}
|
|
305
303
|
|
|
@@ -390,17 +388,11 @@ async function runUpload(args, deps) {
|
|
|
390
388
|
const client = await authenticatedClient(deps);
|
|
391
389
|
const issued = await client.createUploadUrl({ byteSize: fileStat.size, contentType, fileName, purpose });
|
|
392
390
|
const { uploadUrl, ...issuedSafe } = issued.data;
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
headers: { "Content-Type": contentType },
|
|
397
|
-
method: "POST",
|
|
398
|
-
});
|
|
399
|
-
const text = await response.text();
|
|
400
|
-
const parsed = text ? JSON.parse(text) : {};
|
|
401
|
-
if (!response.ok) {
|
|
402
|
-
throw new HiveApiError(`Upload failed with status ${response.status}`, { code: "upload_failed", status: response.status });
|
|
391
|
+
const upload = await postFileToUploadUrl(client, deps, uploadUrl, filePath, contentType);
|
|
392
|
+
if (!upload.ok) {
|
|
393
|
+
throw new HiveApiError(`Upload failed with status ${upload.status}`, { code: "upload_failed", status: upload.status });
|
|
403
394
|
}
|
|
395
|
+
const parsed = upload.parsed;
|
|
404
396
|
writeJson(deps.stdout, { data: { ...issuedSafe, ...parsed } });
|
|
405
397
|
}
|
|
406
398
|
|
|
@@ -432,20 +424,23 @@ async function runAccount(args, deps) {
|
|
|
432
424
|
writeJson(deps.stdout, await client.requestAccountExport());
|
|
433
425
|
return;
|
|
434
426
|
}
|
|
435
|
-
if (action === "
|
|
436
|
-
const
|
|
437
|
-
if (
|
|
438
|
-
if (rest.length > 1) throw new Error(`Unknown account
|
|
439
|
-
writeJson(deps.stdout, await client.
|
|
427
|
+
if (action === "linked-communities") {
|
|
428
|
+
const linkedAction = rest[0] ?? "list";
|
|
429
|
+
if (linkedAction === "list") {
|
|
430
|
+
if (rest.length > 1) throw new Error(`Unknown account linked-communities list option: ${rest[1]}`);
|
|
431
|
+
writeJson(deps.stdout, await client.listLinkedCommunities());
|
|
440
432
|
return;
|
|
441
433
|
}
|
|
442
|
-
if (
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
434
|
+
if (linkedAction === "disconnect") {
|
|
435
|
+
const communityId = requiredFlagValue("community id", rest[1]);
|
|
436
|
+
if (rest[2] !== "--confirm" || rest[3] !== communityId) {
|
|
437
|
+
throw new Error("--confirm must exactly match the community id");
|
|
438
|
+
}
|
|
439
|
+
if (rest.length > 4) throw new Error(`Unknown account linked-communities disconnect option: ${rest[4]}`);
|
|
440
|
+
writeJson(deps.stdout, await client.disconnectLinkedCommunity(communityId));
|
|
447
441
|
return;
|
|
448
442
|
}
|
|
443
|
+
throw new Error(`Unknown account linked-communities action: ${linkedAction}`);
|
|
449
444
|
}
|
|
450
445
|
throw new Error(`Unknown account action: ${action || ""}`.trim());
|
|
451
446
|
}
|
|
@@ -496,8 +491,17 @@ async function runNfts(args, deps) {
|
|
|
496
491
|
function renderError(error) {
|
|
497
492
|
if (error instanceof HiveApiError) {
|
|
498
493
|
const scopeText = error.requiredScopes.length > 0 ? ` Required scopes: ${error.requiredScopes.join(", ")}.` : "";
|
|
494
|
+
const teamHint = error.requiredScopes.some(scope => scope.startsWith("team:"))
|
|
495
|
+
? " Staff: run hive3 login again (Team Tools are added automatically for staff accounts)."
|
|
496
|
+
: "";
|
|
499
497
|
if (error.code === "auth_required") return error.message;
|
|
500
|
-
|
|
498
|
+
if (error.code === "plan_required") {
|
|
499
|
+
return [
|
|
500
|
+
"API access needs Honey Bee or above, or current Team access on your account.",
|
|
501
|
+
"Upgrade under Settings in the Hive app. This key works again once the plan is active; you don't need to log in again.",
|
|
502
|
+
].join(" ");
|
|
503
|
+
}
|
|
504
|
+
return `Hive API error ${error.status} (${error.code}): ${error.message}.${scopeText}${teamHint}`;
|
|
501
505
|
}
|
|
502
506
|
return error instanceof Error ? error.message : "Hive CLI failed.";
|
|
503
507
|
}
|
|
@@ -510,6 +514,22 @@ export async function main(argv, overrides = {}) {
|
|
|
510
514
|
writeLine(deps.stdout, usage());
|
|
511
515
|
return 0;
|
|
512
516
|
}
|
|
517
|
+
if (command === "--version" || command === "-v") {
|
|
518
|
+
if (rest.length > 0) throw new Error(`Unknown option: ${rest[0]}`);
|
|
519
|
+
writeLine(deps.stdout, CURRENT_VERSION);
|
|
520
|
+
return 0;
|
|
521
|
+
}
|
|
522
|
+
// --help/-h anywhere after a command prints that command's help and never executes it.
|
|
523
|
+
const wantsHelp = rest.some((argument) => argument === "--help" || argument === "-h");
|
|
524
|
+
const helpText = command === "login"
|
|
525
|
+
? loginUsage()
|
|
526
|
+
: command === "update"
|
|
527
|
+
? updateUsage()
|
|
528
|
+
: isResourceKind(command) ? resourceUsage(command) : commandUsage(command);
|
|
529
|
+
if (helpText && (wantsHelp || (rest.length === 0 && command !== "upload" && !SIMPLE_COMMANDS.has(command)))) {
|
|
530
|
+
writeLine(deps.stdout, helpText);
|
|
531
|
+
return 0;
|
|
532
|
+
}
|
|
513
533
|
if (command === "login") {
|
|
514
534
|
const parsed = parseLoginArgs(rest, deps.env);
|
|
515
535
|
if (parsed.help) writeLine(deps.stdout, loginUsage());
|
|
@@ -530,13 +550,17 @@ export async function main(argv, overrides = {}) {
|
|
|
530
550
|
} catch {
|
|
531
551
|
writeLine(
|
|
532
552
|
deps.stderr,
|
|
533
|
-
"Warning: could not revoke the server credential. Revoke it at Settings → Developer
|
|
553
|
+
"Warning: could not revoke the server credential. Revoke it at Settings → Help & feedback → Developer API keys (https://app.hive3.tech/settings/api-keys).",
|
|
534
554
|
);
|
|
535
555
|
}
|
|
536
556
|
const file = await clearConfig({ env: deps.env, homeDir: deps.homeDir, platform: deps.platform });
|
|
537
557
|
writeLine(deps.stdout, `Removed Hive CLI credential at ${file}.`);
|
|
538
558
|
return 0;
|
|
539
559
|
}
|
|
560
|
+
if (command === "skill") {
|
|
561
|
+
await runSkill(rest, deps);
|
|
562
|
+
return 0;
|
|
563
|
+
}
|
|
540
564
|
if (command === "whoami") {
|
|
541
565
|
if (rest.length > 0) throw new Error(`Unknown whoami option: ${rest[0]}`);
|
|
542
566
|
await runWhoami(deps);
|
|
@@ -567,6 +591,30 @@ export async function main(argv, overrides = {}) {
|
|
|
567
591
|
await runNfts(rest, deps);
|
|
568
592
|
return 0;
|
|
569
593
|
}
|
|
594
|
+
if (command === "team") {
|
|
595
|
+
await runTeamCommand(rest, deps);
|
|
596
|
+
return 0;
|
|
597
|
+
}
|
|
598
|
+
if (command === "stripe") {
|
|
599
|
+
await runStripeCommand(rest, deps);
|
|
600
|
+
return 0;
|
|
601
|
+
}
|
|
602
|
+
if (command === "feedback") {
|
|
603
|
+
await runFeedbackCommand(rest, deps);
|
|
604
|
+
return 0;
|
|
605
|
+
}
|
|
606
|
+
if (command === "payments") {
|
|
607
|
+
await runPaymentsCommand(rest, deps);
|
|
608
|
+
return 0;
|
|
609
|
+
}
|
|
610
|
+
if (command === "conversations") {
|
|
611
|
+
await runConversationsCommand(rest, deps);
|
|
612
|
+
return 0;
|
|
613
|
+
}
|
|
614
|
+
if (command === "products" || command === "subscriptions" || command === "tickets") {
|
|
615
|
+
await runSellingCommand(command, rest, deps);
|
|
616
|
+
return 0;
|
|
617
|
+
}
|
|
570
618
|
if (await runExtendedCommand(command, rest, deps)) return 0;
|
|
571
619
|
if (isResourceKind(command)) {
|
|
572
620
|
await runResource(command, rest, deps);
|
|
@@ -574,7 +622,7 @@ export async function main(argv, overrides = {}) {
|
|
|
574
622
|
}
|
|
575
623
|
throw new Error(`Unknown command: ${command}`);
|
|
576
624
|
} catch (error) {
|
|
577
|
-
writeLine(deps.stderr, renderError(error));
|
|
625
|
+
writeLine(deps.stderr, terminalText(renderError(error)));
|
|
578
626
|
return error instanceof HiveApiError && error.code === "auth_required" ? 1 : 2;
|
|
579
627
|
}
|
|
580
628
|
}
|