UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

87 lines • 3.83 kB
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= import { jsonlBytes, jsonBytes } from "../render/json.js"; import { materialize } from "../../io/types.js"; import { guardRow, resolveRow } from "./resolve.js"; import { makeRead } from "../hierarchy/read.js"; import { enoent } from "../../errors/fs.js"; import { normalizeDataSource, normalizeDatabase, normalizePage, normalizeRow } from "./normalize.js"; import { listBlockTree, getDataSource, getDatabase, getPage, queryDataSourcePages, } from "./pages.js"; import { detectScope } from "./scope.js"; async function readPageJson(accessor, match, path) { if (match.kind === 'row_json') { const page = await resolveRow(accessor, match, path.virtual); const blocks = await listBlockTree(accessor.transport, match.slots.row_id ?? ''); return jsonBytes(normalizePage(page, blocks)); } await guardRow(accessor, match, path.virtual); const pageId = match.slots.page_id ?? ''; const [page, blocks] = await Promise.all([ getPage(accessor.transport, pageId), listBlockTree(accessor.transport, pageId), ]); return jsonBytes(normalizePage(page, blocks)); } async function readDatabaseJson(accessor, match) { const database = await getDatabase(accessor.transport, match.slots.database_id ?? ''); return jsonBytes(normalizeDatabase(database)); } async function readDataSourceJson(accessor, match) { const dataSource = await getDataSource(accessor.transport, match.slots.data_source_id ?? ''); return jsonBytes(normalizeDataSource(dataSource)); } async function readRowsJsonl(accessor, match) { return materialize(rowsJsonl(accessor, match)); } async function* rowsJsonl(accessor, match, limit) { let remaining = limit ?? Infinity; if (remaining <= 0) return; for await (const page of queryDataSourcePages(accessor.transport, match.slots.data_source_id ?? '')) { const rows = page.filter((row) => row.object === 'page').slice(0, remaining); remaining -= rows.length; if (rows.length > 0) yield jsonlBytes(rows.map(normalizeRow)); if (remaining === 0) return; } } export const read = makeRead(detectScope, { page_json: readPageJson, row_json: readPageJson, database_json: readDatabaseJson, data_source_json: readDataSourceJson, rows_jsonl: readRowsJsonl, }, { parented: true }); /** Render rows one query page at a time; read other files whole. */ export async function* readStream(accessor, path, parent = null) { const match = detectScope(path); if (match.kind !== 'rows_jsonl') { yield await read(accessor, path, null, parent); return; } if (parent === null) throw enoent(path.virtual); yield* rowsJsonl(accessor, match); } /** Render only the first n rows, closing before the next query page. */ export async function firstLines(accessor, path, n, parent = null) { const match = detectScope(path); if (match.kind !== 'rows_jsonl') return null; if (parent === null) throw enoent(path.virtual); return materialize(rowsJsonl(accessor, match, n)); } //# sourceMappingURL=read.js.map