Repository navigation
Expand file tree
/
Copy pathslack-workflow.ts
More file actions
107 lines (92 loc) · 3.78 KB
/
Copy pathslack-workflow.ts
File metadata and controls
107 lines (92 loc) · 3.78 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
/**
* Slack Workflow Builder Integration
*
* This plugin sends OpenCode events to Slack via Workflow Builder webhooks.
*
* Setup Instructions:
* 1. Copy this file to ~/.config/opencode/plugin/slack-workflow.ts
* 2. Update the WEBHOOK_URL below with your Slack workflow webhook URL
* 3. Restart OpenCode
*
* To get your webhook URL:
* - Open Slack → Workflow Builder → Create workflow
* - Choose "Webhook" as the trigger
* - Add variables: eventType, sessionId, timestamp, message, eventInfo
* - Add a "Send message" step using those variables
* - Publish and copy the webhook URL
*
* Full guide: https://slack.com/help/articles/360041352714
*/
import type { Plugin } from '@opencode-ai/plugin';
import { createWebhookPlugin } from 'opencode-webhooks';
// ============================================================================
// Configuration
// ============================================================================
const WEBHOOK_URL = 'https://hooks.slack.com/workflows/T00000000/A00000000/123456789/your-webhook-id';
// ============================================================================
// Plugin Setup
// ============================================================================
// Export the plugin with explicit type annotation for OpenCode
const SlackWorkflowPlugin: Plugin = createWebhookPlugin({
webhooks: [
{
url: WEBHOOK_URL,
// Which events to send
events: [
'session.created',
'session.idle',
'session.deleted',
'session.error',
'session.resumed',
'message.updated',
'message.part.updated',
],
// Transform for Slack Workflow Builder
transformPayload: (payload) => {
const eventEmojis: Record<string, string> = {
'session.created': '🆕',
'session.idle': '💤',
'session.deleted': '🗑️',
'session.error': '❌',
'session.resumed': '▶️',
'message.updated': '💬',
'message.part.updated': '✏️',
};
const eventDescriptions: Record<string, string> = {
'session.created': 'A new OpenCode session has been created',
'session.idle': 'The OpenCode session has become idle',
'session.deleted': 'An OpenCode session has been deleted',
'session.error': 'An error occurred in the OpenCode session',
'session.resumed': 'The OpenCode session has resumed activity',
'message.updated': 'A message has been updated',
'message.part.updated': 'Part of a message has been updated',
};
const emoji = eventEmojis[payload.eventType] || '📢';
const description = eventDescriptions[payload.eventType] || 'OpenCode event triggered';
const availableKeys = Object.keys(payload);
// Extract message content if available
const messageContent = payload.content || payload.text || payload.message || '';
const messagePreview = messageContent ? `\n\nMessage: ${messageContent.substring(0, 100)}${messageContent.length > 100 ? '...' : ''}` : '';
// Flatten payload to top level for Slack Workflow Builder
return {
...payload,
eventType: payload.eventType,
sessionId: payload.sessionId || 'N/A',
timestamp: payload.timestamp,
message: `${emoji} ${payload.eventType}`,
eventInfo: `${description}${messagePreview}\n\nAvailable data: ${availableKeys.join(', ')}`,
messageContent: messageContent,
};
},
// Retry configuration
retry: {
maxAttempts: 3,
delayMs: 1000,
},
timeoutMs: 5000,
},
],
// Enable debug logging (set to false in production)
debug: false,
});
export default SlackWorkflowPlugin;