feat: add report download functionality

Add ability to download generated reports in Markdown or JSON format.

- Backend: extend existing download endpoint with ?format=json query param
- Frontend: add downloadReport API helper and download button with
  format dropdown menu in Step4Report component

Closes #251

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bortlesboat 2026-03-21 09:50:33 -04:00
parent 1536a79334
commit ecc5214d74
No known key found for this signature in database
GPG Key ID: A2B96F4BB60D03A1
3 changed files with 152 additions and 21 deletions

View File

@ -393,40 +393,60 @@ def list_reports():
@report_bp.route('/<report_id>/download', methods=['GET'])
def download_report(report_id: str):
"""
下载报告Markdown格式
返回Markdown文件
下载报告Markdown或JSON格式
Query参数
format: 下载格式支持 "markdown"默认 "json"
返回对应格式的文件
"""
try:
report = ReportManager.get_report(report_id)
if not report:
return jsonify({
"success": False,
"error": f"报告不存在: {report_id}"
}), 404
download_format = request.args.get('format', 'markdown').lower()
if download_format == 'json':
import json
import tempfile
report_dict = report.to_dict()
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False, encoding='utf-8') as f:
json.dump(report_dict, f, ensure_ascii=False, indent=2)
temp_path = f.name
return send_file(
temp_path,
as_attachment=True,
download_name=f"{report_id}.json",
mimetype='application/json'
)
# Default: markdown format
md_path = ReportManager._get_report_markdown_path(report_id)
if not os.path.exists(md_path):
# 如果MD文件不存在生成一个临时文件
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
f.write(report.markdown_content)
temp_path = f.name
return send_file(
temp_path,
as_attachment=True,
download_name=f"{report_id}.md"
)
return send_file(
md_path,
as_attachment=True,
download_name=f"{report_id}.md"
)
except Exception as e:
logger.error(f"下载报告失败: {str(e)}")
return jsonify({

View File

@ -49,3 +49,14 @@ export const getReport = (reportId) => {
export const chatWithReport = (data) => {
return requestWithRetry(() => service.post('/api/report/chat', data), 3, 1000)
}
/**
* Get the download URL for a report
* @param {string} reportId
* @param {string} format - 'markdown' or 'json'
* @returns {string} download URL
*/
export const getReportDownloadUrl = (reportId, format = 'markdown') => {
const baseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:5001'
return `${baseURL}/api/report/${reportId}/download?format=${format}`
}

View File

@ -127,14 +127,36 @@
</div>
</div>
<!-- Next Step Button - 在完成后显示 -->
<button v-if="isComplete" class="next-step-btn" @click="goToInteraction">
<span>进入深度互动</span>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
<line x1="5" y1="12" x2="19" y2="12"></line>
<polyline points="12 5 19 12 12 19"></polyline>
</svg>
</button>
<!-- Download & Next Step Buttons - 在完成后显示 -->
<div v-if="isComplete" class="report-actions">
<div class="download-dropdown">
<button class="download-btn" @click="toggleDownloadMenu">
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
<polyline points="7 10 12 15 17 10"></polyline>
<line x1="12" y1="15" x2="12" y2="3"></line>
</svg>
<span>Download Report</span>
</button>
<div v-if="showDownloadMenu" class="download-menu">
<a :href="getReportDownloadUrl(reportId, 'markdown')" class="download-menu-item" @click="showDownloadMenu = false">
<span class="format-icon">MD</span>
<span>Markdown (.md)</span>
</a>
<a :href="getReportDownloadUrl(reportId, 'json')" class="download-menu-item" @click="showDownloadMenu = false">
<span class="format-icon">{ }</span>
<span>JSON (.json)</span>
</a>
</div>
</div>
<button class="next-step-btn" @click="goToInteraction">
<span>进入深度互动</span>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
<line x1="5" y1="12" x2="19" y2="12"></line>
<polyline points="12 5 19 12 12 19"></polyline>
</svg>
</button>
</div>
<div class="workflow-divider"></div>
</div>
@ -392,7 +414,7 @@
<script setup>
import { ref, computed, watch, onMounted, onUnmounted, nextTick, h, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { getAgentLog, getConsoleLog } from '../api/report'
import { getAgentLog, getConsoleLog, getReportDownloadUrl } from '../api/report'
const router = useRouter()
@ -411,6 +433,12 @@ const goToInteraction = () => {
}
}
// Download menu state
const showDownloadMenu = ref(false)
const toggleDownloadMenu = () => {
showDownloadMenu.value = !showDownloadMenu.value
}
// State
const agentLogs = ref([])
const consoleLogs = ref([])
@ -3397,13 +3425,85 @@ watch(() => props.reportId, (newId) => {
font-size: 14px;
}
.report-actions {
display: flex;
gap: 8px;
width: calc(100% - 40px);
margin: 4px 20px 0 20px;
}
.download-dropdown {
position: relative;
}
.download-btn {
display: flex;
align-items: center;
gap: 8px;
padding: 14px 20px;
font-size: 14px;
font-weight: 600;
color: #1F2937;
background: #F3F4F6;
border: 1px solid #D1D5DB;
border-radius: 8px;
cursor: pointer;
transition: all 0.2s ease;
white-space: nowrap;
}
.download-btn:hover {
background: #E5E7EB;
}
.download-menu {
position: absolute;
bottom: 100%;
left: 0;
margin-bottom: 4px;
background: #FFFFFF;
border: 1px solid #E5E7EB;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
overflow: hidden;
z-index: 10;
min-width: 180px;
}
.download-menu-item {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 16px;
font-size: 13px;
color: #374151;
text-decoration: none;
cursor: pointer;
transition: background 0.15s ease;
}
.download-menu-item:hover {
background: #F3F4F6;
}
.format-icon {
font-family: 'JetBrains Mono', monospace;
font-size: 11px;
font-weight: 700;
color: #6B7280;
background: #F3F4F6;
padding: 2px 6px;
border-radius: 4px;
min-width: 28px;
text-align: center;
}
.next-step-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
width: calc(100% - 40px);
margin: 4px 20px 0 20px;
flex: 1;
padding: 14px 20px;
font-size: 14px;
font-weight: 600;