@tanstack/ai-grok 0.7.0 → 0.7.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.
- package/dist/esm/adapters/image.d.ts +8 -7
- package/dist/esm/adapters/image.js +10 -60
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/summarize.d.ts +2 -7
- package/dist/esm/adapters/summarize.js +11 -95
- package/dist/esm/adapters/summarize.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +10 -34
- package/dist/esm/adapters/text.js +4 -381
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/adapters/transcription.js +3 -1
- package/dist/esm/adapters/transcription.js.map +1 -1
- package/dist/esm/adapters/tts.js +3 -1
- package/dist/esm/adapters/tts.js.map +1 -1
- package/dist/esm/realtime/token.js +2 -0
- package/dist/esm/realtime/token.js.map +1 -1
- package/dist/esm/text/text-provider-options.d.ts +0 -14
- package/dist/esm/tools/index.d.ts +1 -2
- package/dist/esm/tools/index.js +3 -4
- package/dist/esm/tools/index.js.map +1 -1
- package/dist/esm/utils/client.d.ts +5 -9
- package/dist/esm/utils/client.js +11 -17
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/index.d.ts +2 -1
- package/dist/esm/utils/schema-converter.d.ts +2 -24
- package/package.json +6 -6
- package/src/adapters/image.ts +18 -87
- package/src/adapters/summarize.ts +16 -121
- package/src/adapters/text.ts +13 -562
- package/src/text/text-provider-options.ts +0 -22
- package/src/tools/index.ts +4 -4
- package/src/utils/client.ts +15 -30
- package/src/utils/index.ts +2 -2
- package/src/utils/schema-converter.ts +2 -110
- package/dist/esm/text/text-provider-options.js +0 -6
- package/dist/esm/text/text-provider-options.js.map +0 -1
- package/dist/esm/tools/function-tool.d.ts +0 -15
- package/dist/esm/tools/function-tool.js +0 -27
- package/dist/esm/tools/function-tool.js.map +0 -1
- package/dist/esm/tools/tool-converter.d.ts +0 -7
- package/dist/esm/tools/tool-converter.js +0 -10
- package/dist/esm/tools/tool-converter.js.map +0 -1
- package/dist/esm/utils/schema-converter.js +0 -71
- package/dist/esm/utils/schema-converter.js.map +0 -1
- package/src/tools/function-tool.ts +0 -45
- package/src/tools/tool-converter.ts +0 -17
package/src/utils/client.ts
CHANGED
|
@@ -1,46 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import { getApiKeyFromEnv } from '@tanstack/ai-utils'
|
|
2
|
+
import type { OpenAICompatibleClientConfig } from '@tanstack/openai-base'
|
|
3
3
|
|
|
4
|
-
export interface GrokClientConfig extends
|
|
5
|
-
apiKey: string
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates a Grok SDK client instance using OpenAI SDK with xAI's base URL
|
|
10
|
-
*/
|
|
11
|
-
export function createGrokClient(config: GrokClientConfig): OpenAI_SDK {
|
|
12
|
-
return new OpenAI_SDK({
|
|
13
|
-
...config,
|
|
14
|
-
apiKey: config.apiKey,
|
|
15
|
-
baseURL: config.baseURL || 'https://api.x.ai/v1',
|
|
16
|
-
})
|
|
17
|
-
}
|
|
4
|
+
export interface GrokClientConfig extends OpenAICompatibleClientConfig {}
|
|
18
5
|
|
|
19
6
|
/**
|
|
20
7
|
* Gets Grok API key from environment variables
|
|
21
8
|
* @throws Error if XAI_API_KEY is not found
|
|
22
9
|
*/
|
|
23
10
|
export function getGrokApiKeyFromEnv(): string {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
: typeof process !== 'undefined'
|
|
28
|
-
? process.env
|
|
29
|
-
: undefined
|
|
30
|
-
const key = env?.XAI_API_KEY
|
|
31
|
-
|
|
32
|
-
if (!key) {
|
|
11
|
+
try {
|
|
12
|
+
return getApiKeyFromEnv('XAI_API_KEY')
|
|
13
|
+
} catch {
|
|
33
14
|
throw new Error(
|
|
34
15
|
'XAI_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.',
|
|
35
16
|
)
|
|
36
17
|
}
|
|
37
|
-
|
|
38
|
-
return key
|
|
39
18
|
}
|
|
40
19
|
|
|
41
20
|
/**
|
|
42
|
-
*
|
|
21
|
+
* Returns a Grok client config with the default xAI base URL applied
|
|
22
|
+
* when not already set.
|
|
43
23
|
*/
|
|
44
|
-
export function
|
|
45
|
-
|
|
24
|
+
export function withGrokDefaults(
|
|
25
|
+
config: GrokClientConfig,
|
|
26
|
+
): OpenAICompatibleClientConfig {
|
|
27
|
+
return {
|
|
28
|
+
...config,
|
|
29
|
+
baseURL: config.baseURL || 'https://api.x.ai/v1',
|
|
30
|
+
}
|
|
46
31
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,110 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* This is needed because Grok's structured output (via OpenAI-compatible API) requires all fields to be
|
|
5
|
-
* in the `required` array, with optional fields made nullable (type: ["string", "null"]).
|
|
6
|
-
* When Grok returns null for optional fields, we need to convert them back to
|
|
7
|
-
* undefined to match the original Zod schema expectations.
|
|
8
|
-
*
|
|
9
|
-
* @param obj - Object to transform
|
|
10
|
-
* @returns Object with nulls converted to undefined
|
|
11
|
-
*/
|
|
12
|
-
export function transformNullsToUndefined<T>(obj: T): T {
|
|
13
|
-
if (obj === null) {
|
|
14
|
-
return undefined as unknown as T
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (Array.isArray(obj)) {
|
|
18
|
-
return obj.map((item) => transformNullsToUndefined(item)) as unknown as T
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (typeof obj === 'object') {
|
|
22
|
-
const result: Record<string, unknown> = {}
|
|
23
|
-
for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
|
|
24
|
-
const transformed = transformNullsToUndefined(value)
|
|
25
|
-
// Only include the key if the value is not undefined
|
|
26
|
-
// This makes { notes: null } become {} (field absent) instead of { notes: undefined }
|
|
27
|
-
if (transformed !== undefined) {
|
|
28
|
-
result[key] = transformed
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return result as T
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return obj
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Transform a JSON schema to be compatible with Grok's structured output requirements (OpenAI-compatible).
|
|
39
|
-
* Grok requires:
|
|
40
|
-
* - All properties must be in the `required` array
|
|
41
|
-
* - Optional fields should have null added to their type union
|
|
42
|
-
* - additionalProperties must be false for objects
|
|
43
|
-
*
|
|
44
|
-
* @param schema - JSON schema to transform
|
|
45
|
-
* @param originalRequired - Original required array (to know which fields were optional)
|
|
46
|
-
* @returns Transformed schema compatible with Grok structured output
|
|
47
|
-
*/
|
|
48
|
-
export function makeGrokStructuredOutputCompatible(
|
|
49
|
-
schema: Record<string, any>,
|
|
50
|
-
originalRequired: Array<string> = [],
|
|
51
|
-
): Record<string, any> {
|
|
52
|
-
const result = { ...schema }
|
|
53
|
-
|
|
54
|
-
// Handle object types
|
|
55
|
-
if (result.type === 'object' && result.properties) {
|
|
56
|
-
const properties = { ...result.properties }
|
|
57
|
-
const allPropertyNames = Object.keys(properties)
|
|
58
|
-
|
|
59
|
-
// Transform each property
|
|
60
|
-
for (const propName of allPropertyNames) {
|
|
61
|
-
const prop = properties[propName]
|
|
62
|
-
const wasOptional = !originalRequired.includes(propName)
|
|
63
|
-
|
|
64
|
-
// Recursively transform nested objects/arrays
|
|
65
|
-
if (prop.type === 'object' && prop.properties) {
|
|
66
|
-
properties[propName] = makeGrokStructuredOutputCompatible(
|
|
67
|
-
prop,
|
|
68
|
-
prop.required || [],
|
|
69
|
-
)
|
|
70
|
-
} else if (prop.type === 'array' && prop.items) {
|
|
71
|
-
properties[propName] = {
|
|
72
|
-
...prop,
|
|
73
|
-
items: makeGrokStructuredOutputCompatible(
|
|
74
|
-
prop.items,
|
|
75
|
-
prop.items.required || [],
|
|
76
|
-
),
|
|
77
|
-
}
|
|
78
|
-
} else if (wasOptional) {
|
|
79
|
-
// Make optional fields nullable by adding null to the type
|
|
80
|
-
if (prop.type && !Array.isArray(prop.type)) {
|
|
81
|
-
properties[propName] = {
|
|
82
|
-
...prop,
|
|
83
|
-
type: [prop.type, 'null'],
|
|
84
|
-
}
|
|
85
|
-
} else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
|
|
86
|
-
properties[propName] = {
|
|
87
|
-
...prop,
|
|
88
|
-
type: [...prop.type, 'null'],
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
result.properties = properties
|
|
95
|
-
// ALL properties must be required for Grok structured output
|
|
96
|
-
result.required = allPropertyNames
|
|
97
|
-
// additionalProperties must be false
|
|
98
|
-
result.additionalProperties = false
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Handle array types with object items
|
|
102
|
-
if (result.type === 'array' && result.items) {
|
|
103
|
-
result.items = makeGrokStructuredOutputCompatible(
|
|
104
|
-
result.items,
|
|
105
|
-
result.items.required || [],
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return result
|
|
110
|
-
}
|
|
1
|
+
export { transformNullsToUndefined } from '@tanstack/ai-utils'
|
|
2
|
+
export { makeStructuredOutputCompatible as makeGrokStructuredOutputCompatible } from '@tanstack/openai-base'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"text-provider-options.js","sources":["../../../src/text/text-provider-options.ts"],"sourcesContent":["import type { FunctionTool } from '../tools/function-tool'\n\n/**\n * Grok Text Provider Options\n *\n * Grok uses an OpenAI-compatible Chat Completions API.\n * However, not all OpenAI features may be supported by Grok.\n */\n\n/**\n * Base provider options for Grok text/chat models\n */\nexport interface GrokBaseOptions {\n /**\n * A unique identifier representing your end-user.\n * Can help xAI to monitor and detect abuse.\n */\n user?: string\n}\n\n/**\n * Grok-specific provider options for text/chat\n * Based on OpenAI-compatible API options\n */\nexport interface GrokTextProviderOptions extends GrokBaseOptions {\n /**\n * Temperature for response generation (0-2)\n * Higher values make output more random, lower values more focused\n */\n temperature?: number\n /**\n * Top-p sampling parameter (0-1)\n * Alternative to temperature, nucleus sampling\n */\n top_p?: number\n /**\n * Maximum tokens in the response\n */\n max_tokens?: number\n /**\n * Frequency penalty (-2.0 to 2.0)\n */\n frequency_penalty?: number\n /**\n * Presence penalty (-2.0 to 2.0)\n */\n presence_penalty?: number\n /**\n * Stop sequences\n */\n stop?: string | Array<string>\n}\n\n/**\n * Internal options interface for validation\n * Used internally by the adapter\n */\nexport interface InternalTextProviderOptions extends GrokTextProviderOptions {\n model: string\n stream?: boolean\n tools?: Array<FunctionTool>\n}\n\n/**\n * External provider options (what users pass in)\n */\nexport type ExternalTextProviderOptions = GrokTextProviderOptions\n\n/**\n * Validates text provider options\n */\nexport function validateTextProviderOptions(\n _options: InternalTextProviderOptions,\n): void {\n // Basic validation can be added here if needed\n // For now, Grok API will handle validation\n}\n"],"names":[],"mappings":"AAuEO,SAAS,4BACd,UACM;AAGR;"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Tool } from '@tanstack/ai';
|
|
2
|
-
import { default as OpenAI } from 'openai';
|
|
3
|
-
export type FunctionTool = OpenAI.Chat.Completions.ChatCompletionTool;
|
|
4
|
-
/**
|
|
5
|
-
* Converts a standard Tool to Grok ChatCompletionTool format.
|
|
6
|
-
*
|
|
7
|
-
* Tool schemas are already converted to JSON Schema in the ai layer.
|
|
8
|
-
* We apply Grok-specific transformations for strict mode:
|
|
9
|
-
* - All properties in required array
|
|
10
|
-
* - Optional fields made nullable
|
|
11
|
-
* - additionalProperties: false
|
|
12
|
-
*
|
|
13
|
-
* This enables strict mode for all tools automatically.
|
|
14
|
-
*/
|
|
15
|
-
export declare function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { makeGrokStructuredOutputCompatible } from "../utils/schema-converter.js";
|
|
2
|
-
function convertFunctionToolToAdapterFormat(tool) {
|
|
3
|
-
const inputSchema = tool.inputSchema ?? {
|
|
4
|
-
type: "object",
|
|
5
|
-
properties: {},
|
|
6
|
-
required: []
|
|
7
|
-
};
|
|
8
|
-
const jsonSchema = makeGrokStructuredOutputCompatible(
|
|
9
|
-
inputSchema,
|
|
10
|
-
inputSchema.required || []
|
|
11
|
-
);
|
|
12
|
-
jsonSchema.additionalProperties = false;
|
|
13
|
-
return {
|
|
14
|
-
type: "function",
|
|
15
|
-
function: {
|
|
16
|
-
name: tool.name,
|
|
17
|
-
description: tool.description,
|
|
18
|
-
parameters: jsonSchema,
|
|
19
|
-
strict: true
|
|
20
|
-
// Always use strict mode since our schema converter handles the requirements
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
convertFunctionToolToAdapterFormat
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=function-tool.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"function-tool.js","sources":["../../../src/tools/function-tool.ts"],"sourcesContent":["import { makeGrokStructuredOutputCompatible } from '../utils/schema-converter'\nimport type { JSONSchema, Tool } from '@tanstack/ai'\nimport type OpenAI from 'openai'\n\n// Use Chat Completions API tool format (not Responses API)\nexport type FunctionTool = OpenAI.Chat.Completions.ChatCompletionTool\n\n/**\n * Converts a standard Tool to Grok ChatCompletionTool format.\n *\n * Tool schemas are already converted to JSON Schema in the ai layer.\n * We apply Grok-specific transformations for strict mode:\n * - All properties in required array\n * - Optional fields made nullable\n * - additionalProperties: false\n *\n * This enables strict mode for all tools automatically.\n */\nexport function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {\n // Tool schemas are already converted to JSON Schema in the ai layer\n // Apply Grok-specific transformations for strict mode\n const inputSchema = (tool.inputSchema ?? {\n type: 'object',\n properties: {},\n required: [],\n }) as JSONSchema\n\n const jsonSchema = makeGrokStructuredOutputCompatible(\n inputSchema,\n inputSchema.required || [],\n )\n\n // Ensure additionalProperties is false for strict mode\n jsonSchema.additionalProperties = false\n\n return {\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: jsonSchema,\n strict: true, // Always use strict mode since our schema converter handles the requirements\n },\n } satisfies FunctionTool\n}\n"],"names":[],"mappings":";AAkBO,SAAS,mCAAmC,MAA0B;AAG3E,QAAM,cAAe,KAAK,eAAe;AAAA,IACvC,MAAM;AAAA,IACN,YAAY,CAAA;AAAA,IACZ,UAAU,CAAA;AAAA,EAAC;AAGb,QAAM,aAAa;AAAA,IACjB;AAAA,IACA,YAAY,YAAY,CAAA;AAAA,EAAC;AAI3B,aAAW,uBAAuB;AAElC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,KAAK;AAAA,MACX,aAAa,KAAK;AAAA,MAClB,YAAY;AAAA,MACZ,QAAQ;AAAA;AAAA,IAAA;AAAA,EACV;AAEJ;"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { FunctionTool } from './function-tool.js';
|
|
2
|
-
import { Tool } from '@tanstack/ai';
|
|
3
|
-
/**
|
|
4
|
-
* Converts an array of standard Tools to Grok-specific format
|
|
5
|
-
* Grok uses OpenAI-compatible API, so we primarily support function tools
|
|
6
|
-
*/
|
|
7
|
-
export declare function convertToolsToProviderFormat(tools: Array<Tool>): Array<FunctionTool>;
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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 Grok-specific format\n * Grok uses OpenAI-compatible API, so we primarily support function tools\n */\nexport function convertToolsToProviderFormat(\n tools: Array<Tool>,\n): Array<FunctionTool> {\n return tools.map((tool) => {\n // For Grok, all tools are converted as function tools\n // Grok uses OpenAI-compatible API which primarily supports function tools\n return convertFunctionToolToAdapterFormat(tool)\n })\n}\n"],"names":[],"mappings":";AAQO,SAAS,6BACd,OACqB;AACrB,SAAO,MAAM,IAAI,CAAC,SAAS;AAGzB,WAAO,mCAAmC,IAAI;AAAA,EAChD,CAAC;AACH;"}
|
|
@@ -1,71 +0,0 @@
|
|
|
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") {
|
|
9
|
-
const result = {};
|
|
10
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
11
|
-
const transformed = transformNullsToUndefined(value);
|
|
12
|
-
if (transformed !== void 0) {
|
|
13
|
-
result[key] = transformed;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
return obj;
|
|
19
|
-
}
|
|
20
|
-
function makeGrokStructuredOutputCompatible(schema, originalRequired = []) {
|
|
21
|
-
const result = { ...schema };
|
|
22
|
-
if (result.type === "object" && result.properties) {
|
|
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
|
-
properties[propName] = makeGrokStructuredOutputCompatible(
|
|
30
|
-
prop,
|
|
31
|
-
prop.required || []
|
|
32
|
-
);
|
|
33
|
-
} else if (prop.type === "array" && prop.items) {
|
|
34
|
-
properties[propName] = {
|
|
35
|
-
...prop,
|
|
36
|
-
items: makeGrokStructuredOutputCompatible(
|
|
37
|
-
prop.items,
|
|
38
|
-
prop.items.required || []
|
|
39
|
-
)
|
|
40
|
-
};
|
|
41
|
-
} else if (wasOptional) {
|
|
42
|
-
if (prop.type && !Array.isArray(prop.type)) {
|
|
43
|
-
properties[propName] = {
|
|
44
|
-
...prop,
|
|
45
|
-
type: [prop.type, "null"]
|
|
46
|
-
};
|
|
47
|
-
} else if (Array.isArray(prop.type) && !prop.type.includes("null")) {
|
|
48
|
-
properties[propName] = {
|
|
49
|
-
...prop,
|
|
50
|
-
type: [...prop.type, "null"]
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
result.properties = properties;
|
|
56
|
-
result.required = allPropertyNames;
|
|
57
|
-
result.additionalProperties = false;
|
|
58
|
-
}
|
|
59
|
-
if (result.type === "array" && result.items) {
|
|
60
|
-
result.items = makeGrokStructuredOutputCompatible(
|
|
61
|
-
result.items,
|
|
62
|
-
result.items.required || []
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
return result;
|
|
66
|
-
}
|
|
67
|
-
export {
|
|
68
|
-
makeGrokStructuredOutputCompatible,
|
|
69
|
-
transformNullsToUndefined
|
|
70
|
-
};
|
|
71
|
-
//# sourceMappingURL=schema-converter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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 Grok's structured output (via OpenAI-compatible API) requires all fields to be\n * in the `required` array, with optional fields made nullable (type: [\"string\", \"null\"]).\n * When Grok returns null for optional fields, we need to convert them back to\n * undefined to match the original Zod schema expectations.\n *\n * @param obj - Object to transform\n * @returns Object with nulls converted to undefined\n */\nexport function transformNullsToUndefined<T>(obj: T): T {\n if (obj === null) {\n return undefined as unknown as T\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => transformNullsToUndefined(item)) as unknown as T\n }\n\n if (typeof obj === 'object') {\n const result: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {\n const transformed = transformNullsToUndefined(value)\n // Only include the key if the value is not undefined\n // This makes { notes: null } become {} (field absent) instead of { notes: undefined }\n if (transformed !== undefined) {\n result[key] = transformed\n }\n }\n return result as T\n }\n\n return obj\n}\n\n/**\n * Transform a JSON schema to be compatible with Grok's structured output requirements (OpenAI-compatible).\n * Grok 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 *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with Grok structured output\n */\nexport function makeGrokStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired: Array<string> = [],\n): Record<string, any> {\n const result = { ...schema }\n\n // Handle object types\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n // Transform each property\n for (const propName of allPropertyNames) {\n const prop = properties[propName]\n const wasOptional = !originalRequired.includes(propName)\n\n // Recursively transform nested objects/arrays\n if (prop.type === 'object' && prop.properties) {\n properties[propName] = makeGrokStructuredOutputCompatible(\n prop,\n prop.required || [],\n )\n } else if (prop.type === 'array' && prop.items) {\n properties[propName] = {\n ...prop,\n items: makeGrokStructuredOutputCompatible(\n prop.items,\n prop.items.required || [],\n ),\n }\n } else if (wasOptional) {\n // Make optional fields nullable by adding null to the type\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 }\n }\n }\n\n result.properties = properties\n // ALL properties must be required for Grok structured output\n result.required = allPropertyNames\n // additionalProperties must be false\n result.additionalProperties = false\n }\n\n // Handle array types with object items\n if (result.type === 'array' && result.items) {\n result.items = makeGrokStructuredOutputCompatible(\n result.items,\n result.items.required || [],\n )\n }\n\n return result\n}\n"],"names":[],"mappings":"AAWO,SAAS,0BAA6B,KAAW;AACtD,MAAI,QAAQ,MAAM;AAChB,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,WAAO,IAAI,IAAI,CAAC,SAAS,0BAA0B,IAAI,CAAC;AAAA,EAC1D;AAEA,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,SAAkC,CAAA;AACxC,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAA8B,GAAG;AACzE,YAAM,cAAc,0BAA0B,KAAK;AAGnD,UAAI,gBAAgB,QAAW;AAC7B,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAaO,SAAS,mCACd,QACA,mBAAkC,IACb;AACrB,QAAM,SAAS,EAAE,GAAG,OAAA;AAGpB,MAAI,OAAO,SAAS,YAAY,OAAO,YAAY;AACjD,UAAM,aAAa,EAAE,GAAG,OAAO,WAAA;AAC/B,UAAM,mBAAmB,OAAO,KAAK,UAAU;AAG/C,eAAW,YAAY,kBAAkB;AACvC,YAAM,OAAO,WAAW,QAAQ;AAChC,YAAM,cAAc,CAAC,iBAAiB,SAAS,QAAQ;AAGvD,UAAI,KAAK,SAAS,YAAY,KAAK,YAAY;AAC7C,mBAAW,QAAQ,IAAI;AAAA,UACrB;AAAA,UACA,KAAK,YAAY,CAAA;AAAA,QAAC;AAAA,MAEtB,WAAW,KAAK,SAAS,WAAW,KAAK,OAAO;AAC9C,mBAAW,QAAQ,IAAI;AAAA,UACrB,GAAG;AAAA,UACH,OAAO;AAAA,YACL,KAAK;AAAA,YACL,KAAK,MAAM,YAAY,CAAA;AAAA,UAAC;AAAA,QAC1B;AAAA,MAEJ,WAAW,aAAa;AAEtB,YAAI,KAAK,QAAQ,CAAC,MAAM,QAAQ,KAAK,IAAI,GAAG;AAC1C,qBAAW,QAAQ,IAAI;AAAA,YACrB,GAAG;AAAA,YACH,MAAM,CAAC,KAAK,MAAM,MAAM;AAAA,UAAA;AAAA,QAE5B,WAAW,MAAM,QAAQ,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,MAAM,GAAG;AAClE,qBAAW,QAAQ,IAAI;AAAA,YACrB,GAAG;AAAA,YACH,MAAM,CAAC,GAAG,KAAK,MAAM,MAAM;AAAA,UAAA;AAAA,QAE/B;AAAA,MACF;AAAA,IACF;AAEA,WAAO,aAAa;AAEpB,WAAO,WAAW;AAElB,WAAO,uBAAuB;AAAA,EAChC;AAGA,MAAI,OAAO,SAAS,WAAW,OAAO,OAAO;AAC3C,WAAO,QAAQ;AAAA,MACb,OAAO;AAAA,MACP,OAAO,MAAM,YAAY,CAAA;AAAA,IAAC;AAAA,EAE9B;AAEA,SAAO;AACT;"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { makeGrokStructuredOutputCompatible } from '../utils/schema-converter'
|
|
2
|
-
import type { JSONSchema, Tool } from '@tanstack/ai'
|
|
3
|
-
import type OpenAI from 'openai'
|
|
4
|
-
|
|
5
|
-
// Use Chat Completions API tool format (not Responses API)
|
|
6
|
-
export type FunctionTool = OpenAI.Chat.Completions.ChatCompletionTool
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Converts a standard Tool to Grok ChatCompletionTool format.
|
|
10
|
-
*
|
|
11
|
-
* Tool schemas are already converted to JSON Schema in the ai layer.
|
|
12
|
-
* We apply Grok-specific transformations for strict mode:
|
|
13
|
-
* - All properties in required array
|
|
14
|
-
* - Optional fields made nullable
|
|
15
|
-
* - additionalProperties: false
|
|
16
|
-
*
|
|
17
|
-
* This enables strict mode for all tools automatically.
|
|
18
|
-
*/
|
|
19
|
-
export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
|
|
20
|
-
// Tool schemas are already converted to JSON Schema in the ai layer
|
|
21
|
-
// Apply Grok-specific transformations for strict mode
|
|
22
|
-
const inputSchema = (tool.inputSchema ?? {
|
|
23
|
-
type: 'object',
|
|
24
|
-
properties: {},
|
|
25
|
-
required: [],
|
|
26
|
-
}) as JSONSchema
|
|
27
|
-
|
|
28
|
-
const jsonSchema = makeGrokStructuredOutputCompatible(
|
|
29
|
-
inputSchema,
|
|
30
|
-
inputSchema.required || [],
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
// Ensure additionalProperties is false for strict mode
|
|
34
|
-
jsonSchema.additionalProperties = false
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
type: 'function',
|
|
38
|
-
function: {
|
|
39
|
-
name: tool.name,
|
|
40
|
-
description: tool.description,
|
|
41
|
-
parameters: jsonSchema,
|
|
42
|
-
strict: true, // Always use strict mode since our schema converter handles the requirements
|
|
43
|
-
},
|
|
44
|
-
} satisfies FunctionTool
|
|
45
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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 Grok-specific format
|
|
7
|
-
* Grok uses OpenAI-compatible API, so we primarily support function tools
|
|
8
|
-
*/
|
|
9
|
-
export function convertToolsToProviderFormat(
|
|
10
|
-
tools: Array<Tool>,
|
|
11
|
-
): Array<FunctionTool> {
|
|
12
|
-
return tools.map((tool) => {
|
|
13
|
-
// For Grok, all tools are converted as function tools
|
|
14
|
-
// Grok uses OpenAI-compatible API which primarily supports function tools
|
|
15
|
-
return convertFunctionToolToAdapterFormat(tool)
|
|
16
|
-
})
|
|
17
|
-
}
|