-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Timo Glastra <timo@animo.id>
- Loading branch information
1 parent
b1979ca
commit 150b0bf
Showing
28 changed files
with
171 additions
and
46 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,14 @@ | ||
import type { Wallet } from '../../wallet' | ||
import type { AgentConfig } from '../AgentConfig' | ||
|
||
export interface AgentContext { | ||
readonly wallet: Wallet | ||
readonly config: AgentConfig | ||
|
||
/** | ||
* An identifier that allows to correlate this context across usages. An example of the contextCorrelationId could be | ||
* the id of the `TenantRecord` that is associated with this context. The AgentContextProvider can use this identifier to | ||
* correlate an inbound message to a specific context (if the message is not encrypted, it's impossible to correlate it to a tenant) | ||
*/ | ||
readonly contextCorrelationId: string | ||
} |
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> | ||
} |
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,15 @@ | ||
import type { Wallet } from '../../wallet' | ||
import type { AgentConfig } from '../AgentConfig' | ||
import type { AgentContext } from './AgentContext' | ||
|
||
export class DefaultAgentContext implements AgentContext { | ||
public readonly wallet: Wallet | ||
public readonly config: AgentConfig | ||
public readonly contextCorrelationId: string | ||
|
||
public constructor(wallet: Wallet, config: AgentConfig, contextCorrelationId: string) { | ||
this.wallet = wallet | ||
this.config = config | ||
this.contextCorrelationId = contextCorrelationId | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import type { AgentContext } from './AgentContext' | ||
import type { AgentContextProvider } from './AgentContextProvider' | ||
|
||
/** | ||
* Default implementation of AgentContextProvider. | ||
* | ||
* Holds a single `AgentContext` instance that will be used for all messages, i.e. a | ||
* a single tenant agent. | ||
*/ | ||
export class DefaultAgentContextProvider implements AgentContextProvider { | ||
private agentContext: AgentContext | ||
|
||
public constructor(agentContext: AgentContext) { | ||
this.agentContext = agentContext | ||
} | ||
|
||
public async getContextForInboundMessage(): Promise<AgentContext> { | ||
return this.agentContext | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import type { AgentContextProvider } from '../AgentContextProvider' | ||
|
||
import { getAgentConfig } from '../../../../tests/helpers' | ||
import { MockAgentContext } from '../../../../tests/mocks' | ||
import { DefaultAgentContextProvider } from '../DefaultAgentContextProvider' | ||
|
||
const agentConfig = getAgentConfig('DefaultAgentContextProvider') | ||
|
||
describe('DefaultAgentContextProvider', () => { | ||
describe('getContextForInboundMessage()', () => { | ||
test('returns the agent context provided in the constructor', async () => { | ||
const agentContext = new MockAgentContext(agentConfig) | ||
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,4 @@ | ||
export * from './AgentContext' | ||
export * from './DefaultAgentContext' | ||
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
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.