|
| 1 | +import { getErrorMessage } from '@sim/utils/errors' |
| 2 | +import { env } from '@/lib/core/config/env' |
| 3 | +import { |
| 4 | + extractTrelloErrorMessage, |
| 5 | + mapTrelloChecklistItem, |
| 6 | + TRELLO_API_BASE_URL, |
| 7 | +} from '@/tools/trello/shared' |
| 8 | +import type { |
| 9 | + TrelloAddChecklistItemParams, |
| 10 | + TrelloAddChecklistItemResponse, |
| 11 | +} from '@/tools/trello/types' |
| 12 | +import type { ToolConfig } from '@/tools/types' |
| 13 | + |
| 14 | +export const trelloAddChecklistItemTool: ToolConfig< |
| 15 | + TrelloAddChecklistItemParams, |
| 16 | + TrelloAddChecklistItemResponse |
| 17 | +> = { |
| 18 | + id: 'trello_add_checklist_item', |
| 19 | + name: 'Trello Add Checklist Item', |
| 20 | + description: 'Add an item to a Trello checklist', |
| 21 | + version: '1.0.0', |
| 22 | + |
| 23 | + oauth: { |
| 24 | + required: true, |
| 25 | + provider: 'trello', |
| 26 | + }, |
| 27 | + |
| 28 | + params: { |
| 29 | + accessToken: { |
| 30 | + type: 'string', |
| 31 | + required: true, |
| 32 | + visibility: 'hidden', |
| 33 | + description: 'Trello OAuth access token', |
| 34 | + }, |
| 35 | + checklistId: { |
| 36 | + type: 'string', |
| 37 | + required: true, |
| 38 | + visibility: 'user-or-llm', |
| 39 | + description: 'Trello checklist ID to add the item to (24-character hex string)', |
| 40 | + }, |
| 41 | + name: { |
| 42 | + type: 'string', |
| 43 | + required: true, |
| 44 | + visibility: 'user-or-llm', |
| 45 | + description: 'Name of the checklist item', |
| 46 | + }, |
| 47 | + pos: { |
| 48 | + type: 'string', |
| 49 | + required: false, |
| 50 | + visibility: 'user-or-llm', |
| 51 | + description: 'Position of the item (top, bottom, or positive float)', |
| 52 | + }, |
| 53 | + checked: { |
| 54 | + type: 'boolean', |
| 55 | + required: false, |
| 56 | + visibility: 'user-or-llm', |
| 57 | + description: 'Whether the item should start checked off', |
| 58 | + }, |
| 59 | + }, |
| 60 | + |
| 61 | + request: { |
| 62 | + url: (params) => { |
| 63 | + if (!params.checklistId) { |
| 64 | + throw new Error('Checklist ID is required') |
| 65 | + } |
| 66 | + if (!params.name) { |
| 67 | + throw new Error('Checklist item name is required') |
| 68 | + } |
| 69 | + const apiKey = env.TRELLO_API_KEY |
| 70 | + |
| 71 | + if (!apiKey) { |
| 72 | + throw new Error('TRELLO_API_KEY environment variable is not set') |
| 73 | + } |
| 74 | + |
| 75 | + const url = new URL( |
| 76 | + `${TRELLO_API_BASE_URL}/checklists/${params.checklistId.trim()}/checkItems` |
| 77 | + ) |
| 78 | + url.searchParams.set('key', apiKey) |
| 79 | + url.searchParams.set('token', params.accessToken) |
| 80 | + url.searchParams.set('name', params.name.trim()) |
| 81 | + |
| 82 | + if (params.pos) url.searchParams.set('pos', params.pos) |
| 83 | + if (params.checked !== undefined) url.searchParams.set('checked', String(params.checked)) |
| 84 | + |
| 85 | + return url.toString() |
| 86 | + }, |
| 87 | + method: 'POST', |
| 88 | + headers: () => ({ |
| 89 | + Accept: 'application/json', |
| 90 | + }), |
| 91 | + }, |
| 92 | + |
| 93 | + transformResponse: async (response) => { |
| 94 | + const data = await response.json().catch(() => null) |
| 95 | + |
| 96 | + if (!response.ok) { |
| 97 | + const error = extractTrelloErrorMessage(response, data, 'Failed to add checklist item') |
| 98 | + |
| 99 | + return { |
| 100 | + success: false, |
| 101 | + output: { |
| 102 | + error, |
| 103 | + }, |
| 104 | + error, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + const item = mapTrelloChecklistItem(data) |
| 110 | + |
| 111 | + return { |
| 112 | + success: true, |
| 113 | + output: { |
| 114 | + item, |
| 115 | + }, |
| 116 | + } |
| 117 | + } catch (error) { |
| 118 | + const message = getErrorMessage(error, 'Failed to parse created checklist item') |
| 119 | + |
| 120 | + return { |
| 121 | + success: false, |
| 122 | + output: { |
| 123 | + error: message, |
| 124 | + }, |
| 125 | + error: message, |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + |
| 130 | + outputs: { |
| 131 | + item: { |
| 132 | + type: 'json', |
| 133 | + description: 'Created checklist item (id, name, state, pos, idChecklist)', |
| 134 | + optional: true, |
| 135 | + properties: { |
| 136 | + id: { type: 'string', description: 'Checklist item ID' }, |
| 137 | + name: { type: 'string', description: 'Checklist item name' }, |
| 138 | + state: { type: 'string', description: 'Item state (complete or incomplete)' }, |
| 139 | + pos: { type: 'number', description: 'Item position on the checklist' }, |
| 140 | + idChecklist: { |
| 141 | + type: 'string', |
| 142 | + description: 'Checklist ID containing the item', |
| 143 | + optional: true, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | +} |
0 commit comments