|
19 | 19 | import com.fasterxml.jackson.databind.JsonNode; |
20 | 20 | import io.agentscope.core.agent.RuntimeContext; |
21 | 21 | import io.agentscope.core.message.TextBlock; |
| 22 | +import io.agentscope.core.message.ToolResultBlock; |
22 | 23 | import io.agentscope.core.message.ToolResultState; |
23 | 24 | import io.agentscope.core.message.ToolUseBlock; |
24 | 25 | import io.agentscope.core.model.ToolSchema; |
25 | 26 | import io.agentscope.core.tool.AgentTool; |
26 | 27 | import io.agentscope.core.tool.ToolCallParam; |
| 28 | +import io.agentscope.core.tool.ToolEmitter; |
27 | 29 | import io.agentscope.core.tool.ToolValidator; |
28 | 30 | import io.agentscope.core.tool.Toolkit; |
29 | 31 | import io.agentscope.core.util.JsonUtils; |
|
32 | 34 | import io.agentscope.durable.DurableTypes.ExecutionRequest; |
33 | 35 | import io.agentscope.durable.DurableTypes.ExecutionResult; |
34 | 36 | import io.agentscope.durable.DurableTypes.ExecutionState; |
| 37 | +import io.agentscope.durable.DurableTypes.OutputPage; |
35 | 38 | import io.agentscope.durable.Executor; |
36 | 39 | import io.agentscope.durable.ToolBinding; |
37 | 40 | import java.time.Duration; |
| 41 | +import java.util.Base64; |
38 | 42 | import java.util.Collection; |
39 | 43 | import java.util.LinkedHashMap; |
40 | 44 | import java.util.Map; |
@@ -145,8 +149,13 @@ public void validateStart(ExecutionRequest request) { |
145 | 149 | DurableException.Code.INVALID, "Invalid tool arguments: " + invalid); |
146 | 150 | } |
147 | 151 |
|
148 | | - @SuppressWarnings("unchecked") |
149 | 152 | public ExecutionResult startExecution(ExecutionRequest request) { |
| 153 | + return startExecution(request, null); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + @SuppressWarnings("unchecked") |
| 158 | + public ExecutionResult startExecution(ExecutionRequest request, Preview preview) { |
150 | 159 | validateStart(request); |
151 | 160 | var binding = bindings.get(request.call().name()); |
152 | 161 | Map<String, Object> arguments = DurableJson.convert(request.call().arguments(), Map.class); |
@@ -174,6 +183,7 @@ public ExecutionResult startExecution(ExecutionRequest request) { |
174 | 183 | ToolCallParam.builder() |
175 | 184 | .toolUseBlock(call) |
176 | 185 | .input(arguments) |
| 186 | + .emitter(preview == null ? null : emitter(preview)) |
177 | 187 | .propagateErrors(true) |
178 | 188 | .runtimeContext(context) |
179 | 189 | .build()) |
@@ -209,6 +219,35 @@ public ExecutionResult startExecution(ExecutionRequest request) { |
209 | 219 | null); |
210 | 220 | } |
211 | 221 |
|
| 222 | + private static ToolEmitter emitter(Preview preview) { |
| 223 | + return new ToolEmitter() { |
| 224 | + private long offset; |
| 225 | + |
| 226 | + public synchronized void emit(ToolResultBlock chunk) { |
| 227 | + if (chunk.getOutput() == null) return; |
| 228 | + byte[] bytes = AgentScopeToolEvents.textBytes(chunk); |
| 229 | + if (bytes.length > 0) { |
| 230 | + long next = offset + bytes.length; |
| 231 | + preview.output( |
| 232 | + new OutputPage( |
| 233 | + offset, |
| 234 | + next, |
| 235 | + Base64.getEncoder().encodeToString(bytes), |
| 236 | + next, |
| 237 | + false, |
| 238 | + null)); |
| 239 | + offset = next; |
| 240 | + } |
| 241 | + if (chunk.getOutput().stream().anyMatch(block -> !(block instanceof TextBlock)) |
| 242 | + || chunk.getMetadata() != null && !chunk.getMetadata().isEmpty()) { |
| 243 | + preview.progress( |
| 244 | + DurableJson.value( |
| 245 | + JsonUtils.getJsonCodec().convertValue(chunk, Map.class))); |
| 246 | + } |
| 247 | + } |
| 248 | + }; |
| 249 | + } |
| 250 | + |
212 | 251 | public ExecutionResult query(ExecutionRequest request, String handle) { |
213 | 252 | return ExecutionResult.unknown("Native tool has no external operation lookup"); |
214 | 253 | } |
|
0 commit comments