|
| 1 | +import { type ReadableBoxedValues } from "svelte-toolbelt"; |
| 2 | +import { lazyPromise } from "$lib/util"; |
| 3 | + |
1 | 4 | export interface FileSystemEntry { |
2 | 5 | fileName: string; |
3 | 6 | } |
@@ -128,3 +131,118 @@ function filesToDirectory(files: FileList): DirectoryEntry { |
128 | 131 |
|
129 | 132 | return ret; |
130 | 133 | } |
| 134 | + |
| 135 | +export type FileInputMode = "file" | "url" | "text"; |
| 136 | + |
| 137 | +export type MultimodalFileInputValueMetadata = { |
| 138 | + type: FileInputMode; |
| 139 | + name: string; |
| 140 | +}; |
| 141 | + |
| 142 | +export type MultimodalFileInputProps = { |
| 143 | + state?: MultimodalFileInputState | undefined; |
| 144 | + |
| 145 | + label?: string | undefined; |
| 146 | +}; |
| 147 | + |
| 148 | +export type MultimodalFileInputStateProps = { |
| 149 | + state: MultimodalFileInputState | undefined; |
| 150 | +} & ReadableBoxedValues<{ |
| 151 | + label: string; |
| 152 | +}>; |
| 153 | + |
| 154 | +export class MultimodalFileInputState { |
| 155 | + private readonly opts: MultimodalFileInputStateProps; |
| 156 | + mode: FileInputMode = $state("file"); |
| 157 | + text: string = $state(""); |
| 158 | + file: File | undefined = $state(undefined); |
| 159 | + url: string = $state(""); |
| 160 | + private urlResolver = $derived.by(() => { |
| 161 | + let url = this.url; |
| 162 | + if (!url.startsWith("http://") && !url.startsWith("https://")) { |
| 163 | + url = `https://${url}`; |
| 164 | + } |
| 165 | + return lazyPromise(async () => { |
| 166 | + let threw = false; |
| 167 | + try { |
| 168 | + const response = await fetch(url); |
| 169 | + if (!response.ok) { |
| 170 | + threw = true; |
| 171 | + throw new Error(`Failed to fetch from URL: ${url}\nStatus: ${response.status}\nBody:\n${await response.text()}`); |
| 172 | + } |
| 173 | + return await response.blob(); |
| 174 | + } catch (e) { |
| 175 | + if (threw) { |
| 176 | + throw e; |
| 177 | + } |
| 178 | + throw new Error(`Failed to fetch from URL: ${url}\nSome errors, such as those caused by CORS, will only print in the console.\nCause: ${e}`); |
| 179 | + } |
| 180 | + }); |
| 181 | + }); |
| 182 | + dragActive = $state(false); |
| 183 | + |
| 184 | + constructor(opts: MultimodalFileInputStateProps) { |
| 185 | + this.opts = opts; |
| 186 | + if (this.opts.state) { |
| 187 | + this.mode = this.opts.state.mode; |
| 188 | + this.text = this.opts.state.text; |
| 189 | + this.file = this.opts.state.file; |
| 190 | + this.url = this.opts.state.url; |
| 191 | + this.urlResolver = this.opts.state.urlResolver; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + get metadata(): MultimodalFileInputValueMetadata | null { |
| 196 | + const mode = this.mode; |
| 197 | + const label = this.opts.label.current; |
| 198 | + if (mode === "file" && this.file !== undefined) { |
| 199 | + const file = this.file; |
| 200 | + return { type: "file", name: file.name }; |
| 201 | + } else if (mode === "url" && this.url !== "") { |
| 202 | + return { type: "url", name: this.url }; |
| 203 | + } else if (mode === "text" && this.text !== "") { |
| 204 | + return { type: "text", name: `${label} (Text Input)` }; |
| 205 | + } else { |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + async resolve(): Promise<Blob> { |
| 211 | + const mode = this.mode; |
| 212 | + if (mode === "file" && this.file !== undefined) { |
| 213 | + return this.file; |
| 214 | + } else if (mode === "url" && this.url !== "") { |
| 215 | + return this.urlResolver.getValue(); |
| 216 | + } else if (mode === "text" && this.text !== "") { |
| 217 | + return new Blob([this.text], { type: "text/plain" }); |
| 218 | + } else { |
| 219 | + throw Error("No value present"); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + reset() { |
| 224 | + this.text = ""; |
| 225 | + this.file = undefined; |
| 226 | + this.url = ""; |
| 227 | + } |
| 228 | + |
| 229 | + swapState(other: MultimodalFileInputState) { |
| 230 | + const mode = this.mode; |
| 231 | + const text = this.text; |
| 232 | + const file = this.file; |
| 233 | + const url = this.url; |
| 234 | + const urlResolver = this.urlResolver; |
| 235 | + |
| 236 | + this.mode = other.mode; |
| 237 | + this.text = other.text; |
| 238 | + this.file = other.file; |
| 239 | + this.url = other.url; |
| 240 | + this.urlResolver = other.urlResolver; |
| 241 | + |
| 242 | + other.mode = mode; |
| 243 | + other.text = text; |
| 244 | + other.file = file; |
| 245 | + other.url = url; |
| 246 | + other.urlResolver = urlResolver; |
| 247 | + } |
| 248 | +} |
0 commit comments