@blockrun/franklin 3.31.0 → 3.32.1

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.
@@ -12,7 +12,7 @@ import { execSync } from 'node:child_process';
12
12
  import { BLOCKRUN_DIR, VERSION } from '../config.js';
13
13
  import { estimateHistoryTokens, getAnchoredTokenCount, getContextWindow, resetTokenAnchor } from './tokens.js';
14
14
  import { forceCompact } from './compact.js';
15
- import { getStatsSummary } from '../stats/tracker.js';
15
+ import { getStatsSummary, recordUsage } from '../stats/tracker.js';
16
16
  import { resolveModel } from '../ui/model-picker.js';
17
17
  import { listSessions, loadSessionHistory, } from '../session/storage.js';
18
18
  import { matchSkill } from '../skills/invoke.js';
@@ -236,6 +236,7 @@ const DIRECT_COMMANDS = {
236
236
  ` **Session:** /plan /ultraplan /execute /compact /retry /sessions /resume /session-search /context /tasks /history /transcript\n` +
237
237
  ` **Power:** /ultrathink [query] /ultraplan /noplan /moa [query] /dump\n` +
238
238
  ` **Info:** /model /auto /wallet /cost /tokens /learnings /brain /mcp /doctor /version /bug /help\n` +
239
+ ` **Market:** /market [keyword] · /market info <slug> · /market run <slug> <input>\n` +
239
240
  ` **UI:** /clear /exit\n` +
240
241
  skillsBlock +
241
242
  (ultrathinkOn ? `\n Ultrathink: ON\n` : '')
@@ -635,6 +636,75 @@ export async function handleSlashCommand(input, ctx) {
635
636
  emitDone(ctx);
636
637
  return { handled: true };
637
638
  }
639
+ // /market [keyword | info <slug> | run <slug> <input>] — browse + hire paid
640
+ // skills from the BlockRun agent marketplace (business.blockrun.ai). Browsing
641
+ // and search are free GETs; `run` pays ONE standard x402 from the wallet.
642
+ if (input === '/market' || input.startsWith('/market ')) {
643
+ const arg = input.slice('/market'.length).trim();
644
+ const { fetchCatalog, runMarketSkill, formatCatalogList, formatSkillCard, fmtUsd } = await import('../market/client.js');
645
+ // run <slug> <input> — the only paid path.
646
+ if (/^run(\s|$)/.test(arg)) {
647
+ const runMatch = arg.match(/^run\s+(\S+)\s+([\s\S]+)$/);
648
+ if (!runMatch) {
649
+ ctx.onEvent({ kind: 'text_delta', text: 'Usage: /market run <slug> <input>\n' });
650
+ emitDone(ctx);
651
+ return { handled: true };
652
+ }
653
+ const [, slug, skillInput] = runMatch;
654
+ ctx.onEvent({ kind: 'text_delta', text: `Hiring ${slug}…\n` });
655
+ const outcome = await runMarketSkill(slug, skillInput);
656
+ // Record the on-chain spend so the human /market run path is visible to
657
+ // the audit ledger, /cost, and the --max-spend accumulator (parity with
658
+ // the agent_talent tool path).
659
+ try {
660
+ recordUsage(`market:run:${slug}`, 0, 0, outcome.paidUsd, 0);
661
+ }
662
+ catch { /* best-effort */ }
663
+ if (!outcome.ok) {
664
+ ctx.onEvent({ kind: 'text_delta', text: `Could not run ${slug}: ${outcome.error}. No charge.\n` });
665
+ }
666
+ else {
667
+ const tx = outcome.txHash ? ` . tx ${outcome.txHash.slice(0, 12)}…` : '';
668
+ ctx.onEvent({ kind: 'text_delta', text: `Paid ${fmtUsd(outcome.paidUsd)}${tx}\n\n${outcome.result ?? ''}\n` });
669
+ }
670
+ emitDone(ctx);
671
+ return { handled: true };
672
+ }
673
+ // info <slug> — detail card.
674
+ if (/^info(\s|$)/.test(arg)) {
675
+ const slug = arg.slice('info'.length).trim();
676
+ if (!slug) {
677
+ ctx.onEvent({ kind: 'text_delta', text: 'Usage: /market info <slug>\n' });
678
+ emitDone(ctx);
679
+ return { handled: true };
680
+ }
681
+ try {
682
+ const skills = await fetchCatalog({ limit: 200 });
683
+ const skill = skills.find((s) => s.slug === slug);
684
+ ctx.onEvent({ kind: 'text_delta', text: skill ? formatSkillCard(skill) : `No skill "${slug}" in the marketplace.\n` });
685
+ }
686
+ catch (err) {
687
+ ctx.onEvent({ kind: 'text_delta', text: `Could not reach the marketplace: ${err.message}\n` });
688
+ }
689
+ emitDone(ctx);
690
+ return { handled: true };
691
+ }
692
+ // (no arg) browse the top skills, or <keyword> to search — both free.
693
+ try {
694
+ const TOP = 12;
695
+ const skills = await fetchCatalog({ limit: 200, query: arg || undefined });
696
+ const shown = arg ? skills : skills.slice(0, TOP);
697
+ const heading = arg
698
+ ? `Agent talents — ${shown.length} skill(s) matching "${arg}":`
699
+ : `Agent talents — top ${shown.length}${skills.length > TOP ? ` of ${skills.length}` : ''}:`;
700
+ ctx.onEvent({ kind: 'text_delta', text: formatCatalogList(shown, { heading }) });
701
+ }
702
+ catch (err) {
703
+ ctx.onEvent({ kind: 'text_delta', text: `Could not reach the marketplace: ${err.message}\n` });
704
+ }
705
+ emitDone(ctx);
706
+ return { handled: true };
707
+ }
638
708
  // /insights [--days N] — rich usage insights
639
709
  if (input === '/insights' || input.startsWith('/insights ')) {
640
710
  const daysMatch = input.match(/--days\s+(\d+)/);
@@ -1051,7 +1121,7 @@ export async function handleSlashCommand(input, ctx) {
1051
1121
  ...Object.keys(REWRITE_COMMANDS),
1052
1122
  ...ARG_COMMANDS.map(c => c.prefix.trim()),
1053
1123
  ...skillNames,
1054
- '/branch', '/resume', '/model', '/auto', '/wallet', '/cost', '/help', '/clear', '/retry', '/exit', '/session-search', '/ssearch', '/failures',
1124
+ '/branch', '/resume', '/model', '/auto', '/wallet', '/cost', '/help', '/clear', '/retry', '/exit', '/session-search', '/ssearch', '/failures', '/market',
1055
1125
  ];
1056
1126
  const cmd = input.split(/\s/)[0];
1057
1127
  const close = allCommands.filter(c => {
@@ -106,7 +106,7 @@ export declare function modelHasExtendedThinking(model: string): boolean;
106
106
  * direct unit testing — the happy path hits it only on stream error.
107
107
  */
108
108
  export declare function classifyToolCallFailure(toolName: string, rawInput: string, signal: AbortSignal | undefined, model: string): string;
109
- export declare function isRoleplayedJsonToolCallText(text: string): boolean;
109
+ export declare function isRoleplayedJsonToolCallText(text: string, knownToolNames?: ReadonlySet<string>): boolean;
110
110
  export declare class ModelClient {
111
111
  private apiUrl;
112
112
  private chain;
package/dist/agent/llm.js CHANGED
@@ -280,22 +280,51 @@ export function classifyToolCallFailure(toolName, rawInput, signal, model) {
280
280
  `Preview: ${preview}${rawInput.length > 120 ? '…' : ''} — ` +
281
281
  `this is usually a model output bug; try \`/model <other>\` or retry.]`;
282
282
  }
283
- export function isRoleplayedJsonToolCallText(text) {
283
+ export function isRoleplayedJsonToolCallText(text, knownToolNames) {
284
284
  const trimmed = text.trim();
285
- if (!trimmed.startsWith('{') || !trimmed.endsWith('}'))
285
+ if (!trimmed)
286
286
  return false;
287
+ // Cheap pre-check: every shape we accept either starts with `{` or begins
288
+ // with a known XML wrapper. Avoid JSON.parse on long prose.
289
+ const looksJson = trimmed.startsWith('{') && trimmed.endsWith('}');
290
+ const looksXml = /^<tool_call(s)?>/i.test(trimmed);
291
+ if (!looksJson && !looksXml)
292
+ return false;
293
+ if (looksXml) {
294
+ // Match `<tool_call>{...}</tool_call>`, including the multi-call variant.
295
+ return /<tool_call(s)?>[\s\S]*?<\/tool_call(s)?>/i.test(trimmed);
296
+ }
287
297
  try {
288
298
  const parsed = JSON.parse(trimmed);
289
- return (parsed !== null &&
290
- typeof parsed === 'object' &&
291
- !Array.isArray(parsed) &&
292
- parsed.type === 'function' &&
293
- typeof parsed.name === 'string' &&
294
- ('parameters' in parsed || 'arguments' in parsed));
299
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
300
+ return false;
301
+ const obj = parsed;
302
+ // OpenAI envelope: assistant message that forgot to use the tool_calls
303
+ // channel and dumped the whole message JSON into the text stream.
304
+ if (Array.isArray(obj.tool_calls))
305
+ return true;
306
+ // Hermes/Claude roleplay envelope (`type: "function"` + name + arguments).
307
+ if (obj.type === 'function' &&
308
+ typeof obj.name === 'string' &&
309
+ ('parameters' in obj || 'arguments' in obj)) {
310
+ return true;
311
+ }
312
+ // Bare Hermes envelope (no `type`): what nvidia/llama-4-maverick,
313
+ // nvidia/qwen3-next, and similar small models emit. The matching
314
+ // `name` + `parameters|arguments` pair is enough for the scavenger — but
315
+ // only when `name` is a real registered tool. Otherwise ordinary JSON a
316
+ // user asked for (an object that happens to have a `name` and
317
+ // `parameters`/`arguments` field) would be suppressed from display and
318
+ // handed to the scavenger. When no tool set is supplied (e.g. unit tests),
319
+ // fall back to the permissive shape match.
320
+ if (typeof obj.name === 'string' && ('parameters' in obj || 'arguments' in obj)) {
321
+ return knownToolNames ? knownToolNames.has(obj.name) : true;
322
+ }
295
323
  }
296
324
  catch {
297
- return false;
325
+ // Fall through to false; not roleplayed tool-call JSON.
298
326
  }
327
+ return false;
299
328
  }
300
329
  /**
301
330
  * Apply Anthropic prompt caching, budgeted to Anthropic's hard limit of 4
@@ -771,6 +800,11 @@ export class ModelClient {
771
800
  let currentToolName = '';
772
801
  let currentToolInput = '';
773
802
  const textEmission = { mode: 'undecided' };
803
+ // Registered tool names for this request — used to distinguish a roleplayed
804
+ // bare tool-call envelope from ordinary JSON the user asked the model for.
805
+ const knownToolNames = new Set((request.tools ?? [])
806
+ .map((t) => t.name)
807
+ .filter((n) => typeof n === 'string'));
774
808
  const isNemotronProse = isNemotronProseModel(request.model);
775
809
  // Split inline <think>…</think> emitted by reasoning models (nemotron,
776
810
  // deepseek-r1, qwq, etc.) that use the text field instead of the native
@@ -800,9 +834,16 @@ export class ModelClient {
800
834
  const trimmed = currentText.trimStart();
801
835
  if (!trimmed)
802
836
  return;
803
- // Nemotron Omni leaks reasoning prose into the text channel without
804
- // <think> tags. Hold the buffer for end-of-stream stripping.
805
- textEmission.mode = isNemotronProse || trimmed.startsWith('{') ? 'hold' : 'stream';
837
+ // Hold the buffer when the model is roleplaying a tool call as text
838
+ // (free-tier llama/qwen/nemotron variants frequently do this) so
839
+ // the loop's scavenger can recover it instead of streaming junk
840
+ // to the user. Three shapes we recognize:
841
+ // 1. {…} roleplay JSON envelope
842
+ // 2. <tool_call>…</tool_call> XML
843
+ // 3. Nemotron Omni prose (handled separately below)
844
+ const looksLikeRoleplayJson = trimmed.startsWith('{');
845
+ const looksLikeRoleplayXml = /^<tool_call(s)?>/i.test(trimmed);
846
+ textEmission.mode = isNemotronProse || looksLikeRoleplayJson || looksLikeRoleplayXml ? 'hold' : 'stream';
806
847
  if (textEmission.mode === 'stream') {
807
848
  onStreamDelta?.({ type: 'text', text: currentText });
808
849
  }
@@ -952,7 +993,7 @@ export class ModelClient {
952
993
  }
953
994
  }
954
995
  if (currentText) {
955
- if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText)) {
996
+ if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText, knownToolNames)) {
956
997
  // A roleplayed JSON tool call. Do NOT stream it to the UI,
957
998
  // but DO keep it in `collected` so the loop's scavenge pass
958
999
  // (src/agent/repair/scavenge.ts) can turn it into a real
@@ -1048,7 +1089,7 @@ export class ModelClient {
1048
1089
  }
1049
1090
  }
1050
1091
  if (currentText) {
1051
- if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText)) {
1092
+ if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText, knownToolNames)) {
1052
1093
  // See the matching branch in content_block_stop: keep it for the
1053
1094
  // loop's scavenge pass instead of dropping it.
1054
1095
  collected.push({ type: 'text', text: currentText });
@@ -1285,6 +1285,22 @@ export async function interactiveSession(config, getUserInput, onEvent, onAbortR
1285
1285
  if (turnSkillHints) {
1286
1286
  callSystemPrompt = callSystemPrompt + '\n\n' + turnSkillHints;
1287
1287
  }
1288
+ // Force a final answer: withhold tools so the model must commit to text,
1289
+ // either on the last turn or once the tool-call budget is spent. Without
1290
+ // this, models that keep calling tools every turn hit maxTurns with no
1291
+ // answer (and waste the spend). Opt-in per config. Ordered last so a
1292
+ // forced turn intentionally rebuilds the prompt from the base (dropping
1293
+ // the skill hints above); normal turns keep them.
1294
+ const onFinalTurn = config.forceAnswerOnFinalTurn && loopCount === maxTurns;
1295
+ const toolBudgetSpent = config.maxToolCalls != null && turnToolCalls >= config.maxToolCalls;
1296
+ if ((onFinalTurn || toolBudgetSpent) && callToolDefs.length > 0) {
1297
+ callToolDefs = [];
1298
+ callSystemPrompt = systemPrompt + '\n\n' +
1299
+ (toolBudgetSpent
1300
+ ? `You have used your research budget (${config.maxToolCalls} tool calls) — no more tools are available.`
1301
+ : 'This is your FINAL turn — no more tools are available.') +
1302
+ ' Based on the research so far, output ONLY the final answer now, in the exact format requested.';
1303
+ }
1288
1304
  // ── Hallucination guard for weak models ──
1289
1305
  // Weak / free models (nemotron-ultra, GLM-4, qwen coder, free-profile
1290
1306
  // resolves) have been observed inventing tool names (e.g. MixtureOfAgents)
@@ -1403,7 +1419,7 @@ export async function interactiveSession(config, getUserInput, onEvent, onAbortR
1403
1419
  // entries are $0 nvidia models.
1404
1420
  const EMPTY_FALLBACK_MODELS = ['nvidia/qwen3-next-80b-a3b-instruct', 'nvidia/llama-4-maverick'];
1405
1421
  const nextModel = EMPTY_FALLBACK_MODELS.find(m => m !== config.model && !turnFailedModels.has(m));
1406
- if (nextModel && recoveryAttempts < 2) {
1422
+ if (nextModel && recoveryAttempts < 2 && !config.disableModelFallback) {
1407
1423
  recoveryAttempts++;
1408
1424
  turnFailedModels.add(config.model);
1409
1425
  const oldModel = config.model;
@@ -1448,7 +1464,7 @@ export async function interactiveSession(config, getUserInput, onEvent, onAbortR
1448
1464
  'anthropic/claude-sonnet-4.6',
1449
1465
  ];
1450
1466
  const nextModel = TOOL_USE_FALLBACK_MODELS.find(m => m !== config.model && !turnFailedModels.has(m));
1451
- if (nextModel && recoveryAttempts < 2) {
1467
+ if (nextModel && recoveryAttempts < 2 && !config.disableModelFallback) {
1452
1468
  recoveryAttempts++;
1453
1469
  turnFailedModels.add(config.model);
1454
1470
  const oldModel = config.model;
@@ -2064,7 +2080,7 @@ export async function interactiveSession(config, getUserInput, onEvent, onAbortR
2064
2080
  .filter(p => p.type === 'text' && typeof p.text === 'string')
2065
2081
  .map(p => p.text)
2066
2082
  .join('');
2067
- if (shouldCheckGrounding(lastUserInput || '', assistantText)) {
2083
+ if (!config.disableGroundingRetry && shouldCheckGrounding(lastUserInput || '', assistantText)) {
2068
2084
  const gResult = await checkGrounding(lastUserInput, history, assistantText, client, {
2069
2085
  abortSignal: abort.signal,
2070
2086
  });
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Media router — one LLM call that picks which image/video model fits
3
+ * the user's request, with alternatives and cost estimates, so the agent
4
+ * can show a clean AskUser proposal before spending.
5
+ *
6
+ * Principle (matches turn-analyzer): harness orchestrates, free model
7
+ * decides. No keyword-to-model mapping in TypeScript. Classifier reads
8
+ * the prompt + the current gateway catalog (pulled dynamically), picks
9
+ * one recommended model plus a cheaper + a premium alternative, and
10
+ * explains the choice in one sentence.
11
+ *
12
+ * Cost estimates come from `gateway-models.ts` — always dynamic,
13
+ * margin-adjusted.
14
+ */
15
+ import type { ModelClient } from './llm.js';
16
+ export type MediaKind = 'image' | 'video';
17
+ export type MediaStyle = 'photoreal' | 'illustration' | 'anime' | 'logo' | 'concept' | 'other';
18
+ export type MediaPriority = 'cost' | 'quality' | 'balanced';
19
+ export interface MediaChoice {
20
+ model: string;
21
+ estimatedCostUsd: number;
22
+ rationale: string;
23
+ }
24
+ export interface MediaProposal {
25
+ kind: MediaKind;
26
+ quantity: number;
27
+ durationSeconds?: number;
28
+ maxDurationSeconds?: number;
29
+ recommended: MediaChoice;
30
+ cheaper?: MediaChoice;
31
+ premium?: MediaChoice;
32
+ intent: {
33
+ style: MediaStyle;
34
+ priority: MediaPriority;
35
+ };
36
+ /**
37
+ * A fuller rewrite of the user's prompt, following the 5-slot template
38
+ * (scene/subject/details/use-case/constraints). Null when the classifier
39
+ * judged the input already well-specified, or when the env opt-out is set,
40
+ * or when the rewrite was identical to the raw input. When non-null, the
41
+ * AskUser layout surfaces it as "Refined:" with a "Use ORIGINAL" option.
42
+ */
43
+ refinedPrompt: string | null;
44
+ refinementSummary: string;
45
+ totalCostUsd: number;
46
+ }
47
+ export declare function clearMediaRouterCache(): void;
48
+ /**
49
+ * Normalize a refined prompt: trim, cap length, reject obvious junk.
50
+ * Returns null when the value should be treated as absent (missing,
51
+ * non-string, empty after trim).
52
+ *
53
+ * Exported for testability — invariants matter more here than elsewhere
54
+ * because the output is user-visible and paid for.
55
+ */
56
+ export declare function validateRefined(raw: unknown, maxChars: number): string | null;
57
+ /**
58
+ * Whitespace-insensitive, case-insensitive identity check — if the
59
+ * classifier's "refinement" is just the input with different spacing,
60
+ * don't bother the user with a "Refined:" block.
61
+ */
62
+ export declare function isEffectivelyIdentical(a: string, b: string): boolean;
63
+ export declare const REFINED_PROMPT_MAX_CHARS = 500;
64
+ export declare const REFINEMENT_SUMMARY_LIMIT = 80;
65
+ export interface AnalyzeMediaOpts {
66
+ kind: MediaKind;
67
+ prompt: string;
68
+ client: ModelClient;
69
+ quantity?: number;
70
+ durationSeconds?: number;
71
+ signal?: AbortSignal;
72
+ /**
73
+ * One-shot opt-out — caller stripped a `///` prefix from the user's input
74
+ * and wants the proposal rendered without a Refined block or Use-original
75
+ * option. The classifier still runs (for model selection), but the
76
+ * refinement is discarded at parse time.
77
+ */
78
+ skipRefine?: boolean;
79
+ }
80
+ /**
81
+ * Pick the best model + alternatives for this media request. Returns null
82
+ * on any failure path (classifier timeout, parse error, empty catalog) so
83
+ * the caller can fall back to its old hardcoded default rather than
84
+ * blocking the user.
85
+ */
86
+ export declare function analyzeMediaRequest(opts: AnalyzeMediaOpts): Promise<MediaProposal | null>;
87
+ /**
88
+ * Render a proposal as the user-facing AskUser question. Layout matches
89
+ * the spec from v3.8.31 planning: recommended first with • bullet,
90
+ * alternatives below with ○ bullets, prices include the 5% margin note.
91
+ */
92
+ export declare function renderProposalForAskUser(p: MediaProposal, userPrompt: string): {
93
+ question: string;
94
+ options: Array<{
95
+ id: string;
96
+ label: string;
97
+ }>;
98
+ };