Skip to content

Commit f9d304e

Browse files
feat(tools): allow passing apiKey via options for browser support (supermemoryai#599)
Co-authored-by: Mahesh Sanikommmu <mahesh.svmr@gmail.com>
1 parent 16979f8 commit f9d304e

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/tools/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const addTool = addMemoryTool(process.env.SUPERMEMORY_API_KEY!, {
5757
#### AI SDK Middleware with Supermemory
5858

5959
- `withSupermemory` will take advantage supermemory profile v4 endpoint personalized based on container tag
60-
- Make sure you have `SUPERMEMORY_API_KEY` in env
60+
- You can provide the Supermemory API key via the `apiKey` option to `withSupermemory` (recommended for browser usage), or fall back to `SUPERMEMORY_API_KEY` in the environment for server usage.
6161

6262
```typescript
6363
import { generateText } from "ai"
@@ -395,6 +395,8 @@ interface WithSupermemoryOptions {
395395
verbose?: boolean
396396
mode?: "profile" | "query" | "full"
397397
addMemory?: "always" | "never"
398+
/** Optional Supermemory API key. Use this in browser environments. */
399+
apiKey?: string
398400
}
399401
```
400402

packages/tools/src/vercel/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface WrapVercelLanguageModelOptions {
77
verbose?: boolean;
88
mode?: "profile" | "query" | "full";
99
addMemory?: "always" | "never";
10+
apiKey?: string;
1011
}
1112

1213
/**
@@ -24,6 +25,7 @@ interface WrapVercelLanguageModelOptions {
2425
* @param options.verbose - Optional flag to enable detailed logging of memory search and injection process (default: false)
2526
* @param options.mode - Optional mode for memory search: "profile", "query", or "full" (default: "profile")
2627
* @param options.addMemory - Optional mode for memory search: "always", "never" (default: "never")
28+
* @param options.apiKey - Optional Supermemory API key to use instead of the environment variable
2729
*
2830
* @returns A wrapped language model that automatically includes relevant memories in prompts
2931
*
@@ -44,18 +46,18 @@ interface WrapVercelLanguageModelOptions {
4446
* })
4547
* ```
4648
*
47-
* @throws {Error} When SUPERMEMORY_API_KEY environment variable is not set
49+
* @throws {Error} When neither `options.apiKey` nor `process.env.SUPERMEMORY_API_KEY` are set
4850
* @throws {Error} When supermemory API request fails
4951
*/
5052
const wrapVercelLanguageModel = (
5153
model: LanguageModelV2,
5254
containerTag: string,
5355
options?: WrapVercelLanguageModelOptions,
5456
): LanguageModelV2 => {
55-
const SUPERMEMORY_API_KEY = process.env.SUPERMEMORY_API_KEY
57+
const providedApiKey = options?.apiKey ?? process.env.SUPERMEMORY_API_KEY
5658

57-
if (!SUPERMEMORY_API_KEY) {
58-
throw new Error("SUPERMEMORY_API_KEY is not set")
59+
if (!providedApiKey) {
60+
throw new Error("SUPERMEMORY_API_KEY is not set — provide it via `options.apiKey` or set `process.env.SUPERMEMORY_API_KEY`")
5961
}
6062

6163
const conversationId = options?.conversationId
@@ -65,7 +67,7 @@ const wrapVercelLanguageModel = (
6567

6668
const wrappedModel = wrapLanguageModel({
6769
model,
68-
middleware: createSupermemoryMiddleware(containerTag, conversationId, verbose, mode, addMemory),
70+
middleware: createSupermemoryMiddleware(containerTag, providedApiKey, conversationId, verbose, mode, addMemory),
6971
})
7072

7173
return wrappedModel

packages/tools/src/vercel/middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const addMemoryTool = async (
6868

6969
export const createSupermemoryMiddleware = (
7070
containerTag: string,
71+
apiKey: string,
7172
conversationId?: string,
7273
verbose = false,
7374
mode: "profile" | "query" | "full" = "profile",
@@ -76,7 +77,7 @@ export const createSupermemoryMiddleware = (
7677
const logger = createLogger(verbose)
7778

7879
const client = new Supermemory({
79-
apiKey: process.env.SUPERMEMORY_API_KEY,
80+
apiKey,
8081
})
8182

8283
return {

0 commit comments

Comments
 (0)