deepThink.ts 1.5 KB
export function createDeepThink(content: string) {
  const lines = content.split("\n");
  let deepThink: string[] = [];
  let j = 0;
  let isBreak = false;
  for (let i = 0; i < lines.length; i++) {
    if (lines[i] === "") {
      lines.splice(i, 1); // 删除空行
      i--;
      continue;
    }
    if (
      lines[i].startsWith(">") ||
      lines[i].startsWith("2") ||
      lines[i].startsWith("3") ||
      lines[i].startsWith("4")
    ) {
      if (j !== 0 && lines[i].startsWith(">")) {
        lines[i] = lines[i].substring(1);
      }
      deepThink.push(lines[i]);
      lines.splice(i, 1);
      j++;
      i--;
    } else {
      break;
    }
  }
  const deepThinkStr = deepThink.join("");
  const linesStr = lines.join("\n");
  const result = [deepThinkStr, "", linesStr].join("\n");
  return result;
}

export function removeDeepThink(content: string) {
  const lines = content.split("\n");
  let deepThink: string[] = [];
  let j = 0;
  let isBreak = false;
  for (let i = 0; i < lines.length; i++) {
    if (lines[i] === "") {
      lines.splice(i, 1); // 删除空行
      i--;
      continue;
    }
    if (
      lines[i].startsWith(">") ||
      lines[i].startsWith("2") ||
      lines[i].startsWith("3") ||
      lines[i].startsWith("4")
    ) {
      if (j !== 0 && lines[i].startsWith(">")) {
        lines[i] = lines[i].substring(1);
      }
      deepThink.push(lines[i]);
      lines.splice(i, 1);
      j++;
      i--;
    } else {
      break;
    }
  }
  return lines.join("\n");
}