@gullabs/xai 0.4.1 → 0.5.1

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/index.js CHANGED
@@ -171,6 +171,11 @@ function isXaiAuthFailureBody(rawErr) {
171
171
  const text = extractXaiErrorBodyText(rawErr);
172
172
  return text !== void 0 && text.startsWith(XAI_AUTH_ERROR_MESSAGE_PREFIX);
173
173
  }
174
+ var XAI_SAFETY_CHECK_MESSAGE_PREFIX = "Content violates usage guidelines";
175
+ function isXaiSafetyCheckBody(rawErr) {
176
+ const text = extractXaiErrorBodyText(rawErr);
177
+ return text !== void 0 && text.startsWith(XAI_SAFETY_CHECK_MESSAGE_PREFIX);
178
+ }
174
179
  var XAI_TRANSPORT_ERROR_PATTERN = /connection error|econnreset|econnrefused|etimedout|eai_again|epipe|socket hang up|fetch failed/i;
175
180
  function matchesXaiTransportSignature(err) {
176
181
  if (!(err instanceof Error)) return false;
@@ -209,6 +214,16 @@ function classifyXaiError(rawErr) {
209
214
  cause: base.cause ?? rawErr
210
215
  });
211
216
  }
217
+ if (base.httpStatus === 403 && isXaiSafetyCheckBody(rawErr)) {
218
+ const bodyText = extractXaiErrorBodyText(rawErr);
219
+ return new LlmError(bodyText ?? base.message, {
220
+ kind: "content_filter",
221
+ retryable: false,
222
+ httpStatus: base.httpStatus,
223
+ provider: "xai",
224
+ cause: base.cause ?? rawErr
225
+ });
226
+ }
212
227
  if (base.kind === "unknown" && isXaiTransportError(rawErr)) {
213
228
  return new LlmError(base.message, {
214
229
  kind: "server",
@@ -260,10 +275,19 @@ function xaiAdapter(opts) {
260
275
  if (genConfig.maxOutputTokens !== void 0) {
261
276
  params.max_output_tokens = genConfig.maxOutputTokens;
262
277
  }
278
+ const admittedTiers = req.modelDescriptor?.capabilities?.serviceTiers;
263
279
  if (genConfig.serviceTier !== void 0) {
264
- throw badXaiRequest(
265
- `serviceTier is not supported for xai models (got "${genConfig.serviceTier}").`
266
- );
280
+ if (admittedTiers === void 0 || !admittedTiers.includes(genConfig.serviceTier)) {
281
+ throw badXaiRequest(
282
+ `serviceTier is not supported for xai model "${model}" (got "${genConfig.serviceTier}").`
283
+ );
284
+ }
285
+ if (genConfig.serviceTier !== "priority") {
286
+ throw badXaiRequest(
287
+ `serviceTier "${genConfig.serviceTier}" is not supported for xai model "${model}" (only "priority" is admitted).`
288
+ );
289
+ }
290
+ params.service_tier = "priority";
267
291
  }
268
292
  const reasoning = genConfig.reasoning;
269
293
  if (reasoning !== void 0) {
@@ -274,13 +298,13 @@ function xaiAdapter(opts) {
274
298
  }
275
299
  if (reasoning.effort !== void 0) {
276
300
  const effort = reasoning.effort;
277
- if (effort !== "low" && effort !== "high") {
301
+ if (effort === "none") {
278
302
  throw badXaiRequest(
279
- `reasoning.effort "${effort}" is not supported for xai model "${model}" (only "low" and "high" are admitted).`
303
+ `reasoning.effort "none" is not supported for xai model "${model}".`
280
304
  );
281
305
  }
282
306
  const admitted = req.modelDescriptor?.capabilities?.admittedReasoningEfforts;
283
- if (admitted !== void 0 && !admitted.includes(effort)) {
307
+ if (admitted === void 0 || !admitted.includes(effort)) {
284
308
  throw badXaiRequest(
285
309
  `reasoning.effort "${effort}" is not supported for xai model "${model}".`
286
310
  );
@@ -358,6 +382,7 @@ function xaiAdapter(opts) {
358
382
  if (isPlainRecord(response.metadata)) {
359
383
  providerMeta["metadata"] = response.metadata;
360
384
  }
385
+ const servedServiceTier = typeof response.service_tier === "string" && response.service_tier.length > 0 ? response.service_tier : void 0;
361
386
  const result = {
362
387
  model: response.model,
363
388
  usage,
@@ -367,6 +392,7 @@ function xaiAdapter(opts) {
367
392
  ...text.length > 0 ? { text } : {},
368
393
  ...reasoningText !== void 0 ? { reasoningText } : {},
369
394
  ...rawStructured !== void 0 ? { rawStructured } : {},
395
+ ...servedServiceTier !== void 0 ? { servedServiceTier } : {},
370
396
  ...Object.keys(providerMeta).length > 0 ? { providerMetadata: providerMeta } : {}
371
397
  };
372
398
  return result;
@@ -873,6 +899,55 @@ var Grok45ConfigSchema = z.strictObject({
873
899
  description: "Strict Responses API config for model grok-4.5. Level reasoning (low/high only), tunable sampling, no service tiers, structured output, vision, priced.",
874
900
  examples: [{ reasoning: { effort: "high" } }]
875
901
  });
902
+ var Grok46ConfigSchema = z.strictObject({
903
+ temperature: z.number().optional().meta({
904
+ title: "Temperature",
905
+ description: "Sampling temperature forwarded verbatim to grok-4.6."
906
+ }),
907
+ topP: z.number().optional().meta({
908
+ title: "Top P",
909
+ description: "Nucleus sampling parameter forwarded verbatim to grok-4.6."
910
+ }),
911
+ maxOutputTokens: z.number().int().positive().optional().meta({
912
+ title: "Max Output Tokens",
913
+ description: "Maximum output token cap for grok-4.6. No artificial ceiling \u2014 xAI accepts arbitrarily large values; truncation surfaces as finishReason:'length', not an error."
914
+ }),
915
+ reasoning: z.strictObject({
916
+ effort: z.enum(["low", "medium", "high", "xhigh"]).meta({
917
+ title: "Reasoning Effort",
918
+ description: 'Reasoning effort for grok-4.6. Live-verified 2026-08-12: "low", "medium", "high", and "xhigh" are accepted; "none" is rejected. Vendor default when omitted is "high".'
919
+ })
920
+ }).optional().meta({
921
+ title: "Reasoning",
922
+ description: "grok-4.6 effort-level reasoning configuration. No budgetTokens field \u2014 xAI uses level-style reasoning, not token budgets."
923
+ }),
924
+ serviceTier: z.literal("priority").optional().meta({
925
+ title: "Service Tier",
926
+ description: 'xAI priority processing for grok-4.6 (Responses `service_tier: "priority"`). Echo live-verified 2026-08-12. Bills at 2\xD7 after the cache discount (uncached standard-list 2\xD7 confirmed by live ticks; cached/long-context legs follow the official 2\xD7 rule). Omitted requests stay on xAI default. "flex"/"standard"/"batch" are rejected.'
927
+ }),
928
+ timeoutMs: z.number().int().positive().optional().meta({
929
+ title: "Timeout",
930
+ description: "Logical request timeout in milliseconds."
931
+ }),
932
+ providerOptions: z.strictObject({
933
+ xai: z.strictObject({
934
+ promptCacheKey: z.string().min(1).optional().meta({
935
+ title: "Prompt Cache Key",
936
+ description: "xAI conversation-routing cache key \u2014 maps to Responses API `prompt_cache_key`."
937
+ })
938
+ }).optional().meta({
939
+ title: "xAI Provider Options",
940
+ description: "Allowlisted xAI provider options for grok-4.6."
941
+ })
942
+ }).optional().meta({
943
+ title: "Provider Options",
944
+ description: "Provider-specific options accepted for grok-4.6."
945
+ })
946
+ }).meta({
947
+ title: "Grok46Config",
948
+ description: "Strict Responses API config for model grok-4.6. Level reasoning (low/medium/high/xhigh), optional priority service tier, tunable sampling, structured output, vision, priced.",
949
+ examples: [{ reasoning: { effort: "high" } }]
950
+ });
876
951
 
877
952
  // src/models.ts
878
953
  var grok45ModelDescriptor = {
@@ -890,20 +965,55 @@ var grok45ModelDescriptor = {
890
965
  sampling: "tunable",
891
966
  caching: { explicit: false, minTokens: 0 },
892
967
  grounding: false
893
- // No serviceTiers key — xai has no service-tier concept.
968
+ // No serviceTiers key — grok-4.5 has no admitted service-tier vocabulary.
894
969
  },
895
970
  configSchema: Grok45ConfigSchema,
896
971
  configJsonSchema: toConfigJsonSchema(Grok45ConfigSchema),
897
972
  validateConfig: zodToStandardSchema(Grok45ConfigSchema)
898
973
  };
899
- var xaiModelDescriptors = [grok45ModelDescriptor];
974
+ var grok46ModelDescriptor = {
975
+ model: "grok-4.6",
976
+ provider: "xai",
977
+ pricingFamily: "grok-4.6",
978
+ capabilities: {
979
+ reasoning: true,
980
+ reasoningApi: "level",
981
+ admittedReasoningEfforts: ["low", "medium", "high", "xhigh"],
982
+ structuredOutput: true,
983
+ nativeStructuredOutput: true,
984
+ vision: true,
985
+ audioInput: false,
986
+ sampling: "tunable",
987
+ caching: { explicit: false, minTokens: 0 },
988
+ grounding: false,
989
+ serviceTiers: ["priority"]
990
+ },
991
+ configSchema: Grok46ConfigSchema,
992
+ configJsonSchema: toConfigJsonSchema(Grok46ConfigSchema),
993
+ validateConfig: zodToStandardSchema(Grok46ConfigSchema)
994
+ };
995
+ var xaiModelDescriptors = [
996
+ grok45ModelDescriptor,
997
+ grok46ModelDescriptor
998
+ ];
900
999
  var xaiRegistry = createModelRegistry(xaiModelDescriptors);
901
1000
 
902
1001
  // src/pricing.ts
903
- var xaiPricingVersion = "xai-2026-07-09";
1002
+ var xaiPricingVersion = "xai-2026-08-12";
904
1003
  var XAI_PRICING = Object.freeze({
905
- // ── grok-4.5 ── $2.00/$6.00 (≤200k), $4.00/$12.00 (>200k); cached $0.50/$1.00
1004
+ // ── grok-4.5 ── $2.00/$6.00 (≤200k), $4.00/$12.00 (>200k); cached $0.30/$0.60
906
1005
  "grok-4.5": {
1006
+ inputPerM: 2e6,
1007
+ cachedPerM: 3e5,
1008
+ outputPerM: 6e6,
1009
+ gt200k: {
1010
+ inputPerM: 4e6,
1011
+ cachedPerM: 6e5,
1012
+ outputPerM: 12e6
1013
+ }
1014
+ },
1015
+ // ── grok-4.6 ── $2.00/$6.00 (≤200k), $4.00/$12.00 (>200k); cached $0.50/$1.00
1016
+ "grok-4.6": {
907
1017
  inputPerM: 2e6,
908
1018
  cachedPerM: 5e5,
909
1019
  outputPerM: 6e6,
@@ -911,7 +1021,9 @@ var XAI_PRICING = Object.freeze({
911
1021
  inputPerM: 4e6,
912
1022
  cachedPerM: 1e6,
913
1023
  outputPerM: 12e6
914
- }
1024
+ },
1025
+ // Confirmed 2026-08-12 by fixture 12 cost_in_usd_ticks (2× list).
1026
+ priorityFactor: 2
915
1027
  }
916
1028
  });
917
1029
  var LONG_CONTEXT_THRESHOLD = 2e5;
@@ -940,22 +1052,29 @@ function computeXaiCost(model, usage, tier) {
940
1052
  unpricedReason: `Unknown model "${model}"; no pricing entry found.`
941
1053
  };
942
1054
  }
943
- if (tier !== void 0) {
944
- return {
945
- microUsd: null,
946
- usd: null,
947
- pricingVersion: xaiPricingVersion,
948
- confidence: "estimated",
949
- details: { input: 0, cached: 0, output: 0 },
950
- unpricedReason: `Unknown service tier "${tier}"; xai has no service tiers, refusing to guess a pricing multiplier.`
951
- };
1055
+ let factor = 1;
1056
+ if (tier !== void 0 && tier !== "default") {
1057
+ if (tier === "priority" && rates.priorityFactor !== void 0) {
1058
+ factor = rates.priorityFactor;
1059
+ } else {
1060
+ return {
1061
+ microUsd: null,
1062
+ usd: null,
1063
+ pricingVersion: xaiPricingVersion,
1064
+ confidence: "estimated",
1065
+ details: { input: 0, cached: 0, output: 0 },
1066
+ unpricedReason: `Unknown service tier "${tier}"; xai model "${model}" has no such tier, refusing to guess a pricing multiplier.`
1067
+ };
1068
+ }
952
1069
  }
953
1070
  const base = selectRates(rates, usage.inputTokens);
954
1071
  const cached = usage.cachedInputTokens ?? 0;
955
1072
  const billableInput = Math.max(0, usage.inputTokens - cached);
956
- const inputCost = Math.round(billableInput * base.inputPerM / 1e6);
957
- const cachedCost = Math.round(cached * base.cachedPerM / 1e6);
958
- const outputCost = Math.round(usage.outputTokens * base.outputPerM / 1e6);
1073
+ const inputCost = Math.round(billableInput * base.inputPerM * factor / 1e6);
1074
+ const cachedCost = Math.round(cached * base.cachedPerM * factor / 1e6);
1075
+ const outputCost = Math.round(
1076
+ usage.outputTokens * base.outputPerM * factor / 1e6
1077
+ );
959
1078
  const microUsd = inputCost + cachedCost + outputCost;
960
1079
  return {
961
1080
  microUsd,
@@ -993,6 +1112,6 @@ function xaiProvider(opts) {
993
1112
  };
994
1113
  }
995
1114
 
996
- export { Grok45ConfigSchema, XAI_FILES_DEFAULT_BASE_URL, XAI_FILE_MAX_BYTES, XAI_FILE_TTL_MAX_SECONDS, XAI_FILE_TTL_MIN_SECONDS, XAI_PRICING, XaiFileStore, buildXaiClient, classifyXaiError, computeXaiCost, grok45ModelDescriptor, requireApiKey, xaiAdapter, xaiModelDescriptors, xaiPricingSource, xaiPricingVersion, xaiProvider, xaiRegistry };
1115
+ export { Grok45ConfigSchema, Grok46ConfigSchema, XAI_FILES_DEFAULT_BASE_URL, XAI_FILE_MAX_BYTES, XAI_FILE_TTL_MAX_SECONDS, XAI_FILE_TTL_MIN_SECONDS, XAI_PRICING, XaiFileStore, buildXaiClient, classifyXaiError, computeXaiCost, grok45ModelDescriptor, grok46ModelDescriptor, requireApiKey, xaiAdapter, xaiModelDescriptors, xaiPricingSource, xaiPricingVersion, xaiProvider, xaiRegistry };
997
1116
  //# sourceMappingURL=index.js.map
998
1117
  //# sourceMappingURL=index.js.map