feat(mobile): Add terminal powered by libghostty (#2056)
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
d686613e34
commit
da6ea88331
84 files changed
+10510
-66
No files matched your search
@@ -11,6 +11,7 @@ export * from "./gitActions.ts";
|
||||
export * from "./gitActionState.ts";
|
||||
export * from "./gitBranchState.ts";
|
||||
export * from "./gitStatusState.ts";
|
||||
export * from "./terminalSessionState.ts";
|
||||
export * from "./transportError.ts";
|
||||
export * from "./wsRpcProtocol.ts";
|
||||
export * from "./wsTransport.ts";
|
||||
|
||||
@@ -0,0 +1,276 @@
|
||||
import { AtomRegistry } from "effect/unstable/reactivity";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import type { TerminalSessionSnapshot } from "@t3tools/contracts";
|
||||
|
||||
import { createTerminalSessionManager } from "./terminalSessionState";
|
||||
|
||||
let atomRegistry = AtomRegistry.make();
|
||||
|
||||
function resetAtomRegistry() {
|
||||
atomRegistry.dispose();
|
||||
atomRegistry = AtomRegistry.make();
|
||||
}
|
||||
|
||||
const TARGET = {
|
||||
environmentId: "env-local",
|
||||
threadId: "thread-1",
|
||||
terminalId: "default",
|
||||
} as const;
|
||||
|
||||
const BASE_SNAPSHOT: TerminalSessionSnapshot = {
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
cwd: "/repo",
|
||||
worktreePath: null,
|
||||
status: "running",
|
||||
pid: 123,
|
||||
history: "hello",
|
||||
exitCode: null,
|
||||
exitSignal: null,
|
||||
updatedAt: "2026-04-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
describe("createTerminalSessionManager", () => {
|
||||
afterEach(() => {
|
||||
resetAtomRegistry();
|
||||
});
|
||||
|
||||
it("hydrates from started snapshots and appends output events", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "started",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: BASE_SNAPSHOT.updatedAt,
|
||||
snapshot: BASE_SNAPSHOT,
|
||||
});
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "output",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:01.000Z",
|
||||
data: " world",
|
||||
});
|
||||
|
||||
expect(manager.getSnapshot(TARGET)).toMatchObject({
|
||||
snapshot: BASE_SNAPSHOT,
|
||||
buffer: "hello world",
|
||||
status: "running",
|
||||
error: null,
|
||||
updatedAt: "2026-04-01T00:00:01.000Z",
|
||||
});
|
||||
});
|
||||
|
||||
it("caps retained output", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
maxBufferBytes: 5,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "output",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:01.000Z",
|
||||
data: "abcdef",
|
||||
});
|
||||
|
||||
expect(manager.getSnapshot(TARGET).buffer).toBe("bcdef");
|
||||
});
|
||||
|
||||
it("caps retained output by utf-8 byte length", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
maxBufferBytes: 4,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "output",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:01.000Z",
|
||||
data: "🙂🙂",
|
||||
});
|
||||
|
||||
expect(manager.getSnapshot(TARGET).buffer).toBe("🙂");
|
||||
});
|
||||
|
||||
it("invalidates one environment without clearing others", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
const otherTarget = {
|
||||
environmentId: "env-remote",
|
||||
threadId: "thread-1",
|
||||
terminalId: "default",
|
||||
} as const;
|
||||
|
||||
for (const target of [TARGET, otherTarget]) {
|
||||
manager.applyEvent(target, {
|
||||
type: "output",
|
||||
threadId: target.threadId,
|
||||
terminalId: target.terminalId,
|
||||
createdAt: "2026-04-01T00:00:01.000Z",
|
||||
data: target.environmentId,
|
||||
});
|
||||
}
|
||||
|
||||
manager.invalidateEnvironment(TARGET.environmentId);
|
||||
|
||||
expect(manager.getSnapshot(TARGET).buffer).toBe("");
|
||||
expect(manager.getSnapshot(otherTarget).buffer).toBe("env-remote");
|
||||
});
|
||||
|
||||
it("lists known sessions for a thread ordered by recency", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "started",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:00.000Z",
|
||||
snapshot: BASE_SNAPSHOT,
|
||||
});
|
||||
manager.applyEvent(
|
||||
{
|
||||
environmentId: TARGET.environmentId,
|
||||
},
|
||||
{
|
||||
type: "started",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: "term-2",
|
||||
createdAt: "2026-04-01T00:00:02.000Z",
|
||||
snapshot: {
|
||||
...BASE_SNAPSHOT,
|
||||
terminalId: "term-2",
|
||||
updatedAt: "2026-04-01T00:00:02.000Z",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(
|
||||
manager
|
||||
.listSessions({
|
||||
environmentId: TARGET.environmentId,
|
||||
threadId: TARGET.threadId,
|
||||
})
|
||||
.map((session) => session.target.terminalId),
|
||||
).toEqual(["term-2", "default"]);
|
||||
});
|
||||
|
||||
it("drops known sessions when an environment is invalidated", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "output",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:01.000Z",
|
||||
data: "hello",
|
||||
});
|
||||
|
||||
manager.invalidateEnvironment(TARGET.environmentId);
|
||||
|
||||
expect(
|
||||
manager.listSessions({
|
||||
environmentId: TARGET.environmentId,
|
||||
threadId: TARGET.threadId,
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("removes closed sessions from the known-session index while keeping local closed state", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "started",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: BASE_SNAPSHOT.updatedAt,
|
||||
snapshot: BASE_SNAPSHOT,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "closed",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:04.000Z",
|
||||
});
|
||||
|
||||
expect(
|
||||
manager.listSessions({
|
||||
environmentId: TARGET.environmentId,
|
||||
threadId: TARGET.threadId,
|
||||
}),
|
||||
).toEqual([]);
|
||||
expect(manager.getSnapshot(TARGET)).toMatchObject({
|
||||
buffer: "hello",
|
||||
status: "closed",
|
||||
snapshot: null,
|
||||
updatedAt: "2026-04-01T00:00:04.000Z",
|
||||
});
|
||||
});
|
||||
|
||||
it("clears locally retained closed state on reset", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "started",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: BASE_SNAPSHOT.updatedAt,
|
||||
snapshot: BASE_SNAPSHOT,
|
||||
});
|
||||
manager.applyEvent(TARGET, {
|
||||
type: "closed",
|
||||
threadId: TARGET.threadId,
|
||||
terminalId: TARGET.terminalId,
|
||||
createdAt: "2026-04-01T00:00:04.000Z",
|
||||
});
|
||||
|
||||
manager.reset();
|
||||
|
||||
expect(manager.getSnapshot(TARGET)).toEqual({
|
||||
snapshot: null,
|
||||
buffer: "",
|
||||
status: "closed",
|
||||
error: null,
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: null,
|
||||
version: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("syncs snapshots returned from open calls immediately", () => {
|
||||
const manager = createTerminalSessionManager({
|
||||
getRegistry: () => atomRegistry,
|
||||
});
|
||||
|
||||
manager.syncSnapshot(
|
||||
{ environmentId: TARGET.environmentId },
|
||||
{
|
||||
...BASE_SNAPSHOT,
|
||||
history: "prompt$ ",
|
||||
updatedAt: "2026-04-01T00:00:03.000Z",
|
||||
},
|
||||
);
|
||||
|
||||
expect(manager.getSnapshot(TARGET)).toMatchObject({
|
||||
buffer: "prompt$ ",
|
||||
status: "running",
|
||||
updatedAt: "2026-04-01T00:00:03.000Z",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,404 @@
|
||||
import type { TerminalEvent, TerminalSessionSnapshot } from "@t3tools/contracts";
|
||||
import { Atom, type AtomRegistry } from "effect/unstable/reactivity";
|
||||
|
||||
export interface TerminalSessionState {
|
||||
readonly snapshot: TerminalSessionSnapshot | null;
|
||||
readonly buffer: string;
|
||||
readonly status: TerminalSessionSnapshot["status"] | "closed";
|
||||
readonly error: string | null;
|
||||
readonly hasRunningSubprocess: boolean;
|
||||
readonly updatedAt: string | null;
|
||||
readonly version: number;
|
||||
}
|
||||
|
||||
export interface TerminalSessionTarget {
|
||||
readonly environmentId: string | null;
|
||||
readonly threadId: string | null;
|
||||
readonly terminalId: string | null;
|
||||
}
|
||||
|
||||
export interface KnownTerminalSessionTarget {
|
||||
readonly environmentId: string;
|
||||
readonly threadId: string;
|
||||
readonly terminalId: string;
|
||||
}
|
||||
|
||||
export interface KnownTerminalSession {
|
||||
readonly target: KnownTerminalSessionTarget;
|
||||
readonly state: TerminalSessionState;
|
||||
}
|
||||
|
||||
export interface TerminalSessionManagerConfig {
|
||||
readonly getRegistry: () => AtomRegistry.AtomRegistry;
|
||||
readonly maxBufferBytes?: number;
|
||||
}
|
||||
|
||||
export const EMPTY_TERMINAL_SESSION_STATE = Object.freeze<TerminalSessionState>({
|
||||
snapshot: null,
|
||||
buffer: "",
|
||||
status: "closed",
|
||||
error: null,
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: null,
|
||||
version: 0,
|
||||
});
|
||||
|
||||
const DEFAULT_MAX_BUFFER_BYTES = 512 * 1024;
|
||||
const knownTerminalSessionKeys = new Set<string>();
|
||||
const textEncoder = new TextEncoder();
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
export const terminalSessionStateAtom = Atom.family((key: string) => {
|
||||
knownTerminalSessionKeys.add(key);
|
||||
return Atom.make(EMPTY_TERMINAL_SESSION_STATE).pipe(
|
||||
Atom.keepAlive,
|
||||
Atom.withLabel(`terminal-session:${key}`),
|
||||
);
|
||||
});
|
||||
|
||||
export const EMPTY_TERMINAL_SESSION_ATOM = Atom.make(EMPTY_TERMINAL_SESSION_STATE).pipe(
|
||||
Atom.keepAlive,
|
||||
Atom.withLabel("terminal-session:null"),
|
||||
);
|
||||
|
||||
export const knownTerminalSessionsAtom = Atom.make<Record<string, KnownTerminalSessionTarget>>(
|
||||
{},
|
||||
).pipe(Atom.keepAlive, Atom.withLabel("terminal-session:index"));
|
||||
|
||||
export function getTerminalSessionTargetKey(target: TerminalSessionTarget): string | null {
|
||||
if (target.environmentId === null || target.threadId === null || target.terminalId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return `${target.environmentId}:${target.threadId}:${target.terminalId}`;
|
||||
}
|
||||
|
||||
function toKnownTarget(target: TerminalSessionTarget): KnownTerminalSessionTarget | null {
|
||||
const targetKey = getTerminalSessionTargetKey(target);
|
||||
if (targetKey === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const environmentId = target.environmentId;
|
||||
const threadId = target.threadId;
|
||||
const terminalId = target.terminalId;
|
||||
if (environmentId === null || threadId === null || terminalId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
environmentId,
|
||||
threadId,
|
||||
terminalId,
|
||||
};
|
||||
}
|
||||
|
||||
function trimBufferToBytes(buffer: string, maxBufferBytes: number): string {
|
||||
if (maxBufferBytes <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const encoded = textEncoder.encode(buffer);
|
||||
if (encoded.byteLength <= maxBufferBytes) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
let start = encoded.byteLength - maxBufferBytes;
|
||||
while (start < encoded.length) {
|
||||
const byte = encoded[start];
|
||||
if (byte === undefined || (byte & 0b1100_0000) !== 0b1000_0000) {
|
||||
break;
|
||||
}
|
||||
start += 1;
|
||||
}
|
||||
|
||||
return textDecoder.decode(encoded.subarray(start));
|
||||
}
|
||||
|
||||
function stateFromSnapshot(
|
||||
snapshot: TerminalSessionSnapshot,
|
||||
maxBufferBytes: number,
|
||||
): TerminalSessionState {
|
||||
return {
|
||||
snapshot,
|
||||
buffer: trimBufferToBytes(snapshot.history, maxBufferBytes),
|
||||
status: snapshot.status,
|
||||
error: null,
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: snapshot.updatedAt,
|
||||
version: 1,
|
||||
};
|
||||
}
|
||||
|
||||
export function createTerminalSessionManager(config: TerminalSessionManagerConfig) {
|
||||
const maxBufferBytes = config.maxBufferBytes ?? DEFAULT_MAX_BUFFER_BYTES;
|
||||
const closedSessionStates = new Map<string, TerminalSessionState>();
|
||||
|
||||
function clearKnownSessionAtomState(targetKey: string): void {
|
||||
knownTerminalSessionKeys.delete(targetKey);
|
||||
config.getRegistry().set(terminalSessionStateAtom(targetKey), EMPTY_TERMINAL_SESSION_STATE);
|
||||
}
|
||||
|
||||
function rememberTarget(target: TerminalSessionTarget): string | null {
|
||||
const targetKey = getTerminalSessionTargetKey(target);
|
||||
const knownTarget = toKnownTarget(target);
|
||||
if (targetKey === null || knownTarget === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
closedSessionStates.delete(targetKey);
|
||||
|
||||
const current = config.getRegistry().get(knownTerminalSessionsAtom);
|
||||
const existing = current[targetKey];
|
||||
if (
|
||||
existing?.environmentId === knownTarget.environmentId &&
|
||||
existing.threadId === knownTarget.threadId &&
|
||||
existing.terminalId === knownTarget.terminalId
|
||||
) {
|
||||
return targetKey;
|
||||
}
|
||||
|
||||
config.getRegistry().set(knownTerminalSessionsAtom, {
|
||||
...current,
|
||||
[targetKey]: knownTarget,
|
||||
});
|
||||
return targetKey;
|
||||
}
|
||||
|
||||
function removeTargets(
|
||||
match: (target: KnownTerminalSessionTarget) => boolean,
|
||||
): ReadonlyArray<string> {
|
||||
const current = config.getRegistry().get(knownTerminalSessionsAtom);
|
||||
const removedKeys: string[] = [];
|
||||
const next = Object.fromEntries(
|
||||
Object.entries(current).filter(([key, target]) => {
|
||||
const shouldRemove = match(target);
|
||||
if (shouldRemove) {
|
||||
removedKeys.push(key);
|
||||
}
|
||||
return !shouldRemove;
|
||||
}),
|
||||
) as Record<string, KnownTerminalSessionTarget>;
|
||||
if (Object.keys(next).length === Object.keys(current).length) {
|
||||
return removedKeys;
|
||||
}
|
||||
|
||||
config.getRegistry().set(knownTerminalSessionsAtom, next);
|
||||
return removedKeys;
|
||||
}
|
||||
|
||||
function getSnapshot(target: TerminalSessionTarget): TerminalSessionState {
|
||||
const targetKey = getTerminalSessionTargetKey(target);
|
||||
if (targetKey === null) {
|
||||
return EMPTY_TERMINAL_SESSION_STATE;
|
||||
}
|
||||
|
||||
const closedState = closedSessionStates.get(targetKey);
|
||||
if (closedState) {
|
||||
return closedState;
|
||||
}
|
||||
|
||||
const rememberedTargetKey = rememberTarget(target);
|
||||
if (rememberedTargetKey === null) {
|
||||
return EMPTY_TERMINAL_SESSION_STATE;
|
||||
}
|
||||
|
||||
return config.getRegistry().get(terminalSessionStateAtom(rememberedTargetKey));
|
||||
}
|
||||
|
||||
function setState(targetKey: string, nextState: TerminalSessionState): void {
|
||||
config.getRegistry().set(terminalSessionStateAtom(targetKey), nextState);
|
||||
}
|
||||
|
||||
function syncSnapshot(
|
||||
target: Pick<TerminalSessionTarget, "environmentId">,
|
||||
snapshot: TerminalSessionSnapshot,
|
||||
): void {
|
||||
const targetKey = rememberTarget({
|
||||
environmentId: target.environmentId,
|
||||
threadId: snapshot.threadId,
|
||||
terminalId: snapshot.terminalId,
|
||||
});
|
||||
if (targetKey === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(targetKey, stateFromSnapshot(snapshot, maxBufferBytes));
|
||||
}
|
||||
|
||||
function applyEvent(
|
||||
target: Pick<TerminalSessionTarget, "environmentId">,
|
||||
event: TerminalEvent,
|
||||
): void {
|
||||
const targetKey = rememberTarget({
|
||||
environmentId: target.environmentId,
|
||||
threadId: event.threadId,
|
||||
terminalId: event.terminalId,
|
||||
});
|
||||
if (targetKey === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const current = config.getRegistry().get(terminalSessionStateAtom(targetKey));
|
||||
switch (event.type) {
|
||||
case "started":
|
||||
case "restarted":
|
||||
setState(targetKey, stateFromSnapshot(event.snapshot, maxBufferBytes));
|
||||
return;
|
||||
case "output":
|
||||
setState(targetKey, {
|
||||
...current,
|
||||
buffer: trimBufferToBytes(`${current.buffer}${event.data}`, maxBufferBytes),
|
||||
status: current.status === "closed" ? "running" : current.status,
|
||||
error: null,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
return;
|
||||
case "cleared":
|
||||
setState(targetKey, {
|
||||
...current,
|
||||
buffer: "",
|
||||
error: null,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
return;
|
||||
case "exited":
|
||||
setState(targetKey, {
|
||||
...current,
|
||||
snapshot: current.snapshot
|
||||
? {
|
||||
...current.snapshot,
|
||||
status: "exited",
|
||||
exitCode: event.exitCode,
|
||||
exitSignal: event.exitSignal,
|
||||
updatedAt: event.createdAt,
|
||||
}
|
||||
: null,
|
||||
status: "exited",
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
return;
|
||||
case "closed":
|
||||
closedSessionStates.set(targetKey, {
|
||||
...current,
|
||||
snapshot: null,
|
||||
status: "closed",
|
||||
error: null,
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
clearKnownSessionAtomState(targetKey);
|
||||
removeTargets(
|
||||
(knownTarget) =>
|
||||
knownTarget.environmentId === target.environmentId &&
|
||||
knownTarget.threadId === event.threadId &&
|
||||
knownTarget.terminalId === event.terminalId,
|
||||
);
|
||||
return;
|
||||
case "error":
|
||||
setState(targetKey, {
|
||||
...current,
|
||||
status: "error",
|
||||
error: event.message,
|
||||
hasRunningSubprocess: false,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
return;
|
||||
case "activity":
|
||||
setState(targetKey, {
|
||||
...current,
|
||||
hasRunningSubprocess: event.hasRunningSubprocess,
|
||||
updatedAt: event.createdAt,
|
||||
version: current.version + 1,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function invalidate(target?: TerminalSessionTarget): void {
|
||||
if (target) {
|
||||
const targetKey = getTerminalSessionTargetKey(target);
|
||||
if (targetKey !== null) {
|
||||
closedSessionStates.delete(targetKey);
|
||||
setState(targetKey, EMPTY_TERMINAL_SESSION_STATE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (const key of knownTerminalSessionKeys) {
|
||||
setState(key, EMPTY_TERMINAL_SESSION_STATE);
|
||||
}
|
||||
closedSessionStates.clear();
|
||||
knownTerminalSessionKeys.clear();
|
||||
config.getRegistry().set(knownTerminalSessionsAtom, {});
|
||||
}
|
||||
|
||||
function invalidateEnvironment(environmentId: string): void {
|
||||
const prefix = `${environmentId}:`;
|
||||
for (const key of knownTerminalSessionKeys) {
|
||||
if (key.startsWith(prefix)) {
|
||||
setState(key, EMPTY_TERMINAL_SESSION_STATE);
|
||||
}
|
||||
}
|
||||
for (const key of closedSessionStates.keys()) {
|
||||
if (key.startsWith(prefix)) {
|
||||
closedSessionStates.delete(key);
|
||||
}
|
||||
}
|
||||
for (const key of removeTargets((target) => target.environmentId === environmentId)) {
|
||||
closedSessionStates.delete(key);
|
||||
clearKnownSessionAtomState(key);
|
||||
}
|
||||
}
|
||||
|
||||
function reset(): void {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
function listSessions(
|
||||
filter?: Partial<KnownTerminalSessionTarget>,
|
||||
): ReadonlyArray<KnownTerminalSession> {
|
||||
const knownTargets = Object.values(config.getRegistry().get(knownTerminalSessionsAtom));
|
||||
return knownTargets
|
||||
.filter((target) => {
|
||||
if (filter?.environmentId && target.environmentId !== filter.environmentId) {
|
||||
return false;
|
||||
}
|
||||
if (filter?.threadId && target.threadId !== filter.threadId) {
|
||||
return false;
|
||||
}
|
||||
if (filter?.terminalId && target.terminalId !== filter.terminalId) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.map((target) => ({
|
||||
target,
|
||||
state: getSnapshot(target),
|
||||
}))
|
||||
.sort((left, right) => {
|
||||
const leftUpdatedAt = left.state.updatedAt ? Date.parse(left.state.updatedAt) : 0;
|
||||
const rightUpdatedAt = right.state.updatedAt ? Date.parse(right.state.updatedAt) : 0;
|
||||
if (leftUpdatedAt !== rightUpdatedAt) {
|
||||
return rightUpdatedAt - leftUpdatedAt;
|
||||
}
|
||||
return left.target.terminalId.localeCompare(right.target.terminalId);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
applyEvent,
|
||||
getSnapshot,
|
||||
invalidate,
|
||||
invalidateEnvironment,
|
||||
listSessions,
|
||||
syncSnapshot,
|
||||
reset,
|
||||
};
|
||||
}
|
||||
@@ -197,6 +197,17 @@ describe("TerminalEvent", () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts closed events", () => {
|
||||
expect(
|
||||
decodes(TerminalEvent, {
|
||||
type: "closed",
|
||||
threadId: "thread-1",
|
||||
terminalId: DEFAULT_TERMINAL_ID,
|
||||
createdAt: new Date().toISOString(),
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts activity events", () => {
|
||||
expect(
|
||||
decodes(TerminalEvent, {
|
||||
|
||||
@@ -119,6 +119,11 @@ const TerminalExitedEvent = Schema.Struct({
|
||||
exitSignal: Schema.NullOr(Schema.Int),
|
||||
});
|
||||
|
||||
const TerminalClosedEvent = Schema.Struct({
|
||||
...TerminalEventBaseSchema.fields,
|
||||
type: Schema.Literal("closed"),
|
||||
});
|
||||
|
||||
const TerminalErrorEvent = Schema.Struct({
|
||||
...TerminalEventBaseSchema.fields,
|
||||
type: Schema.Literal("error"),
|
||||
@@ -146,6 +151,7 @@ export const TerminalEvent = Schema.Union([
|
||||
TerminalStartedEvent,
|
||||
TerminalOutputEvent,
|
||||
TerminalExitedEvent,
|
||||
TerminalClosedEvent,
|
||||
TerminalErrorEvent,
|
||||
TerminalClearedEvent,
|
||||
TerminalRestartedEvent,
|
||||
|
||||
Reference in new issue
Block a user