ag-ui-protocol / ag-ui-protocol/ag-ui

fix: agent crashes with TypeError when messages is null/undefined

Đang mở
#1,961 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug SDK
Ngôn ngữ chính
Python
Star
15.9k
Fork
1.4k
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
163

Mô tả

## Bug

`AbstractAgent.messages` is a public property typed as `Message[]`. If it is assigned `null` or `undefined` at runtime, any subsequent call to `runAgent()`, `connectAgent()`, or event processing crashes with:

```
TypeError: Cannot read properties of undefined (reading 'map')
```

or

```
TypeError: Cannot read properties of null (reading 'filter')
```

## Reproduction

```ts
const agent = new MyAgent({ threadId: "t1" });
(agent as any).messages = undefined;
await agent.runAgent(); // TypeError
```

Also reproduces during a live run — if `messages` becomes `undefined` between the time the run starts and when a `RUN_ERROR` event fires, the event-processing pipeline crashes inside the subscriber callback with:

```
TypeError: Cannot read properties of undefined (reading 'map')
at Object.onRunErrorEvent
```

## Root Cause

Seven call sites in `agent.ts` and `default.ts` dereference `this.messages` / `agent.messages` directly without guarding against `null`/`undefined`:

- `runAgent` — `this.messages.map(...)` (build `currentMessageIds` set)
- `connectAgent` — same
- `prepareRunAgentInput` — `structuredClone_(this.messages).filter(...)`
- `runAgent` / `connectAgent` post-run — `structuredClone_(this.messages).filter(...)`
- `onInitialize` — `runSubscribersWithMutation(subscribers, this.messages, ...)`
- `onError` — same
- `defaultApplyEvents` — `structuredClone_(agent.messages)` → passed as `initialMessages` to every subscriber call including `onRunErrorEvent`

## Fix

Add `?? []` fallback at each site so a missing `messages` value is treated as an empty array rather than crashing.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.