Skip to content

Broadcast chat messages to all clients

Nicolas Dular requested to merge nd/chat-broadcast into master

What does this MR do and why?

Part of #420296 (closed)

This change introduces the aiAction parameter as part of the aiCompletion subscription. It currently only works for chat and is used to receive all chat responses from a user over the subscription. This enables us to sync all open chats from a client.

To not break existing chat clients, we now broadcast events twice:

  1. For old clients via userId, resourceId
  2. For new clients via userId, aiAction

We can remove broadcasting twice once all clients are up to date.

We also use the "streamed" subscription to broadcast the currently used tool. Otherwise, new clients that only listen on userId and aiAction would not receive the current tool (as this is a response that should be broadcasted to all clients). Tools now get broadcasted using userId and clientSubscriptionId

Screenshots or screen recordings

Screen_Recording_2023-09-18_at_16.43.33

Before After

How to set up and validate locally

  1. It needs to work without any FE changes
  2. It also needs to work with the following FE changes:
diff --git a/ee/app/assets/javascripts/ai/tanuki_bot/components/app.vue b/ee/app/assets/javascripts/ai/tanuki_bot/components/app.vue
index 5f255b4db7a1..4b7346c8aa31 100644
--- a/ee/app/assets/javascripts/ai/tanuki_bot/components/app.vue
+++ b/ee/app/assets/javascripts/ai/tanuki_bot/components/app.vue
@@ -59,14 +59,12 @@ export default {
         query: aiResponseSubscription,
         variables() {
           return {
-            resourceId: this.resourceId || this.userId,
             userId: this.userId,
+            aiAction: 'CHAT',
           };
         },
-        skip() {
-          return !this.loading;
-        },
         result({ data }) {
+          console.log(data);
           this.addDuoChatMessage(data?.aiCompletionResponse);
         },
         error(err) {
diff --git a/ee/app/assets/javascripts/graphql_shared/subscriptions/ai_completion_response.subscription.graphql b/ee/app/assets/javascripts/graphql_shared/subscriptions/ai_completion_response.subscription.graphql
index c0542e2edc54..93f4e67c5676 100644
--- a/ee/app/assets/javascripts/graphql_shared/subscriptions/ai_completion_response.subscription.graphql
+++ b/ee/app/assets/javascripts/graphql_shared/subscriptions/ai_completion_response.subscription.graphql
@@ -1,12 +1,14 @@
 subscription aiCompletionResponse(
   $userId: UserID
-  $resourceId: AiModelID!
+  $resourceId: AiModelID
   $clientSubscriptionId: String
+  $aiAction: AiAction
   $htmlResponse: Boolean = true
 ) {
   aiCompletionResponse(
     userId: $userId
     resourceId: $resourceId
+    aiAction: $aiAction
     clientSubscriptionId: $clientSubscriptionId
   ) {
     id

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Nicolas Dular

Merge request reports

Loading