Release: Chat API migration progress#113
Conversation
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThis 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
Sequence DiagramssequenceDiagram
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)
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
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
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
Poem
✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (72)
✏️ Tip: You can disable this entire section by setting 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. Comment |
Merge test to main
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.