Skip to content

Commit 080ba89

Browse files
authored
Merge branch 'agentscope-ai:main' into main
2 parents cd1caa7 + dd358b7 commit 080ba89

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

  • agentscope-extensions/agentscope-extensions-mem0/src/main/java/io/agentscope/core/memory/mem0

agentscope-extensions/agentscope-extensions-mem0/src/main/java/io/agentscope/core/memory/mem0/Mem0Client.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,41 +227,39 @@ public Mono<Mem0AddResponse> add(Mem0AddRequest request) {
227227
* memories relevant to the query string. Results are ordered by relevance score
228228
* (highest first).
229229
*
230-
* <p>The v2 API returns a direct array of results, which this method wraps
231-
* into a Mem0SearchResponse object for consistency with the existing API.
230+
* <p>Automatically compatible with two Mem0 API response formats:
231+
* <ul>
232+
* <li><b>format v1.1</b> — response is a JSON object with a {@code results} field
233+
* (e.g. {@code {"results": [...]}}), deserialized directly into
234+
* {@link Mem0SearchResponse}.</li>
235+
* <li><b>format v1.0</b> — response is a direct JSON array (e.g. {@code [...]}),
236+
* parsed as a list of results and wrapped into a {@link Mem0SearchResponse}.</li>
237+
* </ul>
232238
*
233239
* <p>The metadata filters (agent_id, user_id, run_id) in the request ensure
234240
* that only memories from the specified context are returned.
235241
*
236-
* <p>The operation is performed asynchronously on the bounded elastic scheduler
237-
* to avoid blocking the caller thread.
238-
*
239242
* @param request The search request containing query and filters
240243
* @return A Mono emitting the search response with relevant memories
241244
*/
242245
public Mono<Mem0SearchResponse> search(Mem0SearchRequest request) {
243246
return executePostRaw(searchEndpoint, request, "search request")
244247
.map(
245248
responseBody -> {
246-
// Platform Mem0 uses /v2/memories/search/ endpoint and returns
247-
// direct array
248-
// Self-hosted Mem0 uses /search endpoint and returns wrapped format
249-
if (searchEndpoint.contains("/v2/")) {
250-
// Platform Mem0 returns direct array
249+
// Support both response formats: direct array or object with results
250+
String trimmed = responseBody != null ? responseBody.trim() : "";
251+
if (trimmed.startsWith("[")) {
252+
// Response is a JSON array: parse as list and wrap in results
251253
List<Mem0SearchResult> results =
252254
jsonCodec.fromJson(
253255
responseBody,
254256
new TypeReference<List<Mem0SearchResult>>() {});
255-
256-
// Wrap in Mem0SearchResponse for consistency
257257
Mem0SearchResponse searchResponse = new Mem0SearchResponse();
258258
searchResponse.setResults(results);
259259
return searchResponse;
260-
} else {
261-
// Self-hosted Mem0 returns response wrapped in {"results":
262-
// [...]}
263-
return jsonCodec.fromJson(responseBody, Mem0SearchResponse.class);
264260
}
261+
// Response is an object (e.g. {"results": [...]})
262+
return jsonCodec.fromJson(responseBody, Mem0SearchResponse.class);
265263
});
266264
}
267265

0 commit comments

Comments
 (0)