解决冲突

This commit is contained in:
chenyuxin221 2025-04-23 19:33:45 +08:00
commit 0217b3bdf9
5 changed files with 97 additions and 67 deletions

View file

@ -1394,4 +1394,4 @@ export class chatgpt extends plugin {
await this.abstractChat(e, prompt, mode, forcePictureMode)
return true
}
}
}

View file

@ -873,4 +873,4 @@ async function collectTools (e) {
}
}
export default new Core()
export default new Core()

View file

@ -2,52 +2,45 @@ import { Config } from './config.js'
import { newFetch } from './proxy.js'
export async function getChatHistoryGroup (e, num) {
if (e.adapter_name && e.adapter_name === 'OneBotv11') {
return await e.group.getChatHistory(0, num, false)
} else {
let latestChats = await e.group.getChatHistory(e.seq || e.message_id, 1)
if (latestChats.length > 0) {
let latestChat = latestChats[0]
if (latestChat) {
let seq = latestChat.seq || latestChat.message_id
let chats = []
while (chats.length < num) {
let chatHistory = await e.group.getChatHistory(seq, 20)
if (!chatHistory || chatHistory.length === 0) {
break
}
chats.push(...chatHistory.reverse())
if (seq === chatHistory[chatHistory.length - 1].seq || seq === chatHistory[chatHistory.length - 1].message_id) {
break
}
seq = chatHistory[chatHistory.length - 1].seq || chatHistory[chatHistory.length - 1].message_id
}
chats = chats.slice(0, num).reverse()
try {
let mm = await e.bot.gml
for (const chat of chats) {
if (e.adapter === 'shamrock') {
if (chat.sender?.user_id === 0) {
// 奇怪格式的历史消息,过滤掉
continue
}
let sender = await pickMemberAsync(e, chat.sender.user_id)
if (sender) {
chat.sender = sender
}
} else {
let sender = mm.get(chat.sender.user_id)
if (sender) {
chat.sender = sender
}
// if (e.adapter === 'shamrock') {
// return await e.group.getChatHistory(0, num, false)
// } else {
let latestChats = await e.group.getChatHistory(e.seq || e.message_id, 1)
if (latestChats.length > 0) {
let latestChat = latestChats[0]
if (latestChat) {
let seq = latestChat.seq || latestChat.message_id
let chats = [e]
while(chats.length < num){
let chatHistory = await e.group.getChatHistory(seq, 20)
if(seq === (chatHistory[0].seq || chatHistory[0].message_id)) break
seq = chatHistory[0].seq || chatHistory[0].message_id
chats.unshift(...chatHistory.filter(chat => chat.sender?.user_id).slice(0, -1))
}
chats = chats.slice(chats.length - num)
try {
let mm = await e.bot.gml
for (const chat of chats) {
if (e.adapter === 'shamrock') {
if (chat.sender?.user_id === 0) {
// 奇怪格式的历史消息,过滤掉
continue
}
let sender = await pickMemberAsync(e, chat.sender.user_id)
if (sender) {
chat.sender = sender
}
} else {
let sender = mm.get(chat.sender.user_id)
if (sender) {
chat.sender = sender
}
}
} catch (err) {
}} catch (err) {
logger.warn(err)
}
// console.log(chats)
return chats
}
return chats
}
}
return []

View file

@ -88,7 +88,7 @@ var ChatGPTAPI = /** @class */ (function () {
this._apiBaseUrl = apiBaseUrl;
this._debug = !!debug;
this._fetch = fetch;
this._completionParams = __assign({ model: CHATGPT_MODEL, temperature: 0.8, top_p: 1.0, presence_penalty: 1.0 }, completionParams);
this._completionParams = __assign({ model: CHATGPT_MODEL, temperature: 1, top_p: 1.0 }, completionParams);
this._systemMessage = systemMessage;
if (this._systemMessage === undefined) {
var currentDate = new Date().toISOString().split('T')[0];
@ -189,7 +189,7 @@ var ChatGPTAPI = /** @class */ (function () {
'Content-Type': 'application/json',
Authorization: "Bearer ".concat(this._apiKey)
};
body = __assign(__assign(__assign({ max_tokens: maxTokens }, this._completionParams), completionParams), { messages: messages, stream: stream });
body = __assign(__assign(__assign({ max_completion_tokens: maxTokens }, this._completionParams), completionParams), { messages: messages, stream: stream });
if (this._debug) {
console.log(JSON.stringify(body));
}

View file

@ -62,7 +62,51 @@ export class SendPictureTool extends AbstractTool {
// 处理消息数组
const messages = []
for (const msg of msgArray) {
if (typeof msg === 'object' && msg.type === 'text' && msg.data?.text) {
if (typeof msg === 'string' && msg.startsWith('"') && msg.endsWith('"')) {
// 处理字符串格式的CQ码
const cqCode = msg.replace(/^"|"$/g, '')
if (cqCode.includes('file=') && !cqCode.startsWith('[CQ:image')) {
// 提取文件信息
const fileMatch = cqCode.match(/file=([^,]+)/)
const urlMatch = cqCode.match(/url=([^,]+)/)
const summaryMatch = cqCode.match(/summary=([^,]+)/)
if (fileMatch || urlMatch) {
const file = fileMatch?.[1] || urlMatch?.[1]
// 检查文件大小和格式
try {
const response = await fetch(file)
const contentType = response.headers.get('content-type')
const contentLength = response.headers.get('content-length')
// 检查文件大小最大10MB
if (contentLength && parseInt(contentLength) > 10 * 1024 * 1024) {
logger.warn(`图片文件过大: ${Math.floor(parseInt(contentLength)/1024/1024)}MB`)
continue
}
// 检查文件格式
if (!contentType?.startsWith('image/')) {
logger.warn(`不支持的图片格式: ${contentType}`)
continue
}
messages.push({
type: 'image',
data: {
file: file,
summary: summaryMatch?.[1]
}
})
} catch (err) {
logger.error(`检查图片文件失败: ${err.message}`)
continue
}
}
} else {
messages.push(msg)
}
} else if (typeof msg === 'object' && msg.type === 'text' && msg.data?.text) {
if (msg.data.text.includes('[CQ:image')) {
// 提取 CQ 码中的参数
const cqMatch = msg.data.text.match(/\[CQ:image,([^\]]+)\]/)
@ -82,8 +126,8 @@ export class SendPictureTool extends AbstractTool {
messages.push({
type: 'image',
data: {
file: params.url || params.file,
...params
file: params.file || params.url,
summary: params.summary
}
})
}
@ -95,13 +139,8 @@ export class SendPictureTool extends AbstractTool {
messages.push(msg)
}
}
// 只保留图片消息
const imageMsg = messages.find(msg => msg.type === 'image')
if (imageMsg) {
urlOfPicture = imageMsg.data.file
} else {
return 'No valid image found in the message'
}
// 返回处理后的消息数组
return messages
}
} catch (err) {
// 如果不是JSON格式尝试解析 CQ 码
@ -119,7 +158,13 @@ export class SendPictureTool extends AbstractTool {
.replace(/&quot;/g, '"')
}
})
urlOfPicture = params.url || params.file
return [{
type: 'image',
data: {
file: params.file || params.url,
summary: params.summary
}
}]
}
}
}
@ -148,8 +193,8 @@ export class SendPictureTool extends AbstractTool {
messages.push({
type: 'image',
data: {
file: params.url || params.file,
...params
file: params.file || params.url,
summary: params.summary
}
})
}
@ -161,15 +206,7 @@ export class SendPictureTool extends AbstractTool {
messages.push(msg)
}
}
// 只保留图片消息
const imageMsg = messages.find(msg => msg.type === 'image')
if (imageMsg) {
urlOfPicture = imageMsg.data.file
} else {
return 'No valid image found in the message'
}
} else {
urlOfPicture = urlOfPicture.join(' ')
return messages
}
}