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:
root
2025-10-09 07:26:10 +02:00
parent 3d08b825a6
commit 5202761e2a

View File

@@ -65,6 +65,7 @@ export class SendMessageTool extends ToolHandler<SendMessageInput, any> {
export class ReceiveMessagesTool extends ToolHandler<ReceiveMessagesInput, any> { export class ReceiveMessagesTool extends ToolHandler<ReceiveMessagesInput, any> {
private messages: any[] = []; private messages: any[] = [];
private subscribed = false;
constructor(private natsClient: NatsClient) { constructor(private natsClient: NatsClient) {
super( super(
@@ -74,20 +75,29 @@ export class ReceiveMessagesTool extends ToolHandler<ReceiveMessagesInput, any>
}, },
ReceiveMessagesSchema, ReceiveMessagesSchema,
); );
this.setupSubscription();
} }
private setupSubscription() { private setupSubscription() {
if (this.subscribed) return;
const myRole = detectRole(); const myRole = detectRole();
const channel = `dev.messages.${myRole}`; const channel = `dev.messages.${myRole}`;
// Subscribe to messages for this role // Subscribe to messages for this role (only if NATS is connected)
this.natsClient.subscribe(channel, (msg) => { try {
this.messages.push(msg); 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> { 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 limit = input.limit || 10;
const recentMessages = this.messages.slice(-limit); const recentMessages = this.messages.slice(-limit);