@carrierllc/mcp 0.13.87 → 0.13.88

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
@@ -91,7 +91,7 @@ import {
91
91
  verifyStorefront,
92
92
  withOcsListSummary,
93
93
  withTemplateResellerId
94
- } from "./chunk-RCYBPELI.js";
94
+ } from "./chunk-BDW6EFM3.js";
95
95
  import "./chunk-SHKKVIIA.js";
96
96
 
97
97
  // src/index.ts
@@ -275,13 +275,23 @@ var NANO_USD_PER_USD = 1e9;
275
275
  var AI_COST_MULTIPLIER = 4;
276
276
  var AI_PRICE_MAX_AGE_DAYS = 30;
277
277
  var DAY_MS = 864e5;
278
+ var AI_ORG_REQUIRED_MESSAGE = "Select or create an organization to use AI credits. In the console use the organization switcher; in the CLI run `carrier login` and pick an organization; in an MCP client reconnect and choose an organization.";
278
279
  var AiCreditError = class extends Error {
279
- constructor(message, code) {
280
+ /**
281
+ * @param funded Set by the wallet service on a refused reservation. True
282
+ * when the organisation holds a plan credit (the free-plan grant or a paid
283
+ * Pro period), false when it was never provisioned, undefined when the
284
+ * wallet service did not say. Only the wallet service can tell an empty
285
+ * ledger that was spent apart from one that was never filled.
286
+ */
287
+ constructor(message, code, funded) {
280
288
  super(message);
281
289
  this.code = code;
290
+ this.funded = funded;
282
291
  this.name = "AiCreditError";
283
292
  }
284
293
  code;
294
+ funded;
285
295
  };
286
296
  function positive(value) {
287
297
  return typeof value === "number" && Number.isFinite(value) && value > 0;
@@ -292,7 +302,12 @@ function validRate(rate) {
292
302
  function validLongContext(rates) {
293
303
  const tier = rates.longContext;
294
304
  if (tier === void 0) return true;
295
- return typeof tier === "object" && tier !== null && Number.isSafeInteger(tier.thresholdTokens) && tier.thresholdTokens > 0 && tier.thresholdTokens < rates.maxInputTokens && [tier.inputPerMTok, tier.outputPerMTok, tier.cacheReadPerMTok, tier.cacheWritePerMTok].every(validRate) && positive(tier.inputPerMTok) && positive(tier.outputPerMTok);
305
+ return typeof tier === "object" && tier !== null && Number.isSafeInteger(tier.thresholdTokens) && tier.thresholdTokens > 0 && tier.thresholdTokens < rates.maxInputTokens && [
306
+ tier.inputPerMTok,
307
+ tier.outputPerMTok,
308
+ tier.cacheReadPerMTok,
309
+ tier.cacheWritePerMTok
310
+ ].every(validRate) && positive(tier.inputPerMTok) && positive(tier.outputPerMTok);
296
311
  }
297
312
  function aiRatesForPrompt(rates, promptTokens) {
298
313
  const tier = rates.longContext;
@@ -303,43 +318,78 @@ function readAiCreditPricing(raw, modelId, now = Date.now()) {
303
318
  try {
304
319
  value = JSON.parse(raw ?? "");
305
320
  } catch {
306
- throw new AiCreditError("AI credit pricing is not configured.", "AI_PRICING_UNAVAILABLE");
321
+ throw new AiCreditError(
322
+ "AI credit pricing is not configured.",
323
+ "AI_PRICING_UNAVAILABLE"
324
+ );
307
325
  }
308
326
  const rates = value?.models?.[modelId];
309
327
  const validDate = (date) => typeof date === "string" && Number.isFinite(Date.parse(date));
310
- if (!value || !validDate(value.priceAsOf) || !rates || ![rates.inputPerMTok, rates.outputPerMTok, rates.cacheReadPerMTok, rates.cacheWritePerMTok].every(validRate) || !positive(rates.inputPerMTok) || !positive(rates.outputPerMTok) || !Number.isSafeInteger(rates.maxInputTokens) || rates.maxInputTokens <= 0 || !validLongContext(rates)) {
311
- throw new AiCreditError("AI credit pricing is not configured for this model.", "AI_PRICING_UNAVAILABLE");
328
+ if (!value || !validDate(value.priceAsOf) || !rates || ![
329
+ rates.inputPerMTok,
330
+ rates.outputPerMTok,
331
+ rates.cacheReadPerMTok,
332
+ rates.cacheWritePerMTok
333
+ ].every(validRate) || !positive(rates.inputPerMTok) || !positive(rates.outputPerMTok) || !Number.isSafeInteger(rates.maxInputTokens) || rates.maxInputTokens <= 0 || !validLongContext(rates)) {
334
+ throw new AiCreditError(
335
+ "AI credit pricing is not configured for this model.",
336
+ "AI_PRICING_UNAVAILABLE"
337
+ );
312
338
  }
313
339
  const age = now - Date.parse(value.priceAsOf);
314
340
  if (age > AI_PRICE_MAX_AGE_DAYS * DAY_MS || age < -DAY_MS) {
315
- throw new AiCreditError("AI credit pricing is out of date.", "AI_PRICING_STALE");
341
+ throw new AiCreditError(
342
+ "AI credit pricing is out of date.",
343
+ "AI_PRICING_STALE"
344
+ );
316
345
  }
317
346
  return value;
318
347
  }
319
348
  function aiSupplierCost(usage, pricing, modelId) {
320
- const counts = [usage.inputTokens, usage.outputTokens, usage.cacheReadTokens, usage.cacheWriteTokens];
349
+ const counts = [
350
+ usage.inputTokens,
351
+ usage.outputTokens,
352
+ usage.cacheReadTokens,
353
+ usage.cacheWriteTokens
354
+ ];
321
355
  if (!counts.every((count) => Number.isSafeInteger(count) && count >= 0)) {
322
356
  throw new AiCreditError("Supplier usage is invalid.", "AI_USAGE_INVALID");
323
357
  }
324
358
  const model = pricing.models[modelId];
325
- if (!model) throw new AiCreditError("Unpriced model.", "AI_PRICING_UNAVAILABLE");
326
- const rates = aiRatesForPrompt(model, usage.inputTokens + usage.cacheReadTokens + usage.cacheWriteTokens);
359
+ if (!model)
360
+ throw new AiCreditError("Unpriced model.", "AI_PRICING_UNAVAILABLE");
361
+ const rates = aiRatesForPrompt(
362
+ model,
363
+ usage.inputTokens + usage.cacheReadTokens + usage.cacheWriteTokens
364
+ );
327
365
  const usd = (usage.inputTokens * rates.inputPerMTok + usage.outputTokens * rates.outputPerMTok + usage.cacheReadTokens * rates.cacheReadPerMTok + usage.cacheWriteTokens * rates.cacheWritePerMTok) / 1e6;
328
366
  const nanos2 = Math.round(usd * NANO_USD_PER_USD);
329
367
  if (!Number.isSafeInteger(nanos2 * AI_COST_MULTIPLIER) || nanos2 < 0) {
330
- throw new AiCreditError("Supplier cost is outside the supported range.", "AI_USAGE_INVALID");
368
+ throw new AiCreditError(
369
+ "Supplier cost is outside the supported range.",
370
+ "AI_USAGE_INVALID"
371
+ );
331
372
  }
332
373
  return { supplier_cost_nano_usd: nanos2, supplier_cost_usd: usd };
333
374
  }
334
375
  async function aiWalletRequest(env, orgId, suffix = "", body2) {
335
376
  if (!orgId || !env.CARRIER_INTERNAL_API_KEY || !env.ATLAS_BASE_URL) {
336
- throw new AiCreditError("AI credit wallet is not configured.", "AI_WALLET_UNAVAILABLE");
377
+ throw new AiCreditError(
378
+ "AI credit wallet is not configured.",
379
+ "AI_WALLET_UNAVAILABLE"
380
+ );
337
381
  }
338
382
  let base;
339
383
  try {
340
- base = resolveInternalAtlasBase({ ATLAS_BASE_URL: env.ATLAS_BASE_URL, CARRIER_ENVIRONMENT: env.CARRIER_ENVIRONMENT });
384
+ base = resolveInternalAtlasBase({
385
+ ATLAS_BASE_URL: env.ATLAS_BASE_URL,
386
+ CARRIER_ENVIRONMENT: env.CARRIER_ENVIRONMENT
387
+ });
341
388
  } catch {
342
- throw new AiCreditError("AI credit wallet is not configured.", "AI_WALLET_UNAVAILABLE");
389
+ throw new AiCreditError(
390
+ "AI credit wallet is not configured.",
391
+ "AI_WALLET_UNAVAILABLE"
392
+ );
343
393
  }
344
394
  const response = await internalAtlasFetch(
345
395
  `${base}/internal/ai-wallet/${encodeURIComponent(orgId)}${suffix}`,
@@ -355,32 +405,92 @@ async function aiWalletRequest(env, orgId, suffix = "", body2) {
355
405
  );
356
406
  if (!response.ok) {
357
407
  if (response.status === 402) {
358
- throw new AiCreditError("Insufficient AI credits for this request.", "AI_CREDITS_EXHAUSTED");
408
+ const body3 = await response.json().catch(() => null);
409
+ throw new AiCreditError(
410
+ "Insufficient AI credits for this request.",
411
+ "AI_CREDITS_EXHAUSTED",
412
+ typeof body3?.funded === "boolean" ? body3.funded : void 0
413
+ );
359
414
  }
360
- throw new AiCreditError("AI credit wallet is unavailable.", "AI_WALLET_UNAVAILABLE");
415
+ throw new AiCreditError(
416
+ "AI credit wallet is unavailable.",
417
+ "AI_WALLET_UNAVAILABLE"
418
+ );
361
419
  }
362
420
  return response.json();
363
421
  }
422
+ function unbilledSettlement(ref) {
423
+ return {
424
+ ref,
425
+ unbilled: true,
426
+ async settle() {
427
+ },
428
+ async cancel() {
429
+ },
430
+ async pending() {
431
+ }
432
+ };
433
+ }
364
434
  function createAiCreditBilling(env) {
365
435
  if (env.CARRIER_AI_CREDITS_ENABLED !== "true") return void 0;
366
436
  return {
367
- async begin(modelId, attribution, maxOutputTokens) {
437
+ async begin(modelId, attribution, maxOutputTokens, promptTokenBound2) {
368
438
  const orgId = attribution?.orgId;
369
- if (!orgId) throw new AiCreditError("Select an organization to use AI credits.", "AI_ORG_REQUIRED");
439
+ if (!orgId) {
440
+ if (attribution?.unmetered !== "personal" || env.CARRIER_AI_PERSONAL_UNMETERED !== "true") {
441
+ throw new AiCreditError(
442
+ AI_ORG_REQUIRED_MESSAGE,
443
+ "AI_ORG_REQUIRED"
444
+ );
445
+ }
446
+ if (!Number.isSafeInteger(maxOutputTokens) || maxOutputTokens <= 0) {
447
+ throw new AiCreditError("Invalid output budget.", "AI_USAGE_INVALID");
448
+ }
449
+ const ref2 = `ai:${crypto.randomUUID()}`;
450
+ console.warn(
451
+ JSON.stringify({
452
+ event: "ai_credit_unbilled",
453
+ org_id: null,
454
+ model_id: modelId,
455
+ surface: attribution?.surface ?? "unknown",
456
+ ref: ref2
457
+ })
458
+ );
459
+ return unbilledSettlement(ref2);
460
+ }
370
461
  const pricing = readAiCreditPricing(env.AI_CREDIT_PRICING_JSON, modelId);
371
462
  if (!Number.isSafeInteger(maxOutputTokens) || maxOutputTokens <= 0) {
372
463
  throw new AiCreditError("Invalid output budget.", "AI_USAGE_INVALID");
373
464
  }
374
465
  const ref = `ai:${crypto.randomUUID()}`;
375
466
  const rates = pricing.models[modelId];
467
+ if (promptTokenBound2 !== void 0 && (!Number.isSafeInteger(promptTokenBound2) || promptTokenBound2 < 0)) {
468
+ throw new AiCreditError("Invalid prompt budget.", "AI_USAGE_INVALID");
469
+ }
470
+ const maxPrompt = Math.min(
471
+ rates.maxInputTokens,
472
+ promptTokenBound2 ?? rates.maxInputTokens
473
+ );
474
+ const threshold = rates.longContext?.thresholdTokens ?? rates.maxInputTokens;
376
475
  const tiers = [
377
- { prompt: Math.min(rates.maxInputTokens, rates.longContext?.thresholdTokens ?? rates.maxInputTokens), rates },
378
- ...rates.longContext ? [{ prompt: rates.maxInputTokens, rates: rates.longContext }] : []
476
+ { prompt: Math.min(maxPrompt, threshold), rates },
477
+ ...rates.longContext && maxPrompt > threshold ? [{ prompt: maxPrompt, rates: rates.longContext }] : []
379
478
  ];
380
- const maxUsd = Math.max(...tiers.map(({ prompt, rates: tier }) => (prompt * Math.max(tier.inputPerMTok, tier.cacheReadPerMTok, tier.cacheWritePerMTok) + maxOutputTokens * tier.outputPerMTok) / 1e6));
479
+ const maxUsd = Math.max(
480
+ ...tiers.map(
481
+ ({ prompt, rates: tier }) => (prompt * Math.max(
482
+ tier.inputPerMTok,
483
+ tier.cacheReadPerMTok,
484
+ tier.cacheWritePerMTok
485
+ ) + maxOutputTokens * tier.outputPerMTok) / 1e6
486
+ )
487
+ );
381
488
  const maxCost = Math.ceil(maxUsd * NANO_USD_PER_USD) * AI_COST_MULTIPLIER;
382
489
  if (!Number.isSafeInteger(maxCost) || maxCost <= 0) {
383
- throw new AiCreditError("Invalid reservation budget.", "AI_PRICING_UNAVAILABLE");
490
+ throw new AiCreditError(
491
+ "Invalid reservation budget.",
492
+ "AI_PRICING_UNAVAILABLE"
493
+ );
384
494
  }
385
495
  const reservation = {
386
496
  ref,
@@ -397,9 +507,25 @@ function createAiCreditBilling(env) {
397
507
  await aiWalletRequest(env, orgId, "/reserve", reservation);
398
508
  break;
399
509
  } catch (error) {
400
- if (error instanceof AiCreditError && error.code === "AI_CREDITS_EXHAUSTED") throw error;
510
+ if (error instanceof AiCreditError && error.code === "AI_CREDITS_EXHAUSTED") {
511
+ if (error.funded === false && env.CARRIER_AI_UNFUNDED_FALLBACK === "true") {
512
+ console.warn(
513
+ JSON.stringify({
514
+ event: "ai_credit_unbilled",
515
+ org_id: orgId,
516
+ model_id: modelId,
517
+ surface: attribution?.surface ?? "unknown",
518
+ ref
519
+ })
520
+ );
521
+ return unbilledSettlement(ref);
522
+ }
523
+ throw error;
524
+ }
401
525
  if (attempt >= 2) {
402
- await aiWalletRequest(env, orgId, "/cancel", { ref }).catch(() => void 0);
526
+ await aiWalletRequest(env, orgId, "/cancel", { ref }).catch(
527
+ () => void 0
528
+ );
403
529
  throw error;
404
530
  }
405
531
  }
@@ -415,8 +541,16 @@ function createAiCreditBilling(env) {
415
541
  },
416
542
  async settle(usage) {
417
543
  if (settled) return;
418
- if (![usage.inputTokens, usage.outputTokens, usage.cacheReadTokens, usage.cacheWriteTokens].some((count) => count > 0)) {
419
- throw new AiCreditError("Supplier did not report token usage.", "AI_USAGE_UNAVAILABLE");
544
+ if (![
545
+ usage.inputTokens,
546
+ usage.outputTokens,
547
+ usage.cacheReadTokens,
548
+ usage.cacheWriteTokens
549
+ ].some((count) => count > 0)) {
550
+ throw new AiCreditError(
551
+ "Supplier did not report token usage.",
552
+ "AI_USAGE_UNAVAILABLE"
553
+ );
420
554
  }
421
555
  const cost = aiSupplierCost(usage, pricing, modelId);
422
556
  const body2 = {
@@ -634,6 +768,9 @@ function headersFor(attribution, accept, billingRef) {
634
768
  if (meta) headers[REQUEST_METADATA_HEADER] = JSON.stringify(meta);
635
769
  return headers;
636
770
  }
771
+ function billedOnly(settlement) {
772
+ return settlement?.unbilled ? void 0 : settlement;
773
+ }
637
774
  async function releaseQuietly(settlement) {
638
775
  await settlement?.cancel().catch(() => void 0);
639
776
  }
@@ -707,6 +844,34 @@ async function sendWithRetry(aws, url, init, signal, deadlineAt, allowAmbiguousR
707
844
  if (throttled) return throttled;
708
845
  throw timedOut();
709
846
  }
847
+ var TOOL_PROMPT_SLACK = 8192;
848
+ function boundedContent(content) {
849
+ if (typeof content === "string") return true;
850
+ if (!Array.isArray(content)) return false;
851
+ return content.every((block) => {
852
+ if (typeof block !== "object" || block === null) return false;
853
+ const { type, content: inner } = block;
854
+ if (type === "text" || type === "tool_use") return true;
855
+ return type === "tool_result" && boundedContent(inner);
856
+ });
857
+ }
858
+ function promptTokenBound(requestBody) {
859
+ let parsed;
860
+ try {
861
+ parsed = JSON.parse(requestBody);
862
+ } catch {
863
+ return void 0;
864
+ }
865
+ if (typeof parsed !== "object" || parsed === null) return void 0;
866
+ const { messages, system, tools } = parsed;
867
+ if (!Array.isArray(messages)) return void 0;
868
+ const messagesOk = messages.every(
869
+ (m) => typeof m === "object" && m !== null && boundedContent(m.content)
870
+ );
871
+ if (!messagesOk || system !== void 0 && !boundedContent(system)) return void 0;
872
+ const slack = 1024 + (Array.isArray(tools) && tools.length > 0 ? TOOL_PROMPT_SLACK : 0);
873
+ return new TextEncoder().encode(requestBody).length + slack;
874
+ }
710
875
  async function invokeTool(creds, req, signal) {
711
876
  const json = await invokeModelRaw(
712
877
  creds,
@@ -723,7 +888,15 @@ async function invokeModelRaw(creds, payload, opts = {}, signal) {
723
888
  if (signal?.aborted) throw signal.reason ?? timedOut();
724
889
  const timeoutMs = opts.timeoutMs ?? BEDROCK_TIMEOUT_MS;
725
890
  const deadlineAt = Date.now() + timeoutMs;
726
- const settlement = await creds.billing?.begin(modelId, opts.attribution, Number(payload.max_tokens));
891
+ const requestBody = JSON.stringify(payload);
892
+ const settlement = billedOnly(
893
+ await creds.billing?.begin(
894
+ modelId,
895
+ opts.attribution,
896
+ Number(payload.max_tokens),
897
+ promptTokenBound(requestBody)
898
+ )
899
+ );
727
900
  if (signal?.aborted || Date.now() >= deadlineAt) {
728
901
  await releaseQuietly(settlement);
729
902
  throw signal?.reason ?? timedOut();
@@ -740,7 +913,7 @@ async function invokeModelRaw(creds, payload, opts = {}, signal) {
740
913
  {
741
914
  method: "POST",
742
915
  headers: headersFor(opts.attribution, "application/json", settlement?.ref),
743
- body: JSON.stringify(payload),
916
+ body: requestBody,
744
917
  signal: controller.signal
745
918
  },
746
919
  controller.signal,
@@ -11659,7 +11832,7 @@ function registerAllCarrierAskTools(server2, ctx) {
11659
11832
  try {
11660
11833
  route = await _routeIntent(intent, context, ctx.env, {
11661
11834
  attribution: {
11662
- ...ctx.props.org_id ? { orgId: ctx.props.org_id } : {},
11835
+ ...ctx.props.org_id ? { orgId: ctx.props.org_id } : { unmetered: "personal" },
11663
11836
  ...ctx.props.reseller_id ? { subAccountId: String(ctx.props.reseller_id) } : {},
11664
11837
  surface: "carrier_ask",
11665
11838
  tier: ctx.props.tier
@@ -14155,6 +14328,8 @@ if (!token && !process.env.CARRIER_API_TOKEN) {
14155
14328
  var stdioEnv = {
14156
14329
  CARRIER_API_TOKEN: process.env.CARRIER_API_TOKEN,
14157
14330
  CARRIER_AI_CREDITS_ENABLED: process.env.CARRIER_AI_CREDITS_ENABLED,
14331
+ CARRIER_AI_UNFUNDED_FALLBACK: process.env.CARRIER_AI_UNFUNDED_FALLBACK,
14332
+ CARRIER_AI_PERSONAL_UNMETERED: process.env.CARRIER_AI_PERSONAL_UNMETERED,
14158
14333
  CARRIER_AI_AUTO_TOPUP_ENABLED: process.env.CARRIER_AI_AUTO_TOPUP_ENABLED,
14159
14334
  AI_CREDIT_PRICING_JSON: process.env.AI_CREDIT_PRICING_JSON,
14160
14335
  CARRIER_API_BASE_URL: process.env.CARRIER_API_BASE_URL ?? "https://api.carrier.llc",