@teamflojo/floimg-xai 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/dist/index.d.ts +112 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +308 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Flojo, Inc.
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xAI Grok text and vision providers for floimg
|
|
3
|
+
*
|
|
4
|
+
* Uses the OpenAI SDK with xAI's OpenAI-compatible API endpoint.
|
|
5
|
+
* Supports structured JSON outputs via response_format.
|
|
6
|
+
*/
|
|
7
|
+
import type { TextProvider, TextProviderSchema, VisionProvider, VisionProviderSchema } from "@teamflojo/floimg";
|
|
8
|
+
export interface GrokTextConfig {
|
|
9
|
+
/** xAI API key (defaults to XAI_API_KEY env var) */
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
/** Model to use (default: grok-2) */
|
|
12
|
+
model?: "grok-2" | "grok-3-beta";
|
|
13
|
+
/** Maximum tokens in response */
|
|
14
|
+
maxTokens?: number;
|
|
15
|
+
/** Temperature for response creativity (0-2) */
|
|
16
|
+
temperature?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface GrokTextParams {
|
|
19
|
+
/** The prompt for text generation */
|
|
20
|
+
prompt: string;
|
|
21
|
+
/** Optional system prompt to guide model behavior */
|
|
22
|
+
systemPrompt?: string;
|
|
23
|
+
/** Optional context from previous step (e.g., vision analysis) */
|
|
24
|
+
context?: string;
|
|
25
|
+
/** Output format: text or structured JSON */
|
|
26
|
+
outputFormat?: "text" | "json";
|
|
27
|
+
/** JSON schema for structured output (when outputFormat is "json") */
|
|
28
|
+
jsonSchema?: Record<string, unknown>;
|
|
29
|
+
/** Maximum tokens in response */
|
|
30
|
+
maxTokens?: number;
|
|
31
|
+
/** Temperature (0-2) */
|
|
32
|
+
temperature?: number;
|
|
33
|
+
/** Per-request API key override */
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Schema for the Grok Text provider
|
|
38
|
+
*/
|
|
39
|
+
export declare const grokTextSchema: TextProviderSchema;
|
|
40
|
+
/**
|
|
41
|
+
* Create a Grok Text provider for text generation
|
|
42
|
+
*
|
|
43
|
+
* Uses xAI's OpenAI-compatible API with structured output support.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import createClient from '@teamflojo/floimg';
|
|
48
|
+
* import { grokText } from '@teamflojo/floimg-xai';
|
|
49
|
+
*
|
|
50
|
+
* const floimg = createClient();
|
|
51
|
+
* floimg.registerTextProvider(grokText({ apiKey: process.env.XAI_API_KEY }));
|
|
52
|
+
*
|
|
53
|
+
* const result = await floimg.text({
|
|
54
|
+
* provider: 'grok-text',
|
|
55
|
+
* params: {
|
|
56
|
+
* prompt: 'Generate 5 creative image prompts for a fantasy landscape',
|
|
57
|
+
* outputFormat: 'json',
|
|
58
|
+
* }
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function grokText(config?: GrokTextConfig): TextProvider;
|
|
63
|
+
export interface GrokVisionConfig {
|
|
64
|
+
/** xAI API key (defaults to XAI_API_KEY env var) */
|
|
65
|
+
apiKey?: string;
|
|
66
|
+
/** Model to use (default: grok-2-vision) */
|
|
67
|
+
model?: "grok-2-vision" | "grok-2-vision-1212";
|
|
68
|
+
/** Maximum tokens in response */
|
|
69
|
+
maxTokens?: number;
|
|
70
|
+
}
|
|
71
|
+
export interface GrokVisionParams {
|
|
72
|
+
/** What to analyze or ask about the image */
|
|
73
|
+
prompt?: string;
|
|
74
|
+
/** Output format: text or structured JSON */
|
|
75
|
+
outputFormat?: "text" | "json";
|
|
76
|
+
/** JSON schema for structured output (when outputFormat is "json") */
|
|
77
|
+
jsonSchema?: Record<string, unknown>;
|
|
78
|
+
/** Maximum tokens in response */
|
|
79
|
+
maxTokens?: number;
|
|
80
|
+
/** Per-request API key override */
|
|
81
|
+
apiKey?: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Schema for the Grok Vision provider
|
|
85
|
+
*/
|
|
86
|
+
export declare const grokVisionSchema: VisionProviderSchema;
|
|
87
|
+
/**
|
|
88
|
+
* Create a Grok Vision provider for image analysis
|
|
89
|
+
*
|
|
90
|
+
* Uses xAI's OpenAI-compatible API with structured output support.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* import createClient from '@teamflojo/floimg';
|
|
95
|
+
* import { grokVision } from '@teamflojo/floimg-xai';
|
|
96
|
+
*
|
|
97
|
+
* const floimg = createClient();
|
|
98
|
+
* floimg.registerVisionProvider(grokVision({ apiKey: process.env.XAI_API_KEY }));
|
|
99
|
+
*
|
|
100
|
+
* const result = await floimg.vision({
|
|
101
|
+
* provider: 'grok-vision',
|
|
102
|
+
* blob: imageBlob,
|
|
103
|
+
* params: {
|
|
104
|
+
* prompt: 'What objects are in this image?',
|
|
105
|
+
* outputFormat: 'json',
|
|
106
|
+
* }
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function grokVision(config?: GrokVisionConfig): VisionProvider;
|
|
111
|
+
export { grokText as default };
|
|
112
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAGV,YAAY,EACZ,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAM3B,MAAM,WAAW,cAAc;IAC7B,oDAAoD;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qCAAqC;IACrC,KAAK,CAAC,EAAE,QAAQ,GAAG,aAAa,CAAC;IACjC,iCAAiC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gDAAgD;IAChD,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,qCAAqC;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kEAAkE;IAClE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC/B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g
|
|
3
|
+
*
|
|
4
|
+
* Uses the OpenAI SDK with xAI's OpenAI-compatible API endpoint.
|
|
5
|
+
* Supports structured JSON outputs via response_format.
|
|
6
|
+
*/
|
|
7
|
+
import OpenAI from "openai";
|
|
8
|
+
/**
|
|
9
|
+
* Schema for the Grok Text provider
|
|
10
|
+
*/
|
|
11
|
+
export const grokTextSchema = {
|
|
12
|
+
name: "grok-text",
|
|
13
|
+
description: "Generate text using xAI's Grok models with structured output support",
|
|
14
|
+
category: "Cloud",
|
|
15
|
+
parameters: {
|
|
16
|
+
prompt: {
|
|
17
|
+
type: "string",
|
|
18
|
+
title: "Prompt",
|
|
19
|
+
description: "The prompt for text generation",
|
|
20
|
+
},
|
|
21
|
+
systemPrompt: {
|
|
22
|
+
type: "string",
|
|
23
|
+
title: "System Prompt",
|
|
24
|
+
description: "Optional system prompt to guide the model's behavior",
|
|
25
|
+
},
|
|
26
|
+
context: {
|
|
27
|
+
type: "string",
|
|
28
|
+
title: "Context",
|
|
29
|
+
description: "Optional context from a previous step (e.g., vision analysis)",
|
|
30
|
+
},
|
|
31
|
+
outputFormat: {
|
|
32
|
+
type: "string",
|
|
33
|
+
title: "Output Format",
|
|
34
|
+
description: "Response format: plain text or structured JSON",
|
|
35
|
+
enum: ["text", "json"],
|
|
36
|
+
default: "text",
|
|
37
|
+
},
|
|
38
|
+
maxTokens: {
|
|
39
|
+
type: "number",
|
|
40
|
+
title: "Max Tokens",
|
|
41
|
+
description: "Maximum tokens in the response",
|
|
42
|
+
default: 1000,
|
|
43
|
+
},
|
|
44
|
+
temperature: {
|
|
45
|
+
type: "number",
|
|
46
|
+
title: "Temperature",
|
|
47
|
+
description: "Creativity level (0-2)",
|
|
48
|
+
default: 0.7,
|
|
49
|
+
minimum: 0,
|
|
50
|
+
maximum: 2,
|
|
51
|
+
},
|
|
52
|
+
apiKey: {
|
|
53
|
+
type: "string",
|
|
54
|
+
title: "API Key",
|
|
55
|
+
description: "Your xAI API key (optional - uses server key if not provided)",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
requiredParameters: ["prompt"],
|
|
59
|
+
requiresApiKey: true,
|
|
60
|
+
outputType: "data",
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Create a Grok Text provider for text generation
|
|
64
|
+
*
|
|
65
|
+
* Uses xAI's OpenAI-compatible API with structured output support.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import createClient from '@teamflojo/floimg';
|
|
70
|
+
* import { grokText } from '@teamflojo/floimg-xai';
|
|
71
|
+
*
|
|
72
|
+
* const floimg = createClient();
|
|
73
|
+
* floimg.registerTextProvider(grokText({ apiKey: process.env.XAI_API_KEY }));
|
|
74
|
+
*
|
|
75
|
+
* const result = await floimg.text({
|
|
76
|
+
* provider: 'grok-text',
|
|
77
|
+
* params: {
|
|
78
|
+
* prompt: 'Generate 5 creative image prompts for a fantasy landscape',
|
|
79
|
+
* outputFormat: 'json',
|
|
80
|
+
* }
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function grokText(config = {}) {
|
|
85
|
+
// Config API key is optional - user can provide per-request
|
|
86
|
+
const configApiKey = config.apiKey || process.env.XAI_API_KEY;
|
|
87
|
+
const createClient = (apiKey) => new OpenAI({
|
|
88
|
+
apiKey,
|
|
89
|
+
baseURL: "https://api.x.ai/v1",
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
name: "grok-text",
|
|
93
|
+
schema: grokTextSchema,
|
|
94
|
+
async generate(params) {
|
|
95
|
+
const { prompt, systemPrompt, context, outputFormat = "text", jsonSchema, maxTokens = config.maxTokens || 1000, temperature = config.temperature || 0.7, apiKey: paramApiKey, } = params;
|
|
96
|
+
if (!prompt) {
|
|
97
|
+
throw new Error("prompt is required for Grok text generation");
|
|
98
|
+
}
|
|
99
|
+
// Use per-request API key if provided, otherwise use config key
|
|
100
|
+
const apiKey = paramApiKey || configApiKey;
|
|
101
|
+
if (!apiKey) {
|
|
102
|
+
throw new Error("xAI API key is required. Set XAI_API_KEY environment variable, pass apiKey in config, or provide it in params.");
|
|
103
|
+
}
|
|
104
|
+
const client = createClient(apiKey);
|
|
105
|
+
const model = config.model || "grok-2";
|
|
106
|
+
// Build system message
|
|
107
|
+
let system = systemPrompt || "You are a helpful assistant.";
|
|
108
|
+
if (outputFormat === "json" && !jsonSchema) {
|
|
109
|
+
system += " Always respond with valid JSON.";
|
|
110
|
+
}
|
|
111
|
+
// Build user message with optional context
|
|
112
|
+
let userMessage = prompt;
|
|
113
|
+
if (context) {
|
|
114
|
+
userMessage = `Context from previous analysis:\n${context}\n\n${prompt}`;
|
|
115
|
+
}
|
|
116
|
+
// Build request options
|
|
117
|
+
const requestOptions = {
|
|
118
|
+
model,
|
|
119
|
+
max_tokens: maxTokens,
|
|
120
|
+
temperature,
|
|
121
|
+
messages: [
|
|
122
|
+
{ role: "system", content: system },
|
|
123
|
+
{ role: "user", content: userMessage },
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
// Add structured output if JSON schema is provided
|
|
127
|
+
if (outputFormat === "json" && jsonSchema) {
|
|
128
|
+
// Use type assertion for xAI's structured output format
|
|
129
|
+
requestOptions.response_format = {
|
|
130
|
+
type: "json_schema",
|
|
131
|
+
json_schema: jsonSchema,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
else if (outputFormat === "json") {
|
|
135
|
+
requestOptions.response_format = { type: "json_object" };
|
|
136
|
+
}
|
|
137
|
+
const response = await client.chat.completions.create(requestOptions);
|
|
138
|
+
const content = response.choices[0]?.message?.content || "";
|
|
139
|
+
// Try to parse JSON if requested
|
|
140
|
+
let parsed;
|
|
141
|
+
if (outputFormat === "json") {
|
|
142
|
+
try {
|
|
143
|
+
parsed = JSON.parse(content);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// If JSON parsing fails, treat as text
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
type: parsed ? "json" : "text",
|
|
151
|
+
content,
|
|
152
|
+
parsed,
|
|
153
|
+
source: `ai:grok-text:${model}`,
|
|
154
|
+
metadata: {
|
|
155
|
+
model,
|
|
156
|
+
prompt,
|
|
157
|
+
temperature,
|
|
158
|
+
usage: response.usage,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Schema for the Grok Vision provider
|
|
166
|
+
*/
|
|
167
|
+
export const grokVisionSchema = {
|
|
168
|
+
name: "grok-vision",
|
|
169
|
+
description: "Analyze images using xAI's Grok Vision models with structured output support",
|
|
170
|
+
category: "Cloud",
|
|
171
|
+
parameters: {
|
|
172
|
+
prompt: {
|
|
173
|
+
type: "string",
|
|
174
|
+
title: "Prompt",
|
|
175
|
+
description: "What to analyze or ask about the image",
|
|
176
|
+
default: "Describe this image in detail.",
|
|
177
|
+
},
|
|
178
|
+
outputFormat: {
|
|
179
|
+
type: "string",
|
|
180
|
+
title: "Output Format",
|
|
181
|
+
description: "Response format: plain text or structured JSON",
|
|
182
|
+
enum: ["text", "json"],
|
|
183
|
+
default: "text",
|
|
184
|
+
},
|
|
185
|
+
maxTokens: {
|
|
186
|
+
type: "number",
|
|
187
|
+
title: "Max Tokens",
|
|
188
|
+
description: "Maximum tokens in the response",
|
|
189
|
+
default: 1000,
|
|
190
|
+
},
|
|
191
|
+
apiKey: {
|
|
192
|
+
type: "string",
|
|
193
|
+
title: "API Key",
|
|
194
|
+
description: "Your xAI API key (optional - uses server key if not provided)",
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
outputFormats: ["text", "json"],
|
|
198
|
+
requiresApiKey: true,
|
|
199
|
+
inputType: "image",
|
|
200
|
+
outputType: "data",
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Create a Grok Vision provider for image analysis
|
|
204
|
+
*
|
|
205
|
+
* Uses xAI's OpenAI-compatible API with structured output support.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* import createClient from '@teamflojo/floimg';
|
|
210
|
+
* import { grokVision } from '@teamflojo/floimg-xai';
|
|
211
|
+
*
|
|
212
|
+
* const floimg = createClient();
|
|
213
|
+
* floimg.registerVisionProvider(grokVision({ apiKey: process.env.XAI_API_KEY }));
|
|
214
|
+
*
|
|
215
|
+
* const result = await floimg.vision({
|
|
216
|
+
* provider: 'grok-vision',
|
|
217
|
+
* blob: imageBlob,
|
|
218
|
+
* params: {
|
|
219
|
+
* prompt: 'What objects are in this image?',
|
|
220
|
+
* outputFormat: 'json',
|
|
221
|
+
* }
|
|
222
|
+
* });
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export function grokVision(config = {}) {
|
|
226
|
+
// Config API key is optional - user can provide per-request
|
|
227
|
+
const configApiKey = config.apiKey || process.env.XAI_API_KEY;
|
|
228
|
+
const createClient = (apiKey) => new OpenAI({
|
|
229
|
+
apiKey,
|
|
230
|
+
baseURL: "https://api.x.ai/v1",
|
|
231
|
+
});
|
|
232
|
+
return {
|
|
233
|
+
name: "grok-vision",
|
|
234
|
+
schema: grokVisionSchema,
|
|
235
|
+
async analyze(input, params) {
|
|
236
|
+
const { prompt = "Describe this image in detail.", outputFormat = "text", jsonSchema, maxTokens = config.maxTokens || 1000, apiKey: paramApiKey, } = params;
|
|
237
|
+
// Use per-request API key if provided, otherwise use config key
|
|
238
|
+
const apiKey = paramApiKey || configApiKey;
|
|
239
|
+
if (!apiKey) {
|
|
240
|
+
throw new Error("xAI API key is required. Set XAI_API_KEY environment variable, pass apiKey in config, or provide it in params.");
|
|
241
|
+
}
|
|
242
|
+
const client = createClient(apiKey);
|
|
243
|
+
const model = config.model || "grok-2-vision";
|
|
244
|
+
// Convert image to base64 data URL
|
|
245
|
+
const base64 = input.bytes.toString("base64");
|
|
246
|
+
const dataUrl = `data:${input.mime};base64,${base64}`;
|
|
247
|
+
// Build system prompt
|
|
248
|
+
const systemPrompt = outputFormat === "json" && !jsonSchema
|
|
249
|
+
? "You are a helpful assistant that analyzes images. Always respond with valid JSON."
|
|
250
|
+
: "You are a helpful assistant that analyzes images.";
|
|
251
|
+
// Build request options
|
|
252
|
+
const requestOptions = {
|
|
253
|
+
model,
|
|
254
|
+
max_tokens: maxTokens,
|
|
255
|
+
messages: [
|
|
256
|
+
{ role: "system", content: systemPrompt },
|
|
257
|
+
{
|
|
258
|
+
role: "user",
|
|
259
|
+
content: [
|
|
260
|
+
{ type: "text", text: prompt },
|
|
261
|
+
{
|
|
262
|
+
type: "image_url",
|
|
263
|
+
image_url: { url: dataUrl },
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
};
|
|
269
|
+
// Add structured output if JSON schema is provided
|
|
270
|
+
if (outputFormat === "json" && jsonSchema) {
|
|
271
|
+
// Use type assertion for xAI's structured output format
|
|
272
|
+
requestOptions.response_format = {
|
|
273
|
+
type: "json_schema",
|
|
274
|
+
json_schema: jsonSchema,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
else if (outputFormat === "json") {
|
|
278
|
+
requestOptions.response_format = { type: "json_object" };
|
|
279
|
+
}
|
|
280
|
+
const response = await client.chat.completions.create(requestOptions);
|
|
281
|
+
const content = response.choices[0]?.message?.content || "";
|
|
282
|
+
// Try to parse JSON if requested
|
|
283
|
+
let parsed;
|
|
284
|
+
if (outputFormat === "json") {
|
|
285
|
+
try {
|
|
286
|
+
parsed = JSON.parse(content);
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
// If JSON parsing fails, treat as text
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
type: parsed ? "json" : "text",
|
|
294
|
+
content,
|
|
295
|
+
parsed,
|
|
296
|
+
source: `ai:grok-vision:${model}`,
|
|
297
|
+
metadata: {
|
|
298
|
+
model,
|
|
299
|
+
prompt,
|
|
300
|
+
usage: response.usage,
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
// Named exports
|
|
307
|
+
export { grokText as default };
|
|
308
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AA4C5B;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAuB;IAChD,IAAI,EAAE,WAAW;IACjB,WAAW,EAAE,sEAAsE;IACnF,QAAQ,EAAE,OAAO;IACjB,UAAU,EAAE;QACV,MAAM,EAAE;YACN,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,QAAQ;YACf,WAAW,EAAE,gCAAgC;SAC9C;QACD,YAAY,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,eAAe;YACtB,WAAW,EAAE,sDAAsD;SACpE;QACD,OAAO,EAAE;YACP,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,SAAS;YAChB,WAAW,EAAE,+DAA+D;SAC7E;QACD,YAAY,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,eAAe;YACtB,WAAW,EAAE,gDAAgD;YAC7D,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;YACtB,OAAO,EAAE,MAAM;SAChB;QACD,SAAS,EAAE;YACT,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,YAAY;YACnB,WAAW,EAAE,gCAAgC;YAC7C,OAAO,EAAE,IAAI;SACd;QACD,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,aAAa;YACpB,WAAW,EAAE,wBAAwB;YACrC,OAAO,EAAE,GAAG;YACZ,OAAO,EAAE,CAAC;YACV,OAAO,EAAE,CAAC;SACX;QACD,MAAM,EAAE;YACN,IAAI,EA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BACtB;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,gBAAgB;AAChB,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teamflojo/floimg-xai",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "xAI Grok text and vision providers for floimg",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"floimg",
|
|
21
|
+
"xai",
|
|
22
|
+
"grok",
|
|
23
|
+
"text-generation",
|
|
24
|
+
"vision",
|
|
25
|
+
"ai"
|
|
26
|
+
],
|
|
27
|
+
"author": "Brett Cooke",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/TeamFlojo/floimg.git",
|
|
32
|
+
"directory": "packages/floimg-xai"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@teamflojo/floimg": "^0.2.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"openai": "^4.76.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.10.2",
|
|
42
|
+
"typescript": "^5.7.2",
|
|
43
|
+
"vitest": "^2.1.8",
|
|
44
|
+
"@teamflojo/floimg": "0.7.1"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.0.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc",
|
|
51
|
+
"dev": "tsc --watch",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"clean": "rm -rf dist",
|
|
54
|
+
"test": "vitest --run"
|
|
55
|
+
}
|
|
56
|
+
}
|