Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] feat: edit inline with AI #2453

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions frontend/src/components/shortcuts/renderShortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ function prettyPrintHotkey(key: string): [label: string, symbol?: string] {
return [lowerKey];
}

export function getSymbol(key: string) {
const platform = isPlatformMac() ? "mac" : "default";
const keyData = KEY_MAPPINGS[key.toLowerCase()];
if (keyData) {
return keyData.symbols[platform] || keyData.symbols.default;
}
return key;
}

interface KeyData {
symbols: {
mac?: string;
Expand Down Expand Up @@ -190,6 +199,10 @@ const KEY_MAPPINGS: Record<string, KeyData> = {
symbols: { mac: "↘", default: "End" },
label: "End",
},
mod: {
symbols: { mac: "⌘", windows: "⊞ Win", default: "Ctrl" },
label: "Control",
},
};

function capitalize(str: string) {
Expand Down
19 changes: 18 additions & 1 deletion frontend/src/core/codemirror/cm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ import { historyCompartment } from "./editing/extensions";
import { goToDefinitionBundle } from "./go-to-definition/extension";
import type { HotkeyProvider } from "../hotkeys/hotkeys";
import { lightTheme } from "./theme/light";

import { promptPlugin } from "./prompt/prompt";
import { requestEditCompletion } from "./prompt/request";
import { getCurrentLanguageAdapter } from "./language/commands";
export interface CodeMirrorSetupOpts {
cellId: CellId;
showPlaceholder: boolean;
Expand All @@ -79,6 +81,7 @@ export const setupCodeMirror = (opts: CodeMirrorSetupOpts): Extension[] => {
cellCodeCallbacks,
keymapConfig,
hotkeys,
enableAI,
} = opts;

return [
Expand All @@ -91,6 +94,20 @@ export const setupCodeMirror = (opts: CodeMirrorSetupOpts): Extension[] => {
basicBundle(opts),
// Underline cmd+clickable placeholder
goToDefinitionBundle(),
// AI prompt edit
enableAI
? promptPlugin({
complete: (req) => {
return requestEditCompletion({
prompt: req.prompt,
selection: req.selection,
codeBefore: req.codeBefore,
code: req.editorView.state.doc.toString(),
language: getCurrentLanguageAdapter(req.editorView),
});
},
})
: [],
];
};

Expand Down
51 changes: 51 additions & 0 deletions frontend/src/core/codemirror/prompt/complete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright 2024 Marimo. All rights reserved. */

import { API } from "@/core/network/api";
import { asURL } from "@/utils/url";
import type { LanguageAdapterType } from "../language/types";
import { getCodes } from "../copilot/getCodes";

/**
* Request to edit code with AI
*/
export async function requestEditCompletion(opts: {
prompt: string;
selection: string;
code: string;
codeBefore: string;
language: LanguageAdapterType;
}): Promise<string> {
const currentCode = opts.code;

const otherCodes = getCodes(currentCode);
// Other code to include is the codeBefore and the other codes
const includeOtherCode = `${opts.codeBefore}\n${otherCodes}`;

const response = await fetch(asURL("api/ai/completion").toString(), {
method: "POST",
headers: API.headers(),
body: JSON.stringify({
prompt: opts.prompt,
code: opts.selection,
includeOtherCode: includeOtherCode,
language: opts.language,
}),
});

const reader = response.body?.getReader();
if (!reader) {
throw new Error("Failed to get response reader");
}

let result = "";
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result += new TextDecoder().decode(value);
}

return result;
}
Loading
Loading