@qvac/core 0.10.3 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -1
- package/dist-lib/lib/assistant/history.d.ts +2 -1
- package/dist-lib/lib/assistant/history.js +32 -5
- package/dist-lib/lib/assistant/message.d.ts +20 -0
- package/dist-lib/lib/assistant/message.js +36 -0
- package/dist-lib/lib/assistant/pdf-budget.d.ts +3 -0
- package/dist-lib/lib/assistant/pdf-budget.js +13 -0
- package/dist-lib/lib/assistant/video-budget.d.ts +13 -0
- package/dist-lib/lib/assistant/video-budget.js +29 -0
- package/dist-lib/lib/assistant-tree.d.ts +1 -0
- package/dist-lib/lib/assistant-tree.js +9 -2
- package/dist-lib/lib/assistant.d.ts +26 -3
- package/dist-lib/lib/assistant.js +471 -47
- package/dist-lib/lib/core/chat-fork.js +1 -0
- package/dist-lib/lib/core/chat-search.js +1 -1
- package/dist-lib/lib/core/chunk.d.ts +2 -2
- package/dist-lib/lib/core/chunk.js +5 -1
- package/dist-lib/lib/core/departures.js +6 -0
- package/dist-lib/lib/core/document-attachment.d.ts +7 -0
- package/dist-lib/lib/core/document-attachment.js +9 -0
- package/dist-lib/lib/core/files/index.js +4 -4
- package/dist-lib/lib/core/files/records/chunks.js +21 -0
- package/dist-lib/lib/core/files/records/purge.js +4 -3
- package/dist-lib/lib/core/files/records/source.js +3 -5
- package/dist-lib/lib/core/files/upload.js +3 -2
- package/dist-lib/lib/core/identity.d.ts +2 -0
- package/dist-lib/lib/core/identity.js +19 -0
- package/dist-lib/lib/core/pdf/rasterize.d.ts +0 -1
- package/dist-lib/lib/core/pdf/rasterize.js +0 -1
- package/dist-lib/lib/core/video/attachment.d.ts +1 -0
- package/dist-lib/lib/core/video/attachment.js +7 -0
- package/dist-lib/lib/core/video/digest.d.ts +16 -0
- package/dist-lib/lib/core/video/digest.js +57 -0
- package/dist-lib/lib/core/video/frames.d.ts +32 -0
- package/dist-lib/lib/core/video/frames.js +224 -0
- package/dist-lib/lib/core.d.ts +1 -0
- package/dist-lib/lib/core.js +44 -7
- package/dist-lib/lib/credential-pusher.d.ts +6 -0
- package/dist-lib/lib/credential-pusher.js +76 -59
- package/dist-lib/lib/errors.d.ts +8 -0
- package/dist-lib/lib/errors.js +8 -0
- package/dist-lib/lib/oauth/flow-core.js +2 -0
- package/dist-lib/lib/oauth/loopback.d.ts +1 -0
- package/dist-lib/lib/oauth/loopback.js +31 -1
- package/dist-lib/lib/oauth/register.js +33 -18
- package/dist-lib/lib/oauth/request.d.ts +10 -0
- package/dist-lib/lib/oauth/request.js +22 -0
- package/dist-lib/lib/oauth/token-exchange-core.js +29 -32
- package/dist-lib/lib/oauth/types.d.ts +3 -0
- package/dist-lib/lib/oauth.d.ts +2 -1
- package/dist-lib/lib/oauth.js +1 -0
- package/dist-lib/lib/run-notices.d.ts +1 -0
- package/dist-lib/lib/run-notices.js +2 -1
- package/dist-lib/lib/run-replay.d.ts +8 -2
- package/dist-lib/lib/run-replay.js +29 -11
- package/dist-lib/lib/task.js +1 -1
- package/dist-lib/schema/engine.js +4 -2
- package/dist-lib/schema/indexes.js +2 -2
- package/dist-lib/schema/qvac.js +4 -2
- package/dist-lib/spec/hrpc/messages.js +16 -6
- package/dist-lib/spec/hyperschema/index.js +16 -6
- package/dist-lib/spec/hyperschema/schema.json +10 -0
- package/dist-lib/spec/hyperschema/types.d.ts +2 -0
- package/dist-lib/spec/mesh/hyperdb/messages.js +26 -11
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +18 -8
- package/dist-lib/spec/mesh/hyperschema/index.js +18 -8
- package/dist-lib/spec/mesh/hyperschema/schema.json +11 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +3 -1
- package/package.json +7 -4
- package/tui/app.mjs +34 -1
- package/tui/lib/render.mjs +5 -0
- package/tui/main.mjs +8 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
const AV_TIME_BASE = 1_000_000;
|
|
4
|
+
const YIELD_EVERY_PACKETS = 64;
|
|
5
|
+
const NO_PTS = -1;
|
|
6
|
+
const UPRIGHT = { rotate: 0, flipH: false, flipV: false };
|
|
7
|
+
const CORRECTIONS = new Map([
|
|
8
|
+
['0,-1,1,0', { rotate: 270, flipH: false, flipV: false }],
|
|
9
|
+
['-1,0,0,-1', { rotate: 180, flipH: false, flipV: false }],
|
|
10
|
+
['0,1,-1,0', { rotate: 90, flipH: false, flipV: false }],
|
|
11
|
+
['-1,0,0,1', { rotate: 0, flipH: true, flipV: false }],
|
|
12
|
+
['1,0,0,-1', { rotate: 0, flipH: false, flipV: true }],
|
|
13
|
+
['0,1,1,0', { rotate: 270, flipH: true, flipV: false }],
|
|
14
|
+
['0,-1,-1,0', { rotate: 270, flipH: false, flipV: true }]
|
|
15
|
+
]);
|
|
16
|
+
let loaded = null;
|
|
17
|
+
function loadFfmpeg() {
|
|
18
|
+
loaded ??= import('bare-ffmpeg').then((mod) => mod.default);
|
|
19
|
+
return loaded;
|
|
20
|
+
}
|
|
21
|
+
function fileIO(ff, fd) {
|
|
22
|
+
const size = fs.fstatSync(fd).size;
|
|
23
|
+
const { SET, END, SIZE } = ff.constants.seek;
|
|
24
|
+
let offset = 0;
|
|
25
|
+
return new ff.IOContext(4096, {
|
|
26
|
+
onread: (buffer, requested) => {
|
|
27
|
+
const read = fs.readSync(fd, buffer, 0, requested, offset);
|
|
28
|
+
offset += read;
|
|
29
|
+
return read;
|
|
30
|
+
},
|
|
31
|
+
onseek: (to, whence) => {
|
|
32
|
+
if (whence === SIZE) {
|
|
33
|
+
return size;
|
|
34
|
+
}
|
|
35
|
+
if (whence === SET) {
|
|
36
|
+
offset = to;
|
|
37
|
+
}
|
|
38
|
+
else if (whence === END) {
|
|
39
|
+
offset = size + to;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
offset += to;
|
|
43
|
+
}
|
|
44
|
+
return offset;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function openInput(ff, file) {
|
|
49
|
+
const fd = fs.openSync(file, 'r');
|
|
50
|
+
try {
|
|
51
|
+
const input = new ff.InputFormatContext(fileIO(ff, fd));
|
|
52
|
+
return {
|
|
53
|
+
input,
|
|
54
|
+
close: () => {
|
|
55
|
+
input.destroy();
|
|
56
|
+
fs.closeSync(fd);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
fs.closeSync(fd);
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export async function probeVideo(file) {
|
|
66
|
+
const ff = await loadFfmpeg();
|
|
67
|
+
const { input, close } = openInput(ff, file);
|
|
68
|
+
try {
|
|
69
|
+
const hasAudio = Boolean(input.getBestStream(ff.constants.mediaTypes.AUDIO));
|
|
70
|
+
const video = input.getBestStream(ff.constants.mediaTypes.VIDEO);
|
|
71
|
+
const formatDuration = input.duration > 0 ? input.duration / AV_TIME_BASE : 0;
|
|
72
|
+
if (!video) {
|
|
73
|
+
return {
|
|
74
|
+
hasVideo: false,
|
|
75
|
+
hasAudio,
|
|
76
|
+
duration: formatDuration,
|
|
77
|
+
width: 0,
|
|
78
|
+
height: 0,
|
|
79
|
+
aspect: 1,
|
|
80
|
+
...UPRIGHT
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const { numerator, denominator } = video.timeBase;
|
|
84
|
+
const streamDuration = video.duration > 0 && denominator ? (video.duration * numerator) / denominator : 0;
|
|
85
|
+
return {
|
|
86
|
+
hasVideo: true,
|
|
87
|
+
hasAudio,
|
|
88
|
+
duration: streamDuration || formatDuration,
|
|
89
|
+
width: video.codecParameters.width,
|
|
90
|
+
height: video.codecParameters.height,
|
|
91
|
+
aspect: pixelAspect(video),
|
|
92
|
+
...orientation(ff, video)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
close();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function orientation(ff, stream) {
|
|
100
|
+
const matrix = stream.sideData.find((entry) => entry.type === ff.constants.packetSideDataType.DISPLAYMATRIX);
|
|
101
|
+
if (!matrix) {
|
|
102
|
+
return UPRIGHT;
|
|
103
|
+
}
|
|
104
|
+
const key = [0, 4, 12, 16]
|
|
105
|
+
.map((offset) => snap(b4a.readInt32LE(matrix.data, offset) / 65536))
|
|
106
|
+
.join(',');
|
|
107
|
+
return CORRECTIONS.get(key) ?? UPRIGHT;
|
|
108
|
+
}
|
|
109
|
+
function snap(value) {
|
|
110
|
+
return Math.abs(value - Math.round(value)) < 1e-3 ? Math.round(value) + 0 : value;
|
|
111
|
+
}
|
|
112
|
+
export function frameTimes(duration, count) {
|
|
113
|
+
const slice = duration / count;
|
|
114
|
+
return Array.from({ length: count }, (_, i) => (i + 0.5) * slice);
|
|
115
|
+
}
|
|
116
|
+
export function frameSize(probe, maxEdge) {
|
|
117
|
+
const size = fit(probe.width * probe.aspect, probe.height, maxEdge);
|
|
118
|
+
return probe.rotate % 180 ? { width: size.height, height: size.width } : size;
|
|
119
|
+
}
|
|
120
|
+
function fit(width, height, maxEdge) {
|
|
121
|
+
const scale = Math.min(1, maxEdge / Math.max(width, height));
|
|
122
|
+
const even = (n) => Math.max(2, Math.round((n * scale) / 2) * 2);
|
|
123
|
+
return { width: even(width), height: even(height) };
|
|
124
|
+
}
|
|
125
|
+
function toRaster(ff, frame, size) {
|
|
126
|
+
const { RGBA } = ff.constants.pixelFormats;
|
|
127
|
+
const scaler = new ff.Scaler(frame.format, frame.width, frame.height, RGBA, size.width, size.height);
|
|
128
|
+
const rgba = new ff.Frame();
|
|
129
|
+
try {
|
|
130
|
+
rgba.width = size.width;
|
|
131
|
+
rgba.height = size.height;
|
|
132
|
+
rgba.format = RGBA;
|
|
133
|
+
rgba.alloc();
|
|
134
|
+
scaler.scale(frame, rgba);
|
|
135
|
+
const image = new ff.Image(RGBA, size.width, size.height);
|
|
136
|
+
image.read(rgba);
|
|
137
|
+
return { width: size.width, height: size.height, data: image.data };
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
rgba.destroy();
|
|
141
|
+
scaler.destroy();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function* received(decoder, frame) {
|
|
145
|
+
while (decoder.receiveFrame(frame)) {
|
|
146
|
+
yield frame;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function* decodedFrames(ff, input, stream) {
|
|
150
|
+
const decoder = stream.decoder();
|
|
151
|
+
const packet = new ff.Packet();
|
|
152
|
+
const frame = new ff.Frame();
|
|
153
|
+
try {
|
|
154
|
+
decoder.open();
|
|
155
|
+
let packets = 0;
|
|
156
|
+
while (input.readFrame(packet)) {
|
|
157
|
+
if (packet.streamIndex === stream.index && decoder.sendPacket(packet)) {
|
|
158
|
+
yield* received(decoder, frame);
|
|
159
|
+
}
|
|
160
|
+
packet.unref();
|
|
161
|
+
if (++packets % YIELD_EVERY_PACKETS === 0) {
|
|
162
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
decoder.sendPacket(new ff.Packet());
|
|
166
|
+
yield* received(decoder, frame);
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
frame.destroy();
|
|
170
|
+
packet.destroy();
|
|
171
|
+
decoder.destroy();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function pixelAspect(stream) {
|
|
175
|
+
const sar = stream.codecParameters.sampleAspectRatio;
|
|
176
|
+
return sar && sar.numerator > 0 && sar.denominator > 0 ? sar.numerator / sar.denominator : 1;
|
|
177
|
+
}
|
|
178
|
+
async function pick(ff, input, opts) {
|
|
179
|
+
const stream = input.getBestStream(ff.constants.mediaTypes.VIDEO);
|
|
180
|
+
if (!stream) {
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
const targets = [...opts.times].sort((x, y) => x - y);
|
|
184
|
+
const tb = stream.timeBase.numerator / stream.timeBase.denominator;
|
|
185
|
+
const aspect = pixelAspect(stream);
|
|
186
|
+
const picked = [];
|
|
187
|
+
let origin = null;
|
|
188
|
+
for await (const frame of decodedFrames(ff, input, stream)) {
|
|
189
|
+
const pts = frame.pts === NO_PTS ? frame.packetDTS : frame.pts;
|
|
190
|
+
origin ??= pts;
|
|
191
|
+
const time = (pts - origin) * tb;
|
|
192
|
+
while (picked.length < targets.length && time >= targets[picked.length]) {
|
|
193
|
+
const size = fit(frame.width * aspect, frame.height, opts.maxEdge);
|
|
194
|
+
picked.push({ time, raster: toRaster(ff, frame, size) });
|
|
195
|
+
}
|
|
196
|
+
if (picked.length === targets.length) {
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return picked;
|
|
201
|
+
}
|
|
202
|
+
export async function sampleFrames(file, opts) {
|
|
203
|
+
const ff = await loadFfmpeg();
|
|
204
|
+
const media = await import('bare-media');
|
|
205
|
+
const { input, close } = openInput(ff, file);
|
|
206
|
+
let picked;
|
|
207
|
+
try {
|
|
208
|
+
picked = await pick(ff, input, opts);
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
close();
|
|
212
|
+
}
|
|
213
|
+
const { rotate, flipH, flipV } = opts.probe;
|
|
214
|
+
const orientate = media.image.orientate;
|
|
215
|
+
const frames = [];
|
|
216
|
+
for (const { time, raster } of picked) {
|
|
217
|
+
const upright = rotate || flipH || flipV
|
|
218
|
+
? await orientate(raster, { transform: { rotate, flipH, flipV } })
|
|
219
|
+
: raster;
|
|
220
|
+
const jpeg = await media.image.encode(upright, { mimetype: 'image/jpeg' });
|
|
221
|
+
frames.push({ time, width: upright.width, height: upright.height, jpeg });
|
|
222
|
+
}
|
|
223
|
+
return frames;
|
|
224
|
+
}
|
package/dist-lib/lib/core.d.ts
CHANGED
|
@@ -431,6 +431,7 @@ export declare class Core extends ReadyResource {
|
|
|
431
431
|
private _assertMeshIsHere;
|
|
432
432
|
getMeshInvite(opts?: MeshInviteOptions): Promise<MeshInvite | null>;
|
|
433
433
|
private _delegatePairing;
|
|
434
|
+
private _shortestAnchorProof;
|
|
434
435
|
previewMeshInvite(invite: string): MeshInvitePreview;
|
|
435
436
|
private _closeSwarm;
|
|
436
437
|
pushSkillSecret(request: SecretRequest): Promise<void>;
|
package/dist-lib/lib/core.js
CHANGED
|
@@ -40,7 +40,7 @@ import { createInferenceCallsReceiver } from './core/inference-calls-receiver.js
|
|
|
40
40
|
import { SkillOverlayHandoff } from './core/skill-overlay-handoff.js';
|
|
41
41
|
import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
|
|
42
42
|
import { createSkillSecretReceiver } from './core/skill-secrets.js';
|
|
43
|
-
import { attestDeviceProof, attestMemberClaim, claimAttestation, delegationFits, openIdentity } from './core/identity.js';
|
|
43
|
+
import { attestDeviceProof, attestMemberClaim, chainTo, claimAttestation, delegationFits, openIdentity, pairingAnchorSeed } from './core/identity.js';
|
|
44
44
|
import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
|
|
45
45
|
import { claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
|
|
46
46
|
import { runMigrations } from './core/migrations/index.js';
|
|
@@ -3411,15 +3411,23 @@ export class Core extends ReadyResource {
|
|
|
3411
3411
|
if (!mesh.writable)
|
|
3412
3412
|
return;
|
|
3413
3413
|
const removed = [];
|
|
3414
|
+
const live = [];
|
|
3414
3415
|
for await (const row of mesh.view.find(DEVICES)) {
|
|
3415
3416
|
const ours = mesh === account || ownedBy(row, mesh.ourMemberId);
|
|
3416
3417
|
if (row.revoked && ours && !b4a.equals(row.id, self)) {
|
|
3417
3418
|
removed.push(row.id);
|
|
3418
3419
|
}
|
|
3420
|
+
else if (!row.revoked && ours) {
|
|
3421
|
+
live.push(row);
|
|
3422
|
+
}
|
|
3419
3423
|
}
|
|
3424
|
+
const adopter = await earliestPaired(account, live);
|
|
3425
|
+
const adopts = adopter !== undefined && b4a.equals(adopter.id, self);
|
|
3420
3426
|
for (const id of removed) {
|
|
3421
3427
|
const device = await account.view.get(DEVICES, { id });
|
|
3422
|
-
|
|
3428
|
+
const by = device?.revokedBy;
|
|
3429
|
+
const left = device?.revoked && device.writerKey && by && b4a.equals(by, device.writerKey);
|
|
3430
|
+
if ((by && b4a.equals(by, account.localWriterKey)) || (left && adopts)) {
|
|
3423
3431
|
await rehomeAgents(mesh, id, self).catch((err) => this._log?.warn(['p2p', 'mesh'], "handing over a removed device's agents failed", err));
|
|
3424
3432
|
}
|
|
3425
3433
|
}
|
|
@@ -3614,17 +3622,36 @@ export class Core extends ReadyResource {
|
|
|
3614
3622
|
async _delegatePairing(account) {
|
|
3615
3623
|
const keyPair = this._requireDeviceKeyPair();
|
|
3616
3624
|
const own = await account.view.get(DEVICES, { id: keyPair.publicKey });
|
|
3617
|
-
if (!own?.memberProof) {
|
|
3625
|
+
if (!own?.memberId || !own.memberProof) {
|
|
3618
3626
|
return null;
|
|
3619
3627
|
}
|
|
3620
|
-
const deviceSeed =
|
|
3621
|
-
const
|
|
3622
|
-
const deviceProof = await
|
|
3623
|
-
|
|
3628
|
+
const deviceSeed = pairingAnchorSeed(account.seed, own.memberId);
|
|
3629
|
+
const anchor = crypto.keyPair(deviceSeed);
|
|
3630
|
+
const deviceProof = await this._shortestAnchorProof(account, {
|
|
3631
|
+
anchor,
|
|
3632
|
+
memberId: own.memberId,
|
|
3633
|
+
memberProof: own.memberProof
|
|
3634
|
+
});
|
|
3635
|
+
if (!(await delegationFits(deviceProof, anchor))) {
|
|
3624
3636
|
throw new CoreError('DEVICE_INVITE_CHAIN_TOO_LONG');
|
|
3625
3637
|
}
|
|
3626
3638
|
return { deviceSeed, deviceProof };
|
|
3627
3639
|
}
|
|
3640
|
+
async _shortestAnchorProof(account, { anchor, memberId, memberProof }) {
|
|
3641
|
+
const keyPair = this._requireDeviceKeyPair();
|
|
3642
|
+
let shortest = await attestDeviceProof(anchor.publicKey, keyPair, memberProof);
|
|
3643
|
+
for await (const row of account.view.find(DEVICES)) {
|
|
3644
|
+
if (row.revoked || !row.memberId || !row.memberProof)
|
|
3645
|
+
continue;
|
|
3646
|
+
if (!b4a.equals(row.memberId, memberId))
|
|
3647
|
+
continue;
|
|
3648
|
+
const cut = chainTo(row.memberProof, anchor.publicKey);
|
|
3649
|
+
if (cut && cut.byteLength < shortest.byteLength) {
|
|
3650
|
+
shortest = cut;
|
|
3651
|
+
}
|
|
3652
|
+
}
|
|
3653
|
+
return shortest;
|
|
3654
|
+
}
|
|
3628
3655
|
previewMeshInvite(invite) {
|
|
3629
3656
|
const decoded = decodeMeshInvite(invite);
|
|
3630
3657
|
if (!decoded) {
|
|
@@ -4741,6 +4768,16 @@ function unmarkedNotice(losers) {
|
|
|
4741
4768
|
return `another device of yours opened a shared mesh at the same moment, so ${ids} is an ordinary mesh now`;
|
|
4742
4769
|
}
|
|
4743
4770
|
function noop() { }
|
|
4771
|
+
async function earliestPaired(account, rows) {
|
|
4772
|
+
const paired = [];
|
|
4773
|
+
for (const row of rows) {
|
|
4774
|
+
const device = await account.view.get(DEVICES, { id: row.id });
|
|
4775
|
+
if (device && !device.revoked) {
|
|
4776
|
+
paired.push(device);
|
|
4777
|
+
}
|
|
4778
|
+
}
|
|
4779
|
+
return paired.sort((a, b) => a.joinedAt - b.joinedAt || b4a.compare(a.id, b.id))[0];
|
|
4780
|
+
}
|
|
4744
4781
|
function ownedBy(device, member) {
|
|
4745
4782
|
const owner = device?.memberId ?? device?.claimedMemberId;
|
|
4746
4783
|
return !!owner && !!member && b4a.equals(owner, member);
|
|
@@ -35,6 +35,11 @@ export interface CredentialEngine extends ConfigEngine {
|
|
|
35
35
|
}): AsyncIterable<unknown> & {
|
|
36
36
|
destroy(): void;
|
|
37
37
|
};
|
|
38
|
+
agentsWatch(req: {
|
|
39
|
+
meshId: string;
|
|
40
|
+
}): AsyncIterable<unknown> & {
|
|
41
|
+
destroy(): void;
|
|
42
|
+
};
|
|
38
43
|
account(req: {}): Promise<{
|
|
39
44
|
meshId?: string | null;
|
|
40
45
|
}>;
|
|
@@ -58,6 +63,7 @@ export interface CredentialPusherLogger {
|
|
|
58
63
|
}
|
|
59
64
|
export type CredentialPusherHandle = (() => Promise<void>) & {
|
|
60
65
|
sync(): Promise<void>;
|
|
66
|
+
ready(): Promise<void>;
|
|
61
67
|
};
|
|
62
68
|
export declare function nextRefreshDelay(entries: ReadonlyArray<Pick<CredentialEntry, 'expiresAt'>>, now?: number): number;
|
|
63
69
|
export declare function credentialOwners(catalog: ReadonlyArray<PusherSkill>): ReadonlyMap<string, readonly string[]>;
|
|
@@ -101,52 +101,39 @@ function fanOut(entry, owners) {
|
|
|
101
101
|
const skills = owners.get(entry.key) ?? [];
|
|
102
102
|
return skills.length > 0 ? skills.map((skill) => ({ ...entry, skill })) : [entry];
|
|
103
103
|
}
|
|
104
|
-
async function
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (entry) {
|
|
111
|
-
last.set(mk, entry);
|
|
112
|
-
entries.push(...fanOut(entry, owners));
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
last.delete(mk);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
catch (err) {
|
|
119
|
-
logger?.warn('credential-pusher', `credential resolve failed for ${spec.credentialKey}`, errorMessage(err));
|
|
120
|
-
const kept = last.get(mk);
|
|
121
|
-
if (kept) {
|
|
122
|
-
entries.push(...fanOut(kept, owners));
|
|
123
|
-
}
|
|
104
|
+
async function resolveEntry({ mk, failure, read }, { last, owners, logger }) {
|
|
105
|
+
try {
|
|
106
|
+
const entry = await read();
|
|
107
|
+
if (!entry) {
|
|
108
|
+
last.delete(mk);
|
|
109
|
+
return [];
|
|
124
110
|
}
|
|
111
|
+
last.set(mk, entry);
|
|
112
|
+
return fanOut(entry, owners);
|
|
125
113
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const mk = storeKey(key, agentId);
|
|
131
|
-
try {
|
|
132
|
-
const entry = await plainEntry(engine, key, agentId);
|
|
133
|
-
if (entry) {
|
|
134
|
-
last.set(mk, entry);
|
|
135
|
-
entries.push(...fanOut(entry, owners));
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
last.delete(mk);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
catch (err) {
|
|
142
|
-
logger?.warn('credential-pusher', `credential read failed for ${key}`, errorMessage(err));
|
|
143
|
-
const kept = last.get(mk);
|
|
144
|
-
if (kept) {
|
|
145
|
-
entries.push(...fanOut(kept, owners));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
logger?.warn('credential-pusher', failure, errorMessage(err));
|
|
116
|
+
const kept = last.get(mk);
|
|
117
|
+
return kept ? fanOut(kept, owners) : [];
|
|
148
118
|
}
|
|
149
|
-
|
|
119
|
+
}
|
|
120
|
+
async function resolveScope(agentId, resolution) {
|
|
121
|
+
const { engine, specs } = resolution;
|
|
122
|
+
const specKeys = new Set(specs.map((spec) => spec.credentialKey));
|
|
123
|
+
const pending = [
|
|
124
|
+
...specs.map((spec) => ({
|
|
125
|
+
mk: storeKey(spec.credentialKey, agentId),
|
|
126
|
+
failure: `credential resolve failed for ${spec.credentialKey}`,
|
|
127
|
+
read: () => oauthEntry(engine, spec, agentId)
|
|
128
|
+
})),
|
|
129
|
+
...PLAIN_KEYS.filter((key) => !specKeys.has(key)).map((key) => ({
|
|
130
|
+
mk: storeKey(key, agentId),
|
|
131
|
+
failure: `credential read failed for ${key}`,
|
|
132
|
+
read: () => plainEntry(engine, key, agentId)
|
|
133
|
+
}))
|
|
134
|
+
];
|
|
135
|
+
const resolved = await Promise.all(pending.map((entry) => resolveEntry(entry, resolution)));
|
|
136
|
+
return resolved.flat();
|
|
150
137
|
}
|
|
151
138
|
async function resolveSettings(engine, scopes, { catalog, logger }) {
|
|
152
139
|
const fields = settingFields(catalog);
|
|
@@ -174,14 +161,22 @@ async function resolveSettings(engine, scopes, { catalog, logger }) {
|
|
|
174
161
|
}
|
|
175
162
|
return entries;
|
|
176
163
|
}
|
|
164
|
+
async function accountScopes(engine) {
|
|
165
|
+
const { meshId } = await engine.account({});
|
|
166
|
+
const ids = meshId
|
|
167
|
+
? ((await engine.listAgents({ meshId })).agents ?? []).map((agent) => agent.id)
|
|
168
|
+
: [];
|
|
169
|
+
return { meshId, scopes: [undefined, ...ids] };
|
|
170
|
+
}
|
|
177
171
|
export function startCredentialPusher(harness, engine, logger) {
|
|
178
172
|
const last = new Map();
|
|
179
173
|
const watches = new Map();
|
|
174
|
+
const agentLists = new Map();
|
|
180
175
|
let disposed = false;
|
|
181
176
|
let timer = null;
|
|
182
177
|
const pushLock = new ScopeLock({ debounce: true });
|
|
183
178
|
const watch = (key) => {
|
|
184
|
-
const stream = engine.configWatch({ key
|
|
179
|
+
const stream = engine.configWatch({ key });
|
|
185
180
|
watches.set(key, stream);
|
|
186
181
|
(async () => {
|
|
187
182
|
for await (const _ of stream) {
|
|
@@ -197,10 +192,11 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
197
192
|
logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
|
|
198
193
|
});
|
|
199
194
|
};
|
|
200
|
-
const syncWatches = (oauthKeys) => {
|
|
195
|
+
const syncWatches = (oauthKeys, scopes) => {
|
|
201
196
|
if (disposed)
|
|
202
197
|
return;
|
|
203
|
-
const
|
|
198
|
+
const keys = new Set([...oauthKeys, ...PLAIN_KEYS]);
|
|
199
|
+
const wanted = new Set(scopes.flatMap((agentId) => [...keys].map((key) => scopedKey(agentId, key))));
|
|
204
200
|
for (const [key, stream] of watches) {
|
|
205
201
|
if (wanted.has(key))
|
|
206
202
|
continue;
|
|
@@ -217,11 +213,34 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
217
213
|
}
|
|
218
214
|
for (const mk of last.keys()) {
|
|
219
215
|
const key = mk.includes('\0') ? mk.slice(mk.indexOf('\0') + 1) : mk;
|
|
220
|
-
if (!
|
|
216
|
+
if (!keys.has(key)) {
|
|
221
217
|
last.delete(mk);
|
|
222
218
|
}
|
|
223
219
|
}
|
|
224
220
|
};
|
|
221
|
+
const syncAgentsWatch = (meshId) => {
|
|
222
|
+
if (disposed)
|
|
223
|
+
return;
|
|
224
|
+
for (const [id, stream] of agentLists) {
|
|
225
|
+
if (id === meshId)
|
|
226
|
+
continue;
|
|
227
|
+
try {
|
|
228
|
+
stream.destroy();
|
|
229
|
+
}
|
|
230
|
+
catch { }
|
|
231
|
+
agentLists.delete(id);
|
|
232
|
+
}
|
|
233
|
+
if (!meshId || agentLists.has(meshId)) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const stream = engine.agentsWatch({ meshId });
|
|
237
|
+
agentLists.set(meshId, stream);
|
|
238
|
+
(async () => {
|
|
239
|
+
for await (const _ of stream) {
|
|
240
|
+
schedule();
|
|
241
|
+
}
|
|
242
|
+
})().catch(noop);
|
|
243
|
+
};
|
|
225
244
|
const armTimer = (delay) => {
|
|
226
245
|
if (disposed)
|
|
227
246
|
return;
|
|
@@ -253,16 +272,12 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
253
272
|
}
|
|
254
273
|
const owners = credentialOwners(skills);
|
|
255
274
|
const specs = oauthSpecs(skills);
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
? ((await engine.listAgents({ meshId })).agents ?? []).map((agent) => agent.id)
|
|
260
|
-
: [];
|
|
261
|
-
const scopes = [undefined, ...ids];
|
|
275
|
+
const { meshId, scopes } = await accountScopes(engine);
|
|
276
|
+
syncWatches(specs.map((spec) => spec.credentialKey), scopes);
|
|
277
|
+
syncAgentsWatch(meshId);
|
|
262
278
|
const resolution = { engine, last, owners, specs, logger };
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
279
|
+
const resolved = await Promise.all(scopes.map((agentId) => resolveScope(agentId, resolution)));
|
|
280
|
+
entries.push(...resolved.flat());
|
|
266
281
|
if (disposed) {
|
|
267
282
|
return;
|
|
268
283
|
}
|
|
@@ -317,7 +332,7 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
317
332
|
pushLock.unlock();
|
|
318
333
|
}
|
|
319
334
|
};
|
|
320
|
-
schedule();
|
|
335
|
+
const first = schedule();
|
|
321
336
|
const stop = async () => {
|
|
322
337
|
if (disposed)
|
|
323
338
|
return;
|
|
@@ -326,15 +341,17 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
326
341
|
clearTimeout(timer);
|
|
327
342
|
}
|
|
328
343
|
timer = null;
|
|
329
|
-
for (const stream of watches.values()) {
|
|
344
|
+
for (const stream of [...watches.values(), ...agentLists.values()]) {
|
|
330
345
|
try {
|
|
331
346
|
stream.destroy();
|
|
332
347
|
}
|
|
333
348
|
catch { }
|
|
334
349
|
}
|
|
335
350
|
watches.clear();
|
|
351
|
+
agentLists.clear();
|
|
336
352
|
pushLock.destroy();
|
|
337
353
|
await pushLock.flush();
|
|
338
354
|
};
|
|
339
|
-
return Object.assign(stop, { sync });
|
|
355
|
+
return Object.assign(stop, { sync, ready: () => first });
|
|
340
356
|
}
|
|
357
|
+
function noop() { }
|
package/dist-lib/lib/errors.d.ts
CHANGED
|
@@ -588,6 +588,14 @@ declare const DEFINITIONS: {
|
|
|
588
588
|
readonly code: 101218;
|
|
589
589
|
readonly message: (status: number, text: string) => string;
|
|
590
590
|
};
|
|
591
|
+
readonly OAUTH_TOKEN_EXCHANGE_TIMEOUT: {
|
|
592
|
+
readonly code: 101219;
|
|
593
|
+
readonly message: () => string;
|
|
594
|
+
};
|
|
595
|
+
readonly OAUTH_REGISTRATION_TIMEOUT: {
|
|
596
|
+
readonly code: 101220;
|
|
597
|
+
readonly message: () => string;
|
|
598
|
+
};
|
|
591
599
|
readonly SIDECAR_ENTRY_MISSING: {
|
|
592
600
|
readonly code: 101301;
|
|
593
601
|
readonly message: () => string;
|
package/dist-lib/lib/errors.js
CHANGED
|
@@ -518,6 +518,14 @@ const DEFINITIONS = {
|
|
|
518
518
|
code: 101218,
|
|
519
519
|
message: (status, text) => `Token refresh failed (${status}): ${text}`
|
|
520
520
|
},
|
|
521
|
+
OAUTH_TOKEN_EXCHANGE_TIMEOUT: {
|
|
522
|
+
code: 101219,
|
|
523
|
+
message: () => 'OAuth token exchange timed out'
|
|
524
|
+
},
|
|
525
|
+
OAUTH_REGISTRATION_TIMEOUT: {
|
|
526
|
+
code: 101220,
|
|
527
|
+
message: () => 'OAuth client registration timed out'
|
|
528
|
+
},
|
|
521
529
|
SIDECAR_ENTRY_MISSING: {
|
|
522
530
|
code: 101301,
|
|
523
531
|
message: () => 'sidecar mode needs a stowed bundle entry'
|
|
@@ -32,10 +32,12 @@ export async function runOAuthFlowWithRuntime(config, deps, runtime) {
|
|
|
32
32
|
state,
|
|
33
33
|
pkce
|
|
34
34
|
});
|
|
35
|
+
deps.onStage?.('awaiting-callback');
|
|
35
36
|
const callback = await redirect.authorize(`${config.authUrl}?${authParams.toString()}`, deps.timeoutMs, state);
|
|
36
37
|
if (callback.state !== state) {
|
|
37
38
|
throw new CoreError('OAUTH_STATE_MISMATCH');
|
|
38
39
|
}
|
|
40
|
+
deps.onStage?.('exchanging-code');
|
|
39
41
|
return exchangeCodeWithFetch(config, {
|
|
40
42
|
clientId,
|
|
41
43
|
redirectUri: redirect.redirectUri,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { OAuthLoopbackServer, OAuthLoopbackTarget } from './redirect-types.ts';
|
|
2
2
|
export declare function abortOAuthLoopback(credentialKey: string): Promise<boolean>;
|
|
3
|
+
export declare function deliverOAuthCallback(address: string): boolean;
|
|
3
4
|
export declare function openOAuthLoopback(port: number, target: OAuthLoopbackTarget): Promise<OAuthLoopbackServer>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createServer } from 'http';
|
|
2
2
|
import { OAUTH_CALLBACK_PATH, OAUTH_WAIT_FOR_CODE_TIMEOUT_MS, oauthRedirectUri } from './keys.js';
|
|
3
3
|
import { CoreError } from '../errors.js';
|
|
4
|
-
const DONE_PAGE = '<html><body><p>Authorization complete. You may close this tab.</p>' +
|
|
4
|
+
const DONE_PAGE = '<html><body><p>Authorization complete. Return to QV.AC to finish connecting. You may close this tab.</p>' +
|
|
5
5
|
'<script>window.close();</script></body></html>';
|
|
6
6
|
const STALE_PAGE = '<html><body><p>This sign-in link is from an earlier attempt. ' +
|
|
7
7
|
'You can close this tab.</p></body></html>';
|
|
@@ -18,6 +18,36 @@ function abortFor(credentialKey, reason) {
|
|
|
18
18
|
}
|
|
19
19
|
return pending.length > 0;
|
|
20
20
|
}
|
|
21
|
+
export function deliverOAuthCallback(address) {
|
|
22
|
+
const params = callbackParams(address);
|
|
23
|
+
const state = params?.get('state');
|
|
24
|
+
const waiter = state ? pendingWaiter(state) : undefined;
|
|
25
|
+
if (!params || !state || !waiter) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
settle(waiter, callbackSettlement(params, state));
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
function callbackParams(address) {
|
|
32
|
+
let url;
|
|
33
|
+
try {
|
|
34
|
+
url = new URL(address.trim());
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const finished = url.searchParams.has('code') || url.searchParams.has('error');
|
|
40
|
+
return url.pathname === OAUTH_CALLBACK_PATH && finished ? url.searchParams : null;
|
|
41
|
+
}
|
|
42
|
+
function pendingWaiter(state) {
|
|
43
|
+
for (const loopback of loopbacks) {
|
|
44
|
+
const waiter = loopback.waiters.get(state);
|
|
45
|
+
if (waiter && waiter.settled === null) {
|
|
46
|
+
return waiter;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
21
51
|
export async function openOAuthLoopback(port, target) {
|
|
22
52
|
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
23
53
|
throw new CoreError('OAUTH_LOOPBACK_PORT_INVALID', [port]);
|