@radcliffetech/symbolos-core
Version:
Core symbolic simulation and execution engine for Symbolos
80 lines (79 loc) • 2.66 kB
JavaScript
import { createSymbolicObject } from "./object-factory";
import chalk from "chalk";
/**
*
* Converts a WorldInstance to a WorldFrame symbolic object.
* This is useful for archiving or storing the world state.
*/
export function toWorldFrame(world) {
return createSymbolicObject("WorldFrame", {
description: `World frame for tick ${world.tick}`,
id: `frame-${world.tick}`,
tick: world.tick,
step: world.step,
runId: world.runId,
pipelineId: world.pipelineId,
members: Array.from(world.artifacts.values()),
metadata: {
artifactCount: world.artifacts.size,
},
});
}
/**
* Converts a WorldFrame symbolic object back to a WorldInstance.
* This is useful for restoring the world state from an archived frame.
*/
export function toWorldInstance(frame) {
return {
id: frame.id || `world-${frame.tick}`,
tick: frame.tick,
step: frame.step,
runId: frame.runId,
pipelineId: frame.pipelineId,
artifacts: new Map(frame.members.map((m) => [m.id, m])),
context: new Map(),
};
}
export function addToWorld(world, obj) {
const list = Array.isArray(obj) ? obj : [obj];
for (const o of list) {
if (!o.createdAt)
o.createdAt = new Date().toISOString();
world.artifacts.set(o.id, o);
}
}
/**
* Removes an object or objects from the world by their ID(s).
* If the object is not found, a warning is logged.
*
* @param world - The WorldInstance from which to remove the object(s).
* @param id - The ID(s) of the object(s) to remove. Can be a single ID, an array of IDs,
* or SymbolicObject(s) whose IDs will be extracted.
*/
export function removeFromWorld(world, id) {
const ids = Array.isArray(id)
? id.map((i) => (typeof i === "string" ? i : i.id))
: [typeof id === "string" ? id : id.id];
for (const objId of ids) {
if (world.artifacts.has(objId)) {
world.artifacts.delete(objId);
}
else {
console.warn(chalk.yellow(`[symbolos] Warning: Object with ID ${objId} not found in world.`));
}
}
}
export function hasType(world, type) {
return Array.from(world.artifacts.values()).some((o) => o.type === type);
}
export function getFromWorldByType(world, type) {
return Array.from(world.artifacts.values()).filter((o) => o.type === type);
}
export function getFromWorldById(world, id) {
return world.artifacts.get(id);
}
export function getFromWorldByIds(world, ids) {
return ids
.map((id) => world.artifacts.get(id))
.filter((o) => o !== undefined);
}