新增功能:
支持导出为:
MarkdownJSON
好处是后面可以:
- 保存聊天记录
- 做项目展示
- 为后续接入“知识库导入 / 训练数据沉淀”做准备
继续,这次做一个很实用、很像正式产品的功能:
1)web/src/App.vue
新增两个方法
const downloadFile = (filename, content, type = 'text/plain;charset=utf-8') => {
const blob = new Blob([content], { type })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(url)
}
const exportCurrentSession = format => {
if (!currentSession.value) return
const session = currentSession.value
const safeTitle = (session.title || '未命名会话').replace(/[\/:*?"<>|]/g, '-')
const time = formatTime(session.updatedAt).replace(/[ :]/g, '-')
if (format === 'json') {
downloadFile(
`${safeTitle}-${time}.json`,
JSON.stringify(session, null, 2),
'application/json;charset=utf-8'
)
return
}
if (format === 'md') {
const lines = [
`# ${session.title || '未命名会话'}`,
'',
`- 会话ID:${session.id}`,
`- 更新时间:${new Date(session.updatedAt).toLocaleString()}`,
'',
'---',
'',
]
session.messages.forEach(item => {
if (item.role === 'system') return
const roleName =
item.role === 'user'
? '我'
: item.role === 'assistant'
? 'AI'
: item.role
lines.push(`## ${roleName}`)
lines.push('')
lines.push(item.content || '')
lines.push('')
})
if (memoryList.value.length) {
lines.push('---')
lines.push('')
lines.push('## 会话记忆')
lines.push('')
memoryList.value.forEach(item => {
lines.push(`- ${item}`)
})
lines.push('')
}
downloadFile(
`${safeTitle}-${time}.md`,
lines.join('\n'),
'text/markdown;charset=utf-8'
)
}
}
添加导出按钮
<div class="main-header">
<h1>AI Companion Agent</h1>
<div class="export-actions">
<button class="export-btn" @click="exportCurrentSession('md')">导出 Markdown</button>
<button class="export-btn secondary" @click="exportCurrentSession('json')">导出 JSON</button>
</div>
</div>
新增样式
.main-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 20px;
}
.main-header h1 {
margin: 0;
}
.export-actions {
display: flex;
align-items: center;
gap: 10px;
flex-shrink: 0;
}
.export-btn {
border: none;
border-radius: 10px;
background: #111827;
color: #fff;
padding: 10px 14px;
font-size: 13px;
cursor: pointer;
}
.export-btn.secondary {
background: #4b5563;
}
2)验证
导出 Markdown
点:
导出 Markdown
会下载一个 .md 文件,里面包含:
- 会话标题
- 会话 ID
- 更新时间
- 用户 / AI 对话内容
- 当前会话记忆
导出 JSON
点:
导出 JSON
会下载完整结构化数据,适合后续:
- 导入数据库
- 做会话回放
- 做训练样本整理
现在项目已经有:
- 多会话
- 记忆
- 流式输出
- Markdown 渲染
- 代码复制
- 会话搜索
- 会话导出