@alaikis/translation-sdk 1.2.1 → 1.2.2
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 +9 -3
- package/rollup.config.js +19 -0
- package/translation-client.d.ts +351 -353
- package/translation-client.js +56 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alaikis/translation-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Laker Translation Service SDK for TypeScript/JavaScript",
|
|
5
5
|
"main": "translation-client.js",
|
|
6
6
|
"types": "translation-client.d.ts",
|
|
@@ -24,5 +24,11 @@
|
|
|
24
24
|
"bugs": {
|
|
25
25
|
"url": "https://github.com/alaikis/laker-translate-sdk/issues"
|
|
26
26
|
},
|
|
27
|
-
"homepage": "https://github.com/alaikis/laker-translate-sdk#readme"
|
|
28
|
-
|
|
27
|
+
"homepage": "https://github.com/alaikis/laker-translate-sdk#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
30
|
+
"rollup": "^4.60.0",
|
|
31
|
+
"tslib": "^2.8.1",
|
|
32
|
+
"typescript": "^6.0.2"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import typescript from '@rollup/plugin-typescript';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
input: 'translation-client.ts',
|
|
5
|
+
output: [
|
|
6
|
+
{
|
|
7
|
+
file: 'translation-client.js',
|
|
8
|
+
format: 'umd',
|
|
9
|
+
name: 'LakerTranslation',
|
|
10
|
+
sourcemap: false
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
plugins: [typescript({
|
|
14
|
+
target: 'es2015',
|
|
15
|
+
declaration: true,
|
|
16
|
+
declarationDir: './',
|
|
17
|
+
strict: false
|
|
18
|
+
})]
|
|
19
|
+
};
|
package/translation-client.d.ts
CHANGED
|
@@ -1,357 +1,355 @@
|
|
|
1
|
-
|
|
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
|
+
src_lang?: string;
|
|
20
|
+
dst_lang?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface GetSenseTranslateResponse {
|
|
23
|
+
senseId: string;
|
|
24
|
+
total: number;
|
|
25
|
+
page: number;
|
|
26
|
+
pageSize: number;
|
|
27
|
+
result: TranslateRecord[];
|
|
28
|
+
}
|
|
29
|
+
export interface TranslateStreamRequest {
|
|
30
|
+
senseId: string;
|
|
31
|
+
fingerprint?: string;
|
|
32
|
+
batchSize?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface TranslateStreamResponse {
|
|
35
|
+
batch: number;
|
|
36
|
+
hasMore: boolean;
|
|
37
|
+
total: number;
|
|
38
|
+
translations: TranslateRecord[];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Result of template extraction
|
|
42
|
+
*/
|
|
43
|
+
export interface TemplateExtractResult {
|
|
44
|
+
isTemplated: boolean;
|
|
45
|
+
srcTemplate: string;
|
|
46
|
+
dstTemplate: string;
|
|
47
|
+
variables: string[];
|
|
48
|
+
}
|
|
49
|
+
export interface LLMTranslateRequest {
|
|
50
|
+
text: string;
|
|
51
|
+
fromLang?: string;
|
|
52
|
+
toLang: string;
|
|
53
|
+
provider?: string;
|
|
54
|
+
senseId?: string;
|
|
55
|
+
fingerprint?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface LLMTranslateResponse {
|
|
58
|
+
originalText: string;
|
|
59
|
+
translatedText: string;
|
|
60
|
+
provider: string;
|
|
61
|
+
timestamp: number;
|
|
62
|
+
finished: boolean;
|
|
63
|
+
cached: boolean;
|
|
64
|
+
fromLang?: string;
|
|
65
|
+
toLang?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Result of translation lookup in TranslationPool
|
|
69
|
+
*/
|
|
70
|
+
export interface TranslationLookupResult {
|
|
71
|
+
found: boolean;
|
|
72
|
+
translation: string;
|
|
73
|
+
source: 'special' | 'common' | null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Cross-tab cache synchronization options
|
|
77
|
+
*/
|
|
78
|
+
export interface CrossTabOptions {
|
|
79
|
+
enabled: boolean;
|
|
80
|
+
channelName?: string;
|
|
81
|
+
storageKeyPrefix?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Automatic template extraction from text containing numeric variables
|
|
85
|
+
* @param text Original text that may contain numeric variables
|
|
86
|
+
* @returns Template extraction result
|
|
87
|
+
*/
|
|
88
|
+
export declare function extractTemplate(text: string): TemplateExtractResult;
|
|
89
|
+
/**
|
|
90
|
+
* Translation options
|
|
91
|
+
*/
|
|
92
|
+
export interface TranslationServiceOptions {
|
|
93
|
+
senseId: string;
|
|
94
|
+
fingerprint?: string;
|
|
95
|
+
fromLang?: string;
|
|
96
|
+
toLang?: string;
|
|
97
|
+
crossTab?: boolean;
|
|
98
|
+
crossTabChannelName?: string;
|
|
99
|
+
crossTabStorageKeyPrefix?: string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Cross-tab cache synchronization options for TranslationClient
|
|
103
|
+
*/
|
|
104
|
+
export interface ClientCrossTabOptions {
|
|
105
|
+
enabled: boolean;
|
|
106
|
+
channelName?: string;
|
|
107
|
+
storageKey?: string;
|
|
108
|
+
}
|
|
109
|
+
export interface TranslationClientConfig {
|
|
110
|
+
/** JWT authentication token */
|
|
111
|
+
token: string;
|
|
112
|
+
/** Translation sense ID */
|
|
113
|
+
senseId: string;
|
|
114
|
+
/** API base URL (optional, defaults to https://api.hottol.com/laker/) */
|
|
115
|
+
baseUrl?: string;
|
|
116
|
+
/** Request timeout in milliseconds (optional, defaults to 30000) */
|
|
117
|
+
timeout?: number;
|
|
118
|
+
/** LRU cache size (optional, defaults to 1000, set to 0 to disable cache) */
|
|
119
|
+
cacheSize?: number;
|
|
120
|
+
/** Initial fingerprint for personalized translations (optional) */
|
|
121
|
+
fingerprint?: string;
|
|
122
|
+
/** Enable cross-tab cache synchronization (optional, default: false) */
|
|
123
|
+
crossTab?: boolean;
|
|
124
|
+
/** Custom cross-tab channel name (optional) */
|
|
125
|
+
crossTabChannelName?: string;
|
|
126
|
+
/** Custom cross-tab storage key prefix (optional) */
|
|
127
|
+
crossTabStorageKeyPrefix?: string;
|
|
128
|
+
/** Enable cache (optional, default: true, set false to disable all caching) */
|
|
129
|
+
useCache?: boolean;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* TranslationClient - Main entry point for Laker Translation SDK
|
|
133
|
+
*
|
|
134
|
+
* Features:
|
|
135
|
+
* - Automatic cache lookup: preloaded translations → LRU cache → backend request
|
|
136
|
+
* - Fingerprint-based personalized translations support
|
|
137
|
+
* - Optional cross-browser-tab cache synchronization
|
|
138
|
+
* - Lazy initialization: automatically preloads on first use
|
|
139
|
+
* - Simple single-level API, no complex layering
|
|
140
|
+
*/
|
|
141
|
+
export declare class TranslationClient {
|
|
142
|
+
private baseUrl;
|
|
143
|
+
private token;
|
|
144
|
+
private timeout;
|
|
145
|
+
private config;
|
|
146
|
+
private llmCacheEnabled;
|
|
147
|
+
private llmCache;
|
|
148
|
+
private pool;
|
|
149
|
+
private currentFingerprint;
|
|
150
|
+
private initialized;
|
|
151
|
+
private initPromise;
|
|
152
|
+
private broadcastChannel;
|
|
153
|
+
private crossTabOptions;
|
|
154
|
+
private storageKey;
|
|
155
|
+
/**
|
|
156
|
+
* Create a new TranslationClient - the only entry point you need
|
|
157
|
+
* @param config Client configuration
|
|
158
|
+
*/
|
|
159
|
+
constructor(config: TranslationClientConfig);
|
|
160
|
+
/**
|
|
161
|
+
* Initialize cross-tab synchronization via Broadcast Channel
|
|
162
|
+
*/
|
|
163
|
+
private initCrossTabSync;
|
|
164
|
+
/**
|
|
165
|
+
* Load cache from localStorage
|
|
166
|
+
*/
|
|
167
|
+
private loadFromStorage;
|
|
168
|
+
/**
|
|
169
|
+
* Save cache to localStorage
|
|
170
|
+
*/
|
|
171
|
+
private saveToStorage;
|
|
172
|
+
/**
|
|
173
|
+
* Get all cache entries for storage/broadcast
|
|
174
|
+
*/
|
|
175
|
+
private getAllCacheEntries;
|
|
176
|
+
/**
|
|
177
|
+
* Broadcast full cache to other tabs
|
|
178
|
+
*/
|
|
179
|
+
private broadcastFullCache;
|
|
180
|
+
/**
|
|
181
|
+
* Broadcast a single cache update to other tabs
|
|
182
|
+
*/
|
|
183
|
+
private broadcastCacheUpdate;
|
|
184
|
+
/**
|
|
185
|
+
* Set or update the JWT authentication token
|
|
186
|
+
* @param token JWT token
|
|
187
|
+
*/
|
|
188
|
+
setToken(token: string): void;
|
|
189
|
+
/**
|
|
190
|
+
* Enable or disable LLM cache
|
|
191
|
+
* @param enabled Whether to enable cache
|
|
192
|
+
*/
|
|
193
|
+
setCacheEnabled(enabled: boolean): void;
|
|
194
|
+
/**
|
|
195
|
+
* Clear the LLM translation cache (also clears localStorage and broadcasts to other tabs)
|
|
196
|
+
*/
|
|
197
|
+
clearCache(): void;
|
|
198
|
+
/**
|
|
199
|
+
* Get current LLM cache size
|
|
200
|
+
*/
|
|
201
|
+
getCacheSize(): number;
|
|
202
|
+
/**
|
|
203
|
+
* Check if cross-tab synchronization is enabled
|
|
204
|
+
*/
|
|
205
|
+
isCrossTabEnabled(): boolean;
|
|
206
|
+
/**
|
|
207
|
+
凤 * GetSenseTranslate - One-shot unary request with pagination
|
|
208
|
+
* @param request Request parameters
|
|
209
|
+
*/
|
|
210
|
+
getSenseTranslate(request: GetSenseTranslateRequest): Promise<GetSenseTranslateResponse>;
|
|
2
211
|
/**
|
|
3
|
-
*
|
|
212
|
+
* TranslateStream - Server streaming, receives multiple batches progressively
|
|
213
|
+
* @param request Request parameters
|
|
214
|
+
* @param onBatch Callback for each batch received. Return false to stop streaming early.
|
|
215
|
+
*/
|
|
216
|
+
translateStream(request: TranslateStreamRequest, onBatch: (response: TranslateStreamResponse) => boolean | void): Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* Collect all streaming responses into an array
|
|
219
|
+
* @param request Request parameters
|
|
220
|
+
*/
|
|
221
|
+
translateStreamCollect(request: TranslateStreamRequest): Promise<TranslateStreamResponse[]>;
|
|
222
|
+
/**
|
|
223
|
+
* LLMTranslate - One-shot large language model translation
|
|
224
|
+
* Uses LRU cache to avoid repeated requests for the same text
|
|
225
|
+
* With cross-tab enabled, automatically syncs cache across browser tabs
|
|
226
|
+
* @param request Translation request
|
|
227
|
+
* @param skipCache If true, bypass cache and always request from backend
|
|
228
|
+
*/
|
|
229
|
+
llmTranslate(request: LLMTranslateRequest, skipCache?: boolean): Promise<LLMTranslateResponse>;
|
|
230
|
+
/**
|
|
231
|
+
* Translate text - this is the main method you need
|
|
4
232
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
233
|
+
* Workflow:
|
|
234
|
+
* 1. Check pre-loaded translation pool (provided fingerprint → current fingerprint → common)
|
|
235
|
+
* 2. If found, return immediately from cache
|
|
236
|
+
* 3. If not found, request LLM translation from backend
|
|
237
|
+
* 4. Auto-initialize on first call
|
|
134
238
|
*
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
* @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;
|
|
239
|
+
* @param text Text to translate
|
|
240
|
+
* @param toLang Target language
|
|
241
|
+
* @param fromLang Source language (optional, auto-detected if not provided)
|
|
242
|
+
* @param fingerprint Optional specific fingerprint for this translation (overrides client-level fingerprint)
|
|
243
|
+
* @returns Translated text
|
|
244
|
+
*/
|
|
245
|
+
translate(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<string>;
|
|
246
|
+
/**
|
|
247
|
+
* Translate text with full response details
|
|
248
|
+
* @param text Text to translate
|
|
249
|
+
* @param toLang Target language
|
|
250
|
+
* @param fromLang Source language (optional)
|
|
251
|
+
* @param fingerprint Optional specific fingerprint for this translation (overrides client-level fingerprint)
|
|
252
|
+
* @returns Full translation response
|
|
253
|
+
*/
|
|
254
|
+
translateWithDetails(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<LLMTranslateResponse>;
|
|
255
|
+
/**
|
|
256
|
+
* Translate without using cache (always request from backend)
|
|
257
|
+
* @param text Text to translate
|
|
258
|
+
* @param toLang Target language
|
|
259
|
+
* @param fromLang Source language (optional)
|
|
260
|
+
* @param fingerprint Optional specific fingerprint for this translation
|
|
261
|
+
* @returns Translated text
|
|
262
|
+
*/
|
|
263
|
+
translateNoCache(text: string, toLang: string, fromLang?: string, fingerprint?: string): Promise<string>;
|
|
264
|
+
/**
|
|
265
|
+
* Batch translate multiple texts
|
|
266
|
+
* @param texts Array of texts to translate
|
|
267
|
+
* @param toLang Target language
|
|
268
|
+
* @param fromLang Source language (optional)
|
|
269
|
+
* @param fingerprint Optional specific fingerprint for all translations in this batch
|
|
270
|
+
* @returns Array of translated texts in same order
|
|
271
|
+
*/
|
|
272
|
+
translateBatch(texts: string[], toLang: string, fromLang?: string, fingerprint?: string): Promise<string[]>;
|
|
273
|
+
/**
|
|
274
|
+
* Initialize and preload all translations
|
|
275
|
+
* Call this to warm up cache before translating
|
|
276
|
+
*/
|
|
277
|
+
preload(): Promise<void>;
|
|
278
|
+
/**
|
|
279
|
+
* Internal initialization - preloads translations via streaming
|
|
280
|
+
*/
|
|
281
|
+
private initialize;
|
|
282
|
+
/**
|
|
283
|
+
* Check if service is initialized
|
|
284
|
+
*/
|
|
285
|
+
isInitialized(): boolean;
|
|
286
|
+
/**
|
|
287
|
+
* Set or change the current fingerprint
|
|
288
|
+
* Automatically loads special translations for this fingerprint
|
|
289
|
+
* @param fingerprint The fingerprint to use
|
|
290
|
+
*/
|
|
291
|
+
setFingerprint(fingerprint: string): Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Clear the current fingerprint
|
|
294
|
+
* Falls back to common translations
|
|
295
|
+
*/
|
|
296
|
+
clearFingerprint(): void;
|
|
297
|
+
/**
|
|
298
|
+
* Get the current fingerprint
|
|
299
|
+
*/
|
|
300
|
+
getFingerprint(): string | null;
|
|
301
|
+
/**
|
|
302
|
+
* Check if cache is enabled
|
|
303
|
+
* @returns true if cache is enabled
|
|
304
|
+
*/
|
|
305
|
+
isCacheEnabled(): boolean;
|
|
306
|
+
/**
|
|
307
|
+
* Check if a translation exists in pre-loaded pool
|
|
308
|
+
* @param text Text to check
|
|
309
|
+
* @param fingerprint Optional specific fingerprint to check
|
|
310
|
+
* @returns true if translation exists in cache
|
|
311
|
+
*/
|
|
312
|
+
hasTranslation(text: string, fingerprint?: string): boolean;
|
|
313
|
+
/**
|
|
314
|
+
* Get translation from pre-loaded cache without requesting from backend
|
|
315
|
+
* @param text Text to look up
|
|
316
|
+
* @param fingerprint Optional specific fingerprint to look up
|
|
317
|
+
* @returns Translation if found, null otherwise
|
|
318
|
+
*/
|
|
319
|
+
getCached(text: string, fingerprint?: string): string | null;
|
|
320
|
+
/**
|
|
321
|
+
* Add a custom translation to the pre-loaded pool
|
|
322
|
+
* @param text Original text
|
|
323
|
+
* @param translation Translated text
|
|
324
|
+
* @param fingerprint Optional specific fingerprint to add to
|
|
325
|
+
*/
|
|
326
|
+
addTranslation(text: string, translation: string, fingerprint?: string): void;
|
|
327
|
+
/**
|
|
328
|
+
* Clear all cached translations
|
|
329
|
+
*/
|
|
330
|
+
clearAllCache(): void;
|
|
331
|
+
/**
|
|
332
|
+
* Destroy the instance and free resources
|
|
333
|
+
* Call this when the instance is no longer needed
|
|
334
|
+
*/
|
|
335
|
+
destroy(): void;
|
|
336
|
+
/**
|
|
337
|
+
* LLMTranslateStream - Streaming large language model translation
|
|
338
|
+
* Note: Streaming requests are not cached
|
|
339
|
+
* @param request Translation request
|
|
340
|
+
* @param onResponse Callback for each response chunk
|
|
341
|
+
*/
|
|
342
|
+
llmTranslateStream(request: LLMTranslateRequest, onResponse: (response: LLMTranslateResponse) => boolean | void): Promise<void>;
|
|
343
|
+
private getHeaders;
|
|
344
|
+
private fetchJson;
|
|
345
|
+
private fetchWithTimeout;
|
|
357
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* Create a TranslationClient instance with simplified configuration
|
|
349
|
+
* @param token JWT authentication token
|
|
350
|
+
* @param senseId Translation sense ID
|
|
351
|
+
* @param options Additional options
|
|
352
|
+
* @returns TranslationClient instance
|
|
353
|
+
*/
|
|
354
|
+
export declare function createTranslation(token: string, senseId: string, options?: Partial<TranslationClientConfig>): TranslationClient;
|
|
355
|
+
export default createTranslation;
|
package/translation-client.js
CHANGED
|
@@ -1,23 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.LakerTranslation = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
25
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
26
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
27
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
28
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
29
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
35
|
+
var e = new Error(message);
|
|
36
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Translation Service gRPC-Web TypeScript/JavaScript Client
|
|
41
|
+
*
|
|
42
|
+
* Auto-generated for api.laker.dev Translation Service
|
|
43
|
+
* Service: TranslationService
|
|
44
|
+
* Source: proto/translation.proto
|
|
45
|
+
*/
|
|
21
46
|
const defaultCrossTabOptions = {
|
|
22
47
|
enabled: false,
|
|
23
48
|
channelName: 'laker-translation-cache',
|
|
@@ -54,7 +79,6 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
54
79
|
variables
|
|
55
80
|
};
|
|
56
81
|
}
|
|
57
|
-
exports.extractTemplate = extractTemplate;
|
|
58
82
|
/**
|
|
59
83
|
* TranslationPool - Multi-fingerprint translation cache with automatic common preloading
|
|
60
84
|
*
|
|
@@ -240,12 +264,9 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
240
264
|
*/
|
|
241
265
|
handleCacheClear(message) {
|
|
242
266
|
const fp = message.fingerprint || 'common';
|
|
243
|
-
|
|
267
|
+
{
|
|
244
268
|
this.clearFingerprint(fp);
|
|
245
269
|
}
|
|
246
|
-
else {
|
|
247
|
-
this.clearAll();
|
|
248
|
-
}
|
|
249
270
|
}
|
|
250
271
|
/**
|
|
251
272
|
* Handle initial sync request from a new tab
|
|
@@ -550,7 +571,7 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
550
571
|
* Usage:
|
|
551
572
|
* - Simple: Just create and call translate() - initialization is automatic
|
|
552
573
|
* - Advanced: Call initialize() explicitly to preload all translations upfront
|
|
553
|
-
|
|
574
|
+
*/
|
|
554
575
|
/**
|
|
555
576
|
* Simple LRU Cache implementation for TranslationClient
|
|
556
577
|
* Uses Map with access order for O(1) get/set operations
|
|
@@ -1226,7 +1247,6 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
1226
1247
|
});
|
|
1227
1248
|
}
|
|
1228
1249
|
}
|
|
1229
|
-
exports.TranslationClient = TranslationClient;
|
|
1230
1250
|
/**
|
|
1231
1251
|
* Create a TranslationClient instance with simplified configuration
|
|
1232
1252
|
* @param token JWT authentication token
|
|
@@ -1238,8 +1258,6 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
1238
1258
|
return new TranslationClient(Object.assign({ token,
|
|
1239
1259
|
senseId }, options));
|
|
1240
1260
|
}
|
|
1241
|
-
exports.createTranslation = createTranslation;
|
|
1242
|
-
exports.default = createTranslation;
|
|
1243
1261
|
// Auto-export to global for browser usage
|
|
1244
1262
|
if (typeof window !== 'undefined') {
|
|
1245
1263
|
window.LakerTranslation = {
|
|
@@ -1248,4 +1266,12 @@ define("translation-client", ["require", "exports"], function (require, exports)
|
|
|
1248
1266
|
default: createTranslation
|
|
1249
1267
|
};
|
|
1250
1268
|
}
|
|
1251
|
-
|
|
1269
|
+
|
|
1270
|
+
exports.TranslationClient = TranslationClient;
|
|
1271
|
+
exports.createTranslation = createTranslation;
|
|
1272
|
+
exports.default = createTranslation;
|
|
1273
|
+
exports.extractTemplate = extractTemplate;
|
|
1274
|
+
|
|
1275
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1276
|
+
|
|
1277
|
+
}));
|