Skip to content

Commit

Permalink
add images store
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangWei-KUMO committed Dec 18, 2024
1 parent 71bd84b commit 5c0834c
Show file tree
Hide file tree
Showing 26 changed files with 11,744 additions and 179 deletions.
Binary file modified .DS_Store
Binary file not shown.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This is an open-source WeChat robot project based on WechatY, Dify API services,
|AI understands non-text data such as transfers, red envelopes, and geographical locations. |AI理解转账、红包、地理位置等非文本数据||
|WeChat room chat management |微信群聊天管理||
|Regular message push in WeChat group |微信群定时消息推送||
|Third-party knowledge base access |第三方知识库接入||
|Third-party knowledge base access |第三方长期记忆接入||
|Email notification to Administrator |管理员邮件通知||
| Long Memeory for chat | 聊天长记忆 ||
| Self-running business | 自我运维 ||
Expand Down Expand Up @@ -106,5 +106,11 @@ [email protected]

https://tubex.chat

### 记忆分类

1. ShortMemory (文本文件)
2. VisionMemory(图片)
3. LongMemory (Dify机制)
4. Prompt
5. SpecificMemory (专业知识)

97 changes: 97 additions & 0 deletions db/flashmemories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import sqlite3 from 'sqlite3';
import moment from 'moment-timezone';

moment.tz.setDefault("Asia/Shanghai");

const dbFile = 'flashmemories.db';

const db = new sqlite3.Database(dbFile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('连接记忆数据库成功');
db.run(`
CREATE TABLE IF NOT EXISTS flashmemories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT, -- Store timestamp as TEXT for simplicity
talkid TEXT,
content TEXT,
type TEXT
)
`, (err) => {
if (err) {
console.error("Error creating flashmemories table:", err.message);
}
});
}
});


// 记录记忆
export function saveFlashMemory(talkid, content,type) {
const timestamp = moment().format("YYYY-MM-DD HH:mm:ss");
db.serialize(() => {
db.run(`INSERT INTO flashmemories (timestamp, talkid, content,type) VALUES (?, ?, ?, ?)`, [timestamp, talkid, content,type], (err) => {
if (err) {
console.error('Error inserting memory:', err.message);
}
});
});
}

// 根据talkid查询记忆
export function getFlashMemory(talkid) {
return new Promise((resolve, reject) => {
db.get(`SELECT * FROM flashmemories WHERE talkid = ?`, [talkid], (err, row) => {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve(row);
}
});
});
}


// 查询全部记忆
export function getFlashMemories() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM flashmemories ORDER BY timestamp DESC`, [], (err, rows) => {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve(rows);
}
});
});
}

// 根据talkid删除记忆
export function deleteFlashMemory(talkid) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM flashmemories WHERE talkid = ?`, [talkid], function(err) {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve({message: '日志已删除'}); // Resolve with a success message
}
});
});
}

// 删除所有记忆
export function deleteFlashMemories() {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM flashmemories`, [], function(err) {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve({message: '所有日志已删除'}); // Resolve with a success message
}
});
});
}
108 changes: 108 additions & 0 deletions db/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import sqlite3 from 'sqlite3';
import moment from 'moment-timezone';

moment.tz.setDefault("Asia/Shanghai");

const dbFile = 'images.db';

const db = new sqlite3.Database(dbFile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('连接图片数据库成功');
db.run(`
CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT, -- Store timestamp as TEXT for simplicity
title TEXT,
content TEXT,
type TEXT
)
`, (err) => {
if (err) {
console.error("Error creating images table:", err.message);
}
});
}
});


// 存储图片
export function saveImage(config) {
return new Promise((resolve, reject) => {
const { title, content, type } = config;
const timestamp = moment().format("YYYY-MM-DD HH:mm:ss");

db.serialize(() => {
db.run(
`INSERT INTO images (timestamp, title, content,type) VALUES (?, ?, ?, ?)`,
[timestamp, title, content, type],
(err) => {
if (err) {
console.error("Error inserting image:", err.message);
reject(err); // Reject the promise on error
} else {
resolve(); // Resolve the promise on successful insertion
}
}
);
});
});
}

// 根据id查询记忆
export function getImage(id) {
return new Promise((resolve, reject) => {
db.get(`SELECT * FROM images WHERE id = ?`, [id], (err, row) => {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve(row);
}
});
});
}


// 查询全部图片
export function getImages() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM images ORDER BY timestamp DESC`, [], (err, rows) => {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve(rows);
}
});
});
}

// 根据id删除图片
export function deleteImage(id) {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM images WHERE id = ?`, [id], function(err) {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve({message: '日志已删除'}); // Resolve with a success message
}
});
});
}

// 删除所有记忆
export function deleteImages() {
return new Promise((resolve, reject) => {
db.run(`DELETE FROM images`, [], function(err) {
if (err) {
console.error(err.message);
reject(err);
} else {
resolve({message: '所有日志已删除'}); // Resolve with a success message
}
});
});
}
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import router from './router/router.js';
import staticRouter from './router/static.js';
import {saveWechatConfig} from './db/wechat.js';
// import {Jimp} from 'jimp';
import bodyParser from 'body-parser';

const port = 3000;
config();
const app = express();
app.use(bodyParser.json({'limit':'800kb'}));
app.use(express.urlencoded({'extended':false,'limit':'800kb'}));
app.use(router);
app.use(staticRouter);


export async function prepareBot() {
bot.on("message", async (message) => {
const contact = message.talker();
Expand Down Expand Up @@ -136,7 +141,9 @@ async function startBot() {

}

startBot().catch(console.error);
startBot().catch((e)=>{
console.log("Bot Catch",e);
});

process.on('exit', async(code) => {
log('warning', "程序退出"+code);
Expand Down
Loading

0 comments on commit 5c0834c

Please sign in to comment.