refactor(llm, memory): Use generic types prompt builder and prompt component for actor,intent and handoff

This commit is contained in:
2026-07-19 18:20:28 +05:30
parent 1e34becec7
commit ee25bf4a4c
11 changed files with 267 additions and 158 deletions

View File

@@ -39,44 +39,13 @@ export async function runHandoffResolution(session: SimSession): Promise<void> {
worldState.clock.get(),
);
if (ran) {
const lastResult = (handoffEngine as any).lastResult;
const lastCall =
session.handoffProvider.lastCalls?.[
(session.handoffProvider.lastCalls?.length || 0) - 1
];
const info = session.entities.find((e) => e.id === entity.id);
const entityName = info?.name || entity.id;
let handoffPrompt = undefined;
if (lastCall) {
const header = "Cognitive Buffer Candidates for Handoff:";
const userContext = lastCall.userContext;
const idx = userContext.indexOf(header);
let contextStr = userContext;
let candidatesStr = "";
if (idx !== -1) {
contextStr = userContext.substring(0, idx).trim();
candidatesStr = userContext.substring(idx).trim();
}
handoffPrompt = {
systemPrompt: lastCall.systemPrompt,
userContext: lastCall.userContext,
components: [
{
label: "System Prompt",
type: "system",
content: lastCall.systemPrompt,
},
{ label: "Entity Context", type: "world", content: contextStr },
{
label: "Cognitive Candidates",
type: "input",
content: candidatesStr,
},
],
};
}
session.log.push({
turn: session.turn,
@@ -86,9 +55,15 @@ export async function runHandoffResolution(session: SimSession): Promise<void> {
intents: [],
timestamp: worldState.clock.get().toISOString(),
isHandoff: true,
rawPrompt: handoffPrompt,
rawPrompt: lastResult
? {
systemPrompt: lastResult.systemPrompt || "",
userContext: lastResult.userContext || "",
components: lastResult.promptComponents,
}
: undefined,
usage: lastCall?.usage,
handoffResult: lastCall?.response,
handoffResult: lastResult?.response || lastCall?.response,
});
}
}

View File

@@ -168,7 +168,7 @@ export async function processNpcTurn(
rawPrompt: {
systemPrompt: result.systemPrompt || "",
userContext: result.userContext || "",
sections: result.promptComponents,
components: result.promptComponents,
},
};
@@ -267,7 +267,7 @@ export async function executePlayerAction(
rawPrompt: {
systemPrompt: result.systemPrompt || ctx.systemPrompt,
userContext: result.userContext || ctx.userContext,
sections: result.promptComponents,
components: result.promptComponents,
},
};
@@ -279,9 +279,26 @@ export async function executePlayerAction(
session.decoderProvider.lastCalls[
session.decoderProvider.lastCalls.length - 1
];
const proseHeader = "=== NARRATIVE PROSE ===";
const userContext = call.userContext;
const idx = userContext.indexOf(proseHeader);
let contextStr = userContext;
let proseStr = "";
if (idx !== -1) {
contextStr = userContext.substring(0, idx).trim();
proseStr = userContext.substring(idx).trim();
}
entry.decoderPrompt = {
systemPrompt: call.systemPrompt,
userContext: call.userContext,
components: [
{ label: "System Prompt", type: "system", content: call.systemPrompt },
{ label: "Decoder Context", type: "world", content: contextStr },
{ label: "Narrative Prose", type: "input", content: proseStr },
],
};
entry.decoderUsage = call.usage;
}