logseq / logseq/logseq-plugin-samples

registerBlockContextMenuItem registers multiple times

Abierto
#40 0 comentarios 0 reacciones 1 asignado Ver en GitHub

@xyhp915 ya está trabajando en esto.

Desde el 12/11/2024.

Lenguaje dominante
CSS
Estrellas
383
Forks
97
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.