@@ -9,7 +9,6 @@ import type {
99 FunctionTool ,
1010} from "@mistralai/mistralai/models/components" ;
1111import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
12- import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice" ;
1312import { getEnvApiKey } from "../env-api-keys.js" ;
1413import { getAiTransportHost } from "../host.js" ;
1514import { calculateCost , clampThinkingLevel } from "../model-utils.js" ;
@@ -31,6 +30,7 @@ import type {
3130import { AssistantMessageEventStream } from "../utils/event-stream.js" ;
3231import { shortHash } from "../utils/hash.js" ;
3332import { parseStreamingJson } from "../utils/json-parse.js" ;
33+ import { projectProviderError } from "../utils/provider-error.js" ;
3434import { sanitizeSurrogates } from "../utils/sanitize-unicode.js" ;
3535import { createSseByteGuard } from "../utils/streaming-byte-guard.js" ;
3636import { stripSystemPromptCacheBoundary } from "../utils/system-prompt-cache-boundary.js" ;
@@ -43,7 +43,6 @@ import {
4343import { transformMessages } from "./transform-messages.js" ;
4444
4545const MISTRAL_TOOL_CALL_ID_LENGTH = 9 ;
46- const MAX_MISTRAL_ERROR_BODY_CHARS = 4000 ;
4746
4847// 16 MiB cap on Mistral streaming success bodies, matching the
4948// `PROVIDER_TEXT_RESPONSE_MAX_BYTES` / `PROVIDER_JSON_RESPONSE_MAX_BYTES`
@@ -194,9 +193,9 @@ export const streamMistral: StreamFunction<"mistral-conversations", MistralOptio
194193 } catch ( error ) {
195194 // Failed or canceled generations must never retain partially repaired tool calls.
196195 output . content = output . content . filter ( ( block ) => block . type !== "toolCall" ) ;
197- output . stopReason = options ?. signal ?. aborted ? "aborted" : "error" ;
198- output . errorMessage = formatMistralError ( error ) ;
199- stream . push ( { type : "error" , reason : output . stopReason , error : output } ) ;
196+ const terminal = projectProviderError ( error , options ?. signal ) ;
197+ Object . assign ( output , terminal ) ;
198+ stream . push ( { type : "error" , reason : terminal . stopReason , error : output } ) ;
200199 stream . end ( ) ;
201200 }
202201 } ) ( ) ;
@@ -294,39 +293,6 @@ function deriveMistralToolCallId(id: string, attempt: number): string {
294293 . slice ( 0 , MISTRAL_TOOL_CALL_ID_LENGTH ) ;
295294}
296295
297- function formatMistralError ( error : unknown ) : string {
298- if ( error instanceof Error ) {
299- const sdkError = error as Error & { statusCode ?: unknown ; body ?: unknown } ;
300- const statusCode = typeof sdkError . statusCode === "number" ? sdkError . statusCode : undefined ;
301- const bodyText = typeof sdkError . body === "string" ? sdkError . body . trim ( ) : undefined ;
302- if ( statusCode !== undefined && bodyText ) {
303- return `Mistral API error (${ statusCode } ): ${ truncateErrorText ( bodyText , MAX_MISTRAL_ERROR_BODY_CHARS ) } ` ;
304- }
305- if ( statusCode !== undefined ) {
306- return `Mistral API error (${ statusCode } ): ${ error . message } ` ;
307- }
308- return error . message ;
309- }
310- return safeJsonStringify ( error ) ;
311- }
312-
313- function truncateErrorText ( text : string , maxChars : number ) : string {
314- if ( text . length <= maxChars ) {
315- return text ;
316- }
317- const truncated = truncateUtf16Safe ( text , maxChars ) ;
318- return `${ truncated } ... [truncated ${ text . length - truncated . length } chars]` ;
319- }
320-
321- function safeJsonStringify ( value : unknown ) : string {
322- try {
323- const serialized = JSON . stringify ( value ) ;
324- return serialized === undefined ? String ( value ) : serialized ;
325- } catch {
326- return String ( value ) ;
327- }
328- }
329-
330296function buildChatPayload (
331297 model : Model < "mistral-conversations" > ,
332298 context : Context ,
0 commit comments