Something went wrong. Try again.
Parametric learning in your code harness.
Something went wrong. Try again.
TypeScript
at letta-code
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { appendFileSync } from "node:fs";import { mkdir } from "node:fs/promises";import { randomBytes } from "node:crypto";import path from "node:path";import { countJsonlRows, readJsonl, rel } from "./cli.ts";import { hardenMachinePath, machineDataFile, machineProjectDataDir, readMachineSettings, type MachineDataKind,} from "./storage.ts";import { extractMessageText, previewText, type TaggedExampleTag, type TrajectoryRecord } from "./trajectory.ts";
/** 12-character lowercase hex. Unique enough for local JSONL rows; not a backfill key. */export function newMachineRowId(): string { return randomBytes(6).toString("hex");}
export function displayRowId(row: Record<string, unknown>): string { return typeof row.id === "string" && row.id.trim() ? row.id.trim() : "(no id)";}
export type TaggedExampleInput = { tag: TaggedExampleTag; messages: Array<Record<string, unknown>>; records?: TrajectoryRecord[]; note?: string; outFile: string; source?: string; sessionFile?: string; userId?: string; assistantId?: string; kind?: "final" | "trace" | "auto-compact" | "auto-headless"; trigger?: string; id?: string;};
export function buildTaggedExampleRow(options: Omit<TaggedExampleInput, "outFile">): Record<string, unknown> { const source = options.source ?? (options.sessionFile ? path.basename(options.sessionFile) : undefined); const row: Record<string, unknown> = { id: options.id ?? newMachineRowId(), messages: options.messages, tag: options.tag, kind: options.kind ?? "final", }; if (options.trigger) row.trigger = options.trigger; if (options.records) row.trajectory = options.records; if (options.note) row.note = options.note; if (source) row.source = source; if (options.userId || options.assistantId) { row.entryIds = { user: options.userId, assistant: options.assistantId }; } return row;}
export async function appendTaggedExample(options: TaggedExampleInput): Promise<{ row: Record<string, unknown>; count: number }> { const row = buildTaggedExampleRow(options); await mkdir(path.dirname(options.outFile), { recursive: true, mode: 0o700 }); appendFileSync(options.outFile, `${JSON.stringify(row)}\n`, { encoding: "utf8", mode: 0o600 }); hardenMachinePath(options.outFile, 0o600); return { row, count: countJsonlRows(options.outFile) };}
export function formatTaggedExampleList(cwd: string, files: string[]): string { const sections = files.map((file) => { const rows = readJsonl(file); if (rows.length === 0) return `## ${rel(cwd, file)}\nNo examples yet.`; const items = rows.slice(-10).map((row, idx) => { const messages = Array.isArray(row.messages) ? row.messages as Array<{ role?: string; content?: unknown }> : []; const user = extractMessageText(messages.find((message) => message.role === "user")?.content); const assistant = extractMessageText(messages.find((message) => message.role === "assistant")?.content); const n = rows.length - Math.min(rows.length, 10) + idx + 1; return `${n}. ${displayRowId(row)} [${String(row.tag ?? "?")}] ${previewText(user || assistant || JSON.stringify(row))}`; }); return `## ${rel(cwd, file)} (${rows.length})\n${items.join("\n")}`; }); return sections.join("\n\n");}
export const ROW_LOOKUP_ORDER: MachineDataKind[] = ["observed", "traces", "train"];
export type FoundRow = { id: string; kind: MachineDataKind; file: string; row: Record<string, unknown>;};
export function findRowById(cwd: string, id: string): FoundRow | undefined { const needle = id.trim(); if (!needle) return undefined; for (const kind of ROW_LOOKUP_ORDER) { const file = machineDataFile(cwd, kind); for (const row of readJsonl(file)) { if (typeof row.id === "string" && row.id === needle) { return { id: needle, kind, file, row }; } } } return undefined;}
export function originalTurnText(row: Record<string, unknown>): string { const messages = Array.isArray(row.messages) ? row.messages as Array<{ role?: string; content?: unknown }> : []; for (const message of messages) { if (message?.role === "user") { const text = extractMessageText(message.content); if (text) return text; } } const trajectory = Array.isArray(row.trajectory) ? row.trajectory as Array<{ role?: string; content?: unknown }> : []; for (const record of trajectory) { if (record?.role === "user") { const text = extractMessageText(record.content); if (text) return text; } } return "";}
export function buildCorrectionPrompt(originalTurn: string, correction: string): string { const task = originalTurn.trim(); const extra = correction.trim(); if (!task) return extra; if (!extra) return task; return `${task}\n\nCorrection: ${extra}`;}
export function formatMachineDataStatus(cwd: string): string { const settings = readMachineSettings(cwd); const files = (["train", "traces", "observed", "rejected"] as const).map((kind) => { const file = machineDataFile(cwd, kind); const count = countJsonlRows(file); return `- ${kind}: \`${file}\` (${count} row${count === 1 ? "" : "s"})`; }); return [ `Project store: \`${machineProjectDataDir(cwd)}\``, `Automatic observation: ${settings.autoTrace ? "on" : "off"}`, "", ...files, "", "Default store is `~/.machine`. Set `MACHINE_DATA_DIR` to override the base directory. Machine still creates a per-project subdirectory.", ].join("\n");}