koztv-blog-tools 1.0.5 → 1.0.6
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/dist/index.d.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +6 -52
- package/dist/index.mjs +6 -52
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -115,22 +115,22 @@ declare function trackLearnMore(service: string): void;
|
|
|
115
115
|
declare function trackServiceClick(service: string): void;
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
* Translation utilities using
|
|
118
|
+
* Translation utilities using OpenAI-compatible APIs
|
|
119
119
|
*/
|
|
120
120
|
interface TranslateOptions {
|
|
121
|
-
/** API key
|
|
121
|
+
/** API key */
|
|
122
122
|
apiKey: string;
|
|
123
|
+
/** API base URL (e.g., https://api.openai.com/v1, https://open.bigmodel.cn/api/paas/v4) */
|
|
124
|
+
apiUrl: string;
|
|
125
|
+
/** Model name (e.g., gpt-4o-mini, GLM-4.7-Flash) */
|
|
126
|
+
model: string;
|
|
123
127
|
/** Target language (default: 'en') */
|
|
124
128
|
targetLang?: string;
|
|
125
129
|
/** Source language (default: 'ru') */
|
|
126
130
|
sourceLang?: string;
|
|
127
|
-
/** API provider (default: 'glm') */
|
|
128
|
-
provider?: 'glm' | 'openai';
|
|
129
|
-
/** Model to use (default depends on provider) */
|
|
130
|
-
model?: string;
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
|
-
* Translate text
|
|
133
|
+
* Translate text using any OpenAI-compatible API
|
|
134
134
|
*/
|
|
135
135
|
declare function translateContent(text: string, options: TranslateOptions): Promise<string>;
|
|
136
136
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -115,22 +115,22 @@ declare function trackLearnMore(service: string): void;
|
|
|
115
115
|
declare function trackServiceClick(service: string): void;
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
* Translation utilities using
|
|
118
|
+
* Translation utilities using OpenAI-compatible APIs
|
|
119
119
|
*/
|
|
120
120
|
interface TranslateOptions {
|
|
121
|
-
/** API key
|
|
121
|
+
/** API key */
|
|
122
122
|
apiKey: string;
|
|
123
|
+
/** API base URL (e.g., https://api.openai.com/v1, https://open.bigmodel.cn/api/paas/v4) */
|
|
124
|
+
apiUrl: string;
|
|
125
|
+
/** Model name (e.g., gpt-4o-mini, GLM-4.7-Flash) */
|
|
126
|
+
model: string;
|
|
123
127
|
/** Target language (default: 'en') */
|
|
124
128
|
targetLang?: string;
|
|
125
129
|
/** Source language (default: 'ru') */
|
|
126
130
|
sourceLang?: string;
|
|
127
|
-
/** API provider (default: 'glm') */
|
|
128
|
-
provider?: 'glm' | 'openai';
|
|
129
|
-
/** Model to use (default depends on provider) */
|
|
130
|
-
model?: string;
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
|
-
* Translate text
|
|
133
|
+
* Translate text using any OpenAI-compatible API
|
|
134
134
|
*/
|
|
135
135
|
declare function translateContent(text: string, options: TranslateOptions): Promise<string>;
|
|
136
136
|
/**
|
package/dist/index.js
CHANGED
|
@@ -309,10 +309,8 @@ function trackServiceClick(service) {
|
|
|
309
309
|
}
|
|
310
310
|
|
|
311
311
|
// src/translate.ts
|
|
312
|
-
async function
|
|
313
|
-
const model =
|
|
314
|
-
const targetLang = options.targetLang || "en";
|
|
315
|
-
const sourceLang = options.sourceLang || "ru";
|
|
312
|
+
async function translateContent(text, options) {
|
|
313
|
+
const { apiKey, apiUrl, model, targetLang = "en", sourceLang = "ru" } = options;
|
|
316
314
|
const messages = [
|
|
317
315
|
{
|
|
318
316
|
role: "system",
|
|
@@ -326,11 +324,12 @@ Do not add any explanations or notes.`
|
|
|
326
324
|
content: text
|
|
327
325
|
}
|
|
328
326
|
];
|
|
329
|
-
const
|
|
327
|
+
const endpoint = apiUrl.endsWith("/") ? `${apiUrl}chat/completions` : `${apiUrl}/chat/completions`;
|
|
328
|
+
const response = await fetch(endpoint, {
|
|
330
329
|
method: "POST",
|
|
331
330
|
headers: {
|
|
332
331
|
"Content-Type": "application/json",
|
|
333
|
-
"Authorization": `Bearer ${
|
|
332
|
+
"Authorization": `Bearer ${apiKey}`
|
|
334
333
|
},
|
|
335
334
|
body: JSON.stringify({
|
|
336
335
|
model,
|
|
@@ -340,56 +339,11 @@ Do not add any explanations or notes.`
|
|
|
340
339
|
});
|
|
341
340
|
if (!response.ok) {
|
|
342
341
|
const error = await response.text();
|
|
343
|
-
throw new Error(`
|
|
342
|
+
throw new Error(`API error: ${response.status} - ${error}`);
|
|
344
343
|
}
|
|
345
344
|
const data = await response.json();
|
|
346
345
|
return data.choices[0]?.message?.content || "";
|
|
347
346
|
}
|
|
348
|
-
async function translateWithOpenAI(text, options) {
|
|
349
|
-
const model = options.model || "gpt-4o-mini";
|
|
350
|
-
const targetLang = options.targetLang || "en";
|
|
351
|
-
const sourceLang = options.sourceLang || "ru";
|
|
352
|
-
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
353
|
-
method: "POST",
|
|
354
|
-
headers: {
|
|
355
|
-
"Content-Type": "application/json",
|
|
356
|
-
"Authorization": `Bearer ${options.apiKey}`
|
|
357
|
-
},
|
|
358
|
-
body: JSON.stringify({
|
|
359
|
-
model,
|
|
360
|
-
messages: [
|
|
361
|
-
{
|
|
362
|
-
role: "system",
|
|
363
|
-
content: `You are a professional translator. Translate the following text from ${sourceLang} to ${targetLang}.
|
|
364
|
-
Keep the markdown formatting intact.
|
|
365
|
-
Only output the translated text, nothing else.`
|
|
366
|
-
},
|
|
367
|
-
{
|
|
368
|
-
role: "user",
|
|
369
|
-
content: text
|
|
370
|
-
}
|
|
371
|
-
],
|
|
372
|
-
temperature: 0.3
|
|
373
|
-
})
|
|
374
|
-
});
|
|
375
|
-
if (!response.ok) {
|
|
376
|
-
const error = await response.text();
|
|
377
|
-
throw new Error(`OpenAI API error: ${response.status} - ${error}`);
|
|
378
|
-
}
|
|
379
|
-
const data = await response.json();
|
|
380
|
-
return data.choices[0]?.message?.content || "";
|
|
381
|
-
}
|
|
382
|
-
async function translateContent(text, options) {
|
|
383
|
-
const provider = options.provider || "glm";
|
|
384
|
-
switch (provider) {
|
|
385
|
-
case "glm":
|
|
386
|
-
return translateWithGLM(text, options);
|
|
387
|
-
case "openai":
|
|
388
|
-
return translateWithOpenAI(text, options);
|
|
389
|
-
default:
|
|
390
|
-
throw new Error(`Unknown provider: ${provider}`);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
347
|
async function translateTitle(title, options) {
|
|
394
348
|
const translated = await translateContent(title, options);
|
|
395
349
|
return translated.replace(/^["']|["']$/g, "").trim();
|
package/dist/index.mjs
CHANGED
|
@@ -256,10 +256,8 @@ function trackServiceClick(service) {
|
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
// src/translate.ts
|
|
259
|
-
async function
|
|
260
|
-
const model =
|
|
261
|
-
const targetLang = options.targetLang || "en";
|
|
262
|
-
const sourceLang = options.sourceLang || "ru";
|
|
259
|
+
async function translateContent(text, options) {
|
|
260
|
+
const { apiKey, apiUrl, model, targetLang = "en", sourceLang = "ru" } = options;
|
|
263
261
|
const messages = [
|
|
264
262
|
{
|
|
265
263
|
role: "system",
|
|
@@ -273,11 +271,12 @@ Do not add any explanations or notes.`
|
|
|
273
271
|
content: text
|
|
274
272
|
}
|
|
275
273
|
];
|
|
276
|
-
const
|
|
274
|
+
const endpoint = apiUrl.endsWith("/") ? `${apiUrl}chat/completions` : `${apiUrl}/chat/completions`;
|
|
275
|
+
const response = await fetch(endpoint, {
|
|
277
276
|
method: "POST",
|
|
278
277
|
headers: {
|
|
279
278
|
"Content-Type": "application/json",
|
|
280
|
-
"Authorization": `Bearer ${
|
|
279
|
+
"Authorization": `Bearer ${apiKey}`
|
|
281
280
|
},
|
|
282
281
|
body: JSON.stringify({
|
|
283
282
|
model,
|
|
@@ -287,56 +286,11 @@ Do not add any explanations or notes.`
|
|
|
287
286
|
});
|
|
288
287
|
if (!response.ok) {
|
|
289
288
|
const error = await response.text();
|
|
290
|
-
throw new Error(`
|
|
289
|
+
throw new Error(`API error: ${response.status} - ${error}`);
|
|
291
290
|
}
|
|
292
291
|
const data = await response.json();
|
|
293
292
|
return data.choices[0]?.message?.content || "";
|
|
294
293
|
}
|
|
295
|
-
async function translateWithOpenAI(text, options) {
|
|
296
|
-
const model = options.model || "gpt-4o-mini";
|
|
297
|
-
const targetLang = options.targetLang || "en";
|
|
298
|
-
const sourceLang = options.sourceLang || "ru";
|
|
299
|
-
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
300
|
-
method: "POST",
|
|
301
|
-
headers: {
|
|
302
|
-
"Content-Type": "application/json",
|
|
303
|
-
"Authorization": `Bearer ${options.apiKey}`
|
|
304
|
-
},
|
|
305
|
-
body: JSON.stringify({
|
|
306
|
-
model,
|
|
307
|
-
messages: [
|
|
308
|
-
{
|
|
309
|
-
role: "system",
|
|
310
|
-
content: `You are a professional translator. Translate the following text from ${sourceLang} to ${targetLang}.
|
|
311
|
-
Keep the markdown formatting intact.
|
|
312
|
-
Only output the translated text, nothing else.`
|
|
313
|
-
},
|
|
314
|
-
{
|
|
315
|
-
role: "user",
|
|
316
|
-
content: text
|
|
317
|
-
}
|
|
318
|
-
],
|
|
319
|
-
temperature: 0.3
|
|
320
|
-
})
|
|
321
|
-
});
|
|
322
|
-
if (!response.ok) {
|
|
323
|
-
const error = await response.text();
|
|
324
|
-
throw new Error(`OpenAI API error: ${response.status} - ${error}`);
|
|
325
|
-
}
|
|
326
|
-
const data = await response.json();
|
|
327
|
-
return data.choices[0]?.message?.content || "";
|
|
328
|
-
}
|
|
329
|
-
async function translateContent(text, options) {
|
|
330
|
-
const provider = options.provider || "glm";
|
|
331
|
-
switch (provider) {
|
|
332
|
-
case "glm":
|
|
333
|
-
return translateWithGLM(text, options);
|
|
334
|
-
case "openai":
|
|
335
|
-
return translateWithOpenAI(text, options);
|
|
336
|
-
default:
|
|
337
|
-
throw new Error(`Unknown provider: ${provider}`);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
294
|
async function translateTitle(title, options) {
|
|
341
295
|
const translated = await translateContent(title, options);
|
|
342
296
|
return translated.replace(/^["']|["']$/g, "").trim();
|