32 Commits

Author SHA1 Message Date
b77122d025 更新版本信息为1.8.4-a 2026-01-20 12:52:34 +08:00
60befb69ea 页面调整 2026-01-20 12:00:28 +08:00
ab161e475d 添加假流功能的设置选项和事件绑定 2026-01-20 11:56:58 +08:00
859588461d fix 2026-01-20 11:36:33 +08:00
3043c3d80a rollback 2026-01-20 11:32:54 +08:00
40c3d6c735 fix 2026-01-20 11:21:14 +08:00
91e85aec94 fix 2026-01-20 10:58:41 +08:00
70224a6b61 fix 2026-01-20 10:45:41 +08:00
53ff9bd307 fix 2026-01-20 10:36:44 +08:00
4c61837b93 优化 _buildProfilePayload 方法,全面继承 Profile 属性并规范化关键字段,确保参数完整性和兼容性 2026-01-20 10:30:31 +08:00
1136707f1f 重构参数合并逻辑,封装 Profile 参数提取至 _buildProfilePayload 方法,确保后端正确应用预设配置 2026-01-20 10:20:28 +08:00
3ba4e39193 修复预设模式下的参数合并逻辑,确保后端正确应用配置,优化请求有效性 2026-01-20 10:17:15 +08:00
8f2b91c36c 优化 callNccsAI 函数,增强配置管理和兼容性,自动收集额外参数,改进日志记录 2026-01-20 10:03:34 +08:00
56017782bb 重构项目架构,添加 Nccs API 设置,优化流式支持逻辑 2026-01-20 09:51:21 +08:00
398649c754 增强 NccsApi 和 Amily2Bus 的注册逻辑,添加 FakeStream 设置选项,优化初始化流程 2026-01-18 23:01:34 +08:00
dedc4418c7 fix 2026-01-18 12:42:38 +08:00
a5f3c92fa3 优化 ModelCaller 的日志记录和流式处理逻辑,增强 SSE 解析能力 2026-01-18 12:38:37 +08:00
3d16bb20bb 移除 Logger 类中 process 方法的调试输出,使用注释替代 2026-01-18 11:59:48 +08:00
33f09a615e 修复 ModelCaller 中的导入路径,确保正确引用 amilyHelper 2026-01-18 11:42:13 +08:00
d405f1d624 添加 ModelCaller 和 Options 类,重构 Logger,增强 Amily2Bus 架构,更新 README 和 TODO 文件 2026-01-18 11:40:35 +08:00
4e9f2defb7 Merge branch 'LICENCE' into SL-Dev-2601 2026-01-18 10:14:57 +08:00
4895a259e6 重调整项目目录结构 2026-01-18 04:41:19 +08:00
6ab18f545e 重调整项目目录结构 2026-01-18 04:34:21 +08:00
9625974d33 重调整项目目录结构 2026-01-18 04:31:28 +08:00
256b295739 增强日志记录功能,添加安全控制台以避免劫持,并在日志处理时输出调试信息 2026-01-16 15:56:20 +08:00
b9df92435a Debug 2026-01-16 11:03:39 +08:00
c56780570a little fix... 2026-01-16 10:39:06 +08:00
f9b2f35828 初步实现Logger功能 2026-01-16 10:24:48 +08:00
cb54fc3eb4 little fix... 2026-01-16 10:06:47 +08:00
3d051d69a1 AmilyBus注入初始化 2026-01-16 00:25:41 +08:00
e370ce7d3f 进行解耦合功能拆分处理 2026-01-15 23:22:55 +08:00
46fea93115 总线开发工程-init 2026-01-15 17:41:51 +08:00
25 changed files with 5106 additions and 3906 deletions

180
SL/bus/Amily2Bus.js Normal file
View File

@@ -0,0 +1,180 @@
import Logger from './log/Logger.js';
import FilePipe from './file/FilePipe.js';
import ModelCaller from './api/ModelCaller.js';
import Options from './api/Options.js';
// 【逃生通道】创建一个纯净的 Console 对象,绕过任何潜在的劫持
const getSafeConsole = () => {
try {
if (window._amilySafeConsole) return window._amilySafeConsole;
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const safe = iframe.contentWindow.console;
// document.body.removeChild(iframe); // 保持 iframe 以维持 console 引用有效
window._amilySafeConsole = safe;
return safe;
} catch (e) {
return window.console; // Fallback
}
};
class Amily2Bus {
constructor() {
this.safeConsole = getSafeConsole();
// 1. 初始化 Logger
/** @type {Logger} */
this.Logger = new Logger(this.safeConsole);
/** @type {FilePipe} */
this.FilePipe = new FilePipe();
// 2. 依赖注入 (Dependency Injection)
// 创建一个 Logger 代理适配器传给 ModelCaller
const loggerDelegate = {
log: (type, message, origin, plugin) => {
// 回调 Bus 的 Logger 实例
this.Logger.process(plugin || 'Global', origin || 'Model', type, message);
}
};
// ModelCaller 不再包含 Bus只包含 logger 代理
/** @type {ModelCaller} */
this.ModelCaller = new ModelCaller(loggerDelegate);
// 存储上下文引用(严格锁:每个插件名仅限一次成功注册)
this.registry = new Map();
// 存储公开的联动接口(联动模块)
this.publicRegistry = new Map();
this.safeConsole.log('[Amily2Bus] Core Initialized (Decoupled Architecture).');
// 3. 自动注册并锁定 PUBLIC 命名空间
this._initPublicNamespace();
this.register('Amily2');
}
/**
* 初始化系统保留的 PUBLIC 模块
* 用于提供系统级信息的联动查询,防止 PUBLIC 命名被滥用
*/
_initPublicNamespace() {
try {
// 这里利用 register 的机制,直接抢占 'PUBLIC' 并加上严格锁
const sysCtx = this.register('PUBLIC');
// 暴露系统级能力给 query('PUBLIC')
sysCtx.expose({
description: 'Amily2 System Public Interface',
version: '2.0.0-Core',
// 允许查询当前有哪些插件暴露了公共接口
getAvailableModules: () => {
return Array.from(this.publicRegistry.keys());
},
// 允许查询当前所有已注册(被锁定的)插件名
getRegisteredPlugins: () => {
return Array.from(this.registry.keys());
},
// 简单的系统状态检查
ping: () => 'pong'
});
// 内部记录一条初始化日志
sysCtx.log('System', 'info', 'PUBLIC namespace reserved and strictly locked.');
} catch (e) {
this.safeConsole.error('[Amily2Bus] CRITICAL: Failed to init PUBLIC namespace.', e);
}
}
/**
* 直接记录系统级日志 (Global Scope)
* 支持手动指定来源,方便终端调试或非插件环境调用
* @param {string} type 日志级别 (debug, info, warn, error)
* @param {string} message 消息内容
* @param {string} [origin='Bus' 来源模块,默认为 'Bus'
* @param {string} [plugin='Global'] 来源插件/命名空间,调试时可指定如 'Console'
*/
log(type, message, origin = 'Bus', plugin = 'Global') {
if (this.Logger) {
this.Logger.process(plugin, origin, type, message);
}
}
/**
* 注册插件并获取专属上下文 (严格锁机制)
* @param {string} pluginName 插件名称
* @returns {Object} 包含该插件专属 API 的上下文对象
*/
register(pluginName) {
if (!pluginName || typeof pluginName !== 'string') {
throw new Error('[Amily2Bus] Invalid plugin name.');
}
if (this.registry.has(pluginName)) {
const errorMsg = `[Amily2Bus] Security Error: Plugin '${pluginName}' is already registered and locked.`;
this.safeConsole.error(errorMsg);
throw new Error(errorMsg);
}
// 返回该插件专属的 API 上下文 (Capability Token)
const context = {
// 1. 日志能力 (绑定了身份的日志接口)
log: (origin, type, message) => this.Logger.log(pluginName, origin, type, message),
// 2. 文件能力 (绑定了身份的文件接口)
file: {
read: (path) => {
return this.FilePipe ? this.FilePipe.read(pluginName, path) : null;
},
write: (path, data) => {
return this.FilePipe ? this.FilePipe.write(pluginName, path, data) : false;
}
},
// 3. 网络能力 (ModelCaller)
model: {
// 暴露 Options 类,方便插件直接 new context.model.Options() 或使用 builder
Options: Options,
// 插件调用时Bus 负责将 pluginName 传给无状态的 ModelCaller
call: (messages, options) => this.ModelCaller.call(pluginName, messages, options)
},
/**
* 将本插件的能力暴露给公共查询池,实现插件间联动
* @param {Object} apiMethods
*/
expose: (apiMethods) => {
if (typeof apiMethods !== 'object') throw new Error('Exposed API must be an object');
this.publicRegistry.set(pluginName, Object.freeze(apiMethods));
this.log('info', `Module exposed to public registry.`, 'Bus', pluginName);
}
};
this.registry.set(pluginName, context);
this.safeConsole.log(`[Amily2Bus] Plugin registered: ${pluginName}`);
return context;
}
/**
* 联动查询:获取其他插件通过 expose 暴露的能力
* @param {string} pluginName 目标插件名称
* @returns {Object|null}
*/
query(pluginName) {
return this.publicRegistry.get(pluginName) || null;
}
}
// 挂载全局单例
if (!window.Amily2Bus) {
window.Amily2Bus = new Amily2Bus();
}
export function initializeAmilyBus() {
if (!(window.Amily2Bus instanceof Amily2Bus)) {
window.Amily2Bus = new Amily2Bus();
console.log('[Amily2] Amily2Bus 已成功初始化并附加到 window 对象');
}
}

111
SL/bus/README.md Normal file
View File

@@ -0,0 +1,111 @@
# Amily2Bus (Amily2 总线系统)
Amily2Bus 是 Amily2-Chat-Optimisation 插件系统的核心基础设施。它为所有子模块和外部插件提供了一个规范化、安全且高兼容性的运行环境。
## 核心特性
- **安全控制台 (SafeConsole)**: 通过 Iframe 逃生通道获取纯净 Console 引用,绕过 SillyTavern 等环境的日志劫持。
- **能力令牌 (Capability Token)**: 插件注册后获取专属上下文,自动绑定身份,实现日志追踪与文件隔离。
- **防超时网络层 (FakeStream)**: `ModelCaller` 支持伪流式聚合,通过持续保持 TCP 连接活跃,彻底解决 CloudFlare 524 超时问题。
- **位运算日志系统**: 基于位掩码的日志级别控制,支持针对特定插件或模块动态调整输出粒度。
- **异步责任链**: 预置 `Chain` 模块,支持插件化的异步中间件处理流程。
---
## 快速开始
### 1. 初始化
总线通常在系统启动时自动挂载到 `window.Amily2Bus`
```javascript
import { initializeAmilyBus } from './SL/bus/Amily2Bus.js';
initializeAmilyBus();
```
### 2. 插件注册
所有插件必须注册以获取专属上下文:
```javascript
const amily = window.Amily2Bus.register('MyAwesomePlugin');
```
---
## 模块说明
### 1. 标准日志 (Logger)
支持 `debug`, `info`, `warn`, `error` 四个级别。
```javascript
// 自动绑定插件名,输出格式: [时间] [MyAwesomePlugin::Main] [INFO]: 消息内容
amily.log('Main', 'info', '插件已就绪');
```
### 2. 网络请求 (ModelCaller & Options)
统一处理 API 调用,支持自动切换 ST 配置文件 (Profile) 及防超时处理。
```javascript
const { Options } = amily.model;
const opt = Options.builder()
.setMode('direct') // 'direct' (直连) 或 'preset' (ST预设)
.setFakeStream(true) // 开启伪流式聚合,防止 524 超时
.setApiUrl('...')
.setApiKey('...')
.setModel('gpt-4o')
.build();
const result = await amily.model.call(messages, opt);
```
### 3. 文件操作 (FilePipe)
提供基于插件命名的虚拟文件系统隔离,防止插件间非法访问。
```javascript
// 写入文件 (自动定位到 /virtual_fs/MyAwesomePlugin/config.json)
await amily.file.write('config.json', { theme: 'dark' });
// 读取文件
const data = await amily.file.read('config.json');
```
### 4. 责任链 (Chain)
用于处理复杂的、可扩展的异步逻辑流。
```javascript
import { Chain } from './SL/bus/chain/Chain.js';
const pipeline = new Chain();
pipeline.use(async (ctx, next) => {
ctx.data += " -> 步骤1处理";
await next();
});
const context = { data: "开始" };
await pipeline.execute(context);
```
---
## 目录结构
- `Amily2Bus.js`: 总线入口,协调各模块。
- `log/Logger.js`: 位运算日志管理器。
- `file/FilePipe.js`: 安全文件操作管道。
- `api/ModelCaller.js`: 核心 API 调用器。
- `api/Options.js`: API 请求配置构建器。
- `chain/Chain.js`: 异步责任链工具。
---
## 开发规范
1. **强制类型**: `model.call` 必须接收 `Options` 类的实例,建议始终使用 `Options.builder()` 构建参数。
2. **路径安全**: 使用 `file` 接口时,禁止在路径中使用 `..` 等跳转符,系统会自动进行安全校验。
3. **日志分级**: 生产环境默认屏蔽 `debug` 级别,调试时可通过 `window.Amily2Bus.Logger.setLevel('PluginName', 'all')` 动态开启。

323
SL/bus/api/ModelCaller.js Normal file
View File

@@ -0,0 +1,323 @@
import { getRequestHeaders } from "/script.js";
import { getContext, extension_settings } from "/scripts/extensions.js";
import { amilyHelper } from '../../../core/tavern-helper/main.js';
import Options from './Options.js';
import RequestBody from './RequestBody.js';
/**
* ModelCaller Service
* 负责执行 API 调用逻辑,旨在替换 NccsApi 及其他散乱的请求逻辑。
* 支持标准直连、ST预设调用、伪流式聚合(防超时)、数据归一化。
*/
export default class ModelCaller {
/**
* 构造函数注入 Logger 依赖
* @param {Object} loggerDelegate - { log: (level, msg, origin, plugin) => void }
*/
constructor(loggerDelegate) {
/** @type {Object} */
this.logger = loggerDelegate;
this.defaultHeaders = {
'Content-Type': 'application/json'
};
}
/**
* 统一调用入口
* @param {string} callerName - 调用者名称(日志用)
* @param {Array} messages - 聊天消息历史
* @param {Options} options - 配置对象实例
* @returns {Promise<string>} - 返回归一化后的文本内容
*/
async call(callerName, messages, options) {
// 1. 强制类型检查
if (!(options instanceof Options)) {
const errorMsg = `[ModelCaller] Options must be instance of Options class.`;
throw new TypeError(errorMsg);
}
// 2. 逻辑中直接使用 options 属性
// 记录一下当前的流模式,方便调试
this._log('info', `API Request [${options.mode}] StreamMode: ${options.fakeStream}`, callerName);
try {
// 统一构建请求体 DTO
const requestBody = new RequestBody(messages, options);
let result;
if (options.mode === 'preset') {
result = await this._callPreset(callerName, requestBody, options);
} else {
result = await this._callDirect(callerName, requestBody, options);
}
// 如果是流式返回result 已经是拼接好的字符串,不需要 normalize 的部分逻辑
// 但为了统一,我们还是传进去检查一下
return this._normalize(result, options.fakeStream);
} catch (error) {
this._log('error', `Request Failed: ${error.message}`, callerName);
throw error;
}
}
// 内部日志封装
_log(level, msg, plugin) {
if (this.logger?.log) {
this.logger.log(level, msg, 'ModelCaller', plugin);
}
}
// ========================================================================
// 模式一Direct (标准直连)
// 对应 NccsApi 中的 callNccsOpenAITest
// ========================================================================
async _callDirect(callerName, requestBody, options) {
// 构建标准 OpenAI 兼容 Body
// 目标通常是 ST 的后端代理接口
const url = '/api/backends/chat-completions/generate';
const payload = requestBody.toPayload(); // 使用 DTO 生成数据
const fetchOpts = {
method: 'POST',
headers: { ...getRequestHeaders(), ...this.defaultHeaders },
body: JSON.stringify(payload)
};
return options.fakeStream
? this._fetchFakeStream(url, fetchOpts)
: this._fetchStandard(url, fetchOpts);
}
// ========================================================================
// 模式二Preset (ST预设调用)
// 对应 NccsApi 中的 callNccsSillyTavernPreset
// ========================================================================
async _callPreset(callerName, requestBody, options) {
const context = getContext();
// 1. 记录并切换 Profile
const originalProfile = await amilyHelper.triggerSlash('/profile');
const targetProfile = context.extensionSettings?.connectionManager?.profiles?.find(p => p.id === options.presetId);
if (!targetProfile) throw new Error(`Preset ID ${options.presetId} not found`);
if (originalProfile !== targetProfile.name) {
this._log('info', `Switching profile: ${originalProfile} -> ${targetProfile.name}`, callerName);
const escapedName = targetProfile.name.replace(/"/g, '\\"');
await amilyHelper.triggerSlash(`/profile await=true "${escapedName}"`);
}
try {
// 2. 根据流式需求分流处理
if (options.fakeStream) {
// 【流式预设请求】
// 难点ST 的 ConnectionManagerRequestService 不暴露流。
// 策略:切换 Profile 后,手动向生成接口发送请求。
const url = '/api/backends/chat-completions/generate';
// [修复]: 手动合并 Profile 中的关键参数,否则后端不会自动应用预设配置
// 提取逻辑已封装至 _buildProfilePayload
const profilePayload = this._buildProfilePayload(targetProfile);
// 合并顺序基础Payload(msg) < Profile预设 < 显式Params覆盖
// toMinimalPayload 包含: messages, stream, max_tokens, ...params
const minimal = requestBody.toMinimalPayload();
const finalPayload = {
...profilePayload,
...minimal,
...options.params
};
const fetchOpts = {
method: 'POST',
headers: { ...getRequestHeaders(), ...this.defaultHeaders },
body: JSON.stringify(finalPayload)
};
return await this._fetchFakeStream(url, fetchOpts);
} else {
// 【非流式预设请求】
// 直接使用 ST 原生服务,最稳妥
if (!context.ConnectionManagerRequestService) throw new Error('ST Request Service unavailable');
return await context.ConnectionManagerRequestService.sendRequest(
targetProfile.id,
requestBody.messages,
options.maxTokens
);
}
} finally {
// 3. 恢复 Profile
if (originalProfile) {
try {
const current = await amilyHelper.triggerSlash('/profile');
if (originalProfile !== current) {
const escapedOriginal = originalProfile.replace(/"/g, '\\"');
await amilyHelper.triggerSlash(`/profile await=true "${escapedOriginal}"`);
}
} catch (e) {
this._log('warn', `Failed to restore profile: ${e.message}`, callerName);
}
}
}
}
// ========================================================================
// 网络层核心
// ========================================================================
async _fetchStandard(url, opts) {
const res = await fetch(url, opts);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
}
// 【核心升级】:支持 SSE 解析的伪流式聚合,防 CloudFlare 超时
async _fetchFakeStream(url, opts) {
const res = await fetch(url, opts);
if (!res.ok) throw new Error(`Stream HTTP ${res.status}`);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let fullContent = ""; // 用于存储最终拼接的纯文本
let buffer = ""; // 用于存储未处理完的数据片段
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
// 1. 解码当前数据包
const chunk = decoder.decode(value, { stream: true });
buffer += chunk;
// 2. 处理 SSE 格式 (data: {...})
// 以双换行符分割每一条 SSE 消息
const lines = buffer.split('\n');
// 保留最后一个可能不完整的片段在 buffer 中
buffer = lines.pop();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed === 'data: [DONE]') continue;
if (trimmed.startsWith('data: ')) {
try {
const jsonStr = trimmed.substring(6); // 去掉 'data: '
const json = JSON.parse(jsonStr);
// 提取 delta content
const delta = json.choices?.[0]?.delta?.content;
if (delta) {
fullContent += delta;
}
} catch (e) {
// 忽略解析错误的行,防止因为个别丢包导致整个请求失败
console.warn('[ModelCaller] SSE Parse Error:', e);
}
}
}
}
} finally {
reader.releaseLock();
}
// 如果 fullContent 是空的,说明可能服务端根本没返回 SSE 格式,而是直接返回了纯文本或 JSON
// 这种情况下尝试降级处理
if (!fullContent && buffer) {
try {
const json = JSON.parse(buffer);
return json; // 是标准 JSON
} catch {
return buffer; // 是纯文本
}
}
return fullContent;
}
// ========================================================================
// 数据归一化
// ========================================================================
_normalize(data, isFromStream = false) {
// 如果是从流式聚合来的,它已经是一个纯字符串了,直接返回
if (isFromStream && typeof data === 'string') {
return data;
}
// 如果是 JSON 字符串则解析
if (typeof data === 'string') {
try { data = JSON.parse(data); } catch (e) { return data; }
}
// 处理 OpenAI 格式
if (data?.choices?.[0]?.message?.content) {
return data.choices[0].message.content.trim();
}
// 处理常规 content 格式
if (data?.content) {
return data.content.trim();
}
// Fallback
return typeof data === 'object' ? JSON.stringify(data) : data;
}
/**
* 辅助方法:从 Profile 对象中提取标准生成参数
* 严格复刻 SillyTavern 原始 Payload 逻辑
*/
_buildProfilePayload(targetProfile) {
const context = getContext();
// 1. 基础克隆
const payload = { ...targetProfile };
// 2. 注入运行时元数据 (这是旧版能通的关键,包含用户/角色名等)
payload.user_name = context.name1 || 'User';
payload.char_name = context.name2 || 'AI';
payload.group_names = []; // 暂不处理群组
payload.use_sysprompt = true;
payload.type = 'quiet';
payload.custom_prompt_post_processing = payload.custom_prompt_post_processing || 'strict';
// 3. 规范化模型字段
if (!payload.model) {
payload.model = payload.openai_model || payload.claude_model || payload.mistral_model || '';
}
// 4. 精准对齐 URL 映射 (解决 403/400 错误的核心)
const rawUrl = payload['api-url'] || payload['api_url'] || payload.custom_url || payload.url;
if (rawUrl) {
// 如果 Source 是 custom严格遵循旧版custom_url 有值reverse_proxy 为空
if (payload.chat_completion_source === 'custom') {
payload.custom_url = rawUrl;
payload.reverse_proxy = payload.reverse_proxy || '';
} else {
// 如果是 openai则填充 reverse_proxy
payload.reverse_proxy = rawUrl;
payload.custom_url = rawUrl;
}
// 兼容性修补
payload.zai_endpoint = rawUrl;
payload.vertexai_region = rawUrl;
}
// 5. 补全采样参数 (严格对齐 UI 当前状态)
const globalGenSettings = extension_settings.text_generation || {};
const fields = ['temperature', 'max_tokens', 'top_p', 'top_k', 'min_p', 'frequency_penalty', 'presence_penalty', 'repetition_penalty'];
fields.forEach(field => {
if (payload[field] === undefined) {
payload[field] = globalGenSettings[field] ?? (field === 'temperature' ? 1 : 0);
}
});
// 6. 确保 Source 存在且不被错误覆盖
if (!payload.chat_completion_source) {
payload.chat_completion_source = 'openai';
}
return payload;
}
}

98
SL/bus/api/Options.js Normal file
View File

@@ -0,0 +1,98 @@
/**
* ModelCaller 请求配置类
* 支持构造函数直接传入对象,或使用 Builder 链式调用
*/
export class Options {
constructor(config = {}) {
/** @type {'direct'|'preset'} */
this.mode = config.mode || 'direct';
/** @type {boolean} */
this.fakeStream = config.fakeStream ?? false;
/** @type {string} */
this.apiUrl = config.apiUrl || '';
/** @type {string} */
this.apiKey = config.apiKey || '';
/** @type {string} */
this.model = config.model || '';
/** @type {string} */
this.presetId = config.presetId || '';
/** @type {number} */
this.maxTokens = config.maxTokens || 4000;
/** @type {number} */
this.temperature = config.temperature || 0.7;
/** @type {Object} 额外透传参数 */
this.params = config.params || {};
}
/**
* 获取 Builder 实例
* @returns {OptionsBuilder}
*/
static builder() {
return new OptionsBuilder();
}
}
/**
* Options 构建器类
*/
class OptionsBuilder {
constructor() {
this.config = {};
}
setMode(mode) {
this.config.mode = mode;
return this;
}
setFakeStream(enabled) {
this.config.fakeStream = enabled;
return this;
}
setApiUrl(url) {
this.config.apiUrl = url;
return this;
}
setApiKey(key) {
this.config.apiKey = key;
return this;
}
setModel(model) {
this.config.model = model;
return this;
}
setPresetId(id) {
this.config.presetId = id;
return this;
}
setMaxTokens(tokens) {
this.config.maxTokens = tokens;
return this;
}
setTemperature(temp) {
this.config.temperature = temp;
return this;
}
setParams(params) {
this.config.params = { ...(this.config.params || {}), ...params };
return this;
}
/**
* 构建最终的 Options 对象
* @returns {Options}
*/
build() {
return new Options(this.config);
}
}
export default Options;

74
SL/bus/api/RequestBody.js Normal file
View File

@@ -0,0 +1,74 @@
import Options from './Options.js';
/**
* RequestBody (DTO)
* 严格约束发送给 LLM/ST 后端的请求体结构
* 类似于 Java 中的 RequestBean
*/
export class RequestBody {
/**
* @param {Array} messages
* @param {Options} options
*/
constructor(messages, options) {
if (!Array.isArray(messages)) throw new TypeError('messages must be an array');
if (!(options instanceof Options)) throw new TypeError('options must be an instance of Options');
this.messages = messages;
this.options = options;
}
/**
* 构建标准 OpenAI 兼容的 Payload
* @returns {Object} 纯净的 JSON 对象
*/
toPayload() {
const { apiUrl, apiKey, model, maxTokens, temperature, params, fakeStream } = this.options;
const isGoogle = apiUrl && apiUrl.includes('googleapis.com');
// 基础字段 (Base Fields)
const payload = {
chat_completion_source: 'openai',
messages: this.messages,
model: model,
reverse_proxy: apiUrl,
proxy_password: apiKey,
// 【核心修正】: 如果客户端开启防超时聚合(fakeStream)
// 必须告诉服务端开启流式传输,否则服务端不会分块发送数据。
stream: fakeStream,
max_tokens: maxTokens,
temperature: temperature,
// 允许 Options 中的 params 覆盖上述字段
...params
};
// 平台特定字段处理 (Platform Specific Logic)
if (!isGoogle) {
Object.assign(payload, {
custom_prompt_post_processing: 'strict',
presence_penalty: 0.12,
include_reasoning: false,
reasoning_effort: 'medium'
});
}
return payload;
}
/**
* 仅获取消息体的 Payload (用于 Preset 模式)
*/
toMinimalPayload() {
return {
messages: this.messages,
// 同样需要联动
stream: this.options.fakeStream,
max_tokens: this.options.maxTokens,
...this.options.params
};
}
}
export default RequestBody;

56
SL/bus/chain/Chain.js Normal file
View File

@@ -0,0 +1,56 @@
/**
* 通用责任链/中间件管理器
* 用于规范操作顺序,支持异步流程控制
*/
export class Chain {
constructor() {
this.middlewares = [];
}
/**
* 注册中间件
* @param {Function} fn (context, next) => Promise<void> | void
*/
use(fn) {
if (typeof fn !== 'function') {
throw new Error('[Chain] Middleware must be a function');
}
this.middlewares.push(fn);
return this;
}
/**
* 执行责任链
* @param {Object} context 传递给中间件的上下文对象
*/
async execute(context = {}) {
let index = -1;
const dispatch = async (i) => {
if (i <= index) {
throw new Error('[Chain] next() called multiple times in one middleware');
}
index = i;
const fn = this.middlewares[i];
if (!fn) return; // 链结束
try {
// 执行中间件,传入 context 和 next 函数
await fn(context, () => dispatch(i + 1));
} catch (err) {
console.error('[Chain] Middleware execution error:', err);
throw err;
}
};
await dispatch(0);
}
/**
* 清空链
*/
clear() {
this.middlewares = [];
}
}

61
SL/bus/file/FilePipe.js Normal file
View File

@@ -0,0 +1,61 @@
class FilePipe {
constructor() {
this.name = "FilePipe";
// 模拟的根存储路径,实际环境可能对应 IndexedDB 的 Store Name 或 Node 的 baseDir
this.basePath = "/virtual_fs/";
}
/**
* 安全路径解析与校验
* @param {string} plugin 插件名称(命名空间)
* @param {string} relativePath 相对路径
* @returns {string|null} 合法的绝对路径,如果违规则返回 null
*/
_resolvePath(plugin, relativePath) {
if (!plugin || typeof plugin !== 'string') {
console.error(`[FilePipe] Security Error: Invalid plugin identity.`);
return null;
}
// 简单防越权:禁止包含 ".."
if (relativePath.includes('..')) {
console.error(`[FilePipe] Security Error: Directory traversal attempt blocked for plugin '${plugin}'. Path: ${relativePath}`);
return null;
}
// 强制限定在插件目录下
// 格式: /virtual_fs/PluginName/filename
return `${this.basePath}${plugin}/${relativePath}`;
}
/**
* 读取文件
* @param {string} plugin 调用方插件名
* @param {string} path 文件相对路径
*/
async read(plugin, path) {
const safePath = this._resolvePath(plugin, path);
if (!safePath) return null;
console.log(`[FilePipe] Reading from: ${safePath}`);
// TODO: Implement actual file reading logic
return null;
}
/**
* 写入文件
* @param {string} plugin 调用方插件名
* @param {string} path 文件相对路径
* @param {any} data 数据
*/
async write(plugin, path, data) {
const safePath = this._resolvePath(plugin, path);
if (!safePath) return false;
console.log(`[FilePipe] Writing to: ${safePath}`);
// TODO: Implement actual file writing logic
return true;
}
}
export default FilePipe;

220
SL/bus/log/Logger.js Normal file
View File

@@ -0,0 +1,220 @@
/**
* 日志总类,用于记录日志信息
* 支持基于位运算的自定义日志级别控制
*/
class Logger {
static LOG_HEADER_DEBUG = '[DEBUG]';
static LOG_HEADER_INFO = '[INFO]';
static LOG_HEADER_WARN = '[WARN]';
static LOG_HEADER_ERROR = '[ERROR]';
static LOG_LEVEL_CODE = {
none: 0x0, // 0
debug: 0x1, // 1
info: 0x2, // 2
warn: 0x4, // 4
error: 0x8, // 8
all: 0xF // 15
};
constructor(safeConsole = null) {
// 使用传入的 safeConsole如果没有则回退到全局 console
this.safeConsole = safeConsole || (typeof window !== 'undefined' ? window.console : console);
// 全局默认级别 (默认开启 info, warn, error)
this.globalLevel = Logger.LOG_LEVEL_CODE.info | Logger.LOG_LEVEL_CODE.warn | Logger.LOG_LEVEL_CODE.error;
// 针对特定插件或模块的配置
// 结构示例:
// {
// "PluginA": 3, // PluginA 下所有模块掩码为 3 (debug | info)
// "PluginB::ModuleX": 8 // 仅 PluginB 下的 ModuleX 掩码为 8 (error)
// }
this.levelConfig = {};
}
/**
* 将输入转换为对应的日志级别掩码
* @param {number|string|string[]} levelInput
* @returns {number} 掩码
*/
_parseLevelInput(levelInput) {
if (typeof levelInput === 'number') {
return levelInput;
}
if (typeof levelInput === 'string') {
if (Logger.LOG_LEVEL_CODE.hasOwnProperty(levelInput)) {
return Logger.LOG_LEVEL_CODE[levelInput];
}
// 支持 "debug|info" 这种写法
if (levelInput.includes('|')) {
return levelInput.split('|').reduce((mask, l) => mask | (Logger.LOG_LEVEL_CODE[l.trim()] || 0), 0);
}
this.safeConsole.warn(`[Logger] Unknown log level string: ${levelInput}`);
return 0;
}
if (Array.isArray(levelInput)) {
return levelInput.reduce((mask, l) => mask | (Logger.LOG_LEVEL_CODE[l] || 0), 0);
}
return 0;
}
/**
* 设置日志级别(覆盖模式)
* @param {string} target 目标范围,可以是 'Global'、'PluginName' 或 'PluginName::ModuleName'
* @param {number|string|string[]} level 输入的级别配置
*/
setLevel(target, level) {
const mask = this._parseLevelInput(level);
if (target === 'Global') {
this.globalLevel = mask;
this.safeConsole.log(`[Logger] Global log level mask set to: ${mask.toString(2)}`);
} else {
this.levelConfig[target] = mask;
this.safeConsole.log(`[Logger] Log level mask for '${target}' set to: ${mask.toString(2)}`);
}
}
/**
* 添加日志级别(增量模式)
* @param {string} target
* @param {number|string|string[]} level
*/
addLevel(target, level) {
const maskToAdd = this._parseLevelInput(level);
let currentMask;
if (target === 'Global') {
currentMask = this.globalLevel;
this.globalLevel = currentMask | maskToAdd;
this.safeConsole.log(`[Logger] Added level to Global. New mask: ${this.globalLevel.toString(2)}`);
} else {
currentMask = this.levelConfig[target] !== undefined ? this.levelConfig[target] : this.globalLevel;
this.levelConfig[target] = currentMask | maskToAdd;
this.safeConsole.log(`[Logger] Added level to '${target}'. New mask: ${this.levelConfig[target].toString(2)}`);
}
}
/**
* 移除日志级别(减量模式)
* @param {string} target
* @param {number|string|string[]} level
*/
removeLevel(target, level) {
const maskToRemove = this._parseLevelInput(level);
let currentMask;
if (target === 'Global') {
currentMask = this.globalLevel;
// 使用 & ~mask 实现移除
this.globalLevel = currentMask & ~maskToRemove;
this.safeConsole.log(`[Logger] Removed level from Global. New mask: ${this.globalLevel.toString(2)}`);
} else {
currentMask = this.levelConfig[target] !== undefined ? this.levelConfig[target] : this.globalLevel;
this.levelConfig[target] = currentMask & ~maskToRemove;
this.safeConsole.log(`[Logger] Removed level from '${target}'. New mask: ${this.levelConfig[target].toString(2)}`);
}
}
/**
* 获取指定上下文的生效日志级别掩码(级联查找)
* @param {string} plugin
* @param {string} origin (Module)
*/
_getEffectiveLevelMask(plugin, origin) {
// 1. 检查精确匹配 "Plugin::Module"
const specificKey = `${plugin}::${origin}`;
if (this.levelConfig.hasOwnProperty(specificKey)) {
return this.levelConfig[specificKey];
}
// 2. 检查插件级匹配 "Plugin"
if (this.levelConfig.hasOwnProperty(plugin)) {
return this.levelConfig[plugin];
}
// 3. 返回全局默认
return this.globalLevel;
}
/**
* 标准日志处理方法 (Core Processing)
* 统一处理过滤、格式化和输出,支持默认归属 Global
*/
process(plugin, origin, type, message, inFile = false) {
// [DEBUG] 强制输出以确认方法被调用 (使用 error 级别防止被过滤)
// 【核心修改】:使用 safeConsole 替代全局 console
// this.safeConsole.error('[Logger DEBUG] Process called:', { plugin, origin, type, message });
// 1. 默认归属处理
const safePlugin = plugin || 'Global';
const safeOrigin = origin || 'System';
// 2. 获取当前上下文生效的日志级别掩码
const effectiveMask = this._getEffectiveLevelMask(safePlugin, safeOrigin);
// 3. 获取当前日志类型的位码
const typeCode = Logger.LOG_LEVEL_CODE[type];
// 4. 级别筛选位与运算结果为0则表示该级别未开启
if (typeCode === undefined || (effectiveMask & typeCode) === 0) {
return;
}
const timestamp = new Date().toLocaleTimeString();
// 格式: [12:00:00] [PluginName::ClassName] [INFO: message
const fullMessage = `[${timestamp}] [${safePlugin}::${safeOrigin}] [${type.toUpperCase()}]: ${message}`;
// 5. Console Output
// 【核心修改】:使用 safeConsole 替代全局 console
switch (type) {
case 'debug':
this.safeConsole.debug(fullMessage);
break;
case 'info':
this.safeConsole.info(fullMessage);
break;
case 'warn':
this.safeConsole.warn(fullMessage);
break;
case 'error':
this.safeConsole.error(fullMessage);
break;
default:
this.safeConsole.log(fullMessage);
break;
}
// 6. File Output (via FilePipe)
if (inFile) {
// Logger 自身也需要作为系统组件注册,获取写入权限
if (!this.sysBus) {
if (window.Amily2Bus && window.Amily2Bus.register) {
this.sysBus = window.Amily2Bus.register('SystemLogger');
}
}
if (this.sysBus && this.sysBus.file) {
// 使用注册后的安全接口写入,无需再手动传 'SystemLogger'
this.sysBus.file.write('runtime.log', fullMessage + '\n');
} else {
// Fallback: 如果总线未就绪,仅在控制台警告一次,避免死循环
if (!this._warned) {
this.safeConsole.warn('[Logger] FilePipe system not linked. Log not saved to file.');
this._warned = true;
}
}
}
}
log(plugin, origin, type, message, inFile = false) {
this.process(plugin, origin, type, message, inFile);
}
}
export default Logger;

14
TODO.md
View File

@@ -13,6 +13,10 @@
以下为待开发内容
- **项目框架重构 (Project Refactoring)**:
- 现状:大量功能模块(如 `NccsApi.js`)存在手动组装参数、逻辑耦合度高、代码风格不统一("能跑就行"遗留债)等问题。
- 目标:系统性重构项目架构,统一使用 Builder 模式(如 `Options.builder`),解耦业务逻辑与配置管理,提升代码可维护性和优雅度。
## 未修复
以下为示例(预计三个版本后移除)
@@ -31,3 +35,13 @@
- 添加了**TODO.md**,现在可以记录任务清单并更清楚的记录开发完成状态了。
- 无实际功能更新
### 中间版本未维护该文件
### 1.8.3
以下为修复内容
以下为更新内容:
- 添加记忆管理并发调用

View File

@@ -190,6 +190,11 @@
<input type="range" id="amily2_ngms_temperature" min="0" max="2" step="0.1" value="0.7" />
</div>
<div class="control-group" style="display: flex; align-items: center; gap: 10px;">
<label for="amily2_ngms_fakestream_enabled" style="margin-bottom: 0;">启用流式支持 (防超时)</label>
<input type="checkbox" id="amily2_ngms_fakestream_enabled" style="width: auto;" />
</div>
<!-- 测试按钮组 - 水平排列 -->
<div class="ngms-button-row" style="display: flex; gap: 10px; justify-content: center; margin-top: 15px;">
<button id="amily2_ngms_test_connection" class="menu_button primary small_button interactable">

View File

@@ -336,6 +336,11 @@
<input type="range" id="nccs-temperature" min="0" max="2" step="0.1" value="0.7">
</div>
<div class="amily2_opt_settings_block" style="margin-bottom: 10px;">
<label for="nccs-api-fakestream-enabled">启用流式支持: </label>
<input type="checkbox" id="nccs-api-fakestream-enabled" data-setting-key="nccsFakeStreamEnabled" data-type="boolean">
</div>
<div class="amily2_opt_settings_block" style="margin-bottom: 10px;">
<label for="nccs-sillytavern-preset">SillyTavern 预设</label>
<select id="nccs-sillytavern-preset" class="text_pole">

View File

@@ -12,89 +12,80 @@ try {
console.warn("[Amily2号-Nccs外交部] 未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。", e);
}
function normalizeApiResponse(responseData) {
let data = responseData;
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
console.error(`[${extensionName}] Nccs API响应JSON解析失败:`, e);
return { error: { message: 'Invalid JSON response' } };
}
let nccsCtx = null;
// 尝试连接总线
if (window.Amily2Bus) {
try {
// 注册 'NccsApi' 身份,获取专属上下文
nccsCtx = window.Amily2Bus.register('NccsApi');
// 【联动】暴露 Nccs 的核心调用能力,允许其他插件通过 query('NccsApi') 借用此通道
nccsCtx.expose({
call: callNccsAI,
getSettings: getNccsApiSettings
});
nccsCtx.log('Init', 'info', 'NccsApi 已连接至 Amily2Bus网络通道准备就绪。');
} catch (e) {
// 如果是热重载导致重复注册尝试降级获取注意严格锁模式下无法获取旧Context这里仅做日志提示
// 在生产环境中,页面刷新会重置 Bus不会有问题。
console.warn('[Amily2-Nccs] Bus 注册警告 (可能是热重载):', e);
}
if (data && typeof data.data === 'object' && data.data !== null && !Array.isArray(data.data)) {
if (Object.hasOwn(data.data, 'data')) {
data = data.data;
}
}
if (data && data.choices && data.choices[0]) {
return { content: data.choices[0].message?.content?.trim() };
}
if (data && data.content) {
return { content: data.content.trim() };
}
if (data && data.data) {
return { data: data.data };
}
if (data && data.error) {
return { error: data.error };
}
return data;
} else {
console.error('[Amily2-Nccs] 严重警告: Amily2Bus 未找到NccsApi 网络层将无法工作!');
toastr.error("核心组件 Amily2Bus 丢失,请检查安装。", "Nccs-System");
}
export function getNccsApiSettings() {
return {
nccsEnabled: extension_settings[extensionName]?.nccsEnabled || false,
apiMode: extension_settings[extensionName]?.nccsApiMode || 'openai_test',
apiUrl: extension_settings[extensionName]?.nccsApiUrl?.trim() || '',
apiKey: extension_settings[extensionName]?.nccsApiKey?.trim() || '',
model: extension_settings[extensionName]?.nccsModel || '',
maxTokens: extension_settings[extensionName]?.nccsMaxTokens || 4000,
temperature: extension_settings[extensionName]?.nccsTemperature || 0.7,
tavernProfile: extension_settings[extensionName]?.nccsTavernProfile || ''
tavernProfile: extension_settings[extensionName]?.nccsTavernProfile || '',
useFakeStream: extension_settings[extensionName]?.nccsFakeStreamEnabled || false
};
}
// =================================================================================================
// 核心调用入口 (Legacy First Mode)
// =================================================================================================
export async function callNccsAI(messages, options = {}) {
if (window.AMILY2_SYSTEM_PARALYZED === true) {
console.error("[Amily2-Nccs制裁] 系统完整性已受损,所有外交活动被无限期中止。");
return null;
}
const apiSettings = getNccsApiSettings();
const settings = getNccsApiSettings();
const finalOptions = {
maxTokens: apiSettings.maxTokens,
temperature: apiSettings.temperature,
model: apiSettings.model,
apiUrl: apiSettings.apiUrl,
apiKey: apiSettings.apiKey,
apiMode: apiSettings.apiMode,
tavernProfile: apiSettings.tavernProfile,
...settings,
...options
};
// 确保 stream 标志位存在
finalOptions.stream = finalOptions.useFakeStream ?? false;
if (finalOptions.apiMode !== 'sillytavern_preset') {
if (!finalOptions.apiUrl || !finalOptions.model || !finalOptions.apiKey) {
console.warn("[Amily2-Nccs外交部] API配置不完整无法调用AI");
toastr.error("API配置不完整请检查URL、Key和模型配置。", "Nccs-外交部");
return null;
}
} else {
// [限制] 预设模式暂不支持流式
if (finalOptions.stream) {
console.warn("[Amily2-Nccs] 预设模式目前尚不支持流式处理方案,已自动切换为标准模式。");
toastr.warning("SillyTavern预设模式目前暂不支持流式处理假流式已为您切换为标准请求模式。该功能将在后续版本中支持。", "Nccs-外交部");
finalOptions.stream = false;
}
}
console.groupCollapsed(`[Amily2号-Nccs统一API调用] ${new Date().toLocaleTimeString()}`);
console.log("【请求参数】:", {
mode: finalOptions.apiMode,
model: finalOptions.model,
maxTokens: finalOptions.maxTokens,
temperature: finalOptions.temperature,
messagesCount: messages.length
});
console.log("【消息内容】:", messages);
console.groupEnd();
try {
let responseContent;
switch (finalOptions.apiMode) {
case 'openai_test':
responseContent = await callNccsOpenAITest(messages, finalOptions);
@@ -103,53 +94,86 @@ export async function callNccsAI(messages, options = {}) {
responseContent = await callNccsSillyTavernPreset(messages, finalOptions);
break;
default:
console.error(`[Amily2-Nccs外交部] 未支持的API模式: ${finalOptions.apiMode}`);
console.error(`未支持的 API 模式: ${finalOptions.apiMode}`);
return null;
}
if (!responseContent) {
console.warn('[Amily2-Nccs外交部] 未能获取AI响应内容');
return null;
}
console.groupCollapsed("[Amily2号-Nccs AI回复]");
console.log(responseContent);
console.groupEnd();
return responseContent;
} catch (error) {
console.error(`[Amily2-Nccs外交部] API调用发生错误:`, error);
if (error.message.includes('400')) {
toastr.error(`API请求格式错误 (400): 请检查消息格式和模型配置`, "Nccs API调用失败");
} else if (error.message.includes('401')) {
toastr.error(`API认证失败 (401): 请检查API Key配置`, "Nccs API调用失败");
} else if (error.message.includes('403')) {
toastr.error(`API访问被拒绝 (403): 请检查权限设置`, "Nccs API调用失败");
} else if (error.message.includes('429')) {
toastr.error(`API调用频率超限 (429): 请稍后重试`, "Nccs API调用失败");
} else if (error.message.includes('500')) {
toastr.error(`API服务器错误 (500): 请稍后重试`, "Nccs API调用失败");
} else {
toastr.error(`API调用失败: ${error.message}`, "Nccs API调用失败");
}
console.error(`[Amily2-Nccs] API 调用失败:`, error);
toastr.error(`调用失败: ${error.message}`, "Nccs API Error");
return null;
}
}
async function fetchFakeStream(url, opts) {
const res = await fetch(url, opts);
if (!res.ok) throw new Error(`Stream HTTP ${res.status}: ${await res.text()}`);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let fullContent = "";
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed === 'data: [DONE]') continue;
if (trimmed.startsWith('data: ')) {
try {
const json = JSON.parse(trimmed.substring(6));
const delta = json.choices?.[0]?.delta?.content;
if (delta) fullContent += delta;
} catch (e) {
console.warn('[NccsApi] SSE Parse Error:', e);
}
}
}
}
} finally {
reader.releaseLock();
}
if (!fullContent && buffer) {
try {
const data = JSON.parse(buffer);
return data.choices?.[0]?.message?.content || data.content || buffer;
} catch { return buffer; }
}
return fullContent;
}
// =================================================================================================
// Legacy Implementations
// =================================================================================================
function normalizeApiResponse(responseData) {
let data = responseData;
if (typeof data === 'string') {
try { data = JSON.parse(data); } catch (e) { return data; }
}
if (data?.choices?.[0]?.message?.content) return data.choices[0].message.content.trim();
if (data?.content) return data.content.trim();
return typeof data === 'object' ? JSON.stringify(data) : data;
}
async function callNccsOpenAITest(messages, options) {
const isGoogleApi = options.apiUrl.includes('googleapis.com');
const body = {
chat_completion_source: 'openai',
messages: messages,
model: options.model,
reverse_proxy: options.apiUrl,
proxy_password: options.apiKey,
stream: false,
max_tokens: options.maxTokens || 30000,
stream: !!options.stream,
max_tokens: options.maxTokens || 4000,
temperature: options.temperature || 1,
top_p: options.top_p || 1,
};
@@ -157,104 +181,60 @@ async function callNccsOpenAITest(messages, options) {
if (!isGoogleApi) {
Object.assign(body, {
custom_prompt_post_processing: 'strict',
enable_web_search: false,
frequency_penalty: 0,
group_names: [],
include_reasoning: false,
presence_penalty: 0.12,
reasoning_effort: 'medium',
request_images: false,
});
}
const response = await fetch('/api/backends/chat-completions/generate', {
const fetchOpts = {
method: 'POST',
headers: { ...getRequestHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
};
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Nccs全兼容API请求失败: ${response.status} - ${errorText}`);
if (options.stream) {
return await fetchFakeStream('/api/backends/chat-completions/generate', fetchOpts);
}
const responseData = await response.json();
return responseData?.choices?.[0]?.message?.content;
const response = await fetch('/api/backends/chat-completions/generate', fetchOpts);
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
return normalizeApiResponse(await response.json());
}
async function callNccsSillyTavernPreset(messages, options) {
console.log('[Amily2号-NccsST预设] 使用SillyTavern预设调用');
const context = getContext();
if (!context) {
throw new Error('无法获取SillyTavern上下文');
}
if (!context) throw new Error('SillyTavern context unavailable');
const profileId = options.tavernProfile;
if (!profileId) {
throw new Error('未配置SillyTavern预设ID');
}
if (!profileId) throw new Error('No profile ID configured');
let originalProfile = '';
let responsePromise;
const originalProfile = await amilyHelper.triggerSlash('/profile');
const targetProfile = context.extensionSettings?.connectionManager?.profiles?.find(p => p.id === profileId);
if (!targetProfile) throw new Error(`Profile ${profileId} not found`);
try {
originalProfile = await amilyHelper.triggerSlash('/profile');
console.log(`[Amily2号-NccsST预设] 当前配置文件: ${originalProfile}`);
const targetProfile = context.extensionSettings?.connectionManager?.profiles?.find(p => p.id === profileId);
if (!targetProfile) {
throw new Error(`未找到配置文件ID: ${profileId}`);
if (originalProfile !== targetProfile.name) {
await amilyHelper.triggerSlash(`/profile await=true "${targetProfile.name.replace(/"/g, '\\"')}"`);
}
const targetProfileName = targetProfile.name;
console.log(`[Amily2号-NccsST预设] 目标配置文件: ${targetProfileName}`);
if (!context.ConnectionManagerRequestService) throw new Error('ConnectionManagerRequestService unavailable');
const currentProfile = await amilyHelper.triggerSlash('/profile');
if (currentProfile !== targetProfileName) {
console.log(`[Amily2号-NccsST预设] 切换配置文件: ${currentProfile} -> ${targetProfileName}`);
const escapedProfileName = targetProfileName.replace(/"/g, '\\"');
await amilyHelper.triggerSlash(`/profile await=true "${escapedProfileName}"`);
}
if (!context.ConnectionManagerRequestService) {
throw new Error('ConnectionManagerRequestService不可用');
}
console.log(`[Amily2号-NccsST预设] 通过配置文件 ${targetProfileName} 发送请求`);
responsePromise = context.ConnectionManagerRequestService.sendRequest(
const result = await context.ConnectionManagerRequestService.sendRequest(
targetProfile.id,
messages,
options.maxTokens || 4000
);
return normalizeApiResponse(result);
} finally {
try {
const currentProfileAfterCall = await amilyHelper.triggerSlash('/profile');
if (originalProfile && originalProfile !== currentProfileAfterCall) {
console.log(`[Amily2号-NccsST预设] 恢复原始配置文件: ${currentProfileAfterCall} -> ${originalProfile}`);
const escapedOriginalProfile = originalProfile.replace(/"/g, '\\"');
await amilyHelper.triggerSlash(`/profile await=true "${escapedOriginalProfile}"`);
}
} catch (restoreError) {
console.error('[Amily2号-NccsST预设] 恢复配置文件失败:', restoreError);
// Restore profile
const current = await amilyHelper.triggerSlash('/profile');
if (originalProfile && originalProfile !== current) {
await amilyHelper.triggerSlash(`/profile await=true "${originalProfile.replace(/"/g, '\\"')}"`);
}
}
const result = await responsePromise;
if (!result) {
throw new Error('未收到API响应');
}
const normalizedResult = normalizeApiResponse(result);
if (normalizedResult.error) {
throw new Error(normalizedResult.error.message || 'SillyTavern预设API调用失败');
}
return normalizedResult.content;
}
export async function fetchNccsModels() {
console.log('[Amily2号-Nccs外交部] 开始获取模型列表');
@@ -284,7 +264,6 @@ export async function fetchNccsModels() {
console.log('[Amily2号-Nccs外交部] SillyTavern预设模式获取到模型:', models);
return models;
} else {
if (!apiSettings.apiUrl || !apiSettings.apiKey) {
throw new Error('API URL或Key未配置');
@@ -383,3 +362,4 @@ export async function testNccsApiConnection() {
return false;
}
}

View File

@@ -50,7 +50,8 @@ export function getNgmsApiSettings() {
model: extension_settings[extensionName]?.ngmsModel || '',
maxTokens: extension_settings[extensionName]?.ngmsMaxTokens || 4000,
temperature: extension_settings[extensionName]?.ngmsTemperature || 0.7,
tavernProfile: extension_settings[extensionName]?.ngmsTavernProfile || ''
tavernProfile: extension_settings[extensionName]?.ngmsTavernProfile || '',
useFakeStream: extension_settings[extensionName]?.ngmsFakeStreamEnabled || false
};
}
@@ -73,12 +74,22 @@ export async function callNgmsAI(messages, options = {}) {
...options
};
// 确保 stream 标志位存在
finalOptions.stream = finalOptions.useFakeStream ?? apiSettings.useFakeStream ?? false;
if (finalOptions.apiMode !== 'sillytavern_preset') {
if (!finalOptions.apiUrl || !finalOptions.model || !finalOptions.apiKey) {
console.warn("[Amily2-Ngms外交部] API配置不完整无法调用AI");
toastr.error("API配置不完整请检查URL、Key和模型配置。", "Ngms-外交部");
return null;
}
} else {
// [限制] 预设模式暂不支持流式
if (finalOptions.stream) {
console.warn("[Amily2-Ngms] 预设模式目前尚不支持流式处理方案,已自动切换为标准模式。");
toastr.warning("SillyTavern预设模式目前暂不支持流式处理假流式已为您切换为标准请求模式。该功能将在后续版本中支持。", "Ngms-外交部");
finalOptions.stream = false;
}
}
console.groupCollapsed(`[Amily2号-Ngms统一API调用] ${new Date().toLocaleTimeString()}`);
@@ -87,6 +98,7 @@ export async function callNgmsAI(messages, options = {}) {
model: finalOptions.model,
maxTokens: finalOptions.maxTokens,
temperature: finalOptions.temperature,
stream: finalOptions.stream,
messagesCount: messages.length
});
console.log("【消息内容】:", messages);
@@ -139,6 +151,54 @@ export async function callNgmsAI(messages, options = {}) {
}
}
async function fetchFakeStream(url, opts) {
const res = await fetch(url, opts);
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Stream HTTP ${res.status}: ${errorText}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let fullContent = "";
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed === 'data: [DONE]') continue;
if (trimmed.startsWith('data: ')) {
try {
const json = JSON.parse(trimmed.substring(6));
const delta = json.choices?.[0]?.delta?.content;
if (delta) fullContent += delta;
} catch (e) {
console.warn('[NgmsApi] SSE Parse Error:', e);
}
}
}
}
} finally {
reader.releaseLock();
}
if (!fullContent && buffer) {
try {
const data = JSON.parse(buffer);
return data.choices?.[0]?.message?.content || data.content || buffer;
} catch { return buffer; }
}
return fullContent;
}
async function callNgmsOpenAITest(messages, options) {
const isGoogleApi = options.apiUrl.includes('googleapis.com');
@@ -148,7 +208,7 @@ async function callNgmsOpenAITest(messages, options) {
model: options.model,
reverse_proxy: options.apiUrl,
proxy_password: options.apiKey,
stream: false,
stream: !!options.stream,
max_tokens: options.maxTokens || 30000,
temperature: options.temperature || 1,
top_p: options.top_p || 1,
@@ -167,11 +227,17 @@ async function callNgmsOpenAITest(messages, options) {
});
}
const response = await fetch('/api/backends/chat-completions/generate', {
const fetchOpts = {
method: 'POST',
headers: { ...getRequestHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify(body)
});
};
if (options.stream) {
return await fetchFakeStream('/api/backends/chat-completions/generate', fetchOpts);
}
const response = await fetch('/api/backends/chat-completions/generate', fetchOpts);
if (!response.ok) {
const errorText = await response.text();

View File

@@ -155,4 +155,8 @@ export const tableSystemDefaultSettings = {
table_independent_rules_enabled: false,
table_tags_to_extract: '',
table_exclusion_rules: [],
// Nccs API 设置
nccsEnabled: false,
nccsFakeStreamEnabled: false,
};

41
imports.js Normal file
View File

@@ -0,0 +1,41 @@
// Side-effect imports (独立模块/自初始化模块)
import "./PresetSettings/index.js";
import "./PreOptimizationViewer/index.js";
import "./WorldEditor/WorldEditor.js";
import './core/amily2-updater.js';
import './SL/bus/Amily2Bus.js'
// Re-exports (重新导出供 index.js 使用)
export { createDrawer } from "./ui/drawer.js";
export { showPlotOptimizationProgress, updatePlotOptimizationProgress, hidePlotOptimizationProgress } from './ui/optimization-progress.js';
export { registerSlashCommands } from "./core/commands.js";
export { onMessageReceived, handleTableUpdate } from "./core/events.js";
export { processPlotOptimization } from "./core/summarizer.js";
// External SillyTavern scripts (外部脚本)
export { getContext, extension_settings } from "/scripts/extensions.js";
export { characters, this_chid, eventSource, event_types, saveSettingsDebounced } from '/script.js';
// Core Systems
export { injectTableData, generateTableContent } from "./core/table-system/injector.js";
export { initialize as initializeRagProcessor } from "./core/rag-processor.js";
export { loadTables, clearHighlights, rollbackAndRefill, rollbackState, commitPendingDeletions, saveStateToMessage, getMemoryState, clearUpdatedTables } from './core/table-system/manager.js';
export { fillWithSecondaryApi } from './core/table-system/secondary-filler.js';
export { renderTables } from './ui/table-bindings.js';
export { log } from './core/table-system/logger.js';
export { checkForUpdates, fetchMessageBoardContent } from './core/api.js';
export { setUpdateInfo, applyUpdateIndicator } from './ui/state.js';
export { pluginVersion, extensionName, defaultSettings } from './utils/settings.js';
export { checkAuthorization, refreshUserInfo } from './utils/auth.js';
export { tableSystemDefaultSettings } from './core/table-system/settings.js';
export { manageLorebookEntriesForChat } from './core/lore.js';
// Feature Modules
export { initializeCharacterWorldBook } from './CharacterWorldBook/cwb_index.js';
export { cwbDefaultSettings } from './CharacterWorldBook/src/cwb_config.js';
export { bindGlossaryEvents } from './glossary/GT_bindings.js';
export { updateOrInsertTableInChat, startContinuousRendering, stopContinuousRendering } from './ui/message-table-renderer.js';
export { initializeRenderer } from './core/tavern-helper/renderer.js';
export { initializeApiListener, registerApiHandler, amilyHelper, initializeAmilyHelper } from './core/tavern-helper/main.js';
export { registerContextOptimizerMacros, resetContextBuffer } from './core/context-optimizer.js';
export { initializeSuperMemory } from './core/super-memory/manager.js';

1140
index.js

File diff suppressed because it is too large Load Diff

View File

@@ -1,58 +1,13 @@
{
"name": "Amily2号聊天优化助手",
"display_name": "Amily2号助手",
"version": "1.8.3",
"author": "Wx-2025",
"description": "一个拥有独立UI的智能引擎正文优化、自动总结、记忆表格、rag向量、隐藏楼层、剧情推进等多功能整合。",
"minSillyTavernVersion": "1.10.0",
"requires": [],
"homePage": "https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git",
"loading_order": 9999,
"js": "index.js",
"styles": ["style.css"]
"name": "Amily2号聊天优化助手",
"display_name": "Amily2号助手",
"version": "1.8.4-a",
"author": "Wx-2025",
"description": "一个拥有独立UI的智能引擎正文优化、自动总结、记忆表格、rag向量、隐藏楼层、剧情推进等多功能整合。",
"minSillyTavernVersion": "1.10.0",
"requires": [],
"homePage": "https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git",
"loading_order": 9999,
"js": "index.js",
"styles": ["style.css"]
}

View File

@@ -75,7 +75,7 @@ async function initializePanel(contentPanel, errorContainer) {
const mainContainer = contentPanel.find('#amily2_chat_optimiser');
if (mainContainer.length) {
const additionalFeaturesContent = await $.get(`${extensionFolderPath}/assets/Amily2-AdditionalFeatures.html`);
const additionalFeaturesContent = await $.get(`${extensionFolderPath}/assets/amily-additional-features/Amily2-AdditionalFeatures.html`);
const additionalPanelHtml = `<div id="amily2_additional_features_panel" style="display: none;">${additionalFeaturesContent}</div>`;
mainContainer.append(additionalPanelHtml);
@@ -83,11 +83,11 @@ async function initializePanel(contentPanel, errorContainer) {
const textOptimizationPanelHtml = `<div id="amily2_text_optimization_panel" style="display: none;">${textOptimizationContent}</div>`;
mainContainer.append(textOptimizationPanelHtml);
const hanlinyuanContent = await $.get(`${extensionFolderPath}/assets/hanlinyuan.html`);
const hanlinyuanContent = await $.get(`${extensionFolderPath}/assets/amily-hanlinyuan-system/hanlinyuan.html`);
const hanlinyuanPanelHtml = `<div id="amily2_hanlinyuan_panel" style="display: none;">${hanlinyuanContent}</div>`;
mainContainer.append(hanlinyuanPanelHtml);
const memorisationFormsContent = await $.get(`${extensionFolderPath}/assets/Memorisation-forms.html`);
const memorisationFormsContent = await $.get(`${extensionFolderPath}/assets/amily-data-table/Memorisation-forms.html`);
const memorisationFormsPanelHtml = `<div id="amily2_memorisation_forms_panel" style="display: none;">${memorisationFormsContent}</div>`;
mainContainer.append(memorisationFormsPanelHtml);
@@ -103,7 +103,7 @@ async function initializePanel(contentPanel, errorContainer) {
const worldEditorPanelHtml = `<div id="amily2_world_editor_panel" style="display: none;">${worldEditorContent}</div>`;
mainContainer.append(worldEditorPanelHtml);
const glossaryContent = await $.get(`${extensionFolderPath}/assets/amily2-glossary.html`);
const glossaryContent = await $.get(`${extensionFolderPath}/assets/amily-glossary-system/amily2-glossary.html`);
const glossaryPanelHtml = `<div id="amily2_glossary_panel" style="display: none;">${glossaryContent}</div>`;
mainContainer.append(glossaryPanelHtml);

View File

@@ -408,6 +408,7 @@ function bindNgmsApiEvents() {
// Ngms API 开关控制
const ngmsToggle = document.getElementById('amily2_ngms_enabled');
const ngmsFakeStreamToggle = document.getElementById('amily2_ngms_fakestream_enabled');
const ngmsContent = document.getElementById('amily2_ngms_content');
if (ngmsToggle && ngmsContent) {
@@ -421,6 +422,13 @@ function bindNgmsApiEvents() {
});
}
if (ngmsFakeStreamToggle) {
ngmsFakeStreamToggle.checked = extension_settings[extensionName].ngmsFakeStreamEnabled ?? false;
ngmsFakeStreamToggle.addEventListener('change', function() {
updateAndSaveSetting('ngmsFakeStreamEnabled', this.checked);
});
}
// API模式切换
const apiModeSelect = document.getElementById('amily2_ngms_api_mode');
const compatibleConfig = document.getElementById('amily2_ngms_compatible_config');

View File

@@ -1980,6 +1980,7 @@ function bindNccsApiEvents() {
const settings = extension_settings[extensionName];
if (settings.nccsEnabled === undefined) settings.nccsEnabled = false;
if (settings.nccsFakeStreamEnabled === undefined) settings.nccsFakeStreamEnabled = false;
if (settings.nccsApiMode === undefined) settings.nccsApiMode = 'openai_test';
if (settings.nccsApiUrl === undefined) settings.nccsApiUrl = 'https://api.openai.com/v1';
if (settings.nccsApiKey === undefined) settings.nccsApiKey = '';
@@ -1989,6 +1990,7 @@ function bindNccsApiEvents() {
if (settings.nccsTavernProfile === undefined) settings.nccsTavernProfile = '';
const enabledToggle = document.getElementById('nccs-api-enabled');
const enabledFakeStreamToggle = document.getElementById('nccs-api-fakestream-enabled');
const configDiv = document.getElementById('nccs-api-config');
const modeSelect = document.getElementById('nccs-api-mode');
const urlInput = document.getElementById('nccs-api-url');
@@ -2005,6 +2007,7 @@ function bindNccsApiEvents() {
if (!enabledToggle || !configDiv) return;
enabledToggle.checked = settings.nccsEnabled;
enabledFakeStreamToggle.checked = settings.nccsFakeStreamEnabled;
if (modeSelect) modeSelect.value = settings.nccsApiMode;
if (urlInput) urlInput.value = settings.nccsApiUrl;
if (keyInput) keyInput.value = settings.nccsApiKey;
@@ -2065,6 +2068,12 @@ function bindNccsApiEvents() {
log(`Nccs API ${enabledToggle.checked ? '已启用' : '已禁用'}`, 'info');
});
enabledFakeStreamToggle.addEventListener('change', () => {
settings.nccsFakeStreamEnabled = enabledFakeStreamToggle.checked;
saveSettingsDebounced();
log(`Nccs API FakeStream ${enabledFakeStreamToggle.checked ? 'Enabled' : 'Disabled'}`, 'info');
});
if (modeSelect) {
modeSelect.addEventListener('change', () => {
settings.nccsApiMode = modeSelect.value;