Entropic 2.9.4
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 <unordered_map>
25#include <vector>
26
27namespace entropic {
28
29/* ToolExecutorHooks moved to engine_types.h — v2.0.2 */
30
45
56public:
65 ToolExecutor(ServerManager& server_manager,
66 const LoopConfig& loop_config,
67 EngineCallbacks& callbacks,
68 ToolExecutorHooks hooks = {});
69
76
93 const std::unordered_map<std::string,
94 std::vector<std::string>>* map) {
95 tier_allowed_tools_ = map;
96 }
97
105 std::vector<Message> process_tool_calls(
106 LoopContext& ctx,
107 const std::vector<ToolCall>& tool_calls);
108
109private:
116 static std::vector<ToolCall> sort_tool_calls(
117 const std::vector<ToolCall>& calls);
118
125 void extract_and_process_directives(
126 LoopContext& ctx, const std::string& raw_result);
127
135 std::string check_duplicate(const LoopContext& ctx,
136 const ToolCall& call) const;
137
146 Message handle_duplicate(LoopContext& ctx,
147 const ToolCall& call,
148 const std::string& previous_result);
149
156 bool check_approval(const ToolCall& call);
157
165 std::optional<Message> check_tier_allowed(
166 const LoopContext& ctx, const ToolCall& call) const;
167
175 std::pair<Message, std::string> execute_tool(
176 LoopContext& ctx, const ToolCall& call);
177
191 void record_tool_history(const ToolCall& call,
192 const std::string& args_json,
193 const std::string& result_text,
194 long long ms, int iteration);
195
212 void finalize_tool_call(LoopContext& ctx, const ToolCall& call,
213 Message& msg, const std::string& raw_result,
214 double exec_ms);
215
226 void log_tool_call(LoopContext& ctx, const ToolCall& call,
227 double exec_ms, const std::string& raw_result,
228 ToolResultKind kind);
229
236 static std::string tool_call_key(const ToolCall& call);
237
245 void record_tool_call(LoopContext& ctx,
246 const ToolCall& call,
247 const std::string& result);
248
256 static Message create_denied_message(const ToolCall& call,
257 const std::string& reason);
258
266 static Message create_error_message(const ToolCall& call,
267 const std::string& error);
268
275 void fire_state_callback(const LoopContext& ctx);
276
284 void truncate_to_limit(std::vector<ToolCall>& calls, int limit) const;
285
294 PreconditionCheck check_call_preconditions(
295 LoopContext& ctx, const ToolCall& call);
296
305 PreconditionCheck check_approval_pc(
306 LoopContext& ctx, const ToolCall& call);
307
314 std::optional<Message> check_schema(const ToolCall& call);
315
324 std::optional<Message> check_dup_or_approval(
325 LoopContext& ctx, const ToolCall& call);
326
335 std::vector<Message> process_single_call(
336 LoopContext& ctx, const ToolCall& call);
337
346 bool should_stop_batch(const LoopContext& ctx,
347 const std::vector<Message>& results) const;
348
355 void run_post_tool_hooks(LoopContext& ctx);
356
363 static Message create_circuit_breaker_message();
364
373 static Message create_duplicate_message(
374 const ToolCall& call,
375 const std::string& previous_result);
376
384 static std::string serialize_args(const ToolCall& call);
385
393 static std::string serialize_tool_call(const ToolCall& call);
394
408 static std::string build_post_tool_json(
409 const ToolCall& call,
410 const std::string& raw_result,
411 double elapsed_ms,
412 const std::string& tier,
413 int iteration,
414 ToolResultKind kind);
415
435 void update_anti_spiral_tracking(LoopContext& ctx,
436 const std::string& tool_name);
437
451 int effective_hard_block_threshold() const;
452
470 PreconditionCheck check_anti_spiral_hard_block(
471 const LoopContext& ctx, const ToolCall& call) const;
472
490 void apply_result_size_cap(std::string& content) const;
491
501 static std::string build_pre_tool_json(
502 const ToolCall& call,
503 const std::string& tier,
504 int iteration);
505
533 void fire_post_tool_hook(const LoopContext& ctx,
534 const ToolCall& call,
535 const std::string& raw_result,
536 double elapsed_ms,
537 ToolResultKind kind,
538 Message& msg);
539
548 bool fire_pre_tool_hook(const LoopContext& ctx,
549 const ToolCall& call);
550
559 void fire_tool_complete_callback(const ToolCall& call,
560 const std::string& result,
561 long long ms);
562
563 ServerManager& server_manager_;
564 const LoopConfig& loop_config_;
565 EngineCallbacks& callbacks_;
566 ToolExecutorHooks hooks_;
567 PermissionPersistInterface permission_persist_;
571 const std::unordered_map<std::string, std::vector<std::string>>*
572 tier_allowed_tools_ = nullptr;
573 HookInterface hook_iface_;
574 ToolCallHistory history_{100};
575 std::atomic<size_t> history_seq_{0};
576
577public:
585 const ToolCallHistory& tool_history() const { return history_; }
586
593 void set_hooks(const HookInterface& hooks) { hook_iface_ = hooks; }
594
602 auth_mgr_ = auth_mgr;
603 }
604
605private:
606 MCPAuthorizationManager* auth_mgr_ = nullptr;
607
616 std::optional<Message> check_mcp_authorization(
617 const LoopContext& ctx,
618 const ToolCall& call) const;
619};
620
621} // 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_tier_allowed_tools(const std::unordered_map< std::string, std::vector< std::string > > *map)
Wire the per-tier allowed_tools map for dispatch-time enforcement.
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.