getappmap / appmap-js

Client libraries for AppMap
48 stars 17 forks source link

@generate mode doesn't know the name of the pinned context file #2010

Open kgilpin opened 1 month ago

kgilpin commented 1 month ago

When using the @generate command, Navie doesn't know the name of the pinned file.

You can see how, in this screenshot, I have pinned a context file called "official.py".

Then I've asked a typical code generation question. Navie outputs code snippets referring to `/path/to/your/script.py' rather than the pinned file.

Screen Shot 2024-09-21 at 10 20 39 AM

It will also propose (correct) changes to the wrong file name:

Screen Shot 2024-09-21 at 2 10 46 PM

--

github-actions[bot] commented 1 month ago

Title

Fix @generate mode to recognize and use pinned context file names

Problem

When using the @generate command in Navie, the generated code includes generic placeholders like /path/to/your/script.py instead of recognizing and using the actual pinned file names. This leads to incorrect file references in the output, which can be erroneous and confusing.

Analysis

The root cause of this issue is that the @generate mode is currently not configured to handle the file names from the pinned context. When the context is passed to the navie package, it lacks proper handling to identify and use the pinned file names within the @generate mode.

The main changes needed include:

  1. Detecting the pinned file names in the context being passed.
  2. Updating the logic in @generate mode to use these pinned file names for code generation.

To solve this issue, the context should incorporate file names and content, and the @generate mode should be adapted to observe and use this information.

Proposed Changes

  1. Update PinFileRequest class:

    • Ensure it includes properties for name, uri, and content.
    export class PinFileRequest {
      name: string; 
      uri?: string;
      content?: string;
    
      constructor(arg: PinFileRequest) {
        this.name = arg.name;
        this.uri = arg.uri;
        this.content = arg.content;
      }
    }
  2. Update ContextItemMixin in File.vue to properly handle and pass pinned files in the context.

    methods: {
        onPin(evt: PinEvent) {
          // Ensure pin event passes the file name and content
          const pinFileRequest = new PinFileRequest({
            name: this.header,
            uri: this.location,
            content: this.content,
          });
          this.$root.$emit('pin', pinFileRequest);
        },
      }
  3. Ensure the AgentSelectionService class handles and passes the pinned file context correctly when selecting the @generate mode.

    selectAgent(
      question: string,
      classification: ContextV2.ContextLabel[],
      userOptions: UserOptions
    ): AgentModeResult {
      let modifiedQuestion = question;
    
      // Include contextual information with pinned files
      const pinnedFileContext = classification.find(label => label.name === ContextV2.ContextLabelName.Generate);
      const contextService = new ContextService(
        this.history,
        this.vectorTermsService,
        this.lookupContextService,
        this.applyContextService
      );
    
      const generateAgent = () => new GenerateAgent(this.history, contextService, this.fileChangeExtractorService);
    
      const buildAgent: { [key in AgentMode]: () => Agent } = {
        ...
      };
  4. Update AgentMode.Generate logic to make use of the pinned file name and content during code generation.

    class GenerateAgent {
      ...
      handleGenerate(question: string, context: ContextV2.ContextLabel[], userContext: UserOptions) {
        const pinnedFile = context.find(ctx => ctx.name === ContextV2.ContextLabelName.Generate);
        if (pinnedFile) {
          question = question.replace('/path/to/your/script.py', pinnedFile.name);
        }
        ...
      }
    }

By following these changes, the @generate mode should properly observe and use provided file names in the pinned context, resulting in accurate file references during code generation.

dustinbyrne commented 1 month ago

In the first image, no code snippets are found. The instructions referencing a file are included as part of the code snippet prompt description, which will not be included in the absence of code snippets: https://github.com/getappmap/appmap-js/blob/main/packages/navie/src/services/apply-context-service.ts#L103-L110