feat: support kat-coder & template value (#77)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
// 供应商配置处理工具函数
|
||||
|
||||
import type { TemplateValueConfig } from "../config/providerPresets";
|
||||
|
||||
const isPlainObject = (value: unknown): value is Record<string, any> => {
|
||||
return Object.prototype.toString.call(value) === "[object Object]";
|
||||
};
|
||||
@@ -173,6 +175,51 @@ export const getApiKeyFromConfig = (jsonString: string): string => {
|
||||
}
|
||||
};
|
||||
|
||||
// 模板变量替换
|
||||
export const applyTemplateValues = (
|
||||
config: any,
|
||||
templateValues: Record<string, TemplateValueConfig> | undefined
|
||||
): any => {
|
||||
const resolvedValues = Object.fromEntries(
|
||||
Object.entries(templateValues ?? {}).map(([key, value]) => {
|
||||
const resolvedValue =
|
||||
value.editorValue !== undefined
|
||||
? value.editorValue
|
||||
: value.defaultValue ?? "";
|
||||
return [key, resolvedValue];
|
||||
})
|
||||
);
|
||||
|
||||
const replaceInString = (str: string): string => {
|
||||
return Object.entries(resolvedValues).reduce((acc, [key, value]) => {
|
||||
const placeholder = `\${${key}}`;
|
||||
if (!acc.includes(placeholder)) {
|
||||
return acc;
|
||||
}
|
||||
return acc.split(placeholder).join(value ?? "");
|
||||
}, str);
|
||||
};
|
||||
|
||||
const traverse = (obj: any): any => {
|
||||
if (typeof obj === "string") {
|
||||
return replaceInString(obj);
|
||||
}
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(traverse);
|
||||
}
|
||||
if (obj && typeof obj === "object") {
|
||||
const result: any = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
result[key] = traverse(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
return traverse(config);
|
||||
};
|
||||
|
||||
// 判断配置中是否存在 API Key 字段
|
||||
export const hasApiKeyField = (jsonString: string): boolean => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user