oculon 0.0.0-stage → 0.1.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.
- 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 +346 -0
- package/dist/index.js +503 -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 +149 -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 +59 -0
- package/dist/modules/tortoise.js +133 -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 +49 -0
- package/dist/sources/opentimestamps.js +145 -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 +281 -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 +1144 -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 +136 -0
- package/dist/web/server.d.ts +69 -0
- package/dist/web/server.js +764 -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
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `oculon` binary. node:sqlite still prints an ExperimentalWarning on some Node versions; it is filtered
|
|
4
|
+
* here (other warnings still show) before the real CLI is loaded with a dynamic import.
|
|
5
|
+
*/
|
|
6
|
+
process.removeAllListeners('warning');
|
|
7
|
+
process.on('warning', (w) => {
|
|
8
|
+
if (w.name !== 'ExperimentalWarning')
|
|
9
|
+
console.warn(w);
|
|
10
|
+
});
|
|
11
|
+
const { main } = await import('./cli/main.js');
|
|
12
|
+
const code = await main();
|
|
13
|
+
if (code !== 0)
|
|
14
|
+
process.exitCode = code;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Graph } from './graph.js';
|
|
2
|
+
import { Http, type HttpConfig } from './http.js';
|
|
3
|
+
import { Store } from './store.js';
|
|
4
|
+
export { oculonHome } from './paths.js';
|
|
5
|
+
export interface Ctx {
|
|
6
|
+
store: Store;
|
|
7
|
+
http: Http;
|
|
8
|
+
graph: Graph;
|
|
9
|
+
now: () => Date;
|
|
10
|
+
}
|
|
11
|
+
export interface ContextOptions extends HttpConfig {
|
|
12
|
+
dbPath?: string;
|
|
13
|
+
now?: () => Date;
|
|
14
|
+
}
|
|
15
|
+
export declare function createContext(opts?: ContextOptions): Ctx;
|
|
16
|
+
export declare const DAY_MS = 86400000;
|
|
17
|
+
/** YYYYMMDD in UTC. */
|
|
18
|
+
export declare function ymd(d: Date): string;
|
|
19
|
+
export declare const enc: typeof encodeURIComponent;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Graph } from './graph.js';
|
|
2
|
+
import { Http } from './http.js';
|
|
3
|
+
import { Store } from './store.js';
|
|
4
|
+
export { oculonHome } from './paths.js';
|
|
5
|
+
export function createContext(opts = {}) {
|
|
6
|
+
const store = new Store(opts.dbPath);
|
|
7
|
+
return {
|
|
8
|
+
store,
|
|
9
|
+
http: new Http(store, opts),
|
|
10
|
+
graph: new Graph(store),
|
|
11
|
+
now: opts.now ?? (() => new Date()),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export const DAY_MS = 86_400_000;
|
|
15
|
+
/** YYYYMMDD in UTC. */
|
|
16
|
+
export function ymd(d) {
|
|
17
|
+
return d.toISOString().slice(0, 10).replace(/-/g, '');
|
|
18
|
+
}
|
|
19
|
+
export const enc = encodeURIComponent;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature switches.
|
|
3
|
+
*
|
|
4
|
+
* On-chain features (Robinhood Chain pools, token and deployer checks through Heron; wallet and token
|
|
5
|
+
* checks through Magpie) are paused by default. The code stays in place and keeps its tests; setting
|
|
6
|
+
* OCULON_ONCHAIN=1 turns the commands, MCP tools, agent tools, schedule task, watch kinds and web
|
|
7
|
+
* sections back on.
|
|
8
|
+
*/
|
|
9
|
+
export declare function onchainEnabled(): boolean;
|
|
10
|
+
/** CLI commands that belong to the on-chain features. */
|
|
11
|
+
export declare const ONCHAIN_COMMANDS: Set<string>;
|
|
12
|
+
/** MCP and agent tools that belong to the on-chain features. */
|
|
13
|
+
export declare const ONCHAIN_TOOLS: Set<string>;
|
|
14
|
+
/** Modules whose signals are on-chain findings. */
|
|
15
|
+
export declare const ONCHAIN_MODULES: Set<string>;
|
|
16
|
+
/** Watchlist kinds that run on-chain checks. */
|
|
17
|
+
export declare const ONCHAIN_WATCH_KINDS: Set<string>;
|
|
18
|
+
/** Graph entities created by on-chain checks (tokens, deployers, wallets). */
|
|
19
|
+
export declare function isOnchainEntity(id: string): boolean;
|
|
20
|
+
export declare const ONCHAIN_PAUSED_MESSAGE = "On-chain features (Robinhood Chain, wallet and token checks, DeFi) are paused in this build. Set OCULON_ONCHAIN=1 to turn them back on.";
|
|
21
|
+
/** Sources and hosts that only serve the on-chain features. */
|
|
22
|
+
export declare function isOnchainSource(name: string): boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature switches.
|
|
3
|
+
*
|
|
4
|
+
* On-chain features (Robinhood Chain pools, token and deployer checks through Heron; wallet and token
|
|
5
|
+
* checks through Magpie) are paused by default. The code stays in place and keeps its tests; setting
|
|
6
|
+
* OCULON_ONCHAIN=1 turns the commands, MCP tools, agent tools, schedule task, watch kinds and web
|
|
7
|
+
* sections back on.
|
|
8
|
+
*/
|
|
9
|
+
export function onchainEnabled() {
|
|
10
|
+
return process.env.OCULON_ONCHAIN === '1';
|
|
11
|
+
}
|
|
12
|
+
/** CLI commands that belong to the on-chain features. */
|
|
13
|
+
export const ONCHAIN_COMMANDS = new Set(['rh', 'deployer', 'wallet', 'token', 'defi', 'protocol']);
|
|
14
|
+
/** MCP and agent tools that belong to the on-chain features. */
|
|
15
|
+
export const ONCHAIN_TOOLS = new Set(['oculon_wallet', 'oculon_token', 'oculon_robinhood', 'oculon_robinhood_token', 'oculon_robinhood_deployer', 'oculon_defi', 'oculon_protocol']);
|
|
16
|
+
/** Modules whose signals are on-chain findings. */
|
|
17
|
+
export const ONCHAIN_MODULES = new Set(['heron', 'magpie', 'defi']);
|
|
18
|
+
/** Watchlist kinds that run on-chain checks. */
|
|
19
|
+
export const ONCHAIN_WATCH_KINDS = new Set(['wallet', 'token']);
|
|
20
|
+
/** Graph entities created by on-chain checks (tokens, deployers, wallets). */
|
|
21
|
+
export function isOnchainEntity(id) {
|
|
22
|
+
return /^event:(token:|deployer:)|^(wallet|token):/i.test(id);
|
|
23
|
+
}
|
|
24
|
+
export const ONCHAIN_PAUSED_MESSAGE = 'On-chain features (Robinhood Chain, wallet and token checks, DeFi) are paused in this build. Set OCULON_ONCHAIN=1 to turn them back on.';
|
|
25
|
+
/** Sources and hosts that only serve the on-chain features. */
|
|
26
|
+
export function isOnchainSource(name) {
|
|
27
|
+
return /defillama|llama\.fi|alternative\.me|blockscout|geckoterminal|robinhood|dexscreener|zerion|etherscan|helius|webacy|goplus|codex|4byte|openchain|eth-labels/i.test(name);
|
|
28
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Store } from './store.js';
|
|
2
|
+
import type { Signal } from './types.js';
|
|
3
|
+
export type EntityKind = 'domain' | 'subdomain' | 'url' | 'org' | 'repo' | 'topic' | 'article' | 'story' | 'market' | 'event' | 'aircraft' | 'company';
|
|
4
|
+
export interface Entity {
|
|
5
|
+
id: string;
|
|
6
|
+
kind: EntityKind;
|
|
7
|
+
name: string;
|
|
8
|
+
attrs: Record<string, unknown>;
|
|
9
|
+
createdAt: number;
|
|
10
|
+
updatedAt: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Neighbor {
|
|
13
|
+
id: string;
|
|
14
|
+
kind: EntityKind;
|
|
15
|
+
name: string;
|
|
16
|
+
rel: string;
|
|
17
|
+
direction: 'out' | 'in';
|
|
18
|
+
source: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function entityId(kind: EntityKind, name: string): string;
|
|
21
|
+
/** Entity graph: everything Oculon sees (domains, repos, articles, markets...) becomes a node with typed edges. */
|
|
22
|
+
export declare class Graph {
|
|
23
|
+
private readonly store;
|
|
24
|
+
constructor(store: Store);
|
|
25
|
+
get(id: string): Entity | undefined;
|
|
26
|
+
has(id: string): boolean;
|
|
27
|
+
/** Insert or merge attributes. Returns the entity id. */
|
|
28
|
+
upsert(kind: EntityKind, name: string, attrs?: Record<string, unknown>, now?: number): string;
|
|
29
|
+
link(src: string, dst: string, rel: string, source: string, weight?: number, now?: number): void;
|
|
30
|
+
neighbors(id: string): Neighbor[];
|
|
31
|
+
observe(entity: string, module: string, kind: string, value: number | null, source: string, payload?: Record<string, unknown>, now?: number): void;
|
|
32
|
+
history(entity: string, kind: string, limit?: number): {
|
|
33
|
+
value: number | null;
|
|
34
|
+
observedAt: number;
|
|
35
|
+
}[];
|
|
36
|
+
saveSignal(s: Signal): void;
|
|
37
|
+
recentSignals(limit?: number): Signal[];
|
|
38
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export function entityId(kind, name) {
|
|
2
|
+
return `${kind}:${name.trim().toLowerCase()}`;
|
|
3
|
+
}
|
|
4
|
+
const toEntity = (r) => ({
|
|
5
|
+
id: r.id,
|
|
6
|
+
kind: r.kind,
|
|
7
|
+
name: r.name,
|
|
8
|
+
attrs: JSON.parse(r.attrs),
|
|
9
|
+
createdAt: r.created_at,
|
|
10
|
+
updatedAt: r.updated_at,
|
|
11
|
+
});
|
|
12
|
+
/** Entity graph: everything Oculon sees (domains, repos, articles, markets...) becomes a node with typed edges. */
|
|
13
|
+
export class Graph {
|
|
14
|
+
store;
|
|
15
|
+
constructor(store) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
}
|
|
18
|
+
get(id) {
|
|
19
|
+
const row = this.store.db.prepare('SELECT * FROM entities WHERE id = ?').get(id);
|
|
20
|
+
return row ? toEntity(row) : undefined;
|
|
21
|
+
}
|
|
22
|
+
has(id) {
|
|
23
|
+
return this.store.db.prepare('SELECT 1 FROM entities WHERE id = ?').get(id) !== undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Insert or merge attributes. Returns the entity id. */
|
|
26
|
+
upsert(kind, name, attrs = {}, now = Date.now()) {
|
|
27
|
+
const id = entityId(kind, name);
|
|
28
|
+
const existing = this.get(id);
|
|
29
|
+
const merged = JSON.stringify({ ...(existing?.attrs ?? {}), ...attrs });
|
|
30
|
+
this.store.db
|
|
31
|
+
.prepare(`INSERT INTO entities (id, kind, name, attrs, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)
|
|
32
|
+
ON CONFLICT(id) DO UPDATE SET attrs = excluded.attrs, updated_at = excluded.updated_at`)
|
|
33
|
+
.run(id, kind, name.trim(), merged, now, now);
|
|
34
|
+
return id;
|
|
35
|
+
}
|
|
36
|
+
link(src, dst, rel, source, weight = 1, now = Date.now()) {
|
|
37
|
+
this.store.db
|
|
38
|
+
.prepare(`INSERT INTO edges (src, dst, rel, source, weight, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
39
|
+
ON CONFLICT(src, dst, rel) DO UPDATE SET weight = excluded.weight, updated_at = excluded.updated_at`)
|
|
40
|
+
.run(src, dst, rel, source, weight, now, now);
|
|
41
|
+
}
|
|
42
|
+
neighbors(id) {
|
|
43
|
+
const rows = this.store.db
|
|
44
|
+
.prepare(`SELECT e.dst AS id, n.kind, n.name, e.rel, 'out' AS direction, e.source FROM edges e JOIN entities n ON n.id = e.dst WHERE e.src = ?
|
|
45
|
+
UNION ALL
|
|
46
|
+
SELECT e.src AS id, n.kind, n.name, e.rel, 'in' AS direction, e.source FROM edges e JOIN entities n ON n.id = e.src WHERE e.dst = ?`)
|
|
47
|
+
.all(id, id);
|
|
48
|
+
return rows;
|
|
49
|
+
}
|
|
50
|
+
observe(entity, module, kind, value, source, payload = {}, now = Date.now()) {
|
|
51
|
+
this.store.db
|
|
52
|
+
.prepare('INSERT INTO observations (entity_id, module, kind, value, payload, source, observed_at) VALUES (?, ?, ?, ?, ?, ?, ?)')
|
|
53
|
+
.run(entity, module, kind, value, JSON.stringify(payload), source, now);
|
|
54
|
+
}
|
|
55
|
+
history(entity, kind, limit = 100) {
|
|
56
|
+
const rows = this.store.db
|
|
57
|
+
.prepare('SELECT value, observed_at FROM observations WHERE entity_id = ? AND kind = ? ORDER BY observed_at DESC LIMIT ?')
|
|
58
|
+
.all(entity, kind, limit);
|
|
59
|
+
return rows.map((r) => ({ value: r.value, observedAt: r.observed_at }));
|
|
60
|
+
}
|
|
61
|
+
saveSignal(s) {
|
|
62
|
+
this.store.db
|
|
63
|
+
.prepare(`INSERT INTO signals (id, module, entity_id, title, score, evidence, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
64
|
+
ON CONFLICT(id) DO UPDATE SET score = excluded.score, evidence = excluded.evidence, title = excluded.title`)
|
|
65
|
+
.run(s.id, s.module, s.entity ?? null, s.title, s.score, JSON.stringify(s.evidence), Date.parse(s.createdAt));
|
|
66
|
+
}
|
|
67
|
+
recentSignals(limit = 50) {
|
|
68
|
+
const rows = this.store.db
|
|
69
|
+
.prepare('SELECT * FROM signals ORDER BY created_at DESC, score DESC LIMIT ?')
|
|
70
|
+
.all(limit);
|
|
71
|
+
return rows.map((r) => ({
|
|
72
|
+
id: r.id,
|
|
73
|
+
module: r.module,
|
|
74
|
+
title: r.title,
|
|
75
|
+
score: r.score,
|
|
76
|
+
...(r.entity_id ? { entity: r.entity_id } : {}),
|
|
77
|
+
evidence: JSON.parse(r.evidence),
|
|
78
|
+
createdAt: new Date(r.created_at).toISOString(),
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Store } from './store.js';
|
|
2
|
+
import type { Sourced } from './types.js';
|
|
3
|
+
export declare function userAgent(url?: string): string;
|
|
4
|
+
export interface GetOptions {
|
|
5
|
+
source: string;
|
|
6
|
+
as?: 'json' | 'text';
|
|
7
|
+
/** How long a cached response counts as fresh. Default 15 minutes. */
|
|
8
|
+
ttlSec?: number;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
retries?: number;
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
/** Some APIs (GDELT) answer 200 with a plain-text rate-limit message; treat a match as HTTP 429. */
|
|
13
|
+
rateLimitText?: RegExp;
|
|
14
|
+
/** Keep only what the caller needs before caching (JSON only). Huge documents such as npm packuments shrink to a few bytes. */
|
|
15
|
+
keep?: (data: any) => unknown;
|
|
16
|
+
}
|
|
17
|
+
export interface HttpConfig {
|
|
18
|
+
fetch?: typeof fetch;
|
|
19
|
+
throttle?: boolean;
|
|
20
|
+
retryDelayMs?: number;
|
|
21
|
+
}
|
|
22
|
+
export declare class Http {
|
|
23
|
+
private readonly store;
|
|
24
|
+
private readonly cfg;
|
|
25
|
+
constructor(store: Store, cfg?: HttpConfig);
|
|
26
|
+
private parse;
|
|
27
|
+
/** Uncached binary POST (used for timestamp calendars). Returns the raw response bytes. */
|
|
28
|
+
postBytes(url: string, body: Uint8Array, o: {
|
|
29
|
+
source: string;
|
|
30
|
+
headers?: Record<string, string>;
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
retries?: number;
|
|
33
|
+
}): Promise<Sourced<Buffer>>;
|
|
34
|
+
/** Uncached binary GET; 404 is reported as UNAVAILABLE. Network errors and 5xx are retried once. */
|
|
35
|
+
getBytes(url: string, o: {
|
|
36
|
+
source: string;
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
retries?: number;
|
|
40
|
+
}): Promise<Sourced<Buffer>>;
|
|
41
|
+
get<T = unknown>(url: string, o: GetOptions): Promise<Sourced<T>>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { scrubSecrets } from './keys.js';
|
|
2
|
+
import { takeQuota } from './quota.js';
|
|
3
|
+
/** Minimum gap between requests per host, following each provider's published guidance. */
|
|
4
|
+
const HOST_GAP_MS = {
|
|
5
|
+
'crt.sh': 2000,
|
|
6
|
+
'api.gdeltproject.org': 5500,
|
|
7
|
+
'export.arxiv.org': 3100,
|
|
8
|
+
'api.github.com': 1500,
|
|
9
|
+
'web.archive.org': 1000,
|
|
10
|
+
'api.certspotter.com': 1000,
|
|
11
|
+
'opensky-network.org': 1000,
|
|
12
|
+
'wikimedia.org': 400,
|
|
13
|
+
'www.sec.gov': 150,
|
|
14
|
+
'data.sec.gov': 150,
|
|
15
|
+
'hexdb.io': 500,
|
|
16
|
+
'api.adsb.lol': 1000,
|
|
17
|
+
'nominatim.openstreetmap.org': 1100,
|
|
18
|
+
'gibs.earthdata.nasa.gov': 150,
|
|
19
|
+
'content.guardianapis.com': 1100,
|
|
20
|
+
'api.nytimes.com': 12_500,
|
|
21
|
+
'gnews.io': 1100,
|
|
22
|
+
'finnhub.io': 1100,
|
|
23
|
+
'api.openalex.org': 120,
|
|
24
|
+
'www.courtlistener.com': 800,
|
|
25
|
+
'api.adzuna.com': 500,
|
|
26
|
+
'urlscan.io': 1000,
|
|
27
|
+
'api.zerion.io': 1100,
|
|
28
|
+
'api.etherscan.io': 250,
|
|
29
|
+
'api.gopluslabs.io': 500,
|
|
30
|
+
'api.webacy.com': 500,
|
|
31
|
+
'api.dexscreener.com': 250,
|
|
32
|
+
'api.coingecko.com': 2100,
|
|
33
|
+
'api.openaq.org': 1100,
|
|
34
|
+
'www.googleapis.com': 200,
|
|
35
|
+
'bsky.social': 300,
|
|
36
|
+
'r.jina.ai': 500,
|
|
37
|
+
'api.tavily.com': 300,
|
|
38
|
+
'api.exa.ai': 300,
|
|
39
|
+
'api.stlouisfed.org': 600,
|
|
40
|
+
'api.elections.kalshi.com': 500,
|
|
41
|
+
'api.manifold.markets': 300,
|
|
42
|
+
'itunes.apple.com': 3100,
|
|
43
|
+
'huggingface.co': 300,
|
|
44
|
+
'registry.npmjs.org': 200,
|
|
45
|
+
'clob.polymarket.com': 300,
|
|
46
|
+
'archive.org': 1000,
|
|
47
|
+
'www.treasury.gov': 1000,
|
|
48
|
+
'query.wikidata.org': 1100,
|
|
49
|
+
'api.geckoterminal.com': 2100,
|
|
50
|
+
'api.blockscout.com': 300,
|
|
51
|
+
'services.nvd.nist.gov': 6500, // keyless: 5 requests per 30 seconds
|
|
52
|
+
'efts.sec.gov': 150,
|
|
53
|
+
'api.fda.gov': 300,
|
|
54
|
+
'clinicaltrials.gov': 1300,
|
|
55
|
+
'www.federalregister.gov': 300,
|
|
56
|
+
'api.osv.dev': 100,
|
|
57
|
+
'www.4byte.directory': 500,
|
|
58
|
+
'api.semanticscholar.org': 1100, // 1 request per second with a key
|
|
59
|
+
};
|
|
60
|
+
const chains = new Map();
|
|
61
|
+
async function throttle(host) {
|
|
62
|
+
const gap = HOST_GAP_MS[host];
|
|
63
|
+
if (!gap)
|
|
64
|
+
return;
|
|
65
|
+
const prev = chains.get(host) ?? Promise.resolve();
|
|
66
|
+
let release;
|
|
67
|
+
const mine = new Promise((r) => (release = r));
|
|
68
|
+
chains.set(host, prev.then(() => mine));
|
|
69
|
+
await prev;
|
|
70
|
+
setTimeout(release, gap);
|
|
71
|
+
}
|
|
72
|
+
/** Hosts that require contact details in the User-Agent. Every other host gets no personal data. */
|
|
73
|
+
const CONTACT_HOSTS = /(^|\.)sec\.gov$/;
|
|
74
|
+
export function userAgent(url) {
|
|
75
|
+
let host = '';
|
|
76
|
+
try {
|
|
77
|
+
host = url ? new URL(url).hostname : '';
|
|
78
|
+
}
|
|
79
|
+
catch { }
|
|
80
|
+
const contact = CONTACT_HOSTS.test(host) ? process.env.OCULON_CONTACT?.trim() : undefined;
|
|
81
|
+
return `Oculon/0.1 (open-source OSINT engine${contact ? `; ${contact}` : ''})`;
|
|
82
|
+
}
|
|
83
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
84
|
+
/** Provenance is shown to users, LLMs and dashboards, so API keys in URLs or notes are always masked. */
|
|
85
|
+
function prov(source, url, status, fetchedAt, note) {
|
|
86
|
+
return { source, url: scrubSecrets(url), status, fetchedAt: new Date(fetchedAt).toISOString(), ...(note ? { note: scrubSecrets(note) } : {}) };
|
|
87
|
+
}
|
|
88
|
+
function errMsg(e) {
|
|
89
|
+
if (e instanceof Error)
|
|
90
|
+
return e.name === 'TimeoutError' ? 'timeout' : e.message;
|
|
91
|
+
return String(e);
|
|
92
|
+
}
|
|
93
|
+
/** A short reason from a JSON error body (Cloudflare `errors[0]`, or `message` / `error`), as " (reason)". */
|
|
94
|
+
function errorReason(text) {
|
|
95
|
+
try {
|
|
96
|
+
const j = JSON.parse(text);
|
|
97
|
+
const e = j.errors?.[0];
|
|
98
|
+
const why = e?.message ? `${e.code ? `${e.code} ` : ''}${e.message}` : typeof j.message === 'string' ? j.message : typeof j.error === 'string' ? j.error : '';
|
|
99
|
+
return why ? ` (${scrubSecrets(why).slice(0, 120)})` : '';
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return '';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
class ParseError extends Error {
|
|
106
|
+
}
|
|
107
|
+
export class Http {
|
|
108
|
+
store;
|
|
109
|
+
cfg;
|
|
110
|
+
constructor(store, cfg = {}) {
|
|
111
|
+
this.store = store;
|
|
112
|
+
this.cfg = cfg;
|
|
113
|
+
}
|
|
114
|
+
parse(body, as) {
|
|
115
|
+
if (as === 'text')
|
|
116
|
+
return body;
|
|
117
|
+
try {
|
|
118
|
+
return JSON.parse(body);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
throw new ParseError(`invalid JSON: ${body.slice(0, 120).replace(/\s+/g, ' ')}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Uncached binary POST (used for timestamp calendars). Returns the raw response bytes. */
|
|
125
|
+
async postBytes(url, body, o) {
|
|
126
|
+
if (!takeQuota(this.store, o.source))
|
|
127
|
+
return { data: null, prov: prov(o.source, url, 'UNAVAILABLE', Date.now(), 'daily quota reached') };
|
|
128
|
+
const doFetch = this.cfg.fetch ?? fetch;
|
|
129
|
+
const retries = o.retries ?? 1;
|
|
130
|
+
let lastErr = 'unknown error';
|
|
131
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
132
|
+
if (this.cfg.throttle !== false)
|
|
133
|
+
await throttle(new URL(url).host);
|
|
134
|
+
try {
|
|
135
|
+
const res = await doFetch(url, {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
body: body,
|
|
138
|
+
headers: { 'user-agent': userAgent(url), ...o.headers },
|
|
139
|
+
signal: AbortSignal.timeout(o.timeoutMs ?? 20_000),
|
|
140
|
+
});
|
|
141
|
+
if (!res.ok) {
|
|
142
|
+
lastErr = `HTTP ${res.status}`;
|
|
143
|
+
if (res.status < 500 && res.status !== 429)
|
|
144
|
+
break;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
return { data: Buffer.from(await res.arrayBuffer()), prov: prov(o.source, url, 'LIVE', Date.now()) };
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
lastErr = errMsg(e);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { data: null, prov: prov(o.source, url, 'ERROR', Date.now(), lastErr) };
|
|
154
|
+
}
|
|
155
|
+
/** Uncached binary GET; 404 is reported as UNAVAILABLE. Network errors and 5xx are retried once. */
|
|
156
|
+
async getBytes(url, o) {
|
|
157
|
+
const doFetch = this.cfg.fetch ?? fetch;
|
|
158
|
+
const retries = o.retries ?? 1;
|
|
159
|
+
let lastErr = 'unknown error';
|
|
160
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
161
|
+
if (this.cfg.throttle !== false)
|
|
162
|
+
await throttle(new URL(url).host);
|
|
163
|
+
try {
|
|
164
|
+
const res = await doFetch(url, { headers: { 'user-agent': userAgent(url), ...o.headers }, signal: AbortSignal.timeout(o.timeoutMs ?? 20_000) });
|
|
165
|
+
if (res.status === 404)
|
|
166
|
+
return { data: null, prov: prov(o.source, url, 'UNAVAILABLE', Date.now(), 'HTTP 404') };
|
|
167
|
+
if (res.ok)
|
|
168
|
+
return { data: Buffer.from(await res.arrayBuffer()), prov: prov(o.source, url, 'LIVE', Date.now()) };
|
|
169
|
+
lastErr = `HTTP ${res.status}`;
|
|
170
|
+
if (res.status < 500 && res.status !== 429)
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
lastErr = errMsg(e);
|
|
175
|
+
}
|
|
176
|
+
if (attempt < retries)
|
|
177
|
+
await sleep(this.cfg.retryDelayMs ?? 1500);
|
|
178
|
+
}
|
|
179
|
+
return { data: null, prov: prov(o.source, url, 'ERROR', Date.now(), lastErr) };
|
|
180
|
+
}
|
|
181
|
+
async get(url, o) {
|
|
182
|
+
const as = o.as ?? 'json';
|
|
183
|
+
const ttl = (o.ttlSec ?? 900) * 1000;
|
|
184
|
+
const retries = o.retries ?? 2;
|
|
185
|
+
const cached = this.store.cacheGet(url);
|
|
186
|
+
const now = Date.now();
|
|
187
|
+
if (cached && now - cached.fetchedAt < ttl) {
|
|
188
|
+
return { data: this.parse(cached.body, as), prov: prov(o.source, url, 'CACHED', cached.fetchedAt) };
|
|
189
|
+
}
|
|
190
|
+
if (!takeQuota(this.store, o.source)) {
|
|
191
|
+
if (cached)
|
|
192
|
+
return { data: this.parse(cached.body, as), prov: prov(o.source, url, 'CACHED', cached.fetchedAt, 'daily quota reached; showing last cached result') };
|
|
193
|
+
return { data: null, prov: prov(o.source, url, 'UNAVAILABLE', Date.now(), 'daily quota reached') };
|
|
194
|
+
}
|
|
195
|
+
const doFetch = this.cfg.fetch ?? fetch;
|
|
196
|
+
const host = new URL(url).host;
|
|
197
|
+
const baseDelay = this.cfg.retryDelayMs ?? 1500;
|
|
198
|
+
let lastErr = 'unknown error';
|
|
199
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
200
|
+
if (this.cfg.throttle !== false)
|
|
201
|
+
await throttle(host);
|
|
202
|
+
try {
|
|
203
|
+
const res = await doFetch(url, {
|
|
204
|
+
headers: {
|
|
205
|
+
'user-agent': userAgent(url),
|
|
206
|
+
accept: as === 'json' ? 'application/json' : '*/*',
|
|
207
|
+
...o.headers,
|
|
208
|
+
},
|
|
209
|
+
signal: AbortSignal.timeout(o.timeoutMs ?? 20_000),
|
|
210
|
+
});
|
|
211
|
+
if (res.status === 404) {
|
|
212
|
+
return { data: null, prov: prov(o.source, url, 'UNAVAILABLE', Date.now(), 'HTTP 404') };
|
|
213
|
+
}
|
|
214
|
+
if (res.status === 429 || res.status >= 500) {
|
|
215
|
+
lastErr = `HTTP ${res.status}`;
|
|
216
|
+
const retryAfter = Number(res.headers.get('retry-after'));
|
|
217
|
+
const wait = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1000, 30_000) : baseDelay * 2 ** attempt;
|
|
218
|
+
if (attempt < retries)
|
|
219
|
+
await sleep(this.cfg.retryDelayMs !== undefined ? baseDelay : wait);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (!res.ok) {
|
|
223
|
+
const text = await res.text().catch(() => '');
|
|
224
|
+
lastErr = `HTTP ${res.status}${errorReason(text)}`;
|
|
225
|
+
// GitHub signals (secondary) rate limits with 403.
|
|
226
|
+
if (res.status === 403 && (res.headers.get('x-ratelimit-remaining') === '0' || /rate limit/i.test(text))) {
|
|
227
|
+
lastErr = 'HTTP 403 rate limited';
|
|
228
|
+
if (attempt < retries)
|
|
229
|
+
await sleep(this.cfg.retryDelayMs !== undefined ? baseDelay : 8000 * 2 ** attempt);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
const body = await res.text();
|
|
235
|
+
if (o.rateLimitText?.test(body.slice(0, 300))) {
|
|
236
|
+
lastErr = 'rate limited';
|
|
237
|
+
if (attempt < retries)
|
|
238
|
+
await sleep(Math.max(baseDelay, 6000) * 2 ** attempt);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
let data = this.parse(body, as);
|
|
242
|
+
let stored = body;
|
|
243
|
+
if (o.keep && as === 'json' && data != null) {
|
|
244
|
+
data = o.keep(data);
|
|
245
|
+
stored = JSON.stringify(data);
|
|
246
|
+
}
|
|
247
|
+
this.store.cachePut(url, stored);
|
|
248
|
+
return { data, prov: prov(o.source, url, 'LIVE', Date.now()) };
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
lastErr = errMsg(e);
|
|
252
|
+
if (e instanceof ParseError)
|
|
253
|
+
break;
|
|
254
|
+
if (attempt < retries)
|
|
255
|
+
await sleep(baseDelay * 2 ** attempt);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (cached) {
|
|
259
|
+
return {
|
|
260
|
+
data: this.parse(cached.body, as),
|
|
261
|
+
prov: prov(o.source, url, 'CACHED', cached.fetchedAt, `stale, live fetch failed: ${lastErr}`),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
return { data: null, prov: prov(o.source, url, 'ERROR', Date.now(), lastErr) };
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Provenance, Sourced } from './types.js';
|
|
2
|
+
/** Read an optional API key from the environment. */
|
|
3
|
+
export declare function key(name: string): string | undefined;
|
|
4
|
+
/** Provenance for a source that is skipped because its key is not configured. */
|
|
5
|
+
export declare function noKey<T>(source: string, envName: string): Sourced<T>;
|
|
6
|
+
export declare function missingKey(source: string, envName: string): Provenance;
|
|
7
|
+
/** Remove known secret values from text before it is shown or stored. */
|
|
8
|
+
export declare function scrubSecrets(text: string): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Read an optional API key from the environment. */
|
|
2
|
+
export function key(name) {
|
|
3
|
+
const v = process.env[name]?.trim();
|
|
4
|
+
return v ? v : undefined;
|
|
5
|
+
}
|
|
6
|
+
/** Provenance for a source that is skipped because its key is not configured. */
|
|
7
|
+
export function noKey(source, envName) {
|
|
8
|
+
return { data: null, prov: missingKey(source, envName) };
|
|
9
|
+
}
|
|
10
|
+
export function missingKey(source, envName) {
|
|
11
|
+
return { source, url: '', status: 'UNAVAILABLE', fetchedAt: new Date().toISOString(), note: `set ${envName} to enable` };
|
|
12
|
+
}
|
|
13
|
+
/** Remove known secret values from text before it is shown or stored. */
|
|
14
|
+
export function scrubSecrets(text) {
|
|
15
|
+
let out = text;
|
|
16
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
17
|
+
if (!v || v.length < 12 || !/KEY|TOKEN|SECRET|PASSWORD/.test(k))
|
|
18
|
+
continue;
|
|
19
|
+
out = out.split(v).join('***');
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export interface ToolCall {
|
|
2
|
+
id: string;
|
|
3
|
+
type: 'function';
|
|
4
|
+
function: {
|
|
5
|
+
name: string;
|
|
6
|
+
arguments: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export interface ChatMessage {
|
|
10
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
11
|
+
content: string | null;
|
|
12
|
+
tool_calls?: ToolCall[];
|
|
13
|
+
tool_call_id?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ToolSpec {
|
|
17
|
+
type: 'function';
|
|
18
|
+
function: {
|
|
19
|
+
name: string;
|
|
20
|
+
description: string;
|
|
21
|
+
parameters: Record<string, unknown>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export interface ChatResult {
|
|
25
|
+
message: ChatMessage;
|
|
26
|
+
provider: string;
|
|
27
|
+
model: string;
|
|
28
|
+
}
|
|
29
|
+
interface Provider {
|
|
30
|
+
name: string;
|
|
31
|
+
env: string;
|
|
32
|
+
url: string;
|
|
33
|
+
model: string;
|
|
34
|
+
extraHeaders?: Record<string, string>;
|
|
35
|
+
/** Extra tokens for models that think before answering, so the answer is not cut off. */
|
|
36
|
+
reasoningHeadroom?: number;
|
|
37
|
+
}
|
|
38
|
+
export declare const PROVIDERS: Provider[];
|
|
39
|
+
export declare const availableProviders: () => (Provider | {
|
|
40
|
+
name: string;
|
|
41
|
+
})[];
|
|
42
|
+
export interface ChatOptions {
|
|
43
|
+
tools?: ToolSpec[];
|
|
44
|
+
temperature?: number;
|
|
45
|
+
maxTokens?: number;
|
|
46
|
+
json?: boolean;
|
|
47
|
+
fetch?: typeof fetch;
|
|
48
|
+
timeoutMs?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Use the paid Claude model first when ANTHROPIC_API_KEY is set. Off by default so that high-volume
|
|
51
|
+
* background work (radar linking, market matching, narratives) stays on free tiers.
|
|
52
|
+
* 'deep' picks the most capable model, for case investigations.
|
|
53
|
+
*/
|
|
54
|
+
premium?: boolean | 'deep';
|
|
55
|
+
}
|
|
56
|
+
export declare function setPaidQuota(fn: (source: string) => boolean): void;
|
|
57
|
+
export declare const CLAUDE_MODEL: () => string;
|
|
58
|
+
export declare const CLAUDE_DEEP_MODEL: () => string;
|
|
59
|
+
type Block = {
|
|
60
|
+
type: 'text';
|
|
61
|
+
text: string;
|
|
62
|
+
} | {
|
|
63
|
+
type: 'tool_use';
|
|
64
|
+
id: string;
|
|
65
|
+
name: string;
|
|
66
|
+
input: unknown;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'tool_result';
|
|
69
|
+
tool_use_id: string;
|
|
70
|
+
content: string;
|
|
71
|
+
} | {
|
|
72
|
+
type: 'thinking' | 'redacted_thinking';
|
|
73
|
+
[k: string]: unknown;
|
|
74
|
+
};
|
|
75
|
+
/** OpenAI-style history to the Messages API shape: system lifted out, tool calls and results as blocks. */
|
|
76
|
+
export declare function toAnthropic(messages: ChatMessage[]): {
|
|
77
|
+
system: string;
|
|
78
|
+
messages: {
|
|
79
|
+
role: 'user' | 'assistant';
|
|
80
|
+
content: Block[];
|
|
81
|
+
}[];
|
|
82
|
+
};
|
|
83
|
+
/** Remember the raw Claude blocks when a caller copies an assistant message into its own history. */
|
|
84
|
+
export declare function carryRaw(from: ChatMessage, to: ChatMessage): ChatMessage;
|
|
85
|
+
/** One chat completion; falls through providers on errors and rate limits. */
|
|
86
|
+
export declare function chat(messages: ChatMessage[], opts?: ChatOptions): Promise<ChatResult>;
|
|
87
|
+
/** Plain-text completion helper. */
|
|
88
|
+
export declare function complete(system: string, user: string, opts?: ChatOptions): Promise<{
|
|
89
|
+
text: string;
|
|
90
|
+
provider: string;
|
|
91
|
+
model: string;
|
|
92
|
+
}>;
|
|
93
|
+
export {};
|