实现右侧记忆面板可编辑

本次功能

右侧记忆面板可编辑

支持:

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

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

相关推荐
嵌入式小企鹅1 小时前
CPU供需趋紧、DeepSeek V4全链适配、小米开源万亿模型
人工智能·学习·开源·嵌入式·小米·算力·昇腾
草莓熊Lotso1 小时前
Vibe Coding 时代:LangChain 与 LangGraph 全链路解析
linux·运维·服务器·数据库·人工智能·mysql·langchain
快乐非自愿2 小时前
RAG夺命10连问,你能抗住第几问?
人工智能·面试·程序员
千匠网络4 小时前
破局出海壁垒,千匠网络新能源汽车跨境出海解决方案
人工智能
candyTong6 小时前
一觉醒来,大模型就帮我排查完页面性能问题
前端·javascript·架构
魔术师Grace6 小时前
我给 AI 做了场入职培训
前端·程序员
马丁聊GEO6 小时前
解码AI用户心智,筑牢可信GEO根基——悠易科技深度参与《中国AI用户态度与行为研究报告(2026)》发布会
人工智能·科技
nap-joker6 小时前
Fusion - Mamba用于跨模态目标检测
人工智能·目标检测·计算机视觉·fusion-mamba·可见光-红外成像融合·远距离/伪目标问题
一只幸运猫.7 小时前
2026Java 后端面试完整版|八股简答 + AI 大模型集成技术(最新趋势)
人工智能·面试·职场和发展
Promise微笑7 小时前
2026年国产替代油介损测试仪:油介损全场景解决方案与技术演进
大数据·网络·人工智能