|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2026 NotepadAI contributors |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "AcpConversationCopy.h" |
| 20 | + |
| 21 | +#include "GoalAgent.h" |
| 22 | + |
| 23 | +namespace AcpConversationCopy { |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +const AcpMessage *messageAt(const AcpTimelineEntry &entry, const QVector<AcpMessage> &messages) |
| 28 | +{ |
| 29 | + if (entry.kind != AcpTimelineEntry::Kind::Message |
| 30 | + || entry.messageIndex < 0 || entry.messageIndex >= messages.size()) { |
| 31 | + return nullptr; |
| 32 | + } |
| 33 | + return &messages.at(entry.messageIndex); |
| 34 | +} |
| 35 | + |
| 36 | +// Allocation-free: scans the blocks in place instead of joining and trimming. |
| 37 | +bool hasVisibleText(const AcpMessage &msg) |
| 38 | +{ |
| 39 | + for (const auto &block : msg.content) { |
| 40 | + if (block.kind == AcpProtocol::AcpContentBlock::Kind::Image) |
| 41 | + return true; // rendered as "[image]" |
| 42 | + for (const QChar c : block.text) { |
| 43 | + if (!c.isSpace()) |
| 44 | + return true; |
| 45 | + } |
| 46 | + } |
| 47 | + return false; |
| 48 | +} |
| 49 | + |
| 50 | +bool isReportCandidate(const AcpMessage &msg) |
| 51 | +{ |
| 52 | + return msg.role == QLatin1String("assistant") && !msg.fromGoalJudge && hasVisibleText(msg); |
| 53 | +} |
| 54 | + |
| 55 | +void appendSection(QString &out, QLatin1StringView heading, const QString &text) |
| 56 | +{ |
| 57 | + const QString body = text.trimmed(); |
| 58 | + if (body.isEmpty()) |
| 59 | + return; |
| 60 | + if (!out.isEmpty()) |
| 61 | + out += QLatin1String("\n\n"); |
| 62 | + out += QLatin1String("## "); |
| 63 | + out += heading; |
| 64 | + out += QLatin1String("\n\n"); |
| 65 | + out += body; |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +QString messageText(const AcpMessage &msg) |
| 71 | +{ |
| 72 | + QString out; |
| 73 | + for (const auto &block : msg.content) { |
| 74 | + if (block.kind == AcpProtocol::AcpContentBlock::Kind::Text) |
| 75 | + out += block.text; |
| 76 | + else |
| 77 | + out += QLatin1String("[image]"); |
| 78 | + } |
| 79 | + return out; |
| 80 | +} |
| 81 | + |
| 82 | +QVector<int> finalReports(const QVector<AcpTimelineEntry> &timeline, |
| 83 | + const QVector<AcpMessage> &messages, |
| 84 | + bool includeNewest) |
| 85 | +{ |
| 86 | + QVector<int> out; |
| 87 | + int survivor = -1; |
| 88 | + for (const AcpTimelineEntry &entry : timeline) { |
| 89 | + const AcpMessage *m = messageAt(entry, messages); |
| 90 | + if (!m) |
| 91 | + continue; |
| 92 | + if (m->role == QLatin1String("user")) { |
| 93 | + if (survivor >= 0) |
| 94 | + out.append(survivor); |
| 95 | + survivor = -1; |
| 96 | + } else if (isReportCandidate(*m)) { |
| 97 | + survivor = entry.messageIndex; |
| 98 | + } |
| 99 | + } |
| 100 | + if (includeNewest && survivor >= 0) |
| 101 | + out.append(survivor); |
| 102 | + return out; |
| 103 | +} |
| 104 | + |
| 105 | +QString buildMarkdown(const QVector<AcpTimelineEntry> &timeline, |
| 106 | + const QVector<AcpMessage> &messages, |
| 107 | + int upToMessage) |
| 108 | +{ |
| 109 | + QString out; |
| 110 | + int pending = -1; // the current turn's report so far |
| 111 | + auto flush = [&]() { |
| 112 | + if (pending < 0) |
| 113 | + return; |
| 114 | + appendSection(out, QLatin1StringView("Assistant"), |
| 115 | + GoalAgent::nativeGoalDisplayText(messageText(messages.at(pending)))); |
| 116 | + pending = -1; |
| 117 | + }; |
| 118 | + bool found = false; |
| 119 | + for (const AcpTimelineEntry &entry : timeline) { |
| 120 | + const AcpMessage *m = messageAt(entry, messages); |
| 121 | + if (!m) |
| 122 | + continue; |
| 123 | + if (m->role == QLatin1String("user")) { |
| 124 | + flush(); |
| 125 | + appendSection(out, QLatin1StringView("User"), messageText(*m)); |
| 126 | + } else if (isReportCandidate(*m)) { |
| 127 | + pending = entry.messageIndex; |
| 128 | + } |
| 129 | + if (entry.messageIndex == upToMessage) { |
| 130 | + found = true; |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + if (!found) |
| 135 | + return {}; |
| 136 | + flush(); |
| 137 | + return out; |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace AcpConversationCopy |
0 commit comments