smart_agent 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/smart_agent/agent.rb +38 -6
- data/lib/smart_agent/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d47b79a77d34353e5bc8edf2b872e2fc86f33a86e89ad4b27071829ea5f50621
|
|
4
|
+
data.tar.gz: 43bbcf0ae440c94fd9ff2aa8f1e4cd28acd7d984850ae7d63c3dcbc1173c78e3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 67269ed3772754ca359b5eecfe4fadfdeb6cbd6d836c6421a39ae0e458d0bee0135ec669bf2894fd13f99557bedbceb6dfe12508cfa58b7f7e02bdb7601ab8aa
|
|
7
|
+
data.tar.gz: 002b98eef6bf175add168a8cc3b3af9d21234e135a6f7b7d7f2ba0dd715192883072de261520075112dc3d22be9cbd3c81788f7b3f637250bcbde0f512183068
|
data/lib/smart_agent/agent.rb
CHANGED
|
@@ -72,6 +72,8 @@ module SmartAgent
|
|
|
72
72
|
def call_worker(name, params, with_tools: true, with_history: false)
|
|
73
73
|
SmartAgent.logger.info("Call Worker name is: #{name}")
|
|
74
74
|
SmartAgent.logger.info("Call Worker params is: #{params}")
|
|
75
|
+
# 记录 session_id,call_tools 写工具结果时需要按 session 路由(HistoryManager 启用时)
|
|
76
|
+
@session_id = params[:session_id]
|
|
75
77
|
if with_tools
|
|
76
78
|
simple_tools = []
|
|
77
79
|
if @agent.tools
|
|
@@ -142,11 +144,21 @@ module SmartAgent
|
|
|
142
144
|
end
|
|
143
145
|
end
|
|
144
146
|
|
|
145
|
-
def call_tools(result)
|
|
147
|
+
def call_tools(result, calls: nil)
|
|
146
148
|
@agent.processor(:tool).call({ :status => :start }) if @agent.processor(:tool)
|
|
147
149
|
SmartAgent.logger.info("call tools: " + result.to_s)
|
|
148
150
|
results = []
|
|
149
|
-
|
|
151
|
+
completed_calls = []
|
|
152
|
+
completed_messages = []
|
|
153
|
+
# DeepSeek thinking 模式要求带 tool_calls 的 assistant 消息回传 reasoning_content,否则 400
|
|
154
|
+
reasoning = begin
|
|
155
|
+
result.response.dig("choices", 0, "message", "reasoning_content")
|
|
156
|
+
rescue StandardError
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
# 调用方可传入 calls: 覆盖本次要执行的工具调用集合(如截断 / DSML 兑底解析后的结果)
|
|
160
|
+
source = calls || result.call_tools
|
|
161
|
+
(source || []).each do |tool|
|
|
150
162
|
tool_call_id = tool["id"]
|
|
151
163
|
tool_name = tool["function"]["name"].to_sym
|
|
152
164
|
params = safe_parse(tool["function"]["arguments"])
|
|
@@ -154,8 +166,8 @@ module SmartAgent
|
|
|
154
166
|
tool_result = Tool.find_tool(tool_name).call(params, @agent)
|
|
155
167
|
if tool_result
|
|
156
168
|
@agent.processor(:tool).call({ :content => tool_result }) if @agent.processor(:tool)
|
|
157
|
-
|
|
158
|
-
|
|
169
|
+
completed_calls << tool
|
|
170
|
+
completed_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s.force_encoding("UTF-8") }
|
|
159
171
|
results << tool_result
|
|
160
172
|
end
|
|
161
173
|
end
|
|
@@ -163,8 +175,8 @@ module SmartAgent
|
|
|
163
175
|
tool_result = MCPClient.new(server_name).call(tool_name, params, @agent)
|
|
164
176
|
if tool_result
|
|
165
177
|
@agent.processor(:tool).call({ :content => tool_result }) if @agent.processor(:tool)
|
|
166
|
-
|
|
167
|
-
|
|
178
|
+
completed_calls << tool
|
|
179
|
+
completed_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s }
|
|
168
180
|
results << tool_result
|
|
169
181
|
end
|
|
170
182
|
end
|
|
@@ -172,6 +184,26 @@ module SmartAgent
|
|
|
172
184
|
end
|
|
173
185
|
@agent.processor(:tool).call({ :status => :end }) if @agent.processor(:tool)
|
|
174
186
|
return results
|
|
187
|
+
ensure
|
|
188
|
+
# 每轮写一条 assistant 消息(含全部 tool_calls),而不是每个工具各写一条,
|
|
189
|
+
# 避免交错产生 assistant/tool/assistant/tool 序列;被中断时也保留已完成部分。
|
|
190
|
+
if completed_calls && completed_calls.any?
|
|
191
|
+
message = { "role" => "assistant", "content" => "", "tool_calls" => completed_calls }
|
|
192
|
+
message["reasoning_content"] = reasoning if reasoning
|
|
193
|
+
append_history_message(message)
|
|
194
|
+
completed_messages.each { |m| append_history_message(m) }
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# 写历史:HistoryManager 可用时按 session 路由(否则工具结果写全局、读 session 会失忆),
|
|
199
|
+
# 不可用时回退到全局数组(与旧行为一致)。
|
|
200
|
+
def append_history_message(message)
|
|
201
|
+
engine = SmartAgent.prompt_engine
|
|
202
|
+
if engine.respond_to?(:history_manager) && engine.history_manager && @session_id
|
|
203
|
+
engine.history_manager.add_message(@session_id, message)
|
|
204
|
+
elsif engine.respond_to?(:history_messages)
|
|
205
|
+
engine.history_messages << message
|
|
206
|
+
end
|
|
175
207
|
end
|
|
176
208
|
|
|
177
209
|
def call_tool(name, params = {})
|
data/lib/smart_agent/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: smart_agent
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Zhuang Biaowei
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.5.
|
|
18
|
+
version: 0.5.4
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.5.
|
|
25
|
+
version: 0.5.4
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: mcp-sdk.rb
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
85
85
|
- !ruby/object:Gem::Version
|
|
86
86
|
version: '0'
|
|
87
87
|
requirements: []
|
|
88
|
-
rubygems_version: 4.0.
|
|
88
|
+
rubygems_version: 4.0.16
|
|
89
89
|
specification_version: 4
|
|
90
90
|
summary: Intelligent agent framework with DSL and MCP integration
|
|
91
91
|
test_files: []
|