/** * Article Management Tools */ import { getPayloadClient } from '../client.js'; export async function articlesListTool(args: any) { try { const client = await getPayloadClient(); const response = await client.articles.list({ page: args.page || 1, limit: Math.min(args.limit || 10, 100), // Cap at 100 sort: args.sort, where: args.where, }); return { success: true, data: response, message: `Retrieved ${response.docs?.length || 0} articles`, }; } catch (error: any) { throw new Error(`Failed to list articles: ${error.message}`); } } export async function articlesGetTool(args: any) { if (!args.id) { throw new Error('Article ID is required'); } try { const client = await getPayloadClient(); const article = await client.articles.retrieve(args.id); return { success: true, data: article, }; } catch (error: any) { if (error.status === 404) { throw new Error(`Article not found: ${args.id}`); } throw new Error(`Failed to get article: ${error.message}`); } } export async function articlesCreateTool(args: any) { if (!args.title) { throw new Error('Article title is required'); } try { const client = await getPayloadClient(); const article = await client.articles.create({ title: args.title, content: args.content, author: args.author, category: args.category, status: args.status || 'draft', ...args.meta, }); return { success: true, data: article, message: `Article created: ${args.title}`, }; } catch (error: any) { throw new Error(`Failed to create article: ${error.message}`); } } export async function articlesUpdateTool(args: any) { if (!args.id) { throw new Error('Article ID is required'); } try { const client = await getPayloadClient(); const { id, ...updateData } = args; const article = await client.articles.update(id, updateData); return { success: true, data: article, message: `Article updated: ${id}`, }; } catch (error: any) { if (error.status === 404) { throw new Error(`Article not found: ${args.id}`); } throw new Error(`Failed to update article: ${error.message}`); } } export async function articlesDeleteTool(args: any) { if (!args.id) { throw new Error('Article ID is required'); } try { const client = await getPayloadClient(); await client.articles.delete(args.id); return { success: true, message: `Article deleted: ${args.id}`, }; } catch (error: any) { if (error.status === 404) { throw new Error(`Article not found: ${args.id}`); } throw new Error(`Failed to delete article: ${error.message}`); } }