Skip to content

feat: migrate to AI SDK v5.0.0-beta Agent abstraction#579

Merged
miurla merged 3 commits intov0.5from
feat/ai-sdk-v5-agent-abstraction
Jul 26, 2025
Merged

feat: migrate to AI SDK v5.0.0-beta Agent abstraction#579
miurla merged 3 commits intov0.5from
feat/ai-sdk-v5-agent-abstraction

Conversation

@miurla
Copy link
Copy Markdown
Owner

@miurla miurla commented Jul 26, 2025

Summary

  • Migrated from traditional streamText API to new Agent abstraction introduced in AI SDK v5.0.0-beta.8
  • Implemented sequential agent execution pattern for research and related questions generation
  • Fixed message handling and data structure issues

Changes

  • Agent Migration:

    • Updated researcher function to return an Agent instance
    • Updated generateRelatedQuestions to use Agent pattern
    • Implemented proper activeTools and stopWhen configurations
  • Sequential Execution:

    • Research agent runs first with sendFinish: false
    • Related questions agent runs after research completes with sendStart: false
    • Proper message merging for saving complete responses
  • Bug Fixes:

    • Fixed API route to handle AI SDK v5 messages array format
    • Fixed related questions component to handle both array and object data formats
    • Improved error handling and abort signal support
  • Improvements:

    • Enhanced related questions prompt to avoid repetition and generate unique follow-ups
    • Added proper TypeScript types and removed debug code
    • Maintained backward compatibility with existing UI components

Test Plan

  • Test basic chat functionality
  • Verify research results are displayed correctly
  • Confirm related questions are generated and displayed
  • Test error handling when agents fail
  • Verify message saving works correctly
  • Run linting and type checking
  • Test production build

Breaking Changes

  • Requires AI SDK v5.0.0-beta or higher
  • Agent abstraction is experimental and may change in future releases

🤖 Generated with Claude Code

- Update researcher and generateRelatedQuestions to use new Agent class
- Implement sequential agent execution pattern
- Fix message array handling in API route
- Update related questions component to handle both array and object formats
- Improve related questions prompts to generate unique follow-up questions
- Add proper message merging for saving complete responses

BREAKING CHANGE: Requires AI SDK v5.0.0-beta or higher

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Jul 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
morphic ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 26, 2025 7:53am

@miurla miurla changed the base branch from main to v0.5 July 26, 2025 07:47
@miurla miurla requested a review from Copilot July 26, 2025 07:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR migrates the chat system from the traditional streamText API to the new Agent abstraction introduced in AI SDK v5.0.0-beta.8, implementing a sequential execution pattern for research and related questions generation.

  • Migration from streamText API to Agent-based architecture with proper tool configuration
  • Implementation of sequential agent execution where research runs first, followed by related questions generation
  • Enhanced message handling to support AI SDK v5 message formats and improved error handling

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
lib/streaming/create-chat-stream-response.ts Implements sequential agent execution pattern with proper message merging and error handling
lib/agents/researcher.ts Converts researcher function to return Agent instance with updated tool configuration
lib/agents/generate-related-questions.ts Migrates to Agent pattern with improved prompt to avoid question repetition
components/related-questions.tsx Adds backward compatibility for both array and object data formats
app/api/chat/route.ts Updates API route to handle AI SDK v5 messages array format
Comments suppressed due to low confidence (1)

lib/agents/researcher.ts:60

  • The tool name has been changed from 'ask_question' to 'askQuestion'. Ensure this naming change is consistent with tool usage elsewhere in the codebase, as the snake_case to camelCase change could break existing references.
        askQuestion: askQuestionTool


return streamText(config)
: undefined,
stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1)
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The magic number 10 for maximum steps should be extracted to a constant for better maintainability. Consider defining MAX_SEARCH_STEPS = 10 to make this configurable and self-documenting.

Suggested change
stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1)
stopWhen: searchMode ? stepCountIs(MAX_SEARCH_STEPS) : stepCountIs(1)

Copilot uses AI. Check for mistakes.
miurla and others added 2 commits July 26, 2025 16:50
- Add await keyword to ensure proper error handling and completion tracking
- Prevents potential race conditions when saving messages on error

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Capture research message during initial stream merge
- Remove duplicate researchResult.toUIMessageStream() call
- Prevents potential stream consumption errors

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@miurla miurla merged commit 7a1c2d1 into v0.5 Jul 26, 2025
3 checks passed
@miurla miurla deleted the feat/ai-sdk-v5-agent-abstraction branch July 26, 2025 07:53
byte-rose pushed a commit to Bastsec/morphic that referenced this pull request Sep 23, 2025
…ction

feat: migrate to AI SDK v5.0.0-beta Agent abstraction
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.

2 participants