| 48 | } |
| 49 | |
| 50 | export class Session { |
| 51 | readonly id: string; |
| 52 | readonly workDir: string; |
| 53 | summary?: SessionSummary | undefined; |
| 54 | private resumeState: ResumedSessionState | undefined; |
| 55 | |
| 56 | private readonly rpc: SDKRpcClientBase; |
| 57 | private readonly onClose?: (() => void | Promise<void>) | undefined; |
| 58 | private closed = false; |
| 59 | |
| 60 | constructor(options: SessionOptions) { |
| 61 | this.id = options.id; |
| 62 | this.workDir = options.workDir; |
| 63 | this.summary = options.summary; |
| 64 | this.resumeState = options.resumeState ?? resumeStateFromSummary(options.summary); |
| 65 | this.rpc = options.rpc; |
| 66 | this.onClose = options.onClose; |
| 67 | } |
| 68 | |
| 69 | getResumeState(): ResumedSessionState | undefined { |
| 70 | this.ensureOpen(); |
| 71 | return this.resumeState; |
| 72 | } |
| 73 | |
| 74 | async reloadSession(options?: ReloadSessionOptions): Promise<ResumedSessionSummary> { |
| 75 | this.ensureOpen(); |
| 76 | const summary = await this.rpc.reloadSession({ |
| 77 | sessionId: this.id, |
| 78 | forcePluginSessionStartReminder: options?.forcePluginSessionStartReminder, |
| 79 | }); |
| 80 | this.summary = summary; |
| 81 | this.resumeState = resumeStateFromSummary(summary); |
| 82 | return summary; |
| 83 | } |
| 84 | |
| 85 | onEvent(listener: (event: Event) => void): Unsubscribe { |
| 86 | this.ensureOpen(); |
| 87 | return this.rpc.onEvent((event) => { |
| 88 | if (event.sessionId === this.id) { |
| 89 | listener(event); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | setApprovalHandler(handler: ApprovalHandler | undefined): void { |
| 95 | this.ensureOpen(); |
| 96 | this.rpc.setApprovalHandler(this.id, handler); |
| 97 | } |
| 98 | |
| 99 | setQuestionHandler(handler: QuestionHandler | undefined): void { |
| 100 | this.ensureOpen(); |
| 101 | this.rpc.setQuestionHandler(this.id, handler); |
| 102 | } |
| 103 | |
| 104 | async prompt(input: string | PromptInput): Promise<void> { |
| 105 | this.ensureOpen(); |
| 106 | await this.rpc.prompt({ |
| 107 | sessionId: this.id, |
nothing calls this directly
no outgoing calls
no test coverage detected