Skip to content
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

fix: listen to incoming messages on agent initialize not constructor #1542

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface AgentOptions<AgentModules extends AgentModulesInput> {
// Any makes sure you can use Agent as a type without always needing to specify the exact generics for the agent
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Agent<AgentModules extends AgentModulesInput = any> extends BaseAgent<AgentModules> {
public messageSubscription: Subscription
public messageSubscription?: Subscription

public constructor(options: AgentOptions<AgentModules>, dependencyManager = new DependencyManager()) {
const agentConfig = new AgentConfig(options.config, options.dependencies)
Expand Down Expand Up @@ -110,26 +110,6 @@ export class Agent<AgentModules extends AgentModulesInput = any> extends BaseAge
}

super(agentConfig, dependencyManager)

const stop$ = this.dependencyManager.resolve<Subject<boolean>>(InjectionSymbols.Stop$)

// Listen for new messages (either from transports or somewhere else in the framework / extensions)
this.messageSubscription = this.eventEmitter
.observable<AgentMessageReceivedEvent>(AgentEventTypes.AgentMessageReceived)
.pipe(
takeUntil(stop$),
concatMap((e) =>
this.messageReceiver
.receiveMessage(e.payload.message, {
connection: e.payload.connection,
contextCorrelationId: e.payload.contextCorrelationId,
})
.catch((error) => {
this.logger.error('Failed to process message', { error })
})
)
)
.subscribe()
}

public registerInboundTransport(inboundTransport: InboundTransport) {
Expand Down Expand Up @@ -199,6 +179,26 @@ export class Agent<AgentModules extends AgentModulesInput = any> extends BaseAge
await this.mediator.initialize()
await this.mediationRecipient.initialize()

const stop$ = this.dependencyManager.resolve<Subject<boolean>>(InjectionSymbols.Stop$)

// Listen for new messages (either from transports or somewhere else in the framework / extensions)
this.messageSubscription = this.eventEmitter
.observable<AgentMessageReceivedEvent>(AgentEventTypes.AgentMessageReceived)
.pipe(
takeUntil(stop$),
concatMap((e) =>
this.messageReceiver
.receiveMessage(e.payload.message, {
connection: e.payload.connection,
contextCorrelationId: e.payload.contextCorrelationId,
})
.catch((error) => {
this.logger.error('Failed to process message', { error })
})
)
)
.subscribe()

this._isInitialized = true
}

Expand Down