feat: add per-DM history limit overrides

This commit is contained in:
Marc Terns
2026-01-11 08:53:50 -06:00
parent ab9ea827a4
commit 54abf4b0d7
4 changed files with 139 additions and 11 deletions

View File

@@ -431,6 +431,71 @@ describe("getDmHistoryLimitFromSessionKey", () => {
).toBe(5);
}
});
it("returns per-DM override when set", () => {
const config = {
telegram: {
dmHistoryLimit: 15,
dms: { "123": { historyLimit: 5 } },
},
} as ClawdbotConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(5);
});
it("falls back to provider default when per-DM not set", () => {
const config = {
telegram: {
dmHistoryLimit: 15,
dms: { "456": { historyLimit: 5 } },
},
} as ClawdbotConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(15);
});
it("returns per-DM override for agent-prefixed keys", () => {
const config = {
telegram: {
dmHistoryLimit: 20,
dms: { "789": { historyLimit: 3 } },
},
} as ClawdbotConfig;
expect(
getDmHistoryLimitFromSessionKey("agent:main:telegram:dm:789", config),
).toBe(3);
});
it("handles userId with colons (e.g., email)", () => {
const config = {
msteams: {
dmHistoryLimit: 10,
dms: { "user@example.com": { historyLimit: 7 } },
},
} as ClawdbotConfig;
expect(
getDmHistoryLimitFromSessionKey("msteams:dm:user@example.com", config),
).toBe(7);
});
it("returns undefined when per-DM historyLimit is not set", () => {
const config = {
telegram: {
dms: { "123": {} },
},
} as ClawdbotConfig;
expect(
getDmHistoryLimitFromSessionKey("telegram:dm:123", config),
).toBeUndefined();
});
it("returns 0 when per-DM historyLimit is explicitly 0 (unlimited)", () => {
const config = {
telegram: {
dmHistoryLimit: 15,
dms: { "123": { historyLimit: 0 } },
},
} as ClawdbotConfig;
expect(getDmHistoryLimitFromSessionKey("telegram:dm:123", config)).toBe(0);
});
});
describe("runEmbeddedPiAgent", () => {

View File

@@ -446,12 +446,16 @@ export function limitHistoryTurns(
}
/**
* Extracts the provider name from a session key and looks up dmHistoryLimit
* from the provider config.
* Extracts the provider name and user ID from a session key and looks up
* dmHistoryLimit from the provider config, with per-DM override support.
*
* Session key formats:
* - `telegram:dm:123` → provider = telegram
* - `agent:main:telegram:dm:123` → provider = telegram (skip "agent:<id>:")
* - `telegram:dm:123` → provider = telegram, userId = 123
* - `agent:main:telegram:dm:123` → provider = telegram, userId = 123
*
* Resolution order:
* 1. Per-DM override: provider.dms[userId].historyLimit
* 2. Provider default: provider.dmHistoryLimit
*/
export function getDmHistoryLimitFromSessionKey(
sessionKey: string | undefined,
@@ -467,22 +471,49 @@ export function getDmHistoryLimitFromSessionKey(
const provider = providerParts[0]?.toLowerCase();
if (!provider) return undefined;
// Extract userId: format is provider:dm:userId or provider:dm:userId:...
// The userId may contain colons (e.g., email addresses), so join remaining parts
const kind = providerParts[1]?.toLowerCase();
const userId = providerParts.slice(2).join(":");
// Helper to get limit with per-DM override support
const getLimit = (
providerConfig:
| {
dmHistoryLimit?: number;
dms?: Record<string, { historyLimit?: number }>;
}
| undefined,
): number | undefined => {
if (!providerConfig) return undefined;
// Check per-DM override first
if (
userId &&
kind === "dm" &&
providerConfig.dms?.[userId]?.historyLimit !== undefined
) {
return providerConfig.dms[userId].historyLimit;
}
// Fall back to provider default
return providerConfig.dmHistoryLimit;
};
// Map provider to config key
switch (provider) {
case "telegram":
return config.telegram?.dmHistoryLimit;
return getLimit(config.telegram);
case "whatsapp":
return config.whatsapp?.dmHistoryLimit;
return getLimit(config.whatsapp);
case "discord":
return config.discord?.dmHistoryLimit;
return getLimit(config.discord);
case "slack":
return config.slack?.dmHistoryLimit;
return getLimit(config.slack);
case "signal":
return config.signal?.dmHistoryLimit;
return getLimit(config.signal);
case "imessage":
return config.imessage?.dmHistoryLimit;
return getLimit(config.imessage);
case "msteams":
return config.msteams?.dmHistoryLimit;
return getLimit(config.msteams);
default:
return undefined;
}