@rahul_vendure/ai-chat-plugin 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +214 -0
- package/dist/ai-assistant.plugin.d.ts +18 -0
- package/dist/ai-assistant.plugin.d.ts.map +1 -0
- package/dist/ai-assistant.plugin.js +86 -0
- package/dist/ai-assistant.plugin.js.map +1 -0
- package/dist/api/ai-assistant.resolver.d.ts +11 -0
- package/dist/api/ai-assistant.resolver.d.ts.map +1 -0
- package/dist/api/ai-assistant.resolver.js +43 -0
- package/dist/api/ai-assistant.resolver.js.map +1 -0
- package/dist/api/schema-extensions.d.ts +3 -0
- package/dist/api/schema-extensions.d.ts.map +1 -0
- package/dist/api/schema-extensions.js +15 -0
- package/dist/api/schema-extensions.js.map +1 -0
- package/dist/constants.d.ts +41 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +24 -0
- package/dist/constants.js.map +1 -0
- package/dist/controllers/admin-chat.controller.d.ts +21 -0
- package/dist/controllers/admin-chat.controller.d.ts.map +1 -0
- package/dist/controllers/admin-chat.controller.js +64 -0
- package/dist/controllers/admin-chat.controller.js.map +1 -0
- package/dist/controllers/ai-assistant.controller.d.ts +96 -0
- package/dist/controllers/ai-assistant.controller.d.ts.map +1 -0
- package/dist/controllers/ai-assistant.controller.js +272 -0
- package/dist/controllers/ai-assistant.controller.js.map +1 -0
- package/dist/entities/collection-embedding.entity.d.ts +13 -0
- package/dist/entities/collection-embedding.entity.d.ts.map +1 -0
- package/dist/entities/collection-embedding.entity.js +43 -0
- package/dist/entities/collection-embedding.entity.js.map +1 -0
- package/dist/entities/product-embedding.entity.d.ts +14 -0
- package/dist/entities/product-embedding.entity.d.ts.map +1 -0
- package/dist/entities/product-embedding.entity.js +44 -0
- package/dist/entities/product-embedding.entity.js.map +1 -0
- package/dist/entities/variant-embedding.entity.d.ts +13 -0
- package/dist/entities/variant-embedding.entity.d.ts.map +1 -0
- package/dist/entities/variant-embedding.entity.js +43 -0
- package/dist/entities/variant-embedding.entity.js.map +1 -0
- package/dist/handlers/collection-embedding-handler.d.ts +18 -0
- package/dist/handlers/collection-embedding-handler.d.ts.map +1 -0
- package/dist/handlers/collection-embedding-handler.js +90 -0
- package/dist/handlers/collection-embedding-handler.js.map +1 -0
- package/dist/handlers/product-embedding-handler.d.ts +20 -0
- package/dist/handlers/product-embedding-handler.d.ts.map +1 -0
- package/dist/handlers/product-embedding-handler.js +96 -0
- package/dist/handlers/product-embedding-handler.js.map +1 -0
- package/dist/handlers/variant-embedding-handler.d.ts +19 -0
- package/dist/handlers/variant-embedding-handler.d.ts.map +1 -0
- package/dist/handlers/variant-embedding-handler.js +101 -0
- package/dist/handlers/variant-embedding-handler.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/services/admin-ai-chat.service.d.ts +40 -0
- package/dist/services/admin-ai-chat.service.d.ts.map +1 -0
- package/dist/services/admin-ai-chat.service.js +201 -0
- package/dist/services/admin-ai-chat.service.js.map +1 -0
- package/dist/services/admin-system-prompt.d.ts +2 -0
- package/dist/services/admin-system-prompt.d.ts.map +1 -0
- package/dist/services/admin-system-prompt.js +44 -0
- package/dist/services/admin-system-prompt.js.map +1 -0
- package/dist/services/admin-tools/admin-collection-tools.d.ts +15 -0
- package/dist/services/admin-tools/admin-collection-tools.d.ts.map +1 -0
- package/dist/services/admin-tools/admin-collection-tools.js +48 -0
- package/dist/services/admin-tools/admin-collection-tools.js.map +1 -0
- package/dist/services/admin-tools/admin-customer-tools.d.ts +15 -0
- package/dist/services/admin-tools/admin-customer-tools.d.ts.map +1 -0
- package/dist/services/admin-tools/admin-customer-tools.js +56 -0
- package/dist/services/admin-tools/admin-customer-tools.js.map +1 -0
- package/dist/services/admin-tools/admin-order-tools.d.ts +16 -0
- package/dist/services/admin-tools/admin-order-tools.d.ts.map +1 -0
- package/dist/services/admin-tools/admin-order-tools.js +69 -0
- package/dist/services/admin-tools/admin-order-tools.js.map +1 -0
- package/dist/services/admin-tools/admin-product-tools.d.ts +29 -0
- package/dist/services/admin-tools/admin-product-tools.d.ts.map +1 -0
- package/dist/services/admin-tools/admin-product-tools.js +139 -0
- package/dist/services/admin-tools/admin-product-tools.js.map +1 -0
- package/dist/services/ai-chat.service.d.ts +42 -0
- package/dist/services/ai-chat.service.d.ts.map +1 -0
- package/dist/services/ai-chat.service.js +308 -0
- package/dist/services/ai-chat.service.js.map +1 -0
- package/dist/services/chat-result-mapper.d.ts +17 -0
- package/dist/services/chat-result-mapper.d.ts.map +1 -0
- package/dist/services/chat-result-mapper.js +176 -0
- package/dist/services/chat-result-mapper.js.map +1 -0
- package/dist/services/collection-embedding.service.d.ts +14 -0
- package/dist/services/collection-embedding.service.d.ts.map +1 -0
- package/dist/services/collection-embedding.service.js +67 -0
- package/dist/services/collection-embedding.service.js.map +1 -0
- package/dist/services/conversation-cache.service.d.ts +47 -0
- package/dist/services/conversation-cache.service.d.ts.map +1 -0
- package/dist/services/conversation-cache.service.js +87 -0
- package/dist/services/conversation-cache.service.js.map +1 -0
- package/dist/services/embed-all-job.service.d.ts +28 -0
- package/dist/services/embed-all-job.service.d.ts.map +1 -0
- package/dist/services/embed-all-job.service.js +151 -0
- package/dist/services/embed-all-job.service.js.map +1 -0
- package/dist/services/embedding.service.d.ts +21 -0
- package/dist/services/embedding.service.d.ts.map +1 -0
- package/dist/services/embedding.service.js +58 -0
- package/dist/services/embedding.service.js.map +1 -0
- package/dist/services/product-embedding.service.d.ts +19 -0
- package/dist/services/product-embedding.service.d.ts.map +1 -0
- package/dist/services/product-embedding.service.js +78 -0
- package/dist/services/product-embedding.service.js.map +1 -0
- package/dist/services/system-prompt.d.ts +2 -0
- package/dist/services/system-prompt.d.ts.map +1 -0
- package/dist/services/system-prompt.js +107 -0
- package/dist/services/system-prompt.js.map +1 -0
- package/dist/services/tools/cart-tools.d.ts +14 -0
- package/dist/services/tools/cart-tools.d.ts.map +1 -0
- package/dist/services/tools/cart-tools.js +168 -0
- package/dist/services/tools/cart-tools.js.map +1 -0
- package/dist/services/tools/checkout-tools.d.ts +37 -0
- package/dist/services/tools/checkout-tools.d.ts.map +1 -0
- package/dist/services/tools/checkout-tools.js +228 -0
- package/dist/services/tools/checkout-tools.js.map +1 -0
- package/dist/services/tools/order-tools.d.ts +15 -0
- package/dist/services/tools/order-tools.d.ts.map +1 -0
- package/dist/services/tools/order-tools.js +105 -0
- package/dist/services/tools/order-tools.js.map +1 -0
- package/dist/services/tools/search-tools.d.ts +21 -0
- package/dist/services/tools/search-tools.d.ts.map +1 -0
- package/dist/services/tools/search-tools.js +214 -0
- package/dist/services/tools/search-tools.js.map +1 -0
- package/dist/services/types.d.ts +118 -0
- package/dist/services/types.d.ts.map +1 -0
- package/dist/services/types.js +3 -0
- package/dist/services/types.js.map +1 -0
- package/dist/services/variant-embedding.service.d.ts +14 -0
- package/dist/services/variant-embedding.service.d.ts.map +1 -0
- package/dist/services/variant-embedding.service.js +59 -0
- package/dist/services/variant-embedding.service.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatResultMapper = void 0;
|
|
4
|
+
const core_1 = require("@vendure/core");
|
|
5
|
+
const typeorm_1 = require("typeorm");
|
|
6
|
+
class ChatResultMapper {
|
|
7
|
+
constructor(connection, assetUrlPrefix) {
|
|
8
|
+
this.connection = connection;
|
|
9
|
+
this.assetUrlPrefix = assetUrlPrefix;
|
|
10
|
+
}
|
|
11
|
+
resolveAssetUrl(previewPath) {
|
|
12
|
+
if (!previewPath)
|
|
13
|
+
return null;
|
|
14
|
+
if (previewPath.startsWith('http://') || previewPath.startsWith('https://')) {
|
|
15
|
+
return previewPath;
|
|
16
|
+
}
|
|
17
|
+
return `${this.assetUrlPrefix}${previewPath.replace(/^\//, '')}`;
|
|
18
|
+
}
|
|
19
|
+
mapOrders(orders) {
|
|
20
|
+
return orders.map(order => {
|
|
21
|
+
var _a, _b, _c, _d;
|
|
22
|
+
const lines = ((_a = order.lines) !== null && _a !== void 0 ? _a : []).map((line) => {
|
|
23
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
|
|
24
|
+
const name = ((_c = (_b = (_a = line.productVariant) === null || _a === void 0 ? void 0 : _a.translations) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.name)
|
|
25
|
+
|| ((_d = line.productVariant) === null || _d === void 0 ? void 0 : _d.name)
|
|
26
|
+
|| 'Unknown product';
|
|
27
|
+
// Resolve product image: prefer order line's featured asset, then variant's, then product's
|
|
28
|
+
const imagePreview = (_o = (_j = (_f = (_e = line.featuredAsset) === null || _e === void 0 ? void 0 : _e.preview) !== null && _f !== void 0 ? _f : (_h = (_g = line.productVariant) === null || _g === void 0 ? void 0 : _g.featuredAsset) === null || _h === void 0 ? void 0 : _h.preview) !== null && _j !== void 0 ? _j : (_m = (_l = (_k = line.productVariant) === null || _k === void 0 ? void 0 : _k.product) === null || _l === void 0 ? void 0 : _l.featuredAsset) === null || _m === void 0 ? void 0 : _m.preview) !== null && _o !== void 0 ? _o : null;
|
|
29
|
+
// Resolve slug from product translations or product itself
|
|
30
|
+
const slug = (_w = (_t = (_s = (_r = (_q = (_p = line.productVariant) === null || _p === void 0 ? void 0 : _p.product) === null || _q === void 0 ? void 0 : _q.translations) === null || _r === void 0 ? void 0 : _r[0]) === null || _s === void 0 ? void 0 : _s.slug) !== null && _t !== void 0 ? _t : (_v = (_u = line.productVariant) === null || _u === void 0 ? void 0 : _u.product) === null || _v === void 0 ? void 0 : _v.slug) !== null && _w !== void 0 ? _w : '';
|
|
31
|
+
return {
|
|
32
|
+
productName: name,
|
|
33
|
+
quantity: (_x = line.quantity) !== null && _x !== void 0 ? _x : 0,
|
|
34
|
+
slug: String(slug),
|
|
35
|
+
variantId: String((_z = (_y = line.productVariant) === null || _y === void 0 ? void 0 : _y.id) !== null && _z !== void 0 ? _z : ''),
|
|
36
|
+
image: this.resolveAssetUrl(imagePreview),
|
|
37
|
+
linePriceWithTax: (_1 = (_0 = line.linePriceWithTax) !== null && _0 !== void 0 ? _0 : line.linePrice) !== null && _1 !== void 0 ? _1 : 0,
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
id: String(order.id),
|
|
42
|
+
code: order.code,
|
|
43
|
+
state: order.state,
|
|
44
|
+
total: (_c = (_b = order.totalWithTax) !== null && _b !== void 0 ? _b : order.total) !== null && _c !== void 0 ? _c : 0,
|
|
45
|
+
orderPlacedAt: order.orderPlacedAt ? new Date(order.orderPlacedAt).toISOString() : null,
|
|
46
|
+
lines,
|
|
47
|
+
fulfillments: ((_d = order.fulfillments) !== null && _d !== void 0 ? _d : []).map((f) => {
|
|
48
|
+
var _a, _b, _c;
|
|
49
|
+
return ({
|
|
50
|
+
state: (_a = f.state) !== null && _a !== void 0 ? _a : 'Unknown',
|
|
51
|
+
method: (_b = f.method) !== null && _b !== void 0 ? _b : 'Unknown',
|
|
52
|
+
trackingCode: (_c = f.trackingCode) !== null && _c !== void 0 ? _c : null,
|
|
53
|
+
});
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
mapActiveOrder(order) {
|
|
59
|
+
var _a, _b, _c;
|
|
60
|
+
const lines = ((_a = order.lines) !== null && _a !== void 0 ? _a : []).map((line) => {
|
|
61
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
62
|
+
const name = ((_c = (_b = (_a = line.productVariant) === null || _a === void 0 ? void 0 : _a.translations) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.name)
|
|
63
|
+
|| ((_d = line.productVariant) === null || _d === void 0 ? void 0 : _d.name)
|
|
64
|
+
|| 'Unknown product';
|
|
65
|
+
return {
|
|
66
|
+
orderLineId: String(line.id),
|
|
67
|
+
productName: name,
|
|
68
|
+
variantId: String((_f = (_e = line.productVariant) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : ''),
|
|
69
|
+
quantity: (_g = line.quantity) !== null && _g !== void 0 ? _g : 0,
|
|
70
|
+
linePriceWithTax: (_j = (_h = line.linePriceWithTax) !== null && _h !== void 0 ? _h : line.linePrice) !== null && _j !== void 0 ? _j : 0,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
id: String(order.id),
|
|
75
|
+
code: order.code,
|
|
76
|
+
totalQuantity: (_b = order.totalQuantity) !== null && _b !== void 0 ? _b : lines.reduce((sum, l) => sum + l.quantity, 0),
|
|
77
|
+
totalWithTax: (_c = order.totalWithTax) !== null && _c !== void 0 ? _c : 0,
|
|
78
|
+
lines,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
mapProducts(products) {
|
|
82
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
83
|
+
const result = [];
|
|
84
|
+
for (const p of products) {
|
|
85
|
+
const translations = p.translations;
|
|
86
|
+
const name = (_c = (_b = (_a = translations === null || translations === void 0 ? void 0 : translations[0]) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : p.name) !== null && _c !== void 0 ? _c : '';
|
|
87
|
+
const slug = (_f = (_e = (_d = translations === null || translations === void 0 ? void 0 : translations[0]) === null || _d === void 0 ? void 0 : _d.slug) !== null && _e !== void 0 ? _e : p.slug) !== null && _f !== void 0 ? _f : '';
|
|
88
|
+
const variant = (_g = p.variants) === null || _g === void 0 ? void 0 : _g[0];
|
|
89
|
+
const prices = variant === null || variant === void 0 ? void 0 : variant.productVariantPrices;
|
|
90
|
+
const priceValue = (_k = (_j = (_h = prices === null || prices === void 0 ? void 0 : prices[0]) === null || _h === void 0 ? void 0 : _h.price) !== null && _j !== void 0 ? _j : variant === null || variant === void 0 ? void 0 : variant.price) !== null && _k !== void 0 ? _k : 0;
|
|
91
|
+
const asset = p.featuredAsset;
|
|
92
|
+
const image = (_l = asset === null || asset === void 0 ? void 0 : asset.preview) !== null && _l !== void 0 ? _l : null;
|
|
93
|
+
const variantId = variant ? String(variant.id) : '';
|
|
94
|
+
result.push({
|
|
95
|
+
id: String(p.id),
|
|
96
|
+
name: String(name),
|
|
97
|
+
slug: String(slug),
|
|
98
|
+
price: Number(priceValue),
|
|
99
|
+
image: this.resolveAssetUrl(image),
|
|
100
|
+
variantId,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
mapCollections(collections) {
|
|
106
|
+
return collections
|
|
107
|
+
.filter(c => {
|
|
108
|
+
var _a, _b, _c, _d;
|
|
109
|
+
const isRoot = c.isRoot;
|
|
110
|
+
if (isRoot)
|
|
111
|
+
return false;
|
|
112
|
+
const t = c.translations;
|
|
113
|
+
const slug = (_b = (_a = t === null || t === void 0 ? void 0 : t[0]) === null || _a === void 0 ? void 0 : _a.slug) !== null && _b !== void 0 ? _b : '';
|
|
114
|
+
const name = (_d = (_c = t === null || t === void 0 ? void 0 : t[0]) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : '';
|
|
115
|
+
if (slug.includes('__root') || name.includes('__root'))
|
|
116
|
+
return false;
|
|
117
|
+
if (!name || !slug)
|
|
118
|
+
return false;
|
|
119
|
+
return true;
|
|
120
|
+
})
|
|
121
|
+
.map(c => {
|
|
122
|
+
var _a, _b, _c, _d;
|
|
123
|
+
const t = c.translations;
|
|
124
|
+
return {
|
|
125
|
+
id: String(c.id),
|
|
126
|
+
name: String((_b = (_a = t === null || t === void 0 ? void 0 : t[0]) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : ''),
|
|
127
|
+
slug: String((_d = (_c = t === null || t === void 0 ? void 0 : t[0]) === null || _c === void 0 ? void 0 : _c.slug) !== null && _d !== void 0 ? _d : ''),
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async loadProductsByIds(productIds) {
|
|
132
|
+
if (productIds.length === 0)
|
|
133
|
+
return [];
|
|
134
|
+
const repo = this.connection.rawConnection.getRepository(core_1.Product);
|
|
135
|
+
const list = await repo.find({
|
|
136
|
+
where: { id: (0, typeorm_1.In)(productIds) },
|
|
137
|
+
relations: ['variants', 'variants.productVariantPrices', 'featuredAsset', 'translations'],
|
|
138
|
+
});
|
|
139
|
+
const byId = new Map(list.map(p => [p.id, p]));
|
|
140
|
+
return this.mapProducts(productIds.map(id => byId.get(id)).filter((p) => p != null));
|
|
141
|
+
}
|
|
142
|
+
async loadCollectionsByIds(collectionIds) {
|
|
143
|
+
if (collectionIds.length === 0)
|
|
144
|
+
return [];
|
|
145
|
+
const repo = this.connection.rawConnection.getRepository(core_1.Collection);
|
|
146
|
+
const list = await repo.find({
|
|
147
|
+
where: { id: (0, typeorm_1.In)(collectionIds) },
|
|
148
|
+
relations: ['translations'],
|
|
149
|
+
});
|
|
150
|
+
return this.mapCollections(list);
|
|
151
|
+
}
|
|
152
|
+
mergeProducts(existing, incoming) {
|
|
153
|
+
const ids = new Set(existing.map(p => p.id));
|
|
154
|
+
const merged = [...existing];
|
|
155
|
+
for (const p of incoming) {
|
|
156
|
+
if (!ids.has(p.id)) {
|
|
157
|
+
merged.push(p);
|
|
158
|
+
ids.add(p.id);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return merged;
|
|
162
|
+
}
|
|
163
|
+
mergeCollections(existing, incoming) {
|
|
164
|
+
const ids = new Set(existing.map(c => c.id));
|
|
165
|
+
const merged = [...existing];
|
|
166
|
+
for (const c of incoming) {
|
|
167
|
+
if (!ids.has(c.id)) {
|
|
168
|
+
merged.push(c);
|
|
169
|
+
ids.add(c.id);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return merged;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.ChatResultMapper = ChatResultMapper;
|
|
176
|
+
//# sourceMappingURL=chat-result-mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-result-mapper.js","sourceRoot":"","sources":["../../src/services/chat-result-mapper.ts"],"names":[],"mappings":";;;AAAA,wCAAoG;AACpG,qCAA6B;AAG7B,MAAa,gBAAgB;IACzB,YACY,UAAmC,EACnC,cAAsB;QADtB,eAAU,GAAV,UAAU,CAAyB;QACnC,mBAAc,GAAd,cAAc,CAAQ;IAC/B,CAAC;IAEJ,eAAe,CAAC,WAA0B;QACtC,IAAI,CAAC,WAAW;YAAE,OAAO,IAAI,CAAC;QAC9B,IAAI,WAAW,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAC1E,OAAO,WAAW,CAAC;QACvB,CAAC;QACD,OAAO,GAAG,IAAI,CAAC,cAAc,GAAG,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC;IACrE,CAAC;IAED,SAAS,CAAC,MAAe;QACrB,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;;YACtB,MAAM,KAAK,GAAsB,CAAC,MAAC,KAAa,CAAC,KAAK,mCAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE;;gBAC5E,MAAM,IAAI,GAAG,CAAA,MAAA,MAAA,MAAA,IAAI,CAAC,cAAc,0CAAE,YAAY,0CAAG,CAAC,CAAC,0CAAE,IAAI;wBAClD,MAAA,IAAI,CAAC,cAAc,0CAAE,IAAI,CAAA;uBACzB,iBAAiB,CAAC;gBACzB,4FAA4F;gBAC5F,MAAM,YAAY,GAAG,MAAA,MAAA,MAAA,MAAA,IAAI,CAAC,aAAa,0CAAE,OAAO,mCACzC,MAAA,MAAA,IAAI,CAAC,cAAc,0CAAE,aAAa,0CAAE,OAAO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ClB,CAAC;CACJ;AAlKD,4CAkKC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RequestContext, TransactionalConnection } from '@vendure/core';
|
|
2
|
+
export interface SimilarCollectionResult {
|
|
3
|
+
collectionId: number;
|
|
4
|
+
score: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class CollectionEmbeddingService {
|
|
7
|
+
private connection;
|
|
8
|
+
constructor(connection: TransactionalConnection);
|
|
9
|
+
upsertEmbedding(ctx: RequestContext, collectionId: number, embedding: number[]): Promise<void>;
|
|
10
|
+
deleteByCollectionId(ctx: RequestContext, collectionId: number): Promise<void>;
|
|
11
|
+
findSimilar(ctx: RequestContext, queryEmbedding: number[], limit?: number, minScore?: number): Promise<SimilarCollectionResult[]>;
|
|
12
|
+
ensureRow(ctx: RequestContext, collectionId: number): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=collection-embedding.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection-embedding.service.d.ts","sourceRoot":"","sources":["../../src/services/collection-embedding.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAGxE,MAAM,WAAW,uBAAuB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,qBACa,0BAA0B;IACvB,OAAO,CAAC,UAAU;gBAAV,UAAU,EAAE,uBAAuB;IAEjD,eAAe,CACjB,GAAG,EAAE,cAAc,EACnB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,EAAE,GACpB,OAAO,CAAC,IAAI,CAAC;IAWV,oBAAoB,CAAC,GAAG,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK9E,WAAW,CACb,GAAG,EAAE,cAAc,EACnB,cAAc,EAAE,MAAM,EAAE,EACxB,KAAK,GAAE,MAAU,EACjB,QAAQ,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,uBAAuB,EAAE,CAAC;IAqB/B,SAAS,CAAC,GAAG,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ5E"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CollectionEmbeddingService = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@vendure/core");
|
|
15
|
+
const collection_embedding_entity_1 = require("../entities/collection-embedding.entity");
|
|
16
|
+
let CollectionEmbeddingService = class CollectionEmbeddingService {
|
|
17
|
+
constructor(connection) {
|
|
18
|
+
this.connection = connection;
|
|
19
|
+
}
|
|
20
|
+
async upsertEmbedding(ctx, collectionId, embedding) {
|
|
21
|
+
var _a;
|
|
22
|
+
await this.ensureRow(ctx, collectionId);
|
|
23
|
+
const conn = this.connection.rawConnection;
|
|
24
|
+
const vectorStr = '[' + embedding.map((n) => Number(n)).join(',') + ']';
|
|
25
|
+
const schema = (_a = conn.options.schema) !== null && _a !== void 0 ? _a : 'public';
|
|
26
|
+
await conn.query(`UPDATE ${schema}.collection_embedding SET embedding = $1::vector, "updatedAt" = NOW() WHERE "collectionId" = $2`, [vectorStr, collectionId]);
|
|
27
|
+
}
|
|
28
|
+
async deleteByCollectionId(ctx, collectionId) {
|
|
29
|
+
const repo = this.connection.rawConnection.getRepository(collection_embedding_entity_1.CollectionEmbedding);
|
|
30
|
+
await repo.delete({ collectionId });
|
|
31
|
+
}
|
|
32
|
+
async findSimilar(ctx, queryEmbedding, limit = 5, minScore) {
|
|
33
|
+
var _a;
|
|
34
|
+
const conn = this.connection.rawConnection;
|
|
35
|
+
const vectorStr = '[' + queryEmbedding.map((n) => Number(n)).join(',') + ']';
|
|
36
|
+
const schema = (_a = conn.options.schema) !== null && _a !== void 0 ? _a : 'public';
|
|
37
|
+
const hasThreshold = minScore != null && minScore > 0;
|
|
38
|
+
const sql = hasThreshold
|
|
39
|
+
? `SELECT "collectionId", 1 - (embedding <=> $1::vector) AS score
|
|
40
|
+
FROM ${schema}.collection_embedding
|
|
41
|
+
WHERE embedding IS NOT NULL AND (1 - (embedding <=> $1::vector)) >= $3
|
|
42
|
+
ORDER BY embedding <=> $1::vector LIMIT $2`
|
|
43
|
+
: `SELECT "collectionId", 1 - (embedding <=> $1::vector) AS score
|
|
44
|
+
FROM ${schema}.collection_embedding WHERE embedding IS NOT NULL
|
|
45
|
+
ORDER BY embedding <=> $1::vector LIMIT $2`;
|
|
46
|
+
const params = hasThreshold ? [vectorStr, limit, minScore] : [vectorStr, limit];
|
|
47
|
+
const rows = await conn.query(sql, params);
|
|
48
|
+
return rows.map((r) => ({
|
|
49
|
+
collectionId: r.collectionId,
|
|
50
|
+
score: Number(r.score),
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
async ensureRow(ctx, collectionId) {
|
|
54
|
+
const repo = this.connection.rawConnection.getRepository(collection_embedding_entity_1.CollectionEmbedding);
|
|
55
|
+
let row = await repo.findOne({ where: { collectionId } });
|
|
56
|
+
if (!row) {
|
|
57
|
+
row = repo.create({ collectionId });
|
|
58
|
+
await repo.save(row);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.CollectionEmbeddingService = CollectionEmbeddingService;
|
|
63
|
+
exports.CollectionEmbeddingService = CollectionEmbeddingService = __decorate([
|
|
64
|
+
(0, common_1.Injectable)(),
|
|
65
|
+
__metadata("design:paramtypes", [core_1.TransactionalConnection])
|
|
66
|
+
], CollectionEmbeddingService);
|
|
67
|
+
//# sourceMappingURL=collection-embedding.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection-embedding.service.js","sourceRoot":"","sources":["../../src/services/collection-embedding.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,wCAAwE;AACxE,yFAA8E;AAQvE,IAAM,0BAA0B,GAAhC,MAAM,0BAA0B;IACnC,YAAoB,UAAmC;QAAnC,eAAU,GAAV,UAAU,CAAyB;IAAG,CAAC;IAE3D,KAAK,CAAC,eAAe,CACjB,GAAmB,EACnB,YAAoB,EACpB,SAAmB;;QAEnB,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;QACxC,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC;QAC3C,MAAM,SAAS,GAAG,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;QACxE,MAAM,MAAM,GAAG,MAAC,IAAI,CAAC,OAA+B,CAAC,MAAM,mCAAI,QAAQ,CAAC;QACxE,MAAM,IAAI,CAAC,KAAK,CACZ,UAAU,MAAM,iGAAiG,EACjH,CAAC,SAAS,EAAE,YAAY,CAAC,CAC5B,CAAC;IACN,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,GAAmB,EAAE,YAAoB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AE,YAAoB;QACrD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,aAAa,CAAC,iDAAmB,CAAC,CAAC;QAC9E,IAAI,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,YAAY,EAAE,EAAE,CAAC,CAAC;QAC1D,IAAI,CAAC,GAAG,EAAE,CAAC;YACP,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC;YACpC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;IACL,CAAC;CACJ,CAAA;AAzDY,gEAA0B;qCAA1B,0BAA0B;IADtC,IAAA,mBAAU,GAAE;qCAEuB,8BAAuB;GAD9C,0BAA0B,CAyDtC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { CacheService } from '@vendure/core';
|
|
3
|
+
export interface CachedConversation {
|
|
4
|
+
messages: Array<{
|
|
5
|
+
role: 'user' | 'assistant';
|
|
6
|
+
content: string;
|
|
7
|
+
}>;
|
|
8
|
+
customerId?: string | number;
|
|
9
|
+
createdAt: number;
|
|
10
|
+
lastActiveAt: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Caches conversation history server-side using Vendure's CacheService.
|
|
14
|
+
*
|
|
15
|
+
* Instead of the frontend sending the full message history on every request,
|
|
16
|
+
* the server maintains the history keyed by a `conversationId` (UUID generated
|
|
17
|
+
* by the frontend when the chat widget mounts).
|
|
18
|
+
*
|
|
19
|
+
* Conversations expire after 30 minutes of inactivity.
|
|
20
|
+
*/
|
|
21
|
+
export declare class ConversationCacheService implements OnModuleInit {
|
|
22
|
+
private cacheService;
|
|
23
|
+
private readonly logger;
|
|
24
|
+
constructor(cacheService: CacheService);
|
|
25
|
+
onModuleInit(): void;
|
|
26
|
+
private cacheKey;
|
|
27
|
+
/**
|
|
28
|
+
* Retrieve the cached conversation history for a given conversationId.
|
|
29
|
+
* Returns undefined if the conversation has expired or doesn't exist.
|
|
30
|
+
*/
|
|
31
|
+
getHistory(conversationId: string): Promise<CachedConversation | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Append a user message and the corresponding assistant response to the
|
|
34
|
+
* cached conversation. If the conversation doesn't exist yet, it is
|
|
35
|
+
* created implicitly.
|
|
36
|
+
*/
|
|
37
|
+
appendMessages(conversationId: string, userMessage: string, assistantResponse: string, customerId?: string | number): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Delete a specific conversation from cache.
|
|
40
|
+
*/
|
|
41
|
+
deleteConversation(conversationId: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Invalidate all cached conversations (e.g. on deployment or cache reset).
|
|
44
|
+
*/
|
|
45
|
+
invalidateAll(): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=conversation-cache.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation-cache.service.d.ts","sourceRoot":"","sources":["../../src/services/conversation-cache.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,WAAW,kBAAkB;IAC/B,QAAQ,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACjE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;CACxB;AAKD;;;;;;;;GAQG;AACH,qBACa,wBAAyB,YAAW,YAAY;IAG7C,OAAO,CAAC,YAAY;IAFhC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6C;gBAEhD,YAAY,EAAE,YAAY;IAE9C,YAAY;IAIZ,OAAO,CAAC,QAAQ;IAIhB;;;OAGG;IACG,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC;IAIjF;;;;OAIG;IACG,cAAc,CAChB,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,MAAM,EACnB,iBAAiB,EAAE,MAAM,EACzB,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAC7B,OAAO,CAAC,IAAI,CAAC;IA6BhB;;OAEG;IACG,kBAAkB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK/D;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;CAIvC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var ConversationCacheService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.ConversationCacheService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const core_1 = require("@vendure/core");
|
|
16
|
+
/** TTL for cached conversations: 30 minutes */
|
|
17
|
+
const CONVERSATION_TTL_MS = 1000 * 60 * 30;
|
|
18
|
+
/**
|
|
19
|
+
* Caches conversation history server-side using Vendure's CacheService.
|
|
20
|
+
*
|
|
21
|
+
* Instead of the frontend sending the full message history on every request,
|
|
22
|
+
* the server maintains the history keyed by a `conversationId` (UUID generated
|
|
23
|
+
* by the frontend when the chat widget mounts).
|
|
24
|
+
*
|
|
25
|
+
* Conversations expire after 30 minutes of inactivity.
|
|
26
|
+
*/
|
|
27
|
+
let ConversationCacheService = ConversationCacheService_1 = class ConversationCacheService {
|
|
28
|
+
constructor(cacheService) {
|
|
29
|
+
this.cacheService = cacheService;
|
|
30
|
+
this.logger = new common_1.Logger(ConversationCacheService_1.name);
|
|
31
|
+
}
|
|
32
|
+
onModuleInit() {
|
|
33
|
+
this.logger.log('ConversationCacheService initialized');
|
|
34
|
+
}
|
|
35
|
+
cacheKey(conversationId) {
|
|
36
|
+
return `ai-chat:conversation:${conversationId}`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Retrieve the cached conversation history for a given conversationId.
|
|
40
|
+
* Returns undefined if the conversation has expired or doesn't exist.
|
|
41
|
+
*/
|
|
42
|
+
async getHistory(conversationId) {
|
|
43
|
+
return this.cacheService.get(this.cacheKey(conversationId));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Append a user message and the corresponding assistant response to the
|
|
47
|
+
* cached conversation. If the conversation doesn't exist yet, it is
|
|
48
|
+
* created implicitly.
|
|
49
|
+
*/
|
|
50
|
+
async appendMessages(conversationId, userMessage, assistantResponse, customerId) {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
const existing = await this.getHistory(conversationId);
|
|
53
|
+
const messages = (_a = existing === null || existing === void 0 ? void 0 : existing.messages) !== null && _a !== void 0 ? _a : [];
|
|
54
|
+
messages.push({ role: 'user', content: userMessage }, { role: 'assistant', content: assistantResponse });
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
await this.cacheService.set(this.cacheKey(conversationId), {
|
|
57
|
+
messages,
|
|
58
|
+
customerId: customerId !== null && customerId !== void 0 ? customerId : existing === null || existing === void 0 ? void 0 : existing.customerId,
|
|
59
|
+
createdAt: (_b = existing === null || existing === void 0 ? void 0 : existing.createdAt) !== null && _b !== void 0 ? _b : now,
|
|
60
|
+
lastActiveAt: now,
|
|
61
|
+
}, {
|
|
62
|
+
ttl: CONVERSATION_TTL_MS,
|
|
63
|
+
tags: ['ai-chat-conversations'],
|
|
64
|
+
});
|
|
65
|
+
this.logger.debug(`Conversation ${conversationId}: ${messages.length} messages cached`);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Delete a specific conversation from cache.
|
|
69
|
+
*/
|
|
70
|
+
async deleteConversation(conversationId) {
|
|
71
|
+
await this.cacheService.delete(this.cacheKey(conversationId));
|
|
72
|
+
this.logger.debug(`Conversation ${conversationId} deleted from cache`);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Invalidate all cached conversations (e.g. on deployment or cache reset).
|
|
76
|
+
*/
|
|
77
|
+
async invalidateAll() {
|
|
78
|
+
await this.cacheService.invalidateTags(['ai-chat-conversations']);
|
|
79
|
+
this.logger.log('All cached conversations invalidated');
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
exports.ConversationCacheService = ConversationCacheService;
|
|
83
|
+
exports.ConversationCacheService = ConversationCacheService = ConversationCacheService_1 = __decorate([
|
|
84
|
+
(0, common_1.Injectable)(),
|
|
85
|
+
__metadata("design:paramtypes", [core_1.CacheService])
|
|
86
|
+
], ConversationCacheService);
|
|
87
|
+
//# sourceMappingURL=conversation-cache.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation-cache.service.js","sourceRoot":"","sources":["../../src/services/conversation-cache.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAkE;AAClE,wCAA6C;AAS7C,+CAA+C;AAC/C,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,CAAC;AAE3C;;;;;;;;GAQ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AAQ,uBAAR,QAAQ,CAAE,SAAS,mCAAI,GAAG;YACrC,YAAY,EAAE,GAAG;SACpB,EACD;YACI,GAAG,EAAE,mBAAmB;YACxB,IAAI,EAAE,CAAC,uBAAuB,CAAC;SAClC,CACJ,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,KAAK,CACb,gBAAgB,cAAc,KAAK,QAAQ,CAAC,MAAM,kBAAkB,CACvE,CAAC;IACN,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,kBAAkB,CAAC,cAAsB;QAC3C,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC;QAC9D,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,cAAc,qBAAqB,CAAC,CAAC;IAC3E,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,aAAa;QACf,MAAM,IAAI,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC;QAClE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;IAC5D,CAAC;CACJ,CAAA;AA3EY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;qCAIyB,mBAAY;GAHrC,wBAAwB,CA2EpC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { JobQueueService, RequestContextService, TransactionalConnection } from '@vendure/core';
|
|
3
|
+
import { EmbeddingService } from './embedding.service';
|
|
4
|
+
import { ProductEmbeddingService } from './product-embedding.service';
|
|
5
|
+
import { VariantEmbeddingService } from './variant-embedding.service';
|
|
6
|
+
import { CollectionEmbeddingService } from './collection-embedding.service';
|
|
7
|
+
export type EmbedAllJobData = Record<string, never>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates the embed-all job queue and processes it: loads all products, variants, collections
|
|
10
|
+
* and generates/upserts embeddings in the background.
|
|
11
|
+
*/
|
|
12
|
+
export declare class EmbedAllJobService implements OnModuleInit {
|
|
13
|
+
private jobQueueService;
|
|
14
|
+
private requestContextService;
|
|
15
|
+
private connection;
|
|
16
|
+
private embeddingService;
|
|
17
|
+
private productEmbeddingService;
|
|
18
|
+
private variantEmbeddingService;
|
|
19
|
+
private collectionEmbeddingService;
|
|
20
|
+
private jobQueue;
|
|
21
|
+
constructor(jobQueueService: JobQueueService, requestContextService: RequestContextService, connection: TransactionalConnection, embeddingService: EmbeddingService, productEmbeddingService: ProductEmbeddingService, variantEmbeddingService: VariantEmbeddingService, collectionEmbeddingService: CollectionEmbeddingService);
|
|
22
|
+
onModuleInit(): Promise<void>;
|
|
23
|
+
enqueue(): Promise<{
|
|
24
|
+
jobId: string;
|
|
25
|
+
}>;
|
|
26
|
+
private runEmbedAll;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=embed-all-job.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embed-all-job.service.d.ts","sourceRoot":"","sources":["../../src/services/embed-all-job.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EACH,eAAe,EAEf,qBAAqB,EACrB,uBAAuB,EAI1B,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,0BAA0B,EAAE,MAAM,gCAAgC,CAAC;AAE5E,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAEpD;;;GAGG;AACH,qBACa,kBAAmB,YAAW,YAAY;IAI/C,OAAO,CAAC,eAAe;IACvB,OAAO,CAAC,qBAAqB;IAC7B,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,gBAAgB;IACxB,OAAO,CAAC,uBAAuB;IAC/B,OAAO,CAAC,uBAAuB;IAC/B,OAAO,CAAC,0BAA0B;IATtC,OAAO,CAAC,QAAQ,CAA6B;gBAGjC,eAAe,EAAE,eAAe,EAChC,qBAAqB,EAAE,qBAAqB,EAC5C,UAAU,EAAE,uBAAuB,EACnC,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,uBAAuB,EAChD,uBAAuB,EAAE,uBAAuB,EAChD,0BAA0B,EAAE,0BAA0B;IAG5D,YAAY;IAYZ,OAAO,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;YAK7B,WAAW;CAgE5B"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EmbedAllJobService = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@vendure/core");
|
|
15
|
+
const constants_1 = require("../constants");
|
|
16
|
+
const embedding_service_1 = require("./embedding.service");
|
|
17
|
+
const product_embedding_service_1 = require("./product-embedding.service");
|
|
18
|
+
const variant_embedding_service_1 = require("./variant-embedding.service");
|
|
19
|
+
const collection_embedding_service_1 = require("./collection-embedding.service");
|
|
20
|
+
/**
|
|
21
|
+
* Creates the embed-all job queue and processes it: loads all products, variants, collections
|
|
22
|
+
* and generates/upserts embeddings in the background.
|
|
23
|
+
*/
|
|
24
|
+
let EmbedAllJobService = class EmbedAllJobService {
|
|
25
|
+
constructor(jobQueueService, requestContextService, connection, embeddingService, productEmbeddingService, variantEmbeddingService, collectionEmbeddingService) {
|
|
26
|
+
this.jobQueueService = jobQueueService;
|
|
27
|
+
this.requestContextService = requestContextService;
|
|
28
|
+
this.connection = connection;
|
|
29
|
+
this.embeddingService = embeddingService;
|
|
30
|
+
this.productEmbeddingService = productEmbeddingService;
|
|
31
|
+
this.variantEmbeddingService = variantEmbeddingService;
|
|
32
|
+
this.collectionEmbeddingService = collectionEmbeddingService;
|
|
33
|
+
}
|
|
34
|
+
async onModuleInit() {
|
|
35
|
+
this.jobQueue = await this.jobQueueService.createQueue({
|
|
36
|
+
name: constants_1.EMBED_ALL_QUEUE_NAME,
|
|
37
|
+
process: async () => {
|
|
38
|
+
const ctx = await this.requestContextService.create({
|
|
39
|
+
apiType: 'admin',
|
|
40
|
+
});
|
|
41
|
+
await this.runEmbedAll(ctx);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async enqueue() {
|
|
46
|
+
const job = await this.jobQueue.add({});
|
|
47
|
+
return { jobId: String(job.id) };
|
|
48
|
+
}
|
|
49
|
+
async runEmbedAll(ctx) {
|
|
50
|
+
const reqCtx = ctx;
|
|
51
|
+
const conn = this.connection.rawConnection;
|
|
52
|
+
const productRepo = conn.getRepository(core_1.Product);
|
|
53
|
+
const variantRepo = conn.getRepository(core_1.ProductVariant);
|
|
54
|
+
const collectionRepo = conn.getRepository(core_1.Collection);
|
|
55
|
+
const products = await productRepo.find({
|
|
56
|
+
relations: ['translations'],
|
|
57
|
+
});
|
|
58
|
+
for (const product of products) {
|
|
59
|
+
const text = buildProductText(product);
|
|
60
|
+
if (text.trim()) {
|
|
61
|
+
try {
|
|
62
|
+
const embedding = await this.embeddingService.embed(text);
|
|
63
|
+
await this.productEmbeddingService.upsertEmbedding(reqCtx, product.id, embedding);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.error('[AiAssistantPlugin] embed-all product', product.id, e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const variants = await variantRepo.find({
|
|
71
|
+
relations: ['translations', 'product', 'product.translations'],
|
|
72
|
+
});
|
|
73
|
+
for (const variant of variants) {
|
|
74
|
+
const text = buildVariantText(variant);
|
|
75
|
+
if (text.trim()) {
|
|
76
|
+
try {
|
|
77
|
+
const embedding = await this.embeddingService.embed(text);
|
|
78
|
+
await this.variantEmbeddingService.upsertEmbedding(reqCtx, variant.id, embedding);
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
console.error('[AiAssistantPlugin] embed-all variant', variant.id, e);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const collections = await collectionRepo.find({
|
|
86
|
+
relations: ['translations'],
|
|
87
|
+
});
|
|
88
|
+
for (const collection of collections) {
|
|
89
|
+
const text = buildCollectionText(collection);
|
|
90
|
+
if (text.trim()) {
|
|
91
|
+
try {
|
|
92
|
+
const embedding = await this.embeddingService.embed(text);
|
|
93
|
+
await this.collectionEmbeddingService.upsertEmbedding(reqCtx, collection.id, embedding);
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
console.error('[AiAssistantPlugin] embed-all collection', collection.id, e);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
exports.EmbedAllJobService = EmbedAllJobService;
|
|
103
|
+
exports.EmbedAllJobService = EmbedAllJobService = __decorate([
|
|
104
|
+
(0, common_1.Injectable)(),
|
|
105
|
+
__metadata("design:paramtypes", [core_1.JobQueueService,
|
|
106
|
+
core_1.RequestContextService,
|
|
107
|
+
core_1.TransactionalConnection,
|
|
108
|
+
embedding_service_1.EmbeddingService,
|
|
109
|
+
product_embedding_service_1.ProductEmbeddingService,
|
|
110
|
+
variant_embedding_service_1.VariantEmbeddingService,
|
|
111
|
+
collection_embedding_service_1.CollectionEmbeddingService])
|
|
112
|
+
], EmbedAllJobService);
|
|
113
|
+
function buildProductText(product) {
|
|
114
|
+
const parts = [];
|
|
115
|
+
for (const t of product.translations || []) {
|
|
116
|
+
if (t === null || t === void 0 ? void 0 : t.name)
|
|
117
|
+
parts.push(String(t.name));
|
|
118
|
+
if (t === null || t === void 0 ? void 0 : t.description)
|
|
119
|
+
parts.push(String(t.description));
|
|
120
|
+
}
|
|
121
|
+
return parts.join(' ').trim();
|
|
122
|
+
}
|
|
123
|
+
function buildVariantText(variant) {
|
|
124
|
+
const parts = [];
|
|
125
|
+
for (const t of variant.translations || []) {
|
|
126
|
+
if (t === null || t === void 0 ? void 0 : t.name)
|
|
127
|
+
parts.push(String(t.name));
|
|
128
|
+
}
|
|
129
|
+
const sku = variant.sku;
|
|
130
|
+
if (sku)
|
|
131
|
+
parts.push(sku);
|
|
132
|
+
if (variant.product) {
|
|
133
|
+
for (const pt of variant.product
|
|
134
|
+
.translations || []) {
|
|
135
|
+
if (pt === null || pt === void 0 ? void 0 : pt.name)
|
|
136
|
+
parts.push(String(pt.name));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return parts.join(' ').trim();
|
|
140
|
+
}
|
|
141
|
+
function buildCollectionText(collection) {
|
|
142
|
+
const parts = [];
|
|
143
|
+
for (const t of collection.translations || []) {
|
|
144
|
+
if (t === null || t === void 0 ? void 0 : t.name)
|
|
145
|
+
parts.push(String(t.name));
|
|
146
|
+
if (t === null || t === void 0 ? void 0 : t.description)
|
|
147
|
+
parts.push(String(t.description));
|
|
148
|
+
}
|
|
149
|
+
return parts.join(' ').trim();
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=embed-all-job.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embed-all-job.service.js","sourceRoot":"","sources":["../../src/services/embed-all-job.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA0D;AAC1D,wCAQuB;AACvB,4CAAoD;AACpD,2DAAuD;AACvD,2EAAsE;AACtE,2EAAsE;AACtE,iFAA4E;AAI5E;;;GAGG;AAEI,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAG3B,YACY,eAAgC,EAChC,qBAA4C,EAC5C,UAAmC,EACnC,gBAAkC,EAClC,uBAAgD,EAChD,uBAAgD,EAChD,0BAAsD;QANtD,oBAAe,GAAf,eAAe,CAAiB;QAChC,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,eAAU,GAAV,UAAU,CAAyB;QACnC,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,4BAAuB,GAAvB,uBAAuB,CAAyB;QAChD,4BAAuB,GAAvB,uBAAuB,CAAyB;QAChD,+BAA0B,GAA1B,0BAA0B,CAA4B;IAC/D,CAAC;IAEJ,KAAK,CAAC,YAAY;QACd,IAAI,CAAC,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC;YACnD,IAAI,EAAE,gCAAoB;YAC1B,OAAO,EAAE,KAAK,IAAI,EAAE;gBAChB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC;oBAChD,OAAO,EAAE,OAAO;iBACnB,CAAC,CAAC;gBACH,MAAM,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YAChC,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,OAAO;QACT,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACxC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC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