-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add agent context provider (#921)
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
b47cfcb
commit a1b1e5a
Showing
26 changed files
with
189 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,49 @@ | ||
import type { DependencyManager } from '../../plugins' | ||
import type { Wallet } from '../../wallet' | ||
|
||
import { InjectionSymbols } from '../../constants' | ||
import { AgentConfig } from '../AgentConfig' | ||
|
||
export class AgentContext { | ||
/** | ||
* Dependency manager holds all dependencies for the current context. Possibly a child of a parent dependency manager, | ||
* in which case all singleton dependencies from the parent context are also available to this context. | ||
*/ | ||
public readonly dependencyManager: DependencyManager | ||
|
||
/** | ||
* An identifier that allows to correlate this context across sessions. This identifier is created by the `AgentContextProvider` | ||
* and should only be meaningful to the `AgentContextProvider`. The `contextCorrelationId` MUST uniquely identity the context and | ||
* should be enough to start a new session. | ||
* | ||
* An example of the `contextCorrelationId` is for example the id of the `TenantRecord` that is associated with this context when using the tenant module. | ||
* The `TenantAgentContextProvider` will set the `contextCorrelationId` to the `TenantRecord` id when creating the context, and will be able to create a context | ||
* for a specific tenant using the `contextCorrelationId`. | ||
*/ | ||
public readonly contextCorrelationId: string | ||
|
||
public constructor({ | ||
dependencyManager, | ||
contextCorrelationId, | ||
}: { | ||
dependencyManager: DependencyManager | ||
contextCorrelationId: string | ||
}) { | ||
this.dependencyManager = dependencyManager | ||
this.contextCorrelationId = contextCorrelationId | ||
} | ||
|
||
/** | ||
* Convenience method to access the agent config for the current context. | ||
*/ | ||
public get config() { | ||
return this.dependencyManager.resolve(AgentConfig) | ||
} | ||
|
||
/** | ||
* Convenience method to access the wallet for the current context. | ||
*/ | ||
public get wallet() { | ||
return this.dependencyManager.resolve<Wallet>(InjectionSymbols.Wallet) | ||
} | ||
} |
This file contains 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,17 @@ | ||
import type { AgentContext } from './AgentContext' | ||
|
||
export interface AgentContextProvider { | ||
/** | ||
* Find the agent context based for an inbound message. It's possible to provide a contextCorrelationId to make it | ||
* easier for the context provider implementation to correlate inbound messages to the correct context. This can be useful if | ||
* a plaintext message is passed and the context provider can't determine the context based on the recipient public keys | ||
* of the inbound message. | ||
* | ||
* The implementation of this method could range from a very simple one that always returns the same context to | ||
* a complex one that manages the context for a multi-tenant agent. | ||
*/ | ||
getContextForInboundMessage( | ||
inboundMessage: unknown, | ||
options?: { contextCorrelationId?: string } | ||
): Promise<AgentContext> | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/core/src/agent/context/DefaultAgentContextProvider.ts
This file contains 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,24 @@ | ||
import type { AgentContextProvider } from './AgentContextProvider' | ||
|
||
import { injectable } from '../../plugins' | ||
|
||
import { AgentContext } from './AgentContext' | ||
|
||
/** | ||
* Default implementation of AgentContextProvider. | ||
* | ||
* Holds a single `AgentContext` instance that will be used for all messages, i.e. a | ||
* a single tenant agent. | ||
*/ | ||
@injectable() | ||
export class DefaultAgentContextProvider implements AgentContextProvider { | ||
private agentContext: AgentContext | ||
|
||
public constructor(agentContext: AgentContext) { | ||
this.agentContext = agentContext | ||
} | ||
|
||
public async getContextForInboundMessage(): Promise<AgentContext> { | ||
return this.agentContext | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/core/src/agent/context/__tests__/DefaultAgentContextProvider.test.ts
This file contains 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,18 @@ | ||
import type { AgentContextProvider } from '../AgentContextProvider' | ||
|
||
import { getAgentContext } from '../../../../tests/helpers' | ||
import { DefaultAgentContextProvider } from '../DefaultAgentContextProvider' | ||
|
||
const agentContext = getAgentContext() | ||
|
||
describe('DefaultAgentContextProvider', () => { | ||
describe('getContextForInboundMessage()', () => { | ||
test('returns the agent context provided in the constructor', async () => { | ||
const agentContextProvider: AgentContextProvider = new DefaultAgentContextProvider(agentContext) | ||
|
||
const message = {} | ||
|
||
await expect(agentContextProvider.getContextForInboundMessage(message)).resolves.toBe(agentContext) | ||
}) | ||
}) | ||
}) |
This file contains 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,3 @@ | ||
export * from './AgentContext' | ||
export * from './AgentContextProvider' | ||
export * from './DefaultAgentContextProvider' |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './AgentContext' | ||
export * from './context' |
This file contains 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 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 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 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 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 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 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.