Fix: Defer NATS subscription until after connection
- Remove setupSubscription() call from constructor - Call setupSubscription() on first handle() invocation - Prevents 'NATS not connected' error during initialization - Subscription happens after NATS client is connected 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -65,6 +65,7 @@ export class SendMessageTool extends ToolHandler<SendMessageInput, any> {
|
||||
|
||||
export class ReceiveMessagesTool extends ToolHandler<ReceiveMessagesInput, any> {
|
||||
private messages: any[] = [];
|
||||
private subscribed = false;
|
||||
|
||||
constructor(private natsClient: NatsClient) {
|
||||
super(
|
||||
@@ -74,20 +75,29 @@ export class ReceiveMessagesTool extends ToolHandler<ReceiveMessagesInput, any>
|
||||
},
|
||||
ReceiveMessagesSchema,
|
||||
);
|
||||
this.setupSubscription();
|
||||
}
|
||||
|
||||
private setupSubscription() {
|
||||
if (this.subscribed) return;
|
||||
|
||||
const myRole = detectRole();
|
||||
const channel = `dev.messages.${myRole}`;
|
||||
|
||||
// Subscribe to messages for this role
|
||||
this.natsClient.subscribe(channel, (msg) => {
|
||||
this.messages.push(msg);
|
||||
});
|
||||
// Subscribe to messages for this role (only if NATS is connected)
|
||||
try {
|
||||
this.natsClient.subscribe(channel, (msg) => {
|
||||
this.messages.push(msg);
|
||||
});
|
||||
this.subscribed = true;
|
||||
} catch (error) {
|
||||
// NATS not connected yet, will be called later
|
||||
}
|
||||
}
|
||||
|
||||
protected async handle(input: ReceiveMessagesInput, _context: ToolContext): Promise<any> {
|
||||
// Setup subscription on first use (after NATS is connected)
|
||||
this.setupSubscription();
|
||||
|
||||
const limit = input.limit || 10;
|
||||
const recentMessages = this.messages.slice(-limit);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user