refactor(shared): centralize configuration and types
Migrate configuration validation and core moderation types from individual services to the `@bete/shared` package to ensure consistency across the monorepo. - Move `AppConfig` and moderation-related interfaces to `packages/shared`. - Replace service-specific Zod schemas with the centralized shared configuration. - Refactor `services/backend` and `services/discord-gateway` to consume shared config and types. - Remove redundant type definitions and local configuration logic in services. - Update `packages/shared` exports to include new `config` and `moderation-types` modules. - Clean up unused files and deprecated utility functions in `packages/shared`.
This commit is contained in:
+3
-10
@@ -49,8 +49,8 @@ BACKLOG_SYNC_BATCH_SIZE=100
|
|||||||
# AI Analysis Configuration
|
# AI Analysis Configuration
|
||||||
AI_ANALYSIS_ENABLED=false
|
AI_ANALYSIS_ENABLED=false
|
||||||
AI_LLM_API_KEY=your_9router_key_here
|
AI_LLM_API_KEY=your_9router_key_here
|
||||||
AI_LLM_BASE_URL=https://9router.asepharyana.tech/v1
|
AI_LLM_BASE_URL=https://9router.asepharyana.my.id/v1
|
||||||
AI_LLM_MODEL=free
|
AI_LLM_MODEL=text
|
||||||
# Vision model for image/video moderation (falls back to AI_LLM_MODEL if unset)
|
# Vision model for image/video moderation (falls back to AI_LLM_MODEL if unset)
|
||||||
AI_LLM_VISION_MODEL=multimodal
|
AI_LLM_VISION_MODEL=multimodal
|
||||||
# Max concurrent LLM API calls (default: 5)
|
# Max concurrent LLM API calls (default: 5)
|
||||||
@@ -110,7 +110,7 @@ AUTO_DELETE_FLAGGED_ENABLED=true
|
|||||||
AUTO_DELETE_FLAGGED_DRY_RUN=true
|
AUTO_DELETE_FLAGGED_DRY_RUN=true
|
||||||
AUTO_DELETE_FLAGGED_DELAY_MS=0
|
AUTO_DELETE_FLAGGED_DELAY_MS=0
|
||||||
AUTO_DELETE_MIN_CONFIDENCE=0.50
|
AUTO_DELETE_MIN_CONFIDENCE=0.50
|
||||||
AUTO_DELETE_ALLOWED_SEVERITIES=critical,high,medium
|
AUTO_DELETE_ALLOWED_SEVERITIES=critical,high,medium,low
|
||||||
AUTO_DELETE_NOTIFY_USER=false
|
AUTO_DELETE_NOTIFY_USER=false
|
||||||
# Optional: comma-separated channel/user IDs to exclude
|
# Optional: comma-separated channel/user IDs to exclude
|
||||||
# AUTO_DELETE_EXCLUDED_CHANNEL_IDS=
|
# AUTO_DELETE_EXCLUDED_CHANNEL_IDS=
|
||||||
@@ -134,10 +134,3 @@ AUTO_MIGRATE_ON_STARTUP=true
|
|||||||
|
|
||||||
# Worker Pool Configuration
|
# Worker Pool Configuration
|
||||||
# PISCINA_MAX_THREADS=4
|
# PISCINA_MAX_THREADS=4
|
||||||
|
|
||||||
# Cache Model Versioning
|
|
||||||
# Bump this version when the vision/LLM model prompt changes significantly.
|
|
||||||
# Old cache entries with mismatched versions are automatically ignored, forcing fresh analysis.
|
|
||||||
# Format: "v<N>" or "v<N>-<date>-<description>"
|
|
||||||
# Example progression: v1 → v2-2026-06-02-terminal-fix → v3-2026-06-15-new-model
|
|
||||||
# CACHE_MODEL_VERSION=v2-2026-06-02
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./dist/index.js",
|
".": "./dist/index.js",
|
||||||
"./types": "./dist/types/index.js",
|
"./config": "./dist/config/index.js",
|
||||||
"./errors": "./dist/errors/index.js",
|
"./errors": "./dist/errors/index.js",
|
||||||
"./logger": "./dist/logger/index.js",
|
"./logger": "./dist/logger/index.js",
|
||||||
"./utils": "./dist/utils/index.js"
|
"./utils": "./dist/utils/index.js"
|
||||||
|
|||||||
@@ -0,0 +1,240 @@
|
|||||||
|
/**
|
||||||
|
* Unified configuration schema shared by all services.
|
||||||
|
*
|
||||||
|
* This is the single source of truth for all environment variables.
|
||||||
|
* Individual services re-export from here; they do NOT define their own schemas.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ConfigError } from "../errors/index.js";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const configSchema = z
|
||||||
|
.object({
|
||||||
|
// ── Discord ──────────────────────────────────────────────────────────
|
||||||
|
DISCORD_TOKEN: z
|
||||||
|
.string()
|
||||||
|
.min(1, "DISCORD_TOKEN is required")
|
||||||
|
.transform((value) => value.replace(/^("|')|(?:("|'))$/g, "")),
|
||||||
|
MONITOR_GUILD_ID: z.string().min(1).optional(),
|
||||||
|
|
||||||
|
// ── Legacy voice ─────────────────────────────────────────────────────
|
||||||
|
GUILD_ID: z.string().min(1).optional(),
|
||||||
|
VOICE_GUILD_ID: z.string().min(1).optional(),
|
||||||
|
VOICE_CHANNEL_ID: z.string().min(1).optional(),
|
||||||
|
|
||||||
|
// ── Text capture legacy ──────────────────────────────────────────────
|
||||||
|
TEXT_GUILD_ID: z.string().min(1).optional(),
|
||||||
|
TEXT_CHANNEL_ID: z.string().min(1).optional(),
|
||||||
|
|
||||||
|
// ── Recording ────────────────────────────────────────────────────────
|
||||||
|
RECORDINGS_DIR: z.string().default("./recordings"),
|
||||||
|
RECORDING_SEGMENT_MS: z.coerce.number().positive().default(5000),
|
||||||
|
|
||||||
|
// ── Decoder ──────────────────────────────────────────────────────────
|
||||||
|
DECODER_ROTATE_MS: z.coerce.number().positive().default(5000),
|
||||||
|
DECODER_COOLDOWN_MS: z.coerce.number().positive().default(30000),
|
||||||
|
|
||||||
|
// ── Audio ────────────────────────────────────────────────────────────
|
||||||
|
AUDIO_STREAM_SILENCE_DURATION_MS: z.coerce.number().positive().default(3000),
|
||||||
|
PACKET_FILTER_MIN_SIZE: z.coerce.number().positive().default(8),
|
||||||
|
OPUS_FRAME_SIZE: z.coerce.number().positive().default(960),
|
||||||
|
AUDIO_SAMPLE_RATE: z.coerce.number().positive().default(48000),
|
||||||
|
AUDIO_CHANNELS: z.coerce.number().positive().default(2),
|
||||||
|
AVATAR_SIZE: z.coerce.number().positive().default(64),
|
||||||
|
|
||||||
|
// ── Server ───────────────────────────────────────────────────────────
|
||||||
|
WEBSERVER_PORT: z.coerce.number().positive().default(3000),
|
||||||
|
NODE_ENV: z
|
||||||
|
.enum(["development", "production", "test"])
|
||||||
|
.default("development"),
|
||||||
|
LOG_LEVEL: z
|
||||||
|
.enum(["error", "warn", "info", "http", "verbose", "debug", "silly"])
|
||||||
|
.default("info"),
|
||||||
|
VERBOSE: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(false),
|
||||||
|
ADMIN_PASSWORD: z.string().default("admin123"),
|
||||||
|
|
||||||
|
// ── Database (PostgreSQL) ────────────────────────────────────────────
|
||||||
|
DATABASE_URL: z.string().optional(),
|
||||||
|
POSTGRES_HOST: z.string().default("localhost"),
|
||||||
|
POSTGRES_PORT: z.coerce.number().int().positive().default(5432),
|
||||||
|
POSTGRES_USER: z.string().optional(),
|
||||||
|
POSTGRES_PASSWORD: z.string().optional(),
|
||||||
|
POSTGRES_DB: z.string().optional(),
|
||||||
|
POSTGRES_POOL_MIN: z.coerce.number().int().positive().default(2),
|
||||||
|
POSTGRES_POOL_MAX: z.coerce.number().int().positive().default(10),
|
||||||
|
|
||||||
|
// ── Redis ────────────────────────────────────────────────────────────
|
||||||
|
REDIS_URL: z.string().default("redis://localhost:6379"),
|
||||||
|
|
||||||
|
// ── Connection ───────────────────────────────────────────────────────
|
||||||
|
VOICE_CONNECTION_TIMEOUT_MS: z.coerce.number().positive().default(15000),
|
||||||
|
RECONNECT_TIMEOUT_MS: z.coerce.number().positive().default(5000),
|
||||||
|
|
||||||
|
// ── Attachments ─────────────────────────────────────────────────────
|
||||||
|
TELE_UPLOAD_URL: z
|
||||||
|
.string()
|
||||||
|
.url()
|
||||||
|
.default("https://upload.asepharyana.my.id/api/upload"),
|
||||||
|
ATTACHMENT_UPLOAD_TIMEOUT_MS: z.coerce.number().positive().default(30000),
|
||||||
|
ATTACHMENT_MAX_SIZE_MB: z.coerce.number().positive().default(100),
|
||||||
|
ATTACHMENT_RETRY_ATTEMPTS: z.coerce.number().positive().default(3),
|
||||||
|
BACKLOG_SYNC_HOURS: z.coerce.number().positive().default(24),
|
||||||
|
BACKLOG_SYNC_BATCH_SIZE: z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.max(100)
|
||||||
|
.default(100),
|
||||||
|
|
||||||
|
// ── AI Analysis ─────────────────────────────────────────────────────
|
||||||
|
AI_ANALYSIS_ENABLED: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(false),
|
||||||
|
AI_LLM_API_KEY: z.string().optional(),
|
||||||
|
AI_LLM_BASE_URL: z
|
||||||
|
.string()
|
||||||
|
.url()
|
||||||
|
.default("https://9router.asepharyana.my.id/v1"),
|
||||||
|
AI_LLM_MODEL: z.string().default("text"),
|
||||||
|
AI_LLM_VISION_MODEL: z.string().optional(),
|
||||||
|
AI_LLM_MAX_CONCURRENT: z.coerce.number().int().positive().default(5),
|
||||||
|
AI_LLM_IMAGE_MAX_DIMENSION: z.coerce.number().int().positive().default(1024),
|
||||||
|
AI_LLM_TEXT_BATCH_SIZE: z.coerce.number().int().positive().default(20),
|
||||||
|
AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS: z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.default(60000),
|
||||||
|
|
||||||
|
// ── AI Analysis Timing ──────────────────────────────────────────────
|
||||||
|
AI_ANALYSIS_DEBOUNCE_MS: z.coerce.number().positive().default(500),
|
||||||
|
AI_ANALYSIS_RECOVERY_INTERVAL_MS: z.coerce.number().positive().default(15000),
|
||||||
|
AI_ANALYSIS_ERROR_COOLDOWN_MS: z.coerce.number().positive().default(30000),
|
||||||
|
|
||||||
|
// ── AI Analysis Batch ───────────────────────────────────────────────
|
||||||
|
AI_ANALYSIS_MAX_BATCH_SIZE: z.coerce.number().int().positive().default(200),
|
||||||
|
AI_ANALYSIS_MAX_CONTEXT_TOKENS: z.coerce.number().positive().default(8000),
|
||||||
|
AI_ANALYSIS_MAX_TARGET_TOKENS: z.coerce.number().positive().default(4000),
|
||||||
|
AI_ANALYSIS_CONTEXT_MESSAGE_LIMIT: z.coerce.number().int().positive().default(20),
|
||||||
|
AI_ANALYSIS_PROCESSING_TIMEOUT_MS: z.coerce
|
||||||
|
.number()
|
||||||
|
.positive()
|
||||||
|
.default(120000),
|
||||||
|
AI_ANALYSIS_INDIVIDUAL_MAX_CONCURRENT: z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.default(50),
|
||||||
|
AI_ANALYSIS_INDIVIDUAL_CB_THRESHOLD: z.coerce
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
|
.default(50),
|
||||||
|
PISCINA_MAX_THREADS: z.coerce.number().int().positive().optional(),
|
||||||
|
|
||||||
|
// ── OpenAI Moderation ───────────────────────────────────────────────
|
||||||
|
OPENAI_MODERATION_API_KEY: z.string().optional(),
|
||||||
|
OPENAI_MODERATION_BASE_URL: z
|
||||||
|
.string()
|
||||||
|
.url()
|
||||||
|
.default("https://api.openai.com/v1"),
|
||||||
|
OPENAI_MODERATION_MODEL: z.string().default("omni-moderation-latest"),
|
||||||
|
|
||||||
|
// ── Auto Delete ─────────────────────────────────────────────────────
|
||||||
|
AUTO_DELETE_FLAGGED_ENABLED: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(true),
|
||||||
|
AUTO_DELETE_FLAGGED_DRY_RUN: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(false),
|
||||||
|
AUTO_DELETE_FLAGGED_DELAY_MS: z.coerce.number().min(0).default(0),
|
||||||
|
AUTO_DELETE_MIN_CONFIDENCE: z.coerce.number().min(0).max(1).default(0.5),
|
||||||
|
AUTO_DELETE_ALLOWED_SEVERITIES: z.string().default("critical,high,medium,low"),
|
||||||
|
AUTO_DELETE_ALLOWED_CATEGORIES: z.string().default(""),
|
||||||
|
AUTO_DELETE_EXCLUDED_CHANNEL_IDS: z.string().default(""),
|
||||||
|
AUTO_DELETE_EXCLUDED_USER_IDS: z.string().default(""),
|
||||||
|
AUTO_DELETE_NOTIFY_USER: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(false),
|
||||||
|
AUTO_DELETE_LOG_CHANNEL_ID: z.string().default(""),
|
||||||
|
|
||||||
|
// ── Retention ───────────────────────────────────────────────────────
|
||||||
|
RETENTION_MESSAGES_DAYS: z.coerce.number().int().min(0).default(0),
|
||||||
|
RETENTION_ATTACHMENTS_DAYS: z.coerce.number().int().min(0).default(0),
|
||||||
|
RETENTION_VOICE_DAYS: z.coerce.number().int().min(0).default(0),
|
||||||
|
RETENTION_CLEANUP_INTERVAL_MS: z.coerce
|
||||||
|
.number()
|
||||||
|
.positive()
|
||||||
|
.default(24 * 60 * 60 * 1000),
|
||||||
|
RETENTION_DRY_RUN: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(true),
|
||||||
|
AUTO_MIGRATE_ON_STARTUP: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v === "true")
|
||||||
|
.default(true),
|
||||||
|
})
|
||||||
|
.superRefine((value, ctx) => {
|
||||||
|
if (!value.AI_ANALYSIS_ENABLED) {
|
||||||
|
// skip: AI analysis not enabled
|
||||||
|
} else if (!value.AI_LLM_API_KEY) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
path: ["AI_LLM_API_KEY"],
|
||||||
|
message: "AI_LLM_API_KEY is required when AI_ANALYSIS_ENABLED=true",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate database configuration
|
||||||
|
if (!value.DATABASE_URL && !value.POSTGRES_HOST) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
path: ["DATABASE_URL"],
|
||||||
|
message: "Either DATABASE_URL or POSTGRES_HOST must be provided",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AppConfig = z.infer<typeof configSchema> & {
|
||||||
|
EFFECTIVE_TEXT_GUILD_ID?: string;
|
||||||
|
EFFECTIVE_VOICE_GUILD_ID?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function loadConfig(
|
||||||
|
env: NodeJS.ProcessEnv = process.env,
|
||||||
|
): AppConfig {
|
||||||
|
try {
|
||||||
|
const parsed = configSchema.parse(env);
|
||||||
|
return {
|
||||||
|
...parsed,
|
||||||
|
EFFECTIVE_TEXT_GUILD_ID: parsed.MONITOR_GUILD_ID,
|
||||||
|
EFFECTIVE_VOICE_GUILD_ID: parsed.VOICE_GUILD_ID ?? parsed.GUILD_ID,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof z.ZodError) {
|
||||||
|
const messages = error.issues
|
||||||
|
.map((e) => `${e.path.join(".")}: ${e.message}`)
|
||||||
|
.join("\n");
|
||||||
|
throw new ConfigError(`Configuration validation failed:\n${messages}`);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Singleton config loaded from process.env at import time. */
|
||||||
|
export const config = loadConfig();
|
||||||
@@ -33,30 +33,6 @@ export class UnauthorizedError extends AppError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ForbiddenError extends AppError {
|
|
||||||
constructor(message = "Forbidden") {
|
|
||||||
super(message, "FORBIDDEN", 403);
|
|
||||||
this.name = "ForbiddenError";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ConflictError extends AppError {
|
|
||||||
constructor(message: string) {
|
|
||||||
super(message, "CONFLICT", 409);
|
|
||||||
this.name = "ConflictError";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class InternalServerError extends AppError {
|
|
||||||
constructor(
|
|
||||||
message = "Internal server error",
|
|
||||||
details?: Record<string, unknown>,
|
|
||||||
) {
|
|
||||||
super(message, "INTERNAL_SERVER_ERROR", 500, details);
|
|
||||||
this.name = "InternalServerError";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DatabaseError extends AppError {
|
export class DatabaseError extends AppError {
|
||||||
constructor(message: string, details?: Record<string, unknown>) {
|
constructor(message: string, details?: Record<string, unknown>) {
|
||||||
super(message, "DATABASE_ERROR", 500, details);
|
super(message, "DATABASE_ERROR", 500, details);
|
||||||
@@ -70,17 +46,3 @@ export class ConfigError extends AppError {
|
|||||||
this.name = "ConfigError";
|
this.name = "ConfigError";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DiscordError extends AppError {
|
|
||||||
constructor(message: string, details?: Record<string, unknown>) {
|
|
||||||
super(message, "DISCORD_ERROR", 500, details);
|
|
||||||
this.name = "DiscordError";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class TimeoutError extends AppError {
|
|
||||||
constructor(operation: string) {
|
|
||||||
super(`${operation} timed out`, "TIMEOUT", 504);
|
|
||||||
this.name = "TimeoutError";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export * from "./errors/index.js";
|
export * from "./errors/index.js";
|
||||||
export * from "./logger/index.js";
|
export * from "./logger/index.js";
|
||||||
export * from "./types/index.js";
|
|
||||||
export * from "./utils/index.js";
|
export * from "./utils/index.js";
|
||||||
|
export * from "./moderation-types.js";
|
||||||
|
export * from "./config/index.js";
|
||||||
|
|||||||
@@ -0,0 +1,220 @@
|
|||||||
|
// Shared moderation types for all services
|
||||||
|
// Source of truth — snake_case + number (matching PostgreSQL schema)
|
||||||
|
|
||||||
|
export type AIStatus =
|
||||||
|
| "pending"
|
||||||
|
| "processing"
|
||||||
|
| "clean"
|
||||||
|
| "warn"
|
||||||
|
| "flagged"
|
||||||
|
| "error";
|
||||||
|
export type AISeverity = "none" | "low" | "medium" | "high" | "critical";
|
||||||
|
export type AIRecommendedAction =
|
||||||
|
| "none"
|
||||||
|
| "monitor"
|
||||||
|
| "warn"
|
||||||
|
| "review"
|
||||||
|
| "delete"
|
||||||
|
| "escalate";
|
||||||
|
|
||||||
|
export interface BroadcasterClient {
|
||||||
|
messageCreated: (data: unknown) => void;
|
||||||
|
messageUpdated: (data: unknown) => void;
|
||||||
|
messageDeleted: (data: unknown) => void;
|
||||||
|
messageAnalyzed: (data: unknown) => void;
|
||||||
|
attachmentCreated: (data: unknown) => void;
|
||||||
|
attachmentUploaded: (data: unknown) => void;
|
||||||
|
voiceRecordingStarted: (data: unknown) => void;
|
||||||
|
voiceRecordingStopped: (data: unknown) => void;
|
||||||
|
voiceRecordingUploaded: (data: unknown) => void;
|
||||||
|
analysisQueueStatus: (data: unknown) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ModerationBroadcaster = BroadcasterClient;
|
||||||
|
|
||||||
|
export interface RoleMetadata {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
position: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserMetadata {
|
||||||
|
userId: string;
|
||||||
|
username: string;
|
||||||
|
tag: string;
|
||||||
|
displayName: string;
|
||||||
|
avatarUrl: string;
|
||||||
|
bot: boolean;
|
||||||
|
roles: RoleMetadata[];
|
||||||
|
highestRole: RoleMetadata | null;
|
||||||
|
joinedTimestamp: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageRecord {
|
||||||
|
id: string;
|
||||||
|
guild_id: string;
|
||||||
|
channel_id: string;
|
||||||
|
thread_id: string | null;
|
||||||
|
user_id: string;
|
||||||
|
username: string;
|
||||||
|
avatar_url: string | null;
|
||||||
|
content: string;
|
||||||
|
edited_content: string | null;
|
||||||
|
created_at: number;
|
||||||
|
edited_at: number | null;
|
||||||
|
deleted_at: number | null;
|
||||||
|
type: "text" | "edited" | "deleted";
|
||||||
|
metadata: string | null;
|
||||||
|
ai_status?: AIStatus | null;
|
||||||
|
ai_moderation_flags?: string | null;
|
||||||
|
ai_moderation_score?: number | null;
|
||||||
|
ai_analysis?: string | null;
|
||||||
|
ai_categories?: string | null;
|
||||||
|
ai_severity?: AISeverity | null;
|
||||||
|
ai_confidence?: number | null;
|
||||||
|
ai_recommended_action?: AIRecommendedAction | null;
|
||||||
|
ai_analyzed_at?: number | null;
|
||||||
|
ai_error?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AttachmentRecord {
|
||||||
|
id: string;
|
||||||
|
message_id: string;
|
||||||
|
guild_id: string;
|
||||||
|
channel_id: string;
|
||||||
|
thread_id: string | null;
|
||||||
|
user_id: string;
|
||||||
|
filename: string;
|
||||||
|
size: number;
|
||||||
|
type: string;
|
||||||
|
discord_url: string;
|
||||||
|
uploaded_url: string | null;
|
||||||
|
upload_status: "pending" | "uploaded" | "failed";
|
||||||
|
upload_error: string | null;
|
||||||
|
created_at: number;
|
||||||
|
uploaded_at: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface VoiceSegmentRecord {
|
||||||
|
id: string;
|
||||||
|
user_id: string;
|
||||||
|
session_id: string;
|
||||||
|
guild_id: string;
|
||||||
|
channel_id: string;
|
||||||
|
filename: string;
|
||||||
|
duration_ms: number;
|
||||||
|
created_at: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DashboardMessage {
|
||||||
|
id: string;
|
||||||
|
channel_id: string;
|
||||||
|
user_id: string;
|
||||||
|
username: string;
|
||||||
|
avatar_url: string | null;
|
||||||
|
content: string;
|
||||||
|
created_at: number;
|
||||||
|
type: "text" | "image" | "voice";
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageQuery {
|
||||||
|
guildId?: string;
|
||||||
|
channelId?: string;
|
||||||
|
threadId?: string;
|
||||||
|
status?: AIStatus[];
|
||||||
|
userId?: string;
|
||||||
|
q?: string;
|
||||||
|
cursor?: string;
|
||||||
|
limit: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PageResult<T> {
|
||||||
|
data: T[];
|
||||||
|
nextCursor: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AnalysisResult {
|
||||||
|
messageId: string;
|
||||||
|
status: Exclude<AIStatus, "pending">;
|
||||||
|
flags: string[];
|
||||||
|
score: number;
|
||||||
|
analysis: string;
|
||||||
|
categories?: string[];
|
||||||
|
severity?: AISeverity;
|
||||||
|
confidence?: number;
|
||||||
|
recommendedAction?: AIRecommendedAction;
|
||||||
|
policyVersion?: string;
|
||||||
|
evidence?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface VoiceRecordingUploadData {
|
||||||
|
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;
|
||||||
|
upload_status: string;
|
||||||
|
created_at: number;
|
||||||
|
uploaded_at: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AnalysisQueueStatus {
|
||||||
|
queuedConversations: number;
|
||||||
|
activeRequests: number;
|
||||||
|
activeIndividualRequests: number;
|
||||||
|
individualInFlightCount: number;
|
||||||
|
individualCircuitBreakerActive: boolean;
|
||||||
|
lastError: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ReviewStatus = "pending" | "approved" | "rejected" | "escalated";
|
||||||
|
|
||||||
|
export interface MessageReview {
|
||||||
|
id: string;
|
||||||
|
message_id: string;
|
||||||
|
guild_id: string;
|
||||||
|
channel_id: string;
|
||||||
|
reviewer_id: string | null;
|
||||||
|
status: ReviewStatus;
|
||||||
|
notes: string | null;
|
||||||
|
created_at: number;
|
||||||
|
reviewed_at: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ModerationActionType =
|
||||||
|
| "delete_message"
|
||||||
|
| "mute_user"
|
||||||
|
| "warn_user"
|
||||||
|
| "kick_user"
|
||||||
|
| "ban_user";
|
||||||
|
|
||||||
|
export interface ModerationAction {
|
||||||
|
id: string;
|
||||||
|
message_id: string | null;
|
||||||
|
user_id: string | null;
|
||||||
|
guild_id: string;
|
||||||
|
action_type: ModerationActionType;
|
||||||
|
reason: string | null;
|
||||||
|
executed_by: string | null;
|
||||||
|
status: "pending" | "executed" | "failed";
|
||||||
|
error: string | null;
|
||||||
|
created_at: number;
|
||||||
|
executed_at: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RetentionPolicy {
|
||||||
|
id: string;
|
||||||
|
guild_id: string;
|
||||||
|
channel_id: string | null;
|
||||||
|
retention_days: number;
|
||||||
|
apply_to_media: boolean;
|
||||||
|
apply_to_voice: boolean;
|
||||||
|
enabled: boolean;
|
||||||
|
created_at: number;
|
||||||
|
updated_at: number;
|
||||||
|
}
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
// Shared types for all services
|
|
||||||
export interface AppConfig {
|
|
||||||
NODE_ENV: "development" | "production" | "test";
|
|
||||||
LOG_LEVEL: string;
|
|
||||||
VERBOSE: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DatabaseConfig {
|
|
||||||
DATABASE_URL: string;
|
|
||||||
AUTO_MIGRATE_ON_STARTUP: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DiscordConfig {
|
|
||||||
DISCORD_TOKEN: string;
|
|
||||||
MONITOR_GUILD_ID: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AIConfig {
|
|
||||||
AI_LLM_API_KEY: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RedisConfig {
|
|
||||||
REDIS_URL: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WebServerConfig {
|
|
||||||
WEBSERVER_PORT: number;
|
|
||||||
ADMIN_PASSWORD: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MessageRecord {
|
|
||||||
id: string;
|
|
||||||
guildId: string;
|
|
||||||
channelId: string;
|
|
||||||
userId: string;
|
|
||||||
username: string;
|
|
||||||
content: string;
|
|
||||||
createdAt: Date;
|
|
||||||
editedAt?: Date;
|
|
||||||
deletedAt?: Date;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AttachmentRecord {
|
|
||||||
id: string;
|
|
||||||
messageId: string;
|
|
||||||
filename: string;
|
|
||||||
size: number;
|
|
||||||
mimeType: string;
|
|
||||||
discordUrl: string;
|
|
||||||
uploadedUrl?: string;
|
|
||||||
uploadStatus: "pending" | "uploaded" | "failed";
|
|
||||||
createdAt: Date;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface VoiceSegment {
|
|
||||||
userId: string;
|
|
||||||
sessionStart: number;
|
|
||||||
segmentIndex: number;
|
|
||||||
duration: number;
|
|
||||||
filePath: string;
|
|
||||||
createdAt: Date;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AnalyticsData {
|
|
||||||
totalMessages: number;
|
|
||||||
totalAttachments: number;
|
|
||||||
totalVoiceSegments: number;
|
|
||||||
activeUsers: number;
|
|
||||||
lastUpdated: Date;
|
|
||||||
}
|
|
||||||
@@ -4,82 +4,24 @@ export function delay(ms: number): Promise<void> {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatBytes(bytes: number): string {
|
|
||||||
if (bytes === 0) return "0 Bytes";
|
|
||||||
const k = 1024;
|
|
||||||
const sizes = ["Bytes", "KB", "MB", "GB"];
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
||||||
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + " " + sizes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateId(): string {
|
|
||||||
return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isValidUrl(url: string): boolean {
|
|
||||||
try {
|
|
||||||
new URL(url);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sanitizeString(str: string): string {
|
|
||||||
return str.replace(/[<>]/g, "").trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PaginationParams {
|
|
||||||
page: number;
|
|
||||||
limit: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PaginatedResponse<T> {
|
|
||||||
data: T[];
|
|
||||||
total: number;
|
|
||||||
page: number;
|
|
||||||
limit: number;
|
|
||||||
pages: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function calculatePagination(
|
|
||||||
total: number,
|
|
||||||
page: number,
|
|
||||||
limit: number,
|
|
||||||
): PaginatedResponse<never> {
|
|
||||||
return {
|
|
||||||
data: [],
|
|
||||||
total,
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
pages: Math.ceil(total / limit),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getOffset(page: number, limit: number): number {
|
|
||||||
return (page - 1) * limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Retry with exponential backoff (port of discord-gateway retry utility)
|
// Retry with exponential backoff
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
export interface RetryOptions {
|
|
||||||
/** Number of retry attempts (default: 3) */
|
|
||||||
retries?: number;
|
|
||||||
/** Initial delay in ms (default: 1000) */
|
|
||||||
minTimeout?: number;
|
|
||||||
/** Maximum delay in ms (default: 30000) */
|
|
||||||
maxTimeout?: number;
|
|
||||||
/** Multiplication factor for each retry (default: 2) */
|
|
||||||
factor?: number;
|
|
||||||
/** Optional AbortSignal to cancel retries */
|
|
||||||
signal?: AbortSignal;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function retryWithBackoff<T>(
|
export async function retryWithBackoff<T>(
|
||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
options: RetryOptions = {},
|
options: {
|
||||||
|
/** Number of retry attempts (default: 3) */
|
||||||
|
retries?: number;
|
||||||
|
/** Initial delay in ms (default: 1000) */
|
||||||
|
minTimeout?: number;
|
||||||
|
/** Maximum delay in ms (default: 30000) */
|
||||||
|
maxTimeout?: number;
|
||||||
|
/** Multiplication factor for each retry (default: 2) */
|
||||||
|
factor?: number;
|
||||||
|
/** Optional AbortSignal to cancel retries */
|
||||||
|
signal?: AbortSignal;
|
||||||
|
} = {},
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const {
|
const {
|
||||||
retries = 3,
|
retries = 3,
|
||||||
@@ -131,114 +73,3 @@ export async function retryWithBackoff<T>(
|
|||||||
}
|
}
|
||||||
throw lastError!;
|
throw lastError!;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Generic in-memory TTL cache with LRU-style pruning
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
interface CacheEntry<V> {
|
|
||||||
value: V;
|
|
||||||
expiresAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TtlCacheOptions<K> {
|
|
||||||
/** Default TTL in ms for entries (default: 60000) */
|
|
||||||
defaultTtlMs?: number;
|
|
||||||
/** Maximum entries before pruning (default: 500) */
|
|
||||||
maxEntries?: number;
|
|
||||||
/** Called when an entry is evicted */
|
|
||||||
onEvict?: (key: K, value: unknown) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class TtlCache<K = string, V = unknown> {
|
|
||||||
private store = new Map<K, CacheEntry<V>>();
|
|
||||||
private readonly defaultTtlMs: number;
|
|
||||||
private readonly maxEntries: number;
|
|
||||||
private readonly onEvict?: (key: K, value: V) => void;
|
|
||||||
|
|
||||||
constructor(options: TtlCacheOptions<K> = {}) {
|
|
||||||
this.defaultTtlMs = options.defaultTtlMs ?? 60_000;
|
|
||||||
this.maxEntries = options.maxEntries ?? 500;
|
|
||||||
this.onEvict = options.onEvict;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a value by key. Returns undefined if missing or expired.
|
|
||||||
*/
|
|
||||||
get(key: K): V | undefined {
|
|
||||||
const entry = this.store.get(key);
|
|
||||||
if (!entry) return undefined;
|
|
||||||
if (Date.now() > entry.expiresAt) {
|
|
||||||
this.store.delete(key);
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return entry.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a value with optional custom TTL. Prunes oldest entries if at capacity.
|
|
||||||
*/
|
|
||||||
set(key: K, value: V, ttlMs?: number): void {
|
|
||||||
if (this.store.size >= this.maxEntries) {
|
|
||||||
this.prune();
|
|
||||||
}
|
|
||||||
this.store.set(key, {
|
|
||||||
value,
|
|
||||||
expiresAt: Date.now() + (ttlMs ?? this.defaultTtlMs),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a key exists and is not expired (without removing it).
|
|
||||||
*/
|
|
||||||
has(key: K): boolean {
|
|
||||||
return this.get(key) !== undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a specific entry.
|
|
||||||
*/
|
|
||||||
delete(key: K): boolean {
|
|
||||||
return this.store.delete(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all expired entries.
|
|
||||||
*/
|
|
||||||
prune(): void {
|
|
||||||
const now = Date.now();
|
|
||||||
const toDelete: K[] = [];
|
|
||||||
for (const [key, entry] of this.store) {
|
|
||||||
if (now > entry.expiresAt) {
|
|
||||||
toDelete.push(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const key of toDelete) {
|
|
||||||
const entry = this.store.get(key);
|
|
||||||
this.store.delete(key);
|
|
||||||
if (entry && this.onEvict) this.onEvict(key, entry.value);
|
|
||||||
}
|
|
||||||
// If still over limit after TTL pruning, drop oldest entries
|
|
||||||
if (this.store.size > this.maxEntries) {
|
|
||||||
const keysToDelete = Array.from(this.store.keys()).slice(
|
|
||||||
0,
|
|
||||||
this.store.size - this.maxEntries,
|
|
||||||
);
|
|
||||||
for (const key of keysToDelete) {
|
|
||||||
const entry = this.store.get(key);
|
|
||||||
this.store.delete(key);
|
|
||||||
if (entry && this.onEvict) this.onEvict(key, entry.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Current number of entries (including possibly expired ones). */
|
|
||||||
get size(): number {
|
|
||||||
return this.store.size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Remove all entries. */
|
|
||||||
clear(): void {
|
|
||||||
this.store.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Generated
-13
@@ -76,9 +76,6 @@ importers:
|
|||||||
pino:
|
pino:
|
||||||
specifier: ^9.6.0
|
specifier: ^9.6.0
|
||||||
version: 9.14.0
|
version: 9.14.0
|
||||||
pino-http:
|
|
||||||
specifier: ^10.3.0
|
|
||||||
version: 10.5.0
|
|
||||||
prom-client:
|
prom-client:
|
||||||
specifier: ^15.1.3
|
specifier: ^15.1.3
|
||||||
version: 15.1.3
|
version: 15.1.3
|
||||||
@@ -4268,9 +4265,6 @@ packages:
|
|||||||
pino-abstract-transport@3.0.0:
|
pino-abstract-transport@3.0.0:
|
||||||
resolution: {integrity: sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg==}
|
resolution: {integrity: sha512-wlfUczU+n7Hy/Ha5j9a/gZNy7We5+cXp8YL+X+PG8S0KXxw7n/JXA3c46Y0zQznIJ83URJiwy7Lh56WLokNuxg==}
|
||||||
|
|
||||||
pino-http@10.5.0:
|
|
||||||
resolution: {integrity: sha512-hD91XjgaKkSsdn8P7LaebrNzhGTdB086W3pyPihX0EzGPjq5uBJBXo4N5guqNaK6mUjg9aubMF7wDViYek9dRA==}
|
|
||||||
|
|
||||||
pino-pretty@13.1.3:
|
pino-pretty@13.1.3:
|
||||||
resolution: {integrity: sha512-ttXRkkOz6WWC95KeY9+xxWL6AtImwbyMHrL1mSwqwW9u+vLp/WIElvHvCSDg0xO/Dzrggz1zv3rN5ovTRVowKg==}
|
resolution: {integrity: sha512-ttXRkkOz6WWC95KeY9+xxWL6AtImwbyMHrL1mSwqwW9u+vLp/WIElvHvCSDg0xO/Dzrggz1zv3rN5ovTRVowKg==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
@@ -8948,13 +8942,6 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
split2: 4.2.0
|
split2: 4.2.0
|
||||||
|
|
||||||
pino-http@10.5.0:
|
|
||||||
dependencies:
|
|
||||||
get-caller-file: 2.0.5
|
|
||||||
pino: 9.14.0
|
|
||||||
pino-std-serializers: 7.1.0
|
|
||||||
process-warning: 5.0.0
|
|
||||||
|
|
||||||
pino-pretty@13.1.3:
|
pino-pretty@13.1.3:
|
||||||
dependencies:
|
dependencies:
|
||||||
colorette: 2.0.20
|
colorette: 2.0.20
|
||||||
|
|||||||
@@ -24,7 +24,6 @@
|
|||||||
"ioredis": "^5.11.0",
|
"ioredis": "^5.11.0",
|
||||||
"pg": "^8.21.0",
|
"pg": "^8.21.0",
|
||||||
"pino": "^9.6.0",
|
"pino": "^9.6.0",
|
||||||
"pino-http": "^10.3.0",
|
|
||||||
"prom-client": "^15.1.3",
|
"prom-client": "^15.1.3",
|
||||||
"ws": "^8.20.1",
|
"ws": "^8.20.1",
|
||||||
"zod": "^4.4.3"
|
"zod": "^4.4.3"
|
||||||
|
|||||||
@@ -1,117 +1,5 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import { ConfigError } from "@bete/shared/errors";
|
import { config as sharedConfig } from "@bete/shared/config";
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
const configSchema = z
|
export const config = sharedConfig;
|
||||||
.object({
|
|
||||||
// Server
|
|
||||||
WEBSERVER_PORT: z.coerce.number().positive().default(3001),
|
|
||||||
NODE_ENV: z
|
|
||||||
.enum(["development", "production", "test"])
|
|
||||||
.default("development"),
|
|
||||||
LOG_LEVEL: z
|
|
||||||
.enum(["error", "warn", "info", "http", "verbose", "debug", "silly"])
|
|
||||||
.default("info"),
|
|
||||||
VERBOSE: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
|
|
||||||
// Database
|
|
||||||
DATABASE_URL: z.string().url().optional(),
|
|
||||||
DATABASE_HOST: z.string().default("localhost"),
|
|
||||||
DATABASE_PORT: z.coerce.number().default(5432),
|
|
||||||
DATABASE_NAME: z.string().default("discord_moderation"),
|
|
||||||
DATABASE_USER: z.string().default("postgres"),
|
|
||||||
DATABASE_PASSWORD: z.string().optional(),
|
|
||||||
|
|
||||||
// Redis (optional, for pub/sub)
|
|
||||||
REDIS_URL: z.string().url().optional(),
|
|
||||||
REDIS_HOST: z.string().default("localhost"),
|
|
||||||
REDIS_PORT: z.coerce.number().default(6379),
|
|
||||||
|
|
||||||
// Discord
|
|
||||||
MONITOR_GUILD_ID: z.string().min(1).optional(),
|
|
||||||
|
|
||||||
// Admin
|
|
||||||
ADMIN_PASSWORD: z.string().optional(),
|
|
||||||
|
|
||||||
// Analytics
|
|
||||||
BACKLOG_SYNC_HOURS: z.coerce.number().positive().default(24),
|
|
||||||
BACKLOG_SYNC_BATCH_SIZE: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.max(100)
|
|
||||||
.default(100),
|
|
||||||
|
|
||||||
// AI Moderation
|
|
||||||
AI_ANALYSIS_ENABLED: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
OPENAI_MODERATION_API_KEY: z.string().optional(),
|
|
||||||
OPENAI_MODERATION_BASE_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://api.openai.com/v1"),
|
|
||||||
OPENAI_MODERATION_MODEL: z.string().default("omni-moderation-latest"),
|
|
||||||
AI_LLM_API_KEY: z.string().optional(),
|
|
||||||
AI_LLM_BASE_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://9router.asepharyana.my.id/v1"),
|
|
||||||
AI_LLM_MODEL: z.string().default("text"),
|
|
||||||
AI_LLM_VISION_MODEL: z.string().optional(),
|
|
||||||
AI_LLM_MAX_CONCURRENT: z.coerce.number().int().positive().default(5),
|
|
||||||
AI_LLM_IMAGE_MAX_DIMENSION: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(1024),
|
|
||||||
AI_LLM_TEXT_BATCH_SIZE: z.coerce.number().int().positive().default(20),
|
|
||||||
AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(60000),
|
|
||||||
|
|
||||||
// Attachments
|
|
||||||
ATTACHMENT_UPLOAD_TIMEOUT_MS: z.coerce.number().positive().default(30000),
|
|
||||||
ATTACHMENT_MAX_SIZE_MB: z.coerce.number().positive().default(100),
|
|
||||||
ATTACHMENT_RETRY_ATTEMPTS: z.coerce.number().positive().default(3),
|
|
||||||
TELE_UPLOAD_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://upload.asepharyana.my.id/api/upload"),
|
|
||||||
})
|
|
||||||
.superRefine((value, ctx) => {
|
|
||||||
if (!value.DATABASE_URL && !value.DATABASE_HOST) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: z.ZodIssueCode.custom,
|
|
||||||
path: ["DATABASE_URL"],
|
|
||||||
message: "Either DATABASE_URL or DATABASE_HOST must be provided",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export function loadConfig(
|
|
||||||
env: NodeJS.ProcessEnv = process.env,
|
|
||||||
): z.infer<typeof configSchema> {
|
|
||||||
try {
|
|
||||||
return configSchema.parse(env);
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof z.ZodError) {
|
|
||||||
const messages = error.issues
|
|
||||||
.map((e) => `${e.path.join(".")}: ${e.message}`)
|
|
||||||
.join("\n");
|
|
||||||
throw new ConfigError(`Configuration validation failed:\n${messages}`);
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const config = loadConfig();
|
|
||||||
export type Config = typeof config;
|
export type Config = typeof config;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export async function initializeDatabase() {
|
|||||||
|
|
||||||
const databaseUrl =
|
const databaseUrl =
|
||||||
config.DATABASE_URL ||
|
config.DATABASE_URL ||
|
||||||
`postgresql://${config.DATABASE_USER}${config.DATABASE_PASSWORD ? `:${config.DATABASE_PASSWORD}` : ""}@${config.DATABASE_HOST}:${config.DATABASE_PORT}/${config.DATABASE_NAME}`;
|
`postgresql://${config.POSTGRES_USER}${config.POSTGRES_PASSWORD ? `:${config.POSTGRES_PASSWORD}` : ""}@${config.POSTGRES_HOST}:${config.POSTGRES_PORT}/${config.POSTGRES_DB}`;
|
||||||
|
|
||||||
pool = new Pool({
|
pool = new Pool({
|
||||||
connectionString: databaseUrl,
|
connectionString: databaseUrl,
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ export function requireParam(
|
|||||||
name: string,
|
name: string,
|
||||||
): string {
|
): string {
|
||||||
if (typeof value !== "string" || value.length === 0) {
|
if (typeof value !== "string" || value.length === 0) {
|
||||||
throw new Error(`Missing ${kind}: ${name}`);
|
throw new ValidationError(`Missing ${kind}: ${name}`);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,18 +31,11 @@ let publisherClient: Redis | null = null;
|
|||||||
let subscriberClient: Redis | null = null;
|
let subscriberClient: Redis | null = null;
|
||||||
|
|
||||||
function ensureRedisConfig(): boolean {
|
function ensureRedisConfig(): boolean {
|
||||||
return !!(config.REDIS_URL || config.REDIS_HOST);
|
return !!(config.REDIS_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createClient(): Redis {
|
function createClient(): Redis {
|
||||||
if (config.REDIS_URL) {
|
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
|
||||||
}
|
|
||||||
return new Redis({
|
|
||||||
host: config.REDIS_HOST,
|
|
||||||
port: config.REDIS_PORT,
|
|
||||||
keyPrefix: "",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Global broadcast functions for WebSocket events.
|
* Broadcast functions for WebSocket events.
|
||||||
*
|
*
|
||||||
* These are assigned by ws/server.ts when the WebSocket server initializes.
|
* These are injected by ws/server.ts when the WebSocket server initializes.
|
||||||
* Other modules call them to push real-time events to connected frontend clients.
|
* Other modules call them to push real-time events to connected frontend clients.
|
||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
@@ -13,38 +13,48 @@ type BroadcastFn = (data: unknown) => void;
|
|||||||
type BroadcastRawFn = (type: string, data: unknown) => void;
|
type BroadcastRawFn = (type: string, data: unknown) => void;
|
||||||
type BroadcastBinaryFn = (data: Buffer) => void;
|
type BroadcastBinaryFn = (data: Buffer) => void;
|
||||||
|
|
||||||
declare global {
|
export interface BroadcastFunctions {
|
||||||
// biome-ignore lint/suspicious/noAssignInExpressions: intentional global broadcast registry
|
messageCreated: BroadcastFn;
|
||||||
var __broadcastFns:
|
messageUpdated: BroadcastFn;
|
||||||
| {
|
messageDeleted: BroadcastFn;
|
||||||
messageCreated: BroadcastFn;
|
attachmentUploaded: BroadcastFn;
|
||||||
messageUpdated: BroadcastFn;
|
raw: BroadcastRawFn;
|
||||||
messageDeleted: BroadcastFn;
|
binary: BroadcastBinaryFn;
|
||||||
attachmentUploaded: BroadcastFn;
|
|
||||||
raw: BroadcastRawFn;
|
|
||||||
binary: BroadcastBinaryFn;
|
|
||||||
}
|
|
||||||
| undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const noop: BroadcastFn = () => {};
|
const noop: BroadcastFn = () => {};
|
||||||
const noopRaw: BroadcastRawFn = () => {};
|
const noopRaw: BroadcastRawFn = () => {};
|
||||||
const noopBinary: BroadcastBinaryFn = () => {};
|
const noopBinary: BroadcastBinaryFn = () => {};
|
||||||
|
|
||||||
|
let _fns: BroadcastFunctions | null = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inject broadcast functions from the WebSocket server initializer.
|
||||||
|
* Must be called once during server startup before any broadcast is used.
|
||||||
|
*/
|
||||||
|
export function setBroadcastFunctions(fns: BroadcastFunctions): void {
|
||||||
|
_fns = fns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clear injected functions (used during cleanup). */
|
||||||
|
export function clearBroadcastFunctions(): void {
|
||||||
|
_fns = null;
|
||||||
|
}
|
||||||
|
|
||||||
export const broadcastMessageCreated: BroadcastFn = (data) =>
|
export const broadcastMessageCreated: BroadcastFn = (data) =>
|
||||||
(globalThis.__broadcastFns?.messageCreated ?? noop)(data);
|
(_fns?.messageCreated ?? noop)(data);
|
||||||
|
|
||||||
export const broadcastMessageUpdated: BroadcastFn = (data) =>
|
export const broadcastMessageUpdated: BroadcastFn = (data) =>
|
||||||
(globalThis.__broadcastFns?.messageUpdated ?? noop)(data);
|
(_fns?.messageUpdated ?? noop)(data);
|
||||||
|
|
||||||
export const broadcastMessageDeleted: BroadcastFn = (data) =>
|
export const broadcastMessageDeleted: BroadcastFn = (data) =>
|
||||||
(globalThis.__broadcastFns?.messageDeleted ?? noop)(data);
|
(_fns?.messageDeleted ?? noop)(data);
|
||||||
|
|
||||||
export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
|
export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
|
||||||
(globalThis.__broadcastFns?.attachmentUploaded ?? noop)(data);
|
(_fns?.attachmentUploaded ?? noop)(data);
|
||||||
|
|
||||||
export const broadcastRaw: BroadcastRawFn = (type, data) =>
|
export const broadcastRaw: BroadcastRawFn = (type, data) =>
|
||||||
(globalThis.__broadcastFns?.raw ?? noopRaw)(type, data);
|
(_fns?.raw ?? noopRaw)(type, data);
|
||||||
|
|
||||||
export const broadcastBinary: BroadcastBinaryFn = (data) =>
|
export const broadcastBinary: BroadcastBinaryFn = (data) =>
|
||||||
(globalThis.__broadcastFns?.binary ?? noopBinary)(data);
|
(_fns?.binary ?? noopBinary)(data);
|
||||||
|
|||||||
@@ -32,14 +32,7 @@ const SUBSCRIPTIONS: ChannelMapping[] = [
|
|||||||
let subscriber: Redis | null = null;
|
let subscriber: Redis | null = null;
|
||||||
|
|
||||||
function createSubscriber(): Redis {
|
function createSubscriber(): Redis {
|
||||||
if (config.REDIS_URL) {
|
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
|
||||||
}
|
|
||||||
return new Redis({
|
|
||||||
host: config.REDIS_HOST,
|
|
||||||
port: config.REDIS_PORT,
|
|
||||||
keyPrefix: "",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,7 +86,7 @@ function handleSubscriptionMessage(channel: string, message: string): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function startRedisBridge(): Promise<void> {
|
export async function startRedisBridge(): Promise<void> {
|
||||||
if (!config.REDIS_URL && !config.REDIS_HOST) {
|
if (!config.REDIS_URL) {
|
||||||
logger.info("Redis not configured, skipping Redis bridge");
|
logger.info("Redis not configured, skipping Redis bridge");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { Server } from "node:http";
|
import type { Server } from "node:http";
|
||||||
import { createChildLogger } from "@bete/shared/logger";
|
import { createChildLogger } from "@bete/shared/logger";
|
||||||
import { WebSocket, WebSocketServer } from "ws";
|
import { WebSocket, WebSocketServer } from "ws";
|
||||||
|
import { setBroadcastFunctions } from "./broadcast.js";
|
||||||
|
|
||||||
const logger = createChildLogger("ws.server");
|
const logger = createChildLogger("ws.server");
|
||||||
|
|
||||||
@@ -149,7 +150,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
|||||||
// Don't let the interval keep the process alive after wss closes
|
// Don't let the interval keep the process alive after wss closes
|
||||||
heartbeatInterval.unref();
|
heartbeatInterval.unref();
|
||||||
|
|
||||||
// Expose broadcast functions on globalThis
|
// Defines broadcast functions and injects them via setBroadcastFunctions
|
||||||
function broadcast(event: Omit<BroadcastEvent, "timestamp">) {
|
function broadcast(event: Omit<BroadcastEvent, "timestamp">) {
|
||||||
const payload = JSON.stringify({
|
const payload = JSON.stringify({
|
||||||
...event,
|
...event,
|
||||||
@@ -166,7 +167,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function broadcastRaw(data: Buffer) {
|
function broadcastBinary(data: Buffer) {
|
||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
if (client.readyState === WebSocket.OPEN) {
|
if (client.readyState === WebSocket.OPEN) {
|
||||||
try {
|
try {
|
||||||
@@ -178,7 +179,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
globalThis.__broadcastFns = {
|
setBroadcastFunctions({
|
||||||
messageCreated: (data: unknown) =>
|
messageCreated: (data: unknown) =>
|
||||||
broadcast({ type: "message_created", data }),
|
broadcast({ type: "message_created", data }),
|
||||||
messageUpdated: (data: unknown) =>
|
messageUpdated: (data: unknown) =>
|
||||||
@@ -188,13 +189,12 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
|||||||
attachmentUploaded: (data: unknown) =>
|
attachmentUploaded: (data: unknown) =>
|
||||||
broadcast({ type: "attachment_uploaded", data }),
|
broadcast({ type: "attachment_uploaded", data }),
|
||||||
raw: (type: string, data: unknown) => broadcast({ type, data }),
|
raw: (type: string, data: unknown) => broadcast({ type, data }),
|
||||||
binary: broadcastRaw,
|
binary: broadcastBinary,
|
||||||
};
|
});
|
||||||
|
|
||||||
// Cleanup on close
|
// Cleanup on close
|
||||||
wss.on("close", () => {
|
wss.on("close", () => {
|
||||||
clearInterval(heartbeatInterval);
|
clearInterval(heartbeatInterval);
|
||||||
globalThis.__broadcastFns = undefined;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info({ path: "/ws" }, "WebSocket server created");
|
logger.info({ path: "/ws" }, "WebSocket server created");
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
// Mock CRC for discord.js compatibility
|
|
||||||
export {};
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
var crc32: ((data: Buffer) => number) | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!globalThis.crc32) {
|
|
||||||
globalThis.crc32 = (data: Buffer) => {
|
|
||||||
let crc = 0 ^ -1;
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
|
||||||
crc = (crc >>> 8) ^ ((crc ^ data[i]) & 0xff);
|
|
||||||
}
|
|
||||||
return (crc ^ -1) >>> 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createChildLogger } from "@bete/shared/logger";
|
import { createChildLogger } from "@bete/shared/logger";
|
||||||
import { config } from "../../shared/config/config.js";
|
import { config } from "../../shared/config/config.js";
|
||||||
import { executeAll, executeGet } from "../../shared/database/drizzle.js";
|
import { executeAll, executeGet } from "../../shared/database/drizzle.js";
|
||||||
import { uploadToTele } from "../attachment-upload/teleUpload.js";
|
import { uploadToTele } from "../voice-recording/teleUpload.js";
|
||||||
|
|
||||||
const logger = createChildLogger("sticker-cache");
|
const logger = createChildLogger("sticker-cache");
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
updateAttachmentAsUploaded,
|
updateAttachmentAsUploaded,
|
||||||
updateAttachmentDiscordUrl,
|
updateAttachmentDiscordUrl,
|
||||||
} from "../message-capture/messageStore.js";
|
} from "../message-capture/messageStore.js";
|
||||||
import { uploadToTele } from "./teleUpload.js";
|
import { uploadToTele } from "../voice-recording/teleUpload.js";
|
||||||
|
|
||||||
const logger = createChildLogger("attachment-uploader");
|
const logger = createChildLogger("attachment-uploader");
|
||||||
|
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
import { retryWithBackoff } from "@bete/shared/utils";
|
|
||||||
|
|
||||||
export interface TeleUploadResponse {
|
|
||||||
download_url: string;
|
|
||||||
public_id?: string;
|
|
||||||
file_name?: string;
|
|
||||||
size_bytes?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TeleUploadResult {
|
|
||||||
url: string;
|
|
||||||
publicId?: string;
|
|
||||||
filename?: string;
|
|
||||||
sizeBytes?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseTeleUploadResponse(
|
|
||||||
response: TeleUploadResponse,
|
|
||||||
): TeleUploadResult {
|
|
||||||
if (!response.download_url) {
|
|
||||||
throw new Error("Missing download_url in response");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
url: response.download_url,
|
|
||||||
publicId: response.public_id,
|
|
||||||
filename: response.file_name,
|
|
||||||
sizeBytes: response.size_bytes,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function uploadToTele(input: {
|
|
||||||
buffer: Buffer;
|
|
||||||
filename: string;
|
|
||||||
contentType: string;
|
|
||||||
uploadUrl: string;
|
|
||||||
timeoutMs?: number;
|
|
||||||
retries: number;
|
|
||||||
}): Promise<TeleUploadResult> {
|
|
||||||
const { buffer, filename, contentType, uploadUrl, timeoutMs, retries } =
|
|
||||||
input;
|
|
||||||
|
|
||||||
const response = await retryWithBackoff(
|
|
||||||
async () => {
|
|
||||||
const fileBlob = new Blob([new Uint8Array(buffer)], {
|
|
||||||
type: contentType,
|
|
||||||
});
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append("file", fileBlob, filename);
|
|
||||||
formData.append("fileName", filename);
|
|
||||||
|
|
||||||
const res = await fetch(uploadUrl, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
accept: "application/json",
|
|
||||||
},
|
|
||||||
body: formData,
|
|
||||||
...(timeoutMs ? { signal: AbortSignal.timeout(timeoutMs) } : {}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(`Upload failed: Status ${res.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await res.json()) as TeleUploadResponse;
|
|
||||||
},
|
|
||||||
{
|
|
||||||
retries,
|
|
||||||
minTimeout: 0,
|
|
||||||
maxTimeout: 0,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return parseTeleUploadResponse(response);
|
|
||||||
}
|
|
||||||
@@ -1,55 +1,45 @@
|
|||||||
import type fs from "node:fs";
|
import type fs from "node:fs";
|
||||||
import type prism from "prism-media";
|
import type prism from "prism-media";
|
||||||
|
import type {
|
||||||
|
AIStatus,
|
||||||
|
AISeverity,
|
||||||
|
AIRecommendedAction,
|
||||||
|
BroadcasterClient,
|
||||||
|
ModerationBroadcaster,
|
||||||
|
RoleMetadata,
|
||||||
|
UserMetadata,
|
||||||
|
MessageRecord,
|
||||||
|
AttachmentRecord,
|
||||||
|
VoiceRecordingUploadData,
|
||||||
|
AnalysisQueueStatus,
|
||||||
|
} from "@bete/shared";
|
||||||
|
|
||||||
export type AIStatus =
|
// Re-export all shared types for backward compatibility
|
||||||
| "pending"
|
export type {
|
||||||
| "processing"
|
AIStatus,
|
||||||
| "clean"
|
AISeverity,
|
||||||
| "warn"
|
AIRecommendedAction,
|
||||||
| "flagged"
|
BroadcasterClient,
|
||||||
| "error";
|
ModerationBroadcaster,
|
||||||
export type AISeverity = "none" | "low" | "medium" | "high" | "critical";
|
RoleMetadata,
|
||||||
export type AIRecommendedAction =
|
UserMetadata,
|
||||||
| "none"
|
MessageRecord,
|
||||||
| "monitor"
|
AttachmentRecord,
|
||||||
| "warn"
|
VoiceSegmentRecord,
|
||||||
| "review"
|
DashboardMessage,
|
||||||
| "delete"
|
MessageQuery,
|
||||||
| "escalate";
|
PageResult,
|
||||||
|
AnalysisResult,
|
||||||
export interface BroadcasterClient {
|
VoiceRecordingUploadData,
|
||||||
messageCreated: (data: unknown) => void;
|
AnalysisQueueStatus,
|
||||||
messageUpdated: (data: unknown) => void;
|
MessageReview,
|
||||||
messageDeleted: (data: unknown) => void;
|
ModerationAction,
|
||||||
messageAnalyzed: (data: unknown) => void;
|
RetentionPolicy,
|
||||||
attachmentCreated: (data: unknown) => void;
|
ReviewStatus,
|
||||||
attachmentUploaded: (data: unknown) => void;
|
ModerationActionType,
|
||||||
voiceRecordingStarted: (data: unknown) => void;
|
} from "@bete/shared";
|
||||||
voiceRecordingStopped: (data: unknown) => void;
|
|
||||||
voiceRecordingUploaded: (data: unknown) => void;
|
|
||||||
analysisQueueStatus: (data: unknown) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ModerationBroadcaster = BroadcasterClient;
|
|
||||||
|
|
||||||
export interface RoleMetadata {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
position: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UserMetadata {
|
|
||||||
userId: string;
|
|
||||||
username: string;
|
|
||||||
tag: string;
|
|
||||||
displayName: string;
|
|
||||||
avatarUrl: string;
|
|
||||||
bot: boolean;
|
|
||||||
roles: RoleMetadata[];
|
|
||||||
highestRole: RoleMetadata | null;
|
|
||||||
joinedTimestamp: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Types that are LOCAL ONLY (not in shared) — keep here
|
||||||
export interface SegmentState {
|
export interface SegmentState {
|
||||||
index: number;
|
index: number;
|
||||||
startTime: number;
|
startTime: number;
|
||||||
@@ -80,119 +70,7 @@ export interface PcmBroadcaster {
|
|||||||
) => void;
|
) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MessageRecord {
|
// Local-only types (not shared across services)
|
||||||
id: string;
|
|
||||||
guild_id: string;
|
|
||||||
channel_id: string;
|
|
||||||
thread_id: string | null;
|
|
||||||
user_id: string;
|
|
||||||
username: string;
|
|
||||||
avatar_url: string | null;
|
|
||||||
content: string;
|
|
||||||
edited_content: string | null;
|
|
||||||
created_at: number;
|
|
||||||
edited_at: number | null;
|
|
||||||
deleted_at: number | null;
|
|
||||||
type: "text" | "edited" | "deleted";
|
|
||||||
metadata: string | null;
|
|
||||||
ai_status?: AIStatus | null;
|
|
||||||
ai_moderation_flags?: string | null;
|
|
||||||
ai_moderation_score?: number | null;
|
|
||||||
ai_analysis?: string | null;
|
|
||||||
ai_categories?: string | null;
|
|
||||||
ai_severity?: AISeverity | null;
|
|
||||||
ai_confidence?: number | null;
|
|
||||||
ai_recommended_action?: AIRecommendedAction | null;
|
|
||||||
ai_analyzed_at?: number | null;
|
|
||||||
ai_error?: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AttachmentRecord {
|
|
||||||
id: string;
|
|
||||||
message_id: string;
|
|
||||||
guild_id: string;
|
|
||||||
channel_id: string;
|
|
||||||
thread_id: string | null;
|
|
||||||
user_id: string;
|
|
||||||
filename: string;
|
|
||||||
size: number;
|
|
||||||
type: string;
|
|
||||||
discord_url: string;
|
|
||||||
uploaded_url: string | null;
|
|
||||||
upload_status: "pending" | "uploaded" | "failed";
|
|
||||||
upload_error: string | null;
|
|
||||||
created_at: number;
|
|
||||||
uploaded_at: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface VoiceSegmentRecord {
|
|
||||||
id: string;
|
|
||||||
user_id: string;
|
|
||||||
session_id: string;
|
|
||||||
guild_id: string;
|
|
||||||
channel_id: string;
|
|
||||||
filename: string;
|
|
||||||
duration_ms: number;
|
|
||||||
created_at: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DashboardMessage {
|
|
||||||
id: string;
|
|
||||||
channel_id: string;
|
|
||||||
user_id: string;
|
|
||||||
username: string;
|
|
||||||
avatar_url: string | null;
|
|
||||||
content: string;
|
|
||||||
created_at: number;
|
|
||||||
type: "text" | "image" | "voice";
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MessageQuery {
|
|
||||||
guildId?: string;
|
|
||||||
channelId?: string;
|
|
||||||
threadId?: string;
|
|
||||||
status?: AIStatus[];
|
|
||||||
userId?: string;
|
|
||||||
q?: string;
|
|
||||||
cursor?: string;
|
|
||||||
limit: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PageResult<T> {
|
|
||||||
data: T[];
|
|
||||||
nextCursor: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AnalysisResult {
|
|
||||||
messageId: string;
|
|
||||||
status: Exclude<AIStatus, "pending">;
|
|
||||||
flags: string[];
|
|
||||||
score: number;
|
|
||||||
analysis: string;
|
|
||||||
categories?: string[];
|
|
||||||
severity?: AISeverity;
|
|
||||||
confidence?: number;
|
|
||||||
recommendedAction?: AIRecommendedAction;
|
|
||||||
policyVersion?: string;
|
|
||||||
evidence?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface VoiceRecordingUploadData {
|
|
||||||
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;
|
|
||||||
upload_status: string;
|
|
||||||
created_at: number;
|
|
||||||
uploaded_at: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ModerationWsEvent =
|
export type ModerationWsEvent =
|
||||||
| { type: "ui_state"; state: unknown }
|
| { type: "ui_state"; state: unknown }
|
||||||
| { type: "user_state"; users: unknown[] }
|
| { type: "user_state"; users: unknown[] }
|
||||||
@@ -204,59 +82,3 @@ export type ModerationWsEvent =
|
|||||||
| { type: "analysis_queue_status"; data: AnalysisQueueStatus }
|
| { type: "analysis_queue_status"; data: AnalysisQueueStatus }
|
||||||
| { type: "media_state"; state: unknown }
|
| { type: "media_state"; state: unknown }
|
||||||
| { type: "voice_recording_uploaded"; data: VoiceRecordingUploadData };
|
| { type: "voice_recording_uploaded"; data: VoiceRecordingUploadData };
|
||||||
|
|
||||||
export interface AnalysisQueueStatus {
|
|
||||||
queuedConversations: number;
|
|
||||||
activeRequests: number;
|
|
||||||
activeIndividualRequests: number;
|
|
||||||
individualInFlightCount: number;
|
|
||||||
individualCircuitBreakerActive: boolean;
|
|
||||||
lastError: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ReviewStatus = "pending" | "approved" | "rejected" | "escalated";
|
|
||||||
|
|
||||||
export interface MessageReview {
|
|
||||||
id: string;
|
|
||||||
message_id: string;
|
|
||||||
guild_id: string;
|
|
||||||
channel_id: string;
|
|
||||||
reviewer_id: string | null;
|
|
||||||
status: ReviewStatus;
|
|
||||||
notes: string | null;
|
|
||||||
created_at: number;
|
|
||||||
reviewed_at: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ModerationActionType =
|
|
||||||
| "delete_message"
|
|
||||||
| "mute_user"
|
|
||||||
| "warn_user"
|
|
||||||
| "kick_user"
|
|
||||||
| "ban_user";
|
|
||||||
|
|
||||||
export interface ModerationAction {
|
|
||||||
id: string;
|
|
||||||
message_id: string | null;
|
|
||||||
user_id: string | null;
|
|
||||||
guild_id: string;
|
|
||||||
action_type: ModerationActionType;
|
|
||||||
reason: string | null;
|
|
||||||
executed_by: string | null;
|
|
||||||
status: "pending" | "executed" | "failed";
|
|
||||||
error: string | null;
|
|
||||||
created_at: number;
|
|
||||||
executed_at: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RetentionPolicy {
|
|
||||||
id: string;
|
|
||||||
guild_id: string;
|
|
||||||
channel_id: string | null;
|
|
||||||
retention_days: number;
|
|
||||||
apply_to_media: boolean;
|
|
||||||
apply_to_voice: boolean;
|
|
||||||
enabled: boolean;
|
|
||||||
created_at: number;
|
|
||||||
updated_at: number;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
export { buildMuxFfmpegArgs, runFfmpeg } from "./ffmpegProcess.js";
|
|
||||||
@@ -1,236 +1,20 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import { ConfigError } from "@bete/shared/errors";
|
import type { AppConfig as SharedAppConfig } from "@bete/shared/config";
|
||||||
import { z } from "zod";
|
import { config as sharedConfig, loadConfig as sharedLoadConfig } from "@bete/shared/config";
|
||||||
|
|
||||||
const configSchema = z
|
// Re-export the unified config with EFFECTIVE_* fields added
|
||||||
.object({
|
export type AppConfig = SharedAppConfig & {
|
||||||
DISCORD_TOKEN: z
|
|
||||||
.string()
|
|
||||||
.min(1, "DISCORD_TOKEN is required")
|
|
||||||
.transform((value) => value.replace(/^("|')|(?:("|'))$/g, "")),
|
|
||||||
VOICE_CHANNEL_ID: z.string().min(1).optional(),
|
|
||||||
GUILD_ID: z.string().min(1).optional(),
|
|
||||||
TEXT_GUILD_ID: z.string().min(1).optional(),
|
|
||||||
TEXT_CHANNEL_ID: z.string().min(1).optional(),
|
|
||||||
VOICE_GUILD_ID: z.string().min(1).optional(),
|
|
||||||
VERBOSE: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
RECORDINGS_DIR: z.string().default("./recordings"),
|
|
||||||
RECORDING_SEGMENT_MS: z.coerce.number().positive().default(5000),
|
|
||||||
DECODER_ROTATE_MS: z.coerce.number().positive().default(5000),
|
|
||||||
DECODER_COOLDOWN_MS: z.coerce.number().positive().default(30000),
|
|
||||||
WEBSERVER_PORT: z.coerce.number().positive().default(3000),
|
|
||||||
VOICE_CONNECTION_TIMEOUT_MS: z.coerce.number().positive().default(15000),
|
|
||||||
RECONNECT_TIMEOUT_MS: z.coerce.number().positive().default(5000),
|
|
||||||
AUDIO_STREAM_SILENCE_DURATION_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.positive()
|
|
||||||
.default(3000),
|
|
||||||
PACKET_FILTER_MIN_SIZE: z.coerce.number().positive().default(8),
|
|
||||||
OPUS_FRAME_SIZE: z.coerce.number().positive().default(960),
|
|
||||||
AUDIO_SAMPLE_RATE: z.coerce.number().positive().default(48000),
|
|
||||||
AUDIO_CHANNELS: z.coerce.number().positive().default(2),
|
|
||||||
AVATAR_SIZE: z.coerce.number().positive().default(64),
|
|
||||||
LOG_LEVEL: z
|
|
||||||
.enum(["error", "warn", "info", "http", "verbose", "debug", "silly"])
|
|
||||||
.default("info"),
|
|
||||||
NODE_ENV: z
|
|
||||||
.enum(["development", "production", "test"])
|
|
||||||
.default("development"),
|
|
||||||
MONITOR_GUILD_ID: z.string().min(1).optional(),
|
|
||||||
TELE_UPLOAD_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://upload.asepharyana.my.id/api/upload"),
|
|
||||||
ATTACHMENT_UPLOAD_TIMEOUT_MS: z.coerce.number().positive().default(30000),
|
|
||||||
ATTACHMENT_MAX_SIZE_MB: z.coerce.number().positive().default(100),
|
|
||||||
ATTACHMENT_RETRY_ATTEMPTS: z.coerce.number().positive().default(3),
|
|
||||||
BACKLOG_SYNC_HOURS: z.coerce.number().positive().default(24),
|
|
||||||
BACKLOG_SYNC_BATCH_SIZE: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.max(100)
|
|
||||||
.default(100),
|
|
||||||
AI_ANALYSIS_ENABLED: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
OPENAI_MODERATION_API_KEY: z.string().optional(),
|
|
||||||
OPENAI_MODERATION_BASE_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://api.openai.com/v1"),
|
|
||||||
OPENAI_MODERATION_MODEL: z.string().default("omni-moderation-latest"),
|
|
||||||
AI_LLM_API_KEY: z.string().optional(),
|
|
||||||
AI_LLM_BASE_URL: z
|
|
||||||
.string()
|
|
||||||
.url()
|
|
||||||
.default("https://9router.asepharyana.my.id/v1"),
|
|
||||||
/** Model used for text-only moderation (messages, badword analysis). */
|
|
||||||
AI_LLM_MODEL: z.string().default("text"),
|
|
||||||
/** Model used for image/video moderation (vision-capable model). */
|
|
||||||
AI_LLM_VISION_MODEL: z.string().optional(),
|
|
||||||
/** Max concurrent LLM API calls (default: 5). */
|
|
||||||
AI_LLM_MAX_CONCURRENT: z.coerce.number().int().positive().default(5),
|
|
||||||
/** Maximum image dimension in pixels before resize for vision API (default: 1024). */
|
|
||||||
AI_LLM_IMAGE_MAX_DIMENSION: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(1024),
|
|
||||||
/** Maximum messages per text-only moderation batch (default: 20). */
|
|
||||||
AI_LLM_TEXT_BATCH_SIZE: z.coerce.number().int().positive().default(20),
|
|
||||||
/** Timeout in ms for individual media analysis calls (default: 60000). */
|
|
||||||
AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(60000),
|
|
||||||
AI_ANALYSIS_DEBOUNCE_MS: z.coerce.number().positive().default(500),
|
|
||||||
AI_ANALYSIS_RECOVERY_INTERVAL_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.positive()
|
|
||||||
.default(15000),
|
|
||||||
AI_ANALYSIS_ERROR_COOLDOWN_MS: z.coerce.number().positive().default(30000),
|
|
||||||
/** Max messages fetched per conversation batch (token budget is the real constraint). */
|
|
||||||
AI_ANALYSIS_MAX_BATCH_SIZE: z.coerce.number().int().positive().default(200),
|
|
||||||
AI_ANALYSIS_MAX_CONTEXT_TOKENS: z.coerce.number().positive().default(8000),
|
|
||||||
/** Token budget for target messages specifically (separate from context window). */
|
|
||||||
AI_ANALYSIS_MAX_TARGET_TOKENS: z.coerce.number().positive().default(4000),
|
|
||||||
AI_ANALYSIS_CONTEXT_MESSAGE_LIMIT: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(20),
|
|
||||||
/**
|
|
||||||
* How long a conversation is considered locked while being processed.
|
|
||||||
* Must exceed (LLM timeout × max retries) + network overhead.
|
|
||||||
* LLM client timeout=30s, retries=3 → minimum safe value ≈ 100s.
|
|
||||||
*/
|
|
||||||
AI_ANALYSIS_PROCESSING_TIMEOUT_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.positive()
|
|
||||||
.default(120000),
|
|
||||||
/** Max concurrent individual-fallback jobs admitted by the main event loop. */
|
|
||||||
AI_ANALYSIS_INDIVIDUAL_MAX_CONCURRENT: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(50),
|
|
||||||
/**
|
|
||||||
* How many consecutive individual-fallback errors trigger the individual
|
|
||||||
* circuit breaker (separate from the batch circuit breaker).
|
|
||||||
*/
|
|
||||||
AI_ANALYSIS_INDIVIDUAL_CB_THRESHOLD: z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.positive()
|
|
||||||
.default(50),
|
|
||||||
/** Max Piscina worker threads for batch AI analysis (default: os.availableParallelism). */
|
|
||||||
PISCINA_MAX_THREADS: z.coerce.number().int().positive().optional(),
|
|
||||||
// AI moderation uses the Primary LLM (AI_LLM_*) endpoint only.
|
|
||||||
// No NVIDIA or Groq fallback.
|
|
||||||
AUTO_DELETE_FLAGGED_ENABLED: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(true),
|
|
||||||
AUTO_DELETE_FLAGGED_DELAY_MS: z.coerce.number().min(0).default(0),
|
|
||||||
AUTO_DELETE_FLAGGED_DRY_RUN: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
AUTO_DELETE_MIN_CONFIDENCE: z.coerce.number().min(0).max(1).default(0.5),
|
|
||||||
AUTO_DELETE_ALLOWED_SEVERITIES: z
|
|
||||||
.string()
|
|
||||||
.default("critical,high,medium,low"),
|
|
||||||
AUTO_DELETE_ALLOWED_CATEGORIES: z.string().default(""),
|
|
||||||
AUTO_DELETE_EXCLUDED_CHANNEL_IDS: z.string().default(""),
|
|
||||||
AUTO_DELETE_EXCLUDED_USER_IDS: z.string().default(""),
|
|
||||||
AUTO_DELETE_NOTIFY_USER: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(false),
|
|
||||||
AUTO_DELETE_LOG_CHANNEL_ID: z.string().default(""),
|
|
||||||
RETENTION_MESSAGES_DAYS: z.coerce.number().int().min(0).default(0),
|
|
||||||
RETENTION_ATTACHMENTS_DAYS: z.coerce.number().int().min(0).default(0),
|
|
||||||
RETENTION_VOICE_DAYS: z.coerce.number().int().min(0).default(0),
|
|
||||||
RETENTION_CLEANUP_INTERVAL_MS: z.coerce
|
|
||||||
.number()
|
|
||||||
.positive()
|
|
||||||
.default(24 * 60 * 60 * 1000),
|
|
||||||
RETENTION_DRY_RUN: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(true),
|
|
||||||
AUTO_MIGRATE_ON_STARTUP: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.transform((v) => v === "true")
|
|
||||||
.default(true),
|
|
||||||
DATABASE_URL: z.string().optional(),
|
|
||||||
POSTGRES_HOST: z.string().default("localhost"),
|
|
||||||
POSTGRES_PORT: z.coerce.number().int().positive().default(5432),
|
|
||||||
POSTGRES_USER: z.string().optional(),
|
|
||||||
POSTGRES_PASSWORD: z.string().optional(),
|
|
||||||
POSTGRES_DB: z.string().optional(),
|
|
||||||
POSTGRES_POOL_MIN: z.coerce.number().int().positive().default(2),
|
|
||||||
POSTGRES_POOL_MAX: z.coerce.number().int().positive().default(10),
|
|
||||||
ADMIN_PASSWORD: z.string().default("admin123"),
|
|
||||||
REDIS_URL: z.string().min(1).default("redis://localhost:6379"),
|
|
||||||
})
|
|
||||||
.superRefine((value, ctx) => {
|
|
||||||
if (!value.AI_ANALYSIS_ENABLED) {
|
|
||||||
// Continue to database validation
|
|
||||||
} else if (!value.AI_LLM_API_KEY) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: z.ZodIssueCode.custom,
|
|
||||||
path: ["AI_LLM_API_KEY"],
|
|
||||||
message: "AI_LLM_API_KEY is required when AI_ANALYSIS_ENABLED=true",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate PostgreSQL configuration
|
|
||||||
if (!value.DATABASE_URL && !value.POSTGRES_HOST) {
|
|
||||||
ctx.addIssue({
|
|
||||||
code: z.ZodIssueCode.custom,
|
|
||||||
path: ["DATABASE_URL"],
|
|
||||||
message: "Either DATABASE_URL or POSTGRES_HOST must be provided",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export type AppConfig = z.infer<typeof configSchema> & {
|
|
||||||
EFFECTIVE_TEXT_GUILD_ID?: string;
|
EFFECTIVE_TEXT_GUILD_ID?: string;
|
||||||
EFFECTIVE_VOICE_GUILD_ID?: string;
|
EFFECTIVE_VOICE_GUILD_ID?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
|
export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
|
||||||
try {
|
const parsed = sharedLoadConfig(env);
|
||||||
const parsed = configSchema.parse(env);
|
return {
|
||||||
return {
|
...parsed,
|
||||||
...parsed,
|
EFFECTIVE_TEXT_GUILD_ID: parsed.MONITOR_GUILD_ID,
|
||||||
// AI text capture and analytics are pinned to the monitor guild.
|
EFFECTIVE_VOICE_GUILD_ID: parsed.VOICE_GUILD_ID ?? parsed.GUILD_ID,
|
||||||
EFFECTIVE_TEXT_GUILD_ID: parsed.MONITOR_GUILD_ID,
|
};
|
||||||
EFFECTIVE_VOICE_GUILD_ID: parsed.VOICE_GUILD_ID ?? parsed.GUILD_ID,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof z.ZodError) {
|
|
||||||
const messages = error.issues
|
|
||||||
.map((e) => `${e.path.join(".")}: ${e.message}`)
|
|
||||||
.join("\n");
|
|
||||||
throw new ConfigError(`Configuration validation failed:\n${messages}`);
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = loadConfig();
|
export const config = loadConfig();
|
||||||
|
|||||||
-60
@@ -1,60 +0,0 @@
|
|||||||
const baseURL = process.env.AI_LLM_BASE_URL;
|
|
||||||
const apiKey = process.env.AI_LLM_API_KEY;
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
console.log("🚀 Initializing test script...");
|
|
||||||
console.log("baseURL:", baseURL);
|
|
||||||
console.log("apiKey:", apiKey ? "Set (Hidden)" : "Not Set");
|
|
||||||
|
|
||||||
const model = "cf/@cf/google/gemma-4-26b-a4b-it";
|
|
||||||
const startMs = Date.now();
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.log(`\n📡 Mengirim request ke model: ${model} ...`);
|
|
||||||
|
|
||||||
// Kita gunakan timeout manual menggunakan AbortController untuk mensimulasikan
|
|
||||||
// timeout LLM klien di batas waktu tinggi
|
|
||||||
const controller = new AbortController();
|
|
||||||
const timeoutId = setTimeout(() => controller.abort(), 120_000); // 120 detik
|
|
||||||
|
|
||||||
const response = await fetch(`${baseURL}/chat/completions`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {})
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: model,
|
|
||||||
messages: [
|
|
||||||
{ role: "system", content: "You are a helpful assistant." },
|
|
||||||
{ role: "user", content: "Jelaskan secara singkat cara kerja timeout API." }
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
signal: controller.signal
|
|
||||||
});
|
|
||||||
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
|
|
||||||
const endMs = Date.now();
|
|
||||||
console.log(`\n✅ Respons diterima dalam ${endMs - startMs}ms`);
|
|
||||||
console.log("Status HTTP:", response.status);
|
|
||||||
|
|
||||||
const text = await response.text();
|
|
||||||
try {
|
|
||||||
const data = JSON.parse(text);
|
|
||||||
console.dir(data, { depth: null });
|
|
||||||
console.log("\n📝 Content:");
|
|
||||||
console.log(data.choices?.[0]?.message?.content);
|
|
||||||
} catch {
|
|
||||||
console.log("\n📝 Raw Text Response:");
|
|
||||||
console.log(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
const endMs = Date.now();
|
|
||||||
console.log(`\n❌ Request gagal setelah ${endMs - startMs}ms`);
|
|
||||||
console.error("Pesan Error:", error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
main().catch(console.error);
|
|
||||||
Reference in New Issue
Block a user