UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

58 lines • 2.51 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 { commandFiles, resolveOrEmpty } from "../files.js"; import { specOf } from "../../../spec/builtins.js"; import { FlagView } from "../../../spec/flag_view.js"; import { IOResult } from "../../../../io/types.js"; import { StreamRefusal } from "../../../../rs/errors.js"; import { refused } from "../../../../rs/scan.js"; import { openStreamSync, runStream } from "../../../../rs/stream.js"; import { byteView } from "../../../../shell/bytes.js"; import { reportedStream } from "../../utils/operands.js"; /** The lines that start with the prefix; one makes the exit 0. Mirrors * Python's _matched. */ async function* matched(source, config, io, signal) { for await (const chunk of runStream('look', config, source, signal)) { io.exitCode = 0; yield chunk; } } const look = async (_accessor, paths, texts, ctx) => { const files = commandFiles(ctx, paths); const resolved = await resolveOrEmpty(files, paths); const words = texts.length > 0 ? [...texts, ...resolved.map((path) => path.rawPath)] : []; const config = { operands: words.map((word) => byteView(word)), fold: new FlagView(ctx.flags, specOf('look')).asBool('f'), }; try { openStreamSync('look', config).close(); } catch (err) { if (!(err instanceof StreamRefusal)) throw err; return [null, refused(err)]; } const io = new IOResult({ exitCode: 1 }); const path = resolved[0]; const source = path === undefined ? (ctx.stdin ?? new Uint8Array()) : files.read(path); const output = matched(source, config, io, ctx.signal); return [path === undefined ? output : reportedStream(output, io, path, 'look'), io]; }; export const BUILDER = { name: 'look', read: true, fn: look, }; //# sourceMappingURL=look.js.map