fix: accept a Supermemory API key programmatically in the OpenAI integration - #1244
Open
rajarshidattapy wants to merge 6 commits into
Open
fix: accept a Supermemory API key programmatically in the OpenAI integration#1244rajarshidattapy wants to merge 6 commits into
rajarshidattapy wants to merge 6 commits into
Conversation
Contributor
|
@rajarshidattapy please fix the conflicts |
Contributor
Author
|
@ishaanxgupta give me 5 mins, on it. |
Contributor
Author
|
@ishaanxgupta done, you can merge it now. |
Contributor
|
I still see some conflicts |
…re-apikey-option # Conflicts: # packages/tools/src/openai/middleware.ts
Contributor
Author
|
@ishaanxgupta Thanks for catching that. I pulled the latest upstream/main again, resolved the remaining conflicts, and pushed the changes. It should be ready for another look. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1242.
The OpenAI integration was the only one that couldn't accept an API key programmatically.
The Vercel, Mastra, and Voltagent integrations all resolve:
through the shared
validateApiKeyhelper (src/shared/context.ts).The OpenAI middleware instead read
process.env.SUPERMEMORY_API_KEYdirectly in two places and exposed noapiKeyfield onOpenAIMiddlewareOptions.As a result,
withSupermemory()hard-threw whenever the key wasn't available as a process environment variable (for example when using a secrets manager, an edge runtime, or a multi-tenant server with a per-request key), with no way to supply one programmatically.Fix
This PR adds an optional
apiKeyfield and routes all API key resolution through the sharedvalidateApiKey()helper, bringing the OpenAI integration in line with the others.src/openai/middleware.tsapiKey?: stringtoOpenAIMiddlewareOptionssupermemoryProfileSearch()(now accepts anapiKeyparameter instead of readingprocess.envwhen constructing theAuthorizationheader)addSystemPrompt()addMemoryTool()instead ofprocess.env.SUPERMEMORY_API_KEYsrc/openai/index.tswith:
This preserves the existing fail-fast behavior while allowing callers to provide the API key programmatically.
Documentation
Updated the OpenAI JSDoc
@throwsdescriptions to match the shared behavior:Example
The following now works:
Previously this threw:
Testing
Added:
src/openai/middleware.test.ts(4 tests)The tests mock the SDK and stub
fetch, so no network access is required.I also verified that the new tests reproduce the original bug.
Against
main, the two new capability tests fail with:The two backward-compatibility tests continue to pass before and after the change:
options.apiKeynorSUPERMEMORY_API_KEYis providedOne test additionally verifies that the outgoing
/v4/profilerequest contains:Authorization: Bearer <key>confirming the supplied key is propagated all the way to the HTTP request rather than only accepted by the API surface.
Checks
Per
CONTRIBUTING.md:vitest— 80 passing, 0 failing assertionscheck-types— 146 errors on this branch, 146 onmain(no new errors; existing ones are intest/files andsrc/openai/tools.ts)buildformat-lint(Biome clean)Breaking changes
None.
apiKeyis optional, andSUPERMEMORY_API_KEYremains the fallback, so existing code continues to work unchanged.The only behavioral change is that a programmatically supplied API key is now honored instead of causing an exception.