supermemory/packages/tools/src/shared/context.ts
nexxeln 9553434c9a mastra integration (#717)
adds withSupermemory wrapper and input/output processors for
mastra agents:

- input processor fetches and injects memories into system prompt
before llm calls
- output processor saves conversations to supermemory after
responses
- supports profile, query, and full memory search modes
- includes custom prompt templates and requestcontext support

const agent = new Agent(withSupermemory(
{ id: "my-assistant", model: openai("gpt-4o"), instructions:
"..." },
"user-123",
{ mode: "full", addMemory: "always", threadId: "conv-456" }
))

includes docs as well

this pr also reworks how the tools package works into shared modules
2026-02-03 00:43:08 +00:00

61 lines
1.6 KiB
TypeScript

import Supermemory from "supermemory"
/**
* Normalizes a base URL by removing trailing slashes.
*
* @param url - Optional base URL to normalize
* @returns Normalized URL without trailing slash, or default API URL
*/
export const normalizeBaseUrl = (url?: string): string => {
const defaultUrl = "https://api.supermemory.ai"
if (!url) return defaultUrl
return url.endsWith("/") ? url.slice(0, -1) : url
}
/**
* Options for creating a Supermemory client.
*/
export interface CreateSupermemoryClientOptions {
/** Supermemory API key */
apiKey: string
/** Optional custom base URL */
baseUrl?: string
}
/**
* Creates a configured Supermemory client instance.
*
* @param options - Client configuration options
* @returns Configured Supermemory client
*/
export function createSupermemoryClient(
options: CreateSupermemoryClientOptions,
): Supermemory {
const normalizedBaseUrl = normalizeBaseUrl(options.baseUrl)
return new Supermemory({
apiKey: options.apiKey,
...(normalizedBaseUrl !== "https://api.supermemory.ai"
? { baseURL: normalizedBaseUrl }
: {}),
})
}
/**
* Validates that an API key is provided either via options or environment variable.
*
* @param apiKey - Optional API key from options
* @returns The validated API key
* @throws Error if no API key is available
*/
export function validateApiKey(apiKey?: string): string {
const providedApiKey = apiKey ?? process.env.SUPERMEMORY_API_KEY
if (!providedApiKey) {
throw new Error(
"SUPERMEMORY_API_KEY is not set — provide it via `options.apiKey` or set `process.env.SUPERMEMORY_API_KEY`",
)
}
return providedApiKey
}