@mindstone/mcp-server-microsoft-mail 0.2.0 → 0.4.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.
@@ -194,6 +194,7 @@ export const COMPOSE_EMAIL_HTML = `
194
194
  justify-content: flex-end;
195
195
  gap: 10px;
196
196
  margin-top: 2px;
197
+ flex-wrap: wrap;
197
198
  }
198
199
 
199
200
  .button {
@@ -367,6 +368,7 @@ export const COMPOSE_EMAIL_HTML = `
367
368
 
368
369
  <div class="toggles">
369
370
  <button id="toggleCcButton" type="button" class="link-button" aria-expanded="false" aria-controls="ccRow">Add CC</button>
371
+ <button id="toggleBccButton" type="button" class="link-button" aria-expanded="false" aria-controls="bccRow">Add BCC</button>
370
372
  </div>
371
373
 
372
374
  <div id="ccRow" class="field hidden">
@@ -374,6 +376,11 @@ export const COMPOSE_EMAIL_HTML = `
374
376
  <input id="ccInput" class="input" type="text" autocomplete="off" placeholder="cc@example.com">
375
377
  </div>
376
378
 
379
+ <div id="bccRow" class="field hidden">
380
+ <label for="bccInput">BCC</label>
381
+ <input id="bccInput" class="input" type="text" autocomplete="off" placeholder="bcc@example.com">
382
+ </div>
383
+
377
384
  <div class="field">
378
385
  <label for="subjectInput">Subject</label>
379
386
  <input id="subjectInput" class="input" type="text" autocomplete="off" placeholder="Email subject">
@@ -398,6 +405,10 @@ export const COMPOSE_EMAIL_HTML = `
398
405
 
399
406
  <div class="actions">
400
407
  <button id="cancelButton" type="button" class="button button-secondary">Cancel</button>
408
+ <button id="draftButton" type="button" class="button button-secondary">
409
+ <span id="draftSpinner" class="spinner hidden" aria-hidden="true"></span>
410
+ <span id="draftLabel">Save draft</span>
411
+ </button>
401
412
  <button id="sendButton" type="button" class="button button-primary">
402
413
  <span id="sendSpinner" class="spinner hidden" aria-hidden="true"></span>
403
414
  <span id="sendLabel">Send email</span>
@@ -426,6 +437,11 @@ export const COMPOSE_EMAIL_HTML = `
426
437
  <span id="sentCc" class="sent-value"></span>
427
438
  </div>
428
439
 
440
+ <div id="sentBccField" class="field hidden">
441
+ <label>BCC</label>
442
+ <span id="sentBcc" class="sent-value"></span>
443
+ </div>
444
+
429
445
  <div class="field">
430
446
  <label>Subject</label>
431
447
  <span id="sentSubject" class="sent-value"></span>
@@ -454,11 +470,21 @@ export const COMPOSE_EMAIL_HTML = `
454
470
  // already reviewed before clicking Send.
455
471
  var draftApplied = false;
456
472
  var pendingRequestId = null;
457
- var pendingPayload = null;
473
+ var pendingSendPayload = null;
474
+ // Retain across the local timeout so even a late reply clears its payload.
475
+ var pendingSendRequestId = null;
458
476
  var sending = false;
477
+ var hostSending = false;
459
478
  var collapsed = false;
460
- var outcomeUnknown = false;
479
+ var sendMayHaveGone = false;
461
480
  var sent = false;
481
+ // Which action the in-flight request belongs to ('send' | 'draft').
482
+ // Captured before clearing on reply/timeout so the response handler can
483
+ // route error/success correctly. retryAction survives the reply so a
484
+ // later Retry click still knows which tool to re-invoke.
485
+ var pendingAction = null;
486
+ var retryAction = null;
487
+ var sendBlocked = false;
462
488
  var sendTimeoutId = null;
463
489
  // Just beyond the host-side tool-call timeout (60s) so a legitimately slow
464
490
  // send resolves to its real result first; this only fires when the reply
@@ -476,22 +502,30 @@ export const COMPOSE_EMAIL_HTML = `
476
502
  var sentTo = document.getElementById('sentTo');
477
503
  var sentCcField = document.getElementById('sentCcField');
478
504
  var sentCc = document.getElementById('sentCc');
505
+ var sentBccField = document.getElementById('sentBccField');
506
+ var sentBcc = document.getElementById('sentBcc');
479
507
  var sentSubject = document.getElementById('sentSubject');
480
508
  var sentBody = document.getElementById('sentBody');
481
509
  var sentCollapseButton = document.getElementById('sentCollapseButton');
482
510
 
483
511
  var toInput = document.getElementById('toInput');
484
512
  var ccInput = document.getElementById('ccInput');
513
+ var bccInput = document.getElementById('bccInput');
485
514
  var subjectInput = document.getElementById('subjectInput');
486
515
  var bodyInput = document.getElementById('bodyInput');
487
516
  var fromValue = document.getElementById('fromValue');
488
517
  var fromHelper = document.getElementById('fromHelper');
489
518
  var ccRow = document.getElementById('ccRow');
519
+ var bccRow = document.getElementById('bccRow');
490
520
  var toggleCcButton = document.getElementById('toggleCcButton');
521
+ var toggleBccButton = document.getElementById('toggleBccButton');
491
522
 
492
523
  var sendButton = document.getElementById('sendButton');
493
524
  var sendLabel = document.getElementById('sendLabel');
494
525
  var sendSpinner = document.getElementById('sendSpinner');
526
+ var draftButton = document.getElementById('draftButton');
527
+ var draftLabel = document.getElementById('draftLabel');
528
+ var draftSpinner = document.getElementById('draftSpinner');
495
529
  var cancelButton = document.getElementById('cancelButton');
496
530
  var retryButton = document.getElementById('retryButton');
497
531
 
@@ -528,7 +562,7 @@ export const COMPOSE_EMAIL_HTML = `
528
562
  } else {
529
563
  fromValue.textContent = 'Account not shown';
530
564
  fromValue.style.color = 'var(--muted)';
531
- fromHelper.textContent = 'Rebel could not confirm the sending account. Cancel and ask Rebel to recreate the draft before sending.';
565
+ fromHelper.textContent = 'Couldn\\'t confirm which email account to send from. Cancel, then ask your assistant to recreate this draft.';
532
566
  fromHelper.classList.remove('hidden');
533
567
  }
534
568
  }
@@ -612,11 +646,16 @@ export const COMPOSE_EMAIL_HTML = `
612
646
  }
613
647
 
614
648
  function setSending(nextSending) {
649
+ // Only a host outcome can release another copy's in-flight send.
650
+ nextSending = nextSending || hostSending;
615
651
  sending = nextSending;
616
- sendButton.disabled = nextSending;
652
+ sendButton.disabled = nextSending || sendBlocked;
653
+ retryButton.disabled = nextSending;
654
+ draftButton.disabled = nextSending;
617
655
  cancelButton.disabled = nextSending;
618
656
  toInput.disabled = nextSending;
619
657
  ccInput.disabled = nextSending;
658
+ bccInput.disabled = nextSending;
620
659
  subjectInput.disabled = nextSending;
621
660
  bodyInput.disabled = nextSending;
622
661
  sendSpinner.classList.toggle('hidden', !nextSending);
@@ -625,12 +664,35 @@ export const COMPOSE_EMAIL_HTML = `
625
664
  postResize();
626
665
  }
627
666
 
667
+ // One local request is in flight at a time, but a send in another
668
+ // copy can overlap this draft save and must keep the controls locked.
669
+ function setSavingDraft(nextSaving) {
670
+ sending = nextSaving;
671
+ sendButton.disabled = nextSaving || sendBlocked;
672
+ draftButton.disabled = nextSaving;
673
+ cancelButton.disabled = nextSaving;
674
+ toInput.disabled = nextSaving;
675
+ ccInput.disabled = nextSaving;
676
+ bccInput.disabled = nextSaving;
677
+ subjectInput.disabled = nextSaving;
678
+ bodyInput.disabled = nextSaving;
679
+ sendSpinner.classList.add('hidden');
680
+ sendLabel.textContent = 'Send email';
681
+ sendButton.setAttribute('aria-busy', 'false');
682
+ draftSpinner.classList.toggle('hidden', !nextSaving);
683
+ draftLabel.textContent = nextSaving ? 'Saving…' : 'Save draft';
684
+ draftButton.setAttribute('aria-busy', nextSaving ? 'true' : 'false');
685
+ if (hostSending) setSending(true);
686
+ postResize();
687
+ }
688
+
628
689
  function clearError() {
629
690
  errorText.textContent = '';
630
691
  errorBox.classList.add('hidden');
631
692
  }
632
693
 
633
694
  function showError(message) {
695
+ retryButton.classList.toggle('hidden', sendBlocked && retryAction !== 'draft');
634
696
  errorText.textContent = message || 'Failed to send email.';
635
697
  errorBox.classList.remove('hidden');
636
698
  postResize();
@@ -655,10 +717,10 @@ export const COMPOSE_EMAIL_HTML = `
655
717
  }
656
718
 
657
719
  function clearUnknown() {
658
- outcomeUnknown = false;
659
720
  unknownTitle.textContent = '';
660
721
  unknownText.textContent = '';
661
722
  unknownBox.classList.add('hidden');
723
+ errorBox.appendChild(retryButton);
662
724
  }
663
725
 
664
726
  // No reply ever came back for the send (e.g. the host's response was lost
@@ -667,35 +729,63 @@ export const COMPOSE_EMAIL_HTML = `
667
729
  // tell the user to verify before resending (the send is not idempotent, so
668
730
  // a blind retry could send a duplicate).
669
731
  function showSendUnknown() {
670
- outcomeUnknown = true;
732
+ sendMayHaveGone = true;
671
733
  clearError();
672
734
  clearSuccess();
673
735
  unknownTitle.textContent = 'Not sure if this sent.';
674
- unknownText.textContent = "Rebel didn't hear back in time. Check your Sent folder before sending again, so it doesn't go out twice.";
736
+ unknownText.textContent = "No confirmation came back in time. Check your Sent folder before sending again, so it doesn't go out twice.";
675
737
  unknownBox.classList.remove('hidden');
738
+ if (!sendBlocked) unknownBox.appendChild(retryButton);
676
739
  postResize();
677
740
  }
678
741
 
679
742
  function handleSendTimeout() {
680
743
  sendTimeoutId = null;
681
- if (!pendingRequestId) {
744
+ if (!pendingRequestId || sent) {
682
745
  return;
683
746
  }
684
747
  pendingRequestId = null;
748
+ if (pendingAction === 'draft') {
749
+ pendingAction = null;
750
+ setSavingDraft(false);
751
+ showDraftSaveUnknown();
752
+ return;
753
+ }
754
+ pendingAction = null;
685
755
  setSending(false);
686
756
  showSendUnknown();
687
757
  }
688
758
 
759
+ // Same lost-reply honesty as a send, lower stakes: a blind retry here
760
+ // could create a duplicate draft, so we say we're not sure and point at
761
+ // the Drafts folder instead of offering Retry.
762
+ function showDraftSaveUnknown() {
763
+ clearError();
764
+ clearSuccess();
765
+ unknownTitle.textContent = 'Not sure if the draft was saved.';
766
+ unknownText.textContent =
767
+ "No confirmation came back in time. Check your Drafts folder before trying again, so you don't create a duplicate.";
768
+ unknownBox.classList.remove('hidden');
769
+ postResize();
770
+ }
771
+
689
772
  function setCcVisible(visible) {
690
773
  ccRow.classList.toggle('hidden', !visible);
691
774
  toggleCcButton.textContent = visible ? 'Hide CC' : 'Add CC';
692
775
  toggleCcButton.setAttribute('aria-expanded', visible ? 'true' : 'false');
693
776
  }
694
777
 
778
+ function setBccVisible(visible) {
779
+ bccRow.classList.toggle('hidden', !visible);
780
+ toggleBccButton.textContent = visible ? 'Hide BCC' : 'Add BCC';
781
+ toggleBccButton.setAttribute('aria-expanded', visible ? 'true' : 'false');
782
+ }
783
+
695
784
  function readFormPayload() {
696
785
  var payload = {
697
786
  to: normalizeAddressList(toInput.value),
698
787
  cc: normalizeAddressList(ccInput.value),
788
+ bcc: normalizeAddressList(bccInput.value),
699
789
  subject: String(subjectInput.value || ''),
700
790
  body: String(bodyInput.value || '')
701
791
  };
@@ -718,9 +808,28 @@ export const COMPOSE_EMAIL_HTML = `
718
808
  return null;
719
809
  }
720
810
 
811
+ // Same provider-compatible minimum as sending (the draft tools require
812
+ // To, Subject, and Body too) with save-specific copy. Draft is
813
+ // email-mode only, so this body is the email shape unconditionally.
814
+ function validateDraftPayload(payload) {
815
+ if (!payload.to || payload.to.length === 0) {
816
+ return 'Add at least one recipient in To.';
817
+ }
818
+ if (!trimString(payload.subject)) {
819
+ return 'Add a subject before saving.';
820
+ }
821
+ if (!trimString(payload.body)) {
822
+ return 'Add an email body before saving.';
823
+ }
824
+ return null;
825
+ }
826
+
721
827
  function sendPayload(payload) {
722
- pendingPayload = payload;
828
+ pendingSendPayload = payload;
723
829
  pendingRequestId = 'send-' + Date.now() + '-' + Math.random().toString(16).slice(2);
830
+ pendingSendRequestId = pendingRequestId;
831
+ pendingAction = 'send';
832
+ retryAction = null;
724
833
  setSending(true);
725
834
  clearError();
726
835
  clearSuccess();
@@ -734,6 +843,32 @@ export const COMPOSE_EMAIL_HTML = `
734
843
  method: 'tools/call',
735
844
  params: {
736
845
  name: 'send_email',
846
+ _meta: { 'mcp-app/cardOutcome': 'sent' },
847
+ arguments: payload
848
+ },
849
+ id: pendingRequestId
850
+ },
851
+ '*'
852
+ );
853
+ }
854
+
855
+ function saveDraftPayload(payload) {
856
+ pendingRequestId = 'draft-' + Date.now() + '-' + Math.random().toString(16).slice(2);
857
+ pendingAction = 'draft';
858
+ retryAction = null;
859
+ setSavingDraft(true);
860
+ clearError();
861
+ clearSuccess();
862
+ clearUnknown();
863
+ clearSendTimeout();
864
+ sendTimeoutId = setTimeout(handleSendTimeout, SEND_TIMEOUT_MS);
865
+
866
+ window.parent.postMessage(
867
+ {
868
+ jsonrpc: '2.0',
869
+ method: 'tools/call',
870
+ params: {
871
+ name: 'create_draft',
737
872
  arguments: payload
738
873
  },
739
874
  id: pendingRequestId
@@ -746,11 +881,13 @@ export const COMPOSE_EMAIL_HTML = `
746
881
  var draft = rawDraft && typeof rawDraft === 'object' ? rawDraft : {};
747
882
  toInput.value = listToInputValue(draft.to);
748
883
  ccInput.value = listToInputValue(draft.cc);
884
+ bccInput.value = listToInputValue(draft.bcc);
749
885
  subjectInput.value = typeof draft.subject === 'string' ? draft.subject : '';
750
886
  bodyInput.value = typeof draft.body === 'string' ? draft.body : '';
751
887
  currentEmail = typeof draft.email === 'string' ? draft.email : '';
752
888
  applyFromValue(currentEmail);
753
889
  setCcVisible(normalizeAddressList(draft.cc).length > 0);
890
+ setBccVisible(normalizeAddressList(draft.bcc).length > 0);
754
891
  clearError();
755
892
  clearSuccess();
756
893
  postResize();
@@ -844,9 +981,12 @@ export const COMPOSE_EMAIL_HTML = `
844
981
  }
845
982
  }
846
983
 
847
- function formatSentTime() {
984
+ function formatSentTime(at) {
985
+ if (typeof at !== 'number' || !Number.isFinite(at)) return '';
986
+ var date = new Date(at);
987
+ if (!Number.isFinite(date.getTime())) return '';
848
988
  try {
849
- return new Date().toLocaleString(undefined, {
989
+ return date.toLocaleString(undefined, {
850
990
  month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit'
851
991
  });
852
992
  } catch (_) {
@@ -854,6 +994,41 @@ export const COMPOSE_EMAIL_HTML = `
854
994
  }
855
995
  }
856
996
 
997
+ function applyCardOutcome(o) {
998
+ if (sent) return;
999
+ if (pendingRequestId && pendingRequestId === pendingSendRequestId &&
1000
+ (!o || o.outcome === 'sending' || o.outcome === 'maybe-sent')) return;
1001
+ if (hostSending) {
1002
+ hostSending = false;
1003
+ setSending(false);
1004
+ if (sendMayHaveGone) showSendUnknown();
1005
+ }
1006
+ if (!o || typeof o !== 'object') return;
1007
+ if (o.outcome === 'sending') {
1008
+ hostSending = true;
1009
+ setCollapsed(false);
1010
+ setSending(true);
1011
+ clearUnknown();
1012
+ return;
1013
+ }
1014
+ if (o.outcome === 'maybe-sent') {
1015
+ showSendUnknown();
1016
+ setCollapsed(false);
1017
+ return;
1018
+ }
1019
+ if (o.outcome !== 'sent' && o.outcome !== 'submitted') return;
1020
+ sent = true;
1021
+ sendMayHaveGone = false;
1022
+ clearSendTimeout();
1023
+ clearError();
1024
+ clearSuccess();
1025
+ clearUnknown();
1026
+ var stamp = formatSentTime(o.at);
1027
+ setCollapsed(true, stamp ? 'Email sent · ' + stamp : 'Email sent.');
1028
+ if (pendingSendPayload) renderSentView(pendingSendPayload, null, stamp);
1029
+ reopenButton.classList.toggle('hidden', !pendingSendPayload);
1030
+ }
1031
+
857
1032
  function renderSentView(payload, meta, stamp) {
858
1033
  payload = payload && typeof payload === 'object' ? payload : {};
859
1034
  sentTimestamp.textContent = stamp ? 'Sent ' + stamp : 'Sent';
@@ -862,13 +1037,17 @@ export const COMPOSE_EMAIL_HTML = `
862
1037
  var ccList = normalizeAddressList(payload.cc);
863
1038
  sentCc.textContent = ccList.join(', ');
864
1039
  sentCcField.classList.toggle('hidden', ccList.length === 0);
1040
+ var bccList = normalizeAddressList(payload.bcc);
1041
+ sentBcc.textContent = bccList.join(', ');
1042
+ sentBccField.classList.toggle('hidden', bccList.length === 0);
865
1043
  sentSubject.textContent = String(payload.subject || '');
866
1044
  sentBody.textContent = String(payload.body || '');
867
1045
  }
868
1046
 
869
1047
  function handleSendRequest() {
1048
+ if (sent) return;
870
1049
  if (sending) return;
871
-
1050
+ if (sendBlocked) return;
872
1051
  var payload = readFormPayload();
873
1052
  var validationError = validatePayload(payload);
874
1053
  if (validationError) {
@@ -880,7 +1059,7 @@ export const COMPOSE_EMAIL_HTML = `
880
1059
  }
881
1060
 
882
1061
  // The Send button is type="button" with an explicit click handler rather
883
- // than a form submit button. The Rebel host renders this iframe with a
1062
+ // than a form submit button. The host renders this iframe with a
884
1063
  // sandbox of "allow-scripts" only — no "allow-forms" — so native form
885
1064
  // submission is blocked by the browser BEFORE the submit event fires
886
1065
  // ("Blocked form submission ... the form's frame is sandboxed and the
@@ -895,14 +1074,37 @@ export const COMPOSE_EMAIL_HTML = `
895
1074
  handleSendRequest();
896
1075
  });
897
1076
 
1077
+ draftButton.addEventListener('click', function () {
1078
+ if (sending) return;
1079
+ var payload = readFormPayload();
1080
+ var validationError = validateDraftPayload(payload);
1081
+ if (validationError) {
1082
+ showError(validationError);
1083
+ return;
1084
+ }
1085
+ saveDraftPayload(payload);
1086
+ });
1087
+
898
1088
  composeForm.addEventListener('submit', function (event) {
899
1089
  event.preventDefault();
900
1090
  handleSendRequest();
901
1091
  });
902
1092
 
903
1093
  retryButton.addEventListener('click', function () {
1094
+ if (sent) return;
904
1095
  if (sending) return;
1096
+ if (sendBlocked && retryAction !== 'draft') return;
905
1097
  var payload = readFormPayload();
1098
+ if (retryAction === 'draft') {
1099
+ var draftValidationError = validateDraftPayload(payload);
1100
+ if (draftValidationError) {
1101
+ showError(draftValidationError);
1102
+ return;
1103
+ }
1104
+ retryAction = null;
1105
+ saveDraftPayload(payload);
1106
+ return;
1107
+ }
906
1108
  var validationError = validatePayload(payload);
907
1109
  if (validationError) {
908
1110
  showError(validationError);
@@ -913,9 +1115,8 @@ export const COMPOSE_EMAIL_HTML = `
913
1115
 
914
1116
  cancelButton.addEventListener('click', function () {
915
1117
  if (sending) return;
916
- // After a lost-reply timeout we don't know if the email sent, so don't
917
- // claim "Nothing sent." on collapse — point the user to verify instead.
918
- var collapseMessage = outcomeUnknown
1118
+ // Hiding the notice does not resolve whether an earlier send went out.
1119
+ var collapseMessage = sendMayHaveGone
919
1120
  ? 'Collapsed. Check your Sent folder if you are not sure.'
920
1121
  : 'Draft collapsed. Nothing sent.';
921
1122
  clearError();
@@ -926,6 +1127,7 @@ export const COMPOSE_EMAIL_HTML = `
926
1127
 
927
1128
  reopenButton.addEventListener('click', function () {
928
1129
  setCollapsed(false);
1130
+ if (!sent && sendMayHaveGone) showSendUnknown();
929
1131
  });
930
1132
 
931
1133
  sentCollapseButton.addEventListener('click', function () {
@@ -937,11 +1139,16 @@ export const COMPOSE_EMAIL_HTML = `
937
1139
  postResize();
938
1140
  });
939
1141
 
940
- [toInput, ccInput, subjectInput, bodyInput].forEach(function (element) {
1142
+ toggleBccButton.addEventListener('click', function () {
1143
+ setBccVisible(bccRow.classList.contains('hidden'));
1144
+ postResize();
1145
+ });
1146
+
1147
+ [toInput, ccInput, bccInput, subjectInput, bodyInput].forEach(function (element) {
941
1148
  element.addEventListener('input', function () {
942
- clearError();
1149
+ if (!sendBlocked) clearError();
943
1150
  clearSuccess();
944
- clearUnknown();
1151
+ if (!sendMayHaveGone) clearUnknown();
945
1152
  postResize();
946
1153
  });
947
1154
  });
@@ -965,6 +1172,11 @@ export const COMPOSE_EMAIL_HTML = `
965
1172
  return;
966
1173
  }
967
1174
 
1175
+ if (data.method === 'mcp-app:card-outcome') {
1176
+ if (event.source === window.parent) applyCardOutcome(data.params);
1177
+ return;
1178
+ }
1179
+
968
1180
  if (data.method === 'ui/notifications/tool-result') {
969
1181
  // Always parse (so the migration-shim's malformed-envelope warning
970
1182
  // still fires), but apply only the first successful prefill: a stray
@@ -978,32 +1190,72 @@ export const COMPOSE_EMAIL_HTML = `
978
1190
  return;
979
1191
  }
980
1192
 
981
- if (data.jsonrpc !== '2.0' || !pendingRequestId || data.id !== pendingRequestId) {
1193
+ if (data.jsonrpc !== '2.0') return;
1194
+ var completedSendPayload = null;
1195
+ if (pendingSendRequestId && data.id === pendingSendRequestId) {
1196
+ completedSendPayload = pendingSendPayload;
1197
+ pendingSendPayload = null;
1198
+ pendingSendRequestId = null;
1199
+ }
1200
+ if (!pendingRequestId || data.id !== pendingRequestId) {
982
1201
  return;
983
1202
  }
984
1203
 
985
1204
  pendingRequestId = null;
986
1205
  clearSendTimeout();
987
1206
  clearUnknown();
988
- setSending(false);
1207
+ var completedAction = pendingAction;
1208
+ pendingAction = null;
1209
+ if (completedAction === 'draft') {
1210
+ setSavingDraft(false);
1211
+ } else {
1212
+ setSending(false);
1213
+ }
989
1214
 
990
1215
  if (data.error) {
991
1216
  var errorMessage = data.error && typeof data.error.message === 'string'
992
1217
  ? data.error.message
993
- : 'Failed to send email.';
1218
+ : (completedAction === 'draft' ? 'Failed to save draft.' : 'Failed to send email.');
1219
+ if (completedAction === 'draft') {
1220
+ // Retry re-invokes the draft tool, not the send tool — pendingAction
1221
+ // is already cleared, so this is what the Retry handler routes on.
1222
+ retryAction = 'draft';
1223
+ }
1224
+ var blockedReason = data.error.data && data.error.data.reason;
1225
+ var retryCannotChangeBlock = blockedReason === 'user-disabled' || blockedReason === 'admin-disabled' || blockedReason === 'security-policy';
1226
+ var blockedBeforeSend = retryCannotChangeBlock || blockedReason === 'not-dispatched';
1227
+ if (completedAction !== 'draft' && retryCannotChangeBlock) {
1228
+ sendBlocked = true;
1229
+ sendButton.disabled = true;
1230
+ }
1231
+ if (completedAction !== 'draft' && (!blockedBeforeSend || sendMayHaveGone)) {
1232
+ showSendUnknown();
1233
+ }
994
1234
  showError(errorMessage);
995
1235
  return;
996
1236
  }
997
1237
 
998
1238
  clearError();
999
1239
  clearSuccess();
1000
- // Preserve what was sent instead of discarding the draft. pendingPayload
1240
+ if (completedAction === 'draft') {
1241
+ // Terminal: the draft now lives in the mailbox's Drafts folder.
1242
+ // Collapse to a stamped confirmation and retire the form — Reopen
1243
+ // would resurrect an editable copy whose next Save/Send silently
1244
+ // diverges from (or duplicates) the saved draft.
1245
+ var draftStamp = formatSentTime(Date.now());
1246
+ setCollapsed(true, draftStamp ? 'Draft saved · ' + draftStamp : 'Draft saved.');
1247
+ reopenButton.classList.add('hidden');
1248
+ return;
1249
+ }
1250
+ // Preserve what was sent instead of discarding the draft. completedSendPayload
1001
1251
  // is the exact payload we posted. We default to the collapsed summary, but
1002
1252
  // Reopen now restores a read-only view of the sent message.
1003
- var sentPayload = pendingPayload || readFormPayload();
1004
- var stamp = formatSentTime();
1253
+ var sentPayload = completedSendPayload || readFormPayload();
1254
+ var stamp = formatSentTime(Date.now());
1005
1255
  renderSentView(sentPayload, getSendMetaFromResult(data.result), stamp);
1006
1256
  sent = true;
1257
+ sendMayHaveGone = false;
1258
+ reopenButton.classList.remove('hidden');
1007
1259
  setCollapsed(true, stamp ? 'Email sent · ' + stamp : 'Email sent.');
1008
1260
  });
1009
1261
 
@@ -1014,7 +1266,10 @@ export const COMPOSE_EMAIL_HTML = `
1014
1266
 
1015
1267
  applyThemeFromHostContext();
1016
1268
  setCcVisible(false);
1017
- setCollapsed(false);
1269
+ setBccVisible(false);
1270
+ var hostOutcome = (window.__MCP_HOST_CONTEXT__ || {}).cardOutcome;
1271
+ if (hostOutcome) applyCardOutcome(hostOutcome);
1272
+ else setCollapsed(false);
1018
1273
  postResize();
1019
1274
  postReady();
1020
1275