@melaya/runner 1.0.18 → 1.0.19

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.
@@ -188,6 +188,34 @@ export async function connect(opts) {
188
188
  });
189
189
  }
190
190
  });
191
+ // ── Luma cookie capture (one-time) ────────────────────────────────
192
+ // Same shape as the LinkedIn flow above. Cookies persist envelope-encrypted
193
+ // in agents.credentials → flow into pipeline subprocess env as
194
+ // LUMA_AUTH_SESSION_KEY / LUMA_USER_ID → consumed by shared/tools/luma.py.
195
+ socket.on("luma:start-login", async (req) => {
196
+ const sid = req?.session_id;
197
+ if (!sid)
198
+ return;
199
+ console.log(chalk.hex("#FFD43B")(`\n ▶ Luma login requested (session ${sid.slice(0, 8)}…)`));
200
+ const emitProgress = (msg) => {
201
+ socket.emit("luma:login-progress", { session_id: sid, message: msg });
202
+ if (opts.verbose)
203
+ console.log(chalk.gray(` [luma] ${msg}`));
204
+ };
205
+ try {
206
+ const { runLumaLoginFlow, describeOutcome } = await import("./lumaLogin.js");
207
+ const outcome = await runLumaLoginFlow(emitProgress);
208
+ console.log(describeOutcome(outcome));
209
+ socket.emit("luma:login-result", { session_id: sid, ...outcome });
210
+ }
211
+ catch (e) {
212
+ const msg = e?.message || "luma_login_unhandled_error";
213
+ console.log(chalk.red(` ✗ Luma login failed: ${msg}`));
214
+ socket.emit("luma:login-result", {
215
+ session_id: sid, ok: false, error: "login_unhandled_error", detail: msg,
216
+ });
217
+ }
218
+ });
191
219
  // ── Kill command ───────────────────────────────────────────────────
192
220
  socket.on("runner:kill", (data) => {
193
221
  const proc = activeProcesses.get(data.runId);
@@ -197,102 +225,13 @@ export async function connect(opts) {
197
225
  console.log(chalk.yellow(` ■ Killed run ${data.runId.slice(0, 10)}...`));
198
226
  }
199
227
  });
200
- // ── LLM stream relay ───────────────────────────────────────────────
201
- //
202
- // The cloud server picked the user's local Ollama / LM Studio model
203
- // for an AI feature (Build-with-AI architect, etc.) — but the LLM
204
- // server only exists on this machine, so we proxy the chat-completion
205
- // here: fetch the local URL, stream chunks back over the existing
206
- // socket. The server-side `streamLLMViaRunner()` consumes them.
228
+ // ── AI build-pipeline LLM-relay ────────────────────────────────────
207
229
  //
208
- // Protocol:
209
- // server runner: runner:llm-stream-request { reqId, url, headers, body }
210
- // runner server: runner:llm-stream-chunk { reqId, chunk: string }
211
- // runner server: runner:llm-stream-end { reqId, status }
212
- // runner server: runner:llm-stream-error { reqId, error }
213
- // server → runner: runner:llm-stream-cancel { reqId }
214
- const _activeLLMStreams = new Map();
215
- socket.on("runner:llm-stream-request", async (req) => {
216
- const reqId = req?.reqId;
217
- if (!reqId || typeof req.url !== "string")
218
- return;
219
- // Defense-in-depth: only allow loopback URLs. The server already
220
- // generates these as `http://localhost:1234` / `http://localhost:11434`,
221
- // but enforcing it here means a compromised server connection can't
222
- // pivot the runner into making outbound requests on its behalf.
223
- let parsed;
224
- try {
225
- parsed = new URL(req.url);
226
- }
227
- catch {
228
- socket.emit("runner:llm-stream-error", { reqId, error: "invalid_url" });
229
- return;
230
- }
231
- if (parsed.hostname !== "localhost" && parsed.hostname !== "127.0.0.1" && parsed.hostname !== "::1") {
232
- socket.emit("runner:llm-stream-error", { reqId, error: "non_loopback_url_rejected" });
233
- return;
234
- }
235
- if (opts.verbose)
236
- console.log(chalk.gray(` [llm-relay] ${reqId.slice(0, 12)} → ${req.url}`));
237
- const ac = new AbortController();
238
- _activeLLMStreams.set(reqId, ac);
239
- try {
240
- const upstream = await fetch(req.url, {
241
- method: "POST",
242
- headers: req.headers || { "content-type": "application/json" },
243
- body: req.body || "",
244
- signal: ac.signal,
245
- });
246
- if (!upstream.body) {
247
- const text = await upstream.text().catch(() => "");
248
- socket.emit("runner:llm-stream-error", {
249
- reqId,
250
- error: `upstream_no_body status=${upstream.status} ${text.slice(0, 200)}`,
251
- });
252
- return;
253
- }
254
- // Pipe chunks back as decoded text. The cloud server's parser is
255
- // text-oriented (SSE `data:` lines) so we decode here and ship
256
- // strings rather than base64 — keeps the WS payload small + lets
257
- // the server keep its existing line-buffer code path.
258
- const reader = upstream.body.getReader();
259
- const decoder = new TextDecoder();
260
- while (true) {
261
- const { value, done } = await reader.read();
262
- if (done)
263
- break;
264
- const chunk = decoder.decode(value, { stream: true });
265
- if (chunk.length > 0) {
266
- socket.emit("runner:llm-stream-chunk", { reqId, chunk });
267
- }
268
- }
269
- // Flush any tail bytes.
270
- const tail = decoder.decode();
271
- if (tail.length > 0)
272
- socket.emit("runner:llm-stream-chunk", { reqId, chunk: tail });
273
- socket.emit("runner:llm-stream-end", { reqId, status: upstream.status });
274
- if (opts.verbose)
275
- console.log(chalk.gray(` [llm-relay] ${reqId.slice(0, 12)} ◄ end ${upstream.status}`));
276
- }
277
- catch (e) {
278
- if (e?.name === "AbortError") {
279
- socket.emit("runner:llm-stream-error", { reqId, error: "aborted" });
280
- }
281
- else {
282
- socket.emit("runner:llm-stream-error", { reqId, error: String(e?.message || e) });
283
- }
284
- }
285
- finally {
286
- _activeLLMStreams.delete(reqId);
287
- }
288
- });
289
- socket.on("runner:llm-stream-cancel", (req) => {
290
- const ac = _activeLLMStreams.get(req?.reqId);
291
- if (ac) {
292
- ac.abort();
293
- _activeLLMStreams.delete(req.reqId);
294
- }
295
- });
230
+ // No new socket events — the build-pipeline relay rides on the
231
+ // EXISTING `runner:run` event. builder_server packages a thin Python
232
+ // script that fetches `localhost:1234`/`11434` and POSTs each SSE
233
+ // line back as an HTTP callback to the cloud server. Same pattern
234
+ // as the BacktestOptimizer dispatch.
296
235
  // ── Graceful shutdown ──────────────────────────────────────────────
297
236
  const cleanup = () => {
298
237
  console.log(chalk.gray("\n Shutting down..."));
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Luma cookie capture via a controlled Chromium window.
3
+ *
4
+ * Architecture mirrors `linkedinLogin.ts` — Luma auth is cookie-only,
5
+ * not OAuth, and the cookies can only be obtained by completing a real
6
+ * sign-in (Google OAuth handoff, passkey challenge, or magic-link email)
7
+ * through Luma's UI. Once captured, every subsequent Luma API call is
8
+ * direct aiohttp HTTPS via `melayaAgents/shared/tools/luma.py` — no
9
+ * browser needed for ongoing pipeline work.
10
+ *
11
+ * Flow
12
+ * ----
13
+ * 1. The Melaya server emits `luma:start-login` to this runner.
14
+ * 2. We launch a HEADED Chromium window pointed at https://luma.com/signin.
15
+ * 3. Poll the page URL — Luma redirects to /home (or /events/manage) once
16
+ * the user is authenticated.
17
+ * 4. Extract `luma.auth-session-key` and `luma.user-id` cookies and
18
+ * return them via `luma:login-result`.
19
+ *
20
+ * The chromium-install bootstrap is shared with the LinkedIn flow — if
21
+ * the user has already done a LinkedIn sign-in once, the second connector
22
+ * runs in <2s.
23
+ */
24
+ export interface LumaLoginResult {
25
+ ok: true;
26
+ cookies: {
27
+ auth_session_key: string;
28
+ user_id?: string;
29
+ };
30
+ profileName?: string;
31
+ }
32
+ export interface LumaLoginError {
33
+ ok: false;
34
+ error: string;
35
+ detail?: string;
36
+ }
37
+ export type LumaLoginOutcome = LumaLoginResult | LumaLoginError;
38
+ interface ProgressEmitter {
39
+ (msg: string): void;
40
+ }
41
+ export declare function runLumaLoginFlow(progress: ProgressEmitter): Promise<LumaLoginOutcome>;
42
+ export declare function describeOutcome(outcome: LumaLoginOutcome): string;
43
+ export {};
@@ -0,0 +1,190 @@
1
+ /**
2
+ * Luma cookie capture via a controlled Chromium window.
3
+ *
4
+ * Architecture mirrors `linkedinLogin.ts` — Luma auth is cookie-only,
5
+ * not OAuth, and the cookies can only be obtained by completing a real
6
+ * sign-in (Google OAuth handoff, passkey challenge, or magic-link email)
7
+ * through Luma's UI. Once captured, every subsequent Luma API call is
8
+ * direct aiohttp HTTPS via `melayaAgents/shared/tools/luma.py` — no
9
+ * browser needed for ongoing pipeline work.
10
+ *
11
+ * Flow
12
+ * ----
13
+ * 1. The Melaya server emits `luma:start-login` to this runner.
14
+ * 2. We launch a HEADED Chromium window pointed at https://luma.com/signin.
15
+ * 3. Poll the page URL — Luma redirects to /home (or /events/manage) once
16
+ * the user is authenticated.
17
+ * 4. Extract `luma.auth-session-key` and `luma.user-id` cookies and
18
+ * return them via `luma:login-result`.
19
+ *
20
+ * The chromium-install bootstrap is shared with the LinkedIn flow — if
21
+ * the user has already done a LinkedIn sign-in once, the second connector
22
+ * runs in <2s.
23
+ */
24
+ import { spawn } from "child_process";
25
+ import chalk from "chalk";
26
+ const LOGIN_TIMEOUT_MS = 5 * 60_000;
27
+ const POLL_INTERVAL_MS = 750;
28
+ // Pages that mean "logged in to Luma". Luma's web app routes the user to
29
+ // /home after sign-in; some flows (especially Google OAuth) bounce through
30
+ // /events or /home depending on the entry point.
31
+ const LOGGED_IN_PATHS = [
32
+ "/home",
33
+ "/manage",
34
+ "/events/manage",
35
+ "/calendar/",
36
+ "/u/",
37
+ "/settings",
38
+ ];
39
+ async function _ensurePlaywrightAvailable(progress) {
40
+ try {
41
+ return await import("playwright");
42
+ }
43
+ catch (e) {
44
+ progress("Playwright module not found in node_modules. Re-installing the runner usually fixes this.");
45
+ throw new Error(`playwright_unavailable: ${e?.message || e}`);
46
+ }
47
+ }
48
+ function _installChromium(progress) {
49
+ return new Promise((resolve, reject) => {
50
+ progress("Preparing browser for Luma sign-in (one-time setup, ~140 MB)…");
51
+ const child = spawn("npx", ["playwright", "install", "chromium"], {
52
+ shell: true,
53
+ stdio: ["ignore", "pipe", "pipe"],
54
+ });
55
+ child.stdout?.on("data", (b) => {
56
+ const line = b.toString().trim();
57
+ if (line)
58
+ progress(line);
59
+ });
60
+ child.stderr?.on("data", (b) => {
61
+ const line = b.toString().trim();
62
+ if (line)
63
+ progress(line);
64
+ });
65
+ child.on("error", (e) => reject(new Error(`chromium_install_error: ${e.message}`)));
66
+ child.on("exit", (code) => {
67
+ if (code === 0) {
68
+ progress("Browser ready.");
69
+ resolve();
70
+ }
71
+ else {
72
+ reject(new Error(`chromium_install_failed: exit code ${code}`));
73
+ }
74
+ });
75
+ });
76
+ }
77
+ function _pageLooksLoggedIn(url) {
78
+ try {
79
+ const u = new URL(url);
80
+ if (u.hostname !== "luma.com" && u.hostname !== "lu.ma" && u.hostname !== "www.luma.com")
81
+ return false;
82
+ return LOGGED_IN_PATHS.some((p) => u.pathname.startsWith(p));
83
+ }
84
+ catch {
85
+ return false;
86
+ }
87
+ }
88
+ export async function runLumaLoginFlow(progress) {
89
+ let playwright;
90
+ try {
91
+ playwright = await _ensurePlaywrightAvailable(progress);
92
+ }
93
+ catch (e) {
94
+ return { ok: false, error: "playwright_unavailable", detail: e?.message };
95
+ }
96
+ try {
97
+ await _installChromium(progress);
98
+ }
99
+ catch (e) {
100
+ return { ok: false, error: "chromium_install_failed", detail: e?.message };
101
+ }
102
+ let browser = null;
103
+ try {
104
+ progress("Opening Luma login window…");
105
+ browser = await playwright.chromium.launch({
106
+ headless: false,
107
+ args: ["--disable-blink-features=AutomationControlled"],
108
+ });
109
+ const context = await browser.newContext({
110
+ userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
111
+ "(KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36",
112
+ viewport: { width: 1280, height: 820 },
113
+ });
114
+ const page = await context.newPage();
115
+ await page.goto("https://luma.com/signin", { waitUntil: "domcontentloaded" });
116
+ progress("Sign in with your Luma account in the window that just opened (Google / passkey / magic-link email all work).");
117
+ const start = Date.now();
118
+ let succeeded = false;
119
+ while (Date.now() - start < LOGIN_TIMEOUT_MS) {
120
+ let url;
121
+ try {
122
+ url = page.url();
123
+ }
124
+ catch {
125
+ return { ok: false, error: "user_cancelled", detail: "Login window was closed before sign-in completed." };
126
+ }
127
+ if (_pageLooksLoggedIn(url)) {
128
+ succeeded = true;
129
+ break;
130
+ }
131
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
132
+ }
133
+ if (!succeeded) {
134
+ return { ok: false, error: "login_timeout", detail: "5-minute sign-in window expired." };
135
+ }
136
+ // Cookie names captured 2026-05-01 from luma.com after signin:
137
+ // luma.auth-session-key — required for every api2.luma.com call
138
+ // luma.user-id — informational, lets tools short-circuit /me
139
+ // Read both luma.com and api2.luma.com cookies — the session is shared
140
+ // across both subdomains via the registrable parent.
141
+ const allCookies = await context.cookies();
142
+ const auth_c = allCookies.find((c) => c.name === "luma.auth-session-key");
143
+ const uid_c = allCookies.find((c) => c.name === "luma.user-id");
144
+ if (!auth_c?.value) {
145
+ return {
146
+ ok: false, error: "cookies_not_found",
147
+ detail: "Sign-in completed but luma.auth-session-key cookie was missing. Try again and ensure the sign-in fully redirects to luma.com/home.",
148
+ };
149
+ }
150
+ let profileName;
151
+ try {
152
+ profileName = await page.evaluate(() => {
153
+ const sel = (s) => {
154
+ const el = document.querySelector(s);
155
+ return el ? (el.textContent || "").trim() : "";
156
+ };
157
+ return (sel('[data-testid="user-menu-name"]')
158
+ || sel('header [class*="UserMenu"] [class*="name"]')
159
+ || sel('a[href*="/u/"] [class*="name"]')
160
+ || undefined);
161
+ });
162
+ }
163
+ catch { /* swallow */ }
164
+ progress(profileName ? `Captured session for ${profileName}.` : "Captured session.");
165
+ await context.close();
166
+ await browser.close();
167
+ return {
168
+ ok: true,
169
+ cookies: { auth_session_key: auth_c.value, user_id: uid_c?.value },
170
+ profileName,
171
+ };
172
+ }
173
+ catch (e) {
174
+ return { ok: false, error: "login_flow_error", detail: e?.message || String(e) };
175
+ }
176
+ finally {
177
+ if (browser) {
178
+ try {
179
+ await browser.close();
180
+ }
181
+ catch { /* already closed */ }
182
+ }
183
+ }
184
+ }
185
+ export function describeOutcome(outcome) {
186
+ if (outcome.ok) {
187
+ return chalk.green(` ✓ Luma session captured${outcome.profileName ? ` (${outcome.profileName})` : ""}.`);
188
+ }
189
+ return chalk.red(` ✗ Luma login: ${outcome.error}${outcome.detail ? ` — ${outcome.detail}` : ""}`);
190
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@melaya/runner",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "Run Melaya AI pipelines locally with your own LM Studio or Ollama models",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,