- 新增日志系统配置选项,支持日志级别、输出模式、文件大小等设置 - 添加当日日志文件下载功能,可通过Web界面直接下载 - 将console.log/error替换为结构化logger,提升日志可管理性 - 在日志页面添加自动滚动到底部功能 - 更新配置示例文件,包含完整的日志配置参数
84 lines
3.2 KiB
JavaScript
84 lines
3.2 KiB
JavaScript
import { promises as fs } from 'fs';
|
|
import logger from './logger.js';
|
|
import { FETCH_SYSTEM_PROMPT_FILE } from '../utils/common.js';
|
|
|
|
/**
|
|
* Abstract provider strategy class, defining the interface for handling different model providers.
|
|
*/
|
|
export class ProviderStrategy {
|
|
/**
|
|
* Extracts model and stream information.
|
|
* @param {object} req - HTTP request object.
|
|
* @param {object} requestBody - Parsed request body.
|
|
* @returns {{model: string, isStream: boolean}} Object containing model name and stream status.
|
|
*/
|
|
extractModelAndStreamInfo(req, requestBody) {
|
|
throw new Error("Method 'extractModelAndStreamInfo()' must be implemented.");
|
|
}
|
|
|
|
/**
|
|
* Extracts text content from the response.
|
|
* @param {object} response - API response object.
|
|
* @returns {string} Extracted text content.
|
|
*/
|
|
extractResponseText(response) {
|
|
throw new Error("Method 'extractResponseText()' must be implemented.");
|
|
}
|
|
|
|
/**
|
|
* Extracts prompt text from the request body.
|
|
* @param {object} requestBody - Request body object.
|
|
* @returns {string} Extracted prompt text.
|
|
*/
|
|
extractPromptText(requestBody) {
|
|
throw new Error("Method 'extractPromptText()' must be implemented.");
|
|
}
|
|
|
|
/**
|
|
* Applies system prompt file content to the request body.
|
|
* @param {object} config - Configuration object.
|
|
* @param {object} requestBody - Request body object.
|
|
* @returns {Promise<object>} Modified request body.
|
|
*/
|
|
async applySystemPromptFromFile(config, requestBody) {
|
|
throw new Error("Method 'applySystemPromptFromFile()' must be implemented.");
|
|
}
|
|
|
|
/**
|
|
* Manages the system prompt file.
|
|
* @param {object} requestBody - Request body object.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async manageSystemPrompt(requestBody) {
|
|
throw new Error("Method 'manageSystemPrompt()' must be implemented.");
|
|
}
|
|
|
|
/**
|
|
* Updates the system prompt file.
|
|
* @param {string} incomingSystemText - Incoming system prompt text.
|
|
* @param {string} providerName - Provider name (for logging).
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async _updateSystemPromptFile(incomingSystemText, providerName) {
|
|
let currentSystemText = '';
|
|
try {
|
|
currentSystemText = await fs.readFile(FETCH_SYSTEM_PROMPT_FILE, 'utf8');
|
|
} catch (error) {
|
|
if (error.code !== 'ENOENT') {
|
|
logger.error(`[System Prompt Manager] Error reading system prompt file: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
try {
|
|
if (incomingSystemText && incomingSystemText !== currentSystemText) {
|
|
await fs.writeFile(FETCH_SYSTEM_PROMPT_FILE, incomingSystemText);
|
|
logger.info(`[System Prompt Manager] System prompt updated in file for provider '${providerName}'.`);
|
|
} else if (!incomingSystemText && currentSystemText) {
|
|
await fs.writeFile(FETCH_SYSTEM_PROMPT_FILE, '');
|
|
logger.info('[System Prompt Manager] System prompt cleared from file.');
|
|
}
|
|
} catch (error) {
|
|
logger.error(`[System Prompt Manager] Failed to manage system prompt file: ${error.message}`);
|
|
}
|
|
}
|
|
}
|