ux improved

This commit is contained in:
Lucas Tettamanti
2026-01-17 04:13:35 -03:00
parent 98e3d78e3d
commit 63b9ecef61
35 changed files with 4266 additions and 75 deletions

View File

@@ -65,7 +65,7 @@ export async function touchConversationState({ tenant_id, wa_chat_id }) {
on conflict (tenant_id, wa_chat_id)
do update set
updated_at = now()
returning tenant_id, wa_chat_id, state, last_intent, context, updated_at
returning tenant_id, wa_chat_id, state, last_intent, last_order_id, context, state_updated_at, updated_at, created_at
`;
const { rows } = await pool.query(q, [tenant_id, wa_chat_id]);
return rows[0] || null;
@@ -272,10 +272,16 @@ export async function getRunById({ tenant_id, run_id }) {
export async function getRecentMessagesForLLM({
tenant_id,
wa_chat_id,
limit = 20,
maxCharsPerMessage = 800,
}) {
const lim = Math.max(1, Math.min(50, parseInt(limit, 10) || 20));
const limRaw = parseInt(process.env.LIMIT_CONVERSATIONS || "", 10);
const maxCharsRaw = parseInt(process.env.MAX_CHARS_PER_MESSAGE || "", 10);
if (!Number.isFinite(limRaw) || limRaw <= 0) {
throw new Error("LIMIT_CONVERSATIONS env is required and must be a positive integer");
}
if (!Number.isFinite(maxCharsRaw) || maxCharsRaw <= 0) {
throw new Error("MAX_CHARS_PER_MESSAGE env is required and must be a positive integer");
}
const lim = Math.max(1, Math.min(50, limRaw));
const q = `
select direction, ts, text
from wa_messages
@@ -290,7 +296,7 @@ export async function getRecentMessagesForLLM({
return rows.reverse().map((r) => ({
role: r.direction === "in" ? "user" : "assistant",
content: String(r.text).trim().slice(0, maxCharsPerMessage),
content: String(r.text).trim().slice(0, maxCharsRaw),
}));
}
@@ -557,6 +563,28 @@ export async function searchProductAliases({ tenant_id, q = "", limit = 20 }) {
}));
}
export async function getRecoRules({ tenant_id }) {
const sql = `
select id, tenant_id, rule_key, trigger, queries, boosts, ask_slots, active, priority, created_at, updated_at
from product_reco_rules
where tenant_id=$1 and active=true
order by priority asc, id asc
`;
const { rows } = await pool.query(sql, [tenant_id]);
return rows;
}
export async function getRecoRuleByKey({ tenant_id, rule_key }) {
const sql = `
select id, tenant_id, rule_key, trigger, queries, boosts, ask_slots, active, priority, created_at, updated_at
from product_reco_rules
where tenant_id=$1 and rule_key=$2
limit 1
`;
const { rows } = await pool.query(sql, [tenant_id, rule_key]);
return rows[0] || null;
}
export async function getProductEmbedding({ tenant_id, content_hash }) {
const sql = `
select tenant_id, content_hash, content_text, embedding, model, updated_at