@peam-ai/next 0.1.4 → 0.1.5

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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { NextConfig, NextAdapter } from 'next';
2
- import { SearchExporterConfig, SearchIndexExporter } from '@peam-ai/search';
1
+ import { NextConfig } from 'next';
2
+ import { SearchExporterConfig } from '@peam-ai/search';
3
3
 
4
4
  interface PeamConfig {
5
5
  /**
@@ -27,14 +27,6 @@ interface PeamConfig {
27
27
  */
28
28
  exclude?: string[];
29
29
  }
30
- type ResolvedPeamAdapterConfig = Required<Omit<PeamConfig, 'robotsTxtPath' | 'searchExporter'>> & {
31
- robotsTxtPath?: string;
32
- searchIndexExporter: SearchIndexExporter;
33
- };
34
- declare function setNextConfig(nextConfig: NextConfig, peamConfig?: PeamConfig): void;
35
- declare const getConfig: () => ResolvedPeamAdapterConfig;
36
-
37
- declare function createPeamAdapter(config: ResolvedPeamAdapterConfig): NextAdapter;
38
30
 
39
31
  /**
40
32
  * Wraps Next.js config to enable Peam content extraction during build.
@@ -42,13 +34,23 @@ declare function createPeamAdapter(config: ResolvedPeamAdapterConfig): NextAdapt
42
34
  * @example
43
35
  * ```typescript
44
36
  * // next.config.ts
45
- * import { withPeam } from '@peam-ai/next';
37
+ * import withPeam from '@peam-ai/next';
46
38
  *
47
39
  * export default withPeam()({
48
40
  * // your Next.js config
49
41
  * });
50
42
  * ```
43
+ *
44
+ * Or with `require()`:
45
+ * ```javascript
46
+ * // next.config.js
47
+ * const withPeam = require('@peam-ai/next');
48
+ *
49
+ * module.exports = withPeam()({
50
+ * // your Next.js config
51
+ * });
52
+ * ```
51
53
  */
52
54
  declare function withPeam(peamConfig?: PeamConfig): (nextConfig?: NextConfig) => NextConfig;
53
55
 
54
- export { type PeamConfig, type ResolvedPeamAdapterConfig, createPeamAdapter, getConfig, setNextConfig, withPeam };
56
+ export { withPeam as default };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { NextConfig, NextAdapter } from 'next';
2
- import { SearchExporterConfig, SearchIndexExporter } from '@peam-ai/search';
1
+ import { NextConfig } from 'next';
2
+ import { SearchExporterConfig } from '@peam-ai/search';
3
3
 
4
4
  interface PeamConfig {
5
5
  /**
@@ -27,14 +27,6 @@ interface PeamConfig {
27
27
  */
28
28
  exclude?: string[];
29
29
  }
30
- type ResolvedPeamAdapterConfig = Required<Omit<PeamConfig, 'robotsTxtPath' | 'searchExporter'>> & {
31
- robotsTxtPath?: string;
32
- searchIndexExporter: SearchIndexExporter;
33
- };
34
- declare function setNextConfig(nextConfig: NextConfig, peamConfig?: PeamConfig): void;
35
- declare const getConfig: () => ResolvedPeamAdapterConfig;
36
-
37
- declare function createPeamAdapter(config: ResolvedPeamAdapterConfig): NextAdapter;
38
30
 
39
31
  /**
40
32
  * Wraps Next.js config to enable Peam content extraction during build.
@@ -42,13 +34,23 @@ declare function createPeamAdapter(config: ResolvedPeamAdapterConfig): NextAdapt
42
34
  * @example
43
35
  * ```typescript
44
36
  * // next.config.ts
45
- * import { withPeam } from '@peam-ai/next';
37
+ * import withPeam from '@peam-ai/next';
46
38
  *
47
39
  * export default withPeam()({
48
40
  * // your Next.js config
49
41
  * });
50
42
  * ```
43
+ *
44
+ * Or with `require()`:
45
+ * ```javascript
46
+ * // next.config.js
47
+ * const withPeam = require('@peam-ai/next');
48
+ *
49
+ * module.exports = withPeam()({
50
+ * // your Next.js config
51
+ * });
52
+ * ```
51
53
  */
52
54
  declare function withPeam(peamConfig?: PeamConfig): (nextConfig?: NextConfig) => NextConfig;
53
55
 
54
- export { type PeamConfig, type ResolvedPeamAdapterConfig, createPeamAdapter, getConfig, setNextConfig, withPeam };
56
+ export { withPeam as default };
package/dist/index.js CHANGED
@@ -1,11 +1,9 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs');
4
- var logger = require('peam/logger');
5
- var parser = require('peam/parser');
6
- var search = require('peam/search');
7
- var fs$1 = require('fs/promises');
3
+ var module$1 = require('module');
8
4
  var path = require('path');
5
+ var fsSync = require('fs');
6
+ var fs = require('fs/promises');
9
7
 
10
8
  function _interopNamespace(e) {
11
9
  if (e && e.__esModule) return e;
@@ -25,8 +23,9 @@ function _interopNamespace(e) {
25
23
  return Object.freeze(n);
26
24
  }
27
25
 
28
- var fs__namespace = /*#__PURE__*/_interopNamespace(fs$1);
29
26
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
27
+ var fsSync__namespace = /*#__PURE__*/_interopNamespace(fsSync);
28
+ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
30
29
 
31
30
  var __create = Object.create;
32
31
  var __defProp = Object.defineProperty;
@@ -131,125 +130,6 @@ var require_picocolors = __commonJS({
131
130
  module2.exports.createColors = createColors;
132
131
  }
133
132
  });
134
- var log = logger.loggers.adapter;
135
- function extractRobotsFromPrerender(prerender) {
136
- var _a;
137
- try {
138
- if (prerender.pathname !== "/robots.txt") {
139
- return null;
140
- }
141
- if ((_a = prerender.fallback) == null ? void 0 : _a.filePath) {
142
- const content = fs.readFileSync(prerender.fallback.filePath, "utf-8");
143
- return content;
144
- }
145
- } catch (error) {
146
- log.error("Error extracting robots from prerender:", error);
147
- }
148
- return null;
149
- }
150
- function loadRobotsTxt(projectDir, prerenders, robotsTxtPath) {
151
- try {
152
- let robotsContent = null;
153
- let foundPath = null;
154
- if (prerenders && prerenders.length > 0) {
155
- for (const prerender of prerenders) {
156
- const content = extractRobotsFromPrerender(prerender);
157
- if (content) {
158
- log.debug("Found dynamic robots.txt from prerenders");
159
- robotsContent = content;
160
- foundPath = prerender.pathname;
161
- break;
162
- }
163
- }
164
- }
165
- if (!robotsContent) {
166
- const searchPaths = ["public/robots.txt", "app/robots.txt", "src/app/robots.txt"];
167
- const result = parser.loadRobotsTxt(projectDir, searchPaths, robotsTxtPath);
168
- if (result) {
169
- log.debug("Loaded robots.txt from:", result.path);
170
- return result;
171
- }
172
- return null;
173
- }
174
- return {
175
- parser: parser.createRobotsParser(robotsContent),
176
- path: foundPath || ""
177
- };
178
- } catch (error) {
179
- log.error("Error loading robots.txt:", error);
180
- return null;
181
- }
182
- }
183
- function createPeamAdapter(config) {
184
- return {
185
- name: "peam-adapter",
186
- async onBuildComplete(ctx) {
187
- var _a, _b;
188
- log.debug("Extracting page content via adapter");
189
- const outputs = ctx.outputs;
190
- let prerenders;
191
- if (Array.isArray(outputs)) {
192
- prerenders = outputs.filter((output) => output.type === "PRERENDER");
193
- } else {
194
- prerenders = outputs.prerenders || [];
195
- }
196
- log.debug("Total prerenders:", prerenders.length);
197
- const projectDir = ctx.projectDir || process.cwd();
198
- const robotsResult = config.respectRobotsTxt ? loadRobotsTxt(projectDir, prerenders, config.robotsTxtPath) : null;
199
- if (robotsResult) {
200
- log.debug("Using robots.txt from:", robotsResult.path);
201
- }
202
- const pages = [];
203
- for (const prerender of prerenders) {
204
- const pathname = prerender.pathname;
205
- let fallbackFilePath = (_a = prerender.fallback) == null ? void 0 : _a.filePath;
206
- if (!fallbackFilePath) {
207
- continue;
208
- }
209
- if (fallbackFilePath == null ? void 0 : fallbackFilePath.endsWith("/.html")) {
210
- fallbackFilePath = fallbackFilePath.replace("/.html", "/index.html");
211
- }
212
- const filterResult = parser.shouldIncludePath(
213
- pathname,
214
- (_b = robotsResult == null ? void 0 : robotsResult.parser) != null ? _b : null,
215
- config.exclude,
216
- config.respectRobotsTxt
217
- );
218
- if (!filterResult.included) {
219
- if (filterResult.reason === "robots-txt") {
220
- log.debug("Path excluded by robots.txt:", pathname);
221
- } else if (filterResult.reason === "exclude-pattern") {
222
- log.debug("Path excluded by user pattern:", pathname);
223
- }
224
- continue;
225
- }
226
- try {
227
- log.debug("Reading HTML from:", fallbackFilePath);
228
- const html = fs.readFileSync(fallbackFilePath, "utf-8");
229
- const structuredPage = parser.parseHTML(html);
230
- if (!structuredPage) {
231
- log.warn("No content extracted from", pathname);
232
- continue;
233
- }
234
- log.debug("Successfully extracted content from", pathname);
235
- pages.push({
236
- path: pathname,
237
- htmlFile: fallbackFilePath.replace(projectDir + "/", ""),
238
- structuredPage,
239
- type: "page"
240
- });
241
- } catch (error) {
242
- log.error("Error processing", pathname, error);
243
- }
244
- }
245
- log.debug("Creating search index...");
246
- const searchIndexData = await search.buildSearchIndex(pages);
247
- await config.searchIndexExporter.export(searchIndexData);
248
- log.debug("Saved search index via exporter");
249
- log.debug("Extraction complete with total pages:", pages.length);
250
- }
251
- };
252
- }
253
133
 
254
134
  // ../logger/dist/index.mjs
255
135
  var import_picocolors = __toESM(require_picocolors());
@@ -293,7 +173,8 @@ function print(message, args, { namespace, color = "white" }) {
293
173
  const ns = namespace ? `${import_picocolors.default.dim(namespace)} ` : "";
294
174
  console.log(header + ns + colorize(String(message)), ...args);
295
175
  }
296
- var loggers2 = {
176
+ var loggers = {
177
+ next: createLogger("peam:next"),
297
178
  search: createLogger("peam:search")
298
179
  };
299
180
 
@@ -2586,11 +2467,12 @@ var __async = (__this, __arguments, generator) => {
2586
2467
  step((generator = generator.apply(__this, __arguments)).next());
2587
2468
  });
2588
2469
  };
2589
- var log3 = loggers2.search;
2470
+ var log3 = loggers.search;
2590
2471
  var FileBasedSearchIndexExporter = class {
2591
2472
  constructor(options) {
2592
2473
  this.cachedData = null;
2593
- this.baseDir = options.baseDir;
2474
+ var _a;
2475
+ this.baseDir = (_a = options.baseDir) != null ? _a : process.cwd();
2594
2476
  this.indexPath = options.indexPath;
2595
2477
  }
2596
2478
  getFullPath() {
@@ -2628,10 +2510,18 @@ var FileBasedSearchIndexExporter = class {
2628
2510
  return data;
2629
2511
  });
2630
2512
  }
2631
- export(data) {
2632
- return __async(this, null, function* () {
2513
+ export(_0) {
2514
+ return __async(this, arguments, function* (data, options = { override: true }) {
2633
2515
  const fullPath = this.getFullPath();
2634
2516
  try {
2517
+ if (!(options == null ? void 0 : options.override)) {
2518
+ try {
2519
+ yield fs__namespace.access(fullPath);
2520
+ log3.debug("Search index file already exists and override is false, skipping export:", fullPath);
2521
+ return;
2522
+ } catch (e) {
2523
+ }
2524
+ }
2635
2525
  const dir = path__namespace.dirname(fullPath);
2636
2526
  yield fs__namespace.mkdir(dir, { recursive: true });
2637
2527
  yield fs__namespace.writeFile(fullPath, JSON.stringify(data, null, 2), "utf-8");
@@ -2642,6 +2532,26 @@ var FileBasedSearchIndexExporter = class {
2642
2532
  }
2643
2533
  });
2644
2534
  }
2535
+ exportSync(data, options = { override: true }) {
2536
+ const fullPath = this.getFullPath();
2537
+ try {
2538
+ if (!(options == null ? void 0 : options.override)) {
2539
+ try {
2540
+ fsSync__namespace.accessSync(fullPath);
2541
+ log3.debug("Search index file already exists and override is false, skipping export:", fullPath);
2542
+ return;
2543
+ } catch (e) {
2544
+ }
2545
+ }
2546
+ const dir = path__namespace.dirname(fullPath);
2547
+ fsSync__namespace.mkdirSync(dir, { recursive: true });
2548
+ fsSync__namespace.writeFileSync(fullPath, JSON.stringify(data, null, 2), "utf-8");
2549
+ log3.debug("Search index saved to file:", fullPath, "with", data.keys.length, "keys");
2550
+ } catch (error) {
2551
+ log3.error("Failed to save search index to file:", fullPath, error);
2552
+ throw error;
2553
+ }
2554
+ }
2645
2555
  };
2646
2556
  function createExporterFromConfig(exporterConfig) {
2647
2557
  if (exporterConfig.type === "fileBased") {
@@ -2654,7 +2564,7 @@ function createExporterFromConfig(exporterConfig) {
2654
2564
  var defaultConfig = {
2655
2565
  searchExporter: {
2656
2566
  type: "fileBased",
2657
- config: { baseDir: process.cwd(), indexPath: ".peam/index.json" }
2567
+ config: { indexPath: ".peam/index.json" }
2658
2568
  },
2659
2569
  respectRobotsTxt: true,
2660
2570
  exclude: []
@@ -2683,12 +2593,13 @@ var getConfig = () => {
2683
2593
  "Peam configuration not found. Make sure withPeam() is properly configured in your next.config file."
2684
2594
  );
2685
2595
  }
2686
- const searchExporter = {
2596
+ const searchExporterConfig = {
2687
2597
  type: process.env.PEAM_SEARCH_EXPORTER_TYPE,
2688
2598
  config: JSON.parse(process.env.PEAM_SEARCH_EXPORTER_CONFIG)
2689
2599
  };
2690
2600
  const resolvedConfig = {
2691
- searchIndexExporter: createExporterFromConfig(searchExporter),
2601
+ searchExporter: searchExporterConfig,
2602
+ searchIndexExporter: createExporterFromConfig(searchExporterConfig),
2692
2603
  respectRobotsTxt: process.env.PEAM_RESPECT_ROBOTS_TXT === "true",
2693
2604
  robotsTxtPath: process.env.PEAM_ROBOTS_TXT_PATH || void 0,
2694
2605
  exclude: process.env.PEAM_EXCLUDE ? JSON.parse(process.env.PEAM_EXCLUDE) : []
@@ -2697,22 +2608,117 @@ var getConfig = () => {
2697
2608
  };
2698
2609
 
2699
2610
  // src/withPeam.ts
2611
+ var require2 = module$1.createRequire(process.cwd() + "/");
2612
+ var log4 = loggers.next;
2613
+ var isProd = process.env.NODE_ENV === "production";
2614
+ function getNextVersion() {
2615
+ try {
2616
+ const [major, minor] = require2("next/package.json").version.split(".", 2).map(Number);
2617
+ return { major, minor };
2618
+ } catch (error) {
2619
+ log4.error("Could not resolve Next.js version.", error);
2620
+ return void 0;
2621
+ }
2622
+ }
2623
+ function addStubIndex() {
2624
+ var _a, _b, _c;
2625
+ try {
2626
+ if (!isProd) {
2627
+ return;
2628
+ }
2629
+ const config = getConfig();
2630
+ if (((_a = config.searchExporter) == null ? void 0 : _a.type) !== "fileBased") {
2631
+ return;
2632
+ }
2633
+ const stubData = { keys: [], data: {} };
2634
+ (_c = (_b = config.searchIndexExporter).exportSync) == null ? void 0 : _c.call(_b, stubData, { override: false });
2635
+ } catch (error) {
2636
+ log4.error("Failed to create stub index:", error);
2637
+ }
2638
+ }
2639
+ function addAdapter(config) {
2640
+ const nextVersion = getNextVersion();
2641
+ if (!nextVersion || nextVersion.major < 16) {
2642
+ log4.warn(
2643
+ "Peam adapter requires Next.js 16 or higher, skipping adapter configuration. Make sure the postbuild script is set up correctly, See more here: https://peam.ai/docs."
2644
+ );
2645
+ return config;
2646
+ }
2647
+ return {
2648
+ ...config,
2649
+ experimental: {
2650
+ ...config.experimental,
2651
+ adapterPath: require2.resolve(path__namespace.join(__dirname, "peam.adapter.js"))
2652
+ }
2653
+ };
2654
+ }
2655
+ function addOutputFileTracing(nextConfig, peamConfig) {
2656
+ var _a, _b, _c, _d;
2657
+ nextConfig = { ...nextConfig };
2658
+ if (((_a = peamConfig.searchExporter) == null ? void 0 : _a.type) !== "fileBased") {
2659
+ return nextConfig;
2660
+ }
2661
+ const exporterConfig = peamConfig.searchExporter.config;
2662
+ const indexDir = path__namespace.dirname(exporterConfig.indexPath);
2663
+ const tracingConfig = {
2664
+ "/api/peam": [`./${indexDir}/**/*`]
2665
+ };
2666
+ const nextVersion = getNextVersion();
2667
+ if (!nextVersion) {
2668
+ log4.warn(
2669
+ "Could not determine Next.js version. Adding outputFileTracingIncludes to both experimental and root config."
2670
+ );
2671
+ const existingExperimentalTracing = nextConfig.experimental && typeof nextConfig.experimental === "object" && "outputFileTracingIncludes" in nextConfig.experimental ? nextConfig.experimental.outputFileTracingIncludes : void 0;
2672
+ if (nextConfig.experimental) {
2673
+ Object.assign(nextConfig.experimental, {
2674
+ outputFileTracingIncludes: {
2675
+ ...existingExperimentalTracing || {},
2676
+ ...tracingConfig
2677
+ }
2678
+ });
2679
+ }
2680
+ const existingRootTracing = (_b = nextConfig.outputFileTracingIncludes) != null ? _b : void 0;
2681
+ Object.assign(nextConfig, {
2682
+ outputFileTracingIncludes: {
2683
+ ...existingRootTracing || {},
2684
+ ...tracingConfig
2685
+ }
2686
+ });
2687
+ } else if (nextVersion.major < 15) {
2688
+ const existingTracing = typeof nextConfig.experimental === "object" && "outputFileTracingIncludes" in nextConfig.experimental ? (_c = nextConfig.experimental) == null ? void 0 : _c.outputFileTracingIncludes : void 0;
2689
+ if (nextConfig.experimental) {
2690
+ Object.assign(nextConfig.experimental, {
2691
+ outputFileTracingIncludes: {
2692
+ ...existingTracing || {},
2693
+ ...tracingConfig
2694
+ }
2695
+ });
2696
+ }
2697
+ } else {
2698
+ const existingTracing = (_d = nextConfig.outputFileTracingIncludes) != null ? _d : void 0;
2699
+ Object.assign(nextConfig, {
2700
+ outputFileTracingIncludes: {
2701
+ ...existingTracing || {},
2702
+ ...tracingConfig
2703
+ }
2704
+ });
2705
+ }
2706
+ return nextConfig;
2707
+ }
2700
2708
  function withPeam(peamConfig) {
2701
2709
  return function(nextConfig = {}) {
2702
2710
  setNextConfig(nextConfig, peamConfig);
2703
- return {
2704
- ...nextConfig,
2705
- experimental: {
2706
- ...nextConfig.experimental,
2707
- adapterPath: __require.resolve("../dist/peam.adapter.js")
2708
- }
2709
- };
2711
+ addStubIndex();
2712
+ let updatedNextConfig = { ...nextConfig };
2713
+ updatedNextConfig = addAdapter(updatedNextConfig);
2714
+ updatedNextConfig = addOutputFileTracing(updatedNextConfig, getConfig());
2715
+ return updatedNextConfig;
2710
2716
  };
2711
2717
  }
2712
2718
 
2713
- exports.createPeamAdapter = createPeamAdapter;
2714
- exports.getConfig = getConfig;
2715
- exports.setNextConfig = setNextConfig;
2716
- exports.withPeam = withPeam;
2719
+ // src/index.ts
2720
+ var index_default = withPeam;
2721
+
2722
+ module.exports = index_default;
2717
2723
  //# sourceMappingURL=index.js.map
2718
2724
  //# sourceMappingURL=index.js.map