@tanstack/ai-mistral 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- import { Mistral } from '@mistralai/mistralai';
2
1
  export interface MistralClientConfig {
3
2
  /** Mistral API key. */
4
3
  apiKey: string;
@@ -27,10 +26,6 @@ export interface MistralClientConfig {
27
26
  /** Optional model id sent on the wire. Vertex uses publisher model ids. */
28
27
  requestModel?: string;
29
28
  }
30
- /**
31
- * Creates a Mistral SDK client instance. A `fetcher` replaces the SDK's fetch.
32
- */
33
- export declare function createMistralClient(config: MistralClientConfig, fetcher?: typeof fetch): Mistral;
34
29
  /**
35
30
  * Gets Mistral API key from environment variables.
36
31
  * @throws Error if MISTRAL_API_KEY is not found
@@ -1,36 +1,4 @@
1
- import { HTTPClient, Mistral } from "@mistralai/mistralai";
2
- //#region src/utils/client.ts
3
- /**
4
- * Creates a Mistral SDK client instance. A `fetcher` replaces the SDK's fetch.
5
- */
6
- function createMistralClient(config, fetcher) {
7
- const { apiKey, baseURL, timeoutMs, defaultHeaders, getAccessToken, resolveRequestUrl } = config;
8
- const serverURL = baseURL ?? config.serverURL;
9
- const needsHook = defaultHeaders !== void 0 && Object.keys(defaultHeaders).length > 0 || getAccessToken !== void 0 || resolveRequestUrl !== void 0;
10
- let httpClient;
11
- if (needsHook || fetcher) httpClient = new HTTPClient({ fetcher: fetcher && (async (input) => {
12
- const request = new Request(input);
13
- return fetcher(request.url, {
14
- method: request.method,
15
- headers: request.headers,
16
- body: request.body && await request.arrayBuffer(),
17
- signal: request.signal
18
- });
19
- }) });
20
- if (httpClient && needsHook) httpClient.addHook("beforeRequest", async (req) => {
21
- const nextUrl = resolveRequestUrl === void 0 ? req.url : resolveRequestUrl(false);
22
- const next = new Request(nextUrl, req);
23
- if (defaultHeaders) for (const [key, value] of Object.entries(defaultHeaders)) next.headers.set(key, value);
24
- if (getAccessToken !== void 0) next.headers.set("Authorization", `Bearer ${await getAccessToken()}`);
25
- return next;
26
- });
27
- return new Mistral({
28
- apiKey,
29
- ...serverURL !== void 0 ? { serverURL } : {},
30
- ...timeoutMs !== void 0 ? { timeoutMs } : {},
31
- ...httpClient !== void 0 ? { httpClient } : {}
32
- });
33
- }
1
+ // src/utils/client.ts
34
2
  /**
35
3
  * Gets Mistral API key from environment variables.
36
4
  * @throws Error if MISTRAL_API_KEY is not found
@@ -48,7 +16,6 @@ function getMistralApiKeyFromEnv() {
48
16
  function generateId(prefix) {
49
17
  return `${prefix}-${crypto.randomUUID()}`;
50
18
  }
51
- //#endregion
52
- export { createMistralClient, generateId, getMistralApiKeyFromEnv };
19
+ export { generateId, getMistralApiKeyFromEnv };
53
20
 
54
21
  //# sourceMappingURL=client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["import { HTTPClient, Mistral } from '@mistralai/mistralai'\n\nexport interface MistralClientConfig {\n /** Mistral API key. */\n apiKey: string\n\n /**\n * Base URL for every request. Same option name as the other adapters, so a\n * gateway config can be spread into any of them. Wins over `serverURL` when\n * both are set.\n */\n baseURL?: string\n\n /** Alias of `baseURL`. */\n serverURL?: string\n\n /** Optional request timeout (ms). */\n timeoutMs?: number\n\n /** Optional default headers to include with every request. */\n defaultHeaders?: Record<string, string>\n\n /**\n * Optional Google / Vertex access token. When set, it replaces\n * `apiKey` on the Authorization header.\n */\n getAccessToken?: () => Promise<string>\n\n /**\n * Optional chat completions URL. Vertex uses this for\n * `:rawPredict` and `:streamRawPredict`.\n */\n resolveRequestUrl?: (stream: boolean) => string\n\n /** Optional model id sent on the wire. Vertex uses publisher model ids. */\n requestModel?: string\n}\n\n/**\n * Creates a Mistral SDK client instance. A `fetcher` replaces the SDK's fetch.\n */\nexport function createMistralClient(\n config: MistralClientConfig,\n fetcher?: typeof fetch,\n): Mistral {\n const {\n apiKey,\n baseURL,\n timeoutMs,\n defaultHeaders,\n getAccessToken,\n resolveRequestUrl,\n } = config\n const serverURL = baseURL ?? config.serverURL\n\n const needsHook =\n (defaultHeaders !== undefined && Object.keys(defaultHeaders).length > 0) ||\n getAccessToken !== undefined ||\n resolveRequestUrl !== undefined\n\n let httpClient: HTTPClient | undefined\n if (needsHook || fetcher) {\n httpClient = new HTTPClient({\n // The SDK sends a Request. Send a URL and an init, as a plain fetch\n // call does, so a wrapper that reads `init.headers` keeps them.\n fetcher:\n fetcher &&\n (async (input) => {\n const request = new Request(input)\n return fetcher(request.url, {\n method: request.method,\n headers: request.headers,\n body: request.body && (await request.arrayBuffer()),\n signal: request.signal,\n })\n }),\n })\n }\n if (httpClient && needsHook) {\n httpClient.addHook('beforeRequest', async (req) => {\n const nextUrl =\n resolveRequestUrl === undefined ? req.url : resolveRequestUrl(false)\n const next = new Request(nextUrl, req)\n if (defaultHeaders) {\n for (const [key, value] of Object.entries(defaultHeaders)) {\n next.headers.set(key, value)\n }\n }\n if (getAccessToken !== undefined) {\n next.headers.set('Authorization', `Bearer ${await getAccessToken()}`)\n }\n return next\n })\n }\n\n return new Mistral({\n apiKey,\n ...(serverURL !== undefined ? { serverURL } : {}),\n ...(timeoutMs !== undefined ? { timeoutMs } : {}),\n ...(httpClient !== undefined ? { httpClient } : {}),\n })\n}\n\n/**\n * Gets Mistral API key from environment variables.\n * @throws Error if MISTRAL_API_KEY is not found\n */\nexport function getMistralApiKeyFromEnv(): string {\n let key: string | undefined\n\n if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {\n key = process.env.MISTRAL_API_KEY\n } else {\n const g = globalThis as { window?: { env?: Record<string, string> } }\n key = g.window?.env?.MISTRAL_API_KEY\n }\n\n if (!key) {\n throw new Error(\n 'MISTRAL_API_KEY is required. In Node.js set it as an environment variable; in browser environments inject it via window.env.MISTRAL_API_KEY or use the factory function with an explicit API key.',\n )\n }\n\n return key\n}\n\n/**\n * Generates a unique ID with a prefix.\n */\nexport function generateId(prefix: string): string {\n return `${prefix}-${crypto.randomUUID()}`\n}\n"],"mappings":";;;;;AAyCA,SAAgB,oBACd,QACA,SACS;CACT,MAAM,EACJ,QACA,SACA,WACA,gBACA,gBACA,sBACE;CACJ,MAAM,YAAY,WAAW,OAAO;CAEpC,MAAM,YACH,mBAAmB,KAAA,KAAa,OAAO,KAAK,cAAc,CAAC,CAAC,SAAS,KACtE,mBAAmB,KAAA,KACnB,sBAAsB,KAAA;CAExB,IAAI;CACJ,IAAI,aAAa,SACf,aAAa,IAAI,WAAW,EAG1B,SACE,YACC,OAAO,UAAU;EAChB,MAAM,UAAU,IAAI,QAAQ,KAAK;EACjC,OAAO,QAAQ,QAAQ,KAAK;GAC1B,QAAQ,QAAQ;GAChB,SAAS,QAAQ;GACjB,MAAM,QAAQ,QAAS,MAAM,QAAQ,YAAY;GACjD,QAAQ,QAAQ;EAClB,CAAC;CACH,GACJ,CAAC;CAEH,IAAI,cAAc,WAChB,WAAW,QAAQ,iBAAiB,OAAO,QAAQ;EACjD,MAAM,UACJ,sBAAsB,KAAA,IAAY,IAAI,MAAM,kBAAkB,KAAK;EACrE,MAAM,OAAO,IAAI,QAAQ,SAAS,GAAG;EACrC,IAAI,gBACF,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,cAAc,GACtD,KAAK,QAAQ,IAAI,KAAK,KAAK;EAG/B,IAAI,mBAAmB,KAAA,GACrB,KAAK,QAAQ,IAAI,iBAAiB,UAAU,MAAM,eAAe,GAAG;EAEtE,OAAO;CACT,CAAC;CAGH,OAAO,IAAI,QAAQ;EACjB;EACA,GAAI,cAAc,KAAA,IAAY,EAAE,UAAU,IAAI,CAAC;EAC/C,GAAI,cAAc,KAAA,IAAY,EAAE,UAAU,IAAI,CAAC;EAC/C,GAAI,eAAe,KAAA,IAAY,EAAE,WAAW,IAAI,CAAC;CACnD,CAAC;AACH;;;;;AAMA,SAAgB,0BAAkC;CAChD,IAAI;CAEJ,IAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAC3D,MAAM,QAAQ,IAAI;MAGlB,MAAM,WAAE,QAAQ,KAAK;CAGvB,IAAI,CAAC,KACH,MAAM,IAAI,MACR,mMACF;CAGF,OAAO;AACT;;;;AAKA,SAAgB,WAAW,QAAwB;CACjD,OAAO,GAAG,OAAO,GAAG,OAAO,WAAW;AACxC"}
1
+ {"version":3,"file":"client.js","names":["g"],"sources":["../../../src/utils/client.ts"],"sourcesContent":["export interface MistralClientConfig {\n /** Mistral API key. */\n apiKey: string\n\n /**\n * Base URL for every request. Same option name as the other adapters, so a\n * gateway config can be spread into any of them. Wins over `serverURL` when\n * both are set.\n */\n baseURL?: string\n\n /** Alias of `baseURL`. */\n serverURL?: string\n\n /** Optional request timeout (ms). */\n timeoutMs?: number\n\n /** Optional default headers to include with every request. */\n defaultHeaders?: Record<string, string>\n\n /**\n * Optional Google / Vertex access token. When set, it replaces\n * `apiKey` on the Authorization header.\n */\n getAccessToken?: () => Promise<string>\n\n /**\n * Optional chat completions URL. Vertex uses this for\n * `:rawPredict` and `:streamRawPredict`.\n */\n resolveRequestUrl?: (stream: boolean) => string\n\n /** Optional model id sent on the wire. Vertex uses publisher model ids. */\n requestModel?: string\n}\n\n/**\n * Gets Mistral API key from environment variables.\n * @throws Error if MISTRAL_API_KEY is not found\n */\nexport function getMistralApiKeyFromEnv(): string {\n let key: string | undefined\n\n if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {\n key = process.env.MISTRAL_API_KEY\n } else {\n const g = globalThis as { window?: { env?: Record<string, string> } }\n key = g.window?.env?.MISTRAL_API_KEY\n }\n\n if (!key) {\n throw new Error(\n 'MISTRAL_API_KEY is required. In Node.js set it as an environment variable; in browser environments inject it via window.env.MISTRAL_API_KEY or use the factory function with an explicit API key.',\n )\n }\n\n return key\n}\n\n/**\n * Generates a unique ID with a prefix.\n */\nexport function generateId(prefix: string): string {\n return `${prefix}-${crypto.randomUUID()}`\n}\n"],"mappings":";;;;;AAwCA,SAAgB,0BAAkC;CAChD,IAAI;CAEJ,IAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAC3D,MAAM,QAAQ,IAAI;MAGlB,MAAMA,WAAE,QAAQ,KAAK;CAGvB,IAAI,CAAC,KACH,MAAM,IAAI,MACR,mMACF;CAGF,OAAO;AACT;;;;AAKA,SAAgB,WAAW,QAAwB;CACjD,OAAO,GAAG,OAAO,GAAG,OAAO,WAAW;AACxC"}
@@ -1,4 +1,4 @@
1
- //#region src/utils/schema-converter.ts
1
+ // src/utils/schema-converter.ts
2
2
  /**
3
3
  * Convert a schema for Mistral strict mode and record how to invert it.
4
4
  *
@@ -194,7 +194,6 @@ function coerceMistralStrictSchema(schema, originalRequired) {
194
194
  hasUntrackableAnyOfWidening
195
195
  };
196
196
  }
197
- //#endregion
198
197
  export { makeMistralStructuredOutputCompatibleWithMap };
199
198
 
200
199
  //# sourceMappingURL=schema-converter.js.map
@@ -1,6 +1,6 @@
1
1
  import { makeMistralStructuredOutputCompatibleWithMap } from "./schema-converter.js";
2
2
  import { undoNullWidening } from "@tanstack/ai-utils";
3
- //#region src/utils/tool-input-normalizer.ts
3
+ // src/utils/tool-input-normalizer.ts
4
4
  /**
5
5
  * Per-request inverse of Mistral optional-null widening. Recomputes maps from
6
6
  * each tool's input schema; duplicate names and non-strict schemas are left
@@ -28,7 +28,6 @@ function createToolInputNormalizer(tools) {
28
28
  }
29
29
  return (toolName, input) => undoNullWidening(input, maps.get(toolName));
30
30
  }
31
- //#endregion
32
31
  export { createToolInputNormalizer };
33
32
 
34
33
  //# sourceMappingURL=tool-input-normalizer.js.map
@@ -1,4 +1,4 @@
1
- //#region src/vertex/auth.ts
1
+ // src/vertex/auth.ts
2
2
  var MistralVertexAuthError = class extends Error {
3
3
  constructor(message, options) {
4
4
  super(message, options);
@@ -56,7 +56,6 @@ async function resolveMistralVertexAccessToken(config) {
56
56
  throw new MistralVertexAuthError("Mistral Vertex could not load a Google access token from Application Default Credentials.", { cause: error });
57
57
  }
58
58
  }
59
- //#endregion
60
59
  export { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject };
61
60
 
62
61
  //# sourceMappingURL=auth.js.map
@@ -1,7 +1,7 @@
1
- import { MISTRAL_VERTEX_CHAT_MODELS } from "../model-meta.js";
2
1
  import { MistralTextAdapter } from "../adapters/text.js";
3
2
  import { MistralVertexAuthError, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject } from "./auth.js";
4
- //#region src/vertex/index.ts
3
+ import { MISTRAL_VERTEX_CHAT_MODELS } from "../model-meta.js";
4
+ // src/vertex/index.ts
5
5
  /**
6
6
  * Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
7
7
  *
@@ -20,7 +20,6 @@ function mistralVertexText(model, config = {}) {
20
20
  defaultHeaders: config.defaultHeaders
21
21
  }, model);
22
22
  }
23
- //#endregion
24
23
  export { MISTRAL_VERTEX_CHAT_MODELS, MistralVertexAuthError, mistralVertexText, resolveMistralVertexAccessToken, resolveMistralVertexLocation, resolveMistralVertexModelUrl, resolveMistralVertexProject };
25
24
 
26
25
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-mistral",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "type": "module",
5
5
  "description": "Mistral adapter for TanStack AI",
6
6
  "author": "",
@@ -34,6 +34,7 @@
34
34
  "import": "./dist/esm/vertex/index.js"
35
35
  }
36
36
  },
37
+ "sideEffects": false,
37
38
  "files": [
38
39
  "dist",
39
40
  "src"
@@ -45,14 +46,14 @@
45
46
  "adapter"
46
47
  ],
47
48
  "devDependencies": {
48
- "@vitest/coverage-v8": "4.1.10",
49
+ "@vitest/coverage-v8": "4.1.12",
49
50
  "google-auth-library": "^10.5.0",
50
- "vite": "^8.2.1",
51
- "@tanstack/ai": "1.1.0"
51
+ "vite": "^8.3.8",
52
+ "@tanstack/ai": "1.5.0"
52
53
  },
53
54
  "peerDependencies": {
54
55
  "google-auth-library": "^10.5.0",
55
- "@tanstack/ai": "^1.1.0"
56
+ "@tanstack/ai": "^1.5.0"
56
57
  },
57
58
  "peerDependenciesMeta": {
58
59
  "google-auth-library": {
@@ -61,7 +62,7 @@
61
62
  },
62
63
  "dependencies": {
63
64
  "@mistralai/mistralai": "2.2.0",
64
- "@tanstack/ai-utils": "^1.0.1"
65
+ "@tanstack/ai-utils": "^1.1.0"
65
66
  },
66
67
  "scripts": {
67
68
  "build": "vite build",
@@ -1,17 +1,14 @@
1
1
  import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'
2
2
  import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
3
3
  import { requireTextOnlyEmbeddingInput } from '@tanstack/ai'
4
- import {
5
- createMistralClient,
6
- generateId,
7
- getMistralApiKeyFromEnv,
8
- } from '../utils/client'
4
+ import { createMistralClient } from '../client/index'
5
+ import { generateId, getMistralApiKeyFromEnv } from '../utils/client'
9
6
  import type {
10
7
  EmbeddingOptions,
11
8
  EmbeddingResult,
12
9
  TokenUsage,
13
10
  } from '@tanstack/ai'
14
- import type { Mistral } from '@mistralai/mistralai'
11
+ import type { MistralClient } from '../client/index'
15
12
  import type { EmbeddingRequest } from '@mistralai/mistralai/models/components'
16
13
  import type {
17
14
  MistralEmbeddingModel,
@@ -48,7 +45,7 @@ export class MistralEmbeddingAdapter<
48
45
  > {
49
46
  readonly name = 'mistral' as const
50
47
 
51
- protected client: Mistral
48
+ protected client: MistralClient
52
49
 
53
50
  constructor(config: MistralEmbeddingConfig, model: TModel) {
54
51
  super(model, {})
@@ -1,12 +1,13 @@
1
- import { isFileSource, unsupportedFileSourceError } from '@tanstack/ai'
1
+ import {
2
+ CONTEXT_OVERFLOW_CODE,
3
+ isFileSource,
4
+ unsupportedFileSourceError,
5
+ } from '@tanstack/ai'
2
6
  import { BaseTextAdapter } from '@tanstack/ai/adapters'
3
7
  import { undoNullWidening } from '@tanstack/ai-utils'
4
8
  import { convertToolsToProviderFormat } from '../tools/tool-converter'
5
- import {
6
- createMistralClient,
7
- generateId,
8
- getMistralApiKeyFromEnv,
9
- } from '../utils/client'
9
+ import { createMistralClient } from '../client/index'
10
+ import { generateId, getMistralApiKeyFromEnv } from '../utils/client'
10
11
  import { makeMistralStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
11
12
  import { createToolInputNormalizer } from '../utils/tool-input-normalizer'
12
13
  import {
@@ -43,7 +44,6 @@ import type {
43
44
  StructuredOutputOptions,
44
45
  StructuredOutputResult,
45
46
  } from '@tanstack/ai/adapters'
46
- import type { Mistral } from '@mistralai/mistralai'
47
47
  import type {
48
48
  AssistantMessage,
49
49
  ChatCompletionStreamRequest,
@@ -59,6 +59,7 @@ import type {
59
59
  MistralImageMetadata,
60
60
  MistralMessageMetadataByModality,
61
61
  } from '../message-types'
62
+ import type { MistralClient } from '../client/index'
62
63
  import type { MistralClientConfig } from '../utils/client'
63
64
  import type { ToolInputNormalizer } from '../utils/tool-input-normalizer'
64
65
 
@@ -315,14 +316,14 @@ export class MistralTextAdapter<
315
316
  override readonly provider: string
316
317
  override readonly inputModalities = MISTRAL_MODEL_INPUT_MODALITIES[this.model]
317
318
 
318
- private readonly client: Mistral
319
+ private readonly client: MistralClient
319
320
  private readonly rawConfig: MistralClientConfig
320
321
 
321
322
  constructor(config: MistralTextConfig, model: TModel) {
322
323
  super(config, model)
323
- // The SDK client is retained for `structuredOutput` (non-streaming). The
324
- // streaming path bypasses the SDK and uses `fetchRawMistralStream` because
325
- // the SDK's Zod schemas reject partial tool-call argument deltas.
324
+ // The client serves `structuredOutput` (non-streaming). The streaming
325
+ // path uses `fetchRawMistralStream`, which reads partial tool-call
326
+ // argument deltas as they come.
326
327
  this.client = createMistralClient(config)
327
328
  this.rawConfig = config
328
329
  this.provider =
@@ -500,7 +501,17 @@ export class MistralTextAdapter<
500
501
  // A response cut off at the output cap is a truncated JSON document;
501
502
  // report it before the parse error (issue #1426). The errors carry the
502
503
  // usage, because the provider already billed the tokens.
503
- if (response.choices[0]?.finishReason === 'length') {
504
+ const structuredFinishReason = response.choices[0]?.finishReason
505
+ if (structuredFinishReason === 'model_length') {
506
+ throw Object.assign(
507
+ new Error(
508
+ 'mistral.structuredOutput: the response was cut off because the model reached the end of its context window (finish_reason=model_length); shorten the prompt or use a model with a larger context window',
509
+ ),
510
+ { code: CONTEXT_OVERFLOW_CODE },
511
+ usage && { usage },
512
+ )
513
+ }
514
+ if (structuredFinishReason === 'length') {
504
515
  throw Object.assign(
505
516
  new Error(
506
517
  'mistral.structuredOutput: the response was cut off because the maximum token limit was reached (finish_reason=length); raise modelOptions.max_tokens',
@@ -782,6 +793,15 @@ export class MistralTextAdapter<
782
793
  }
783
794
 
784
795
  const finishReason = choice.finish_reason
796
+ if (finishReason === 'model_length') {
797
+ // Not max_tokens. code context_overflow is what withCompaction retries.
798
+ throw Object.assign(
799
+ new Error(
800
+ 'The response was cut off because the model reached the end of its context window.',
801
+ ),
802
+ { code: CONTEXT_OVERFLOW_CODE },
803
+ )
804
+ }
785
805
  if (finishReason) {
786
806
  if (finishReason === 'tool_calls' || toolCallsInProgress.size > 0) {
787
807
  for (const [, toolCall] of toolCallsInProgress) {
@@ -794,10 +814,16 @@ export class MistralTextAdapter<
794
814
  continue
795
815
  }
796
816
 
797
- const parsedInput = parseToolCallInput(
798
- toolCall,
799
- normalizeToolInput,
800
- )
817
+ // Complete JSON is normalized. Incomplete JSON throws, except
818
+ // on `length`, where the raw args stay and `input` is omitted.
819
+ let parsedInput: unknown
820
+ let parsed = false
821
+ try {
822
+ parsedInput = parseToolCallInput(toolCall, normalizeToolInput)
823
+ parsed = true
824
+ } catch (error) {
825
+ if (finishReason !== 'length') throw error
826
+ }
801
827
 
802
828
  toolCall.ended = true
803
829
  hasEmittedToolCall = true
@@ -808,16 +834,16 @@ export class MistralTextAdapter<
808
834
  model: chunkModel,
809
835
  timestamp,
810
836
  args: toolCall.arguments,
811
- input: parsedInput,
837
+ ...(parsed ? { input: parsedInput } : {}),
812
838
  })
813
839
  }
814
840
  }
815
841
 
816
842
  const computedFinishReason =
817
- finishReason === 'tool_calls' || hasEmittedToolCall
818
- ? 'tool_calls'
819
- : finishReason === 'length'
820
- ? 'length'
843
+ finishReason === 'length'
844
+ ? 'length'
845
+ : finishReason === 'tool_calls' || hasEmittedToolCall
846
+ ? 'tool_calls'
821
847
  : 'stop'
822
848
 
823
849
  // If the run finished while reasoning was still open (no text or