@tanstack/ai-grok 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -28
- package/dist/esm/adapters/image.d.ts +77 -0
- package/dist/esm/adapters/image.js +63 -0
- package/dist/esm/adapters/image.js.map +1 -0
- package/dist/esm/adapters/summarize.d.ts +75 -0
- package/dist/esm/adapters/summarize.js +81 -0
- package/dist/esm/adapters/summarize.js.map +1 -0
- package/dist/esm/adapters/text.d.ts +96 -0
- package/dist/esm/adapters/text.js +300 -0
- package/dist/esm/adapters/text.js.map +1 -0
- package/dist/esm/image/image-provider-options.d.ts +66 -0
- package/dist/esm/image/image-provider-options.js +39 -0
- package/dist/esm/image/image-provider-options.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/message-types.d.ts +64 -0
- package/dist/esm/model-meta.d.ts +223 -0
- package/dist/esm/model-meta.js +47 -0
- package/dist/esm/model-meta.js.map +1 -0
- package/dist/esm/text/text-provider-options.d.ts +66 -0
- package/dist/esm/text/text-provider-options.js +6 -0
- package/dist/esm/text/text-provider-options.js.map +1 -0
- package/dist/esm/tools/function-tool.d.ts +15 -0
- package/dist/esm/tools/function-tool.js +27 -0
- package/dist/esm/tools/function-tool.js.map +1 -0
- package/dist/esm/tools/index.d.ts +2 -0
- package/dist/esm/tools/tool-converter.d.ts +7 -0
- package/dist/esm/tools/tool-converter.js +10 -0
- package/dist/esm/tools/tool-converter.js.map +1 -0
- package/dist/esm/utils/client.d.ts +18 -0
- package/dist/esm/utils/client.js +26 -0
- package/dist/esm/utils/client.js.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/schema-converter.d.ts +24 -0
- package/dist/esm/utils/schema-converter.js +71 -0
- package/dist/esm/utils/schema-converter.js.map +1 -0
- package/package.json +50 -7
- package/src/adapters/image.ts +176 -0
- package/src/adapters/summarize.ts +174 -0
- package/src/adapters/text.ts +506 -0
- package/src/image/image-provider-options.ts +118 -0
- package/src/index.ts +55 -0
- package/src/message-types.ts +67 -0
- package/src/model-meta.ts +298 -0
- package/src/text/text-provider-options.ts +77 -0
- package/src/tools/function-tool.ts +45 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/tool-converter.ts +17 -0
- package/src/utils/client.ts +45 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/schema-converter.ts +110 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,130 @@
|
|
|
1
1
|
# @tanstack/ai-grok
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Grok (xAI) adapter for TanStack AI
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tanstack/ai-grok
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @tanstack/ai-grok
|
|
11
|
+
# or
|
|
12
|
+
yarn add @tanstack/ai-grok
|
|
13
|
+
```
|
|
8
14
|
|
|
9
|
-
##
|
|
15
|
+
## Setup
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
1. Configure OIDC trusted publishing for the package name `@tanstack/ai-grok`
|
|
13
|
-
2. Enable secure, token-less publishing from CI/CD workflows
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
17
|
+
Get your API key from [xAI Console](https://console.x.ai) and set it as an environment variable:
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
```bash
|
|
20
|
+
export XAI_API_KEY="xai-..."
|
|
21
|
+
```
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
## Usage
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
### Text/Chat Adapter
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
```typescript
|
|
28
|
+
import { grokText } from '@tanstack/ai-grok'
|
|
29
|
+
import { generate } from '@tanstack/ai'
|
|
23
30
|
|
|
24
|
-
|
|
25
|
-
2. Configure the trusted publisher (e.g., GitHub Actions)
|
|
26
|
-
3. Specify the repository and workflow that should be allowed to publish
|
|
27
|
-
4. Use the configured workflow to publish your actual package
|
|
31
|
+
const adapter = grokText()
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
const result = await generate({
|
|
34
|
+
adapter,
|
|
35
|
+
model: 'grok-3',
|
|
36
|
+
messages: [
|
|
37
|
+
{ role: 'user', content: 'Explain quantum computing in simple terms' },
|
|
38
|
+
],
|
|
39
|
+
})
|
|
30
40
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- Provides no functionality
|
|
34
|
-
- Should not be installed as a dependency
|
|
35
|
-
- Exists only for administrative purposes
|
|
41
|
+
console.log(result.text)
|
|
42
|
+
```
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
### Summarization Adapter
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
46
|
+
```typescript
|
|
47
|
+
import { grokSummarize } from '@tanstack/ai-grok'
|
|
48
|
+
import { summarize } from '@tanstack/ai'
|
|
42
49
|
|
|
43
|
-
|
|
50
|
+
const adapter = grokSummarize()
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
const result = await summarize({
|
|
53
|
+
adapter,
|
|
54
|
+
model: 'grok-3',
|
|
55
|
+
text: 'Long article text...',
|
|
56
|
+
style: 'bullet-points',
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
console.log(result.summary)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Image Generation Adapter
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { grokImage } from '@tanstack/ai-grok'
|
|
66
|
+
import { generateImages } from '@tanstack/ai'
|
|
67
|
+
|
|
68
|
+
const adapter = grokImage()
|
|
69
|
+
|
|
70
|
+
const result = await generateImages({
|
|
71
|
+
adapter,
|
|
72
|
+
model: 'grok-2-image-1212',
|
|
73
|
+
prompt: 'A beautiful sunset over mountains',
|
|
74
|
+
numberOfImages: 1,
|
|
75
|
+
size: '1024x1024',
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
console.log(result.images[0].url)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### With Explicit API Key
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { createGrokText } from '@tanstack/ai-grok'
|
|
85
|
+
|
|
86
|
+
const adapter = createGrokText('xai-your-api-key-here')
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Supported Models
|
|
90
|
+
|
|
91
|
+
### Chat Models
|
|
92
|
+
|
|
93
|
+
- `grok-4` - Latest flagship model
|
|
94
|
+
- `grok-3` - Previous generation model
|
|
95
|
+
- `grok-3-mini` - Smaller, faster model
|
|
96
|
+
- `grok-4-fast` - Fast inference model
|
|
97
|
+
- `grok-4.1-fast` - Production-focused fast model
|
|
98
|
+
- `grok-2-vision-1212` - Vision-capable model (text + image input)
|
|
99
|
+
|
|
100
|
+
### Image Models
|
|
101
|
+
|
|
102
|
+
- `grok-2-image-1212` - Image generation model
|
|
103
|
+
|
|
104
|
+
## Features
|
|
105
|
+
|
|
106
|
+
- ✅ Streaming chat completions
|
|
107
|
+
- ✅ Structured output (JSON Schema)
|
|
108
|
+
- ✅ Function/tool calling
|
|
109
|
+
- ✅ Multimodal input (text + images for vision models)
|
|
110
|
+
- ✅ Image generation
|
|
111
|
+
- ✅ Text summarization
|
|
112
|
+
- ❌ Embeddings (not supported by xAI)
|
|
113
|
+
|
|
114
|
+
## Tree-Shakeable Adapters
|
|
115
|
+
|
|
116
|
+
This package uses tree-shakeable adapters, so you only import what you need:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// Only imports text adapter
|
|
120
|
+
import { grokText } from '@tanstack/ai-grok'
|
|
121
|
+
|
|
122
|
+
// Only imports image adapter
|
|
123
|
+
import { grokImage } from '@tanstack/ai-grok'
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This keeps your bundle size small!
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BaseImageAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { GROK_IMAGE_MODELS } from '../model-meta.js';
|
|
3
|
+
import { GrokImageModelProviderOptionsByName, GrokImageModelSizeByName, GrokImageProviderOptions } from '../image/image-provider-options.js';
|
|
4
|
+
import { ImageGenerationOptions, ImageGenerationResult } from '@tanstack/ai';
|
|
5
|
+
import { GrokClientConfig } from '../utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for Grok image adapter
|
|
8
|
+
*/
|
|
9
|
+
export interface GrokImageConfig extends GrokClientConfig {
|
|
10
|
+
}
|
|
11
|
+
/** Model type for Grok Image */
|
|
12
|
+
export type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number];
|
|
13
|
+
/**
|
|
14
|
+
* Grok Image Generation Adapter
|
|
15
|
+
*
|
|
16
|
+
* Tree-shakeable adapter for Grok image generation functionality.
|
|
17
|
+
* Supports grok-2-image-1212 model.
|
|
18
|
+
*
|
|
19
|
+
* Features:
|
|
20
|
+
* - Model-specific type-safe provider options
|
|
21
|
+
* - Size validation per model
|
|
22
|
+
* - Number of images validation
|
|
23
|
+
*/
|
|
24
|
+
export declare class GrokImageAdapter<TModel extends GrokImageModel> extends BaseImageAdapter<TModel, GrokImageProviderOptions, GrokImageModelProviderOptionsByName, GrokImageModelSizeByName> {
|
|
25
|
+
readonly kind: "image";
|
|
26
|
+
readonly name: "grok";
|
|
27
|
+
private client;
|
|
28
|
+
constructor(config: GrokImageConfig, model: TModel);
|
|
29
|
+
generateImages(options: ImageGenerationOptions<GrokImageProviderOptions>): Promise<ImageGenerationResult>;
|
|
30
|
+
private buildRequest;
|
|
31
|
+
private transformResponse;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a Grok image adapter with explicit API key.
|
|
35
|
+
* Type resolution happens here at the call site.
|
|
36
|
+
*
|
|
37
|
+
* @param model - The model name (e.g., 'grok-2-image-1212')
|
|
38
|
+
* @param apiKey - Your xAI API key
|
|
39
|
+
* @param config - Optional additional configuration
|
|
40
|
+
* @returns Configured Grok image adapter instance with resolved types
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const adapter = createGrokImage('grok-2-image-1212', "xai-...");
|
|
45
|
+
*
|
|
46
|
+
* const result = await generateImage({
|
|
47
|
+
* adapter,
|
|
48
|
+
* prompt: 'A cute baby sea otter'
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function createGrokImage<TModel extends GrokImageModel>(model: TModel, apiKey: string, config?: Omit<GrokImageConfig, 'apiKey'>): GrokImageAdapter<TModel>;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a Grok image adapter with automatic API key detection from environment variables.
|
|
55
|
+
* Type resolution happens here at the call site.
|
|
56
|
+
*
|
|
57
|
+
* Looks for `XAI_API_KEY` in:
|
|
58
|
+
* - `process.env` (Node.js)
|
|
59
|
+
* - `window.env` (Browser with injected env)
|
|
60
|
+
*
|
|
61
|
+
* @param model - The model name (e.g., 'grok-2-image-1212')
|
|
62
|
+
* @param config - Optional configuration (excluding apiKey which is auto-detected)
|
|
63
|
+
* @returns Configured Grok image adapter instance with resolved types
|
|
64
|
+
* @throws Error if XAI_API_KEY is not found in environment
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* // Automatically uses XAI_API_KEY from environment
|
|
69
|
+
* const adapter = grokImage('grok-2-image-1212');
|
|
70
|
+
*
|
|
71
|
+
* const result = await generateImage({
|
|
72
|
+
* adapter,
|
|
73
|
+
* prompt: 'A beautiful sunset over mountains'
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function grokImage<TModel extends GrokImageModel>(model: TModel, config?: Omit<GrokImageConfig, 'apiKey'>): GrokImageAdapter<TModel>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { BaseImageAdapter } from "@tanstack/ai/adapters";
|
|
2
|
+
import { createGrokClient, generateId, getGrokApiKeyFromEnv } from "../utils/client.js";
|
|
3
|
+
import { validatePrompt, validateImageSize, validateNumberOfImages } from "../image/image-provider-options.js";
|
|
4
|
+
class GrokImageAdapter extends BaseImageAdapter {
|
|
5
|
+
constructor(config, model) {
|
|
6
|
+
super({}, model);
|
|
7
|
+
this.kind = "image";
|
|
8
|
+
this.name = "grok";
|
|
9
|
+
this.client = createGrokClient(config);
|
|
10
|
+
}
|
|
11
|
+
async generateImages(options) {
|
|
12
|
+
const { model, prompt, numberOfImages, size } = options;
|
|
13
|
+
validatePrompt({ prompt });
|
|
14
|
+
validateImageSize(model, size);
|
|
15
|
+
validateNumberOfImages(model, numberOfImages);
|
|
16
|
+
const request = this.buildRequest(options);
|
|
17
|
+
const response = await this.client.images.generate({
|
|
18
|
+
...request,
|
|
19
|
+
stream: false
|
|
20
|
+
});
|
|
21
|
+
return this.transformResponse(model, response);
|
|
22
|
+
}
|
|
23
|
+
buildRequest(options) {
|
|
24
|
+
const { model, prompt, numberOfImages, size, modelOptions } = options;
|
|
25
|
+
return {
|
|
26
|
+
model,
|
|
27
|
+
prompt,
|
|
28
|
+
n: numberOfImages ?? 1,
|
|
29
|
+
size,
|
|
30
|
+
...modelOptions
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
transformResponse(model, response) {
|
|
34
|
+
const images = (response.data ?? []).map((item) => ({
|
|
35
|
+
b64Json: item.b64_json,
|
|
36
|
+
url: item.url,
|
|
37
|
+
revisedPrompt: item.revised_prompt
|
|
38
|
+
}));
|
|
39
|
+
return {
|
|
40
|
+
id: generateId(this.name),
|
|
41
|
+
model,
|
|
42
|
+
images,
|
|
43
|
+
usage: response.usage ? {
|
|
44
|
+
inputTokens: response.usage.input_tokens,
|
|
45
|
+
outputTokens: response.usage.output_tokens,
|
|
46
|
+
totalTokens: response.usage.total_tokens
|
|
47
|
+
} : void 0
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function createGrokImage(model, apiKey, config) {
|
|
52
|
+
return new GrokImageAdapter({ apiKey, ...config }, model);
|
|
53
|
+
}
|
|
54
|
+
function grokImage(model, config) {
|
|
55
|
+
const apiKey = getGrokApiKeyFromEnv();
|
|
56
|
+
return createGrokImage(model, apiKey, config);
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
GrokImageAdapter,
|
|
60
|
+
createGrokImage,
|
|
61
|
+
grokImage
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +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 { GROK_IMAGE_MODELS } 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/** Model type for Grok Image */\nexport type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number]\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 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 }\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":";;;AAwCO,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,SAAS;AAGhD,mBAAe,EAAE,OAAc,CAAC;AAChC,sBAAkB,OAAO,IAAI;AAC7B,2BAAuB,OAAO,cAAc;AAG5C,UAAM,UAAU,KAAK,aAAa,OAAO;AAEzC,UAAM,WAAW,MAAM,KAAK,OAAO,OAAO,SAAS;AAAA,MACjD,GAAG;AAAA,MACH,QAAQ;AAAA,IAAA,CACT;AAED,WAAO,KAAK,kBAAkB,OAAO,QAAQ;AAAA,EAC/C;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;"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { BaseSummarizeAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { GROK_CHAT_MODELS } from '../model-meta.js';
|
|
3
|
+
import { StreamChunk, SummarizationOptions, SummarizationResult } from '@tanstack/ai';
|
|
4
|
+
import { GrokClientConfig } from '../utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for Grok summarize adapter
|
|
7
|
+
*/
|
|
8
|
+
export interface GrokSummarizeConfig extends GrokClientConfig {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Grok-specific provider options for summarization
|
|
12
|
+
*/
|
|
13
|
+
export interface GrokSummarizeProviderOptions {
|
|
14
|
+
/** Temperature for response generation (0-2) */
|
|
15
|
+
temperature?: number;
|
|
16
|
+
/** Maximum tokens in the response */
|
|
17
|
+
maxTokens?: number;
|
|
18
|
+
}
|
|
19
|
+
/** Model type for Grok summarization */
|
|
20
|
+
export type GrokSummarizeModel = (typeof GROK_CHAT_MODELS)[number];
|
|
21
|
+
/**
|
|
22
|
+
* Grok Summarize Adapter
|
|
23
|
+
*
|
|
24
|
+
* A thin wrapper around the text adapter that adds summarization-specific prompting.
|
|
25
|
+
* Delegates all API calls to the GrokTextAdapter.
|
|
26
|
+
*/
|
|
27
|
+
export declare class GrokSummarizeAdapter<TModel extends GrokSummarizeModel> extends BaseSummarizeAdapter<TModel, GrokSummarizeProviderOptions> {
|
|
28
|
+
readonly kind: "summarize";
|
|
29
|
+
readonly name: "grok";
|
|
30
|
+
private textAdapter;
|
|
31
|
+
constructor(config: GrokSummarizeConfig, model: TModel);
|
|
32
|
+
summarize(options: SummarizationOptions): Promise<SummarizationResult>;
|
|
33
|
+
summarizeStream(options: SummarizationOptions): AsyncIterable<StreamChunk>;
|
|
34
|
+
private buildSummarizationPrompt;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a Grok summarize adapter with explicit API key.
|
|
38
|
+
* Type resolution happens here at the call site.
|
|
39
|
+
*
|
|
40
|
+
* @param model - The model name (e.g., 'grok-3', 'grok-4')
|
|
41
|
+
* @param apiKey - Your xAI API key
|
|
42
|
+
* @param config - Optional additional configuration
|
|
43
|
+
* @returns Configured Grok summarize adapter instance with resolved types
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const adapter = createGrokSummarize('grok-3', "xai-...");
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function createGrokSummarize<TModel extends GrokSummarizeModel>(model: TModel, apiKey: string, config?: Omit<GrokSummarizeConfig, 'apiKey'>): GrokSummarizeAdapter<TModel>;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a Grok summarize adapter with automatic API key detection from environment variables.
|
|
53
|
+
* Type resolution happens here at the call site.
|
|
54
|
+
*
|
|
55
|
+
* Looks for `XAI_API_KEY` in:
|
|
56
|
+
* - `process.env` (Node.js)
|
|
57
|
+
* - `window.env` (Browser with injected env)
|
|
58
|
+
*
|
|
59
|
+
* @param model - The model name (e.g., 'grok-3', 'grok-4')
|
|
60
|
+
* @param config - Optional configuration (excluding apiKey which is auto-detected)
|
|
61
|
+
* @returns Configured Grok summarize adapter instance with resolved types
|
|
62
|
+
* @throws Error if XAI_API_KEY is not found in environment
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // Automatically uses XAI_API_KEY from environment
|
|
67
|
+
* const adapter = grokSummarize('grok-3');
|
|
68
|
+
*
|
|
69
|
+
* await summarize({
|
|
70
|
+
* adapter,
|
|
71
|
+
* text: "Long article text..."
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function grokSummarize<TModel extends GrokSummarizeModel>(model: TModel, config?: Omit<GrokSummarizeConfig, 'apiKey'>): GrokSummarizeAdapter<TModel>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseSummarizeAdapter } from "@tanstack/ai/adapters";
|
|
2
|
+
import { getGrokApiKeyFromEnv } from "../utils/client.js";
|
|
3
|
+
import { GrokTextAdapter } from "./text.js";
|
|
4
|
+
class GrokSummarizeAdapter extends BaseSummarizeAdapter {
|
|
5
|
+
constructor(config, model) {
|
|
6
|
+
super({}, model);
|
|
7
|
+
this.kind = "summarize";
|
|
8
|
+
this.name = "grok";
|
|
9
|
+
this.textAdapter = new GrokTextAdapter(config, model);
|
|
10
|
+
}
|
|
11
|
+
async summarize(options) {
|
|
12
|
+
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
13
|
+
let summary = "";
|
|
14
|
+
let id = "";
|
|
15
|
+
let model = options.model;
|
|
16
|
+
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
17
|
+
for await (const chunk of this.textAdapter.chatStream({
|
|
18
|
+
model: options.model,
|
|
19
|
+
messages: [{ role: "user", content: options.text }],
|
|
20
|
+
systemPrompts: [systemPrompt],
|
|
21
|
+
maxTokens: options.maxLength,
|
|
22
|
+
temperature: 0.3
|
|
23
|
+
})) {
|
|
24
|
+
if (chunk.type === "content") {
|
|
25
|
+
summary = chunk.content;
|
|
26
|
+
id = chunk.id;
|
|
27
|
+
model = chunk.model;
|
|
28
|
+
}
|
|
29
|
+
if (chunk.type === "done" && chunk.usage) {
|
|
30
|
+
usage = chunk.usage;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { id, model, summary, usage };
|
|
34
|
+
}
|
|
35
|
+
async *summarizeStream(options) {
|
|
36
|
+
const systemPrompt = this.buildSummarizationPrompt(options);
|
|
37
|
+
yield* this.textAdapter.chatStream({
|
|
38
|
+
model: options.model,
|
|
39
|
+
messages: [{ role: "user", content: options.text }],
|
|
40
|
+
systemPrompts: [systemPrompt],
|
|
41
|
+
maxTokens: options.maxLength,
|
|
42
|
+
temperature: 0.3
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
buildSummarizationPrompt(options) {
|
|
46
|
+
let prompt = "You are a professional summarizer. ";
|
|
47
|
+
switch (options.style) {
|
|
48
|
+
case "bullet-points":
|
|
49
|
+
prompt += "Provide a summary in bullet point format. ";
|
|
50
|
+
break;
|
|
51
|
+
case "paragraph":
|
|
52
|
+
prompt += "Provide a summary in paragraph format. ";
|
|
53
|
+
break;
|
|
54
|
+
case "concise":
|
|
55
|
+
prompt += "Provide a very concise summary in 1-2 sentences. ";
|
|
56
|
+
break;
|
|
57
|
+
default:
|
|
58
|
+
prompt += "Provide a clear and concise summary. ";
|
|
59
|
+
}
|
|
60
|
+
if (options.focus && options.focus.length > 0) {
|
|
61
|
+
prompt += `Focus on the following aspects: ${options.focus.join(", ")}. `;
|
|
62
|
+
}
|
|
63
|
+
if (options.maxLength) {
|
|
64
|
+
prompt += `Keep the summary under ${options.maxLength} tokens. `;
|
|
65
|
+
}
|
|
66
|
+
return prompt;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function createGrokSummarize(model, apiKey, config) {
|
|
70
|
+
return new GrokSummarizeAdapter({ apiKey, ...config }, model);
|
|
71
|
+
}
|
|
72
|
+
function grokSummarize(model, config) {
|
|
73
|
+
const apiKey = getGrokApiKeyFromEnv();
|
|
74
|
+
return createGrokSummarize(model, apiKey, config);
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
GrokSummarizeAdapter,
|
|
78
|
+
createGrokSummarize,
|
|
79
|
+
grokSummarize
|
|
80
|
+
};
|
|
81
|
+
//# sourceMappingURL=summarize.js.map
|
|
@@ -0,0 +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 let 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 model: options.model,\n messages: [{ role: 'user', content: options.text }],\n systemPrompts: [systemPrompt],\n maxTokens: options.maxLength,\n temperature: 0.3,\n })) {\n if (chunk.type === 'content') {\n summary = chunk.content\n id = chunk.id\n model = chunk.model\n }\n if (chunk.type === 'done' && chunk.usage) {\n usage = chunk.usage\n }\n }\n\n return { id, model, summary, usage }\n }\n\n async *summarizeStream(\n options: SummarizationOptions,\n ): AsyncIterable<StreamChunk> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n\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 })\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,KAAK,yBAAyB,OAAO;AAG1D,QAAI,UAAU;AACd,QAAI,KAAK;AACT,QAAI,QAAQ,QAAQ;AACpB,QAAI,QAAQ,EAAE,cAAc,GAAG,kBAAkB,GAAG,aAAa,EAAA;AAEjE,qBAAiB,SAAS,KAAK,YAAY,WAAW;AAAA,MACpD,OAAO,QAAQ;AAAA,MACf,UAAU,CAAC,EAAE,MAAM,QAAQ,SAAS,QAAQ,MAAM;AAAA,MAClD,eAAe,CAAC,YAAY;AAAA,MAC5B,WAAW,QAAQ;AAAA,MACnB,aAAa;AAAA,IAAA,CACd,GAAG;AACF,UAAI,MAAM,SAAS,WAAW;AAC5B,kBAAU,MAAM;AAChB,aAAK,MAAM;AACX,gBAAQ,MAAM;AAAA,MAChB;AACA,UAAI,MAAM,SAAS,UAAU,MAAM,OAAO;AACxC,gBAAQ,MAAM;AAAA,MAChB;AAAA,IACF;AAEA,WAAO,EAAE,IAAI,OAAO,SAAS,MAAA;AAAA,EAC/B;AAAA,EAEA,OAAO,gBACL,SAC4B;AAC5B,UAAM,eAAe,KAAK,yBAAyB,OAAO;AAG1D,WAAO,KAAK,YAAY,WAAW;AAAA,MACjC,OAAO,QAAQ;AAAA,MACf,UAAU,CAAC,EAAE,MAAM,QAAQ,SAAS,QAAQ,MAAM;AAAA,MAClD,eAAe,CAAC,YAAY;AAAA,MAC5B,WAAW,QAAQ;AAAA,MACnB,aAAa;AAAA,IAAA,CACd;AAAA,EACH;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;"}
|
|
@@ -0,0 +1,96 @@
|
|
|
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';
|
|
4
|
+
import { GrokMessageMetadataByModality } from '../message-types.js';
|
|
5
|
+
import { GrokClientConfig } from '../utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for Grok text adapter
|
|
8
|
+
*/
|
|
9
|
+
export interface GrokTextConfig extends GrokClientConfig {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Alias for TextProviderOptions for external use
|
|
13
|
+
*/
|
|
14
|
+
export type { ExternalTextProviderOptions as GrokTextProviderOptions } from '../text/text-provider-options.js';
|
|
15
|
+
/**
|
|
16
|
+
* Grok Text (Chat) Adapter
|
|
17
|
+
*
|
|
18
|
+
* Tree-shakeable adapter for Grok chat/text completion functionality.
|
|
19
|
+
* Uses OpenAI-compatible Chat Completions API (not Responses API).
|
|
20
|
+
*/
|
|
21
|
+
export declare class GrokTextAdapter<TModel extends (typeof GROK_CHAT_MODELS)[number]> extends BaseTextAdapter<TModel, ResolveProviderOptions<TModel>, ResolveInputModalities<TModel>, GrokMessageMetadataByModality> {
|
|
22
|
+
readonly kind: "text";
|
|
23
|
+
readonly name: "grok";
|
|
24
|
+
private client;
|
|
25
|
+
constructor(config: GrokTextConfig, model: TModel);
|
|
26
|
+
chatStream(options: TextOptions<ResolveProviderOptions<TModel>>): AsyncIterable<StreamChunk>;
|
|
27
|
+
/**
|
|
28
|
+
* Generate structured output using Grok's JSON Schema response format.
|
|
29
|
+
* Uses stream: false to get the complete response in one call.
|
|
30
|
+
*
|
|
31
|
+
* Grok has strict requirements for structured output (via OpenAI-compatible API):
|
|
32
|
+
* - All properties must be in the `required` array
|
|
33
|
+
* - Optional fields should have null added to their type union
|
|
34
|
+
* - additionalProperties must be false for all objects
|
|
35
|
+
*
|
|
36
|
+
* The outputSchema is already JSON Schema (converted in the ai layer).
|
|
37
|
+
* We apply Grok-specific transformations for structured output compatibility.
|
|
38
|
+
*/
|
|
39
|
+
structuredOutput(options: StructuredOutputOptions<ResolveProviderOptions<TModel>>): Promise<StructuredOutputResult<unknown>>;
|
|
40
|
+
private processGrokStreamChunks;
|
|
41
|
+
/**
|
|
42
|
+
* Maps common options to Grok-specific Chat Completions format
|
|
43
|
+
*/
|
|
44
|
+
private mapTextOptionsToGrok;
|
|
45
|
+
private convertMessageToGrok;
|
|
46
|
+
/**
|
|
47
|
+
* Normalizes message content to an array of ContentPart.
|
|
48
|
+
* Handles backward compatibility with string content.
|
|
49
|
+
*/
|
|
50
|
+
private normalizeContent;
|
|
51
|
+
/**
|
|
52
|
+
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
53
|
+
*/
|
|
54
|
+
private extractTextContent;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a Grok text adapter with explicit API key.
|
|
58
|
+
* Type resolution happens here at the call site.
|
|
59
|
+
*
|
|
60
|
+
* @param model - The model name (e.g., 'grok-3', 'grok-4')
|
|
61
|
+
* @param apiKey - Your xAI API key
|
|
62
|
+
* @param config - Optional additional configuration
|
|
63
|
+
* @returns Configured Grok text adapter instance with resolved types
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const adapter = createGrokText('grok-3', "xai-...");
|
|
68
|
+
* // adapter has type-safe providerOptions for grok-3
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function createGrokText<TModel extends (typeof GROK_CHAT_MODELS)[number]>(model: TModel, apiKey: string, config?: Omit<GrokTextConfig, 'apiKey'>): GrokTextAdapter<TModel>;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a Grok text adapter with automatic API key detection from environment variables.
|
|
74
|
+
* Type resolution happens here at the call site.
|
|
75
|
+
*
|
|
76
|
+
* Looks for `XAI_API_KEY` in:
|
|
77
|
+
* - `process.env` (Node.js)
|
|
78
|
+
* - `window.env` (Browser with injected env)
|
|
79
|
+
*
|
|
80
|
+
* @param model - The model name (e.g., 'grok-3', 'grok-4')
|
|
81
|
+
* @param config - Optional configuration (excluding apiKey which is auto-detected)
|
|
82
|
+
* @returns Configured Grok text adapter instance with resolved types
|
|
83
|
+
* @throws Error if XAI_API_KEY is not found in environment
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Automatically uses XAI_API_KEY from environment
|
|
88
|
+
* const adapter = grokText('grok-3');
|
|
89
|
+
*
|
|
90
|
+
* const stream = chat({
|
|
91
|
+
* adapter,
|
|
92
|
+
* messages: [{ role: "user", content: "Hello!" }]
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function grokText<TModel extends (typeof GROK_CHAT_MODELS)[number]>(model: TModel, config?: Omit<GrokTextConfig, 'apiKey'>): GrokTextAdapter<TModel>;
|