@blockrun/clawrouter 0.9.27 → 0.9.28

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/cli.js CHANGED
@@ -511,6 +511,7 @@ function calibrateConfidence(distance, steepness) {
511
511
  }
512
512
 
513
513
  // src/router/selector.ts
514
+ var BASELINE_MODEL_ID = "anthropic/claude-opus-4-5";
514
515
  function selectModel(tier, confidence, method, reasoning, tierConfigs, modelPricing, estimatedInputTokens, maxOutputTokens, routingProfile) {
515
516
  const tierConfig = tierConfigs[tier];
516
517
  const model = tierConfig.primary;
@@ -520,7 +521,7 @@ function selectModel(tier, confidence, method, reasoning, tierConfigs, modelPric
520
521
  const inputCost = estimatedInputTokens / 1e6 * inputPrice;
521
522
  const outputCost = maxOutputTokens / 1e6 * outputPrice;
522
523
  const costEstimate = inputCost + outputCost;
523
- const opusPricing = modelPricing.get("anthropic/claude-opus-4.5");
524
+ const opusPricing = modelPricing.get(BASELINE_MODEL_ID);
524
525
  const opusInputPrice = opusPricing?.inputPrice ?? 0;
525
526
  const opusOutputPrice = opusPricing?.outputPrice ?? 0;
526
527
  const baselineInput = estimatedInputTokens / 1e6 * opusInputPrice;
@@ -549,7 +550,7 @@ function calculateModelCost(model, modelPricing, estimatedInputTokens, maxOutput
549
550
  const inputCost = estimatedInputTokens / 1e6 * inputPrice;
550
551
  const outputCost = maxOutputTokens / 1e6 * outputPrice;
551
552
  const costEstimate = inputCost + outputCost;
552
- const opusPricing = modelPricing.get("anthropic/claude-opus-4.5");
553
+ const opusPricing = modelPricing.get(BASELINE_MODEL_ID);
553
554
  const opusInputPrice = opusPricing?.inputPrice ?? 0;
554
555
  const opusOutputPrice = opusPricing?.outputPrice ?? 0;
555
556
  const baselineInput = estimatedInputTokens / 1e6 * opusInputPrice;
@@ -3789,6 +3790,83 @@ var PROVIDER_ERROR_PATTERNS = [
3789
3790
  /request.*size.*exceeds/i,
3790
3791
  /payload too large/i
3791
3792
  ];
3793
+ var DEGRADED_RESPONSE_PATTERNS = [
3794
+ /the ai service is temporarily overloaded/i,
3795
+ /service is temporarily overloaded/i,
3796
+ /please try again in a moment/i
3797
+ ];
3798
+ var DEGRADED_LOOP_PATTERNS = [
3799
+ /the boxed is the response\./i,
3800
+ /the response is the text\./i,
3801
+ /the final answer is the boxed\./i
3802
+ ];
3803
+ function extractAssistantContent(payload) {
3804
+ if (!payload || typeof payload !== "object") return void 0;
3805
+ const record = payload;
3806
+ const choices = record.choices;
3807
+ if (!Array.isArray(choices) || choices.length === 0) return void 0;
3808
+ const firstChoice = choices[0];
3809
+ if (!firstChoice || typeof firstChoice !== "object") return void 0;
3810
+ const choice = firstChoice;
3811
+ const message = choice.message;
3812
+ if (!message || typeof message !== "object") return void 0;
3813
+ const content = message.content;
3814
+ return typeof content === "string" ? content : void 0;
3815
+ }
3816
+ function hasKnownLoopSignature(text) {
3817
+ const matchCount = DEGRADED_LOOP_PATTERNS.reduce(
3818
+ (count, pattern) => pattern.test(text) ? count + 1 : count,
3819
+ 0
3820
+ );
3821
+ if (matchCount >= 2) return true;
3822
+ const lines = text.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
3823
+ if (lines.length < 8) return false;
3824
+ const counts = /* @__PURE__ */ new Map();
3825
+ for (const line of lines) {
3826
+ counts.set(line, (counts.get(line) ?? 0) + 1);
3827
+ }
3828
+ const maxRepeat = Math.max(...counts.values());
3829
+ const uniqueRatio = counts.size / lines.length;
3830
+ return maxRepeat >= 3 && uniqueRatio <= 0.45;
3831
+ }
3832
+ function detectDegradedSuccessResponse(body) {
3833
+ const trimmed = body.trim();
3834
+ if (!trimmed) return void 0;
3835
+ if (DEGRADED_RESPONSE_PATTERNS.some((pattern) => pattern.test(trimmed))) {
3836
+ return "degraded response: overloaded placeholder";
3837
+ }
3838
+ if (hasKnownLoopSignature(trimmed)) {
3839
+ return "degraded response: repetitive loop output";
3840
+ }
3841
+ try {
3842
+ const parsed = JSON.parse(trimmed);
3843
+ const errorField = parsed.error;
3844
+ let errorText = "";
3845
+ if (typeof errorField === "string") {
3846
+ errorText = errorField;
3847
+ } else if (errorField && typeof errorField === "object") {
3848
+ const errObj = errorField;
3849
+ errorText = [
3850
+ typeof errObj.message === "string" ? errObj.message : "",
3851
+ typeof errObj.type === "string" ? errObj.type : "",
3852
+ typeof errObj.code === "string" ? errObj.code : ""
3853
+ ].filter(Boolean).join(" ");
3854
+ }
3855
+ if (errorText && PROVIDER_ERROR_PATTERNS.some((pattern) => pattern.test(errorText))) {
3856
+ return `degraded response: ${errorText.slice(0, 120)}`;
3857
+ }
3858
+ const assistantContent = extractAssistantContent(parsed);
3859
+ if (!assistantContent) return void 0;
3860
+ if (DEGRADED_RESPONSE_PATTERNS.some((pattern) => pattern.test(assistantContent))) {
3861
+ return "degraded response: overloaded assistant content";
3862
+ }
3863
+ if (hasKnownLoopSignature(assistantContent)) {
3864
+ return "degraded response: repetitive assistant loop";
3865
+ }
3866
+ } catch {
3867
+ }
3868
+ return void 0;
3869
+ }
3792
3870
  var FALLBACK_STATUS_CODES = [
3793
3871
  400,
3794
3872
  // Bad request - sometimes used for billing errors
@@ -4328,6 +4406,22 @@ async function tryModelRequest(upstreamUrl, method, headers, body, modelId, maxT
4328
4406
  isProviderError: isProviderErr
4329
4407
  };
4330
4408
  }
4409
+ const contentType = response.headers.get("content-type") || "";
4410
+ if (contentType.includes("json") || contentType.includes("text")) {
4411
+ try {
4412
+ const responseBody = await response.clone().text();
4413
+ const degradedReason = detectDegradedSuccessResponse(responseBody);
4414
+ if (degradedReason) {
4415
+ return {
4416
+ success: false,
4417
+ errorBody: degradedReason,
4418
+ errorStatus: 503,
4419
+ isProviderError: true
4420
+ };
4421
+ }
4422
+ } catch {
4423
+ }
4424
+ }
4331
4425
  return { success: true, response };
4332
4426
  } catch (err) {
4333
4427
  const errorMsg = err instanceof Error ? err.message : String(err);