fix(server): 修复消息持久化问题
- 在 adapter.ts 中为 Agent 绑定 Core SessionManager - 添加 getOrCreateSessionManager 函数创建和恢复会话 - 修改 getOrCreateAgent 为异步函数 - 在 sessions.ts 中转换 AI SDK 消息格式为字符串
This commit is contained in:
@@ -134,13 +134,30 @@ sessionsRouter.get('/:id/messages', async (c) => {
|
||||
});
|
||||
}
|
||||
|
||||
// 为消息添加 ID(Core 的 ModelMessage 格式没有 id 字段)
|
||||
// 为消息添加 ID 并转换内容格式(AI SDK 格式 -> 字符串)
|
||||
const messagesWithId = sessionData.messages.map(
|
||||
(msg: { role: string; content: unknown }, index: number) => ({
|
||||
...msg,
|
||||
id: `${msg.role}-${id}-${index}`,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
(msg: { role: string; content: unknown }, index: number) => {
|
||||
// 转换 AI SDK 内容格式为字符串
|
||||
let content: string;
|
||||
if (typeof msg.content === 'string') {
|
||||
content = msg.content;
|
||||
} else if (Array.isArray(msg.content)) {
|
||||
// AI SDK 格式: [{type: "text", text: "..."}, ...]
|
||||
content = msg.content
|
||||
.filter((block: { type?: string }) => block.type === 'text')
|
||||
.map((block: { text?: string }) => block.text || '')
|
||||
.join('');
|
||||
} else {
|
||||
content = String(msg.content);
|
||||
}
|
||||
|
||||
return {
|
||||
id: `${msg.role}-${id}-${index}`,
|
||||
role: msg.role,
|
||||
content,
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
return c.json({
|
||||
|
||||
Reference in New Issue
Block a user