Files
CatoandClaude Opus 5.5 12278862aa fix(ask): never answer with a tool call; give the model the site's numbers, production tables and jobs (#136)
Measured on 12 real questions (2026-10-02): 3 'answered' with a raw
'TOOL: …' line, tools ran with empty arguments, and answers lacked data
the site has.

- Final round: a text tool call is run once and the model told to write;
  TOOL/ARGS lines never reach the reader; a turn with no prose names the
  records read instead of a blank.
- Tool arguments accept the names models use (id, slug, q, search…).
- ai-kit 1.19.1: Gemini's parallel tool calls stay separate (they were
  glued into one call with unparseable arguments). Release-age exemption
  for our own packages is by name, not version.
- get_bottleneck carries key_numbers (dated, sourced series: e.g. 128 weeks
  transformer lead time) within its own budget.
- New tools: resource_production (USGS/EIA by country, synonyms like
  palladium -> PGM series), open_roles (live job board).
- listed_exposure adds listed makers one step downstream (EUV -> TSMC,
  Samsung, Intel) and names each ticker's exchange.
- Planner: jobs, resources and countries looked up before the model;
  'machines' etc. no longer pick a bottleneck alone.
- Prompt: answer first; figures with unit/date/link; news leads with the
  newest unreviewed leads; no relationships not in the results; absence is
  not evidence; off-topic gets a one-line redirect.
- loop.ts failure handling moved to answer.ts (file ceiling).

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 04:13:09 -07:00

52 lines
2.1 KiB
TypeScript

/** The contract every chat tool implements, and its argument coercions. */
import type { ToolEnv } from './ledger';
export type Args = Record<string, unknown>;
export const str = (v: unknown) => (typeof v === 'string' ? v.trim().slice(0, 200) : '');
export const num = (v: unknown, fallback: number, min: number, max: number) => {
const n = typeof v === 'number' ? v : typeof v === 'string' ? Number(v) : NaN;
return Number.isFinite(n) ? Math.min(Math.max(Math.round(n), min), max) : fallback;
};
export interface ChatTool {
name: string;
description: string;
parameters: Record<string, unknown>;
/** Short present-tense label shown to the reader while it runs. */
label: (args: Args) => string;
/** Whether this deployment can run it at all; a tool that cannot is not offered. */
available?: (env: ToolEnv) => boolean;
run: (args: Args, env: ToolEnv) => Promise<unknown>;
/** Characters its result may take; the registry default when absent. */
budget?: number;
}
/**
* The names small models actually use for a tool's arguments, observed live
* (2026-10-02): Gemini asked `get_bottleneck {"id": …}` and `search_corpus
* {"kind": …, "query": …}`; others write `q`, `slug`, `term`. Refusing them
* ran the tool empty ("Reading the record"). A declared parameter that is
* missing takes its value from the first alias present; nothing else changes.
*/
const ALIASES: Record<string, readonly string[]> = {
name: ['id', 'slug', 'bottleneck', 'company', 'entity', 'title', 'record', 'key'],
query: ['q', 'search', 'term', 'terms', 'keywords', 'text', 'topic', 'name'],
bottleneck: ['name', 'id', 'slug'],
company: ['name', 'id', 'slug', 'ticker'],
};
export function withAliases(parameters: Record<string, unknown>, args: Args): Args {
const declared = Object.keys(
((parameters as { properties?: Record<string, unknown> }).properties ?? {}) as object,
);
const out: Args = { ...args };
for (const key of declared) {
if (str(out[key])) continue;
const from = (ALIASES[key] ?? []).find(
(alias) => !declared.includes(alias) && str(args[alias]),
);
if (from) out[key] = args[from];
}
return out;
}