ReplyKit
  • How it works
  • Radar
  • Pricing
  • FAQ
Web appAdd to Chrome →

Tweet ideas

See all →
  • April Fools
  • Christmas
  • New Year
  • Valentine's Day
  • Halloween
  • Thanksgiving
  • Black Friday
  • Mother's Day
  • Father's Day
  • Birthday
  • Monday motivation
  • Friday
  • Good morning
  • Weekend
  • Summer
  • Product launch
  • Follower milestone
  • Hiring
  • Build in public
  • Founder
  • Funny
  • Motivational
  • Back to school
  • Earth Day
  • Work from home
  • New Year's resolutions
  • Year in review
  • Winter
  • Cyber Monday
  • New Year's Eve
  • Super Bowl
  • International Women's Day
  • St. Patrick's Day
  • Easter
  • Good night
  • Coffee
  • Gym
  • Relatable

Reply playbooks

See all →
  • SaaS
  • Startups
  • AI
  • Crypto
  • Marketing
  • Coding
  • Design
  • Product management
  • Data science
  • Sales
  • Fitness
  • Nutrition
  • Personal finance
  • Investing
  • Real estate
  • E-commerce
  • Freelancing
  • Content creators
  • Writing
  • Productivity
  • Career
  • Education
  • Gaming
  • Photography
  • Parenting
ReplyKit

Finds the posts worth replying to and drafts three replies in your voice. You post them yourself, always.

Product

  • Chrome Web Store
  • Mobile web app
  • Pricing
  • FAQ
  • Changelog

Resources

  • X reply generator
  • LinkedIn comment generator
  • Reddit reply generator
  • Guides
  • Tweet ideas
  • Reply playbooks
  • Compare
  • Blog

Company

  • Privacy policy
  • contact@replykit.eu
  • @FredericTheTall
  • Built by Frédéric Legrand

© 2026 ReplyKit · Paris

Not affiliated with X, LinkedIn or Reddit

ReplyKit on PeerPushReplyKit - Featured on Startup FameFeatured on Wall of ToolsListed on Beast Launches

Blog · engineering

21 Nov 202610 min read

Streaming an LLM reply through a Manifest V3 service worker

How ReplyKit streams a model's reply from a service worker into a page: split SSE lines, a worker that can vanish, stale chunks, and a permission trap.

Author

Frédéric Legrand builds ReplyKit on the side of a day job shipping computer-use agents. @FredericTheTall

ReplyKit is a Chrome extension that drafts replies on X, LinkedIn and Reddit. You click a button under a post, and three drafts appear word by word. The words come from a model behind my own API, as a server-sent event stream. Getting that stream from the network into a panel on someone else's page crosses two processes, and each crossing has a way to fail quietly.

This is how that path works as of November 2026, and the bugs that shaped it. The code is in src/background.ts, src/streaming.ts and src/content.ts.

The shape of the problem

A Manifest V3 extension has a content script, which runs inside the page, and a service worker, which runs in the background and can be shut down by Chrome when it has nothing to do. In ReplyKit the content script owns the panel and the worker is the only place that calls fetch for a generation. That gives one place to enforce a rule I care about: the worker fetches on behalf of messages from content scripts, so it only ever fetches my own API.

export function isApiUrl(url: unknown): url is string {
  return typeof url === "string" && url.startsWith(`${API_BASE}/`);
}

A content script lives in a page I do not control. If that page's renderer were compromised, a worker that fetched any URL a message named would be a general-purpose relay with the extension's privileges. With the check, the worst a forged message can do is call an endpoint that would have accepted the request anyway.

So the text has to travel: network, worker, content script, panel.

One message gets one response

The content script asks for a generation with chrome.runtime.sendMessage. The worker can answer through sendResponse, exactly once. A stream is many answers.

So the single response is spent on "the stream has started", and everything after it is pushed the other way, as new messages from the worker to the tab:

sendResponse({ success: true, streaming: true, chunk: "", complete: false });

const sendChunk = (chunk, fullText, complete, extra) => {
  const message = { action: "streamChunk", chunk, fullText, complete,
                    requestId: data.requestId, ...extra };
  chrome.tabs.sendMessage(tabId, message).catch(() => {});
};

Three details in those few lines each came from a bug.

Which tab. The tempting move is to ask Chrome for the active tab when it is time to send a chunk. Start a reply, switch tabs, and the rest of the draft goes to the wrong tab or nowhere. The tab that asked is on the original message as sender.tab.id. Use that and nothing else.

Every chunk carries the whole text so far. fullText is cumulative. The panel never appends; it renders what the latest message says. A chunk that fails to deliver costs nothing, because the next one contains it.

Delivery failures are swallowed. The user may have navigated away mid-reply. tabs.sendMessage then rejects, and an unhandled rejection inside the read loop would stop the stream before its bookkeeping ran. Hence the empty catch.

One more case showed up later. The welcome page of the extension also streams a first draft, and an extension page is not a content script: tabs.sendMessage never reaches it. For that sender the chunks go out with chrome.runtime.sendMessage instead. The worker tells the two apart by checking whether the sender's URL starts with the extension's own origin.

SSE lines do not respect chunk boundaries

The response body arrives through a reader in pieces of whatever size the network felt like. A server-sent event is a line starting with data: . Nothing says a piece ends where a line ends, so a JSON payload can be cut in half between two reads. Parsing each piece on its own works on a fast connection and loses words on a slow one.

The fix is the usual one: keep the unfinished tail and prepend it to the next piece.

export function splitSseChunk(buffer: string, chunk: string) {
  const lines = (buffer + chunk).split("\n");
  const rest = lines.pop() || "";
  return { lines, buffer: rest };
}

Two smaller things belong with it. The decoder is called with { stream: true }, so a multi-byte character split across two reads is held back instead of turning into a replacement character, which matters for emoji and for replies in Japanese or Korean. And when the reader reports it is done, whatever is left in the buffer is processed as a last line, because the final event does not always end with a newline.

A stream can fail after it has started

An HTTP error before the first byte is easy: the status is not OK, the worker maps it to a message a person can read, and sendResponse carries it back. After the stream starts, sendResponse is already used. Anything that goes wrong from then on has only one way to reach the page: a final chunk with an error field.

There are three such cases, and each gets its own ending:

  • The model reports an error mid-stream (overloaded, for example). The stream ends normally from the network's point of view. The worker notices the error event and finishes with "ReplyKit is busy right now" instead of presenting half a sentence as a draft.
  • The reply hits the output limit. The text is kept, since it may still be usable, and the final chunk is flagged truncated so the panel can say the draft may be cut off.
  • The read throws (the connection dropped). The catch sends a terminal error chunk. Without it the panel shows "Generating…" forever.

The panel treats a partial draft differently from an empty one. If nothing was written, the error replaces the placeholder. If half a reply is there, the text stays and a note says it was interrupted.

The worker can vanish, so the page keeps its own clock

The terminal error chunk covers failures the worker lives to see. It does not cover the worker itself going away. Chrome may shut a service worker down, and an extension update or reload replaces it. When that happens mid-stream there is no exception to catch and nobody left to send the last message.

So the content script does not trust the worker to finish. It records when the last chunk arrived, and a timer checks every five seconds whether that was more than 25 seconds ago. If so, it stops waiting, tells the user the reply stalled, and gives the Retry button back.

watchdogTimer = setInterval(() => {
  if (activeRequestId === null || Date.now() - lastChunkAt < STREAM_STALL_MS) return;
  cancelActiveStream();
  showGenerationFailure(area, "The reply stalled. Check your connection and hit Retry.");
  endGeneration();
}, 5000);

Giving up on the page side is not enough. If the worker is still alive and the fetch is merely slow, the stream keeps running: the server keeps generating, and when the user hits Retry a second stream runs next to the first. So giving up also sends a cancelStream message, and the worker keeps an AbortController per live stream to act on it.

That had a race of its own. Before the content script sends the request it does some asynchronous preparation (reading storage, making sure a token exists). The watchdog starts at the click, so if that preparation is what hangs, the cancel can arrive at the worker before the request it cancels. The worker would find nothing to abort, drop the cancel, and then start the stream. Now a cancel for an unknown stream leaves a short-lived tombstone, and a request that arrives with a tombstoned id is refused.

Chunks from a generation nobody is waiting for

Click Retry while a draft is still streaming and, for a moment, two streams exist. The old one's chunks are still in flight. Because every chunk carries the full text, a late chunk from the old stream does not append a few stray words: it replaces the new draft with the old one.

Every generation therefore gets a requestId, claimed synchronously at the click, before any await, so two fast clicks cannot both get through. The listener drops any chunk whose id is not the live one:

if (activeRequestId === null || message.requestId !== activeRequestId) return;

The id is a counter per tab, and the worker keys its streams by tab id plus request id. The counter is seeded from the clock, not from zero. The tab id survives a page reload while the content script does not, so a counter that restarted at 1 could collide with a stream or a tombstone its previous incarnation left behind.

The permission I did not add

The API lives on its own host. The obvious thing is to list that host under host_permissions in the manifest. I do not, on purpose: when an update adds a host permission, Chrome disables the extension for every existing user until they open the browser's prompt and approve it. Anyone who misses that prompt has an extension that just stopped working.

Without the permission, the worker's request is an ordinary cross-origin fetch, so the API has to allow it with CORS. That part is one header. The less obvious part is that a cross-origin response hides most of its headers from the caller. The API reports the user's remaining quota in response headers, and the worker reads null for all of them unless the API lists them explicitly:

"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "X-Calls-Used, X-Calls-Limit, X-Plan",

The server's count is the authoritative one. The worker passes it along on the final chunk, and the panel overwrites its local counter with it, so the number the user sees cannot drift from the one the server enforces.

What reaches the reply box

None of this streams into the site's own editor. The words appear in ReplyKit's panel, and only the draft you pick is inserted, in one go. X's composer is a Draft.js editor that does not tolerate being written to piece by piece; that is its own story, in How a Chrome extension types into X's reply box.

What I would tell someone starting this

  • Spend the one response on "started", then push. Address chunks to the tab that asked.
  • Send the cumulative text with every chunk. Lost and late messages stop being a correctness problem.
  • Buffer partial SSE lines and decode in streaming mode. Test on a throttled connection.
  • After the first byte, every failure needs a terminal message of its own, or the UI spins forever.
  • Assume the worker can disappear. The page needs a clock, and giving up has to cancel the fetch too.
  • Tag every generation with an id and drop what does not match.
  • Think twice before adding a host permission to an extension people already use.

Built by · One person

ReplyKit is a solo project. Tell me what you need.

There is no company behind this, just one developer who reads every message. If ReplyKit does not fit how you work, a feature, a platform, a different style of reply, say so. Requests from users decide what ships next.

Prefer email? contact@replykit.eu

Goes to my phone as a Telegram message, not into a queue.

← All postsAdd to Chrome, free →