mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-08-28 05:25:33 +00:00
feat(tools): allow passing apiKey via options for browser support (#599)
Co-authored-by: Mahesh Sanikommmu <mahesh.svmr@gmail.com>
This commit is contained in:
parent
16979f8bee
commit
f9d304ec3e
3 changed files with 12 additions and 7 deletions
|
|
@ -57,7 +57,7 @@ const addTool = addMemoryTool(process.env.SUPERMEMORY_API_KEY!, {
|
|||
#### AI SDK Middleware with Supermemory
|
||||
|
||||
- `withSupermemory` will take advantage supermemory profile v4 endpoint personalized based on container tag
|
||||
- Make sure you have `SUPERMEMORY_API_KEY` in env
|
||||
- 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.
|
||||
|
||||
```typescript
|
||||
import { generateText } from "ai"
|
||||
|
|
@ -395,6 +395,8 @@ interface WithSupermemoryOptions {
|
|||
verbose?: boolean
|
||||
mode?: "profile" | "query" | "full"
|
||||
addMemory?: "always" | "never"
|
||||
/** Optional Supermemory API key. Use this in browser environments. */
|
||||
apiKey?: string
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ interface WrapVercelLanguageModelOptions {
|
|||
verbose?: boolean;
|
||||
mode?: "profile" | "query" | "full";
|
||||
addMemory?: "always" | "never";
|
||||
apiKey?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -24,6 +25,7 @@ interface WrapVercelLanguageModelOptions {
|
|||
* @param options.verbose - Optional flag to enable detailed logging of memory search and injection process (default: false)
|
||||
* @param options.mode - Optional mode for memory search: "profile", "query", or "full" (default: "profile")
|
||||
* @param options.addMemory - Optional mode for memory search: "always", "never" (default: "never")
|
||||
* @param options.apiKey - Optional Supermemory API key to use instead of the environment variable
|
||||
*
|
||||
* @returns A wrapped language model that automatically includes relevant memories in prompts
|
||||
*
|
||||
|
|
@ -44,7 +46,7 @@ interface WrapVercelLanguageModelOptions {
|
|||
* })
|
||||
* ```
|
||||
*
|
||||
* @throws {Error} When SUPERMEMORY_API_KEY environment variable is not set
|
||||
* @throws {Error} When neither `options.apiKey` nor `process.env.SUPERMEMORY_API_KEY` are set
|
||||
* @throws {Error} When supermemory API request fails
|
||||
*/
|
||||
const wrapVercelLanguageModel = (
|
||||
|
|
@ -52,10 +54,10 @@ const wrapVercelLanguageModel = (
|
|||
containerTag: string,
|
||||
options?: WrapVercelLanguageModelOptions,
|
||||
): LanguageModelV2 => {
|
||||
const SUPERMEMORY_API_KEY = process.env.SUPERMEMORY_API_KEY
|
||||
const providedApiKey = options?.apiKey ?? process.env.SUPERMEMORY_API_KEY
|
||||
|
||||
if (!SUPERMEMORY_API_KEY) {
|
||||
throw new Error("SUPERMEMORY_API_KEY is not set")
|
||||
if (!providedApiKey) {
|
||||
throw new Error("SUPERMEMORY_API_KEY is not set — provide it via `options.apiKey` or set `process.env.SUPERMEMORY_API_KEY`")
|
||||
}
|
||||
|
||||
const conversationId = options?.conversationId
|
||||
|
|
@ -65,7 +67,7 @@ const wrapVercelLanguageModel = (
|
|||
|
||||
const wrappedModel = wrapLanguageModel({
|
||||
model,
|
||||
middleware: createSupermemoryMiddleware(containerTag, conversationId, verbose, mode, addMemory),
|
||||
middleware: createSupermemoryMiddleware(containerTag, providedApiKey, conversationId, verbose, mode, addMemory),
|
||||
})
|
||||
|
||||
return wrappedModel
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@ const addMemoryTool = async (
|
|||
|
||||
export const createSupermemoryMiddleware = (
|
||||
containerTag: string,
|
||||
apiKey: string,
|
||||
conversationId?: string,
|
||||
verbose = false,
|
||||
mode: "profile" | "query" | "full" = "profile",
|
||||
|
|
@ -76,7 +77,7 @@ export const createSupermemoryMiddleware = (
|
|||
const logger = createLogger(verbose)
|
||||
|
||||
const client = new Supermemory({
|
||||
apiKey: process.env.SUPERMEMORY_API_KEY,
|
||||
apiKey,
|
||||
})
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue