fix(workbench): persist user message through event projection (#2045)
This commit is contained in:
@@ -144,6 +144,7 @@ export async function writeWorkbenchProjectionEvent({ runtimeStore = null, event
|
|||||||
const previousFinalText = finalResponseTextValue(previousCheckpoint?.finalResponse, previousCheckpoint?.assistantText, previousCheckpoint?.finalText);
|
const previousFinalText = finalResponseTextValue(previousCheckpoint?.finalResponse, previousCheckpoint?.assistantText, previousCheckpoint?.finalText);
|
||||||
const messageFinalText = finalResponseTextValue(event.finalResponse, event.assistantText, event.reply, previousFinalText);
|
const messageFinalText = finalResponseTextValue(event.finalResponse, event.assistantText, event.reply, previousFinalText);
|
||||||
const checkpointFinalResponse = messageFinalText ? { text: messageFinalText, status, traceId, valuesPrinted: false } : null;
|
const checkpointFinalResponse = messageFinalText ? { text: messageFinalText, status, traceId, valuesPrinted: false } : null;
|
||||||
|
const userMessageFact = sessionId && !suppressedAfterSeal && previousCheckpoint?.userMessage ? normalizeMessageFact(previousCheckpoint.userMessage, 0, { traceId, sessionId, turnId, terminal: false, terminalStatus: "sent", finalText: null, timestamp: projectedAt, timing }) : null;
|
||||||
const messageFact = sessionId && !suppressedAfterSeal ? normalizeMessageFact({
|
const messageFact = sessionId && !suppressedAfterSeal ? normalizeMessageFact({
|
||||||
messageId: eventProjectionAssistantMessageId(traceId, event),
|
messageId: eventProjectionAssistantMessageId(traceId, event),
|
||||||
role: "agent",
|
role: "agent",
|
||||||
@@ -164,7 +165,7 @@ export async function writeWorkbenchProjectionEvent({ runtimeStore = null, event
|
|||||||
createdAt: occurredAt,
|
createdAt: occurredAt,
|
||||||
updatedAt: projectedAt,
|
updatedAt: projectedAt,
|
||||||
source: "workbench-event-projection"
|
source: "workbench-event-projection"
|
||||||
}, 0, { traceId, sessionId, turnId, terminal, terminalStatus: status, finalText: messageFinalText, timestamp: projectedAt, timing }) : null;
|
}, userMessageFact ? 1 : 0, { traceId, sessionId, turnId, terminal, terminalStatus: status, finalText: messageFinalText, timestamp: projectedAt, timing }) : null;
|
||||||
const facts = {
|
const facts = {
|
||||||
sessions: sessionId && !suppressedAfterSeal ? [{
|
sessions: sessionId && !suppressedAfterSeal ? [{
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -204,7 +205,7 @@ export async function writeWorkbenchProjectionEvent({ runtimeStore = null, event
|
|||||||
occurredAt,
|
occurredAt,
|
||||||
updatedAt: projectedAt
|
updatedAt: projectedAt
|
||||||
}],
|
}],
|
||||||
messages: messageFact ? [messageFact] : [],
|
messages: [userMessageFact, messageFact].filter(Boolean),
|
||||||
checkpoints: !suppressedAfterSeal ? [{
|
checkpoints: !suppressedAfterSeal ? [{
|
||||||
traceId,
|
traceId,
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -363,6 +364,7 @@ function buildWorkbenchProjectionFacts({ traceId = null, ownerUserId = null, own
|
|||||||
diagnostic,
|
diagnostic,
|
||||||
finalResponse: checkpointFinalResponse,
|
finalResponse: checkpointFinalResponse,
|
||||||
assistantText: messageFinalText,
|
assistantText: messageFinalText,
|
||||||
|
userMessage: previousCheckpoint?.userMessage ?? null,
|
||||||
timing,
|
timing,
|
||||||
startedAt: timing.startedAt,
|
startedAt: timing.startedAt,
|
||||||
lastEventAt: timing.lastEventAt,
|
lastEventAt: timing.lastEventAt,
|
||||||
@@ -386,6 +388,7 @@ function buildWorkbenchProjectionFacts({ traceId = null, ownerUserId = null, own
|
|||||||
diagnostic,
|
diagnostic,
|
||||||
finalResponse: projectedFinalResponse,
|
finalResponse: projectedFinalResponse,
|
||||||
assistantText: finalText,
|
assistantText: finalText,
|
||||||
|
userMessage: messages.find((message) => message.role === "user") ?? null,
|
||||||
timing,
|
timing,
|
||||||
startedAt: timing.startedAt,
|
startedAt: timing.startedAt,
|
||||||
lastEventAt: timing.lastEventAt,
|
lastEventAt: timing.lastEventAt,
|
||||||
@@ -401,24 +404,11 @@ function workbenchProjectionInputMessages({ session = {}, payload = {}, params =
|
|||||||
if (Array.isArray(payload?.messages) && payload.messages.length > 0) return payload.messages;
|
if (Array.isArray(payload?.messages) && payload.messages.length > 0) return payload.messages;
|
||||||
if (!traceId) return [];
|
if (!traceId) return [];
|
||||||
const turnId = textValue(payload?.turnId) || traceId;
|
const turnId = textValue(payload?.turnId) || traceId;
|
||||||
const userText = textValue(params?.message ?? params?.prompt ?? payload?.prompt ?? payload?.message);
|
|
||||||
const userMessageId = textValue(payload?.userMessageId ?? payload?.userMessage?.messageId) || stableFactId("msg", { traceId, role: "user" });
|
|
||||||
const assistantMessageId = textValue(payload?.assistantMessageId ?? payload?.messageId ?? payload?.assistantMessage?.messageId) || stableFactId("msg", { traceId, role: "agent" });
|
const assistantMessageId = textValue(payload?.assistantMessageId ?? payload?.messageId ?? payload?.assistantMessage?.messageId) || stableFactId("msg", { traceId, role: "agent" });
|
||||||
const createdAt = timestampValue(timestamp ?? payload?.createdAt ?? payload?.updatedAt);
|
const createdAt = timestampValue(timestamp ?? payload?.createdAt ?? payload?.updatedAt);
|
||||||
const messages = [];
|
const messages = [];
|
||||||
if (userText) {
|
const userMessage = workbenchProjectionUserMessage({ payload, params, traceId, turnId, timestamp: createdAt });
|
||||||
messages.push({
|
if (userMessage) messages.push(userMessage);
|
||||||
messageId: userMessageId,
|
|
||||||
role: "user",
|
|
||||||
status: "sent",
|
|
||||||
text: userText,
|
|
||||||
traceId,
|
|
||||||
turnId,
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt,
|
|
||||||
valuesPrinted: false
|
|
||||||
});
|
|
||||||
}
|
|
||||||
messages.push({
|
messages.push({
|
||||||
messageId: assistantMessageId,
|
messageId: assistantMessageId,
|
||||||
role: "agent",
|
role: "agent",
|
||||||
@@ -438,6 +428,23 @@ function workbenchProjectionInputMessages({ session = {}, payload = {}, params =
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function workbenchProjectionUserMessage({ payload = {}, params = {}, traceId = null, turnId = null, timestamp = null } = {}) {
|
||||||
|
const userText = textValue(params?.message ?? params?.prompt ?? payload?.prompt ?? payload?.message);
|
||||||
|
if (!traceId || !userText) return null;
|
||||||
|
const createdAt = timestampValue(timestamp ?? payload?.createdAt ?? payload?.updatedAt);
|
||||||
|
return {
|
||||||
|
messageId: textValue(payload?.userMessageId ?? payload?.userMessage?.messageId) || stableFactId("msg", { traceId, role: "user" }),
|
||||||
|
role: "user",
|
||||||
|
status: "sent",
|
||||||
|
text: userText,
|
||||||
|
traceId,
|
||||||
|
turnId: textValue(turnId) || traceId,
|
||||||
|
createdAt,
|
||||||
|
updatedAt: createdAt,
|
||||||
|
valuesPrinted: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeMessages(messages, context) {
|
function normalizeMessages(messages, context) {
|
||||||
const normalized = Array.isArray(messages) ? messages.map((message, index) => normalizeMessageFact(message, index, context)).filter(Boolean) : [];
|
const normalized = Array.isArray(messages) ? messages.map((message, index) => normalizeMessageFact(message, index, context)).filter(Boolean) : [];
|
||||||
if (context?.terminal && context?.finalText && context?.traceId && !normalized.some((message) => message.role !== "user" && message.traceId === context.traceId)) {
|
if (context?.terminal && context?.finalText && context?.traceId && !normalized.some((message) => message.role !== "user" && message.traceId === context.traceId)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user