Skip to content

Commit f411f04

Browse files
feat(ai): copy ACP turns; set Claude config dir
Users need clean prompt/report markdown from finished turns without thoughts, tools, system lines, or goal-judge noise. Point the usage chip at another Claude config folder so multi-account setups track the correct credentials.
1 parent 9cad890 commit f411f04

21 files changed

Lines changed: 767 additions & 2 deletions

‎src/AcpConversationCopy.cpp‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

‎src/AcpConversationCopy.h‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#ifndef ACP_CONVERSATION_COPY_H
20+
#define ACP_CONVERSATION_COPY_H
21+
22+
#include <QString>
23+
#include <QVector>
24+
25+
#include "AcpSessionModel.h"
26+
27+
// "Copy response" / "Copy conversation" under an assistant bubble. Both walk
28+
// the timeline (not the message vector: a finished goal-judge turn leaves its
29+
// messages behind with no timeline entry).
30+
//
31+
// A turn runs from a user prompt to the next one (an agent-only prefix is a
32+
// turn too). Its final report is its LAST assistant message with visible text;
33+
// goal-judge output is never a report. Thoughts, tool calls, system lines and
34+
// interim replies are not part of the conversation.
35+
//
36+
// Complexity: O(n) over the timeline, one pass, no per-entry allocation beyond
37+
// the output.
38+
namespace AcpConversationCopy {
39+
40+
// Visible text of a message: text blocks joined, images as "[image]".
41+
QString messageText(const AcpMessage &msg);
42+
43+
// Message indices of each turn's final report, in timeline order. The newest
44+
// turn counts only when includeNewest is set (it has ended).
45+
QVector<int> finalReports(const QVector<AcpTimelineEntry> &timeline,
46+
const QVector<AcpMessage> &messages,
47+
bool includeNewest);
48+
49+
// Markdown transcript from the start through the timeline entry of message
50+
// upToMessage (inclusive; its turn counts as ended): every prompt under
51+
// "## User" and each turn's report under "## Assistant". Empty when that
52+
// message is not on the timeline.
53+
QString buildMarkdown(const QVector<AcpTimelineEntry> &timeline,
54+
const QVector<AcpMessage> &messages,
55+
int upToMessage);
56+
57+
} // namespace AcpConversationCopy
58+
59+
#endif // ACP_CONVERSATION_COPY_H

‎src/ApplicationSettings.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ CREATE_SETTING(Ai, CommitMessageGenerateShortcut, commitMessageGenerateShortcut,
189189

190190
CREATE_SETTING(Ai, SyncWorkspaceOnAiSwitch, syncWorkspaceOnAiSwitch, bool, true)
191191
CREATE_SETTING(Ai, UsagePercentRemaining, usagePercentRemaining, bool, false)
192+
CREATE_SETTING(Ai, UsageClaudeConfigDir, usageClaudeConfigDir, QString, QString())
192193
// NOLINTEND(bugprone-throwing-static-initialization,performance-unnecessary-value-param)
193194

194195
// --- AI / ACP agent settings ---------------------------------------------------

‎src/ApplicationSettings.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ public slots:
225225
// Storage key Ai/UsagePercentRemaining.
226226
DEFINE_SETTING(UsagePercentRemaining, usagePercentRemaining, bool)
227227

228+
// Claude config folder the usage chip reads `.credentials.json` from.
229+
// Empty = $CLAUDE_CONFIG_DIR, else ~/.claude. Storage key Ai/UsageClaudeConfigDir.
230+
DEFINE_SETTING(UsageClaudeConfigDir, usageClaudeConfigDir, QString)
231+
228232
// AI / ACP agent settings. Stored under the "Ai/" group.
229233
public:
230234
QString defaultAiAgentId() const;

‎src/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ qt_add_executable(NotepadAI
66
AcpAgentPicker.h
77
AcpAgentRegistry.h
88
AcpConnection.h
9+
AcpConversationCopy.h
910
AcpErrorClassifier.h
1011
AcpHistoryStore.h
1112
AcpPromptQueue.h
@@ -126,6 +127,7 @@ qt_add_executable(NotepadAI
126127
AcpAgentManager.cpp
127128
AcpAgentRegistry.cpp
128129
AcpConnection.cpp
130+
AcpConversationCopy.cpp
129131
AcpErrorClassifier.cpp
130132
AcpHistoryStore.cpp
131133
AcpPromptQueue.cpp

‎src/ai/ClaudeUsageService.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ void ClaudeUsageService::refresh(bool force)
151151
readKeychainAsync();
152152
}
153153

154+
void ClaudeUsageService::setConfigDir(const QString &dir)
155+
{
156+
m_credentialsFile = claudeusage::credentialsPath(
157+
dir.isEmpty() ? qEnvironmentVariable("CLAUDE_CONFIG_DIR") : dir, QDir::homePath());
158+
159+
m_transport->cancel();
160+
m_inflight = false;
161+
m_usage = {};
162+
m_health = {};
163+
m_loaded = false;
164+
m_lastCompletedMs = 0;
165+
refresh(true);
166+
}
167+
154168
void ClaudeUsageService::sendWithToken(const QString &token)
155169
{
156170
m_transport->get({usageUrl(), token});

‎src/ai/ClaudeUsageService.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class ClaudeUsageService : public QObject
106106
void startPolling();
107107

108108
void setClock(std::function<qint64()> clock) { m_clock = std::move(clock); }
109+
// Switches to another Claude config folder (`dir` empty = $CLAUDE_CONFIG_DIR /
110+
// ~/.claude). It is a different account, so the snapshot, the cooldown and
111+
// any request in flight are dropped and a fresh fetch starts right away.
112+
void setConfigDir(const QString &dir);
109113
// 0..1 source for the backoff jitter; tests pin it.
110114
void setJitter(std::function<double()> jitter) { m_jitter = std::move(jitter); }
111115

‎src/dialogs/MainWindow.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,13 @@ MainWindow::MainWindow(NotepadNextApplication *app) :
15931593
cornerLayout->setContentsMargins(0, 0, 0, 0);
15941594
cornerLayout->setSpacing(0);
15951595

1596-
auto *usageService = new ai::ClaudeUsageService(new ai::ClaudeUsageHttpTransport(this), QString(), this);
1596+
const QString usageConfigDir = app->getSettings()->usageClaudeConfigDir();
1597+
auto *usageService = new ai::ClaudeUsageService(
1598+
new ai::ClaudeUsageHttpTransport(this),
1599+
usageConfigDir.isEmpty() ? QString() : ai::claudeusage::credentialsPath(usageConfigDir, QDir::homePath()),
1600+
this);
1601+
connect(app->getSettings(), &ApplicationSettings::usageClaudeConfigDirChanged, usageService,
1602+
&ai::ClaudeUsageService::setConfigDir);
15971603
auto *usageButton = new UsageButton(usageService, cornerHost);
15981604
usageButton->setPercentMode(app->getSettings()->usagePercentRemaining()
15991605
? ai::claudeusage::PercentMode::Remaining

‎src/dialogs/PreferencesDialog.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,29 @@ PreferencesDialog::PreferencesDialog(ApplicationSettings *settings, QWidget *par
402402
ui->spinBoxTerminalMemoryLimit->setValue(settings->terminalChildMemoryLimitGb());
403403
});
404404

405+
// --- Claude usage config folder --------------------------------------------
406+
407+
ui->lineEditClaudeConfigDir->setText(QDir::toNativeSeparators(settings->usageClaudeConfigDir()));
408+
connect(ui->lineEditClaudeConfigDir, &QLineEdit::textChanged, this, [=](const QString &) {
409+
m_pending.markDirty();
410+
});
411+
connect(ui->btnBrowseClaudeConfigDir, &QToolButton::clicked, this, [this]() {
412+
const QString dir = QFileDialog::getExistingDirectory(
413+
this, tr("Claude Config Folder"), ui->lineEditClaudeConfigDir->text());
414+
if (!dir.isEmpty())
415+
ui->lineEditClaudeConfigDir->setText(QDir::toNativeSeparators(dir));
416+
});
417+
m_pending.addApply([=]() {
418+
// The setter always emits, and a change drops the usage snapshot: skip no-ops.
419+
const QString dir = QDir::fromNativeSeparators(ui->lineEditClaudeConfigDir->text().trimmed());
420+
if (dir != settings->usageClaudeConfigDir())
421+
settings->setUsageClaudeConfigDir(dir);
422+
});
423+
m_pending.addRevert([=]() {
424+
QSignalBlocker blocker(ui->lineEditClaudeConfigDir);
425+
ui->lineEditClaudeConfigDir->setText(QDir::toNativeSeparators(settings->usageClaudeConfigDir()));
426+
});
427+
405428
// --- Data Directory section ------------------------------------------------
406429

407430
ui->lineEditDataDir->setText(QDir::toNativeSeparators(DataPaths::appDataLocation()));

‎src/dialogs/PreferencesDialog.ui‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,46 @@
386386
</layout>
387387
</widget>
388388
</item>
389+
<item>
390+
<widget class="QGroupBox" name="groupBoxClaudeUsage">
391+
<property name="title">
392+
<string>Claude Usage</string>
393+
</property>
394+
<layout class="QFormLayout" name="formLayoutClaudeUsage">
395+
<item row="0" column="0">
396+
<widget class="QLabel" name="labelClaudeConfigDir">
397+
<property name="text">
398+
<string>Config folder</string>
399+
</property>
400+
<property name="toolTip">
401+
<string>Folder holding .credentials.json for the usage indicator. Leave empty for $CLAUDE_CONFIG_DIR or ~/.claude.</string>
402+
</property>
403+
</widget>
404+
</item>
405+
<item row="0" column="1">
406+
<layout class="QHBoxLayout" name="layoutClaudeConfigDir">
407+
<item>
408+
<widget class="QLineEdit" name="lineEditClaudeConfigDir">
409+
<property name="placeholderText">
410+
<string>Default (~/.claude)</string>
411+
</property>
412+
<property name="clearButtonEnabled">
413+
<bool>true</bool>
414+
</property>
415+
</widget>
416+
</item>
417+
<item>
418+
<widget class="QToolButton" name="btnBrowseClaudeConfigDir">
419+
<property name="text">
420+
<string>...</string>
421+
</property>
422+
</widget>
423+
</item>
424+
</layout>
425+
</item>
426+
</layout>
427+
</widget>
428+
</item>
389429
<item>
390430
<widget class="QGroupBox" name="groupBoxTerminal">
391431
<property name="title">

0 commit comments

Comments
 (0)