langchain-stripe-loader 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +83 -9
- package/dist/index.cjs +602 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +185 -0
- package/dist/index.d.ts +185 -3
- package/dist/index.js +580 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -19
- package/dist/StripeComLoader.d.ts +0 -44
- package/dist/StripeComLoader.d.ts.map +0 -1
- package/dist/StripeDocsLoader.d.ts +0 -34
- package/dist/StripeDocsLoader.d.ts.map +0 -1
- package/dist/StripeDocsLoader.spec.d.ts +0 -2
- package/dist/StripeDocsLoader.spec.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.spec.d.ts +0 -2
- package/dist/index.spec.d.ts.map +0 -1
- package/dist/langchain-stripe-loader.cjs +0 -30
- package/dist/langchain-stripe-loader.cjs.map +0 -1
- package/dist/langchain-stripe-loader.js +0 -6922
- package/dist/langchain-stripe-loader.js.map +0 -1
- package/dist/utils/index.d.ts +0 -3
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/url-filter.d.ts +0 -26
- package/dist/utils/url-filter.d.ts.map +0 -1
- package/dist/utils/url-filter.spec.d.ts +0 -2
- package/dist/utils/url-filter.spec.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -13
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.spec.d.ts +0 -2
- package/dist/utils.spec.d.ts.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
StripeChangelogDocumentLoader: () => StripeChangelogDocumentLoader,
|
|
24
|
+
StripeComDocumentLoader: () => StripeComDocumentLoader,
|
|
25
|
+
StripeDocsDocumentLoader: () => StripeDocsDocumentLoader
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/StripeDocsLoader.ts
|
|
30
|
+
var import_stripe_loaders_core3 = require("stripe-loaders-core");
|
|
31
|
+
var import_base = require("@langchain/core/document_loaders/base");
|
|
32
|
+
var import_documents = require("@langchain/core/documents");
|
|
33
|
+
var import_node_html_markdown = require("node-html-markdown");
|
|
34
|
+
|
|
35
|
+
// src/utils.ts
|
|
36
|
+
var import_stripe_loaders_core = require("stripe-loaders-core");
|
|
37
|
+
var defaultLogger = new import_stripe_loaders_core.ConsoleLogger(false);
|
|
38
|
+
function extractBodyFromHTML(htmlString, logger = defaultLogger) {
|
|
39
|
+
try {
|
|
40
|
+
if (!htmlString) {
|
|
41
|
+
logger.log("Input HTML is null or undefined");
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const bodyRegex = /<body[^>]*>([\s\S]*?)<\/body>/gi;
|
|
45
|
+
const matches = [];
|
|
46
|
+
let match;
|
|
47
|
+
while ((match = bodyRegex.exec(htmlString)) !== null) {
|
|
48
|
+
if (match[1]) {
|
|
49
|
+
matches.push(match[1].trim());
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (matches.length === 0) {
|
|
53
|
+
logger.log("No body tags found");
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
return matches;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
logger.error(`An error occurred: ${error}`);
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function extractArticleFromHTML(htmlString, logger = defaultLogger) {
|
|
63
|
+
try {
|
|
64
|
+
if (!htmlString) {
|
|
65
|
+
logger.log("Input HTML is null or undefined");
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
const articles = [];
|
|
69
|
+
const processArticleTags = () => {
|
|
70
|
+
const articleRegex = /<article(?:\s+[^>]*)?>(.*?)<\/article>/gis;
|
|
71
|
+
let articleMatch;
|
|
72
|
+
let foundArticles = false;
|
|
73
|
+
while ((articleMatch = articleRegex.exec(htmlString)) !== null) {
|
|
74
|
+
if (articleMatch[1]) {
|
|
75
|
+
articles.push(articleMatch[1].trim());
|
|
76
|
+
foundArticles = true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return foundArticles;
|
|
80
|
+
};
|
|
81
|
+
const processMainContent = () => {
|
|
82
|
+
const mainContentRegex = /<(div|section|main|article)(?:\s+[^>]*?)id=["']main-content["'](?:[^>]*?)>(.*?)<\/\1>/is;
|
|
83
|
+
const mainContentMatch = mainContentRegex.exec(htmlString);
|
|
84
|
+
if (mainContentMatch && mainContentMatch[2]) {
|
|
85
|
+
logger.log("Found element with main-content ID");
|
|
86
|
+
const contentArticleRegex = /<div(?:\s+[^>]*?)class=["'][^"']*Content-article[^"']*["'](?:[^>]*?)>(.*?)<\/div>/is;
|
|
87
|
+
const contentArticleMatch = contentArticleRegex.exec(mainContentMatch[2]);
|
|
88
|
+
if (contentArticleMatch && contentArticleMatch[1]) {
|
|
89
|
+
logger.log("Found content with Content-article class");
|
|
90
|
+
articles.push(contentArticleMatch[1].trim());
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
const documentRegex = /<div(?:\s+[^>]*?)class=["'][^"']*Document[^"']*["'](?:[^>]*?)>(.*?)<\/div>/is;
|
|
94
|
+
const documentMatch = documentRegex.exec(mainContentMatch[2]);
|
|
95
|
+
if (documentMatch && documentMatch[1]) {
|
|
96
|
+
logger.log("Found content with Document class");
|
|
97
|
+
articles.push(documentMatch[1].trim());
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
logger.log("Using all content within main-content");
|
|
101
|
+
articles.push(mainContentMatch[2].trim());
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
};
|
|
106
|
+
const processBody = () => {
|
|
107
|
+
const bodyRegex = /<body(?:\s+[^>]*?)>(.*?)<\/body>/is;
|
|
108
|
+
const bodyMatch = bodyRegex.exec(htmlString);
|
|
109
|
+
if (bodyMatch && bodyMatch[1]) {
|
|
110
|
+
const contentArticleRegex = /<div(?:\s+[^>]*?)class=["'][^"']*Content-article[^"']*["'](?:[^>]*?)>(.*?)<\/div>/is;
|
|
111
|
+
const contentArticleMatch = contentArticleRegex.exec(bodyMatch[1]);
|
|
112
|
+
if (contentArticleMatch && contentArticleMatch[1]) {
|
|
113
|
+
logger.log("Found content with Content-article class in body");
|
|
114
|
+
articles.push(contentArticleMatch[1].trim());
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
const documentRegex = /<div(?:\s+[^>]*?)class=["'][^"']*Document[^"']*["'](?:[^>]*?)>(.*?)<\/div>/is;
|
|
118
|
+
const documentMatch = documentRegex.exec(bodyMatch[1]);
|
|
119
|
+
if (documentMatch && documentMatch[1]) {
|
|
120
|
+
logger.log("Found content with Document class in body");
|
|
121
|
+
articles.push(documentMatch[1].trim());
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
logger.log("No specific content container found");
|
|
126
|
+
return false;
|
|
127
|
+
};
|
|
128
|
+
if (!processArticleTags()) {
|
|
129
|
+
logger.log("No article tags found, trying to find main-content");
|
|
130
|
+
if (!processMainContent()) {
|
|
131
|
+
logger.log("No main-content found, trying to find body content");
|
|
132
|
+
processBody();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return articles;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
logger.error(`Error extracting article from HTML: ${error}`);
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function toMarkdownUrl(pageUrl, locale) {
|
|
142
|
+
const url = new URL(pageUrl);
|
|
143
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
144
|
+
url.pathname = path.endsWith(".md") ? path : `${path || ""}.md`;
|
|
145
|
+
url.hash = "";
|
|
146
|
+
url.searchParams.set("locale", locale);
|
|
147
|
+
return url.toString();
|
|
148
|
+
}
|
|
149
|
+
function parseMarkdownDocument(markdown) {
|
|
150
|
+
const lines = (markdown ?? "").replace(/\r\n?/g, "\n").split("\n");
|
|
151
|
+
let title = "Unknown";
|
|
152
|
+
let description = "No description";
|
|
153
|
+
let index = lines.findIndex((line) => /^#\s+\S/.test(line));
|
|
154
|
+
if (index === -1) {
|
|
155
|
+
return { title, description };
|
|
156
|
+
}
|
|
157
|
+
title = lines[index].replace(/^#\s+/, "").trim();
|
|
158
|
+
const paragraph = [];
|
|
159
|
+
for (index += 1; index < lines.length; index++) {
|
|
160
|
+
const line = lines[index].trim();
|
|
161
|
+
if (line === "") {
|
|
162
|
+
if (paragraph.length > 0) break;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (/^(#|```|[-*+]\s|\d+\.\s|>|\||<)/.test(line)) {
|
|
166
|
+
if (paragraph.length > 0) break;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
paragraph.push(line);
|
|
170
|
+
}
|
|
171
|
+
if (paragraph.length > 0) {
|
|
172
|
+
description = paragraph.join(" ");
|
|
173
|
+
}
|
|
174
|
+
return { title, description };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/utils/url-filter.ts
|
|
178
|
+
function matchesResource(url, resource) {
|
|
179
|
+
const urlPath = new URL(url).pathname;
|
|
180
|
+
const pathSegments = urlPath.split("/").filter(Boolean);
|
|
181
|
+
if (resource.includes("/")) {
|
|
182
|
+
const resourceSegments = resource.split("/").filter(Boolean);
|
|
183
|
+
return resourceSegments.every((segment, index) => pathSegments[index] === segment);
|
|
184
|
+
} else {
|
|
185
|
+
return pathSegments[0] === resource;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function matchesExcludeResources(url, excludeResources) {
|
|
189
|
+
if (!excludeResources || excludeResources.length === 0) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
const urlPath = new URL(url).pathname;
|
|
193
|
+
const pathSegments = urlPath.split("/").filter(Boolean);
|
|
194
|
+
for (const excludeResource of excludeResources) {
|
|
195
|
+
if (excludeResource.includes("/")) {
|
|
196
|
+
const excludeSegments = excludeResource.split("/").filter(Boolean);
|
|
197
|
+
const matchesExclude = excludeSegments.every(
|
|
198
|
+
(segment, index) => pathSegments[index] === segment
|
|
199
|
+
);
|
|
200
|
+
if (matchesExclude) {
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
if (pathSegments[0] === excludeResource) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
function filterUrls(urls, resource, excludeResources) {
|
|
212
|
+
if (!resource && (!excludeResources || excludeResources.length === 0)) {
|
|
213
|
+
return urls;
|
|
214
|
+
}
|
|
215
|
+
return urls.filter((url) => {
|
|
216
|
+
if (resource && !matchesResource(url, resource)) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (excludeResources && matchesExcludeResources(url, excludeResources)) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/options.ts
|
|
227
|
+
var import_stripe_loaders_core2 = require("stripe-loaders-core");
|
|
228
|
+
function resolveLoaderOptions(options) {
|
|
229
|
+
const opts = typeof options === "boolean" ? { debug: options } : options ?? {};
|
|
230
|
+
const debug = opts.debug ?? false;
|
|
231
|
+
return {
|
|
232
|
+
debug,
|
|
233
|
+
logger: opts.logger ?? new import_stripe_loaders_core2.ConsoleLogger(debug),
|
|
234
|
+
rateLimit: (0, import_stripe_loaders_core2.resolveRateLimit)(opts.rateLimit),
|
|
235
|
+
fetch: opts.fetch,
|
|
236
|
+
sleep: opts.sleep
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/StripeDocsLoader.ts
|
|
241
|
+
var StripeDocsDocumentLoader = class extends import_base.BaseDocumentLoader {
|
|
242
|
+
/**
|
|
243
|
+
* @param options Loader options, or a boolean to enable debug logging (legacy signature)
|
|
244
|
+
*/
|
|
245
|
+
constructor(options) {
|
|
246
|
+
super();
|
|
247
|
+
this.options = resolveLoaderOptions(options);
|
|
248
|
+
this.format = typeof options === "object" && options.format || "html";
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Fetches URLs from the Stripe Docs sitemap
|
|
252
|
+
* @returns {Promise<string[]>} Array of URLs from the sitemap
|
|
253
|
+
*/
|
|
254
|
+
async fetchURLsFromSitemap() {
|
|
255
|
+
const documentUrls = await new import_stripe_loaders_core3.SitemapProcessor({
|
|
256
|
+
debug: this.options.debug,
|
|
257
|
+
logger: this.options.logger,
|
|
258
|
+
rateLimit: this.options.rateLimit,
|
|
259
|
+
fetch: this.options.fetch,
|
|
260
|
+
sleep: this.options.sleep
|
|
261
|
+
}).fetchAndProcessSitemap("https://docs.stripe.com/sitemap.xml");
|
|
262
|
+
return documentUrls;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Fetches articles from the sitemap
|
|
266
|
+
*/
|
|
267
|
+
async fetchArticlesFromSitemap({
|
|
268
|
+
locale,
|
|
269
|
+
resource,
|
|
270
|
+
excludeResources,
|
|
271
|
+
format
|
|
272
|
+
}) {
|
|
273
|
+
const documentUrls = await this.fetchURLsFromSitemap();
|
|
274
|
+
const filteredDocumentUrls = filterUrls(documentUrls, resource, excludeResources);
|
|
275
|
+
return this.fetchArticlesFromURLs(filteredDocumentUrls, locale, format);
|
|
276
|
+
}
|
|
277
|
+
async fetchArticlesFromURLs(documentUrls, locale, format = this.format) {
|
|
278
|
+
const { logger } = this.options;
|
|
279
|
+
const pacer = new import_stripe_loaders_core3.RequestPacer(this.options.rateLimit, this.options.sleep);
|
|
280
|
+
const articles = [];
|
|
281
|
+
for (const docsUrl of documentUrls) {
|
|
282
|
+
const requestUrl = format === "markdown" ? toMarkdownUrl(docsUrl, locale) : `${docsUrl}?locale=${locale}`;
|
|
283
|
+
try {
|
|
284
|
+
await pacer.wait();
|
|
285
|
+
logger.log(`Fetching ${requestUrl}`);
|
|
286
|
+
const response = await (0, import_stripe_loaders_core3.fetchWithRetry)(requestUrl, void 0, {
|
|
287
|
+
rateLimit: this.options.rateLimit,
|
|
288
|
+
fetch: this.options.fetch,
|
|
289
|
+
sleep: this.options.sleep,
|
|
290
|
+
onRetry: ({ attempt, waitMs, reason }) => logger.warn(`Retrying ${requestUrl} (attempt ${attempt}, ${reason}) in ${waitMs}ms`)
|
|
291
|
+
});
|
|
292
|
+
if (response.status >= 400) {
|
|
293
|
+
logger.warn(`Skipping ${docsUrl} - HTTP status: ${response.status}`);
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const body = await response.text();
|
|
297
|
+
if (format === "markdown") {
|
|
298
|
+
const { title: title2, description: description2 } = parseMarkdownDocument(body);
|
|
299
|
+
articles.push({ url: docsUrl, content: body.trim(), title: title2, description: description2, format });
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const extracted = extractArticleFromHTML(body, logger);
|
|
303
|
+
const titleMatch = body.match(/<title[^>]*>([^<]+)<\/title>/);
|
|
304
|
+
const descMatch = body.match(/<meta[^>]*name="description"[^>]*content="([^"]+)"[^>]*>/);
|
|
305
|
+
const title = titleMatch ? titleMatch[1].trim() : "Unknown";
|
|
306
|
+
const description = descMatch ? descMatch[1].trim() : "No description";
|
|
307
|
+
articles.push(
|
|
308
|
+
...extracted.map((content) => ({ url: docsUrl, content, title, description, format }))
|
|
309
|
+
);
|
|
310
|
+
} catch (error) {
|
|
311
|
+
logger.error(`Error fetching ${docsUrl}: ${error}`);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return articles;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* @deprecated Use `this.load()` and `splitter.splitDocuments()` individually.
|
|
319
|
+
* Loads the documents and splits them using the given document transformer.
|
|
320
|
+
* Kept for compatibility with LangChain's `index()` API, which only treats a
|
|
321
|
+
* source as a document loader when it has both `load()` and `loadAndSplit()`.
|
|
322
|
+
* `@langchain/core` 1.x no longer provides this method on BaseDocumentLoader.
|
|
323
|
+
* @param splitter The document transformer (e.g. a text splitter) to apply
|
|
324
|
+
* @returns {Promise<Document[]>} The loaded and split documents
|
|
325
|
+
*/
|
|
326
|
+
async loadAndSplit(splitter) {
|
|
327
|
+
if (splitter === void 0) {
|
|
328
|
+
throw new Error("You must pass a text splitter to use this method.");
|
|
329
|
+
}
|
|
330
|
+
const docs = await this.load();
|
|
331
|
+
return splitter.invoke(docs);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Loads documents from Stripe Docs
|
|
335
|
+
* @returns {Promise<Document[]>} Array of LangChain documents
|
|
336
|
+
*/
|
|
337
|
+
async load(options) {
|
|
338
|
+
const { urls, resource, excludeResources } = options ?? {};
|
|
339
|
+
const locale = options?.locale ?? "en-US";
|
|
340
|
+
const format = options?.format ?? this.format;
|
|
341
|
+
const articles = urls ? await this.fetchArticlesFromURLs(urls, locale, format) : await this.fetchArticlesFromSitemap({ locale, resource, excludeResources, format });
|
|
342
|
+
const nhm = new import_node_html_markdown.NodeHtmlMarkdown();
|
|
343
|
+
return articles.map(
|
|
344
|
+
(article) => new import_documents.Document({
|
|
345
|
+
pageContent: article.format === "markdown" ? article.content : nhm.translate(article.content),
|
|
346
|
+
metadata: {
|
|
347
|
+
source: article.url,
|
|
348
|
+
title: article.title,
|
|
349
|
+
description: article.description
|
|
350
|
+
}
|
|
351
|
+
})
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// src/StripeComLoader.ts
|
|
357
|
+
var import_stripe_loaders_core4 = require("stripe-loaders-core");
|
|
358
|
+
var import_base2 = require("@langchain/core/document_loaders/base");
|
|
359
|
+
var import_documents2 = require("@langchain/core/documents");
|
|
360
|
+
var import_node_html_markdown2 = require("node-html-markdown");
|
|
361
|
+
var StripeComDocumentLoader = class extends import_base2.BaseDocumentLoader {
|
|
362
|
+
/**
|
|
363
|
+
* @param options Loader options, or a boolean to enable debug logging (legacy signature)
|
|
364
|
+
*/
|
|
365
|
+
constructor(options) {
|
|
366
|
+
super();
|
|
367
|
+
this.options = resolveLoaderOptions(options);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Fetches URLs from the Stripe.com sitemap
|
|
371
|
+
* @param {string} resource Optional resource path to filter URLs
|
|
372
|
+
* @returns {Promise<string[]>} Array of URLs from the sitemap
|
|
373
|
+
*/
|
|
374
|
+
async fetchURLsFromSitemap(resource) {
|
|
375
|
+
return new import_stripe_loaders_core4.SitemapProcessor({
|
|
376
|
+
debug: this.options.debug,
|
|
377
|
+
logger: this.options.logger,
|
|
378
|
+
rateLimit: this.options.rateLimit,
|
|
379
|
+
fetch: this.options.fetch,
|
|
380
|
+
sleep: this.options.sleep
|
|
381
|
+
}).fetchAndProcessSitemapIndex(
|
|
382
|
+
"https://stripe.com/sitemap/sitemap.xml",
|
|
383
|
+
["https://stripe.com/", resource].filter(Boolean).join("")
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Fetches article content from a list of URLs
|
|
388
|
+
* @param {string[]} urls Array of URLs to fetch content from
|
|
389
|
+
* @param {string} locale Locale for the content
|
|
390
|
+
* @returns {Promise<StripeDocsArticle[]>} Array of article objects
|
|
391
|
+
*/
|
|
392
|
+
async fetchArticlesFromURLs(urls, locale) {
|
|
393
|
+
const { logger } = this.options;
|
|
394
|
+
const pacer = new import_stripe_loaders_core4.RequestPacer(this.options.rateLimit, this.options.sleep);
|
|
395
|
+
const articles = [];
|
|
396
|
+
for (const pageUrl of urls) {
|
|
397
|
+
const requestUrl = `${pageUrl}?locale=${locale}`;
|
|
398
|
+
try {
|
|
399
|
+
await pacer.wait();
|
|
400
|
+
logger.log(`Fetching ${requestUrl}`);
|
|
401
|
+
const response = await (0, import_stripe_loaders_core4.fetchWithRetry)(requestUrl, void 0, {
|
|
402
|
+
rateLimit: this.options.rateLimit,
|
|
403
|
+
fetch: this.options.fetch,
|
|
404
|
+
sleep: this.options.sleep,
|
|
405
|
+
onRetry: ({ attempt, waitMs, reason }) => logger.warn(`Retrying ${requestUrl} (attempt ${attempt}, ${reason}) in ${waitMs}ms`)
|
|
406
|
+
});
|
|
407
|
+
if (response.status >= 400) {
|
|
408
|
+
logger.warn(`Skipping ${pageUrl} - HTTP status: ${response.status}`);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
const html = await response.text();
|
|
412
|
+
const rawArticles = extractBodyFromHTML(html, logger);
|
|
413
|
+
const titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/);
|
|
414
|
+
const descMatch = html.match(/<meta[^>]*name="description"[^>]*content="([^"]+)"[^>]*>/);
|
|
415
|
+
const title = titleMatch ? titleMatch[1].trim() : "Unknown";
|
|
416
|
+
const description = descMatch ? descMatch[1].trim() : "No description";
|
|
417
|
+
articles.push(
|
|
418
|
+
...rawArticles.map((content) => ({ url: pageUrl, content, title, description }))
|
|
419
|
+
);
|
|
420
|
+
} catch (error) {
|
|
421
|
+
logger.error(`Error fetching ${pageUrl}: ${error}`);
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return articles;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Fetches articles from the sitemap
|
|
429
|
+
* @param {string} resource Optional resource path to filter URLs
|
|
430
|
+
* @param {string} locale Locale for the content
|
|
431
|
+
* @returns {Promise<StripeDocsArticle[]>} Array of article objects
|
|
432
|
+
*/
|
|
433
|
+
async fetchArticlesFromSitemap(resource, locale) {
|
|
434
|
+
const documentUrls = await this.fetchURLsFromSitemap(resource);
|
|
435
|
+
return this.fetchArticlesFromURLs(documentUrls, locale);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* @deprecated Use `this.load()` and `splitter.splitDocuments()` individually.
|
|
439
|
+
* Loads the documents and splits them using the given document transformer.
|
|
440
|
+
* Kept for compatibility with LangChain's `index()` API, which only treats a
|
|
441
|
+
* source as a document loader when it has both `load()` and `loadAndSplit()`.
|
|
442
|
+
* `@langchain/core` 1.x no longer provides this method on BaseDocumentLoader.
|
|
443
|
+
* @param splitter The document transformer (e.g. a text splitter) to apply
|
|
444
|
+
* @returns {Promise<Document[]>} The loaded and split documents
|
|
445
|
+
*/
|
|
446
|
+
async loadAndSplit(splitter) {
|
|
447
|
+
if (splitter === void 0) {
|
|
448
|
+
throw new Error("You must pass a text splitter to use this method.");
|
|
449
|
+
}
|
|
450
|
+
const docs = await this.load();
|
|
451
|
+
return splitter.invoke(docs);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Loads documents from Stripe.com
|
|
455
|
+
* @param {Object} options Loading options
|
|
456
|
+
* @param {string} options.resource Optional resource path to filter URLs
|
|
457
|
+
* @param {string} options.locale Optional locale for the content (defaults to 'en-US')
|
|
458
|
+
* @param {string[]} options.urls Optional list of specific URLs to load
|
|
459
|
+
* @returns {Promise<Document[]>} Array of LangChain documents
|
|
460
|
+
*/
|
|
461
|
+
async load(options) {
|
|
462
|
+
const { resource, urls } = options ?? {};
|
|
463
|
+
const locale = options?.locale ?? "en-US";
|
|
464
|
+
const articles = urls ? await this.fetchArticlesFromURLs(urls, locale) : await this.fetchArticlesFromSitemap(resource, locale);
|
|
465
|
+
const nhm = new import_node_html_markdown2.NodeHtmlMarkdown();
|
|
466
|
+
return articles.map(
|
|
467
|
+
(article) => new import_documents2.Document({
|
|
468
|
+
pageContent: nhm.translate(article.content),
|
|
469
|
+
metadata: {
|
|
470
|
+
source: article.url,
|
|
471
|
+
title: article.title,
|
|
472
|
+
description: article.description
|
|
473
|
+
}
|
|
474
|
+
})
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// src/StripeChangelogLoader.ts
|
|
480
|
+
var import_stripe_loaders_core5 = require("stripe-loaders-core");
|
|
481
|
+
var import_base3 = require("@langchain/core/document_loaders/base");
|
|
482
|
+
var import_documents3 = require("@langchain/core/documents");
|
|
483
|
+
function entryMetadata(entry) {
|
|
484
|
+
return {
|
|
485
|
+
source: entry.link || "https://docs.stripe.com/changelog",
|
|
486
|
+
title: entry.title,
|
|
487
|
+
version: entry.version,
|
|
488
|
+
date: entry.date,
|
|
489
|
+
release: entry.release,
|
|
490
|
+
category: entry.category,
|
|
491
|
+
affectedProducts: entry.affectedProducts,
|
|
492
|
+
breaking: entry.isBreakingChange,
|
|
493
|
+
changeType: entry.changeType
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
function entrySummary(entry) {
|
|
497
|
+
return [
|
|
498
|
+
`# ${entry.title}`,
|
|
499
|
+
"",
|
|
500
|
+
`- Version: ${entry.version}`,
|
|
501
|
+
...entry.release ? [`- Release: ${entry.release}`] : [],
|
|
502
|
+
...entry.category ? [`- Category: ${entry.category}`] : [],
|
|
503
|
+
`- Affected products: ${entry.affectedProducts.join(", ") || "None"}`,
|
|
504
|
+
`- Breaking change: ${entry.isBreakingChange ? "Yes" : "No"}`,
|
|
505
|
+
...entry.link ? [`- Details: ${entry.link}`] : []
|
|
506
|
+
].join("\n");
|
|
507
|
+
}
|
|
508
|
+
var StripeChangelogDocumentLoader = class extends import_base3.BaseDocumentLoader {
|
|
509
|
+
/**
|
|
510
|
+
* @param options Loader options, or a boolean to enable debug logging
|
|
511
|
+
*/
|
|
512
|
+
constructor(options) {
|
|
513
|
+
super();
|
|
514
|
+
this.options = resolveLoaderOptions(options);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Fetches the changelog and returns the entries that match the filters (newest first).
|
|
518
|
+
*/
|
|
519
|
+
async fetchEntries(options = {}) {
|
|
520
|
+
const locale = options.locale ?? "en-US";
|
|
521
|
+
this.options.logger.log(`Fetching Stripe changelog (${locale})`);
|
|
522
|
+
const versions = await (0, import_stripe_loaders_core5.fetchStripeChangelog)({
|
|
523
|
+
locale,
|
|
524
|
+
rateLimit: this.options.rateLimit,
|
|
525
|
+
fetch: this.options.fetch,
|
|
526
|
+
sleep: this.options.sleep
|
|
527
|
+
});
|
|
528
|
+
const releases = options.releases?.map((release) => release.toLowerCase());
|
|
529
|
+
return versions.flatMap((version) => version.entries).filter(
|
|
530
|
+
(entry) => (!options.since || entry.date >= options.since) && (!releases || entry.release !== void 0 && releases.includes(entry.release.toLowerCase())) && (!options.breakingOnly || entry.isBreakingChange)
|
|
531
|
+
).slice(0, Math.max(0, options.limit ?? Infinity));
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* @deprecated Use `this.load()` and `splitter.splitDocuments()` individually.
|
|
535
|
+
* Kept for compatibility with LangChain's `index()` API, which only treats a
|
|
536
|
+
* source as a document loader when it has both `load()` and `loadAndSplit()`.
|
|
537
|
+
*/
|
|
538
|
+
async loadAndSplit(splitter) {
|
|
539
|
+
if (splitter === void 0) {
|
|
540
|
+
throw new Error("You must pass a text splitter to use this method.");
|
|
541
|
+
}
|
|
542
|
+
return splitter.invoke(await this.load());
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Loads changelog entries as LangChain documents.
|
|
546
|
+
* When `includeDetails` is true and a detail page cannot be fetched, the entry falls back
|
|
547
|
+
* to the changelog row summary (and `metadata.detailsFetched` is false).
|
|
548
|
+
*/
|
|
549
|
+
async load(options = {}) {
|
|
550
|
+
const locale = options.locale ?? "en-US";
|
|
551
|
+
const entries = await this.fetchEntries(options);
|
|
552
|
+
if (!options.includeDetails) {
|
|
553
|
+
return entries.map(
|
|
554
|
+
(entry) => new import_documents3.Document({ pageContent: entrySummary(entry), metadata: entryMetadata(entry) })
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
const documents = [];
|
|
558
|
+
const { requestDelayMs, requestJitterMs } = this.options.rateLimit;
|
|
559
|
+
for (const entry of entries) {
|
|
560
|
+
let detail = null;
|
|
561
|
+
if (entry.link) {
|
|
562
|
+
await (0, import_stripe_loaders_core5.delayWithJitter)(requestDelayMs, requestJitterMs, this.options.sleep);
|
|
563
|
+
let url = entry.link;
|
|
564
|
+
try {
|
|
565
|
+
url = toMarkdownUrl(entry.link, locale);
|
|
566
|
+
this.options.logger.log(`Fetching ${url}`);
|
|
567
|
+
const response = await (0, import_stripe_loaders_core5.fetchWithRetry)(url, void 0, {
|
|
568
|
+
rateLimit: this.options.rateLimit,
|
|
569
|
+
fetch: this.options.fetch,
|
|
570
|
+
sleep: this.options.sleep,
|
|
571
|
+
onRetry: ({ attempt, waitMs, reason }) => this.options.logger.warn(
|
|
572
|
+
`Retrying ${url} (attempt ${attempt}, ${reason}) in ${waitMs}ms`
|
|
573
|
+
)
|
|
574
|
+
});
|
|
575
|
+
if (response.status >= 400) {
|
|
576
|
+
this.options.logger.warn(
|
|
577
|
+
`Skipping details of ${entry.link} - HTTP status: ${response.status}`
|
|
578
|
+
);
|
|
579
|
+
} else {
|
|
580
|
+
detail = (await response.text()).trim() || null;
|
|
581
|
+
}
|
|
582
|
+
} catch (error) {
|
|
583
|
+
this.options.logger.error(`Error fetching ${url}: ${error}`);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
documents.push(
|
|
587
|
+
new import_documents3.Document({
|
|
588
|
+
pageContent: detail ?? entrySummary(entry),
|
|
589
|
+
metadata: { ...entryMetadata(entry), detailsFetched: detail !== null }
|
|
590
|
+
})
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
return documents;
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
597
|
+
0 && (module.exports = {
|
|
598
|
+
StripeChangelogDocumentLoader,
|
|
599
|
+
StripeComDocumentLoader,
|
|
600
|
+
StripeDocsDocumentLoader
|
|
601
|
+
});
|
|
602
|
+
//# sourceMappingURL=index.cjs.map
|