From 1085d7909c41aaa8458fe3b16e57a30819237ba5 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Mon, 18 May 2026 04:40:54 +0700 Subject: [PATCH] fix: address LLM square bracket mapping format and resolve Biome import formatting Co-Authored-By: Claude Opus 4.7 --- src/media/mediaResolver.ts | 2 +- src/media/screenShareController.ts | 2 +- src/moderation/llmModerationClient.ts | 5 +++- src/player.ts | 2 +- src/streaming/index.ts | 12 ++++----- src/streaming/transcoder.ts | 6 ++--- src/webserver.ts | 2 +- test_dank.ts | 2 +- test_dank2.ts | 2 +- test_stream.ts | 2 +- tests/media/musicPlayer.test.ts | 2 +- tests/moderation/llmModerationClient.test.ts | 27 +++++++++++++++++--- tests/streaming/playTranscode.test.ts | 2 +- tests/streaming/transcoder.test.ts | 2 +- 14 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/media/mediaResolver.ts b/src/media/mediaResolver.ts index 54739c3..a87bfed 100644 --- a/src/media/mediaResolver.ts +++ b/src/media/mediaResolver.ts @@ -1,7 +1,7 @@ import { existsSync, statSync } from "node:fs"; import path from "node:path"; import { AppError } from "../errors"; -import type { ResolvedMediaSource, MediaMode } from "./mediaTypes"; +import type { MediaMode, ResolvedMediaSource } from "./mediaTypes"; import { createPlayDlResolver } from "./playDlResolver"; import { createYtDlp, type YtDlpClient } from "./ytdlp"; diff --git a/src/media/screenShareController.ts b/src/media/screenShareController.ts index 4dbd48a..9688a3a 100644 --- a/src/media/screenShareController.ts +++ b/src/media/screenShareController.ts @@ -1,7 +1,7 @@ -import { Streamer, playPreparedStream } from "../streaming"; import { AppError } from "../errors"; import { createChildLogger } from "../logger"; import { discordPlayer } from "../player"; +import { playPreparedStream, Streamer } from "../streaming"; const logger = createChildLogger("screen-share"); diff --git a/src/moderation/llmModerationClient.ts b/src/moderation/llmModerationClient.ts index 02a5870..c6df1c3 100644 --- a/src/moderation/llmModerationClient.ts +++ b/src/moderation/llmModerationClient.ts @@ -101,7 +101,10 @@ export function parseModerationResponse( throw new Error("Result missing 'message_id'"); } - let finalId = String(message_id); + let finalId = String(message_id).trim(); + if (finalId.startsWith("[") && finalId.endsWith("]")) { + finalId = finalId.slice(1, -1).trim(); + } // Precision loss fix: If the ID from LLM is not found, // try to find the closest match in targets if it looks rounded (ends in 000) diff --git a/src/player.ts b/src/player.ts index 84117ab..94858e3 100644 --- a/src/player.ts +++ b/src/player.ts @@ -9,8 +9,8 @@ import { VoiceConnection, } from "@discordjs/voice"; import type { - DiscordPlayOptions, DiscordPlayerOwner, + DiscordPlayOptions, } from "./media/mediaTypes"; export class DiscordPlayer { diff --git a/src/streaming/index.ts b/src/streaming/index.ts index 0a1b4ee..1bfb4d8 100644 --- a/src/streaming/index.ts +++ b/src/streaming/index.ts @@ -1,15 +1,15 @@ -import { EventEmitter } from "node:events"; -import { PassThrough } from "node:stream"; -import type { Readable } from "node:stream"; import type { ChildProcess } from "node:child_process"; -import type { Client } from "discord.js-selfbot-v13"; +import { EventEmitter } from "node:events"; +import type { Readable } from "node:stream"; +import { PassThrough } from "node:stream"; import { Streamer as DankStreamer, - prepareStream as dankPrepareStream, playStream as dankPlayStream, - Utils, + prepareStream as dankPrepareStream, Encoders, + Utils, } from "@dank074/discord-video-stream"; +import type { Client } from "discord.js-selfbot-v13"; type VoiceConnectionLike = any; type StreamConnectionLike = any; diff --git a/src/streaming/transcoder.ts b/src/streaming/transcoder.ts index 7678411..50bdfec 100644 --- a/src/streaming/transcoder.ts +++ b/src/streaming/transcoder.ts @@ -1,9 +1,9 @@ -import { spawn, ChildProcess } from "node:child_process"; -import { PassThrough } from "node:stream"; +import { ChildProcess, spawn } from "node:child_process"; import type { Readable } from "node:stream"; -import { retryWithBackoff } from "../retry"; +import { PassThrough } from "node:stream"; import { createChildLogger } from "../logger"; import { transcoderRestartsCounter, transcoderRunningGauge } from "../metrics"; +import { retryWithBackoff } from "../retry"; const logger = createChildLogger("transcoder"); diff --git a/src/webserver.ts b/src/webserver.ts index f16199d..9858ec5 100644 --- a/src/webserver.ts +++ b/src/webserver.ts @@ -2,7 +2,6 @@ import fs from "node:fs"; import http from "node:http"; import path from "node:path"; import { fileURLToPath } from "node:url"; -import { Streamer } from "./streaming"; import { AudioPlayerStatus } from "@discordjs/voice"; import type { Client } from "discord.js-selfbot-v13"; import express, { @@ -29,6 +28,7 @@ import { createMessageRoutes } from "./routes/messageRoutes"; import { createSyncRoutes } from "./routes/syncRoutes"; import { createUIStateRoutes } from "./routes/uiStateRoutes"; import { createVoiceRoutes } from "./routes/voiceRoutes"; +import { Streamer } from "./streaming"; import type { VoiceController } from "./voiceController"; const __filename = fileURLToPath(import.meta.url); diff --git a/test_dank.ts b/test_dank.ts index 79da909..cae969a 100644 --- a/test_dank.ts +++ b/test_dank.ts @@ -1,4 +1,4 @@ -import { prepareStream, Encoders } from "@dank074/discord-video-stream"; +import { Encoders, prepareStream } from "@dank074/discord-video-stream"; import fs from "fs"; async function run() { diff --git a/test_dank2.ts b/test_dank2.ts index a7e6b77..62717c8 100644 --- a/test_dank2.ts +++ b/test_dank2.ts @@ -1,4 +1,4 @@ -import { prepareStream, Encoders } from "@dank074/discord-video-stream"; +import { Encoders, prepareStream } from "@dank074/discord-video-stream"; import { demux } from "@dank074/discord-video-stream/dist/media/LibavDemuxer.js"; async function run() { diff --git a/test_stream.ts b/test_stream.ts index 050fe7e..a0ea95f 100644 --- a/test_stream.ts +++ b/test_stream.ts @@ -1,6 +1,6 @@ import { prepareStream } from "@dank074/discord-video-stream"; -import { demux } from "@dank074/discord-video-stream/dist/media/LibavDemuxer.js"; import { Encoders } from "@dank074/discord-video-stream/dist/media/encoders/index.js"; +import { demux } from "@dank074/discord-video-stream/dist/media/LibavDemuxer.js"; async function run() { const { command, output } = prepareStream( diff --git a/tests/media/musicPlayer.test.ts b/tests/media/musicPlayer.test.ts index 32f936c..8629555 100644 --- a/tests/media/musicPlayer.test.ts +++ b/tests/media/musicPlayer.test.ts @@ -4,8 +4,8 @@ type Spawn = typeof nodeSpawn; import { EventEmitter } from "node:events"; import { PassThrough } from "node:stream"; -import { describe, expect, it, vi } from "vitest"; import { StreamType } from "@discordjs/voice"; +import { describe, expect, it, vi } from "vitest"; import type { DiscordAudioPlayer, DiscordPlayerOwner, diff --git a/tests/moderation/llmModerationClient.test.ts b/tests/moderation/llmModerationClient.test.ts index a1c8c24..046e295 100644 --- a/tests/moderation/llmModerationClient.test.ts +++ b/tests/moderation/llmModerationClient.test.ts @@ -298,6 +298,23 @@ describe("parseModerationResponse", () => { expect(result).toHaveLength(1); expect(result[0].messageId).toBe("m1"); }); + + it("handles message_id returned with square brackets", () => { + const content = JSON.stringify({ + results: [ + { + message_id: "[m1]", + status: "clean", + flags: [], + score: 0.1, + analysis: "OK", + }, + ], + }); + const result = parseModerationResponse(content, ["m1"]); + expect(result).toHaveLength(1); + expect(result[0].messageId).toBe("m1"); + }); }); describe("runModerationAnalysis", () => { @@ -519,7 +536,11 @@ describe("runModerationAnalysis", () => { }); }); - const createAttachment = (id: string, msgId: string, createdAt: number) => ({ + const createAttachment = ( + id: string, + msgId: string, + createdAt: number, + ) => ({ id, message_id: msgId, guild_id: "guild123", @@ -564,9 +585,7 @@ describe("runModerationAnalysis", () => { // Target attachments (t3, t2, t1) must be fetched, then context in descending order of created_at: // Sorted order: t3 (800), t2 (500), t1 (300), c7 (1000), c6 (900), c5 (700), c4 (600), c3 (400) // Excluded: c2 (200), c1 (100) - const downloadedUrls = fetchCalls - .slice(0, 8) - .map((call: any) => call[0]); + const downloadedUrls = fetchCalls.slice(0, 8).map((call: any) => call[0]); expect(downloadedUrls).toContain("https://picser.tech/t3.png"); expect(downloadedUrls).toContain("https://picser.tech/t2.png"); diff --git a/tests/streaming/playTranscode.test.ts b/tests/streaming/playTranscode.test.ts index 6ecbd91..8b512fa 100644 --- a/tests/streaming/playTranscode.test.ts +++ b/tests/streaming/playTranscode.test.ts @@ -1,5 +1,5 @@ -import { describe, it, expect, vi } from "vitest"; import { PassThrough } from "node:stream"; +import { describe, expect, it, vi } from "vitest"; vi.mock("node:child_process", async () => { const actual = await vi.importActual("node:child_process"); diff --git a/tests/streaming/transcoder.test.ts b/tests/streaming/transcoder.test.ts index 5f07c78..03fdb5d 100644 --- a/tests/streaming/transcoder.test.ts +++ b/tests/streaming/transcoder.test.ts @@ -1,5 +1,5 @@ -import { describe, it, expect, vi } from "vitest"; import { PassThrough } from "node:stream"; +import { describe, expect, it, vi } from "vitest"; // Mock spawn to avoid calling real ffmpeg vi.mock("node:child_process", async () => {