Skip to content

refactor(ai): make the open-set value types extension types - #42

Open
VelikovPetar wants to merge 2 commits into
mainfrom
refactor/open-set-extension-types
Open

VelikovPetar wants to merge 2 commits into
mainfrom
refactor/open-set-extension-types

Conversation

@VelikovPetar

Copy link
Copy Markdown
Contributor

🎯 Goal

The package's open sets — values an agent writes as strings, of which the package knows some but must keep the rest — were immutable classes wrapping a String with hand-written ==, hashCode and toString. The Stream Chat Flutter SDK models the same thing as extension types on String (e.g. AttachmentType); this does the same here, so callers compare and switch on them as plain strings.

🛠 Implementation details

  • AIMessagePartKind, AIReasoningStatus, AIToolCallStatus, AIToolExecutor, AIToolApprovalDecision, AIMessageRole and AIMessageAttachmentKind are now extension type const X(String _) implements String, each with its constants, values and isKnown. AIToolCallStatus.isFinished stays.
  • AIMessageRole and AIMessageAttachmentKind gain values and isKnown, like the other five.
  • No more .value: JSON writing, toStrings, the doc samples and the README now use the value itself (switch (part.kind) { 'ai_citation' => … }).
  • The debug warning for an unknown value quotes the raw string ( `status` "queued" is not one this package knows) instead of printing the old toString().
  • Since these are strings at runtime, two types with the same raw value are now equal (AIToolCallStatus.running == AIToolExecutor('running')); the test that asserted otherwise is replaced by ones checking each type equals its raw string.
  • CHANGELOG under 🔄 Changed — not breaking, none of these shipped in 0.1.0.

#41 adds AIToolCallStatus.awaitingAuthorization to the class form; whichever lands second needs a small rebase in ai_tool_call_part.dart.

🧪 Testing

  • dart analyze --fatal-infos and dart format clean.
  • CI=true flutter test on parts, conversation, components and tools: all pass except AIToolApprovalView goldens light/dark (variant: CI), which fail identically on main on a macOS host (CI goldens are Linux-rendered).

🤖 Generated with Claude Code

AIMessagePartKind, AIReasoningStatus, AIToolCallStatus, AIToolExecutor,
AIToolApprovalDecision, AIMessageRole and AIMessageAttachmentKind become
`extension type const X(String _) implements String`, the pattern the
Stream Chat Flutter SDK uses for its open sets. Callers compare or switch
on them as strings, with no `.value`. AIMessageRole and
AIMessageAttachmentKind gain `values` and `isKnown` like the others.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VelikovPetar
VelikovPetar requested a review from a team October 27, 2026 09:41

@xsahil03x xsahil03x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a nice cleanup: the hand-written ==/hashCode/toString boilerplate goes away and the types now line up with AttachmentType in stream_chat. I checked that all seven types are converted the same way, that nothing in lib/, the example, the README or the tests still reaches for .value, that no runtime is checks or mixed-type maps or sets depend on the old class identity, and that the JSON still round-trips as plain strings. dart analyze --fatal-infos, dart format and flutter test --exclude-tags golden (1250 tests) all pass.

Please do these before merging:

  1. Fix the CHANGELOG entry, which describes a change from a class form that no release ever had (see inline).

The rest is optional and inline.

Comment thread packages/stream_chat_flutter_ai/CHANGELOG.md Outdated
Comment thread packages/stream_chat_flutter_ai/test/src/parts/ai_message_part_test.dart Outdated
Merges the two equality tests, which both only exercise String ==.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@xsahil03x xsahil03x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Each type's Added entry now says what it is, and the tests read well merged. Verified against the new code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants