refactor: remove unused getThreads function and related code from voice API and controller
This commit is contained in:
@@ -35,10 +35,16 @@ async function processAnalysisRequest({
|
||||
messages,
|
||||
}: AnalysisWorkerRequest): Promise<AnalysisWorkerResponse> {
|
||||
try {
|
||||
if (!dbInitialized) {
|
||||
await initializeDatabase();
|
||||
dbInitialized = true;
|
||||
try {
|
||||
if (!dbInitialized) {
|
||||
await initializeDatabase();
|
||||
dbInitialized = true;
|
||||
}
|
||||
} catch (dbError) {
|
||||
const msg = dbError instanceof Error ? dbError.message : String(dbError);
|
||||
return { ok: false, conversationKey, rows: [], error: `Database init failed: ${msg}` };
|
||||
}
|
||||
|
||||
const firstMessage = messages[0];
|
||||
if (!firstMessage) return { ok: true, conversationKey, rows: [] };
|
||||
|
||||
|
||||
@@ -26,35 +26,39 @@ export function parseModerationResponse(
|
||||
content: string,
|
||||
targetIds: string[],
|
||||
): AnalysisResult[] {
|
||||
// Find first opening brace
|
||||
// Find first opening brace and last closing brace
|
||||
const startIdx = content.indexOf("{");
|
||||
if (startIdx === -1) {
|
||||
const endIdx = content.lastIndexOf("}");
|
||||
|
||||
if (startIdx === -1 || endIdx === -1 || endIdx < startIdx) {
|
||||
throw new Error("No JSON object found in response");
|
||||
}
|
||||
|
||||
// Scan from start and try parsing at each closing brace
|
||||
// Attempt to parse the largest possible JSON object
|
||||
let parsed: unknown;
|
||||
let lastError: Error | null = null;
|
||||
const candidate = content.substring(startIdx, endIdx + 1);
|
||||
|
||||
for (let i = startIdx + 1; i < content.length; i++) {
|
||||
if (content[i] === "}") {
|
||||
const candidate = content.substring(startIdx, i + 1);
|
||||
try {
|
||||
parsed = JSON.parse(candidate);
|
||||
// Successfully parsed, break out
|
||||
break;
|
||||
} catch (error) {
|
||||
// Store error and continue scanning
|
||||
lastError = error instanceof Error ? error : new Error(String(error));
|
||||
continue;
|
||||
try {
|
||||
parsed = JSON.parse(candidate);
|
||||
} catch (error) {
|
||||
// If full substring fails, try scanning backwards from the last }
|
||||
let lastError: Error = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
for (let i = endIdx - 1; i > startIdx; i--) {
|
||||
if (content[i] === "}") {
|
||||
try {
|
||||
parsed = JSON.parse(content.substring(startIdx, i + 1));
|
||||
break;
|
||||
} catch (innerError) {
|
||||
lastError = innerError instanceof Error ? innerError : new Error(String(innerError));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!parsed) {
|
||||
throw new Error(
|
||||
`Failed to parse JSON: ${lastError?.message || "No valid JSON object found"}`,
|
||||
);
|
||||
if (!parsed) {
|
||||
throw new Error(`Failed to parse JSON: ${lastError.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate structure
|
||||
@@ -219,7 +223,18 @@ Return ONLY valid JSON, no other text.`;
|
||||
throw new Error(`LLM API error ${response.status}: ${text}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
const bodyText = await response.text();
|
||||
try {
|
||||
return JSON.parse(bodyText);
|
||||
} catch (e) {
|
||||
// Handle cases where the API provider returns trailing garbage
|
||||
const start = bodyText.indexOf("{");
|
||||
const end = bodyText.lastIndexOf("}");
|
||||
if (start !== -1 && end !== -1 && end > start) {
|
||||
return JSON.parse(bodyText.substring(start, end + 1));
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
@@ -89,22 +89,6 @@ export function createVoiceRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/guilds/:guildId/threads - List threads in a guild
|
||||
router.get("/guilds/:guildId/threads", async (req, res, next) => {
|
||||
try {
|
||||
const { guildId } = req.params;
|
||||
|
||||
if (!guildId) {
|
||||
throw new AppError("Guild ID is required", "MISSING_GUILD_ID", 400);
|
||||
}
|
||||
|
||||
const threads = await voiceController.listThreads(guildId);
|
||||
res.json(threads);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/connect - Connect to a voice channel
|
||||
router.post("/connect", async (req, res, next) => {
|
||||
try {
|
||||
|
||||
@@ -83,46 +83,6 @@ export class VoiceController {
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
async listThreads(guildId: string): Promise<ChannelSummary[]> {
|
||||
const guild = this.getGuild(guildId);
|
||||
await guild.channels.fetch().catch(() => null);
|
||||
|
||||
const threads: ChannelSummary[] = [];
|
||||
type ThreadFetchResult = {
|
||||
threads: Map<string, { id: string; name: string; type: string }>;
|
||||
};
|
||||
for (const channel of guild.channels.cache.values()) {
|
||||
const threadParent = channel as typeof channel & {
|
||||
threads?: {
|
||||
fetch: (options: {
|
||||
archived: boolean;
|
||||
limit: number;
|
||||
}) => Promise<ThreadFetchResult>;
|
||||
};
|
||||
};
|
||||
if (!threadParent.threads?.fetch) continue;
|
||||
|
||||
for (const archived of [false, true]) {
|
||||
const fetched = await threadParent.threads
|
||||
.fetch({ archived, limit: 100 })
|
||||
.catch(() => null);
|
||||
if (!fetched?.threads) continue;
|
||||
|
||||
for (const thread of fetched.threads.values()) {
|
||||
threads.push({
|
||||
id: thread.id,
|
||||
name: `${channel.name} / ${thread.name}`,
|
||||
type: thread.type,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(
|
||||
new Map(threads.map((thread) => [thread.id, thread])).values(),
|
||||
).sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
async connect(guildId: string, channelId: string): Promise<VoiceStatus> {
|
||||
if (!this.client.isReady()) {
|
||||
throw new AppError(
|
||||
|
||||
Reference in New Issue
Block a user