Register a new template function from a plug?

Can I call silverbullet.registerFunction from a plug somehow? It looks like it only works from inside a space script block.

I also tried overriding builtinFunctions from builtin_query_functions.ts just to see if I could insert a new function that way but it doesn’t work probably because of the load order.

edit: This sort of works as a workaround if I put this in a spacescript, but I’m wanting to avoid the space script part and provide it from the plug:

silverbullet.registerFunction({name: "queryOpenAI"}, async (systemPrompt, userPrompt) => {
  const pageContent = await syscall("system.invokeFunction", "silverbullet-ai.queryOpenAI", systemPrompt, userPrompt);
  return pageContent;
})

Gentle bump in-case someone is squatting on a secret answer. I’m interested in this too.

No, this is not supported in any other way than how you demonstrated. However it does come with a performance penalty.

Plugs run in their own sandbox and communicate asynchronously via message passing with the editor. Since functions tend to be invoked from queries, often on thousands of entries, this would result in a LOT of message passing between the plug and query engine. That’s why space script run in the main process, it’s less safe but way more performant.

2 Likes