feat: compatible with json strings with curly braces {
This commit is contained in:
@@ -39,6 +39,7 @@ import { msAuth } from "../libs/auth";
|
|||||||
import { genDeeplFree } from "./deepl";
|
import { genDeeplFree } from "./deepl";
|
||||||
import { genBaidu } from "./baidu";
|
import { genBaidu } from "./baidu";
|
||||||
import interpreter from "../libs/interpreter";
|
import interpreter from "../libs/interpreter";
|
||||||
|
import { parseJsonObj } from "../libs/utils";
|
||||||
|
|
||||||
const keyMap = new Map();
|
const keyMap = new Map();
|
||||||
const urlMap = new Map();
|
const urlMap = new Map();
|
||||||
@@ -256,8 +257,9 @@ const genOpenAI = ({
|
|||||||
.replaceAll(INPUT_PLACE_TO, to)
|
.replaceAll(INPUT_PLACE_TO, to)
|
||||||
.replaceAll(INPUT_PLACE_TEXT, text);
|
.replaceAll(INPUT_PLACE_TEXT, text);
|
||||||
|
|
||||||
customHeader = JSON.parse("{" + customHeader + "}");
|
// TODO: 同时支持json对象和hook函数
|
||||||
customBody = JSON.parse("{" + customBody + "}");
|
customHeader = parseJsonObj(customHeader);
|
||||||
|
customBody = parseJsonObj(customBody);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
model,
|
model,
|
||||||
@@ -316,8 +318,8 @@ const genGemini = ({
|
|||||||
.replaceAll(INPUT_PLACE_TO, to)
|
.replaceAll(INPUT_PLACE_TO, to)
|
||||||
.replaceAll(INPUT_PLACE_TEXT, text);
|
.replaceAll(INPUT_PLACE_TEXT, text);
|
||||||
|
|
||||||
customHeader = JSON.parse("{" + customHeader + "}");
|
customHeader = parseJsonObj(customHeader);
|
||||||
customBody = JSON.parse("{" + customBody + "}");
|
customBody = parseJsonObj(customBody);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
system_instruction: {
|
system_instruction: {
|
||||||
@@ -375,8 +377,8 @@ const genGemini2 = ({
|
|||||||
.replaceAll(INPUT_PLACE_TO, to)
|
.replaceAll(INPUT_PLACE_TO, to)
|
||||||
.replaceAll(INPUT_PLACE_TEXT, text);
|
.replaceAll(INPUT_PLACE_TEXT, text);
|
||||||
|
|
||||||
customHeader = JSON.parse("{" + customHeader + "}");
|
customHeader = parseJsonObj(customHeader);
|
||||||
customBody = JSON.parse("{" + customBody + "}");
|
customBody = parseJsonObj(customBody);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
model,
|
model,
|
||||||
@@ -431,8 +433,8 @@ const genClaude = ({
|
|||||||
.replaceAll(INPUT_PLACE_TO, to)
|
.replaceAll(INPUT_PLACE_TO, to)
|
||||||
.replaceAll(INPUT_PLACE_TEXT, text);
|
.replaceAll(INPUT_PLACE_TEXT, text);
|
||||||
|
|
||||||
customHeader = JSON.parse("{" + customHeader + "}");
|
customHeader = parseJsonObj(customHeader);
|
||||||
customBody = JSON.parse("{" + customBody + "}");
|
customBody = parseJsonObj(customBody);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
model,
|
model,
|
||||||
@@ -485,8 +487,8 @@ const genOllama = ({
|
|||||||
.replaceAll(INPUT_PLACE_TO, to)
|
.replaceAll(INPUT_PLACE_TO, to)
|
||||||
.replaceAll(INPUT_PLACE_TEXT, text);
|
.replaceAll(INPUT_PLACE_TEXT, text);
|
||||||
|
|
||||||
customHeader = JSON.parse("{" + customHeader + "}");
|
customHeader = parseJsonObj(customHeader);
|
||||||
customBody = JSON.parse("{" + customBody + "}");
|
customBody = parseJsonObj(customBody);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
model,
|
model,
|
||||||
|
|||||||
@@ -267,3 +267,25 @@ export const getHtmlText = (htmlStr, skipTag = "") => {
|
|||||||
|
|
||||||
return doc.body.innerText.trim();
|
return doc.body.innerText.trim();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析JSON字符串对象
|
||||||
|
* @param {*} str
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const parseJsonObj = (str) => {
|
||||||
|
if (!str || type(str) !== "string") {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (str.trim()[0] !== "{") {
|
||||||
|
str = `{${str}}`;
|
||||||
|
}
|
||||||
|
return JSON.parse(str);
|
||||||
|
} catch (err) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user