@umituz/react-native-ai-generation-content 1.3.0 → 1.5.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.
- package/package.json +5 -2
- package/src/index.ts +26 -0
- package/src/infrastructure/middleware/credit-check.middleware.ts +74 -0
- package/src/infrastructure/middleware/history-tracking.middleware.ts +69 -0
- package/src/infrastructure/middleware/index.ts +15 -0
- package/src/presentation/components/GenerationProgressModal.tsx +166 -0
- package/src/presentation/components/index.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-ai-generation-content",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Provider-agnostic AI generation orchestration for React Native",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -28,13 +28,16 @@
|
|
|
28
28
|
"url": "git+https://github.com/umituz/react-native-ai-generation-content.git"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"react": ">=18.0.0"
|
|
31
|
+
"react": ">=18.0.0",
|
|
32
|
+
"react-native": ">=0.74.0"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"@types/react": "^19.0.0",
|
|
36
|
+
"@types/react-native": "^0.73.0",
|
|
35
37
|
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
36
38
|
"@typescript-eslint/parser": "^7.0.0",
|
|
37
39
|
"eslint": "^8.57.0",
|
|
40
|
+
"react-native": "^0.76.0",
|
|
38
41
|
"typescript": "^5.3.0"
|
|
39
42
|
},
|
|
40
43
|
"publishConfig": {
|
package/src/index.ts
CHANGED
|
@@ -72,6 +72,21 @@ export type {
|
|
|
72
72
|
WrapperConfig,
|
|
73
73
|
} from "./infrastructure/services";
|
|
74
74
|
|
|
75
|
+
// =============================================================================
|
|
76
|
+
// INFRASTRUCTURE LAYER - Middleware Factories
|
|
77
|
+
// =============================================================================
|
|
78
|
+
|
|
79
|
+
export {
|
|
80
|
+
createCreditCheckMiddleware,
|
|
81
|
+
createHistoryTrackingMiddleware,
|
|
82
|
+
} from "./infrastructure/middleware";
|
|
83
|
+
|
|
84
|
+
export type {
|
|
85
|
+
CreditCheckConfig,
|
|
86
|
+
HistoryConfig,
|
|
87
|
+
HistoryEntry,
|
|
88
|
+
} from "./infrastructure/middleware";
|
|
89
|
+
|
|
75
90
|
// =============================================================================
|
|
76
91
|
// INFRASTRUCTURE LAYER - Utils
|
|
77
92
|
// =============================================================================
|
|
@@ -117,3 +132,14 @@ export type {
|
|
|
117
132
|
UseGenerationOptions,
|
|
118
133
|
UseGenerationReturn,
|
|
119
134
|
} from "./presentation/hooks";
|
|
135
|
+
|
|
136
|
+
// =============================================================================
|
|
137
|
+
// PRESENTATION LAYER - Components
|
|
138
|
+
// =============================================================================
|
|
139
|
+
|
|
140
|
+
export { GenerationProgressModal } from "./presentation/components";
|
|
141
|
+
|
|
142
|
+
export type {
|
|
143
|
+
GenerationProgressModalProps,
|
|
144
|
+
GenerationProgressRenderProps,
|
|
145
|
+
} from "./presentation/components";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credit Check Middleware Factory
|
|
3
|
+
* Generic credit checking with app-provided config
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { GenerationMiddleware } from "../../domain/entities";
|
|
7
|
+
|
|
8
|
+
export interface CreditCheckConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Get credit type from operation type
|
|
11
|
+
* App-specific logic
|
|
12
|
+
*/
|
|
13
|
+
getCreditType: (operationType: string) => string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Check if user has available credits
|
|
17
|
+
* App provides implementation
|
|
18
|
+
*/
|
|
19
|
+
checkCredits: (
|
|
20
|
+
userId: string | undefined,
|
|
21
|
+
operationType: string,
|
|
22
|
+
) => Promise<{
|
|
23
|
+
success: boolean;
|
|
24
|
+
error?: string;
|
|
25
|
+
creditType?: string;
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Deduct credits after successful generation
|
|
30
|
+
* App provides implementation
|
|
31
|
+
*/
|
|
32
|
+
deductCredits: (
|
|
33
|
+
userId: string | undefined,
|
|
34
|
+
creditType: string,
|
|
35
|
+
) => Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create credit check middleware
|
|
40
|
+
* Checks credits before generation, deducts after success
|
|
41
|
+
*/
|
|
42
|
+
export function createCreditCheckMiddleware(
|
|
43
|
+
config: CreditCheckConfig,
|
|
44
|
+
): GenerationMiddleware {
|
|
45
|
+
return {
|
|
46
|
+
async beforeGenerate(context) {
|
|
47
|
+
const operationType =
|
|
48
|
+
(context.request.input?.type as string) || "default";
|
|
49
|
+
|
|
50
|
+
const result = await config.checkCredits(
|
|
51
|
+
context.userId,
|
|
52
|
+
operationType,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (!result.success) {
|
|
56
|
+
throw new Error(result.error || "credits_exhausted");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
context.metadata = {
|
|
60
|
+
...context.metadata,
|
|
61
|
+
creditType: result.creditType || config.getCreditType(operationType),
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
async afterGenerate(context) {
|
|
66
|
+
if (context.result.success && context.metadata?.creditType) {
|
|
67
|
+
await config.deductCredits(
|
|
68
|
+
context.userId,
|
|
69
|
+
context.metadata.creditType as string,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History Tracking Middleware Factory
|
|
3
|
+
* Generic history saving with app-provided config
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { GenerationMiddleware } from "../../domain/entities";
|
|
7
|
+
|
|
8
|
+
export interface HistoryEntry {
|
|
9
|
+
type: string;
|
|
10
|
+
prompt: string;
|
|
11
|
+
result: string | null;
|
|
12
|
+
success: boolean;
|
|
13
|
+
error?: string;
|
|
14
|
+
userId: string;
|
|
15
|
+
metadata?: Record<string, unknown>;
|
|
16
|
+
timestamp: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface HistoryConfig {
|
|
20
|
+
/**
|
|
21
|
+
* Save generation to history
|
|
22
|
+
* App provides storage implementation (Firestore, API, etc.)
|
|
23
|
+
*/
|
|
24
|
+
saveToHistory: (entry: HistoryEntry) => Promise<void>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create timestamp for history entry
|
|
28
|
+
* App-specific (serverTimestamp for Firestore, new Date() for API, etc.)
|
|
29
|
+
*/
|
|
30
|
+
createTimestamp: () => unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create history tracking middleware
|
|
35
|
+
* Saves generation history after completion
|
|
36
|
+
*/
|
|
37
|
+
export function createHistoryTrackingMiddleware(
|
|
38
|
+
config: HistoryConfig,
|
|
39
|
+
): GenerationMiddleware {
|
|
40
|
+
return {
|
|
41
|
+
async afterGenerate(context) {
|
|
42
|
+
if (!context.userId) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const operationType =
|
|
48
|
+
(context.request.input?.type as string) || "default";
|
|
49
|
+
const prompt =
|
|
50
|
+
(context.request.input?.prompt as string) || "";
|
|
51
|
+
|
|
52
|
+
const entry: HistoryEntry = {
|
|
53
|
+
type: operationType,
|
|
54
|
+
prompt,
|
|
55
|
+
result: context.result.success ? String(context.result.data) : null,
|
|
56
|
+
success: context.result.success,
|
|
57
|
+
error: context.result.error,
|
|
58
|
+
userId: context.userId,
|
|
59
|
+
metadata: context.result.metadata as Record<string, unknown>,
|
|
60
|
+
timestamp: config.createTimestamp(),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
await config.saveToHistory(entry);
|
|
64
|
+
} catch {
|
|
65
|
+
// Silent fail
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware Factories
|
|
3
|
+
* Generic middleware with app-provided config
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
createCreditCheckMiddleware,
|
|
8
|
+
type CreditCheckConfig,
|
|
9
|
+
} from "./credit-check.middleware";
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
createHistoryTrackingMiddleware,
|
|
13
|
+
type HistoryConfig,
|
|
14
|
+
type HistoryEntry,
|
|
15
|
+
} from "./history-tracking.middleware";
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GenerationProgressModal
|
|
3
|
+
* Generic AI generation progress modal with customizable rendering
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React from "react";
|
|
7
|
+
import { Modal, View, Text, StyleSheet } from "react-native";
|
|
8
|
+
|
|
9
|
+
export interface GenerationProgressModalProps {
|
|
10
|
+
visible: boolean;
|
|
11
|
+
progress: number;
|
|
12
|
+
title?: string;
|
|
13
|
+
message?: string;
|
|
14
|
+
hint?: string;
|
|
15
|
+
overlayColor?: string;
|
|
16
|
+
modalBackgroundColor?: string;
|
|
17
|
+
textColor?: string;
|
|
18
|
+
progressColor?: string;
|
|
19
|
+
progressBackgroundColor?: string;
|
|
20
|
+
renderContent?: (props: GenerationProgressRenderProps) => React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface GenerationProgressRenderProps {
|
|
24
|
+
progress: number;
|
|
25
|
+
title?: string;
|
|
26
|
+
message?: string;
|
|
27
|
+
hint?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const GenerationProgressModal: React.FC<
|
|
31
|
+
GenerationProgressModalProps
|
|
32
|
+
> = ({
|
|
33
|
+
visible,
|
|
34
|
+
progress,
|
|
35
|
+
title,
|
|
36
|
+
message,
|
|
37
|
+
hint,
|
|
38
|
+
overlayColor = "rgba(0, 0, 0, 0.7)",
|
|
39
|
+
modalBackgroundColor = "#1C1C1E",
|
|
40
|
+
textColor = "#FFFFFF",
|
|
41
|
+
progressColor = "#007AFF",
|
|
42
|
+
progressBackgroundColor = "#3A3A3C",
|
|
43
|
+
renderContent,
|
|
44
|
+
}) => {
|
|
45
|
+
const clampedProgress = Math.max(0, Math.min(100, progress));
|
|
46
|
+
|
|
47
|
+
if (renderContent) {
|
|
48
|
+
return (
|
|
49
|
+
<Modal
|
|
50
|
+
visible={visible}
|
|
51
|
+
transparent
|
|
52
|
+
animationType="fade"
|
|
53
|
+
statusBarTranslucent
|
|
54
|
+
>
|
|
55
|
+
<View style={[styles.overlay, { backgroundColor: overlayColor }]}>
|
|
56
|
+
{renderContent({ progress: clampedProgress, title, message, hint })}
|
|
57
|
+
</View>
|
|
58
|
+
</Modal>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<Modal
|
|
64
|
+
visible={visible}
|
|
65
|
+
transparent
|
|
66
|
+
animationType="fade"
|
|
67
|
+
statusBarTranslucent
|
|
68
|
+
>
|
|
69
|
+
<View style={[styles.overlay, { backgroundColor: overlayColor }]}>
|
|
70
|
+
<View
|
|
71
|
+
style={[styles.modal, { backgroundColor: modalBackgroundColor }]}
|
|
72
|
+
>
|
|
73
|
+
{title && (
|
|
74
|
+
<Text style={[styles.title, { color: textColor }]}>{title}</Text>
|
|
75
|
+
)}
|
|
76
|
+
|
|
77
|
+
{message && (
|
|
78
|
+
<Text style={[styles.message, { color: textColor }]}>
|
|
79
|
+
{message}
|
|
80
|
+
</Text>
|
|
81
|
+
)}
|
|
82
|
+
|
|
83
|
+
<View style={styles.progressContainer}>
|
|
84
|
+
<View
|
|
85
|
+
style={[
|
|
86
|
+
styles.progressBackground,
|
|
87
|
+
{ backgroundColor: progressBackgroundColor },
|
|
88
|
+
]}
|
|
89
|
+
>
|
|
90
|
+
<View
|
|
91
|
+
style={[
|
|
92
|
+
styles.progressFill,
|
|
93
|
+
{
|
|
94
|
+
backgroundColor: progressColor,
|
|
95
|
+
width: `${clampedProgress}%`,
|
|
96
|
+
},
|
|
97
|
+
]}
|
|
98
|
+
/>
|
|
99
|
+
</View>
|
|
100
|
+
<Text style={[styles.progressText, { color: textColor }]}>
|
|
101
|
+
{Math.round(clampedProgress)}%
|
|
102
|
+
</Text>
|
|
103
|
+
</View>
|
|
104
|
+
|
|
105
|
+
{hint && (
|
|
106
|
+
<Text style={[styles.hint, { color: textColor }]}>{hint}</Text>
|
|
107
|
+
)}
|
|
108
|
+
</View>
|
|
109
|
+
</View>
|
|
110
|
+
</Modal>
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const styles = StyleSheet.create({
|
|
115
|
+
overlay: {
|
|
116
|
+
flex: 1,
|
|
117
|
+
justifyContent: "center",
|
|
118
|
+
alignItems: "center",
|
|
119
|
+
padding: 20,
|
|
120
|
+
},
|
|
121
|
+
modal: {
|
|
122
|
+
width: "100%",
|
|
123
|
+
maxWidth: 400,
|
|
124
|
+
borderRadius: 24,
|
|
125
|
+
padding: 32,
|
|
126
|
+
alignItems: "center",
|
|
127
|
+
},
|
|
128
|
+
title: {
|
|
129
|
+
fontSize: 20,
|
|
130
|
+
fontWeight: "700",
|
|
131
|
+
marginBottom: 12,
|
|
132
|
+
textAlign: "center",
|
|
133
|
+
},
|
|
134
|
+
message: {
|
|
135
|
+
fontSize: 16,
|
|
136
|
+
marginBottom: 24,
|
|
137
|
+
textAlign: "center",
|
|
138
|
+
opacity: 0.8,
|
|
139
|
+
},
|
|
140
|
+
progressContainer: {
|
|
141
|
+
width: "100%",
|
|
142
|
+
marginBottom: 16,
|
|
143
|
+
alignItems: "center",
|
|
144
|
+
},
|
|
145
|
+
progressBackground: {
|
|
146
|
+
width: "100%",
|
|
147
|
+
height: 8,
|
|
148
|
+
borderRadius: 4,
|
|
149
|
+
overflow: "hidden",
|
|
150
|
+
},
|
|
151
|
+
progressFill: {
|
|
152
|
+
height: "100%",
|
|
153
|
+
borderRadius: 4,
|
|
154
|
+
},
|
|
155
|
+
progressText: {
|
|
156
|
+
fontSize: 14,
|
|
157
|
+
fontWeight: "600",
|
|
158
|
+
marginTop: 8,
|
|
159
|
+
},
|
|
160
|
+
hint: {
|
|
161
|
+
fontSize: 14,
|
|
162
|
+
textAlign: "center",
|
|
163
|
+
fontStyle: "italic",
|
|
164
|
+
opacity: 0.6,
|
|
165
|
+
},
|
|
166
|
+
});
|