Skip to content

Commit 4c4152e

Browse files
fix(channels): honor supported group history limits (#129710)
Co-authored-by: ayaangazali <[email protected]>
1 parent 4a846f1 commit 4c4152e

15 files changed

Lines changed: 184 additions & 31 deletions

File tree

‎extensions/clickclack/src/config-schema.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ const ClickClackAccountConfigSchema = z
6161
})
6262
.strict();
6363

64-
const ClickClackConfigSchema = buildMultiAccountChannelSchema(ClickClackAccountConfigSchema, {
65-
accountSchema: ClickClackAccountConfigSchema.partial(),
66-
});
64+
const ClickClackConfigSchema = buildMultiAccountChannelSchema(
65+
ClickClackAccountConfigSchema.extend({ historyLimit: z.number().int().min(0).optional() }),
66+
{ accountSchema: ClickClackAccountConfigSchema.partial() },
67+
);
6768

6869
/**
6970
* Config schema exported to core so `openclaw doctor` and config validation

‎extensions/line/src/bot.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const { createLineWebhookSpoolMock, handleLineWebhookEventsMock } = vi.hoisted((
1313
createLineWebhookSpoolMock: vi.fn(),
1414
// Typed parameters so the recorded call can be read back as the handler context.
1515
handleLineWebhookEventsMock: vi.fn(
16-
async (_events: webhook.Event[], _context: { mediaMaxBytes: number }) => {},
16+
async (
17+
_events: webhook.Event[],
18+
_context: { mediaMaxBytes: number; historyLimit: number },
19+
) => {},
1720
),
1821
}));
1922

@@ -105,3 +108,40 @@ describe("createLineBot media cap", () => {
105108
).resolves.toBe(2 * MB);
106109
});
107110
});
111+
112+
describe("createLineBot pending history cap", () => {
113+
it.each([
114+
{ root: 20, account: 3, expected: 3 },
115+
{ root: 20, account: undefined, expected: 20 },
116+
{ root: undefined, account: undefined, expected: 7 },
117+
{ root: 20, account: 0, expected: 0 },
118+
])("resolves account $account then root $root before the global fallback", async (testCase) => {
119+
let deliver: DeliverFn | undefined;
120+
createLineWebhookSpoolMock.mockImplementation((options: { deliver: DeliverFn }) => {
121+
deliver = options.deliver;
122+
return { accept: vi.fn(), start: vi.fn(), stop: vi.fn() };
123+
});
124+
125+
const line = {
126+
enabled: true,
127+
channelAccessToken: "test-token",
128+
channelSecret: "test-secret",
129+
...(testCase.root === undefined ? {} : { historyLimit: testCase.root }),
130+
accounts: {
131+
work: testCase.account === undefined ? {} : { historyLimit: testCase.account },
132+
},
133+
};
134+
createLineBot({
135+
channelAccessToken: "test-token",
136+
channelSecret: "test-secret",
137+
accountId: "work",
138+
config: {
139+
messages: { groupChat: { historyLimit: 7 } },
140+
channels: { line },
141+
} as unknown as OpenClawConfig,
142+
});
143+
144+
await deliver?.({ type: "message" } as webhook.Event, "destination", {});
145+
expect(handleLineWebhookEventsMock.mock.calls.at(-1)?.[1].historyLimit).toBe(testCase.expected);
146+
});
147+
});

‎extensions/line/src/bot.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ export function createLineBot(opts: LineBotOptions): LineBot {
7878
? { turnAdoptionLifecycle: control.turnAdoptionLifecycle }
7979
: {}),
8080
groupHistories,
81-
historyLimit: cfg.messages?.groupChat?.historyLimit ?? DEFAULT_GROUP_HISTORY_LIMIT,
81+
historyLimit:
82+
account.config.historyLimit ??
83+
cfg.messages?.groupChat?.historyLimit ??
84+
DEFAULT_GROUP_HISTORY_LIMIT,
8285
}),
8386
});
8487
spool.start();

‎extensions/line/src/config-schema.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ThreadBindingsSchema = z
2323
const LineCommonConfigSchemaBase = z.object({
2424
enabled: z.boolean().optional(),
2525
configWrites: z.boolean().optional(),
26+
historyLimit: z.number().int().min(0).optional(),
2627
channelAccessToken: z.string().optional(),
2728
channelSecret: z.string().optional(),
2829
tokenFile: z.string().optional(),

‎extensions/line/src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface LineAccountBaseConfig {
3131
groupPolicy?: "open" | "allowlist" | "disabled";
3232
responsePrefix?: string;
3333
mediaMaxMb?: number;
34+
historyLimit?: number;
3435
webhookPath?: string;
3536
threadBindings?: LineThreadBindingsConfig;
3637
groups?: Record<string, LineGroupConfig>;

‎extensions/mattermost/src/config-schema-core.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const MattermostAccountSchemaBase = z
134134
markdown: MarkdownConfigSchema,
135135
enabled: z.boolean().optional(),
136136
configWrites: z.boolean().optional(),
137+
historyLimit: z.number().int().min(0).optional(),
137138
botToken: buildSecretInputSchema().optional(),
138139
baseUrl: z.string().optional(),
139140
chatmode: z.enum(["oncall", "onmessage", "onchar"]).optional(),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
const buildEventPlan = vi.hoisted(() => vi.fn());
4+
const recordHistory = vi.hoisted(() => vi.fn());
5+
6+
vi.mock("./monitor-event-plan.js", () => ({ buildMattermostEventPlan: buildEventPlan }));
7+
vi.mock("./runtime-api.js", async (importOriginal) => ({
8+
...((await importOriginal()) as Record<string, unknown>),
9+
createChannelHistoryWindow: () => ({ record: recordHistory }),
10+
}));
11+
12+
const { createMattermostPostHandler } = await import("./monitor-posts.js");
13+
14+
describe("Mattermost pending history limit", () => {
15+
it.each([
16+
{ account: 3, expected: 3 },
17+
{ account: undefined, expected: 7 },
18+
{ account: 0, expected: 0 },
19+
])("passes the effective $expected limit into the pending-history owner", async (testCase) => {
20+
recordHistory.mockClear();
21+
buildEventPlan.mockResolvedValue({
22+
channelDisplay: "General",
23+
kind: "group",
24+
roomLabel: "general",
25+
route: { agentId: "main" },
26+
thread: { sessionKey: "agent:main:mattermost:group:chan-1" },
27+
finalizeContext: () => {},
28+
});
29+
30+
const monitor = {
31+
account: { accountId: "work", config: { historyLimit: testCase.account } },
32+
botUserId: "bot",
33+
botUsername: "bot",
34+
cfg: { messages: { groupChat: { historyLimit: 7 } } },
35+
core: {
36+
channel: {
37+
activity: { record: () => {} },
38+
commands: {
39+
shouldHandleTextCommands: () => false,
40+
isControlCommandMessage: () => false,
41+
},
42+
groups: { resolveRequireMention: () => true },
43+
mentions: { buildMentionRegexes: () => [], matchesMentionPatterns: () => false },
44+
pairing: { buildPairingReply: () => undefined },
45+
},
46+
},
47+
groupPolicy: "open",
48+
pairing: {},
49+
resources: {
50+
resolveMattermostMedia: async () => [],
51+
resolveUserInfo: async () => ({ username: "sender" }),
52+
},
53+
logVerboseMessage: () => {},
54+
logDebugMessage: () => {},
55+
} as unknown as Parameters<typeof createMattermostPostHandler>[0];
56+
57+
const handler = createMattermostPostHandler(monitor);
58+
await handler(
59+
{
60+
id: "post-1",
61+
channel_id: "chan-1",
62+
user_id: "sender",
63+
message: "no mention here",
64+
create_at: 1,
65+
} as never,
66+
{ data: { sender_name: "sender" } } as never,
67+
);
68+
69+
expect(recordHistory.mock.calls[0]?.[0]?.limit).toBe(testCase.expected);
70+
});
71+
});

‎extensions/mattermost/src/mattermost/monitor-posts.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export function createMattermostPostHandler(monitor: MattermostMonitorContext) {
5454
const channelHistories = new Map<string, HistoryEntry[]>();
5555
const historyLimit = Math.max(
5656
0,
57-
cfg.messages?.groupChat?.historyLimit ?? DEFAULT_GROUP_HISTORY_LIMIT,
57+
account.config.historyLimit ??
58+
cfg.messages?.groupChat?.historyLimit ??
59+
DEFAULT_GROUP_HISTORY_LIMIT,
5860
);
5961

6062
return async (

‎extensions/mattermost/src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type MattermostAccountConfig = {
5454
groupPolicy?: GroupPolicy;
5555
/** Outbound text chunk size (chars). Default: 4000. */
5656
textChunkLimit?: number;
57+
historyLimit?: number;
5758
/** Preview streaming config (nested-only; scalar modes migrate via doctor). */
5859
streaming?: ChannelPreviewStreamingConfig;
5960
/** Outbound response prefix override for this channel/account. */

‎extensions/qa-channel/src/config-schema.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ const QaChannelAccountConfigSchema = z
4040
})
4141
.strict();
4242

43-
const QaChannelConfigSchema = buildMultiAccountChannelSchema(QaChannelAccountConfigSchema, {
44-
accountSchema: QaChannelAccountConfigSchema.partial(),
45-
});
43+
const QaChannelConfigSchema = buildMultiAccountChannelSchema(
44+
QaChannelAccountConfigSchema.extend({ historyLimit: z.number().int().min(0).optional() }),
45+
{ accountSchema: QaChannelAccountConfigSchema.partial() },
46+
);
4647

4748
export const qaChannelPluginConfigSchema = buildChannelConfigSchema(QaChannelConfigSchema);

0 commit comments

Comments
 (0)