From 8148e7cbbb48c87f9adf67e22d4b3e7544d128a1 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Mon, 6 Jul 2026 12:42:43 +0530 Subject: [PATCH] refactor: Use a generic serializer for attributableObject and use polymorphism for world --- packages/architect/src/architect.ts | 2 +- packages/architect/src/index.ts | 2 +- .../src/{llmValidator.ts => llm-validator.ts} | 36 +------------------ packages/core/src/attribute.ts | 10 ++++++ packages/core/src/world.ts | 29 +++++++++++++++ 5 files changed, 42 insertions(+), 37 deletions(-) rename packages/architect/src/{llmValidator.ts => llm-validator.ts} (67%) diff --git a/packages/architect/src/architect.ts b/packages/architect/src/architect.ts index 5824434..dd5f4bc 100644 --- a/packages/architect/src/architect.ts +++ b/packages/architect/src/architect.ts @@ -1,5 +1,5 @@ import { WorldState } from "@omnia/core"; -import { LLMValidator, ValidationResult } from "./llmValidator.js"; +import { LLMValidator, ValidationResult } from "./llm-validator.js"; import { ILLMProvider } from "@omnia/llm"; export class Architect { diff --git a/packages/architect/src/index.ts b/packages/architect/src/index.ts index 21dbc46..638c0e0 100644 --- a/packages/architect/src/index.ts +++ b/packages/architect/src/index.ts @@ -1,2 +1,2 @@ -export * from "./llmValidator.js"; +export * from "./llm-validator.js"; export * from "./architect.js"; diff --git a/packages/architect/src/llmValidator.ts b/packages/architect/src/llm-validator.ts similarity index 67% rename from packages/architect/src/llmValidator.ts rename to packages/architect/src/llm-validator.ts index 561244e..48a95f6 100644 --- a/packages/architect/src/llmValidator.ts +++ b/packages/architect/src/llm-validator.ts @@ -29,7 +29,7 @@ export class LLMValidator { } // 1. Serialize the objective world state for the LLM - const serializedWorld = this.serializeWorldState(worldState); + const serializedWorld = worldState.serialize(); // 2. Build the prompts const systemPrompt = ` @@ -75,38 +75,4 @@ Decide if the proposed action is logically valid and physically possible. return response.data; } - - private serializeWorldState(worldState: WorldState): string { - const lines: string[] = []; - - // Serialize world attributes - if (worldState.attributes.size > 0) { - lines.push("World Attributes:"); - for (const [name, attr] of worldState.attributes.entries()) { - lines.push( - ` - ${name}: ${attr.getValue()} (Visibility: ${attr.getVisibility()})`, - ); - } - } - - // Serialize entities and their attributes - lines.push("Entities:"); - for (const entity of worldState.entities.values()) { - lines.push(` - Entity [ID: ${entity.id}]:`); - if (entity.attributes.size > 0) { - for (const [name, attr] of entity.attributes.entries()) { - const aclList = Array.from(attr.getAllowedEntities()); - const aclStr = - aclList.length > 0 ? ` (Visible to: ${aclList.join(", ")})` : ""; - lines.push( - ` * ${name}: ${attr.getValue()} (Visibility: ${attr.getVisibility()})${aclStr}`, - ); - } - } else { - lines.push(" * (No attributes)"); - } - } - - return lines.join("\n"); - } } diff --git a/packages/core/src/attribute.ts b/packages/core/src/attribute.ts index 8776f32..3564a36 100644 --- a/packages/core/src/attribute.ts +++ b/packages/core/src/attribute.ts @@ -112,4 +112,14 @@ export abstract class AttributableObject implements IAttribute { attr.hasAccess(viewerId), ); } + + serialize(): string { + const lines: string[] = []; + for (const [name, attr] of this.attributes.entries()) { + const aclList = Array.from(attr.getAllowedEntities()); + const aclStr = aclList.length > 0 ? ` (Visible to: ${aclList.join(", ")})` : ""; + lines.push(`* ${name}: ${attr.getValue()} (Visibility: ${attr.getVisibility()})${aclStr}`); + } + return lines.join("\n"); + } } diff --git a/packages/core/src/world.ts b/packages/core/src/world.ts index 50f4801..4ac97b1 100644 --- a/packages/core/src/world.ts +++ b/packages/core/src/world.ts @@ -27,4 +27,33 @@ export class WorldState extends AttributableObject { getEntity(id: string): Entity | undefined { return this.entities.get(id); } + + override serialize(): string { + const lines: string[] = []; + + // 1. Serialize self attributes (world attributes) + const selfSerialized = super.serialize(); + if (selfSerialized) { + lines.push("World Attributes:"); + lines.push(selfSerialized.split("\n").map(l => " " + l).join("\n")); + } + + // 2. Serialize entities + lines.push("Entities:"); + if (this.entities.size > 0) { + for (const entity of this.entities.values()) { + lines.push(` - Entity [ID: ${entity.id}]:`); + const entitySerialized = entity.serialize(); + if (entitySerialized) { + lines.push(entitySerialized.split("\n").map(l => " " + l).join("\n")); + } else { + lines.push(" * (No attributes)"); + } + } + } else { + lines.push(" (No entities)"); + } + + return lines.join("\n"); + } }