|
| 1 | +import { defineSource, type DatasetSourceCallbacks } from '../../source/registry'; |
| 2 | +import { customApiConfig, CustomApiConfigSchema, type CustomApiConfig } from './config'; |
| 3 | +import type { FileItem, FileContentResponse } from '../../type/source'; |
| 4 | + |
| 5 | +type ResponseDataType = { |
| 6 | + success: boolean; |
| 7 | + message: string; |
| 8 | + data: any; |
| 9 | +}; |
| 10 | + |
| 11 | +type APIFileListResponse = { |
| 12 | + id: string; |
| 13 | + parentId: string | null; |
| 14 | + name: string; |
| 15 | + type: 'file' | 'folder'; |
| 16 | + updateTime: string; |
| 17 | + createTime: string; |
| 18 | + hasChild?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +type APIFileContentResponse = { |
| 22 | + title?: string; |
| 23 | + content?: string; |
| 24 | + previewUrl?: string; |
| 25 | +}; |
| 26 | + |
| 27 | +type APIFileReadResponse = { |
| 28 | + url: string; |
| 29 | +}; |
| 30 | + |
| 31 | +type APIFileDetailResponse = { |
| 32 | + id: string; |
| 33 | + name: string; |
| 34 | + parentId: string | null; |
| 35 | + type: 'file' | 'folder'; |
| 36 | + updateTime: string; |
| 37 | + createTime: string; |
| 38 | +}; |
| 39 | + |
| 40 | +function parseConfig(config: Record<string, any>): CustomApiConfig { |
| 41 | + return CustomApiConfigSchema.parse(config); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * 响应数据检查 |
| 46 | + */ |
| 47 | +function checkResponse(data: ResponseDataType): any { |
| 48 | + if (data === undefined) { |
| 49 | + throw new Error('服务器异常:响应为空'); |
| 50 | + } |
| 51 | + if (!data.success) { |
| 52 | + throw new Error(data.message || '请求失败'); |
| 53 | + } |
| 54 | + return data.data; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * 发送请求到用户的自定义 API |
| 59 | + */ |
| 60 | +async function apiRequest<T>( |
| 61 | + baseUrl: string, |
| 62 | + authorization: string | undefined, |
| 63 | + path: string, |
| 64 | + method: 'GET' | 'POST', |
| 65 | + data?: Record<string, any> |
| 66 | +): Promise<T> { |
| 67 | + // 清理 undefined 值 |
| 68 | + if (data) { |
| 69 | + for (const key in data) { |
| 70 | + if (data[key] === undefined) { |
| 71 | + delete data[key]; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const url = new URL(path, baseUrl); |
| 77 | + const isGetMethod = method === 'GET'; |
| 78 | + |
| 79 | + // GET 请求将参数添加到 URL |
| 80 | + if (isGetMethod && data) { |
| 81 | + Object.entries(data).forEach(([key, value]) => { |
| 82 | + if (value !== undefined && value !== null) { |
| 83 | + url.searchParams.append(key, String(value)); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + const headers: Record<string, string> = { |
| 89 | + 'Content-Type': 'application/json' |
| 90 | + }; |
| 91 | + |
| 92 | + if (authorization) { |
| 93 | + headers['Authorization'] = `Bearer ${authorization}`; |
| 94 | + } |
| 95 | + |
| 96 | + const response = await fetch(url.toString(), { |
| 97 | + method, |
| 98 | + headers, |
| 99 | + body: isGetMethod ? undefined : JSON.stringify(data) |
| 100 | + }); |
| 101 | + |
| 102 | + if (!response.ok) { |
| 103 | + throw new Error(`请求失败: ${response.status} ${response.statusText}`); |
| 104 | + } |
| 105 | + |
| 106 | + const jsonData = await response.json(); |
| 107 | + return checkResponse(jsonData); |
| 108 | +} |
| 109 | + |
| 110 | +const callbacks: DatasetSourceCallbacks = { |
| 111 | + async listFiles({ config, parentId }): Promise<FileItem[]> { |
| 112 | + const { baseUrl, authorization, basePath } = parseConfig(config); |
| 113 | + |
| 114 | + const files = await apiRequest<APIFileListResponse[]>( |
| 115 | + baseUrl, |
| 116 | + authorization, |
| 117 | + '/v1/file/list', |
| 118 | + 'POST', |
| 119 | + { |
| 120 | + parentId: parentId || basePath |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + if (!Array.isArray(files)) { |
| 125 | + throw new Error('Invalid file list format'); |
| 126 | + } |
| 127 | + |
| 128 | + return files.map((file) => ({ |
| 129 | + id: file.id, |
| 130 | + rawId: file.id, |
| 131 | + parentId: file.parentId, |
| 132 | + name: file.name, |
| 133 | + type: file.type, |
| 134 | + hasChild: file.hasChild ?? file.type === 'folder', |
| 135 | + updateTime: file.updateTime, |
| 136 | + createTime: file.createTime |
| 137 | + })); |
| 138 | + }, |
| 139 | + |
| 140 | + async getFileContent({ config, fileId }): Promise<FileContentResponse> { |
| 141 | + const { baseUrl, authorization } = parseConfig(config); |
| 142 | + |
| 143 | + const data = await apiRequest<APIFileContentResponse>( |
| 144 | + baseUrl, |
| 145 | + authorization, |
| 146 | + '/v1/file/content', |
| 147 | + 'GET', |
| 148 | + { id: fileId } |
| 149 | + ); |
| 150 | + |
| 151 | + const { title, content, previewUrl } = data; |
| 152 | + |
| 153 | + // 如果有直接内容,返回 |
| 154 | + if (content) { |
| 155 | + return { |
| 156 | + title, |
| 157 | + rawText: content |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + // 如果有预览 URL,返回给 FastGPT 去解析 |
| 162 | + if (previewUrl) { |
| 163 | + return { |
| 164 | + title, |
| 165 | + previewUrl |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + throw new Error('Invalid content type: content or previewUrl is required'); |
| 170 | + }, |
| 171 | + |
| 172 | + async getFilePreviewUrl({ config, fileId }): Promise<string> { |
| 173 | + const { baseUrl, authorization } = parseConfig(config); |
| 174 | + |
| 175 | + const data = await apiRequest<APIFileReadResponse>( |
| 176 | + baseUrl, |
| 177 | + authorization, |
| 178 | + '/v1/file/read', |
| 179 | + 'GET', |
| 180 | + { id: fileId } |
| 181 | + ); |
| 182 | + |
| 183 | + if (!data.url || typeof data.url !== 'string') { |
| 184 | + throw new Error('Invalid response url'); |
| 185 | + } |
| 186 | + |
| 187 | + return data.url; |
| 188 | + }, |
| 189 | + |
| 190 | + async getFileDetail({ config, fileId }): Promise<FileItem> { |
| 191 | + const { baseUrl, authorization } = parseConfig(config); |
| 192 | + |
| 193 | + const data = await apiRequest<APIFileDetailResponse>( |
| 194 | + baseUrl, |
| 195 | + authorization, |
| 196 | + '/v1/file/detail', |
| 197 | + 'GET', |
| 198 | + { id: fileId } |
| 199 | + ); |
| 200 | + |
| 201 | + if (!data) { |
| 202 | + throw new Error('File not found'); |
| 203 | + } |
| 204 | + |
| 205 | + return { |
| 206 | + id: data.id, |
| 207 | + rawId: fileId, |
| 208 | + parentId: data.parentId, |
| 209 | + name: data.name, |
| 210 | + type: data.type, |
| 211 | + hasChild: data.type === 'folder', |
| 212 | + updateTime: data.updateTime, |
| 213 | + createTime: data.createTime |
| 214 | + }; |
| 215 | + } |
| 216 | +}; |
| 217 | + |
| 218 | +export default defineSource(customApiConfig, callbacks); |
0 commit comments