Skip to content

Commit

Permalink
Add some channel logging
Browse files Browse the repository at this point in the history
  • Loading branch information
parzival418 committed Nov 27, 2024
1 parent 0c7eb51 commit 3d945c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dev:server": "nx serve @magickml/server",
"bucket:init": "ts-node ./scripts/init-bucket.ts",
"build-release": "nx run-many --target=build --parallel=20 --projects=tag:release",
"build-release:watch": "nx run-many --target=build --parallel=20 --projects=tag:release --watch",
"build-client": "npx nx build @magickml/client",
"build-cloud-agent-manager": "npm run generateClients && npx nx build @magickml/cloud-agent-manager-app",
"build-cloud-agent-worker": "npm run generateClients && npx nx build @magickml/cloud-agent-worker-app",
Expand Down
19 changes: 18 additions & 1 deletion packages/server/agents/src/lib/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,35 @@ type ChannelEvents = {
class Channel extends (EventEmitter as new () => TypedEmitter<ChannelEvents>) {
constructor(private channelId: string, private agent: Agent) {
super()
console.log(`Channel ${channelId} constructed`)
}

emit<K extends keyof ChannelEvents>(
event: K,
data: Parameters<ChannelEvents[K]>[0]
): boolean {
console.log(`Channel ${this.channelId} emitting ${event}:`, data)
return super.emit(event, ...([data] as Parameters<ChannelEvents[K]>))
}

emitToAgent<K extends keyof AgentEvents>(
event: K,
data: Parameters<AgentEvents[K]>[0]
): boolean {
console.log(`Channel ${this.channelId} emitting to agent ${event}:`, data)
return this.agent.emit(event, {
...data,
channel: this.channelId,
})
}

on<K extends keyof ChannelEvents>(
event: K,
listener: ChannelEvents[K]
): this {
console.log(`Channel ${this.channelId} adding listener for ${event}`)
return super.on(event, listener)
}
}

const plugins = [CorePlugin, KnowledgePlugin, DiscordPlugin, SlackPlugin]
Expand Down Expand Up @@ -204,17 +215,21 @@ export class Agent

this.logger.info('New agent created: %s | %s', this.name, this.id)

// Set up global event routing to channels
// Set up global event routing to channels with debug logging
this.on('messageReceived', (data: ActionPayload) => {
console.log('Agent received messageReceived event:', data)
const channelId = (data as any)?.channel
if (channelId && this.channels.has(channelId)) {
console.log(`Forwarding messageReceived to channel ${channelId}`)
this.channels.get(channelId)!.emit('messageReceived', data)
}
})

this.on('message', (data: EventPayload) => {
console.log('Agent received message event:', data)
const channelId = (data as any)?.channel
if (channelId && this.channels.has(channelId)) {
console.log(`Forwarding message to channel ${channelId}`)
this.channels.get(channelId)!.emit('message', data)
}
})
Expand Down Expand Up @@ -527,7 +542,9 @@ export class Agent

// Add channel method to get/create channel instance
channel(channelId: string): Channel {
console.log(`Getting/creating channel for ${channelId}`)
if (!this.channels.has(channelId)) {
console.log(`Creating new channel for ${channelId}`)
const channel = new Channel(channelId, this)
this.channels.set(channelId, channel)
}
Expand Down
2 changes: 1 addition & 1 deletion portal/cloud
Submodule cloud updated from c739a7 to 6697b5

0 comments on commit 3d945c6

Please sign in to comment.