@ascegu/teamily 1.0.16 → 1.0.18

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/channel.ts +0 -433
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ascegu/teamily",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "OpenClaw Teamily channel plugin - Team instant messaging server integration",
5
5
  "keywords": [
6
6
  "channel",
package/src/channel.ts DELETED
@@ -1,433 +0,0 @@
1
- import {
2
- applyAccountNameToChannelSection,
3
- buildChannelConfigSchema,
4
- buildPendingHistoryContextFromMap,
5
- clearHistoryEntriesIfEnabled,
6
- DEFAULT_ACCOUNT_ID,
7
- DEFAULT_GROUP_HISTORY_LIMIT,
8
- deleteAccountFromConfigSection,
9
- normalizeAccountId,
10
- PAIRING_APPROVED_MESSAGE,
11
- recordPendingHistoryEntryIfEnabled,
12
- setAccountEnabledInConfigSection,
13
- type ChannelPlugin,
14
- type ChannelOutboundContext,
15
- type ChannelStatusIssue,
16
- type HistoryEntry,
17
- } from "openclaw/plugin-sdk";
18
- import {
19
- buildAccountScopedDmSecurityPolicy,
20
- createScopedAccountConfigAccessors,
21
- } from "openclaw/plugin-sdk/compat";
22
- import {
23
- listTeamilyAccountIds,
24
- resolveDefaultTeamilyAccountId,
25
- resolveTeamilyAccount,
26
- } from "./accounts.js";
27
- import { TeamilyConfigSchema } from "./config-schema.js";
28
- import type { CoreConfig } from "./config-schema.js";
29
- import { getTeamilyMonitor, startTeamilyMonitoring, stopTeamilyMonitoring } from "./monitor.js";
30
- import { normalizeTeamilyTarget, normalizeTeamilyAllowEntry } from "./normalize.js";
31
- import { probeTeamily } from "./probe.js";
32
- import { getTeamilyRuntime } from "./runtime.js";
33
- import type { ResolvedTeamilyAccount } from "./types.js";
34
- import { isGroupSession } from "./types.js";
35
-
36
- const meta = {
37
- id: "teamily",
38
- label: "Teamily",
39
- selectionLabel: "Teamily (self-hosted)",
40
- docsPath: "/channels/teamily",
41
- docsLabel: "teamily",
42
- blurb: "Team instant messaging server",
43
- order: 75,
44
- quickstartAllowFrom: true,
45
- };
46
-
47
- const teamilyConfigAccessors = createScopedAccountConfigAccessors({
48
- resolveAccount: ({ cfg, accountId }) => resolveTeamilyAccount(cfg as CoreConfig, accountId),
49
- resolveAllowFrom: (account) => account.dm?.allowFrom,
50
- formatAllowFrom: (allowFrom) => allowFrom.map((id) => String(id)),
51
- });
52
-
53
- export const teamilyPlugin: ChannelPlugin<ResolvedTeamilyAccount> = {
54
- id: "teamily",
55
- meta,
56
- capabilities: {
57
- chatTypes: ["direct", "group"],
58
- media: true,
59
- reactions: false,
60
- threads: false,
61
- polls: false,
62
- },
63
- reload: { configPrefixes: ["channels.teamily"] },
64
- setup: {
65
- resolveAccountId: ({ accountId, input }) => {
66
- if (accountId) return accountId;
67
- if (input?.name) return normalizeAccountId(String(input.name));
68
- return DEFAULT_ACCOUNT_ID;
69
- },
70
- applyAccountName: ({ cfg, accountId, name }) =>
71
- applyAccountNameToChannelSection({
72
- cfg,
73
- channelKey: "teamily",
74
- accountId,
75
- name,
76
- }),
77
- applyAccountConfig: ({ cfg, accountId, input }) =>
78
- applyTeamilyAccountConfig({
79
- cfg: cfg as CoreConfig,
80
- accountId,
81
- input: input as Record<string, unknown>,
82
- }),
83
- },
84
- pairing: {
85
- idLabel: "teamilyUserId",
86
- normalizeAllowEntry: (entry) => {
87
- try {
88
- return normalizeTeamilyAllowEntry(entry);
89
- } catch {
90
- return entry;
91
- }
92
- },
93
- notifyApproval: async ({ id, cfg }) => {
94
- try {
95
- const accountId = resolveDefaultTeamilyAccountId(cfg as CoreConfig);
96
- const target = normalizeTeamilyTarget(id);
97
- const monitor = getTeamilyMonitor(accountId);
98
- if (monitor) {
99
- await monitor.sendText(target, PAIRING_APPROVED_MESSAGE);
100
- }
101
- // If monitor isn't running, skip silently — pairing was still approved
102
- } catch {
103
- // Silently fail on notification
104
- }
105
- },
106
- },
107
- configSchema: buildChannelConfigSchema(TeamilyConfigSchema),
108
- config: {
109
- listAccountIds: (cfg) => listTeamilyAccountIds(cfg as CoreConfig),
110
- resolveAccount: (cfg, accountId) => resolveTeamilyAccount(cfg as CoreConfig, accountId),
111
- defaultAccountId: (cfg) => resolveDefaultTeamilyAccountId(cfg as CoreConfig),
112
- setAccountEnabled: ({ cfg, accountId, enabled }) =>
113
- setAccountEnabledInConfigSection({
114
- cfg: cfg as CoreConfig,
115
- sectionKey: "teamily",
116
- accountId,
117
- enabled,
118
- allowTopLevel: true,
119
- }),
120
- deleteAccount: ({ cfg, accountId }) =>
121
- deleteAccountFromConfigSection({
122
- cfg: cfg as CoreConfig,
123
- sectionKey: "teamily",
124
- accountId,
125
- clearBaseFields: ["name", "userID", "token", "nickname", "faceURL"],
126
- }),
127
- isConfigured: (account) => !!account.token,
128
- describeAccount: (account) => ({
129
- accountId: account.accountId,
130
- name: account.nickname || account.userID,
131
- enabled: account.enabled,
132
- configured: !!account.token,
133
- }),
134
- ...teamilyConfigAccessors,
135
- },
136
- security: {
137
- resolveDmPolicy: ({ cfg, accountId, account }) => {
138
- return buildAccountScopedDmSecurityPolicy({
139
- cfg: cfg as CoreConfig,
140
- channelKey: "teamily",
141
- accountId,
142
- fallbackAccountId: account.accountId ?? DEFAULT_ACCOUNT_ID,
143
- policy: account.dm?.policy,
144
- allowFrom: account.dm?.allowFrom ?? [],
145
- allowFromPathSuffix: "dm.",
146
- normalizeEntry: (raw) => normalizeTeamilyAllowEntry(raw),
147
- });
148
- },
149
- },
150
- outbound: {
151
- deliveryMode: "gateway",
152
- resolveTarget: ({ to }) => {
153
- if (!to?.trim()) {
154
- return { ok: false, error: new Error("Teamily requires --to <userId|group:groupId>") };
155
- }
156
- try {
157
- const target = normalizeTeamilyTarget(to);
158
- // Preserve the full target format so sendText/sendMedia can distinguish user vs group
159
- const resolved = target.type === "group" ? `group:${target.id}` : target.id;
160
- return { ok: true, to: resolved };
161
- } catch (err) {
162
- return { ok: false, error: err instanceof Error ? err : new Error(String(err)) };
163
- }
164
- },
165
- sendText: async (ctx: ChannelOutboundContext) => {
166
- const { to, text, accountId } = ctx;
167
- const monitor = requireMonitor(accountId);
168
- const target = normalizeTeamilyTarget(to);
169
- const messageId = await monitor.sendText(target, text);
170
- return { channel: "teamily" as const, messageId };
171
- },
172
- sendMedia: async (ctx: ChannelOutboundContext) => {
173
- const { to, accountId } = ctx;
174
- const mediaUrl = ctx.mediaUrl;
175
- if (!mediaUrl) {
176
- throw new Error("Media URL is required");
177
- }
178
- const monitor = requireMonitor(accountId);
179
- const target = normalizeTeamilyTarget(to);
180
-
181
- let messageId: string;
182
- const urlLower = mediaUrl.toLowerCase();
183
- if (urlLower.endsWith(".mp4") || urlLower.endsWith(".mov") || urlLower.endsWith(".webm")) {
184
- messageId = await monitor.sendVideo(target, mediaUrl);
185
- } else if (urlLower.endsWith(".mp3") || urlLower.endsWith(".m4a") || urlLower.endsWith(".wav")) {
186
- messageId = await monitor.sendAudio(target, mediaUrl);
187
- } else if (urlLower.endsWith(".pdf") || urlLower.endsWith(".doc") || urlLower.endsWith(".docx") || urlLower.endsWith(".zip")) {
188
- messageId = await monitor.sendFile(target, mediaUrl);
189
- } else {
190
- messageId = await monitor.sendImage(target, mediaUrl);
191
- }
192
-
193
- return { channel: "teamily" as const, messageId };
194
- },
195
- },
196
- status: {
197
- probeAccount: async ({ account }) => {
198
- const result = await probeTeamily(account);
199
- if (!result.connected) {
200
- return {
201
- ok: false,
202
- error: result.error || "Failed to connect to Teamily server",
203
- };
204
- }
205
- return { ok: true };
206
- },
207
- buildAccountSnapshot: ({ account, runtime, probe }) => ({
208
- accountId: account.accountId,
209
- name: account.nickname || account.userID,
210
- enabled: account.enabled,
211
- configured: !!account.token,
212
- running: runtime?.running ?? false,
213
- lastStartAt: runtime?.lastStartAt ?? null,
214
- lastStopAt: runtime?.lastStopAt ?? null,
215
- lastError: runtime?.lastError ?? null,
216
- probe,
217
- }),
218
- collectStatusIssues: (accounts) => {
219
- const issues: ChannelStatusIssue[] = [];
220
- for (const snap of accounts) {
221
- if (snap.lastError) {
222
- issues.push({
223
- channel: "teamily",
224
- accountId: snap.accountId,
225
- kind: "runtime",
226
- message: snap.lastError,
227
- fix: "Check that the Teamily server is running and accessible",
228
- });
229
- }
230
- }
231
- return issues;
232
- },
233
- },
234
- gateway: {
235
- startAccount: async (ctx) => {
236
- const { accountId, account, log } = ctx;
237
-
238
- if (!account.token) {
239
- log?.warn?.(`Teamily account ${accountId} not configured (missing token)`);
240
- return;
241
- }
242
-
243
- log?.info?.(`Starting Teamily channel (account: ${accountId})`);
244
-
245
- const MEDIA_MAX_BYTES = 5 * 1024 * 1024; // 5 MB
246
- const historyLimit = DEFAULT_GROUP_HISTORY_LIMIT; // 50 messages per group
247
- const groupHistories = new Map<string, HistoryEntry[]>();
248
-
249
- const stopFn = startTeamilyMonitoring(account, async (message) => {
250
- const rt = getTeamilyRuntime();
251
- const currentCfg = rt.config.loadConfig();
252
-
253
- const isGroup = isGroupSession(message.sessionType);
254
- const from = message.sendID;
255
- const rawText = message.content?.text || "";
256
-
257
- log?.info?.(
258
- `[${accountId}] Incoming message: sessionType=${message.sessionType}, isGroup=${isGroup}, ` +
259
- `from=${from}, recvID=${message.recvID}, isAtSelf=${message.isAtSelf ?? false}`,
260
- );
261
-
262
- const historyKey = isGroup ? `teamily:group:${message.recvID}` : undefined;
263
-
264
- // In group chats, buffer non-@-mention messages for context and skip dispatch.
265
- // Only @-mention messages are dispatched to the agent, with the buffer injected.
266
- if (isGroup && !message.isAtSelf) {
267
- if (historyKey && rawText) {
268
- recordPendingHistoryEntryIfEnabled({
269
- historyMap: groupHistories,
270
- historyKey,
271
- limit: historyLimit,
272
- entry: {
273
- sender: from,
274
- body: rawText,
275
- timestamp: message.sendTime,
276
- },
277
- });
278
- }
279
- return;
280
- }
281
-
282
- const sessionKey = isGroup ? `teamily:group:${message.recvID}` : `teamily:${from}`;
283
-
284
- let mediaUrl: string | undefined;
285
- if (message.content?.picture?.sourcePicture?.url) {
286
- mediaUrl = message.content.picture.sourcePicture.url;
287
- } else if (message.content?.video?.videoUrl) {
288
- mediaUrl = message.content.video.videoUrl;
289
- } else if (message.content?.audio?.sourceUrl) {
290
- mediaUrl = message.content.audio.sourceUrl;
291
- }
292
-
293
- // Download remote media to a local temp file so the agent recognises
294
- // image-only messages (hasMediaAttachment checks MediaPath, not MediaUrl).
295
- let mediaPath: string | undefined;
296
- let mediaType: string | undefined;
297
- if (mediaUrl) {
298
- try {
299
- const fetched = await rt.channel.media.fetchRemoteMedia({ url: mediaUrl, maxBytes: MEDIA_MAX_BYTES });
300
- const saved = await rt.channel.media.saveMediaBuffer(
301
- fetched.buffer,
302
- fetched.contentType,
303
- "inbound",
304
- MEDIA_MAX_BYTES,
305
- );
306
- mediaPath = saved.path;
307
- mediaType = saved.contentType;
308
- } catch (err) {
309
- log?.warn?.(`[${accountId}] Failed to download Teamily media: ${String(err)}`);
310
- }
311
- }
312
-
313
- // For group @-mention messages, prepend buffered history as context.
314
- const body =
315
- isGroup && historyKey
316
- ? buildPendingHistoryContextFromMap({
317
- historyMap: groupHistories,
318
- historyKey,
319
- limit: historyLimit,
320
- currentMessage: rawText,
321
- formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
322
- })
323
- : rawText;
324
-
325
- const replyTarget = isGroup ? `group:${message.recvID}` : from;
326
- const msgCtx = {
327
- Body: body,
328
- From: from,
329
- To: account.userID,
330
- SessionKey: sessionKey,
331
- AccountId: accountId,
332
- Provider: "teamily" as const,
333
- Surface: "teamily" as const,
334
- OriginatingChannel: "teamily" as const,
335
- OriginatingTo: replyTarget,
336
- ChatType: isGroup ? "group" : "direct",
337
- MediaUrl: mediaUrl,
338
- MediaPath: mediaPath,
339
- MediaType: mediaType,
340
- };
341
-
342
- await rt.channel.reply.dispatchReplyWithBufferedBlockDispatcher({
343
- ctx: msgCtx,
344
- cfg: currentCfg,
345
- dispatcherOptions: {
346
- deliver: async (payload: { text?: string; body?: string }) => {
347
- const replyText = payload?.text ?? payload?.body;
348
- if (replyText) {
349
- const monitor = getTeamilyMonitor(accountId);
350
- if (!monitor) throw new Error(`Teamily monitor not running for account ${accountId}`);
351
- log?.info?.(`[${accountId}] Sending reply to: ${replyTarget} (isGroup=${isGroup})`);
352
- const target = normalizeTeamilyTarget(replyTarget);
353
- await monitor.sendText(target, replyText);
354
- }
355
- },
356
- onReplyStart: () => {
357
- log?.info?.(`Agent reply started for ${from}`);
358
- },
359
- },
360
- });
361
-
362
- // Clear history buffer after dispatch so context doesn't repeat.
363
- if (isGroup && historyKey) {
364
- clearHistoryEntriesIfEnabled({
365
- historyMap: groupHistories,
366
- historyKey,
367
- limit: historyLimit,
368
- });
369
- }
370
- });
371
-
372
- // Respect abort signal from the gateway framework
373
- ctx.abortSignal.addEventListener("abort", () => {
374
- stopFn();
375
- stopTeamilyMonitoring(accountId);
376
- });
377
-
378
- // Block until aborted — monitor runs indefinitely
379
- await new Promise<void>((resolve) => {
380
- ctx.abortSignal.addEventListener("abort", () => resolve());
381
- });
382
- },
383
- },
384
- };
385
-
386
- function applyTeamilyAccountConfig(params: {
387
- cfg: CoreConfig;
388
- accountId: string;
389
- input: Record<string, unknown>;
390
- }): CoreConfig {
391
- const { cfg, accountId, input } = params;
392
- const existing = cfg.channels?.teamily;
393
- const server = existing?.server ?? { platformUrl: "", apiURL: "", wsURL: "" };
394
- const accounts = existing?.accounts ?? {};
395
-
396
- const accountUpdate: Record<string, unknown> = {};
397
- if (input.userID) accountUpdate.userID = String(input.userID);
398
- if (input.token) accountUpdate.token = String(input.token);
399
- if (input.nickname) accountUpdate.nickname = String(input.nickname);
400
- if (input.faceURL) accountUpdate.faceURL = String(input.faceURL);
401
-
402
- const serverUpdate: Record<string, string> = {};
403
- if (input.platformUrl) serverUpdate.platformUrl = String(input.platformUrl);
404
- if (input.apiURL) serverUpdate.apiURL = String(input.apiURL);
405
- if (input.wsURL) serverUpdate.wsURL = String(input.wsURL);
406
-
407
- return {
408
- ...cfg,
409
- channels: {
410
- ...cfg.channels,
411
- teamily: {
412
- enabled: true,
413
- server: { ...server, ...serverUpdate },
414
- accounts: {
415
- ...accounts,
416
- [accountId]: {
417
- ...accounts[accountId],
418
- ...accountUpdate,
419
- },
420
- },
421
- },
422
- },
423
- } as CoreConfig;
424
- }
425
-
426
- function requireMonitor(accountId?: string | null) {
427
- const id = accountId || "default";
428
- const monitor = getTeamilyMonitor(id);
429
- if (!monitor) {
430
- throw new Error(`Teamily gateway not running for account "${id}" — outbound requires an active gateway`);
431
- }
432
- return monitor;
433
- }