后端存入数据库的内容正常,但是前端看不到消息具体内容。
$ sqlite3 data/koishi.db
SQLite version 3.40.1 2022-12-28 14:03:47
Enter ".help" for usage hints.
sqlite> select * from sns_record;
1|1737221748158|onebot|
2|1737221770910|onebot|现在一问就是
3|1737221773151|onebot|
4|1737221774989|onebot|不知道啊
5|1737221780150|onebot|谁知道啊
6|1737221784642|onebot|我怎么知道
7|1737221797527|onebot|抱着手机不撒手
8|1737221801910|onebot|电脑也不会用
9|1737221806913|onebot|
10|1737221809561|onebot|我真觉得要完蛋了
11|1737221826842|onebot|玩迷你世界吗
sqlite> SELECT name FROM sqlite_master WHERE type='table';
user
sqlite_sequence
binding
channel
analytics.message
analytics.command
sns_record
sqlite> PRAGMA table_info(sns_record);
0|id|INTEGER|1||1
1|create_time|INTEGER|0||0
2|platform|TEXT|0|''|0
3|textContent|TEXT|0|''|0
sqlite>
这里是代码实现:
创建表:
declare module 'koishi' {
interface Tables {
schedule: SnsRecord;
}
}
export interface SnsRecord {
id: number;
create_time: Date;
platform: string;
textContent: string[];
}
export function init(sns_record, ctx: Context){
ctx.model.extend(sns_record, {
id: 'bigint',
create_time: 'timestamp',
platform: 'char(32)',
textContent: 'list',
}, {
primary: 'id',
autoInc: true,
});
}
消息插入:
export function apply(ctx: Context) {
ctx.on('message-created', async (session) => {
try {
// 获取消息内容
const platform = session.event.platform;
const userId = session.event.user.id;
const timestamp = session.event.timestamp;
const messageElements = session.event.message.elements;
// 处理消息
let textContent: string[] = [];
messageElements.forEach((element) => {
switch (element.type) {
case 'text': {
textContent.push(element.attrs.content);
break;
}
case 'face': {
textContent.push(element.attrs.name);
break;
}
case 'img': {
let fileUrl = element.attrs.src;
// 调用OSS
//textContent = await oss.getFileContent(fileUrl);
break;
}
case 'video': {
let fileUrl = element.attrs.src;
// 调用OSS
//textContent = await oss.getFileContent(fileUrl);
break;
}
}
});
// 存储全部消息到数据库
let data = {
create_time: Date.now(),
platform: platform,
textContent: textContent,
}
ctx.logger.info(data)
try {
await ctx.database.create(table_name as any, data)
}
catch (error) {
ctx.logger.info(error)
if (error.message === `cannot resolve table "${table_name}"`){
init(table_name, ctx)
await ctx.database.create(table_name as any, data)
}
}
let result = await ctx.database.get(table_name as any, {id: 1})
//
// 调用异步函数进行向量生成
// const embeddingResponse = await generateEmbedding([messageContent], Config);
// 在这里处理嵌入结果,例如存储到数据库
} catch (error) {
ctx.logger.error('Error processing message:', error);
}
});
}
目前logger输出均符合预期,所以koishi的web console只在我创建的表中无法显示具体内容,是什么原因导致的?