|
1 | 1 | import { |
2 | 2 | Model, |
3 | 3 | Usage, |
4 | | - withResponseSpan, |
5 | 4 | createResponseSpan, |
6 | 5 | setCurrentSpan, |
7 | 6 | resetCurrentSpan, |
@@ -876,32 +875,56 @@ export class OpenAIResponsesModel implements Model { |
876 | 875 | * @returns A promise that resolves to the response from the model. |
877 | 876 | */ |
878 | 877 | async getResponse(request: ModelRequest): Promise<ModelResponse> { |
879 | | - const response = await withResponseSpan(async (span) => { |
| 878 | + const span = request.tracing ? createResponseSpan() : undefined; |
| 879 | + |
| 880 | + try { |
| 881 | + if (span) { |
| 882 | + span.start(); |
| 883 | + setCurrentSpan(span); |
| 884 | + } |
| 885 | + |
880 | 886 | const response = await this.#fetchResponse(request, false); |
881 | 887 |
|
882 | | - if (request.tracing) { |
| 888 | + if (request.tracing && span) { |
883 | 889 | span.spanData.response_id = response.id; |
884 | 890 | span.spanData._input = request.input; |
885 | 891 | span.spanData._response = response; |
886 | 892 | } |
887 | 893 |
|
888 | | - return response; |
889 | | - }); |
890 | | - |
891 | | - const output: ModelResponse = { |
892 | | - usage: new Usage({ |
893 | | - inputTokens: response.usage?.input_tokens ?? 0, |
894 | | - outputTokens: response.usage?.output_tokens ?? 0, |
895 | | - totalTokens: response.usage?.total_tokens ?? 0, |
896 | | - inputTokensDetails: { ...response.usage?.input_tokens_details }, |
897 | | - outputTokensDetails: { ...response.usage?.output_tokens_details }, |
898 | | - }), |
899 | | - output: convertToOutputItem(response.output), |
900 | | - responseId: response.id, |
901 | | - providerData: response, |
902 | | - }; |
| 894 | + const output: ModelResponse = { |
| 895 | + usage: new Usage({ |
| 896 | + inputTokens: response.usage?.input_tokens ?? 0, |
| 897 | + outputTokens: response.usage?.output_tokens ?? 0, |
| 898 | + totalTokens: response.usage?.total_tokens ?? 0, |
| 899 | + inputTokensDetails: { ...response.usage?.input_tokens_details }, |
| 900 | + outputTokensDetails: { ...response.usage?.output_tokens_details }, |
| 901 | + }), |
| 902 | + output: convertToOutputItem(response.output), |
| 903 | + responseId: response.id, |
| 904 | + providerData: response, |
| 905 | + }; |
903 | 906 |
|
904 | | - return output; |
| 907 | + return output; |
| 908 | + } catch (error) { |
| 909 | + if (span) { |
| 910 | + span.setError({ |
| 911 | + message: 'Error getting response', |
| 912 | + data: { |
| 913 | + error: request.tracing |
| 914 | + ? String(error) |
| 915 | + : error instanceof Error |
| 916 | + ? error.name |
| 917 | + : undefined, |
| 918 | + }, |
| 919 | + }); |
| 920 | + } |
| 921 | + throw error; |
| 922 | + } finally { |
| 923 | + if (span) { |
| 924 | + span.end(); |
| 925 | + resetCurrentSpan(); |
| 926 | + } |
| 927 | + } |
905 | 928 | } |
906 | 929 |
|
907 | 930 | /** |
|
0 commit comments