Something went wrong. Try again.
Parametric learning in your code harness.
Something went wrong. Try again.
TypeScript
at letta-code
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import { existsSync, readFileSync } from "node:fs";import path from "node:path";
/** * Split a raw command string the way a POSIX shell would, honoring single * quotes, double quotes, and backslash escapes (except inside single quotes). */export function shellSplit(input: string): string[] { const out: string[] = []; let current = ""; let quote: "'" | '"' | undefined; let escaping = false;
for (const ch of input.trim()) { if (escaping) { current += ch; escaping = false; continue; } if (ch === "\\" && quote !== "'") { escaping = true; continue; } if ((ch === "'" || ch === '"') && !quote) { quote = ch; continue; } if (ch === quote) { quote = undefined; continue; } if (/\s/.test(ch) && !quote) { if (current) out.push(current); current = ""; continue; } current += ch; } if (current) out.push(current); return out;}
/** * Parse `--flag`, `--key value`, and `--key=value` tokens. Anything that is * not an option is collected positionally. A lone flag becomes `true`; a flag * followed by a non-option token consumes it as its value. */export function parseOptions(tokens: string[]): { positional: string[]; options: Record<string, string | boolean> } { const positional: string[] = []; const options: Record<string, string | boolean> = {}; for (let i = 0; i < tokens.length; i++) { const token = tokens[i]!; if (!token.startsWith("--")) { positional.push(token); continue; } const raw = token.slice(2); const eq = raw.indexOf("="); if (eq >= 0) { options[raw.slice(0, eq)] = raw.slice(eq + 1); continue; } const next = tokens[i + 1]; if (next && !next.startsWith("--")) { options[raw] = next; i++; } else { options[raw] = true; } } return { positional, options };}
/** Interpret an option value as a boolean flag, returning a stray value as leftover. */export function flagOn(options: Record<string, string | boolean>, name: string): { on: boolean; leftover?: string } { const value = options[name]; if (value === undefined) return { on: false }; if (value === true || value === "true" || value === "1") return { on: true }; if (value === false || value === "false" || value === "0") return { on: false }; return { on: true, leftover: String(value) };}
/** Render a path relative to cwd when it lives under cwd, otherwise as-is. */export function rel(cwd: string, filePath?: string) { if (!filePath) return ""; const r = path.relative(cwd, filePath); return r && !r.startsWith("..") ? r : filePath;}
/** * Read JSONL rows, skipping lines that fail to parse. Used for listings and * counts where one corrupted line should not take the whole command down; * strict validation lives in the dataset validators. */export function readJsonl(filePath: string): Record<string, unknown>[] { if (!existsSync(filePath)) return []; return readFileSync(filePath, "utf8") .split(/\n/) .flatMap((line) => { const trimmed = line.trim(); if (!trimmed) return []; try { return [JSON.parse(trimmed) as Record<string, unknown>]; } catch { return []; } });}
/** Count JSONL rows without parsing, so counts survive corrupted lines. */export function countJsonlRows(filePath: string): number { if (!existsSync(filePath)) return 0; return readFileSync(filePath, "utf8").split(/\n/).filter((line) => line.trim()).length;}