aweshare 0.7.6 → 0.7.8
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.ai.md +2 -2
- package/README.md +49 -9
- package/apps/agent/dist/config.d.ts +1 -1
- package/apps/agent/dist/config.js +1 -0
- package/apps/agent/dist/config.js.map +1 -1
- package/apps/agent/dist/connection.js +1 -0
- package/apps/agent/dist/connection.js.map +1 -1
- package/apps/agent/dist/table.js +7 -0
- package/apps/agent/dist/table.js.map +1 -1
- package/apps/consumer/dist/cli.js +9 -1
- package/apps/consumer/dist/cli.js.map +1 -1
- package/apps/consumer/dist/hub.d.ts +5 -0
- package/apps/consumer/dist/hub.js +7 -1
- package/apps/consumer/dist/hub.js.map +1 -1
- package/apps/consumer/dist/table.d.ts +23 -3
- package/apps/consumer/dist/table.js +63 -7
- package/apps/consumer/dist/table.js.map +1 -1
- package/apps/hub/dist/admin.js +39 -0
- package/apps/hub/dist/admin.js.map +1 -1
- package/apps/hub/dist/cli.js +425 -108
- package/apps/hub/dist/cli.js.map +1 -1
- package/apps/hub/dist/config.d.ts +3 -0
- package/apps/hub/dist/config.js +1 -0
- package/apps/hub/dist/config.js.map +1 -1
- package/apps/hub/dist/consumer.d.ts +56 -0
- package/apps/hub/dist/consumer.js +152 -14
- package/apps/hub/dist/consumer.js.map +1 -1
- package/apps/hub/dist/db.d.ts +3 -0
- package/apps/hub/dist/db.js +20 -2
- package/apps/hub/dist/db.js.map +1 -1
- package/apps/hub/dist/groups.d.ts +86 -0
- package/apps/hub/dist/groups.js +213 -0
- package/apps/hub/dist/groups.js.map +1 -0
- package/apps/hub/dist/local.d.ts +19 -2
- package/apps/hub/dist/local.js +30 -3
- package/apps/hub/dist/local.js.map +1 -1
- package/apps/hub/dist/rest.d.ts +37 -0
- package/apps/hub/dist/rest.js +66 -0
- package/apps/hub/dist/rest.js.map +1 -0
- package/apps/hub/dist/server.d.ts +10 -4
- package/apps/hub/dist/server.js +42 -10
- package/apps/hub/dist/server.js.map +1 -1
- package/apps/hub/dist/table.d.ts +6 -0
- package/apps/hub/dist/table.js +6 -0
- package/apps/hub/dist/table.js.map +1 -1
- package/apps/hub/dist/tokens.d.ts +2 -2
- package/apps/hub/dist/tokens.js +8 -2
- package/apps/hub/dist/tokens.js.map +1 -1
- package/package.json +1 -1
- package/packages/producer-core/dist/adapters.js +5 -1
- package/packages/producer-core/dist/adapters.js.map +1 -1
- package/packages/producer-core/dist/catalog.d.ts +2 -0
- package/packages/producer-core/dist/catalog.js +56 -0
- package/packages/producer-core/dist/catalog.js.map +1 -1
- package/packages/producer-core/dist/probe.d.ts +2 -1
- package/packages/producer-core/dist/probe.js +28 -23
- package/packages/producer-core/dist/probe.js.map +1 -1
package/apps/hub/dist/cli.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { fmtDate, fmtTime, fmtTokens, isValidAlias } from '../../../packages/protocol/dist/index.js';
|
|
3
|
+
import { fmtDate, fmtDuration, fmtTime, fmtTokens, isValidAlias } from '../../../packages/protocol/dist/index.js';
|
|
4
4
|
import { parseBackups, readBackupsFile, serializeBackups, writeBackupsFile } from './backups.js';
|
|
5
5
|
import { ensureDataDir, HUB_CONFIG_TEMPLATE, initHubProduceFiles, loadHubConfig, readAdminToken, resolvePaths, } from './config.js';
|
|
6
|
+
import { parseGroups, readGroupsFile, serializeGroups, writeGroupsFile, } from './groups.js';
|
|
6
7
|
import { createHubServer, installConfigReloader } from './server.js';
|
|
7
8
|
import { refreshTargets, renderConsumers, renderHubStatus, renderInvites, renderOfferings, renderProducers, renderUsage, renderUsageSummary, } from './table.js';
|
|
8
|
-
import { loadOrCreateAdminToken, loadOrCreatePepper } from './tokens.js';
|
|
9
9
|
const VERSION = JSON.parse(readFileSync(new URL('../../../package.json', import.meta.url), 'utf8')).version;
|
|
10
10
|
// One-liners only; flags per command live in their own -h, everything else in README.
|
|
11
11
|
const USAGE = `Usage: aweshare hub [OPTIONS] COMMAND [ARGS]...
|
|
@@ -20,11 +20,13 @@ Commands:
|
|
|
20
20
|
init Create the data dir, pepper, and admin token.
|
|
21
21
|
serve Run the hub server (the only runner; config.produce.toml mounts itself).
|
|
22
22
|
produce Hub-local models: init scaffolds, refresh re-anchors today's budgets.
|
|
23
|
+
probe Force a health re-check of hub-local backends, skipping the backoff.
|
|
23
24
|
admin Govern: invite mint|revoke|restore, offering revoke|restore.
|
|
24
25
|
list Read hub state: invites (default), producers, consumers, offerings, usage.
|
|
25
26
|
status Live dashboard: capacity, 5m health, admission pressure, defaults.
|
|
26
27
|
limits Show, set or clear one or more consumers' limit overrides.
|
|
27
|
-
backups Per-alias failover queues: show, add, remove or clear
|
|
28
|
+
backups Per-alias failover queues: show, add, remove or clear.
|
|
29
|
+
groups Route groups: one name in front of an ordered member list.`;
|
|
28
30
|
const COMMAND_HELP = {
|
|
29
31
|
init: `Usage: aweshare hub init
|
|
30
32
|
|
|
@@ -114,6 +116,39 @@ Options:
|
|
|
114
116
|
|
|
115
117
|
Options:
|
|
116
118
|
-h, --help Show this message and exit.`,
|
|
119
|
+
probe: `Usage: aweshare hub probe [BACKEND...] [OPTIONS]
|
|
120
|
+
|
|
121
|
+
Force a health re-check of the hub-local catalog's backends, ignoring the
|
|
122
|
+
recovery-probe backoff.
|
|
123
|
+
|
|
124
|
+
A backend that degraded once stays degraded until a probe succeeds, and the
|
|
125
|
+
backoff between probes grows with the length of the streak — 5 minutes at
|
|
126
|
+
first, then hourly, then daily. That is right for a genuinely dead upstream,
|
|
127
|
+
and wrong for one that has long since come back: the streak outlives the
|
|
128
|
+
outage and nothing re-checks it. Neither editing config.produce.toml nor
|
|
129
|
+
sending SIGHUP touches the in-memory health state, so before this command
|
|
130
|
+
the only recovery was restarting the hub.
|
|
131
|
+
|
|
132
|
+
A probe names the offering's real upstream model, so a 200 clears the
|
|
133
|
+
degraded state and the alias returns to online immediately. Nothing is
|
|
134
|
+
mutated when a probe fails — the probe is the whole action.
|
|
135
|
+
|
|
136
|
+
With no argument every degraded hub-local backend is probed. Name one or
|
|
137
|
+
several to narrow the pass; ids are the [[backends]] ids in
|
|
138
|
+
config.produce.toml (see: aweshare hub list offerings).
|
|
139
|
+
|
|
140
|
+
Only hub-local (hub/...) backends are the hub's to probe. A producer's own
|
|
141
|
+
backends live in that producer's agent — it runs 'aweshare producer
|
|
142
|
+
doctor' there.
|
|
143
|
+
|
|
144
|
+
Options:
|
|
145
|
+
--json Print the raw API response.
|
|
146
|
+
-h, --help Show this message and exit.
|
|
147
|
+
|
|
148
|
+
Examples:
|
|
149
|
+
aweshare hub probe # every degraded backend, right now
|
|
150
|
+
aweshare hub probe kimi # one backend
|
|
151
|
+
aweshare hub probe kimi zhipu # several, comma- or space-separated`,
|
|
117
152
|
'admin invite': `Usage: aweshare hub admin invite [mint|revoke|restore|extend] [ARGS] [OPTIONS]
|
|
118
153
|
|
|
119
154
|
The invite lifecycle. One code, one identity — and the invite is the
|
|
@@ -250,6 +285,48 @@ Options:
|
|
|
250
285
|
--clear Drop ALIAS's queue entirely (the key disappears).
|
|
251
286
|
--json Print the queue(s) as JSON.
|
|
252
287
|
-h, --help Show this message and exit.`,
|
|
288
|
+
groups: `Usage: aweshare hub groups [GROUP] [OPTIONS]
|
|
289
|
+
|
|
290
|
+
Route groups in the data dir's config.groups.toml: one name in front of
|
|
291
|
+
an ordered list of member aliases. A consumer calling the group name is
|
|
292
|
+
served by the first member that can — the members are tried in order,
|
|
293
|
+
each at most once per request, and a member that resolves to nothing is
|
|
294
|
+
skipped. Members are ordinary aliases, each keeping its own caps, budget
|
|
295
|
+
and audit; a group may only pool members in its OWN namespace (a hub/
|
|
296
|
+
group lists hub/... aliases), since pooling another producer's model
|
|
297
|
+
would spend their quota. A group name that is also a real alias resolves
|
|
298
|
+
to the real alias. Edits hot-reload within ~2s. The file is this
|
|
299
|
+
machine's, so the command edits it locally: on a remote hub run it there
|
|
300
|
+
(ssh / docker exec), like every other config.toml change.
|
|
301
|
+
|
|
302
|
+
Caps: a group that sets none inherits from its members — DAILY TOKENS is
|
|
303
|
+
their sum (any unlimited member makes the pool unlimited), MAX USERS and
|
|
304
|
+
PER USER are their max. Override any of them on the group itself, or drop
|
|
305
|
+
the overrides with --clear-caps to inherit again.
|
|
306
|
+
|
|
307
|
+
Bare GROUP shows its members and caps; no GROUP lists every group.
|
|
308
|
+
|
|
309
|
+
Examples:
|
|
310
|
+
aweshare hub groups # every group
|
|
311
|
+
aweshare hub groups hub/step-5-preview # one group
|
|
312
|
+
aweshare hub groups hub/step-5-preview --add hub/step-5-preview-2
|
|
313
|
+
aweshare hub groups hub/step-5-preview --add hub/step-5-preview-0 --first
|
|
314
|
+
aweshare hub groups hub/step-5-preview --remove hub/step-5-preview-2
|
|
315
|
+
aweshare hub groups hub/step-5-preview --max-users 6 --per-user 4
|
|
316
|
+
aweshare hub groups hub/step-5-preview --clear-caps
|
|
317
|
+
aweshare hub groups hub/step-5-preview --clear
|
|
318
|
+
|
|
319
|
+
Options:
|
|
320
|
+
--add ALIASES Append (comma-separated aliases accepted); --first prepends instead.
|
|
321
|
+
--first With --add: put the new members at the front of the list.
|
|
322
|
+
--remove A Remove one member alias from GROUP.
|
|
323
|
+
--daily-tokens N Override the group's daily token cap (0 = unlimited).
|
|
324
|
+
--max-users N Override the group's distinct-consumer cap.
|
|
325
|
+
--per-user N Override the group's per-consumer concurrency cap.
|
|
326
|
+
--clear-caps Drop every cap override (back to inheriting from members).
|
|
327
|
+
--clear Drop GROUP entirely (the key disappears).
|
|
328
|
+
--json Print the group(s) as JSON.
|
|
329
|
+
-h, --help Show this message and exit.`,
|
|
253
330
|
status: `Usage: aweshare hub status
|
|
254
331
|
|
|
255
332
|
The hub's live dashboard: capacity, the last-5m health, admission
|
|
@@ -506,6 +583,272 @@ const SORT_LABELS = {
|
|
|
506
583
|
tokens: 'tokens, busiest first',
|
|
507
584
|
requests: 'requests, busiest first',
|
|
508
585
|
};
|
|
586
|
+
const BACKUPS_FILE = {
|
|
587
|
+
read: readBackupsFile,
|
|
588
|
+
write: writeBackupsFile,
|
|
589
|
+
parse: parseBackups,
|
|
590
|
+
serialize: serializeBackups,
|
|
591
|
+
};
|
|
592
|
+
/** `hub backups` / `hub groups`: view (all / one), mutate (add/remove/clear),
|
|
593
|
+
* revalidate the whole file the way the hub parses it, write once. Every
|
|
594
|
+
* mutation lands in memory first, so a rejected flag leaves the file
|
|
595
|
+
* untouched. */
|
|
596
|
+
function runQueueCommand(spec, positional, flags) {
|
|
597
|
+
const { file, api, words } = spec;
|
|
598
|
+
const alias = positional[0];
|
|
599
|
+
if (positional[1])
|
|
600
|
+
fail(`unexpected argument '${positional[1]}' (see: aweshare hub ${words.command} -h)`);
|
|
601
|
+
const json = flags.get('json') === 'true';
|
|
602
|
+
const add = flags.get('add');
|
|
603
|
+
const remove = flags.get('remove');
|
|
604
|
+
const clear = flags.get('clear') === 'true';
|
|
605
|
+
const mutating = add !== undefined || remove !== undefined || clear;
|
|
606
|
+
if (mutating && alias === undefined)
|
|
607
|
+
fail(`which ${words.noun} to modify: pass ALIAS (see: aweshare hub ${words.command} -h)`);
|
|
608
|
+
if (clear && (add !== undefined || remove !== undefined))
|
|
609
|
+
fail(`--clear drops the whole ${words.noun} — drop --add/--remove too`);
|
|
610
|
+
if (flags.get('first') === 'true' && add === undefined)
|
|
611
|
+
fail('--first only makes sense with --add');
|
|
612
|
+
let queues;
|
|
613
|
+
try {
|
|
614
|
+
queues = api.read(file);
|
|
615
|
+
}
|
|
616
|
+
catch (err) {
|
|
617
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
618
|
+
}
|
|
619
|
+
if (!mutating) {
|
|
620
|
+
const print = (key, queue) => console.log(queue && queue.length > 0
|
|
621
|
+
? `${key} → ${queue.join(', ')}`
|
|
622
|
+
: `${key}: no ${words.entry}s configured`);
|
|
623
|
+
if (!alias) {
|
|
624
|
+
if (queues.size === 0) {
|
|
625
|
+
console.log(`no ${words.noun}s configured — create one:`);
|
|
626
|
+
console.log(` aweshare hub ${words.command} SOME/ALIAS ${words.addHint}`);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
if (json)
|
|
630
|
+
console.log(JSON.stringify(Object.fromEntries(queues), null, 2));
|
|
631
|
+
else
|
|
632
|
+
for (const [key, queue] of queues)
|
|
633
|
+
print(key, queue);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
const queue = queues.get(alias);
|
|
637
|
+
if (json)
|
|
638
|
+
console.log(JSON.stringify({ [alias]: queue ?? [] }, null, 2));
|
|
639
|
+
else
|
|
640
|
+
print(alias, queue);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
// unreachable in plain control flow (mutating implies ALIAS, checked
|
|
644
|
+
// above) but needed so the compiler sees the string
|
|
645
|
+
if (alias === undefined)
|
|
646
|
+
fail(`which ${words.noun} to modify: pass ALIAS (see: aweshare hub ${words.command} -h)`);
|
|
647
|
+
if (!isValidAlias(alias))
|
|
648
|
+
fail(`'${alias}' is not an alias (namespace/name, lowercase)`);
|
|
649
|
+
if (clear) {
|
|
650
|
+
if (!queues.delete(alias))
|
|
651
|
+
fail(`${alias}: no ${words.noun} configured (see: aweshare hub ${words.command})`);
|
|
652
|
+
}
|
|
653
|
+
if (remove !== undefined) {
|
|
654
|
+
for (const target of splitAliases(remove)) {
|
|
655
|
+
const queue = queues.get(alias);
|
|
656
|
+
if (!queue?.includes(target))
|
|
657
|
+
fail(`${alias}: '${target}' is not in its ${words.noun}`);
|
|
658
|
+
const rest = queue.filter((entry) => entry !== target);
|
|
659
|
+
if (rest.length > 0)
|
|
660
|
+
queues.set(alias, rest);
|
|
661
|
+
else
|
|
662
|
+
queues.delete(alias);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (add !== undefined) {
|
|
666
|
+
const additions = splitAliases(add);
|
|
667
|
+
if (new Set(additions).size !== additions.length)
|
|
668
|
+
fail(`--add repeats an alias: '${add}'`);
|
|
669
|
+
for (const entry of additions) {
|
|
670
|
+
if (!isValidAlias(entry))
|
|
671
|
+
fail(`'${entry}' is not an alias (namespace/name, lowercase)`);
|
|
672
|
+
if (entry === alias)
|
|
673
|
+
fail(`${alias}: it cannot be its own ${words.entry}`);
|
|
674
|
+
if (queues.get(alias)?.includes(entry))
|
|
675
|
+
fail(`${alias}: '${entry}' is already in its ${words.noun}`);
|
|
676
|
+
}
|
|
677
|
+
const queue = queues.get(alias) ?? [];
|
|
678
|
+
queues.set(alias, flags.get('first') === 'true' ? [...additions, ...queue] : [...queue, ...additions]);
|
|
679
|
+
}
|
|
680
|
+
try {
|
|
681
|
+
// revalidate the whole file the way the hub will parse it
|
|
682
|
+
api.parse(api.serialize(queues));
|
|
683
|
+
}
|
|
684
|
+
catch (err) {
|
|
685
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
686
|
+
}
|
|
687
|
+
api.write(file, queues);
|
|
688
|
+
const queue = queues.get(alias);
|
|
689
|
+
if (json) {
|
|
690
|
+
console.log(JSON.stringify(Object.fromEntries(queues), null, 2));
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
const shown = queue && queue.length > 0 ? queue.join(', ') : `(none — ${words.noun} removed)`;
|
|
694
|
+
console.log(`✓ ${alias} → ${shown}`);
|
|
695
|
+
console.log(`(${words.fileName} written — the hub picks it up within ~2s)`);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/** `hub groups`: view (all / one), mutate (add/remove/clear), and set or drop
|
|
699
|
+
* the group's own cap overrides. A group with no overrides inherits from its
|
|
700
|
+
* members (dailyTokens summed, concurrency caps maxed), which is the state
|
|
701
|
+
* `--daily-tokens`/`--max-users`/`--per-user` depart from and `--clear-caps`
|
|
702
|
+
* returns to. Every mutation lands in memory first, so a rejected flag leaves
|
|
703
|
+
* the file untouched. */
|
|
704
|
+
function runGroupCommand(positional, flags) {
|
|
705
|
+
const file = resolvePaths().groupsFile;
|
|
706
|
+
const alias = positional[0];
|
|
707
|
+
if (positional[1])
|
|
708
|
+
fail(`unexpected argument '${positional[1]}' (see: aweshare hub groups -h)`);
|
|
709
|
+
const json = flags.get('json') === 'true';
|
|
710
|
+
const add = flags.get('add');
|
|
711
|
+
const remove = flags.get('remove');
|
|
712
|
+
const clear = flags.get('clear') === 'true';
|
|
713
|
+
const clearCaps = flags.get('clear-caps') === 'true';
|
|
714
|
+
const dailyTokens = flags.get('daily-tokens');
|
|
715
|
+
const maxUsers = flags.get('max-users');
|
|
716
|
+
const perUser = flags.get('per-user');
|
|
717
|
+
const settingCaps = dailyTokens !== undefined || maxUsers !== undefined || perUser !== undefined;
|
|
718
|
+
const mutating = add !== undefined || remove !== undefined || clear || clearCaps || settingCaps;
|
|
719
|
+
if (mutating && alias === undefined)
|
|
720
|
+
fail('which group to modify: pass ALIAS (see: aweshare hub groups -h)');
|
|
721
|
+
if (clear && (add !== undefined || remove !== undefined || clearCaps || settingCaps))
|
|
722
|
+
fail('--clear drops the whole group — drop the other flags too');
|
|
723
|
+
if (clearCaps && settingCaps)
|
|
724
|
+
fail('--clear-caps drops every override — drop the value flags too');
|
|
725
|
+
if (flags.get('first') === 'true' && add === undefined)
|
|
726
|
+
fail('--first only makes sense with --add');
|
|
727
|
+
if (settingCaps && add === undefined && remove === undefined && alias === undefined)
|
|
728
|
+
fail('which group to modify: pass ALIAS (see: aweshare hub groups -h)');
|
|
729
|
+
let groups;
|
|
730
|
+
try {
|
|
731
|
+
groups = readGroupsFile(file);
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
735
|
+
}
|
|
736
|
+
const describe = (spec) => {
|
|
737
|
+
if (spec === undefined)
|
|
738
|
+
return 'no route group configured';
|
|
739
|
+
const caps = [];
|
|
740
|
+
if (spec.dailyTokens !== undefined)
|
|
741
|
+
caps.push(`dailyTokens=${spec.dailyTokens}`);
|
|
742
|
+
if (spec.maxConcurrentUsers !== undefined)
|
|
743
|
+
caps.push(`maxUsers=${spec.maxConcurrentUsers}`);
|
|
744
|
+
if (spec.maxConcurrencyPerUser !== undefined)
|
|
745
|
+
caps.push(`perUser=${spec.maxConcurrencyPerUser}`);
|
|
746
|
+
const inherited = caps.length === 0 ? ' (caps inherited: tokens summed, concurrency maxed)' : '';
|
|
747
|
+
return `${spec.members.join(', ')}${inherited}${caps.length > 0 ? ` [${caps.join(' ')}]` : ''}`;
|
|
748
|
+
};
|
|
749
|
+
if (!mutating) {
|
|
750
|
+
if (!alias) {
|
|
751
|
+
if (groups.size === 0) {
|
|
752
|
+
console.log('no route groups configured — create one:');
|
|
753
|
+
console.log(' aweshare hub groups hub/SOMETHING --add hub/MEMBER-1,hub/MEMBER-2');
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
if (json)
|
|
757
|
+
console.log(JSON.stringify(Object.fromEntries(groups), null, 2));
|
|
758
|
+
else
|
|
759
|
+
for (const [key, spec] of groups)
|
|
760
|
+
console.log(`${key} → ${describe(spec)}`);
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
const spec = groups.get(alias);
|
|
764
|
+
if (json)
|
|
765
|
+
console.log(JSON.stringify({ [alias]: spec ?? null }, null, 2));
|
|
766
|
+
else
|
|
767
|
+
console.log(`${alias} → ${describe(spec)}`);
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
if (alias === undefined)
|
|
771
|
+
fail('which group to modify: pass ALIAS (see: aweshare hub groups -h)');
|
|
772
|
+
if (!isValidAlias(alias))
|
|
773
|
+
fail(`'${alias}' is not an alias (namespace/name, lowercase)`);
|
|
774
|
+
const namespace = alias.slice(0, alias.indexOf('/'));
|
|
775
|
+
if (clear) {
|
|
776
|
+
if (!groups.delete(alias))
|
|
777
|
+
fail(`${alias}: no route group configured (see: aweshare hub groups)`);
|
|
778
|
+
}
|
|
779
|
+
const current = () => groups.get(alias) ?? { members: [] };
|
|
780
|
+
if (remove !== undefined) {
|
|
781
|
+
for (const target of splitAliases(remove)) {
|
|
782
|
+
const spec = groups.get(alias);
|
|
783
|
+
if (!spec?.members.includes(target))
|
|
784
|
+
fail(`${alias}: '${target}' is not a member`);
|
|
785
|
+
const rest = spec.members.filter((entry) => entry !== target);
|
|
786
|
+
if (rest.length > 0)
|
|
787
|
+
groups.set(alias, { ...spec, members: rest });
|
|
788
|
+
else
|
|
789
|
+
groups.delete(alias);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (add !== undefined) {
|
|
793
|
+
const additions = splitAliases(add);
|
|
794
|
+
if (new Set(additions).size !== additions.length)
|
|
795
|
+
fail(`--add repeats an alias: '${add}'`);
|
|
796
|
+
for (const entry of additions) {
|
|
797
|
+
if (!isValidAlias(entry))
|
|
798
|
+
fail(`'${entry}' is not an alias (namespace/name, lowercase)`);
|
|
799
|
+
if (entry === alias)
|
|
800
|
+
fail(`${alias}: it cannot be its own member`);
|
|
801
|
+
if (entry.slice(0, entry.indexOf('/')) !== namespace)
|
|
802
|
+
fail(`${alias}: '${entry}' is outside its namespace — a group may only pool '${namespace}/...' aliases`);
|
|
803
|
+
if (current().members.includes(entry))
|
|
804
|
+
fail(`${alias}: '${entry}' is already a member`);
|
|
805
|
+
}
|
|
806
|
+
const spec = current();
|
|
807
|
+
const members = flags.get('first') === 'true'
|
|
808
|
+
? [...additions, ...spec.members]
|
|
809
|
+
: [...spec.members, ...additions];
|
|
810
|
+
groups.set(alias, { ...spec, members });
|
|
811
|
+
}
|
|
812
|
+
if (settingCaps || clearCaps) {
|
|
813
|
+
const spec = current();
|
|
814
|
+
const next = { members: [...spec.members] };
|
|
815
|
+
if (!clearCaps) {
|
|
816
|
+
const daily = dailyTokens === undefined ? spec.dailyTokens : Number(dailyTokens);
|
|
817
|
+
const users = maxUsers === undefined ? spec.maxConcurrentUsers : Number(maxUsers);
|
|
818
|
+
const per = perUser === undefined ? spec.maxConcurrencyPerUser : Number(perUser);
|
|
819
|
+
for (const [flag, value] of [
|
|
820
|
+
['--daily-tokens', daily],
|
|
821
|
+
['--max-users', users],
|
|
822
|
+
['--per-user', per],
|
|
823
|
+
]) {
|
|
824
|
+
if (value !== undefined && (!Number.isInteger(value) || value < 0))
|
|
825
|
+
fail(`${flag} must be a non-negative integer, got '${value}'`);
|
|
826
|
+
}
|
|
827
|
+
if (daily !== undefined)
|
|
828
|
+
next.dailyTokens = daily;
|
|
829
|
+
if (users !== undefined)
|
|
830
|
+
next.maxConcurrentUsers = users;
|
|
831
|
+
if (per !== undefined)
|
|
832
|
+
next.maxConcurrencyPerUser = per;
|
|
833
|
+
}
|
|
834
|
+
groups.set(alias, next);
|
|
835
|
+
}
|
|
836
|
+
try {
|
|
837
|
+
// revalidate the whole file the way the hub will parse it
|
|
838
|
+
parseGroups(serializeGroups(groups));
|
|
839
|
+
}
|
|
840
|
+
catch (err) {
|
|
841
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
842
|
+
}
|
|
843
|
+
writeGroupsFile(file, groups);
|
|
844
|
+
if (json) {
|
|
845
|
+
console.log(JSON.stringify(Object.fromEntries(groups), null, 2));
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
console.log(`✓ ${alias} → ${describe(groups.get(alias))}`);
|
|
849
|
+
console.log('(config.groups.toml written — the hub picks it up within ~2s)');
|
|
850
|
+
}
|
|
851
|
+
}
|
|
509
852
|
async function main() {
|
|
510
853
|
const [cmd, ...rest] = process.argv.slice(2);
|
|
511
854
|
const { positional, flags } = parseArgs(rest);
|
|
@@ -528,9 +871,7 @@ async function main() {
|
|
|
528
871
|
checkFlags('init', flags, []);
|
|
529
872
|
noPositionals('init', positional);
|
|
530
873
|
ensureDataDir(paths);
|
|
531
|
-
loadOrCreatePepper(paths);
|
|
532
874
|
const tokenExisted = existsSync(paths.adminTokenFile);
|
|
533
|
-
const adminToken = loadOrCreateAdminToken(paths);
|
|
534
875
|
if (!existsSync(paths.configFile)) {
|
|
535
876
|
writeFileSync(paths.configFile, HUB_CONFIG_TEMPLATE, { mode: 0o600 });
|
|
536
877
|
console.log(`config file: ${paths.configFile} (every key commented out — edit to override)`);
|
|
@@ -539,7 +880,12 @@ async function main() {
|
|
|
539
880
|
writeFileSync(paths.backupsFile, serializeBackups(new Map()), { mode: 0o600 });
|
|
540
881
|
console.log(`backups file: ${paths.backupsFile} (empty — per-alias failover queues, see: aweshare hub backups -h)`);
|
|
541
882
|
}
|
|
883
|
+
if (!existsSync(paths.groupsFile)) {
|
|
884
|
+
writeFileSync(paths.groupsFile, serializeGroups(new Map()), { mode: 0o600 });
|
|
885
|
+
console.log(`groups file: ${paths.groupsFile} (empty — route groups, see: aweshare hub groups -h)`);
|
|
886
|
+
}
|
|
542
887
|
const hub = createHubServer({ paths }); // creates the db schema
|
|
888
|
+
const adminToken = hub.auth.adminToken;
|
|
543
889
|
await hub.close();
|
|
544
890
|
console.log(`data dir: ${paths.dataDir}`);
|
|
545
891
|
console.log(`admin token: ${adminToken}`);
|
|
@@ -577,9 +923,8 @@ async function main() {
|
|
|
577
923
|
if (positional[1])
|
|
578
924
|
fail(`unexpected argument '${positional[1]}' (see: aweshare hub produce -h)`);
|
|
579
925
|
const files = initHubProduceFiles(paths);
|
|
580
|
-
loadOrCreatePepper(paths);
|
|
581
|
-
const adminToken = loadOrCreateAdminToken(paths);
|
|
582
926
|
const hub = createHubServer({ paths }); // creates the db schema
|
|
927
|
+
const adminToken = hub.auth.adminToken;
|
|
583
928
|
await hub.close();
|
|
584
929
|
console.log(`produce config file: ${paths.produceConfigFile} (${files.produceConfigCreated ? 'created' : 'already exists; kept'})`);
|
|
585
930
|
console.log(`secrets file: ${paths.secretsFile} (${files.secretsCreated ? 'created' : 'already exists; kept'})`);
|
|
@@ -796,6 +1141,53 @@ async function main() {
|
|
|
796
1141
|
});
|
|
797
1142
|
return;
|
|
798
1143
|
}
|
|
1144
|
+
case 'probe': {
|
|
1145
|
+
// Force a recovery probe for hub-local backends, skipping the HealthGate's
|
|
1146
|
+
// backoff. This is the escape hatch for the streak that outlived its
|
|
1147
|
+
// upstream: the cap grows to 24h, and neither a config hot-reload nor a
|
|
1148
|
+
// SIGHUP touches the in-memory gate, so without this the only recovery
|
|
1149
|
+
// was a restart.
|
|
1150
|
+
checkFlags('probe', flags, ['json']);
|
|
1151
|
+
const api = adminApi();
|
|
1152
|
+
const raws = parseTargets(positional);
|
|
1153
|
+
const params = new URLSearchParams();
|
|
1154
|
+
if (raws.length > 0)
|
|
1155
|
+
params.set('backend', raws.join(','));
|
|
1156
|
+
const qs = params.toString();
|
|
1157
|
+
const out = (await api('POST', `/admin/v1/probe${qs ? `?${qs}` : ''}`));
|
|
1158
|
+
if (flags.get('json') === 'true') {
|
|
1159
|
+
console.log(JSON.stringify(out, null, 2));
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
const ids = out.probed === 'all-degraded' ? Object.keys(out.backends) : out.probed;
|
|
1163
|
+
console.log(`probed: ${out.probed === 'all-degraded' ? 'every degraded backend' : out.probed.join(', ')}`);
|
|
1164
|
+
if (out.changed.length > 0) {
|
|
1165
|
+
console.log(` changed: ${out.changed.join(', ')}`);
|
|
1166
|
+
}
|
|
1167
|
+
else {
|
|
1168
|
+
console.log(' changed: nothing');
|
|
1169
|
+
}
|
|
1170
|
+
for (const id of ids.sort()) {
|
|
1171
|
+
const s = out.backends[id];
|
|
1172
|
+
if (s === undefined) {
|
|
1173
|
+
console.log(` ${id.padEnd(24)} unknown — not in the catalog`);
|
|
1174
|
+
continue;
|
|
1175
|
+
}
|
|
1176
|
+
const line = ` ${id.padEnd(24)} ${s.state}${s.reason ? ` (${s.reason})` : ''}`;
|
|
1177
|
+
const since = s.since === undefined ? null : Date.now() - Date.parse(s.since);
|
|
1178
|
+
console.log(s.state === 'degraded' && since !== null && Number.isFinite(since)
|
|
1179
|
+
? `${line} — degraded for ${fmtDuration(since)}`
|
|
1180
|
+
: line);
|
|
1181
|
+
}
|
|
1182
|
+
const stillDown = ids.filter((id) => out.backends[id]?.state === 'degraded');
|
|
1183
|
+
if (stillDown.length > 0) {
|
|
1184
|
+
console.log(` still degraded: ${stillDown.join(', ')} — upstream is still failing, or the alias's backend is not hub-local`);
|
|
1185
|
+
}
|
|
1186
|
+
else {
|
|
1187
|
+
console.log(' all probed backends are healthy');
|
|
1188
|
+
}
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
799
1191
|
case 'list': {
|
|
800
1192
|
// the read verbs, one table per noun; bare `list` defaults to the ledger
|
|
801
1193
|
const sub = (positional[0] ?? 'invites');
|
|
@@ -990,107 +1382,32 @@ async function main() {
|
|
|
990
1382
|
}
|
|
991
1383
|
case 'backups': {
|
|
992
1384
|
checkFlags('backups', flags, ['add', 'first', 'remove', 'clear', 'json']);
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
if (queues.size === 0) {
|
|
1020
|
-
console.log('no backup queues configured — create one:');
|
|
1021
|
-
console.log(' aweshare hub backups SOME/ALIAS --add OTHER/BACKUP');
|
|
1022
|
-
return;
|
|
1023
|
-
}
|
|
1024
|
-
if (json)
|
|
1025
|
-
console.log(JSON.stringify(Object.fromEntries(queues), null, 2));
|
|
1026
|
-
else
|
|
1027
|
-
for (const [key, queue] of queues)
|
|
1028
|
-
print(key, queue);
|
|
1029
|
-
return;
|
|
1030
|
-
}
|
|
1031
|
-
const queue = queues.get(alias);
|
|
1032
|
-
if (json)
|
|
1033
|
-
console.log(JSON.stringify({ [alias]: queue ?? [] }, null, 2));
|
|
1034
|
-
else
|
|
1035
|
-
print(alias, queue);
|
|
1036
|
-
return;
|
|
1037
|
-
}
|
|
1038
|
-
// unreachable in plain control flow (mutating implies ALIAS, checked
|
|
1039
|
-
// above) but needed so the compiler sees the string
|
|
1040
|
-
if (alias === undefined)
|
|
1041
|
-
fail('which queue to modify: pass ALIAS (see: aweshare hub backups -h)');
|
|
1042
|
-
if (!isValidAlias(alias))
|
|
1043
|
-
fail(`'${alias}' is not an alias (namespace/name, lowercase)`);
|
|
1044
|
-
// every mutation lands in memory first and the file is written once,
|
|
1045
|
-
// so a rejected flag leaves the file untouched
|
|
1046
|
-
if (clear) {
|
|
1047
|
-
if (!queues.delete(alias))
|
|
1048
|
-
fail(`${alias}: no queue configured (see: aweshare hub backups)`);
|
|
1049
|
-
}
|
|
1050
|
-
if (remove !== undefined) {
|
|
1051
|
-
for (const target of splitAliases(remove)) {
|
|
1052
|
-
const queue = queues.get(alias);
|
|
1053
|
-
if (!queue?.includes(target))
|
|
1054
|
-
fail(`${alias}: '${target}' is not in its queue`);
|
|
1055
|
-
const rest = queue.filter((entry) => entry !== target);
|
|
1056
|
-
if (rest.length > 0)
|
|
1057
|
-
queues.set(alias, rest);
|
|
1058
|
-
else
|
|
1059
|
-
queues.delete(alias);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
if (add !== undefined) {
|
|
1063
|
-
const additions = splitAliases(add);
|
|
1064
|
-
if (new Set(additions).size !== additions.length)
|
|
1065
|
-
fail(`--add repeats an alias: '${add}'`);
|
|
1066
|
-
for (const entry of additions) {
|
|
1067
|
-
if (!isValidAlias(entry))
|
|
1068
|
-
fail(`'${entry}' is not an alias (namespace/name, lowercase)`);
|
|
1069
|
-
if (entry === alias)
|
|
1070
|
-
fail(`${alias}: it cannot be its own backup`);
|
|
1071
|
-
if (queues.get(alias)?.includes(entry))
|
|
1072
|
-
fail(`${alias}: '${entry}' is already in its queue`);
|
|
1073
|
-
}
|
|
1074
|
-
const queue = queues.get(alias) ?? [];
|
|
1075
|
-
queues.set(alias, flags.get('first') === 'true' ? [...additions, ...queue] : [...queue, ...additions]);
|
|
1076
|
-
}
|
|
1077
|
-
try {
|
|
1078
|
-
// revalidate the whole file the way the hub will parse it
|
|
1079
|
-
parseBackups(serializeBackups(queues));
|
|
1080
|
-
}
|
|
1081
|
-
catch (err) {
|
|
1082
|
-
fail(err instanceof Error ? err.message : String(err));
|
|
1083
|
-
}
|
|
1084
|
-
writeBackupsFile(paths.backupsFile, queues);
|
|
1085
|
-
const queue = queues.get(alias);
|
|
1086
|
-
if (json) {
|
|
1087
|
-
console.log(JSON.stringify(Object.fromEntries(queues), null, 2));
|
|
1088
|
-
}
|
|
1089
|
-
else {
|
|
1090
|
-
const shown = queue && queue.length > 0 ? queue.join(', ') : '(none — queue removed)';
|
|
1091
|
-
console.log(`✓ ${alias} → ${shown}`);
|
|
1092
|
-
console.log('(config.backups.toml written — the hub picks it up within ~2s)');
|
|
1093
|
-
}
|
|
1385
|
+
runQueueCommand({
|
|
1386
|
+
file: paths.backupsFile,
|
|
1387
|
+
api: BACKUPS_FILE,
|
|
1388
|
+
words: {
|
|
1389
|
+
command: 'backups',
|
|
1390
|
+
noun: 'backup queue',
|
|
1391
|
+
entry: 'backup',
|
|
1392
|
+
fileName: 'config.backups.toml',
|
|
1393
|
+
addHint: '--add OTHER/BACKUP',
|
|
1394
|
+
},
|
|
1395
|
+
}, positional, flags);
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
case 'groups': {
|
|
1399
|
+
checkFlags('groups', flags, [
|
|
1400
|
+
'add',
|
|
1401
|
+
'first',
|
|
1402
|
+
'remove',
|
|
1403
|
+
'clear',
|
|
1404
|
+
'clear-caps',
|
|
1405
|
+
'daily-tokens',
|
|
1406
|
+
'max-users',
|
|
1407
|
+
'per-user',
|
|
1408
|
+
'json',
|
|
1409
|
+
]);
|
|
1410
|
+
runGroupCommand(positional, flags);
|
|
1094
1411
|
return;
|
|
1095
1412
|
}
|
|
1096
1413
|
case 'status': {
|