File size: 2,424 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type {
  McpConnectionManager,
  McpConnectionView,
  McpServerEntry,
  McpStatusListener,
} from '#/mcpCore/connection-manager';
import type { McpServerConfig } from '#/mcpCore/config-schema';
import type { McpOAuthService } from '#/mcpCore/oauth/service';
import { abortable } from '#/_base/utils/abort';

export class MergedMcpConnectionView implements McpConnectionView {
  constructor(
    private readonly base: McpConnectionManager,
    private readonly overlay: McpConnectionManager,
    private readonly overlayNames: ReadonlySet<string>,
  ) {}

  get oauthService(): McpOAuthService | undefined {
    return this.overlay.oauthService ?? this.base.oauthService;
  }

  list(): readonly McpServerEntry[] {
    const baseEntries = this.base.list().filter((entry) => !this.overlayNames.has(entry.name));
    return [...baseEntries, ...this.overlay.list()];
  }

  get(name: string): McpServerEntry | undefined {
    return this.owner(name).get(name);
  }

  configOf(name: string): McpServerConfig | undefined {
    return this.owner(name).configOf(name);
  }

  resolved(name: string): ReturnType<McpConnectionView['resolved']> {
    return this.owner(name).resolved(name);
  }

  getRemoteServerUrl(name: string): string | undefined {
    return this.owner(name).getRemoteServerUrl(name);
  }

  reconnect(name: string): Promise<void> {
    return this.owner(name).reconnect(name);
  }

  reconnectAndJoin(name: string): Promise<void> {
    return this.owner(name).reconnectAndJoin(name);
  }

  waitForInitialLoad(signal?: AbortSignal): Promise<void> {
    signal?.throwIfAborted();
    const both = Promise.all([
      this.base.waitForInitialLoad(),
      this.overlay.waitForInitialLoad(),
    ]).then(() => undefined);
    return signal === undefined ? both : abortable(both, signal);
  }

  initialLoadDurationMs(): number {
    return Math.max(this.base.initialLoadDurationMs(), this.overlay.initialLoadDurationMs());
  }

  onStatusChange(listener: McpStatusListener): () => void {
    const unsubscribeBase = this.base.onStatusChange((entry) => {
      if (!this.overlayNames.has(entry.name)) listener(entry);
    });
    const unsubscribeOverlay = this.overlay.onStatusChange(listener);
    return () => {
      unsubscribeBase();
      unsubscribeOverlay();
    };
  }

  private owner(name: string): McpConnectionManager {
    return this.overlayNames.has(name) ? this.overlay : this.base;
  }
}