feat: 添加部分替换功能及优化重写流程
- 新增部分替换视图模式,支持选择性替换原文句子 - 过滤 AI 返回的 <thinking> 标签内容 - 为 applySelectedChanges 添加边界检查防止返回空结果 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -211,6 +211,16 @@ export const computeDiff = (original, rewritten) => {
|
||||
* @returns {string} - 精确替换后的文本
|
||||
*/
|
||||
export const applySelectedChanges = (original, diffSegments, acceptedChanges) => {
|
||||
// 边界检查:如果没有选中任何修改,直接返回原文
|
||||
if (!acceptedChanges || acceptedChanges.size === 0) {
|
||||
return original
|
||||
}
|
||||
|
||||
// 边界检查:如果没有差异片段,直接返回原文
|
||||
if (!diffSegments || diffSegments.length === 0) {
|
||||
return original
|
||||
}
|
||||
|
||||
// 收集所有需要执行的替换操作
|
||||
const operations = []
|
||||
|
||||
@@ -248,6 +258,11 @@ export const applySelectedChanges = (original, diffSegments, acceptedChanges) =>
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有有效的操作,返回原文
|
||||
if (operations.length === 0) {
|
||||
return original
|
||||
}
|
||||
|
||||
// 按位置从后往前排序(避免位置偏移)
|
||||
operations.sort((a, b) => {
|
||||
const posA = a.start !== undefined ? a.start : a.position
|
||||
@@ -267,9 +282,16 @@ export const applySelectedChanges = (original, diffSegments, acceptedChanges) =>
|
||||
}
|
||||
}
|
||||
|
||||
// 最终检查:如果结果为空,返回原文(保护性措施)
|
||||
if (!result || result.trim() === '') {
|
||||
console.warn('applySelectedChanges: 结果为空,返回原文')
|
||||
return original
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取差异统计
|
||||
* @param {Array} diffSegments - 差异片段
|
||||
|
||||
Reference in New Issue
Block a user