hypha-cli 0.1.9 → 0.1.11
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/dist/{apps-rK1_N8nw.mjs → apps-BJCjouF4.mjs} +2 -2
- package/dist/cli.mjs +1 -1
- package/package.json +1 -1
- package/dist/apps-CpJkx863.mjs +0 -512
- package/dist/apps-D659xu7q.mjs +0 -424
- package/dist/artifacts-8p2A4NMr.mjs +0 -764
- package/dist/artifacts-C0yhSTgw.mjs +0 -629
- package/dist/artifacts-DdgZRh8t.mjs +0 -764
- package/dist/helpers-BvfSCkvr.mjs +0 -188
- package/dist/workspace-BwLwj61H.mjs +0 -206
- package/dist/workspace-DMqqSmvB.mjs +0 -283
- package/dist/workspace-RZLvWJpe.mjs +0 -128
- package/dist/workspace-XO_tEtii.mjs +0 -284
- package/dist/workspace-wlyR33kp.mjs +0 -127
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
-
import { join } from 'path';
|
|
3
|
-
import { homedir } from 'os';
|
|
4
|
-
|
|
5
|
-
function getHyphaHome() {
|
|
6
|
-
return process.env.HYPHA_HOME || join(homedir(), ".hypha");
|
|
7
|
-
}
|
|
8
|
-
function getEnvFilePath() {
|
|
9
|
-
return join(getHyphaHome(), ".env");
|
|
10
|
-
}
|
|
11
|
-
function readEnvValue(key) {
|
|
12
|
-
const envFile = getEnvFilePath();
|
|
13
|
-
if (!existsSync(envFile)) return void 0;
|
|
14
|
-
const lines = readFileSync(envFile, "utf-8").split("\n");
|
|
15
|
-
for (const line of lines) {
|
|
16
|
-
const trimmed = line.trim();
|
|
17
|
-
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
18
|
-
const eqIdx = trimmed.indexOf("=");
|
|
19
|
-
if (eqIdx === -1) continue;
|
|
20
|
-
const k = trimmed.slice(0, eqIdx).trim();
|
|
21
|
-
const v = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, "");
|
|
22
|
-
if (k === key) return v;
|
|
23
|
-
}
|
|
24
|
-
return void 0;
|
|
25
|
-
}
|
|
26
|
-
function writeEnvValue(key, value) {
|
|
27
|
-
const envFile = getEnvFilePath();
|
|
28
|
-
const dir = getHyphaHome();
|
|
29
|
-
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
30
|
-
let lines = [];
|
|
31
|
-
if (existsSync(envFile)) {
|
|
32
|
-
lines = readFileSync(envFile, "utf-8").split("\n");
|
|
33
|
-
}
|
|
34
|
-
let found = false;
|
|
35
|
-
for (let i = 0; i < lines.length; i++) {
|
|
36
|
-
const trimmed = lines[i].trim();
|
|
37
|
-
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
38
|
-
const eqIdx = trimmed.indexOf("=");
|
|
39
|
-
if (eqIdx === -1) continue;
|
|
40
|
-
if (trimmed.slice(0, eqIdx).trim() === key) {
|
|
41
|
-
lines[i] = `${key}=${value}`;
|
|
42
|
-
found = true;
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (!found) {
|
|
47
|
-
lines.push(`${key}=${value}`);
|
|
48
|
-
}
|
|
49
|
-
writeFileSync(envFile, lines.join("\n") + "\n", { mode: 384 });
|
|
50
|
-
}
|
|
51
|
-
function resolveServerUrl(opts) {
|
|
52
|
-
return opts?.serverUrl || process.env.HYPHA_SERVER_URL || readEnvValue("HYPHA_SERVER_URL") || "https://hypha.aicell.io";
|
|
53
|
-
}
|
|
54
|
-
function resolveToken(opts) {
|
|
55
|
-
return opts?.token || process.env.HYPHA_TOKEN || readEnvValue("HYPHA_TOKEN");
|
|
56
|
-
}
|
|
57
|
-
function resolveWorkspace(opts) {
|
|
58
|
-
return opts?.workspace || process.env.HYPHA_WORKSPACE || readEnvValue("HYPHA_WORKSPACE");
|
|
59
|
-
}
|
|
60
|
-
async function connectToHypha(opts) {
|
|
61
|
-
const serverUrl = resolveServerUrl(opts);
|
|
62
|
-
const token = resolveToken(opts);
|
|
63
|
-
const workspace = resolveWorkspace(opts);
|
|
64
|
-
if (!token) {
|
|
65
|
-
console.error("No token found. Run `hypha login` first, or set HYPHA_TOKEN.");
|
|
66
|
-
process.exit(1);
|
|
67
|
-
}
|
|
68
|
-
const { hyphaWebsocketClient } = await import('hypha-rpc');
|
|
69
|
-
const server = await hyphaWebsocketClient.connectToServer({
|
|
70
|
-
server_url: serverUrl,
|
|
71
|
-
token,
|
|
72
|
-
workspace: workspace || void 0
|
|
73
|
-
});
|
|
74
|
-
return server;
|
|
75
|
-
}
|
|
76
|
-
async function loginToHypha(serverUrl) {
|
|
77
|
-
const url = serverUrl || resolveServerUrl();
|
|
78
|
-
const { hyphaWebsocketClient } = await import('hypha-rpc');
|
|
79
|
-
console.log(`Logging in to ${url}...`);
|
|
80
|
-
const token = await hyphaWebsocketClient.login({
|
|
81
|
-
server_url: url
|
|
82
|
-
});
|
|
83
|
-
if (!token) {
|
|
84
|
-
console.error("Login failed \u2014 no token received.");
|
|
85
|
-
process.exit(1);
|
|
86
|
-
}
|
|
87
|
-
const server = await hyphaWebsocketClient.connectToServer({
|
|
88
|
-
server_url: url,
|
|
89
|
-
token
|
|
90
|
-
});
|
|
91
|
-
const ws = server.config.workspace;
|
|
92
|
-
writeEnvValue("HYPHA_SERVER_URL", url);
|
|
93
|
-
writeEnvValue("HYPHA_TOKEN", token);
|
|
94
|
-
writeEnvValue("HYPHA_WORKSPACE", ws);
|
|
95
|
-
console.log(`Logged in to workspace: ${ws}`);
|
|
96
|
-
console.log(`Credentials saved to ${getEnvFilePath()}`);
|
|
97
|
-
await server.disconnect();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function getFlag(args, flag, shortFlag) {
|
|
101
|
-
for (let i = 0; i < args.length; i++) {
|
|
102
|
-
if ((args[i] === flag || shortFlag && args[i] === shortFlag) && i + 1 < args.length) {
|
|
103
|
-
return args[i + 1];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return void 0;
|
|
107
|
-
}
|
|
108
|
-
function getFlagInt(args, flag, shortFlag) {
|
|
109
|
-
const val = getFlag(args, flag, shortFlag);
|
|
110
|
-
if (val === void 0) return void 0;
|
|
111
|
-
const n = parseInt(val, 10);
|
|
112
|
-
return isNaN(n) ? void 0 : n;
|
|
113
|
-
}
|
|
114
|
-
function getAllFlags(args, flag) {
|
|
115
|
-
const results = [];
|
|
116
|
-
for (let i = 0; i < args.length; i++) {
|
|
117
|
-
if (args[i] === flag && i + 1 < args.length) {
|
|
118
|
-
results.push(args[i + 1]);
|
|
119
|
-
i++;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return results;
|
|
123
|
-
}
|
|
124
|
-
function hasFlag(args, ...flags) {
|
|
125
|
-
return args.some((a) => flags.includes(a));
|
|
126
|
-
}
|
|
127
|
-
function positionalArgs(args, knownFlags = []) {
|
|
128
|
-
const result = [];
|
|
129
|
-
for (let i = 0; i < args.length; i++) {
|
|
130
|
-
if (args[i].startsWith("-")) {
|
|
131
|
-
if (knownFlags.includes(args[i]) && i + 1 < args.length) {
|
|
132
|
-
i++;
|
|
133
|
-
}
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
result.push(args[i]);
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
}
|
|
140
|
-
function formatJson(data) {
|
|
141
|
-
return JSON.stringify(data, null, 2);
|
|
142
|
-
}
|
|
143
|
-
function formatTable(rows) {
|
|
144
|
-
if (rows.length === 0) return "";
|
|
145
|
-
const colCount = rows[0].length;
|
|
146
|
-
const widths = new Array(colCount).fill(0);
|
|
147
|
-
for (const row of rows) {
|
|
148
|
-
for (let i = 0; i < row.length; i++) {
|
|
149
|
-
widths[i] = Math.max(widths[i], (row[i] || "").length);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return rows.map(
|
|
153
|
-
(row) => row.map((cell, i) => (cell || "").padEnd(widths[i])).join(" ")
|
|
154
|
-
).join("\n");
|
|
155
|
-
}
|
|
156
|
-
function humanSize(bytes) {
|
|
157
|
-
if (bytes < 1024) return `${bytes} B`;
|
|
158
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
159
|
-
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
160
|
-
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
|
|
161
|
-
}
|
|
162
|
-
function relativeTime(timestamp) {
|
|
163
|
-
const diff = Date.now() - timestamp;
|
|
164
|
-
const seconds = Math.floor(diff / 1e3);
|
|
165
|
-
if (seconds < 60) return `${seconds}s ago`;
|
|
166
|
-
const minutes = Math.floor(seconds / 60);
|
|
167
|
-
if (minutes < 60) return `${minutes}m ago`;
|
|
168
|
-
const hours = Math.floor(minutes / 60);
|
|
169
|
-
if (hours < 24) return `${hours}h ago`;
|
|
170
|
-
const days = Math.floor(hours / 24);
|
|
171
|
-
return `${days}d ago`;
|
|
172
|
-
}
|
|
173
|
-
function printProgress(label, current, total) {
|
|
174
|
-
if (total <= 0) return;
|
|
175
|
-
const pct = Math.min(100, Math.round(current / total * 100));
|
|
176
|
-
const barWidth = 30;
|
|
177
|
-
const filled = Math.round(barWidth * (current / total));
|
|
178
|
-
const bar = "=".repeat(filled) + (filled < barWidth ? ">" : "") + " ".repeat(Math.max(0, barWidth - filled - 1));
|
|
179
|
-
const sizeStr = `${humanSize(current)}/${humanSize(total)}`;
|
|
180
|
-
const maxLabel = 25;
|
|
181
|
-
const displayLabel = label.length > maxLabel ? "..." + label.slice(-22) : label;
|
|
182
|
-
process.stderr.write(`\r ${displayLabel.padEnd(maxLabel)} [${bar}] ${pct}% ${sizeStr} `);
|
|
183
|
-
if (current >= total) {
|
|
184
|
-
process.stderr.write("\n");
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export { formatTable as a, resolveToken as b, connectToHypha as c, resolveWorkspace as d, hasFlag as e, formatJson as f, getFlagInt as g, humanSize as h, getFlag as i, relativeTime as j, getAllFlags as k, loginToHypha as l, printProgress as m, positionalArgs as p, resolveServerUrl as r };
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { e as hasFlag, l as loginToHypha, c as connectToHypha, g as getFlagInt, i as getFlag, f as formatJson, a as formatTable, r as resolveServerUrl } from './helpers-DWQC3Lr8.mjs';
|
|
2
|
-
import 'fs';
|
|
3
|
-
import 'path';
|
|
4
|
-
import 'os';
|
|
5
|
-
|
|
6
|
-
async function loginCommand(args) {
|
|
7
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
8
|
-
console.log(`Usage: hypha login [server-url]
|
|
9
|
-
|
|
10
|
-
Login to a Hypha server via browser-based OAuth.
|
|
11
|
-
Credentials are saved to ~/.hypha/.env.
|
|
12
|
-
|
|
13
|
-
Default server: https://hypha.aicell.io`);
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const serverUrl = args[0] && !args[0].startsWith("-") ? args[0] : void 0;
|
|
17
|
-
await loginToHypha(serverUrl);
|
|
18
|
-
}
|
|
19
|
-
async function tokenCommand(args) {
|
|
20
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
21
|
-
console.log(`Usage: hypha token [options]
|
|
22
|
-
|
|
23
|
-
Generate a workspace token.
|
|
24
|
-
|
|
25
|
-
Options:
|
|
26
|
-
--expires-in <seconds> Token expiration (default: 3600)
|
|
27
|
-
--permission <perm> Permission level: read, read_write, admin (default: read_write)
|
|
28
|
-
--workspace <ws> Target workspace (default: current)
|
|
29
|
-
--json Output as JSON`);
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const server = await connectToHypha();
|
|
33
|
-
try {
|
|
34
|
-
const expiresIn = getFlagInt(args, "--expires-in") || 3600;
|
|
35
|
-
const permission = getFlag(args, "--permission") || "read_write";
|
|
36
|
-
const workspace = getFlag(args, "--workspace") || server.config.workspace;
|
|
37
|
-
const json = hasFlag(args, "--json");
|
|
38
|
-
const token = await server.generateToken({
|
|
39
|
-
expires_in: expiresIn,
|
|
40
|
-
permission,
|
|
41
|
-
workspace
|
|
42
|
-
});
|
|
43
|
-
if (json) {
|
|
44
|
-
console.log(formatJson({
|
|
45
|
-
token,
|
|
46
|
-
workspace,
|
|
47
|
-
permission,
|
|
48
|
-
expires_in: expiresIn
|
|
49
|
-
}));
|
|
50
|
-
} else {
|
|
51
|
-
console.log(token);
|
|
52
|
-
}
|
|
53
|
-
} finally {
|
|
54
|
-
await server.disconnect();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
async function servicesCommand(args) {
|
|
58
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
59
|
-
console.log(`Usage: hypha services [options]
|
|
60
|
-
|
|
61
|
-
List services in the current workspace.
|
|
62
|
-
|
|
63
|
-
Options:
|
|
64
|
-
--type <type> Filter by service type
|
|
65
|
-
--include-unlisted Include unlisted services
|
|
66
|
-
--json Output as JSON`);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const server = await connectToHypha();
|
|
70
|
-
try {
|
|
71
|
-
const type = getFlag(args, "--type");
|
|
72
|
-
const includeUnlisted = hasFlag(args, "--include-unlisted");
|
|
73
|
-
const json = hasFlag(args, "--json");
|
|
74
|
-
const query = { _rkwargs: true };
|
|
75
|
-
if (type) query.type = type;
|
|
76
|
-
if (includeUnlisted) query.include_unlisted = true;
|
|
77
|
-
const services = await server.listServices(query);
|
|
78
|
-
if (json) {
|
|
79
|
-
console.log(formatJson(services));
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (!services || services.length === 0) {
|
|
83
|
-
console.log("No services found.");
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const rows = [["ID", "NAME", "TYPE", "DESCRIPTION"]];
|
|
87
|
-
for (const svc of services) {
|
|
88
|
-
rows.push([
|
|
89
|
-
svc.id || "",
|
|
90
|
-
svc.name || "",
|
|
91
|
-
svc.type || "",
|
|
92
|
-
(svc.description || "").slice(0, 60)
|
|
93
|
-
]);
|
|
94
|
-
}
|
|
95
|
-
console.log(formatTable(rows));
|
|
96
|
-
} finally {
|
|
97
|
-
await server.disconnect();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
async function clientsCommand(args) {
|
|
101
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
102
|
-
console.log(`Usage: hypha clients [options]
|
|
103
|
-
|
|
104
|
-
List connected clients in the workspace.
|
|
105
|
-
|
|
106
|
-
Options:
|
|
107
|
-
--json Output as JSON`);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
const server = await connectToHypha();
|
|
111
|
-
try {
|
|
112
|
-
const json = hasFlag(args, "--json");
|
|
113
|
-
const wm = await server.getService("public/*:workspace-manager");
|
|
114
|
-
const clients = await wm.listClients({ workspace: server.config.workspace, _rkwargs: true });
|
|
115
|
-
if (json) {
|
|
116
|
-
console.log(formatJson(clients));
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
console.log(`${clients.length} client(s) in workspace ${server.config.workspace}`);
|
|
120
|
-
if (clients.length === 0) return;
|
|
121
|
-
const byToken = {};
|
|
122
|
-
for (const c of clients) {
|
|
123
|
-
const id = typeof c === "string" ? c : c.id || c;
|
|
124
|
-
const key = String(id).split("/").pop()?.split(":")[0] || String(id);
|
|
125
|
-
if (!byToken[key]) byToken[key] = [];
|
|
126
|
-
byToken[key].push(String(id));
|
|
127
|
-
}
|
|
128
|
-
for (const c of clients.slice(0, 30)) {
|
|
129
|
-
const id = typeof c === "string" ? c : c.id || c;
|
|
130
|
-
console.log(` ${id}`);
|
|
131
|
-
}
|
|
132
|
-
if (clients.length > 30) {
|
|
133
|
-
console.log(` ... and ${clients.length - 30} more`);
|
|
134
|
-
}
|
|
135
|
-
} finally {
|
|
136
|
-
await server.disconnect();
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
async function cleanupCommand(args) {
|
|
140
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
141
|
-
console.log(`Usage: hypha cleanup [options]
|
|
142
|
-
|
|
143
|
-
Ping all clients in the workspace and remove dead/stale ones.
|
|
144
|
-
Requires admin permission.
|
|
145
|
-
|
|
146
|
-
Options:
|
|
147
|
-
--timeout <seconds> Ping timeout per client (default: 2)
|
|
148
|
-
--json Output as JSON`);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const server = await connectToHypha();
|
|
152
|
-
try {
|
|
153
|
-
const timeout = getFlagInt(args, "--timeout") || 2;
|
|
154
|
-
const json = hasFlag(args, "--json");
|
|
155
|
-
const ws = server.config.workspace;
|
|
156
|
-
const wm = await server.getService("public/*:workspace-manager");
|
|
157
|
-
const clients = await wm.listClients({ workspace: ws, _rkwargs: true });
|
|
158
|
-
console.log(`Workspace ${ws}: ${clients.length} client(s)`);
|
|
159
|
-
console.log(`Pinging all clients (timeout=${timeout}s)... this may take a while.`);
|
|
160
|
-
const result = await wm.cleanup({ workspace: ws, timeout, _rkwargs: true });
|
|
161
|
-
const removed = result?.removed_clients || result?.removedClients || [];
|
|
162
|
-
if (json) {
|
|
163
|
-
console.log(formatJson(result));
|
|
164
|
-
} else {
|
|
165
|
-
console.log(`Removed ${removed.length} dead client(s).`);
|
|
166
|
-
if (removed.length > 0 && removed.length <= 20) {
|
|
167
|
-
for (const c of removed) {
|
|
168
|
-
console.log(` - ${c}`);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if (result?.removed_workspace || result?.removedWorkspace) {
|
|
172
|
-
console.log(`Workspace ${ws} was removed (no live clients remaining).`);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
} finally {
|
|
176
|
-
await server.disconnect();
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
async function infoCommand(args) {
|
|
180
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
181
|
-
console.log(`Usage: hypha info [--json]
|
|
182
|
-
|
|
183
|
-
Show workspace information.`);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
const server = await connectToHypha();
|
|
187
|
-
try {
|
|
188
|
-
const json = hasFlag(args, "--json");
|
|
189
|
-
const info = {
|
|
190
|
-
server_url: server.config.public_base_url || resolveServerUrl(),
|
|
191
|
-
workspace: server.config.workspace,
|
|
192
|
-
client_id: server.config.client_id
|
|
193
|
-
};
|
|
194
|
-
if (json) {
|
|
195
|
-
console.log(formatJson(info));
|
|
196
|
-
} else {
|
|
197
|
-
console.log(`Server: ${info.server_url}`);
|
|
198
|
-
console.log(`Workspace: ${info.workspace}`);
|
|
199
|
-
console.log(`Client ID: ${info.client_id}`);
|
|
200
|
-
}
|
|
201
|
-
} finally {
|
|
202
|
-
await server.disconnect();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export { cleanupCommand, clientsCommand, infoCommand, loginCommand, servicesCommand, tokenCommand };
|
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import { e as hasFlag, l as loginToHypha, c as connectToHypha, g as getFlagInt, i as getFlag, f as formatJson, a as formatTable, r as resolveServerUrl } from './helpers-DWQC3Lr8.mjs';
|
|
2
|
-
import 'fs';
|
|
3
|
-
import 'path';
|
|
4
|
-
import 'os';
|
|
5
|
-
|
|
6
|
-
async function getWorkspaceManager(server) {
|
|
7
|
-
return server.getService("public/*:workspace-manager");
|
|
8
|
-
}
|
|
9
|
-
async function loginCommand(args) {
|
|
10
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
11
|
-
console.log(`Usage: hypha login [server-url]
|
|
12
|
-
|
|
13
|
-
Login to a Hypha server via browser-based OAuth.
|
|
14
|
-
Credentials are saved to ~/.hypha/.env.
|
|
15
|
-
|
|
16
|
-
Default server: https://hypha.aicell.io`);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const serverUrl = args[0] && !args[0].startsWith("-") ? args[0] : void 0;
|
|
20
|
-
await loginToHypha(serverUrl);
|
|
21
|
-
}
|
|
22
|
-
async function tokenCommand(args) {
|
|
23
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
24
|
-
console.log(`Usage: hypha token [options]
|
|
25
|
-
|
|
26
|
-
Generate a workspace token.
|
|
27
|
-
|
|
28
|
-
Options:
|
|
29
|
-
--expires-in <seconds> Token expiration (default: 3600)
|
|
30
|
-
--permission <perm> Permission level: read, read_write, admin (default: read_write)
|
|
31
|
-
--workspace <ws> Target workspace (default: current)
|
|
32
|
-
--json Output as JSON`);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const server = await connectToHypha();
|
|
36
|
-
try {
|
|
37
|
-
const expiresIn = getFlagInt(args, "--expires-in") || 3600;
|
|
38
|
-
const permission = getFlag(args, "--permission") || "read_write";
|
|
39
|
-
const workspace = getFlag(args, "--workspace") || server.config.workspace;
|
|
40
|
-
const json = hasFlag(args, "--json");
|
|
41
|
-
const token = await server.generateToken({
|
|
42
|
-
expires_in: expiresIn,
|
|
43
|
-
permission,
|
|
44
|
-
workspace
|
|
45
|
-
});
|
|
46
|
-
if (json) {
|
|
47
|
-
console.log(formatJson({
|
|
48
|
-
token,
|
|
49
|
-
workspace,
|
|
50
|
-
permission,
|
|
51
|
-
expires_in: expiresIn
|
|
52
|
-
}));
|
|
53
|
-
} else {
|
|
54
|
-
console.log(token);
|
|
55
|
-
}
|
|
56
|
-
} finally {
|
|
57
|
-
await server.disconnect();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
async function servicesCommand(args) {
|
|
61
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
62
|
-
console.log(`Usage: hypha services [options]
|
|
63
|
-
|
|
64
|
-
List services in the current workspace.
|
|
65
|
-
|
|
66
|
-
Options:
|
|
67
|
-
--type <type> Filter by service type
|
|
68
|
-
--include-unlisted Include unlisted services
|
|
69
|
-
--json Output as JSON`);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const server = await connectToHypha();
|
|
73
|
-
try {
|
|
74
|
-
const type = getFlag(args, "--type");
|
|
75
|
-
const includeUnlisted = hasFlag(args, "--include-unlisted");
|
|
76
|
-
const json = hasFlag(args, "--json");
|
|
77
|
-
const query = { _rkwargs: true };
|
|
78
|
-
if (type) query.type = type;
|
|
79
|
-
if (includeUnlisted) query.include_unlisted = true;
|
|
80
|
-
const services = await server.listServices(query);
|
|
81
|
-
if (json) {
|
|
82
|
-
console.log(formatJson(services));
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (!services || services.length === 0) {
|
|
86
|
-
console.log("No services found.");
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const rows = [["ID", "NAME", "TYPE", "DESCRIPTION"]];
|
|
90
|
-
for (const svc of services) {
|
|
91
|
-
rows.push([
|
|
92
|
-
svc.id || "",
|
|
93
|
-
svc.name || "",
|
|
94
|
-
svc.type || "",
|
|
95
|
-
(svc.description || "").slice(0, 60)
|
|
96
|
-
]);
|
|
97
|
-
}
|
|
98
|
-
console.log(formatTable(rows));
|
|
99
|
-
} finally {
|
|
100
|
-
await server.disconnect();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
async function clientsCommand(args) {
|
|
104
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
105
|
-
console.log(`Usage: hypha clients [workspace] [options]
|
|
106
|
-
|
|
107
|
-
List connected clients in a workspace. Defaults to the connected workspace.
|
|
108
|
-
Use a positional argument to target a different workspace (requires admin).
|
|
109
|
-
|
|
110
|
-
Options:
|
|
111
|
-
--json Output as JSON`);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
const server = await connectToHypha();
|
|
115
|
-
try {
|
|
116
|
-
const json = hasFlag(args, "--json");
|
|
117
|
-
const targetWs = args[0] && !args[0].startsWith("-") ? args[0] : server.config.workspace;
|
|
118
|
-
const wm = await getWorkspaceManager(server);
|
|
119
|
-
const clients = await wm.listClients({ workspace: targetWs, _rkwargs: true });
|
|
120
|
-
if (json) {
|
|
121
|
-
console.log(formatJson(clients));
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
console.log(`${clients.length} client(s) in workspace ${targetWs}`);
|
|
125
|
-
if (clients.length === 0) return;
|
|
126
|
-
for (const c of clients.slice(0, 50)) {
|
|
127
|
-
console.log(` ${c}`);
|
|
128
|
-
}
|
|
129
|
-
if (clients.length > 50) {
|
|
130
|
-
console.log(` ... and ${clients.length - 50} more`);
|
|
131
|
-
}
|
|
132
|
-
} finally {
|
|
133
|
-
await server.disconnect();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
async function pingCommand(args) {
|
|
137
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
138
|
-
console.log(`Usage: hypha ping <client-id> [options]
|
|
139
|
-
|
|
140
|
-
Ping a specific client to check if it's alive.
|
|
141
|
-
|
|
142
|
-
Options:
|
|
143
|
-
--timeout <seconds> Ping timeout (default: 5)`);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const clientId = args[0] && !args[0].startsWith("-") ? args[0] : void 0;
|
|
147
|
-
if (!clientId) {
|
|
148
|
-
console.error("Usage: hypha ping <client-id>");
|
|
149
|
-
process.exit(1);
|
|
150
|
-
}
|
|
151
|
-
const server = await connectToHypha();
|
|
152
|
-
try {
|
|
153
|
-
const timeout = getFlagInt(args, "--timeout") || 5;
|
|
154
|
-
const wm = await getWorkspaceManager(server);
|
|
155
|
-
try {
|
|
156
|
-
const result = await wm.pingClient({ client_id: clientId, timeout, _rkwargs: true });
|
|
157
|
-
console.log(`${clientId}: ${result}`);
|
|
158
|
-
} catch (e) {
|
|
159
|
-
console.log(`${clientId}: dead (${e.message || e})`);
|
|
160
|
-
}
|
|
161
|
-
} finally {
|
|
162
|
-
await server.disconnect();
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
async function kickCommand(args) {
|
|
166
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
167
|
-
console.log(`Usage: hypha kick <client-id> [workspace]
|
|
168
|
-
|
|
169
|
-
Disconnect a client and remove all its services. Requires admin.
|
|
170
|
-
If workspace is not specified, uses the connected workspace.`);
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const clientId = args[0] && !args[0].startsWith("-") ? args[0] : void 0;
|
|
174
|
-
if (!clientId) {
|
|
175
|
-
console.error("Usage: hypha kick <client-id> [workspace]");
|
|
176
|
-
process.exit(1);
|
|
177
|
-
}
|
|
178
|
-
const server = await connectToHypha();
|
|
179
|
-
try {
|
|
180
|
-
const targetWs = args[1] && !args[1].startsWith("-") ? args[1] : server.config.workspace;
|
|
181
|
-
const wm = await getWorkspaceManager(server);
|
|
182
|
-
await wm.deleteClient({ client_id: clientId, workspace: targetWs, _rkwargs: true });
|
|
183
|
-
console.log(`Kicked client ${clientId} from ${targetWs}`);
|
|
184
|
-
} finally {
|
|
185
|
-
await server.disconnect();
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
async function cleanupCommand(args) {
|
|
189
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
190
|
-
console.log(`Usage: hypha cleanup [workspace] [options]
|
|
191
|
-
|
|
192
|
-
Ping all clients in a workspace and remove dead/stale ones.
|
|
193
|
-
Requires admin permission.
|
|
194
|
-
Defaults to the connected workspace. Pass workspace name as positional arg.
|
|
195
|
-
|
|
196
|
-
Options:
|
|
197
|
-
--timeout <seconds> Ping timeout per client (default: 2)
|
|
198
|
-
--json Output as JSON`);
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
const server = await connectToHypha();
|
|
202
|
-
try {
|
|
203
|
-
const timeout = getFlagInt(args, "--timeout") || 2;
|
|
204
|
-
const json = hasFlag(args, "--json");
|
|
205
|
-
const targetWs = args[0] && !args[0].startsWith("-") ? args[0] : server.config.workspace;
|
|
206
|
-
const wm = await getWorkspaceManager(server);
|
|
207
|
-
const clients = await wm.listClients({ workspace: targetWs, _rkwargs: true });
|
|
208
|
-
console.log(`Workspace ${targetWs}: ${clients.length} client(s)`);
|
|
209
|
-
console.log(`Pinging all clients (timeout=${timeout}s)... this may take a while.`);
|
|
210
|
-
const result = await wm.cleanup({ workspace: targetWs, timeout, _rkwargs: true });
|
|
211
|
-
const removed = result?.removed_clients || result?.removedClients || [];
|
|
212
|
-
if (json) {
|
|
213
|
-
console.log(formatJson(result));
|
|
214
|
-
} else {
|
|
215
|
-
console.log(`Removed ${removed.length} dead client(s).`);
|
|
216
|
-
if (removed.length > 0 && removed.length <= 30) {
|
|
217
|
-
for (const c of removed) {
|
|
218
|
-
console.log(` - ${c}`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (result?.removed_workspace || result?.removedWorkspace) {
|
|
222
|
-
console.log(`Workspace ${targetWs} was removed (no live clients remaining).`);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
} finally {
|
|
226
|
-
await server.disconnect();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
async function workspaceInfoCommand(args) {
|
|
230
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
231
|
-
console.log(`Usage: hypha workspace-info [workspace] [--json]
|
|
232
|
-
|
|
233
|
-
Show detailed workspace information (admin).`);
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
const server = await connectToHypha();
|
|
237
|
-
try {
|
|
238
|
-
const json = hasFlag(args, "--json");
|
|
239
|
-
const targetWs = args[0] && !args[0].startsWith("-") ? args[0] : server.config.workspace;
|
|
240
|
-
const wm = await getWorkspaceManager(server);
|
|
241
|
-
const wsInfo = await wm.getWorkspaceInfo({ workspace: targetWs, _rkwargs: true });
|
|
242
|
-
if (json) {
|
|
243
|
-
console.log(formatJson(wsInfo));
|
|
244
|
-
} else {
|
|
245
|
-
console.log(`Workspace: ${wsInfo.id || wsInfo.name || targetWs}`);
|
|
246
|
-
for (const [k, v] of Object.entries(wsInfo)) {
|
|
247
|
-
if (k === "id" || k === "name") continue;
|
|
248
|
-
const val = typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
249
|
-
console.log(` ${k}: ${val.slice(0, 120)}`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
} finally {
|
|
253
|
-
await server.disconnect();
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
async function infoCommand(args) {
|
|
257
|
-
if (hasFlag(args, "--help", "-h")) {
|
|
258
|
-
console.log(`Usage: hypha info [--json]
|
|
259
|
-
|
|
260
|
-
Show connection information.`);
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
const server = await connectToHypha();
|
|
264
|
-
try {
|
|
265
|
-
const json = hasFlag(args, "--json");
|
|
266
|
-
const info = {
|
|
267
|
-
server_url: server.config.public_base_url || resolveServerUrl(),
|
|
268
|
-
workspace: server.config.workspace,
|
|
269
|
-
client_id: server.config.client_id
|
|
270
|
-
};
|
|
271
|
-
if (json) {
|
|
272
|
-
console.log(formatJson(info));
|
|
273
|
-
} else {
|
|
274
|
-
console.log(`Server: ${info.server_url}`);
|
|
275
|
-
console.log(`Workspace: ${info.workspace}`);
|
|
276
|
-
console.log(`Client ID: ${info.client_id}`);
|
|
277
|
-
}
|
|
278
|
-
} finally {
|
|
279
|
-
await server.disconnect();
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export { cleanupCommand, clientsCommand, infoCommand, kickCommand, loginCommand, pingCommand, servicesCommand, tokenCommand, workspaceInfoCommand };
|