实现右侧记忆面板可编辑

本次功能

右侧记忆面板可编辑

支持:

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

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 🌟🌟🌟 谢谢~

相关推荐
AI问答工程师2 小时前
4 小时攻破 FreeBSD 内核:MAD Bugs 计划揭示 AI 自主漏洞发现的新纪元
人工智能
星马梦缘2 小时前
cannot import name ‘__version__‘ from ‘tensorflow.keras‘ 的解决方案
人工智能·tensorflow·keras
DanCheOo2 小时前
从"会用 AI"到"架构 AI":高级前端的认知升级
前端·agent
补三补四2 小时前
模型编辑入门:给AI“打补丁”的核心技术解析
人工智能·深度学习
diving deep2 小时前
从零构建大模型--理解大语言模型
人工智能·语言模型·自然语言处理
我就是全世界2 小时前
开源脚本破解Claude Code金色传说宠物:1%概率闪光龙,三步暴力刷出
人工智能·宠物·claudecode
数字卢语2 小时前
Ollama 实战排障:为什么明明装了显卡,模型却跑在 CPU 上?一套完整可复现的 GPU 排查方法
人工智能
I love studying!!!2 小时前
Web项目:从Django入手
后端·python·django
jinanwuhuaguo2 小时前
OpenClaw、Agent、Skill、MCP 深度解读与区分分析
网络·人工智能·网络协议·rpc·openclaw