feat: add custom api examples

This commit is contained in:
Gabe
2025-10-16 23:51:49 +08:00
parent 74ce6f2f1f
commit 32c6d45cb0
7 changed files with 135 additions and 14 deletions

View File

@@ -98,8 +98,9 @@ const parseAIRes = (raw) => {
try {
const jsonString = extractJson(raw);
const data = JSON.parse(jsonString);
if (!jsonString) return [];
const data = JSON.parse(jsonString);
if (Array.isArray(data.translations)) {
// todo: 考虑序号id可能会打乱
return data.translations.map((item) => [
@@ -925,7 +926,7 @@ export const handleTranslate = async (
userMsg,
...apiSetting,
});
if (!Array.isArray(result)) {
if (!result?.length) {
throw new Error("tranlate got an unexpected result");
}

View File

@@ -409,16 +409,16 @@ Good morning.
\`\`\``;
const defaultRequestHook = `async (args, { url, body, headers, userMsg, method } = {}) => {
console.log("request hook args:", args);
console.log("request hook args:", { args, url, body, headers, userMsg, method });
// return { url, body, headers, userMsg, method };
}`;
};`;
const defaultResponseHook = `async ({ res, ...args }) => {
console.log("reaponse hook args:", res, args);
console.log("reaponse hook args:", { res, args });
// const translations = [["你好", "zh"]];
// const modelMsg = "";
// return { translations, modelMsg };
}`;
};`;
// 翻译接口默认参数
const defaultApi = {

View File

@@ -137,46 +137,42 @@ ${customApiLangs}
`;
const requestHookHelperZH = `1、第一个参数包含如下字段'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ...
2、返回值必须是包含以下字段的对象 'url', 'body', 'headers', 'userMsg', 'method'
2、返回值必须是包含以下字段的对象 'url', 'body', 'headers', 'method'
3、如返回空值则hook函数不会产生任何效果。
// 示例
async (args, { url, body, headers, userMsg, method } = {}) => {
console.log("request hook args:", args);
return { url, body, headers, userMsg, method };
}`;
const requestHookHelperEN = `1. The first parameter contains the following fields: 'texts', 'from', 'to', 'url', 'key', 'model', 'systemPrompt', ...
2. The return value must be an object containing the following fields: 'url', 'body', 'headers', 'userMsg', 'method'
2. The return value must be an object containing the following fields: 'url', 'body', 'headers', 'method'
3. If a null value is returned, the hook function will have no effect.
// Example
async (args, { url, body, headers, userMsg, method } = {}) => {
console.log("request hook args:", args);
return { url, body, headers, userMsg, method };
}`;
const responsetHookHelperZH = `1、第一个参数包含如下字段'res', ...
2、返回值必须是包含以下字段的对象 'translations', 'modelMsg'
2、返回值必须是包含以下字段的对象 'translations'
'translations' 应为一个二维数组:[[译文, 源语言]]
3、如返回空值则hook函数不会产生任何效果。
// 示例
async ({ res, ...args }) => {
console.log("reaponse hook args:", res, args);
const translations = [["你好", "zh"]];
const modelMsg = "";
return { translations, modelMsg };
}`;
const responsetHookHelperEN = `1. The first parameter contains the following fields: 'res', ...
2. The return value must be an object containing the following fields: 'translations', 'modelMsg'
2. The return value must be an object containing the following fields: 'translations'
('translations' should be a two-dimensional array: [[translation, source language]]).
3. If a null value is returned, the hook function will have no effect.
// Example
async ({ res, ...args }) => {
console.log("reaponse hook args:", res, args);
const translations = [["你好", "zh"]];
const modelMsg = "";
return { translations, modelMsg };