refactor(pilot-chat): stream native ai sdk reasoning parts
This commit is contained in:
@@ -15,15 +15,7 @@ import type { ChangeSet } from "../utils/changeSet";
|
||||
import { startPilotRun, addPilotTrace, finishPilotRun } from "../utils/pilotRunStore";
|
||||
import { broadcastToConversation } from "../routes/ws/crm-updates";
|
||||
|
||||
type PilotDataTypes = {
|
||||
"agent-log": {
|
||||
requestId: string;
|
||||
at: string;
|
||||
text: string;
|
||||
};
|
||||
};
|
||||
|
||||
type PilotUiMessage = UIMessage<unknown, PilotDataTypes>;
|
||||
type PilotUiMessage = UIMessage;
|
||||
|
||||
type PilotChatRequestBody = {
|
||||
messages?: unknown;
|
||||
@@ -41,7 +33,7 @@ function extractMessageText(message: PilotUiMessage): string {
|
||||
function getLastUserText(messages: PilotUiMessage[]): string {
|
||||
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
||||
const message = messages[i];
|
||||
if (message.role !== "user") continue;
|
||||
if (!message || message.role !== "user") continue;
|
||||
const text = extractMessageText(message);
|
||||
if (text) return text;
|
||||
}
|
||||
@@ -156,11 +148,10 @@ function renderChangeSetSummary(changeSet: ChangeSet): string {
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function writePilotLog(writer: UIMessageStreamWriter<PilotUiMessage>, payload: PilotDataTypes["agent-log"]) {
|
||||
writer.write({
|
||||
type: "data-agent-log",
|
||||
data: payload,
|
||||
});
|
||||
function writeAssistantReasoning(writer: UIMessageStreamWriter<PilotUiMessage>, reasoningId: string, text: string) {
|
||||
writer.write({ type: "reasoning-start", id: reasoningId });
|
||||
writer.write({ type: "reasoning-delta", id: reasoningId, delta: text });
|
||||
writer.write({ type: "reasoning-end", id: reasoningId });
|
||||
}
|
||||
|
||||
function writeAssistantText(writer: UIMessageStreamWriter<PilotUiMessage>, textId: string, text: string) {
|
||||
@@ -229,12 +220,9 @@ export default defineEventHandler(async (event) => {
|
||||
onTrace: async (trace: AgentTraceEvent) => {
|
||||
const traceText = humanizeTraceText(trace);
|
||||
const traceAt = new Date().toISOString();
|
||||
const reasoningId = `reasoning-${Date.now()}-${Math.floor(Math.random() * 1_000_000)}`;
|
||||
|
||||
writePilotLog(writer, {
|
||||
requestId,
|
||||
at: traceAt,
|
||||
text: traceText,
|
||||
});
|
||||
writeAssistantReasoning(writer, reasoningId, traceText);
|
||||
|
||||
addPilotTrace(auth.conversationId, traceText);
|
||||
broadcastToConversation(auth.conversationId, {
|
||||
@@ -245,6 +233,16 @@ export default defineEventHandler(async (event) => {
|
||||
},
|
||||
});
|
||||
|
||||
for (const thought of reply.thinking ?? []) {
|
||||
const text = String(thought ?? "").trim();
|
||||
if (!text) continue;
|
||||
writeAssistantReasoning(
|
||||
writer,
|
||||
`reasoning-${Date.now()}-${Math.floor(Math.random() * 1_000_000)}`,
|
||||
text,
|
||||
);
|
||||
}
|
||||
|
||||
const snapshotAfter = await captureSnapshot(prisma, auth.teamId);
|
||||
const changeSet = buildChangeSet(snapshotBefore, snapshotAfter);
|
||||
|
||||
@@ -297,11 +295,11 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
finalizePilotExecution(auth.conversationId, "error");
|
||||
|
||||
writePilotLog(writer, {
|
||||
requestId,
|
||||
at: new Date().toISOString(),
|
||||
text: "Ошибка выполнения агентского цикла.",
|
||||
});
|
||||
writeAssistantReasoning(
|
||||
writer,
|
||||
`reasoning-${Date.now()}-${Math.floor(Math.random() * 1_000_000)}`,
|
||||
"Ошибка выполнения агентского цикла.",
|
||||
);
|
||||
|
||||
writeAssistantText(writer, textId, errorText);
|
||||
writer.write({ type: "finish", finishReason: "error" });
|
||||
|
||||
Reference in New Issue
Block a user