import { Command } from 'commander' import { handleError } from '@/shared/utils/error-handler' import { formatOutput } from '@/shared/utils/output' import type { BotOption } from './shared' import { getClient, getCurrentServer } from './shared' interface ThreadOutput { id: string name: string type: number parent_id?: string } interface ListActionResult { error?: string threads?: Array<{ id: string name: string type: number parent_id?: string archived?: boolean }> has_more?: boolean } interface CreateActionResult { success?: boolean error?: string thread?: ThreadOutput } interface ArchiveActionResult { success?: boolean error?: string threadId?: string } export async function listAction( channel: string | undefined, options: BotOption & { archived?: boolean }, ): Promise { try { if (options.archived && !channel) { return { error: 'thread list --archived requires a channel' } } const serverId = await getCurrentServer(options) const client = await getClient(options) const parentId = channel ? await client.resolveChannel(serverId, channel) : undefined const result = await client.listThreads(serverId, { parentId, archived: options.archived }) return { threads: result.threads.map((thread) => ({ id: thread.id, name: thread.name, type: thread.type, parent_id: thread.parent_id, archived: thread.thread_metadata?.archived, })), has_more: result.has_more, } } catch (error) { return { error: (error as Error).message } } } export async function createAction( channel: string, name: string, options: BotOption & { autoArchiveDuration?: string }, ): Promise { try { const serverId = await getCurrentServer(options) const client = await getClient(options) const channelId = await client.resolveChannel(serverId, channel) const threadOptions: { auto_archive_duration?: number } = {} if (options.autoArchiveDuration) { threadOptions.auto_archive_duration = parseInt(options.autoArchiveDuration, 10) } const thread = await client.createThread(channelId, name, threadOptions) const output: ThreadOutput = { id: thread.id, name: thread.name, type: thread.type, parent_id: thread.parent_id, } return { success: true, thread: output } } catch (error) { return { error: (error as Error).message } } } export async function archiveAction(threadId: string, options: BotOption): Promise { try { const client = await getClient(options) await client.archiveThread(threadId) return { success: true, threadId } } catch (error) { return { error: (error as Error).message } } } export const threadCommand = new Command('thread') .description('Thread commands') .addCommand( new Command('list') .description('List threads') .argument('[channel]', 'Channel ID or name') .option('--archived', 'List archived threads (requires a channel)') .option('--server ', 'Use specific server') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (channelArg: string | undefined, options: BotOption & { archived?: boolean }) => { try { const result = await listAction(channelArg, options) console.log(formatOutput(result, options.pretty)) } catch (error) { handleError(error as Error) } }), ) .addCommand( new Command('create') .description('Create a thread') .argument('', 'Channel ID or name') .argument('', 'Thread name') .option('--auto-archive-duration ', 'Auto archive duration in minutes') .option('--server ', 'Use specific server') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (channelArg: string, nameArg: string, options: BotOption & { autoArchiveDuration?: string }) => { try { const result = await createAction(channelArg, nameArg, options) console.log(formatOutput(result, options.pretty)) } catch (error) { handleError(error as Error) } }), ) .addCommand( new Command('archive') .description('Archive a thread') .argument('', 'Thread ID') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (threadIdArg: string, options: BotOption) => { try { const result = await archiveAction(threadIdArg, options) console.log(formatOutput(result, options.pretty)) } catch (error) { handleError(error as Error) } }), )