Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerBlockContextMenuItem registers multiple times #40

Open
stormsson opened this issue Nov 11, 2024 · 0 comments
Open

registerBlockContextMenuItem registers multiple times #40

stormsson opened this issue Nov 11, 2024 · 0 comments
Assignees

Comments

@stormsson
Copy link

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)
`
@xyhp915 xyhp915 self-assigned this Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants