- 新增日志系统配置选项,支持日志级别、输出模式、文件大小等设置 - 添加当日日志文件下载功能,可通过Web界面直接下载 - 将console.log/error替换为结构化logger,提升日志可管理性 - 在日志页面添加自动滚动到底部功能 - 更新配置示例文件,包含完整的日志配置参数
75 lines
2.8 KiB
JavaScript
75 lines
2.8 KiB
JavaScript
import { ProviderStrategy } from '../../utils/provider-strategy.js';
|
|
import logger from '../../utils/logger.js';
|
|
import { extractSystemPromptFromRequestBody, MODEL_PROTOCOL_PREFIX } from '../../utils/common.js';
|
|
|
|
/**
|
|
* Claude provider strategy implementation.
|
|
*/
|
|
class ClaudeStrategy extends ProviderStrategy {
|
|
extractModelAndStreamInfo(req, requestBody) {
|
|
const model = requestBody.model;
|
|
const isStream = requestBody.stream === true;
|
|
return { model, isStream };
|
|
}
|
|
|
|
extractResponseText(response) {
|
|
if (response.type === 'content_block_delta' && response.delta ) {
|
|
if(response.delta.type === 'text_delta' ){
|
|
return response.delta.text;
|
|
}
|
|
if(response.delta.type === 'input_json_delta' ){
|
|
return response.delta.partial_json;
|
|
}
|
|
}
|
|
if (response.content && Array.isArray(response.content)) {
|
|
return response.content
|
|
.filter(block => block.type === 'text' && block.text)
|
|
.map(block => block.text)
|
|
.join('');
|
|
} else if (response.content && response.content.type === 'text') {
|
|
return response.content.text;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
extractPromptText(requestBody) {
|
|
if (requestBody.messages && requestBody.messages.length > 0) {
|
|
const lastMessage = requestBody.messages[requestBody.messages.length - 1];
|
|
if (lastMessage.content && Array.isArray(lastMessage.content)) {
|
|
return lastMessage.content.map(block => block.text).join('');
|
|
}
|
|
return lastMessage.content;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
async applySystemPromptFromFile(config, requestBody) {
|
|
if (!config.SYSTEM_PROMPT_FILE_PATH) {
|
|
return requestBody;
|
|
}
|
|
|
|
const filePromptContent = config.SYSTEM_PROMPT_CONTENT;
|
|
if (filePromptContent === null) {
|
|
return requestBody;
|
|
}
|
|
|
|
const existingSystemText = extractSystemPromptFromRequestBody(requestBody, MODEL_PROTOCOL_PREFIX.CLAUDE);
|
|
|
|
const newSystemText = config.SYSTEM_PROMPT_MODE === 'append' && existingSystemText
|
|
? `${existingSystemText}\n${filePromptContent}`
|
|
: filePromptContent;
|
|
|
|
requestBody.system = newSystemText;
|
|
logger.info(`[System Prompt] Applied system prompt from ${config.SYSTEM_PROMPT_FILE_PATH} in '${config.SYSTEM_PROMPT_MODE}' mode for provider 'claude'.`);
|
|
|
|
return requestBody;
|
|
}
|
|
|
|
async manageSystemPrompt(requestBody) {
|
|
const incomingSystemText = extractSystemPromptFromRequestBody(requestBody, MODEL_PROTOCOL_PREFIX.CLAUDE);
|
|
await this._updateSystemPromptFile(incomingSystemText, MODEL_PROTOCOL_PREFIX.CLAUDE);
|
|
}
|
|
}
|
|
|
|
export { ClaudeStrategy };
|
|
|