@umituz/react-native-ai-fal-provider 2.0.0 → 2.0.2
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/exports/domain.ts +59 -0
- package/src/exports/infrastructure.ts +97 -0
- package/src/exports/presentation.ts +23 -0
- package/src/exports/registry.ts +39 -0
- package/src/index.ts +8 -201
- package/src/infrastructure/services/fal-provider.ts +23 -73
- package/src/infrastructure/utils/cost-tracking-executor.util.ts +54 -0
- package/src/infrastructure/utils/index.ts +2 -0
package/package.json
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain Layer Exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
FalConfig,
|
|
7
|
+
FalModel,
|
|
8
|
+
FalModelType,
|
|
9
|
+
FalModelPricing,
|
|
10
|
+
FalJobInput,
|
|
11
|
+
FalJobResult,
|
|
12
|
+
FalLogEntry,
|
|
13
|
+
FalQueueStatus,
|
|
14
|
+
FalSubscribeOptions,
|
|
15
|
+
} from "../domain/entities/fal.types";
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
GenerationCost,
|
|
19
|
+
CostTrackerConfig,
|
|
20
|
+
CostSummary,
|
|
21
|
+
ModelCostInfo,
|
|
22
|
+
} from "../domain/entities/cost-tracking.types";
|
|
23
|
+
|
|
24
|
+
export { FalErrorType } from "../domain/entities/error.types";
|
|
25
|
+
export type {
|
|
26
|
+
FalErrorCategory,
|
|
27
|
+
FalErrorInfo,
|
|
28
|
+
FalErrorMessages,
|
|
29
|
+
} from "../domain/entities/error.types";
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
DEFAULT_TEXT_TO_IMAGE_MODELS,
|
|
33
|
+
DEFAULT_TEXT_TO_VOICE_MODELS,
|
|
34
|
+
DEFAULT_TEXT_TO_VIDEO_MODELS,
|
|
35
|
+
DEFAULT_IMAGE_TO_VIDEO_MODELS,
|
|
36
|
+
getAllDefaultModels,
|
|
37
|
+
getDefaultModelsByType,
|
|
38
|
+
getDefaultModel,
|
|
39
|
+
findModelById,
|
|
40
|
+
} from "../domain/constants/default-models.constants";
|
|
41
|
+
export type { FalModelConfig } from "../domain/constants/default-models.constants";
|
|
42
|
+
|
|
43
|
+
export { FAL_IMAGE_FEATURE_MODELS } from "../domain/constants/feature-models.constants";
|
|
44
|
+
|
|
45
|
+
export type {
|
|
46
|
+
UpscaleOptions,
|
|
47
|
+
PhotoRestoreOptions,
|
|
48
|
+
FaceSwapOptions,
|
|
49
|
+
ImageToImagePromptConfig,
|
|
50
|
+
RemoveBackgroundOptions,
|
|
51
|
+
RemoveObjectOptions,
|
|
52
|
+
ReplaceBackgroundOptions,
|
|
53
|
+
VideoFromImageOptions,
|
|
54
|
+
ModelType,
|
|
55
|
+
ModelSelectionConfig,
|
|
56
|
+
ModelSelectionState,
|
|
57
|
+
ModelSelectionActions,
|
|
58
|
+
UseModelsReturn,
|
|
59
|
+
} from "../domain/types";
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Infrastructure Layer Exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
FalProvider,
|
|
7
|
+
falProvider,
|
|
8
|
+
falModelsService,
|
|
9
|
+
NSFWContentError,
|
|
10
|
+
cancelCurrentFalRequest,
|
|
11
|
+
hasRunningFalRequest,
|
|
12
|
+
} from "../infrastructure/services";
|
|
13
|
+
export type { FalProviderType } from "../infrastructure/services";
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
categorizeFalError,
|
|
17
|
+
falErrorMapper,
|
|
18
|
+
mapFalError,
|
|
19
|
+
isFalErrorRetryable,
|
|
20
|
+
buildSingleImageInput,
|
|
21
|
+
buildDualImageInput,
|
|
22
|
+
buildUpscaleInput,
|
|
23
|
+
buildPhotoRestoreInput,
|
|
24
|
+
buildVideoFromImageInput,
|
|
25
|
+
buildFaceSwapInput,
|
|
26
|
+
buildImageToImageInput,
|
|
27
|
+
buildRemoveBackgroundInput,
|
|
28
|
+
buildRemoveObjectInput,
|
|
29
|
+
buildReplaceBackgroundInput,
|
|
30
|
+
buildHDTouchUpInput,
|
|
31
|
+
} from "../infrastructure/utils";
|
|
32
|
+
|
|
33
|
+
export { CostTracker } from "../infrastructure/utils/cost-tracker";
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
isFalModelType,
|
|
37
|
+
isModelType,
|
|
38
|
+
isFalErrorType,
|
|
39
|
+
isValidBase64Image,
|
|
40
|
+
isValidApiKey,
|
|
41
|
+
isValidModelId,
|
|
42
|
+
isValidPrompt,
|
|
43
|
+
isValidTimeout,
|
|
44
|
+
isValidRetryCount,
|
|
45
|
+
} from "../infrastructure/utils";
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
formatImageDataUri,
|
|
49
|
+
extractBase64,
|
|
50
|
+
getDataUriExtension,
|
|
51
|
+
isImageDataUri,
|
|
52
|
+
uploadToFalStorage,
|
|
53
|
+
uploadMultipleToFalStorage,
|
|
54
|
+
calculateTimeoutWithJitter,
|
|
55
|
+
formatCreditCost,
|
|
56
|
+
truncatePrompt,
|
|
57
|
+
sanitizePrompt,
|
|
58
|
+
buildErrorMessage,
|
|
59
|
+
isDefined,
|
|
60
|
+
removeNullish,
|
|
61
|
+
debounce,
|
|
62
|
+
throttle,
|
|
63
|
+
} from "../infrastructure/utils";
|
|
64
|
+
|
|
65
|
+
export {
|
|
66
|
+
createJobMetadata,
|
|
67
|
+
updateJobMetadata,
|
|
68
|
+
isJobCompleted,
|
|
69
|
+
isJobRunning,
|
|
70
|
+
isJobStale,
|
|
71
|
+
getJobDuration,
|
|
72
|
+
formatJobDuration,
|
|
73
|
+
calculateJobProgress,
|
|
74
|
+
serializeJobMetadata,
|
|
75
|
+
deserializeJobMetadata,
|
|
76
|
+
filterValidJobs,
|
|
77
|
+
sortJobsByCreation,
|
|
78
|
+
getActiveJobs,
|
|
79
|
+
getCompletedJobs,
|
|
80
|
+
} from "../infrastructure/utils";
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
saveJobMetadata,
|
|
84
|
+
loadJobMetadata,
|
|
85
|
+
deleteJobMetadata,
|
|
86
|
+
loadAllJobs,
|
|
87
|
+
cleanupOldJobs,
|
|
88
|
+
getJobsByModel,
|
|
89
|
+
getJobsByStatus,
|
|
90
|
+
updateJobStatus,
|
|
91
|
+
} from "../infrastructure/utils";
|
|
92
|
+
|
|
93
|
+
export type {
|
|
94
|
+
FalJobMetadata,
|
|
95
|
+
IJobStorage,
|
|
96
|
+
InMemoryJobStorage,
|
|
97
|
+
} from "../infrastructure/utils";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presentation Layer Exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
useFalGeneration,
|
|
7
|
+
useModels,
|
|
8
|
+
useModelCapabilities,
|
|
9
|
+
useVideoDurations,
|
|
10
|
+
useVideoResolutions,
|
|
11
|
+
useAspectRatios,
|
|
12
|
+
} from "../presentation/hooks";
|
|
13
|
+
|
|
14
|
+
export type {
|
|
15
|
+
UseFalGenerationOptions,
|
|
16
|
+
UseFalGenerationResult,
|
|
17
|
+
UseModelsProps,
|
|
18
|
+
UseModelCapabilitiesOptions,
|
|
19
|
+
UseModelCapabilitiesReturn,
|
|
20
|
+
UseVideoDurationsReturn,
|
|
21
|
+
UseVideoResolutionsReturn,
|
|
22
|
+
UseAspectRatiosReturn,
|
|
23
|
+
} from "../presentation/hooks";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model Registry Exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
VIDEO_ASPECT_RATIOS,
|
|
7
|
+
IMAGE_ASPECT_RATIOS,
|
|
8
|
+
GLOBAL_CAPABILITIES,
|
|
9
|
+
GLOBAL_DEFAULTS,
|
|
10
|
+
SORA_2_DURATIONS,
|
|
11
|
+
SORA_2_RESOLUTIONS,
|
|
12
|
+
SORA_2_CREDITS_BY_DURATION,
|
|
13
|
+
SORA_2_TEXT_TO_VIDEO,
|
|
14
|
+
SORA_2_IMAGE_TO_VIDEO,
|
|
15
|
+
SORA_2_MODELS,
|
|
16
|
+
MODEL_REGISTRY,
|
|
17
|
+
getModelConfig,
|
|
18
|
+
getModelsByType,
|
|
19
|
+
getEnabledModels,
|
|
20
|
+
getModelCapabilities,
|
|
21
|
+
getModelCreditCost,
|
|
22
|
+
getDefaultModelFromRegistry,
|
|
23
|
+
} from "../registry";
|
|
24
|
+
|
|
25
|
+
export type {
|
|
26
|
+
ModelType as RegistryModelType,
|
|
27
|
+
ModelProvider,
|
|
28
|
+
DurationOption,
|
|
29
|
+
ResolutionOption,
|
|
30
|
+
AspectRatioOption,
|
|
31
|
+
ModelCapabilities,
|
|
32
|
+
ModelPricing,
|
|
33
|
+
ModelDefaults,
|
|
34
|
+
ModelConfig,
|
|
35
|
+
ModelRegistry as ModelRegistryType,
|
|
36
|
+
ResolvedCapabilities,
|
|
37
|
+
GlobalCapabilities,
|
|
38
|
+
GlobalDefaults,
|
|
39
|
+
} from "../registry";
|
package/src/index.ts
CHANGED
|
@@ -3,213 +3,20 @@
|
|
|
3
3
|
* FAL AI provider for React Native - implements IAIProvider interface
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
FalModel,
|
|
9
|
-
FalModelType,
|
|
10
|
-
FalModelPricing,
|
|
11
|
-
FalJobInput,
|
|
12
|
-
FalJobResult,
|
|
13
|
-
FalLogEntry,
|
|
14
|
-
FalQueueStatus,
|
|
15
|
-
FalSubscribeOptions,
|
|
16
|
-
} from "./domain/entities/fal.types";
|
|
6
|
+
// Domain Layer
|
|
7
|
+
export * from "./exports/domain";
|
|
17
8
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
CostTrackerConfig,
|
|
21
|
-
CostSummary,
|
|
22
|
-
ModelCostInfo,
|
|
23
|
-
} from "./domain/entities/cost-tracking.types";
|
|
9
|
+
// Infrastructure Layer
|
|
10
|
+
export * from "./exports/infrastructure";
|
|
24
11
|
|
|
25
|
-
|
|
26
|
-
export
|
|
27
|
-
FalErrorCategory,
|
|
28
|
-
FalErrorInfo,
|
|
29
|
-
FalErrorMessages,
|
|
30
|
-
} from "./domain/entities/error.types";
|
|
12
|
+
// Presentation Layer
|
|
13
|
+
export * from "./exports/presentation";
|
|
31
14
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
DEFAULT_TEXT_TO_VOICE_MODELS,
|
|
35
|
-
DEFAULT_TEXT_TO_VIDEO_MODELS,
|
|
36
|
-
DEFAULT_IMAGE_TO_VIDEO_MODELS,
|
|
37
|
-
getAllDefaultModels,
|
|
38
|
-
getDefaultModelsByType,
|
|
39
|
-
getDefaultModel,
|
|
40
|
-
findModelById,
|
|
41
|
-
} from "./domain/constants/default-models.constants";
|
|
42
|
-
export type { FalModelConfig } from "./domain/constants/default-models.constants";
|
|
43
|
-
|
|
44
|
-
export { FAL_IMAGE_FEATURE_MODELS } from "./domain/constants/feature-models.constants";
|
|
45
|
-
|
|
46
|
-
export {
|
|
47
|
-
FalProvider,
|
|
48
|
-
falProvider,
|
|
49
|
-
falModelsService,
|
|
50
|
-
NSFWContentError,
|
|
51
|
-
cancelCurrentFalRequest,
|
|
52
|
-
hasRunningFalRequest,
|
|
53
|
-
} from "./infrastructure/services";
|
|
54
|
-
export type { FalProviderType } from "./infrastructure/services";
|
|
55
|
-
|
|
56
|
-
export {
|
|
57
|
-
categorizeFalError,
|
|
58
|
-
falErrorMapper,
|
|
59
|
-
mapFalError,
|
|
60
|
-
isFalErrorRetryable,
|
|
61
|
-
buildSingleImageInput,
|
|
62
|
-
buildDualImageInput,
|
|
63
|
-
buildUpscaleInput,
|
|
64
|
-
buildPhotoRestoreInput,
|
|
65
|
-
buildVideoFromImageInput,
|
|
66
|
-
buildFaceSwapInput,
|
|
67
|
-
buildImageToImageInput,
|
|
68
|
-
buildRemoveBackgroundInput,
|
|
69
|
-
buildRemoveObjectInput,
|
|
70
|
-
buildReplaceBackgroundInput,
|
|
71
|
-
buildHDTouchUpInput,
|
|
72
|
-
} from "./infrastructure/utils";
|
|
73
|
-
|
|
74
|
-
export { CostTracker } from "./infrastructure/utils/cost-tracker";
|
|
75
|
-
|
|
76
|
-
export {
|
|
77
|
-
isFalModelType,
|
|
78
|
-
isModelType,
|
|
79
|
-
isFalErrorType,
|
|
80
|
-
isValidBase64Image,
|
|
81
|
-
isValidApiKey,
|
|
82
|
-
isValidModelId,
|
|
83
|
-
isValidPrompt,
|
|
84
|
-
isValidTimeout,
|
|
85
|
-
isValidRetryCount,
|
|
86
|
-
} from "./infrastructure/utils";
|
|
87
|
-
|
|
88
|
-
export {
|
|
89
|
-
formatImageDataUri,
|
|
90
|
-
extractBase64,
|
|
91
|
-
getDataUriExtension,
|
|
92
|
-
isImageDataUri,
|
|
93
|
-
uploadToFalStorage,
|
|
94
|
-
uploadMultipleToFalStorage,
|
|
95
|
-
calculateTimeoutWithJitter,
|
|
96
|
-
formatCreditCost,
|
|
97
|
-
truncatePrompt,
|
|
98
|
-
sanitizePrompt,
|
|
99
|
-
buildErrorMessage,
|
|
100
|
-
isDefined,
|
|
101
|
-
removeNullish,
|
|
102
|
-
debounce,
|
|
103
|
-
throttle,
|
|
104
|
-
} from "./infrastructure/utils";
|
|
105
|
-
|
|
106
|
-
export {
|
|
107
|
-
createJobMetadata,
|
|
108
|
-
updateJobMetadata,
|
|
109
|
-
isJobCompleted,
|
|
110
|
-
isJobRunning,
|
|
111
|
-
isJobStale,
|
|
112
|
-
getJobDuration,
|
|
113
|
-
formatJobDuration,
|
|
114
|
-
calculateJobProgress,
|
|
115
|
-
serializeJobMetadata,
|
|
116
|
-
deserializeJobMetadata,
|
|
117
|
-
filterValidJobs,
|
|
118
|
-
sortJobsByCreation,
|
|
119
|
-
getActiveJobs,
|
|
120
|
-
getCompletedJobs,
|
|
121
|
-
} from "./infrastructure/utils";
|
|
122
|
-
|
|
123
|
-
export {
|
|
124
|
-
saveJobMetadata,
|
|
125
|
-
loadJobMetadata,
|
|
126
|
-
deleteJobMetadata,
|
|
127
|
-
loadAllJobs,
|
|
128
|
-
cleanupOldJobs,
|
|
129
|
-
getJobsByModel,
|
|
130
|
-
getJobsByStatus,
|
|
131
|
-
updateJobStatus,
|
|
132
|
-
} from "./infrastructure/utils";
|
|
133
|
-
|
|
134
|
-
export type {
|
|
135
|
-
FalJobMetadata,
|
|
136
|
-
IJobStorage,
|
|
137
|
-
InMemoryJobStorage,
|
|
138
|
-
} from "./infrastructure/utils";
|
|
139
|
-
|
|
140
|
-
export type {
|
|
141
|
-
UpscaleOptions,
|
|
142
|
-
PhotoRestoreOptions,
|
|
143
|
-
FaceSwapOptions,
|
|
144
|
-
ImageToImagePromptConfig,
|
|
145
|
-
RemoveBackgroundOptions,
|
|
146
|
-
RemoveObjectOptions,
|
|
147
|
-
ReplaceBackgroundOptions,
|
|
148
|
-
VideoFromImageOptions,
|
|
149
|
-
ModelType,
|
|
150
|
-
ModelSelectionConfig,
|
|
151
|
-
ModelSelectionState,
|
|
152
|
-
ModelSelectionActions,
|
|
153
|
-
UseModelsReturn,
|
|
154
|
-
} from "./domain/types";
|
|
155
|
-
|
|
156
|
-
export {
|
|
157
|
-
useFalGeneration,
|
|
158
|
-
useModels,
|
|
159
|
-
useModelCapabilities,
|
|
160
|
-
useVideoDurations,
|
|
161
|
-
useVideoResolutions,
|
|
162
|
-
useAspectRatios,
|
|
163
|
-
} from "./presentation/hooks";
|
|
164
|
-
export type {
|
|
165
|
-
UseFalGenerationOptions,
|
|
166
|
-
UseFalGenerationResult,
|
|
167
|
-
UseModelsProps,
|
|
168
|
-
UseModelCapabilitiesOptions,
|
|
169
|
-
UseModelCapabilitiesReturn,
|
|
170
|
-
UseVideoDurationsReturn,
|
|
171
|
-
UseVideoResolutionsReturn,
|
|
172
|
-
UseAspectRatiosReturn,
|
|
173
|
-
} from "./presentation/hooks";
|
|
15
|
+
// Model Registry
|
|
16
|
+
export * from "./exports/registry";
|
|
174
17
|
|
|
175
18
|
// Init Module Factory
|
|
176
19
|
export {
|
|
177
20
|
createAiProviderInitModule,
|
|
178
21
|
type AiProviderInitModuleConfig,
|
|
179
22
|
} from './init';
|
|
180
|
-
|
|
181
|
-
// Model Registry
|
|
182
|
-
export {
|
|
183
|
-
VIDEO_ASPECT_RATIOS,
|
|
184
|
-
IMAGE_ASPECT_RATIOS,
|
|
185
|
-
GLOBAL_CAPABILITIES,
|
|
186
|
-
GLOBAL_DEFAULTS,
|
|
187
|
-
SORA_2_DURATIONS,
|
|
188
|
-
SORA_2_RESOLUTIONS,
|
|
189
|
-
SORA_2_CREDITS_BY_DURATION,
|
|
190
|
-
SORA_2_TEXT_TO_VIDEO,
|
|
191
|
-
SORA_2_IMAGE_TO_VIDEO,
|
|
192
|
-
SORA_2_MODELS,
|
|
193
|
-
MODEL_REGISTRY,
|
|
194
|
-
getModelConfig,
|
|
195
|
-
getModelsByType,
|
|
196
|
-
getEnabledModels,
|
|
197
|
-
getModelCapabilities,
|
|
198
|
-
getModelCreditCost,
|
|
199
|
-
getDefaultModelFromRegistry,
|
|
200
|
-
} from "./registry";
|
|
201
|
-
export type {
|
|
202
|
-
ModelType as RegistryModelType,
|
|
203
|
-
ModelProvider,
|
|
204
|
-
DurationOption,
|
|
205
|
-
ResolutionOption,
|
|
206
|
-
AspectRatioOption,
|
|
207
|
-
ModelCapabilities,
|
|
208
|
-
ModelPricing,
|
|
209
|
-
ModelDefaults,
|
|
210
|
-
ModelConfig,
|
|
211
|
-
ModelRegistry as ModelRegistryType,
|
|
212
|
-
ResolvedCapabilities,
|
|
213
|
-
GlobalCapabilities,
|
|
214
|
-
GlobalDefaults,
|
|
215
|
-
} from "./registry";
|
|
@@ -12,23 +12,13 @@ import type {
|
|
|
12
12
|
import type { CostTrackerConfig } from "../../domain/entities/cost-tracking.types";
|
|
13
13
|
import { DEFAULT_FAL_CONFIG, FAL_CAPABILITIES } from "./fal-provider.constants";
|
|
14
14
|
import { handleFalSubscription, handleFalRun } from "./fal-provider-subscription";
|
|
15
|
-
import { CostTracker } from "../utils
|
|
15
|
+
import { CostTracker, executeWithCostTracking, preprocessInput } from "../utils";
|
|
16
16
|
import {
|
|
17
17
|
createRequestKey, getExistingRequest, storeRequest,
|
|
18
18
|
removeRequest, cancelAllRequests, hasActiveRequests,
|
|
19
19
|
} from "./request-store";
|
|
20
|
-
import
|
|
21
|
-
|
|
22
|
-
getJobStatus as getJobStatusImpl,
|
|
23
|
-
getJobResult as getJobResultImpl,
|
|
24
|
-
} from "./fal-queue-operations";
|
|
25
|
-
import {
|
|
26
|
-
getImageFeatureModel as getImageFeatureModelImpl,
|
|
27
|
-
getVideoFeatureModel as getVideoFeatureModelImpl,
|
|
28
|
-
buildImageFeatureInput as buildImageFeatureInputImpl,
|
|
29
|
-
buildVideoFeatureInput as buildVideoFeatureInputImpl,
|
|
30
|
-
} from "./fal-feature-models";
|
|
31
|
-
import { preprocessInput } from "../utils/input-preprocessor.util";
|
|
20
|
+
import * as queueOps from "./fal-queue-operations";
|
|
21
|
+
import * as featureModels from "./fal-feature-models";
|
|
32
22
|
|
|
33
23
|
declare const __DEV__: boolean | undefined;
|
|
34
24
|
|
|
@@ -96,17 +86,17 @@ export class FalProvider implements IAIProvider {
|
|
|
96
86
|
|
|
97
87
|
async submitJob(model: string, input: Record<string, unknown>): Promise<JobSubmission> {
|
|
98
88
|
this.validateInit();
|
|
99
|
-
return
|
|
89
|
+
return queueOps.submitJob(model, input);
|
|
100
90
|
}
|
|
101
91
|
|
|
102
92
|
async getJobStatus(model: string, requestId: string): Promise<JobStatus> {
|
|
103
93
|
this.validateInit();
|
|
104
|
-
return
|
|
94
|
+
return queueOps.getJobStatus(model, requestId);
|
|
105
95
|
}
|
|
106
96
|
|
|
107
97
|
async getJobResult<T = unknown>(model: string, requestId: string): Promise<T> {
|
|
108
98
|
this.validateInit();
|
|
109
|
-
return
|
|
99
|
+
return queueOps.getJobResult<T>(model, requestId);
|
|
110
100
|
}
|
|
111
101
|
|
|
112
102
|
async subscribe<T = unknown>(
|
|
@@ -128,35 +118,15 @@ export class FalProvider implements IAIProvider {
|
|
|
128
118
|
}
|
|
129
119
|
|
|
130
120
|
const abortController = new AbortController();
|
|
131
|
-
const operationId = this.costTracker?.startOperation(model, "subscribe");
|
|
132
|
-
// Capture costTracker reference to avoid race condition during reset
|
|
133
121
|
const tracker = this.costTracker;
|
|
134
122
|
|
|
135
|
-
const promise =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
console.warn("[FalProvider] Cost tracking failed:", costError);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return result;
|
|
147
|
-
})
|
|
148
|
-
.catch((error) => {
|
|
149
|
-
// Mark operation as failed in cost tracker
|
|
150
|
-
if (operationId && tracker) {
|
|
151
|
-
try {
|
|
152
|
-
tracker.failOperation(operationId);
|
|
153
|
-
} catch {
|
|
154
|
-
// Silently ignore cost tracking errors on failure path
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
throw error;
|
|
158
|
-
})
|
|
159
|
-
.finally(() => removeRequest(key));
|
|
123
|
+
const promise = executeWithCostTracking({
|
|
124
|
+
tracker,
|
|
125
|
+
model,
|
|
126
|
+
operation: "subscribe",
|
|
127
|
+
execute: () => handleFalSubscription<T>(model, processedInput, options, abortController.signal),
|
|
128
|
+
getRequestId: (res) => res.requestId ?? undefined,
|
|
129
|
+
}).then((res) => res.result).finally(() => removeRequest(key));
|
|
160
130
|
|
|
161
131
|
storeRequest(key, { promise, abortController });
|
|
162
132
|
return promise;
|
|
@@ -165,33 +135,13 @@ export class FalProvider implements IAIProvider {
|
|
|
165
135
|
async run<T = unknown>(model: string, input: Record<string, unknown>, options?: RunOptions): Promise<T> {
|
|
166
136
|
this.validateInit();
|
|
167
137
|
const processedInput = await preprocessInput(input);
|
|
168
|
-
const operationId = this.costTracker?.startOperation(model, "run");
|
|
169
|
-
// Capture costTracker reference to avoid race condition during reset
|
|
170
|
-
const tracker = this.costTracker;
|
|
171
138
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
179
|
-
console.warn("[FalProvider] Cost tracking failed:", costError);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return result;
|
|
184
|
-
} catch (error) {
|
|
185
|
-
// Mark operation as failed in cost tracker
|
|
186
|
-
if (operationId && tracker) {
|
|
187
|
-
try {
|
|
188
|
-
tracker.failOperation(operationId);
|
|
189
|
-
} catch {
|
|
190
|
-
// Silently ignore cost tracking errors on failure path
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
throw error;
|
|
194
|
-
}
|
|
139
|
+
return executeWithCostTracking({
|
|
140
|
+
tracker: this.costTracker,
|
|
141
|
+
model,
|
|
142
|
+
operation: "run",
|
|
143
|
+
execute: () => handleFalRun<T>(model, processedInput, options),
|
|
144
|
+
});
|
|
195
145
|
}
|
|
196
146
|
|
|
197
147
|
reset(): void {
|
|
@@ -209,19 +159,19 @@ export class FalProvider implements IAIProvider {
|
|
|
209
159
|
}
|
|
210
160
|
|
|
211
161
|
getImageFeatureModel(feature: ImageFeatureType): string {
|
|
212
|
-
return
|
|
162
|
+
return featureModels.getImageFeatureModel(this.imageFeatureModels, feature);
|
|
213
163
|
}
|
|
214
164
|
|
|
215
165
|
buildImageFeatureInput(feature: ImageFeatureType, data: ImageFeatureInputData): Record<string, unknown> {
|
|
216
|
-
return
|
|
166
|
+
return featureModels.buildImageFeatureInput(feature, data);
|
|
217
167
|
}
|
|
218
168
|
|
|
219
169
|
getVideoFeatureModel(feature: VideoFeatureType): string {
|
|
220
|
-
return
|
|
170
|
+
return featureModels.getVideoFeatureModel(this.videoFeatureModels, feature);
|
|
221
171
|
}
|
|
222
172
|
|
|
223
173
|
buildVideoFeatureInput(feature: VideoFeatureType, data: VideoFeatureInputData): Record<string, unknown> {
|
|
224
|
-
return
|
|
174
|
+
return featureModels.buildVideoFeatureInput(feature, data);
|
|
225
175
|
}
|
|
226
176
|
}
|
|
227
177
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cost Tracking Executor
|
|
3
|
+
* Wraps operations with cost tracking logic
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { CostTracker } from "./cost-tracker";
|
|
7
|
+
|
|
8
|
+
declare const __DEV__: boolean | undefined;
|
|
9
|
+
|
|
10
|
+
interface ExecuteWithCostTrackingOptions<T> {
|
|
11
|
+
tracker: CostTracker | null;
|
|
12
|
+
model: string;
|
|
13
|
+
operation: string;
|
|
14
|
+
execute: () => Promise<T>;
|
|
15
|
+
getRequestId?: (result: T) => string | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Execute an operation with cost tracking
|
|
20
|
+
* Handles start, complete, and fail operations automatically
|
|
21
|
+
*/
|
|
22
|
+
export async function executeWithCostTracking<T>(
|
|
23
|
+
options: ExecuteWithCostTrackingOptions<T>
|
|
24
|
+
): Promise<T> {
|
|
25
|
+
const { tracker, model, operation, execute, getRequestId } = options;
|
|
26
|
+
|
|
27
|
+
if (!tracker) {
|
|
28
|
+
return execute();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const operationId = tracker.startOperation(model, operation);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const result = await execute();
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const requestId = getRequestId?.(result);
|
|
38
|
+
tracker.completeOperation(operationId, model, operation, requestId);
|
|
39
|
+
} catch (costError) {
|
|
40
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
41
|
+
console.warn("[CostTracking] Failed:", costError);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return result;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
try {
|
|
48
|
+
tracker.failOperation(operationId);
|
|
49
|
+
} catch {
|
|
50
|
+
// Silently ignore cost tracking errors on failure path
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -87,4 +87,6 @@ export type { IJobStorage, InMemoryJobStorage } from "./job-storage";
|
|
|
87
87
|
|
|
88
88
|
export { CostTracker } from "./cost-tracker";
|
|
89
89
|
|
|
90
|
+
export { executeWithCostTracking } from "./cost-tracking-executor.util";
|
|
91
|
+
|
|
90
92
|
export { preprocessInput } from "./input-preprocessor.util";
|