(
options: TextOptions<TProviderOptions>,
)
| 183 | } |
| 184 | |
| 185 | async *chatStream( |
| 186 | options: TextOptions<TProviderOptions>, |
| 187 | ): AsyncIterable<StreamChunk> { |
| 188 | const requestParams = this.mapTextOptionsToMistral(options) |
| 189 | const timestamp = Date.now() |
| 190 | |
| 191 | const aguiState = { |
| 192 | runId: options.runId ?? generateId(this.name), |
| 193 | threadId: options.threadId ?? generateId(this.name), |
| 194 | messageId: generateId(this.name), |
| 195 | timestamp, |
| 196 | hasEmittedRunStarted: false, |
| 197 | } |
| 198 | |
| 199 | try { |
| 200 | const stream = this.fetchRawMistralStream(requestParams, this.rawConfig) |
| 201 | yield* this.processMistralStreamChunks(stream, options, aguiState) |
| 202 | } catch (error: unknown) { |
| 203 | const err = error as Error & { code?: string } |
| 204 | |
| 205 | if (!aguiState.hasEmittedRunStarted) { |
| 206 | aguiState.hasEmittedRunStarted = true |
| 207 | yield asChunk({ |
| 208 | type: 'RUN_STARTED', |
| 209 | runId: aguiState.runId, |
| 210 | threadId: aguiState.threadId, |
| 211 | model: options.model, |
| 212 | timestamp, |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | yield asChunk({ |
| 217 | type: 'RUN_ERROR', |
| 218 | runId: aguiState.runId, |
| 219 | model: options.model, |
| 220 | timestamp, |
| 221 | message: err.message || 'Unknown error', |
| 222 | code: err.code, |
| 223 | error: { |
| 224 | message: err.message || 'Unknown error', |
| 225 | code: err.code, |
| 226 | }, |
| 227 | }) |
| 228 | |
| 229 | throw err |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Generate structured output using Mistral's JSON Schema response format. |
no test coverage detected