feat(frontend): adapter web HTTP+WebSocket derrière les ports (#13)

Lot F1 du chantier server/client mode : nouvel adaptateur web branché
derrière les ports d'invocation et de flux live, permettant au frontend de
dialoguer avec le backend via HTTP + WebSocket en mode client/serveur. Le
mode desktop (Tauri IPC) reste inchangé.

- frontend/src/adapters/http : invoker HTTP, client live WebSocket, gateways
  request/response et stream, frames, garde unsupported (7 fichiers + 2 tests).
- frontend/src/app : câblage DI (di.tsx) et son test, typage vite-env.d.ts.

Validé : build vert, garde no-direct-invoke verte, 724 tests verts,
desktop inchangé.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-15 12:50:38 +02:00
parent c8fef2a76a
commit e0cdb4aa56
12 changed files with 1783 additions and 5 deletions

View File

@ -0,0 +1,124 @@
/**
* WebSocket frame contract for the web (client/server) transport — ticket #13,
* lot F1. Mirrors the first-draft frames in
* `docs/ticket13-b0-backend-transport-inventory.md` (§ "WebSocket first draft").
*
* F1 delivers the *types + skeleton* only. The full PTY wiring runs on top of
* these in F3/B5 once a server exists (B3/B4). Keeping the frame shapes here — in
* the adapters layer — lets the terminal/agent/chat gateways be structurally
* correct against the contract today without a live backend.
*
* Envelope (B0): every frame carries an `id` (client→server) or optional
* `replyTo` (server→client, present for acknowledgements, absent for unsolicited
* output/events). PTY bytes travel base64-encoded (`bytesBase64`) so binary
* survives JSON.
*/
// ---------------------------------------------------------------------------
// Base64 helpers (binary PTY bytes over JSON frames)
// ---------------------------------------------------------------------------
/** Encodes raw bytes to a base64 string for a `bytesBase64` frame field. */
export function bytesToBase64(bytes: Uint8Array): string {
let binary = "";
for (let i = 0; i < bytes.length; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
// `btoa` exists in browsers and jsdom.
return btoa(binary);
}
/** Decodes a base64 `bytesBase64` frame field back to raw bytes. */
export function base64ToBytes(base64: string): Uint8Array {
const binary = atob(base64);
const out = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) {
out[i] = binary.charCodeAt(i);
}
return out;
}
// ---------------------------------------------------------------------------
// Client → server frames
// ---------------------------------------------------------------------------
/** Client→server frame kinds (B0 § PTY + structured chat + ping). */
export type ClientFrameKind =
| "terminal.attach"
| "terminal.open"
| "agent.launch"
| "terminal.input"
| "terminal.resize"
| "terminal.detach"
| "terminal.close"
| "chat.attach"
| "chat.send"
| "chat.detach"
| "ping";
/** A client→server frame. `id` correlates the eventual `replyTo`. */
export interface ClientFrame {
id: string;
kind: ClientFrameKind;
payload: Record<string, unknown>;
}
// ---------------------------------------------------------------------------
// Server → client frames
// ---------------------------------------------------------------------------
/** Server→client frame kinds. */
export type ServerFrameKind =
| "terminal.attached"
| "terminal.output"
| "terminal.status"
| "chat.attached"
| "chat.output"
| "event.domain"
| "error"
| "pong";
/** Suggested lifecycle statuses (B0). */
export type TerminalStatus =
| "starting"
| "running"
| "exited"
| "closed"
| "detached"
| "reattached";
/** A server→client frame. `replyTo` is present only for acknowledgements. */
export interface ServerFrame {
kind: ServerFrameKind;
replyTo?: string;
payload: Record<string, unknown>;
}
/** Payload of a `terminal.attached` acknowledgement. */
export interface AttachedPayload {
session: {
sessionId: string;
projectId?: string;
nodeId?: string | null;
rows: number;
cols: number;
};
nextSeq: number;
scrollback: { seq: number; bytesBase64: string }[];
gap: boolean;
/** Conversation id minted by an `agent.launch` (mirrors `assignedConversationId`). */
assignedConversationId?: string;
}
/** Payload of a `terminal.output` frame. */
export interface OutputPayload {
sessionId: string;
seq: number;
bytesBase64: string;
}
/** Payload of a `chat.output` frame (structured assistant stream). */
export interface ChatOutputPayload {
sessionId: string;
chunk: unknown; // ReplyChunk-shaped (kind-tagged); normalized by the caller.
}