@ulthon/ul-opencode-event 0.1.0 → 0.1.3

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.
@@ -0,0 +1,78 @@
1
+ import { renderTemplate } from './template.js';
2
+ import { createEmailSender } from './email.js';
3
+ // Default templates when channel doesn't have custom templates
4
+ const defaultTemplates = {
5
+ created: {
6
+ subject: '[OpenCode] Task Started - {{projectName}}',
7
+ body: 'Task started at {{timestamp}}\nProject: {{projectName}}\nSession: {{sessionId}}',
8
+ },
9
+ idle: {
10
+ subject: '[OpenCode] Task Completed - {{projectName}}',
11
+ body: 'Task completed at {{timestamp}}\nDuration: {{duration}}\nMessage: {{message}}',
12
+ },
13
+ error: {
14
+ subject: '[OpenCode] Task Error - {{projectName}}',
15
+ body: 'Error at {{timestamp}}\nError: {{error}}',
16
+ },
17
+ };
18
+ /**
19
+ * Creates an event handler that dispatches notifications to all enabled channels
20
+ */
21
+ export function createEventHandler(config) {
22
+ return {
23
+ handle: async (eventType, payload) => {
24
+ // Process each channel
25
+ for (const channel of config.channels) {
26
+ // Skip disabled channels
27
+ if (channel.enabled === false) {
28
+ continue;
29
+ }
30
+ // Check if this event type is enabled for the channel
31
+ const eventEnabled = channel.events[eventType];
32
+ if (eventEnabled === false) {
33
+ continue;
34
+ }
35
+ // Get template (channel-specific or default)
36
+ const template = channel.templates?.[eventType] ?? defaultTemplates[eventType];
37
+ // Prepare template variables from payload
38
+ const variables = {
39
+ eventType: payload.eventType,
40
+ timestamp: payload.timestamp,
41
+ projectName: payload.projectName,
42
+ sessionId: payload.sessionId,
43
+ message: payload.message,
44
+ error: payload.error,
45
+ duration: payload.duration,
46
+ };
47
+ // Render templates
48
+ const subject = template.subject ? renderTemplate(template.subject, variables) : '';
49
+ const body = template.body ? renderTemplate(template.body, variables) : '';
50
+ // Dispatch based on channel type
51
+ await dispatchToChannel(channel, subject, body);
52
+ }
53
+ },
54
+ };
55
+ }
56
+ /**
57
+ * Dispatch notification to the appropriate channel sender
58
+ */
59
+ async function dispatchToChannel(channel, subject, body) {
60
+ switch (channel.type) {
61
+ case 'smtp': {
62
+ const sender = createEmailSender(channel);
63
+ if (sender) {
64
+ await sender.send(subject, body);
65
+ }
66
+ break;
67
+ }
68
+ case 'feishu':
69
+ case 'dingtalk':
70
+ // Skip unsupported channel types silently
71
+ break;
72
+ default: {
73
+ // Log warning for unknown channel types
74
+ const _exhaustiveCheck = channel.type;
75
+ console.warn(`[EventHandler] Unsupported channel type: ${_exhaustiveCheck}`);
76
+ }
77
+ }
78
+ }