Skip to content

Conversation

@hadv
Copy link
Owner

@hadv hadv commented Oct 4, 2025

Problem

When users sent their first message in a new chat session, they would receive a "No valid messages" error instead of an AI response. This occurred because:

  • The context engine correctly returns empty arrays for recentMessages and relevantHistory on first messages
  • The server was only using these arrays to build the message context for the LLM
  • This resulted in an empty message array, which was correctly rejected by validation
  • Users got error responses instead of AI replies when starting new conversations

Solution

Added logic to include the current user message when no context messages exist:

// If no context messages exist (first message), add the current user message
if (allContextMessages.length === 0) {
  allContextMessages.push({
    role: 'user',
    content: sanitizedMessage
  });
}

Changes

  • Fixed both processing paths: Applied the fix to both MCP client and direct Anthropic API paths in server/src/index.ts
  • Added comprehensive tests: Created server/src/tests/firstMessage.test.ts with tests for:
    • Empty context handling for first messages
    • MCP client context building
    • No interference with subsequent messages
  • Minimal impact: The fix only triggers when context is empty, ensuring no impact on existing conversations

Testing

✅ All new tests pass
✅ Existing functionality unaffected
✅ First messages now generate proper AI responses
✅ Subsequent messages continue to work normally

Result

Users can now start new conversations and receive AI responses to their first messages instead of error messages.


Pull Request opened by Augment Code with guidance from the PR author

- Fix issue where first user messages resulted in 'No valid messages' error
- Add current user message to context when no previous messages exist
- Apply fix to both MCP client and direct Anthropic API paths
- Add comprehensive tests for first message handling logic
- Ensure subsequent messages with existing context are unaffected

Resolves issue where users received error responses instead of AI replies
when starting new conversations.
@hadv hadv requested a review from prpeh October 4, 2025 14:46
@hadv hadv merged commit 65413d4 into main Oct 4, 2025
3 checks passed
@hadv hadv deleted the fix/first-message-handling branch October 4, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants