- feat(form): Support API Key ⇄ JSON two-way binding in edit modal

- feat(utils): Add helpers to read/write/detect API Key in config
- refactor(form): Reuse unified linking logic for preset and edit flows
- chore: Preserve website URL extraction and signature-disable behaviors
- build: Verify renderer build locally
This commit is contained in:
Jason
2025-08-26 10:37:44 +08:00
parent 616e230218
commit 001664c67d
2 changed files with 81 additions and 22 deletions

View File

@@ -44,4 +44,48 @@ export const extractWebsiteUrl = (jsonString: string): string => {
// 忽略JSON解析错误
}
return ''
}
}
// 读取配置中的 API Keyenv.ANTHROPIC_AUTH_TOKEN
export const getApiKeyFromConfig = (jsonString: string): string => {
try {
const config = JSON.parse(jsonString)
const key = config?.env?.ANTHROPIC_AUTH_TOKEN
return typeof key === 'string' ? key : ''
} catch (err) {
return ''
}
}
// 判断配置中是否存在 API Key 字段
export const hasApiKeyField = (jsonString: string): boolean => {
try {
const config = JSON.parse(jsonString)
return Object.prototype.hasOwnProperty.call(config?.env ?? {}, 'ANTHROPIC_AUTH_TOKEN')
} catch (err) {
return false
}
}
// 写入/更新配置中的 API Key默认不新增缺失字段
export const setApiKeyInConfig = (
jsonString: string,
apiKey: string,
options: { createIfMissing?: boolean } = {}
): string => {
const { createIfMissing = false } = options
try {
const config = JSON.parse(jsonString)
if (!config.env) {
if (!createIfMissing) return jsonString
config.env = {}
}
if (!('ANTHROPIC_AUTH_TOKEN' in config.env) && !createIfMissing) {
return jsonString
}
config.env.ANTHROPIC_AUTH_TOKEN = apiKey
return JSON.stringify(config, null, 2)
} catch (err) {
return jsonString
}
}