|
| 1 | +import type { ToolConfig } from '../types' |
| 2 | +import type { KnowledgeUploadDocumentResponse } from './types' |
| 3 | + |
| 4 | +export const knowledgeUploadDocumentTool: ToolConfig<any, KnowledgeUploadDocumentResponse> = { |
| 5 | + id: 'knowledge_upload_document', |
| 6 | + name: 'Knowledge Upload Document', |
| 7 | + description: 'Upload documents to a knowledge base', |
| 8 | + version: '1.0.0', |
| 9 | + params: { |
| 10 | + knowledgeBaseId: { |
| 11 | + type: 'string', |
| 12 | + required: true, |
| 13 | + description: 'ID of the knowledge base containing the document', |
| 14 | + }, |
| 15 | + knowledgeBaseName: { |
| 16 | + type: 'string', |
| 17 | + required: true, |
| 18 | + description: 'Name of the knowledge base to upload the document to', |
| 19 | + }, |
| 20 | + file: { |
| 21 | + type: 'file', |
| 22 | + required: true, |
| 23 | + description: 'Document(s) to upload', |
| 24 | + }, |
| 25 | + }, |
| 26 | + request: { |
| 27 | + url: (params) => `/api/knowledge/${params.knowledgeBaseId}/documents`, |
| 28 | + method: 'POST', |
| 29 | + headers: () => ({ |
| 30 | + 'Content-Type': 'application/json', |
| 31 | + }), |
| 32 | + body: (params) => { |
| 33 | + // Handle both single file and array of files from FileUpload component |
| 34 | + const files = Array.isArray(params.file) ? params.file : [params.file] |
| 35 | + |
| 36 | + // Map files to the expected document format |
| 37 | + const documents = files.map( |
| 38 | + (fileData: { name: string; path: string; size: number; type: string }) => { |
| 39 | + // Create file URL (handle both relative and absolute paths) |
| 40 | + const fileUrl = fileData.path?.startsWith('http') |
| 41 | + ? fileData.path |
| 42 | + : `${typeof window !== 'undefined' ? window.location.origin : ''}${fileData.path}` |
| 43 | + |
| 44 | + return { |
| 45 | + filename: fileData.name, |
| 46 | + fileUrl: fileUrl, |
| 47 | + fileSize: fileData.size, |
| 48 | + mimeType: fileData.type, |
| 49 | + } |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + // Use bulk upload format (required for processing) |
| 54 | + const requestBody = { |
| 55 | + documents: documents, |
| 56 | + processingOptions: { |
| 57 | + chunkSize: 1024, |
| 58 | + minCharactersPerChunk: 100, |
| 59 | + chunkOverlap: 200, |
| 60 | + recipe: 'default', |
| 61 | + lang: 'en', |
| 62 | + }, |
| 63 | + bulk: true, |
| 64 | + } |
| 65 | + |
| 66 | + return requestBody |
| 67 | + }, |
| 68 | + isInternalRoute: true, |
| 69 | + }, |
| 70 | + transformResponse: async (response): Promise<KnowledgeUploadDocumentResponse> => { |
| 71 | + try { |
| 72 | + const result = await response.json() |
| 73 | + |
| 74 | + if (!response.ok) { |
| 75 | + const errorMessage = result.error?.message || result.message || 'Failed to upload documents' |
| 76 | + throw new Error(errorMessage) |
| 77 | + } |
| 78 | + |
| 79 | + const data = result.data || result |
| 80 | + const documentsCreated = data.documentsCreated || [] |
| 81 | + |
| 82 | + // Handle multiple documents response |
| 83 | + const uploadCount = documentsCreated.length |
| 84 | + const firstDocument = documentsCreated[0] |
| 85 | + |
| 86 | + return { |
| 87 | + success: true, |
| 88 | + output: { |
| 89 | + data: { |
| 90 | + id: firstDocument?.documentId || firstDocument?.id || '', |
| 91 | + name: |
| 92 | + uploadCount > 1 ? `${uploadCount} documents` : firstDocument?.filename || 'Unknown', |
| 93 | + size: 0, // Size not returned in bulk response |
| 94 | + type: 'document', |
| 95 | + url: '', |
| 96 | + createdAt: new Date().toISOString(), |
| 97 | + updatedAt: new Date().toISOString(), |
| 98 | + enabled: true, |
| 99 | + }, |
| 100 | + message: |
| 101 | + uploadCount > 1 |
| 102 | + ? `Successfully uploaded ${uploadCount} documents to knowledge base` |
| 103 | + : `Successfully uploaded document to knowledge base`, |
| 104 | + documentId: firstDocument?.documentId || firstDocument?.id || '', |
| 105 | + }, |
| 106 | + } |
| 107 | + } catch (error: any) { |
| 108 | + return { |
| 109 | + success: false, |
| 110 | + output: { |
| 111 | + data: { |
| 112 | + id: '', |
| 113 | + name: '', |
| 114 | + size: 0, |
| 115 | + type: '', |
| 116 | + url: '', |
| 117 | + enabled: true, |
| 118 | + createdAt: '', |
| 119 | + updatedAt: '', |
| 120 | + }, |
| 121 | + message: `Failed to upload documents: ${error.message || 'Unknown error'}`, |
| 122 | + documentId: '', |
| 123 | + }, |
| 124 | + error: `Failed to upload documents: ${error.message || 'Unknown error'}`, |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + transformError: async (error): Promise<KnowledgeUploadDocumentResponse> => { |
| 129 | + const errorMessage = `Failed to upload documents: ${error.message || 'Unknown error'}` |
| 130 | + return { |
| 131 | + success: false, |
| 132 | + output: { |
| 133 | + data: { |
| 134 | + id: '', |
| 135 | + name: '', |
| 136 | + size: 0, |
| 137 | + type: '', |
| 138 | + url: '', |
| 139 | + enabled: true, |
| 140 | + createdAt: '', |
| 141 | + updatedAt: '', |
| 142 | + }, |
| 143 | + message: errorMessage, |
| 144 | + documentId: '', |
| 145 | + }, |
| 146 | + error: errorMessage, |
| 147 | + } |
| 148 | + }, |
| 149 | +} |
0 commit comments