Clean up AI tools

This commit is contained in:
Konstantin Wohlwend 2026-06-30 10:08:06 -07:00
parent 61698f6ecf
commit d31d29c112
4 changed files with 38 additions and 7 deletions

View File

@ -31,7 +31,7 @@ export const POST = createSmartRouteHandler({
response: yupMixed<SmartResponse>().defined(),
async handler({ params, body }, fullReq) {
const { mode } = params;
const isAuthenticated = fullReq.auth != null;
const isAuthenticated = fullReq.auth?.user != null;
const { quality, speed, systemPrompt: systemPromptId, tools: toolNames, messages, projectId } = body;
if (projectId != null) {

View File

@ -41,7 +41,7 @@ export async function getTools(
}
case "sql-query": {
const sqlTool = createSqlQueryTool(context.auth, context.targetProjectId);
const sqlTool = createSqlQueryTool(context.targetProjectId);
if (sqlTool != null) {
tools["queryAnalytics"] = sqlTool;
}

View File

@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { getTools } from ".";
import { createSqlQueryTool } from "./sql-query";
describe("createSqlQueryTool", () => {
it("does not create a SQL tool without an explicit project target", () => {
expect(createSqlQueryTool(null)).toBeNull();
expect(createSqlQueryTool(undefined)).toBeNull();
});
it("creates a SQL tool for an explicit project target", () => {
expect(createSqlQueryTool("00000000-0000-0000-0000-000000000000")).not.toBeNull();
});
});
describe("getTools", () => {
it("omits queryAnalytics when sql-query has no explicit project target", async () => {
await expect(getTools(["sql-query"], {
auth: null,
targetProjectId: null,
})).resolves.toEqual({});
});
it("includes queryAnalytics when sql-query has an explicit project target", async () => {
const tools = await getTools(["sql-query"], {
auth: null,
targetProjectId: "00000000-0000-0000-0000-000000000000",
});
expect(tools).toHaveProperty("queryAnalytics");
});
});

View File

@ -1,19 +1,18 @@
import { getClickhouseExternalClient } from "@/lib/clickhouse";
import { getSafeClickhouseErrorMessage } from "@/lib/clickhouse-errors";
import { SmartRequestAuth } from "@/route-handlers/smart-request";
import { ClickHouseError } from "@clickhouse/client";
import { tool } from "ai";
import { z } from "zod";
export const SQL_QUERY_RESULT_MAX_CHARS = 50_000;
export function createSqlQueryTool(auth: SmartRequestAuth | null, targetProjectId?: string | null) {
if (auth == null) {
export function createSqlQueryTool(targetProjectId?: string | null) {
if (targetProjectId == null) {
return null;
}
const projectId = targetProjectId ?? auth.tenancy.project.id;
const branchId = targetProjectId ? "main" : auth.tenancy.branchId;
const projectId = targetProjectId;
const branchId = "main";
// Max rows returned to the model (backstop if LIMIT is missing).
const MAX_ROWS_FOR_AI = 50;