@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
78 lines • 3.36 kB
JavaScript
// ========= 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 { Runtime } from "./base.js";
import { resolveProgram } from "./source.js";
/**
* A runtime that interprets one language's code inside a command.
*
* The engine inside a single command (python3, node): the workspace
* splits the line, and a captured stage's operands land here as run().
* Never the whole line; that is LineExecutorMixin's entry point.
*
* The language it interprets is declared once, on its language tier
* (PythonRuntime, JsRuntime), and a script CLI finds its interpreter by
* it (runtimeForLanguage).
*
* run() resolves the program from the operands the way a real
* interpreter does (`-c` in `ctx.flags`, else the script operand read
* through `ctx.vfs`, else piped stdin) and hands it to interpret(). A
* guest sees `ctx.env`, a copy whose writes do not mutate the Mirage
* session; an adapter that means to write the session uses `ctx.session`.
*/
export class LanguageRuntime extends Runtime {
/**
* The head words of this language's interpreter commands, the first
* naming a program run with no command of its own.
*/
static commands = [];
/** What this interpreter calls itself where no file names the program. */
argv0Rules = {};
/**
* Resolve the program from the operands, then interpret it.
*
* @param engine the install's engine.
* @param paths the script operand, if any.
* @param texts the program's argv.
* @param ctx the run's context.
* @param output receives stdout as it comes.
* @param errors receives stderr as it comes.
*/
async run(engine, paths, texts, ctx, output, errors) {
const named = ctx.spec?.name ?? '';
const label = named !== '' ? named : (this.constructor.commands[0] ?? this.name);
const program = await resolveProgram(paths, texts, ctx, label, this.argv0Rules, (name, shown) => this.moduleCode(name, shown), this.name);
if ('exitCode' in program)
return program;
return this.interpret(engine, program, ctx, output, errors);
}
/**
* The program `-m name` runs; null where this runtime cannot run a
* module.
*
* @param _name the module.
* @param _label the command name a not-found message uses.
*/
moduleCode(_name, _label) {
return null;
}
/** Report the bound interpreter's version. */
version(_env, _signal, _timeoutSeconds) {
return Promise.resolve({
stdout: new Uint8Array(),
stderr: new TextEncoder().encode(`${this.name}: version information unavailable\n`),
exitCode: 1,
});
}
}
//# sourceMappingURL=language.js.map