Uname: Linux premium294.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
Software: LiteSpeed
PHP version: 8.1.32 [ PHP INFO ] PHP os: Linux
Server Ip: 104.21.32.1
Your Ip: 216.73.216.223
User: mjbynoyq (1574) | Group: mjbynoyq (1570)
Safe Mode: OFF
Disable Function:
NONE

name : index.ts
import { Str } from "@brizy/readers";
import { franc } from "franc";
import { get } from "lodash";
import { request } from "../api";
import { getConfig } from "../config";
import { Action, RequestOptions } from "../types/AiText";
import { t } from "../utils/i18n";
import { createConversation } from "./utils";

export const doAiRequest = async (
  res: (value: string) => void,
  rej: (error: string) => void,
  data: { prompt: string; action?: Action }
): Promise<void> => {
  const config = getConfig();

  if (!config) {
    throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
  }

  if (typeof config.api.openAIUrl === "undefined") {
    throw new Error(t("Missing OpenAiUrl"));
  }

  try {
    const { prompt, action } = data;

    const conversation = createConversation({
      action,
      prompt,
      language: franc(data.prompt)
    });

    const r = await request(config.api.openAIUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        model: RequestOptions.Model,
        ...conversation
      })
    });

    const json = await r.json();

    const content = Str.read(get(json, ["choices", "0", "message", "content"]));

    if (content) {
      res(content);
    } else {
      rej(t("Something went wrong, please try again"));
    }
  } catch (e) {
    rej(t("Something went wrong, please try again"));
  }
};
© 2025 XylotrechusZ