mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 18:22:42 -08:00
refactor: extract overlay shortcuts runtime for task 27.2
This commit is contained in:
103
src/anki-integration/ai.ts
Normal file
103
src/anki-integration/ai.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import axios from "axios";
|
||||
|
||||
import { DEFAULT_ANKI_CONNECT_CONFIG } from "../config";
|
||||
|
||||
const DEFAULT_AI_SYSTEM_PROMPT =
|
||||
"You are a translation engine. Return only the translated text with no explanations.";
|
||||
|
||||
export function extractAiText(content: unknown): string {
|
||||
if (typeof content === "string") {
|
||||
return content.trim();
|
||||
}
|
||||
if (!Array.isArray(content)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
for (const item of content) {
|
||||
if (
|
||||
item &&
|
||||
typeof item === "object" &&
|
||||
"type" in item &&
|
||||
(item as { type?: unknown }).type === "text" &&
|
||||
"text" in item &&
|
||||
typeof (item as { text?: unknown }).text === "string"
|
||||
) {
|
||||
parts.push((item as { text: string }).text);
|
||||
}
|
||||
}
|
||||
|
||||
return parts.join("").trim();
|
||||
}
|
||||
|
||||
export function normalizeOpenAiBaseUrl(baseUrl: string): string {
|
||||
const trimmed = baseUrl.trim().replace(/\/+$/, "");
|
||||
if (/\/v1$/i.test(trimmed)) {
|
||||
return trimmed;
|
||||
}
|
||||
return `${trimmed}/v1`;
|
||||
}
|
||||
|
||||
export interface AiTranslateRequest {
|
||||
sentence: string;
|
||||
apiKey: string;
|
||||
baseUrl?: string;
|
||||
model?: string;
|
||||
targetLanguage?: string;
|
||||
systemPrompt?: string;
|
||||
}
|
||||
|
||||
export interface AiTranslateCallbacks {
|
||||
logWarning: (message: string) => void;
|
||||
}
|
||||
|
||||
export async function translateSentenceWithAi(
|
||||
request: AiTranslateRequest,
|
||||
callbacks: AiTranslateCallbacks,
|
||||
): Promise<string | null> {
|
||||
const aiConfig = DEFAULT_ANKI_CONNECT_CONFIG.ai;
|
||||
if (!request.apiKey.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const baseUrl = normalizeOpenAiBaseUrl(
|
||||
request.baseUrl || aiConfig.baseUrl || "https://openrouter.ai/api",
|
||||
);
|
||||
const model = request.model || "openai/gpt-4o-mini";
|
||||
const targetLanguage = request.targetLanguage || "English";
|
||||
const prompt =
|
||||
request.systemPrompt?.trim() || DEFAULT_AI_SYSTEM_PROMPT;
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${baseUrl}/chat/completions`,
|
||||
{
|
||||
model,
|
||||
temperature: 0,
|
||||
messages: [
|
||||
{ role: "system", content: prompt },
|
||||
{
|
||||
role: "user",
|
||||
content: `Translate this text to ${targetLanguage}:\n\n${request.sentence}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${request.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
timeout: 15000,
|
||||
},
|
||||
);
|
||||
const content = (response.data as { choices?: unknown[] })?.choices?.[0] as
|
||||
| { message?: { content?: unknown } }
|
||||
| undefined;
|
||||
return extractAiText(content?.message?.content) || null;
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown translation error";
|
||||
callbacks.logWarning(`AI translation failed: ${message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user