openai service and basic tables with migrations

This commit is contained in:
Lucas Tettamanti
2026-01-01 23:20:13 -03:00
parent 5c67b27859
commit 5e9cc8fe1a
9 changed files with 172 additions and 4 deletions

View File

@@ -0,0 +1,12 @@
-- migrate:up
create table wa_identity_map (
tenant_id uuid not null references tenants(id) on delete cascade,
wa_chat_id text not null,
woo_customer_id bigint not null,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
primary key (tenant_id, wa_chat_id)
);
-- migrate:down
drop table if exists wa_identity_map;

View File

@@ -0,0 +1,13 @@
-- migrate:up
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE tenants (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
key text NOT NULL UNIQUE,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
-- migrate:down
DROP TABLE IF EXISTS tenants;
DROP EXTENSION IF EXISTS pgcrypto;

View File

@@ -0,0 +1,23 @@
-- migrate:up
create table wa_conversation_state (
tenant_id uuid not null references tenants(id) on delete cascade,
wa_chat_id text not null,
state text not null, -- IDLE / BUILDING_ORDER / WAITING_PAYMENT
last_intent text null,
last_order_id bigint null,
context jsonb not null default '{}'::jsonb,
state_updated_at timestamptz not null default now(),
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
primary key (tenant_id, wa_chat_id)
);
create index idx_state_tenant_updated
on wa_conversation_state (tenant_id, updated_at desc);
-- migrate:down
drop table if exists wa_conversation_state;

View File

@@ -0,0 +1,24 @@
-- migrate:up
create table wa_messages (
id bigserial primary key,
tenant_id uuid not null references tenants(id) on delete cascade,
wa_chat_id text not null,
provider text not null, -- sim / evolution / twilio
message_id text not null, -- idempotencia por provider
direction text not null, -- in / out
ts timestamptz not null default now(),
text text null,
payload jsonb not null default '{}'::jsonb,
run_id uuid null,
unique (tenant_id, provider, message_id)
);
create index idx_msgs_tenant_chat_ts
on wa_messages (tenant_id, wa_chat_id, ts desc);
-- migrate:down
drop table if exists wa_messages;

View File

@@ -0,0 +1,32 @@
-- migrate:up
create table conversation_runs (
id uuid primary key default gen_random_uuid(),
tenant_id uuid not null references tenants(id) on delete cascade,
wa_chat_id text not null,
message_id text not null,
ts timestamptz not null default now(),
prev_state text null,
user_text text null,
llm_output jsonb null,
tools jsonb not null default '[]'::jsonb,
invariants jsonb not null default '{}'::jsonb,
final_reply text null,
order_id bigint null,
payment_link text null,
status text not null default 'ok', -- ok | warn | error
error_code text null,
error_detail text null,
latency_ms int null,
unique (tenant_id, message_id)
);
create index idx_runs_tenant_chat_ts
on conversation_runs (tenant_id, wa_chat_id, ts desc);
-- migrate:down
drop table if exists conversation_runs;