Grammar and style checks on pcom

dimapetrov·

Since a few days ago pcom has acquired the functionality to check grammar and styling for the posts.

And yeah, LLMs are involved, so if you want to see a privacy friendly local first multilingual solution, you may check Harper in case it works for your language.

Times are weird and the part that was the most complex some years ago is now trivial, it's all about plumbing. Prior to implementing the feature I've been checking my posts manually from time to time. It was as simple as "check the text below for grammar" to get a list of issues that need to be addressed in a given text.

image.png

The other part that Claude specifically and other LLMs in general are good at is assessing the text and giving the feedback. I really don't like LLM-generated texts since I love the process of writing by itself, but I do find it useful to get some feedback regarding the logic in the text and stylistic problems. All that was needed was again a small prompt + text. Cool part about doing it in the chat though was the context. I would adjust the story in response to the feedback in case I agree with the suggestion and then send the updated text into the same chat to see if there is more feedback to work on.

I usually do not rely on claude to give me a corrected version of the text, but rather apply the grammar and style fixes myself, which allows me to assess the quality and in almost all the cases the agent did a great job for all the languages I use. I'm really sorry for the Grammarly, DeepL and other products that made a bet on the linguistic part of the equation.

The difference between the two checks is that in case of grammar fixes the solution can definitely be automatically applied if needed, since it's just a search and replace activity. Style fixes are a different beast — since they may require rearranging the parts or adding new information to improve the text.

image.png

I didn't even consider implementing a local check in any shape or form (local as in browser, or local as in constrained to pcom backend) since I'm a one man army and linguistic tools are not my hobby yet. My hobby is two build pcom and use it to write stories like this.

Enough intro, let me walk you through the details, the PR is linked above.

Three things you need to know about pcom:

  • It's built with Go + Go templates
  • It's an SPA, which is achieved by using Htmx + Stimulus.js combo
  • I aim to keep the project as lightweight as possible.

The last point made me rule out any big editor components like Code Mirror, that's an obvious overkill. Hence I've had as separate session with Claude where I iterated on the requirements to make sure nothing crazy would pop up later, which resulted in a plan for the feature. Just for clarity, for the requirements and plan preparation I always use Opus 5.5, but there is a requirement for every plan to split the work with cheaper models wherever possible, check the tasks in the plan for details.

The plan was executed in a fresh session, same Opus 5.5 as a coordinator.

Instead of messing with overlays only to be able to underscore the text I've decided to go with a text selection which is more than good enough and does not require rewriting half of the browser in Javascript.

image.png

pcom has two system prompts for grammar and style checks respectively plus two JSON schemas to get the structured response back.

Grammar checks are stateless, style checks are stateful in a sense that every next style check includes previous attempts thus enabling the iterations on the text.

Since it's my toy project I don't want to burn all my money on grammar checks, hence there are a number of settings to limit the request rate and the text size per user per day.

Now, how does the apply functionality work? How do we know that the text got stale and the fix is not applicable anymore? As you can see from the schemas the logic does not rely on exact positions, but rather on text match — we're looking for exact quote and use the previous text as an anchor. The panel code is quite fat but self contained and is loaded lazily for the editor page specifically.

In the database we have three new tables now:

  • one to keep track of all requests and use this for rate limiting
  • the other one to keep the style checks history and use it in the context
  • the third one to keep the writer feedback. Suppose you got a feedback that some piece of text does not make sense. What you do is you discard the suggestion and add a note why the agent got it wrong. This info will be included in the context for the next iteration.

Since pcom has a great test coverage now, this feature had to have one too which it did. Here is an e2e test. No request goes to the LLM provider, all the responses are mocked with wiremock which allows testing all scenarios without any problems.

In conclusion, I have to say that Opus 5.5 works almost magically for pcom at this moment. The feature was implemented almost from the first shot and most of the fixes I've made were fixes in business requirements, not in the code itself.