@aichatwar/shared 1.0.123 → 1.0.125

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,6 @@ export declare abstract class Listener<T extends BaseEvent> {
14
14
  private crashHandlerSetup;
15
15
  constructor(consumer: Consumer);
16
16
  private setupCrashHandler;
17
- ack(): Promise<void>;
17
+ ack(payload?: EachMessagePayload): Promise<void>;
18
18
  listen(): Promise<void>;
19
19
  }
@@ -39,15 +39,17 @@ class Listener {
39
39
  this.crashHandlerSetup = true;
40
40
  }
41
41
  // Manual acknowledgment method
42
- ack() {
42
+ // Accepts optional payload for custom consumer patterns (e.g., realtime-gateway)
43
+ ack(payload) {
43
44
  return __awaiter(this, void 0, void 0, function* () {
44
- if (!this.currentPayload) {
45
- throw new Error('No current payload to acknowledge');
45
+ const targetPayload = payload || this.currentPayload;
46
+ if (!targetPayload) {
47
+ throw new Error('No current payload to acknowledge. Either set currentPayload or pass payload parameter.');
46
48
  }
47
49
  yield this.consumer.commitOffsets([{
48
- topic: this.currentPayload.topic,
49
- partition: this.currentPayload.partition,
50
- offset: (BigInt(this.currentPayload.message.offset) + BigInt(1)).toString()
50
+ topic: targetPayload.topic,
51
+ partition: targetPayload.partition,
52
+ offset: (BigInt(targetPayload.message.offset) + BigInt(1)).toString()
51
53
  }]);
52
54
  console.log(`Message manually acknowledged for topic: ${this.topic}`);
53
55
  });
@@ -57,29 +59,33 @@ class Listener {
57
59
  var _a;
58
60
  // Prevent multiple simultaneous listen attempts
59
61
  if (this.isListening) {
62
+ console.log(`⚠️ [${this.topic}] Listener already listening, skipping...`);
60
63
  return;
61
64
  }
62
65
  try {
66
+ console.log(`🔌 [${this.topic}] Connecting consumer with groupId: ${this.groupId}`);
63
67
  yield this.consumer.connect();
68
+ console.log(`✅ [${this.topic}] Consumer connected`);
64
69
  // Crash handler is set up in constructor, no need to add it again
70
+ console.log(`📋 [${this.topic}] Subscribing to topic...`);
65
71
  yield this.consumer.subscribe({
66
72
  topic: this.topic,
67
73
  fromBeginning: false,
68
74
  });
75
+ console.log(`✅ [${this.topic}] Successfully subscribed to topic with groupId: ${this.groupId}`);
69
76
  // Log success message, especially if there were previous retries
70
77
  if (this.retryCount > 0) {
71
78
  console.log(`✅ [${this.topic}] Successfully connected after ${this.retryCount} retry attempts. Listening with groupId: ${this.groupId}`);
72
79
  }
73
- else {
74
- console.log(`✅ [${this.topic}] Successfully connected. Listening to topic with groupId: ${this.groupId}`);
75
- }
76
80
  // Reset retry count on successful connection
77
81
  this.retryCount = 0;
82
+ console.log(`🚀 [${this.topic}] Starting consumer.run() with groupId: ${this.groupId}`);
78
83
  yield this.consumer.run({
79
84
  // CRITICAL: Disable auto-commit to prevent message loss during rebalancing
80
85
  // Offsets are only committed when we explicitly call ack()
81
86
  autoCommit: false,
82
87
  eachMessage: (payload) => __awaiter(this, void 0, void 0, function* () {
88
+ var _a;
83
89
  if (!payload.message.value) {
84
90
  // Acknowledge empty messages to avoid blocking
85
91
  yield this.consumer.commitOffsets([{
@@ -89,7 +95,7 @@ class Listener {
89
95
  }]);
90
96
  return;
91
97
  }
92
- console.log(`Message received -> topic: ${this.topic}, groupId: ${this.groupId}, offset: ${payload.message.offset}`);
98
+ console.log(`📨 [${this.topic}] Message received -> groupId: ${this.groupId}, partition: ${payload.partition}, offset: ${payload.message.offset}, key: ${((_a = payload.message.key) === null || _a === void 0 ? void 0 : _a.toString()) || 'none'}`);
93
99
  // Store current payload for manual ack
94
100
  this.currentPayload = payload;
95
101
  try {
@@ -18,12 +18,13 @@ class Publisher {
18
18
  }
19
19
  publish(data) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
+ var _a, _b;
21
22
  if (!this.producer)
22
23
  throw new Error('Producer not defined');
23
24
  let lastError = null;
24
25
  for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
25
26
  try {
26
- yield this.producer.send({
27
+ const result = yield this.producer.send({
27
28
  topic: this.topic,
28
29
  messages: [{
29
30
  value: JSON.stringify(data),
@@ -31,7 +32,12 @@ class Publisher {
31
32
  timestamp: Date.now().toString()
32
33
  }],
33
34
  });
34
- console.log(`Event published successfully: ${this.topic}`, data);
35
+ console.log(`✅ Event published successfully: ${this.topic}`, {
36
+ topic: this.topic,
37
+ partition: (_a = result[0]) === null || _a === void 0 ? void 0 : _a.partition,
38
+ offset: (_b = result[0]) === null || _b === void 0 ? void 0 : _b.offset,
39
+ data: JSON.stringify(data).substring(0, 200) // First 200 chars for brevity
40
+ });
35
41
  return; // Success, exit retry loop
36
42
  }
37
43
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aichatwar/shared",
3
- "version": "1.0.123",
3
+ "version": "1.0.125",
4
4
  "main": "./build/index.js",
5
5
  "typs": "./build/index.d.ts",
6
6
  "files": [