Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: Quick Reply drafts now carry the original sender and reply subject (#10717)

Adam Eivy4e7281c952ee
diff --git a/server/routes/messages.js b/server/routes/messages.js
index f6495041ee..d8901eb7e1 100644
--- a/server/routes/messages.js
+++ b/server/routes/messages.js
@@ -7,7 +7,7 @@ import {
messageActionBodySchema, messageTokenProviderBodySchema,
} from '../lib/validation.js';
import { UUID_RE } from '../lib/fileUtils.js';
-import { sendViaForAccountType } from '../lib/messageTransport.js';
+import { MESSAGE_ACCOUNT_TRANSPORT, sendViaForAccountType } from '../lib/messageTransport.js';
import * as messageAccounts from '../services/messageAccounts.js';
import * as messageSync from '../services/messageSync.js';
import * as messageDrafts from '../services/messageDrafts.js';
@@ -223,6 +223,11 @@ router.post('/drafts', asyncHandler(async (req, res) => {
res.status(201).json(draft);
}));
+const replySubject = (subject) => {
+ const trimmed = String(subject ?? '').trim();
+ return /^re:/i.test(trimmed) ? trimmed : `Re: ${trimmed}`.trim();
+};
+
router.post('/drafts/generate', asyncHandler(async (req, res) => {
const data = validateRequest(generateDraftSchema, req.body);
const account = await messageAccounts.getAccount(data.accountId);
@@ -230,21 +235,29 @@ router.post('/drafts/generate', asyncHandler(async (req, res) => {
// Fetch the original message to build AI reply
let replyBody = '';
+ let to = [];
+ let subject = '';
if (data.replyToMessageId) {
const originalMsg = await messageSync.getMessage(data.accountId, data.replyToMessageId);
- if (originalMsg) {
- // Load thread context if available
- let threadMessages = null;
- if (data.threadId) {
- threadMessages = await messageSync.getThread(data.accountId, data.threadId).catch(() => null);
- }
- const aiResult = await generateReplyBody(originalMsg, data.instructions, {
- useVoice: data.useVoice,
- threadMessages
- });
- if (!aiResult?.body?.trim()) throw new ServerError('The selected model did not produce a reply draft. Check Models > LLMs > Abuse Guard.', { status: 422, code: 'UNTRUSTED_REPLY_UNAVAILABLE' });
- replyBody = aiResult.body;
+ if (!originalMsg) throw new ServerError('The message being replied to was not found', { status: 404, code: 'REPLY_ORIGINAL_NOT_FOUND' });
+ // Email replies need explicit delivery fields; Teams/browser chats target a conversation, not a mailbox.
+ if (MESSAGE_ACCOUNT_TRANSPORT[account.type]?.email) {
+ const sender = originalMsg.from?.email;
+ if (!sender) throw new ServerError('The original message has no sender address to reply to', { status: 422, code: 'REPLY_TARGET_UNAVAILABLE' });
+ to = [sender];
+ subject = replySubject(originalMsg.subject);
+ }
+ // Load thread context if available
+ let threadMessages = null;
+ if (data.threadId) {
+ threadMessages = await messageSync.getThread(data.accountId, data.threadId).catch(() => null);
}
+ const aiResult = await generateReplyBody(originalMsg, data.instructions, {
+ useVoice: data.useVoice,
+ threadMessages
+ });
+ if (!aiResult?.body?.trim()) throw new ServerError('The selected model did not produce a reply draft. Check Models > LLMs > Abuse Guard.', { status: 422, code: 'UNTRUSTED_REPLY_UNAVAILABLE' });
+ replyBody = aiResult.body;
}
if (!replyBody) {
replyBody = `[No original message found]\n\nContext: ${data.context}\nInstructions: ${data.instructions}`;
@@ -254,7 +267,8 @@ router.post('/drafts/generate', asyncHandler(async (req, res) => {
accountId: data.accountId,
replyToMessageId: data.replyToMessageId,
threadId: data.threadId,
- subject: '',
+ to,
+ subject,
body: replyBody,
generatedBy: 'ai',
sendVia: sendViaForAccountType(account.type) ?? 'playwright'
diff --git a/server/routes/messages.test.js b/server/routes/messages.test.js
index 0833954d2f..acdde91867 100644
--- a/server/routes/messages.test.js
+++ b/server/routes/messages.test.js
@@ -495,7 +495,7 @@ describe('Messages Routes', () => {
const emit = vi.fn();
app.set('io', { emit });
messageAccounts.getAccount.mockResolvedValue({ id: VALID_UUID, type: 'gmail' });
- messageSync.getMessage.mockResolvedValue({ id: 'message-example', bodyText: 'Example message.' });
+ messageSync.getMessage.mockResolvedValue({ id: 'message-example', bodyText: 'Example message.', from: { email: '[email protected]' } });
generateReplyBody.mockRejectedValue(new ServerError('Configure a local API provider in Models > LLMs > Abuse Guard.', { status: 422, code: 'untrusted-content-provider-unavailable' }));
const response = await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'message-example' });
expect(response.status).toBe(422);
@@ -505,6 +505,43 @@ describe('Messages Routes', () => {
expect(emit).not.toHaveBeenCalledWith('messages:draft:created', expect.anything());
});
+ it('addresses a generated email reply to the original sender with a normalized subject', async () => {
+ messageAccounts.getAccount.mockResolvedValue({ id: VALID_UUID, type: 'gmail' });
+ messageSync.getMessage.mockResolvedValue({ id: 'message-example', subject: 'Lunch?', from: { email: '[email protected]' }, bodyText: 'Example.' });
+ generateReplyBody.mockResolvedValue({ body: 'Synthetic generated reply' });
+ messageDrafts.createDraft.mockImplementation(async (d) => ({ id: DRAFT_UUID, ...d }));
+ const response = await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'message-example' });
+ expect(response.status).toBe(201);
+ expect(messageDrafts.createDraft).toHaveBeenCalledWith(expect.objectContaining({ to: ['[email protected]'], subject: 'Re: Lunch?', body: 'Synthetic generated reply' }));
+
+ messageSync.getMessage.mockResolvedValue({ id: 'message-example', subject: 'RE: Lunch?', from: { email: '[email protected]' } });
+ await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'message-example' });
+ expect(messageDrafts.createDraft).toHaveBeenLastCalledWith(expect.objectContaining({ subject: 'RE: Lunch?' }));
+ });
+
+ it('leaves delivery fields empty for Teams replies', async () => {
+ messageAccounts.getAccount.mockResolvedValue({ id: VALID_UUID, type: 'teams' });
+ messageSync.getMessage.mockResolvedValue({ id: 'message-example', subject: 'Chat', from: { email: '[email protected]' } });
+ generateReplyBody.mockResolvedValue({ body: 'Synthetic generated reply' });
+ messageDrafts.createDraft.mockImplementation(async (d) => ({ id: DRAFT_UUID, ...d }));
+ await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'message-example' });
+ expect(messageDrafts.createDraft).toHaveBeenCalledWith(expect.objectContaining({ to: [], subject: '' }));
+ });
+
+ it('returns typed errors and creates no draft when the original or its sender is unavailable', async () => {
+ messageAccounts.getAccount.mockResolvedValue({ id: VALID_UUID, type: 'gmail' });
+ messageSync.getMessage.mockResolvedValue(null);
+ let response = await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'missing' });
+ expect(response.status).toBe(404);
+ expect(response.body.code).toBe('REPLY_ORIGINAL_NOT_FOUND');
+
+ messageSync.getMessage.mockResolvedValue({ id: 'm', subject: 'Hi', from: {} });
+ response = await request(app).post('/api/messages/drafts/generate').send({ accountId: VALID_UUID, replyToMessageId: 'm' });
+ expect(response.status).toBe(422);
+ expect(response.body.code).toBe('REPLY_TARGET_UNAVAILABLE');
+ expect(messageDrafts.createDraft).not.toHaveBeenCalled();
+ });
+
it('should return 404 if account not found', async () => {
messageAccounts.getAccount.mockResolvedValue(null);