@qvac/core 0.6.7 → 0.7.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +145 -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/assistant/fake-models.d.ts +22 -0
  6. package/dist-lib/lib/assistant/fake-models.js +112 -0
  7. package/dist-lib/lib/assistant/harness.d.ts +10 -0
  8. package/dist-lib/lib/assistant/harness.js +26 -0
  9. package/dist-lib/lib/assistant/history.d.ts +5 -1
  10. package/dist-lib/lib/assistant/history.js +14 -2
  11. package/dist-lib/lib/assistant-tree.d.ts +4 -0
  12. package/dist-lib/lib/assistant-tree.js +45 -7
  13. package/dist-lib/lib/assistant.d.ts +15 -2
  14. package/dist-lib/lib/assistant.js +175 -41
  15. package/dist-lib/lib/constants.d.ts +6 -0
  16. package/dist-lib/lib/constants.js +6 -0
  17. package/dist-lib/lib/core/chat-activity.d.ts +1 -1
  18. package/dist-lib/lib/core/chat-activity.js +3 -0
  19. package/dist-lib/lib/core/collections.d.ts +1 -0
  20. package/dist-lib/lib/core/collections.js +1 -0
  21. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  22. package/dist-lib/lib/core/contract/compute.js +2 -1
  23. package/dist-lib/lib/core/contract/index.js +6 -1
  24. package/dist-lib/lib/core/contract/invites.d.ts +1 -11
  25. package/dist-lib/lib/core/contract/invites.js +3 -3
  26. package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
  27. package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
  28. package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
  29. package/dist-lib/lib/core/contract/open-join.js +57 -0
  30. package/dist-lib/lib/core/departures.d.ts +3 -0
  31. package/dist-lib/lib/core/departures.js +44 -1
  32. package/dist-lib/lib/core/identity.d.ts +1 -1
  33. package/dist-lib/lib/core/identity.js +6 -4
  34. package/dist-lib/lib/core/invite.d.ts +2 -0
  35. package/dist-lib/lib/core/invite.js +6 -2
  36. package/dist-lib/lib/core/membership.d.ts +1 -12
  37. package/dist-lib/lib/core/membership.js +1 -94
  38. package/dist-lib/lib/core/mesh.d.ts +8 -13
  39. package/dist-lib/lib/core/mesh.js +41 -87
  40. package/dist-lib/lib/core/sync/index.js +2 -16
  41. package/dist-lib/lib/core.d.ts +30 -49
  42. package/dist-lib/lib/core.js +341 -868
  43. package/dist-lib/lib/diagnostics.d.ts +7 -0
  44. package/dist-lib/lib/diagnostics.js +22 -0
  45. package/dist-lib/lib/doctor/checks.d.ts +15 -0
  46. package/dist-lib/lib/doctor/checks.js +269 -0
  47. package/dist-lib/lib/doctor/redact.d.ts +4 -0
  48. package/dist-lib/lib/doctor/redact.js +31 -0
  49. package/dist-lib/lib/doctor/render.d.ts +8 -0
  50. package/dist-lib/lib/doctor/render.js +83 -0
  51. package/dist-lib/lib/doctor/tree.d.ts +5 -0
  52. package/dist-lib/lib/doctor/tree.js +167 -0
  53. package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
  54. package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
  55. package/dist-lib/lib/guards.d.ts +1 -0
  56. package/dist-lib/lib/guards.js +4 -0
  57. package/dist-lib/lib/inference-calls.js +1 -5
  58. package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
  59. package/dist-lib/lib/kv-cache-reclaim.js +24 -0
  60. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  61. package/dist-lib/lib/model-download-status.d.ts +1 -1
  62. package/dist-lib/lib/model-download.d.ts +9 -1
  63. package/dist-lib/lib/model-download.js +48 -3
  64. package/dist-lib/lib/model-memory-cap.d.ts +29 -0
  65. package/dist-lib/lib/model-memory-cap.js +69 -0
  66. package/dist-lib/lib/models-watch.d.ts +1 -1
  67. package/dist-lib/lib/models-watch.js +4 -4
  68. package/dist-lib/lib/oauth/flow.js +1 -1
  69. package/dist-lib/lib/oauth/loopback.js +16 -2
  70. package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
  71. package/dist-lib/lib/run-replay.d.ts +26 -0
  72. package/dist-lib/lib/run-replay.js +107 -0
  73. package/dist-lib/schema/config.d.ts +1 -1
  74. package/dist-lib/schema/config.js +1 -1
  75. package/dist-lib/schema/content.js +9 -1
  76. package/dist-lib/schema/engine.js +30 -5
  77. package/dist-lib/schema/local.js +3 -2
  78. package/dist-lib/schema/qvac.js +18 -1
  79. package/dist-lib/spec/bind.js +3 -0
  80. package/dist-lib/spec/calls.js +1 -0
  81. package/dist-lib/spec/capabilities.d.ts +2 -0
  82. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  83. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  84. package/dist-lib/spec/hrpc/index.js +15 -3
  85. package/dist-lib/spec/hrpc/messages.js +646 -557
  86. package/dist-lib/spec/hyperschema/index.js +646 -557
  87. package/dist-lib/spec/hyperschema/schema.json +80 -0
  88. package/dist-lib/spec/hyperschema/types.d.ts +20 -3
  89. package/dist-lib/spec/local/hyperdb/messages.js +22 -10
  90. package/dist-lib/spec/local/hyperschema/index.js +16 -7
  91. package/dist-lib/spec/local/hyperschema/schema.json +10 -1
  92. package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
  93. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  94. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  95. package/dist-lib/spec/mesh/hyperdb/messages.js +477 -240
  96. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
  97. package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
  98. package/dist-lib/spec/mesh/hyperdispatch/messages.js +180 -7
  99. package/dist-lib/spec/mesh/hyperschema/index.js +180 -7
  100. package/dist-lib/spec/mesh/hyperschema/schema.json +98 -1
  101. package/dist-lib/spec/mesh/hyperschema/types.d.ts +26 -2
  102. package/package.json +8 -16
  103. package/tui/README.md +7 -1
  104. package/tui/app.mjs +245 -11
  105. package/tui/boot.mjs +1 -0
  106. package/tui/index.mjs +1 -0
  107. package/tui/lib/render.mjs +25 -1
  108. package/worker/assistant-sidecar.mjs +5 -2
  109. package/dist-lib/lib/core/device-invites.d.ts +0 -11
  110. package/dist-lib/lib/core/device-invites.js +0 -63
  111. package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
  112. package/dist-lib/lib/core/grant-expiry.js +0 -66
  113. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
  114. package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
  115. package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
  116. package/dist-lib/lib/oauth/loopback.stub.js +0 -6
@@ -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 = [];
@@ -89,6 +89,7 @@ const DEVICE_LOCAL = new Set([
89
89
  'setConfig',
90
90
  'setModelPaused',
91
91
  'setModelReleased',
92
+ 'setAutoDownloadModels',
92
93
  'configWatchTyped',
93
94
  'modelDownloadsWatch',
94
95
  'setModelDownloadStatus',
@@ -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;
@@ -141,6 +144,7 @@ export declare class ModelDownloadReconciler {
141
144
  private readonly _observedProgress;
142
145
  private readonly _observedSpeed;
143
146
  private readonly _requestedModels;
147
+ private readonly _heldLoads;
144
148
  private readonly _releasing;
145
149
  private readonly _targetTotals;
146
150
  private _config;
@@ -171,10 +175,12 @@ export declare class ModelDownloadReconciler {
171
175
  resume(): void;
172
176
  meshAdopted(): void;
173
177
  yieldToRuns(models: ReadonlyArray<string>): void;
178
+ observeHeldLoad(model: string): void;
179
+ observeDroppedHeldLoad(model: string): void;
174
180
  observeImageProgress(observation: ImageDownloadObservation): void;
175
181
  private _transfer;
176
182
  private _completeTransfer;
177
- waitUntilCached(model: string, signal: AbortSignal): Promise<void>;
183
+ waitUntilCached(model: string, signal: AbortSignal, { onHold }?: CacheWaitOptions): Promise<void>;
178
184
  retry(name: string): Promise<void>;
179
185
  release<T>(name: string, removeFiles: () => Promise<T>): Promise<T>;
180
186
  close(): Promise<void>;
@@ -189,12 +195,14 @@ export declare class ModelDownloadReconciler {
189
195
  private _drain;
190
196
  private _step;
191
197
  private _writeWaiting;
198
+ private _announceHolds;
192
199
  private _flushObservedProgress;
193
200
  private _shouldPreempt;
194
201
  private _waiting;
195
202
  private _nextTarget;
196
203
  private _scheduleNextRetry;
197
204
  private _desired;
205
+ private _heldBack;
198
206
  private _reclaimCached;
199
207
  private _modelAvailability;
200
208
  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;
@@ -128,6 +129,17 @@ export class ModelDownloadReconciler {
128
129
  this._runModels = next;
129
130
  this._kick();
130
131
  }
132
+ observeHeldLoad(model) {
133
+ if (this._closed)
134
+ return;
135
+ this._heldLoads.add(model);
136
+ this._kick();
137
+ }
138
+ observeDroppedHeldLoad(model) {
139
+ if (this._closed || !this._heldLoads.delete(model))
140
+ return;
141
+ this._kick();
142
+ }
131
143
  observeImageProgress(observation) {
132
144
  if (this._closed)
133
145
  return;
@@ -170,7 +182,7 @@ export class ModelDownloadReconciler {
170
182
  this._pendingCompletions.delete(name);
171
183
  this._completedTransfers.add(name);
172
184
  }
173
- async waitUntilCached(model, signal) {
185
+ async waitUntilCached(model, signal, { onHold } = {}) {
174
186
  if (this._closed ||
175
187
  !this._harness.list ||
176
188
  !this._harness.download ||
@@ -195,7 +207,7 @@ export class ModelDownloadReconciler {
195
207
  this._removeDemandWaiter(model, waiter);
196
208
  resolve();
197
209
  };
198
- const waiter = { signal, abort, reject, resolve };
210
+ const waiter = { signal, abort, reject, resolve, onHold: onHold ?? null };
199
211
  const demand = this._demands.get(model) ?? {
200
212
  addon,
201
213
  failures: 0,
@@ -410,7 +422,7 @@ export class ModelDownloadReconciler {
410
422
  if (this._closed || this._paused) {
411
423
  return;
412
424
  }
413
- const downloadable = new Map(targets.filter((t) => !t.paused).map((t) => [t.name, t]));
425
+ const downloadable = new Map(targets.filter((t) => !t.paused && !t.held).map((t) => [t.name, t]));
414
426
  if (this._active && !downloadable.has(this._active.model)) {
415
427
  const stillWanted = targets.some((t) => t.name === this._active?.model);
416
428
  await this._stopActive(!stillWanted);
@@ -427,6 +439,7 @@ export class ModelDownloadReconciler {
427
439
  }
428
440
  const next = this._active ? null : preferred;
429
441
  await this._writeWaiting(targets, next?.name, now);
442
+ this._announceHolds(targets);
430
443
  if (next && !this._closed && !this._paused) {
431
444
  this._begin(next);
432
445
  }
@@ -436,6 +449,9 @@ export class ModelDownloadReconciler {
436
449
  if (target.paused) {
437
450
  await this._status.write(target.name, 'paused', { bytesPerSecond: 0 });
438
451
  }
452
+ else if (target.held) {
453
+ await this._status.write(target.name, 'held', { total: target.total, bytesPerSecond: 0 });
454
+ }
439
455
  else if (this._waiting(target.name, nextName, now)) {
440
456
  await this._status.write(target.name, 'queued', {
441
457
  total: target.total,
@@ -444,6 +460,17 @@ export class ModelDownloadReconciler {
444
460
  }
445
461
  }
446
462
  }
463
+ _announceHolds(targets) {
464
+ for (const target of targets) {
465
+ if (!target.held)
466
+ continue;
467
+ for (const waiter of this._demands.get(target.name)?.waiters ?? []) {
468
+ const onHold = waiter.onHold;
469
+ waiter.onHold = null;
470
+ onHold?.();
471
+ }
472
+ }
473
+ }
447
474
  async _flushObservedProgress() {
448
475
  for (const [model, progress] of this._observedProgress) {
449
476
  await this._transfer(model);
@@ -518,7 +545,11 @@ export class ModelDownloadReconciler {
518
545
  }
519
546
  }
520
547
  async _desired() {
548
+ if (this._config === null) {
549
+ return [];
550
+ }
521
551
  if (this._demands.size === 0 &&
552
+ this._heldLoads.size === 0 &&
522
553
  this._handled.size === 0 &&
523
554
  !this._config?.onboardingCompleted) {
524
555
  await this._status.clearUnwanted(new Set());
@@ -565,6 +596,16 @@ export class ModelDownloadReconciler {
565
596
  }
566
597
  wanted.set(name, { name, addon, paused: paused.has(name) });
567
598
  }
599
+ for (const name of this._heldLoads) {
600
+ if (this._config?.autoDownloadModels !== false || availability.cached.has(name)) {
601
+ this._heldLoads.delete(name);
602
+ continue;
603
+ }
604
+ const addon = availability.addons.get(name);
605
+ if (!wanted.has(name) && addon !== undefined) {
606
+ wanted.set(name, { name, addon, paused: paused.has(name) });
607
+ }
608
+ }
568
609
  const wantedNames = new Set(wanted.keys());
569
610
  for (const name of this._attemptGenerations.keys()) {
570
611
  if (!wantedNames.has(name)) {
@@ -583,6 +624,7 @@ export class ModelDownloadReconciler {
583
624
  }
584
625
  pending.push({
585
626
  ...model,
627
+ held: this._heldBack(model.name),
586
628
  total: availability.totals.get(model.name) ?? 0
587
629
  });
588
630
  }
@@ -599,6 +641,9 @@ export class ModelDownloadReconciler {
599
641
  }
600
642
  return pending;
601
643
  }
644
+ _heldBack(name) {
645
+ return this._config?.autoDownloadModels === false && !this._requestedModels.has(name);
646
+ }
602
647
  async _reclaimCached(cached) {
603
648
  for (const name of this._config?.releasedModels ?? []) {
604
649
  if (!cached.has(name) || this._releasing.has(name))
@@ -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;
@@ -0,0 +1,107 @@
1
+ import fs from 'fs';
2
+ import { join } from 'path';
3
+ import { persistent } from 'bare-storage';
4
+ import { isRecord, safeName } from './guards.js';
5
+ export const LOCAL_HOLDER = 'local';
6
+ export class RunReplayStore {
7
+ _dir;
8
+ constructor(dir = defaultRunReplayDir()) {
9
+ this._dir = dir;
10
+ }
11
+ async started() {
12
+ try {
13
+ await fs.promises.stat(this._dir);
14
+ return true;
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
20
+ async start() {
21
+ await fs.promises.mkdir(this._dir, { recursive: true });
22
+ }
23
+ async load({ holder, chatId }) {
24
+ const chatDir = this._chatDir(holder, chatId);
25
+ const runs = new Map();
26
+ let names;
27
+ try {
28
+ names = await fs.promises.readdir(chatDir);
29
+ }
30
+ catch {
31
+ return runs;
32
+ }
33
+ for (const name of names) {
34
+ if (!name.endsWith('.json'))
35
+ continue;
36
+ const parsed = JSON.parse(await fs.promises.readFile(join(chatDir, name), 'utf8'));
37
+ if (isRecord(parsed) && typeof parsed.runId === 'string' && Array.isArray(parsed.messages)) {
38
+ runs.set(parsed.runId, parsed.messages.filter(isChatMessage));
39
+ }
40
+ }
41
+ return runs;
42
+ }
43
+ async save({ holder, chatId }, runId, messages) {
44
+ const chatDir = this._chatDir(holder, chatId);
45
+ await fs.promises.mkdir(chatDir, { recursive: true });
46
+ await fs.promises.writeFile(join(chatDir, `${safeName(runId)}.json`), JSON.stringify({ runId, messages }));
47
+ }
48
+ async forget(chatId, holder) {
49
+ const holders = holder === undefined ? await this._holders() : [holder];
50
+ for (const each of holders) {
51
+ await fs.promises.rm(this._chatDir(each, chatId), { recursive: true, force: true });
52
+ }
53
+ }
54
+ async clear(holder) {
55
+ await fs.promises.rm(join(this._dir, safeName(holder)), { recursive: true, force: true });
56
+ }
57
+ _chatDir(holder, chatId) {
58
+ return join(this._dir, safeName(holder), safeName(chatId));
59
+ }
60
+ async _holders() {
61
+ try {
62
+ return await fs.promises.readdir(this._dir);
63
+ }
64
+ catch {
65
+ return [];
66
+ }
67
+ }
68
+ }
69
+ export function trackSentRun(cacheKey) {
70
+ let used = false;
71
+ let last = null;
72
+ return {
73
+ get used() {
74
+ return used;
75
+ },
76
+ observe(call) {
77
+ if (cacheKey && call.cacheKey === cacheKey) {
78
+ used = true;
79
+ last = call.droppedTurns ? null : call.messages;
80
+ }
81
+ else if (used) {
82
+ last = null;
83
+ }
84
+ },
85
+ turns: (inputLength) => appendedTurns(last, inputLength)
86
+ };
87
+ }
88
+ export function appendedTurns(sent, inputLength) {
89
+ if (!Array.isArray(sent))
90
+ return null;
91
+ const offset = (isRecord(sent[0]) && sent[0].role === 'system' ? 1 : 0) + inputLength;
92
+ if (sent.length < offset) {
93
+ return null;
94
+ }
95
+ const turns = sent
96
+ .slice(offset)
97
+ .map((turn) => (isRecord(turn) ? { role: turn.role, content: turn.content } : null));
98
+ return turns.every(isChatMessage) ? turns : null;
99
+ }
100
+ export function defaultRunReplayDir() {
101
+ return join(persistent(), 'qvac', 'run-replay');
102
+ }
103
+ function isChatMessage(value) {
104
+ return (isRecord(value) &&
105
+ (value.role === 'user' || value.role === 'assistant' || value.role === 'tool') &&
106
+ typeof value.content === 'string');
107
+ }
@@ -1,3 +1,3 @@
1
1
  import type { Registry } from './content.ts';
2
2
  export declare function registerConfigTypes(ns: Registry): void;
3
- export declare const VENDORED_MIN_SUPPORTED_VERSION = ">=0.19.0";
3
+ export declare const VENDORED_MIN_SUPPORTED_VERSION = ">=0.19.1";
@@ -4,4 +4,4 @@ export function registerConfigTypes(ns) {
4
4
  fields: [{ name: 'minSupportedVersion', type: 'string', required: true }]
5
5
  });
6
6
  }
7
- export const VENDORED_MIN_SUPPORTED_VERSION = '>=0.19.0';
7
+ export const VENDORED_MIN_SUPPORTED_VERSION = '>=0.19.1';
@@ -53,7 +53,15 @@ export function registerContentTypes(ns, namespace) {
53
53
  });
54
54
  ns.register({
55
55
  name: 'progress-phase',
56
- enum: ['loading', 'downloading', 'queued', 'generating', 'transcribing', 'compacting'],
56
+ enum: [
57
+ 'loading',
58
+ 'downloading',
59
+ 'queued',
60
+ 'generating',
61
+ 'transcribing',
62
+ 'compacting',
63
+ 'awaiting-download'
64
+ ],
57
65
  strings: true
58
66
  });
59
67
  ns.register({