supermemory/packages/tools/src
Dhravya d0f53b0d64
feat(tools): SDK-level cross-source memory deduplication (#1531)
## Stack Context

This stack moves memory deduplication **out of the playground UI and into the SDKs themselves**, so every integration injects a single, deduplicated, self-replacing memory block. Three PRs:

1. **`sdk-dedup/tools-ts`** (this PR) — TypeScript SDK core + integrations
2. `sdk-dedup/python` — Python SDKs
3. `sdk-dedup/playground` — playground debug view reflects the SDK-owned block

## What?

Move profile deduplication into the SDK middleware for the TypeScript tools package.

- Facts are normalized (strip leading `[YYYY-MM-DD]`, trim, collapse whitespace, casefold) and deduplicated in **`static > dynamic > search`** priority within a single request.
- The result is injected as one **owned `<supermemory>` block** that *replaces* the previous block instead of accumulating a new one each turn.
- Dedup is **mode-aware**: in query mode, search results are not dropped against a profile that isn't being injected.
- Deduplication is **request-local** — no global/browser `Set`. Safe for multiple users, concurrent requests, and Cloudflare Worker isolates.

Covers AI SDK, OpenAI (Chat + Responses), Mastra, and VoltAgent. New `shared/memory-context.ts` owns the block-replacement logic.

## Why?

The earlier "conversation-scoped deduplication" was only a playground browser `Set` — a UI debug affordance that did not change what the SDK sent to the model, and would have been unsafe as server-side global state. Real cross-source dedup belongs in the SDK, applied fresh per stateless model request.

## Testing

- `bun run test` in `packages/tools`: 145 passed (the one failing suite, `claude-memory.test.ts`, is a pre-existing broken import unrelated to this change).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes how system prompts and instructions are built across all TypeScript integrations; behavior is well-covered by unit tests but incorrect strip/replace logic could drop or duplicate context in production prompts.
>
> **Overview**
> Moves **cross-source memory deduplication** and **owned prompt injection** into `@supermemory/tools` so every integration sends one deduplicated memory block per request instead of growing context each turn.
>
> **Deduplication:** Facts are normalized via `normalizeMemoryFact` (strip `[YYYY-MM-DD]`, trim, collapse whitespace, lowercase) and deduplicated with **static → dynamic → search** priority. `deduplicateMemoriesForMode` keeps search hits in **query** mode when the profile is not injected.
>
> **Owned `<supermemory>` block:** New `shared/memory-context.ts` wraps memories in `<supermemory context="user-memories" readonly>`, strips stale blocks, and **replaces** prior SDK context while preserving caller system instructions. Applied in AI SDK (`injectMemoriesIntoParams`), OpenAI Chat/Responses middleware, Mastra input processor (`wrapMemoryContext`), and VoltAgent hooks.
>
> **Tests:** Unit coverage for block replacement (with-supermemory, OpenAI, VoltAgent), Mastra wrapper tag assertion, normalized dedup variants, and concurrent `containerTag` isolation.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 2fa2e0d85c. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
2026-09-01 06:10:36 +00:00
..
mastra feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
openai feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
shared feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
vercel feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
voltagent feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
ai-sdk.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
claude-memory-simple-example.ts feat: mobile responsive, lint formats, toast, render issue fix (#688) 2026-01-21 03:11:53 +00:00
claude-memory.test.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
claude-memory.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
conversations-client.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
index.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
mastra.ts mastra integration (#717) 2026-02-03 00:43:08 +00:00
tool-operations.test.ts feat(tools): 7-tool parity and description refresh (#1432) 2026-09-01 05:59:57 +00:00
tools-shared.test.ts feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
tools-shared.ts feat(tools): SDK-level cross-source memory deduplication (#1531) 2026-09-01 06:10:36 +00:00
tools.test.ts fix(tools): repair SDK call mismatches that broke five tools (#1186) 2026-07-10 18:30:41 -07:00
types.ts feat: tools package strict mode for openai models (#699) 2026-01-23 03:15:39 +00:00