Skip to content

Commit c3efb85

Browse files
vedantthapaCopilot
andauthored
fix: allow chat history persistence throughout user session (#92)
* Initial plan * Implement chat history persistence across user sessions and workspaces Co-authored-by: vedantthapa <43611693+vedantthapa@users.noreply.github.com> * Fix chat history persistence to use workspace and user scoped cache Co-authored-by: vedantthapa <43611693+vedantthapa@users.noreply.github.com> * Address code review feedback - add nil checks and fix callback handling Co-authored-by: vedantthapa <43611693+vedantthapa@users.noreply.github.com> * Cache only messages instead of entire LLMChain struct Co-authored-by: vedantthapa <43611693+vedantthapa@users.noreply.github.com> * Refactor: Use pattern matching instead of if conditions for cache operations Co-authored-by: vedantthapa <43611693+vedantthapa@users.noreply.github.com> * refactor: use original code patterns to reduce diff * refactor: rm helper function * fix: rm extra Map.put since it's not required * fix: fmt file * chore: rm tool-versions --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 4217e98 commit c3efb85

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

valentine/lib/valentine_web/components/layouts/app.html.heex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
active_module={@active_module}
119119
active_action={@active_action}
120120
workspace_id={@workspace_id || nil}
121+
current_user={@current_user}
121122
/>
122123
</div>
123124
</div>

valentine/lib/valentine_web/live/workspace_live/components/chat_component.ex

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
9191
socket.assigns.chain
9292
|> LLMChain.apply_delta(data)
9393

94-
Valentine.Cache.put({socket.id, :chatbot_history}, chain, expire: :timer.hours(24))
94+
%{workspace_id: workspace_id, current_user: user_id} = socket.assigns
95+
96+
Valentine.Cache.put({workspace_id, user_id, :chatbot_history}, chain.messages,
97+
expire: :timer.hours(24)
98+
)
9599

96100
{:ok,
97101
socket
@@ -119,7 +123,8 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
119123
end
120124

121125
def update(assigns, socket) do
122-
cached_chain = Valentine.Cache.get({socket.id, :chatbot_history}) || %LLMChain{}
126+
%{workspace_id: workspace_id, current_user: user_id} = assigns
127+
cached_messages = Valentine.Cache.get({workspace_id, user_id, :chatbot_history}) || []
123128

124129
{:ok,
125130
socket
@@ -137,7 +142,7 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
137142
callbacks: [llm_handler(self(), socket.assigns.myself)],
138143
cid: socket.assigns.myself
139144
})
140-
|> LLMChain.add_messages(cached_chain.messages)
145+
|> LLMChain.add_messages(cached_messages)
141146
)
142147
|> assign(assigns)}
143148
end
@@ -155,6 +160,8 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
155160
end
156161

157162
def handle_event("chat_submit", %{"value" => "/clear"}, socket) do
163+
%{workspace_id: workspace_id, current_user: user_id} = socket.assigns
164+
158165
chain =
159166
build_chain(%{
160167
stream: true,
@@ -169,16 +176,21 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
169176
cid: socket.assigns.myself
170177
})
171178

172-
Valentine.Cache.put({socket.id, :chatbot_history}, chain, expire: :timer.hours(24))
179+
# Clear from cache (store empty messages list)
180+
Valentine.Cache.put({workspace_id, user_id, :chatbot_history}, [], expire: :timer.hours(24))
173181

174182
{:noreply,
175183
socket
176184
|> assign(chain: chain)}
177185
end
178186

179187
def handle_event("chat_submit", %{"value" => value}, socket) do
180-
%{active_module: active_module, active_action: active_action, workspace_id: workspace_id} =
181-
socket.assigns
188+
%{
189+
workspace_id: workspace_id,
190+
current_user: user_id,
191+
active_module: active_module,
192+
active_action: active_action
193+
} = socket.assigns
182194

183195
chain =
184196
socket.assigns.chain
@@ -189,7 +201,9 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponent do
189201
Message.new_user!(value)
190202
])
191203

192-
Valentine.Cache.put({socket.id, :chatbot_history}, chain, expire: :timer.hours(24))
204+
Valentine.Cache.put({workspace_id, user_id, :chatbot_history}, chain.messages,
205+
expire: :timer.hours(24)
206+
)
193207

194208
{:noreply,
195209
socket

valentine/test/valentine_web/live/components/chat_component_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponentTest do
1717
chain: %LangChain.Chains.LLMChain{},
1818
id: "chat-component",
1919
skills: [],
20-
workspace_id: workspace.id
20+
workspace_id: workspace.id,
21+
current_user: "test_user@example.com"
2122
}
2223

2324
socket = %Phoenix.LiveView.Socket{
@@ -158,6 +159,8 @@ defmodule ValentineWeb.WorkspaceLive.Components.ChatComponentTest do
158159
assigns = %{
159160
active_module: "some_active_module",
160161
active_action: "some_active_action",
162+
workspace_id: socket.assigns.workspace_id,
163+
current_user: socket.assigns.current_user,
161164
some_key: "some_value"
162165
}
163166

0 commit comments

Comments
 (0)