| 94 | } |
| 95 | |
| 96 | function extractDeltaReasoning(delta: Record<string, unknown> | undefined): string { |
| 97 | if (!delta) { |
| 98 | return ""; |
| 99 | } |
| 100 | |
| 101 | const { reasoning, reasoning_content, reasoning_details } = delta; |
| 102 | |
| 103 | if (typeof reasoning === "string") { |
| 104 | return reasoning; |
| 105 | } |
| 106 | |
| 107 | if (typeof reasoning_content === "string") { |
| 108 | return reasoning_content; |
| 109 | } |
| 110 | |
| 111 | if (!Array.isArray(reasoning_details)) { |
| 112 | return ""; |
| 113 | } |
| 114 | |
| 115 | return reasoning_details |
| 116 | .map((detail) => { |
| 117 | if (!detail || typeof detail !== "object") { |
| 118 | return ""; |
| 119 | } |
| 120 | const entry = detail as Record<string, unknown>; |
| 121 | if (entry.type === "reasoning.text" && typeof entry.text === "string") { |
| 122 | return entry.text; |
| 123 | } |
| 124 | if (entry.type === "reasoning.summary" && typeof entry.summary === "string") { |
| 125 | return entry.summary; |
| 126 | } |
| 127 | if (typeof entry.text === "string") { |
| 128 | return entry.text; |
| 129 | } |
| 130 | if (typeof entry.summary === "string") { |
| 131 | return entry.summary; |
| 132 | } |
| 133 | return ""; |
| 134 | }) |
| 135 | .join(""); |
| 136 | } |
| 137 | |
| 138 | function extractDeltaParts(delta: Record<string, unknown> | undefined): StreamPart[] { |
| 139 | const parts: StreamPart[] = []; |