Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: forward status of copilot to footer icon (#7312)

Myles Scolnickc54359dc7e0a
diff --git a/frontend/src/components/editor/chrome/wrapper/footer-items/copilot-status.tsx b/frontend/src/components/editor/chrome/wrapper/footer-items/copilot-status.tsx
index 3f80e3c135..4da68b63ec 100644
--- a/frontend/src/components/editor/chrome/wrapper/footer-items/copilot-status.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/footer-items/copilot-status.tsx
@@ -10,6 +10,7 @@ import { toast } from "@/components/ui/use-toast";
import { getCopilotClient } from "@/core/codemirror/copilot/client";
import {
copilotSignedInState,
+ copilotStatusState,
githubCopilotLoadingVersion,
isGitHubCopilotSignedInState,
} from "@/core/codemirror/copilot/state";
@@ -40,10 +41,18 @@ const logger = Logger.get("[copilot-status-bar]");
const GitHubCopilotStatus: React.FC = () => {
const isGitHubCopilotSignedIn = useAtomValue(isGitHubCopilotSignedInState);
const isLoading = useAtomValue(githubCopilotLoadingVersion) !== null;
+ const status = useAtomValue(copilotStatusState);
const { handleClick } = useOpenSettingsToTab();
const openSettings = () => handleClick("ai");
- const label = isGitHubCopilotSignedIn ? "Ready" : "Not connected";
+ // Build label from status
+ let label = isGitHubCopilotSignedIn ? "Ready" : "Not connected";
+ if (status.message) {
+ label = status.message;
+ } else if (status.busy) {
+ label = "Processing...";
+ }
+
const setCopilotSignedIn = useSetAtom(isGitHubCopilotSignedInState);
const setStep = useSetAtom(copilotSignedInState);
@@ -100,11 +109,25 @@ const GitHubCopilotStatus: React.FC = () => {
};
});
+ // Determine icon color based on status
+ const iconColorClass =
+ status.kind === "Warning" || status.kind === "Error"
+ ? "text-(--yellow-11)"
+ : isGitHubCopilotSignedIn
+ ? ""
+ : "opacity-60";
+
return (
<FooterItem
tooltip={
<>
<b>GitHub Copilot:</b> {label}
+ {status.kind && (
+ <>
+ <br />
+ <span className="text-xs">Status: {status.kind}</span>
+ </>
+ )}
</>
}
selected={false}
@@ -112,12 +135,10 @@ const GitHubCopilotStatus: React.FC = () => {
data-testid="footer-copilot-status"
>
<span>
- {isLoading ? (
+ {isLoading || status.busy ? (
<Spinner className="h-4 w-4" />
) : (
- <GitHubCopilotIcon
- className={cn("h-4 w-4", !isGitHubCopilotSignedIn && "opacity-60")}
- />
+ <GitHubCopilotIcon className={cn("h-4 w-4", iconColorClass)} />
)}
</span>
</FooterItem>
diff --git a/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts b/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts
new file mode 100644
index 0000000000..94b041a43d
--- /dev/null
+++ b/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts
@@ -0,0 +1,439 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+/* eslint-disable @typescript-eslint/no-explicit-any */
+
+import { WebSocketTransport } from "@open-rpc/client-js";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { Mocks } from "@/__mocks__/common";
+import { LazyWebsocketTransport } from "../transport";
+
+// Mock the Logger
+vi.mock("@/utils/Logger", () => ({
+ Logger: Mocks.logger(),
+}));
+
+// Mock the WebSocketTransport
+vi.mock("@open-rpc/client-js", () => {
+ const mockWebSocketTransport = vi.fn();
+ mockWebSocketTransport.prototype.connect = vi.fn();
+ mockWebSocketTransport.prototype.close = vi.fn();
+ mockWebSocketTransport.prototype.sendData = vi.fn();
+ mockWebSocketTransport.prototype.subscribe = vi.fn();
+ mockWebSocketTransport.prototype.unsubscribe = vi.fn();
+
+ return {
+ WebSocketTransport: mockWebSocketTransport,
+ };
+});
+
+describe("LazyWebsocketTransport", () => {
+ const mockWsUrl = "ws://localhost:8080/copilot";
+ let mockGetWsUrl: ReturnType<typeof vi.fn>;
+ let mockWaitForReady: ReturnType<typeof vi.fn>;
+ let mockShowError: ReturnType<typeof vi.fn>;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+
+ mockGetWsUrl = vi.fn(() => mockWsUrl);
+ mockWaitForReady = vi.fn().mockResolvedValue(undefined);
+ mockShowError = vi.fn();
+
+ // Mock the WebSocketTransport constructor
+ (WebSocketTransport as any).mockImplementation(function (this: any) {
+ this.connect = vi.fn().mockResolvedValue(undefined);
+ this.close = vi.fn();
+ this.sendData = vi.fn().mockResolvedValue({ result: "success" });
+ this.subscribe = vi.fn();
+ this.unsubscribe = vi.fn();
+ });
+ });
+
+ describe("constructor", () => {
+ it("should create a transport with default options", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ expect(transport).toBeDefined();
+ expect(mockGetWsUrl).not.toHaveBeenCalled();
+ });
+
+ it("should accept custom retry options", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ retries: 5,
+ retryDelayMs: 2000,
+ maxTimeoutMs: 10_000,
+ });
+
+ expect(transport).toBeDefined();
+ });
+ });
+
+ describe("connect", () => {
+ it("should wait for prerequisites before connecting", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ expect(mockWaitForReady).toHaveBeenCalledTimes(1);
+ expect(mockGetWsUrl).toHaveBeenCalledTimes(1);
+ expect(WebSocketTransport).toHaveBeenCalledWith(mockWsUrl);
+ });
+
+ it("should create delegate and connect", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ const delegate = (transport as any).delegate;
+ expect(delegate).toBeDefined();
+ expect(delegate.connect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should retry on connection failure", async () => {
+ let attemptCount = 0;
+ (WebSocketTransport as any).mockImplementation(function (this: any) {
+ this.connect = vi.fn().mockImplementation(() => {
+ attemptCount++;
+ if (attemptCount < 2) {
+ return Promise.reject(new Error("Connection failed"));
+ }
+ return Promise.resolve(undefined);
+ });
+ this.close = vi.fn();
+ this.sendData = vi.fn();
+ this.subscribe = vi.fn();
+ this.unsubscribe = vi.fn();
+ });
+
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ retries: 3,
+ retryDelayMs: 10,
+ });
+
+ await transport.connect();
+
+ expect(attemptCount).toBe(2);
+ });
+
+ it("should show error toast on final retry failure", async () => {
+ const connectionError = new Error("Connection failed");
+ (WebSocketTransport as any).mockImplementation(function (this: any) {
+ this.connect = vi.fn().mockRejectedValue(connectionError);
+ this.close = vi.fn();
+ this.sendData = vi.fn();
+ this.subscribe = vi.fn();
+ this.unsubscribe = vi.fn();
+ });
+
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ retries: 2,
+ retryDelayMs: 10,
+ });
+
+ await expect(transport.connect()).rejects.toThrow("Connection failed");
+
+ expect(mockShowError).toHaveBeenCalledTimes(1);
+ expect(mockShowError).toHaveBeenCalledWith(
+ "GitHub Copilot Connection Error",
+ "Failed to connect to GitHub Copilot. Please check your settings and try again.",
+ );
+ expect((transport as any).delegate).toBeUndefined();
+ });
+
+ it("should handle waitForReady failure", async () => {
+ const waitError = new Error("Prerequisites not ready");
+ mockWaitForReady.mockRejectedValue(waitError);
+
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await expect(transport.connect()).rejects.toThrow(
+ "Prerequisites not ready",
+ );
+ expect(WebSocketTransport).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("subscribe", () => {
+ it("should register handler on parent and track subscription", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const handler = vi.fn();
+ transport.subscribe("notification", handler);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(1);
+ expect((transport as any).pendingSubscriptions[0]).toEqual({
+ event: "notification",
+ handler,
+ });
+ });
+
+ it("should register handler on delegate if it exists", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ const handler = vi.fn();
+ transport.subscribe("notification", handler);
+
+ const delegate = (transport as any).delegate;
+ expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler);
+ });
+
+ it("should register pending subscriptions when delegate is created", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const handler1 = vi.fn();
+ const handler2 = vi.fn();
+ transport.subscribe("notification", handler1);
+ transport.subscribe("response", handler2);
+
+ await transport.connect();
+
+ const delegate = (transport as any).delegate;
+ expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler1);
+ expect(delegate.subscribe).toHaveBeenCalledWith("response", handler2);
+ });
+ });
+
+ describe("unsubscribe", () => {
+ it("should remove specific handler for specific event", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const handler1 = vi.fn();
+ const handler2 = vi.fn();
+ transport.subscribe("notification", handler1);
+ transport.subscribe("notification", handler2);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(2);
+
+ transport.unsubscribe("notification", handler1);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(1);
+ expect((transport as any).pendingSubscriptions[0].handler).toBe(handler2);
+ });
+
+ it("should remove handler from all events when event not specified", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const handler = vi.fn();
+ transport.subscribe("notification", handler);
+ transport.subscribe("response", handler);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(2);
+
+ transport.unsubscribe(undefined, handler);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(0);
+ });
+
+ it("should remove all handlers for event when handler not specified", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const handler1 = vi.fn();
+ const handler2 = vi.fn();
+ transport.subscribe("notification", handler1);
+ transport.subscribe("notification", handler2);
+ transport.subscribe("response", handler1);
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(3);
+
+ transport.unsubscribe("notification");
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(1);
+ expect((transport as any).pendingSubscriptions[0].event).toBe("response");
+ });
+
+ it("should remove all subscriptions when neither event nor handler specified", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ transport.subscribe("notification", vi.fn());
+ transport.subscribe("response", vi.fn());
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(2);
+
+ transport.unsubscribe();
+
+ expect((transport as any).pendingSubscriptions).toHaveLength(0);
+ });
+
+ it("should unsubscribe from delegate if it exists", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ const handler = vi.fn();
+ transport.subscribe("notification", handler);
+ transport.unsubscribe("notification", handler);
+
+ const delegate = (transport as any).delegate;
+ expect(delegate.unsubscribe).toHaveBeenCalledWith(
+ "notification",
+ handler,
+ );
+ });
+ });
+
+ describe("sendData", () => {
+ it("should send data through delegate when connected", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ const data: any = { method: "test", params: [] };
+ const result = await transport.sendData(data, 5000);
+
+ const delegate = (transport as any).delegate;
+ expect(delegate.sendData).toHaveBeenCalledWith(data, 5000);
+ expect(result).toEqual({ result: "success" });
+ });
+
+ it("should reconnect if delegate is undefined", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ const data: any = { method: "test", params: [] };
+ await transport.sendData(data, 5000);
+
+ // sendData calls tryConnect directly, not connect, so wait functions aren't called
+ // But it should still create and connect the delegate
+ expect(WebSocketTransport).toHaveBeenCalled();
+ const delegate = (transport as any).delegate;
+ expect(delegate).toBeDefined();
+ expect(delegate.connect).toHaveBeenCalled();
+ });
+
+ it("should clamp timeout to maxTimeoutMs", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ maxTimeoutMs: 5000,
+ });
+
+ await transport.connect();
+
+ const data: any = { method: "test", params: [] };
+ await transport.sendData(data, 10_000);
+
+ const delegate = (transport as any).delegate;
+ expect(delegate.sendData).toHaveBeenCalledWith(data, 5000);
+ });
+
+ it("should throw error if reconnection fails", async () => {
+ const connectionError = new Error("Connection failed");
+ (WebSocketTransport as any).mockImplementation(function (this: any) {
+ this.connect = vi.fn().mockRejectedValue(connectionError);
+ this.close = vi.fn();
+ this.sendData = vi.fn();
+ this.subscribe = vi.fn();
+ this.unsubscribe = vi.fn();
+ });
+
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ retries: 1,
+ retryDelayMs: 10,
+ });
+
+ const data: any = { method: "test", params: [] };
+ await expect(transport.sendData(data, 5000)).rejects.toThrow(
+ "Unable to connect to GitHub Copilot",
+ );
+ });
+ });
+
+ describe("close", () => {
+ it("should close delegate and clear it", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ await transport.connect();
+
+ const delegate = (transport as any).delegate;
+ expect(delegate).toBeDefined();
+
+ transport.close();
+
+ expect(delegate.close).toHaveBeenCalledTimes(1);
+ expect((transport as any).delegate).toBeUndefined();
+ });
+
+ it("should handle close when delegate is undefined", () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: mockGetWsUrl,
+ waitForReady: mockWaitForReady,
+ showError: mockShowError,
+ });
+
+ expect(() => transport.close()).not.toThrow();
+ });
+ });
+});
diff --git a/frontend/src/core/codemirror/copilot/client.ts b/frontend/src/core/codemirror/copilot/client.ts
index 62c48a1303..b2a474b8c1 100644
--- a/frontend/src/core/codemirror/copilot/client.ts
+++ b/frontend/src/core/codemirror/copilot/client.ts
@@ -1,16 +1,13 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { languageServerWithClient } from "@marimo-team/codemirror-languageserver";
-import { WebSocketTransport } from "@open-rpc/client-js";
-import type { JSONRPCRequestData } from "@open-rpc/client-js/build/Request";
-import { Transport } from "@open-rpc/client-js/build/transports/Transport";
import { toast } from "@/components/ui/use-toast";
import { waitForConnectionOpen } from "@/core/network/connection";
import { getRuntimeManager } from "@/core/runtime/config";
-import { Logger } from "@/utils/Logger";
import { once } from "@/utils/once";
import { CopilotLanguageServerClient } from "./language-server";
import { waitForEnabledCopilot } from "./state";
+import { LazyWebsocketTransport } from "./transport";
// Dummy file for the copilot language server
export const COPILOT_FILENAME = "/__marimo_copilot__.py";
@@ -18,103 +15,23 @@ export const LANGUAGE_ID = "copilot";
const FILE_URI = `file://${COPILOT_FILENAME}`;
export const createWSTransport = once(() => {
- return new LazyWebsocketTransport();
+ const runtimeManager = getRuntimeManager();
+ return new LazyWebsocketTransport({
+ getWsUrl: () => runtimeManager.getLSPURL("copilot").toString(),
+ waitForReady: async () => {
+ await waitForEnabledCopilot();
+ await waitForConnectionOpen();
+ },
+ showError: (title, description) => {
+ toast({
+ variant: "danger",
+ title,
+ description,
+ });
+ },
+ });
});
-/**
- * Custom WSTransport that:
- * - waits for copilot to be enabled
- * - wait for the websocket to be available
- */
-class LazyWebsocketTransport extends Transport {
- private delegate: WebSocketTransport | undefined;
-
- constructor() {
- super();
- this.delegate = undefined;
- }
-
- private getWsUrl(): string {
- const runtimeManager = getRuntimeManager();
- return runtimeManager.getLSPURL("copilot").toString();
- }
-
- private async tryConnect(retries = 3, delayMs = 1000): Promise<void> {
- for (let attempt = 1; attempt <= retries; attempt++) {
- try {
- // Create delegate, if it doesn't exist
- if (!this.delegate) {
- this.delegate = new WebSocketTransport(this.getWsUrl());
- }
- await this.delegate.connect();
- Logger.log("Copilot#connect: Connected successfully");
- return;
- } catch (error) {
- Logger.warn(
- `Copilot#connect: Connection attempt ${attempt}/${retries} failed`,
- error,
- );
- if (attempt === retries) {
- this.delegate = undefined;
- // Show error toast on final retry
- toast({
- variant: "danger",
- title: "GitHub Copilot Connection Error",
- description:
- "Failed to connect to GitHub Copilot. Please check settings and try again.",
- });
- throw error;
- }
- await new Promise((resolve) => setTimeout(resolve, delayMs));
- }
- }
- }
-
- override async connect() {
- // Wait for copilot to be enabled
- await waitForEnabledCopilot();
- // Wait for ws to be available with retries
- await waitForConnectionOpen();
-
- // Try connecting with retries
- return this.tryConnect();
- }
-
- override close() {
- this.delegate?.close();
- this.delegate = undefined;
- }
-
- override async sendData(
- data: JSONRPCRequestData,
- timeout: number | null | undefined,
- ) {
- // If delegate is undefined, try to reconnect
- if (!this.delegate) {
- Logger.log("Copilot#sendData: Delegate not initialized, reconnecting...");
- try {
- await this.tryConnect();
- } catch (error) {
- Logger.error("Copilot#sendData: Failed to reconnect transport", error);
- throw new Error(
- "Unable to connect to GitHub Copilot. Please check your settings and try again.",
- );
- }
- }
-
- // After reconnection, delegate should be initialized
- if (!this.delegate) {
- throw new Error(
- "Failed to initialize GitHub Copilot connection. Please try again.",
- );
- }
-
- // Clamp timeout to 5 seconds
- timeout = Math.min(timeout ?? 5000, 5000);
- return this.delegate.sendData(data, timeout);
- }
-}
-
export const getCopilotClient = once(
() =>
new CopilotLanguageServerClient({
diff --git a/frontend/src/core/codemirror/copilot/language-server.ts b/frontend/src/core/codemirror/copilot/language-server.ts
index 56ec6e981f..773f196d06 100644
--- a/frontend/src/core/codemirror/copilot/language-server.ts
+++ b/frontend/src/core/codemirror/copilot/language-server.ts
@@ -15,16 +15,19 @@ import type {
InlineCompletionParams,
} from "vscode-languageserver-protocol";
import { VersionedTextDocumentIdentifier } from "vscode-languageserver-protocol";
+import { store } from "@/core/state/jotai";
import { Logger } from "@/utils/Logger";
import { getCodes } from "./getCodes";
import {
clearGitHubCopilotLoadingVersion,
+ copilotStatusState,
isCopilotEnabled,
setGitHubCopilotLoadingVersion,
} from "./state";
import type {
GitHubCopilotSignInConfirmParams,
GitHubCopilotSignInInitiateResult,
+ GitHubCopilotStatusNotificationParams,
GitHubCopilotStatusResult,
} from "./types";
@@ -42,6 +45,21 @@ export interface LSPRequestMap {
];
}
+export interface LSPEventMap {
+ statusNotification: GitHubCopilotStatusNotificationParams;
+ didChangeStatus: GitHubCopilotStatusNotificationParams;
+ "window/logMessage": { type: number; message: string };
+}
+
+export type EnhancedNotification = {
+ [key in keyof LSPEventMap]: {
+ jsonrpc: "2.0";
+ id?: null | undefined;
+ method: key;
+ params: LSPEventMap[key];
+ };
+}[keyof LSPEventMap];
+
/**
* A client for the Copilot language server.
*/
@@ -49,17 +67,23 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
private documentVersion = 0;
private hasOpenedDocument = false;
+ constructor(...args: ConstructorParameters<typeof LanguageServerClient>) {
+ super(...args);
+ this.onNotification(this.handleNotification);
+ }
+
private async _request<Method extends keyof LSPRequestMap>(
method: Method,
params: LSPRequestMap[Method][0],
): Promise<LSPRequestMap[Method][1]> {
- try {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return await (this as any).request(method, params);
- } catch (error) {
- logger.error("#request: Error", error);
- throw error;
- }
+ return await (
+ this as unknown as {
+ request: (
+ method: Method,
+ params: LSPRequestMap[Method][0],
+ ) => Promise<LSPRequestMap[Method][1]>;
+ }
+ ).request(method, params);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -252,4 +276,50 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
return response ?? null;
}
+
+ /**
+ * Handle notifications from the Copilot language server.
+ * Uses onNotification to listen for statusNotification, didChangeStatus, and window/logMessage.
+ */
+ private handleNotification: Parameters<
+ LanguageServerClient["onNotification"]
+ >[0] = (notif) => {
+ if (!notif.params) {
+ return;
+ }
+
+ const notification = notif as unknown as EnhancedNotification;
+
+ // Handle statusNotification
+ if (notification.method === "statusNotification") {
+ store.set(copilotStatusState, notification.params);
+ }
+
+ // Handle didChangeStatus
+ if (notification.method === "didChangeStatus") {
+ store.set(copilotStatusState, notification.params);
+ }
+
+ // Handle window/logMessage
+ if (notification.method === "window/logMessage") {
+ const params = notification.params as { type: number; message: string };
+ const { type, message } = params;
+ // Map LSP log types to console methods
+ // type: 1 = Error, 2 = Warning, 3 = Info, 4 = Log
+ switch (type) {
+ case 1: // Error
+ logger.error("[GitHub Copilot]", message);
+ break;
+ case 2: // Warning
+ logger.warn("[GitHub Copilot]", message);
+ break;
+ case 3: // Info
+ logger.debug("[GitHub Copilot]", message);
+ break;
+ default: // Log (type 4 and others)
+ logger.log("[GitHub Copilot]", message);
+ break;
+ }
+ }
+ };
}
diff --git a/frontend/src/core/codemirror/copilot/state.ts b/frontend/src/core/codemirror/copilot/state.ts
index fc121c43f7..4c0ec3c618 100644
--- a/frontend/src/core/codemirror/copilot/state.ts
+++ b/frontend/src/core/codemirror/copilot/state.ts
@@ -9,6 +9,7 @@ import {
import { store, waitFor } from "@/core/state/jotai";
import { jotaiJsonStorage } from "@/utils/storage/jotai";
import { availableStorage } from "@/utils/storage/storage";
+import type { GitHubCopilotStatusNotificationParams } from "./types";
const KEY = "marimo:copilot:signedIn";
@@ -34,6 +35,12 @@ export const copilotSignedInState = atom<Step | null>(null);
export const githubCopilotLoadingVersion = atom<number | null>(null);
+export const copilotStatusState = atom<GitHubCopilotStatusNotificationParams>({
+ busy: false,
+ kind: null,
+ message: null,
+});
+
/**
* Set the currently loading document version
*/
diff --git a/frontend/src/core/codemirror/copilot/transport.ts b/frontend/src/core/codemirror/copilot/transport.ts
new file mode 100644
index 0000000000..9082b6381d
--- /dev/null
+++ b/frontend/src/core/codemirror/copilot/transport.ts
@@ -0,0 +1,219 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { WebSocketTransport } from "@open-rpc/client-js";
+import type { JSONRPCRequestData } from "@open-rpc/client-js/build/Request";
+import { Transport } from "@open-rpc/client-js/build/transports/Transport";
+import { Logger } from "@/utils/Logger";
+
+export interface LazyWebsocketTransportOptions {
+ /**
+ * Function that returns the WebSocket URL to connect to.
+ */
+ getWsUrl: () => string;
+
+ /**
+ * Function to wait for before attempting to connect.
+ * This ensures all prerequisites (like copilot being enabled and runtime connection) are ready.
+ */
+ waitForReady: () => Promise<void>;
+
+ /**
+ * Function to show error toast notifications.
+ */
+ showError: (title: string, description: string) => void;
+
+ /**
+ * Number of retry attempts for connection.
+ * @default 3
+ */
+ retries?: number;
+
+ /**
+ * Delay between retry attempts in milliseconds.
+ * @default 1000
+ */
+ retryDelayMs?: number;
+
+ /**
+ * Maximum timeout for sendData operations in milliseconds.
+ * @default 5000
+ */
+ maxTimeoutMs?: number;
+}
+
+interface Subscription {
+ event: "pending" | "notification" | "response" | "error";
+ handler: Parameters<Transport["subscribe"]>[1];
+}
+
+/**
+ * A WebSocket transport that lazily connects after waiting for prerequisites.
+ *
+ * This transport:
+ * - Waits for copilot to be enabled before connecting
+ * - Waits for the websocket to be available before connecting
+ * - Forwards subscriptions/unsubscriptions to the delegate
+ * - Handles reconnection automatically
+ */
+export class LazyWebsocketTransport extends Transport {
+ private delegate: WebSocketTransport | undefined;
+ private pendingSubscriptions: Subscription[] = [];
+ private readonly options: Required<LazyWebsocketTransportOptions>;
+
+ constructor(options: LazyWebsocketTransportOptions) {
+ super();
+ this.delegate = undefined;
+ this.pendingSubscriptions = [];
+ this.options = {
+ retries: options.retries ?? 3,
+ retryDelayMs: options.retryDelayMs ?? 1000,
+ maxTimeoutMs: options.maxTimeoutMs ?? 5000,
+ getWsUrl: options.getWsUrl,
+ waitForReady: options.waitForReady,
+ showError: options.showError,
+ };
+ }
+
+ override subscribe(...args: Parameters<Transport["subscribe"]>): void {
+ // Register handler on parent Transport
+ super.subscribe(...args);
+
+ const [event, handler] = args;
+
+ // Track the subscription
+ this.pendingSubscriptions.push({ event, handler });
+
+ // Also register on delegate if it exists
+ if (this.delegate) {
+ this.delegate.subscribe(event, handler);
+ }
+ }
+
+ override unsubscribe(
+ ...args: Parameters<Transport["unsubscribe"]>
+ ): import("events").EventEmitter | undefined {
+ // Unsubscribe from parent Transport
+ const result = super.unsubscribe(...args);
+
+ const [event, handler] = args;
+ // Also unsubscribe from delegate if it exists
+ if (this.delegate) {
+ this.delegate.unsubscribe(event, handler);
+ }
+
+ // Remove from pending subscriptions if handler is provided
+ if (handler) {
+ if (event) {
+ // Remove specific handler for specific event
+ const index = this.pendingSubscriptions.findIndex(
+ (sub) => sub.event === event && sub.handler === handler,
+ );
+ if (index > -1) {
+ this.pendingSubscriptions.splice(index, 1);
+ }
+ } else {
+ // Remove handler from all events
+ this.pendingSubscriptions = this.pendingSubscriptions.filter(
+ (sub) => sub.handler !== handler,
+ );
+ }
+ } else if (event) {
+ // Remove all handlers for specific event
+ this.pendingSubscriptions = this.pendingSubscriptions.filter(
+ (sub) => sub.event !== event,
+ );
+ } else {
+ // Remove all subscriptions
+ this.pendingSubscriptions = [];
+ }
+
+ return result;
+ }
+
+ private createDelegate(): WebSocketTransport {
+ const delegate = new WebSocketTransport(this.options.getWsUrl());
+ // Register all pending subscriptions on the delegate
+ for (const { event, handler } of this.pendingSubscriptions) {
+ delegate.subscribe(event, handler);
+ }
+ return delegate;
+ }
+
+ private async tryConnect(): Promise<void> {
+ for (let attempt = 1; attempt <= this.options.retries; attempt++) {
+ try {
+ // Create delegate, if it doesn't exist
+ if (!this.delegate) {
+ this.delegate = this.createDelegate();
+ }
+ await this.delegate.connect();
+ Logger.log("Copilot#connect: Connected successfully");
+ return;
+ } catch (error) {
+ Logger.warn(
+ `Copilot#connect: Connection attempt ${attempt}/${this.options.retries} failed`,
+ error,
+ );
+ if (attempt === this.options.retries) {
+ this.delegate = undefined;
+ // Show error toast on final retry
+ this.options.showError(
+ "GitHub Copilot Connection Error",
+ "Failed to connect to GitHub Copilot. Please check your settings and try again.",
+ );
+ throw error;
+ }
+ await new Promise((resolve) =>
+ setTimeout(resolve, this.options.retryDelayMs),
+ );
+ }
+ }
+ }
+
+ override async connect(): Promise<void> {
+ // Wait for all prerequisites to be ready
+ await this.options.waitForReady();
+
+ // Try connecting with retries
+ return this.tryConnect();
+ }
+
+ override close(): void {
+ this.delegate?.close();
+ this.delegate = undefined;
+ }
+
+ override async sendData(
+ data: JSONRPCRequestData,
+ timeout: number | null | undefined,
+ ): Promise<unknown> {
+ // If delegate is undefined, try to reconnect
+ if (!this.delegate) {
+ Logger.log("Copilot#sendData: Delegate not initialized, reconnecting...");
+ try {
+ // Ensure prerequisites are ready before attempting connection
+ await this.options.waitForReady();
+ await this.tryConnect();
+ } catch (error) {
+ Logger.error("Copilot#sendData: Failed to reconnect transport", error);
+ throw new Error(
+ "Unable to connect to GitHub Copilot. Please check your settings and try again.",
+ );
+ }
+ }
+
+ // After reconnection, delegate should be initialized
+ if (!this.delegate) {
+ throw new Error(
+ "Failed to initialize GitHub Copilot connection. Please try again.",
+ );
+ }
+
+ // Clamp timeout to maxTimeoutMs
+ timeout = Math.min(
+ timeout ?? this.options.maxTimeoutMs,
+ this.options.maxTimeoutMs,
+ );
+ return this.delegate.sendData(data, timeout);
+ }
+}
diff --git a/frontend/src/core/codemirror/copilot/types.ts b/frontend/src/core/codemirror/copilot/types.ts
index aff3c9da76..8f1e807a15 100644
--- a/frontend/src/core/codemirror/copilot/types.ts
+++ b/frontend/src/core/codemirror/copilot/types.ts
@@ -35,6 +35,13 @@ export type GitHubCopilotStatus =
*/
export type GitHubCopilotRequestStatus = "InProgress" | "Warning" | "Normal";
+export interface GitHubCopilotStatusNotificationParams {
+ busy: boolean;
+ kind: "Warning" | "Normal" | "Error" | null;
+ message: string | null;
+ status?: "InProgress" | "Warning" | "Normal";
+}
+
export interface GitHubCopilotStatusResult {
status: GitHubCopilotStatus;
user: string;