Skip to content

Commit 4c841ac

Browse files
authored
refactor: remove Telegram session deps adapter (#96524)
* refactor: remove telegram session deps adapter * test: update telegram session ratchet expectation
1 parent 8ecbf83 commit 4c841ac

4 files changed

Lines changed: 40 additions & 39 deletions

File tree

extensions/telegram/src/bot.create-telegram-bot.test-harness.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type AnyMock = ReturnType<typeof vi.fn>;
1212
type AnyAsyncMock = ReturnType<typeof vi.fn<(...args: unknown[]) => Promise<unknown>>>;
1313
type GetRuntimeConfigFn =
1414
typeof import("openclaw/plugin-sdk/runtime-config-snapshot").getRuntimeConfig;
15+
type GetSessionEntryFn = typeof import("openclaw/plugin-sdk/session-store-runtime").getSessionEntry;
16+
type ListSessionEntriesFn =
17+
typeof import("openclaw/plugin-sdk/session-store-runtime").listSessionEntries;
1518
type LoadSessionStoreFn =
1619
typeof import("openclaw/plugin-sdk/session-store-runtime").loadSessionStore;
1720
type ResolveStorePathFn =
@@ -61,28 +64,43 @@ vi.mock("openclaw/plugin-sdk/web-media", () => ({
6164
}));
6265

6366
const {
67+
getSessionEntryMock,
6468
getRuntimeConfig,
69+
listSessionEntriesMock,
6570
loadSessionStoreMock,
6671
readSessionUpdatedAtMock,
6772
recordInboundSessionMock,
6873
resolveStorePathMock,
6974
sessionStoreEntries,
7075
} = vi.hoisted(
7176
(): {
77+
getSessionEntryMock: MockFn<GetSessionEntryFn>;
7278
getRuntimeConfig: MockFn<GetRuntimeConfigFn>;
79+
listSessionEntriesMock: MockFn<ListSessionEntriesFn>;
7380
loadSessionStoreMock: MockFn<LoadSessionStoreFn>;
7481
readSessionUpdatedAtMock: MockFn<ReadSessionUpdatedAtFn>;
7582
recordInboundSessionMock: MockFn<NonNullable<TelegramBotDeps["recordInboundSession"]>>;
7683
resolveStorePathMock: MockFn<ResolveStorePathFn>;
7784
sessionStoreEntries: { value: SessionStore };
7885
} => ({
7986
getRuntimeConfig: vi.fn<GetRuntimeConfigFn>(() => ({})),
80-
loadSessionStoreMock: vi.fn<LoadSessionStoreFn>(
81-
(_storePath, _opts) => sessionStoreEntries.value,
82-
),
8387
resolveStorePathMock: vi.fn<ResolveStorePathFn>(
8488
(storePath?: string) => storePath ?? sessionStorePath,
8589
),
90+
loadSessionStoreMock: vi.fn<LoadSessionStoreFn>(
91+
(_storePath, _opts) => sessionStoreEntries.value,
92+
),
93+
getSessionEntryMock: vi.fn<GetSessionEntryFn>(({ storePath, sessionKey, agentId }) => {
94+
const resolvedStorePath = storePath ?? resolveStorePathMock(undefined, { agentId });
95+
return loadSessionStoreMock(resolvedStorePath)[sessionKey];
96+
}),
97+
listSessionEntriesMock: vi.fn<ListSessionEntriesFn>(({ storePath, agentId } = {}) => {
98+
const resolvedStorePath = storePath ?? resolveStorePathMock(undefined, { agentId });
99+
return Object.entries(loadSessionStoreMock(resolvedStorePath)).map(([sessionKey, entry]) => ({
100+
sessionKey,
101+
entry,
102+
}));
103+
}),
86104
readSessionUpdatedAtMock: vi.fn<ReadSessionUpdatedAtFn>(() => undefined),
87105
recordInboundSessionMock: vi.fn(async () => undefined),
88106
sessionStoreEntries: { value: {} as SessionStore },
@@ -444,6 +462,8 @@ export const telegramBotRuntimeForTest: TelegramBotRuntimeForTest = {
444462
};
445463
export const telegramBotDepsForTest: TelegramBotDeps = {
446464
getRuntimeConfig,
465+
getSessionEntry: getSessionEntryMock,
466+
listSessionEntries: listSessionEntriesMock,
447467
loadSessionStore: loadSessionStoreMock as TelegramBotDeps["loadSessionStore"],
448468
resolveStorePath: resolveStorePathMock,
449469
readSessionUpdatedAt: readSessionUpdatedAtMock,
@@ -564,6 +584,19 @@ beforeEach(() => {
564584
loadSessionStoreMock.mockImplementation(() => sessionStoreEntries.value);
565585
resolveStorePathMock.mockReset();
566586
resolveStorePathMock.mockImplementation((storePath?: string) => storePath ?? sessionStorePath);
587+
getSessionEntryMock.mockReset();
588+
getSessionEntryMock.mockImplementation(({ storePath, sessionKey, agentId }) => {
589+
const resolvedStorePath = storePath ?? resolveStorePathMock(undefined, { agentId });
590+
return loadSessionStoreMock(resolvedStorePath)[sessionKey];
591+
});
592+
listSessionEntriesMock.mockReset();
593+
listSessionEntriesMock.mockImplementation(({ storePath, agentId } = {}) => {
594+
const resolvedStorePath = storePath ?? resolveStorePathMock(undefined, { agentId });
595+
return Object.entries(loadSessionStoreMock(resolvedStorePath)).map(([sessionKey, entry]) => ({
596+
sessionKey,
597+
entry,
598+
}));
599+
});
567600
readSessionUpdatedAtMock.mockReset();
568601
readSessionUpdatedAtMock.mockReturnValue(undefined);
569602
recordInboundSessionMock.mockReset();

extensions/telegram/src/bot.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Telegram plugin module implements bot behavior.
2-
import { getSessionEntry, listSessionEntries } from "openclaw/plugin-sdk/session-store-runtime";
32
import {
43
createTelegramBotCore,
54
getTelegramSequentialKey,
65
setTelegramBotRuntimeForTest,
76
} from "./bot-core.js";
8-
import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js";
7+
import { defaultTelegramBotDeps } from "./bot-deps.js";
98
import type { TelegramBotOptions } from "./bot.types.js";
109

1110
export type { TelegramBotOptions } from "./bot.types.js";
@@ -17,39 +16,6 @@ export function createTelegramBot(
1716
): ReturnType<typeof createTelegramBotCore> {
1817
return createTelegramBotCore({
1918
...opts,
20-
telegramDeps: withTelegramSessionAccessorDeps(opts.telegramDeps ?? defaultTelegramBotDeps),
19+
telegramDeps: opts.telegramDeps ?? defaultTelegramBotDeps,
2120
});
2221
}
23-
24-
function withTelegramSessionAccessorDeps(deps: TelegramBotDeps): TelegramBotDeps {
25-
if (!deps.loadSessionStore) {
26-
return {
27-
...deps,
28-
getSessionEntry: deps.getSessionEntry ?? getSessionEntry,
29-
listSessionEntries: deps.listSessionEntries ?? listSessionEntries,
30-
};
31-
}
32-
33-
const listInjectedEntries = (
34-
scope: Parameters<NonNullable<TelegramBotDeps["listSessionEntries"]>>[0] = {},
35-
) => {
36-
const storePath =
37-
scope.storePath ?? deps.resolveStorePath(undefined, { agentId: scope.agentId });
38-
return Object.entries(deps.loadSessionStore?.(storePath) ?? {}).map(([sessionKey, entry]) => ({
39-
sessionKey,
40-
entry,
41-
}));
42-
};
43-
44-
return {
45-
...deps,
46-
// Existing Telegram tests and custom deps inject loadSessionStore; expose
47-
// the same data through the accessor seam consumed by migrated handlers.
48-
getSessionEntry:
49-
deps.getSessionEntry ??
50-
((scope) =>
51-
listInjectedEntries(scope).find(({ sessionKey }) => sessionKey === scope.sessionKey)
52-
?.entry),
53-
listSessionEntries: deps.listSessionEntries ?? listInjectedEntries,
54-
};
55-
}

scripts/check-session-accessor-boundary.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export const migratedBundledPluginSessionAccessorFiles = new Set([
132132
"extensions/memory-core/src/dreaming-narrative.ts",
133133
"extensions/mattermost/src/mattermost/model-picker.ts",
134134
"extensions/telegram/src/bot-handlers.runtime.ts",
135+
"extensions/telegram/src/bot.ts",
135136
]);
136137

137138
export const migratedSessionAccessorWriteFiles = new Set([

test/scripts/check-session-accessor-boundary.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe("session accessor boundary guard", () => {
8686
"extensions/memory-core/src/dreaming-narrative.ts",
8787
"extensions/mattermost/src/mattermost/model-picker.ts",
8888
"extensions/telegram/src/bot-handlers.runtime.ts",
89+
"extensions/telegram/src/bot.ts",
8990
]),
9091
);
9192
});

0 commit comments

Comments
 (0)