2026-02-13 22:43:55 -08:00
|
|
|
import { performance } from "node:perf_hooks";
|
|
|
|
|
import { prisma } from "../db.js";
|
2026-06-13 12:02:22 -07:00
|
|
|
import { buildToolLogMessageData } from "./chat-tools.js";
|
|
|
|
|
import { getProviderChatAdapter } from "./provider-adapters.js";
|
2026-05-04 21:52:39 -07:00
|
|
|
import { toPrismaProvider } from "./provider-ids.js";
|
2026-02-13 22:43:55 -08:00
|
|
|
import type { MultiplexRequest, MultiplexResponse, Provider } from "./types.js";
|
|
|
|
|
|
|
|
|
|
function asProviderEnum(p: Provider) {
|
2026-05-04 21:52:39 -07:00
|
|
|
return toPrismaProvider(p);
|
2026-02-13 22:43:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResponse> {
|
|
|
|
|
const t0 = performance.now();
|
2026-02-14 22:06:30 -08:00
|
|
|
const chatId = req.chatId ?? (await prisma.chat.create({ data: {}, select: { id: true } })).id;
|
2026-02-13 22:43:55 -08:00
|
|
|
|
|
|
|
|
// Persist call record early so we can attach errors.
|
|
|
|
|
const call = await prisma.llmCall.create({
|
|
|
|
|
data: {
|
2026-02-14 22:06:30 -08:00
|
|
|
chatId,
|
2026-02-13 22:43:55 -08:00
|
|
|
provider: asProviderEnum(req.provider) as any,
|
|
|
|
|
model: req.model,
|
|
|
|
|
request: req as any,
|
|
|
|
|
},
|
|
|
|
|
select: { id: true, chatId: true },
|
|
|
|
|
});
|
2026-02-14 22:06:30 -08:00
|
|
|
|
|
|
|
|
await prisma.$transaction([
|
|
|
|
|
prisma.chat.update({
|
|
|
|
|
where: { id: chatId },
|
|
|
|
|
data: {
|
|
|
|
|
lastUsedProvider: asProviderEnum(req.provider) as any,
|
|
|
|
|
lastUsedModel: req.model,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
prisma.chat.updateMany({
|
|
|
|
|
where: { id: chatId, initiatedProvider: null },
|
|
|
|
|
data: {
|
|
|
|
|
initiatedProvider: asProviderEnum(req.provider) as any,
|
|
|
|
|
initiatedModel: req.model,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
]);
|
2026-02-13 22:43:55 -08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let outText = "";
|
|
|
|
|
let usage: MultiplexResponse["usage"] | undefined;
|
|
|
|
|
let raw: unknown;
|
2026-03-02 16:13:34 -08:00
|
|
|
let toolMessages: ReturnType<typeof buildToolLogMessageData>[] = [];
|
2026-06-13 12:02:22 -07:00
|
|
|
const adapter = getProviderChatAdapter(req.provider);
|
|
|
|
|
const r = await adapter.complete({
|
|
|
|
|
model: req.model,
|
|
|
|
|
messages: req.messages,
|
|
|
|
|
enabledTools: req.enabledTools,
|
|
|
|
|
userLocation: req.userLocation,
|
|
|
|
|
temperature: req.temperature,
|
|
|
|
|
maxTokens: req.maxTokens,
|
|
|
|
|
logContext: {
|
|
|
|
|
provider: req.provider,
|
2026-02-13 22:43:55 -08:00
|
|
|
model: req.model,
|
2026-06-13 12:02:22 -07:00
|
|
|
chatId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
raw = r.raw;
|
|
|
|
|
outText = r.text;
|
|
|
|
|
usage = r.usage;
|
|
|
|
|
toolMessages = r.toolEvents.map((event) => buildToolLogMessageData(call.chatId, event));
|
2026-02-13 22:43:55 -08:00
|
|
|
|
|
|
|
|
const latencyMs = Math.round(performance.now() - t0);
|
|
|
|
|
|
2026-03-02 16:13:34 -08:00
|
|
|
// Store tool activity (if any), assistant message, and call record.
|
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
|
|
|
if (toolMessages.length) {
|
|
|
|
|
await tx.message.createMany({
|
|
|
|
|
data: toolMessages.map((message) => ({
|
|
|
|
|
chatId: message.chatId,
|
|
|
|
|
role: message.role as any,
|
|
|
|
|
content: message.content,
|
|
|
|
|
name: message.name,
|
|
|
|
|
metadata: message.metadata as any,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
await tx.message.create({
|
2026-02-13 22:43:55 -08:00
|
|
|
data: {
|
|
|
|
|
chatId: call.chatId,
|
|
|
|
|
role: "assistant" as any,
|
|
|
|
|
content: outText,
|
|
|
|
|
},
|
2026-03-02 16:13:34 -08:00
|
|
|
});
|
|
|
|
|
await tx.llmCall.update({
|
2026-02-13 22:43:55 -08:00
|
|
|
where: { id: call.id },
|
|
|
|
|
data: {
|
|
|
|
|
response: raw as any,
|
|
|
|
|
latencyMs,
|
|
|
|
|
inputTokens: usage?.inputTokens,
|
|
|
|
|
outputTokens: usage?.outputTokens,
|
|
|
|
|
totalTokens: usage?.totalTokens,
|
|
|
|
|
},
|
2026-03-02 16:13:34 -08:00
|
|
|
});
|
|
|
|
|
});
|
2026-02-13 22:43:55 -08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
provider: req.provider,
|
|
|
|
|
model: req.model,
|
|
|
|
|
message: { role: "assistant", content: outText },
|
|
|
|
|
usage,
|
|
|
|
|
raw,
|
|
|
|
|
};
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
const latencyMs = Math.round(performance.now() - t0);
|
|
|
|
|
await prisma.llmCall.update({
|
|
|
|
|
where: { id: call.id },
|
|
|
|
|
data: {
|
|
|
|
|
error: e?.message ?? String(e),
|
|
|
|
|
latencyMs,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|