Files
GMW/services/frontend/src/shared/api/client.ts
T
MythEclipse 7108f6bb47 feat(system): implement graceful shutdown and expand websocket events
Improve system reliability and real-time capabilities by implementing a robust lifecycle management system and adding new broadcast events for attachments and voice recordings.

- Implement asynchronous graceful shutdown in backend to close HTTP, WebSocket, Redis, and database connections.
- Add new WebSocket broadcast events: `attachment_created`, `voice_recording_started`, `voice_recording_stopped`, `voice_recording_uploaded`, and `analysis_queue_status`.
- Refactor media status handling to use boolean `playing` state instead of string-based status.
- Centralize `PageResult` and `VoiceRecording` types to improve consistency between frontend and backend.
- Update frontend API client to include `listRecordings` and handle new WebSocket event types.
- Fix type mismatches in voice command handling and media status reporting.
2026-06-09 16:56:44 +07:00

279 lines
8.2 KiB
TypeScript

// ─── Shared HTTP client — all API endpoints in one file ──────────────────────
import type { MessageRecord, PageResult } from "@bete/shared";
const BE_API_URL = import.meta.env.VITE_BE_API_URL || "http://localhost:3001";
const BE_WS_URL = import.meta.env.VITE_BE_WS_URL || "ws://localhost:3001";
class ApiError extends Error {
code: string;
statusCode: number;
constructor(code: string, message: string, statusCode: number) {
super(message);
this.name = "ApiError";
this.code = code;
this.statusCode = statusCode;
}
}
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
const password = localStorage.getItem("admin-password");
const url = path.startsWith("http") ? path : `${BE_API_URL}${path}`;
const res = await fetch(url, {
headers: {
"Content-Type": "application/json",
...(password ? { "X-Admin-Password": password } : {}),
},
...init,
});
if (!res.ok) {
let message = res.statusText;
let code = "REQUEST_FAILED";
try {
const body = (await res.json()) as { error?: string; message?: string };
if (body.message) message = body.message;
if (body.error) code = body.error;
} catch {
// ignore parse errors
}
throw new ApiError(code, message, res.status);
}
return res.json() as Promise<T>;
}
export function getWebSocketURL(): string {
return BE_WS_URL;
}
export function getAPIURL(): string {
return BE_API_URL;
}
// ─── Types ───────────────────────────────────────────────────────────────────
export type { MessageRecord, PageResult };
export interface Guild {
id: string;
name: string;
icon: string | null;
}
export interface Channel {
id: string;
name: string;
type?: string;
parentId?: string | null;
}
export interface VoiceStatus {
connected: boolean;
activeGuildId: string | null;
activeChannelId: string | null;
activeChannelName: string | null;
}
export interface ActiveSpeaker {
id?: string;
userId?: string;
username: string;
avatar: string;
speaking: boolean;
}
export type MediaMode = "music" | "screen";
export interface MediaItem {
id?: string;
source: string;
title: string;
mode?: "music" | "screen";
durationMs?: number | null;
thumbnailUrl?: string | null;
}
export interface MediaState {
playing: boolean;
musicVolume: number;
current: MediaItem | null;
queue: MediaItem[];
}
export interface UIState {
selectedGuild?: string;
selectedVoiceGuild?: string;
selectedVoiceChannel?: string;
selectedTextGuild?: string;
selectedTextChannel?: string;
selectedAnalyticsGuild?: string;
selectedAnalyticsChannel?: string;
activeTab?: "live" | "messages";
isListening?: boolean;
isStreaming?: boolean;
}
export interface AppConfig {
monitorGuildId: string | null;
}
export interface ChatResponse {
response?: string;
}
export type DashboardTab = "live" | "messages";
// ─── Messages ────────────────────────────────────────────────────────────────
export function listMessages(params: {
guildId: string;
channelId?: string;
limit?: number;
cursor?: string;
}): Promise<PageResult<MessageRecord>> {
const sp = new URLSearchParams({
guildId: params.guildId,
limit: String(params.limit ?? 100),
...(params.channelId && { channelId: params.channelId }),
...(params.cursor && { cursor: params.cursor }),
});
return request<PageResult<MessageRecord>>(`/api/messages?${sp}`);
}
export function listReview(
params: URLSearchParams,
): Promise<PageResult<MessageRecord>> {
return request<PageResult<MessageRecord>>(`/api/review?${params}`);
}
export function reanalyzeMessage(id: string): Promise<void> {
return request<void>(`/api/messages/${id}/reanalyze`, { method: "POST" });
}
export function reanalyzeErrorBatch(opts: {
guildId?: string;
channelId?: string;
messageIds?: string[];
}): Promise<{ ok: boolean; count: number }> {
return request<{ ok: boolean; count: number }>(
"/api/messages/reanalyze-batch",
{ method: "POST", body: JSON.stringify(opts) },
);
}
// ─── Guilds / Config ─────────────────────────────────────────────────────────
export function getGuilds(): Promise<Guild[]> {
return request<Guild[]>("/api/guilds");
}
export function getAppConfig(): Promise<AppConfig> {
return request<AppConfig>("/api/config");
}
// ─── Voice ───────────────────────────────────────────────────────────────────
export function getVoiceChannels(guildId: string): Promise<Channel[]> {
return request<Channel[]>(`/api/guilds/${guildId}/voice-channels`);
}
export function getTextChannels(guildId: string): Promise<Channel[]> {
return request<Channel[]>(`/api/guilds/${guildId}/channels`);
}
export function getVoiceStatus(): Promise<VoiceStatus> {
return request<VoiceStatus>("/api/status");
}
export function connectVoice(
guildId: string,
channelId: string,
): Promise<VoiceStatus> {
return request<VoiceStatus>("/api/connect", {
method: "POST",
body: JSON.stringify({ guildId, channelId }),
});
}
export function disconnectVoice(): Promise<VoiceStatus> {
return request<VoiceStatus>("/api/disconnect", { method: "POST" });
}
// ─── Media ───────────────────────────────────────────────────────────────────
export function getMediaStatus(): Promise<MediaState> {
return request<MediaState>("/api/media/status");
}
export function queueMedia(
source: string,
mode: "music" | "screen",
): Promise<MediaState> {
return request<MediaState>("/api/media/queue", {
method: "POST",
body: JSON.stringify({ source, mode }),
});
}
export function skipMedia(): Promise<MediaState> {
return request<MediaState>("/api/media/skip", { method: "POST" });
}
export function stopMedia(): Promise<MediaState> {
return request<MediaState>("/api/media/stop", { method: "POST" });
}
export function setMediaVolume(volume: number): Promise<MediaState> {
return request<MediaState>("/api/media/volume", {
method: "POST",
body: JSON.stringify({ volume }),
});
}
// ─── Recordings ──────────────────────────────────────────────────────────────
export interface VoiceRecording {
id: string;
user_id: string;
username: string;
avatar_url: string | null;
guild_id: string | null;
channel_id: string | null;
channel_name: string | null;
filename: string;
size_bytes: number;
download_url: string | null;
upload_status: "pending" | "uploaded" | "failed";
upload_error: string | null;
created_at: number;
uploaded_at: number | null;
}
export function listRecordings(limit = 50): Promise<VoiceRecording[]> {
return request<VoiceRecording[]>(`/api/recordings?limit=${limit}`);
}
// ─── Auth ────────────────────────────────────────────────────────────────────
export function login(password: string): Promise<{ ok: boolean }> {
return request<{ ok: boolean }>("/api/auth/login", {
method: "POST",
body: JSON.stringify({ password }),
});
}
// ─── UI State ────────────────────────────────────────────────────────────────
export function getUIState(): Promise<UIState> {
return request<UIState>("/api/ui-state");
}
export function updateUIState(patch: Partial<UIState>): Promise<UIState> {
return request<UIState>("/api/ui-state", {
method: "POST",
body: JSON.stringify(patch),
});
}