Skip to content

Commit edd755d

Browse files
skeptrunedevcdxker
authored andcommitted
bugfix(trieve-shopify-extension): improve product image handling and update types for webhook
1 parent 7d053ee commit edd755d

5 files changed

Lines changed: 59 additions & 37 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ clients/python-sdk/dist
9696
pdf2md/ch_migrations/chm.toml
9797
clients/trieve-shopify-extension/shopify.app.skept-trieve-extension-test.toml
9898
clients/trieve-shopify-extension/shopify.app.skept-laptop-trieve-extension.toml
99-
charts/charts/**/*.tgz
99+
charts/charts/**/*.tgz
100+
clients/trieve-shopify-extension/.env.skept-trieve-extension-test

clients/search-component/src/recommendations/use-recommendations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const useRecommendations = (config: RecommendationsConfig) => {
5050
{
5151
method: "POST",
5252
body: JSON.stringify({
53-
positive_group_tracking_ids: [config.productId],
53+
positive_group_tracking_ids: [config.productId.toString()],
5454
limit: config.maxResults,
5555
filters: config.filter,
5656
} satisfies GetRecommendedGroupsData["requestBody"]),

clients/trieve-shopify-extension/app/processors/getProducts.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ function createChunkFromProduct(
1515
baseUrl: string,
1616
crawlOptions: ExtendedCrawlOptions,
1717
): ChunkReqPayload {
18-
// Extract image URLs
19-
const imageUrls = product.media.nodes.map((media) => media.preview.image.url);
18+
const imageUrls: string[] = [
19+
...(product?.media?.nodes
20+
?.map((media) => media?.preview?.image?.url)
21+
.filter((url): url is string => !!url) ?? []),
22+
...(product?.images
23+
?.map((image) => image?.src)
24+
.filter((url): url is string => !!url) ?? []),
25+
];
2026

2127
// Handle text cleaning
2228
let productTitle = product.title || "";
@@ -73,6 +79,7 @@ function createChunkFromProduct(
7379
const semanticBoostPhrase = groupVariants ? variantTitle : productTitle;
7480
const fulltextBoostPhrase = groupVariants ? variantTitle : productTitle;
7581
const tags = product.tags;
82+
tags.push(...variantTitle.split(" / "));
7683

7784
if (crawlOptions.include_metafields) {
7885
product.variants.nodes.forEach((v) => {
@@ -84,15 +91,12 @@ function createChunkFromProduct(
8491
tags.push(...values);
8592
});
8693
}
87-
tags.push(...variantTitle.split(" / "));
8894
const metadata = {
8995
body_html: product.bodyHtml,
9096
variantName: variantTitle,
9197
handle: product.handle,
9298
id: parseInt(product.id.split("/").pop() || "0"),
93-
images: product.media.nodes.map((media) => ({
94-
src: media.preview.image.url,
95-
})),
99+
images: imageUrls,
96100
tags: product.tags,
97101
status: product.status,
98102
title: product.title,
@@ -147,8 +151,14 @@ export function createChunkFromProductWebhook(
147151
baseUrl: string,
148152
crawlOptions: ExtendedCrawlOptions,
149153
): ChunkReqPayload {
150-
// Extract image URLs
151-
const imageUrls = product.media.map((media) => media.preview.image.url);
154+
const imageUrls: string[] = [
155+
...(product?.media
156+
?.map((media) => media?.preview?.image?.url)
157+
.filter((url): url is string => !!url) ?? []),
158+
...(product?.images
159+
?.map((image) => image?.src)
160+
.filter((url): url is string => !!url) ?? []),
161+
];
152162

153163
// Handle text cleaning
154164
let productTitle = product.title || "";
@@ -176,14 +186,19 @@ export function createChunkFromProductWebhook(
176186
variant.title === "Default Title"
177187
? `<h1>${productTitle}</h1>${productBodyHtml}`
178188
: `<h1>${productTitle} - ${variantTitle}</h1>${productBodyHtml}`;
189+
let tags = product.tags;
190+
if (typeof tags === "string") {
191+
tags = tags.split(",").map((tag) => tag.trim());
192+
}
193+
tags.push(...variantTitle.split(" / "));
179194

180195
if (crawlOptions.scrape_options?.tag_regexes) {
181196
const tagMatches = new Set<string>();
182197

183198
crawlOptions.scrape_options.tag_regexes.forEach((pattern) => {
184199
try {
185200
const regex = new RegExp(pattern);
186-
product.tags.forEach((tag) => {
201+
tags.forEach((tag) => {
187202
if (regex.test(tag)) {
188203
tagMatches.add(`<span>${pattern}</span>`);
189204
}
@@ -204,7 +219,6 @@ export function createChunkFromProductWebhook(
204219
const semanticBoostPhrase = groupVariants ? variantTitle : productTitle;
205220
const fulltextBoostPhrase = groupVariants ? variantTitle : productTitle;
206221

207-
const tags = product.tags;
208222
if (crawlOptions.include_metafields) {
209223
product.variants.forEach((v) => {
210224
let values: string[] = JSON.parse(
@@ -242,9 +256,9 @@ export function createChunkFromProductWebhook(
242256
tag_set: tags,
243257
num_value: parseFloat(variant.price),
244258
metadata,
245-
tracking_id: groupVariants ? variant.id : product.id,
246-
group_tracking_ids: groupVariants ? [product.id] : undefined,
247-
image_urls: imageUrls,
259+
tracking_id: groupVariants ? variant.id.toString() : product.id.toString(),
260+
group_tracking_ids: groupVariants ? [product.id.toString()] : undefined,
261+
image_urls: imageUrls ?? [],
248262
fulltext_boost: {
249263
phrase: fulltextBoostPhrase,
250264
boost_factor: 1.3,
@@ -284,15 +298,17 @@ export async function sendChunksFromWebhook(
284298
if (response.error) {
285299
throw response.error;
286300
}
287-
let data = response.data as {
288-
data: {
289-
productVariant?: {
290-
metafields: { nodes: { key: string; value: string }[] };
301+
let data = (
302+
response as {
303+
data: {
304+
productVariant?: {
305+
metafields: { nodes: { key: string; value: string }[] };
306+
};
291307
};
292-
};
293-
};
308+
}
309+
).data;
294310

295-
variant.metafields = data?.data.productVariant?.metafields.nodes ?? [];
311+
variant.metafields = data?.productVariant?.metafields?.nodes ?? [];
296312
return createChunkFromProductWebhook(
297313
product,
298314
variant,
@@ -411,8 +427,8 @@ export const sendChunks = async (
411427
if (response.error) {
412428
throw response.error;
413429
}
414-
const dataChunks: ChunkReqPayload[] = response.data.products.nodes
415-
.flatMap((product) =>
430+
const dataChunks: ChunkReqPayload[] = response.data.products.nodes.flatMap(
431+
(product) =>
416432
product.variants.nodes.map((variant) =>
417433
createChunkFromProduct(
418434
product,
@@ -421,7 +437,7 @@ export const sendChunks = async (
421437
crawlOptions,
422438
),
423439
),
424-
);
440+
);
425441

426442
for (const batch of chunk_to_size(dataChunks, 120)) {
427443
const sendPromise = sendChunksToTrieve(batch, key, datasetId ?? "");

clients/trieve-shopify-extension/app/routes/webhooks.app.products.update.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { ExtendedCrawlOptions } from "app/components/DatasetSettings";
77
import { buildAdminApiFetcherForServer } from "app/loaders/serverLoader";
88

99
export const action = async ({ request }: ActionFunctionArgs) => {
10-
const { admin, payload, session, topic, shop } =
11-
await authenticate.webhook(request);
10+
const { payload, session, topic, shop } = await authenticate.webhook(request);
1211
console.log(`Received ${topic} webhook for ${shop}`);
1312

1413
const current = payload as ProductWebhook;

clients/trieve-shopify-extension/app/types.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ export type Product = {
4141
};
4242
}[];
4343
};
44-
media: {
45-
nodes: {
46-
preview: {
47-
image: {
48-
url: string;
44+
images?: {
45+
src?: string;
46+
}[];
47+
media?: {
48+
nodes?: {
49+
preview?: {
50+
image?: {
51+
url?: string;
4952
};
5053
};
5154
}[];
@@ -58,7 +61,7 @@ export type ProductWebhook = {
5861
product_type: string;
5962
body_html: string;
6063
handle: string;
61-
tags: string[];
64+
tags: string[] | string;
6265
category: {
6366
name: string;
6467
};
@@ -75,10 +78,13 @@ export type ProductWebhook = {
7578
value: string;
7679
}[];
7780
}[];
78-
media: {
79-
preview: {
80-
image: {
81-
url: string;
81+
images?: {
82+
src?: string;
83+
}[];
84+
media?: {
85+
preview?: {
86+
image?: {
87+
url?: string;
8288
};
8389
};
8490
}[];

0 commit comments

Comments
 (0)