Skip to content

Commit b9fcd04

Browse files
authored
Support file operation (#32)
1 parent 95f75ab commit b9fcd04

22 files changed

Lines changed: 2424 additions & 401 deletions

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ env:
1818
TestChaCha20Poly1305
1919
TestCounter
2020
TestCryptoKdfHkdfSha256
21-
TestDatabase /tmp/tui-test.db
21+
TestDatabase /tmp/tui-test.db /tmp/files
2222
TestEcdhePsk
2323
TestEd25519
2424
TestFakeCredentialGenerator
@@ -61,7 +61,8 @@ jobs:
6161
nlohmann-json3-dev \
6262
libssl-dev \
6363
valgrind \
64-
libzstd-dev
64+
libzstd-dev \
65+
libxxhash-dev
6566
6667
- name: Install libsodium (>=1.0.19) from source
6768
run: |

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ target_link_libraries(tui-server
4949
sqlite3
5050
uuid
5151
sodium
52-
zstd)
52+
zstd
53+
xxhash)
5354

5455
add_executable(tui-register
5556
${CMAKE_CURRENT_SOURCE_DIR}/src/tui-register.cpp
@@ -66,7 +67,8 @@ target_include_directories(tui-register
6667
target_link_libraries(tui-register
6768
PRIVATE
6869
sqlite3
69-
uuid)
70+
uuid
71+
xxhash)
7072

7173
install(TARGETS tui-server RUNTIME DESTINATION bin)
7274
install(TARGETS tui-register RUNTIME DESTINATION bin)

src/apiProvider/AzureOpenAI.cpp

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void AzureOpenAI::Initialize(const nlohmann::json& params)
4848
_params = ParamsDefinition.Parse(params);
4949
}
5050

51-
RequestData AzureOpenAI::FormatRequest(const Schema::IServer::LinearHistory& history, bool stream) const
51+
RequestData AzureOpenAI::FormatRequest(const Schema::IServer::LinearHistory& history, bool stream, const std::optional<std::vector<Schema::IServer::Tool>>& /*tools*/) const
5252
{
5353
RequestData data{};
5454
data.url = _params.url;
@@ -67,55 +67,64 @@ RequestData AzureOpenAI::FormatRequest(const Schema::IServer::LinearHistory& his
6767
}
6868
for (const auto& message : history)
6969
{
70-
auto messageJson = nlohmann::json::object();
71-
messageJson["content"] = nlohmann::json::array();
72-
if (message.get_role() == Schema::IServer::MessageRole::DEVELOPER)
70+
if (std::holds_alternative<Schema::IServer::ChatMessage>(message))
7371
{
74-
messageJson["role"] = "system";
75-
}
76-
else if(message.get_role() == Schema::IServer::MessageRole::USER)
77-
{
78-
messageJson["role"] = "user";
79-
}
80-
else if(message.get_role() == Schema::IServer::MessageRole::ASSISTANT)
81-
{
82-
messageJson["role"] = "assistant";
83-
}
84-
else
85-
{
86-
continue;
87-
}
88-
for (const auto& content : message.get_content())
89-
{
90-
if (content.get_type() == Schema::IServer::Type::TEXT || content.get_type() == Schema::IServer::Type::REFUSAL)
72+
const auto& chatMessage = std::get<Schema::IServer::ChatMessage>(message);
73+
auto messageJson = nlohmann::json::object();
74+
messageJson["content"] = nlohmann::json::array();
75+
if (chatMessage.get_role() == Schema::IServer::ChatMessageRole::DEVELOPER)
76+
{
77+
messageJson["role"] = "system";
78+
}
79+
else if(chatMessage.get_role() == Schema::IServer::ChatMessageRole::USER)
9180
{
92-
/** Azure open ai does not seem to have a special REFUSAL message type */
93-
messageJson["content"].push_back({
94-
{"type", "text"},
95-
{"text", content.get_data()}
96-
});
81+
messageJson["role"] = "user";
9782
}
98-
else if (content.get_type() == Schema::IServer::Type::IMAGE_URL)
83+
else if(chatMessage.get_role() == Schema::IServer::ChatMessageRole::ASSISTANT)
9984
{
100-
messageJson["content"].push_back({
101-
{"type", "image_url"},
102-
{"image_url", {
103-
{"url", content.get_data()}
104-
}}
105-
});
85+
messageJson["role"] = "assistant";
10686
}
10787
else
10888
{
10989
continue;
11090
}
91+
for (const auto& content : chatMessage.get_content())
92+
{
93+
if (content.get_type() == Schema::IServer::MessageContentType::TEXT || content.get_type() == Schema::IServer::MessageContentType::REFUSAL)
94+
{
95+
/** Azure open ai does not seem to have a special REFUSAL message type */
96+
messageJson["content"].push_back({
97+
{"type", "text"},
98+
{"text", content.get_data()}
99+
});
100+
}
101+
else if (content.get_type() == Schema::IServer::MessageContentType::IMAGE_URL)
102+
{
103+
messageJson["content"].push_back({
104+
{"type", "image_url"},
105+
{"image_url", {
106+
{"url", content.get_data()}
107+
}}
108+
});
109+
}
110+
else
111+
{
112+
continue;
113+
}
114+
}
115+
body["messages"].push_back(messageJson);
116+
}
117+
else if (std::holds_alternative<Schema::IServer::FunctionCallMessage>(message)
118+
|| std::holds_alternative<Schema::IServer::FunctionCallOutputMessage>(message))
119+
{
120+
throw std::runtime_error("AzureOpenAI does not support function call messages");
111121
}
112-
body["messages"].push_back(messageJson);
113122
}
114123
data.body = body.dump();
115124
return data;
116125
}
117126

118-
Schema::IServer::MessageContent AzureOpenAI::ParseResponse(const std::string& responseString) const
127+
Schema::IServer::LinearHistory AzureOpenAI::ParseResponse(const std::string& responseString) const
119128
{
120129
Schema::AzureOpenAI::BulkResponse response;
121130
try
@@ -131,14 +140,17 @@ Schema::IServer::MessageContent AzureOpenAI::ParseResponse(const std::string& re
131140
throw std::runtime_error("No choices in response");
132141
}
133142
const auto& choice = response.get_choices().front();
134-
const auto& message = choice.get_message();
143+
const auto& msg = choice.get_message();
135144
Schema::IServer::MessageContent content;
136-
content.set_type(message.get_refusal() ? Schema::IServer::Type::REFUSAL : Schema::IServer::Type::TEXT);
137-
content.set_data(message.get_content());
138-
return content;
145+
content.set_type(msg.get_refusal() ? Schema::IServer::MessageContentType::REFUSAL : Schema::IServer::MessageContentType::TEXT);
146+
content.set_data(msg.get_content());
147+
Schema::IServer::ChatMessage chatMessage;
148+
chatMessage.set_role(Schema::IServer::ChatMessageRole::ASSISTANT);
149+
chatMessage.set_content({content});
150+
return Schema::IServer::LinearHistory{std::move(chatMessage)};
139151
}
140152

141-
std::optional<Schema::IServer::MessageContent> AzureOpenAI::ParseStreamResponse(const StreamResponse::Event& event) const
153+
std::optional<Schema::IServer::ChatCompletionSegment> AzureOpenAI::ParseStreamResponse(const StreamResponse::Event& event) const
142154
{
143155
if (!event.value.has_value())
144156
{
@@ -172,8 +184,5 @@ std::optional<Schema::IServer::MessageContent> AzureOpenAI::ParseStreamResponse(
172184
{
173185
return std::nullopt;
174186
}
175-
Schema::IServer::MessageContent content{};
176-
content.set_type(Schema::IServer::Type::TEXT);
177-
content.set_data(delta.get_content().value());
178-
return content;
187+
return delta.get_content().value();
179188
}

src/apiProvider/AzureOpenAI.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ namespace TUI::ApiProvider
1212
~AzureOpenAI() override = default;
1313
nlohmann::json GetParams() const override;
1414
void Initialize(const nlohmann::json& params) override;
15-
Network::Http::RequestData FormatRequest(const Schema::IServer::LinearHistory& history, bool stream) const override;
16-
Schema::IServer::MessageContent ParseResponse(const std::string& response) const override;
17-
std::optional<Schema::IServer::MessageContent> ParseStreamResponse(const Network::Http::StreamResponse::Event& event) const override;
15+
Network::Http::RequestData FormatRequest(
16+
const Schema::IServer::LinearHistory& history,
17+
bool stream,
18+
const std::optional<std::vector<Schema::IServer::Tool>>& tools = std::nullopt) const override;
19+
Schema::IServer::LinearHistory ParseResponse(const std::string& response) const override;
20+
std::optional<Schema::IServer::ChatCompletionSegment> ParseStreamResponse(const Network::Http::StreamResponse::Event& event) const override;
1821
private:
1922
struct Params
2023
{

src/apiProvider/IProvider.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ namespace TUI::ApiProvider
2222
virtual ~IProvider() = default;
2323
virtual nlohmann::json GetParams() const = 0;
2424
virtual void Initialize(const nlohmann::json& params) = 0;
25-
virtual Network::Http::RequestData FormatRequest(const Schema::IServer::LinearHistory& history, bool stream) const = 0;
26-
virtual Schema::IServer::MessageContent ParseResponse(const std::string& response) const = 0;
27-
virtual std::optional<Schema::IServer::MessageContent> ParseStreamResponse(const Network::Http::StreamResponse::Event& event) const = 0;
25+
virtual Network::Http::RequestData FormatRequest(
26+
const Schema::IServer::LinearHistory& history,
27+
bool stream,
28+
const std::optional<std::vector<Schema::IServer::Tool>>& tools = std::nullopt) const = 0;
29+
virtual Schema::IServer::LinearHistory ParseResponse(const std::string& response) const = 0;
30+
virtual std::optional<Schema::IServer::ChatCompletionSegment> ParseStreamResponse(const Network::Http::StreamResponse::Event& event) const = 0;
2831
};
2932
}

0 commit comments

Comments
 (0)