调整为默认不传参数的模式

This commit is contained in:
RT15548
2026-01-21 11:40:37 +08:00
committed by GitHub
parent 84aa77b426
commit e56cfa6a18

View File

@@ -35,9 +35,9 @@ class StreamingGeneration {
init() { init() {
if (this.isInitialized) return; if (this.isInitialized) return;
try { localStorage.removeItem('xbgen:lastToggleSnap'); } catch {} try { localStorage.removeItem('xbgen:lastToggleSnap'); } catch { }
this.registerCommands(); this.registerCommands();
try { xbLog.info('streamingGeneration', 'init'); } catch {} try { xbLog.info('streamingGeneration', 'init'); } catch { }
this.isInitialized = true; this.isInitialized = true;
} }
@@ -65,7 +65,7 @@ class StreamingGeneration {
_cleanupOldestSessions() { _cleanupOldestSessions() {
const sorted = [...this.sessions.entries()].sort((a, b) => a[1].updatedAt - b[1].updatedAt); const sorted = [...this.sessions.entries()].sort((a, b) => a[1].updatedAt - b[1].updatedAt);
sorted.slice(0, Math.max(0, sorted.length - 9)).forEach(([sid, s]) => { sorted.slice(0, Math.max(0, sorted.length - 9)).forEach(([sid, s]) => {
try { s.abortController?.abort(); } catch {} try { s.abortController?.abort(); } catch { }
this.sessions.delete(sid); this.sessions.delete(sid);
}); });
} }
@@ -97,10 +97,10 @@ class StreamingGeneration {
try { frames[i].postMessage(msg, targetOrigin); } catch { fail++; } try { frames[i].postMessage(msg, targetOrigin); } catch { fail++; }
} }
if (fail) { if (fail) {
try { xbLog.warn('streamingGeneration', `postToFrames fail=${fail} total=${frames.length} type=${name}`); } catch {} try { xbLog.warn('streamingGeneration', `postToFrames fail=${fail} total=${frames.length} type=${name}`); } catch { }
} }
} }
} catch {} } catch { }
} }
resolveCurrentApiAndModel(apiOptions = {}) { resolveCurrentApiAndModel(apiOptions = {}) {
@@ -152,14 +152,14 @@ class StreamingGeneration {
if (!source) { if (!source) {
console.error('[xbgen:callAPI] 不支持的 api:', opts.api); console.error('[xbgen:callAPI] 不支持的 api:', opts.api);
try { xbLog.error('streamingGeneration', `unsupported api: ${opts.api}`, null); } catch {} try { xbLog.error('streamingGeneration', `unsupported api: ${opts.api}`, null); } catch { }
} }
if (!source) throw new Error(`不支持的 api: ${opts.api}`); if (!source) throw new Error(`不支持的 api: ${opts.api}`);
const model = String(opts.model || '').trim(); const model = String(opts.model || '').trim();
if (!model) { if (!model) {
try { xbLog.error('streamingGeneration', 'missing model', null); } catch {} try { xbLog.error('streamingGeneration', 'missing model', null); } catch { }
} }
if (!model) throw new Error('未检测到当前模型,请在聊天面板选择模型或在插件设置中为分析显式指定模型。'); if (!model) throw new Error('未检测到当前模型,请在聊天面板选择模型或在插件设置中为分析显式指定模型。');
@@ -169,7 +169,7 @@ class StreamingGeneration {
const msgCount = Array.isArray(messages) ? messages.length : null; const msgCount = Array.isArray(messages) ? messages.length : null;
xbLog.info('streamingGeneration', `callAPI stream=${!!stream} api=${String(opts.api || '')} model=${model} messages=${msgCount ?? '-'}`); xbLog.info('streamingGeneration', `callAPI stream=${!!stream} api=${String(opts.api || '')} model=${model} messages=${msgCount ?? '-'}`);
} }
} catch {} } catch { }
const provider = String(opts.api || '').toLowerCase(); const provider = String(opts.api || '').toLowerCase();
const reverseProxyConfigured = String(opts.apiurl || '').trim().length > 0; const reverseProxyConfigured = String(opts.apiurl || '').trim().length > 0;
const pwd = String(opts.apipassword || '').trim(); const pwd = String(opts.apipassword || '').trim();
@@ -187,33 +187,20 @@ class StreamingGeneration {
await writeSecret(secretKey, pwd, 'xbgen-inline'); await writeSecret(secretKey, pwd, 'xbgen-inline');
} }
} }
} catch {} } catch { }
const num = (v) => { const num = (v) => {
const n = Number(v); const n = Number(v);
return Number.isFinite(n) ? n : undefined; return Number.isFinite(n) ? n : undefined;
}; };
const isUnset = (k) => baseOptions?.[k] === '__unset__'; const isUnset = (k) => baseOptions?.[k] === '__unset__';
const tUser = num(baseOptions?.temperature); // 只使用命令参数,不从 UI 设置读取
const ppUser = num(baseOptions?.presence_penalty); const effectiveTemperature = isUnset('temperature') ? undefined : num(baseOptions?.temperature);
const fpUser = num(baseOptions?.frequency_penalty); const effectivePresence = isUnset('presence_penalty') ? undefined : num(baseOptions?.presence_penalty);
const tpUser = num(baseOptions?.top_p); const effectiveFrequency = isUnset('frequency_penalty') ? undefined : num(baseOptions?.frequency_penalty);
const tkUser = num(baseOptions?.top_k); const effectiveTopP = isUnset('top_p') ? undefined : num(baseOptions?.top_p);
const mtUser = num(baseOptions?.max_tokens); const effectiveTopK = isUnset('top_k') ? undefined : num(baseOptions?.top_k);
const tUI = num(oai_settings?.temp_openai); const effectiveMaxT = isUnset('max_tokens') ? undefined : num(baseOptions?.max_tokens);
const ppUI = num(oai_settings?.pres_pen_openai);
const fpUI = num(oai_settings?.freq_pen_openai);
const tpUI_OpenAI = num(oai_settings?.top_p_openai ?? oai_settings?.top_p);
const mtUI_OpenAI = num(oai_settings?.openai_max_tokens ?? oai_settings?.max_tokens);
const tpUI_Gemini = num(oai_settings?.makersuite_top_p ?? oai_settings?.top_p);
const tkUI_Gemini = num(oai_settings?.makersuite_top_k ?? oai_settings?.top_k);
const mtUI_Gemini = num(oai_settings?.makersuite_max_tokens ?? oai_settings?.max_output_tokens ?? oai_settings?.openai_max_tokens ?? oai_settings?.max_tokens);
const effectiveTemperature = isUnset('temperature') ? undefined : (tUser ?? tUI);
const effectivePresence = isUnset('presence_penalty') ? undefined : (ppUser ?? ppUI);
const effectiveFrequency = isUnset('frequency_penalty') ? undefined : (fpUser ?? fpUI);
const effectiveTopP = isUnset('top_p') ? undefined : (tpUser ?? (source === chat_completion_sources.MAKERSUITE ? tpUI_Gemini : tpUI_OpenAI));
const effectiveTopK = isUnset('top_k') ? undefined : (tkUser ?? (source === chat_completion_sources.MAKERSUITE ? tkUI_Gemini : undefined));
const effectiveMaxT = isUnset('max_tokens') ? undefined : (mtUser ?? (source === chat_completion_sources.MAKERSUITE ? (mtUI_Gemini ?? mtUI_OpenAI) : mtUI_OpenAI) ?? 4000);
const body = { const body = {
messages, model, stream, messages, model, stream,
@@ -335,7 +322,7 @@ class StreamingGeneration {
console.error('[xbgen:stream] err.name:', err?.name); console.error('[xbgen:stream] err.name:', err?.name);
console.error('[xbgen:stream] err.message:', err?.message); console.error('[xbgen:stream] err.message:', err?.message);
if (err?.name === 'AbortError') return; if (err?.name === 'AbortError') return;
try { xbLog.error('streamingGeneration', 'Stream error', err); } catch {} try { xbLog.error('streamingGeneration', 'Stream error', err); } catch { }
throw err; throw err;
} }
})(); })();
@@ -368,7 +355,7 @@ class StreamingGeneration {
if (Array.isArray(chatArray)) { if (Array.isArray(chatArray)) {
await eventSource?.emit?.(event_types.CHAT_COMPLETION_PROMPT_READY, { chat: chatArray, dryRun: false }); await eventSource?.emit?.(event_types.CHAT_COMPLETION_PROMPT_READY, { chat: chatArray, dryRun: false });
} }
} catch {} } catch { }
} }
async processGeneration(generateData, prompt, sessionId, stream = true) { async processGeneration(generateData, prompt, sessionId, stream = true) {
@@ -377,7 +364,7 @@ class StreamingGeneration {
session.abortController = abortController; session.abortController = abortController;
try { try {
try { xbLog.info('streamingGeneration', `processGeneration start sid=${session.id} stream=${!!stream} promptLen=${String(prompt || '').length}`); } catch {} try { xbLog.info('streamingGeneration', `processGeneration start sid=${session.id} stream=${!!stream} promptLen=${String(prompt || '').length}`); } catch { }
this.isStreaming = true; this.isStreaming = true;
this.activeCount++; this.activeCount++;
session.isStreaming = true; session.isStreaming = true;
@@ -403,17 +390,17 @@ class StreamingGeneration {
this.postToFrames(EVT_DONE, payload); this.postToFrames(EVT_DONE, payload);
try { window?.postMessage?.({ type: EVT_DONE, payload, from: 'xiaobaix' }, getTrustedOrigin()); } catch { } try { window?.postMessage?.({ type: EVT_DONE, payload, from: 'xiaobaix' }, getTrustedOrigin()); } catch { }
try { xbLog.info('streamingGeneration', `processGeneration done sid=${session.id} outLen=${String(session.text || '').length}`); } catch {} try { xbLog.info('streamingGeneration', `processGeneration done sid=${session.id} outLen=${String(session.text || '').length}`); } catch { }
return String(session.text || ''); return String(session.text || '');
} catch (err) { } catch (err) {
if (err?.name === 'AbortError') { if (err?.name === 'AbortError') {
try { xbLog.warn('streamingGeneration', `processGeneration aborted sid=${session.id}`); } catch {} try { xbLog.warn('streamingGeneration', `processGeneration aborted sid=${session.id}`); } catch { }
return String(session.text || ''); return String(session.text || '');
} }
console.error('[StreamingGeneration] Generation error:', err); console.error('[StreamingGeneration] Generation error:', err);
console.error('[StreamingGeneration] error.error =', err?.error); console.error('[StreamingGeneration] error.error =', err?.error);
try { xbLog.error('streamingGeneration', `processGeneration error sid=${session.id}`, err); } catch {} try { xbLog.error('streamingGeneration', `processGeneration error sid=${session.id}`, err); } catch { }
let errorMessage = '生成失败'; let errorMessage = '生成失败';
@@ -653,7 +640,7 @@ class StreamingGeneration {
const uid = it?.uid || it?.id || it?.entry?.uid || it?.entry?.id; const uid = it?.uid || it?.id || it?.entry?.uid || it?.entry?.id;
if (uid) activatedUids.add(uid); if (uid) activatedUids.add(uid);
} }
} catch {} } catch { }
}; };
eventSource.on(event_types.WORLD_INFO_ACTIVATED, wiListener); eventSource.on(event_types.WORLD_INFO_ACTIVATED, wiListener);
try { try {
@@ -683,7 +670,7 @@ class StreamingGeneration {
const text = pieces.join('\n\n').replace(/\n{3,}/g, '\n\n').trim(); const text = pieces.join('\n\n').replace(/\n{3,}/g, '\n\n').trim();
if (text) return text; if (text) return text;
} }
} catch {} } catch { }
let src = []; let src = [];
const cd = capturedData; const cd = capturedData;
if (Array.isArray(cd)) { if (Array.isArray(cd)) {
@@ -837,7 +824,7 @@ class StreamingGeneration {
const cmd = getCmdForRoot(root); const cmd = getCmdForRoot(root);
const result = await window.STscript(cmd); const result = await window.STscript(cmd);
let parsed = result; let parsed = result;
try { parsed = JSON.parse(result); } catch {} try { parsed = JSON.parse(result); } catch { }
cache.set(root, parsed); cache.set(root, parsed);
} catch { } catch {
cache.set(root, ''); cache.set(root, '');
@@ -881,7 +868,7 @@ class StreamingGeneration {
const apiOptions = { const apiOptions = {
api: args?.api, apiurl: args?.apiurl, api: args?.api, apiurl: args?.apiurl,
apipassword: args?.apipassword, model: args?.model, apipassword: args?.apipassword, model: args?.model,
enableNet: ['on','true','1','yes'].includes(String(args?.net ?? '').toLowerCase()), enableNet: ['on', 'true', '1', 'yes'].includes(String(args?.net ?? '').toLowerCase()),
top_p: this.parseOpt(args, 'top_p'), top_p: this.parseOpt(args, 'top_p'),
top_k: this.parseOpt(args, 'top_k'), top_k: this.parseOpt(args, 'top_k'),
max_tokens: this.parseOpt(args, 'max_tokens'), max_tokens: this.parseOpt(args, 'max_tokens'),
@@ -898,7 +885,7 @@ class StreamingGeneration {
parsedStop = Array.isArray(j) ? j : (typeof j === 'string' ? [j] : undefined); parsedStop = Array.isArray(j) ? j : (typeof j === 'string' ? [j] : undefined);
} }
} }
} catch {} } catch { }
const nonstream = String(args?.nonstream || '').toLowerCase() === 'true'; const nonstream = String(args?.nonstream || '').toLowerCase() === 'true';
const b64dUtf8 = (s) => { const b64dUtf8 = (s) => {
try { try {
@@ -1012,7 +999,7 @@ class StreamingGeneration {
if (typeof prompt === 'string') prompt = prompt.replace(wiRegex, wiTrim); if (typeof prompt === 'string') prompt = prompt.replace(wiRegex, wiTrim);
} }
} }
} catch {} } catch { }
const addonSetStr = String(args?.addon || '').trim(); const addonSetStr = String(args?.addon || '').trim();
const shouldUsePM = addonSetStr.length > 0; const shouldUsePM = addonSetStr.length > 0;
if (!shouldUsePM) { if (!shouldUsePM) {
@@ -1023,20 +1010,20 @@ class StreamingGeneration {
const common = { messages, apiOptions, stop: parsedStop }; const common = { messages, apiOptions, stop: parsedStop };
if (nonstream) { if (nonstream) {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
try { try {
await this._emitPromptReady(messages); await this._emitPromptReady(messages);
const finalText = await this.processGeneration(common, prompt || '', sessionId, false); const finalText = await this.processGeneration(common, prompt || '', sessionId, false);
return String(finalText ?? ''); return String(finalText ?? '');
} finally { } finally {
try { if (lock) activateSendButtons(); } catch {} try { if (lock) activateSendButtons(); } catch { }
} }
} else { } else {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
await this._emitPromptReady(messages); await this._emitPromptReady(messages);
const p = this.processGeneration(common, prompt || '', sessionId, true); const p = this.processGeneration(common, prompt || '', sessionId, true);
p.finally(() => { try { if (lock) activateSendButtons(); } catch {} }); p.finally(() => { try { if (lock) activateSendButtons(); } catch { } });
p.catch(() => {}); p.catch(() => { });
return String(sessionId); return String(sessionId);
} }
} }
@@ -1059,7 +1046,7 @@ class StreamingGeneration {
chatBackup = chat.slice(); chatBackup = chat.slice();
chat.length = 0; chat.length = 0;
chat.push({ name: name1 || 'User', is_user: true, is_system: false, mes: '[hist]', send_date: new Date().toISOString() }); chat.push({ name: name1 || 'User', is_user: true, is_system: false, mes: '[hist]', send_date: new Date().toISOString() });
} catch {} } catch { }
} }
try { try {
await context.generate('normal', { await context.generate('normal', {
@@ -1131,7 +1118,7 @@ class StreamingGeneration {
return finalMessages; return finalMessages;
}; };
if (nonstream) { if (nonstream) {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
try { try {
const finalMessages = await buildAddonFinalMessages(); const finalMessages = await buildAddonFinalMessages();
const common = { messages: finalMessages, apiOptions, stop: parsedStop }; const common = { messages: finalMessages, apiOptions, stop: parsedStop };
@@ -1139,18 +1126,18 @@ class StreamingGeneration {
const finalText = await this.processGeneration(common, prompt || '', sessionId, false); const finalText = await this.processGeneration(common, prompt || '', sessionId, false);
return String(finalText ?? ''); return String(finalText ?? '');
} finally { } finally {
try { if (lock) activateSendButtons(); } catch {} try { if (lock) activateSendButtons(); } catch { }
} }
} else { } else {
(async () => { (async () => {
try { try {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
const finalMessages = await buildAddonFinalMessages(); const finalMessages = await buildAddonFinalMessages();
const common = { messages: finalMessages, apiOptions, stop: parsedStop }; const common = { messages: finalMessages, apiOptions, stop: parsedStop };
await this._emitPromptReady(finalMessages); await this._emitPromptReady(finalMessages);
await this.processGeneration(common, prompt || '', sessionId, true); await this.processGeneration(common, prompt || '', sessionId, true);
} catch {} finally { } catch { } finally {
try { if (lock) activateSendButtons(); } catch {} try { if (lock) activateSendButtons(); } catch { }
} }
})(); })();
return String(sessionId); return String(sessionId);
@@ -1207,7 +1194,7 @@ class StreamingGeneration {
const apiOptions = { const apiOptions = {
api: args?.api, apiurl: args?.apiurl, api: args?.api, apiurl: args?.apiurl,
apipassword: args?.apipassword, model: args?.model, apipassword: args?.apipassword, model: args?.model,
enableNet: ['on','true','1','yes'].includes(String(args?.net ?? '').toLowerCase()), enableNet: ['on', 'true', '1', 'yes'].includes(String(args?.net ?? '').toLowerCase()),
top_p: this.parseOpt(args, 'top_p'), top_p: this.parseOpt(args, 'top_p'),
top_k: this.parseOpt(args, 'top_k'), top_k: this.parseOpt(args, 'top_k'),
max_tokens: this.parseOpt(args, 'max_tokens'), max_tokens: this.parseOpt(args, 'max_tokens'),
@@ -1259,7 +1246,7 @@ class StreamingGeneration {
return dataWithOptions; return dataWithOptions;
}; };
if (nonstream) { if (nonstream) {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
try { try {
const dataWithOptions = await buildGenDataWithOptions(); const dataWithOptions = await buildGenDataWithOptions();
const chatMsgs = Array.isArray(dataWithOptions?.prompt) ? dataWithOptions.prompt const chatMsgs = Array.isArray(dataWithOptions?.prompt) ? dataWithOptions.prompt
@@ -1268,21 +1255,21 @@ class StreamingGeneration {
const finalText = await this.processGeneration(dataWithOptions, prompt, sessionId, false); const finalText = await this.processGeneration(dataWithOptions, prompt, sessionId, false);
return String(finalText ?? ''); return String(finalText ?? '');
} finally { } finally {
try { if (lock) activateSendButtons(); } catch {} try { if (lock) activateSendButtons(); } catch { }
} }
} }
(async () => { (async () => {
try { try {
try { if (lock) deactivateSendButtons(); } catch {} try { if (lock) deactivateSendButtons(); } catch { }
const dataWithOptions = await buildGenDataWithOptions(); const dataWithOptions = await buildGenDataWithOptions();
const chatMsgs = Array.isArray(dataWithOptions?.prompt) ? dataWithOptions.prompt const chatMsgs = Array.isArray(dataWithOptions?.prompt) ? dataWithOptions.prompt
: (Array.isArray(dataWithOptions?.messages) ? dataWithOptions.messages : []); : (Array.isArray(dataWithOptions?.messages) ? dataWithOptions.messages : []);
await this._emitPromptReady(chatMsgs); await this._emitPromptReady(chatMsgs);
const finalText = await this.processGeneration(dataWithOptions, prompt, sessionId, true); const finalText = await this.processGeneration(dataWithOptions, prompt, sessionId, true);
try { if (args && args._scope) args._scope.pipe = String(finalText ?? ''); } catch {} try { if (args && args._scope) args._scope.pipe = String(finalText ?? ''); } catch { }
} catch {} } catch { }
finally { finally {
try { if (lock) activateSendButtons(); } catch {} try { if (lock) activateSendButtons(); } catch { }
} }
})(); })();
return String(sessionId); return String(sessionId);
@@ -1292,7 +1279,7 @@ class StreamingGeneration {
const commonArgs = [ const commonArgs = [
{ name: 'id', description: '会话ID', typeList: [ARGUMENT_TYPE.STRING] }, { name: 'id', description: '会话ID', typeList: [ARGUMENT_TYPE.STRING] },
{ name: 'api', description: '后端: openai/claude/gemini/cohere/deepseek/custom', typeList: [ARGUMENT_TYPE.STRING] }, { name: 'api', description: '后端: openai/claude/gemini/cohere/deepseek/custom', typeList: [ARGUMENT_TYPE.STRING] },
{ name: 'net', description: '联网 on/off', typeList: [ARGUMENT_TYPE.STRING], enumList: ['on','off'] }, { name: 'net', description: '联网 on/off', typeList: [ARGUMENT_TYPE.STRING], enumList: ['on', 'off'] },
{ name: 'apiurl', description: '自定义后端URL', typeList: [ARGUMENT_TYPE.STRING] }, { name: 'apiurl', description: '自定义后端URL', typeList: [ARGUMENT_TYPE.STRING] },
{ name: 'apipassword', description: '后端密码', typeList: [ARGUMENT_TYPE.STRING] }, { name: 'apipassword', description: '后端密码', typeList: [ARGUMENT_TYPE.STRING] },
{ name: 'model', description: '模型名', typeList: [ARGUMENT_TYPE.STRING] }, { name: 'model', description: '模型名', typeList: [ARGUMENT_TYPE.STRING] },
@@ -1394,7 +1381,7 @@ CacheRegistry.register('streamingGeneration', {
} }
}, },
clear: () => { clear: () => {
try { streamingGeneration.cleanup(); } catch {} try { streamingGeneration.cleanup(); } catch { }
}, },
getDetail: () => { getDetail: () => {
try { try {
@@ -1415,7 +1402,7 @@ CacheRegistry.register('streamingGeneration', {
export function initStreamingGeneration() { export function initStreamingGeneration() {
const w = window; const w = window;
if ((w)?.isXiaobaixEnabled === false) return; if ((w)?.isXiaobaixEnabled === false) return;
try { xbLog.info('streamingGeneration', 'initStreamingGeneration'); } catch {} try { xbLog.info('streamingGeneration', 'initStreamingGeneration'); } catch { }
streamingGeneration.init(); streamingGeneration.init();
(w)?.registerModuleCleanup?.('streamingGeneration', () => streamingGeneration.cleanup()); (w)?.registerModuleCleanup?.('streamingGeneration', () => streamingGeneration.cleanup());
} }