|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import type { |
| 3 | + HubSpotAddListMembershipsParams, |
| 4 | + HubSpotAddListMembershipsResponse, |
| 5 | +} from '@/tools/hubspot/types' |
| 6 | +import type { ToolConfig } from '@/tools/types' |
| 7 | + |
| 8 | +const logger = createLogger('HubSpotAddListMemberships') |
| 9 | + |
| 10 | +export const hubspotAddListMembershipsTool: ToolConfig< |
| 11 | + HubSpotAddListMembershipsParams, |
| 12 | + HubSpotAddListMembershipsResponse |
| 13 | +> = { |
| 14 | + id: 'hubspot_add_list_memberships', |
| 15 | + name: 'Add List Members in HubSpot', |
| 16 | + description: 'Add records to a manual (static) HubSpot list by record ID', |
| 17 | + version: '1.0.0', |
| 18 | + |
| 19 | + oauth: { |
| 20 | + required: true, |
| 21 | + provider: 'hubspot', |
| 22 | + }, |
| 23 | + |
| 24 | + params: { |
| 25 | + accessToken: { |
| 26 | + type: 'string', |
| 27 | + required: true, |
| 28 | + visibility: 'hidden', |
| 29 | + description: 'The access token for the HubSpot API', |
| 30 | + }, |
| 31 | + listId: { |
| 32 | + type: 'string', |
| 33 | + required: true, |
| 34 | + visibility: 'user-or-llm', |
| 35 | + description: 'The ID of the list to add records to (MANUAL or SNAPSHOT lists only)', |
| 36 | + }, |
| 37 | + recordIds: { |
| 38 | + type: 'array', |
| 39 | + required: true, |
| 40 | + visibility: 'user-or-llm', |
| 41 | + description: |
| 42 | + 'Record IDs to add to the list, as a JSON array (e.g., ["123","456"]) or comma-separated string', |
| 43 | + }, |
| 44 | + }, |
| 45 | + |
| 46 | + request: { |
| 47 | + url: (params) => `https://api.hubapi.com/crm/v3/lists/${params.listId.trim()}/memberships/add`, |
| 48 | + method: 'PUT', |
| 49 | + headers: (params) => { |
| 50 | + if (!params.accessToken) { |
| 51 | + throw new Error('Access token is required') |
| 52 | + } |
| 53 | + return { |
| 54 | + Authorization: `Bearer ${params.accessToken}`, |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + } |
| 57 | + }, |
| 58 | + body: (params) => { |
| 59 | + let recordIds = params.recordIds |
| 60 | + if (typeof recordIds === 'string') { |
| 61 | + const trimmed = recordIds.trim() |
| 62 | + if (trimmed.startsWith('[')) { |
| 63 | + try { |
| 64 | + recordIds = JSON.parse(trimmed) |
| 65 | + } catch (e) { |
| 66 | + throw new Error(`Invalid JSON for recordIds: ${(e as Error).message}`) |
| 67 | + } |
| 68 | + } else { |
| 69 | + recordIds = trimmed.split(',') |
| 70 | + } |
| 71 | + } |
| 72 | + if (!Array.isArray(recordIds)) { |
| 73 | + throw new Error('recordIds must be an array of record IDs') |
| 74 | + } |
| 75 | + const ids = recordIds.map((id) => String(id).trim()).filter(Boolean) |
| 76 | + if (ids.length === 0) { |
| 77 | + throw new Error('At least one record ID is required') |
| 78 | + } |
| 79 | + return ids |
| 80 | + }, |
| 81 | + }, |
| 82 | + |
| 83 | + transformResponse: async (response: Response) => { |
| 84 | + const data = await response.json().catch(() => ({})) |
| 85 | + if (!response.ok) { |
| 86 | + logger.error('HubSpot API request failed', { data, status: response.status }) |
| 87 | + throw new Error(data.message || 'Failed to add records to HubSpot list') |
| 88 | + } |
| 89 | + return { |
| 90 | + success: true, |
| 91 | + output: { |
| 92 | + recordIdsAdded: data.recordIdsAdded ?? [], |
| 93 | + recordIdsMissing: data.recordIdsMissing ?? [], |
| 94 | + success: true, |
| 95 | + }, |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + outputs: { |
| 100 | + recordIdsAdded: { |
| 101 | + type: 'array', |
| 102 | + description: 'IDs of the records that were added to the list', |
| 103 | + items: { type: 'string' }, |
| 104 | + }, |
| 105 | + recordIdsMissing: { |
| 106 | + type: 'array', |
| 107 | + description: 'IDs of the requested records that were not found', |
| 108 | + items: { type: 'string' }, |
| 109 | + }, |
| 110 | + success: { type: 'boolean', description: 'Operation success status' }, |
| 111 | + }, |
| 112 | +} |
0 commit comments