@tanstack/ai-grok 0.7.1 → 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 +5 -5
- 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
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleImageAdapter } from '@tanstack/openai-base';
|
|
2
2
|
import { GrokImageModel } from '../model-meta.js';
|
|
3
3
|
import { GrokImageModelProviderOptionsByName, GrokImageModelSizeByName, GrokImageProviderOptions } from '../image/image-provider-options.js';
|
|
4
|
-
import { ImageGenerationOptions, ImageGenerationResult } from '@tanstack/ai';
|
|
5
4
|
import { GrokClientConfig } from '../utils.js';
|
|
6
5
|
/**
|
|
7
6
|
* Configuration for Grok image adapter
|
|
@@ -19,14 +18,16 @@ export interface GrokImageConfig extends GrokClientConfig {
|
|
|
19
18
|
* - Size validation per model
|
|
20
19
|
* - Number of images validation
|
|
21
20
|
*/
|
|
22
|
-
export declare class GrokImageAdapter<TModel extends GrokImageModel> extends
|
|
21
|
+
export declare class GrokImageAdapter<TModel extends GrokImageModel> extends OpenAICompatibleImageAdapter<TModel, GrokImageProviderOptions, GrokImageModelProviderOptionsByName, GrokImageModelSizeByName> {
|
|
23
22
|
readonly kind: "image";
|
|
24
23
|
readonly name: "grok";
|
|
25
|
-
private client;
|
|
26
24
|
constructor(config: GrokImageConfig, model: TModel);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
protected validatePrompt(options: {
|
|
26
|
+
prompt: string;
|
|
27
|
+
model: string;
|
|
28
|
+
}): void;
|
|
29
|
+
protected validateImageSize(model: string, size: string | undefined): void;
|
|
30
|
+
protected validateNumberOfImages(model: string, numberOfImages: number | undefined): void;
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* Creates a Grok image adapter with explicit API key.
|
|
@@ -1,70 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { OpenAICompatibleImageAdapter } from "@tanstack/openai-base";
|
|
2
|
+
import { withGrokDefaults, getGrokApiKeyFromEnv } from "../utils/client.js";
|
|
3
3
|
import { validatePrompt, validateImageSize, validateNumberOfImages } from "../image/image-provider-options.js";
|
|
4
|
-
class GrokImageAdapter extends
|
|
4
|
+
class GrokImageAdapter extends OpenAICompatibleImageAdapter {
|
|
5
5
|
constructor(config, model) {
|
|
6
|
-
super(model,
|
|
6
|
+
super(withGrokDefaults(config), model, "grok");
|
|
7
7
|
this.kind = "image";
|
|
8
8
|
this.name = "grok";
|
|
9
|
-
this.client = createGrokClient(config);
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
logger.request(`activity=generateImage provider=grok model=${this.model}`, {
|
|
14
|
-
provider: "grok",
|
|
15
|
-
model: this.model
|
|
16
|
-
});
|
|
17
|
-
try {
|
|
18
|
-
validatePrompt({ prompt, model });
|
|
19
|
-
validateImageSize(model, size);
|
|
20
|
-
validateNumberOfImages(model, numberOfImages);
|
|
21
|
-
const request = this.buildRequest(options);
|
|
22
|
-
const response = await this.client.images.generate({
|
|
23
|
-
...request,
|
|
24
|
-
stream: false
|
|
25
|
-
});
|
|
26
|
-
return this.transformResponse(model, response);
|
|
27
|
-
} catch (error) {
|
|
28
|
-
logger.errors("grok.generateImage fatal", {
|
|
29
|
-
error,
|
|
30
|
-
source: "grok.generateImage"
|
|
31
|
-
});
|
|
32
|
-
throw error;
|
|
33
|
-
}
|
|
10
|
+
validatePrompt(options) {
|
|
11
|
+
validatePrompt(options);
|
|
34
12
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
...modelOptions,
|
|
39
|
-
model,
|
|
40
|
-
prompt,
|
|
41
|
-
n: numberOfImages ?? 1,
|
|
42
|
-
size
|
|
43
|
-
};
|
|
13
|
+
validateImageSize(model, size) {
|
|
14
|
+
validateImageSize(model, size);
|
|
44
15
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(item) => {
|
|
48
|
-
const revisedPrompt = item.revised_prompt;
|
|
49
|
-
if (item.b64_json) {
|
|
50
|
-
return [{ b64Json: item.b64_json, revisedPrompt }];
|
|
51
|
-
}
|
|
52
|
-
if (item.url) {
|
|
53
|
-
return [{ url: item.url, revisedPrompt }];
|
|
54
|
-
}
|
|
55
|
-
return [];
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
return {
|
|
59
|
-
id: generateId(this.name),
|
|
60
|
-
model,
|
|
61
|
-
images,
|
|
62
|
-
usage: response.usage ? {
|
|
63
|
-
inputTokens: response.usage.input_tokens,
|
|
64
|
-
outputTokens: response.usage.output_tokens,
|
|
65
|
-
totalTokens: response.usage.total_tokens
|
|
66
|
-
} : void 0
|
|
67
|
-
};
|
|
16
|
+
validateNumberOfImages(model, numberOfImages) {
|
|
17
|
+
validateNumberOfImages(model, numberOfImages);
|
|
68
18
|
}
|
|
69
19
|
}
|
|
70
20
|
function createGrokImage(model, apiKey, config) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { OpenAICompatibleImageAdapter } from '@tanstack/openai-base'\nimport { getGrokApiKeyFromEnv, withGrokDefaults } from '../utils/client'\nimport {\n validateImageSize,\n validateNumberOfImages,\n validatePrompt,\n} from '../image/image-provider-options'\nimport type { GrokImageModel } from '../model-meta'\nimport type {\n GrokImageModelProviderOptionsByName,\n GrokImageModelSizeByName,\n GrokImageProviderOptions,\n} from '../image/image-provider-options'\nimport type { GrokClientConfig } from '../utils'\n\n/**\n * Configuration for Grok image adapter\n */\nexport interface GrokImageConfig extends GrokClientConfig {}\n\n/**\n * Grok Image Generation Adapter\n *\n * Tree-shakeable adapter for Grok image generation functionality.\n * Supports grok-2-image-1212 model.\n *\n * Features:\n * - Model-specific type-safe provider options\n * - Size validation per model\n * - Number of images validation\n */\nexport class GrokImageAdapter<\n TModel extends GrokImageModel,\n> extends OpenAICompatibleImageAdapter<\n TModel,\n GrokImageProviderOptions,\n GrokImageModelProviderOptionsByName,\n GrokImageModelSizeByName\n> {\n readonly kind = 'image' as const\n readonly name = 'grok' as const\n\n constructor(config: GrokImageConfig, model: TModel) {\n super(withGrokDefaults(config), model, 'grok')\n }\n\n protected override validatePrompt(options: {\n prompt: string\n model: string\n }): void {\n validatePrompt(options)\n }\n\n protected override validateImageSize(\n model: string,\n size: string | undefined,\n ): void {\n validateImageSize(model, size)\n }\n\n protected override validateNumberOfImages(\n model: string,\n numberOfImages: number | undefined,\n ): void {\n validateNumberOfImages(model, numberOfImages)\n }\n}\n\n/**\n * Creates a Grok image adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'grok-2-image-1212')\n * @param apiKey - Your xAI API key\n * @param config - Optional additional configuration\n * @returns Configured Grok image adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createGrokImage('grok-2-image-1212', \"xai-...\");\n *\n * const result = await generateImage({\n * adapter,\n * prompt: 'A cute baby sea otter'\n * });\n * ```\n */\nexport function createGrokImage<TModel extends GrokImageModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<GrokImageConfig, 'apiKey'>,\n): GrokImageAdapter<TModel> {\n return new GrokImageAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Grok image adapter with automatic API key detection from environment variables.\n * Type resolution happens here at the call site.\n *\n * Looks for `XAI_API_KEY` in:\n * - `process.env` (Node.js)\n * - `window.env` (Browser with injected env)\n *\n * @param model - The model name (e.g., 'grok-2-image-1212')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Grok image adapter instance with resolved types\n * @throws Error if XAI_API_KEY is not found in environment\n *\n * @example\n * ```typescript\n * // Automatically uses XAI_API_KEY from environment\n * const adapter = grokImage('grok-2-image-1212');\n *\n * const result = await generateImage({\n * adapter,\n * prompt: 'A beautiful sunset over mountains'\n * });\n * ```\n */\nexport function grokImage<TModel extends GrokImageModel>(\n model: TModel,\n config?: Omit<GrokImageConfig, 'apiKey'>,\n): GrokImageAdapter<TModel> {\n const apiKey = getGrokApiKeyFromEnv()\n return createGrokImage(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;AA+BO,MAAM,yBAEH,6BAKR;AAAA,EAIA,YAAY,QAAyB,OAAe;AAClD,UAAM,iBAAiB,MAAM,GAAG,OAAO,MAAM;AAJ/C,SAAS,OAAO;AAChB,SAAS,OAAO;AAAA,EAIhB;AAAA,EAEmB,eAAe,SAGzB;AACP,mBAAe,OAAO;AAAA,EACxB;AAAA,EAEmB,kBACjB,OACA,MACM;AACN,sBAAkB,OAAO,IAAI;AAAA,EAC/B;AAAA,EAEmB,uBACjB,OACA,gBACM;AACN,2BAAuB,OAAO,cAAc;AAAA,EAC9C;AACF;AAqBO,SAAS,gBACd,OACA,QACA,QAC0B;AAC1B,SAAO,IAAI,iBAAiB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AAC1D;AA0BO,SAAS,UACd,OACA,QAC0B;AAC1B,QAAM,SAAS,qBAAA;AACf,SAAO,gBAAgB,OAAO,QAAQ,MAAM;AAC9C;"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleSummarizeAdapter } from '@tanstack/openai-base';
|
|
2
2
|
import { GROK_CHAT_MODELS } from '../model-meta.js';
|
|
3
|
-
import { StreamChunk, SummarizationOptions, SummarizationResult } from '@tanstack/ai';
|
|
4
3
|
import { GrokClientConfig } from '../utils.js';
|
|
5
4
|
/**
|
|
6
5
|
* Configuration for Grok summarize adapter
|
|
@@ -24,14 +23,10 @@ export type GrokSummarizeModel = (typeof GROK_CHAT_MODELS)[number];
|
|
|
24
23
|
* A thin wrapper around the text adapter that adds summarization-specific prompting.
|
|
25
24
|
* Delegates all API calls to the GrokTextAdapter.
|
|
26
25
|
*/
|
|
27
|
-
export declare class GrokSummarizeAdapter<TModel extends GrokSummarizeModel> extends
|
|
26
|
+
export declare class GrokSummarizeAdapter<TModel extends GrokSummarizeModel> extends OpenAICompatibleSummarizeAdapter<TModel, GrokSummarizeProviderOptions> {
|
|
28
27
|
readonly kind: "summarize";
|
|
29
28
|
readonly name: "grok";
|
|
30
|
-
private textAdapter;
|
|
31
29
|
constructor(config: GrokSummarizeConfig, model: TModel);
|
|
32
|
-
summarize(options: SummarizationOptions): Promise<SummarizationResult>;
|
|
33
|
-
summarizeStream(options: SummarizationOptions): AsyncIterable<StreamChunk>;
|
|
34
|
-
private buildSummarizationPrompt;
|
|
35
30
|
}
|
|
36
31
|
/**
|
|
37
32
|
* Creates a Grok summarize adapter with explicit API key.
|
|
@@ -1,103 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleSummarizeAdapter } from "@tanstack/openai-base";
|
|
2
|
+
import "@tanstack/ai-utils";
|
|
2
3
|
import { getGrokApiKeyFromEnv } from "../utils/client.js";
|
|
3
4
|
import { GrokTextAdapter } from "./text.js";
|
|
4
|
-
class GrokSummarizeAdapter extends
|
|
5
|
+
class GrokSummarizeAdapter extends OpenAICompatibleSummarizeAdapter {
|
|
5
6
|
constructor(config, model) {
|
|
6
|
-
super(
|
|
7
|
+
super(
|
|
8
|
+
new GrokTextAdapter(
|
|
9
|
+
config,
|
|
10
|
+
model
|
|
11
|
+
),
|
|
12
|
+
model,
|
|
13
|
+
"grok"
|
|
14
|
+
);
|
|
7
15
|
this.kind = "summarize";
|
|
8
16
|
this.name = "grok";
|
|
9
|
-
this.textAdapter = new GrokTextAdapter(config, model);
|
|
10
|
-
}
|
|
11
|
-
async summarize(options) {
|
|
12
|
-
const { logger } = options;
|
|
13
|
-
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
14
|
-
logger.request(`activity=summarize provider=grok`, {
|
|
15
|
-
provider: "grok",
|
|
16
|
-
model: options.model
|
|
17
|
-
});
|
|
18
|
-
let summary = "";
|
|
19
|
-
const id = "";
|
|
20
|
-
let model = options.model;
|
|
21
|
-
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
22
|
-
try {
|
|
23
|
-
for await (const chunk of this.textAdapter.chatStream({
|
|
24
|
-
model: options.model,
|
|
25
|
-
messages: [{ role: "user", content: options.text }],
|
|
26
|
-
systemPrompts: [systemPrompt],
|
|
27
|
-
maxTokens: options.maxLength,
|
|
28
|
-
temperature: 0.3,
|
|
29
|
-
logger
|
|
30
|
-
})) {
|
|
31
|
-
if (chunk.type === "TEXT_MESSAGE_CONTENT") {
|
|
32
|
-
if (chunk.content) {
|
|
33
|
-
summary = chunk.content;
|
|
34
|
-
} else {
|
|
35
|
-
summary += chunk.delta;
|
|
36
|
-
}
|
|
37
|
-
model = chunk.model || model;
|
|
38
|
-
}
|
|
39
|
-
if (chunk.type === "RUN_FINISHED") {
|
|
40
|
-
if (chunk.usage) {
|
|
41
|
-
usage = chunk.usage;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
} catch (error) {
|
|
46
|
-
logger.errors("grok.summarize fatal", {
|
|
47
|
-
error,
|
|
48
|
-
source: "grok.summarize"
|
|
49
|
-
});
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
|
-
return { id, model, summary, usage };
|
|
53
|
-
}
|
|
54
|
-
async *summarizeStream(options) {
|
|
55
|
-
const { logger } = options;
|
|
56
|
-
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
57
|
-
logger.request(`activity=summarize provider=grok`, {
|
|
58
|
-
provider: "grok",
|
|
59
|
-
model: options.model,
|
|
60
|
-
stream: true
|
|
61
|
-
});
|
|
62
|
-
try {
|
|
63
|
-
yield* this.textAdapter.chatStream({
|
|
64
|
-
model: options.model,
|
|
65
|
-
messages: [{ role: "user", content: options.text }],
|
|
66
|
-
systemPrompts: [systemPrompt],
|
|
67
|
-
maxTokens: options.maxLength,
|
|
68
|
-
temperature: 0.3,
|
|
69
|
-
logger
|
|
70
|
-
});
|
|
71
|
-
} catch (error) {
|
|
72
|
-
logger.errors("grok.summarize fatal", {
|
|
73
|
-
error,
|
|
74
|
-
source: "grok.summarize"
|
|
75
|
-
});
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
buildSummarizationPrompt(options) {
|
|
80
|
-
let prompt = "You are a professional summarizer. ";
|
|
81
|
-
switch (options.style) {
|
|
82
|
-
case "bullet-points":
|
|
83
|
-
prompt += "Provide a summary in bullet point format. ";
|
|
84
|
-
break;
|
|
85
|
-
case "paragraph":
|
|
86
|
-
prompt += "Provide a summary in paragraph format. ";
|
|
87
|
-
break;
|
|
88
|
-
case "concise":
|
|
89
|
-
prompt += "Provide a very concise summary in 1-2 sentences. ";
|
|
90
|
-
break;
|
|
91
|
-
default:
|
|
92
|
-
prompt += "Provide a clear and concise summary. ";
|
|
93
|
-
}
|
|
94
|
-
if (options.focus && options.focus.length > 0) {
|
|
95
|
-
prompt += `Focus on the following aspects: ${options.focus.join(", ")}. `;
|
|
96
|
-
}
|
|
97
|
-
if (options.maxLength) {
|
|
98
|
-
prompt += `Keep the summary under ${options.maxLength} tokens. `;
|
|
99
|
-
}
|
|
100
|
-
return prompt;
|
|
101
17
|
}
|
|
102
18
|
}
|
|
103
19
|
function createGrokSummarize(model, apiKey, config) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import { OpenAICompatibleSummarizeAdapter } from '@tanstack/openai-base'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport { GrokTextAdapter } from './text'\nimport type { ChatStreamCapable } from '@tanstack/openai-base'\nimport type { GROK_CHAT_MODELS } from '../model-meta'\nimport type { GrokClientConfig } from '../utils'\n\n/**\n * Configuration for Grok summarize adapter\n */\nexport interface GrokSummarizeConfig extends GrokClientConfig {}\n\n/**\n * Grok-specific provider options for summarization\n */\nexport interface GrokSummarizeProviderOptions {\n /** Temperature for response generation (0-2) */\n temperature?: number\n /** Maximum tokens in the response */\n maxTokens?: number\n}\n\n/** Model type for Grok summarization */\nexport type GrokSummarizeModel = (typeof GROK_CHAT_MODELS)[number]\n\n/**\n * Grok Summarize Adapter\n *\n * A thin wrapper around the text adapter that adds summarization-specific prompting.\n * Delegates all API calls to the GrokTextAdapter.\n */\nexport class GrokSummarizeAdapter<\n TModel extends GrokSummarizeModel,\n> extends OpenAICompatibleSummarizeAdapter<\n TModel,\n GrokSummarizeProviderOptions\n> {\n readonly kind = 'summarize' as const\n readonly name = 'grok' as const\n\n constructor(config: GrokSummarizeConfig, model: TModel) {\n // The text adapter accepts richer provider options than the summarize adapter needs,\n // but we only pass basic options (model, messages, systemPrompts, etc.) at call time.\n super(\n new GrokTextAdapter(\n config,\n model,\n ) as unknown as ChatStreamCapable<GrokSummarizeProviderOptions>,\n model,\n 'grok',\n )\n }\n}\n\n/**\n * Creates a Grok summarize adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'grok-3', 'grok-4')\n * @param apiKey - Your xAI API key\n * @param config - Optional additional configuration\n * @returns Configured Grok summarize adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createGrokSummarize('grok-3', \"xai-...\");\n * ```\n */\nexport function createGrokSummarize<TModel extends GrokSummarizeModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<GrokSummarizeConfig, 'apiKey'>,\n): GrokSummarizeAdapter<TModel> {\n return new GrokSummarizeAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Grok summarize adapter with automatic API key detection from environment variables.\n * Type resolution happens here at the call site.\n *\n * Looks for `XAI_API_KEY` in:\n * - `process.env` (Node.js)\n * - `window.env` (Browser with injected env)\n *\n * @param model - The model name (e.g., 'grok-3', 'grok-4')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Grok summarize adapter instance with resolved types\n * @throws Error if XAI_API_KEY is not found in environment\n *\n * @example\n * ```typescript\n * // Automatically uses XAI_API_KEY from environment\n * const adapter = grokSummarize('grok-3');\n *\n * await summarize({\n * adapter,\n * text: \"Long article text...\"\n * });\n * ```\n */\nexport function grokSummarize<TModel extends GrokSummarizeModel>(\n model: TModel,\n config?: Omit<GrokSummarizeConfig, 'apiKey'>,\n): GrokSummarizeAdapter<TModel> {\n const apiKey = getGrokApiKeyFromEnv()\n return createGrokSummarize(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;AA+BO,MAAM,6BAEH,iCAGR;AAAA,EAIA,YAAY,QAA6B,OAAe;AAGtD;AAAA,MACE,IAAI;AAAA,QACF;AAAA,QACA;AAAA,MAAA;AAAA,MAEF;AAAA,MACA;AAAA,IAAA;AAZJ,SAAS,OAAO;AAChB,SAAS,OAAO;AAAA,EAahB;AACF;AAgBO,SAAS,oBACd,OACA,QACA,QAC8B;AAC9B,SAAO,IAAI,qBAAqB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AAC9D;AA0BO,SAAS,cACd,OACA,QAC8B;AAC9B,QAAM,SAAS,qBAAA;AACf,SAAO,oBAAoB,OAAO,QAAQ,MAAM;AAClD;"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAICompatibleChatCompletionsTextAdapter } from '@tanstack/openai-base';
|
|
2
2
|
import { GROK_CHAT_MODELS, GrokChatModelToolCapabilitiesByName, ResolveInputModalities, ResolveProviderOptions } from '../model-meta.js';
|
|
3
|
-
import { Modality
|
|
4
|
-
import { ExternalTextProviderOptions as GrokTextProviderOptions } from '../text/text-provider-options.js';
|
|
3
|
+
import { Modality } from '@tanstack/ai';
|
|
5
4
|
import { GrokMessageMetadataByModality } from '../message-types.js';
|
|
6
5
|
import { GrokClientConfig } from '../utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Resolve tool capabilities for a specific Grok model.
|
|
8
|
+
*/
|
|
7
9
|
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof GrokChatModelToolCapabilitiesByName ? NonNullable<GrokChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
8
10
|
/**
|
|
9
11
|
* Configuration for Grok text adapter
|
|
@@ -19,41 +21,15 @@ export type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../
|
|
|
19
21
|
*
|
|
20
22
|
* Tree-shakeable adapter for Grok chat/text completion functionality.
|
|
21
23
|
* Uses OpenAI-compatible Chat Completions API (not Responses API).
|
|
24
|
+
*
|
|
25
|
+
* Delegates implementation to {@link OpenAICompatibleChatCompletionsTextAdapter}
|
|
26
|
+
* from `@tanstack/openai-base` and threads Grok-specific tool-capability typing
|
|
27
|
+
* through the 5th generic of the base class.
|
|
22
28
|
*/
|
|
23
|
-
export declare class GrokTextAdapter<TModel extends (typeof GROK_CHAT_MODELS)[number], TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>, TToolCapabilities extends ReadonlyArray<string> = ResolveToolCapabilities<TModel>> extends
|
|
29
|
+
export declare class GrokTextAdapter<TModel extends (typeof GROK_CHAT_MODELS)[number], TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>, TToolCapabilities extends ReadonlyArray<string> = ResolveToolCapabilities<TModel>> extends OpenAICompatibleChatCompletionsTextAdapter<TModel, TProviderOptions, TInputModalities, GrokMessageMetadataByModality, TToolCapabilities> {
|
|
24
30
|
readonly kind: "text";
|
|
25
31
|
readonly name: "grok";
|
|
26
|
-
private client;
|
|
27
32
|
constructor(config: GrokTextConfig, model: TModel);
|
|
28
|
-
chatStream(options: TextOptions<GrokTextProviderOptions>): AsyncIterable<StreamChunk>;
|
|
29
|
-
/**
|
|
30
|
-
* Generate structured output using Grok's JSON Schema response format.
|
|
31
|
-
* Uses stream: false to get the complete response in one call.
|
|
32
|
-
*
|
|
33
|
-
* Grok has strict requirements for structured output (via OpenAI-compatible API):
|
|
34
|
-
* - All properties must be in the `required` array
|
|
35
|
-
* - Optional fields should have null added to their type union
|
|
36
|
-
* - additionalProperties must be false for all objects
|
|
37
|
-
*
|
|
38
|
-
* The outputSchema is already JSON Schema (converted in the ai layer).
|
|
39
|
-
* We apply Grok-specific transformations for structured output compatibility.
|
|
40
|
-
*/
|
|
41
|
-
structuredOutput(options: StructuredOutputOptions<GrokTextProviderOptions>): Promise<StructuredOutputResult<unknown>>;
|
|
42
|
-
private processGrokStreamChunks;
|
|
43
|
-
/**
|
|
44
|
-
* Maps common options to Grok-specific Chat Completions format
|
|
45
|
-
*/
|
|
46
|
-
private mapTextOptionsToGrok;
|
|
47
|
-
private convertMessageToGrok;
|
|
48
|
-
/**
|
|
49
|
-
* Normalizes message content to an array of ContentPart.
|
|
50
|
-
* Handles backward compatibility with string content.
|
|
51
|
-
*/
|
|
52
|
-
private normalizeContent;
|
|
53
|
-
/**
|
|
54
|
-
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
55
|
-
*/
|
|
56
|
-
private extractTextContent;
|
|
57
33
|
}
|
|
58
34
|
/**
|
|
59
35
|
* Creates a Grok text adapter with explicit API key.
|