@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
109 lines • 3.67 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 { METHODS } from "../../policy/constants.js";
import { ScriptSource } from "../../runtime/types.js";
import { snakeToCamel } from "../../utils/normalize.js";
export function ruleToDict(rule) {
const out = {
reason: rule.reason,
commands: [...(rule.commands ?? [])],
paths: [...(rule.paths ?? [])],
};
if (rule.mount !== undefined && rule.mount !== '')
out.mount = rule.mount;
return out;
}
export function ruleFromDict(data) {
return {
reason: data.reason,
commands: data.commands ?? [],
paths: data.paths ?? [],
mount: data.mount ?? '',
};
}
export function commandsToDict(spec) {
return {
allow: spec.allow === null ? null : [...spec.allow],
ask: spec.ask.map(ruleToDict),
deny: spec.deny.map(ruleToDict),
};
}
export function commandsFromDict(data) {
return {
allow: data.allow ?? null,
ask: data.ask.map(ruleFromDict),
deny: data.deny.map(ruleFromDict),
};
}
export function scriptToDict(entry) {
return {
profile: entry.profile,
language: entry.script.language,
source: entry.script.source,
runtime: entry.runtime,
config: { ...entry.config },
env: { ...entry.env },
};
}
export function scriptFromDict(data) {
return {
profile: data.profile,
script: new ScriptSource(data.source, data.language),
runtime: data.runtime,
config: { ...(data.config ?? {}) },
env: { ...(data.env ?? {}) },
};
}
/** A session's policy programs as the record stores them, keyed by the document's method names. */
export function scriptsToDict(scripts) {
return Object.fromEntries(Object.entries(scripts).map(([method, entries]) => [
METHODS[method],
entries.map(scriptToDict),
]));
}
/** A session's policy programs read back from a record, keyed by the document's method names. */
export function scriptsFromDict(data) {
return Object.fromEntries(Object.entries(data).map(([key, entries]) => [snakeToCamel(key), entries.map(scriptFromDict)]));
}
export function decisionToDict(record) {
return {
id: record.id,
session_id: record.sessionId,
command: record.command,
argv: [...record.argv],
cwd: record.cwd,
paths: [...record.paths],
reason: record.reason,
rule: ruleToDict(record.rule),
outcome: record.outcome,
scope: record.scope,
note: record.note,
};
}
export function decisionFromDict(data) {
return {
id: data.id,
sessionId: data.session_id,
command: data.command,
argv: data.argv,
cwd: data.cwd,
paths: data.paths,
reason: data.reason,
rule: ruleFromDict(data.rule),
outcome: data.outcome !== null ? data.outcome : null,
scope: data.scope,
note: data.note,
};
}
//# sourceMappingURL=serialize.js.map