"use client"; import Link from "next/link"; import { ArrowLeft, Download, RefreshCw, Send } from "lucide-react"; import { FormEvent, useEffect, useState } from "react"; import { ApiError, apiFetch } from "@/lib/api"; interface HistoryItem { query_id?: string; question?: string; status?: string; row_count?: number; create_time?: string; } export default function Nl2SqlPage() { const [tab, setTab] = useState<"query" | "history" | "config">("query"); const [question, setQuestion] = useState(""); const [result, setResult] = useState(null); const [history, setHistory] = useState([]); const [config, setConfig] = useState>({}); const [message, setMessage] = useState(""); const [loading, setLoading] = useState(false); const [userType, setUserType] = useState(null); const isAdmin = userType === "ADMIN"; const canQuery = userType === "EMPLOYEE"; useEffect(() => { void apiFetch<{ user: { user_type?: string } }>("/auth/me") .then(({ user }) => { const nextType = user.user_type ?? null; setUserType(nextType); if (nextType === "ADMIN") setTab("config"); }) .catch(() => setUserType(null)); }, []); useEffect(() => { if (tab === "history" && canQuery) void loadHistory(); if (tab === "config" && isAdmin) void loadConfig(); }, [tab, isAdmin, canQuery]); async function query(event: FormEvent) { event.preventDefault(); setLoading(true); setMessage(""); if (!canQuery) { setMessage("当前账号仅可管理 NL2SQL 配置,不能执行数据查询。"); setLoading(false); return; } try { setResult(await apiFetch("/nl2sql/query", { method: "POST", body: JSON.stringify({ question, output_format: "json", include_sql: true, page: 1, page_size: 100 }) })); } catch (error) { setMessage(error instanceof ApiError && error.status === 403 ? "当前账号没有 NL2SQL 查询权限,请使用员工账号登录。" : error instanceof Error ? error.message : "查询失败"); } finally { setLoading(false); } } async function loadHistory() { try { const response = await apiFetch<{ items?: HistoryItem[] }>("/nl2sql/query-history?page=1&page_size=50"); setHistory(response.items ?? []); } catch (error) { setMessage(error instanceof Error ? error.message : "历史加载失败"); } } async function loadConfig() { try { setConfig(await apiFetch>("/nl2sql/admin/runtime-config")); } catch (error) { setMessage(error instanceof Error ? error.message : "配置加载失败"); } } async function saveConfig() { try { await apiFetch("/nl2sql/admin/runtime-config", { method: "PATCH", body: JSON.stringify(config) }); setMessage("运行配置已保存"); } catch (error) { setMessage(error instanceof Error ? error.message : "配置保存失败"); } } if (userType === null) return
返回员工工作区

正在校验账号权限...

; return
返回员工工作区

NL2SQL 数据中心

{isAdmin ?

当前为 ADMIN 账号,仅开放运行配置管理。数据查询由普通员工账号使用。

:

当前账号可使用查询和历史功能,运行配置仅对 ADMIN 开放。

}
{canQuery && <>}{isAdmin && }
{message &&

{message}

}{tab === "query" && canQuery && <>
setQuestion(event.target.value)} placeholder="例如:查询本月基金产品数量" className="min-w-0 flex-1 rounded-lg border border-slate-200 px-4 py-3 text-sm" />
{result ? JSON.stringify(result, null, 2) : "查询结果将在这里显示"}
}{tab === "history" && canQuery &&

查询历史

{history.map((item) =>
)}{!history.length &&

暂无查询历史

}
{Boolean(result) &&
{JSON.stringify(result, null, 2)}
}
}{tab === "config" && isAdmin &&

运行配置