pollo-mcp
Version:
Pollo AI Model Context Protocol (MCP) Server for video generation
123 lines (122 loc) • 4.91 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import axios from 'axios';
import { env } from './env.js';
export function getGenerationModels() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield axios
.get(`${env.POLLO_AI_BASE_URL}/api/platform/mcp_config.json`)
.then((res) => res.data);
return response;
});
}
export function getTaskStatus(taskId) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield callOpenAPI('GET', `/api/platform/generation/${taskId}/status`);
return response;
});
}
export function generateVideo(modelBrand, modelAlias, data) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield callOpenAPI('POST', `/api/platform/generation/${modelBrand}/${modelAlias}`, {
input: Object.assign(Object.assign({}, data), { entryCode: 'api-platform' }),
});
return response;
});
}
export function signImage(filename) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield callOpenAPI('POST', '/api/platform/file/sign', {
filename,
type: 'image',
action: 'put',
});
return response;
});
}
function callOpenAPI(method, path, body) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
// Execute the request
const response = yield axios.request({
method,
url: `${env.POLLO_AI_BASE_URL}${path}`,
headers: {
'Content-Type': 'application/json',
'x-api-key': env.POLLO_AI_API_KEY,
},
data: body,
});
// Process and format the response
const contentType = ((_a = response.headers['content-type']) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
// Handle JSON responses
if (contentType.includes('application/json') &&
typeof response.data === 'object' &&
response.data !== null) {
const result = response.data;
if (!result.data) {
throw new Error(result.message);
}
return result.data;
}
throw new Error(`Unexpected response format: ${contentType}`);
}
catch (error) {
// Handle errors during execution
let errorMessage;
// Format Axios errors specially
if (axios.isAxiosError(error)) {
errorMessage = formatAxiosError(error);
}
// Handle standard errors
else if (error instanceof Error) {
errorMessage = error.message;
}
// Handle unexpected error types
else {
errorMessage = 'Unexpected error: ' + String(error);
}
throw new Error(errorMessage);
}
});
}
function formatAxiosError(error) {
let message = 'API request failed.';
if (error.response) {
message = `API Error: Status ${error.response.status} (${error.response.statusText || 'Status text not available'}). `;
const responseData = error.response.data;
const MAX_LEN = 200;
if (typeof responseData === 'string') {
message += `Response: ${responseData.substring(0, MAX_LEN)}${responseData.length > MAX_LEN ? '...' : ''}`;
}
else if (responseData) {
try {
const jsonString = JSON.stringify(responseData);
message += `Response: ${jsonString.substring(0, MAX_LEN)}${jsonString.length > MAX_LEN ? '...' : ''}`;
}
catch (_a) {
message += 'Response: [Could not serialize data]';
}
}
else {
message += 'No response body received.';
}
}
else if (error.request) {
message = 'API Network Error: No response received from server.';
if (error.code)
message += ` (Code: ${error.code})`;
}
else {
message += `API Request Setup Error: ${error.message}`;
}
return message;
}