Entropic 2.9.4
Local-first agentic inference engine
Loading...
Searching...
No Matches
adapter_base.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
27#pragma once
28
31
32#include <optional>
33#include <string>
34#include <string_view>
35#include <unordered_set>
36#include <vector>
37
38namespace entropic {
39
45 std::string cleaned_content;
46 std::vector<ToolCall> tool_calls;
47};
48
64std::vector<ToolCall> recover_action_envelope_calls(const std::string& raw);
65
79void apply_action_envelope_recovery(std::vector<ToolCall>& calls,
80 const std::string& raw);
81
100void coerce_string_typed_args(std::vector<ToolCall>& calls,
101 const std::string& tools_json);
102
113public:
120 ChatAdapter(std::string tier_name, std::string identity_prompt);
121 virtual ~ChatAdapter() = default;
122
123 /* ── System prompt assembly ──────────────────────────── */
124
132 std::string format_system_prompt(
133 const std::string& base_prompt,
134 const std::vector<std::string>& tool_jsons) const;
135
136 /* ── Tool call parsing (subclass overrides) ──────────── */
137
145 const std::string& content) const = 0;
146
147 /* ── Tool result formatting ──────────────────────────── */
148
157 const ToolCall& tool_call,
158 const std::string& result) const;
159
160 /* ── Tool formatting (subclass can override) ─────────── */
161
168 virtual std::string format_tools(
169 const std::vector<std::string>& tool_jsons) const;
170
171 /* ── Response completeness ───────────────────────────── */
172
181 const std::string& content,
182 const std::vector<ToolCall>& tool_calls) const;
183
189 virtual std::string chat_format() const = 0;
190
191protected:
192 /* ── Shared parsing primitives ───────────────────────── */
193
198 std::vector<ToolCall> parse_tagged_tool_calls(
199 const std::string& content) const;
200
205 std::vector<ToolCall> parse_bare_json_tool_calls(
206 const std::string& content) const;
207
212 std::string extract_thinking(const std::string& content) const;
213
218 std::string strip_think_blocks(const std::string& content) const;
219
224 std::optional<ToolCall> try_recover_json(
225 const std::string& json_str) const;
226
233 std::optional<ToolCall> parse_single_tool_call(
234 const std::string& json_str) const;
235
236 std::string tier_name_;
237 std::string identity_prompt_;
238 mutable std::unordered_set<std::string> tool_prefixes_;
239
240public:
241 /* ── Vision / multimodal (v1.9.11) ────────────────────── */
242
253 virtual std::string format_system_with_vision(
254 const std::string& base_system,
255 bool has_vision) const;
256
266 virtual std::string format_content_parts(
267 const std::vector<ContentPart>& parts) const;
268};
269
270} // namespace entropic
Concrete base class for chat format adapters (80% logic).
std::string format_system_prompt(const std::string &base_prompt, const std::vector< std::string > &tool_jsons) const
Assemble system prompt: identity + context + tools.
std::unordered_set< std::string > tool_prefixes_
Known tool prefixes.
std::optional< ToolCall > try_recover_json(const std::string &json_str) const
Attempt JSON recovery on malformed tool call string.
virtual std::string format_content_parts(const std::vector< ContentPart > &parts) const
Convert multimodal content parts to adapter-specific format.
virtual std::string format_system_with_vision(const std::string &base_system, bool has_vision) const
Format system prompt with optional vision context.
virtual std::string chat_format() const =0
Chat format identifier (e.g.
std::optional< ToolCall > parse_single_tool_call(const std::string &json_str) const
Parse a single JSON tool call string.
std::vector< ToolCall > parse_tagged_tool_calls(const std::string &content) const
Parse <tool_call>JSON</tool_call> tagged blocks.
std::vector< ToolCall > parse_bare_json_tool_calls(const std::string &content) const
Parse bare JSON lines containing "name" key.
virtual std::string format_tools(const std::vector< std::string > &tool_jsons) const
Format tool definitions for injection into system prompt.
virtual ParseResult parse_tool_calls(const std::string &content) const =0
Parse tool calls from model output.
bool is_response_complete(const std::string &content, const std::vector< ToolCall > &tool_calls) const
Check if response represents task completion.
std::string strip_think_blocks(const std::string &content) const
Strip all <think>...</think> blocks from content.
std::string identity_prompt_
Assembled identity prompt.
virtual Message format_tool_result(const ToolCall &tool_call, const std::string &result) const
Format a tool result as a user message.
std::string tier_name_
Identity tier name.
std::string extract_thinking(const std::string &content) const
Extract <think>...</think> content.
Message struct for conversation history.
Activate model on GPU (WARM → ACTIVE).
void coerce_string_typed_args(std::vector< ToolCall > &calls, const std::string &tools_json)
gh#90: coerce numeric scalars back to strings for string-typed tool parameters.
std::vector< ToolCall > recover_action_envelope_calls(const std::string &raw)
gh#88: recover tool calls a gemma model parroted as bare-JSON.
void apply_action_envelope_recovery(std::vector< ToolCall > &calls, const std::string &raw)
gh#88: substitute recovered bare-JSON calls when a reliable (PEG_GEMMA4 / gemma) parse produced none;...
A message in a conversation.
Definition message.h:35
Parsed tool call result: cleaned content + extracted calls.
std::string cleaned_content
Content with tool calls removed.
std::vector< ToolCall > tool_calls
Extracted tool calls.
A tool call request parsed from model output.
Definition tool_call.h:31
Tool call and tool result types.