@caupulican/pi-adaptative 0.85.3 → 0.85.4

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 (192) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/core/agent-session-services.d.ts.map +1 -1
  3. package/dist/core/agent-session-services.js +2 -0
  4. package/dist/core/agent-session-services.js.map +1 -1
  5. package/dist/core/agent-session.d.ts +6 -5
  6. package/dist/core/agent-session.d.ts.map +1 -1
  7. package/dist/core/agent-session.js +76 -87
  8. package/dist/core/agent-session.js.map +1 -1
  9. package/dist/core/auth-guidance.d.ts.map +1 -1
  10. package/dist/core/auth-guidance.js +3 -0
  11. package/dist/core/auth-guidance.js.map +1 -1
  12. package/dist/core/autonomy/lane-tool-surface.d.ts +2 -0
  13. package/dist/core/autonomy/lane-tool-surface.d.ts.map +1 -1
  14. package/dist/core/autonomy/lane-tool-surface.js +4 -3
  15. package/dist/core/autonomy/lane-tool-surface.js.map +1 -1
  16. package/dist/core/background-lane-controller.d.ts +4 -0
  17. package/dist/core/background-lane-controller.d.ts.map +1 -1
  18. package/dist/core/background-lane-controller.js +2 -0
  19. package/dist/core/background-lane-controller.js.map +1 -1
  20. package/dist/core/background-tool-task-controller.d.ts +12 -7
  21. package/dist/core/background-tool-task-controller.d.ts.map +1 -1
  22. package/dist/core/background-tool-task-controller.js +55 -19
  23. package/dist/core/background-tool-task-controller.js.map +1 -1
  24. package/dist/core/bedrock-scope.d.ts +26 -0
  25. package/dist/core/bedrock-scope.d.ts.map +1 -0
  26. package/dist/core/bedrock-scope.js +178 -0
  27. package/dist/core/bedrock-scope.js.map +1 -0
  28. package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -1
  29. package/dist/core/delegation/worker-delegation-controller.js +6 -0
  30. package/dist/core/delegation/worker-delegation-controller.js.map +1 -1
  31. package/dist/core/extensions/loader.d.ts.map +1 -1
  32. package/dist/core/extensions/loader.js +3 -0
  33. package/dist/core/extensions/loader.js.map +1 -1
  34. package/dist/core/foreground-recovery-controller.d.ts +25 -2
  35. package/dist/core/foreground-recovery-controller.d.ts.map +1 -1
  36. package/dist/core/foreground-recovery-controller.js +111 -2
  37. package/dist/core/foreground-recovery-controller.js.map +1 -1
  38. package/dist/core/foreground-terminal-handoff-controller.d.ts +27 -0
  39. package/dist/core/foreground-terminal-handoff-controller.d.ts.map +1 -0
  40. package/dist/core/foreground-terminal-handoff-controller.js +56 -0
  41. package/dist/core/foreground-terminal-handoff-controller.js.map +1 -0
  42. package/dist/core/goal-loop-controller.d.ts.map +1 -1
  43. package/dist/core/goal-loop-controller.js +5 -0
  44. package/dist/core/goal-loop-controller.js.map +1 -1
  45. package/dist/core/goals/goal-auto-continue-controller.d.ts +3 -0
  46. package/dist/core/goals/goal-auto-continue-controller.d.ts.map +1 -1
  47. package/dist/core/goals/goal-auto-continue-controller.js +28 -7
  48. package/dist/core/goals/goal-auto-continue-controller.js.map +1 -1
  49. package/dist/core/human-input-controller.d.ts +9 -0
  50. package/dist/core/human-input-controller.d.ts.map +1 -1
  51. package/dist/core/human-input-controller.js +60 -5
  52. package/dist/core/human-input-controller.js.map +1 -1
  53. package/dist/core/human-input.d.ts +12 -0
  54. package/dist/core/human-input.d.ts.map +1 -1
  55. package/dist/core/human-input.js +59 -12
  56. package/dist/core/human-input.js.map +1 -1
  57. package/dist/core/local-runtime-controller.d.ts.map +1 -1
  58. package/dist/core/local-runtime-controller.js +15 -9
  59. package/dist/core/local-runtime-controller.js.map +1 -1
  60. package/dist/core/memory/providers/file-store.d.ts +1 -0
  61. package/dist/core/memory/providers/file-store.d.ts.map +1 -1
  62. package/dist/core/memory/providers/file-store.js +252 -125
  63. package/dist/core/memory/providers/file-store.js.map +1 -1
  64. package/dist/core/model-capability.d.ts +15 -1
  65. package/dist/core/model-capability.d.ts.map +1 -1
  66. package/dist/core/model-capability.js +24 -1
  67. package/dist/core/model-capability.js.map +1 -1
  68. package/dist/core/model-registry.d.ts +5 -0
  69. package/dist/core/model-registry.d.ts.map +1 -1
  70. package/dist/core/model-registry.js +18 -0
  71. package/dist/core/model-registry.js.map +1 -1
  72. package/dist/core/model-router-controller.d.ts +6 -6
  73. package/dist/core/model-router-controller.d.ts.map +1 -1
  74. package/dist/core/model-router-controller.js +5 -5
  75. package/dist/core/model-router-controller.js.map +1 -1
  76. package/dist/core/models/context-sizing.d.ts +1 -0
  77. package/dist/core/models/context-sizing.d.ts.map +1 -1
  78. package/dist/core/models/context-sizing.js +4 -1
  79. package/dist/core/models/context-sizing.js.map +1 -1
  80. package/dist/core/models/default-model-suggestions.d.ts.map +1 -1
  81. package/dist/core/models/default-model-suggestions.js +20 -10
  82. package/dist/core/models/default-model-suggestions.js.map +1 -1
  83. package/dist/core/models/llamacpp-runtime.d.ts +32 -11
  84. package/dist/core/models/llamacpp-runtime.d.ts.map +1 -1
  85. package/dist/core/models/llamacpp-runtime.js +79 -28
  86. package/dist/core/models/llamacpp-runtime.js.map +1 -1
  87. package/dist/core/models/local-execution-planner.d.ts +24 -0
  88. package/dist/core/models/local-execution-planner.d.ts.map +1 -0
  89. package/dist/core/models/local-execution-planner.js +119 -0
  90. package/dist/core/models/local-execution-planner.js.map +1 -0
  91. package/dist/core/models/local-inference-profile.d.ts +34 -0
  92. package/dist/core/models/local-inference-profile.d.ts.map +1 -0
  93. package/dist/core/models/local-inference-profile.js +74 -0
  94. package/dist/core/models/local-inference-profile.js.map +1 -0
  95. package/dist/core/models/local-runtime.d.ts +23 -4
  96. package/dist/core/models/local-runtime.d.ts.map +1 -1
  97. package/dist/core/models/local-runtime.js +11 -8
  98. package/dist/core/models/local-runtime.js.map +1 -1
  99. package/dist/core/models/managed-ollama-model.d.ts +26 -0
  100. package/dist/core/models/managed-ollama-model.d.ts.map +1 -0
  101. package/dist/core/models/managed-ollama-model.js +59 -0
  102. package/dist/core/models/managed-ollama-model.js.map +1 -0
  103. package/dist/core/models/model-ref.js +11 -11
  104. package/dist/core/models/model-ref.js.map +1 -1
  105. package/dist/core/models/prism-llamacpp-lifecycle.d.ts +10 -20
  106. package/dist/core/models/prism-llamacpp-lifecycle.d.ts.map +1 -1
  107. package/dist/core/models/prism-llamacpp-lifecycle.js +11 -31
  108. package/dist/core/models/prism-llamacpp-lifecycle.js.map +1 -1
  109. package/dist/core/models/prism-llamacpp-server-probe.d.ts +10 -0
  110. package/dist/core/models/prism-llamacpp-server-probe.d.ts.map +1 -0
  111. package/dist/core/models/prism-llamacpp-server-probe.js +36 -0
  112. package/dist/core/models/prism-llamacpp-server-probe.js.map +1 -0
  113. package/dist/core/models/runtime-arbiter.d.ts +3 -0
  114. package/dist/core/models/runtime-arbiter.d.ts.map +1 -1
  115. package/dist/core/models/runtime-arbiter.js +2 -1
  116. package/dist/core/models/runtime-arbiter.js.map +1 -1
  117. package/dist/core/research/research-lane-controller.d.ts.map +1 -1
  118. package/dist/core/research/research-lane-controller.js +14 -5
  119. package/dist/core/research/research-lane-controller.js.map +1 -1
  120. package/dist/core/runtime-builder.d.ts +5 -1
  121. package/dist/core/runtime-builder.d.ts.map +1 -1
  122. package/dist/core/runtime-builder.js +37 -4
  123. package/dist/core/runtime-builder.js.map +1 -1
  124. package/dist/core/scout-controller.d.ts +3 -2
  125. package/dist/core/scout-controller.d.ts.map +1 -1
  126. package/dist/core/scout-controller.js +1 -0
  127. package/dist/core/scout-controller.js.map +1 -1
  128. package/dist/core/sdk.d.ts.map +1 -1
  129. package/dist/core/sdk.js +10 -8
  130. package/dist/core/sdk.js.map +1 -1
  131. package/dist/core/settings-manager.d.ts +13 -0
  132. package/dist/core/settings-manager.d.ts.map +1 -1
  133. package/dist/core/settings-manager.js +59 -0
  134. package/dist/core/settings-manager.js.map +1 -1
  135. package/dist/core/system-prompt-builder.d.ts +5 -2
  136. package/dist/core/system-prompt-builder.d.ts.map +1 -1
  137. package/dist/core/system-prompt-builder.js +22 -10
  138. package/dist/core/system-prompt-builder.js.map +1 -1
  139. package/dist/core/system-prompt.d.ts +3 -0
  140. package/dist/core/system-prompt.d.ts.map +1 -1
  141. package/dist/core/system-prompt.js +181 -55
  142. package/dist/core/system-prompt.js.map +1 -1
  143. package/dist/core/tool-protocol-controller.d.ts.map +1 -1
  144. package/dist/core/tool-protocol-controller.js +5 -3
  145. package/dist/core/tool-protocol-controller.js.map +1 -1
  146. package/dist/core/tools/edit-diff.d.ts +2 -0
  147. package/dist/core/tools/edit-diff.d.ts.map +1 -1
  148. package/dist/core/tools/edit-diff.js +9 -1
  149. package/dist/core/tools/edit-diff.js.map +1 -1
  150. package/dist/core/tools/edit.d.ts +5 -8
  151. package/dist/core/tools/edit.d.ts.map +1 -1
  152. package/dist/core/tools/edit.js +88 -55
  153. package/dist/core/tools/edit.js.map +1 -1
  154. package/dist/core/tools/file-mutation-intent.d.ts +42 -15
  155. package/dist/core/tools/file-mutation-intent.d.ts.map +1 -1
  156. package/dist/core/tools/file-mutation-intent.js +271 -77
  157. package/dist/core/tools/file-mutation-intent.js.map +1 -1
  158. package/dist/core/tools/index.d.ts +1 -1
  159. package/dist/core/tools/index.d.ts.map +1 -1
  160. package/dist/core/tools/index.js +1 -1
  161. package/dist/core/tools/index.js.map +1 -1
  162. package/dist/core/tools/write.d.ts +5 -10
  163. package/dist/core/tools/write.d.ts.map +1 -1
  164. package/dist/core/tools/write.js +92 -73
  165. package/dist/core/tools/write.js.map +1 -1
  166. package/dist/index.d.ts +1 -1
  167. package/dist/index.d.ts.map +1 -1
  168. package/dist/index.js +1 -1
  169. package/dist/index.js.map +1 -1
  170. package/dist/modes/interactive/auth-dialogs-controller.d.ts +6 -0
  171. package/dist/modes/interactive/auth-dialogs-controller.d.ts.map +1 -1
  172. package/dist/modes/interactive/auth-dialogs-controller.js +59 -12
  173. package/dist/modes/interactive/auth-dialogs-controller.js.map +1 -1
  174. package/dist/modes/interactive/local-model-commands.d.ts +1 -1
  175. package/dist/modes/interactive/local-model-commands.d.ts.map +1 -1
  176. package/dist/modes/interactive/local-model-commands.js +51 -8
  177. package/dist/modes/interactive/local-model-commands.js.map +1 -1
  178. package/docs/providers.md +34 -10
  179. package/docs/tool-boundary-performance-roadmap-2026-08-02.md +21 -18
  180. package/examples/extensions/built-in-tool-renderer.ts +1 -1
  181. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  182. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  183. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  184. package/examples/extensions/minimal-mode.ts +1 -1
  185. package/examples/extensions/prompt-customizer.ts +2 -2
  186. package/examples/extensions/sandbox/package-lock.json +2 -2
  187. package/examples/extensions/sandbox/package.json +1 -1
  188. package/examples/extensions/ssh.ts +17 -0
  189. package/examples/extensions/with-deps/package-lock.json +2 -2
  190. package/examples/extensions/with-deps/package.json +1 -1
  191. package/npm-shrinkwrap.json +240 -260
  192. package/package.json +6 -6
@@ -1,10 +1,11 @@
1
1
  import type { Readable } from "node:stream";
2
+ import { type LocalInferenceProfileMode } from "./local-inference-profile.ts";
2
3
  import { type ManagedRuntimeSpawn } from "./runtime-process.ts";
3
4
  /**
4
- * Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their
5
- * llama.cpp fork (Q1_0_g128 hybrid-attention kernels) stock llama.cpp/Ollama cannot serve these
6
- * weights so this module owns runtime install (prebuilt release download+extract), GGUF
7
- * downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable
5
+ * Managed runtime for Pi's curated Prism 1-bit and ternary GGUF models. Q1_0 is available in
6
+ * upstream llama.cpp, while the selected group-128 Q2_0 artifacts and paired DSpark acceleration
7
+ * still require Prism's validated build. This module therefore owns the pinned runtime install,
8
+ * GGUF downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable
8
9
  * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),
9
10
  * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies
10
11
  * instead of silent fallbacks.
@@ -28,23 +29,38 @@ export interface PrismModelDescriptor {
28
29
  file: string;
29
30
  mmprojFile?: string;
30
31
  displayName: string;
32
+ architecture: "dense";
33
+ runtime: "prism-llamacpp";
34
+ family: "bonsai" | "ternary-bonsai";
35
+ parameterScale: "1.7B" | "4B" | "8B" | "27B";
36
+ weightFormat: "q1_0" | "q2_0";
37
+ matchedDrafter?: {
38
+ kind: "dspark";
39
+ file: string;
40
+ draftMax: 4;
41
+ minimumContext: 16_384;
42
+ validatedBackend: "cuda";
43
+ };
31
44
  }
32
- /** Curated first-model descriptor; the `/models add` wiring task consumes this. */
45
+ /** Curated 27B descriptor shared by the full local execution catalog. */
33
46
  export declare const BONSAI_27B: PrismModelDescriptor;
34
47
  export type PrismBackend = "cpu" | "cuda";
35
48
  export type PrismAssetKind = "tar-gz" | "zip";
36
- export interface PrismLlamaAsset {
49
+ export interface PrismArchiveAsset {
37
50
  name: string;
38
51
  kind: PrismAssetKind;
52
+ }
53
+ export interface PrismLlamaAsset extends PrismArchiveAsset {
39
54
  backend: PrismBackend;
55
+ companionAssets?: readonly PrismArchiveAsset[];
40
56
  }
41
57
  /**
42
58
  * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for
43
59
  * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.
44
- * CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU
45
- * present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a
46
- * companion `cudart-*` archive and is out of scope for v1 Windows always resolves to the CPU zip
47
- * regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.
60
+ * CPU asset by default; CUDA 12.4 is selected for x64 Linux or Windows with an NVIDIA GPU. The
61
+ * pinned Windows build is incomplete without its matching `cudart-*` archive, so the resolver
62
+ * returns both as one mandatory installation plan. Pure and exported so it's independently
63
+ * testable.
48
64
  */
49
65
  export declare function resolvePrismLlamaAsset(plat: string, architecture: string, hasNvidiaGpu: boolean): PrismLlamaAsset | undefined;
50
66
  export interface PrismDetectResult {
@@ -81,6 +97,9 @@ export interface PrismLlamaCppDeps {
81
97
  hasNvidiaGpu?: () => boolean;
82
98
  platform?: () => string;
83
99
  arch?: () => string;
100
+ /** Host capacity probes used once to derive the bounded local inference profile. */
101
+ totalMemoryBytes?: () => number;
102
+ logicalCpuCount?: () => number;
84
103
  /** Runs the extraction step for a downloaded archive. Injectable so installManaged's
85
104
  * download->extract->scan orchestration is testable without a real tar/unzip pipeline; defaults
86
105
  * to the real spawn-based extractor. */
@@ -102,9 +121,11 @@ export declare class PrismLlamaCppRuntime {
102
121
  private readonly _extractArchiveFn;
103
122
  private readonly _healthPollAttempts;
104
123
  private readonly _healthPollIntervalMs;
124
+ private readonly _profile;
105
125
  private _child;
106
126
  constructor(args: {
107
127
  agentDir: string;
128
+ profileMode?: LocalInferenceProfileMode;
108
129
  deps?: PrismLlamaCppDeps;
109
130
  });
110
131
  runtimeDir(): string;
@@ -144,7 +165,6 @@ export declare class PrismLlamaCppRuntime {
144
165
  repo: string;
145
166
  file: string;
146
167
  }, onProgress?: (status: string) => void): Promise<PrismDownloadResult>;
147
- private _healthUp;
148
168
  /**
149
169
  * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without
150
170
  * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed
@@ -152,6 +172,7 @@ export declare class PrismLlamaCppRuntime {
152
172
  */
153
173
  serve(args: {
154
174
  modelPath: string;
175
+ modelAlias: string;
155
176
  mmprojPath?: string;
156
177
  port: number;
157
178
  numCtx: number;
@@ -1 +1 @@
1
- {"version":3,"file":"llamacpp-runtime.d.ts","sourceRoot":"","sources":["../../../src/core/models/llamacpp-runtime.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAK5C,OAAO,EAIN,KAAK,mBAAmB,EAOxB,MAAM,sBAAsB,CAAC;AAE9B;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,6BAA6B,wBAAwB,C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AC;QACxC,IAAI,CAAC,EAAE,iBAAiB,CAAC;KACzB,EAYA;IAED,UAAU,IAAI,MAAM,CAEnB;IAED,SAAS,IAAI,MAAM,CAElB;IAED,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,aAAa;IAoBrB,OAAO,CAAC,cAAc;IAIhB,MAAM,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAUzC;IAED;;;yDAGqD;IACrD,OAAO,CAAC,kBAAkB;IAqB1B;;;;;OAKG;IACG,cAAc,CAAC,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAqBpG;IAED,OAAO,CAAC,eAAe;YAeT,eAAe;YAYf,aAAa;YA+Bb,WAAW;YAYX,oBAAoB;IAYlC;;;;;OAKG;IACG,aAAa,CAClB,IAAI,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EACpC,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACnC,OAAO,CAAC,mBAAmB,CAAC,CA6B9B;IAED;;;;OAIG;IACG,KAAK,CAAC,IAAI,EAAE;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;KACf,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAmE5B;IAED,IAAI,IAAI;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAS3B;IAED,SAAS,IAAI,OAAO,CAEnB;CACD"}
@@ -5,12 +5,14 @@ import { pipeline } from "node:stream/promises";
5
5
  import { spawnProcess, waitForChildProcessWithTermination } from "../../utils/child-process.js";
6
6
  import { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from "../../utils/shell.js";
7
7
  import { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from "../agent-paths.js";
8
+ import { deriveHostLocalInferenceProfile, } from "./local-inference-profile.js";
9
+ import { probePrismLlamaCppServer } from "./prism-llamacpp-server-probe.js";
8
10
  import { extractZipArchive, fetchRuntimeDownload, installRuntimeArchive, removePartialDownload, requireRuntimeStdin, resolveRuntimeLifecycleDependencies, runtimeCommandAvailable, tryFileSizeBytes, writeRuntimeDownload, } from "./runtime-process.js";
9
11
  /**
10
- * Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their
11
- * llama.cpp fork (Q1_0_g128 hybrid-attention kernels) stock llama.cpp/Ollama cannot serve these
12
- * weights so this module owns runtime install (prebuilt release download+extract), GGUF
13
- * downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable
12
+ * Managed runtime for Pi's curated Prism 1-bit and ternary GGUF models. Q1_0 is available in
13
+ * upstream llama.cpp, while the selected group-128 Q2_0 artifacts and paired DSpark acceleration
14
+ * still require Prism's validated build. This module therefore owns the pinned runtime install,
15
+ * GGUF downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable
14
16
  * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),
15
17
  * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies
16
18
  * instead of silent fallbacks.
@@ -29,12 +31,24 @@ import { extractZipArchive, fetchRuntimeDownload, installRuntimeArchive, removeP
29
31
  */
30
32
  export const PRISM_LLAMACPP_PINNED_RELEASE = "prism-b9594-38c66ad";
31
33
  export const PRISM_LLAMACPP_RELEASES_BASE_URL = "https://github.com/PrismML-Eng/llama.cpp/releases/download";
32
- /** Curated first-model descriptor; the `/models add` wiring task consumes this. */
34
+ /** Curated 27B descriptor shared by the full local execution catalog. */
33
35
  export const BONSAI_27B = {
34
36
  repo: "prism-ml/Bonsai-27B-gguf",
35
37
  file: "Bonsai-27B-Q1_0.gguf",
36
38
  mmprojFile: "Bonsai-27B-mmproj-Q8_0.gguf",
37
39
  displayName: "Bonsai-27B (1-bit Q1_0 + vision)",
40
+ architecture: "dense",
41
+ runtime: "prism-llamacpp",
42
+ family: "bonsai",
43
+ parameterScale: "27B",
44
+ weightFormat: "q1_0",
45
+ matchedDrafter: {
46
+ kind: "dspark",
47
+ file: "Bonsai-27B-dspark-Q4_1.gguf",
48
+ draftMax: 4,
49
+ minimumContext: 16_384,
50
+ validatedBackend: "cuda",
51
+ },
38
52
  };
39
53
  // Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —
40
54
  // hardcoded rather than templated from the tag because the provider's naming isn't fully
@@ -47,13 +61,15 @@ const MACOS_ARM64_ASSET = "llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz";
47
61
  const MACOS_X64_ASSET = "llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz";
48
62
  const WIN_X64_CPU_ASSET = "llama-bin-win-cpu-x64.zip";
49
63
  const WIN_ARM64_CPU_ASSET = "llama-bin-win-cpu-arm64.zip";
64
+ const WIN_X64_CUDA_ASSET = "llama-prism-b1-38c66ad-bin-win-cuda-12.4-x64.zip";
65
+ const WIN_X64_CUDA_COMPANION_ASSET = "cudart-llama-bin-win-cuda-12.4-x64.zip";
50
66
  /**
51
67
  * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for
52
68
  * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.
53
- * CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU
54
- * present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a
55
- * companion `cudart-*` archive and is out of scope for v1 Windows always resolves to the CPU zip
56
- * regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.
69
+ * CPU asset by default; CUDA 12.4 is selected for x64 Linux or Windows with an NVIDIA GPU. The
70
+ * pinned Windows build is incomplete without its matching `cudart-*` archive, so the resolver
71
+ * returns both as one mandatory installation plan. Pure and exported so it's independently
72
+ * testable.
57
73
  */
58
74
  export function resolvePrismLlamaAsset(plat, architecture, hasNvidiaGpu) {
59
75
  if (plat === "linux") {
@@ -74,8 +90,16 @@ export function resolvePrismLlamaAsset(plat, architecture, hasNvidiaGpu) {
74
90
  return undefined;
75
91
  }
76
92
  if (plat === "win32") {
77
- if (architecture === "x64")
78
- return { name: WIN_X64_CPU_ASSET, kind: "zip", backend: "cpu" };
93
+ if (architecture === "x64") {
94
+ return hasNvidiaGpu
95
+ ? {
96
+ name: WIN_X64_CUDA_ASSET,
97
+ kind: "zip",
98
+ backend: "cuda",
99
+ companionAssets: [{ name: WIN_X64_CUDA_COMPANION_ASSET, kind: "zip" }],
100
+ }
101
+ : { name: WIN_X64_CPU_ASSET, kind: "zip", backend: "cpu" };
102
+ }
79
103
  if (architecture === "arm64")
80
104
  return { name: WIN_ARM64_CPU_ASSET, kind: "zip", backend: "cpu" };
81
105
  return undefined;
@@ -94,6 +118,9 @@ function parseContentLength(header) {
94
118
  const value = Number(header);
95
119
  return Number.isFinite(value) && value >= 0 ? value : undefined;
96
120
  }
121
+ function modelIdentityConflictError(servedModelIds) {
122
+ return `model-identity-conflict:${servedModelIds.join(",") || "unknown"}`;
123
+ }
97
124
  export class PrismLlamaCppRuntime {
98
125
  constructor(args) {
99
126
  this._agentDir = args.agentDir;
@@ -102,6 +129,7 @@ export class PrismLlamaCppRuntime {
102
129
  this._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand("nvidia-smi"));
103
130
  this._platform = args.deps?.platform ?? osPlatform;
104
131
  this._arch = args.deps?.arch ?? osArch;
132
+ this._profile = deriveHostLocalInferenceProfile(args.profileMode ?? "balanced", args.deps);
105
133
  this._extractArchiveFn =
106
134
  args.deps?.extractArchive ?? ((input, destDir, kind) => this._extractArchive(input, destDir, kind));
107
135
  this._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;
@@ -186,10 +214,13 @@ export class PrismLlamaCppRuntime {
186
214
  const asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());
187
215
  if (!asset)
188
216
  return { ok: false, error: "unsupported-platform" };
189
- const downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${asset.name}`;
190
- const extracted = await this._installArchive(downloadUrl, asset, onProgress);
191
- if (!extracted.ok)
192
- return extracted;
217
+ const archives = [asset, ...(asset.companionAssets ?? [])];
218
+ for (const archive of archives) {
219
+ const downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${archive.name}`;
220
+ const extracted = await this._installArchive(downloadUrl, archive, onProgress);
221
+ if (!extracted.ok)
222
+ return extracted;
223
+ }
193
224
  onProgress?.("Locating llama-server binary…");
194
225
  const destDir = this.runtimeDir();
195
226
  const binaryRelPath = this._findBinaryRelPath(destDir);
@@ -297,17 +328,6 @@ export class PrismLlamaCppRuntime {
297
328
  onProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);
298
329
  return { ok: true, path: destPath };
299
330
  }
300
- async _healthUp(baseUrl) {
301
- try {
302
- const response = await this._fetch(`${baseUrl}/health`, {
303
- signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),
304
- });
305
- return response.ok;
306
- }
307
- catch {
308
- return false;
309
- }
310
- }
311
331
  /**
312
332
  * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without
313
333
  * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed
@@ -321,9 +341,19 @@ export class PrismLlamaCppRuntime {
321
341
  if (!this._exists(binaryPath))
322
342
  return { ok: false, error: "binary-missing" };
323
343
  const baseUrl = `http://127.0.0.1:${args.port}`;
344
+ const existing = await probePrismLlamaCppServer(baseUrl, args.modelAlias, this._fetch, HEALTH_CHECK_TIMEOUT_MS);
345
+ if (existing.status === "matching")
346
+ return { ok: true, baseUrl };
347
+ if (existing.status === "conflict" && !this._child) {
348
+ return { ok: false, error: modelIdentityConflictError(existing.servedModelIds) };
349
+ }
350
+ if (this._child)
351
+ this.stop();
324
352
  const argv = [
325
353
  "-m",
326
354
  args.modelPath,
355
+ "--alias",
356
+ args.modelAlias,
327
357
  ...(args.mmprojPath ? ["--mmproj", args.mmprojPath] : []),
328
358
  "--host",
329
359
  "127.0.0.1",
@@ -331,6 +361,20 @@ export class PrismLlamaCppRuntime {
331
361
  String(args.port),
332
362
  "-c",
333
363
  String(args.numCtx),
364
+ "--cache-ram",
365
+ String(this._profile.promptCacheMiB),
366
+ "-np",
367
+ String(this._profile.parallelRequests),
368
+ "-fa",
369
+ this._profile.flashAttention ? "on" : "off",
370
+ "-ctk",
371
+ this._profile.kvCacheType,
372
+ "-ctv",
373
+ this._profile.kvCacheType,
374
+ "-t",
375
+ String(this._profile.generationThreads),
376
+ "-tb",
377
+ String(this._profile.batchThreads),
334
378
  ...(manifest.backend === "cuda" ? ["-ngl", "99"] : []),
335
379
  ];
336
380
  const child = this._spawn(binaryPath, argv, {
@@ -342,12 +386,19 @@ export class PrismLlamaCppRuntime {
342
386
  trackDetachedChildPid(child.pid);
343
387
  child.unref?.();
344
388
  child.on("exit", () => {
345
- this._child = undefined;
389
+ if (this._child === child) {
390
+ this._child = undefined;
391
+ }
346
392
  });
347
393
  this._child = child;
348
394
  for (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {
349
- if (await this._healthUp(baseUrl))
395
+ const probe = await probePrismLlamaCppServer(baseUrl, args.modelAlias, this._fetch, HEALTH_CHECK_TIMEOUT_MS);
396
+ if (probe.status === "matching")
350
397
  return { ok: true, baseUrl };
398
+ if (probe.status === "conflict") {
399
+ this.stop();
400
+ return { ok: false, error: modelIdentityConflictError(probe.servedModelIds) };
401
+ }
351
402
  await this._sleep(this._healthPollIntervalMs);
352
403
  }
353
404
  this.stop();
@@ -1 +1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,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC1B,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC;IAClC,CAAC;CACD","sourcesContent":["import type { ChildProcess } from \"node:child_process\";\nimport { type Dirent, mkdirSync, readdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { arch as osArch, platform as osPlatform } from \"node:os\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { Readable } from \"node:stream\";\nimport { pipeline } from \"node:stream/promises\";\nimport { spawnProcess, waitForChildProcessWithTermination } from \"../../utils/child-process.ts\";\nimport { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from \"../../utils/shell.ts\";\nimport { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from \"../agent-paths.ts\";\nimport {\n\textractZipArchive,\n\tfetchRuntimeDownload,\n\tinstallRuntimeArchive,\n\ttype ManagedRuntimeSpawn,\n\tremovePartialDownload,\n\trequireRuntimeStdin,\n\tresolveRuntimeLifecycleDependencies,\n\truntimeCommandAvailable,\n\ttryFileSizeBytes,\n\twriteRuntimeDownload,\n} from \"./runtime-process.ts\";\n\n/**\n * Managed runtime for prism-ml 1-bit GGUF models (Bonsai-27B first). The provider requires their\n * llama.cpp fork (Q1_0_g128 hybrid-attention kernels) — stock llama.cpp/Ollama cannot serve these\n * weights — so this module owns runtime install (prebuilt release download+extract), GGUF\n * downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable\n * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),\n * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies\n * instead of silent fallbacks.\n *\n * The fork publishes precompiled per-platform archives on GitHub Releases (no local build): pin a\n * release tag, download the matching asset, extract it, and locate the `llama-server` binary once\n * at install time (release archive layouts vary — bin/ vs build/bin/ at the top level — so this is\n * a deterministic one-time scan, not a fallback). The found relative path and backend (cpu/cuda)\n * are persisted to a manifest so later `detect()`/`serve()` calls never need to re-scan.\n */\n\n/**\n * Pinned Prism ML llama.cpp fork release — verified via\n * `GET https://api.github.com/repos/PrismML-Eng/llama.cpp/releases/tags/prism-b9594-38c66ad` on\n * 2026-07-18 (release exists, all assets referenced below are present on it). Bump here (and\n * re-verify with the same API call) when the provider ships new kernels.\n */\nexport const PRISM_LLAMACPP_PINNED_RELEASE = \"prism-b9594-38c66ad\";\n\nexport const PRISM_LLAMACPP_RELEASES_BASE_URL = \"https://github.com/PrismML-Eng/llama.cpp/releases/download\";\n\nexport interface PrismModelDescriptor {\n\trepo: string;\n\tfile: string;\n\tmmprojFile?: string;\n\tdisplayName: string;\n}\n\n/** Curated first-model descriptor; the `/models add` wiring task consumes this. */\nexport const BONSAI_27B: PrismModelDescriptor = {\n\trepo: \"prism-ml/Bonsai-27B-gguf\",\n\tfile: \"Bonsai-27B-Q1_0.gguf\",\n\tmmprojFile: \"Bonsai-27B-mmproj-Q8_0.gguf\",\n\tdisplayName: \"Bonsai-27B (1-bit Q1_0 + vision)\",\n};\n\nexport type PrismBackend = \"cpu\" | \"cuda\";\nexport type PrismAssetKind = \"tar-gz\" | \"zip\";\n\nexport interface PrismLlamaAsset {\n\tname: string;\n\tkind: PrismAssetKind;\n\tbackend: PrismBackend;\n}\n\n// Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —\n// hardcoded rather than templated from the tag because the provider's naming isn't fully\n// consistent across platforms (Windows CPU assets drop the tag/commit segment entirely; the\n// Windows CUDA asset uses a different build-number segment, \"b1\" not \"b9594\").\nconst LINUX_X64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-x64.tar.gz\";\nconst LINUX_ARM64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-arm64.tar.gz\";\nconst LINUX_X64_CUDA_ASSET = \"llama-prism-b9594-38c66ad-bin-linux-cuda-12.4-x64.tar.gz\";\nconst MACOS_ARM64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz\";\nconst MACOS_X64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz\";\nconst WIN_X64_CPU_ASSET = \"llama-bin-win-cpu-x64.zip\";\nconst WIN_ARM64_CPU_ASSET = \"llama-bin-win-cpu-arm64.zip\";\n\n/**\n * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for\n * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.\n * CPU asset by default; the CUDA 12.4 variant is only offered for linux x64 with an NVIDIA GPU\n * present (a 12.8 variant also exists upstream but has no wired caller yet). Windows CUDA needs a\n * companion `cudart-*` archive and is out of scope for v1 — Windows always resolves to the CPU zip\n * regardless of `hasNvidiaGpu`. Pure and exported so it's independently testable.\n */\nexport function resolvePrismLlamaAsset(\n\tplat: string,\n\tarchitecture: string,\n\thasNvidiaGpu: boolean,\n): PrismLlamaAsset | undefined {\n\tif (plat === \"linux\") {\n\t\tif (architecture === \"x64\") {\n\t\t\treturn hasNvidiaGpu\n\t\t\t\t? { name: LINUX_X64_CUDA_ASSET, kind: \"tar-gz\", backend: \"cuda\" }\n\t\t\t\t: { name: LINUX_X64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\t}\n\t\tif (architecture === \"arm64\") return { name: LINUX_ARM64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"darwin\") {\n\t\tif (architecture === \"arm64\") return { name: MACOS_ARM64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\tif (architecture === \"x64\") return { name: MACOS_X64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"win32\") {\n\t\tif (architecture === \"x64\") return { name: WIN_X64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\tif (architecture === \"arm64\") return { name: WIN_ARM64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\treturn undefined;\n}\n\nexport interface PrismDetectResult {\n\truntimeInstalled: boolean;\n\tbinaryPath?: string;\n\trelease?: string;\n}\n\nexport interface PrismDownloadResult {\n\tok: boolean;\n\tpath?: string;\n\tskipped?: boolean;\n\terror?: string;\n}\n\nexport type PrismServeResult = { ok: true; baseUrl: string } | { ok: false; error: string };\n\ninterface PrismInstallManifest {\n\trelease: string;\n\tbinaryRelPath: string;\n\tbackend: PrismBackend;\n}\n\ntype PrismExtractArchiveFn = (\n\tinput: Readable,\n\tdestDir: string,\n\tkind: PrismAssetKind,\n) => Promise<{ ok: boolean; error?: string }>;\n\nexport interface PrismLlamaCppDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: ManagedRuntimeSpawn;\n\texistsFn?: (path: string) => boolean;\n\tsleepFn?: (ms: number) => Promise<void>;\n\t/** Whether a named command exists on PATH (nvidia-smi, tar — for zip extraction on Windows). */\n\thasCommand?: (command: string) => boolean;\n\t/** Decided once at install time and persisted as `backend` — serve() reads the persisted value\n\t * so `-ngl 99` reflects what was actually installed, not the host's current GPU state. */\n\thasNvidiaGpu?: () => boolean;\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Runs the extraction step for a downloaded archive. Injectable so installManaged's\n\t * download->extract->scan orchestration is testable without a real tar/unzip pipeline; defaults\n\t * to the real spawn-based extractor. */\n\textractArchive?: PrismExtractArchiveFn;\n\t/** Health-poll bounds for serve(); default ~120s (240 * 500ms), both overridable for tests. */\n\thealthPollAttempts?: number;\n\thealthPollIntervalMs?: number;\n}\n\nconst EXTRACTION_TIMEOUT_MS = 10 * 60_000;\nconst COMMAND_KILL_GRACE_MS = 2_000;\nconst DEFAULT_HEALTH_POLL_ATTEMPTS = 240;\nconst DEFAULT_HEALTH_POLL_INTERVAL_MS = 500;\nconst HEALTH_CHECK_TIMEOUT_MS = 2_000;\nconst HEAD_REQUEST_TIMEOUT_MS = 10_000;\n\nfunction parseContentLength(header: string | null): number | undefined {\n\tif (!header) return undefined;\n\tconst value = Number(header);\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\nexport class PrismLlamaCppRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: ManagedRuntimeSpawn;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _hasCommand: (command: string) => boolean;\n\tprivate readonly _hasNvidiaGpu: () => boolean;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _arch: () => string;\n\tprivate readonly _extractArchiveFn: PrismExtractArchiveFn;\n\tprivate readonly _healthPollAttempts: number;\n\tprivate readonly _healthPollIntervalMs: number;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: { agentDir: string; deps?: PrismLlamaCppDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\t[this._fetch, this._spawn, this._exists, this._sleep] = resolveRuntimeLifecycleDependencies(args.deps);\n\t\tthis._hasCommand = args.deps?.hasCommand ?? runtimeCommandAvailable;\n\t\tthis._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand(\"nvidia-smi\"));\n\t\tthis._platform = args.deps?.platform ?? osPlatform;\n\t\tthis._arch = args.deps?.arch ?? osArch;\n\t\tthis._extractArchiveFn =\n\t\t\targs.deps?.extractArchive ?? ((input, destDir, kind) => this._extractArchive(input, destDir, kind));\n\t\tthis._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;\n\t\tthis._healthPollIntervalMs = args.deps?.healthPollIntervalMs ?? DEFAULT_HEALTH_POLL_INTERVAL_MS;\n\t}\n\n\truntimeDir(): string {\n\t\treturn agentRuntimesDir(\"prism-llamacpp\", this._agentDir);\n\t}\n\n\tmodelsDir(): string {\n\t\treturn agentModelsDir(\"llamacpp\", this._agentDir);\n\t}\n\n\tprivate _binaryName(): string {\n\t\treturn this._platform() === \"win32\" ? \"llama-server.exe\" : \"llama-server\";\n\t}\n\n\tprivate _manifestPath(): string {\n\t\treturn join(this.runtimeDir(), \"install.json\");\n\t}\n\n\tprivate _readManifest(): PrismInstallManifest | undefined {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(readFileSync(this._manifestPath(), \"utf8\")) as {\n\t\t\t\trelease?: unknown;\n\t\t\t\tbinaryRelPath?: unknown;\n\t\t\t\tbackend?: unknown;\n\t\t\t};\n\t\t\tif (\n\t\t\t\ttypeof parsed.release === \"string\" &&\n\t\t\t\ttypeof parsed.binaryRelPath === \"string\" &&\n\t\t\t\t(parsed.backend === \"cpu\" || parsed.backend === \"cuda\")\n\t\t\t) {\n\t\t\t\treturn { release: parsed.release, binaryRelPath: parsed.binaryRelPath, backend: parsed.backend };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _writeManifest(manifest: PrismInstallManifest): void {\n\t\twriteFileSync(this._manifestPath(), JSON.stringify(manifest, null, \"\\t\"));\n\t}\n\n\tasync detect(): Promise<PrismDetectResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { runtimeInstalled: false };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tconst runtimeInstalled = this._exists(binaryPath);\n\t\treturn {\n\t\t\truntimeInstalled,\n\t\t\tbinaryPath: runtimeInstalled ? binaryPath : undefined,\n\t\t\trelease: manifest.release,\n\t\t};\n\t}\n\n\t/** One-time recursive scan for the `llama-server` binary inside a freshly extracted release\n\t * archive — layouts vary (bin/ vs build/bin/ at the top level) across platforms/backends, so\n\t * this locates it deterministically instead of assuming a fixed path, then the result is\n\t * persisted so later calls never need to re-scan. */\n\tprivate _findBinaryRelPath(root: string): string | undefined {\n\t\tconst targetName = this._binaryName();\n\t\tconst stack: string[] = [root];\n\t\twhile (stack.length > 0) {\n\t\t\tconst dir = stack.pop();\n\t\t\tif (dir === undefined) continue;\n\t\t\tlet entries: Dirent[];\n\t\t\ttry {\n\t\t\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const entry of entries) {\n\t\t\t\tconst full = join(dir, entry.name);\n\t\t\t\tif (entry.isDirectory()) stack.push(full);\n\t\t\t\telse if (entry.isFile() && entry.name === targetName) return relative(root, full);\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Download the pinned release asset for this host and extract it (consent-gated by the caller,\n\t * same contract as OllamaRuntime#installManaged — this method only does the mechanical\n\t * download+extract+locate and reports the outcome honestly). No compiler toolchain required:\n\t * the fork ships prebuilt binaries.\n\t */\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tconst asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());\n\t\tif (!asset) return { ok: false, error: \"unsupported-platform\" };\n\n\t\tconst downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${asset.name}`;\n\t\tconst extracted = await this._installArchive(downloadUrl, asset, onProgress);\n\t\tif (!extracted.ok) return extracted;\n\n\t\tonProgress?.(\"Locating llama-server binary…\");\n\t\tconst destDir = this.runtimeDir();\n\t\tconst binaryRelPath = this._findBinaryRelPath(destDir);\n\t\tif (!binaryRelPath) {\n\t\t\treturn { ok: false, error: \"binary-missing: no llama-server binary found in the extracted archive\" };\n\t\t}\n\n\t\tthis._writeManifest({ release: PRISM_LLAMACPP_PINNED_RELEASE, binaryRelPath, backend: asset.backend });\n\t\tonProgress?.(\"Prism llama.cpp runtime installed.\");\n\t\treturn { ok: true };\n\t}\n\n\tprivate _installArchive(\n\t\tdownloadUrl: string,\n\t\tasset: PrismLlamaAsset,\n\t\tonProgress?: (status: string) => void,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\treturn installRuntimeArchive(\n\t\t\tthis._fetch,\n\t\t\tdownloadUrl,\n\t\t\tthis.runtimeDir(),\n\t\t\tasset,\n\t\t\tthis._extractArchiveFn,\n\t\t\tonProgress,\n\t\t);\n\t}\n\n\tprivate async _extractArchive(\n\t\tinput: Readable,\n\t\tdestDir: string,\n\t\tkind: PrismAssetKind,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\treturn kind === \"zip\" ? await this._extractZip(input, destDir) : await this._extractTarGz(input, destDir);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t}\n\n\tprivate async _extractTarGz(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\tconst tarProc = spawnProcess(\"tar\", [\"-xzf\", \"-\", \"-C\", destDir], {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: [\"pipe\", \"ignore\", \"ignore\"],\n\t\t});\n\t\tconst terminationController = new AbortController();\n\t\tconst processWait = waitForChildProcessWithTermination(tarProc, {\n\t\t\tsignal: terminationController.signal,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t\ttry {\n\t\t\tawait pipeline(input, requireRuntimeStdin(tarProc, \"tar\"));\n\t\t} catch (error) {\n\t\t\tterminationController.abort();\n\t\t\tawait processWait.catch(() => {});\n\t\t\tthrow error;\n\t\t}\n\t\tconst terminal = await processWait;\n\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\terror:\n\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t? `extract-fail: tar extraction timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t: `extract-fail: tar exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t};\n\t\t}\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractZip(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\treturn extractZipArchive({\n\t\t\tinput,\n\t\t\tdestDir,\n\t\t\ttempPrefix: \"prism-llamacpp-download\",\n\t\t\tplatform: this._platform,\n\t\t\thasCommand: this._hasCommand,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t}\n\n\tprivate async _remoteContentLength(url: string): Promise<number | undefined> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(url, {\n\t\t\t\tmethod: \"HEAD\",\n\t\t\t\tsignal: AbortSignal.timeout(HEAD_REQUEST_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok ? parseContentLength(response.headers.get(\"content-length\")) : undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Stream a GGUF (or mmproj) file from Hugging Face into pi's owned models dir. Skips a re-download\n\t * when the local file already matches the remote size; verifies size when the response reports\n\t * `content-length` and deletes the partial file on any failure or mismatch — never leaves a\n\t * corrupt/truncated weight file behind for a later load to silently misread.\n\t */\n\tasync downloadModel(\n\t\targs: { repo: string; file: string },\n\t\tonProgress?: (status: string) => void,\n\t): Promise<PrismDownloadResult> {\n\t\tconst destPath = join(this.modelsDir(), args.repo, args.file);\n\t\tconst url = `https://huggingface.co/${args.repo}/resolve/main/${args.file}`;\n\t\tmkdirSync(dirname(destPath), { recursive: true });\n\n\t\tif (this._exists(destPath)) {\n\t\t\tconst remoteSize = await this._remoteContentLength(url);\n\t\t\tif (remoteSize !== undefined && remoteSize === tryFileSizeBytes(destPath)) {\n\t\t\t\tonProgress?.(`${args.file} already downloaded (${remoteSize} bytes) — skipping.`);\n\t\t\t\treturn { ok: true, path: destPath, skipped: true };\n\t\t\t}\n\t\t}\n\n\t\tonProgress?.(`Downloading ${args.file} from ${args.repo}…`);\n\t\tconst download = await fetchRuntimeDownload(this._fetch, url);\n\t\tif (!download.ok) return download;\n\n\t\tconst expectedBytes = parseContentLength(download.response.headers.get(\"content-length\"));\n\t\tconst written = await writeRuntimeDownload(download.body as unknown as Readable, destPath);\n\t\tif (!written.ok) return written;\n\n\t\tconst actualBytes = tryFileSizeBytes(destPath);\n\t\tif (expectedBytes !== undefined && actualBytes !== expectedBytes) {\n\t\t\tremovePartialDownload(destPath);\n\t\t\treturn { ok: false, error: `size-mismatch: expected ${expectedBytes} bytes, got ${actualBytes ?? 0}` };\n\t\t}\n\n\t\tonProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);\n\t\treturn { ok: true, path: destPath };\n\t}\n\n\tprivate async _healthUp(baseUrl: string): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${baseUrl}/health`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without\n\t * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed\n\t * asset's persisted `backend` is `\"cuda\"`, not from the host's current GPU state.\n\t */\n\tasync serve(args: {\n\t\tmodelPath: string;\n\t\tmmprojPath?: string;\n\t\tport: number;\n\t\tnumCtx: number;\n\t}): Promise<PrismServeResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { ok: false, error: \"binary-missing\" };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tif (!this._exists(binaryPath)) return { ok: false, error: \"binary-missing\" };\n\n\t\tconst baseUrl = `http://127.0.0.1:${args.port}`;\n\t\tconst argv = [\n\t\t\t\"-m\",\n\t\t\targs.modelPath,\n\t\t\t...(args.mmprojPath ? [\"--mmproj\", args.mmprojPath] : []),\n\t\t\t\"--host\",\n\t\t\t\"127.0.0.1\",\n\t\t\t\"--port\",\n\t\t\tString(args.port),\n\t\t\t\"-c\",\n\t\t\tString(args.numCtx),\n\t\t\t...(manifest.backend === \"cuda\" ? [\"-ngl\", \"99\"] : []),\n\t\t];\n\n\t\tconst child = this._spawn(binaryPath, argv, {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: \"ignore\",\n\t\t\tenv: process.env,\n\t\t});\n\t\tif (child.pid) trackDetachedChildPid(child.pid);\n\t\tchild.unref?.();\n\t\tchild.on(\"exit\", () => {\n\t\t\tthis._child = undefined;\n\t\t});\n\t\tthis._child = child;\n\n\t\tfor (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {\n\t\t\tif (await this._healthUp(baseUrl)) return { ok: true, baseUrl };\n\t\t\tawait this._sleep(this._healthPollIntervalMs);\n\t\t}\n\t\tthis.stop();\n\t\treturn { ok: false, error: \"health-timeout\" };\n\t}\n\n\tstop(): { stopped: boolean } {\n\t\tconst child = this._child;\n\t\tif (!child) return { stopped: false };\n\t\tthis._child = undefined;\n\t\tif (child.pid) {\n\t\t\tuntrackDetachedChildPid(child.pid);\n\t\t\tkillProcessTree(child.pid);\n\t\t}\n\t\treturn { stopped: true };\n\t}\n\n\tisRunning(): boolean {\n\t\treturn this._child !== undefined;\n\t}\n}\n"]}
1
+ {"version":3,"file":"llamacpp-runtime.js","sourceRoot":"","sources":["../../../src/core/models/llamacpp-runtime.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,IAAI,IAAI,MAAM,EAAE,QAAQ,IAAI,UAAU,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAEpD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,kCAAkC,EAAE,MAAM,8BAA8B,CAAC;AAChG,OAAO,EAAE,eAAe,EAAE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C,CAAC;QACD,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,CAAC;IAC/C,CAAC;IAED,IAAI;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,KAAK;YAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;QACtC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,KAAK,CAAC,GAAG,EAAE,CAAC;YACf,uBAAuB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnC,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC1B,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC;IAClC,CAAC;CACD","sourcesContent":["import type { ChildProcess } from \"node:child_process\";\nimport { type Dirent, mkdirSync, readdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { arch as osArch, platform as osPlatform } from \"node:os\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { Readable } from \"node:stream\";\nimport { pipeline } from \"node:stream/promises\";\nimport { spawnProcess, waitForChildProcessWithTermination } from \"../../utils/child-process.ts\";\nimport { killProcessTree, trackDetachedChildPid, untrackDetachedChildPid } from \"../../utils/shell.ts\";\nimport { modelsDir as agentModelsDir, runtimesDir as agentRuntimesDir } from \"../agent-paths.ts\";\nimport {\n\tderiveHostLocalInferenceProfile,\n\ttype LocalInferenceProfile,\n\ttype LocalInferenceProfileMode,\n} from \"./local-inference-profile.ts\";\nimport { probePrismLlamaCppServer } from \"./prism-llamacpp-server-probe.ts\";\nimport {\n\textractZipArchive,\n\tfetchRuntimeDownload,\n\tinstallRuntimeArchive,\n\ttype ManagedRuntimeSpawn,\n\tremovePartialDownload,\n\trequireRuntimeStdin,\n\tresolveRuntimeLifecycleDependencies,\n\truntimeCommandAvailable,\n\ttryFileSizeBytes,\n\twriteRuntimeDownload,\n} from \"./runtime-process.ts\";\n\n/**\n * Managed runtime for Pi's curated Prism 1-bit and ternary GGUF models. Q1_0 is available in\n * upstream llama.cpp, while the selected group-128 Q2_0 artifacts and paired DSpark acceleration\n * still require Prism's validated build. This module therefore owns the pinned runtime install,\n * GGUF downloads, and llama-server lifecycle. Mirrors local-runtime.ts's OllamaRuntime: injectable\n * seams for fetch/spawn/exists, pi-owned directories under agentDir (runtimes/, models/),\n * detached+tracked child processes, onProgress as best-effort UI feedback, honest error taxonomies\n * instead of silent fallbacks.\n *\n * The fork publishes precompiled per-platform archives on GitHub Releases (no local build): pin a\n * release tag, download the matching asset, extract it, and locate the `llama-server` binary once\n * at install time (release archive layouts vary — bin/ vs build/bin/ at the top level — so this is\n * a deterministic one-time scan, not a fallback). The found relative path and backend (cpu/cuda)\n * are persisted to a manifest so later `detect()`/`serve()` calls never need to re-scan.\n */\n\n/**\n * Pinned Prism ML llama.cpp fork release — verified via\n * `GET https://api.github.com/repos/PrismML-Eng/llama.cpp/releases/tags/prism-b9594-38c66ad` on\n * 2026-07-18 (release exists, all assets referenced below are present on it). Bump here (and\n * re-verify with the same API call) when the provider ships new kernels.\n */\nexport const PRISM_LLAMACPP_PINNED_RELEASE = \"prism-b9594-38c66ad\";\n\nexport const PRISM_LLAMACPP_RELEASES_BASE_URL = \"https://github.com/PrismML-Eng/llama.cpp/releases/download\";\n\nexport interface PrismModelDescriptor {\n\trepo: string;\n\tfile: string;\n\tmmprojFile?: string;\n\tdisplayName: string;\n\tarchitecture: \"dense\";\n\truntime: \"prism-llamacpp\";\n\tfamily: \"bonsai\" | \"ternary-bonsai\";\n\tparameterScale: \"1.7B\" | \"4B\" | \"8B\" | \"27B\";\n\tweightFormat: \"q1_0\" | \"q2_0\";\n\tmatchedDrafter?: {\n\t\tkind: \"dspark\";\n\t\tfile: string;\n\t\tdraftMax: 4;\n\t\tminimumContext: 16_384;\n\t\tvalidatedBackend: \"cuda\";\n\t};\n}\n\n/** Curated 27B descriptor shared by the full local execution catalog. */\nexport const BONSAI_27B: PrismModelDescriptor = {\n\trepo: \"prism-ml/Bonsai-27B-gguf\",\n\tfile: \"Bonsai-27B-Q1_0.gguf\",\n\tmmprojFile: \"Bonsai-27B-mmproj-Q8_0.gguf\",\n\tdisplayName: \"Bonsai-27B (1-bit Q1_0 + vision)\",\n\tarchitecture: \"dense\",\n\truntime: \"prism-llamacpp\",\n\tfamily: \"bonsai\",\n\tparameterScale: \"27B\",\n\tweightFormat: \"q1_0\",\n\tmatchedDrafter: {\n\t\tkind: \"dspark\",\n\t\tfile: \"Bonsai-27B-dspark-Q4_1.gguf\",\n\t\tdraftMax: 4,\n\t\tminimumContext: 16_384,\n\t\tvalidatedBackend: \"cuda\",\n\t},\n};\n\nexport type PrismBackend = \"cpu\" | \"cuda\";\nexport type PrismAssetKind = \"tar-gz\" | \"zip\";\n\nexport interface PrismArchiveAsset {\n\tname: string;\n\tkind: PrismAssetKind;\n}\n\nexport interface PrismLlamaAsset extends PrismArchiveAsset {\n\tbackend: PrismBackend;\n\tcompanionAssets?: readonly PrismArchiveAsset[];\n}\n\n// Verbatim asset names from the pinned release (see PRISM_LLAMACPP_PINNED_RELEASE doc comment) —\n// hardcoded rather than templated from the tag because the provider's naming isn't fully\n// consistent across platforms (Windows CPU assets drop the tag/commit segment entirely; the\n// Windows CUDA asset uses a different build-number segment, \"b1\" not \"b9594\").\nconst LINUX_X64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-x64.tar.gz\";\nconst LINUX_ARM64_CPU_ASSET = \"llama-prism-b9594-38c66ad-bin-ubuntu-arm64.tar.gz\";\nconst LINUX_X64_CUDA_ASSET = \"llama-prism-b9594-38c66ad-bin-linux-cuda-12.4-x64.tar.gz\";\nconst MACOS_ARM64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-arm64.tar.gz\";\nconst MACOS_X64_ASSET = \"llama-prism-b9594-38c66ad-bin-macos-x64.tar.gz\";\nconst WIN_X64_CPU_ASSET = \"llama-bin-win-cpu-x64.zip\";\nconst WIN_ARM64_CPU_ASSET = \"llama-bin-win-cpu-arm64.zip\";\nconst WIN_X64_CUDA_ASSET = \"llama-prism-b1-38c66ad-bin-win-cuda-12.4-x64.zip\";\nconst WIN_X64_CUDA_COMPANION_ASSET = \"cudart-llama-bin-win-cuda-12.4-x64.zip\";\n\n/**\n * Maps a platform/arch/GPU triple to the exact Prism llama.cpp release asset for\n * {@link PRISM_LLAMACPP_PINNED_RELEASE} — verified against the real GitHub release, not guessed.\n * CPU asset by default; CUDA 12.4 is selected for x64 Linux or Windows with an NVIDIA GPU. The\n * pinned Windows build is incomplete without its matching `cudart-*` archive, so the resolver\n * returns both as one mandatory installation plan. Pure and exported so it's independently\n * testable.\n */\nexport function resolvePrismLlamaAsset(\n\tplat: string,\n\tarchitecture: string,\n\thasNvidiaGpu: boolean,\n): PrismLlamaAsset | undefined {\n\tif (plat === \"linux\") {\n\t\tif (architecture === \"x64\") {\n\t\t\treturn hasNvidiaGpu\n\t\t\t\t? { name: LINUX_X64_CUDA_ASSET, kind: \"tar-gz\", backend: \"cuda\" }\n\t\t\t\t: { name: LINUX_X64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\t}\n\t\tif (architecture === \"arm64\") return { name: LINUX_ARM64_CPU_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"darwin\") {\n\t\tif (architecture === \"arm64\") return { name: MACOS_ARM64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\tif (architecture === \"x64\") return { name: MACOS_X64_ASSET, kind: \"tar-gz\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\tif (plat === \"win32\") {\n\t\tif (architecture === \"x64\") {\n\t\t\treturn hasNvidiaGpu\n\t\t\t\t? {\n\t\t\t\t\t\tname: WIN_X64_CUDA_ASSET,\n\t\t\t\t\t\tkind: \"zip\",\n\t\t\t\t\t\tbackend: \"cuda\",\n\t\t\t\t\t\tcompanionAssets: [{ name: WIN_X64_CUDA_COMPANION_ASSET, kind: \"zip\" }],\n\t\t\t\t\t}\n\t\t\t\t: { name: WIN_X64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\t}\n\t\tif (architecture === \"arm64\") return { name: WIN_ARM64_CPU_ASSET, kind: \"zip\", backend: \"cpu\" };\n\t\treturn undefined;\n\t}\n\treturn undefined;\n}\n\nexport interface PrismDetectResult {\n\truntimeInstalled: boolean;\n\tbinaryPath?: string;\n\trelease?: string;\n}\n\nexport interface PrismDownloadResult {\n\tok: boolean;\n\tpath?: string;\n\tskipped?: boolean;\n\terror?: string;\n}\n\nexport type PrismServeResult = { ok: true; baseUrl: string } | { ok: false; error: string };\n\ninterface PrismInstallManifest {\n\trelease: string;\n\tbinaryRelPath: string;\n\tbackend: PrismBackend;\n}\n\ntype PrismExtractArchiveFn = (\n\tinput: Readable,\n\tdestDir: string,\n\tkind: PrismAssetKind,\n) => Promise<{ ok: boolean; error?: string }>;\n\nexport interface PrismLlamaCppDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: ManagedRuntimeSpawn;\n\texistsFn?: (path: string) => boolean;\n\tsleepFn?: (ms: number) => Promise<void>;\n\t/** Whether a named command exists on PATH (nvidia-smi, tar — for zip extraction on Windows). */\n\thasCommand?: (command: string) => boolean;\n\t/** Decided once at install time and persisted as `backend` — serve() reads the persisted value\n\t * so `-ngl 99` reflects what was actually installed, not the host's current GPU state. */\n\thasNvidiaGpu?: () => boolean;\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Host capacity probes used once to derive the bounded local inference profile. */\n\ttotalMemoryBytes?: () => number;\n\tlogicalCpuCount?: () => number;\n\t/** Runs the extraction step for a downloaded archive. Injectable so installManaged's\n\t * download->extract->scan orchestration is testable without a real tar/unzip pipeline; defaults\n\t * to the real spawn-based extractor. */\n\textractArchive?: PrismExtractArchiveFn;\n\t/** Health-poll bounds for serve(); default ~120s (240 * 500ms), both overridable for tests. */\n\thealthPollAttempts?: number;\n\thealthPollIntervalMs?: number;\n}\n\nconst EXTRACTION_TIMEOUT_MS = 10 * 60_000;\nconst COMMAND_KILL_GRACE_MS = 2_000;\nconst DEFAULT_HEALTH_POLL_ATTEMPTS = 240;\nconst DEFAULT_HEALTH_POLL_INTERVAL_MS = 500;\nconst HEALTH_CHECK_TIMEOUT_MS = 2_000;\nconst HEAD_REQUEST_TIMEOUT_MS = 10_000;\n\nfunction parseContentLength(header: string | null): number | undefined {\n\tif (!header) return undefined;\n\tconst value = Number(header);\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\nfunction modelIdentityConflictError(servedModelIds: readonly string[]): string {\n\treturn `model-identity-conflict:${servedModelIds.join(\",\") || \"unknown\"}`;\n}\n\nexport class PrismLlamaCppRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: ManagedRuntimeSpawn;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _hasCommand: (command: string) => boolean;\n\tprivate readonly _hasNvidiaGpu: () => boolean;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _arch: () => string;\n\tprivate readonly _extractArchiveFn: PrismExtractArchiveFn;\n\tprivate readonly _healthPollAttempts: number;\n\tprivate readonly _healthPollIntervalMs: number;\n\tprivate readonly _profile: LocalInferenceProfile;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: {\n\t\tagentDir: string;\n\t\tprofileMode?: LocalInferenceProfileMode;\n\t\tdeps?: PrismLlamaCppDeps;\n\t}) {\n\t\tthis._agentDir = args.agentDir;\n\t\t[this._fetch, this._spawn, this._exists, this._sleep] = resolveRuntimeLifecycleDependencies(args.deps);\n\t\tthis._hasCommand = args.deps?.hasCommand ?? runtimeCommandAvailable;\n\t\tthis._hasNvidiaGpu = args.deps?.hasNvidiaGpu ?? (() => this._hasCommand(\"nvidia-smi\"));\n\t\tthis._platform = args.deps?.platform ?? osPlatform;\n\t\tthis._arch = args.deps?.arch ?? osArch;\n\t\tthis._profile = deriveHostLocalInferenceProfile(args.profileMode ?? \"balanced\", args.deps);\n\t\tthis._extractArchiveFn =\n\t\t\targs.deps?.extractArchive ?? ((input, destDir, kind) => this._extractArchive(input, destDir, kind));\n\t\tthis._healthPollAttempts = args.deps?.healthPollAttempts ?? DEFAULT_HEALTH_POLL_ATTEMPTS;\n\t\tthis._healthPollIntervalMs = args.deps?.healthPollIntervalMs ?? DEFAULT_HEALTH_POLL_INTERVAL_MS;\n\t}\n\n\truntimeDir(): string {\n\t\treturn agentRuntimesDir(\"prism-llamacpp\", this._agentDir);\n\t}\n\n\tmodelsDir(): string {\n\t\treturn agentModelsDir(\"llamacpp\", this._agentDir);\n\t}\n\n\tprivate _binaryName(): string {\n\t\treturn this._platform() === \"win32\" ? \"llama-server.exe\" : \"llama-server\";\n\t}\n\n\tprivate _manifestPath(): string {\n\t\treturn join(this.runtimeDir(), \"install.json\");\n\t}\n\n\tprivate _readManifest(): PrismInstallManifest | undefined {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(readFileSync(this._manifestPath(), \"utf8\")) as {\n\t\t\t\trelease?: unknown;\n\t\t\t\tbinaryRelPath?: unknown;\n\t\t\t\tbackend?: unknown;\n\t\t\t};\n\t\t\tif (\n\t\t\t\ttypeof parsed.release === \"string\" &&\n\t\t\t\ttypeof parsed.binaryRelPath === \"string\" &&\n\t\t\t\t(parsed.backend === \"cpu\" || parsed.backend === \"cuda\")\n\t\t\t) {\n\t\t\t\treturn { release: parsed.release, binaryRelPath: parsed.binaryRelPath, backend: parsed.backend };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\tprivate _writeManifest(manifest: PrismInstallManifest): void {\n\t\twriteFileSync(this._manifestPath(), JSON.stringify(manifest, null, \"\\t\"));\n\t}\n\n\tasync detect(): Promise<PrismDetectResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { runtimeInstalled: false };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tconst runtimeInstalled = this._exists(binaryPath);\n\t\treturn {\n\t\t\truntimeInstalled,\n\t\t\tbinaryPath: runtimeInstalled ? binaryPath : undefined,\n\t\t\trelease: manifest.release,\n\t\t};\n\t}\n\n\t/** One-time recursive scan for the `llama-server` binary inside a freshly extracted release\n\t * archive — layouts vary (bin/ vs build/bin/ at the top level) across platforms/backends, so\n\t * this locates it deterministically instead of assuming a fixed path, then the result is\n\t * persisted so later calls never need to re-scan. */\n\tprivate _findBinaryRelPath(root: string): string | undefined {\n\t\tconst targetName = this._binaryName();\n\t\tconst stack: string[] = [root];\n\t\twhile (stack.length > 0) {\n\t\t\tconst dir = stack.pop();\n\t\t\tif (dir === undefined) continue;\n\t\t\tlet entries: Dirent[];\n\t\t\ttry {\n\t\t\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const entry of entries) {\n\t\t\t\tconst full = join(dir, entry.name);\n\t\t\t\tif (entry.isDirectory()) stack.push(full);\n\t\t\t\telse if (entry.isFile() && entry.name === targetName) return relative(root, full);\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Download the pinned release asset for this host and extract it (consent-gated by the caller,\n\t * same contract as OllamaRuntime#installManaged — this method only does the mechanical\n\t * download+extract+locate and reports the outcome honestly). No compiler toolchain required:\n\t * the fork ships prebuilt binaries.\n\t */\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tconst asset = resolvePrismLlamaAsset(this._platform(), this._arch(), this._hasNvidiaGpu());\n\t\tif (!asset) return { ok: false, error: \"unsupported-platform\" };\n\n\t\tconst archives: readonly PrismArchiveAsset[] = [asset, ...(asset.companionAssets ?? [])];\n\t\tfor (const archive of archives) {\n\t\t\tconst downloadUrl = `${PRISM_LLAMACPP_RELEASES_BASE_URL}/${PRISM_LLAMACPP_PINNED_RELEASE}/${archive.name}`;\n\t\t\tconst extracted = await this._installArchive(downloadUrl, archive, onProgress);\n\t\t\tif (!extracted.ok) return extracted;\n\t\t}\n\n\t\tonProgress?.(\"Locating llama-server binary…\");\n\t\tconst destDir = this.runtimeDir();\n\t\tconst binaryRelPath = this._findBinaryRelPath(destDir);\n\t\tif (!binaryRelPath) {\n\t\t\treturn { ok: false, error: \"binary-missing: no llama-server binary found in the extracted archive\" };\n\t\t}\n\n\t\tthis._writeManifest({ release: PRISM_LLAMACPP_PINNED_RELEASE, binaryRelPath, backend: asset.backend });\n\t\tonProgress?.(\"Prism llama.cpp runtime installed.\");\n\t\treturn { ok: true };\n\t}\n\n\tprivate _installArchive(\n\t\tdownloadUrl: string,\n\t\tasset: PrismArchiveAsset,\n\t\tonProgress?: (status: string) => void,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\treturn installRuntimeArchive(\n\t\t\tthis._fetch,\n\t\t\tdownloadUrl,\n\t\t\tthis.runtimeDir(),\n\t\t\tasset,\n\t\t\tthis._extractArchiveFn,\n\t\t\tonProgress,\n\t\t);\n\t}\n\n\tprivate async _extractArchive(\n\t\tinput: Readable,\n\t\tdestDir: string,\n\t\tkind: PrismAssetKind,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\treturn kind === \"zip\" ? await this._extractZip(input, destDir) : await this._extractTarGz(input, destDir);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t}\n\n\tprivate async _extractTarGz(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\tconst tarProc = spawnProcess(\"tar\", [\"-xzf\", \"-\", \"-C\", destDir], {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: [\"pipe\", \"ignore\", \"ignore\"],\n\t\t});\n\t\tconst terminationController = new AbortController();\n\t\tconst processWait = waitForChildProcessWithTermination(tarProc, {\n\t\t\tsignal: terminationController.signal,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t\ttry {\n\t\t\tawait pipeline(input, requireRuntimeStdin(tarProc, \"tar\"));\n\t\t} catch (error) {\n\t\t\tterminationController.abort();\n\t\t\tawait processWait.catch(() => {});\n\t\t\tthrow error;\n\t\t}\n\t\tconst terminal = await processWait;\n\t\tif (terminal.reason === \"timeout\" || terminal.code !== 0) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\terror:\n\t\t\t\t\tterminal.reason === \"timeout\"\n\t\t\t\t\t\t? `extract-fail: tar extraction timed out after ${EXTRACTION_TIMEOUT_MS}ms`\n\t\t\t\t\t\t: `extract-fail: tar exited with code ${terminal.code ?? \"unknown\"}`,\n\t\t\t};\n\t\t}\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractZip(input: Readable, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\treturn extractZipArchive({\n\t\t\tinput,\n\t\t\tdestDir,\n\t\t\ttempPrefix: \"prism-llamacpp-download\",\n\t\t\tplatform: this._platform,\n\t\t\thasCommand: this._hasCommand,\n\t\t\ttimeoutMs: EXTRACTION_TIMEOUT_MS,\n\t\t\tkillGraceMs: COMMAND_KILL_GRACE_MS,\n\t\t});\n\t}\n\n\tprivate async _remoteContentLength(url: string): Promise<number | undefined> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(url, {\n\t\t\t\tmethod: \"HEAD\",\n\t\t\t\tsignal: AbortSignal.timeout(HEAD_REQUEST_TIMEOUT_MS),\n\t\t\t});\n\t\t\treturn response.ok ? parseContentLength(response.headers.get(\"content-length\")) : undefined;\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n\n\t/**\n\t * Stream a GGUF (or mmproj) file from Hugging Face into pi's owned models dir. Skips a re-download\n\t * when the local file already matches the remote size; verifies size when the response reports\n\t * `content-length` and deletes the partial file on any failure or mismatch — never leaves a\n\t * corrupt/truncated weight file behind for a later load to silently misread.\n\t */\n\tasync downloadModel(\n\t\targs: { repo: string; file: string },\n\t\tonProgress?: (status: string) => void,\n\t): Promise<PrismDownloadResult> {\n\t\tconst destPath = join(this.modelsDir(), args.repo, args.file);\n\t\tconst url = `https://huggingface.co/${args.repo}/resolve/main/${args.file}`;\n\t\tmkdirSync(dirname(destPath), { recursive: true });\n\n\t\tif (this._exists(destPath)) {\n\t\t\tconst remoteSize = await this._remoteContentLength(url);\n\t\t\tif (remoteSize !== undefined && remoteSize === tryFileSizeBytes(destPath)) {\n\t\t\t\tonProgress?.(`${args.file} already downloaded (${remoteSize} bytes) — skipping.`);\n\t\t\t\treturn { ok: true, path: destPath, skipped: true };\n\t\t\t}\n\t\t}\n\n\t\tonProgress?.(`Downloading ${args.file} from ${args.repo}…`);\n\t\tconst download = await fetchRuntimeDownload(this._fetch, url);\n\t\tif (!download.ok) return download;\n\n\t\tconst expectedBytes = parseContentLength(download.response.headers.get(\"content-length\"));\n\t\tconst written = await writeRuntimeDownload(download.body as unknown as Readable, destPath);\n\t\tif (!written.ok) return written;\n\n\t\tconst actualBytes = tryFileSizeBytes(destPath);\n\t\tif (expectedBytes !== undefined && actualBytes !== expectedBytes) {\n\t\t\tremovePartialDownload(destPath);\n\t\t\treturn { ok: false, error: `size-mismatch: expected ${expectedBytes} bytes, got ${actualBytes ?? 0}` };\n\t\t}\n\n\t\tonProgress?.(`${args.file} downloaded (${actualBytes ?? 0} bytes).`);\n\t\treturn { ok: true, path: destPath };\n\t}\n\n\t/**\n\t * Spawn `llama-server` detached+tracked (killed on parent shutdown even if pi crashes without\n\t * calling stop()) and poll `/health` until ready. `-ngl 99` is only added when the installed\n\t * asset's persisted `backend` is `\"cuda\"`, not from the host's current GPU state.\n\t */\n\tasync serve(args: {\n\t\tmodelPath: string;\n\t\tmodelAlias: string;\n\t\tmmprojPath?: string;\n\t\tport: number;\n\t\tnumCtx: number;\n\t}): Promise<PrismServeResult> {\n\t\tconst manifest = this._readManifest();\n\t\tif (!manifest) return { ok: false, error: \"binary-missing\" };\n\t\tconst binaryPath = join(this.runtimeDir(), manifest.binaryRelPath);\n\t\tif (!this._exists(binaryPath)) return { ok: false, error: \"binary-missing\" };\n\n\t\tconst baseUrl = `http://127.0.0.1:${args.port}`;\n\t\tconst existing = await probePrismLlamaCppServer(baseUrl, args.modelAlias, this._fetch, HEALTH_CHECK_TIMEOUT_MS);\n\t\tif (existing.status === \"matching\") return { ok: true, baseUrl };\n\t\tif (existing.status === \"conflict\" && !this._child) {\n\t\t\treturn { ok: false, error: modelIdentityConflictError(existing.servedModelIds) };\n\t\t}\n\t\tif (this._child) this.stop();\n\t\tconst argv = [\n\t\t\t\"-m\",\n\t\t\targs.modelPath,\n\t\t\t\"--alias\",\n\t\t\targs.modelAlias,\n\t\t\t...(args.mmprojPath ? [\"--mmproj\", args.mmprojPath] : []),\n\t\t\t\"--host\",\n\t\t\t\"127.0.0.1\",\n\t\t\t\"--port\",\n\t\t\tString(args.port),\n\t\t\t\"-c\",\n\t\t\tString(args.numCtx),\n\t\t\t\"--cache-ram\",\n\t\t\tString(this._profile.promptCacheMiB),\n\t\t\t\"-np\",\n\t\t\tString(this._profile.parallelRequests),\n\t\t\t\"-fa\",\n\t\t\tthis._profile.flashAttention ? \"on\" : \"off\",\n\t\t\t\"-ctk\",\n\t\t\tthis._profile.kvCacheType,\n\t\t\t\"-ctv\",\n\t\t\tthis._profile.kvCacheType,\n\t\t\t\"-t\",\n\t\t\tString(this._profile.generationThreads),\n\t\t\t\"-tb\",\n\t\t\tString(this._profile.batchThreads),\n\t\t\t...(manifest.backend === \"cuda\" ? [\"-ngl\", \"99\"] : []),\n\t\t];\n\n\t\tconst child = this._spawn(binaryPath, argv, {\n\t\t\tdetached: process.platform !== \"win32\",\n\t\t\tstdio: \"ignore\",\n\t\t\tenv: process.env,\n\t\t});\n\t\tif (child.pid) trackDetachedChildPid(child.pid);\n\t\tchild.unref?.();\n\t\tchild.on(\"exit\", () => {\n\t\t\tif (this._child === child) {\n\t\t\t\tthis._child = undefined;\n\t\t\t}\n\t\t});\n\t\tthis._child = child;\n\n\t\tfor (let attempt = 0; attempt < this._healthPollAttempts; attempt++) {\n\t\t\tconst probe = await probePrismLlamaCppServer(baseUrl, args.modelAlias, this._fetch, HEALTH_CHECK_TIMEOUT_MS);\n\t\t\tif (probe.status === \"matching\") return { ok: true, baseUrl };\n\t\t\tif (probe.status === \"conflict\") {\n\t\t\t\tthis.stop();\n\t\t\t\treturn { ok: false, error: modelIdentityConflictError(probe.servedModelIds) };\n\t\t\t}\n\t\t\tawait this._sleep(this._healthPollIntervalMs);\n\t\t}\n\t\tthis.stop();\n\t\treturn { ok: false, error: \"health-timeout\" };\n\t}\n\n\tstop(): { stopped: boolean } {\n\t\tconst child = this._child;\n\t\tif (!child) return { stopped: false };\n\t\tthis._child = undefined;\n\t\tif (child.pid) {\n\t\t\tuntrackDetachedChildPid(child.pid);\n\t\t\tkillProcessTree(child.pid);\n\t\t}\n\t\treturn { stopped: true };\n\t}\n\n\tisRunning(): boolean {\n\t\treturn this._child !== undefined;\n\t}\n}\n"]}
@@ -0,0 +1,24 @@
1
+ import { type PrismBackend, type PrismModelDescriptor } from "./llamacpp-runtime.ts";
2
+ /**
3
+ * The complete model set Pi may manage through its pinned Prism llama.cpp adapter. Unknown models
4
+ * never enter this path. Every descriptor pins the exact GGUF artifact so user-provided quant
5
+ * suffixes cannot silently select a different format or runtime.
6
+ */
7
+ export declare const PRISM_LLAMACPP_DESCRIPTORS: Readonly<Record<string, PrismModelDescriptor>>;
8
+ export declare function resolvePrismLocalModelDescriptor(modelId: string): PrismModelDescriptor | undefined;
9
+ export type PrismSpeculationPlan = {
10
+ enabled: true;
11
+ kind: "dspark";
12
+ drafterFile: string;
13
+ draftMax: 4;
14
+ minimumContext: 16_384;
15
+ } | {
16
+ enabled: false;
17
+ reason: "not-requested" | "model-has-no-matched-drafter" | "backend-not-validated";
18
+ };
19
+ /** MTP is deliberately absent: Bonsai acceleration uses only the model's exact paired drafter. */
20
+ export declare function planPrismSpeculation(descriptor: PrismModelDescriptor, args: {
21
+ requested: boolean;
22
+ backend: PrismBackend;
23
+ }): PrismSpeculationPlan;
24
+ //# sourceMappingURL=local-execution-planner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-execution-planner.d.ts","sourceRoot":"","sources":["../../../src/core/models/local-execution-planner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAE,KAAK,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AA2FjG;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CASpF,CAAC;AAMH,wBAAgB,gCAAgC,CAAC,OAAO,EAAE,MAAM,GAAG,oBAAoB,GAAG,SAAS,CAElG;AAED,MAAM,MAAM,oBAAoB,GAC7B;IACA,OAAO,EAAE,IAAI,CAAC;IACd,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,CAAC,CAAC;IACZ,cAAc,EAAE,MAAM,CAAC;CACtB,GACD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,eAAe,GAAG,8BAA8B,GAAG,uBAAuB,CAAA;CAAE,CAAC;AAE1G,kGAAkG;AAClG,wBAAgB,oBAAoB,CACnC,UAAU,EAAE,oBAAoB,EAChC,IAAI,EAAE;IAAE,SAAS,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,YAAY,CAAA;CAAE,GACjD,oBAAoB,CAatB"}