logseq / logseq/logseq-plugin-samples

registerBlockContextMenuItem registers multiple times

Open
#40 0 comments 0 reactions 1 assignee View on GitHub

@xyhp915 is already working on this.

Since Nov 12, 2024.

Dominant language
CSS
Stars
383
Forks
97
PR merge metrics
No merged PRs in 30d

Description

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)
`

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.