|
| 1 | +import { createLogger } from '@/lib/logs/console/logger' |
| 2 | +import type { |
| 3 | + SharepointToolParams, |
| 4 | + SharepointAddListItemResponse, |
| 5 | +} from '@/tools/sharepoint/types' |
| 6 | +import type { ToolConfig } from '@/tools/types' |
| 7 | + |
| 8 | +const logger = createLogger('SharePointAddListItem') |
| 9 | + |
| 10 | +export const addListItemTool: ToolConfig< |
| 11 | + SharepointToolParams, |
| 12 | + SharepointAddListItemResponse |
| 13 | +> = { |
| 14 | + id: 'sharepoint_add_list_items', |
| 15 | + name: 'Add SharePoint List Item', |
| 16 | + description: 'Add a new item to a SharePoint list', |
| 17 | + version: '1.0', |
| 18 | + |
| 19 | + oauth: { |
| 20 | + required: true, |
| 21 | + provider: 'sharepoint', |
| 22 | + additionalScopes: ['openid', 'profile', 'email', 'Sites.ReadWrite.All', 'offline_access'], |
| 23 | + }, |
| 24 | + |
| 25 | + params: { |
| 26 | + accessToken: { |
| 27 | + type: 'string', |
| 28 | + required: true, |
| 29 | + visibility: 'hidden', |
| 30 | + description: 'The access token for the SharePoint API', |
| 31 | + }, |
| 32 | + siteSelector: { |
| 33 | + type: 'string', |
| 34 | + required: false, |
| 35 | + visibility: 'user-only', |
| 36 | + description: 'Select the SharePoint site', |
| 37 | + }, |
| 38 | + siteId: { |
| 39 | + type: 'string', |
| 40 | + required: false, |
| 41 | + visibility: 'hidden', |
| 42 | + description: 'The ID of the SharePoint site (internal use)', |
| 43 | + }, |
| 44 | + listId: { |
| 45 | + type: 'string', |
| 46 | + required: true, |
| 47 | + visibility: 'user-only', |
| 48 | + description: 'The ID of the list to add the item to', |
| 49 | + }, |
| 50 | + listItemFields: { |
| 51 | + type: 'object', |
| 52 | + required: true, |
| 53 | + visibility: 'user-only', |
| 54 | + description: 'Field values for the new list item', |
| 55 | + }, |
| 56 | + }, |
| 57 | + |
| 58 | + request: { |
| 59 | + url: (params) => { |
| 60 | + const siteId = params.siteId || params.siteSelector || 'root' |
| 61 | + if (!params.listId) { |
| 62 | + throw new Error('listId must be provided') |
| 63 | + } |
| 64 | + const listSegment = params.listId |
| 65 | + return `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listSegment}/items` |
| 66 | + }, |
| 67 | + method: 'POST', |
| 68 | + headers: (params) => ({ |
| 69 | + Authorization: `Bearer ${params.accessToken}`, |
| 70 | + 'Content-Type': 'application/json', |
| 71 | + Accept: 'application/json', |
| 72 | + }), |
| 73 | + body: (params) => { |
| 74 | + if (!params.listItemFields || Object.keys(params.listItemFields).length === 0) { |
| 75 | + throw new Error('listItemFields must not be empty') |
| 76 | + } |
| 77 | + |
| 78 | + // Support both { fields: {...} } and raw { fieldName: value } inputs |
| 79 | + const providedFields = |
| 80 | + typeof params.listItemFields === 'object' && |
| 81 | + params.listItemFields !== null && |
| 82 | + 'fields' in (params.listItemFields as Record<string, unknown>) && |
| 83 | + Object.keys(params.listItemFields as Record<string, unknown>).length === 1 |
| 84 | + ? ((params.listItemFields as any).fields as Record<string, unknown>) |
| 85 | + : (params.listItemFields as Record<string, unknown>) |
| 86 | + |
| 87 | + if (!providedFields || Object.keys(providedFields).length === 0) { |
| 88 | + throw new Error('No fields provided to create the SharePoint list item') |
| 89 | + } |
| 90 | + |
| 91 | + // Filter out system/read-only fields that cannot be set via Graph |
| 92 | + const readOnlyFields = new Set<string>([ |
| 93 | + 'Id', |
| 94 | + 'id', |
| 95 | + 'UniqueId', |
| 96 | + 'GUID', |
| 97 | + 'ContentTypeId', |
| 98 | + 'Created', |
| 99 | + 'Modified', |
| 100 | + 'Author', |
| 101 | + 'Editor', |
| 102 | + 'CreatedBy', |
| 103 | + 'ModifiedBy', |
| 104 | + 'AuthorId', |
| 105 | + 'EditorId', |
| 106 | + '_UIVersionString', |
| 107 | + 'Attachments', |
| 108 | + 'FileRef', |
| 109 | + 'FileDirRef', |
| 110 | + 'FileLeafRef', |
| 111 | + ]) |
| 112 | + |
| 113 | + const entries = Object.entries(providedFields) |
| 114 | + const creatableEntries = entries.filter(([key]) => !readOnlyFields.has(key)) |
| 115 | + |
| 116 | + if (creatableEntries.length !== entries.length) { |
| 117 | + const removed = entries.filter(([key]) => readOnlyFields.has(key)).map(([key]) => key) |
| 118 | + logger.warn('Removed read-only SharePoint fields from create', { |
| 119 | + removed, |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + if (creatableEntries.length === 0) { |
| 124 | + const requestedKeys = Object.keys(providedFields) |
| 125 | + throw new Error( |
| 126 | + `All provided fields are read-only and cannot be set: ${requestedKeys.join(', ')}` |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + const sanitizedFields = Object.fromEntries(creatableEntries) |
| 131 | + |
| 132 | + logger.info('Creating SharePoint list item', { |
| 133 | + listId: params.listId, |
| 134 | + fieldsKeys: Object.keys(sanitizedFields), |
| 135 | + }) |
| 136 | + |
| 137 | + return { |
| 138 | + fields: sanitizedFields, |
| 139 | + } |
| 140 | + }, |
| 141 | + }, |
| 142 | + |
| 143 | + transformResponse: async (response: Response, params) => { |
| 144 | + let data: any = undefined |
| 145 | + try { |
| 146 | + data = await response.json() |
| 147 | + } catch { |
| 148 | + // Some Graph endpoints may not return a body consistently |
| 149 | + data = undefined |
| 150 | + } |
| 151 | + |
| 152 | + const itemId: string | undefined = data?.id |
| 153 | + const fields: Record<string, unknown> | undefined = data?.fields || params?.listItemFields |
| 154 | + |
| 155 | + return { |
| 156 | + success: true, |
| 157 | + output: { |
| 158 | + item: { |
| 159 | + id: itemId || 'unknown', |
| 160 | + fields, |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + }, |
| 165 | + |
| 166 | + outputs: { |
| 167 | + item: { |
| 168 | + type: 'object', |
| 169 | + description: 'Created SharePoint list item', |
| 170 | + properties: { |
| 171 | + id: { type: 'string', description: 'Item ID' }, |
| 172 | + fields: { type: 'object', description: 'Field values for the new item' }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | +} |
| 177 | + |
| 178 | + |
0 commit comments