UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

52 lines • 2.37 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 { sliceWindow } from "../../utils/ranges.js"; import { fileEntry } from "../hierarchy/probe.js"; import { joinLines } from "../slug_tree/read.js"; import { scalarString } from "../slug_tree/rows.js"; import { getDocumentSegments, iterSegmentPages } from "./client.js"; const ENC = new TextEncoder(); export function segmentText(segment) { return scalarString(segment.content) ?? ''; } /** * Read a document, optionally only a byte range of it. * * A document is rendered here from its segments, so its bytes do not exist * until we make them and the window can only be taken afterwards, the same * way the rendered branches of gdrive, slack and discord take theirs. * * Args: * accessor: Dify accessor. * path: the path to read. * entry: what is at the path. * options: `{offset, size}`, the byte window, or absent for the whole file. */ export async function read(accessor, path, entry = null, options) { const found = fileEntry(path, entry); const segments = await getDocumentSegments(accessor, String(found.extra.document_id)); const rendered = ENC.encode(segments.map((segment) => segmentText(segment)).join('\n')); return sliceWindow(rendered, options?.offset ?? 0, options?.size ?? null); } export async function* readStream(accessor, path, entry = null) { const found = fileEntry(path, entry); yield* joinLines(segmentTexts(accessor, String(found.extra.document_id))); } async function* segmentTexts(accessor, documentId) { for await (const page of iterSegmentPages(accessor, documentId)) { for (const segment of page) yield segmentText(segment); } } //# sourceMappingURL=read.js.map