mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-19 21:18:16 +02:00
feat: implement admin AI chat API with SQL execution and database schema tools
This commit is contained in:
1 parent
6891aff668
commit
9f05eb9bc9
1 file changed
+15
-4
@@ -168,7 +168,7 @@ async function callOpenRouter(
|
|||||||
model,
|
model,
|
||||||
messages,
|
messages,
|
||||||
temperature: 0.3,
|
temperature: 0.3,
|
||||||
max_tokens: 4096,
|
max_tokens: 8192,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (useTools) {
|
if (useTools) {
|
||||||
@@ -225,9 +225,10 @@ export async function POST(req: NextRequest) {
|
|||||||
let currentMessages = [...fullMessages];
|
let currentMessages = [...fullMessages];
|
||||||
let totalPromptTokens = 0;
|
let totalPromptTokens = 0;
|
||||||
let totalCompletionTokens = 0;
|
let totalCompletionTokens = 0;
|
||||||
|
let directAnswer: string | null = null;
|
||||||
|
|
||||||
// Tool-calling loop (non-streaming for tool phase)
|
// Tool-calling loop (non-streaming for tool phase)
|
||||||
for (let round = 0; round < 5; round++) {
|
for (let round = 0; round < 8; round++) {
|
||||||
const res = await callOpenRouter(currentMessages, model, apiKey, true, false);
|
const res = await callOpenRouter(currentMessages, model, apiKey, true, false);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -253,7 +254,8 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
const toolCalls = assistantMessage.tool_calls;
|
const toolCalls = assistantMessage.tool_calls;
|
||||||
if (!toolCalls || toolCalls.length === 0) {
|
if (!toolCalls || toolCalls.length === 0) {
|
||||||
// No more tool calls — stream final response
|
// Model gave a direct answer — capture it and skip the extra final call
|
||||||
|
directAnswer = assistantMessage.content ?? "";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,7 +291,16 @@ export async function POST(req: NextRequest) {
|
|||||||
currentMessages.push(...toolResults);
|
currentMessages.push(...toolResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final streaming response
|
// If model gave a direct answer in the loop, stream it character by character
|
||||||
|
if (directAnswer !== null) {
|
||||||
|
send({ type: "text", content: directAnswer });
|
||||||
|
send({ type: "usage", prompt_tokens: totalPromptTokens, completion_tokens: totalCompletionTokens });
|
||||||
|
send({ type: "done" });
|
||||||
|
controller.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise make a final streaming call after tool use
|
||||||
const finalRes = await callOpenRouter(currentMessages, model, apiKey, false, true);
|
const finalRes = await callOpenRouter(currentMessages, model, apiKey, false, true);
|
||||||
|
|
||||||
if (!finalRes.ok) {
|
if (!finalRes.ok) {
|
||||||
|
|||||||
Reference in new issue
Block a user