mcp-newoutlook 0.32.21 → 0.32.22
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/dist/bridge.js +109 -33
- package/dist/bridge.js.map +1 -1
- package/dist/hub.js +7 -0
- package/dist/hub.js.map +1 -1
- package/dist/tools.js +8 -1
- package/dist/tools.js.map +1 -1
- package/extension/background.js +188 -45
- package/extension/manifest.json +1 -1
- package/package.json +1 -1
package/extension/background.js
CHANGED
|
@@ -416,7 +416,7 @@ function openOutlookTab() {
|
|
|
416
416
|
*/
|
|
417
417
|
function requestAt(ctx) {
|
|
418
418
|
if (!ctx) return findOutlookTab().then((found) => ({ ...found, ctx }));
|
|
419
|
-
ctx.at ??= findOutlookTab().then((found) => ({ ...found, ctx }));
|
|
419
|
+
ctx.at ??= findOutlookTab(ctx).then((found) => ({ ...found, ctx }));
|
|
420
420
|
return ctx.at;
|
|
421
421
|
}
|
|
422
422
|
|
|
@@ -450,7 +450,8 @@ async function protectOutlookTabs() {
|
|
|
450
450
|
const lostSessionKey = (origin) => `lostSession:${origin}`;
|
|
451
451
|
const siteLostSession = async (origin) => !!(await chrome.storage.session.get(lostSessionKey(origin)))[lostSessionKey(origin)];
|
|
452
452
|
|
|
453
|
-
|
|
453
|
+
/** ctx: the request it is for, if any (see refreshSession). */
|
|
454
|
+
async function findOutlookTab(ctx = null) {
|
|
454
455
|
const tabs = await chrome.tabs.query({ url: OUTLOOK_URLS });
|
|
455
456
|
if (tabs.length === 0) return openOutlookTab();
|
|
456
457
|
const scored = await Promise.all(
|
|
@@ -480,7 +481,8 @@ async function findOutlookTab() {
|
|
|
480
481
|
// still holds (its page state, unsaved text included), which is why reloadTab decides. Without
|
|
481
482
|
// a reload no script will run in it, so the request says so now instead of at its deadline.
|
|
482
483
|
if (isAsleep(found.t)) {
|
|
483
|
-
const { cap, refused } = await refreshSession(found.t, { quickWaitMs: 0, why: "wake" });
|
|
484
|
+
const { cap, refused } = await refreshSession(found.t, { quickWaitMs: 0, why: "wake", ctx });
|
|
485
|
+
if (refused === "gone") throw new Error(`${NOT_RELOADED.gone}; try again.`);
|
|
484
486
|
if (refused) throw new Error(`The Outlook tab is asleep. ${NOT_RELOADED[refused]}; click on it or reload it, then try again.`);
|
|
485
487
|
found.cap = cap ?? found.cap;
|
|
486
488
|
}
|
|
@@ -546,9 +548,28 @@ const NOT_RELOADED = {
|
|
|
546
548
|
busy: "Requests are still running in the Outlook tab, so it wasn't reloaded",
|
|
547
549
|
recent: "The Outlook tab was reloaded recently",
|
|
548
550
|
answering: "The Outlook tab still answers, so it wasn't reloaded",
|
|
549
|
-
|
|
551
|
+
failed: "The Outlook tab couldn't be reloaded",
|
|
552
|
+
gone: "The Outlook tab was closed",
|
|
550
553
|
};
|
|
551
554
|
|
|
555
|
+
/**
|
|
556
|
+
* For reloadTab: whether any request waiting on a reload still wants it. Each in wants is a request's
|
|
557
|
+
* check, which throws once it was withdrawn (see requestContext), or a search stopped waiting. The
|
|
558
|
+
* reload is the tab's, shared by every request waiting on it, so one withdrawn doesn't stop it for
|
|
559
|
+
* the others: it is left out only when none is left that wants it.
|
|
560
|
+
*/
|
|
561
|
+
const stillWanted = (wants) => () =>
|
|
562
|
+
[...wants].some((check) => {
|
|
563
|
+
try {
|
|
564
|
+
check();
|
|
565
|
+
return true;
|
|
566
|
+
} catch {
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
const tabExists = (tabId) => chrome.tabs.get(tabId).then(() => true, () => false);
|
|
572
|
+
|
|
552
573
|
/**
|
|
553
574
|
* What each tab's scripts are doing, for reloadTab: a reload kills a script mid-flight, and a
|
|
554
575
|
* send killed that way returns an error though the email may have gone. Per tab: Outlook's
|
|
@@ -581,25 +602,27 @@ const tabsReloading = new Map(); // tabId -> Promise, settled once the tab has l
|
|
|
581
602
|
* can't be running: a tab Chrome reports asleep runs no scripts. A request that gave up says only
|
|
582
603
|
* that its own script was lost, so "stuck" asks the page: one that answers is working, and is left
|
|
583
604
|
* alone with whatever runs in it; one that doesn't runs nothing. Gives { cap } once the tab has
|
|
584
|
-
* loaded, or { refused }: a key of NOT_RELOADED
|
|
605
|
+
* loaded, or { refused }: a key of NOT_RELOADED, "gone" also if it closed after the reload, or
|
|
606
|
+
* "withdrawn" if, just before the reload, wanted() said no request waiting on it still wants it
|
|
607
|
+
* (see stillWanted): the waits before can take seconds, and a request may be withdrawn meanwhile.
|
|
585
608
|
*/
|
|
586
|
-
async function reloadTab(tab, why, waitForSession) {
|
|
609
|
+
async function reloadTab(tab, why, waitForSession, wanted = () => true) {
|
|
587
610
|
const origin = new URL(tab.url).origin;
|
|
588
|
-
const turn = (reloadTurns.get(origin) ?? Promise.resolve()).then(() => startReload(tab, origin, why, waitForSession));
|
|
611
|
+
const turn = (reloadTurns.get(origin) ?? Promise.resolve()).then(() => startReload(tab, origin, why, waitForSession, wanted));
|
|
589
612
|
reloadTurns.set(origin, turn.catch(() => {}));
|
|
590
613
|
const { refused, waiting, finish } = await turn;
|
|
591
614
|
if (refused) return { refused };
|
|
592
615
|
try {
|
|
593
616
|
const cap = await waiting;
|
|
594
617
|
if (cap) await waitForTabLoaded(tab.id);
|
|
595
|
-
return { cap };
|
|
618
|
+
return (await tabExists(tab.id)) ? { cap } : { refused: "gone" };
|
|
596
619
|
} finally {
|
|
597
620
|
finish();
|
|
598
621
|
}
|
|
599
622
|
}
|
|
600
623
|
|
|
601
624
|
/** reloadTab's decision and the reload itself; the session and the page load are waited for after. */
|
|
602
|
-
async function startReload(tab, origin, why, waitForSession) {
|
|
625
|
+
async function startReload(tab, origin, why, waitForSession, wanted) {
|
|
603
626
|
await tabsReloading.get(tab.id);
|
|
604
627
|
const stampKey = `tabReload:${origin}`;
|
|
605
628
|
const lastReload = (await chrome.storage.session.get(stampKey))[stampKey] ?? 0;
|
|
@@ -616,6 +639,8 @@ async function startReload(tab, origin, why, waitForSession) {
|
|
|
616
639
|
if (Date.now() >= giveUpAt) return { refused: "busy" };
|
|
617
640
|
}
|
|
618
641
|
if (Date.now() - lastReload < RELOAD_EVERY_MS[why]) return { refused: "recent" };
|
|
642
|
+
// Nothing awaits from here to the reload, so no request is withdrawn in between.
|
|
643
|
+
if (!wanted()) return { refused: "withdrawn" };
|
|
619
644
|
// From here no script starts in the tab (see holdWhileReloading).
|
|
620
645
|
let finish;
|
|
621
646
|
const loaded = new Promise((resolve) => {
|
|
@@ -634,7 +659,7 @@ async function startReload(tab, origin, why, waitForSession) {
|
|
|
634
659
|
await chrome.tabs.reload(tab.id);
|
|
635
660
|
} catch {
|
|
636
661
|
finish();
|
|
637
|
-
return { refused: "gone" };
|
|
662
|
+
return { refused: (await tabExists(tab.id)) ? "failed" : "gone" };
|
|
638
663
|
}
|
|
639
664
|
// After the reload: one that failed doesn't count.
|
|
640
665
|
await chrome.storage.session.set({ [stampKey]: since });
|
|
@@ -681,7 +706,10 @@ async function holdWhileReloading(tabId, ctx) {
|
|
|
681
706
|
|
|
682
707
|
/** How long a request waits, on a tab it can't reload, for a request of Outlook's own to bring a session. */
|
|
683
708
|
const OWN_REQUEST_WAIT_MS = 15_000;
|
|
684
|
-
const refreshing = new Map(); // origin -> { why, done: Promise<{ cap, refused }>, notSession }, so parallel calls reload the tab once
|
|
709
|
+
const refreshing = new Map(); // origin -> { why, done: Promise<{ cap, refused }>, notSession, wants }, so parallel calls reload the tab once
|
|
710
|
+
|
|
711
|
+
/** What a wait found, unless the tab closed meanwhile: a session may come from another tab of the site, and is no use without this one. */
|
|
712
|
+
const unlessGone = async (tab, found) => ((await tabExists(tab.id)) ? found : { cap: null, refused: "gone" });
|
|
685
713
|
|
|
686
714
|
/**
|
|
687
715
|
* The captured session is lost when the extension reloads (chrome.storage.session is
|
|
@@ -690,19 +718,34 @@ const refreshing = new Map(); // origin -> { why, done: Promise<{ cap, refused }
|
|
|
690
718
|
* be reloaded because it is visible, or was just reloaded, is left to send requests of its own, so
|
|
691
719
|
* those are waited for, for a while (a quiet tab may send none for minutes). notSession: a token
|
|
692
720
|
* just refused, left out of the wait on a tab just reloaded. A request joining a refresh under way
|
|
693
|
-
* shares its wait, but not its notSession: if that brings the token it was refused, it goes on.
|
|
721
|
+
* shares its wait, but not its notSession: if that brings the token it was refused, it goes on. On
|
|
722
|
+
* its retry it also leaves out the first request's, in what it looks for itself (see below).
|
|
694
723
|
* One that needs a session takes one it can use as soon as there is one, without waiting out a
|
|
695
|
-
* wait for a session; if that wait brings nothing, it looks once more
|
|
696
|
-
* was refused, or on its retry the one the first request was, unless
|
|
697
|
-
* with a dead token it takes at most the rest of that wait, a 401, and a
|
|
724
|
+
* wait for a session; if that wait brings nothing, it looks once more, unless the tab has closed.
|
|
725
|
+
* It can't use the token it was refused, or on its retry the one the first request was, unless
|
|
726
|
+
* that wait accepted it. So with a dead token it takes at most the rest of that wait, a 401, and a
|
|
727
|
+
* wait of its own. A tab closed by the time any of this ends gives { refused: "gone" }, session or not.
|
|
728
|
+
* That is the request's own tab: a wait it joined may be for another tab of the site, and if that one
|
|
729
|
+
* closes, a request whose tab is alive starts over.
|
|
698
730
|
* why: what asks for it ("session": a request needs one; "wake": the tab is asleep; "stuck": a
|
|
699
|
-
* script never answered).
|
|
731
|
+
* script never answered). ctx: the request it is for, which a retry first checks (see requestContext).
|
|
732
|
+
* The reload is checked again just before it happens, for every request waiting on it: it goes ahead
|
|
733
|
+
* while one of them still wants it (see stillWanted). One withdrawn by then gets its Cancelled or
|
|
734
|
+
* Stopped error, and one still wanting it that came too late to count starts over.
|
|
735
|
+
* Gives { cap } (null if a reload brought none), and { refused } if the tab wasn't reloaded (see reloadTab).
|
|
700
736
|
*/
|
|
701
|
-
function refreshSession(tab, { quickWaitMs = 3_000, why = "session", notSession = null } = {}) {
|
|
737
|
+
function refreshSession(tab, { quickWaitMs = 3_000, why = "session", notSession = null, ctx = null } = {}) {
|
|
702
738
|
const origin = new URL(tab.url).origin;
|
|
703
739
|
const running = refreshing.get(origin);
|
|
704
|
-
|
|
740
|
+
// Starting over may reload the tab, long after the request came: it may be withdrawn by now.
|
|
741
|
+
const again = () => {
|
|
742
|
+
ctx?.check();
|
|
743
|
+
return refreshSession(tab, { quickWaitMs, why, notSession, ctx });
|
|
744
|
+
};
|
|
745
|
+
// This call's, so that leaving takes out only its own.
|
|
746
|
+
const wants = () => ctx?.check();
|
|
705
747
|
if (running) {
|
|
748
|
+
running.wants.add(wants);
|
|
706
749
|
// On its retry a request has no try left, so it leaves out the first request's refused token too:
|
|
707
750
|
// a second 401 would lose the reason it can give. With a try to spare, a 401 costs it only that.
|
|
708
751
|
const usable = (cap) => (cap && !(notSession && [notSession, running.notSession].includes(sessionId(cap))) ? cap : null);
|
|
@@ -712,25 +755,33 @@ function refreshSession(tab, { quickWaitMs = 3_000, why = "session", notSession
|
|
|
712
755
|
// look during a wake or stuck check, whose tab may run no script until its reload.
|
|
713
756
|
if (why === "session" && running.why === "session") {
|
|
714
757
|
const cap = await whenCaptured(async () => usable(await getCapture(origin)), (changes) => usable(changes[`capture:${origin}`]?.newValue), running.done);
|
|
715
|
-
if (cap) return { cap };
|
|
758
|
+
if (cap) return unlessGone(tab, { cap });
|
|
716
759
|
}
|
|
717
760
|
const r = await running.done;
|
|
761
|
+
// That answers for the first request's tab, and this request answers for its own.
|
|
762
|
+
if (!(await tabExists(tab.id))) return { cap: null, refused: "gone" };
|
|
763
|
+
// Joined after the reload was left out, or withdrawn itself.
|
|
764
|
+
if (r.refused === "withdrawn") return again();
|
|
765
|
+
// The tab that closed was the first request's. Its run has ended, so this request starts one of
|
|
766
|
+
// its own, or joins another request's: a request running its own refresh never comes back here.
|
|
767
|
+
if (r.refused === "gone") return again();
|
|
718
768
|
// A stuck check's refusal answers only that check: a tab that still answers may need a session or a wake.
|
|
719
769
|
if (r.refused && running.why === "stuck" && why !== "stuck") return again();
|
|
720
770
|
if (notSession && sessionId(r.cap) === notSession) return again();
|
|
721
|
-
if (r.cap || why !== "session"
|
|
771
|
+
if (r.cap || why !== "session") return r;
|
|
722
772
|
const stored = usable(await getCapture(origin));
|
|
723
773
|
return stored ? { cap: stored } : r;
|
|
724
|
-
})();
|
|
774
|
+
})().finally(() => running.wants.delete(wants));
|
|
725
775
|
}
|
|
726
776
|
const since = Date.now();
|
|
777
|
+
const entry = { why, notSession, wants: new Set([wants]) };
|
|
727
778
|
const done = (async () => {
|
|
728
779
|
const quick = quickWaitMs ? await waitForCapture(origin, since, quickWaitMs) : null;
|
|
729
780
|
if (quick) return { cap: quick };
|
|
730
|
-
const { cap, refused } = await reloadTab(tab, why, (at) => waitForCapture(origin, at, 30_000));
|
|
781
|
+
const { cap, refused } = await reloadTab(tab, why, (at) => waitForCapture(origin, at, 30_000), stillWanted(entry.wants));
|
|
731
782
|
if (!refused) return { cap };
|
|
732
|
-
// A session stored is no use without the tab to send it from.
|
|
733
|
-
if (why !== "session" || refused === "gone") return { cap: null, refused };
|
|
783
|
+
// A session stored is no use without the tab to send it from, and none is waited for for a request withdrawn.
|
|
784
|
+
if (why !== "session" || refused === "gone" || refused === "withdrawn") return { cap: null, refused };
|
|
734
785
|
// A session may have come meanwhile: from the reload another path just did, or from Outlook's
|
|
735
786
|
// own next request, which is all that can bring one to a tab that is visible or was just
|
|
736
787
|
// reloaded (right after a reload, its first token may be refused).
|
|
@@ -744,9 +795,13 @@ function refreshSession(tab, { quickWaitMs = 3_000, why = "session", notSession
|
|
|
744
795
|
const current = refused === "recent" && notSession && sessionId(stored) === notSession ? null : stored;
|
|
745
796
|
const own = current ?? (refused === "recent" ? await waitForCapture(origin, since, OWN_REQUEST_WAIT_MS, notSession) : null);
|
|
746
797
|
return own ? { cap: own } : { cap: null, refused };
|
|
747
|
-
})()
|
|
748
|
-
|
|
749
|
-
|
|
798
|
+
})()
|
|
799
|
+
.then((r) => unlessGone(tab, r))
|
|
800
|
+
.finally(() => refreshing.delete(origin));
|
|
801
|
+
entry.done = done;
|
|
802
|
+
refreshing.set(origin, entry);
|
|
803
|
+
// Its own check is among those the reload was left out for: again() gives its error.
|
|
804
|
+
return done.then((r) => (r.refused === "withdrawn" ? again() : r));
|
|
750
805
|
}
|
|
751
806
|
|
|
752
807
|
const SESSION_HELP =
|
|
@@ -761,7 +816,8 @@ async function sessionFor(tab, opts) {
|
|
|
761
816
|
if (cap) return cap;
|
|
762
817
|
if (!refused) throw new NoSessionError(SESSION_HELP);
|
|
763
818
|
// A visible tab can bring one back without losing anything: using it makes Outlook send a request.
|
|
764
|
-
|
|
819
|
+
// A closed one has nothing to reload: the next request finds another tab or opens one.
|
|
820
|
+
const remedy = refused === "visible" ? "open another email in it or reload it, then try again" : refused === "gone" ? "try again" : "reload it yourself and try again";
|
|
765
821
|
throw new NoSessionError(`Outlook session is not authorized. ${NOT_RELOADED[refused]}; ${remedy}.`);
|
|
766
822
|
}
|
|
767
823
|
|
|
@@ -774,7 +830,7 @@ async function owaCall(action, body, at) {
|
|
|
774
830
|
ctx?.check();
|
|
775
831
|
if (!at?.t) at = await requestAt(ctx);
|
|
776
832
|
const tab = at.t;
|
|
777
|
-
at.cap ??= await sessionFor(tab);
|
|
833
|
+
at.cap ??= await sessionFor(tab, { ctx });
|
|
778
834
|
const idempotent = READ_ACTIONS.has(action);
|
|
779
835
|
const origin = new URL(tab.url).origin;
|
|
780
836
|
const key = `capture:${origin}`;
|
|
@@ -798,7 +854,7 @@ async function owaCall(action, body, at) {
|
|
|
798
854
|
// `at` keeps a session until then: calls sharing it still need one to send.
|
|
799
855
|
at.cap =
|
|
800
856
|
(await forgetRefused(key, sentWith, () => chrome.storage.session.set({ [lostSessionKey(origin)]: true }))) ??
|
|
801
|
-
(await sessionFor(tab, { quickWaitMs: 0, notSession: sessionId(sentWith) }));
|
|
857
|
+
(await sessionFor(tab, { quickWaitMs: 0, notSession: sessionId(sentWith), ctx }));
|
|
802
858
|
res = await sendWithRetry(send, { idempotent, ctx });
|
|
803
859
|
if (res.status === 401 || res.status === 440) throw new NoSessionError(SESSION_HELP);
|
|
804
860
|
}
|
|
@@ -814,11 +870,17 @@ async function owaCall(action, body, at) {
|
|
|
814
870
|
* reaches Outlook (as ctx, or inside `at`), so a request the server gave up on starts no
|
|
815
871
|
* more Outlook requests. session: the AI session the server tagged it with (an opaque id;
|
|
816
872
|
* servers before #76 send none, and their requests share one session), for withOwaSlot.
|
|
873
|
+
* sentDeadline: the absolute deadline the hub sent (#160; on the clock of the MCP server that
|
|
874
|
+
* started the call, from when it started), which only ever shortens timeoutMs, never
|
|
875
|
+
* lengthens it: the request may have taken a while to get here. Servers before #160 send none.
|
|
817
876
|
*/
|
|
818
|
-
function requestContext(timeoutMs, session = "") {
|
|
877
|
+
function requestContext(timeoutMs, session = "", sentDeadline) {
|
|
819
878
|
const listeners = new Set();
|
|
879
|
+
const now = Date.now();
|
|
880
|
+
let deadline = timeoutMs > 0 ? now + timeoutMs : null;
|
|
881
|
+
if (deadline && Number.isFinite(sentDeadline)) deadline = Math.min(deadline, Math.max(now, sentDeadline));
|
|
820
882
|
const ctx = {
|
|
821
|
-
deadline
|
|
883
|
+
deadline,
|
|
822
884
|
session,
|
|
823
885
|
cancelled: false,
|
|
824
886
|
cancel() {
|
|
@@ -882,7 +944,10 @@ async function injectInTab(tab, func, args, ctx, key, cap) {
|
|
|
882
944
|
counted = false;
|
|
883
945
|
};
|
|
884
946
|
let timer;
|
|
947
|
+
let stopOnCancel;
|
|
885
948
|
const gaveUp = new Promise((_, reject) => {
|
|
949
|
+
// A cancel ends the wait at once (#160), freeing the Outlook slot; the page stops the script by its deadline.
|
|
950
|
+
stopOnCancel = ctx?.onCancel(() => reject(new Error("Cancelled: the request was withdrawn.")));
|
|
886
951
|
timer = setTimeout(() => {
|
|
887
952
|
// Past its deadline the page has stopped this script, if it ever ran. If the page has
|
|
888
953
|
// stopped answering altogether, the next request finds it working again after a reload,
|
|
@@ -897,6 +962,7 @@ async function injectInTab(tab, func, args, ctx, key, cap) {
|
|
|
897
962
|
return injection?.result;
|
|
898
963
|
} finally {
|
|
899
964
|
clearTimeout(timer);
|
|
965
|
+
stopOnCancel?.();
|
|
900
966
|
stopCounting();
|
|
901
967
|
ownSends.delete(own);
|
|
902
968
|
}
|
|
@@ -952,7 +1018,7 @@ async function usableSearchCapture(origin) {
|
|
|
952
1018
|
|
|
953
1019
|
/** How long a search waits for the tab to reload and Outlook to set up its search session. */
|
|
954
1020
|
const SEARCH_SESSION_WAIT_MS = 30_000;
|
|
955
|
-
const searchRefreshing = new Map(); // origin -> Promise, so parallel searches reload the tab once
|
|
1021
|
+
const searchRefreshing = new Map(); // origin -> { p: Promise, wants }, so parallel searches reload the tab once
|
|
956
1022
|
|
|
957
1023
|
const NO_SEARCH_SESSION = "Outlook has no working search session (it sets one up when its tab loads, and the token runs out)";
|
|
958
1024
|
/** How long before a request's deadline a search stops waiting for a session: matching fields instead takes time too. */
|
|
@@ -976,18 +1042,27 @@ async function searchSessionFromLastReload(origin) {
|
|
|
976
1042
|
* Outlook sends the requests that give a search session only when its tab loads. A session is
|
|
977
1043
|
* lost when the extension reloads, and runs out after about an hour: nothing else brings it
|
|
978
1044
|
* back. So a search that needs one reloads the tab, unless the user can see it.
|
|
979
|
-
* Gives { cap } or, when there is none, { cap: null, reason }
|
|
1045
|
+
* Gives { cap } or, when there is none, { cap: null, reason }, or { cap: null, gone: tabId } if the tab
|
|
1046
|
+
* it reloaded closed, or { cap: null, withdrawn: true } if no search waiting on it still wanted the
|
|
1047
|
+
* reload, just before it (check: the search's, which throws once it no longer waits; see stillWanted).
|
|
980
1048
|
*/
|
|
981
|
-
function refreshSearchSession(tab) {
|
|
1049
|
+
function refreshSearchSession(tab, check) {
|
|
982
1050
|
const origin = new URL(tab.url).origin;
|
|
1051
|
+
// This call's, so that leaving takes out only its own.
|
|
1052
|
+
const wants = () => check?.();
|
|
983
1053
|
if (!searchRefreshing.has(origin)) {
|
|
984
|
-
const
|
|
1054
|
+
const started = { wants: new Set() };
|
|
1055
|
+
started.p = (async () => {
|
|
985
1056
|
const key = `search:${origin}`;
|
|
986
|
-
const
|
|
1057
|
+
const wait = (since) => {
|
|
987
1058
|
const fresh = (c) => (c && c.at >= since && !tokenExpired(c.headers) ? c : null);
|
|
988
1059
|
return whenCaptured(async () => fresh(await getSearchCapture(origin)), (changes) => fresh(changes[key]?.newValue), SEARCH_SESSION_WAIT_MS);
|
|
989
|
-
}
|
|
1060
|
+
};
|
|
1061
|
+
const { cap, refused } = await reloadTab(tab, "search", wait, stillWanted(started.wants));
|
|
1062
|
+
if (refused === "withdrawn") return { cap: null, withdrawn: true };
|
|
990
1063
|
if (refused) {
|
|
1064
|
+
// Parallel searches share this, and only this tab is known to be gone: see searchSessionFor.
|
|
1065
|
+
if (refused === "gone") return { cap: null, gone: tab.id };
|
|
991
1066
|
// The reload another path just did may have brought one, or be about to.
|
|
992
1067
|
const current = (await usableSearchCapture(origin)) ?? (await searchSessionFromLastReload(origin));
|
|
993
1068
|
return current ? { cap: current } : { cap: null, reason: `${NO_SEARCH_SESSION}. ${NOT_RELOADED[refused]}; reload it or search once in Outlook to turn on full-text search` };
|
|
@@ -995,19 +1070,46 @@ function refreshSearchSession(tab) {
|
|
|
995
1070
|
if (!cap) return { cap: null, reason: `${NO_SEARCH_SESSION}. Reloading the Outlook tab didn't bring one within ${SEARCH_SESSION_WAIT_MS / 1000} s` };
|
|
996
1071
|
return { cap };
|
|
997
1072
|
})().finally(() => searchRefreshing.delete(origin));
|
|
998
|
-
searchRefreshing.set(origin,
|
|
1073
|
+
searchRefreshing.set(origin, started);
|
|
999
1074
|
}
|
|
1000
|
-
|
|
1075
|
+
const entry = searchRefreshing.get(origin);
|
|
1076
|
+
entry.wants.add(wants);
|
|
1077
|
+
return entry.p.finally(() => entry.wants.delete(wants));
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/**
|
|
1081
|
+
* refreshSearchSession, for a request on this tab. Matching fields instead would need the tab too, and
|
|
1082
|
+
* the request is held to the one it chose, so with that tab closed it can only say so. With another
|
|
1083
|
+
* tab closed it starts over: the site's reload stamp stops that being a second reload soon after.
|
|
1084
|
+
* Only another tab closing starts it over, so it does that at most once per tab that closes. Starting
|
|
1085
|
+
* over may reload the tab, so it first checks the request is still waiting: check throws if not. The
|
|
1086
|
+
* reload checks it again just before it happens (see refreshSearchSession). A search whose reload was
|
|
1087
|
+
* left out throws there, unless it came too late to count, when it starts over.
|
|
1088
|
+
*/
|
|
1089
|
+
async function searchSessionFor(tab, check) {
|
|
1090
|
+
const r = await refreshSearchSession(tab, check);
|
|
1091
|
+
if (r.gone === tab.id || !(await tabExists(tab.id))) throw new Error(`${NOT_RELOADED.gone}; try again.`);
|
|
1092
|
+
if (!r.gone && !r.withdrawn) return r;
|
|
1093
|
+
check?.();
|
|
1094
|
+
return searchSessionFor(tab, check);
|
|
1001
1095
|
}
|
|
1002
1096
|
|
|
1003
|
-
/**
|
|
1097
|
+
/** searchSessionFor, for as long as the request's deadline leaves time to match fields instead. */
|
|
1004
1098
|
async function searchSessionInTime(tab, ctx) {
|
|
1005
|
-
|
|
1099
|
+
// Once it has stopped waiting, nothing would use what starting over brings.
|
|
1100
|
+
let gaveUp = false;
|
|
1101
|
+
const refreshed = searchSessionFor(tab, () => {
|
|
1102
|
+
ctx?.check();
|
|
1103
|
+
if (gaveUp) throw new Error("The search stopped waiting for a session.");
|
|
1104
|
+
});
|
|
1006
1105
|
if (!ctx?.deadline) return refreshed;
|
|
1007
1106
|
let timer;
|
|
1008
1107
|
const late = new Promise((resolve) => {
|
|
1009
1108
|
const reason = `${NO_SEARCH_SESSION}. It didn't come in the time this request has left`;
|
|
1010
|
-
timer = setTimeout(() =>
|
|
1109
|
+
timer = setTimeout(() => {
|
|
1110
|
+
gaveUp = true;
|
|
1111
|
+
resolve({ cap: null, reason });
|
|
1112
|
+
}, Math.max(0, ctx.deadline - Date.now() - SEARCH_FALLBACK_MS));
|
|
1011
1113
|
});
|
|
1012
1114
|
try {
|
|
1013
1115
|
return await Promise.race([refreshed, late]);
|
|
@@ -2454,7 +2556,42 @@ const STREAM_FIELDS = ["contentBase64", "mimeBase64"];
|
|
|
2454
2556
|
/** The server's own pace between pieces: the next turn of the event loop (a cancel can come in). */
|
|
2455
2557
|
const nextTurn = () => new Promise((r) => setTimeout(r, 0));
|
|
2456
2558
|
|
|
2457
|
-
|
|
2559
|
+
/**
|
|
2560
|
+
* A request that reached the worker too late to be of use (#160): its deadline had passed, or
|
|
2561
|
+
* it came more than LATE_ARRIVAL_MS after it was sent with less than that left. It is answered
|
|
2562
|
+
* with code "late" (not Busy, not a timeout), so callers don't send it again right away.
|
|
2563
|
+
*/
|
|
2564
|
+
class LateArrivalError extends Error {
|
|
2565
|
+
code = "late";
|
|
2566
|
+
}
|
|
2567
|
+
const LATE_ARRIVAL_MS = 1_000;
|
|
2568
|
+
|
|
2569
|
+
/** The error for a request that arrived too late to run, or null (see LateArrivalError). */
|
|
2570
|
+
function lateArrival(msg, timeoutMs, ctx) {
|
|
2571
|
+
if (!(timeoutMs > 0) || !Number.isFinite(msg.deadline) || !ctx.deadline) return null;
|
|
2572
|
+
const now = Date.now();
|
|
2573
|
+
const delay = now - (msg.deadline - timeoutMs);
|
|
2574
|
+
const left = ctx.deadline - now;
|
|
2575
|
+
if (left > 0 && (delay <= LATE_ARRIVAL_MS || left >= LATE_ARRIVAL_MS)) return null;
|
|
2576
|
+
return new LateArrivalError(
|
|
2577
|
+
`The request reached the extension ${Math.round(delay / 1000)} s after it was sent, with ${Math.round(timeoutMs / 1000)} s to run, so it wasn't run and Outlook wasn't asked. The computer or the browser is overloaded; sending it again at once won't help.`,
|
|
2578
|
+
);
|
|
2579
|
+
}
|
|
2580
|
+
|
|
2581
|
+
/**
|
|
2582
|
+
* Requests that open a confirmation window: after a cancel their answer still reports what happened.
|
|
2583
|
+
* Keep it the same as CONFIRM_CALLS (src/bridge.ts), whose methods UNQUEUED there holds too;
|
|
2584
|
+
* nothing checks this, so change all three together.
|
|
2585
|
+
*/
|
|
2586
|
+
const ANSWER_AFTER_CANCEL = new Set(["requestSend", "requestEventResponse", "requestCreateEvent"]);
|
|
2587
|
+
|
|
2588
|
+
const errorReply = (id, err) => ({
|
|
2589
|
+
type: "response",
|
|
2590
|
+
id,
|
|
2591
|
+
ok: false,
|
|
2592
|
+
error: err?.message ?? String(err),
|
|
2593
|
+
...(err instanceof LateArrivalError ? { code: err.code } : {}),
|
|
2594
|
+
});
|
|
2458
2595
|
|
|
2459
2596
|
/**
|
|
2460
2597
|
* Runs one request from the server and answers it through send(reply, text), text being the
|
|
@@ -2473,14 +2610,20 @@ export async function dispatch(
|
|
|
2473
2610
|
{ maxMessageBytes = server?.maxMessageBytes, streamMinChars = STREAM_MIN_CHARS, pieceChars = STREAM_PIECE_CHARS, pace = nextTurn } = {},
|
|
2474
2611
|
) {
|
|
2475
2612
|
const handler = handlers[msg.method];
|
|
2476
|
-
const
|
|
2613
|
+
const timeoutMs = Number(msg.timeoutMs) || 0;
|
|
2614
|
+
const ctx = requestContext(timeoutMs, typeof msg.session === "string" ? msg.session : "", msg.deadline);
|
|
2477
2615
|
if (msg.id != null) running.set(msg.id, ctx);
|
|
2478
2616
|
let reply;
|
|
2479
2617
|
let text;
|
|
2480
2618
|
let content = null; // the base64 that goes as pieces
|
|
2481
2619
|
try {
|
|
2482
2620
|
if (!handler) throw new Error(`Unknown method ${msg.method}`);
|
|
2621
|
+
const late = lateArrival(msg, timeoutMs, ctx);
|
|
2622
|
+
if (late) throw late;
|
|
2483
2623
|
let result = await handler(msg.params ?? {}, ctx);
|
|
2624
|
+
// Nobody waits for the answer to a cancelled request (#160): a short error, not the result.
|
|
2625
|
+
// A confirmation window's answer still says what the user did (sent, or withdrawn).
|
|
2626
|
+
if (ctx.cancelled && !ANSWER_AFTER_CANCEL.has(msg.method)) ctx.check();
|
|
2484
2627
|
const field =
|
|
2485
2628
|
msg.stream === true && result && typeof result === "object"
|
|
2486
2629
|
? STREAM_FIELDS.find((f) => typeof result[f] === "string" && result[f].length > streamMinChars)
|
package/extension/manifest.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifest_version": 3,
|
|
3
3
|
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyYxkVrq+THOJVscuwRU4IvQ4K0VWiXVOjhDWjcwpILY3r7e/Fb+VhI813kRV149laUXLfehqI6WrQKbVs/KODArPIfiNEpm/zsb2nmgbQtoYuzMO3b5Hvsp1JNZ+IEMQGRFwHlOQt+w+qLAbSgdgLK2mtvjhGu9kWLmiK2wxKPaI0/zVAdvvthzMeGZCcC7V9tH2WX8I3RVD/PZcgmmFY38APGwgatg5AV5zz0AZEPSH7ldcAI2dHkmxuFh85k1cJJjWYEHikXc+ibTv9n1OfGbt/BzV/+QpMjbpJGmnJJrzCEqjw0WOyoabtpRKympl/jshjA5Kp+avlUvEYse12QIDAQAB",
|
|
4
4
|
"name": "mcp-newoutlook bridge",
|
|
5
|
-
"version": "0.32.
|
|
5
|
+
"version": "0.32.22",
|
|
6
6
|
"description": "Lets AI assistants (via the mcp-newoutlook MCP server) read, search, draft and — with your explicit confirmation — send mail in New Outlook.",
|
|
7
7
|
"minimum_chrome_version": "116",
|
|
8
8
|
"icons": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-newoutlook",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.22",
|
|
4
4
|
"description": "MCP server that lets AI assistants work with New Outlook (web) through a Chrome/Edge extension bridge",
|
|
5
5
|
"keywords": ["mcp", "model-context-protocol", "outlook", "office365", "email", "calendar", "claude", "chrome-extension"],
|
|
6
6
|
"repository": {
|