openclaw/extensions/discord/src/monitor/thread-session-close.ts
scoootscooob 5682ec37fa
refactor: move Discord channel implementation to extensions/ (#45660)
* refactor: move Discord channel implementation to extensions/discord/src/

Move all Discord source files from src/discord/ to extensions/discord/src/,
following the extension migration pattern. Source files in src/discord/ are
replaced with re-export shims. Channel-plugin files from
src/channels/plugins/*/discord* are similarly moved and shimmed.

- Copy all .ts source files preserving subdirectory structure (monitor/, voice/)
- Move channel-plugin files (actions, normalize, onboarding, outbound, status-issues)
- Fix all relative imports to use correct paths from new location
- Create re-export shims at original locations for backward compatibility
- Delete test files from shim locations (tests live in extension now)
- Update tsconfig.plugin-sdk.dts.json rootDir from "src" to "." to accommodate
  extension files outside src/
- Update write-plugin-sdk-entry-dts.ts to match new declaration output paths

* fix: add importOriginal to thread-bindings session-meta mock for extensions test

* style: fix formatting in thread-bindings lifecycle test
2026-03-14 02:53:57 -07:00

60 lines
2.1 KiB
TypeScript

import type { OpenClawConfig } from "../../../../src/config/config.js";
import { resolveStorePath, updateSessionStore } from "../../../../src/config/sessions.js";
/**
* Marks every session entry in the store whose key contains {@link threadId}
* as "reset" by setting `updatedAt` to 0.
*
* This mirrors how the daily / idle session reset works: zeroing `updatedAt`
* makes `evaluateSessionFreshness` treat the session as stale on the next
* inbound message, so the bot starts a fresh conversation without deleting
* any on-disk transcript history.
*/
export async function closeDiscordThreadSessions(params: {
cfg: OpenClawConfig;
accountId: string;
threadId: string;
}): Promise<number> {
const { cfg, accountId, threadId } = params;
const normalizedThreadId = threadId.trim().toLowerCase();
if (!normalizedThreadId) {
return 0;
}
// Match when the threadId appears as a complete colon-separated segment.
// e.g. "999" must be followed by ":" (middle) or end-of-string (final).
// Using a regex avoids false-positives where one snowflake is a prefix of
// another (e.g. searching for "999" must not match ":99900").
//
// Session key shapes:
// agent:<agentId>:discord:channel:<threadId>
// agent:<agentId>:discord:channel:<parentId>:thread:<threadId>
const segmentRe = new RegExp(`:${normalizedThreadId}(?::|$)`, "i");
function sessionKeyContainsThreadId(key: string): boolean {
return segmentRe.test(key);
}
// Resolve the store file. We pass `accountId` as `agentId` here to mirror
// how other Discord subsystems resolve their per-account sessions stores.
const storePath = resolveStorePath(cfg.session?.store, { agentId: accountId });
let resetCount = 0;
await updateSessionStore(storePath, (store) => {
for (const [key, entry] of Object.entries(store)) {
if (!entry || !sessionKeyContainsThreadId(key)) {
continue;
}
// Setting updatedAt to 0 signals that this session is stale.
// evaluateSessionFreshness will create a new session on the next message.
entry.updatedAt = 0;
resetCount += 1;
}
return resetCount;
});
return resetCount;
}