@qvac/core 0.11.1 → 0.11.2
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 +69 -1
- package/README.md +19 -16
- package/dist-lib/lib/api/chunks.js +4 -2
- package/dist-lib/lib/api/rag.js +7 -5
- package/dist-lib/lib/assistant/history.d.ts +1 -0
- package/dist-lib/lib/assistant/history.js +0 -16
- package/dist-lib/lib/assistant-tree.d.ts +2 -0
- package/dist-lib/lib/assistant-tree.js +4 -2
- package/dist-lib/lib/assistant.d.ts +5 -1
- package/dist-lib/lib/assistant.js +63 -20
- package/dist-lib/lib/compaction.d.ts +2 -1
- package/dist-lib/lib/compaction.js +14 -4
- package/dist-lib/lib/constants.d.ts +3 -0
- package/dist-lib/lib/constants.js +3 -0
- package/dist-lib/lib/core/chat-activity.js +12 -15
- package/dist-lib/lib/core/chunk.d.ts +9 -1
- package/dist-lib/lib/core/chunk.js +42 -8
- package/dist-lib/lib/core/files/attachments.js +3 -3
- package/dist-lib/lib/core/files/index.js +2 -1
- package/dist-lib/lib/core/files/processing/index.d.ts +1 -0
- package/dist-lib/lib/core/files/processing/index.js +8 -0
- package/dist-lib/lib/core/files/processing/settle.d.ts +1 -0
- package/dist-lib/lib/core/files/processing/settle.js +20 -13
- package/dist-lib/lib/core/hosted-agents.d.ts +3 -0
- package/dist-lib/lib/core/hosted-agents.js +19 -0
- package/dist-lib/lib/core/notification.js +16 -13
- package/dist-lib/lib/core.d.ts +6 -0
- package/dist-lib/lib/core.js +5 -0
- package/dist-lib/lib/credential-pusher.d.ts +11 -7
- package/dist-lib/lib/credential-pusher.js +40 -8
- package/dist-lib/lib/integrations/setup-routes.js +7 -5
- package/dist-lib/lib/kv-cache-retention.d.ts +34 -0
- package/dist-lib/lib/kv-cache-retention.js +98 -0
- package/dist-lib/schema/content.js +2 -1
- package/dist-lib/schema/qvac.js +4 -2
- package/dist-lib/spec/hrpc/messages.js +4 -10
- package/dist-lib/spec/hyperschema/index.js +4 -10
- package/dist-lib/spec/hyperschema/schema.json +2 -1
- package/dist-lib/spec/hyperschema/types.d.ts +1 -1
- package/dist-lib/spec/mesh/hyperdb/messages.js +34 -14
- 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 +5 -2
- package/tui/app.mjs +743 -266
- package/tui/boot.mjs +2 -2
- package/tui/lib/connect-sheet.mjs +350 -0
- package/tui/lib/setup.mjs +59 -25
- package/tui/lib/skill-connect.mjs +145 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
1
2
|
import ScopeLock from 'scope-lock';
|
|
2
|
-
import {
|
|
3
|
+
import { findChunksOfTypes } from './chunk.js';
|
|
3
4
|
import { ACTIVE_CHATS, CHUNKS } from './collections.js';
|
|
4
5
|
const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
5
6
|
const ACTIVITY_CHUNK_TYPES = [
|
|
@@ -39,15 +40,16 @@ class ChatActivityCatalog {
|
|
|
39
40
|
try {
|
|
40
41
|
const head = store.view.db.head();
|
|
41
42
|
const forwards = store.autobee.stats.fastForwards;
|
|
42
|
-
|
|
43
|
-
this._head
|
|
44
|
-
|
|
45
|
-
this._forwards = forwards;
|
|
43
|
+
const from = forwards === this._forwards && this._head && head && b4a.equals(this._head.key, head.key)
|
|
44
|
+
? this._head
|
|
45
|
+
: null;
|
|
46
46
|
const snap = store.view.snapshot();
|
|
47
47
|
try {
|
|
48
48
|
const chats = await snap.find(ACTIVE_CHATS).toArray();
|
|
49
49
|
const activeChatIds = new Set(chats.map((chat) => chat.id));
|
|
50
|
-
await this._refresh(snap,
|
|
50
|
+
await this._refresh(snap, from, activeChatIds);
|
|
51
|
+
this._head = head;
|
|
52
|
+
this._forwards = forwards;
|
|
51
53
|
const window = chatIds && chatIds.length > 0 ? new Set(chatIds) : null;
|
|
52
54
|
return [...this._states()]
|
|
53
55
|
.filter(([id]) => !window || window.has(id))
|
|
@@ -62,18 +64,17 @@ class ChatActivityCatalog {
|
|
|
62
64
|
this._lock.unlock();
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
async _refresh(snap,
|
|
66
|
-
if (!
|
|
67
|
+
async _refresh(snap, from, activeChatIds) {
|
|
68
|
+
if (!from) {
|
|
67
69
|
await this._rebuild(snap, activeChatIds);
|
|
68
70
|
}
|
|
69
71
|
else {
|
|
70
72
|
this._prune(activeChatIds);
|
|
71
73
|
await this._loadMissing(snap, activeChatIds);
|
|
72
|
-
if (!(await this._applyChanges(snap,
|
|
74
|
+
if (!(await this._applyChanges(snap, from, activeChatIds))) {
|
|
73
75
|
await this._rebuild(snap, activeChatIds);
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
|
-
this._head = head;
|
|
77
78
|
}
|
|
78
79
|
async _rebuild(snap, activeChatIds) {
|
|
79
80
|
this._chats.clear();
|
|
@@ -98,7 +99,7 @@ class ChatActivityCatalog {
|
|
|
98
99
|
async _loadChat(snap, chatId) {
|
|
99
100
|
const activity = { runs: new Map() };
|
|
100
101
|
this._chats.set(chatId, activity);
|
|
101
|
-
for (const chunk of await
|
|
102
|
+
for (const chunk of await findChunksOfTypes(snap, { chatId, types: ACTIVITY_CHUNK_TYPES })) {
|
|
102
103
|
applyChunk(activity, chunk);
|
|
103
104
|
}
|
|
104
105
|
}
|
|
@@ -252,10 +253,6 @@ function applyApproval(run, chunk) {
|
|
|
252
253
|
run.pendingApprovals.delete(chunk.id);
|
|
253
254
|
}
|
|
254
255
|
}
|
|
255
|
-
async function activityChunks(snap, chatId) {
|
|
256
|
-
const groups = await Promise.all(ACTIVITY_CHUNK_TYPES.map((type) => findChunksOfType(snap, { chatId, type })));
|
|
257
|
-
return groups.flat().sort((left, right) => left.seq - right.seq);
|
|
258
|
-
}
|
|
259
256
|
function settleTool(tools, name) {
|
|
260
257
|
for (let index = tools.length - 1; index >= 0; index -= 1) {
|
|
261
258
|
if (tools[index]?.name !== name)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type HyperDB from 'hyperdb';
|
|
2
2
|
import type { QvacChunk, QvacUpdateChunkOperation } from '../../spec/mesh/hyperschema/types.d.ts';
|
|
3
|
+
import type { AssistantChunkContentToolResult } from '../../spec/hyperschema/types.d.ts';
|
|
3
4
|
import type { Mesh, MeshDispatcher } from './mesh.ts';
|
|
4
5
|
import { type Bounds } from './range.ts';
|
|
5
6
|
type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction'>;
|
|
@@ -18,11 +19,13 @@ type ListAttachmentsByKindInput = Bounds & {
|
|
|
18
19
|
kind: MediaKind;
|
|
19
20
|
origin?: AttachmentOrigin;
|
|
20
21
|
};
|
|
21
|
-
export type ReadChunk = QvacChunk & {
|
|
22
|
+
export type ReadChunk = Omit<QvacChunk, 'toolResult' | 'attachment'> & {
|
|
23
|
+
readonly toolResult: AssistantChunkContentToolResult | null;
|
|
22
24
|
readonly attachment?: (NonNullable<QvacChunk['attachment']> & {
|
|
23
25
|
readonly url?: string;
|
|
24
26
|
}) | null;
|
|
25
27
|
};
|
|
28
|
+
export declare function readChunk({ toolResult, ...chunk }: QvacChunk): ReadChunk;
|
|
26
29
|
export declare function linkChunks(store: Mesh, chunks: QvacChunk[]): ReadChunk[];
|
|
27
30
|
export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
|
|
28
31
|
export type ListChunksInput = Bounds & {
|
|
@@ -35,6 +38,11 @@ export interface FindChunksOfTypeInput extends Bounds {
|
|
|
35
38
|
readonly type: QvacChunk['type'];
|
|
36
39
|
}
|
|
37
40
|
export declare function findChunksOfType(snap: HyperDB, { chatId, type, ...bounds }: FindChunksOfTypeInput): Promise<QvacChunk[]>;
|
|
41
|
+
export interface FindChunksOfTypesInput extends Omit<Bounds, 'limit'> {
|
|
42
|
+
readonly chatId: string;
|
|
43
|
+
readonly types: ReadonlyArray<QvacChunk['type']>;
|
|
44
|
+
}
|
|
45
|
+
export declare function findChunksOfTypes(db: HyperDB, { chatId, types, ...bounds }: FindChunksOfTypesInput): Promise<QvacChunk[]>;
|
|
38
46
|
export declare function listChunks(store: Mesh, input: ListChunksInput): Promise<ReadChunk[]>;
|
|
39
47
|
export declare function readChunks(db: HyperDB, { chatId, type, types, ...bounds }: ListChunksInput): Promise<QvacChunk[]>;
|
|
40
48
|
export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
|
|
@@ -16,12 +16,31 @@ export const HISTORY_CHUNK_TYPES = [
|
|
|
16
16
|
'attachment',
|
|
17
17
|
'compaction'
|
|
18
18
|
];
|
|
19
|
+
const FALSY_RESULTS = new Map([
|
|
20
|
+
['false', false],
|
|
21
|
+
['0', 0],
|
|
22
|
+
['""', '']
|
|
23
|
+
]);
|
|
24
|
+
export function readChunk({ toolResult, ...chunk }) {
|
|
25
|
+
if (!toolResult) {
|
|
26
|
+
return { ...chunk, toolResult: null };
|
|
27
|
+
}
|
|
28
|
+
const stored = chunk.falsyResult ?? '';
|
|
29
|
+
const result = FALSY_RESULTS.has(stored) ? FALSY_RESULTS.get(stored) : (toolResult.result ?? null);
|
|
30
|
+
return { ...chunk, toolResult: { ...toolResult, result } };
|
|
31
|
+
}
|
|
19
32
|
export function linkChunks(store, chunks) {
|
|
20
33
|
const mint = store.attachmentUrl;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
return chunks.map((row) => {
|
|
35
|
+
const chunk = readChunk(row);
|
|
36
|
+
return mint && chunk.attachment
|
|
37
|
+
? { ...chunk, attachment: linkAttachment(mint, chunk.attachment) }
|
|
38
|
+
: chunk;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function falsyResultOf(payload) {
|
|
42
|
+
const result = payload.toolResult?.result;
|
|
43
|
+
return result === false || result === 0 || result === '' ? JSON.stringify(result) : undefined;
|
|
25
44
|
}
|
|
26
45
|
export async function recordLinks(store, chunks) {
|
|
27
46
|
await store.fileIndex?.record(chunks.filter((chunk) => chunk.attachment?.url));
|
|
@@ -33,6 +52,14 @@ export function findChunksOfType(snap, { chatId, type, ...bounds }) {
|
|
|
33
52
|
.find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
|
|
34
53
|
.toArray();
|
|
35
54
|
}
|
|
55
|
+
export async function findChunksOfTypes(db, { chatId, types, ...bounds }) {
|
|
56
|
+
const ordered = await readChatOrdered(db, chatId, { types, ...bounds });
|
|
57
|
+
if (ordered) {
|
|
58
|
+
return ordered;
|
|
59
|
+
}
|
|
60
|
+
const byType = await Promise.all(types.map((type) => findChunksOfType(db, { chatId, type, ...bounds })));
|
|
61
|
+
return byType.flat().sort((left, right) => left.seq - right.seq);
|
|
62
|
+
}
|
|
36
63
|
export async function listChunks(store, input) {
|
|
37
64
|
const snap = store.view.snapshot();
|
|
38
65
|
try {
|
|
@@ -54,15 +81,20 @@ export async function readChunks(db, { chatId, type, types, ...bounds }) {
|
|
|
54
81
|
return findChunksOfType(db, { chatId, type: selected[0], ...bounds });
|
|
55
82
|
}
|
|
56
83
|
const history = selected !== null;
|
|
57
|
-
const ordered = history && bounds.limit
|
|
84
|
+
const ordered = history && bounds.limit
|
|
85
|
+
? null
|
|
86
|
+
: await readChatOrdered(db, chatId, {
|
|
87
|
+
types: history ? HISTORY_CHUNK_TYPES : null,
|
|
88
|
+
...bounds
|
|
89
|
+
});
|
|
58
90
|
if (ordered) {
|
|
59
|
-
return
|
|
91
|
+
return ordered;
|
|
60
92
|
}
|
|
61
93
|
return db
|
|
62
94
|
.find(history ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, keyRange({ chatId }, 'seq', bounds))
|
|
63
95
|
.toArray();
|
|
64
96
|
}
|
|
65
|
-
async function readChatOrdered(db, chatId, bounds) {
|
|
97
|
+
async function readChatOrdered(db, chatId, { types, ...bounds }) {
|
|
66
98
|
const counter = await db.get(CHUNK_COUNTERS, { chatId });
|
|
67
99
|
if (!counter) {
|
|
68
100
|
return null;
|
|
@@ -82,7 +114,8 @@ async function readChatOrdered(db, chatId, bounds) {
|
|
|
82
114
|
const settled = new Set(chunks
|
|
83
115
|
.filter((chunk) => TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? ''))
|
|
84
116
|
.map((c) => c.runId));
|
|
85
|
-
|
|
117
|
+
const selected = types ? chunks.filter((chunk) => types.includes(chunk.type)) : chunks;
|
|
118
|
+
return Promise.all(selected.map((chunk) => (changeable(chunk, settled) ? current(db, chunk) : chunk)));
|
|
86
119
|
}
|
|
87
120
|
function changeable(chunk, settled) {
|
|
88
121
|
return ((chunk.type === 'run-status' && !settled.has(chunk.runId)) ||
|
|
@@ -214,6 +247,7 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
|
|
|
214
247
|
capabilityArgs,
|
|
215
248
|
enhance,
|
|
216
249
|
sourceId,
|
|
250
|
+
falsyResult: falsyResultOf(payload),
|
|
217
251
|
createdAt: Date.now(),
|
|
218
252
|
senderMemberId: type === 'user-message' && store.ourDeviceId && b4a.equals(store.ourDeviceId, originDeviceId)
|
|
219
253
|
? store.ourMemberId || undefined
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CoreError } from '../../errors.js';
|
|
2
2
|
import { linkAttachment } from '../attachment-link.js';
|
|
3
|
-
import { getChunk, updateChunk } from '../chunk.js';
|
|
3
|
+
import { getChunk, readChunk, updateChunk } from '../chunk.js';
|
|
4
4
|
import { uploadFile } from './upload.js';
|
|
5
5
|
import b4a from 'b4a';
|
|
6
6
|
export const MAX_ATTACHMENT_BYTES = 100 * 1024 * 1024;
|
|
@@ -39,7 +39,7 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
|
|
|
39
39
|
}
|
|
40
40
|
const attachment = chunk.attachment;
|
|
41
41
|
if (attachment.deletedAt) {
|
|
42
|
-
return chunk;
|
|
42
|
+
return readChunk(chunk);
|
|
43
43
|
}
|
|
44
44
|
const updated = await updateChunk(mesh, {
|
|
45
45
|
id: chunkId,
|
|
@@ -48,7 +48,7 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
|
|
|
48
48
|
if (!updated.attachment?.deletedAt) {
|
|
49
49
|
throw new CoreError('ATTACHMENT_DELETE_DROPPED');
|
|
50
50
|
}
|
|
51
|
-
return updated;
|
|
51
|
+
return readChunk(updated);
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
}
|
|
@@ -332,7 +332,8 @@ export class Files extends EventEmitter {
|
|
|
332
332
|
processing: this._processing,
|
|
333
333
|
chatIds: async () => {
|
|
334
334
|
const rows = await Promise.all(ids.map((id) => getKnowledge(this._mesh, id)));
|
|
335
|
-
|
|
335
|
+
const chatIds = rows.flatMap((row) => (row ? [row.chatId] : []));
|
|
336
|
+
return [...new Set(options.chatId ? [options.chatId, ...chatIds] : chatIds)];
|
|
336
337
|
}
|
|
337
338
|
});
|
|
338
339
|
}
|
|
@@ -61,6 +61,7 @@ export declare class FileProcessing {
|
|
|
61
61
|
enqueue(ids: Iterable<string>, options?: EnqueueOptions): Promise<void>;
|
|
62
62
|
private _enqueue;
|
|
63
63
|
get active(): boolean;
|
|
64
|
+
canProduce(id: string): Promise<boolean>;
|
|
64
65
|
pending(id: string): boolean;
|
|
65
66
|
prioritize(ids: string[]): void;
|
|
66
67
|
private _shutdown;
|
|
@@ -118,6 +118,14 @@ export class FileProcessing {
|
|
|
118
118
|
get active() {
|
|
119
119
|
return !this._paused && !this._closed && !this._mesh.closing;
|
|
120
120
|
}
|
|
121
|
+
async canProduce(id) {
|
|
122
|
+
const record = await fileRecord(this._mesh.view, id, this._processor.input);
|
|
123
|
+
return Boolean(record &&
|
|
124
|
+
!record.deletedAt &&
|
|
125
|
+
this._mesh.writable &&
|
|
126
|
+
this._processor.applies(record) &&
|
|
127
|
+
(await this._processor.canProduce(record, { mesh: this._mesh, deviceId: this._deviceId })));
|
|
128
|
+
}
|
|
121
129
|
pending(id) {
|
|
122
130
|
return (!this._paused &&
|
|
123
131
|
!this._closed &&
|
|
@@ -3,6 +3,7 @@ import type { Files } from '../index.ts';
|
|
|
3
3
|
import type { FileProcessing } from './index.ts';
|
|
4
4
|
export interface SettleOptions {
|
|
5
5
|
readonly timeoutMs?: number;
|
|
6
|
+
readonly chatId?: string;
|
|
6
7
|
readonly signal?: AbortSignal;
|
|
7
8
|
}
|
|
8
9
|
export interface SettleFilesOptions extends SettleOptions {
|
|
@@ -48,22 +48,29 @@ export function settleFiles(files, options) {
|
|
|
48
48
|
if (!processing.active) {
|
|
49
49
|
return finish();
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
let waiting = false;
|
|
52
|
+
for (const file of snapshots.flatMap((snapshot) => snapshot.files)) {
|
|
53
|
+
if (waiting)
|
|
54
|
+
break;
|
|
54
55
|
if (!wanted.has(file.fileId) || file.readyForChat)
|
|
55
|
-
|
|
56
|
+
continue;
|
|
56
57
|
const rag = file.processors.find((entry) => entry.processorId === 'rag');
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
const settled = !rag ||
|
|
59
|
+
rag.state === 'not-applicable' ||
|
|
60
|
+
(rag.state === 'failed' && (!rag.retryAt || rag.retryAt > deadline));
|
|
61
|
+
if (settled) {
|
|
62
|
+
continue;
|
|
59
63
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
64
|
+
waiting =
|
|
65
|
+
processing.pending(file.fileId) ||
|
|
66
|
+
rag.state === 'running' ||
|
|
67
|
+
(rag.retryAt !== undefined && rag.retryAt <= deadline) ||
|
|
68
|
+
(rag.reason === 'waiting for agent' &&
|
|
69
|
+
(!(await files.record(file.fileId)) || (await processing.canProduce(file.fileId))));
|
|
70
|
+
}
|
|
71
|
+
if (done) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
67
74
|
if (!waiting) {
|
|
68
75
|
finish();
|
|
69
76
|
}
|
|
@@ -24,6 +24,8 @@ export declare class HostedAgents {
|
|
|
24
24
|
private readonly _log;
|
|
25
25
|
private readonly _heldKey;
|
|
26
26
|
private readonly _lock;
|
|
27
|
+
private readonly _retry;
|
|
28
|
+
private readonly _deadline;
|
|
27
29
|
private _closed;
|
|
28
30
|
private readonly _onDirty;
|
|
29
31
|
constructor(opts: HostedAgentsOptions);
|
|
@@ -32,6 +34,7 @@ export declare class HostedAgents {
|
|
|
32
34
|
close(): Promise<void>;
|
|
33
35
|
private _run;
|
|
34
36
|
private _pass;
|
|
37
|
+
private _scheduleRetry;
|
|
35
38
|
private _handOver;
|
|
36
39
|
private _stored;
|
|
37
40
|
private _held;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import ScopeLock from 'scope-lock';
|
|
3
|
+
import { DeadlineScheduler } from '../deadline.js';
|
|
4
|
+
import { RetryBudget } from '../retry-budget.js';
|
|
3
5
|
import { getAgent, listAgents } from './agent.js';
|
|
4
6
|
import { agentSkillValues } from './skill-secrets.js';
|
|
5
7
|
const SCOPE = ['p2p', 'skill-values'];
|
|
8
|
+
const RETRY_MS = 1_000;
|
|
6
9
|
export const LEGACY_HELD_AGENTS_KEY = 'skill-values.held-agents';
|
|
7
10
|
export function heldAgentsKey(meshKey) {
|
|
8
11
|
return `${LEGACY_HELD_AGENTS_KEY}.${b4a.toHex(meshKey)}`;
|
|
@@ -17,6 +20,8 @@ export class HostedAgents {
|
|
|
17
20
|
_log;
|
|
18
21
|
_heldKey;
|
|
19
22
|
_lock = new ScopeLock({ debounce: true });
|
|
23
|
+
_retry = new RetryBudget({ retryMs: RETRY_MS });
|
|
24
|
+
_deadline = new DeadlineScheduler(() => this.kick());
|
|
20
25
|
_closed;
|
|
21
26
|
_onDirty = () => this.kick();
|
|
22
27
|
constructor(opts) {
|
|
@@ -40,6 +45,7 @@ export class HostedAgents {
|
|
|
40
45
|
async close() {
|
|
41
46
|
this._closed = true;
|
|
42
47
|
this._mesh.off('agents-dirty', this._onDirty);
|
|
48
|
+
this._deadline.clear();
|
|
43
49
|
this._lock.destroy();
|
|
44
50
|
await this._lock.flush();
|
|
45
51
|
}
|
|
@@ -76,13 +82,26 @@ export class HostedAgents {
|
|
|
76
82
|
});
|
|
77
83
|
if (released) {
|
|
78
84
|
holding.delete(id);
|
|
85
|
+
this._retry.reset(id);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this._retry.fail(id, Date.now());
|
|
79
89
|
}
|
|
80
90
|
}
|
|
81
91
|
}
|
|
92
|
+
this._scheduleRetry(new Set(departed.filter((id) => holding.has(id))));
|
|
82
93
|
if (!sameIds(held, holding)) {
|
|
83
94
|
await this._local.set(this._heldKey, [...holding]);
|
|
84
95
|
}
|
|
85
96
|
}
|
|
97
|
+
_scheduleRetry(pending) {
|
|
98
|
+
this._retry.retain(pending);
|
|
99
|
+
this._deadline.clear();
|
|
100
|
+
const next = this._retry.nextDeadline(pending, Date.now());
|
|
101
|
+
if (next !== null && !this._closed) {
|
|
102
|
+
this._deadline.schedule(next);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
86
105
|
async _handOver(id, catalog) {
|
|
87
106
|
const agent = await getAgent(this._mesh, id);
|
|
88
107
|
if (!agent) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { diagramRepairLinks, isDiagramRepairPrompt } from '@qvac/harness/lib/diagram-repair.ts';
|
|
2
|
-
import { findChunksOfType } from './chunk.js';
|
|
2
|
+
import { findChunksOfType, findChunksOfTypes } from './chunk.js';
|
|
3
3
|
import b4a from 'b4a';
|
|
4
4
|
import { ACTIVE_CHATS, AGENTS, CHUNK_COUNTERS, DEVICES, MEMBERS } from './collections.js';
|
|
5
5
|
const DEFAULT_LIMIT = 200;
|
|
@@ -46,7 +46,7 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
|
|
|
46
46
|
}
|
|
47
47
|
const contentByChat = new Map();
|
|
48
48
|
for (const [chatId, terminalSeq] of terminalSeqByChat) {
|
|
49
|
-
contentByChat.set(chatId, await
|
|
49
|
+
contentByChat.set(chatId, await findChunksOfTypes(snap, { chatId, types: ['content'], lte: terminalSeq }));
|
|
50
50
|
}
|
|
51
51
|
const agentNameById = new Map();
|
|
52
52
|
for (const agentId of new Set(selected.map((candidate) => candidate.agentId))) {
|
|
@@ -74,15 +74,18 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
|
|
|
74
74
|
async function chatCandidates(snap, chat, { local, deviceId, own }) {
|
|
75
75
|
const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
|
|
76
76
|
const ackedThrough = (await local.getChatNotify(chat.id))?.ackedThrough ?? 0;
|
|
77
|
-
const [
|
|
77
|
+
const [unread, approvals] = await Promise.all([
|
|
78
78
|
(counter?.count ?? 0) <= ackedThrough
|
|
79
79
|
? []
|
|
80
|
-
:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
: findChunksOfTypes(snap, {
|
|
81
|
+
chatId: chat.id,
|
|
82
|
+
types: ['run-status', 'user-message'],
|
|
83
|
+
gte: ackedThrough
|
|
84
|
+
}),
|
|
85
|
+
findChunksOfType(snap, { chatId: chat.id, type: 'approval-request' })
|
|
85
86
|
]);
|
|
87
|
+
const statuses = unread.filter((chunk) => chunk.type === 'run-status');
|
|
88
|
+
const messages = unread.filter((chunk) => chunk.type === 'user-message');
|
|
86
89
|
const terminalStatuses = statuses.filter((chunk) => {
|
|
87
90
|
const state = chunk.runStatus?.state;
|
|
88
91
|
return state === 'completed' || state === 'failed';
|
|
@@ -148,11 +151,11 @@ async function messageCandidate(snap, chunk, { chat, deviceId, own }) {
|
|
|
148
151
|
};
|
|
149
152
|
}
|
|
150
153
|
async function diagramRepairRunIds(snap, chatId, toSeq) {
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
const records = await findChunksOfTypes(snap, {
|
|
155
|
+
chatId,
|
|
156
|
+
types: ['user-message', 'run-request'],
|
|
157
|
+
lte: toSeq
|
|
158
|
+
});
|
|
156
159
|
const repairs = diagramRepairLinks(records, (chunk) => chunk.userMessage?.text ?? null);
|
|
157
160
|
const runIds = new Set();
|
|
158
161
|
for (const chunk of records) {
|
package/dist-lib/lib/core.d.ts
CHANGED
|
@@ -87,6 +87,12 @@ export interface NetworkConnection {
|
|
|
87
87
|
}
|
|
88
88
|
export interface NetworkGate {
|
|
89
89
|
readonly onConnection?: (conn: NetworkConnection) => void;
|
|
90
|
+
readonly onMeshTopic?: (topic: MeshTopic) => void;
|
|
91
|
+
}
|
|
92
|
+
export interface MeshTopic {
|
|
93
|
+
readonly meshId: string;
|
|
94
|
+
readonly topic: Buffer;
|
|
95
|
+
readonly swarm: Hyperswarm;
|
|
90
96
|
}
|
|
91
97
|
export declare const DEVICE_UNKNOWN = "This device has no device details: pass `device` when creating Core";
|
|
92
98
|
export interface SharedMesh {
|
package/dist-lib/lib/core.js
CHANGED
|
@@ -1502,6 +1502,11 @@ export class Core extends ReadyResource {
|
|
|
1502
1502
|
return;
|
|
1503
1503
|
if (this._swarm) {
|
|
1504
1504
|
this._swarm.join(mesh.discoveryKey, { server: true, client: true });
|
|
1505
|
+
this._network?.onMeshTopic?.({
|
|
1506
|
+
meshId: this._meshIdOf(mesh),
|
|
1507
|
+
topic: mesh.discoveryKey,
|
|
1508
|
+
swarm: this._swarm
|
|
1509
|
+
});
|
|
1505
1510
|
this._startBlindPeering(mesh);
|
|
1506
1511
|
if (this.suspended) {
|
|
1507
1512
|
void mesh.suspend().catch(noop);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Harness } from './assistant.ts';
|
|
2
2
|
import { type ConfigEngine } from './oauth.ts';
|
|
3
|
-
import type { OAuthRouteLike } from './oauth/keys.ts';
|
|
3
|
+
import type { OAuthKeySpec, OAuthRouteLike } from './oauth/keys.ts';
|
|
4
4
|
export interface CredentialEntry {
|
|
5
5
|
key: string;
|
|
6
6
|
token?: string | null;
|
|
@@ -14,19 +14,21 @@ export interface SettingEntry {
|
|
|
14
14
|
value: string;
|
|
15
15
|
agentId?: string | null;
|
|
16
16
|
}
|
|
17
|
+
type PusherRoute = OAuthRouteLike & {
|
|
18
|
+
fields?: ReadonlyArray<{
|
|
19
|
+
key: string;
|
|
20
|
+
secret?: boolean;
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
17
23
|
export interface PusherSkill {
|
|
18
24
|
name: string;
|
|
25
|
+
origin?: string;
|
|
19
26
|
credentials?: readonly string[];
|
|
20
27
|
requires?: {
|
|
21
28
|
credentials?: readonly string[];
|
|
22
29
|
};
|
|
23
30
|
setup?: {
|
|
24
|
-
routes?: ReadonlyArray<
|
|
25
|
-
fields?: ReadonlyArray<{
|
|
26
|
-
key: string;
|
|
27
|
-
secret?: boolean;
|
|
28
|
-
}>;
|
|
29
|
-
}>;
|
|
31
|
+
routes?: ReadonlyArray<PusherRoute>;
|
|
30
32
|
};
|
|
31
33
|
}
|
|
32
34
|
export interface CredentialEngine extends ConfigEngine {
|
|
@@ -67,6 +69,8 @@ export type CredentialPusherHandle = (() => Promise<void>) & {
|
|
|
67
69
|
};
|
|
68
70
|
export declare function nextRefreshDelay(entries: ReadonlyArray<Pick<CredentialEntry, 'expiresAt'>>, now?: number): number;
|
|
69
71
|
export declare function credentialOwners(catalog: ReadonlyArray<PusherSkill>): ReadonlyMap<string, readonly string[]>;
|
|
72
|
+
export declare function plainCredentialKeys(catalog: ReadonlyArray<PusherSkill>, specs: ReadonlyArray<OAuthKeySpec>): ReadonlyArray<string>;
|
|
70
73
|
export declare function settingFields(catalog: ReadonlyArray<PusherSkill>): ReadonlyArray<Pick<SettingEntry, 'skill' | 'key'>>;
|
|
71
74
|
export declare function settingConfigKey(agentId: string | undefined, skill: string, key: string): string;
|
|
72
75
|
export declare function startCredentialPusher(harness: CredentialTarget, engine: CredentialEngine, logger?: CredentialPusherLogger | null): CredentialPusherHandle;
|
|
76
|
+
export {};
|
|
@@ -8,6 +8,7 @@ import ScopeLock from 'scope-lock';
|
|
|
8
8
|
import { credentialSlots } from '@qvac/harness/lib/tools/skills/credential-slots.ts';
|
|
9
9
|
import { errorMessage } from './guards.js';
|
|
10
10
|
const PLAIN_KEYS = ['github_access_token', OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY];
|
|
11
|
+
const PLAIN_KEY = /^[A-Za-z0-9_-]+$/;
|
|
11
12
|
const REFRESH_INTERVAL = 10 * 60 * 1000;
|
|
12
13
|
const MIN_REFRESH_DELAY = 30_000;
|
|
13
14
|
function storeKey(key, agentId) {
|
|
@@ -69,7 +70,7 @@ async function plainEntry(engine, key, agentId) {
|
|
|
69
70
|
}
|
|
70
71
|
export function credentialOwners(catalog) {
|
|
71
72
|
const owners = new Map();
|
|
72
|
-
for (const skill of catalog) {
|
|
73
|
+
for (const skill of trusted(catalog)) {
|
|
73
74
|
for (const key of credentialSlots(skill).flat()) {
|
|
74
75
|
const list = owners.get(key) ?? [];
|
|
75
76
|
if (!list.includes(skill.name)) {
|
|
@@ -80,6 +81,37 @@ export function credentialOwners(catalog) {
|
|
|
80
81
|
}
|
|
81
82
|
return owners;
|
|
82
83
|
}
|
|
84
|
+
function trusted(catalog) {
|
|
85
|
+
return catalog.filter((skill) => skill.origin !== 'overlay');
|
|
86
|
+
}
|
|
87
|
+
export function plainCredentialKeys(catalog, specs) {
|
|
88
|
+
const oauth = new Set(specs.map((spec) => spec.credentialKey));
|
|
89
|
+
const reserved = new Set();
|
|
90
|
+
const declared = new Set(PLAIN_KEYS);
|
|
91
|
+
for (const skill of trusted(catalog)) {
|
|
92
|
+
for (const key of credentialSlots(skill).flat()) {
|
|
93
|
+
declared.add(key);
|
|
94
|
+
}
|
|
95
|
+
for (const route of skill.setup?.routes ?? []) {
|
|
96
|
+
routeKeys(route, route.oauth ? reserved : declared);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const allowed = (key) => PLAIN_KEYS.includes(key) || (!reserved.has(key) && PLAIN_KEY.test(key));
|
|
100
|
+
return [...declared].filter((key) => !oauth.has(key) && allowed(key));
|
|
101
|
+
}
|
|
102
|
+
function routeKeys(route, into) {
|
|
103
|
+
if (route.oauth?.stateKey) {
|
|
104
|
+
into.add(route.oauth.stateKey);
|
|
105
|
+
}
|
|
106
|
+
if (route.credentialKey && !route.oauth) {
|
|
107
|
+
into.add(route.credentialKey);
|
|
108
|
+
}
|
|
109
|
+
for (const field of route.fields ?? []) {
|
|
110
|
+
if (route.oauth || field.secret === true) {
|
|
111
|
+
into.add(field.key);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
83
115
|
export function settingFields(catalog) {
|
|
84
116
|
const fields = [];
|
|
85
117
|
for (const skill of catalog) {
|
|
@@ -118,15 +150,14 @@ async function resolveEntry({ mk, failure, read }, { last, owners, logger }) {
|
|
|
118
150
|
}
|
|
119
151
|
}
|
|
120
152
|
async function resolveScope(agentId, resolution) {
|
|
121
|
-
const { engine, specs } = resolution;
|
|
122
|
-
const specKeys = new Set(specs.map((spec) => spec.credentialKey));
|
|
153
|
+
const { engine, specs, plainKeys } = resolution;
|
|
123
154
|
const pending = [
|
|
124
155
|
...specs.map((spec) => ({
|
|
125
156
|
mk: storeKey(spec.credentialKey, agentId),
|
|
126
157
|
failure: `credential resolve failed for ${spec.credentialKey}`,
|
|
127
158
|
read: () => oauthEntry(engine, spec, agentId)
|
|
128
159
|
})),
|
|
129
|
-
...
|
|
160
|
+
...plainKeys.map((key) => ({
|
|
130
161
|
mk: storeKey(key, agentId),
|
|
131
162
|
failure: `credential read failed for ${key}`,
|
|
132
163
|
read: () => plainEntry(engine, key, agentId)
|
|
@@ -192,10 +223,10 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
192
223
|
logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
|
|
193
224
|
});
|
|
194
225
|
};
|
|
195
|
-
const syncWatches = (
|
|
226
|
+
const syncWatches = (pushedKeys, scopes) => {
|
|
196
227
|
if (disposed)
|
|
197
228
|
return;
|
|
198
|
-
const keys = new Set(
|
|
229
|
+
const keys = new Set(pushedKeys);
|
|
199
230
|
const wanted = new Set(scopes.flatMap((agentId) => [...keys].map((key) => scopedKey(agentId, key))));
|
|
200
231
|
for (const [key, stream] of watches) {
|
|
201
232
|
if (wanted.has(key))
|
|
@@ -272,10 +303,11 @@ export function startCredentialPusher(harness, engine, logger) {
|
|
|
272
303
|
}
|
|
273
304
|
const owners = credentialOwners(skills);
|
|
274
305
|
const specs = oauthSpecs(skills);
|
|
306
|
+
const plainKeys = plainCredentialKeys(skills, specs);
|
|
275
307
|
const { meshId, scopes } = await accountScopes(engine);
|
|
276
|
-
syncWatches(specs.map((spec) => spec.credentialKey), scopes);
|
|
308
|
+
syncWatches([...specs.map((spec) => spec.credentialKey), ...plainKeys], scopes);
|
|
277
309
|
syncAgentsWatch(meshId);
|
|
278
|
-
const resolution = { engine, last, owners, specs, logger };
|
|
310
|
+
const resolution = { engine, last, owners, specs, plainKeys, logger };
|
|
279
311
|
const resolved = await Promise.all(scopes.map((agentId) => resolveScope(agentId, resolution)));
|
|
280
312
|
entries.push(...resolved.flat());
|
|
281
313
|
if (disposed) {
|
|
@@ -47,10 +47,7 @@ function prioritizeVaultPicker(setup) {
|
|
|
47
47
|
function derivedRoutes(entry) {
|
|
48
48
|
const routes = [];
|
|
49
49
|
for (const key of credentialKeysFor(entry)) {
|
|
50
|
-
|
|
51
|
-
if (route) {
|
|
52
|
-
routes.push(route);
|
|
53
|
-
}
|
|
50
|
+
routes.push(credentialRoute(key));
|
|
54
51
|
}
|
|
55
52
|
const install = installRoute(entry);
|
|
56
53
|
if (install) {
|
|
@@ -79,7 +76,12 @@ function credentialRoute(credentialKey) {
|
|
|
79
76
|
fields: [{ key: credentialKey, label: token.label, secret: true, required: true }]
|
|
80
77
|
};
|
|
81
78
|
}
|
|
82
|
-
return
|
|
79
|
+
return {
|
|
80
|
+
kind: SETUP_ROUTE_KIND.token,
|
|
81
|
+
label: `Enter ${credentialKey}`,
|
|
82
|
+
credentialKey,
|
|
83
|
+
fields: [{ key: credentialKey, label: credentialKey, secret: true, required: true }]
|
|
84
|
+
};
|
|
83
85
|
}
|
|
84
86
|
function installRoute(entry) {
|
|
85
87
|
const steps = entry.metadata.install ?? [];
|