@tanstack/ai-vercel-gateway 0.0.1
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/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/esm/adapters/embedding.d.ts +21 -0
- package/dist/esm/adapters/embedding.js +73 -0
- package/dist/esm/adapters/embedding.js.map +1 -0
- package/dist/esm/adapters/factory.d.ts +16 -0
- package/dist/esm/adapters/factory.js +28 -0
- package/dist/esm/adapters/factory.js.map +1 -0
- package/dist/esm/adapters/image.d.ts +22 -0
- package/dist/esm/adapters/image.js +87 -0
- package/dist/esm/adapters/image.js.map +1 -0
- package/dist/esm/adapters/responses-text.d.ts +26 -0
- package/dist/esm/adapters/responses-text.js +38 -0
- package/dist/esm/adapters/responses-text.js.map +1 -0
- package/dist/esm/adapters/summarize.d.ts +9 -0
- package/dist/esm/adapters/summarize.js +17 -0
- package/dist/esm/adapters/summarize.js.map +1 -0
- package/dist/esm/adapters/text.d.ts +60 -0
- package/dist/esm/adapters/text.js +29 -0
- package/dist/esm/adapters/text.js.map +1 -0
- package/dist/esm/embedding/embedding-provider-options.d.ts +5 -0
- package/dist/esm/image/image-provider-options.d.ts +7 -0
- package/dist/esm/index.d.ts +17 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/message-types.d.ts +44 -0
- package/dist/esm/model-meta.d.ts +493 -0
- package/dist/esm/model-meta.js +351 -0
- package/dist/esm/model-meta.js.map +1 -0
- package/dist/esm/text/responses-provider-options.d.ts +3 -0
- package/dist/esm/text/text-provider-options.d.ts +40 -0
- package/dist/esm/tools/function-tool.d.ts +16 -0
- package/dist/esm/tools/function-tool.js +30 -0
- package/dist/esm/tools/function-tool.js.map +1 -0
- package/dist/esm/tools/index.d.ts +2 -0
- package/dist/esm/tools/index.js +3 -0
- package/dist/esm/tools/tool-converter.d.ts +7 -0
- package/dist/esm/tools/tool-converter.js +15 -0
- package/dist/esm/tools/tool-converter.js.map +1 -0
- package/dist/esm/utils/client.d.ts +8 -0
- package/dist/esm/utils/client.js +29 -0
- package/dist/esm/utils/client.js.map +1 -0
- package/dist/esm/utils/map-gateway-options.d.ts +4 -0
- package/dist/esm/utils/map-gateway-options.js +14 -0
- package/dist/esm/utils/map-gateway-options.js.map +1 -0
- package/package.json +73 -0
- package/src/adapters/embedding.ts +122 -0
- package/src/adapters/factory.ts +81 -0
- package/src/adapters/image.ts +156 -0
- package/src/adapters/responses-text.ts +99 -0
- package/src/adapters/summarize.ts +44 -0
- package/src/adapters/text.ts +68 -0
- package/src/embedding/embedding-provider-options.ts +6 -0
- package/src/image/image-provider-options.ts +13 -0
- package/src/index.ts +78 -0
- package/src/message-types.ts +45 -0
- package/src/model-meta.ts +2512 -0
- package/src/text/responses-provider-options.ts +10 -0
- package/src/text/text-provider-options.ts +47 -0
- package/src/tools/function-tool.ts +46 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/tool-converter.ts +15 -0
- package/src/utils/client.ts +40 -0
- package/src/utils/map-gateway-options.ts +15 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
VercelGatewayBaseOptions,
|
|
3
|
+
VercelGatewayCommonOptions,
|
|
4
|
+
} from './text-provider-options'
|
|
5
|
+
|
|
6
|
+
export type VercelGatewayResponsesProviderOptions = VercelGatewayCommonOptions &
|
|
7
|
+
Pick<VercelGatewayBaseOptions, 'temperature' | 'top_p' | 'max_output_tokens'>
|
|
8
|
+
|
|
9
|
+
export type ExternalResponsesProviderOptions =
|
|
10
|
+
VercelGatewayResponsesProviderOptions
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
VercelGatewayChatModel,
|
|
3
|
+
VercelGatewayProvider,
|
|
4
|
+
} from '../model-meta'
|
|
5
|
+
|
|
6
|
+
export interface VercelGatewayRoutingOptions {
|
|
7
|
+
order?: Array<VercelGatewayProvider>
|
|
8
|
+
only?: Array<VercelGatewayProvider>
|
|
9
|
+
sort?: 'cost' | 'ttft' | 'tps'
|
|
10
|
+
models?: Array<VercelGatewayChatModel>
|
|
11
|
+
caching?: 'auto'
|
|
12
|
+
byok?: Record<string, Array<{ apiKey: string }>>
|
|
13
|
+
zeroDataRetention?: boolean
|
|
14
|
+
disallowPromptTraining?: boolean
|
|
15
|
+
user?: string
|
|
16
|
+
tags?: Array<string>
|
|
17
|
+
serviceTier?: string
|
|
18
|
+
providerTimeouts?: Record<string, number>
|
|
19
|
+
quotaEntityId?: string
|
|
20
|
+
has?: Array<string>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface VercelGatewayCommonOptions {
|
|
24
|
+
gateway?: VercelGatewayRoutingOptions
|
|
25
|
+
user?: string | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface VercelGatewayBaseOptions {
|
|
29
|
+
temperature?: number | null
|
|
30
|
+
top_p?: number | null
|
|
31
|
+
max_tokens?: number | null
|
|
32
|
+
max_completion_tokens?: number | null
|
|
33
|
+
max_output_tokens?: number | null
|
|
34
|
+
frequency_penalty?: number | null
|
|
35
|
+
presence_penalty?: number | null
|
|
36
|
+
stop?: string | null | Array<string>
|
|
37
|
+
seed?: number | null
|
|
38
|
+
reasoning?: boolean | Record<string, unknown> | null
|
|
39
|
+
include_reasoning?: boolean | null
|
|
40
|
+
response_format?: unknown
|
|
41
|
+
structured_outputs?: boolean | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type VercelGatewayTextProviderOptions = VercelGatewayCommonOptions &
|
|
45
|
+
VercelGatewayBaseOptions
|
|
46
|
+
|
|
47
|
+
export type ExternalTextProviderOptions = VercelGatewayTextProviderOptions
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { makeStructuredOutputCompatible } from '@tanstack/openai-base'
|
|
2
|
+
import type { JSONSchema, Tool } from '@tanstack/ai'
|
|
3
|
+
|
|
4
|
+
export interface FunctionTool {
|
|
5
|
+
type: 'function'
|
|
6
|
+
function: {
|
|
7
|
+
name: string
|
|
8
|
+
description?: string
|
|
9
|
+
parameters: Record<string, unknown>
|
|
10
|
+
strict?: boolean
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Converts a standard Tool to Vercel AI Gateway Chat Completions tool format.
|
|
16
|
+
*
|
|
17
|
+
* Tool schemas are already converted to JSON Schema in the ai layer.
|
|
18
|
+
*/
|
|
19
|
+
export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
|
|
20
|
+
const inputSchema = (tool.inputSchema ?? {
|
|
21
|
+
type: 'object',
|
|
22
|
+
properties: {},
|
|
23
|
+
required: [],
|
|
24
|
+
}) as JSONSchema
|
|
25
|
+
|
|
26
|
+
if (inputSchema.type === 'object' && !inputSchema.properties) {
|
|
27
|
+
inputSchema.properties = {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const jsonSchema = makeStructuredOutputCompatible(
|
|
31
|
+
inputSchema,
|
|
32
|
+
inputSchema.required || [],
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
jsonSchema.additionalProperties = false
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
type: 'function',
|
|
39
|
+
function: {
|
|
40
|
+
name: tool.name,
|
|
41
|
+
description: tool.description,
|
|
42
|
+
parameters: jsonSchema,
|
|
43
|
+
strict: true,
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { convertFunctionToolToAdapterFormat } from './function-tool'
|
|
2
|
+
import type { FunctionTool } from './function-tool'
|
|
3
|
+
import type { Tool } from '@tanstack/ai'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Converts an array of standard Tools to Vercel AI Gateway format.
|
|
7
|
+
* The Gateway Chat Completions API is OpenAI-compatible, so we support function tools.
|
|
8
|
+
*/
|
|
9
|
+
export function convertToolsToProviderFormat(
|
|
10
|
+
tools: Array<Tool>,
|
|
11
|
+
): Array<FunctionTool> {
|
|
12
|
+
return tools.map((tool) => {
|
|
13
|
+
return convertFunctionToolToAdapterFormat(tool)
|
|
14
|
+
})
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getApiKeyFromEnv } from '@tanstack/ai-utils'
|
|
2
|
+
import type { ClientOptions } from 'openai'
|
|
3
|
+
|
|
4
|
+
export interface VercelGatewayClientConfig extends Omit<
|
|
5
|
+
ClientOptions,
|
|
6
|
+
'apiKey'
|
|
7
|
+
> {
|
|
8
|
+
apiKey: string
|
|
9
|
+
httpReferer?: string
|
|
10
|
+
xTitle?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getVercelGatewayApiKeyFromEnv(): string {
|
|
14
|
+
try {
|
|
15
|
+
return getApiKeyFromEnv('AI_GATEWAY_API_KEY')
|
|
16
|
+
} catch {
|
|
17
|
+
try {
|
|
18
|
+
return getApiKeyFromEnv('VERCEL_OIDC_TOKEN')
|
|
19
|
+
} catch {
|
|
20
|
+
throw new Error(
|
|
21
|
+
'AI_GATEWAY_API_KEY or VERCEL_OIDC_TOKEN is required. Set one in the environment or pass apiKey to the factory.',
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function withVercelGatewayDefaults(
|
|
28
|
+
config: VercelGatewayClientConfig,
|
|
29
|
+
): ClientOptions {
|
|
30
|
+
const { httpReferer, xTitle, defaultHeaders, ...rest } = config
|
|
31
|
+
return {
|
|
32
|
+
...rest,
|
|
33
|
+
baseURL: config.baseURL || 'https://ai-gateway.vercel.sh/v1',
|
|
34
|
+
defaultHeaders: {
|
|
35
|
+
...(httpReferer ? { 'http-referer': httpReferer } : {}),
|
|
36
|
+
...(xTitle ? { 'x-title': xTitle } : {}),
|
|
37
|
+
...defaultHeaders,
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { VercelGatewayRoutingOptions } from '../text/text-provider-options'
|
|
2
|
+
|
|
3
|
+
export function mapGatewayModelOptions(
|
|
4
|
+
modelOptions:
|
|
5
|
+
| (Record<string, unknown> & { gateway?: VercelGatewayRoutingOptions })
|
|
6
|
+
| undefined,
|
|
7
|
+
): Record<string, unknown> {
|
|
8
|
+
if (!modelOptions) return {}
|
|
9
|
+
const { gateway, ...rest } = modelOptions
|
|
10
|
+
if (gateway === undefined) return rest
|
|
11
|
+
return {
|
|
12
|
+
...rest,
|
|
13
|
+
providerOptions: { gateway },
|
|
14
|
+
}
|
|
15
|
+
}
|