Skip to content

Store databunny chat responses.#102

Merged
izadoesdev merged 1 commit into
databuddy-analytics:stagingfrom
sbansal1999:store-ai-messages
Aug 19, 2025
Merged

Store databunny chat responses.#102
izadoesdev merged 1 commit into
databuddy-analytics:stagingfrom
sbansal1999:store-ai-messages

Conversation

@sbansal1999

@sbansal1999 sbansal1999 commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Automatic conversation saving with support for continuing via conversationId.
    • Batch message handling (send/save multiple messages at once).
    • Richer message payloads (roles, content, thinking steps, chart type, SQL, final result).
  • Improvements

    • More consistent single-result outputs for metric and chart responses.
    • Smoother, paced streaming and clearer error responses.
  • API Changes

    • Assistant requests now include optional conversationId and require a defined model and user.
    • Updated streaming endpoint to await and return a finite set of updates.
    • saveConversation/addMessage now accept structured message arrays; responses changed (IDs batched).

@coderabbitai

coderabbitai Bot commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Replaces generator-based streaming with single-shot updates across agent handlers and processor. Adds batched “thinking steps,” converts streaming response creator to accept arrays, and introduces conversation persistence utilities. Updates request/response schemas (including AIResponse and conversationId), revises DB message schema, and restructures RPC routes for batched message inserts.

Changes

Cohort / File(s) Summary
Agent handlers: single-shot refactor
apps/api/src/agent/handlers/chart-handler.ts, apps/api/src/agent/handlers/metric-handler.ts
Convert AsyncGenerator handlers to Promise-returning functions; replace yields with early returns; unify success/error paths into single StreamingUpdate; helper for metric response returns a single update.
Processor: array-based pipeline + persistence
apps/api/src/agent/processor.ts
Replace generator with Promise<StreamingUpdate[]>; map response types via helper; batch “thinking steps”; enforce non-null user; persist conversations/messages via new utils; adjust AssistantRequest/AssistantContext types.
Prompt schema and types
apps/api/src/agent/prompts/agent.ts
Remove nullable() from several fields; keep optionality; add exported AIResponse type alias.
Stream utilities: array input
apps/api/src/agent/utils/stream-utils.ts
createStreamingResponse now accepts StreamingUpdate[]; per-update 200ms pacing; generateThinkingSteps returns an array; createThinkingStep made internal.
Route: assistant streaming
apps/api/src/routes/assistant.ts
Error helper returns array; include conversationId in request; pass non-null user; await processAssistantRequest; adjust types.
Assistant request schema
apps/api/src/schemas/assistant-schemas.ts
Add optional conversationId to AssistantRequestSchema (and derived type).
Conversation persistence utils (new)
apps/api/src/agent/utils/conversation-utils.ts
Add createNewConversation and addMessageToConversation; transactional inserts/updates; augment messages with conversationId/modelType.
DB schema: assistantMessages redesign
packages/db/src/drizzle/schema.ts
Replace userMessage with role/content; make responseType nullable; add sql, chartType, finalResult, textResponse, thinkingSteps; remove finalResponse/sqlQuery/chartData; export AssistantMessageInput type.
RPC router: batched message APIs
packages/rpc/src/routers/assistant.ts
Introduce messageSchema; saveConversation takes {conversationId?, websiteId, title, messages[]}; returns {conversationId}; addMessage takes array of messages, validates ownership, updates updatedAt, returns {messageIds}.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Client
  participant R as /v1/assistant/stream
  participant P as processAssistantRequest
  participant H as Handler (text/metric/chart)
  participant S as Stream Utils
  participant U as Conversation Utils

  C->>R: POST body (prompt, model, conversationId?)
  R->>P: AssistantRequest + Context (user, website)
  P->>P: generateThinkingSteps()
  P->>H: route by AI response type
  H-->>P: StreamingUpdate (single)
  P->>S: createStreamingResponse(thinkingSteps + final)
  Note over P,U: setImmediate: saveConversationWithResult(...)
  P-)U: createNewConversation / addMessageToConversation
  S-->>C: SSE stream (batched updates with pacing)
Loading

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120+ minutes

Possibly related PRs

Poem

I nibble bytes, then hop—no more drip-drip streams,
One tidy carrot bundle with well-ordered dreams.
Conversations stored, in burrows safe and bright,
Thinking steps in piles, lined neatly left to right.
With charts and metrics crisp—thump-thump!—I cheer,
A rabbit shipping code, concise and clear. 🥕✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e2e06d4 and a4bb8b0.

📒 Files selected for processing (10)
  • apps/api/src/agent/handlers/chart-handler.ts (3 hunks)
  • apps/api/src/agent/handlers/metric-handler.ts (3 hunks)
  • apps/api/src/agent/processor.ts (5 hunks)
  • apps/api/src/agent/prompts/agent.ts (2 hunks)
  • apps/api/src/agent/utils/conversation-utils.ts (1 hunks)
  • apps/api/src/agent/utils/stream-utils.ts (2 hunks)
  • apps/api/src/routes/assistant.ts (3 hunks)
  • apps/api/src/schemas/assistant-schemas.ts (1 hunks)
  • packages/db/src/drizzle/schema.ts (2 hunks)
  • packages/rpc/src/routers/assistant.ts (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

@vercel

vercel Bot commented Aug 19, 2025

Copy link
Copy Markdown

@sbansal1999 is attempting to deploy a commit to the Databuddy Team on Vercel.

A member of the Team first needs to authorize it.

@sbansal1999

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 19, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Comment thread apps/api/src/agent/processor.ts
@izadoesdev
izadoesdev merged commit ffc88ff into databuddy-analytics:staging Aug 19, 2025
3 of 5 checks passed
@izadoesdev

Copy link
Copy Markdown
Member

is this idempotent? what happens if the loop runs twice @sbansal1999

@sbansal1999

Copy link
Copy Markdown
Contributor Author

heck, idk what will happen when the loop runs once

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