Update chat client handling#1381
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThis PR defaults/optionalizes several Streams env vars, removes STREAMS_SECRET from examples and a migration doc, starts a durable streams server using a computed internal URL fallback, and standardizes durable-session error handling while making stop() async and proxy-based. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant DurableClient as Durable Chat Client
participant Proxy
participant Streams as Streams Server
Client->>DurableClient: stop() (await client.stop())
DurableClient->>Proxy: POST /v1/sessions/{sessionId}/stop { messageId: null }
Proxy-->>Streams: Forward stop request
Streams-->>Proxy: 200 OK
Proxy-->>DurableClient: 200 OK
DurableClient-->>Client: resolve
sequenceDiagram
participant Desktop
participant DurableClient as Durable Chat Client
participant Proxy
participant AuthDB as Auth DB
participant Streams as Streams Server
Desktop->>DurableClient: connect/create session (token)
DurableClient->>Proxy: POST /v1/sessions (with token)
Proxy->>AuthDB: validate session token
AuthDB-->>Proxy: valid
Proxy->>Streams: create/validate session
Streams-->>Proxy: 201/200
Proxy-->>DurableClient: 201/200
DurableClient-->>Desktop: session info / ws url
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/durable-session/src/client.ts`:
- Around line 384-388: The stop() method currently calls postToProxy directly
and lacks the connection guard and error-state handling used by other mutation
methods; update stop() to first check this._isConnected and then perform the
POST via this.executeAction so any failures set this._error and invoke
this.options.onError (i.e., wrap the call to
this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, { messageId: null })
inside this.executeAction and ensure the method short-circuits when
!this._isConnected).
| async stop(): Promise<void> { | ||
| await this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, { | ||
| messageId: null, | ||
| }); | ||
| } |
There was a problem hiding this comment.
stop() lacks the connection guard and error-state handling present in other mutation methods.
Every other public mutation (sendMessage, addToolResult, addToolApprovalResponse, addToolAnswerResponse) checks this._isConnected before proceeding and routes errors through executeAction (which sets this._error and calls this.options.onError). stop() skips both.
If stop() is called on a disconnected client, the fetch will likely fail with an unhelpful network error. And on failure, this._error won't be set at the client level (though the React hook does catch it separately).
Proposed fix
async stop(): Promise<void> {
+ if (!this._isConnected) {
+ throw new Error("Client not connected. Call connect() first.");
+ }
- await this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, {
- messageId: null,
- });
+ try {
+ await this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, {
+ messageId: null,
+ });
+ } catch (error) {
+ this._error = error instanceof Error ? error : new Error(String(error));
+ this.options.onError?.(this._error);
+ throw error;
+ }
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| async stop(): Promise<void> { | |
| await this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, { | |
| messageId: null, | |
| }); | |
| } | |
| async stop(): Promise<void> { | |
| if (!this._isConnected) { | |
| throw new Error("Client not connected. Call connect() first."); | |
| } | |
| try { | |
| await this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, { | |
| messageId: null, | |
| }); | |
| } catch (error) { | |
| this._error = error instanceof Error ? error : new Error(String(error)); | |
| this.options.onError?.(this._error); | |
| throw error; | |
| } | |
| } |
🤖 Prompt for AI Agents
In `@packages/durable-session/src/client.ts` around lines 384 - 388, The stop()
method currently calls postToProxy directly and lacks the connection guard and
error-state handling used by other mutation methods; update stop() to first
check this._isConnected and then perform the POST via this.executeAction so any
failures set this._error and invoke this.options.onError (i.e., wrap the call to
this.postToProxy(`/v1/sessions/${this.sessionId}/stop`, { messageId: null })
inside this.executeAction and ensure the method short-circuits when
!this._isConnected).
🚀 Preview Deployment🔗 Preview Links
Preview updates automatically with new commits |
Description
Related Issues
Type of Change
Testing
Screenshots (if applicable)
Additional Notes
Summary by CodeRabbit
Chores
New Features
Refactor
Bug Fixes