@memberjunction/messaging-adapters 0.0.1 → 5.17.0

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.
Files changed (58) hide show
  1. package/README.md +231 -43
  2. package/dist/base/BaseMessagingAdapter.d.ts +428 -0
  3. package/dist/base/BaseMessagingAdapter.d.ts.map +1 -0
  4. package/dist/base/BaseMessagingAdapter.js +934 -0
  5. package/dist/base/BaseMessagingAdapter.js.map +1 -0
  6. package/dist/base/message-formatter.d.ts +70 -0
  7. package/dist/base/message-formatter.d.ts.map +1 -0
  8. package/dist/base/message-formatter.js +201 -0
  9. package/dist/base/message-formatter.js.map +1 -0
  10. package/dist/base/types.d.ts +211 -0
  11. package/dist/base/types.d.ts.map +1 -0
  12. package/dist/base/types.js +6 -0
  13. package/dist/base/types.js.map +1 -0
  14. package/dist/index.d.ts +72 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +76 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/slack/SlackAdapter.d.ts +141 -0
  19. package/dist/slack/SlackAdapter.d.ts.map +1 -0
  20. package/dist/slack/SlackAdapter.js +291 -0
  21. package/dist/slack/SlackAdapter.js.map +1 -0
  22. package/dist/slack/SlackMessagingExtension.d.ts +148 -0
  23. package/dist/slack/SlackMessagingExtension.d.ts.map +1 -0
  24. package/dist/slack/SlackMessagingExtension.js +433 -0
  25. package/dist/slack/SlackMessagingExtension.js.map +1 -0
  26. package/dist/slack/slack-block-builder.d.ts +133 -0
  27. package/dist/slack/slack-block-builder.d.ts.map +1 -0
  28. package/dist/slack/slack-block-builder.js +748 -0
  29. package/dist/slack/slack-block-builder.js.map +1 -0
  30. package/dist/slack/slack-formatter.d.ts +37 -0
  31. package/dist/slack/slack-formatter.d.ts.map +1 -0
  32. package/dist/slack/slack-formatter.js +116 -0
  33. package/dist/slack/slack-formatter.js.map +1 -0
  34. package/dist/slack/slack-interactivity.d.ts +38 -0
  35. package/dist/slack/slack-interactivity.d.ts.map +1 -0
  36. package/dist/slack/slack-interactivity.js +414 -0
  37. package/dist/slack/slack-interactivity.js.map +1 -0
  38. package/dist/slack/slack-routes.d.ts +35 -0
  39. package/dist/slack/slack-routes.d.ts.map +1 -0
  40. package/dist/slack/slack-routes.js +98 -0
  41. package/dist/slack/slack-routes.js.map +1 -0
  42. package/dist/teams/TeamsAdapter.d.ts +155 -0
  43. package/dist/teams/TeamsAdapter.d.ts.map +1 -0
  44. package/dist/teams/TeamsAdapter.js +383 -0
  45. package/dist/teams/TeamsAdapter.js.map +1 -0
  46. package/dist/teams/TeamsMessagingExtension.d.ts +75 -0
  47. package/dist/teams/TeamsMessagingExtension.d.ts.map +1 -0
  48. package/dist/teams/TeamsMessagingExtension.js +176 -0
  49. package/dist/teams/TeamsMessagingExtension.js.map +1 -0
  50. package/dist/teams/teams-card-builder.d.ts +94 -0
  51. package/dist/teams/teams-card-builder.d.ts.map +1 -0
  52. package/dist/teams/teams-card-builder.js +648 -0
  53. package/dist/teams/teams-card-builder.js.map +1 -0
  54. package/dist/teams/teams-formatter.d.ts +39 -0
  55. package/dist/teams/teams-formatter.d.ts.map +1 -0
  56. package/dist/teams/teams-formatter.js +107 -0
  57. package/dist/teams/teams-formatter.js.map +1 -0
  58. package/package.json +40 -7
package/README.md CHANGED
@@ -1,45 +1,233 @@
1
1
  # @memberjunction/messaging-adapters
2
2
 
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
4
-
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
6
-
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
8
-
9
- ## Purpose
10
-
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `@memberjunction/messaging-adapters`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
15
-
16
- ## What is OIDC Trusted Publishing?
17
-
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
19
-
20
- ## Setup Instructions
21
-
22
- To properly configure OIDC trusted publishing for this package:
23
-
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
28
-
29
- ## DO NOT USE THIS PACKAGE
30
-
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
36
-
37
- ## More Information
38
-
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
42
-
43
- ---
44
-
45
- **Maintained for OIDC setup purposes only**
3
+ Slack and Microsoft Teams integrations for MemberJunction AI agents. Receive messages from messaging platforms, route them to MJ AI agents via `AgentRunner.RunAgentInConversation()`, and stream rich formatted responses back to users.
4
+
5
+ ## Overview
6
+
7
+ This package provides ready-to-use server extensions that connect messaging platforms to MJ AI agents:
8
+
9
+ - **Slack** — Webhook + Socket Mode integration with Block Kit rich formatting, interactive modals, slash commands, and HMAC-SHA256 signature verification
10
+ - **Microsoft Teams** — Bot Framework integration with Adaptive Card formatting, form handling, and JWT validation
11
+
12
+ Built on the [`@memberjunction/server-extensions-core`](../ServerExtensionsCore/) framework, these adapters are auto-discovered by MJServer via `@RegisterClass` and configured in `mj.config.cjs` — no MJServer source code changes needed.
13
+
14
+ ## Features
15
+
16
+ - **Multi-agent routing** — Users can @mention different agents (e.g., `@Research Agent`). 4-pass name matching handles full names, prefixes, bare names, and anywhere-in-message patterns. Thread affinity remembers the agent across follow-up messages.
17
+ - **Agent delegation** — When an agent returns `payload.invokeAgent`, the adapter auto-delegates to the target agent (up to 3 chained hops), mirroring MJ Explorer behavior.
18
+ - **User identity mapping** — Platform user emails are mapped to MJ `UserInfo` records for proper per-user permission scoping. Falls back to a configured service account.
19
+ - **Streaming responses** — Progressive message updates as the agent generates content, with configurable throttle intervals to avoid rate limiting.
20
+ - **Rich formatting** — Markdown responses convert to Slack Block Kit blocks or Teams Adaptive Cards, with agent identity headers, artifact deep-links, action buttons, response forms, and metadata footers.
21
+ - **Conversation context** — Thread history is passed to agents for multi-turn conversations. MJ Conversations and Artifacts are automatically created.
22
+ - **Interactive forms** — Response forms render as Slack modals or Teams Adaptive Card forms, with round-trip back to the agent.
23
+ - **Slash commands** — Slack slash commands auto-generated from agent names, with config overrides.
24
+ - **Deep links** — "View in MJ Explorer" buttons link to artifacts and conversations.
25
+ - **Platform-specific auth** — Slack uses HMAC-SHA256 signature verification with replay protection; Teams uses Bot Framework JWT validation.
26
+ - **Dual Slack connection modes** — HTTP webhooks (production) or Socket Mode (local dev, no public URL needed).
27
+
28
+ ## Quick Start
29
+
30
+ See **[SETUP_GUIDE.md](SETUP_GUIDE.md)** for detailed step-by-step instructions including Slack App creation, Azure Bot registration, and Teams manifest setup.
31
+
32
+ ### Slack (minimal config)
33
+
34
+ ```javascript
35
+ // mj.config.cjs
36
+ module.exports = {
37
+ serverExtensions: [{
38
+ Enabled: true,
39
+ DriverClass: 'SlackMessagingExtension',
40
+ RootPath: '/webhook/slack',
41
+ Settings: {
42
+ DefaultAgentName: 'Sage',
43
+ ContextUserEmail: 'bot@company.com',
44
+ BotToken: process.env.SLACK_BOT_TOKEN,
45
+ SigningSecret: process.env.SLACK_SIGNING_SECRET,
46
+ }
47
+ }]
48
+ };
49
+ ```
50
+
51
+ ### Teams (minimal config)
52
+
53
+ ```javascript
54
+ // mj.config.cjs
55
+ module.exports = {
56
+ serverExtensions: [{
57
+ Enabled: true,
58
+ DriverClass: 'TeamsMessagingExtension',
59
+ RootPath: '/webhook/teams',
60
+ Settings: {
61
+ DefaultAgentName: 'Sage',
62
+ ContextUserEmail: 'bot@company.com',
63
+ MicrosoftAppId: process.env.MICROSOFT_APP_ID,
64
+ MicrosoftAppPassword: process.env.MICROSOFT_APP_PASSWORD,
65
+ }
66
+ }]
67
+ };
68
+ ```
69
+
70
+ ### Tree-Shaking Prevention
71
+
72
+ Ensure the `@RegisterClass` decorators fire at module load time:
73
+
74
+ ```typescript
75
+ import { LoadMessagingAdapters } from '@memberjunction/messaging-adapters';
76
+ LoadMessagingAdapters();
77
+ ```
78
+
79
+ ## Architecture
80
+
81
+ ```
82
+ ┌─────────────────────────────────────┐
83
+ │ mj.config.cjs │
84
+ │ serverExtensions: [{ │
85
+ │ DriverClass: 'SlackMessaging...' │
86
+ │ Settings: { ... } │
87
+ │ }] │
88
+ └──────────────┬──────────────────────┘
89
+ │
90
+ ┌──────────────▼──────────────────────┐
91
+ │ ServerExtensionLoader │
92
+ │ ClassFactory.CreateInstance( │
93
+ │ BaseServerExtension, driverClass │
94
+ │ ) │
95
+ └──────────────┬──────────────────────┘
96
+ │
97
+ ┌────────────────────┼────────────────────┐
98
+ │ │ │
99
+ ┌─────────▼──────────┐ ┌──────▼──────────┐ ┌───────▼─────────┐
100
+ │ SlackMessaging │ │ TeamsMessaging │ │ YourCustom │
101
+ │ Extension │ │ Extension │ │ Extension │
102
+ │ POST /webhook/slack │ │ POST /webhook/ │ │ │
103
+ │ POST .../interact │ │ teams │ │ │
104
+ │ POST .../slash │ │ │ │ │
105
+ └─────────┬──────────┘ └──────┬──────────┘ └─────────────────┘
106
+ │ │
107
+ ┌─────────▼──────────┐ ┌──────▼──────────┐
108
+ │ SlackAdapter │ │ TeamsAdapter │
109
+ │ extends │ │ extends │
110
+ │ BaseMessaging- │ │ BaseMessaging- │
111
+ │ Adapter │ │ Adapter │
112
+ └─────────┬──────────┘ └──────┬──────────┘
113
+ │ │
114
+ └────────┬───────────┘
115
+ │
116
+ ┌────────▼────────┐
117
+ │ BaseMessaging │
118
+ │ Adapter │
119
+ │ (orchestration) │
120
+ │ │
121
+ │ 1. Resolve user │
122
+ │ 2. Resolve agent│
123
+ │ 3. Fetch thread │
124
+ │ 4. Run agent │
125
+ │ 5. Delegation │
126
+ │ 6. Format resp │
127
+ │ 7. Send/update │
128
+ └─────────────────┘
129
+ ```
130
+
131
+ ## Settings Reference
132
+
133
+ | Setting | Type | Default | Description |
134
+ |---------|------|---------|-------------|
135
+ | `DefaultAgentName` | `string` | (required) | Default MJ AI Agent name |
136
+ | `ContextUserEmail` | `string` | (required) | Fallback service account email |
137
+ | `BotToken` | `string` | (required for Slack) | Slack Bot OAuth Token (`xoxb-...`) |
138
+ | `SigningSecret` | `string` | — | Slack signing secret for webhook verification |
139
+ | `AppToken` | `string` | — | Slack App-Level Token for Socket Mode (`xapp-...`) |
140
+ | `ConnectionMode` | `'http' \| 'socket'` | `'http'` | Slack connection mode |
141
+ | `MaxThreadMessages` | `number` | `50` | Max thread messages for conversation context |
142
+ | `ShowTypingIndicator` | `boolean` | `true` | Show typing indicator while processing |
143
+ | `StreamingUpdateIntervalMs` | `number` | `1000` | Min interval between streaming updates (ms) |
144
+ | `ExplorerBaseURL` | `string` | — | MJ Explorer URL for "View in Explorer" buttons |
145
+ | `SlashCommands` | `Record<string, string>` | — | Slash command → agent name mapping (Slack only) |
146
+ | `MicrosoftAppId` | `string` | — | Azure Bot Service App ID (Teams only) |
147
+ | `MicrosoftAppPassword` | `string` | — | Azure Bot Service App Password (Teams only) |
148
+ | `MicrosoftAppTenantId` | `string` | — | Azure AD Tenant ID for Single Tenant (Teams only) |
149
+ | `MicrosoftAppType` | `string` | auto | `'SingleTenant'`, `'MultiTenant'`, or `'UserAssignedMsi'` |
150
+
151
+ ## Exported Utilities
152
+
153
+ The package exports shared formatting utilities useful for building custom adapters:
154
+
155
+ ```typescript
156
+ import {
157
+ // Base utilities
158
+ BaseMessagingAdapter,
159
+ splitMarkdownIntoSections,
160
+ convertToSlackMrkdwn,
161
+ truncateText,
162
+ splitTextIntoChunks,
163
+
164
+ // Slack
165
+ SlackAdapter,
166
+ SlackMessagingExtension,
167
+ markdownToBlocks,
168
+ verifySlackSignature,
169
+ handleSlackInteraction,
170
+ buildRichResponse,
171
+ buildAgentContextBlock,
172
+ buildFormModal,
173
+
174
+ // Teams
175
+ TeamsAdapter,
176
+ TeamsMessagingExtension,
177
+ markdownToAdaptiveCard,
178
+ buildRichAdaptiveCard,
179
+
180
+ // Tree-shaking prevention
181
+ LoadMessagingAdapters,
182
+ } from '@memberjunction/messaging-adapters';
183
+ ```
184
+
185
+ ## Building a Custom Adapter
186
+
187
+ To add support for a new messaging platform (Discord, Google Chat, etc.):
188
+
189
+ 1. Create a class extending `BaseMessagingAdapter` and implement all abstract methods
190
+ 2. Create a `BaseServerExtension` subclass with `@RegisterClass` that sets up routes
191
+ 3. Add a formatter that converts Markdown to your platform's rich format
192
+ 4. Optionally add a block/card builder for rich layouts
193
+
194
+ See `SlackAdapter.ts` and `SlackMessagingExtension.ts` for a complete reference implementation.
195
+
196
+ ## Testing
197
+
198
+ ```bash
199
+ npm run test # Run all 301 tests
200
+ npm run test:watch # Watch mode
201
+ npm run test:coverage # With coverage report
202
+ ```
203
+
204
+ Test suites cover every module:
205
+
206
+ | Test File | Tests | Coverage |
207
+ |-----------|-------|----------|
208
+ | `BaseMessagingAdapter.test.ts` | 40 | Core orchestration, agent resolution, thread affinity, delegation |
209
+ | `message-formatter.test.ts` | 28 | Markdown parsing, text splitting, Slack mrkdwn conversion |
210
+ | `slack-formatter.test.ts` | 10 | Markdown → Block Kit conversion |
211
+ | `slack-block-builder.test.ts` | 57 | Rich layouts, truncation, payload limits, "View Full" |
212
+ | `slack-routes.test.ts` | 10 | HMAC-SHA256 verification, replay attack prevention |
213
+ | `slack-interactivity.test.ts` | 19 | Modal forms, button routing, form submissions |
214
+ | `SlackAdapter.test.ts` | 12 | Event mapping, mention parsing, typing indicators |
215
+ | `SlackMessagingExtension.test.ts` | 26 | HTTP/Socket mode, signature verification, slash commands |
216
+ | `TeamsAdapter.test.ts` | 31 | Activity mapping, form extraction, streaming |
217
+ | `TeamsMessagingExtension.test.ts` | 12 | Bot Framework routing, JWT validation |
218
+ | `teams-formatter.test.ts` | 7 | Markdown → Adaptive Card TextBlock |
219
+ | `teams-card-builder.test.ts` | 52 | Rich card composition, actions, forms, payload limits |
220
+
221
+ ## Known Limitations
222
+
223
+ - **Teams thread history**: Requires Microsoft Graph API with `ChannelMessage.Read.All` permission, not yet implemented. Teams conversations are currently single-turn.
224
+ - **Full response store**: The "View Full" button in Slack stores content in memory with a 30-minute TTL. Content is lost on server restart.
225
+ - **Conversation references**: Teams conversation references for proactive messaging are stored in memory (lost on restart).
226
+ - **Max delegation hops**: Agent delegation chains limited to 3 hops.
227
+
228
+ ## Related Packages
229
+
230
+ - [`@memberjunction/server-extensions-core`](../ServerExtensionsCore/) — The extension framework this package builds on
231
+ - [`@memberjunction/server`](../MJServer/) — MJServer that loads and manages extensions
232
+ - [`@memberjunction/ai-agents`](../AI/Agents/) — Agent execution engine (`AgentRunner`)
233
+ - [`@memberjunction/ai-core-plus`](../AI/CorePlus/) — Agent types and execution result structures