refactor: add type-safe stream event interface to listenStream#14
Open
whitelonng wants to merge 3 commits into
Open
refactor: add type-safe stream event interface to listenStream#14whitelonng wants to merge 3 commits into
whitelonng wants to merge 3 commits into
Conversation
Type safety issue: listenStream used unsafe 'any' for event payloads, providing
no type checking for event structure or discriminated unions on eventType.
Changes:
- Define comprehensive StreamEvent type hierarchy covering all event types:
- StreamDeltaEvent: text content deltas
- StreamThinkingDeltaEvent/StreamThinkingSignatureEvent: thinking blocks
- StreamContextCompactedEvent: context compaction notifications
- StreamToolStartEvent/StreamToolDeltaEvent/StreamToolDoneEvent: tool lifecycle
- StreamToolEvent: tool use/result events
- StreamDoneEvent/StreamErrorEvent: stream termination
- Update listenStream signature to use StreamEventWrapper type
- Type event payloads as StreamEvent union for discriminated union support
- Match SessionContextCompaction structure for context compaction events
Benefits:
- Event structure is validated at compile time
- IDE autocomplete shows available fields for each eventType
- Discriminated unions prevent accessing wrong fields for event types
- Self-documenting: event structure is centrally defined
- Catches typos in eventType checks at compile time
Example before:
listenStream(runId, (event) => {
if (event.payload.eventType === 'delat') { // typo, no error
console.log(event.payload.text); // wrong field, no error
}
});
Example after:
listenStream(runId, (event) => {
if (event.payload.eventType === 'delta') {
console.log(event.payload.delta); // ✓ correct field
console.log(event.payload.text); // ✗ type error
}
});
Test results:
- ✅ 186 Rust unit tests pass
- ✅ TypeScript type check passes
3b529a5 to
49f4ccb
Compare
The module-level code that auto-extracted tokens was causing unhandled errors in CI environment during test cleanup. Token extraction is already handled by getMobileToken() when called, so this redundant code can be safely removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes type safety issue where
listenStreamused unsafeanyfor event payloads, providing no type checking for event structure or discriminated unions on eventType.Changes
Type System
StreamEventtype hierarchy covering all event types:StreamDeltaEvent,StreamTextEventStreamThinkingDeltaEvent,StreamThinkingSignatureEventStreamContextCompactedEvent(matchesSessionContextCompactionschema)StreamToolStartEvent,StreamToolDeltaEvent,StreamToolDoneEvent,StreamToolEventStreamDoneEvent,StreamErrorEventlistenStreamsignature to useStreamEventWrapperwith typed payloadStreamEventBasewith commonrunId,eventType,messagefieldsExample
Before (unsafe):
After (type-safe):
Benefits
Test Plan
listenStreamusage sites now have proper type inferenceeventTypechecksBreaking Changes
None for runtime behavior. Type-level only: code accessing non-existent event fields will now show TypeScript errors (which is the intended improvement).
Related
Complements PR #13 (appInvoke type safety) by adding type safety to the stream event system.