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.
Files changed (255) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +344 -2
  3. package/dist/agent.d.ts +47 -0
  4. package/dist/agent.js +104 -0
  5. package/dist/cases.d.ts +68 -0
  6. package/dist/cases.js +158 -0
  7. package/dist/cli/main.d.ts +12 -0
  8. package/dist/cli/main.js +1999 -0
  9. package/dist/cli.d.ts +2 -0
  10. package/dist/cli.js +15 -0
  11. package/dist/core/context.d.ts +19 -0
  12. package/dist/core/context.js +19 -0
  13. package/dist/core/features.d.ts +22 -0
  14. package/dist/core/features.js +28 -0
  15. package/dist/core/graph.d.ts +38 -0
  16. package/dist/core/graph.js +81 -0
  17. package/dist/core/http.d.ts +42 -0
  18. package/dist/core/http.js +266 -0
  19. package/dist/core/keys.d.ts +8 -0
  20. package/dist/core/keys.js +22 -0
  21. package/dist/core/llm.d.ts +93 -0
  22. package/dist/core/llm.js +180 -0
  23. package/dist/core/merkle.d.ts +15 -0
  24. package/dist/core/merkle.js +46 -0
  25. package/dist/core/paths.d.ts +2 -0
  26. package/dist/core/paths.js +6 -0
  27. package/dist/core/png.d.ts +17 -0
  28. package/dist/core/png.js +101 -0
  29. package/dist/core/quota.d.ts +15 -0
  30. package/dist/core/quota.js +90 -0
  31. package/dist/core/stats.d.ts +24 -0
  32. package/dist/core/stats.js +67 -0
  33. package/dist/core/store.d.ts +20 -0
  34. package/dist/core/store.js +213 -0
  35. package/dist/core/text.d.ts +9 -0
  36. package/dist/core/text.js +30 -0
  37. package/dist/core/types.d.ts +38 -0
  38. package/dist/core/types.js +1 -0
  39. package/dist/index.d.ts +342 -0
  40. package/dist/index.js +495 -0
  41. package/dist/live.d.ts +53 -0
  42. package/dist/live.js +236 -0
  43. package/dist/mcp.d.ts +43 -0
  44. package/dist/mcp.js +651 -0
  45. package/dist/modules/airport.d.ts +60 -0
  46. package/dist/modules/airport.js +111 -0
  47. package/dist/modules/audit.d.ts +73 -0
  48. package/dist/modules/audit.js +211 -0
  49. package/dist/modules/bgp.d.ts +147 -0
  50. package/dist/modules/bgp.js +206 -0
  51. package/dist/modules/blackout.d.ts +41 -0
  52. package/dist/modules/blackout.js +98 -0
  53. package/dist/modules/brief.d.ts +26 -0
  54. package/dist/modules/brief.js +99 -0
  55. package/dist/modules/calendar.d.ts +27 -0
  56. package/dist/modules/calendar.js +145 -0
  57. package/dist/modules/calibrate.d.ts +46 -0
  58. package/dist/modules/calibrate.js +92 -0
  59. package/dist/modules/cloud.d.ts +32 -0
  60. package/dist/modules/cloud.js +94 -0
  61. package/dist/modules/condor.d.ts +66 -0
  62. package/dist/modules/condor.js +214 -0
  63. package/dist/modules/connect.d.ts +47 -0
  64. package/dist/modules/connect.js +114 -0
  65. package/dist/modules/contradictions.d.ts +27 -0
  66. package/dist/modules/contradictions.js +76 -0
  67. package/dist/modules/country.d.ts +82 -0
  68. package/dist/modules/country.js +136 -0
  69. package/dist/modules/cricket.d.ts +34 -0
  70. package/dist/modules/cricket.js +166 -0
  71. package/dist/modules/darkships.d.ts +219 -0
  72. package/dist/modules/darkships.js +400 -0
  73. package/dist/modules/defi.d.ts +237 -0
  74. package/dist/modules/defi.js +512 -0
  75. package/dist/modules/emailsec.d.ts +47 -0
  76. package/dist/modules/emailsec.js +146 -0
  77. package/dist/modules/exposure.d.ts +43 -0
  78. package/dist/modules/exposure.js +89 -0
  79. package/dist/modules/factcheck.d.ts +39 -0
  80. package/dist/modules/factcheck.js +127 -0
  81. package/dist/modules/falcon.d.ts +58 -0
  82. package/dist/modules/falcon.js +219 -0
  83. package/dist/modules/feedback.d.ts +24 -0
  84. package/dist/modules/feedback.js +37 -0
  85. package/dist/modules/fence.d.ts +52 -0
  86. package/dist/modules/fence.js +128 -0
  87. package/dist/modules/ferret.d.ts +47 -0
  88. package/dist/modules/ferret.js +220 -0
  89. package/dist/modules/filings.d.ts +86 -0
  90. package/dist/modules/filings.js +166 -0
  91. package/dist/modules/fires.d.ts +56 -0
  92. package/dist/modules/fires.js +150 -0
  93. package/dist/modules/hazards.d.ts +37 -0
  94. package/dist/modules/hazards.js +83 -0
  95. package/dist/modules/health.d.ts +24 -0
  96. package/dist/modules/health.js +82 -0
  97. package/dist/modules/heron.d.ts +68 -0
  98. package/dist/modules/heron.js +184 -0
  99. package/dist/modules/hound.d.ts +100 -0
  100. package/dist/modules/hound.js +301 -0
  101. package/dist/modules/impact.d.ts +39 -0
  102. package/dist/modules/impact.js +63 -0
  103. package/dist/modules/launches.d.ts +24 -0
  104. package/dist/modules/launches.js +61 -0
  105. package/dist/modules/listen.d.ts +48 -0
  106. package/dist/modules/listen.js +125 -0
  107. package/dist/modules/lynx.d.ts +43 -0
  108. package/dist/modules/lynx.js +127 -0
  109. package/dist/modules/magpie.d.ts +70 -0
  110. package/dist/modules/magpie.js +174 -0
  111. package/dist/modules/meerkat.d.ts +42 -0
  112. package/dist/modules/meerkat.js +293 -0
  113. package/dist/modules/monitor.d.ts +27 -0
  114. package/dist/modules/monitor.js +105 -0
  115. package/dist/modules/narratives.d.ts +33 -0
  116. package/dist/modules/narratives.js +136 -0
  117. package/dist/modules/owl.d.ts +71 -0
  118. package/dist/modules/owl.js +290 -0
  119. package/dist/modules/papers.d.ts +46 -0
  120. package/dist/modules/papers.js +135 -0
  121. package/dist/modules/personal.d.ts +54 -0
  122. package/dist/modules/personal.js +153 -0
  123. package/dist/modules/pulse.d.ts +19 -0
  124. package/dist/modules/pulse.js +59 -0
  125. package/dist/modules/raven.d.ts +80 -0
  126. package/dist/modules/raven.js +147 -0
  127. package/dist/modules/recall.d.ts +44 -0
  128. package/dist/modules/recall.js +123 -0
  129. package/dist/modules/regs.d.ts +88 -0
  130. package/dist/modules/regs.js +142 -0
  131. package/dist/modules/rewind.d.ts +22 -0
  132. package/dist/modules/rewind.js +68 -0
  133. package/dist/modules/sanctions.d.ts +62 -0
  134. package/dist/modules/sanctions.js +173 -0
  135. package/dist/modules/scam.d.ts +43 -0
  136. package/dist/modules/scam.js +111 -0
  137. package/dist/modules/scoop.d.ts +31 -0
  138. package/dist/modules/scoop.js +74 -0
  139. package/dist/modules/ships.d.ts +33 -0
  140. package/dist/modules/ships.js +113 -0
  141. package/dist/modules/sky.d.ts +47 -0
  142. package/dist/modules/sky.js +183 -0
  143. package/dist/modules/squawk.d.ts +46 -0
  144. package/dist/modules/squawk.js +106 -0
  145. package/dist/modules/tortoise.d.ts +40 -0
  146. package/dist/modules/tortoise.js +104 -0
  147. package/dist/modules/travel.d.ts +94 -0
  148. package/dist/modules/travel.js +241 -0
  149. package/dist/modules/urlcheck.d.ts +63 -0
  150. package/dist/modules/urlcheck.js +223 -0
  151. package/dist/modules/vulns.d.ts +142 -0
  152. package/dist/modules/vulns.js +276 -0
  153. package/dist/plugins.d.ts +41 -0
  154. package/dist/plugins.js +70 -0
  155. package/dist/report.d.ts +12 -0
  156. package/dist/report.js +90 -0
  157. package/dist/scheduler.d.ts +22 -0
  158. package/dist/scheduler.js +139 -0
  159. package/dist/sources/aircraft.d.ts +76 -0
  160. package/dist/sources/aircraft.js +107 -0
  161. package/dist/sources/airports.d.ts +28 -0
  162. package/dist/sources/airports.js +100 -0
  163. package/dist/sources/ais.d.ts +32 -0
  164. package/dist/sources/ais.js +111 -0
  165. package/dist/sources/apify.d.ts +28 -0
  166. package/dist/sources/apify.js +80 -0
  167. package/dist/sources/arxiv.d.ts +13 -0
  168. package/dist/sources/arxiv.js +27 -0
  169. package/dist/sources/certs.d.ts +12 -0
  170. package/dist/sources/certs.js +23 -0
  171. package/dist/sources/corp.d.ts +46 -0
  172. package/dist/sources/corp.js +60 -0
  173. package/dist/sources/earth.d.ts +39 -0
  174. package/dist/sources/earth.js +76 -0
  175. package/dist/sources/fda.d.ts +77 -0
  176. package/dist/sources/fda.js +121 -0
  177. package/dist/sources/finance.d.ts +181 -0
  178. package/dist/sources/finance.js +270 -0
  179. package/dist/sources/gdelt.d.ts +19 -0
  180. package/dist/sources/gdelt.js +33 -0
  181. package/dist/sources/github.d.ts +17 -0
  182. package/dist/sources/github.js +33 -0
  183. package/dist/sources/hackernews.d.ts +20 -0
  184. package/dist/sources/hackernews.js +37 -0
  185. package/dist/sources/labels.d.ts +21 -0
  186. package/dist/sources/labels.js +87 -0
  187. package/dist/sources/launch.d.ts +16 -0
  188. package/dist/sources/launch.js +41 -0
  189. package/dist/sources/ofac.d.ts +29 -0
  190. package/dist/sources/ofac.js +86 -0
  191. package/dist/sources/onchain.d.ts +77 -0
  192. package/dist/sources/onchain.js +189 -0
  193. package/dist/sources/opentimestamps.d.ts +44 -0
  194. package/dist/sources/opentimestamps.js +131 -0
  195. package/dist/sources/polymarket.d.ts +36 -0
  196. package/dist/sources/polymarket.js +70 -0
  197. package/dist/sources/predict.d.ts +18 -0
  198. package/dist/sources/predict.js +59 -0
  199. package/dist/sources/press.d.ts +30 -0
  200. package/dist/sources/press.js +111 -0
  201. package/dist/sources/resend.d.ts +14 -0
  202. package/dist/sources/resend.js +29 -0
  203. package/dist/sources/robinhood.d.ts +109 -0
  204. package/dist/sources/robinhood.js +129 -0
  205. package/dist/sources/sec.d.ts +67 -0
  206. package/dist/sources/sec.js +129 -0
  207. package/dist/sources/sentinel.d.ts +13 -0
  208. package/dist/sources/sentinel.js +63 -0
  209. package/dist/sources/signatures.d.ts +14 -0
  210. package/dist/sources/signatures.js +103 -0
  211. package/dist/sources/social.d.ts +22 -0
  212. package/dist/sources/social.js +56 -0
  213. package/dist/sources/space.d.ts +160 -0
  214. package/dist/sources/space.js +230 -0
  215. package/dist/sources/usgs.d.ts +17 -0
  216. package/dist/sources/usgs.js +24 -0
  217. package/dist/sources/wayback.d.ts +10 -0
  218. package/dist/sources/wayback.js +28 -0
  219. package/dist/sources/websec.d.ts +19 -0
  220. package/dist/sources/websec.js +40 -0
  221. package/dist/sources/wikidata.d.ts +11 -0
  222. package/dist/sources/wikidata.js +30 -0
  223. package/dist/sources/wikimedia.d.ts +28 -0
  224. package/dist/sources/wikimedia.js +52 -0
  225. package/dist/telegram.d.ts +104 -0
  226. package/dist/telegram.js +927 -0
  227. package/dist/web/assets/favicon-32.png +0 -0
  228. package/dist/web/assets/fx.js +178 -0
  229. package/dist/web/assets/icon-192.png +0 -0
  230. package/dist/web/assets/icon-512.png +0 -0
  231. package/dist/web/assets/icon-maskable-512.png +0 -0
  232. package/dist/web/assets/night-americas.jpg +0 -0
  233. package/dist/web/assets/night-emea.jpg +0 -0
  234. package/dist/web/assets/og.png +0 -0
  235. package/dist/web/assets/redsea-bm.jpg +0 -0
  236. package/dist/web/docs.html +263 -0
  237. package/dist/web/index.html +1828 -0
  238. package/dist/web/insights.d.ts +100 -0
  239. package/dist/web/insights.js +134 -0
  240. package/dist/web/landing.html +1143 -0
  241. package/dist/web/legal.html +176 -0
  242. package/dist/web/manifest.webmanifest +20 -0
  243. package/dist/web/mcp.html +104 -0
  244. package/dist/web/mcphttp.d.ts +10 -0
  245. package/dist/web/mcphttp.js +67 -0
  246. package/dist/web/openapi.d.ts +1 -0
  247. package/dist/web/openapi.js +51 -0
  248. package/dist/web/public.d.ts +45 -0
  249. package/dist/web/public.js +135 -0
  250. package/dist/web/server.d.ts +69 -0
  251. package/dist/web/server.js +750 -0
  252. package/dist/web/share.d.ts +35 -0
  253. package/dist/web/share.js +132 -0
  254. package/dist/web/sw.js +28 -0
  255. package/package.json +60 -4
@@ -0,0 +1,17 @@
1
+ import type { Ctx } from '../core/context.js';
2
+ import type { Sourced } from '../core/types.js';
3
+ export interface Quake {
4
+ id: string;
5
+ mag: number;
6
+ place: string;
7
+ time: number;
8
+ url: string;
9
+ tsunami: boolean;
10
+ alert: string | null;
11
+ lat: number | null;
12
+ lon: number | null;
13
+ }
14
+ /** M4.5+ earthquakes in the past day. */
15
+ export declare function recentQuakes(ctx: Ctx): Promise<Sourced<Quake[]>>;
16
+ /** Earthquakes of at least `minMag` between two dates (YYYY-MM-DD), for looking back in time. */
17
+ export declare function quakesBetween(ctx: Ctx, start: string, end: string, minMag?: number): Promise<Sourced<Quake[]>>;
@@ -0,0 +1,24 @@
1
+ const toQuake = (f) => ({
2
+ id: f.id,
3
+ mag: f.properties.mag,
4
+ place: f.properties.place,
5
+ time: f.properties.time,
6
+ url: f.properties.url,
7
+ tsunami: f.properties.tsunami === 1,
8
+ alert: f.properties.alert,
9
+ lon: f.geometry?.coordinates?.[0] ?? null,
10
+ lat: f.geometry?.coordinates?.[1] ?? null,
11
+ });
12
+ /** M4.5+ earthquakes in the past day. */
13
+ export async function recentQuakes(ctx) {
14
+ const r = await ctx.http.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson', {
15
+ source: 'usgs',
16
+ ttlSec: 600,
17
+ });
18
+ return { data: r.data?.features?.map(toQuake) ?? null, prov: r.prov };
19
+ }
20
+ /** Earthquakes of at least `minMag` between two dates (YYYY-MM-DD), for looking back in time. */
21
+ export async function quakesBetween(ctx, start, end, minMag = 6) {
22
+ const r = await ctx.http.get(`https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=${start}&endtime=${end}&minmagnitude=${minMag}&orderby=magnitude`, { source: 'usgs', ttlSec: 30 * 86_400 });
23
+ return { data: r.data?.features?.map(toQuake) ?? null, prov: r.prov };
24
+ }
@@ -0,0 +1,10 @@
1
+ import { type Ctx } from '../core/context.js';
2
+ import type { Sourced } from '../core/types.js';
3
+ export interface Capture {
4
+ url: string;
5
+ timestamp: string;
6
+ }
7
+ /** Normalize a captured URL to host + path so the same page is not counted twice. */
8
+ export declare function normalizePath(raw: string): string | null;
9
+ /** HTML pages under `domain` captured by the Wayback Machine between two dates. */
10
+ export declare function captures(ctx: Ctx, domain: string, fromDaysAgo: number, toDaysAgo: number, limit?: number): Promise<Sourced<Capture[]>>;
@@ -0,0 +1,28 @@
1
+ import { DAY_MS, enc, ymd } from '../core/context.js';
2
+ const ASSET = /\.(css|js|mjs|png|jpe?g|gif|svg|webp|ico|woff2?|ttf|map|json|xml|txt|pdf|mp4|webm)$/i;
3
+ /** Normalize a captured URL to host + path so the same page is not counted twice. */
4
+ export function normalizePath(raw) {
5
+ try {
6
+ const u = new URL(raw.startsWith('http') ? raw : `https://${raw}`);
7
+ if (ASSET.test(u.pathname))
8
+ return null;
9
+ const path = u.pathname.replace(/\/+$/, '') || '/';
10
+ return `${u.hostname.replace(/^www\./, '').toLowerCase()}${path.toLowerCase()}`;
11
+ }
12
+ catch {
13
+ return null;
14
+ }
15
+ }
16
+ /** HTML pages under `domain` captured by the Wayback Machine between two dates. */
17
+ export async function captures(ctx, domain, fromDaysAgo, toDaysAgo, limit = 3000) {
18
+ const now = ctx.now().getTime();
19
+ const from = ymd(new Date(now - fromDaysAgo * DAY_MS));
20
+ const to = ymd(new Date(now - toDaysAgo * DAY_MS));
21
+ const url = `https://web.archive.org/cdx/search/cdx?url=${enc(`${domain}/*`)}&output=json&fl=original,timestamp` +
22
+ `&collapse=urlkey&filter=statuscode:200&filter=mimetype:text/html&from=${from}&to=${to}&limit=${limit}`;
23
+ const r = await ctx.http.get(url, { source: 'wayback', ttlSec: 12 * 3600, timeoutMs: 45_000, retries: 0 });
24
+ const rows = r.data?.slice(1) ?? null;
25
+ const data = rows?.map(([u, ts]) => ({ url: u ?? '', timestamp: ts ?? '' })) ?? null;
26
+ const truncated = rows && rows.length >= limit;
27
+ return { data, prov: truncated ? { ...r.prov, note: `sample: capped at ${limit} URLs` } : r.prov };
28
+ }
@@ -0,0 +1,19 @@
1
+ /** Web security footprints: urlscan.io scan history, Google Safe Browsing. */
2
+ import { type Ctx } from '../core/context.js';
3
+ import type { Sourced } from '../core/types.js';
4
+ export interface Scan {
5
+ url: string;
6
+ host: string;
7
+ time: string;
8
+ ip: string | null;
9
+ country: string | null;
10
+ server: string | null;
11
+ screenshot: string;
12
+ }
13
+ /** Recent urlscan.io scans of pages on the domain (any subdomain). */
14
+ export declare function urlscan(ctx: Ctx, domain: string, size?: number): Promise<Sourced<Scan[]>>;
15
+ /** URLs that Google Safe Browsing flags (malware, phishing, unwanted software). */
16
+ export declare function safeBrowsing(ctx: Ctx, urls: string[]): Promise<Sourced<{
17
+ url: string;
18
+ threat: string;
19
+ }[]>>;
@@ -0,0 +1,40 @@
1
+ /** Web security footprints: urlscan.io scan history, Google Safe Browsing. */
2
+ import { enc } from '../core/context.js';
3
+ import { key, noKey } from '../core/keys.js';
4
+ import { postJson } from './press.js';
5
+ /** Recent urlscan.io scans of pages on the domain (any subdomain). */
6
+ export async function urlscan(ctx, domain, size = 100) {
7
+ const k = key('URLSCAN_API_KEY');
8
+ const r = await ctx.http.get(`https://urlscan.io/api/v1/search/?q=${enc(`page.apexDomain:${domain}`)}&size=${size}`, { source: 'urlscan', ttlSec: 3 * 3600, ...(k ? { headers: { 'api-key': k } } : {}) });
9
+ return {
10
+ data: r.data?.results?.map((s) => ({
11
+ url: s.page?.url ?? '',
12
+ host: (s.page?.domain ?? '').toLowerCase(),
13
+ time: s.task?.time ?? '',
14
+ ip: s.page?.ip ?? null,
15
+ country: s.page?.country ?? null,
16
+ server: s.page?.server ?? null,
17
+ screenshot: s.screenshot ?? `https://urlscan.io/screenshots/${s._id}.png`,
18
+ })) ?? null,
19
+ prov: r.prov,
20
+ };
21
+ }
22
+ /** URLs that Google Safe Browsing flags (malware, phishing, unwanted software). */
23
+ export async function safeBrowsing(ctx, urls) {
24
+ const k = key('SAFE_BROWSING_KEY');
25
+ if (!k)
26
+ return noKey('safe-browsing', 'SAFE_BROWSING_KEY');
27
+ if (!urls.length)
28
+ return { data: [], prov: { source: 'safe-browsing', url: '', status: 'LIVE', fetchedAt: new Date().toISOString(), note: 'nothing to check' } };
29
+ const r = await postJson(ctx, `https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${k}`, {
30
+ client: { clientId: 'oculon', clientVersion: '0.1' },
31
+ threatInfo: {
32
+ threatTypes: ['MALWARE', 'SOCIAL_ENGINEERING', 'UNWANTED_SOFTWARE', 'POTENTIALLY_HARMFUL_APPLICATION'],
33
+ platformTypes: ['ANY_PLATFORM'],
34
+ threatEntryTypes: ['URL'],
35
+ threatEntries: urls.slice(0, 500).map((url) => ({ url })),
36
+ },
37
+ }, {}, 'safe-browsing');
38
+ // The request URL carries the key; keep it out of provenance.
39
+ return { data: r.data ? (r.data.matches ?? []).map((m) => ({ url: m.threat.url, threat: m.threatType })) : null, prov: { ...r.prov, url: 'https://safebrowsing.googleapis.com/v4/threatMatches:find' } };
40
+ }
@@ -0,0 +1,11 @@
1
+ /** Populated places near a point, from Wikidata (keyless SPARQL). */
2
+ import { type Ctx } from '../core/context.js';
3
+ import type { Sourced } from '../core/types.js';
4
+ export interface Settlement {
5
+ name: string;
6
+ population: number;
7
+ distanceKm: number;
8
+ qid: string;
9
+ }
10
+ /** Cities and towns within `radiusKm`, largest first; duplicate places keep their largest population figure. */
11
+ export declare function settlementsNear(ctx: Ctx, lat: number, lon: number, radiusKm: number, limit?: number): Promise<Sourced<Settlement[]>>;
@@ -0,0 +1,30 @@
1
+ /** Populated places near a point, from Wikidata (keyless SPARQL). */
2
+ import { enc } from '../core/context.js';
3
+ /** Cities and towns within `radiusKm`, largest first; duplicate places keep their largest population figure. */
4
+ export async function settlementsNear(ctx, lat, lon, radiusKm, limit = 40) {
5
+ const q = `SELECT ?place ?placeLabel ?pop ?dist WHERE {
6
+ SERVICE wikibase:around { ?place wdt:P625 ?loc . bd:serviceParam wikibase:center "Point(${lon.toFixed(4)} ${lat.toFixed(4)})"^^geo:wktLiteral . bd:serviceParam wikibase:radius "${Math.round(radiusKm)}" . bd:serviceParam wikibase:distance ?dist . }
7
+ ?place wdt:P1082 ?pop . ?place wdt:P31 ?type .
8
+ VALUES ?type { wd:Q515 wd:Q1549591 wd:Q3957 wd:Q532 wd:Q1093829 wd:Q200250 wd:Q1637706 wd:Q5119 }
9
+ SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
10
+ } ORDER BY DESC(?pop) LIMIT ${limit * 2}`;
11
+ const r = await ctx.http.get(`https://query.wikidata.org/sparql?query=${enc(q)}`, { source: 'wikidata', ttlSec: 7 * 86_400, timeoutMs: 60_000, headers: { accept: 'application/sparql-results+json' } });
12
+ if (!r.data?.results)
13
+ return { data: null, prov: r.prov };
14
+ const best = new Map();
15
+ for (const b of r.data.results.bindings) {
16
+ const qid = b.place.value.split('/').pop();
17
+ const s = { name: b.placeLabel.value, population: Number(b.pop.value), distanceKm: Math.round(Number(b.dist.value)), qid };
18
+ if (!Number.isFinite(s.population))
19
+ continue;
20
+ const prev = best.get(qid);
21
+ if (!prev || s.population > prev.population)
22
+ best.set(qid, s);
23
+ }
24
+ // Same-name entries (a city and its municipality) would double count; keep one per name.
25
+ const byName = new Map();
26
+ for (const s of best.values())
27
+ if (!byName.has(s.name) || byName.get(s.name).population < s.population)
28
+ byName.set(s.name, s);
29
+ return { data: [...byName.values()].sort((a, b) => b.population - a.population).slice(0, limit), prov: r.prov };
30
+ }
@@ -0,0 +1,28 @@
1
+ import { type Ctx } from '../core/context.js';
2
+ import type { Sourced } from '../core/types.js';
3
+ export interface DailyViews {
4
+ date: string;
5
+ views: number;
6
+ }
7
+ export interface TopArticle {
8
+ article: string;
9
+ views: number;
10
+ rank: number;
11
+ }
12
+ /** Find the best-matching English Wikipedia article title for a free-text query. */
13
+ export declare function resolveTitle(ctx: Ctx, query: string): Promise<Sourced<string>>;
14
+ /** Daily human pageviews for an article, ending yesterday (UTC). */
15
+ export declare function dailyViews(ctx: Ctx, title: string, days?: number): Promise<Sourced<DailyViews[]>>;
16
+ export interface Summary {
17
+ title: string;
18
+ description: string | null;
19
+ extract: string;
20
+ url: string;
21
+ }
22
+ export declare function summary(ctx: Ctx, title: string): Promise<Sourced<Summary>>;
23
+ export declare function isNoiseArticle(article: string): boolean;
24
+ /** Most-viewed articles for the latest day that has published data (yesterday, else the day before). */
25
+ export declare function topArticles(ctx: Ctx): Promise<Sourced<{
26
+ date: string;
27
+ articles: TopArticle[];
28
+ }>>;
@@ -0,0 +1,52 @@
1
+ import { DAY_MS, enc, ymd } from '../core/context.js';
2
+ const WM = 'https://wikimedia.org/api/rest_v1/metrics/pageviews';
3
+ /** Find the best-matching English Wikipedia article title for a free-text query. */
4
+ export async function resolveTitle(ctx, query) {
5
+ const url = `https://en.wikipedia.org/w/api.php?action=opensearch&format=json&limit=1&namespace=0&search=${enc(query)}`;
6
+ const r = await ctx.http.get(url, { source: 'wikipedia', ttlSec: 86_400 });
7
+ return { data: r.data?.[1]?.[0] ?? null, prov: r.prov };
8
+ }
9
+ /** Daily human pageviews for an article, ending yesterday (UTC). */
10
+ export async function dailyViews(ctx, title, days = 56) {
11
+ const end = new Date(ctx.now().getTime() - DAY_MS);
12
+ const start = new Date(end.getTime() - (days - 1) * DAY_MS);
13
+ const art = enc(title.replace(/ /g, '_'));
14
+ const url = `${WM}/per-article/en.wikipedia/all-access/user/${art}/daily/${ymd(start)}00/${ymd(end)}00`;
15
+ const r = await ctx.http.get(url, {
16
+ source: 'wikimedia-pageviews',
17
+ ttlSec: 3 * 3600,
18
+ });
19
+ const items = r.data?.items?.map((i) => ({ date: i.timestamp.slice(0, 8), views: i.views })) ?? null;
20
+ return { data: items, prov: r.prov };
21
+ }
22
+ export async function summary(ctx, title) {
23
+ const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${enc(title.replace(/ /g, '_'))}`;
24
+ const r = await ctx.http.get(url, {
25
+ source: 'wikipedia',
26
+ ttlSec: 86_400,
27
+ });
28
+ const d = r.data;
29
+ return {
30
+ data: d?.title ? { title: d.title, description: d.description ?? null, extract: d.extract ?? '', url: d.content_urls?.desktop?.page ?? `https://en.wikipedia.org/wiki/${enc(title)}` } : null,
31
+ prov: r.prov,
32
+ };
33
+ }
34
+ const NOISE = /^(Main_Page|Special:|Wikipedia:|Portal:|File:|Help:|Category:|Template:|Talk:|User:|-$|Cleopatra$|Undefined$|Search$)/i;
35
+ export function isNoiseArticle(article) {
36
+ return NOISE.test(article) || /\.(php|html?)$/i.test(article);
37
+ }
38
+ /** Most-viewed articles for the latest day that has published data (yesterday, else the day before). */
39
+ export async function topArticles(ctx) {
40
+ let last;
41
+ for (const back of [1, 2]) {
42
+ const d = new Date(ctx.now().getTime() - back * DAY_MS);
43
+ const [y, m, dd] = d.toISOString().slice(0, 10).split('-');
44
+ const url = `${WM}/top/en.wikipedia/all-access/${y}/${m}/${dd}`;
45
+ const r = await ctx.http.get(url, { source: 'wikimedia-top', ttlSec: 3 * 3600 });
46
+ const articles = r.data?.items?.[0]?.articles;
47
+ last = { data: articles ? { date: ymd(d), articles } : null, prov: r.prov };
48
+ if (articles)
49
+ return last;
50
+ }
51
+ return last;
52
+ }
@@ -0,0 +1,104 @@
1
+ import { Oculon, type Signal } from './index.js';
2
+ import { type Delivery } from './modules/personal.js';
3
+ export interface BotOptions {
4
+ token: string;
5
+ /** Chat ids allowed to use the bot. */
6
+ allowedChats: string[];
7
+ oculon?: Oculon;
8
+ fetch?: typeof fetch;
9
+ /** Minimum score for pushed alerts. */
10
+ alertMinScore?: number;
11
+ /** Minutes between background scans for alerts; 0 disables. */
12
+ watchMinutes?: number;
13
+ /** Anyone may use the public commands (rate limited); allowlisted chats keep every command. Default OCULON_TELEGRAM_PUBLIC=1. */
14
+ publicMode?: boolean;
15
+ log?: (m: string) => void;
16
+ }
17
+ interface Update {
18
+ update_id: number;
19
+ message?: {
20
+ chat: {
21
+ id: number;
22
+ type: string;
23
+ };
24
+ text?: string;
25
+ caption?: string;
26
+ from?: {
27
+ username?: string;
28
+ };
29
+ };
30
+ callback_query?: {
31
+ id: string;
32
+ data?: string;
33
+ from: {
34
+ id: number;
35
+ };
36
+ message?: {
37
+ chat: {
38
+ id: number;
39
+ };
40
+ message_id: number;
41
+ };
42
+ };
43
+ }
44
+ export declare const esc: (s: unknown) => string;
45
+ export declare function formatSignal(s: Signal): string;
46
+ /** Commands anyone may use when the bot is public. Everything else stays with the allowlisted chats. */
47
+ export declare const PUBLIC_COMMANDS: Set<string>;
48
+ export declare const PUBLIC_HELP: string;
49
+ /** Help text without the paused on-chain commands (they return with OCULON_ONCHAIN=1). */
50
+ export declare function helpHtml(): string;
51
+ export declare const HELP: string;
52
+ export declare class TelegramBot {
53
+ private readonly opts;
54
+ private offset;
55
+ private running;
56
+ private scheduler;
57
+ readonly oculon: Oculon;
58
+ private readonly doFetch;
59
+ private readonly log;
60
+ constructor(opts: BotOptions);
61
+ /** Bot API call. Never logs the URL, because it contains the token. */
62
+ call<T = unknown>(method: string, body: Record<string, unknown> | FormData): Promise<T>;
63
+ /** Send HTML text, split under Telegram's 4096-character limit at line breaks. */
64
+ send(chatId: string | number, html: string): Promise<void>;
65
+ sendDocument(chatId: string | number, file: string, caption: string): Promise<void>;
66
+ /** Send a report file to every subscribed chat. */
67
+ pushReport(file: string, caption: string): Promise<number>;
68
+ sendPhoto(chatId: string | number, file: string, caption: string): Promise<void>;
69
+ allowed(chatId: string): boolean;
70
+ get isPublic(): boolean;
71
+ private usage;
72
+ /** Per chat: 30 commands an hour, 8 of them heavy. */
73
+ rateOk(chatId: string, heavy: boolean): boolean;
74
+ /** Changes found by personal monitors, sent to the Telegram chats that own them. */
75
+ pushPersonal(deliveries: Delivery[]): Promise<number>;
76
+ /** Turn one command into a reply. Exposed for tests. */
77
+ respond(chatId: string, text: string, ctx?: {
78
+ owner?: boolean;
79
+ }): Promise<{
80
+ html?: string;
81
+ photo?: {
82
+ file: string;
83
+ caption: string;
84
+ };
85
+ document?: {
86
+ file: string;
87
+ caption: string;
88
+ };
89
+ }>;
90
+ /** A tap on Useful / Noise under an alert. */
91
+ handleCallback(q: NonNullable<Update['callback_query']>): Promise<void>;
92
+ handle(u: Update): Promise<void>;
93
+ /** Push signals the chat has not seen yet. Returns how many were sent. */
94
+ pushAlerts(signals: Signal[]): Promise<number>;
95
+ scanAndAlert(): Promise<number>;
96
+ pollOnce(): Promise<number>;
97
+ start(): Promise<void>;
98
+ stop(): void;
99
+ /** Send the daily brief to every subscribed chat. Markdown is reduced to Telegram HTML (bold and links). */
100
+ pushBrief(markdown: string): Promise<number>;
101
+ }
102
+ /** Minimal Markdown to Telegram HTML: headings and **bold** to <b>, [text](url) to links; everything else escaped. */
103
+ export declare function markdownToTelegram(md: string): string;
104
+ export {};