keep assistant errors in the active response slot - #9
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis PR changes how Ollama IPC failures are surfaced: instead of adding a new assistant error message, it updates the existing assistant placeholder in the active response slot and tightens the corresponding test expectations. Sequence diagram for handling Ollama IPC failure in useOllamasequenceDiagram
actor User
participant useOllama
participant ask_ollama
participant MessagesState
User->>useOllama: invoke ask
useOllama->>MessagesState: setMessages add assistant placeholder
useOllama->>ask_ollama: ask_ollama
ask_ollama-->>useOllama: [reject]
useOllama->>useOllama: console.error
useOllama->>MessagesState: setMessages map update assistantId
useOllama->>MessagesState: setIsGenerating false
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning Review limit reached
Next review available in: 58 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✨ 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.
Hey - I've found 1 issue, and left some high level feedback:
- In the catch handler, if
assistantIdis not found in the current messages array,setMessageswill be a no-op and no error message will be visible; consider adding a fallback that appends an error message when the placeholder message cannot be located. - The error message string
'Something went wrong Could not reach Ollama.'is duplicated between the hook and the test; consider extracting it to a shared constant to keep the test aligned with the implementation and make future changes safer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the catch handler, if `assistantId` is not found in the current messages array, `setMessages` will be a no-op and no error message will be visible; consider adding a fallback that appends an error message when the placeholder message cannot be located.
- The error message string `'Something went wrong
Could not reach Ollama.'` is duplicated between the hook and the test; consider extracting it to a shared constant to keep the test aligned with the implementation and make future changes safer.
## Individual Comments
### Comment 1
<location path="src/hooks/__tests__/useOllama.test.tsx" line_range="492-495" />
<code_context>
);
expect(errorMsg?.errorKind).toBe('Other');
- expect(errorMsg?.content).toBeTruthy();
+ expect(errorMsg?.content).toBe(
+ 'Something went wrong\nCould not reach Ollama.',
+ );
+ expect(result.current.messages).toHaveLength(2);
});
});
</code_context>
<issue_to_address>
**suggestion (testing):** Add assertions that the assistant message is replaced in-place (same id, single assistant message) rather than a new one being appended.
The added `toHaveLength(2)` helps ensure we don’t append an extra assistant message, but the test should also verify that the existing assistant placeholder is updated in-place. Consider capturing the assistant message id before triggering the error, then after the rejection assert that there is still exactly one assistant message and that its id is unchanged. This will more directly validate the new `map`-based replacement behavior and protect against regressions that reintroduce multiple assistant messages.
Suggested implementation:
```typescript
(m) => m.errorKind === 'Other',
);
expect(errorMsg?.errorKind).toBe('Other');
expect(errorMsg?.content).toBe(
'Something went wrong\nCould not reach Ollama.',
);
expect(result.current.messages).toHaveLength(2);
const assistantMessages = result.current.messages.filter(
(m) => m.role === 'assistant',
);
expect(assistantMessages).toHaveLength(1);
expect(assistantMessages[0].id).toBe(assistantMessageId);
});
});
```
To fully implement the suggestion, you also need to:
1. Capture the assistant placeholder message id *before* triggering the error in this test, e.g.:
`const assistantMessageId = result.current.messages.find((m) => m.role === 'assistant')!.id;`
2. Ensure `assistantMessageId` is in scope where the new expectations are added (likely defined near the start of the test case, before the action that causes the rejection).
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| expect(errorMsg?.content).toBe( | ||
| 'Something went wrong\nCould not reach Ollama.', | ||
| ); | ||
| expect(result.current.messages).toHaveLength(2); |
There was a problem hiding this comment.
suggestion (testing): Add assertions that the assistant message is replaced in-place (same id, single assistant message) rather than a new one being appended.
The added toHaveLength(2) helps ensure we don’t append an extra assistant message, but the test should also verify that the existing assistant placeholder is updated in-place. Consider capturing the assistant message id before triggering the error, then after the rejection assert that there is still exactly one assistant message and that its id is unchanged. This will more directly validate the new map-based replacement behavior and protect against regressions that reintroduce multiple assistant messages.
Suggested implementation:
(m) => m.errorKind === 'Other',
);
expect(errorMsg?.errorKind).toBe('Other');
expect(errorMsg?.content).toBe(
'Something went wrong\nCould not reach Ollama.',
);
expect(result.current.messages).toHaveLength(2);
const assistantMessages = result.current.messages.filter(
(m) => m.role === 'assistant',
);
expect(assistantMessages).toHaveLength(1);
expect(assistantMessages[0].id).toBe(assistantMessageId);
});
});To fully implement the suggestion, you also need to:
- Capture the assistant placeholder message id before triggering the error in this test, e.g.:
const assistantMessageId = result.current.messages.find((m) => m.role === 'assistant')!.id; - Ensure
assistantMessageIdis in scope where the new expectations are added (likely defined near the start of the test case, before the action that causes the rejection).
When the Ollama IPC invocation rejects, replace the existing assistant placeholder instead of appending a second assistant message. This keeps the failed turn grouped with its user prompt and adds an assertion for the resulting message count.
Summary by Sourcery
Handle Ollama IPC failures by updating the existing assistant message instead of adding a new one, and ensure message count and content are validated in tests.
Enhancements:
Tests: