@reactionary/source 0.3.2 → 0.3.4

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 (33) hide show
  1. package/core/src/client/client-builder.ts +8 -0
  2. package/core/src/client/client.ts +4 -0
  3. package/core/src/providers/index.ts +2 -0
  4. package/core/src/providers/product-associations.provider.ts +49 -0
  5. package/core/src/providers/product-recommendations.provider.ts +150 -0
  6. package/core/src/schemas/capabilities.schema.ts +2 -0
  7. package/core/src/schemas/models/identifiers.model.ts +8 -0
  8. package/core/src/schemas/models/index.ts +1 -0
  9. package/core/src/schemas/models/product-recommendations.model.ts +10 -0
  10. package/core/src/schemas/queries/index.ts +2 -0
  11. package/core/src/schemas/queries/product-associations.query.ts +23 -0
  12. package/core/src/schemas/queries/product-recommendations.query.ts +72 -0
  13. package/documentation/docs/7-marketing.md +95 -0
  14. package/examples/node/package.json +6 -6
  15. package/examples/node/src/capabilities/product-recommendations.spec.ts +140 -0
  16. package/examples/node/src/utils.ts +3 -0
  17. package/package.json +2 -2
  18. package/providers/algolia/package.json +1 -1
  19. package/providers/algolia/src/core/initialize.ts +6 -0
  20. package/providers/algolia/src/index.ts +1 -0
  21. package/providers/algolia/src/providers/index.ts +2 -1
  22. package/providers/algolia/src/providers/product-recommendations.provider.ts +236 -0
  23. package/providers/algolia/src/schema/capabilities.schema.ts +2 -1
  24. package/providers/algolia/src/schema/product-recommendation.schema.ts +9 -0
  25. package/providers/medusa/src/core/initialize.ts +6 -0
  26. package/providers/medusa/src/index.ts +1 -0
  27. package/providers/medusa/src/providers/product-recommendations.provider.ts +117 -0
  28. package/providers/medusa/src/schema/capabilities.schema.ts +1 -0
  29. package/providers/meilisearch/src/core/initialize.ts +6 -0
  30. package/providers/meilisearch/src/index.ts +1 -0
  31. package/providers/meilisearch/src/providers/index.ts +1 -0
  32. package/providers/meilisearch/src/providers/product-recommendations.provider.ts +89 -0
  33. package/providers/meilisearch/src/schema/capabilities.schema.ts +1 -0
@@ -0,0 +1,89 @@
1
+ import {
2
+ type Cache,
3
+ ProductRecommendationsProvider,
4
+ type ProductRecommendation,
5
+ type ProductRecommendationAlgorithmFrequentlyBoughtTogetherQuery,
6
+ type ProductRecommendationAlgorithmSimilarProductsQuery,
7
+ type ProductRecommendationAlgorithmRelatedProductsQuery,
8
+ type ProductRecommendationAlgorithmTrendingInCategoryQuery,
9
+ type RequestContext,
10
+ } from '@reactionary/core';
11
+ import { MeiliSearch, type Hits, type RecordAny, type SearchParams, type SearchResponse } from 'meilisearch';
12
+ import type { MeilisearchConfiguration } from '../schema/configuration.schema.js';
13
+
14
+ interface MeilisearchRecommendHit {
15
+ id: string;
16
+ }
17
+
18
+ /**
19
+ * MeilisearchProductRecommendationsProvider
20
+ *
21
+ * Provides product recommendations using Meilisearch's hybrid search and filtering capabilities.
22
+ * Supports frequentlyBoughtTogether, similar, related, and trendingInCategory algorithms.
23
+ *
24
+ * Note: This implementation uses semantic search (if AI embedding is enabled) and facet-based filtering.
25
+ * For production use, consider implementing more sophisticated recommendation logic or integrating
26
+ * with a dedicated recommendation engine.
27
+ */
28
+ export class MeilisearchProductRecommendationsProvider extends ProductRecommendationsProvider {
29
+ protected config: MeilisearchConfiguration;
30
+
31
+ constructor(config: MeilisearchConfiguration, cache: Cache, context: RequestContext) {
32
+ super(cache, context);
33
+ this.config = config;
34
+ }
35
+
36
+ /**
37
+ * Get similar product recommendations
38
+ * Uses semantic search to find visually or data-wise similar products
39
+ */
40
+ protected override async getSimilarProductsRecommendations(
41
+ query: ProductRecommendationAlgorithmSimilarProductsQuery
42
+ ): Promise<ProductRecommendation[]> {
43
+ const client = new MeiliSearch({
44
+ host: this.config.apiUrl,
45
+ apiKey: this.config.apiKey,
46
+ });
47
+
48
+ const index = client.index(this.config.indexName);
49
+
50
+ if (!this.config.useAIEmbedding) {
51
+ console.warn('AI embedding is not enabled in configuration. Similar product recommendations will be based on keyword matching, which may not provide optimal results.');
52
+ return [];
53
+ }
54
+
55
+ try {
56
+ const searchOptions: SearchParams = {
57
+ limit: query.numberOfRecommendations,
58
+ };
59
+
60
+ const response = await index.searchSimilarDocuments<MeilisearchRecommendHit>({
61
+ id: query.sourceProduct.key,
62
+ limit: query.numberOfRecommendations,
63
+ embedder: this.config.useAIEmbedding,
64
+ });
65
+
66
+
67
+ return this.parseRecommendations(response, 'similar');
68
+ } catch (error) {
69
+ console.error('Error fetching similar product recommendations:', error);
70
+ return [];
71
+ }
72
+ }
73
+
74
+
75
+ /**
76
+ * Maps Meilisearch search results to ProductRecommendation format
77
+ */
78
+ protected parseRecommendations(recommendation: SearchResponse<MeilisearchRecommendHit>, algorithm: string): ProductRecommendation[] {
79
+ return recommendation.hits.map((hit) => ({
80
+ recommendationIdentifier: {
81
+ key: hit.id,
82
+ algorithm,
83
+ },
84
+ product: {
85
+ key: hit.id,
86
+ },
87
+ }));
88
+ }
89
+ }
@@ -3,6 +3,7 @@ import type { z } from 'zod';
3
3
 
4
4
  export const MeilisearchCapabilitiesSchema = CapabilitiesSchema.pick({
5
5
  productSearch: true,
6
+ productRecommendations: true,
6
7
  orderSearch: true,
7
8
  analytics: true
8
9
  }).partial();