From 661edd6a62c917c111be2af19754c8d965c8ef6c Mon Sep 17 00:00:00 2001 From: John Wilger Date: Thu, 15 Feb 2024 10:53:37 -0800 Subject: [PATCH] Update assistant_id on each connection It doesn't make any sense to require that this value be specified on connect but then ignore it if the agent is already running. Let's put the responsibility of ensuring that any change to an assistant ID is appropriate on the client application rather than forcing the client application to jump through unnecessary hoops to update the value separately from the connection request. --- lib/gpt_agent.ex | 5 +++-- mix.exs | 2 +- test/gpt_agent_test.exs | 12 +++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/gpt_agent.ex b/lib/gpt_agent.ex index 9c67d3f..79c3915 100644 --- a/lib/gpt_agent.ex +++ b/lib/gpt_agent.ex @@ -458,7 +458,7 @@ defmodule GptAgent do case Registry.lookup(GptAgent.Registry, opts.thread_id) do [{pid, :gpt_agent}] -> - handle_existing_agent(pid, opts.last_message_id) + handle_existing_agent(pid, opts.last_message_id, opts.assistant_id) [] -> handle_no_existing_agent( @@ -523,10 +523,11 @@ defmodule GptAgent do defp maybe_subscribe(result, _opts), do: result - defp handle_existing_agent(pid, last_message_id) do + defp handle_existing_agent(pid, last_message_id, assistant_id) do log("Found existing GPT Agent with PID #{inspect(pid)}") log("Updating last message ID to #{inspect(last_message_id)}") GenServer.cast(pid, {:set_last_message_id, last_message_id}) + GenServer.cast(pid, {:set_assistant_id, assistant_id}) {:ok, pid} end diff --git a/mix.exs b/mix.exs index ce31dc2..4d2a7ae 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule GptAgent.MixProject do def project do [ app: :gpt_agent, - version: "6.2.0", + version: "7.0.0", elixir: "~> 1.16", start_permanent: Mix.env() == :prod, aliases: aliases(), diff --git a/test/gpt_agent_test.exs b/test/gpt_agent_test.exs index aaf11c6..316849d 100644 --- a/test/gpt_agent_test.exs +++ b/test/gpt_agent_test.exs @@ -281,16 +281,22 @@ defmodule GptAgentTest do refute_receive {^pid, %UserMessageAdded{}} end - test "does not update the assistant id on an agent if the agent is already running", + test "updates the assistant id on an agent if the agent is already running", %{thread_id: thread_id, assistant_id: assistant_id} do {:ok, pid1} = GptAgent.connect(thread_id: thread_id, last_message_id: nil, assistant_id: assistant_id) + new_assistant_id = UUID.uuid4() + {:ok, pid2} = - GptAgent.connect(thread_id: thread_id, last_message_id: nil, assistant_id: UUID.uuid4()) + GptAgent.connect( + thread_id: thread_id, + last_message_id: nil, + assistant_id: new_assistant_id + ) assert pid1 == pid2 - assert %GptAgent{assistant_id: ^assistant_id} = :sys.get_state(pid1) + assert %GptAgent{assistant_id: ^new_assistant_id} = :sys.get_state(pid1) end test "loads existing thread run status when connecting to thread with a run history", %{