openai service and basic tables with migrations
This commit is contained in:
12
db/migrations/20260102021529_wa_identity_map.sql
Normal file
12
db/migrations/20260102021529_wa_identity_map.sql
Normal 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;
|
||||
13
db/migrations/20260102021640_tenants.sql
Normal file
13
db/migrations/20260102021640_tenants.sql
Normal 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;
|
||||
23
db/migrations/20260102021749_wa_conversation_state.sql
Normal file
23
db/migrations/20260102021749_wa_conversation_state.sql
Normal 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;
|
||||
24
db/migrations/20260102021829_wa_messages.sql
Normal file
24
db/migrations/20260102021829_wa_messages.sql
Normal 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;
|
||||
32
db/migrations/20260102021907_conversation_runs.sql
Normal file
32
db/migrations/20260102021907_conversation_runs.sql
Normal 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;
|
||||
Reference in New Issue
Block a user