|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import type { ToolConfig } from '@/tools/types' |
| 3 | +import type { AttioCreateAttributeParams, AttioCreateAttributeResponse } from './types' |
| 4 | +import { ATTRIBUTE_OUTPUT_PROPERTIES } from './types' |
| 5 | + |
| 6 | +const logger = createLogger('AttioCreateAttribute') |
| 7 | + |
| 8 | +export const attioCreateAttributeTool: ToolConfig< |
| 9 | + AttioCreateAttributeParams, |
| 10 | + AttioCreateAttributeResponse |
| 11 | +> = { |
| 12 | + id: 'attio_create_attribute', |
| 13 | + name: 'Attio Create Attribute', |
| 14 | + description: 'Create a new attribute (schema field) on an Attio object or list', |
| 15 | + version: '1.0.0', |
| 16 | + |
| 17 | + oauth: { |
| 18 | + required: true, |
| 19 | + provider: 'attio', |
| 20 | + }, |
| 21 | + |
| 22 | + params: { |
| 23 | + accessToken: { |
| 24 | + type: 'string', |
| 25 | + required: true, |
| 26 | + visibility: 'hidden', |
| 27 | + description: 'The OAuth access token for the Attio API', |
| 28 | + }, |
| 29 | + target: { |
| 30 | + type: 'string', |
| 31 | + required: true, |
| 32 | + visibility: 'user-or-llm', |
| 33 | + description: 'Whether to create the attribute on an object or a list: objects or lists', |
| 34 | + }, |
| 35 | + identifier: { |
| 36 | + type: 'string', |
| 37 | + required: true, |
| 38 | + visibility: 'user-or-llm', |
| 39 | + description: 'The object or list ID or slug (e.g. people, companies)', |
| 40 | + }, |
| 41 | + title: { |
| 42 | + type: 'string', |
| 43 | + required: true, |
| 44 | + visibility: 'user-or-llm', |
| 45 | + description: 'The attribute display title', |
| 46 | + }, |
| 47 | + apiSlug: { |
| 48 | + type: 'string', |
| 49 | + required: true, |
| 50 | + visibility: 'user-or-llm', |
| 51 | + description: 'The attribute API slug (unique, snake_case)', |
| 52 | + }, |
| 53 | + type: { |
| 54 | + type: 'string', |
| 55 | + required: true, |
| 56 | + visibility: 'user-or-llm', |
| 57 | + description: |
| 58 | + 'The attribute value type (e.g. text, number, checkbox, currency, date, timestamp, rating, status, select, record-reference, actor-reference, location, domain, email-address, phone-number)', |
| 59 | + }, |
| 60 | + description: { |
| 61 | + type: 'string', |
| 62 | + required: false, |
| 63 | + visibility: 'user-or-llm', |
| 64 | + description: 'A description of the attribute', |
| 65 | + }, |
| 66 | + isRequired: { |
| 67 | + type: 'boolean', |
| 68 | + required: false, |
| 69 | + visibility: 'user-or-llm', |
| 70 | + description: 'Whether new records must provide a value (default false)', |
| 71 | + }, |
| 72 | + isUnique: { |
| 73 | + type: 'boolean', |
| 74 | + required: false, |
| 75 | + visibility: 'user-or-llm', |
| 76 | + description: 'Whether the attribute enforces uniqueness on new data (default false)', |
| 77 | + }, |
| 78 | + isMultiselect: { |
| 79 | + type: 'boolean', |
| 80 | + required: false, |
| 81 | + visibility: 'user-or-llm', |
| 82 | + description: 'Whether the attribute supports multiple values (default false)', |
| 83 | + }, |
| 84 | + config: { |
| 85 | + type: 'string', |
| 86 | + required: false, |
| 87 | + visibility: 'user-or-llm', |
| 88 | + description: |
| 89 | + 'JSON object of type-dependent configuration (e.g. currency or record-reference settings)', |
| 90 | + }, |
| 91 | + }, |
| 92 | + |
| 93 | + request: { |
| 94 | + url: (params) => |
| 95 | + `https://api.attio.com/v2/${params.target.trim()}/${params.identifier.trim()}/attributes`, |
| 96 | + method: 'POST', |
| 97 | + headers: (params) => ({ |
| 98 | + Authorization: `Bearer ${params.accessToken}`, |
| 99 | + 'Content-Type': 'application/json', |
| 100 | + }), |
| 101 | + body: (params) => { |
| 102 | + const data: Record<string, unknown> = { |
| 103 | + title: params.title, |
| 104 | + api_slug: params.apiSlug, |
| 105 | + description: params.description ?? null, |
| 106 | + type: params.type, |
| 107 | + is_required: params.isRequired ?? false, |
| 108 | + is_unique: params.isUnique ?? false, |
| 109 | + is_multiselect: params.isMultiselect ?? false, |
| 110 | + // `config` is a required key on Attio's create-attribute request body (even though its |
| 111 | + // nested fields are only required for type-dependent configs like currency/record-reference). |
| 112 | + config: {}, |
| 113 | + } |
| 114 | + if (params.config) { |
| 115 | + try { |
| 116 | + data.config = |
| 117 | + typeof params.config === 'string' ? JSON.parse(params.config) : params.config |
| 118 | + } catch { |
| 119 | + throw new Error('Invalid JSON provided for attribute config') |
| 120 | + } |
| 121 | + } |
| 122 | + return { data } |
| 123 | + }, |
| 124 | + }, |
| 125 | + |
| 126 | + transformResponse: async (response) => { |
| 127 | + const data = await response.json() |
| 128 | + if (!response.ok) { |
| 129 | + logger.error('Attio API request failed', { data, status: response.status }) |
| 130 | + throw new Error(data.message || 'Failed to create attribute') |
| 131 | + } |
| 132 | + const attr = data.data |
| 133 | + return { |
| 134 | + success: true, |
| 135 | + output: { |
| 136 | + attributeId: attr.id?.attribute_id ?? null, |
| 137 | + title: attr.title ?? null, |
| 138 | + apiSlug: attr.api_slug ?? null, |
| 139 | + description: attr.description ?? null, |
| 140 | + type: attr.type ?? null, |
| 141 | + isSystemAttribute: attr.is_system_attribute ?? false, |
| 142 | + isWritable: attr.is_writable ?? false, |
| 143 | + isRequired: attr.is_required ?? false, |
| 144 | + isUnique: attr.is_unique ?? false, |
| 145 | + isMultiselect: attr.is_multiselect ?? false, |
| 146 | + isDefaultValueEnabled: attr.is_default_value_enabled ?? false, |
| 147 | + isArchived: attr.is_archived ?? false, |
| 148 | + defaultValue: attr.default_value ?? null, |
| 149 | + relationship: attr.relationship ?? null, |
| 150 | + config: attr.config ?? null, |
| 151 | + createdAt: attr.created_at ?? null, |
| 152 | + }, |
| 153 | + } |
| 154 | + }, |
| 155 | + |
| 156 | + outputs: ATTRIBUTE_OUTPUT_PROPERTIES, |
| 157 | +} |
0 commit comments