[feature] adds web_search and fetch_url tool calls
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { performance } from "node:perf_hooks";
|
||||
import { prisma } from "../db.js";
|
||||
import { anthropicClient, openaiClient, xaiClient } from "./providers.js";
|
||||
import { buildToolLogMessageData, runToolAwareOpenAIChat } from "./chat-tools.js";
|
||||
import type { MultiplexRequest, MultiplexResponse, Provider } from "./types.js";
|
||||
|
||||
function asProviderEnum(p: Provider) {
|
||||
@@ -44,25 +45,26 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
let outText = "";
|
||||
let usage: MultiplexResponse["usage"] | undefined;
|
||||
let raw: unknown;
|
||||
let toolMessages: ReturnType<typeof buildToolLogMessageData>[] = [];
|
||||
|
||||
if (req.provider === "openai" || req.provider === "xai") {
|
||||
const client = req.provider === "openai" ? openaiClient() : xaiClient();
|
||||
const r = await client.chat.completions.create({
|
||||
const r = await runToolAwareOpenAIChat({
|
||||
client,
|
||||
model: req.model,
|
||||
// OpenAI SDK has very specific message union types; our normalized schema is compatible.
|
||||
messages: req.messages.map((m) => ({ role: m.role, content: m.content, name: m.name })) as any,
|
||||
messages: req.messages,
|
||||
temperature: req.temperature,
|
||||
max_tokens: req.maxTokens,
|
||||
maxTokens: req.maxTokens,
|
||||
logContext: {
|
||||
provider: req.provider,
|
||||
model: req.model,
|
||||
chatId,
|
||||
},
|
||||
});
|
||||
raw = r;
|
||||
outText = r.choices?.[0]?.message?.content ?? "";
|
||||
usage = r.usage
|
||||
? {
|
||||
inputTokens: r.usage.prompt_tokens,
|
||||
outputTokens: r.usage.completion_tokens,
|
||||
totalTokens: r.usage.total_tokens,
|
||||
}
|
||||
: undefined;
|
||||
raw = r.raw;
|
||||
outText = r.text;
|
||||
usage = r.usage;
|
||||
toolMessages = r.toolEvents.map((event) => buildToolLogMessageData(call.chatId, event));
|
||||
} else if (req.provider === "anthropic") {
|
||||
const client = anthropicClient();
|
||||
|
||||
@@ -100,16 +102,27 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
|
||||
const latencyMs = Math.round(performance.now() - t0);
|
||||
|
||||
// Store assistant message + call record
|
||||
await prisma.$transaction([
|
||||
prisma.message.create({
|
||||
// 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({
|
||||
data: {
|
||||
chatId: call.chatId,
|
||||
role: "assistant" as any,
|
||||
content: outText,
|
||||
},
|
||||
}),
|
||||
prisma.llmCall.update({
|
||||
});
|
||||
await tx.llmCall.update({
|
||||
where: { id: call.id },
|
||||
data: {
|
||||
response: raw as any,
|
||||
@@ -118,8 +131,8 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
outputTokens: usage?.outputTokens,
|
||||
totalTokens: usage?.totalTokens,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
provider: req.provider,
|
||||
|
||||
Reference in New Issue
Block a user