langmail-mcp 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +89 -169
- package/dist/chunk-0fxfqny0.js +12642 -0
- package/dist/langmail-mcp.js +7 -0
- package/package.json +93 -98
- package/THIRD_PARTY_NOTICES.md +0 -388
- package/dist/attachments/download.js +0 -69
- package/dist/attachments/routes.js +0 -42
- package/dist/attachments/tickets.js +0 -45
- package/dist/attachments/upload-tickets.js +0 -43
- package/dist/attachments/upload.js +0 -136
- package/dist/caldav/account-settings.js +0 -92
- package/dist/caldav/agenda.js +0 -277
- package/dist/caldav/alarm.js +0 -97
- package/dist/caldav/client.js +0 -408
- package/dist/caldav/ical.js +0 -248
- package/dist/caldav/occurrence.js +0 -534
- package/dist/caldav/recurrence.js +0 -313
- package/dist/caldav/rsvp.js +0 -107
- package/dist/caldav/scheduling-handover.js +0 -22
- package/dist/caldav/timezone.js +0 -103
- package/dist/caldav/types.js +0 -5
- package/dist/config.js +0 -219
- package/dist/contacts/address-class.js +0 -79
- package/dist/contacts/birthdays.js +0 -81
- package/dist/contacts/card.js +0 -58
- package/dist/contacts/constants.js +0 -46
- package/dist/contacts/fold.js +0 -309
- package/dist/contacts/index.js +0 -10
- package/dist/contacts/merge.js +0 -388
- package/dist/contacts/observations.js +0 -115
- package/dist/contacts/profile.js +0 -402
- package/dist/contacts/scoring.js +0 -146
- package/dist/contacts/types.js +0 -7
- package/dist/contacts/upsert.js +0 -131
- package/dist/drafts/attachments.js +0 -254
- package/dist/drafts/idempotency.js +0 -60
- package/dist/drafts/send-guard.js +0 -184
- package/dist/email-render/email-viewer.html +0 -143
- package/dist/email-render/image-proxy.js +0 -389
- package/dist/email-render/meta.js +0 -48
- package/dist/email-render/sanitize.js +0 -93
- package/dist/email-render/viewer-resource.js +0 -104
- package/dist/http/bearer.js +0 -30
- package/dist/http/guard.js +0 -57
- package/dist/http/hosts.js +0 -10
- package/dist/http/listener.js +0 -170
- package/dist/http/routes.js +0 -72
- package/dist/ical/ical-parse.js +0 -357
- package/dist/ical/index.js +0 -4
- package/dist/ical/timezone.js +0 -22
- package/dist/ical/windows-zones.js +0 -155
- package/dist/index.js +0 -79
- package/dist/jmap/blob.js +0 -49
- package/dist/jmap/contacts.js +0 -226
- package/dist/jmap/drafts.js +0 -480
- package/dist/jmap/email.js +0 -668
- package/dist/jmap/index.js +0 -12
- package/dist/jmap/keywords.js +0 -22
- package/dist/jmap/live.js +0 -21
- package/dist/jmap/origin.js +0 -113
- package/dist/jmap/patch.js +0 -29
- package/dist/jmap/request.js +0 -37
- package/dist/jmap/session.js +0 -89
- package/dist/jmap/sieve.js +0 -71
- package/dist/jmap/submission.js +0 -107
- package/dist/jmap/timeout.js +0 -30
- package/dist/jmap/types.js +0 -6
- package/dist/logger.js +0 -48
- package/dist/mcp/handler.js +0 -255
- package/dist/mcp/http.js +0 -29
- package/dist/mcp/instructions.js +0 -44
- package/dist/mcp/links.js +0 -64
- package/dist/mcp/test-client.js +0 -43
- package/dist/mcp/tool-logging.js +0 -67
- package/dist/observability/errors.js +0 -41
- package/dist/rules/apply.js +0 -236
- package/dist/rules/rule.js +0 -280
- package/dist/rules/store.js +0 -92
- package/dist/tools/annotations.js +0 -71
- package/dist/tools/archive-emails.js +0 -40
- package/dist/tools/calendar-lookup.js +0 -64
- package/dist/tools/contact-common.js +0 -146
- package/dist/tools/contact-format.js +0 -72
- package/dist/tools/contact-write-through.js +0 -37
- package/dist/tools/create-calendar-event.js +0 -231
- package/dist/tools/create-calendar.js +0 -54
- package/dist/tools/create-contact.js +0 -172
- package/dist/tools/create-draft.js +0 -369
- package/dist/tools/create-mail-rule.js +0 -101
- package/dist/tools/delete-calendar-event.js +0 -165
- package/dist/tools/delete-contact.js +0 -60
- package/dist/tools/delete-draft.js +0 -129
- package/dist/tools/delete-mail-rule.js +0 -38
- package/dist/tools/download-attachment.js +0 -90
- package/dist/tools/download-email.js +0 -112
- package/dist/tools/draft-common.js +0 -600
- package/dist/tools/email-types.js +0 -58
- package/dist/tools/error-detail.js +0 -10
- package/dist/tools/file-emails-common.js +0 -75
- package/dist/tools/format.js +0 -131
- package/dist/tools/forward-message.js +0 -237
- package/dist/tools/forward-quote.js +0 -179
- package/dist/tools/get-draft.js +0 -61
- package/dist/tools/get-email.js +0 -164
- package/dist/tools/get-thread.js +0 -67
- package/dist/tools/keywords.js +0 -43
- package/dist/tools/list-calendar-events.js +0 -180
- package/dist/tools/list-calendars.js +0 -44
- package/dist/tools/list-drafts.js +0 -134
- package/dist/tools/list-mail-rules.js +0 -28
- package/dist/tools/list-unsubscribe.js +0 -152
- package/dist/tools/list-upcoming-birthdays.js +0 -165
- package/dist/tools/mail-rule-common.js +0 -43
- package/dist/tools/mail-rule-format.js +0 -79
- package/dist/tools/mailbox-lookup.js +0 -13
- package/dist/tools/mark-emails.js +0 -78
- package/dist/tools/merge-contacts.js +0 -145
- package/dist/tools/move-calendar-events.js +0 -340
- package/dist/tools/move-emails.js +0 -64
- package/dist/tools/one-click-post.js +0 -275
- package/dist/tools/preview-email.js +0 -204
- package/dist/tools/preview-mail-rule.js +0 -79
- package/dist/tools/reply-quote.js +0 -426
- package/dist/tools/reply-to-message.js +0 -292
- package/dist/tools/respond-to-event.js +0 -110
- package/dist/tools/result.js +0 -9
- package/dist/tools/search-contacts.js +0 -431
- package/dist/tools/search-emails.js +0 -570
- package/dist/tools/send-draft.js +0 -308
- package/dist/tools/trash-emails.js +0 -36
- package/dist/tools/unsubscribe-from-list.js +0 -249
- package/dist/tools/update-calendar-event.js +0 -973
- package/dist/tools/update-calendar.js +0 -100
- package/dist/tools/update-contact.js +0 -382
- package/dist/tools/update-draft.js +0 -714
- package/dist/tools/update-mail-rule.js +0 -103
- package/dist/tools/upload-attachments.js +0 -126
|
@@ -1,714 +0,0 @@
|
|
|
1
|
-
// update_draft tool — edit a staged draft.
|
|
2
|
-
//
|
|
3
|
-
// The draft id does not change. RFC 8621 makes an Email's content immutable
|
|
4
|
-
// (only `keywords` and `mailboxIds` can be patched), so editing a draft means
|
|
5
|
-
// writing a new message and destroying the old one — see src/jmap
|
|
6
|
-
// `drafts.ts`. The JMAP id therefore changes on every edit, but the replacement
|
|
7
|
-
// carries the draft's stable id as a `$draftid:` keyword (`stableDraftId` in
|
|
8
|
-
// draft-common.ts), so the id create_draft handed out keeps resolving to the
|
|
9
|
-
// current revision in get_draft, update_draft and send_draft.
|
|
10
|
-
//
|
|
11
|
-
// On a reply, `text` and `html` are the new part only: the quoted original is
|
|
12
|
-
// re-attached below them the way create_draft attaches it, if the staged draft
|
|
13
|
-
// carried it — so rewording a reply never drops the conversation it answers.
|
|
14
|
-
//
|
|
15
|
-
// Merge semantics are field-by-field: an omitted field keeps its staged value,
|
|
16
|
-
// a supplied one replaces it, and an empty recipient array clears that field.
|
|
17
|
-
// The sender and the threading headers are not editable — changing who a
|
|
18
|
-
// message is from or which conversation it belongs to is a different message,
|
|
19
|
-
// so stage it with create_draft.
|
|
20
|
-
import { randomUUID } from "node:crypto";
|
|
21
|
-
import { z } from "zod";
|
|
22
|
-
import { attachmentsSchema, resolveAttachments, } from "../drafts/attachments.js";
|
|
23
|
-
import { authorizationHeader, } from "../jmap/index.js";
|
|
24
|
-
import { ACCOUNT_DESTRUCTIVE } from "./annotations.js";
|
|
25
|
-
import { bodyTruncated, draftFromRecord, draftLabelKeywords, draftRefusalReason, formatDraft, forwardOriginalId, forwardOriginalKeyword, isDraft, isTrashed, loadDraft, MAX_DRAFT_BODY_VALUE_BYTES, parseRecipients, replyOriginalId, replyOriginalKeyword, restoreFromTrashHint, stableDraftId, stableDraftIdKeyword, unreferencableAttachments, } from "./draft-common.js";
|
|
26
|
-
import { carriesForwardBlock, forwardedBody, originalAsAttachment, } from "./forward-quote.js";
|
|
27
|
-
import { carriesQuoteOf, htmlToText, quoteWithheldFrom, quoteWithheldNote, textAsHtml, withQuotedOriginal, } from "./reply-quote.js";
|
|
28
|
-
import { errorResult, textResult } from "./result.js";
|
|
29
|
-
const MAX_RECIPIENTS = 50;
|
|
30
|
-
const MAX_BODY_CHARS = 100_000;
|
|
31
|
-
const addressListSchema = (field) => z
|
|
32
|
-
.array(z.string().min(3).max(320))
|
|
33
|
-
.max(MAX_RECIPIENTS)
|
|
34
|
-
.optional()
|
|
35
|
-
.describe(`Replace the ${field} recipients. Omit to keep them; pass an empty array to clear them. Each entry ` +
|
|
36
|
-
'is a bare address (person@example.com) or the "Name <person@example.com>" form.');
|
|
37
|
-
export const updateDraftInput = z.object({
|
|
38
|
-
attachments: attachmentsSchema
|
|
39
|
-
.optional()
|
|
40
|
-
.describe(`${attachmentsSchema.description} Omit to keep the files already attached; pass an empty array to ` +
|
|
41
|
-
"remove them all. Supplying the field replaces the whole list, so to add one more file, list the " +
|
|
42
|
-
"existing ones by the blobIds get_draft reports alongside the new one."),
|
|
43
|
-
bcc: addressListSchema("Bcc"),
|
|
44
|
-
cc: addressListSchema("Cc"),
|
|
45
|
-
draftId: z
|
|
46
|
-
.string()
|
|
47
|
-
.min(1)
|
|
48
|
-
.describe("The draft id, as returned by create_draft or list_drafts — edits do not change it — or a message id " +
|
|
49
|
-
'from search_emails with `mailbox: "Drafts"`. Editing a draft found that way returns the draftId to use from then on.'),
|
|
50
|
-
html: z
|
|
51
|
-
.string()
|
|
52
|
-
.max(MAX_BODY_CHARS)
|
|
53
|
-
.optional()
|
|
54
|
-
.describe("Replace the HTML body. On a reply, write only the new part: the quoted original is re-attached below " +
|
|
55
|
-
"it, so do not paste the quote from get_draft back in. Omit to keep the body."),
|
|
56
|
-
quoteOriginal: z
|
|
57
|
-
.boolean()
|
|
58
|
-
.optional()
|
|
59
|
-
.describe("On a reply, with `text` or `html`: whether the original is quoted below the new text. Defaults to " +
|
|
60
|
-
"whatever the staged draft does — unless the same call adds a `to`/`cc`/`bcc` recipient the " +
|
|
61
|
-
"original was not addressed to, where quoting would forward it to them, so the quote is left off. " +
|
|
62
|
-
"Pass false to drop the quote, true to add it to a bare reply or to quote it for a new recipient."),
|
|
63
|
-
subject: z
|
|
64
|
-
.string()
|
|
65
|
-
.max(500)
|
|
66
|
-
.optional()
|
|
67
|
-
.describe("Replace the subject. Omit to keep it."),
|
|
68
|
-
text: z
|
|
69
|
-
.string()
|
|
70
|
-
.max(MAX_BODY_CHARS)
|
|
71
|
-
.optional()
|
|
72
|
-
.describe("Replace the plain-text body. On a reply, write only the new part: the quoted original is " +
|
|
73
|
-
"re-attached below it, so do not paste the quote from get_draft back in. Omit to keep the body."),
|
|
74
|
-
to: addressListSchema("To"),
|
|
75
|
-
});
|
|
76
|
-
// Which body part would be left holding old content, if any.
|
|
77
|
-
//
|
|
78
|
-
// The test is on the *merged* result, not on what was staged: an html-only draft
|
|
79
|
-
// given a new `text` also ends up with both parts, one of them stale — and it is
|
|
80
|
-
// the html one, which is what clients render. Checking the staged draft for
|
|
81
|
-
// "has both" missed exactly that case.
|
|
82
|
-
const staleBodyPart = (staged, args) => {
|
|
83
|
-
const mergedHasText = args.text !== undefined || staged.text !== undefined;
|
|
84
|
-
const mergedHasHtml = args.html !== undefined || staged.html !== undefined;
|
|
85
|
-
if (!(mergedHasText && mergedHasHtml)) {
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
if (args.text !== undefined && args.html === undefined) {
|
|
89
|
-
return { stale: "html", supplied: "text" };
|
|
90
|
-
}
|
|
91
|
-
if (args.html !== undefined && args.text === undefined) {
|
|
92
|
-
return { stale: "text", supplied: "html" };
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
};
|
|
96
|
-
// Whether the caller asked for any change at all. A bare `draftId` would
|
|
97
|
-
// otherwise replace and destroy the draft for nothing, running the
|
|
98
|
-
// replacement's failure modes for no benefit.
|
|
99
|
-
const editsNothing = (args) => args.to === undefined &&
|
|
100
|
-
args.cc === undefined &&
|
|
101
|
-
args.bcc === undefined &&
|
|
102
|
-
args.subject === undefined &&
|
|
103
|
-
args.text === undefined &&
|
|
104
|
-
args.html === undefined &&
|
|
105
|
-
args.attachments === undefined;
|
|
106
|
-
// The attachment list the replacement message should carry, or the reason one
|
|
107
|
-
// could not be built.
|
|
108
|
-
//
|
|
109
|
-
// Attachments merge like every other field: an omitted `attachments` keeps what
|
|
110
|
-
// is staged — which costs nothing, because the staged draft carries the existing
|
|
111
|
-
// blobIds and the re-create just re-references them — while a supplied list
|
|
112
|
-
// replaces the old one wholesale. `[]` goes down the supplied branch, which is
|
|
113
|
-
// what makes it mean "take the files off" rather than "leave them alone".
|
|
114
|
-
const mergedAttachments = async (deps, p) => p.args.attachments === undefined
|
|
115
|
-
? { attachments: p.staged.attachments }
|
|
116
|
-
: await resolveAttachments(deps, {
|
|
117
|
-
accountId: p.accountId,
|
|
118
|
-
authorization: p.authorization,
|
|
119
|
-
entries: p.args.attachments,
|
|
120
|
-
});
|
|
121
|
-
// The staged draft, or the reason it cannot be edited at all.
|
|
122
|
-
//
|
|
123
|
-
// The edit is a rewrite followed by a destroy, so anything the rewrite could
|
|
124
|
-
// not carry across would be lost for good — and reported as a success. Every
|
|
125
|
-
// refusal here therefore runs *before* the replacement is written, while the
|
|
126
|
-
// draft is still whole and nothing has changed.
|
|
127
|
-
const loadEditableDraft = async (deps, p) => {
|
|
128
|
-
const { args } = p;
|
|
129
|
-
const record = await loadDraft(deps, {
|
|
130
|
-
accountId: p.accountId,
|
|
131
|
-
authorization: p.authorization,
|
|
132
|
-
draftId: args.draftId,
|
|
133
|
-
});
|
|
134
|
-
if (!record) {
|
|
135
|
-
return {
|
|
136
|
-
error: errorResult(`Draft not found: ${args.draftId}. Find it with list_drafts.`),
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
if (!isDraft(record)) {
|
|
140
|
-
return {
|
|
141
|
-
error: errorResult(`${args.draftId} is not a draft — it has already been sent or received, and cannot be edited. Read ` +
|
|
142
|
-
`it with get_email, passing id ${record.id}.`),
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
// Re-staging would put the discarded draft back into Drafts behind the
|
|
146
|
-
// user's back.
|
|
147
|
-
if (isTrashed(record, p.mailboxes)) {
|
|
148
|
-
return {
|
|
149
|
-
error: errorResult(`Draft ${args.draftId} is in Trash — it was discarded, so it was left unchanged. ` +
|
|
150
|
-
restoreFromTrashHint(record)),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
// A body that was truncated on the way in would be truncated on the way out.
|
|
154
|
-
if (bodyTruncated(record)) {
|
|
155
|
-
return {
|
|
156
|
-
error: errorResult(`Draft ${args.draftId} has a body too large to edit without risking losing part of it, so it was left ` +
|
|
157
|
-
"unchanged. Stage a fresh draft with create_draft instead."),
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
// An attachment the replacement could not reference would be dropped by the
|
|
161
|
-
// rewrite. Skipped when the caller is replacing the attachment list anyway:
|
|
162
|
-
// they are not relying on the old files surviving.
|
|
163
|
-
const unreferencable = args.attachments === undefined ? unreferencableAttachments(record) : [];
|
|
164
|
-
if (unreferencable.length > 0) {
|
|
165
|
-
return {
|
|
166
|
-
error: errorResult(`Draft ${args.draftId} carries a file this tool cannot carry across an edit ` +
|
|
167
|
-
`(${unreferencable.join(", ")}), so it was left unchanged. Editing rewrites the message and destroys ` +
|
|
168
|
-
"the original, which would lose the file for good. Edit it in the user's mail client, or pass `attachments` " +
|
|
169
|
-
"explicitly to say what the edited draft should carry."),
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
return { record };
|
|
173
|
-
};
|
|
174
|
-
// The tail of an attribution line ("On …, Name <address> wrote:") in either
|
|
175
|
-
// body form — the end of a text line, `\r?` because `$` does not match before
|
|
176
|
-
// a CR, or the end of the `<div>` the HTML quote wraps it in — as a sign a body
|
|
177
|
-
// quotes something when the quoted message itself cannot be read.
|
|
178
|
-
const QUOTE_ATTRIBUTION_RE = / wrote:(?:\r?$|<\/div>)/mu;
|
|
179
|
-
// Quoted history in either body form: a `>`-prefixed line or a blockquote.
|
|
180
|
-
const FOREIGN_QUOTE_RE = /^>|<blockquote\b/imu;
|
|
181
|
-
// Both body parts, so a test finds a quote in whichever one carries it.
|
|
182
|
-
const bothParts = (draft) => `${draft.text ?? ""}\n${draft.html ?? ""}`;
|
|
183
|
-
// The message a reply draft answers: the one create_draft recorded, failing
|
|
184
|
-
// that — a reply staged by another client — the one the thread holds, which it
|
|
185
|
-
// does only while the subject is unchanged. Resolved on every edit of a reply,
|
|
186
|
-
// not only on body edits, so it can be recorded on the replacement before a
|
|
187
|
-
// subject edit moves the reply out of that thread for good.
|
|
188
|
-
//
|
|
189
|
-
// A candidate counts only if its Message-ID is the one the draft's In-Reply-To
|
|
190
|
-
// names: a recorded id outlives the message it named, and should the server
|
|
191
|
-
// hand it to another message, quoting that one would put unrelated mail into
|
|
192
|
-
// the user's outgoing reply.
|
|
193
|
-
const replyOriginal = async (deps, p) => {
|
|
194
|
-
const answers = p.staged.inReplyTo?.[0];
|
|
195
|
-
if (!answers) {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
const read = async (id) => {
|
|
199
|
-
const candidate = await deps.emailComposeGet({
|
|
200
|
-
accountId: p.accountId,
|
|
201
|
-
apiUrl: "",
|
|
202
|
-
authorization: p.authorization,
|
|
203
|
-
bodyValues: "body",
|
|
204
|
-
id,
|
|
205
|
-
maxBodyValueBytes: MAX_DRAFT_BODY_VALUE_BYTES,
|
|
206
|
-
});
|
|
207
|
-
return candidate?.messageId?.includes(answers) ? candidate : null;
|
|
208
|
-
};
|
|
209
|
-
const recorded = replyOriginalId(p.record);
|
|
210
|
-
const fromRecorded = recorded ? await read(recorded) : null;
|
|
211
|
-
if (fromRecorded) {
|
|
212
|
-
return fromRecorded;
|
|
213
|
-
}
|
|
214
|
-
// From here on the recorded id, if there was one, has been read and is
|
|
215
|
-
// missing or names a different message, so a tolerated failure must not
|
|
216
|
-
// carry it forward again.
|
|
217
|
-
const disproved = (error) => {
|
|
218
|
-
throw recorded ? new RecordedOriginalDisproved(error) : error;
|
|
219
|
-
};
|
|
220
|
-
const inThread = await deps
|
|
221
|
-
.threadMessageFind({
|
|
222
|
-
accountId: p.accountId,
|
|
223
|
-
apiUrl: "",
|
|
224
|
-
authorization: p.authorization,
|
|
225
|
-
emailId: p.record.id,
|
|
226
|
-
messageId: answers,
|
|
227
|
-
})
|
|
228
|
-
.catch(disproved);
|
|
229
|
-
if (!(inThread && inThread !== recorded)) {
|
|
230
|
-
return null;
|
|
231
|
-
}
|
|
232
|
-
return await read(inThread).catch(disproved);
|
|
233
|
-
};
|
|
234
|
-
// A lookup that failed after the recorded original id was read and found to be
|
|
235
|
-
// missing or to name a different message — that id must not be recorded again.
|
|
236
|
-
class RecordedOriginalDisproved extends Error {
|
|
237
|
-
constructor(cause) {
|
|
238
|
-
super("the recorded original no longer matches, and the thread lookup failed", { cause });
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
// What a failed lookup of the original costs depends on the edit. One that
|
|
242
|
-
// rewrites the body needs the original to quote, and one that changes the
|
|
243
|
-
// subject moves the reply out of the thread that could still find it — both
|
|
244
|
-
// fail loudly. Any other edit goes ahead, keeping whatever id was already
|
|
245
|
-
// recorded — the next edit can verify it, or find the original in the thread.
|
|
246
|
-
//
|
|
247
|
-
// `id` is what the replacement records; `record` is the original itself, when
|
|
248
|
-
// it was read and verified.
|
|
249
|
-
const replyOriginalForEdit = (deps, p) => replyOriginal(deps, p).then((original) => ({ id: original?.id ?? null, record: original }), (error) => {
|
|
250
|
-
if (p.args.subject !== undefined ||
|
|
251
|
-
p.args.text !== undefined ||
|
|
252
|
-
p.args.html !== undefined) {
|
|
253
|
-
throw error;
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
id: error instanceof RecordedOriginalDisproved
|
|
257
|
-
? null
|
|
258
|
-
: replyOriginalId(p.record),
|
|
259
|
-
record: null,
|
|
260
|
-
};
|
|
261
|
-
});
|
|
262
|
-
// Every address that is the user — the authenticated principal, the draft's
|
|
263
|
-
// sender, and every identity the account can send as — as `resolveIdentity`
|
|
264
|
-
// counts them (see `ResolvedIdentity.selfAddresses`). Read only when an edit
|
|
265
|
-
// puts the quote in front of recipients that have not been checked against the
|
|
266
|
-
// original — one that adds recipients to a quoted reply, or one that pastes the
|
|
267
|
-
// quote onto a reply that did not carry it.
|
|
268
|
-
const selfAddressesOf = async (deps, p) => {
|
|
269
|
-
const identities = await deps.identityGet({
|
|
270
|
-
accountId: await deps.resolveSubmissionAccountId(p.authorization),
|
|
271
|
-
apiUrl: "",
|
|
272
|
-
authorization: p.authorization,
|
|
273
|
-
});
|
|
274
|
-
return [
|
|
275
|
-
p.principal,
|
|
276
|
-
...p.staged.from.map((address) => address.email),
|
|
277
|
-
...identities.map((identity) => identity.email),
|
|
278
|
-
];
|
|
279
|
-
};
|
|
280
|
-
// Someone this edit adds who the original was never addressed to, or null.
|
|
281
|
-
// When the staged body already carried the quote, only an added recipient
|
|
282
|
-
// counts: everyone already on the draft was allowed to see the original, and
|
|
283
|
-
// re-passing them is not a new decision. When it did not — the quote arrives
|
|
284
|
-
// in the caller's own text — nobody on the draft has seen it yet, so every
|
|
285
|
-
// recipient is checked.
|
|
286
|
-
const outsiderShownQuote = async (deps, p) => {
|
|
287
|
-
// With the quote already staged, an edit that touches no recipients leaves
|
|
288
|
-
// the disclosure where it was. Without it, the quote is new to everyone.
|
|
289
|
-
if (p.stagedCarriedQuote &&
|
|
290
|
-
p.args.to === undefined &&
|
|
291
|
-
p.args.cc === undefined &&
|
|
292
|
-
p.args.bcc === undefined) {
|
|
293
|
-
return null;
|
|
294
|
-
}
|
|
295
|
-
const alreadyNamed = p.stagedCarriedQuote
|
|
296
|
-
? [...(p.staged.to ?? []), ...(p.staged.cc ?? []), ...(p.staged.bcc ?? [])]
|
|
297
|
-
: [];
|
|
298
|
-
return quoteWithheldFrom(p.original, p.recipients, [
|
|
299
|
-
...(await selfAddressesOf(deps, p)),
|
|
300
|
-
...alreadyNamed.map((address) => address.email),
|
|
301
|
-
]);
|
|
302
|
-
};
|
|
303
|
-
// What to say when the original cannot be read, so no quote can be re-attached.
|
|
304
|
-
// Only worth saying when a quote was wanted or looks to have been there — in
|
|
305
|
-
// this tool's format or another client's — since a bare reply never had one to
|
|
306
|
-
// lose.
|
|
307
|
-
const missingOriginalNote = (args, staged) => {
|
|
308
|
-
const body = bothParts(staged);
|
|
309
|
-
const wanted = args.quoteOriginal === true ||
|
|
310
|
-
QUOTE_ATTRIBUTION_RE.test(body) ||
|
|
311
|
-
FOREIGN_QUOTE_RE.test(body);
|
|
312
|
-
return wanted
|
|
313
|
-
? {
|
|
314
|
-
note: "The message this draft replies to could not be found, so no quote was re-attached: the body is " +
|
|
315
|
-
"exactly the text given.",
|
|
316
|
-
}
|
|
317
|
-
: {};
|
|
318
|
-
};
|
|
319
|
-
// The note for quoted text going to someone the original was never addressed to.
|
|
320
|
-
// Dropping that recipient again works on every path, and is the only remedy for
|
|
321
|
-
// a body another mail client wrote: the agent cannot read it back whole, so it
|
|
322
|
-
// has no "own wording" to pass. The other remedy needs both halves. Without `quoteOriginal: false`, the staged body
|
|
323
|
-
// this edit writes — quote included — makes the next edit's default re-attach
|
|
324
|
-
// it; and `quoteOriginal: false` on a text that still holds the quote stages it
|
|
325
|
-
// verbatim.
|
|
326
|
-
const quotedTextNote = (outsider) => `The draft carries quoted text from the original, and ${outsider} was not on it, so that now goes to them ` +
|
|
327
|
-
"too. To take it off, drop that recipient again, or pass your own wording again — without the quoted " +
|
|
328
|
-
"part — with `quoteOriginal: false`.";
|
|
329
|
-
// An edit that leaves the body alone re-stages it as it is, quote included —
|
|
330
|
-
// the same as changing the recipients of a quoted reply in a mail client. When
|
|
331
|
-
// that edit adds someone the original was never addressed to, the quote now
|
|
332
|
-
// goes to them too: say so, since the agent did not ask for it in so many
|
|
333
|
-
// words, and say how to take it off.
|
|
334
|
-
const stagedQuoteNote = async (deps, p) => {
|
|
335
|
-
// Any quoted history counts — another client's quote as much as one this
|
|
336
|
-
// tool wrote. The note is advisory, so a hand-typed `>` line costs a spare
|
|
337
|
-
// sentence, never a changed body. Without the original there is no roster
|
|
338
|
-
// to compare against, so nothing is said.
|
|
339
|
-
const { original } = p;
|
|
340
|
-
if (!(original &&
|
|
341
|
-
(carriesQuoteOf(p.staged, original) ||
|
|
342
|
-
FOREIGN_QUOTE_RE.test(bothParts(p.staged))))) {
|
|
343
|
-
return {};
|
|
344
|
-
}
|
|
345
|
-
const outsider = await outsiderShownQuote(deps, {
|
|
346
|
-
...p,
|
|
347
|
-
original,
|
|
348
|
-
stagedCarriedQuote: true,
|
|
349
|
-
});
|
|
350
|
-
return outsider
|
|
351
|
-
? {
|
|
352
|
-
note: quotedTextNote(outsider),
|
|
353
|
-
}
|
|
354
|
-
: {};
|
|
355
|
-
};
|
|
356
|
-
// The body the edit stages: what the caller supplied, with the original quoted
|
|
357
|
-
// below it when the draft is a reply that quotes it (or the caller asks for the
|
|
358
|
-
// quote). `note` says when a quote could not be re-attached, so the agent learns
|
|
359
|
-
// the reply now goes out without it.
|
|
360
|
-
//
|
|
361
|
-
// The default follows the staged draft rather than the recipient rule
|
|
362
|
-
// create_draft applies: the quoting decision was made when the reply was staged
|
|
363
|
-
// — by the user, when they asked for a quote despite a new recipient — and an
|
|
364
|
-
// edit of the wording is not a reason to revisit it. An edit that also changes
|
|
365
|
-
// the recipients is: re-attaching the quote for someone the original was never
|
|
366
|
-
// addressed to would forward it to them, so that takes `quoteOriginal: true`,
|
|
367
|
-
// as it does on create_draft. An edit that leaves the body alone keeps the
|
|
368
|
-
// staged quote as it is and only says so — see `stagedQuoteNote`.
|
|
369
|
-
const editedBody = async (deps, p) => {
|
|
370
|
-
const { args, original } = p;
|
|
371
|
-
const supplied = {
|
|
372
|
-
...(args.text === undefined ? {} : { text: args.text }),
|
|
373
|
-
...(args.html === undefined ? {} : { html: args.html }),
|
|
374
|
-
};
|
|
375
|
-
if (args.text === undefined && args.html === undefined) {
|
|
376
|
-
return { body: supplied, ...(await stagedQuoteNote(deps, p)) };
|
|
377
|
-
}
|
|
378
|
-
if (!p.staged.inReplyTo?.[0] || args.quoteOriginal === false) {
|
|
379
|
-
return { body: supplied };
|
|
380
|
-
}
|
|
381
|
-
if (!original) {
|
|
382
|
-
return { body: supplied, ...missingOriginalNote(args, p.staged) };
|
|
383
|
-
}
|
|
384
|
-
// A caller who passed the whole body, quote included, gets it as written
|
|
385
|
-
// rather than quoted twice.
|
|
386
|
-
if (carriesQuoteOf(supplied, original)) {
|
|
387
|
-
// The quote came in the caller's own text, so it goes out as written — but
|
|
388
|
-
// an added outsider is told about, as on a recipients-only edit.
|
|
389
|
-
const outsider = args.quoteOriginal === undefined
|
|
390
|
-
? await outsiderShownQuote(deps, {
|
|
391
|
-
...p,
|
|
392
|
-
original,
|
|
393
|
-
stagedCarriedQuote: carriesQuoteOf(p.staged, original),
|
|
394
|
-
})
|
|
395
|
-
: null;
|
|
396
|
-
return outsider
|
|
397
|
-
? { body: supplied, note: quotedTextNote(outsider) }
|
|
398
|
-
: { body: supplied };
|
|
399
|
-
}
|
|
400
|
-
if (!(args.quoteOriginal ?? carriesQuoteOf(p.staged, original))) {
|
|
401
|
-
// A quote in a format this tool did not write — another mail client's —
|
|
402
|
-
// goes with the old wording. Say so, since the description promised the
|
|
403
|
-
// quote would be kept.
|
|
404
|
-
const foreignQuote = args.quoteOriginal === undefined &&
|
|
405
|
-
FOREIGN_QUOTE_RE.test(bothParts(p.staged));
|
|
406
|
-
return {
|
|
407
|
-
body: supplied,
|
|
408
|
-
...(foreignQuote
|
|
409
|
-
? {
|
|
410
|
-
note: "The staged body held quoted text in a format this tool did not write, and it was replaced along " +
|
|
411
|
-
"with the old wording. Pass `quoteOriginal: true` with the text to quote the original below it.",
|
|
412
|
-
}
|
|
413
|
-
: {}),
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
const outsider = args.quoteOriginal === undefined
|
|
417
|
-
? await outsiderShownQuote(deps, {
|
|
418
|
-
...p,
|
|
419
|
-
original,
|
|
420
|
-
stagedCarriedQuote: true,
|
|
421
|
-
})
|
|
422
|
-
: null;
|
|
423
|
-
if (outsider) {
|
|
424
|
-
return { body: supplied, note: quoteWithheldNote(outsider) };
|
|
425
|
-
}
|
|
426
|
-
return { body: withQuotedOriginal(supplied, original) };
|
|
427
|
-
};
|
|
428
|
-
// A body given in one form, in both: the part left out is derived from the one
|
|
429
|
-
// given, so the two alternatives cannot disagree.
|
|
430
|
-
const inBothParts = (body) => ({
|
|
431
|
-
html: body.html ?? textAsHtml(body.text ?? ""),
|
|
432
|
-
text: body.text ?? htmlToText(body.html ?? ""),
|
|
433
|
-
});
|
|
434
|
-
// The body an edit of a forward's note stages: the new note, with the
|
|
435
|
-
// forwarded block rebuilt below it from the original, so the recipient still
|
|
436
|
-
// gets the original's headers and body — both parts, whichever one the note
|
|
437
|
-
// came in. `attach` is the whole original as a `message/rfc822` file, when the
|
|
438
|
-
// rebuilt body no longer holds it in full and it is not attached yet.
|
|
439
|
-
const forwardEditedBody = async (deps, p) => {
|
|
440
|
-
const { args, staged } = p;
|
|
441
|
-
const supplied = {
|
|
442
|
-
...(args.text === undefined ? {} : { text: args.text }),
|
|
443
|
-
...(args.html === undefined ? {} : { html: args.html }),
|
|
444
|
-
};
|
|
445
|
-
if (args.text === undefined && args.html === undefined) {
|
|
446
|
-
return { body: {} };
|
|
447
|
-
}
|
|
448
|
-
// The caller passed the whole body back, block included: keep it as written,
|
|
449
|
-
// and derive the part they left out from it so both say the same thing.
|
|
450
|
-
if (carriesForwardBlock(supplied)) {
|
|
451
|
-
return { body: inBothParts(supplied) };
|
|
452
|
-
}
|
|
453
|
-
const candidate = await deps.emailComposeGet({
|
|
454
|
-
accountId: p.accountId,
|
|
455
|
-
apiUrl: "",
|
|
456
|
-
authorization: p.authorization,
|
|
457
|
-
bodyValues: "body",
|
|
458
|
-
id: p.originalId,
|
|
459
|
-
maxBodyValueBytes: MAX_DRAFT_BODY_VALUE_BYTES,
|
|
460
|
-
});
|
|
461
|
-
// Only the message whose Message-ID the forward references: a recorded id
|
|
462
|
-
// that the server handed to another message must not put that one in the
|
|
463
|
-
// user's outgoing mail. An original with no Message-ID left the forward no
|
|
464
|
-
// References to check, so it is taken as long as it still has none.
|
|
465
|
-
const references = staged.references ?? [];
|
|
466
|
-
const ids = candidate?.messageId ?? [];
|
|
467
|
-
const verified = references.length === 0
|
|
468
|
-
? ids.length === 0
|
|
469
|
-
: ids.some((id) => references.includes(id));
|
|
470
|
-
const original = candidate && verified ? candidate : null;
|
|
471
|
-
if (!original) {
|
|
472
|
-
return {
|
|
473
|
-
body: inBothParts(supplied),
|
|
474
|
-
note: "The forwarded message could not be found any more, so the forwarded block was not re-attached: the " +
|
|
475
|
-
"body is exactly the note given. The files already attached are kept.",
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
const rebuilt = forwardedBody(supplied, original);
|
|
479
|
-
const whole = rebuilt.attachOriginal ? originalAsAttachment(original) : null;
|
|
480
|
-
// By type, not blobId: a re-staged draft's parts carry blobIds of the draft's
|
|
481
|
-
// own blob, so the original's blobId never matches what reads back.
|
|
482
|
-
const alreadyAttached = (staged.attachments ?? []).some((file) => file.type === "message/rfc822");
|
|
483
|
-
return {
|
|
484
|
-
body: { html: rebuilt.html, text: rebuilt.text },
|
|
485
|
-
...(whole && !alreadyAttached && args.attachments === undefined
|
|
486
|
-
? { attach: whole }
|
|
487
|
-
: {}),
|
|
488
|
-
};
|
|
489
|
-
};
|
|
490
|
-
// Why the arguments alone describe no edit, if they don't — checked before
|
|
491
|
-
// anything is read.
|
|
492
|
-
const argumentRefusal = (args) => {
|
|
493
|
-
if (args.quoteOriginal !== undefined &&
|
|
494
|
-
args.text === undefined &&
|
|
495
|
-
args.html === undefined) {
|
|
496
|
-
return errorResult("`quoteOriginal` only applies together with a new `text` or `html`: pass the new wording as well. " +
|
|
497
|
-
`Draft ${args.draftId} was left unchanged.`);
|
|
498
|
-
}
|
|
499
|
-
if (editsNothing(args)) {
|
|
500
|
-
return errorResult(`No changes were given, so draft ${args.draftId} was left exactly as it is. Pass at least one of ` +
|
|
501
|
-
"to/cc/bcc/subject/text/html/attachments to edit it.");
|
|
502
|
-
}
|
|
503
|
-
return null;
|
|
504
|
-
};
|
|
505
|
-
// The result of a landed edit.
|
|
506
|
-
const updatedSummary = (p) => {
|
|
507
|
-
// A create that landed while the destroy didn't leaves the superseded copy in
|
|
508
|
-
// the folder, holding the *pre-edit* content. The draftId resolves to the new
|
|
509
|
-
// revision (see `loadDraft`), but the user sees two drafts in their client, so
|
|
510
|
-
// the note says which one is stale.
|
|
511
|
-
const leftover = p.created.replacedDestroyed === false
|
|
512
|
-
? `\nNote: the pre-edit copy (message ${p.replacedId}) could not be removed and is still in Drafts. ` +
|
|
513
|
-
`send_draft with draftId ${p.draftId} sends the edited version. Do not send ${p.replacedId}; with the ` +
|
|
514
|
-
"user's OK, remove it with trash_emails."
|
|
515
|
-
: "";
|
|
516
|
-
const note = p.note ? `\n${p.note}` : "";
|
|
517
|
-
// A caller who came in with a revision's message id (from search_emails) is
|
|
518
|
-
// told the id that keeps working; one who held the draftId already is told it
|
|
519
|
-
// still does.
|
|
520
|
-
const heading = p.args.draftId === p.draftId
|
|
521
|
-
? "Draft updated and still unsent — the draftId is unchanged."
|
|
522
|
-
: `Draft updated and still unsent. Its draftId is ${p.draftId}; use that from now on.`;
|
|
523
|
-
return `${heading}\ndraftId: ${p.draftId}\n${formatDraft(p.merged, { threadId: p.created.threadId })}${note}${leftover}`;
|
|
524
|
-
};
|
|
525
|
-
// Why the body this edit supplies cannot be staged on this draft, if it can't.
|
|
526
|
-
const bodyRefusal = (args, staged, forwardOf) => {
|
|
527
|
-
if (forwardOf && args.quoteOriginal !== undefined) {
|
|
528
|
-
return errorResult(`\`quoteOriginal\` applies to replies, and draft ${args.draftId} is a forward: the forwarded message is ` +
|
|
529
|
-
"always kept below the note. The draft was left unchanged. Pass only the new `text`/`html` note.");
|
|
530
|
-
}
|
|
531
|
-
// Replacing one body part while the other survives is how a stale body gets
|
|
532
|
-
// delivered: mail clients render the html alternative, so an html draft
|
|
533
|
-
// "corrected" via `text` alone goes out saying the old thing. Both parts or
|
|
534
|
-
// neither. A forward's note is exempt: both parts are rebuilt from it.
|
|
535
|
-
const stalePart = forwardOf ? null : staleBodyPart(staged, args);
|
|
536
|
-
if (stalePart) {
|
|
537
|
-
return errorResult(`This draft has both a plain-text and an HTML body, and only \`${stalePart.supplied}\` was given — the ` +
|
|
538
|
-
`\`${stalePart.stale}\` version would keep its old content, and that is the one most mail clients ` +
|
|
539
|
-
"show. Pass both `text` and `html`, or stage a fresh draft with create_draft. Nothing was changed.");
|
|
540
|
-
}
|
|
541
|
-
return null;
|
|
542
|
-
};
|
|
543
|
-
// The keywords the replacement carries. The user's own labels survive the edit
|
|
544
|
-
// — an edit shouldn't quietly untag a draft they filed. The reply's or the
|
|
545
|
-
// forward's original rides along so the next edit can rebuild below it (and a
|
|
546
|
-
// send can mark a forwarded one), and the stable id is what keeps the caller's
|
|
547
|
-
// draftId pointing at this revision.
|
|
548
|
-
const revisionKeywords = (record, p) => [
|
|
549
|
-
...draftLabelKeywords(record),
|
|
550
|
-
...(p.replyOriginal ? [replyOriginalKeyword(p.replyOriginal)] : []),
|
|
551
|
-
...(p.forwardOf ? [forwardOriginalKeyword(p.forwardOf)] : []),
|
|
552
|
-
stableDraftIdKeyword(p.draftId),
|
|
553
|
-
];
|
|
554
|
-
// The replacement: the staged draft with every supplied field laid over it.
|
|
555
|
-
const mergedDraft = (staged, p) => {
|
|
556
|
-
const files = [
|
|
557
|
-
...(p.attachments ?? []),
|
|
558
|
-
...(p.edited.attach ? [p.edited.attach] : []),
|
|
559
|
-
];
|
|
560
|
-
return {
|
|
561
|
-
...staged,
|
|
562
|
-
...p.recipients,
|
|
563
|
-
...(p.args.subject === undefined ? {} : { subject: p.args.subject }),
|
|
564
|
-
...p.edited.body,
|
|
565
|
-
...(p.attachments === undefined && !p.edited.attach
|
|
566
|
-
? {}
|
|
567
|
-
: { attachments: files }),
|
|
568
|
-
};
|
|
569
|
-
};
|
|
570
|
-
export const updateDraft = async (deps, ctx) => {
|
|
571
|
-
const { args } = ctx;
|
|
572
|
-
const refused = argumentRefusal(args);
|
|
573
|
-
if (refused) {
|
|
574
|
-
return refused;
|
|
575
|
-
}
|
|
576
|
-
const authorization = authorizationHeader(ctx.token);
|
|
577
|
-
const accountId = await deps.resolveAccountId(authorization);
|
|
578
|
-
const mailboxes = await deps.resolveComposeMailboxes(authorization);
|
|
579
|
-
const loaded = await loadEditableDraft(deps, {
|
|
580
|
-
accountId,
|
|
581
|
-
args,
|
|
582
|
-
authorization,
|
|
583
|
-
mailboxes,
|
|
584
|
-
});
|
|
585
|
-
if ("error" in loaded) {
|
|
586
|
-
return loaded.error;
|
|
587
|
-
}
|
|
588
|
-
const { record } = loaded;
|
|
589
|
-
const staged = draftFromRecord(record);
|
|
590
|
-
const forwardOf = forwardOriginalId(record);
|
|
591
|
-
const bodyRefused = bodyRefusal(args, staged, forwardOf);
|
|
592
|
-
if (bodyRefused) {
|
|
593
|
-
return bodyRefused;
|
|
594
|
-
}
|
|
595
|
-
const recipients = parseRecipients({
|
|
596
|
-
...(args.to ? { to: args.to } : {}),
|
|
597
|
-
...(args.cc ? { cc: args.cc } : {}),
|
|
598
|
-
...(args.bcc ? { bcc: args.bcc } : {}),
|
|
599
|
-
});
|
|
600
|
-
if ("error" in recipients) {
|
|
601
|
-
return errorResult(recipients.error);
|
|
602
|
-
}
|
|
603
|
-
if (!mailboxes.drafts) {
|
|
604
|
-
return errorResult("This account has no Drafts folder, so the draft cannot be re-staged.");
|
|
605
|
-
}
|
|
606
|
-
const attachments = await mergedAttachments(deps, {
|
|
607
|
-
accountId,
|
|
608
|
-
args,
|
|
609
|
-
authorization,
|
|
610
|
-
staged,
|
|
611
|
-
});
|
|
612
|
-
if ("error" in attachments) {
|
|
613
|
-
return errorResult(attachments.error);
|
|
614
|
-
}
|
|
615
|
-
// Recorded by create_draft, or found in the thread just now — kept on the
|
|
616
|
-
// replacement either way, so the next edit does not have to search again.
|
|
617
|
-
const resolvedOriginal = await replyOriginalForEdit(deps, {
|
|
618
|
-
accountId,
|
|
619
|
-
args,
|
|
620
|
-
authorization,
|
|
621
|
-
record,
|
|
622
|
-
staged,
|
|
623
|
-
});
|
|
624
|
-
const mergedRecipients = {
|
|
625
|
-
to: recipients.to ?? staged.to ?? [],
|
|
626
|
-
cc: recipients.cc ?? staged.cc ?? [],
|
|
627
|
-
bcc: recipients.bcc ?? staged.bcc ?? [],
|
|
628
|
-
};
|
|
629
|
-
const edited = forwardOf
|
|
630
|
-
? await forwardEditedBody(deps, {
|
|
631
|
-
accountId,
|
|
632
|
-
args,
|
|
633
|
-
authorization,
|
|
634
|
-
originalId: forwardOf,
|
|
635
|
-
staged,
|
|
636
|
-
})
|
|
637
|
-
: await editedBody(deps, {
|
|
638
|
-
args,
|
|
639
|
-
authorization,
|
|
640
|
-
original: resolvedOriginal.record,
|
|
641
|
-
principal: ctx.principal,
|
|
642
|
-
recipients: mergedRecipients,
|
|
643
|
-
staged,
|
|
644
|
-
});
|
|
645
|
-
const merged = mergedDraft(staged, {
|
|
646
|
-
args,
|
|
647
|
-
attachments: attachments.attachments,
|
|
648
|
-
edited,
|
|
649
|
-
recipients,
|
|
650
|
-
});
|
|
651
|
-
// A draft another client staged gets its token now — see `stableDraftId`.
|
|
652
|
-
const draftId = stableDraftId(record) ?? randomUUID();
|
|
653
|
-
let created;
|
|
654
|
-
try {
|
|
655
|
-
created = await deps.draftCreate({
|
|
656
|
-
accountId,
|
|
657
|
-
apiUrl: "",
|
|
658
|
-
authorization,
|
|
659
|
-
draft: merged,
|
|
660
|
-
mailboxId: mailboxes.drafts,
|
|
661
|
-
keywords: revisionKeywords(record, {
|
|
662
|
-
draftId,
|
|
663
|
-
forwardOf,
|
|
664
|
-
replyOriginal: resolvedOriginal.id,
|
|
665
|
-
}),
|
|
666
|
-
replaces: record.id,
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
catch (error) {
|
|
670
|
-
const refusal = draftRefusalReason(error, merged);
|
|
671
|
-
if (refusal === null) {
|
|
672
|
-
throw error;
|
|
673
|
-
}
|
|
674
|
-
// The destroy only runs once the replacement exists (src/jmap
|
|
675
|
-
// drafts.ts), so a refused create leaves the original exactly as it was.
|
|
676
|
-
return errorResult(`${refusal} Draft ${args.draftId} was left unchanged.`);
|
|
677
|
-
}
|
|
678
|
-
return textResult(updatedSummary({
|
|
679
|
-
args,
|
|
680
|
-
created,
|
|
681
|
-
draftId,
|
|
682
|
-
merged,
|
|
683
|
-
note: edited.note,
|
|
684
|
-
replacedId: record.id,
|
|
685
|
-
}));
|
|
686
|
-
};
|
|
687
|
-
// --- MCP registration ----------------------------------------------------
|
|
688
|
-
export const registerUpdateDraft = (server, deps,
|
|
689
|
-
// The credential and the principal are read per call — see mcp/handler.ts.
|
|
690
|
-
getToken, getPrincipal) => {
|
|
691
|
-
server.registerTool("update_draft", {
|
|
692
|
-
annotations: ACCOUNT_DESTRUCTIVE,
|
|
693
|
-
description: "Edit a staged draft that has not been sent. Omitted fields keep their staged values; an empty " +
|
|
694
|
-
"recipient or `attachments` array clears that field. Files already attached are carried across " +
|
|
695
|
-
"untouched unless `attachments` is given, in which case the list is replaced wholesale — read the " +
|
|
696
|
-
"existing blobIds back with get_draft first if you mean to add one rather than swap them. The " +
|
|
697
|
-
"draftId stays the same across edits, so keep using the one create_draft returned. On a reply, " +
|
|
698
|
-
"`text`/`html` is the new part only: the quoted original is kept below it (`quoteOriginal: false` " +
|
|
699
|
-
"drops it, `true` adds one to a bare reply). On a forward staged with create_draft(`forwardOf`), " +
|
|
700
|
-
"`text`/`html` is the note only: the forwarded message is rebuilt below it. The previous version is not recoverable. If the draft has both a plain-text and an HTML body, pass " +
|
|
701
|
-
"both `text` and `html` or neither; replacing one would leave the other saying the old thing. The " +
|
|
702
|
-
"sender and the reply threading cannot be changed here; stage a new draft with create_draft instead, then retire this one " +
|
|
703
|
-
"with delete_draft.",
|
|
704
|
-
inputSchema: updateDraftInput,
|
|
705
|
-
title: "Update draft",
|
|
706
|
-
}, (args) => {
|
|
707
|
-
const parsed = updateDraftInput.parse(args);
|
|
708
|
-
return updateDraft(deps, {
|
|
709
|
-
args: parsed,
|
|
710
|
-
principal: getPrincipal(),
|
|
711
|
-
token: getToken(),
|
|
712
|
-
});
|
|
713
|
-
});
|
|
714
|
-
};
|