|
| 1 | +import type { ToolConfig } from '@/tools/types' |
| 2 | +import { |
| 3 | + buildZendeskUrl, |
| 4 | + extractCursorPagingInfo, |
| 5 | + handleZendeskError, |
| 6 | + TICKETS_ARRAY_OUTPUT, |
| 7 | +} from '@/tools/zendesk/types' |
| 8 | + |
| 9 | +export interface ZendeskGetTicketsV2Params { |
| 10 | + email: string |
| 11 | + apiToken: string |
| 12 | + subdomain: string |
| 13 | + status?: string |
| 14 | + priority?: string |
| 15 | + type?: string |
| 16 | + assigneeId?: string |
| 17 | + organizationId?: string |
| 18 | + sort?: string |
| 19 | + /** Internal: set by auto-pagination via buildNextPageParams */ |
| 20 | + pageAfter?: string |
| 21 | +} |
| 22 | + |
| 23 | +export interface ZendeskGetTicketsV2Response { |
| 24 | + success: boolean |
| 25 | + output: { |
| 26 | + tickets: any[] |
| 27 | + paging?: { |
| 28 | + after_cursor: string | null |
| 29 | + has_more: boolean |
| 30 | + } |
| 31 | + metadata: { |
| 32 | + total_returned: number |
| 33 | + has_more: boolean |
| 34 | + } |
| 35 | + success: boolean |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export const zendeskGetTicketsV2Tool: ToolConfig< |
| 40 | + ZendeskGetTicketsV2Params, |
| 41 | + ZendeskGetTicketsV2Response |
| 42 | +> = { |
| 43 | + id: 'zendesk_get_tickets_v2', |
| 44 | + name: 'Get All Tickets from Zendesk', |
| 45 | + description: |
| 46 | + 'Retrieve all tickets from Zendesk with optional filtering. Automatically paginates through all results.', |
| 47 | + version: '2.0.0', |
| 48 | + |
| 49 | + params: { |
| 50 | + email: { |
| 51 | + type: 'string', |
| 52 | + required: true, |
| 53 | + visibility: 'user-only', |
| 54 | + description: 'Your Zendesk email address', |
| 55 | + }, |
| 56 | + apiToken: { |
| 57 | + type: 'string', |
| 58 | + required: true, |
| 59 | + visibility: 'hidden', |
| 60 | + description: 'Zendesk API token', |
| 61 | + }, |
| 62 | + subdomain: { |
| 63 | + type: 'string', |
| 64 | + required: true, |
| 65 | + visibility: 'user-only', |
| 66 | + description: 'Your Zendesk subdomain (e.g., "mycompany" for mycompany.zendesk.com)', |
| 67 | + }, |
| 68 | + status: { |
| 69 | + type: 'string', |
| 70 | + required: false, |
| 71 | + visibility: 'user-or-llm', |
| 72 | + description: 'Filter by status: "new", "open", "pending", "hold", "solved", or "closed"', |
| 73 | + }, |
| 74 | + priority: { |
| 75 | + type: 'string', |
| 76 | + required: false, |
| 77 | + visibility: 'user-or-llm', |
| 78 | + description: 'Filter by priority: "low", "normal", "high", or "urgent"', |
| 79 | + }, |
| 80 | + type: { |
| 81 | + type: 'string', |
| 82 | + required: false, |
| 83 | + visibility: 'user-or-llm', |
| 84 | + description: 'Filter by type: "problem", "incident", "question", or "task"', |
| 85 | + }, |
| 86 | + assigneeId: { |
| 87 | + type: 'string', |
| 88 | + required: false, |
| 89 | + visibility: 'user-or-llm', |
| 90 | + description: 'Filter by assignee user ID as a numeric string (e.g., "12345")', |
| 91 | + }, |
| 92 | + organizationId: { |
| 93 | + type: 'string', |
| 94 | + required: false, |
| 95 | + visibility: 'user-or-llm', |
| 96 | + description: 'Filter by organization ID as a numeric string (e.g., "67890")', |
| 97 | + }, |
| 98 | + sort: { |
| 99 | + type: 'string', |
| 100 | + required: false, |
| 101 | + visibility: 'user-or-llm', |
| 102 | + description: |
| 103 | + 'Sort field for ticket listing (only applies without filters): "updated_at", "id", or "status". Prefix with "-" for descending (e.g., "-updated_at")', |
| 104 | + }, |
| 105 | + }, |
| 106 | + |
| 107 | + request: { |
| 108 | + url: (params) => { |
| 109 | + const hasFilters = |
| 110 | + params.status || |
| 111 | + params.priority || |
| 112 | + params.type || |
| 113 | + params.assigneeId || |
| 114 | + params.organizationId |
| 115 | + |
| 116 | + if (hasFilters) { |
| 117 | + const searchTerms: string[] = ['type:ticket'] |
| 118 | + if (params.status) searchTerms.push(`status:${params.status}`) |
| 119 | + if (params.priority) searchTerms.push(`priority:${params.priority}`) |
| 120 | + if (params.type) searchTerms.push(`ticket_type:${params.type}`) |
| 121 | + if (params.assigneeId) searchTerms.push(`assignee_id:${params.assigneeId}`) |
| 122 | + if (params.organizationId) searchTerms.push(`organization_id:${params.organizationId}`) |
| 123 | + |
| 124 | + const queryParams = new URLSearchParams() |
| 125 | + queryParams.append('query', searchTerms.join(' ')) |
| 126 | + queryParams.append('filter[type]', 'ticket') |
| 127 | + queryParams.append('page[size]', '100') |
| 128 | + if (params.pageAfter) queryParams.append('page[after]', params.pageAfter) |
| 129 | + |
| 130 | + return `${buildZendeskUrl(params.subdomain, '/search/export')}?${queryParams.toString()}` |
| 131 | + } |
| 132 | + |
| 133 | + const queryParams = new URLSearchParams() |
| 134 | + if (params.sort) queryParams.append('sort', params.sort) |
| 135 | + queryParams.append('page[size]', '100') |
| 136 | + if (params.pageAfter) queryParams.append('page[after]', params.pageAfter) |
| 137 | + |
| 138 | + const query = queryParams.toString() |
| 139 | + const url = buildZendeskUrl(params.subdomain, '/tickets') |
| 140 | + return query ? `${url}?${query}` : url |
| 141 | + }, |
| 142 | + method: 'GET', |
| 143 | + headers: (params) => { |
| 144 | + const credentials = `${params.email}/token:${params.apiToken}` |
| 145 | + const base64Credentials = Buffer.from(credentials).toString('base64') |
| 146 | + return { |
| 147 | + Authorization: `Basic ${base64Credentials}`, |
| 148 | + 'Content-Type': 'application/json', |
| 149 | + } |
| 150 | + }, |
| 151 | + }, |
| 152 | + |
| 153 | + transformResponse: async (response: Response) => { |
| 154 | + if (!response.ok) { |
| 155 | + const data = await response.json() |
| 156 | + handleZendeskError(data, response.status, 'get_tickets') |
| 157 | + } |
| 158 | + |
| 159 | + const data = await response.json() |
| 160 | + const tickets = data.tickets || data.results || [] |
| 161 | + const paging = extractCursorPagingInfo(data) |
| 162 | + |
| 163 | + return { |
| 164 | + success: true, |
| 165 | + output: { |
| 166 | + tickets, |
| 167 | + paging, |
| 168 | + metadata: { |
| 169 | + total_returned: tickets.length, |
| 170 | + has_more: paging.has_more, |
| 171 | + }, |
| 172 | + success: true, |
| 173 | + }, |
| 174 | + } |
| 175 | + }, |
| 176 | + |
| 177 | + outputs: { |
| 178 | + tickets: TICKETS_ARRAY_OUTPUT, |
| 179 | + }, |
| 180 | + |
| 181 | + pagination: { |
| 182 | + pageField: 'tickets', |
| 183 | + getItems: (output) => output.tickets ?? [], |
| 184 | + getNextPageToken: (output) => { |
| 185 | + if (output.paging?.has_more && output.paging?.after_cursor) { |
| 186 | + return output.paging.after_cursor |
| 187 | + } |
| 188 | + return null |
| 189 | + }, |
| 190 | + buildNextPageParams: (params, token) => ({ |
| 191 | + ...params, |
| 192 | + pageAfter: String(token), |
| 193 | + }), |
| 194 | + }, |
| 195 | +} |
0 commit comments