fix(feishu): bound sender name cache (#103513)

This commit is contained in:
mikasa
2026-07-18 00:11:51 +01:00
committed by GitHub
parent 0f192e79d4
commit d03b015ff4
2 changed files with 36 additions and 0 deletions
@@ -65,4 +65,37 @@ describe("resolveFeishuSenderName", () => {
expect(get).toHaveBeenCalledTimes(2);
});
it("evicts the oldest sender while retaining recent sender names", async () => {
const get = vi.fn(async (params: { path: { user_id: string } }) => ({
data: { user: { name: `name-${params.path.user_id}` } },
}));
createFeishuClientMock.mockReturnValue({ contact: { user: { get } } });
for (let index = 0; index < 501; index += 1) {
await resolveFeishuSenderName({
account,
senderId: `ou_sender_cap_${index}`,
log: vi.fn(),
});
}
await resolveFeishuSenderName({
account,
senderId: "ou_sender_cap_0",
log: vi.fn(),
});
await resolveFeishuSenderName({
account,
senderId: "ou_sender_cap_500",
log: vi.fn(),
});
expect(get).toHaveBeenCalledTimes(502);
expect(
get.mock.calls.filter(([params]) => params.path.user_id === "ou_sender_cap_0"),
).toHaveLength(2);
expect(
get.mock.calls.filter(([params]) => params.path.user_id === "ou_sender_cap_500"),
).toHaveLength(1);
});
});
+3
View File
@@ -1,4 +1,5 @@
// Feishu plugin module implements bot sender name behavior.
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
import {
asDateTimestampMs,
resolveExpiresAtMsFromDurationMs,
@@ -29,6 +30,7 @@ const FEISHU_SCOPE_CORRECTIONS: Record<string, string> = {
"contact:contact.base:readonly": "contact:user.base:readonly",
};
const SENDER_NAME_TTL_MS = 10 * 60 * 1000;
const SENDER_NAME_CACHE_MAX_SIZE = 500;
const senderNameCache = new Map<string, { name: string; expireAt: number }>();
function correctFeishuScopeInUrl(url: string): string {
@@ -117,6 +119,7 @@ export async function resolveFeishuSenderName(params: {
const expireAt = resolveExpiresAtMsFromDurationMs(SENDER_NAME_TTL_MS);
if (expireAt !== undefined) {
senderNameCache.set(normalizedSenderId, { name, expireAt });
pruneMapToMaxSize(senderNameCache, SENDER_NAME_CACHE_MAX_SIZE);
}
return { name };
}