Skip to content

Commit a00a751

Browse files
committed
pkg(tools): Expose raw search results in MemoryPromptData for prompt templates (#787)
Expose raw search results in `MemoryPromptData` so prompt templates can traverse, filter, and selectively include results based on metadata (e.g. score, source). ##### Usage example ```typescript const promptTemplate = (data: MemoryPromptData) => { const relevant = data.searchResults.filter( (r) => (r.metadata?.score as number) > 0.7 ) return `${data.userMemories}\n${relevant.map(r => r.memory).join('\n')}` } ```
1 parent c534008 commit a00a751

8 files changed

Lines changed: 40 additions & 8 deletions

File tree

apps/docs/ai-sdk/user-profiles.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The `MemoryPromptData` object passed to your template function provides:
154154

155155
- `userMemories`: Pre-formatted markdown combining static profile facts (name, preferences, goals) and dynamic context (current projects, recent interests)
156156
- `generalSearchMemories`: Pre-formatted search results based on semantic similarity to the current query (empty string if mode is "profile")
157+
- `searchResults`: Raw search results array (`Array<{ memory: string; metadata?: Record<string, unknown> }>`) for traversing, filtering, or selectively including results based on metadata
157158

158159
### XML-Based Prompting for Claude
159160

@@ -179,6 +180,31 @@ const model = withSupermemory(anthropic("claude-3-sonnet"), "user-123", {
179180
})
180181
```
181182

183+
### Filtering Search Results
184+
185+
Use `searchResults` to traverse the raw data and pick what's important:
186+
187+
```typescript
188+
const selectivePrompt = (data: MemoryPromptData) => {
189+
const relevant = data.searchResults.filter(
190+
(r) => (r.metadata?.score as number) > 0.7
191+
)
192+
return `
193+
<user_memories>
194+
${data.userMemories}
195+
</user_memories>
196+
<relevant_context>
197+
${relevant.map((r) => `- ${r.memory}`).join("\n")}
198+
</relevant_context>
199+
`.trim()
200+
}
201+
202+
const model = withSupermemory(openai("gpt-4"), "user-123", {
203+
mode: "full",
204+
promptTemplate: selectivePrompt
205+
})
206+
```
207+
182208
### Custom Branding
183209

184210
Remove "supermemories" references and use your own branding:

apps/docs/integrations/ai-sdk.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const model = withSupermemory(openai("gpt-4"), "user-123", { mode: "full" })
7575

7676
### Custom Prompt Templates
7777

78-
Customize how memories are formatted:
78+
Customize how memories are formatted. The template receives `userMemories`, `generalSearchMemories`, and `searchResults` (raw array for filtering by metadata):
7979

8080
```typescript
8181
import { withSupermemory, type MemoryPromptData } from "@supermemory/tools/ai-sdk"

apps/docs/integrations/mastra.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ await agent.generate("My favorite framework is Next.js")
162162

163163
## Custom Prompt Templates
164164

165-
Customize how memories are formatted and injected:
165+
Customize how memories are formatted and injected. The template receives `userMemories`, `generalSearchMemories`, and `searchResults` (raw array for filtering by metadata):
166166

167167
```typescript
168168
import { Agent } from "@mastra/core/agent"

packages/tools/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ const result = await generateText({
253253
The `MemoryPromptData` object provides:
254254
- `userMemories`: Pre-formatted markdown combining static profile facts (name, preferences, goals) and dynamic context (current projects, recent interests)
255255
- `generalSearchMemories`: Pre-formatted search results based on semantic similarity to the current query
256+
- `searchResults`: Raw search results array for traversing, filtering, or selectively including results based on metadata
256257

257258
### OpenAI SDK Usage
258259

packages/tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@supermemory/tools",
33
"type": "module",
4-
"version": "1.4.00",
4+
"version": "1.4.01",
55
"description": "Memory tools for AI SDK and OpenAI function calling with supermemory",
66
"scripts": {
77
"build": "tsdown",

packages/tools/src/shared/memory-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export const buildMemoriesText = async (
154154
const promptData: MemoryPromptData = {
155155
userMemories,
156156
generalSearchMemories,
157+
searchResults: memoriesResponse.searchResults?.results ?? [],
157158
}
158159

159160
const memories = promptTemplate(promptData)

packages/tools/src/shared/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export interface MemoryPromptData {
1313
* Empty string if mode is "profile" only.
1414
*/
1515
generalSearchMemories: string
16+
/**
17+
* Raw search results from the API for the current query.
18+
* Use this to traverse, filter, or selectively include results based on metadata.
19+
* Empty array if mode is "profile" or when no search was performed.
20+
*/
21+
searchResults: Array<{ memory: string; metadata?: Record<string, unknown> }>
1622
}
1723

1824
/**
@@ -28,6 +34,7 @@ export interface MemoryPromptData {
2834
* ${data.generalSearchMemories}
2935
* </user_memories>
3036
* `.trim()
37+
* // data.searchResults provides raw results for custom filtering/formatting
3138
* ```
3239
*/
3340
export type PromptTemplate = (data: MemoryPromptData) => string

packages/tools/src/vercel/memory-prompt.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export {
88
type BuildMemoriesTextOptions,
99
} from "../shared"
1010

11-
import type { Logger } from "../shared"
11+
import type { Logger, MemoryPromptData } from "../shared"
1212
import type { LanguageModelCallOptions } from "./util"
1313

1414
/**
@@ -100,10 +100,7 @@ export const addSystemPrompt = async (
100100
mode: "profile" | "query" | "full",
101101
baseUrl: string,
102102
apiKey: string,
103-
promptTemplate?: (data: {
104-
userMemories: string
105-
generalSearchMemories: string
106-
}) => string,
103+
promptTemplate?: (data: MemoryPromptData) => string,
107104
): Promise<LanguageModelCallOptions> => {
108105
const { buildMemoriesText } = await import("../shared")
109106

0 commit comments

Comments
 (0)