diff --git a/lib/gpt_agent.ex b/lib/gpt_agent.ex index 7b36b1e..1cb9d83 100644 --- a/lib/gpt_agent.ex +++ b/lib/gpt_agent.ex @@ -60,7 +60,7 @@ defmodule GptAgent do |> send_callback(%UserMessageAdded{ id: id, thread_id: state.thread_id, - content: message.value + content: message }) |> noreply() end @@ -68,8 +68,9 @@ defmodule GptAgent do @doc """ Starts the GPT Agent """ - @spec start_link(pid(), binary() | nil) :: {:ok, pid()} | {:error, reason :: term()} - def start_link(callback_handler, thread_id \\ nil) when is_pid(callback_handler) do + @spec start_link(pid(), binary(), binary() | nil) :: {:ok, pid()} | {:error, reason :: term()} + def start_link(callback_handler, _assistant_id, thread_id \\ nil) + when is_pid(callback_handler) do GenServer.start_link(__MODULE__, callback_handler: callback_handler, thread_id: thread_id) end diff --git a/lib/gpt_agent/events/user_message_added.ex b/lib/gpt_agent/events/user_message_added.ex index 19ce26f..b1a7b34 100644 --- a/lib/gpt_agent/events/user_message_added.ex +++ b/lib/gpt_agent/events/user_message_added.ex @@ -3,11 +3,13 @@ defmodule GptAgent.Events.UserMessageAdded do An OpenAI Assistants user message was added to a thread """ + alias GptAgent.Values.NonblankString + use TypedStruct typedstruct do field :id, binary(), enforce: true field :thread_id, binary(), enforce: true - field :content, String.t(), enforce: true + field :content, NonblankString.t(), enforce: true end end diff --git a/test/gpt_agent_test.exs b/test/gpt_agent_test.exs index a7251f0..4698775 100644 --- a/test/gpt_agent_test.exs +++ b/test/gpt_agent_test.exs @@ -5,6 +5,7 @@ defmodule GptAgentTest do doctest GptAgent alias GptAgent.Events.{ThreadCreated, UserMessageAdded} + alias GptAgent.Values.NonblankString setup _context do bypass = Bypass.open() @@ -34,9 +35,9 @@ defmodule GptAgentTest do {:ok, bypass: bypass, thread_id: thread_id} end - describe "start_link/1" do + describe "start_link/2" do test "starts the agent" do - {:ok, pid} = GptAgent.start_link(self()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) assert Process.alive?(pid) end @@ -55,21 +56,21 @@ defmodule GptAgentTest do ) end) - {:ok, pid} = GptAgent.start_link(self()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) assert_receive {GptAgent, ^pid, :ready}, 5_000 end test "sends the ThreadCreated event to the callback handler", %{thread_id: thread_id} do - {:ok, pid} = GptAgent.start_link(self()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) assert_receive {GptAgent, ^pid, %ThreadCreated{id: ^thread_id}}, 5_000 end end - describe "start_link/2" do + describe "start_link/3" do test "starts the agent" do - {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word(), Faker.Lorem.word()) assert Process.alive?(pid) end @@ -78,13 +79,13 @@ defmodule GptAgentTest do raise "Should not have called the OpenAI API to create a thread" end) - {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word(), Faker.Lorem.word()) assert_receive {GptAgent, ^pid, :ready}, 5_000 end test "does not send the ThreadCreated event to the callback handler" do - {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word(), Faker.Lorem.word()) refute_receive {GptAgent, ^pid, %ThreadCreated{}}, 100 end @@ -95,7 +96,7 @@ defmodule GptAgentTest do bypass: bypass, thread_id: thread_id } do - {:ok, pid} = GptAgent.start_link(self(), thread_id) + {:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word(), thread_id) user_message_id = Faker.Lorem.word() message_content = Faker.Lorem.paragraph() @@ -134,9 +135,11 @@ defmodule GptAgentTest do %UserMessageAdded{ id: ^user_message_id, thread_id: ^thread_id, - content: ^message_content + content: %NonblankString{} = content }}, 5_000 + + assert content.value == message_content end end end