Skip to content

Conversation

JeevaRamu0104
Copy link
Contributor

@JeevaRamu0104 JeevaRamu0104 commented Aug 4, 2025

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

  • Store the user query and response from the ai service for the analysis.
  • Allow the api only for internal user

Additional Changes

This pull request introduces support for encrypted chat conversations and adds database models and APIs for storing and retrieving chat interactions. The changes span configuration, API models, database schema, and core logic to enable secure chat storage and querying, with proper secret management for encryption keys.

Chat encryption and configuration:

  • Added encryption_key to [chat] sections in all config files and updated ChatSettings to use a secret for the encryption key, with secret management integration for secure handling.

Chat API model extensions:

  • Added new API models for listing chat conversations (ChatListRequest, ChatConversation, ChatListResponse)

Database schema and models for chat interactions:

  • Introduced hyperswitch_ai_interaction table and corresponding Rust models for storing encrypted chat queries and

Core chat logic improvements:

  • Enhanced chat workflow to fetch role information, set entity type, and use the decrypted chat encryption key for secure communication with the AI service.

Run this as part of migration

1.Manual Partition creation for next two year.
2.Creates partitions for each 3-month range
3.Add calendar event to re run this after two year.

DO $$
DECLARE
    start_date date;
    end_date date;
    i int;
    partition_name text;
BEGIN
    -- Get start date as the beginning of the current quarter
    start_date := date_trunc('quarter', current_date)::date;

    -- Create 8 quarterly partitions (2 years)
    FOR i IN 1..8 LOOP
        end_date := (start_date + interval '3 month')::date;

        partition_name := format(
            'hyperswitch_ai_interaction_%s_q%s',
            to_char(start_date, 'YYYY'),
            extract(quarter from start_date)
        );

        EXECUTE format(
            'CREATE TABLE IF NOT EXISTS %I PARTITION OF hyperswitch_ai_interaction
             FOR VALUES FROM (%L) TO (%L)',
            partition_name, start_date, end_date
        );

        start_date := end_date;
    END LOOP;
END$$;

General enhancements and constants:

  • Added default limit and offset constants for list operations, improving pagination support for chat conversation queries.
  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

Closes #8847

How did you test it?

Hitting AI service, should store information in DB

curl --location 'http://localhost:8080/chat/ai/data' \
--header 'x-feature: integ-custom' \
--header 'x-chat-session-id: chat-dop' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer JWT' \
--data '{
        "message": "total count of payments"
    
}'

Can check hyperswitch AI interaction table for it.

Internal roles can see the list of interactions for the given merchant id
List

curl --location 'http://localhost:8080/chat/ai/list?merchant_id=merchant_id' \
--header 'Authorization: Bearer JWT' \
--data ''

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@JeevaRamu0104 JeevaRamu0104 requested review from a team as code owners August 4, 2025 07:06
Copy link

semanticdiff-com bot commented Aug 4, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/types/storage.rs  87% smaller
  crates/diesel_models/src/lib.rs  87% smaller
  crates/api_models/src/events/chat.rs  83% smaller
  crates/router/src/routes/app.rs  82% smaller
  crates/router/src/configs/settings.rs  50% smaller
  crates/router/src/core/chat.rs  14% smaller
  crates/router/src/db.rs  5% smaller
  crates/router/src/routes/lock_utils.rs  4% smaller
  config/config.example.toml Unsupported file format
  config/deployments/env_specific.toml Unsupported file format
  config/development.toml Unsupported file format
  config/docker_compose.toml Unsupported file format
  crates/api_models/src/chat.rs  0% smaller
  crates/common_utils/src/consts.rs  0% smaller
  crates/diesel_models/src/hyperswitch_ai_interaction.rs  0% smaller
  crates/diesel_models/src/query.rs  0% smaller
  crates/diesel_models/src/query/hyperswitch_ai_interaction.rs  0% smaller
  crates/diesel_models/src/query/utils.rs  0% smaller
  crates/diesel_models/src/schema.rs  0% smaller
  crates/diesel_models/src/schema_v2.rs  0% smaller
  crates/hyperswitch_domain_models/src/chat.rs  0% smaller
  crates/router/src/configs/secrets_transformers.rs  0% smaller
  crates/router/src/core/errors/chat.rs  0% smaller
  crates/router/src/db/hyperswitch_ai_interaction.rs  0% smaller
  crates/router/src/db/kafka_store.rs  0% smaller
  crates/router/src/routes/chat.rs  0% smaller
  crates/router/src/types/storage/hyperswitch_ai_interaction.rs  0% smaller
  crates/router/src/utils.rs  0% smaller
  crates/router/src/utils/chat.rs  0% smaller
  crates/router_env/src/logger/types.rs  0% smaller
  crates/storage_impl/src/mock_db.rs  0% smaller
  loadtest/config/development.toml Unsupported file format
  migrations/2025-07-21-120614_add_table_for_hyperswitch_ai_interactions/down.sql Unsupported file format
  migrations/2025-07-21-120614_add_table_for_hyperswitch_ai_interactions/up.sql Unsupported file format

@hyperswitch-bot hyperswitch-bot bot added the M-database-changes Metadata: This PR involves database schema changes label Aug 4, 2025
@JeevaRamu0104 JeevaRamu0104 linked an issue Aug 5, 2025 that may be closed by this pull request
6 tasks
@JeevaRamu0104 JeevaRamu0104 linked an issue Aug 5, 2025 that may be closed by this pull request
@JeevaRamu0104 JeevaRamu0104 added the C-feature Category: Feature request or enhancement label Aug 5, 2025
Copy link
Member

@SanchithHegde SanchithHegde left a comment

Choose a reason for hiding this comment

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

Other than that, looks good to me!

SanchithHegde
SanchithHegde previously approved these changes Sep 2, 2025
inventvenkat
inventvenkat previously approved these changes Sep 3, 2025
Copy link
Contributor

@sai-harsha-vardhan sai-harsha-vardhan left a comment

Choose a reason for hiding this comment

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

Core changes looks good!

Copy link
Contributor

@sai-harsha-vardhan sai-harsha-vardhan left a comment

Choose a reason for hiding this comment

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

Core changes looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: Feature request or enhancement M-database-changes Metadata: This PR involves database schema changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Add schema to store chat request response [FEATURE]:Store the user query of the chat bot
6 participants