@caupulican/pi-adaptative 0.81.15 → 0.81.17
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 +23 -0
- package/dist/bundled-resources/runtimes/hf-transformers-openai-server.py +427 -0
- package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +18 -0
- package/dist/bundled-resources/skills/tool-call-repair/references/text-protocol-grammar.md +14 -7
- package/dist/core/agent-session.d.ts +15 -3
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +201 -25
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/local-runtime-controller.d.ts +17 -9
- package/dist/core/local-runtime-controller.d.ts.map +1 -1
- package/dist/core/local-runtime-controller.js +124 -20
- package/dist/core/local-runtime-controller.js.map +1 -1
- package/dist/core/models/adaptation-store.d.ts +5 -0
- package/dist/core/models/adaptation-store.d.ts.map +1 -1
- package/dist/core/models/adaptation-store.js +16 -0
- package/dist/core/models/adaptation-store.js.map +1 -1
- package/dist/core/models/context-sizing.d.ts +30 -0
- package/dist/core/models/context-sizing.d.ts.map +1 -0
- package/dist/core/models/context-sizing.js +90 -0
- package/dist/core/models/context-sizing.js.map +1 -0
- package/dist/core/models/default-model-suggestions.d.ts +4 -4
- package/dist/core/models/default-model-suggestions.d.ts.map +1 -1
- package/dist/core/models/default-model-suggestions.js +9 -0
- package/dist/core/models/default-model-suggestions.js.map +1 -1
- package/dist/core/models/local-registration.d.ts +12 -0
- package/dist/core/models/local-registration.d.ts.map +1 -1
- package/dist/core/models/local-registration.js +68 -0
- package/dist/core/models/local-registration.js.map +1 -1
- package/dist/core/models/local-runtime.d.ts +96 -1
- package/dist/core/models/local-runtime.d.ts.map +1 -1
- package/dist/core/models/local-runtime.js +338 -4
- package/dist/core/models/local-runtime.js.map +1 -1
- package/dist/core/models/model-ref.d.ts +4 -0
- package/dist/core/models/model-ref.d.ts.map +1 -1
- package/dist/core/models/model-ref.js +12 -3
- package/dist/core/models/model-ref.js.map +1 -1
- package/dist/core/models/perf-profile.d.ts +36 -0
- package/dist/core/models/perf-profile.d.ts.map +1 -0
- package/dist/core/models/perf-profile.js +163 -0
- package/dist/core/models/perf-profile.js.map +1 -0
- package/dist/core/models/runtime-arbiter.d.ts +57 -0
- package/dist/core/models/runtime-arbiter.d.ts.map +1 -0
- package/dist/core/models/runtime-arbiter.js +49 -0
- package/dist/core/models/runtime-arbiter.js.map +1 -0
- package/dist/core/tool-repair-health.d.ts.map +1 -1
- package/dist/core/tool-repair-health.js +2 -1
- package/dist/core/tool-repair-health.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +4 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/local-model-commands.d.ts +3 -1
- package/dist/modes/interactive/local-model-commands.d.ts.map +1 -1
- package/dist/modes/interactive/local-model-commands.js +163 -22
- package/dist/modes/interactive/local-model-commands.js.map +1 -1
- package/docs/models.md +19 -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 -12
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-registration.js","sourceRoot":"","sources":["../../../src/core/models/local-registration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA+CjC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,eA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,IAAI,EAAE,cAAc,EAAE,CAAC;IAC3E,gGAAgG;IAChG,yFAAuF;IACvF,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;IACxC,CAAC;IACD,aAAa,CAAC,cAAc,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAChF,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;AAAA,CACpC","sourcesContent":["import { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\n/**\n * Persistent registration for pulled local models: merges an \"ollama\" provider entry into the\n * user's `<agentDir>/models.json` — the exact file ModelRegistry loads at startup — so a pulled\n * model resolves as `ollama/<ref>` immediately AND across sessions (usable as session model,\n * lane model, judge, or curator).\n *\n * Non-destructive contract: the file is parsed with STRICT JSON first; a file that only parses\n * with comments/relaxed syntax is the user's hand-authored config and is never rewritten — the\n * caller gets `manualSnippet` to show instead.\n */\n\ninterface ModelsJsonModel {\n\tid: string;\n\tname?: string;\n\tcontextWindow?: number;\n\t/** Measured by the local capacity probe; compaction uses min(contextWindow, servedContextWindow). */\n\tservedContextWindow?: number;\n\tmaxTokens?: number;\n\treasoning?: boolean;\n\tinput?: string[];\n\tcost?: { input: number; output: number; cacheRead: number; cacheWrite: number };\n}\n\ninterface ModelsJson {\n\tproviders: Record<\n\t\tstring,\n\t\t{\n\t\t\tbaseUrl?: string;\n\t\t\tapi?: string;\n\t\t\tapiKey?: string;\n\t\t\tmodels?: ModelsJsonModel[];\n\t\t\t[key: string]: unknown;\n\t\t}\n\t>;\n\t[key: string]: unknown;\n}\n\nexport interface LocalRegistrationResult {\n\tok: boolean;\n\tmodelsJsonPath: string;\n\treason?: string;\n\t/** When the file cannot be safely rewritten: the entry the user should add by hand. */\n\tmanualSnippet?: string;\n}\n\n/** Provider name pi registers pulled local models under (see registerLocalModel below). */\nexport const OLLAMA_PROVIDER = \"ollama\";\n\nfunction localModelEntry(ref: string, contextWindow: number, servedContextWindow?: number): ModelsJsonModel {\n\treturn {\n\t\tid: ref,\n\t\tname: ref,\n\t\tcontextWindow,\n\t\t...(servedContextWindow !== undefined ? { servedContextWindow } : {}),\n\t\tmaxTokens: 2048,\n\t\treasoning: false,\n\t\tinput: [\"text\"],\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\t};\n}\n\nfunction loadStrict(path: string): { json?: ModelsJson; reason?: string } {\n\tif (!existsSync(path)) return { json: { providers: {} } };\n\ttry {\n\t\tconst parsed = JSON.parse(readFileSync(path, \"utf-8\")) as ModelsJson;\n\t\tif (!parsed || typeof parsed !== \"object\") return { reason: \"models.json is not a JSON object\" };\n\t\tparsed.providers = parsed.providers ?? {};\n\t\treturn { json: parsed };\n\t} catch {\n\t\treturn { reason: \"models.json uses comments/relaxed JSON — pi will not rewrite a hand-authored file\" };\n\t}\n}\n\nexport function registerLocalModel(args: {\n\tagentDir: string;\n\tref: string;\n\tbaseUrl: string;\n\tcontextWindow?: number;\n\tservedContextWindow?: number;\n}): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst contextWindow = args.contextWindow ?? 8192;\n\tconst entry = localModelEntry(args.ref, contextWindow, args.servedContextWindow);\n\tconst providerBase = {\n\t\tbaseUrl: `${args.baseUrl.replace(/\\/$/, \"\")}/v1`,\n\t\tapi: \"openai-completions\",\n\t\tapiKey: \"ollama\",\n\t};\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\tmodelsJsonPath,\n\t\t\treason,\n\t\t\tmanualSnippet: JSON.stringify(\n\t\t\t\t{ providers: { [OLLAMA_PROVIDER]: { ...providerBase, models: [entry] } } },\n\t\t\t\tnull,\n\t\t\t\t\"\\t\",\n\t\t\t),\n\t\t};\n\t}\n\tjson.providers[OLLAMA_PROVIDER] ??= { ...providerBase, models: [] };\n\tconst provider = json.providers[OLLAMA_PROVIDER];\n\tprovider.baseUrl ??= providerBase.baseUrl;\n\tprovider.api ??= providerBase.api;\n\tprovider.apiKey ??= providerBase.apiKey;\n\tprovider.models ??= [];\n\tconst existing = provider.models.findIndex((model) => model.id === args.ref);\n\tif (existing >= 0) {\n\t\tprovider.models[existing] = { ...provider.models[existing], ...entry };\n\t} else {\n\t\tprovider.models.push(entry);\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n\nexport function unregisterLocalModel(args: { agentDir: string; ref: string }): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) return { ok: false, modelsJsonPath, reason };\n\tconst provider = json.providers[OLLAMA_PROVIDER];\n\tif (!provider?.models) return { ok: true, modelsJsonPath };\n\tconst before = provider.models.length;\n\tprovider.models = provider.models.filter((model) => model.id !== args.ref);\n\tif (provider.models.length === before) return { ok: true, modelsJsonPath };\n\t// Drop the whole provider entry when its last pi-registered model goes (leave user fields alone\n\t// if they added any models themselves — only an all-pi-managed empty list is removed).\n\tif (provider.models.length === 0) {\n\t\tdelete json.providers[OLLAMA_PROVIDER];\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"local-registration.js","sourceRoot":"","sources":["../../../src/core/models/local-registration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAmDjC,2FAA2F;AAC3F,MAAM,CAA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AC,wBAAwB,CAAC,CAAC;IACjD,CAAC;IACD,aAAa,CAAC,cAAc,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAChF,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;AAAA,CACpC","sourcesContent":["import { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\n/**\n * Persistent registration for pulled local models: merges an \"ollama\" provider entry into the\n * user's `<agentDir>/models.json` — the exact file ModelRegistry loads at startup — so a pulled\n * model resolves as `ollama/<ref>` immediately AND across sessions (usable as session model,\n * lane model, judge, or curator).\n *\n * Non-destructive contract: the file is parsed with STRICT JSON first; a file that only parses\n * with comments/relaxed syntax is the user's hand-authored config and is never rewritten — the\n * caller gets `manualSnippet` to show instead.\n */\n\ninterface ModelsJsonModel {\n\tid: string;\n\tname?: string;\n\tapi?: string;\n\tbaseUrl?: string;\n\tcontextWindow?: number;\n\t/** Measured by the local capacity probe; compaction uses min(contextWindow, servedContextWindow). */\n\tservedContextWindow?: number;\n\tmaxTokens?: number;\n\treasoning?: boolean;\n\ttextToolCallProtocol?: boolean;\n\tinput?: string[];\n\tcost?: { input: number; output: number; cacheRead: number; cacheWrite: number };\n\tcompat?: Record<string, unknown>;\n}\n\ninterface ModelsJson {\n\tproviders: Record<\n\t\tstring,\n\t\t{\n\t\t\tbaseUrl?: string;\n\t\t\tapi?: string;\n\t\t\tapiKey?: string;\n\t\t\tmodels?: ModelsJsonModel[];\n\t\t\t[key: string]: unknown;\n\t\t}\n\t>;\n\t[key: string]: unknown;\n}\n\nexport interface LocalRegistrationResult {\n\tok: boolean;\n\tmodelsJsonPath: string;\n\treason?: string;\n\t/** When the file cannot be safely rewritten: the entry the user should add by hand. */\n\tmanualSnippet?: string;\n}\n\n/** Provider name pi registers pulled local models under (see registerLocalModel below). */\nexport const OLLAMA_PROVIDER = \"ollama\";\n/** Provider name for pi-managed Hugging Face Transformers sidecar models. */\nexport const HF_TRANSFORMERS_PROVIDER = \"pi-hf-transformers\";\n\nfunction localModelEntry(ref: string, contextWindow: number, servedContextWindow?: number): ModelsJsonModel {\n\treturn {\n\t\tid: ref,\n\t\tname: ref,\n\t\tcontextWindow,\n\t\t...(servedContextWindow !== undefined ? { servedContextWindow } : {}),\n\t\tmaxTokens: 2048,\n\t\treasoning: false,\n\t\tinput: [\"text\"],\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\t};\n}\n\nfunction transformersModelEntry(args: { modelId: string; baseUrl: string; contextWindow?: number }): ModelsJsonModel {\n\treturn {\n\t\tid: args.modelId,\n\t\tname: args.modelId,\n\t\tbaseUrl: `${args.baseUrl.replace(/\\/$/, \"\")}/v1`,\n\t\tcontextWindow: args.contextWindow ?? 131_072,\n\t\tmaxTokens: 1024,\n\t\treasoning: false,\n\t\tinput: [\"text\"],\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\t\tcompat: { supportsUsageInStreaming: false },\n\t};\n}\n\nfunction loadStrict(path: string): { json?: ModelsJson; reason?: string } {\n\tif (!existsSync(path)) return { json: { providers: {} } };\n\ttry {\n\t\tconst parsed = JSON.parse(readFileSync(path, \"utf-8\")) as ModelsJson;\n\t\tif (!parsed || typeof parsed !== \"object\") return { reason: \"models.json is not a JSON object\" };\n\t\tparsed.providers = parsed.providers ?? {};\n\t\treturn { json: parsed };\n\t} catch {\n\t\treturn { reason: \"models.json uses comments/relaxed JSON — pi will not rewrite a hand-authored file\" };\n\t}\n}\n\nexport function registerLocalModel(args: {\n\tagentDir: string;\n\tref: string;\n\tbaseUrl: string;\n\tcontextWindow?: number;\n\tservedContextWindow?: number;\n}): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst contextWindow = args.contextWindow ?? 8192;\n\tconst entry = localModelEntry(args.ref, contextWindow, args.servedContextWindow);\n\tconst providerBase = {\n\t\tbaseUrl: `${args.baseUrl.replace(/\\/$/, \"\")}/v1`,\n\t\tapi: \"openai-completions\",\n\t\tapiKey: \"ollama\",\n\t};\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\tmodelsJsonPath,\n\t\t\treason,\n\t\t\tmanualSnippet: JSON.stringify(\n\t\t\t\t{ providers: { [OLLAMA_PROVIDER]: { ...providerBase, models: [entry] } } },\n\t\t\t\tnull,\n\t\t\t\t\"\\t\",\n\t\t\t),\n\t\t};\n\t}\n\tjson.providers[OLLAMA_PROVIDER] ??= { ...providerBase, models: [] };\n\tconst provider = json.providers[OLLAMA_PROVIDER];\n\tprovider.baseUrl ??= providerBase.baseUrl;\n\tprovider.api ??= providerBase.api;\n\tprovider.apiKey ??= providerBase.apiKey;\n\tprovider.models ??= [];\n\tconst existing = provider.models.findIndex((model) => model.id === args.ref);\n\tif (existing >= 0) {\n\t\tprovider.models[existing] = { ...provider.models[existing], ...entry };\n\t} else {\n\t\tprovider.models.push(entry);\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n\nexport function registerTransformersModel(args: {\n\tagentDir: string;\n\tmodelId: string;\n\tbaseUrl: string;\n\tcontextWindow?: number;\n}): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst entry = transformersModelEntry(args);\n\tconst providerBase = {\n\t\tname: \"Hugging Face Transformers (pi-managed)\",\n\t\tbaseUrl: entry.baseUrl,\n\t\tapi: \"openai-completions\",\n\t\tapiKey: \"pi-transformers\",\n\t};\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\tmodelsJsonPath,\n\t\t\treason,\n\t\t\tmanualSnippet: JSON.stringify(\n\t\t\t\t{ providers: { [HF_TRANSFORMERS_PROVIDER]: { ...providerBase, models: [entry] } } },\n\t\t\t\tnull,\n\t\t\t\t\"\\t\",\n\t\t\t),\n\t\t};\n\t}\n\tjson.providers[HF_TRANSFORMERS_PROVIDER] ??= { ...providerBase, models: [] };\n\tconst provider = json.providers[HF_TRANSFORMERS_PROVIDER];\n\tprovider.name ??= providerBase.name;\n\tprovider.baseUrl ??= providerBase.baseUrl;\n\tprovider.api ??= providerBase.api;\n\tprovider.apiKey ??= providerBase.apiKey;\n\tprovider.models ??= [];\n\tconst existing = provider.models.findIndex((model) => model.id === args.modelId);\n\tif (existing >= 0) {\n\t\tprovider.models[existing] = { ...provider.models[existing], ...entry };\n\t} else {\n\t\tprovider.models.push(entry);\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n\nexport function unregisterLocalModel(args: { agentDir: string; ref: string }): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) return { ok: false, modelsJsonPath, reason };\n\tconst provider = json.providers[OLLAMA_PROVIDER];\n\tif (!provider?.models) return { ok: true, modelsJsonPath };\n\tconst before = provider.models.length;\n\tprovider.models = provider.models.filter((model) => model.id !== args.ref);\n\tif (provider.models.length === before) return { ok: true, modelsJsonPath };\n\t// Drop the whole provider entry when its last pi-registered model goes (leave user fields alone\n\t// if they added any models themselves — only an all-pi-managed empty list is removed).\n\tif (provider.models.length === 0) {\n\t\tdelete json.providers[OLLAMA_PROVIDER];\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n\nexport function unregisterTransformersModel(args: { agentDir: string; modelId: string }): LocalRegistrationResult {\n\tconst modelsJsonPath = join(args.agentDir, \"models.json\");\n\tconst { json, reason } = loadStrict(modelsJsonPath);\n\tif (!json) return { ok: false, modelsJsonPath, reason };\n\tconst provider = json.providers[HF_TRANSFORMERS_PROVIDER];\n\tif (!provider?.models) return { ok: true, modelsJsonPath };\n\tconst before = provider.models.length;\n\tprovider.models = provider.models.filter((model) => model.id !== args.modelId);\n\tif (provider.models.length === before) return { ok: true, modelsJsonPath };\n\tif (provider.models.length === 0) {\n\t\tdelete json.providers[HF_TRANSFORMERS_PROVIDER];\n\t}\n\twriteFileSync(modelsJsonPath, `${JSON.stringify(json, null, \"\\t\")}\\n`, \"utf-8\");\n\treturn { ok: true, modelsJsonPath };\n}\n"]}
|
|
@@ -25,6 +25,9 @@ export interface InstalledLocalModel {
|
|
|
25
25
|
name: string;
|
|
26
26
|
sizeBytes: number;
|
|
27
27
|
}
|
|
28
|
+
export interface OllamaModelInfo {
|
|
29
|
+
modelInfo: Record<string, unknown>;
|
|
30
|
+
}
|
|
28
31
|
/** Pinned ollama release for the managed installer below. Bump here when needed — one constant. */
|
|
29
32
|
export declare const OLLAMA_PINNED_VERSION = "0.31.1";
|
|
30
33
|
export type OllamaAssetKind = "tar-zst" | "tar-gz" | "zip";
|
|
@@ -41,6 +44,17 @@ export interface OllamaReleaseAsset {
|
|
|
41
44
|
* (e.g. by a future doctor-driven managed install) without touching OllamaRuntime.
|
|
42
45
|
*/
|
|
43
46
|
export declare function resolveOllamaAsset(plat: string, architecture: string): OllamaReleaseAsset | undefined;
|
|
47
|
+
export interface RuntimeCommandResult {
|
|
48
|
+
ok: boolean;
|
|
49
|
+
stdout: string;
|
|
50
|
+
stderr: string;
|
|
51
|
+
code?: number | null;
|
|
52
|
+
error?: string;
|
|
53
|
+
}
|
|
54
|
+
export type RuntimeCommandRunner = (command: string, args: string[], options?: {
|
|
55
|
+
env?: NodeJS.ProcessEnv;
|
|
56
|
+
onOutput?: (chunk: string) => void;
|
|
57
|
+
}) => Promise<RuntimeCommandResult>;
|
|
44
58
|
export interface LocalRuntimeDeps {
|
|
45
59
|
fetchFn?: typeof fetch;
|
|
46
60
|
spawnFn?: (command: string, args: string[], options: {
|
|
@@ -50,9 +64,14 @@ export interface LocalRuntimeDeps {
|
|
|
50
64
|
envPath?: string;
|
|
51
65
|
homeDir?: string;
|
|
52
66
|
sleepFn?: (ms: number) => Promise<void>;
|
|
53
|
-
/** os.platform()/os.arch() equivalents — injectable so asset resolution is testable per platform. */
|
|
67
|
+
/** os.platform()/os.arch() equivalents — injectable so asset/runtime resolution is testable per platform. */
|
|
54
68
|
platform?: () => string;
|
|
55
69
|
arch?: () => string;
|
|
70
|
+
/** Runs a runtime-management command (Python venv/pip/download probe). Injectable so tests never
|
|
71
|
+
* install packages or hit the network. */
|
|
72
|
+
runCommand?: RuntimeCommandRunner;
|
|
73
|
+
/** Override for bundled runtime helper scripts, used by tests and source/binary packaging checks. */
|
|
74
|
+
transformersServerScriptPath?: string;
|
|
56
75
|
/** Node's built-in zstd decompress transform, when THIS runtime's node:zlib has it — undefined
|
|
57
76
|
* forces the system-zstd fallback. Injectable so tests can force either path deterministically
|
|
58
77
|
* instead of depending on whatever Node happens to be running the test. */
|
|
@@ -67,6 +86,7 @@ export interface LocalRuntimeDeps {
|
|
|
67
86
|
error?: string;
|
|
68
87
|
}>;
|
|
69
88
|
}
|
|
89
|
+
export declare function resolveTransformersBaseUrl(modelId: string): string;
|
|
70
90
|
export declare class OllamaRuntime {
|
|
71
91
|
private readonly _agentDir;
|
|
72
92
|
private readonly _baseUrl;
|
|
@@ -144,6 +164,22 @@ export declare class OllamaRuntime {
|
|
|
144
164
|
stopped: boolean;
|
|
145
165
|
};
|
|
146
166
|
list(): Promise<InstalledLocalModel[]>;
|
|
167
|
+
show(ref: string): Promise<{
|
|
168
|
+
ok: true;
|
|
169
|
+
info: OllamaModelInfo;
|
|
170
|
+
} | {
|
|
171
|
+
ok: false;
|
|
172
|
+
error: string;
|
|
173
|
+
}>;
|
|
174
|
+
createFromModelfile(args: {
|
|
175
|
+
name: string;
|
|
176
|
+
modelfile: string;
|
|
177
|
+
}): Promise<{
|
|
178
|
+
ok: true;
|
|
179
|
+
} | {
|
|
180
|
+
ok: false;
|
|
181
|
+
error: string;
|
|
182
|
+
}>;
|
|
147
183
|
/** Pull a model through the server API (weights land in the SERVER's models dir). */
|
|
148
184
|
pull(ref: string, onProgress?: (status: string) => void): Promise<{
|
|
149
185
|
ok: boolean;
|
|
@@ -155,4 +191,63 @@ export declare class OllamaRuntime {
|
|
|
155
191
|
error?: string;
|
|
156
192
|
}>;
|
|
157
193
|
}
|
|
194
|
+
export interface TransformersRuntimeStatus {
|
|
195
|
+
runtimeInstalled: boolean;
|
|
196
|
+
serverUp: boolean;
|
|
197
|
+
baseUrl: string;
|
|
198
|
+
modelId: string;
|
|
199
|
+
venvDir: string;
|
|
200
|
+
cacheDir: string;
|
|
201
|
+
serverScriptPath: string;
|
|
202
|
+
}
|
|
203
|
+
export declare class TransformersRuntime {
|
|
204
|
+
private readonly _agentDir;
|
|
205
|
+
private readonly _modelId;
|
|
206
|
+
private readonly _baseUrl;
|
|
207
|
+
private readonly _fetch;
|
|
208
|
+
private readonly _spawn;
|
|
209
|
+
private readonly _exists;
|
|
210
|
+
private readonly _sleep;
|
|
211
|
+
private readonly _platform;
|
|
212
|
+
private readonly _runCommand;
|
|
213
|
+
private readonly _serverScriptPath;
|
|
214
|
+
private _proc?;
|
|
215
|
+
constructor(args: {
|
|
216
|
+
agentDir: string;
|
|
217
|
+
modelId: string;
|
|
218
|
+
baseUrl?: string;
|
|
219
|
+
deps?: LocalRuntimeDeps;
|
|
220
|
+
});
|
|
221
|
+
get baseUrl(): string;
|
|
222
|
+
get modelId(): string;
|
|
223
|
+
get runtimeDir(): string;
|
|
224
|
+
get venvDir(): string;
|
|
225
|
+
get cacheDir(): string;
|
|
226
|
+
get pythonPath(): string;
|
|
227
|
+
private get serverPort();
|
|
228
|
+
private get serverHost();
|
|
229
|
+
private runtimeEnv;
|
|
230
|
+
private serverUp;
|
|
231
|
+
detect(): Promise<TransformersRuntimeStatus>;
|
|
232
|
+
private pythonCandidates;
|
|
233
|
+
private findPython;
|
|
234
|
+
private torchInstallArgs;
|
|
235
|
+
private runPython;
|
|
236
|
+
installManaged(onProgress?: (status: string) => void): Promise<{
|
|
237
|
+
ok: boolean;
|
|
238
|
+
error?: string;
|
|
239
|
+
}>;
|
|
240
|
+
downloadModel(onProgress?: (status: string) => void): Promise<{
|
|
241
|
+
ok: boolean;
|
|
242
|
+
error?: string;
|
|
243
|
+
}>;
|
|
244
|
+
start(): Promise<{
|
|
245
|
+
started: boolean;
|
|
246
|
+
reason: string;
|
|
247
|
+
}>;
|
|
248
|
+
stop(): {
|
|
249
|
+
stopped: boolean;
|
|
250
|
+
};
|
|
251
|
+
installGuide(): string[];
|
|
252
|
+
}
|
|
158
253
|
//# sourceMappingURL=local-runtime.d.ts.map
|
|
@@ -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;AAoB9D;;;;;;;;;;;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;CAClB;AAED,qGAAmG;AACnG,eAAO,MAAM,qBAAqB,WAAW,CAAC;AAE9C,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE3D,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI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AAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAc3D;IAED;;;;;OAKG;IACG,kBAAkB,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAYxE;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, 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
|
+
{"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;AAsB9D;;;;;;;;;;;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;CAClB;AAED,MAAM,WAAW,eAAe;IAC/B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED,qGAAmG;AACnG,eAAO,MAAM,qBAAqB,WAAW,CAAC;AAE9C,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE3D,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,eAAe,CAAC;CACtB;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS,CAcrG;AAED,MAAM,WAAW,oBAAoB;IACpC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,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AA;KAAE,EAYjG;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,IAAI,UAAU,IAAI,MAAM,CAIvB;IAED,OAAO,KAAK,UAAU,GAGrB;IAED,OAAO,KAAK,UAAU,GAErB;IAED,OAAO,CAAC,UAAU;YAcJ,QAAQ;IAahB,MAAM,IAAI,OAAO,CAAC,yBAAyB,CAAC,CAUjD;IAED,OAAO,CAAC,gBAAgB;YAYV,UAAU;IAQxB,OAAO,CAAC,gBAAgB;YAcV,SAAS;IAYjB,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,CAuCpG;IAEK,aAAa,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,CAanG;IAEK,KAAK,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CA4B3D;IAED,IAAI,IAAI;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAK3B;IAED,YAAY,IAAI,MAAM,EAAE,CAUvB;CACD","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { createWriteStream, existsSync, mkdirSync, rmSync } 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 { pipeline } from \"node:stream/promises\";\nimport * as nodeZlib from \"node:zlib\";\nimport { getBundledResourcesDir } from \"../../config.ts\";\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\nexport interface OllamaModelInfo {\n\tmodelInfo: Record<string, unknown>;\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 RuntimeCommandResult {\n\tok: boolean;\n\tstdout: string;\n\tstderr: string;\n\tcode?: number | null;\n\terror?: string;\n}\n\nexport type RuntimeCommandRunner = (\n\tcommand: string,\n\targs: string[],\n\toptions?: { env?: NodeJS.ProcessEnv; onOutput?: (chunk: string) => void },\n) => Promise<RuntimeCommandResult>;\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/runtime resolution is testable per platform. */\n\tplatform?: () => string;\n\tarch?: () => string;\n\t/** Runs a runtime-management command (Python venv/pip/download probe). Injectable so tests never\n\t * install packages or hit the network. */\n\trunCommand?: RuntimeCommandRunner;\n\t/** Override for bundled runtime helper scripts, used by tests and source/binary packaging checks. */\n\ttransformersServerScriptPath?: 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;\nconst TRANSFORMERS_PORT_BASE = 18_100;\nconst TRANSFORMERS_PORT_SPAN = 1_000;\nconst TRANSFORMERS_START_POLL_ATTEMPTS = 240;\nconst TRANSFORMERS_RUNTIME_DIR_NAME = \"hf-transformers\";\nconst TRANSFORMERS_VENV_DIR_NAME = \"venv\";\nconst TRANSFORMERS_SERVER_RELATIVE_PATH = join(\"runtimes\", \"hf-transformers-openai-server.py\");\nconst TRANSFORMERS_PINNED_PACKAGES = [\"transformers==5.13.0\", \"huggingface-hub==1.22.0\", \"safetensors==0.8.0\"];\nconst TORCH_PINNED_VERSION = \"2.12.1\";\n\nasync function runCommandDefault(\n\tcommand: string,\n\targs: string[],\n\toptions: { env?: NodeJS.ProcessEnv; onOutput?: (chunk: string) => void } = {},\n): Promise<RuntimeCommandResult> {\n\ttry {\n\t\tconst proc = spawnProcess(command, args, {\n\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\tenv: options.env ? { ...process.env, ...options.env } : process.env,\n\t\t});\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tproc.stdout.setEncoding(\"utf8\");\n\t\tproc.stderr.setEncoding(\"utf8\");\n\t\tproc.stdout.on(\"data\", (chunk: string) => {\n\t\t\tstdout += chunk;\n\t\t\toptions.onOutput?.(chunk);\n\t\t});\n\t\tproc.stderr.on(\"data\", (chunk: string) => {\n\t\t\tstderr += chunk;\n\t\t\toptions.onOutput?.(chunk);\n\t\t});\n\t\tconst code = await waitForChildProcess(proc);\n\t\treturn {\n\t\t\tok: code === 0,\n\t\t\tstdout,\n\t\t\tstderr,\n\t\t\tcode,\n\t\t\t...(code === 0 ? {} : { error: stderr.trim() || stdout.trim() || `exit code ${code ?? \"unknown\"}` }),\n\t\t};\n\t} catch (error) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\tstdout: \"\",\n\t\t\tstderr: \"\",\n\t\t\terror: error instanceof Error ? error.message : String(error),\n\t\t};\n\t}\n}\n\nfunction sanitizeCommandOutput(value: string | undefined): string {\n\treturn value?.trim().split(\"\\n\").slice(-3).join(\"\\n\") || \"unknown error\";\n}\n\nfunction hashString(value: string): number {\n\tlet hash = 2_166_136_261;\n\tfor (let index = 0; index < value.length; index++) {\n\t\thash ^= value.charCodeAt(index);\n\t\thash = Math.imul(hash, 16_777_619) >>> 0;\n\t}\n\treturn hash;\n}\n\nexport function resolveTransformersBaseUrl(modelId: string): string {\n\tconst port = TRANSFORMERS_PORT_BASE + (hashString(modelId) % TRANSFORMERS_PORT_SPAN);\n\treturn `http://127.0.0.1:${port}`;\n}\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 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\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_KEEP_ALIVE: \"30m\",\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_KEEP_ALIVE: \"30m\",\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\tasync show(ref: string): Promise<{ ok: true; info: OllamaModelInfo } | { ok: false; error: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/show`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: ref }),\n\t\t\t\tsignal: AbortSignal.timeout(10_000),\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: `show failed: HTTP ${response.status} ${await response.text().catch(() => \"\")}`,\n\t\t\t\t};\n\t\t\t}\n\t\t\tconst data = (await response.json()) as { model_info?: Record<string, unknown> };\n\t\t\treturn { ok: true, info: { modelInfo: data.model_info ?? {} } };\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\tasync createFromModelfile(args: {\n\t\tname: string;\n\t\tmodelfile: string;\n\t}): Promise<{ ok: true } | { ok: false; error: string }> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/api/create`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { \"content-type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ model: args.name, modelfile: args.modelfile, stream: false }),\n\t\t\t\tsignal: AbortSignal.timeout(60_000),\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: `create 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\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\nexport interface TransformersRuntimeStatus {\n\truntimeInstalled: boolean;\n\tserverUp: boolean;\n\tbaseUrl: string;\n\tmodelId: string;\n\tvenvDir: string;\n\tcacheDir: string;\n\tserverScriptPath: string;\n}\n\nexport class TransformersRuntime {\n\tprivate readonly _agentDir: string;\n\tprivate readonly _modelId: 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 _sleep: (ms: number) => Promise<void>;\n\tprivate readonly _platform: () => string;\n\tprivate readonly _runCommand: RuntimeCommandRunner;\n\tprivate readonly _serverScriptPath: string;\n\tprivate _proc?: Pick<ChildProcess, \"pid\" | \"kill\" | \"unref\" | \"on\">;\n\n\tconstructor(args: { agentDir: string; modelId: string; baseUrl?: string; deps?: LocalRuntimeDeps }) {\n\t\tthis._agentDir = args.agentDir;\n\t\tthis._modelId = args.modelId;\n\t\tthis._baseUrl = args.baseUrl?.replace(/\\/$/, \"\") ?? resolveTransformersBaseUrl(args.modelId);\n\t\tthis._fetch = args.deps?.fetchFn ?? fetch;\n\t\tthis._spawn = args.deps?.spawnFn ?? ((command, argv, options) => spawn(command, argv, { env: options.env }));\n\t\tthis._exists = args.deps?.existsFn ?? existsSync;\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._runCommand = args.deps?.runCommand ?? runCommandDefault;\n\t\tthis._serverScriptPath =\n\t\t\targs.deps?.transformersServerScriptPath ?? join(getBundledResourcesDir(), TRANSFORMERS_SERVER_RELATIVE_PATH);\n\t}\n\n\tget baseUrl(): string {\n\t\treturn this._baseUrl;\n\t}\n\n\tget modelId(): string {\n\t\treturn this._modelId;\n\t}\n\n\tget runtimeDir(): string {\n\t\treturn join(this._agentDir, \"runtimes\", TRANSFORMERS_RUNTIME_DIR_NAME);\n\t}\n\n\tget venvDir(): string {\n\t\treturn join(this.runtimeDir, TRANSFORMERS_VENV_DIR_NAME);\n\t}\n\n\tget cacheDir(): string {\n\t\treturn join(this._agentDir, \"models\", \"huggingface\");\n\t}\n\n\tget pythonPath(): string {\n\t\treturn this._platform() === \"win32\"\n\t\t\t? join(this.venvDir, \"Scripts\", \"python.exe\")\n\t\t\t: join(this.venvDir, \"bin\", \"python\");\n\t}\n\n\tprivate get serverPort(): string {\n\t\tconst parsed = new URL(this._baseUrl);\n\t\treturn parsed.port || (parsed.protocol === \"https:\" ? \"443\" : \"80\");\n\t}\n\n\tprivate get serverHost(): string {\n\t\treturn new URL(this._baseUrl).hostname;\n\t}\n\n\tprivate runtimeEnv(): NodeJS.ProcessEnv {\n\t\tconst venvBin = this._platform() === \"win32\" ? join(this.venvDir, \"Scripts\") : join(this.venvDir, \"bin\");\n\t\treturn {\n\t\t\t...process.env,\n\t\t\tVIRTUAL_ENV: this.venvDir,\n\t\t\tPATH: `${venvBin}${delimiter}${process.env.PATH ?? \"\"}`,\n\t\t\tPYTHONNOUSERSITE: \"1\",\n\t\t\tHF_HOME: this.cacheDir,\n\t\t\tHUGGINGFACE_HUB_CACHE: join(this.cacheDir, \"hub\"),\n\t\t\tTRANSFORMERS_CACHE: join(this.cacheDir, \"transformers\"),\n\t\t\tHF_HUB_DISABLE_TELEMETRY: \"1\",\n\t\t};\n\t}\n\n\tprivate async serverUp(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst response = await this._fetch(`${this._baseUrl}/health`, {\n\t\t\t\tsignal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),\n\t\t\t});\n\t\t\tif (!response.ok) return false;\n\t\t\tconst body = (await response.json().catch(() => undefined)) as { model?: unknown } | undefined;\n\t\t\treturn body?.model === this._modelId;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tasync detect(): Promise<TransformersRuntimeStatus> {\n\t\treturn {\n\t\t\truntimeInstalled: this._exists(this.pythonPath),\n\t\t\tserverUp: await this.serverUp(),\n\t\t\tbaseUrl: this._baseUrl,\n\t\t\tmodelId: this._modelId,\n\t\t\tvenvDir: this.venvDir,\n\t\t\tcacheDir: this.cacheDir,\n\t\t\tserverScriptPath: this._serverScriptPath,\n\t\t};\n\t}\n\n\tprivate pythonCandidates(): Array<{ command: string; args: string[] }> {\n\t\treturn this._platform() === \"win32\"\n\t\t\t? [\n\t\t\t\t\t{ command: \"py\", args: [\"-3\"] },\n\t\t\t\t\t{ command: \"python\", args: [] },\n\t\t\t\t]\n\t\t\t: [\n\t\t\t\t\t{ command: \"python3\", args: [] },\n\t\t\t\t\t{ command: \"python\", args: [] },\n\t\t\t\t];\n\t}\n\n\tprivate async findPython(): Promise<{ command: string; args: string[] } | undefined> {\n\t\tfor (const candidate of this.pythonCandidates()) {\n\t\t\tconst result = await this._runCommand(candidate.command, [...candidate.args, \"--version\"]);\n\t\t\tif (result.ok) return candidate;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tprivate torchInstallArgs(): string[] {\n\t\tif (this._platform() === \"darwin\") {\n\t\t\treturn [\"-m\", \"pip\", \"install\", `torch==${TORCH_PINNED_VERSION}`];\n\t\t}\n\t\treturn [\n\t\t\t\"-m\",\n\t\t\t\"pip\",\n\t\t\t\"install\",\n\t\t\t`torch==${TORCH_PINNED_VERSION}+cpu`,\n\t\t\t\"--index-url\",\n\t\t\t\"https://download.pytorch.org/whl/cpu\",\n\t\t];\n\t}\n\n\tprivate async runPython(args: string[], onProgress?: (status: string) => void): Promise<RuntimeCommandResult> {\n\t\treturn this._runCommand(this.pythonPath, args, {\n\t\t\tenv: this.runtimeEnv(),\n\t\t\tonOutput: onProgress\n\t\t\t\t? (chunk) => {\n\t\t\t\t\t\tconst line = chunk.trim();\n\t\t\t\t\t\tif (line) onProgress(line);\n\t\t\t\t\t}\n\t\t\t\t: undefined,\n\t\t});\n\t}\n\n\tasync installManaged(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tmkdirSync(this.runtimeDir, { recursive: true });\n\t\tmkdirSync(this.cacheDir, { recursive: true });\n\t\tif (!this._exists(this.pythonPath)) {\n\t\t\tconst python = await this.findPython();\n\t\t\tif (!python) return { ok: false, error: \"python-missing\" };\n\t\t\tonProgress?.(`creating isolated Python venv at ${this.venvDir}`);\n\t\t\tconst venv = await this._runCommand(python.command, [...python.args, \"-m\", \"venv\", this.venvDir]);\n\t\t\tif (!venv.ok) return { ok: false, error: `venv-fail: ${sanitizeCommandOutput(venv.error ?? venv.stderr)}` };\n\t\t}\n\t\tif (!this._exists(this.pythonPath)) {\n\t\t\treturn { ok: false, error: `venv-fail: ${this.pythonPath} was not created` };\n\t\t}\n\n\t\tonProgress?.(\"upgrading pip inside pi-managed venv\");\n\t\tconst pip = await this.runPython([\"-m\", \"pip\", \"install\", \"--upgrade\", \"pip\"], onProgress);\n\t\tif (!pip.ok) return { ok: false, error: `pip-upgrade-fail: ${sanitizeCommandOutput(pip.error ?? pip.stderr)}` };\n\n\t\tonProgress?.(\"installing pinned Transformers runtime packages\");\n\t\tconst transformers = await this.runPython([\"-m\", \"pip\", \"install\", ...TRANSFORMERS_PINNED_PACKAGES], onProgress);\n\t\tif (!transformers.ok) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\terror: `transformers-install-fail: ${sanitizeCommandOutput(transformers.error ?? transformers.stderr)}`,\n\t\t\t};\n\t\t}\n\n\t\tonProgress?.(\"installing pinned CPU PyTorch backend\");\n\t\tconst torch = await this.runPython(this.torchInstallArgs(), onProgress);\n\t\tif (!torch.ok)\n\t\t\treturn { ok: false, error: `torch-install-fail: ${sanitizeCommandOutput(torch.error ?? torch.stderr)}` };\n\n\t\tconst verify = await this.runPython([\n\t\t\t\"-c\",\n\t\t\t\"import torch, transformers, huggingface_hub; print(transformers.__version__)\",\n\t\t]);\n\t\tif (!verify.ok)\n\t\t\treturn { ok: false, error: `verify-fail: ${sanitizeCommandOutput(verify.error ?? verify.stderr)}` };\n\t\treturn { ok: true };\n\t}\n\n\tasync downloadModel(onProgress?: (status: string) => void): Promise<{ ok: boolean; error?: string }> {\n\t\tif (!this._exists(this.pythonPath)) return { ok: false, error: \"runtime-missing\" };\n\t\tif (!this._exists(this._serverScriptPath))\n\t\t\treturn { ok: false, error: `server-script-missing: ${this._serverScriptPath}` };\n\t\tmkdirSync(this.cacheDir, { recursive: true });\n\t\tonProgress?.(`downloading ${this._modelId} into ${this.cacheDir}`);\n\t\tconst result = await this.runPython(\n\t\t\t[this._serverScriptPath, \"--model-id\", this._modelId, \"--cache-dir\", this.cacheDir, \"--download-only\"],\n\t\t\tonProgress,\n\t\t);\n\t\treturn result.ok\n\t\t\t? { ok: true }\n\t\t\t: { ok: false, error: `download-fail: ${sanitizeCommandOutput(result.error ?? result.stderr)}` };\n\t}\n\n\tasync start(): Promise<{ started: boolean; reason: string }> {\n\t\tif (await this.serverUp()) return { started: false, reason: \"already_running\" };\n\t\tif (!this._exists(this.pythonPath)) return { started: false, reason: \"runtime_missing\" };\n\t\tif (!this._exists(this._serverScriptPath)) return { started: false, reason: \"server_script_missing\" };\n\t\tmkdirSync(this.cacheDir, { recursive: true });\n\t\tconst env = this.runtimeEnv();\n\t\tthis._proc = this._spawn(\n\t\t\tthis.pythonPath,\n\t\t\t[\n\t\t\t\tthis._serverScriptPath,\n\t\t\t\t\"--model-id\",\n\t\t\t\tthis._modelId,\n\t\t\t\t\"--host\",\n\t\t\t\tthis.serverHost,\n\t\t\t\t\"--port\",\n\t\t\t\tthis.serverPort,\n\t\t\t\t\"--cache-dir\",\n\t\t\t\tthis.cacheDir,\n\t\t\t],\n\t\t\t{ env },\n\t\t);\n\t\tthis._proc.unref?.();\n\t\tfor (let attempt = 0; attempt < TRANSFORMERS_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\tstop(): { stopped: boolean } {\n\t\tif (!this._proc) return { stopped: false };\n\t\tthis._proc.kill();\n\t\tthis._proc = undefined;\n\t\treturn { stopped: true };\n\t}\n\n\tinstallGuide(): string[] {\n\t\treturn [\n\t\t\t\"Hugging Face Transformers runtime is not installed for this model.\",\n\t\t\t`Pi can install it into its own venv at ${this.venvDir} and cache weights under ${this.cacheDir}.`,\n\t\t\t\"Manual equivalent:\",\n\t\t\t` python3 -m venv ${this.venvDir}`,\n\t\t\t` ${this.pythonPath} -m pip install --upgrade pip`,\n\t\t\t` ${this.pythonPath} -m pip install ${TRANSFORMERS_PINNED_PACKAGES.join(\" \")}`,\n\t\t\t` ${this.pythonPath} ${this.torchInstallArgs().join(\" \")}`,\n\t\t];\n\t}\n}\n"]}
|