@meuecommerce/frete-adapter-node 0.9.0 → 0.10.1
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.
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DIMENSION_PROMPT_VERSION = void 0;
|
|
4
4
|
exports.createOpenAIDimensionEstimator = createOpenAIDimensionEstimator;
|
|
5
|
+
const frete_1 = require("@meuecommerce/frete");
|
|
5
6
|
const openAIResponsesAgentRunner_js_1 = require("./openAIResponsesAgentRunner.js");
|
|
6
7
|
/**
|
|
7
8
|
* Bump whenever the prompt or the schema changes. It is written next to every
|
|
8
9
|
* `L3` record so a prompt change can invalidate stale estimates in background
|
|
9
10
|
* instead of at checkout.
|
|
10
11
|
*/
|
|
11
|
-
exports.DIMENSION_PROMPT_VERSION = "2026-09-11.responses.
|
|
12
|
+
exports.DIMENSION_PROMPT_VERSION = "2026-09-11.responses.v3-detalhes";
|
|
12
13
|
/**
|
|
13
14
|
* Strict JSON schema for the response.
|
|
14
15
|
*
|
|
@@ -53,6 +54,28 @@ const DIMENSIONS_SCHEMA = {
|
|
|
53
54
|
function largestBox(boxes) {
|
|
54
55
|
return boxes.reduce((largest, current) => current.width * current.height * current.length > largest.width * largest.height * largest.length ? current : largest);
|
|
55
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Uma entrada da lista numerada: a linha de identificação e, recuado embaixo
|
|
59
|
+
* dela, o contexto que a loja tiver mandado.
|
|
60
|
+
*
|
|
61
|
+
* A separação é o que faz a resposta voltar: a primeira linha é curta e é dela
|
|
62
|
+
* que sai o `name` que o modelo copia. Antes de `details` existir, quem tinha
|
|
63
|
+
* descrição e categoria para mandar empurrava tudo para dentro do `name`, a
|
|
64
|
+
* lista numerada virava um bloco de cinco linhas por item, e o modelo
|
|
65
|
+
* respondia o título limpo — correto, e impossível de casar com o que foi
|
|
66
|
+
* perguntado.
|
|
67
|
+
*/
|
|
68
|
+
function descreverProduto(p, i) {
|
|
69
|
+
const linha = `${i + 1}. ${p.name} - ${p.quantity}x (${p.weight}kg cada)`;
|
|
70
|
+
if (!p.details?.trim())
|
|
71
|
+
return linha;
|
|
72
|
+
const contexto = p.details
|
|
73
|
+
.trim()
|
|
74
|
+
.split("\n")
|
|
75
|
+
.map((l) => ` ${l.trim()}`)
|
|
76
|
+
.join("\n");
|
|
77
|
+
return `${linha}\n${contexto}`;
|
|
78
|
+
}
|
|
56
79
|
function createPrompt(products, boxes, samples) {
|
|
57
80
|
const box = largestBox(boxes);
|
|
58
81
|
const maxWidth = Math.floor(box.width * 0.8);
|
|
@@ -64,7 +87,7 @@ function createPrompt(products, boxes, samples) {
|
|
|
64
87
|
: `\n📋 INSTRUÇÕES (SEM EXEMPLOS):\n1. Considere a embalagem comercial típica brasileira\n2. Use conhecimento sobre produtos similares\n3. Ajuste baseado no peso\n`;
|
|
65
88
|
return (`Você é um especialista em dimensões de produtos brasileiros para e-commerce.\n\n` +
|
|
66
89
|
`🚨 LIMITES FÍSICOS OBRIGATÓRIOS:\n- Largura máxima: ${maxWidth}cm\n- Altura máxima: ${maxHeight}cm\n- Comprimento máximo: ${maxLength}cm\n(Maior caixa: ${box.name ?? "—"})\n\n` +
|
|
67
|
-
`📦 PRODUTOS PARA ESTIMAR:\n${products.map(
|
|
90
|
+
`📦 PRODUTOS PARA ESTIMAR:\n${products.map(descreverProduto).join("\n")}\n` +
|
|
68
91
|
samplesSection +
|
|
69
92
|
`\n🧱 COMO O PRODUTO OCUPA ESPAÇO (campo attrs, um por produto):\n` +
|
|
70
93
|
`- fragile: quebra se levar peso em cima — vidro, cerâmica, garrafa, ovo\n` +
|
|
@@ -72,7 +95,9 @@ function createPrompt(products, boxes, samples) {
|
|
|
72
95
|
`- nestable: unidades IGUAIS encaixam uma na outra — tigela, copo, cone, pote, vaso\n` +
|
|
73
96
|
`- keepUpright: precisa viajar em pé — líquido, tinta, bolo, planta\n` +
|
|
74
97
|
`Na dúvida responda false. Cada traço afrouxa o encaixe, e afrouxar errado faz o pacote não fechar.\n` +
|
|
75
|
-
`\n⚠️ REGRAS: respeite os limites;
|
|
98
|
+
`\n⚠️ REGRAS: respeite os limites; retorne length×width×height em cm; ` +
|
|
99
|
+
`copie em "name" o nome EXATO da linha numerada do produto, sem as linhas recuadas de contexto — ` +
|
|
100
|
+
`é por ele que a resposta é reconhecida, e um nome diferente descarta a estimativa.`);
|
|
76
101
|
}
|
|
77
102
|
/**
|
|
78
103
|
* Só os traços verdadeiros, e nada quando não há nenhum.
|
|
@@ -104,7 +129,10 @@ function validateAndOptimize(aiResult, products, boxes) {
|
|
|
104
129
|
if (height > maxHeight)
|
|
105
130
|
height = maxHeight;
|
|
106
131
|
const volume = length * width * height;
|
|
107
|
-
|
|
132
|
+
// Mesma normalização que liga a resposta de volta ao produto na memória.
|
|
133
|
+
// Comparando as strings cruas, um espaço a mais fazia o peso sumir e o
|
|
134
|
+
// piso de volume cair para o de um produto de 100 g.
|
|
135
|
+
const original = products.find((p) => (0, frete_1.productNameKey)(p.name) === (0, frete_1.productNameKey)(product.name));
|
|
108
136
|
const minVolume = (original?.weight || 0.1) * 500;
|
|
109
137
|
if (volume > 0 && volume < minVolume) {
|
|
110
138
|
const scale = Math.cbrt(minVolume / volume);
|
|
@@ -25,7 +25,7 @@ type FetchLike = typeof globalThis.fetch;
|
|
|
25
25
|
* `L3` record so a prompt change can invalidate stale estimates in background
|
|
26
26
|
* instead of at checkout.
|
|
27
27
|
*/
|
|
28
|
-
export declare const DIMENSION_PROMPT_VERSION = "2026-09-11.responses.
|
|
28
|
+
export declare const DIMENSION_PROMPT_VERSION = "2026-09-11.responses.v3-detalhes";
|
|
29
29
|
export interface OpenAIDimensionEstimatorOptions {
|
|
30
30
|
apiKey?: string;
|
|
31
31
|
fetch?: FetchLike;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { productNameKey } from "@meuecommerce/frete";
|
|
1
2
|
import { createOpenAIResponsesAgentRunner } from "./openAIResponsesAgentRunner.js";
|
|
2
3
|
/**
|
|
3
4
|
* Bump whenever the prompt or the schema changes. It is written next to every
|
|
4
5
|
* `L3` record so a prompt change can invalidate stale estimates in background
|
|
5
6
|
* instead of at checkout.
|
|
6
7
|
*/
|
|
7
|
-
export const DIMENSION_PROMPT_VERSION = "2026-09-11.responses.
|
|
8
|
+
export const DIMENSION_PROMPT_VERSION = "2026-09-11.responses.v3-detalhes";
|
|
8
9
|
/**
|
|
9
10
|
* Strict JSON schema for the response.
|
|
10
11
|
*
|
|
@@ -49,6 +50,28 @@ const DIMENSIONS_SCHEMA = {
|
|
|
49
50
|
function largestBox(boxes) {
|
|
50
51
|
return boxes.reduce((largest, current) => current.width * current.height * current.length > largest.width * largest.height * largest.length ? current : largest);
|
|
51
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Uma entrada da lista numerada: a linha de identificação e, recuado embaixo
|
|
55
|
+
* dela, o contexto que a loja tiver mandado.
|
|
56
|
+
*
|
|
57
|
+
* A separação é o que faz a resposta voltar: a primeira linha é curta e é dela
|
|
58
|
+
* que sai o `name` que o modelo copia. Antes de `details` existir, quem tinha
|
|
59
|
+
* descrição e categoria para mandar empurrava tudo para dentro do `name`, a
|
|
60
|
+
* lista numerada virava um bloco de cinco linhas por item, e o modelo
|
|
61
|
+
* respondia o título limpo — correto, e impossível de casar com o que foi
|
|
62
|
+
* perguntado.
|
|
63
|
+
*/
|
|
64
|
+
function descreverProduto(p, i) {
|
|
65
|
+
const linha = `${i + 1}. ${p.name} - ${p.quantity}x (${p.weight}kg cada)`;
|
|
66
|
+
if (!p.details?.trim())
|
|
67
|
+
return linha;
|
|
68
|
+
const contexto = p.details
|
|
69
|
+
.trim()
|
|
70
|
+
.split("\n")
|
|
71
|
+
.map((l) => ` ${l.trim()}`)
|
|
72
|
+
.join("\n");
|
|
73
|
+
return `${linha}\n${contexto}`;
|
|
74
|
+
}
|
|
52
75
|
function createPrompt(products, boxes, samples) {
|
|
53
76
|
const box = largestBox(boxes);
|
|
54
77
|
const maxWidth = Math.floor(box.width * 0.8);
|
|
@@ -60,7 +83,7 @@ function createPrompt(products, boxes, samples) {
|
|
|
60
83
|
: `\n📋 INSTRUÇÕES (SEM EXEMPLOS):\n1. Considere a embalagem comercial típica brasileira\n2. Use conhecimento sobre produtos similares\n3. Ajuste baseado no peso\n`;
|
|
61
84
|
return (`Você é um especialista em dimensões de produtos brasileiros para e-commerce.\n\n` +
|
|
62
85
|
`🚨 LIMITES FÍSICOS OBRIGATÓRIOS:\n- Largura máxima: ${maxWidth}cm\n- Altura máxima: ${maxHeight}cm\n- Comprimento máximo: ${maxLength}cm\n(Maior caixa: ${box.name ?? "—"})\n\n` +
|
|
63
|
-
`📦 PRODUTOS PARA ESTIMAR:\n${products.map(
|
|
86
|
+
`📦 PRODUTOS PARA ESTIMAR:\n${products.map(descreverProduto).join("\n")}\n` +
|
|
64
87
|
samplesSection +
|
|
65
88
|
`\n🧱 COMO O PRODUTO OCUPA ESPAÇO (campo attrs, um por produto):\n` +
|
|
66
89
|
`- fragile: quebra se levar peso em cima — vidro, cerâmica, garrafa, ovo\n` +
|
|
@@ -68,7 +91,9 @@ function createPrompt(products, boxes, samples) {
|
|
|
68
91
|
`- nestable: unidades IGUAIS encaixam uma na outra — tigela, copo, cone, pote, vaso\n` +
|
|
69
92
|
`- keepUpright: precisa viajar em pé — líquido, tinta, bolo, planta\n` +
|
|
70
93
|
`Na dúvida responda false. Cada traço afrouxa o encaixe, e afrouxar errado faz o pacote não fechar.\n` +
|
|
71
|
-
`\n⚠️ REGRAS: respeite os limites;
|
|
94
|
+
`\n⚠️ REGRAS: respeite os limites; retorne length×width×height em cm; ` +
|
|
95
|
+
`copie em "name" o nome EXATO da linha numerada do produto, sem as linhas recuadas de contexto — ` +
|
|
96
|
+
`é por ele que a resposta é reconhecida, e um nome diferente descarta a estimativa.`);
|
|
72
97
|
}
|
|
73
98
|
/**
|
|
74
99
|
* Só os traços verdadeiros, e nada quando não há nenhum.
|
|
@@ -100,7 +125,10 @@ function validateAndOptimize(aiResult, products, boxes) {
|
|
|
100
125
|
if (height > maxHeight)
|
|
101
126
|
height = maxHeight;
|
|
102
127
|
const volume = length * width * height;
|
|
103
|
-
|
|
128
|
+
// Mesma normalização que liga a resposta de volta ao produto na memória.
|
|
129
|
+
// Comparando as strings cruas, um espaço a mais fazia o peso sumir e o
|
|
130
|
+
// piso de volume cair para o de um produto de 100 g.
|
|
131
|
+
const original = products.find((p) => productNameKey(p.name) === productNameKey(product.name));
|
|
104
132
|
const minVolume = (original?.weight || 0.1) * 500;
|
|
105
133
|
if (volume > 0 && volume < minVolume) {
|
|
106
134
|
const scale = Math.cbrt(minVolume / volume);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meuecommerce/frete-adapter-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Node implementation of @meuecommerce/frete ports (Correios HTTP client) using global fetch. Used by the Shopify/Fly deployment and the MCP server.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@meuecommerce/frete": "^0.
|
|
34
|
+
"@meuecommerce/frete": "^0.10.1"
|
|
35
35
|
},
|
|
36
36
|
"module": "./dist/index.js",
|
|
37
37
|
"repository": {
|