"use strict";

/**
 * 仅调用 OpenAI 兼容 Chat API 生成文案（撰写文案按钮）
 * 由 `/api/v1/nodejs-scripts?name=script_generate.js` 下发
 */

const pipelineNative = require("./pipeline_native.js");
const desktopConfig = require("./desktop_config.js");

globalThis.__nodejsMain = async function (params) {
  const p = params || {};
  const modelConfig = desktopConfig.buildModelConfig();
  const check = desktopConfig.validateModelConfig(modelConfig);
  if (!check.ok) {
    return { success: false, error: check.error };
  }

  const messages = p.messages;
  if (!Array.isArray(messages) || messages.length === 0) {
    return { success: false, error: "缺少 messages" };
  }

  return pipelineNative.openaiChat({
    apiUrl: modelConfig.apiUrl,
    apiKey: modelConfig.apiKey,
    model: modelConfig.apiModelId,
    messages,
    temperature: typeof p.temperature === "number" ? p.temperature : 0.8,
    max_tokens: p.max_tokens || p.maxTokens || 2000,
  });
};
