@@ -14,6 +14,8 @@ type OpenAIResponsesStreamHandler struct {
1414 Usage * types.Usage
1515 Prefix string
1616 Model string
17+
18+ searchType string
1719}
1820
1921func (p * OpenAIProvider ) CreateResponses (request * types.OpenAIResponsesRequest ) (openaiResponse * types.OpenAIResponsesResponses , errWithCode * types.OpenAIErrorWithStatusCode ) {
@@ -43,7 +45,7 @@ func (p *OpenAIProvider) CreateResponses(request *types.OpenAIResponsesRequest)
4345
4446 * p .Usage = * response .Usage .ToOpenAIUsage ()
4547
46- getResponsesExtraBilling (request . Tools , p .Usage )
48+ getResponsesExtraBilling (response , p .Usage )
4749
4850 return response , nil
4951}
@@ -93,39 +95,73 @@ func (h *OpenAIResponsesStreamHandler) HandlerChatStream(rawLine *[]byte, dataCh
9395 }
9496
9597 switch openaiResponse .Type {
98+ case "response.created" :
99+ if len (openaiResponse .Response .Tools ) > 0 {
100+ for _ , tool := range openaiResponse .Response .Tools {
101+ if tool .Type == types .APITollTypeWebSearchPreview {
102+ h .searchType = "medium"
103+ if tool .SearchContextSize != "" {
104+ h .searchType = tool .SearchContextSize
105+ }
106+ }
107+ }
108+ }
96109 case "response.output_text.delta" :
97110 delta , ok := openaiResponse .Delta .(string )
98111 if ok {
99112 h .Usage .TextBuilder .WriteString (delta )
100113 }
114+ case "response.output_item.added" :
115+ if openaiResponse .Item != nil {
116+ switch openaiResponse .Item .Type {
117+ case types .InputTypeWebSearchCall :
118+ if h .searchType == "" {
119+ h .searchType = "medium"
120+ }
121+ h .Usage .IncExtraBilling (types .APITollTypeWebSearchPreview , h .searchType )
122+ case types .InputTypeCodeInterpreterCall :
123+ h .Usage .IncExtraBilling (types .APITollTypeCodeInterpreter , "" )
124+ case types .InputTypeFileSearchCall :
125+ h .Usage .IncExtraBilling (types .APITollTypeFileSearch , "" )
126+ }
127+ }
101128 case "response.completed" :
102129 if openaiResponse .Response != nil {
103130 usage := openaiResponse .Response .Usage
104131 * h .Usage = * usage .ToOpenAIUsage ()
105- getResponsesExtraBilling (openaiResponse .Response .Tools , h .Usage )
132+ getResponsesExtraBilling (openaiResponse .Response , h .Usage )
133+
106134 }
107135 }
108136
109137 dataChan <- rawStr
110138}
111139
112- func getResponsesExtraBilling (tools [] types.ResponsesTools , usage * types.Usage ) {
113- if len ( tools ) == 0 || usage == nil {
140+ func getResponsesExtraBilling (response * types.OpenAIResponsesResponses , usage * types.Usage ) {
141+ if usage == nil {
114142 return
115143 }
116144
117- for _ , tool := range tools {
118- switch tool .Type {
119- case types .APITollTypeWebSearchPreview :
120- searchType := "medium"
121- if tool .SearchContextSize != "" {
122- searchType = tool .SearchContextSize
145+ searchType := "medium"
146+ if len (response .Tools ) > 0 {
147+ for _ , tool := range response .Tools {
148+ if tool .Type == types .APITollTypeWebSearchPreview {
149+ if tool .SearchContextSize != "" {
150+ searchType = tool .SearchContextSize
151+ }
152+ }
153+ }
154+ }
155+ if len (response .Output ) > 0 {
156+ for _ , output := range response .Output {
157+ switch output .Type {
158+ case types .InputTypeWebSearchCall :
159+ usage .IncExtraBilling (types .APITollTypeWebSearchPreview , searchType )
160+ case types .InputTypeCodeInterpreterCall :
161+ usage .IncExtraBilling (types .APITollTypeCodeInterpreter , "" )
162+ case types .InputTypeFileSearchCall :
163+ usage .IncExtraBilling (types .APITollTypeFileSearch , "" )
123164 }
124- usage .IncExtraBilling (types .APITollTypeWebSearchPreview , searchType )
125- case types .APITollTypeCodeInterpreter :
126- usage .IncExtraBilling (types .APITollTypeCodeInterpreter , "" )
127- case types .APITollTypeFileSearch :
128- usage .IncExtraBilling (types .APITollTypeFileSearch , "" )
129165 }
130166 }
131167}
0 commit comments