@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
53 lines • 2.39 kB
JavaScript
import { materialize } from "../../io/types.js";
import { FileType } from "../../types.js";
import { eisdir, enoent } from "../../errors/fs.js";
import { jsonBytes, jsonlBytes } from "../render/json.js";
import { LEAVES, parts, runVars } from "./pathing.js";
import { RUN, RUN_CONFIG, RUN_SUMMARY } from "./queries.js";
import { runMetadata } from "./metadata.js";
import { parentSpec, rowsOf } from "../hierarchy/probe.js";
import { readdir } from "./readdir.js";
async function isDirectory(accessor, path, name) {
const listed = rowsOf(await readdir(accessor, parentSpec(path)));
return listed.some((row) => row.name === name && row.type === FileType.DIRECTORY);
}
export async function* readStream(accessor, path) {
const ps = parts(accessor, path);
if (ps.length > 4 && ps[3] === 'files') {
const file = await accessor.client.file(runVars(ps), ps.slice(4).join('/'));
if (file) {
yield* accessor.client.download(file.directUrl === undefined || file.directUrl === null || file.directUrl === ''
? file.url
: file.directUrl);
return;
}
}
if (ps.length !== 4 || !LEAVES.includes(ps[3] ?? '')) {
if (ps.length === 0 || (await isDirectory(accessor, path, ps.at(-1) ?? '')))
throw eisdir(path);
throw enoent(path);
}
const variables = runVars(ps);
if (ps[3] === 'history.jsonl') {
for await (const row of accessor.client.history(variables))
yield jsonlBytes([row]);
return;
}
const query = ps[3] === 'run.json' ? RUN : ps[3] === 'config.json' ? RUN_CONFIG : RUN_SUMMARY;
const run = await accessor.client.run(variables, query);
if (ps[3] === 'run.json')
yield jsonBytes(runMetadata(run, variables));
else if (ps[3] === 'config.json') {
const raw = run.config === '' ? '{}' : (run.config ?? '{}');
const config = typeof raw === 'string' ? JSON.parse(raw) : raw;
yield jsonBytes(Object.fromEntries(Object.entries(config).map(([k, v]) => [k, v.value])));
}
else {
const raw = run.summaryMetrics === '' ? '{}' : (run.summaryMetrics ?? '{}');
yield jsonBytes(typeof raw === 'string' ? JSON.parse(raw) : raw);
}
}
export async function read(accessor, path) {
return materialize(readStream(accessor, path));
}
//# sourceMappingURL=read.js.map