refactor: remove redundant as-any casts in event-stream and agent#3331
refactor: remove redundant as-any casts in event-stream and agent#3331oldschoola wants to merge 2 commits into
Conversation
- EventStream.end()/endWaiting(): removed 'undefined as any' casts. IteratorResult<T> already allows undefined for value when done:true. - Agent turn_end handler: removed (event.message as any).errorMessage casts — errorMessage is already on the AssistantMessage type.
roboomp
left a comment
There was a problem hiding this comment.
P1: small, coherent cleanup with changelog coverage; bun check passed locally.
One should-fix: packages/ai/src/utils/event-stream.ts still leans on IteratorResult<T>'s implicit any return type instead of tightening the waiter result to undefined.
No duplicate/superseding PR found in the GitHub search results for this fix. Thanks for the cleanup.
| while (this.waiting.length > 0) { | ||
| const waiter = this.waiting.shift()!; | ||
| waiter.resolve({ value: undefined as any, done: true }); | ||
| waiter.resolve({ value: undefined, done: true }); |
There was a problem hiding this comment.
should-fix This still type-checks because waiting.resolve is typed as IteratorResult<T>, whose TReturn defaults to any. That removes the as any text but keeps the same implicit any escape hatch for both done: true resolves. Could we type the waiter result and the promise in [Symbol.asyncIterator] as IteratorResult<T, undefined> instead, so undefined is checked without relying on any?
…ing on TReturn=any Address review feedback: typing the waiter result and the Promise in the async iterator as IteratorResult<T, undefined> makes the undefined value explicit instead of relying on the default TReturn=any escape hatch.
|
Good point — addressed in the latest push. The waiter type and the Promise in the async iterator are now explicitly typed as |
Summary
Removes 3 redundant
as anycasts across two packages:packages/ai/src/utils/event-stream.tsEventStream.end()andendWaiting()resolved waiting consumers with{ value: undefined as any, done: true }. Theas anycast is unnecessary —IteratorResult<T>isIteratorYieldResult<T> | IteratorReturnResult<TReturn>whereTReturndefaults toany, soundefinedis assignable to thevaluefield whendone: true.packages/agent/src/agent.tsThe
turn_endhandler accessederrorMessagevia(event.message as any).errorMessage. TheerrorMessagefield is already on theAssistantMessageinterface (packages/ai/src/types.ts:524), so the cast is unnecessary.Verification
bun checkpassesagent.test.ts,agent-loop.test.ts,yield.test.ts,proxy-stream-disconnect.test.ts