mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-18 06:17:06 +00:00
fix: memory forward
This commit is contained in:
parent
d0e365cb32
commit
82de8308b3
3 changed files with 96 additions and 32 deletions
|
|
@ -1,26 +1,10 @@
|
||||||
import Config from '../config/config.js'
|
import Config from '../config/config.js'
|
||||||
import { GroupMessageCollector } from '../models/memory/collector.js'
|
import { GroupMessageCollector } from '../models/memory/collector.js'
|
||||||
import { memoryService } from '../models/memory/service.js'
|
import { memoryService } from '../models/memory/service.js'
|
||||||
|
import common from '../../../lib/common/common.js'
|
||||||
|
|
||||||
const collector = new GroupMessageCollector()
|
const collector = new GroupMessageCollector()
|
||||||
|
|
||||||
function formatUserMemoryList (memories) {
|
|
||||||
if (!memories.length) {
|
|
||||||
return '暂无记录~'
|
|
||||||
}
|
|
||||||
return memories.map(item => `${item.id}. ${item.value}(重要度 ${item.importance.toFixed(2)})`).join('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatGroupFactList (facts) {
|
|
||||||
if (!facts.length) {
|
|
||||||
return '暂无群记忆。'
|
|
||||||
}
|
|
||||||
return facts.map(item => {
|
|
||||||
const topic = item.topic ? `【${item.topic}】` : ''
|
|
||||||
return `${item.id}. ${topic}${item.fact}`
|
|
||||||
}).join('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
function isGroupManager (e) {
|
function isGroupManager (e) {
|
||||||
if (e.isMaster) {
|
if (e.isMaster) {
|
||||||
return true
|
return true
|
||||||
|
|
@ -55,6 +39,10 @@ export class MemoryManager extends plugin {
|
||||||
reg: '^#?(我的)?记忆$',
|
reg: '^#?(我的)?记忆$',
|
||||||
fnc: 'showUserMemory'
|
fnc: 'showUserMemory'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
reg: '^#?他的记忆$',
|
||||||
|
fnc: 'showTargetUserMemory'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
reg: '^#?(删除|清除)(我的)?记忆\\s*(\\d+)$',
|
reg: '^#?(删除|清除)(我的)?记忆\\s*(\\d+)$',
|
||||||
fnc: 'deleteUserMemory'
|
fnc: 'deleteUserMemory'
|
||||||
|
|
@ -96,9 +84,52 @@ export class MemoryManager extends plugin {
|
||||||
await e.reply('私人记忆未开启或您未被授权。')
|
await e.reply('私人记忆未开启或您未被授权。')
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
const memories = memoryService.listUserMemories(e.sender.user_id, e.isGroup ? e.group_id : null, 10)
|
const memories = memoryService.listUserMemories(e.sender.user_id, e.isGroup ? e.group_id : null)
|
||||||
const content = formatUserMemoryList(memories)
|
|
||||||
await e.reply(`🧠 您的记忆:\n${content}`)
|
if (!memories.length) {
|
||||||
|
await e.reply('🧠 您的记忆:\n暂无记录~')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgs = memories.map(item =>
|
||||||
|
`${item.id}. ${item.value}(重要度 ${item.importance.toFixed(2)})`
|
||||||
|
)
|
||||||
|
|
||||||
|
const forwardMsg = await common.makeForwardMsg(e, ['🧠 您的记忆:', ...msgs], '私人记忆列表')
|
||||||
|
await e.reply(forwardMsg)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
async showTargetUserMemory (e) {
|
||||||
|
if (!e.isGroup) {
|
||||||
|
await e.reply('该指令仅可在群聊中使用。')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const at = e.at || (e.message?.find(m => m.type === 'at')?.qq)
|
||||||
|
if (!at) {
|
||||||
|
await e.reply('请@要查询的用户。')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!memoryService.isUserMemoryEnabled(at)) {
|
||||||
|
await e.reply('该用户未开启私人记忆或未被授权。')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const memories = memoryService.listUserMemories(at, e.group_id)
|
||||||
|
|
||||||
|
if (!memories.length) {
|
||||||
|
await e.reply('🧠 TA的记忆:\n暂无记录~')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgs = memories.map(item =>
|
||||||
|
`${item.id}. ${item.value}(重要度 ${item.importance.toFixed(2)})`
|
||||||
|
)
|
||||||
|
|
||||||
|
const forwardMsg = await common.makeForwardMsg(e, ['🧠 TA的记忆:', ...msgs], 'TA的记忆列表')
|
||||||
|
await e.reply(forwardMsg)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,9 +161,20 @@ export class MemoryManager extends plugin {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
await collector.flush(e.group_id)
|
await collector.flush(e.group_id)
|
||||||
const facts = memoryService.listGroupFacts(e.group_id, 10)
|
const facts = memoryService.listGroupFacts(e.group_id)
|
||||||
const content = formatGroupFactList(facts)
|
|
||||||
await e.reply(`📚 本群记忆:\n${content}`)
|
if (!facts.length) {
|
||||||
|
await e.reply('📚 本群记忆:\n暂无群记忆。')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgs = facts.map(item => {
|
||||||
|
const topic = item.topic ? `【${item.topic}】` : ''
|
||||||
|
return `${item.id}. ${topic}${item.fact}`
|
||||||
|
})
|
||||||
|
|
||||||
|
const forwardMsg = await common.makeForwardMsg(e, ['📚 本群记忆:', ...msgs], '群记忆列表')
|
||||||
|
await e.reply(forwardMsg)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,13 @@ import path from 'path'
|
||||||
import https from 'https'
|
import https from 'https'
|
||||||
import { pipeline } from 'stream'
|
import { pipeline } from 'stream'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import AdmZip from 'adm-zip'
|
let AdmZip
|
||||||
|
try {
|
||||||
|
AdmZip = (await import('adm-zip')).default
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn('Failed to load AdmZip, maybe you need to install it manually:', e)
|
||||||
|
}
|
||||||
|
import { execSync } from "child_process"
|
||||||
import {
|
import {
|
||||||
Chaite,
|
Chaite,
|
||||||
ChaiteResponse,
|
ChaiteResponse,
|
||||||
|
|
@ -219,14 +225,28 @@ async function downloadSimpleExtensionArchive ({ assetKey, assetName, targetDir
|
||||||
await downloadToFile(downloadUrl, tempFile)
|
await downloadToFile(downloadUrl, tempFile)
|
||||||
removeDirectoryIfExists(targetDir)
|
removeDirectoryIfExists(targetDir)
|
||||||
ensureDirectoryExists(targetDir)
|
ensureDirectoryExists(targetDir)
|
||||||
try {
|
if (AdmZip) {
|
||||||
const zip = new AdmZip(tempFile)
|
try {
|
||||||
zip.extractAllTo(targetDir, true)
|
const zip = new AdmZip(tempFile)
|
||||||
} finally {
|
zip.extractAllTo(targetDir, true)
|
||||||
if (fs.existsSync(tempFile)) {
|
} finally {
|
||||||
fs.unlinkSync(tempFile)
|
if (fs.existsSync(tempFile)) {
|
||||||
|
fs.unlinkSync(tempFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 尝试使用 unzip 命令解压
|
||||||
|
try {
|
||||||
|
execSync(`unzip "${tempFile}" -d "${targetDir}"`, { stdio: 'inherit' })
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to extract zip file: ${error.message}. Please install adm-zip manually: pnpm i`)
|
||||||
|
} finally {
|
||||||
|
if (fs.existsSync(tempFile)) {
|
||||||
|
fs.unlinkSync(tempFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const libraryFile = findLibraryFile(targetDir)
|
const libraryFile = findLibraryFile(targetDir)
|
||||||
if (!libraryFile) {
|
if (!libraryFile) {
|
||||||
throw new Error('Downloaded extension package does not contain libsimple library.')
|
throw new Error('Downloaded extension package does not contain libsimple library.')
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "chatgpt-plugin",
|
"name": "chatgpt-plugin",
|
||||||
"version": "3.0.0-beta.1",
|
"version": "3.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"author": "ikechan8370",
|
"author": "ikechan8370",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
@ -12,8 +12,10 @@
|
||||||
"keyv-file": "^5.1.2",
|
"keyv-file": "^5.1.2",
|
||||||
"lowdb": "^7.0.1",
|
"lowdb": "^7.0.1",
|
||||||
"sqlite-vec": "^0.1.7-alpha.2",
|
"sqlite-vec": "^0.1.7-alpha.2",
|
||||||
"sqlite3": "^5.1.6",
|
|
||||||
"vectra": "^0.9.0"
|
"vectra": "^0.9.0"
|
||||||
},
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"sqlite3": ">=5.1.6"
|
||||||
|
},
|
||||||
"pnpm": {}
|
"pnpm": {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue