|
1 | 1 | import type { WebClient as SlackWebClient } from "@slack/web-api"; |
| 2 | +import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime"; |
2 | 3 | import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; |
3 | 4 | import { normalizeHostname } from "openclaw/plugin-sdk/host-runtime"; |
4 | 5 | import { fetchWithRuntimeDispatcher } from "openclaw/plugin-sdk/infra-runtime"; |
@@ -385,18 +386,11 @@ function evictThreadStarterCache(): void { |
385 | 386 | THREAD_STARTER_CACHE.delete(cacheKey); |
386 | 387 | } |
387 | 388 | } |
388 | | - if (THREAD_STARTER_CACHE.size <= THREAD_STARTER_CACHE_MAX) { |
389 | | - return; |
390 | | - } |
391 | | - const excess = THREAD_STARTER_CACHE.size - THREAD_STARTER_CACHE_MAX; |
392 | | - let removed = 0; |
393 | | - for (const cacheKey of THREAD_STARTER_CACHE.keys()) { |
394 | | - THREAD_STARTER_CACHE.delete(cacheKey); |
395 | | - removed += 1; |
396 | | - if (removed >= excess) { |
397 | | - break; |
398 | | - } |
399 | | - } |
| 389 | + pruneMapToMaxSize(THREAD_STARTER_CACHE, THREAD_STARTER_CACHE_MAX); |
| 390 | +} |
| 391 | + |
| 392 | +function formatSlackFilePlaceholder(files: SlackFile[] | undefined): string { |
| 393 | + return `[attached: ${files?.map((file) => file.name ?? "file").join(", ") ?? "file"}]`; |
400 | 394 | } |
401 | 395 |
|
402 | 396 | export async function resolveSlackThreadStarter(params: { |
@@ -430,15 +424,16 @@ export async function resolveSlackThreadStarter(params: { |
430 | 424 | }; |
431 | 425 | const message = response?.messages?.[0]; |
432 | 426 | const text = (message?.text ?? "").trim(); |
433 | | - if (!message || !text) { |
| 427 | + const files = message?.files?.length ? message.files : undefined; |
| 428 | + if (!message || (!text && !files)) { |
434 | 429 | return null; |
435 | 430 | } |
436 | 431 | const starter: SlackThreadStarter = { |
437 | | - text, |
| 432 | + text: text || formatSlackFilePlaceholder(files), |
438 | 433 | userId: message.user, |
439 | 434 | botId: message.bot_id, |
440 | 435 | ts: message.ts, |
441 | | - files: message.files, |
| 436 | + files, |
442 | 437 | }; |
443 | 438 | if (THREAD_STARTER_CACHE.has(cacheKey)) { |
444 | 439 | THREAD_STARTER_CACHE.delete(cacheKey); |
@@ -536,9 +531,7 @@ export async function resolveSlackThreadHistory(params: { |
536 | 531 |
|
537 | 532 | return retained.map((msg) => ({ |
538 | 533 | // For file-only messages, create a placeholder showing attached filenames |
539 | | - text: msg.text?.trim() |
540 | | - ? msg.text |
541 | | - : `[attached: ${msg.files?.map((f) => f.name ?? "file").join(", ")}]`, |
| 534 | + text: msg.text?.trim() ? msg.text : formatSlackFilePlaceholder(msg.files), |
542 | 535 | userId: msg.user, |
543 | 536 | botId: msg.bot_id, |
544 | 537 | ts: msg.ts, |
|
0 commit comments