Skip to content

Release: Chat API migration progress#113

Merged
sweetmantech merged 20 commits intomainfrom
test
Jan 15, 2026
Merged

Release: Chat API migration progress#113
sweetmantech merged 20 commits intomainfrom
test

Conversation

@sweetmantech
Copy link
Contributor

@sweetmantech sweetmantech commented Jan 15, 2026

Merge test to main

Summary by CodeRabbit

  • New Features
    • Added streaming and non-streaming chat generation endpoints with CORS support.
    • Integrated Google Sheets authentication and tooling.
    • Auto-generate chat titles from first message.
    • New deep research capabilities for web and artist information.
    • Automated tool chains for artist creation and release report workflows.
    • Enhanced system prompts with image context, user details, and knowledge base integration.
    • Credit tracking and usage deduction system.

✏️ Tip: You can customize this high-level summary in your review settings.

sweetmantech and others added 20 commits January 14, 2026 18:01
Add ChatConfig interface that extends RoutingDecision with:
- system prompt, messages, and tools
- experimental_generateMessageId and experimental_download functions
- prepareStep function and providerOptions

Add 10 unit tests for the chat types.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add validateChatRequest function to recoup-api that:
- Validates request body using Zod schema (prompt/messages mutual exclusivity)
- Authenticates via API key using getApiKeyAccountId
- Supports accountId override for org API keys via validateOverrideAccountId
- Normalizes prompt to messages array using getMessages

Includes 18 unit tests covering schema validation, authentication,
accountId override, message normalization, and optional fields.

Part of MYC-3520 API chat endpoint migration.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add setupChatRequest that prepares chat configuration:
- Calls getGeneralAgent for routing decision
- Converts messages with MAX_MESSAGES limit (55)
- Configures provider options for Anthropic/Google/OpenAI
- Includes experimental_generateMessageId

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Implements POST /api/chat with createUIMessageStream for real-time
streaming responses using the existing chat infrastructure.

- Add handleChatStream function for streaming chat processing
- Add route handler with CORS support
- Add 7 unit tests covering validation, streaming, and error handling

Note: handleChatCredits and handleChatCompletion hooks are no-ops
until those functions are migrated.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add POST /api/chat/generate endpoint for synchronous text generation:
- handleChatGenerate function using AI SDK's generateText
- Returns JSON with text, reasoningText, sources, finishReason, usage
- Validates via validateChatRequest, configures via setupChatRequest
- Includes 9 unit tests covering validation, generation, and error handling

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Add generateChatTitle function using LIGHTWEIGHT_MODEL (gpt-4o-mini)
- Generate formal titles under 20 characters from firstMessage
- Highlight segment names if present in the message
- Strip accidental quote wrapping from generated titles
- Add firstMessage field to createChatBody schema
- Integrate title generation into createChatHandler
- Gracefully fall back to null topic on generation failure
- Add 21 new unit tests (15 for generateChatTitle, 6 for validation/handler)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Use NextResponse instead of Response in the return type so TypeScript
properly narrows the union type when checking instanceof NextResponse.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Use specific provider option types (AnthropicProviderOptions,
GoogleGenerativeAIProviderOptions, OpenAIResponsesProviderOptions)
instead of Record<string, unknown> for compatibility with generateText.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add @ai-sdk/anthropic, @ai-sdk/google, and @ai-sdk/openai for
provider-specific type definitions used in ChatConfig.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add credit calculation and deduction logic for chat usage:
- getAvailableModels: fetches models from Vercel AI Gateway
- getModel: finds model by ID from available models
- isEmbedModel: filters embed models by output price
- getCreditUsage: calculates USD cost from token usage
- handleChatCredits: orchestrates credit deduction after chat

Includes 22 unit tests (8 for handleChatCredits, 7 for getCreditUsage,
4 for getModel, 3 for getAvailableModels).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add @ai-sdk/gateway for getAvailableModels functionality.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add post-completion handler for chat messages:
- handleChatCompletion: orchestrates post-chat tasks
- selectRoom: fetches room by ID from Supabase
- upsertMemory: stores messages to memories table
- extractSendEmailResults: parses email tool outputs
- handleSendEmailToolOutputs: links emails to memories
- sendErrorNotification: sends errors to Telegram
- serializeError: converts errors to JSON format

Includes 14 unit tests covering message storage, new conversation
handling, email tool outputs, account email lookup, and error handling.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add comprehensive test coverage for the chat agent system:
- getGeneralAgent: 23 tests covering agent creation, account lookups, artist context
- getSystemPrompt: 29 tests for context values, user/artist sections, knowledge base
- extractImageUrlsFromMessages: 19 tests for media type filtering and edge cases
- buildSystemPromptWithImages: 12 tests for output formatting
- getKnowledgeBaseText: 21 tests for file type filtering and content fetching

Total: 104 new unit tests bringing test count to 317

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…gration

Add Google Sheets tools integration via Composio to setupToolsForRequest.
Now aggregates tools from MCP client and Google Sheets:
- If user is authenticated: returns full Composio Google Sheets toolkit
- If not authenticated: returns google_sheets_login tool for OAuth flow

New files:
- getGoogleSheetsTools: checks auth status and returns appropriate tools
- getLatestUserMessageText: extracts latest user message for callback URL
- googleSheetsLoginTool: initiates OAuth authentication via Composio

Added 32 unit tests for setupToolsForRequest (11), getGoogleSheetsTools (10),
and getLatestUserMessageText (11). All 349 tests pass.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
AI SDK v6 uses inputSchema property instead of parameters for tool
schema definition.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Migrates the complete toolChains orchestration framework for multi-step
tool sequences. This enables automated workflows like artist onboarding
and release report generation.

Components migrated:
- getPrepareStepResult: Orchestration engine that determines next tool
- getExecutedToolTimeline: Tracks tool execution history from steps
- TOOL_CHAINS: Registry with create_new_artist (17 steps) and
  create_release_report (5 steps) chains
- TOOL_MODEL_MAP: Maps tools to specific models (e.g., gemini-2.5-pro)
- Reference message generators for knowledge base and release reports

Integration:
- setupChatRequest now uses getPrepareStepResult in prepareStep callback
- When a chain trigger tool is executed, subsequent tools are
  automatically routed through the defined sequence

Test coverage: 38 new unit tests for toolChains framework plus
2 integration tests for setupChatRequest.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Chat endpoints now support both x-api-key and Authorization Bearer token
authentication. Uses getAuthenticatedAccountId for JWT validation via Privy.

- Updated validateChatRequest to support dual auth mechanisms
- Enforces exactly one auth method (x-api-key XOR Authorization)
- Added 3 new tests for Authorization header support
- Updated handleChatStream and handleChatGenerate tests with mock

This enables Recoup-Chat to proxy requests to recoup-api by forwarding
the Authorization header directly.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…rom Recoup-Chat

Add critical MCP tools needed for toolChains:
- web_deep_research: Perplexity sonar-deep-research for comprehensive research with citations
- artist_deep_research: Fetches artist socials for deep research
- chatWithPerplexity: New Perplexity API wrapper for non-streaming chat completions

These tools are required by create_new_artist and create_release_report toolChains.
Migrated with 21 unit tests total (8 + 7 + 6).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add comprehensive integration tests for the chat migration:
- validateChatRequest integration (7 tests): auth validation, prompt/messages
  validation, parameter passing
- setupChatRequest integration (6 tests): account email lookup, artist context
  fetching, account details, model override
- handleChatCompletion integration (6 tests): room creation, memory storage,
  email tool outputs, error handling
- handleChatCredits integration (5 tests): credit calculation, deduction,
  zero cost handling, error recovery
- End-to-end validation flow (3 tests): complete pipeline validation

Total: 27 new integration tests, 438 tests passing

Co-Authored-By: Claude Opus 4.5 <[email protected]>
…-migrate-apichat-endpoints-from-recoup-chat-to-recoup-api

feat: migrate chat types from Recoup-Chat to recoup-api
@vercel
Copy link
Contributor

vercel bot commented Jan 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
recoup-api Building Building Preview Jan 15, 2026 2:42am

@sweetmantech sweetmantech merged commit 01c4dca into main Jan 15, 2026
1 of 3 checks passed
@coderabbitai
Copy link

coderabbitai bot commented Jan 15, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This PR introduces a complete chat API infrastructure including non-streaming and streaming endpoints, request validation and chat setup pipelines, sequential tool chain orchestration, Google Sheets integration with authentication, credit usage tracking and deduction, and supporting utilities for message processing, error handling, email extraction, and chat title generation.

Changes

Cohort / File(s) Summary
API Routes
app/api/chat/generate/route.ts, app/api/chat/route.ts
New POST/OPTIONS handlers for non-streaming chat generation and streaming chat endpoints with CORS support and delegation to handlers
Chat Request Validation & Setup
lib/chat/validateChatRequest.ts, lib/chat/setupChatRequest.ts, lib/chat/validateMessages.ts
Request validation with authentication, account resolution, and message normalization; chat config assembly with routing decisions, message conversion, and provider options
Chat Handlers
lib/chat/handleChatGenerate.ts, lib/chat/handleChatStream.ts, lib/chat/handleChatCompletion.ts
Non-streaming text generation, streaming response creation, and post-chat orchestration (room creation, memory storage, email processing) with error handling
Tool Chain Orchestration
lib/chat/toolChains/toolChains.ts, lib/chat/toolChains/getPrepareStepResult.ts, lib/chat/toolChains/getExecutedToolTimeline.ts
Tool chain definitions, step progression logic, and tool execution timeline tracking with model overrides and system prompts
Tool Chain Definitions
lib/chat/toolChains/createNewArtistToolChain.ts, lib/chat/toolChains/create_release_report/*
Sequential tool workflows for artist creation and release reports with reference messages
Tools Setup
lib/chat/setupToolsForRequest.ts, lib/agents/googleSheetsAgent/*
MCP and Google Sheets tool aggregation; Google Sheets authentication with callback URL and active status verification
Google Sheets Tool
lib/composio/tools/googleSheetsLoginTool.ts
Login tool for initiating Google Sheets authentication
MCP Tools
lib/mcp/tools/registerWebDeepResearchTool.ts, lib/mcp/tools/registerArtistDeepResearchTool.ts, lib/mcp/tools/index.ts
New Perplexity-backed web research and artist deep research tools with citation formatting
Perplexity Integration
lib/perplexity/chatWithPerplexity.ts
Perplexity API client for web research with multi-source synthesis
AI Model Management
lib/ai/getAvailableModels.ts, lib/ai/getModel.ts, lib/ai/isEmbedModel.ts
Vercel AI Gateway integration for model availability, lookup by ID, and embedding model filtering
Credit System
lib/credits/getCreditUsage.ts, lib/credits/handleChatCredits.ts
Usage calculation from token counts and model pricing; deduction orchestration with rounding logic
Message Utilities
lib/messages/getLatestUserMessageText.ts, lib/messages/extractImageUrlsFromMessages.ts
Latest user message text extraction; image URL extraction from message parts
Chat Title Generation
lib/chats/generateChatTitle.ts, lib/chats/validateCreateChatBody.ts, lib/chats/createChatHandler.ts
Lightweight model-based title generation with quote stripping; firstMessage field addition to chat creation
Email Processing
lib/emails/extractSendEmailResults.ts, lib/emails/handleSendEmailToolOutputs.ts
Email result extraction from tool outputs; memory-email mapping insertion
Error Handling
lib/errors/serializeError.ts, lib/telegram/sendErrorNotification.ts
Error serialization with stack traces; Telegram notifications with context (path, room, email)
Type Definitions
lib/chat/types.ts, lib/chat/const.ts
ChatConfig extending RoutingDecision with system, messages, tools, and provider options; MAX_MESSAGES constant (55)
Database Utilities
lib/supabase/memories/upsertMemory.ts, lib/supabase/rooms/selectRoom.ts
Memory upsert; room selection with fallback
System Prompts
lib/prompts/getSystemPrompt.ts
System prompt generation with context sections (account, user, artist, knowledge base) and image editing instructions
Test Suites
lib/chat/__tests__/*, lib/agents/__tests__/*, lib/credits/__tests__/*, lib/messages/__tests__/*, lib/mcp/tools/__tests__/*, lib/chats/__tests__/*, lib/perplexity/__tests__/*, lib/prompts/__tests__/*, lib/files/__tests__/*
Comprehensive unit and integration tests with mocked dependencies covering validation, setup, handlers, tool chains, credits, models, and utilities
Dependencies
package.json
Added @ai-sdk/anthropic, @ai-sdk/gateway, @ai-sdk/google, @ai-sdk/openai

Sequence Diagrams

sequenceDiagram
    participant Client
    participant API as /api/chat/generate
    participant Validator as validateChatRequest
    participant Authenticator as Auth Helper
    participant Setup as setupChatRequest
    participant Agent as getGeneralAgent
    participant AI as generateText
    participant Credits as handleChatCredits
    
    Client->>API: POST request + auth header
    API->>Validator: validate request
    Validator->>Authenticator: resolve accountId
    Authenticator-->>Validator: accountId
    Validator-->>API: ChatRequestBody
    API->>Setup: setupChatRequest
    Setup->>Agent: getGeneralAgent
    Agent-->>Setup: RoutingDecision (tools, system)
    Setup-->>API: ChatConfig
    API->>AI: generateText with config
    AI-->>API: text, usage, reasoning
    API->>Credits: handleChatCredits
    Credits-->>API: done (fire & forget)
    API-->>Client: JSON response (text, usage, sources)
Loading
sequenceDiagram
    participant Client
    participant API as /api/chat
    participant Validator as validateChatRequest
    participant Setup as setupChatRequest
    participant ToolSetup as setupToolsForRequest
    participant MCP as MCP Client
    participant GoogleSheets as getGoogleSheetsTools
    participant Agent as agent.stream
    participant Stream as UIStream
    
    Client->>API: POST request + messages
    API->>Validator: validate
    Validator-->>API: ChatRequestBody
    API->>Setup: setupChatRequest
    Setup->>ToolSetup: get tools
    ToolSetup->>MCP: getMCPTools
    ToolSetup->>GoogleSheets: getGoogleSheetsTools
    MCP-->>ToolSetup: MCP tools
    GoogleSheets-->>ToolSetup: Google Sheets tools
    ToolSetup-->>Setup: merged tools
    Setup-->>API: ChatConfig with tools
    API->>Agent: stream(config)
    Agent->>Stream: create streaming UI
    Stream-->>Client: streaming responses
    Agent-->>API: done
    API-->>Client: complete stream
Loading
sequenceDiagram
    participant User
    participant Client
    participant API as googleSheetsTools
    participant Composio as composio.getConnectedAccount
    participant Auth as auth provider
    
    User->>Client: Request Google Sheets access
    Client->>API: getGoogleSheetsTools
    API->>Composio: getConnectedAccount(accountId, options)
    Composio->>Auth: check status
    
    alt Connected & Active
        Auth-->>Composio: status = ACTIVE
        Composio-->>API: account data
        API->>Composio: tools.get (GOOGLESHEETS)
        Composio-->>API: googlesheets_create, googlesheets_read
        API-->>Client: Google Sheets tools ready
    else Not Connected
        Auth-->>Composio: status = PENDING/EXPIRED/undefined
        Composio-->>API: not active
        API-->>Client: google_sheets_login tool
        Client->>Auth: initiate login (callback URL with latest message)
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

This PR introduces substantial new infrastructure spanning 60+ files with heterogeneous changes: API routes, type definitions, chat validation/setup/handlers, tool chain orchestration logic, MCP tool registration, AI model integration, credit tracking, email processing, and extensive test coverage (12,000+ lines). The changes involve multiple interacting systems (authentication, routing, tool orchestration, AI SDK integration) with dense logic in tool chains and system prompt generation, demanding careful review of control flow, error handling, and integration points despite significant test coverage.

Possibly related PRs

  • Sweetmantech/myc 3794 api email agentgenerate pass in full message history for the #79 — Both PRs add/modify overlapping chat infrastructure modules (getGeneralAgent, buildSystemPromptWithImages, generateChatTitle, validateChatRequest, setupToolsForRequest) for chat request handling and email routing, indicating direct code-level intersection.
  • feat: migrate chat types from Recoup-Chat to recoup-api #112 — Both PRs introduce or modify the same chat subsystem modules (ChatConfig types, /api/chat routes, setupChatRequest, validateChatRequest, handleChatGenerate/handleChatStream, and related test coverage), showing significant overlap in chat plumbing.
  • Test #105 — Both PRs add/modify the chat creation route and closely overlapping lib/chats modules (validateCreateChatBody, createChatHandler, generateChatTitle, firstMessage field handling), indicating direct code-level changes to the same files.

Poem

🐰 A toolchain dance through streams and threads,
With Google Sheets and research spreads,
Credits count while models think,
Agent chains that never blink,
From prompt's flow to chat's embrace,
We hop toward this guided space! 🎯

✨ Finishing touches
  • 📝 Generate docstrings


📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c8fd03 and 0e4f835.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (72)
  • app/api/chat/generate/route.ts
  • app/api/chat/route.ts
  • lib/agents/generalAgent/__tests__/getGeneralAgent.test.ts
  • lib/agents/googleSheetsAgent/__tests__/getGoogleSheetsTools.test.ts
  • lib/agents/googleSheetsAgent/getGoogleSheetsTools.ts
  • lib/agents/googleSheetsAgent/index.ts
  • lib/ai/__tests__/getAvailableModels.test.ts
  • lib/ai/__tests__/getModel.test.ts
  • lib/ai/getAvailableModels.ts
  • lib/ai/getModel.ts
  • lib/ai/isEmbedModel.ts
  • lib/chat/__tests__/buildSystemPromptWithImages.test.ts
  • lib/chat/__tests__/handleChatCompletion.test.ts
  • lib/chat/__tests__/handleChatGenerate.test.ts
  • lib/chat/__tests__/handleChatStream.test.ts
  • lib/chat/__tests__/integration/chatEndToEnd.test.ts
  • lib/chat/__tests__/setupChatRequest.test.ts
  • lib/chat/__tests__/setupToolsForRequest.test.ts
  • lib/chat/__tests__/types.test.ts
  • lib/chat/__tests__/validateChatRequest.test.ts
  • lib/chat/const.ts
  • lib/chat/handleChatCompletion.ts
  • lib/chat/handleChatGenerate.ts
  • lib/chat/handleChatStream.ts
  • lib/chat/setupChatRequest.ts
  • lib/chat/setupToolsForRequest.ts
  • lib/chat/toolChains/__tests__/getExecutedToolTimeline.test.ts
  • lib/chat/toolChains/__tests__/getPrepareStepResult.test.ts
  • lib/chat/toolChains/__tests__/toolChains.test.ts
  • lib/chat/toolChains/createNewArtistToolChain.ts
  • lib/chat/toolChains/create_release_report/createReleaseReportToolChain.ts
  • lib/chat/toolChains/create_release_report/getReleaseReportReferenceMessage.ts
  • lib/chat/toolChains/create_release_report/referenceReleaseReport.ts
  • lib/chat/toolChains/getExecutedToolTimeline.ts
  • lib/chat/toolChains/getKnowledgeBaseReportReferenceMessage.ts
  • lib/chat/toolChains/getPrepareStepResult.ts
  • lib/chat/toolChains/index.ts
  • lib/chat/toolChains/knowledgeBaseReferenceReport.ts
  • lib/chat/toolChains/toolChains.ts
  • lib/chat/types.ts
  • lib/chat/validateChatRequest.ts
  • lib/chat/validateMessages.ts
  • lib/chats/__tests__/createChatHandler.test.ts
  • lib/chats/__tests__/generateChatTitle.test.ts
  • lib/chats/__tests__/validateCreateChatBody.test.ts
  • lib/chats/createChatHandler.ts
  • lib/chats/generateChatTitle.ts
  • lib/chats/validateCreateChatBody.ts
  • lib/composio/tools/googleSheetsLoginTool.ts
  • lib/credits/__tests__/getCreditUsage.test.ts
  • lib/credits/__tests__/handleChatCredits.test.ts
  • lib/credits/getCreditUsage.ts
  • lib/credits/handleChatCredits.ts
  • lib/emails/extractSendEmailResults.ts
  • lib/emails/handleSendEmailToolOutputs.ts
  • lib/errors/serializeError.ts
  • lib/files/__tests__/getKnowledgeBaseText.test.ts
  • lib/mcp/tools/__tests__/registerArtistDeepResearchTool.test.ts
  • lib/mcp/tools/__tests__/registerWebDeepResearchTool.test.ts
  • lib/mcp/tools/index.ts
  • lib/mcp/tools/registerArtistDeepResearchTool.ts
  • lib/mcp/tools/registerWebDeepResearchTool.ts
  • lib/messages/__tests__/extractImageUrlsFromMessages.test.ts
  • lib/messages/__tests__/getLatestUserMessageText.test.ts
  • lib/messages/getLatestUserMessageText.ts
  • lib/perplexity/__tests__/chatWithPerplexity.test.ts
  • lib/perplexity/chatWithPerplexity.ts
  • lib/prompts/__tests__/getSystemPrompt.test.ts
  • lib/supabase/memories/upsertMemory.ts
  • lib/supabase/rooms/selectRoom.ts
  • lib/telegram/sendErrorNotification.ts
  • package.json

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Comments