Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
tool_executor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#pragma once
14
21
22#include <optional>
23#include <string>
24#include <vector>
25
26namespace entropic {
27
28/* ToolExecutorHooks moved to engine_types.h — v2.0.2 */
29
44
55public:
64 ToolExecutor(ServerManager& server_manager,
65 const LoopConfig& loop_config,
66 EngineCallbacks& callbacks,
67 ToolExecutorHooks hooks = {});
68
75
83 std::vector<Message> process_tool_calls(
84 LoopContext& ctx,
85 const std::vector<ToolCall>& tool_calls);
86
87private:
94 static std::vector<ToolCall> sort_tool_calls(
95 const std::vector<ToolCall>& calls);
96
103 void extract_and_process_directives(
104 LoopContext& ctx, const std::string& raw_result);
105
113 std::string check_duplicate(const LoopContext& ctx,
114 const ToolCall& call) const;
115
124 Message handle_duplicate(LoopContext& ctx,
125 const ToolCall& call,
126 const std::string& previous_result);
127
134 bool check_approval(const ToolCall& call);
135
143 std::optional<Message> check_tier_allowed(
144 const LoopContext& ctx, const ToolCall& call) const;
145
153 std::pair<Message, std::string> execute_tool(
154 LoopContext& ctx, const ToolCall& call);
155
169 void record_tool_history(const ToolCall& call,
170 const std::string& args_json,
171 const std::string& result_text,
172 long long ms, int iteration);
173
190 void finalize_tool_call(LoopContext& ctx, const ToolCall& call,
191 Message& msg, const std::string& raw_result,
192 double exec_ms);
193
204 void log_tool_call(LoopContext& ctx, const ToolCall& call,
205 double exec_ms, const std::string& raw_result,
206 ToolResultKind kind);
207
214 static std::string tool_call_key(const ToolCall& call);
215
223 void record_tool_call(LoopContext& ctx,
224 const ToolCall& call,
225 const std::string& result);
226
234 static Message create_denied_message(const ToolCall& call,
235 const std::string& reason);
236
244 static Message create_error_message(const ToolCall& call,
245 const std::string& error);
246
253 void fire_state_callback(const LoopContext& ctx);
254
262 void truncate_to_limit(std::vector<ToolCall>& calls, int limit) const;
263
272 PreconditionCheck check_call_preconditions(
273 LoopContext& ctx, const ToolCall& call);
274
283 PreconditionCheck check_approval_pc(
284 LoopContext& ctx, const ToolCall& call);
285
292 std::optional<Message> check_schema(const ToolCall& call);
293
302 std::optional<Message> check_dup_or_approval(
303 LoopContext& ctx, const ToolCall& call);
304
313 std::vector<Message> process_single_call(
314 LoopContext& ctx, const ToolCall& call);
315
324 bool should_stop_batch(const LoopContext& ctx,
325 const std::vector<Message>& results) const;
326
333 void run_post_tool_hooks(LoopContext& ctx);
334
341 static Message create_circuit_breaker_message();
342
351 static Message create_duplicate_message(
352 const ToolCall& call,
353 const std::string& previous_result);
354
362 static std::string serialize_args(const ToolCall& call);
363
371 static std::string serialize_tool_call(const ToolCall& call);
372
386 static std::string build_post_tool_json(
387 const ToolCall& call,
388 const std::string& raw_result,
389 double elapsed_ms,
390 const std::string& tier,
391 int iteration,
392 ToolResultKind kind);
393
413 void update_anti_spiral_tracking(LoopContext& ctx,
414 const std::string& tool_name);
415
429 int effective_hard_block_threshold() const;
430
448 PreconditionCheck check_anti_spiral_hard_block(
449 const LoopContext& ctx, const ToolCall& call) const;
450
468 void apply_result_size_cap(std::string& content) const;
469
479 static std::string build_pre_tool_json(
480 const ToolCall& call,
481 const std::string& tier,
482 int iteration);
483
511 void fire_post_tool_hook(const LoopContext& ctx,
512 const ToolCall& call,
513 const std::string& raw_result,
514 double elapsed_ms,
515 ToolResultKind kind,
516 Message& msg);
517
526 bool fire_pre_tool_hook(const LoopContext& ctx,
527 const ToolCall& call);
528
537 void fire_tool_complete_callback(const ToolCall& call,
538 const std::string& result,
539 long long ms);
540
541 ServerManager& server_manager_;
542 const LoopConfig& loop_config_;
543 EngineCallbacks& callbacks_;
544 ToolExecutorHooks hooks_;
545 PermissionPersistInterface permission_persist_;
546 HookInterface hook_iface_;
547 ToolCallHistory history_{100};
548 std::atomic<size_t> history_seq_{0};
549
550public:
558 const ToolCallHistory& tool_history() const { return history_; }
559
566 void set_hooks(const HookInterface& hooks) { hook_iface_ = hooks; }
567
575 auth_mgr_ = auth_mgr;
576 }
577
578private:
579 MCPAuthorizationManager* auth_mgr_ = nullptr;
580
589 std::optional<Message> check_mcp_authorization(
590 const LoopContext& ctx,
591 const ToolCall& call) const;
592};
593
594} // namespace entropic
Per-identity MCP authorization with runtime grant/revoke.
Manages MCP server instances and routes tool calls.
Fixed-size ring buffer of recent tool calls.
Processes tool calls from model output.
void set_authorization_manager(MCPAuthorizationManager *auth_mgr)
Set the MCP authorization manager.
std::vector< Message > process_tool_calls(LoopContext &ctx, const std::vector< ToolCall > &tool_calls)
Process a batch of tool calls.
void set_permission_persist(const PermissionPersistInterface &persist)
Set permission persistence interface.
const ToolCallHistory & tool_history() const
Access the tool-call history ring buffer.
void set_hooks(const HookInterface &hooks)
Set the hook dispatch interface.
Directive processing for tool-to-engine communication.
Types for the agentic loop engine.
Per-identity MCP authorization with runtime grant/revoke.
Activate model on GPU (WARM → ACTIVE).
ToolResultKind
Categorical outcome of a single tool invocation.
Definition tool_result.h:31
@ ok
Tool dispatched, returned non-empty content.
@ error
Tool server returned an error payload.
MCP server lifecycle management and tool routing.
Callback function pointer types for engine events.
Configuration for the agentic loop.
Mutable state carried through the agentic loop.
A message in a conversation.
Definition message.h:35
Permission persistence interface.
Outcome of precondition checks for a single tool call.
std::optional< Message > rejection
Rejection message (nullopt if cleared)
ToolResultKind kind
Category on rejection.
A tool call request parsed from model output.
Definition tool_call.h:31
Engine-level hooks called during tool processing.
In-memory ring buffer of recent tool calls for introspection.
Typed outcome for POST_TOOL_CALL hook consumers.