imagees del agente generadas y subidas con proxy server
This commit is contained in:
@@ -5,6 +5,7 @@ import { withAuth, getSessionCredentials } from "../../auth/index.js";
|
||||
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
|
||||
import { withAuthParams } from "../helpers/authSchema.js";
|
||||
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
|
||||
import { pythonPost } from "../helpers/pythonServerClient.js";
|
||||
|
||||
/**
|
||||
* Helper: POST to mcp_respond.php via viewer_functions.php
|
||||
@@ -117,36 +118,44 @@ export function registerUploadRecordImageTool(server) {
|
||||
);
|
||||
if (validationError) return validationError;
|
||||
|
||||
const credentials = await getSessionCredentials(extra.sessionId);
|
||||
const projectSlug = path.basename(process.env.ACAI_PROJECT_DIR || "");
|
||||
|
||||
// Si la URL es local, descargar y enviar base64 (el servidor remoto no
|
||||
// puede descargarla en modo produccion).
|
||||
const localFile = await resolveLocalImageAsBase64(imageUrl);
|
||||
const uploadPayload = localFile
|
||||
? { tableName, recordId, fieldName, alt, fileBase64: localFile.fileBase64, fileName: localFile.fileName }
|
||||
: { tableName, recordId, fieldName, imageUrl, alt };
|
||||
// Intentar via Python server (tiene sync + optimizacion)
|
||||
let result;
|
||||
try {
|
||||
result = await pythonPost("/api/cms/upload-to-field", {
|
||||
project: projectSlug,
|
||||
table: tableName,
|
||||
num: recordId,
|
||||
field: fieldName,
|
||||
imageUrl: imageUrl,
|
||||
});
|
||||
} catch (pyErr) {
|
||||
// Fallback: si Python no es accesible, usar el flujo directo al PHP
|
||||
console.error(`[upload_record_image] Python server failed (${pyErr.message}), falling back to PHP direct`);
|
||||
const credentials = await getSessionCredentials(extra.sessionId);
|
||||
const localFile = await resolveLocalImageAsBase64(imageUrl);
|
||||
const uploadPayload = localFile
|
||||
? { tableName, recordId, fieldName, alt, fileBase64: localFile.fileBase64, fileName: localFile.fileName }
|
||||
: { tableName, recordId, fieldName, imageUrl, alt };
|
||||
const response = await mcpPost(credentials, "uploadRecordImage", uploadPayload, credentials.token, credentials.tokenHash);
|
||||
const apiError = handleApiResponse(response.data, 'upload_record_image');
|
||||
if (apiError) return apiError;
|
||||
result = { success: true, data: response.data };
|
||||
}
|
||||
|
||||
const response = await mcpPost(
|
||||
credentials,
|
||||
"uploadRecordImage",
|
||||
uploadPayload,
|
||||
credentials.token,
|
||||
credentials.tokenHash
|
||||
);
|
||||
|
||||
const apiError = handleApiResponse(response.data, 'upload_record_image');
|
||||
if (apiError) return apiError;
|
||||
if (!result?.success && !result?.data?.success) {
|
||||
const errMsg = result?.error || result?.data?.error || "Unknown error";
|
||||
return { content: [{ type: "text", text: JSON.stringify({ error: errMsg }, null, 2) }], isError: true };
|
||||
}
|
||||
|
||||
const uploadData = result.data || result;
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
message: "Image uploaded successfully",
|
||||
tableName,
|
||||
recordId,
|
||||
fieldName,
|
||||
...response.data
|
||||
...uploadData,
|
||||
}, null, 2)
|
||||
}],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user