@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,70 @@
1
+ import { HTTPClient, Mistral } from '@mistralai/mistralai'
2
+
3
+ export interface MistralClientConfig {
4
+ /** Mistral API key. */
5
+ apiKey: string
6
+
7
+ /** Optional server URL override. */
8
+ serverURL?: string
9
+
10
+ /** Optional request timeout (ms). */
11
+ timeoutMs?: number
12
+
13
+ /** Optional default headers to include with every request. */
14
+ defaultHeaders?: Record<string, string>
15
+ }
16
+
17
+ /**
18
+ * Creates a Mistral SDK client instance.
19
+ */
20
+ export function createMistralClient(config: MistralClientConfig): Mistral {
21
+ const { apiKey, serverURL, timeoutMs, defaultHeaders } = config
22
+
23
+ let httpClient: HTTPClient | undefined
24
+ if (defaultHeaders && Object.keys(defaultHeaders).length > 0) {
25
+ httpClient = new HTTPClient()
26
+ httpClient.addHook('beforeRequest', (req) => {
27
+ for (const [key, value] of Object.entries(defaultHeaders)) {
28
+ req.headers.set(key, value)
29
+ }
30
+ return req
31
+ })
32
+ }
33
+
34
+ return new Mistral({
35
+ apiKey,
36
+ ...(serverURL !== undefined ? { serverURL } : {}),
37
+ ...(timeoutMs !== undefined ? { timeoutMs } : {}),
38
+ ...(httpClient !== undefined ? { httpClient } : {}),
39
+ })
40
+ }
41
+
42
+ /**
43
+ * Gets Mistral API key from environment variables.
44
+ * @throws Error if MISTRAL_API_KEY is not found
45
+ */
46
+ export function getMistralApiKeyFromEnv(): string {
47
+ let key: string | undefined
48
+
49
+ if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {
50
+ key = process.env.MISTRAL_API_KEY
51
+ } else {
52
+ const g = globalThis as { window?: { env?: Record<string, string> } }
53
+ key = g.window?.env?.MISTRAL_API_KEY
54
+ }
55
+
56
+ if (!key) {
57
+ throw new Error(
58
+ '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.',
59
+ )
60
+ }
61
+
62
+ return key
63
+ }
64
+
65
+ /**
66
+ * Generates a unique ID with a prefix.
67
+ */
68
+ export function generateId(prefix: string): string {
69
+ return `${prefix}-${crypto.randomUUID()}`
70
+ }
@@ -0,0 +1,10 @@
1
+ export {
2
+ createMistralClient,
3
+ getMistralApiKeyFromEnv,
4
+ generateId,
5
+ type MistralClientConfig,
6
+ } from './client'
7
+ export {
8
+ makeMistralStructuredOutputCompatible,
9
+ transformNullsToUndefined,
10
+ } from './schema-converter'
@@ -0,0 +1,136 @@
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 function transformNullsToUndefined<T>(obj: T): T {
9
+ if (obj === null) {
10
+ // eslint-disable-next-line no-restricted-syntax -- generic T has no structural overlap with undefined; null→undefined conversion is this function's documented contract
11
+ return undefined as unknown as T
12
+ }
13
+
14
+ if (Array.isArray(obj)) {
15
+ // Preserve array length and indices — converting null elements to
16
+ // undefined slots rather than dropping them. `Array<T | null>` schemas
17
+ // depend on positional alignment.
18
+ // 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
19
+ return obj.map((item) => transformNullsToUndefined(item)) as unknown as T
20
+ }
21
+
22
+ if (
23
+ typeof obj === 'object' &&
24
+ Object.getPrototypeOf(obj) === Object.prototype
25
+ ) {
26
+ // Preserve every key — `null` values become `undefined` values, but the
27
+ // key itself is not removed. Schemas distinguishing absent vs explicit
28
+ // null rely on this.
29
+ const result: Record<string, unknown> = {}
30
+ for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
31
+ result[key] = transformNullsToUndefined(value)
32
+ }
33
+ return result as T
34
+ }
35
+
36
+ return obj
37
+ }
38
+
39
+ /**
40
+ * Transform a JSON schema to be compatible with Mistral's structured output
41
+ * requirements when `strict: true` is used.
42
+ *
43
+ * Mistral (in strict mode) requires:
44
+ * - All properties must be in the `required` array
45
+ * - Optional fields should have null added to their type union
46
+ * - additionalProperties must be false for objects
47
+ */
48
+ export function makeMistralStructuredOutputCompatible(
49
+ schema: Record<string, any>,
50
+ originalRequired: Array<string> = [],
51
+ ): Record<string, any> {
52
+ const result = { ...schema }
53
+
54
+ if (result.type === 'object') {
55
+ if (!result.properties) {
56
+ result.properties = {}
57
+ }
58
+ const properties = { ...result.properties }
59
+ const allPropertyNames = Object.keys(properties)
60
+
61
+ for (const propName of allPropertyNames) {
62
+ const prop = properties[propName]
63
+ const wasOptional = !originalRequired.includes(propName)
64
+
65
+ if (prop.type === 'object' && prop.properties) {
66
+ const converted = makeMistralStructuredOutputCompatible(
67
+ prop,
68
+ prop.required || [],
69
+ )
70
+ if (wasOptional) {
71
+ properties[propName] = {
72
+ ...converted,
73
+ type: Array.isArray(converted.type)
74
+ ? converted.type.includes('null')
75
+ ? converted.type
76
+ : [...converted.type, 'null']
77
+ : [converted.type, 'null'],
78
+ }
79
+ } else {
80
+ properties[propName] = converted
81
+ }
82
+ } else if (prop.type === 'array' && prop.items) {
83
+ const converted = {
84
+ ...prop,
85
+ items: makeMistralStructuredOutputCompatible(
86
+ prop.items,
87
+ prop.items.required || [],
88
+ ),
89
+ }
90
+ if (wasOptional) {
91
+ properties[propName] = {
92
+ ...converted,
93
+ type: Array.isArray(converted.type)
94
+ ? converted.type.includes('null')
95
+ ? converted.type
96
+ : [...converted.type, 'null']
97
+ : [converted.type, 'null'],
98
+ }
99
+ } else {
100
+ properties[propName] = converted
101
+ }
102
+ } else if (wasOptional) {
103
+ if (prop.type && !Array.isArray(prop.type)) {
104
+ properties[propName] = {
105
+ ...prop,
106
+ type: [prop.type, 'null'],
107
+ }
108
+ } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
109
+ properties[propName] = {
110
+ ...prop,
111
+ type: [...prop.type, 'null'],
112
+ }
113
+ } else if (!prop.type) {
114
+ properties[propName] = { anyOf: [prop, { type: 'null' }] }
115
+ }
116
+ }
117
+ }
118
+
119
+ result.properties = properties
120
+ if (allPropertyNames.length > 0) {
121
+ result.required = allPropertyNames
122
+ } else {
123
+ delete result.required
124
+ }
125
+ result.additionalProperties = false
126
+ }
127
+
128
+ if (result.type === 'array' && result.items) {
129
+ result.items = makeMistralStructuredOutputCompatible(
130
+ result.items,
131
+ result.items.required || [],
132
+ )
133
+ }
134
+
135
+ return result
136
+ }