@umituz/react-native-ai-generation-content 1.3.0 → 1.4.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
CHANGED
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
|
// =============================================================================
|
|
@@ -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";
|