Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/translate/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Translate command', () => {
test(
'should fail on unknown provider',
'--provider unknown --folder 1',
`error: option '--provider <value>' argument 'unknown' is invalid. Allowed choices are yandex.`,
`error: option '--provider <value>' argument 'unknown' is invalid. Allowed choices are yandex, yandexgpt, openai, openrouter, anthropic.`,
);

test('should handle default', '--folder 1', {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {DESCRIPTION, NAME, options} from './config';
import {Extract} from './commands/extract';
import {Compose} from './commands/compose';
import {Extension as YandexTranslation} from './providers/yandex';
import {Extension as AITranslation} from './providers/ai';
import {resolveSource, resolveTargets, resolveVars} from './utils';
import {Run} from './run';
import {configDefaults} from './utils/config';
Expand Down Expand Up @@ -89,6 +90,7 @@ export class Translate extends BaseProgram<TranslateConfig, TranslateArgs> {
this.extract,
this.compose,
new YandexTranslation(),
new AITranslation(),
new ExtractOpenapiIncluderFakeExtension(),
];

Expand Down
35 changes: 35 additions & 0 deletions src/commands/translate/providers/ai/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {existsSync, readFileSync} from 'node:fs';

const YANDEX_BEARER_PREFIXES = ['y0_', 't1.'];

/**
* Resolves an auth token from a CLI value.
* The value may be either the raw token or a path to a file containing the token.
*/
export function resolveToken(value: string): string {
if (!value) {
throw new Error('No auth token provided');
}

const trimmed = value.trim();

if (existsSync(trimmed)) {
return readFileSync(trimmed, 'utf8').trim();
}

return trimmed;
}

/**
* Builds an Authorization header value for Yandex AI Studio.
* Supports IAM/OAuth tokens (Bearer) and service-account API keys (Api-Key).
*/
export function yandexAuthHeader(token: string): string {
for (const prefix of YANDEX_BEARER_PREFIXES) {
if (token.startsWith(prefix)) {
return 'Bearer ' + token;
}
}

return 'Api-Key ' + token;
}
91 changes: 91 additions & 0 deletions src/commands/translate/providers/ai/clients/anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type {ChatMessage, CompletionOptions, CompletionResult, LLMClient} from './types';

import axios from 'axios';

import {LLMResponseError, throwLLMError} from '../utils';

const DEFAULT_BASE_URL = 'https://api.anthropic.com/v1';
const ANTHROPIC_VERSION = '2023-06-01';

type AnthropicMessagesResponse = {
content: {type: string; text?: string}[];
stop_reason: string;
usage?: {
input_tokens?: number;
output_tokens?: number;
};
};

export type AnthropicClientOptions = {
token: string;
model: string;
baseUrl?: string;
timeout?: number;
};

export class AnthropicClient implements LLMClient {
readonly name = 'anthropic';

private readonly token: string;
private readonly model: string;
private readonly baseUrl: string;
private readonly timeout: number;

constructor(options: AnthropicClientOptions) {
this.token = options.token;
this.model = options.model;
this.baseUrl = (options.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, '');
this.timeout = options.timeout ?? 60_000;
}

async complete(messages: ChatMessage[], options: CompletionOptions): Promise<CompletionResult> {
const system = messages
.filter((m) => m.role === 'system')
.map((m) => m.content)
.join('\n\n');
const conversation = messages
.filter((m) => m.role !== 'system')
.map((m) => ({role: m.role, content: m.content}));

try {
const {data} = await axios.post<AnthropicMessagesResponse>(
`${this.baseUrl}/messages`,
{
model: this.model,
system: system || undefined,
messages: conversation,
temperature: options.temperature,
max_tokens: options.maxTokens,
},
{
timeout: this.timeout,
headers: {
'x-api-key': this.token,
'anthropic-version': ANTHROPIC_VERSION,
'Content-Type': 'application/json',
'User-Agent': 'github.com/diplodoc-platform/cli',
},
},
);

const text = data.content
.filter((block) => block.type === 'text' && block.text)
.map((block) => block.text as string)
.join('');

if (!text) {
throw new LLMResponseError('Anthropic returned an empty response');
}

return {
text,
usage: {
inputTokens: data.usage?.input_tokens ?? 0,
outputTokens: data.usage?.output_tokens ?? 0,
},
};
} catch (error) {
throwLLMError(error, 'Anthropic');
}
}
}
113 changes: 113 additions & 0 deletions src/commands/translate/providers/ai/clients/openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type {ChatMessage, CompletionOptions, CompletionResult, LLMClient} from './types';

import axios from 'axios';

import {LLMResponseError, throwLLMError} from '../utils';

const DEFAULT_OPENAI_BASE_URL = 'https://api.openai.com/v1';
const DEFAULT_OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1';

type OpenAIChatResponse = {
choices: {
message: {role: string; content: string};
finish_reason: string;
}[];
usage?: {
prompt_tokens?: number;
completion_tokens?: number;
total_tokens?: number;
};
};

export type OpenAICompatibleClientOptions = {
token: string;
model: string;
baseUrl?: string;
timeout?: number;
extraHeaders?: Record<string, string>;
name?: string;
};

export class OpenAICompatibleClient implements LLMClient {
readonly name: string;

private readonly token: string;
private readonly model: string;
private readonly baseUrl: string;
private readonly timeout: number;
private readonly extraHeaders: Record<string, string>;

constructor(options: OpenAICompatibleClientOptions) {
this.name = options.name || 'openai';
this.token = options.token;
this.model = options.model;
this.baseUrl = (options.baseUrl || DEFAULT_OPENAI_BASE_URL).replace(/\/$/, '');
this.timeout = options.timeout ?? 60_000;
this.extraHeaders = options.extraHeaders || {};
}

async complete(messages: ChatMessage[], options: CompletionOptions): Promise<CompletionResult> {
try {
const {data} = await axios.post<OpenAIChatResponse>(
`${this.baseUrl}/chat/completions`,
{
model: this.model,
messages: messages.map((m) => ({role: m.role, content: m.content})),
temperature: options.temperature,
max_tokens: options.maxTokens,
},
{
timeout: this.timeout,
headers: {
Authorization: `Bearer ${this.token}`,
'Content-Type': 'application/json',
'User-Agent': 'github.com/diplodoc-platform/cli',
...this.extraHeaders,
},
},
);

const text = data.choices?.[0]?.message?.content;
if (!text) {
throw new LLMResponseError(`${this.name} returned an empty response`);
}

return {
text,
usage: {
inputTokens: data.usage?.prompt_tokens ?? 0,
outputTokens: data.usage?.completion_tokens ?? 0,
},
};
} catch (error) {
throwLLMError(error, this.name);
}
}
}

export function createOpenAIClient(
opts: Omit<OpenAICompatibleClientOptions, 'name' | 'baseUrl'> & {
baseUrl?: string;
},
) {
return new OpenAICompatibleClient({
...opts,
name: 'openai',
baseUrl: opts.baseUrl || DEFAULT_OPENAI_BASE_URL,
});
}

export function createOpenRouterClient(
opts: Omit<OpenAICompatibleClientOptions, 'name' | 'baseUrl'> & {baseUrl?: string},
) {
return new OpenAICompatibleClient({
...opts,
name: 'openrouter',
baseUrl: opts.baseUrl || DEFAULT_OPENROUTER_BASE_URL,
extraHeaders: {
'HTTP-Referer': 'https://github.com/diplodoc-platform/cli',
'X-Title': 'Diplodoc CLI',
...(opts.extraHeaders || {}),

Check warning on line 110 in src/commands/translate/providers/ai/clients/openai.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The empty object is useless.

See more on https://sonarcloud.io/project/issues?id=diplodoc-platform_cli&issues=AZ9ODKgI522HQpBMpyw4&open=AZ9ODKgI522HQpBMpyw4&pullRequest=1906
},
});
}
26 changes: 26 additions & 0 deletions src/commands/translate/providers/ai/clients/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type ChatRole = 'system' | 'user' | 'assistant';

export type ChatMessage = {
role: ChatRole;
content: string;
};

export type CompletionOptions = {
temperature: number;
maxTokens: number;
};

export type CompletionUsage = {
inputTokens: number;
outputTokens: number;
};

export type CompletionResult = {
text: string;
usage?: CompletionUsage;
};

export interface LLMClient {
readonly name: string;
complete(messages: ChatMessage[], options: CompletionOptions): Promise<CompletionResult>;
}
Loading
Loading