@tanstack/ai-mistral 0.3.4 → 0.4.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.
- package/dist/esm/adapters/text.d.ts +2 -2
- package/dist/esm/adapters/text.js +16 -12
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/model-meta.d.ts +71 -0
- package/dist/esm/model-meta.js +335 -242
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/tools/function-tool.d.ts +4 -5
- package/dist/esm/tools/function-tool.js +7 -8
- package/dist/esm/tools/function-tool.js.map +1 -1
- package/dist/esm/tools/function-tool.test.d.ts +1 -0
- package/dist/esm/utils/client.d.ts +12 -0
- package/dist/esm/utils/client.js +9 -5
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/schema-converter.d.ts +15 -15
- package/dist/esm/utils/schema-converter.js +173 -50
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/dist/esm/utils/schema-converter.test.d.ts +1 -0
- package/dist/esm/utils/tool-input-normalizer.d.ts +8 -0
- package/dist/esm/utils/tool-input-normalizer.js +34 -0
- package/dist/esm/utils/tool-input-normalizer.js.map +1 -0
- package/dist/esm/utils/tool-input-normalizer.test.d.ts +1 -0
- package/dist/esm/vertex/auth.d.ts +29 -0
- package/dist/esm/vertex/auth.js +62 -0
- package/dist/esm/vertex/auth.js.map +1 -0
- package/dist/esm/vertex/index.d.ts +12 -0
- package/dist/esm/vertex/index.js +26 -0
- package/dist/esm/vertex/index.js.map +1 -0
- package/package.json +16 -4
- package/src/adapters/text.ts +40 -20
- package/src/index.ts +6 -1
- package/src/model-meta.ts +70 -0
- package/src/tools/function-tool.test.ts +32 -0
- package/src/tools/function-tool.ts +11 -11
- package/src/utils/client.ts +41 -6
- package/src/utils/schema-converter.test.ts +256 -0
- package/src/utils/schema-converter.ts +251 -88
- package/src/utils/tool-input-normalizer.test.ts +50 -0
- package/src/utils/tool-input-normalizer.ts +42 -0
- package/src/vertex/auth.ts +154 -0
- package/src/vertex/index.ts +50 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { undoNullWidening } from '@tanstack/ai-utils'
|
|
2
|
+
import { makeMistralStructuredOutputCompatibleWithMap } from './schema-converter'
|
|
3
|
+
import type { JSONSchema, Tool } from '@tanstack/ai'
|
|
4
|
+
import type { NullWideningMap } from '@tanstack/ai-utils'
|
|
5
|
+
|
|
6
|
+
export type ToolInputNormalizer = (toolName: string, input: unknown) => unknown
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Per-request inverse of Mistral optional-null widening. Recomputes maps from
|
|
10
|
+
* each tool's input schema; duplicate names and non-strict schemas are left
|
|
11
|
+
* untouched so we never guess.
|
|
12
|
+
*/
|
|
13
|
+
export function createToolInputNormalizer(
|
|
14
|
+
tools: Array<Tool> | undefined,
|
|
15
|
+
): ToolInputNormalizer {
|
|
16
|
+
const maps = new Map<string, NullWideningMap>()
|
|
17
|
+
const seenNames = new Set<string>()
|
|
18
|
+
const ambiguousNames = new Set<string>()
|
|
19
|
+
|
|
20
|
+
for (const tool of tools ?? []) {
|
|
21
|
+
if (ambiguousNames.has(tool.name)) continue
|
|
22
|
+
if (seenNames.has(tool.name)) {
|
|
23
|
+
maps.delete(tool.name)
|
|
24
|
+
ambiguousNames.add(tool.name)
|
|
25
|
+
continue
|
|
26
|
+
}
|
|
27
|
+
seenNames.add(tool.name)
|
|
28
|
+
|
|
29
|
+
const inputSchema = (tool.inputSchema ?? {
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {},
|
|
32
|
+
required: [],
|
|
33
|
+
}) as JSONSchema
|
|
34
|
+
const { nullWideningMap } = makeMistralStructuredOutputCompatibleWithMap(
|
|
35
|
+
inputSchema,
|
|
36
|
+
inputSchema.required || [],
|
|
37
|
+
)
|
|
38
|
+
if (nullWideningMap) maps.set(tool.name, nullWideningMap)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (toolName, input) => undoNullWidening(input, maps.get(toolName))
|
|
42
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export class MistralVertexAuthError extends Error {
|
|
2
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
3
|
+
super(message, options)
|
|
4
|
+
this.name = 'MistralVertexAuthError'
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const MISTRAL_VERTEX_LOCATIONS = ['us-central1', 'europe-west4'] as const
|
|
9
|
+
|
|
10
|
+
function isMistralVertexLocation(
|
|
11
|
+
location: string,
|
|
12
|
+
): location is (typeof MISTRAL_VERTEX_LOCATIONS)[number] {
|
|
13
|
+
return (MISTRAL_VERTEX_LOCATIONS as ReadonlyArray<string>).includes(location)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isMissingGoogleAuthLibrary(error: unknown): boolean {
|
|
17
|
+
if (!(error instanceof Error) || !('code' in error)) {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
const code = error.code
|
|
21
|
+
if (code !== 'ERR_MODULE_NOT_FOUND' && code !== 'MODULE_NOT_FOUND') {
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
24
|
+
return error.message.includes('google-auth-library')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type VertexAuthClient = {
|
|
28
|
+
getRequestHeaders: (url?: string | URL) => Promise<Headers>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Public Vertex config for Mistral. `project` and `location` match the
|
|
33
|
+
* Gemini Vertex factories so one auth object works for both.
|
|
34
|
+
*
|
|
35
|
+
* Mistral on Vertex is regional only (`us-central1`, `europe-west4`).
|
|
36
|
+
* There is no global endpoint.
|
|
37
|
+
*/
|
|
38
|
+
export type MistralVertexConfig = {
|
|
39
|
+
project?: string
|
|
40
|
+
location?: string
|
|
41
|
+
/**
|
|
42
|
+
* Override the chat completions URL. When set, the Vertex
|
|
43
|
+
* `:rawPredict` / `:streamRawPredict` rewrite is skipped. Used by e2e.
|
|
44
|
+
*/
|
|
45
|
+
resolveRequestUrl?: (stream: boolean) => string
|
|
46
|
+
getAccessToken?: () => Promise<string>
|
|
47
|
+
authClient?: VertexAuthClient
|
|
48
|
+
defaultHeaders?: Record<string, string>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function nonEmpty(value: string | undefined): string | undefined {
|
|
52
|
+
if (value === undefined || value.length === 0) {
|
|
53
|
+
return undefined
|
|
54
|
+
}
|
|
55
|
+
return value
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readEnv(name: string): string | undefined {
|
|
59
|
+
if (typeof process === 'undefined' || process.env === undefined) {
|
|
60
|
+
return undefined
|
|
61
|
+
}
|
|
62
|
+
return nonEmpty(process.env[name])
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function resolveMistralVertexProject(
|
|
66
|
+
config: MistralVertexConfig,
|
|
67
|
+
): string | undefined {
|
|
68
|
+
return (
|
|
69
|
+
nonEmpty(config.project) ??
|
|
70
|
+
readEnv('GOOGLE_CLOUD_PROJECT') ??
|
|
71
|
+
readEnv('GOOGLE_VERTEX_PROJECT')
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function resolveMistralVertexLocation(
|
|
76
|
+
config: MistralVertexConfig,
|
|
77
|
+
): string {
|
|
78
|
+
const location =
|
|
79
|
+
nonEmpty(config.location) ??
|
|
80
|
+
readEnv('GOOGLE_CLOUD_LOCATION') ??
|
|
81
|
+
readEnv('GOOGLE_VERTEX_LOCATION')
|
|
82
|
+
if (location === undefined) {
|
|
83
|
+
throw new MistralVertexAuthError(
|
|
84
|
+
'Mistral Vertex needs a location. Pass location on the factory, or set GOOGLE_CLOUD_LOCATION or GOOGLE_VERTEX_LOCATION. Use us-central1 or europe-west4.',
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
if (!isMistralVertexLocation(location)) {
|
|
88
|
+
throw new MistralVertexAuthError(
|
|
89
|
+
'Mistral Vertex location must be us-central1 or europe-west4. There is no global endpoint.',
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
return location
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function resolveMistralVertexModelUrl(
|
|
96
|
+
model: string,
|
|
97
|
+
config: MistralVertexConfig,
|
|
98
|
+
): string {
|
|
99
|
+
const project = resolveMistralVertexProject(config)
|
|
100
|
+
if (project === undefined) {
|
|
101
|
+
throw new MistralVertexAuthError(
|
|
102
|
+
'Mistral Vertex needs a project. Pass project on the factory, or set GOOGLE_CLOUD_PROJECT or GOOGLE_VERTEX_PROJECT.',
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
const location = resolveMistralVertexLocation(config)
|
|
106
|
+
return `https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/mistralai/models/${model}`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function resolveMistralVertexAccessToken(
|
|
110
|
+
config: MistralVertexConfig,
|
|
111
|
+
): Promise<string> {
|
|
112
|
+
if (config.getAccessToken !== undefined) {
|
|
113
|
+
return config.getAccessToken()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (config.authClient !== undefined) {
|
|
117
|
+
const headers = await config.authClient.getRequestHeaders()
|
|
118
|
+
const authorization = headers.get('Authorization')
|
|
119
|
+
if (authorization === null || !authorization.startsWith('Bearer ')) {
|
|
120
|
+
throw new MistralVertexAuthError(
|
|
121
|
+
'Mistral Vertex authClient.getRequestHeaders() must return an Authorization Bearer token.',
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
return authorization.slice('Bearer '.length)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const { GoogleAuth } = await import('google-auth-library')
|
|
129
|
+
const auth = new GoogleAuth({
|
|
130
|
+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
|
|
131
|
+
})
|
|
132
|
+
const client = await auth.getClient()
|
|
133
|
+
const token = await client.getAccessToken()
|
|
134
|
+
if (token.token === null || token.token === undefined) {
|
|
135
|
+
throw new MistralVertexAuthError(
|
|
136
|
+
'Mistral Vertex could not load a Google access token from Application Default Credentials.',
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
return token.token
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (error instanceof MistralVertexAuthError) {
|
|
142
|
+
throw error
|
|
143
|
+
}
|
|
144
|
+
if (isMissingGoogleAuthLibrary(error)) {
|
|
145
|
+
throw new MistralVertexAuthError(
|
|
146
|
+
'Mistral Vertex needs google-auth-library, or pass authClient or getAccessToken. Install google-auth-library next to @tanstack/ai-mistral.',
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
throw new MistralVertexAuthError(
|
|
150
|
+
'Mistral Vertex could not load a Google access token from Application Default Credentials.',
|
|
151
|
+
{ cause: error },
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { MistralTextAdapter } from '../adapters/text'
|
|
2
|
+
import {
|
|
3
|
+
resolveMistralVertexAccessToken,
|
|
4
|
+
resolveMistralVertexModelUrl,
|
|
5
|
+
} from './auth'
|
|
6
|
+
import type { MistralVertexChatModel } from '../model-meta'
|
|
7
|
+
import type { MistralVertexConfig } from './auth'
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
MistralVertexAuthError,
|
|
11
|
+
resolveMistralVertexAccessToken,
|
|
12
|
+
resolveMistralVertexLocation,
|
|
13
|
+
resolveMistralVertexModelUrl,
|
|
14
|
+
resolveMistralVertexProject,
|
|
15
|
+
type MistralVertexConfig,
|
|
16
|
+
type VertexAuthClient,
|
|
17
|
+
} from './auth'
|
|
18
|
+
export {
|
|
19
|
+
MISTRAL_VERTEX_CHAT_MODELS,
|
|
20
|
+
type MistralVertexChatModel,
|
|
21
|
+
} from '../model-meta'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a Mistral chat adapter that talks to Mistral on Vertex AI.
|
|
25
|
+
*
|
|
26
|
+
* Install `google-auth-library` next to `@tanstack/ai-mistral` for
|
|
27
|
+
* Application Default Credentials. Or pass `authClient` or `getAccessToken`.
|
|
28
|
+
*/
|
|
29
|
+
export function mistralVertexText<TModel extends MistralVertexChatModel>(
|
|
30
|
+
model: TModel,
|
|
31
|
+
config: MistralVertexConfig = {},
|
|
32
|
+
): MistralTextAdapter<TModel> {
|
|
33
|
+
const resolveRequestUrl =
|
|
34
|
+
config.resolveRequestUrl ??
|
|
35
|
+
((stream: boolean) => {
|
|
36
|
+
const modelUrl = resolveMistralVertexModelUrl(model, config)
|
|
37
|
+
return `${modelUrl}:${stream ? 'streamRawPredict' : 'rawPredict'}`
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
return new MistralTextAdapter(
|
|
41
|
+
{
|
|
42
|
+
apiKey: 'vertex',
|
|
43
|
+
getAccessToken: () => resolveMistralVertexAccessToken(config),
|
|
44
|
+
resolveRequestUrl,
|
|
45
|
+
requestModel: model,
|
|
46
|
+
defaultHeaders: config.defaultHeaders,
|
|
47
|
+
},
|
|
48
|
+
model,
|
|
49
|
+
)
|
|
50
|
+
}
|