import * as plugins from './plugins.js'; import type { IOpenCodeConnectionConfig } from './interfaces.opencode.js'; export class SessionIntelligenceReader { private readonly registry: plugins.crossharness.ConnectionRegistry; private connectionId?: string; constructor(private readonly config: IOpenCodeConnectionConfig) { this.registry = new plugins.crossharness.ConnectionRegistry({ allowedServerUrls: [config.baseUrl], maxConnections: 1, openCodeCredentials: { serverUrl: config.baseUrl, username: config.username, password: config.password, }, }); } public async init(signalArg?: AbortSignal): Promise { if (this.connectionId) throw new Error('The Session Intelligence reader is already initialized.'); const connection = await this.registry.connect( 'opencode', this.config.baseUrl, this.config.directory, signalArg, ); this.connectionId = connection.connectionId; } public listChats(limitArg: number): Promise { return this.connection().listChats(limitArg); } public readChat( chatIdArg: string, limitArg: number, maxCharsArg: number, ): Promise { return this.connection().readChat(chatIdArg, limitArg, maxCharsArg); } public async close(): Promise { await this.registry.closeAll(); this.connectionId = undefined; } private connection(): plugins.crossharness.IHarnessConnection { if (!this.connectionId) throw new Error('The Session Intelligence reader is not initialized.'); return this.registry.get(this.connectionId); } }