@tanstack/ai-mistral 0.1.0

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.
Files changed (38) hide show
  1. package/README.md +93 -0
  2. package/dist/esm/adapters/text.d.ts +106 -0
  3. package/dist/esm/adapters/text.js +741 -0
  4. package/dist/esm/adapters/text.js.map +1 -0
  5. package/dist/esm/index.d.ts +10 -0
  6. package/dist/esm/index.js +9 -0
  7. package/dist/esm/index.js.map +1 -0
  8. package/dist/esm/message-types.d.ts +155 -0
  9. package/dist/esm/model-meta.d.ts +263 -0
  10. package/dist/esm/model-meta.js +50 -0
  11. package/dist/esm/model-meta.js.map +1 -0
  12. package/dist/esm/text/text-provider-options.d.ts +80 -0
  13. package/dist/esm/tools/function-tool.d.ts +13 -0
  14. package/dist/esm/tools/function-tool.js +26 -0
  15. package/dist/esm/tools/function-tool.js.map +1 -0
  16. package/dist/esm/tools/index.d.ts +2 -0
  17. package/dist/esm/tools/tool-converter.d.ts +6 -0
  18. package/dist/esm/tools/tool-converter.js +10 -0
  19. package/dist/esm/tools/tool-converter.js.map +1 -0
  20. package/dist/esm/utils/client.d.ts +24 -0
  21. package/dist/esm/utils/client.js +44 -0
  22. package/dist/esm/utils/client.js.map +1 -0
  23. package/dist/esm/utils/index.d.ts +2 -0
  24. package/dist/esm/utils/schema-converter.d.ts +18 -0
  25. package/dist/esm/utils/schema-converter.js +93 -0
  26. package/dist/esm/utils/schema-converter.js.map +1 -0
  27. package/package.json +60 -0
  28. package/src/adapters/text.ts +1141 -0
  29. package/src/index.ts +33 -0
  30. package/src/message-types.ts +194 -0
  31. package/src/model-meta.ts +285 -0
  32. package/src/text/text-provider-options.ts +103 -0
  33. package/src/tools/function-tool.ts +42 -0
  34. package/src/tools/index.ts +5 -0
  35. package/src/tools/tool-converter.ts +14 -0
  36. package/src/utils/client.ts +70 -0
  37. package/src/utils/index.ts +10 -0
  38. package/src/utils/schema-converter.ts +136 -0
@@ -0,0 +1,80 @@
1
+ import { ChatCompletionMessageParam, ChatCompletionTool, ChatCompletionToolChoiceOption, ResponseFormatJsonObject, ResponseFormatJsonSchema, ResponseFormatText } from '../message-types.js';
2
+ /**
3
+ * Mistral-specific provider options for text/chat models.
4
+ *
5
+ * @see https://docs.mistral.ai/api/
6
+ */
7
+ export interface MistralTextProviderOptions {
8
+ /**
9
+ * Sampling temperature. The default varies by model; lower values make output
10
+ * more deterministic. We recommend altering this OR `top_p`, not both.
11
+ */
12
+ temperature?: number | null;
13
+ /**
14
+ * Nucleus sampling — consider the tokens with `top_p` probability mass.
15
+ */
16
+ top_p?: number | null;
17
+ /**
18
+ * The maximum number of tokens to generate.
19
+ */
20
+ max_tokens?: number | null;
21
+ /**
22
+ * Stop sequences where the API will stop generating further tokens.
23
+ */
24
+ stop?: string | Array<string> | null;
25
+ /**
26
+ * A seed for deterministic sampling. Repeated requests with the same seed
27
+ * and parameters should return the same result (best-effort).
28
+ */
29
+ random_seed?: number | null;
30
+ /**
31
+ * Specifies the format the model must output.
32
+ */
33
+ response_format?: ResponseFormatText | ResponseFormatJsonSchema | ResponseFormatJsonObject | null;
34
+ /**
35
+ * Controls which (if any) tool is called by the model.
36
+ */
37
+ tool_choice?: ChatCompletionToolChoiceOption | null;
38
+ /**
39
+ * Whether parallel tool calls are allowed during tool use.
40
+ */
41
+ parallel_tool_calls?: boolean | null;
42
+ /**
43
+ * Number between -2.0 and 2.0. Positive values penalize tokens based on
44
+ * their frequency in the text so far.
45
+ */
46
+ frequency_penalty?: number | null;
47
+ /**
48
+ * Number between -2.0 and 2.0. Positive values penalize tokens based on
49
+ * whether they appear in the text so far.
50
+ */
51
+ presence_penalty?: number | null;
52
+ /**
53
+ * How many chat completion choices to generate for each input message.
54
+ */
55
+ n?: number | null;
56
+ /**
57
+ * Prediction — used to speed up generation with speculative decoding.
58
+ */
59
+ prediction?: {
60
+ type: 'content';
61
+ content: string;
62
+ } | null;
63
+ /**
64
+ * Safe prompt — enables safety guarding injected into the system prompt.
65
+ */
66
+ safe_prompt?: boolean | null;
67
+ }
68
+ /**
69
+ * Internal options interface used for validation within the adapter.
70
+ */
71
+ export interface InternalTextProviderOptions extends MistralTextProviderOptions {
72
+ messages: Array<ChatCompletionMessageParam>;
73
+ model: string;
74
+ stream?: boolean | null;
75
+ tools?: Array<ChatCompletionTool>;
76
+ }
77
+ /**
78
+ * External provider options (what users pass in).
79
+ */
80
+ export type ExternalTextProviderOptions = MistralTextProviderOptions;
@@ -0,0 +1,13 @@
1
+ import { Tool } from '@tanstack/ai';
2
+ import { ChatCompletionTool } from '../message-types.js';
3
+ export type FunctionTool = ChatCompletionTool;
4
+ /**
5
+ * Converts a standard Tool to Mistral ChatCompletionTool format.
6
+ *
7
+ * Tool schemas are already converted to JSON Schema in the ai layer.
8
+ * We apply Mistral-specific transformations for strict mode:
9
+ * - All properties in required array
10
+ * - Optional fields made nullable
11
+ * - additionalProperties: false
12
+ */
13
+ export declare function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool;
@@ -0,0 +1,26 @@
1
+ import { makeMistralStructuredOutputCompatible } from "../utils/schema-converter.js";
2
+ function convertFunctionToolToAdapterFormat(tool) {
3
+ const baseSchema = tool.inputSchema ?? {
4
+ type: "object",
5
+ properties: {},
6
+ required: []
7
+ };
8
+ const inputSchema = baseSchema.type === "object" && !baseSchema.properties ? { ...baseSchema, properties: {} } : { ...baseSchema };
9
+ const jsonSchema = makeMistralStructuredOutputCompatible(
10
+ inputSchema,
11
+ inputSchema.required || []
12
+ );
13
+ return {
14
+ type: "function",
15
+ function: {
16
+ name: tool.name,
17
+ description: tool.description,
18
+ parameters: jsonSchema,
19
+ strict: true
20
+ }
21
+ };
22
+ }
23
+ export {
24
+ convertFunctionToolToAdapterFormat
25
+ };
26
+ //# sourceMappingURL=function-tool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function-tool.js","sources":["../../../src/tools/function-tool.ts"],"sourcesContent":["import { makeMistralStructuredOutputCompatible } from '../utils/schema-converter'\nimport type { JSONSchema, Tool } from '@tanstack/ai'\nimport type { ChatCompletionTool } from '../message-types'\n\nexport type FunctionTool = ChatCompletionTool\n\n/**\n * Converts a standard Tool to Mistral ChatCompletionTool format.\n *\n * Tool schemas are already converted to JSON Schema in the ai layer.\n * We apply Mistral-specific transformations for strict mode:\n * - All properties in required array\n * - Optional fields made nullable\n * - additionalProperties: false\n */\nexport function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {\n const baseSchema = (tool.inputSchema ?? {\n type: 'object',\n properties: {},\n required: [],\n }) as JSONSchema\n\n const inputSchema: JSONSchema =\n baseSchema.type === 'object' && !baseSchema.properties\n ? { ...baseSchema, properties: {} }\n : { ...baseSchema }\n\n const jsonSchema = makeMistralStructuredOutputCompatible(\n inputSchema,\n inputSchema.required || [],\n )\n\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: jsonSchema,\n strict: true,\n },\n } satisfies FunctionTool\n}\n"],"names":[],"mappings":";AAeO,SAAS,mCAAmC,MAA0B;AAC3E,QAAM,aAAc,KAAK,eAAe;AAAA,IACtC,MAAM;AAAA,IACN,YAAY,CAAA;AAAA,IACZ,UAAU,CAAA;AAAA,EAAC;AAGb,QAAM,cACJ,WAAW,SAAS,YAAY,CAAC,WAAW,aACxC,EAAE,GAAG,YAAY,YAAY,CAAA,EAAC,IAC9B,EAAE,GAAG,WAAA;AAEX,QAAM,aAAa;AAAA,IACjB;AAAA,IACA,YAAY,YAAY,CAAA;AAAA,EAAC;AAG3B,SAAO;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,KAAK;AAAA,MACX,aAAa,KAAK;AAAA,MAClB,YAAY;AAAA,MACZ,QAAQ;AAAA,IAAA;AAAA,EACV;AAEJ;"}
@@ -0,0 +1,2 @@
1
+ export { convertFunctionToolToAdapterFormat, type FunctionTool, } from './function-tool.js';
2
+ export { convertToolsToProviderFormat } from './tool-converter.js';
@@ -0,0 +1,6 @@
1
+ import { FunctionTool } from './function-tool.js';
2
+ import { Tool } from '@tanstack/ai';
3
+ /**
4
+ * Converts an array of standard Tools to Mistral-specific format.
5
+ */
6
+ export declare function convertToolsToProviderFormat(tools: Array<Tool>): Array<FunctionTool>;
@@ -0,0 +1,10 @@
1
+ import { convertFunctionToolToAdapterFormat } from "./function-tool.js";
2
+ function convertToolsToProviderFormat(tools) {
3
+ return tools.map((tool) => {
4
+ return convertFunctionToolToAdapterFormat(tool);
5
+ });
6
+ }
7
+ export {
8
+ convertToolsToProviderFormat
9
+ };
10
+ //# sourceMappingURL=tool-converter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-converter.js","sources":["../../../src/tools/tool-converter.ts"],"sourcesContent":["import { convertFunctionToolToAdapterFormat } from './function-tool'\nimport type { FunctionTool } from './function-tool'\nimport type { Tool } from '@tanstack/ai'\n\n/**\n * Converts an array of standard Tools to Mistral-specific format.\n */\nexport function convertToolsToProviderFormat(\n tools: Array<Tool>,\n): Array<FunctionTool> {\n return tools.map((tool) => {\n return convertFunctionToolToAdapterFormat(tool)\n })\n}\n"],"names":[],"mappings":";AAOO,SAAS,6BACd,OACqB;AACrB,SAAO,MAAM,IAAI,CAAC,SAAS;AACzB,WAAO,mCAAmC,IAAI;AAAA,EAChD,CAAC;AACH;"}
@@ -0,0 +1,24 @@
1
+ import { Mistral } from '@mistralai/mistralai';
2
+ export interface MistralClientConfig {
3
+ /** Mistral API key. */
4
+ apiKey: string;
5
+ /** Optional server URL override. */
6
+ serverURL?: string;
7
+ /** Optional request timeout (ms). */
8
+ timeoutMs?: number;
9
+ /** Optional default headers to include with every request. */
10
+ defaultHeaders?: Record<string, string>;
11
+ }
12
+ /**
13
+ * Creates a Mistral SDK client instance.
14
+ */
15
+ export declare function createMistralClient(config: MistralClientConfig): Mistral;
16
+ /**
17
+ * Gets Mistral API key from environment variables.
18
+ * @throws Error if MISTRAL_API_KEY is not found
19
+ */
20
+ export declare function getMistralApiKeyFromEnv(): string;
21
+ /**
22
+ * Generates a unique ID with a prefix.
23
+ */
24
+ export declare function generateId(prefix: string): string;
@@ -0,0 +1,44 @@
1
+ import { HTTPClient, Mistral } from "@mistralai/mistralai";
2
+ function createMistralClient(config) {
3
+ const { apiKey, serverURL, timeoutMs, defaultHeaders } = config;
4
+ let httpClient;
5
+ if (defaultHeaders && Object.keys(defaultHeaders).length > 0) {
6
+ httpClient = new HTTPClient();
7
+ httpClient.addHook("beforeRequest", (req) => {
8
+ for (const [key, value] of Object.entries(defaultHeaders)) {
9
+ req.headers.set(key, value);
10
+ }
11
+ return req;
12
+ });
13
+ }
14
+ return new Mistral({
15
+ apiKey,
16
+ ...serverURL !== void 0 ? { serverURL } : {},
17
+ ...timeoutMs !== void 0 ? { timeoutMs } : {},
18
+ ...httpClient !== void 0 ? { httpClient } : {}
19
+ });
20
+ }
21
+ function getMistralApiKeyFromEnv() {
22
+ let key;
23
+ if (typeof process !== "undefined" && typeof process.env !== "undefined") {
24
+ key = process.env.MISTRAL_API_KEY;
25
+ } else {
26
+ const g = globalThis;
27
+ key = g.window?.env?.MISTRAL_API_KEY;
28
+ }
29
+ if (!key) {
30
+ throw new Error(
31
+ "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."
32
+ );
33
+ }
34
+ return key;
35
+ }
36
+ function generateId(prefix) {
37
+ return `${prefix}-${crypto.randomUUID()}`;
38
+ }
39
+ export {
40
+ createMistralClient,
41
+ generateId,
42
+ getMistralApiKeyFromEnv
43
+ };
44
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","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 /** Optional server URL override. */\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/**\n * Creates a Mistral SDK client instance.\n */\nexport function createMistralClient(config: MistralClientConfig): Mistral {\n const { apiKey, serverURL, timeoutMs, defaultHeaders } = config\n\n let httpClient: HTTPClient | undefined\n if (defaultHeaders && Object.keys(defaultHeaders).length > 0) {\n httpClient = new HTTPClient()\n httpClient.addHook('beforeRequest', (req) => {\n for (const [key, value] of Object.entries(defaultHeaders)) {\n req.headers.set(key, value)\n }\n return req\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"],"names":[],"mappings":";AAmBO,SAAS,oBAAoB,QAAsC;AACxE,QAAM,EAAE,QAAQ,WAAW,WAAW,mBAAmB;AAEzD,MAAI;AACJ,MAAI,kBAAkB,OAAO,KAAK,cAAc,EAAE,SAAS,GAAG;AAC5D,iBAAa,IAAI,WAAA;AACjB,eAAW,QAAQ,iBAAiB,CAAC,QAAQ;AAC3C,iBAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,cAAc,GAAG;AACzD,YAAI,QAAQ,IAAI,KAAK,KAAK;AAAA,MAC5B;AACA,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAEA,SAAO,IAAI,QAAQ;AAAA,IACjB;AAAA,IACA,GAAI,cAAc,SAAY,EAAE,UAAA,IAAc,CAAA;AAAA,IAC9C,GAAI,cAAc,SAAY,EAAE,UAAA,IAAc,CAAA;AAAA,IAC9C,GAAI,eAAe,SAAY,EAAE,eAAe,CAAA;AAAA,EAAC,CAClD;AACH;AAMO,SAAS,0BAAkC;AAChD,MAAI;AAEJ,MAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,aAAa;AACxE,UAAM,QAAQ,IAAI;AAAA,EACpB,OAAO;AACL,UAAM,IAAI;AACV,UAAM,EAAE,QAAQ,KAAK;AAAA,EACvB;AAEA,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO;AACT;AAKO,SAAS,WAAW,QAAwB;AACjD,SAAO,GAAG,MAAM,IAAI,OAAO,YAAY;AACzC;"}
@@ -0,0 +1,2 @@
1
+ export { createMistralClient, getMistralApiKeyFromEnv, generateId, type MistralClientConfig, } from './client.js';
2
+ export { makeMistralStructuredOutputCompatible, transformNullsToUndefined, } from './schema-converter.js';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Recursively transform null values to undefined in an object.
3
+ *
4
+ * This is needed because Mistral's structured output may require optional
5
+ * fields to be declared nullable. When Mistral returns null for optional
6
+ * fields, we convert them back to undefined to match the original Zod schema.
7
+ */
8
+ export declare function transformNullsToUndefined<T>(obj: T): T;
9
+ /**
10
+ * Transform a JSON schema to be compatible with Mistral's structured output
11
+ * requirements when `strict: true` is used.
12
+ *
13
+ * Mistral (in strict mode) requires:
14
+ * - All properties must be in the `required` array
15
+ * - Optional fields should have null added to their type union
16
+ * - additionalProperties must be false for objects
17
+ */
18
+ export declare function makeMistralStructuredOutputCompatible(schema: Record<string, any>, originalRequired?: Array<string>): Record<string, any>;
@@ -0,0 +1,93 @@
1
+ function transformNullsToUndefined(obj) {
2
+ if (obj === null) {
3
+ return void 0;
4
+ }
5
+ if (Array.isArray(obj)) {
6
+ return obj.map((item) => transformNullsToUndefined(item));
7
+ }
8
+ if (typeof obj === "object" && Object.getPrototypeOf(obj) === Object.prototype) {
9
+ const result = {};
10
+ for (const [key, value] of Object.entries(obj)) {
11
+ result[key] = transformNullsToUndefined(value);
12
+ }
13
+ return result;
14
+ }
15
+ return obj;
16
+ }
17
+ function makeMistralStructuredOutputCompatible(schema, originalRequired = []) {
18
+ const result = { ...schema };
19
+ if (result.type === "object") {
20
+ if (!result.properties) {
21
+ result.properties = {};
22
+ }
23
+ const properties = { ...result.properties };
24
+ const allPropertyNames = Object.keys(properties);
25
+ for (const propName of allPropertyNames) {
26
+ const prop = properties[propName];
27
+ const wasOptional = !originalRequired.includes(propName);
28
+ if (prop.type === "object" && prop.properties) {
29
+ const converted = makeMistralStructuredOutputCompatible(
30
+ prop,
31
+ prop.required || []
32
+ );
33
+ if (wasOptional) {
34
+ properties[propName] = {
35
+ ...converted,
36
+ type: Array.isArray(converted.type) ? converted.type.includes("null") ? converted.type : [...converted.type, "null"] : [converted.type, "null"]
37
+ };
38
+ } else {
39
+ properties[propName] = converted;
40
+ }
41
+ } else if (prop.type === "array" && prop.items) {
42
+ const converted = {
43
+ ...prop,
44
+ items: makeMistralStructuredOutputCompatible(
45
+ prop.items,
46
+ prop.items.required || []
47
+ )
48
+ };
49
+ if (wasOptional) {
50
+ properties[propName] = {
51
+ ...converted,
52
+ type: Array.isArray(converted.type) ? converted.type.includes("null") ? converted.type : [...converted.type, "null"] : [converted.type, "null"]
53
+ };
54
+ } else {
55
+ properties[propName] = converted;
56
+ }
57
+ } else if (wasOptional) {
58
+ if (prop.type && !Array.isArray(prop.type)) {
59
+ properties[propName] = {
60
+ ...prop,
61
+ type: [prop.type, "null"]
62
+ };
63
+ } else if (Array.isArray(prop.type) && !prop.type.includes("null")) {
64
+ properties[propName] = {
65
+ ...prop,
66
+ type: [...prop.type, "null"]
67
+ };
68
+ } else if (!prop.type) {
69
+ properties[propName] = { anyOf: [prop, { type: "null" }] };
70
+ }
71
+ }
72
+ }
73
+ result.properties = properties;
74
+ if (allPropertyNames.length > 0) {
75
+ result.required = allPropertyNames;
76
+ } else {
77
+ delete result.required;
78
+ }
79
+ result.additionalProperties = false;
80
+ }
81
+ if (result.type === "array" && result.items) {
82
+ result.items = makeMistralStructuredOutputCompatible(
83
+ result.items,
84
+ result.items.required || []
85
+ );
86
+ }
87
+ return result;
88
+ }
89
+ export {
90
+ makeMistralStructuredOutputCompatible,
91
+ transformNullsToUndefined
92
+ };
93
+ //# sourceMappingURL=schema-converter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-converter.js","sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * Recursively transform null values to undefined in an object.\n *\n * This is needed because Mistral's structured output may require optional\n * fields to be declared nullable. When Mistral returns null for optional\n * fields, we convert them back to undefined to match the original Zod schema.\n */\nexport function transformNullsToUndefined<T>(obj: T): T {\n if (obj === null) {\n // eslint-disable-next-line no-restricted-syntax -- generic T has no structural overlap with undefined; null→undefined conversion is this function's documented contract\n return undefined as unknown as T\n }\n\n if (Array.isArray(obj)) {\n // Preserve array length and indices — converting null elements to\n // undefined slots rather than dropping them. `Array<T | null>` schemas\n // depend on positional alignment.\n // eslint-disable-next-line no-restricted-syntax -- the mapped array (unknown[]) has no structural overlap with generic T; positional null→undefined transform preserves the array shape T describes\n return obj.map((item) => transformNullsToUndefined(item)) as unknown as T\n }\n\n if (\n typeof obj === 'object' &&\n Object.getPrototypeOf(obj) === Object.prototype\n ) {\n // Preserve every key — `null` values become `undefined` values, but the\n // key itself is not removed. Schemas distinguishing absent vs explicit\n // null rely on this.\n const result: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {\n result[key] = transformNullsToUndefined(value)\n }\n return result as T\n }\n\n return obj\n}\n\n/**\n * Transform a JSON schema to be compatible with Mistral's structured output\n * requirements when `strict: true` is used.\n *\n * Mistral (in strict mode) requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n */\nexport function makeMistralStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired: Array<string> = [],\n): Record<string, any> {\n const result = { ...schema }\n\n if (result.type === 'object') {\n if (!result.properties) {\n result.properties = {}\n }\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n const prop = properties[propName]\n const wasOptional = !originalRequired.includes(propName)\n\n if (prop.type === 'object' && prop.properties) {\n const converted = makeMistralStructuredOutputCompatible(\n prop,\n prop.required || [],\n )\n if (wasOptional) {\n properties[propName] = {\n ...converted,\n type: Array.isArray(converted.type)\n ? converted.type.includes('null')\n ? converted.type\n : [...converted.type, 'null']\n : [converted.type, 'null'],\n }\n } else {\n properties[propName] = converted\n }\n } else if (prop.type === 'array' && prop.items) {\n const converted = {\n ...prop,\n items: makeMistralStructuredOutputCompatible(\n prop.items,\n prop.items.required || [],\n ),\n }\n if (wasOptional) {\n properties[propName] = {\n ...converted,\n type: Array.isArray(converted.type)\n ? converted.type.includes('null')\n ? converted.type\n : [...converted.type, 'null']\n : [converted.type, 'null'],\n }\n } else {\n properties[propName] = converted\n }\n } else if (wasOptional) {\n if (prop.type && !Array.isArray(prop.type)) {\n properties[propName] = {\n ...prop,\n type: [prop.type, 'null'],\n }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n properties[propName] = {\n ...prop,\n type: [...prop.type, 'null'],\n }\n } else if (!prop.type) {\n properties[propName] = { anyOf: [prop, { type: 'null' }] }\n }\n }\n }\n\n result.properties = properties\n if (allPropertyNames.length > 0) {\n result.required = allPropertyNames\n } else {\n delete result.required\n }\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = makeMistralStructuredOutputCompatible(\n result.items,\n result.items.required || [],\n )\n }\n\n return result\n}\n"],"names":[],"mappings":"AAOO,SAAS,0BAA6B,KAAW;AACtD,MAAI,QAAQ,MAAM;AA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
+ "name": "@tanstack/ai-mistral",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Mistral adapter for TanStack AI",
6
+ "author": "",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/TanStack/ai.git",
11
+ "directory": "packages/ai-mistral"
12
+ },
13
+ "module": "./dist/esm/index.js",
14
+ "types": "./dist/esm/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/esm/index.d.ts",
18
+ "import": "./dist/esm/index.js"
19
+ },
20
+ "./adapters/text": {
21
+ "types": "./dist/esm/adapters/text.d.ts",
22
+ "import": "./dist/esm/adapters/text.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "src"
28
+ ],
29
+ "scripts": {
30
+ "build": "vite build",
31
+ "clean": "premove ./build ./dist",
32
+ "lint:fix": "eslint ./src --fix",
33
+ "test:build": "publint --strict",
34
+ "test:eslint": "eslint ./src",
35
+ "test:lib": "vitest run",
36
+ "test:lib:dev": "pnpm test:lib --watch",
37
+ "test:types": "tsc"
38
+ },
39
+ "keywords": [
40
+ "ai",
41
+ "mistral",
42
+ "tanstack",
43
+ "adapter"
44
+ ],
45
+ "devDependencies": {
46
+ "@tanstack/ai": "workspace:*",
47
+ "@vitest/coverage-v8": "4.0.14",
48
+ "vite": "^7.3.3"
49
+ },
50
+ "peerDependencies": {
51
+ "@tanstack/ai": "workspace:^",
52
+ "zod": "^4.0.0"
53
+ },
54
+ "dependencies": {
55
+ "@mistralai/mistralai": "2.2.0"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }