copilot-operator 0.1.85 → 0.1.88

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.
Files changed (50) hide show
  1. package/dist/src/api/operator.controller.js +23 -0
  2. package/dist/src/api/operator.service.js +59 -2
  3. package/dist/src/config/schema.js +15 -0
  4. package/dist/src/orchestrator/kerriganRunner.js +3 -1
  5. package/dist/src/orchestrator/leadRunner.js +3 -1
  6. package/dist/src/orchestrator/taskRunner.js +56 -31
  7. package/dist/src/plan/systemGuide.js +35 -4
  8. package/dist/src/session/compose.js +5 -1
  9. package/dist/src/transport/chatTransport.js +6 -2
  10. package/dist/src/transport/copilotTransport.js +226 -11
  11. package/dist/src/transport/keptWindow.js +247 -0
  12. package/dist/src/transport/locators.js +14 -4
  13. package/dist/web/404.html +2 -2
  14. package/dist/web/_next/static/chunks/1077.26ca37faa3726fff.js +1 -0
  15. package/dist/web/_next/static/chunks/1548.f5790cd11a91309f.js +1 -0
  16. package/dist/web/_next/static/chunks/2163.aa9d9b82fcde6caa.js +1 -0
  17. package/dist/web/_next/static/chunks/714.b7dbce98ba0b59a8.js +1 -0
  18. package/dist/web/_next/static/chunks/7220.0ca53eefec5d01ea.js +1 -0
  19. package/dist/web/_next/static/chunks/7921-8f6503aba840928a.js +1 -0
  20. package/dist/web/_next/static/chunks/8584.9f3b058a8f67d3d7.js +1 -0
  21. package/dist/web/_next/static/chunks/9067.107f6164157e0aa1.js +1 -0
  22. package/dist/web/_next/static/chunks/9747.984a86f70f9cd973.js +1 -0
  23. package/dist/web/_next/static/chunks/app/page-b71841852e6b9ff5.js +1 -0
  24. package/dist/web/_next/static/chunks/app/settings/page-95069cb94d79e909.js +1 -0
  25. package/dist/web/_next/static/chunks/{webpack-13ccb80d7b7094ca.js → webpack-c015bbeb103c0856.js} +1 -1
  26. package/dist/web/_next/static/css/{6e2b8656501f0dd7.css → 0c5d6d5914aa3824.css} +1 -1
  27. package/dist/web/defaults.html +2 -2
  28. package/dist/web/defaults.txt +8 -8
  29. package/dist/web/index.html +3 -3
  30. package/dist/web/index.txt +9 -9
  31. package/dist/web/queue.html +2 -2
  32. package/dist/web/queue.txt +9 -9
  33. package/dist/web/settings.html +2 -2
  34. package/dist/web/settings.txt +9 -9
  35. package/dist/web/system.html +2 -2
  36. package/dist/web/system.txt +9 -9
  37. package/package.json +3 -2
  38. package/dist/web/_next/static/chunks/1077.06fe73b2c16f8f49.js +0 -1
  39. package/dist/web/_next/static/chunks/1548.b38c5d8d00ab2a72.js +0 -1
  40. package/dist/web/_next/static/chunks/2163.ba933c9a506302ca.js +0 -1
  41. package/dist/web/_next/static/chunks/714.575273a189d030fe.js +0 -1
  42. package/dist/web/_next/static/chunks/7220.988dfd28e00f407c.js +0 -1
  43. package/dist/web/_next/static/chunks/7921-65dd56d87144bd44.js +0 -1
  44. package/dist/web/_next/static/chunks/8584.2221587201bc2fa2.js +0 -1
  45. package/dist/web/_next/static/chunks/9067.8d94a08830c3d1da.js +0 -1
  46. package/dist/web/_next/static/chunks/9747.8671194406acd839.js +0 -1
  47. package/dist/web/_next/static/chunks/app/page-76139370c7ca3715.js +0 -1
  48. package/dist/web/_next/static/chunks/app/settings/page-116da78c83f0c5c9.js +0 -1
  49. /package/dist/web/_next/static/{aCQe1FDhYTj4zGeI5ueVq → hLBBsG7Kn4vd0p_J-47BJ}/_buildManifest.js +0 -0
  50. /package/dist/web/_next/static/{aCQe1FDhYTj4zGeI5ueVq → hLBBsG7Kn4vd0p_J-47BJ}/_ssgManifest.js +0 -0
@@ -38,7 +38,6 @@ export class ReplyTimeoutError extends Error {
38
38
  export function isReplyTimeout(e) {
39
39
  return e instanceof ReplyTimeoutError || (e instanceof Error && e.name === 'ReplyTimeoutError' && typeof e.seconds === 'number');
40
40
  }
41
- const ORIGIN = 'https://m365.cloud.microsoft';
42
41
  /**
43
42
  * Which Copilot an address is, by its host — not by whether the text appears anywhere in it: the
44
43
  * consumer Copilot and a sign-in page carry the work chat's address in their query (`ru=`,
@@ -62,6 +61,21 @@ export function surfaceOfUrl(url) {
62
61
  return 'work';
63
62
  return 'unknown';
64
63
  }
64
+ /** Whether an address is the work Copilot's chat — a new chat, a conversation, the list of chats — on either of its hosts. */
65
+ export function isWorkChatAddress(url) {
66
+ if (surfaceOfUrl(url) !== 'work')
67
+ return false;
68
+ const path = new URL(url).pathname.replace(/\/+$/, '');
69
+ return path === '/chat' || path.startsWith('/chat/');
70
+ }
71
+ /**
72
+ * Whether an address is another page of the work Copilot, not its chat: its home page
73
+ * (`copilot.cloud.microsoft/home?es=SSR&redirfrom=mcmToCcm`, where the work machine's start ended on 2026-11-17), or any
74
+ * other of its own. Signed in, then — a sign-in page is on Microsoft's sign-in host — and only away from the chat.
75
+ */
76
+ export function isCopilotOtherPage(url) {
77
+ return surfaceOfUrl(url) === 'work' && !isWorkChatAddress(url);
78
+ }
65
79
  /**
66
80
  * The operator's locators for a model, from the templates in force and where the list read from the page found it
67
81
  * (operator, 2026-10-24): a model of the main menu by the template for those; one in a provider's menu by the
@@ -140,6 +154,13 @@ export class WindowClosed extends Error {
140
154
  this.name = 'WindowClosed';
141
155
  }
142
156
  }
157
+ /** Said when a window closed under the work cannot be put back on its conversation: it had no address yet. */
158
+ export const NO_ADDRESS_YET = 'the browser window was closed before its conversation had an address, so it could not be found again; nothing more was sent';
159
+ /** How many times Edge is started again on the profile when a window is reopened, and the pause between (see `reopen`). */
160
+ const REOPEN_TRIES = 10;
161
+ const REOPEN_PAUSE_MS = 1_500;
162
+ /** How long the box is waited for on the work Copilot's host before the page is said not to be its chat. */
163
+ const SURFACE_WAIT_MS = 5_000;
143
164
  /**
144
165
  * The window had left the conversation the bot put it on, and could not be put back (operator, 2026-11-09; see
145
166
  * `ensureOnChat`): the conversation does not open again, or the sign-in the page asked for first was not done in time.
@@ -289,11 +310,22 @@ export class CopilotTransport {
289
310
  chatNames = new Map();
290
311
  /** How long a page is given to show the composer before it is taken to be asking for a sign-in. */
291
312
  firstLookMs = 15_000;
313
+ /**
314
+ * The host the chat was last found on, which the chat's addresses are opened on (2026-11-17): the address set in
315
+ * Settings when it is the work Copilot's, else the old host, until the chat is found on the new one. Going through
316
+ * the old host's redirect is what ended the work machine's start on Copilot's home page.
317
+ */
318
+ chatOrigin;
319
+ /** How long the box to write in must stay before the page is taken for the chat: one that showed on its way elsewhere is not. */
320
+ settleMs = 2_000;
321
+ /** How long another page of the work Copilot is given to turn into its chat on its own before the bot opens the chat. */
322
+ otherPageMs = 3_000;
292
323
  constructor(opts) {
293
324
  this.opts = opts;
294
325
  const { locators, problems } = resolvePickerLocators(opts.pickerLocators);
295
326
  this.locators = locators;
296
327
  this.locatorProblems = problems;
328
+ this.chatOrigin = surfaceOfUrl(opts.chatUrl) === 'work' ? new URL(opts.chatUrl).origin : Url.origin;
297
329
  }
298
330
  /** Says, once, which locators saved in Settings could not be used and why. */
299
331
  sayLocatorProblems() {
@@ -396,6 +428,41 @@ export class CopilotTransport {
396
428
  closedOutside() {
397
429
  return this.gone;
398
430
  }
431
+ /**
432
+ * The window again, after it was closed under the work (operator, 2026-11-13: "if I close the browser with the chat by
433
+ * accident, the bot should carry the task on from where it got to"). Edge is started again on the same profile, and
434
+ * the window put back on the conversation it was on: by its address, after a sign-in when the page asks for one, or by
435
+ * its name in the sidebar (see `ensureOnChat`). The Edge that was closed may still be on its way out and holding the
436
+ * profile, so the start is tried again for a while. A conversation that had no address yet — closed before its first
437
+ * answer — cannot be found again: `WindowClosed`, with the new window open on nothing. When and how often this is done
438
+ * is the runner's (`KeptWindow` in keptWindow.ts); this only does it.
439
+ */
440
+ async reopen(opts = {}) {
441
+ // What `close` leaves alone, kept: the conversation the window was on, and the names given to conversations.
442
+ const want = this.onChat;
443
+ await this.close();
444
+ for (let tries = 1;; tries += 1) {
445
+ try {
446
+ await this.open();
447
+ break;
448
+ }
449
+ catch (e) {
450
+ await this.close().catch(() => undefined);
451
+ const busy = /already running with this profile|already in use by copilot-operator/i.test(e.message);
452
+ if (!busy || tries >= REOPEN_TRIES || opts.signal?.aborted)
453
+ throw e;
454
+ await new Promise((r) => setTimeout(r, REOPEN_PAUSE_MS));
455
+ }
456
+ }
457
+ this.onChat = want;
458
+ if (!want || want === 'new')
459
+ throw new WindowClosed(NO_ADDRESS_YET);
460
+ await this.p.goto(this.chatAddress(Url.conversationPath(want.id)), { waitUntil: 'domcontentloaded' }).catch(() => undefined);
461
+ // On it, as nearly always: waited for until its box is there to write in. Elsewhere (a sign-in): `ensureOnChat`.
462
+ if (parseChatId(this.p.url()) === want.id)
463
+ await this.composerWithin(this.firstLookMs, opts.signal, 'stopped by the operator while the window was opened again');
464
+ return await this.ensureOnChat(opts);
465
+ }
399
466
  /**
400
467
  * A card over the chat's page with one button that takes it away (2026-10-19: "when the chat is done, let the
401
468
  * browser show a message — the task has finished — and an OK under it"). The window of the workbench's chat is
@@ -510,6 +577,115 @@ export class CopilotTransport {
510
577
  }
511
578
  }
512
579
  }
580
+ /** One of the chat's addresses (`Url.newChatPath`, `Url.conversationPath`) on the host the chat was last found on. */
581
+ chatAddress(path) {
582
+ return `${this.chatOrigin}${path}`;
583
+ }
584
+ /** Keeps the host the page is on when it is one of the chat's addresses: the chat's addresses are opened there from then on. */
585
+ learnChatOrigin() {
586
+ const url = this.p.url();
587
+ if (isWorkChatAddress(url))
588
+ this.chatOrigin = new URL(url).origin;
589
+ }
590
+ /** Whether the box to write in is still there, on the same page, after `settleMs`: one that showed on the way elsewhere is not. */
591
+ async boxStays() {
592
+ const at = whereIs(this.p.url());
593
+ await this.p.waitForTimeout(this.settleMs);
594
+ return whereIs(this.p.url()) === at && (await this.composer().isVisible().catch(() => false));
595
+ }
596
+ /**
597
+ * After the window was sent to the chat (`path`, one of its addresses): whether it got there. Operator, 2026-11-17:
598
+ * the start ended on "The page is not the Microsoft 365 Copilot chat" at
599
+ * `copilot.cloud.microsoft/home?es=SSR&redirfrom=mcmToCcm` — the box to write in had shown, and a few seconds later the
600
+ * page was Copilot's home page, with none, and the bot neither waited nor went to the chat from there.
601
+ *
602
+ * - `chat`: the box is there and stays (`boxStays`). The host it is on is the chat's from then on (`learnChatOrigin`).
603
+ * - `elsewhere`: the page is another of the work Copilot's own (`isCopilotOtherPage`), and stayed one after the chat
604
+ * was opened from it — by `path` on the page's own host, then, for a new chat, by the page's link to its chat. It
605
+ * is not a sign-in, and is not waited on as one.
606
+ * - `no-box`: anything else with no box within `firstLookMs` — a sign-in page, the consumer Copilot, a chat slow to
607
+ * load — left to the caller, as before.
608
+ *
609
+ * Looked at a second at a time, so a Stop ends it at once; another of Copilot's pages is given `otherPageMs` to turn
610
+ * into the chat on its own, not the whole first look.
611
+ */
612
+ async arrive(path, signal, stopped) {
613
+ for (let move = 0;; move += 1) {
614
+ const until = Date.now() + this.firstLookMs;
615
+ let otherSince = 0;
616
+ for (;;) {
617
+ if ((await this.composerWithin(Math.max(1, Math.min(1_000, until - Date.now())), signal, stopped)) && (await this.boxStays())) {
618
+ this.learnChatOrigin();
619
+ return 'chat';
620
+ }
621
+ const other = isCopilotOtherPage(this.p.url());
622
+ otherSince = other ? otherSince || Date.now() : 0;
623
+ if (other && Date.now() - otherSince >= this.otherPageMs)
624
+ break;
625
+ if (Date.now() >= until) {
626
+ if (other)
627
+ break;
628
+ return 'no-box';
629
+ }
630
+ }
631
+ if (move >= 2)
632
+ return 'elsewhere';
633
+ const found = whereIs(this.p.url());
634
+ if (move === 0) {
635
+ const to = `${new URL(this.p.url()).origin}${path}`;
636
+ this.emit('chat-page-elsewhere', { found, by: 'address', to: whereIs(to) });
637
+ await this.p.goto(to, { waitUntil: 'domcontentloaded' });
638
+ }
639
+ else {
640
+ // A conversation's address is the only way to it: a link to the chat would open a new one.
641
+ if (path !== Url.newChatPath)
642
+ return 'elsewhere';
643
+ const link = await this.chatLink();
644
+ if (!link)
645
+ return 'elsewhere';
646
+ this.emit('chat-page-elsewhere', { found, by: 'link', to: whereIs(link.href) });
647
+ await link.at.click({ timeout: 5_000 }).catch(() => undefined);
648
+ }
649
+ }
650
+ }
651
+ /**
652
+ * The page's own link to its chat, for another of Copilot's pages that the chat's address did not get past: an anchor
653
+ * on the page's host to `/chat`, shown, and opening in this window. Nothing else on the page is clicked.
654
+ */
655
+ async chatLink() {
656
+ const found = await this.p
657
+ .evaluate(() => {
658
+ const links = [...document.querySelectorAll('a[href]')];
659
+ const i = links.findIndex((a) => {
660
+ let u;
661
+ try {
662
+ u = new URL(a.href);
663
+ }
664
+ catch {
665
+ return false;
666
+ }
667
+ const box = a.getBoundingClientRect();
668
+ const shown = box.width > 0 && box.height > 0 && getComputedStyle(a).visibility !== 'hidden';
669
+ return u.origin === location.origin && u.pathname.replace(/\/+$/, '') === '/chat' && (!a.target || a.target === '_self') && shown;
670
+ });
671
+ return i < 0 ? null : { i, href: links[i].href };
672
+ })
673
+ .catch(() => null);
674
+ return found ? { at: this.p.locator('a[href]').nth(found.i), href: found.href } : null;
675
+ }
676
+ /**
677
+ * The error for a window that would not go to the chat from another of Copilot's pages (`arrive`'s `elsewhere`), with
678
+ * a picture of the page — a screenshot always, its HTML when Settings keep it — so the next look is at what it showed.
679
+ */
680
+ async notTheChat() {
681
+ const url = this.p.url();
682
+ const dir = join(this.opts.transportDir, '..', 'failures');
683
+ const tag = `not-the-chat-${Date.now()}`;
684
+ await this.dumpFailure(dir, tag).catch(() => undefined);
685
+ return new Error(`Copilot showed its page ${whereIs(url)} instead of the chat, and the chat did not open from there ` +
686
+ `(by its address on that host, nor by a link to it on the page). Nothing was sent. A picture of the page: ${join(dir, `${tag}.png`)}
687
+ Current URL: ${url}`);
688
+ }
513
689
  /**
514
690
  * Which Copilot we actually landed on.
515
691
  *
@@ -579,9 +755,17 @@ export class CopilotTransport {
579
755
  this.onChat = null;
580
756
  await this.p.goto(target.toString(), { waitUntil: 'domcontentloaded' });
581
757
  }
582
- /** Throws with an explanation when the profile is not on the work Copilot. */
758
+ /**
759
+ * Throws with an explanation when the profile is not on the work Copilot. On the work Copilot's host with no box to
760
+ * write in, the box is waited for a few seconds rather than looked for once (2026-11-17: the page was changing); a
761
+ * page that is still not the chat leaves a picture of itself, since its address alone does not say what it showed.
762
+ */
583
763
  async assertWorkSurface() {
584
- const where = await this.surface();
764
+ let where = await this.surface();
765
+ if (where === 'unknown' && surfaceOfUrl(this.p.url()) === 'work') {
766
+ await this.composerWithin(SURFACE_WAIT_MS, undefined, '');
767
+ where = await this.surface();
768
+ }
585
769
  if (where === 'work')
586
770
  return;
587
771
  const url = this.p.url();
@@ -592,7 +776,14 @@ export class CopilotTransport {
592
776
  'sign-in': 'The profile is signed out. Run "cop login".',
593
777
  unknown: 'The page is not the Microsoft 365 Copilot chat.',
594
778
  };
595
- throw new Error(`${explain[where] ?? explain.unknown}
779
+ let picture = '';
780
+ if (where === 'unknown') {
781
+ const dir = join(this.opts.transportDir, '..', 'failures');
782
+ const tag = `not-the-chat-${Date.now()}`;
783
+ await this.dumpFailure(dir, tag).catch(() => undefined);
784
+ picture = ` A picture of the page: ${join(dir, `${tag}.png`)}`;
785
+ }
786
+ throw new Error(`${explain[where] ?? explain.unknown}${picture}
596
787
  Current URL: ${url}`);
597
788
  }
598
789
  /**
@@ -694,29 +885,48 @@ Current URL: ${url}`);
694
885
  * ends the wait at once, rather than after the whole sign-in timeout, fifteen minutes by default (2026-10-17).
695
886
  */
696
887
  const stoppedHere = 'stopped by the operator while waiting for the sign-in';
697
- if (!(await this.composerWithin(this.firstLookMs, opts.signal, stoppedHere))) {
888
+ /*
889
+ * The chat; or another of Copilot's own pages, which the chat is opened from (2026-11-17: Copilot's home page, where
890
+ * the work machine's start ended); or a page asking for a sign-in, the only one waited on for the operator.
891
+ */
892
+ let at = await this.arrive(Url.newChatPath, opts.signal, stoppedHere);
893
+ if (at === 'no-box') {
698
894
  this.emit('sign-in-required', { url: this.p.url() });
699
895
  if (!(await this.composerWithin(this.opts.signInTimeoutMs, opts.signal, stoppedHere))) {
700
896
  throw new Error(`The sign-in was not completed within ${Math.round(this.opts.signInTimeoutMs / 1000)} s (Settings → copilot.signInTimeoutSec). ` +
701
897
  'Sign in in the open Edge window, then start again.');
702
898
  }
703
899
  this.emit('signed-in');
900
+ // A sign-in may come back to Copilot's home page rather than to its chat.
901
+ at = await this.arrive(Url.newChatPath, opts.signal, stoppedHere);
704
902
  }
903
+ if (at === 'elsewhere')
904
+ throw await this.notTheChat();
705
905
  await this.clearBlockers();
906
+ // The page may have moved on while the blockers were looked at, as the work machine's did: once more to the chat.
907
+ if ((await this.surface()) !== 'work' && (await this.arrive(Url.newChatPath, opts.signal, stoppedHere)) === 'elsewhere') {
908
+ throw await this.notTheChat();
909
+ }
706
910
  await this.assertWorkSurface();
707
911
  }
708
912
  async newChat() {
709
913
  this.onChat = null;
710
- await this.p.goto(Url.newChat, { waitUntil: 'domcontentloaded' });
711
- await this.composer().waitFor({ state: 'visible' });
914
+ await this.p.goto(this.chatAddress(Url.newChatPath), { waitUntil: 'domcontentloaded' });
915
+ // Another of Copilot's pages instead (2026-11-17): the chat opened from it, and said so when it would not.
916
+ if ((await this.arrive(Url.newChatPath, undefined, '')) === 'elsewhere')
917
+ throw await this.notTheChat();
918
+ // A chat slow to load: the rest of the minute it was always given (the page's default timeout).
919
+ await this.composer().waitFor({ state: 'visible', timeout: 45_000 });
712
920
  await this.dismissPopups();
713
921
  this.onChat = 'new';
714
922
  }
715
923
  /** Reopens a conversation by its id. Returns false when it no longer opens. */
716
924
  async openConversation(chatId) {
717
925
  this.onChat = null;
718
- await this.p.goto(Url.conversation(chatId), { waitUntil: 'domcontentloaded' });
719
- if (!(await this.isChatReady(20_000)))
926
+ const path = Url.conversationPath(chatId);
927
+ await this.p.goto(this.chatAddress(path), { waitUntil: 'domcontentloaded' });
928
+ const at = await this.arrive(path, undefined, '');
929
+ if (at === 'elsewhere' || (at === 'no-box' && !(await this.isChatReady(5_000))))
720
930
  return false;
721
931
  const there = parseChatId(this.p.url()) === chatId;
722
932
  if (there)
@@ -788,9 +998,14 @@ Current URL: ${url}`);
788
998
  this.emit('chat-left', { chatId: want.id, found: from });
789
999
  const stopped = 'stopped by the operator while waiting for the sign-in';
790
1000
  let signedIn = false;
1001
+ const path = Url.conversationPath(want.id);
791
1002
  for (let tries = 0; tries < 3; tries += 1) {
792
- await this.p.goto(Url.conversation(want.id), { waitUntil: 'domcontentloaded' });
793
- if (await this.composerWithin(this.firstLookMs, opts.signal, stopped)) {
1003
+ await this.p.goto(this.chatAddress(path), { waitUntil: 'domcontentloaded' });
1004
+ // Another of Copilot's pages, the conversation's address not getting past it (2026-11-17): by its name, not a sign-in.
1005
+ const at = await this.arrive(path, opts.signal, stopped);
1006
+ if (at === 'elsewhere')
1007
+ break;
1008
+ if (at === 'chat') {
794
1009
  if (parseChatId(this.p.url()) !== want.id)
795
1010
  break;
796
1011
  this.onChat = want;
@@ -0,0 +1,247 @@
1
+ import { WindowClosed } from './copilotTransport.js';
2
+ /** The chat asked for the answer it was writing when the window closed. */
3
+ export const ASK_AGAIN = 'The browser window was closed while you were writing your answer to my last message, so your answer did not reach ' +
4
+ 'me. Send that answer again, in full and in the same format as before.';
5
+ /** Put before a message sent again after the window closed while it was being sent. */
6
+ export const SENT_AGAIN = '(Sent again: the browser window was closed while this message was being sent. If it reached you already, answer ' +
7
+ 'this copy only.)';
8
+ /** Why a message stopped when its window was closed and Settings say not to open it again. */
9
+ export const CLOSED_NOT_REOPENED = 'the browser window was closed; nothing more was sent';
10
+ export class KeptWindow {
11
+ inner;
12
+ speak;
13
+ /** Whether a message is at work: only then is a window closed under it opened again. */
14
+ atWork = false;
15
+ /** How many times it may still be opened again for the message at work, of how many. */
16
+ left = 0;
17
+ allowed = 0;
18
+ signal;
19
+ /** It is being opened again now. */
20
+ back = null;
21
+ /** Why the work on the message cannot go on in it: closed past the number, or not put back on its conversation. */
22
+ gaveUp = null;
23
+ /** How many times it has gone, so a call can tell it went while it ran. */
24
+ generation = 0;
25
+ /** Which of those the last message sent went out in: an answer is read only from the window its message went in. */
26
+ sentIn = -1;
27
+ constructor(inner, speak = () => undefined) {
28
+ this.inner = inner;
29
+ this.speak = speak;
30
+ }
31
+ /**
32
+ * A message is at work: a window closed under it is opened again up to `times` times (`limits.reopenWindow`; 0 is
33
+ * none, as before 2026-11-13), and its Stop ends a reopening under way. Called for each message the runner takes.
34
+ */
35
+ holdWindow(times, signal) {
36
+ this.atWork = true;
37
+ this.allowed = Math.max(0, Math.floor(times));
38
+ this.left = this.allowed;
39
+ this.signal = signal;
40
+ this.gaveUp = null;
41
+ }
42
+ /**
43
+ * No message at work any more: a window closed now is the next message's to open. Waits for a reopening under way; the
44
+ * window is then what it is — open, or gone — for whoever holds it next.
45
+ */
46
+ async releaseWindow() {
47
+ this.atWork = false;
48
+ await this.back?.catch(() => undefined);
49
+ this.gaveUp = null;
50
+ }
51
+ /** How many times the window was opened again for the message at work. */
52
+ reopenedTimes() {
53
+ return this.allowed - this.left;
54
+ }
55
+ /** Told by the window when it went (its `window-closed`): opened again at once while a message is at work. */
56
+ windowWent() {
57
+ this.generation += 1;
58
+ if (!this.atWork || this.back)
59
+ return;
60
+ this.back = this.bringBack().finally(() => {
61
+ this.back = null;
62
+ });
63
+ }
64
+ async bringBack() {
65
+ for (;;) {
66
+ if (this.signal?.aborted) {
67
+ this.gaveUp = new WindowClosed('the browser window was closed, and the work was stopped before it was opened again; nothing more was sent');
68
+ return;
69
+ }
70
+ if (this.left <= 0) {
71
+ this.gaveUp = new WindowClosed(this.allowed === 0
72
+ ? CLOSED_NOT_REOPENED
73
+ : `the browser window was closed ${this.allowed + 1} times while this message was at work; it was opened again ${this.allowed} time(s), ` +
74
+ 'as many as Settings allow for one message (limits.reopenWindow), so nothing more was sent');
75
+ this.speak('window-not-reopened', { reason: this.gaveUp.message, times: this.allowed });
76
+ return;
77
+ }
78
+ this.left -= 1;
79
+ const time = this.allowed - this.left;
80
+ const gen = this.generation;
81
+ this.speak('window-reopening', { time, of: this.allowed });
82
+ try {
83
+ const at = await this.inner.reopen({ signal: this.signal });
84
+ if (this.generation !== gen)
85
+ continue;
86
+ this.speak('window-reopened', { time, of: this.allowed, ...('chatId' in at ? { chatId: at.chatId } : {}) });
87
+ return;
88
+ }
89
+ catch (e) {
90
+ // Closed again while it was being opened: another time, if there is one left.
91
+ if (this.generation !== gen && !this.signal?.aborted)
92
+ continue;
93
+ const why = e.message;
94
+ this.gaveUp = new WindowClosed(e instanceof WindowClosed
95
+ ? why
96
+ : this.signal?.aborted
97
+ ? 'the browser window was closed, and the work was stopped while it was being opened again; nothing more was sent'
98
+ : `the browser window was closed, and it could not be opened again on its conversation: ${why}`);
99
+ this.speak('window-not-reopened', { reason: this.gaveUp.message, times: time });
100
+ return;
101
+ }
102
+ }
103
+ }
104
+ /** Before a call that needs the window: one being opened again is waited for; one that cannot be, said. */
105
+ async ready() {
106
+ if (!this.back && this.atWork && !this.gaveUp && this.inner.closedOutside())
107
+ this.windowWent();
108
+ if (this.back)
109
+ await this.back;
110
+ if (this.gaveUp)
111
+ throw this.gaveUp;
112
+ }
113
+ /**
114
+ * A call on the window, made again when the window went while it ran and was opened again; `again` says it is the
115
+ * second time. Bounded by the number of times it may be opened: past it, `ready` throws.
116
+ */
117
+ async withWindow(call) {
118
+ let again = false;
119
+ for (;;) {
120
+ await this.ready();
121
+ const gen = this.generation;
122
+ try {
123
+ return await call(again);
124
+ }
125
+ catch (e) {
126
+ const went = gen !== this.generation || this.inner.closedOutside();
127
+ if (!went || !this.atWork)
128
+ throw e;
129
+ again = true;
130
+ }
131
+ }
132
+ }
133
+ async sendAndConfirm(text, attachments = [], maxAttempts) {
134
+ let wentIn = -1;
135
+ const before = await this.withWindow(async (again) => {
136
+ wentIn = this.generation;
137
+ if (again)
138
+ this.speak('message-sent-again', { chars: text.length });
139
+ return await this.inner.sendAndConfirm(again ? `${SENT_AGAIN}\n\n${text}` : text, attachments, maxAttempts);
140
+ });
141
+ // The window it went out in: closed as it was taken, its answer is asked for again (see `waitForReply`).
142
+ this.sentIn = wentIn;
143
+ return before;
144
+ }
145
+ async waitForReply(previousTurns) {
146
+ return await this.withWindow(async (again) => {
147
+ if (!again && this.sentIn === this.generation)
148
+ return await this.inner.waitForReply(previousTurns);
149
+ // The window its message went out in is gone, and with it the answer being written: asked for again.
150
+ const asked = this.generation;
151
+ const before = await this.inner.sendAndConfirm(ASK_AGAIN);
152
+ this.sentIn = asked;
153
+ this.speak('reply-asked-again', { text: ASK_AGAIN });
154
+ if (this.generation !== asked)
155
+ throw new WindowClosed('closed again as the answer was asked for');
156
+ return await this.inner.waitForReply(before);
157
+ });
158
+ }
159
+ /** Whether the window is gone for the work: closed, and not being opened again, or past opening again. */
160
+ closedOutside() {
161
+ if (this.gaveUp)
162
+ return true;
163
+ if (this.back)
164
+ return false;
165
+ return this.inner.closedOutside();
166
+ }
167
+ // --- the rest, as the window does them; made again in a window opened again under the work ----------------------
168
+ open() {
169
+ return this.inner.open();
170
+ }
171
+ async close() {
172
+ this.atWork = false;
173
+ await this.back?.catch(() => undefined);
174
+ await this.inner.close();
175
+ }
176
+ reopen(opts) {
177
+ return this.inner.reopen(opts);
178
+ }
179
+ isOpen() {
180
+ return this.inner.isOpen();
181
+ }
182
+ ensureSignedIn(...a) {
183
+ return this.inner.ensureSignedIn(...a);
184
+ }
185
+ signOut(...a) {
186
+ return this.inner.signOut(...a);
187
+ }
188
+ gotoChatAs(...a) {
189
+ return this.inner.gotoChatAs(...a);
190
+ }
191
+ findAccountsInPage(...a) {
192
+ return this.inner.findAccountsInPage(...a);
193
+ }
194
+ dumpFailure(...a) {
195
+ return this.inner.dumpFailure(...a);
196
+ }
197
+ recentCrash(...a) {
198
+ return this.inner.recentCrash(...a);
199
+ }
200
+ showNotice(...a) {
201
+ return this.withWindow(() => this.inner.showNotice(...a));
202
+ }
203
+ clearNotice(...a) {
204
+ return this.withWindow(() => this.inner.clearNotice(...a));
205
+ }
206
+ newChat(...a) {
207
+ return this.withWindow(() => this.inner.newChat(...a));
208
+ }
209
+ openConversation(...a) {
210
+ return this.withWindow(() => this.inner.openConversation(...a));
211
+ }
212
+ openConversationByName(...a) {
213
+ return this.withWindow(() => this.inner.openConversationByName(...a));
214
+ }
215
+ currentChatId(...a) {
216
+ return this.withWindow(() => this.inner.currentChatId(...a));
217
+ }
218
+ ensureOnChat(...a) {
219
+ return this.withWindow(() => this.inner.ensureOnChat(...a));
220
+ }
221
+ nameChat(...a) {
222
+ return this.withWindow(() => this.inner.nameChat(...a));
223
+ }
224
+ selectModel(...a) {
225
+ return this.withWindow(() => this.inner.selectModel(...a));
226
+ }
227
+ currentModel(...a) {
228
+ return this.withWindow(() => this.inner.currentModel(...a));
229
+ }
230
+ listModels(...a) {
231
+ return this.withWindow(() => this.inner.listModels(...a));
232
+ }
233
+ }
234
+ /** A window kept for the work, its `window-closed` heard as it happens. */
235
+ export function keptWindow(make, opts) {
236
+ let kept = null;
237
+ const inner = make({
238
+ ...opts,
239
+ onEvent: (event, detail) => {
240
+ opts.onEvent?.(event, detail);
241
+ if (event === 'window-closed')
242
+ kept?.windowWent();
243
+ },
244
+ });
245
+ kept = new KeptWindow(inner, (event, detail) => opts.onEvent?.(event, detail));
246
+ return kept;
247
+ }
@@ -382,13 +382,23 @@ export const Surface = {
382
382
  /** Chips that only the consumer surface shows. */
383
383
  consumerMarkers: ['Shop', 'Play'],
384
384
  };
385
- /** Chat URL. Microsoft is consolidating this to copilot.cloud.microsoft. */
385
+ /**
386
+ * Chat URL. Microsoft is consolidating this to copilot.cloud.microsoft.
387
+ *
388
+ * The bot starts on `origin`; from then on a window goes to the chat's addresses on the host it found the chat on
389
+ * (`chatOrigin` in copilotTransport.ts), not through the old host's redirect: on 2026-11-17 the work machine's
390
+ * `m365.cloud.microsoft/chat` ended on `copilot.cloud.microsoft/home?es=SSR&redirfrom=mcmToCcm`, Copilot's home page,
391
+ * with no box to write in.
392
+ */
386
393
  export const Url = {
387
394
  chat: 'https://m365.cloud.microsoft/chat',
388
- /** "New chat" is an anchor, so a plain navigation is equivalent to clicking it. */
395
+ origin: 'https://m365.cloud.microsoft',
396
+ /** A new chat, on whichever host. "New chat" is an anchor, so a plain navigation is equivalent to clicking it. */
397
+ newChatPath: '/chat?es=SSR',
389
398
  newChat: 'https://m365.cloud.microsoft/chat?es=SSR',
390
- /** Every conversation has a stable address: /chat/conversation/<uuid>. */
391
- conversation: (id) => `https://m365.cloud.microsoft/chat/conversation/${id}?es=SSR`,
399
+ /** Every conversation has a stable address: /chat/conversation/<uuid>, on whichever host. */
400
+ conversationPath: (id) => `/chat/conversation/${id}?es=SSR`,
401
+ conversation: (id, origin = 'https://m365.cloud.microsoft') => `${origin}/chat/conversation/${id}?es=SSR`,
392
402
  /** Full chat list, used as the last resort when looking a chat up by name. */
393
403
  allChats: 'https://m365.cloud.microsoft/chat/all?es=SSR',
394
404
  };