-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo-batching.js
More file actions
124 lines (100 loc) · 3.89 KB
/
Copy pathdemo-batching.js
File metadata and controls
124 lines (100 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
/**
* Demo script to show rate limiting and queuing in action
* Run with: TEST_SLACK_WEBHOOK_URL=https://hooks.slack.com/... node demo-batching.js
*/
import { WebhookPlugin, OpencodeEventType } from './dist/index.js';
const SLACK_WEBHOOK_URL = process.env.TEST_SLACK_WEBHOOK_URL;
if (!SLACK_WEBHOOK_URL) {
console.error('❌ Error: TEST_SLACK_WEBHOOK_URL environment variable is not set\n');
console.log('Usage:');
console.log(' TEST_SLACK_WEBHOOK_URL=https://hooks.slack.com/triggers/... node demo-batching.js\n');
process.exit(1);
}
console.log('🧪 Rate Limiting & Queuing Demo\n');
console.log('This demo shows how the plugin queues events to avoid rate limits.\n');
console.log('Slack allows 10 requests per minute, so we\'ll simulate sending 15 events.\n');
console.log('='.repeat(70) + '\n');
const plugin = new WebhookPlugin({
webhooks: [
{
url: SLACK_WEBHOOK_URL,
events: [
OpencodeEventType.SESSION_CREATED,
OpencodeEventType.SESSION_ERROR,
OpencodeEventType.FILE_EDITED,
],
// Configure rate limiting (10 requests per minute)
rateLimit: {
maxRequests: 10,
windowMs: 60000, // 1 minute
},
transformPayload: (payload) => {
return {
summary: `Event: ${payload.eventType}\nSession: ${payload.sessionId}\nTime: ${payload.timestamp}`,
eventType: payload.eventType,
sessionId: payload.sessionId,
timestamp: payload.timestamp,
};
},
retry: {
maxAttempts: 2,
delayMs: 1000,
},
timeoutMs: 10000,
},
],
debug: true,
async onResult(_event, results) {
for (const result of results) {
if (result.rateLimitDelayed) {
console.log(` 🔄 Event was queued and sent after rate limit cooldown`);
}
}
},
});
async function runDemo() {
console.log('📤 Sending 15 events...\n');
const results = [];
// Send 15 events in quick succession
for (let i = 0; i < 15; i++) {
const eventType = i % 3 === 0
? OpencodeEventType.SESSION_CREATED
: i % 3 === 1
? OpencodeEventType.SESSION_ERROR
: OpencodeEventType.FILE_EDITED;
const payload = {
sessionId: `session-${i + 1}`,
...(eventType === OpencodeEventType.SESSION_ERROR && { error: `Error ${i + 1}` }),
...(eventType === OpencodeEventType.FILE_EDITED && { filePath: `/test/file-${i + 1}.ts` }),
};
console.log(`Event ${i + 1}/15: ${eventType}`);
const result = await plugin.handleEvent(eventType, payload);
results.push(...result);
// Small delay between events
await new Promise(resolve => setTimeout(resolve, 100));
}
console.log('\n' + '='.repeat(70) + '\n');
console.log('📊 Summary:\n');
const immediatelySent = results.filter(r => !r.rateLimitDelayed && r.success && r.attempts > 0).length;
const queued = results.filter(r => r.rateLimitDelayed === undefined).length;
const failed = results.filter(r => !r.success).length;
console.log(`✅ Sent immediately: ${immediatelySent} (under rate limit)`);
console.log(`📦 Queued for later: ${queued} (over rate limit)`);
console.log(`❌ Failed: ${failed}`);
console.log('\n' + '='.repeat(70) + '\n');
console.log('ℹ️ Note: Queued events will be sent after the rate limit window resets (60 seconds)');
console.log(' Each queued event will be sent individually with its original data.\n');
console.log(' Check your Slack channel in ~60 seconds for the queued events!\n');
// Wait for queue to flush
console.log('⏳ Waiting 65 seconds for queue to flush...\n');
await new Promise(resolve => setTimeout(resolve, 65000));
console.log('✅ Demo complete! Check your Slack channel for all messages.\n');
// Cleanup
plugin.destroy();
process.exit(0);
}
runDemo().catch(error => {
console.error('❌ Error:', error);
process.exit(1);
});