Preserve and display the generating model for each assistant reply
This commit is contained in:
@@ -85,6 +85,7 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
chatId: call.chatId,
|
||||
role: "assistant" as any,
|
||||
content: outText,
|
||||
metadata: { generation: { provider: req.provider, model: req.model } },
|
||||
},
|
||||
});
|
||||
await tx.llmCall.update({
|
||||
|
||||
@@ -123,7 +123,10 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator
|
||||
chatId,
|
||||
role: "assistant" as any,
|
||||
content: text,
|
||||
metadata: req.clientRequestId ? ({ clientRequestId: req.clientRequestId } as any) : undefined,
|
||||
metadata: {
|
||||
...(req.clientRequestId ? { clientRequestId: req.clientRequestId } : {}),
|
||||
generation: { provider: req.provider, model: req.model },
|
||||
},
|
||||
},
|
||||
});
|
||||
await tx.llmCall.update({
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { fromPrismaProvider } from "./llm/provider-ids.js";
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
type RecordedCall = {
|
||||
provider: unknown;
|
||||
model: string;
|
||||
request: unknown;
|
||||
error: string | null;
|
||||
latencyMs: number | null;
|
||||
};
|
||||
|
||||
/** Recover older streaming replies only through an unambiguous, completed request ID. */
|
||||
export function withMessageGenerations<T extends { role: string; metadata: unknown }>(messages: T[], calls: RecordedCall[]): T[] {
|
||||
const byRequest = new Map<string, { provider: string; model: string } | null>();
|
||||
for (const call of calls) {
|
||||
const requestId = record(call.request).clientRequestId;
|
||||
if (typeof requestId !== "string" || !requestId || call.error !== null || call.latencyMs === null) continue;
|
||||
const provider = fromPrismaProvider(call.provider);
|
||||
if (!provider || !call.model.trim()) continue;
|
||||
byRequest.set(requestId, byRequest.has(requestId) ? null : { provider, model: call.model });
|
||||
}
|
||||
return messages.map(message => {
|
||||
const metadata = record(message.metadata);
|
||||
if (message.role !== "assistant" || metadata.agentAside === true || metadata.generation !== undefined) return message;
|
||||
const generation = typeof metadata.clientRequestId === "string" ? byRequest.get(metadata.clientRequestId) : null;
|
||||
return generation ? { ...message, metadata: { ...metadata, generation } } : message;
|
||||
});
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { invokeAgentInline, type AgentInlineInvoker } from "./agent-inline.js";
|
||||
import { buildInlineAgentThreadContext, decodeChatHistoryCursor, decodeChatListCursor, encodeChatListCursor, loadCompleteChatHistory, readChatHistoryPage } from "./chat-history.js";
|
||||
import { containsSybilMention, filterAgentAsideMessages } from "./agent-mention.js";
|
||||
import { prisma } from "./db.js";
|
||||
import { withMessageGenerations } from "./message-generation.js";
|
||||
import { requireAdmin } from "./auth.js";
|
||||
import { env } from "./env.js";
|
||||
import { buildComparableAttachments } from "./llm/message-content.js";
|
||||
@@ -1173,11 +1174,12 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
enabledTools: true,
|
||||
userId: true,
|
||||
messages: { orderBy: [{ createdAt: "asc" }, { id: "asc" }] },
|
||||
calls: { select: { provider: true, model: true, request: true, error: true, latencyMs: true } },
|
||||
},
|
||||
});
|
||||
if (!source) return { status: "chat-not-found" as const };
|
||||
|
||||
let messages = source.messages;
|
||||
let messages = withMessageGenerations(source.messages, source.calls);
|
||||
let selectedMessage: (typeof source.messages)[number] | undefined;
|
||||
if (parsed.data.messageId) {
|
||||
const selectedIndex = messages.findIndex((message) => message.id === parsed.data.messageId);
|
||||
@@ -1680,7 +1682,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
},
|
||||
});
|
||||
if (!chat) return app.httpErrors.notFound("chat not found");
|
||||
return { chat: serializeChatLike(chat) };
|
||||
return { chat: serializeChatLike({ ...chat, messages: withMessageGenerations(chat.messages, chat.calls) }) };
|
||||
});
|
||||
|
||||
app.post("/v1/chats/:chatId/messages", async (req) => {
|
||||
|
||||
Reference in New Issue
Block a user