MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / compute_diffs

Method compute_diffs

smallthinker/common/chat.cpp:83–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81}
82
83std::vector<common_chat_msg_diff> common_chat_msg_diff::compute_diffs(const common_chat_msg & previous_msg, const common_chat_msg & new_msg) {
84 std::vector<common_chat_msg_diff> diffs;
85 if (previous_msg.reasoning_content != new_msg.reasoning_content) {
86 auto & diff = diffs.emplace_back();
87 diff.reasoning_content_delta = string_diff(previous_msg.reasoning_content, new_msg.reasoning_content);
88 }
89 if (previous_msg.content != new_msg.content) {
90 auto & diff = diffs.emplace_back();
91 diff.content_delta = string_diff(previous_msg.content, new_msg.content);
92 }
93
94 if (new_msg.tool_calls.size() < previous_msg.tool_calls.size()) {
95 throw std::runtime_error("Invalid diff: now finding less tool calls!");
96 }
97
98 if (!previous_msg.tool_calls.empty()) {
99 auto idx = previous_msg.tool_calls.size() - 1;
100 const auto & pref = previous_msg.tool_calls[idx];
101 const auto & newf = new_msg.tool_calls[idx];
102 if (pref.name != newf.name) {
103 throw std::runtime_error("Invalid diff: tool call mismatch!");
104 }
105 auto args_diff = string_diff(pref.arguments, newf.arguments);
106 if (!args_diff.empty() || pref.id != newf.id) {
107 auto & diff = diffs.emplace_back();
108 diff.tool_call_index = idx;
109 if (pref.id != newf.id) {
110 diff.tool_call_delta.id = newf.id;
111 diff.tool_call_delta.name = newf.name;
112 }
113 diff.tool_call_delta.arguments = args_diff;
114 }
115 }
116 for (size_t idx = previous_msg.tool_calls.size(); idx < new_msg.tool_calls.size(); ++idx) {
117 auto & diff = diffs.emplace_back();
118 diff.tool_call_index = idx;
119 diff.tool_call_delta = new_msg.tool_calls[idx];
120 }
121 return diffs;
122}
123
124typedef minja::chat_template common_chat_template;
125

Callers

nothing calls this directly

Calls 3

string_diffFunction · 0.85
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected