@carrierllc/mcp 0.13.67 → 0.13.68
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-
|
|
94
|
+
} from "./chunk-3DC4HQMU.js";
|
|
95
95
|
import "./chunk-SHKKVIIA.js";
|
|
96
96
|
|
|
97
97
|
// src/index.ts
|
|
@@ -209,6 +209,151 @@ async function assertManagedIccidAccess(baseUrl2, auth, iccid) {
|
|
|
209
209
|
|
|
210
210
|
// ../../packages/carrier-ai/dist/index.js
|
|
211
211
|
import { AwsClient } from "aws4fetch";
|
|
212
|
+
var NANO_EUR_PER_EUR = 1e9;
|
|
213
|
+
var AI_COST_MULTIPLIER = 3;
|
|
214
|
+
var AiCreditError = class extends Error {
|
|
215
|
+
constructor(message, code) {
|
|
216
|
+
super(message);
|
|
217
|
+
this.code = code;
|
|
218
|
+
this.name = "AiCreditError";
|
|
219
|
+
}
|
|
220
|
+
code;
|
|
221
|
+
};
|
|
222
|
+
function positive(value) {
|
|
223
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
224
|
+
}
|
|
225
|
+
function readAiCreditPricing(raw, modelId) {
|
|
226
|
+
let value;
|
|
227
|
+
try {
|
|
228
|
+
value = JSON.parse(raw ?? "");
|
|
229
|
+
} catch {
|
|
230
|
+
throw new AiCreditError("AI credit pricing is not configured.", "AI_PRICING_UNAVAILABLE");
|
|
231
|
+
}
|
|
232
|
+
const rates = value?.models?.[modelId];
|
|
233
|
+
const validDate = (date) => typeof date === "string" && Number.isFinite(Date.parse(date));
|
|
234
|
+
if (!positive(value?.eurPerUsd) || !validDate(value.fxAsOf) || !validDate(value.priceAsOf) || !rates || ![rates.inputPerMTok, rates.outputPerMTok, rates.cacheReadPerMTok, rates.cacheWritePerMTok].every((rate) => typeof rate === "number" && Number.isFinite(rate) && rate >= 0) || !positive(rates.inputPerMTok) || !positive(rates.outputPerMTok) || !Number.isSafeInteger(rates.maxInputTokens) || rates.maxInputTokens <= 0) {
|
|
235
|
+
throw new AiCreditError("AI credit pricing is not configured for this model.", "AI_PRICING_UNAVAILABLE");
|
|
236
|
+
}
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
function aiSupplierCost(usage, pricing, modelId) {
|
|
240
|
+
const counts = [usage.inputTokens, usage.outputTokens, usage.cacheReadTokens, usage.cacheWriteTokens];
|
|
241
|
+
if (!counts.every((count) => Number.isSafeInteger(count) && count >= 0)) {
|
|
242
|
+
throw new AiCreditError("Supplier usage is invalid.", "AI_USAGE_INVALID");
|
|
243
|
+
}
|
|
244
|
+
const rates = pricing.models[modelId];
|
|
245
|
+
if (!rates) throw new AiCreditError("Unpriced model.", "AI_PRICING_UNAVAILABLE");
|
|
246
|
+
const usd = (usage.inputTokens * rates.inputPerMTok + usage.outputTokens * rates.outputPerMTok + usage.cacheReadTokens * rates.cacheReadPerMTok + usage.cacheWriteTokens * rates.cacheWritePerMTok) / 1e6;
|
|
247
|
+
const nanos2 = Math.round(usd * pricing.eurPerUsd * NANO_EUR_PER_EUR);
|
|
248
|
+
if (!Number.isSafeInteger(nanos2 * AI_COST_MULTIPLIER) || nanos2 < 0) {
|
|
249
|
+
throw new AiCreditError("Supplier cost is outside the supported range.", "AI_USAGE_INVALID");
|
|
250
|
+
}
|
|
251
|
+
return { supplier_cost_nano_eur: nanos2, supplier_cost_usd: usd };
|
|
252
|
+
}
|
|
253
|
+
async function aiWalletRequest(env, orgId, suffix = "", body2) {
|
|
254
|
+
if (!orgId || !env.CARRIER_INTERNAL_API_KEY || !env.ATLAS_BASE_URL) {
|
|
255
|
+
throw new AiCreditError("AI credit wallet is not configured.", "AI_WALLET_UNAVAILABLE");
|
|
256
|
+
}
|
|
257
|
+
const response = await fetch(
|
|
258
|
+
`${env.ATLAS_BASE_URL.replace(/\/+$/, "")}/internal/ai-wallet/${encodeURIComponent(orgId)}${suffix}`,
|
|
259
|
+
{
|
|
260
|
+
method: body2 === void 0 ? "GET" : "POST",
|
|
261
|
+
headers: {
|
|
262
|
+
"X-Carrier-Internal-Key": env.CARRIER_INTERNAL_API_KEY,
|
|
263
|
+
"Content-Type": "application/json"
|
|
264
|
+
},
|
|
265
|
+
...body2 === void 0 ? {} : { body: JSON.stringify(body2) },
|
|
266
|
+
signal: AbortSignal.timeout(5e3)
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
if (!response.ok) {
|
|
270
|
+
if (response.status === 402) {
|
|
271
|
+
throw new AiCreditError("Insufficient AI credits for this request.", "AI_CREDITS_EXHAUSTED");
|
|
272
|
+
}
|
|
273
|
+
throw new AiCreditError("AI credit wallet is unavailable.", "AI_WALLET_UNAVAILABLE");
|
|
274
|
+
}
|
|
275
|
+
return response.json();
|
|
276
|
+
}
|
|
277
|
+
function createAiCreditBilling(env) {
|
|
278
|
+
if (env.CARRIER_AI_CREDITS_ENABLED !== "true") return void 0;
|
|
279
|
+
return {
|
|
280
|
+
async begin(modelId, attribution, maxOutputTokens) {
|
|
281
|
+
const orgId = attribution?.orgId;
|
|
282
|
+
if (!orgId) throw new AiCreditError("Select an organization to use AI credits.", "AI_ORG_REQUIRED");
|
|
283
|
+
const pricing = readAiCreditPricing(env.AI_CREDIT_PRICING_JSON, modelId);
|
|
284
|
+
if (!Number.isSafeInteger(maxOutputTokens) || maxOutputTokens <= 0) {
|
|
285
|
+
throw new AiCreditError("Invalid output budget.", "AI_USAGE_INVALID");
|
|
286
|
+
}
|
|
287
|
+
const ref = `ai:${crypto.randomUUID()}`;
|
|
288
|
+
const rates = pricing.models[modelId];
|
|
289
|
+
const maxCost = Math.ceil((rates.maxInputTokens * Math.max(rates.inputPerMTok, rates.cacheReadPerMTok, rates.cacheWritePerMTok) + maxOutputTokens * rates.outputPerMTok) / 1e6 * pricing.eurPerUsd * NANO_EUR_PER_EUR) * AI_COST_MULTIPLIER;
|
|
290
|
+
if (!Number.isSafeInteger(maxCost) || maxCost <= 0) {
|
|
291
|
+
throw new AiCreditError("Invalid reservation budget.", "AI_PRICING_UNAVAILABLE");
|
|
292
|
+
}
|
|
293
|
+
const reservation = {
|
|
294
|
+
ref,
|
|
295
|
+
model_id: modelId,
|
|
296
|
+
max_cost_nano_eur: maxCost,
|
|
297
|
+
cost_basis: {
|
|
298
|
+
rates,
|
|
299
|
+
eur_per_usd: pricing.eurPerUsd,
|
|
300
|
+
fx_as_of: pricing.fxAsOf,
|
|
301
|
+
price_as_of: pricing.priceAsOf,
|
|
302
|
+
surface: attribution?.surface ?? "unknown"
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
for (let attempt = 0; ; attempt++) {
|
|
306
|
+
try {
|
|
307
|
+
await aiWalletRequest(env, orgId, "/reserve", reservation);
|
|
308
|
+
break;
|
|
309
|
+
} catch (error) {
|
|
310
|
+
if (error instanceof AiCreditError && error.code === "AI_CREDITS_EXHAUSTED") throw error;
|
|
311
|
+
if (attempt >= 2) {
|
|
312
|
+
await aiWalletRequest(env, orgId, "/cancel", { ref }).catch(() => void 0);
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
let settled = false;
|
|
318
|
+
return {
|
|
319
|
+
ref,
|
|
320
|
+
async cancel() {
|
|
321
|
+
await aiWalletRequest(env, orgId, "/cancel", { ref });
|
|
322
|
+
},
|
|
323
|
+
async pending(usage) {
|
|
324
|
+
await aiWalletRequest(env, orgId, "/pending", { ref, usage });
|
|
325
|
+
},
|
|
326
|
+
async settle(usage) {
|
|
327
|
+
if (settled) return;
|
|
328
|
+
if (![usage.inputTokens, usage.outputTokens, usage.cacheReadTokens, usage.cacheWriteTokens].some((count) => count > 0)) {
|
|
329
|
+
throw new AiCreditError("Supplier did not report token usage.", "AI_USAGE_UNAVAILABLE");
|
|
330
|
+
}
|
|
331
|
+
const cost = aiSupplierCost(usage, pricing, modelId);
|
|
332
|
+
const body2 = {
|
|
333
|
+
ref,
|
|
334
|
+
...cost,
|
|
335
|
+
model_id: modelId,
|
|
336
|
+
surface: attribution?.surface ?? "unknown",
|
|
337
|
+
usage,
|
|
338
|
+
eur_per_usd: pricing.eurPerUsd,
|
|
339
|
+
fx_as_of: pricing.fxAsOf,
|
|
340
|
+
price_as_of: pricing.priceAsOf,
|
|
341
|
+
rates: pricing.models[modelId]
|
|
342
|
+
};
|
|
343
|
+
for (let attempt = 0; ; attempt++) {
|
|
344
|
+
try {
|
|
345
|
+
await aiWalletRequest(env, orgId, "/debit", body2);
|
|
346
|
+
break;
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (attempt >= 2) throw error;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
settled = true;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
212
357
|
var AI_TIER_POLICY = {
|
|
213
358
|
free: { limit: 5, window: "day" },
|
|
214
359
|
pro: { limit: 1e3, window: "month" },
|
|
@@ -380,15 +525,31 @@ function body(req) {
|
|
|
380
525
|
...req.tools ? { tools: req.tools, tool_choice: { type: "any" } } : {}
|
|
381
526
|
};
|
|
382
527
|
}
|
|
383
|
-
function headersFor(attribution, accept) {
|
|
528
|
+
function headersFor(attribution, accept, billingRef) {
|
|
384
529
|
const headers = {
|
|
385
530
|
"Content-Type": "application/json",
|
|
386
531
|
Accept: accept
|
|
387
532
|
};
|
|
388
533
|
const meta = attribution ? requestMetadata(attribution) : null;
|
|
534
|
+
if (meta && billingRef) meta.billing_ref = billingRef;
|
|
389
535
|
if (meta) headers[REQUEST_METADATA_HEADER] = JSON.stringify(meta);
|
|
390
536
|
return headers;
|
|
391
537
|
}
|
|
538
|
+
async function releaseQuietly(settlement) {
|
|
539
|
+
await settlement?.cancel().catch(() => void 0);
|
|
540
|
+
}
|
|
541
|
+
function usageHolder(payload) {
|
|
542
|
+
if (!payload || typeof payload !== "object") return void 0;
|
|
543
|
+
const root = payload;
|
|
544
|
+
const holder = root.usage ?? root.message?.usage;
|
|
545
|
+
return holder && typeof holder === "object" ? holder : void 0;
|
|
546
|
+
}
|
|
547
|
+
var validCounter = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
548
|
+
function hasRequiredCounters(holder, need) {
|
|
549
|
+
if (!holder) return false;
|
|
550
|
+
const optional = (value) => value === void 0 || validCounter(value);
|
|
551
|
+
return (!need.input || validCounter(holder.input_tokens)) && (!need.output || validCounter(holder.output_tokens)) && optional(holder.cache_read_input_tokens) && optional(holder.cache_creation_input_tokens);
|
|
552
|
+
}
|
|
392
553
|
function reportUsage(req, usage) {
|
|
393
554
|
if (!req.onUsage) return;
|
|
394
555
|
try {
|
|
@@ -426,7 +587,7 @@ function sleep(ms, signal) {
|
|
|
426
587
|
function retryable(status) {
|
|
427
588
|
return status === 429 || status >= 500;
|
|
428
589
|
}
|
|
429
|
-
async function sendWithRetry(aws, url, init, signal, deadlineAt) {
|
|
590
|
+
async function sendWithRetry(aws, url, init, signal, deadlineAt, allowAmbiguousRetries = true) {
|
|
430
591
|
let throttled = null;
|
|
431
592
|
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
432
593
|
if (signal.aborted) break;
|
|
@@ -434,10 +595,10 @@ async function sendWithRetry(aws, url, init, signal, deadlineAt) {
|
|
|
434
595
|
try {
|
|
435
596
|
resp = await aws.fetch(url, init);
|
|
436
597
|
} catch (err9) {
|
|
437
|
-
if (signal.aborted && throttled) return throttled;
|
|
598
|
+
if (signal.aborted && throttled && allowAmbiguousRetries) return throttled;
|
|
438
599
|
throw err9;
|
|
439
600
|
}
|
|
440
|
-
if (!retryable(resp.status)) return resp;
|
|
601
|
+
if (!retryable(resp.status) || !allowAmbiguousRetries && resp.status >= 500) return resp;
|
|
441
602
|
throttled = resp;
|
|
442
603
|
if (attempt === MAX_ATTEMPTS - 1) break;
|
|
443
604
|
const backoff = Math.random() * INITIAL_RETRY_MS * 2 ** attempt;
|
|
@@ -460,27 +621,47 @@ async function invokeTool(creds, req, signal) {
|
|
|
460
621
|
}
|
|
461
622
|
async function invokeModelRaw(creds, payload, opts = {}, signal) {
|
|
462
623
|
const { aws, region, modelId } = client(creds);
|
|
624
|
+
if (signal?.aborted) throw signal.reason ?? timedOut();
|
|
463
625
|
const timeoutMs = opts.timeoutMs ?? BEDROCK_TIMEOUT_MS;
|
|
464
626
|
const deadlineAt = Date.now() + timeoutMs;
|
|
627
|
+
const settlement = await creds.billing?.begin(modelId, opts.attribution, Number(payload.max_tokens));
|
|
628
|
+
if (signal?.aborted || Date.now() >= deadlineAt) {
|
|
629
|
+
await releaseQuietly(settlement);
|
|
630
|
+
throw signal?.reason ?? timedOut();
|
|
631
|
+
}
|
|
465
632
|
const controller = new AbortController();
|
|
466
|
-
const timer = setTimeout(() => controller.abort(),
|
|
633
|
+
const timer = setTimeout(() => controller.abort(), Math.max(0, deadlineAt - Date.now()));
|
|
467
634
|
const onAbort = () => controller.abort();
|
|
468
635
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
636
|
+
let usage = ZERO_USAGE;
|
|
469
637
|
try {
|
|
470
638
|
const resp = await sendWithRetry(
|
|
471
639
|
aws,
|
|
472
640
|
`https://bedrock-runtime.${region}.amazonaws.com/model/${encodeURIComponent(modelId)}/invoke`,
|
|
473
641
|
{
|
|
474
642
|
method: "POST",
|
|
475
|
-
headers: headersFor(opts.attribution, "application/json"),
|
|
643
|
+
headers: headersFor(opts.attribution, "application/json", settlement?.ref),
|
|
476
644
|
body: JSON.stringify(payload),
|
|
477
645
|
signal: controller.signal
|
|
478
646
|
},
|
|
479
647
|
controller.signal,
|
|
480
|
-
deadlineAt
|
|
648
|
+
deadlineAt,
|
|
649
|
+
!settlement
|
|
481
650
|
);
|
|
482
|
-
if (!resp.ok)
|
|
483
|
-
|
|
651
|
+
if (!resp.ok) {
|
|
652
|
+
if (resp.status < 500) await releaseQuietly(settlement);
|
|
653
|
+
await fail(resp);
|
|
654
|
+
}
|
|
655
|
+
const result3 = await resp.json();
|
|
656
|
+
usage = extractUsage(result3);
|
|
657
|
+
if (settlement && !hasRequiredCounters(usageHolder(result3), { input: true, output: true })) {
|
|
658
|
+
throw new AiCreditError("Supplier did not report complete token usage.", "AI_USAGE_UNAVAILABLE");
|
|
659
|
+
}
|
|
660
|
+
await settlement?.settle(usage);
|
|
661
|
+
return result3;
|
|
662
|
+
} catch (error) {
|
|
663
|
+
await settlement?.pending(usage);
|
|
664
|
+
throw error;
|
|
484
665
|
} finally {
|
|
485
666
|
clearTimeout(timer);
|
|
486
667
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -9196,6 +9377,27 @@ async function resolveUserTier2(env, sub) {
|
|
|
9196
9377
|
|
|
9197
9378
|
// src/pricing-tools.ts
|
|
9198
9379
|
var PRICING_TOOLS = [
|
|
9380
|
+
{
|
|
9381
|
+
name: "ai_credit_balance",
|
|
9382
|
+
description: "Read your organization's AI credit wallet in EUR: balance_eur and available_eur. Pro plan payments fund this wallet and AI usage spends it. No token counts are exposed.",
|
|
9383
|
+
scope: "read",
|
|
9384
|
+
inputSchema: { type: "object", properties: {}, required: [] },
|
|
9385
|
+
handler: async (env, props2) => {
|
|
9386
|
+
if (env.CARRIER_API_TOKEN) {
|
|
9387
|
+
const response = await fetch(`${env.CARRIER_API_BASE_URL ?? "https://api.carrier.llc"}/v1/ai/wallet`, {
|
|
9388
|
+
headers: { Authorization: `Bearer ${env.CARRIER_API_TOKEN}` },
|
|
9389
|
+
signal: AbortSignal.timeout(5e3)
|
|
9390
|
+
});
|
|
9391
|
+
if (!response.ok) throw new Error("AI credits unavailable. Sign in to Carrier and select an organization.");
|
|
9392
|
+
const { balance_eur: balance_eur2, available_eur: available_eur2 } = (await response.json()).data;
|
|
9393
|
+
return { currency: "EUR", balance_eur: balance_eur2, available_eur: available_eur2 };
|
|
9394
|
+
}
|
|
9395
|
+
if (env.CARRIER_AI_CREDITS_ENABLED !== "true") throw new Error("AI credits are not available on this deployment.");
|
|
9396
|
+
if (!props2.org_id) throw new Error("Select an organization to read AI credits.");
|
|
9397
|
+
const { balance_eur, available_eur } = await aiWalletRequest(env, props2.org_id);
|
|
9398
|
+
return { currency: "EUR", balance_eur, available_eur };
|
|
9399
|
+
}
|
|
9400
|
+
},
|
|
9199
9401
|
// -------------------------------------------------------------------------
|
|
9200
9402
|
// credit_balance — View current credit balance and usage
|
|
9201
9403
|
// -------------------------------------------------------------------------
|
|
@@ -10766,6 +10968,7 @@ Pre-resolved context: ${JSON.stringify(definedContext)}` : "";
|
|
|
10766
10968
|
toolCall = await invokeTool(
|
|
10767
10969
|
{
|
|
10768
10970
|
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
|
10971
|
+
...env.CARRIER_AI_CREDITS_ENABLED === "true" ? { billing: createAiCreditBilling(env) } : {},
|
|
10769
10972
|
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
|
|
10770
10973
|
...env.AWS_SESSION_TOKEN ? { sessionToken: env.AWS_SESSION_TOKEN } : {},
|
|
10771
10974
|
...env.AWS_REGION ? { region: env.AWS_REGION } : {},
|
|
@@ -13441,6 +13644,9 @@ if (!token && !process.env.CARRIER_API_TOKEN) {
|
|
|
13441
13644
|
process.exit(1);
|
|
13442
13645
|
}
|
|
13443
13646
|
var stdioEnv = {
|
|
13647
|
+
CARRIER_API_TOKEN: process.env.CARRIER_API_TOKEN,
|
|
13648
|
+
CARRIER_AI_CREDITS_ENABLED: process.env.CARRIER_AI_CREDITS_ENABLED,
|
|
13649
|
+
AI_CREDIT_PRICING_JSON: process.env.AI_CREDIT_PRICING_JSON,
|
|
13444
13650
|
CARRIER_API_BASE_URL: process.env.CARRIER_API_BASE_URL ?? "https://api.carrier.llc",
|
|
13445
13651
|
CARRIER_OCS_BASE_URL: baseUrl,
|
|
13446
13652
|
// Empty on purpose. rotate_credentials reads this and throws a clear
|