11#include "absl/strings/match.h"
12#include "absl/strings/str_cat.h"
13#include "absl/strings/str_format.h"
14#include "absl/strings/str_split.h"
15#include "absl/strings/strip.h"
16#include "absl/time/clock.h"
17#include "absl/time/time.h"
25#include <TargetConditionals.h>
28#if defined(__APPLE__) && \
29 (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
31#define YAZE_AI_IOS_URLSESSION 1
39#include "nlohmann/json.hpp"
42#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
43#include <openssl/crypto.h>
44#include <openssl/err.h>
45#include <openssl/ssl.h>
48static std::atomic<bool> g_openssl_initialized{
false};
49static std::mutex g_openssl_init_mutex;
51static void EnsureOpenSSLInitialized() {
52 std::lock_guard<std::mutex> lock(g_openssl_init_mutex);
53 if (!g_openssl_initialized.exchange(
true)) {
55 OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS,
57 std::cerr <<
"✓ OpenSSL initialized for HTTPS support" << std::endl;
66#ifdef YAZE_AI_RUNTIME_AVAILABLE
70absl::StatusOr<nlohmann::json> BuildOpenAIToolPayload(
71 const PromptBuilder& prompt_builder) {
72 auto declarations_or =
74 if (!declarations_or.ok()) {
75 return declarations_or.status();
83 : function_calling_enabled_(config.use_function_calling), config_(config) {
84 if (config_.verbose) {
85 std::cerr <<
"[DEBUG] Initializing OpenAI service..." << std::endl;
86 std::cerr <<
"[DEBUG] Model: " << config_.model << std::endl;
87 std::cerr <<
"[DEBUG] Function calling: "
88 << (function_calling_enabled_ ?
"enabled" :
"disabled")
92#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
93 EnsureOpenSSLInitialized();
94 if (config_.verbose) {
95 std::cerr <<
"[DEBUG] OpenSSL initialized for HTTPS" << std::endl;
100 std::string catalogue_path = config_.prompt_version ==
"v2"
101 ?
"assets/agent/prompt_catalogue_v2.yaml"
102 :
"assets/agent/prompt_catalogue.yaml";
103 if (
auto status = prompt_builder_.LoadResourceCatalogue(catalogue_path);
105 std::cerr <<
"⚠️ Failed to load agent prompt catalogue: "
106 << status.message() << std::endl;
109 if (config_.system_instruction.empty()) {
111 std::string prompt_file;
112 if (config_.prompt_version ==
"v3") {
113 prompt_file =
"agent/system_prompt_v3.txt";
114 }
else if (config_.prompt_version ==
"v2") {
115 prompt_file =
"agent/system_prompt_v2.txt";
117 prompt_file =
"agent/system_prompt.txt";
120 auto prompt_path = util::PlatformPaths::FindAsset(prompt_file);
121 if (prompt_path.ok()) {
122 std::ifstream file(prompt_path->string());
124 std::stringstream buffer;
125 buffer << file.rdbuf();
126 config_.system_instruction = buffer.str();
127 if (config_.verbose) {
128 std::cerr <<
"[DEBUG] Loaded prompt: " << prompt_path->string()
134 if (config_.system_instruction.empty()) {
135 config_.system_instruction = BuildSystemInstruction();
139 if (config_.verbose) {
140 std::cerr <<
"[DEBUG] OpenAI service initialized" << std::endl;
144void OpenAIAIService::EnableFunctionCalling(
bool enable) {
145 function_calling_enabled_ = enable;
148std::vector<std::string> OpenAIAIService::GetAvailableTools()
const {
149 return {
"resource-list",
"resource-search",
150 "dungeon-list-sprites",
"dungeon-describe-room",
151 "overworld-find-tile",
"overworld-describe-map",
152 "overworld-list-warps"};
155std::string OpenAIAIService::BuildSystemInstruction() {
156 return prompt_builder_.BuildSystemInstruction();
159void OpenAIAIService::SetRomContext(Rom* rom) {
160 prompt_builder_.SetRom(rom);
163absl::StatusOr<std::vector<ModelInfo>> OpenAIAIService::ListAvailableModels() {
164#ifndef YAZE_WITH_JSON
165 return absl::UnimplementedError(
"OpenAI AI service requires JSON support");
167 const bool is_openai_cloud =
168 absl::StrContains(config_.base_url,
"api.openai.com");
169 if (config_.api_key.empty() && is_openai_cloud) {
171 std::vector<ModelInfo> defaults = {
173 .display_name =
"GPT-4o",
175 .description =
"Most capable GPT-4 model"},
176 {.name =
"gpt-4o-mini",
177 .display_name =
"GPT-4o Mini",
179 .description =
"Fast and cost-effective"},
180 {.name =
"gpt-4-turbo",
181 .display_name =
"GPT-4 Turbo",
183 .description =
"GPT-4 with larger context"},
184 {.name =
"gpt-3.5-turbo",
185 .display_name =
"GPT-3.5 Turbo",
187 .description =
"Fast and efficient"}};
192 if (config_.verbose) {
193 std::cerr <<
"[DEBUG] Listing OpenAI models..." << std::endl;
196 std::string response_str;
197#if defined(YAZE_AI_IOS_URLSESSION)
198 std::map<std::string, std::string> headers;
199 if (!config_.api_key.empty()) {
200 headers.emplace(
"Authorization",
"Bearer " + config_.api_key);
202 auto resp_or = ios::UrlSessionHttpRequest(
203 "GET", config_.base_url +
"/v1/models", headers,
"", 8000);
205 if (config_.verbose) {
206 std::cerr <<
"[DEBUG] OpenAI /v1/models failed: "
207 << resp_or.status().message() << std::endl;
210 std::vector<ModelInfo> defaults = {{.name =
"gpt-4o-mini",
211 .display_name =
"GPT-4o Mini",
214 .display_name =
"GPT-4o",
216 {.name =
"gpt-3.5-turbo",
217 .display_name =
"GPT-3.5 Turbo",
221 if (resp_or->status_code != 200) {
222 if (config_.verbose) {
223 std::cerr <<
"[DEBUG] OpenAI /v1/models HTTP " << resp_or->status_code
226 std::vector<ModelInfo> defaults = {{.name =
"gpt-4o-mini",
227 .display_name =
"GPT-4o Mini",
230 .display_name =
"GPT-4o",
232 {.name =
"gpt-3.5-turbo",
233 .display_name =
"GPT-3.5 Turbo",
237 response_str = resp_or->body;
240 std::string auth_header =
241 config_.api_key.empty()
243 :
"-H 'Authorization: Bearer " + config_.api_key +
"' ";
244 std::string curl_cmd =
"curl -s -X GET '" + config_.base_url +
245 "/v1/models' " + auth_header +
"2>&1";
248 FILE* pipe = _popen(curl_cmd.c_str(),
"r");
250 FILE* pipe = popen(curl_cmd.c_str(),
"r");
253 return absl::InternalError(
"Failed to execute curl command");
257 while (fgets(buffer,
sizeof(buffer), pipe) !=
nullptr) {
258 response_str += buffer;
268 auto models_json = nlohmann::json::parse(response_str,
nullptr,
false);
269 if (models_json.is_discarded()) {
270 return absl::InternalError(
"Failed to parse OpenAI models JSON");
273 if (!models_json.contains(
"data")) {
275 std::vector<ModelInfo> defaults = {{.name =
"gpt-4o-mini",
276 .display_name =
"GPT-4o Mini",
279 .display_name =
"GPT-4o",
281 {.name =
"gpt-3.5-turbo",
282 .display_name =
"GPT-3.5 Turbo",
287 std::vector<ModelInfo> models;
288 for (
const auto& m : models_json[
"data"]) {
289 std::string
id = m.value(
"id",
"");
293 bool is_local = !absl::StrContains(config_.base_url,
"api.openai.com");
295 if (is_local || absl::StartsWith(
id,
"gpt-4") ||
296 absl::StartsWith(
id,
"gpt-3.5") || absl::StartsWith(
id,
"o1") ||
297 absl::StartsWith(
id,
"chatgpt")) {
300 info.display_name = id;
302 info.family = is_local ?
"local" :
"gpt";
303 info.is_local = is_local;
307 info.display_name =
"GPT-4o";
308 else if (
id ==
"gpt-4o-mini")
309 info.display_name =
"GPT-4o Mini";
310 else if (
id ==
"gpt-4-turbo")
311 info.display_name =
"GPT-4 Turbo";
312 else if (
id ==
"gpt-3.5-turbo")
313 info.display_name =
"GPT-3.5 Turbo";
314 else if (
id ==
"o1-preview")
315 info.display_name =
"o1 Preview";
316 else if (
id ==
"o1-mini")
317 info.display_name =
"o1 Mini";
319 models.push_back(std::move(info));
324 }
catch (
const std::exception& e) {
325 return absl::InternalError(
326 absl::StrCat(
"Failed to list models: ", e.what()));
331absl::Status OpenAIAIService::CheckAvailability() {
332#ifndef YAZE_WITH_JSON
333 return absl::UnimplementedError(
334 "OpenAI AI service requires JSON support. Build with "
335 "-DYAZE_WITH_JSON=ON");
339 bool is_local_server = config_.base_url !=
"https://api.openai.com";
340 if (config_.api_key.empty() && !is_local_server) {
341 return absl::FailedPreconditionError(
342 "❌ OpenAI API key not configured\n"
343 " Set OPENAI_API_KEY environment variable\n"
344 " Get your API key at: https://platform.openai.com/api-keys\n"
345 " For LMStudio, use --openai_base_url=http://localhost:1234");
349#if defined(YAZE_AI_IOS_URLSESSION)
350 std::map<std::string, std::string> headers;
351 if (!config_.api_key.empty()) {
352 headers.emplace(
"Authorization",
"Bearer " + config_.api_key);
354 auto resp_or = ios::UrlSessionHttpRequest(
355 "GET", config_.base_url +
"/v1/models", headers,
"", 8000);
357 return absl::UnavailableError(absl::StrCat(
358 "❌ Cannot reach OpenAI API\n ", resp_or.status().message()));
360 if (resp_or->status_code == 401) {
361 return absl::PermissionDeniedError(
362 "❌ Invalid OpenAI API key\n"
363 " Verify your key at: https://platform.openai.com/api-keys");
365 if (resp_or->status_code != 200) {
366 return absl::InternalError(
367 absl::StrCat(
"❌ OpenAI API error: ", resp_or->status_code,
"\n ",
371 httplib::Client cli(config_.base_url);
372 cli.set_connection_timeout(5, 0);
374 httplib::Headers headers = {};
375 if (!config_.api_key.empty()) {
376 headers.emplace(
"Authorization",
"Bearer " + config_.api_key);
379 auto res = cli.Get(
"/v1/models", headers);
382 return absl::UnavailableError(
383 "❌ Cannot reach OpenAI API\n"
384 " Check your internet connection");
387 if (res->status == 401) {
388 return absl::PermissionDeniedError(
389 "❌ Invalid OpenAI API key\n"
390 " Verify your key at: https://platform.openai.com/api-keys");
393 if (res->status != 200) {
394 return absl::InternalError(absl::StrCat(
395 "❌ OpenAI API error: ", res->status,
"\n ", res->body));
399 return absl::OkStatus();
400 }
catch (
const std::exception& e) {
401 return absl::InternalError(
402 absl::StrCat(
"Exception during availability check: ", e.what()));
407absl::StatusOr<AgentResponse> OpenAIAIService::GenerateResponse(
408 const std::string& prompt) {
409 return GenerateResponse(
410 {{{agent::ChatMessage::Sender::kUser, prompt, absl::Now()}}});
413absl::StatusOr<AgentResponse> OpenAIAIService::GenerateResponse(
414 const std::vector<agent::ChatMessage>& history) {
415#ifndef YAZE_WITH_JSON
416 return absl::UnimplementedError(
417 "OpenAI AI service requires JSON support. Build with "
418 "-DYAZE_WITH_JSON=ON");
420 if (history.empty()) {
421 return absl::InvalidArgumentError(
"History cannot be empty.");
424 const bool is_openai_cloud =
425 absl::StrContains(config_.base_url,
"api.openai.com");
426 if (config_.api_key.empty() && is_openai_cloud) {
427 return absl::FailedPreconditionError(
"OpenAI API key not configured");
430 absl::Time request_start = absl::Now();
433 if (config_.verbose) {
434 std::cerr <<
"[DEBUG] Using curl for OpenAI HTTPS request" << std::endl;
435 std::cerr <<
"[DEBUG] Processing " << history.size()
436 <<
" messages in history" << std::endl;
440 nlohmann::json messages = nlohmann::json::array();
444 {{
"role",
"system"}, {
"content", config_.system_instruction}});
447 int start_idx = std::max(0,
static_cast<int>(history.size()) - 10);
448 for (
size_t i = start_idx; i < history.size(); ++i) {
449 const auto& msg = history[i];
450 std::string role = (msg.sender == agent::ChatMessage::Sender::kUser)
454 messages.push_back({{
"role", role}, {
"content", msg.message}});
458 nlohmann::json request_body = {{
"model", config_.model},
459 {
"messages", messages},
460 {
"temperature", config_.temperature},
461 {
"max_tokens", config_.max_output_tokens}};
464 if (function_calling_enabled_) {
465 auto tools_or = BuildOpenAIToolPayload(prompt_builder_);
466 if (!tools_or.ok()) {
467 if (config_.verbose) {
468 std::cerr <<
"[DEBUG] Function calling schemas unavailable: "
469 << tools_or.status().message() << std::endl;
471 }
else if (!tools_or->empty()) {
472 if (config_.verbose) {
473 std::string tools_str = tools_or->dump();
474 std::cerr <<
"[DEBUG] Function calling schemas: "
475 << tools_str.substr(0, 200) <<
"..." << std::endl;
478 request_body[
"tools"] = *tools_or;
482 if (config_.verbose) {
483 std::cerr <<
"[DEBUG] Sending " << messages.size()
484 <<
" messages to OpenAI" << std::endl;
487 std::string response_str;
488#if defined(YAZE_AI_IOS_URLSESSION)
489 std::map<std::string, std::string> headers;
490 headers.emplace(
"Content-Type",
"application/json");
491 if (!config_.api_key.empty()) {
492 headers.emplace(
"Authorization",
"Bearer " + config_.api_key);
494 auto resp_or = ios::UrlSessionHttpRequest(
495 "POST", config_.base_url +
"/v1/chat/completions", headers,
496 request_body.dump(), 60000);
498 return resp_or.status();
500 if (resp_or->status_code == 401) {
501 return absl::PermissionDeniedError(
502 "❌ Invalid OpenAI API key\n"
503 " Verify your key at: https://platform.openai.com/api-keys");
505 if (resp_or->status_code != 200) {
506 return absl::InternalError(
507 absl::StrCat(
"❌ OpenAI API error: ", resp_or->status_code,
"\n ",
510 response_str = resp_or->body;
513 std::string temp_file =
"/tmp/openai_request.json";
514 std::ofstream out(temp_file);
515 out << request_body.dump();
519 std::string auth_header =
520 config_.api_key.empty()
522 :
"-H 'Authorization: Bearer " + config_.api_key +
"' ";
523 std::string curl_cmd =
"curl -s -X POST '" + config_.base_url +
524 "/v1/chat/completions' "
525 "-H 'Content-Type: application/json' " +
526 auth_header +
"-d @" + temp_file +
" 2>&1";
528 if (config_.verbose) {
529 std::cerr <<
"[DEBUG] Executing OpenAI API request..." << std::endl;
533 FILE* pipe = _popen(curl_cmd.c_str(),
"r");
535 FILE* pipe = popen(curl_cmd.c_str(),
"r");
538 return absl::InternalError(
"Failed to execute curl command");
542 while (fgets(buffer,
sizeof(buffer), pipe) !=
nullptr) {
543 response_str += buffer;
547 int status = _pclose(pipe);
549 int status = pclose(pipe);
551 std::remove(temp_file.c_str());
554 return absl::InternalError(
555 absl::StrCat(
"Curl failed with status ", status));
559 if (response_str.empty()) {
560 return absl::InternalError(
"Empty response from OpenAI API");
563 if (config_.verbose) {
566 <<
"🔍 Raw OpenAI API Response:"
569 <<
"\033[2m" << response_str.substr(0, 500) <<
"\033[0m"
573 if (config_.verbose) {
574 std::cerr <<
"[DEBUG] Parsing response..." << std::endl;
577 auto parsed_or = ParseOpenAIResponse(response_str);
578 if (!parsed_or.ok()) {
579 return parsed_or.status();
582 AgentResponse agent_response = std::move(parsed_or.value());
584 agent_response.model = config_.model;
585 agent_response.latency_seconds =
586 absl::ToDoubleSeconds(absl::Now() - request_start);
587 agent_response.parameters[
"prompt_version"] = config_.prompt_version;
588 agent_response.parameters[
"temperature"] =
589 absl::StrFormat(
"%.2f", config_.temperature);
590 agent_response.parameters[
"max_output_tokens"] =
591 absl::StrFormat(
"%d", config_.max_output_tokens);
592 agent_response.parameters[
"function_calling"] =
593 function_calling_enabled_ ?
"true" :
"false";
595 return agent_response;
597 }
catch (
const std::exception& e) {
598 if (config_.verbose) {
599 std::cerr <<
"[ERROR] Exception: " << e.what() << std::endl;
601 return absl::InternalError(
602 absl::StrCat(
"Exception during generation: ", e.what()));
607absl::StatusOr<AgentResponse> OpenAIAIService::ParseOpenAIResponse(
608 const std::string& response_body) {
609#ifndef YAZE_WITH_JSON
610 return absl::UnimplementedError(
"JSON support required");
612 AgentResponse agent_response;
614 auto response_json = nlohmann::json::parse(response_body,
nullptr,
false);
615 if (response_json.is_discarded()) {
616 return absl::InternalError(
"❌ Failed to parse OpenAI response JSON");
620 if (response_json.contains(
"error")) {
621 std::string error_msg =
622 response_json[
"error"].value(
"message",
"Unknown error");
623 return absl::InternalError(
624 absl::StrCat(
"❌ OpenAI API error: ", error_msg));
628 if (!response_json.contains(
"choices") || response_json[
"choices"].empty()) {
629 return absl::InternalError(
"❌ No choices in OpenAI response");
632 const auto& choice = response_json[
"choices"][0];
633 if (!choice.contains(
"message")) {
634 return absl::InternalError(
"❌ No message in OpenAI response");
637 const auto& message = choice[
"message"];
640 if (message.contains(
"content") && !message[
"content"].is_null()) {
641 std::string text_content = message[
"content"].get<std::string>();
643 if (config_.verbose) {
646 <<
"🔍 Raw LLM Response:"
649 <<
"\033[2m" << text_content <<
"\033[0m"
654 text_content = std::string(absl::StripAsciiWhitespace(text_content));
655 if (absl::StartsWith(text_content,
"```json")) {
656 text_content = text_content.substr(7);
657 }
else if (absl::StartsWith(text_content,
"```")) {
658 text_content = text_content.substr(3);
660 if (absl::EndsWith(text_content,
"```")) {
661 text_content = text_content.substr(0, text_content.length() - 3);
663 text_content = std::string(absl::StripAsciiWhitespace(text_content));
666 auto parsed_text = nlohmann::json::parse(text_content,
nullptr,
false);
667 if (!parsed_text.is_discarded()) {
669 if (parsed_text.contains(
"text_response") &&
670 parsed_text[
"text_response"].is_string()) {
671 agent_response.text_response =
672 parsed_text[
"text_response"].get<std::string>();
676 if (parsed_text.contains(
"reasoning") &&
677 parsed_text[
"reasoning"].is_string()) {
678 agent_response.reasoning = parsed_text[
"reasoning"].get<std::string>();
682 if (parsed_text.contains(
"commands") &&
683 parsed_text[
"commands"].is_array()) {
684 for (
const auto& cmd : parsed_text[
"commands"]) {
685 if (cmd.is_string()) {
686 std::string command = cmd.get<std::string>();
687 if (absl::StartsWith(command,
"z3ed ")) {
688 command = command.substr(5);
690 agent_response.commands.push_back(command);
696 if (parsed_text.contains(
"tool_calls") &&
697 parsed_text[
"tool_calls"].is_array()) {
698 for (
const auto& call : parsed_text[
"tool_calls"]) {
699 if (call.contains(
"tool_name") && call[
"tool_name"].is_string()) {
701 tool_call.tool_name = call[
"tool_name"].get<std::string>();
702 if (call.contains(
"args") && call[
"args"].is_object()) {
703 tool_call.args = ai::DecodeToolCallArguments(call[
"args"]);
705 agent_response.tool_calls.push_back(tool_call);
711 agent_response.text_response = text_content;
716 if (message.contains(
"tool_calls") && message[
"tool_calls"].is_array()) {
717 for (
const auto& call : message[
"tool_calls"]) {
718 if (call.contains(
"function")) {
719 const auto& func = call[
"function"];
721 tool_call.tool_name = func.value(
"name",
"");
723 if (func.contains(
"arguments") && func[
"arguments"].is_string()) {
724 auto args_json = nlohmann::json::parse(
725 func[
"arguments"].get<std::string>(),
nullptr,
false);
726 if (!args_json.is_discarded() && args_json.is_object()) {
727 tool_call.args = ai::DecodeToolCallArguments(args_json);
730 agent_response.tool_calls.push_back(tool_call);
735 if (agent_response.text_response.empty() && agent_response.commands.empty() &&
736 agent_response.tool_calls.empty()) {
737 return absl::InternalError(
738 "❌ No valid response extracted from OpenAI\n"
739 " Expected at least one of: text_response, commands, or tool_calls");
742 return agent_response;
OpenAIAIService(const OpenAIConfig &)
constexpr char kProviderOpenAi[]