|
24 | 24 | import io.agentscope.core.message.ToolUseBlock; |
25 | 25 | import io.agentscope.core.model.Model; |
26 | 26 | import io.agentscope.core.util.JsonUtils; |
| 27 | +import io.agentscope.harness.agent.filesystem.sandbox.AbstractSandboxFilesystem; |
27 | 28 | import io.agentscope.harness.agent.memory.compaction.ConversationCompactor; |
28 | | -import io.agentscope.harness.agent.memory.session.SessionEntry; |
29 | | -import io.agentscope.harness.agent.memory.session.SessionTree; |
| 29 | +import io.agentscope.harness.agent.memory.session.SessionTranscriptWriter; |
30 | 30 | import io.agentscope.harness.agent.workspace.WorkspaceConstants; |
31 | 31 | import io.agentscope.harness.agent.workspace.WorkspaceManager; |
32 | | -import java.nio.file.Path; |
33 | 32 | import java.time.LocalDate; |
34 | 33 | import java.time.format.DateTimeFormatter; |
35 | 34 | import java.util.ArrayList; |
36 | | -import java.util.HashSet; |
37 | 35 | import java.util.List; |
38 | | -import java.util.Set; |
39 | 36 | import java.util.stream.Collectors; |
40 | 37 | import org.slf4j.Logger; |
41 | 38 | import org.slf4j.LoggerFactory; |
@@ -191,125 +188,37 @@ public Mono<Void> flushMemories(RuntimeContext rc, List<Msg> messages) { |
191 | 188 | * Returns the string path of the session JSONL file where messages for the given agent and |
192 | 189 | * session are offloaded. Used by the compaction layer to embed the archive location in the |
193 | 190 | * summary message so the agent can retrieve full history if needed. |
| 191 | + * |
| 192 | + * @deprecated Prefer {@link SessionTranscriptWriter#resolveContextPath}. |
194 | 193 | */ |
| 194 | + @Deprecated |
195 | 195 | public String resolveOffloadPath(RuntimeContext rc, String agentId, String sessionId) { |
196 | | - try { |
197 | | - Path p = workspaceManager.resolveSessionContextFile(rc, agentId, sessionId); |
198 | | - return p != null ? p.toString() : ""; |
199 | | - } catch (Exception e) { |
200 | | - log.debug( |
201 | | - "Could not resolve offload path for agent={}, session={}: {}", |
202 | | - agentId, |
203 | | - sessionId, |
204 | | - e.getMessage()); |
| 196 | + // Session archives are persisted in the host-side workspace. A sandboxed agent cannot |
| 197 | + // resolve that path, so do not advertise it as agent-readable in the summary prompt. |
| 198 | + if (workspaceManager.getFilesystem() instanceof AbstractSandboxFilesystem) { |
205 | 199 | return ""; |
206 | 200 | } |
| 201 | + return new SessionTranscriptWriter(workspaceManager) |
| 202 | + .resolveContextPath(rc, agentId, sessionId); |
207 | 203 | } |
208 | 204 |
|
209 | 205 | /** |
210 | 206 | * Offloads raw messages to the JSONL session tree. |
| 207 | + * |
| 208 | + * @deprecated Prefer {@link SessionTranscriptWriter#appendMessages}. Kept as a thin |
| 209 | + * delegate so compaction / overflow-recovery call sites keep compiling during the |
| 210 | + * migration; new code should use {@link SessionTranscriptWriter} directly. |
211 | 211 | */ |
| 212 | + @Deprecated |
212 | 213 | public void offloadMessages( |
213 | 214 | RuntimeContext rc, List<Msg> messages, String agentId, String sessionId) { |
214 | | - offloadToSessionTree(rc, messages, agentId, sessionId); |
215 | | - |
| 215 | + new SessionTranscriptWriter(workspaceManager) |
| 216 | + .appendMessages(rc, messages, agentId, sessionId); |
216 | 217 | log.debug( |
217 | 218 | "Offloaded {} messages for agent={}, session={}", |
218 | 219 | messages.size(), |
219 | 220 | agentId, |
220 | 221 | sessionId); |
221 | | - workspaceManager.updateSessionIndex(rc, agentId, sessionId, "conversation offloaded"); |
222 | | - } |
223 | | - |
224 | | - private void offloadToSessionTree( |
225 | | - RuntimeContext rc, List<Msg> messages, String agentId, String sessionId) { |
226 | | - try { |
227 | | - Path contextFile = workspaceManager.resolveSessionContextFile(rc, agentId, sessionId); |
228 | | - String contextRelPath = |
229 | | - WorkspaceConstants.AGENTS_DIR |
230 | | - + "/" |
231 | | - + agentId |
232 | | - + "/" |
233 | | - + WorkspaceConstants.SESSIONS_DIR |
234 | | - + "/" |
235 | | - + sessionId |
236 | | - + WorkspaceConstants.SESSION_CONTEXT_EXT; |
237 | | - SessionTree tree = |
238 | | - new SessionTree( |
239 | | - contextFile, |
240 | | - workspaceManager.getWorkspace(), |
241 | | - workspaceManager.getFilesystem(), |
242 | | - workspaceManager.getIndex(), |
243 | | - contextRelPath) |
244 | | - .setRuntimeContext(rc); |
245 | | - tree.load(); |
246 | | - // Sync from remote before appending so that entries written by a previous replica |
247 | | - // (cross-machine handoff) are included in the merged file pushed to remote. |
248 | | - tree.syncFromRemote(); |
249 | | - |
250 | | - List<SessionEntry> existingEntries = new ArrayList<>(tree.getAllEntries()); |
251 | | - Set<String> existingIds = |
252 | | - existingEntries.stream() |
253 | | - .map(SessionEntry::getId) |
254 | | - .collect(Collectors.toCollection(HashSet::new)); |
255 | | - String lastId = |
256 | | - existingEntries.isEmpty() |
257 | | - ? null |
258 | | - : existingEntries.get(existingEntries.size() - 1).getId(); |
259 | | - |
260 | | - // The caller passes the full conversation on every turn. Use the stable Msg IDs |
261 | | - // to keep the session JSONL append-only and idempotent across repeated offloads. |
262 | | - for (Msg msg : messages) { |
263 | | - if (msg.getRole() == null || isSessionContextMessage(msg)) { |
264 | | - continue; |
265 | | - } |
266 | | - String rendered = renderContentBlocks(msg); |
267 | | - if (rendered == null || rendered.isBlank()) { |
268 | | - continue; |
269 | | - } |
270 | | - String entryId = normalizeEntryId(msg.getId()); |
271 | | - if (entryId == null) { |
272 | | - log.warn( |
273 | | - "Msg without stable ID encountered (role={}); dedup skipped", |
274 | | - msg.getRole()); |
275 | | - } |
276 | | - if (entryId != null && existingIds.contains(entryId)) { |
277 | | - continue; |
278 | | - } |
279 | | - String toolCallId = extractToolCallId(msg); |
280 | | - SessionEntry.MessageEntry entry = |
281 | | - new SessionEntry.MessageEntry( |
282 | | - entryId, lastId, null, msg.getRole().name(), rendered, toolCallId); |
283 | | - tree.append(entry); |
284 | | - existingIds.add(entry.getId()); |
285 | | - lastId = entry.getId(); |
286 | | - } |
287 | | - |
288 | | - tree.flush(); |
289 | | - } catch (Exception e) { |
290 | | - log.warn("Failed to offload to JSONL session tree: {}", e.getMessage()); |
291 | | - } |
292 | | - } |
293 | | - |
294 | | - /** |
295 | | - * Extracts a representative tool call ID from a message, if present. |
296 | | - * For TOOL messages, returns the first ToolResultBlock's id. |
297 | | - * For ASSISTANT messages with tool calls, returns the first ToolUseBlock's id. |
298 | | - */ |
299 | | - private static String extractToolCallId(Msg msg) { |
300 | | - for (ContentBlock block : msg.getContent()) { |
301 | | - if (block instanceof ToolResultBlock tr && tr.getId() != null) { |
302 | | - return tr.getId(); |
303 | | - } |
304 | | - if (block instanceof ToolUseBlock tu && tu.getId() != null) { |
305 | | - return tu.getId(); |
306 | | - } |
307 | | - } |
308 | | - return null; |
309 | | - } |
310 | | - |
311 | | - private static String normalizeEntryId(String id) { |
312 | | - return id != null && !id.isBlank() ? id : null; |
313 | 222 | } |
314 | 223 |
|
315 | 224 | /** |
|
0 commit comments