oh-langfuse 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/cli.js +78 -98
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env node
2
- import fs from "node:fs";
3
- import path from "node:path";
4
- import readline from "node:readline";
5
- import { createInterface } from "node:readline/promises";
6
- import { fileURLToPath } from "node:url";
7
- import { spawnSync } from "node:child_process";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { createInterface } from "node:readline/promises";
5
+ import { fileURLToPath } from "node:url";
6
+ import { spawnSync } from "node:child_process";
8
7
 
9
8
  const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
10
9
  const scriptsDir = path.join(rootDir, "scripts");
@@ -271,41 +270,22 @@ async function askYesNo(rl, label, { defaultValue = true } = {}) {
271
270
  }
272
271
  }
273
272
 
274
- function keySeq(raw, key = {}) {
275
- return String(key.sequence ?? raw ?? "");
273
+ function rawKeySeq(raw) {
274
+ if (Buffer.isBuffer(raw)) return raw.toString("latin1");
275
+ return String(raw ?? "");
276
276
  }
277
277
 
278
- function isCtrlC(raw, key = {}) {
279
- return Boolean(key.ctrl && key.name === "c") || keySeq(raw, key) === "\x03";
280
- }
281
-
282
- function isEscape(raw, key = {}) {
283
- const seq = keySeq(raw, key);
284
- return key.name === "escape" || seq === "\x1b";
285
- }
286
-
287
- function isUpKey(raw, key = {}) {
288
- const seq = keySeq(raw, key);
289
- return key.name === "up" || seq === "\x1b[A" || seq === "\x1bOA";
290
- }
291
-
292
- function isDownKey(raw, key = {}) {
293
- const seq = keySeq(raw, key);
294
- return key.name === "down" || seq === "\x1b[B" || seq === "\x1bOB";
295
- }
296
-
297
- function isEnterKey(raw, key = {}) {
298
- const seq = keySeq(raw, key);
299
- return key.name === "return" || key.name === "enter" || seq === "\r" || seq === "\n";
300
- }
301
-
302
- function isSpaceKey(raw, key = {}) {
303
- return key.name === "space" || keySeq(raw, key) === " ";
304
- }
305
-
306
- function numberKey(raw, key = {}) {
307
- const seq = keySeq(raw, key);
308
- return /^[1-9]$/.test(seq) ? Number.parseInt(seq, 10) : Number.NaN;
278
+ function parseRawKey(raw) {
279
+ const seq = rawKeySeq(raw);
280
+ if (seq === "\x03") return { name: "ctrl-c", sequence: seq };
281
+ if (seq === "\x1b[A" || seq === "\x1bOA" || seq === "\x00H" || seq === "\xe0H") return { name: "up", sequence: seq };
282
+ if (seq === "\x1b[B" || seq === "\x1bOB" || seq === "\x00P" || seq === "\xe0P") return { name: "down", sequence: seq };
283
+ if (seq === "\r" || seq === "\n" || seq === "\r\n") return { name: "enter", sequence: seq };
284
+ if (seq === " ") return { name: "space", sequence: seq };
285
+ if (seq === "\x1b") return { name: "escape", sequence: seq };
286
+ if (/^[1-9]$/.test(seq)) return { name: "number", number: Number.parseInt(seq, 10), sequence: seq };
287
+ if (seq.length === 1) return { name: seq.toLowerCase(), sequence: seq };
288
+ return { name: "", sequence: seq };
309
289
  }
310
290
 
311
291
  function renderChoiceScreen(label, choices, index, options = {}) {
@@ -329,47 +309,47 @@ function renderChoiceScreen(label, choices, index, options = {}) {
329
309
  });
330
310
  }
331
311
 
332
- async function askChoice(rl, label, choices, options = {}) {
333
- if (process.stdin.isTTY && process.stdout.isTTY) {
334
- rl.pause();
335
- return await new Promise((resolve) => {
336
- let index = 0;
312
+ async function askChoice(rl, label, choices, options = {}) {
313
+ if (process.stdin.isTTY && process.stdout.isTTY) {
314
+ rl.pause();
315
+ return await new Promise((resolve) => {
316
+ let index = 0;
337
317
  const stdin = process.stdin;
338
-
339
- function cleanup(value) {
340
- stdin.off("keypress", onKeypress);
341
- if (stdin.isTTY) stdin.setRawMode(false);
342
- stdin.pause();
343
- rl.resume();
344
- clearScreen();
345
- resolve(value);
346
- }
347
-
348
- function onKeypress(raw, key = {}) {
349
- if (isCtrlC(raw, key)) return cleanup("exit");
350
- if (isUpKey(raw, key)) {
318
+
319
+ function cleanup(value) {
320
+ stdin.off("data", onData);
321
+ if (stdin.isTTY) stdin.setRawMode(false);
322
+ stdin.pause();
323
+ rl.resume();
324
+ clearScreen();
325
+ resolve(value);
326
+ }
327
+
328
+ function onData(raw) {
329
+ const key = parseRawKey(raw);
330
+ if (key.name === "ctrl-c") return cleanup("exit");
331
+ if (key.name === "up") {
351
332
  index = (index - 1 + choices.length) % choices.length;
352
333
  renderChoiceScreen(label, choices, index, options);
353
334
  return;
354
335
  }
355
- if (isDownKey(raw, key)) {
336
+ if (key.name === "down") {
356
337
  index = (index + 1) % choices.length;
357
338
  renderChoiceScreen(label, choices, index, options);
358
339
  return;
359
340
  }
360
- if (keySeq(raw, key).toLowerCase() === "q" || isEscape(raw, key)) return cleanup("exit");
361
- if (isEnterKey(raw, key)) return cleanup(choices[index].value);
362
- const num = numberKey(raw, key);
341
+ if (key.name === "q" || key.name === "escape") return cleanup("exit");
342
+ if (key.name === "enter") return cleanup(choices[index].value);
343
+ const num = key.name === "number" ? key.number : Number.NaN;
363
344
  if (Number.isInteger(num) && choices[num - 1]) return cleanup(choices[num - 1].value);
364
345
  }
365
-
366
- readline.emitKeypressEvents(stdin);
367
- if (stdin.isTTY) stdin.setRawMode(true);
368
- stdin.on("keypress", onKeypress);
369
- stdin.resume();
370
- renderChoiceScreen(label, choices, index, options);
371
- });
372
- }
346
+
347
+ if (stdin.isTTY) stdin.setRawMode(true);
348
+ stdin.on("data", onData);
349
+ stdin.resume();
350
+ renderChoiceScreen(label, choices, index, options);
351
+ });
352
+ }
373
353
 
374
354
  console.log("");
375
355
  console.log(label);
@@ -413,52 +393,52 @@ async function askMultiChoice(rl, label, choices, options = {}) {
413
393
  let index = 0;
414
394
  const selected = new Set(choices.filter((choice) => choice.selected).map((choice) => choice.value));
415
395
  const stdin = process.stdin;
416
-
417
- function cleanup(value) {
418
- stdin.off("keypress", onKeypress);
419
- if (stdin.isTTY) stdin.setRawMode(false);
420
- stdin.pause();
421
- rl.resume();
422
- clearScreen();
423
- resolve(value);
396
+
397
+ function cleanup(value) {
398
+ stdin.off("data", onData);
399
+ if (stdin.isTTY) stdin.setRawMode(false);
400
+ stdin.pause();
401
+ rl.resume();
402
+ clearScreen();
403
+ resolve(value);
424
404
  }
425
405
 
426
406
  function toggle() {
427
407
  const value = choices[index].value;
428
408
  if (selected.has(value)) selected.delete(value);
429
409
  else selected.add(value);
430
- renderMultiChoiceScreen(label, choices, index, selected, options);
431
- }
432
-
433
- function onKeypress(raw, key = {}) {
434
- if (isCtrlC(raw, key)) return cleanup([]);
435
- if (isUpKey(raw, key)) {
410
+ renderMultiChoiceScreen(label, choices, index, selected, options);
411
+ }
412
+
413
+ function onData(raw) {
414
+ const key = parseRawKey(raw);
415
+ if (key.name === "ctrl-c") return cleanup([]);
416
+ if (key.name === "up") {
436
417
  index = (index - 1 + choices.length) % choices.length;
437
418
  renderMultiChoiceScreen(label, choices, index, selected, options);
438
419
  return;
439
420
  }
440
- if (isDownKey(raw, key)) {
421
+ if (key.name === "down") {
441
422
  index = (index + 1) % choices.length;
442
423
  renderMultiChoiceScreen(label, choices, index, selected, options);
443
424
  return;
444
425
  }
445
- if (keySeq(raw, key).toLowerCase() === "q" || isEscape(raw, key)) return cleanup([]);
446
- if (isSpaceKey(raw, key)) return toggle();
447
- if (isEnterKey(raw, key)) return cleanup([...selected]);
448
- const num = numberKey(raw, key);
426
+ if (key.name === "q" || key.name === "escape") return cleanup([]);
427
+ if (key.name === "space") return toggle();
428
+ if (key.name === "enter") return cleanup([...selected]);
429
+ const num = key.name === "number" ? key.number : Number.NaN;
449
430
  if (Number.isInteger(num) && choices[num - 1]) {
450
431
  index = num - 1;
451
432
  toggle();
452
- }
453
- }
454
-
455
- readline.emitKeypressEvents(stdin);
456
- if (stdin.isTTY) stdin.setRawMode(true);
457
- stdin.on("keypress", onKeypress);
458
- stdin.resume();
459
- renderMultiChoiceScreen(label, choices, index, selected, options);
460
- });
461
- }
433
+ }
434
+ }
435
+
436
+ if (stdin.isTTY) stdin.setRawMode(true);
437
+ stdin.on("data", onData);
438
+ stdin.resume();
439
+ renderMultiChoiceScreen(label, choices, index, selected, options);
440
+ });
441
+ }
462
442
 
463
443
  console.log("");
464
444
  console.log(label);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oh-langfuse",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Use npm scripts to configure Claude Code / OpenCode / Codex with Langfuse tracing.",