SIGN IN SIGN UP

feat(chat): runtime clientData validation for custom agents (#4646)

## Summary

`chat.withClientData({ schema }).customAgent()` now parses
`payload.metadata` before passing it to `run`, `chat.messages`, or
`chat.createSession`. Schema defaults and transforms are preserved.

Custom agents without a schema keep the existing pass-through behavior.
This does not change `chat.agent()`. Raw custom agents do not expose an
action schema, so `payload.action` remains `unknown`.

## Validation failures

Invalid client data is logged and never passed to user code. The client
receives a fixed `Invalid client data` error; validator details stay in
the task log and `onClientDataValidationError`.

- Submitted turns and async reads write the error followed by
`turn-complete`, then wait for the next valid frame. This settles the
invalid input before the raw read returns. Callers that need to
coordinate validation with their own persistence or settlement should
omit the schema and validate the full frame in their loop.
- Messageless preload and continuation boots call
`onClientDataValidationError` and wait without writing a terminal frame.
- Active `chat.messages.on()` subscriptions skip invalid frames and call
`onClientDataValidationError` without ending the response. `off()` stops
new frames. A valid frame accepted before `off()` finishes validation
and is delivered; an invalid pending frame is logged without invoking
user callbacks.
- `chat.messages.peek()` throws synchronously.
- Invalid head-start handovers fail closed. A skip ends the run. A real
handover writes the validation error after the warm output, writes
`turn-complete`, and ends the run.

Validation is automatic when a schema is declared. We can make it opt-in
or return a typed failure if maintainers prefer that contract.

## Testing

- `pnpm --filter @trigger.dev/sdk run test -- --run`
- `pnpm --filter @trigger.dev/sdk run typecheck`
- `pnpm run build --filter @trigger.dev/sdk`
- `pnpm run lint`
- Formatting checks pass

## ✅ Checklist

- [x] I followed the contributing guide
- [x] The PR title follows the convention
- [x] I ran and tested the change

## Changelog

Custom chat agents now validate and parse client data declared with
`chat.withClientData({ schema })` before passing it to agent code.

## Screenshots

Not applicable.

---------

Co-authored-by: Eric Allam <eallam@icloud.com>
G
Graham Tremper committed
acaa5ec2278c26594b955ac5ce96980f68ab6827
Parent: a3af29f
Committed by GitHub <noreply@github.com> on 8/27/2026, 5:40:41 PM