1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
import type OpenAI from "openai"
import Supermemory from "supermemory"
import { addConversation } from "../conversations-client"
import { deduplicateMemories } from "../shared"
import { createLogger, type Logger } from "../vercel/logger"
import { convertProfileToMarkdown } from "../vercel/util"
const normalizeBaseUrl = (url?: string): string => {
const defaultUrl = "https://api.supermemory.ai"
if (!url) return defaultUrl
return url.endsWith("/") ? url.slice(0, -1) : url
}
export interface OpenAIMiddlewareOptions {
conversationId?: string
verbose?: boolean
mode?: "profile" | "query" | "full"
addMemory?: "always" | "never"
baseUrl?: string
}
interface SupermemoryProfileSearch {
profile: {
static?: Array<{ memory: string; metadata?: Record<string, unknown> }>
dynamic?: Array<{ memory: string; metadata?: Record<string, unknown> }>
}
searchResults: {
results: Array<{ memory: string; metadata?: Record<string, unknown> }>
}
}
/**
* Extracts the last user message from an array of chat completion messages.
*
* Searches through the messages array in reverse order to find the most recent
* message with role "user" and returns its content as a string.
*
* @param messages - Array of chat completion message parameters
* @returns The content of the last user message, or empty string if none found
*
* @example
* ```typescript
* const messages = [
* { role: "system", content: "You are a helpful assistant." },
* { role: "user", content: "Hello there!" },
* { role: "assistant", content: "Hi! How can I help you?" },
* { role: "user", content: "What's the weather like?" }
* ]
*
* const lastMessage = getLastUserMessage(messages)
* // Returns: "What's the weather like?"
* ```
*/
const getLastUserMessage = (
messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
) => {
const lastUserMessage = messages
.slice()
.reverse()
.find((msg) => msg.role === "user")
return typeof lastUserMessage?.content === "string"
? lastUserMessage.content
: ""
}
/**
* Searches for memories using the SuperMemory profile API.
*
* Makes a POST request to the SuperMemory API to retrieve user profile memories
* and search results based on the provided container tag and optional query text.
*
* @param containerTag - The container tag/identifier for memory search (e.g., user ID, project ID)
* @param queryText - Optional query text to search for specific memories. If empty, returns all profile memories
* @returns Promise that resolves to the SuperMemory profile search response
* @throws {Error} When the API request fails or returns an error status
*
* @example
* ```typescript
* // Search with query
* const results = await supermemoryProfileSearch("user-123", "favorite programming language")
*
* // Get all profile memories
* const profile = await supermemoryProfileSearch("user-123", "")
* ```
*/
const supermemoryProfileSearch = async (
containerTag: string,
queryText: string,
baseUrl: string,
): Promise<SupermemoryProfileSearch> => {
const payload = queryText
? JSON.stringify({
q: queryText,
containerTag: containerTag,
})
: JSON.stringify({
containerTag: containerTag,
})
try {
const response = await fetch(`${baseUrl}/v4/profile`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.SUPERMEMORY_API_KEY}`,
},
body: payload,
})
if (!response.ok) {
const errorText = await response.text().catch(() => "Unknown error")
throw new Error(
`Supermemory profile search failed: ${response.status} ${response.statusText}. ${errorText}`,
)
}
return await response.json()
} catch (error) {
if (error instanceof Error) {
throw error
}
throw new Error(`Supermemory API request failed: ${error}`)
}
}
/**
* Adds memory-enhanced system prompts to chat completion messages.
*
* Searches for relevant memories based on the specified mode and injects them
* into the conversation. If a system prompt already exists, memories are appended
* to it. Otherwise, a new system prompt is created with the memories.
*
* @param messages - Array of chat completion message parameters
* @param containerTag - The container tag/identifier for memory search
* @param logger - Logger instance for debugging and info output
* @param mode - Memory search mode: "profile" (all memories), "query" (search-based), or "full" (both)
* @returns Promise that resolves to enhanced messages with memory-injected system prompt
*
* @example
* ```typescript
* const messages = [
* { role: "user", content: "What's my favorite programming language?" }
* ]
*
* const enhancedMessages = await addSystemPrompt(
* messages,
* "user-123",
* logger,
* "full"
* )
* // Returns messages with system prompt containing relevant memories
* ```
*/
const addSystemPrompt = async (
messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
containerTag: string,
logger: Logger,
mode: "profile" | "query" | "full",
baseUrl: string,
) => {
const systemPromptExists = messages.some((msg) => msg.role === "system")
const queryText = mode !== "profile" ? getLastUserMessage(messages) : ""
const memoriesResponse = await supermemoryProfileSearch(
containerTag,
queryText,
baseUrl,
)
const memoryCountStatic = memoriesResponse.profile.static?.length || 0
const memoryCountDynamic = memoriesResponse.profile.dynamic?.length || 0
logger.info("Memory search completed for chat API", {
containerTag,
memoryCountStatic,
memoryCountDynamic,
queryText:
queryText.substring(0, 100) + (queryText.length > 100 ? "..." : ""),
mode,
})
const deduplicated = deduplicateMemories({
static: memoriesResponse.profile.static,
dynamic: memoriesResponse.profile.dynamic,
searchResults: memoriesResponse.searchResults?.results,
})
logger.debug("Memory deduplication completed for chat API", {
static: {
original: memoryCountStatic,
deduplicated: deduplicated.static.length,
},
dynamic: {
original: memoryCountDynamic,
deduplicated: deduplicated.dynamic.length,
},
searchResults: {
original: memoriesResponse.searchResults?.results?.length,
deduplicated: deduplicated.searchResults.length,
},
})
const profileData =
mode !== "query"
? convertProfileToMarkdown({
profile: {
static: deduplicated.static,
dynamic: deduplicated.dynamic,
},
searchResults: { results: [] },
})
: ""
const searchResultsMemories =
mode !== "profile"
? `Search results for user's recent message: \n${deduplicated.searchResults
.map((memory) => `- ${memory}`)
.join("\n")}`
: ""
const memories = `${profileData}\n${searchResultsMemories}`.trim()
if (memories) {
logger.debug("Memory content preview for chat API", {
content: memories,
fullLength: memories.length,
})
}
if (systemPromptExists) {
logger.debug("Added memories to existing system prompt")
return messages.map((msg) =>
msg.role === "system"
? { ...msg, content: `${msg.content} \n ${memories}` }
: msg,
)
}
logger.debug(
"System prompt does not exist, created system prompt with memories",
)
return [{ role: "system" as const, content: memories }, ...messages]
}
/**
* Converts an array of chat completion messages into a formatted conversation string.
*
* Transforms the messages array into a readable conversation format where each
* message is prefixed with its role (User/Assistant) and messages are separated
* by double newlines.
*
* @param messages - Array of chat completion message parameters
* @returns Formatted conversation string with role prefixes
*
* @example
* ```typescript
* const messages = [
* { role: "user", content: "Hello!" },
* { role: "assistant", content: "Hi there!" },
* { role: "user", content: "How are you?" }
* ]
*
* const conversation = getConversationContent(messages)
* // Returns: "User: Hello!\n\nAssistant: Hi there!\n\nUser: How are you?"
* ```
*/
const getConversationContent = (
messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
) => {
return messages
.map((msg) => {
const role = msg.role === "user" ? "User" : "Assistant"
const content = typeof msg.content === "string" ? msg.content : ""
return `${role}: ${content}`
})
.join("\n\n")
}
/**
* Adds a new memory to the SuperMemory system.
*
* Saves the provided content as a memory with the specified container tag and
* optional custom ID. Logs success or failure information for debugging.
*
* If customId starts with "conversation:" and messages are provided, uses the
* /v4/conversations endpoint with structured messages instead of the memories endpoint.
*
* @param client - SuperMemory client instance
* @param containerTag - The container tag/identifier for the memory
* @param content - The content to save as a memory (used for fallback)
* @param customId - Optional custom ID for the memory (e.g., conversation:456)
* @param logger - Logger instance for debugging and info output
* @param messages - Optional OpenAI messages array (for conversation endpoint)
* @param apiKey - API key for direct conversation endpoint calls
* @param baseUrl - Base URL for API calls
* @returns Promise that resolves when memory is saved (or fails silently)
*
* @example
* ```typescript
* await addMemoryTool(
* supermemoryClient,
* "user-123",
* "User: Hello\n\nAssistant: Hi!",
* "conversation:456",
* logger,
* messages, // OpenAI messages array
* apiKey,
* baseUrl
* )
* ```
*/
const addMemoryTool = async (
client: Supermemory,
containerTag: string,
content: string,
customId: string | undefined,
logger: Logger,
messages?: OpenAI.Chat.Completions.ChatCompletionMessageParam[],
apiKey?: string,
baseUrl?: string,
): Promise<void> => {
try {
if (customId && messages && apiKey) {
const conversationId = customId.replace("conversation:", "")
// Convert OpenAI messages to conversation format
const conversationMessages = messages.map((msg) => ({
role: msg.role as "user" | "assistant" | "system" | "tool",
content:
typeof msg.content === "string"
? msg.content
: Array.isArray(msg.content)
? msg.content
.filter((c) => c.type === "text")
.map((c) => ({
type: "text" as const,
text: (c as { type: "text"; text: string }).text,
}))
: "",
...((msg as any).name && { name: (msg as any).name }),
...((msg as any).tool_calls && { tool_calls: (msg as any).tool_calls }),
...((msg as any).tool_call_id && {
tool_call_id: (msg as any).tool_call_id,
}),
}))
const response = await addConversation({
conversationId,
messages: conversationMessages,
containerTags: [containerTag],
apiKey,
baseUrl,
})
logger.info("Conversation saved successfully via /v4/conversations", {
containerTag,
conversationId,
messageCount: messages.length,
responseId: response.id,
})
return
}
// Fallback to old behavior for non-conversation memories
const response = await client.add({
content,
containerTags: [containerTag],
customId,
})
logger.info("Memory saved successfully", {
containerTag,
customId,
contentLength: content.length,
memoryId: response.id,
})
} catch (error) {
logger.error("Error saving memory", {
error: error instanceof Error ? error.message : "Unknown error",
})
}
}
/**
* Creates SuperMemory middleware for OpenAI clients.
*
* This function creates middleware that automatically injects relevant memories
* into OpenAI chat completions and optionally saves new memories. The middleware
* can wrap existing OpenAI clients or create new ones with SuperMemory capabilities.
*
* @param containerTag - The container tag/identifier for memory search (e.g., user ID, project ID)
* @param options - Optional configuration options for the middleware
* @param options.conversationId - Optional conversation ID to group messages for contextual memory generation
* @param options.verbose - Enable detailed logging of memory operations (default: false)
* @param options.mode - Memory search mode: "profile" (all memories), "query" (search-based), or "full" (both) (default: "profile")
* @param options.addMemory - Automatic memory storage mode: "always" or "never" (default: "never")
* @returns Object with `wrapClient` and `createClient` methods
* @throws {Error} When SUPERMEMORY_API_KEY environment variable is not set
*
* @example
* ```typescript
* const openaiWithSupermemory = createOpenAIMiddleware(openai, "user-123", {
* conversationId: "conversation-456",
* mode: "full",
* addMemory: "always",
* verbose: true
* })
*
* ```
*/
export function createOpenAIMiddleware(
openaiClient: OpenAI,
containerTag: string,
options?: OpenAIMiddlewareOptions,
) {
const logger = createLogger(options?.verbose ?? false)
const baseUrl = normalizeBaseUrl(options?.baseUrl)
const client = new Supermemory({
apiKey: process.env.SUPERMEMORY_API_KEY,
...(baseUrl !== "https://api.supermemory.ai" ? { baseURL: baseUrl } : {}),
})
const conversationId = options?.conversationId
const mode = options?.mode ?? "profile"
const addMemory = options?.addMemory ?? "never"
const originalCreate = openaiClient.chat.completions.create
const originalResponsesCreate = openaiClient.responses?.create
/**
* Searches for memories and formats them for injection into API calls.
*
* This shared function handles memory search and formatting for both Chat Completions
* and Responses APIs, reducing code duplication.
*
* @param queryText - The text to search for (empty string for profile-only mode)
* @param containerTag - The container tag for memory search
* @param logger - Logger instance
* @param mode - Memory search mode
* @param context - API context for logging differentiation
* @returns Formatted memories string
*/
const searchAndFormatMemories = async (
queryText: string,
containerTag: string,
logger: Logger,
mode: "profile" | "query" | "full",
context: "chat" | "responses",
) => {
const memoriesResponse = await supermemoryProfileSearch(
containerTag,
queryText,
baseUrl,
)
const memoryCountStatic = memoriesResponse.profile.static?.length || 0
const memoryCountDynamic = memoriesResponse.profile.dynamic?.length || 0
logger.info(`Memory search completed for ${context} API`, {
containerTag,
memoryCountStatic,
memoryCountDynamic,
queryText:
queryText.substring(0, 100) + (queryText.length > 100 ? "..." : ""),
mode,
})
const deduplicated = deduplicateMemories({
static: memoriesResponse.profile.static,
dynamic: memoriesResponse.profile.dynamic,
searchResults: memoriesResponse.searchResults?.results,
})
logger.debug(`Memory deduplication completed for ${context} API`, {
static: {
original: memoryCountStatic,
deduplicated: deduplicated.static.length,
},
dynamic: {
original: memoryCountDynamic,
deduplicated: deduplicated.dynamic.length,
},
searchResults: {
original: memoriesResponse.searchResults?.results?.length,
deduplicated: deduplicated.searchResults.length,
},
})
const profileData =
mode !== "query"
? convertProfileToMarkdown({
profile: {
static: deduplicated.static,
dynamic: deduplicated.dynamic,
},
searchResults: { results: [] },
})
: ""
const searchResultsMemories =
mode !== "profile"
? `Search results for user's ${context === "chat" ? "recent message" : "input"}: \n${deduplicated.searchResults
.map((memory) => `- ${memory}`)
.join("\n")}`
: ""
const memories = `${profileData}\n${searchResultsMemories}`.trim()
if (memories) {
logger.debug(`Memory content preview for ${context} API`, {
content: memories,
fullLength: memories.length,
})
}
return memories
}
const createResponsesWithMemory = async (
params: Parameters<typeof originalResponsesCreate>[0],
) => {
if (!originalResponsesCreate) {
throw new Error(
"Responses API is not available in this OpenAI client version",
)
}
const input = typeof params.input === "string" ? params.input : ""
if (mode !== "profile" && !input) {
logger.debug("No input found for Responses API, skipping memory search")
return originalResponsesCreate.call(openaiClient.responses, params)
}
logger.info("Starting memory search for Responses API", {
containerTag,
conversationId,
mode,
})
const operations: Promise<any>[] = []
if (addMemory === "always" && input?.trim()) {
const content = conversationId ? `Input: ${input}` : input
const customId = conversationId
? `conversation:${conversationId}`
: undefined
operations.push(
addMemoryTool(client, containerTag, content, customId, logger),
)
}
const queryText = mode !== "profile" ? input : ""
operations.push(
searchAndFormatMemories(
queryText,
containerTag,
logger,
mode,
"responses",
),
)
const results = await Promise.all(operations)
const memories = results[results.length - 1] // Memory search result is always last
const enhancedInstructions = memories
? `${params.instructions || ""}\n\n${memories}`.trim()
: params.instructions
return originalResponsesCreate.call(openaiClient.responses, {
...params,
instructions: enhancedInstructions,
})
}
const createWithMemory = async (
params: OpenAI.Chat.Completions.ChatCompletionCreateParams,
) => {
const messages = Array.isArray(params.messages) ? params.messages : []
if (mode !== "profile") {
const userMessage = getLastUserMessage(messages)
if (!userMessage) {
logger.debug("No user message found, skipping memory search")
return originalCreate.call(openaiClient.chat.completions, params)
}
}
logger.info("Starting memory search", {
containerTag,
conversationId,
mode,
})
const operations: Promise<any>[] = []
if (addMemory === "always") {
const userMessage = getLastUserMessage(messages)
if (userMessage?.trim()) {
const content = conversationId
? getConversationContent(messages)
: userMessage
const customId = conversationId
? `conversation:${conversationId}`
: undefined
operations.push(
addMemoryTool(
client,
containerTag,
content,
customId,
logger,
messages,
process.env.SUPERMEMORY_API_KEY,
baseUrl,
),
)
}
}
operations.push(
addSystemPrompt(messages, containerTag, logger, mode, baseUrl),
)
const results = await Promise.all(operations)
const enhancedMessages = results[results.length - 1] // Enhanced messages result is always last
return originalCreate.call(openaiClient.chat.completions, {
...params,
messages: enhancedMessages,
})
}
openaiClient.chat.completions.create =
createWithMemory as typeof originalCreate
// Wrap Responses API if available
if (originalResponsesCreate) {
openaiClient.responses.create =
createResponsesWithMemory as typeof originalResponsesCreate
}
return openaiClient
}
|