Entropic 2.11.1
Local-first agentic inference engine
Loading...
Searching...
No Matches
external_bridge.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
29#pragma once
30
33
34#include <nlohmann/json_fwd.hpp>
35
36#include <atomic>
37#include <chrono>
38#include <filesystem>
39#include <memory>
40#include <mutex>
41#include <string>
42#include <thread>
43#include <unordered_map>
44#include <unordered_set>
45#include <vector>
46
47// Forward declaration — bridge holds a raw pointer to the opaque handle.
48struct entropic_engine;
50
51namespace entropic {
52
68class ENTROPIC_EXPORT ExternalBridge {
69public:
78 entropic_handle_t handle,
79 const ExternalMCPConfig& config,
80 const std::filesystem::path& project_dir);
81
83
89 bool start();
90
95 void stop();
96
103 const std::filesystem::path& socket_path() const { return socket_path_; }
104
117 bool ask_streaming() const { return config_.ask_streaming; }
118
126 nlohmann::json handle_ask_status(const nlohmann::json& args);
127
149 void attach_phase_observer(const std::string& task_id);
150
156 void detach_phase_observer();
157
158 void run_async_ask(const std::string& prompt,
159 const std::string& task_id,
160 int client_fd);
161
182 void write_sentinel(const std::string& task_id,
183 const std::string& status);
184
200 std::filesystem::path async_sentinel_dir() const;
201
215 void set_async_sentinel_root(const std::filesystem::path& root);
216
222 void cleanup_expired_tasks();
223
225 mutable std::mutex tasks_mutex_;
226
236 struct AsyncTask {
237 std::string status = "queued";
238 std::string phase = "queued";
239 std::string result;
240 std::chrono::steady_clock::time_point created;
241 };
242
255 std::unordered_map<std::string, AsyncTask>& tasks_for_cancel() {
256 return tasks_;
257 }
258
265 const std::string& active_task_id_for_observer() const {
266 return active_task_id_;
267 }
268
275 void subscribe(int fd);
276
283 void unsubscribe(int fd);
284
296 void broadcast_notification(const nlohmann::json& notif);
297
304 size_t subscriber_count() const {
305 std::lock_guard<std::mutex> lock(subscribers_mutex_);
306 return subscribers_.size();
307 }
308
322 void update_task_phase(const std::string& task_id,
323 const std::string& status,
324 const std::string& phase);
325
340 return observer_gen_.load() != attached_gen_;
341 }
342
343private:
349 void accept_loop();
350
357 void serve_client(int client_fd);
358
367 std::string dispatch(const std::string& request, int client_fd);
368
379 void reap_finished_clients_locked();
380
381 entropic_handle_t handle_;
382 ExternalMCPConfig config_;
383 std::filesystem::path socket_path_;
384 std::string bound_canonical_;
385 int listen_fd_ = -1;
386 std::atomic<bool> running_{false};
387 std::thread accept_thread_;
388
397 struct ClientThread {
398 int fd;
399 std::atomic<bool> finished{false};
400 std::thread thread;
401 };
402 std::vector<std::unique_ptr<ClientThread>> client_threads_;
403 mutable std::mutex client_threads_mutex_;
404
406 std::unordered_map<std::string, AsyncTask> tasks_;
407
413 std::unordered_set<int> subscribers_;
414 mutable std::mutex subscribers_mutex_;
415
421 std::string active_task_id_;
422
427 std::atomic<uint64_t> observer_gen_{0};
428
432 uint64_t attached_gen_ = 0;
433
437 std::filesystem::path async_sentinel_root_override_;
438};
439
440} // namespace entropic
Unix socket MCP bridge for external client access.
std::mutex tasks_mutex_
Async task mutex (public for dispatch_tool access).
const std::string & active_task_id_for_observer() const
Return the currently-active async task_id (if any).
const std::filesystem::path & socket_path() const
Get the socket path (for logging/diagnostics).
bool ask_streaming() const
Whether entropic.ask routes through entropic_run_streaming.
bool observer_call_is_stale() const
Test whether an in-flight observer callback is stale.
std::unordered_map< std::string, AsyncTask > & tasks_for_cancel()
Mutable accessor to the task registry.
size_t subscriber_count() const
Current subscriber count (for diagnostics / testing).
Configuration structs with defaults.
Symbol visibility macro for all exported symbols.
Activate model on GPU (WARM → ACTIVE).
Async task state for background entropic.ask runs.
std::string result
Final text or error message.
std::chrono::steady_clock::time_point created
For TTL cleanup.
External MCP server configuration (Entropic-as-server).
Definition config.h:642
Engine handle struct — owns all subsystems.