(streamInfo: WorkspaceStreamInfo)
| 1988 | } |
| 1989 | |
| 1990 | private async getRefusalUsageSnapshot(streamInfo: WorkspaceStreamInfo): Promise<{ |
| 1991 | usage?: LanguageModelV2Usage; |
| 1992 | providerMetadata?: Record<string, unknown>; |
| 1993 | }> { |
| 1994 | let usage = hasTokenUsage(streamInfo.cumulativeUsage) ? streamInfo.cumulativeUsage : undefined; |
| 1995 | let providerMetadata = streamInfo.cumulativeProviderMetadata |
| 1996 | ? { ...streamInfo.cumulativeProviderMetadata } |
| 1997 | : undefined; |
| 1998 | |
| 1999 | if (!usage) { |
| 2000 | const streamMeta = await this.getStreamMetadata(streamInfo); |
| 2001 | usage = hasTokenUsage(streamMeta.totalUsage) ? streamMeta.totalUsage : undefined; |
| 2002 | providerMetadata = providerMetadata ?? streamMeta.contextProviderMetadata; |
| 2003 | } |
| 2004 | |
| 2005 | if (!usage) { |
| 2006 | return {}; |
| 2007 | } |
| 2008 | |
| 2009 | // Refused attempts never reach the normal stream-end path, so backfill |
| 2010 | // provider-omitted reasoningTokens before snapshotting usage for the |
| 2011 | // refusing model. reasoningBackfillStartIndex excludes earlier refused hops |
| 2012 | // when a later fallback also refuses after preserved parts. |
| 2013 | await this.backfillReasoningTokensFromParts(streamInfo, usage); |
| 2014 | |
| 2015 | providerMetadata = |
| 2016 | providerMetadata ?? |
| 2017 | (streamInfo.cumulativeProviderMetadata |
| 2018 | ? { ...streamInfo.cumulativeProviderMetadata } |
| 2019 | : await this.getAggregatedProviderMetadata(streamInfo)); |
| 2020 | |
| 2021 | return { |
| 2022 | usage: cloneUsage(usage), |
| 2023 | providerMetadata: markProviderMetadataCostsIncluded( |
| 2024 | providerMetadata ? { ...providerMetadata } : undefined, |
| 2025 | streamInfo.initialMetadata?.costsIncluded |
| 2026 | ), |
| 2027 | }; |
| 2028 | } |
| 2029 | |
| 2030 | /** |
| 2031 | * Attribute a refused fallback attempt's token usage to the refusing model |
no test coverage detected