@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
74 lines • 2.92 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 { parseLimit } from "../utils/size.js";
/**
* A runtime's `maxMemory` in bytes, null for no cap.
*
* Spelled as sandlock spells its own (`10M`, `512M`, `1G`), or a count of
* bytes. Mirrors the Python `memory_limit`.
*
* Args:
* value: the setting as configured.
*
* Throws:
* Error: the setting is not a size.
*/
export function memoryLimit(value) {
if (value === undefined || value === null)
return null;
return parseLimit(value, 'max_memory');
}
export const HOME_CONFIG_KEYS = ['home'];
/**
* Where a runtime's engine is: the config's `home`, else `envVar`'s value,
* else undefined for the runtime's built-in default. Mirrors Python's
* `resolve_home`.
*/
export function resolveHome(config, envVar) {
if (config.home !== undefined && config.home !== '')
return config.home;
const proc = globalThis.process;
const value = proc?.env?.[envVar];
return value !== undefined && value !== '' ? value : undefined;
}
/**
* A constructor's config option as the runtime's own config, mirroring
* Python's RuntimeConfig.coerce: keys outside the runtime's list fail
* loud (Python gets this from the dataclass raising TypeError; a TS
* object spread would silently swallow a typo key without it).
*
* Args:
* value: the caller's config block, or undefined for the defaults.
* keys: the field names this runtime accepts.
* label: what to call the config in the failure message.
*/
export function coerceRuntimeConfig(value, keys, label = 'runtime') {
const config = value ?? {};
for (const key of Object.keys(config)) {
if (!keys.includes(key)) {
const known = keys.length > 0 ? `expected: ${keys.map((k) => `'${k}'`).join(', ')}` : 'none allowed';
throw new Error(`unknown ${label} config key '${key}' (${known})`);
}
}
if ('env' in config) {
const env = config.env;
if (env === null ||
typeof env !== 'object' ||
Array.isArray(env) ||
Object.values(env).some((value) => typeof value !== 'string'))
throw new Error('runtime env must map strings to strings');
}
return { ...config };
}
//# sourceMappingURL=config.js.map