@umituz/react-native-ai-generation-content 1.17.309 → 1.18.0

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.
Files changed (25) hide show
  1. package/package.json +1 -1
  2. package/src/features/anime-selfie/domain/types/anime-selfie.types.ts +1 -0
  3. package/src/features/anime-selfie/presentation/hooks/useAnimeSelfieFeature.ts +31 -126
  4. package/src/features/hd-touch-up/domain/types/hd-touch-up.types.ts +1 -0
  5. package/src/features/hd-touch-up/presentation/hooks/useHDTouchUpFeature.ts +25 -105
  6. package/src/features/image-to-image/presentation/hooks/useDualImageFeature.ts +111 -75
  7. package/src/features/image-to-image/presentation/hooks/useImageWithPromptFeature.ts +115 -84
  8. package/src/features/image-to-image/presentation/hooks/useSingleImageFeature.ts +93 -69
  9. package/src/features/photo-restoration/domain/types/photo-restore.types.ts +1 -0
  10. package/src/features/photo-restoration/presentation/hooks/usePhotoRestoreFeature.ts +25 -121
  11. package/src/features/remove-object/presentation/hooks/useRemoveObjectFeature.ts +125 -79
  12. package/src/features/shared/dual-image-video/presentation/hooks/useDualImageVideoFeature.ts +106 -64
  13. package/src/index.ts +6 -4
  14. package/src/presentation/hooks/generation/index.ts +19 -0
  15. package/src/presentation/hooks/generation/useImageGeneration.ts +157 -0
  16. package/src/presentation/hooks/generation/useVideoGeneration.ts +107 -0
  17. package/src/presentation/hooks/index.ts +8 -12
  18. package/src/presentation/hooks/base/index.ts +0 -9
  19. package/src/presentation/hooks/base/types.ts +0 -47
  20. package/src/presentation/hooks/base/use-dual-image-feature.ts +0 -170
  21. package/src/presentation/hooks/base/use-image-with-prompt-feature.ts +0 -167
  22. package/src/presentation/hooks/base/use-single-image-feature.ts +0 -154
  23. package/src/presentation/hooks/base/utils/feature-state.factory.ts +0 -133
  24. package/src/presentation/hooks/generation-callbacks.types.ts +0 -42
  25. package/src/presentation/hooks/useGenerationCallbacksBuilder.ts +0 -126
@@ -0,0 +1,157 @@
1
+ /**
2
+ * useImageGeneration Hook
3
+ * Generic image generation hook for ANY image feature
4
+ * Uses centralized orchestrator for credit/error handling
5
+ */
6
+
7
+ import { useMemo, useCallback } from "react";
8
+ import { useGenerationOrchestrator } from "./orchestrator";
9
+ import type { GenerationStrategy, AlertMessages } from "./types";
10
+ import { executeImageFeature } from "../../../infrastructure/services";
11
+ import type { ImageFeatureType } from "../../../domain/interfaces";
12
+ import { createCreationsRepository } from "../../../domains/creations/infrastructure/adapters";
13
+ import type { Creation } from "../../../domains/creations/domain/entities/Creation";
14
+
15
+ /**
16
+ * Generic input for single image features
17
+ */
18
+ export interface SingleImageInput {
19
+ imageBase64: string;
20
+ prompt?: string;
21
+ options?: Record<string, unknown>;
22
+ }
23
+
24
+ /**
25
+ * Generic input for dual image features (face-swap, etc.)
26
+ */
27
+ export interface DualImageInput {
28
+ sourceImageBase64: string;
29
+ targetImageBase64: string;
30
+ options?: Record<string, unknown>;
31
+ }
32
+
33
+ export type ImageGenerationInput = SingleImageInput | DualImageInput;
34
+
35
+ export interface ImageGenerationConfig<TInput extends ImageGenerationInput, TResult> {
36
+ /** Feature type (face-swap, upscale, remove-background, etc.) */
37
+ featureType: ImageFeatureType;
38
+ /** User ID for credit operations */
39
+ userId: string | undefined;
40
+ /** Transform image URL to result type */
41
+ processResult: (imageUrl: string, input: TInput) => TResult;
42
+ /** Build input for executor from generic input */
43
+ buildExecutorInput?: (input: TInput) => {
44
+ imageBase64?: string;
45
+ targetImageBase64?: string;
46
+ prompt?: string;
47
+ options?: Record<string, unknown>;
48
+ };
49
+ /** Optional: Build creation for saving */
50
+ buildCreation?: (result: TResult, input: TInput) => Creation | null;
51
+ /** Credit cost (default: 1) */
52
+ creditCost?: number;
53
+ /** Alert messages for errors */
54
+ alertMessages: AlertMessages;
55
+ /** Callbacks */
56
+ onCreditsExhausted?: () => void;
57
+ onSuccess?: (result: TResult) => void;
58
+ onError?: (error: string) => void;
59
+ }
60
+
61
+ /**
62
+ * Default input builder for single image
63
+ */
64
+ const defaultSingleImageBuilder = (input: SingleImageInput) => ({
65
+ imageBase64: input.imageBase64,
66
+ prompt: input.prompt,
67
+ options: input.options,
68
+ });
69
+
70
+ /**
71
+ * Default input builder for dual image
72
+ */
73
+ const defaultDualImageBuilder = (input: DualImageInput) => ({
74
+ imageBase64: input.sourceImageBase64,
75
+ targetImageBase64: input.targetImageBase64,
76
+ options: input.options,
77
+ });
78
+
79
+ /**
80
+ * Check if input is dual image type
81
+ */
82
+ const isDualImageInput = (input: ImageGenerationInput): input is DualImageInput => {
83
+ return "sourceImageBase64" in input && "targetImageBase64" in input;
84
+ };
85
+
86
+ export const useImageGeneration = <
87
+ TInput extends ImageGenerationInput,
88
+ TResult,
89
+ >(config: ImageGenerationConfig<TInput, TResult>) => {
90
+ const {
91
+ featureType,
92
+ userId,
93
+ processResult,
94
+ buildExecutorInput,
95
+ buildCreation,
96
+ creditCost = 1,
97
+ alertMessages,
98
+ onCreditsExhausted,
99
+ onSuccess,
100
+ onError,
101
+ } = config;
102
+
103
+ const repository = useMemo(
104
+ () => createCreationsRepository("creations"),
105
+ [],
106
+ );
107
+
108
+ const strategy: GenerationStrategy<TInput, TResult> = useMemo(
109
+ () => ({
110
+ execute: async (input, onProgress) => {
111
+ // Build executor input
112
+ const executorInput = buildExecutorInput
113
+ ? buildExecutorInput(input)
114
+ : isDualImageInput(input)
115
+ ? defaultDualImageBuilder(input)
116
+ : defaultSingleImageBuilder(input as SingleImageInput);
117
+
118
+ const result = await executeImageFeature(
119
+ featureType,
120
+ executorInput,
121
+ { onProgress },
122
+ );
123
+
124
+ if (!result.success || !result.imageUrl) {
125
+ throw new Error(result.error || "Image generation failed");
126
+ }
127
+
128
+ return processResult(result.imageUrl, input);
129
+ },
130
+ getCreditCost: () => creditCost,
131
+ save: buildCreation
132
+ ? async (result, uid) => {
133
+ const creation = buildCreation(result, {} as TInput);
134
+ if (creation) {
135
+ await repository.create(uid, creation);
136
+ }
137
+ }
138
+ : undefined,
139
+ }),
140
+ [featureType, processResult, buildExecutorInput, buildCreation, repository, creditCost],
141
+ );
142
+
143
+ const handleError = useCallback(
144
+ (error: { message: string }) => {
145
+ onError?.(error.message);
146
+ },
147
+ [onError],
148
+ );
149
+
150
+ return useGenerationOrchestrator(strategy, {
151
+ userId,
152
+ alertMessages,
153
+ onCreditsExhausted,
154
+ onSuccess: onSuccess as (result: unknown) => void,
155
+ onError: handleError,
156
+ });
157
+ };
@@ -0,0 +1,107 @@
1
+ /**
2
+ * useVideoGeneration Hook
3
+ * Generic video generation hook for dual-image video features (ai-hug, ai-kiss)
4
+ * Uses centralized orchestrator for credit/error handling
5
+ */
6
+
7
+ import { useMemo, useCallback } from "react";
8
+ import { useGenerationOrchestrator } from "./orchestrator";
9
+ import type { GenerationStrategy, AlertMessages } from "./types";
10
+ import { executeVideoFeature } from "../../../infrastructure/services";
11
+ import type { VideoFeatureType } from "../../../domain/interfaces";
12
+ import { createCreationsRepository } from "../../../domains/creations/infrastructure/adapters";
13
+ import type { Creation } from "../../../domains/creations/domain/entities/Creation";
14
+
15
+ /**
16
+ * Input for dual image video features (ai-hug, ai-kiss)
17
+ */
18
+ export interface DualImageVideoInput {
19
+ sourceImageBase64: string;
20
+ targetImageBase64: string;
21
+ }
22
+
23
+ export interface VideoGenerationConfig<TResult> {
24
+ /** Feature type (ai-hug, ai-kiss) */
25
+ featureType: VideoFeatureType;
26
+ /** User ID for credit operations */
27
+ userId: string | undefined;
28
+ /** Transform video URL to result type */
29
+ processResult: (videoUrl: string, input: DualImageVideoInput) => TResult;
30
+ /** Optional: Build creation for saving */
31
+ buildCreation?: (result: TResult, input: DualImageVideoInput) => Creation | null;
32
+ /** Credit cost (default: 1) */
33
+ creditCost?: number;
34
+ /** Alert messages for errors */
35
+ alertMessages: AlertMessages;
36
+ /** Callbacks */
37
+ onCreditsExhausted?: () => void;
38
+ onSuccess?: (result: TResult) => void;
39
+ onError?: (error: string) => void;
40
+ }
41
+
42
+ export const useVideoGeneration = <TResult>(
43
+ config: VideoGenerationConfig<TResult>,
44
+ ) => {
45
+ const {
46
+ featureType,
47
+ userId,
48
+ processResult,
49
+ buildCreation,
50
+ creditCost = 1,
51
+ alertMessages,
52
+ onCreditsExhausted,
53
+ onSuccess,
54
+ onError,
55
+ } = config;
56
+
57
+ const repository = useMemo(
58
+ () => createCreationsRepository("creations"),
59
+ [],
60
+ );
61
+
62
+ const strategy: GenerationStrategy<DualImageVideoInput, TResult> = useMemo(
63
+ () => ({
64
+ execute: async (input, onProgress) => {
65
+ const result = await executeVideoFeature(
66
+ featureType,
67
+ {
68
+ sourceImageBase64: input.sourceImageBase64,
69
+ targetImageBase64: input.targetImageBase64,
70
+ },
71
+ { onProgress },
72
+ );
73
+
74
+ if (!result.success || !result.videoUrl) {
75
+ throw new Error(result.error || "Video generation failed");
76
+ }
77
+
78
+ return processResult(result.videoUrl, input);
79
+ },
80
+ getCreditCost: () => creditCost,
81
+ save: buildCreation
82
+ ? async (result, uid) => {
83
+ const creation = buildCreation(result, {} as DualImageVideoInput);
84
+ if (creation) {
85
+ await repository.create(uid, creation);
86
+ }
87
+ }
88
+ : undefined,
89
+ }),
90
+ [featureType, processResult, buildCreation, repository, creditCost],
91
+ );
92
+
93
+ const handleError = useCallback(
94
+ (error: { message: string }) => {
95
+ onError?.(error.message);
96
+ },
97
+ [onError],
98
+ );
99
+
100
+ return useGenerationOrchestrator(strategy, {
101
+ userId,
102
+ alertMessages,
103
+ onCreditsExhausted,
104
+ onSuccess: onSuccess as (result: unknown) => void,
105
+ onError: handleError,
106
+ });
107
+ };
@@ -2,12 +2,11 @@
2
2
  * Presentation Hooks
3
3
  */
4
4
 
5
- // Base Feature Hooks (Provider-Agnostic)
6
- export * from "./base";
7
-
8
5
  // Generation Orchestrator (Centralized)
9
6
  export {
10
7
  useGenerationOrchestrator,
8
+ useImageGeneration,
9
+ useVideoGeneration,
11
10
  createGenerationError,
12
11
  getAlertMessage,
13
12
  parseError,
@@ -21,6 +20,12 @@ export type {
21
20
  GenerationErrorType,
22
21
  AlertMessages,
23
22
  UseGenerationOrchestratorReturn,
23
+ SingleImageInput,
24
+ DualImageInput,
25
+ ImageGenerationInput,
26
+ ImageGenerationConfig,
27
+ DualImageVideoInput,
28
+ VideoGenerationConfig,
24
29
  } from "./generation";
25
30
 
26
31
  export { useGeneration } from "./use-generation";
@@ -48,15 +53,6 @@ export type {
48
53
  UseGenerationFlowReturn,
49
54
  } from "./useGenerationFlow";
50
55
 
51
- export { useGenerationCallbacksBuilder } from "./useGenerationCallbacksBuilder";
52
- export type {
53
- CreditType,
54
- GenerationExecutionResult,
55
- GenerationCallbacksConfig,
56
- GenerationCallbacks,
57
- UseGenerationCallbacksBuilderOptions,
58
- } from "./generation-callbacks.types";
59
-
60
56
  export { useAIFeatureCallbacks } from "./useAIFeatureCallbacks";
61
57
  export type {
62
58
  AIFeatureCallbacksConfig,
@@ -1,9 +0,0 @@
1
- /**
2
- * Base Feature Hooks
3
- * Provider-agnostic hooks for AI image processing features
4
- */
5
-
6
- export * from "./types";
7
- export * from "./use-single-image-feature";
8
- export * from "./use-dual-image-feature";
9
- export * from "./use-image-with-prompt-feature";
@@ -1,47 +0,0 @@
1
- /**
2
- * Base Feature Hook Types
3
- * Provider-agnostic types for feature hooks
4
- */
5
-
6
- /**
7
- * Result from AI processing
8
- */
9
- export interface FeatureProcessResult {
10
- readonly success: boolean;
11
- readonly outputUrl?: string;
12
- readonly error?: string;
13
- readonly metadata?: Record<string, unknown>;
14
- }
15
-
16
- /**
17
- * Base state shared by all feature hooks
18
- */
19
- export interface BaseFeatureState {
20
- readonly isProcessing: boolean;
21
- readonly progress: number;
22
- readonly error: string | null;
23
- readonly processedUrl: string | null;
24
- }
25
-
26
- /**
27
- * Base actions shared by all feature hooks
28
- */
29
- export interface BaseFeatureActions {
30
- readonly reset: () => void;
31
- readonly clearError: () => void;
32
- }
33
-
34
- /**
35
- * Progress callback type
36
- */
37
- export type OnProgressCallback = (progress: number) => void;
38
-
39
- /**
40
- * Image selection callback - provided by app
41
- */
42
- export type OnSelectImageCallback = () => Promise<string | null>;
43
-
44
- /**
45
- * Save callback - provided by app
46
- */
47
- export type OnSaveCallback = (url: string) => Promise<void>;
@@ -1,170 +0,0 @@
1
- /**
2
- * useDualImageFeature Hook
3
- * Provider-agnostic hook for dual image processing features
4
- * Examples: AI Hug, AI Kiss, Face Swap
5
- */
6
-
7
- import { useCallback, useState } from "react";
8
- import type {
9
- BaseFeatureState,
10
- BaseFeatureActions,
11
- FeatureProcessResult,
12
- OnProgressCallback,
13
- OnSelectImageCallback,
14
- OnSaveCallback,
15
- } from "./types";
16
- import { createFeatureStateHandlers, executeProcess, executeSave } from "./utils/feature-state.factory";
17
-
18
- /**
19
- * Request passed to processRequest callback
20
- */
21
- export interface DualImageProcessRequest {
22
- readonly firstImageUri: string;
23
- readonly secondImageUri: string;
24
- readonly onProgress: OnProgressCallback;
25
- }
26
-
27
- /**
28
- * Configuration for dual image feature
29
- */
30
- export interface UseDualImageFeatureConfig {
31
- readonly onSelectFirstImage: OnSelectImageCallback;
32
- readonly onSelectSecondImage: OnSelectImageCallback;
33
- readonly processRequest: (
34
- request: DualImageProcessRequest,
35
- ) => Promise<FeatureProcessResult>;
36
- readonly onSave?: OnSaveCallback;
37
- readonly onError?: (error: string) => void;
38
- readonly onSuccess?: (url: string) => void;
39
- }
40
-
41
- /**
42
- * State for dual image feature
43
- */
44
- export interface DualImageFeatureState extends BaseFeatureState {
45
- readonly firstImageUri: string | null;
46
- readonly secondImageUri: string | null;
47
- }
48
-
49
- /**
50
- * Return type for dual image feature hook
51
- */
52
- export interface UseDualImageFeatureReturn
53
- extends DualImageFeatureState,
54
- BaseFeatureActions {
55
- readonly selectFirstImage: () => Promise<void>;
56
- readonly selectSecondImage: () => Promise<void>;
57
- readonly process: () => Promise<void>;
58
- readonly save: () => Promise<void>;
59
- }
60
-
61
- const initialState: DualImageFeatureState = {
62
- firstImageUri: null,
63
- secondImageUri: null,
64
- processedUrl: null,
65
- isProcessing: false,
66
- progress: 0,
67
- error: null,
68
- };
69
-
70
- export function useDualImageFeature(
71
- config: UseDualImageFeatureConfig,
72
- ): UseDualImageFeatureReturn {
73
- const [state, setState] = useState<DualImageFeatureState>(initialState);
74
-
75
- const { reset, clearError } = createFeatureStateHandlers({
76
- setState,
77
- initialState,
78
- });
79
-
80
- const selectFirstImage = useCallback(async (): Promise<void> => {
81
- try {
82
- const uri = await config.onSelectFirstImage();
83
- if (uri) {
84
- setState((prev) => ({
85
- ...prev,
86
- firstImageUri: uri,
87
- error: null,
88
- processedUrl: null,
89
- }));
90
- }
91
- } catch (err) {
92
- const message = err instanceof Error ? err.message : "error.selectImage";
93
- setState((prev) => ({ ...prev, error: message }));
94
- config.onError?.(message);
95
- }
96
- }, [config]);
97
-
98
- const selectSecondImage = useCallback(async (): Promise<void> => {
99
- try {
100
- const uri = await config.onSelectSecondImage();
101
- if (uri) {
102
- setState((prev) => ({
103
- ...prev,
104
- secondImageUri: uri,
105
- error: null,
106
- processedUrl: null,
107
- }));
108
- }
109
- } catch (err) {
110
- const message = err instanceof Error ? err.message : "error.selectImage";
111
- setState((prev) => ({ ...prev, error: message }));
112
- config.onError?.(message);
113
- }
114
- }, [config]);
115
-
116
- const process = useCallback(async (): Promise<void> => {
117
- if (!state.firstImageUri || !state.secondImageUri) {
118
- const message = "error.noImages";
119
- setState((prev) => ({ ...prev, error: message }));
120
- config.onError?.(message);
121
- return;
122
- }
123
-
124
- const result = await executeProcess<FeatureProcessResult>({
125
- canProcess: () => !!state.firstImageUri && !!state.secondImageUri,
126
- setError: (error: string | null) => setState((prev) => ({ ...prev, error })),
127
- setProcessing: (isProcessing: boolean) => setState((prev) => ({ ...prev, isProcessing })),
128
- onError: config.onError,
129
- processFn: () =>
130
- config.processRequest({
131
- firstImageUri: state.firstImageUri!,
132
- secondImageUri: state.secondImageUri!,
133
- onProgress: (progress) => setState((prev) => ({ ...prev, progress })),
134
- }),
135
- onSuccess: (result: FeatureProcessResult) => {
136
- if (result.outputUrl) {
137
- setState((prev) => ({ ...prev, processedUrl: result.outputUrl ?? null }));
138
- config.onSuccess?.(result.outputUrl);
139
- } else {
140
- const message = result.error || "error.processing";
141
- setState((prev) => ({ ...prev, error: message }));
142
- config.onError?.(message);
143
- }
144
- },
145
- });
146
-
147
- if (!result) {
148
- setState((prev) => ({ ...prev, progress: 0 }));
149
- }
150
- }, [state.firstImageUri, state.secondImageUri, config]);
151
-
152
- const save = useCallback(async (): Promise<void> => {
153
- await executeSave({
154
- processedUrl: state.processedUrl,
155
- onSave: config.onSave,
156
- setError: (error) => setState((prev) => ({ ...prev, error })),
157
- onError: config.onError,
158
- });
159
- }, [state.processedUrl, config]);
160
-
161
- return {
162
- ...state,
163
- selectFirstImage,
164
- selectSecondImage,
165
- process,
166
- save,
167
- reset,
168
- clearError,
169
- };
170
- }
@@ -1,167 +0,0 @@
1
- /**
2
- * useImageWithPromptFeature Hook
3
- * Provider-agnostic hook for image + prompt processing features
4
- * Examples: Inpainting, Style Transfer, Background Replacement
5
- */
6
-
7
- import { useCallback, useState } from "react";
8
- import type {
9
- BaseFeatureState,
10
- BaseFeatureActions,
11
- FeatureProcessResult,
12
- OnProgressCallback,
13
- OnSelectImageCallback,
14
- OnSaveCallback,
15
- } from "./types";
16
- import { createFeatureStateHandlers, executeProcess, executeSave } from "./utils/feature-state.factory";
17
-
18
- /**
19
- * Request passed to processRequest callback
20
- */
21
- export interface ImageWithPromptProcessRequest {
22
- readonly imageUri: string;
23
- readonly prompt: string;
24
- readonly onProgress: OnProgressCallback;
25
- }
26
-
27
- /**
28
- * Configuration for image with prompt feature
29
- */
30
- export interface UseImageWithPromptFeatureConfig {
31
- readonly onSelectImage: OnSelectImageCallback;
32
- readonly processRequest: (
33
- request: ImageWithPromptProcessRequest
34
- ) => Promise<FeatureProcessResult>;
35
- readonly onSave?: OnSaveCallback;
36
- readonly onError?: (error: string) => void;
37
- readonly onSuccess?: (url: string) => void;
38
- readonly requirePrompt?: boolean;
39
- }
40
-
41
- /**
42
- * State for image with prompt feature
43
- */
44
- export interface ImageWithPromptFeatureState extends BaseFeatureState {
45
- readonly imageUri: string | null;
46
- readonly prompt: string;
47
- }
48
-
49
- /**
50
- * Return type for image with prompt feature hook
51
- */
52
- export interface UseImageWithPromptFeatureReturn
53
- extends ImageWithPromptFeatureState,
54
- BaseFeatureActions {
55
- readonly selectImage: () => Promise<void>;
56
- readonly setPrompt: (prompt: string) => void;
57
- readonly process: () => Promise<void>;
58
- readonly save: () => Promise<void>;
59
- }
60
-
61
- const initialState: ImageWithPromptFeatureState = {
62
- imageUri: null,
63
- prompt: "",
64
- processedUrl: null,
65
- isProcessing: false,
66
- progress: 0,
67
- error: null,
68
- };
69
-
70
- export function useImageWithPromptFeature(
71
- config: UseImageWithPromptFeatureConfig,
72
- ): UseImageWithPromptFeatureReturn {
73
- const [state, setState] = useState<ImageWithPromptFeatureState>(initialState);
74
-
75
- const { reset, clearError } = createFeatureStateHandlers({
76
- setState,
77
- initialState,
78
- });
79
-
80
- const selectImage = useCallback(async (): Promise<void> => {
81
- try {
82
- const uri = await config.onSelectImage();
83
- if (uri) {
84
- setState((prev) => ({
85
- ...prev,
86
- imageUri: uri,
87
- error: null,
88
- processedUrl: null,
89
- }));
90
- }
91
- } catch (err) {
92
- const message = err instanceof Error ? err.message : "error.selectImage";
93
- setState((prev) => ({ ...prev, error: message }));
94
- config.onError?.(message);
95
- }
96
- }, [config]);
97
-
98
- const setPrompt = useCallback((prompt: string) => {
99
- setState((prev) => ({ ...prev, prompt }));
100
- }, []);
101
-
102
- const process = useCallback(async (): Promise<void> => {
103
- if (!state.imageUri) {
104
- const message = "error.noImage";
105
- setState((prev) => ({ ...prev, error: message }));
106
- config.onError?.(message);
107
- return;
108
- }
109
-
110
- if (config.requirePrompt && !state.prompt.trim()) {
111
- const message = "error.noPrompt";
112
- setState((prev) => ({ ...prev, error: message }));
113
- config.onError?.(message);
114
- return;
115
- }
116
-
117
- const result = await executeProcess<FeatureProcessResult>({
118
- canProcess: () => {
119
- if (!state.imageUri) return false;
120
- if (config.requirePrompt) return !!state.prompt.trim();
121
- return true;
122
- },
123
- setError: (error: string | null) => setState((prev) => ({ ...prev, error })),
124
- setProcessing: (isProcessing: boolean) => setState((prev) => ({ ...prev, isProcessing })),
125
- onError: config.onError,
126
- processFn: () =>
127
- config.processRequest({
128
- imageUri: state.imageUri!,
129
- prompt: state.prompt.trim(),
130
- onProgress: (progress) => setState((prev) => ({ ...prev, progress })),
131
- }),
132
- onSuccess: (result: FeatureProcessResult) => {
133
- if (result.outputUrl) {
134
- setState((prev) => ({ ...prev, processedUrl: result.outputUrl ?? null }));
135
- config.onSuccess?.(result.outputUrl);
136
- } else {
137
- const message = result.error || "error.processing";
138
- setState((prev) => ({ ...prev, error: message }));
139
- config.onError?.(message);
140
- }
141
- },
142
- });
143
-
144
- if (!result) {
145
- setState((prev) => ({ ...prev, progress: 0 }));
146
- }
147
- }, [state.imageUri, state.prompt, config]);
148
-
149
- const save = useCallback(async (): Promise<void> => {
150
- await executeSave({
151
- processedUrl: state.processedUrl,
152
- onSave: config.onSave,
153
- setError: (error) => setState((prev) => ({ ...prev, error })),
154
- onError: config.onError,
155
- });
156
- }, [state.processedUrl, config]);
157
-
158
- return {
159
- ...state,
160
- selectImage,
161
- setPrompt,
162
- process,
163
- save,
164
- reset,
165
- clearError,
166
- };
167
- }