Skip to content

Commit

Permalink
Use NonblankString type in UserMessageAdded event
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilger committed Dec 16, 2023
1 parent 3d30cdd commit ba48130
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 4 additions & 3 deletions lib/gpt_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ defmodule GptAgent do
|> send_callback(%UserMessageAdded{
id: id,
thread_id: state.thread_id,
content: message.value
content: message
})
|> noreply()
end

@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

Expand Down
4 changes: 3 additions & 1 deletion lib/gpt_agent/events/user_message_added.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 13 additions & 10 deletions test/gpt_agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule GptAgentTest do
doctest GptAgent

alias GptAgent.Events.{ThreadCreated, UserMessageAdded}
alias GptAgent.Values.NonblankString

setup _context do
bypass = Bypass.open()
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit ba48130

Please sign in to comment.