调整为默认不传参数的模式
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 = {}) {
|
||||
@@ -120,255 +120,242 @@ class StreamingGeneration {
|
||||
}
|
||||
|
||||
|
||||
async callAPI(generateData, abortSignal, stream = true) {
|
||||
const messages = Array.isArray(generateData) ? generateData :
|
||||
(generateData?.prompt || generateData?.messages || generateData);
|
||||
const baseOptions = (!Array.isArray(generateData) && generateData?.apiOptions) ? generateData.apiOptions : {};
|
||||
const opts = { ...baseOptions, ...this.resolveCurrentApiAndModel(baseOptions) };
|
||||
async callAPI(generateData, abortSignal, stream = true) {
|
||||
const messages = Array.isArray(generateData) ? generateData :
|
||||
(generateData?.prompt || generateData?.messages || generateData);
|
||||
const baseOptions = (!Array.isArray(generateData) && generateData?.apiOptions) ? generateData.apiOptions : {};
|
||||
const opts = { ...baseOptions, ...this.resolveCurrentApiAndModel(baseOptions) };
|
||||
|
||||
const modelLower = String(opts.model || '').toLowerCase();
|
||||
const isClaudeThinkingModel =
|
||||
modelLower.includes('claude') &&
|
||||
modelLower.includes('thinking') &&
|
||||
!modelLower.includes('nothinking');
|
||||
const modelLower = String(opts.model || '').toLowerCase();
|
||||
const isClaudeThinkingModel =
|
||||
modelLower.includes('claude') &&
|
||||
modelLower.includes('thinking') &&
|
||||
!modelLower.includes('nothinking');
|
||||
|
||||
if (isClaudeThinkingModel && Array.isArray(messages) && messages.length > 0) {
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
if (lastMsg?.role === 'assistant') {
|
||||
console.log('[xbgen] Claude Thinking 模型:移除 assistant prefill');
|
||||
messages.pop();
|
||||
}
|
||||
}
|
||||
|
||||
const source = {
|
||||
openai: chat_completion_sources.OPENAI,
|
||||
claude: chat_completion_sources.CLAUDE,
|
||||
gemini: chat_completion_sources.MAKERSUITE,
|
||||
google: chat_completion_sources.MAKERSUITE,
|
||||
cohere: chat_completion_sources.COHERE,
|
||||
deepseek: chat_completion_sources.DEEPSEEK,
|
||||
custom: chat_completion_sources.CUSTOM,
|
||||
}[String(opts.api || '').toLowerCase()];
|
||||
|
||||
if (!source) {
|
||||
console.error('[xbgen:callAPI] 不支持的 api:', opts.api);
|
||||
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 {}
|
||||
}
|
||||
if (!model) throw new Error('未检测到当前模型,请在聊天面板选择模型或在插件设置中为分析显式指定模型。');
|
||||
|
||||
try {
|
||||
try {
|
||||
if (xbLog.isEnabled?.()) {
|
||||
const msgCount = Array.isArray(messages) ? messages.length : null;
|
||||
xbLog.info('streamingGeneration', `callAPI stream=${!!stream} api=${String(opts.api || '')} model=${model} messages=${msgCount ?? '-'}`);
|
||||
}
|
||||
} catch {}
|
||||
const provider = String(opts.api || '').toLowerCase();
|
||||
const reverseProxyConfigured = String(opts.apiurl || '').trim().length > 0;
|
||||
const pwd = String(opts.apipassword || '').trim();
|
||||
if (!reverseProxyConfigured && pwd) {
|
||||
const providerToSecretKey = {
|
||||
openai: SECRET_KEYS.OPENAI,
|
||||
gemini: SECRET_KEYS.MAKERSUITE,
|
||||
google: SECRET_KEYS.MAKERSUITE,
|
||||
cohere: SECRET_KEYS.COHERE,
|
||||
deepseek: SECRET_KEYS.DEEPSEEK,
|
||||
custom: SECRET_KEYS.CUSTOM,
|
||||
};
|
||||
const secretKey = providerToSecretKey[provider];
|
||||
if (secretKey) {
|
||||
await writeSecret(secretKey, pwd, 'xbgen-inline');
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
|
||||
const body = {
|
||||
messages, model, stream,
|
||||
chat_completion_source: source,
|
||||
temperature: effectiveTemperature,
|
||||
presence_penalty: effectivePresence,
|
||||
frequency_penalty: effectiveFrequency,
|
||||
top_p: effectiveTopP,
|
||||
max_tokens: effectiveMaxT,
|
||||
stop: Array.isArray(generateData?.stop) ? generateData.stop : undefined,
|
||||
use_makersuite_sysprompt: false,
|
||||
claude_use_sysprompt: oai_settings?.claude_use_sysprompt ?? false,
|
||||
custom_prompt_post_processing: undefined,
|
||||
// thinking 模型支持
|
||||
include_reasoning: oai_settings?.show_thoughts ?? true,
|
||||
reasoning_effort: oai_settings?.reasoning_effort || 'medium',
|
||||
};
|
||||
|
||||
// Claude 专用:top_k
|
||||
if (source === chat_completion_sources.CLAUDE) {
|
||||
body.top_k = Number(oai_settings?.top_k_openai) || undefined;
|
||||
}
|
||||
|
||||
if (source === chat_completion_sources.MAKERSUITE) {
|
||||
if (effectiveTopK !== undefined) body.top_k = effectiveTopK;
|
||||
body.max_output_tokens = effectiveMaxT;
|
||||
}
|
||||
const useNet = !!opts.enableNet;
|
||||
if (source === chat_completion_sources.MAKERSUITE && useNet) {
|
||||
body.tools = Array.isArray(body.tools) ? body.tools : [];
|
||||
if (!body.tools.some(t => t && t.google_search_retrieval)) {
|
||||
body.tools.push({ google_search_retrieval: {} });
|
||||
}
|
||||
body.enable_web_search = true;
|
||||
body.makersuite_use_google_search = true;
|
||||
}
|
||||
let reverseProxy = String(opts.apiurl || oai_settings?.reverse_proxy || '').trim();
|
||||
let proxyPassword = String(oai_settings?.proxy_password || '').trim();
|
||||
const cmdApiUrl = String(opts.apiurl || '').trim();
|
||||
const cmdApiPwd = String(opts.apipassword || '').trim();
|
||||
if (cmdApiUrl) {
|
||||
if (cmdApiPwd) proxyPassword = cmdApiPwd;
|
||||
} else if (cmdApiPwd) {
|
||||
reverseProxy = '';
|
||||
proxyPassword = '';
|
||||
}
|
||||
if (PROXY_SUPPORTED.has(source) && reverseProxy) {
|
||||
body.reverse_proxy = reverseProxy.replace(/\/?$/, '');
|
||||
if (proxyPassword) body.proxy_password = proxyPassword;
|
||||
}
|
||||
if (source === chat_completion_sources.CUSTOM) {
|
||||
const customUrl = String(cmdApiUrl || oai_settings?.custom_url || '').trim();
|
||||
if (customUrl) {
|
||||
body.custom_url = customUrl;
|
||||
} else {
|
||||
throw new Error('未配置自定义后端URL,请在命令中提供 apiurl 或在设置中填写 custom_url');
|
||||
}
|
||||
if (oai_settings?.custom_include_headers) body.custom_include_headers = oai_settings.custom_include_headers;
|
||||
if (oai_settings?.custom_include_body) body.custom_include_body = oai_settings.custom_include_body;
|
||||
if (oai_settings?.custom_exclude_body) body.custom_exclude_body = oai_settings.custom_exclude_body;
|
||||
}
|
||||
|
||||
|
||||
if (stream) {
|
||||
const payload = ChatCompletionService.createRequestData(body);
|
||||
|
||||
const streamFactory = await ChatCompletionService.sendRequest(payload, false, abortSignal);
|
||||
|
||||
const generator = (typeof streamFactory === 'function') ? streamFactory() : streamFactory;
|
||||
|
||||
return (async function* () {
|
||||
let last = '';
|
||||
try {
|
||||
for await (const item of (generator || [])) {
|
||||
if (abortSignal?.aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
let accumulated = '';
|
||||
if (typeof item === 'string') {
|
||||
accumulated = item;
|
||||
} else if (item && typeof item === 'object') {
|
||||
// 尝试多种字段
|
||||
accumulated = (typeof item.text === 'string' ? item.text : '') ||
|
||||
(typeof item.content === 'string' ? item.content : '') || '';
|
||||
|
||||
// thinking 相关字段
|
||||
if (!accumulated) {
|
||||
const thinking = item?.delta?.thinking || item?.thinking;
|
||||
if (typeof thinking === 'string') {
|
||||
accumulated = thinking;
|
||||
}
|
||||
}
|
||||
if (!accumulated) {
|
||||
const rc = item?.reasoning_content || item?.reasoning;
|
||||
if (typeof rc === 'string') {
|
||||
accumulated = rc;
|
||||
}
|
||||
}
|
||||
if (!accumulated) {
|
||||
const rc = item?.choices?.[0]?.delta?.reasoning_content;
|
||||
if (typeof rc === 'string') accumulated = rc;
|
||||
}
|
||||
}
|
||||
|
||||
if (!accumulated) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (accumulated.startsWith(last)) {
|
||||
last = accumulated;
|
||||
} else {
|
||||
last += accumulated;
|
||||
}
|
||||
yield last;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[xbgen:stream] 流式错误:', err);
|
||||
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 {}
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
const payload = ChatCompletionService.createRequestData(body);
|
||||
const extracted = await ChatCompletionService.sendRequest(payload, false, abortSignal);
|
||||
|
||||
let result = '';
|
||||
if (extracted && typeof extracted === 'object') {
|
||||
const msg = extracted?.choices?.[0]?.message;
|
||||
result = String(
|
||||
msg?.content ??
|
||||
msg?.reasoning_content ??
|
||||
extracted?.choices?.[0]?.text ??
|
||||
extracted?.content ??
|
||||
extracted?.reasoning_content ??
|
||||
''
|
||||
);
|
||||
} else {
|
||||
result = String(extracted ?? '');
|
||||
}
|
||||
|
||||
return result;
|
||||
if (isClaudeThinkingModel && Array.isArray(messages) && messages.length > 0) {
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
if (lastMsg?.role === 'assistant') {
|
||||
console.log('[xbgen] Claude Thinking 模型:移除 assistant prefill');
|
||||
messages.pop();
|
||||
}
|
||||
}
|
||||
|
||||
const source = {
|
||||
openai: chat_completion_sources.OPENAI,
|
||||
claude: chat_completion_sources.CLAUDE,
|
||||
gemini: chat_completion_sources.MAKERSUITE,
|
||||
google: chat_completion_sources.MAKERSUITE,
|
||||
cohere: chat_completion_sources.COHERE,
|
||||
deepseek: chat_completion_sources.DEEPSEEK,
|
||||
custom: chat_completion_sources.CUSTOM,
|
||||
}[String(opts.api || '').toLowerCase()];
|
||||
|
||||
if (!source) {
|
||||
console.error('[xbgen:callAPI] 不支持的 api:', opts.api);
|
||||
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 { }
|
||||
}
|
||||
if (!model) throw new Error('未检测到当前模型,请在聊天面板选择模型或在插件设置中为分析显式指定模型。');
|
||||
|
||||
try {
|
||||
try {
|
||||
if (xbLog.isEnabled?.()) {
|
||||
const msgCount = Array.isArray(messages) ? messages.length : null;
|
||||
xbLog.info('streamingGeneration', `callAPI stream=${!!stream} api=${String(opts.api || '')} model=${model} messages=${msgCount ?? '-'}`);
|
||||
}
|
||||
} catch { }
|
||||
const provider = String(opts.api || '').toLowerCase();
|
||||
const reverseProxyConfigured = String(opts.apiurl || '').trim().length > 0;
|
||||
const pwd = String(opts.apipassword || '').trim();
|
||||
if (!reverseProxyConfigured && pwd) {
|
||||
const providerToSecretKey = {
|
||||
openai: SECRET_KEYS.OPENAI,
|
||||
gemini: SECRET_KEYS.MAKERSUITE,
|
||||
google: SECRET_KEYS.MAKERSUITE,
|
||||
cohere: SECRET_KEYS.COHERE,
|
||||
deepseek: SECRET_KEYS.DEEPSEEK,
|
||||
custom: SECRET_KEYS.CUSTOM,
|
||||
};
|
||||
const secretKey = providerToSecretKey[provider];
|
||||
if (secretKey) {
|
||||
await writeSecret(secretKey, pwd, 'xbgen-inline');
|
||||
}
|
||||
}
|
||||
} catch { }
|
||||
|
||||
const num = (v) => {
|
||||
const n = Number(v);
|
||||
return Number.isFinite(n) ? n : undefined;
|
||||
};
|
||||
const isUnset = (k) => baseOptions?.[k] === '__unset__';
|
||||
// 只使用命令参数,不从 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,
|
||||
chat_completion_source: source,
|
||||
temperature: effectiveTemperature,
|
||||
presence_penalty: effectivePresence,
|
||||
frequency_penalty: effectiveFrequency,
|
||||
top_p: effectiveTopP,
|
||||
max_tokens: effectiveMaxT,
|
||||
stop: Array.isArray(generateData?.stop) ? generateData.stop : undefined,
|
||||
use_makersuite_sysprompt: false,
|
||||
claude_use_sysprompt: oai_settings?.claude_use_sysprompt ?? false,
|
||||
custom_prompt_post_processing: undefined,
|
||||
// thinking 模型支持
|
||||
include_reasoning: oai_settings?.show_thoughts ?? true,
|
||||
reasoning_effort: oai_settings?.reasoning_effort || 'medium',
|
||||
};
|
||||
|
||||
// Claude 专用:top_k
|
||||
if (source === chat_completion_sources.CLAUDE) {
|
||||
body.top_k = Number(oai_settings?.top_k_openai) || undefined;
|
||||
}
|
||||
|
||||
if (source === chat_completion_sources.MAKERSUITE) {
|
||||
if (effectiveTopK !== undefined) body.top_k = effectiveTopK;
|
||||
body.max_output_tokens = effectiveMaxT;
|
||||
}
|
||||
const useNet = !!opts.enableNet;
|
||||
if (source === chat_completion_sources.MAKERSUITE && useNet) {
|
||||
body.tools = Array.isArray(body.tools) ? body.tools : [];
|
||||
if (!body.tools.some(t => t && t.google_search_retrieval)) {
|
||||
body.tools.push({ google_search_retrieval: {} });
|
||||
}
|
||||
body.enable_web_search = true;
|
||||
body.makersuite_use_google_search = true;
|
||||
}
|
||||
let reverseProxy = String(opts.apiurl || oai_settings?.reverse_proxy || '').trim();
|
||||
let proxyPassword = String(oai_settings?.proxy_password || '').trim();
|
||||
const cmdApiUrl = String(opts.apiurl || '').trim();
|
||||
const cmdApiPwd = String(opts.apipassword || '').trim();
|
||||
if (cmdApiUrl) {
|
||||
if (cmdApiPwd) proxyPassword = cmdApiPwd;
|
||||
} else if (cmdApiPwd) {
|
||||
reverseProxy = '';
|
||||
proxyPassword = '';
|
||||
}
|
||||
if (PROXY_SUPPORTED.has(source) && reverseProxy) {
|
||||
body.reverse_proxy = reverseProxy.replace(/\/?$/, '');
|
||||
if (proxyPassword) body.proxy_password = proxyPassword;
|
||||
}
|
||||
if (source === chat_completion_sources.CUSTOM) {
|
||||
const customUrl = String(cmdApiUrl || oai_settings?.custom_url || '').trim();
|
||||
if (customUrl) {
|
||||
body.custom_url = customUrl;
|
||||
} else {
|
||||
throw new Error('未配置自定义后端URL,请在命令中提供 apiurl 或在设置中填写 custom_url');
|
||||
}
|
||||
if (oai_settings?.custom_include_headers) body.custom_include_headers = oai_settings.custom_include_headers;
|
||||
if (oai_settings?.custom_include_body) body.custom_include_body = oai_settings.custom_include_body;
|
||||
if (oai_settings?.custom_exclude_body) body.custom_exclude_body = oai_settings.custom_exclude_body;
|
||||
}
|
||||
|
||||
|
||||
if (stream) {
|
||||
const payload = ChatCompletionService.createRequestData(body);
|
||||
|
||||
const streamFactory = await ChatCompletionService.sendRequest(payload, false, abortSignal);
|
||||
|
||||
const generator = (typeof streamFactory === 'function') ? streamFactory() : streamFactory;
|
||||
|
||||
return (async function* () {
|
||||
let last = '';
|
||||
try {
|
||||
for await (const item of (generator || [])) {
|
||||
if (abortSignal?.aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
let accumulated = '';
|
||||
if (typeof item === 'string') {
|
||||
accumulated = item;
|
||||
} else if (item && typeof item === 'object') {
|
||||
// 尝试多种字段
|
||||
accumulated = (typeof item.text === 'string' ? item.text : '') ||
|
||||
(typeof item.content === 'string' ? item.content : '') || '';
|
||||
|
||||
// thinking 相关字段
|
||||
if (!accumulated) {
|
||||
const thinking = item?.delta?.thinking || item?.thinking;
|
||||
if (typeof thinking === 'string') {
|
||||
accumulated = thinking;
|
||||
}
|
||||
}
|
||||
if (!accumulated) {
|
||||
const rc = item?.reasoning_content || item?.reasoning;
|
||||
if (typeof rc === 'string') {
|
||||
accumulated = rc;
|
||||
}
|
||||
}
|
||||
if (!accumulated) {
|
||||
const rc = item?.choices?.[0]?.delta?.reasoning_content;
|
||||
if (typeof rc === 'string') accumulated = rc;
|
||||
}
|
||||
}
|
||||
|
||||
if (!accumulated) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (accumulated.startsWith(last)) {
|
||||
last = accumulated;
|
||||
} else {
|
||||
last += accumulated;
|
||||
}
|
||||
yield last;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[xbgen:stream] 流式错误:', err);
|
||||
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 { }
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
const payload = ChatCompletionService.createRequestData(body);
|
||||
const extracted = await ChatCompletionService.sendRequest(payload, false, abortSignal);
|
||||
|
||||
let result = '';
|
||||
if (extracted && typeof extracted === 'object') {
|
||||
const msg = extracted?.choices?.[0]?.message;
|
||||
result = String(
|
||||
msg?.content ??
|
||||
msg?.reasoning_content ??
|
||||
extracted?.choices?.[0]?.text ??
|
||||
extracted?.content ??
|
||||
extracted?.reasoning_content ??
|
||||
''
|
||||
);
|
||||
} else {
|
||||
result = String(extracted ?? '');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async _emitPromptReady(chatArray) {
|
||||
try {
|
||||
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);
|
||||
@@ -1184,7 +1171,7 @@ class StreamingGeneration {
|
||||
const m = messages[i];
|
||||
if (m.content === promptText &&
|
||||
((role !== 'system' && m.role === 'system') ||
|
||||
(role === 'system' && m.role === 'user'))) {
|
||||
(role === 'system' && m.role === 'user'))) {
|
||||
messages.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
@@ -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] },
|
||||
@@ -1355,7 +1342,7 @@ class StreamingGeneration {
|
||||
const sid = this._getSlotId(sessionId);
|
||||
const s = this.sessions.get(sid);
|
||||
return s ? { isStreaming: !!s.isStreaming, text: s.text, sessionId: sid }
|
||||
: { isStreaming: false, text: '', sessionId: sid };
|
||||
: { isStreaming: false, text: '', sessionId: sid };
|
||||
}
|
||||
return { isStreaming: !!this.isStreaming, text: this.tempreply };
|
||||
};
|
||||
@@ -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