feat: 结束会话总结支持 AI 重新生成
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { useSummaryReport } from '@/components/composables/useSummaryReport'
|
||||
import { submitAiTask, getAiTask } from '@/api/aiTasks'
|
||||
import type { AiTask } from '@/api/aiTasks'
|
||||
|
||||
vi.mock('@/api/aiTasks', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/api/aiTasks')>()
|
||||
return { ...actual, submitAiTask: vi.fn(), getAiTask: vi.fn(), getLatestAiTask: vi.fn() }
|
||||
})
|
||||
|
||||
const ok = <T,>(data: T) => ({ code: 200, message: '请求成功', data })
|
||||
|
||||
const task = (overrides: Partial<AiTask> = {}): AiTask => ({
|
||||
taskNum: 'AI001',
|
||||
type: 'REPORT_DRAFT',
|
||||
targetNum: 'S001',
|
||||
status: 'RUNNING',
|
||||
...overrides,
|
||||
})
|
||||
|
||||
const mountReport = (hasFragments = true) =>
|
||||
useSummaryReport({ getSessionNum: () => 'S001', hasFragments: () => hasFragments })
|
||||
|
||||
describe('useSummaryReport(结束会话总结)', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(submitAiTask).mockResolvedValue(ok(task({ status: 'PENDING' })))
|
||||
vi.mocked(getAiTask).mockResolvedValue(
|
||||
ok(
|
||||
task({
|
||||
status: 'SUCCEEDED',
|
||||
result: { sessionSummary: '新摘要', expectationVerdict: '新判断', freeSummary: '新总结' },
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
it('AI 重新生成:三段非空时先确认,确认后整段覆盖为新草稿', async () => {
|
||||
const report = mountReport()
|
||||
report.sessionSummary.value = '旧摘要'
|
||||
report.freeSummary.value = '旧总结'
|
||||
|
||||
await report.regenerateDraft()
|
||||
|
||||
expect(ElMessageBox.confirm).toHaveBeenCalled()
|
||||
expect(submitAiTask).toHaveBeenCalledWith({ type: 'REPORT_DRAFT', targetNum: 'S001' })
|
||||
expect(report.sessionSummary.value).toBe('新摘要')
|
||||
expect(report.expectationVerdict.value).toBe('新判断')
|
||||
expect(report.freeSummary.value).toBe('新总结')
|
||||
})
|
||||
|
||||
it('AI 重新生成:用户取消确认时不提交、不改动内容', async () => {
|
||||
vi.mocked(ElMessageBox.confirm).mockRejectedValueOnce('cancel')
|
||||
const report = mountReport()
|
||||
report.sessionSummary.value = '旧摘要'
|
||||
|
||||
await report.regenerateDraft()
|
||||
|
||||
expect(submitAiTask).not.toHaveBeenCalled()
|
||||
expect(report.sessionSummary.value).toBe('旧摘要')
|
||||
})
|
||||
|
||||
it('AI 重新生成:三段都空时不弹确认,直接生成', async () => {
|
||||
const report = mountReport()
|
||||
|
||||
await report.regenerateDraft()
|
||||
|
||||
expect(ElMessageBox.confirm).not.toHaveBeenCalled()
|
||||
expect(submitAiTask).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('AI 重新生成:这次学习没有残片时提示且不提交', async () => {
|
||||
const report = mountReport(false)
|
||||
|
||||
await report.regenerateDraft()
|
||||
|
||||
expect(ElMessage.warning).toHaveBeenCalledWith('这次学习还没有残片,暂无可聚合的内容')
|
||||
expect(submitAiTask).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -138,6 +138,7 @@ const {
|
||||
summaryLoading,
|
||||
summaryLoadingSeconds,
|
||||
openSummaryDialog,
|
||||
regenerateDraft,
|
||||
closeSummaryDialog,
|
||||
} = useSummaryReport({
|
||||
getSessionNum: () => taskInfo.value.sessionNum,
|
||||
@@ -1026,6 +1027,14 @@ defineExpose({
|
||||
:type="summaryPreview ? 'primary' : ''"
|
||||
@click="summaryPreview = true"
|
||||
>预览</el-button>
|
||||
<el-button
|
||||
size="small"
|
||||
type="success"
|
||||
plain
|
||||
class="summary-regenerate"
|
||||
:loading="summaryLoading"
|
||||
@click="regenerateDraft"
|
||||
>AI 重新生成</el-button>
|
||||
</div>
|
||||
<div v-if="!summaryPreview" v-loading="summaryLoading" class="summary-fields">
|
||||
<div class="summary-field">
|
||||
@@ -1321,6 +1330,10 @@ defineExpose({
|
||||
border: none;
|
||||
}
|
||||
|
||||
.summary-toolbar .summary-regenerate {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.summary-preview {
|
||||
padding: 12px 16px;
|
||||
min-height: 200px;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { computed, ref } from 'vue';
|
||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import { useAiTask } from './useAiTask';
|
||||
|
||||
/** 结束会话提交的学习报告三段文本 */
|
||||
@@ -18,6 +19,8 @@ export const isSummarySectionsEmpty = (sections: SummarySections): boolean =>
|
||||
* “结束会话总结”弹窗:报告为结构化三段(会话摘要 / 预期达成判断 / 自由总结),各自可编辑。
|
||||
* 有学习残片且三段都还没填写时,先提交 REPORT_DRAFT 任务并轮询取回三段草稿作为编辑起点(失败不阻塞手写),
|
||||
* 等待期间展示“已等待 N 秒”提示。
|
||||
*
|
||||
* 另外提供「AI 重新生成」:重跑一次草稿生成并覆盖三段(报告-02 的总结质量依赖 AI,允许用户不满意时再生成)。
|
||||
*/
|
||||
export function useSummaryReport(options: {
|
||||
getSessionNum: () => string;
|
||||
@@ -47,6 +50,14 @@ export function useSummaryReport(options: {
|
||||
if (typeof draftFree === "string" && draftFree) freeSummary.value = draftFree;
|
||||
};
|
||||
|
||||
/** 重新生成时整段覆盖:结果就是这份新草稿,不留上次的旧文本 */
|
||||
const overwriteWithDraft = (result: Record<string, unknown> | undefined) => {
|
||||
const text = (value: unknown): string => (typeof value === "string" ? value : "");
|
||||
sessionSummary.value = text(result?.sessionSummary);
|
||||
expectationVerdict.value = text(result?.expectationVerdict);
|
||||
freeSummary.value = text(result?.freeSummary);
|
||||
};
|
||||
|
||||
const openSummaryDialog = async () => {
|
||||
summaryDialogVisible.value = true;
|
||||
// 有残片且三段都还空着时,用 AI 聚合草稿作为编辑起点
|
||||
@@ -63,6 +74,39 @@ export function useSummaryReport(options: {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* AI 重新生成:重跑 REPORT_DRAFT。
|
||||
* 三段只要有一段非空就先确认,避免手滑覆盖用户已编辑的内容;确认后整段覆盖为新草稿。
|
||||
*/
|
||||
const regenerateDraft = async (): Promise<void> => {
|
||||
if (reportTask.running.value) return;
|
||||
if (!options.hasFragments()) {
|
||||
ElMessage.warning("这次学习还没有残片,暂无可聚合的内容");
|
||||
return;
|
||||
}
|
||||
if (!isSummarySectionsEmpty(sections.value)) {
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
"重新生成会用 AI 草稿覆盖当前三段内容(包括你已编辑的部分),确定继续吗?",
|
||||
"重新生成学习总结",
|
||||
{ type: "warning", confirmButtonText: "重新生成", cancelButtonText: "取消" },
|
||||
);
|
||||
} catch {
|
||||
return; // 用户取消
|
||||
}
|
||||
}
|
||||
summaryLoading.value = true;
|
||||
try {
|
||||
const result = await reportTask.run("REPORT_DRAFT", options.getSessionNum());
|
||||
overwriteWithDraft(result);
|
||||
ElMessage.success("已重新生成,可继续编辑");
|
||||
} catch {
|
||||
ElMessage.error("AI 重新生成失败,请稍后重试");
|
||||
} finally {
|
||||
summaryLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const closeSummaryDialog = () => {
|
||||
summaryDialogVisible.value = false;
|
||||
reportTask.stop();
|
||||
@@ -78,6 +122,7 @@ export function useSummaryReport(options: {
|
||||
summaryLoading,
|
||||
summaryLoadingSeconds: reportTask.seconds,
|
||||
openSummaryDialog,
|
||||
regenerateDraft,
|
||||
closeSummaryDialog,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user