5#include <openssl/ssl.h>
6#include <openssl/err.h>
8#include <tt/world/World.h>
10#include <tt/world/Status.h>
11#include <tt/world/Turn.h>
12#include <tt/io/Connect.h>
13#include <tt/world/State.h>
15#include <tt/util/BlockingQueue.h>
17#include <tt/ExternalAPI.h>
104 Client(
const std::string& name,
const std::string& password,
const std::string& world,
Role role,
const std::string& partner,
const char* key,
const std::string& url,
int port);
122 Client(
const std::string& name,
const std::string& world,
Role role,
const std::string& partner,
const std::string& url,
int port);
137 Client(
const std::string& name,
const std::string& world,
Role role,
const std::string& partner);
150 Client(
const std::string& name,
const std::string& world,
Role role);
159 Client(
const std::string& name);
166 virtual std::string
toString()
const;
173 const std::string&
getName()
const;
323 SSL*
connect(
const std::string& url,
int port);
359 void setName(
const std::string& name);
481 virtual void onStop(
const std::string& message){
522 virtual void onWarning(
const std::string& message);
535 virtual void onError(
const std::string& message);
547 template <
typename T>
572 std::unique_ptr<Join> join;
576 SSL_CTX* ctx =
nullptr;
580 std::thread receiveThread_;
581 std::atomic<bool> running_{
true};
587 std::unique_ptr<World> world =
nullptr;
593 std::unique_ptr<Status> status =
nullptr;
596 std::vector<const Turn *> choices;
599 std::unique_ptr<Stop> stop =
nullptr;
602 std::string session =
"";
613 void failIfJoined(
const std::string& property);
626 template <
typename T>
627 static const T* failIfNotStarted(
const T*
object,
const std::string& description);
640 template <
typename T>
641 static const T* failIfNotStarted(
const std::unique_ptr<T>&
object,
const std::string& description);
665 BlockingQueue<std::unique_ptr<Message>> messageQueue_;
674 std::unique_ptr<Message> processMessage(
const char* data,
int length);
677 std::string recvBuffer_;
680 bool receivedStop =
false;
683 std::mutex closeMutex_;
A client factory continuously creates clients to connect to a server and is useful for deploying auto...
Definition ClientFactory.h:46
A client connects to a server to find a partner and play a role in a storytelling session.
Definition Client.h:51
virtual void onError(const std::string &message)
This method is called if the server reports an error to this client.
Definition Client.cpp:419
void setRole(Role role)
Sets the role this client wants to play in its session.
Definition Client.cpp:446
virtual void onDisconnect()
If the client ever established its connection to the server, this method is called once right before ...
Definition Client.h:510
std::unique_ptr< T > receive()
Waits for a Message of a specified type to be received.
Definition Client.cpp:573
virtual void onClose()
This method is called once if the client was closed, if it was interrupted, or if the socket was disc...
Definition Client.h:497
std::vector< const Turn * > getChoices() const
If it is currently this client's turn to act, this method returns the list of turns they can take nex...
Definition Client.cpp:458
std::unique_ptr< Message > receiveAny()
Waits for a Message of any type to be received.
Definition Client.cpp:562
std::string execute()
Definition Client.cpp:126
void setPartner(const std::string &partner)
Sets the name of the partner this client wants to play in its session.
Definition Client.cpp:452
static const std::string DEFAULT_URL
The default URL the client will attempt to connect to if one is not explicitly provided in the constr...
Definition Client.h:75
friend std::ostream & operator<<(std::ostream &os, const Client &a)
Override stream insertion operator to allow printing the Client class.
std::vector< const Turn * > getHistory() const
Returns the list of all turns that have been taken so far in this client's session that this client h...
Definition Client.cpp:116
void setWorldName(const std::string &world)
Sets the name of the story world this client wants to play in.
Definition Client.cpp:440
virtual int onChoice(const Status *status)=0
This method is called each time it is this client's turn to make a choice in the story.
virtual void onUpdate(const Status *status)
This method is called each time the story world changes as a result of a turn the client observes.
Definition Client.h:436
virtual void onEnd(const Ending *ending)
This method is called once if the story reaches one of its pre-defined endings.
Definition Client.h:466
void sendMessage(const tt::Message &m)
This method sends a message to the server.
Definition Client.cpp:364
static const int DEFAULT_PORT
The default network port the client will attempt to connect to if one is not explicitly provided in t...
Definition Client.h:81
void setName(const std::string &name)
Sets the name this client will use.
Definition Client.cpp:428
const std::string & getPartner() const
Returns the name of the partner this client wants to play with.
Definition Client.cpp:106
virtual void onWarning(const std::string &message)
This method is called if this client encounters a problem which does not immediately require it to cl...
Definition Client.cpp:414
const std::string & getName() const
Returns the client's name.
Definition Client.cpp:85
virtual void onStop(const std::string &message)
This method is called once when the client's session ends.
Definition Client.h:481
SSL * connect(const std::string &url, int port)
Establishes a secure socket to the server based on this client's network configuration.
Definition Client.cpp:276
virtual void onConnect(const Connect *connect)
This method is called after the client connects to the server and the server sends the list of availa...
Definition Client.h:405
static const std::string ENVIRONMENT_VARIABLE_PASSWORD
The name of the environment variable where the client expects to find the password this agent will us...
Definition Client.h:60
ExternalAPI external
Used to access external APIs.
Definition Client.h:84
const std::string & getSession()
Returns the ID of this client's session.
Definition Client.cpp:464
static const std::string ENVIRONMENT_VARIABLE_API_KEY
The name of the environment variable where the client expects to find the API key used to authenticat...
Definition Client.h:69
Role getRole() const
Returns the role this client wants to play or is currently playing in its session.
Definition Client.cpp:98
const State * getState() const
Returns the current state of the story world.
Definition Client.cpp:121
Client(const std::string &name, const std::string &password, const std::string &world, Role role, const std::string &partner, const char *key, const std::string &url, int port)
Constructs a client with the given session preferences, password, API key, and network details.
Definition Client.cpp:32
void setPassword(const std::string &password)
Sets the password this client will use.
Definition Client.cpp:434
virtual std::string toString() const
Gives a string representation of this client.
Definition Client.cpp:71
void close()
Definition Client.cpp:530
virtual void onStart(const World *world, Role role, const State *initial)
This method is called once when the client's session starts.
Definition Client.h:420
const World * getWorld()
Returns a const pointer to the story world in which this client's session is taking place.
Definition Client.cpp:111
const std::string & getWorldName() const
Returns the name of the story world this client will play in or is currently playing in.
Definition Client.cpp:90
The connect message is sent from the server to a client as soon as the agent connects to give details...
Definition Connect.h:17
An ending is a signed asset that represents one of several possible ways a story can end.
Definition Ending.h:17
Represents the external API used for text embedding and completion.
Definition ExternalAPI.h:16
The join message is the first message sent from a client to the server to provide the agent's credent...
Definition Join.h:18
A message is information sent to or from a server in JSON format.
Definition Message.h:23
A state is an assignment of values to each variable in a story world.
Definition State.h:18
A status contains the full history of a session in a story world, a description of its current state,...
Definition Status.h:22
A story world is a collection of assets needed for two partners in a session to tell a collaborative ...
Definition World.h:20
Tandem Tales is a platform that enables short, paired storytelling exercises between two agents over ...
Definition Client.h:22
Role
Represents the two roles in a collaborative storytelling session: the Player, who typically controls ...
Definition Role.h:16
@ NONE
No role, used as a "null" value.
Definition Role.h:32
std::ostream & operator<<(std::ostream &os, const Client &a)
Override stream insertion operator to allow printing the Client class.
Definition Client.cpp:556