Add provider orchestrator and Codex adapter
- ProviderOrchestrator manages provider sessions and message dispatch - CodexProviderAdapter wraps Codex app-server integration - Refactor wsServer to use new provider abstraction Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
1 parent
4ce3c3bf8a
commit
dbbc225fe6
4 files changed
+667
-14
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
providerInterruptTurnInputSchema,
|
||||
providerRespondToRequestInputSchema,
|
||||
providerSendTurnInputSchema,
|
||||
providerSessionStartInputSchema,
|
||||
providerStopSessionInputSchema,
|
||||
} from "@t3tools/contracts";
|
||||
import type {
|
||||
ProviderEvent,
|
||||
ProviderInterruptTurnInput,
|
||||
ProviderRespondToRequestInput,
|
||||
ProviderSendTurnInput,
|
||||
ProviderSession,
|
||||
ProviderSessionStartInput,
|
||||
ProviderStopSessionInput,
|
||||
ProviderTurnStartResult,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { CodexAppServerManager } from "../../codexAppServerManager";
|
||||
import type { ProviderAdapter, ProviderThreadSnapshot } from "./providerAdapter";
|
||||
|
||||
export class CodexProviderAdapter implements ProviderAdapter {
|
||||
readonly kind = "codex" as const;
|
||||
private readonly manager = new CodexAppServerManager();
|
||||
|
||||
onEvent(listener: (event: ProviderEvent) => void): () => void {
|
||||
this.manager.on("event", listener);
|
||||
return () => {
|
||||
this.manager.off("event", listener);
|
||||
};
|
||||
}
|
||||
|
||||
hasSession(sessionId: string): boolean {
|
||||
return this.manager.hasSession(sessionId);
|
||||
}
|
||||
|
||||
startSession(input: ProviderSessionStartInput): Promise<ProviderSession> {
|
||||
const parsed = providerSessionStartInputSchema.parse(input);
|
||||
if (parsed.provider !== this.kind) {
|
||||
throw new Error(`Provider '${parsed.provider}' is not implemented by CodexProviderAdapter.`);
|
||||
}
|
||||
return this.manager.startSession(parsed);
|
||||
}
|
||||
|
||||
sendTurn(input: ProviderSendTurnInput): Promise<ProviderTurnStartResult> {
|
||||
return this.manager.sendTurn(providerSendTurnInputSchema.parse(input));
|
||||
}
|
||||
|
||||
interruptTurn(input: ProviderInterruptTurnInput): Promise<void> {
|
||||
const parsed = providerInterruptTurnInputSchema.parse(input);
|
||||
return this.manager.interruptTurn(parsed.sessionId, parsed.turnId);
|
||||
}
|
||||
|
||||
respondToRequest(input: ProviderRespondToRequestInput): Promise<void> {
|
||||
const parsed = providerRespondToRequestInputSchema.parse(input);
|
||||
return this.manager.respondToRequest(parsed.sessionId, parsed.requestId, parsed.decision);
|
||||
}
|
||||
|
||||
stopSession(input: ProviderStopSessionInput): void {
|
||||
const parsed = providerStopSessionInputSchema.parse(input);
|
||||
this.manager.stopSession(parsed.sessionId);
|
||||
}
|
||||
|
||||
listSessions(): ProviderSession[] {
|
||||
return this.manager.listSessions();
|
||||
}
|
||||
|
||||
async readThread(sessionId: string): Promise<ProviderThreadSnapshot> {
|
||||
const snapshot = await this.manager.readThread(sessionId);
|
||||
return {
|
||||
threadId: snapshot.threadId,
|
||||
turns: snapshot.turns.map((turn) => ({
|
||||
id: turn.id,
|
||||
items: turn.items,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
async rollbackThread(sessionId: string, numTurns: number): Promise<ProviderThreadSnapshot> {
|
||||
const snapshot = await this.manager.rollbackThread(sessionId, numTurns);
|
||||
return {
|
||||
threadId: snapshot.threadId,
|
||||
turns: snapshot.turns.map((turn) => ({
|
||||
id: turn.id,
|
||||
items: turn.items,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
stopAll(): void {
|
||||
this.manager.stopAll();
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.manager.stopAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import type {
|
||||
ProviderEvent,
|
||||
ProviderInterruptTurnInput,
|
||||
ProviderKind,
|
||||
ProviderRespondToRequestInput,
|
||||
ProviderSendTurnInput,
|
||||
ProviderSession,
|
||||
ProviderSessionStartInput,
|
||||
ProviderStopSessionInput,
|
||||
ProviderTurnStartResult,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export interface ProviderThreadTurnSnapshot {
|
||||
id: string;
|
||||
items: unknown[];
|
||||
}
|
||||
|
||||
export interface ProviderThreadSnapshot {
|
||||
threadId: string;
|
||||
turns: ProviderThreadTurnSnapshot[];
|
||||
}
|
||||
|
||||
export interface ProviderAdapter {
|
||||
readonly kind: ProviderKind;
|
||||
onEvent(listener: (event: ProviderEvent) => void): () => void;
|
||||
hasSession(sessionId: string): boolean;
|
||||
startSession(input: ProviderSessionStartInput): Promise<ProviderSession>;
|
||||
sendTurn(input: ProviderSendTurnInput): Promise<ProviderTurnStartResult>;
|
||||
interruptTurn(input: ProviderInterruptTurnInput): Promise<void>;
|
||||
respondToRequest(input: ProviderRespondToRequestInput): Promise<void>;
|
||||
stopSession(input: ProviderStopSessionInput): void;
|
||||
listSessions(): ProviderSession[];
|
||||
readThread(sessionId: string): Promise<ProviderThreadSnapshot>;
|
||||
rollbackThread(sessionId: string, numTurns: number): Promise<ProviderThreadSnapshot>;
|
||||
stopAll(): void;
|
||||
dispose(): void;
|
||||
}
|
||||
@@ -0,0 +1,518 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import type {
|
||||
ProviderCheckpoint,
|
||||
ProviderEvent,
|
||||
ProviderGetCheckpointDiffInput,
|
||||
ProviderGetCheckpointDiffResult,
|
||||
ProviderInterruptTurnInput,
|
||||
ProviderKind,
|
||||
ProviderListCheckpointsInput,
|
||||
ProviderListCheckpointsResult,
|
||||
ProviderRevertToCheckpointInput,
|
||||
ProviderRevertToCheckpointResult,
|
||||
ProviderRespondToRequestInput,
|
||||
ProviderSendTurnInput,
|
||||
ProviderSession,
|
||||
ProviderSessionStartInput,
|
||||
ProviderStopSessionInput,
|
||||
ProviderTurnStartResult,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
providerGetCheckpointDiffInputSchema,
|
||||
providerInterruptTurnInputSchema,
|
||||
providerListCheckpointsInputSchema,
|
||||
providerRevertToCheckpointInputSchema,
|
||||
providerRespondToRequestInputSchema,
|
||||
providerSendTurnInputSchema,
|
||||
providerSessionStartInputSchema,
|
||||
providerStopSessionInputSchema,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { FilesystemCheckpointStore } from "../../filesystemCheckpointStore";
|
||||
import type { ProviderAdapter, ProviderThreadTurnSnapshot } from "./providerAdapter";
|
||||
|
||||
export interface ProviderOrchestratorEvents {
|
||||
event: [event: ProviderEvent];
|
||||
}
|
||||
|
||||
export class ProviderOrchestrator extends EventEmitter<ProviderOrchestratorEvents> {
|
||||
private readonly adapters: Map<ProviderKind, ProviderAdapter>;
|
||||
private readonly filesystemCheckpointStore = new FilesystemCheckpointStore();
|
||||
private readonly sessionToProvider = new Map<string, ProviderKind>();
|
||||
private readonly sessionCheckpointCwds = new Map<string, string>();
|
||||
private readonly filesystemLocks = new Map<string, Promise<void>>();
|
||||
private readonly unsubscribers: Array<() => void>;
|
||||
|
||||
constructor(adapters: ProviderAdapter[]) {
|
||||
super();
|
||||
this.adapters = new Map(adapters.map((adapter) => [adapter.kind, adapter] as const));
|
||||
this.unsubscribers = adapters.map((adapter) =>
|
||||
adapter.onEvent((event) => {
|
||||
this.sessionToProvider.set(event.sessionId, event.provider);
|
||||
this.emit("event", event);
|
||||
this.maybeCaptureFilesystemCheckpoint(event);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async startSession(raw: ProviderSessionStartInput): Promise<ProviderSession> {
|
||||
const input = providerSessionStartInputSchema.parse(raw);
|
||||
const provider = input.provider ?? "codex";
|
||||
const adapter = this.requireAdapter(provider);
|
||||
const session = await adapter.startSession(input);
|
||||
this.sessionToProvider.set(session.sessionId, session.provider);
|
||||
await this.initializeFilesystemCheckpointing(adapter, session, input.cwd).catch((error) => {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Failed to initialize filesystem checkpoints.";
|
||||
this.emitFilesystemCheckpointError(session.sessionId, message, session.threadId);
|
||||
});
|
||||
return session;
|
||||
}
|
||||
|
||||
async sendTurn(raw: ProviderSendTurnInput): Promise<ProviderTurnStartResult> {
|
||||
const input = providerSendTurnInputSchema.parse(raw);
|
||||
return this.resolveAdapterForSession(input.sessionId).sendTurn(input);
|
||||
}
|
||||
|
||||
async interruptTurn(raw: ProviderInterruptTurnInput): Promise<void> {
|
||||
const input = providerInterruptTurnInputSchema.parse(raw);
|
||||
return this.resolveAdapterForSession(input.sessionId).interruptTurn(input);
|
||||
}
|
||||
|
||||
async respondToRequest(raw: ProviderRespondToRequestInput): Promise<void> {
|
||||
const input = providerRespondToRequestInputSchema.parse(raw);
|
||||
return this.resolveAdapterForSession(input.sessionId).respondToRequest(input);
|
||||
}
|
||||
|
||||
stopSession(raw: ProviderStopSessionInput): void {
|
||||
const input = providerStopSessionInputSchema.parse(raw);
|
||||
this.resolveAdapterForSession(input.sessionId).stopSession(input);
|
||||
this.sessionToProvider.delete(input.sessionId);
|
||||
this.sessionCheckpointCwds.delete(input.sessionId);
|
||||
this.filesystemLocks.delete(input.sessionId);
|
||||
}
|
||||
|
||||
listSessions(): ProviderSession[] {
|
||||
const sessions = [...this.adapters.values()].flatMap((adapter) => adapter.listSessions());
|
||||
for (const session of sessions) {
|
||||
this.sessionToProvider.set(session.sessionId, session.provider);
|
||||
}
|
||||
return sessions;
|
||||
}
|
||||
|
||||
async listCheckpoints(raw: ProviderListCheckpointsInput): Promise<ProviderListCheckpointsResult> {
|
||||
const input = providerListCheckpointsInputSchema.parse(raw);
|
||||
const adapter = this.resolveAdapterForSession(input.sessionId);
|
||||
const snapshot = await adapter.readThread(input.sessionId);
|
||||
return {
|
||||
threadId: snapshot.threadId,
|
||||
checkpoints: buildCheckpoints(snapshot.turns),
|
||||
};
|
||||
}
|
||||
|
||||
async getCheckpointDiff(
|
||||
raw: ProviderGetCheckpointDiffInput,
|
||||
): Promise<ProviderGetCheckpointDiffResult> {
|
||||
const input = providerGetCheckpointDiffInputSchema.parse(raw);
|
||||
const adapter = this.resolveAdapterForSession(input.sessionId);
|
||||
const checkpointCwd = await this.getOrInitializeFilesystemCheckpointCwd(input.sessionId, adapter);
|
||||
if (!checkpointCwd) {
|
||||
throw new Error("Filesystem checkpoints are unavailable for this session.");
|
||||
}
|
||||
|
||||
return this.withFilesystemLock(input.sessionId, async () => {
|
||||
const snapshot = await adapter.readThread(input.sessionId);
|
||||
if (input.toTurnCount > snapshot.turns.length) {
|
||||
throw new Error(
|
||||
`Checkpoint turn count ${input.toTurnCount} exceeds current turn count ${snapshot.turns.length}.`,
|
||||
);
|
||||
}
|
||||
|
||||
const diff = await this.filesystemCheckpointStore.diffCheckpoints({
|
||||
cwd: checkpointCwd,
|
||||
threadId: snapshot.threadId,
|
||||
fromTurnCount: input.fromTurnCount,
|
||||
toTurnCount: input.toTurnCount,
|
||||
});
|
||||
return {
|
||||
threadId: snapshot.threadId,
|
||||
fromTurnCount: input.fromTurnCount,
|
||||
toTurnCount: input.toTurnCount,
|
||||
diff,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async revertToCheckpoint(
|
||||
raw: ProviderRevertToCheckpointInput,
|
||||
): Promise<ProviderRevertToCheckpointResult> {
|
||||
const input = providerRevertToCheckpointInputSchema.parse(raw);
|
||||
const adapter = this.resolveAdapterForSession(input.sessionId);
|
||||
const checkpointCwd = await this.getOrInitializeFilesystemCheckpointCwd(input.sessionId, adapter);
|
||||
if (!checkpointCwd) {
|
||||
throw new Error("Filesystem checkpoints are unavailable for this session.");
|
||||
}
|
||||
|
||||
return this.withFilesystemLock(input.sessionId, async () => {
|
||||
const beforeSnapshot = await adapter.readThread(input.sessionId);
|
||||
const currentTurnCount = beforeSnapshot.turns.length;
|
||||
if (input.turnCount > currentTurnCount) {
|
||||
throw new Error(
|
||||
`Checkpoint turn count ${input.turnCount} exceeds current turn count ${currentTurnCount}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (input.turnCount > 0) {
|
||||
const hasCheckpoint = await this.filesystemCheckpointStore.hasCheckpoint({
|
||||
cwd: checkpointCwd,
|
||||
threadId: beforeSnapshot.threadId,
|
||||
turnCount: input.turnCount,
|
||||
});
|
||||
if (!hasCheckpoint) {
|
||||
throw new Error(
|
||||
`Filesystem checkpoint is unavailable for turn ${input.turnCount} in thread ${beforeSnapshot.threadId}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const restored = await this.filesystemCheckpointStore.restoreCheckpoint({
|
||||
cwd: checkpointCwd,
|
||||
threadId: beforeSnapshot.threadId,
|
||||
turnCount: input.turnCount,
|
||||
});
|
||||
if (!restored) {
|
||||
throw new Error(
|
||||
`Filesystem checkpoint is unavailable for turn ${input.turnCount} in thread ${beforeSnapshot.threadId}.`,
|
||||
);
|
||||
}
|
||||
|
||||
const rollbackTurns = currentTurnCount - input.turnCount;
|
||||
const afterSnapshot =
|
||||
rollbackTurns > 0
|
||||
? await adapter.rollbackThread(input.sessionId, rollbackTurns)
|
||||
: beforeSnapshot;
|
||||
|
||||
await this.filesystemCheckpointStore.pruneAfterTurn({
|
||||
cwd: checkpointCwd,
|
||||
threadId: afterSnapshot.threadId,
|
||||
maxTurnCount: afterSnapshot.turns.length,
|
||||
});
|
||||
|
||||
const checkpoints = buildCheckpoints(afterSnapshot.turns);
|
||||
const currentCheckpoint =
|
||||
checkpoints.find((checkpoint) => checkpoint.isCurrent) ??
|
||||
checkpoints[checkpoints.length - 1] ??
|
||||
checkpoints[0];
|
||||
const rolledBackTurns = Math.max(0, currentTurnCount - afterSnapshot.turns.length);
|
||||
|
||||
return {
|
||||
threadId: afterSnapshot.threadId,
|
||||
turnCount: currentCheckpoint?.turnCount ?? 0,
|
||||
messageCount: currentCheckpoint?.messageCount ?? 0,
|
||||
rolledBackTurns,
|
||||
checkpoints,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
stopAll(): void {
|
||||
for (const adapter of this.adapters.values()) {
|
||||
adapter.stopAll();
|
||||
}
|
||||
this.sessionToProvider.clear();
|
||||
this.sessionCheckpointCwds.clear();
|
||||
this.filesystemLocks.clear();
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const unsubscribe of this.unsubscribers) {
|
||||
unsubscribe();
|
||||
}
|
||||
for (const adapter of this.adapters.values()) {
|
||||
adapter.dispose();
|
||||
}
|
||||
this.unsubscribers.length = 0;
|
||||
this.sessionToProvider.clear();
|
||||
this.sessionCheckpointCwds.clear();
|
||||
this.filesystemLocks.clear();
|
||||
}
|
||||
|
||||
private async initializeFilesystemCheckpointing(
|
||||
adapter: ProviderAdapter,
|
||||
session: ProviderSession,
|
||||
preferredCwd?: string,
|
||||
): Promise<void> {
|
||||
const cwd = preferredCwd ?? session.cwd ?? process.cwd();
|
||||
await this.withFilesystemLock(session.sessionId, async () => {
|
||||
const supportsGit = await this.filesystemCheckpointStore.isGitRepository(cwd);
|
||||
if (!supportsGit) {
|
||||
this.sessionCheckpointCwds.delete(session.sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = await adapter.readThread(session.sessionId);
|
||||
await this.filesystemCheckpointStore.ensureRootCheckpoint({
|
||||
cwd,
|
||||
threadId: snapshot.threadId,
|
||||
});
|
||||
await this.filesystemCheckpointStore.captureCheckpoint({
|
||||
cwd,
|
||||
threadId: snapshot.threadId,
|
||||
turnCount: snapshot.turns.length,
|
||||
});
|
||||
this.sessionCheckpointCwds.set(session.sessionId, cwd);
|
||||
});
|
||||
}
|
||||
|
||||
private async getOrInitializeFilesystemCheckpointCwd(
|
||||
sessionId: string,
|
||||
adapter: ProviderAdapter,
|
||||
): Promise<string | null> {
|
||||
const existingCwd = this.sessionCheckpointCwds.get(sessionId);
|
||||
if (existingCwd) return existingCwd;
|
||||
|
||||
const session = adapter.listSessions().find((entry) => entry.sessionId === sessionId);
|
||||
const candidateCwds = session?.cwd ? [session.cwd] : [process.cwd()];
|
||||
|
||||
await this.withFilesystemLock(sessionId, async () => {
|
||||
const currentCwd = this.sessionCheckpointCwds.get(sessionId);
|
||||
if (currentCwd) return;
|
||||
|
||||
const cwdSupport = await Promise.all(
|
||||
candidateCwds.map(async (cwd) => ({
|
||||
cwd,
|
||||
supportsGit: await this.filesystemCheckpointStore.isGitRepository(cwd),
|
||||
})),
|
||||
);
|
||||
const supportedCwd = cwdSupport.find((entry) => entry.supportsGit)?.cwd;
|
||||
if (!supportedCwd) {
|
||||
this.sessionCheckpointCwds.delete(sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = await adapter.readThread(sessionId);
|
||||
await this.filesystemCheckpointStore.ensureRootCheckpoint({
|
||||
cwd: supportedCwd,
|
||||
threadId: snapshot.threadId,
|
||||
});
|
||||
await this.filesystemCheckpointStore.captureCheckpoint({
|
||||
cwd: supportedCwd,
|
||||
threadId: snapshot.threadId,
|
||||
turnCount: snapshot.turns.length,
|
||||
});
|
||||
this.sessionCheckpointCwds.set(sessionId, supportedCwd);
|
||||
});
|
||||
|
||||
return this.sessionCheckpointCwds.get(sessionId) ?? null;
|
||||
}
|
||||
|
||||
private maybeCaptureFilesystemCheckpoint(event: ProviderEvent): void {
|
||||
if (event.kind !== "notification" || event.method !== "turn/completed") {
|
||||
return;
|
||||
}
|
||||
const adapter = this.resolveAdapterForSession(event.sessionId);
|
||||
const checkpointCwd = this.sessionCheckpointCwds.get(event.sessionId);
|
||||
if (!checkpointCwd) {
|
||||
void this.getOrInitializeFilesystemCheckpointCwd(event.sessionId, adapter)
|
||||
.then(async (initializedCwd) => {
|
||||
if (!initializedCwd) return;
|
||||
const snapshot = await adapter.readThread(event.sessionId);
|
||||
this.emitCheckpointCaptured(event.sessionId, snapshot.threadId, snapshot.turns.length);
|
||||
})
|
||||
.catch((error) => {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Failed to initialize filesystem checkpoints.";
|
||||
this.emitFilesystemCheckpointError(event.sessionId, message, event.threadId);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
void this.withFilesystemLock(event.sessionId, async () => {
|
||||
const snapshot = await adapter.readThread(event.sessionId);
|
||||
await this.filesystemCheckpointStore.captureCheckpoint({
|
||||
cwd: checkpointCwd,
|
||||
threadId: snapshot.threadId,
|
||||
turnCount: snapshot.turns.length,
|
||||
});
|
||||
this.emitCheckpointCaptured(event.sessionId, snapshot.threadId, snapshot.turns.length);
|
||||
}).catch((error) => {
|
||||
const message = error instanceof Error ? error.message : "Failed to capture checkpoint.";
|
||||
this.emitFilesystemCheckpointError(event.sessionId, message, event.threadId);
|
||||
});
|
||||
}
|
||||
|
||||
private emitCheckpointCaptured(sessionId: string, threadId: string, turnCount: number): void {
|
||||
this.emit("event", {
|
||||
id: randomUUID(),
|
||||
kind: "notification",
|
||||
provider: this.sessionToProvider.get(sessionId) ?? "codex",
|
||||
sessionId,
|
||||
createdAt: new Date().toISOString(),
|
||||
method: "checkpoint/captured",
|
||||
threadId,
|
||||
payload: {
|
||||
threadId,
|
||||
turnCount,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private emitFilesystemCheckpointError(
|
||||
sessionId: string,
|
||||
message: string,
|
||||
threadId?: string,
|
||||
): void {
|
||||
this.emit("event", {
|
||||
id: randomUUID(),
|
||||
kind: "error",
|
||||
provider: this.sessionToProvider.get(sessionId) ?? "codex",
|
||||
sessionId,
|
||||
createdAt: new Date().toISOString(),
|
||||
method: "checkpoint/filesystemError",
|
||||
message,
|
||||
...(threadId ? { threadId } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
private withFilesystemLock<T>(sessionId: string, fn: () => Promise<T>): Promise<T> {
|
||||
const previous = this.filesystemLocks.get(sessionId) ?? Promise.resolve();
|
||||
const next = previous.catch(() => undefined).then(fn);
|
||||
const completion = next.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
this.filesystemLocks.set(sessionId, completion);
|
||||
return next.finally(() => {
|
||||
const tracked = this.filesystemLocks.get(sessionId);
|
||||
if (tracked === completion) {
|
||||
this.filesystemLocks.delete(sessionId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private resolveAdapterForSession(sessionId: string): ProviderAdapter {
|
||||
const mappedProvider = this.sessionToProvider.get(sessionId);
|
||||
if (mappedProvider) {
|
||||
return this.requireAdapter(mappedProvider);
|
||||
}
|
||||
for (const adapter of this.adapters.values()) {
|
||||
if (adapter.hasSession(sessionId)) {
|
||||
this.sessionToProvider.set(sessionId, adapter.kind);
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
throw new Error(`Unknown provider session: ${sessionId}`);
|
||||
}
|
||||
|
||||
private requireAdapter(provider: ProviderKind): ProviderAdapter {
|
||||
const adapter = this.adapters.get(provider);
|
||||
if (!adapter) {
|
||||
throw new Error(`Provider '${provider}' is not implemented yet.`);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
}
|
||||
|
||||
function asObject(value: unknown): Record<string, unknown> | undefined {
|
||||
if (!value || typeof value !== "object") return undefined;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function asString(value: unknown): string | undefined {
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
function asArray(value: unknown): unknown[] {
|
||||
return Array.isArray(value) ? value : [];
|
||||
}
|
||||
|
||||
function trimToPreview(value: string): string {
|
||||
const normalized = value.replace(/\s+/g, " ").trim();
|
||||
if (normalized.length <= 120) return normalized;
|
||||
return `${normalized.slice(0, 117)}...`;
|
||||
}
|
||||
|
||||
function summarizeUserMessageContent(content: unknown[]): string | undefined {
|
||||
const segments: string[] = [];
|
||||
for (const part of content) {
|
||||
const record = asObject(part);
|
||||
const type = asString(record?.type);
|
||||
if (!type) continue;
|
||||
if (type === "text") {
|
||||
const text = asString(record?.text);
|
||||
if (text && text.trim().length > 0) {
|
||||
segments.push(text.trim());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (type === "image") {
|
||||
segments.push("[Image attachment]");
|
||||
continue;
|
||||
}
|
||||
if (type === "localImage") {
|
||||
segments.push("[Local image attachment]");
|
||||
}
|
||||
}
|
||||
if (segments.length === 0) return undefined;
|
||||
return trimToPreview(segments.join(" "));
|
||||
}
|
||||
|
||||
function summarizeTurn(turn: ProviderThreadTurnSnapshot): { messageCountDelta: number; preview?: string } {
|
||||
let messageCountDelta = 0;
|
||||
let preview: string | undefined;
|
||||
for (const item of turn.items) {
|
||||
const record = asObject(item);
|
||||
const type = asString(record?.type);
|
||||
if (!type) continue;
|
||||
if (type === "userMessage") {
|
||||
messageCountDelta += 1;
|
||||
if (!preview) {
|
||||
preview = summarizeUserMessageContent(asArray(record?.content));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (type === "agentMessage") {
|
||||
messageCountDelta += 1;
|
||||
if (!preview) {
|
||||
const text = asString(record?.text);
|
||||
if (text && text.trim().length > 0) {
|
||||
preview = trimToPreview(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
messageCountDelta,
|
||||
...(preview ? { preview } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function buildCheckpoints(turns: ProviderThreadTurnSnapshot[]): ProviderCheckpoint[] {
|
||||
const checkpoints: ProviderCheckpoint[] = [];
|
||||
let messageCount = 0;
|
||||
const isEmpty = turns.length === 0;
|
||||
checkpoints.push({
|
||||
id: "root",
|
||||
turnCount: 0,
|
||||
messageCount: 0,
|
||||
label: "Start of conversation",
|
||||
isCurrent: isEmpty,
|
||||
});
|
||||
for (let index = 0; index < turns.length; index += 1) {
|
||||
const turn = turns[index];
|
||||
if (!turn) continue;
|
||||
const turnSummary = summarizeTurn(turn);
|
||||
messageCount += turnSummary.messageCountDelta;
|
||||
checkpoints.push({
|
||||
id: turn.id,
|
||||
turnCount: index + 1,
|
||||
messageCount,
|
||||
label: `Turn ${index + 1}`,
|
||||
...(turnSummary.preview ? { preview: turnSummary.preview } : {}),
|
||||
isCurrent: index === turns.length - 1,
|
||||
});
|
||||
}
|
||||
return checkpoints;
|
||||
}
|
||||
+15
-14
@@ -24,7 +24,6 @@ import { WebSocketServer, type WebSocket } from "ws";
|
||||
|
||||
import { createLogger } from "./logger";
|
||||
import { ProjectRegistry } from "./projectRegistry";
|
||||
import { ProviderManager } from "./providerManager";
|
||||
import { GitManager } from "./gitManager";
|
||||
import {
|
||||
checkoutGitBranch,
|
||||
@@ -40,6 +39,8 @@ import { loadResolvedKeybindingsConfig, upsertKeybindingRule } from "./keybindin
|
||||
import { searchWorkspaceEntries } from "./workspaceEntries";
|
||||
import { CoreEngine } from "./effect/coreEngine";
|
||||
import { SqliteEventStore } from "./effect/eventStore";
|
||||
import { CodexProviderAdapter } from "./effect/providers/codexProviderAdapter";
|
||||
import { ProviderOrchestrator } from "./effect/providers/providerOrchestrator";
|
||||
|
||||
const MIME_TYPES: Record<string, string> = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
@@ -92,7 +93,7 @@ export function createServer(options: ServerOptions) {
|
||||
terminalManager: providedTerminalManager,
|
||||
authToken,
|
||||
} = options;
|
||||
const providerManager = new ProviderManager();
|
||||
const providerOrchestrator = new ProviderOrchestrator([new CodexProviderAdapter()]);
|
||||
const terminalManager = providedTerminalManager ?? new TerminalManager();
|
||||
const projectRegistry =
|
||||
providedRegistry ?? new ProjectRegistry(path.join(os.homedir(), ".t3", "userdata"));
|
||||
@@ -143,7 +144,7 @@ export function createServer(options: ServerOptions) {
|
||||
}
|
||||
|
||||
// Forward provider events to all connected WebSocket clients
|
||||
providerManager.on("event", (event) => {
|
||||
providerOrchestrator.on("event", (event) => {
|
||||
const push: WsPush = {
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.providerEvent,
|
||||
@@ -336,33 +337,33 @@ export function createServer(options: ServerOptions) {
|
||||
async function routeRequest(request: WsRequest): Promise<unknown> {
|
||||
switch (request.method) {
|
||||
case WS_METHODS.providersStartSession:
|
||||
return providerManager.startSession(request.params as never);
|
||||
return providerOrchestrator.startSession(request.params as never);
|
||||
|
||||
case WS_METHODS.providersSendTurn:
|
||||
return providerManager.sendTurn(request.params as never);
|
||||
return providerOrchestrator.sendTurn(request.params as never);
|
||||
|
||||
case WS_METHODS.providersInterruptTurn:
|
||||
return providerManager.interruptTurn(request.params as never);
|
||||
return providerOrchestrator.interruptTurn(request.params as never);
|
||||
|
||||
case WS_METHODS.providersRespondToRequest:
|
||||
return providerManager.respondToRequest(request.params as never);
|
||||
return providerOrchestrator.respondToRequest(request.params as never);
|
||||
|
||||
case WS_METHODS.providersStopSession: {
|
||||
providerManager.stopSession(request.params as never);
|
||||
providerOrchestrator.stopSession(request.params as never);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
case WS_METHODS.providersListSessions:
|
||||
return providerManager.listSessions();
|
||||
return providerOrchestrator.listSessions();
|
||||
|
||||
case WS_METHODS.providersListCheckpoints:
|
||||
return providerManager.listCheckpoints(request.params as never);
|
||||
return providerOrchestrator.listCheckpoints(request.params as never);
|
||||
|
||||
case WS_METHODS.providersGetCheckpointDiff:
|
||||
return providerManager.getCheckpointDiff(request.params as never);
|
||||
return providerOrchestrator.getCheckpointDiff(request.params as never);
|
||||
|
||||
case WS_METHODS.providersRevertToCheckpoint:
|
||||
return providerManager.revertToCheckpoint(request.params as never);
|
||||
return providerOrchestrator.revertToCheckpoint(request.params as never);
|
||||
|
||||
case WS_METHODS.projectsList:
|
||||
return projectRegistry.list();
|
||||
@@ -651,8 +652,8 @@ export function createServer(options: ServerOptions) {
|
||||
unsubscribeCoreDelta = null;
|
||||
}
|
||||
terminalManager.off("event", onTerminalEvent);
|
||||
providerManager.stopAll();
|
||||
providerManager.dispose();
|
||||
providerOrchestrator.stopAll();
|
||||
providerOrchestrator.dispose();
|
||||
terminalManager.dispose();
|
||||
await coreEngine.stop();
|
||||
eventStore.close();
|
||||
|
||||
Reference in new issue
Block a user