MCPcopy
hub / github.com/coder/mux / getStreamMetadata

Method getStreamMetadata

src/node/services/streamManager.ts:925–966  ·  view source on GitHub ↗
(
    streamInfo: WorkspaceStreamInfo,
    timeoutMs = 1000
  )

Source from the content-addressed store, hash-verified

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,

Calls 2

resolveMethod · 0.80
withTimeoutFunction · 0.70

Tested by

no test coverage detected