usegrunt 0.6.6 → 0.6.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.md +31 -3
- package/dist/local/api.js +1 -0
- package/dist/local/dashboard.js +474 -0
- package/dist/local/fallback-setup.js +52 -13
- package/dist/local/main.js +69 -11
- package/dist/local/managed-router.js +4 -2
- package/dist/local/opencode.js +38 -18
- package/dist/local/routing.js +17 -5
- package/dist/local/setup-dashboard.js +27 -0
- package/dist/local/setup-session.js +248 -0
- package/dist/local/setup.js +14 -1
- package/dist/local/source-status.js +129 -0
- package/dist/local/source.js +49 -5
- package/dist/local/terminal.js +31 -1
- package/dist/local/verification-queue.js +31 -1
- package/dist/local/wizard.js +353 -98
- package/package.json +1 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
const codes = new Set(['not_installed', 'logged_out', 'quota', 'timeout', 'upstream', 'refused', 'cancelled']);
|
|
2
|
+
const failureCode = (error) => {
|
|
3
|
+
const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined;
|
|
4
|
+
return typeof code === 'string' && codes.has(code) ? code : 'upstream';
|
|
5
|
+
};
|
|
6
|
+
const detail = {
|
|
7
|
+
unknown: 'No runtime activity observed yet', available: 'Configured; awaiting a verified reply', ready: 'A successful reply was observed',
|
|
8
|
+
answering: 'Request in progress', connecting: 'Setup or a background check is in progress', verifying: 'Setup is verifying this source', approval: 'Needs foreground verification',
|
|
9
|
+
limited: 'Free quota or rate limit reached', error: 'Source failed; another source may answer', disabled: 'Routing is disabled',
|
|
10
|
+
unavailable: 'Not currently available; setup may be needed'
|
|
11
|
+
};
|
|
12
|
+
// This observer records only events the runtime already performs. Reading a
|
|
13
|
+
// snapshot never calls a provider, reads credentials, or stores request content.
|
|
14
|
+
export class SourceStatus {
|
|
15
|
+
now;
|
|
16
|
+
observations = new Map();
|
|
17
|
+
constructor(ids, now = Date.now) {
|
|
18
|
+
this.now = now;
|
|
19
|
+
for (const id of ids)
|
|
20
|
+
this.observations.set(id, { state: 'unknown', active: 0, revision: 0 });
|
|
21
|
+
}
|
|
22
|
+
entry(id) {
|
|
23
|
+
let entry = this.observations.get(id);
|
|
24
|
+
if (!entry) {
|
|
25
|
+
entry = { state: 'unknown', active: 0, revision: 0 };
|
|
26
|
+
this.observations.set(id, entry);
|
|
27
|
+
}
|
|
28
|
+
return entry;
|
|
29
|
+
}
|
|
30
|
+
healthy(id) {
|
|
31
|
+
const entry = this.entry(id);
|
|
32
|
+
entry.revision++;
|
|
33
|
+
delete entry.failure;
|
|
34
|
+
delete entry.retryAt;
|
|
35
|
+
if (entry.state !== 'ready')
|
|
36
|
+
entry.state = 'available';
|
|
37
|
+
}
|
|
38
|
+
async available(source, healthCheck = false) {
|
|
39
|
+
const entry = this.entry(source.id), revision = entry.revision;
|
|
40
|
+
try {
|
|
41
|
+
const result = await (healthCheck ? source.healthCheck() : source.available());
|
|
42
|
+
// A reply or explicit recovery observed after this probe started is
|
|
43
|
+
// newer evidence. The caller still receives the actual probe result.
|
|
44
|
+
if (entry.revision === revision) {
|
|
45
|
+
if (!result)
|
|
46
|
+
entry.state = 'unavailable';
|
|
47
|
+
else if (healthCheck)
|
|
48
|
+
this.healthy(source.id);
|
|
49
|
+
else if (entry.state === 'unknown' || entry.state === 'unavailable')
|
|
50
|
+
entry.state = 'available';
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (entry.revision === revision)
|
|
56
|
+
entry.state = 'unavailable';
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async complete(source, request, signal) {
|
|
61
|
+
const entry = this.entry(source.id), started = this.now();
|
|
62
|
+
entry.active++;
|
|
63
|
+
try {
|
|
64
|
+
const result = await source.complete(request, signal);
|
|
65
|
+
// A provider may finish after the caller cancelled. That is not evidence
|
|
66
|
+
// of an admitted successful response, and must not resurrect readiness.
|
|
67
|
+
if (!signal.aborted) {
|
|
68
|
+
entry.revision++;
|
|
69
|
+
entry.state = 'ready';
|
|
70
|
+
entry.lastSuccessAt = this.now();
|
|
71
|
+
entry.lastReplyMs = Math.max(0, this.now() - started);
|
|
72
|
+
delete entry.failure;
|
|
73
|
+
delete entry.retryAt;
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
const code = failureCode(error);
|
|
79
|
+
if (!signal.aborted && code !== 'cancelled') {
|
|
80
|
+
entry.revision++;
|
|
81
|
+
entry.failure = code;
|
|
82
|
+
entry.state = code === 'quota' ? 'limited' : code === 'logged_out' || code === 'not_installed' ? 'unavailable' : 'error';
|
|
83
|
+
const retry = error && typeof error === 'object' && 'retryAfter' in error ? error.retryAfter : undefined;
|
|
84
|
+
const seconds = code === 'quota' ? (typeof retry === 'number' && Number.isFinite(retry) && retry >= 0 ? retry : undefined) : undefined;
|
|
85
|
+
const until = seconds === undefined ? undefined : this.now() + seconds * 1000;
|
|
86
|
+
if (until !== undefined && Number.isFinite(until))
|
|
87
|
+
entry.retryAt = until;
|
|
88
|
+
else
|
|
89
|
+
delete entry.retryAt;
|
|
90
|
+
}
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
entry.active--;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
snapshot(options) {
|
|
98
|
+
const now = this.now();
|
|
99
|
+
const sources = [...new Set([...options.ids, ...options.disabled])].map((id) => {
|
|
100
|
+
const entry = this.entry(id), model = options.model?.(id);
|
|
101
|
+
const row = { id, state: entry.state, active: entry.active,
|
|
102
|
+
...(model ? { model, modelKind: 'registry' } : {}), ...(entry.failure ? { errorCode: entry.failure } : {}), ...(entry.lastReplyMs !== undefined ? { lastReplyMs: entry.lastReplyMs } : {}),
|
|
103
|
+
...(entry.lastSuccessAt !== undefined ? { lastSuccessAt: entry.lastSuccessAt } : {}) };
|
|
104
|
+
const cooldown = options.cooldown?.(id);
|
|
105
|
+
if (options.disabled.has(id) || options.listed && !options.listed(id))
|
|
106
|
+
row.state = 'disabled';
|
|
107
|
+
else if (options.pending.has(id))
|
|
108
|
+
row.state = options.verifying.has(id) ? 'verifying' : options.approvals.has(id) ? 'approval' : 'connecting';
|
|
109
|
+
else if (entry.active)
|
|
110
|
+
row.state = 'answering';
|
|
111
|
+
else if (cooldown !== undefined && cooldown.until > now) {
|
|
112
|
+
row.state = cooldown.code === 'quota' ? 'limited' : cooldown.until === Infinity ? 'unavailable' : 'error';
|
|
113
|
+
if (Number.isFinite(cooldown.until))
|
|
114
|
+
row.retryAt = cooldown.until;
|
|
115
|
+
}
|
|
116
|
+
else if (options.preparing.has(id) && entry.state !== 'ready')
|
|
117
|
+
row.state = 'connecting';
|
|
118
|
+
else if (cooldown !== undefined && (entry.state === 'limited' || entry.state === 'error'))
|
|
119
|
+
row.state = 'available';
|
|
120
|
+
else if (entry.retryAt !== undefined && entry.retryAt > now)
|
|
121
|
+
row.retryAt = entry.retryAt;
|
|
122
|
+
else if (entry.retryAt !== undefined && entry.state === 'limited')
|
|
123
|
+
row.state = 'available';
|
|
124
|
+
row.detail = `${detail[row.state]}${model ? '; model label comes from the registry, not response attestation' : ''}`;
|
|
125
|
+
return row;
|
|
126
|
+
});
|
|
127
|
+
return { version: 1, updatedAt: now, sources, activeRequests: sources.reduce((total, source) => total + source.active, 0) };
|
|
128
|
+
}
|
|
129
|
+
}
|
package/dist/local/source.js
CHANGED
|
@@ -13,24 +13,47 @@ export class SourceError extends Error {
|
|
|
13
13
|
export const answeredBy = new WeakMap();
|
|
14
14
|
export class Sources {
|
|
15
15
|
sources;
|
|
16
|
+
routing;
|
|
16
17
|
disabledModels;
|
|
17
18
|
router;
|
|
19
|
+
observation;
|
|
18
20
|
// Adapters whose background setup has not finished yet.
|
|
19
21
|
preparing = new Set();
|
|
22
|
+
// Foreground signups must not probe or dispatch incomplete sources. Routine
|
|
23
|
+
// background checks remain metadata and do not interrupt already-bound work.
|
|
24
|
+
pending = new Set();
|
|
25
|
+
// An exact-ID setup probe may run without admitting that source to the pool.
|
|
26
|
+
verifying = new Set();
|
|
27
|
+
// Foreground approval metadata contains adapter IDs only, never handoff URLs.
|
|
28
|
+
approvals = new Set();
|
|
20
29
|
constructor(sources, routing, disabledModels = new Set()) {
|
|
21
30
|
this.sources = sources;
|
|
31
|
+
this.routing = routing;
|
|
22
32
|
this.disabledModels = disabledModels;
|
|
23
33
|
if ((!sources.length && !disabledModels.size) || new Set(sources.map(s => s.id)).size !== sources.length)
|
|
24
34
|
throw new Error('Configure distinct grunt sources');
|
|
35
|
+
this.observation = new SourceStatus(sources.map(source => source.id), routing?.now);
|
|
25
36
|
if (routing)
|
|
26
|
-
this.router = new StickyRouter(sources, routing);
|
|
37
|
+
this.router = new StickyRouter(sources, { ...routing, pending: this.pending, observation: this.observation });
|
|
38
|
+
}
|
|
39
|
+
healthy(id) { this.router?.healthy(id); this.observation.healthy(id); }
|
|
40
|
+
status() {
|
|
41
|
+
return this.observation.snapshot({ ids: this.sources.map(source => source.id), disabled: this.disabledModels,
|
|
42
|
+
pending: this.pending, verifying: this.verifying, approvals: this.approvals, preparing: this.preparing,
|
|
43
|
+
...(this.router ? { listed: (id) => this.router.listed(id), cooldown: (id) => this.router.cooldown(id) } : {}),
|
|
44
|
+
model: id => {
|
|
45
|
+
const adapter = this.routing?.registry().adapters.find(adapter => adapter.id === id);
|
|
46
|
+
// A rotation is a configured set, not an assertion about which model
|
|
47
|
+
// generated the latest answer. Never inspect private adapter configs.
|
|
48
|
+
const model = adapter?.models && adapter.models.length > 1 ? 'Free model rotation' : adapter?.models?.[0] ?? adapter?.model;
|
|
49
|
+
return model && /^[a-zA-Z0-9][a-zA-Z0-9._:/+ -]{0,159}$/.test(model) ? model : undefined;
|
|
50
|
+
} });
|
|
27
51
|
}
|
|
28
|
-
healthy(id) { this.router?.healthy(id); }
|
|
29
52
|
async models() {
|
|
30
53
|
const available = await Promise.all(this.sources.map(async (s) => {
|
|
31
|
-
if (this.router && !this.router.listed(s.id) || !await
|
|
54
|
+
if (this.pending.has(s.id) || this.router && !this.router.listed(s.id) || !await this.observation.available(s))
|
|
32
55
|
return null;
|
|
33
|
-
if (this.router?.needsHealthCheck(s.id) && await
|
|
56
|
+
if (this.router?.needsHealthCheck(s.id) && s.healthCheck && await this.observation.available(s, true).catch(() => false))
|
|
34
57
|
this.healthy(s.id);
|
|
35
58
|
return s.id;
|
|
36
59
|
}));
|
|
@@ -40,6 +63,24 @@ export class Sources {
|
|
|
40
63
|
signal.throwIfAborted();
|
|
41
64
|
if (this.disabledModels.has(request.model))
|
|
42
65
|
throw new SourceError('refused', `${request.model} routing is disabled`);
|
|
66
|
+
if (this.pending.has(request.model)) {
|
|
67
|
+
if (!this.verifying.has(request.model))
|
|
68
|
+
throw new SourceError('not_installed', `${request.model} setup is still in progress`);
|
|
69
|
+
const source = this.sources.find(source => source.id === request.model);
|
|
70
|
+
if (!source)
|
|
71
|
+
throw new SourceError('not_installed', `No source for model ${request.model}`);
|
|
72
|
+
if (this.router && !this.router.listed(source.id))
|
|
73
|
+
throw new SourceError('not_installed', `${request.model} is not in the current provider registry`);
|
|
74
|
+
if (this.router && !this.router.supportsInput(source.id, request.imageInput))
|
|
75
|
+
throw new SourceError('refused', `${request.model} does not support this input modality`);
|
|
76
|
+
// A setup probe is one exact-source call, never sticky failover or a
|
|
77
|
+
// retry of a provider refusal. The source retains every normal guard.
|
|
78
|
+
const completion = await this.observation.complete(source, request, signal);
|
|
79
|
+
signal.throwIfAborted();
|
|
80
|
+
this.router?.verified(source.id);
|
|
81
|
+
answeredBy.set(completion, source.id);
|
|
82
|
+
return completion;
|
|
83
|
+
}
|
|
43
84
|
if (this.router)
|
|
44
85
|
return this.router.complete(request, signal);
|
|
45
86
|
const known = this.sources.some(s => s.id === request.model);
|
|
@@ -49,9 +90,11 @@ export class Sources {
|
|
|
49
90
|
let last = new SourceError('not_installed', `No source for model ${request.model}`);
|
|
50
91
|
for (const source of candidates) {
|
|
51
92
|
signal.throwIfAborted();
|
|
93
|
+
if (this.pending.has(source.id))
|
|
94
|
+
continue;
|
|
52
95
|
let completion;
|
|
53
96
|
try {
|
|
54
|
-
completion = await
|
|
97
|
+
completion = await this.observation.complete(source, request, signal);
|
|
55
98
|
}
|
|
56
99
|
catch (error) {
|
|
57
100
|
if (signal.aborted || (error instanceof SourceError && error.code === 'cancelled')) {
|
|
@@ -114,3 +157,4 @@ export class Gateway {
|
|
|
114
157
|
}
|
|
115
158
|
}
|
|
116
159
|
import { StickyRouter } from './routing.js';
|
|
160
|
+
import { SourceStatus } from './source-status.js';
|
package/dist/local/terminal.js
CHANGED
|
@@ -1,8 +1,38 @@
|
|
|
1
1
|
// Interactive child processes (for example sudo) and browser confirmations
|
|
2
2
|
// must never read stdin at the same time. Noninteractive jobs stay concurrent.
|
|
3
3
|
let tail = Promise.resolve();
|
|
4
|
+
let active = false;
|
|
5
|
+
const observers = new Set();
|
|
6
|
+
export const terminalBusy = () => active;
|
|
7
|
+
// Status displays may observe terminal ownership, but never receive the prompt,
|
|
8
|
+
// browser capability link, or any data entered by the user.
|
|
9
|
+
export function observeTerminal(observer) {
|
|
10
|
+
observers.add(observer);
|
|
11
|
+
try {
|
|
12
|
+
observer(active);
|
|
13
|
+
}
|
|
14
|
+
catch { /* A display cannot block confirmation. */ }
|
|
15
|
+
return () => { observers.delete(observer); };
|
|
16
|
+
}
|
|
17
|
+
function changed(busy) {
|
|
18
|
+
active = busy;
|
|
19
|
+
for (const observer of observers) {
|
|
20
|
+
try {
|
|
21
|
+
observer(busy);
|
|
22
|
+
}
|
|
23
|
+
catch { /* Observability is independent of setup. */ }
|
|
24
|
+
}
|
|
25
|
+
}
|
|
4
26
|
export function withTerminal(task) {
|
|
5
|
-
const result = tail.then(
|
|
27
|
+
const result = tail.then(async () => {
|
|
28
|
+
changed(true);
|
|
29
|
+
try {
|
|
30
|
+
return await task();
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
changed(false);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
6
36
|
tail = result.catch(() => { });
|
|
7
37
|
return result;
|
|
8
38
|
}
|
|
@@ -4,11 +4,35 @@ export class VerificationQueue {
|
|
|
4
4
|
confirm;
|
|
5
5
|
pending = [];
|
|
6
6
|
running = false;
|
|
7
|
+
active = [];
|
|
8
|
+
observers = new Set();
|
|
7
9
|
constructor(confirm) {
|
|
8
10
|
this.confirm = confirm;
|
|
9
11
|
}
|
|
12
|
+
services() {
|
|
13
|
+
// This safe projection deliberately excludes URLs and requested goals.
|
|
14
|
+
return [...new Set([...this.active, ...this.pending.map(entry => entry.item)]
|
|
15
|
+
.map(item => item.service).filter(service => /^[a-z0-9][a-z0-9-]{0,63}$/.test(service)))];
|
|
16
|
+
}
|
|
17
|
+
subscribe(observer) {
|
|
18
|
+
this.observers.add(observer);
|
|
19
|
+
try {
|
|
20
|
+
observer(this.services());
|
|
21
|
+
}
|
|
22
|
+
catch { /* Status cannot stop approval. */ }
|
|
23
|
+
return () => { this.observers.delete(observer); };
|
|
24
|
+
}
|
|
25
|
+
changed() {
|
|
26
|
+
for (const observer of this.observers) {
|
|
27
|
+
try {
|
|
28
|
+
observer(this.services());
|
|
29
|
+
}
|
|
30
|
+
catch { /* Isolate display failures. */ }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
10
33
|
wait(item) {
|
|
11
34
|
const result = new Promise((resolve, reject) => this.pending.push({ item, resolve, reject }));
|
|
35
|
+
this.changed();
|
|
12
36
|
if (!this.running) {
|
|
13
37
|
this.running = true;
|
|
14
38
|
setImmediate(() => { void this.drain(); });
|
|
@@ -18,13 +42,19 @@ export class VerificationQueue {
|
|
|
18
42
|
async drain() {
|
|
19
43
|
while (this.pending.length) {
|
|
20
44
|
const batch = this.pending.splice(0);
|
|
45
|
+
this.active = batch.map(entry => entry.item);
|
|
46
|
+
this.changed();
|
|
21
47
|
try {
|
|
22
|
-
await this.confirm(
|
|
48
|
+
await this.confirm(this.active);
|
|
23
49
|
batch.forEach(entry => entry.resolve());
|
|
24
50
|
}
|
|
25
51
|
catch (error) {
|
|
26
52
|
[...batch, ...this.pending.splice(0)].forEach(entry => entry.reject(error));
|
|
27
53
|
}
|
|
54
|
+
finally {
|
|
55
|
+
this.active = [];
|
|
56
|
+
this.changed();
|
|
57
|
+
}
|
|
28
58
|
}
|
|
29
59
|
this.running = false;
|
|
30
60
|
}
|