@blockrun/franklin 3.31.0 → 3.32.0

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
  });
@@ -131,6 +131,16 @@ export class PermissionManager {
131
131
  }
132
132
  return { behavior: 'ask' };
133
133
  }
134
+ // agent_talent: browsing the marketplace is a free read (auto-allow);
135
+ // hiring (action="run") spends USDC from the wallet and has no refund, so
136
+ // it asks — same policy as the other paid, irreversible tools (VoiceCall,
137
+ // BuyPhoneNumber). describeAction spells out the spend in the prompt.
138
+ if (toolName === 'agent_talent') {
139
+ const action = typeof input.action === 'string' ? input.action.toLowerCase() : '';
140
+ return action === 'run'
141
+ ? { behavior: 'ask' }
142
+ : { behavior: 'allow', reason: 'free marketplace browse' };
143
+ }
134
144
  // Default: read-only tools are auto-allowed, others ask
135
145
  if (READ_ONLY_TOOLS.has(toolName)) {
136
146
  return { behavior: 'allow', reason: 'read-only default' };
@@ -325,6 +335,13 @@ export class PermissionManager {
325
335
  }
326
336
  case 'Agent':
327
337
  return `Launch sub-agent: ${input.description || input.prompt?.slice(0, 80) || 'task'}`;
338
+ case 'agent_talent': {
339
+ if ((input.action || '').toLowerCase() === 'run') {
340
+ const slug = input.slug || 'a skill';
341
+ return `Hire '${slug}' from the agent marketplace — pays from your wallet (USDC on Base, up to $5/hire), charged only on a successful run.`;
342
+ }
343
+ return 'Browse the agent marketplace (free).';
344
+ }
328
345
  default:
329
346
  return JSON.stringify(input).slice(0, 120);
330
347
  }
@@ -187,6 +187,30 @@ export interface AgentConfig {
187
187
  maxSpendUsd?: number;
188
188
  /** Show user-visible harness prefetch status lines (interactive UX only). */
189
189
  showPrefetchStatus?: boolean;
190
+ /**
191
+ * On the final turn, withhold tools so the model must commit to a text answer
192
+ * instead of researching until cut off. For one-shot forecasting/extraction
193
+ * callers (e.g. `franklin predict`) where some models never stop calling tools
194
+ * and would otherwise hit maxTurns with no answer.
195
+ */
196
+ forceAnswerOnFinalTurn?: boolean;
197
+ /**
198
+ * Hard cap on total tool calls for the turn. Once reached, tools are withheld
199
+ * and the model is forced to answer from what it has. Bounds research/cost
200
+ * deterministically (a turn budget alone doesn't — a turn may have no tool).
201
+ */
202
+ maxToolCalls?: number;
203
+ /**
204
+ * Disable Franklin's automatic model-switching (empty-response / stalled-intent
205
+ * fallbacks). One-shot callers want a clean abstain from the requested model,
206
+ * not a silent switch to a different one.
207
+ */
208
+ disableModelFallback?: boolean;
209
+ /**
210
+ * Disable the post-response "ungrounded claims → force a tool-use retry" guard.
211
+ * It fights the forced-answer path and pollutes one-shot structured output.
212
+ */
213
+ disableGroundingRetry?: boolean;
190
214
  /** Mid-turn "research-bloat" compaction — summarizes history when a turn
191
215
  * racks up many tool calls + spend, to cut input-replay cost. Default on;
192
216
  * set false to disable (the desktop exposes this as a toggle). */
@@ -0,0 +1,10 @@
1
+ export interface PredictOptions {
2
+ model?: string;
3
+ question?: string;
4
+ maxTurns?: string;
5
+ maxToolCalls?: string;
6
+ maxSpend?: string;
7
+ json?: boolean;
8
+ debug?: boolean;
9
+ }
10
+ export declare function predictCommand(options: PredictOptions): Promise<void>;
@@ -0,0 +1,143 @@
1
+ /**
2
+ * `franklin predict` — Franklin prediction mode (headless).
3
+ *
4
+ * Runs ONE model as a disciplined forecaster: it researches a single real-world
5
+ * event with a tight, read-only toolset (web search, source fetch, Exa, X, live
6
+ * prediction markets, a little market data) the way a bettor would before
7
+ * putting money down — then commits to a pick with a confidence.
8
+ *
9
+ * Designed for machine callers (e.g. BlockRun Oracle): with --json it emits a
10
+ * single JSON envelope on stdout containing the model's final answer, the full
11
+ * tool-call trace (what it searched and what it found), the terminal reason and
12
+ * token usage. Human-readable streaming otherwise.
13
+ *
14
+ * franklin predict --model anthropic/claude-opus-4.8 \
15
+ * --question "Who wins the 2026 FIFA World Cup? Pick one country." --json
16
+ */
17
+ import { interactiveSession } from '../agent/loop.js';
18
+ import { predictionCapabilities, resetToolSessionState } from '../tools/index.js';
19
+ import { loadChain, API_URLS } from '../config.js';
20
+ import { resolveModel } from '../ui/model-picker.js';
21
+ const PREDICTION_SYSTEM = [
22
+ 'You are a sharp, disciplined forecasting analyst — think like a professional who is about to put real money on this question.',
23
+ 'Your job: predict the outcome of ONE real-world event. Before answering you MUST do research the way a bettor would:',
24
+ "1. Use web_search (and webfetch / exa tools) for the most CURRENT facts and news — today's real-world state matters far more than your training data.",
25
+ '2. Use search_prediction_markets to read the CURRENT market-implied odds (Polymarket, Kalshi, etc.) for this or a closely related question.',
26
+ '3. Weigh it: where is the consensus, where might the market be mispriced, what is your edge.',
27
+ 'Budget your research: make AT MOST 4-5 focused tool calls in total. As soon as you have enough to decide, STOP calling tools and output the JSON. Do not keep researching — an answer with light research beats no answer.',
28
+ 'Your FINAL message must end with EXACTLY ONE single-line minified JSON object and NOTHING after it:',
29
+ '{"pick": string, "confidence": number, "rationale": string, "analysis": string, "marketOdds": string}',
30
+ '- pick: one option from the question (a short label, e.g. a country, party, bucket, or Yes/No).',
31
+ '- confidence: your probability (0-1) that THIS pick is correct.',
32
+ '- rationale: one sharp sentence (max 22 words).',
33
+ '- analysis: 3-5 sentences citing what your research found, the strongest counter-argument, and why you still land here. No literal newlines inside the string.',
34
+ "- marketOdds: what the prediction market currently implies (e.g. 'Polymarket: France 18%'), or 'n/a' if none found.",
35
+ 'Be decisive. Do not hedge with "it depends".',
36
+ ];
37
+ export async function predictCommand(options) {
38
+ const question = options.question?.trim();
39
+ if (!question) {
40
+ process.stderr.write('predict: --question is required\n');
41
+ process.exitCode = 1;
42
+ return;
43
+ }
44
+ if (!options.model) {
45
+ process.stderr.write('predict: --model is required\n');
46
+ process.exitCode = 1;
47
+ return;
48
+ }
49
+ const chain = loadChain();
50
+ const apiUrl = API_URLS[chain];
51
+ const model = resolveModel(options.model);
52
+ const asJson = options.json !== false;
53
+ resetToolSessionState();
54
+ const agentConfig = {
55
+ model,
56
+ apiUrl,
57
+ chain,
58
+ systemInstructions: PREDICTION_SYSTEM,
59
+ capabilities: predictionCapabilities,
60
+ maxTurns: options.maxTurns != null ? Number(options.maxTurns) : 8,
61
+ permissionMode: 'trust',
62
+ debug: !!options.debug,
63
+ showPrefetchStatus: false,
64
+ // Governance for one-shot forecasting: bound research by tool-call count and
65
+ // force an answer; don't silently switch models or fight a grounding retry.
66
+ // Tool budget (5) is the real research limiter; maxTurns (8) is just slack
67
+ // above it for a thinking turn + the forced-answer turn.
68
+ forceAnswerOnFinalTurn: true,
69
+ maxToolCalls: options.maxToolCalls != null ? Number(options.maxToolCalls) : 6,
70
+ disableModelFallback: true,
71
+ disableGroundingRetry: true,
72
+ ...(options.maxSpend != null ? { maxSpendUsd: Number(options.maxSpend) } : {}),
73
+ };
74
+ let finalText = '';
75
+ let turnReason = 'completed';
76
+ let turnError;
77
+ let inputTokens = 0;
78
+ let outputTokens = 0;
79
+ const trace = [];
80
+ const nameById = new Map();
81
+ const inputById = new Map();
82
+ const previewById = new Map();
83
+ let delivered = false;
84
+ const getInput = async () => {
85
+ if (delivered)
86
+ return null;
87
+ delivered = true;
88
+ return question;
89
+ };
90
+ await interactiveSession(agentConfig, getInput, (event) => {
91
+ switch (event.kind) {
92
+ case 'text_delta':
93
+ finalText += event.text;
94
+ if (!asJson)
95
+ process.stdout.write(event.text);
96
+ break;
97
+ case 'capability_start':
98
+ nameById.set(event.id, event.name);
99
+ inputById.set(event.id, '');
100
+ if (event.preview)
101
+ previewById.set(event.id, event.preview);
102
+ if (!asJson)
103
+ process.stderr.write(`\n · ${event.name}${event.preview ? ` ${event.preview}` : ''}\n`);
104
+ break;
105
+ case 'capability_input_delta':
106
+ inputById.set(event.id, (inputById.get(event.id) || '') + event.delta);
107
+ break;
108
+ case 'capability_done': {
109
+ const tool = nameById.get(event.id) || 'tool';
110
+ const input = (inputById.get(event.id) || '').trim() || previewById.get(event.id) || '';
111
+ const output = event.result?.fullOutput || event.result?.output || '';
112
+ trace.push({ tool, input, output: output.slice(0, 1500), isError: event.result?.isError });
113
+ break;
114
+ }
115
+ case 'usage':
116
+ // Fires once per LLM call; accumulate across research turns so the
117
+ // --json envelope reports total spend, not just the final turn.
118
+ inputTokens += event.inputTokens;
119
+ outputTokens += event.outputTokens;
120
+ break;
121
+ case 'turn_done':
122
+ turnReason = event.reason;
123
+ turnError = event.error;
124
+ break;
125
+ }
126
+ });
127
+ if (asJson) {
128
+ const envelope = {
129
+ model,
130
+ question,
131
+ finalText: finalText.trim(),
132
+ trace,
133
+ turnReason,
134
+ ...(turnError ? { error: turnError } : {}),
135
+ usage: { inputTokens, outputTokens },
136
+ };
137
+ process.stdout.write(JSON.stringify(envelope) + '\n');
138
+ }
139
+ else if (turnReason !== 'completed' && turnError) {
140
+ process.stderr.write(`\n${turnError}\n`);
141
+ }
142
+ process.exitCode = turnReason === 'completed' ? 0 : 1;
143
+ }
@@ -13,7 +13,7 @@ import { interactiveSession } from '../agent/loop.js';
13
13
  import { allCapabilities, createSubAgentCapability } from '../tools/index.js';
14
14
  import { validateToolDescriptions } from '../tools/validate.js';
15
15
  import { launchInkUI } from '../ui/app.js';
16
- import { pickModel, resolveModel } from '../ui/model-picker.js';
16
+ import { pickModel, resolveModel, resolveModelStrict } from '../ui/model-picker.js';
17
17
  import { loadMcpConfig } from '../mcp/config.js';
18
18
  import { connectMcpServers, disconnectMcpServers, getMcpServerInstructions } from '../mcp/client.js';
19
19
  import { ensureCodegraphIndex } from '../mcp/codegraph.js';
@@ -620,9 +620,16 @@ async function runWithBasicUI(agentConfig, model, workDir, initialInput) {
620
620
  continue;
621
621
  }
622
622
  if (input.startsWith('/model ')) {
623
- const newModel = resolveModel(input.slice(7).trim());
624
- agentConfig.model = newModel;
625
- console.error(chalk.green(` Model → ${newModel}`));
623
+ const resolved = resolveModelStrict(input.slice(7).trim());
624
+ if (!resolved.ok) {
625
+ console.error(chalk.yellow(` ${resolved.suggestion}`));
626
+ continue;
627
+ }
628
+ agentConfig.model = resolved.id;
629
+ console.error(chalk.green(` Model → ${resolved.id}`));
630
+ if (resolved.viaShortcut) {
631
+ console.error(chalk.dim(` (alias ${input.slice(7).trim()} → ${resolved.id})`));
632
+ }
626
633
  continue;
627
634
  }
628
635
  // /retry — resend last prompt
@@ -739,9 +746,19 @@ async function handleSlashCommand(cmd, config, ui) {
739
746
  case '/model': {
740
747
  const newModel = parts[1];
741
748
  if (newModel) {
742
- config.model = resolveModel(newModel);
743
- config.baseModel = config.model;
744
- console.error(chalk.green(` Model → ${config.model}`));
749
+ const resolved = resolveModelStrict(newModel);
750
+ if (!resolved.ok) {
751
+ console.error(chalk.yellow(` ${resolved.suggestion}`));
752
+ return null;
753
+ }
754
+ config.model = resolved.id;
755
+ config.baseModel = resolved.id;
756
+ const suffix = resolved.viaShortcut
757
+ ? chalk.dim(` (alias ${newModel} → ${resolved.id})`)
758
+ : '';
759
+ console.error(chalk.green(` Model → ${resolved.id}`));
760
+ if (suffix)
761
+ console.error(suffix);
745
762
  return null;
746
763
  }
747
764
  const picked = await pickModel(config.model);
package/dist/config.d.ts CHANGED
@@ -5,5 +5,6 @@ export declare const BLOCKRUN_DIR: string;
5
5
  export declare const CHAIN_FILE: string;
6
6
  export declare const API_URLS: Record<Chain, string>;
7
7
  export declare const DEFAULT_PROXY_PORT = 8402;
8
+ export declare const MARKET_URL: string;
8
9
  export declare function saveChain(chain: Chain): void;
9
10
  export declare function loadChain(): Chain;
package/dist/config.js CHANGED
@@ -19,6 +19,12 @@ export const API_URLS = {
19
19
  solana: 'https://sol.blockrun.ai/api',
20
20
  };
21
21
  export const DEFAULT_PROXY_PORT = 8402;
22
+ // BlockRun agent-market (the paid skill marketplace Franklin browses with
23
+ // `/market` and hires with the agent_talent tool). It speaks standard
24
+ // single-leg `exact` x402 on Base, so Franklin pays it with the same EVM
25
+ // wallet it uses for the gateway. Overridable via env for local end-to-end
26
+ // testing against a dev server.
27
+ export const MARKET_URL = (process.env.BLOCKRUN_MARKET_URL || 'https://business.blockrun.ai').replace(/\/+$/, '');
22
28
  export function saveChain(chain) {
23
29
  fs.mkdirSync(BLOCKRUN_DIR, { recursive: true });
24
30
  fs.writeFileSync(CHAIN_FILE, chain + '\n', { mode: 0o600 });
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ import { uninitCommand } from './commands/uninit.js';
28
28
  import { proxyCommand } from './commands/proxy.js';
29
29
  import { buildTaskCommand } from './commands/task.js';
30
30
  import { buildContentCommand } from './commands/content.js';
31
+ import { predictCommand } from './commands/predict.js';
31
32
  import { VERSION as version } from './config.js';
32
33
  const program = new Command();
33
34
  program
@@ -68,6 +69,17 @@ program
68
69
  .option('--no-fallback', 'Disable automatic fallback to backup models')
69
70
  .option('--debug', 'Enable debug logging')
70
71
  .action((options) => proxyCommand({ ...options, version }));
72
+ program
73
+ .command('predict')
74
+ .description('Prediction mode — forecast one real-world event with a research-only toolset (web/markets), headless')
75
+ .requiredOption('-m, --model <model>', 'Model to use (e.g. anthropic/claude-opus-4.8, openai/gpt-5.5)')
76
+ .requiredOption('-q, --question <text>', 'The event question to forecast (include the allowed options)')
77
+ .option('--max-turns <n>', 'Max agent turns before forcing an answer', '8')
78
+ .option('--max-tool-calls <n>', 'Max tool calls before forcing an answer', '6')
79
+ .option('--max-spend <usd>', 'Hard USD cap on this prediction run')
80
+ .option('--no-json', 'Human-readable streaming instead of a JSON envelope')
81
+ .option('--debug', 'Enable debug logging')
82
+ .action((options) => predictCommand(options));
71
83
  program
72
84
  .command('init')
73
85
  .description('Configure franklin auto-start (writes ~/.claude/settings.json + installs LaunchAgent on macOS)')