Skip to content

Commit

Permalink
Update assistant_id on each connection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwilger committed Feb 15, 2024
1 parent c1b0435 commit 661edd6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/gpt_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 9 additions & 3 deletions test/gpt_agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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", %{
Expand Down

0 comments on commit 661edd6

Please sign in to comment.