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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: add Copilot icon to the footer and show loading state (#2240)
Myles Scolnickb9fbff58169f
diff --git a/frontend/src/components/debug/indicator.tsx b/frontend/src/components/debug/indicator.tsxindex 402c54d77f..05f816a399 100644--- a/frontend/src/components/debug/indicator.tsx+++ b/frontend/src/components/debug/indicator.tsx@@ -5,7 +5,7 @@ export const TailwindIndicator = () => {}return (- <div className="fixed bottom-2 right-[60px] z-50 flex size-5 items-center justify-center rounded-lg bg-gray-800 py-3 px-4 font-mono text-xs text-white">+ <div className="fixed bottom-2 right-[300px] z-50 flex size-5 items-center justify-center rounded-lg bg-gray-800 py-3 px-4 font-mono text-xs text-white"><div className="block sm:hidden">xs</div><div className="hidden sm:block md:hidden lg:hidden xl:hidden 2xl:hidden">smdiff --git a/frontend/src/components/editor/chrome/wrapper/copilot-status.tsx b/frontend/src/components/editor/chrome/wrapper/copilot-status.tsxnew file mode 100644index 0000000000..6acca383b4--- /dev/null+++ b/frontend/src/components/editor/chrome/wrapper/copilot-status.tsx@@ -0,0 +1,49 @@+/* Copyright 2024 Marimo. All rights reserved. */+import React from "react";+import { Tooltip } from "@/components/ui/tooltip";+import { cn } from "@/utils/cn";+import { Spinner } from "@/components/icons/spinner";+import {+ isGitHubCopilotSignedInState,+ githubCopilotLoadingVersion,+} from "@/core/codemirror/copilot/state";+import { atom, useAtomValue } from "jotai";+import { userConfigAtom } from "@/core/config/config";+import { GitHubCopilotIcon } from "@/components/icons/github-copilot";++const copilotAtom = atom((get) => {+ return get(userConfigAtom).completion.copilot;+});++export const CopilotStatusIcon: React.FC = () => {+ const copilot = useAtomValue(copilotAtom);++ // We only show an icon for GitHub Copilot, but not for other copilot providers,+ // this can be extended in the future+ if (copilot === "github") {+ return <GitHubCopilotStatus />;+ }++ return null;+};++const GitHubCopilotStatus: React.FC = () => {+ const isGitHubCopilotSignedIn = useAtomValue(isGitHubCopilotSignedInState);+ const isLoading = useAtomValue(githubCopilotLoadingVersion) !== null;++ const label = isGitHubCopilotSignedIn ? "Ready" : "Not connected";++ return (+ <Tooltip content={`GitHub Copilot: ${label}`}>+ <span>+ {isLoading ? (+ <Spinner className="h-4 w-4" />+ ) : (+ <GitHubCopilotIcon+ className={cn("h-4 w-4", !isGitHubCopilotSignedIn && "opacity-60")}+ />+ )}+ </span>+ </Tooltip>+ );+};diff --git a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsxindex 80d307255e..0c4b774a41 100644--- a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx+++ b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx@@ -18,6 +18,7 @@ import {} from "lucide-react";import type React from "react";import { useState } from "react";+import { CopilotStatusIcon } from "./copilot-status";export const MachineStats: React.FC = (props) => {const [nonce, setNonce] = useState(0);@@ -48,6 +49,7 @@ export const MachineStats: React.FC = (props) => {/>)}{data && <CPUBar cpu={data.cpu} />}+ <CopilotStatusIcon /><BackendConnection connection={connection.state} /></div>);diff --git a/frontend/src/components/icons/github-copilot.tsx b/frontend/src/components/icons/github-copilot.tsxnew file mode 100644index 0000000000..849049c7a5--- /dev/null+++ b/frontend/src/components/icons/github-copilot.tsx@@ -0,0 +1,17 @@+/* Copyright 2024 Marimo. All rights reserved. */+import type { SVGProps } from "react";++export const GitHubCopilotIcon = (props: SVGProps<SVGSVGElement>) => {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ width="1.24em"+ height="1em"+ viewBox="0 0 256 208"+ fill="currentColor"+ {...props}+ >+ <path d="M205.28 31.36c14.096 14.88 20.016 35.2 22.512 63.68c6.626 0 12.805 1.47 16.976 7.152l7.792 10.56A17.55 17.55 0 0 1 256 123.2v28.688c-.008 3.704-1.843 7.315-4.832 9.504C215.885 187.222 172.35 208 128 208c-49.066 0-98.19-28.273-123.168-46.608c-2.989-2.189-4.825-5.8-4.832-9.504V123.2c0-3.776 1.2-7.424 3.424-10.464l7.792-10.544c4.173-5.657 10.38-7.152 16.992-7.152c2.496-28.48 8.4-48.8 22.512-63.68C77.331 3.165 112.567.06 127.552 0H128c14.72 0 50.4 2.88 77.28 31.36m-77.264 47.376c-3.04 0-6.544.176-10.272.544c-1.312 4.896-3.248 9.312-6.08 12.128c-11.2 11.2-24.704 12.928-31.936 12.928c-6.802 0-13.927-1.42-19.744-5.088c-5.502 1.808-10.786 4.415-11.136 10.912c-.586 12.28-.637 24.55-.688 36.824c-.026 6.16-.05 12.322-.144 18.488c.024 3.579 2.182 6.903 5.44 8.384C79.936 185.92 104.976 192 128.016 192c23.008 0 48.048-6.08 74.512-18.144c3.258-1.48 5.415-4.805 5.44-8.384c.317-18.418.062-36.912-.816-55.312h.016c-.342-6.534-5.648-9.098-11.168-10.912c-5.82 3.652-12.927 5.088-19.728 5.088c-7.232 0-20.72-1.728-31.936-12.928c-2.832-2.816-4.768-7.232-6.08-12.128a106 106 0 0 0-10.24-.544m-26.941 43.93c5.748 0 10.408 4.66 10.408 10.409v19.183c0 5.749-4.66 10.409-10.408 10.409s-10.408-4.66-10.408-10.409v-19.183c0-5.748 4.66-10.408 10.408-10.408m53.333 0c5.749 0 10.409 4.66 10.409 10.409v19.183c0 5.749-4.66 10.409-10.409 10.409c-5.748 0-10.408-4.66-10.408-10.409v-19.183c0-5.748 4.66-10.408 10.408-10.408M81.44 28.32c-11.2 1.12-20.64 4.8-25.44 9.92c-10.4 11.36-8.16 40.16-2.24 46.24c4.32 4.32 12.48 7.2 21.28 7.2c6.72 0 19.52-1.44 30.08-12.16c4.64-4.48 7.52-15.68 7.2-27.04c-.32-9.12-2.88-16.64-6.72-19.84c-4.16-3.68-13.6-5.28-24.16-4.32m68.96 4.32c-3.84 3.2-6.4 10.72-6.72 19.84c-.32 11.36 2.56 22.56 7.2 27.04c10.56 10.72 23.36 12.16 30.08 12.16c8.8 0 16.96-2.88 21.28-7.2c5.92-6.08 8.16-34.88-2.24-46.24c-4.8-5.12-14.24-8.8-25.44-9.92c-10.56-.96-20 .64-24.16 4.32M128 56c-2.56 0-5.6.16-8.96.48c.32 1.76.48 3.68.64 5.76c0 1.44 0 2.88-.16 4.48c3.2-.32 5.92-.32 8.48-.32s5.28 0 8.48.32c-.16-1.6-.16-3.04-.16-4.48c.16-2.08.32-4 .64-5.76c-3.36-.32-6.4-.48-8.96-.48" />+ </svg>+ );+};diff --git a/frontend/src/core/codemirror/copilot/copilot-config.tsx b/frontend/src/core/codemirror/copilot/copilot-config.tsxindex 2d1d00e1f2..c26b460547 100644--- a/frontend/src/core/codemirror/copilot/copilot-config.tsx+++ b/frontend/src/core/codemirror/copilot/copilot-config.tsx@@ -1,6 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */import { useAtom } from "jotai";-import { copilotSignedInState } from "./state";+import { isGitHubCopilotSignedInState } from "./state";import { memo, useEffect, useState } from "react";import { getCopilotClient } from "./client";import { Button, buttonVariants } from "@/components/ui/button";@@ -17,7 +17,9 @@ type Step =| "notConnected";export const CopilotConfig = memo(() => {- const [copilotSignedIn, copilotChangeSignIn] = useAtom(copilotSignedInState);+ const [copilotSignedIn, copilotChangeSignIn] = useAtom(+ isGitHubCopilotSignedInState,+ );const [step, setStep] = useState<Step>();const [localData, setLocalData] = useState<{ url: string; code: string }>();diff --git a/frontend/src/core/codemirror/copilot/extension.ts b/frontend/src/core/codemirror/copilot/extension.tsindex c6443ed3ea..b59a889550 100644--- a/frontend/src/core/codemirror/copilot/extension.ts+++ b/frontend/src/core/codemirror/copilot/extension.ts@@ -51,7 +51,7 @@ export const copilotBundle = (config: CompletionConfig): Extension => {if (config.copilot === "github") {extensions.push(inlineSuggestion({- delay: 200, // default is 500ms+ delay: 500, // default is 500msfetchFn: async (state) => {if (!isCopilotEnabled()) {return "";diff --git a/frontend/src/core/codemirror/copilot/language-server.ts b/frontend/src/core/codemirror/copilot/language-server.tsindex b61f759eca..dd85728868 100644--- a/frontend/src/core/codemirror/copilot/language-server.ts+++ b/frontend/src/core/codemirror/copilot/language-server.ts@@ -24,9 +24,14 @@ import type {CopilotAcceptCompletionParams,CopilotRejectCompletionParams,} from "./types";-import { isCopilotEnabled } from "./state";+import {+ isCopilotEnabled,+ setGitHubCopilotLoadingVersion,+ clearGitHubCopilotLoadingVersion,+} from "./state";import { getCodes } from "./getCodes";import { Logger } from "@/utils/Logger";+import { debounce } from "lodash-es";// A map of request methods and their parameters and return typesexport interface LSPRequestMap {@@ -128,19 +133,20 @@ export class CopilotLanguageServerClient extends LanguageServerClient {return this._request("notifyRejected", params);}- async getCompletion(params: CopilotGetCompletionsParams) {- if (this.isDisabled()) {- return { completions: [] };- }-- const version = this.documentVersion;-+ private async getCompletionInternal(+ params: CopilotGetCompletionsParams,+ version: number,+ ): Promise<CopilotGetCompletionsResult> {+ // Start a loading indicator+ setGitHubCopilotLoadingVersion(version);const response = await this._request("getCompletions", {doc: {...params.doc,version: version,},});+ // Stop the loading indicator (only if the version hasn't changed)+ clearGitHubCopilotLoadingVersion(version);// If the document version has changed since the request was made, return an empty responseif (version !== this.documentVersion) {@@ -149,4 +155,30 @@ export class CopilotLanguageServerClient extends LanguageServerClient {return response;}++ // Even though the copilot extension has a debounce,+ // there are multiple requests sent at the same time+ // when multiple Codemirror instances are mounted at the same time.+ private debouncedGetCompletionInternal = debounce(+ this.getCompletionInternal.bind(this),+ 300,+ );++ async getCompletion(+ params: CopilotGetCompletionsParams,+ ): Promise<CopilotGetCompletionsResult> {+ if (this.isDisabled()) {+ return { completions: [] };+ }++ const version = this.documentVersion;++ // If version is 0, it means the document hasn't been opened yet+ if (version === 0) {+ return { completions: [] };+ }++ const response = await this.debouncedGetCompletionInternal(params, version);+ return response || { completions: [] };+ }}diff --git a/frontend/src/core/codemirror/copilot/state.ts b/frontend/src/core/codemirror/copilot/state.tsindex 1bcfda8675..32a5ea4549 100644--- a/frontend/src/core/codemirror/copilot/state.ts+++ b/frontend/src/core/codemirror/copilot/state.ts@@ -1,11 +1,12 @@/* Copyright 2024 Marimo. All rights reserved. */import { getUserConfig, userConfigAtom } from "@/core/config/config";-import { waitFor } from "@/core/state/jotai";+import { store, waitFor } from "@/core/state/jotai";+import { atom } from "jotai";import { atomWithStorage } from "jotai/utils";const KEY = "marimo:copilot:signedIn";-export const copilotSignedInState = atomWithStorage<boolean | null>(+export const isGitHubCopilotSignedInState = atomWithStorage<boolean | null>(KEY,null,undefined,@@ -14,6 +15,24 @@ export const copilotSignedInState = atomWithStorage<boolean | null>(},);+export const githubCopilotLoadingVersion = atom<number | null>(null);++/**+ * Set the currently loading document version+ */+export function setGitHubCopilotLoadingVersion(version: number) {+ store.set(githubCopilotLoadingVersion, version);+}+/**+ * Clear the currently loading document version, if it matches the current version+ */+export function clearGitHubCopilotLoadingVersion(expectedVersion: number) {+ const currentVersion = store.get(githubCopilotLoadingVersion);+ if (currentVersion === expectedVersion) {+ store.set(githubCopilotLoadingVersion, null);+ }+}+function getIsLastSignedIn() {const lastSignedIn = localStorage.getItem(KEY);return lastSignedIn === "true";