From ef53d113bef870a698584a1057e489c49d620754 Mon Sep 17 00:00:00 2001 From: adarshramiyer Date: Mon, 23 Jun 2025 15:32:32 -0700 Subject: [PATCH] dev data schemas, edit logging refinement --- core/core.ts | 5 +++ core/index.d.ts | 1 + core/nextEdit/context/aggregateEdits.ts | 28 +++++++++----- .../context/autocompleteContextFetching.ts | 38 ++++++++++++------- core/nextEdit/context/processNextEditData.ts | 24 +++++++++--- .../vscode/src/extension/VsCodeExtension.ts | 5 ++- .../config-yaml/src/schemas/data/index.ts | 8 ++++ .../src/schemas/data/nextEdit/index.ts | 12 ++++++ .../src/schemas/data/nextEdit/v0.2.0.ts | 28 ++++++++++++++ 9 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 packages/config-yaml/src/schemas/data/nextEdit/index.ts create mode 100644 packages/config-yaml/src/schemas/data/nextEdit/v0.2.0.ts diff --git a/core/core.ts b/core/core.ts index 0349d4885..c387f2c92 100644 --- a/core/core.ts +++ b/core/core.ts @@ -726,17 +726,22 @@ export class Core { currentData.getDefsFromLspFunction, currentData.recentlyEditedRanges, currentData.recentlyVisitedRanges, + currentData.workspaceDir, ); }, ); } + const workspaceDir = + data.actions.length > 0 ? data.actions[0].workspaceDir : undefined; + // Store the latest context data on the aggregator (global._editAggregator as any).latestContextData = { configHandler: data.configHandler, getDefsFromLspFunction: data.getDefsFromLspFunction, recentlyEditedRanges: data.recentlyEditedRanges, recentlyVisitedRanges: data.recentlyVisitedRanges, + workspaceDir: workspaceDir, }; // queueMicrotask prevents blocking the UI thread during typing diff --git a/core/index.d.ts b/core/index.d.ts index 6cce48fb3..ca942f654 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1307,6 +1307,7 @@ export interface RangeInFileWithNextEditInfo { editText: string; afterCursorPos: Position; beforeCursorPos: Position; + workspaceDir: string; } export type SetCodeToEditPayload = RangeInFileWithContents | FileWithContents; diff --git a/core/nextEdit/context/aggregateEdits.ts b/core/nextEdit/context/aggregateEdits.ts index e63df43b8..2a1898fe1 100644 --- a/core/nextEdit/context/aggregateEdits.ts +++ b/core/nextEdit/context/aggregateEdits.ts @@ -147,7 +147,7 @@ export class EditAggregator { const clustersToFinalize = this.identifyClustersToFinalize( fileState, - editLine, + edit, timestamp, false, ); @@ -360,29 +360,39 @@ export class EditAggregator { private identifyClustersToFinalize( fileState: FileState, - editLine: number, + edit: RangeInFileWithNextEditInfo, timestamp: number, isStructuralEdit: boolean, ): ClusterState[] { const clustersToFinalize: ClusterState[] = []; + const editLine = edit.range.start.line; fileState.activeClusters.forEach((cluster) => { const timeSinceLastEdit = (timestamp - cluster.lastTimestamp) / 1000; - // Only consider the line changed if it's a DIFFERENT line - const isOnDifferentLine = cluster.lastLine !== editLine; + const isOnDifferentLineByNumber = cluster.lastLine !== editLine; - // Finalize if we moved to a different line AND the time gap exceeds deltaT + const isOnDifferentLineByNewline = edit.editText.includes("\n"); + + // Use different time thresholds for different types of line change detection + const shouldFinalizeByLineNumber = + isOnDifferentLineByNumber && timeSinceLastEdit > this.config.deltaT; + + const shouldFinalizeByNewline = + isOnDifferentLineByNewline && + timeSinceLastEdit > this.config.deltaT * 1.5; + + // Finalize if we moved to a different line AND the time gap exceeds the respective threshold const shouldFinalizeByTime = - isOnDifferentLine && timeSinceLastEdit > this.config.deltaT; - + shouldFinalizeByLineNumber || shouldFinalizeByNewline; const shouldFinalizeByCount = cluster.edits.length >= this.config.maxEdits; - const shouldFinalizeByDuration = (timestamp - cluster.firstTimestamp) / 1000 > this.config.maxDuration; - // Structural edits should only finalize other clusters, not their own + // For structural edits, use the combined line detection + const isOnDifferentLine = + isOnDifferentLineByNumber || isOnDifferentLineByNewline; const shouldFinalizeByStructuralEdit = isStructuralEdit && isOnDifferentLine; diff --git a/core/nextEdit/context/autocompleteContextFetching.ts b/core/nextEdit/context/autocompleteContextFetching.ts index 47f6148d7..8b57a4a17 100644 --- a/core/nextEdit/context/autocompleteContextFetching.ts +++ b/core/nextEdit/context/autocompleteContextFetching.ts @@ -56,7 +56,6 @@ export const getAutocompleteContext = async ( manuallyPassFileContents, }; - // Load config to get autocomplete options const { config } = await configHandler.loadConfig(); if (!config) { throw new Error("No config available"); @@ -64,20 +63,31 @@ export const getAutocompleteContext = async ( // Use provided autocomplete model or fall back to configured autocomplete model let finalModel: ILLM; + let modelNameForTemplating: string; + if (autocompleteModel) { if (typeof autocompleteModel === "string") { - // If model name is provided as string, find it in the config + // Try to find the model in config first const foundModel = config.modelsByRole.autocomplete.find( (m) => m.title === autocompleteModel, ); - if (!foundModel) { - throw new Error( - `Autocomplete model with title "${autocompleteModel}" not found`, - ); + if (foundModel) { + finalModel = foundModel; + modelNameForTemplating = foundModel.model; + } else { + // Model not found in config, but we can still use it for template selection + const configuredModel = config.selectedModelByRole.autocomplete; + if (!configuredModel) { + throw new Error( + "No autocomplete model configured and provided model not found in config", + ); + } + finalModel = configuredModel; + modelNameForTemplating = autocompleteModel; // Use the provided string for template selection } - finalModel = foundModel; } else { finalModel = autocompleteModel; + modelNameForTemplating = autocompleteModel.model; } } else { const configuredModel = config.selectedModelByRole.autocomplete; @@ -85,27 +95,29 @@ export const getAutocompleteContext = async ( throw new Error("No autocomplete model configured and no model provided"); } finalModel = configuredModel; + modelNameForTemplating = configuredModel.model; } - // Build options exactly like CompletionProvider does const options = { ...DEFAULT_AUTOCOMPLETE_OPTS, ...config.tabAutocompleteOptions, ...finalModel.autocompleteOptions, - // Allow maxPromptTokens override if provided ...(maxPromptTokens && { maxPromptTokens }), }; - // Handle template from model like CompletionProvider does if (finalModel.promptTemplates?.autocomplete) { options.template = finalModel.promptTemplates.autocomplete as string; } - const helper = await HelperVars.create(input, options, finalModel.model, ide); + const helper = await HelperVars.create( + input, + options, + modelNameForTemplating, + ide, + ); + const contextRetrievalService = new ContextRetrievalService(ide); - // CRITICAL: Initialize the import definitions cache for this file - // This is normally done when the active text editor changes, but we need to do it manually await contextRetrievalService.initializeForFile(filepath); const [snippetPayload, workspaceDirs] = await Promise.all([ diff --git a/core/nextEdit/context/processNextEditData.ts b/core/nextEdit/context/processNextEditData.ts index 3039304e7..cc0f9a5f5 100644 --- a/core/nextEdit/context/processNextEditData.ts +++ b/core/nextEdit/context/processNextEditData.ts @@ -2,9 +2,9 @@ import { IDE, Position } from "../.."; import { AutocompleteCodeSnippet } from "../../autocomplete/snippets/types"; import { GetLspDefinitionsFunction } from "../../autocomplete/types"; import { ConfigHandler } from "../../config/ConfigHandler"; +import { DataLogger } from "../../data/log"; import { RecentlyEditedRange } from "../types"; import { getAutocompleteContext } from "./autocompleteContextFetching"; -import { createDiff, DiffFormatType } from "./diffFormatting"; const randomNumberBetween = (min: number, max: number) => { min = Math.ceil(min); // Ensure min is an integer @@ -23,6 +23,7 @@ export const processNextEditData = async ( getDefinitionsFromLsp: GetLspDefinitionsFunction, recentlyEditedRanges: RecentlyEditedRange[], recentlyVisitedRanges: AutocompleteCodeSnippet[], + workspaceDir: string, modelNameOrInstance?: string | undefined, // eslint-disable-next-line max-params ) => { @@ -34,7 +35,7 @@ export const processNextEditData = async ( const modelName = "Codestral"; - const maxPromptTokens = randomNumberBetween(500, 14000); + const maxPromptTokens = randomNumberBetween(500, 12000); const autocompleteContext = await getAutocompleteContext( filePath, @@ -49,7 +50,20 @@ export const processNextEditData = async ( modelName, ); - console.log( - createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), - ); + // console.log( + // createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), + // ); + + void DataLogger.getInstance().logDevData({ + name: "nextEdit", + data: { + fileURI: filePath, + workspaceDirURI: workspaceDir, + beforeContent, + afterContent, + beforeCursorPos: cursorPosBeforeEdit, + afterCursorPos: cursorPosAfterPrevEdit, + context: autocompleteContext, + }, + }); }; diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 7fdeb7121..e1c178813 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -301,7 +301,9 @@ export class VsCodeExtension { const workspaceFolder = vscode.workspace.getWorkspaceFolder( event.document.uri, ); - if (!workspaceFolder) return; + if (!workspaceFolder) { + return; + } const workspaceDirUri = workspaceFolder.uri.toString(); const repoName = await this.ide.getRepoName(workspaceDirUri); @@ -325,6 +327,7 @@ export class VsCodeExtension { : change.range.start) as Position, afterCursorPos: activeCursorPos as Position, + workspaceDir: workspaceDirUri, }), ); diff --git a/packages/config-yaml/src/schemas/data/index.ts b/packages/config-yaml/src/schemas/data/index.ts index 7251e12a7..b5421caf1 100644 --- a/packages/config-yaml/src/schemas/data/index.ts +++ b/packages/config-yaml/src/schemas/data/index.ts @@ -28,6 +28,11 @@ import { editInteractionEventSchema_0_2_0, editInteractionEventSchema_0_2_0_noCode, } from "./editInteraction/v0.2.0.js"; +import { nextEditEventAllSchema } from "./nextEdit/index.js"; +import { + nextEditEventSchema_0_2_0, + nextEditEventSchema_0_2_0_noCode, +} from "./nextEdit/v0.2.0.js"; import { quickEditEventAllSchema } from "./quickEdit/index.js"; import { quickEditEventSchema_0_1_0, @@ -72,6 +77,7 @@ const devEventAllVersionDataSchemas = z.object({ tokensGenerated: tokensGeneratedEventAllSchema, chatInteraction: chatInteractionEventAllSchema, editInteraction: editInteractionEventAllSchema, + nextEdit: nextEditEventAllSchema, }); // Version and level specific schemas are organized here @@ -97,6 +103,7 @@ export const devDataVersionedSchemas = { tokensGenerated: tokensGeneratedEventSchema_0_2_0, chatInteraction: chatInteractionEventSchema_0_2_0, editInteraction: editInteractionEventSchema_0_2_0, + nextEdit: nextEditEventSchema_0_2_0, }, noCode: { autocomplete: autocompleteEventSchema_0_2_0_noCode, @@ -104,6 +111,7 @@ export const devDataVersionedSchemas = { tokensGenerated: tokensGeneratedEventSchema_0_2_0_noCode, chatInteraction: chatInteractionEventSchema_0_2_0_noCode, editInteraction: editInteractionEventSchema_0_2_0_noCode, + nextEdit: nextEditEventSchema_0_2_0_noCode, }, }, }; diff --git a/packages/config-yaml/src/schemas/data/nextEdit/index.ts b/packages/config-yaml/src/schemas/data/nextEdit/index.ts new file mode 100644 index 000000000..de2607e5c --- /dev/null +++ b/packages/config-yaml/src/schemas/data/nextEdit/index.ts @@ -0,0 +1,12 @@ +import { z } from "zod"; +import { baseDevDataAllSchema } from "../base.js"; + +export const nextEditEventAllSchema = baseDevDataAllSchema.extend({ + fileURI: z.string(), + workspaceDirURI: z.string(), + beforeContent: z.string(), + afterContent: z.string(), + beforeCursorPos: z.object({ line: z.number(), character: z.number() }), + afterCursorPos: z.object({ line: z.number(), character: z.number() }), + context: z.string(), +}); diff --git a/packages/config-yaml/src/schemas/data/nextEdit/v0.2.0.ts b/packages/config-yaml/src/schemas/data/nextEdit/v0.2.0.ts new file mode 100644 index 000000000..3c95d4e3c --- /dev/null +++ b/packages/config-yaml/src/schemas/data/nextEdit/v0.2.0.ts @@ -0,0 +1,28 @@ +import { nextEditEventAllSchema } from "./index.js"; + +export const nextEditEventSchema_0_2_0 = nextEditEventAllSchema.pick({ + // base + timestamp: true, + userId: true, + userAgent: true, + selectedProfileId: true, + eventName: true, + schema: true, + + // nextedit-specific + fileURI: true, + workspaceDirURI: true, + beforeContent: true, + afterContent: true, + beforeCursorPos: true, + afterCursorPos: true, + context: true, +}); + +export const nextEditEventSchema_0_2_0_noCode = nextEditEventSchema_0_2_0.omit({ + fileURI: true, + workspaceDirURI: true, + beforeContent: true, + afterContent: true, + context: true, +});