oculon 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +344 -2
- package/dist/agent.d.ts +47 -0
- package/dist/agent.js +104 -0
- package/dist/cases.d.ts +68 -0
- package/dist/cases.js +158 -0
- package/dist/cli/main.d.ts +12 -0
- package/dist/cli/main.js +1999 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +15 -0
- package/dist/core/context.d.ts +19 -0
- package/dist/core/context.js +19 -0
- package/dist/core/features.d.ts +22 -0
- package/dist/core/features.js +28 -0
- package/dist/core/graph.d.ts +38 -0
- package/dist/core/graph.js +81 -0
- package/dist/core/http.d.ts +42 -0
- package/dist/core/http.js +266 -0
- package/dist/core/keys.d.ts +8 -0
- package/dist/core/keys.js +22 -0
- package/dist/core/llm.d.ts +93 -0
- package/dist/core/llm.js +180 -0
- package/dist/core/merkle.d.ts +15 -0
- package/dist/core/merkle.js +46 -0
- package/dist/core/paths.d.ts +2 -0
- package/dist/core/paths.js +6 -0
- package/dist/core/png.d.ts +17 -0
- package/dist/core/png.js +101 -0
- package/dist/core/quota.d.ts +15 -0
- package/dist/core/quota.js +90 -0
- package/dist/core/stats.d.ts +24 -0
- package/dist/core/stats.js +67 -0
- package/dist/core/store.d.ts +20 -0
- package/dist/core/store.js +213 -0
- package/dist/core/text.d.ts +9 -0
- package/dist/core/text.js +30 -0
- package/dist/core/types.d.ts +38 -0
- package/dist/core/types.js +1 -0
- package/dist/index.d.ts +342 -0
- package/dist/index.js +495 -0
- package/dist/live.d.ts +53 -0
- package/dist/live.js +236 -0
- package/dist/mcp.d.ts +43 -0
- package/dist/mcp.js +651 -0
- package/dist/modules/airport.d.ts +60 -0
- package/dist/modules/airport.js +111 -0
- package/dist/modules/audit.d.ts +73 -0
- package/dist/modules/audit.js +211 -0
- package/dist/modules/bgp.d.ts +147 -0
- package/dist/modules/bgp.js +206 -0
- package/dist/modules/blackout.d.ts +41 -0
- package/dist/modules/blackout.js +98 -0
- package/dist/modules/brief.d.ts +26 -0
- package/dist/modules/brief.js +99 -0
- package/dist/modules/calendar.d.ts +27 -0
- package/dist/modules/calendar.js +145 -0
- package/dist/modules/calibrate.d.ts +46 -0
- package/dist/modules/calibrate.js +92 -0
- package/dist/modules/cloud.d.ts +32 -0
- package/dist/modules/cloud.js +94 -0
- package/dist/modules/condor.d.ts +66 -0
- package/dist/modules/condor.js +214 -0
- package/dist/modules/connect.d.ts +47 -0
- package/dist/modules/connect.js +114 -0
- package/dist/modules/contradictions.d.ts +27 -0
- package/dist/modules/contradictions.js +76 -0
- package/dist/modules/country.d.ts +82 -0
- package/dist/modules/country.js +136 -0
- package/dist/modules/cricket.d.ts +34 -0
- package/dist/modules/cricket.js +166 -0
- package/dist/modules/darkships.d.ts +219 -0
- package/dist/modules/darkships.js +400 -0
- package/dist/modules/defi.d.ts +237 -0
- package/dist/modules/defi.js +512 -0
- package/dist/modules/emailsec.d.ts +47 -0
- package/dist/modules/emailsec.js +146 -0
- package/dist/modules/exposure.d.ts +43 -0
- package/dist/modules/exposure.js +89 -0
- package/dist/modules/factcheck.d.ts +39 -0
- package/dist/modules/factcheck.js +127 -0
- package/dist/modules/falcon.d.ts +58 -0
- package/dist/modules/falcon.js +219 -0
- package/dist/modules/feedback.d.ts +24 -0
- package/dist/modules/feedback.js +37 -0
- package/dist/modules/fence.d.ts +52 -0
- package/dist/modules/fence.js +128 -0
- package/dist/modules/ferret.d.ts +47 -0
- package/dist/modules/ferret.js +220 -0
- package/dist/modules/filings.d.ts +86 -0
- package/dist/modules/filings.js +166 -0
- package/dist/modules/fires.d.ts +56 -0
- package/dist/modules/fires.js +150 -0
- package/dist/modules/hazards.d.ts +37 -0
- package/dist/modules/hazards.js +83 -0
- package/dist/modules/health.d.ts +24 -0
- package/dist/modules/health.js +82 -0
- package/dist/modules/heron.d.ts +68 -0
- package/dist/modules/heron.js +184 -0
- package/dist/modules/hound.d.ts +100 -0
- package/dist/modules/hound.js +301 -0
- package/dist/modules/impact.d.ts +39 -0
- package/dist/modules/impact.js +63 -0
- package/dist/modules/launches.d.ts +24 -0
- package/dist/modules/launches.js +61 -0
- package/dist/modules/listen.d.ts +48 -0
- package/dist/modules/listen.js +125 -0
- package/dist/modules/lynx.d.ts +43 -0
- package/dist/modules/lynx.js +127 -0
- package/dist/modules/magpie.d.ts +70 -0
- package/dist/modules/magpie.js +174 -0
- package/dist/modules/meerkat.d.ts +42 -0
- package/dist/modules/meerkat.js +293 -0
- package/dist/modules/monitor.d.ts +27 -0
- package/dist/modules/monitor.js +105 -0
- package/dist/modules/narratives.d.ts +33 -0
- package/dist/modules/narratives.js +136 -0
- package/dist/modules/owl.d.ts +71 -0
- package/dist/modules/owl.js +290 -0
- package/dist/modules/papers.d.ts +46 -0
- package/dist/modules/papers.js +135 -0
- package/dist/modules/personal.d.ts +54 -0
- package/dist/modules/personal.js +153 -0
- package/dist/modules/pulse.d.ts +19 -0
- package/dist/modules/pulse.js +59 -0
- package/dist/modules/raven.d.ts +80 -0
- package/dist/modules/raven.js +147 -0
- package/dist/modules/recall.d.ts +44 -0
- package/dist/modules/recall.js +123 -0
- package/dist/modules/regs.d.ts +88 -0
- package/dist/modules/regs.js +142 -0
- package/dist/modules/rewind.d.ts +22 -0
- package/dist/modules/rewind.js +68 -0
- package/dist/modules/sanctions.d.ts +62 -0
- package/dist/modules/sanctions.js +173 -0
- package/dist/modules/scam.d.ts +43 -0
- package/dist/modules/scam.js +111 -0
- package/dist/modules/scoop.d.ts +31 -0
- package/dist/modules/scoop.js +74 -0
- package/dist/modules/ships.d.ts +33 -0
- package/dist/modules/ships.js +113 -0
- package/dist/modules/sky.d.ts +47 -0
- package/dist/modules/sky.js +183 -0
- package/dist/modules/squawk.d.ts +46 -0
- package/dist/modules/squawk.js +106 -0
- package/dist/modules/tortoise.d.ts +40 -0
- package/dist/modules/tortoise.js +104 -0
- package/dist/modules/travel.d.ts +94 -0
- package/dist/modules/travel.js +241 -0
- package/dist/modules/urlcheck.d.ts +63 -0
- package/dist/modules/urlcheck.js +223 -0
- package/dist/modules/vulns.d.ts +142 -0
- package/dist/modules/vulns.js +276 -0
- package/dist/plugins.d.ts +41 -0
- package/dist/plugins.js +70 -0
- package/dist/report.d.ts +12 -0
- package/dist/report.js +90 -0
- package/dist/scheduler.d.ts +22 -0
- package/dist/scheduler.js +139 -0
- package/dist/sources/aircraft.d.ts +76 -0
- package/dist/sources/aircraft.js +107 -0
- package/dist/sources/airports.d.ts +28 -0
- package/dist/sources/airports.js +100 -0
- package/dist/sources/ais.d.ts +32 -0
- package/dist/sources/ais.js +111 -0
- package/dist/sources/apify.d.ts +28 -0
- package/dist/sources/apify.js +80 -0
- package/dist/sources/arxiv.d.ts +13 -0
- package/dist/sources/arxiv.js +27 -0
- package/dist/sources/certs.d.ts +12 -0
- package/dist/sources/certs.js +23 -0
- package/dist/sources/corp.d.ts +46 -0
- package/dist/sources/corp.js +60 -0
- package/dist/sources/earth.d.ts +39 -0
- package/dist/sources/earth.js +76 -0
- package/dist/sources/fda.d.ts +77 -0
- package/dist/sources/fda.js +121 -0
- package/dist/sources/finance.d.ts +181 -0
- package/dist/sources/finance.js +270 -0
- package/dist/sources/gdelt.d.ts +19 -0
- package/dist/sources/gdelt.js +33 -0
- package/dist/sources/github.d.ts +17 -0
- package/dist/sources/github.js +33 -0
- package/dist/sources/hackernews.d.ts +20 -0
- package/dist/sources/hackernews.js +37 -0
- package/dist/sources/labels.d.ts +21 -0
- package/dist/sources/labels.js +87 -0
- package/dist/sources/launch.d.ts +16 -0
- package/dist/sources/launch.js +41 -0
- package/dist/sources/ofac.d.ts +29 -0
- package/dist/sources/ofac.js +86 -0
- package/dist/sources/onchain.d.ts +77 -0
- package/dist/sources/onchain.js +189 -0
- package/dist/sources/opentimestamps.d.ts +44 -0
- package/dist/sources/opentimestamps.js +131 -0
- package/dist/sources/polymarket.d.ts +36 -0
- package/dist/sources/polymarket.js +70 -0
- package/dist/sources/predict.d.ts +18 -0
- package/dist/sources/predict.js +59 -0
- package/dist/sources/press.d.ts +30 -0
- package/dist/sources/press.js +111 -0
- package/dist/sources/resend.d.ts +14 -0
- package/dist/sources/resend.js +29 -0
- package/dist/sources/robinhood.d.ts +109 -0
- package/dist/sources/robinhood.js +129 -0
- package/dist/sources/sec.d.ts +67 -0
- package/dist/sources/sec.js +129 -0
- package/dist/sources/sentinel.d.ts +13 -0
- package/dist/sources/sentinel.js +63 -0
- package/dist/sources/signatures.d.ts +14 -0
- package/dist/sources/signatures.js +103 -0
- package/dist/sources/social.d.ts +22 -0
- package/dist/sources/social.js +56 -0
- package/dist/sources/space.d.ts +160 -0
- package/dist/sources/space.js +230 -0
- package/dist/sources/usgs.d.ts +17 -0
- package/dist/sources/usgs.js +24 -0
- package/dist/sources/wayback.d.ts +10 -0
- package/dist/sources/wayback.js +28 -0
- package/dist/sources/websec.d.ts +19 -0
- package/dist/sources/websec.js +40 -0
- package/dist/sources/wikidata.d.ts +11 -0
- package/dist/sources/wikidata.js +30 -0
- package/dist/sources/wikimedia.d.ts +28 -0
- package/dist/sources/wikimedia.js +52 -0
- package/dist/telegram.d.ts +104 -0
- package/dist/telegram.js +927 -0
- package/dist/web/assets/favicon-32.png +0 -0
- package/dist/web/assets/fx.js +178 -0
- package/dist/web/assets/icon-192.png +0 -0
- package/dist/web/assets/icon-512.png +0 -0
- package/dist/web/assets/icon-maskable-512.png +0 -0
- package/dist/web/assets/night-americas.jpg +0 -0
- package/dist/web/assets/night-emea.jpg +0 -0
- package/dist/web/assets/og.png +0 -0
- package/dist/web/assets/redsea-bm.jpg +0 -0
- package/dist/web/docs.html +263 -0
- package/dist/web/index.html +1828 -0
- package/dist/web/insights.d.ts +100 -0
- package/dist/web/insights.js +134 -0
- package/dist/web/landing.html +1143 -0
- package/dist/web/legal.html +176 -0
- package/dist/web/manifest.webmanifest +20 -0
- package/dist/web/mcp.html +104 -0
- package/dist/web/mcphttp.d.ts +10 -0
- package/dist/web/mcphttp.js +67 -0
- package/dist/web/openapi.d.ts +1 -0
- package/dist/web/openapi.js +51 -0
- package/dist/web/public.d.ts +45 -0
- package/dist/web/public.js +135 -0
- package/dist/web/server.d.ts +69 -0
- package/dist/web/server.js +750 -0
- package/dist/web/share.d.ts +35 -0
- package/dist/web/share.js +132 -0
- package/dist/web/sw.js +28 -0
- package/package.json +60 -4
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regulated-product trails (no keys):
|
|
3
|
+
* openFDA enforcement reports: drug, device and food recalls by recalling firm (api.fda.gov; 1,000 requests/day
|
|
4
|
+
* per IP without a key, 240/minute).
|
|
5
|
+
* ClinicalTrials.gov v2: trials a company sponsors or collaborates on, counted by status, plus recent terminations.
|
|
6
|
+
*/
|
|
7
|
+
import { type Ctx } from '../core/context.js';
|
|
8
|
+
import type { Provenance, Sourced } from '../core/types.js';
|
|
9
|
+
export type RecallCategory = 'drug' | 'device' | 'food';
|
|
10
|
+
export interface Recall {
|
|
11
|
+
category: RecallCategory;
|
|
12
|
+
recallNumber: string;
|
|
13
|
+
firm: string;
|
|
14
|
+
/** Class I: reasonable probability of serious harm or death. Class II: temporary or reversible harm. Class III: unlikely to cause harm. */
|
|
15
|
+
classification: string;
|
|
16
|
+
status: string;
|
|
17
|
+
product: string;
|
|
18
|
+
reason: string;
|
|
19
|
+
quantity: string;
|
|
20
|
+
distribution: string;
|
|
21
|
+
/** Dates as YYYY-MM-DD. */
|
|
22
|
+
initiated: string;
|
|
23
|
+
reported: string;
|
|
24
|
+
voluntary: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}
|
|
27
|
+
export interface FirmRecalls {
|
|
28
|
+
firm: string;
|
|
29
|
+
/** All enforcement reports matching the firm name, per category (openFDA total). */
|
|
30
|
+
totals: Record<RecallCategory, number>;
|
|
31
|
+
/** Newest first, up to 10 per category. */
|
|
32
|
+
recent: Recall[];
|
|
33
|
+
/** Class I recalls reported within the window. */
|
|
34
|
+
newClassI: Recall[];
|
|
35
|
+
}
|
|
36
|
+
export declare function enforcement(ctx: Ctx, category: RecallCategory, firm: string, limit?: number): Promise<Sourced<{
|
|
37
|
+
total: number;
|
|
38
|
+
recalls: Recall[];
|
|
39
|
+
}>>;
|
|
40
|
+
/** Drug, device and food recalls for a firm. Each recall must name the firm (all query words), to drop look-alike firms. */
|
|
41
|
+
export declare function recalls(ctx: Ctx, firm: string, opts?: {
|
|
42
|
+
days?: number;
|
|
43
|
+
match?: (recallingFirm: string) => boolean;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
data: FirmRecalls | null;
|
|
46
|
+
provenance: Provenance[];
|
|
47
|
+
}>;
|
|
48
|
+
export interface Trial {
|
|
49
|
+
nctId: string;
|
|
50
|
+
title: string;
|
|
51
|
+
status: string;
|
|
52
|
+
phases: string[];
|
|
53
|
+
leadSponsor: string;
|
|
54
|
+
conditions: string[];
|
|
55
|
+
enrollment: number | null;
|
|
56
|
+
whyStopped: string | null;
|
|
57
|
+
completionDate: string | null;
|
|
58
|
+
lastUpdate: string | null;
|
|
59
|
+
url: string;
|
|
60
|
+
}
|
|
61
|
+
export interface SponsorTrials {
|
|
62
|
+
sponsor: string;
|
|
63
|
+
counts: {
|
|
64
|
+
recruiting: number;
|
|
65
|
+
active: number;
|
|
66
|
+
completed: number;
|
|
67
|
+
terminated: number;
|
|
68
|
+
};
|
|
69
|
+
/** Terminated trials with a completion (stop) date inside the window, newest first. */
|
|
70
|
+
recentlyTerminated: Trial[];
|
|
71
|
+
}
|
|
72
|
+
/** Trials where the company is lead sponsor or collaborator (ClinicalTrials.gov query.spons). */
|
|
73
|
+
export declare function sponsorTrials(ctx: Ctx, sponsor: string, days?: number): Promise<{
|
|
74
|
+
data: SponsorTrials | null;
|
|
75
|
+
provenance: Provenance[];
|
|
76
|
+
}>;
|
|
77
|
+
export declare const isPhase3: (t: Trial) => boolean;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regulated-product trails (no keys):
|
|
3
|
+
* openFDA enforcement reports: drug, device and food recalls by recalling firm (api.fda.gov; 1,000 requests/day
|
|
4
|
+
* per IP without a key, 240/minute).
|
|
5
|
+
* ClinicalTrials.gov v2: trials a company sponsors or collaborates on, counted by status, plus recent terminations.
|
|
6
|
+
*/
|
|
7
|
+
import { DAY_MS, enc } from '../core/context.js';
|
|
8
|
+
const isoDay = (s) => (s && /^\d{8}$/.test(s) ? `${s.slice(0, 4)}-${s.slice(4, 6)}-${s.slice(6, 8)}` : (s ?? ''));
|
|
9
|
+
const compact = (s, n) => (s ?? '').replace(/\s+/g, ' ').trim().slice(0, n);
|
|
10
|
+
/** openFDA phrase search; quotes and other syntax characters in the name are dropped. */
|
|
11
|
+
const firmTerm = (firm) => `"${firm.replace(/["\\:()[\]{}+]/g, ' ').replace(/\s+/g, ' ').trim()}"`;
|
|
12
|
+
export async function enforcement(ctx, category, firm, limit = 10) {
|
|
13
|
+
const url = `https://api.fda.gov/${category}/enforcement.json?search=recalling_firm:${enc(firmTerm(firm))}&sort=report_date:desc&limit=${limit}`;
|
|
14
|
+
const r = await ctx.http.get(url, {
|
|
15
|
+
source: 'openfda',
|
|
16
|
+
ttlSec: 12 * 3600,
|
|
17
|
+
keep: (d) => ({
|
|
18
|
+
total: d.meta?.results?.total ?? d.results?.length ?? 0,
|
|
19
|
+
recalls: (d.results ?? []).map((x) => ({
|
|
20
|
+
category,
|
|
21
|
+
recallNumber: x.recall_number ?? '',
|
|
22
|
+
firm: x.recalling_firm ?? '',
|
|
23
|
+
classification: x.classification ?? '',
|
|
24
|
+
status: x.status ?? '',
|
|
25
|
+
product: compact(x.product_description, 240),
|
|
26
|
+
reason: compact(x.reason_for_recall, 300),
|
|
27
|
+
quantity: compact(x.product_quantity, 80),
|
|
28
|
+
distribution: compact(x.distribution_pattern, 160),
|
|
29
|
+
initiated: isoDay(x.recall_initiation_date),
|
|
30
|
+
reported: isoDay(x.report_date),
|
|
31
|
+
voluntary: x.voluntary_mandated ?? '',
|
|
32
|
+
// The openFDA record itself; FDA's enforcement-report site has no stable per-recall link.
|
|
33
|
+
url: `https://api.fda.gov/${category}/enforcement.json?search=recall_number:${enc(`"${x.recall_number ?? ''}"`)}`,
|
|
34
|
+
})),
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
// openFDA answers 404 "No matches found" when nothing matches; that is an empty result, not an outage.
|
|
38
|
+
if (!r.data && r.prov.note === 'HTTP 404')
|
|
39
|
+
return { data: { total: 0, recalls: [] }, prov: r.prov };
|
|
40
|
+
return r;
|
|
41
|
+
}
|
|
42
|
+
/** Drug, device and food recalls for a firm. Each recall must name the firm (all query words), to drop look-alike firms. */
|
|
43
|
+
export async function recalls(ctx, firm, opts = {}) {
|
|
44
|
+
const days = opts.days ?? 90;
|
|
45
|
+
const cats = ['drug', 'device', 'food'];
|
|
46
|
+
const rs = await Promise.all(cats.map((c) => enforcement(ctx, c, firm)));
|
|
47
|
+
const provenance = rs.map((r) => r.prov);
|
|
48
|
+
if (rs.every((r) => !r.data))
|
|
49
|
+
return { data: null, provenance };
|
|
50
|
+
const words = firm.toLowerCase().split(/[^a-z0-9]+/).filter((w) => w.length > 1);
|
|
51
|
+
const match = opts.match ?? ((f) => words.every((w) => new RegExp(`\\b${w}\\b`).test(f.toLowerCase())));
|
|
52
|
+
const totals = { drug: 0, device: 0, food: 0 };
|
|
53
|
+
const recent = [];
|
|
54
|
+
cats.forEach((c, i) => {
|
|
55
|
+
const d = rs[i].data;
|
|
56
|
+
if (!d)
|
|
57
|
+
return;
|
|
58
|
+
const mine = d.recalls.filter((x) => match(x.firm));
|
|
59
|
+
// The total counts every firm the phrase matched; only report it when the sample is all this firm.
|
|
60
|
+
totals[c] = mine.length === d.recalls.length ? d.total : mine.length;
|
|
61
|
+
recent.push(...mine);
|
|
62
|
+
});
|
|
63
|
+
recent.sort((a, b) => b.reported.localeCompare(a.reported));
|
|
64
|
+
const since = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
65
|
+
return { data: { firm, totals, recent, newClassI: recent.filter((x) => x.classification === 'Class I' && x.reported >= since) }, provenance };
|
|
66
|
+
}
|
|
67
|
+
const CT = 'https://clinicaltrials.gov/api/v2/studies';
|
|
68
|
+
const CT_FIELDS = 'NCTId,BriefTitle,OverallStatus,Phase,WhyStopped,CompletionDate,LastUpdatePostDate,LeadSponsorName,Condition,EnrollmentCount';
|
|
69
|
+
const toTrial = (s) => {
|
|
70
|
+
const p = s.protocolSection ?? {};
|
|
71
|
+
const id = p.identificationModule?.nctId ?? '';
|
|
72
|
+
return {
|
|
73
|
+
nctId: id,
|
|
74
|
+
title: p.identificationModule?.briefTitle ?? '',
|
|
75
|
+
status: p.statusModule?.overallStatus ?? '',
|
|
76
|
+
phases: p.designModule?.phases ?? [],
|
|
77
|
+
leadSponsor: p.sponsorCollaboratorsModule?.leadSponsor?.name ?? '',
|
|
78
|
+
conditions: (p.conditionsModule?.conditions ?? []).slice(0, 4),
|
|
79
|
+
enrollment: p.designModule?.enrollmentInfo?.count ?? null,
|
|
80
|
+
whyStopped: p.statusModule?.whyStopped ?? null,
|
|
81
|
+
completionDate: p.statusModule?.completionDateStruct?.date ?? null,
|
|
82
|
+
lastUpdate: p.statusModule?.lastUpdatePostDateStruct?.date ?? null,
|
|
83
|
+
url: `https://clinicaltrials.gov/study/${id}`,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
async function ctCount(ctx, sponsor, status) {
|
|
87
|
+
const r = await ctx.http.get(`${CT}?query.spons=${enc(sponsor)}&filter.overallStatus=${status}&pageSize=1&countTotal=true&fields=NCTId`, {
|
|
88
|
+
source: 'clinicaltrials',
|
|
89
|
+
ttlSec: 24 * 3600,
|
|
90
|
+
keep: (d) => ({ totalCount: d.totalCount ?? 0 }),
|
|
91
|
+
});
|
|
92
|
+
return { data: r.data ? (r.data.totalCount ?? 0) : null, prov: r.prov };
|
|
93
|
+
}
|
|
94
|
+
/** Trials where the company is lead sponsor or collaborator (ClinicalTrials.gov query.spons). */
|
|
95
|
+
export async function sponsorTrials(ctx, sponsor, days = 180) {
|
|
96
|
+
const [rec, act, done, term] = await Promise.all([
|
|
97
|
+
ctCount(ctx, sponsor, 'RECRUITING'),
|
|
98
|
+
ctCount(ctx, sponsor, 'ACTIVE_NOT_RECRUITING'),
|
|
99
|
+
ctCount(ctx, sponsor, 'COMPLETED'),
|
|
100
|
+
ctx.http.get(`${CT}?query.spons=${enc(sponsor)}&filter.overallStatus=TERMINATED&pageSize=20&countTotal=true&sort=CompletionDate:desc&fields=${CT_FIELDS}`, {
|
|
101
|
+
source: 'clinicaltrials',
|
|
102
|
+
ttlSec: 12 * 3600,
|
|
103
|
+
}),
|
|
104
|
+
]);
|
|
105
|
+
const provenance = [rec.prov, act.prov, done.prov, term.prov];
|
|
106
|
+
if (rec.data === null && act.data === null && done.data === null && !term.data)
|
|
107
|
+
return { data: null, provenance };
|
|
108
|
+
const today = ctx.now().toISOString().slice(0, 10);
|
|
109
|
+
const since = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
110
|
+
// CompletionDate may be "2026-10" (month only); compare as strings, which still orders correctly.
|
|
111
|
+
const recentlyTerminated = (term.data?.studies ?? []).map(toTrial).filter((t) => t.completionDate && t.completionDate >= since.slice(0, t.completionDate.length) && t.completionDate <= today);
|
|
112
|
+
return {
|
|
113
|
+
data: {
|
|
114
|
+
sponsor,
|
|
115
|
+
counts: { recruiting: rec.data ?? 0, active: act.data ?? 0, completed: done.data ?? 0, terminated: term.data?.totalCount ?? 0 },
|
|
116
|
+
recentlyTerminated,
|
|
117
|
+
},
|
|
118
|
+
provenance,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export const isPhase3 = (t) => t.phases.includes('PHASE3');
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** Markets: Finnhub (stocks, insiders, company news), FRED (macro), CoinGecko (crypto). */
|
|
2
|
+
import { type Ctx } from '../core/context.js';
|
|
3
|
+
import type { Sourced } from '../core/types.js';
|
|
4
|
+
export interface Quote {
|
|
5
|
+
symbol: string;
|
|
6
|
+
price: number;
|
|
7
|
+
changePct: number;
|
|
8
|
+
high: number;
|
|
9
|
+
low: number;
|
|
10
|
+
prevClose: number;
|
|
11
|
+
}
|
|
12
|
+
export interface CompanyProfile {
|
|
13
|
+
name: string;
|
|
14
|
+
ticker: string;
|
|
15
|
+
exchange: string;
|
|
16
|
+
industry: string;
|
|
17
|
+
marketCapM: number;
|
|
18
|
+
weburl: string;
|
|
19
|
+
country: string;
|
|
20
|
+
}
|
|
21
|
+
export interface Insider {
|
|
22
|
+
name: string;
|
|
23
|
+
change: number;
|
|
24
|
+
shares: number;
|
|
25
|
+
date: string;
|
|
26
|
+
code: string;
|
|
27
|
+
price: number;
|
|
28
|
+
}
|
|
29
|
+
/** Best US common-stock symbol for a company name or ticker. */
|
|
30
|
+
export declare function findSymbol(ctx: Ctx, q: string): Promise<Sourced<string>>;
|
|
31
|
+
export declare function quote(ctx: Ctx, symbol: string): Promise<Sourced<Quote>>;
|
|
32
|
+
export declare function profile(ctx: Ctx, symbol: string): Promise<Sourced<CompanyProfile>>;
|
|
33
|
+
export declare function insiders(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<Insider[]>>;
|
|
34
|
+
export interface NewsItem {
|
|
35
|
+
title: string;
|
|
36
|
+
url: string;
|
|
37
|
+
date: string;
|
|
38
|
+
source: string;
|
|
39
|
+
}
|
|
40
|
+
export declare function companyNews(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<NewsItem[]>>;
|
|
41
|
+
export interface Recommendation {
|
|
42
|
+
period: string;
|
|
43
|
+
strongBuy: number;
|
|
44
|
+
buy: number;
|
|
45
|
+
hold: number;
|
|
46
|
+
sell: number;
|
|
47
|
+
strongSell: number;
|
|
48
|
+
}
|
|
49
|
+
/** Monthly analyst recommendation counts, newest first. */
|
|
50
|
+
export declare function recommendations(ctx: Ctx, symbol: string): Promise<Sourced<Recommendation[]>>;
|
|
51
|
+
/** Share of bullish ratings (strong buy + buy) in one month, 0-1. */
|
|
52
|
+
export declare const bullishShare: (r: Recommendation) => number | null;
|
|
53
|
+
export interface InsiderSentimentMonth {
|
|
54
|
+
/** YYYY-MM. */
|
|
55
|
+
month: string;
|
|
56
|
+
/** Net shares bought (+) or sold (-) by insiders. */
|
|
57
|
+
change: number;
|
|
58
|
+
/** Monthly share purchase ratio, -100 (all selling) to +100 (all buying). */
|
|
59
|
+
mspr: number;
|
|
60
|
+
}
|
|
61
|
+
export declare function insiderSentiment(ctx: Ctx, symbol: string, months?: number): Promise<Sourced<InsiderSentimentMonth[]>>;
|
|
62
|
+
/** Companies Finnhub groups with this one (same industry), excluding itself. */
|
|
63
|
+
export declare function peers(ctx: Ctx, symbol: string): Promise<Sourced<string[]>>;
|
|
64
|
+
export interface Ipo {
|
|
65
|
+
date: string;
|
|
66
|
+
symbol: string;
|
|
67
|
+
name: string;
|
|
68
|
+
exchange: string;
|
|
69
|
+
/** Single price or range, as Finnhub gives it, e.g. "10.00" or "17.00-19.00". */
|
|
70
|
+
price: string;
|
|
71
|
+
shares: number | null;
|
|
72
|
+
valueUsd: number | null;
|
|
73
|
+
status: string;
|
|
74
|
+
}
|
|
75
|
+
export declare function ipoCalendar(ctx: Ctx, from: string, to: string): Promise<Sourced<Ipo[]>>;
|
|
76
|
+
/** A compact subset of Finnhub's ~130 basic-financial metrics (the raw response is ~250 KB of history). */
|
|
77
|
+
export interface KeyMetrics {
|
|
78
|
+
peTTM: number | null;
|
|
79
|
+
psTTM: number | null;
|
|
80
|
+
pb: number | null;
|
|
81
|
+
epsTTM: number | null;
|
|
82
|
+
revenueGrowthTTMYoy: number | null;
|
|
83
|
+
epsGrowthTTMYoy: number | null;
|
|
84
|
+
grossMarginTTM: number | null;
|
|
85
|
+
netMarginTTM: number | null;
|
|
86
|
+
roeTTM: number | null;
|
|
87
|
+
debtToEquity: number | null;
|
|
88
|
+
currentRatio: number | null;
|
|
89
|
+
dividendYield: number | null;
|
|
90
|
+
beta: number | null;
|
|
91
|
+
high52w: number | null;
|
|
92
|
+
low52w: number | null;
|
|
93
|
+
return52w: number | null;
|
|
94
|
+
marketCapM: number | null;
|
|
95
|
+
}
|
|
96
|
+
export declare function metrics(ctx: Ctx, symbol: string): Promise<Sourced<KeyMetrics>>;
|
|
97
|
+
export interface EarningsSurprise {
|
|
98
|
+
period: string;
|
|
99
|
+
actual: number | null;
|
|
100
|
+
estimate: number | null;
|
|
101
|
+
surprisePct: number | null;
|
|
102
|
+
}
|
|
103
|
+
/** Last four quarters of reported EPS against the consensus estimate. */
|
|
104
|
+
export declare function earningsSurprises(ctx: Ctx, symbol: string): Promise<Sourced<EarningsSurprise[]>>;
|
|
105
|
+
export interface Lobbying {
|
|
106
|
+
/** Sum of reported lobbying income/expenses over the window (USD). */
|
|
107
|
+
totalUsd: number;
|
|
108
|
+
filings: number;
|
|
109
|
+
byPeriod: {
|
|
110
|
+
period: string;
|
|
111
|
+
usd: number;
|
|
112
|
+
}[];
|
|
113
|
+
latestUrl: string | null;
|
|
114
|
+
}
|
|
115
|
+
/** US Senate LDA lobbying disclosures linked to the company. */
|
|
116
|
+
export declare function lobbying(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<Lobbying>>;
|
|
117
|
+
export interface GovContracts {
|
|
118
|
+
/** Distinct awards with an action in the window (latest action per award). */
|
|
119
|
+
awards: number;
|
|
120
|
+
/** Cumulative obligated value of those awards (multi-year contracts count in full), not money spent in the window. */
|
|
121
|
+
totalUsd: number;
|
|
122
|
+
topAgencies: {
|
|
123
|
+
agency: string;
|
|
124
|
+
usd: number;
|
|
125
|
+
}[];
|
|
126
|
+
latest: {
|
|
127
|
+
date: string;
|
|
128
|
+
agency: string;
|
|
129
|
+
usd: number;
|
|
130
|
+
description: string;
|
|
131
|
+
url: string;
|
|
132
|
+
}[];
|
|
133
|
+
}
|
|
134
|
+
/** US federal contract awards (USASpending.gov) to the company. The raw response can exceed 1 MB, so it is summarized. */
|
|
135
|
+
export declare function govContracts(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<GovContracts>>;
|
|
136
|
+
export interface VisaFilings {
|
|
137
|
+
/** Finnhub returns at most 500 rows, so 500 means "500 or more". */
|
|
138
|
+
applications: number;
|
|
139
|
+
byClass: Record<string, number>;
|
|
140
|
+
topTitles: {
|
|
141
|
+
title: string;
|
|
142
|
+
count: number;
|
|
143
|
+
}[];
|
|
144
|
+
/** Median lower bound of the offered annual wage (USD). */
|
|
145
|
+
medianWageFrom: number | null;
|
|
146
|
+
}
|
|
147
|
+
/** H-1B / PERM labor certification filings (US DOL); a proxy for which roles a company hires from abroad. */
|
|
148
|
+
export declare function visaApplications(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<VisaFilings>>;
|
|
149
|
+
export interface Patents {
|
|
150
|
+
/** Finnhub returns at most 250 rows, so 250 means "250 or more". */
|
|
151
|
+
count: number;
|
|
152
|
+
latest: {
|
|
153
|
+
date: string;
|
|
154
|
+
title: string;
|
|
155
|
+
number: string;
|
|
156
|
+
url: string;
|
|
157
|
+
}[];
|
|
158
|
+
}
|
|
159
|
+
/** USPTO patent applications and grants filed by the company. */
|
|
160
|
+
export declare function patents(ctx: Ctx, symbol: string, days?: number): Promise<Sourced<Patents>>;
|
|
161
|
+
export interface FredPoint {
|
|
162
|
+
date: string;
|
|
163
|
+
value: number;
|
|
164
|
+
}
|
|
165
|
+
export declare function fredSeries(ctx: Ctx, id: string, limit?: number): Promise<Sourced<FredPoint[]>>;
|
|
166
|
+
export interface TrendingCoin {
|
|
167
|
+
id: string;
|
|
168
|
+
name: string;
|
|
169
|
+
symbol: string;
|
|
170
|
+
rank: number | null;
|
|
171
|
+
change24h: number | null;
|
|
172
|
+
}
|
|
173
|
+
export declare function trendingCoins(ctx: Ctx): Promise<Sourced<TrendingCoin[]>>;
|
|
174
|
+
export declare function coinByContract(ctx: Ctx, platform: string, address: string): Promise<Sourced<{
|
|
175
|
+
id: string;
|
|
176
|
+
name: string;
|
|
177
|
+
symbol: string;
|
|
178
|
+
priceUsd: number | null;
|
|
179
|
+
marketCapUsd: number | null;
|
|
180
|
+
change24h: number | null;
|
|
181
|
+
}>>;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/** Markets: Finnhub (stocks, insiders, company news), FRED (macro), CoinGecko (crypto). */
|
|
2
|
+
import { DAY_MS, enc } from '../core/context.js';
|
|
3
|
+
import { key, noKey } from '../core/keys.js';
|
|
4
|
+
const FH = 'https://finnhub.io/api/v1';
|
|
5
|
+
const fh = (ctx, path, ttlSec) => {
|
|
6
|
+
const k = key('FINNHUB_API_KEY');
|
|
7
|
+
if (!k)
|
|
8
|
+
return Promise.resolve(noKey('finnhub', 'FINNHUB_API_KEY'));
|
|
9
|
+
return ctx.http.get(`${FH}${path}${path.includes('?') ? '&' : '?'}token=${k}`, { source: 'finnhub', ttlSec });
|
|
10
|
+
};
|
|
11
|
+
/** Best US common-stock symbol for a company name or ticker. */
|
|
12
|
+
export async function findSymbol(ctx, q) {
|
|
13
|
+
const r = await fh(ctx, `/search?q=${enc(q)}`, 7 * 86_400);
|
|
14
|
+
const hits = r.data?.result?.filter((x) => x.type === 'Common Stock' && !x.symbol.includes('.')) ?? [];
|
|
15
|
+
const exact = hits.find((x) => x.symbol.toLowerCase() === q.toLowerCase());
|
|
16
|
+
return { data: (exact ?? hits[0])?.symbol ?? null, prov: r.prov };
|
|
17
|
+
}
|
|
18
|
+
export async function quote(ctx, symbol) {
|
|
19
|
+
const r = await fh(ctx, `/quote?symbol=${enc(symbol)}`, 300);
|
|
20
|
+
return { data: r.data && r.data.c ? { symbol, price: r.data.c, changePct: r.data.dp, high: r.data.h, low: r.data.l, prevClose: r.data.pc } : null, prov: r.prov };
|
|
21
|
+
}
|
|
22
|
+
export async function profile(ctx, symbol) {
|
|
23
|
+
const r = await fh(ctx, `/stock/profile2?symbol=${enc(symbol)}`, 7 * 86_400);
|
|
24
|
+
const d = r.data;
|
|
25
|
+
return {
|
|
26
|
+
data: d?.name ? { name: d.name, ticker: d.ticker ?? symbol, exchange: d.exchange ?? '', industry: d.finnhubIndustry ?? '', marketCapM: d.marketCapitalization ?? 0, weburl: d.weburl ?? '', country: d.country ?? '' } : null,
|
|
27
|
+
prov: r.prov,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export async function insiders(ctx, symbol, days = 30) {
|
|
31
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
32
|
+
const r = await fh(ctx, `/stock/insider-transactions?symbol=${enc(symbol)}&from=${from}`, 6 * 3600);
|
|
33
|
+
return { data: r.data?.data?.map((x) => ({ name: x.name, change: x.change, shares: x.share, date: x.transactionDate, code: x.transactionCode, price: x.transactionPrice })) ?? null, prov: r.prov };
|
|
34
|
+
}
|
|
35
|
+
export async function companyNews(ctx, symbol, days = 7) {
|
|
36
|
+
const to = ctx.now().toISOString().slice(0, 10);
|
|
37
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
38
|
+
const r = await fh(ctx, `/company-news?symbol=${enc(symbol)}&from=${from}&to=${to}`, 1800);
|
|
39
|
+
return { data: r.data?.map((n) => ({ title: n.headline, url: n.url, date: new Date(n.datetime * 1000).toISOString(), source: n.source })) ?? null, prov: r.prov };
|
|
40
|
+
}
|
|
41
|
+
/** Monthly analyst recommendation counts, newest first. */
|
|
42
|
+
export async function recommendations(ctx, symbol) {
|
|
43
|
+
const r = await fh(ctx, `/stock/recommendation?symbol=${enc(symbol)}`, 12 * 3600);
|
|
44
|
+
return {
|
|
45
|
+
data: Array.isArray(r.data) ? r.data.map((x) => ({ period: x.period, strongBuy: x.strongBuy, buy: x.buy, hold: x.hold, sell: x.sell, strongSell: x.strongSell })).sort((a, b) => b.period.localeCompare(a.period)) : null,
|
|
46
|
+
prov: r.prov,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Share of bullish ratings (strong buy + buy) in one month, 0-1. */
|
|
50
|
+
export const bullishShare = (r) => {
|
|
51
|
+
const n = r.strongBuy + r.buy + r.hold + r.sell + r.strongSell;
|
|
52
|
+
return n ? (r.strongBuy + r.buy) / n : null;
|
|
53
|
+
};
|
|
54
|
+
export async function insiderSentiment(ctx, symbol, months = 12) {
|
|
55
|
+
const now = ctx.now();
|
|
56
|
+
const from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - months, 1)).toISOString().slice(0, 10);
|
|
57
|
+
const r = await fh(ctx, `/stock/insider-sentiment?symbol=${enc(symbol)}&from=${from}&to=${now.toISOString().slice(0, 10)}`, 12 * 3600);
|
|
58
|
+
return {
|
|
59
|
+
data: r.data?.data?.map((x) => ({ month: `${x.year}-${String(x.month).padStart(2, '0')}`, change: x.change, mspr: Math.round(x.mspr * 10) / 10 })).sort((a, b) => b.month.localeCompare(a.month)) ?? null,
|
|
60
|
+
prov: r.prov,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Companies Finnhub groups with this one (same industry), excluding itself. */
|
|
64
|
+
export async function peers(ctx, symbol) {
|
|
65
|
+
const r = await fh(ctx, `/stock/peers?symbol=${enc(symbol)}`, 7 * 86_400);
|
|
66
|
+
return { data: Array.isArray(r.data) ? r.data.filter((s) => s.toUpperCase() !== symbol.toUpperCase()) : null, prov: r.prov };
|
|
67
|
+
}
|
|
68
|
+
export async function ipoCalendar(ctx, from, to) {
|
|
69
|
+
const r = await fh(ctx, `/calendar/ipo?from=${from}&to=${to}`, 6 * 3600);
|
|
70
|
+
return {
|
|
71
|
+
data: r.data?.ipoCalendar?.map((x) => ({ date: x.date, symbol: x.symbol ?? '', name: x.name, exchange: x.exchange ?? '', price: x.price ?? '', shares: x.numberOfShares ?? null, valueUsd: x.totalSharesValue ?? null, status: x.status ?? '' })) ?? null,
|
|
72
|
+
prov: r.prov,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export async function metrics(ctx, symbol) {
|
|
76
|
+
const k = key('FINNHUB_API_KEY');
|
|
77
|
+
if (!k)
|
|
78
|
+
return noKey('finnhub', 'FINNHUB_API_KEY');
|
|
79
|
+
const n = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : null);
|
|
80
|
+
const r = await ctx.http.get(`${FH}/stock/metric?symbol=${enc(symbol)}&metric=all&token=${k}`, {
|
|
81
|
+
source: 'finnhub',
|
|
82
|
+
ttlSec: 24 * 3600,
|
|
83
|
+
keep: (d) => {
|
|
84
|
+
const m = d.metric;
|
|
85
|
+
if (!m || !Object.keys(m).length)
|
|
86
|
+
return null;
|
|
87
|
+
return {
|
|
88
|
+
peTTM: n(m.peTTM ?? m.peBasicExclExtraTTM),
|
|
89
|
+
psTTM: n(m.psTTM),
|
|
90
|
+
pb: n(m.pb ?? m.pbQuarterly),
|
|
91
|
+
epsTTM: n(m.epsTTM ?? m.epsBasicExclExtraItemsTTM),
|
|
92
|
+
revenueGrowthTTMYoy: n(m.revenueGrowthTTMYoy),
|
|
93
|
+
epsGrowthTTMYoy: n(m.epsGrowthTTMYoy),
|
|
94
|
+
grossMarginTTM: n(m.grossMarginTTM),
|
|
95
|
+
netMarginTTM: n(m.netProfitMarginTTM),
|
|
96
|
+
roeTTM: n(m.roeTTM),
|
|
97
|
+
debtToEquity: n(m['totalDebt/totalEquityQuarterly'] ?? m['totalDebt/totalEquityAnnual']),
|
|
98
|
+
currentRatio: n(m.currentRatioQuarterly ?? m.currentRatioAnnual),
|
|
99
|
+
dividendYield: n(m.currentDividendYieldTTM ?? m.dividendYieldIndicatedAnnual),
|
|
100
|
+
beta: n(m.beta),
|
|
101
|
+
high52w: n(m['52WeekHigh']),
|
|
102
|
+
low52w: n(m['52WeekLow']),
|
|
103
|
+
return52w: n(m['52WeekPriceReturnDaily']),
|
|
104
|
+
marketCapM: n(m.marketCapitalization),
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
return { data: r.data ?? null, prov: r.prov };
|
|
109
|
+
}
|
|
110
|
+
/** Last four quarters of reported EPS against the consensus estimate. */
|
|
111
|
+
export async function earningsSurprises(ctx, symbol) {
|
|
112
|
+
const r = await fh(ctx, `/stock/earnings?symbol=${enc(symbol)}`, 24 * 3600);
|
|
113
|
+
return { data: Array.isArray(r.data) ? r.data.map((x) => ({ period: x.period, actual: x.actual, estimate: x.estimate, surprisePct: x.surprisePercent })) : null, prov: r.prov };
|
|
114
|
+
}
|
|
115
|
+
/** US Senate LDA lobbying disclosures linked to the company. */
|
|
116
|
+
export async function lobbying(ctx, symbol, days = 730) {
|
|
117
|
+
const k = key('FINNHUB_API_KEY');
|
|
118
|
+
if (!k)
|
|
119
|
+
return noKey('finnhub', 'FINNHUB_API_KEY');
|
|
120
|
+
const to = ctx.now().toISOString().slice(0, 10);
|
|
121
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
122
|
+
const r = await ctx.http.get(`${FH}/stock/lobbying?symbol=${enc(symbol)}&from=${from}&to=${to}&token=${k}`, {
|
|
123
|
+
source: 'finnhub',
|
|
124
|
+
ttlSec: 7 * 86_400,
|
|
125
|
+
keep: (d) => {
|
|
126
|
+
const rows = d.data ?? [];
|
|
127
|
+
if (!rows.length)
|
|
128
|
+
return null;
|
|
129
|
+
const per = new Map();
|
|
130
|
+
for (const x of rows) {
|
|
131
|
+
const p = `${x.year}-${x.period}`;
|
|
132
|
+
per.set(p, (per.get(p) ?? 0) + (x.income ?? 0) + (x.expenses ?? 0));
|
|
133
|
+
}
|
|
134
|
+
const byPeriod = [...per].map(([period, usd]) => ({ period, usd })).sort((a, b) => b.period.localeCompare(a.period));
|
|
135
|
+
const latest = [...rows].sort((a, b) => `${b.year}-${b.period}`.localeCompare(`${a.year}-${a.period}`))[0];
|
|
136
|
+
return { totalUsd: byPeriod.reduce((a, b) => a + b.usd, 0), filings: rows.length, byPeriod, latestUrl: latest?.documentUrl || null };
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
return { data: r.data ?? null, prov: r.prov };
|
|
140
|
+
}
|
|
141
|
+
/** US federal contract awards (USASpending.gov) to the company. The raw response can exceed 1 MB, so it is summarized. */
|
|
142
|
+
export async function govContracts(ctx, symbol, days = 365) {
|
|
143
|
+
const k = key('FINNHUB_API_KEY');
|
|
144
|
+
if (!k)
|
|
145
|
+
return noKey('finnhub', 'FINNHUB_API_KEY');
|
|
146
|
+
const to = ctx.now().toISOString().slice(0, 10);
|
|
147
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
148
|
+
const r = await ctx.http.get(`${FH}/stock/usa-spending?symbol=${enc(symbol)}&from=${from}&to=${to}&token=${k}`, {
|
|
149
|
+
source: 'finnhub',
|
|
150
|
+
ttlSec: 7 * 86_400,
|
|
151
|
+
timeoutMs: 30_000,
|
|
152
|
+
keep: (d) => {
|
|
153
|
+
// Each row is a contract action; obligated amounts are cumulative per award, so keep the latest action per award.
|
|
154
|
+
const byAward = new Map();
|
|
155
|
+
for (const x of d.data ?? []) {
|
|
156
|
+
const k = x.permalink || `${x.awardingAgencyName}|${x.awardDescription ?? ''}|${x.actionDate}`;
|
|
157
|
+
const prev = byAward.get(k);
|
|
158
|
+
if (!prev || x.actionDate > prev.actionDate)
|
|
159
|
+
byAward.set(k, x);
|
|
160
|
+
}
|
|
161
|
+
const rows = [...byAward.values()];
|
|
162
|
+
if (!rows.length)
|
|
163
|
+
return null;
|
|
164
|
+
const val = (x) => x.obligatedAmount ?? x.totalValue ?? 0;
|
|
165
|
+
const ag = new Map();
|
|
166
|
+
for (const x of rows)
|
|
167
|
+
ag.set(x.awardingAgencyName, (ag.get(x.awardingAgencyName) ?? 0) + val(x));
|
|
168
|
+
return {
|
|
169
|
+
awards: rows.length,
|
|
170
|
+
totalUsd: rows.reduce((a, x) => a + val(x), 0),
|
|
171
|
+
topAgencies: [...ag].map(([agency, usd]) => ({ agency, usd })).sort((a, b) => b.usd - a.usd).slice(0, 5),
|
|
172
|
+
latest: [...rows]
|
|
173
|
+
.sort((a, b) => b.actionDate.localeCompare(a.actionDate))
|
|
174
|
+
.slice(0, 5)
|
|
175
|
+
.map((x) => ({ date: x.actionDate, agency: x.awardingAgencyName, usd: val(x), description: (x.awardDescription ?? '').slice(0, 200), url: x.permalink ?? '' })),
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
return { data: r.data ?? null, prov: r.prov };
|
|
180
|
+
}
|
|
181
|
+
/** H-1B / PERM labor certification filings (US DOL); a proxy for which roles a company hires from abroad. */
|
|
182
|
+
export async function visaApplications(ctx, symbol, days = 365) {
|
|
183
|
+
const k = key('FINNHUB_API_KEY');
|
|
184
|
+
if (!k)
|
|
185
|
+
return noKey('finnhub', 'FINNHUB_API_KEY');
|
|
186
|
+
const to = ctx.now().toISOString().slice(0, 10);
|
|
187
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
188
|
+
const r = await ctx.http.get(`${FH}/stock/visa-application?symbol=${enc(symbol)}&from=${from}&to=${to}&token=${k}`, {
|
|
189
|
+
source: 'finnhub',
|
|
190
|
+
ttlSec: 7 * 86_400,
|
|
191
|
+
timeoutMs: 30_000,
|
|
192
|
+
keep: (d) => {
|
|
193
|
+
const rows = d.data ?? [];
|
|
194
|
+
if (!rows.length)
|
|
195
|
+
return null;
|
|
196
|
+
const byClass = {};
|
|
197
|
+
const titles = new Map();
|
|
198
|
+
for (const x of rows) {
|
|
199
|
+
byClass[x.visaClass || 'unknown'] = (byClass[x.visaClass || 'unknown'] ?? 0) + 1;
|
|
200
|
+
const t = (x.jobTitle ?? '').trim();
|
|
201
|
+
if (t)
|
|
202
|
+
titles.set(t, (titles.get(t) ?? 0) + 1);
|
|
203
|
+
}
|
|
204
|
+
const wages = rows
|
|
205
|
+
.filter((x) => !x.wageUnitOfPay || x.wageUnitOfPay === 'Year')
|
|
206
|
+
.map((x) => x.wageRangeFrom).filter((w) => typeof w === 'number' && w > 0).sort((a, b) => a - b);
|
|
207
|
+
return {
|
|
208
|
+
applications: rows.length,
|
|
209
|
+
byClass,
|
|
210
|
+
topTitles: [...titles].map(([title, count]) => ({ title, count })).sort((a, b) => b.count - a.count).slice(0, 8),
|
|
211
|
+
medianWageFrom: wages.length ? wages[Math.floor(wages.length / 2)] : null,
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
return { data: r.data ?? null, prov: r.prov };
|
|
216
|
+
}
|
|
217
|
+
/** USPTO patent applications and grants filed by the company. */
|
|
218
|
+
export async function patents(ctx, symbol, days = 365) {
|
|
219
|
+
const k = key('FINNHUB_API_KEY');
|
|
220
|
+
if (!k)
|
|
221
|
+
return noKey('finnhub', 'FINNHUB_API_KEY');
|
|
222
|
+
const to = ctx.now().toISOString().slice(0, 10);
|
|
223
|
+
const from = new Date(ctx.now().getTime() - days * DAY_MS).toISOString().slice(0, 10);
|
|
224
|
+
const r = await ctx.http.get(`${FH}/stock/uspto-patent?symbol=${enc(symbol)}&from=${from}&to=${to}&token=${k}`, {
|
|
225
|
+
source: 'finnhub',
|
|
226
|
+
ttlSec: 7 * 86_400,
|
|
227
|
+
timeoutMs: 30_000,
|
|
228
|
+
keep: (d) => {
|
|
229
|
+
const rows = d.data ?? [];
|
|
230
|
+
if (!rows.length)
|
|
231
|
+
return null;
|
|
232
|
+
return {
|
|
233
|
+
count: rows.length,
|
|
234
|
+
latest: [...rows]
|
|
235
|
+
.sort((a, b) => (b.publicationDate || b.filingDate).localeCompare(a.publicationDate || a.filingDate))
|
|
236
|
+
.slice(0, 5)
|
|
237
|
+
.map((x) => ({ date: (x.publicationDate || x.filingDate || '').slice(0, 10), title: x.description, number: x.patentNumber || x.applicationNumber, url: x.url ?? '' })),
|
|
238
|
+
};
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
return { data: r.data ?? null, prov: r.prov };
|
|
242
|
+
}
|
|
243
|
+
export async function fredSeries(ctx, id, limit = 24) {
|
|
244
|
+
const k = key('FRED_API_KEY');
|
|
245
|
+
if (!k)
|
|
246
|
+
return noKey('fred', 'FRED_API_KEY');
|
|
247
|
+
const r = await ctx.http.get(`https://api.stlouisfed.org/fred/series/observations?series_id=${enc(id)}&file_type=json&sort_order=desc&limit=${limit}&api_key=${k}`, { source: 'fred', ttlSec: 6 * 3600 });
|
|
248
|
+
return { data: r.data?.observations?.filter((o) => o.value !== '.').map((o) => ({ date: o.date, value: Number(o.value) })) ?? null, prov: r.prov };
|
|
249
|
+
}
|
|
250
|
+
const cgHeaders = () => {
|
|
251
|
+
const k = key('COINGECKO_API_KEY');
|
|
252
|
+
return k ? { 'x-cg-demo-api-key': k } : undefined;
|
|
253
|
+
};
|
|
254
|
+
export async function trendingCoins(ctx) {
|
|
255
|
+
const h = cgHeaders();
|
|
256
|
+
const r = await ctx.http.get('https://api.coingecko.com/api/v3/search/trending', { source: 'coingecko', ttlSec: 900, ...(h ? { headers: h } : {}) });
|
|
257
|
+
return {
|
|
258
|
+
data: r.data?.coins?.map(({ item }) => ({ id: item.id, name: item.name, symbol: item.symbol, rank: item.market_cap_rank, change24h: item.data?.price_change_percentage_24h?.usd ?? null })) ?? null,
|
|
259
|
+
prov: r.prov,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
export async function coinByContract(ctx, platform, address) {
|
|
263
|
+
const h = cgHeaders();
|
|
264
|
+
const r = await ctx.http.get(`https://api.coingecko.com/api/v3/coins/${enc(platform)}/contract/${enc(address.toLowerCase())}`, { source: 'coingecko', ttlSec: 600, ...(h ? { headers: h } : {}) });
|
|
265
|
+
const d = r.data;
|
|
266
|
+
return {
|
|
267
|
+
data: d?.id ? { id: d.id, name: d.name ?? '', symbol: d.symbol ?? '', priceUsd: d.market_data?.current_price?.usd ?? null, marketCapUsd: d.market_data?.market_cap?.usd ?? null, change24h: d.market_data?.price_change_percentage_24h ?? null } : null,
|
|
268
|
+
prov: r.prov,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Ctx } from '../core/context.js';
|
|
2
|
+
import type { Sourced } from '../core/types.js';
|
|
3
|
+
export interface GdeltPoint {
|
|
4
|
+
/** YYYYMMDD */
|
|
5
|
+
date: string;
|
|
6
|
+
/** Share of all monitored global news coverage, in percent. */
|
|
7
|
+
value: number;
|
|
8
|
+
}
|
|
9
|
+
export interface GdeltArticle {
|
|
10
|
+
url: string;
|
|
11
|
+
title: string;
|
|
12
|
+
seendate: string;
|
|
13
|
+
domain: string;
|
|
14
|
+
sourcecountry?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function gdeltQuery(q: string): string;
|
|
17
|
+
/** Daily share of global news coverage mentioning the query. */
|
|
18
|
+
export declare function coverageTimeline(ctx: Ctx, query: string, days?: number): Promise<Sourced<GdeltPoint[]>>;
|
|
19
|
+
export declare function latestArticles(ctx: Ctx, query: string, max?: number): Promise<Sourced<GdeltArticle[]>>;
|