refactor(ai): consolidate AI around chat tool-calling; add OpenRouter
- rework chat backend (chat.rs, chat_tools.rs, ai.rs, models, state) around tool calls - add OpenRouter provider alongside Ollama/Fireworks in settings - drop inline AiBar, ResultsPanel explain/fix UI and ChartPreview in favour of the chat panel - add frontend chat tool-registry
This commit is contained in:
@@ -13,7 +13,7 @@ import { useCompletionSchema } from "@/hooks/use-completion-schema";
|
||||
import { useConnections } from "@/hooks/use-connections";
|
||||
import { useAppStore } from "@/stores/app-store";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Play, Loader2, Lock, BarChart3, Download, AlignLeft, Bookmark, Table2, Braces, Sparkles, BrainCircuit } from "lucide-react";
|
||||
import { Play, Loader2, Lock, BarChart3, Download, AlignLeft, Bookmark, Table2, Braces } from "lucide-react";
|
||||
import { format as formatSql } from "sql-formatter";
|
||||
import { SaveQueryDialog } from "@/components/saved-queries/SaveQueryDialog";
|
||||
import {
|
||||
@@ -25,8 +25,6 @@ import {
|
||||
import { exportCsv, exportJson } from "@/lib/tauri";
|
||||
import { save } from "@tauri-apps/plugin-dialog";
|
||||
import { toast } from "sonner";
|
||||
import { AiBar } from "@/components/ai/AiBar";
|
||||
import { useExplainSql, useFixSqlError } from "@/hooks/use-ai";
|
||||
import type { QueryResult, ExplainResult } from "@/types";
|
||||
|
||||
interface Props {
|
||||
@@ -53,12 +51,8 @@ export function WorkspacePanel({
|
||||
const [resultView, setResultView] = useState<"results" | "explain">("results");
|
||||
const [resultViewMode, setResultViewMode] = useState<"table" | "json">("table");
|
||||
const [saveDialogOpen, setSaveDialogOpen] = useState(false);
|
||||
const [aiBarOpen, setAiBarOpen] = useState(false);
|
||||
const [aiExplanation, setAiExplanation] = useState<string | null>(null);
|
||||
|
||||
const queryMutation = useQueryExecution();
|
||||
const explainMutation = useExplainSql();
|
||||
const fixMutation = useFixSqlError();
|
||||
const addHistoryMutation = useAddHistory();
|
||||
const { data: connections } = useConnections();
|
||||
const { data: completionSchema } = useCompletionSchema(connectionId);
|
||||
@@ -102,7 +96,6 @@ export function WorkspacePanel({
|
||||
if (!sqlValue.trim() || !connectionId) return;
|
||||
setError(null);
|
||||
setExplainData(null);
|
||||
setAiExplanation(null);
|
||||
setResultView("results");
|
||||
queryMutation.mutate(
|
||||
{ connectionId, sql: sqlValue },
|
||||
@@ -196,60 +189,6 @@ export function WorkspacePanel({
|
||||
[result]
|
||||
);
|
||||
|
||||
const isAiLoading = explainMutation.isPending || fixMutation.isPending;
|
||||
|
||||
const handleAiExplain = useCallback(() => {
|
||||
if (!sqlValue.trim() || !connectionId) return;
|
||||
setAiExplanation(null);
|
||||
setResultView("results");
|
||||
explainMutation.mutate(
|
||||
{ connectionId, sql: sqlValue },
|
||||
{
|
||||
onSuccess: (explanation) => {
|
||||
setAiExplanation(explanation);
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error("AI Explain failed", { description: String(err) });
|
||||
},
|
||||
}
|
||||
);
|
||||
}, [connectionId, sqlValue, explainMutation]);
|
||||
|
||||
const handleExplainError = useCallback(() => {
|
||||
if (!sqlValue.trim() || !connectionId || !error) return;
|
||||
setAiExplanation(null);
|
||||
explainMutation.mutate(
|
||||
{ connectionId, sql: `${sqlValue}\n\n-- Error: ${error}` },
|
||||
{
|
||||
onSuccess: (explanation) => {
|
||||
setAiExplanation(explanation);
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error("AI Explain failed", { description: String(err) });
|
||||
},
|
||||
}
|
||||
);
|
||||
}, [connectionId, sqlValue, error, explainMutation]);
|
||||
|
||||
const handleFixError = useCallback(() => {
|
||||
if (!sqlValue.trim() || !connectionId || !error) return;
|
||||
fixMutation.mutate(
|
||||
{ connectionId, sql: sqlValue, errorMessage: error },
|
||||
{
|
||||
onSuccess: (fixedSql) => {
|
||||
setSqlValue(fixedSql);
|
||||
onSqlChange?.(fixedSql);
|
||||
setError(null);
|
||||
setAiExplanation(null);
|
||||
toast.success("SQL replaced by AI suggestion");
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error("AI Fix failed", { description: String(err) });
|
||||
},
|
||||
}
|
||||
);
|
||||
}, [connectionId, sqlValue, error, fixMutation, onSqlChange]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<ResizablePanelGroup orientation="vertical">
|
||||
@@ -308,35 +247,6 @@ export function WorkspacePanel({
|
||||
Save
|
||||
</Button>
|
||||
|
||||
<div className="mx-1 h-3.5 w-px bg-border/40" />
|
||||
|
||||
{/* AI actions group — purple-branded */}
|
||||
<Button
|
||||
size="xs"
|
||||
variant={aiBarOpen ? "secondary" : "ghost"}
|
||||
className={`gap-1 text-[11px] ${aiBarOpen ? "text-tusk-purple" : ""}`}
|
||||
onClick={() => setAiBarOpen(!aiBarOpen)}
|
||||
title="AI SQL Generator"
|
||||
>
|
||||
<Sparkles className={`h-3 w-3 ${aiBarOpen ? "tusk-ai-icon" : ""}`} />
|
||||
AI
|
||||
</Button>
|
||||
<Button
|
||||
size="xs"
|
||||
variant="ghost"
|
||||
className="gap-1 text-[11px]"
|
||||
onClick={handleAiExplain}
|
||||
disabled={isAiLoading || !sqlValue.trim()}
|
||||
title="Explain query with AI"
|
||||
>
|
||||
{isAiLoading ? (
|
||||
<Loader2 className="h-3 w-3 animate-spin" />
|
||||
) : (
|
||||
<BrainCircuit className="h-3 w-3" />
|
||||
)}
|
||||
AI Explain
|
||||
</Button>
|
||||
|
||||
{result && result.columns.length > 0 && (
|
||||
<>
|
||||
<div className="mx-1 h-3.5 w-px bg-border/40" />
|
||||
@@ -369,23 +279,12 @@ export function WorkspacePanel({
|
||||
{"\u2318"}Enter
|
||||
</span>
|
||||
{isReadOnly && (
|
||||
<span className="ml-2 flex items-center gap-1 rounded-sm bg-amber-500/10 px-1.5 py-0.5 text-[10px] font-semibold tracking-wide text-amber-500">
|
||||
<span className="ml-2 flex items-center gap-1 rounded-sm bg-warning/10 px-1.5 py-0.5 text-[10px] font-semibold tracking-wide text-warning">
|
||||
<Lock className="h-2.5 w-2.5" />
|
||||
READ
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{aiBarOpen && (
|
||||
<AiBar
|
||||
connectionId={connectionId}
|
||||
onSqlGenerated={(sql) => {
|
||||
setSqlValue(sql);
|
||||
onSqlChange?.(sql);
|
||||
}}
|
||||
onClose={() => setAiBarOpen(false)}
|
||||
onExecute={handleExecute}
|
||||
/>
|
||||
)}
|
||||
<div className="min-h-0 flex-1">
|
||||
<SqlEditor
|
||||
value={sqlValue}
|
||||
@@ -400,7 +299,7 @@ export function WorkspacePanel({
|
||||
<ResizableHandle withHandle />
|
||||
<ResizablePanel id="results" defaultSize="60%" minSize="15%">
|
||||
<div className="flex h-full flex-col overflow-hidden">
|
||||
{(explainData || result || error || aiExplanation) && (
|
||||
{(explainData || result || error) && (
|
||||
<div className="flex shrink-0 items-center border-b border-border/40 text-xs">
|
||||
<button
|
||||
className={`relative px-3 py-1.5 font-medium transition-colors ${
|
||||
@@ -469,10 +368,6 @@ export function WorkspacePanel({
|
||||
error={error}
|
||||
isLoading={queryMutation.isPending && resultView === "results"}
|
||||
viewMode={resultViewMode}
|
||||
aiExplanation={aiExplanation}
|
||||
isAiLoading={isAiLoading}
|
||||
onExplainError={error ? handleExplainError : undefined}
|
||||
onFixError={error ? handleFixError : undefined}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user