@fre4x/grok 1.0.54 → 1.0.57

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.
Files changed (2) hide show
  1. package/dist/index.js +167 -106
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -18444,22 +18444,26 @@ import { fileURLToPath } from "node:url";
18444
18444
  // ../packages/shared/dist/errors.js
18445
18445
  function createApiError(message, statusCode) {
18446
18446
  let hint = "Check your network connection and retry.";
18447
+ let type = "Service Error";
18447
18448
  if (statusCode === 429) {
18448
- hint = "Rate limit exceeded. Suggestion: Wait for 60 seconds (Exponential Backoff) before retrying or reduce concurrent calls.";
18449
+ type = "Rate Limit";
18450
+ hint = "Request volume is too high. Suggestion: Wait briefly before retrying or reduce concurrent calls.";
18449
18451
  } else if (statusCode === 401 || statusCode === 403) {
18450
- hint = "Authentication failed. Suggestion: Verify your API key or token in the environment configuration.";
18452
+ type = "Authentication Error";
18453
+ hint = "The request could not be authorized. Suggestion: Verify your API key or token in the environment configuration.";
18451
18454
  } else if (statusCode && statusCode >= 500) {
18452
- hint = "Upstream service is having issues. Suggestion: This is a temporary external error. Try again in a few minutes.";
18455
+ type = "Upstream Error";
18456
+ hint = "The remote service is experiencing temporary issues. Suggestion: Try again in a few minutes.";
18453
18457
  } else if (statusCode === 404) {
18454
- hint = "The requested resource was not found. Suggestion: Check if the ID or Ticker is correct, or search for it first.";
18458
+ type = "Not Found";
18459
+ hint = "The requested information could not be found. Suggestion: Check if the ID, Ticker, or query parameters are correct.";
18455
18460
  }
18456
- const detail = statusCode ? ` (HTTP ${statusCode})` : "";
18457
18461
  return {
18458
18462
  isError: true,
18459
18463
  content: [
18460
18464
  {
18461
18465
  type: "text",
18462
- text: `API Error${detail}: ${message}
18466
+ text: `${type}: ${message}
18463
18467
 
18464
18468
  **Next Action**: ${hint}`
18465
18469
  }
@@ -32541,7 +32545,8 @@ config(en_default());
32541
32545
  var zod_default = external_exports;
32542
32546
 
32543
32547
  // ../packages/shared/dist/pagination.js
32544
- var z2 = external_exports || zod_default || zod_exports;
32548
+ var zodCompat = zod_exports;
32549
+ var z2 = zodCompat.z ?? zodCompat.default?.z ?? zodCompat.default ?? zodCompat;
32545
32550
  var paginationSchema = z2.object({
32546
32551
  limit: z2.number().int().min(1).max(100).default(20).describe("Maximum results to return (1\u2013100, default 20)"),
32547
32552
  offset: z2.number().int().min(0).default(0).describe("Number of results to skip for pagination (default 0)")
@@ -45932,72 +45937,94 @@ var {
45932
45937
  var IS_MOCK = process.env.MOCK === "true" || process.env.GROK_MOCK === "true";
45933
45938
  var MOCK_FIXTURES = {
45934
45939
  grok_search_x: {
45935
- output_text: "Recent X discussion highlights Grok MCP availability and early developer feedback.",
45936
- choices: [
45940
+ id: "mock_resp_1",
45941
+ model: "grok-4-1-fast-reasoning",
45942
+ output: [
45937
45943
  {
45938
- message: {
45939
- role: "assistant",
45940
- content: "Recent posts discuss Grok MCP availability and positive early feedback."
45941
- },
45942
- citations: [
45944
+ type: "server_tool_use",
45945
+ call_id: "call_search1",
45946
+ server_tool_name: "x_search",
45947
+ input: { query: "Grok MCP" }
45948
+ },
45949
+ {
45950
+ type: "server_tool_result",
45951
+ call_id: "call_search1",
45952
+ output: [
45943
45953
  {
45944
- title: "xAI launch post",
45945
- url: "https://x.com/xai/status/123456789"
45954
+ type: "server_tool_result_content",
45955
+ content: JSON.stringify({
45956
+ results: [
45957
+ {
45958
+ id: "123456789",
45959
+ text: "Grok is now available via MCP! #xAI #Grok #MCP",
45960
+ author: "xAI",
45961
+ created_at: "2026-03-04T12:00:00Z",
45962
+ url: "https://x.com/xai/status/123456789"
45963
+ },
45964
+ {
45965
+ id: "123456790",
45966
+ text: "Testing Grok MCP today and the mock mode is handy.",
45967
+ author: "dev_agent",
45968
+ created_at: "2026-03-04T14:30:00Z",
45969
+ url: "https://x.com/dev_agent/status/123456790"
45970
+ }
45971
+ ]
45972
+ })
45946
45973
  }
45947
- ],
45948
- tool_calls: [
45974
+ ]
45975
+ },
45976
+ {
45977
+ type: "message",
45978
+ role: "assistant",
45979
+ content: [
45949
45980
  {
45950
- type: "x_search",
45951
- outputs: [
45952
- {
45953
- id: "123456789",
45954
- text: "Grok is now available via MCP! #xAI #Grok #MCP",
45955
- author: "xAI",
45956
- created_at: "2026-03-04T12:00:00Z",
45957
- url: "https://x.com/xai/status/123456789"
45958
- },
45959
- {
45960
- id: "123456790",
45961
- text: "Testing Grok MCP today and the mock mode is handy.",
45962
- author: "dev_agent",
45963
- created_at: "2026-03-04T14:30:00Z",
45964
- url: "https://x.com/dev_agent/status/123456790"
45965
- }
45966
- ]
45981
+ type: "output_text",
45982
+ text: "Recent posts discuss Grok MCP availability and positive early feedback."
45967
45983
  }
45968
45984
  ]
45969
45985
  }
45970
45986
  ]
45971
45987
  },
45972
45988
  grok_search_web: {
45973
- output_text: "The MCP documentation is the clearest reference for integrating external tools with language models.",
45974
- choices: [
45989
+ id: "mock_resp_2",
45990
+ model: "grok-4-1-fast-reasoning",
45991
+ output: [
45975
45992
  {
45976
- message: {
45977
- role: "assistant",
45978
- content: "The MCP documentation is the primary reference and includes tool, resource, and prompt specs."
45979
- },
45980
- citations: [
45993
+ type: "server_tool_use",
45994
+ call_id: "call_web1",
45995
+ server_tool_name: "web_search",
45996
+ input: { query: "MCP Documentation" }
45997
+ },
45998
+ {
45999
+ type: "server_tool_result",
46000
+ call_id: "call_web1",
46001
+ output: [
45981
46002
  {
45982
- title: "MCP Documentation",
45983
- url: "https://modelcontextprotocol.io"
46003
+ type: "server_tool_result_content",
46004
+ content: JSON.stringify({
46005
+ results: [
46006
+ {
46007
+ title: "Model Context Protocol Documentation",
46008
+ url: "https://modelcontextprotocol.io",
46009
+ snippet: "MCP is an open standard that enables AI models to connect to external tools."
46010
+ },
46011
+ {
46012
+ title: "Tools - MCP Specification",
46013
+ url: "https://modelcontextprotocol.io/specification/draft/server/tools",
46014
+ snippet: "Servers expose tools through tools/list and tools/call with JSON Schema metadata."
46015
+ }
46016
+ ]
46017
+ })
45984
46018
  }
45985
- ],
45986
- tool_calls: [
46019
+ ]
46020
+ },
46021
+ {
46022
+ type: "message",
46023
+ role: "assistant",
46024
+ content: [
45987
46025
  {
45988
- type: "web_search",
45989
- outputs: [
45990
- {
45991
- title: "Model Context Protocol Documentation",
45992
- url: "https://modelcontextprotocol.io",
45993
- snippet: "MCP is an open standard that enables AI models to connect to external tools."
45994
- },
45995
- {
45996
- title: "Tools - MCP Specification",
45997
- url: "https://modelcontextprotocol.io/specification/draft/server/tools",
45998
- snippet: "Servers expose tools through tools/list and tools/call with JSON Schema metadata."
45999
- }
46000
- ]
46026
+ type: "output_text",
46027
+ text: "The MCP documentation is the primary reference and includes tool, resource, and prompt specs."
46001
46028
  }
46002
46029
  ]
46003
46030
  }
@@ -46023,7 +46050,8 @@ var MOCK_FIXTURES = {
46023
46050
  };
46024
46051
 
46025
46052
  // src/index.ts
46026
- var z3 = external_exports || zod_default || zod_exports;
46053
+ var zodCompat2 = zod_exports;
46054
+ var z3 = zodCompat2.z ?? zodCompat2.default?.z ?? zodCompat2.default ?? zodCompat2;
46027
46055
  var XAI_API_KEY = process.env.XAI_API_KEY;
46028
46056
  var PACKAGE_VERSION = getPackageVersion(import.meta.url);
46029
46057
  var GROK_SEARCH_MODEL = process.env.GROK_SEARCH_MODEL || "grok-4-1-fast-reasoning";
@@ -46040,6 +46068,9 @@ var MEDIA_FETCH_TIMEOUT_MS = Number.parseInt(
46040
46068
  );
46041
46069
  var MOCK_IMAGE_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
46042
46070
  var MOCK_VIDEO_BASE64 = "AAAAHGZ0eXBpc29tAAAAAGlzb21pc28yYXZjMQ==";
46071
+ function getErrorMessage(error48) {
46072
+ return error48 instanceof Error ? error48.message : String(error48);
46073
+ }
46043
46074
  var xSearchSchema = paginationSchema.extend({
46044
46075
  query: z3.string().min(1).max(500),
46045
46076
  allowed_handles: z3.array(z3.string().min(1).max(50)).max(10).optional(),
@@ -46135,11 +46166,10 @@ async function handleGrokSearchX(params, client = xaiClient) {
46135
46166
  tools: [
46136
46167
  {
46137
46168
  type: "x_search",
46138
- x_search: {
46139
- allowed_x_handles: allowed_handles,
46140
- from_date,
46141
- to_date
46142
- }
46169
+ query,
46170
+ allowed_x_handles: allowed_handles,
46171
+ from_date,
46172
+ to_date
46143
46173
  }
46144
46174
  ]
46145
46175
  });
@@ -46168,7 +46198,7 @@ async function handleGrokSearchX(params, client = xaiClient) {
46168
46198
  } catch (error48) {
46169
46199
  extraText = `
46170
46200
 
46171
- \u26A0\uFE0F Failed to save results: ${error48.message}`;
46201
+ \u26A0\uFE0F Failed to save results: ${getErrorMessage(error48)}`;
46172
46202
  }
46173
46203
  }
46174
46204
  return toolResult(renderXSearch(output) + extraText, output);
@@ -46206,7 +46236,8 @@ async function handleGrokSearchWeb(params, client = xaiClient) {
46206
46236
  tools: [
46207
46237
  {
46208
46238
  type: "web_search",
46209
- web_search: { allowed_domains }
46239
+ query,
46240
+ allowed_domains
46210
46241
  }
46211
46242
  ]
46212
46243
  });
@@ -46235,7 +46266,7 @@ async function handleGrokSearchWeb(params, client = xaiClient) {
46235
46266
  } catch (error48) {
46236
46267
  extraText = `
46237
46268
 
46238
- \u26A0\uFE0F Failed to save results: ${error48.message}`;
46269
+ \u26A0\uFE0F Failed to save results: ${getErrorMessage(error48)}`;
46239
46270
  }
46240
46271
  }
46241
46272
  return toolResult(renderWebSearch(output) + extraText, output);
@@ -46290,7 +46321,7 @@ ${savedPaths.join("\n")}`;
46290
46321
  } catch (error48) {
46291
46322
  extraText = `
46292
46323
 
46293
- \u26A0\uFE0F Failed to save images: ${error48.message}`;
46324
+ \u26A0\uFE0F Failed to save images: ${getErrorMessage(error48)}`;
46294
46325
  }
46295
46326
  }
46296
46327
  return toolResult(
@@ -46361,7 +46392,7 @@ async function handleGrokVideoStatus(params, client = xaiClient) {
46361
46392
  } catch (error48) {
46362
46393
  extraText = `
46363
46394
 
46364
- \u26A0\uFE0F Failed to save video: ${error48.message}`;
46395
+ \u26A0\uFE0F Failed to save video: ${getErrorMessage(error48)}`;
46365
46396
  }
46366
46397
  }
46367
46398
  }
@@ -46551,18 +46582,6 @@ function buildMockVideoContent(videoUrl) {
46551
46582
  buffer: Buffer.from(MOCK_VIDEO_BASE64, "base64")
46552
46583
  };
46553
46584
  }
46554
- function extractNestedText(value) {
46555
- if (typeof value === "string") {
46556
- return value.trim() ? [value.trim()] : [];
46557
- }
46558
- if (Array.isArray(value)) {
46559
- return value.flatMap((item) => extractNestedText(item));
46560
- }
46561
- if (isRecord(value)) {
46562
- return Object.values(value).flatMap((item) => extractNestedText(item));
46563
- }
46564
- return [];
46565
- }
46566
46585
  function dedupeByKey(items, keyFn) {
46567
46586
  const seen = /* @__PURE__ */ new Set();
46568
46587
  const result = [];
@@ -46592,40 +46611,82 @@ function getResponseSummary(raw) {
46592
46611
  if (!isRecord(raw)) {
46593
46612
  return "";
46594
46613
  }
46595
- const outputText = asString(raw.output_text);
46596
- if (outputText) {
46597
- return outputText;
46614
+ const outputItems = asArray(raw.output);
46615
+ const summaries = [];
46616
+ for (const item of outputItems) {
46617
+ if (!isRecord(item)) continue;
46618
+ if (asString(item.type) === "message" && asString(item.role) === "assistant") {
46619
+ const content = asArray(item.content);
46620
+ for (const part of content) {
46621
+ if (isRecord(part) && asString(part.type) === "output_text") {
46622
+ const text = asString(part.text);
46623
+ if (text) {
46624
+ summaries.push(text);
46625
+ }
46626
+ }
46627
+ }
46628
+ }
46598
46629
  }
46599
- const choiceMessages = asArray(raw.choices).map((choice) => isRecord(choice) ? choice.message : void 0).flatMap((message) => extractNestedText(message));
46600
- return choiceMessages.join("\n").trim();
46630
+ return summaries.join("\n").trim();
46601
46631
  }
46602
- function getToolOutputs(raw, toolType) {
46632
+ function getToolOutputs(raw, _toolType) {
46603
46633
  if (!isRecord(raw)) {
46604
46634
  return [];
46605
46635
  }
46606
- const toolCalls = asArray(raw.choices).flatMap((choice) => {
46607
- if (!isRecord(choice)) {
46608
- return [];
46636
+ const outputItems = asArray(raw.output);
46637
+ const results = [];
46638
+ for (const item of outputItems) {
46639
+ if (!isRecord(item)) continue;
46640
+ const itemType = asString(item.type);
46641
+ if (itemType === "server_tool_result") {
46642
+ const itemOutput = asArray(item.output);
46643
+ for (const outPart of itemOutput) {
46644
+ if (!isRecord(outPart)) continue;
46645
+ const outType = asString(outPart.type);
46646
+ if (outType !== "server_tool_result_content") continue;
46647
+ const content = asString(outPart.content);
46648
+ if (!content) continue;
46649
+ try {
46650
+ const parsed = JSON.parse(content);
46651
+ results.push(parsed);
46652
+ } catch {
46653
+ }
46654
+ }
46609
46655
  }
46610
- const choiceToolCalls = asArray(choice.tool_calls);
46611
- const messageToolCalls = isRecord(choice.message) ? asArray(choice.message.tool_calls) : [];
46612
- return [...choiceToolCalls, ...messageToolCalls];
46613
- });
46614
- return toolCalls.filter(
46615
- (call) => isRecord(call) && asString(call.type) === toolType && Array.isArray(call.outputs)
46616
- ).flatMap((call) => asArray(call.outputs));
46656
+ }
46657
+ return results;
46617
46658
  }
46618
46659
  function getCitations(raw) {
46619
46660
  const candidates = [];
46620
- for (const citation of collectRecords(raw).flatMap((record2) => asArray(record2.citations)).filter(isRecord)) {
46621
- const url3 = asString(citation.url);
46622
- if (!url3) {
46623
- continue;
46661
+ if (!isRecord(raw)) {
46662
+ return [];
46663
+ }
46664
+ const outputItems = asArray(raw.output);
46665
+ for (const item of outputItems) {
46666
+ if (!isRecord(item)) continue;
46667
+ if (asString(item.type) !== "server_tool_result") continue;
46668
+ const itemOutput = asArray(item.output);
46669
+ for (const outPart of itemOutput) {
46670
+ if (!isRecord(outPart)) continue;
46671
+ if (asString(outPart.type) !== "server_tool_result_content")
46672
+ continue;
46673
+ const content = asString(outPart.content);
46674
+ if (!content) continue;
46675
+ try {
46676
+ const parsed = JSON.parse(content);
46677
+ const records = collectRecords(parsed);
46678
+ for (const record2 of records) {
46679
+ const url3 = asString(record2.url);
46680
+ if (url3) {
46681
+ candidates.push({
46682
+ url: url3,
46683
+ title: asString(record2.title)
46684
+ });
46685
+ }
46686
+ }
46687
+ } catch {
46688
+ }
46624
46689
  }
46625
- candidates.push({
46626
- url: url3,
46627
- title: asString(citation.title)
46628
- });
46629
46690
  }
46630
46691
  return dedupeByKey(candidates, (citation) => citation.url);
46631
46692
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fre4x/grok",
3
- "version": "1.0.54",
3
+ "version": "1.0.57",
4
4
  "description": "An MCP server for xAI (Grok) with search and generation capabilities.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",