| 73 | } |
| 74 | |
| 75 | function extractResponseText(payload: OpenAIChatCompletionResponse): string { |
| 76 | const content = payload.choices?.[0]?.message?.content; |
| 77 | if (typeof content === "string") { |
| 78 | return content.trim(); |
| 79 | } |
| 80 | if (Array.isArray(content)) { |
| 81 | return content |
| 82 | .map((part) => (typeof part?.text === "string" ? part.text : "")) |
| 83 | .join("") |
| 84 | .trim(); |
| 85 | } |
| 86 | return ""; |
| 87 | } |
| 88 | |
| 89 | function extractUsage(payload: OpenAIChatCompletionResponse): TokenUsageStats { |
| 90 | const promptTokens = payload.usage?.prompt_tokens ?? 0; |