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:
173
packages/vscode/src/api/rest-client.ts
Normal file
173
packages/vscode/src/api/rest-client.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
// Client REST typé du daemon Arboretum — module pur (pas d'import vscode), testable.
|
||||
// Auth par en-tête `Authorization: Bearer <token>` (le hook preValidation du serveur l'accepte
|
||||
// au même titre que le cookie). Un client Node n'envoie pas d'en-tête Origin → il passe le check
|
||||
// Origin strict côté serveur. Réutilise les types de `@arboretum/shared`.
|
||||
import type {
|
||||
ApiError,
|
||||
CommitWorktreeRequest,
|
||||
CreateGroupSessionRequest,
|
||||
CreateSessionRequest,
|
||||
CreateWorktreeRequest,
|
||||
CreateWorktreeResponse,
|
||||
GroupSessionResponse,
|
||||
GroupsListResponse,
|
||||
MeResponse,
|
||||
PromoteWorktreeRequest,
|
||||
PushWorktreeRequest,
|
||||
RepoBranchesResponse,
|
||||
ReposListResponse,
|
||||
SessionResponse,
|
||||
SessionsListResponse,
|
||||
StartRepoSessionRequest,
|
||||
WorktreeResponse,
|
||||
WorktreesListResponse,
|
||||
} from '@arboretum/shared';
|
||||
import { normalizeBaseUrl } from '../config.js';
|
||||
|
||||
/** Erreur normalisée portant le code/HTTP renvoyés par le serveur. */
|
||||
export class RestError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: number,
|
||||
readonly code: string,
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'RestError';
|
||||
}
|
||||
}
|
||||
|
||||
type FetchLike = typeof fetch;
|
||||
|
||||
export class RestClient {
|
||||
private baseUrl: string;
|
||||
private token: string | null;
|
||||
|
||||
constructor(
|
||||
opts: { baseUrl: string; token?: string | null },
|
||||
private readonly fetchImpl: FetchLike = fetch,
|
||||
) {
|
||||
this.baseUrl = normalizeBaseUrl(opts.baseUrl);
|
||||
this.token = opts.token ?? null;
|
||||
}
|
||||
|
||||
setBaseUrl(url: string): void {
|
||||
this.baseUrl = normalizeBaseUrl(url);
|
||||
}
|
||||
|
||||
setToken(token: string | null): void {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
get url(): string {
|
||||
return this.baseUrl;
|
||||
}
|
||||
|
||||
// ---- auth ----
|
||||
/** Valide le token courant ; lève RestError(401) si invalide. */
|
||||
me(): Promise<MeResponse> {
|
||||
return this.request<MeResponse>('GET', '/api/v1/auth/me');
|
||||
}
|
||||
|
||||
// ---- repos & worktrees ----
|
||||
listRepos(): Promise<ReposListResponse> {
|
||||
return this.request<ReposListResponse>('GET', '/api/v1/repos');
|
||||
}
|
||||
|
||||
listWorktrees(): Promise<WorktreesListResponse> {
|
||||
return this.request<WorktreesListResponse>('GET', '/api/v1/worktrees');
|
||||
}
|
||||
|
||||
getBranches(repoId: string): Promise<RepoBranchesResponse> {
|
||||
return this.request<RepoBranchesResponse>('GET', `/api/v1/repos/${encodeURIComponent(repoId)}/branches`);
|
||||
}
|
||||
|
||||
createWorktree(repoId: string, body: CreateWorktreeRequest): Promise<CreateWorktreeResponse> {
|
||||
return this.request<CreateWorktreeResponse>('POST', `/api/v1/repos/${encodeURIComponent(repoId)}/worktrees`, body);
|
||||
}
|
||||
|
||||
commitWorktree(repoId: string, body: CommitWorktreeRequest): Promise<WorktreeResponse> {
|
||||
return this.request<WorktreeResponse>('POST', `/api/v1/repos/${encodeURIComponent(repoId)}/worktrees/commit`, body);
|
||||
}
|
||||
|
||||
pushWorktree(repoId: string, body: PushWorktreeRequest): Promise<WorktreeResponse> {
|
||||
return this.request<WorktreeResponse>('POST', `/api/v1/repos/${encodeURIComponent(repoId)}/worktrees/push`, body);
|
||||
}
|
||||
|
||||
promoteWorktree(repoId: string, body: PromoteWorktreeRequest): Promise<WorktreeResponse> {
|
||||
return this.request<WorktreeResponse>('POST', `/api/v1/repos/${encodeURIComponent(repoId)}/worktrees/promote`, body);
|
||||
}
|
||||
|
||||
startRepoSession(repoId: string, body: StartRepoSessionRequest): Promise<SessionResponse> {
|
||||
return this.request<SessionResponse>('POST', `/api/v1/repos/${encodeURIComponent(repoId)}/session`, body);
|
||||
}
|
||||
|
||||
// ---- sessions ----
|
||||
createSession(body: CreateSessionRequest): Promise<SessionResponse> {
|
||||
return this.request<SessionResponse>('POST', '/api/v1/sessions', body);
|
||||
}
|
||||
|
||||
listSessions(includeHidden = false): Promise<SessionsListResponse> {
|
||||
const q = includeHidden ? '?includeHidden=true' : '';
|
||||
return this.request<SessionsListResponse>('GET', `/api/v1/sessions${q}`);
|
||||
}
|
||||
|
||||
resumeSession(id: string): Promise<SessionResponse> {
|
||||
return this.request<SessionResponse>('POST', `/api/v1/sessions/${encodeURIComponent(id)}/resume`, {});
|
||||
}
|
||||
|
||||
forkSession(id: string): Promise<SessionResponse> {
|
||||
return this.request<SessionResponse>('POST', `/api/v1/sessions/${encodeURIComponent(id)}/fork`, {});
|
||||
}
|
||||
|
||||
hideSession(id: string): Promise<{ ok: true }> {
|
||||
return this.request<{ ok: true }>('POST', `/api/v1/sessions/${encodeURIComponent(id)}/hide`, {});
|
||||
}
|
||||
|
||||
killSession(id: string): Promise<{ ok: true }> {
|
||||
return this.request<{ ok: true }>('DELETE', `/api/v1/sessions/${encodeURIComponent(id)}`);
|
||||
}
|
||||
|
||||
// ---- groupes ----
|
||||
listGroups(): Promise<GroupsListResponse> {
|
||||
return this.request<GroupsListResponse>('GET', '/api/v1/groups');
|
||||
}
|
||||
|
||||
createGroupSession(groupId: string, body: CreateGroupSessionRequest): Promise<GroupSessionResponse> {
|
||||
return this.request<GroupSessionResponse>('POST', `/api/v1/groups/${encodeURIComponent(groupId)}/session`, body);
|
||||
}
|
||||
|
||||
// ---- interne ----
|
||||
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
const headers: Record<string, string> = { Accept: 'application/json' };
|
||||
if (this.token) headers.Authorization = `Bearer ${this.token}`;
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
||||
|
||||
const init: RequestInit = { method, headers };
|
||||
if (body !== undefined) init.body = JSON.stringify(body);
|
||||
|
||||
let res: Response;
|
||||
try {
|
||||
res = await this.fetchImpl(`${this.baseUrl}${path}`, init);
|
||||
} catch (err) {
|
||||
throw new RestError(`Cannot reach Arboretum at ${this.baseUrl}: ${(err as Error).message}`, 0, 'NETWORK');
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
let code = `HTTP_${res.status}`;
|
||||
let message = `${method} ${path} → ${res.status}`;
|
||||
try {
|
||||
const data = (await res.json()) as ApiError;
|
||||
if (data?.error) {
|
||||
code = data.error.code ?? code;
|
||||
message = data.error.message ?? message;
|
||||
}
|
||||
} catch {
|
||||
/* corps non-JSON : on garde le message générique */
|
||||
}
|
||||
throw new RestError(message, res.status, code);
|
||||
}
|
||||
|
||||
if (res.status === 204) return undefined as T;
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user