agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

feat(smart-search): add format parameter (full/narrative/compact) | 添加 format 参数

Open mechanic-Q opened this issue 7 months ago • 3 comments

Summary | 概述

Add format parameter to mem::smart-search to align with mem::search, which already supports format-aware output (full / compact / narrative).

为 mem::smart-search 添加 format 参数,与已有的 mem::search 格式体系对齐(支持 full / compact / narrative 三种输出模式)。

Motivation | 动机

When using agentmemory with Hermes Agent and other MCP clients in a real-world workflow, we found that memory_smart_search (which calls mem::smart-search) is the primary search tool exposed to LLMs. However it only returned compact results — obsId, title, type, score. For the LLM to actually use the search results, it needs to see the narrative field (the human-readable summary of each observation).

Without this, every smart-search call required a follow-up expandIds round-trip just to read the narratives — doubling latency and token usage. Meanwhile memory_search (mem::search) already had format support. This inconsistency meant integrators had to choose between the smarter hybrid search OR readable results, but couldn't have both.

本补丁来自实际使用 agentmemory + Hermes Agent 的体验:memory_smart_search 是 LLM 使用的主要搜索工具,但只返回 compact 格式(obsId/title/type/score),LLM 无法直接读取记忆的 narrative 内容。每次搜索都需要额外 expandIds 调用来获取正文,导致延迟翻倍。而 memory_search 已有 format 支持。这个不一致让集成分叉——要么用智能搜索,要么用可读结果,无法兼得。

Problem | 问题

mem::search supports a format parameter (full | compact | narrative) that controls the richness of returned results. mem::smart-search was hardcoded to compact-only output, creating an inconsistency between the two search functions.

Changes | 改动

  • Added format?: string to the input type (defaults to "compact" for backwards compatibility)
  • Three format modes:
    • compact — minimal: obsId, sessionId, title, type, score, timestamp (existing behavior)
    • narrative — adds narrative field for LLM consumption (replaces the expandIds round-trip)
    • full — complete observation object with all fields
  • Validates format against allowed values, returns error for invalid input
  • Removes unused CompactSearchResult import

Backwards Compatibility | 向后兼容

Default format is "compact", preserving existing behavior. All existing callers work without changes.

Related | 相关

mem::search format implementation: src/functions/search.ts L82-84

mechanic-Q avatar May 02 '26 06:05 mechanic-Q

@mechanic-Q is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar May 02 '26 06:05 vercel[bot]

📝 Walkthrough

Walkthrough

The smart-search function now accepts a format parameter to customize the response structure. The function validates the format against three options—"full," "compact," and "narrative"—and returns results with format-specific field sets. Access tracking and logging are updated to work format-agnostically, recording IDs from the original hybrid results.

Changes

Smart-Search Format Customization

Layer / File(s) Summary
Input Parameters
src/functions/smart-search.ts (lines 18–21)
Function input payload gains format?: string parameter to specify response shape.
Format Validation & Defaults
src/functions/smart-search.ts (lines 71–75)
Format defaults to "compact" and is validated against allowed modes; invalid format returns an error.
Format-Aware Response Mapping
src/functions/smart-search.ts (lines 77–103)
Result mapping branches on format: "full" returns observation and sessionId; "narrative" returns obsId, title, narrative, score, and timestamp; "compact" returns obsId, title, type, score, and timestamp.
Access Tracking & Logging
src/functions/smart-search.ts (lines 104–113)
Access tracking records observation IDs directly from hybridResults; logging and return structure updated to include mode: format and reflect results count.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A format for every quest,
Full, narrative, compact—pick your best!
Smart search now bends to your command,
Three shapes of results, oh how grand!
twitches nose ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a format parameter with three supported modes (full/narrative/compact) to the smart-search function.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 02 '26 06:05 coderabbitai[bot]

Thanks @mechanic-Q — format as a query parameter is the right call. Saves a downstream client from having to expand obsIds when they really wanted the narrative inline.

Two small nits before merge:

1. Bad-format response shouldn't echo the bad string.

return { mode: format, results: [], error: "format must be one of 'full', 'compact', or 'narrative'" };

If a caller sends format: "foobar", you return mode: "foobar", which makes the response shape lie about itself. Either return mode: "compact" (the safe default) plus the error, or drop mode entirely on the error path. Same applies in the log line.

2. Array<Record<string, unknown>> is loose.

The three branches each produce a well-shaped variant. Mind defining a SearchResultFormatted discriminated union (or keeping CompactSearchResult and adding NarrativeSearchResult / FullSearchResult)? Otherwise downstream callers lose all type safety on what they get back.

Functionally clean otherwise. Once those are addressed, happy to merge.

rohitg00 avatar May 08 '26 17:05 rohitg00