SIGN IN SIGN UP

fix: accept documented Moonshot streaming tool-call and usage variants (#19586)

## Background

Moonshot streams could reject documented index-less tool-call chunks and
lose usage reported inside a choice.

## Root Cause

The Moonshot chunk schema required every tool-call index and omitted
choice-level usage, while the stream processor only retained top-level
usage. Captured SSE fixtures confirmed both failures.

## Summary

Made streaming tool-call indices nullish, derived stable array-position
fallbacks, collected choice-level usage with top-level precedence, and
added a patch changeset.

## Testing

Added captured-style streaming fixtures covering index-less and explicit
tool calls, choice-level and top-level usage, precedence, and
malformed-index validation.

## End-to-end Validation

- `pnpm -C examples/ai-functions exec tsx
src/stream-text/moonshot/tool-call.ts` — live Moonshot streaming
completed with assembled tool calls, results, token usage, and a `stop`
finish reason.

## Related Issues

Fixes #19546

Closes #19572

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
Co-authored-by: ai-sdk-factory <308175966+ai-sdk-factory@users.noreply.github.com>
A
ai-sdk-factory[bot] committed
d53589a9d575928c1acee791db1bbb3f339aeae1
Parent: c88b272
Committed by GitHub <noreply@github.com> on 8/26/2026, 1:18:01 AM