diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 898d756ac..6bfc6cfe1 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -1,5 +1,4 @@ import { streamLines } from "../../../diff/util"; -import { DEFAULT_AUTOCOMPLETE_OPTS } from "../../../util/parameters"; import { PosthogFeatureFlag, Telemetry } from "../../../util/posthog"; import { HelperVars } from "../../util/HelperVars"; @@ -74,12 +73,10 @@ export class StreamTransformPipeline { fullStop, ); - const timeoutValue = - helper.options.showWhateverWeHaveAtXMs ?? - (await Telemetry.getValueForFeatureFlag( - PosthogFeatureFlag.AutocompleteTimeout, - )) ?? - DEFAULT_AUTOCOMPLETE_OPTS.showWhateverWeHaveAtXMs; + const timeoutValue = await Telemetry.getValueForFeatureFlag( + PosthogFeatureFlag.AutocompleteTimeout, + ); + // helper.options.showWhateverWeHaveAtXMs ?? DEFAULT_AUTOCOMPLETE_OPTS.showWhateverWeHaveAtXMs lineGenerator = showWhateverWeHaveAtXMs(lineGenerator, timeoutValue!); diff --git a/core/util/posthog.ts b/core/util/posthog.ts index 198041207..25b3ab600 100644 --- a/core/util/posthog.ts +++ b/core/util/posthog.ts @@ -106,12 +106,21 @@ export class Telemetry { } } + private static featureValueCache: Record = {}; + static async getFeatureFlag(flag: PosthogFeatureFlag) { - return Telemetry.client?.getFeatureFlag(flag, Telemetry.uniqueId); + const value = Telemetry.client?.getFeatureFlag(flag, Telemetry.uniqueId); + + Telemetry.featureValueCache[flag] = value; + return value; } static async getValueForFeatureFlag(flag: PosthogFeatureFlag) { try { + if (Telemetry.featureValueCache[flag]) { + return Telemetry.featureValueCache[flag]; + } + const userGroup = await Telemetry.getFeatureFlag(flag); if (typeof userGroup === "string") { return EXPERIMENTS[flag][userGroup].value; diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 382be954b..110229e03 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -176,6 +176,7 @@ "@types/jest": "^29.5.12", "@types/jquery": "^3.5.29", "@types/jsdom": "^21.1.6", + "@types/json-schema": "^7.0.15", "@types/mozilla-readability": "^0.2.1", "@types/mustache": "^4.2.5", "@types/node-fetch": "^2.6.11", diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 448e6baf2..d81c5ae76 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -268,6 +268,10 @@ async function processDiff( numDiffs: 0, }); } + + if (action === "accept") { + await sidebar.webviewProtocol.request("exitEditMode", undefined); + } } function waitForSidebarReady( @@ -742,8 +746,8 @@ const getCommandsMap: ( return; } - // Clear the sidebar to prevent overwriting changes made in fullscreen - vscode.commands.executeCommand("continue.newSession") + // Clear the sidebar to prevent overwriting changes made in fullscreen + vscode.commands.executeCommand("continue.newSession"); // Full screen not open - open it captureCommandTelemetry("openFullScreen"); @@ -777,7 +781,7 @@ const getCommandsMap: ( sessionId, ); } - sessionLoader.dispose() + sessionLoader.dispose(); }); // When panel closes, reset the webview and focus diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 4d8a99020..ced48e03d 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -10,8 +10,8 @@ import { redDecorationType, } from "./decorations"; -import type { VerticalDiffCodeLens } from "./manager"; import type { ApplyState, DiffLine } from "core"; +import type { VerticalDiffCodeLens } from "./manager"; export interface VerticalDiffHandlerOptions { input?: string; @@ -486,9 +486,12 @@ export class VerticalDiffHandler implements vscode.Disposable { * we have received all of the diff lines. */ async reapplyWithMeyersDiff(diffLines: DiffLine[]) { + // Diff is messed up without this delay. + await new Promise((resolve) => setTimeout(resolve, 100)); + // First, we reset the original diff by rejecting all pending diff blocks - for (const block of this.editorToVerticalDiffCodeLens.get(this.fileUri) ?? - []) { + const blocks = this.editorToVerticalDiffCodeLens.get(this.fileUri) ?? []; + for (const block of blocks.reverse()) { await this.acceptRejectBlock( false, block.start, diff --git a/extensions/vscode/src/webviewProtocol.ts b/extensions/vscode/src/webviewProtocol.ts index fea6a3e8e..bfbb37ce6 100644 --- a/extensions/vscode/src/webviewProtocol.ts +++ b/extensions/vscode/src/webviewProtocol.ts @@ -78,7 +78,7 @@ export class VsCodeWebviewProtocol status: "success", }); } else { - respond({ done: true, content: response || {}, status: "success" }); + respond({ done: true, content: response ?? {}, status: "success" }); } } catch (e: any) { respond({ done: true, error: e.message, status: "error" }); diff --git a/gui/package-lock.json b/gui/package-lock.json index 4c735e3ef..ac7f6ce81 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -178,6 +178,7 @@ "@types/jest": "^29.5.12", "@types/jquery": "^3.5.29", "@types/jsdom": "^21.1.6", + "@types/json-schema": "^7.0.15", "@types/mozilla-readability": "^0.2.1", "@types/mustache": "^4.2.5", "@types/node-fetch": "^2.6.11", diff --git a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx index 6ccedc74a..45faf550a 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -1,11 +1,18 @@ import { ChevronDownIcon } from "@heroicons/react/24/outline"; +import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; import { debounce } from "lodash"; import { useContext, useEffect, useRef, useState } from "react"; import styled from "styled-components"; import { v4 as uuidv4 } from "uuid"; -import { defaultBorderRadius, lightGray, vscEditorBackground } from "../.."; +import { lightGray, vscEditorBackground } from "../.."; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; +import { useAppSelector } from "../../../redux/hooks"; +import { selectDefaultModel } from "../../../redux/slices/configSlice"; +import { + selectApplyStateByStreamId, + selectIsInEditMode, +} from "../../../redux/slices/sessionSlice"; import { getFontSize } from "../../../util"; import { childrenToText, isTerminalCodeBlock } from "../utils"; import ApplyActions from "./ApplyActions"; @@ -13,13 +20,6 @@ import CopyButton from "./CopyButton"; import FileInfo from "./FileInfo"; import GeneratingCodeLoader from "./GeneratingCodeLoader"; import RunInTerminalButton from "./RunInTerminalButton"; -import { useAppSelector } from "../../../redux/hooks"; -import { selectDefaultModel } from "../../../redux/slices/configSlice"; -import { - selectApplyStateByStreamId, - selectIsInEditMode, -} from "../../../redux/slices/sessionSlice"; -import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; const TopDiv = styled.div` outline: 0.5px solid rgba(153, 153, 152); @@ -94,11 +94,21 @@ export default function StepContainerPreToolbar( return; } - const fileUri = await inferResolvedUriFromRelativePath( + let fileUri = await inferResolvedUriFromRelativePath( props.relativeFilepath, ideMessenger.ide, ); + // Sometimes the model will decide to only output the base name + // in which case we shouldn't create a new file if it matches the current file + const exists = await ideMessenger.ide.fileExists(fileUri); + if (!exists) { + const activeFile = await ideMessenger.ide.getCurrentFile(); + if (activeFile && activeFile.path.endsWith(props.relativeFilepath)) { + fileUri = activeFile.path; + } + } + ideMessenger.post("applyToFile", { streamId: streamIdRef.current, filepath: fileUri, diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index c391b8a91..52d343fe4 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -6,30 +6,30 @@ import rehypeKatex from "rehype-katex"; import remarkMath from "remark-math"; import styled from "styled-components"; import { visit } from "unist-util-visit"; +import { v4 as uuidv4 } from "uuid"; import { defaultBorderRadius, vscBackground, vscEditorBackground, vscForeground, } from ".."; +import useUpdatingRef from "../../hooks/useUpdatingRef"; +import { useAppSelector } from "../../redux/hooks"; +import { selectUIConfig } from "../../redux/slices/configSlice"; +import { getContextItemsFromHistory } from "../../redux/thunks/updateFileSymbols"; import { getFontSize, isJetBrains } from "../../util"; +import { ToolTip } from "../gui/Tooltip"; import FilenameLink from "./FilenameLink"; import "./katex.css"; import "./markdown.css"; import StepContainerPreActionButtons from "./StepContainerPreActionButtons"; import StepContainerPreToolbar from "./StepContainerPreToolbar"; import SymbolLink from "./SymbolLink"; -import useUpdatingRef from "../../hooks/useUpdatingRef"; -import { remarkTables } from "./utils/remarkTables"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; -import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; -import { useAppSelector } from "../../redux/hooks"; +import { isSymbolNotRif, matchCodeToSymbolOrFile } from "./utils"; import { fixDoubleDollarNewLineLatex } from "./utils/fixDoubleDollarLatex"; -import { selectUIConfig } from "../../redux/slices/configSlice"; -import { ToolTip } from "../gui/Tooltip"; -import { v4 as uuidv4 } from "uuid"; -import { ContextItemWithId, RangeInFileWithContents } from "core"; -import { getContextItemsFromHistory } from "../../redux/thunks/updateFileSymbols"; +import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; +import { remarkTables } from "./utils/remarkTables"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -301,30 +301,19 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( if (content) { const { symbols, rifs } = pastFileInfoRef.current; - // Insert file links for matching previous context items - // With some reasonable limitations on what might be a filename - if (rifs.length && content.includes(".") && content.length > 2) { - const match = rifs.find( - (rif) => rif.filepath.split("/").pop() === content, // Exact match for last segment of URI - ); - - if (match) { - return ; - } - } - - // Insert symbols for exact matches - const exactSymbol = symbols.find((s) => s.name === content); - if (exactSymbol) { - return ; - } - - // Partial matches - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` - const partialSymbol = symbols.find((s) => - content.startsWith(s.name), + const matchedSymbolOrFile = matchCodeToSymbolOrFile( + content, + symbols, + rifs, ); - if (partialSymbol) { - return ; + if (matchedSymbolOrFile) { + if (isSymbolNotRif(matchedSymbolOrFile)) { + return ( + + ); + } else { + return ; + } } } return {codeProps.children}; diff --git a/gui/src/components/markdown/utils.ts b/gui/src/components/markdown/utils.ts index d34044089..3961f11d0 100644 --- a/gui/src/components/markdown/utils.ts +++ b/gui/src/components/markdown/utils.ts @@ -1,3 +1,5 @@ +import { RangeInFileWithContents, SymbolWithRange } from "core"; + const terminalLanguages = ["bash", "sh"]; const commonTerminalCommands = [ "npm", @@ -52,3 +54,39 @@ function childToText(child: any): string { export function childrenToText(children: any) { return children.map((child: any) => childToText(child)).join(""); } + +export function matchCodeToSymbolOrFile( + content: string, + symbols: SymbolWithRange[], + rifs: RangeInFileWithContents[], +): SymbolWithRange | RangeInFileWithContents | undefined { + // Insert file links for matching previous context items + // With some reasonable limitations on what might be a filename + if (rifs.length && content.includes(".") && content.length > 2) { + const match = rifs.find( + (rif) => rif.filepath.split("/").pop() === content, // Exact match for last segment of URI + ); + + if (match) { + return match; + } + } + + // Insert symbols for exact matches + const exactSymbol = symbols.find((s) => s.name === content); + if (exactSymbol) { + return exactSymbol; + } + + // Partial matches - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` + const partialSymbol = symbols.find((s) => content.startsWith(s.name)); + if (partialSymbol) { + return partialSymbol; + } +} + +export function isSymbolNotRif( + item: SymbolWithRange | RangeInFileWithContents, +): item is SymbolWithRange { + return (item as SymbolWithRange).type !== undefined; +}