Skip to content

registerBlockContextMenuItem registers multiple times  #40

Description

@stormsson

I tried finding some more documentation but wasn't able to ...
basically when I have my plugin i find 3 times the entry in the block context menu. and when the plugin is run, it is run three times instead of just one...

I believe this is a bug, but honestly I'm not sure it's not something I actually wrote wrong

Menu Item

image

## Plugin execution adds 3 times the block afterwards
image

Code here:

import '@logseq/libs';

  const settings = {
    name: 'logseq-claude-prompt',
    description: 'Send text to Claude with a hardcoded prompt',
    key: 'MY_KEY',
    prompt: 'You are a helpful assistant. Please analyze the following text and provide a summary:  do nod add anything else' 
  };
  

  const callClaudio = async (contents) => {
      console.log("Sending to Claude...")
      const content = contents.join("\n\n")
      const response = await fetch('https://api.anthropic.com/v1/messages', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': settings.key,
          'anthropic-version': '2023-06-01'
        },
        body: JSON.stringify({
          model: 'claude-3-5-sonnet-latest',
          messages: [{
            role: 'user',
            content: `${settings.prompt}\n\n${content}`
          }],
          max_tokens: 1500
        })
      });

      const data = await response.json();
      return data;
  }

  const main = () => {
    logseq.Editor.registerBlockContextMenuItem(
      'Send to Claude Block and children',
      async ({uuid}) => {        
        let contents = []
        const block  = await logseq.Editor.getBlock(uuid, {includeChildren: true})        
        contents.push(block.content)
        for (const child of block.children) {
          contents.push(child.content)
        }
        console.log("contents", contents)
        
        try {
          const data = await callClaudio(contents)
        
          console.log("data", data)
          
          if (data.content && data.content[0]) {

            // Insert Claude's response in a new block below
            await logseq.Editor.insertBlock(
              uuid,
              `${data.content[0].text}`,
              { sibling: false }
            );
          } 
        } catch (error) {
          console.error('Error calling Claude:', error);
          logseq.UI.showMsg('Error calling Claude API', 'error');
        }
      } 
    )
      
      console.log(`${settings.name} plugin loaded`);
  }
  

  logseq.ready(main).catch(console.error)
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions