ui-react: bytes-to-base64 and back are hand-written in five places
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 80/100
- Issue type
- Refactor
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- bun, typescript
Research direction
Add the shared helpers in packages/ui-react/src/lib/base64.ts and update the five listed encode and decode sites plus drafts.ts and image-attachments.ts. Start by reading those implementations and the corresponding tests; add packages/ui-react/tests/base64.test.ts for the stated round trips and decoded length checks. Done means the acceptance criteria pass, existing tests remain unchanged and pass, and api-report/ui-react.txt is unchanged.
Written by the indexing model from the issue text.
Description
Context
packages/ui-react converts between bytes and base64 by hand at each place that needs it.
Encode, by appending one character per byte to a string and calling btoa:
packages/ui-react/src/voice/asr-gemini.ts:241-243, once per captured PCM frame;packages/ui-react/src/voice/conversation-client.ts:589-591, once per captured PCM frame;packages/ui-react/src/lib/push-registration.ts:164-166, followed by the base64url substitutions.
Decode:
- (
decodeBase64,packages/ui-react/src/lib/draft-api.ts:37-42), acharCodeAtloop into a preallocated array; packages/ui-react/src/voice/conversation-client.ts:1041,Uint8Array.from(atob(pcm), c => c.charCodeAt(0)), once perconversation_outputframe on the playback path.
Decoded length, from the base64 text's length and padding, is a third pair of identical copies: (decodedBytes, packages/ui-react/src/lib/drafts.ts:19-23) and (decodedBytesFromBase64, packages/ui-react/src/lib/image-attachments.ts:30-34).
The impact is small: five copies of a loop that is easy to get subtly wrong, with two different decode strategies. The package has no shared binary helper; packages/ui-react/src/lib/ is where its other pure helpers live, so the surviving implementation is a new lib/base64.ts.
Rule that wins: encode with the per-byte loop the three sites share; decode with the preallocated charCodeAt loop from draft-api.ts. Both produce the same bytes as today.
Scope
- Add
bytesToBase64(bytes: Uint8Array): stringandbase64ToBytes(base64: string): Uint8Arraytopackages/ui-react/src/lib/base64.ts. - Add
base64DecodedLength(base64: string): numberbeside them;drafts.tsandimage-attachments.tscall it and delete their copies. - The three encode sites and the two decode sites call them.
push-registration.tskeeps its base64url substitutions after the call.
Out of scope
- Replacing the loops with
Uint8Array.prototype.toBase64/Uint8Array.fromBase64. Whether the platform methods may be used depends on the browser support policy under #2587 and has to be checked against current support data first. String.fromCharCode(...payload)inpackages/ui-react/src/voice/encoded-duration.ts:79, which compares a codec tag and is not base64.- Base64 handling outside
packages/ui-react. - Exporting the helper from the package entry.
Acceptance criteria
-
git grep -n "String.fromCharCode\|atob(\|btoa(" -- packages/ui-react/srcreturns onlylib/base64.tsandvoice/encoded-duration.ts:79. -
git grep -n 'b64.length \* 3' -- packages/ui-react/srcreturns one line, inlib/base64.ts, and the unit test assertsbase64DecodedLength(bytesToBase64(b)) === b.lengthfor every length in the round-trip set. - A unit test round-trips byte arrays of length 0, 1, 2, 3, 4 and 4,097 containing every byte value, and pins
bytesToBase64(new Uint8Array([0, 255, 16]))to"AP8Q". - The existing voice, push-registration and draft tests pass unchanged (
packages/ui-react/tests/asr-gemini.test.tsand the tests that cover the other four files). -
api-report/ui-react.txtis unchanged.
Verification
bun run test packages/ui-react/tests/base64.test.ts packages/ui-react/tests/asr-gemini.test.ts
bun run test packages/ui-react/tests
NODE_OPTIONS=--max-old-space-size=6144 bun run typecheck
bun run lint
bun run check:pr --base origin/main
Contract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
The bytes and strings produced are the same. The changeset gate still asks for a changeset on any edit under a published package's src; add a patch entry that says the change is internal.
Found by a repository audit on 2026-11-26 (main 87b96b10c); independently reviewed before filing.
- Dominant language
- TypeScript
- Stars
- 5
- Forks
- 0
- Avg merge
- 8h 3m
- Merged PRs (30d)
- 606
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from schlessera/brain-kit
-
agent-ready area: ui-server priority: p3 type: refactor
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
schlessera/brain-kit#4833 ·
Maintainers usually reply within 1 day
-
ui-sdk: the turnId doc comment tells client authors echoing is optional, which a rev 3 host refusesOpenagent-ready area: ui-sdk priority: p3 type: docs
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
schlessera/brain-kit#4824 ·
Maintainers usually reply within 1 day
-
agent-ready area: ui-react blocked priority: p3 type: refactor
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
schlessera/brain-kit#4819 ·
Maintainers usually reply within 1 day
-
jobs: every command writes on open, so a read fails with "database is locked" during a long writeOpenagent-ready area: modules priority: p2 type: fix
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
schlessera/brain-kit#4809 ·
Maintainers usually reply within 1 day
-
agent-ready area: ui-server priority: p3 type: fix
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
schlessera/brain-kit#4807 ·
Maintainers usually reply within 1 day
All issues in schlessera/brain-kit
Similar issues
-
P3
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
bug L3 level 1 needs-review wocs
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
SatyamPandey-07/WorkSphere#10739 · 1 comment ·
Maintainers usually reply within 1 day
-
maintenance
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
assistant-ui/assistant-ui#10946 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:auth-provider issue-rating: 🦞 diamond lobster no-stale P2
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
openclaw/openclaw#206303 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day