Skip to content

feat: add hyperswitch ai chats table #8831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

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?

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/core/chat.rs  12% 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/configs/settings.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
pub database_query: Option<String>,
pub interaction_status: Option<String>,
pub created_at: PrimitiveDateTime,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have it under a feature flag
Cc: @jarnura

Copy link
Contributor

Choose a reason for hiding this comment

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

currently all the routes related to the chat are inside chat config, it is a runtime feature flag chat_enabled that says whether chat feature is enabled or not in the given env.

#[diesel(table_name = hyperswitch_ai_interaction, primary_key(id), check_for_backend(diesel::pg::Pg))]
pub struct HyperswitchAiInteraction {
pub id: String,
pub session_id: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Difference between id and session_id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

id is a request-id and session_id is something which generated for that particular chat window

Copy link
Contributor

Choose a reason for hiding this comment

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

id is the message_id* for particular user request query and response, and the session_id is related to the whole conversation, i.e. current chat window

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
3 participants