@melaya/runner 1.0.24 → 1.0.25

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.
@@ -116,6 +116,58 @@ export async function connect(opts) {
116
116
  mkdirSync(runDir, { recursive: true });
117
117
  writeFileSync(join(runDir, "main.py"), payload.mainPyContent, "utf-8");
118
118
  writeFileSync(join(runDir, "config.json"), payload.configJson, "utf-8");
119
+ // Local-model preflight: ensure LM Studio / Ollama have the
120
+ // pipeline's model loaded BEFORE we spawn the Python subprocess.
121
+ // Without this, the subprocess hangs on its first chat completion
122
+ // call (LM Studio JIT-loads transparently, can take 60-120s with
123
+ // zero progress feedback). Pipeline run d5ead8b8655b47fa hit this
124
+ // — only the `format openai` span landed, no `chat …` span ever
125
+ // arrived because LM Studio was still loading the qwen weights.
126
+ const { preflightModel } = await import("./modelLoader.js");
127
+ const onPreflightProgress = (msg) => {
128
+ console.log(chalk.gray(` [model] ${msg}`));
129
+ // Surface to the FE via a pipeline_phase event so the launching
130
+ // animation reflects "Loading qwen3-vl-8b…" instead of staying
131
+ // on a generic spinner.
132
+ socket.emit("runner:event", {
133
+ runId: payload.runId,
134
+ payload: {
135
+ event_type: "pipeline_phase",
136
+ run_id: payload.runId,
137
+ project: payload.project,
138
+ step: 1,
139
+ total: 5,
140
+ label: msg,
141
+ status: "started",
142
+ },
143
+ });
144
+ };
145
+ const preflight = await preflightModel(payload.configJson, onPreflightProgress);
146
+ if (!preflight.ok) {
147
+ const detail = `${preflight.reason ?? "unknown"}${preflight.hint ? ` — ${preflight.hint}` : ""}`;
148
+ console.log(chalk.red(` ✗ model preflight failed: ${detail}`));
149
+ socket.emit("runner:event", {
150
+ runId: payload.runId,
151
+ payload: {
152
+ event_type: "agent_message",
153
+ run_id: payload.runId,
154
+ project: payload.project,
155
+ replyId: `preflight-${payload.runId}`,
156
+ replyName: "Runner",
157
+ replyRole: "system",
158
+ msg: {
159
+ id: `preflight-${payload.runId}`,
160
+ name: "Runner",
161
+ role: "system",
162
+ content: [{ type: "text", text: `Model preflight failed: ${detail}` }],
163
+ metadata: {},
164
+ timestamp: new Date().toISOString(),
165
+ },
166
+ },
167
+ });
168
+ socket.emit("runner:runComplete", { runId: payload.runId, status: "failed" });
169
+ return;
170
+ }
119
171
  // Build env vars for the subprocess
120
172
  const relayUrl = `http://127.0.0.1:${relay.port}/events/relay/${relay.nonce}`;
121
173
  const sharedDir = getSharedDir();
@@ -154,19 +206,60 @@ export async function connect(opts) {
154
206
  stdio: ["ignore", "pipe", "pipe"],
155
207
  });
156
208
  activeProcesses.set(payload.runId, proc);
209
+ // Ring buffer of recent stderr — surfaced to the FE on non-zero
210
+ // exit so the operator sees the actual Python traceback in the
211
+ // chat panel instead of a silent "failed" with no context.
212
+ // Capped at 60 lines so a noisy traceback can't OOM us.
213
+ const stderrTail = [];
214
+ const STDERR_TAIL_MAX = 60;
157
215
  proc.stdout?.on("data", (data) => {
158
216
  const line = data.toString().trim();
159
217
  if (line && opts.verbose)
160
218
  console.log(chalk.gray(` [stdout] ${line}`));
161
219
  });
162
220
  proc.stderr?.on("data", (data) => {
163
- const line = data.toString().trim();
164
- if (line)
165
- console.log(chalk.yellow(` [stderr] ${line}`));
221
+ const text = data.toString();
222
+ for (const ln of text.split("\n")) {
223
+ const t = ln.trimEnd();
224
+ if (!t)
225
+ continue;
226
+ console.log(chalk.yellow(` [stderr] ${t}`));
227
+ stderrTail.push(t);
228
+ if (stderrTail.length > STDERR_TAIL_MAX)
229
+ stderrTail.shift();
230
+ }
166
231
  });
167
232
  proc.on("exit", (code) => {
168
233
  activeProcesses.delete(payload.runId);
169
234
  const status = code === 0 ? "done" : "failed";
235
+ // On non-zero exit, relay the captured stderr tail to the FE
236
+ // as a system agent_message so the operator sees the actual
237
+ // failure reason. Without this, a Python crash before
238
+ // agentscope.init() runs (bad import, missing dep, syntax
239
+ // error in generated main.py) shows up only as a silent
240
+ // run_status:failed and the user has no clue what happened.
241
+ if (status === "failed" && stderrTail.length > 0) {
242
+ const tail = stderrTail.slice(-15).join("\n");
243
+ socket.emit("runner:event", {
244
+ run_id: payload.runId,
245
+ event_type: "agent_message",
246
+ project: payload.project,
247
+ replyId: `runner-exit-${payload.runId}`,
248
+ replyName: "Runner",
249
+ replyRole: "system",
250
+ msg: {
251
+ id: `runner-exit-${payload.runId}`,
252
+ name: "Runner",
253
+ role: "system",
254
+ content: [{
255
+ type: "text",
256
+ text: `Pipeline exited with code ${code}. Last stderr:\n\`\`\`\n${tail}\n\`\`\``,
257
+ }],
258
+ metadata: { exitCode: code },
259
+ timestamp: new Date().toISOString(),
260
+ },
261
+ });
262
+ }
170
263
  // Emit run_status FIRST (before runComplete deletes from ownedRunIds)
171
264
  socket.emit("runner:event", {
172
265
  run_id: payload.runId,
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Local-runner preflight: ensure the LLM model the pipeline is about to
3
+ * call is actually loaded in LM Studio / pulled in Ollama before we spawn
4
+ * the Python subprocess.
5
+ *
6
+ * Why this exists:
7
+ * - LM Studio's OpenAI-compat `/v1/chat/completions` will JIT-load a
8
+ * downloaded model on first request, but the request blocks for the
9
+ * entire load (60-120s for 8B-class quantised models). The pipeline
10
+ * subprocess sees no progress, the FE sees a stalled "Launching
11
+ * pipeline" spinner, the agent's first ChatResponse never arrives.
12
+ * Run d5ead8b8655b47fa hit this exactly: 1 `format openai` span, no
13
+ * `chat ...` span, no messages.
14
+ * - Ollama works similarly — first call to a model that isn't pulled
15
+ * will fail with "model not found"; downloads have to be explicit.
16
+ *
17
+ * What we do:
18
+ * 1. Read the model name + provider from the pipeline's config.json.
19
+ * 2. For lmstudio: GET /api/v0/models to see {id, state} for every
20
+ * model. If state="loaded" we're good. If state="not-loaded" we
21
+ * POST a tiny chat completion to force the JIT load while
22
+ * surfacing progress to the operator's stderr + emitting a
23
+ * socket event so the FE can show "Loading qwen/qwen3-vl-8b…".
24
+ * If model isn't in the list at all → fail fast with a clear
25
+ * "open LM Studio and download <name>" message.
26
+ * 3. For ollama: GET /api/tags. If model not present → fail fast with
27
+ * `ollama pull <name>` instruction. If present, ollama auto-loads.
28
+ */
29
+ export interface ModelPreflightResult {
30
+ ok: boolean;
31
+ reason?: string;
32
+ hint?: string;
33
+ loaded?: boolean;
34
+ }
35
+ export declare function preflightLmStudio(modelName: string, onProgress?: (msg: string) => void): Promise<ModelPreflightResult>;
36
+ export declare function preflightOllama(modelName: string, onProgress?: (msg: string) => void): Promise<ModelPreflightResult>;
37
+ /**
38
+ * Top-level preflight — resolves the provider/model from the pipeline
39
+ * config and dispatches to the right helper. Returns immediately for
40
+ * non-local providers (cloud-spawn shouldn't reach the runner anyway).
41
+ */
42
+ export declare function preflightModel(configJson: string, onProgress?: (msg: string) => void): Promise<ModelPreflightResult>;
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Local-runner preflight: ensure the LLM model the pipeline is about to
3
+ * call is actually loaded in LM Studio / pulled in Ollama before we spawn
4
+ * the Python subprocess.
5
+ *
6
+ * Why this exists:
7
+ * - LM Studio's OpenAI-compat `/v1/chat/completions` will JIT-load a
8
+ * downloaded model on first request, but the request blocks for the
9
+ * entire load (60-120s for 8B-class quantised models). The pipeline
10
+ * subprocess sees no progress, the FE sees a stalled "Launching
11
+ * pipeline" spinner, the agent's first ChatResponse never arrives.
12
+ * Run d5ead8b8655b47fa hit this exactly: 1 `format openai` span, no
13
+ * `chat ...` span, no messages.
14
+ * - Ollama works similarly — first call to a model that isn't pulled
15
+ * will fail with "model not found"; downloads have to be explicit.
16
+ *
17
+ * What we do:
18
+ * 1. Read the model name + provider from the pipeline's config.json.
19
+ * 2. For lmstudio: GET /api/v0/models to see {id, state} for every
20
+ * model. If state="loaded" we're good. If state="not-loaded" we
21
+ * POST a tiny chat completion to force the JIT load while
22
+ * surfacing progress to the operator's stderr + emitting a
23
+ * socket event so the FE can show "Loading qwen/qwen3-vl-8b…".
24
+ * If model isn't in the list at all → fail fast with a clear
25
+ * "open LM Studio and download <name>" message.
26
+ * 3. For ollama: GET /api/tags. If model not present → fail fast with
27
+ * `ollama pull <name>` instruction. If present, ollama auto-loads.
28
+ */
29
+ import chalk from "chalk";
30
+ const LMSTUDIO_BASE = process.env.LMSTUDIO_BASE_URL || "http://127.0.0.1:1234";
31
+ const OLLAMA_BASE = process.env.OLLAMA_BASE_URL || "http://127.0.0.1:11434";
32
+ // JIT-load timeout. 8B-class quantised models on M-series Macs typically
33
+ // load in 30-60s; bigger models or slow disks can take longer. We give
34
+ // 180s total before declaring the load stuck. Below this the runner
35
+ // simply prints progress.
36
+ const LMSTUDIO_LOAD_TIMEOUT_MS = 180_000;
37
+ async function fetchWithTimeout(url, init = {}) {
38
+ const { timeoutMs = 5_000, ...rest } = init;
39
+ return fetch(url, { ...rest, signal: AbortSignal.timeout(timeoutMs) });
40
+ }
41
+ async function listLmStudioModels() {
42
+ try {
43
+ // /api/v0/models returns an array WITH `state` (loaded/not-loaded).
44
+ // /v1/models returns OpenAI-shape without state. We prefer v0 for
45
+ // the state field; fall back to v1 if v0 returns 404 on older
46
+ // LM Studio versions.
47
+ const r = await fetchWithTimeout(`${LMSTUDIO_BASE}/api/v0/models`, { timeoutMs: 3_000 });
48
+ if (r.ok) {
49
+ const j = await r.json();
50
+ const arr = Array.isArray(j) ? j : (j.data ?? []);
51
+ return arr;
52
+ }
53
+ if (r.status === 404) {
54
+ const r2 = await fetchWithTimeout(`${LMSTUDIO_BASE}/v1/models`, { timeoutMs: 3_000 });
55
+ if (!r2.ok)
56
+ return null;
57
+ const j = await r2.json();
58
+ // No state field — treat as state-unknown so we fall through to
59
+ // a JIT-load attempt below.
60
+ return (j.data ?? []).map((m) => ({ id: m.id }));
61
+ }
62
+ return null;
63
+ }
64
+ catch {
65
+ return null;
66
+ }
67
+ }
68
+ async function jitLoadLmStudioModel(modelId, onProgress) {
69
+ // POST a 1-token chat completion; LM Studio JIT-loads the model and
70
+ // blocks until it's resident. We use a small max_tokens so the actual
71
+ // generation cost is negligible — what we care about is the load
72
+ // happening before the real pipeline call.
73
+ const start = Date.now();
74
+ const tick = setInterval(() => {
75
+ const elapsed = ((Date.now() - start) / 1000).toFixed(0);
76
+ onProgress(`still loading ${modelId} (${elapsed}s elapsed)…`);
77
+ }, 10_000);
78
+ try {
79
+ const r = await fetchWithTimeout(`${LMSTUDIO_BASE}/v1/chat/completions`, {
80
+ method: "POST",
81
+ headers: { "content-type": "application/json" },
82
+ body: JSON.stringify({
83
+ model: modelId,
84
+ messages: [{ role: "user", content: "." }],
85
+ max_tokens: 1,
86
+ stream: false,
87
+ }),
88
+ timeoutMs: LMSTUDIO_LOAD_TIMEOUT_MS,
89
+ });
90
+ if (!r.ok) {
91
+ const body = await r.text().catch(() => "");
92
+ onProgress(`LM Studio rejected load: HTTP ${r.status} ${body.slice(0, 200)}`);
93
+ return false;
94
+ }
95
+ return true;
96
+ }
97
+ catch (e) {
98
+ onProgress(`load timed out / failed: ${e.message}`);
99
+ return false;
100
+ }
101
+ finally {
102
+ clearInterval(tick);
103
+ }
104
+ }
105
+ export async function preflightLmStudio(modelName, onProgress = () => { }) {
106
+ const models = await listLmStudioModels();
107
+ if (models === null) {
108
+ return {
109
+ ok: false,
110
+ reason: "lmstudio_unreachable",
111
+ hint: `Could not reach LM Studio at ${LMSTUDIO_BASE}. Start LM Studio and enable the local server (Developer tab → Start Server).`,
112
+ };
113
+ }
114
+ // Match by exact id first, then by suffix (LM Studio model ids include
115
+ // the publisher prefix; users sometimes type just the leaf name).
116
+ const exact = models.find((m) => m.id === modelName);
117
+ const suffix = exact ? null : models.find((m) => m.id.endsWith(`/${modelName}`) || m.id.endsWith(`:${modelName}`));
118
+ const match = exact ?? suffix;
119
+ if (!match) {
120
+ const available = models.map((m) => m.id).slice(0, 6).join(", ");
121
+ return {
122
+ ok: false,
123
+ reason: "model_not_downloaded",
124
+ hint: `Model "${modelName}" not found in LM Studio. Open LM Studio → Search → download it. Available: ${available || "(none)"}`,
125
+ };
126
+ }
127
+ const state = (match.state ?? "unknown");
128
+ if (state === "loaded") {
129
+ onProgress(`✓ ${match.id} already loaded in LM Studio`);
130
+ return { ok: true, loaded: true };
131
+ }
132
+ onProgress(`Loading ${match.id} into LM Studio (this can take 30-120s for first load)…`);
133
+ const loaded = await jitLoadLmStudioModel(match.id, onProgress);
134
+ if (!loaded) {
135
+ return {
136
+ ok: false,
137
+ reason: "load_failed",
138
+ hint: `LM Studio could not load "${match.id}". Check available RAM and the LM Studio server log.`,
139
+ };
140
+ }
141
+ onProgress(`✓ ${match.id} loaded`);
142
+ return { ok: true, loaded: true };
143
+ }
144
+ export async function preflightOllama(modelName, onProgress = () => { }) {
145
+ try {
146
+ const r = await fetchWithTimeout(`${OLLAMA_BASE}/api/tags`, { timeoutMs: 3_000 });
147
+ if (!r.ok) {
148
+ return {
149
+ ok: false,
150
+ reason: "ollama_unreachable",
151
+ hint: `Could not reach Ollama at ${OLLAMA_BASE}. Start the Ollama daemon.`,
152
+ };
153
+ }
154
+ const j = await r.json();
155
+ const tags = (j.models ?? []).map((m) => m.name);
156
+ if (!tags.includes(modelName)) {
157
+ return {
158
+ ok: false,
159
+ reason: "model_not_pulled",
160
+ hint: `Model "${modelName}" not pulled in Ollama. Run: ollama pull ${modelName}`,
161
+ };
162
+ }
163
+ onProgress(`✓ ${modelName} present in Ollama`);
164
+ return { ok: true, loaded: true };
165
+ }
166
+ catch (e) {
167
+ return { ok: false, reason: "ollama_error", hint: e.message };
168
+ }
169
+ }
170
+ /**
171
+ * Top-level preflight — resolves the provider/model from the pipeline
172
+ * config and dispatches to the right helper. Returns immediately for
173
+ * non-local providers (cloud-spawn shouldn't reach the runner anyway).
174
+ */
175
+ export async function preflightModel(configJson, onProgress = (m) => console.log(chalk.gray(` [model] ${m}`))) {
176
+ let cfg;
177
+ try {
178
+ cfg = JSON.parse(configJson);
179
+ }
180
+ catch {
181
+ return { ok: true }; /* let main.py error naturally */
182
+ }
183
+ // Pipeline-level + per-step / per-agent providers — match the same
184
+ // walk as builder_server.py:2716-2725 so we honour the agent that
185
+ // overrides the top-level pick.
186
+ const providers = new Set();
187
+ const models = new Set();
188
+ const top = String(cfg.model_provider ?? cfg.model?.provider ?? "").toLowerCase();
189
+ if (top)
190
+ providers.add(top);
191
+ const topModel = String(cfg.model_name ?? cfg.model?.name ?? "");
192
+ if (topModel)
193
+ models.add(topModel);
194
+ for (const step of (cfg.steps ?? [])) {
195
+ const a = step?.agent ?? {};
196
+ const p = String(a?.model?.provider ?? "").toLowerCase();
197
+ const n = String(a?.model?.name ?? "");
198
+ if (p)
199
+ providers.add(p);
200
+ if (n)
201
+ models.add(n);
202
+ }
203
+ for (const a of (cfg.agents ?? [])) {
204
+ const p = String(a?.model_provider ?? a?.model?.provider ?? "").toLowerCase();
205
+ const n = String(a?.model_name ?? a?.model?.name ?? "");
206
+ if (p)
207
+ providers.add(p);
208
+ if (n)
209
+ models.add(n);
210
+ }
211
+ // Run preflight for every distinct (provider, model) combo this
212
+ // pipeline will exercise. One bad agent = whole run fails fast.
213
+ const tasks = [];
214
+ for (const m of models) {
215
+ if (providers.has("lmstudio"))
216
+ tasks.push(preflightLmStudio(m, onProgress));
217
+ if (providers.has("ollama"))
218
+ tasks.push(preflightOllama(m, onProgress));
219
+ }
220
+ if (tasks.length === 0)
221
+ return { ok: true };
222
+ const results = await Promise.all(tasks);
223
+ const failed = results.find((r) => !r.ok);
224
+ return failed ?? { ok: true, loaded: true };
225
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@melaya/runner",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "Run Melaya AI pipelines locally with your own LM Studio or Ollama models",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,