@blockrun/franklin 3.35.0 → 3.35.2

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.
@@ -534,9 +534,12 @@ export function getModelGuidance(model) {
534
534
  - Keep responses under 300 words. Be direct, not verbose.
535
535
  - Before responding: does every URL and fact come from a tool result? If not, remove it.`;
536
536
  }
537
- // Medium models: balanced guidance
537
+ // Medium models: balanced guidance. The bare `qwen` match dates from when
538
+ // every qwen id on the gateway was a free NVIDIA SKU — qwen3.7-max is a paid
539
+ // 1M-context flagship and belongs in the strong branch below.
538
540
  if (m.includes('kimi') || m.includes('grok') || m.includes('flash') ||
539
- m.includes('haiku') || m.includes('deepseek') || m.includes('qwen')) {
541
+ m.includes('haiku') || m.includes('deepseek') ||
542
+ (m.includes('qwen') && !m.includes('qwen3.7-max'))) {
540
543
  return `# Execution Guidance
541
544
  - Use tools to verify facts before stating them. Do not answer from memory when a tool can confirm.
542
545
  - Batch independent tool calls in one response (parallel execution).
@@ -546,7 +549,8 @@ export function getModelGuidance(model) {
546
549
  // Strong models: quality standards + thinking guidance
547
550
  if (m.includes('claude') || m.includes('gpt-5') || m.includes('opus') ||
548
551
  m.includes('sonnet') || m.includes('gemini-2.5-pro') || m.includes('gemini-3') ||
549
- m.includes('o3') || m.includes('o1') || m.includes('codex')) {
552
+ m.includes('o3') || m.includes('o1') || m.includes('codex') ||
553
+ m.includes('qwen3.7-max')) {
550
554
  return `# Quality Standards (strong model)
551
555
  - Keep calling tools until the task is complete AND the result is verified. Don't stop at "this should work" — prove it works.
552
556
  - Before finalizing: check correctness, grounding in tool output, and formatting.
package/dist/agent/llm.js CHANGED
@@ -19,6 +19,10 @@ const MODELS_WITHOUT_TOOL_CHOICE_SUBSTR = [
19
19
  'deepseek-reasoner',
20
20
  'openai/o1',
21
21
  'openai/o3',
22
+ // Qwen3.7 Max — plain tool use works, but any `tool_choice` on the payload
23
+ // 400s ("Invalid request: 400 Provider returned error"). Caught 2026-07-20
24
+ // by the grounding-retry path, which is the only place we force tool use.
25
+ 'qwen3.7-max',
22
26
  ];
23
27
  function modelDoesNotSupportToolChoice(model) {
24
28
  if (!model)
@@ -678,10 +682,19 @@ export class ModelClient {
678
682
  // know that. Catch the error, drop tool_choice, re-fire once.
679
683
  // No payment re-sign needed — original 402 already settled, and
680
684
  // the gateway treats this as the same logical request.
685
+ // Second shape, verified 2026-07-20 on qwen/qwen3.7-max: the gateway
686
+ // returns an OPAQUE `400 Invalid request: 400 Provider returned error`
687
+ // that never names tool_choice, so the precise match below can't fire
688
+ // and the user just sees "Tool schema rejected by this model". Since
689
+ // the request has already failed, dropping tool_choice and re-firing
690
+ // once costs nothing on the happy path and self-heals the whole class
691
+ // for models we haven't catalogued yet. If tool_choice wasn't the
692
+ // cause, the retry 400s again and the original error still surfaces.
681
693
  const lc = message.toLowerCase();
682
694
  const looksLikeToolChoiceReject = response.status === 400 &&
683
- lc.includes('tool_choice') &&
684
- (lc.includes('not support') || lc.includes('unsupported') || lc.includes('does not support'));
695
+ ((lc.includes('tool_choice') &&
696
+ (lc.includes('not support') || lc.includes('unsupported') || lc.includes('does not support'))) ||
697
+ requestPayload['tool_choice'] !== undefined);
685
698
  if (looksLikeToolChoiceReject && requestPayload['tool_choice'] !== undefined) {
686
699
  delete requestPayload['tool_choice'];
687
700
  const retryBody = JSON.stringify(requestPayload);
@@ -13,7 +13,14 @@ import type { Dialogue } from './types.js';
13
13
  export declare const CAPPED_MAX_TOKENS = 16384;
14
14
  /** Escalated max_tokens after hitting the cap */
15
15
  export declare const ESCALATED_MAX_TOKENS = 65536;
16
- /** Get max output tokens for a model */
16
+ /**
17
+ * Get max output tokens for a model.
18
+ *
19
+ * The static table above wins. The gateway catalog is consulted only for
20
+ * models with no entry — its own max_output values are demonstrably wrong for
21
+ * models we do have knowledge of (it reports 8192 for claude-haiku-4.5, which
22
+ * Anthropic documents as 64000), so it is a gap-filler, not a source of truth.
23
+ */
17
24
  export declare function getMaxOutputTokens(model: string): number;
18
25
  /**
19
26
  * Cap tool result sizes to prevent context bloat.
@@ -8,6 +8,7 @@
8
8
  * 4. Adaptive max_tokens — start low (8K), escalate on hit
9
9
  * 5. Pre-compact stripping — remove images/docs before summarization
10
10
  */
11
+ import { peekGatewayModel, warmGatewayModelsCache } from '../gateway-models.js';
11
12
  // ─── Constants ─────────────────────────────────────────────────────────────
12
13
  /** Max chars per individual tool result before truncation (history-level safety net) */
13
14
  const MAX_TOOL_RESULT_CHARS = 32_000;
@@ -57,10 +58,28 @@ const MODEL_MAX_OUTPUT = {
57
58
  'moonshot/kimi-k3': 65_536,
58
59
  'moonshot/kimi-k2.7': 65_536,
59
60
  'moonshot/kimi-k2.6': 65_536,
61
+ // Qwen3.7 Max — gateway model entry reports max_output: 65536. Same 4×
62
+ // headroom the K3 entry above recovers; without it the paid model gets the
63
+ // 16K default and truncates mid-answer, burning USDC on continuation calls.
64
+ 'qwen/qwen3.7-max': 65_536,
60
65
  };
61
- /** Get max output tokens for a model */
66
+ /**
67
+ * Get max output tokens for a model.
68
+ *
69
+ * The static table above wins. The gateway catalog is consulted only for
70
+ * models with no entry — its own max_output values are demonstrably wrong for
71
+ * models we do have knowledge of (it reports 8192 for claude-haiku-4.5, which
72
+ * Anthropic documents as 64000), so it is a gap-filler, not a source of truth.
73
+ */
62
74
  export function getMaxOutputTokens(model) {
63
- return MODEL_MAX_OUTPUT[model] ?? 16_384;
75
+ const known = MODEL_MAX_OUTPUT[model];
76
+ if (known)
77
+ return known;
78
+ const live = peekGatewayModel(model)?.max_output;
79
+ if (live && live > 0)
80
+ return live;
81
+ warmGatewayModelsCache();
82
+ return 16_384;
64
83
  }
65
84
  /** Idle gap (minutes) after which old tool results are cleared.
66
85
  * Set to 30 min — a coffee break shouldn't lose tool context.
@@ -3,6 +3,7 @@
3
3
  * Uses byte-based heuristic (no external tokenizer dependency).
4
4
  * Anchors to actual API counts when available, estimates on top for new messages.
5
5
  */
6
+ import { peekGatewayModel, warmGatewayModelsCache } from '../gateway-models.js';
6
7
  const DEFAULT_BYTES_PER_TOKEN = 4;
7
8
  /**
8
9
  * Model-specific bytes-per-token ratios for more accurate estimation.
@@ -266,13 +267,30 @@ const MODEL_CONTEXT_WINDOWS = {
266
267
  'nvidia/nemotron-nano-12b-v2-vl': 131_072,
267
268
  'nvidia/llama-4-maverick': 131_072,
268
269
  'nvidia/mistral-large-3-675b': 131_072,
270
+ // Qwen (paid) — Max tier is 1M ctx; the generic `qwen` fallback below is
271
+ // 128k and would compact ~8x too early.
272
+ 'qwen/qwen3.7-max': 1_000_000,
269
273
  };
270
274
  /**
271
275
  * Get the context window size for a model, with a conservative default.
272
276
  */
273
277
  export function getContextWindow(model) {
278
+ // The static table wins. It is not merely a cache of the gateway catalog —
279
+ // several entries are deliberate DOWNGRADES from what the gateway advertises
280
+ // (see the Anthropic block above: the gateway reports 1M, but its 1M beta
281
+ // header is not enabled, so anything over 200k 413s). Letting the catalog
282
+ // override these would reintroduce the exact bug those comments prevent.
274
283
  if (MODEL_CONTEXT_WINDOWS[model])
275
284
  return MODEL_CONTEXT_WINDOWS[model];
285
+ // No static entry — a live catalog value beats the blind default below.
286
+ // This is the qwen3.7-max class: a real model nobody has catalogued yet,
287
+ // which would otherwise silently compact at 128k.
288
+ const live = peekGatewayModel(model)?.context_window;
289
+ if (live && live > 0)
290
+ return live;
291
+ // Cache is cold. Kick a fetch (deduped in-flight, errors swallowed) so the
292
+ // next call in this session gets a real number instead of the blind default.
293
+ warmGatewayModelsCache();
276
294
  // Pattern-based inference for unknown models
277
295
  if (model.includes('gemini'))
278
296
  return 1_000_000;
@@ -56,6 +56,28 @@ export interface GatewayModel {
56
56
  }
57
57
  /** Test / reset helper. */
58
58
  export declare function clearGatewayModelsCache(): void;
59
+ /**
60
+ * Synchronous, cache-only lookup. Returns null when the catalog has never been
61
+ * fetched in this process — it never triggers a fetch, so it is safe to call
62
+ * from the hot path and from sync functions like getContextWindow().
63
+ *
64
+ * Deliberately ignores the TTL: a stale gateway record still beats no record
65
+ * at all, and the only callers are fallbacks for models we have no static
66
+ * entry for. Callers must treat the static tables as authoritative — the
67
+ * gateway's own metadata is not reliable (verified 2026-07-20: it reports
68
+ * max_output 8192 for claude-haiku-4.5, which Anthropic documents as 64000,
69
+ * and 64000 for claude-sonnet-4.6, documented as 128000). It is also not
70
+ * enforced: Franklin sends max_tokens 16384 to haiku today and the gateway
71
+ * accepts it. Treat this as "better than a blind default", nothing more.
72
+ */
73
+ export declare function peekGatewayModel(id: string): GatewayModel | null;
74
+ /** Test helper — seed the cache without a network call. */
75
+ export declare function __primeGatewayModelsCache(models: GatewayModel[]): void;
76
+ /**
77
+ * Fire-and-forget catalog warm. Populates the cache so the sync peek above has
78
+ * something to read. Errors are swallowed — every caller has a static fallback.
79
+ */
80
+ export declare function warmGatewayModelsCache(): void;
59
81
  /**
60
82
  * Fetch the model catalog, honoring the 5-minute cache. Concurrent callers
61
83
  * during a cold cache share a single in-flight promise so we don't stampede
@@ -24,6 +24,36 @@ export function clearGatewayModelsCache() {
24
24
  cache = null;
25
25
  inflight = null;
26
26
  }
27
+ /**
28
+ * Synchronous, cache-only lookup. Returns null when the catalog has never been
29
+ * fetched in this process — it never triggers a fetch, so it is safe to call
30
+ * from the hot path and from sync functions like getContextWindow().
31
+ *
32
+ * Deliberately ignores the TTL: a stale gateway record still beats no record
33
+ * at all, and the only callers are fallbacks for models we have no static
34
+ * entry for. Callers must treat the static tables as authoritative — the
35
+ * gateway's own metadata is not reliable (verified 2026-07-20: it reports
36
+ * max_output 8192 for claude-haiku-4.5, which Anthropic documents as 64000,
37
+ * and 64000 for claude-sonnet-4.6, documented as 128000). It is also not
38
+ * enforced: Franklin sends max_tokens 16384 to haiku today and the gateway
39
+ * accepts it. Treat this as "better than a blind default", nothing more.
40
+ */
41
+ export function peekGatewayModel(id) {
42
+ if (!cache)
43
+ return null;
44
+ return cache.models.find(m => m.id === id) ?? null;
45
+ }
46
+ /** Test helper — seed the cache without a network call. */
47
+ export function __primeGatewayModelsCache(models) {
48
+ cache = { models, expiresAt: Date.now() + CACHE_TTL_MS };
49
+ }
50
+ /**
51
+ * Fire-and-forget catalog warm. Populates the cache so the sync peek above has
52
+ * something to read. Errors are swallowed — every caller has a static fallback.
53
+ */
54
+ export function warmGatewayModelsCache() {
55
+ void getGatewayModels().catch(() => { });
56
+ }
27
57
  // ─── Fetch ──────────────────────────────────────────────────────────────
28
58
  async function doFetch() {
29
59
  const chain = loadChain();
package/dist/pricing.js CHANGED
@@ -105,6 +105,8 @@ export const MODEL_PRICING = {
105
105
  'minimax/minimax-m3': { input: 0.3, output: 1.2 },
106
106
  'minimax/minimax-m2.7': { input: 0.3, output: 1.2 },
107
107
  'minimax/minimax-m2.5': { input: 0.3, output: 1.2 },
108
+ // Qwen
109
+ 'qwen/qwen3.7-max': { input: 1.475, output: 4.425 },
108
110
  // Moonshot — K3 is the gateway flagship (2026-07): 2.8T open MoE, 1M
109
111
  // context, multimodal (image+text), returns reasoning_content. Pricier
110
112
  // than the K2.x line it replaced ($3/$15 vs K2.7's $0.95/$4).
@@ -9,6 +9,7 @@ import { appendAudit } from '../stats/audit.js';
9
9
  import { buildFallbackChain, DEFAULT_FALLBACK_CONFIG, ROUTING_PROFILES, } from './fallback.js';
10
10
  import { routeRequest, parseRoutingProfile, isVisionModel, messagesNeedVision, pickVisionSibling, } from '../router/index.js';
11
11
  import { estimateCost } from '../pricing.js';
12
+ import { getMaxOutputTokens } from '../agent/optimize.js';
12
13
  import { VERSION } from '../config.js';
13
14
  // User-Agent for backend requests
14
15
  const USER_AGENT = `franklin/${VERSION}`;
@@ -317,12 +318,11 @@ export function createProxy(options) {
317
318
  }
318
319
  {
319
320
  const original = parsed.max_tokens;
320
- const model = (parsed.model || '').toLowerCase();
321
- const modelCap = model.includes('deepseek') ||
322
- model.includes('haiku') ||
323
- model.includes('gpt-oss')
324
- ? 8192
325
- : 16384;
321
+ // Was a hardcoded substring ladder (deepseek|haiku|gpt-oss → 8192,
322
+ // everything else → 16384) that ignored MODEL_MAX_OUTPUT entirely,
323
+ // so proxy users got a 16K ceiling on models the CLI already knew
324
+ // could emit 65K+ (Kimi K3, GPT-5.6 Sol). Same table both paths now.
325
+ const modelCap = getMaxOutputTokens(parsed.model || '');
326
326
  // Use max of (last output × 2, default 4096) capped by model limit
327
327
  // This ensures short replies don't starve the next request
328
328
  const lastOut = lastOutputByModel.get(requestModel) ?? 0;
@@ -132,6 +132,11 @@ export const MODEL_SHORTCUTS = {
132
132
  minimax: 'minimax/minimax-m3',
133
133
  'm3': 'minimax/minimax-m3',
134
134
  'm2.7': 'minimax/minimax-m2.7',
135
+ // Qwen (paid). NOTE: bare `qwen` stays pinned to the FREE nvidia default
136
+ // above — never repoint a free alias at a paid model.
137
+ 'qwen-max': 'qwen/qwen3.7-max',
138
+ 'qwen3.7-max': 'qwen/qwen3.7-max',
139
+ 'qwen-3.7-max': 'qwen/qwen3.7-max',
135
140
  glm: 'zai/glm-5.2',
136
141
  'glm-5': 'zai/glm-5',
137
142
  'glm-5.2': 'zai/glm-5.2',
@@ -205,6 +210,7 @@ const PROVIDER_ORDER = [
205
210
  'zai',
206
211
  'moonshot',
207
212
  'minimax',
213
+ 'qwen',
208
214
  'deepseek',
209
215
  'nvidia',
210
216
  ];
@@ -216,6 +222,7 @@ const PROVIDER_LABELS = {
216
222
  zai: 'Z.AI / GLM',
217
223
  moonshot: 'Moonshot / Kimi',
218
224
  minimax: 'MiniMax',
225
+ qwen: 'Qwen / Alibaba',
219
226
  deepseek: 'DeepSeek',
220
227
  nvidia: 'Free / NVIDIA',
221
228
  };
@@ -254,6 +261,7 @@ export const PICKER_CATEGORIES = [
254
261
  { id: 'anthropic/claude-fable-5', shortcut: 'fable', label: 'Claude Fable 5', price: '$10/$50' },
255
262
  { id: 'anthropic/claude-opus-4.8', shortcut: 'opus', label: 'Claude Opus 4.8', price: '$5/$25', highlight: true },
256
263
  { id: 'anthropic/claude-sonnet-5', shortcut: 'sonnet', label: 'Claude Sonnet 5', price: '$3/$15' },
264
+ { id: 'qwen/qwen3.7-max', shortcut: 'qwen-max', label: 'Qwen3.7 Max', price: '$1.475/$4.425', highlight: true },
257
265
  { id: 'openai/gpt-5.6-sol', shortcut: 'gpt', label: 'GPT-5.6 Sol', price: '$5/$30', highlight: true },
258
266
  { id: 'google/gemini-3.1-pro', shortcut: 'gemini-3', label: 'Gemini 3.1 Pro', price: '$2/$12' },
259
267
  { id: 'google/gemini-2.5-pro', shortcut: 'gemini', label: 'Gemini 2.5 Pro', price: '$1.25/$10' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/franklin",
3
- "version": "3.35.0",
3
+ "version": "3.35.2",
4
4
  "description": "Franklin Agent — The AI agent with a wallet. Spends USDC autonomously to get real work done. Pay per action, no subscriptions.",
5
5
  "type": "module",
6
6
  "exports": {