@mindstone/mcp-server-microsoft-mail 0.1.1 → 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.
@@ -0,0 +1,1035 @@
1
+ // GENERATED by scripts/gen-compose-html.mjs — do not edit by hand.
2
+ // Regenerate with "npm run compose:template"; "npm test" runs the --check drift gate.
3
+ // The generator also writes compose-email.html, keeping the two in sync.
4
+ export const COMPOSE_EMAIL_HTML = `
5
+ <!DOCTYPE html>
6
+ <html lang="en">
7
+ <head>
8
+ <meta charset="utf-8">
9
+ <meta name="viewport" content="width=device-width, initial-scale=1">
10
+ <title>Compose Email</title>
11
+ <style>
12
+ :root {
13
+ --bg: #ffffff;
14
+ --panel: #ffffff;
15
+ --border: #d9dde5;
16
+ --text: #1f2937;
17
+ --muted: #6b7280;
18
+ --input-bg: #ffffff;
19
+ --button-bg: #1f6feb;
20
+ --button-text: #ffffff;
21
+ --button-bg-hover: #1b63d3;
22
+ --secondary-bg: #f3f4f6;
23
+ --secondary-text: #1f2937;
24
+ --secondary-border: #d1d5db;
25
+ --error-bg: #fef2f2;
26
+ --error-border: #fecaca;
27
+ --error-text: #991b1b;
28
+ --success-bg: #ecfdf5;
29
+ --success-border: #bbf7d0;
30
+ --success-text: #166534;
31
+ --warning-bg: #fffbeb;
32
+ --warning-border: #fde68a;
33
+ --warning-text: #92400e;
34
+ }
35
+
36
+ @media (prefers-color-scheme: dark) {
37
+ :root {
38
+ --bg: #0f172a;
39
+ --panel: #111827;
40
+ --border: #334155;
41
+ --text: #e5e7eb;
42
+ --muted: #9ca3af;
43
+ --input-bg: #0b1220;
44
+ --button-bg: #3b82f6;
45
+ --button-text: #eff6ff;
46
+ --button-bg-hover: #2563eb;
47
+ --secondary-bg: #1f2937;
48
+ --secondary-text: #e5e7eb;
49
+ --secondary-border: #374151;
50
+ --error-bg: #2b1011;
51
+ --error-border: #7f1d1d;
52
+ --error-text: #fca5a5;
53
+ --success-bg: #052e1b;
54
+ --success-border: #166534;
55
+ --success-text: #86efac;
56
+ --warning-bg: #2a2106;
57
+ --warning-border: #854d0e;
58
+ --warning-text: #fcd34d;
59
+ }
60
+ }
61
+
62
+ html[data-theme="light"] {
63
+ color-scheme: light;
64
+ }
65
+
66
+ html[data-theme="dark"] {
67
+ color-scheme: dark;
68
+ --bg: #0f172a;
69
+ --panel: #111827;
70
+ --border: #334155;
71
+ --text: #e5e7eb;
72
+ --muted: #9ca3af;
73
+ --input-bg: #0b1220;
74
+ --button-bg: #3b82f6;
75
+ --button-text: #eff6ff;
76
+ --button-bg-hover: #2563eb;
77
+ --secondary-bg: #1f2937;
78
+ --secondary-text: #e5e7eb;
79
+ --secondary-border: #374151;
80
+ --error-bg: #2b1011;
81
+ --error-border: #7f1d1d;
82
+ --error-text: #fca5a5;
83
+ --success-bg: #052e1b;
84
+ --success-border: #166534;
85
+ --success-text: #86efac;
86
+ --warning-bg: #2a2106;
87
+ --warning-border: #854d0e;
88
+ --warning-text: #fcd34d;
89
+ }
90
+
91
+ * {
92
+ box-sizing: border-box;
93
+ }
94
+
95
+ body {
96
+ margin: 0;
97
+ padding: 16px;
98
+ background: var(--bg);
99
+ color: var(--text);
100
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
101
+ line-height: 1.4;
102
+ }
103
+
104
+ .wrapper {
105
+ width: 100%;
106
+ max-width: 720px;
107
+ margin: 0 auto;
108
+ }
109
+
110
+ .card {
111
+ background: var(--panel);
112
+ border: 1px solid var(--border);
113
+ border-radius: 12px;
114
+ padding: 14px;
115
+ display: flex;
116
+ flex-direction: column;
117
+ gap: 12px;
118
+ }
119
+
120
+ .field {
121
+ display: flex;
122
+ flex-direction: column;
123
+ gap: 6px;
124
+ }
125
+
126
+ .field label {
127
+ font-size: 12px;
128
+ font-weight: 600;
129
+ color: var(--muted);
130
+ text-transform: uppercase;
131
+ letter-spacing: 0.04em;
132
+ }
133
+
134
+ .from-value {
135
+ color: var(--text);
136
+ font-size: 14px;
137
+ padding: 2px 0;
138
+ word-break: break-word;
139
+ }
140
+
141
+ .from-helper {
142
+ font-size: 12px;
143
+ color: var(--muted);
144
+ margin-top: 2px;
145
+ }
146
+
147
+ .input,
148
+ .textarea {
149
+ width: 100%;
150
+ background: var(--input-bg);
151
+ color: var(--text);
152
+ border: 1px solid var(--border);
153
+ border-radius: 8px;
154
+ padding: 10px 12px;
155
+ font-size: 14px;
156
+ font-family: inherit;
157
+ }
158
+
159
+ .input:focus,
160
+ .textarea:focus {
161
+ outline: 2px solid rgba(59, 130, 246, 0.35);
162
+ outline-offset: 1px;
163
+ }
164
+
165
+ .textarea {
166
+ min-height: 180px;
167
+ resize: vertical;
168
+ white-space: pre-wrap;
169
+ }
170
+
171
+ .toggles {
172
+ display: flex;
173
+ gap: 12px;
174
+ align-items: center;
175
+ flex-wrap: wrap;
176
+ }
177
+
178
+ .link-button {
179
+ border: none;
180
+ padding: 0;
181
+ background: none;
182
+ color: var(--muted);
183
+ text-decoration: underline;
184
+ font-size: 12px;
185
+ cursor: pointer;
186
+ }
187
+
188
+ .link-button:hover {
189
+ color: var(--text);
190
+ }
191
+
192
+ .actions {
193
+ display: flex;
194
+ justify-content: flex-end;
195
+ gap: 10px;
196
+ margin-top: 2px;
197
+ }
198
+
199
+ .button {
200
+ border-radius: 8px;
201
+ border: 1px solid transparent;
202
+ min-height: 36px;
203
+ padding: 0 14px;
204
+ font-size: 13px;
205
+ font-weight: 600;
206
+ font-family: inherit;
207
+ cursor: pointer;
208
+ display: inline-flex;
209
+ align-items: center;
210
+ justify-content: center;
211
+ gap: 8px;
212
+ transition: background 120ms ease, opacity 120ms ease, border-color 120ms ease;
213
+ }
214
+
215
+ .button:disabled {
216
+ cursor: not-allowed;
217
+ opacity: 0.75;
218
+ }
219
+
220
+ .button-primary {
221
+ background: var(--button-bg);
222
+ color: var(--button-text);
223
+ }
224
+
225
+ .button-primary:hover:enabled {
226
+ background: var(--button-bg-hover);
227
+ }
228
+
229
+ .button-secondary {
230
+ background: var(--secondary-bg);
231
+ color: var(--secondary-text);
232
+ border-color: var(--secondary-border);
233
+ }
234
+
235
+ .spinner {
236
+ width: 14px;
237
+ height: 14px;
238
+ border: 2px solid currentColor;
239
+ border-right-color: transparent;
240
+ border-radius: 50%;
241
+ display: inline-block;
242
+ animation: spin 700ms linear infinite;
243
+ }
244
+
245
+ @keyframes spin {
246
+ to { transform: rotate(360deg); }
247
+ }
248
+
249
+ .status {
250
+ border-radius: 8px;
251
+ border: 1px solid transparent;
252
+ padding: 8px 10px;
253
+ font-size: 13px;
254
+ }
255
+
256
+ .error {
257
+ background: var(--error-bg);
258
+ border-color: var(--error-border);
259
+ color: var(--error-text);
260
+ display: flex;
261
+ justify-content: space-between;
262
+ align-items: center;
263
+ gap: 8px;
264
+ flex-wrap: wrap;
265
+ }
266
+
267
+ .success {
268
+ background: var(--success-bg);
269
+ border-color: var(--success-border);
270
+ color: var(--success-text);
271
+ }
272
+
273
+ .warning {
274
+ background: var(--warning-bg);
275
+ border-color: var(--warning-border);
276
+ color: var(--warning-text);
277
+ display: block;
278
+ }
279
+
280
+ .warning strong {
281
+ display: block;
282
+ margin-bottom: 2px;
283
+ }
284
+
285
+ .warning .warning-detail {
286
+ opacity: 0.85;
287
+ }
288
+
289
+ .collapsed {
290
+ border: 1px solid var(--border);
291
+ border-radius: 10px;
292
+ background: var(--panel);
293
+ padding: 10px 12px;
294
+ display: flex;
295
+ justify-content: space-between;
296
+ align-items: center;
297
+ gap: 8px;
298
+ font-size: 13px;
299
+ }
300
+
301
+ .hidden {
302
+ display: none !important;
303
+ }
304
+
305
+ .sent-header {
306
+ display: flex;
307
+ align-items: center;
308
+ gap: 8px;
309
+ flex-wrap: wrap;
310
+ }
311
+
312
+ .sent-badge {
313
+ display: inline-flex;
314
+ align-items: center;
315
+ background: var(--success-bg);
316
+ color: var(--success-text);
317
+ border: 1px solid var(--success-border);
318
+ border-radius: 999px;
319
+ padding: 2px 10px;
320
+ font-size: 12px;
321
+ font-weight: 600;
322
+ }
323
+
324
+ .sent-timestamp {
325
+ color: var(--muted);
326
+ font-size: 12px;
327
+ }
328
+
329
+ .sent-value {
330
+ color: var(--text);
331
+ font-size: 14px;
332
+ word-break: break-word;
333
+ }
334
+
335
+ .sent-body {
336
+ color: var(--text);
337
+ font-size: 14px;
338
+ white-space: pre-wrap;
339
+ word-break: break-word;
340
+ max-height: 260px;
341
+ overflow: auto;
342
+ border: 1px solid var(--border);
343
+ border-radius: 8px;
344
+ padding: 10px 12px;
345
+ background: var(--input-bg);
346
+ }
347
+ </style>
348
+ </head>
349
+ <body>
350
+ <div class="wrapper">
351
+ <div id="collapsedState" class="collapsed hidden">
352
+ <span id="collapsedMessage">Draft collapsed.</span>
353
+ <button id="reopenButton" type="button" class="button button-secondary">Reopen</button>
354
+ </div>
355
+
356
+ <form id="composeForm" class="card" novalidate aria-label="Compose email">
357
+ <div class="field from-field">
358
+ <label>From</label>
359
+ <span id="fromValue" class="from-value"></span>
360
+ <span id="fromHelper" class="from-helper hidden"></span>
361
+ </div>
362
+
363
+ <div class="field">
364
+ <label for="toInput">To</label>
365
+ <input id="toInput" class="input" type="text" autocomplete="off" placeholder="name@example.com, team@example.com">
366
+ </div>
367
+
368
+ <div class="toggles">
369
+ <button id="toggleCcButton" type="button" class="link-button" aria-expanded="false" aria-controls="ccRow">Add CC</button>
370
+ </div>
371
+
372
+ <div id="ccRow" class="field hidden">
373
+ <label for="ccInput">CC</label>
374
+ <input id="ccInput" class="input" type="text" autocomplete="off" placeholder="cc@example.com">
375
+ </div>
376
+
377
+ <div class="field">
378
+ <label for="subjectInput">Subject</label>
379
+ <input id="subjectInput" class="input" type="text" autocomplete="off" placeholder="Email subject">
380
+ </div>
381
+
382
+ <div class="field">
383
+ <label for="bodyInput">Body</label>
384
+ <textarea id="bodyInput" class="textarea" placeholder="Write your email"></textarea>
385
+ </div>
386
+
387
+ <div id="errorBox" class="status error hidden" role="alert">
388
+ <span id="errorText"></span>
389
+ <button id="retryButton" type="button" class="link-button">Retry</button>
390
+ </div>
391
+
392
+ <div id="successBox" class="status success hidden" role="status" aria-live="polite"></div>
393
+
394
+ <div id="unknownBox" class="status warning hidden" role="status" aria-live="polite">
395
+ <strong id="unknownTitle"></strong>
396
+ <span id="unknownText" class="warning-detail"></span>
397
+ </div>
398
+
399
+ <div class="actions">
400
+ <button id="cancelButton" type="button" class="button button-secondary">Cancel</button>
401
+ <button id="sendButton" type="button" class="button button-primary">
402
+ <span id="sendSpinner" class="spinner hidden" aria-hidden="true"></span>
403
+ <span id="sendLabel">Send email</span>
404
+ </button>
405
+ </div>
406
+ </form>
407
+
408
+ <div id="sentView" class="card hidden" role="status" aria-live="polite">
409
+ <div class="sent-header">
410
+ <span class="sent-badge">Sent</span>
411
+ <span id="sentTimestamp" class="sent-timestamp"></span>
412
+ </div>
413
+
414
+ <div class="field">
415
+ <label>From</label>
416
+ <span id="sentFrom" class="sent-value"></span>
417
+ </div>
418
+
419
+ <div class="field">
420
+ <label>To</label>
421
+ <span id="sentTo" class="sent-value"></span>
422
+ </div>
423
+
424
+ <div id="sentCcField" class="field hidden">
425
+ <label>CC</label>
426
+ <span id="sentCc" class="sent-value"></span>
427
+ </div>
428
+
429
+ <div class="field">
430
+ <label>Subject</label>
431
+ <span id="sentSubject" class="sent-value"></span>
432
+ </div>
433
+
434
+ <div class="field">
435
+ <label>Message</label>
436
+ <div id="sentBody" class="sent-body"></div>
437
+ </div>
438
+
439
+ <div class="actions">
440
+ <button id="sentCollapseButton" type="button" class="button button-secondary">Collapse</button>
441
+ </div>
442
+ </div>
443
+ </div>
444
+
445
+ <script>
446
+ (function () {
447
+ 'use strict';
448
+
449
+ var RESOURCE_URI = 'ui://microsoft-mail/compose-email';
450
+ var currentEmail = '';
451
+ // The host prefills the draft exactly once via ui/notifications/tool-result
452
+ // on ready. We apply only the FIRST prefill and ignore any later ones, so a
453
+ // stray or hostile re-post can't silently rewrite a draft the user has
454
+ // already reviewed before clicking Send.
455
+ var draftApplied = false;
456
+ var pendingRequestId = null;
457
+ var pendingPayload = null;
458
+ var sending = false;
459
+ var collapsed = false;
460
+ var outcomeUnknown = false;
461
+ var sent = false;
462
+ var sendTimeoutId = null;
463
+ // Just beyond the host-side tool-call timeout (60s) so a legitimately slow
464
+ // send resolves to its real result first; this only fires when the reply
465
+ // is genuinely lost, so the button can never stay stuck-silent.
466
+ var SEND_TIMEOUT_MS = 75000;
467
+
468
+ var composeForm = document.getElementById('composeForm');
469
+ var collapsedState = document.getElementById('collapsedState');
470
+ var collapsedMessage = document.getElementById('collapsedMessage');
471
+ var reopenButton = document.getElementById('reopenButton');
472
+
473
+ var sentView = document.getElementById('sentView');
474
+ var sentTimestamp = document.getElementById('sentTimestamp');
475
+ var sentFrom = document.getElementById('sentFrom');
476
+ var sentTo = document.getElementById('sentTo');
477
+ var sentCcField = document.getElementById('sentCcField');
478
+ var sentCc = document.getElementById('sentCc');
479
+ var sentSubject = document.getElementById('sentSubject');
480
+ var sentBody = document.getElementById('sentBody');
481
+ var sentCollapseButton = document.getElementById('sentCollapseButton');
482
+
483
+ var toInput = document.getElementById('toInput');
484
+ var ccInput = document.getElementById('ccInput');
485
+ var subjectInput = document.getElementById('subjectInput');
486
+ var bodyInput = document.getElementById('bodyInput');
487
+ var fromValue = document.getElementById('fromValue');
488
+ var fromHelper = document.getElementById('fromHelper');
489
+ var ccRow = document.getElementById('ccRow');
490
+ var toggleCcButton = document.getElementById('toggleCcButton');
491
+
492
+ var sendButton = document.getElementById('sendButton');
493
+ var sendLabel = document.getElementById('sendLabel');
494
+ var sendSpinner = document.getElementById('sendSpinner');
495
+ var cancelButton = document.getElementById('cancelButton');
496
+ var retryButton = document.getElementById('retryButton');
497
+
498
+ var errorBox = document.getElementById('errorBox');
499
+ var errorText = document.getElementById('errorText');
500
+ var successBox = document.getElementById('successBox');
501
+ var unknownBox = document.getElementById('unknownBox');
502
+ var unknownTitle = document.getElementById('unknownTitle');
503
+ var unknownText = document.getElementById('unknownText');
504
+
505
+ function applyThemeFromHostContext() {
506
+ try {
507
+ var context = window.__MCP_HOST_CONTEXT__;
508
+ if (!context || (context.theme !== 'light' && context.theme !== 'dark')) {
509
+ return;
510
+ }
511
+ document.documentElement.setAttribute('data-theme', context.theme);
512
+ } catch (_) {
513
+ // Ignore theme extraction errors
514
+ }
515
+ }
516
+
517
+ function trimString(value) {
518
+ return String(value || '').trim();
519
+ }
520
+
521
+ function applyFromValue(email) {
522
+ var trimmed = trimString(email);
523
+ if (trimmed) {
524
+ fromValue.textContent = trimmed;
525
+ fromValue.style.color = '';
526
+ fromHelper.textContent = '';
527
+ fromHelper.classList.add('hidden');
528
+ } else {
529
+ fromValue.textContent = 'Account not shown';
530
+ 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.';
532
+ fromHelper.classList.remove('hidden');
533
+ }
534
+ }
535
+
536
+ function formatAddressItem(item) {
537
+ if (item && typeof item === 'object') {
538
+ var email = trimString(item.email || item.address || item.value);
539
+ var name = trimString(item.name || item.displayName);
540
+ if (email && name) {
541
+ return name + ' <' + email + '>';
542
+ }
543
+ return email;
544
+ }
545
+ return trimString(item);
546
+ }
547
+
548
+ function normalizeAddressList(value) {
549
+ if (Array.isArray(value)) {
550
+ return value.map(formatAddressItem).filter(Boolean);
551
+ }
552
+ if (typeof value === 'string') {
553
+ return value
554
+ .split(',')
555
+ .map(function (item) { return trimString(item); })
556
+ .filter(Boolean);
557
+ }
558
+ return [];
559
+ }
560
+
561
+ function listToInputValue(list) {
562
+ return normalizeAddressList(list).join(', ');
563
+ }
564
+
565
+ var resizeQueued = false;
566
+ function postResize() {
567
+ if (resizeQueued) return;
568
+ resizeQueued = true;
569
+ requestAnimationFrame(function () {
570
+ resizeQueued = false;
571
+ var height = Math.max(
572
+ document.documentElement.scrollHeight,
573
+ document.body ? document.body.scrollHeight : 0
574
+ );
575
+ window.parent.postMessage(
576
+ { jsonrpc: '2.0', method: 'ui/resize', params: { height: height } },
577
+ '*'
578
+ );
579
+ });
580
+ }
581
+
582
+ function postReady() {
583
+ window.parent.postMessage(
584
+ {
585
+ method: 'mcp-app:ready',
586
+ params: { resourceUri: RESOURCE_URI }
587
+ },
588
+ '*'
589
+ );
590
+ }
591
+
592
+ // Exactly one of the three panels is visible at a time: the editable form,
593
+ // the read-only sent view, or the collapsed summary bar.
594
+ function showView(view) {
595
+ composeForm.classList.toggle('hidden', view !== 'form');
596
+ sentView.classList.toggle('hidden', view !== 'sent');
597
+ collapsedState.classList.toggle('hidden', view !== 'collapsed');
598
+ postResize();
599
+ }
600
+
601
+ function setCollapsed(nextCollapsed, message) {
602
+ collapsed = nextCollapsed;
603
+ if (nextCollapsed) {
604
+ collapsedMessage.textContent = message || 'Draft collapsed.';
605
+ showView('collapsed');
606
+ } else {
607
+ // Expanding after a send restores the read-only sent view (never the
608
+ // editable form) so the user can re-read what went out without any risk
609
+ // of a duplicate send; before sending, it restores the editable form.
610
+ showView(sent ? 'sent' : 'form');
611
+ }
612
+ }
613
+
614
+ function setSending(nextSending) {
615
+ sending = nextSending;
616
+ sendButton.disabled = nextSending;
617
+ cancelButton.disabled = nextSending;
618
+ toInput.disabled = nextSending;
619
+ ccInput.disabled = nextSending;
620
+ subjectInput.disabled = nextSending;
621
+ bodyInput.disabled = nextSending;
622
+ sendSpinner.classList.toggle('hidden', !nextSending);
623
+ sendLabel.textContent = nextSending ? 'Sending…' : 'Send email';
624
+ sendButton.setAttribute('aria-busy', nextSending ? 'true' : 'false');
625
+ postResize();
626
+ }
627
+
628
+ function clearError() {
629
+ errorText.textContent = '';
630
+ errorBox.classList.add('hidden');
631
+ }
632
+
633
+ function showError(message) {
634
+ errorText.textContent = message || 'Failed to send email.';
635
+ errorBox.classList.remove('hidden');
636
+ postResize();
637
+ }
638
+
639
+ function clearSuccess() {
640
+ successBox.textContent = '';
641
+ successBox.classList.add('hidden');
642
+ }
643
+
644
+ function showSuccess(message) {
645
+ successBox.textContent = message;
646
+ successBox.classList.remove('hidden');
647
+ postResize();
648
+ }
649
+
650
+ function clearSendTimeout() {
651
+ if (sendTimeoutId !== null) {
652
+ clearTimeout(sendTimeoutId);
653
+ sendTimeoutId = null;
654
+ }
655
+ }
656
+
657
+ function clearUnknown() {
658
+ outcomeUnknown = false;
659
+ unknownTitle.textContent = '';
660
+ unknownText.textContent = '';
661
+ unknownBox.classList.add('hidden');
662
+ }
663
+
664
+ // No reply ever came back for the send (e.g. the host's response was lost
665
+ // before reaching this iframe). We genuinely don't know whether the email
666
+ // went out, so we must NOT claim success or failure: re-enable the form and
667
+ // tell the user to verify before resending (the send is not idempotent, so
668
+ // a blind retry could send a duplicate).
669
+ function showSendUnknown() {
670
+ outcomeUnknown = true;
671
+ clearError();
672
+ clearSuccess();
673
+ 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.";
675
+ unknownBox.classList.remove('hidden');
676
+ postResize();
677
+ }
678
+
679
+ function handleSendTimeout() {
680
+ sendTimeoutId = null;
681
+ if (!pendingRequestId) {
682
+ return;
683
+ }
684
+ pendingRequestId = null;
685
+ setSending(false);
686
+ showSendUnknown();
687
+ }
688
+
689
+ function setCcVisible(visible) {
690
+ ccRow.classList.toggle('hidden', !visible);
691
+ toggleCcButton.textContent = visible ? 'Hide CC' : 'Add CC';
692
+ toggleCcButton.setAttribute('aria-expanded', visible ? 'true' : 'false');
693
+ }
694
+
695
+ function readFormPayload() {
696
+ var payload = {
697
+ to: normalizeAddressList(toInput.value),
698
+ cc: normalizeAddressList(ccInput.value),
699
+ subject: String(subjectInput.value || ''),
700
+ body: String(bodyInput.value || '')
701
+ };
702
+ if (currentEmail) {
703
+ payload.email = currentEmail;
704
+ }
705
+ return payload;
706
+ }
707
+
708
+ function validatePayload(payload) {
709
+ if (!payload.to || payload.to.length === 0) {
710
+ return 'Add at least one recipient in To.';
711
+ }
712
+ if (!trimString(payload.subject)) {
713
+ return 'Add a subject before sending.';
714
+ }
715
+ if (!trimString(payload.body)) {
716
+ return 'Add an email body before sending.';
717
+ }
718
+ return null;
719
+ }
720
+
721
+ function sendPayload(payload) {
722
+ pendingPayload = payload;
723
+ pendingRequestId = 'send-' + Date.now() + '-' + Math.random().toString(16).slice(2);
724
+ setSending(true);
725
+ clearError();
726
+ clearSuccess();
727
+ clearUnknown();
728
+ clearSendTimeout();
729
+ sendTimeoutId = setTimeout(handleSendTimeout, SEND_TIMEOUT_MS);
730
+
731
+ window.parent.postMessage(
732
+ {
733
+ jsonrpc: '2.0',
734
+ method: 'tools/call',
735
+ params: {
736
+ name: 'send_email',
737
+ arguments: payload
738
+ },
739
+ id: pendingRequestId
740
+ },
741
+ '*'
742
+ );
743
+ }
744
+
745
+ function applyDraftData(rawDraft) {
746
+ var draft = rawDraft && typeof rawDraft === 'object' ? rawDraft : {};
747
+ toInput.value = listToInputValue(draft.to);
748
+ ccInput.value = listToInputValue(draft.cc);
749
+ subjectInput.value = typeof draft.subject === 'string' ? draft.subject : '';
750
+ bodyInput.value = typeof draft.body === 'string' ? draft.body : '';
751
+ currentEmail = typeof draft.email === 'string' ? draft.email : '';
752
+ applyFromValue(currentEmail);
753
+ setCcVisible(normalizeAddressList(draft.cc).length > 0);
754
+ clearError();
755
+ clearSuccess();
756
+ postResize();
757
+ }
758
+
759
+ function getDraftFromToolResult(payload) {
760
+ if (!payload || typeof payload !== 'object') {
761
+ return null;
762
+ }
763
+ // CANONICAL: post-A0 super-mcp hoists structuredContent onto the
764
+ // outer block; the host posts it through \`params.structuredContent\`.
765
+ if (payload.structuredContent && typeof payload.structuredContent === 'object') {
766
+ return payload.structuredContent;
767
+ }
768
+ // MIGRATION SHIM: pre-A0 stored sessions replayed through the
769
+ // current iframe lack \`params.structuredContent\` because the outer
770
+ // block had no hoist at capture time. Read inner envelope text →
771
+ // JSON.parse → result.structuredContent. NOT regex-based.
772
+ //
773
+ // Lifetime: PERMANENT defensive read-side helper. Pre-A0 sessions
774
+ // are persisted on disk and may be replayed indefinitely; there is
775
+ // no time-based sunset. Removing this fallback would silently break
776
+ // pre-A0 conversation replay.
777
+ var text = typeof payload.text === 'string' ? payload.text :
778
+ (Array.isArray(payload.content)
779
+ ? (payload.content.find(function (c) { return c && c.type === 'text' && typeof c.text === 'string'; }) || {}).text
780
+ : null);
781
+ if (typeof text !== 'string') return null;
782
+ try {
783
+ var parsed = JSON.parse(text);
784
+ if (parsed && typeof parsed === 'object' && parsed.result && typeof parsed.result === 'object') {
785
+ var inner = parsed.result;
786
+ if (inner.structuredContent && typeof inner.structuredContent === 'object') {
787
+ return inner.structuredContent;
788
+ }
789
+ }
790
+ } catch (_) {
791
+ // Likely-envelope heuristic: text starts with \`{\` and contains
792
+ // \`"package_id"\` in the first 64 chars. If JSON.parse failed on a
793
+ // likely-envelope, surface a warning so the developer chasing
794
+ // "compose form is empty on replay" sees a signal. For non-envelope
795
+ // text (the dominant case), no log fires.
796
+ var prefix = text.length > 64 ? text.slice(0, 64) : text;
797
+ var likelyEnvelope = prefix.charAt(0) === '{' && prefix.indexOf('"package_id"') !== -1;
798
+ if (likelyEnvelope && typeof console !== 'undefined' && typeof console.warn === 'function') {
799
+ console.warn('[compose-email] Migration shim: JSON.parse failed on likely super-mcp envelope; pre-fill skipped');
800
+ }
801
+ }
802
+ return null;
803
+ }
804
+
805
+ function findTextBlock(blocks) {
806
+ if (!Array.isArray(blocks)) return null;
807
+ var block = blocks.find(function (c) {
808
+ return c && c.type === 'text' && typeof c.text === 'string';
809
+ });
810
+ return block ? block.text : null;
811
+ }
812
+
813
+ // Extract the send result identifiers ({ messageId, threadId, labelIds })
814
+ // from the tool-call reply. The connector attaches structuredContent, which
815
+ // super-mcp hoists onto the outer block, so the fast path is a single read.
816
+ // The envelope-unwrapping fallback keeps this robust for hosts (or replayed
817
+ // sessions) that forward only the serialized super-mcp text envelope.
818
+ function getSendMetaFromResult(result) {
819
+ if (!result || typeof result !== 'object') return null;
820
+ if (result.structuredContent && typeof result.structuredContent === 'object') {
821
+ return result.structuredContent;
822
+ }
823
+ try {
824
+ var outerText = findTextBlock(result.content);
825
+ if (typeof outerText !== 'string' && typeof result.text === 'string') {
826
+ outerText = result.text;
827
+ }
828
+ if (typeof outerText !== 'string') return null;
829
+ var envelope = JSON.parse(outerText);
830
+ if (!envelope || typeof envelope !== 'object') return null;
831
+ // outerText was the connector payload directly (no super-mcp envelope).
832
+ if (envelope.messageId || envelope.threadId) return envelope;
833
+ var inner = envelope.result;
834
+ if (!inner || typeof inner !== 'object') return null;
835
+ if (inner.structuredContent && typeof inner.structuredContent === 'object') {
836
+ return inner.structuredContent;
837
+ }
838
+ var innerText = findTextBlock(inner.content);
839
+ if (typeof innerText !== 'string') return null;
840
+ var payload = JSON.parse(innerText);
841
+ return (payload && typeof payload === 'object') ? payload : null;
842
+ } catch (_) {
843
+ return null;
844
+ }
845
+ }
846
+
847
+ function formatSentTime() {
848
+ try {
849
+ return new Date().toLocaleString(undefined, {
850
+ month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit'
851
+ });
852
+ } catch (_) {
853
+ return '';
854
+ }
855
+ }
856
+
857
+ function renderSentView(payload, meta, stamp) {
858
+ payload = payload && typeof payload === 'object' ? payload : {};
859
+ sentTimestamp.textContent = stamp ? 'Sent ' + stamp : 'Sent';
860
+ sentFrom.textContent = trimString(payload.email) || 'Account not shown';
861
+ sentTo.textContent = normalizeAddressList(payload.to).join(', ');
862
+ var ccList = normalizeAddressList(payload.cc);
863
+ sentCc.textContent = ccList.join(', ');
864
+ sentCcField.classList.toggle('hidden', ccList.length === 0);
865
+ sentSubject.textContent = String(payload.subject || '');
866
+ sentBody.textContent = String(payload.body || '');
867
+ }
868
+
869
+ function handleSendRequest() {
870
+ if (sending) return;
871
+
872
+ var payload = readFormPayload();
873
+ var validationError = validatePayload(payload);
874
+ if (validationError) {
875
+ showError(validationError);
876
+ return;
877
+ }
878
+
879
+ sendPayload(payload);
880
+ }
881
+
882
+ // 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
884
+ // sandbox of "allow-scripts" only — no "allow-forms" — so native form
885
+ // submission is blocked by the browser BEFORE the submit event fires
886
+ // ("Blocked form submission ... the form's frame is sandboxed and the
887
+ // allow-forms permission is not set"). A submit-driven Send therefore
888
+ // silently did nothing: no postMessage, no spinner, no telemetry.
889
+ // A click handler needs no sandbox permission and is the same path
890
+ // Cancel/Retry already use. The submit listener below stays only as a
891
+ // defensive fallback for hosts that DO grant allow-forms; in this sandbox
892
+ // it never fires (neither the button click nor Enter routes through it).
893
+ // The sending guard keeps any double-fire (in an allow-forms host) a no-op.
894
+ sendButton.addEventListener('click', function () {
895
+ handleSendRequest();
896
+ });
897
+
898
+ composeForm.addEventListener('submit', function (event) {
899
+ event.preventDefault();
900
+ handleSendRequest();
901
+ });
902
+
903
+ retryButton.addEventListener('click', function () {
904
+ if (sending) return;
905
+ var payload = readFormPayload();
906
+ var validationError = validatePayload(payload);
907
+ if (validationError) {
908
+ showError(validationError);
909
+ return;
910
+ }
911
+ sendPayload(payload);
912
+ });
913
+
914
+ cancelButton.addEventListener('click', function () {
915
+ 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
919
+ ? 'Collapsed. Check your Sent folder if you are not sure.'
920
+ : 'Draft collapsed. Nothing sent.';
921
+ clearError();
922
+ clearSuccess();
923
+ clearUnknown();
924
+ setCollapsed(true, collapseMessage);
925
+ });
926
+
927
+ reopenButton.addEventListener('click', function () {
928
+ setCollapsed(false);
929
+ });
930
+
931
+ sentCollapseButton.addEventListener('click', function () {
932
+ setCollapsed(true, collapsedMessage.textContent || 'Email sent.');
933
+ });
934
+
935
+ toggleCcButton.addEventListener('click', function () {
936
+ setCcVisible(ccRow.classList.contains('hidden'));
937
+ postResize();
938
+ });
939
+
940
+ [toInput, ccInput, subjectInput, bodyInput].forEach(function (element) {
941
+ element.addEventListener('input', function () {
942
+ clearError();
943
+ clearSuccess();
944
+ clearUnknown();
945
+ postResize();
946
+ });
947
+ });
948
+
949
+ window.addEventListener('message', function (event) {
950
+ // Honest scope (origin-independent): the iframe sandbox origin is 'null',
951
+ // so validating event.origin is unreliable across dev/prod/custom-scheme
952
+ // hosts. Instead reject any *identified* sender that isn't our host frame
953
+ // (window.parent). A real cross-frame postMessage always stamps
954
+ // event.source with the sending window, so a hostile sibling frame is
955
+ // rejected here; a null source only occurs for same-context synthetic
956
+ // dispatch (our own code) and cannot be forged from outside, so we
957
+ // tolerate it. This does not authenticate the host's identity — the host
958
+ // separately authenticates this iframe by origin+source.
959
+ if (event.source && event.source !== window.parent) {
960
+ return;
961
+ }
962
+
963
+ var data = event.data;
964
+ if (!data || typeof data !== 'object') {
965
+ return;
966
+ }
967
+
968
+ if (data.method === 'ui/notifications/tool-result') {
969
+ // Always parse (so the migration-shim's malformed-envelope warning
970
+ // still fires), but apply only the first successful prefill: a stray
971
+ // or hostile re-post can't silently rewrite a draft the user has
972
+ // already reviewed.
973
+ var draft = getDraftFromToolResult(data.params);
974
+ if (draft && !draftApplied) {
975
+ draftApplied = true;
976
+ applyDraftData(draft);
977
+ }
978
+ return;
979
+ }
980
+
981
+ if (data.jsonrpc !== '2.0' || !pendingRequestId || data.id !== pendingRequestId) {
982
+ return;
983
+ }
984
+
985
+ pendingRequestId = null;
986
+ clearSendTimeout();
987
+ clearUnknown();
988
+ setSending(false);
989
+
990
+ if (data.error) {
991
+ var errorMessage = data.error && typeof data.error.message === 'string'
992
+ ? data.error.message
993
+ : 'Failed to send email.';
994
+ showError(errorMessage);
995
+ return;
996
+ }
997
+
998
+ clearError();
999
+ clearSuccess();
1000
+ // Preserve what was sent instead of discarding the draft. pendingPayload
1001
+ // is the exact payload we posted. We default to the collapsed summary, but
1002
+ // Reopen now restores a read-only view of the sent message.
1003
+ var sentPayload = pendingPayload || readFormPayload();
1004
+ var stamp = formatSentTime();
1005
+ renderSentView(sentPayload, getSendMetaFromResult(data.result), stamp);
1006
+ sent = true;
1007
+ setCollapsed(true, stamp ? 'Email sent · ' + stamp : 'Email sent.');
1008
+ });
1009
+
1010
+ if (typeof ResizeObserver === 'function' && document.body) {
1011
+ var observer = new ResizeObserver(postResize);
1012
+ observer.observe(document.body);
1013
+ }
1014
+
1015
+ applyThemeFromHostContext();
1016
+ setCcVisible(false);
1017
+ setCollapsed(false);
1018
+ postResize();
1019
+ postReady();
1020
+
1021
+ window.parent.postMessage(
1022
+ {
1023
+ jsonrpc: '2.0',
1024
+ method: 'ui/initialize',
1025
+ params: { resourceUri: RESOURCE_URI },
1026
+ id: 'compose-email-init'
1027
+ },
1028
+ '*'
1029
+ );
1030
+ })();
1031
+ </script>
1032
+ </body>
1033
+ </html>
1034
+ `;
1035
+ //# sourceMappingURL=compose-email-template.js.map