Skip to content

Commit

Permalink
Add run_in_progress?/1 callback to GptAgent
Browse files Browse the repository at this point in the history
This allows clients to inspect whether we are currently performing a run against
the Agent's thread.
  • Loading branch information
jwilger committed Feb 6, 2024
1 parent 9e7b291 commit 4554468
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/gpt_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule GptAgent do
@callback add_user_message(pid(), Types.nonblank_string()) :: Types.result(:run_in_progress)
@callback submit_tool_output(pid(), Types.tool_name(), Types.tool_output()) ::
Types.result(:invalid_tool_call_id)
@callback run_in_progress?(pid()) :: boolean()

defp noreply(%__MODULE__{} = state), do: {:noreply, state, state.timeout_ms}
defp noreply(%__MODULE__{} = state, next), do: {:noreply, state, next}
Expand Down Expand Up @@ -212,6 +213,10 @@ defmodule GptAgent do
end

@impl true
def handle_call(:run_in_progress?, _caller, %__MODULE__{} = state) do
reply(state, state.running?)
end

def handle_call(:shutdown, _caller, %__MODULE__{} = state) do
log("Shutting down")
Registry.unregister(GptAgent.Registry, state.thread_id)
Expand Down Expand Up @@ -581,5 +586,10 @@ defmodule GptAgent do
def submit_tool_output(pid, tool_call_id, tool_output) do
GenServer.call(pid, {:submit_tool_output, tool_call_id, tool_output})
end

@impl true
def run_in_progress?(pid) do
GenServer.call(pid, :run_in_progress?)
end
end
end
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.0.2",
version: "6.1.0",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
aliases: aliases(),
Expand Down
26 changes: 26 additions & 0 deletions test/gpt_agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,4 +1354,30 @@ defmodule GptAgentTest do
:ok = GptAgent.submit_tool_output(pid, tool_1_id, %{another: "answer"})
end
end

describe "run_in_progress?/1" do
test "returns true if the agent has a run in progress", %{
assistant_id: assistant_id,
thread_id: thread_id
} do
{:ok, pid} =
GptAgent.connect(thread_id: thread_id, last_message_id: nil, assistant_id: assistant_id)

assert :ok = GptAgent.add_user_message(pid, Faker.Lorem.sentence())

assert_receive {^pid, %RunStarted{}}, 5_000

assert GptAgent.run_in_progress?(pid)
end

test "returns false if the agent does not have a run in progress", %{
assistant_id: assistant_id,
thread_id: thread_id
} do
{:ok, pid} =
GptAgent.connect(thread_id: thread_id, last_message_id: nil, assistant_id: assistant_id)

refute GptAgent.run_in_progress?(pid)
end
end
end

0 comments on commit 4554468

Please sign in to comment.