@sonicjs-cms/core 2.4.0 → 2.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.
Files changed (98) hide show
  1. package/dist/{app-Db0AfT5F.d.cts → app-DV27cjPy.d.cts} +1 -1
  2. package/dist/{app-Db0AfT5F.d.ts → app-DV27cjPy.d.ts} +1 -1
  3. package/dist/{chunk-YIXSSJWD.cjs → chunk-63K7XXRX.cjs} +5 -5
  4. package/dist/{chunk-YIXSSJWD.cjs.map → chunk-63K7XXRX.cjs.map} +1 -1
  5. package/dist/{chunk-VNCYCH3H.js → chunk-7DL5SPPX.js} +59 -5
  6. package/dist/chunk-7DL5SPPX.js.map +1 -0
  7. package/dist/{chunk-AZLU3ROK.cjs → chunk-BZC4FYW7.cjs} +4 -4
  8. package/dist/chunk-BZC4FYW7.cjs.map +1 -0
  9. package/dist/chunk-CLIH2T74.js +403 -0
  10. package/dist/chunk-CLIH2T74.js.map +1 -0
  11. package/dist/{chunk-D2NLCPO2.js → chunk-EVZOVYLO.js} +53 -2
  12. package/dist/chunk-EVZOVYLO.js.map +1 -0
  13. package/dist/{chunk-DXM575E2.js → chunk-EYWR6UA2.js} +6 -6
  14. package/dist/chunk-EYWR6UA2.js.map +1 -0
  15. package/dist/{chunk-CPXAVWCU.js → chunk-F332TENF.js} +278 -3
  16. package/dist/chunk-F332TENF.js.map +1 -0
  17. package/dist/{chunk-FT6NBHNX.js → chunk-F6GZURXJ.js} +2536 -600
  18. package/dist/chunk-F6GZURXJ.js.map +1 -0
  19. package/dist/{chunk-2MI3LZFH.cjs → chunk-IIRVZSP2.cjs} +53 -2
  20. package/dist/chunk-IIRVZSP2.cjs.map +1 -0
  21. package/dist/{chunk-V5LBQN3I.js → chunk-KA2PDJNB.js} +4 -4
  22. package/dist/chunk-KA2PDJNB.js.map +1 -0
  23. package/dist/{chunk-AVPUX57O.js → chunk-KAOWRIFD.js} +3 -3
  24. package/dist/{chunk-AVPUX57O.js.map → chunk-KAOWRIFD.js.map} +1 -1
  25. package/dist/{chunk-ILZ3DP4I.cjs → chunk-MPT5PA6U.cjs} +24 -2
  26. package/dist/chunk-MPT5PA6U.cjs.map +1 -0
  27. package/dist/{chunk-A4SVOGG6.cjs → chunk-N7TDLOUE.cjs} +2696 -762
  28. package/dist/chunk-N7TDLOUE.cjs.map +1 -0
  29. package/dist/{chunk-7I5INVNR.cjs → chunk-T3YIKW2A.cjs} +9 -9
  30. package/dist/chunk-T3YIKW2A.cjs.map +1 -0
  31. package/dist/{chunk-DTLB6UIH.cjs → chunk-Y72M3MVX.cjs} +280 -2
  32. package/dist/chunk-Y72M3MVX.cjs.map +1 -0
  33. package/dist/{chunk-SGAG6FD3.js → chunk-YFJJU26H.js} +24 -2
  34. package/dist/chunk-YFJJU26H.js.map +1 -0
  35. package/dist/chunk-YHW27CBV.cjs +406 -0
  36. package/dist/chunk-YHW27CBV.cjs.map +1 -0
  37. package/dist/{chunk-FYEDK7K7.cjs → chunk-YMTTGHEK.cjs} +61 -4
  38. package/dist/chunk-YMTTGHEK.cjs.map +1 -0
  39. package/dist/{collection-config-FLlGtsh9.d.cts → collection-config-BF95LgQb.d.cts} +10 -2
  40. package/dist/{collection-config-FLlGtsh9.d.ts → collection-config-BF95LgQb.d.ts} +10 -2
  41. package/dist/index.cjs +2001 -142
  42. package/dist/index.cjs.map +1 -1
  43. package/dist/index.d.cts +504 -9
  44. package/dist/index.d.ts +504 -9
  45. package/dist/index.js +1893 -41
  46. package/dist/index.js.map +1 -1
  47. package/dist/middleware.cjs +24 -24
  48. package/dist/middleware.d.cts +1 -1
  49. package/dist/middleware.d.ts +1 -1
  50. package/dist/middleware.js +3 -3
  51. package/dist/migrations-QNYAWQLB.cjs +13 -0
  52. package/dist/{migrations-32QAYLWJ.cjs.map → migrations-QNYAWQLB.cjs.map} +1 -1
  53. package/dist/migrations-R6NQBKQV.js +4 -0
  54. package/dist/{migrations-57ZHBQ4X.js.map → migrations-R6NQBKQV.js.map} +1 -1
  55. package/dist/{plugin-bootstrap-CDh0JHtW.d.ts → plugin-bootstrap-CB-xaBfK.d.ts} +2 -2
  56. package/dist/{plugin-bootstrap-C0E3jdz-.d.cts → plugin-bootstrap-U-cw9jn3.d.cts} +2 -2
  57. package/dist/plugin-manager-Baa6xXqB.d.ts +328 -0
  58. package/dist/plugin-manager-vBal9Zip.d.cts +328 -0
  59. package/dist/plugins.cjs +20 -7
  60. package/dist/plugins.d.cts +53 -310
  61. package/dist/plugins.d.ts +53 -310
  62. package/dist/plugins.js +2 -1
  63. package/dist/routes.cjs +27 -26
  64. package/dist/routes.d.cts +1 -1
  65. package/dist/routes.d.ts +1 -1
  66. package/dist/routes.js +7 -6
  67. package/dist/services.cjs +16 -16
  68. package/dist/services.d.cts +2 -2
  69. package/dist/services.d.ts +2 -2
  70. package/dist/services.js +2 -2
  71. package/dist/templates.cjs +17 -17
  72. package/dist/templates.js +2 -2
  73. package/dist/types.d.cts +1 -1
  74. package/dist/types.d.ts +1 -1
  75. package/dist/utils.cjs +23 -11
  76. package/dist/utils.d.cts +38 -1
  77. package/dist/utils.d.ts +38 -1
  78. package/dist/utils.js +1 -1
  79. package/migrations/027_fix_slug_field_type.sql +18 -0
  80. package/migrations/028_fix_slug_field_type_in_schemas.sql +30 -0
  81. package/migrations/029_ai_search_plugin.sql +45 -0
  82. package/package.json +5 -2
  83. package/dist/chunk-2MI3LZFH.cjs.map +0 -1
  84. package/dist/chunk-7I5INVNR.cjs.map +0 -1
  85. package/dist/chunk-A4SVOGG6.cjs.map +0 -1
  86. package/dist/chunk-AZLU3ROK.cjs.map +0 -1
  87. package/dist/chunk-CPXAVWCU.js.map +0 -1
  88. package/dist/chunk-D2NLCPO2.js.map +0 -1
  89. package/dist/chunk-DTLB6UIH.cjs.map +0 -1
  90. package/dist/chunk-DXM575E2.js.map +0 -1
  91. package/dist/chunk-FT6NBHNX.js.map +0 -1
  92. package/dist/chunk-FYEDK7K7.cjs.map +0 -1
  93. package/dist/chunk-ILZ3DP4I.cjs.map +0 -1
  94. package/dist/chunk-SGAG6FD3.js.map +0 -1
  95. package/dist/chunk-V5LBQN3I.js.map +0 -1
  96. package/dist/chunk-VNCYCH3H.js.map +0 -1
  97. package/dist/migrations-32QAYLWJ.cjs +0 -13
  98. package/dist/migrations-57ZHBQ4X.js +0 -4
package/dist/index.js CHANGED
@@ -1,17 +1,19 @@
1
- import { PluginBuilder, api_default, api_media_default, api_system_default, admin_api_default, router, adminCollectionsRoutes, adminSettingsRoutes, admin_content_default, adminMediaRoutes, adminPluginRoutes, adminLogsRoutes, userRoutes, auth_default, test_cleanup_default, checkAdminUserExists } from './chunk-FT6NBHNX.js';
2
- export { ROUTES_INFO, admin_api_default as adminApiRoutes, adminCheckboxRoutes, admin_code_examples_default as adminCodeExamplesRoutes, adminCollectionsRoutes, admin_content_default as adminContentRoutes, router as adminDashboardRoutes, adminDesignRoutes, adminLogsRoutes, adminMediaRoutes, adminPluginRoutes, adminSettingsRoutes, admin_testimonials_default as adminTestimonialsRoutes, userRoutes as adminUsersRoutes, api_content_crud_default as apiContentCrudRoutes, api_media_default as apiMediaRoutes, api_default as apiRoutes, api_system_default as apiSystemRoutes, auth_default as authRoutes } from './chunk-FT6NBHNX.js';
1
+ import { api_default, api_media_default, api_system_default, admin_api_default, router, adminCollectionsRoutes, adminSettingsRoutes, admin_content_default, adminMediaRoutes, adminPluginRoutes, adminLogsRoutes, userRoutes, auth_default, test_cleanup_default } from './chunk-F6GZURXJ.js';
2
+ export { ROUTES_INFO, admin_api_default as adminApiRoutes, adminCheckboxRoutes, admin_code_examples_default as adminCodeExamplesRoutes, adminCollectionsRoutes, admin_content_default as adminContentRoutes, router as adminDashboardRoutes, adminDesignRoutes, adminLogsRoutes, adminMediaRoutes, adminPluginRoutes, adminSettingsRoutes, admin_testimonials_default as adminTestimonialsRoutes, userRoutes as adminUsersRoutes, api_content_crud_default as apiContentCrudRoutes, api_media_default as apiMediaRoutes, api_default as apiRoutes, api_system_default as apiSystemRoutes, auth_default as authRoutes } from './chunk-F6GZURXJ.js';
3
3
  import { schema_exports } from './chunk-3YNNVSMC.js';
4
4
  export { Logger, apiTokens, collections, content, contentVersions, getLogger, initLogger, insertCollectionSchema, insertContentSchema, insertLogConfigSchema, insertMediaSchema, insertPluginActivityLogSchema, insertPluginAssetSchema, insertPluginHookSchema, insertPluginRouteSchema, insertPluginSchema, insertSystemLogSchema, insertUserSchema, insertWorkflowHistorySchema, logConfig, media, pluginActivityLog, pluginAssets, pluginHooks, pluginRoutes, plugins, selectCollectionSchema, selectContentSchema, selectLogConfigSchema, selectMediaSchema, selectPluginActivityLogSchema, selectPluginAssetSchema, selectPluginHookSchema, selectPluginRouteSchema, selectPluginSchema, selectSystemLogSchema, selectUserSchema, selectWorkflowHistorySchema, systemLogs, users, workflowHistory } from './chunk-3YNNVSMC.js';
5
- import { AuthManager, metricsMiddleware, bootstrapMiddleware, requireAuth } from './chunk-DXM575E2.js';
6
- export { AuthManager, PermissionManager, bootstrapMiddleware, cacheHeaders, compressionMiddleware, detailedLoggingMiddleware, getActivePlugins, isPluginActive, logActivity, loggingMiddleware, optionalAuth, performanceLoggingMiddleware, requireActivePlugin, requireActivePlugins, requireAnyPermission, requireAuth, requirePermission, requireRole, securityHeaders, securityLoggingMiddleware } from './chunk-DXM575E2.js';
7
- export { PluginBootstrapService, PluginService as PluginServiceClass, cleanupRemovedCollections, fullCollectionSync, getAvailableCollectionNames, getManagedCollections, isCollectionManaged, loadCollectionConfig, loadCollectionConfigs, registerCollections, syncCollection, syncCollections, validateCollectionConfig } from './chunk-SGAG6FD3.js';
8
- export { MigrationService } from './chunk-D2NLCPO2.js';
9
- export { renderFilterBar } from './chunk-AVPUX57O.js';
10
- import { init_admin_layout_catalyst_template, renderAdminLayout, adminLayoutV2, renderAdminLayoutCatalyst } from './chunk-V5LBQN3I.js';
11
- export { getConfirmationDialogScript, renderAlert, renderConfirmationDialog, renderForm, renderFormField, renderPagination, renderTable } from './chunk-V5LBQN3I.js';
12
- export { HookSystemImpl, HookUtils, PluginManager as PluginManagerClass, PluginRegistryImpl, PluginValidator as PluginValidatorClass, ScopedHookSystem as ScopedHookSystemClass } from './chunk-CPXAVWCU.js';
13
- import { package_default, getCoreVersion } from './chunk-VNCYCH3H.js';
14
- export { QueryFilterBuilder, SONICJS_VERSION, TemplateRenderer, buildQuery, escapeHtml, getCoreVersion, renderTemplate, sanitizeInput, sanitizeObject, templateRenderer } from './chunk-VNCYCH3H.js';
5
+ import { requireAuth, AuthManager, metricsMiddleware, bootstrapMiddleware } from './chunk-EYWR6UA2.js';
6
+ export { AuthManager, PermissionManager, bootstrapMiddleware, cacheHeaders, compressionMiddleware, detailedLoggingMiddleware, getActivePlugins, isPluginActive, logActivity, loggingMiddleware, optionalAuth, performanceLoggingMiddleware, requireActivePlugin, requireActivePlugins, requireAnyPermission, requireAuth, requirePermission, requireRole, securityHeaders, securityLoggingMiddleware } from './chunk-EYWR6UA2.js';
7
+ export { PluginBootstrapService, PluginService as PluginServiceClass, cleanupRemovedCollections, fullCollectionSync, getAvailableCollectionNames, getManagedCollections, isCollectionManaged, loadCollectionConfig, loadCollectionConfigs, registerCollections, syncCollection, syncCollections, validateCollectionConfig } from './chunk-YFJJU26H.js';
8
+ export { MigrationService } from './chunk-EVZOVYLO.js';
9
+ export { renderFilterBar } from './chunk-KAOWRIFD.js';
10
+ import { init_admin_layout_catalyst_template, renderAdminLayout, adminLayoutV2, renderAdminLayoutCatalyst } from './chunk-KA2PDJNB.js';
11
+ export { getConfirmationDialogScript, renderAlert, renderConfirmationDialog, renderForm, renderFormField, renderPagination, renderTable } from './chunk-KA2PDJNB.js';
12
+ export { HookSystemImpl, HookUtils, PluginManager as PluginManagerClass, PluginRegistryImpl, PluginValidator as PluginValidatorClass, ScopedHookSystem as ScopedHookSystemClass } from './chunk-F332TENF.js';
13
+ import { PluginBuilder } from './chunk-CLIH2T74.js';
14
+ export { PluginBuilder, PluginHelpers } from './chunk-CLIH2T74.js';
15
+ import { package_default, getCoreVersion } from './chunk-7DL5SPPX.js';
16
+ export { QueryFilterBuilder, SONICJS_VERSION, TemplateRenderer, buildQuery, escapeHtml, getCoreVersion, renderTemplate, sanitizeInput, sanitizeObject, templateRenderer } from './chunk-7DL5SPPX.js';
15
17
  import './chunk-X7ZAEI5S.js';
16
18
  export { metricsTracker } from './chunk-FICTAGD4.js';
17
19
  export { HOOKS } from './chunk-LOUJRBXV.js';
@@ -22,31 +24,6 @@ import { setCookie } from 'hono/cookie';
22
24
  import { z } from 'zod';
23
25
  import { drizzle } from 'drizzle-orm/d1';
24
26
 
25
- // src/middleware/admin-setup.ts
26
- function adminSetupMiddleware() {
27
- return async (c, next) => {
28
- const path = new URL(c.req.url).pathname;
29
- if (path.startsWith("/auth/")) {
30
- return next();
31
- }
32
- if (path.match(/\.(js|css|ico|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/)) {
33
- return next();
34
- }
35
- if (path === "/health") {
36
- return next();
37
- }
38
- if (path.startsWith("/api/")) {
39
- return next();
40
- }
41
- const db = c.env.DB;
42
- const adminExists = await checkAdminUserExists(db);
43
- if (!adminExists) {
44
- return c.redirect("/auth/register?setup=true");
45
- }
46
- return next();
47
- };
48
- }
49
-
50
27
  // src/plugins/core-plugins/database-tools-plugin/services/database-service.ts
51
28
  var DatabaseToolsService = class {
52
29
  constructor(db) {
@@ -2230,8 +2207,8 @@ function createOTPLoginPlugin() {
2230
2207
  requiresAuth: false,
2231
2208
  priority: 100
2232
2209
  });
2233
- const adminRoutes = new Hono();
2234
- adminRoutes.get("/settings", async (c) => {
2210
+ const adminRoutes2 = new Hono();
2211
+ adminRoutes2.get("/settings", async (c) => {
2235
2212
  const user = c.get("user");
2236
2213
  const contentHTML = await html`
2237
2214
  <div class="p-8">
@@ -2411,7 +2388,7 @@ function createOTPLoginPlugin() {
2411
2388
  })
2412
2389
  );
2413
2390
  });
2414
- builder.addRoute("/admin/plugins/otp-login", adminRoutes, {
2391
+ builder.addRoute("/admin/plugins/otp-login", adminRoutes2, {
2415
2392
  description: "OTP login admin interface",
2416
2393
  requiresAuth: true,
2417
2394
  priority: 85
@@ -2432,6 +2409,1840 @@ function createOTPLoginPlugin() {
2432
2409
  return builder.build();
2433
2410
  }
2434
2411
  var otpLoginPlugin = createOTPLoginPlugin();
2412
+
2413
+ // src/plugins/core-plugins/ai-search-plugin/services/embedding.service.ts
2414
+ var EmbeddingService = class {
2415
+ constructor(ai) {
2416
+ this.ai = ai;
2417
+ }
2418
+ /**
2419
+ * Generate embedding for a single text
2420
+ */
2421
+ async generateEmbedding(text) {
2422
+ try {
2423
+ const response = await this.ai.run("@cf/baai/bge-base-en-v1.5", {
2424
+ text: this.preprocessText(text)
2425
+ });
2426
+ if (response.data && response.data.length > 0) {
2427
+ return response.data[0];
2428
+ }
2429
+ throw new Error("No embedding data returned");
2430
+ } catch (error) {
2431
+ console.error("[EmbeddingService] Error generating embedding:", error);
2432
+ throw error;
2433
+ }
2434
+ }
2435
+ /**
2436
+ * Generate embeddings for multiple texts (batch processing)
2437
+ */
2438
+ async generateBatch(texts) {
2439
+ try {
2440
+ const batchSize = 10;
2441
+ const batches = [];
2442
+ for (let i = 0; i < texts.length; i += batchSize) {
2443
+ batches.push(texts.slice(i, i + batchSize));
2444
+ }
2445
+ const allEmbeddings = [];
2446
+ for (const batch of batches) {
2447
+ const batchEmbeddings = await Promise.all(
2448
+ batch.map((text) => this.generateEmbedding(text))
2449
+ );
2450
+ allEmbeddings.push(...batchEmbeddings);
2451
+ }
2452
+ return allEmbeddings;
2453
+ } catch (error) {
2454
+ console.error("[EmbeddingService] Error generating batch embeddings:", error);
2455
+ throw error;
2456
+ }
2457
+ }
2458
+ /**
2459
+ * Preprocess text before generating embedding
2460
+ * - Trim whitespace
2461
+ * - Limit length to avoid token limits
2462
+ * - Remove special characters that might cause issues
2463
+ */
2464
+ preprocessText(text) {
2465
+ if (!text) return "";
2466
+ let processed = text.trim().replace(/\s+/g, " ");
2467
+ if (processed.length > 8e3) {
2468
+ processed = processed.substring(0, 8e3);
2469
+ }
2470
+ return processed;
2471
+ }
2472
+ /**
2473
+ * Calculate cosine similarity between two embeddings
2474
+ */
2475
+ cosineSimilarity(a, b) {
2476
+ if (a.length !== b.length) {
2477
+ throw new Error("Embeddings must have same dimensions");
2478
+ }
2479
+ let dotProduct = 0;
2480
+ let normA = 0;
2481
+ let normB = 0;
2482
+ for (let i = 0; i < a.length; i++) {
2483
+ const aVal = a[i] ?? 0;
2484
+ const bVal = b[i] ?? 0;
2485
+ dotProduct += aVal * bVal;
2486
+ normA += aVal * aVal;
2487
+ normB += bVal * bVal;
2488
+ }
2489
+ return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
2490
+ }
2491
+ };
2492
+
2493
+ // src/plugins/core-plugins/ai-search-plugin/services/chunking.service.ts
2494
+ var ChunkingService = class {
2495
+ // Default chunk size (in approximate tokens)
2496
+ CHUNK_SIZE = 500;
2497
+ CHUNK_OVERLAP = 50;
2498
+ /**
2499
+ * Chunk a single content item
2500
+ */
2501
+ chunkContent(contentId, collectionId, title, data, metadata = {}) {
2502
+ const text = this.extractText(data);
2503
+ if (!text || text.trim().length === 0) {
2504
+ console.warn(`[ChunkingService] No text found for content ${contentId}`);
2505
+ return [];
2506
+ }
2507
+ const textChunks = this.splitIntoChunks(text);
2508
+ return textChunks.map((chunkText, index) => ({
2509
+ id: `${contentId}_chunk_${index}`,
2510
+ content_id: contentId,
2511
+ collection_id: collectionId,
2512
+ title,
2513
+ text: chunkText,
2514
+ chunk_index: index,
2515
+ metadata: {
2516
+ ...metadata,
2517
+ total_chunks: textChunks.length
2518
+ }
2519
+ }));
2520
+ }
2521
+ /**
2522
+ * Chunk multiple content items
2523
+ */
2524
+ chunkContentBatch(items) {
2525
+ const allChunks = [];
2526
+ for (const item of items) {
2527
+ const chunks = this.chunkContent(
2528
+ item.id,
2529
+ item.collection_id,
2530
+ item.title,
2531
+ item.data,
2532
+ item.metadata
2533
+ );
2534
+ allChunks.push(...chunks);
2535
+ }
2536
+ return allChunks;
2537
+ }
2538
+ /**
2539
+ * Extract all text from content data
2540
+ */
2541
+ extractText(data) {
2542
+ const parts = [];
2543
+ if (data.title) parts.push(String(data.title));
2544
+ if (data.name) parts.push(String(data.name));
2545
+ if (data.description) parts.push(String(data.description));
2546
+ if (data.content) parts.push(String(data.content));
2547
+ if (data.body) parts.push(String(data.body));
2548
+ if (data.text) parts.push(String(data.text));
2549
+ if (data.summary) parts.push(String(data.summary));
2550
+ const extractRecursive = (obj) => {
2551
+ if (typeof obj === "string") {
2552
+ if (obj.length > 10 && !obj.startsWith("http")) {
2553
+ parts.push(obj);
2554
+ }
2555
+ } else if (Array.isArray(obj)) {
2556
+ obj.forEach(extractRecursive);
2557
+ } else if (obj && typeof obj === "object") {
2558
+ const skipKeys = ["id", "slug", "url", "image", "thumbnail", "metadata"];
2559
+ Object.entries(obj).forEach(([key, value]) => {
2560
+ if (!skipKeys.includes(key.toLowerCase())) {
2561
+ extractRecursive(value);
2562
+ }
2563
+ });
2564
+ }
2565
+ };
2566
+ extractRecursive(data);
2567
+ return parts.join("\n\n").trim();
2568
+ }
2569
+ /**
2570
+ * Split text into overlapping chunks
2571
+ */
2572
+ splitIntoChunks(text) {
2573
+ const words = text.split(/\s+/);
2574
+ if (words.length <= this.CHUNK_SIZE) {
2575
+ return [text];
2576
+ }
2577
+ const chunks = [];
2578
+ let startIndex = 0;
2579
+ while (startIndex < words.length) {
2580
+ const endIndex = Math.min(startIndex + this.CHUNK_SIZE, words.length);
2581
+ const chunk = words.slice(startIndex, endIndex).join(" ");
2582
+ chunks.push(chunk);
2583
+ startIndex += this.CHUNK_SIZE - this.CHUNK_OVERLAP;
2584
+ if (startIndex >= words.length - this.CHUNK_OVERLAP) {
2585
+ break;
2586
+ }
2587
+ }
2588
+ return chunks;
2589
+ }
2590
+ /**
2591
+ * Get optimal chunk size based on content type
2592
+ */
2593
+ getOptimalChunkSize(contentType) {
2594
+ switch (contentType) {
2595
+ case "blog_posts":
2596
+ case "articles":
2597
+ return 600;
2598
+ // Larger chunks for long-form content
2599
+ case "products":
2600
+ case "pages":
2601
+ return 400;
2602
+ // Medium chunks for structured content
2603
+ case "messages":
2604
+ case "comments":
2605
+ return 200;
2606
+ // Small chunks for short content
2607
+ default:
2608
+ return this.CHUNK_SIZE;
2609
+ }
2610
+ }
2611
+ };
2612
+
2613
+ // src/plugins/core-plugins/ai-search-plugin/services/custom-rag.service.ts
2614
+ var CustomRAGService = class {
2615
+ constructor(db, ai, vectorize) {
2616
+ this.db = db;
2617
+ this.ai = ai;
2618
+ this.vectorize = vectorize;
2619
+ this.embeddingService = new EmbeddingService(ai);
2620
+ this.chunkingService = new ChunkingService();
2621
+ }
2622
+ embeddingService;
2623
+ chunkingService;
2624
+ /**
2625
+ * Index all content from a collection
2626
+ */
2627
+ async indexCollection(collectionId) {
2628
+ console.log(`[CustomRAG] Starting indexing for collection: ${collectionId}`);
2629
+ try {
2630
+ const { results: contentItems } = await this.db.prepare(`
2631
+ SELECT c.id, c.title, c.data, c.collection_id, c.status,
2632
+ c.created_at, c.updated_at, c.author_id,
2633
+ col.name as collection_name, col.display_name as collection_display_name
2634
+ FROM content c
2635
+ JOIN collections col ON c.collection_id = col.id
2636
+ WHERE c.collection_id = ? AND c.status = 'published'
2637
+ `).bind(collectionId).all();
2638
+ const totalItems = contentItems?.length || 0;
2639
+ if (totalItems === 0) {
2640
+ console.log(`[CustomRAG] No content found in collection ${collectionId}`);
2641
+ return { total_items: 0, total_chunks: 0, indexed_chunks: 0, errors: 0 };
2642
+ }
2643
+ const items = (contentItems || []).map((item) => ({
2644
+ id: item.id,
2645
+ collection_id: item.collection_id,
2646
+ title: item.title || "Untitled",
2647
+ data: typeof item.data === "string" ? JSON.parse(item.data) : item.data,
2648
+ metadata: {
2649
+ status: item.status,
2650
+ created_at: item.created_at,
2651
+ updated_at: item.updated_at,
2652
+ author_id: item.author_id,
2653
+ collection_name: item.collection_name,
2654
+ collection_display_name: item.collection_display_name
2655
+ }
2656
+ }));
2657
+ const chunks = this.chunkingService.chunkContentBatch(items);
2658
+ const totalChunks = chunks.length;
2659
+ console.log(`[CustomRAG] Generated ${totalChunks} chunks from ${totalItems} items`);
2660
+ const embeddings = await this.embeddingService.generateBatch(
2661
+ chunks.map((c) => `${c.title}
2662
+
2663
+ ${c.text}`)
2664
+ );
2665
+ console.log(`[CustomRAG] Generated ${embeddings.length} embeddings`);
2666
+ let indexedChunks = 0;
2667
+ let errors = 0;
2668
+ const batchSize = 100;
2669
+ for (let i = 0; i < chunks.length; i += batchSize) {
2670
+ const chunkBatch = chunks.slice(i, i + batchSize);
2671
+ const embeddingBatch = embeddings.slice(i, i + batchSize);
2672
+ try {
2673
+ await this.vectorize.upsert(
2674
+ chunkBatch.map((chunk, idx) => ({
2675
+ id: chunk.id,
2676
+ values: embeddingBatch[idx],
2677
+ metadata: {
2678
+ content_id: chunk.content_id,
2679
+ collection_id: chunk.collection_id,
2680
+ title: chunk.title,
2681
+ text: chunk.text.substring(0, 500),
2682
+ // Store snippet for display
2683
+ chunk_index: chunk.chunk_index,
2684
+ ...chunk.metadata
2685
+ }
2686
+ }))
2687
+ );
2688
+ indexedChunks += chunkBatch.length;
2689
+ console.log(`[CustomRAG] Indexed batch ${i / batchSize + 1}: ${chunkBatch.length} chunks`);
2690
+ } catch (error) {
2691
+ console.error(`[CustomRAG] Error indexing batch ${i / batchSize + 1}:`, error);
2692
+ errors += chunkBatch.length;
2693
+ }
2694
+ }
2695
+ console.log(`[CustomRAG] Indexing complete: ${indexedChunks}/${totalChunks} chunks indexed`);
2696
+ return {
2697
+ total_items: totalItems,
2698
+ total_chunks: totalChunks,
2699
+ indexed_chunks: indexedChunks,
2700
+ errors
2701
+ };
2702
+ } catch (error) {
2703
+ console.error(`[CustomRAG] Error indexing collection ${collectionId}:`, error);
2704
+ throw error;
2705
+ }
2706
+ }
2707
+ /**
2708
+ * Search using RAG (semantic search with Vectorize)
2709
+ */
2710
+ async search(query, settings) {
2711
+ const startTime = Date.now();
2712
+ try {
2713
+ console.log(`[CustomRAG] Searching for: "${query.query}"`);
2714
+ const queryEmbedding = await this.embeddingService.generateEmbedding(query.query);
2715
+ const filter = {};
2716
+ if (query.filters?.collections && query.filters.collections.length > 0) {
2717
+ filter.collection_id = { $in: query.filters.collections };
2718
+ } else if (settings.selected_collections.length > 0) {
2719
+ filter.collection_id = { $in: settings.selected_collections };
2720
+ }
2721
+ if (query.filters?.status && query.filters.status.length > 0) {
2722
+ filter.status = { $in: query.filters.status };
2723
+ }
2724
+ const vectorResults = await this.vectorize.query(queryEmbedding, {
2725
+ topK: 50,
2726
+ // Max allowed with returnMetadata: true
2727
+ returnMetadata: true
2728
+ });
2729
+ let filteredMatches = vectorResults.matches || [];
2730
+ if (filter.collection_id?.$in && Array.isArray(filter.collection_id.$in)) {
2731
+ const allowedCollections = filter.collection_id.$in;
2732
+ filteredMatches = filteredMatches.filter(
2733
+ (match) => allowedCollections.includes(match.metadata?.collection_id)
2734
+ );
2735
+ }
2736
+ if (filter.status?.$in && Array.isArray(filter.status.$in)) {
2737
+ const allowedStatuses = filter.status.$in;
2738
+ filteredMatches = filteredMatches.filter(
2739
+ (match) => allowedStatuses.includes(match.metadata?.status)
2740
+ );
2741
+ }
2742
+ const topK = query.limit || settings.results_limit || 20;
2743
+ filteredMatches = filteredMatches.slice(0, topK);
2744
+ vectorResults.matches = filteredMatches;
2745
+ if (!vectorResults.matches || vectorResults.matches.length === 0) {
2746
+ return {
2747
+ results: [],
2748
+ total: 0,
2749
+ query_time_ms: Date.now() - startTime,
2750
+ mode: "ai"
2751
+ };
2752
+ }
2753
+ const contentIds = [...new Set(
2754
+ vectorResults.matches.map((m) => m.metadata.content_id)
2755
+ )];
2756
+ const placeholders = contentIds.map(() => "?").join(",");
2757
+ const { results: contentItems } = await this.db.prepare(`
2758
+ SELECT c.id, c.title, c.slug, c.collection_id, c.status,
2759
+ c.created_at, c.updated_at, c.author_id,
2760
+ col.display_name as collection_name
2761
+ FROM content c
2762
+ JOIN collections col ON c.collection_id = col.id
2763
+ WHERE c.id IN (${placeholders})
2764
+ `).bind(...contentIds).all();
2765
+ const searchResults = (contentItems || []).map((item) => {
2766
+ const matchingChunks = vectorResults.matches.filter(
2767
+ (m) => m.metadata.content_id === item.id
2768
+ );
2769
+ const bestMatch = matchingChunks.reduce(
2770
+ (best, current) => current.score > (best?.score || 0) ? current : best,
2771
+ null
2772
+ );
2773
+ return {
2774
+ id: item.id,
2775
+ title: item.title || "Untitled",
2776
+ slug: item.slug || "",
2777
+ collection_id: item.collection_id,
2778
+ collection_name: item.collection_name,
2779
+ snippet: bestMatch?.metadata?.text || "",
2780
+ relevance_score: bestMatch?.score || 0,
2781
+ status: item.status,
2782
+ created_at: item.created_at,
2783
+ updated_at: item.updated_at
2784
+ };
2785
+ });
2786
+ searchResults.sort((a, b) => (b.relevance_score || 0) - (a.relevance_score || 0));
2787
+ const queryTime = Date.now() - startTime;
2788
+ console.log(`[CustomRAG] Search completed in ${queryTime}ms, ${searchResults.length} results`);
2789
+ return {
2790
+ results: searchResults,
2791
+ total: searchResults.length,
2792
+ query_time_ms: queryTime,
2793
+ mode: "ai"
2794
+ };
2795
+ } catch (error) {
2796
+ console.error("[CustomRAG] Search error:", error);
2797
+ throw error;
2798
+ }
2799
+ }
2800
+ /**
2801
+ * Update index for a single content item
2802
+ */
2803
+ async updateContentIndex(contentId) {
2804
+ try {
2805
+ const content2 = await this.db.prepare(`
2806
+ SELECT c.id, c.title, c.data, c.collection_id, c.status,
2807
+ c.created_at, c.updated_at, c.author_id,
2808
+ col.name as collection_name, col.display_name as collection_display_name
2809
+ FROM content c
2810
+ JOIN collections col ON c.collection_id = col.id
2811
+ WHERE c.id = ?
2812
+ `).bind(contentId).first();
2813
+ if (!content2) {
2814
+ console.warn(`[CustomRAG] Content ${contentId} not found`);
2815
+ return;
2816
+ }
2817
+ if (content2.status !== "published") {
2818
+ await this.removeContentFromIndex(contentId);
2819
+ return;
2820
+ }
2821
+ const chunks = this.chunkingService.chunkContent(
2822
+ content2.id,
2823
+ content2.collection_id,
2824
+ content2.title || "Untitled",
2825
+ typeof content2.data === "string" ? JSON.parse(content2.data) : content2.data,
2826
+ {
2827
+ status: content2.status,
2828
+ created_at: content2.created_at,
2829
+ updated_at: content2.updated_at,
2830
+ author_id: content2.author_id,
2831
+ collection_name: content2.collection_name,
2832
+ collection_display_name: content2.collection_display_name
2833
+ }
2834
+ );
2835
+ const embeddings = await this.embeddingService.generateBatch(
2836
+ chunks.map((c) => `${c.title}
2837
+
2838
+ ${c.text}`)
2839
+ );
2840
+ await this.vectorize.upsert(
2841
+ chunks.map((chunk, idx) => ({
2842
+ id: chunk.id,
2843
+ values: embeddings[idx],
2844
+ metadata: {
2845
+ content_id: chunk.content_id,
2846
+ collection_id: chunk.collection_id,
2847
+ title: chunk.title,
2848
+ text: chunk.text.substring(0, 500),
2849
+ chunk_index: chunk.chunk_index,
2850
+ ...chunk.metadata
2851
+ }
2852
+ }))
2853
+ );
2854
+ console.log(`[CustomRAG] Updated index for content ${contentId}: ${chunks.length} chunks`);
2855
+ } catch (error) {
2856
+ console.error(`[CustomRAG] Error updating index for ${contentId}:`, error);
2857
+ throw error;
2858
+ }
2859
+ }
2860
+ /**
2861
+ * Remove content from index
2862
+ */
2863
+ async removeContentFromIndex(contentId) {
2864
+ try {
2865
+ console.log(`[CustomRAG] Removing content ${contentId} from index`);
2866
+ } catch (error) {
2867
+ console.error(`[CustomRAG] Error removing content ${contentId}:`, error);
2868
+ throw error;
2869
+ }
2870
+ }
2871
+ /**
2872
+ * Get search suggestions based on query
2873
+ */
2874
+ async getSuggestions(partialQuery, limit = 5) {
2875
+ try {
2876
+ const queryEmbedding = await this.embeddingService.generateEmbedding(partialQuery);
2877
+ const results = await this.vectorize.query(queryEmbedding, {
2878
+ topK: limit * 2,
2879
+ // Get more to filter
2880
+ returnMetadata: true
2881
+ });
2882
+ const suggestions = [...new Set(
2883
+ results.matches?.map((m) => m.metadata.title).filter(Boolean) || []
2884
+ )].slice(0, limit);
2885
+ return suggestions;
2886
+ } catch (error) {
2887
+ console.error("[CustomRAG] Error getting suggestions:", error);
2888
+ return [];
2889
+ }
2890
+ }
2891
+ /**
2892
+ * Check if Vectorize is available and configured
2893
+ */
2894
+ isAvailable() {
2895
+ return !!this.vectorize && !!this.ai;
2896
+ }
2897
+ };
2898
+
2899
+ // src/plugins/core-plugins/ai-search-plugin/services/ai-search.ts
2900
+ var AISearchService = class {
2901
+ constructor(db, ai, vectorize) {
2902
+ this.db = db;
2903
+ this.ai = ai;
2904
+ this.vectorize = vectorize;
2905
+ if (this.ai && this.vectorize) {
2906
+ this.customRAG = new CustomRAGService(db, ai, vectorize);
2907
+ console.log("[AISearchService] Custom RAG initialized");
2908
+ } else {
2909
+ console.log("[AISearchService] Custom RAG not available, using keyword search only");
2910
+ }
2911
+ }
2912
+ customRAG;
2913
+ /**
2914
+ * Get plugin settings
2915
+ */
2916
+ async getSettings() {
2917
+ try {
2918
+ const plugin = await this.db.prepare(`SELECT settings FROM plugins WHERE id = ? LIMIT 1`).bind("ai-search").first();
2919
+ if (!plugin || !plugin.settings) {
2920
+ return this.getDefaultSettings();
2921
+ }
2922
+ return JSON.parse(plugin.settings);
2923
+ } catch (error) {
2924
+ console.error("Error fetching AI Search settings:", error);
2925
+ return this.getDefaultSettings();
2926
+ }
2927
+ }
2928
+ /**
2929
+ * Get default settings
2930
+ */
2931
+ getDefaultSettings() {
2932
+ return {
2933
+ enabled: true,
2934
+ ai_mode_enabled: true,
2935
+ selected_collections: [],
2936
+ dismissed_collections: [],
2937
+ autocomplete_enabled: true,
2938
+ cache_duration: 1,
2939
+ results_limit: 20,
2940
+ index_media: false
2941
+ };
2942
+ }
2943
+ /**
2944
+ * Update plugin settings
2945
+ */
2946
+ async updateSettings(settings) {
2947
+ const existing = await this.getSettings();
2948
+ const updated = {
2949
+ ...existing,
2950
+ ...settings
2951
+ };
2952
+ try {
2953
+ await this.db.prepare(`
2954
+ UPDATE plugins
2955
+ SET settings = ?,
2956
+ updated_at = unixepoch()
2957
+ WHERE id = 'ai-search'
2958
+ `).bind(JSON.stringify(updated)).run();
2959
+ return updated;
2960
+ } catch (error) {
2961
+ console.error("Error updating AI Search settings:", error);
2962
+ throw error;
2963
+ }
2964
+ }
2965
+ /**
2966
+ * Detect new collections that aren't indexed or dismissed
2967
+ */
2968
+ async detectNewCollections() {
2969
+ try {
2970
+ const collectionsStmt = this.db.prepare(
2971
+ "SELECT id, name, display_name, description FROM collections WHERE is_active = 1"
2972
+ );
2973
+ const { results: allCollections } = await collectionsStmt.all();
2974
+ const collections2 = (allCollections || []).filter(
2975
+ (col) => {
2976
+ if (!col.name) return false;
2977
+ const name = col.name.toLowerCase();
2978
+ return !name.startsWith("test_") && !name.endsWith("_test") && name !== "test_collection" && !name.includes("_test_") && name !== "large_payload_test" && name !== "concurrent_test";
2979
+ }
2980
+ );
2981
+ const settings = await this.getSettings();
2982
+ const selected = settings?.selected_collections || [];
2983
+ const dismissed = settings?.dismissed_collections || [];
2984
+ const notifications = [];
2985
+ for (const collection of collections2 || []) {
2986
+ const collectionId = String(collection.id);
2987
+ if (selected.includes(collectionId) || dismissed.includes(collectionId)) {
2988
+ continue;
2989
+ }
2990
+ const countStmt = this.db.prepare(
2991
+ "SELECT COUNT(*) as count FROM content WHERE collection_id = ?"
2992
+ );
2993
+ const countResult = await countStmt.bind(collectionId).first();
2994
+ const itemCount = countResult?.count || 0;
2995
+ notifications.push({
2996
+ collection: {
2997
+ id: collectionId,
2998
+ name: collection.name,
2999
+ display_name: collection.display_name,
3000
+ description: collection.description,
3001
+ item_count: itemCount,
3002
+ is_indexed: false,
3003
+ is_dismissed: false,
3004
+ is_new: true
3005
+ },
3006
+ message: `New collection "${collection.display_name}" with ${itemCount} items available for indexing`
3007
+ });
3008
+ }
3009
+ return notifications;
3010
+ } catch (error) {
3011
+ console.error("Error detecting new collections:", error);
3012
+ return [];
3013
+ }
3014
+ }
3015
+ /**
3016
+ * Get all collections with indexing status
3017
+ */
3018
+ async getAllCollections() {
3019
+ try {
3020
+ const collectionsStmt = this.db.prepare(
3021
+ "SELECT id, name, display_name, description FROM collections WHERE is_active = 1 ORDER BY display_name"
3022
+ );
3023
+ const { results: allCollections } = await collectionsStmt.all();
3024
+ console.log("[AISearchService.getAllCollections] Raw collections from DB:", allCollections?.length || 0);
3025
+ const firstCollection = allCollections?.[0];
3026
+ if (firstCollection) {
3027
+ console.log("[AISearchService.getAllCollections] Sample collection:", {
3028
+ id: firstCollection.id,
3029
+ name: firstCollection.name,
3030
+ display_name: firstCollection.display_name
3031
+ });
3032
+ }
3033
+ const collections2 = (allCollections || []).filter(
3034
+ (col) => col.id && col.name
3035
+ );
3036
+ console.log("[AISearchService.getAllCollections] After filtering test collections:", collections2.length);
3037
+ console.log("[AISearchService.getAllCollections] Remaining collections:", collections2.map((c) => c.name).join(", "));
3038
+ const settings = await this.getSettings();
3039
+ const selected = settings?.selected_collections || [];
3040
+ const dismissed = settings?.dismissed_collections || [];
3041
+ console.log("[AISearchService.getAllCollections] Settings:", {
3042
+ selected_count: selected.length,
3043
+ dismissed_count: dismissed.length,
3044
+ selected
3045
+ });
3046
+ const collectionInfos = [];
3047
+ for (const collection of collections2) {
3048
+ if (!collection.id || !collection.name) continue;
3049
+ const collectionId = String(collection.id);
3050
+ if (!collectionId) {
3051
+ console.warn("[AISearchService] Skipping invalid collection:", collection);
3052
+ continue;
3053
+ }
3054
+ const countStmt = this.db.prepare(
3055
+ "SELECT COUNT(*) as count FROM content WHERE collection_id = ?"
3056
+ );
3057
+ const countResult = await countStmt.bind(collectionId).first();
3058
+ const itemCount = countResult?.count || 0;
3059
+ collectionInfos.push({
3060
+ id: collectionId,
3061
+ name: collection.name,
3062
+ display_name: collection.display_name || collection.name,
3063
+ description: collection.description,
3064
+ item_count: itemCount,
3065
+ is_indexed: selected.includes(collectionId),
3066
+ is_dismissed: dismissed.includes(collectionId),
3067
+ is_new: !selected.includes(collectionId) && !dismissed.includes(collectionId)
3068
+ });
3069
+ }
3070
+ console.log("[AISearchService.getAllCollections] Returning collectionInfos:", collectionInfos.length);
3071
+ const firstInfo = collectionInfos[0];
3072
+ if (collectionInfos.length > 0 && firstInfo) {
3073
+ console.log("[AISearchService.getAllCollections] First collectionInfo:", {
3074
+ id: firstInfo.id,
3075
+ name: firstInfo.name,
3076
+ display_name: firstInfo.display_name,
3077
+ item_count: firstInfo.item_count
3078
+ });
3079
+ }
3080
+ return collectionInfos;
3081
+ } catch (error) {
3082
+ console.error("[AISearchService] Error fetching collections:", error);
3083
+ return [];
3084
+ }
3085
+ }
3086
+ /**
3087
+ * Execute search query
3088
+ */
3089
+ async search(query) {
3090
+ const settings = await this.getSettings();
3091
+ if (!settings?.enabled) {
3092
+ return {
3093
+ results: [],
3094
+ total: 0,
3095
+ query_time_ms: 0,
3096
+ mode: query.mode
3097
+ };
3098
+ }
3099
+ if (query.mode === "ai" && settings.ai_mode_enabled && this.customRAG?.isAvailable()) {
3100
+ return this.searchAI(query, settings);
3101
+ }
3102
+ return this.searchKeyword(query, settings);
3103
+ }
3104
+ /**
3105
+ * AI-powered semantic search using Custom RAG
3106
+ */
3107
+ async searchAI(query, settings) {
3108
+ try {
3109
+ if (!this.customRAG) {
3110
+ console.warn("[AISearchService] CustomRAG not available, falling back to keyword search");
3111
+ return this.searchKeyword(query, settings);
3112
+ }
3113
+ const result = await this.customRAG.search(query, settings);
3114
+ return result;
3115
+ } catch (error) {
3116
+ console.error("[AISearchService] AI search error, falling back to keyword:", error);
3117
+ return this.searchKeyword(query, settings);
3118
+ }
3119
+ }
3120
+ /**
3121
+ * Traditional keyword search
3122
+ */
3123
+ async searchKeyword(query, settings) {
3124
+ const startTime = Date.now();
3125
+ try {
3126
+ const conditions = [];
3127
+ const params = [];
3128
+ if (query.query) {
3129
+ conditions.push("(c.title LIKE ? OR c.slug LIKE ? OR c.data LIKE ?)");
3130
+ const searchTerm = `%${query.query}%`;
3131
+ params.push(searchTerm, searchTerm, searchTerm);
3132
+ }
3133
+ if (query.filters?.collections && query.filters.collections.length > 0) {
3134
+ const placeholders = query.filters.collections.map(() => "?").join(",");
3135
+ conditions.push(`c.collection_id IN (${placeholders})`);
3136
+ params.push(...query.filters.collections);
3137
+ } else if (settings.selected_collections.length > 0) {
3138
+ const placeholders = settings.selected_collections.map(() => "?").join(",");
3139
+ conditions.push(`c.collection_id IN (${placeholders})`);
3140
+ params.push(...settings.selected_collections);
3141
+ }
3142
+ if (query.filters?.status && query.filters.status.length > 0) {
3143
+ const placeholders = query.filters.status.map(() => "?").join(",");
3144
+ conditions.push(`c.status IN (${placeholders})`);
3145
+ params.push(...query.filters.status);
3146
+ } else {
3147
+ conditions.push("c.status != 'deleted'");
3148
+ }
3149
+ if (query.filters?.dateRange) {
3150
+ const field = query.filters.dateRange.field || "created_at";
3151
+ if (query.filters.dateRange.start) {
3152
+ conditions.push(`c.${field} >= ?`);
3153
+ params.push(query.filters.dateRange.start.getTime());
3154
+ }
3155
+ if (query.filters.dateRange.end) {
3156
+ conditions.push(`c.${field} <= ?`);
3157
+ params.push(query.filters.dateRange.end.getTime());
3158
+ }
3159
+ }
3160
+ if (query.filters?.author) {
3161
+ conditions.push("c.author_id = ?");
3162
+ params.push(query.filters.author);
3163
+ }
3164
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
3165
+ const countStmt = this.db.prepare(`
3166
+ SELECT COUNT(*) as count
3167
+ FROM content c
3168
+ ${whereClause}
3169
+ `);
3170
+ const countResult = await countStmt.bind(...params).first();
3171
+ const total = countResult?.count || 0;
3172
+ const limit = query.limit || settings.results_limit;
3173
+ const offset = query.offset || 0;
3174
+ const resultsStmt = this.db.prepare(`
3175
+ SELECT
3176
+ c.id, c.title, c.slug, c.collection_id, c.status,
3177
+ c.created_at, c.updated_at, c.author_id, c.data,
3178
+ col.name as collection_name, col.display_name as collection_display_name,
3179
+ u.email as author_email
3180
+ FROM content c
3181
+ JOIN collections col ON c.collection_id = col.id
3182
+ LEFT JOIN users u ON c.author_id = u.id
3183
+ ${whereClause}
3184
+ ORDER BY c.updated_at DESC
3185
+ LIMIT ? OFFSET ?
3186
+ `);
3187
+ const { results } = await resultsStmt.bind(...params, limit, offset).all();
3188
+ const searchResults = (results || []).map((row) => ({
3189
+ id: String(row.id),
3190
+ title: row.title || "Untitled",
3191
+ slug: row.slug || "",
3192
+ collection_id: String(row.collection_id),
3193
+ collection_name: row.collection_display_name || row.collection_name,
3194
+ snippet: this.extractSnippet(row.data, query.query),
3195
+ status: row.status,
3196
+ created_at: Number(row.created_at),
3197
+ updated_at: Number(row.updated_at),
3198
+ author_name: row.author_email
3199
+ }));
3200
+ const queryTime = Date.now() - startTime;
3201
+ await this.logSearch(query.query, query.mode, searchResults.length);
3202
+ return {
3203
+ results: searchResults,
3204
+ total,
3205
+ query_time_ms: queryTime,
3206
+ mode: query.mode
3207
+ };
3208
+ } catch (error) {
3209
+ console.error("Keyword search error:", error);
3210
+ return {
3211
+ results: [],
3212
+ total: 0,
3213
+ query_time_ms: Date.now() - startTime,
3214
+ mode: query.mode
3215
+ };
3216
+ }
3217
+ }
3218
+ /**
3219
+ * Extract snippet from content data
3220
+ */
3221
+ extractSnippet(data, query) {
3222
+ try {
3223
+ const parsed = typeof data === "string" ? JSON.parse(data) : data;
3224
+ const text = JSON.stringify(parsed).toLowerCase();
3225
+ const queryLower = query.toLowerCase();
3226
+ const index = text.indexOf(queryLower);
3227
+ if (index === -1) {
3228
+ return JSON.stringify(parsed).substring(0, 200) + "...";
3229
+ }
3230
+ const start = Math.max(0, index - 50);
3231
+ const end = Math.min(text.length, index + query.length + 50);
3232
+ return text.substring(start, end) + "...";
3233
+ } catch {
3234
+ return data.substring(0, 200) + "...";
3235
+ }
3236
+ }
3237
+ /**
3238
+ * Get search suggestions (autocomplete)
3239
+ */
3240
+ async getSearchSuggestions(partial) {
3241
+ try {
3242
+ const settings = await this.getSettings();
3243
+ if (!settings?.autocomplete_enabled) {
3244
+ return [];
3245
+ }
3246
+ if (this.customRAG?.isAvailable()) {
3247
+ try {
3248
+ const aiSuggestions = await this.customRAG.getSuggestions(partial, 5);
3249
+ if (aiSuggestions.length > 0) {
3250
+ return aiSuggestions;
3251
+ }
3252
+ } catch (error) {
3253
+ console.error("[AISearchService] Error getting AI suggestions:", error);
3254
+ }
3255
+ }
3256
+ const stmt = this.db.prepare(`
3257
+ SELECT DISTINCT query
3258
+ FROM ai_search_history
3259
+ WHERE query LIKE ?
3260
+ ORDER BY created_at DESC
3261
+ LIMIT 10
3262
+ `);
3263
+ const { results } = await stmt.bind(`%${partial}%`).all();
3264
+ return (results || []).map((r) => r.query);
3265
+ } catch (error) {
3266
+ console.error("Error getting suggestions:", error);
3267
+ return [];
3268
+ }
3269
+ }
3270
+ /**
3271
+ * Log search query to history
3272
+ */
3273
+ async logSearch(query, mode, resultsCount) {
3274
+ try {
3275
+ const stmt = this.db.prepare(`
3276
+ INSERT INTO ai_search_history (query, mode, results_count, created_at)
3277
+ VALUES (?, ?, ?, ?)
3278
+ `);
3279
+ await stmt.bind(query, mode, resultsCount, Date.now()).run();
3280
+ } catch (error) {
3281
+ console.error("Error logging search:", error);
3282
+ }
3283
+ }
3284
+ /**
3285
+ * Get search analytics
3286
+ */
3287
+ async getSearchAnalytics() {
3288
+ try {
3289
+ const totalStmt = this.db.prepare(`
3290
+ SELECT COUNT(*) as count
3291
+ FROM ai_search_history
3292
+ WHERE created_at >= ?
3293
+ `);
3294
+ const thirtyDaysAgo = Date.now() - 30 * 24 * 60 * 60 * 1e3;
3295
+ const totalResult = await totalStmt.bind(thirtyDaysAgo).first();
3296
+ const modeStmt = this.db.prepare(`
3297
+ SELECT mode, COUNT(*) as count
3298
+ FROM ai_search_history
3299
+ WHERE created_at >= ?
3300
+ GROUP BY mode
3301
+ `);
3302
+ const { results: modeResults } = await modeStmt.bind(thirtyDaysAgo).all();
3303
+ const aiCount = modeResults?.find((r) => r.mode === "ai")?.count || 0;
3304
+ const keywordCount = modeResults?.find((r) => r.mode === "keyword")?.count || 0;
3305
+ const popularStmt = this.db.prepare(`
3306
+ SELECT query, COUNT(*) as count
3307
+ FROM ai_search_history
3308
+ WHERE created_at >= ?
3309
+ GROUP BY query
3310
+ ORDER BY count DESC
3311
+ LIMIT 10
3312
+ `);
3313
+ const { results: popularResults } = await popularStmt.bind(thirtyDaysAgo).all();
3314
+ return {
3315
+ total_queries: totalResult?.count || 0,
3316
+ ai_queries: aiCount,
3317
+ keyword_queries: keywordCount,
3318
+ popular_queries: (popularResults || []).map((r) => ({
3319
+ query: r.query,
3320
+ count: r.count
3321
+ })),
3322
+ average_query_time: 0
3323
+ // TODO: Track query times
3324
+ };
3325
+ } catch (error) {
3326
+ console.error("Error getting analytics:", error);
3327
+ return {
3328
+ total_queries: 0,
3329
+ ai_queries: 0,
3330
+ keyword_queries: 0,
3331
+ popular_queries: [],
3332
+ average_query_time: 0
3333
+ };
3334
+ }
3335
+ }
3336
+ /**
3337
+ * Verify Custom RAG is available
3338
+ */
3339
+ verifyBinding() {
3340
+ return this.customRAG?.isAvailable() ?? false;
3341
+ }
3342
+ /**
3343
+ * Get Custom RAG service instance (for indexer)
3344
+ */
3345
+ getCustomRAG() {
3346
+ return this.customRAG;
3347
+ }
3348
+ };
3349
+
3350
+ // src/plugins/core-plugins/ai-search-plugin/services/indexer.ts
3351
+ var IndexManager = class {
3352
+ constructor(db, ai, vectorize) {
3353
+ this.db = db;
3354
+ this.ai = ai;
3355
+ this.vectorize = vectorize;
3356
+ if (this.ai && this.vectorize) {
3357
+ this.customRAG = new CustomRAGService(db, ai, vectorize);
3358
+ console.log("[IndexManager] Custom RAG initialized");
3359
+ }
3360
+ }
3361
+ customRAG;
3362
+ /**
3363
+ * Index all content items within a collection using Custom RAG
3364
+ */
3365
+ async indexCollection(collectionId) {
3366
+ try {
3367
+ const collectionStmt = this.db.prepare(
3368
+ "SELECT id, name, display_name FROM collections WHERE id = ?"
3369
+ );
3370
+ const collection = await collectionStmt.bind(collectionId).first();
3371
+ if (!collection) {
3372
+ throw new Error(`Collection ${collectionId} not found`);
3373
+ }
3374
+ await this.updateIndexStatus(collectionId, {
3375
+ collection_id: collectionId,
3376
+ collection_name: collection.display_name,
3377
+ total_items: 0,
3378
+ indexed_items: 0,
3379
+ status: "indexing"
3380
+ });
3381
+ if (this.customRAG?.isAvailable()) {
3382
+ console.log(`[IndexManager] Using Custom RAG to index collection ${collectionId}`);
3383
+ const result = await this.customRAG.indexCollection(collectionId);
3384
+ const finalStatus = {
3385
+ collection_id: collectionId,
3386
+ collection_name: collection.display_name,
3387
+ total_items: result.total_items,
3388
+ indexed_items: result.indexed_chunks,
3389
+ last_sync_at: Date.now(),
3390
+ status: result.errors > 0 ? "error" : "completed",
3391
+ error_message: result.errors > 0 ? `${result.errors} errors during indexing` : void 0
3392
+ };
3393
+ await this.updateIndexStatus(collectionId, finalStatus);
3394
+ return finalStatus;
3395
+ }
3396
+ console.warn(`[IndexManager] Custom RAG not available, skipping indexing for ${collectionId}`);
3397
+ const fallbackStatus = {
3398
+ collection_id: collectionId,
3399
+ collection_name: collection.display_name,
3400
+ total_items: 0,
3401
+ indexed_items: 0,
3402
+ last_sync_at: Date.now(),
3403
+ status: "completed",
3404
+ error_message: "Custom RAG not available - using keyword search only"
3405
+ };
3406
+ await this.updateIndexStatus(collectionId, fallbackStatus);
3407
+ return fallbackStatus;
3408
+ } catch (error) {
3409
+ console.error(`[IndexManager] Error indexing collection ${collectionId}:`, error);
3410
+ const errorStatus = {
3411
+ collection_id: collectionId,
3412
+ collection_name: "Unknown",
3413
+ total_items: 0,
3414
+ indexed_items: 0,
3415
+ status: "error",
3416
+ error_message: error instanceof Error ? error.message : String(error)
3417
+ };
3418
+ await this.updateIndexStatus(collectionId, errorStatus);
3419
+ return errorStatus;
3420
+ }
3421
+ }
3422
+ /**
3423
+ * Index a single content item
3424
+ */
3425
+ async indexContentItem(item, collectionId) {
3426
+ try {
3427
+ let parsedData = {};
3428
+ try {
3429
+ parsedData = typeof item.data === "string" ? JSON.parse(item.data) : item.data;
3430
+ } catch {
3431
+ parsedData = {};
3432
+ }
3433
+ const document = {
3434
+ id: `content_${item.id}`,
3435
+ title: item.title || "Untitled",
3436
+ slug: item.slug || "",
3437
+ content: this.extractSearchableText(parsedData),
3438
+ metadata: {
3439
+ collection_id: collectionId,
3440
+ collection_name: item.collection_name,
3441
+ collection_display_name: item.collection_display_name,
3442
+ status: item.status,
3443
+ created_at: item.created_at,
3444
+ updated_at: item.updated_at,
3445
+ author_id: item.author_id
3446
+ }
3447
+ };
3448
+ console.log(`Indexed content item: ${item.id}`);
3449
+ } catch (error) {
3450
+ console.error(`Error indexing content item ${item.id}:`, error);
3451
+ throw error;
3452
+ }
3453
+ }
3454
+ /**
3455
+ * Extract searchable text from content data
3456
+ */
3457
+ extractSearchableText(data) {
3458
+ const parts = [];
3459
+ if (data.title) parts.push(String(data.title));
3460
+ if (data.name) parts.push(String(data.name));
3461
+ if (data.description) parts.push(String(data.description));
3462
+ if (data.content) parts.push(String(data.content));
3463
+ if (data.body) parts.push(String(data.body));
3464
+ if (data.text) parts.push(String(data.text));
3465
+ const extractStrings = (obj) => {
3466
+ if (typeof obj === "string") {
3467
+ parts.push(obj);
3468
+ } else if (Array.isArray(obj)) {
3469
+ obj.forEach(extractStrings);
3470
+ } else if (obj && typeof obj === "object") {
3471
+ Object.values(obj).forEach(extractStrings);
3472
+ }
3473
+ };
3474
+ extractStrings(data);
3475
+ return parts.join(" ");
3476
+ }
3477
+ /**
3478
+ * Update a single content item in the index
3479
+ */
3480
+ async updateIndex(collectionId, contentId) {
3481
+ try {
3482
+ const stmt = this.db.prepare(`
3483
+ SELECT
3484
+ c.id, c.title, c.slug, c.data, c.status,
3485
+ c.created_at, c.updated_at, c.author_id,
3486
+ col.name as collection_name, col.display_name as collection_display_name
3487
+ FROM content c
3488
+ JOIN collections col ON c.collection_id = col.id
3489
+ WHERE c.id = ? AND c.collection_id = ?
3490
+ `);
3491
+ const item = await stmt.bind(contentId, collectionId).first();
3492
+ if (!item) {
3493
+ throw new Error(`Content item ${contentId} not found`);
3494
+ }
3495
+ await this.indexContentItem(item, String(collectionId));
3496
+ const status = await this.getIndexStatus(String(collectionId));
3497
+ if (status) {
3498
+ await this.updateIndexStatus(String(collectionId), {
3499
+ ...status,
3500
+ last_sync_at: Date.now()
3501
+ });
3502
+ }
3503
+ } catch (error) {
3504
+ console.error(`Error updating index for content ${contentId}:`, error);
3505
+ throw error;
3506
+ }
3507
+ }
3508
+ /**
3509
+ * Remove a content item from the index using Custom RAG
3510
+ */
3511
+ async removeFromIndex(collectionId, contentId) {
3512
+ try {
3513
+ if (this.customRAG?.isAvailable()) {
3514
+ console.log(`[IndexManager] Removing content ${contentId} from index`);
3515
+ await this.customRAG.removeContentFromIndex(contentId);
3516
+ } else {
3517
+ console.warn(`[IndexManager] Custom RAG not available, skipping removal for ${contentId}`);
3518
+ }
3519
+ } catch (error) {
3520
+ console.error(`[IndexManager] Error removing content ${contentId} from index:`, error);
3521
+ throw error;
3522
+ }
3523
+ }
3524
+ /**
3525
+ * Get indexing status for a collection
3526
+ */
3527
+ async getIndexStatus(collectionId) {
3528
+ try {
3529
+ const stmt = this.db.prepare(
3530
+ "SELECT * FROM ai_search_index_meta WHERE collection_id = ?"
3531
+ );
3532
+ const result = await stmt.bind(collectionId).first();
3533
+ if (!result) {
3534
+ return null;
3535
+ }
3536
+ return {
3537
+ collection_id: String(result.collection_id),
3538
+ collection_name: result.collection_name,
3539
+ total_items: result.total_items,
3540
+ indexed_items: result.indexed_items,
3541
+ last_sync_at: result.last_sync_at,
3542
+ status: result.status,
3543
+ error_message: result.error_message
3544
+ };
3545
+ } catch (error) {
3546
+ console.error(`Error getting index status for collection ${collectionId}:`, error);
3547
+ return null;
3548
+ }
3549
+ }
3550
+ /**
3551
+ * Get indexing status for all collections
3552
+ */
3553
+ async getAllIndexStatus() {
3554
+ try {
3555
+ const stmt = this.db.prepare("SELECT * FROM ai_search_index_meta");
3556
+ const { results } = await stmt.all();
3557
+ const statusMap = {};
3558
+ for (const row of results || []) {
3559
+ const collectionId = String(row.collection_id);
3560
+ statusMap[collectionId] = {
3561
+ collection_id: collectionId,
3562
+ collection_name: row.collection_name,
3563
+ total_items: row.total_items,
3564
+ indexed_items: row.indexed_items,
3565
+ last_sync_at: row.last_sync_at,
3566
+ status: row.status,
3567
+ error_message: row.error_message
3568
+ };
3569
+ }
3570
+ return statusMap;
3571
+ } catch (error) {
3572
+ console.error("Error getting all index status:", error);
3573
+ return {};
3574
+ }
3575
+ }
3576
+ /**
3577
+ * Update index status in database
3578
+ */
3579
+ async updateIndexStatus(collectionId, status) {
3580
+ try {
3581
+ const checkStmt = this.db.prepare(
3582
+ "SELECT id FROM ai_search_index_meta WHERE collection_id = ?"
3583
+ );
3584
+ const existing = await checkStmt.bind(collectionId).first();
3585
+ if (existing) {
3586
+ const stmt = this.db.prepare(`
3587
+ UPDATE ai_search_index_meta
3588
+ SET collection_name = ?,
3589
+ total_items = ?,
3590
+ indexed_items = ?,
3591
+ last_sync_at = ?,
3592
+ status = ?,
3593
+ error_message = ?
3594
+ WHERE collection_id = ?
3595
+ `);
3596
+ await stmt.bind(
3597
+ status.collection_name,
3598
+ status.total_items,
3599
+ status.indexed_items,
3600
+ status.last_sync_at || null,
3601
+ status.status,
3602
+ status.error_message || null,
3603
+ String(collectionId)
3604
+ ).run();
3605
+ } else {
3606
+ const stmt = this.db.prepare(`
3607
+ INSERT INTO ai_search_index_meta (
3608
+ collection_id, collection_name, total_items, indexed_items,
3609
+ last_sync_at, status, error_message
3610
+ ) VALUES (?, ?, ?, ?, ?, ?, ?)
3611
+ `);
3612
+ await stmt.bind(
3613
+ String(status.collection_id),
3614
+ status.collection_name,
3615
+ status.total_items,
3616
+ status.indexed_items,
3617
+ status.last_sync_at || null,
3618
+ status.status,
3619
+ status.error_message || null
3620
+ ).run();
3621
+ }
3622
+ } catch (error) {
3623
+ console.error(`Error updating index status for collection ${collectionId}:`, error);
3624
+ throw error;
3625
+ }
3626
+ }
3627
+ /**
3628
+ * Sync all selected collections
3629
+ */
3630
+ async syncAll(selectedCollections) {
3631
+ for (const collectionId of selectedCollections) {
3632
+ try {
3633
+ await this.indexCollection(collectionId);
3634
+ } catch (error) {
3635
+ console.error(`Error syncing collection ${collectionId}:`, error);
3636
+ }
3637
+ }
3638
+ }
3639
+ };
3640
+
3641
+ // src/plugins/core-plugins/ai-search-plugin/components/settings-page.ts
3642
+ function renderSettingsPage(data) {
3643
+ const settings = data.settings || {
3644
+ enabled: false,
3645
+ ai_mode_enabled: true,
3646
+ selected_collections: [],
3647
+ dismissed_collections: [],
3648
+ autocomplete_enabled: true,
3649
+ cache_duration: 1,
3650
+ results_limit: 20,
3651
+ index_media: false
3652
+ };
3653
+ const selectedCollections = Array.isArray(settings.selected_collections) ? settings.selected_collections : [];
3654
+ const dismissedCollections = Array.isArray(settings.dismissed_collections) ? settings.dismissed_collections : [];
3655
+ const enabled = settings.enabled === true;
3656
+ const aiModeEnabled = settings.ai_mode_enabled !== false;
3657
+ const autocompleteEnabled = settings.autocomplete_enabled !== false;
3658
+ const indexMedia = settings.index_media === true;
3659
+ const selectedCollectionIds = new Set(selectedCollections.map((id) => String(id)));
3660
+ const dismissedCollectionIds = new Set(dismissedCollections.map((id) => String(id)));
3661
+ const collections2 = Array.isArray(data.collections) ? data.collections : [];
3662
+ console.log("[SettingsPage Template] Collections received:", collections2.length);
3663
+ if (collections2.length > 0) {
3664
+ console.log("[SettingsPage Template] First collection:", collections2[0]);
3665
+ }
3666
+ const content2 = `
3667
+ <div class="w-full px-4 sm:px-6 lg:px-8 py-6">
3668
+ <!-- Header with Back Button -->
3669
+ <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-6">
3670
+ <div>
3671
+ <h1 class="text-2xl/8 font-semibold text-zinc-950 dark:text-white sm:text-xl/8">\u{1F50D} AI Search Settings</h1>
3672
+ <p class="mt-2 text-sm/6 text-zinc-500 dark:text-zinc-400">
3673
+ Configure advanced search with Cloudflare AI Search. Select collections to index and manage search preferences.
3674
+ </p>
3675
+ </div>
3676
+ <div class="mt-4 sm:mt-0 sm:ml-16 sm:flex-none">
3677
+ <a href="/admin/plugins" class="inline-flex items-center justify-center rounded-lg bg-white dark:bg-zinc-800 px-3.5 py-2.5 text-sm font-semibold text-zinc-950 dark:text-white ring-1 ring-inset ring-zinc-950/10 dark:ring-white/10 hover:bg-zinc-50 dark:hover:bg-zinc-700 transition-colors shadow-sm">
3678
+ <svg class="-ml-0.5 mr-1.5 h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
3679
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
3680
+ </svg>
3681
+ Back to Plugins
3682
+ </a>
3683
+ </div>
3684
+ </div>
3685
+
3686
+
3687
+ <!-- Main Settings Card -->
3688
+ <div class="rounded-xl bg-white dark:bg-zinc-900 shadow-sm ring-1 ring-zinc-950/5 dark:ring-white/10 p-6 mb-6">
3689
+ <form id="settingsForm" class="space-y-6">
3690
+ <!-- Enable Search Section -->
3691
+ <div>
3692
+ <h2 class="text-xl font-semibold text-zinc-950 dark:text-white mb-4">\u{1F50D} Search Settings</h2>
3693
+ <div class="space-y-3">
3694
+ <div class="flex items-center gap-3 p-4 border border-indigo-200 bg-indigo-50 dark:bg-indigo-900/20 rounded-lg">
3695
+ <input type="checkbox" id="enabled" name="enabled" ${enabled ? "checked" : ""} class="w-5 h-5 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500 cursor-pointer">
3696
+ <div class="flex-1">
3697
+ <label for="enabled" class="text-base font-semibold text-zinc-900 dark:text-white select-none cursor-pointer block">Enable AI Search</label>
3698
+ <p class="text-xs text-zinc-600 dark:text-zinc-400 mt-0.5">Turn on advanced search capabilities across your content</p>
3699
+ </div>
3700
+ </div>
3701
+
3702
+ <div class="flex items-center gap-3 p-4 border border-blue-200 bg-blue-50 dark:bg-blue-900/20 rounded-lg">
3703
+ <input type="checkbox" id="ai_mode_enabled" name="ai_mode_enabled" ${aiModeEnabled ? "checked" : ""} class="w-5 h-5 rounded border-gray-300 text-blue-600 focus:ring-blue-500 cursor-pointer">
3704
+ <div class="flex-1">
3705
+ <label for="ai_mode_enabled" class="text-base font-semibold text-zinc-900 dark:text-white select-none cursor-pointer block">\u{1F916} AI/Semantic Search</label>
3706
+ <p class="text-xs text-zinc-600 dark:text-zinc-400 mt-0.5">
3707
+ Enable natural language queries (requires Cloudflare Workers AI binding)
3708
+ <a href="https://developers.cloudflare.com/workers-ai/" target="_blank" class="text-blue-600 dark:text-blue-400 hover:underline ml-1">\u2192 Setup Guide</a>
3709
+ </p>
3710
+ <p class="text-xs text-amber-600 dark:text-amber-400 mt-1">
3711
+ \u26A0\uFE0F If AI binding unavailable, will fallback to keyword search
3712
+ </p>
3713
+ </div>
3714
+ </div>
3715
+ </div>
3716
+ </div>
3717
+
3718
+ <hr class="border-zinc-200 dark:border-zinc-800">
3719
+
3720
+ <!-- Collections Section -->
3721
+ <div>
3722
+ <div class="flex items-start justify-between mb-4">
3723
+ <div>
3724
+ <h2 class="text-xl font-semibold text-zinc-950 dark:text-white">\u{1F4DA} Collections to Index</h2>
3725
+ <p class="text-sm text-zinc-600 dark:text-zinc-400 mt-1">
3726
+ Select which content collections should be indexed and searchable. Only checked collections will be included in search results.
3727
+ </p>
3728
+ </div>
3729
+ </div>
3730
+ <div class="space-y-3 max-h-96 overflow-y-auto border-2 border-zinc-300 dark:border-zinc-700 rounded-lg p-4 bg-white dark:bg-zinc-800" id="collections-list">
3731
+ ${collections2.length === 0 ? '<p class="text-sm text-zinc-500 dark:text-zinc-400 p-4">No collections available. Create collections first.</p>' : collections2.map((collection) => {
3732
+ const collectionId = String(collection.id);
3733
+ const isChecked = selectedCollectionIds.has(collectionId);
3734
+ const isDismissed = dismissedCollectionIds.has(collectionId);
3735
+ const indexStatusMap = data.indexStatus || {};
3736
+ const status = indexStatusMap[collectionId];
3737
+ const isNew = collection.is_new === true && !isDismissed && !status;
3738
+ const statusBadge = status && isChecked ? `<span class="ml-2 px-2 py-1 text-xs rounded-full ${status.status === "completed" ? "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300" : status.status === "indexing" ? "bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300" : status.status === "error" ? "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300" : "bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-300"}">${status.status}</span>` : "";
3739
+ return `<div class="flex items-start gap-3 p-3 rounded-lg border border-zinc-200 dark:border-zinc-700 ${isNew ? "bg-blue-50 dark:bg-blue-900/10 border-blue-200 dark:border-blue-800" : "hover:bg-zinc-50 dark:hover:bg-zinc-800"}">
3740
+ <input
3741
+ type="checkbox"
3742
+ id="collection_${collectionId}"
3743
+ name="selected_collections"
3744
+ value="${collectionId}"
3745
+ ${isChecked ? "checked" : ""}
3746
+ class="mt-1 w-5 h-5 text-indigo-600 bg-white border-gray-300 rounded focus:ring-indigo-500 focus:ring-2 cursor-pointer"
3747
+ style="cursor: pointer; flex-shrink: 0;"
3748
+ />
3749
+ <div class="flex-1 min-w-0">
3750
+ <label for="collection_${collectionId}" class="text-sm font-medium text-zinc-950 dark:text-white select-none cursor-pointer flex items-center">
3751
+ ${collection.display_name || collection.name || "Unnamed Collection"}
3752
+ ${isNew ? '<span class="ml-2 px-2 py-0.5 text-xs rounded-full bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300">NEW</span>' : ""}
3753
+ ${statusBadge}
3754
+ </label>
3755
+ <p class="text-xs text-zinc-500 dark:text-zinc-400 mt-1">
3756
+ ${collection.description || collection.name || "No description"} \u2022 ${collection.item_count || 0} items
3757
+ ${status ? ` \u2022 ${status.indexed_items}/${status.total_items} indexed` : ""}
3758
+ </p>
3759
+ ${status && status.status === "indexing" ? `<div class="mt-2 w-full bg-gray-200 rounded-full h-2 dark:bg-gray-700">
3760
+ <div class="bg-blue-600 h-2 rounded-full" style="width: ${status.indexed_items / status.total_items * 100}%"></div>
3761
+ </div>` : ""}
3762
+ </div>
3763
+ ${isChecked ? `
3764
+ <button
3765
+ type="button"
3766
+ onclick="reindexCollection('${collectionId}')"
3767
+ class="px-3 py-1.5 text-xs font-medium text-white bg-indigo-600 hover:bg-indigo-700 rounded-md transition-colors flex items-center gap-1.5 whitespace-nowrap"
3768
+ ${status && status.status === "indexing" ? "disabled" : ""}
3769
+ >
3770
+ <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
3771
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
3772
+ </svg>
3773
+ Re-index
3774
+ </button>
3775
+ ` : ""}
3776
+ </div>`;
3777
+ }).join("")}
3778
+ </div>
3779
+ </div>
3780
+
3781
+ <hr class="border-zinc-200 dark:border-zinc-800">
3782
+
3783
+ <!-- Advanced Options -->
3784
+ <div>
3785
+ <h2 class="text-xl font-semibold text-zinc-950 dark:text-white mb-4">\u2699\uFE0F Advanced Options</h2>
3786
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
3787
+ <div class="flex items-start gap-3 p-3 border border-zinc-200 dark:border-zinc-700 rounded-lg">
3788
+ <input type="checkbox" id="autocomplete_enabled" name="autocomplete_enabled" ${autocompleteEnabled ? "checked" : ""} class="mt-0.5 w-5 h-5 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500 cursor-pointer">
3789
+ <div>
3790
+ <label for="autocomplete_enabled" class="text-sm font-medium text-zinc-950 dark:text-white select-none cursor-pointer block">Autocomplete Suggestions</label>
3791
+ <p class="text-xs text-zinc-500 dark:text-zinc-400 mt-0.5">Show search suggestions as users type</p>
3792
+ </div>
3793
+ </div>
3794
+
3795
+ <div class="flex items-start gap-3 p-3 border border-zinc-200 dark:border-zinc-700 rounded-lg">
3796
+ <input type="checkbox" id="index_media" name="index_media" ${indexMedia ? "checked" : ""} class="mt-0.5 w-5 h-5 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500 cursor-pointer">
3797
+ <div>
3798
+ <label for="index_media" class="text-sm font-medium text-zinc-950 dark:text-white select-none cursor-pointer block">Index Media Metadata</label>
3799
+ <p class="text-xs text-zinc-500 dark:text-zinc-400 mt-0.5">Include media files in search results</p>
3800
+ </div>
3801
+ </div>
3802
+
3803
+ <div>
3804
+ <label class="block text-sm font-medium text-zinc-950 dark:text-white mb-2">Cache Duration (hours)</label>
3805
+ <input type="number" id="cache_duration" name="cache_duration" value="${settings.cache_duration || 1}" min="0" max="24" class="w-full rounded-lg bg-white dark:bg-white/5 px-3 py-2 text-sm text-zinc-950 dark:text-white ring-1 ring-inset ring-zinc-950/10 dark:ring-white/10 focus:ring-2 focus:ring-indigo-500">
3806
+ </div>
3807
+ <div>
3808
+ <label class="block text-sm font-medium text-zinc-950 dark:text-white mb-2">Results Per Page</label>
3809
+ <input type="number" id="results_limit" name="results_limit" value="${settings.results_limit || 20}" min="10" max="100" class="w-full rounded-lg bg-white dark:bg-white/5 px-3 py-2 text-sm text-zinc-950 dark:text-white ring-1 ring-inset ring-zinc-950/10 dark:ring-white/10 focus:ring-2 focus:ring-indigo-500">
3810
+ </div>
3811
+ </div>
3812
+ </div>
3813
+
3814
+ <!-- Save Button -->
3815
+ <div class="flex items-center justify-between pt-4 border-t border-zinc-200 dark:border-zinc-800">
3816
+ <p class="text-xs text-zinc-500 dark:text-zinc-400">
3817
+ \u{1F4A1} Collections marked as <span class="px-1.5 py-0.5 text-xs font-medium rounded-full bg-blue-500 text-white">NEW</span> haven't been indexed yet
3818
+ </p>
3819
+ <button type="submit" class="inline-flex items-center justify-center rounded-lg bg-indigo-600 text-white px-6 py-2.5 text-sm font-semibold hover:bg-indigo-500 shadow-sm transition-colors">
3820
+ \u{1F4BE} Save Settings
3821
+ </button>
3822
+ </div>
3823
+ </form>
3824
+ </div>
3825
+
3826
+
3827
+ <!-- Search Analytics -->
3828
+ <div class="rounded-xl bg-white dark:bg-zinc-900 shadow-sm ring-1 ring-zinc-950/5 dark:ring-white/10 p-6">
3829
+ <h2 class="text-xl font-semibold text-zinc-950 dark:text-white mb-4">\u{1F4CA} Search Analytics</h2>
3830
+ <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
3831
+ <div class="p-4 rounded-lg bg-zinc-50 dark:bg-zinc-800">
3832
+ <div class="text-sm text-zinc-500 dark:text-zinc-400">Total Queries</div>
3833
+ <div class="text-2xl font-bold text-zinc-950 dark:text-white mt-1">${data.analytics.total_queries}</div>
3834
+ </div>
3835
+ <div class="p-4 rounded-lg bg-zinc-50 dark:bg-zinc-800">
3836
+ <div class="text-sm text-zinc-500 dark:text-zinc-400">AI Queries</div>
3837
+ <div class="text-2xl font-bold text-blue-600 dark:text-blue-400 mt-1">${data.analytics.ai_queries}</div>
3838
+ </div>
3839
+ <div class="p-4 rounded-lg bg-zinc-50 dark:bg-zinc-800">
3840
+ <div class="text-sm text-zinc-500 dark:text-zinc-400">Keyword Queries</div>
3841
+ <div class="text-2xl font-bold text-indigo-600 dark:text-indigo-400 mt-1">${data.analytics.keyword_queries}</div>
3842
+ </div>
3843
+ </div>
3844
+ ${data.analytics.popular_queries.length > 0 ? `
3845
+ <div>
3846
+ <h3 class="text-sm font-semibold text-zinc-950 dark:text-white mb-2">Popular Searches</h3>
3847
+ <div class="space-y-1">
3848
+ ${data.analytics.popular_queries.map(
3849
+ (item) => `
3850
+ <div class="flex items-center justify-between text-sm">
3851
+ <span class="text-zinc-700 dark:text-zinc-300">"${item.query}"</span>
3852
+ <span class="text-zinc-500 dark:text-zinc-400">${item.count} times</span>
3853
+ </div>
3854
+ `
3855
+ ).join("")}
3856
+ </div>
3857
+ </div>
3858
+ ` : '<p class="text-sm text-zinc-500 dark:text-zinc-400">No search history yet.</p>'}
3859
+ </div>
3860
+
3861
+ <!-- Success Message -->
3862
+ <div id="msg" class="hidden fixed bottom-4 right-4 p-4 rounded-lg bg-green-50 text-green-900 border border-green-200 dark:bg-green-900/20 dark:text-green-100 dark:border-green-800 shadow-lg z-50">
3863
+ <div class="flex items-center gap-2">
3864
+ <span class="text-xl">\u2705</span>
3865
+ <span class="font-semibold">Settings Saved Successfully!</span>
3866
+ </div>
3867
+ </div>
3868
+ </div>
3869
+ <script>
3870
+ // Form submission with error handling
3871
+ document.getElementById('settingsForm').addEventListener('submit', async (e) => {
3872
+ e.preventDefault();
3873
+ console.log('[AI Search Client] Form submitted');
3874
+
3875
+ try {
3876
+ const btn = e.submitter;
3877
+ btn.innerText = 'Saving...';
3878
+ btn.disabled = true;
3879
+
3880
+ const formData = new FormData(e.target);
3881
+ const selectedCollections = Array.from(formData.getAll('selected_collections')).map(String);
3882
+
3883
+ const data = {
3884
+ enabled: document.getElementById('enabled').checked,
3885
+ ai_mode_enabled: document.getElementById('ai_mode_enabled').checked,
3886
+ selected_collections: selectedCollections,
3887
+ autocomplete_enabled: document.getElementById('autocomplete_enabled').checked,
3888
+ cache_duration: Number(formData.get('cache_duration')),
3889
+ results_limit: Number(formData.get('results_limit')),
3890
+ index_media: document.getElementById('index_media').checked,
3891
+ };
3892
+
3893
+ console.log('[AI Search Client] Sending data:', data);
3894
+ console.log('[AI Search Client] Selected collections:', selectedCollections);
3895
+
3896
+ const res = await fetch('/admin/plugins/ai-search', {
3897
+ method: 'POST',
3898
+ headers: {'Content-Type': 'application/json'},
3899
+ body: JSON.stringify(data)
3900
+ });
3901
+
3902
+ console.log('[AI Search Client] Response status:', res.status);
3903
+
3904
+ if (res.ok) {
3905
+ const result = await res.json();
3906
+ console.log('[AI Search Client] Save successful:', result);
3907
+ document.getElementById('msg').classList.remove('hidden');
3908
+ setTimeout(() => {
3909
+ document.getElementById('msg').classList.add('hidden');
3910
+ location.reload();
3911
+ }, 2000);
3912
+ } else {
3913
+ const error = await res.text();
3914
+ console.error('[AI Search Client] Save failed:', error);
3915
+ alert('Failed to save settings: ' + error);
3916
+ }
3917
+
3918
+ btn.innerText = 'Save Settings';
3919
+ btn.disabled = false;
3920
+ } catch (error) {
3921
+ console.error('[AI Search Client] Error:', error);
3922
+ alert('Error saving settings: ' + error.message);
3923
+ }
3924
+ });
3925
+
3926
+ // Add collection to index
3927
+ async function addCollectionToIndex(collectionId) {
3928
+ const form = document.getElementById('settingsForm');
3929
+ const checkbox = document.getElementById('collection_' + collectionId);
3930
+ if (checkbox) {
3931
+ checkbox.checked = true;
3932
+ form.dispatchEvent(new Event('submit'));
3933
+ }
3934
+ }
3935
+
3936
+ // Dismiss collection
3937
+ async function dismissCollection(collectionId) {
3938
+ const res = await fetch('/admin/plugins/ai-search', {
3939
+ method: 'POST',
3940
+ headers: {'Content-Type': 'application/json'},
3941
+ body: JSON.stringify({
3942
+ dismissed_collections: [collectionId]
3943
+ })
3944
+ });
3945
+ if (res.ok) {
3946
+ location.reload();
3947
+ }
3948
+ }
3949
+
3950
+ // Re-index collection
3951
+ async function reindexCollection(collectionId) {
3952
+ const res = await fetch('/admin/plugins/ai-search/api/reindex', {
3953
+ method: 'POST',
3954
+ headers: {'Content-Type': 'application/json'},
3955
+ body: JSON.stringify({ collection_id: collectionId })
3956
+ });
3957
+ if (res.ok) {
3958
+ alert('Re-indexing started. Page will refresh in a moment.');
3959
+ setTimeout(() => location.reload(), 2000);
3960
+ } else {
3961
+ alert('Failed to start re-indexing. Please try again.');
3962
+ }
3963
+ }
3964
+
3965
+ // Poll for index status updates
3966
+ setInterval(async () => {
3967
+ const res = await fetch('/admin/plugins/ai-search/api/status');
3968
+ if (res.ok) {
3969
+ const { data } = await res.json();
3970
+ // Update status indicators if needed
3971
+ // For now, just reload every 30 seconds if indexing is in progress
3972
+ const hasIndexing = Object.values(data).some((s) => s.status === 'indexing');
3973
+ if (hasIndexing) {
3974
+ location.reload();
3975
+ }
3976
+ }
3977
+ }, 30000);
3978
+ </script>
3979
+ `;
3980
+ return renderAdminLayout({
3981
+ title: "AI Search Settings",
3982
+ pageTitle: "AI Search Settings",
3983
+ currentPath: "/admin/plugins/ai-search/settings",
3984
+ user: data.user,
3985
+ content: content2
3986
+ });
3987
+ }
3988
+
3989
+ // src/plugins/core-plugins/ai-search-plugin/routes/admin.ts
3990
+ var adminRoutes = new Hono();
3991
+ adminRoutes.use("*", requireAuth());
3992
+ adminRoutes.get("/", async (c) => {
3993
+ try {
3994
+ const user = c.get("user");
3995
+ const db = c.env.DB;
3996
+ const ai = c.env.AI;
3997
+ const vectorize = c.env.VECTORIZE_INDEX;
3998
+ const service = new AISearchService(db, ai, vectorize);
3999
+ const indexer = new IndexManager(db, ai, vectorize);
4000
+ const settings = await service.getSettings();
4001
+ console.log("[AI Search Settings Route] Settings loaded:", !!settings);
4002
+ const collections2 = await service.getAllCollections();
4003
+ console.log("[AI Search Settings Route] Collections returned:", collections2.length);
4004
+ if (collections2.length === 0) {
4005
+ const directQuery = await db.prepare("SELECT id, name, display_name FROM collections WHERE is_active = 1").all();
4006
+ console.log("[AI Search Settings Route] Direct DB query found:", directQuery.results?.length || 0, "collections");
4007
+ if (directQuery.results && directQuery.results.length > 0) {
4008
+ console.log("[AI Search Settings Route] Sample from DB:", directQuery.results[0]);
4009
+ }
4010
+ } else if (collections2.length > 0 && collections2[0]) {
4011
+ console.log("[AI Search Settings Route] First collection:", {
4012
+ id: collections2[0].id,
4013
+ name: collections2[0].name,
4014
+ display_name: collections2[0].display_name
4015
+ });
4016
+ }
4017
+ const newCollections = await service.detectNewCollections();
4018
+ console.log("AI Search: New collections:", newCollections.length);
4019
+ const indexStatus = await indexer.getAllIndexStatus();
4020
+ console.log("AI Search: Index status:", Object.keys(indexStatus).length);
4021
+ const analytics = await service.getSearchAnalytics();
4022
+ return c.html(
4023
+ renderSettingsPage({
4024
+ settings,
4025
+ collections: collections2 || [],
4026
+ newCollections: newCollections || [],
4027
+ indexStatus: indexStatus || {},
4028
+ analytics,
4029
+ user: {
4030
+ name: user.email,
4031
+ email: user.email,
4032
+ role: user.role
4033
+ }
4034
+ })
4035
+ );
4036
+ } catch (error) {
4037
+ console.error("Error rendering AI Search settings:", error);
4038
+ return c.html(`<p>Error loading settings: ${error instanceof Error ? error.message : String(error)}</p>`, 500);
4039
+ }
4040
+ });
4041
+ adminRoutes.post("/", async (c) => {
4042
+ try {
4043
+ const db = c.env.DB;
4044
+ const ai = c.env.AI;
4045
+ const vectorize = c.env.VECTORIZE_INDEX;
4046
+ const service = new AISearchService(db, ai, vectorize);
4047
+ const indexer = new IndexManager(db, ai, vectorize);
4048
+ const body = await c.req.json();
4049
+ console.log("[AI Search POST] Received body:", JSON.stringify(body, null, 2));
4050
+ const currentSettings = await service.getSettings();
4051
+ console.log("[AI Search POST] Current settings selected_collections:", currentSettings?.selected_collections);
4052
+ const updatedSettings = {
4053
+ enabled: body.enabled !== void 0 ? Boolean(body.enabled) : currentSettings?.enabled,
4054
+ ai_mode_enabled: body.ai_mode_enabled !== void 0 ? Boolean(body.ai_mode_enabled) : currentSettings?.ai_mode_enabled,
4055
+ selected_collections: Array.isArray(body.selected_collections) ? body.selected_collections.map(String) : currentSettings?.selected_collections || [],
4056
+ dismissed_collections: Array.isArray(body.dismissed_collections) ? body.dismissed_collections.map(String) : currentSettings?.dismissed_collections || [],
4057
+ autocomplete_enabled: body.autocomplete_enabled !== void 0 ? Boolean(body.autocomplete_enabled) : currentSettings?.autocomplete_enabled,
4058
+ cache_duration: body.cache_duration ? Number(body.cache_duration) : currentSettings?.cache_duration,
4059
+ results_limit: body.results_limit ? Number(body.results_limit) : currentSettings?.results_limit,
4060
+ index_media: body.index_media !== void 0 ? Boolean(body.index_media) : currentSettings?.index_media
4061
+ };
4062
+ console.log("[AI Search POST] Updated settings selected_collections:", updatedSettings.selected_collections);
4063
+ const collectionsChanged = JSON.stringify(updatedSettings.selected_collections) !== JSON.stringify(currentSettings?.selected_collections || []);
4064
+ const saved = await service.updateSettings(updatedSettings);
4065
+ console.log("[AI Search POST] Settings saved, selected_collections:", saved.selected_collections);
4066
+ if (collectionsChanged && updatedSettings.selected_collections) {
4067
+ console.log("[AI Search POST] Collections changed, starting background indexing");
4068
+ c.executionCtx.waitUntil(
4069
+ indexer.syncAll(updatedSettings.selected_collections).then(() => console.log("[AI Search POST] Background indexing completed")).catch((error) => console.error("[AI Search POST] Background indexing error:", error))
4070
+ );
4071
+ }
4072
+ return c.json({ success: true, settings: saved });
4073
+ } catch (error) {
4074
+ console.error("Error updating AI Search settings:", error);
4075
+ return c.json({ error: "Failed to update settings" }, 500);
4076
+ }
4077
+ });
4078
+ adminRoutes.get("/api/settings", async (c) => {
4079
+ try {
4080
+ const db = c.env.DB;
4081
+ const ai = c.env.AI;
4082
+ const vectorize = c.env.VECTORIZE_INDEX;
4083
+ const service = new AISearchService(db, ai, vectorize);
4084
+ const settings = await service.getSettings();
4085
+ return c.json({ success: true, data: settings });
4086
+ } catch (error) {
4087
+ console.error("Error fetching settings:", error);
4088
+ return c.json({ error: "Failed to fetch settings" }, 500);
4089
+ }
4090
+ });
4091
+ adminRoutes.get("/api/new-collections", async (c) => {
4092
+ try {
4093
+ const db = c.env.DB;
4094
+ const ai = c.env.AI;
4095
+ const vectorize = c.env.VECTORIZE_INDEX;
4096
+ const service = new AISearchService(db, ai, vectorize);
4097
+ const notifications = await service.detectNewCollections();
4098
+ return c.json({ success: true, data: notifications });
4099
+ } catch (error) {
4100
+ console.error("Error detecting new collections:", error);
4101
+ return c.json({ error: "Failed to detect new collections" }, 500);
4102
+ }
4103
+ });
4104
+ adminRoutes.get("/api/status", async (c) => {
4105
+ try {
4106
+ const db = c.env.DB;
4107
+ const ai = c.env.AI;
4108
+ const vectorize = c.env.VECTORIZE_INDEX;
4109
+ const indexer = new IndexManager(db, ai, vectorize);
4110
+ const status = await indexer.getAllIndexStatus();
4111
+ return c.json({ success: true, data: status });
4112
+ } catch (error) {
4113
+ console.error("Error fetching index status:", error);
4114
+ return c.json({ error: "Failed to fetch status" }, 500);
4115
+ }
4116
+ });
4117
+ adminRoutes.post("/api/reindex", async (c) => {
4118
+ try {
4119
+ const db = c.env.DB;
4120
+ const ai = c.env.AI;
4121
+ const vectorize = c.env.VECTORIZE_INDEX;
4122
+ const indexer = new IndexManager(db, ai, vectorize);
4123
+ const body = await c.req.json();
4124
+ const collectionIdRaw = body.collection_id;
4125
+ const collectionId = collectionIdRaw ? String(collectionIdRaw) : "";
4126
+ if (!collectionId || collectionId === "undefined" || collectionId === "null") {
4127
+ return c.json({ error: "collection_id is required" }, 400);
4128
+ }
4129
+ c.executionCtx.waitUntil(
4130
+ indexer.indexCollection(collectionId).then(() => console.log(`[AI Search Reindex] Completed for collection ${collectionId}`)).catch((error) => console.error(`[AI Search Reindex] Error for collection ${collectionId}:`, error))
4131
+ );
4132
+ return c.json({ success: true, message: "Re-indexing started" });
4133
+ } catch (error) {
4134
+ console.error("Error starting re-index:", error);
4135
+ return c.json({ error: "Failed to start re-indexing" }, 500);
4136
+ }
4137
+ });
4138
+ var admin_default = adminRoutes;
4139
+ var apiRoutes = new Hono();
4140
+ apiRoutes.post("/", async (c) => {
4141
+ try {
4142
+ const db = c.env.DB;
4143
+ const ai = c.env.AI;
4144
+ const vectorize = c.env.VECTORIZE_INDEX;
4145
+ const service = new AISearchService(db, ai, vectorize);
4146
+ const body = await c.req.json();
4147
+ const query = {
4148
+ query: body.query || "",
4149
+ mode: body.mode || "keyword",
4150
+ filters: body.filters || {},
4151
+ limit: body.limit ? Number(body.limit) : void 0,
4152
+ offset: body.offset ? Number(body.offset) : void 0
4153
+ };
4154
+ if (query.filters?.dateRange) {
4155
+ if (typeof query.filters.dateRange.start === "string") {
4156
+ query.filters.dateRange.start = new Date(query.filters.dateRange.start);
4157
+ }
4158
+ if (typeof query.filters.dateRange.end === "string") {
4159
+ query.filters.dateRange.end = new Date(query.filters.dateRange.end);
4160
+ }
4161
+ }
4162
+ const results = await service.search(query);
4163
+ return c.json({
4164
+ success: true,
4165
+ data: results
4166
+ });
4167
+ } catch (error) {
4168
+ console.error("Search error:", error);
4169
+ return c.json(
4170
+ {
4171
+ success: false,
4172
+ error: "Search failed",
4173
+ message: error instanceof Error ? error.message : String(error)
4174
+ },
4175
+ 500
4176
+ );
4177
+ }
4178
+ });
4179
+ apiRoutes.get("/suggest", async (c) => {
4180
+ try {
4181
+ const db = c.env.DB;
4182
+ const ai = c.env.AI;
4183
+ const vectorize = c.env.VECTORIZE_INDEX;
4184
+ const service = new AISearchService(db, ai, vectorize);
4185
+ const query = c.req.query("q") || "";
4186
+ if (!query || query.length < 2) {
4187
+ return c.json({ success: true, data: [] });
4188
+ }
4189
+ const suggestions = await service.getSearchSuggestions(query);
4190
+ return c.json({
4191
+ success: true,
4192
+ data: suggestions
4193
+ });
4194
+ } catch (error) {
4195
+ console.error("Suggestions error:", error);
4196
+ return c.json(
4197
+ {
4198
+ success: false,
4199
+ error: "Failed to get suggestions"
4200
+ },
4201
+ 500
4202
+ );
4203
+ }
4204
+ });
4205
+ apiRoutes.get("/analytics", async (c) => {
4206
+ try {
4207
+ const db = c.env.DB;
4208
+ const ai = c.env.AI;
4209
+ const vectorize = c.env.VECTORIZE_INDEX;
4210
+ const service = new AISearchService(db, ai, vectorize);
4211
+ const analytics = await service.getSearchAnalytics();
4212
+ return c.json({
4213
+ success: true,
4214
+ data: analytics
4215
+ });
4216
+ } catch (error) {
4217
+ console.error("Analytics error:", error);
4218
+ return c.json(
4219
+ {
4220
+ success: false,
4221
+ error: "Failed to get analytics"
4222
+ },
4223
+ 500
4224
+ );
4225
+ }
4226
+ });
4227
+ var api_default2 = apiRoutes;
4228
+
4229
+ // src/plugins/core-plugins/ai-search-plugin/manifest.json
4230
+ var manifest_default = {
4231
+ name: "AI Search",
4232
+ description: "Advanced search with Cloudflare AI Search. Full-text search, semantic search, and advanced filtering across all content collections.",
4233
+ version: "1.0.0",
4234
+ author: "SonicJS"};
4235
+
4236
+ // src/plugins/core-plugins/ai-search-plugin/index.ts
4237
+ var aiSearchPlugin = new PluginBuilder({
4238
+ name: manifest_default.name,
4239
+ version: manifest_default.version,
4240
+ description: manifest_default.description,
4241
+ author: { name: manifest_default.author }
4242
+ }).metadata({
4243
+ description: manifest_default.description,
4244
+ author: { name: manifest_default.author }
4245
+ }).addService("aiSearch", AISearchService).addService("indexManager", IndexManager).addRoute("/admin/plugins/ai-search", admin_default).addRoute("/api/search", api_default2).build();
2435
4246
  var magicLinkRequestSchema = z.object({
2436
4247
  email: z.string().email("Valid email is required")
2437
4248
  });
@@ -2728,6 +4539,35 @@ function renderMagicLinkEmail(magicLink, expiryMinutes) {
2728
4539
  }
2729
4540
  createMagicLinkAuthPlugin();
2730
4541
 
4542
+ // src/assets/favicon.ts
4543
+ var faviconSvg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
4544
+ <svg
4545
+ version="1.1"
4546
+ id="Layer_1"
4547
+ x="0px"
4548
+ y="0px"
4549
+ viewBox="380 1300 257.89001 278.8855"
4550
+ xml:space="preserve"
4551
+ width="257.89001"
4552
+ height="278.8855"
4553
+ xmlns="http://www.w3.org/2000/svg">
4554
+ <g
4555
+ id="g10"
4556
+ transform="translate(-383.935,-60.555509)">
4557
+ <g
4558
+ id="g9">
4559
+ <path
4560
+ fill="#f1f2f2"
4561
+ d="m 974.78,1398.211 c -5.016,6.574 -10.034,13.146 -15.048,19.721 -1.828,2.398 -3.657,4.796 -5.487,7.194 1.994,1.719 3.958,3.51 5.873,5.424 18.724,18.731 28.089,41.216 28.089,67.459 0,26.251 -9.366,48.658 -28.089,67.237 -18.731,18.579 -41.215,27.868 -67.459,27.868 -9.848,0 -19.156,-1.308 -27.923,-3.923 l -4.185,3.354 c -8.587,6.885 -17.154,13.796 -25.725,20.702 17.52,8.967 36.86,13.487 58.054,13.487 35.533,0 65.91,-12.608 91.124,-37.821 25.214,-25.215 37.821,-55.584 37.821,-91.125 0,-35.534 -12.607,-65.911 -37.821,-91.126 -3,-2.999 -6.078,-5.808 -9.224,-8.451 z"
4562
+ id="path2" />
4563
+ <path
4564
+ fill="#34d399"
4565
+ d="m 854.024,1585.195 20.001,-16.028 c 16.616,-13.507 33.04,-27.265 50.086,-40.251 1.13,-0.861 2.9,-1.686 2.003,-3.516 -0.843,-1.716 -2.481,-2.302 -4.484,-2.123 -8.514,0.765 -17.016,-0.538 -25.537,-0.353 -1.124,0.024 -2.768,0.221 -3.163,-1.25 -0.371,-1.369 1.088,-2.063 1.919,-2.894 6.26,-6.242 12.574,-12.43 18.816,-18.691 9.303,-9.327 18.565,-18.714 27.851,-28.066 1.848,-1.859 3.701,-3.713 5.549,-5.572 2.655,-2.661 5.309,-5.315 7.958,-7.982 0.574,-0.579 1.259,-1.141 1.246,-1.94 -0.004,-0.257 -0.078,-0.538 -0.254,-0.853 -0.556,-0.981 -1.441,-1.1 -2.469,-0.957 -0.658,0.096 -1.315,0.185 -1.973,0.275 -3.844,0.538 -7.689,1.076 -11.533,1.608 -3.641,0.505 -7.281,1.02 -10.922,1.529 -4.162,0.582 -8.324,1.158 -12.486,1.748 -1.142,0.161 -2.409,1.662 -3.354,0.508 -0.419,-0.508 -0.431,-1.028 -0.251,-1.531 0.269,-0.741 0.957,-1.441 1.387,-2.021 3.414,-4.58 6.882,-9.124 10.356,-13.662 1.74,-2.272 3.48,-4.544 5.214,-6.822 4.682,-6.141 9.369,-12.281 14.051,-18.422 0.09,-0.119 0.181,-0.237 0.271,-0.355 6.848,-8.98 13.7,-17.958 20.553,-26.936 0.488,-0.64 0.977,-1.28 1.465,-1.92 2.159,-2.828 4.315,-5.658 6.476,-8.486 4.197,-5.501 8.454,-10.954 12.67,-16.442 0.263,-0.347 0.538,-0.718 0.717,-1.106 0.269,-0.586 0.299,-1.196 -0.335,-1.776 -0.825,-0.753 -1.8,-0.15 -2.595,0.419 -0.67,0.472 -1.333,0.957 -1.955,1.489 -2.206,1.889 -4.401,3.797 -6.595,5.698 -3.958,3.438 -7.922,6.876 -11.976,10.194 -2.443,2.003 -4.865,4.028 -7.301,6.038 -18.689,-10.581 -39.53,-15.906 -62.549,-15.906 -35.54,0 -65.911,12.607 -91.125,37.82 -25.214,25.215 -37.821,55.592 -37.821,91.126 0,35.54 12.607,65.91 37.821,91.125 4.146,4.146 8.445,7.916 12.87,11.381 -9.015,11.14 -18.036,22.277 -27.034,33.429 -1.208,1.489 -3.755,3.151 -2.745,4.891 0.078,0.144 0.173,0.281 0.305,0.425 1.321,1.429 3.492,-1.303 4.933,-2.457 6.673,-5.333 13.333,-10.685 19.982,-16.042 3.707,-2.984 7.417,-5.965 11.124,-8.952 1.474,-1.188 2.951,-2.373 4.425,-3.561 6.41,-5.164 12.816,-10.333 19.238,-15.481 z m -56.472,-87.186 c 0,-26.243 9.29,-48.728 27.868,-67.459 18.579,-18.723 40.987,-28.089 67.238,-28.089 12.273,0 23.712,2.075 34.34,6.171 -3.37,2.905 -6.734,5.816 -10.069,8.762 -6.075,5.351 -12.365,10.469 -18.667,15.564 -4.179,3.378 -8.371,6.744 -12.514,10.164 -7.54,6.23 -15.037,12.52 -22.529,18.804 -7.091,5.955 -14.182,11.904 -21.19,17.949 -1.136,0.974 -3.055,1.907 -2.135,3.94 0.831,1.836 2.774,1.417 4.341,1.578 l 12.145,-0.599 14.151,-0.698 c 1.031,-0.102 2.192,-0.257 2.89,0.632 0.034,0.044 0.073,0.078 0.106,0.127 1.017,1.561 -0.67,2.105 -1.387,2.942 -6.308,7.318 -12.616,14.637 -18.978,21.907 -8.161,9.339 -16.353,18.649 -24.544,27.958 -2.146,2.433 -4.275,4.879 -6.422,7.312 -1.034,1.172 -2.129,2.272 -1.238,3.922 0.933,1.728 2.685,1.752 4.323,1.602 4.134,-0.367 8.263,-0.489 12.396,-0.492 0.242,0 0.485,-0.01 0.728,0 2.711,0.01 5.422,0.068 8.134,0.145 2.582,0.074 5.166,0.165 7.752,0.249 0.275,1.62 -0.879,2.356 -1.62,3.259 -1.333,1.626 -2.667,3.247 -4,4.867 -4.315,5.252 -8.62,10.514 -12.928,15.772 -3.562,-2.725 -7.007,-5.733 -10.324,-9.051 -18.577,-18.576 -27.867,-40.983 -27.867,-67.234 z"
4566
+ id="path9" />
4567
+ </g>
4568
+ </g>
4569
+ </svg>`;
4570
+
2731
4571
  // src/app.ts
2732
4572
  function createSonicJSApp(config = {}) {
2733
4573
  const app = new Hono();
@@ -2739,7 +4579,6 @@ function createSonicJSApp(config = {}) {
2739
4579
  });
2740
4580
  app.use("*", metricsMiddleware());
2741
4581
  app.use("*", bootstrapMiddleware(config));
2742
- app.use("*", adminSetupMiddleware());
2743
4582
  if (config.middleware?.beforeAuth) {
2744
4583
  for (const middleware of config.middleware.beforeAuth) {
2745
4584
  app.use("*", middleware);
@@ -2767,6 +4606,11 @@ function createSonicJSApp(config = {}) {
2767
4606
  app.route("/admin/seed-data", createSeedDataAdminRoutes());
2768
4607
  app.route("/admin/content", admin_content_default);
2769
4608
  app.route("/admin/media", adminMediaRoutes);
4609
+ if (aiSearchPlugin.routes && aiSearchPlugin.routes.length > 0) {
4610
+ for (const route of aiSearchPlugin.routes) {
4611
+ app.route(route.path, route.handler);
4612
+ }
4613
+ }
2770
4614
  app.route("/admin/plugins", adminPluginRoutes);
2771
4615
  app.route("/admin/logs", adminLogsRoutes);
2772
4616
  app.route("/admin", userRoutes);
@@ -2788,6 +4632,14 @@ function createSonicJSApp(config = {}) {
2788
4632
  app.route(route.path, route.handler);
2789
4633
  }
2790
4634
  }
4635
+ app.get("/favicon.svg", (c) => {
4636
+ return new Response(faviconSvg, {
4637
+ headers: {
4638
+ "Content-Type": "image/svg+xml",
4639
+ "Cache-Control": "public, max-age=31536000"
4640
+ }
4641
+ });
4642
+ });
2791
4643
  app.get("/files/*", async (c) => {
2792
4644
  try {
2793
4645
  const url = new URL(c.req.url);