@cliwant/mcp-sam-gov 1.4.0 → 1.6.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 -21
- package/README.ja.md +240 -231
- package/README.ko.md +240 -231
- package/README.md +725 -706
- package/dist/cbp-border.d.ts +51 -0
- package/dist/cbp-border.d.ts.map +1 -0
- package/dist/cbp-border.js +123 -0
- package/dist/cbp-border.js.map +1 -0
- package/dist/datagov-catalog.d.ts.map +1 -1
- package/dist/datagov-catalog.js +16 -2
- package/dist/datagov-catalog.js.map +1 -1
- package/dist/ecfr.d.ts +2 -2
- package/dist/ecfr.d.ts.map +1 -1
- package/dist/ecfr.js +24 -10
- package/dist/ecfr.js.map +1 -1
- package/dist/edgar.d.ts.map +1 -1
- package/dist/edgar.js +26 -6
- package/dist/edgar.js.map +1 -1
- package/dist/epa-envirofacts.d.ts.map +1 -1
- package/dist/epa-envirofacts.js +14 -1
- package/dist/epa-envirofacts.js.map +1 -1
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -1
- package/dist/far.d.ts.map +1 -1
- package/dist/far.js +3 -1
- package/dist/far.js.map +1 -1
- package/dist/federal-register.d.ts +2 -2
- package/dist/federal-register.d.ts.map +1 -1
- package/dist/federal-register.js +26 -10
- package/dist/federal-register.js.map +1 -1
- package/dist/feedback.d.ts +64 -0
- package/dist/feedback.d.ts.map +1 -0
- package/dist/feedback.js +131 -0
- package/dist/feedback.js.map +1 -0
- package/dist/fema.d.ts +36 -0
- package/dist/fema.d.ts.map +1 -1
- package/dist/fema.js +124 -0
- package/dist/fema.js.map +1 -1
- package/dist/gov-domains.d.ts +66 -0
- package/dist/gov-domains.d.ts.map +1 -0
- package/dist/gov-domains.js +211 -0
- package/dist/gov-domains.js.map +1 -0
- package/dist/nist-controls.d.ts +48 -0
- package/dist/nist-controls.d.ts.map +1 -0
- package/dist/nist-controls.js +174 -0
- package/dist/nist-controls.js.map +1 -0
- package/dist/nws-weather.d.ts +57 -0
- package/dist/nws-weather.d.ts.map +1 -0
- package/dist/nws-weather.js +131 -0
- package/dist/nws-weather.js.map +1 -0
- package/dist/openfda-drugsfda.d.ts +72 -0
- package/dist/openfda-drugsfda.d.ts.map +1 -0
- package/dist/openfda-drugsfda.js +230 -0
- package/dist/openfda-drugsfda.js.map +1 -0
- package/dist/openfda.d.ts.map +1 -1
- package/dist/openfda.js +31 -8
- package/dist/openfda.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +374 -11
- package/dist/server.js.map +1 -1
- package/dist/treasury.d.ts +2 -0
- package/dist/treasury.d.ts.map +1 -1
- package/dist/treasury.js +7 -0
- package/dist/treasury.js.map +1 -1
- package/dist/usaspending.d.ts +32 -1
- package/dist/usaspending.d.ts.map +1 -1
- package/dist/usaspending.js +143 -16
- package/dist/usaspending.js.map +1 -1
- package/package.json +111 -111
- package/src/attachments.ts +652 -652
- package/src/bea.ts +372 -372
- package/src/bls.ts +1943 -1943
- package/src/cache.ts +73 -73
- package/src/cbp-border.ts +177 -0
- package/src/census-economic.ts +431 -431
- package/src/census.ts +735 -735
- package/src/ckan.ts +495 -495
- package/src/clinicaltrials.ts +923 -923
- package/src/cms-facility.ts +379 -379
- package/src/cms-hospital.ts +344 -344
- package/src/cms-supplier.ts +527 -527
- package/src/cms-utilization.ts +389 -389
- package/src/cms.ts +634 -634
- package/src/coerce.ts +47 -47
- package/src/courtlistener.ts +465 -465
- package/src/cpsc.ts +333 -333
- package/src/datagov-catalog.ts +312 -296
- package/src/datagov.ts +907 -907
- package/src/datagovKey.ts +68 -68
- package/src/datasource.ts +721 -721
- package/src/disclosure.ts +61 -61
- package/src/dol.ts +515 -515
- package/src/ecfr.ts +248 -231
- package/src/echo.ts +496 -496
- package/src/edgar.ts +3046 -3014
- package/src/epa-envirofacts.ts +358 -342
- package/src/errors.ts +324 -303
- package/src/fac.ts +529 -529
- package/src/far.ts +1009 -1007
- package/src/fdic.ts +2052 -2052
- package/src/federal-register.ts +725 -706
- package/src/feedback.ts +160 -0
- package/src/fema.ts +680 -541
- package/src/fpds.ts +620 -620
- package/src/fred.ts +464 -464
- package/src/gao.ts +744 -744
- package/src/gov-domains.ts +237 -0
- package/src/govinfo.ts +497 -497
- package/src/grants.ts +290 -290
- package/src/gsa-csv.ts +992 -992
- package/src/gsa-perdiem.ts +361 -361
- package/src/integrity.ts +928 -928
- package/src/keys.ts +268 -268
- package/src/lda.ts +385 -385
- package/src/meta.ts +292 -292
- package/src/nhtsa.ts +352 -352
- package/src/nih.ts +375 -375
- package/src/nist-controls.ts +219 -0
- package/src/nonprofit.ts +460 -460
- package/src/nppes.ts +834 -834
- package/src/nsf.ts +706 -706
- package/src/nvd.ts +1124 -1124
- package/src/nws-weather.ts +167 -0
- package/src/ofac.ts +1166 -1166
- package/src/openfda-device.ts +356 -356
- package/src/openfda-drugsfda.ts +313 -0
- package/src/openfda.ts +518 -495
- package/src/pricing.ts +1075 -1075
- package/src/sam-gov/client.ts +774 -774
- package/src/sam-gov/index.ts +32 -32
- package/src/sam-gov/types.ts +152 -152
- package/src/sba.ts +357 -357
- package/src/server.ts +6688 -6297
- package/src/snapshot.ts +223 -223
- package/src/socrata.ts +532 -532
- package/src/treasury.ts +582 -575
- package/src/usaspending.ts +2852 -2680
- package/src/usitc.ts +420 -420
package/src/ecfr.ts
CHANGED
|
@@ -1,231 +1,248 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* eCFR (Electronic Code of Federal Regulations) wrappers (keyless).
|
|
3
|
-
*
|
|
4
|
-
* eCFR is the up-to-date version of the CFR — Title 48 = FAR (Federal
|
|
5
|
-
* Acquisition Regulation), Title 2 = Federal financial assistance, etc.
|
|
6
|
-
* For a federal contractor, eCFR is the primary source for regulation
|
|
7
|
-
* text the agent should quote when answering compliance questions.
|
|
8
|
-
*
|
|
9
|
-
* Endpoints:
|
|
10
|
-
* - /versioner/v1/titles.json — list 50 CFR titles + last-amended dates
|
|
11
|
-
* - /search/v1/results — full-text search across the entire CFR
|
|
12
|
-
*
|
|
13
|
-
* Both keyless. Documented at https://www.ecfr.gov/developers/.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { fetchWithRetry } from "./errors.js";
|
|
17
|
-
import { driftError } from "./datasource.js";
|
|
18
|
-
import { memoize } from "./cache.js";
|
|
19
|
-
import { withMeta } from "./meta.js";
|
|
20
|
-
|
|
21
|
-
const ECFR = "https://www.ecfr.gov/api";
|
|
22
|
-
|
|
23
|
-
// eCFR's /search/v1/results `meta.total_count` is capped by Elasticsearch's
|
|
24
|
-
// `index.max_result_window` (live-verified 10,000). A total AT OR ABOVE this
|
|
25
|
-
// sentinel is a LOWER BOUND, not an exact count — see the totalIsLowerBound
|
|
26
|
-
// wiring in `search` (D1). A genuine count below this stays exact.
|
|
27
|
-
const ECFR_TOTAL_COUNT_CAP = 10000;
|
|
28
|
-
|
|
29
|
-
async function fetchJson<T>(url: string): Promise<T> {
|
|
30
|
-
const r = await fetchWithRetry(
|
|
31
|
-
url,
|
|
32
|
-
{
|
|
33
|
-
headers: { Accept: "application/json" },
|
|
34
|
-
signal: AbortSignal.timeout(15_000),
|
|
35
|
-
},
|
|
36
|
-
`ecfr:${url.split("/api/")[1] ?? url}`,
|
|
37
|
-
);
|
|
38
|
-
return (await r.json()) as T;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function listTitles() {
|
|
42
|
-
// 50 CFR titles change very infrequently. Cache aggressively (5 min).
|
|
43
|
-
return memoize("ecfr:titles", async () => {
|
|
44
|
-
type Resp = {
|
|
45
|
-
titles?: {
|
|
46
|
-
number?: number;
|
|
47
|
-
name?: string;
|
|
48
|
-
latest_amended_on?: string;
|
|
49
|
-
latest_issue_date?: string;
|
|
50
|
-
up_to_date_as_of?: string;
|
|
51
|
-
reserved?: boolean;
|
|
52
|
-
}[];
|
|
53
|
-
};
|
|
54
|
-
const json = await fetchJson<Resp>(`${ECFR}/versioner/v1/titles.json`);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
//
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
totalAvailable
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
1
|
+
/**
|
|
2
|
+
* eCFR (Electronic Code of Federal Regulations) wrappers (keyless).
|
|
3
|
+
*
|
|
4
|
+
* eCFR is the up-to-date version of the CFR — Title 48 = FAR (Federal
|
|
5
|
+
* Acquisition Regulation), Title 2 = Federal financial assistance, etc.
|
|
6
|
+
* For a federal contractor, eCFR is the primary source for regulation
|
|
7
|
+
* text the agent should quote when answering compliance questions.
|
|
8
|
+
*
|
|
9
|
+
* Endpoints:
|
|
10
|
+
* - /versioner/v1/titles.json — list 50 CFR titles + last-amended dates
|
|
11
|
+
* - /search/v1/results — full-text search across the entire CFR
|
|
12
|
+
*
|
|
13
|
+
* Both keyless. Documented at https://www.ecfr.gov/developers/.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { fetchWithRetry } from "./errors.js";
|
|
17
|
+
import { driftError } from "./datasource.js";
|
|
18
|
+
import { memoize } from "./cache.js";
|
|
19
|
+
import { withMeta } from "./meta.js";
|
|
20
|
+
|
|
21
|
+
const ECFR = "https://www.ecfr.gov/api";
|
|
22
|
+
|
|
23
|
+
// eCFR's /search/v1/results `meta.total_count` is capped by Elasticsearch's
|
|
24
|
+
// `index.max_result_window` (live-verified 10,000). A total AT OR ABOVE this
|
|
25
|
+
// sentinel is a LOWER BOUND, not an exact count — see the totalIsLowerBound
|
|
26
|
+
// wiring in `search` (D1). A genuine count below this stays exact.
|
|
27
|
+
const ECFR_TOTAL_COUNT_CAP = 10000;
|
|
28
|
+
|
|
29
|
+
async function fetchJson<T>(url: string): Promise<T> {
|
|
30
|
+
const r = await fetchWithRetry(
|
|
31
|
+
url,
|
|
32
|
+
{
|
|
33
|
+
headers: { Accept: "application/json" },
|
|
34
|
+
signal: AbortSignal.timeout(15_000),
|
|
35
|
+
},
|
|
36
|
+
`ecfr:${url.split("/api/")[1] ?? url}`,
|
|
37
|
+
);
|
|
38
|
+
return (await r.json()) as T;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function listTitles() {
|
|
42
|
+
// 50 CFR titles change very infrequently. Cache aggressively (5 min).
|
|
43
|
+
return memoize("ecfr:titles", async () => {
|
|
44
|
+
type Resp = {
|
|
45
|
+
titles?: {
|
|
46
|
+
number?: number;
|
|
47
|
+
name?: string;
|
|
48
|
+
latest_amended_on?: string;
|
|
49
|
+
latest_issue_date?: string;
|
|
50
|
+
up_to_date_as_of?: string;
|
|
51
|
+
reserved?: boolean;
|
|
52
|
+
}[];
|
|
53
|
+
};
|
|
54
|
+
const json = await fetchJson<Resp>(`${ECFR}/versioner/v1/titles.json`);
|
|
55
|
+
const titles = (json.titles ?? []).map((t) => ({
|
|
56
|
+
number: t.number ?? 0,
|
|
57
|
+
name: t.name ?? "",
|
|
58
|
+
latestAmendedOn: t.latest_amended_on,
|
|
59
|
+
latestIssueDate: t.latest_issue_date,
|
|
60
|
+
upToDateAsOf: t.up_to_date_as_of,
|
|
61
|
+
reserved: !!t.reserved,
|
|
62
|
+
}));
|
|
63
|
+
// Carry the honesty envelope every other tool has (dogfooding 2026-07-15: this
|
|
64
|
+
// list tool previously returned a bare {titles} with no _meta). The titles
|
|
65
|
+
// endpoint returns the COMPLETE canonical CFR title set in one response — no
|
|
66
|
+
// pagination, no filter — so the count IS the total and complete derives true.
|
|
67
|
+
return withMeta(
|
|
68
|
+
{ titles },
|
|
69
|
+
{
|
|
70
|
+
source: "ecfr.gov/api (versioner/v1/titles)",
|
|
71
|
+
keylessMode: true,
|
|
72
|
+
returned: titles.length,
|
|
73
|
+
totalAvailable: titles.length,
|
|
74
|
+
truncated: false,
|
|
75
|
+
filtersApplied: [],
|
|
76
|
+
filtersDropped: [],
|
|
77
|
+
notes: [
|
|
78
|
+
"Complete canonical list of all CFR titles — the endpoint returns every title in a single response (no pagination or filtering).",
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function search(args: {
|
|
86
|
+
query: string;
|
|
87
|
+
titleNumber?: number;
|
|
88
|
+
chapter?: number;
|
|
89
|
+
perPage?: number;
|
|
90
|
+
}) {
|
|
91
|
+
const url = new URL(`${ECFR}/search/v1/results`);
|
|
92
|
+
url.searchParams.set("query", args.query);
|
|
93
|
+
url.searchParams.set("per_page", String(args.perPage ?? 5));
|
|
94
|
+
if (args.titleNumber) {
|
|
95
|
+
// eCFR search filter: hierarchy[title]=N (NOT just title=N — that's
|
|
96
|
+
// an "unpermitted parameter" error from the eCFR API).
|
|
97
|
+
url.searchParams.set("hierarchy[title]", String(args.titleNumber));
|
|
98
|
+
}
|
|
99
|
+
// Optional chapter filter (additive; existing ecfr_search callers pass none).
|
|
100
|
+
// Within Title 48: chapter 1 = FAR, chapter 2 = DFARS, chapter 5 = GSAM, etc.
|
|
101
|
+
// This is what lets far_search scope to FAR/DFARS and keep GSAM/agency
|
|
102
|
+
// supplements out server-side. Same hierarchy[…] contract as the title filter.
|
|
103
|
+
if (args.chapter !== undefined) {
|
|
104
|
+
url.searchParams.set("hierarchy[chapter]", String(args.chapter));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type Resp = {
|
|
108
|
+
results?: {
|
|
109
|
+
starts_on?: string;
|
|
110
|
+
ends_on?: string | null;
|
|
111
|
+
type?: string;
|
|
112
|
+
hierarchy?: {
|
|
113
|
+
title?: string;
|
|
114
|
+
chapter?: string;
|
|
115
|
+
subchapter?: string;
|
|
116
|
+
part?: string;
|
|
117
|
+
subpart?: string;
|
|
118
|
+
section?: string;
|
|
119
|
+
};
|
|
120
|
+
hierarchy_headings?: Record<string, string | null>;
|
|
121
|
+
headings?: Record<string, string | null>;
|
|
122
|
+
full_text_excerpt?: string;
|
|
123
|
+
score?: number;
|
|
124
|
+
}[];
|
|
125
|
+
meta?: {
|
|
126
|
+
current_page?: number;
|
|
127
|
+
total_pages?: number;
|
|
128
|
+
total_count?: number;
|
|
129
|
+
max_score?: number;
|
|
130
|
+
description?: string;
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const json = await fetchJson<Resp>(url.toString());
|
|
134
|
+
// F6 (P2 empty-vs-outage): a 200 whose `results` is PRESENT-but-non-array — or
|
|
135
|
+
// a body carrying NEITHER a `results` array NOR a `meta` object — is drift / an
|
|
136
|
+
// unexpected shape, NOT a genuine no-match. Throw (schema_drift) rather than
|
|
137
|
+
// letting `(json.results ?? []).map` coalesce it into a fake AUTHORITATIVE empty.
|
|
138
|
+
// A GENUINE empty (results:[] with meta.total_count:0) flows through honestly
|
|
139
|
+
// below. (eCFR normally signals errors via HTTP status caught by fetchWithRetry;
|
|
140
|
+
// this closes the previously-unhandled + untested 200-body drift path.)
|
|
141
|
+
if (json.results !== undefined && !Array.isArray(json.results)) {
|
|
142
|
+
throw driftError(
|
|
143
|
+
"ecfr.gov",
|
|
144
|
+
"eCFR search returned HTTP 200 but `results` is not an array — treating it as schema drift, NOT an empty result set.",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (json.results === undefined && json.meta === undefined) {
|
|
148
|
+
throw driftError(
|
|
149
|
+
"ecfr.gov",
|
|
150
|
+
"eCFR search returned HTTP 200 with neither a `results` array nor a `meta` object — an unexpected shape; treating it as schema drift, NOT an empty result set.",
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
const data = {
|
|
154
|
+
results: (json.results ?? []).map((r) => ({
|
|
155
|
+
type: r.type ?? "",
|
|
156
|
+
title: r.hierarchy?.title ?? "",
|
|
157
|
+
chapter: r.hierarchy?.chapter,
|
|
158
|
+
part: r.hierarchy?.part,
|
|
159
|
+
subpart: r.hierarchy?.subpart,
|
|
160
|
+
section: r.hierarchy?.section,
|
|
161
|
+
headingPath: Object.values(r.hierarchy_headings ?? {})
|
|
162
|
+
.filter(Boolean)
|
|
163
|
+
.join(" › "),
|
|
164
|
+
excerpt: stripHtml(r.full_text_excerpt ?? ""),
|
|
165
|
+
score: r.score ?? 0,
|
|
166
|
+
// Stable ecfr.gov URL pattern from the hierarchy
|
|
167
|
+
ecfrUrl: r.hierarchy
|
|
168
|
+
? buildEcfrUrl(r.hierarchy)
|
|
169
|
+
: "",
|
|
170
|
+
effectiveOn: r.starts_on ?? "",
|
|
171
|
+
// Additive: the version's end date. null = the CURRENT (in-force) version;
|
|
172
|
+
// a non-null date = a HISTORICAL version. eCFR search returns ~5 versions
|
|
173
|
+
// per section; existing ecfr_search callers simply ignore this extra field,
|
|
174
|
+
// while far_search uses it to collapse historical dups to the current one.
|
|
175
|
+
endsOn: r.ends_on ?? null,
|
|
176
|
+
})),
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Truthful `_meta` (spec §1.2 A6, §2.3). eCFR returns a hit count in
|
|
180
|
+
// `meta.total_count`, BUT that count is capped by Elasticsearch's
|
|
181
|
+
// `index.max_result_window` at 10,000 (a real ceiling — unlike a genuine
|
|
182
|
+
// small count, a value at/above the cap is a LOWER BOUND, not an exact
|
|
183
|
+
// total). Below the cap `totalAvailable` is exact (the AI can tell a top-N
|
|
184
|
+
// slice from the full match set); at/above the cap we flag
|
|
185
|
+
// `totalIsLowerBound:true` + a note so a broad query that truly matches
|
|
186
|
+
// >10,000 sections is NOT reported as if exactly 10,000 (D1). A6: echo the
|
|
187
|
+
// applied title scope so the AI can VERIFY it searched the intended corpus —
|
|
188
|
+
// Title 48 (FAR) vs every CFR title — rather than silently trusting a filter
|
|
189
|
+
// that could return cross-title results if the eCFR param contract ever changes.
|
|
190
|
+
const returned = data.results.length;
|
|
191
|
+
const totalAvailable =
|
|
192
|
+
typeof json.meta?.total_count === "number" ? json.meta.total_count : null;
|
|
193
|
+
// eCFR's search total_count saturates at the Elasticsearch max_result_window
|
|
194
|
+
// (live-verified 10,000). Use `>= cap` (not `=== cap`) so a total AT OR ABOVE
|
|
195
|
+
// the ceiling is treated as a lower bound — strictly safe if the window were
|
|
196
|
+
// ever configured higher. Mirrors federal-register.ts's FR_COUNT_CAP and
|
|
197
|
+
// edgar.ts's FTS_WINDOW.
|
|
198
|
+
const totalIsLowerBound =
|
|
199
|
+
totalAvailable !== null && totalAvailable >= ECFR_TOTAL_COUNT_CAP;
|
|
200
|
+
const scopeNote =
|
|
201
|
+
args.titleNumber !== undefined
|
|
202
|
+
? `searched CFR Title ${args.titleNumber}${
|
|
203
|
+
args.titleNumber === 48 ? " (FAR — Federal Acquisition Regulation)" : ""
|
|
204
|
+
} only`
|
|
205
|
+
: "searched all CFR titles (no title filter applied)";
|
|
206
|
+
const notes = [scopeNote];
|
|
207
|
+
if (totalIsLowerBound) {
|
|
208
|
+
notes.push(
|
|
209
|
+
`eCFR caps total_count at ${ECFR_TOTAL_COUNT_CAP} (Elasticsearch index.max_result_window); totalAvailable is a LOWER BOUND — the true match count may be higher and is UNKNOWN. See totalIsLowerBound. Narrow by title/chapter/date for an exact count.`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return withMeta(data, {
|
|
213
|
+
source: "ecfr.gov/api (search/v1)",
|
|
214
|
+
keylessMode: true,
|
|
215
|
+
returned,
|
|
216
|
+
totalAvailable,
|
|
217
|
+
// Explicit boolean (not conditional): a below-cap count is DEFINITIVELY
|
|
218
|
+
// exact (totalIsLowerBound:false), an at/above-cap count is a lower bound
|
|
219
|
+
// (true). The AI can trust `false` as "this is the real total".
|
|
220
|
+
totalIsLowerBound,
|
|
221
|
+
truncated:
|
|
222
|
+
totalAvailable !== null ? returned < totalAvailable : undefined,
|
|
223
|
+
filtersApplied: args.titleNumber !== undefined ? ["titleNumber"] : [],
|
|
224
|
+
filtersDropped: [],
|
|
225
|
+
fieldsUnavailable: [],
|
|
226
|
+
notes,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function stripHtml(s: string): string {
|
|
231
|
+
return s
|
|
232
|
+
.replace(/<[^>]+>/g, "")
|
|
233
|
+
.replace(/\s+/g, " ")
|
|
234
|
+
.trim();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function buildEcfrUrl(h: {
|
|
238
|
+
title?: string;
|
|
239
|
+
chapter?: string;
|
|
240
|
+
part?: string;
|
|
241
|
+
section?: string;
|
|
242
|
+
}): string {
|
|
243
|
+
const base = `https://www.ecfr.gov/current/title-${h.title}`;
|
|
244
|
+
if (h.section) return `${base}/section-${h.section}`;
|
|
245
|
+
if (h.part) return `${base}/part-${h.part}`;
|
|
246
|
+
if (h.chapter) return `${base}/chapter-${h.chapter}`;
|
|
247
|
+
return base;
|
|
248
|
+
}
|