Files
2026-11-02 22:54:14 +08:00

2393 lines
126 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ==================== 配置 Tab 组件 (拆分版) ====================
window.ConfigTab = {
name: 'ConfigTab',
props: {
currentDomain: { type: String, default: null },
currentConfig: { type: Object, default: null }
},
emits: [
'add-selector', 'remove-selector', 'update-selector-key', 'test-selector',
'add-step', 'remove-step', 'move-step', 'action-change', 'show-templates',
'update-image-config', 'reload-config'
],
// 注册子组件(确保模板可解析)
components: {
'selector-panel': window.SelectorPanel,
'image-config-panel': window.ImageConfigPanel,
'stream-config-panel': window.StreamConfigPanel,
'workflow-panel': window.WorkflowPanel,
'file-paste-panel': window.FilePastePanel,
'prompt-padding-panel': window.PromptPaddingPanel,
'preset-transfer': window.PresetTransfer,
'adapter-panel': window.AdapterPanel
},
data() {
return {
// 🆕 预设管理
selectedPreset: '主预设',
defaultPreset: '主预设',
availablePresets: [],
presetLoading: false,
newPresetName: '',
showNewPresetInput: false,
renamePresetName: '',
showRenamePresetInput: false,
pendingPresetTransfer: 0,
// 当前配置分类
activeWorkspaceSection: 'selectors',
// 各配置面板折叠状态(默认展开)
collapsedPanels: {
selectors: false,
media: false,
response: false,
filePaste: false,
promptPadding: false
},
presetSectionDrafts: {
file_paste: [],
prompt_padding: []
},
advancedConfigSaving: false,
isolatedTabCreating: false,
sharedTabCreating: false,
showConfigCompareDialog: false,
compareMainLoading: false,
compareMainError: '',
compareMainPresetName: '',
compareMainMatchMode: '',
compareMainPath: '',
compareMainSource: {},
compareLocalOriginalText: '',
compareLocalDraft: '',
compareMainOriginalText: '',
compareMainDraft: '',
compareSavingLocal: false,
advancedConfigSaveSeq: 0,
advancedConfigSaveQueue: null,
streamConfigSaveSeq: 0,
streamConfigSaveQueue: null,
// 默认配置
defaultImageConfig: {
enabled: false,
modalities: {
image: {
enabled: false,
run_policy: 'disabled',
quick_probe_timeout_seconds: 1.0,
late_wait_timeout_seconds: 45.0,
blind_wait_timeout_seconds: 1.0
},
audio: {
enabled: false,
run_policy: 'disabled',
quick_probe_timeout_seconds: 1.0,
capture_timeout_seconds: 12.0
},
video: {
enabled: false,
run_policy: 'disabled',
quick_probe_timeout_seconds: 1.0,
late_wait_timeout_seconds: 90.0
}
},
selector: 'img',
audio_selector: 'audio, audio source',
video_selector: 'video, video source',
container_selector: null,
debounce_seconds: 2.0,
wait_for_load: true,
load_timeout_seconds: 5.0,
download_blobs: true,
audio_capture_enabled: true,
src_allow_patterns: [],
max_size_mb: 10,
canvas_export_mime: 'image/jpeg',
canvas_export_quality: 0.88,
audio_network_capture: {
enabled: false,
timeout_seconds: 2.5,
transport: 'page_websocket_probe',
url_patterns: ['voicegenie', 'speech', 'audio', 'tts'],
extractor: 'voicegenie_ogg_pages',
settle_seconds: 0.35,
max_payload_bytes: 10 * 1024 * 1024
},
mode: 'all'
},
defaultStreamConfig: {
mode: 'dom',
request_transport: {
mode: 'workflow',
profile: '',
options: {}
},
send_confirmation: {
attachment_sensitivity: 'medium',
post_click_observe_window: 1.8,
pre_retry_probe_window: 0.12,
retry_observe_window: 0.9,
attachment_observe_window: 6.0,
max_retry_count: 2,
retry_interval: 0.6,
retry_cooldown_window: 1.5,
retry_action: 'click_send_btn',
retry_key_combo: 'Enter',
retry_on_unconfirmed_send: true,
accept_attachment_change: false,
accept_attachment_disappear: false,
accept_probe_confirmation: true,
retry_block_on_stop_button: true,
retry_block_if_generating: true,
trust_network_activity: true,
trust_generating_indicator: true,
trust_send_disabled_with_input_shrink: true
},
hard_timeout: 300,
network: null
}
};
},
computed: {
workspaceSections() {
return [
{ id: 'selectors', title: '元素选择器', subtitle: '定位关键网页元素', heading: '定义网页的关键位置', description: '先配置输入框、发送按钮和回复容器,再按需补充辅助元素。' },
{ id: 'input', title: '输入处理', subtitle: '附件与长文本', heading: '你要给 AI 发送什么?', description: '普通文字无需额外设置。发文件、发长文时,再开启相应能力。' },
{ id: 'response', title: '响应解析', subtitle: '提取与流式输出', heading: '把网页回复转换为 API 响应', description: '配置响应提取与流式输出策略,匹配当前站点的生成方式。' },
{ id: 'media', title: '媒体提取', subtitle: '图片、音频与视频', heading: '你想收到什么内容?', description: '接收 AI 回复里的媒体内容。上传文件请到「输入处理」。' },
{ id: 'workflow', title: '请求工作流', subtitle: '编排自动化步骤', heading: '串起一次完整的请求', description: '编排输入、发送、等待与提取动作,为当前预设定义执行顺序。' },
{ id: 'advanced', title: '高级功能', subtitle: '会话隔离与自愈', heading: '遇到问题,再按需开启', description: '登录互相影响、输入不稳定、消息没发出?只处理你正在遇到的那一项。' },
{ id: 'maintenance', title: '维护与巡检', subtitle: '健康检查与更新', heading: '站点适配器维护', description: '检查选择器是否还能命中、写法是否稳健,并同步官方最新的站点配置。' },
];
},
activeWorkspaceMeta() { return this.workspaceSections.find(s => s.id === this.activeWorkspaceSection) || this.workspaceSections[0]; },
// 🆕 当前预设的配置数据
presetConfig() {
if (!this.currentConfig) return null;
const presets = this.currentConfig.presets;
if (!presets) return this.currentConfig; // 兼容旧格式
return presets[this.selectedPreset]
|| presets[this.defaultPreset]
|| presets['主预设']
|| Object.values(presets)[0]
|| null;
},
imageConfig() {
if (!this.presetConfig) return this.defaultImageConfig;
const current = this.presetConfig.image_extraction || {};
const currentModalities = (current && current.modalities) || {};
const mergeModality = (type) => {
const defaults = ((this.defaultImageConfig.modalities || {})[type]) || {};
const value = currentModalities[type];
const enabledRunPolicy = type === 'audio' ? 'probe_if_trigger_found' : 'on_signal';
if (value && typeof value === 'object' && !Array.isArray(value)) {
const enabled = !!value.enabled;
const hasRunPolicy = Object.prototype.hasOwnProperty.call(value, 'run_policy') && !!value.run_policy;
return {
...defaults,
...value,
enabled,
run_policy: hasRunPolicy ? value.run_policy : (enabled ? enabledRunPolicy : 'disabled')
};
}
return {
...defaults,
enabled: !!value,
run_policy: value ? enabledRunPolicy : 'disabled'
};
};
return {
...this.defaultImageConfig,
...current,
modalities: {
image: mergeModality('image'),
audio: mergeModality('audio'),
video: mergeModality('video')
},
audio_network_capture: {
...(this.defaultImageConfig.audio_network_capture || {}),
...((current && current.audio_network_capture) || {})
}
};
},
streamConfig() {
if (!this.presetConfig) return this.defaultStreamConfig;
const streamConfig = this.presetConfig.stream_config || {};
return {
...this.defaultStreamConfig,
...streamConfig,
mode: streamConfig.mode || this.defaultStreamConfig.mode,
request_transport: {
...(this.defaultStreamConfig.request_transport || {}),
...((streamConfig && streamConfig.request_transport) || {}),
options: {
...((this.defaultStreamConfig.request_transport && this.defaultStreamConfig.request_transport.options) || {}),
...((streamConfig && streamConfig.request_transport && streamConfig.request_transport.options) || {})
}
},
send_confirmation: {
...(this.defaultStreamConfig.send_confirmation || {}),
// 修复:send_confirmation 的权威存储已迁移到 file_paste(后端 engine.py 会把
// stream_config 里的同名键剥离并 merge 进 file_paste),此处只从 stream_config
// 读会恒为默认值,并在用户改动时把默认值整份写回、覆盖真实配置。
...((streamConfig && streamConfig.send_confirmation) || {}),
...(((this.presetConfig && this.presetConfig.file_paste) || {}).send_confirmation || {})
},
hard_timeout: streamConfig.hard_timeout || this.defaultStreamConfig.hard_timeout,
network: streamConfig.network || this.defaultStreamConfig.network
};
},
filePasteConfigRef() {
return this.getPresetMutableSection('file_paste');
},
promptPaddingConfigRef() {
return this.getPresetMutableSection('prompt_padding');
},
siteAdvancedConfig() {
if (!this.currentConfig) {
return {
independent_cookies: false,
independent_cookies_auto_takeover: false,
input_box_stability_wait_enabled: false,
input_box_stability_wait_after_new_chat_only: true,
input_box_stability_wait_timeout: 1.5,
url_transition_wait_on_new_chat: false,
url_transition_wait_patterns: [],
send_confirmation_check_enabled: false,
send_confirmation_check_timeout: 1.5,
skip_new_chat_on_retry: false
};
}
const siteAdvanced = (this.currentConfig.advanced && typeof this.currentConfig.advanced === 'object')
? this.currentConfig.advanced
: {};
const presetAdvanced = (this.presetConfig && this.presetConfig.advanced && typeof this.presetConfig.advanced === 'object')
? this.presetConfig.advanced
: {};
const timingAdvanced = {
input_box_stability_wait_enabled: false,
input_box_stability_wait_after_new_chat_only: true,
input_box_stability_wait_timeout: 1.5,
url_transition_wait_on_new_chat: false,
url_transition_wait_patterns: [],
send_confirmation_check_enabled: false,
send_confirmation_check_timeout: 1.5,
skip_new_chat_on_retry: false,
...siteAdvanced,
...presetAdvanced
};
return {
independent_cookies: !!siteAdvanced.independent_cookies,
independent_cookies_auto_takeover: !!siteAdvanced.independent_cookies_auto_takeover,
input_box_stability_wait_enabled: !!timingAdvanced.input_box_stability_wait_enabled,
input_box_stability_wait_after_new_chat_only: !!timingAdvanced.input_box_stability_wait_after_new_chat_only,
input_box_stability_wait_timeout: this.sanitizeInputStabilityWaitTimeout(
timingAdvanced.input_box_stability_wait_timeout
),
url_transition_wait_on_new_chat: !!timingAdvanced.url_transition_wait_on_new_chat,
url_transition_wait_patterns: this.sanitizeUrlTransitionWaitPatterns(
timingAdvanced.url_transition_wait_patterns
),
send_confirmation_check_enabled: !!timingAdvanced.send_confirmation_check_enabled,
send_confirmation_check_timeout: this.sanitizeSendConfirmationCheckTimeout(
timingAdvanced.send_confirmation_check_timeout
),
skip_new_chat_on_retry: !!timingAdvanced.skip_new_chat_on_retry
};
},
compareLocalParsed() {
return this.parseConfigCompareDraft(this.compareLocalDraft);
},
compareMainParsed() {
return this.parseConfigCompareDraft(this.compareMainDraft);
},
compareLocalDirty() {
return this.compareLocalDraft !== this.compareLocalOriginalText;
},
compareMainDirty() {
return this.compareMainDraft !== this.compareMainOriginalText;
},
compareLocalSummaryItems() {
if (!this.compareLocalParsed.valid) return [];
return this.buildConfigCompareSummaryItems(this.compareLocalParsed.value);
},
compareMainSummaryItems() {
if (!this.compareMainParsed.valid) return [];
return this.buildConfigCompareSummaryItems(this.compareMainParsed.value);
},
compareFieldDiffs() {
if (!this.compareLocalParsed.valid || !this.compareMainParsed.valid) return [];
return this.buildConfigCompareFieldDiffs(
this.compareLocalParsed.value,
this.compareMainParsed.value
);
},
compareDifferentCount() {
return this.compareFieldDiffs.filter(item => item.status !== 'same').length;
},
compareSameCount() {
return this.compareFieldDiffs.filter(item => item.status === 'same').length;
}
},
methods: {
stagePresetOperation(kind, name) {
if (!this.pendingPresetTransfer) return false;
const presets = this.currentConfig.presets, old = this.selectedPreset;
if (['create','rename'].includes(kind) && (!window.PresetTransferSupport.safeName(name) || Object.hasOwn(presets,name))) {
alert('请输入有效且未使用的预设名称。'); return true;
}
this.flushMutableSectionDrafts();
if (kind === 'create') { presets[name] = JSON.parse(JSON.stringify(presets[old])); this.selectedPreset = name; }
if (kind === 'rename') { presets[name] = presets[old]; delete presets[old]; this.selectedPreset = name; }
if (kind === 'delete') { delete presets[old]; this.selectedPreset = Object.keys(presets)[0]; }
if (kind === 'default' || (['rename','delete'].includes(kind) && this.defaultPreset === old)) {
this.defaultPreset = this.selectedPreset; this.currentConfig.default_preset = this.selectedPreset;
}
this.pendingPresetTransfer++;
this.availablePresets = Object.keys(presets); this.ensurePresetMutableSections();
this.showNewPresetInput = false; this.showRenamePresetInput = false;
this.newPresetName = ''; this.renamePresetName = '';
alert('预设草稿已更新,请点击「保存配置」完成保存。');
return true;
},
beginPresetRename() {
this.showRenamePresetInput = true; this.renamePresetName = this.selectedPreset;
this.showNewPresetInput = false; this.newPresetName = '';
this.$nextTick(() => { this.$refs.renamePresetInput?.focus(); this.$refs.renamePresetInput?.select(); });
},
applyPresetTransfer(payload) {
if (payload.domain !== this.currentDomain) return;
this.flushMutableSectionDrafts();
this.pendingPresetTransfer++;
if (!this.currentConfig.presets) {
const legacy = JSON.parse(JSON.stringify(this.currentConfig));
Object.keys(this.currentConfig).forEach(k => delete this.currentConfig[k]);
this.currentConfig.presets = { [this.selectedPreset || '主预设']: legacy };
this.currentConfig.default_preset = this.selectedPreset || '主预设';
}
Object.entries(payload.presets).forEach(([name, config]) => {
this.currentConfig.presets[name] = config;
});
if (payload.settings) Object.entries(payload.settings).forEach(([key,value]) => { this.currentConfig[key] = value; });
this.availablePresets = Object.keys(this.currentConfig.presets);
if (payload.select) this.selectedPreset = payload.select;
this.ensurePresetMutableSections();
},
// R2-8:维护面板使用的请求函数(带鉴权;检查全部站点更新可能较慢,超时放宽到 30 秒)
adapterRequest(url, options = {}) {
return this.fetchJson(url, { ...options, headers: this.buildAuthHeaders(options.headers || {}) }, { timeoutMs: 30000 });
},
onAdapterUpdated() {
this.$emit('reload-config');
},
buildAuthHeaders(extraHeaders = {}) {
const token = String(window.getDashboardAuthToken ? window.getDashboardAuthToken() : '').trim();
const headers = { ...extraHeaders };
if (token) {
headers['Authorization'] = 'Bearer ' + token;
}
return headers;
},
async fetchJson(url, options = {}, requestOptions = {}) {
const timeoutMs = Number(requestOptions.timeoutMs || 10000);
const fetchOptions = { ...options };
let timeoutId = null;
let controller = null;
if (timeoutMs > 0 && typeof AbortController !== 'undefined') {
controller = new AbortController();
fetchOptions.signal = controller.signal;
timeoutId = setTimeout(() => controller.abort(), timeoutMs);
}
try {
const response = await fetch(url, fetchOptions);
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.detail || ('HTTP ' + response.status));
}
return data;
} catch (error) {
if (error && error.name === 'AbortError') {
throw new Error('请求超时,请稍后重试');
}
throw error;
} finally {
if (timeoutId) {
clearTimeout(timeoutId);
}
}
},
isPlainConfigObject(value) {
return !!value && typeof value === 'object' && !Array.isArray(value);
},
canAssignObjectProperty(target, key) {
if (!this.isPlainConfigObject(target)) return false;
try {
const descriptor = Object.getOwnPropertyDescriptor(target, key);
if (!descriptor) {
return Object.isExtensible(target);
}
if (Object.prototype.hasOwnProperty.call(descriptor, 'writable')) {
return !!descriptor.writable;
}
return typeof descriptor.set === 'function';
} catch (_) {
return false;
}
},
cloneConfigSection(value) {
if (Array.isArray(value)) {
return value.map(item => this.cloneConfigSection(item));
}
if (this.isPlainConfigObject(value)) {
return Object.keys(value).reduce((result, key) => {
result[key] = this.cloneConfigSection(value[key]);
return result;
}, {});
}
return value;
},
getPresetSectionDraftSlots(sectionKey) {
let slots = this.presetSectionDrafts[sectionKey];
if (!Array.isArray(slots)) {
slots = slots && typeof slots === 'object' ? [slots] : [];
this.presetSectionDrafts[sectionKey] = slots;
}
return slots;
},
getPresetSectionDraft(sectionKey, preset, source) {
const slots = this.getPresetSectionDraftSlots(sectionKey);
let slot = slots.find(item => item && item.preset === preset);
if (!slot) {
slot = { preset, source: null, value: {} };
slots.push(slot);
}
if (slot.source !== source) {
slot.preset = preset;
slot.source = source;
slot.value = this.isPlainConfigObject(source) ? this.cloneConfigSection(source) : {};
}
return slot.value;
},
getPresetMutableSection(sectionKey) {
const pc = this.presetConfig;
if (!this.isPlainConfigObject(pc)) {
return this.getPresetSectionDraft(sectionKey, null, null);
}
return this.getPresetSectionDraft(sectionKey, pc, pc[sectionKey]);
},
ensurePresetMutableSections() {
this.getPresetMutableSection('file_paste');
this.getPresetMutableSection('prompt_padding');
},
isPresetSectionDraftDirty(source, draft) {
if (!this.isPlainConfigObject(source)) {
return this.isPlainConfigObject(draft) && Object.keys(draft).length > 0;
}
try {
return JSON.stringify(source) !== JSON.stringify(draft);
} catch (_) {
return true;
}
},
shouldFlushPresetSectionDraft(sectionKey, preset, source, draft) {
if (!this.isPlainConfigObject(preset) || !this.isPlainConfigObject(draft)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(preset, sectionKey)) {
return Object.keys(draft).length > 0;
}
return this.isPresetSectionDraftDirty(source, draft);
},
flushPresetSectionDraft(sectionKey, slot) {
const preset = slot && slot.preset;
if (!slot || !this.shouldFlushPresetSectionDraft(sectionKey, preset, slot.source, slot.value)) {
return false;
}
if (!this.canAssignObjectProperty(preset, sectionKey)) {
return false;
}
const nextValue = this.cloneConfigSection(slot.value);
preset[sectionKey] = nextValue;
slot.source = nextValue;
slot.value = this.cloneConfigSection(nextValue);
return true;
},
flushMutableSectionDrafts() {
let changed = false;
['file_paste', 'prompt_padding'].forEach(sectionKey => {
this.getPresetSectionDraftSlots(sectionKey).forEach(slot => {
changed = this.flushPresetSectionDraft(sectionKey, slot) || changed;
});
});
return changed;
},
notifyCompare(message, type = 'info') {
if (this.$parent && typeof this.$parent.notify === 'function') {
this.$parent.notify(message, type);
return;
}
if (type === 'error') {
alert(message);
} else {
console.info(message);
}
},
sortConfigCompareValue(value) {
if (Array.isArray(value)) {
return value.map(item => this.sortConfigCompareValue(item));
}
if (value && typeof value === 'object') {
const sorted = {};
Object.keys(value)
.sort((left, right) => String(left).localeCompare(String(right), 'zh-CN'))
.forEach(key => {
sorted[key] = this.sortConfigCompareValue(value[key]);
});
return sorted;
}
return value;
},
getConfigCompareStableText(value) {
return JSON.stringify(this.sortConfigCompareValue(value));
},
formatConfigCompareJson(value) {
const normalized = (value && typeof value === 'object' && !Array.isArray(value))
? value
: {};
return JSON.stringify(this.sortConfigCompareValue(normalized), null, 2);
},
validateConfigCompareObject(parsed) {
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('JSON 顶层必须是对象');
}
if (
parsed.presets !== undefined
|| parsed.default_preset !== undefined
) {
throw new Error('这里只接受单个预设配置对象,不要包含 presets/default_preset');
}
if (
parsed.selectors !== undefined
&& (
parsed.selectors === null
|| typeof parsed.selectors !== 'object'
|| Array.isArray(parsed.selectors)
)
) {
throw new Error('selectors 必须是对象');
}
if (parsed.workflow !== undefined && !Array.isArray(parsed.workflow)) {
throw new Error('workflow 必须是数组');
}
if (
parsed.advanced !== undefined
&& (
parsed.advanced === null
|| typeof parsed.advanced !== 'object'
|| Array.isArray(parsed.advanced)
)
) {
throw new Error('advanced 必须是对象');
}
},
parseConfigCompareDraft(rawText) {
const text = String(rawText || '').trim();
if (!text) {
return {
valid: false,
error: 'JSON 不能为空',
value: null
};
}
try {
const parsed = JSON.parse(text);
this.validateConfigCompareObject(parsed);
return {
valid: true,
error: '',
value: parsed
};
} catch (error) {
return {
valid: false,
error: error && error.message ? error.message : 'JSON 解析失败',
value: null
};
}
},
buildConfigCompareSummaryItems(config) {
const selectors = (config && config.selectors && typeof config.selectors === 'object' && !Array.isArray(config.selectors))
? Object.keys(config.selectors).length
: 0;
const workflowSteps = Array.isArray(config && config.workflow)
? config.workflow.length
: 0;
const rawStreamMode = String((config && config.stream_config && config.stream_config.mode) || 'dom').trim().toLowerCase();
const streamMode = rawStreamMode === 'network'
? '网络模式'
: (rawStreamMode === 'dom' ? 'DOM 模式' : (rawStreamMode || 'DOM 模式'));
const imageEnabled = !!(config && config.image_extraction && config.image_extraction.enabled);
const filePasteEnabled = !!(config && config.file_paste && config.file_paste.enabled);
const promptPaddingEnabled = !!(config && config.prompt_padding && config.prompt_padding.enabled);
return [
{ label: '选择器', value: String(selectors) },
{ label: '工作流', value: workflowSteps + ' 步' },
{ label: '流式', value: streamMode, compact: true },
{ label: '图片提取', value: imageEnabled ? '开启' : '关闭' },
{ label: '文件粘贴', value: filePasteEnabled ? '开启' : '关闭' },
{ label: '开头注入', value: promptPaddingEnabled ? '开启' : '关闭' }
];
},
buildConfigCompareFieldDiffs(localConfig, mainConfig) {
const labelMap = {
selectors: '选择器',
workflow: '工作流',
stream_config: '流式配置',
image_extraction: '图片提取',
file_paste: '文件粘贴',
prompt_padding: '开头注入',
advanced: '高级配置',
stealth: '低熵模式',
extractor_id: '提取器',
extractor_verified: '提取器验证'
};
const priorityKeys = [
'selectors',
'workflow',
'stream_config',
'image_extraction',
'file_paste',
'prompt_padding',
'advanced',
'stealth',
'extractor_id',
'extractor_verified'
];
const local = localConfig || {};
const main = mainConfig || {};
const restKeys = new Set([
...Object.keys(local),
...Object.keys(main)
]);
const orderedKeys = [];
priorityKeys.forEach(key => {
if (restKeys.has(key)) {
orderedKeys.push(key);
restKeys.delete(key);
}
});
Array.from(restKeys)
.sort((left, right) => String(left).localeCompare(String(right), 'zh-CN'))
.forEach(key => orderedKeys.push(key));
return orderedKeys.map(key => {
const hasLocal = Object.prototype.hasOwnProperty.call(local, key);
const hasMain = Object.prototype.hasOwnProperty.call(main, key);
let status = 'same';
if (hasLocal && hasMain) {
status = this.getConfigCompareStableText(local[key]) === this.getConfigCompareStableText(main[key])
? 'same'
: 'different';
} else if (hasLocal) {
status = 'local_only';
} else if (hasMain) {
status = 'main_only';
}
return {
key,
label: labelMap[key] || key,
status
};
});
},
getConfigCompareDiffClass(status) {
if (status === 'same') {
return 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-300';
}
if (status === 'different') {
return 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-800 dark:bg-amber-900/30 dark:text-amber-300';
}
if (status === 'main_only') {
return 'border-blue-200 bg-blue-50 text-blue-700 dark:border-blue-800 dark:bg-blue-900/30 dark:text-blue-300';
}
return 'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-800 dark:bg-rose-900/30 dark:text-rose-300';
},
getConfigCompareDiffText(status) {
if (status === 'same') return '一致';
if (status === 'different') return '内容不同';
if (status === 'main_only') return '仅 main 有';
return '仅本地有';
},
getConfigCompareMatchLabel(matchMode) {
if (matchMode === 'exact') return '已命中同名预设';
if (matchMode === 'default') return 'main 默认预设回退';
if (matchMode === 'main_preset') return 'main 主预设回退';
if (matchMode === 'first') return 'main 首个预设回退';
if (matchMode === 'legacy_flat') return 'main 旧版扁平配置';
return '';
},
syncConfigCompareLocalDraft(force = false) {
const nextText = this.formatConfigCompareJson(this.presetConfig || {});
this.compareLocalOriginalText = nextText;
if (force || !this.compareLocalDirty) {
this.compareLocalDraft = nextText;
}
},
resetConfigCompareState() {
this.showConfigCompareDialog = false;
this.compareMainLoading = false;
this.compareMainError = '';
this.compareMainPresetName = '';
this.compareMainMatchMode = '';
this.compareMainPath = '';
this.compareMainSource = {};
this.compareLocalOriginalText = '';
this.compareLocalDraft = '';
this.compareMainOriginalText = '';
this.compareMainDraft = '';
this.compareSavingLocal = false;
},
closeConfigCompareDialog() {
if (
(this.compareLocalDirty || this.compareMainDirty)
&& !window.confirm('关闭后未保存的比对草稿会丢失,确定关闭吗?')
) {
return;
}
this.resetConfigCompareState();
},
async openConfigCompare() {
if (!this.currentDomain || !this.currentConfig) return;
if (!this.presetConfig && this.availablePresets.length) {
this.selectedPreset = this.selectedPreset || this.defaultPreset || this.availablePresets[0] || '主预设';
}
this.showConfigCompareDialog = true;
this.compareMainError = '';
this.compareMainPresetName = '';
this.compareMainMatchMode = '';
this.compareMainPath = '';
this.compareMainSource = {};
this.compareMainOriginalText = '';
this.compareMainDraft = '';
this.syncConfigCompareLocalDraft(true);
this.$nextTick(() => {
const root = this.$el && this.$el.querySelector ? this.$el.querySelector('[data-config-compare-root]') : null;
if (root) {
root.style.display = 'flex';
}
});
await this.loadMainBranchCompareConfig();
},
async openConfigCompareForPreset(presetName) {
const targetPreset = String(presetName || '').trim();
if (targetPreset) {
if (!this.availablePresets.length) {
await this.loadPresets();
}
if (this.availablePresets.includes(targetPreset)) {
this.selectedPreset = targetPreset;
}
}
await this.openConfigCompare();
},
formatGitCompareErrorMessage(error) {
if (typeof window.formatGitCompareErrorText === 'function') {
return window.formatGitCompareErrorText(error);
}
const raw = String((error && error.message) || error || '').trim();
return raw || '读取官方配置失败';
},
formatOfficialConfigSourceTime(value) {
const raw = String(value || '').trim();
if (!raw) return '';
const timestamp = new Date(raw);
if (Number.isNaN(timestamp.getTime())) return raw;
return timestamp.toLocaleString('zh-CN', { hour12: false });
},
async loadMainBranchCompareConfig() {
if (!this.currentDomain) return;
const domain = this.currentDomain;
const preset = this.selectedPreset || '';
this.compareMainLoading = true;
this.compareMainError = '';
try {
const queryPreset = encodeURIComponent(preset);
const data = await this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/main-branch-config?preset_name=' + queryPreset,
{ headers: this.buildAuthHeaders() },
{ timeoutMs: 22000 }
);
if (domain !== this.currentDomain || preset !== (this.selectedPreset || '')) return;
const formatted = this.formatConfigCompareJson(data.config || {});
this.compareMainPresetName = String(data.preset_name || '').trim() || '主预设';
this.compareMainMatchMode = String(data.match_mode || '').trim();
this.compareMainPath = String(data.path || 'config/sites/index.json').trim() || 'config/sites/index.json';
this.compareMainSource = { ...(data.source || {}) };
this.compareMainOriginalText = formatted;
this.compareMainDraft = formatted;
} catch (error) {
if (domain !== this.currentDomain || preset !== (this.selectedPreset || '')) return;
console.error('加载 main 分支配置失败:', error);
this.compareMainError = this.formatGitCompareErrorMessage(error);
this.compareMainPresetName = '';
this.compareMainMatchMode = '';
this.compareMainPath = '';
this.compareMainSource = {};
this.compareMainOriginalText = '';
this.compareMainDraft = '';
} finally {
if (domain === this.currentDomain && preset === (this.selectedPreset || '')) {
this.compareMainLoading = false;
}
}
},
resetConfigCompareLocalDraft() {
const changed = this.compareLocalDirty;
this.compareLocalDraft = this.compareLocalOriginalText;
this.notifyCompare(changed ? '已撤销左侧草稿更改' : '左侧没有可撤销的改动', changed ? 'success' : 'info');
},
resetConfigCompareMainDraft() {
const changed = this.compareMainDirty;
this.compareMainDraft = this.compareMainOriginalText;
this.notifyCompare(changed ? '已恢复右侧参考草稿' : '右侧没有可恢复的改动', changed ? 'success' : 'info');
},
async saveConfigCompareLocalDraft() {
if (!this.currentDomain || !this.selectedPreset) return;
if (!this.compareLocalParsed.valid) {
this.notifyCompare('左侧本地草稿 JSON 无效,不能保存', 'warning');
return;
}
const domain = this.currentDomain;
const preset = this.selectedPreset;
const parsedValue = this.compareLocalParsed.value;
this.compareSavingLocal = true;
try {
const data = await this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/preset-config',
{
method: 'PUT',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
preset_name: preset,
config: parsedValue
})
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
const resolvedPreset = String(data.preset_name || preset).trim() || preset;
const savedConfig = data.config || parsedValue;
const cloned = JSON.parse(JSON.stringify(savedConfig));
if (this.currentConfig) {
if (this.currentConfig.presets && typeof this.currentConfig.presets === 'object') {
this.currentConfig.presets[resolvedPreset] = cloned;
} else {
Object.keys(this.currentConfig).forEach(key => {
delete this.currentConfig[key];
});
Object.assign(this.currentConfig, cloned);
}
}
this.selectedPreset = resolvedPreset;
this.compareLocalOriginalText = this.formatConfigCompareJson(savedConfig);
this.compareLocalDraft = this.compareLocalOriginalText;
this.notifyCompare('当前预设已保存', 'success');
} catch (error) {
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
console.error('保存当前预设失败:', error);
this.notifyCompare('保存失败: ' + error.message, 'error');
} finally {
if (domain === this.currentDomain && preset === this.selectedPreset) {
this.compareSavingLocal = false;
}
}
},
async applyMainDraftToCurrentPreset() {
if (!this.compareMainParsed.valid) {
this.notifyCompare('右侧 main 配置 JSON 无效,不能覆盖当前预设', 'warning');
return;
}
this.compareLocalDraft = this.formatConfigCompareJson(this.compareMainParsed.value);
await this.saveConfigCompareLocalDraft();
},
copyMainToLocalDraft() {
this.compareLocalDraft = this.compareMainDraft;
},
copyLocalToMainDraft() {
this.compareMainDraft = this.compareLocalDraft;
},
// 选择器值更新
updateSelectorValue(key, value) {
const pc = this.presetConfig;
if (pc && pc.selectors) {
pc.selectors[key] = value;
}
},
// 流式配置保存
async saveStreamConfig(config) {
if (!this.currentDomain) return;
const domain = this.currentDomain;
const preset = this.selectedPreset;
const nextConfig = this.cloneConfigSection(config || {});
const saveSeq = Number(this.streamConfigSaveSeq || 0) + 1;
this.streamConfigSaveSeq = saveSeq;
const pc = this.presetConfig;
if (pc) {
pc.stream_config = nextConfig;
// 修复:后端 set_site_stream_config 会把 send_confirmation 从 stream_config 剥离、
// merge 进 file_paste(engine.py:2826-2840),而 streamConfig computed 又以
// file_paste 为权威。若这里只写 stream_config,用户在「流式配置 → 发送确认」
// 的改动会在下一次重算时被旧的 file_paste 值盖回去,看起来像"没保存成功"。
if (nextConfig && nextConfig.send_confirmation && typeof nextConfig.send_confirmation === 'object') {
if (!pc.file_paste || typeof pc.file_paste !== 'object') pc.file_paste = {};
pc.file_paste.send_confirmation = {
...(pc.file_paste.send_confirmation || {}),
...nextConfig.send_confirmation
};
}
}
const previousSave = this.streamConfigSaveQueue || Promise.resolve();
const saveRequest = previousSave
.catch(() => undefined)
.then(() => {
const payload = { ...nextConfig, preset_name: preset };
return this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/stream-config',
{
method: 'PUT',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify(payload)
},
{ timeoutMs: 12000 }
);
});
this.streamConfigSaveQueue = saveRequest;
try {
await saveRequest;
if (
saveSeq !== this.streamConfigSaveSeq
|| domain !== this.currentDomain
|| preset !== this.selectedPreset
) return;
} catch (e) {
if (
saveSeq !== this.streamConfigSaveSeq
|| domain !== this.currentDomain
|| preset !== this.selectedPreset
) return;
console.error('保存流式配置失败:', e);
alert('保存失败: ' + e.message);
this.$emit('reload-config');
} finally {
if (this.streamConfigSaveQueue === saveRequest) {
this.streamConfigSaveQueue = null;
}
}
},
updateModelCatalog(value) {
// 模型目录已独立持久化存储至 config/arena_model_catalog.local.json,不再污染主预设对象
},
sanitizeInputStabilityWaitTimeout(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) {
return 1.5;
}
return Math.min(10, Math.max(0.1, parsed));
},
sanitizeSendConfirmationCheckTimeout(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) {
return 1.5;
}
return Math.min(10, Math.max(0.1, parsed));
},
sanitizeUrlTransitionWaitPatterns(value) {
const seen = new Set();
const items = Array.isArray(value)
? value
: (typeof value === 'string'
? value.replace(/\r\n/g, '\n').replace(/;/g, '\n').split('\n')
: []);
const result = [];
items.forEach(item => {
const text = String(item || '').trim();
if (!text || seen.has(text)) return;
seen.add(text);
result.push(text);
});
return result;
},
formatUrlTransitionWaitPatterns(value) {
return this.sanitizeUrlTransitionWaitPatterns(value).join('\n');
},
buildSiteAdvancedPayload(overrides = {}) {
const siteAdvanced = {
independent_cookies: false,
independent_cookies_auto_takeover: false,
...((this.currentConfig && this.currentConfig.advanced) || {}),
...overrides
};
return {
independent_cookies: !!siteAdvanced.independent_cookies,
independent_cookies_auto_takeover: !!siteAdvanced.independent_cookies_auto_takeover,
};
},
buildPresetAdvancedPayload(overrides = {}, options = {}) {
const nextAdvanced = {
input_box_stability_wait_enabled: !!this.siteAdvancedConfig.input_box_stability_wait_enabled,
input_box_stability_wait_after_new_chat_only: !!this.siteAdvancedConfig.input_box_stability_wait_after_new_chat_only,
input_box_stability_wait_timeout: this.sanitizeInputStabilityWaitTimeout(
this.siteAdvancedConfig.input_box_stability_wait_timeout
),
url_transition_wait_on_new_chat: !!this.siteAdvancedConfig.url_transition_wait_on_new_chat,
send_confirmation_check_enabled: !!this.siteAdvancedConfig.send_confirmation_check_enabled,
send_confirmation_check_timeout: this.sanitizeSendConfirmationCheckTimeout(
this.siteAdvancedConfig.send_confirmation_check_timeout
),
skip_new_chat_on_retry: !!this.siteAdvancedConfig.skip_new_chat_on_retry,
...overrides
};
const payload = {
preset_name: this.selectedPreset,
input_box_stability_wait_enabled: !!nextAdvanced.input_box_stability_wait_enabled,
input_box_stability_wait_after_new_chat_only: !!nextAdvanced.input_box_stability_wait_after_new_chat_only,
input_box_stability_wait_timeout: this.sanitizeInputStabilityWaitTimeout(
nextAdvanced.input_box_stability_wait_timeout
),
url_transition_wait_on_new_chat: !!nextAdvanced.url_transition_wait_on_new_chat,
send_confirmation_check_enabled: !!nextAdvanced.send_confirmation_check_enabled,
send_confirmation_check_timeout: this.sanitizeSendConfirmationCheckTimeout(
nextAdvanced.send_confirmation_check_timeout
),
skip_new_chat_on_retry: !!nextAdvanced.skip_new_chat_on_retry
};
const shouldSendPatterns = (
options.includeUrlTransitionWaitPatterns
|| Object.prototype.hasOwnProperty.call(overrides, 'url_transition_wait_patterns')
);
if (shouldSendPatterns) {
payload.url_transition_wait_patterns = this.sanitizeUrlTransitionWaitPatterns(
nextAdvanced.url_transition_wait_patterns
);
}
return payload;
},
filterPresetAdvancedFields(config = {}) {
const keys = [
'input_box_stability_wait_enabled',
'input_box_stability_wait_after_new_chat_only',
'input_box_stability_wait_timeout',
'url_transition_wait_on_new_chat',
'url_transition_wait_patterns',
'send_confirmation_check_enabled',
'send_confirmation_check_timeout',
'skip_new_chat_on_retry'
];
const result = {};
keys.forEach(key => {
if (Object.prototype.hasOwnProperty.call(config, key)) {
if (key === 'input_box_stability_wait_timeout') {
result[key] = this.sanitizeInputStabilityWaitTimeout(config[key]);
} else if (key === 'send_confirmation_check_timeout') {
result[key] = this.sanitizeSendConfirmationCheckTimeout(config[key]);
} else if (key === 'url_transition_wait_patterns') {
result[key] = this.sanitizeUrlTransitionWaitPatterns(config[key]);
} else {
result[key] = !!config[key];
}
}
});
return result;
},
filterSiteAdvancedFields(config = {}) {
const keys = [
'independent_cookies',
'independent_cookies_auto_takeover'
];
const result = {};
keys.forEach(key => {
if (Object.prototype.hasOwnProperty.call(config, key)) {
result[key] = !!config[key];
}
});
return result;
},
assignConfigSection(target, key, value) {
if (!this.canAssignObjectProperty(target, key)) {
return false;
}
target[key] = this.cloneConfigSection(value);
return true;
},
assignCurrentConfigAdvanced(value) {
if (!this.currentConfig) {
return false;
}
return this.assignConfigSection(this.currentConfig, 'advanced', value);
},
assignPresetAdvanced(value) {
if (!this.presetConfig) {
return false;
}
return this.assignConfigSection(this.presetConfig, 'advanced', value);
},
startAdvancedConfigSave() {
const saveSeq = Number(this.advancedConfigSaveSeq || 0) + 1;
this.advancedConfigSaveSeq = saveSeq;
this.advancedConfigSaving = true;
return saveSeq;
},
finishAdvancedConfigSave(saveSeq) {
if (saveSeq === this.advancedConfigSaveSeq) {
this.advancedConfigSaving = false;
}
},
async persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, options = {}) {
const presetScoped = !!options.presetScoped;
const saveSeq = Number(options.saveSeq || 0);
const previousTarget = previousAdvanced || {};
const domain = this.currentDomain;
const preset = this.selectedPreset;
let saveRequest = null;
try {
const previousSave = this.advancedConfigSaveQueue || Promise.resolve();
saveRequest = previousSave
.catch(() => undefined)
.then(() => this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/advanced-config',
{
method: 'PUT',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify(nextAdvanced)
},
{ timeoutMs: 12000 }
));
this.advancedConfigSaveQueue = saveRequest;
const data = await saveRequest;
if (
domain !== this.currentDomain
|| (presetScoped && preset !== this.selectedPreset)
|| (saveSeq && saveSeq !== this.advancedConfigSaveSeq)
) {
return false;
}
if (presetScoped && this.presetConfig) {
this.assignPresetAdvanced(this.filterPresetAdvancedFields(data.advanced || nextAdvanced));
} else {
this.assignCurrentConfigAdvanced({
...previousTarget,
...this.filterSiteAdvancedFields(data.advanced || nextAdvanced)
});
}
this.$emit('reload-config');
return true;
} catch (error) {
if (
domain !== this.currentDomain
|| (presetScoped && preset !== this.selectedPreset)
|| (saveSeq && saveSeq !== this.advancedConfigSaveSeq)
) {
return false;
}
if (presetScoped && this.presetConfig) {
this.assignPresetAdvanced(previousTarget);
} else if (this.currentConfig) {
this.assignCurrentConfigAdvanced(previousTarget);
}
throw error;
} finally {
if (saveRequest && this.advancedConfigSaveQueue === saveRequest) {
this.advancedConfigSaveQueue = null;
}
}
},
async updateIndependentCookies(enabled, event) {
if (!this.currentDomain || !this.currentConfig) return;
const nextEnabled = !!enabled;
const currentEnabled = !!this.siteAdvancedConfig.independent_cookies;
if (nextEnabled && !currentEnabled) {
const confirmed = window.confirm(
[
'开启“独立 Cookie 标签页”后,可能带来这些影响:',
'',
'1. 新开的该站点独立会话通常不会继承当前受控浏览器里的登录态、Cookie 和 localStorage,可能表现为未登录。',
'2. 独立会话通常会以单独窗口出现,并且内存占用会明显高于普通标签页。',
'',
'确认仍要开启吗?'
].join('\n')
);
if (!confirmed) {
// 用户取消:把 checkbox 视觉状态还原
if (event && event.target) event.target.checked = currentEnabled;
return;
}
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.currentConfig.advanced || {}) };
const nextAdvanced = this.buildSiteAdvancedPayload({
independent_cookies: nextEnabled,
independent_cookies_auto_takeover: !!previousAdvanced.independent_cookies_auto_takeover
});
this.assignCurrentConfigAdvanced({
...previousAdvanced,
...this.filterSiteAdvancedFields(nextAdvanced)
});
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { saveSeq });
} catch (e) {
console.error('保存站点高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateIndependentCookiesAutoTakeover(enabled) {
if (!this.currentDomain || !this.currentConfig) return;
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.currentConfig.advanced || {}) };
const nextAdvanced = this.buildSiteAdvancedPayload({
independent_cookies_auto_takeover: !!enabled
});
this.assignCurrentConfigAdvanced({
...previousAdvanced,
...this.filterSiteAdvancedFields(nextAdvanced)
});
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { saveSeq });
} catch (e) {
console.error('保存站点高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateInputStabilityWaitEnabled(enabled) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
input_box_stability_wait_enabled: !!enabled
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateInputStabilityWaitAfterNewChatOnly(enabled) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
input_box_stability_wait_after_new_chat_only: !!enabled
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateInputStabilityWaitTimeout(value) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
input_box_stability_wait_timeout: this.sanitizeInputStabilityWaitTimeout(value)
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateUrlTransitionWaitOnNewChat(enabled) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
url_transition_wait_on_new_chat: !!enabled
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateUrlTransitionWaitPatterns(value) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
url_transition_wait_patterns: this.sanitizeUrlTransitionWaitPatterns(value)
}, { includeUrlTransitionWaitPatterns: true });
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateSendConfirmationCheckEnabled(enabled) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
send_confirmation_check_enabled: !!enabled
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateSendConfirmationCheckTimeout(value) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
send_confirmation_check_timeout: this.sanitizeSendConfirmationCheckTimeout(value)
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async updateSkipNewChatOnRetry(enabled) {
// 修复:切站点时 selectedPreset 会被短暂置空,空预设名会被后端当作站点级配置写入,污染该站点全部预设
if (!this.currentDomain || !this.currentConfig || !this.presetConfig || !this.selectedPreset) {
if (this.currentDomain && !this.selectedPreset) {
this.notifyCompare('预设尚未加载完成,请稍后重试', 'warning');
}
return;
}
const saveSeq = this.startAdvancedConfigSave();
const previousAdvanced = { ...(this.presetConfig.advanced || {}) };
const nextAdvanced = this.buildPresetAdvancedPayload({
skip_new_chat_on_retry: !!enabled
});
this.assignPresetAdvanced(this.filterPresetAdvancedFields(nextAdvanced));
try {
await this.persistSiteAdvancedConfig(nextAdvanced, previousAdvanced, { presetScoped: true, saveSeq });
} catch (e) {
console.error('保存预设高级配置失败:', e);
alert('保存失败: ' + e.message);
} finally {
this.finishAdvancedConfigSave(saveSeq);
}
},
async createIsolatedCookieTab() {
if (!this.currentDomain) return;
const domain = this.currentDomain;
this.isolatedTabCreating = true;
try {
const result = await this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/isolated-tab',
{
method: 'POST',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' })
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain) return;
alert(result.message || ('已为 ' + domain + ' 新建独立 Cookie 标签页'));
} catch (e) {
if (domain !== this.currentDomain) return;
console.error('新建独立 Cookie 标签页失败:', e);
alert('新建失败: ' + e.message);
} finally {
if (domain === this.currentDomain) {
this.isolatedTabCreating = false;
}
}
},
async createSharedCookieTab() {
if (!this.currentDomain) return;
const domain = this.currentDomain;
this.sharedTabCreating = true;
try {
const result = await this.fetchJson(
'/api/sites/' + encodeURIComponent(domain) + '/shared-tab',
{
method: 'POST',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' })
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain) return;
alert(result.message || ('已为 ' + domain + ' 打开共享 Cookie 受控窗口'));
} catch (e) {
if (domain !== this.currentDomain) return;
console.error('打开共享 Cookie 受控窗口失败:', e);
alert('打开失败: ' + e.message);
} finally {
if (domain === this.currentDomain) {
this.sharedTabCreating = false;
}
}
},
// ===== 🆕 预设管理方法 =====
async loadPresets() {
if (!this.currentDomain) return;
const domain = this.currentDomain;
this.presetLoading = true;
try {
const data = await this.fetchJson(
'/api/presets/' + encodeURIComponent(domain),
{ headers: this.buildAuthHeaders() },
{ timeoutMs: 10000 }
);
if (domain !== this.currentDomain) return;
this.availablePresets = [...new Set([...(data.presets || ['主预设']), ...Object.keys(this.currentConfig?.presets || {})])];
const apiDefault = data.default_preset;
if (apiDefault && this.availablePresets.includes(apiDefault)) {
this.defaultPreset = apiDefault;
} else if (this.availablePresets.includes('主预设')) {
this.defaultPreset = '主预设';
} else {
this.defaultPreset = this.availablePresets[0] || '主预设';
}
// 确保选中的预设仍然有效
if (!this.availablePresets.includes(this.selectedPreset)) {
this.selectedPreset = this.defaultPreset || this.availablePresets[0] || '主预设';
}
} catch (e) {
if (domain !== this.currentDomain) return;
console.error('加载预设列表失败:', e);
if (!this.availablePresets.length) {
this.availablePresets = ['主预设'];
this.defaultPreset = '主预设';
}
// 修复:切站点时 selectedPreset 会先被同步清空。若此处只在 availablePresets
// 为空时才纠正,上一个站点的列表还在(非空)就会让 selectedPreset 永久卡在 '',
// 导致所有预设级开关一直被守卫拒绝,且用户无从察觉。
if (!this.selectedPreset) {
this.selectedPreset = this.defaultPreset || this.availablePresets[0] || '主预设';
}
} finally {
if (domain === this.currentDomain) {
this.presetLoading = false;
}
}
},
switchPreset(presetName) {
this.selectedPreset = presetName;
// All preset drafts already belong to currentConfig; fetching here loses unsaved imports.
this.ensurePresetMutableSections();
},
async setDefaultPreset() {
if (!this.currentDomain || !this.selectedPreset) return;
const domain = this.currentDomain;
const preset = this.selectedPreset;
if (this.stagePresetOperation('default')) return;
try {
await this.fetchJson(
'/api/presets/' + encodeURIComponent(domain) + '/default',
{
method: 'PUT',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ preset_name: preset })
},
{ timeoutMs: 10000 }
);
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
this.defaultPreset = preset;
this.currentConfig.default_preset = preset;
alert('✅ 默认预设已设置为 "' + preset + '"(仅本地覆盖)');
} catch (e) {
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
alert('❌ 设置默认预设失败: ' + e.message);
}
},
async createPreset() {
const name = this.newPresetName.trim();
if (!name) return;
if (!this.currentDomain) return;
const domain = this.currentDomain;
const sourcePreset = this.selectedPreset;
if (this.stagePresetOperation('create', name)) return;
this.flushMutableSectionDrafts();
const sourceDraft = JSON.parse(JSON.stringify(this.presetConfig));
try {
await this.fetchJson(
'/api/presets/' + encodeURIComponent(domain),
{
method: 'POST',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
new_name: name,
source_name: sourcePreset
})
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain) return;
this.newPresetName = '';
this.showNewPresetInput = false;
this.currentConfig.presets[name] = sourceDraft;
this.availablePresets = Object.keys(this.currentConfig.presets);
this.selectedPreset = name;
alert('✅ 预设 "' + name + '" 已创建(克隆自 "' + sourcePreset + '")');
} catch (e) {
if (domain !== this.currentDomain) return;
alert('❌ 创建失败: ' + e.message);
}
},
async renamePreset() {
const newName = this.renamePresetName.trim();
if (!newName) return;
if (!this.currentDomain) return;
if (!this.selectedPreset) return;
if (newName === this.selectedPreset) {
this.showRenamePresetInput = false;
this.renamePresetName = '';
return;
}
const domain = this.currentDomain;
const oldName = this.selectedPreset;
if (this.stagePresetOperation('rename', newName)) return;
try {
await this.fetchJson(
'/api/presets/' + encodeURIComponent(domain) + '/rename',
{
method: 'PUT',
headers: this.buildAuthHeaders({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
old_name: oldName,
new_name: newName
})
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain || oldName !== this.selectedPreset) return;
this.showRenamePresetInput = false;
this.renamePresetName = '';
this.flushMutableSectionDrafts();
if (this.currentConfig.presets && Object.hasOwn(this.currentConfig.presets, oldName)) {
this.currentConfig.presets[newName] = this.currentConfig.presets[oldName];
delete this.currentConfig.presets[oldName];
}
if (this.currentConfig.default_preset === oldName) this.currentConfig.default_preset = newName;
if (this.defaultPreset === oldName) this.defaultPreset = newName;
this.availablePresets = this.availablePresets.map(n => n === oldName ? newName : n);
this.selectedPreset = newName;
alert('✅ 预设已重命名为 "' + newName + '"');
} catch (e) {
if (domain !== this.currentDomain || oldName !== this.selectedPreset) return;
alert('❌ 重命名失败: ' + e.message);
}
},
async deletePreset() {
if (this.availablePresets.length <= 1) {
alert('不能删除最后一个预设');
return;
}
if (!confirm('确定要删除预设 "' + this.selectedPreset + '" 吗?此操作不可撤销。')) {
return;
}
const domain = this.currentDomain;
const preset = this.selectedPreset;
if (this.stagePresetOperation('delete')) return;
try {
await this.fetchJson(
'/api/presets/' + encodeURIComponent(domain) + '/' + encodeURIComponent(preset),
{
method: 'DELETE',
headers: this.buildAuthHeaders()
},
{ timeoutMs: 12000 }
);
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
this.flushMutableSectionDrafts();
delete this.currentConfig.presets[preset];
await this.loadPresets();
if (domain !== this.currentDomain) return;
this.currentConfig.default_preset = this.defaultPreset;
this.selectedPreset = this.defaultPreset || this.availablePresets[0] || '主预设';
alert('✅ 预设已删除');
} catch (e) {
if (domain !== this.currentDomain || preset !== this.selectedPreset) return;
alert('❌ 删除失败: ' + e.message);
}
}
},
watch: {
currentDomain: {
handler(newDomain) {
this.resetConfigCompareState();
this.pendingPresetTransfer = 0;
if (newDomain) {
// 切换站点时强制按站点默认预设初始化
this.selectedPreset = '';
this.defaultPreset = '主预设';
this.showNewPresetInput = false;
this.showRenamePresetInput = false;
this.newPresetName = '';
this.renamePresetName = '';
this.loadPresets();
} else {
this.availablePresets = [];
this.selectedPreset = '主预设';
this.defaultPreset = '主预设';
this.showNewPresetInput = false;
this.showRenamePresetInput = false;
this.newPresetName = '';
this.renamePresetName = '';
}
},
immediate: true
},
selectedPreset(newValue, oldValue) {
this.ensurePresetMutableSections();
if (newValue === oldValue || !this.showConfigCompareDialog) return;
this.syncConfigCompareLocalDraft(true);
this.loadMainBranchCompareConfig();
},
currentConfig: {
handler() {
this.ensurePresetMutableSections();
if (!this.showConfigCompareDialog) return;
this.syncConfigCompareLocalDraft(false);
},
deep: true,
immediate: true
}
},
template: `
<div class="config-workspace h-full overflow-auto p-4">
<!-- 空状态 -->
<div v-if="!currentDomain || !currentConfig" class="h-full flex items-center justify-center">
<div class="text-center text-gray-400 dark:text-gray-500">
<div class="text-4xl mb-4">📝</div>
<div class="text-lg">请选择或新增站点配置</div>
</div>
</div>
<!-- 配置内容 -->
<div v-else class="space-y-4">
<!-- 🆕 预设选择器 -->
<div class="config-preset-card bg-white dark:bg-gray-800 border dark:border-gray-700 rounded-lg shadow-sm px-4 py-3">
<div class="config-preset-toolbar flex items-center justify-between flex-wrap gap-3">
<div class="config-preset-meta flex items-center gap-3">
<span class="config-preset-label studio-tip" tabindex="0" data-tip="同一站点可以保存多套设置,在标签页池为各标签页选择不同预设。"><span v-html="$icons.folderOpen"></span>当前预设</span>
<select v-model="selectedPreset" aria-label="当前预设"
@change="switchPreset(selectedPreset)"
:disabled="presetLoading"
class="border dark:border-gray-600 px-3 py-1.5 rounded-md text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-400 focus:border-transparent min-w-[150px]">
<option v-if="presetLoading && !availablePresets.length" value="">读取预设中…</option>
<option v-for="p in availablePresets" :key="p" :value="p">{{ p }}</option>
</select>
<span class="config-preset-count">{{ presetLoading ? '读取中…' : '共 ' + availablePresets.length + ' 个' }}</span>
<span class="config-preset-chip" :title="'未手动指定预设的标签页会使用默认预设'">
默认 · {{ defaultPreset || '主预设' }}
</span>
</div>
<div class="config-preset-actions flex items-center gap-2">
<!-- 新建预设:行内输入 -->
<template v-if="showNewPresetInput">
<input v-model="newPresetName"
@keyup.enter="createPreset"
@keyup.escape="showNewPresetInput = false; newPresetName = ''"
placeholder="输入新预设名称"
class="preset-inline-input border dark:border-gray-600 px-2 py-1 rounded text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-400"
autofocus>
<button @click="createPreset"
:disabled="!newPresetName.trim()"
class="preset-btn">
创建
</button>
<button @click="showNewPresetInput = false; newPresetName = ''"
class="preset-btn">
取消
</button>
</template>
<!-- 重命名预设:行内输入 -->
<template v-else-if="showRenamePresetInput">
<input ref="renamePresetInput" aria-label="新的预设名称" v-model="renamePresetName"
@keyup.enter="renamePreset"
@keyup.escape="showRenamePresetInput = false; renamePresetName = ''"
:placeholder="'重命名 ' + selectedPreset"
class="preset-inline-input border dark:border-gray-600 px-2 py-1 rounded text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-400">
<button @click="renamePreset"
:disabled="!renamePresetName.trim()"
class="preset-btn">
重命名
</button>
<button @click="showRenamePresetInput = false; renamePresetName = ''"
class="preset-btn">
取消
</button>
</template>
<!-- 常规操作 -->
<template v-else>
<button @click="setDefaultPreset"
:disabled="!selectedPreset || selectedPreset === defaultPreset"
:title="selectedPreset === defaultPreset ? '当前预设已是默认预设' : '把当前预设设为默认'"
class="preset-btn">
设为默认
</button>
<button title="复制当前预设的设置,创建一份新的预设。" @click="showNewPresetInput = true; showRenamePresetInput = false; renamePresetName = ''"
class="preset-btn">
<span v-html="$icons.plusCircle"></span>新建预设
</button>
<button type="button" class="preset-btn" :disabled="!selectedPreset" @click="beginPresetRename"><span v-html="$icons.pencil"></span>重命名</button>
<preset-transfer :domain="currentDomain" :site="currentConfig" :preset="selectedPreset" :prepare="flushMutableSectionDrafts" :disabled="presetLoading" @apply="applyPresetTransfer"></preset-transfer>
<details class="preset-more">
<summary class="preset-btn" aria-label="预设工具">预设工具<span v-html="$icons.chevronDown"></span></summary>
<div class="preset-more-pop">
<button title="读取 Git main 分支中的已提交配置并对比;不会自动覆盖当前草稿。" @click="$event.currentTarget.closest('details').open = false; openConfigCompare()">
<span v-html="$icons.arrowPathRoundedSquare"></span>与仓库版本对比
</button>
<hr class="preset-more-sep">
<button class="is-danger"
:disabled="availablePresets.length <= 1"
:title="availablePresets.length <= 1 ? '不能删除最后一个预设' : '删除当前预设'"
@click="$event.currentTarget.closest('details').open = false; deletePreset()">
<span v-html="$icons.trash"></span>删除当前预设
</button>
</div>
</details>
</template>
</div>
</div>
</div>
<label class="site-section-select">配置目录<select v-model="activeWorkspaceSection" aria-label="移动端配置分类"><option v-for="(section, index) in workspaceSections" :key="section.id" :value="section.id">{{ String(index + 1).padStart(2, '0') }} · {{ section.title }}</option></select></label>
<div class="site-edit-layout">
<aside class="site-config-outline">
<p>配置目录</p>
<nav class="uwa-config-section-nav" aria-label="配置分类">
<button v-for="(section, index) in workspaceSections" :key="section.id" type="button" :class="{ 'is-active': activeWorkspaceSection === section.id }" :aria-pressed="activeWorkspaceSection === section.id" @click="activeWorkspaceSection = section.id"><span class="site-section-number">{{ String(index + 1).padStart(2, '0') }}</span><span><strong>{{ section.title }}</strong><small>{{ section.subtitle }}</small></span><i v-html="$icons.arrowRight"></i></button>
</nav>
<div class="site-outline-note"><span v-html="$icons.bookOpen"></span><strong>按需配置,不必全部填写</strong><p>先把核心元素定位好,再根据站点能力逐步扩展。</p></div>
</aside>
<div class="site-config-content">
<header class="site-section-intro"><span>{{ String(workspaceSections.findIndex(s => s.id === activeWorkspaceSection) + 1).padStart(2, '0') }} / 配置预设</span><h3>{{ activeWorkspaceMeta.heading }}</h3><p>{{ activeWorkspaceMeta.description }}</p></header>
<!-- 选择器面板 -->
<selector-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'selectors'" class="config-fixed-panel"
:selectors="presetConfig.selectors || {}"
:collapsed="collapsedPanels.selectors"
@update:collapsed="collapsedPanels.selectors = $event"
@add-selector="$emit('add-selector', $event)"
@remove-selector="$emit('remove-selector', $event)"
@update-selector-key="(oldKey, newKey) => $emit('update-selector-key', oldKey, newKey)"
@update-selector-value="updateSelectorValue"
@test-selector="(key, val) => $emit('test-selector', key, val)"
/>
<!-- 图片配置面板 -->
<image-config-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'media'" class="config-fixed-panel"
:image-config="imageConfig"
:current-domain="currentDomain"
:collapsed="collapsedPanels.media"
@update:collapsed="collapsedPanels.media = $event"
@update-image-config="$emit('update-image-config', $event)"
@reload-config="$emit('reload-config')"
/>
<!-- 流式配置面板 -->
<stream-config-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'response'" class="config-fixed-panel"
:stream-config="streamConfig"
:current-domain="currentDomain"
:collapsed="collapsedPanels.response"
@update:collapsed="collapsedPanels.response = $event"
@save-stream-config="saveStreamConfig"
/>
<!-- 文件粘贴配置面板 -->
<file-paste-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'input'" class="config-fixed-panel"
:file-paste-config="filePasteConfigRef"
:current-domain="currentDomain"
:selected-preset="selectedPreset"
:collapsed="collapsedPanels.filePaste"
@update:collapsed="collapsedPanels.filePaste = $event"
/>
<prompt-padding-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'input'" class="config-fixed-panel"
:prompt-padding-config="promptPaddingConfigRef"
:current-domain="currentDomain"
:selected-preset="selectedPreset"
:collapsed="collapsedPanels.promptPadding"
@update:collapsed="collapsedPanels.promptPadding = $event"
/>
<!-- 高级功能面板 -->
<div v-show="activeWorkspaceSection === 'advanced'" class="config-advanced-panel config-fixed-panel cap-panel"><div class="cap-legacy-header"></div><div class="cap-feature-stack"><p class="cap-module-note">这里的修改会自动保存。</p><section class="cap-card" :class="{ 'is-enabled': siteAdvancedConfig.independent_cookies }" aria-label="隔离登录会话"><div class="cap-card-header"><span class="cap-icon" v-html="$icons.folderOpen"></span><div class="cap-copy"><div class="cap-title-row"><h4>隔离登录会话</h4><span class="cap-scope">整个站点</span></div><p>让不同标签页各用各的登录状态,避免多个账号互相影响。</p></div><label class="cap-switch"><span class="cap-state" :class="{ 'is-on': siteAdvancedConfig.independent_cookies }">{{ siteAdvancedConfig.independent_cookies ? '已开启' : '已关闭' }}</span><input role="switch" aria-label="隔离登录会话"
type="checkbox"
class="cap-switch-input"
:checked="siteAdvancedConfig.independent_cookies"
:disabled="advancedConfigSaving"
@change="updateIndependentCookies($event.target.checked, $event)"
><i aria-hidden="true"></i></label></div><p class="cap-warning">开启后,新会话不继承原登录状态,可能需要重新登录。</p><details class="cap-settings"><summary><span><strong>管理会话与接管规则</strong><small>需要多账号或匿名环境时使用。</small></span><span class="cap-chevron" v-html="$icons.chevronDown"></span></summary><div class="cap-settings-body">
<details class="group">
<summary class="text-xs text-blue-500 dark:text-blue-400 cursor-pointer select-none">
查看说明
</summary>
<div class="mt-2 space-y-2 pl-2">
<p class="text-xs text-gray-400 dark:text-gray-500">开启后,可以为这个站点创建独立 Cookie 会话。</p>
<p class="text-xs text-gray-400 dark:text-gray-500">说明:Chromium 的独立上下文通常会显示为单独窗口。这不是新起一个完全独立的浏览器进程,而是同一浏览器里的隔离会话。</p>
<p class="text-xs text-amber-600 dark:text-amber-400">注意:开启后,新开的该站点标签页不会继承当前浏览器里已有的登录态、Cookie 或 localStorage。原本已登录的共享标签页如果重新进入并被转换为独立标签页,通常会表现为未登录。</p>
<p class="text-xs text-gray-400 dark:text-gray-500">单标签页清 Cookie 不会影响同站点的其它独立标签页。</p>
<p class="text-xs text-gray-400 dark:text-gray-500">默认不会自动接管你手动新开的普通标签页,避免原标签页被关闭;只有点下面的按钮才会新建独立会话。</p>
</div>
</details>
<div class="flex items-center justify-between">
</div>
<label class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400 cursor-pointer">
<input
type="checkbox"
class="rounded"
:checked="siteAdvancedConfig.independent_cookies_auto_takeover"
:disabled="advancedConfigSaving || !siteAdvancedConfig.independent_cookies"
@change="updateIndependentCookiesAutoTakeover($event.target.checked)"
>
<span>自动接管手动新标签页(会关闭原页并改为独立窗口)</span>
</label>
<div class="flex items-center gap-3 flex-wrap">
<button
@click="createSharedCookieTab"
:disabled="sharedTabCreating"
class="px-3 py-1.5 text-xs font-medium bg-slate-600 text-white rounded hover:bg-slate-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
{{ sharedTabCreating ? '打开中...' : '打开共享 Cookie 受控窗口' }}
</button>
<button
@click="createIsolatedCookieTab"
:disabled="!siteAdvancedConfig.independent_cookies || isolatedTabCreating"
class="px-3 py-1.5 text-xs font-medium bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed"
>
{{ isolatedTabCreating ? '创建中...' : '新建独立 Cookie 会话(单独窗口)' }}
</button>
</div>
</div></details></section><section class="cap-card" :class="{ 'is-enabled': siteAdvancedConfig.input_box_stability_wait_enabled }" aria-label="等待输入框就绪"><div class="cap-card-header"><span class="cap-icon" v-html="$icons.timer"></span><div class="cap-copy"><div class="cap-title-row"><h4>等待输入框就绪</h4><span class="cap-scope">当前预设</span></div><p>新建对话后输入框会重建?等它稳定下来,再填写消息。</p></div><label class="cap-switch"><span class="cap-state" :class="{ 'is-on': siteAdvancedConfig.input_box_stability_wait_enabled }">{{ siteAdvancedConfig.input_box_stability_wait_enabled ? '已开启' : '已关闭' }}</span><input role="switch" aria-label="等待输入框就绪"
type="checkbox"
class="cap-switch-input"
:checked="siteAdvancedConfig.input_box_stability_wait_enabled"
:disabled="advancedConfigSaving"
@change="updateInputStabilityWaitEnabled($event.target.checked)"
><i aria-hidden="true"></i></label></div><details class="cap-settings"><summary><span><strong>调整等待规则</strong><small>只有偶发填入失败、输入位置丢失时再调整。</small></span><span class="cap-chevron" v-html="$icons.chevronDown"></span></summary><div class="cap-settings-body">
<label class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400 cursor-pointer">
<input
type="checkbox"
class="rounded"
:checked="siteAdvancedConfig.input_box_stability_wait_after_new_chat_only"
:disabled="advancedConfigSaving || !siteAdvancedConfig.input_box_stability_wait_enabled"
@change="updateInputStabilityWaitAfterNewChatOnly($event.target.checked)"
>
<span>仅在刚点击 new_chat_btn 后启用</span>
</label>
<label class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400 cursor-pointer">
<input
type="checkbox"
class="rounded"
:checked="siteAdvancedConfig.url_transition_wait_on_new_chat"
:disabled="advancedConfigSaving"
@change="updateUrlTransitionWaitOnNewChat($event.target.checked)"
>
<span>新建对话后等待 URL 切换(当前预设)</span>
</label>
<label class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400">
<span>最长等待</span>
<input
type="number"
min="0.2"
max="10"
step="0.1"
class="w-24 rounded border dark:border-gray-600 px-2 py-1 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
:value="siteAdvancedConfig.input_box_stability_wait_timeout"
:disabled="advancedConfigSaving || !siteAdvancedConfig.input_box_stability_wait_enabled"
@change="updateInputStabilityWaitTimeout($event.target.value)"
>
<span>秒</span>
</label>
<label class="flex items-start gap-3 text-xs text-gray-500 dark:text-gray-400">
<span class="pt-1">URL 匹配</span>
<textarea
rows="3"
class="flex-1 min-w-0 rounded border dark:border-gray-600 px-2 py-1 bg-white dark:bg-gray-700 text-gray-900 dark:text-white text-xs leading-5"
:value="formatUrlTransitionWaitPatterns(siteAdvancedConfig.url_transition_wait_patterns)"
:disabled="advancedConfigSaving"
placeholder="每行一个正则或关键字"
@change="updateUrlTransitionWaitPatterns($event.target.value)"
></textarea>
</label>
<p class="text-[11px] text-gray-400 dark:text-gray-500">
这里的规则会作为旧会话 URL 的兜底匹配;留空则只用启发式判断。
</p>
</div></details></section><section class="cap-card" :class="{ 'is-enabled': siteAdvancedConfig.send_confirmation_check_enabled }" aria-label="发送失败时自动恢复"><div class="cap-card-header"><span class="cap-icon" v-html="$icons.arrowPath"></span><div class="cap-copy"><div class="cap-title-row"><h4>发送失败时自动恢复</h4><span class="cap-scope">当前预设</span></div><p>消息没有真正发出时,确认状态并尝试恢复。</p></div><label class="cap-switch"><span class="cap-state" :class="{ 'is-on': siteAdvancedConfig.send_confirmation_check_enabled }">{{ siteAdvancedConfig.send_confirmation_check_enabled ? '已开启' : '已关闭' }}</span><input role="switch" aria-label="发送失败时自动恢复"
type="checkbox"
class="cap-switch-input"
:checked="siteAdvancedConfig.send_confirmation_check_enabled"
:disabled="advancedConfigSaving"
@change="updateSendConfirmationCheckEnabled($event.target.checked)"
><i aria-hidden="true"></i></label></div><details class="cap-settings"><summary><span><strong>调整确认与重试</strong><small>设置确认超时,以及重试时是否新建对话。</small></span><span class="cap-chevron" v-html="$icons.chevronDown"></span></summary><div class="cap-settings-body">
<label class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400">
<span>确认超时</span>
<input
type="number"
min="0.1"
max="10"
step="0.1"
class="w-24 rounded border dark:border-gray-600 px-2 py-1 bg-white dark:bg-gray-700 text-gray-900 dark:text-white"
:value="siteAdvancedConfig.send_confirmation_check_timeout"
:disabled="advancedConfigSaving || !siteAdvancedConfig.send_confirmation_check_enabled"
@change="updateSendConfirmationCheckTimeout($event.target.value)"
>
<span>秒</span>
</label>
<label class="flex items-center gap-3 text-sm text-gray-700 dark:text-gray-300 cursor-pointer">
<input
type="checkbox"
class="rounded"
:checked="siteAdvancedConfig.skip_new_chat_on_retry"
:disabled="advancedConfigSaving"
@change="updateSkipNewChatOnRetry($event.target.checked)"
>
<span>重试轮跳过新建对话(适用于 429 或过盾自愈)</span>
</label>
</div></details></section></div></div>
<!-- 工作流面板 -->
<adapter-panel v-if="currentDomain" v-show="activeWorkspaceSection === 'maintenance'" class="config-fixed-panel"
:domain="currentDomain || ''" :preset-name="selectedPreset || ''" :request="adapterRequest"
@updated="onAdapterUpdated"></adapter-panel>
<workflow-panel v-if="presetConfig" v-show="activeWorkspaceSection === 'workflow'" class="config-fixed-panel"
:workflow="presetConfig.workflow || []"
:selectors="presetConfig.selectors || {}"
:model-catalog="presetConfig.model_catalog || {}"
:current-domain="currentDomain"
:selected-preset="selectedPreset"
:collapsed="false"
@update:model-catalog="updateModelCatalog"
@add-step="$emit('add-step')"
@remove-step="$emit('remove-step', $event)"
@move-step="(index, dir) => $emit('move-step', index, dir)"
@action-change="$emit('action-change', $event)"
@show-templates="$emit('show-templates')"
/>
</div>
</div>
<div v-if="showConfigCompareDialog"
data-config-compare-root
class="fixed top-0 left-0 right-0 bottom-0 z-50 flex items-center justify-center bg-slate-950/75 p-4 sm:p-6 backdrop-blur-md"
style="position:fixed; left:0; top:0; width:100vw; height:100vh; z-index:9999; display:flex; align-items:center; justify-content:center;"
@click.self="closeConfigCompareDialog">
<div class="flex h-[94vh] w-full max-w-[1860px] flex-col overflow-hidden rounded-[24px] border border-slate-700/70 bg-slate-900 shadow-2xl ring-1 ring-white/10"
style="width:min(96vw, 1860px); height:94vh; max-height:94vh;">
<div class="relative overflow-hidden border-b border-slate-800 bg-slate-900/85 px-4 py-2.5 sm:px-5">
<div class="absolute inset-0 bg-[radial-gradient(circle_at_top_left,rgba(59,130,246,.18),transparent_35%),radial-gradient(circle_at_top_right,rgba(16,185,129,.12),transparent_28%)]"></div>
<div class="relative z-10 flex flex-col gap-2 lg:flex-row lg:items-center lg:justify-between">
<div>
<div class="flex flex-wrap items-center gap-2">
<span class="rounded-full border border-blue-500/30 bg-blue-500/10 px-2.5 py-1 text-[11px] font-semibold uppercase tracking-[0.18em] text-blue-300">Main Compare</span>
<span class="rounded-full border border-slate-700 bg-slate-800 px-2.5 py-1 text-xs font-medium text-slate-300">{{ currentDomain }}</span>
</div>
<h3 class="mt-1.5 text-base font-bold tracking-tight text-white">配置对比与合并</h3>
</div>
<div class="flex flex-wrap items-center gap-2">
<button @click="loadMainBranchCompareConfig"
:disabled="compareMainLoading"
class="rounded-xl border border-slate-700 bg-slate-800/80 px-3 py-1.5 text-xs font-medium text-slate-200 transition hover:bg-slate-700 hover:text-white disabled:cursor-not-allowed disabled:opacity-50">
{{ compareMainLoading ? '读取中...' : '刷新官方配置' }}
</button>
<button @click="closeConfigCompareDialog"
class="rounded-xl border border-rose-500/20 bg-rose-500/10 px-3 py-1.5 text-xs font-medium text-rose-300 transition hover:bg-rose-500/20 hover:text-rose-200">
关闭
</button>
</div>
</div>
</div>
<div class="flex flex-1 flex-col overflow-hidden bg-slate-950/40 p-3">
<div class="mb-2 rounded-2xl border border-slate-800 bg-slate-900/70 px-3 py-2 shadow-sm">
<div class="flex flex-wrap items-center justify-between gap-2 text-[11px]">
<div class="flex flex-wrap items-center gap-2">
<span class="rounded-full border border-slate-700 bg-slate-800 px-2.5 py-1 text-slate-300">
当前预设: {{ selectedPreset || defaultPreset || '主预设' }}
</span>
<span class="rounded-full border border-slate-700 bg-slate-800 px-2.5 py-1 text-slate-300">
官方来源: {{ compareMainSource.repository || '官方仓库' }}/{{ compareMainSource.branch || 'main' }} · {{ compareMainPath || 'config/sites.json' }}
</span>
<span v-if="compareMainSource.status"
:class="compareMainSource.stale
? 'border-amber-500/30 bg-amber-500/10 text-amber-300'
: 'border-emerald-500/30 bg-emerald-500/10 text-emerald-300'"
class="rounded-full border px-2.5 py-1">
{{ compareMainSource.stale ? '缓存回退' : (compareMainSource.status === 'validated_cache' ? '已确认最新' : '刚刚获取') }}
<template v-if="compareMainSource.stale ? compareMainSource.fetched_at : compareMainSource.checked_at">
· {{ formatOfficialConfigSourceTime(compareMainSource.stale ? compareMainSource.fetched_at : compareMainSource.checked_at) }}
</template>
</span>
<span v-if="compareMainPresetName"
class="rounded-full border border-blue-500/30 bg-blue-500/10 px-2.5 py-1 text-blue-300">
main 预设: {{ compareMainPresetName }}
</span>
<span v-if="compareMainMatchMode && compareMainMatchMode !== 'exact'"
class="rounded-full border border-amber-500/30 bg-amber-500/10 px-2.5 py-1 text-amber-300">
{{ getConfigCompareMatchLabel(compareMainMatchMode) }}
</span>
</div>
<div v-if="compareLocalParsed.valid && compareMainParsed.valid" class="flex flex-wrap items-center gap-2">
<span class="rounded-full border border-rose-500/30 bg-rose-500/10 px-2 py-0.5 font-semibold text-rose-300">
{{ compareDifferentCount }} 项不同
</span>
<span class="rounded-full border border-emerald-500/30 bg-emerald-500/10 px-2 py-0.5 font-semibold text-emerald-300">
{{ compareSameCount }} 项一致
</span>
</div>
</div>
<div v-if="compareLocalParsed.valid && compareMainParsed.valid" class="mt-2 flex flex-wrap gap-1">
<span v-for="item in compareFieldDiffs"
:key="'compare-diff-' + item.key"
:class="['rounded-full border px-2 py-0.5 text-[11px]', getConfigCompareDiffClass(item.status)]">
{{ item.label }} · {{ getConfigCompareDiffText(item.status) }}
</span>
</div>
<div v-if="compareMainSource.warning"
class="mt-2 border-l-2 border-amber-400 bg-amber-500/10 px-3 py-2 text-xs text-amber-200">
{{ compareMainSource.warning }}
</div>
</div>
<div v-if="compareMainError"
class="mb-2 rounded-2xl border border-rose-500/20 bg-rose-500/10 px-3 py-2 text-xs text-rose-200">
{{ compareMainError }}
</div>
<div class="flex min-h-0 flex-1 flex-col gap-2 xl:flex-row">
<div class="flex min-h-0 flex-1 flex-col overflow-hidden rounded-2xl border border-slate-800 bg-slate-900/85 shadow-lg ring-1 ring-white/5">
<div class="flex flex-wrap items-start justify-between gap-2 border-b border-slate-800 bg-slate-800/50 px-4 py-2.5">
<div>
<div class="text-sm font-semibold text-slate-100">本地预设草稿</div>
<div class="mt-0.5 text-[11px] text-slate-500">保存后会直接写入当前工作区。</div>
</div>
<div class="flex flex-wrap gap-2">
<button @click="resetConfigCompareLocalDraft"
:disabled="!compareLocalDirty"
class="rounded-lg px-2.5 py-1 text-[11px] font-medium text-slate-400 transition hover:bg-slate-700 hover:text-white disabled:opacity-30">
撤销更改
</button>
<button @click="saveConfigCompareLocalDraft"
:disabled="compareSavingLocal || !compareLocalParsed.valid"
class="rounded-lg bg-blue-600 px-3 py-1 text-[11px] font-semibold text-white shadow-md transition hover:bg-blue-500 disabled:opacity-50">
{{ compareSavingLocal ? '保存中...' : '保存到本地' }}
</button>
</div>
</div>
<div class="border-b border-slate-800 bg-slate-900/80 p-2">
<div class="flex flex-wrap gap-1">
<div v-for="item in compareLocalSummaryItems"
:key="'local-summary-' + item.label"
class="inline-flex items-center gap-1 rounded-lg border border-slate-700/60 bg-slate-800/60 px-2 py-1 text-[11px] text-slate-300">
<span class="text-slate-500">{{ item.label }}</span>
<span class="font-semibold text-slate-100">{{ item.value }}</span>
</div>
</div>
<div v-if="compareLocalDraft && !compareLocalParsed.valid"
class="mt-2 rounded-lg border border-rose-500/20 bg-rose-500/10 px-3 py-2 text-xs text-rose-300">
JSON 有误: {{ compareLocalParsed.error }}
</div>
</div>
<textarea v-model="compareLocalDraft"
spellcheck="false"
class="flex-1 resize-none bg-[#0d1117] px-4 py-3 font-mono text-sm leading-7 text-slate-300 outline-none"></textarea>
</div>
<div class="flex flex-col items-center justify-center gap-2 xl:w-20">
<div class="hidden h-full w-px bg-gradient-to-b from-transparent via-slate-700 to-transparent xl:block"></div>
<div class="flex flex-row gap-2 xl:flex-col">
<button @click="copyMainToLocalDraft"
class="group flex h-9 min-w-[3.25rem] items-center justify-center rounded-xl border border-slate-700 bg-slate-800 px-2 text-[11px] font-medium text-slate-300 shadow-lg transition hover:scale-105 hover:border-blue-500 hover:bg-blue-500/10 hover:text-blue-300"
title="把右侧草稿复制到左侧草稿">
到左
</button>
<button @click="applyMainDraftToCurrentPreset"
:disabled="compareSavingLocal || !compareMainParsed.valid"
class="flex h-10 min-w-[3.5rem] items-center justify-center rounded-xl bg-gradient-to-br from-emerald-500 to-emerald-600 px-2 text-[11px] font-bold text-white shadow-lg shadow-emerald-500/30 transition hover:scale-105 disabled:pointer-events-none disabled:opacity-50"
title="直接用右侧内容覆盖当前预设并保存">
覆盖
</button>
<button @click="copyLocalToMainDraft"
class="group flex h-9 min-w-[3.25rem] items-center justify-center rounded-xl border border-slate-700 bg-slate-800 px-2 text-[11px] font-medium text-slate-300 shadow-lg transition hover:scale-105 hover:border-emerald-500 hover:bg-emerald-500/10 hover:text-emerald-300"
title="把左侧草稿复制到右侧草稿">
到右
</button>
</div>
<div class="hidden h-full w-px bg-gradient-to-b from-slate-700 via-slate-700 to-transparent xl:block"></div>
</div>
<div class="flex min-h-0 flex-1 flex-col overflow-hidden rounded-2xl border border-slate-800 bg-slate-900/85 shadow-lg ring-1 ring-white/5">
<div class="flex flex-wrap items-start justify-between gap-2 border-b border-slate-800 bg-slate-800/50 px-4 py-2.5">
<div>
<div class="text-sm font-semibold text-slate-100">main 分支参考</div>
<div class="mt-0.5 text-[11px] text-slate-500">右侧是参考草稿,不会回写到远端仓库。</div>
</div>
<div class="flex flex-wrap gap-2">
<button @click="resetConfigCompareMainDraft"
:disabled="!compareMainDirty"
class="rounded-lg px-2.5 py-1 text-[11px] font-medium text-slate-400 transition hover:bg-slate-700 hover:text-white disabled:opacity-30">
恢复右侧
</button>
<button @click="loadMainBranchCompareConfig"
:disabled="compareMainLoading"
class="rounded-lg border border-slate-700 bg-slate-800 px-2.5 py-1 text-[11px] font-medium text-slate-300 transition hover:bg-slate-700 hover:text-white disabled:opacity-40">
{{ compareMainLoading ? '读取中...' : '重读 main' }}
</button>
</div>
</div>
<div class="border-b border-slate-800 bg-slate-900/80 p-2">
<div class="flex flex-wrap gap-1">
<div v-for="item in compareMainSummaryItems"
:key="'main-summary-' + item.label"
class="inline-flex items-center gap-1 rounded-lg border border-slate-700/60 bg-slate-800/60 px-2 py-1 text-[11px] text-slate-300">
<span class="text-slate-500">{{ item.label }}</span>
<span class="font-semibold text-slate-100">{{ item.value }}</span>
</div>
</div>
<div v-if="compareMainDraft && !compareMainParsed.valid"
class="mt-2 rounded-lg border border-rose-500/20 bg-rose-500/10 px-3 py-2 text-xs text-rose-300">
JSON 有误: {{ compareMainParsed.error }}
</div>
</div>
<textarea v-model="compareMainDraft"
spellcheck="false"
class="flex-1 resize-none bg-[#0d1117] px-4 py-3 font-mono text-sm leading-7 text-slate-300 outline-none"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`
};