(
streamInfo: WorkspaceStreamInfo,
timeoutMs = 1000
)
| 923 | } |
| 924 | |
| 925 | private async getStreamMetadata( |
| 926 | streamInfo: WorkspaceStreamInfo, |
| 927 | timeoutMs = 1000 |
| 928 | ): Promise<{ |
| 929 | totalUsage?: LanguageModelV2Usage; |
| 930 | contextUsage?: LanguageModelV2Usage; |
| 931 | contextProviderMetadata?: Record<string, unknown>; |
| 932 | finishReason?: string; |
| 933 | duration: number; |
| 934 | }> { |
| 935 | // Helper: wrap promise with independent timeout + error handling |
| 936 | // Each promise resolves independently - one failure doesn't mask others |
| 937 | const withTimeout = <T>(promise: PromiseLike<T>): Promise<T | undefined> => |
| 938 | Promise.race([ |
| 939 | promise, |
| 940 | new Promise<undefined>((resolve) => setTimeout(() => resolve(undefined), timeoutMs)), |
| 941 | ]).catch(() => undefined); |
| 942 | |
| 943 | // Fetch all metadata in parallel with independent timeouts |
| 944 | // - totalUsage: sum of all steps (for cost calculation) |
| 945 | // - contextUsage: last step only (for context window display) |
| 946 | // - contextProviderMetadata: last step (for context window cache display) |
| 947 | const streamResultWithFinishReason = streamInfo.streamResult as { |
| 948 | finishReason?: PromiseLike<string>; |
| 949 | }; |
| 950 | const [totalUsage, contextUsage, contextProviderMetadata, finishReason] = await Promise.all([ |
| 951 | withTimeout(streamInfo.streamResult.totalUsage), |
| 952 | withTimeout(streamInfo.streamResult.usage), |
| 953 | withTimeout(streamInfo.streamResult.providerMetadata), |
| 954 | streamResultWithFinishReason.finishReason |
| 955 | ? withTimeout(streamResultWithFinishReason.finishReason) |
| 956 | : Promise.resolve(undefined), |
| 957 | ]); |
| 958 | |
| 959 | return { |
| 960 | totalUsage, |
| 961 | contextUsage, |
| 962 | contextProviderMetadata, |
| 963 | finishReason, |
| 964 | duration: Date.now() - streamInfo.startTime, |
| 965 | }; |
| 966 | } |
| 967 | |
| 968 | private resolveTotalUsageForStreamEnd( |
| 969 | streamInfo: WorkspaceStreamInfo, |
no test coverage detected