justyns / silverbullet-ai

Plug for SilverBullet to integrate LLM functionality
https://ai.silverbullet.md/
GNU Affero General Public License v3.0
26 stars 1 forks source link

Operating on a paragrph, line or selection #59

Open meain opened 1 month ago

meain commented 1 month ago

It is possible that I might end up having to build out a separate plugin, but just wondering if it would be possible/worth it to support these use-cases in this PLUG.

I was trying to replicate what b-yp/logseq-magic-todo does but I don't think we currently have a way target to anything specific(task under cursor) like in this case in this case. In case of LogSeq, since it works on a block level, there is option to get the current block which would be analogous to getting a task.

As SB has no way to do "get me the task that the cursor is on", if there is some way this plug can provide "work on the currently selected text", we could probably do something like this.

PS: I'm just exploring possibilities, not in any rush to have this feature :D

justyns commented 1 month ago

Just going off of the demo screencast on that repo, it looks like a cool idea! I think being able to support use-cases like this would be worth it, especially if it can be done without adding a hardcoded command to the plug itself.

As SB has no way to do "get me the task that the cursor is on", if there is some way this plug can provide "work on the currently selected text", we could probably do something like this.

Silverbullet does havedetermineItemBounds to get the start/end position of a task, I'm using it like this in a spacescript:

    const cursorPos = await syscall("editor.getCursor");
    const text = await syscall("editor.getText");
    const currentItemBounds = await syscall("system.invokeFunction", "editor.determineItemBounds", text, cursorPos, undefined, true);
    const currentItemText = text.slice(currentItemBounds.from, currentItemBounds.to);

Thinking about it, this is almost already possible. Templated Prompts support aiPrompt.insertAt: cursor to have the result inserted at the current cursor location. There are two things we'd need:

meain commented 1 month ago

None of the "editor" syscalls seem to be available to Space Script. I also get an error when I use in a space script. Am I missing something?

justyns commented 1 month ago

iirc not all of the syscalls show up on the documentation page, there's a discussion about it on https://github.com/silverbulletmd/silverbullet/issues/839 and the linked issue.

I also forgot to mention that editor.determineItemBounds was only recently exposed, I think in 0.9.0. And it's only exposed using the system.invokeFunction syscall, not as a syscall itself.

zefhemel commented 1 month ago

Full syscall docs are now here: https://jsr.io/@silverbulletmd/silverbullet/doc/syscalls/~

justyns commented 4 days ago

@meain

I just merged #68 into main. It should support use cases like the one you linked to. I also added a 'AI Split Todo' template prompt to test with and see how it works.

When you get a chance, could you give it a try and see if you have any feedback?

(note to self: I still need to add support for replacing a whole paragraph or selected text, but there is a 'replace-line' option now)

meain commented 4 days ago

Just gave it a test run. This is awesome. The idea of post processors is pretty neat.

Unrelated: I've also been thinking more about adding post processors in meain/yap. This was the validation I needed 😅.