@publishpress/potomatic 0.0.0-stage → 1.4.5
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/CHANGELOG.md +127 -0
- package/LICENSE.md +20 -0
- package/README.md +470 -2
- package/config/openai-pricing.json +112 -0
- package/config/po-header.json +10 -0
- package/config/prompt.md +23 -0
- package/package.json +84 -5
- package/potomatic +103 -0
- package/src/config/index.js +497 -0
- package/src/logging/index.js +362 -0
- package/src/orchestrator/index.js +1031 -0
- package/src/processors/batchProcessor.js +171 -0
- package/src/processors/languageProcessor.js +1464 -0
- package/src/providers/ProviderFactory.js +124 -0
- package/src/providers/base/Provider.js +198 -0
- package/src/providers/openai/OpenAIProvider.js +805 -0
- package/src/providers/openai/modelCapabilities.js +256 -0
- package/src/reporters/ConsoleReporter.js +256 -0
- package/src/reporters/JsonReporter.js +254 -0
- package/src/reporters/ReporterFactory.js +218 -0
- package/src/reporters/base/BaseReporter.js +124 -0
- package/src/utils/colorUtils.js +93 -0
- package/src/utils/costCalculations.js +245 -0
- package/src/utils/costTracker.js +277 -0
- package/src/utils/dictionaryUtils.js +211 -0
- package/src/utils/languageMapping.js +1113 -0
- package/src/utils/pluralization.js +39 -0
- package/src/utils/poFileUtils.js +599 -0
- package/src/utils/promptLoader.js +115 -0
- package/src/utils/reportingUtils.js +417 -0
- package/src/utils/validationStats.js +110 -0
- package/src/utils/xmlTranslation.js +532 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { OpenAIProvider } from './openai/OpenAIProvider.js';
|
|
2
|
+
import { KNOWN_MODELS } from './openai/modelCapabilities.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates and configures AI translation providers based on configuration.
|
|
6
|
+
* Currently, supports OpenAI with a unified interface for future providers.
|
|
7
|
+
*
|
|
8
|
+
* @since 1.0.0
|
|
9
|
+
*/
|
|
10
|
+
export class ProviderFactory {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a provider instance based on configuration.
|
|
13
|
+
*
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
*
|
|
16
|
+
* @param {Object} config - Configuration object.
|
|
17
|
+
* @param {string} config.provider - Provider name (currently only 'openai').
|
|
18
|
+
* @param {Object} logger - Logger instance.
|
|
19
|
+
*
|
|
20
|
+
* @return {Provider} Configured provider instance.
|
|
21
|
+
*
|
|
22
|
+
* @throws {Error} If provider is unsupported or configuration is invalid.
|
|
23
|
+
*/
|
|
24
|
+
static createProvider(config, logger) {
|
|
25
|
+
const providerName = config.provider || 'openai'; // Default to OpenAI.
|
|
26
|
+
|
|
27
|
+
switch (providerName.toLowerCase()) {
|
|
28
|
+
case 'openai':
|
|
29
|
+
return new OpenAIProvider(config, logger);
|
|
30
|
+
default:
|
|
31
|
+
throw new Error(`Unsupported provider: ${providerName}. ` + `Supported providers: ${ProviderFactory.getSupportedProviders().join(', ')}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets the list of supported provider names.
|
|
37
|
+
*
|
|
38
|
+
* @since 1.0.0
|
|
39
|
+
*
|
|
40
|
+
* @return {Array<string>} Array of supported provider names.
|
|
41
|
+
*/
|
|
42
|
+
static getSupportedProviders() {
|
|
43
|
+
return ['openai'];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Validates that a provider name is supported.
|
|
48
|
+
*
|
|
49
|
+
* @since 1.0.0
|
|
50
|
+
*
|
|
51
|
+
* @param {string} providerName - Provider name to validate.
|
|
52
|
+
*
|
|
53
|
+
* @return {boolean} True if provider is supported.
|
|
54
|
+
*/
|
|
55
|
+
static isProviderSupported(providerName) {
|
|
56
|
+
return ProviderFactory.getSupportedProviders().includes(providerName.toLowerCase());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Gets information about all providers including implementation status.
|
|
61
|
+
*
|
|
62
|
+
* @since 1.0.0
|
|
63
|
+
*
|
|
64
|
+
* @return {Array<Object>} Provider information objects.
|
|
65
|
+
*/
|
|
66
|
+
static getProviderInfo() {
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
name: 'openai',
|
|
70
|
+
displayName: 'OpenAI',
|
|
71
|
+
description: 'OpenAI GPT models (GPT-5.6, GPT-5.4, GPT-4.1, etc.)',
|
|
72
|
+
status: 'implemented',
|
|
73
|
+
models: [...KNOWN_MODELS].sort(),
|
|
74
|
+
configExample: {
|
|
75
|
+
provider: 'openai',
|
|
76
|
+
apiKey: 'your-openai-api-key',
|
|
77
|
+
model: 'gpt-5.4-mini',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Creates a provider and validates its configuration.
|
|
85
|
+
* Provides detailed error messages for easier troubleshooting.
|
|
86
|
+
*
|
|
87
|
+
* @since 1.0.0
|
|
88
|
+
*
|
|
89
|
+
* @param {Object} config - Configuration object.
|
|
90
|
+
* @param {Object} logger - Logger instance.
|
|
91
|
+
*
|
|
92
|
+
* @return {Provider} Validated provider instance.
|
|
93
|
+
*
|
|
94
|
+
* @throws {Error} If provider creation or validation fails.
|
|
95
|
+
*/
|
|
96
|
+
static async createAndValidateProvider(config, logger) {
|
|
97
|
+
// Check if provider is supported.
|
|
98
|
+
const providerName = config.provider || 'openai';
|
|
99
|
+
|
|
100
|
+
if (!ProviderFactory.isProviderSupported(providerName)) {
|
|
101
|
+
const supportedProviders = ProviderFactory.getSupportedProviders().join(', ');
|
|
102
|
+
|
|
103
|
+
throw new Error(`Unsupported provider: ${providerName}. Supported: ${supportedProviders}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const provider = ProviderFactory.createProvider(config, logger);
|
|
107
|
+
|
|
108
|
+
// Initialize provider first to load pricing data and other resources.
|
|
109
|
+
await provider.initialize();
|
|
110
|
+
|
|
111
|
+
// Validate configuration after initialization so model validation has access to pricing data.
|
|
112
|
+
const validation = provider.validateConfig(config);
|
|
113
|
+
|
|
114
|
+
if (!validation.isValid) {
|
|
115
|
+
const errorMessage = validation.errors.join(', ');
|
|
116
|
+
|
|
117
|
+
throw new Error(`Provider configuration invalid: ${errorMessage}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
logger.debug(`Created and validated ${providerName} provider`);
|
|
121
|
+
|
|
122
|
+
return provider;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract Provider Base Class.
|
|
7
|
+
*
|
|
8
|
+
* Defines the interface that all AI translation providers must implement.
|
|
9
|
+
* Provides common functionality and enforces consistent behavior across providers.
|
|
10
|
+
*
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export class Provider {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new Provider instance.
|
|
16
|
+
*
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*
|
|
19
|
+
* @param {Object} config - Provider configuration.
|
|
20
|
+
* @param {Object} logger - Logger instance.
|
|
21
|
+
*/
|
|
22
|
+
constructor(config, logger) {
|
|
23
|
+
if (new.target === Provider) {
|
|
24
|
+
throw new Error('Provider is an abstract class and cannot be instantiated directly');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
this.providerPricing = null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Loads provider-specific pricing from configuration file.
|
|
34
|
+
* This is a generic method that can be used by all providers.
|
|
35
|
+
*
|
|
36
|
+
* @since 1.0.0
|
|
37
|
+
*
|
|
38
|
+
* @param {string} providerName - Name of the provider (e.g., 'openai', 'anthropic')
|
|
39
|
+
*
|
|
40
|
+
* @return {Promise<void>} Resolves when pricing is loaded
|
|
41
|
+
*
|
|
42
|
+
* @protected
|
|
43
|
+
*/
|
|
44
|
+
async _loadProviderPricing(providerName) {
|
|
45
|
+
try {
|
|
46
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
47
|
+
const configDir = path.resolve(currentDir, '../../../config');
|
|
48
|
+
const pricingPath = path.join(configDir, `${providerName}-pricing.json`);
|
|
49
|
+
const pricingContent = fs.readFileSync(pricingPath, 'utf-8');
|
|
50
|
+
|
|
51
|
+
this.providerPricing = JSON.parse(pricingContent);
|
|
52
|
+
|
|
53
|
+
this.logger.debug(`Loaded pricing for ${providerName} provider`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
this.logger.warn(`Could not load ${providerName} pricing: ${error.message}. Using fallback pricing.`);
|
|
56
|
+
|
|
57
|
+
// Set fallback pricing - each provider can override this.
|
|
58
|
+
this.providerPricing = this._getFallbackPricing();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gets fallback pricing when provider pricing file cannot be loaded.
|
|
64
|
+
* Should be overridden by each provider with their specific fallback.
|
|
65
|
+
*
|
|
66
|
+
* @since 1.0.0
|
|
67
|
+
*
|
|
68
|
+
* @return {Object} Fallback pricing structure.
|
|
69
|
+
*
|
|
70
|
+
* @protected
|
|
71
|
+
*/
|
|
72
|
+
_getFallbackPricing() {
|
|
73
|
+
return {
|
|
74
|
+
models: {},
|
|
75
|
+
fallback: { prompt: 0.0005, completion: 0.0015 },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Initializes the provider.
|
|
81
|
+
* Subclasses should override this method to perform provider-specific setup.
|
|
82
|
+
*
|
|
83
|
+
* @since 1.0.0
|
|
84
|
+
*
|
|
85
|
+
* @return {Promise<void>} Resolves when initialization is complete.
|
|
86
|
+
*
|
|
87
|
+
* @throws {Error} If initialization fails.
|
|
88
|
+
*/
|
|
89
|
+
async initialize() {
|
|
90
|
+
throw new Error('initialize() method must be implemented by subclass');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Validates provider configuration.
|
|
95
|
+
* Subclasses should override this method to perform provider-specific validation.
|
|
96
|
+
*
|
|
97
|
+
* @since 1.0.0
|
|
98
|
+
*
|
|
99
|
+
* @param {Object} config - Configuration to validate.
|
|
100
|
+
*
|
|
101
|
+
* @return {Object} Validation result with isValid flag and errors array.
|
|
102
|
+
*/
|
|
103
|
+
validateConfig(config) {
|
|
104
|
+
throw new Error('validateConfig() method must be implemented by subclass');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Translates a batch of strings.
|
|
109
|
+
* Subclasses must implement this method with provider-specific translation logic.
|
|
110
|
+
*
|
|
111
|
+
* @since 1.0.0
|
|
112
|
+
*
|
|
113
|
+
* @param {Array} batch - Array of translation items.
|
|
114
|
+
* @param {string} targetLang - Target language code.
|
|
115
|
+
* @param {string} model - Model to use.
|
|
116
|
+
* @param {string} systemPrompt - System prompt for translation.
|
|
117
|
+
* @param {number} maxRetries - Maximum retry attempts.
|
|
118
|
+
* @param {number} retryDelayMs - Delay between retries.
|
|
119
|
+
* @param {number} timeout - Request timeout.
|
|
120
|
+
* @param {boolean} isDryRun - Whether this is a dry run.
|
|
121
|
+
* @param {Function} retryProgressCallback - Optional callback for retry progress updates.
|
|
122
|
+
*
|
|
123
|
+
* @return {Promise<Object>} Translation result.
|
|
124
|
+
*/
|
|
125
|
+
async translateBatch(batch, targetLang, model, systemPrompt, maxRetries, retryDelayMs, timeout, isDryRun, retryProgressCallback = null) {
|
|
126
|
+
throw new Error('translateBatch() method must be implemented by subclass');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Calculates cost based on usage data.
|
|
131
|
+
* Subclasses should override this method for provider-specific cost calculations.
|
|
132
|
+
*
|
|
133
|
+
* @since 1.0.0
|
|
134
|
+
*
|
|
135
|
+
* @param {Object} usage - Usage data from provider.
|
|
136
|
+
* @param {string} model - Model used.
|
|
137
|
+
*
|
|
138
|
+
* @return {Object} Cost breakdown.
|
|
139
|
+
*/
|
|
140
|
+
calculateCost(usage, model) {
|
|
141
|
+
throw new Error('calculateCost() method must be implemented by subclass');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Gets token count for text.
|
|
146
|
+
* Subclasses should override this method for provider-specific tokenization.
|
|
147
|
+
*
|
|
148
|
+
* @since 1.0.0
|
|
149
|
+
*
|
|
150
|
+
* @param {string} text - Text to count tokens for.
|
|
151
|
+
* @param {string} model - Model to use for tokenization.
|
|
152
|
+
*
|
|
153
|
+
* @return {number} Token count.
|
|
154
|
+
*/
|
|
155
|
+
getTokenCount(text, model) {
|
|
156
|
+
throw new Error('getTokenCount() method must be implemented by subclass');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Gets supported models for this provider.
|
|
161
|
+
* Subclasses should override this method to return provider-specific models.
|
|
162
|
+
*
|
|
163
|
+
* @since 1.0.0
|
|
164
|
+
*
|
|
165
|
+
* @return {Array<string>} Supported model identifiers.
|
|
166
|
+
*/
|
|
167
|
+
getSupportedModels() {
|
|
168
|
+
throw new Error('getSupportedModels() method must be implemented by subclass');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Gets the provider name.
|
|
173
|
+
* Subclasses should override this method to return the provider identifier.
|
|
174
|
+
*
|
|
175
|
+
* @since 1.0.0
|
|
176
|
+
*
|
|
177
|
+
* @return {string} Provider name.
|
|
178
|
+
*/
|
|
179
|
+
getProviderName() {
|
|
180
|
+
throw new Error('getProviderName() method must be implemented by subclass');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Estimates output tokens for dry-run calculations.
|
|
185
|
+
* Subclasses can override this method for provider-specific estimation logic.
|
|
186
|
+
*
|
|
187
|
+
* @since 1.0.0
|
|
188
|
+
*
|
|
189
|
+
* @param {number} inputTokens - Input token count.
|
|
190
|
+
* @param {string} targetLang - Target language.
|
|
191
|
+
*
|
|
192
|
+
* @return {number} Estimated output tokens.
|
|
193
|
+
*/
|
|
194
|
+
estimateOutputTokens(inputTokens, targetLang) {
|
|
195
|
+
// Default conservative estimate.
|
|
196
|
+
return Math.round(inputTokens * 1.3);
|
|
197
|
+
}
|
|
198
|
+
}
|