@caupulican/pi-adaptative 0.80.99 → 0.80.102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +203 -0
- package/dist/core/agent-session.d.ts +72 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +437 -46
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/autonomy/envelope-enforcement.d.ts +5 -0
- package/dist/core/autonomy/envelope-enforcement.d.ts.map +1 -1
- package/dist/core/autonomy/envelope-enforcement.js +28 -4
- package/dist/core/autonomy/envelope-enforcement.js.map +1 -1
- package/dist/core/autonomy/path-scope.d.ts.map +1 -1
- package/dist/core/autonomy/path-scope.js +33 -2
- package/dist/core/autonomy/path-scope.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +15 -7
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/context/brain-curator.d.ts +10 -0
- package/dist/core/context/brain-curator.d.ts.map +1 -1
- package/dist/core/context/brain-curator.js +19 -2
- package/dist/core/context/brain-curator.js.map +1 -1
- package/dist/core/context-gc.d.ts +8 -3
- package/dist/core/context-gc.d.ts.map +1 -1
- package/dist/core/context-gc.js +5 -0
- package/dist/core/context-gc.js.map +1 -1
- package/dist/core/delegation/worker-actions.d.ts +0 -10
- package/dist/core/delegation/worker-actions.d.ts.map +1 -1
- package/dist/core/delegation/worker-actions.js +5 -7
- package/dist/core/delegation/worker-actions.js.map +1 -1
- package/dist/core/delegation/worker-result.d.ts +6 -0
- package/dist/core/delegation/worker-result.d.ts.map +1 -1
- package/dist/core/delegation/worker-result.js +10 -4
- package/dist/core/delegation/worker-result.js.map +1 -1
- package/dist/core/delegation/worker-runner.d.ts +3 -0
- package/dist/core/delegation/worker-runner.d.ts.map +1 -1
- package/dist/core/delegation/worker-runner.js +6 -1
- package/dist/core/delegation/worker-runner.js.map +1 -1
- package/dist/core/doctor.d.ts +66 -0
- package/dist/core/doctor.d.ts.map +1 -0
- package/dist/core/doctor.js +144 -0
- package/dist/core/doctor.js.map +1 -0
- package/dist/core/learning/learning-audit.d.ts +13 -1
- package/dist/core/learning/learning-audit.d.ts.map +1 -1
- package/dist/core/learning/learning-audit.js +22 -1
- package/dist/core/learning/learning-audit.js.map +1 -1
- package/dist/core/learning/learning-gate.d.ts +3 -0
- package/dist/core/learning/learning-gate.d.ts.map +1 -1
- package/dist/core/learning/learning-gate.js +13 -4
- package/dist/core/learning/learning-gate.js.map +1 -1
- package/dist/core/models/default-model-suggestions.d.ts +34 -0
- package/dist/core/models/default-model-suggestions.d.ts.map +1 -0
- package/dist/core/models/default-model-suggestions.js +58 -0
- package/dist/core/models/default-model-suggestions.js.map +1 -0
- package/dist/core/models/local-registration.d.ts +2 -0
- package/dist/core/models/local-registration.d.ts.map +1 -1
- package/dist/core/models/local-registration.js +2 -1
- package/dist/core/models/local-registration.js.map +1 -1
- package/dist/core/models/local-runtime.d.ts +76 -2
- package/dist/core/models/local-runtime.d.ts.map +1 -1
- package/dist/core/models/local-runtime.js +233 -21
- package/dist/core/models/local-runtime.js.map +1 -1
- package/dist/core/profile-resource-selection.d.ts +15 -3
- package/dist/core/profile-resource-selection.d.ts.map +1 -1
- package/dist/core/profile-resource-selection.js +19 -9
- package/dist/core/profile-resource-selection.js.map +1 -1
- package/dist/core/resource-loader.d.ts +20 -0
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +42 -16
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/settings-manager.d.ts +30 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +65 -3
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +14 -0
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +8 -0
- package/dist/core/skills.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/tools/fff-search-backend.d.ts +34 -1
- package/dist/core/tools/fff-search-backend.d.ts.map +1 -1
- package/dist/core/tools/fff-search-backend.js +43 -4
- package/dist/core/tools/fff-search-backend.js.map +1 -1
- package/dist/core/tools/find.d.ts +16 -0
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +17 -3
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts +20 -0
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +15 -3
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +17 -0
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.d.ts +6 -2
- package/dist/modes/interactive/components/fitness-role-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/fitness-role-selector.js +55 -45
- package/dist/modes/interactive/components/fitness-role-selector.js.map +1 -1
- package/dist/modes/interactive/components/model-suggestion-selector.d.ts +13 -0
- package/dist/modes/interactive/components/model-suggestion-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/model-suggestion-selector.js +40 -0
- package/dist/modes/interactive/components/model-suggestion-selector.js.map +1 -0
- package/dist/modes/interactive/components/profile-resource-editor.d.ts +3 -0
- package/dist/modes/interactive/components/profile-resource-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/profile-resource-editor.js +16 -2
- package/dist/modes/interactive/components/profile-resource-editor.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +98 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +8 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +57 -10
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/utils/safe-write-stream.d.ts +8 -0
- package/dist/utils/safe-write-stream.d.ts.map +1 -1
- package/dist/utils/safe-write-stream.js +32 -0
- package/dist/utils/safe-write-stream.js.map +1 -1
- package/dist/utils/tools-manager.d.ts +92 -2
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +172 -32
- package/dist/utils/tools-manager.js.map +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +12 -36
- package/package.json +4 -4
|
@@ -25,6 +25,22 @@ export interface InstalledLocalModel {
|
|
|
25
25
|
name: string;
|
|
26
26
|
sizeBytes: number;
|
|
27
27
|
}
|
|
28
|
+
/** Pinned ollama release for the managed installer below. Bump here when needed — one constant. */
|
|
29
|
+
export declare const OLLAMA_PINNED_VERSION = "0.31.1";
|
|
30
|
+
export type OllamaAssetKind = "tar-zst" | "tar-gz" | "zip";
|
|
31
|
+
export interface OllamaReleaseAsset {
|
|
32
|
+
name: string;
|
|
33
|
+
kind: OllamaAssetKind;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Maps a platform/arch pair (node's `os.platform()`/`os.arch()`) to the exact ollama release asset
|
|
37
|
+
* name for {@link OLLAMA_PINNED_VERSION} — verified against the real ollama/ollama GitHub release
|
|
38
|
+
* and its own install.sh, not guessed: darwin ships a CLI `.tgz` (the `.app`/`.dmg`/`Ollama-*.zip`
|
|
39
|
+
* assets are the separate GUI-app installer, not what we want), linux ships `.tar.zst` per arch,
|
|
40
|
+
* windows ships `.zip` per arch. Pure and exported so it's independently testable and reusable
|
|
41
|
+
* (e.g. by a future doctor-driven managed install) without touching OllamaRuntime.
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolveOllamaAsset(plat: string, architecture: string): OllamaReleaseAsset | undefined;
|
|
28
44
|
export interface LocalRuntimeDeps {
|
|
29
45
|
fetchFn?: typeof fetch;
|
|
30
46
|
spawnFn?: (command: string, args: string[], options: {
|
|
@@ -34,6 +50,22 @@ export interface LocalRuntimeDeps {
|
|
|
34
50
|
envPath?: string;
|
|
35
51
|
homeDir?: string;
|
|
36
52
|
sleepFn?: (ms: number) => Promise<void>;
|
|
53
|
+
/** os.platform()/os.arch() equivalents — injectable so asset resolution is testable per platform. */
|
|
54
|
+
platform?: () => string;
|
|
55
|
+
arch?: () => string;
|
|
56
|
+
/** Node's built-in zstd decompress transform, when THIS runtime's node:zlib has it — undefined
|
|
57
|
+
* forces the system-zstd fallback. Injectable so tests can force either path deterministically
|
|
58
|
+
* instead of depending on whatever Node happens to be running the test. */
|
|
59
|
+
createZstdDecompress?: () => NodeJS.ReadWriteStream;
|
|
60
|
+
/** Whether a named command exists on PATH (e.g. system `zstd`), for the extraction fallback. */
|
|
61
|
+
hasCommand?: (command: string) => boolean;
|
|
62
|
+
/** Runs the extraction step for a downloaded archive. Injectable so installManaged's
|
|
63
|
+
* download->extract->verify orchestration is testable without a real tar/zstd/unzip pipeline;
|
|
64
|
+
* defaults to the real spawn-based extractor. */
|
|
65
|
+
extractArchive?: (input: NodeJS.ReadableStream, destDir: string, kind: OllamaAssetKind) => Promise<{
|
|
66
|
+
ok: boolean;
|
|
67
|
+
error?: string;
|
|
68
|
+
}>;
|
|
37
69
|
}
|
|
38
70
|
export declare class OllamaRuntime {
|
|
39
71
|
private readonly _agentDir;
|
|
@@ -44,6 +76,11 @@ export declare class OllamaRuntime {
|
|
|
44
76
|
private readonly _envPath;
|
|
45
77
|
private readonly _homeDir;
|
|
46
78
|
private readonly _sleep;
|
|
79
|
+
private readonly _platform;
|
|
80
|
+
private readonly _arch;
|
|
81
|
+
private readonly _createZstdDecompress;
|
|
82
|
+
private readonly _hasCommand;
|
|
83
|
+
private readonly _extractArchiveOverride;
|
|
47
84
|
private _child;
|
|
48
85
|
constructor(args: {
|
|
49
86
|
agentDir: string;
|
|
@@ -55,16 +92,53 @@ export declare class OllamaRuntime {
|
|
|
55
92
|
private _findBinary;
|
|
56
93
|
private _serverUp;
|
|
57
94
|
detect(): Promise<LocalRuntimeStatus>;
|
|
58
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* Manual-steps fallback text — shown when the managed install below wasn't offered (headless),
|
|
97
|
+
* was declined, or itself failed. #31 reversed the stance for ollama specifically: pi CAN install
|
|
98
|
+
* it for you now (consent-gated, a controlled download into pi's own runtimes dir, never
|
|
99
|
+
* `curl | sh`) — these steps are the alternative for when that path isn't taken.
|
|
100
|
+
*/
|
|
59
101
|
installGuide(): string[];
|
|
102
|
+
/** Which decompressor to use for a `.tar.zst` asset — native (this Node's own zlib) is always
|
|
103
|
+
* preferred when available, even if a system `zstd` ALSO exists, since it needs no external
|
|
104
|
+
* dependency at all. Verified per-runtime by feature detection (see the constructor), not by
|
|
105
|
+
* guessing from a Node version number. */
|
|
106
|
+
private _chooseZstdStrategy;
|
|
107
|
+
/**
|
|
108
|
+
* Managed install (#31): a consent-gated, controlled download of the pinned ollama release into
|
|
109
|
+
* pi's OWN runtimes dir (`<agentDir>/runtimes/ollama/`) — never `curl | sh`, never a second
|
|
110
|
+
* server instance, and distinct from {@link start}'s owned MODEL storage (this is the binary
|
|
111
|
+
* itself). Callers (the router's consent flow, and later a doctor-driven install) own the
|
|
112
|
+
* consent/UI; this method only does the mechanical download+extract+verify and reports the
|
|
113
|
+
* outcome honestly. `onProgress` is best-effort UI feedback, not a completion signal.
|
|
114
|
+
*/
|
|
115
|
+
installManaged(onProgress?: (status: string) => void): Promise<{
|
|
116
|
+
ok: boolean;
|
|
117
|
+
error?: string;
|
|
118
|
+
}>;
|
|
119
|
+
private _extractArchive;
|
|
120
|
+
private _extractTar;
|
|
121
|
+
private _extractZip;
|
|
122
|
+
private _spawnAndPoll;
|
|
60
123
|
/**
|
|
61
124
|
* Start a pi-managed serve with OWNED storage and the hardened env verified on this class of
|
|
62
|
-
* hardware. No-op (reported) when a server already responds — pi never double-serves.
|
|
125
|
+
* hardware. No-op (reported) when a server already responds — pi never double-serves. Used by
|
|
126
|
+
* `/models add` et al, where isolated per-model-pull storage is the point.
|
|
63
127
|
*/
|
|
64
128
|
start(): Promise<{
|
|
65
129
|
started: boolean;
|
|
66
130
|
reason: string;
|
|
67
131
|
}>;
|
|
132
|
+
/**
|
|
133
|
+
* Start a serve that REUSES the user's own existing models directory (no `OLLAMA_MODELS`
|
|
134
|
+
* override — falls through to Ollama's own default, `~/.ollama`), for callers that must see the
|
|
135
|
+
* user's already-pulled models rather than pi's isolated/owned storage. Same idempotency and
|
|
136
|
+
* hardened perf env as {@link start}; the only difference is which storage the server sees.
|
|
137
|
+
*/
|
|
138
|
+
startReuseExisting(): Promise<{
|
|
139
|
+
started: boolean;
|
|
140
|
+
reason: string;
|
|
141
|
+
}>;
|
|
68
142
|
/** Resource hygiene only: stops the pi-managed serve process; never deletes anything. */
|
|
69
143
|
stop(): {
|
|
70
144
|
stopped: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-runtime.d.ts","sourceRoot":"","sources":["../../../src/core/models/local-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAS,MAAM,oBAAoB,CAAC;AAK9D;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,kBAAkB;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;IAC9C,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,6FAA6F;IAC7F,WAAW,EAAE,OAAO,CAAC;IACrB,uEAAuE;IACvE,cAAc,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACl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yFAAyF;IACzF,YAAY,IAAI,MAAM,EAAE,CASvB;IAED;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CA0B3D;IAED,yFAAyF;IACzF,IAAI,IAAI;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAS3B;IAEK,IAAI,IAAI,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAO3C;IAED,qFAAqF;IAC/E,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,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,CAiDvG;IAED,6FAA2F;IACrF,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAiBlE;CACD","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { existsSync, mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { delimiter, join } from \"node:path\";\n\n/**\n * Local model runtime manager (local-model-lifecycle-design.md): Ollama first, interface kept\n * runtime-agnostic. Pi SPAWNS the serve process itself with OWNED model storage\n * (`OLLAMA_MODELS=<agentDir>/models/ollama`) so every downloaded weight lives inside pi's tree —\n * per-model disk accounting is trivial and full cleanup is one directory. If a system server is\n * already running, pi uses it instead of double-installing, with the honest tradeoff surfaced:\n * storage then lives in the system daemon's dir and removal control is limited to `ollama rm`.\n *\n * Hard boundaries (design \"Hard boundaries\"): lifecycle actions are USER commands only — this\n * module is never exposed as a model-invokable tool; install is GUIDE MODE (exact manual steps,\n * never `curl | sh`); removal is explicit, disclosed, and never automatic.\n */\n\nexport interface LocalRuntimeStatus {\n\tbinaryPath?: string;\n\tbinarySource?: \"system\" | \"user\" | \"pi-owned\";\n\tserverUp: boolean;\n\tserverUrl: string;\n\t/** True when the responding server is a child process pi spawned (owned storage applies). */\n\tmanagedByPi: boolean;\n\t/** Owned weights directory (only meaningful for pi-managed serves). */\n\townedModelsDir: string;\n}\n\nexport interface InstalledLocalModel {\n\tname: string;\n\tsizeBytes: number;\n}\n\nexport interface LocalRuntimeDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: (\n\t\tcommand: string,\n\t\targs: string[],\n\t\toptions: { env: NodeJS.ProcessEnv },\n\t) => Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\">;\n\texistsFn?: (path: string) => boolean;\n\tenvPath?: string;\n\thomeDir?: string;\n\tsleepFn?: (ms: number) => Promise<void>;\n}\n\nconst DEFAULT_BASE_URL = \"http://127.0.0.1:11434\";\nconst HEALTH_TIMEOUT_MS = 2_000;\nconst START_POLL_ATTEMPTS = 20;\nconst START_POLL_INTERVAL_MS = 500;\n\nexport class OllamaRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _baseUrl: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: NonNullable<LocalRuntimeDeps[\"spawnFn\"]>;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _envPath: string;\n\tprivate readonly _homeDir: string;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: { agentDir: string; baseUrl?: string; deps?: LocalRuntimeDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\tthis._baseUrl = (args.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/$/, \"\");\n\t\tthis._fetch = args.deps?.fetchFn ?? fetch;\n\t\tthis._spawn = args.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, options));\n\t\tthis._exists = args.deps?.existsFn ?? existsSync;\n\t\tthis._envPath = args.deps?.envPath ?? process.env.PATH ?? \"\";\n\t\tthis._homeDir = args.deps?.homeDir ?? homedir();\n\t\tthis._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));\n\t}\n\n\tget baseUrl(): string {\n\t\treturn this._baseUrl;\n\t}\n\n\townedModelsDir(): string {\n\t\treturn join(this._agentDir, \"models\", \"ollama\");\n\t}\n\n\tprivate _findBinary(): { path: string; source: \"system\" | \"user\" | \"pi-owned\" } | undefined {\n\t\tconst piOwned = join(this._agentDir, \"runtimes\", \"ollama\", \"bin\", \"ollama\");\n\t\tif (this._exists(piOwned)) return { path: piOwned, source: \"pi-owned\" };\n\t\tconst userLevel = join(this._homeDir, \".local\", \"share\", \"ollama-dist\", \"bin\", \"ollama\");\n\t\tif (this._exists(userLevel)) return { path: userLevel, source: \"user\" };\n\t\tfor (const dir of this._envPath.split(delimiter)) {\n\t\t\tif (!dir) continue;\n\t\t\tconst candidate = join(dir, \"ollama\");\n\t\t\tif (this._exists(candidate)) return { path: candidate, source: \"system\" };\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate async _serverUp(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/tags`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_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\tasync detect(): Promise<LocalRuntimeStatus> {\n\t\tconst binary = this._findBinary();\n\t\tconst serverUp = await this._serverUp();\n\t\treturn {\n\t\t\tbinaryPath: binary?.path,\n\t\t\tbinarySource: binary?.source,\n\t\t\tserverUp,\n\t\t\tserverUrl: this._baseUrl,\n\t\t\tmanagedByPi: this._child !== undefined && serverUp,\n\t\t\townedModelsDir: this.ownedModelsDir(),\n\t\t};\n\t}\n\n\t/** GUIDE MODE: exact manual steps, printed, never executed (no `curl | sh`, no sudo). */\n\tinstallGuide(): string[] {\n\t\treturn [\n\t\t\t\"Ollama is not installed. Pi never runs installers itself — manual steps (user-level, no sudo):\",\n\t\t\t\" 1. Download the pinned release archive for your platform:\",\n\t\t\t\" https://github.com/ollama/ollama/releases (asset: ollama-linux-amd64.tar.zst or your platform)\",\n\t\t\t` 2. Extract it to ${join(this._homeDir, \".local\", \"share\", \"ollama-dist\")}`,\n\t\t\t\" 3. Re-run your /models command - pi detects the binary automatically.\",\n\t\t\t\"Alternatively install system-wide from https://ollama.com/download and pi will use the system server.\",\n\t\t];\n\t}\n\n\t/**\n\t * Start a pi-managed serve with OWNED storage and the hardened env verified on this class of\n\t * hardware. No-op (reported) when a server already responds — pi never double-serves.\n\t */\n\tasync start(): Promise<{ started: boolean; reason: string }> {\n\t\tif (await this._serverUp()) {\n\t\t\treturn { started: false, reason: this._child ? \"already_running_managed\" : \"already_running_system\" };\n\t\t}\n\t\tconst binary = this._findBinary();\n\t\tif (!binary) return { started: false, reason: \"binary_missing\" };\n\t\tmkdirSync(this.ownedModelsDir(), { recursive: true });\n\t\tconst host = this._baseUrl.replace(/^https?:\\/\\//, \"\");\n\t\tthis._child = this._spawn(binary.path, [\"serve\"], {\n\t\t\tenv: {\n\t\t\t\t...process.env,\n\t\t\t\tOLLAMA_HOST: host,\n\t\t\t\tOLLAMA_MODELS: this.ownedModelsDir(),\n\t\t\t\tOLLAMA_FLASH_ATTENTION: \"1\",\n\t\t\t\tOLLAMA_KV_CACHE_TYPE: \"q8_0\",\n\t\t\t\tOLLAMA_NUM_PARALLEL: \"1\",\n\t\t\t\tOLLAMA_MAX_LOADED_MODELS: \"3\",\n\t\t\t},\n\t\t});\n\t\tthis._child.unref?.();\n\t\tfor (let attempt = 0; attempt < START_POLL_ATTEMPTS; attempt++) {\n\t\t\tif (await this._serverUp()) return { started: true, reason: \"started\" };\n\t\t\tawait this._sleep(START_POLL_INTERVAL_MS);\n\t\t}\n\t\tthis.stop();\n\t\treturn { started: false, reason: \"health_check_timeout\" };\n\t}\n\n\t/** Resource hygiene only: stops the pi-managed serve process; never deletes anything. */\n\tstop(): { stopped: boolean } {\n\t\tif (!this._child) return { stopped: false };\n\t\ttry {\n\t\t\tthis._child.kill(\"SIGTERM\");\n\t\t} catch {\n\t\t\t// already gone\n\t\t}\n\t\tthis._child = undefined;\n\t\treturn { stopped: true };\n\t}\n\n\tasync list(): Promise<InstalledLocalModel[]> {\n\t\tconst response = await this._fetch(`${this._baseUrl}/api/tags`, { signal: AbortSignal.timeout(10_000) });\n\t\tif (!response.ok) throw new Error(`ollama list failed: HTTP ${response.status}`);\n\t\tconst data = (await response.json()) as { models?: Array<{ name?: string; size?: number }> };\n\t\treturn (data.models ?? [])\n\t\t\t.filter((model): model is { name: string; size?: number } => typeof model.name === \"string\")\n\t\t\t.map((model) => ({ name: model.name, sizeBytes: model.size ?? 0 }));\n\t}\n\n\t/** Pull a model through the server API (weights land in the SERVER's models dir). */\n\tasync pull(ref: string, onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/pull`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: ref, stream: true }),\n\t\t\t});\n\t\t\tif (!response.ok || !response.body) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror: `pull failed: HTTP ${response.status} ${await response.text().catch(() => \"\")}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst reader = response.body.getReader();\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buffer = \"\";\n\t\t\tlet lastStatus = \"\";\n\t\t\tlet errorMessage: string | undefined;\n\t\t\tconst handleLine = (line: string): void => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\ttry {\n\t\t\t\t\tconst event = JSON.parse(line) as { status?: string; error?: string };\n\t\t\t\t\tif (event.error) errorMessage = event.error;\n\t\t\t\t\tif (event.status && event.status !== lastStatus) {\n\t\t\t\t\t\tlastStatus = event.status;\n\t\t\t\t\t\tonProgress?.(event.status);\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// partial line noise\n\t\t\t\t}\n\t\t\t};\n\t\t\tfor (;;) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) break;\n\t\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\t\tconst lines = buffer.split(\"\\n\");\n\t\t\t\tbuffer = lines.pop() ?? \"\";\n\t\t\t\tfor (const line of lines) handleLine(line);\n\t\t\t}\n\t\t\t// The stream's FINAL line has no trailing newline — it stays in the buffer and holds\n\t\t\t// the terminal \"success\"/error event; dropping it misreports every completed pull.\n\t\t\thandleLine(buffer);\n\t\t\tif (errorMessage) return { ok: false, error: errorMessage };\n\t\t\treturn lastStatus === \"success\"\n\t\t\t\t? { ok: true }\n\t\t\t\t: { ok: false, error: `pull ended with: ${lastStatus || \"unknown\"}` };\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t}\n\n\t/** EXPLICIT user action only — callers must have shown what gets deleted and confirmed. */\n\tasync remove(ref: string): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/delete`, {\n\t\t\t\tmethod: \"DELETE\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: ref }),\n\t\t\t});\n\t\t\tif (!response.ok) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror: `delete failed: HTTP ${response.status} ${await response.text().catch(() => \"\")}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { ok: true };\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"local-runtime.d.ts","sourceRoot":"","sources":["../../../src/core/models/local-runtime.ts"],"names":[],"m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CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAiBlE;CACD","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { existsSync, mkdirSync } from \"node:fs\";\nimport { homedir, arch as osArch, platform as osPlatform } from \"node:os\";\nimport { delimiter, join } from \"node:path\";\nimport type { Readable, Writable } from \"node:stream\";\nimport * as nodeZlib from \"node:zlib\";\nimport { spawnProcess, spawnProcessSync, waitForChildProcess } from \"../../utils/child-process.ts\";\n\n/**\n * `spawnProcess(..., { stdio: [\"pipe\", \"pipe\", \"pipe\"] })` always yields a non-null `stdin` at\n * runtime, but child-process.ts's typed overload only narrows `.stdin` to non-null for the\n * capture-only (`StdioNull, StdioPipe, StdioPipe`) shape, so the generic overload's `Writable |\n * null` leaks through here. This makes the true invariant explicit and fails loudly (instead of\n * silently misbehaving) if it's ever violated by a future stdio change.\n */\nfunction requireStdin(proc: ChildProcess, label: string): Writable {\n\tif (!proc.stdin) throw new Error(`${label}: no stdin pipe`);\n\treturn proc.stdin;\n}\n\n/**\n * Local model runtime manager (local-model-lifecycle-design.md): Ollama first, interface kept\n * runtime-agnostic. Pi SPAWNS the serve process itself with OWNED model storage\n * (`OLLAMA_MODELS=<agentDir>/models/ollama`) so every downloaded weight lives inside pi's tree —\n * per-model disk accounting is trivial and full cleanup is one directory. If a system server is\n * already running, pi uses it instead of double-installing, with the honest tradeoff surfaced:\n * storage then lives in the system daemon's dir and removal control is limited to `ollama rm`.\n *\n * Hard boundaries (design \"Hard boundaries\"): lifecycle actions are USER commands only — this\n * module is never exposed as a model-invokable tool; install is GUIDE MODE (exact manual steps,\n * never `curl | sh`); removal is explicit, disclosed, and never automatic.\n */\n\nexport interface LocalRuntimeStatus {\n\tbinaryPath?: string;\n\tbinarySource?: \"system\" | \"user\" | \"pi-owned\";\n\tserverUp: boolean;\n\tserverUrl: string;\n\t/** True when the responding server is a child process pi spawned (owned storage applies). */\n\tmanagedByPi: boolean;\n\t/** Owned weights directory (only meaningful for pi-managed serves). */\n\townedModelsDir: string;\n}\n\nexport interface InstalledLocalModel {\n\tname: string;\n\tsizeBytes: number;\n}\n\n/** Pinned ollama release for the managed installer below. Bump here when needed — one constant. */\nexport const OLLAMA_PINNED_VERSION = \"0.31.1\";\n\nexport type OllamaAssetKind = \"tar-zst\" | \"tar-gz\" | \"zip\";\n\nexport interface OllamaReleaseAsset {\n\tname: string;\n\tkind: OllamaAssetKind;\n}\n\n/**\n * Maps a platform/arch pair (node's `os.platform()`/`os.arch()`) to the exact ollama release asset\n * name for {@link OLLAMA_PINNED_VERSION} — verified against the real ollama/ollama GitHub release\n * and its own install.sh, not guessed: darwin ships a CLI `.tgz` (the `.app`/`.dmg`/`Ollama-*.zip`\n * assets are the separate GUI-app installer, not what we want), linux ships `.tar.zst` per arch,\n * windows ships `.zip` per arch. Pure and exported so it's independently testable and reusable\n * (e.g. by a future doctor-driven managed install) without touching OllamaRuntime.\n */\nexport function resolveOllamaAsset(plat: string, architecture: string): OllamaReleaseAsset | undefined {\n\tif (plat === \"darwin\") {\n\t\t// Universal binary — one asset for both arm64 and x64.\n\t\treturn { name: \"ollama-darwin.tgz\", kind: \"tar-gz\" };\n\t}\n\tif (plat === \"linux\") {\n\t\tconst archName = architecture === \"arm64\" ? \"arm64\" : \"amd64\";\n\t\treturn { name: `ollama-linux-${archName}.tar.zst`, kind: \"tar-zst\" };\n\t}\n\tif (plat === \"win32\") {\n\t\tconst archName = architecture === \"arm64\" ? \"arm64\" : \"amd64\";\n\t\treturn { name: `ollama-windows-${archName}.zip`, kind: \"zip\" };\n\t}\n\treturn undefined;\n}\n\nexport interface LocalRuntimeDeps {\n\tfetchFn?: typeof fetch;\n\tspawnFn?: (\n\t\tcommand: string,\n\t\targs: string[],\n\t\toptions: { env: NodeJS.ProcessEnv },\n\t) => Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\">;\n\texistsFn?: (path: string) => boolean;\n\tenvPath?: string;\n\thomeDir?: string;\n\tsleepFn?: (ms: number) => Promise<void>;\n\t/** os.platform()/os.arch() equivalents — injectable so asset resolution is testable per platform. */\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Node's built-in zstd decompress transform, when THIS runtime's node:zlib has it — undefined\n\t * forces the system-zstd fallback. Injectable so tests can force either path deterministically\n\t * instead of depending on whatever Node happens to be running the test. */\n\tcreateZstdDecompress?: () => NodeJS.ReadWriteStream;\n\t/** Whether a named command exists on PATH (e.g. system `zstd`), for the extraction fallback. */\n\thasCommand?: (command: string) => boolean;\n\t/** Runs the extraction step for a downloaded archive. Injectable so installManaged's\n\t * download->extract->verify orchestration is testable without a real tar/zstd/unzip pipeline;\n\t * defaults to the real spawn-based extractor. */\n\textractArchive?: (\n\t\tinput: NodeJS.ReadableStream,\n\t\tdestDir: string,\n\t\tkind: OllamaAssetKind,\n\t) => Promise<{ ok: boolean; error?: string }>;\n}\n\nconst DEFAULT_BASE_URL = \"http://127.0.0.1:11434\";\nconst HEALTH_TIMEOUT_MS = 2_000;\nconst START_POLL_ATTEMPTS = 20;\nconst START_POLL_INTERVAL_MS = 500;\n\nexport class OllamaRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _baseUrl: string;\n\tprivate readonly _fetch: typeof fetch;\n\tprivate readonly _spawn: NonNullable<LocalRuntimeDeps[\"spawnFn\"]>;\n\tprivate readonly _exists: (path: string) => boolean;\n\tprivate readonly _envPath: string;\n\tprivate readonly _homeDir: string;\n\tprivate readonly _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _arch: () => string;\n\tprivate readonly _createZstdDecompress: (() => NodeJS.ReadWriteStream) | undefined;\n\tprivate readonly _hasCommand: (command: string) => boolean;\n\tprivate readonly _extractArchiveOverride: LocalRuntimeDeps[\"extractArchive\"] | undefined;\n\tprivate _child: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\"> | undefined;\n\n\tconstructor(args: { agentDir: string; baseUrl?: string; deps?: LocalRuntimeDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\tthis._baseUrl = (args.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/$/, \"\");\n\t\tthis._fetch = args.deps?.fetchFn ?? fetch;\n\t\tthis._spawn = args.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, options));\n\t\tthis._exists = args.deps?.existsFn ?? existsSync;\n\t\tthis._envPath = args.deps?.envPath ?? process.env.PATH ?? \"\";\n\t\tthis._homeDir = args.deps?.homeDir ?? homedir();\n\t\tthis._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));\n\t\tthis._platform = args.deps?.platform ?? osPlatform;\n\t\tthis._arch = args.deps?.arch ?? osArch;\n\t\t// Feature-detect (not version-sniff): recent Node added zstd natively to node:zlib. Verified\n\t\t// present on this repo's minimum supported Node (>=22.19.0); still detected at runtime rather\n\t\t// than assumed, so an older/different Node correctly falls through to the system-zstd path.\n\t\t// `\"createZstdDecompress\" in deps` (rather than `??`) so a test can force the fallback path by\n\t\t// passing the key as explicitly undefined, distinct from omitting it (real auto-detection).\n\t\tthis._createZstdDecompress =\n\t\t\targs.deps && \"createZstdDecompress\" in args.deps\n\t\t\t\t? args.deps.createZstdDecompress\n\t\t\t\t: typeof nodeZlib.createZstdDecompress === \"function\"\n\t\t\t\t\t? () => nodeZlib.createZstdDecompress()\n\t\t\t\t\t: undefined;\n\t\tthis._hasCommand =\n\t\t\targs.deps?.hasCommand ??\n\t\t\t((command) => spawnProcessSync(command, [\"--version\"], { encoding: \"utf8\" }).error === undefined);\n\t\tthis._extractArchiveOverride = args.deps?.extractArchive;\n\t}\n\n\tget baseUrl(): string {\n\t\treturn this._baseUrl;\n\t}\n\n\townedModelsDir(): string {\n\t\treturn join(this._agentDir, \"models\", \"ollama\");\n\t}\n\n\tprivate _findBinary(): { path: string; source: \"system\" | \"user\" | \"pi-owned\" } | undefined {\n\t\tconst piOwned = join(this._agentDir, \"runtimes\", \"ollama\", \"bin\", \"ollama\");\n\t\tif (this._exists(piOwned)) return { path: piOwned, source: \"pi-owned\" };\n\t\tconst userLevel = join(this._homeDir, \".local\", \"share\", \"ollama-dist\", \"bin\", \"ollama\");\n\t\tif (this._exists(userLevel)) return { path: userLevel, source: \"user\" };\n\t\tfor (const dir of this._envPath.split(delimiter)) {\n\t\t\tif (!dir) continue;\n\t\t\tconst candidate = join(dir, \"ollama\");\n\t\t\tif (this._exists(candidate)) return { path: candidate, source: \"system\" };\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate async _serverUp(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/tags`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_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\tasync detect(): Promise<LocalRuntimeStatus> {\n\t\tconst binary = this._findBinary();\n\t\tconst serverUp = await this._serverUp();\n\t\treturn {\n\t\t\tbinaryPath: binary?.path,\n\t\t\tbinarySource: binary?.source,\n\t\t\tserverUp,\n\t\t\tserverUrl: this._baseUrl,\n\t\t\tmanagedByPi: this._child !== undefined && serverUp,\n\t\t\townedModelsDir: this.ownedModelsDir(),\n\t\t};\n\t}\n\n\t/**\n\t * Manual-steps fallback text — shown when the managed install below wasn't offered (headless),\n\t * was declined, or itself failed. #31 reversed the stance for ollama specifically: pi CAN install\n\t * it for you now (consent-gated, a controlled download into pi's own runtimes dir, never\n\t * `curl | sh`) — these steps are the alternative for when that path isn't taken.\n\t */\n\tinstallGuide(): string[] {\n\t\treturn [\n\t\t\t\"Ollama is not installed. Pi can install it for you now (asks first, never curl|sh) — or follow these manual steps yourself:\",\n\t\t\t\" 1. Download the pinned release archive for your platform:\",\n\t\t\t\" https://github.com/ollama/ollama/releases (asset: ollama-linux-amd64.tar.zst or your platform)\",\n\t\t\t` 2. Extract it to ${join(this._homeDir, \".local\", \"share\", \"ollama-dist\")}`,\n\t\t\t\" 3. Re-run your /models command - pi detects the binary automatically.\",\n\t\t\t\"Alternatively install system-wide from https://ollama.com/download and pi will use the system server.\",\n\t\t];\n\t}\n\n\t/** Which decompressor to use for a `.tar.zst` asset — native (this Node's own zlib) is always\n\t * preferred when available, even if a system `zstd` ALSO exists, since it needs no external\n\t * dependency at all. Verified per-runtime by feature detection (see the constructor), not by\n\t * guessing from a Node version number. */\n\tprivate _chooseZstdStrategy(): { kind: \"native\" } | { kind: \"system\" } | { kind: \"unavailable\" } {\n\t\tif (this._createZstdDecompress) return { kind: \"native\" };\n\t\tif (this._hasCommand(\"zstd\")) return { kind: \"system\" };\n\t\treturn { kind: \"unavailable\" };\n\t}\n\n\t/**\n\t * Managed install (#31): a consent-gated, controlled download of the pinned ollama release into\n\t * pi's OWN runtimes dir (`<agentDir>/runtimes/ollama/`) — never `curl | sh`, never a second\n\t * server instance, and distinct from {@link start}'s owned MODEL storage (this is the binary\n\t * itself). Callers (the router's consent flow, and later a doctor-driven install) own the\n\t * consent/UI; this method only does the mechanical download+extract+verify and reports the\n\t * outcome honestly. `onProgress` is best-effort UI feedback, not a completion signal.\n\t */\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tconst asset = resolveOllamaAsset(this._platform(), this._arch());\n\t\tif (!asset) return { ok: false, error: \"unsupported-platform\" };\n\n\t\tonProgress?.(`Downloading ${asset.name}…`);\n\t\tconst downloadUrl = `https://github.com/ollama/ollama/releases/download/v${OLLAMA_PINNED_VERSION}/${asset.name}`;\n\t\tlet response: Response;\n\t\ttry {\n\t\t\tresponse = await this._fetch(downloadUrl);\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };\n\t\t}\n\t\tif (!response.ok || !response.body) {\n\t\t\treturn { ok: false, error: `download-fail: HTTP ${response.status}` };\n\t\t}\n\n\t\tconst destDir = join(this._agentDir, \"runtimes\", \"ollama\");\n\t\tmkdirSync(destDir, { recursive: true });\n\n\t\tonProgress?.(`Extracting ${asset.name}…`);\n\t\tconst extract = this._extractArchiveOverride ?? ((input, dest, kind) => this._extractArchive(input, dest, kind));\n\t\tconst extracted = await extract(response.body as unknown as NodeJS.ReadableStream, destDir, asset.kind);\n\t\tif (!extracted.ok) return extracted;\n\n\t\tconst binaryName = this._platform() === \"win32\" ? \"ollama.exe\" : \"ollama\";\n\t\tconst binaryPath = join(destDir, \"bin\", binaryName);\n\t\tif (!this._exists(binaryPath)) {\n\t\t\treturn { ok: false, error: \"extract-fail: binary not found after extraction\" };\n\t\t}\n\t\tonProgress?.(\"Ollama installed.\");\n\t\treturn { ok: true };\n\t}\n\n\t/** Real extraction: `tar-gz` lets `tar` itself gunzip; `tar-zst` decompresses first (native\n\t * node:zlib preferred, system `zstd` as fallback, an honest error if neither exists — see\n\t * {@link _chooseZstdStrategy}); `zip` needs a seekable file, so it's buffered to disk first. */\n\tprivate async _extractArchive(\n\t\tinput: NodeJS.ReadableStream,\n\t\tdestDir: string,\n\t\tkind: OllamaAssetKind,\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\tif (kind === \"zip\") {\n\t\t\t\treturn await this._extractZip(input, destDir);\n\t\t\t}\n\t\t\treturn await this._extractTar(input, destDir, kind);\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 _extractTar(\n\t\tinput: NodeJS.ReadableStream,\n\t\tdestDir: string,\n\t\tkind: \"tar-gz\" | \"tar-zst\",\n\t): Promise<{ ok: boolean; error?: string }> {\n\t\tlet decompressed: Readable = input as unknown as Readable;\n\t\tconst tarArgs = [\"-xf\", \"-\", \"-C\", destDir];\n\t\tif (kind === \"tar-zst\") {\n\t\t\tconst strategy = this._chooseZstdStrategy();\n\t\t\tif (strategy.kind === \"unavailable\") {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror: \"zstd-missing: this archive needs zstd to decompress and neither Node's built-in support nor a system `zstd` binary was found. Install zstd (e.g. `apt-get install zstd`, `dnf install zstd`, `pacman -S zstd`) and try again.\",\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (strategy.kind === \"native\") {\n\t\t\t\tdecompressed = decompressed.pipe(\n\t\t\t\t\tthis._createZstdDecompress?.() as unknown as NodeJS.ReadWriteStream & Readable,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tconst zstdProc = spawnProcess(\"zstd\", [\"-d\"], { stdio: [\"pipe\", \"pipe\", \"pipe\"] });\n\t\t\t\tdecompressed.pipe(requireStdin(zstdProc, \"zstd\"));\n\t\t\t\tdecompressed = zstdProc.stdout as unknown as Readable;\n\t\t\t}\n\t\t} else {\n\t\t\t// tar itself handles gzip via -z — no separate decompression step needed.\n\t\t\ttarArgs[0] = \"-xzf\";\n\t\t}\n\t\tconst tarProc = spawnProcess(\"tar\", tarArgs, { stdio: [\"pipe\", \"pipe\", \"pipe\"] });\n\t\tdecompressed.pipe(requireStdin(tarProc, \"tar\"));\n\t\tconst code = await waitForChildProcess(tarProc);\n\t\tif (code !== 0) {\n\t\t\treturn { ok: false, error: `extract-fail: tar exited with code ${code}` };\n\t\t}\n\t\treturn { ok: true };\n\t}\n\n\tprivate async _extractZip(input: NodeJS.ReadableStream, destDir: string): Promise<{ ok: boolean; error?: string }> {\n\t\t// Zip's central directory needs seekable file access — buffer to a temp file first.\n\t\tconst { createWriteStream } = await import(\"node:fs\");\n\t\tconst { pipeline } = await import(\"node:stream/promises\");\n\t\tconst zipPath = join(destDir, \"..\", `ollama-download-${process.pid}-${Date.now()}.zip`);\n\t\tawait pipeline(input as unknown as Readable, createWriteStream(zipPath));\n\t\ttry {\n\t\t\tconst extractCommand = this._platform() === \"win32\" && this._hasCommand(\"tar\") ? \"tar\" : \"unzip\";\n\t\t\tconst args = extractCommand === \"tar\" ? [\"-xf\", zipPath, \"-C\", destDir] : [\"-q\", zipPath, \"-d\", destDir];\n\t\t\tconst proc = spawnProcess(extractCommand, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\t\tconst code = await waitForChildProcess(proc);\n\t\t\tif (code !== 0) {\n\t\t\t\treturn { ok: false, error: `extract-fail: ${extractCommand} exited with code ${code}` };\n\t\t\t}\n\t\t\treturn { ok: true };\n\t\t} finally {\n\t\t\ttry {\n\t\t\t\tconst { rmSync } = await import(\"node:fs\");\n\t\t\t\trmSync(zipPath, { force: true });\n\t\t\t} catch {\n\t\t\t\t// best-effort cleanup\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Shared spawn-then-health-poll for both start modes below; `extraEnv` is the only thing that\n\t * differs between them (owned storage vs reusing the user's own). */\n\tprivate async _spawnAndPoll(\n\t\tbinary: { path: string },\n\t\textraEnv: NodeJS.ProcessEnv,\n\t): Promise<{ started: boolean; reason: string }> {\n\t\tconst host = this._baseUrl.replace(/^https?:\\/\\//, \"\");\n\t\tthis._child = this._spawn(binary.path, [\"serve\"], {\n\t\t\tenv: { ...process.env, OLLAMA_HOST: host, ...extraEnv },\n\t\t});\n\t\tthis._child.unref?.();\n\t\tfor (let attempt = 0; attempt < START_POLL_ATTEMPTS; attempt++) {\n\t\t\tif (await this._serverUp()) return { started: true, reason: \"started\" };\n\t\t\tawait this._sleep(START_POLL_INTERVAL_MS);\n\t\t}\n\t\tthis.stop();\n\t\treturn { started: false, reason: \"health_check_timeout\" };\n\t}\n\n\t/**\n\t * Start a pi-managed serve with OWNED storage and the hardened env verified on this class of\n\t * hardware. No-op (reported) when a server already responds — pi never double-serves. Used by\n\t * `/models add` et al, where isolated per-model-pull storage is the point.\n\t */\n\tasync start(): Promise<{ started: boolean; reason: string }> {\n\t\tif (await this._serverUp()) {\n\t\t\treturn { started: false, reason: this._child ? \"already_running_managed\" : \"already_running_system\" };\n\t\t}\n\t\tconst binary = this._findBinary();\n\t\tif (!binary) return { started: false, reason: \"binary_missing\" };\n\t\tmkdirSync(this.ownedModelsDir(), { recursive: true });\n\t\treturn this._spawnAndPoll(binary, {\n\t\t\tOLLAMA_MODELS: this.ownedModelsDir(),\n\t\t\tOLLAMA_FLASH_ATTENTION: \"1\",\n\t\t\tOLLAMA_KV_CACHE_TYPE: \"q8_0\",\n\t\t\tOLLAMA_NUM_PARALLEL: \"1\",\n\t\t\tOLLAMA_MAX_LOADED_MODELS: \"3\",\n\t\t});\n\t}\n\n\t/**\n\t * Start a serve that REUSES the user's own existing models directory (no `OLLAMA_MODELS`\n\t * override — falls through to Ollama's own default, `~/.ollama`), for callers that must see the\n\t * user's already-pulled models rather than pi's isolated/owned storage. Same idempotency and\n\t * hardened perf env as {@link start}; the only difference is which storage the server sees.\n\t */\n\tasync startReuseExisting(): Promise<{ started: boolean; reason: string }> {\n\t\tif (await this._serverUp()) {\n\t\t\treturn { started: false, reason: this._child ? \"already_running_managed\" : \"already_running_system\" };\n\t\t}\n\t\tconst binary = this._findBinary();\n\t\tif (!binary) return { started: false, reason: \"binary_missing\" };\n\t\treturn this._spawnAndPoll(binary, {\n\t\t\tOLLAMA_FLASH_ATTENTION: \"1\",\n\t\t\tOLLAMA_KV_CACHE_TYPE: \"q8_0\",\n\t\t\tOLLAMA_NUM_PARALLEL: \"1\",\n\t\t\tOLLAMA_MAX_LOADED_MODELS: \"3\",\n\t\t});\n\t}\n\n\t/** Resource hygiene only: stops the pi-managed serve process; never deletes anything. */\n\tstop(): { stopped: boolean } {\n\t\tif (!this._child) return { stopped: false };\n\t\ttry {\n\t\t\tthis._child.kill(\"SIGTERM\");\n\t\t} catch {\n\t\t\t// already gone\n\t\t}\n\t\tthis._child = undefined;\n\t\treturn { stopped: true };\n\t}\n\n\tasync list(): Promise<InstalledLocalModel[]> {\n\t\tconst response = await this._fetch(`${this._baseUrl}/api/tags`, { signal: AbortSignal.timeout(10_000) });\n\t\tif (!response.ok) throw new Error(`ollama list failed: HTTP ${response.status}`);\n\t\tconst data = (await response.json()) as { models?: Array<{ name?: string; size?: number }> };\n\t\treturn (data.models ?? [])\n\t\t\t.filter((model): model is { name: string; size?: number } => typeof model.name === \"string\")\n\t\t\t.map((model) => ({ name: model.name, sizeBytes: model.size ?? 0 }));\n\t}\n\n\t/** Pull a model through the server API (weights land in the SERVER's models dir). */\n\tasync pull(ref: string, onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/pull`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: ref, stream: true }),\n\t\t\t});\n\t\t\tif (!response.ok || !response.body) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror: `pull failed: HTTP ${response.status} ${await response.text().catch(() => \"\")}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst reader = response.body.getReader();\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buffer = \"\";\n\t\t\tlet lastStatus = \"\";\n\t\t\tlet errorMessage: string | undefined;\n\t\t\tconst handleLine = (line: string): void => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\ttry {\n\t\t\t\t\tconst event = JSON.parse(line) as { status?: string; error?: string };\n\t\t\t\t\tif (event.error) errorMessage = event.error;\n\t\t\t\t\tif (event.status && event.status !== lastStatus) {\n\t\t\t\t\t\tlastStatus = event.status;\n\t\t\t\t\t\tonProgress?.(event.status);\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// partial line noise\n\t\t\t\t}\n\t\t\t};\n\t\t\tfor (;;) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) break;\n\t\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\t\tconst lines = buffer.split(\"\\n\");\n\t\t\t\tbuffer = lines.pop() ?? \"\";\n\t\t\t\tfor (const line of lines) handleLine(line);\n\t\t\t}\n\t\t\t// The stream's FINAL line has no trailing newline — it stays in the buffer and holds\n\t\t\t// the terminal \"success\"/error event; dropping it misreports every completed pull.\n\t\t\thandleLine(buffer);\n\t\t\tif (errorMessage) return { ok: false, error: errorMessage };\n\t\t\treturn lastStatus === \"success\"\n\t\t\t\t? { ok: true }\n\t\t\t\t: { ok: false, error: `pull ended with: ${lastStatus || \"unknown\"}` };\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t}\n\n\t/** EXPLICIT user action only — callers must have shown what gets deleted and confirmed. */\n\tasync remove(ref: string): Promise<{ ok: boolean; error?: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/delete`, {\n\t\t\t\tmethod: \"DELETE\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: ref }),\n\t\t\t});\n\t\t\tif (!response.ok) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\terror: `delete failed: HTTP ${response.status} ${await response.text().catch(() => \"\")}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { ok: true };\n\t\t} catch (error) {\n\t\t\treturn { ok: false, error: error instanceof Error ? error.message : String(error) };\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1,7 +1,46 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { existsSync, mkdirSync } from "node:fs";
|
|
3
|
-
import { homedir } from "node:os";
|
|
3
|
+
import { homedir, arch as osArch, platform as osPlatform } from "node:os";
|
|
4
4
|
import { delimiter, join } from "node:path";
|
|
5
|
+
import * as nodeZlib from "node:zlib";
|
|
6
|
+
import { spawnProcess, spawnProcessSync, waitForChildProcess } from "../../utils/child-process.js";
|
|
7
|
+
/**
|
|
8
|
+
* `spawnProcess(..., { stdio: ["pipe", "pipe", "pipe"] })` always yields a non-null `stdin` at
|
|
9
|
+
* runtime, but child-process.ts's typed overload only narrows `.stdin` to non-null for the
|
|
10
|
+
* capture-only (`StdioNull, StdioPipe, StdioPipe`) shape, so the generic overload's `Writable |
|
|
11
|
+
* null` leaks through here. This makes the true invariant explicit and fails loudly (instead of
|
|
12
|
+
* silently misbehaving) if it's ever violated by a future stdio change.
|
|
13
|
+
*/
|
|
14
|
+
function requireStdin(proc, label) {
|
|
15
|
+
if (!proc.stdin)
|
|
16
|
+
throw new Error(`${label}: no stdin pipe`);
|
|
17
|
+
return proc.stdin;
|
|
18
|
+
}
|
|
19
|
+
/** Pinned ollama release for the managed installer below. Bump here when needed — one constant. */
|
|
20
|
+
export const OLLAMA_PINNED_VERSION = "0.31.1";
|
|
21
|
+
/**
|
|
22
|
+
* Maps a platform/arch pair (node's `os.platform()`/`os.arch()`) to the exact ollama release asset
|
|
23
|
+
* name for {@link OLLAMA_PINNED_VERSION} — verified against the real ollama/ollama GitHub release
|
|
24
|
+
* and its own install.sh, not guessed: darwin ships a CLI `.tgz` (the `.app`/`.dmg`/`Ollama-*.zip`
|
|
25
|
+
* assets are the separate GUI-app installer, not what we want), linux ships `.tar.zst` per arch,
|
|
26
|
+
* windows ships `.zip` per arch. Pure and exported so it's independently testable and reusable
|
|
27
|
+
* (e.g. by a future doctor-driven managed install) without touching OllamaRuntime.
|
|
28
|
+
*/
|
|
29
|
+
export function resolveOllamaAsset(plat, architecture) {
|
|
30
|
+
if (plat === "darwin") {
|
|
31
|
+
// Universal binary — one asset for both arm64 and x64.
|
|
32
|
+
return { name: "ollama-darwin.tgz", kind: "tar-gz" };
|
|
33
|
+
}
|
|
34
|
+
if (plat === "linux") {
|
|
35
|
+
const archName = architecture === "arm64" ? "arm64" : "amd64";
|
|
36
|
+
return { name: `ollama-linux-${archName}.tar.zst`, kind: "tar-zst" };
|
|
37
|
+
}
|
|
38
|
+
if (plat === "win32") {
|
|
39
|
+
const archName = architecture === "arm64" ? "arm64" : "amd64";
|
|
40
|
+
return { name: `ollama-windows-${archName}.zip`, kind: "zip" };
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
5
44
|
const DEFAULT_BASE_URL = "http://127.0.0.1:11434";
|
|
6
45
|
const HEALTH_TIMEOUT_MS = 2_000;
|
|
7
46
|
const START_POLL_ATTEMPTS = 20;
|
|
@@ -15,6 +54,11 @@ export class OllamaRuntime {
|
|
|
15
54
|
_envPath;
|
|
16
55
|
_homeDir;
|
|
17
56
|
_sleep;
|
|
57
|
+
_platform;
|
|
58
|
+
_arch;
|
|
59
|
+
_createZstdDecompress;
|
|
60
|
+
_hasCommand;
|
|
61
|
+
_extractArchiveOverride;
|
|
18
62
|
_child;
|
|
19
63
|
constructor(args) {
|
|
20
64
|
this._agentDir = args.agentDir;
|
|
@@ -25,6 +69,23 @@ export class OllamaRuntime {
|
|
|
25
69
|
this._envPath = args.deps?.envPath ?? process.env.PATH ?? "";
|
|
26
70
|
this._homeDir = args.deps?.homeDir ?? homedir();
|
|
27
71
|
this._sleep = args.deps?.sleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
72
|
+
this._platform = args.deps?.platform ?? osPlatform;
|
|
73
|
+
this._arch = args.deps?.arch ?? osArch;
|
|
74
|
+
// Feature-detect (not version-sniff): recent Node added zstd natively to node:zlib. Verified
|
|
75
|
+
// present on this repo's minimum supported Node (>=22.19.0); still detected at runtime rather
|
|
76
|
+
// than assumed, so an older/different Node correctly falls through to the system-zstd path.
|
|
77
|
+
// `"createZstdDecompress" in deps` (rather than `??`) so a test can force the fallback path by
|
|
78
|
+
// passing the key as explicitly undefined, distinct from omitting it (real auto-detection).
|
|
79
|
+
this._createZstdDecompress =
|
|
80
|
+
args.deps && "createZstdDecompress" in args.deps
|
|
81
|
+
? args.deps.createZstdDecompress
|
|
82
|
+
: typeof nodeZlib.createZstdDecompress === "function"
|
|
83
|
+
? () => nodeZlib.createZstdDecompress()
|
|
84
|
+
: undefined;
|
|
85
|
+
this._hasCommand =
|
|
86
|
+
args.deps?.hasCommand ??
|
|
87
|
+
((command) => spawnProcessSync(command, ["--version"], { encoding: "utf8" }).error === undefined);
|
|
88
|
+
this._extractArchiveOverride = args.deps?.extractArchive;
|
|
28
89
|
}
|
|
29
90
|
get baseUrl() {
|
|
30
91
|
return this._baseUrl;
|
|
@@ -71,10 +132,15 @@ export class OllamaRuntime {
|
|
|
71
132
|
ownedModelsDir: this.ownedModelsDir(),
|
|
72
133
|
};
|
|
73
134
|
}
|
|
74
|
-
/**
|
|
135
|
+
/**
|
|
136
|
+
* Manual-steps fallback text — shown when the managed install below wasn't offered (headless),
|
|
137
|
+
* was declined, or itself failed. #31 reversed the stance for ollama specifically: pi CAN install
|
|
138
|
+
* it for you now (consent-gated, a controlled download into pi's own runtimes dir, never
|
|
139
|
+
* `curl | sh`) — these steps are the alternative for when that path isn't taken.
|
|
140
|
+
*/
|
|
75
141
|
installGuide() {
|
|
76
142
|
return [
|
|
77
|
-
"Ollama is not installed. Pi
|
|
143
|
+
"Ollama is not installed. Pi can install it for you now (asks first, never curl|sh) — or follow these manual steps yourself:",
|
|
78
144
|
" 1. Download the pinned release archive for your platform:",
|
|
79
145
|
" https://github.com/ollama/ollama/releases (asset: ollama-linux-amd64.tar.zst or your platform)",
|
|
80
146
|
` 2. Extract it to ${join(this._homeDir, ".local", "share", "ollama-dist")}`,
|
|
@@ -82,29 +148,134 @@ export class OllamaRuntime {
|
|
|
82
148
|
"Alternatively install system-wide from https://ollama.com/download and pi will use the system server.",
|
|
83
149
|
];
|
|
84
150
|
}
|
|
151
|
+
/** Which decompressor to use for a `.tar.zst` asset — native (this Node's own zlib) is always
|
|
152
|
+
* preferred when available, even if a system `zstd` ALSO exists, since it needs no external
|
|
153
|
+
* dependency at all. Verified per-runtime by feature detection (see the constructor), not by
|
|
154
|
+
* guessing from a Node version number. */
|
|
155
|
+
_chooseZstdStrategy() {
|
|
156
|
+
if (this._createZstdDecompress)
|
|
157
|
+
return { kind: "native" };
|
|
158
|
+
if (this._hasCommand("zstd"))
|
|
159
|
+
return { kind: "system" };
|
|
160
|
+
return { kind: "unavailable" };
|
|
161
|
+
}
|
|
85
162
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
163
|
+
* Managed install (#31): a consent-gated, controlled download of the pinned ollama release into
|
|
164
|
+
* pi's OWN runtimes dir (`<agentDir>/runtimes/ollama/`) — never `curl | sh`, never a second
|
|
165
|
+
* server instance, and distinct from {@link start}'s owned MODEL storage (this is the binary
|
|
166
|
+
* itself). Callers (the router's consent flow, and later a doctor-driven install) own the
|
|
167
|
+
* consent/UI; this method only does the mechanical download+extract+verify and reports the
|
|
168
|
+
* outcome honestly. `onProgress` is best-effort UI feedback, not a completion signal.
|
|
88
169
|
*/
|
|
89
|
-
async
|
|
90
|
-
|
|
91
|
-
|
|
170
|
+
async installManaged(onProgress) {
|
|
171
|
+
const asset = resolveOllamaAsset(this._platform(), this._arch());
|
|
172
|
+
if (!asset)
|
|
173
|
+
return { ok: false, error: "unsupported-platform" };
|
|
174
|
+
onProgress?.(`Downloading ${asset.name}…`);
|
|
175
|
+
const downloadUrl = `https://github.com/ollama/ollama/releases/download/v${OLLAMA_PINNED_VERSION}/${asset.name}`;
|
|
176
|
+
let response;
|
|
177
|
+
try {
|
|
178
|
+
response = await this._fetch(downloadUrl);
|
|
92
179
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
180
|
+
catch (error) {
|
|
181
|
+
return { ok: false, error: `download-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
182
|
+
}
|
|
183
|
+
if (!response.ok || !response.body) {
|
|
184
|
+
return { ok: false, error: `download-fail: HTTP ${response.status}` };
|
|
185
|
+
}
|
|
186
|
+
const destDir = join(this._agentDir, "runtimes", "ollama");
|
|
187
|
+
mkdirSync(destDir, { recursive: true });
|
|
188
|
+
onProgress?.(`Extracting ${asset.name}…`);
|
|
189
|
+
const extract = this._extractArchiveOverride ?? ((input, dest, kind) => this._extractArchive(input, dest, kind));
|
|
190
|
+
const extracted = await extract(response.body, destDir, asset.kind);
|
|
191
|
+
if (!extracted.ok)
|
|
192
|
+
return extracted;
|
|
193
|
+
const binaryName = this._platform() === "win32" ? "ollama.exe" : "ollama";
|
|
194
|
+
const binaryPath = join(destDir, "bin", binaryName);
|
|
195
|
+
if (!this._exists(binaryPath)) {
|
|
196
|
+
return { ok: false, error: "extract-fail: binary not found after extraction" };
|
|
197
|
+
}
|
|
198
|
+
onProgress?.("Ollama installed.");
|
|
199
|
+
return { ok: true };
|
|
200
|
+
}
|
|
201
|
+
/** Real extraction: `tar-gz` lets `tar` itself gunzip; `tar-zst` decompresses first (native
|
|
202
|
+
* node:zlib preferred, system `zstd` as fallback, an honest error if neither exists — see
|
|
203
|
+
* {@link _chooseZstdStrategy}); `zip` needs a seekable file, so it's buffered to disk first. */
|
|
204
|
+
async _extractArchive(input, destDir, kind) {
|
|
205
|
+
try {
|
|
206
|
+
if (kind === "zip") {
|
|
207
|
+
return await this._extractZip(input, destDir);
|
|
208
|
+
}
|
|
209
|
+
return await this._extractTar(input, destDir, kind);
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
return { ok: false, error: `extract-fail: ${error instanceof Error ? error.message : String(error)}` };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
async _extractTar(input, destDir, kind) {
|
|
216
|
+
let decompressed = input;
|
|
217
|
+
const tarArgs = ["-xf", "-", "-C", destDir];
|
|
218
|
+
if (kind === "tar-zst") {
|
|
219
|
+
const strategy = this._chooseZstdStrategy();
|
|
220
|
+
if (strategy.kind === "unavailable") {
|
|
221
|
+
return {
|
|
222
|
+
ok: false,
|
|
223
|
+
error: "zstd-missing: this archive needs zstd to decompress and neither Node's built-in support nor a system `zstd` binary was found. Install zstd (e.g. `apt-get install zstd`, `dnf install zstd`, `pacman -S zstd`) and try again.",
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (strategy.kind === "native") {
|
|
227
|
+
decompressed = decompressed.pipe(this._createZstdDecompress?.());
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
const zstdProc = spawnProcess("zstd", ["-d"], { stdio: ["pipe", "pipe", "pipe"] });
|
|
231
|
+
decompressed.pipe(requireStdin(zstdProc, "zstd"));
|
|
232
|
+
decompressed = zstdProc.stdout;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// tar itself handles gzip via -z — no separate decompression step needed.
|
|
237
|
+
tarArgs[0] = "-xzf";
|
|
238
|
+
}
|
|
239
|
+
const tarProc = spawnProcess("tar", tarArgs, { stdio: ["pipe", "pipe", "pipe"] });
|
|
240
|
+
decompressed.pipe(requireStdin(tarProc, "tar"));
|
|
241
|
+
const code = await waitForChildProcess(tarProc);
|
|
242
|
+
if (code !== 0) {
|
|
243
|
+
return { ok: false, error: `extract-fail: tar exited with code ${code}` };
|
|
244
|
+
}
|
|
245
|
+
return { ok: true };
|
|
246
|
+
}
|
|
247
|
+
async _extractZip(input, destDir) {
|
|
248
|
+
// Zip's central directory needs seekable file access — buffer to a temp file first.
|
|
249
|
+
const { createWriteStream } = await import("node:fs");
|
|
250
|
+
const { pipeline } = await import("node:stream/promises");
|
|
251
|
+
const zipPath = join(destDir, "..", `ollama-download-${process.pid}-${Date.now()}.zip`);
|
|
252
|
+
await pipeline(input, createWriteStream(zipPath));
|
|
253
|
+
try {
|
|
254
|
+
const extractCommand = this._platform() === "win32" && this._hasCommand("tar") ? "tar" : "unzip";
|
|
255
|
+
const args = extractCommand === "tar" ? ["-xf", zipPath, "-C", destDir] : ["-q", zipPath, "-d", destDir];
|
|
256
|
+
const proc = spawnProcess(extractCommand, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
257
|
+
const code = await waitForChildProcess(proc);
|
|
258
|
+
if (code !== 0) {
|
|
259
|
+
return { ok: false, error: `extract-fail: ${extractCommand} exited with code ${code}` };
|
|
260
|
+
}
|
|
261
|
+
return { ok: true };
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
try {
|
|
265
|
+
const { rmSync } = await import("node:fs");
|
|
266
|
+
rmSync(zipPath, { force: true });
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
// best-effort cleanup
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** Shared spawn-then-health-poll for both start modes below; `extraEnv` is the only thing that
|
|
274
|
+
* differs between them (owned storage vs reusing the user's own). */
|
|
275
|
+
async _spawnAndPoll(binary, extraEnv) {
|
|
97
276
|
const host = this._baseUrl.replace(/^https?:\/\//, "");
|
|
98
277
|
this._child = this._spawn(binary.path, ["serve"], {
|
|
99
|
-
env: {
|
|
100
|
-
...process.env,
|
|
101
|
-
OLLAMA_HOST: host,
|
|
102
|
-
OLLAMA_MODELS: this.ownedModelsDir(),
|
|
103
|
-
OLLAMA_FLASH_ATTENTION: "1",
|
|
104
|
-
OLLAMA_KV_CACHE_TYPE: "q8_0",
|
|
105
|
-
OLLAMA_NUM_PARALLEL: "1",
|
|
106
|
-
OLLAMA_MAX_LOADED_MODELS: "3",
|
|
107
|
-
},
|
|
278
|
+
env: { ...process.env, OLLAMA_HOST: host, ...extraEnv },
|
|
108
279
|
});
|
|
109
280
|
this._child.unref?.();
|
|
110
281
|
for (let attempt = 0; attempt < START_POLL_ATTEMPTS; attempt++) {
|
|
@@ -115,6 +286,47 @@ export class OllamaRuntime {
|
|
|
115
286
|
this.stop();
|
|
116
287
|
return { started: false, reason: "health_check_timeout" };
|
|
117
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Start a pi-managed serve with OWNED storage and the hardened env verified on this class of
|
|
291
|
+
* hardware. No-op (reported) when a server already responds — pi never double-serves. Used by
|
|
292
|
+
* `/models add` et al, where isolated per-model-pull storage is the point.
|
|
293
|
+
*/
|
|
294
|
+
async start() {
|
|
295
|
+
if (await this._serverUp()) {
|
|
296
|
+
return { started: false, reason: this._child ? "already_running_managed" : "already_running_system" };
|
|
297
|
+
}
|
|
298
|
+
const binary = this._findBinary();
|
|
299
|
+
if (!binary)
|
|
300
|
+
return { started: false, reason: "binary_missing" };
|
|
301
|
+
mkdirSync(this.ownedModelsDir(), { recursive: true });
|
|
302
|
+
return this._spawnAndPoll(binary, {
|
|
303
|
+
OLLAMA_MODELS: this.ownedModelsDir(),
|
|
304
|
+
OLLAMA_FLASH_ATTENTION: "1",
|
|
305
|
+
OLLAMA_KV_CACHE_TYPE: "q8_0",
|
|
306
|
+
OLLAMA_NUM_PARALLEL: "1",
|
|
307
|
+
OLLAMA_MAX_LOADED_MODELS: "3",
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Start a serve that REUSES the user's own existing models directory (no `OLLAMA_MODELS`
|
|
312
|
+
* override — falls through to Ollama's own default, `~/.ollama`), for callers that must see the
|
|
313
|
+
* user's already-pulled models rather than pi's isolated/owned storage. Same idempotency and
|
|
314
|
+
* hardened perf env as {@link start}; the only difference is which storage the server sees.
|
|
315
|
+
*/
|
|
316
|
+
async startReuseExisting() {
|
|
317
|
+
if (await this._serverUp()) {
|
|
318
|
+
return { started: false, reason: this._child ? "already_running_managed" : "already_running_system" };
|
|
319
|
+
}
|
|
320
|
+
const binary = this._findBinary();
|
|
321
|
+
if (!binary)
|
|
322
|
+
return { started: false, reason: "binary_missing" };
|
|
323
|
+
return this._spawnAndPoll(binary, {
|
|
324
|
+
OLLAMA_FLASH_ATTENTION: "1",
|
|
325
|
+
OLLAMA_KV_CACHE_TYPE: "q8_0",
|
|
326
|
+
OLLAMA_NUM_PARALLEL: "1",
|
|
327
|
+
OLLAMA_MAX_LOADED_MODELS: "3",
|
|
328
|
+
});
|
|
329
|
+
}
|
|
118
330
|
/** Resource hygiene only: stops the pi-managed serve process; never deletes anything. */
|
|
119
331
|
stop() {
|
|
120
332
|
if (!this._child)
|