@fre4x/grok 1.0.47 → 1.0.50
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/README.md +6 -0
- package/dist/index.js +148 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ xAI's Grok is more than an LLM. It's a real-time sensor for the global conscious
|
|
|
18
18
|
|
|
19
19
|
Search tools support `limit` and `offset` pagination and return normalized results with citations.
|
|
20
20
|
|
|
21
|
+
Image generation responses include direct MCP `image` content blocks when media can be embedded. Video responses include direct MCP embedded `resource` blobs because MCP does not define a dedicated `video` content block.
|
|
22
|
+
|
|
21
23
|
## Configuration
|
|
22
24
|
|
|
23
25
|
Required environment variable:
|
|
@@ -29,6 +31,10 @@ Mock mode is supported for local development and offline validation:
|
|
|
29
31
|
MOCK=true npx @fre4x/grok
|
|
30
32
|
```
|
|
31
33
|
|
|
34
|
+
Optional media embedding controls:
|
|
35
|
+
- `GROK_MAX_EMBEDDED_MEDIA_BYTES`: Maximum bytes to inline into MCP `content` blocks. Defaults to `5242880`.
|
|
36
|
+
- `GROK_MEDIA_FETCH_TIMEOUT_MS`: Timeout for downloading generated media before embedding. Defaults to `15000`.
|
|
37
|
+
|
|
32
38
|
## Deploy
|
|
33
39
|
|
|
34
40
|
```json
|
package/dist/index.js
CHANGED
|
@@ -43635,6 +43635,16 @@ var z3 = external_exports || zod_default || zod_exports;
|
|
|
43635
43635
|
var XAI_API_KEY = process.env.XAI_API_KEY;
|
|
43636
43636
|
var aspectRatioValues = ["1:1", "16:9", "9:16"];
|
|
43637
43637
|
var MAX_SEARCH_RESULTS = 100;
|
|
43638
|
+
var MAX_EMBEDDED_MEDIA_BYTES = Number.parseInt(
|
|
43639
|
+
process.env.GROK_MAX_EMBEDDED_MEDIA_BYTES || "5242880",
|
|
43640
|
+
10
|
|
43641
|
+
);
|
|
43642
|
+
var MEDIA_FETCH_TIMEOUT_MS = Number.parseInt(
|
|
43643
|
+
process.env.GROK_MEDIA_FETCH_TIMEOUT_MS || "15000",
|
|
43644
|
+
10
|
|
43645
|
+
);
|
|
43646
|
+
var MOCK_IMAGE_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
|
|
43647
|
+
var MOCK_VIDEO_BASE64 = "AAAAHGZ0eXBpc29tAAAAAGlzb21pc28yYXZjMQ==";
|
|
43638
43648
|
var xSearchSchema = paginationSchema.extend({
|
|
43639
43649
|
query: z3.string().min(1).max(500),
|
|
43640
43650
|
allowed_handles: z3.array(z3.string().min(1).max(50)).max(10).optional(),
|
|
@@ -43985,6 +43995,97 @@ function asArray(value) {
|
|
|
43985
43995
|
function asString(value) {
|
|
43986
43996
|
return typeof value === "string" && value.trim() ? value : void 0;
|
|
43987
43997
|
}
|
|
43998
|
+
function inferMimeTypeFromUrl(url3, kind) {
|
|
43999
|
+
try {
|
|
44000
|
+
const pathname = new URL(url3).pathname.toLowerCase();
|
|
44001
|
+
if (pathname.endsWith(".png")) return "image/png";
|
|
44002
|
+
if (pathname.endsWith(".jpg") || pathname.endsWith(".jpeg")) {
|
|
44003
|
+
return "image/jpeg";
|
|
44004
|
+
}
|
|
44005
|
+
if (pathname.endsWith(".webp")) return "image/webp";
|
|
44006
|
+
if (pathname.endsWith(".gif")) return "image/gif";
|
|
44007
|
+
if (pathname.endsWith(".mp4")) return "video/mp4";
|
|
44008
|
+
if (pathname.endsWith(".webm")) return "video/webm";
|
|
44009
|
+
if (pathname.endsWith(".mov")) return "video/quicktime";
|
|
44010
|
+
} catch {
|
|
44011
|
+
}
|
|
44012
|
+
return kind === "image" ? "image/png" : "video/mp4";
|
|
44013
|
+
}
|
|
44014
|
+
function normalizeMediaMimeType(value, kind, sourceUrl) {
|
|
44015
|
+
if (typeof value === "string") {
|
|
44016
|
+
const trimmed = value.split(";", 1)[0]?.trim().toLowerCase();
|
|
44017
|
+
if (trimmed && (kind === "image" && trimmed.startsWith("image/") || kind === "video" && trimmed.startsWith("video/"))) {
|
|
44018
|
+
return trimmed;
|
|
44019
|
+
}
|
|
44020
|
+
}
|
|
44021
|
+
return inferMimeTypeFromUrl(sourceUrl, kind) || "application/octet-stream";
|
|
44022
|
+
}
|
|
44023
|
+
async function fetchMediaAsBase64(sourceUrl, kind, client = axios_default) {
|
|
44024
|
+
try {
|
|
44025
|
+
const response = await client.get(sourceUrl, {
|
|
44026
|
+
responseType: "arraybuffer",
|
|
44027
|
+
timeout: MEDIA_FETCH_TIMEOUT_MS,
|
|
44028
|
+
maxContentLength: MAX_EMBEDDED_MEDIA_BYTES,
|
|
44029
|
+
maxBodyLength: MAX_EMBEDDED_MEDIA_BYTES
|
|
44030
|
+
});
|
|
44031
|
+
const buffer = Buffer.from(response.data);
|
|
44032
|
+
if (buffer.byteLength === 0 || buffer.byteLength > MAX_EMBEDDED_MEDIA_BYTES) {
|
|
44033
|
+
return void 0;
|
|
44034
|
+
}
|
|
44035
|
+
return {
|
|
44036
|
+
data: buffer.toString("base64"),
|
|
44037
|
+
mimeType: normalizeMediaMimeType(
|
|
44038
|
+
response.headers["content-type"],
|
|
44039
|
+
kind,
|
|
44040
|
+
sourceUrl
|
|
44041
|
+
)
|
|
44042
|
+
};
|
|
44043
|
+
} catch {
|
|
44044
|
+
return void 0;
|
|
44045
|
+
}
|
|
44046
|
+
}
|
|
44047
|
+
async function buildImageContent(imageUrl, client = axios_default) {
|
|
44048
|
+
const media = await fetchMediaAsBase64(imageUrl, "image", client);
|
|
44049
|
+
if (!media) {
|
|
44050
|
+
return void 0;
|
|
44051
|
+
}
|
|
44052
|
+
return {
|
|
44053
|
+
type: "image",
|
|
44054
|
+
data: media.data,
|
|
44055
|
+
mimeType: media.mimeType
|
|
44056
|
+
};
|
|
44057
|
+
}
|
|
44058
|
+
async function buildVideoContent(videoUrl, client = axios_default) {
|
|
44059
|
+
const media = await fetchMediaAsBase64(videoUrl, "video", client);
|
|
44060
|
+
if (!media) {
|
|
44061
|
+
return void 0;
|
|
44062
|
+
}
|
|
44063
|
+
return {
|
|
44064
|
+
type: "resource",
|
|
44065
|
+
resource: {
|
|
44066
|
+
uri: videoUrl,
|
|
44067
|
+
mimeType: media.mimeType,
|
|
44068
|
+
blob: media.data
|
|
44069
|
+
}
|
|
44070
|
+
};
|
|
44071
|
+
}
|
|
44072
|
+
function buildMockImageContent() {
|
|
44073
|
+
return {
|
|
44074
|
+
type: "image",
|
|
44075
|
+
data: MOCK_IMAGE_BASE64,
|
|
44076
|
+
mimeType: "image/png"
|
|
44077
|
+
};
|
|
44078
|
+
}
|
|
44079
|
+
function buildMockVideoContent(videoUrl) {
|
|
44080
|
+
return {
|
|
44081
|
+
type: "resource",
|
|
44082
|
+
resource: {
|
|
44083
|
+
uri: videoUrl,
|
|
44084
|
+
mimeType: "video/mp4",
|
|
44085
|
+
blob: MOCK_VIDEO_BASE64
|
|
44086
|
+
}
|
|
44087
|
+
};
|
|
44088
|
+
}
|
|
43988
44089
|
function extractNestedText(value) {
|
|
43989
44090
|
if (typeof value === "string") {
|
|
43990
44091
|
return value.trim() ? [value.trim()] : [];
|
|
@@ -44216,9 +44317,12 @@ function parseArguments(toolName, schema, args) {
|
|
|
44216
44317
|
}
|
|
44217
44318
|
return { data: parsed.data };
|
|
44218
44319
|
}
|
|
44219
|
-
function toolResult(text, structuredContent) {
|
|
44320
|
+
function toolResult(text, structuredContent, extraContent = []) {
|
|
44220
44321
|
return {
|
|
44221
|
-
content: [
|
|
44322
|
+
content: [
|
|
44323
|
+
{ type: "text", text },
|
|
44324
|
+
...extraContent
|
|
44325
|
+
],
|
|
44222
44326
|
structuredContent
|
|
44223
44327
|
};
|
|
44224
44328
|
}
|
|
@@ -44274,20 +44378,24 @@ function normalizeVideoGenerationResponse(raw, prompt, aspectRatio, imageUrl) {
|
|
|
44274
44378
|
request_id: asString(raw.request_id) || "unknown",
|
|
44275
44379
|
status: asString(raw.status) || "unknown",
|
|
44276
44380
|
image_url: imageUrl,
|
|
44277
|
-
video_url:
|
|
44381
|
+
video_url: extractVideoUrl(raw)
|
|
44278
44382
|
};
|
|
44279
44383
|
}
|
|
44280
|
-
function
|
|
44384
|
+
function extractVideoUrl(raw) {
|
|
44385
|
+
const nestedVideo = raw.video;
|
|
44386
|
+
return asString(raw.video_url) || (isRecord(nestedVideo) ? asString(nestedVideo.url) : void 0);
|
|
44387
|
+
}
|
|
44388
|
+
function normalizeVideoStatusResponse(raw, requestId) {
|
|
44281
44389
|
if (!isRecord(raw)) {
|
|
44282
44390
|
return {
|
|
44283
|
-
request_id:
|
|
44391
|
+
request_id: requestId,
|
|
44284
44392
|
status: "unknown"
|
|
44285
44393
|
};
|
|
44286
44394
|
}
|
|
44287
44395
|
return {
|
|
44288
|
-
request_id: asString(raw.request_id) ||
|
|
44396
|
+
request_id: asString(raw.request_id) || requestId,
|
|
44289
44397
|
status: asString(raw.status) || "unknown",
|
|
44290
|
-
video_url:
|
|
44398
|
+
video_url: extractVideoUrl(raw)
|
|
44291
44399
|
};
|
|
44292
44400
|
}
|
|
44293
44401
|
async function handleToolCall(name, args, client = xaiClient) {
|
|
@@ -44371,7 +44479,14 @@ async function handleToolCall(name, args, client = xaiClient) {
|
|
|
44371
44479
|
prompt,
|
|
44372
44480
|
aspect_ratio
|
|
44373
44481
|
);
|
|
44374
|
-
|
|
44482
|
+
const mediaContent = IS_MOCK ? [buildMockImageContent()] : (await Promise.all(
|
|
44483
|
+
output.images.map(
|
|
44484
|
+
(image) => buildImageContent(image.url, client)
|
|
44485
|
+
)
|
|
44486
|
+
)).filter(
|
|
44487
|
+
(item) => item !== void 0
|
|
44488
|
+
);
|
|
44489
|
+
return toolResult(renderImagine(output), output, mediaContent);
|
|
44375
44490
|
}
|
|
44376
44491
|
case "grok_animate": {
|
|
44377
44492
|
const parsed = parseArguments(name, animateSchema, args);
|
|
@@ -44391,7 +44506,16 @@ async function handleToolCall(name, args, client = xaiClient) {
|
|
|
44391
44506
|
aspect_ratio,
|
|
44392
44507
|
image_url
|
|
44393
44508
|
);
|
|
44394
|
-
|
|
44509
|
+
const mediaContent = output.video_url === void 0 ? [] : IS_MOCK ? [buildMockVideoContent(output.video_url)] : (await Promise.all([
|
|
44510
|
+
buildVideoContent(output.video_url, client)
|
|
44511
|
+
])).filter(
|
|
44512
|
+
(item) => item !== void 0
|
|
44513
|
+
);
|
|
44514
|
+
return toolResult(
|
|
44515
|
+
renderVideoGeneration(output),
|
|
44516
|
+
output,
|
|
44517
|
+
mediaContent
|
|
44518
|
+
);
|
|
44395
44519
|
}
|
|
44396
44520
|
case "grok_check_video_status": {
|
|
44397
44521
|
const parsed = parseArguments(name, videoStatusSchema, args);
|
|
@@ -44399,9 +44523,21 @@ async function handleToolCall(name, args, client = xaiClient) {
|
|
|
44399
44523
|
return parsed.error;
|
|
44400
44524
|
}
|
|
44401
44525
|
const { request_id } = parsed.data;
|
|
44402
|
-
const rawResponse = IS_MOCK ? MOCK_FIXTURES.grok_check_video_status : (await client.get(`/videos
|
|
44403
|
-
const output = normalizeVideoStatusResponse(
|
|
44404
|
-
|
|
44526
|
+
const rawResponse = IS_MOCK ? MOCK_FIXTURES.grok_check_video_status : (await client.get(`/videos/${request_id}`)).data;
|
|
44527
|
+
const output = normalizeVideoStatusResponse(
|
|
44528
|
+
rawResponse,
|
|
44529
|
+
request_id
|
|
44530
|
+
);
|
|
44531
|
+
const mediaContent = output.video_url === void 0 ? [] : IS_MOCK ? [buildMockVideoContent(output.video_url)] : (await Promise.all([
|
|
44532
|
+
buildVideoContent(output.video_url, client)
|
|
44533
|
+
])).filter(
|
|
44534
|
+
(item) => item !== void 0
|
|
44535
|
+
);
|
|
44536
|
+
return toolResult(
|
|
44537
|
+
renderVideoStatus(output),
|
|
44538
|
+
output,
|
|
44539
|
+
mediaContent
|
|
44540
|
+
);
|
|
44405
44541
|
}
|
|
44406
44542
|
default:
|
|
44407
44543
|
throw new McpError(
|