docs(console-ui): explain data and byte transformations

This commit is contained in:
Maofeng
2026-07-30 08:13:37 +08:00
parent 223ead37c8
commit 4e32a1246b
3 changed files with 44 additions and 0 deletions
+16
View File
@@ -1,3 +1,11 @@
/**
* Pure conversion and presentation helpers for the management UI.
*
* No function in this module performs I/O or changes backend state. Keeping
* byte decoding here prevents React views from silently treating arbitrary
* Component output as readable text.
*/
import { FileCode2, History, LayoutDashboard, Package, Server, Settings } from "lucide-react";
import { BackendCapability, BackendEvent, BackendService } from "./api";
@@ -160,6 +168,9 @@ export function decodeReadableUtf8(bytes: Uint8Array) {
return null;
}
// Valid UTF-8 can still contain terminal controls. Reject them so the output
// panel cannot render invisible or misleading content; tab/newline/CR remain
// useful for ordinary textual responses.
for (const character of text) {
const codePoint = character.codePointAt(0) ?? 0;
const allowedWhitespace = codePoint === 0x09 || codePoint === 0x0a || codePoint === 0x0d;
@@ -175,6 +186,9 @@ export function formatInvocationOutput(
output: Uint8Array,
format: "utf8" | "hex",
) {
// Counter is the one built-in example with a documented numeric wire
// format. Other eight-byte services remain generic bytes and are not
// guessed as integers.
if (service.id === "counter" && output.byteLength === 8) {
return {
text: new DataView(output.buffer, output.byteOffset, output.byteLength)
@@ -192,6 +206,8 @@ export function formatInvocationOutput(
} satisfies FormattedInvocationOutput;
}
// UTF-8 decoding is fatal rather than replacement-based. If one byte is
// invalid or contains a disallowed control, show the exact bytes as HEX.
const text = decodeReadableUtf8(output);
if (text !== null) {
return {