# Track C — Extend `@bytelyst/fastify-sse` with Per-Request Helpers

> **Repo:** `learning_ai_common_plat`
> **Can start:** Immediately (no dependencies)
> **Estimated time:** ~2 hours
> **Roadmap:** `../LOCAL_AI_CONSOLIDATION_ROADMAP.md` — section 4.2.

---

## Goal

Extend the existing `@bytelyst/fastify-sse` package (`packages/fastify-sse/`) with per-request SSE helpers. The package currently only provides `SSEHub` (multi-client broadcast). We need single-request streaming helpers too.

---

## Context

| Existing                         | What it does                                                                         |
| -------------------------------- | ------------------------------------------------------------------------------------ |
| `@bytelyst/fastify-sse` `SSEHub` | Multi-client broadcast pattern (hub/spoke) — one event pushed to N connected clients |
| LocalMemGPT `sse-helpers.ts`     | Single-request streaming — one route handler streams SSE to one client               |

The per-request pattern is used by chat streaming and model comparison endpoints.

---

## Reference Implementation

Read `../learning_ai_local_memory_gpt/backend/src/lib/sse-helpers.ts` (26 lines):

```ts
startSSE(reply); // Sets Content-Type: text/event-stream headers, calls reply.hijack()
sendSSEData(reply, data); // Writes "data: JSON.stringify(data)\n\n"
sendSSEEvent(reply, event, data); // Writes "event: name\ndata: JSON.stringify(data)\n\n"
endSSE(reply); // Writes "data: [DONE]\n\n" and ends the stream
```

---

## Implementation

1. Add a new file: `packages/fastify-sse/src/per-request.ts`
   - `startSSE(reply: FastifyReply): void`
   - `sendSSEData(reply: FastifyReply, data: unknown): void`
   - `sendSSEEvent(reply: FastifyReply, event: string, data: unknown): void`
   - `endSSE(reply: FastifyReply): void`

2. Export from `packages/fastify-sse/src/index.ts` alongside existing exports:

   ```ts
   export { startSSE, sendSSEData, sendSSEEvent, endSSE } from './per-request.js';
   ```

3. Add tests: `packages/fastify-sse/src/per-request.test.ts`
   - Test that `startSSE` sets correct headers and hijacks the reply
   - Test that `sendSSEData` formats data correctly
   - Test that `sendSSEEvent` formats named events correctly
   - Test that `endSSE` sends the `[DONE]` sentinel and ends the stream

4. Publish updated package to Gitea registry

---

## Conventions

- ESM (`"type": "module"`), TypeScript
- Fastify 5 types (`FastifyReply`)
- Vitest for tests
- Don't break existing `SSEHub` / `ssePlugin` exports

---

## Scope Boundary

**DO NOT** migrate consumer repos — that happens in Track E. Only add the per-request helpers to the package.

---

## Commit

`feat(fastify-sse): add per-request SSE helpers`
