长期记忆
Convai 可以跨会话记住有关用户的信息。当你提供 `endUserId` 时,角色会自动构建并回忆长期记忆。
最后更新于
这有帮助吗?
这有帮助吗?
const client = useConvaiClient({
apiKey: 'YOUR_API_KEY',
characterId: 'YOUR_CHARACTER_ID',
endUserId: 'a1b2c3d4-...', // 任意字符串——优先使用(UUID 或电子邮件),例如 'user@example.com'
});const memory = client.memoryManager;
if (!memory) {
// 未提供 endUserId,或者客户端尚未连接
return;
}const result = await memory.listMemories({ page: 1, pageSize: 50 });
console.log(`总数:${result.total_count}`);
console.log(`是否还有更多:${result.has_more}`);
result.memories.forEach(m => {
console.log(`[${m.id}] ${m.memory}`);
// m.created_at、m.updated_at(ISO 时间戳)
});
// 如有需要,可进行分页
if (result.has_more) {
const page2 = await memory.listMemories({ page: 2, pageSize: 50 });
}const result = await memory.addMemories([
'用户偏好深色模式界面。',
'用户正在学习西班牙语。',
'用户把弹吉他作为爱好。',
]);
result.memories.forEach(m => {
console.log(`已添加:${m.id} → ${m.memory}`);
});const m = await memory.getMemory('f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c');
console.log(m.memory); // “用户偏好深色模式界面。”
console.log(m.created_at); // ISO 时间戳
console.log(m.updated_at); // ISO 时间戳const result = await memory.deleteMemory('f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c');
if (result.deleted) {
console.log('已删除:', result.memory_id);
}const result = await memory.deleteAllMemories();
console.log(result.message); // “记忆删除正在进行中...”
// 稍等片刻并验证
await new Promise(r => setTimeout(r, 2000));
const check = await memory.listMemories();
console.log('剩余:', check.total_count);import { MemoryManager } from '@convai/web-sdk/core';
const manager = new MemoryManager(
'YOUR_API_KEY', // 或认证令牌
'CHARACTER_ID',
'END_USER_ID',
);
const memories = await manager.listMemories();interface Memory {
id: string; // UUID
memory: string; // 文本内容
created_at: string; // ISO 8601 时间戳
updated_at: string; // ISO 8601 时间戳
}