Skip to content

Commit b7ef163

Browse files
committed
refactor(messageProcessor): integrate PII redaction in message processing
- Updated the MessageProcessor to utilize the new redactTextUsingPII function for redacting sensitive information from messages. - Changed buildMemory method to be asynchronous, ensuring proper handling of redaction before processing messages. - Enhanced type definitions for callback parameters to improve code clarity and maintainability.
1 parent 4e0d8a1 commit b7ef163

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/core/src/PII.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pipeline } from "@huggingface/transformers";
2+
import elizaLogger from "./logger";
23

34
type RedactionEntity = {
45
type: string;
@@ -394,3 +395,38 @@ export class PII {
394395
}
395396
}
396397
}
398+
399+
let piiInstance: PII | null = null;
400+
let piiInitPromise: Promise<void> | null = null;
401+
402+
async function ensurePIIInitialized(): Promise<PII | null> {
403+
if (piiInstance) return Promise.resolve(piiInstance);
404+
if (!piiInitPromise) {
405+
piiInitPromise = PII.create()
406+
.then((pii) => {
407+
piiInstance = pii;
408+
})
409+
.catch((error) => {
410+
elizaLogger.error(
411+
"Failed to initialize PII for message redaction:",
412+
error
413+
);
414+
piiInstance = null;
415+
});
416+
}
417+
return piiInitPromise.then(() => piiInstance);
418+
}
419+
420+
export async function redactTextUsingPII(text: string): Promise<string> {
421+
if (process.env.PII_REDACTION !== "true") return text;
422+
if (typeof text !== "string" || text.length === 0) return text;
423+
const pii = await ensurePIIInitialized();
424+
if (!pii) return text;
425+
try {
426+
const result = await pii.redact(text);
427+
return result?.redactedText ?? text;
428+
} catch (error) {
429+
elizaLogger.error("PII redaction failed in message processor:", error);
430+
return text;
431+
}
432+
}

packages/core/src/messageProcessor.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { composeContext } from "./context";
1515
import { generateMessageResponse } from "./generation";
1616
import { InteractionLogger, AgentClient } from "./interactionLogger";
17+
import { redactTextUsingPII } from "./PII";
1718

1819
export interface ReceivedMessage {
1920
rawMessageId: string;
@@ -54,7 +55,7 @@ export class MessageProcessor {
5455
message.source
5556
);
5657

57-
this.messageToProcess = this.buildMemory(message);
58+
this.messageToProcess = await this.buildMemory(message);
5859
await this.saveMemory(this.messageToProcess);
5960
this.state = await this.runtime.composeState(this.messageToProcess);
6061

@@ -73,7 +74,7 @@ export class MessageProcessor {
7374

7475
const callbackWithMemorySaving = async (
7576
content: Content,
76-
files: any[]
77+
files: Array<{ attachment: string; name: string }>
7778
) => {
7879
const memories = await callback(content, files);
7980
await this.saveMemories(memories);
@@ -157,9 +158,11 @@ export class MessageProcessor {
157158
});
158159
}
159160

160-
private buildMemory(message: ReceivedMessage): Memory {
161+
private async buildMemory(message: ReceivedMessage): Promise<Memory> {
162+
const redactedText = await redactTextUsingPII(message.text);
163+
161164
const content: Content = {
162-
text: message.text,
165+
text: redactedText,
163166
attachments: message.attachments,
164167
source: message.source,
165168
inReplyTo: message.inReplyTo,

0 commit comments

Comments
 (0)