@alaikis/translation-sdk 1.0.0 → 1.2.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 +1 -1
- package/translation-client.d.ts +352 -497
- package/translation-client.js +1153 -1157
package/translation-client.d.ts
CHANGED
|
@@ -1,502 +1,357 @@
|
|
|
1
|
-
|
|
2
|
-
* Translation Service gRPC-Web TypeScript/JavaScript Client
|
|
3
|
-
*
|
|
4
|
-
* Auto-generated for api.laker.dev Translation Service
|
|
5
|
-
* Service: TranslationService
|
|
6
|
-
* Source: proto/translation.proto
|
|
7
|
-
*/
|
|
8
|
-
export interface TranslateRecord {
|
|
9
|
-
id: string;
|
|
10
|
-
text: string;
|
|
11
|
-
translate: string;
|
|
12
|
-
isCustom: boolean;
|
|
13
|
-
}
|
|
14
|
-
export interface GetSenseTranslateRequest {
|
|
15
|
-
senseId: string;
|
|
16
|
-
fingerprint?: string;
|
|
17
|
-
page?: number;
|
|
18
|
-
pageSize?: number;
|
|
19
|
-
}
|
|
20
|
-
export interface GetSenseTranslateResponse {
|
|
21
|
-
senseId: string;
|
|
22
|
-
total: number;
|
|
23
|
-
page: number;
|
|
24
|
-
pageSize: number;
|
|
25
|
-
translations: TranslateRecord[];
|
|
26
|
-
}
|
|
27
|
-
export interface TranslateStreamRequest {
|
|
28
|
-
senseId: string;
|
|
29
|
-
fingerprint?: string;
|
|
30
|
-
batchSize?: number;
|
|
31
|
-
}
|
|
32
|
-
export interface TranslateStreamResponse {
|
|
33
|
-
batch: number;
|
|
34
|
-
hasMore: boolean;
|
|
35
|
-
total: number;
|
|
36
|
-
translations: TranslateRecord[];
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Result of template extraction
|
|
40
|
-
*/
|
|
41
|
-
export interface TemplateExtractResult {
|
|
42
|
-
isTemplated: boolean;
|
|
43
|
-
srcTemplate: string;
|
|
44
|
-
dstTemplate: string;
|
|
45
|
-
variables: string[];
|
|
46
|
-
}
|
|
47
|
-
export interface LLMTranslateRequest {
|
|
48
|
-
text: string;
|
|
49
|
-
fromLang?: string;
|
|
50
|
-
toLang: string;
|
|
51
|
-
provider?: string;
|
|
52
|
-
senseId?: string;
|
|
53
|
-
}
|
|
54
|
-
export interface LLMTranslateResponse {
|
|
55
|
-
originalText: string;
|
|
56
|
-
translatedText: string;
|
|
57
|
-
provider: string;
|
|
58
|
-
timestamp: number;
|
|
59
|
-
finished: boolean;
|
|
60
|
-
cached: boolean;
|
|
61
|
-
fromLang?: string;
|
|
62
|
-
toLang?: string;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Result of translation lookup in TranslationPool
|
|
66
|
-
*/
|
|
67
|
-
export interface TranslationLookupResult {
|
|
68
|
-
found: boolean;
|
|
69
|
-
translation: string;
|
|
70
|
-
source: 'special' | 'common' | null;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Cross-tab cache synchronization options
|
|
74
|
-
*/
|
|
75
|
-
export interface CrossTabOptions {
|
|
76
|
-
enabled: boolean;
|
|
77
|
-
channelName?: string;
|
|
78
|
-
storageKeyPrefix?: string;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Automatic template extraction from text containing numeric variables
|
|
82
|
-
* @param text Original text that may contain numeric variables
|
|
83
|
-
* @returns Template extraction result
|
|
84
|
-
*/
|
|
85
|
-
export declare function extractTemplate(text: string): TemplateExtractResult;
|
|
86
|
-
/**
|
|
87
|
-
* Translation options
|
|
88
|
-
*/
|
|
89
|
-
export interface TranslationServiceOptions {
|
|
90
|
-
senseId: string;
|
|
91
|
-
fingerprint?: string;
|
|
92
|
-
crossTab?: boolean;
|
|
93
|
-
crossTabChannelName?: string;
|
|
94
|
-
crossTabStorageKeyPrefix?: string;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* TranslationService - Main entry point for translation operations
|
|
98
|
-
* Provides a clean API with automatic caching and cross-tab synchronization
|
|
99
|
-
*
|
|
100
|
-
* Features:
|
|
101
|
-
* - Lazy initialization: automatically initializes on first translate() call
|
|
102
|
-
* - Streaming batch load: uses streaming API for efficient initialization
|
|
103
|
-
* - On-demand loading: automatically loads when fingerprint changes
|
|
104
|
-
* - Cross-tab sync: optional Broadcast Channel + localStorage synchronization
|
|
105
|
-
*
|
|
106
|
-
* Usage:
|
|
107
|
-
* - Simple: Just create and call translate() - initialization is automatic
|
|
108
|
-
* - Advanced: Call initialize() explicitly to preload all translations upfront
|
|
109
|
-
*/
|
|
110
|
-
export declare class TranslationService {
|
|
111
|
-
private client;
|
|
112
|
-
private pool;
|
|
113
|
-
private initialized;
|
|
114
|
-
private initPromise;
|
|
115
|
-
private options;
|
|
116
|
-
/**
|
|
117
|
-
* Create a new TranslationService instance
|
|
118
|
-
* @param client TranslationClient instance (or base URL string to create one automatically)
|
|
119
|
-
* @param options Translation service options including senseId and optional fingerprint/cross-tab settings
|
|
120
|
-
*/
|
|
121
|
-
constructor(client: TranslationClient | string, options: TranslationServiceOptions);
|
|
122
|
-
/**
|
|
123
|
-
* Ensure the service is initialized (lazy initialization)
|
|
124
|
-
* Called automatically by translate() if needed
|
|
125
|
-
*/
|
|
126
|
-
private ensureInitialized;
|
|
1
|
+
declare module "translation-client" {
|
|
127
2
|
/**
|
|
128
|
-
*
|
|
129
|
-
*/
|
|
130
|
-
private doInitialize;
|
|
131
|
-
/**
|
|
132
|
-
* Initialize the translation service - loads all translations from backend
|
|
3
|
+
* Translation Service gRPC-Web TypeScript/JavaScript Client
|
|
133
4
|
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
5
|
+
* Auto-generated for api.laker.dev Translation Service
|
|
6
|
+
* Service: TranslationService
|
|
7
|
+
* Source: proto/translation.proto
|
|
8
|
+
*/
|
|
9
|
+
export interface TranslateRecord {
|
|
10
|
+
id: string;
|
|
11
|
+
text: string;
|
|
12
|
+
translate: string;
|
|
13
|
+
isCustom: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface GetSenseTranslateRequest {
|
|
16
|
+
senseId: string;
|
|
17
|
+
fingerprint?: string;
|
|
18
|
+
page?: number;
|
|
19
|
+
pageSize?: number;
|
|
20
|
+
src_lang?: string;
|
|
21
|
+
dst_lang?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface GetSenseTranslateResponse {
|
|
24
|
+
senseId: string;
|
|
25
|
+
total: number;
|
|
26
|
+
page: number;
|
|
27
|
+
pageSize: number;
|
|
28
|
+
result: TranslateRecord[];
|
|
29
|
+
}
|
|
30
|
+
export interface TranslateStreamRequest {
|
|
31
|
+
senseId: string;
|
|
32
|
+
fingerprint?: string;
|
|
33
|
+
batchSize?: number;
|
|
34
|
+
}
|
|
35
|
+
export interface TranslateStreamResponse {
|
|
36
|
+
batch: number;
|
|
37
|
+
hasMore: boolean;
|
|
38
|
+
total: number;
|
|
39
|
+
translations: TranslateRecord[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of template extraction
|
|
43
|
+
*/
|
|
44
|
+
export interface TemplateExtractResult {
|
|
45
|
+
isTemplated: boolean;
|
|
46
|
+
srcTemplate: string;
|
|
47
|
+
dstTemplate: string;
|
|
48
|
+
variables: string[];
|
|
49
|
+
}
|
|
50
|
+
export interface LLMTranslateRequest {
|
|
51
|
+
text: string;
|
|
52
|
+
fromLang?: string;
|
|
53
|
+
toLang: string;
|
|
54
|
+
provider?: string;
|
|
55
|
+
senseId?: string;
|
|
56
|
+
fingerprint?: string;
|
|
57
|
+
}
|
|
58
|
+
export interface LLMTranslateResponse {
|
|
59
|
+
originalText: string;
|
|
60
|
+
translatedText: string;
|
|
61
|
+
provider: string;
|
|
62
|
+
timestamp: number;
|
|
63
|
+
finished: boolean;
|
|
64
|
+
cached: boolean;
|
|
65
|
+
fromLang?: string;
|
|
66
|
+
toLang?: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Result of translation lookup in TranslationPool
|
|
70
|
+
*/
|
|
71
|
+
export interface TranslationLookupResult {
|
|
72
|
+
found: boolean;
|
|
73
|
+
translation: string;
|
|
74
|
+
source: 'special' | 'common' | null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Cross-tab cache synchronization options
|
|
78
|
+
*/
|
|
79
|
+
export interface CrossTabOptions {
|
|
80
|
+
enabled: boolean;
|
|
81
|
+
channelName?: string;
|
|
82
|
+
storageKeyPrefix?: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Automatic template extraction from text containing numeric variables
|
|
86
|
+
* @param text Original text that may contain numeric variables
|
|
87
|
+
* @returns Template extraction result
|
|
88
|
+
*/
|
|
89
|
+
export function extractTemplate(text: string): TemplateExtractResult;
|
|
90
|
+
/**
|
|
91
|
+
* Translation options
|
|
92
|
+
*/
|
|
93
|
+
export interface TranslationServiceOptions {
|
|
94
|
+
senseId: string;
|
|
95
|
+
fingerprint?: string;
|
|
96
|
+
fromLang?: string;
|
|
97
|
+
toLang?: string;
|
|
98
|
+
crossTab?: boolean;
|
|
99
|
+
crossTabChannelName?: string;
|
|
100
|
+
crossTabStorageKeyPrefix?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Cross-tab cache synchronization options for TranslationClient
|
|
104
|
+
*/
|
|
105
|
+
export interface ClientCrossTabOptions {
|
|
106
|
+
enabled: boolean;
|
|
107
|
+
channelName?: string;
|
|
108
|
+
storageKey?: string;
|
|
109
|
+
}
|
|
110
|
+
export interface TranslationClientConfig {
|
|
111
|
+
/** JWT authentication token */
|
|
112
|
+
token: string;
|
|
113
|
+
/** Translation sense ID */
|
|
114
|
+
senseId: string;
|
|
115
|
+
/** API base URL (optional, defaults to https://api.hottol.com/laker/) */
|
|
116
|
+
baseUrl?: string;
|
|
117
|
+
/** Request timeout in milliseconds (optional, defaults to 30000) */
|
|
118
|
+
timeout?: number;
|
|
119
|
+
/** LRU cache size (optional, defaults to 1000, set to 0 to disable cache) */
|
|
120
|
+
cacheSize?: number;
|
|
121
|
+
/** Initial fingerprint for personalized translations (optional) */
|
|
122
|
+
fingerprint?: string;
|
|
123
|
+
/** Enable cross-tab cache synchronization (optional, default: false) */
|
|
124
|
+
crossTab?: boolean;
|
|
125
|
+
/** Custom cross-tab channel name (optional) */
|
|
126
|
+
crossTabChannelName?: string;
|
|
127
|
+
/** Custom cross-tab storage key prefix (optional) */
|
|
128
|
+
crossTabStorageKeyPrefix?: string;
|
|
129
|
+
/** Enable cache (optional, default: true, set false to disable all caching) */
|
|
130
|
+
useCache?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* TranslationClient - Main entry point for Laker Translation SDK
|
|
169
134
|
*
|
|
170
135
|
* Features:
|
|
171
|
-
* -
|
|
172
|
-
* -
|
|
173
|
-
* -
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
getFingerprint(): string | null;
|
|
393
|
-
/**
|
|
394
|
-
* Translate text with automatic caching and initialization
|
|
395
|
-
*
|
|
396
|
-
* - First call initializes the service automatically
|
|
397
|
-
* - Checks cache first (fingerprint-specific → common)
|
|
398
|
-
* - Falls back to LLM translation if not found
|
|
399
|
-
* - Caches result automatically (unless useCache: false)
|
|
400
|
-
*
|
|
401
|
-
* @param text Text to translate
|
|
402
|
-
* @param toLang Target language code (e.g., 'zh', 'en')
|
|
403
|
-
* @param fromLang Optional source language code (auto-detected if not provided)
|
|
404
|
-
* @returns Translation result
|
|
405
|
-
*/
|
|
406
|
-
translate(text: string, toLang: string, fromLang?: string): Promise<string>;
|
|
407
|
-
/**
|
|
408
|
-
* Translate text with full response details
|
|
409
|
-
* @param text Text to translate
|
|
410
|
-
* @param toLang Target language code
|
|
411
|
-
* @param fromLang Optional source language code
|
|
412
|
-
* @returns Full translation response
|
|
413
|
-
*/
|
|
414
|
-
translateWithDetails(text: string, toLang: string, fromLang?: string): Promise<LLMTranslateResponse>;
|
|
415
|
-
/**
|
|
416
|
-
* Translate text without using cache (always request from backend)
|
|
417
|
-
* @param text Text to translate
|
|
418
|
-
* @param toLang Target language code
|
|
419
|
-
* @param fromLang Optional source language code
|
|
420
|
-
* @returns Translation result
|
|
421
|
-
*/
|
|
422
|
-
translateNoCache(text: string, toLang: string, fromLang?: string): Promise<string>;
|
|
423
|
-
/**
|
|
424
|
-
* Batch translate multiple texts
|
|
425
|
-
* @param texts Array of texts to translate
|
|
426
|
-
* @param toLang Target language code
|
|
427
|
-
* @param fromLang Optional source language code
|
|
428
|
-
* @returns Array of translated texts in same order
|
|
429
|
-
*/
|
|
430
|
-
translateBatch(texts: string[], toLang: string, fromLang?: string): Promise<string[]>;
|
|
431
|
-
/**
|
|
432
|
-
* Check if cache is enabled
|
|
433
|
-
* @returns true if cache is enabled
|
|
434
|
-
*/
|
|
435
|
-
isCacheEnabled(): boolean;
|
|
436
|
-
/**
|
|
437
|
-
* Check if a translation exists in cache
|
|
438
|
-
* @param text Text to check
|
|
439
|
-
* @returns true if translation exists in cache
|
|
440
|
-
*/
|
|
441
|
-
hasTranslation(text: string): boolean;
|
|
442
|
-
/**
|
|
443
|
-
* Get translation from cache without requesting from backend
|
|
444
|
-
* @param text Text to look up
|
|
445
|
-
* @returns Translation if found, null otherwise
|
|
446
|
-
*/
|
|
447
|
-
getCached(text: string): string | null;
|
|
448
|
-
/**
|
|
449
|
-
* Add a custom translation to the cache
|
|
450
|
-
* @param text Original text
|
|
451
|
-
* @param translation Translated text
|
|
452
|
-
*/
|
|
453
|
-
addTranslation(text: string, translation: string): void;
|
|
454
|
-
/**
|
|
455
|
-
* Preload all translations for current context
|
|
456
|
-
* Call this to warm up the cache before translating
|
|
457
|
-
*/
|
|
458
|
-
preload(): Promise<void>;
|
|
459
|
-
/**
|
|
460
|
-
* Check if the service is initialized
|
|
461
|
-
*/
|
|
462
|
-
isInitialized(): boolean;
|
|
463
|
-
/**
|
|
464
|
-
* Clear all cached translations
|
|
465
|
-
*/
|
|
466
|
-
clearCache(): void;
|
|
467
|
-
/**
|
|
468
|
-
* Destroy the instance and free resources
|
|
469
|
-
* Call this when the instance is no longer needed
|
|
470
|
-
*/
|
|
471
|
-
destroy(): void;
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Configuration for AppTranslation
|
|
475
|
-
*/
|
|
476
|
-
export interface AppTranslationConfig {
|
|
477
|
-
/** JWT authentication token */
|
|
478
|
-
token: string;
|
|
479
|
-
/** Translation sense ID */
|
|
480
|
-
senseId: string;
|
|
481
|
-
/** API base URL (optional, defaults to https://api.hottol.com/laker/) */
|
|
482
|
-
baseUrl?: string;
|
|
483
|
-
/** Request timeout in milliseconds (optional, defaults to 30000) */
|
|
484
|
-
timeout?: number;
|
|
485
|
-
/** LRU cache size (optional, defaults to 1000, set to 0 to disable cache) */
|
|
486
|
-
cacheSize?: number;
|
|
487
|
-
/** Initial fingerprint (optional) */
|
|
488
|
-
fingerprint?: string;
|
|
489
|
-
/** Enable cross-tab synchronization (optional, defaults to false) */
|
|
490
|
-
crossTab?: boolean;
|
|
491
|
-
/** Enable cache (optional, defaults to true. Set to false to disable all caching) */
|
|
492
|
-
useCache?: boolean;
|
|
136
|
+
* - Automatic cache lookup: preloaded translations → LRU cache → backend request
|
|
137
|
+
* - Fingerprint-based personalized translations support
|
|
138
|
+
* - Optional cross-browser-tab cache synchronization
|
|
139
|
+
* - Lazy initialization: automatically preloads on first use
|
|
140
|
+
* - Simple single-level API, no complex layering
|
|
141
|
+
*/
|
|
142
|
+
export class TranslationClient {
|
|
143
|
+
private baseUrl;
|
|
144
|
+
private token;
|
|
145
|
+
private timeout;
|
|
146
|
+
private config;
|
|
147
|
+
private llmCacheEnabled;
|
|
148
|
+
private llmCache;
|
|
149
|
+
private pool;
|
|
150
|
+
private currentFingerprint;
|
|
151
|
+
private initialized;
|
|
152
|
+
private initPromise;
|
|
153
|
+
private broadcastChannel;
|
|
154
|
+
private crossTabOptions;
|
|
155
|
+
private storageKey;
|
|
156
|
+
/**
|
|
157
|
+
* Create a new TranslationClient - the only entry point you need
|
|
158
|
+
* @param config Client configuration
|
|
159
|
+
*/
|
|
160
|
+
constructor(config: TranslationClientConfig);
|
|
161
|
+
/**
|
|
162
|
+
* Initialize cross-tab synchronization via Broadcast Channel
|
|
163
|
+
*/
|
|
164
|
+
private initCrossTabSync;
|
|
165
|
+
/**
|
|
166
|
+
* Load cache from localStorage
|
|
167
|
+
*/
|
|
168
|
+
private loadFromStorage;
|
|
169
|
+
/**
|
|
170
|
+
* Save cache to localStorage
|
|
171
|
+
*/
|
|
172
|
+
private saveToStorage;
|
|
173
|
+
/**
|
|
174
|
+
* Get all cache entries for storage/broadcast
|
|
175
|
+
*/
|
|
176
|
+
private getAllCacheEntries;
|
|
177
|
+
/**
|
|
178
|
+
* Broadcast full cache to other tabs
|
|
179
|
+
*/
|
|
180
|
+
private broadcastFullCache;
|
|
181
|
+
/**
|
|
182
|
+
* Broadcast a single cache update to other tabs
|
|
183
|
+
*/
|
|
184
|
+
private broadcastCacheUpdate;
|
|
185
|
+
/**
|
|
186
|
+
* Set or update the JWT authentication token
|
|
187
|
+
* @param token JWT token
|
|
188
|
+
*/
|
|
189
|
+
setToken(token: string): void;
|
|
190
|
+
/**
|
|
191
|
+
* Enable or disable LLM cache
|
|
192
|
+
* @param enabled Whether to enable cache
|
|
193
|
+
*/
|
|
194
|
+
setCacheEnabled(enabled: boolean): void;
|
|
195
|
+
/**
|
|
196
|
+
* Clear the LLM translation cache (also clears localStorage and broadcasts to other tabs)
|
|
197
|
+
*/
|
|
198
|
+
clearCache(): void;
|
|
199
|
+
/**
|
|
200
|
+
* Get current LLM cache size
|
|
201
|
+
*/
|
|
202
|
+
getCacheSize(): number;
|
|
203
|
+
/**
|
|
204
|
+
* Check if cross-tab synchronization is enabled
|
|
205
|
+
*/
|
|
206
|
+
isCrossTabEnabled(): boolean;
|
|
207
|
+
/**
|
|
208
|
+
凤 * GetSenseTranslate - One-shot unary request with pagination
|
|
209
|
+
* @param request Request parameters
|
|
210
|
+
*/
|
|
211
|
+
getSenseTranslate(request: GetSenseTranslateRequest): Promise<GetSenseTranslateResponse>;
|
|
212
|
+
/**
|
|
213
|
+
* TranslateStream - Server streaming, receives multiple batches progressively
|
|
214
|
+
* @param request Request parameters
|
|
215
|
+
* @param onBatch Callback for each batch received. Return false to stop streaming early.
|
|
216
|
+
*/
|
|
217
|
+
translateStream(request: TranslateStreamRequest, onBatch: (response: TranslateStreamResponse) => boolean | void): Promise<void>;
|
|
218
|
+
/**
|
|
219
|
+
* Collect all streaming responses into an array
|
|
220
|
+
* @param request Request parameters
|
|
221
|
+
*/
|
|
222
|
+
translateStreamCollect(request: TranslateStreamRequest): Promise<TranslateStreamResponse[]>;
|
|
223
|
+
/**
|
|
224
|
+
* LLMTranslate - One-shot large language model translation
|
|
225
|
+
* Uses LRU cache to avoid repeated requests for the same text
|
|
226
|
+
* With cross-tab enabled, automatically syncs cache across browser tabs
|
|
227
|
+
* @param request Translation request
|
|
228
|
+
* @param skipCache If true, bypass cache and always request from backend
|
|
229
|
+
*/
|
|
230
|
+
llmTranslate(request: LLMTranslateRequest, skipCache?: boolean): Promise<LLMTranslateResponse>;
|
|
231
|
+
/**
|
|
232
|
+
* Translate text - this is the main method you need
|
|
233
|
+
*
|
|
234
|
+
* Workflow:
|
|
235
|
+
* 1. Check pre-loaded translation pool (provided fingerprint → current fingerprint → common)
|
|
236
|
+
* 2. If found, return immediately from cache
|
|
237
|
+
* 3. If not found, request LLM translation from backend
|
|
238
|
+
* 4. Auto-initialize on first call
|
|
239
|
+
*
|
|
240
|
+
* @param text Text to translate
|
|
241
|
+
* @param toLang Target language
|
|
242
|
+
* @param fromLang Source language (optional, auto-detected if not provided)
|
|
243
|
+
* @param fingerprint Optional specific fingerprint for this translation (overrides client-level fingerprint)
|
|
244
|
+
* @returns Translated text
|
|
245
|
+
*/
|
|
246
|
+
translate(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<string>;
|
|
247
|
+
/**
|
|
248
|
+
* Translate text with full response details
|
|
249
|
+
* @param text Text to translate
|
|
250
|
+
* @param toLang Target language
|
|
251
|
+
* @param fromLang Source language (optional)
|
|
252
|
+
* @param fingerprint Optional specific fingerprint for this translation (overrides client-level fingerprint)
|
|
253
|
+
* @returns Full translation response
|
|
254
|
+
*/
|
|
255
|
+
translateWithDetails(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<LLMTranslateResponse>;
|
|
256
|
+
/**
|
|
257
|
+
* Translate without using cache (always request from backend)
|
|
258
|
+
* @param text Text to translate
|
|
259
|
+
* @param toLang Target language
|
|
260
|
+
* @param fromLang Source language (optional)
|
|
261
|
+
* @param fingerprint Optional specific fingerprint for this translation
|
|
262
|
+
* @returns Translated text
|
|
263
|
+
*/
|
|
264
|
+
translateNoCache(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<string>;
|
|
265
|
+
/**
|
|
266
|
+
* Batch translate multiple texts
|
|
267
|
+
* @param texts Array of texts to translate
|
|
268
|
+
* @param toLang Target language
|
|
269
|
+
* @param fromLang Source language (optional)
|
|
270
|
+
* @param fingerprint Optional specific fingerprint for all translations in this batch
|
|
271
|
+
* @returns Array of translated texts in same order
|
|
272
|
+
*/
|
|
273
|
+
translateBatch(texts: string[], toLang: string, fromLang?: string, fingerprint?: string): Promise<string[]>;
|
|
274
|
+
/**
|
|
275
|
+
* Initialize and preload all translations
|
|
276
|
+
* Call this to warm up cache before translating
|
|
277
|
+
*/
|
|
278
|
+
preload(): Promise<void>;
|
|
279
|
+
/**
|
|
280
|
+
* Internal initialization - preloads translations via streaming
|
|
281
|
+
*/
|
|
282
|
+
private initialize;
|
|
283
|
+
/**
|
|
284
|
+
* Check if service is initialized
|
|
285
|
+
*/
|
|
286
|
+
isInitialized(): boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Set or change the current fingerprint
|
|
289
|
+
* Automatically loads special translations for this fingerprint
|
|
290
|
+
* @param fingerprint The fingerprint to use
|
|
291
|
+
*/
|
|
292
|
+
setFingerprint(fingerprint: string): Promise<void>;
|
|
293
|
+
/**
|
|
294
|
+
* Clear the current fingerprint
|
|
295
|
+
* Falls back to common translations
|
|
296
|
+
*/
|
|
297
|
+
clearFingerprint(): void;
|
|
298
|
+
/**
|
|
299
|
+
* Get the current fingerprint
|
|
300
|
+
*/
|
|
301
|
+
getFingerprint(): string | null;
|
|
302
|
+
/**
|
|
303
|
+
* Check if cache is enabled
|
|
304
|
+
* @returns true if cache is enabled
|
|
305
|
+
*/
|
|
306
|
+
isCacheEnabled(): boolean;
|
|
307
|
+
/**
|
|
308
|
+
* Check if a translation exists in pre-loaded pool
|
|
309
|
+
* @param text Text to check
|
|
310
|
+
* @param fingerprint Optional specific fingerprint to check
|
|
311
|
+
* @returns true if translation exists in cache
|
|
312
|
+
*/
|
|
313
|
+
hasTranslation(text: string, fingerprint?: string): boolean;
|
|
314
|
+
/**
|
|
315
|
+
* Get translation from pre-loaded cache without requesting from backend
|
|
316
|
+
* @param text Text to look up
|
|
317
|
+
* @param fingerprint Optional specific fingerprint to look up
|
|
318
|
+
* @returns Translation if found, null otherwise
|
|
319
|
+
*/
|
|
320
|
+
getCached(text: string, fingerprint?: string): string | null;
|
|
321
|
+
/**
|
|
322
|
+
* Add a custom translation to the pre-loaded pool
|
|
323
|
+
* @param text Original text
|
|
324
|
+
* @param translation Translated text
|
|
325
|
+
* @param fingerprint Optional specific fingerprint to add to
|
|
326
|
+
*/
|
|
327
|
+
addTranslation(text: string, translation: string, fingerprint?: string): void;
|
|
328
|
+
/**
|
|
329
|
+
* Clear all cached translations
|
|
330
|
+
*/
|
|
331
|
+
clearAllCache(): void;
|
|
332
|
+
/**
|
|
333
|
+
* Destroy the instance and free resources
|
|
334
|
+
* Call this when the instance is no longer needed
|
|
335
|
+
*/
|
|
336
|
+
destroy(): void;
|
|
337
|
+
/**
|
|
338
|
+
* LLMTranslateStream - Streaming large language model translation
|
|
339
|
+
* Note: Streaming requests are not cached
|
|
340
|
+
* @param request Translation request
|
|
341
|
+
* @param onResponse Callback for each response chunk
|
|
342
|
+
*/
|
|
343
|
+
llmTranslateStream(request: LLMTranslateRequest, onResponse: (response: LLMTranslateResponse) => boolean | void): Promise<void>;
|
|
344
|
+
private getHeaders;
|
|
345
|
+
private fetchJson;
|
|
346
|
+
private fetchWithTimeout;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Create a TranslationClient instance with simplified configuration
|
|
350
|
+
* @param token JWT authentication token
|
|
351
|
+
* @param senseId Translation sense ID
|
|
352
|
+
* @param options Additional options
|
|
353
|
+
* @returns TranslationClient instance
|
|
354
|
+
*/
|
|
355
|
+
export function createTranslation(token: string, senseId: string, options?: Partial<TranslationClientConfig>): TranslationClient;
|
|
356
|
+
export default createTranslation;
|
|
493
357
|
}
|
|
494
|
-
/**
|
|
495
|
-
* Create an AppTranslation instance with simplified configuration
|
|
496
|
-
* @param token JWT authentication token
|
|
497
|
-
* @param senseId Translation sense ID
|
|
498
|
-
* @param options Additional options
|
|
499
|
-
* @returns AppTranslation instance
|
|
500
|
-
*/
|
|
501
|
-
export declare function createTranslation(token: string, senseId: string, options?: Partial<AppTranslationConfig>): AppTranslation;
|
|
502
|
-
export default TranslationClient;
|