@tanstack/ai-grok 0.6.6 → 0.6.8
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.js +21 -9
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/summarize.js +51 -22
- package/dist/esm/adapters/summarize.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +7 -5
- package/dist/esm/adapters/text.js +25 -10
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/model-meta.d.ts +30 -0
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/tools/index.js +7 -0
- package/dist/esm/tools/index.js.map +1 -0
- package/package.json +6 -2
- package/src/adapters/image.ts +25 -12
- package/src/adapters/summarize.ts +56 -25
- package/src/adapters/text.ts +48 -15
- package/src/index.ts +1 -0
- package/src/model-meta.ts +32 -0
|
@@ -9,16 +9,28 @@ class GrokImageAdapter extends BaseImageAdapter {
|
|
|
9
9
|
this.client = createGrokClient(config);
|
|
10
10
|
}
|
|
11
11
|
async generateImages(options) {
|
|
12
|
-
const { model, prompt, numberOfImages, size } = options;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const request = this.buildRequest(options);
|
|
17
|
-
const response = await this.client.images.generate({
|
|
18
|
-
...request,
|
|
19
|
-
stream: false
|
|
12
|
+
const { model, prompt, numberOfImages, size, logger } = options;
|
|
13
|
+
logger.request(`activity=generateImage provider=grok model=${this.model}`, {
|
|
14
|
+
provider: "grok",
|
|
15
|
+
model: this.model
|
|
20
16
|
});
|
|
21
|
-
|
|
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
|
+
}
|
|
22
34
|
}
|
|
23
35
|
buildRequest(options) {
|
|
24
36
|
const { model, prompt, numberOfImages, size, modelOptions } = options;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport { createGrokClient, generateId, getGrokApiKeyFromEnv } from '../utils'\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 {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n} from '@tanstack/ai'\nimport type OpenAI_SDK from 'openai'\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 BaseImageAdapter<\n TModel,\n GrokImageProviderOptions,\n GrokImageModelProviderOptionsByName,\n GrokImageModelSizeByName\n> {\n readonly kind = 'image' as const\n readonly name = 'grok' as const\n\n private client: OpenAI_SDK\n\n constructor(config: GrokImageConfig, model: TModel) {\n super({}, model)\n this.client = createGrokClient(config)\n }\n\n async generateImages(\n options: ImageGenerationOptions<GrokImageProviderOptions>,\n ): Promise<ImageGenerationResult> {\n const { model, prompt, numberOfImages, size } = options\n\n // Validate inputs\n
|
|
1
|
+
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport { createGrokClient, generateId, getGrokApiKeyFromEnv } from '../utils'\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 {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n} from '@tanstack/ai'\nimport type OpenAI_SDK from 'openai'\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 BaseImageAdapter<\n TModel,\n GrokImageProviderOptions,\n GrokImageModelProviderOptionsByName,\n GrokImageModelSizeByName\n> {\n readonly kind = 'image' as const\n readonly name = 'grok' as const\n\n private client: OpenAI_SDK\n\n constructor(config: GrokImageConfig, model: TModel) {\n super({}, model)\n this.client = createGrokClient(config)\n }\n\n async generateImages(\n options: ImageGenerationOptions<GrokImageProviderOptions>,\n ): Promise<ImageGenerationResult> {\n const { model, prompt, numberOfImages, size, logger } = options\n\n logger.request(`activity=generateImage provider=grok model=${this.model}`, {\n provider: 'grok',\n model: this.model,\n })\n\n try {\n // Validate inputs\n validatePrompt({ prompt, model })\n validateImageSize(model, size)\n validateNumberOfImages(model, numberOfImages)\n\n // Build request based on model type\n const request = this.buildRequest(options)\n\n const response = await this.client.images.generate({\n ...request,\n stream: false,\n })\n\n return this.transformResponse(model, response)\n } catch (error) {\n logger.errors('grok.generateImage fatal', {\n error,\n source: 'grok.generateImage',\n })\n throw error\n }\n }\n\n private buildRequest(\n options: ImageGenerationOptions<GrokImageProviderOptions>,\n ): OpenAI_SDK.Images.ImageGenerateParams {\n const { model, prompt, numberOfImages, size, modelOptions } = options\n\n return {\n model,\n prompt,\n n: numberOfImages ?? 1,\n size: size as OpenAI_SDK.Images.ImageGenerateParams['size'],\n ...modelOptions,\n }\n }\n\n private transformResponse(\n model: string,\n response: OpenAI_SDK.Images.ImagesResponse,\n ): ImageGenerationResult {\n const images: Array<GeneratedImage> = (response.data ?? []).map((item) => ({\n b64Json: item.b64_json,\n url: item.url,\n revisedPrompt: item.revised_prompt,\n }))\n\n return {\n id: generateId(this.name),\n model,\n images,\n usage: response.usage\n ? {\n inputTokens: response.usage.input_tokens,\n outputTokens: response.usage.output_tokens,\n totalTokens: response.usage.total_tokens,\n }\n : undefined,\n }\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":";;;AAqCO,MAAM,yBAEH,iBAKR;AAAA,EAMA,YAAY,QAAyB,OAAe;AAClD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,SAAS,iBAAiB,MAAM;AAAA,EACvC;AAAA,EAEA,MAAM,eACJ,SACgC;AAChC,UAAM,EAAE,OAAO,QAAQ,gBAAgB,MAAM,WAAW;AAExD,WAAO,QAAQ,8CAA8C,KAAK,KAAK,IAAI;AAAA,MACzE,UAAU;AAAA,MACV,OAAO,KAAK;AAAA,IAAA,CACb;AAED,QAAI;AAEF,qBAAe,EAAE,QAAQ,OAAO;AAChC,wBAAkB,OAAO,IAAI;AAC7B,6BAAuB,OAAO,cAAc;AAG5C,YAAM,UAAU,KAAK,aAAa,OAAO;AAEzC,YAAM,WAAW,MAAM,KAAK,OAAO,OAAO,SAAS;AAAA,QACjD,GAAG;AAAA,QACH,QAAQ;AAAA,MAAA,CACT;AAED,aAAO,KAAK,kBAAkB,OAAO,QAAQ;AAAA,IAC/C,SAAS,OAAO;AACd,aAAO,OAAO,4BAA4B;AAAA,QACxC;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEQ,aACN,SACuC;AACvC,UAAM,EAAE,OAAO,QAAQ,gBAAgB,MAAM,iBAAiB;AAE9D,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA,GAAG,kBAAkB;AAAA,MACrB;AAAA,MACA,GAAG;AAAA,IAAA;AAAA,EAEP;AAAA,EAEQ,kBACN,OACA,UACuB;AACvB,UAAM,UAAiC,SAAS,QAAQ,CAAA,GAAI,IAAI,CAAC,UAAU;AAAA,MACzE,SAAS,KAAK;AAAA,MACd,KAAK,KAAK;AAAA,MACV,eAAe,KAAK;AAAA,IAAA,EACpB;AAEF,WAAO;AAAA,MACL,IAAI,WAAW,KAAK,IAAI;AAAA,MACxB;AAAA,MACA;AAAA,MACA,OAAO,SAAS,QACZ;AAAA,QACE,aAAa,SAAS,MAAM;AAAA,QAC5B,cAAc,SAAS,MAAM;AAAA,QAC7B,aAAa,SAAS,MAAM;AAAA,MAAA,IAE9B;AAAA,IAAA;AAAA,EAER;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;"}
|
|
@@ -9,43 +9,72 @@ class GrokSummarizeAdapter extends BaseSummarizeAdapter {
|
|
|
9
9
|
this.textAdapter = new GrokTextAdapter(config, model);
|
|
10
10
|
}
|
|
11
11
|
async summarize(options) {
|
|
12
|
+
const { logger } = options;
|
|
12
13
|
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
14
|
+
logger.request(`activity=summarize provider=grok`, {
|
|
15
|
+
provider: "grok",
|
|
16
|
+
model: options.model
|
|
17
|
+
});
|
|
13
18
|
let summary = "";
|
|
14
19
|
const id = "";
|
|
15
20
|
let model = options.model;
|
|
16
21
|
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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;
|
|
29
38
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
usage = chunk.usage;
|
|
39
|
+
if (chunk.type === "RUN_FINISHED") {
|
|
40
|
+
if (chunk.usage) {
|
|
41
|
+
usage = chunk.usage;
|
|
42
|
+
}
|
|
35
43
|
}
|
|
36
44
|
}
|
|
45
|
+
} catch (error) {
|
|
46
|
+
logger.errors("grok.summarize fatal", {
|
|
47
|
+
error,
|
|
48
|
+
source: "grok.summarize"
|
|
49
|
+
});
|
|
50
|
+
throw error;
|
|
37
51
|
}
|
|
38
52
|
return { id, model, summary, usage };
|
|
39
53
|
}
|
|
40
54
|
async *summarizeStream(options) {
|
|
55
|
+
const { logger } = options;
|
|
41
56
|
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
42
|
-
|
|
57
|
+
logger.request(`activity=summarize provider=grok`, {
|
|
58
|
+
provider: "grok",
|
|
43
59
|
model: options.model,
|
|
44
|
-
|
|
45
|
-
systemPrompts: [systemPrompt],
|
|
46
|
-
maxTokens: options.maxLength,
|
|
47
|
-
temperature: 0.3
|
|
60
|
+
stream: true
|
|
48
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
|
+
}
|
|
49
78
|
}
|
|
50
79
|
buildSummarizationPrompt(options) {
|
|
51
80
|
let prompt = "You are a professional summarizer. ";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport { GrokTextAdapter } from './text'\nimport type { GROK_CHAT_MODELS } from '../model-meta'\nimport type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '@tanstack/ai'\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 BaseSummarizeAdapter<TModel, GrokSummarizeProviderOptions> {\n readonly kind = 'summarize' as const\n readonly name = 'grok' as const\n\n private textAdapter: GrokTextAdapter<TModel>\n\n constructor(config: GrokSummarizeConfig, model: TModel) {\n super({}, model)\n this.textAdapter = new GrokTextAdapter(config, model)\n }\n\n async summarize(options: SummarizationOptions): Promise<SummarizationResult> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n\n // Use the text adapter's streaming and collect the result\n let summary = ''\n const id = ''\n let model = options.model\n let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }\n\n for await (const chunk of this.textAdapter.chatStream({\n
|
|
1
|
+
{"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'\nimport { getGrokApiKeyFromEnv } from '../utils'\nimport { GrokTextAdapter } from './text'\nimport type { GROK_CHAT_MODELS } from '../model-meta'\nimport type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '@tanstack/ai'\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 BaseSummarizeAdapter<TModel, GrokSummarizeProviderOptions> {\n readonly kind = 'summarize' as const\n readonly name = 'grok' as const\n\n private textAdapter: GrokTextAdapter<TModel>\n\n constructor(config: GrokSummarizeConfig, model: TModel) {\n super({}, model)\n this.textAdapter = new GrokTextAdapter(config, model)\n }\n\n async summarize(options: SummarizationOptions): Promise<SummarizationResult> {\n const { logger } = options\n const systemPrompt = this.buildSummarizationPrompt(options)\n\n logger.request(`activity=summarize provider=grok`, {\n provider: 'grok',\n model: options.model,\n })\n\n // Use the text adapter's streaming and collect the result\n let summary = ''\n const id = ''\n let model = options.model\n let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }\n\n try {\n for await (const chunk of this.textAdapter.chatStream({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n systemPrompts: [systemPrompt],\n maxTokens: options.maxLength,\n temperature: 0.3,\n logger,\n })) {\n // AG-UI TEXT_MESSAGE_CONTENT event\n if (chunk.type === 'TEXT_MESSAGE_CONTENT') {\n if (chunk.content) {\n summary = chunk.content\n } else {\n summary += chunk.delta\n }\n model = chunk.model || model\n }\n // AG-UI RUN_FINISHED event\n if (chunk.type === 'RUN_FINISHED') {\n if (chunk.usage) {\n usage = chunk.usage\n }\n }\n }\n } catch (error) {\n logger.errors('grok.summarize fatal', {\n error,\n source: 'grok.summarize',\n })\n throw error\n }\n\n return { id, model, summary, usage }\n }\n\n async *summarizeStream(\n options: SummarizationOptions,\n ): AsyncIterable<StreamChunk> {\n const { logger } = options\n const systemPrompt = this.buildSummarizationPrompt(options)\n\n logger.request(`activity=summarize provider=grok`, {\n provider: 'grok',\n model: options.model,\n stream: true,\n })\n\n try {\n // Delegate directly to the text adapter's streaming\n yield* this.textAdapter.chatStream({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n systemPrompts: [systemPrompt],\n maxTokens: options.maxLength,\n temperature: 0.3,\n logger,\n })\n } catch (error) {\n logger.errors('grok.summarize fatal', {\n error,\n source: 'grok.summarize',\n })\n throw error\n }\n }\n\n private buildSummarizationPrompt(options: SummarizationOptions): string {\n let prompt = 'You are a professional summarizer. '\n\n switch (options.style) {\n case 'bullet-points':\n prompt += 'Provide a summary in bullet point format. '\n break\n case 'paragraph':\n prompt += 'Provide a summary in paragraph format. '\n break\n case 'concise':\n prompt += 'Provide a very concise summary in 1-2 sentences. '\n break\n default:\n prompt += 'Provide a clear and concise summary. '\n }\n\n if (options.focus && options.focus.length > 0) {\n prompt += `Focus on the following aspects: ${options.focus.join(', ')}. `\n }\n\n if (options.maxLength) {\n prompt += `Keep the summary under ${options.maxLength} tokens. `\n }\n\n return prompt\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":";;;AAmCO,MAAM,6BAEH,qBAA2D;AAAA,EAMnE,YAAY,QAA6B,OAAe;AACtD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,cAAc,IAAI,gBAAgB,QAAQ,KAAK;AAAA,EACtD;AAAA,EAEA,MAAM,UAAU,SAA6D;AAC3E,UAAM,EAAE,WAAW;AACnB,UAAM,eAAe,KAAK,yBAAyB,OAAO;AAE1D,WAAO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Ae,CAAC,YAAY;AAAA,QAC5B,WAAW,QAAQ;AAAA,QACnB,aAAa;AAAA,QACb;AAAA,MAAA,CACD;AAAA,IACH,SAAS,OAAO;AACd,aAAO,OAAO,wBAAwB;AAAA,QACpC;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEQ,yBAAyB,SAAuC;AACtE,QAAI,SAAS;AAEb,YAAQ,QAAQ,OAAA;AAAA,MACd,KAAK;AACH,kBAAU;AACV;AAAA,MACF,KAAK;AACH,kBAAU;AACV;AAAA,MACF,KAAK;AACH,kBAAU;AACV;AAAA,MACF;AACE,kBAAU;AAAA,IAAA;AAGd,QAAI,QAAQ,SAAS,QAAQ,MAAM,SAAS,GAAG;AAC7C,gBAAU,mCAAmC,QAAQ,MAAM,KAAK,IAAI,CAAC;AAAA,IACvE;AAEA,QAAI,QAAQ,WAAW;AACrB,gBAAU,0BAA0B,QAAQ,SAAS;AAAA,IACvD;AAEA,WAAO;AAAA,EACT;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,8 +1,10 @@
|
|
|
1
1
|
import { BaseTextAdapter, StructuredOutputOptions, StructuredOutputResult } from '@tanstack/ai/adapters';
|
|
2
|
-
import { GROK_CHAT_MODELS, ResolveInputModalities, ResolveProviderOptions } from '../model-meta.js';
|
|
3
|
-
import { StreamChunk, TextOptions } from '@tanstack/ai';
|
|
2
|
+
import { GROK_CHAT_MODELS, GrokChatModelToolCapabilitiesByName, ResolveInputModalities, ResolveProviderOptions } from '../model-meta.js';
|
|
3
|
+
import { Modality, StreamChunk, TextOptions } from '@tanstack/ai';
|
|
4
|
+
import { ExternalTextProviderOptions as GrokTextProviderOptions } from '../text/text-provider-options.js';
|
|
4
5
|
import { GrokMessageMetadataByModality } from '../message-types.js';
|
|
5
6
|
import { GrokClientConfig } from '../utils.js';
|
|
7
|
+
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof GrokChatModelToolCapabilitiesByName ? NonNullable<GrokChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
6
8
|
/**
|
|
7
9
|
* Configuration for Grok text adapter
|
|
8
10
|
*/
|
|
@@ -18,12 +20,12 @@ export type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../
|
|
|
18
20
|
* Tree-shakeable adapter for Grok chat/text completion functionality.
|
|
19
21
|
* Uses OpenAI-compatible Chat Completions API (not Responses API).
|
|
20
22
|
*/
|
|
21
|
-
export declare class GrokTextAdapter<TModel extends (typeof GROK_CHAT_MODELS)[number]
|
|
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 BaseTextAdapter<TModel, TProviderOptions, TInputModalities, GrokMessageMetadataByModality, TToolCapabilities> {
|
|
22
24
|
readonly kind: "text";
|
|
23
25
|
readonly name: "grok";
|
|
24
26
|
private client;
|
|
25
27
|
constructor(config: GrokTextConfig, model: TModel);
|
|
26
|
-
chatStream(options: TextOptions<
|
|
28
|
+
chatStream(options: TextOptions<GrokTextProviderOptions>): AsyncIterable<StreamChunk>;
|
|
27
29
|
/**
|
|
28
30
|
* Generate structured output using Grok's JSON Schema response format.
|
|
29
31
|
* Uses stream: false to get the complete response in one call.
|
|
@@ -36,7 +38,7 @@ export declare class GrokTextAdapter<TModel extends (typeof GROK_CHAT_MODELS)[nu
|
|
|
36
38
|
* The outputSchema is already JSON Schema (converted in the ai layer).
|
|
37
39
|
* We apply Grok-specific transformations for structured output compatibility.
|
|
38
40
|
*/
|
|
39
|
-
structuredOutput(options: StructuredOutputOptions<
|
|
41
|
+
structuredOutput(options: StructuredOutputOptions<GrokTextProviderOptions>): Promise<StructuredOutputResult<unknown>>;
|
|
40
42
|
private processGrokStreamChunks;
|
|
41
43
|
/**
|
|
42
44
|
* Maps common options to Grok-specific Chat Completions format
|
|
@@ -14,6 +14,7 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
14
14
|
async *chatStream(options) {
|
|
15
15
|
const requestParams = this.mapTextOptionsToGrok(options);
|
|
16
16
|
const timestamp = Date.now();
|
|
17
|
+
const { logger } = options;
|
|
17
18
|
const aguiState = {
|
|
18
19
|
runId: options.runId ?? generateId(this.name),
|
|
19
20
|
threadId: options.threadId ?? generateId(this.name),
|
|
@@ -22,11 +23,15 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
22
23
|
hasEmittedRunStarted: false
|
|
23
24
|
};
|
|
24
25
|
try {
|
|
26
|
+
logger.request(
|
|
27
|
+
`activity=chat provider=grok model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,
|
|
28
|
+
{ provider: "grok", model: this.model }
|
|
29
|
+
);
|
|
25
30
|
const stream = await this.client.chat.completions.create({
|
|
26
31
|
...requestParams,
|
|
27
32
|
stream: true
|
|
28
33
|
});
|
|
29
|
-
yield* this.processGrokStreamChunks(stream, options, aguiState);
|
|
34
|
+
yield* this.processGrokStreamChunks(stream, options, aguiState, logger);
|
|
30
35
|
} catch (error) {
|
|
31
36
|
const err = error;
|
|
32
37
|
if (!aguiState.hasEmittedRunStarted) {
|
|
@@ -51,10 +56,10 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
51
56
|
code: err.code
|
|
52
57
|
}
|
|
53
58
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
logger.errors("grok.chatStream fatal", {
|
|
60
|
+
error,
|
|
61
|
+
source: "grok.chatStream"
|
|
62
|
+
});
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
65
|
/**
|
|
@@ -72,11 +77,16 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
72
77
|
async structuredOutput(options) {
|
|
73
78
|
const { chatOptions, outputSchema } = options;
|
|
74
79
|
const requestParams = this.mapTextOptionsToGrok(chatOptions);
|
|
80
|
+
const { logger } = chatOptions;
|
|
75
81
|
const jsonSchema = makeGrokStructuredOutputCompatible(
|
|
76
82
|
outputSchema,
|
|
77
83
|
outputSchema.required || []
|
|
78
84
|
);
|
|
79
85
|
try {
|
|
86
|
+
logger.request(
|
|
87
|
+
`activity=chat provider=grok model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,
|
|
88
|
+
{ provider: "grok", model: this.model }
|
|
89
|
+
);
|
|
80
90
|
const response = await this.client.chat.completions.create({
|
|
81
91
|
...requestParams,
|
|
82
92
|
stream: false,
|
|
@@ -104,19 +114,21 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
104
114
|
rawText
|
|
105
115
|
};
|
|
106
116
|
} catch (error) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
logger.errors("grok.structuredOutput fatal", {
|
|
118
|
+
error,
|
|
119
|
+
source: "grok.structuredOutput"
|
|
120
|
+
});
|
|
110
121
|
throw error;
|
|
111
122
|
}
|
|
112
123
|
}
|
|
113
|
-
async *processGrokStreamChunks(stream, options, aguiState) {
|
|
124
|
+
async *processGrokStreamChunks(stream, options, aguiState, logger) {
|
|
114
125
|
let accumulatedContent = "";
|
|
115
126
|
const timestamp = aguiState.timestamp;
|
|
116
127
|
let hasEmittedTextMessageStart = false;
|
|
117
128
|
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
118
129
|
try {
|
|
119
130
|
for await (const chunk of stream) {
|
|
131
|
+
logger.provider(`provider=grok`, { chunk });
|
|
120
132
|
const choice = chunk.choices[0];
|
|
121
133
|
if (!choice) continue;
|
|
122
134
|
if (!aguiState.hasEmittedRunStarted) {
|
|
@@ -243,7 +255,10 @@ class GrokTextAdapter extends BaseTextAdapter {
|
|
|
243
255
|
}
|
|
244
256
|
} catch (error) {
|
|
245
257
|
const err = error;
|
|
246
|
-
|
|
258
|
+
logger.errors("grok stream ended with error", {
|
|
259
|
+
error,
|
|
260
|
+
source: "grok.processGrokStreamChunks"
|
|
261
|
+
});
|
|
247
262
|
yield asChunk({
|
|
248
263
|
type: "RUN_ERROR",
|
|
249
264
|
runId: aguiState.runId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport { convertToolsToProviderFormat } from '../tools'\nimport {\n createGrokClient,\n generateId,\n getGrokApiKeyFromEnv,\n makeGrokStructuredOutputCompatible,\n transformNullsToUndefined,\n} from '../utils'\nimport type {\n GROK_CHAT_MODELS,\n ResolveInputModalities,\n ResolveProviderOptions,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type OpenAI_SDK from 'openai'\nimport type {\n ContentPart,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type { InternalTextProviderOptions } from '../text/text-provider-options'\nimport type {\n GrokImageMetadata,\n GrokMessageMetadataByModality,\n} from '../message-types'\nimport type { GrokClientConfig } from '../utils'\n\n/** Cast an event object to StreamChunk. Adapters construct events with string\n * literal types which are structurally compatible with the EventType enum. */\nconst asChunk = (chunk: Record<string, unknown>) =>\n chunk as unknown as StreamChunk\n\n/**\n * Configuration for Grok text adapter\n */\nexport interface GrokTextConfig extends GrokClientConfig {}\n\n/**\n * Alias for TextProviderOptions for external use\n */\nexport type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../text/text-provider-options'\n\n/**\n * Grok Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Grok chat/text completion functionality.\n * Uses OpenAI-compatible Chat Completions API (not Responses API).\n */\nexport class GrokTextAdapter<\n TModel extends (typeof GROK_CHAT_MODELS)[number],\n> extends BaseTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>,\n GrokMessageMetadataByModality\n> {\n readonly kind = 'text' as const\n readonly name = 'grok' as const\n\n private client: OpenAI_SDK\n\n constructor(config: GrokTextConfig, model: TModel) {\n super({}, model)\n this.client = createGrokClient(config)\n }\n\n async *chatStream(\n options: TextOptions<ResolveProviderOptions<TModel>>,\n ): AsyncIterable<StreamChunk> {\n const requestParams = this.mapTextOptionsToGrok(options)\n const timestamp = Date.now()\n\n // AG-UI lifecycle tracking (mutable state object for ESLint compatibility)\n const aguiState = {\n runId: options.runId ?? generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n try {\n const stream = await this.client.chat.completions.create({\n ...requestParams,\n stream: true,\n })\n\n yield* this.processGrokStreamChunks(stream, options, aguiState)\n } catch (error: unknown) {\n const err = error as Error & { code?: string }\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield asChunk({\n type: 'RUN_STARTED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp,\n })\n }\n\n // Emit AG-UI RUN_ERROR\n yield asChunk({\n type: 'RUN_ERROR',\n runId: aguiState.runId,\n model: options.model,\n timestamp,\n message: err.message || 'Unknown error',\n code: err.code,\n error: {\n message: err.message || 'Unknown error',\n code: err.code,\n },\n })\n\n console.error('>>> chatStream: Fatal error during response creation <<<')\n console.error('>>> Error message:', err.message)\n console.error('>>> Error stack:', err.stack)\n console.error('>>> Full error:', err)\n }\n }\n\n /**\n * Generate structured output using Grok's JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * Grok has strict requirements for structured output (via OpenAI-compatible API):\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 all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply Grok-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<ResolveProviderOptions<TModel>>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapTextOptionsToGrok(chatOptions)\n\n // Apply Grok-specific transformations for structured output compatibility\n const jsonSchema = makeGrokStructuredOutputCompatible(\n outputSchema,\n outputSchema.required || [],\n )\n\n try {\n const response = await this.client.chat.completions.create({\n ...requestParams,\n stream: false,\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n })\n\n // Extract text content from the response\n const rawText = response.choices[0]?.message.content || ''\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Transform null values to undefined to match original Zod schema expectations\n // Grok returns null for optional fields we made nullable in the schema\n const transformed = transformNullsToUndefined(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n const err = error as Error\n console.error('>>> structuredOutput: Error during response creation <<<')\n console.error('>>> Error message:', err.message)\n throw error\n }\n }\n\n private async *processGrokStreamChunks(\n stream: AsyncIterable<OpenAI_SDK.Chat.Completions.ChatCompletionChunk>,\n options: TextOptions,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n timestamp: number\n hasEmittedRunStarted: boolean\n },\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n const timestamp = aguiState.timestamp\n let hasEmittedTextMessageStart = false\n\n // Track tool calls being streamed (arguments come in chunks)\n const toolCallsInProgress = new Map<\n number,\n {\n id: string\n name: string\n arguments: string\n started: boolean // Track if TOOL_CALL_START has been emitted\n }\n >()\n\n try {\n for await (const chunk of stream) {\n const choice = chunk.choices[0]\n\n if (!choice) continue\n\n // Emit RUN_STARTED on first chunk\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield asChunk({\n type: 'RUN_STARTED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp,\n })\n }\n\n const delta = choice.delta\n const deltaContent = delta.content\n const deltaToolCalls = delta.tool_calls\n\n // Handle content delta\n if (deltaContent) {\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield asChunk({\n type: 'TEXT_MESSAGE_START',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n role: 'assistant',\n })\n }\n\n accumulatedContent += deltaContent\n\n // Emit AG-UI TEXT_MESSAGE_CONTENT\n yield asChunk({\n type: 'TEXT_MESSAGE_CONTENT',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n delta: deltaContent,\n content: accumulatedContent,\n })\n }\n\n // Handle tool calls - they come in as deltas\n if (deltaToolCalls) {\n for (const toolCallDelta of deltaToolCalls) {\n const index = toolCallDelta.index\n\n // Initialize or update the tool call in progress\n if (!toolCallsInProgress.has(index)) {\n toolCallsInProgress.set(index, {\n id: toolCallDelta.id || '',\n name: toolCallDelta.function?.name || '',\n arguments: '',\n started: false,\n })\n }\n\n const toolCall = toolCallsInProgress.get(index)!\n\n // Update with any new data from the delta\n if (toolCallDelta.id) {\n toolCall.id = toolCallDelta.id\n }\n if (toolCallDelta.function?.name) {\n toolCall.name = toolCallDelta.function.name\n }\n if (toolCallDelta.function?.arguments) {\n toolCall.arguments += toolCallDelta.function.arguments\n }\n\n // Emit TOOL_CALL_START when we have id and name\n if (toolCall.id && toolCall.name && !toolCall.started) {\n toolCall.started = true\n yield asChunk({\n type: 'TOOL_CALL_START',\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp,\n index,\n })\n }\n\n // Emit TOOL_CALL_ARGS for argument deltas\n if (toolCallDelta.function?.arguments && toolCall.started) {\n yield asChunk({\n type: 'TOOL_CALL_ARGS',\n toolCallId: toolCall.id,\n model: chunk.model || options.model,\n timestamp,\n delta: toolCallDelta.function.arguments,\n })\n }\n }\n }\n\n // Handle finish reason\n if (choice.finish_reason) {\n // Emit all completed tool calls\n if (\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ) {\n for (const [, toolCall] of toolCallsInProgress) {\n // Parse arguments for TOOL_CALL_END\n let parsedInput: unknown = {}\n try {\n parsedInput = toolCall.arguments\n ? JSON.parse(toolCall.arguments)\n : {}\n } catch {\n parsedInput = {}\n }\n\n // Emit AG-UI TOOL_CALL_END\n yield asChunk({\n type: 'TOOL_CALL_END',\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp,\n input: parsedInput,\n })\n }\n }\n\n const computedFinishReason =\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ? 'tool_calls'\n : 'stop'\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield asChunk({\n type: 'TEXT_MESSAGE_END',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n })\n }\n\n // Emit AG-UI RUN_FINISHED\n yield asChunk({\n type: 'RUN_FINISHED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp,\n usage: chunk.usage\n ? {\n promptTokens: chunk.usage.prompt_tokens || 0,\n completionTokens: chunk.usage.completion_tokens || 0,\n totalTokens: chunk.usage.total_tokens || 0,\n }\n : undefined,\n finishReason: computedFinishReason,\n })\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { code?: string }\n console.log('[Grok Adapter] Stream ended with error:', err.message)\n\n // Emit AG-UI RUN_ERROR\n yield asChunk({\n type: 'RUN_ERROR',\n runId: aguiState.runId,\n model: options.model,\n timestamp,\n message: err.message || 'Unknown error occurred',\n code: err.code,\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code,\n },\n })\n }\n }\n\n /**\n * Maps common options to Grok-specific Chat Completions format\n */\n private mapTextOptionsToGrok(\n options: TextOptions,\n ): OpenAI_SDK.Chat.Completions.ChatCompletionCreateParamsStreaming {\n const modelOptions = options.modelOptions as\n | Omit<\n InternalTextProviderOptions,\n 'max_tokens' | 'tools' | 'temperature' | 'input' | 'top_p'\n >\n | undefined\n\n if (modelOptions) {\n validateTextProviderOptions({\n ...modelOptions,\n model: options.model,\n })\n }\n\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n // Build messages array with system prompts\n const messages: Array<OpenAI_SDK.Chat.Completions.ChatCompletionMessageParam> =\n []\n\n // Add system prompts first\n if (options.systemPrompts && options.systemPrompts.length > 0) {\n messages.push({\n role: 'system',\n content: options.systemPrompts.join('\\n'),\n })\n }\n\n // Convert messages\n for (const message of options.messages) {\n messages.push(this.convertMessageToGrok(message))\n }\n\n return {\n model: options.model,\n messages,\n temperature: options.temperature,\n max_tokens: options.maxTokens,\n top_p: options.topP,\n tools: tools as Array<OpenAI_SDK.Chat.Completions.ChatCompletionTool>,\n stream: true,\n stream_options: { include_usage: true },\n }\n }\n\n private convertMessageToGrok(\n message: ModelMessage,\n ): OpenAI_SDK.Chat.Completions.ChatCompletionMessageParam {\n // Handle tool messages\n if (message.role === 'tool') {\n return {\n role: 'tool',\n tool_call_id: message.toolCallId || '',\n content:\n typeof message.content === 'string'\n ? message.content\n : JSON.stringify(message.content),\n }\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n const toolCalls = message.toolCalls?.map((tc) => ({\n id: tc.id,\n type: 'function' as const,\n function: {\n name: tc.function.name,\n arguments:\n typeof tc.function.arguments === 'string'\n ? tc.function.arguments\n : JSON.stringify(tc.function.arguments),\n },\n }))\n\n return {\n role: 'assistant',\n content: this.extractTextContent(message.content),\n ...(toolCalls && toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),\n }\n }\n\n // Handle user messages - support multimodal content\n const contentParts = this.normalizeContent(message.content)\n\n // If only text, use simple string format\n if (contentParts.length === 1 && contentParts[0]?.type === 'text') {\n return {\n role: 'user',\n content: contentParts[0].content,\n }\n }\n\n // Otherwise, use array format for multimodal\n const parts: Array<OpenAI_SDK.Chat.Completions.ChatCompletionContentPart> =\n []\n for (const part of contentParts) {\n if (part.type === 'text') {\n parts.push({ type: 'text', text: part.content })\n } else if (part.type === 'image') {\n const imageMetadata = part.metadata as GrokImageMetadata | undefined\n // For base64 data, construct a data URI using the mimeType from source\n const imageValue = part.source.value\n const imageUrl =\n part.source.type === 'data' && !imageValue.startsWith('data:')\n ? `data:${part.source.mimeType};base64,${imageValue}`\n : imageValue\n parts.push({\n type: 'image_url',\n image_url: {\n url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n },\n })\n }\n }\n\n return {\n role: 'user',\n content: parts.length > 0 ? parts : '',\n }\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n private normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n private extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n\n/**\n * Creates a Grok text 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 text adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createGrokText('grok-3', \"xai-...\");\n * // adapter has type-safe providerOptions for grok-3\n * ```\n */\nexport function createGrokText<\n TModel extends (typeof GROK_CHAT_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<GrokTextConfig, 'apiKey'>,\n): GrokTextAdapter<TModel> {\n return new GrokTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Grok text 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 text 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 = grokText('grok-3');\n *\n * const stream = chat({\n * adapter,\n * messages: [{ role: \"user\", content: \"Hello!\" }]\n * });\n * ```\n */\nexport function grokText<TModel extends (typeof GROK_CHAT_MODELS)[number]>(\n model: TModel,\n config?: Omit<GrokTextConfig, 'apiKey'>,\n): GrokTextAdapter<TModel> {\n const apiKey = getGrokApiKeyFromEnv()\n return createGrokText(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;;AAmCA,MAAM,UAAU,CAAC,UACf;AAkBK,MAAM,wBAEH,gBAKR;AAAA,EAMA,YAAY,QAAwB,OAAe;AACjD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,SAAS,iBAAiB,MAAM;AAAA,EACvC;AAAA,EAEA,OAAO,WACL,SAC4B;AAC5B,UAAM,gBAAgB,KAAK,qBAAqB,OAAO;AACvD,UAAM,YAAY,KAAK,IAAA;AAGvB,UAAM,YAAY;AAAA,MAChB,OAAO,QAAQ,SAAS,WAAW,KAAK,IAAI;AAAA,MAC5C,UAAU,QAAQ,YAAY,WAAW,KAAK,IAAI;AAAA,MAClD,WAAW,WAAW,KAAK,IAAI;AAAA,MAC/B;AAAA,MACA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WAAO;AAAA,MACL,MAAM;AAAA,MACN,SAAS,MAAM,SAAS,IAAI,QAAQ;AAAA,IAAA;AAAA,EAExC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,iBACN,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKQ,mBACN,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;AAiBO,SAAS,eAGd,OACA,QACA,QACyB;AACzB,SAAO,IAAI,gBAAgB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AACzD;AA0BO,SAAS,SACd,OACA,QACyB;AACzB,QAAM,SAAS,qBAAA;AACf,SAAO,eAAe,OAAO,QAAQ,MAAM;AAC7C;"}
|
|
1
|
+
{"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport { convertToolsToProviderFormat } from '../tools'\nimport {\n createGrokClient,\n generateId,\n getGrokApiKeyFromEnv,\n makeGrokStructuredOutputCompatible,\n transformNullsToUndefined,\n} from '../utils'\nimport type {\n GROK_CHAT_MODELS,\n GrokChatModelToolCapabilitiesByName,\n ResolveInputModalities,\n ResolveProviderOptions,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\nimport type OpenAI_SDK from 'openai'\nimport type {\n ContentPart,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type {\n ExternalTextProviderOptions as GrokTextProviderOptions,\n InternalTextProviderOptions,\n} from '../text/text-provider-options'\nimport type {\n GrokImageMetadata,\n GrokMessageMetadataByModality,\n} from '../message-types'\nimport type { GrokClientConfig } from '../utils'\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof GrokChatModelToolCapabilitiesByName\n ? NonNullable<GrokChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n/** Cast an event object to StreamChunk. Adapters construct events with string\n * literal types which are structurally compatible with the EventType enum. */\nconst asChunk = (chunk: Record<string, unknown>) =>\n chunk as unknown as StreamChunk\n\n/**\n * Configuration for Grok text adapter\n */\nexport interface GrokTextConfig extends GrokClientConfig {}\n\n/**\n * Alias for TextProviderOptions for external use\n */\nexport type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../text/text-provider-options'\n\n/**\n * Grok Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Grok chat/text completion functionality.\n * Uses OpenAI-compatible Chat Completions API (not Responses API).\n */\nexport class GrokTextAdapter<\n TModel extends (typeof GROK_CHAT_MODELS)[number],\n TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n GrokMessageMetadataByModality,\n TToolCapabilities\n> {\n readonly kind = 'text' as const\n readonly name = 'grok' as const\n\n private client: OpenAI_SDK\n\n constructor(config: GrokTextConfig, model: TModel) {\n super({}, model)\n this.client = createGrokClient(config)\n }\n\n async *chatStream(\n options: TextOptions<GrokTextProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const requestParams = this.mapTextOptionsToGrok(options)\n const timestamp = Date.now()\n const { logger } = options\n\n // AG-UI lifecycle tracking (mutable state object for ESLint compatibility)\n const aguiState = {\n runId: options.runId ?? generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n try {\n logger.request(\n `activity=chat provider=grok model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: 'grok', model: this.model },\n )\n const stream = await this.client.chat.completions.create({\n ...requestParams,\n stream: true,\n })\n\n yield* this.processGrokStreamChunks(stream, options, aguiState, logger)\n } catch (error: unknown) {\n const err = error as Error & { code?: string }\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield asChunk({\n type: 'RUN_STARTED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp,\n })\n }\n\n // Emit AG-UI RUN_ERROR\n yield asChunk({\n type: 'RUN_ERROR',\n runId: aguiState.runId,\n model: options.model,\n timestamp,\n message: err.message || 'Unknown error',\n code: err.code,\n error: {\n message: err.message || 'Unknown error',\n code: err.code,\n },\n })\n\n logger.errors('grok.chatStream fatal', {\n error,\n source: 'grok.chatStream',\n })\n }\n }\n\n /**\n * Generate structured output using Grok's JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * Grok has strict requirements for structured output (via OpenAI-compatible API):\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 all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply Grok-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<GrokTextProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapTextOptionsToGrok(chatOptions)\n const { logger } = chatOptions\n\n // Apply Grok-specific transformations for structured output compatibility\n const jsonSchema = makeGrokStructuredOutputCompatible(\n outputSchema,\n outputSchema.required || [],\n )\n\n try {\n logger.request(\n `activity=chat provider=grok model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,\n { provider: 'grok', model: this.model },\n )\n const response = await this.client.chat.completions.create({\n ...requestParams,\n stream: false,\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n })\n\n // Extract text content from the response\n const rawText = response.choices[0]?.message.content || ''\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Transform null values to undefined to match original Zod schema expectations\n // Grok returns null for optional fields we made nullable in the schema\n const transformed = transformNullsToUndefined(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n logger.errors('grok.structuredOutput fatal', {\n error,\n source: 'grok.structuredOutput',\n })\n throw error\n }\n }\n\n private async *processGrokStreamChunks(\n stream: AsyncIterable<OpenAI_SDK.Chat.Completions.ChatCompletionChunk>,\n options: TextOptions,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n timestamp: number\n hasEmittedRunStarted: boolean\n },\n logger: InternalLogger,\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n const timestamp = aguiState.timestamp\n let hasEmittedTextMessageStart = false\n\n // Track tool calls being streamed (arguments come in chunks)\n const toolCallsInProgress = new Map<\n number,\n {\n id: string\n name: string\n arguments: string\n started: boolean // Track if TOOL_CALL_START has been emitted\n }\n >()\n\n try {\n for await (const chunk of stream) {\n logger.provider(`provider=grok`, { chunk })\n const choice = chunk.choices[0]\n\n if (!choice) continue\n\n // Emit RUN_STARTED on first chunk\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield asChunk({\n type: 'RUN_STARTED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp,\n })\n }\n\n const delta = choice.delta\n const deltaContent = delta.content\n const deltaToolCalls = delta.tool_calls\n\n // Handle content delta\n if (deltaContent) {\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield asChunk({\n type: 'TEXT_MESSAGE_START',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n role: 'assistant',\n })\n }\n\n accumulatedContent += deltaContent\n\n // Emit AG-UI TEXT_MESSAGE_CONTENT\n yield asChunk({\n type: 'TEXT_MESSAGE_CONTENT',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n delta: deltaContent,\n content: accumulatedContent,\n })\n }\n\n // Handle tool calls - they come in as deltas\n if (deltaToolCalls) {\n for (const toolCallDelta of deltaToolCalls) {\n const index = toolCallDelta.index\n\n // Initialize or update the tool call in progress\n if (!toolCallsInProgress.has(index)) {\n toolCallsInProgress.set(index, {\n id: toolCallDelta.id || '',\n name: toolCallDelta.function?.name || '',\n arguments: '',\n started: false,\n })\n }\n\n const toolCall = toolCallsInProgress.get(index)!\n\n // Update with any new data from the delta\n if (toolCallDelta.id) {\n toolCall.id = toolCallDelta.id\n }\n if (toolCallDelta.function?.name) {\n toolCall.name = toolCallDelta.function.name\n }\n if (toolCallDelta.function?.arguments) {\n toolCall.arguments += toolCallDelta.function.arguments\n }\n\n // Emit TOOL_CALL_START when we have id and name\n if (toolCall.id && toolCall.name && !toolCall.started) {\n toolCall.started = true\n yield asChunk({\n type: 'TOOL_CALL_START',\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp,\n index,\n })\n }\n\n // Emit TOOL_CALL_ARGS for argument deltas\n if (toolCallDelta.function?.arguments && toolCall.started) {\n yield asChunk({\n type: 'TOOL_CALL_ARGS',\n toolCallId: toolCall.id,\n model: chunk.model || options.model,\n timestamp,\n delta: toolCallDelta.function.arguments,\n })\n }\n }\n }\n\n // Handle finish reason\n if (choice.finish_reason) {\n // Emit all completed tool calls\n if (\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ) {\n for (const [, toolCall] of toolCallsInProgress) {\n // Parse arguments for TOOL_CALL_END\n let parsedInput: unknown = {}\n try {\n parsedInput = toolCall.arguments\n ? JSON.parse(toolCall.arguments)\n : {}\n } catch {\n parsedInput = {}\n }\n\n // Emit AG-UI TOOL_CALL_END\n yield asChunk({\n type: 'TOOL_CALL_END',\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp,\n input: parsedInput,\n })\n }\n }\n\n const computedFinishReason =\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ? 'tool_calls'\n : 'stop'\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield asChunk({\n type: 'TEXT_MESSAGE_END',\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp,\n })\n }\n\n // Emit AG-UI RUN_FINISHED\n yield asChunk({\n type: 'RUN_FINISHED',\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp,\n usage: chunk.usage\n ? {\n promptTokens: chunk.usage.prompt_tokens || 0,\n completionTokens: chunk.usage.completion_tokens || 0,\n totalTokens: chunk.usage.total_tokens || 0,\n }\n : undefined,\n finishReason: computedFinishReason,\n })\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { code?: string }\n logger.errors('grok stream ended with error', {\n error,\n source: 'grok.processGrokStreamChunks',\n })\n\n // Emit AG-UI RUN_ERROR\n yield asChunk({\n type: 'RUN_ERROR',\n runId: aguiState.runId,\n model: options.model,\n timestamp,\n message: err.message || 'Unknown error occurred',\n code: err.code,\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code,\n },\n })\n }\n }\n\n /**\n * Maps common options to Grok-specific Chat Completions format\n */\n private mapTextOptionsToGrok(\n options: TextOptions,\n ): OpenAI_SDK.Chat.Completions.ChatCompletionCreateParamsStreaming {\n const modelOptions = options.modelOptions as\n | Omit<\n InternalTextProviderOptions,\n 'max_tokens' | 'tools' | 'temperature' | 'input' | 'top_p'\n >\n | undefined\n\n if (modelOptions) {\n validateTextProviderOptions({\n ...modelOptions,\n model: options.model,\n })\n }\n\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n // Build messages array with system prompts\n const messages: Array<OpenAI_SDK.Chat.Completions.ChatCompletionMessageParam> =\n []\n\n // Add system prompts first\n if (options.systemPrompts && options.systemPrompts.length > 0) {\n messages.push({\n role: 'system',\n content: options.systemPrompts.join('\\n'),\n })\n }\n\n // Convert messages\n for (const message of options.messages) {\n messages.push(this.convertMessageToGrok(message))\n }\n\n return {\n model: options.model,\n messages,\n temperature: options.temperature,\n max_tokens: options.maxTokens,\n top_p: options.topP,\n tools: tools as Array<OpenAI_SDK.Chat.Completions.ChatCompletionTool>,\n stream: true,\n stream_options: { include_usage: true },\n }\n }\n\n private convertMessageToGrok(\n message: ModelMessage,\n ): OpenAI_SDK.Chat.Completions.ChatCompletionMessageParam {\n // Handle tool messages\n if (message.role === 'tool') {\n return {\n role: 'tool',\n tool_call_id: message.toolCallId || '',\n content:\n typeof message.content === 'string'\n ? message.content\n : JSON.stringify(message.content),\n }\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n const toolCalls = message.toolCalls?.map((tc) => ({\n id: tc.id,\n type: 'function' as const,\n function: {\n name: tc.function.name,\n arguments:\n typeof tc.function.arguments === 'string'\n ? tc.function.arguments\n : JSON.stringify(tc.function.arguments),\n },\n }))\n\n return {\n role: 'assistant',\n content: this.extractTextContent(message.content),\n ...(toolCalls && toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),\n }\n }\n\n // Handle user messages - support multimodal content\n const contentParts = this.normalizeContent(message.content)\n\n // If only text, use simple string format\n if (contentParts.length === 1 && contentParts[0]?.type === 'text') {\n return {\n role: 'user',\n content: contentParts[0].content,\n }\n }\n\n // Otherwise, use array format for multimodal\n const parts: Array<OpenAI_SDK.Chat.Completions.ChatCompletionContentPart> =\n []\n for (const part of contentParts) {\n if (part.type === 'text') {\n parts.push({ type: 'text', text: part.content })\n } else if (part.type === 'image') {\n const imageMetadata = part.metadata as GrokImageMetadata | undefined\n // For base64 data, construct a data URI using the mimeType from source\n const imageValue = part.source.value\n const imageUrl =\n part.source.type === 'data' && !imageValue.startsWith('data:')\n ? `data:${part.source.mimeType};base64,${imageValue}`\n : imageValue\n parts.push({\n type: 'image_url',\n image_url: {\n url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n },\n })\n }\n }\n\n return {\n role: 'user',\n content: parts.length > 0 ? parts : '',\n }\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n private normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n private extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n\n/**\n * Creates a Grok text 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 text adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createGrokText('grok-3', \"xai-...\");\n * // adapter has type-safe providerOptions for grok-3\n * ```\n */\nexport function createGrokText<\n TModel extends (typeof GROK_CHAT_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<GrokTextConfig, 'apiKey'>,\n): GrokTextAdapter<TModel> {\n return new GrokTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a Grok text 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 text 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 = grokText('grok-3');\n *\n * const stream = chat({\n * adapter,\n * messages: [{ role: \"user\", content: \"Hello!\" }]\n * });\n * ```\n */\nexport function grokText<TModel extends (typeof GROK_CHAT_MODELS)[number]>(\n model: TModel,\n config?: Omit<GrokTextConfig, 'apiKey'>,\n): GrokTextAdapter<TModel> {\n const apiKey = getGrokApiKeyFromEnv()\n return createGrokText(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;;AA8CA,MAAM,UAAU,CAAC,UACf;AAkBK,MAAM,wBAOH,gBAMR;AAAA,EAMA,YAAY,QAAwB,OAAe;AACjD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,SAAS,iBAAiB,MAAM;AAAA,EACvC;AAAA,EAEA,OAAO,WACL,SAC4B;AAC5B,UAAM,gBAAgB,KAAK,qBAAqB,OAAO;AACvD,UAAM,YAAY,KAAK,IAAA;AACvB,UAAM,EAAE,WAAW;AAGnB,UAAM,YAAY;AAAA,MAChB,OAAO,QAAQ,SAAS,WAAW,KAAK,IAAI;AAAA,MAC5C,UAAU,QAAQ,YAAY,WAAW,KAAK,IAAI;AAAA,MAClD,WAAW,WAAW,KAAK,IAAI;AAAA,MAC/B;AAAA,MACA,sBAAsB;AAAA,IAAA;AAGxB,QAAI;AACF,aAAO;AAAA,QACL,qCAAqC,KAAK,KAAK,aAAa,QAAQ,SAAS,MAAM,UAAU,QAAQ,OAAO,UAAU,CAAC;AAAA,QACvH,EAAE,UAAU,QAAQ,OAAO,KAAK,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AQ,WAAW,UAAU,KACjD;AACN,cAAM,KAAK;AAAA,UACT,MAAM;AAAA,UACN,WAAW;AAAA,YACT,KAAK;AAAA,YACL,QAAQ,eAAe,UAAU;AAAA,UAAA;AAAA,QACnC,CACD;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN,SAAS,MAAM,SAAS,IAAI,QAAQ;AAAA,IAAA;AAAA,EAExC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,iBACN,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKQ,mBACN,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;AAiBO,SAAS,eAGd,OACA,QACA,QACyB;AACzB,SAAO,IAAI,gBAAgB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AACzD;AA0BO,SAAS,SACd,OACA,QACyB;AACzB,QAAM,SAAS,qBAAA;AACf,SAAO,eAAe,OAAO,QAAQ,MAAM;AAC7C;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export { GrokTextAdapter, createGrokText, grokText, type GrokTextConfig, type Gr
|
|
|
2
2
|
export { GrokSummarizeAdapter, createGrokSummarize, grokSummarize, type GrokSummarizeConfig, type GrokSummarizeProviderOptions, type GrokSummarizeModel, } from './adapters/summarize.js';
|
|
3
3
|
export { GrokImageAdapter, createGrokImage, grokImage, type GrokImageConfig, } from './adapters/image.js';
|
|
4
4
|
export type { GrokImageProviderOptions, GrokImageModelProviderOptionsByName, } from './image/image-provider-options.js';
|
|
5
|
-
export type { GrokChatModelProviderOptionsByName, GrokModelInputModalitiesByName, ResolveProviderOptions, ResolveInputModalities, GrokChatModel, GrokImageModel, } from './model-meta.js';
|
|
5
|
+
export type { GrokChatModelProviderOptionsByName, GrokChatModelToolCapabilitiesByName, GrokModelInputModalitiesByName, ResolveProviderOptions, ResolveInputModalities, GrokChatModel, GrokImageModel, } from './model-meta.js';
|
|
6
6
|
export { GROK_CHAT_MODELS, GROK_IMAGE_MODELS } from './model-meta.js';
|
|
7
7
|
export type { GrokTextMetadata, GrokImageMetadata, GrokAudioMetadata, GrokVideoMetadata, GrokDocumentMetadata, GrokMessageMetadataByModality, } from './message-types.js';
|
package/dist/esm/model-meta.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ declare const GROK_4_1_FAST_REASONING: {
|
|
|
5
5
|
readonly input: ["text", "image"];
|
|
6
6
|
readonly output: ["text"];
|
|
7
7
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
8
|
+
readonly tools: readonly [];
|
|
8
9
|
};
|
|
9
10
|
readonly pricing: {
|
|
10
11
|
readonly input: {
|
|
@@ -23,6 +24,7 @@ declare const GROK_4_1_FAST_NON_REASONING: {
|
|
|
23
24
|
readonly input: ["text", "image"];
|
|
24
25
|
readonly output: ["text"];
|
|
25
26
|
readonly capabilities: ["structured_outputs", "tool_calling"];
|
|
27
|
+
readonly tools: readonly [];
|
|
26
28
|
};
|
|
27
29
|
readonly pricing: {
|
|
28
30
|
readonly input: {
|
|
@@ -41,6 +43,7 @@ declare const GROK_CODE_FAST_1: {
|
|
|
41
43
|
readonly input: ["text"];
|
|
42
44
|
readonly output: ["text"];
|
|
43
45
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
46
|
+
readonly tools: readonly [];
|
|
44
47
|
};
|
|
45
48
|
readonly pricing: {
|
|
46
49
|
readonly input: {
|
|
@@ -59,6 +62,7 @@ declare const GROK_4_FAST_REASONING: {
|
|
|
59
62
|
readonly input: ["text", "image"];
|
|
60
63
|
readonly output: ["text"];
|
|
61
64
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
65
|
+
readonly tools: readonly [];
|
|
62
66
|
};
|
|
63
67
|
readonly pricing: {
|
|
64
68
|
readonly input: {
|
|
@@ -77,6 +81,7 @@ declare const GROK_4_FAST_NON_REASONING: {
|
|
|
77
81
|
readonly input: ["text", "image"];
|
|
78
82
|
readonly output: ["text"];
|
|
79
83
|
readonly capabilities: ["structured_outputs", "tool_calling"];
|
|
84
|
+
readonly tools: readonly [];
|
|
80
85
|
};
|
|
81
86
|
readonly pricing: {
|
|
82
87
|
readonly input: {
|
|
@@ -95,6 +100,7 @@ declare const GROK_4: {
|
|
|
95
100
|
readonly input: ["text", "image"];
|
|
96
101
|
readonly output: ["text"];
|
|
97
102
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
103
|
+
readonly tools: readonly [];
|
|
98
104
|
};
|
|
99
105
|
readonly pricing: {
|
|
100
106
|
readonly input: {
|
|
@@ -113,6 +119,7 @@ declare const GROK_3_MINI: {
|
|
|
113
119
|
readonly input: ["text"];
|
|
114
120
|
readonly output: ["text"];
|
|
115
121
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
122
|
+
readonly tools: readonly [];
|
|
116
123
|
};
|
|
117
124
|
readonly pricing: {
|
|
118
125
|
readonly input: {
|
|
@@ -131,6 +138,7 @@ declare const GROK_3: {
|
|
|
131
138
|
readonly input: ["text"];
|
|
132
139
|
readonly output: ["text"];
|
|
133
140
|
readonly capabilities: ["structured_outputs", "tool_calling"];
|
|
141
|
+
readonly tools: readonly [];
|
|
134
142
|
};
|
|
135
143
|
readonly pricing: {
|
|
136
144
|
readonly input: {
|
|
@@ -149,6 +157,7 @@ declare const GROK_2_VISION: {
|
|
|
149
157
|
readonly input: ["text", "image"];
|
|
150
158
|
readonly output: ["text"];
|
|
151
159
|
readonly capabilities: ["structured_outputs", "tool_calling"];
|
|
160
|
+
readonly tools: readonly [];
|
|
152
161
|
};
|
|
153
162
|
readonly pricing: {
|
|
154
163
|
readonly input: {
|
|
@@ -170,6 +179,7 @@ declare const GROK_4_20: {
|
|
|
170
179
|
readonly input: ["text", "image", "document"];
|
|
171
180
|
readonly output: ["text"];
|
|
172
181
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
182
|
+
readonly tools: readonly [];
|
|
173
183
|
};
|
|
174
184
|
readonly pricing: {
|
|
175
185
|
readonly input: {
|
|
@@ -188,6 +198,7 @@ declare const GROK_4_20_MULTI_AGENT: {
|
|
|
188
198
|
readonly input: ["text", "image", "document"];
|
|
189
199
|
readonly output: ["text"];
|
|
190
200
|
readonly capabilities: ["reasoning", "structured_outputs", "tool_calling"];
|
|
201
|
+
readonly tools: readonly [];
|
|
191
202
|
};
|
|
192
203
|
readonly pricing: {
|
|
193
204
|
readonly input: {
|
|
@@ -230,6 +241,25 @@ export type GrokModelInputModalitiesByName = {
|
|
|
230
241
|
export type GrokChatModelProviderOptionsByName = {
|
|
231
242
|
[K in (typeof GROK_CHAT_MODELS)[number]]: GrokProviderOptions;
|
|
232
243
|
};
|
|
244
|
+
/**
|
|
245
|
+
* Type-only map from Grok chat model name to its supported provider tools.
|
|
246
|
+
* Grok exposes no provider-specific tool factories, so every model gets an
|
|
247
|
+
* empty tuple. This ensures that passing an Anthropic/OpenAI ProviderTool to
|
|
248
|
+
* a Grok adapter produces a compile-time type error.
|
|
249
|
+
*/
|
|
250
|
+
export type GrokChatModelToolCapabilitiesByName = {
|
|
251
|
+
[GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.tools;
|
|
252
|
+
[GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.tools;
|
|
253
|
+
[GROK_CODE_FAST_1.name]: typeof GROK_CODE_FAST_1.supports.tools;
|
|
254
|
+
[GROK_4_FAST_REASONING.name]: typeof GROK_4_FAST_REASONING.supports.tools;
|
|
255
|
+
[GROK_4_FAST_NON_REASONING.name]: typeof GROK_4_FAST_NON_REASONING.supports.tools;
|
|
256
|
+
[GROK_4.name]: typeof GROK_4.supports.tools;
|
|
257
|
+
[GROK_3.name]: typeof GROK_3.supports.tools;
|
|
258
|
+
[GROK_3_MINI.name]: typeof GROK_3_MINI.supports.tools;
|
|
259
|
+
[GROK_2_VISION.name]: typeof GROK_2_VISION.supports.tools;
|
|
260
|
+
[GROK_4_20.name]: typeof GROK_4_20.supports.tools;
|
|
261
|
+
[GROK_4_20_MULTI_AGENT.name]: typeof GROK_4_20_MULTI_AGENT.supports.tools;
|
|
262
|
+
};
|
|
233
263
|
/**
|
|
234
264
|
* Grok-specific provider options
|
|
235
265
|
* Based on OpenAI-compatible API options
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-meta.js","sources":["../../src/model-meta.ts"],"sourcesContent":["/**\n * Model metadata interface for documentation and type inference\n */\ninterface ModelMeta {\n name: string\n supports: {\n input: Array<'text' | 'image' | 'audio' | 'video' | 'document'>\n output: Array<'text' | 'image' | 'audio' | 'video'>\n capabilities?: Array<'reasoning' | 'tool_calling' | 'structured_outputs'>\n }\n max_input_tokens?: number\n max_output_tokens?: number\n context_window?: number\n knowledge_cutoff?: string\n pricing?: {\n input: {\n normal: number\n cached?: number\n }\n output: {\n normal: number\n }\n }\n}\n\nconst GROK_4_1_FAST_REASONING = {\n name: 'grok-4-1-fast-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_1_FAST_NON_REASONING = {\n name: 'grok-4-1-fast-non-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_CODE_FAST_1 = {\n name: 'grok-code-fast-1',\n context_window: 256_000,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.02,\n },\n output: {\n normal: 1.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_FAST_REASONING = {\n name: 'grok-4-fast-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_FAST_NON_REASONING = {\n name: 'grok-4-fast-non-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4 = {\n name: 'grok-4',\n context_window: 256_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 3,\n cached: 0.75,\n },\n output: {\n normal: 15,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_3_MINI = {\n name: 'grok-3-mini',\n context_window: 131_072,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 0.3,\n cached: 0.075,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_3 = {\n name: 'grok-3',\n context_window: 131_072,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 3,\n cached: 0.75,\n },\n output: {\n normal: 15,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_2_VISION = {\n name: 'grok-2-vision-1212',\n context_window: 32_768,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 2,\n },\n output: {\n normal: 10,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_2_IMAGE = {\n name: 'grok-2-image-1212',\n supports: {\n input: ['text'],\n output: ['image'],\n },\n pricing: {\n input: {\n normal: 0.07,\n },\n output: {\n normal: 0.07,\n },\n },\n} as const satisfies ModelMeta\n\n/**\n * Grok Chat Models\n * Based on xAI's available models as of 2025\n */\nconst GROK_4_20 = {\n name: 'grok-4.20',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image', 'document'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 2,\n cached: 0.2,\n },\n output: {\n normal: 6,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_20_MULTI_AGENT = {\n name: 'grok-4.20-multi-agent',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image', 'document'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n },\n pricing: {\n input: {\n normal: 2,\n cached: 0.2,\n },\n output: {\n normal: 6,\n },\n },\n} as const satisfies ModelMeta\n\nexport const GROK_CHAT_MODELS = [\n GROK_4_1_FAST_REASONING.name,\n GROK_4_1_FAST_NON_REASONING.name,\n GROK_CODE_FAST_1.name,\n GROK_4_FAST_REASONING.name,\n GROK_4_FAST_NON_REASONING.name,\n GROK_4.name,\n GROK_3.name,\n GROK_3_MINI.name,\n GROK_2_VISION.name,\n\n GROK_4_20.name,\n GROK_4_20_MULTI_AGENT.name,\n] as const\n\n/**\n * Grok Image Generation Models\n */\nexport const GROK_IMAGE_MODELS = [GROK_2_IMAGE.name] as const\n\nexport type GrokChatModel = (typeof GROK_CHAT_MODELS)[number]\nexport type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number]\n\n/**\n * Type-only map from Grok chat model name to its supported input modalities.\n * Used for type inference when constructing multimodal messages.\n */\nexport type GrokModelInputModalitiesByName = {\n [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.input\n [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.input\n [GROK_CODE_FAST_1.name]: typeof GROK_CODE_FAST_1.supports.input\n [GROK_4_FAST_REASONING.name]: typeof GROK_4_FAST_REASONING.supports.input\n [GROK_4_FAST_NON_REASONING.name]: typeof GROK_4_FAST_NON_REASONING.supports.input\n [GROK_4.name]: typeof GROK_4.supports.input\n [GROK_3.name]: typeof GROK_3.supports.input\n [GROK_3_MINI.name]: typeof GROK_3_MINI.supports.input\n [GROK_2_VISION.name]: typeof GROK_2_VISION.supports.input\n [GROK_4_20.name]: typeof GROK_4_20.supports.input\n [GROK_4_20_MULTI_AGENT.name]: typeof GROK_4_20_MULTI_AGENT.supports.input\n}\n\n/**\n * Type-only map from Grok chat model name to its provider options type.\n * Since Grok uses OpenAI-compatible API, we reuse OpenAI provider options.\n */\nexport type GrokChatModelProviderOptionsByName = {\n [K in (typeof GROK_CHAT_MODELS)[number]]: GrokProviderOptions\n}\n\n/**\n * Grok-specific provider options\n * Based on OpenAI-compatible API options\n */\nexport interface GrokProviderOptions {\n /** Temperature for response generation (0-2) */\n temperature?: number\n /** Maximum tokens in the response */\n max_tokens?: number\n /** Top-p sampling parameter */\n top_p?: number\n /** Frequency penalty (-2.0 to 2.0) */\n frequency_penalty?: number\n /** Presence penalty (-2.0 to 2.0) */\n presence_penalty?: number\n /** Stop sequences */\n stop?: string | Array<string>\n /** A unique identifier representing your end-user */\n user?: string\n}\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\nexport type ResolveProviderOptions<TModel extends string> =\n TModel extends keyof GrokChatModelProviderOptionsByName\n ? GrokChatModelProviderOptionsByName[TModel]\n : GrokProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use text only.\n */\nexport type ResolveInputModalities<TModel extends string> =\n TModel extends keyof GrokModelInputModalitiesByName\n ? GrokModelInputModalitiesByName[TModel]\n : readonly ['text']\n"],"names":[],"mappings":"AAyBA,MAAM,0BAA0B;AAAA,EAC9B,MAAM;AAgBR;AAEA,MAAM,8BAA8B;AAAA,EAClC,MAAM;AAgBR;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM;AAgBR;AAEA,MAAM,wBAAwB;AAAA,EAC5B,MAAM;AAgBR;AAEA,MAAM,4BAA4B;AAAA,EAChC,MAAM;AAgBR;AAEA,MAAM,SAAS;AAAA,EACb,MAAM;AAgBR;AAEA,MAAM,cAAc;AAAA,EAClB,MAAM;AAgBR;AAEA,MAAM,SAAS;AAAA,EACb,MAAM;AAgBR;AAEA,MAAM,gBAAgB;AAAA,EACpB,MAAM;AAeR;AAEA,MAAM,eAAe;AAAA,EACnB,MAAM;AAaR;AAMA,MAAM,YAAY;AAAA,EAChB,MAAM;AAgBR;AAEA,MAAM,wBAAwB;AAAA,EAC5B,MAAM;AAgBR;AAEO,MAAM,mBAAmB;AAAA,EAC9B,wBAAwB;AAAA,EACxB,4BAA4B;AAAA,EAC5B,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,0BAA0B;AAAA,EAC1B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,cAAc;AAAA,EAEd,UAAU;AAAA,EACV,sBAAsB;AACxB;AAKO,MAAM,oBAAoB,CAAC,aAAa,IAAI;"}
|
|
1
|
+
{"version":3,"file":"model-meta.js","sources":["../../src/model-meta.ts"],"sourcesContent":["/**\n * Model metadata interface for documentation and type inference\n */\ninterface ModelMeta {\n name: string\n supports: {\n input: Array<'text' | 'image' | 'audio' | 'video' | 'document'>\n output: Array<'text' | 'image' | 'audio' | 'video'>\n capabilities?: Array<'reasoning' | 'tool_calling' | 'structured_outputs'>\n tools?: ReadonlyArray<never>\n }\n max_input_tokens?: number\n max_output_tokens?: number\n context_window?: number\n knowledge_cutoff?: string\n pricing?: {\n input: {\n normal: number\n cached?: number\n }\n output: {\n normal: number\n }\n }\n}\n\nconst GROK_4_1_FAST_REASONING = {\n name: 'grok-4-1-fast-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_1_FAST_NON_REASONING = {\n name: 'grok-4-1-fast-non-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_CODE_FAST_1 = {\n name: 'grok-code-fast-1',\n context_window: 256_000,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.02,\n },\n output: {\n normal: 1.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_FAST_REASONING = {\n name: 'grok-4-fast-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_FAST_NON_REASONING = {\n name: 'grok-4-fast-non-reasoning',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.2,\n cached: 0.05,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4 = {\n name: 'grok-4',\n context_window: 256_000,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 3,\n cached: 0.75,\n },\n output: {\n normal: 15,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_3_MINI = {\n name: 'grok-3-mini',\n context_window: 131_072,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 0.3,\n cached: 0.075,\n },\n output: {\n normal: 0.5,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_3 = {\n name: 'grok-3',\n context_window: 131_072,\n supports: {\n input: ['text'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 3,\n cached: 0.75,\n },\n output: {\n normal: 15,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_2_VISION = {\n name: 'grok-2-vision-1212',\n context_window: 32_768,\n supports: {\n input: ['text', 'image'],\n output: ['text'],\n capabilities: ['structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 2,\n },\n output: {\n normal: 10,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_2_IMAGE = {\n name: 'grok-2-image-1212',\n supports: {\n input: ['text'],\n output: ['image'],\n },\n pricing: {\n input: {\n normal: 0.07,\n },\n output: {\n normal: 0.07,\n },\n },\n} as const satisfies ModelMeta\n\n/**\n * Grok Chat Models\n * Based on xAI's available models as of 2025\n */\nconst GROK_4_20 = {\n name: 'grok-4.20',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image', 'document'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 2,\n cached: 0.2,\n },\n output: {\n normal: 6,\n },\n },\n} as const satisfies ModelMeta\n\nconst GROK_4_20_MULTI_AGENT = {\n name: 'grok-4.20-multi-agent',\n context_window: 2_000_000,\n supports: {\n input: ['text', 'image', 'document'],\n output: ['text'],\n capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],\n tools: [] as const,\n },\n pricing: {\n input: {\n normal: 2,\n cached: 0.2,\n },\n output: {\n normal: 6,\n },\n },\n} as const satisfies ModelMeta\n\nexport const GROK_CHAT_MODELS = [\n GROK_4_1_FAST_REASONING.name,\n GROK_4_1_FAST_NON_REASONING.name,\n GROK_CODE_FAST_1.name,\n GROK_4_FAST_REASONING.name,\n GROK_4_FAST_NON_REASONING.name,\n GROK_4.name,\n GROK_3.name,\n GROK_3_MINI.name,\n GROK_2_VISION.name,\n\n GROK_4_20.name,\n GROK_4_20_MULTI_AGENT.name,\n] as const\n\n/**\n * Grok Image Generation Models\n */\nexport const GROK_IMAGE_MODELS = [GROK_2_IMAGE.name] as const\n\nexport type GrokChatModel = (typeof GROK_CHAT_MODELS)[number]\nexport type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number]\n\n/**\n * Type-only map from Grok chat model name to its supported input modalities.\n * Used for type inference when constructing multimodal messages.\n */\nexport type GrokModelInputModalitiesByName = {\n [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.input\n [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.input\n [GROK_CODE_FAST_1.name]: typeof GROK_CODE_FAST_1.supports.input\n [GROK_4_FAST_REASONING.name]: typeof GROK_4_FAST_REASONING.supports.input\n [GROK_4_FAST_NON_REASONING.name]: typeof GROK_4_FAST_NON_REASONING.supports.input\n [GROK_4.name]: typeof GROK_4.supports.input\n [GROK_3.name]: typeof GROK_3.supports.input\n [GROK_3_MINI.name]: typeof GROK_3_MINI.supports.input\n [GROK_2_VISION.name]: typeof GROK_2_VISION.supports.input\n [GROK_4_20.name]: typeof GROK_4_20.supports.input\n [GROK_4_20_MULTI_AGENT.name]: typeof GROK_4_20_MULTI_AGENT.supports.input\n}\n\n/**\n * Type-only map from Grok chat model name to its provider options type.\n * Since Grok uses OpenAI-compatible API, we reuse OpenAI provider options.\n */\nexport type GrokChatModelProviderOptionsByName = {\n [K in (typeof GROK_CHAT_MODELS)[number]]: GrokProviderOptions\n}\n\n/**\n * Type-only map from Grok chat model name to its supported provider tools.\n * Grok exposes no provider-specific tool factories, so every model gets an\n * empty tuple. This ensures that passing an Anthropic/OpenAI ProviderTool to\n * a Grok adapter produces a compile-time type error.\n */\nexport type GrokChatModelToolCapabilitiesByName = {\n [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.tools\n [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.tools\n [GROK_CODE_FAST_1.name]: typeof GROK_CODE_FAST_1.supports.tools\n [GROK_4_FAST_REASONING.name]: typeof GROK_4_FAST_REASONING.supports.tools\n [GROK_4_FAST_NON_REASONING.name]: typeof GROK_4_FAST_NON_REASONING.supports.tools\n [GROK_4.name]: typeof GROK_4.supports.tools\n [GROK_3.name]: typeof GROK_3.supports.tools\n [GROK_3_MINI.name]: typeof GROK_3_MINI.supports.tools\n [GROK_2_VISION.name]: typeof GROK_2_VISION.supports.tools\n [GROK_4_20.name]: typeof GROK_4_20.supports.tools\n [GROK_4_20_MULTI_AGENT.name]: typeof GROK_4_20_MULTI_AGENT.supports.tools\n}\n\n/**\n * Grok-specific provider options\n * Based on OpenAI-compatible API options\n */\nexport interface GrokProviderOptions {\n /** Temperature for response generation (0-2) */\n temperature?: number\n /** Maximum tokens in the response */\n max_tokens?: number\n /** Top-p sampling parameter */\n top_p?: number\n /** Frequency penalty (-2.0 to 2.0) */\n frequency_penalty?: number\n /** Presence penalty (-2.0 to 2.0) */\n presence_penalty?: number\n /** Stop sequences */\n stop?: string | Array<string>\n /** A unique identifier representing your end-user */\n user?: string\n}\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\nexport type ResolveProviderOptions<TModel extends string> =\n TModel extends keyof GrokChatModelProviderOptionsByName\n ? GrokChatModelProviderOptionsByName[TModel]\n : GrokProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use text only.\n */\nexport type ResolveInputModalities<TModel extends string> =\n TModel extends keyof GrokModelInputModalitiesByName\n ? GrokModelInputModalitiesByName[TModel]\n : readonly ['text']\n"],"names":[],"mappings":"AA0BA,MAAM,0BAA0B;AAAA,EAC9B,MAAM;AAiBR;AAEA,MAAM,8BAA8B;AAAA,EAClC,MAAM;AAiBR;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM;AAiBR;AAEA,MAAM,wBAAwB;AAAA,EAC5B,MAAM;AAiBR;AAEA,MAAM,4BAA4B;AAAA,EAChC,MAAM;AAiBR;AAEA,MAAM,SAAS;AAAA,EACb,MAAM;AAiBR;AAEA,MAAM,cAAc;AAAA,EAClB,MAAM;AAiBR;AAEA,MAAM,SAAS;AAAA,EACb,MAAM;AAiBR;AAEA,MAAM,gBAAgB;AAAA,EACpB,MAAM;AAgBR;AAEA,MAAM,eAAe;AAAA,EACnB,MAAM;AAaR;AAMA,MAAM,YAAY;AAAA,EAChB,MAAM;AAiBR;AAEA,MAAM,wBAAwB;AAAA,EAC5B,MAAM;AAiBR;AAEO,MAAM,mBAAmB;AAAA,EAC9B,wBAAwB;AAAA,EACxB,4BAA4B;AAAA,EAC5B,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,0BAA0B;AAAA,EAC1B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,cAAc;AAAA,EAEd,UAAU;AAAA,EACV,sBAAsB;AACxB;AAKO,MAAM,oBAAoB,CAAC,aAAa,IAAI;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-grok",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.8",
|
|
4
4
|
"description": "Grok (xAI) adapter for TanStack AI",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
".": {
|
|
17
17
|
"types": "./dist/esm/index.d.ts",
|
|
18
18
|
"import": "./dist/esm/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./tools": {
|
|
21
|
+
"types": "./dist/esm/tools/index.d.ts",
|
|
22
|
+
"import": "./dist/esm/tools/index.js"
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
25
|
"files": [
|
|
@@ -38,7 +42,7 @@
|
|
|
38
42
|
},
|
|
39
43
|
"peerDependencies": {
|
|
40
44
|
"zod": "^4.0.0",
|
|
41
|
-
"@tanstack/ai": "^0.
|
|
45
|
+
"@tanstack/ai": "^0.13.0"
|
|
42
46
|
},
|
|
43
47
|
"scripts": {
|
|
44
48
|
"build": "vite build",
|
package/src/adapters/image.ts
CHANGED
|
@@ -56,22 +56,35 @@ export class GrokImageAdapter<
|
|
|
56
56
|
async generateImages(
|
|
57
57
|
options: ImageGenerationOptions<GrokImageProviderOptions>,
|
|
58
58
|
): Promise<ImageGenerationResult> {
|
|
59
|
-
const { model, prompt, numberOfImages, size } = options
|
|
59
|
+
const { model, prompt, numberOfImages, size, logger } = options
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
logger.request(`activity=generateImage provider=grok model=${this.model}`, {
|
|
62
|
+
provider: 'grok',
|
|
63
|
+
model: this.model,
|
|
64
|
+
})
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
try {
|
|
67
|
+
// Validate inputs
|
|
68
|
+
validatePrompt({ prompt, model })
|
|
69
|
+
validateImageSize(model, size)
|
|
70
|
+
validateNumberOfImages(model, numberOfImages)
|
|
68
71
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
stream: false,
|
|
72
|
-
})
|
|
72
|
+
// Build request based on model type
|
|
73
|
+
const request = this.buildRequest(options)
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
const response = await this.client.images.generate({
|
|
76
|
+
...request,
|
|
77
|
+
stream: false,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
return this.transformResponse(model, response)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
logger.errors('grok.generateImage fatal', {
|
|
83
|
+
error,
|
|
84
|
+
source: 'grok.generateImage',
|
|
85
|
+
})
|
|
86
|
+
throw error
|
|
87
|
+
}
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
private buildRequest(
|
|
@@ -47,36 +47,51 @@ export class GrokSummarizeAdapter<
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
async summarize(options: SummarizationOptions): Promise<SummarizationResult> {
|
|
50
|
+
const { logger } = options
|
|
50
51
|
const systemPrompt = this.buildSummarizationPrompt(options)
|
|
51
52
|
|
|
53
|
+
logger.request(`activity=summarize provider=grok`, {
|
|
54
|
+
provider: 'grok',
|
|
55
|
+
model: options.model,
|
|
56
|
+
})
|
|
57
|
+
|
|
52
58
|
// Use the text adapter's streaming and collect the result
|
|
53
59
|
let summary = ''
|
|
54
60
|
const id = ''
|
|
55
61
|
let model = options.model
|
|
56
62
|
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
try {
|
|
65
|
+
for await (const chunk of this.textAdapter.chatStream({
|
|
66
|
+
model: options.model,
|
|
67
|
+
messages: [{ role: 'user', content: options.text }],
|
|
68
|
+
systemPrompts: [systemPrompt],
|
|
69
|
+
maxTokens: options.maxLength,
|
|
70
|
+
temperature: 0.3,
|
|
71
|
+
logger,
|
|
72
|
+
})) {
|
|
73
|
+
// AG-UI TEXT_MESSAGE_CONTENT event
|
|
74
|
+
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
|
|
75
|
+
if (chunk.content) {
|
|
76
|
+
summary = chunk.content
|
|
77
|
+
} else {
|
|
78
|
+
summary += chunk.delta
|
|
79
|
+
}
|
|
80
|
+
model = chunk.model || model
|
|
71
81
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
usage = chunk.usage
|
|
82
|
+
// AG-UI RUN_FINISHED event
|
|
83
|
+
if (chunk.type === 'RUN_FINISHED') {
|
|
84
|
+
if (chunk.usage) {
|
|
85
|
+
usage = chunk.usage
|
|
86
|
+
}
|
|
78
87
|
}
|
|
79
88
|
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
logger.errors('grok.summarize fatal', {
|
|
91
|
+
error,
|
|
92
|
+
source: 'grok.summarize',
|
|
93
|
+
})
|
|
94
|
+
throw error
|
|
80
95
|
}
|
|
81
96
|
|
|
82
97
|
return { id, model, summary, usage }
|
|
@@ -85,16 +100,32 @@ export class GrokSummarizeAdapter<
|
|
|
85
100
|
async *summarizeStream(
|
|
86
101
|
options: SummarizationOptions,
|
|
87
102
|
): AsyncIterable<StreamChunk> {
|
|
103
|
+
const { logger } = options
|
|
88
104
|
const systemPrompt = this.buildSummarizationPrompt(options)
|
|
89
105
|
|
|
90
|
-
|
|
91
|
-
|
|
106
|
+
logger.request(`activity=summarize provider=grok`, {
|
|
107
|
+
provider: 'grok',
|
|
92
108
|
model: options.model,
|
|
93
|
-
|
|
94
|
-
systemPrompts: [systemPrompt],
|
|
95
|
-
maxTokens: options.maxLength,
|
|
96
|
-
temperature: 0.3,
|
|
109
|
+
stream: true,
|
|
97
110
|
})
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
// Delegate directly to the text adapter's streaming
|
|
114
|
+
yield* this.textAdapter.chatStream({
|
|
115
|
+
model: options.model,
|
|
116
|
+
messages: [{ role: 'user', content: options.text }],
|
|
117
|
+
systemPrompts: [systemPrompt],
|
|
118
|
+
maxTokens: options.maxLength,
|
|
119
|
+
temperature: 0.3,
|
|
120
|
+
logger,
|
|
121
|
+
})
|
|
122
|
+
} catch (error) {
|
|
123
|
+
logger.errors('grok.summarize fatal', {
|
|
124
|
+
error,
|
|
125
|
+
source: 'grok.summarize',
|
|
126
|
+
})
|
|
127
|
+
throw error
|
|
128
|
+
}
|
|
98
129
|
}
|
|
99
130
|
|
|
100
131
|
private buildSummarizationPrompt(options: SummarizationOptions): string {
|
package/src/adapters/text.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from '../utils'
|
|
11
11
|
import type {
|
|
12
12
|
GROK_CHAT_MODELS,
|
|
13
|
+
GrokChatModelToolCapabilitiesByName,
|
|
13
14
|
ResolveInputModalities,
|
|
14
15
|
ResolveProviderOptions,
|
|
15
16
|
} from '../model-meta'
|
|
@@ -17,20 +18,30 @@ import type {
|
|
|
17
18
|
StructuredOutputOptions,
|
|
18
19
|
StructuredOutputResult,
|
|
19
20
|
} from '@tanstack/ai/adapters'
|
|
21
|
+
import type { InternalLogger } from '@tanstack/ai/adapter-internals'
|
|
20
22
|
import type OpenAI_SDK from 'openai'
|
|
21
23
|
import type {
|
|
22
24
|
ContentPart,
|
|
25
|
+
Modality,
|
|
23
26
|
ModelMessage,
|
|
24
27
|
StreamChunk,
|
|
25
28
|
TextOptions,
|
|
26
29
|
} from '@tanstack/ai'
|
|
27
|
-
import type {
|
|
30
|
+
import type {
|
|
31
|
+
ExternalTextProviderOptions as GrokTextProviderOptions,
|
|
32
|
+
InternalTextProviderOptions,
|
|
33
|
+
} from '../text/text-provider-options'
|
|
28
34
|
import type {
|
|
29
35
|
GrokImageMetadata,
|
|
30
36
|
GrokMessageMetadataByModality,
|
|
31
37
|
} from '../message-types'
|
|
32
38
|
import type { GrokClientConfig } from '../utils'
|
|
33
39
|
|
|
40
|
+
type ResolveToolCapabilities<TModel extends string> =
|
|
41
|
+
TModel extends keyof GrokChatModelToolCapabilitiesByName
|
|
42
|
+
? NonNullable<GrokChatModelToolCapabilitiesByName[TModel]>
|
|
43
|
+
: readonly []
|
|
44
|
+
|
|
34
45
|
/** Cast an event object to StreamChunk. Adapters construct events with string
|
|
35
46
|
* literal types which are structurally compatible with the EventType enum. */
|
|
36
47
|
const asChunk = (chunk: Record<string, unknown>) =>
|
|
@@ -54,11 +65,17 @@ export type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../
|
|
|
54
65
|
*/
|
|
55
66
|
export class GrokTextAdapter<
|
|
56
67
|
TModel extends (typeof GROK_CHAT_MODELS)[number],
|
|
68
|
+
TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,
|
|
69
|
+
TInputModalities extends ReadonlyArray<Modality> =
|
|
70
|
+
ResolveInputModalities<TModel>,
|
|
71
|
+
TToolCapabilities extends ReadonlyArray<string> =
|
|
72
|
+
ResolveToolCapabilities<TModel>,
|
|
57
73
|
> extends BaseTextAdapter<
|
|
58
74
|
TModel,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
GrokMessageMetadataByModality
|
|
75
|
+
TProviderOptions,
|
|
76
|
+
TInputModalities,
|
|
77
|
+
GrokMessageMetadataByModality,
|
|
78
|
+
TToolCapabilities
|
|
62
79
|
> {
|
|
63
80
|
readonly kind = 'text' as const
|
|
64
81
|
readonly name = 'grok' as const
|
|
@@ -71,10 +88,11 @@ export class GrokTextAdapter<
|
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
async *chatStream(
|
|
74
|
-
options: TextOptions<
|
|
91
|
+
options: TextOptions<GrokTextProviderOptions>,
|
|
75
92
|
): AsyncIterable<StreamChunk> {
|
|
76
93
|
const requestParams = this.mapTextOptionsToGrok(options)
|
|
77
94
|
const timestamp = Date.now()
|
|
95
|
+
const { logger } = options
|
|
78
96
|
|
|
79
97
|
// AG-UI lifecycle tracking (mutable state object for ESLint compatibility)
|
|
80
98
|
const aguiState = {
|
|
@@ -86,12 +104,16 @@ export class GrokTextAdapter<
|
|
|
86
104
|
}
|
|
87
105
|
|
|
88
106
|
try {
|
|
107
|
+
logger.request(
|
|
108
|
+
`activity=chat provider=grok model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,
|
|
109
|
+
{ provider: 'grok', model: this.model },
|
|
110
|
+
)
|
|
89
111
|
const stream = await this.client.chat.completions.create({
|
|
90
112
|
...requestParams,
|
|
91
113
|
stream: true,
|
|
92
114
|
})
|
|
93
115
|
|
|
94
|
-
yield* this.processGrokStreamChunks(stream, options, aguiState)
|
|
116
|
+
yield* this.processGrokStreamChunks(stream, options, aguiState, logger)
|
|
95
117
|
} catch (error: unknown) {
|
|
96
118
|
const err = error as Error & { code?: string }
|
|
97
119
|
|
|
@@ -121,10 +143,10 @@ export class GrokTextAdapter<
|
|
|
121
143
|
},
|
|
122
144
|
})
|
|
123
145
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
146
|
+
logger.errors('grok.chatStream fatal', {
|
|
147
|
+
error,
|
|
148
|
+
source: 'grok.chatStream',
|
|
149
|
+
})
|
|
128
150
|
}
|
|
129
151
|
}
|
|
130
152
|
|
|
@@ -141,10 +163,11 @@ export class GrokTextAdapter<
|
|
|
141
163
|
* We apply Grok-specific transformations for structured output compatibility.
|
|
142
164
|
*/
|
|
143
165
|
async structuredOutput(
|
|
144
|
-
options: StructuredOutputOptions<
|
|
166
|
+
options: StructuredOutputOptions<GrokTextProviderOptions>,
|
|
145
167
|
): Promise<StructuredOutputResult<unknown>> {
|
|
146
168
|
const { chatOptions, outputSchema } = options
|
|
147
169
|
const requestParams = this.mapTextOptionsToGrok(chatOptions)
|
|
170
|
+
const { logger } = chatOptions
|
|
148
171
|
|
|
149
172
|
// Apply Grok-specific transformations for structured output compatibility
|
|
150
173
|
const jsonSchema = makeGrokStructuredOutputCompatible(
|
|
@@ -153,6 +176,10 @@ export class GrokTextAdapter<
|
|
|
153
176
|
)
|
|
154
177
|
|
|
155
178
|
try {
|
|
179
|
+
logger.request(
|
|
180
|
+
`activity=chat provider=grok model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,
|
|
181
|
+
{ provider: 'grok', model: this.model },
|
|
182
|
+
)
|
|
156
183
|
const response = await this.client.chat.completions.create({
|
|
157
184
|
...requestParams,
|
|
158
185
|
stream: false,
|
|
@@ -188,9 +215,10 @@ export class GrokTextAdapter<
|
|
|
188
215
|
rawText,
|
|
189
216
|
}
|
|
190
217
|
} catch (error: unknown) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
218
|
+
logger.errors('grok.structuredOutput fatal', {
|
|
219
|
+
error,
|
|
220
|
+
source: 'grok.structuredOutput',
|
|
221
|
+
})
|
|
194
222
|
throw error
|
|
195
223
|
}
|
|
196
224
|
}
|
|
@@ -205,6 +233,7 @@ export class GrokTextAdapter<
|
|
|
205
233
|
timestamp: number
|
|
206
234
|
hasEmittedRunStarted: boolean
|
|
207
235
|
},
|
|
236
|
+
logger: InternalLogger,
|
|
208
237
|
): AsyncIterable<StreamChunk> {
|
|
209
238
|
let accumulatedContent = ''
|
|
210
239
|
const timestamp = aguiState.timestamp
|
|
@@ -223,6 +252,7 @@ export class GrokTextAdapter<
|
|
|
223
252
|
|
|
224
253
|
try {
|
|
225
254
|
for await (const chunk of stream) {
|
|
255
|
+
logger.provider(`provider=grok`, { chunk })
|
|
226
256
|
const choice = chunk.choices[0]
|
|
227
257
|
|
|
228
258
|
if (!choice) continue
|
|
@@ -392,7 +422,10 @@ export class GrokTextAdapter<
|
|
|
392
422
|
}
|
|
393
423
|
} catch (error: unknown) {
|
|
394
424
|
const err = error as Error & { code?: string }
|
|
395
|
-
|
|
425
|
+
logger.errors('grok stream ended with error', {
|
|
426
|
+
error,
|
|
427
|
+
source: 'grok.processGrokStreamChunks',
|
|
428
|
+
})
|
|
396
429
|
|
|
397
430
|
// Emit AG-UI RUN_ERROR
|
|
398
431
|
yield asChunk({
|
package/src/index.ts
CHANGED
package/src/model-meta.ts
CHANGED
|
@@ -7,6 +7,7 @@ interface ModelMeta {
|
|
|
7
7
|
input: Array<'text' | 'image' | 'audio' | 'video' | 'document'>
|
|
8
8
|
output: Array<'text' | 'image' | 'audio' | 'video'>
|
|
9
9
|
capabilities?: Array<'reasoning' | 'tool_calling' | 'structured_outputs'>
|
|
10
|
+
tools?: ReadonlyArray<never>
|
|
10
11
|
}
|
|
11
12
|
max_input_tokens?: number
|
|
12
13
|
max_output_tokens?: number
|
|
@@ -30,6 +31,7 @@ const GROK_4_1_FAST_REASONING = {
|
|
|
30
31
|
input: ['text', 'image'],
|
|
31
32
|
output: ['text'],
|
|
32
33
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
34
|
+
tools: [] as const,
|
|
33
35
|
},
|
|
34
36
|
pricing: {
|
|
35
37
|
input: {
|
|
@@ -49,6 +51,7 @@ const GROK_4_1_FAST_NON_REASONING = {
|
|
|
49
51
|
input: ['text', 'image'],
|
|
50
52
|
output: ['text'],
|
|
51
53
|
capabilities: ['structured_outputs', 'tool_calling'],
|
|
54
|
+
tools: [] as const,
|
|
52
55
|
},
|
|
53
56
|
pricing: {
|
|
54
57
|
input: {
|
|
@@ -68,6 +71,7 @@ const GROK_CODE_FAST_1 = {
|
|
|
68
71
|
input: ['text'],
|
|
69
72
|
output: ['text'],
|
|
70
73
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
74
|
+
tools: [] as const,
|
|
71
75
|
},
|
|
72
76
|
pricing: {
|
|
73
77
|
input: {
|
|
@@ -87,6 +91,7 @@ const GROK_4_FAST_REASONING = {
|
|
|
87
91
|
input: ['text', 'image'],
|
|
88
92
|
output: ['text'],
|
|
89
93
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
94
|
+
tools: [] as const,
|
|
90
95
|
},
|
|
91
96
|
pricing: {
|
|
92
97
|
input: {
|
|
@@ -106,6 +111,7 @@ const GROK_4_FAST_NON_REASONING = {
|
|
|
106
111
|
input: ['text', 'image'],
|
|
107
112
|
output: ['text'],
|
|
108
113
|
capabilities: ['structured_outputs', 'tool_calling'],
|
|
114
|
+
tools: [] as const,
|
|
109
115
|
},
|
|
110
116
|
pricing: {
|
|
111
117
|
input: {
|
|
@@ -125,6 +131,7 @@ const GROK_4 = {
|
|
|
125
131
|
input: ['text', 'image'],
|
|
126
132
|
output: ['text'],
|
|
127
133
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
134
|
+
tools: [] as const,
|
|
128
135
|
},
|
|
129
136
|
pricing: {
|
|
130
137
|
input: {
|
|
@@ -144,6 +151,7 @@ const GROK_3_MINI = {
|
|
|
144
151
|
input: ['text'],
|
|
145
152
|
output: ['text'],
|
|
146
153
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
154
|
+
tools: [] as const,
|
|
147
155
|
},
|
|
148
156
|
pricing: {
|
|
149
157
|
input: {
|
|
@@ -163,6 +171,7 @@ const GROK_3 = {
|
|
|
163
171
|
input: ['text'],
|
|
164
172
|
output: ['text'],
|
|
165
173
|
capabilities: ['structured_outputs', 'tool_calling'],
|
|
174
|
+
tools: [] as const,
|
|
166
175
|
},
|
|
167
176
|
pricing: {
|
|
168
177
|
input: {
|
|
@@ -182,6 +191,7 @@ const GROK_2_VISION = {
|
|
|
182
191
|
input: ['text', 'image'],
|
|
183
192
|
output: ['text'],
|
|
184
193
|
capabilities: ['structured_outputs', 'tool_calling'],
|
|
194
|
+
tools: [] as const,
|
|
185
195
|
},
|
|
186
196
|
pricing: {
|
|
187
197
|
input: {
|
|
@@ -220,6 +230,7 @@ const GROK_4_20 = {
|
|
|
220
230
|
input: ['text', 'image', 'document'],
|
|
221
231
|
output: ['text'],
|
|
222
232
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
233
|
+
tools: [] as const,
|
|
223
234
|
},
|
|
224
235
|
pricing: {
|
|
225
236
|
input: {
|
|
@@ -239,6 +250,7 @@ const GROK_4_20_MULTI_AGENT = {
|
|
|
239
250
|
input: ['text', 'image', 'document'],
|
|
240
251
|
output: ['text'],
|
|
241
252
|
capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
|
|
253
|
+
tools: [] as const,
|
|
242
254
|
},
|
|
243
255
|
pricing: {
|
|
244
256
|
input: {
|
|
@@ -300,6 +312,26 @@ export type GrokChatModelProviderOptionsByName = {
|
|
|
300
312
|
[K in (typeof GROK_CHAT_MODELS)[number]]: GrokProviderOptions
|
|
301
313
|
}
|
|
302
314
|
|
|
315
|
+
/**
|
|
316
|
+
* Type-only map from Grok chat model name to its supported provider tools.
|
|
317
|
+
* Grok exposes no provider-specific tool factories, so every model gets an
|
|
318
|
+
* empty tuple. This ensures that passing an Anthropic/OpenAI ProviderTool to
|
|
319
|
+
* a Grok adapter produces a compile-time type error.
|
|
320
|
+
*/
|
|
321
|
+
export type GrokChatModelToolCapabilitiesByName = {
|
|
322
|
+
[GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.tools
|
|
323
|
+
[GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.tools
|
|
324
|
+
[GROK_CODE_FAST_1.name]: typeof GROK_CODE_FAST_1.supports.tools
|
|
325
|
+
[GROK_4_FAST_REASONING.name]: typeof GROK_4_FAST_REASONING.supports.tools
|
|
326
|
+
[GROK_4_FAST_NON_REASONING.name]: typeof GROK_4_FAST_NON_REASONING.supports.tools
|
|
327
|
+
[GROK_4.name]: typeof GROK_4.supports.tools
|
|
328
|
+
[GROK_3.name]: typeof GROK_3.supports.tools
|
|
329
|
+
[GROK_3_MINI.name]: typeof GROK_3_MINI.supports.tools
|
|
330
|
+
[GROK_2_VISION.name]: typeof GROK_2_VISION.supports.tools
|
|
331
|
+
[GROK_4_20.name]: typeof GROK_4_20.supports.tools
|
|
332
|
+
[GROK_4_20_MULTI_AGENT.name]: typeof GROK_4_20_MULTI_AGENT.supports.tools
|
|
333
|
+
}
|
|
334
|
+
|
|
303
335
|
/**
|
|
304
336
|
* Grok-specific provider options
|
|
305
337
|
* Based on OpenAI-compatible API options
|