Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] chore: bump vercel ai #512

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

[WIP] chore: bump vercel ai #512

wants to merge 1 commit into from

Conversation

thucpn
Copy link
Collaborator

@thucpn thucpn commented Feb 27, 2025

StreamData is deprecated, Use createDataStream, createDataStreamResponse, and pipeDataStreamToResponse instead
Change in this PR:
https://github.com/vercel/ai/pull/3919/files#diff-44d4973726b4ae9362e9d6f34398def14a0fd80dc2d97fee6b6160f3910aa27e

Follow this example to update:
https://github.com/vercel/ai/blob/main/examples/next-openai/app/api/use-chat-persistence-single-message-tools/route.ts

Summary by CodeRabbit

  • New Features

    • Enhanced streaming responses deliver smoother, more reliable chat interactions with improved handling of message updates.
  • Chores

    • Upgraded the AI package dependency to benefit from enhanced performance and stability.

Copy link

changeset-bot bot commented Feb 27, 2025

⚠️ No Changeset found

Latest commit: e2346e9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Feb 27, 2025

Walkthrough

This pull request updates the handling of streaming data. In the events.ts module, the type StreamData has been replaced by DataStreamWriter in several function signatures along with renaming method calls from appendMessageAnnotation to writeMessageAnnotation. In the Next.js API route, the streaming process now uses createDataStreamResponse and an internal async execute method with an onFinal callback for finalizing responses. Additionally, the "ai" dependency in package.json is upgraded.

Changes

File(s) Summary of Changes
templates/.../streaming/events.ts Replaced StreamData with DataStreamWriter in function signatures (appendSourceData, appendEventData, appendToolData, createCallbackManager); updated method calls from appendMessageAnnotation to writeMessageAnnotation.
templates/.../nextjs/app/api/chat/route.ts Removed StreamData instantiation in favor of createDataStreamResponse; introduced an async execute method and replaced onCompletion with onFinal; updated annotation method.
templates/.../nextjs/package.json Upgraded the "ai" dependency version from ^4.0.3 to ^4.1.16.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant Stream
    participant CallbackManager
    participant ChatEngine

    Client->>API: POST /chat request
    API->>Stream: createDataStreamResponse()
    API->>CallbackManager: Set up callbacks (onFinal)
    CallbackManager->>ChatEngine: Process chat events
    ChatEngine-->>CallbackManager: Return event data
    CallbackManager->>API: Finalize response
    API->>Client: Stream response with annotations
Loading

Possibly related PRs

  • feat: use LlamaIndexAdapter #302: Involves removal of StreamData in favor of a new data handling approach, closely aligning with the type and method changes made in this PR.

Suggested reviewers

  • marcusschiesser
  • leehuwuj

Poem

I'm a coding rabbit on a bright new day,
Hopping through streams in a fresh, clear way.
From old types to writers, my paths now align,
With annotations that sparkle as they refine.
Carrots and code, what a delightful array!
🐰🥕


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee69ce7 and e2346e9.

📒 Files selected for processing (3)
  • templates/components/llamaindex/typescript/streaming/events.ts (6 hunks)
  • templates/types/streaming/nextjs/app/api/chat/route.ts (2 hunks)
  • templates/types/streaming/nextjs/package.json (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`templates/**`: For files under the `templates` folder, do n...

templates/**: For files under the templates folder, do not report 'Missing Dependencies Detected' errors.

  • templates/types/streaming/nextjs/app/api/chat/route.ts
  • templates/components/llamaindex/typescript/streaming/events.ts
  • templates/types/streaming/nextjs/package.json
⏰ Context from checks skipped due to timeout of 90000ms (45)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, express, --llamacloud)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, express, --example-file)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, express, --no-files)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, nextjs, --llamacloud)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, nextjs, --example-file)
  • GitHub Check: typescript (20, 3.11, ubuntu-22.04, nextjs, --no-files)
  • GitHub Check: typescript (20, 3.11, windows-latest, express, --llamacloud)
  • GitHub Check: typescript (20, 3.11, windows-latest, express, --example-file)
  • GitHub Check: typescript (20, 3.11, windows-latest, express, --no-files)
  • GitHub Check: typescript (20, 3.11, windows-latest, nextjs, --llamacloud)
  • GitHub Check: typescript (20, 3.11, windows-latest, nextjs, --example-file)
  • GitHub Check: typescript (20, 3.11, windows-latest, nextjs, --no-files)
  • GitHub Check: typescript (20, 3.11, macos-latest, express, --llamacloud)
  • GitHub Check: typescript (20, 3.11, macos-latest, express, --example-file)
  • GitHub Check: typescript (20, 3.11, macos-latest, express, --no-files)
  • GitHub Check: typescript (20, 3.11, macos-latest, nextjs, --llamacloud)
  • GitHub Check: typescript (20, 3.11, macos-latest, nextjs, --example-file)
  • GitHub Check: typescript (20, 3.11, macos-latest, nextjs, --no-files)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, express, --llamacloud)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, express, --example-file)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, express, --no-files)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, nextjs, --llamacloud)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, nextjs, --example-file)
  • GitHub Check: typescript (18, 3.11, ubuntu-22.04, nextjs, --no-files)
  • GitHub Check: typescript (18, 3.11, windows-latest, express, --llamacloud)
  • GitHub Check: typescript (18, 3.11, windows-latest, express, --example-file)
  • GitHub Check: typescript (18, 3.11, windows-latest, express, --no-files)
  • GitHub Check: typescript (18, 3.11, windows-latest, nextjs, --llamacloud)
  • GitHub Check: python (20, 3.11, ubuntu-22.04, fastapi, --llamacloud)
  • GitHub Check: typescript (18, 3.11, windows-latest, nextjs, --example-file)
  • GitHub Check: typescript (18, 3.11, windows-latest, nextjs, --no-files)
  • GitHub Check: python (20, 3.11, ubuntu-22.04, fastapi, --example-file)
  • GitHub Check: typescript (18, 3.11, macos-latest, express, --llamacloud)
  • GitHub Check: python (20, 3.11, ubuntu-22.04, fastapi, --no-files)
  • GitHub Check: typescript (18, 3.11, macos-latest, express, --example-file)
  • GitHub Check: python (20, 3.11, windows-latest, fastapi, --llamacloud)
  • GitHub Check: typescript (18, 3.11, macos-latest, express, --no-files)
  • GitHub Check: python (20, 3.11, windows-latest, fastapi, --example-file)
  • GitHub Check: typescript (18, 3.11, macos-latest, nextjs, --llamacloud)
  • GitHub Check: python (20, 3.11, windows-latest, fastapi, --no-files)
  • GitHub Check: typescript (18, 3.11, macos-latest, nextjs, --example-file)
  • GitHub Check: python (20, 3.11, macos-latest, fastapi, --llamacloud)
  • GitHub Check: typescript (18, 3.11, macos-latest, nextjs, --no-files)
  • GitHub Check: python (20, 3.11, macos-latest, fastapi, --example-file)
  • GitHub Check: python (20, 3.11, macos-latest, fastapi, --no-files)
🔇 Additional comments (9)
templates/types/streaming/nextjs/package.json (1)

21-21: Verify the new "ai" dependency upgrade

The bump from ^4.0.3 to ^4.1.16 may include breaking changes or newly added features. Please verify compatibility and review the release notes to ensure that no additional adjustments are necessary in downstream code.

templates/types/streaming/nextjs/app/api/chat/route.ts (3)

2-2: Good practice importing the updated constructs from "ai"

Bringing in createDataStreamResponse and Message directly from "ai" aligns the file with the new streaming approach. This keeps dependencies consistent across the codebase.


44-44: Double-check shape consistency when casting to ChatMessage[]

You're slicing off the last item (presumably the user's message) and casting the rest to ChatMessage[]. Verify that the Message type from "ai" is perfectly compatible with the LlamaIndex ChatMessage interface to avoid runtime discrepancies.


46-80: Effective use of createDataStreamResponse for streaming

This new structure—with an execute function for handling the streamed response—appears clear and maintainable. The callbacks and onFinal logic effectively manage final steps like generating suggested questions. The merger via LlamaIndexAdapter.mergeIntoDataStream looks consistent with the updated streaming API.

templates/components/llamaindex/typescript/streaming/events.ts (5)

1-1: Importing DataStreamWriter is correct

Replacing StreamData with DataStreamWriter reflects the new approach. This import aligns with the rest of the updated streaming logic.


18-30: Ensuring compatibility with writeMessageAnnotation

The updated function now writes annotated source data using data.writeMessageAnnotation. Confirm that any consumers expecting the previous annotation method (appendMessageAnnotation) have been updated accordingly. Otherwise, the logic for mapping node data to source metadata looks good.


41-43: Refined event logging flow

Switching from appendMessageAnnotation to writeMessageAnnotation for events is consistent with the new streaming API. The quick return on empty title also avoids unnecessary calls.


52-56: Tool data streaming looks correct

The appendToolData function properly uses data.writeMessageAnnotation to pass along tool invocation details. This keeps debugging info transparent and consistent with the codebase’s event logging pattern.


72-72: Callback manager creation is coherent

The createCallbackManager function now accepts DataStreamWriter and operates seamlessly with on-event hooks to push relevant context and tool data. This unifies the streaming style and ensures all events integrate with the new approach.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@thucpn thucpn marked this pull request as draft February 27, 2025 09:55
Comment on lines +75 to +78
LlamaIndexAdapter.mergeIntoDataStream(response, {
dataStream: vercelStreamData,
callbacks: { onFinal },
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Look like this merge function doesn't work as expectation. It still makes the markdown content fully generated

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.

1 participant