cloudflare / cloudflare/agents

Expose client-facing chat sanitization hooks separate from persistence sanitization

Open
#1,744 2 comments 1 reaction 1 assignee Claimed by @threepointone View on GitHub
Dominant language
TypeScript
Stars
5.6k
Forks
711
Avg merge
1d 20h
Merged PRs (30d)
53

Description

Today Think & AiChatAgent allows to sanitize persisted messages via `sanitizeMessageForPersistence()`, but doesn't expose a hook to sanitize what is exposed to the client. For example, I might want to hide from the frontend a tool output. I know this does not 100% guarantee and the user might just ask like "what was that tool output", but for that we can add more system prompts and other anti-jailbreaks.

Something like:

```ts
protected sanitizeMessageForClient(message: UIMessage): UIMessage

// and

protected sanitizeChatResponseChunkForClient(
chunk: UIMessageChunk
): UIMessageChunk | null
```

or just a

```ts
protected sanitizeOutgoingChatFrame(
frame: OutgoingMessage
): OutgoingMessage | null
```

Ideally they should cover `CF_AGENT_USE_CHAT_RESPONSE`, `CF_AGENT_CHAT_MESSAGES`, `/get-messages`...

A more declarative option like:

```ts
clientTranscriptPolicy = {
stripToolOutputs: true,
stripToolInputs: true,
stripProviderMetadata: true,
}
```

would also be welcomed :)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.