Skip to content

Commit da359ca

Browse files
committed
Update the earnings tool to use new API structure
1 parent 0e5d805 commit da359ca

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

src/tools/finance/earnings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const getEarnings = new DynamicStructuredTool({
1818
func: async (input) => {
1919
const ticker = input.ticker.trim().toUpperCase();
2020
const { data, url } = await api.get('/earnings', { ticker }, { cacheable: true, ttlMs: TTL_24H });
21-
const period = Array.isArray(data?.earnings) ? data.earnings[0] : null;
22-
return formatToolResult(period || {}, [url]);
21+
const record = Array.isArray(data?.earnings) ? data.earnings[0] : null;
22+
return formatToolResult(record || {}, [url]);
2323
},
2424
});

src/tools/finance/formatters.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,17 @@ export function formatAnalystEstimates(data: unknown): string {
202202
export function formatEarnings(data: unknown): string {
203203
const d = (data && typeof data === 'object') ? data as Rec : {};
204204
if (Object.keys(d).length === 0) return 'No earnings data available.';
205-
const filings = Array.isArray(d.filings) ? d.filings as Rec[] : [];
206-
if (filings.length === 0) return 'No earnings data available.';
207-
// Prefer the 8-K announcement (has surprise/estimate data); fall back to first filing.
208-
const primary = filings.find((f) => f?.source_type === '8-K') ?? filings[0];
209-
const figures = ((primary.quarterly ?? primary.annual) && typeof (primary.quarterly ?? primary.annual) === 'object')
210-
? (primary.quarterly ?? primary.annual) as Rec
205+
// Flat shape: each entry IS one filing. data.earnings[0] (already unwrapped upstream)
206+
// lands on the most recent period's 8-K when present (sorted report_period DESC, filing_date ASC).
207+
const figures = ((d.quarterly ?? d.annual) && typeof (d.quarterly ?? d.annual) === 'object')
208+
? (d.quarterly ?? d.annual) as Rec
211209
: {};
212210
const ticker = (d.ticker as string)?.toUpperCase() ?? '';
213211
const lines: string[] = [];
214212
const header = `${ticker} Earnings — ${fmtDate(d.report_period)}${d.fiscal_period ? ` (${d.fiscal_period})` : ''}${d.currency ? ` [${d.currency}]` : ''}`;
215213
lines.push(header.trim());
216214
lines.push('');
217-
lines.push(`Source: ${primary.source_type ?? '—'} | Filed: ${String(primary.filing_date ?? '—').slice(0, 10)} | Accession: ${primary.accession_number ?? '—'}`);
215+
lines.push(`Source: ${d.source_type ?? '—'} | Filed: ${String(d.filing_date ?? '—').slice(0, 10)} | Accession: ${d.accession_number ?? '—'}`);
218216
if (figures.revenue !== undefined) lines.push(`Revenue: ${fmtNum(figures.revenue)}`);
219217
if (figures.net_income !== undefined) lines.push(`Net Income: ${fmtNum(figures.net_income)}`);
220218
const eps = figures.earnings_per_share ?? figures.eps;

0 commit comments

Comments
 (0)