@umituz/react-native-ai-generation-content 1.17.170 → 1.17.171

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.
@@ -1,114 +1,41 @@
1
+ /**
2
+ * Text Generation Service
3
+ * AI prompt generation for text content creation
4
+ */
5
+
1
6
  import type { ITextGenerationService } from '../../domain/repositories/IAIPromptServices';
2
- import type {
3
- TextGenerationConfig,
4
- } from '../../domain/entities/TextGenerationConfig';
7
+ import type { TextGenerationConfig } from '../../domain/entities/TextGenerationConfig';
5
8
  import type { AIPromptTemplate } from '../../domain/entities/AIPromptTemplate';
6
- import type { AIPromptResult } from '../../domain/entities/types';
7
- import { createAIPromptTemplate } from '../../domain/entities/AIPromptTemplate';
8
9
  import {
9
10
  validateTextGenerationConfig,
10
11
  getTokenCount,
11
12
  getTemperature,
12
- getTopP
13
+ getTopP,
13
14
  } from '../../domain/entities/TextGenerationConfig';
14
- import { PromptGenerationService } from '../services/PromptGenerationService';
15
-
16
- const createTextGenerationBaseTemplate = (config?: {
17
- promptType?: string;
18
- tone?: string;
19
- language?: string;
20
- }): string => {
21
- const {
22
- promptType = 'general',
23
- tone = 'professional',
24
- language = 'English'
25
- } = config || {};
15
+ import { BasePromptService } from './base';
26
16
 
27
- return `
17
+ const BASE_TEMPLATE = `
28
18
  You are an expert text generation specialist.
29
19
  This is a TEXT GENERATION task with specific requirements.
30
20
 
31
- GENERATION OBJECTIVES:
32
- - Generate ${promptType} content with ${tone} tone
33
- - Write in ${language} language
34
- - Follow provided context and constraints
35
- - Deliver high-quality, appropriate content
36
-
37
21
  CONTENT STANDARDS:
38
22
  - Clear, coherent, and well-structured text
39
23
  - Appropriate for the specified tone and purpose
40
24
  - Natural language flow and readability
41
- - Relevant and informative content
42
-
43
- STYLE REQUIREMENTS:
44
- - Maintain ${tone} tone throughout
45
- - Use appropriate vocabulary and expressions
46
- - Follow conventions for ${promptType} content
47
- - Adapt complexity to intended audience
48
-
49
- LANGUAGE REQUIREMENTS:
50
- - Write fluently in ${language}
51
- - Use proper grammar and syntax
52
- - Consider cultural and regional variations
53
- - Maintain consistency in language usage
54
25
 
55
- SAFETY AND QUALITY:
26
+ SAFETY:
56
27
  - Generate appropriate, non-offensive content
57
28
  - Avoid harmful or misleading information
58
29
  - Ensure factual accuracy when applicable
59
30
  - Respect copyright and intellectual property
60
-
61
- OUTPUT:
62
- Professional ${promptType} content in ${language},
63
- written with ${tone} tone,
64
- meeting all specified requirements.
65
- `.trim();
66
- };
67
-
68
- export class TextGenerationService implements ITextGenerationService {
69
- private promptService: PromptGenerationService;
70
-
71
- constructor() {
72
- this.promptService = new PromptGenerationService();
73
- }
74
-
75
- generateTemplate(config: TextGenerationConfig): Promise<AIPromptResult<AIPromptTemplate>> {
76
- try {
77
- if (!this.validateConfig(config)) {
78
- return Promise.resolve({
79
- success: false,
80
- error: 'VALIDATION_ERROR',
81
- message: 'Invalid text generation configuration'
82
- });
83
- }
84
-
85
- const template = this.createTextGenerationTemplate(config);
86
- return Promise.resolve({ success: true, data: template });
87
- } catch {
88
- return Promise.resolve({
89
- success: false,
90
- error: 'GENERATION_FAILED',
91
- message: 'Failed to generate text template'
92
- });
93
- }
94
- }
95
-
96
- async generatePrompt(
97
- template: AIPromptTemplate,
98
- config: TextGenerationConfig
99
- ): Promise<AIPromptResult<string>> {
100
- const parameters = this.getGenerationParameters(config);
101
- const variables = {
102
- promptType: config.promptType,
103
- tone: config.tone,
104
- length: config.length,
105
- language: config.language,
106
- context: config.context || '',
107
- keywords: config.keywords?.join(', ') || '',
108
- ...parameters,
109
- };
110
-
111
- return this.promptService.generateFromTemplate(template, variables);
31
+ `.trim();
32
+
33
+ export class TextGenerationService
34
+ extends BasePromptService<TextGenerationConfig>
35
+ implements ITextGenerationService
36
+ {
37
+ protected getServiceName(): string {
38
+ return 'text generation';
112
39
  }
113
40
 
114
41
  validateConfig(config: TextGenerationConfig): boolean {
@@ -129,113 +56,35 @@ export class TextGenerationService implements ITextGenerationService {
129
56
  };
130
57
  }
131
58
 
132
- private createTextGenerationTemplate(config: TextGenerationConfig): AIPromptTemplate {
133
- const templateId = `text-generation-${config.promptType}`;
134
-
135
- const baseTemplate = createTextGenerationBaseTemplate({
59
+ protected buildVariables(config: TextGenerationConfig): Record<string, unknown> {
60
+ return {
136
61
  promptType: config.promptType,
137
62
  tone: config.tone,
63
+ length: config.length,
138
64
  language: config.language,
139
- });
65
+ context: config.context || '',
66
+ keywords: config.keywords?.join(', ') || '',
67
+ ...this.getGenerationParameters(config),
68
+ };
69
+ }
70
+
71
+ protected createTemplate(config: TextGenerationConfig): AIPromptTemplate {
72
+ const tokens = this.estimateTokens(config);
140
73
 
141
- return createAIPromptTemplate({
142
- id: templateId,
74
+ return this.createTemplateWithDefaults({
75
+ id: `text-generation-${config.promptType}`,
143
76
  name: `Text Generation: ${config.promptType}`,
144
77
  description: `Generate ${config.promptType} content in ${config.language}`,
145
78
  category: 'text-generation',
146
- template: `${baseTemplate}
147
-
148
- GENERATION PARAMETERS:
149
- - Prompt Type: ${config.promptType}
150
- - Tone: ${config.tone}
151
- - Length: ${config.length}
152
- - Language: ${config.language}
153
- - Estimated Tokens: ${this.estimateTokens(config)}
154
-
155
- ${config.context ? `CONTEXT:
156
- ${config.context}
157
-
158
- ` : ''}${config.keywords && config.keywords.length > 0 ? `KEYWORDS:
159
- ${config.keywords.join(', ')}
160
-
161
- ` : ''}CONTENT REQUIREMENTS:
162
- - Generate ${config.length} content appropriate for ${config.promptType}
163
- - Use ${config.tone} tone throughout the text
164
- - Follow conventions for ${config.promptType} writing
165
- - Write fluently in ${config.language}
166
-
167
- STYLE GUIDELINES:
168
- ${this.getStyleGuidelines(config)}
169
-
170
- GENERATION PARAMETERS:
171
- - Temperature: ${getTemperature(config.promptType, config.tone)} (creativity level)
172
- - Max Tokens: ${this.estimateTokens(config)} (content length)
173
- - Top-P: ${getTopP(config.promptType)} (nucleus sampling)
174
- - Frequency Penalty: ${config.tone === 'formal' ? 0.3 : 0.1} (repetition reduction)
175
- - Presence Penalty: ${config.promptType === 'creative' ? 0.2 : 0.1} (new topic encouragement)
176
-
177
- EXPECTED OUTPUT:
178
- Professional ${config.promptType} content in ${config.language},
179
- approximately ${this.estimateTokens(config)} tokens long,
180
- with ${config.tone} tone and appropriate style.
181
- `.trim(),
182
- variables: [],
183
- safety: {
184
- contentFilter: true,
185
- adultContentFilter: true,
186
- violenceFilter: true,
187
- hateSpeechFilter: true,
188
- copyrightFilter: true,
189
- },
190
- version: '1.0.0',
79
+ template: `${BASE_TEMPLATE}
80
+
81
+ TYPE: ${config.promptType}
82
+ TONE: ${config.tone}
83
+ LENGTH: ${config.length}
84
+ LANGUAGE: ${config.language}
85
+ TOKENS: ~${tokens}
86
+ ${config.context ? `CONTEXT: ${config.context}` : ''}
87
+ ${config.keywords?.length ? `KEYWORDS: ${config.keywords.join(', ')}` : ''}`,
191
88
  });
192
89
  }
193
-
194
- private getStyleGuidelines(config: TextGenerationConfig): string {
195
- const guidelines: string[] = [];
196
-
197
- switch (config.promptType) {
198
- case 'creative':
199
- guidelines.push('- Use imaginative and engaging language');
200
- guidelines.push('- Incorporate vivid descriptions and storytelling');
201
- break;
202
- case 'technical':
203
- guidelines.push('- Use precise terminology and clear explanations');
204
- guidelines.push('- Maintain logical structure and accuracy');
205
- break;
206
- case 'marketing':
207
- guidelines.push('- Use persuasive and compelling language');
208
- guidelines.push('- Include clear calls to action');
209
- break;
210
- case 'educational':
211
- guidelines.push('- Present information clearly and progressively');
212
- guidelines.push('- Include examples and explanations');
213
- break;
214
- case 'conversational':
215
- guidelines.push('- Use natural, approachable language');
216
- guidelines.push('- Include questions and engagement elements');
217
- break;
218
- }
219
-
220
- switch (config.tone) {
221
- case 'formal':
222
- guidelines.push('- Maintain professional and respectful language');
223
- guidelines.push('- Use proper grammar and complete sentences');
224
- break;
225
- case 'casual':
226
- guidelines.push('- Use relaxed, natural language');
227
- guidelines.push('- Include some conversational elements');
228
- break;
229
- case 'friendly':
230
- guidelines.push('- Use warm and approachable tone');
231
- guidelines.push('- Include encouraging and positive elements');
232
- break;
233
- case 'humorous':
234
- guidelines.push('- Incorporate appropriate humor and wit');
235
- guidelines.push('- Maintain lighthearted and engaging style');
236
- break;
237
- }
238
-
239
- return guidelines.join('\n') || '- Follow standard writing conventions';
240
- }
241
- }
90
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Base Services Index
3
+ */
4
+
5
+ export { BasePromptService, DEFAULT_PROMPT_SAFETY } from './prompt-service.base';
6
+ export type { TemplateParams } from './prompt-service.base';
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Base Prompt Service
3
+ * Abstract class for AI prompt generation services
4
+ * Eliminates code duplication across service implementations
5
+ */
6
+
7
+ import type { AIPromptTemplate } from '../../../domain/entities/AIPromptTemplate';
8
+ import type { AIPromptResult, AIPromptCategory } from '../../../domain/entities/types';
9
+ import type { AIPromptSafety } from '../../../domain/entities/value-objects';
10
+ import { createAIPromptTemplate } from '../../../domain/entities/AIPromptTemplate';
11
+ import { PromptGenerationService } from '../PromptGenerationService';
12
+
13
+ /**
14
+ * Default safety configuration for all AI prompt templates
15
+ */
16
+ export const DEFAULT_PROMPT_SAFETY: AIPromptSafety = {
17
+ contentFilter: true,
18
+ adultContentFilter: true,
19
+ violenceFilter: true,
20
+ hateSpeechFilter: true,
21
+ copyrightFilter: true,
22
+ };
23
+
24
+ /**
25
+ * Template creation parameters
26
+ */
27
+ export interface TemplateParams {
28
+ id: string;
29
+ name: string;
30
+ description: string;
31
+ category: AIPromptCategory;
32
+ template: string;
33
+ }
34
+
35
+ /**
36
+ * Base class for AI prompt generation services
37
+ * Provides common functionality for template generation
38
+ */
39
+ export abstract class BasePromptService<TConfig> {
40
+ protected readonly promptService: PromptGenerationService;
41
+
42
+ constructor() {
43
+ this.promptService = new PromptGenerationService();
44
+ }
45
+
46
+ /**
47
+ * Generate template with standard error handling
48
+ */
49
+ generateTemplate(config: TConfig): Promise<AIPromptResult<AIPromptTemplate>> {
50
+ try {
51
+ if (!this.validateConfig(config)) {
52
+ return Promise.resolve({
53
+ success: false,
54
+ error: 'VALIDATION_ERROR',
55
+ message: `Invalid ${this.getServiceName()} configuration`,
56
+ });
57
+ }
58
+
59
+ const template = this.createTemplate(config);
60
+ return Promise.resolve({ success: true, data: template });
61
+ } catch {
62
+ return Promise.resolve({
63
+ success: false,
64
+ error: 'GENERATION_FAILED',
65
+ message: `Failed to generate ${this.getServiceName()} template`,
66
+ });
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Generate prompt from template
72
+ */
73
+ async generatePrompt(
74
+ template: AIPromptTemplate,
75
+ config: TConfig,
76
+ ): Promise<AIPromptResult<string>> {
77
+ const variables = this.buildVariables(config);
78
+ return this.promptService.generateFromTemplate(template, variables);
79
+ }
80
+
81
+ /**
82
+ * Create template with default safety config
83
+ */
84
+ protected createTemplateWithDefaults(params: TemplateParams): AIPromptTemplate {
85
+ return createAIPromptTemplate({
86
+ ...params,
87
+ variables: [],
88
+ safety: DEFAULT_PROMPT_SAFETY,
89
+ version: '1.0.0',
90
+ });
91
+ }
92
+
93
+ /**
94
+ * Service name for error messages
95
+ */
96
+ protected abstract getServiceName(): string;
97
+
98
+ /**
99
+ * Validate configuration
100
+ */
101
+ abstract validateConfig(config: TConfig): boolean;
102
+
103
+ /**
104
+ * Create the template (service-specific)
105
+ */
106
+ protected abstract createTemplate(config: TConfig): AIPromptTemplate;
107
+
108
+ /**
109
+ * Build variables for prompt generation
110
+ */
111
+ protected abstract buildVariables(config: TConfig): Record<string, unknown>;
112
+ }