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