tryharness 0.7.45 → 0.7.54

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 (125) hide show
  1. package/README.md +239 -14
  2. package/assets/dsh-plugin.mjs +72 -0
  3. package/assets/opencode-plugin.js +4 -3
  4. package/cli.mjs +497 -196
  5. package/lib/actorRoster.mjs +28 -5
  6. package/lib/adapters.mjs +165 -222
  7. package/lib/agentRuntimeConfig.mjs +36 -0
  8. package/lib/agentRuntimes.mjs +570 -0
  9. package/lib/agentSignIn.mjs +226 -0
  10. package/lib/atlas.mjs +7 -2
  11. package/lib/audit.mjs +643 -0
  12. package/lib/backupSetup.mjs +141 -0
  13. package/lib/boardAccess.mjs +8 -1
  14. package/lib/boardBackup.mjs +283 -0
  15. package/lib/boardCommands.mjs +5 -10
  16. package/lib/boardFileCommands.mjs +2 -1
  17. package/lib/boardMirror.mjs +523 -0
  18. package/lib/boardProtocol.json +263 -6
  19. package/lib/boardTools.mjs +2 -6
  20. package/lib/boardWaits.mjs +102 -0
  21. package/lib/book.mjs +659 -0
  22. package/lib/bridge.mjs +26 -4
  23. package/lib/browserWake.mjs +60 -0
  24. package/lib/cards.mjs +159 -16
  25. package/lib/cloudBoot.mjs +124 -0
  26. package/lib/commands.mjs +25 -5
  27. package/lib/connectionTools.d.ts +13 -0
  28. package/lib/connectionTools.js +45 -0
  29. package/lib/connectorAuth.mjs +309 -0
  30. package/lib/connectorCatalog.json +41 -0
  31. package/lib/connectorCommands.mjs +121 -0
  32. package/lib/connectorGoogle.mjs +513 -0
  33. package/lib/connectors.mjs +805 -0
  34. package/lib/context.mjs +18 -4
  35. package/lib/contractGate.mjs +377 -0
  36. package/lib/coordinator.mjs +152 -39
  37. package/lib/coreModels.mjs +78 -0
  38. package/lib/daemon.mjs +973 -54
  39. package/lib/desk.mjs +239 -0
  40. package/lib/desktop.mjs +113 -8
  41. package/lib/digest.mjs +32 -0
  42. package/lib/directLink.mjs +283 -0
  43. package/lib/environments.mjs +825 -0
  44. package/lib/folders.mjs +4 -4
  45. package/lib/gitHost.mjs +174 -0
  46. package/lib/goalCommands.mjs +58 -14
  47. package/lib/hookDialects.mjs +298 -0
  48. package/lib/inference.mjs +263 -152
  49. package/lib/landed.mjs +887 -0
  50. package/lib/lessons.mjs +6 -0
  51. package/lib/lineup.mjs +324 -0
  52. package/lib/live.mjs +284 -13
  53. package/lib/localmodels.mjs +10 -0
  54. package/lib/machineIdentity.mjs +3 -0
  55. package/lib/machineRoutes.mjs +40 -2
  56. package/lib/machineRuns.mjs +30 -3
  57. package/lib/machineSeal.mjs +69 -10
  58. package/lib/mcp-stdio.mjs +189 -120
  59. package/lib/mcpRelay.mjs +119 -0
  60. package/lib/members.json +26 -0
  61. package/lib/members.mjs +33 -0
  62. package/lib/modelMemory.mjs +100 -0
  63. package/lib/models.mjs +48 -15
  64. package/lib/museSession.mjs +122 -0
  65. package/lib/nativeHost.mjs +203 -0
  66. package/lib/neovim.mjs +126 -0
  67. package/lib/network.mjs +7 -2
  68. package/lib/nudge.mjs +57 -18
  69. package/lib/onboard.mjs +430 -1
  70. package/lib/oneclawLocal.mjs +150 -0
  71. package/lib/ownMachines.mjs +828 -39
  72. package/lib/pairing.mjs +182 -0
  73. package/lib/paths.mjs +8 -0
  74. package/lib/personModel.mjs +89 -0
  75. package/lib/phones.mjs +119 -3
  76. package/lib/placeCommands.mjs +27 -0
  77. package/lib/places.mjs +337 -9
  78. package/lib/plans.mjs +151 -27
  79. package/lib/profile.mjs +152 -0
  80. package/lib/projects.mjs +624 -137
  81. package/lib/readiness.mjs +400 -0
  82. package/lib/record.mjs +115 -0
  83. package/lib/repoLinks.mjs +354 -0
  84. package/lib/roomChannels.mjs +641 -0
  85. package/lib/runBrief.mjs +32 -2
  86. package/lib/runRequests.mjs +157 -0
  87. package/lib/runWorker.mjs +37 -9
  88. package/lib/runner.mjs +74 -11
  89. package/lib/secretPatterns.json +55 -0
  90. package/lib/secrets.mjs +10 -27
  91. package/lib/seen.mjs +102 -0
  92. package/lib/service.mjs +11 -6
  93. package/lib/sessionControl.mjs +386 -96
  94. package/lib/sessionIndex.mjs +5 -3
  95. package/lib/specialties.json +74 -0
  96. package/lib/specialties.mjs +477 -0
  97. package/lib/specialtyKeys.d.ts +3 -0
  98. package/lib/specialtyKeys.js +58 -0
  99. package/lib/specialtyNotice.mjs +147 -0
  100. package/lib/specialtySnapshot.mjs +138 -0
  101. package/lib/store.mjs +829 -29
  102. package/lib/subagents.mjs +8 -4
  103. package/lib/swarm.mjs +483 -37
  104. package/lib/swarmMemory.mjs +947 -0
  105. package/lib/t3.mjs +56 -17
  106. package/lib/taskCommands.mjs +91 -13
  107. package/lib/taskRuns.mjs +68 -11
  108. package/lib/team.json +253 -144
  109. package/lib/team.mjs +538 -147
  110. package/lib/threadLine.mjs +116 -0
  111. package/lib/threadRuns.mjs +78 -12
  112. package/lib/threadView.mjs +66 -16
  113. package/lib/tokenizer.mjs +405 -0
  114. package/lib/ui.mjs +16 -2
  115. package/lib/usageTotals.mjs +148 -0
  116. package/lib/vault.mjs +1144 -0
  117. package/lib/vaultCloud.mjs +117 -0
  118. package/lib/vaultCommands.mjs +201 -0
  119. package/lib/vaultTools.d.ts +12 -0
  120. package/lib/vaultTools.js +42 -0
  121. package/lib/windowAsks.mjs +42 -0
  122. package/lib/workFocus.mjs +59 -0
  123. package/lib/workLinks.mjs +90 -0
  124. package/lib/worktrees.mjs +51 -13
  125. package/package.json +10 -5
@@ -1,7 +1,10 @@
1
1
  import os from "node:os";
2
2
  import { spawn } from "node:child_process";
3
+ import { roundFields } from "./cards.mjs";
3
4
  import { MACHINE_RUNNERS, MEMBERS, TAGGED_ONLY, nameOf } from "./members.mjs";
4
5
  import { LEAD_TIMES } from "./schedules.mjs";
6
+ import { SPECIALTIES, pinnedFor, specialtyName } from "./specialties.mjs";
7
+ import { TEAM } from "./team.mjs";
5
8
 
6
9
  // The swarm's coordinator, run from this machine whether or not a Harness window is open. When a thread goes quiet
7
10
  // (no run left going) and has new posts the coordinator has not looked at, it runs one round: it gathers the facts
@@ -81,7 +84,14 @@ In briefs, refer to earlier posts by their seq number instead of restating figur
81
84
  The goal: statement is the whole aim in the person's terms; criteria are one to five checks a person or code can verify. A criterion is met only when a post shows the result, not when an agent only says so. goalState met only when every criterion is met.
82
85
  If the same attempt or failure keeps coming back, say so in loop and ask the next agent for a different approach in the brief, naming what was tried.
83
86
  A check-in brings the thread back to you at that time with a post saying what to look at; checkIns lists the thread's check-ins already set.
84
- The thread's team, when \`team\` is given: who holds which role and the step of its route. When \`team.step.finished\` is true and \`team.exits\` lists where the work goes next, hand on along the route unless a question to the person is open: for a step that judges (\`team.step.judges\`), read the newest report and pick the exit whose \`outcome\` it shows (passed, or sent_back when it found something to fix); hand on to that exit's agents and add "team": {"step": the exit's step, "outcome": its outcome} to your answer. An exit to step "stop" means pause and tell the person the work was sent back as often as the team allows; one to step "done" means decide as usual and add "team": {"step": "done", "outcome": "done"}. Never hand a check or a review to the agent that did the work.`;
87
+ The thread's route, when \`team\` is given: its steps (frame, make, check, combine, judge), who holds each and the step it is at. When \`team.step.finished\` is true and \`team.exits\` lists where the work goes next, hand on along the route unless a question to the person is open: for a step that judges (\`team.step.judges\`), read the newest report and pick the exit whose \`outcome\` it shows (passed, or sent_back when it found something to fix); hand on to that exit's agents and add "team": {"step": the exit's step, "outcome": its outcome} to your answer. An exit to step "stop" means pause and tell the person the work was sent back as often as the route allows; one to step "done" means decide as usual and add "team": {"step": "done", "outcome": "done"}. An exit to step "piece" goes to the next piece in \`team.nextPiece\`: route it as below, hand on to its first step's agents and add "team": {"step": "piece", "outcome": the exit's outcome, "plan": its route}. Never hand a check or a review to the agent that made the work.
88
+ Routing, when \`team\` is null or \`team.nextPiece\` is given, and \`teamMembers\` lists who may hold a step: you choose, you never write the frame. Add "team": {"plan": {"rigor": "light" | "standard" | "careful" | "thorough", "split": true only when the goal needs a framer to split it into pieces for different kinds of agent or one after another (never for a piece), "steps": [{"id": "frame" | "make" | "check" | "combine" | "judge", "specialty": the one specialty id the step asks for, "agents": ids from teamMembers}]}}. Rigor picks the steps: ${Object.entries(
89
+ TEAM.rigor
90
+ )
91
+ .map(([level, rigor]) => `${level}: ${rigor.steps.join(", ")}`)
92
+ .join(
93
+ "; "
94
+ )}; combine also runs when several agents make. At light the work goes straight to one maker. Anything the person depends on, or that moves money, is careful at least. Make may have several agents side by side, only when its parts are independent; every other step has one. Check and judge never go to a maker, judge never to the framer, and combine is one agent, never a vote. An agent the person tagged fills a step, Make unless they said otherwise; a pin in \`pins\` (specialty: agent) holds the step that asks for that specialty. An agent with work running or waiting elsewhere is less available. When you route, hand on to the agents of the route's first step. Specialty ids: ${SPECIALTIES.map((specialty) => specialty.id).join(", ")}.`;
85
95
 
86
96
  function localDecision(agent, facts) {
87
97
  return new Promise((resolve, reject) => {
@@ -176,6 +186,8 @@ const choicesOf = (value) => {
176
186
  const FAILED_REST_MS = 10 * 60_000;
177
187
 
178
188
  export function createCoordinator({
189
+ // Whether this computer acts on the board (home or a stand-in); a worker's copy gets no rounds here.
190
+ holdsBoard = () => true,
179
191
  store,
180
192
  mode,
181
193
  askHarness,
@@ -225,10 +237,18 @@ export function createCoordinator({
225
237
  // Lessons go to a coordinator that reads them: this computer's, or a Harness that has swarm_lessons.
226
238
  const readsLessons = () =>
227
239
  Boolean(lessons) && (mode() === "local" || serverHas("swarm_lessons"));
228
- // The team goes to a coordinator that keeps it: this computer's follows a route, and a Harness with swarm_team also
229
- // proposes one in the thread's first assembly.
240
+ // The route goes to a coordinator that keeps it: this computer's, or a Harness with swarm_routes, which triages the
241
+ // goal in the thread's first assembly and routes each piece a framer splits off.
230
242
  const keepsTeams = () =>
231
- Boolean(team) && (mode() === "local" || serverHas("swarm_team"));
243
+ Boolean(team) && (mode() === "local" || serverHas("swarm_routes"));
244
+ // The person's pins: specialty -> the agent that holds it when a step asks for it.
245
+ const pinsNow = () =>
246
+ Object.fromEntries(
247
+ SPECIALTIES.map((specialty) => [
248
+ specialty.id,
249
+ pinnedFor(store, specialty.id),
250
+ ]).filter(([, agent]) => agent)
251
+ );
232
252
  // The backups, in the order they are tried: coding agents on this computer, most finished work first, one whose last
233
253
  // turn as a backup failed after the others.
234
254
  const backups = () => {
@@ -324,6 +344,9 @@ export function createCoordinator({
324
344
  const causesTaken = mode() === "local" || serverHas("swarm_failure_causes");
325
345
  const verdictsTaken = mode() === "local" || serverHas("swarm_reactions");
326
346
  const machinesTaken = mode() === "local" || serverHas("machines");
347
+ // Each card's specialties, pins and offices go only to a Harness that reads them (feature team_specialties).
348
+ const specialtiesTaken =
349
+ mode() === "local" || serverHas("team_specialties");
327
350
  // Who could hold a role on the thread's team: the candidates and the agents the person named.
328
351
  const eligible = roster()
329
352
  .filter(
@@ -351,21 +374,35 @@ export function createCoordinator({
351
374
  },
352
375
  }),
353
376
  lessons: readsLessons() ? candidate.lessons : undefined,
377
+ specialties: specialtiesTaken
378
+ ? roundFields(candidate).specialties
379
+ : undefined,
380
+ pinned: specialtiesTaken ? roundFields(candidate).pinned : undefined,
381
+ offices: specialtiesTaken ? roundFields(candidate).offices : undefined,
354
382
  machines: machinesTaken
355
383
  ? machinesOf(candidate.id, thread.goal.workspace)
356
384
  : undefined,
385
+ // Whether a coding agent took its readiness prompt is for the person's surfaces; the round's card schema does not
386
+ // have it, and a round carrying it was refused as malformed once an agent's check came back.
387
+ readiness: undefined,
357
388
  }));
358
389
  const candidates = eligible.filter(
359
390
  (candidate) => !asked?.named.includes(candidate.id)
360
391
  );
361
- // The team: where its route stands, or none yet, which the person's message to the swarm settles.
392
+ // The route: where it stands, or none yet, which the person's message to the swarm triages (on the privacy setting,
393
+ // this computer's next round). A round that starts the next piece a framer split off routes it with the same facts.
362
394
  const teamView = keepsTeams()
363
395
  ? team.roundView(thread.id, runs, { head: headOf(thread.id) })
364
396
  : null;
397
+ const pins = keepsTeams() ? pinsNow() : {};
398
+ const routing = {
399
+ teamMembers: eligible.slice(0, 16),
400
+ ...(Object.keys(pins).length ? { pins } : {}),
401
+ };
365
402
  const teamFacts = teamView
366
- ? { team: teamView }
367
- : keepsTeams() && asked && mode() !== "local"
368
- ? { team: null, teamMembers: eligible.slice(0, 16) }
403
+ ? { team: teamView, ...(teamView.nextPiece ? routing : {}) }
404
+ : keepsTeams() && (asked || mode() === "local")
405
+ ? { team: null, ...routing }
369
406
  : {};
370
407
  // Earlier words close in meaning to the newest post that the transcript no longer carries, from this thread and the
371
408
  // person's sessions on this machine.
@@ -635,28 +672,48 @@ export function createCoordinator({
635
672
  });
636
673
  return other ?? agent;
637
674
  };
638
- // The team's part of a decision: a plan for a thread that had none, said in the thread.
639
- const applyTeamPlan = (thread, decision, coordinator) => {
675
+ // The route's part of a decision: one for a thread that had none, said in the thread. The person's tags and pins win
676
+ // over the proposal step by step (team.mjs validPlan).
677
+ const routeFor = (thread, coordinator, named = []) => ({
678
+ members: roster().map((card) => card.id),
679
+ named,
680
+ pins: pinsNow(),
681
+ by: coordinator,
682
+ post: (text) =>
683
+ postAsCoordinator({
684
+ goalId: thread.id,
685
+ author: coordinator,
686
+ kind: "note",
687
+ text,
688
+ }),
689
+ });
690
+ const applyTeamPlan = (thread, decision, coordinator, asked = null) => {
640
691
  const proposed = decision.team?.plan;
641
- if (!proposed || !keepsTeams() || team.plan(thread.id)) return null;
642
- return team.set(thread.id, proposed, {
643
- members: roster().map((card) => card.id),
644
- by: coordinator,
645
- post: (text) =>
646
- postAsCoordinator({
647
- goalId: thread.id,
648
- author: coordinator,
649
- kind: "note",
650
- text,
651
- }),
652
- });
692
+ if (
693
+ !proposed ||
694
+ decision.team?.step === "piece" ||
695
+ !keepsTeams() ||
696
+ team.plan(thread.id)
697
+ )
698
+ return null;
699
+ return team.set(
700
+ thread.id,
701
+ proposed,
702
+ routeFor(thread, coordinator, asked?.named ?? [])
703
+ );
653
704
  };
654
- // Where the work went: along the route to the step the round chose, or, handed outside it, to the step of the agents
655
- // who now have it.
656
- const moveTeam = (thread, decision, agents) => {
705
+ // Where the work went: along the route to the step the round chose, on to the next piece as the round routed it, or,
706
+ // handed outside the route, to the step of the agents who now have it.
707
+ const moveTeam = (thread, decision, agents, coordinator) => {
657
708
  if (!keepsTeams()) return;
658
709
  const step = decision.team?.step;
659
- if (step && step !== "done")
710
+ if (step === "piece")
711
+ team.routePiece(thread.id, decision.team.plan, {
712
+ ...routeFor(thread, coordinator ?? thread.goal.facilitator),
713
+ outcome: decision.team.outcome ?? null,
714
+ head: headOf(thread.id),
715
+ });
716
+ else if (step && step !== "done")
660
717
  team.moveTo(thread.id, step, {
661
718
  outcome: decision.team.outcome ?? null,
662
719
  head: headOf(thread.id),
@@ -673,6 +730,55 @@ export function createCoordinator({
673
730
  });
674
731
  };
675
732
 
733
+ // Work routed to an agent the person pinned for the step's specialty waits while that agent works in another thread,
734
+ // and the thread says so (decision 14); it is handed on once the agent is free (releaseHeld, at every round).
735
+ const HELD = "pin-waits";
736
+ const heldWork = () => {
737
+ try {
738
+ return JSON.parse(store.meta(HELD) || "[]");
739
+ } catch {
740
+ return [];
741
+ }
742
+ };
743
+ const handOnOrHold = (thread, agent, reason, options, coordinator) => {
744
+ const step = keepsTeams() ? team.pinnedStep(thread.id, agent) : null;
745
+ if (!step || !store.busyElsewhere(agent, thread.id)) {
746
+ handOn(thread.id, agent, reason, options);
747
+ return;
748
+ }
749
+ store.meta(
750
+ HELD,
751
+ JSON.stringify([
752
+ ...heldWork(),
753
+ {
754
+ goalId: thread.id,
755
+ agent,
756
+ reason,
757
+ options,
758
+ at: new Date().toISOString(),
759
+ },
760
+ ])
761
+ );
762
+ postAsCoordinator({
763
+ goalId: thread.id,
764
+ author: coordinator,
765
+ kind: "note",
766
+ text: `The ${step.id} step waits for ${nameOf(agent)}, which you pinned for ${specialtyName(step.specialty)}: it is working in another thread, and starts on this when that work ends.`,
767
+ });
768
+ };
769
+ const releaseHeld = () => {
770
+ const waiting = heldWork();
771
+ if (!waiting.length) return;
772
+ const still = [];
773
+ for (const held of waiting) {
774
+ const thread = store.getTask(held.goalId);
775
+ if (!thread || thread.status !== "open") continue;
776
+ if (store.busyElsewhere(held.agent, held.goalId)) still.push(held);
777
+ else handOn(held.goalId, held.agent, held.reason, held.options);
778
+ }
779
+ store.meta(HELD, JSON.stringify(still));
780
+ };
781
+
676
782
  const apply = (
677
783
  thread,
678
784
  given,
@@ -684,7 +790,7 @@ export function createCoordinator({
684
790
  endRoute(thread, given);
685
791
  if (applyGoal(thread, given, coordinator)) return;
686
792
  applyCheckIn(thread, given, coordinator);
687
- const planned = applyTeamPlan(thread, given, coordinator);
793
+ const planned = applyTeamPlan(thread, given, coordinator, asked);
688
794
  const decision = guardQuestion(thread, given, candidates, rules);
689
795
  // The person's message is never met with silence: a round that brings no one in and asks them nothing lets the
690
796
  // agents hear it as they wrote it, as with `@swarm` (owner, October 16: "it should never be silent if jev doesnt
@@ -726,7 +832,8 @@ export function createCoordinator({
726
832
  );
727
833
  if (!parts.length) {
728
834
  if (unheard) tellEveryone(thread, asked, null, { decided: true });
729
- else if (planned) moveTeam(thread, decision, asked?.named ?? []);
835
+ else if (planned)
836
+ moveTeam(thread, decision, asked?.named ?? [], coordinator);
730
837
  notifyBoard(store.getTask(thread.id));
731
838
  return;
732
839
  }
@@ -757,16 +864,19 @@ export function createCoordinator({
757
864
  .join("\n\n"),
758
865
  });
759
866
  // The team moves first, so each brief says the step its agent now acts in.
760
- moveTeam(thread, decision, [
761
- ...worded.map((part) => part.agent),
762
- ...(asked?.named ?? []),
763
- ]);
867
+ moveTeam(
868
+ thread,
869
+ decision,
870
+ [...worded.map((part) => part.agent), ...(asked?.named ?? [])],
871
+ coordinator
872
+ );
764
873
  for (const part of worded)
765
- handOn(
766
- thread.id,
874
+ handOnOrHold(
875
+ thread,
767
876
  part.agent,
768
877
  `${coordinator} asked you: ${part.text.slice(0, 300)}`,
769
- { review: part.review, words: part.text }
878
+ { review: part.review, words: part.text },
879
+ coordinator
770
880
  );
771
881
  notifyBoard(store.getTask(thread.id));
772
882
  return;
@@ -803,12 +913,13 @@ export function createCoordinator({
803
913
  kind: "post",
804
914
  text: `@${agent} ${review ? "(review, no changes) " : ""}${text}`,
805
915
  });
806
- moveTeam(thread, decision, [agent]);
807
- handOn(
808
- thread.id,
916
+ moveTeam(thread, decision, [agent], coordinator);
917
+ handOnOrHold(
918
+ thread,
809
919
  agent,
810
920
  `${coordinator} asked you: ${text.slice(0, 300)}`,
811
- { review, words: text }
921
+ { review, words: text },
922
+ coordinator
812
923
  );
813
924
  }
814
925
  notifyBoard(store.getTask(thread.id));
@@ -865,6 +976,7 @@ export function createCoordinator({
865
976
  };
866
977
 
867
978
  const round = async (goalId) => {
979
+ releaseHeld();
868
980
  const thread = store.getTask(goalId);
869
981
  if (!thread || thread.kind !== "goal" || thread.status !== "open") return;
870
982
  const state = stateOf(goalId);
@@ -1082,6 +1194,7 @@ export function createCoordinator({
1082
1194
 
1083
1195
  /** A thread changed: run a round once it is quiet; one at a time per thread, again if more came in meanwhile. */
1084
1196
  const poke = (goalId) => {
1197
+ if (!holdsBoard()) return;
1085
1198
  if (busy.has(goalId)) {
1086
1199
  again.add(goalId);
1087
1200
  return;
@@ -0,0 +1,78 @@
1
+ import {
2
+ MODELS,
3
+ ensureLlama,
4
+ ensureModels,
5
+ modelsPresent,
6
+ } from "./localmodels.mjs";
7
+
8
+ // The relay's core local models (localmodels.mjs) give agents context packs, code search, the handoff firewall and
9
+ // image checks. Setup used to download them (about 860 MB) before it said Done, so a new person sat at the end of setup
10
+ // waiting for files they did not need yet. Setup now only records that the person wants them
11
+ // (`config.localModels: "download"`) and the daemon fetches them in the background. Until they are all present, each
12
+ // feature works as it does without them (search answers from words and says so), which is how setup already left a
13
+ // computer whose download failed. A computer whose config never asked for them (an older setup, a test) downloads
14
+ // nothing.
15
+
16
+ /** The core models' ids. */
17
+ export const CORE = MODELS.filter((entry) => entry.tier === "core").map(
18
+ (entry) => entry.id
19
+ );
20
+
21
+ /** How much the core models take to download, in MB, for setup to say. */
22
+ export const coreSizeMb = () =>
23
+ Math.round(
24
+ MODELS.filter((entry) => CORE.includes(entry.id))
25
+ .flatMap((entry) => entry.files)
26
+ .reduce((sum, file) => sum + file.size, 0) / 1e6
27
+ );
28
+
29
+ /**
30
+ * The daemon's background download of the core models. `start` begins it when the config asks for the models and any is
31
+ * missing, once per daemon run (a failed one is tried again when the daemon next starts); `status` says how it stands.
32
+ */
33
+ export function createCoreModels({
34
+ root,
35
+ wanted,
36
+ log = () => {},
37
+ download = { ensureLlama, ensureModels },
38
+ }) {
39
+ let running = null;
40
+ let lastError = null;
41
+ let tried = false;
42
+ const ready = () => modelsPresent(root, CORE);
43
+ const start = () => {
44
+ if (running || tried || !wanted() || ready()) return running;
45
+ tried = true;
46
+ log(`local models: downloading about ${coreSizeMb()} MB in the background`);
47
+ // The model server and the models are fetched apart: the classifiers run without the server, so one that cannot
48
+ // start on this computer does not hold the models back.
49
+ running = (async () => {
50
+ const failures = [];
51
+ for (const step of [
52
+ () => download.ensureLlama({ root, log }),
53
+ () => download.ensureModels({ root, ids: CORE, log }),
54
+ ]) {
55
+ try {
56
+ await step();
57
+ } catch (error) {
58
+ failures.push(error.message);
59
+ }
60
+ }
61
+ lastError = failures.length ? failures.join("; ") : null;
62
+ log(
63
+ lastError
64
+ ? `local models: not ready (${lastError}); tried again when the daemon next starts`
65
+ : "local models: ready"
66
+ );
67
+ })().finally(() => (running = null));
68
+ return running;
69
+ };
70
+ return {
71
+ start,
72
+ status: () => ({
73
+ ready: ready(),
74
+ downloading: Boolean(running),
75
+ error: lastError,
76
+ }),
77
+ };
78
+ }