@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.
@@ -0,0 +1,328 @@
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 { getModelsByCategory, estimateCostUsd, defaultDurationSeconds, maxDurationSeconds, } from '../gateway-models.js';
16
+ // ─── Classifier ─────────────────────────────────────────────────────────
17
+ const CLASSIFIER_MODEL = process.env.FRANKLIN_MEDIA_ROUTER_MODEL || 'nvidia/llama-4-maverick';
18
+ const TIMEOUT_MS = 3_500; // slightly more lenient than the turn-analyzer — we're asking for JSON with reasoning
19
+ const MAX_TOKENS = 384; // bumped from 256 to leave room for refined_prompt (≤500 chars) + refinement_summary (≤80)
20
+ const REFINED_MAX_CHARS = 500;
21
+ const REFINEMENT_SUMMARY_MAX_CHARS = 80;
22
+ function buildSystemPrompt(kind, catalog) {
23
+ const catalogLines = catalog.map(m => {
24
+ const p = m.pricing;
25
+ const price = kind === 'image'
26
+ ? `$${(p.per_image ?? 0).toFixed(4)}/image`
27
+ : `$${(p.per_second ?? 0).toFixed(2)}/s (default ${p.default_duration_seconds ?? 8}s, max ${p.max_duration_seconds ?? 8}s)`;
28
+ return ` - ${m.id} · ${price} · ${m.description || m.name}`;
29
+ }).join('\n');
30
+ return `You pick the best ${kind} model for a user's Franklin request AND refine the user's prompt. Output ONE LINE of compact JSON. No markdown, no code fences, no explanation.
31
+
32
+ ## Catalog (${catalog.length} available ${kind} models)
33
+ ${catalogLines}
34
+
35
+ ## Output schema
36
+
37
+ {"style":"photoreal|illustration|anime|logo|concept|other",
38
+ "priority":"cost|quality|balanced",
39
+ "refined_prompt":"<rewritten prompt in the user's language, <=${REFINED_MAX_CHARS} chars, or null if already well-specified>",
40
+ "refinement_summary":"<one short sentence, <=${REFINEMENT_SUMMARY_MAX_CHARS} chars, user-visible>",
41
+ "recommended":{"model":"<id from catalog>","rationale":"<one sentence, <=140 chars>"},
42
+ "cheaper":{"model":"<id from catalog | null>","rationale":"<one sentence>"},
43
+ "premium":{"model":"<id from catalog | null>","rationale":"<one sentence>"}}
44
+
45
+ Rules:
46
+ - recommended is always set to an id from the catalog.
47
+ - cheaper / premium may be null if no strictly cheaper / better option exists.
48
+ - Never invent a model id. Use EXACTLY one of the catalog ids.
49
+ - Match style → model: anime/illustration prefers CogView, photoreal prefers Nano Banana Pro / Grok Imagine Pro, budget-conscious picks cheapest-acceptable.
50
+ - One sentence rationale, user-visible.
51
+
52
+ ## Refinement (emit refined_prompt + refinement_summary)
53
+
54
+ If the user's prompt is missing ≥3 of these 5 slots, rewrite to fill them. If it already has ≥3 covered, set refined_prompt to null and refinement_summary to "Already well-specified".
55
+
56
+ 1. Scene — location, time of day, environment, background
57
+ 2. Subject — primary focus (who / what), preserved EXACTLY from the user's input (no substitution)
58
+ 3. Details — materials, textures, lighting, camera/lens feel, composition, mood (concrete visual facts, not praise)
59
+ 4. Use Case — editorial photo, product mockup, UI screen, logo, storyboard frame, social-media cover, etc.
60
+ 5. Constraints — aspect ratio, what must not drift (no watermark, preserve face, no text), hard asks
61
+
62
+ Anti-slop rules:
63
+ - Concrete visual facts ("overcast daylight", "brushed aluminum") beat vague praise ("stunning", "cinematic masterpiece").
64
+ - Wrap literal text that must appear in the image in double quotes. Spell difficult words letter-by-letter.
65
+ - One revision per turn — do not combine conflicting asks.
66
+ - Natural language, not keyword-tag format.
67
+ - refined_prompt stays in the same language as the user input.
68
+
69
+ Examples:
70
+
71
+ Input: "a photo of a cat on Mars, photoreal"
72
+ Output: {"style":"photoreal","priority":"balanced","refined_prompt":"Eye-level photograph of a cat standing on the rust-colored Martian surface, late-afternoon low sun casting long shadows, distant canyon rim in the background, 50mm feel, shallow depth of field, editorial photo use, no watermark.","refinement_summary":"Added scene, lighting, lens, use case, constraint.","recommended":{"model":"google/nano-banana-pro","rationale":"Photoreal scenes — Nano Banana Pro has strong realism at moderate cost."},"cheaper":{"model":"google/nano-banana","rationale":"Same family, lower cost, slightly less detail."},"premium":{"model":"openai/gpt-image-2","rationale":"Best photoreal fidelity when budget allows."}}
73
+
74
+ Input: "cyberpunk-style anime character"
75
+ Output: {"style":"anime","priority":"balanced","refined_prompt":"Cyberpunk-style anime character standing on a neon-lit rainy street at night, wearing a synthetic-fiber jacket with metallic reflective accents, holographic billboards floating overhead, low-angle view, strong cyan-and-pink contrast, poster use, centered composition.","refinement_summary":"Added scene, lighting, materials, use case, composition.","recommended":{"model":"zai/cogview-4","rationale":"CogView-4 specializes in stylized/anime imagery."},"cheaper":{"model":"google/nano-banana","rationale":"Cheaper but less stylized."},"premium":{"model":"xai/grok-imagine-image-pro","rationale":"Premium detail for complex scenes."}}
76
+
77
+ Input: "a 10-second cinematic drone shot over Tokyo at night"
78
+ Output: {"style":"concept","priority":"quality","refined_prompt":null,"refinement_summary":"Already well-specified.","recommended":{"model":"bytedance/seedance-2.0","rationale":"Seedance 2.0 delivers the best cinematic quality."},"cheaper":{"model":"bytedance/seedance-2.0-fast","rationale":"Faster + cheaper, minor quality trade-off."},"premium":{"model":null,"rationale":"2.0 is already the top tier."}}
79
+
80
+ Output JSON only, single line.`;
81
+ }
82
+ const cache = new Map();
83
+ const CACHE_TTL_MS = 30_000;
84
+ const CACHE_MAX = 32;
85
+ function hashKey(parts) {
86
+ const s = parts.join('|');
87
+ let h = 0;
88
+ for (let i = 0; i < s.length; i++)
89
+ h = ((h << 5) - h + s.charCodeAt(i)) | 0;
90
+ return String(h);
91
+ }
92
+ export function clearMediaRouterCache() { cache.clear(); }
93
+ // ─── Parser ─────────────────────────────────────────────────────────────
94
+ const VALID_STYLES = new Set(['photoreal', 'illustration', 'anime', 'logo', 'concept', 'other']);
95
+ const VALID_PRIORITIES = new Set(['cost', 'quality', 'balanced']);
96
+ function validateChoice(raw, catalog) {
97
+ if (!raw || typeof raw !== 'object')
98
+ return null;
99
+ const id = typeof raw.model === 'string' ? raw.model : '';
100
+ const model = catalog.get(id);
101
+ if (!model)
102
+ return null;
103
+ const rationale = typeof raw.rationale === 'string' ? raw.rationale.slice(0, 240) : '';
104
+ return { model, rationale };
105
+ }
106
+ /**
107
+ * Normalize a refined prompt: trim, cap length, reject obvious junk.
108
+ * Returns null when the value should be treated as absent (missing,
109
+ * non-string, empty after trim).
110
+ *
111
+ * Exported for testability — invariants matter more here than elsewhere
112
+ * because the output is user-visible and paid for.
113
+ */
114
+ export function validateRefined(raw, maxChars) {
115
+ if (typeof raw !== 'string')
116
+ return null;
117
+ const trimmed = raw.trim();
118
+ if (trimmed.length === 0)
119
+ return null;
120
+ return trimmed.slice(0, maxChars);
121
+ }
122
+ /**
123
+ * Whitespace-insensitive, case-insensitive identity check — if the
124
+ * classifier's "refinement" is just the input with different spacing,
125
+ * don't bother the user with a "Refined:" block.
126
+ */
127
+ export function isEffectivelyIdentical(a, b) {
128
+ const normalize = (s) => s.toLowerCase().replace(/\s+/g, ' ').trim();
129
+ return normalize(a) === normalize(b);
130
+ }
131
+ export const REFINED_PROMPT_MAX_CHARS = REFINED_MAX_CHARS;
132
+ export const REFINEMENT_SUMMARY_LIMIT = REFINEMENT_SUMMARY_MAX_CHARS;
133
+ /**
134
+ * Pick the best model + alternatives for this media request. Returns null
135
+ * on any failure path (classifier timeout, parse error, empty catalog) so
136
+ * the caller can fall back to its old hardcoded default rather than
137
+ * blocking the user.
138
+ */
139
+ export async function analyzeMediaRequest(opts) {
140
+ if (process.env.FRANKLIN_NO_MEDIA_ROUTER === '1')
141
+ return null;
142
+ const { kind, prompt, client } = opts;
143
+ if (!prompt || prompt.trim().length === 0)
144
+ return null;
145
+ // Pull catalog first — if the gateway doesn't have any models in this
146
+ // category, there's nothing to recommend.
147
+ const catalog = await getModelsByCategory(kind).catch(() => []);
148
+ if (catalog.length === 0)
149
+ return null;
150
+ // Cache check — classifier output is stable for a given prompt + catalog
151
+ // version, so re-asking within 30s is waste. Cache stores the FULL
152
+ // classifier response (refinement + model picks); the per-call mask for
153
+ // skipRefine / FRANKLIN_NO_MEDIA_PROMPT_REFINE is applied on the way out
154
+ // so the same cache entry serves callers with different opt-out flags.
155
+ const quantity = Math.max(1, Math.floor(opts.quantity ?? 1));
156
+ const globalOptOut = process.env.FRANKLIN_NO_MEDIA_PROMPT_REFINE === '1';
157
+ const shouldDiscard = globalOptOut || opts.skipRefine === true;
158
+ const maskRefinement = (p) => shouldDiscard ? { ...p, refinedPrompt: null, refinementSummary: '' } : p;
159
+ const key = hashKey([kind, prompt.trim().slice(0, 500), String(quantity), String(opts.durationSeconds ?? '')]);
160
+ const hit = cache.get(key);
161
+ if (hit && hit.expiresAt > Date.now())
162
+ return maskRefinement(hit.value);
163
+ // Call the classifier.
164
+ const catalogMap = new Map(catalog.map(m => [m.id, m]));
165
+ const system = buildSystemPrompt(kind, catalog);
166
+ const ctrl = new AbortController();
167
+ const timer = setTimeout(() => ctrl.abort(), TIMEOUT_MS);
168
+ const signal = opts.signal ? combineSignals([opts.signal, ctrl.signal]) : ctrl.signal;
169
+ let raw = '';
170
+ try {
171
+ const response = await client.complete({
172
+ model: CLASSIFIER_MODEL,
173
+ system,
174
+ messages: [{ role: 'user', content: prompt.slice(0, 1000) }],
175
+ tools: [],
176
+ max_tokens: MAX_TOKENS,
177
+ }, signal);
178
+ for (const part of response.content) {
179
+ if (typeof part === 'object' && part.type === 'text' && part.text)
180
+ raw += part.text;
181
+ }
182
+ }
183
+ catch {
184
+ return null;
185
+ }
186
+ finally {
187
+ clearTimeout(timer);
188
+ }
189
+ // Parse one-line JSON (may be wrapped in stray text).
190
+ const match = raw.match(/\{[\s\S]*\}/);
191
+ if (!match)
192
+ return null;
193
+ let parsed;
194
+ try {
195
+ parsed = JSON.parse(match[0]);
196
+ }
197
+ catch {
198
+ return null;
199
+ }
200
+ const style = typeof parsed.style === 'string' && VALID_STYLES.has(parsed.style)
201
+ ? parsed.style : 'other';
202
+ const priority = typeof parsed.priority === 'string' && VALID_PRIORITIES.has(parsed.priority)
203
+ ? parsed.priority : 'balanced';
204
+ const rec = validateChoice(parsed.recommended, catalogMap);
205
+ if (!rec)
206
+ return null;
207
+ const cheaperChoice = validateChoice(parsed.cheaper, catalogMap);
208
+ const premiumChoice = validateChoice(parsed.premium, catalogMap);
209
+ // Refinement fields. The cache stores the full classifier output; the
210
+ // per-call mask is applied on the way out via maskRefinement() above, so
211
+ // here we just normalize + discard rewrites that are effectively the
212
+ // same as the raw input (drift-proof).
213
+ let refinedPrompt = validateRefined(parsed.refined_prompt, REFINED_MAX_CHARS);
214
+ const refinementSummary = validateRefined(parsed.refinement_summary, REFINEMENT_SUMMARY_MAX_CHARS) ?? '';
215
+ if (refinedPrompt !== null && isEffectivelyIdentical(refinedPrompt, prompt)) {
216
+ refinedPrompt = null;
217
+ }
218
+ // Build proposal with live cost estimates.
219
+ const durationSeconds = kind === 'video'
220
+ ? (opts.durationSeconds ?? defaultDurationSeconds(rec.model))
221
+ : undefined;
222
+ const maxDur = kind === 'video' ? (maxDurationSeconds(rec.model) ?? undefined) : undefined;
223
+ const toChoice = (c) => {
224
+ if (!c || c.model.id === rec.model.id)
225
+ return undefined;
226
+ return {
227
+ model: c.model.id,
228
+ estimatedCostUsd: estimateCostUsd(c.model, { quantity, duration_seconds: durationSeconds }),
229
+ rationale: c.rationale,
230
+ };
231
+ };
232
+ const recommended = {
233
+ model: rec.model.id,
234
+ estimatedCostUsd: estimateCostUsd(rec.model, { quantity, duration_seconds: durationSeconds }),
235
+ rationale: rec.rationale,
236
+ };
237
+ const proposal = {
238
+ kind,
239
+ quantity,
240
+ durationSeconds,
241
+ maxDurationSeconds: maxDur,
242
+ recommended,
243
+ cheaper: toChoice(cheaperChoice),
244
+ premium: toChoice(premiumChoice),
245
+ intent: { style, priority },
246
+ refinedPrompt,
247
+ refinementSummary,
248
+ totalCostUsd: recommended.estimatedCostUsd,
249
+ };
250
+ // Evict oldest if bounded
251
+ if (cache.size >= CACHE_MAX) {
252
+ const firstKey = cache.keys().next().value;
253
+ if (firstKey)
254
+ cache.delete(firstKey);
255
+ }
256
+ cache.set(key, { value: proposal, expiresAt: Date.now() + CACHE_TTL_MS });
257
+ return maskRefinement(proposal);
258
+ }
259
+ // ─── Presentation ───────────────────────────────────────────────────────
260
+ /**
261
+ * Render a proposal as the user-facing AskUser question. Layout matches
262
+ * the spec from v3.8.31 planning: recommended first with • bullet,
263
+ * alternatives below with ○ bullets, prices include the 5% margin note.
264
+ */
265
+ export function renderProposalForAskUser(p, userPrompt) {
266
+ const lines = [];
267
+ lines.push(`*Media generation proposal*`);
268
+ lines.push('');
269
+ lines.push(`Prompt: "${userPrompt.trim().slice(0, 200)}"`);
270
+ if (p.refinedPrompt) {
271
+ lines.push('');
272
+ lines.push(`Refined: ${p.refinedPrompt}`);
273
+ if (p.refinementSummary) {
274
+ lines.push(` (${p.refinementSummary})`);
275
+ }
276
+ }
277
+ if (p.kind === 'video' && p.durationSeconds) {
278
+ const maxNote = p.maxDurationSeconds ? ` (max ${p.maxDurationSeconds}s)` : '';
279
+ lines.push(`Duration: ${p.durationSeconds}s${maxNote}`);
280
+ }
281
+ else if (p.kind === 'image' && p.quantity > 1) {
282
+ lines.push(`Quantity: ${p.quantity} images`);
283
+ }
284
+ lines.push('');
285
+ lines.push(` ● Recommended ${p.recommended.model.padEnd(32)} ~${formatUsd(p.recommended.estimatedCostUsd)} ${p.recommended.rationale}`);
286
+ if (p.cheaper) {
287
+ lines.push(` ○ Cheaper ${p.cheaper.model.padEnd(32)} ~${formatUsd(p.cheaper.estimatedCostUsd)} ${p.cheaper.rationale}`);
288
+ }
289
+ if (p.premium) {
290
+ lines.push(` ○ Premium ${p.premium.model.padEnd(32)} ~${formatUsd(p.premium.estimatedCostUsd)} ${p.premium.rationale}`);
291
+ }
292
+ lines.push('');
293
+ lines.push(` (prices include the 5% gateway fee)`);
294
+ const options = [];
295
+ const recLabel = p.refinedPrompt
296
+ ? `Continue with refined prompt + ${p.recommended.model}`
297
+ : `Continue with ${p.recommended.model}`;
298
+ options.push({ id: 'recommended', label: recLabel });
299
+ if (p.cheaper)
300
+ options.push({ id: 'cheaper', label: `Use cheaper (${p.cheaper.model})` });
301
+ if (p.premium)
302
+ options.push({ id: 'premium', label: `Use premium (${p.premium.model})` });
303
+ if (p.refinedPrompt) {
304
+ options.push({ id: 'use-raw', label: `Use ORIGINAL prompt + ${p.recommended.model}` });
305
+ }
306
+ options.push({ id: 'cancel', label: 'Cancel (no charge)' });
307
+ return { question: lines.join('\n'), options };
308
+ }
309
+ function formatUsd(n) {
310
+ if (!Number.isFinite(n) || n <= 0)
311
+ return '$0.00';
312
+ if (n < 0.01)
313
+ return `$${n.toFixed(4)}`;
314
+ if (n < 1)
315
+ return `$${n.toFixed(3)}`;
316
+ return `$${n.toFixed(2)}`;
317
+ }
318
+ function combineSignals(signals) {
319
+ const ctrl = new AbortController();
320
+ for (const s of signals) {
321
+ if (s.aborted) {
322
+ ctrl.abort();
323
+ break;
324
+ }
325
+ s.addEventListener('abort', () => ctrl.abort(), { once: true });
326
+ }
327
+ return ctrl.signal;
328
+ }
@@ -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)')