minor: Improve systme prompt for Alias Generation

This commit is contained in:
2026-07-09 10:56:21 +05:30
parent 9642e2bb54
commit 907c3b8ed7
2 changed files with 79 additions and 49 deletions

View File

@@ -77,7 +77,9 @@ async function runAliasResolution(
if (!viewer.aliases.has(target.id)) {
const alias = await aliasGenerator.generate(viewer, target);
viewer.aliases.set(target.id, alias);
console.log(`\n🔍 [Alias Resolved] "${viewer.id}" sees "${target.id}" -> alias: "${alias}"`);
console.log(
`\n[Alias Resolved] "${viewer.id}" sees "${target.id}" -> alias: "${alias}"`,
);
// Save the viewer state with the new alias
coreRepo.saveEntity(viewer, worldState.id);
}
@@ -93,22 +95,28 @@ async function main() {
let logStream: fs.WriteStream | undefined;
if (logFileIndex !== -1 && args[logFileIndex + 1]) {
const logFilePath = path.resolve(args[logFileIndex + 1]);
logStream = fs.createWriteStream(logFilePath, { flags: "w", encoding: "utf-8" });
logStream = fs.createWriteStream(logFilePath, {
flags: "w",
encoding: "utf-8",
});
// Monkeypatch console.log
const originalLog = console.log;
console.log = (...messageArgs: unknown[]) => {
originalLog(...messageArgs);
const text = messageArgs.map(arg => {
if (typeof arg === "object" && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch {
const text =
messageArgs
.map((arg) => {
if (typeof arg === "object" && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch {
return String(arg);
}
}
return String(arg);
}
}
return String(arg);
}).join(" ") + "\n";
})
.join(" ") + "\n";
logStream?.write(text);
};
@@ -116,16 +124,19 @@ async function main() {
const originalError = console.error;
console.error = (...messageArgs: unknown[]) => {
originalError(...messageArgs);
const text = messageArgs.map(arg => {
if (typeof arg === "object" && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch {
const text =
messageArgs
.map((arg) => {
if (typeof arg === "object" && arg !== null) {
try {
return JSON.stringify(arg, null, 2);
} catch {
return String(arg);
}
}
return String(arg);
}
}
return String(arg);
}).join(" ") + "\n";
})
.join(" ") + "\n";
logStream?.write("[ERROR] " + text);
};
@@ -239,31 +250,49 @@ async function main() {
currentWorldState,
entity,
);
console.log(`\n🔍 [VERBOSE] Assembled Prompts for "${entity.id}":`);
console.log(`\n[VERBOSE] Assembled Prompts for "${entity.id}":`);
console.log("\n--- SYSTEM PROMPT ---");
console.log(systemPrompt);
console.log("\n--- USER CONTEXT ---");
console.log(userContext);
console.log("\n--- CONTEXT BREAKDOWN ---");
const userSections = userContext.split("\n\n");
const momentSection = userSections.find(s => s.startsWith("=== CURRENT MOMENT ===")) || "";
const worldSection = userSections.find(s => s.startsWith("=== THE WORLD AS YOU PERCEIVE IT ===")) || "";
const memorySection = userSections.find(s => s.startsWith("=== YOUR RECENT MEMORY ===")) || "";
const momentSection =
userSections.find((s) => s.startsWith("=== CURRENT MOMENT ===")) ||
"";
const worldSection =
userSections.find((s) =>
s.startsWith("=== THE WORLD AS YOU PERCEIVE IT ==="),
) || "";
const memorySection =
userSections.find((s) =>
s.startsWith("=== YOUR RECENT MEMORY ==="),
) || "";
const systemChars = systemPrompt.length;
const momentChars = momentSection.length;
const worldChars = worldSection.length;
const memoryChars = memorySection.length;
const totalChars = systemChars + userContext.length;
const estTokens = (chars: number) => Math.ceil(chars / 4);
console.log(` ├─ System Instructions: ${systemChars.toLocaleString()} chars (~${estTokens(systemChars)} tokens)`);
console.log(` ├─ Current Moment Context: ${momentChars.toLocaleString()} chars (~${estTokens(momentChars)} tokens)`);
console.log(` ├─ World Perception: ${worldChars.toLocaleString()} chars (~${estTokens(worldChars)} tokens)`);
console.log(` ├─ Recent Memory Buffer: ${memoryChars.toLocaleString()} chars (~${estTokens(memoryChars)} tokens)`);
console.log(` TOTAL ESTIMATED INPUT: ${totalChars.toLocaleString()} chars (~${estTokens(totalChars)} tokens)`);
console.log(
` ├─ System Instructions: ${systemChars.toLocaleString()} chars (~${estTokens(systemChars)} tokens)`,
);
console.log(
` Current Moment Context: ${momentChars.toLocaleString()} chars (~${estTokens(momentChars)} tokens)`,
);
console.log(
` ├─ World Perception: ${worldChars.toLocaleString()} chars (~${estTokens(worldChars)} tokens)`,
);
console.log(
` ├─ Recent Memory Buffer: ${memoryChars.toLocaleString()} chars (~${estTokens(memoryChars)} tokens)`,
);
console.log(
` └─ TOTAL ESTIMATED INPUT: ${totalChars.toLocaleString()} chars (~${estTokens(totalChars)} tokens)`,
);
console.log(
"--------------------------------------------------------------------------------",
);
@@ -284,7 +313,7 @@ async function main() {
// Verbose mode: Output decoded intent structures
if (isVerbose) {
console.log(`\n🔍 [VERBOSE] Decoded Intents from Prose:`);
console.log(`\n[VERBOSE] Decoded Intents from Prose:`);
console.log(JSON.stringify(turnResult.intents.intents, null, 2));
console.log(
"--------------------------------------------------------------------------------",
@@ -301,7 +330,7 @@ async function main() {
// Verbose mode: Output architect evaluation
if (isVerbose) {
console.log(`\n🔍 [VERBOSE] Architect Intent Processing:`);
console.log(`\n[VERBOSE] Architect Intent Processing:`);
console.log(` Type: ${intent.type}`);
console.log(` Description: "${intent.description}"`);
if (intent.type === "monologue") {

View File

@@ -11,19 +11,13 @@ export const TimeDeltaSchema = z.object({
export type TimeDelta = z.infer<typeof TimeDeltaSchema>;
export interface IDeltaGenerator<T> {
generate(
worldState: WorldState,
intent: Intent,
): Promise<T>;
generate(worldState: WorldState, intent: Intent): Promise<T>;
}
export class TimeDeltaGenerator implements IDeltaGenerator<TimeDelta> {
constructor(private llmProvider: ILLMProvider) {}
async generate(
worldState: WorldState,
intent: Intent,
): Promise<TimeDelta> {
async generate(worldState: WorldState, intent: Intent): Promise<TimeDelta> {
const systemPrompt = `
You are the Time Delta Generator for the World Architect.
Your task is to judge how much time (in minutes) a proposed action would logically take to execute in the physical world.
@@ -57,7 +51,9 @@ Target IDs: ${intent.targetIds.join(", ") || "(None)"}
});
if (!response.success || !response.data) {
throw new Error(`Failed to generate time delta: ${response.error || "Unknown LLM error"}`);
throw new Error(
`Failed to generate time delta: ${response.error || "Unknown LLM error"}`,
);
}
return response.data;
@@ -74,7 +70,7 @@ export class AliasDeltaGenerator {
constructor(private llmProvider: ILLMProvider) {}
/**
* Generates a natural, subjective descriptive alias for a target entity
* Generates a natural, subjective descriptive alias for a target entity
* based on its visible attributes from the perspective of a viewer entity.
*/
async generate(
@@ -82,18 +78,21 @@ export class AliasDeltaGenerator {
target: import("@omnia/core").Entity,
): Promise<string> {
const visibleAttrs = target.getVisibleAttributesFor(viewer.id);
const attrsStr = visibleAttrs.map((a) => `* ${a.name}: ${a.getValue()}`).join("\n");
const attrsStr = visibleAttrs
.map((a) => `* ${a.name}: ${a.getValue()}`)
.join("\n");
const systemPrompt = `
You are the Alias Delta Generator for the World Architect.
Your task is to generate a natural, subjective, descriptive alias (noun phrase) that a viewer entity would use to refer to a target entity they are seeing for the first time, based ONLY on the target entity's visible public attributes.
Rules:
1. The alias must be a simple, natural, subjective noun phrase (e.g. "the tall woman in the grey jumpsuit", "the red-haired man", "the hooded figure").
1. The alias must be a simple, natural, subjective noun phrase.
2. Base the description strictly on the target's visible attributes. Do not invent details not present in the attributes.
3. Never use raw system IDs or UUIDs in the alias description.
4. Do not use the target's private name attribute unless they have explicit access to it (which is already filtered in the attributes list).
5. Return a structured JSON object containing:
5. Keep the phrase very short. Not more than 5 words. These aliases can also be internal nicknames for those entities.
6. Return a structured JSON object containing:
- "alias": string representing the descriptive alias.
`.trim();
@@ -112,7 +111,9 @@ ${attrsStr || "(No visible attributes)"}
});
if (!response.success || !response.data) {
throw new Error(`Failed to generate alias delta: ${response.error || "Unknown LLM error"}`);
throw new Error(
`Failed to generate alias delta: ${response.error || "Unknown LLM error"}`,
);
}
return response.data.alias;