实现右侧记忆面板可编辑

本次功能

右侧记忆面板可编辑

支持:

  • 手动新增记忆
  • 删除单条记忆
  • 修改单条记忆

1)改 server/memory_store.py

新增方法

ini 复制代码
def set_session_memories(session_id: str, memories: List[str]):
    if not session_id:
        return

    data = _load()
    cleaned = []

    for item in memories or []:
        text = str(item).strip()
        if text and text not in cleaned:
            cleaned.append(text)

    data[session_id] = cleaned[:50]
    _save(data)

2)改 server/app.py

补 import

javascript 复制代码
from memory_store import (
    get_session_memories,
    add_session_memories,
    delete_session_memories,
    set_session_memories,
)

新增请求模型

less 复制代码
@app.put("/api/memory/{session_id}", response_model=MemoryResponse)
def update_memory(session_id: str, req: UpdateMemoryRequest):
    if not session_id:
        raise HTTPException(status_code=400, detail="session_id不能为空")

    set_session_memories(session_id, req.memories)
    return MemoryResponse(memories=get_session_memories(session_id))

3)改 web/src/App.vue

新增状态

csharp 复制代码
const memoryDraft = ref('')
const editingMemoryIndex = ref(-1)
const editingMemoryValue = ref('')

新增方法

ini 复制代码
const saveMemories = async nextMemories => {
  if (!currentSessionId.value) return

  try {
    const res = await axios.put(`http://127.0.0.1:8000/api/memory/${currentSessionId.value}`, {
      memories: nextMemories,
    })
    memoryList.value = res.data.memories || []
  } catch (error) {
    console.error(error)
  }
}

const handleAddMemory = async () => {
  const text = memoryDraft.value.trim()
  if (!text) return

  await saveMemories([...memoryList.value, text])
  memoryDraft.value = ''
}

const handleDeleteMemory = async index => {
  const nextMemories = memoryList.value.filter((_, i) => i !== index)
  await saveMemories(nextMemories)

  if (editingMemoryIndex.value === index) {
    editingMemoryIndex.value = -1
    editingMemoryValue.value = ''
  }
}

const handleStartEditMemory = (item, index) => {
  editingMemoryIndex.value = index
  editingMemoryValue.value = item
}

const handleSaveEditMemory = async index => {
  const text = editingMemoryValue.value.trim()
  const nextMemories = [...memoryList.value]

  if (!text) {
    nextMemories.splice(index, 1)
  } else {
    nextMemories[index] = text
  }

  await saveMemories(nextMemories)
  editingMemoryIndex.value = -1
  editingMemoryValue.value = ''
}

const handleCancelEditMemory = () => {
  editingMemoryIndex.value = -1
  editingMemoryValue.value = ''
}

4)改模板

ini 复制代码
<aside class="memory-panel">
  <div class="memory-header">会话记忆</div>

  <div class="memory-editor">
    <input
      v-model="memoryDraft"
      class="memory-input"
      placeholder="手动新增一条记忆"
      @keydown.enter="handleAddMemory"
    />
    <button class="memory-add-btn" @click="handleAddMemory">新增</button>
  </div>

  <div v-if="memoryList.length" class="memory-list">
    <div v-for="(item, index) in memoryList" :key="index" class="memory-item">
      <template v-if="editingMemoryIndex === index">
        <input
          v-model="editingMemoryValue"
          class="memory-edit-input"
          @keydown.enter="handleSaveEditMemory(index)"
          @keydown.esc="handleCancelEditMemory"
        />
        <div class="memory-actions">
          <span class="memory-action" @click="handleSaveEditMemory(index)">保存</span>
          <span class="memory-action delete" @click="handleCancelEditMemory">取消</span>
        </div>
      </template>

      <template v-else>
        <div class="memory-text">{{ item }}</div>
        <div class="memory-actions">
          <span class="memory-action" @click="handleStartEditMemory(item, index)">编辑</span>
          <span class="memory-action delete" @click="handleDeleteMemory(index)">删除</span>
        </div>
      </template>
    </div>
  </div>

  <div v-else class="memory-empty">暂时还没有提取到长期记忆</div>
</aside>

5)补充样式

css 复制代码
.memory-editor {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.memory-input,
.memory-edit-input {
  flex: 1;
  min-width: 0;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  box-sizing: border-box;
  background: #fff;
}

.memory-add-btn {
  border: none;
  border-radius: 10px;
  background: #111827;
  color: #fff;
  padding: 0 14px;
  cursor: pointer;
  flex-shrink: 0;
}

.memory-text {
  line-height: 1.6;
  color: #111827;
  word-break: break-word;
}

.memory-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}

.memory-action {
  font-size: 12px;
  color: #6b7280;
  cursor: pointer;
  user-select: none;
}

.memory-action:hover {
  color: #111827;
}

.memory-action.delete:hover {
  color: #dc2626;
}

6)验证

nice !

本次提交仓库

github.com/fhj414/ai-c...

有收获点个 star 🌟🌟🌟 谢谢~

相关推荐
IT_陈寒15 分钟前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
YuanDaima204820 分钟前
Linux 进阶运维与 AI 环境实战:进程管理、网络排错与 GPU 监控
linux·运维·服务器·网络·人工智能
跨境数据猎手29 分钟前
跨境商城反向海淘系统开发全流程逻辑(上)
人工智能·爬虫·系统架构
idcu37 分钟前
深入 Lyt.js 组件系统:L2 渲染引擎层的核心
前端·typescript
听你说321 小时前
丈八科技与浪潮海若达成战略合作:共建人工智能产测一体化超级工厂
人工智能·科技
初心未改HD1 小时前
深度学习之Attention注意力机制详解
人工智能·深度学习
kyriewen1 小时前
面试官让我查各部门工资最高的员工,我用AI三秒写出窗口函数,他愣了
后端·mysql·面试
这是程序猿1 小时前
Spring Boot自动配置详解
java·大数据·前端
文心快码BaiduComate1 小时前
干货|Comate Harness Engineering工程实践指南
前端·后端·程序员
code_pgf1 小时前
模态生成器:原理详解与推荐开源项目
人工智能·深度学习·开源