Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function TableContent({ tableId }: { tableId: TableId }) {
const defaultOrderDir = isAiActive ? undefined : tableConfig.defaultOrderDir;

const handleResetChat = useCallback(() => {
chat.setMessages([]);
chat.clearMessages();
}, [chat]);

const aiSearchBar = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,96 @@
import type { UIMessage } from "@ai-sdk/react";
import type { ThreadMessage } from "@assistant-ui/react";
import { describe, expect, it } from "vitest";
import { extractLatestQuery } from "./use-ai-query-chat";

const fixture = (messages: Array<{
id: string,
role: "assistant",
content: Array<
| { type: "text", text: string }
| { type: "tool-call", toolCallId: string, toolName: string, args: { query?: string }, argsText?: string, result: unknown }
>,
}>) => messages as unknown as readonly ThreadMessage[];

describe("extractLatestQuery", () => {
it("ignores failed queryAnalytics tool calls and keeps the last successful query", () => {
const messages = [
const messages = fixture([
{
id: "assistant-1",
role: "assistant",
parts: [
content: [
{
type: "tool-queryAnalytics",
type: "tool-call",
toolCallId: "call-1",
state: "output-available",
input: { query: "SELECT 1" },
output: { success: true },
toolName: "queryAnalytics",
args: { query: "SELECT 1" },
argsText: '{"query":"SELECT 1"}',
result: { success: true },
},
],
},
{
id: "assistant-2",
role: "assistant",
parts: [
content: [
{
type: "tool-queryAnalytics",
type: "tool-call",
toolCallId: "call-2",
state: "output-error",
input: { query: "SELECT broken" },
errorText: "boom",
toolName: "queryAnalytics",
args: { query: "SELECT broken" },
argsText: '{"query":"SELECT broken"}',
result: { success: false, error: "boom" },
},
],
},
] satisfies UIMessage[];
]);

const result = extractLatestQuery(messages);

expect(result).toEqual({
query: "SELECT 1",
state: "output-available",
toolCallIndex: 2,
});
});

it("returns null while a tool call is still in flight (result == null)", () => {
const messages = fixture([
{
id: "assistant-1",
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "queryAnalytics",
args: { query: "SELECT 1" },
argsText: '{"query":"SELECT 1"}',
result: null,
},
],
},
]);

expect(extractLatestQuery(messages)).toBeNull();
});

it("ignores tool calls from unrelated tools", () => {
const messages = fixture([
{
id: "assistant-1",
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "someOtherTool",
args: { query: "SELECT 1" },
argsText: '{"query":"SELECT 1"}',
result: { success: true },
},
],
},
]);

expect(extractLatestQuery(messages)).toBeNull();
});
});
Loading
Loading