#pragma once // POST /chat/completions: the OpenAI-compatible messages-array endpoint for // the Muse Spark and Muse Wonder text models. It does not carry reasoning // across turns (reasoning_content is redacted for external keys); use the // Responses API for that. Rules enforced before any traffic (docs: // protocols/chat-completions): n is always 1, stop / logit_bias / prediction / // verbosity / web_search_options / modalities are not offered, tool_choice is // only "auto", reasoning_effort "none" is rejected, response_format and tools // pass the schema pre-flight. #include #include #include #include #include "llm/api_client.h" #include "meta/jsonx.h" #include "meta/usage.h" namespace molo::meta { class ChatMessage { public: static Json developer(const std::string& text); static Json system(const std::string& text); // same level as developer static Json user(const std::string& text); static Json user_parts(const std::vector& parts); static Json assistant(const std::string& text); static Json assistant_tool_calls(const std::string& text, const std::vector& tool_calls); static Json tool_result(const std::string& tool_call_id, const std::string& content); // Content parts. static Json text_part(const std::string& text); static Json image_part(const std::string& url, const std::string& detail = ""); static Json video_part(const std::string& url, double fps = 0); static Json file_part(const std::string& filename, const std::string& file_data_url, const std::string& file_id = ""); static Json audio_part(const std::string& format, const std::string& base64_data); // {id, type:"function", function:{name, arguments}} static Json tool_call(const std::string& id, const std::string& name, const std::string& arguments_json); // Tool definition: {type:"function", function:{name, description, parameters, strict}} static Json function_tool(const std::string& name, const std::string& description, const Json& parameters, bool strict = false); }; struct ChatCompletionRequest { std::string model; std::vector messages; std::vector tools; Json response_format; // null, or {type: json_object | json_schema, json_schema:{...}} std::string reasoning_effort; // minimal..max std::optional max_completion_tokens; std::optional temperature; std::optional top_p; std::optional frequency_penalty; std::optional presence_penalty; std::optional seed; std::optional parallel_tool_calls; std::string safety_identifier; std::string prompt_cache_key; std::string prompt_cache_retention; std::string service_tier; bool include_usage = true; // stream_options.include_usage when streaming std::vector> metadata; Json extra; void validate() const; Json to_json(bool stream = false) const; static Json format_json_object(); static Json format_json_schema(const std::string& name, const Json& schema, bool strict = false, const std::string& description = ""); }; struct ChatToolCallState { long index = 0; std::string id, name, arguments; }; // Folds chat.completion.chunk objects into text, reasoning_content, tool calls, // finish_reason and usage. class ChatChunkAccumulator { public: void feed(const Json& chunk); const std::string& text() const { return text_; } const std::string& reasoning() const { return reasoning_; } const std::string& finish_reason() const { return finish_reason_; } const std::string& id() const { return id_; } const std::string& model() const { return model_; } const std::vector& tool_calls() const { return calls_; } bool has_usage() const { return has_usage_; } MetaUsage usage() const { return usage_; } private: std::string text_, reasoning_, finish_reason_, id_, model_; std::vector calls_; MetaUsage usage_; bool has_usage_ = false; }; class ChatCompletionsService : public llm::Service { public: using ChunkSink = std::function; // false: stop explicit ChatCompletionsService(std::shared_ptr client) : llm::Service(std::move(client)) {} Json create(const ChatCompletionRequest& request); ChatChunkAccumulator stream(const ChatCompletionRequest& request, const ChunkSink& sink = nullptr); }; } // namespace molo::meta