fix(ai): improve assistant execution and MCP settings
This commit is contained in:
1 parent
1c18a80092
commit
5a666b1f48
20 files changed
+213
-47
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createAiMessageRenderer } from "@/lib/ai/aiMessageRender";
|
||||
|
||||
describe("createAiMessageRenderer", () => {
|
||||
it("caches completed short messages", () => {
|
||||
const markdown = vi.fn((text: string) => `<p>${text}</p>`);
|
||||
const renderer = createAiMessageRenderer({ markdown, maxCacheableChars: 100 });
|
||||
|
||||
renderer.render("hello");
|
||||
renderer.render("hello");
|
||||
|
||||
expect(markdown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not retain long streaming message versions", () => {
|
||||
const markdown = vi.fn((text: string) => `<p>${text}</p>`);
|
||||
const renderer = createAiMessageRenderer({ markdown, maxCacheableChars: 5 });
|
||||
|
||||
renderer.render("long message");
|
||||
renderer.render("long message");
|
||||
|
||||
expect(markdown).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
@@ -93,6 +93,7 @@ export interface AiRequestInput {
|
||||
mode?: AiAssistantMode;
|
||||
instruction: string;
|
||||
context: AiContext;
|
||||
allowWriteSql?: boolean;
|
||||
}
|
||||
|
||||
function buildAgentRequest(input: AiRequestInput, history?: api.AiMessage[]): { messages: api.AiMessage[]; systemPrompt: string; taskContract: api.AiTaskContract; maxTokens: number } {
|
||||
@@ -163,6 +164,7 @@ export async function runAgentStream(input: AiRequestInput, history: api.AiMessa
|
||||
input.context.databaseType,
|
||||
onEvent,
|
||||
input.mode || "ask",
|
||||
input.allowWriteSql || false,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,13 @@ interface MessageSegment {
|
||||
|
||||
export interface AiMessageRendererOptions {
|
||||
maxEntries?: number;
|
||||
maxCacheableChars?: number;
|
||||
markdown: (text: string) => string;
|
||||
highlightCode?: (content: string, lang: string) => string;
|
||||
}
|
||||
|
||||
const DEFAULT_MAX_ENTRIES = 100;
|
||||
const DEFAULT_MAX_CACHEABLE_CHARS = 20_000;
|
||||
const SQL_LANGUAGES = new Map([
|
||||
["sql", "SQL"],
|
||||
["mysql", "MYSQL"],
|
||||
@@ -48,10 +50,12 @@ const SQL_LANGUAGE_LABELS = new Set(SQL_LANGUAGES.values());
|
||||
|
||||
export function createAiMessageRenderer(options: AiMessageRendererOptions) {
|
||||
const maxEntries = Math.max(1, Math.floor(options.maxEntries ?? DEFAULT_MAX_ENTRIES));
|
||||
const maxCacheableChars = Math.max(0, Math.floor(options.maxCacheableChars ?? DEFAULT_MAX_CACHEABLE_CHARS));
|
||||
const cache = new Map<string, AiMessageRenderSegment[]>();
|
||||
|
||||
function render(content: string): AiMessageRenderSegment[] {
|
||||
const cached = cache.get(content);
|
||||
const cacheable = content.length <= maxCacheableChars;
|
||||
const cached = cacheable ? cache.get(content) : undefined;
|
||||
if (cached) {
|
||||
cache.delete(content);
|
||||
cache.set(content, cached);
|
||||
@@ -72,11 +76,13 @@ export function createAiMessageRenderer(options: AiMessageRendererOptions) {
|
||||
};
|
||||
});
|
||||
|
||||
cache.set(content, rendered);
|
||||
while (cache.size > maxEntries) {
|
||||
const oldestKey = cache.keys().next().value;
|
||||
if (oldestKey === undefined) break;
|
||||
cache.delete(oldestKey);
|
||||
if (cacheable) {
|
||||
cache.set(content, rendered);
|
||||
while (cache.size > maxEntries) {
|
||||
const oldestKey = cache.keys().next().value;
|
||||
if (oldestKey === undefined) break;
|
||||
cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
return rendered;
|
||||
}
|
||||
|
||||
@@ -998,11 +998,11 @@ function isAgentEvent(v: unknown): v is import("@/lib/backend/tauri").AgentEvent
|
||||
return typeof v === "object" && v !== null && "type" in v && typeof (v as Record<string, unknown>).type === "string";
|
||||
}
|
||||
|
||||
export async function aiAgentStream(sessionId: string, request: AiCompletionRequest, connectionId: string, database: string, dbType: string, onEvent: (event: import("@/lib/backend/tauri").AgentEvent) => void, mode?: string, signal?: AbortSignal): Promise<string> {
|
||||
export async function aiAgentStream(sessionId: string, request: AiCompletionRequest, connectionId: string, database: string, dbType: string, onEvent: (event: import("@/lib/backend/tauri").AgentEvent) => void, mode?: string, allowWriteSql = false, signal?: AbortSignal): Promise<string> {
|
||||
const res = await fetch(apiUrl("/api/ai/agent-stream"), {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sessionId, request, connectionId, database, dbType, mode: mode || "ask" }),
|
||||
body: JSON.stringify({ sessionId, request, connectionId, database, dbType, mode: mode || "ask", allowWriteSql }),
|
||||
signal,
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
|
||||
@@ -338,7 +338,7 @@ export type AgentEvent =
|
||||
| { type: "context_compacted"; summary: string; summary_tokens: number; compacted_messages: number; estimated_before: number; estimated_after: number }
|
||||
| { type: "error"; message: string };
|
||||
|
||||
export async function aiAgentStream(sessionId: string, request: AiCompletionRequest, connectionId: string, database: string, dbType: string, onEvent: (event: AgentEvent) => void, mode?: string, _signal?: AbortSignal): Promise<string> {
|
||||
export async function aiAgentStream(sessionId: string, request: AiCompletionRequest, connectionId: string, database: string, dbType: string, onEvent: (event: AgentEvent) => void, mode?: string, allowWriteSql = false, _signal?: AbortSignal): Promise<string> {
|
||||
const unlisten: UnlistenFn = await listen<AgentEvent>("ai-agent-event", (event) => {
|
||||
onEvent(event.payload);
|
||||
if (event.payload.type === "agent_end" || event.payload.type === "error") {
|
||||
@@ -346,7 +346,7 @@ export async function aiAgentStream(sessionId: string, request: AiCompletionRequ
|
||||
}
|
||||
});
|
||||
try {
|
||||
return await invoke("ai_agent_stream", { sessionId, request, connectionId, database, dbType, mode });
|
||||
return await invoke("ai_agent_stream", { sessionId, request, connectionId, database, dbType, mode, allowWriteSql });
|
||||
} catch (e) {
|
||||
unlisten();
|
||||
throw e;
|
||||
|
||||
Reference in new issue
Block a user