@blockrun/franklin 3.35.1 → 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.
- package/dist/agent/optimize.d.ts +8 -1
- package/dist/agent/optimize.js +17 -2
- package/dist/agent/tokens.js +15 -0
- package/dist/gateway-models.d.ts +22 -0
- package/dist/gateway-models.js +30 -0
- package/dist/proxy/server.js +6 -6
- package/package.json +1 -1
package/dist/agent/optimize.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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.
|
package/dist/agent/optimize.js
CHANGED
|
@@ -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;
|
|
@@ -62,9 +63,23 @@ const MODEL_MAX_OUTPUT = {
|
|
|
62
63
|
// 16K default and truncates mid-answer, burning USDC on continuation calls.
|
|
63
64
|
'qwen/qwen3.7-max': 65_536,
|
|
64
65
|
};
|
|
65
|
-
/**
|
|
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
|
+
*/
|
|
66
74
|
export function getMaxOutputTokens(model) {
|
|
67
|
-
|
|
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;
|
|
68
83
|
}
|
|
69
84
|
/** Idle gap (minutes) after which old tool results are cleared.
|
|
70
85
|
* Set to 30 min — a coffee break shouldn't lose tool context.
|
package/dist/agent/tokens.js
CHANGED
|
@@ -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.
|
|
@@ -274,8 +275,22 @@ const MODEL_CONTEXT_WINDOWS = {
|
|
|
274
275
|
* Get the context window size for a model, with a conservative default.
|
|
275
276
|
*/
|
|
276
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.
|
|
277
283
|
if (MODEL_CONTEXT_WINDOWS[model])
|
|
278
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();
|
|
279
294
|
// Pattern-based inference for unknown models
|
|
280
295
|
if (model.includes('gemini'))
|
|
281
296
|
return 1_000_000;
|
package/dist/gateway-models.d.ts
CHANGED
|
@@ -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
|
package/dist/gateway-models.js
CHANGED
|
@@ -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/proxy/server.js
CHANGED
|
@@ -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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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;
|
package/package.json
CHANGED