Rewrite store-server.gbai: use WITH notation, use products table fields

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-01-14 12:23:03 -03:00
parent d6b9016564
commit fc2458c4c1
8 changed files with 51 additions and 72 deletions

View file

@ -5,8 +5,8 @@ IF LEN(existing) > 0 THEN
RETURN existing[0] RETURN existing[0]
END IF END IF
info = LLM "Extract product info: " + query + ". Return JSON {name, category, brand, description}" info = LLM "Extract product info: " + query + ". Return JSON with name, category, brand, description"
info.is_active = true
SAVE "products", info SAVE "products" WITH name AS info.name, category AS info.category, brand AS info.brand, description AS info.description, is_active AS true
RETURN info
RETURN FIND "products" WITH "name = '" + info.name + "'"

View file

@ -3,39 +3,26 @@ PARAM quantity AS INTEGER
product = PRODUCT product_id product = PRODUCT product_id
IF NOT product THEN IF NOT product THEN
RETURN {error: "Product not found"} RETURN WITH error AS "Product not found"
END IF END IF
IF NOT quantity THEN IF NOT quantity THEN
quantity = 1 quantity = 1
END IF END IF
dimensions = { IF NOT product.gross_weight THEN
length: product.length, dims = LLM "Estimate shipping dimensions for: " + product.name + ". Return JSON with length_cm, width_cm, height_cm, weight_kg"
width: product.width, UPDATE "products" WITH length AS dims.length_cm, width AS dims.width_cm, height AS dims.height_cm, gross_weight AS dims.weight_kg WHERE id = product_id
height: product.height, product = PRODUCT product_id
weight: product.gross_weight
}
IF NOT dimensions.weight THEN
dimensions = LLM "Estimate shipping dimensions for: " + product.name + ". Return JSON {length_cm, width_cm, height_cm, weight_kg}"
END IF END IF
volume = dimensions.length * dimensions.width * dimensions.height * quantity volume = product.length * product.width * product.height * quantity
volumetric_weight = volume / 5000 volumetric_weight = volume / 5000
billable = MAX(dimensions.weight * quantity, volumetric_weight) actual_weight = product.gross_weight * quantity
billable = MAX actual_weight, volumetric_weight
shipping = { ground_cost = 5.99 + billable * 1.5
product_id: product_id, express_cost = 12.99 + billable * 2.5
quantity: quantity, overnight_cost = 24.99 + billable * 4.0
dimensions: dimensions,
volume_cm3: volume,
billable_weight_kg: billable,
options: [
{carrier: "Ground", days: "5-7", cost: 5.99 + billable * 1.5},
{carrier: "Express", days: "2-3", cost: 12.99 + billable * 2.5},
{carrier: "Overnight", days: "1", cost: 24.99 + billable * 4.0}
]
}
RETURN shipping RETURN WITH product_id AS product_id, quantity AS quantity, length AS product.length, width AS product.width, height AS product.height, weight AS product.gross_weight, volume_cm3 AS volume, billable_weight_kg AS billable, ground_cost AS ground_cost, ground_days AS "5-7", express_cost AS express_cost, express_days AS "2-3", overnight_cost AS overnight_cost, overnight_days AS "1"

View file

@ -2,12 +2,12 @@ PARAM query AS STRING
categories = FIND "categories" categories = FIND "categories"
similar = SEARCH PRODUCTS query, 5 similar = SEARCH PRODUCTS query, 5
result = LLM "Classify '" + query + "' into: " + categories + ". Similar: " + similar + ". Return JSON {category_id, name, confidence, brand, type}" result = LLM "Classify '" + query + "' into: " + categories + ". Similar: " + similar + ". Return JSON with category_id, name, confidence, brand, type"
cached = FIND "product_cache", "query=" + query cached = FIND "products" WITH "name LIKE '%" + query + "%'"
IF cached THEN IF cached THEN
RETURN cached.result RETURN cached
END IF END IF
SAVE "product_cache", {query: query, result: result} SAVE "products" WITH name AS query, category AS result.name, brand AS result.brand, external_metadata AS result
RETURN result RETURN result

View file

@ -2,7 +2,7 @@ PARAM product_id AS STRING
product = PRODUCT product_id product = PRODUCT product_id
IF NOT product THEN IF NOT product THEN
RETURN {error: "Product not found"} RETURN WITH error AS "Product not found"
END IF END IF
IF product.description AND product.brand THEN IF product.description AND product.brand THEN
@ -21,15 +21,8 @@ FOR i = 0 TO 2
END IF END IF
NEXT NEXT
result = LLM "Analyze these product descriptions: " + enriched + ". Create best description for: " + product.name + ". Return JSON {description, brand, material, features}" result = LLM "Analyze these product descriptions: " + enriched + ". Create best description for: " + product.name + ". Return JSON with description, brand, material, features"
UPDATE "products", product_id, { UPDATE "products" WITH description AS result.description, brand AS result.brand, material AS result.material, external_metadata AS result WHERE id = product_id
description: result.description,
brand: result.brand,
material: result.material,
external_metadata: result
}
product.description = result.description RETURN PRODUCT product_id
product.brand = result.brand
RETURN product

View file

@ -2,13 +2,14 @@ PARAM product_id AS STRING
product = PRODUCT product_id product = PRODUCT product_id
IF NOT product THEN IF NOT product THEN
RETURN {error: "Product not found"} RETURN WITH error AS "Product not found"
END IF END IF
IF product.tax_code THEN IF product.tax_code THEN
RETURN {tax_code: product.tax_code, tax_class: product.tax_class} RETURN WITH tax_code AS product.tax_code, tax_class AS product.tax_class
END IF END IF
info = LLM "Get NCM/tax classification for: " + product.name + " " + product.category + ". Return JSON {tax_code, tax_class, description}" info = LLM "Get NCM/tax classification for: " + product.name + " " + product.category + ". Return JSON with tax_code, tax_class, description"
UPDATE "products", product_id, {tax_code: info.tax_code, tax_class: info.tax_class} UPDATE "products" WITH tax_code AS info.tax_code, tax_class AS info.tax_class WHERE id = product_id
RETURN info
RETURN WITH tax_code AS info.tax_code, tax_class AS info.tax_class, description AS info.description

View file

@ -2,29 +2,22 @@ PARAM image AS STRING
barcode = SCAN BARCODE image barcode = SCAN BARCODE image
IF NOT barcode THEN IF NOT barcode THEN
RETURN {error: "No barcode found"} RETURN WITH error AS "No barcode found"
END IF END IF
ean = barcode.data ean = barcode.data
cached = FIND "products", "global_trade_number=" + ean cached = FIND "products" WITH "global_trade_number = '" + ean + "'"
IF cached THEN IF cached THEN
RETURN cached RETURN cached
END IF END IF
data = GET "https://world.openfoodfacts.org/api/v0/product/" + ean + ".json" data = GET "https://world.openfoodfacts.org/api/v0/product/" + ean + ".json"
IF data.product THEN IF data.product THEN
product = { SAVE "products" WITH name AS data.product.product_name, brand AS data.product.brands, global_trade_number AS ean, description AS data.product.generic_name, category AS data.product.categories, net_weight AS data.product.quantity
name: data.product.product_name, RETURN FIND "products" WITH "global_trade_number = '" + ean + "'"
brand: data.product.brands,
global_trade_number: ean,
description: data.product.generic_name,
category: data.product.categories,
net_weight: data.product.quantity
}
ELSE
product = LLM "Search product info for EAN: " + ean + ". Return JSON {name, brand, description, category}"
product.global_trade_number = ean
END IF END IF
SAVE "products", product info = LLM "Search product info for EAN: " + ean + ". Return JSON with name, brand, description, category"
RETURN product SAVE "products" WITH name AS info.name, brand AS info.brand, description AS info.description, category AS info.category, global_trade_number AS ean
RETURN FIND "products" WITH "global_trade_number = '" + ean + "'"

View file

@ -7,6 +7,7 @@ IF LEN(similar) > 0 THEN
RETURN similar[0] RETURN similar[0]
END IF END IF
product = LLM "Extract product info from: " + description + ". Return JSON {name, brand, category, color, material}" product = LLM "Extract product info from: " + description + ". Return JSON with name, brand, category, color, material"
SAVE "products", product SAVE "products" WITH name AS product.name, brand AS product.brand, category AS product.category, color AS product.color, material AS product.material
RETURN product
RETURN FIND "products" WITH "name = '" + product.name + "'"

View file

@ -1,16 +1,20 @@
PARAM query AS STRING PARAM query AS STRING
cached = FIND "search_cache", "query=" + query cached = FIND "products" WITH "name ILIKE '%" + query + "%' OR description ILIKE '%" + query + "%'"
IF cached THEN IF cached THEN
RETURN cached.result RETURN cached
END IF END IF
result = SEARCH PRODUCTS query, 10 result = SEARCH PRODUCTS query, 10
IF LEN(result) = 0 THEN IF LEN(result) = 0 THEN
web = SCRAPE_ALL "https://www.google.com/search?q=" + query + "+product", ".g" web = SCRAPE_ALL "https://www.google.com/search?q=" + query + "+product", ".g"
result = LLM "Extract products from: " + web + ". Return JSON [{name, price, description}]" result = LLM "Extract products from: " + web + ". Return JSON array with name, price, description"
END IF END IF
enhanced = LLM "Add descriptions: " + result + ". Return JSON [{id, name, price, description, stock}]" enhanced = LLM "Add descriptions: " + result + ". Return JSON array with id, name, price, description, stock"
SAVE "search_cache", {query: query, result: enhanced}
FOR EACH item IN enhanced
SAVE "products" WITH name AS item.name, description AS item.description, price AS item.price, external_metadata AS item
NEXT
RETURN enhanced RETURN enhanced