@ag-ui/proto 0.0.42 → 0.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2162,9 +2162,20 @@ function toCamelCase(str) {
2162
2162
  return str.toLowerCase().replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
2163
2163
  }
2164
2164
  function encode(event) {
2165
- const oneofField = toCamelCase(event.type);
2166
- const { type, timestamp, rawEvent, ...rest } = event;
2167
- if (type === import_core.EventType.MESSAGES_SNAPSHOT) {
2165
+ let validatedEvent;
2166
+ try {
2167
+ validatedEvent = import_core.EventSchemas.parse(event);
2168
+ } catch (err) {
2169
+ console.warn(
2170
+ "[ag-ui][proto.encode] Malformed devent detected, falling back to unvalidated event",
2171
+ err,
2172
+ event
2173
+ );
2174
+ validatedEvent = event;
2175
+ }
2176
+ const oneofField = toCamelCase(validatedEvent.type);
2177
+ const { type, timestamp, rawEvent, ...rest } = validatedEvent;
2178
+ if (type === import_core.EventType.MESSAGES_SNAPSHOT && Array.isArray(rest.messages)) {
2168
2179
  rest.messages = rest.messages.map((message) => {
2169
2180
  const untypedMessage = message;
2170
2181
  if (untypedMessage.toolCalls === void 0) {
@@ -2173,7 +2184,7 @@ function encode(event) {
2173
2184
  return message;
2174
2185
  });
2175
2186
  }
2176
- if (type === import_core.EventType.STATE_DELTA) {
2187
+ if (type === import_core.EventType.STATE_DELTA && Array.isArray(rest.delta)) {
2177
2188
  rest.delta = rest.delta.map((operation) => ({
2178
2189
  ...operation,
2179
2190
  op: JsonPatchOperationType[operation.op.toUpperCase()]