MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / drainStreamInBackgroundToFindAllUsage

Method drainStreamInBackgroundToFindAllUsage

src/core/task/Task.ts:3035–3200  ·  view source on GitHub ↗
(
						apiReqIndex: number,
						status: "completed" | "cancelled" = "completed",
					)

Source from the content-addressed store, hash-verified

3033 }
3034
3035 const drainStreamInBackgroundToFindAllUsage = async (
3036 apiReqIndex: number,
3037 status: "completed" | "cancelled" = "completed",
3038 ) => {
3039 const timeoutMs = DEFAULT_USAGE_COLLECTION_TIMEOUT_MS
3040 const startTime = performance.now()
3041 const modelId = getModelId(this.apiConfiguration)
3042
3043 // Local variables to accumulate usage data without affecting the main flow
3044 let bgInputTokens = currentTokens.input
3045 let bgOutputTokens = currentTokens.output
3046 let bgCacheWriteTokens = currentTokens.cacheWrite
3047 let bgCacheReadTokens = currentTokens.cacheRead
3048 let bgTotalCost = currentTokens.total
3049
3050 // Helper function to capture telemetry and update messages
3051 const captureUsageData = async (
3052 tokens: {
3053 input: number
3054 output: number
3055 cacheWrite: number
3056 cacheRead: number
3057 total?: number
3058 },
3059 messageIndex: number = apiReqIndex,
3060 status: "completed" | "cancelled" = "completed",
3061 ) => {
3062 if (
3063 tokens.input > 0 ||
3064 tokens.output > 0 ||
3065 tokens.cacheWrite > 0 ||
3066 tokens.cacheRead > 0
3067 ) {
3068 // Update the shared variables atomically
3069 inputTokens = tokens.input
3070 outputTokens = tokens.output
3071 cacheWriteTokens = tokens.cacheWrite
3072 cacheReadTokens = tokens.cacheRead
3073 totalCost = tokens.total
3074
3075 // Update the API request message with the latest usage data
3076 updateApiReqMsg()
3077 await this.saveClineMessages()
3078
3079 // Update the specific message in the webview
3080 const apiReqMessage = this.clineMessages[messageIndex]
3081 if (apiReqMessage) {
3082 await this.updateClineMessage(apiReqMessage)
3083 }
3084
3085 // Capture telemetry with provider-aware cost calculation
3086 const modelId = getModelId(this.apiConfiguration)
3087 const apiProvider = this.apiConfiguration.apiProvider
3088 const apiProtocol = getApiProtocol(
3089 apiProvider && !isRetiredProvider(apiProvider) ? apiProvider : undefined,
3090 modelId,
3091 )
3092

Callers

nothing calls this directly

Calls 4

getModelIdFunction · 0.90
nextMethod · 0.80
warnMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected