-
Notifications
You must be signed in to change notification settings - Fork 74
(EAI-1159) Implement middleware in new contentRouter
#816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mmeigs
wants to merge
45
commits into
search_content_route
Choose a base branch
from
implement_search_middleware
base: search_content_route
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
9664693
Create MongoDbSearchResultsStore, add limit to DefaultFindContent and…
mmeigs f35d9a7
Implement saveSearchResult, create MongoDbSearchResultsStore.test.ts
mmeigs b07d9d9
lint, format
mmeigs 3a1060b
Check entire returned document in MongoDbSearchResultsStore.test.ts
mmeigs f62e36c
Create ResultChunk type and zod check
mmeigs b6f9aec
Correct usage of limit in makeDefaultFindContent
mmeigs 1c10dfc
PR feedback: cast badSearchResultRecord as any
mmeigs 3a9b937
Starting structure of searchContent
mmeigs f479ea0
Use unknown instead of any for ResultChunk additional metadata
mmeigs fe1d015
Merge branch 'add_search_results_store' into expose_search_endpoint
mmeigs 4df3c20
Add searchContent test file, broaden QueryFilter & MongoDbAtlasVector…
mmeigs baad5bd
Work on clarity of comments in contentRouter
mmeigs b3ad758
PR feedback: Combine describe blocks in MongoDbSearchResultsStore.tes…
mmeigs d2b9375
Merge branch 'add_search_results_store' into expose_search_endpoint
mmeigs 370bd86
Use generics on middleware: requireRequestOrigin, requireValidIpAddress
mmeigs a9d1910
Structure out contentRouter test file
mmeigs dd3c316
Combine describes
mmeigs 969e3b6
Merge branch 'add_search_results_store' into expose_search_endpoint
mmeigs 5ebefb5
Clean
mmeigs 647144f
makeFindContentWithMongoDbMetadata
mmeigs c65e8e6
config.test.ts
mmeigs a7919c1
Merge branch 'search_content_route' into expose_search_endpoint
mmeigs 147aea0
Clean
mmeigs 5dabbf6
PR feedback
mmeigs 6f377f5
Correct types
mmeigs 31710a4
Correct test
mmeigs 310a607
Merge branch 'search_content_route' into expose_search_endpoint
mmeigs fd66498
Fix test return type
mmeigs 2f1df27
lint
mmeigs 8efc4c6
Revert move of classifyMongoDbProgrammingLanguageAndProduct, jest nee…
mmeigs 2c9a21b
Created addCustomData.ts, generics, use in both contentRouter and con…
mmeigs ff75594
Clean
mmeigs 19ac7d4
Remove unnecessary tests and comments
mmeigs 3446476
Merge branch 'expose_search_endpoint' into implement_search_middleware
mmeigs cadae06
Added custom middleware to contentRouter, used in searchContent route…
mmeigs c8c38ca
Merge branch 'search_content_route' into implement_search_middleware
mmeigs 25e2c5c
Add customData to db...
mmeigs db98f56
Clean: allow undefined customData value
mmeigs d435d8f
Alter types for createConversationsMiddlewareReq
mmeigs 3a58520
Rerun tests
mmeigs 896194c
Merge branch 'search_content_route' into implement_search_middleware
mmeigs 2812d4e
PR feedback
mmeigs 9e9c44d
Add Locals types to middleware invocations
mmeigs 4db7567
Lint, fix trace name, remove unnecessary import
mmeigs 690b895
(EAI-972) Add extra braintrust tracing to searchContent route (#822)
mmeigs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
8 changes: 6 additions & 2 deletions
8
packages/mongodb-chatbot-server/src/middleware/requireRequestOrigin.ts
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
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
8 changes: 6 additions & 2 deletions
8
packages/mongodb-chatbot-server/src/middleware/requireValidIpAddress.ts
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
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
94 changes: 94 additions & 0 deletions
94
packages/mongodb-chatbot-server/src/processors/addCustomData.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Request, Response } from "express"; | ||
|
||
export type RequestCustomData = Record<string, unknown> | undefined; | ||
|
||
/** | ||
Function to add custom data to the {@link Conversation} or content search Request persisted to the database. | ||
Has access to the Express.js request and response plus the values | ||
from the {@link Response.locals} object. | ||
*/ | ||
export type AddCustomDataFunc = ( | ||
request: Request, | ||
response: Response | ||
) => Promise<RequestCustomData>; | ||
|
||
const addIpToCustomData: AddCustomDataFunc = async (req) => | ||
req.ip | ||
? { | ||
ip: req.ip, | ||
} | ||
: undefined; | ||
|
||
const addOriginToCustomData: AddCustomDataFunc = async (_, res) => | ||
res.locals.customData.origin | ||
? { | ||
origin: res.locals.customData.origin, | ||
} | ||
: undefined; | ||
|
||
export const originCodes = [ | ||
"LEARN", | ||
"DEVELOPER", | ||
"DOCS", | ||
"DOTCOM", | ||
"GEMINI_CODE_ASSIST", | ||
"VSCODE", | ||
"OTHER", | ||
] as const; | ||
|
||
export type OriginCode = (typeof originCodes)[number]; | ||
|
||
interface OriginRule { | ||
regex: RegExp; | ||
code: OriginCode; | ||
} | ||
|
||
const ORIGIN_RULES: OriginRule[] = [ | ||
{ regex: /learn\.mongodb\.com/, code: "LEARN" }, | ||
{ regex: /mongodb\.com\/developer/, code: "DEVELOPER" }, | ||
{ regex: /mongodb\.com\/docs/, code: "DOCS" }, | ||
{ regex: /mongodb\.com\//, code: "DOTCOM" }, | ||
{ regex: /google-gemini-code-assist/, code: "GEMINI_CODE_ASSIST" }, | ||
{ regex: /vscode-mongodb-copilot/, code: "VSCODE" }, | ||
]; | ||
|
||
function getOriginCode(origin: string): OriginCode { | ||
for (const rule of ORIGIN_RULES) { | ||
if (rule.regex.test(origin)) { | ||
return rule.code; | ||
} | ||
} | ||
return "OTHER"; | ||
} | ||
|
||
const addOriginCodeToCustomData: AddCustomDataFunc = async (_, res) => { | ||
const origin = res.locals.customData.origin; | ||
return typeof origin === "string" && origin.length > 0 | ||
? { | ||
originCode: getOriginCode(origin), | ||
} | ||
: undefined; | ||
}; | ||
|
||
const addUserAgentToCustomData: AddCustomDataFunc = async (req) => | ||
req.headers["user-agent"] | ||
? { | ||
userAgent: req.headers["user-agent"], | ||
} | ||
: undefined; | ||
|
||
export type AddDefinedCustomDataFunc = ( | ||
...args: Parameters<AddCustomDataFunc> | ||
) => Promise<Exclude<RequestCustomData, undefined>>; | ||
|
||
export const addDefaultCustomData: AddDefinedCustomDataFunc = async ( | ||
req, | ||
res | ||
) => { | ||
return { | ||
...(await addIpToCustomData(req, res)), | ||
...(await addOriginToCustomData(req, res)), | ||
...(await addOriginCodeToCustomData(req, res)), | ||
...(await addUserAgentToCustomData(req, res)), | ||
}; | ||
}; |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related to chris' comment, if the generics aren't ever being used here (which they dont seem to be), lets remove them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the usage of the generics to all the invocations of these middlewares! Otherwise, the function definitions would need an unknown for the locals values.