@qvac/core 0.6.7 → 0.7.1

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.
Files changed (146) hide show
  1. package/CHANGELOG.md +211 -1
  2. package/README.md +2 -0
  3. package/dist-lib/lib/api/app-config.d.ts +3 -1
  4. package/dist-lib/lib/api/app-config.js +15 -4
  5. package/dist-lib/lib/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +9 -0
  7. package/dist-lib/lib/api/index.d.ts +2 -1
  8. package/dist-lib/lib/api/index.js +2 -1
  9. package/dist-lib/lib/api/network.d.ts +8 -1
  10. package/dist-lib/lib/api/network.js +6 -0
  11. package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
  12. package/dist-lib/lib/assistant/fake-models.js +112 -0
  13. package/dist-lib/lib/assistant/harness.d.ts +12 -0
  14. package/dist-lib/lib/assistant/harness.js +26 -0
  15. package/dist-lib/lib/assistant/history.d.ts +5 -1
  16. package/dist-lib/lib/assistant/history.js +14 -2
  17. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  18. package/dist-lib/lib/assistant-tree.js +45 -7
  19. package/dist-lib/lib/assistant.d.ts +15 -2
  20. package/dist-lib/lib/assistant.js +181 -42
  21. package/dist-lib/lib/constants.d.ts +6 -0
  22. package/dist-lib/lib/constants.js +6 -0
  23. package/dist-lib/lib/core/agent.d.ts +2 -0
  24. package/dist-lib/lib/core/agent.js +16 -5
  25. package/dist-lib/lib/core/blind-peers.d.ts +4 -0
  26. package/dist-lib/lib/core/blind-peers.js +1 -0
  27. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  28. package/dist-lib/lib/core/chat-activity.js +3 -0
  29. package/dist-lib/lib/core/chat-copy.d.ts +4 -0
  30. package/dist-lib/lib/core/chat-copy.js +1 -1
  31. package/dist-lib/lib/core/chat-fork.d.ts +21 -0
  32. package/dist-lib/lib/core/chat-fork.js +245 -0
  33. package/dist-lib/lib/core/chat-move.js +1 -4
  34. package/dist-lib/lib/core/chat.d.ts +1 -0
  35. package/dist-lib/lib/core/chat.js +1 -1
  36. package/dist-lib/lib/core/collections.d.ts +1 -0
  37. package/dist-lib/lib/core/collections.js +1 -0
  38. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  39. package/dist-lib/lib/core/contract/compute.js +2 -1
  40. package/dist-lib/lib/core/contract/index.js +6 -1
  41. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  42. package/dist-lib/lib/core/contract/invites.js +3 -3
  43. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  44. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  45. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  46. package/dist-lib/lib/core/contract/open-join.js +57 -0
  47. package/dist-lib/lib/core/departures.d.ts +8 -1
  48. package/dist-lib/lib/core/departures.js +59 -2
  49. package/dist-lib/lib/core/fs-drives.d.ts +8 -1
  50. package/dist-lib/lib/core/fs-drives.js +47 -15
  51. package/dist-lib/lib/core/harness-loan.d.ts +4 -20
  52. package/dist-lib/lib/core/harness-loan.js +7 -184
  53. package/dist-lib/lib/core/id.d.ts +1 -0
  54. package/dist-lib/lib/core/id.js +3 -0
  55. package/dist-lib/lib/core/identity.d.ts +1 -1
  56. package/dist-lib/lib/core/identity.js +6 -4
  57. package/dist-lib/lib/core/invite.d.ts +2 -0
  58. package/dist-lib/lib/core/invite.js +6 -2
  59. package/dist-lib/lib/core/membership.d.ts +1 -12
  60. package/dist-lib/lib/core/membership.js +1 -94
  61. package/dist-lib/lib/core/mesh.d.ts +11 -15
  62. package/dist-lib/lib/core/mesh.js +52 -88
  63. package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
  64. package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
  65. package/dist-lib/lib/core/migrations/index.js +3 -1
  66. package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
  67. package/dist-lib/lib/core/peer-duplex.js +207 -0
  68. package/dist-lib/lib/core/sync/index.js +2 -16
  69. package/dist-lib/lib/core.d.ts +47 -50
  70. package/dist-lib/lib/core.js +524 -878
  71. package/dist-lib/lib/diagnostics.d.ts +7 -0
  72. package/dist-lib/lib/diagnostics.js +22 -0
  73. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  74. package/dist-lib/lib/doctor/checks.js +272 -0
  75. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  76. package/dist-lib/lib/doctor/redact.js +31 -0
  77. package/dist-lib/lib/doctor/render.d.ts +8 -0
  78. package/dist-lib/lib/doctor/render.js +83 -0
  79. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  80. package/dist-lib/lib/doctor/tree.js +167 -0
  81. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  82. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  83. package/dist-lib/lib/guards.d.ts +1 -0
  84. package/dist-lib/lib/guards.js +4 -0
  85. package/dist-lib/lib/inference-calls.js +1 -5
  86. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  87. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  88. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  89. package/dist-lib/lib/model-download-status.d.ts +1 -1
  90. package/dist-lib/lib/model-download.d.ts +9 -10
  91. package/dist-lib/lib/model-download.js +49 -16
  92. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  93. package/dist-lib/lib/model-memory-cap.js +69 -0
  94. package/dist-lib/lib/models-watch.d.ts +1 -1
  95. package/dist-lib/lib/models-watch.js +4 -4
  96. package/dist-lib/lib/oauth/flow.js +1 -1
  97. package/dist-lib/lib/oauth/loopback.js +16 -2
  98. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  99. package/dist-lib/lib/run-replay.d.ts +26 -0
  100. package/dist-lib/lib/run-replay.js +107 -0
  101. package/dist-lib/schema/config.d.ts +1 -1
  102. package/dist-lib/schema/config.js +1 -1
  103. package/dist-lib/schema/content.js +9 -1
  104. package/dist-lib/schema/engine.js +75 -6
  105. package/dist-lib/schema/field-order.js +1 -1
  106. package/dist-lib/schema/local.js +3 -2
  107. package/dist-lib/schema/qvac.js +28 -2
  108. package/dist-lib/spec/bind.js +17 -0
  109. package/dist-lib/spec/calls.js +4 -0
  110. package/dist-lib/spec/capabilities.d.ts +8 -0
  111. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  112. package/dist-lib/spec/hrpc/hrpc.json +52 -0
  113. package/dist-lib/spec/hrpc/index.js +53 -4
  114. package/dist-lib/spec/hrpc/messages.js +1498 -1278
  115. package/dist-lib/spec/hyperschema/index.js +1498 -1278
  116. package/dist-lib/spec/hyperschema/schema.json +182 -0
  117. package/dist-lib/spec/hyperschema/types.d.ts +49 -3
  118. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  119. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  120. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  121. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  122. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  123. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  124. package/dist-lib/spec/mesh/hyperdb/messages.js +530 -246
  125. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  126. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  127. package/dist-lib/spec/mesh/hyperdispatch/messages.js +223 -10
  128. package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
  129. package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
  130. package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
  131. package/package.json +14 -19
  132. package/tui/README.md +7 -1
  133. package/tui/app.mjs +366 -78
  134. package/tui/boot.mjs +1 -0
  135. package/tui/index.mjs +1 -0
  136. package/tui/lib/fork-command.mjs +43 -0
  137. package/tui/lib/render.mjs +25 -1
  138. package/worker/assistant-sidecar.mjs +5 -2
  139. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  140. package/dist-lib/lib/core/device-invites.js +0 -63
  141. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  142. package/dist-lib/lib/core/grant-expiry.js +0 -66
  143. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  144. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  145. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  146. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -0,0 +1,37 @@
1
+ import ScopeLock from 'scope-lock';
2
+ import { errorMessage } from './guards.js';
3
+ export function startDownloadsAllowedPusher(harness, state, logger) {
4
+ const lock = new ScopeLock({ debounce: true });
5
+ let disposed = false;
6
+ let pushed = null;
7
+ const push = async () => {
8
+ const allowed = !(await state.getAppConfigRow())?.manualModelDownloads;
9
+ if (disposed || allowed === pushed) {
10
+ return;
11
+ }
12
+ await harness.setDownloadsAllowed?.({ allowed });
13
+ pushed = allowed;
14
+ };
15
+ const schedule = async () => {
16
+ if (disposed || !(await lock.lock()))
17
+ return;
18
+ try {
19
+ await push();
20
+ }
21
+ catch (err) {
22
+ logger?.warn('downloads-allowed-pusher', 'push failed', errorMessage(err));
23
+ }
24
+ finally {
25
+ lock.unlock();
26
+ }
27
+ };
28
+ const listener = () => void schedule();
29
+ state.watch(listener);
30
+ void schedule();
31
+ return async () => {
32
+ disposed = true;
33
+ state.unwatch(listener);
34
+ lock.destroy();
35
+ await lock.flush();
36
+ };
37
+ }
@@ -1,3 +1,4 @@
1
1
  export declare function isRecord(value: unknown): value is Record<string, unknown>;
2
2
  export declare function errorMessage(err: unknown): string;
3
3
  export declare function asError(err: unknown): Error;
4
+ export declare function safeName(value: string): string;
@@ -7,3 +7,7 @@ export function errorMessage(err) {
7
7
  export function asError(err) {
8
8
  return err instanceof Error ? err : new Error(String(err));
9
9
  }
10
+ export function safeName(value) {
11
+ const fenced = value.replace(/[^A-Za-z0-9._-]/g, '_');
12
+ return /^\.*$/.test(fenced) ? `_${fenced}` : fenced;
13
+ }
@@ -2,7 +2,7 @@ import fs from 'fs';
2
2
  import { join } from 'path';
3
3
  import { persistent } from 'bare-storage';
4
4
  import { WriteQueue } from './write-queue.js';
5
- import { isRecord } from './guards.js';
5
+ import { isRecord, safeName } from './guards.js';
6
6
  export function defaultInferenceCallsDir() {
7
7
  return join(persistent(), 'qvac', 'inspector');
8
8
  }
@@ -271,8 +271,4 @@ async function sizeOf(path) {
271
271
  return null;
272
272
  }
273
273
  }
274
- function safeName(value) {
275
- const fenced = value.replace(/[^A-Za-z0-9._-]/g, '_');
276
- return /^\.*$/.test(fenced) ? `_${fenced}` : fenced;
277
- }
278
274
  function noop() { }
@@ -1,5 +1,6 @@
1
- import type { AssistantAgent } from '../spec/hyperschema/types.d.ts';
1
+ import type { AssistantAgent, AssistantChunk } from '../spec/hyperschema/types.d.ts';
2
2
  export declare function chatKvCacheKey(chatId: string): string;
3
3
  export declare function chatRepairKvCacheKey(chatId: string, acceptedRepairs: number): string;
4
+ export declare function agentSwitched(chunks: ReadonlyArray<AssistantChunk>, request: AssistantChunk, deviceId: Uint8Array): boolean;
4
5
  export declare function disappearedChatIds(known: ReadonlySet<string>, current: ReadonlyArray<string>): string[];
5
6
  export declare function agentRekeyFingerprint(agent: AssistantAgent): string;
@@ -1,3 +1,5 @@
1
+ import b4a from 'b4a';
2
+ import { TERMINAL_STATES } from './assistant/history.js';
1
3
  export function chatKvCacheKey(chatId) {
2
4
  return `chat-${chatId}`;
3
5
  }
@@ -5,6 +7,28 @@ export function chatRepairKvCacheKey(chatId, acceptedRepairs) {
5
7
  const base = chatKvCacheKey(chatId);
6
8
  return acceptedRepairs > 0 ? `${base}/r${acceptedRepairs}` : base;
7
9
  }
10
+ export function agentSwitched(chunks, request, deviceId) {
11
+ const agentOf = new Map();
12
+ let lastHere = null;
13
+ for (const chunk of chunks) {
14
+ if (!chunk.runId || chunk.runId === request.runId) {
15
+ continue;
16
+ }
17
+ if (chunk.type === 'run-request' && !chunk.capability) {
18
+ agentOf.set(chunk.runId, chunk.runRequest?.agentId);
19
+ }
20
+ else if (agentOf.has(chunk.runId) && executedHere(chunk, deviceId)) {
21
+ lastHere = chunk.runId;
22
+ }
23
+ }
24
+ return lastHere !== null && agentOf.get(lastHere) !== request.runRequest?.agentId;
25
+ }
26
+ function executedHere(chunk, deviceId) {
27
+ return (chunk.type === 'run-status' &&
28
+ !TERMINAL_STATES.has(chunk.runStatus?.state ?? '') &&
29
+ !!chunk.originDeviceId &&
30
+ b4a.equals(chunk.originDeviceId, deviceId));
31
+ }
8
32
  export function disappearedChatIds(known, current) {
9
33
  const live = new Set(current);
10
34
  const gone = [];
@@ -14,6 +14,7 @@ export const MESH_SCOPED = new Set([
14
14
  'markChatRead',
15
15
  'setChatAgent',
16
16
  'moveChatToMesh',
17
+ 'forkChat',
17
18
  'chatMoveWatch',
18
19
  'setChatRagEnabled',
19
20
  'setChatWebSearchEnabled',
@@ -89,6 +90,7 @@ const DEVICE_LOCAL = new Set([
89
90
  'setConfig',
90
91
  'setModelPaused',
91
92
  'setModelReleased',
93
+ 'setAutoDownloadModels',
92
94
  'configWatchTyped',
93
95
  'modelDownloadsWatch',
94
96
  'setModelDownloadStatus',
@@ -123,6 +125,8 @@ const DEVICE_LOCAL = new Set([
123
125
  'diagnostics',
124
126
  'diagnosticsWatch',
125
127
  'networkWatch',
128
+ 'setBlindPeering',
129
+ 'blindPeeringWatch',
126
130
  'meshStatusWatch',
127
131
  'listMeshes',
128
132
  'meshesWatch',
@@ -1,5 +1,5 @@
1
1
  import type { Logger } from './log.ts';
2
- export type ModelDownloadPhase = 'queued' | 'downloading' | 'paused' | 'error';
2
+ export type ModelDownloadPhase = 'queued' | 'downloading' | 'paused' | 'error' | 'held';
3
3
  export interface StatusRow {
4
4
  phase: ModelDownloadPhase;
5
5
  percentage: number;
@@ -20,6 +20,9 @@ export interface ImageDownloadObservation {
20
20
  }
21
21
  export type DownloadFrame = ModelProgressFrame;
22
22
  export type DownloadEventStream = AsyncIterable<ModelProgressFrame>;
23
+ export interface CacheWaitOptions {
24
+ readonly onHold?: () => void;
25
+ }
23
26
  interface DownloadHarness {
24
27
  list?(options?: {
25
28
  cachedOnly?: boolean;
@@ -50,14 +53,6 @@ interface DownloadEngine {
50
53
  agentsWatch(request: {
51
54
  meshId: string;
52
55
  }): AsyncIterable<unknown>;
53
- listDevices(request: {
54
- meshId: string;
55
- }): Promise<{
56
- devices?: ReadonlyArray<{
57
- id: Buffer;
58
- capabilityProfile: string;
59
- }> | null;
60
- }>;
61
56
  getAgent(request: {
62
57
  agentId: string;
63
58
  meshId: string;
@@ -141,6 +136,7 @@ export declare class ModelDownloadReconciler {
141
136
  private readonly _observedProgress;
142
137
  private readonly _observedSpeed;
143
138
  private readonly _requestedModels;
139
+ private readonly _heldLoads;
144
140
  private readonly _releasing;
145
141
  private readonly _targetTotals;
146
142
  private _config;
@@ -150,7 +146,6 @@ export declare class ModelDownloadReconciler {
150
146
  private _musicChoice;
151
147
  private _preemptLlm;
152
148
  private _pausedLocalLlm;
153
- private _profile;
154
149
  private _capability;
155
150
  private readonly _handled;
156
151
  private readonly _demands;
@@ -171,10 +166,12 @@ export declare class ModelDownloadReconciler {
171
166
  resume(): void;
172
167
  meshAdopted(): void;
173
168
  yieldToRuns(models: ReadonlyArray<string>): void;
169
+ observeHeldLoad(model: string): void;
170
+ observeDroppedHeldLoad(model: string): void;
174
171
  observeImageProgress(observation: ImageDownloadObservation): void;
175
172
  private _transfer;
176
173
  private _completeTransfer;
177
- waitUntilCached(model: string, signal: AbortSignal): Promise<void>;
174
+ waitUntilCached(model: string, signal: AbortSignal, { onHold }?: CacheWaitOptions): Promise<void>;
178
175
  retry(name: string): Promise<void>;
179
176
  release<T>(name: string, removeFiles: () => Promise<T>): Promise<T>;
180
177
  close(): Promise<void>;
@@ -189,12 +186,14 @@ export declare class ModelDownloadReconciler {
189
186
  private _drain;
190
187
  private _step;
191
188
  private _writeWaiting;
189
+ private _announceHolds;
192
190
  private _flushObservedProgress;
193
191
  private _shouldPreempt;
194
192
  private _waiting;
195
193
  private _nextTarget;
196
194
  private _scheduleNextRetry;
197
195
  private _desired;
196
+ private _heldBack;
198
197
  private _reclaimCached;
199
198
  private _modelAvailability;
200
199
  private _deviceProfile;
@@ -33,6 +33,7 @@ export class ModelDownloadReconciler {
33
33
  _observedProgress = new Map();
34
34
  _observedSpeed = new Map();
35
35
  _requestedModels = new Set();
36
+ _heldLoads = new Set();
36
37
  _releasing = new Set();
37
38
  _targetTotals = new Map();
38
39
  _config = null;
@@ -42,7 +43,6 @@ export class ModelDownloadReconciler {
42
43
  _musicChoice = undefined;
43
44
  _preemptLlm = null;
44
45
  _pausedLocalLlm = null;
45
- _profile = null;
46
46
  _capability = null;
47
47
  _handled = new Set();
48
48
  _demands = new Map();
@@ -128,6 +128,17 @@ export class ModelDownloadReconciler {
128
128
  this._runModels = next;
129
129
  this._kick();
130
130
  }
131
+ observeHeldLoad(model) {
132
+ if (this._closed)
133
+ return;
134
+ this._heldLoads.add(model);
135
+ this._kick();
136
+ }
137
+ observeDroppedHeldLoad(model) {
138
+ if (this._closed || !this._heldLoads.delete(model))
139
+ return;
140
+ this._kick();
141
+ }
131
142
  observeImageProgress(observation) {
132
143
  if (this._closed)
133
144
  return;
@@ -170,7 +181,7 @@ export class ModelDownloadReconciler {
170
181
  this._pendingCompletions.delete(name);
171
182
  this._completedTransfers.add(name);
172
183
  }
173
- async waitUntilCached(model, signal) {
184
+ async waitUntilCached(model, signal, { onHold } = {}) {
174
185
  if (this._closed ||
175
186
  !this._harness.list ||
176
187
  !this._harness.download ||
@@ -195,7 +206,7 @@ export class ModelDownloadReconciler {
195
206
  this._removeDemandWaiter(model, waiter);
196
207
  resolve();
197
208
  };
198
- const waiter = { signal, abort, reject, resolve };
209
+ const waiter = { signal, abort, reject, resolve, onHold: onHold ?? null };
199
210
  const demand = this._demands.get(model) ?? {
200
211
  addon,
201
212
  failures: 0,
@@ -410,7 +421,7 @@ export class ModelDownloadReconciler {
410
421
  if (this._closed || this._paused) {
411
422
  return;
412
423
  }
413
- const downloadable = new Map(targets.filter((t) => !t.paused).map((t) => [t.name, t]));
424
+ const downloadable = new Map(targets.filter((t) => !t.paused && !t.held).map((t) => [t.name, t]));
414
425
  if (this._active && !downloadable.has(this._active.model)) {
415
426
  const stillWanted = targets.some((t) => t.name === this._active?.model);
416
427
  await this._stopActive(!stillWanted);
@@ -427,6 +438,7 @@ export class ModelDownloadReconciler {
427
438
  }
428
439
  const next = this._active ? null : preferred;
429
440
  await this._writeWaiting(targets, next?.name, now);
441
+ this._announceHolds(targets);
430
442
  if (next && !this._closed && !this._paused) {
431
443
  this._begin(next);
432
444
  }
@@ -436,6 +448,9 @@ export class ModelDownloadReconciler {
436
448
  if (target.paused) {
437
449
  await this._status.write(target.name, 'paused', { bytesPerSecond: 0 });
438
450
  }
451
+ else if (target.held) {
452
+ await this._status.write(target.name, 'held', { total: target.total, bytesPerSecond: 0 });
453
+ }
439
454
  else if (this._waiting(target.name, nextName, now)) {
440
455
  await this._status.write(target.name, 'queued', {
441
456
  total: target.total,
@@ -444,6 +459,17 @@ export class ModelDownloadReconciler {
444
459
  }
445
460
  }
446
461
  }
462
+ _announceHolds(targets) {
463
+ for (const target of targets) {
464
+ if (!target.held)
465
+ continue;
466
+ for (const waiter of this._demands.get(target.name)?.waiters ?? []) {
467
+ const onHold = waiter.onHold;
468
+ waiter.onHold = null;
469
+ onHold?.();
470
+ }
471
+ }
472
+ }
447
473
  async _flushObservedProgress() {
448
474
  for (const [model, progress] of this._observedProgress) {
449
475
  await this._transfer(model);
@@ -518,7 +544,11 @@ export class ModelDownloadReconciler {
518
544
  }
519
545
  }
520
546
  async _desired() {
547
+ if (this._config === null) {
548
+ return [];
549
+ }
521
550
  if (this._demands.size === 0 &&
551
+ this._heldLoads.size === 0 &&
522
552
  this._handled.size === 0 &&
523
553
  !this._config?.onboardingCompleted) {
524
554
  await this._status.clearUnwanted(new Set());
@@ -565,6 +595,16 @@ export class ModelDownloadReconciler {
565
595
  }
566
596
  wanted.set(name, { name, addon, paused: paused.has(name) });
567
597
  }
598
+ for (const name of this._heldLoads) {
599
+ if (this._config?.autoDownloadModels !== false || availability.cached.has(name)) {
600
+ this._heldLoads.delete(name);
601
+ continue;
602
+ }
603
+ const addon = availability.addons.get(name);
604
+ if (!wanted.has(name) && addon !== undefined) {
605
+ wanted.set(name, { name, addon, paused: paused.has(name) });
606
+ }
607
+ }
568
608
  const wantedNames = new Set(wanted.keys());
569
609
  for (const name of this._attemptGenerations.keys()) {
570
610
  if (!wantedNames.has(name)) {
@@ -583,6 +623,7 @@ export class ModelDownloadReconciler {
583
623
  }
584
624
  pending.push({
585
625
  ...model,
626
+ held: this._heldBack(model.name),
586
627
  total: availability.totals.get(model.name) ?? 0
587
628
  });
588
629
  }
@@ -599,6 +640,9 @@ export class ModelDownloadReconciler {
599
640
  }
600
641
  return pending;
601
642
  }
643
+ _heldBack(name) {
644
+ return this._config?.autoDownloadModels === false && !this._requestedModels.has(name);
645
+ }
602
646
  async _reclaimCached(cached) {
603
647
  for (const name of this._config?.releasedModels ?? []) {
604
648
  if (!cached.has(name) || this._releasing.has(name))
@@ -619,18 +663,7 @@ export class ModelDownloadReconciler {
619
663
  return { cached, totals, addons };
620
664
  }
621
665
  async _deviceProfile() {
622
- if (this._profile)
623
- return this._profile;
624
- const meshId = this._meshId();
625
- if (!meshId) {
626
- return this._profile ?? 'mid';
627
- }
628
- const { devices } = await this._engine.listDevices({ meshId }).catch(() => ({ devices: [] }));
629
- const self = (devices ?? []).find((device) => b4a.equals(device.id, this._deviceId));
630
- if (self) {
631
- this._profile = self.capabilityProfile;
632
- }
633
- return this._profile ?? 'mid';
666
+ return (await this._hostCapability())?.capabilityProfile ?? 'low';
634
667
  }
635
668
  async _hostCapability() {
636
669
  if (this._capability)
@@ -0,0 +1,29 @@
1
+ import type { Harness } from './assistant.ts';
2
+ import type { openLocalStore } from './core/local/index.ts';
3
+ type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
4
+ interface Logger {
5
+ warn(...args: unknown[]): void;
6
+ }
7
+ export type ModelMemoryTarget = Pick<Harness, 'setModelMemoryCap' | 'capability' | 'memoryUsage'>;
8
+ export type ModelMemorySource = Pick<LocalStore, 'get' | 'watch' | 'unwatch'>;
9
+ export interface ModelMemorySummary {
10
+ readonly usableBytes: number;
11
+ readonly capPercent: number;
12
+ readonly capBytes: number;
13
+ }
14
+ export interface ModelMemoryUse extends ModelMemorySummary {
15
+ readonly usedBytes: number;
16
+ readonly overCap: boolean;
17
+ }
18
+ export interface ModelMemoryCap {
19
+ summary(): Promise<ModelMemoryUse | null>;
20
+ stop(): Promise<void>;
21
+ }
22
+ export interface ModelMemoryCapOptions {
23
+ logger?: Logger | null;
24
+ onPushed?: () => void;
25
+ }
26
+ export declare function startModelMemoryCap(harness: ModelMemoryTarget, state: ModelMemorySource, { logger, onPushed }?: ModelMemoryCapOptions): ModelMemoryCap;
27
+ export declare function withUsage(cap: ModelMemorySummary, usedBytes: number): ModelMemoryUse;
28
+ export declare function memoryCapPercent(value: unknown): number;
29
+ export {};
@@ -0,0 +1,69 @@
1
+ import ScopeLock from 'scope-lock';
2
+ import { MODEL_MEMORY_CAP_DEFAULT, MODEL_MEMORY_CAP_KEY, MODEL_MEMORY_CAP_MAX, MODEL_MEMORY_CAP_MIN } from './constants.js';
3
+ import { errorMessage } from './guards.js';
4
+ export function startModelMemoryCap(harness, state, { logger, onPushed } = {}) {
5
+ const lock = new ScopeLock({ debounce: true });
6
+ let disposed = false;
7
+ let pushed = null;
8
+ let usableBytes = null;
9
+ const push = async () => {
10
+ const percent = memoryCapPercent(await state.get(MODEL_MEMORY_CAP_KEY));
11
+ if (disposed || percent === pushed || !harness.setModelMemoryCap) {
12
+ return;
13
+ }
14
+ await harness.setModelMemoryCap(percent === MODEL_MEMORY_CAP_MIN ? 0 : percent);
15
+ usableBytes ??= (await harness.capability?.())?.memoryBudget?.availableBytes ?? null;
16
+ pushed = percent;
17
+ onPushed?.();
18
+ };
19
+ const schedule = async () => {
20
+ if (disposed || !(await lock.lock()))
21
+ return;
22
+ try {
23
+ await push();
24
+ }
25
+ catch (err) {
26
+ logger?.warn('model-memory-cap', 'push failed', errorMessage(err));
27
+ }
28
+ finally {
29
+ lock.unlock();
30
+ }
31
+ };
32
+ const listener = () => void schedule();
33
+ state.watch(listener);
34
+ void schedule();
35
+ return {
36
+ summary: async () => {
37
+ const capPercent = pushed;
38
+ const usable = usableBytes;
39
+ if (capPercent === null || usable === null || !harness.memoryUsage) {
40
+ return null;
41
+ }
42
+ const usage = await harness.memoryUsage().catch((err) => {
43
+ logger?.warn('model-memory-cap', 'usage read failed', errorMessage(err));
44
+ return null;
45
+ });
46
+ const capBytes = Math.round((capPercent / 100) * usable);
47
+ return usage && withUsage({ usableBytes: usable, capPercent, capBytes }, usage.usedBytes);
48
+ },
49
+ stop: async () => {
50
+ disposed = true;
51
+ state.unwatch(listener);
52
+ lock.destroy();
53
+ await lock.flush();
54
+ }
55
+ };
56
+ }
57
+ export function withUsage(cap, usedBytes) {
58
+ return {
59
+ ...cap,
60
+ usedBytes,
61
+ overCap: cap.capPercent > MODEL_MEMORY_CAP_MIN && usedBytes > cap.capBytes
62
+ };
63
+ }
64
+ export function memoryCapPercent(value) {
65
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
66
+ return MODEL_MEMORY_CAP_DEFAULT;
67
+ }
68
+ return Math.min(MODEL_MEMORY_CAP_MAX, Math.max(MODEL_MEMORY_CAP_MIN, Math.round(value)));
69
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AssistantModelCatalogRevision, AssistantModelInfo, AssistantModelInfoList } from '../spec/hyperschema/types.d.ts';
2
2
  type Catalog = () => Promise<AsyncIterable<AssistantModelCatalogRevision> | null>;
3
- type Read = () => Promise<AssistantModelInfo[] | null>;
3
+ type Read = () => Promise<AssistantModelInfoList | null>;
4
4
  export declare function watchModels(catalog: Catalog, read: Read): AsyncIterable<AssistantModelInfoList>;
5
5
  export declare function asEncoded(models: AssistantModelInfo[]): AssistantModelInfo[];
6
6
  export {};
@@ -15,12 +15,12 @@ class ModelsWatch {
15
15
  this._opening ??= this._open();
16
16
  const revisions = await this._opening;
17
17
  while (revisions && !this._closed && !(await revisions.next()).done) {
18
- const models = this._closed ? null : await this._read();
19
- if (models === null || this._closed || snapshotEquals(models, this._last)) {
18
+ const list = this._closed ? null : await this._read();
19
+ if (list === null || this._closed || snapshotEquals(list, this._last)) {
20
20
  continue;
21
21
  }
22
- this._last = models;
23
- return { value: { models }, done: false };
22
+ this._last = list;
23
+ return { value: list, done: false };
24
24
  }
25
25
  return { value: undefined, done: true };
26
26
  }
@@ -1,4 +1,4 @@
1
- import { abortOAuthLoopback, openOAuthLoopback } from '#oauth-loopback';
1
+ import { abortOAuthLoopback, openOAuthLoopback } from './loopback.js';
2
2
  import { randomBase64Url, sha256Base64Url } from '@qvac/core/oauth/runtime/crypto';
3
3
  import defaultFetch from '@qvac/core/oauth/runtime/fetch';
4
4
  import { runOAuthFlowWithRuntime } from './flow-core.js';
@@ -26,7 +26,12 @@ export async function openOAuthLoopback(port, target) {
26
26
  if (loopback.waiters.has(target.state)) {
27
27
  throw new Error(`OAuth loopback on port ${port} already has a flow on state ${target.state}`);
28
28
  }
29
- const waiter = { credentialKey: target.credentialKey, deliver: null, settled: null };
29
+ const waiter = {
30
+ credentialKey: target.credentialKey,
31
+ returnTo: target.returnTo,
32
+ deliver: null,
33
+ settled: null
34
+ };
30
35
  loopback.waiters.set(target.state, waiter);
31
36
  let bound;
32
37
  try {
@@ -120,7 +125,12 @@ function listen(port, waiters) {
120
125
  respond(response, 200, { contentType: 'text/html; charset=utf-8', body: STALE_PAGE });
121
126
  return;
122
127
  }
123
- respond(response, 200, { contentType: 'text/html; charset=utf-8', body: DONE_PAGE });
128
+ if (waiter.returnTo) {
129
+ redirect(response, waiter.returnTo);
130
+ }
131
+ else {
132
+ respond(response, 200, { contentType: 'text/html; charset=utf-8', body: DONE_PAGE });
133
+ }
124
134
  settle(waiter, callbackSettlement(url.searchParams, state));
125
135
  });
126
136
  let listening = false;
@@ -170,3 +180,7 @@ function respond(response, status, { contentType, body }) {
170
180
  response.writeHead(status, { 'Content-Type': contentType, Connection: 'close' });
171
181
  response.end(body);
172
182
  }
183
+ function redirect(response, location) {
184
+ response.writeHead(302, { Location: location, Connection: 'close' });
185
+ response.end();
186
+ }
@@ -5,6 +5,7 @@ export interface OAuthCallbackResult {
5
5
  export interface OAuthLoopbackTarget {
6
6
  readonly credentialKey: string;
7
7
  readonly state: string;
8
+ readonly returnTo?: string;
8
9
  }
9
10
  export interface OAuthLoopbackServer {
10
11
  readonly redirectUri: string;
@@ -0,0 +1,26 @@
1
+ import type { ChatMessage } from './assistant/message.ts';
2
+ export declare const LOCAL_HOLDER = "local";
3
+ export interface ReplayChat {
4
+ readonly holder: string;
5
+ readonly chatId: string;
6
+ }
7
+ export declare class RunReplayStore {
8
+ private readonly _dir;
9
+ constructor(dir?: string);
10
+ started(): Promise<boolean>;
11
+ start(): Promise<void>;
12
+ load({ holder, chatId }: ReplayChat): Promise<Map<string, ChatMessage[]>>;
13
+ save({ holder, chatId }: ReplayChat, runId: string, messages: ChatMessage[]): Promise<void>;
14
+ forget(chatId: string, holder?: string): Promise<void>;
15
+ clear(holder: string): Promise<void>;
16
+ private _chatDir;
17
+ private _holders;
18
+ }
19
+ export interface SentRunTracker {
20
+ readonly used: boolean;
21
+ observe(call: Record<string, unknown>): void;
22
+ turns(inputLength: number): ChatMessage[] | null;
23
+ }
24
+ export declare function trackSentRun(cacheKey: string | undefined): SentRunTracker;
25
+ export declare function appendedTurns(sent: unknown, inputLength: number): ChatMessage[] | null;
26
+ export declare function defaultRunReplayDir(): string;