feat(vscode): extension VS Code native (intégration native, pas un webview)
Nouveau workspace packages/vscode (git-arboretum, privé, non publié sur npm), client REST/WS réutilisant @arboretum/shared. Auth Authorization: Bearer sur REST et l'upgrade WS (via `ws`) ; un client Node sans en-tête Origin passe le check Origin strict du serveur. - Arbres temps réel : Repositories (repos → worktrees → sessions) et Groups, via le WebSocket. - Terminaux natifs (vscode.Pseudoterminal) pour attacher/observer une session — rendu et scrollback de VS Code ; décodage UTF-8 streaming + comptabilité ACK dans des modules purs. - Status bar (compteur waiting) + notifications natives sur passage en waiting, réponses Yes/No via la commande WS answer. - Mutations git (create worktree, commit, push, promote), start/kill/hide/resume/fork, session de groupe ; conscience du workspace (reveal + start/create here). - Bundle esbuild (format cjs, external vscode) inlinant @arboretum/shared → VSIX autonome. Logique réutilisable sans import vscode → testée par vitest (19 tests). - CI : .gitea/workflows/vscode-release.yml package le VSIX sur tag vscode-vX.Y.Z (artefact + asset de release best-effort). build:vscode hors du build principal (comme le site). - spikes/s5-vscode/STUDY.md : décision de conception (GO phasé A→D), marquée implémentée.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
// Pont d'une session vers un terminal — module pur (aucun import vscode), testable.
|
||||
// Le ws-client remet des octets BRUTS (un chunk PTY peut couper un caractère UTF-8 en frontière de
|
||||
// frame, exactement comme côté web où xterm.write(Uint8Array) gérait le décodage). Ici la cible est
|
||||
// un Pseudoterminal qui veut des `string` → on décode en streaming avec un TextDecoder, et on recrée
|
||||
// le décodeur à chaque RESYNC pour ne pas traîner une séquence partielle au-delà d'un reset.
|
||||
import type { ArbWsClient, Attachment, AttachmentSink, DetachReason } from '../api/ws-client.js';
|
||||
|
||||
/** Décodeur UTF-8 incrémental : conserve l'octet partiel d'une frontière de frame jusqu'au suivant. */
|
||||
export class StreamingUtf8Decoder {
|
||||
private decoder = new TextDecoder('utf-8');
|
||||
|
||||
decode(chunk: Uint8Array): string {
|
||||
return this.decoder.decode(chunk, { stream: true });
|
||||
}
|
||||
|
||||
/** Vide l'état partiel et repart à neuf (appelé sur RESYNC). */
|
||||
reset(): void {
|
||||
this.decoder = new TextDecoder('utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
export interface SessionBridgeCallbacks {
|
||||
/** texte décodé à écrire dans le terminal. */
|
||||
onText(text: string): void;
|
||||
/** RESYNC reçu : le terminal doit se réinitialiser (le texte de replay suit via onText). */
|
||||
onReset(): void;
|
||||
onDetached(reason: DetachReason): void;
|
||||
onControlChanged(controlling: boolean): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relie une session Arboretum à un terminal via le ws-client. Instancié par l'adaptateur
|
||||
* Pseudoterminal ; reste pur pour être testable avec un faux ws-client.
|
||||
*/
|
||||
export class SessionBridge {
|
||||
private attachment: Attachment | null = null;
|
||||
private readonly decoder = new StreamingUtf8Decoder();
|
||||
private detached = false;
|
||||
|
||||
constructor(
|
||||
private readonly ws: ArbWsClient,
|
||||
private readonly sessionId: string,
|
||||
private readonly mode: 'interactive' | 'observer',
|
||||
private readonly cb: SessionBridgeCallbacks,
|
||||
) {}
|
||||
|
||||
/** Ouvre l'attache ; résout quand le canal est établi. Rejette si la session est introuvable. */
|
||||
async open(cols: number, rows: number): Promise<void> {
|
||||
const sink: AttachmentSink = {
|
||||
data: (payload) => {
|
||||
if (this.detached) return;
|
||||
const text = this.decoder.decode(payload);
|
||||
if (text.length > 0) this.cb.onText(text);
|
||||
},
|
||||
reset: () => {
|
||||
this.decoder.reset();
|
||||
this.cb.onReset();
|
||||
},
|
||||
detached: (reason) => {
|
||||
this.detached = true;
|
||||
this.cb.onDetached(reason);
|
||||
},
|
||||
controlChanged: (c) => this.cb.onControlChanged(c),
|
||||
};
|
||||
this.attachment = await this.ws.attach({ sessionId: this.sessionId, mode: this.mode, cols, rows, sink });
|
||||
}
|
||||
|
||||
sendStdin(data: string): void {
|
||||
this.attachment?.sendStdin(data);
|
||||
}
|
||||
|
||||
resize(cols: number, rows: number): void {
|
||||
this.attachment?.resize(cols, rows);
|
||||
}
|
||||
|
||||
answer(action: 'select' | 'confirm' | 'deny', optionN?: number): void {
|
||||
this.attachment?.answer(action, optionN);
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.detached = true;
|
||||
this.attachment?.detach();
|
||||
this.attachment = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// Adaptateur vscode.Pseudoterminal : expose une session Arboretum comme un terminal NATIF de VS Code
|
||||
// (rendu, scrollback, copier/coller, liens gratuits). C'est la pièce maîtresse de l'intégration —
|
||||
// pas de xterm-in-webview. Le pont WS/décodage vit dans SessionBridge (pur) ; cet adaptateur ne fait
|
||||
// que relier les events Pseudoterminal ↔ bridge.
|
||||
import * as vscode from 'vscode';
|
||||
import type { SessionSummary } from '@arboretum/shared';
|
||||
import type { ArbWsClient } from '../api/ws-client.js';
|
||||
import { SessionBridge } from './session-bridge.js';
|
||||
|
||||
const ANSI_RESET = '\x1bc'; // RIS : réinitialisation complète du terminal (avant le replay d'un RESYNC)
|
||||
|
||||
export class SessionTerminalManager {
|
||||
private readonly terminals = new Map<string, vscode.Terminal>();
|
||||
private readonly bridges = new Map<string, SessionBridge>();
|
||||
|
||||
constructor(private readonly ws: ArbWsClient) {}
|
||||
|
||||
/** Bridge d'une session si un terminal est ouvert (pour répondre à un dialogue sans ré-attacher). */
|
||||
getBridge(sessionId: string): SessionBridge | undefined {
|
||||
return this.bridges.get(sessionId);
|
||||
}
|
||||
|
||||
/** Ouvre (ou ré-affiche) un terminal natif attaché à la session. */
|
||||
open(session: SessionSummary, mode: 'interactive' | 'observer'): void {
|
||||
const existing = this.terminals.get(session.id);
|
||||
if (existing) {
|
||||
existing.show();
|
||||
return;
|
||||
}
|
||||
|
||||
const writeEmitter = new vscode.EventEmitter<string>();
|
||||
const closeEmitter = new vscode.EventEmitter<number | void>();
|
||||
let bridge: SessionBridge | null = null;
|
||||
|
||||
const pty: vscode.Pseudoterminal = {
|
||||
onDidWrite: writeEmitter.event,
|
||||
onDidClose: closeEmitter.event,
|
||||
open: (dims) => {
|
||||
bridge = new SessionBridge(this.ws, session.id, mode, {
|
||||
onText: (text) => writeEmitter.fire(text),
|
||||
onReset: () => writeEmitter.fire(ANSI_RESET),
|
||||
onDetached: () => {
|
||||
writeEmitter.fire('\r\n\x1b[2m[arboretum: session ended]\x1b[0m\r\n');
|
||||
closeEmitter.fire();
|
||||
},
|
||||
onControlChanged: (controlling) => {
|
||||
if (!controlling && mode === 'interactive') {
|
||||
writeEmitter.fire('\r\n\x1b[33m[arboretum: control lost — now observing]\x1b[0m\r\n');
|
||||
}
|
||||
},
|
||||
});
|
||||
this.bridges.set(session.id, bridge);
|
||||
bridge.open(dims?.columns ?? 80, dims?.rows ?? 24).catch((err: Error) => {
|
||||
writeEmitter.fire(`\r\n\x1b[31m[arboretum: cannot attach — ${err.message}]\x1b[0m\r\n`);
|
||||
closeEmitter.fire();
|
||||
});
|
||||
},
|
||||
close: () => {
|
||||
bridge?.close();
|
||||
this.bridges.delete(session.id);
|
||||
this.terminals.delete(session.id);
|
||||
},
|
||||
handleInput: (data) => {
|
||||
if (mode === 'interactive') bridge?.sendStdin(data);
|
||||
},
|
||||
setDimensions: (dims) => bridge?.resize(dims.columns, dims.rows),
|
||||
};
|
||||
|
||||
const name = this.terminalName(session, mode);
|
||||
const terminal = vscode.window.createTerminal({ name, pty, iconPath: new vscode.ThemeIcon('comment-discussion') });
|
||||
this.terminals.set(session.id, terminal);
|
||||
terminal.show();
|
||||
}
|
||||
|
||||
private terminalName(session: SessionSummary, mode: 'interactive' | 'observer'): string {
|
||||
const base = session.title?.trim() || session.cwd.split('/').pop() || session.command;
|
||||
const prefix = mode === 'observer' ? '👁 ' : '';
|
||||
return `${prefix}${base}`;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const t of this.terminals.values()) t.dispose();
|
||||
this.terminals.clear();
|
||||
this.bridges.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user