@@ -68,6 +68,12 @@ public enum ChatGPTResponse: Equatable {
6868 case partialText( String )
6969 case partialReasoning( String )
7070 case toolCalls( [ ChatMessage . ToolCall ] )
71+ case usage(
72+ promptTokens: Int ,
73+ completionTokens: Int ,
74+ cachedTokens: Int ,
75+ otherUsage: [ String : Int ]
76+ )
7177}
7278
7379public typealias ChatGPTResponseStream = AsyncThrowingStream < ChatGPTResponse , any Error >
@@ -196,7 +202,7 @@ public class ChatGPTService: ChatGPTServiceType {
196202 switch content {
197203 case let . partialText( text) :
198204 continuation. yield ( ChatGPTResponse . partialText ( text) )
199-
205+
200206 case let . partialReasoning( text) :
201207 continuation. yield ( ChatGPTResponse . partialReasoning ( text) )
202208
@@ -222,6 +228,20 @@ public class ChatGPTService: ChatGPTServiceType {
222228 }
223229 }
224230 }
231+ case let . usage(
232+ promptTokens,
233+ completionTokens,
234+ cachedTokens,
235+ otherUsage
236+ ) :
237+ continuation. yield (
238+ . usage(
239+ promptTokens: promptTokens,
240+ completionTokens: completionTokens,
241+ cachedTokens: cachedTokens,
242+ otherUsage: otherUsage
243+ )
244+ )
225245 }
226246 }
227247
@@ -257,6 +277,12 @@ extension ChatGPTService {
257277 case partialReasoning( String )
258278 case partialText( String )
259279 case partialToolCalls( [ Int : ChatMessage . ToolCall ] )
280+ case usage(
281+ promptTokens: Int ,
282+ completionTokens: Int ,
283+ cachedTokens: Int ,
284+ otherUsage: [ String : Int ]
285+ )
260286 }
261287
262288 enum FunctionCallResult {
@@ -345,14 +371,19 @@ extension ChatGPTService {
345371 if let content = delta. content {
346372 continuation. yield ( . partialText( content) )
347373 }
348-
374+
349375 if let reasoning = delta. reasoningContent {
350376 continuation. yield ( . partialReasoning( reasoning) )
351377 }
352378 }
353379
354380 Logger . service. info ( " ChatGPT usage: \( usage) " )
355-
381+ continuation. yield ( . usage(
382+ promptTokens: usage. promptTokens,
383+ completionTokens: usage. completionTokens,
384+ cachedTokens: usage. cachedTokens,
385+ otherUsage: usage. otherUsage
386+ ) )
356387 continuation. finish ( )
357388 } catch let error as CancellationError {
358389 continuation. finish ( throwing: error)
@@ -554,6 +585,7 @@ extension ChatGPTService {
554585 case . system: . system
555586 case . user: . user
556587 case . assistant: . assistant
588+ case . tool: . tool
557589 }
558590 } ( ) ,
559591 content: chatMessage. content ?? " " ,
0 commit comments