vargai 0.4.0-alpha46 → 0.4.0-alpha47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ai-sdk/providers/fal.ts +50 -2
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from "@ai-sdk/provider";
|
|
13
13
|
import { fal } from "@fal-ai/client";
|
|
14
14
|
import pMap from "p-map";
|
|
15
|
+
import type { CacheStorage } from "../cache";
|
|
15
16
|
import { fileCache } from "../file-cache";
|
|
16
17
|
import type { VideoModelV3, VideoModelV3CallOptions } from "../video-model";
|
|
17
18
|
|
|
@@ -21,7 +22,54 @@ interface PendingRequest {
|
|
|
21
22
|
submitted_at: number;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
const
|
|
25
|
+
const memoryStorage = new Map<string, { value: unknown; expires: number }>();
|
|
26
|
+
|
|
27
|
+
function createMemoryCache(): CacheStorage {
|
|
28
|
+
return {
|
|
29
|
+
async get(key: string) {
|
|
30
|
+
const entry = memoryStorage.get(key);
|
|
31
|
+
if (!entry) return undefined;
|
|
32
|
+
if (entry.expires && Date.now() > entry.expires) {
|
|
33
|
+
memoryStorage.delete(key);
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return entry.value;
|
|
37
|
+
},
|
|
38
|
+
async set(key: string, value: unknown, ttl?: number) {
|
|
39
|
+
memoryStorage.set(key, { value, expires: ttl ? Date.now() + ttl : 0 });
|
|
40
|
+
},
|
|
41
|
+
async delete(key: string) {
|
|
42
|
+
memoryStorage.delete(key);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// TODO: allow passing CacheStorage via providerOptions.fal.cacheStorage for proper serverless support
|
|
48
|
+
function isFilesystemWritable(): boolean {
|
|
49
|
+
try {
|
|
50
|
+
const testPath = `.cache/.write-test-${Date.now()}`;
|
|
51
|
+
Bun.spawnSync(["mkdir", "-p", ".cache"]);
|
|
52
|
+
const result = Bun.spawnSync(["touch", testPath]);
|
|
53
|
+
if (result.exitCode === 0) {
|
|
54
|
+
Bun.spawnSync(["rm", testPath]);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const USE_FILE_CACHE = isFilesystemWritable();
|
|
64
|
+
|
|
65
|
+
function createFalCache(name: string): CacheStorage {
|
|
66
|
+
if (!USE_FILE_CACHE) {
|
|
67
|
+
return createMemoryCache();
|
|
68
|
+
}
|
|
69
|
+
return fileCache({ dir: `.cache/${name}` });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const pendingStorage = createFalCache("fal-pending");
|
|
25
73
|
|
|
26
74
|
const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
27
75
|
const FAL_TIMEOUT_MS = (() => {
|
|
@@ -183,7 +231,7 @@ function detectImageType(bytes: Uint8Array): string | undefined {
|
|
|
183
231
|
return undefined;
|
|
184
232
|
}
|
|
185
233
|
|
|
186
|
-
const uploadCache =
|
|
234
|
+
const uploadCache = createFalCache("fal-uploads");
|
|
187
235
|
|
|
188
236
|
async function fileToUrl(file: ImageModelV3File): Promise<string> {
|
|
189
237
|
if (file.type === "url") return file.url;
|