wip: Centralize all harnesses with single websocket server #581
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromfeature/feat/harness-ws-serveropened View on GitHub- @@ -0,0 +1,21 @@1+import type {2+ EventId,3+ HarnessCapabilitySet,4+ HarnessEvent,5+ HarnessNativeFrame,6+ HarnessProfile,7+ HarnessSession,8+} from "@t3tools/contracts";9+import {10+ EventId as EventIdSchema,11+ HarnessSessionId,12+ RuntimeRequestId,13+} from "@t3tools/contracts";14+import {15+ type HarnessAdapter,16+ HarnessAdapterError,17+ assertHarness,18+ assertSessionHarness,19+} from "./adapters";20+21+export const OPENCODE_HARNESS_CAPABILITIES: HarnessCapabilitySet = {@@ -0,0 +351,21 @@351+ #nextSequence(): number {352+ return this.#store.getSnapshot().sequence + 1;353+ }354+355+ #startStreaming(sessionKey: string): void {356+ const liveSession = this.#liveSessions.get(sessionKey);357+ if (!liveSession || !liveSession.adapter.streamEvents) {358+ return;359+ }360+ liveSession.streamAbort?.abort();361+ const controller = new AbortController();362+ liveSession.streamAbort = controller;363+ void (async () => {364+ try {365+ for await (const event of liveSession.adapter.streamEvents!({366+ session: liveSession.session,367+ signal: controller.signal,368+ })) {369+ this.#publish(event);370+ }371+ } catch (error) {@@ -0,0 +249,21 @@249+ type: "session.state.changed",250+ payload: {251+ state: statusType === "busy" ? "running" : statusType === "retry" ? "waiting" : "ready",252+ },253+ };254+ continue;255+ }256+257+ yield {258+ ...base,259+ type: "native.frame",260+ payload: {261+ source: event.type,262+ payload: event.properties,263+ },264+ };265+ }266+ }267+}268+269+function toHarnessSession(profile: HarnessProfile, session: OpenCodeSessionInfo): HarnessSession {@@ -0,0 +138,21 @@138+ id: HarnessProfileId,139+ name: TrimmedNonEmptyString,140+ harness: HarnessKind,141+ adapterFamily: HarnessAdapterFamily,142+ connectionMode: HarnessConnectionMode,143+ enabled: Schema.Boolean,144+ config: HarnessProfileConfig,145+ createdAt: IsoDateTime,146+ updatedAt: IsoDateTime,147+});148+export type HarnessProfile = typeof HarnessProfile.Type;149+150+export const HarnessBinding = Schema.Struct({151+ sessionId: HarnessSessionId,152+ profileId: Schema.NullOr(HarnessProfileId),153+ harness: HarnessKind,154+ adapterKey: TrimmedNonEmptyString,155+ connectionMode: HarnessConnectionMode,156+ nativeSessionId: Schema.optional(TrimmedNonEmptyString),157+ nativeThreadId: Schema.optional(ThreadId),158+ nativeTurnId: Schema.optional(TurnId),@@ -0,0 +249,21 @@249+ type: "session.state.changed",250+ payload: {251+ state: statusType === "busy" ? "running" : statusType === "retry" ? "waiting" : "ready",252+ },253+ };254+ continue;255+ }256+257+ yield {258+ ...base,259+ type: "native.frame",260+ payload: {261+ source: event.type,262+ payload: event.properties,263+ },264+ };265+ }266+ }267+}268+269+function toHarnessSession(profile: HarnessProfile, session: OpenCodeSessionInfo): HarnessSession {@@ -0,0 +288,21 @@288+ health: event.type === "connector.connected" ? "connected" : "disconnected",289+ ...(event.payload.description ? { description: event.payload.description } : {}),290+ ...(event.payload.version ? { version: event.payload.version } : {}),291+ lastSeenAt: event.createdAt,292+ ...(event.payload.metadata ? { metadata: event.payload.metadata } : {}),293+ }),294+ };295+296+ case "connector.health.changed":297+ return {298+ ...nextBase,299+ connectors: applyConnector(nextBase.connectors, {300+ id: event.payload.connectorId,301+ profileId: null,302+ harness: event.harness,303+ adapterKey: event.adapterKey,304+ health: event.payload.health,305+ ...(event.payload.detail ? { description: event.payload.detail } : {}),306+ lastSeenAt: event.createdAt,307+ }),308+ };@@ -0,0 +18,21 @@18+ type HarnessSendTurnInput,19+ type HarnessShutdownSessionInput,20+ type HarnessUpdateSessionConfigInput,21+ assertHarness,22+} from "./adapters";23+24+export const CLAUDE_HARNESS_CAPABILITIES: HarnessCapabilitySet = {25+ resume: true,26+ cancel: true,27+ modelSwitch: "restart-required",28+ permissions: true,29+ elicitation: true,30+ toolLifecycle: true,31+ reasoningStream: true,32+ planStream: true,33+ fileArtifacts: true,34+ checkpoints: false,35+ subagents: true,36+};37+38+export type ClaudeAgentSdkEvent = Omit<HarnessEvent, "harness" | "adapterKey" | "connectionMode">;@@ -0,0 +267,21 @@267+ });268+ }269+270+ async updateSessionConfig(271+ input: HarnessUpdateSessionConfigInput & { readonly sessionId: HarnessSessionId },272+ ): Promise<void> {273+ const liveSession = await this.#resolveLiveSession(input.sessionId);274+ await liveSession.adapter.updateSessionConfig({275+ session: liveSession.session,276+ ...(input.title !== undefined ? { title: input.title } : {}),277+ ...(input.model !== undefined ? { model: input.model } : {}),278+ ...(input.mode !== undefined ? { mode: input.mode } : {}),279+ });280+ }281+282+ async shutdownSession(sessionId: HarnessSessionId): Promise<void> {283+ const liveSession = await this.#resolveLiveSession(sessionId);284+ liveSession.streamAbort?.abort();285+ await liveSession.adapter.shutdownSession({ session: liveSession.session });286+ this.#liveSessions.delete(asSessionKey(sessionId));287+ }@@ -0,0 +202,21 @@202+ profile,203+ ...(title !== undefined ? { title } : {}),204+ });205+ this.#registerLiveSession(profile, adapter, session);206+ this.#publish(buildSessionCreatedEvent(session, this.#nextSequence(), this.#now()));207+ const bindingEvent = buildSessionBoundEvent(session, profile, this.#nextSequence(), this.#now());208+ if (bindingEvent) {209+ this.#publish(bindingEvent);210+ }211+ this.#startStreaming(asSessionKey(session.id));212+ return this.#requireSession(session.id);213+ }214+215+ async attachSession(profileId: HarnessProfile["id"]): Promise<HarnessSession> {216+ const snapshot = this.#store.getSnapshot();217+ const profile = findProfile(snapshot, profileId);218+ const adapter = this.#resolveAdapter(profile.harness);219+ const session = buildAttachedSession(profile, adapter, this.#now());220+ this.#registerLiveSession(profile, adapter, session);221+ this.#publish(buildSessionCreatedEvent(session, this.#nextSequence(), session.createdAt));222+ return this.#requireSession(session.id);
juliusmarminge reviewed
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
31e45a33-17c1-413b-b945-599c50a6e78bYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands and usage tips.