Rewrite frontend as single self-contained HTML file — all CSS/JS inline, no external files to fail loading
This commit is contained in:
161
dist/providers/vision/openRouterVisionProvider.js
vendored
Normal file
161
dist/providers/vision/openRouterVisionProvider.js
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.OpenRouterVisionProvider = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const axios_1 = __importDefault(require("axios"));
|
||||
class OpenRouterVisionProvider {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.axiosInstance = axios_1.default.create({
|
||||
baseURL: config.baseUrl || 'https://openrouter.ai/api/v1',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${config.apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
'HTTP-Referer': 'https://github.com/anomalyco/aidio-description',
|
||||
'X-Title': 'Aidio Description Generator'
|
||||
}
|
||||
});
|
||||
}
|
||||
async describeImage(imagePath, prompt) {
|
||||
try {
|
||||
const imageData = fs_1.default.readFileSync(imagePath);
|
||||
const base64Image = imageData.toString('base64');
|
||||
const response = await this.axiosInstance.post('/chat/completions', {
|
||||
model: this.config.model,
|
||||
temperature: 0.1,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: prompt },
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64Image}`
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
max_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
const data = response.data;
|
||||
return {
|
||||
description: data.choices?.[0]?.message?.content?.trim() || 'No description generated.',
|
||||
usage: {
|
||||
inputTokens: data.usage?.prompt_tokens || 0,
|
||||
outputTokens: data.usage?.completion_tokens || 0,
|
||||
totalTokens: data.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('OpenRouter describeImage error:', error.response?.data || error.message);
|
||||
return {
|
||||
description: 'Unable to describe this image.',
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
async compareImages(image1Path, image2Path, prompt) {
|
||||
try {
|
||||
const image1Data = fs_1.default.readFileSync(image1Path);
|
||||
const image2Data = fs_1.default.readFileSync(image2Path);
|
||||
const base64Image1 = image1Data.toString('base64');
|
||||
const base64Image2 = image2Data.toString('base64');
|
||||
const response = await this.axiosInstance.post('/chat/completions', {
|
||||
model: this.config.model,
|
||||
temperature: 0.1,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: prompt },
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: { url: `data:image/jpeg;base64,${base64Image1}` }
|
||||
},
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: { url: `data:image/jpeg;base64,${base64Image2}` }
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
max_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
const data = response.data;
|
||||
return {
|
||||
description: data.choices?.[0]?.message?.content?.trim() || 'No description generated.',
|
||||
usage: {
|
||||
inputTokens: data.usage?.prompt_tokens || 0,
|
||||
outputTokens: data.usage?.completion_tokens || 0,
|
||||
totalTokens: data.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('OpenRouter compareImages error:', error.response?.data || error.message);
|
||||
return {
|
||||
description: 'Unable to describe the differences between these images.',
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
async describeBatch(imagePaths, lastBatchContext, prompt) {
|
||||
try {
|
||||
const imagesBase64 = imagePaths.map(fp => {
|
||||
const imageData = fs_1.default.readFileSync(fp);
|
||||
return imageData.toString('base64');
|
||||
});
|
||||
const messages = [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: prompt }
|
||||
]
|
||||
}
|
||||
];
|
||||
if (lastBatchContext && lastBatchContext.lastDescription) {
|
||||
messages.unshift({
|
||||
role: 'system',
|
||||
content: `Previous batch summary: ${lastBatchContext.lastDescription}`
|
||||
});
|
||||
}
|
||||
imagesBase64.forEach(base64 => {
|
||||
messages[messages.length - 1].content.push({
|
||||
type: 'image_url',
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64}`
|
||||
}
|
||||
});
|
||||
});
|
||||
const response = await this.axiosInstance.post('/chat/completions', {
|
||||
model: this.config.model,
|
||||
messages,
|
||||
max_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
const data = response.data;
|
||||
return {
|
||||
description: data.choices?.[0]?.message?.content?.trim() || 'No description generated.',
|
||||
usage: {
|
||||
inputTokens: data.usage?.prompt_tokens || 0,
|
||||
outputTokens: data.usage?.completion_tokens || 0,
|
||||
totalTokens: data.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('OpenRouter describeBatch error:', error.response?.data || error.message);
|
||||
return {
|
||||
description: 'Unable to describe this batch of images.',
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.OpenRouterVisionProvider = OpenRouterVisionProvider;
|
||||
//# sourceMappingURL=openRouterVisionProvider.js.map
|
||||
Reference in New Issue
Block a user