[Customer Portal][BE] Add AI chat agent for case classification during creation - #126
Conversation
📝 WalkthroughWalkthroughAdds an AI chat agent integration: new config section, OAuth2-backed HTTP client with retry settings, typed request/response models, a new service resource POST /cases/classify that forwards payloads to the AI service and returns classification responses or server errors. Changes
Sequence DiagramsequenceDiagram
participant Client
participant Service as Backend Service
participant Module as AI Chat Agent Module
participant AIService as External AI Service
Client->>Service: POST /cases/classify (payload)
Service->>Service: Read user-info header
alt header missing
Service-->>Client: 500 InternalServerError
else header present
Service->>Module: createCaseClassification(payload)
Module->>AIService: POST /case_classification (with OAuth2 token)
Note right of AIService: Retries (3 attempts, 2s interval) on 502/503/504
AIService-->>Module: CaseClassificationResponse
Module-->>Service: Return response
Service-->>Client: CaseClassificationResponse
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@apps/customer-portal/backend/config.toml.local`:
- Around line 28-34: The TOML has the OAuth2 config under the wrong section —
change the header currently named
[customer_portal.updates.clientCredentialsOauth2Config] to
[customer_portal.ai_chat_agent.clientCredentialsOauth2Config] so the
ai_chat_agent gets its own credentials; ensure the block sits under the
customer_portal.ai_chat_agent section (matching aiChatAgentBaseUrl) and remove
any duplicate/incorrect [customer_portal.updates.clientCredentialsOauth2Config]
entry to avoid overwriting the updates config.
In `@apps/customer-portal/backend/modules/ai_chat_agent/types.bal`:
- Around line 30-42: Change the CaseClassificationPayload fields to idiomatic
camelCase (e.g., chatHistory, productDetails, environments, region, tier) and
add `@jsondata`:Name annotations to each field to map them to the snake_case keys
expected by the external API (for example annotate chatHistory with
`@jsondata`:Name("chat_history")); follow the same pattern used in
CaseClassificationResponse (caseInfo -> case_info) so the Ballerina record uses
camelCase internally while the serialized JSON matches the /case_classification
endpoint.
🧹 Nitpick comments (3)
apps/customer-portal/backend/types.bal (1)
312-325: Consider adding input constraints onchatHistory.The
chatHistoryfield accepts an unbounded string. Since this is forwarded to an external AI service (with a 300s timeout and retries), a very large payload could cause performance issues or exceed the downstream service's limits. Consider adding a@constraint:String {maxLength: ...}annotation, consistent with how other payloads in this file use constraints (e.g.,CommentCreatePayload).apps/customer-portal/backend/service.bal (2)
849-849: Track the TODO for persisting chat history.The comment on line 849 notes the need to persist chat history. Consider creating a tracked issue so this doesn't get lost.
Do you want me to open a GitHub issue to track this?
850-858: Inconsistent field naming convention between the twoCaseClassificationPayloadtypes.The public-facing
CaseClassificationPayload(intypes.bal) uses camelCase (chatHistory), while the AI chat agent module'sCaseClassificationPayload(inai_chat_agent/types.bal) uses snake_case (chat_history) for that same field but camelCase for all others (productDetails,environments, etc.). This mixed naming within the internal type is confusing. Consider aligning the AI module's payload to use either all camelCase (with@jsondata:Nameannotations for the wire format) or all snake_case consistently.
1b5d11d to
b652817
Compare
0626dc1
into
wso2-open-operations:customer-portal-milestone-1
Description
This PR introduces an AI-powered chat agent to classify cases during the case creation flow and return relevant suggestions to the case creation page.
Changes
Testing
Related Issue
Checklist
Summary by CodeRabbit