-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add agent custom document loader #321
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
Merged
+85
−1
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b9e29d7
feat: add agent custom document loader
GHkrishna 31325c3
fix: formatting
GHkrishna b597c95
fix: formatting
GHkrishna 751dfa1
feat: add appropriate warning for incorrect custom document loader in…
GHkrishna 5b1ceb7
fix: formatting
GHkrishna a8e27b4
fix: formatting
GHkrishna f271bb4
fix: error handling
GHkrishna 586dfe3
fix: formatting
GHkrishna 1bd804a
fix: formatting
GHkrishna 3177d89
fix: coderabbit suggestions
GHkrishna 712f262
fix: reviewer suggestions
GHkrishna 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* eslint-disable no-console */ | ||
| export const isCustomDocumentLoaderEnabled = (): boolean => { | ||
| const flag = process.env.ENABLE_CUSTOM_DOCUMENT_LOADER ?? 'false' | ||
| const isCustomDocumentLoaderEnabled = flag.toLowerCase() === 'true' | ||
|
|
||
| if (isCustomDocumentLoaderEnabled) { | ||
| if (!process.env.DEPRECATED_DOMAIN || !process.env.CURRENT_DOMAIN) { | ||
| console.debug('Invalid configuration set for enabling custom document loader') | ||
| console.info( | ||
| "If you are unsure about what the error is about. Try setting the 'ENABLE_CUSTOM_DOCUMENT_LOADER' flag in the env variable to false", | ||
| ) | ||
| throw new Error( | ||
| `Custom document loader for the agent is enabled but the deprecated domain and updated domain is not set`, | ||
| ) | ||
| } | ||
| console.warn( | ||
| `Custom document loader for the agent is enabled. Resolution of all URLs from the deprecated domain(${process.env.DEPRECATED_DOMAIN}) will actually be resolved from the current, updated domain(${process.env.CURRENT_DOMAIN})`, | ||
| ) | ||
|
GHkrishna marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return isCustomDocumentLoaderEnabled | ||
| } | ||
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,47 @@ | ||
| import type { AgentContext, DocumentLoader } from '@credo-ts/core' | ||
| import type { DocumentLoaderResult } from '@credo-ts/core/build/modules/vc/data-integrity/jsonldUtil' | ||
|
|
||
| import { CredoError } from '@credo-ts/core' | ||
| import { defaultDocumentLoader } from '@credo-ts/core/build/modules/vc/data-integrity/libraries/documentLoader' | ||
|
|
||
| /** | ||
| * Check if URL belongs to CREDEBL schema domain | ||
| */ | ||
| function isW3CDeprecatedUrl(url: string, agentContext: AgentContext): boolean { | ||
| agentContext.config.logger.debug( | ||
| `Checking if w3c url(${url}) contains deprecated domain for agent: ${agentContext.config.label}`, | ||
| ) | ||
| return url.startsWith(process.env.DEPRECATED_DOMAIN!) | ||
| } | ||
|
|
||
| /** | ||
| * For JSON-LD schemas replace deprecated domain to migrated/updated domain | ||
| */ | ||
| function replaceUrl(url: string, agent: AgentContext): string { | ||
| agent.config.logger.debug(`Replacing deprecated domain with updated domain`) | ||
| return url.replace(process.env.DEPRECATED_DOMAIN!, process.env.CURRENT_DOMAIN!) | ||
| } | ||
|
|
||
| /** | ||
| * Custom loader that extends Credo's default loader | ||
| */ | ||
| export const CustomDocumentLoader = (agentContext: AgentContext): DocumentLoader => { | ||
| const defaultLoader = defaultDocumentLoader(agentContext) | ||
|
|
||
| return async function (url: string): Promise<DocumentLoaderResult> { | ||
| try { | ||
| // Intercept credebl schemas | ||
| if (isW3CDeprecatedUrl(url, agentContext)) { | ||
| agentContext.config.logger.debug( | ||
| `Found w3c url(${url}) containing deprecated domain for agent: ${agentContext.config.label}`, | ||
| ) | ||
| url = replaceUrl(url, agentContext) | ||
| } | ||
|
|
||
| agentContext.config.logger.debug(`Passing url(${url}) to default loader`) | ||
| return await defaultLoader(url) | ||
| } catch (error) { | ||
| throw new CredoError(`Failed to load document for ${url}`, { cause: error as Error }) | ||
| } | ||
|
GHkrishna marked this conversation as resolved.
|
||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.