@djangocfg/llm 2.1.164
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/README.md +181 -0
- package/dist/index.cjs +1164 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +164 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.mjs +1128 -0
- package/dist/index.mjs.map +1 -0
- package/dist/providers/index.cjs +317 -0
- package/dist/providers/index.cjs.map +1 -0
- package/dist/providers/index.d.cts +30 -0
- package/dist/providers/index.d.ts +30 -0
- package/dist/providers/index.mjs +304 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/sdkrouter-D8GMBmTi.d.ts +171 -0
- package/dist/sdkrouter-hlQlVd0v.d.cts +171 -0
- package/dist/text-utils-DoYqMIr6.d.ts +289 -0
- package/dist/text-utils-VXWN-8Oq.d.cts +289 -0
- package/dist/translator/index.cjs +794 -0
- package/dist/translator/index.cjs.map +1 -0
- package/dist/translator/index.d.cts +24 -0
- package/dist/translator/index.d.ts +24 -0
- package/dist/translator/index.mjs +769 -0
- package/dist/translator/index.mjs.map +1 -0
- package/dist/types-D6lazgm1.d.cts +59 -0
- package/dist/types-D6lazgm1.d.ts +59 -0
- package/package.json +82 -0
- package/src/client.ts +119 -0
- package/src/index.ts +70 -0
- package/src/providers/anthropic.ts +98 -0
- package/src/providers/base.ts +90 -0
- package/src/providers/index.ts +15 -0
- package/src/providers/openai.ts +73 -0
- package/src/providers/sdkrouter.ts +279 -0
- package/src/translator/cache.ts +237 -0
- package/src/translator/index.ts +55 -0
- package/src/translator/json-translator.ts +408 -0
- package/src/translator/prompts.ts +90 -0
- package/src/translator/text-utils.ts +148 -0
- package/src/translator/types.ts +112 -0
- package/src/translator/validator.ts +181 -0
- package/src/types.ts +85 -0
- package/src/utils/env.ts +67 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/json.ts +44 -0
- package/src/utils/schema.ts +153 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { c as LLMRequestOptions, a as LLMClient } from './types-D6lazgm1.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Translation Cache Manager
|
|
5
|
+
*
|
|
6
|
+
* Two-level caching: memory + localStorage/file
|
|
7
|
+
* Organized by language pairs for efficient lookup
|
|
8
|
+
*/
|
|
9
|
+
interface CacheStats$1 {
|
|
10
|
+
memorySize: number;
|
|
11
|
+
hits: number;
|
|
12
|
+
misses: number;
|
|
13
|
+
languagePairs: Array<{
|
|
14
|
+
pair: string;
|
|
15
|
+
translations: number;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Translation cache with memory + persistent storage
|
|
20
|
+
*/
|
|
21
|
+
declare class TranslationCache {
|
|
22
|
+
private maxMemorySize;
|
|
23
|
+
private storage?;
|
|
24
|
+
private memoryCache;
|
|
25
|
+
private cacheOrder;
|
|
26
|
+
private hits;
|
|
27
|
+
private misses;
|
|
28
|
+
constructor(maxMemorySize?: number, storage?: Storage);
|
|
29
|
+
/**
|
|
30
|
+
* Generate hash for text
|
|
31
|
+
*/
|
|
32
|
+
private getTextHash;
|
|
33
|
+
/**
|
|
34
|
+
* Get storage key for language pair
|
|
35
|
+
*/
|
|
36
|
+
private getStorageKey;
|
|
37
|
+
/**
|
|
38
|
+
* Load from persistent storage
|
|
39
|
+
*/
|
|
40
|
+
private loadFromStorage;
|
|
41
|
+
/**
|
|
42
|
+
* Save to persistent storage
|
|
43
|
+
*/
|
|
44
|
+
private saveToStorage;
|
|
45
|
+
/**
|
|
46
|
+
* Evict oldest entries if memory is full
|
|
47
|
+
*/
|
|
48
|
+
private evictIfNeeded;
|
|
49
|
+
/**
|
|
50
|
+
* Get translation from cache
|
|
51
|
+
*/
|
|
52
|
+
get(text: string, sourceLang: string, targetLang: string): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Store translation in cache
|
|
55
|
+
*/
|
|
56
|
+
set(text: string, sourceLang: string, targetLang: string, translation: string): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get multiple translations at once
|
|
59
|
+
*/
|
|
60
|
+
getMany(texts: string[], sourceLang: string, targetLang: string): {
|
|
61
|
+
cached: Map<string, string>;
|
|
62
|
+
uncached: string[];
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Store multiple translations at once
|
|
66
|
+
*/
|
|
67
|
+
setMany(translations: Map<string, string>, sourceLang: string, targetLang: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* Clear cache
|
|
70
|
+
*/
|
|
71
|
+
clear(sourceLang?: string, targetLang?: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Get cache statistics
|
|
74
|
+
*/
|
|
75
|
+
getStats(): CacheStats$1;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create translation cache
|
|
79
|
+
*/
|
|
80
|
+
declare function createCache(maxMemorySize?: number, storage?: Storage): TranslationCache;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Translator types
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Language code
|
|
88
|
+
*/
|
|
89
|
+
type LanguageCode = 'en' | 'ru' | 'ko' | 'ja' | 'zh' | 'de' | 'fr' | 'es' | 'it' | 'pt' | 'pt-BR' | 'ar' | 'nl' | 'tr' | 'pl' | 'sv' | 'no' | 'da' | 'uk' | 'hi' | string;
|
|
90
|
+
/**
|
|
91
|
+
* Language names map
|
|
92
|
+
*/
|
|
93
|
+
declare const LANGUAGE_NAMES: Record<string, string>;
|
|
94
|
+
/**
|
|
95
|
+
* Translation options
|
|
96
|
+
*/
|
|
97
|
+
interface TranslateOptions extends LLMRequestOptions {
|
|
98
|
+
/** Source language (default: 'en') */
|
|
99
|
+
sourceLanguage?: LanguageCode;
|
|
100
|
+
/** Max retries on validation failure */
|
|
101
|
+
maxRetries?: number;
|
|
102
|
+
/** Preserve placeholders like {name}, {{var}} */
|
|
103
|
+
preservePlaceholders?: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Translation result
|
|
107
|
+
*/
|
|
108
|
+
interface TranslateResult<T> {
|
|
109
|
+
/** Translated data */
|
|
110
|
+
data: T;
|
|
111
|
+
/** Whether translation is valid (keys preserved) */
|
|
112
|
+
valid: boolean;
|
|
113
|
+
/** Validation errors if any */
|
|
114
|
+
errors: string[];
|
|
115
|
+
/** Number of retries used */
|
|
116
|
+
retries: number;
|
|
117
|
+
/** Source language */
|
|
118
|
+
sourceLanguage: string;
|
|
119
|
+
/** Target language */
|
|
120
|
+
targetLanguage: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Batch translation item
|
|
124
|
+
*/
|
|
125
|
+
interface BatchTranslateItem<T> {
|
|
126
|
+
/** Data to translate */
|
|
127
|
+
data: T;
|
|
128
|
+
/** Target language */
|
|
129
|
+
targetLanguage: LanguageCode;
|
|
130
|
+
/** Options override */
|
|
131
|
+
options?: TranslateOptions;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Cache stats
|
|
135
|
+
*/
|
|
136
|
+
interface CacheStats {
|
|
137
|
+
memorySize: number;
|
|
138
|
+
hits: number;
|
|
139
|
+
misses: number;
|
|
140
|
+
languagePairs: Array<{
|
|
141
|
+
pair: string;
|
|
142
|
+
translations: number;
|
|
143
|
+
}>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* JSON Translator class with smart caching
|
|
148
|
+
*/
|
|
149
|
+
declare class JsonTranslator {
|
|
150
|
+
private client;
|
|
151
|
+
private cache;
|
|
152
|
+
private defaultModel;
|
|
153
|
+
constructor(client: LLMClient, cache?: TranslationCache, defaultModel?: string);
|
|
154
|
+
/**
|
|
155
|
+
* Detect language from text
|
|
156
|
+
*/
|
|
157
|
+
detectLanguage(text: string): LanguageCode;
|
|
158
|
+
/**
|
|
159
|
+
* Check if text needs translation
|
|
160
|
+
*/
|
|
161
|
+
needsTranslation(text: string, sourceLang: string, targetLang: string): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Translate single text
|
|
164
|
+
*/
|
|
165
|
+
translateText(text: string, targetLanguage: LanguageCode, options?: TranslateOptions): Promise<string>;
|
|
166
|
+
/**
|
|
167
|
+
* Translate JSON object with smart text-level caching
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* const translator = new JsonTranslator(llm)
|
|
172
|
+
* const result = await translator.translate(
|
|
173
|
+
* { title: 'Hello', items: ['World', 'Earth'] },
|
|
174
|
+
* 'ru'
|
|
175
|
+
* )
|
|
176
|
+
* // { data: { title: 'Привет', items: ['Мир', 'Земля'] }, valid: true, ... }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
translate<T extends Record<string, unknown>>(data: T, targetLanguage: LanguageCode, options?: TranslateOptions): Promise<TranslateResult<T>>;
|
|
180
|
+
/**
|
|
181
|
+
* Translate to multiple languages in parallel
|
|
182
|
+
*/
|
|
183
|
+
translateToMany<T extends Record<string, unknown>>(data: T, targetLanguages: LanguageCode[], options?: TranslateOptions): Promise<Map<LanguageCode, TranslateResult<T>>>;
|
|
184
|
+
/**
|
|
185
|
+
* Extract all translatable texts from object
|
|
186
|
+
*/
|
|
187
|
+
private extractTranslatableTexts;
|
|
188
|
+
/**
|
|
189
|
+
* Apply translations to object
|
|
190
|
+
*/
|
|
191
|
+
private applyTranslations;
|
|
192
|
+
/**
|
|
193
|
+
* Create partial JSON with only texts that need translation
|
|
194
|
+
*/
|
|
195
|
+
private createPartialJson;
|
|
196
|
+
/**
|
|
197
|
+
* Check if object contains translatable text
|
|
198
|
+
*/
|
|
199
|
+
private hasTranslatable;
|
|
200
|
+
/**
|
|
201
|
+
* Extract translations by comparing original and translated JSON
|
|
202
|
+
*/
|
|
203
|
+
private extractTranslationsByComparison;
|
|
204
|
+
/**
|
|
205
|
+
* Get translation statistics
|
|
206
|
+
*/
|
|
207
|
+
getStats(): CacheStats$1;
|
|
208
|
+
/**
|
|
209
|
+
* Clear translation cache
|
|
210
|
+
*/
|
|
211
|
+
clearCache(sourceLang?: string, targetLang?: string): void;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Translator options
|
|
215
|
+
*/
|
|
216
|
+
interface TranslatorConfig {
|
|
217
|
+
cache?: TranslationCache;
|
|
218
|
+
/** Model for translation (default: openai/gpt-4o-mini) */
|
|
219
|
+
model?: string;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Create JSON translator
|
|
223
|
+
*/
|
|
224
|
+
declare function createTranslator(client: LLMClient, configOrCache?: TranslationCache | TranslatorConfig): JsonTranslator;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Translation validation
|
|
228
|
+
*/
|
|
229
|
+
interface ValidationResult {
|
|
230
|
+
valid: boolean;
|
|
231
|
+
errors: string[];
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Validate that JSON keys are not translated
|
|
235
|
+
*/
|
|
236
|
+
declare function validateJsonKeys(original: unknown, translated: unknown, path?: string): ValidationResult;
|
|
237
|
+
/**
|
|
238
|
+
* Validate that placeholders are preserved
|
|
239
|
+
*/
|
|
240
|
+
declare function validatePlaceholders$1(original: unknown, translated: unknown, path?: string): ValidationResult;
|
|
241
|
+
/**
|
|
242
|
+
* Full validation of translation
|
|
243
|
+
*/
|
|
244
|
+
declare function validateTranslation(original: unknown, translated: unknown): ValidationResult;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Text utilities for translation
|
|
248
|
+
*/
|
|
249
|
+
/**
|
|
250
|
+
* Check if text is technical content that shouldn't be translated
|
|
251
|
+
*/
|
|
252
|
+
declare function isTechnicalContent(text: string): boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Check if text contains CJK characters (Chinese, Japanese, Korean)
|
|
255
|
+
*/
|
|
256
|
+
declare function containsCJK(text: string): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Check if text contains Cyrillic characters
|
|
259
|
+
*/
|
|
260
|
+
declare function containsCyrillic(text: string): boolean;
|
|
261
|
+
/**
|
|
262
|
+
* Check if text contains Arabic characters
|
|
263
|
+
*/
|
|
264
|
+
declare function containsArabic(text: string): boolean;
|
|
265
|
+
/**
|
|
266
|
+
* Detect script/language from text
|
|
267
|
+
*/
|
|
268
|
+
declare function detectScript(text: string): 'cjk' | 'cyrillic' | 'arabic' | 'latin' | 'unknown';
|
|
269
|
+
/**
|
|
270
|
+
* Extract placeholders from text
|
|
271
|
+
*/
|
|
272
|
+
declare function extractPlaceholders(text: string): string[];
|
|
273
|
+
/**
|
|
274
|
+
* Check if all placeholders are preserved in translation
|
|
275
|
+
*/
|
|
276
|
+
declare function validatePlaceholders(original: string, translated: string): {
|
|
277
|
+
valid: boolean;
|
|
278
|
+
missing: string[];
|
|
279
|
+
extra: string[];
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* Get values that need translation from JSON
|
|
283
|
+
*/
|
|
284
|
+
declare function extractTranslatableValues(obj: unknown, path?: string): Array<{
|
|
285
|
+
path: string;
|
|
286
|
+
value: string;
|
|
287
|
+
}>;
|
|
288
|
+
|
|
289
|
+
export { type BatchTranslateItem as B, type CacheStats as C, JsonTranslator as J, LANGUAGE_NAMES as L, TranslationCache as T, type ValidationResult as V, createCache as a, validateJsonKeys as b, createTranslator as c, detectScript as d, extractPlaceholders as e, type TranslateOptions as f, type TranslateResult as g, type LanguageCode as h, isTechnicalContent as i, validatePlaceholders$1 as j, extractTranslatableValues as k, containsCJK as l, containsCyrillic as m, containsArabic as n, validatePlaceholders as o, validateTranslation as v };
|