1545 lines
63 KiB
TypeScript
1545 lines
63 KiB
TypeScript
import { ChevronDown, ChevronRight, Download, MoonStar, PackageOpen, ShieldCheck, Sparkles, Square, UserRoundMinus, UserRoundPlus, WandSparkles } from "lucide-react";
|
||
import { type FormEvent, useCallback, useEffect, useMemo, useState } from "react";
|
||
|
||
import { platformApiClient } from "../api/client";
|
||
import type {
|
||
ConfigDiffLineResponse,
|
||
ArtifactDownloadReferenceResponse,
|
||
ArtifactResponse,
|
||
GamePluginResponse,
|
||
JobResponse,
|
||
LogEntryBody,
|
||
LogStreamResponse,
|
||
ServerConfigDiffPreviewResponse,
|
||
ServerConfigResponse,
|
||
ServerInstanceResponse,
|
||
ServerMemberResponse,
|
||
ServerMetricsResponse
|
||
} from "../api/types";
|
||
import { ConfirmDialog, DiffView, UsageMeter } from "../components/OperationControls";
|
||
import { DiagnosticSummary, EmptyState, ErrorState, LoadingState, ResultBadge } from "../components/StateViews";
|
||
import type { PageComponentProps } from "../contracts/page";
|
||
import type { PluginBridgeAction, PluginBridgeManifestContract } from "../contracts/pluginBridge";
|
||
import { canStartServer, canStopServer, pluginLabel } from "../contracts/serverManagement";
|
||
import {
|
||
serverDetailSections,
|
||
serverIsOnline,
|
||
type ConfigDiffView,
|
||
type LlmSuggestionView,
|
||
type PluginControlDescriptor,
|
||
type PluginControlGroupView,
|
||
type ServerDetailSection
|
||
} from "../contracts/workspace";
|
||
import { serverLifecycleCommandRequest } from "../schemas/serverManagement";
|
||
import { buildConfigDiff, diffHasChanges } from "../utils/diff";
|
||
import { createPluginBridgeDispatcher, createPluginBridgeHostContext, parsePluginArtifactReference } from "../utils/pluginBridgeHost";
|
||
import { cx } from "../utils/classes";
|
||
import { stateLabel, statusClass } from "./ServersPage";
|
||
|
||
type LoadState<T> = { status: "loading" } | { status: "error"; reason: string } | { status: "ready"; data: T };
|
||
|
||
const fallbackConfig = "# server.properties\nmax-players=20\nmotd=Welcome to the server\npvp=true\nview-distance=8\n";
|
||
const defaultConfigKey = "server.properties";
|
||
|
||
export function ServerDetailPage({ session, params, operations, onNavigate }: PageComponentProps) {
|
||
const serverId = params.serverId ?? "";
|
||
const [section, setSection] = useState<ServerDetailSection>("overview");
|
||
const [instance, setInstance] = useState<LoadState<ServerInstanceResponse>>({ status: "loading" });
|
||
const [metrics, setMetrics] = useState<ServerMetricsResponse | null>(null);
|
||
const [plugins, setPlugins] = useState<GamePluginResponse[]>([]);
|
||
const [jobs, setJobs] = useState<JobResponse[]>([]);
|
||
const [artifacts, setArtifacts] = useState<ArtifactResponse[]>([]);
|
||
const [confirm, setConfirm] = useState<null | { title: string; description: string; danger?: boolean; run: () => Promise<void> }>(null);
|
||
const [confirmBusy, setConfirmBusy] = useState(false);
|
||
|
||
const refresh = useCallback(async () => {
|
||
if (!serverId) {
|
||
setInstance({ status: "error", reason: "缺少服务器 ID" });
|
||
return;
|
||
}
|
||
setInstance({ status: "loading" });
|
||
try {
|
||
const [detail, pluginResponse, jobResponse] = await Promise.all([
|
||
platformApiClient.getServerInstance(serverId),
|
||
platformApiClient.listGamePlugins(),
|
||
platformApiClient.listJobs(serverId)
|
||
]);
|
||
setInstance({ status: "ready", data: detail });
|
||
setPlugins(pluginResponse.items);
|
||
setJobs(jobResponse.items);
|
||
const artifactLists = await Promise.all(
|
||
jobResponse.items.slice(0, 20).map((job) =>
|
||
platformApiClient
|
||
.listArtifacts({ ownerKind: "job", ownerId: job.id, state: "available" })
|
||
.then((response) => response.items)
|
||
.catch(() => [] as ArtifactResponse[])
|
||
)
|
||
);
|
||
setArtifacts(uniqueArtifacts(artifactLists.flat()));
|
||
} catch (error) {
|
||
setInstance({ status: "error", reason: error instanceof Error ? error.message : "加载失败" });
|
||
setArtifacts([]);
|
||
}
|
||
try {
|
||
const metricsResponse = await platformApiClient.listServerMetrics();
|
||
setMetrics(metricsResponse.items.find((item) => item.serverInstanceId === serverId) ?? null);
|
||
} catch {
|
||
setMetrics(null);
|
||
}
|
||
}, [serverId]);
|
||
|
||
useEffect(() => {
|
||
void refresh();
|
||
}, [refresh]);
|
||
|
||
const serverOperations = useMemo(
|
||
() => operations.operations.filter((operation) => operation.targetId === serverId || operation.targetId.startsWith(`${serverId}:`)),
|
||
[operations.operations, serverId]
|
||
);
|
||
|
||
function requestLifecycle(current: ServerInstanceResponse, action: "start" | "stop") {
|
||
setConfirm({
|
||
title: action === "start" ? "启动服务器" : "停止服务器",
|
||
description:
|
||
action === "start"
|
||
? `确认启动服务器 ${current.name}(${current.id})?`
|
||
: `停止服务器 ${current.name}(${current.id})会断开所有在线玩家,确认继续?`,
|
||
danger: action === "stop",
|
||
run: async () => {
|
||
const operationId = operations.begin({
|
||
intent: action === "start" ? "启动服务器" : "停止服务器",
|
||
targetKind: "server",
|
||
targetId: current.id,
|
||
requester: session.displayName
|
||
});
|
||
try {
|
||
const result =
|
||
action === "start"
|
||
? await platformApiClient.startServerInstance(current.id, serverLifecycleCommandRequest(current, "start"))
|
||
: await platformApiClient.stopServerInstance(current.id, serverLifecycleCommandRequest(current, "stop"));
|
||
operations.succeed(operationId, `任务 ${result.job.id}(${result.job.capability})已派发`, result.job);
|
||
await refresh();
|
||
} catch (error) {
|
||
operations.fail(operationId, error instanceof Error ? error.message : "操作失败", operationId);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
if (!serverId) {
|
||
return (
|
||
<EmptyState title="未选择服务器" description="请从服务器列表进入详情页。" actionLabel="返回服务器列表" onAction={() => onNavigate("servers")} />
|
||
);
|
||
}
|
||
|
||
return (
|
||
<section className="server-detail-page" aria-labelledby="server-detail-title">
|
||
<div className="action-strip">
|
||
<button type="button" className="icon-command" onClick={() => onNavigate("servers")}>
|
||
<MoonStar size={16} />
|
||
<span>返回列表</span>
|
||
</button>
|
||
<button type="button" className="icon-command" onClick={() => void refresh()}>
|
||
<Sparkles size={16} />
|
||
<span>刷新</span>
|
||
</button>
|
||
</div>
|
||
|
||
{instance.status === "loading" && <LoadingState label="正在加载服务器详情…" />}
|
||
{instance.status === "error" && (
|
||
<ErrorState title="服务器详情加载失败" reason={instance.reason} diagnosticId={`server-detail:${serverId}`} onRetry={() => void refresh()} />
|
||
)}
|
||
|
||
{instance.status !== "ready" && (
|
||
<nav className="section-tabs" aria-label="server sections">
|
||
{serverDetailSections.map((entry) => (
|
||
<button
|
||
key={entry.id}
|
||
type="button"
|
||
className={cx("section-tab", section === entry.id && "section-tab-active")}
|
||
aria-current={section === entry.id ? "page" : undefined}
|
||
onClick={() => setSection(entry.id)}
|
||
>
|
||
{entry.label}
|
||
</button>
|
||
))}
|
||
</nav>
|
||
)}
|
||
|
||
{instance.status === "ready" && (
|
||
<>
|
||
<header className="server-detail-header">
|
||
<div className="server-detail-title-row">
|
||
<div>
|
||
<h1 id="server-detail-title">{instance.data.name}</h1>
|
||
<span className="provider-id">
|
||
{instance.data.id} · 插件 {instance.data.pluginId}@{instance.data.pluginVersion} · 节点 {instance.data.runEndpointId}
|
||
</span>
|
||
</div>
|
||
<div className="action-strip">
|
||
<span className={cx("status-pill", statusClass(instance.data.state))}>{stateLabel(instance.data.state)}</span>
|
||
<button
|
||
type="button"
|
||
className="icon-command"
|
||
disabled={!canStartServer(instance.data.state) || operations.isPending(instance.data.id, "启动服务器")}
|
||
onClick={() => requestLifecycle(instance.data, "start")}
|
||
>
|
||
<WandSparkles size={15} />
|
||
<span>启动</span>
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="icon-command danger-command"
|
||
disabled={!canStopServer(instance.data.state) || operations.isPending(instance.data.id, "停止服务器")}
|
||
onClick={() => requestLifecycle(instance.data, "stop")}
|
||
>
|
||
<Square size={15} />
|
||
<span>停止</span>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<div className="server-detail-stat-strip">
|
||
<HeaderStat label="状态" value={serverIsOnline(instance.data.state) ? "在线" : "离线"} />
|
||
<HeaderStat label="玩家" value={metrics?.playerCount !== undefined ? `${metrics.playerCount}${metrics.maxPlayers ? `/${metrics.maxPlayers}` : ""}` : "--"} />
|
||
<HeaderStat label="TPS" value={metrics?.tps !== undefined ? metrics.tps.toFixed(1) : "--"} />
|
||
<HeaderStat label="延迟" value={metrics?.latencyMs !== undefined ? `${Math.round(metrics.latencyMs)}ms` : "--"} />
|
||
<HeaderStat label="CPU" value={metrics?.cpuPercent !== undefined ? `${Math.round(metrics.cpuPercent)}%` : "--"} />
|
||
<HeaderStat label="内存" value={metrics?.memoryPercent !== undefined ? `${Math.round(metrics.memoryPercent)}%` : "--"} />
|
||
<HeaderStat label="磁盘" value={metrics?.diskPercent !== undefined ? `${Math.round(metrics.diskPercent)}%` : "--"} />
|
||
</div>
|
||
</header>
|
||
|
||
<nav className="section-tabs" aria-label="server sections">
|
||
{serverDetailSections.map((entry) => (
|
||
<button
|
||
key={entry.id}
|
||
type="button"
|
||
className={cx("section-tab", section === entry.id && "section-tab-active")}
|
||
aria-current={section === entry.id ? "page" : undefined}
|
||
onClick={() => setSection(entry.id)}
|
||
>
|
||
{entry.label}
|
||
</button>
|
||
))}
|
||
</nav>
|
||
|
||
{section === "overview" && <OverviewSection instance={instance.data} metrics={metrics} jobs={jobs} onOpenLogs={() => setSection("logs")} />}
|
||
{section === "overview" && <ServerAdministratorsSection instance={instance.data} session={session} onChanged={(next) => setInstance({ status: "ready", data: next })} />}
|
||
{section === "logs" && <LogsSection serverId={serverId} />}
|
||
{section === "config" && <ConfigSection serverId={serverId} instance={instance.data} session={session} operations={operations} />}
|
||
{section === "plugins" && <PluginControlsSection serverId={serverId} instance={instance.data} plugins={plugins} artifacts={artifacts} session={session} operations={operations} />}
|
||
{section === "llm" && <LlmSection serverId={serverId} instance={instance.data} session={session} operations={operations} />}
|
||
{section === "history" && <HistorySection serverId={serverId} serverOperations={serverOperations} jobs={jobs} artifacts={artifacts} />}
|
||
</>
|
||
)}
|
||
|
||
<ConfirmDialog
|
||
open={confirm !== null}
|
||
title={confirm?.title ?? ""}
|
||
description={confirm?.description ?? ""}
|
||
confirmLabel="确认执行"
|
||
danger={confirm?.danger}
|
||
busy={confirmBusy}
|
||
onCancel={() => setConfirm(null)}
|
||
onConfirm={() => {
|
||
if (!confirm) {
|
||
return;
|
||
}
|
||
setConfirmBusy(true);
|
||
void confirm.run().finally(() => {
|
||
setConfirmBusy(false);
|
||
setConfirm(null);
|
||
});
|
||
}}
|
||
/>
|
||
</section>
|
||
);
|
||
}
|
||
|
||
function uniqueArtifacts(artifacts: ArtifactResponse[]): ArtifactResponse[] {
|
||
const byID = new Map<string, ArtifactResponse>();
|
||
for (const artifact of artifacts) {
|
||
byID.set(artifact.id, artifact);
|
||
}
|
||
return [...byID.values()];
|
||
}
|
||
|
||
interface ServerAdministratorsSectionProps {
|
||
instance: ServerInstanceResponse;
|
||
session: PageComponentProps["session"];
|
||
onChanged: (instance: ServerInstanceResponse) => void;
|
||
}
|
||
|
||
function ServerAdministratorsSection({ instance, session, onChanged }: ServerAdministratorsSectionProps) {
|
||
const [candidates, setCandidates] = useState<LoadState<ServerMemberResponse[]>>({ status: "loading" });
|
||
const [selectedUserId, setSelectedUserId] = useState("");
|
||
const [busyUserId, setBusyUserId] = useState<string | null>(null);
|
||
const [result, setResult] = useState<{ status: "succeeded" | "failed"; label: string } | null>(null);
|
||
const isOwner = instance.ownerUserId === session.id;
|
||
|
||
const refreshCandidates = useCallback(async () => {
|
||
if (!isOwner) {
|
||
setCandidates({ status: "ready", data: [] });
|
||
return;
|
||
}
|
||
setCandidates({ status: "loading" });
|
||
try {
|
||
const response = await platformApiClient.listServerAdministratorCandidates(instance.id);
|
||
setCandidates({ status: "ready", data: response.items });
|
||
setSelectedUserId((current) => (response.items.some((user) => user.id === current) ? current : response.items[0]?.id ?? ""));
|
||
} catch (error) {
|
||
setCandidates({ status: "error", reason: error instanceof Error ? error.message : "加载候选管理员失败" });
|
||
}
|
||
}, [instance.id, isOwner]);
|
||
|
||
useEffect(() => {
|
||
void refreshCandidates();
|
||
}, [refreshCandidates]);
|
||
|
||
async function addAdministrator(event: FormEvent<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
if (!selectedUserId) {
|
||
return;
|
||
}
|
||
setBusyUserId(selectedUserId);
|
||
setResult(null);
|
||
try {
|
||
const next = await platformApiClient.addServerAdministrator(instance.id, { userId: selectedUserId });
|
||
onChanged(next);
|
||
setResult({ status: "succeeded", label: "已添加服务器管理员" });
|
||
await refreshCandidates();
|
||
} catch (error) {
|
||
setResult({ status: "failed", label: error instanceof Error ? error.message : "添加管理员失败" });
|
||
} finally {
|
||
setBusyUserId(null);
|
||
}
|
||
}
|
||
|
||
async function removeAdministrator(userId: string) {
|
||
setBusyUserId(userId);
|
||
setResult(null);
|
||
try {
|
||
const next = await platformApiClient.removeServerAdministrator(instance.id, userId);
|
||
onChanged(next);
|
||
setResult({ status: "succeeded", label: "已移除服务器管理员" });
|
||
await refreshCandidates();
|
||
} catch (error) {
|
||
setResult({ status: "failed", label: error instanceof Error ? error.message : "移除管理员失败" });
|
||
} finally {
|
||
setBusyUserId(null);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<article className="console-panel" aria-label="server administrators">
|
||
<div className="panel-header">
|
||
<h2>
|
||
<ShieldCheck size={16} style={{ verticalAlign: "-2px" }} /> 管理成员
|
||
</h2>
|
||
<span className="page-status">{isOwner ? "服主可邀请/移除管理员" : "仅服主可调整成员"}</span>
|
||
</div>
|
||
{result && (
|
||
<div style={{ marginBottom: 10 }}>
|
||
<ResultBadge status={result.status} label={result.label} />
|
||
</div>
|
||
)}
|
||
<div className="action-list">
|
||
<span>服主:{instance.ownerUserId || "未绑定"}</span>
|
||
<span>服务器管理员:{instance.adminUserIds.length > 0 ? instance.adminUserIds.join(" / ") : "暂无"}</span>
|
||
</div>
|
||
{isOwner && (
|
||
<>
|
||
<form className="provider-form" style={{ marginTop: 12 }} onSubmit={(event) => void addAdministrator(event)} aria-label="邀请服务器管理员">
|
||
<div className="form-grid">
|
||
<label>
|
||
邀请管理员
|
||
<select value={selectedUserId} onChange={(event) => setSelectedUserId(event.target.value)} disabled={candidates.status !== "ready" || candidates.data.length === 0}>
|
||
{candidates.status === "ready" && candidates.data.length === 0 && <option value="">暂无可邀请用户</option>}
|
||
{candidates.status === "ready" &&
|
||
candidates.data.map((user) => (
|
||
<option key={user.id} value={user.id}>
|
||
{user.displayName}({user.email ?? user.id})
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
</div>
|
||
{candidates.status === "loading" && <LoadingState label="正在加载可邀请用户…" compact />}
|
||
{candidates.status === "error" && (
|
||
<ErrorState title="候选管理员加载失败" reason={candidates.reason} diagnosticId={`server-admin-candidates:${instance.id}`} onRetry={() => void refreshCandidates()} compact />
|
||
)}
|
||
<button type="submit" className="primary-command" disabled={!selectedUserId || busyUserId !== null}>
|
||
<UserRoundPlus size={14} />
|
||
<span>{busyUserId === selectedUserId ? "邀请中…" : "邀请为管理员"}</span>
|
||
</button>
|
||
</form>
|
||
{instance.adminUserIds.length > 0 && (
|
||
<div className="resource-list" style={{ marginTop: 12 }}>
|
||
{instance.adminUserIds.map((userId) => (
|
||
<article key={userId} className="resource-list-item">
|
||
<span>
|
||
<strong>{userId}</strong>
|
||
<span className="provider-id">服务器管理员</span>
|
||
</span>
|
||
<button type="button" className="theme-upload" disabled={busyUserId !== null} onClick={() => void removeAdministrator(userId)}>
|
||
<UserRoundMinus size={13} />
|
||
<span>{busyUserId === userId ? "移除中…" : "移除"}</span>
|
||
</button>
|
||
</article>
|
||
))}
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</article>
|
||
);
|
||
}
|
||
|
||
function HeaderStat({ label, value }: { label: string; value: string }) {
|
||
return (
|
||
<span className="server-card-stat">
|
||
<span>{label}</span>
|
||
<strong>{value}</strong>
|
||
</span>
|
||
);
|
||
}
|
||
|
||
interface OverviewSectionProps {
|
||
instance: ServerInstanceResponse;
|
||
metrics: ServerMetricsResponse | null;
|
||
jobs: JobResponse[];
|
||
onOpenLogs: () => void;
|
||
}
|
||
|
||
function OverviewSection({ instance, metrics, jobs, onOpenLogs }: OverviewSectionProps) {
|
||
const pending = jobs.filter((job) => job.state === "queued" || job.state === "accepted" || job.state === "running");
|
||
const failed = jobs.filter((job) => job.state === "failed");
|
||
return (
|
||
<div className="overview-two-col">
|
||
<article className="console-panel">
|
||
<div className="panel-header">
|
||
<h2>资源使用</h2>
|
||
<span className="page-status">{metrics ? new Date(metrics.collectedAt).toLocaleTimeString() : "暂无指标"}</span>
|
||
</div>
|
||
<div className="server-card-meters">
|
||
<UsageMeter label="CPU" percent={metrics?.cpuPercent} />
|
||
<UsageMeter label="内存" percent={metrics?.memoryPercent} />
|
||
<UsageMeter label="磁盘" percent={metrics?.diskPercent} />
|
||
</div>
|
||
</article>
|
||
<article className="console-panel">
|
||
<div className="panel-header">
|
||
<h2>需要关注</h2>
|
||
<button type="button" className="icon-command" onClick={onOpenLogs}>
|
||
查看日志
|
||
</button>
|
||
</div>
|
||
<div className="action-list">
|
||
{instance.state === "failed" && <span>⚠ 服务器处于异常状态,建议查看日志与操作历史。</span>}
|
||
{failed.length > 0 && <span>⚠ 最近有 {failed.length} 个任务失败。</span>}
|
||
{pending.length > 0 ? (
|
||
<span>
|
||
进行中任务:{pending[0].capability}({pending[0].state},{pending[0].progress.percent}%)
|
||
</span>
|
||
) : (
|
||
<span>当前没有进行中的任务。</span>
|
||
)}
|
||
<span>配置版本:v{instance.configVersion},最近更新 {new Date(instance.updatedAt).toLocaleString()}</span>
|
||
</div>
|
||
</article>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
interface LogsSectionProps {
|
||
serverId: string;
|
||
}
|
||
|
||
interface LogFilterState {
|
||
level: string;
|
||
keyword: string;
|
||
source: string;
|
||
sinceMinutes: string;
|
||
}
|
||
|
||
function LogsSection({ serverId }: LogsSectionProps) {
|
||
const [streams, setStreams] = useState<LoadState<LogStreamResponse[]>>({ status: "loading" });
|
||
const [entries, setEntries] = useState<Array<LogEntryBody & { source: string }>>([]);
|
||
const [filter, setFilter] = useState<LogFilterState>({ level: "all", keyword: "", source: "all", sinceMinutes: "all" });
|
||
const [selected, setSelected] = useState<(LogEntryBody & { source: string }) | null>(null);
|
||
|
||
const refresh = useCallback(async () => {
|
||
setStreams({ status: "loading" });
|
||
try {
|
||
const response = await platformApiClient.listLogStreams();
|
||
const serverStreams = response.items.filter((stream) => stream.serverInstanceId === serverId);
|
||
setStreams({ status: "ready", data: serverStreams });
|
||
const collected: Array<LogEntryBody & { source: string }> = [];
|
||
for (const stream of serverStreams) {
|
||
try {
|
||
const cursor = await platformApiClient.queryLogStream({ logStreamId: stream.id, afterSeq: 0, limit: 200 });
|
||
collected.push(...cursor.entries.map((entry) => ({ ...entry, source: stream.source || stream.streamKey })));
|
||
} catch {
|
||
// one unreadable stream should not blank the rest
|
||
}
|
||
}
|
||
collected.sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1));
|
||
setEntries(collected);
|
||
} catch (error) {
|
||
setStreams({ status: "error", reason: error instanceof Error ? error.message : "加载失败" });
|
||
}
|
||
}, [serverId]);
|
||
|
||
useEffect(() => {
|
||
void refresh();
|
||
}, [refresh]);
|
||
|
||
const sources = useMemo(() => [...new Set(entries.map((entry) => entry.source))], [entries]);
|
||
|
||
const visible = useMemo(() => {
|
||
const keyword = filter.keyword.trim().toLowerCase();
|
||
const sinceMs = filter.sinceMinutes === "all" ? null : Date.now() - Number(filter.sinceMinutes) * 60_000;
|
||
return entries.filter((entry) => {
|
||
if (filter.level !== "all" && (entry.level ?? "info").toLowerCase() !== filter.level) {
|
||
return false;
|
||
}
|
||
if (filter.source !== "all" && entry.source !== filter.source) {
|
||
return false;
|
||
}
|
||
if (keyword && !entry.line.toLowerCase().includes(keyword)) {
|
||
return false;
|
||
}
|
||
if (sinceMs !== null && new Date(entry.timestamp).getTime() < sinceMs) {
|
||
return false;
|
||
}
|
||
return true;
|
||
});
|
||
}, [entries, filter]);
|
||
|
||
return (
|
||
<article className="console-panel" aria-label="server logs">
|
||
<div className="panel-header">
|
||
<h2>日志</h2>
|
||
<button type="button" className="icon-command" onClick={() => void refresh()}>
|
||
<Sparkles size={14} />
|
||
<span>刷新</span>
|
||
</button>
|
||
</div>
|
||
<div className="log-filter-bar">
|
||
<select value={filter.level} aria-label="按级别过滤" onChange={(event) => setFilter((current) => ({ ...current, level: event.target.value }))}>
|
||
<option value="all">全部级别</option>
|
||
<option value="error">error</option>
|
||
<option value="warn">warn</option>
|
||
<option value="info">info</option>
|
||
<option value="debug">debug</option>
|
||
</select>
|
||
<select value={filter.source} aria-label="按来源过滤" onChange={(event) => setFilter((current) => ({ ...current, source: event.target.value }))}>
|
||
<option value="all">全部来源</option>
|
||
{sources.map((source) => (
|
||
<option key={source} value={source}>
|
||
{source}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<select
|
||
value={filter.sinceMinutes}
|
||
aria-label="按时间过滤"
|
||
onChange={(event) => setFilter((current) => ({ ...current, sinceMinutes: event.target.value }))}
|
||
>
|
||
<option value="all">全部时间</option>
|
||
<option value="15">最近 15 分钟</option>
|
||
<option value="60">最近 1 小时</option>
|
||
<option value="1440">最近 24 小时</option>
|
||
</select>
|
||
<input
|
||
type="search"
|
||
value={filter.keyword}
|
||
placeholder="关键字"
|
||
aria-label="按关键字过滤"
|
||
onChange={(event) => setFilter((current) => ({ ...current, keyword: event.target.value }))}
|
||
/>
|
||
</div>
|
||
{streams.status === "loading" && <LoadingState label="正在加载日志…" compact />}
|
||
{streams.status === "error" && <ErrorState title="日志加载失败" reason={streams.reason} diagnosticId={`logs:${serverId}`} onRetry={() => void refresh()} compact />}
|
||
{streams.status === "ready" && entries.length === 0 && (
|
||
<EmptyState title="暂无日志" description="该服务器还没有已入库的日志流,或运行端尚未上报日志。" actionLabel="刷新" onAction={() => void refresh()} />
|
||
)}
|
||
{streams.status === "ready" && entries.length > 0 && visible.length === 0 && (
|
||
<EmptyState title="没有匹配的日志" description="调整级别、来源、时间范围或关键字后再试。" />
|
||
)}
|
||
{visible.length > 0 && (
|
||
<div className="log-list" role="list">
|
||
{visible.map((entry) => (
|
||
<button key={`${entry.source}-${entry.seq}`} type="button" className="log-line" role="listitem" onClick={() => setSelected(entry)}>
|
||
<time>{new Date(entry.timestamp).toLocaleTimeString()}</time>
|
||
<span className={cx("log-level", levelClass(entry.level))}>{(entry.level ?? "info").toUpperCase()}</span>
|
||
<span>{entry.line}</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{selected && (
|
||
<div className="drawer-backdrop" role="presentation" onClick={() => setSelected(null)}>
|
||
<div className="drawer-panel" role="dialog" aria-modal="true" aria-label="日志详情" onClick={(event) => event.stopPropagation()}>
|
||
<div className="panel-header">
|
||
<h2>日志详情</h2>
|
||
<button type="button" className="drawer-close" onClick={() => setSelected(null)}>
|
||
关闭
|
||
</button>
|
||
</div>
|
||
<dl className="detail-list">
|
||
<div>
|
||
<dt>时间</dt>
|
||
<dd>{new Date(selected.timestamp).toLocaleString()}</dd>
|
||
</div>
|
||
<div>
|
||
<dt>级别</dt>
|
||
<dd>{(selected.level ?? "info").toUpperCase()}</dd>
|
||
</div>
|
||
<div>
|
||
<dt>来源</dt>
|
||
<dd>{selected.source}</dd>
|
||
</div>
|
||
<div>
|
||
<dt>序号</dt>
|
||
<dd>{selected.seq}</dd>
|
||
</div>
|
||
<div>
|
||
<dt>内容</dt>
|
||
<dd>{selected.line}</dd>
|
||
</div>
|
||
{selected.fields && Object.keys(selected.fields).length > 0 && (
|
||
<div>
|
||
<dt>字段</dt>
|
||
<dd>
|
||
{Object.entries(selected.fields)
|
||
.map(([key, value]) => `${key}=${value}`)
|
||
.join(" ")}
|
||
</dd>
|
||
</div>
|
||
)}
|
||
</dl>
|
||
<DiagnosticSummary diagnosticId={`log:${serverId}:${selected.source}:${selected.seq}`} detail={selected.line} />
|
||
</div>
|
||
</div>
|
||
)}
|
||
</article>
|
||
);
|
||
}
|
||
|
||
function levelClass(level?: string): string {
|
||
const normalized = (level ?? "info").toLowerCase();
|
||
if (normalized === "error" || normalized === "fatal") {
|
||
return "log-level-error";
|
||
}
|
||
if (normalized === "warn" || normalized === "warning") {
|
||
return "log-level-warn";
|
||
}
|
||
return "log-level-info";
|
||
}
|
||
|
||
interface ConfigSectionProps {
|
||
serverId: string;
|
||
instance: ServerInstanceResponse;
|
||
session: PageComponentProps["session"];
|
||
operations: PageComponentProps["operations"];
|
||
}
|
||
|
||
function ConfigSection({ serverId, instance, session, operations }: ConfigSectionProps) {
|
||
const [config, setConfig] = useState<LoadState<{ content: string; source: "api" | "local" }>>({ status: "loading" });
|
||
const [draft, setDraft] = useState("");
|
||
const [diff, setDiff] = useState<ConfigDiffView | null>(null);
|
||
const [previewBusy, setPreviewBusy] = useState(false);
|
||
const [previewError, setPreviewError] = useState<string | null>(null);
|
||
|
||
const refresh = useCallback(async () => {
|
||
setConfig({ status: "loading" });
|
||
try {
|
||
const response: ServerConfigResponse = await platformApiClient.getServerConfig(serverId);
|
||
setConfig({ status: "ready", data: { content: response.content, source: "api" } });
|
||
setDraft(response.content);
|
||
} catch {
|
||
setConfig({ status: "ready", data: { content: fallbackConfig, source: "local" } });
|
||
setDraft(fallbackConfig);
|
||
}
|
||
}, [serverId]);
|
||
|
||
useEffect(() => {
|
||
void refresh();
|
||
}, [refresh]);
|
||
|
||
async function prepareDiff(event: FormEvent<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
if (config.status !== "ready") {
|
||
return;
|
||
}
|
||
setPreviewBusy(true);
|
||
setPreviewError(null);
|
||
try {
|
||
const preview = await platformApiClient.previewServerConfigDiff(serverId, {
|
||
expectedConfigVersion: instance.configVersion,
|
||
key: defaultConfigKey,
|
||
proposedContent: draft
|
||
});
|
||
setDiff(configDiffViewFromPreview(preview));
|
||
} catch (error) {
|
||
setPreviewError(error instanceof Error ? error.message : "配置差异预览失败");
|
||
} finally {
|
||
setPreviewBusy(false);
|
||
}
|
||
}
|
||
|
||
async function submitDiff() {
|
||
if (!diff || config.status !== "ready") {
|
||
return;
|
||
}
|
||
const operationId = operations.begin({ intent: "写入配置", targetKind: "config", targetId: serverId, requester: session.displayName });
|
||
try {
|
||
const dispatch = await platformApiClient.approveServerConfigWrite(serverId, {
|
||
expectedConfigVersion: diff.configVersion ?? instance.configVersion,
|
||
key: diff.key ?? defaultConfigKey,
|
||
proposedContent: diff.nextContent,
|
||
proposedContentInputRef: diff.proposedContentInputRef,
|
||
idempotencyKey: `web:config.write:${serverId}:${Date.now()}`
|
||
});
|
||
const job = dispatch.job;
|
||
operations.succeed(operationId, `配置写入任务 ${job.id} 已派发`, job);
|
||
setDiff(null);
|
||
} catch (error) {
|
||
operations.fail(operationId, error instanceof Error ? error.message : "配置写入任务派发失败", operationId);
|
||
}
|
||
}
|
||
|
||
const writeOperation = operations.operations.find((operation) => operation.intent === "写入配置" && operation.targetId === serverId);
|
||
|
||
return (
|
||
<article className="console-panel" aria-label="server configuration">
|
||
<div className="panel-header">
|
||
<h2>配置</h2>
|
||
{config.status === "ready" && (
|
||
<span className="page-status">{config.data.source === "api" ? `配置版本 v${instance.configVersion}` : "本地示例配置(配置读取接口未提供)"}</span>
|
||
)}
|
||
</div>
|
||
{writeOperation && (
|
||
<div style={{ marginBottom: 10 }}>
|
||
<ResultBadge
|
||
status={writeOperation.status}
|
||
label={
|
||
writeOperation.status === "pending"
|
||
? "配置写入中…"
|
||
: writeOperation.status === "succeeded"
|
||
? (writeOperation.message ?? "写入任务已派发")
|
||
: `写入失败:${writeOperation.errorReason}(诊断 ${writeOperation.diagnosticId})`
|
||
}
|
||
/>
|
||
</div>
|
||
)}
|
||
{config.status === "loading" && <LoadingState label="正在加载配置…" compact />}
|
||
{previewError && <ErrorState title="配置差异预览失败" reason={previewError} compact />}
|
||
{config.status === "ready" && (
|
||
<form className="provider-form" style={{ border: 0, padding: 0 }} onSubmit={(event) => void prepareDiff(event)}>
|
||
<label>
|
||
配置内容
|
||
<textarea value={draft} onChange={(event) => setDraft(event.target.value)} rows={10} aria-label="配置编辑器" />
|
||
</label>
|
||
<button type="submit" className="primary-command" disabled={previewBusy || draft === config.data.content}>
|
||
{previewBusy ? "预览中…" : "预览变更"}
|
||
</button>
|
||
</form>
|
||
)}
|
||
|
||
{diff && (
|
||
<div className="drawer-backdrop" role="presentation" onClick={() => setDiff(null)}>
|
||
<div className="drawer-panel" role="dialog" aria-modal="true" aria-label="配置变更确认" onClick={(event) => event.stopPropagation()}>
|
||
<div className="panel-header">
|
||
<h2>确认配置变更</h2>
|
||
<span className="page-status">{diff.summary}</span>
|
||
</div>
|
||
<p style={{ margin: 0, color: "var(--ink-soft)", fontSize: 14 }}>
|
||
目标服务器:<strong>{instance.name}</strong>({serverId})。请检查平台返回的差异,确认后才会派发写入任务。
|
||
</p>
|
||
<DiffView lines={diff.lines} />
|
||
<div className="confirm-actions">
|
||
<button type="button" onClick={() => setDiff(null)}>
|
||
取消
|
||
</button>
|
||
<button type="button" className="confirm-primary" disabled={!diffHasChanges(diff)} onClick={() => void submitDiff()}>
|
||
确认并派发写入任务
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</article>
|
||
);
|
||
}
|
||
|
||
interface PluginControlsSectionProps {
|
||
serverId: string;
|
||
instance: ServerInstanceResponse;
|
||
plugins: GamePluginResponse[];
|
||
artifacts: ArtifactResponse[];
|
||
session: PageComponentProps["session"];
|
||
operations: PageComponentProps["operations"];
|
||
}
|
||
|
||
function controlsForPlugin(plugin: GamePluginResponse): PluginControlDescriptor[] {
|
||
const controls: PluginControlDescriptor[] = [];
|
||
for (const [action] of Object.entries(plugin.lifecycleActions)) {
|
||
if (action === "install" || action === "restart" || action === "status") {
|
||
continue;
|
||
}
|
||
if (action !== "start" && action !== "stop") {
|
||
continue;
|
||
}
|
||
controls.push({
|
||
key: `lifecycle:${action}`,
|
||
label: lifecycleControlLabel(action),
|
||
description: `通过平台生命周期 API 执行插件声明的 ${action} 动作`,
|
||
capability: `process.${action}`,
|
||
lifecycleAction: action,
|
||
dangerous: action === "stop"
|
||
});
|
||
}
|
||
for (const bridgeAction of plugin.bridgeActions) {
|
||
if (bridgeAction === "jobs.dispatch") {
|
||
controls.push({
|
||
key: "bridge:gift",
|
||
label: "发送礼物",
|
||
description: "通过插件任务向在线玩家发放礼物",
|
||
capability: "plugin.gift.send",
|
||
dangerous: false
|
||
});
|
||
controls.push({
|
||
key: "bridge:activity",
|
||
label: "调整活动",
|
||
description: "修改插件当前的活动配置",
|
||
capability: "plugin.activity.update",
|
||
dangerous: false
|
||
});
|
||
}
|
||
if (bridgeAction === "logs.query") {
|
||
controls.push({
|
||
key: "bridge:module-restart",
|
||
label: "重启插件模块",
|
||
description: "重启该插件在此服务器上的运行模块",
|
||
capability: "plugin.module.restart",
|
||
dangerous: true
|
||
});
|
||
}
|
||
}
|
||
const unique = new Map(controls.map((control) => [control.key, control]));
|
||
return [...unique.values()];
|
||
}
|
||
|
||
function lifecycleControlLabel(action: string): string {
|
||
switch (action) {
|
||
case "start":
|
||
return "启动进程";
|
||
case "stop":
|
||
return "停止进程";
|
||
case "restart":
|
||
return "重启进程";
|
||
default:
|
||
return action;
|
||
}
|
||
}
|
||
|
||
function PluginControlsSection({ serverId, instance, plugins, artifacts, session, operations }: PluginControlsSectionProps) {
|
||
const [collapsed, setCollapsed] = useState<Set<string>>(new Set());
|
||
const [confirmControl, setConfirmControl] = useState<null | { plugin: PluginControlGroupView; control: PluginControlDescriptor }>(null);
|
||
const [confirmBusy, setConfirmBusy] = useState(false);
|
||
|
||
const groups = useMemo<PluginControlGroupView[]>(() => {
|
||
const installed = plugins.filter((plugin) => plugin.id === instance.pluginId || plugin.status === "installed");
|
||
const relevant = installed.some((plugin) => plugin.id === instance.pluginId)
|
||
? installed.filter((plugin) => plugin.id === instance.pluginId)
|
||
: installed;
|
||
return relevant.map((plugin) => ({
|
||
pluginId: plugin.id,
|
||
pluginName: pluginLabel(plugin, plugin.id),
|
||
version: plugin.version,
|
||
status: plugin.status,
|
||
controls: controlsForPlugin(plugin)
|
||
}));
|
||
}, [plugins, instance.pluginId]);
|
||
|
||
function toggleGroup(pluginId: string) {
|
||
setCollapsed((current) => {
|
||
const next = new Set(current);
|
||
if (next.has(pluginId)) {
|
||
next.delete(pluginId);
|
||
} else {
|
||
next.add(pluginId);
|
||
}
|
||
return next;
|
||
});
|
||
}
|
||
|
||
async function dispatchControl(group: PluginControlGroupView, control: PluginControlDescriptor) {
|
||
const operationId = operations.begin({
|
||
intent: control.label,
|
||
targetKind: "plugin",
|
||
targetId: `${serverId}:${group.pluginId}`,
|
||
requester: session.displayName
|
||
});
|
||
try {
|
||
if (control.lifecycleAction === "start" || control.lifecycleAction === "stop") {
|
||
const result =
|
||
control.lifecycleAction === "start"
|
||
? await platformApiClient.startServerInstance(instance.id, serverLifecycleCommandRequest(instance, "start"))
|
||
: await platformApiClient.stopServerInstance(instance.id, serverLifecycleCommandRequest(instance, "stop"));
|
||
operations.succeed(operationId, `平台生命周期任务 ${result.job.id} 已派发(${result.job.capability})`, result.job);
|
||
return;
|
||
}
|
||
const job = await platformApiClient.createJob({
|
||
id: `job-${control.capability.replaceAll(".", "-")}-${serverId}-${Date.now()}`,
|
||
serverInstanceId: serverId,
|
||
runEndpointId: instance.runEndpointId,
|
||
capability: control.capability,
|
||
idempotencyKey: `web:${control.capability}:${serverId}:${group.pluginId}:${Date.now()}`
|
||
});
|
||
operations.succeed(operationId, `任务 ${job.id} 已派发(${control.capability})`, job);
|
||
} catch (error) {
|
||
operations.fail(operationId, error instanceof Error ? error.message : "插件操作派发失败", operationId);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div className="operation-list" aria-label="plugin controls">
|
||
{groups.length === 0 && (
|
||
<EmptyState title="该服务器没有可控制的插件" description="安装插件后,这里会按插件分组显示可用的日常操作。" />
|
||
)}
|
||
{groups.map((group) => {
|
||
const isCollapsed = collapsed.has(group.pluginId);
|
||
return (
|
||
<article key={group.pluginId} className="plugin-group">
|
||
<button type="button" className="plugin-group-header" aria-expanded={!isCollapsed} onClick={() => toggleGroup(group.pluginId)}>
|
||
<span>
|
||
<strong>{group.pluginName}</strong>
|
||
<span className="provider-id">
|
||
{group.pluginId}@{group.version} · 作用于 {serverId}
|
||
</span>
|
||
</span>
|
||
{isCollapsed ? <ChevronRight size={18} /> : <ChevronDown size={18} />}
|
||
</button>
|
||
{!isCollapsed && (
|
||
<div className="plugin-group-body">
|
||
{plugins.find((plugin) => plugin.id === group.pluginId) && (
|
||
<PluginBridgeExecutionPanel
|
||
plugin={plugins.find((plugin) => plugin.id === group.pluginId)!}
|
||
serverId={serverId}
|
||
serverInstance={instance}
|
||
artifacts={artifacts}
|
||
/>
|
||
)}
|
||
{group.controls.length === 0 && <span className="provider-id">该插件未声明可用控制项。</span>}
|
||
{group.controls.map((control) => {
|
||
const targetId = `${serverId}:${group.pluginId}`;
|
||
const latest = operations.operations.find((operation) => operation.targetId === targetId && operation.intent === control.label);
|
||
const pending = latest?.status === "pending";
|
||
return (
|
||
<div key={control.key} className="plugin-control-row">
|
||
<span>
|
||
<strong>{control.label}</strong>
|
||
<p>{control.description}</p>
|
||
{latest && (
|
||
<ResultBadge
|
||
status={latest.status}
|
||
label={
|
||
latest.status === "pending"
|
||
? "执行中…"
|
||
: latest.status === "succeeded"
|
||
? (latest.message ?? "已完成")
|
||
: `失败:${latest.errorReason}(诊断 ${latest.diagnosticId})`
|
||
}
|
||
/>
|
||
)}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
className={cx("icon-command", control.dangerous && "danger-command")}
|
||
disabled={pending}
|
||
onClick={() => setConfirmControl({ plugin: group, control })}
|
||
>
|
||
{pending ? "执行中…" : "执行"}
|
||
</button>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</article>
|
||
);
|
||
})}
|
||
|
||
<ConfirmDialog
|
||
open={confirmControl !== null}
|
||
title={`执行「${confirmControl?.control.label ?? ""}」`}
|
||
description={`目标:服务器 ${serverId} 的插件 ${confirmControl?.plugin.pluginName ?? ""}。该操作只影响当前服务器实例。`}
|
||
confirmLabel="确认执行"
|
||
danger={confirmControl?.control.dangerous}
|
||
busy={confirmBusy}
|
||
onCancel={() => setConfirmControl(null)}
|
||
onConfirm={() => {
|
||
if (!confirmControl) {
|
||
return;
|
||
}
|
||
setConfirmBusy(true);
|
||
void dispatchControl(confirmControl.plugin, confirmControl.control).finally(() => {
|
||
setConfirmBusy(false);
|
||
setConfirmControl(null);
|
||
});
|
||
}}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
interface PluginBridgeExecutionPanelProps {
|
||
plugin: GamePluginResponse;
|
||
serverId: string;
|
||
serverInstance: ServerInstanceResponse;
|
||
artifacts: ArtifactResponse[];
|
||
}
|
||
|
||
function PluginBridgeExecutionPanel({ plugin, serverId, serverInstance, artifacts }: PluginBridgeExecutionPanelProps) {
|
||
const [pendingAction, setPendingAction] = useState<PluginBridgeAction | null>(null);
|
||
const [result, setResult] = useState<{ status: "succeeded" | "failed" | "pending"; label: string } | null>(null);
|
||
const page = plugin.pages[0];
|
||
if (!page || plugin.bridgeActions.length === 0) {
|
||
return null;
|
||
}
|
||
const contract: PluginBridgeManifestContract = {
|
||
id: plugin.id,
|
||
declaredPermissions: plugin.declaredPermissions as PluginBridgeManifestContract["declaredPermissions"],
|
||
bridgeActions: plugin.bridgeActions as PluginBridgeAction[],
|
||
pages: plugin.pages.map((item) => ({
|
||
key: item.key,
|
||
title: item.title,
|
||
path: item.path,
|
||
permissions: item.permissions as PluginBridgeManifestContract["declaredPermissions"],
|
||
bridgeActions: item.bridgeActions as PluginBridgeAction[] | undefined
|
||
})),
|
||
aiPurposes: plugin.aiPurposes
|
||
};
|
||
const context = createPluginBridgeHostContext({
|
||
plugin: contract,
|
||
routeKey: page.key,
|
||
serverInstanceId: serverId,
|
||
themeTokens: { colorScheme: "dark", accentColor: "#7dd3fc" }
|
||
});
|
||
const executableActions = context.bridgeActions.filter(
|
||
(action) => action === "server.instances.read" || action === "files.request" || action === "artifacts.open" || action === "ai.invoke"
|
||
);
|
||
|
||
async function execute(action: PluginBridgeAction) {
|
||
setPendingAction(action);
|
||
setResult({ status: "pending", label: "桥接请求执行中" });
|
||
const dispatch = createPluginBridgeDispatcher(context, platformApiClient);
|
||
const response = await dispatch({
|
||
requestId: `web:bridge:${serverId}:${plugin.id}:${action}:${Date.now()}`,
|
||
action,
|
||
aiPurpose: action === "ai.invoke" ? plugin.aiPurposes[0] : undefined,
|
||
payload: bridgePayloadForAction(action, serverInstance, artifacts[0])
|
||
});
|
||
setPendingAction(null);
|
||
if (response.status === "ok" || response.status === "queued") {
|
||
setResult({ status: "succeeded", label: bridgeResultLabel(response.status, response.result) });
|
||
return;
|
||
}
|
||
setResult({ status: "failed", label: response.error?.message ?? "桥接执行被拒绝" });
|
||
}
|
||
|
||
return (
|
||
<div className="plugin-control-row" aria-label="plugin bridge execution">
|
||
<span>
|
||
<strong>{page.title} 桥接执行</strong>
|
||
<p>{context.permissions.join(" / ") || "无可用权限"}</p>
|
||
{result && <ResultBadge status={result.status} label={result.label} />}
|
||
</span>
|
||
<div className="action-strip">
|
||
{executableActions.slice(0, 3).map((action) => (
|
||
<button
|
||
key={action}
|
||
type="button"
|
||
className="icon-command"
|
||
disabled={pendingAction !== null || (action === "artifacts.open" && artifacts.length === 0)}
|
||
onClick={() => void execute(action)}
|
||
title={`执行 ${action}`}
|
||
>
|
||
<Sparkles size={14} />
|
||
<span>{pendingAction === action ? "执行中" : bridgeActionLabel(action)}</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function bridgePayloadForAction(action: PluginBridgeAction, serverInstance: ServerInstanceResponse, artifact?: ArtifactResponse): Record<string, string> | undefined {
|
||
if (action === "files.request") {
|
||
return { operation: "read", key: "logs/latest.log", expectedConfigVersion: String(serverInstance.configVersion) };
|
||
}
|
||
if (action === "artifacts.open" && artifact) {
|
||
return { artifactId: artifact.id };
|
||
}
|
||
return undefined;
|
||
}
|
||
|
||
function bridgeActionLabel(action: PluginBridgeAction): string {
|
||
switch (action) {
|
||
case "server.instances.read":
|
||
return "读取上下文";
|
||
case "files.request":
|
||
return "请求文件";
|
||
case "ai.invoke":
|
||
return "AI 调用";
|
||
case "artifacts.open":
|
||
return "打开制品";
|
||
default:
|
||
return action;
|
||
}
|
||
}
|
||
|
||
function bridgeResultLabel(status: string, result?: Record<string, string>): string {
|
||
if (status === "queued") {
|
||
return `已派发任务 ${result?.jobId ?? ""}`.trim();
|
||
}
|
||
if (result?.recommendation) {
|
||
return "AI 建议已返回";
|
||
}
|
||
if (parsePluginArtifactReference(result)) {
|
||
return "制品引用已返回";
|
||
}
|
||
return result?.serverInstanceId ? `服务器上下文 ${result.serverInstanceId} 已返回` : "桥接请求已完成";
|
||
}
|
||
|
||
interface LlmSectionProps {
|
||
serverId: string;
|
||
instance: ServerInstanceResponse;
|
||
session: PageComponentProps["session"];
|
||
operations: PageComponentProps["operations"];
|
||
}
|
||
|
||
function LlmSection({ serverId, instance, session, operations }: LlmSectionProps) {
|
||
const [prompt, setPrompt] = useState("");
|
||
const [currentConfig, setCurrentConfig] = useState<string>(fallbackConfig);
|
||
const [suggestion, setSuggestion] = useState<LlmSuggestionView | null>(null);
|
||
const [confirming, setConfirming] = useState(false);
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
useEffect(() => {
|
||
let cancelled = false;
|
||
void platformApiClient
|
||
.getServerConfig(serverId)
|
||
.then((response) => {
|
||
if (!cancelled) {
|
||
setCurrentConfig(response.content);
|
||
}
|
||
})
|
||
.catch(() => {
|
||
// keep the local fallback config
|
||
});
|
||
return () => {
|
||
cancelled = true;
|
||
};
|
||
}, [serverId]);
|
||
|
||
async function requestSuggestion(event: FormEvent<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
if (!prompt.trim()) {
|
||
return;
|
||
}
|
||
setBusy(true);
|
||
setSuggestion(null);
|
||
try {
|
||
const response = await platformApiClient.suggestServerConfig({ serverInstanceId: serverId, prompt: prompt.trim(), currentConfig });
|
||
const preview = response.suggestedConfig
|
||
? await platformApiClient.previewServerConfigDiff(serverId, {
|
||
expectedConfigVersion: instance.configVersion,
|
||
key: defaultConfigKey,
|
||
proposedContent: response.suggestedConfig
|
||
})
|
||
: undefined;
|
||
setSuggestion({
|
||
serverInstanceId: serverId,
|
||
source: "api",
|
||
recommendation: response.recommendation,
|
||
diff: preview ? configDiffViewFromPreview(preview) : undefined
|
||
});
|
||
} catch {
|
||
setSuggestion(buildLocalSuggestion(serverId, prompt.trim(), currentConfig));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function applySuggestion() {
|
||
if (!suggestion?.diff) {
|
||
return;
|
||
}
|
||
const operationId = operations.begin({ intent: "应用 AI 配置建议", targetKind: "llm", targetId: serverId, requester: session.displayName });
|
||
try {
|
||
const dispatch = await platformApiClient.approveServerConfigWrite(serverId, {
|
||
expectedConfigVersion: suggestion.diff.configVersion ?? instance.configVersion,
|
||
key: suggestion.diff.key ?? defaultConfigKey,
|
||
proposedContent: suggestion.diff.nextContent,
|
||
proposedContentInputRef: suggestion.diff.proposedContentInputRef,
|
||
idempotencyKey: `web:config.write.llm:${serverId}:${Date.now()}`
|
||
});
|
||
const job = dispatch.job;
|
||
operations.succeed(operationId, `AI 建议已确认,写入任务 ${job.id} 已派发`, job);
|
||
setSuggestion(null);
|
||
setConfirming(false);
|
||
} catch (error) {
|
||
operations.fail(operationId, error instanceof Error ? error.message : "写入任务派发失败", operationId);
|
||
setConfirming(false);
|
||
}
|
||
}
|
||
|
||
const llmOperation = operations.operations.find((operation) => operation.intent === "应用 AI 配置建议" && operation.targetId === serverId);
|
||
|
||
return (
|
||
<article className="console-panel" aria-label="llm configuration assistance">
|
||
<div className="panel-header">
|
||
<h2>
|
||
<Sparkles size={16} style={{ verticalAlign: "-2px" }} /> AI 配置助手
|
||
</h2>
|
||
<span className="page-status">建议仅作用于 {serverId}</span>
|
||
</div>
|
||
<p style={{ margin: "0 0 12px", color: "var(--ink-soft)", fontSize: 13.5 }}>
|
||
AI 建议会先生成推荐说明和配置差异,<strong>不会自动写入</strong>。只有你确认差异后,平台才会派发写入任务。前端不会接触任何 AI 提供商密钥。
|
||
</p>
|
||
{llmOperation && (
|
||
<div style={{ marginBottom: 10 }}>
|
||
<ResultBadge
|
||
status={llmOperation.status}
|
||
label={
|
||
llmOperation.status === "pending"
|
||
? "写入中…"
|
||
: llmOperation.status === "succeeded"
|
||
? (llmOperation.message ?? "已派发")
|
||
: `失败:${llmOperation.errorReason}(诊断 ${llmOperation.diagnosticId})`
|
||
}
|
||
/>
|
||
</div>
|
||
)}
|
||
<form className="provider-form" style={{ border: 0, padding: 0 }} onSubmit={(event) => void requestSuggestion(event)}>
|
||
<label>
|
||
想让 AI 帮你调整什么?
|
||
<textarea
|
||
value={prompt}
|
||
rows={3}
|
||
placeholder="例如:把最大玩家数提高到 40,并关闭 PVP"
|
||
onChange={(event) => setPrompt(event.target.value)}
|
||
/>
|
||
</label>
|
||
<button type="submit" className="primary-command" disabled={busy || !prompt.trim()}>
|
||
{busy ? "生成建议中…" : "生成建议"}
|
||
</button>
|
||
</form>
|
||
|
||
{suggestion && (
|
||
<div style={{ display: "grid", gap: 12, marginTop: 14 }}>
|
||
<div className="panel-header" style={{ marginBottom: 0 }}>
|
||
<h3>AI 建议</h3>
|
||
<span className="page-status">{suggestion.source === "api" ? "平台 LLM" : "本地建议(LLM 接口未提供)"}</span>
|
||
</div>
|
||
<p style={{ margin: 0, color: "var(--ink-soft)", fontSize: 14 }}>{suggestion.recommendation}</p>
|
||
{suggestion.diff ? (
|
||
<>
|
||
<DiffView lines={suggestion.diff.lines} />
|
||
<div className="confirm-actions">
|
||
<button type="button" onClick={() => setSuggestion(null)}>
|
||
放弃建议
|
||
</button>
|
||
<button type="button" className="confirm-primary" onClick={() => setConfirming(true)}>
|
||
确认差异并写入
|
||
</button>
|
||
</div>
|
||
</>
|
||
) : (
|
||
<span className="provider-id">该建议没有生成可应用的配置差异,仅供参考。</span>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
<ConfirmDialog
|
||
open={confirming}
|
||
title="确认应用 AI 配置建议"
|
||
description={`即将向服务器 ${instance.name}(${serverId})派发配置写入任务。写入内容以上方差异为准。`}
|
||
confirmLabel="确认写入"
|
||
busy={llmOperation?.status === "pending"}
|
||
onCancel={() => setConfirming(false)}
|
||
onConfirm={() => void applySuggestion()}
|
||
/>
|
||
</article>
|
||
);
|
||
}
|
||
|
||
function buildLocalSuggestion(serverId: string, prompt: string, currentConfig: string): LlmSuggestionView {
|
||
const lines = currentConfig.split("\n");
|
||
const next = [...lines];
|
||
const changed: string[] = [];
|
||
const playerMatch = prompt.match(/(\d+)\s*(?:人|名玩家|players?)/i) ?? prompt.match(/玩家[^\d]*(\d+)/);
|
||
if (playerMatch) {
|
||
const index = next.findIndex((line) => line.startsWith("max-players="));
|
||
if (index >= 0) {
|
||
next[index] = `max-players=${playerMatch[1]}`;
|
||
changed.push(`max-players 调整为 ${playerMatch[1]}`);
|
||
}
|
||
}
|
||
if (/关闭\s*pvp|禁用\s*pvp|pvp.*(off|false|关)/i.test(prompt)) {
|
||
const index = next.findIndex((line) => line.startsWith("pvp="));
|
||
if (index >= 0) {
|
||
next[index] = "pvp=false";
|
||
changed.push("pvp 关闭");
|
||
}
|
||
}
|
||
if (/开启\s*pvp|pvp.*(on|true|开)/i.test(prompt)) {
|
||
const index = next.findIndex((line) => line.startsWith("pvp="));
|
||
if (index >= 0) {
|
||
next[index] = "pvp=true";
|
||
changed.push("pvp 开启");
|
||
}
|
||
}
|
||
const nextContent = next.join("\n");
|
||
if (changed.length === 0) {
|
||
return {
|
||
serverInstanceId: serverId,
|
||
source: "local",
|
||
recommendation: `暂时无法为“${prompt}”生成配置差异。平台 LLM 建议接口尚未提供;本地建议引擎只支持常见字段(如 max-players、pvp)。`
|
||
};
|
||
}
|
||
return {
|
||
serverInstanceId: serverId,
|
||
source: "local",
|
||
recommendation: `根据请求“${prompt}”,建议:${changed.join(";")}。请确认差异后再写入。`,
|
||
diff: buildConfigDiff(serverId, currentConfig, nextContent)
|
||
};
|
||
}
|
||
|
||
export function configDiffViewFromPreview(preview: ServerConfigDiffPreviewResponse): ConfigDiffView {
|
||
const lines = preview.diff.map(configDiffLineFromPreviewLine);
|
||
const added = lines.filter((line) => line.kind === "added").length;
|
||
const removed = lines.filter((line) => line.kind === "removed").length;
|
||
return {
|
||
serverInstanceId: preview.serverInstanceId,
|
||
configVersion: preview.configVersion,
|
||
key: preview.key,
|
||
source: preview.source,
|
||
summary: `+${added} / -${removed} 行变更`,
|
||
lines,
|
||
nextContent: preview.proposedContent ?? "",
|
||
proposedContentInputRef: preview.proposedContentInputRef
|
||
};
|
||
}
|
||
|
||
function configDiffLineFromPreviewLine(line: ConfigDiffLineResponse): ConfigDiffView["lines"][number] {
|
||
return {
|
||
kind: line.kind === "context" ? "same" : line.kind,
|
||
text: line.content
|
||
};
|
||
}
|
||
|
||
interface HistorySectionProps {
|
||
serverId: string;
|
||
serverOperations: PageComponentProps["operations"]["operations"];
|
||
jobs: JobResponse[];
|
||
artifacts: ArtifactResponse[];
|
||
}
|
||
|
||
function HistorySection({ serverId, serverOperations, jobs, artifacts }: HistorySectionProps) {
|
||
return (
|
||
<div className="overview-two-col" aria-label="operation history">
|
||
<article className="console-panel">
|
||
<div className="panel-header">
|
||
<h2>本次会话操作</h2>
|
||
</div>
|
||
{serverOperations.length === 0 ? (
|
||
<EmptyState title="暂无操作记录" description="在本页发起启动、停止、插件控制或配置写入后,这里会显示完整的操作生命周期。" />
|
||
) : (
|
||
<div className="operation-list">
|
||
{serverOperations.map((operation) => (
|
||
<div key={operation.id} className="operation-item">
|
||
<div className="operation-item-head">
|
||
<strong>{operation.intent}</strong>
|
||
<ResultBadge
|
||
status={operation.status}
|
||
label={operation.status === "pending" ? "进行中" : operation.status === "succeeded" ? "成功" : "失败"}
|
||
/>
|
||
</div>
|
||
<div className="operation-meta">
|
||
<span>
|
||
操作 <code>{operation.id}</code>
|
||
</span>
|
||
{operation.jobId && (
|
||
<span>
|
||
任务 <code>{operation.jobId}</code>
|
||
{operation.jobState ? `(${operation.jobState})` : ""}
|
||
</span>
|
||
)}
|
||
<span>目标 {operation.targetId}</span>
|
||
<span>发起人 {operation.requester}</span>
|
||
<span>{new Date(operation.updatedAt).toLocaleString()}</span>
|
||
</div>
|
||
{operation.message && <span className="provider-id">{operation.message}</span>}
|
||
{operation.status === "failed" && operation.errorReason && (
|
||
<span className="provider-id">
|
||
失败原因:{operation.errorReason}
|
||
{operation.diagnosticId ? `(诊断 ${operation.diagnosticId})` : ""}
|
||
</span>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</article>
|
||
<article className="console-panel">
|
||
<div className="panel-header">
|
||
<h2>平台任务记录</h2>
|
||
</div>
|
||
{jobs.length === 0 ? (
|
||
<EmptyState title="暂无任务" description="该服务器还没有平台侧任务记录。" />
|
||
) : (
|
||
<div className="operation-list">
|
||
{jobs.slice(0, 20).map((job) => (
|
||
<div key={job.id} className="operation-item">
|
||
<div className="operation-item-head">
|
||
<strong>{job.capability}</strong>
|
||
<span className={cx("status-pill", job.state === "succeeded" ? "status-active" : job.state === "failed" ? "status-error" : "status-disabled")}>
|
||
{job.state}
|
||
</span>
|
||
</div>
|
||
<div className="operation-meta">
|
||
<span>
|
||
任务 <code>{job.id}</code>
|
||
</span>
|
||
<span>进度 {job.progress.percent}%</span>
|
||
<span>{new Date(job.updatedAt).toLocaleString()}</span>
|
||
</div>
|
||
{job.progress.message && <span className="provider-id">{job.progress.message}</span>}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</article>
|
||
<ArtifactDownloadPanel serverId={serverId} artifacts={artifacts} />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
interface ArtifactDownloadPanelProps {
|
||
serverId: string;
|
||
artifacts: ArtifactResponse[];
|
||
}
|
||
|
||
function ArtifactDownloadPanel({ serverId, artifacts }: ArtifactDownloadPanelProps) {
|
||
const [activeId, setActiveId] = useState<string | null>(null);
|
||
const [result, setResult] = useState<Record<string, { status: "pending" | "succeeded" | "failed"; label: string; progress?: number }>>({});
|
||
|
||
async function downloadArtifact(artifact: ArtifactResponse) {
|
||
setActiveId(artifact.id);
|
||
setResult((current) => ({ ...current, [artifact.id]: { status: "pending", label: "正在打开制品", progress: 0 } }));
|
||
try {
|
||
const reference = await platformApiClient.openArtifactDownload(artifact.id);
|
||
const chunks: ArrayBuffer[] = [];
|
||
let offset = 0;
|
||
while (offset < reference.sizeBytes) {
|
||
const chunk = await platformApiClient.readArtifactContent(reference.artifactId, offset, reference.chunkSizeBytes);
|
||
chunks.push(chunk.payload);
|
||
offset += chunk.payload.byteLength;
|
||
const progress = Math.min(100, Math.round((offset / reference.sizeBytes) * 100));
|
||
setResult((current) => ({ ...current, [artifact.id]: { status: "pending", label: `传输 ${progress}%`, progress } }));
|
||
if (chunk.payload.byteLength === 0) {
|
||
break;
|
||
}
|
||
}
|
||
openArtifactBlob(reference, chunks);
|
||
setResult((current) => ({ ...current, [artifact.id]: { status: "succeeded", label: `已打开 ${safeArtifactFilename(reference.filename)}`, progress: 100 } }));
|
||
} catch (error) {
|
||
setResult((current) => ({ ...current, [artifact.id]: { status: "failed", label: safeArtifactError(error) } }));
|
||
} finally {
|
||
setActiveId(null);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<article className="console-panel">
|
||
<div className="panel-header">
|
||
<h2>
|
||
<PackageOpen size={16} />
|
||
浏览器制品传输
|
||
</h2>
|
||
</div>
|
||
{artifacts.length === 0 ? (
|
||
<EmptyState title="暂无可下载制品" description="该服务器当前没有已完成的可用制品。" />
|
||
) : (
|
||
<div className="operation-list">
|
||
{artifacts.slice(0, 12).map((artifact) => {
|
||
const itemResult = result[artifact.id];
|
||
return (
|
||
<div key={artifact.id} className="operation-item">
|
||
<div className="operation-item-head">
|
||
<strong>{artifact.id}</strong>
|
||
<ResultBadge status={itemResult?.status ?? "pending"} label={itemResult?.label ?? artifact.state} />
|
||
</div>
|
||
<div className="operation-meta">
|
||
<span>服务器 {serverId}</span>
|
||
<span>{formatBytes(artifact.sizeBytes)}</span>
|
||
<span>{artifact.checksum}</span>
|
||
</div>
|
||
{itemResult?.progress !== undefined && <UsageMeter label="传输" percent={itemResult.progress} />}
|
||
<div className="action-strip">
|
||
<button type="button" className="icon-command" disabled={activeId !== null} onClick={() => void downloadArtifact(artifact)}>
|
||
<Download size={14} />
|
||
<span>{activeId === artifact.id ? "传输中" : "打开"}</span>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</article>
|
||
);
|
||
}
|
||
|
||
function openArtifactBlob(reference: ArtifactDownloadReferenceResponse, chunks: ArrayBuffer[]) {
|
||
if (typeof document === "undefined" || typeof URL === "undefined") {
|
||
return;
|
||
}
|
||
const blob = new Blob(chunks, { type: reference.contentType });
|
||
const url = URL.createObjectURL(blob);
|
||
const anchor = document.createElement("a");
|
||
anchor.href = url;
|
||
anchor.download = safeArtifactFilename(reference.filename);
|
||
anchor.rel = "noopener";
|
||
document.body.append(anchor);
|
||
anchor.click();
|
||
anchor.remove();
|
||
URL.revokeObjectURL(url);
|
||
}
|
||
|
||
function safeArtifactFilename(filename: string): string {
|
||
const cleaned = filename.replace(/[\\/]/g, "").trim();
|
||
return cleaned || "artifact.bin";
|
||
}
|
||
|
||
function safeArtifactError(error: unknown): string {
|
||
const message = error instanceof Error ? error.message : "制品传输失败";
|
||
return message.replace(/\/Users\/[^\s]+/g, "[path]").replace(/Bearer\s+[^\s]+/gi, "[token]").replace(/sk-[A-Za-z0-9_-]+/g, "[secret]");
|
||
}
|
||
|
||
function formatBytes(value: number): string {
|
||
if (value < 1024) {
|
||
return `${value} B`;
|
||
}
|
||
if (value < 1024 * 1024) {
|
||
return `${(value / 1024).toFixed(1)} KiB`;
|
||
}
|
||
return `${(value / (1024 * 1024)).toFixed(1)} MiB`;
|
||
}
|