@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.
Files changed (45) hide show
  1. package/README.md +181 -0
  2. package/dist/index.cjs +1164 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +164 -0
  5. package/dist/index.d.ts +164 -0
  6. package/dist/index.mjs +1128 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/providers/index.cjs +317 -0
  9. package/dist/providers/index.cjs.map +1 -0
  10. package/dist/providers/index.d.cts +30 -0
  11. package/dist/providers/index.d.ts +30 -0
  12. package/dist/providers/index.mjs +304 -0
  13. package/dist/providers/index.mjs.map +1 -0
  14. package/dist/sdkrouter-D8GMBmTi.d.ts +171 -0
  15. package/dist/sdkrouter-hlQlVd0v.d.cts +171 -0
  16. package/dist/text-utils-DoYqMIr6.d.ts +289 -0
  17. package/dist/text-utils-VXWN-8Oq.d.cts +289 -0
  18. package/dist/translator/index.cjs +794 -0
  19. package/dist/translator/index.cjs.map +1 -0
  20. package/dist/translator/index.d.cts +24 -0
  21. package/dist/translator/index.d.ts +24 -0
  22. package/dist/translator/index.mjs +769 -0
  23. package/dist/translator/index.mjs.map +1 -0
  24. package/dist/types-D6lazgm1.d.cts +59 -0
  25. package/dist/types-D6lazgm1.d.ts +59 -0
  26. package/package.json +82 -0
  27. package/src/client.ts +119 -0
  28. package/src/index.ts +70 -0
  29. package/src/providers/anthropic.ts +98 -0
  30. package/src/providers/base.ts +90 -0
  31. package/src/providers/index.ts +15 -0
  32. package/src/providers/openai.ts +73 -0
  33. package/src/providers/sdkrouter.ts +279 -0
  34. package/src/translator/cache.ts +237 -0
  35. package/src/translator/index.ts +55 -0
  36. package/src/translator/json-translator.ts +408 -0
  37. package/src/translator/prompts.ts +90 -0
  38. package/src/translator/text-utils.ts +148 -0
  39. package/src/translator/types.ts +112 -0
  40. package/src/translator/validator.ts +181 -0
  41. package/src/types.ts +85 -0
  42. package/src/utils/env.ts +67 -0
  43. package/src/utils/index.ts +2 -0
  44. package/src/utils/json.ts +44 -0
  45. package/src/utils/schema.ts +153 -0
@@ -0,0 +1,171 @@
1
+ import { a as LLMClient, b as LLMProvider, L as LLMConfig, f as LLMMessage, c as LLMRequestOptions, d as LLMResponse } from './types-D6lazgm1.cjs';
2
+
3
+ /**
4
+ * Base LLM provider
5
+ */
6
+
7
+ declare abstract class BaseLLMProvider implements LLMClient {
8
+ abstract provider: LLMProvider;
9
+ protected config: Required<Pick<LLMConfig, 'model' | 'temperature' | 'maxTokens'>> & LLMConfig;
10
+ constructor(config: LLMConfig);
11
+ /**
12
+ * Send chat messages (implemented by provider)
13
+ */
14
+ abstract chatMessages(messages: LLMMessage[], options?: LLMRequestOptions): Promise<LLMResponse>;
15
+ /**
16
+ * Send single chat message
17
+ */
18
+ chat(prompt: string, options?: LLMRequestOptions): Promise<LLMResponse>;
19
+ /**
20
+ * Get JSON response
21
+ */
22
+ json<T = unknown>(prompt: string, options?: LLMRequestOptions): Promise<T>;
23
+ /**
24
+ * Get JSON response with schema hint
25
+ */
26
+ jsonSchema<T = unknown>(prompt: string, schema: string, options?: LLMRequestOptions): Promise<T>;
27
+ }
28
+
29
+ /**
30
+ * SDKRouter provider
31
+ *
32
+ * Uses https://llm.sdkrouter.com - OpenAI-compatible LLM router
33
+ * with smart model aliases like @smart, @cheap, @balanced
34
+ */
35
+
36
+ /** SDKRouter base URL */
37
+ declare const SDKROUTER_BASE_URL = "https://llm.sdkrouter.com/v1";
38
+ /**
39
+ * Model tier presets
40
+ *
41
+ * @example '@cheap', '@smart', '@balanced'
42
+ */
43
+ type ModelTier = 'cheap' | 'budget' | 'standard' | 'balanced' | 'smart' | 'fast' | 'premium';
44
+ /**
45
+ * Model capabilities (features)
46
+ *
47
+ * @example '@cheap+vision', '@smart+tools+json'
48
+ */
49
+ type ModelCapability = 'vision' | 'tools' | 'agents' | 'json' | 'streaming' | 'long' | 'image';
50
+ /**
51
+ * Model categories (use cases)
52
+ *
53
+ * @example '@balanced+code', '@smart+reasoning'
54
+ */
55
+ type ModelCategory = 'code' | 'vision' | 'reasoning' | 'agents' | 'creative' | 'chat' | 'analysis';
56
+ /**
57
+ * Model alias builder options
58
+ */
59
+ interface ModelOptions {
60
+ vision?: boolean;
61
+ tools?: boolean;
62
+ agents?: boolean;
63
+ json?: boolean;
64
+ streaming?: boolean;
65
+ long?: boolean;
66
+ image?: boolean;
67
+ code?: boolean;
68
+ reasoning?: boolean;
69
+ creative?: boolean;
70
+ chat?: boolean;
71
+ analysis?: boolean;
72
+ }
73
+ /**
74
+ * Build model alias string
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * buildModelAlias('smart') // '@smart'
79
+ * buildModelAlias('cheap', { vision: true }) // '@cheap+vision'
80
+ * buildModelAlias('balanced', { code: true, tools: true }) // '@balanced+code+tools'
81
+ * ```
82
+ */
83
+ declare function buildModelAlias(tier: ModelTier, options?: ModelOptions): string;
84
+ /**
85
+ * Model alias builder with IDE autocomplete
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * import { Model } from '@djangocfg/llm'
90
+ *
91
+ * Model.smart() // '@smart'
92
+ * Model.cheap({ vision: true }) // '@cheap+vision'
93
+ * Model.balanced({ code: true }) // '@balanced+code'
94
+ * Model.fast({ tools: true, json: true }) // '@fast+tools+json'
95
+ * ```
96
+ */
97
+ declare const Model: {
98
+ /** Cheapest available model */
99
+ readonly cheap: (options?: ModelOptions) => string;
100
+ /** Budget-friendly with decent quality */
101
+ readonly budget: (options?: ModelOptions) => string;
102
+ /** Standard tier */
103
+ readonly standard: (options?: ModelOptions) => string;
104
+ /** Best quality/price ratio (recommended) */
105
+ readonly balanced: (options?: ModelOptions) => string;
106
+ /** Highest quality model */
107
+ readonly smart: (options?: ModelOptions) => string;
108
+ /** Lowest latency model */
109
+ readonly fast: (options?: ModelOptions) => string;
110
+ /** Top-tier premium model */
111
+ readonly premium: (options?: ModelOptions) => string;
112
+ /**
113
+ * Build alias from raw strings (escape hatch)
114
+ *
115
+ * @example Model.alias('cheap', 'vision', 'code') // '@cheap+vision+code'
116
+ */
117
+ readonly alias: (tier: string, ...modifiers: string[]) => string;
118
+ };
119
+ /**
120
+ * Pre-built model aliases for common use cases
121
+ */
122
+ declare const ModelPresets: {
123
+ /** Translation: cheap + json mode */
124
+ readonly translation: string;
125
+ /** Code generation: balanced + code */
126
+ readonly code: string;
127
+ /** Code with tools: balanced + code + tools */
128
+ readonly codeWithTools: string;
129
+ /** Vision: balanced + vision */
130
+ readonly vision: string;
131
+ /** Reasoning: smart + reasoning */
132
+ readonly reasoning: string;
133
+ /** Creative writing: balanced + creative */
134
+ readonly creative: string;
135
+ /** Fast chat: fast + chat */
136
+ readonly fastChat: string;
137
+ /** Analysis: balanced + analysis */
138
+ readonly analysis: string;
139
+ /** Agents: smart + agents + tools */
140
+ readonly agents: string;
141
+ };
142
+ /**
143
+ * SDKRouter provider config
144
+ */
145
+ interface SDKRouterConfig extends LLMConfig {
146
+ /** Model tier (shortcut for building alias) */
147
+ tier?: ModelTier;
148
+ /** Model options for alias building */
149
+ modelOptions?: ModelOptions;
150
+ }
151
+ /**
152
+ * SDKRouter LLM provider
153
+ *
154
+ * Uses OpenAI-compatible API at https://llm.sdkrouter.com
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const llm = new SDKRouterProvider({
159
+ * apiKey: process.env.SDKROUTER_API_KEY,
160
+ * model: Model.balanced({ code: true })
161
+ * })
162
+ * ```
163
+ */
164
+ declare class SDKRouterProvider extends BaseLLMProvider {
165
+ provider: LLMProvider;
166
+ private client;
167
+ constructor(config: SDKRouterConfig);
168
+ chatMessages(messages: LLMMessage[], options?: LLMRequestOptions): Promise<LLMResponse>;
169
+ }
170
+
171
+ export { BaseLLMProvider as B, Model as M, SDKROUTER_BASE_URL as S, ModelPresets as a, buildModelAlias as b, type ModelTier as c, type ModelCapability as d, type ModelCategory as e, type ModelOptions as f, SDKRouterProvider as g, type SDKRouterConfig as h };
@@ -0,0 +1,289 @@
1
+ import { c as LLMRequestOptions, a as LLMClient } from './types-D6lazgm1.js';
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 };