refactor(architect): Add self description for intent decoder and refine context (#22)

This commit is contained in:
2026-07-11 09:11:49 +05:30
parent 9bdc3ca04b
commit bfff93e793
12 changed files with 106 additions and 36 deletions

View File

@@ -25,27 +25,28 @@ export function serializeSubjectiveBufferEntry(
entry: BufferEntry,
viewer: Entity,
): string {
const actorAlias = resolveAlias(viewer, entry.intent.actorId);
const isSelf = viewer.id === entry.intent.actorId;
const targetAliases = entry.intent.targetIds.map((tid) =>
resolveAlias(viewer, tid),
);
let details: string;
const content = entry.intent.description.trim() || entry.intent.originalText.trim();
if (entry.intent.type === "dialogue") {
details = `spoke to ${targetAliases.join(", ") || "someone"}: "${content}"`;
} else if (entry.intent.type === "monologue") {
details = `thought: "${content}"`;
} else {
details = content;
if (entry.outcome) {
if (isSelf) {
let details = (entry.intent.selfDescription || entry.intent.description || entry.intent.originalText).trim();
if (details.length > 0) {
details = details.charAt(0).toUpperCase() + details.slice(1);
}
if (entry.intent.type === "action" && entry.outcome) {
details += ` (Outcome: ${entry.outcome.isValid ? "Succeeded" : `Failed - ${entry.outcome.reason}`})`;
}
return details;
}
return `${actorAlias} ${details}`;
const actorAlias = resolveAlias(viewer, entry.intent.actorId);
const subjectStr = actorAlias.charAt(0).toUpperCase() + actorAlias.slice(1);
let details = (entry.intent.description || entry.intent.originalText).trim();
if (entry.intent.type === "action" && entry.outcome) {
details += ` (Outcome: ${entry.outcome.isValid ? "Succeeded" : `Failed - ${entry.outcome.reason}`})`;
}
return `${subjectStr} ${details}`;
}
export class BufferRepository {

View File

@@ -32,14 +32,15 @@ describe("Subjective Buffer Entry Serializer Tests (Tier 1)", () => {
intent: {
type: "dialogue",
originalText: '"Hello there," Bob said to Charlie.',
description: "Bob greets Charlie",
description: "says, 'Hello there' to the bartender",
selfDescription: "You say, 'Hello there' to the bartender.",
actorId: "bob",
targetIds: ["charlie"],
},
};
const result = serializeSubjectiveBufferEntry(entry, viewer);
expect(result).toBe('the hooded figure spoke to the bartender: "Bob greets Charlie"');
expect(result).toBe("The hooded figure says, 'Hello there' to the bartender");
});
test("serializes action intent with outcome details", () => {
@@ -54,7 +55,8 @@ describe("Subjective Buffer Entry Serializer Tests (Tier 1)", () => {
intent: {
type: "action",
originalText: "Bob tried to break the latch.",
description: "Bob attempts to break the lock latch",
description: "attempts to break the lock latch",
selfDescription: "You attempt to break the lock latch.",
actorId: "bob",
targetIds: [],
},
@@ -65,7 +67,7 @@ describe("Subjective Buffer Entry Serializer Tests (Tier 1)", () => {
};
const result = serializeSubjectiveBufferEntry(entry, viewer);
expect(result).toBe('the hooded figure Bob attempts to break the lock latch (Outcome: Failed - The lock is made of reinforced steel.)');
expect(result).toBe('The hooded figure attempts to break the lock latch (Outcome: Failed - The lock is made of reinforced steel.)');
});
test("serializes self-reference and unfamiliar actors", () => {
@@ -80,13 +82,14 @@ describe("Subjective Buffer Entry Serializer Tests (Tier 1)", () => {
type: "action",
originalText: "I opened the window.",
description: "open the window",
selfDescription: "You open the window.",
actorId: "alice",
targetIds: [],
},
};
const resultSelf = serializeSubjectiveBufferEntry(entrySelf, viewer);
expect(resultSelf).toBe("you open the window");
expect(resultSelf).toBe("You open the window.");
const entryUnfamiliar: BufferEntry = {
id: "entry-unfamiliar",
@@ -96,14 +99,15 @@ describe("Subjective Buffer Entry Serializer Tests (Tier 1)", () => {
intent: {
type: "action",
originalText: "Someone knocked.",
description: "knock on the door",
description: "knocks on the door",
selfDescription: "You knock on the door.",
actorId: "stranger-1",
targetIds: [],
},
};
const resultUnfamiliar = serializeSubjectiveBufferEntry(entryUnfamiliar, viewer);
expect(resultUnfamiliar).toBe("an unfamiliar figure knock on the door");
expect(resultUnfamiliar).toBe("An unfamiliar figure knocks on the door");
});
});
@@ -123,6 +127,7 @@ describe("BufferRepository Persistence Tests (Tier 1)", () => {
type: "action",
originalText: "Alice picked up a stick.",
description: "Alice gathers a stick",
selfDescription: "You gather a stick.",
actorId: "alice",
targetIds: [],
};