| 55 | } |
| 56 | |
| 57 | export class FetchImportAdapter implements ImportAdapter { |
| 58 | async importFile( |
| 59 | file: File | string, |
| 60 | options?: ImportOptions, |
| 61 | onProgress?: (p: ImportProgress) => void |
| 62 | ): Promise<ImportResult> { |
| 63 | if (typeof file === 'string') { |
| 64 | return { success: false, error: 'File path import is not supported in Web mode' } |
| 65 | } |
| 66 | |
| 67 | const form = new FormData() |
| 68 | form.append('file', file) |
| 69 | if (options?.formatId) form.append('formatId', options.formatId) |
| 70 | if (options?.chatIndex !== undefined) form.append('chatIndex', String(options.chatIndex)) |
| 71 | |
| 72 | const res = await fetchWithAuth(`${getBaseUrl()}/import`, { method: 'POST', body: form }) |
| 73 | |
| 74 | if (!res.ok) { |
| 75 | const text = await res.text() |
| 76 | return { success: false, error: `HTTP ${res.status}: ${text}` } |
| 77 | } |
| 78 | |
| 79 | return consumeSseStream<ImportResult>(res, { success: false, error: 'Unknown error' }, onProgress) |
| 80 | } |
| 81 | |
| 82 | async detectFormat(file: File | string): Promise<FormatInfo | null> { |
| 83 | if (typeof file === 'string') return null |
| 84 | const form = new FormData() |
| 85 | form.append('file', file) |
| 86 | const res = await fetchWithAuth(`${getBaseUrl()}/detect-format`, { method: 'POST', body: form }) |
| 87 | if (!res.ok) return null |
| 88 | const data = (await res.json()) as { format: FormatInfo | null } |
| 89 | return data.format |
| 90 | } |
| 91 | |
| 92 | async scanMultiChatFile(file: File | string): Promise<MultiChatEntry[]> { |
| 93 | if (typeof file === 'string') return [] |
| 94 | const form = new FormData() |
| 95 | form.append('file', file) |
| 96 | const res = await fetchWithAuth(`${getBaseUrl()}/scan-multi-chat`, { method: 'POST', body: form }) |
| 97 | if (!res.ok) return [] |
| 98 | const data = (await res.json()) as { chats: MultiChatEntry[] } |
| 99 | return data.chats |
| 100 | } |
| 101 | |
| 102 | async prepareImportSource(file: File | string): Promise<PreparedImportSourceResult> { |
| 103 | if (typeof file === 'string') { |
| 104 | return { success: false, error: 'File path import is not supported in Web mode' } |
| 105 | } |
| 106 | |
| 107 | const form = new FormData() |
| 108 | form.append('file', file) |
| 109 | const res = await fetchWithAuth(`${getBaseUrl()}/import-sources`, { method: 'POST', body: form }) |
| 110 | const data = (await res.json().catch(() => null)) as PreparedImportSourceResult | null |
| 111 | if (!res.ok) { |
| 112 | return { success: false, error: data?.error || `HTTP ${res.status}` } |
| 113 | } |
| 114 | return data ?? { success: false, error: 'Invalid import source response' } |
nothing calls this directly
no outgoing calls
no test coverage detected