|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import type { OneDriveCopyResponse, OneDriveToolParams } from '@/tools/onedrive/types' |
| 3 | +import type { ToolConfig } from '@/tools/types' |
| 4 | + |
| 5 | +const logger = createLogger('OneDriveCopyTool') |
| 6 | + |
| 7 | +/** |
| 8 | + * Microsoft Graph processes driveItem copies asynchronously: a successful request returns |
| 9 | + * `202 Accepted` with a `Location` header pointing to a monitor URL, not the copied item itself. |
| 10 | + * See https://learn.microsoft.com/en-us/graph/api/driveitem-copy |
| 11 | + */ |
| 12 | +export const copyTool: ToolConfig<OneDriveToolParams, OneDriveCopyResponse> = { |
| 13 | + id: 'onedrive_copy', |
| 14 | + name: 'Copy OneDrive File', |
| 15 | + description: 'Copy a file or folder to another location within OneDrive', |
| 16 | + version: '1.0', |
| 17 | + |
| 18 | + oauth: { |
| 19 | + required: true, |
| 20 | + provider: 'onedrive', |
| 21 | + }, |
| 22 | + |
| 23 | + params: { |
| 24 | + accessToken: { |
| 25 | + type: 'string', |
| 26 | + required: true, |
| 27 | + visibility: 'hidden', |
| 28 | + description: 'The access token for the OneDrive API', |
| 29 | + }, |
| 30 | + fileId: { |
| 31 | + type: 'string', |
| 32 | + required: true, |
| 33 | + visibility: 'user-or-llm', |
| 34 | + description: 'The ID of the file or folder to copy', |
| 35 | + }, |
| 36 | + destinationFolderId: { |
| 37 | + type: 'string', |
| 38 | + required: true, |
| 39 | + visibility: 'user-or-llm', |
| 40 | + description: 'The ID of the destination parent folder', |
| 41 | + }, |
| 42 | + destinationFileName: { |
| 43 | + type: 'string', |
| 44 | + required: false, |
| 45 | + visibility: 'user-or-llm', |
| 46 | + description: 'Optional new name for the copy (defaults to the original name)', |
| 47 | + }, |
| 48 | + }, |
| 49 | + |
| 50 | + request: { |
| 51 | + url: (params) => |
| 52 | + `https://graph.microsoft.com/v1.0/me/drive/items/${encodeURIComponent(params.fileId || '')}/copy`, |
| 53 | + method: 'POST', |
| 54 | + headers: (params) => ({ |
| 55 | + Authorization: `Bearer ${params.accessToken}`, |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + }), |
| 58 | + body: (params) => ({ |
| 59 | + parentReference: { id: params.destinationFolderId }, |
| 60 | + ...(params.destinationFileName && { name: params.destinationFileName }), |
| 61 | + }), |
| 62 | + }, |
| 63 | + |
| 64 | + transformResponse: async (response: Response, params?: OneDriveToolParams) => { |
| 65 | + if (response.status !== 202) { |
| 66 | + const data = await response.json().catch(() => ({})) |
| 67 | + throw new Error(data.error?.message || 'Failed to start OneDrive copy') |
| 68 | + } |
| 69 | + |
| 70 | + const monitorUrl = response.headers.get('location') || undefined |
| 71 | + |
| 72 | + logger.info('OneDrive copy accepted for async processing', { |
| 73 | + fileId: params?.fileId, |
| 74 | + monitorUrl, |
| 75 | + }) |
| 76 | + |
| 77 | + return { |
| 78 | + success: true, |
| 79 | + output: { |
| 80 | + sourceFileId: params?.fileId || '', |
| 81 | + name: params?.destinationFileName, |
| 82 | + monitorUrl, |
| 83 | + }, |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | + outputs: { |
| 88 | + success: { type: 'boolean', description: 'Whether the copy request was accepted' }, |
| 89 | + sourceFileId: { type: 'string', description: 'The ID of the file or folder that was copied' }, |
| 90 | + name: { type: 'string', description: 'The requested name for the copy, if provided' }, |
| 91 | + monitorUrl: { |
| 92 | + type: 'string', |
| 93 | + description: |
| 94 | + 'URL to poll for the status of the asynchronous copy operation (copy completes in the background)', |
| 95 | + }, |
| 96 | + }, |
| 97 | +} |
0 commit comments