Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/channels/base/src/ChannelBase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5792,6 +5792,26 @@ describe('ChannelBase', () => {
expect(promptText).not.toContain('\u202E');
});

it('truncates long channel memory before injecting it into the prompt', async () => {
const channelMemory = {
readChannelMemory: vi
.fn()
.mockResolvedValue(`${'a'.repeat(13_000)}TAIL`),
appendChannelMemory: vi.fn().mockResolvedValue({ changed: true }),
clearChannelMemory: vi.fn().mockResolvedValue({ changed: true }),
};
const ch = createChannel({ allowedUsers: ['alice'] }, { channelMemory });

await ch.handleInbound(envelope({ text: 'ship it', senderId: 'alice' }));

const promptText = (bridge.prompt as ReturnType<typeof vi.fn>).mock
.calls[0][1] as string;
expect(promptText).toContain('Channel memory for this chat (truncated):');
expect(promptText).toContain('[Channel memory truncated]');
expect(promptText).not.toContain('TAIL');
expect(promptText.length).toBeLessThan(12_500);
});

it('does not read or inject memory again in the same session', async () => {
let reads = 0;
const channelMemory = {
Expand Down
31 changes: 23 additions & 8 deletions packages/channels/base/src/ChannelBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const CURRENT_MESSAGE_MARKER = '[Current message - respond to this]';
const GROUP_HISTORY_ENTRY_TEXT_LIMIT = 1000;
const GROUP_HISTORY_ENTRY_METADATA_LIMIT = 256;
const LOOP_CANCEL_GRACE_MS = 5000;
const CHANNEL_MEMORY_PROMPT_CHAR_LIMIT = 12_000;
const CHANNEL_MEMORY_CLASSIFIER_MIN_CONFIDENCE = 0.7;
const CHANNEL_MEMORY_CLASSIFIER_TRIGGER_RE =
/(记住|记得|记一下|记忆|忘掉|忘记|清空|清除|删除|保存|remember|memory|forget)/iu;
Expand Down Expand Up @@ -97,6 +98,20 @@ const SENSITIVE_PAYLOAD_KEY_PATTERN = new RegExp(
'i',
);

function formatChannelMemoryPrompt(memoryText: string): string | undefined {
const sanitized = sanitizePromptText(memoryText).trim();
if (!sanitized) {
return undefined;
}
if (sanitized.length <= CHANNEL_MEMORY_PROMPT_CHAR_LIMIT) {
return `Channel memory for this chat:\n${sanitized}`;
}
const truncated = sanitized
.slice(0, CHANNEL_MEMORY_PROMPT_CHAR_LIMIT)
.trimEnd();
return `Channel memory for this chat (truncated):\n${truncated}\n[Channel memory truncated]`;
}

export interface ChannelBaseOptions {
router?: SessionRouter;
proxy?: string;
Expand Down Expand Up @@ -696,10 +711,9 @@ export abstract class ChannelBase {
threadId: job.target.threadId,
})
).trim();
if (memoryText) {
context.push(
`Channel memory for this chat:\n${sanitizePromptText(memoryText)}`,
);
const memoryPrompt = formatChannelMemoryPrompt(memoryText);
if (memoryPrompt) {
context.push(memoryPrompt);
}
} catch (error) {
process.stderr.write(
Expand Down Expand Up @@ -3447,10 +3461,11 @@ export abstract class ChannelBase {
this.instructedSessions.delete(sessionId);
}
}
if (memoryText) {
sessionContext.push(
`Channel memory for this chat:\n${sanitizePromptText(memoryText)}`,
);
const memoryPrompt = memoryText
? formatChannelMemoryPrompt(memoryText)
: undefined;
if (memoryPrompt) {
sessionContext.push(memoryPrompt);
}
if (this.config.instructions) {
sessionContext.push(this.config.instructions);
Expand Down
Loading