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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 666973e72e303998abcf62405a78a124205ccdbc741c011d3a3cec05a234e7a0
4
- data.tar.gz: eaf248d80cd7f5c203cfde80de6dc1ed9827539173dce3afb704e5661631cf77
3
+ metadata.gz: d47b79a77d34353e5bc8edf2b872e2fc86f33a86e89ad4b27071829ea5f50621
4
+ data.tar.gz: 43bbcf0ae440c94fd9ff2aa8f1e4cd28acd7d984850ae7d63c3dcbc1173c78e3
5
5
  SHA512:
6
- metadata.gz: bcf37c58978695dd35a844a31ea998f30cc8035c8b51b4267b47ab9a90d29d0d4b59de0a67194632c641e2737de1bc1692452d31fa1640b89e344760cb2e62e4
7
- data.tar.gz: c4f6a71d195797d30ebf34a82c02e5f637bd31e3085ac056092189199628b0c34b6d23d54be3936e4c13d02d8e879c228840e2c66b3c66f16334b6cb0b7010de
6
+ metadata.gz: 67269ed3772754ca359b5eecfe4fadfdeb6cbd6d836c6421a39ae0e458d0bee0135ec669bf2894fd13f99557bedbceb6dfe12508cfa58b7f7e02bdb7601ab8aa
7
+ data.tar.gz: 002b98eef6bf175add168a8cc3b3af9d21234e135a6f7b7d7f2ba0dd715192883072de261520075112dc3d22be9cbd3c81788f7b3f637250bcbde0f512183068
@@ -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
- result.call_tools.each do |tool|
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
- SmartAgent.prompt_engine.history_messages << { "role" => "assistant", "content" => "", "tool_calls" => [tool] } #result.response.dig("choices", 0, "message")
158
- SmartAgent.prompt_engine.history_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s.force_encoding("UTF-8") }
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
- SmartAgent.prompt_engine.history_messages << { "role" => "assistant", "content" => "", "tool_calls" => [tool] } # result.response.dig("choices", 0, "message")
167
- SmartAgent.prompt_engine.history_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s }
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 = {})
@@ -1,3 +1,3 @@
1
1
  module SmartAgent
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
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.8
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.2
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.2
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.13
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: []