Interactive queries

Parameterizing Knowboard queries for user input.


Play

Adding Resource Actions to Knowboard meant that I could have all the updates I wanted quickly accessible as Code Actions in the document. This was extremely powerful, and I started adding actions for many of my common updates like: creating tasks, marking them as completed, or re-scheduling, as well as managing many other types of documents like tracking status of articles to read, or adding reviews to a book I read.

One feature I left out of the initial implementation was the ability to use parameters in the queries. Ideally I wanted to take additional inputs like the name of a new task, or picking a review rating. However, the Language Server Protocol is fairly limited in support for user-input. There’s one call to prompt the user to select from a small list of choices, and that’s basically it. If you need others, it requires adding custom client-side commands and bespoke UI integrations for each editor. So, I decided to forego parameters while I learned what patterns I would use most. Typically I would write the query to insert a placeholder value to edit afterwards, which was sufficient to get a lot of the value without the added parameter complexity. In a few cases I started trying more complicated workarounds like adding a parameter into the document before running the action, which the query would read. For example, inserting deferDays: 7 and then running “Re-Schedule” to move the date by a week. This was quite clunky, but let me think about how I wanted things to work.

Then the Go team wrote about how they added Interactive Refactoring to the Go Language Server in collaboration with the Dart team. At the core is a proposal to the LSP standard to introduce a new command to request user inputs. With this change, when a user clicks a Code Action the client first asks the server “does this command require any inputs?” The server responds with a list of “form fields” that the client should request from the user. The client sends those inputs back to the server, checking “now is it complete?” This back-and-forth allows the server to do validation, marking any fields that have errors, or requesting additional inputs until all the necessary fields have been completed. The client can then execute the desired command, along with the user-provided input values.

Along with the proposal they provided reference client-side implementations, such as the one they’re using in the VS Code extension to demonstrate the feature in action. This fulfilled what I was looking for and I easily imported it into the Knowboard VS Code extension and focused on the server-side to support parameterized queries.

SPARQL does not include a standard way to provide query parameters, though the SHACL functions specification includes sh:parameter which is also used in DASH templates, so I used that as the basis for declaring parameters. Knowboard uses Oxigraph as the SPARQL engine, which does have an API for parameter substitution, but with some strict limitations. So I ended up building a more flexible version that still uses the Oxigraph SPARQL AST to parse and replace the parameters with concrete values which I’m intending to release as a separate library. There are some subtleties to why Oxigraph has its limitations, which probably affect the correctness of my implementation in some situations, but in practice it works well.

With SPARQL parameters in place I started connecting it to the proposed LSP API. Knowboard attempts to map SHACL contraints onto the approprate input types. A numeric parameter will ask for a number input, and sh:in will use enum to prompt the user with a select list of valid inputs. For parameters that link to other resources, Knowboard uses the lazy enum input which is designed for when there are too many options to list out in the initial response. So if you have a parameter with the restriction sh:class schema:Movie, when the client prompts for an input, it sends a new request to fetch the values, and Knowboard looks up matching subjects with type schema:Movie in your workspace.

Now, after using it for a few weeks most my actions include at least one parameter and this has opened up possibilities for actions that would not have been practical before. Marking a task as “Completed” prompts for a description, turning a “quick note” into a task now prompts for the name and project, etc.

There are a few finishing touches to finalize this release, but I hope to have Knowboard 0.4 out soon with this feature.