Spaces:
Running
Running
File size: 15,135 Bytes
64378fa cecfe55 64378fa cecfe55 64378fa cecfe55 64378fa cecfe55 64378fa cecfe55 64378fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | -- Explication — accounts, plans, models, shared wording and spend.
-- Paste this into Supabase → SQL Editor → New query → Run.
--
-- One address is the developer: lucas.owen13@icloud.com. Every write to plans,
-- models, API keys and page wording is restricted to that account here, in the
-- database, so the rule holds even if someone opens the developer page.
create extension if not exists pgcrypto;
-- ---------------------------------------------------------------------------
-- Who is the developer
-- ---------------------------------------------------------------------------
create or replace function public.explication_is_dev()
returns boolean
language sql
stable
as $$
select coalesce(lower(auth.jwt() ->> 'email'), '') = 'lucas.owen13@icloud.com'
$$;
-- ---------------------------------------------------------------------------
-- Tables
-- ---------------------------------------------------------------------------
create table if not exists public.explication_settings (
id integer primary key default 1 check (id = 1),
paypal_client_id text not null default '',
paypal_mode text not null default 'live' check (paypal_mode in ('live', 'sandbox')),
checkout_mode text not null default 'function' check (checkout_mode in ('payment_links', 'function')),
checkout_function text not null default 'paypal-checkout',
currency text not null default 'GBP',
support_email text not null default 'lucas.owen13@icloud.com',
updated_at timestamptz not null default now()
);
create table if not exists public.explication_plans (
id text primary key,
name text not null,
kind text not null check (kind in ('one_time', 'subscription')),
price_cents integer not null default 0,
currency text not null default 'GBP',
interval text check (interval is null or interval in ('month', 'year')),
blurb text not null default '',
features jsonb not null default '[]'::jsonb,
cost_limit_cents integer not null default 0,
download_tier integer not null default 0,
byok boolean not null default true,
paypal_plan_id text not null default '',
paypal_payment_link text not null default '',
active boolean not null default true,
sort integer not null default 0,
updated_at timestamptz not null default now()
);
create table if not exists public.explication_models (
id text primary key,
name text not null,
host text not null default '',
endpoint_url text not null default '',
model_name text not null default '',
cost_in_per_million numeric not null default 0,
cost_out_per_million numeric not null default 0,
min_tier integer not null default 0,
download_size text not null default '',
note text not null default '',
enabled boolean not null default true,
sort integer not null default 0,
updated_at timestamptz not null default now()
);
-- Hosted model keys. Read and write by the developer only; the AI function
-- reads them with the service role, so they never reach a browser.
create table if not exists public.explication_model_secrets (
model_id text primary key references public.explication_models(id) on delete cascade,
api_key text not null,
updated_at timestamptz not null default now()
);
-- Page wording overridden by the developer, by key.
create table if not exists public.explication_content (
key text primary key,
value text not null,
updated_at timestamptz not null default now(),
updated_by text
);
create table if not exists public.explication_entitlements (
id uuid primary key default gen_random_uuid(),
user_id uuid references auth.users(id) on delete cascade,
email text,
plan_id text references public.explication_plans(id) on delete set null,
source text not null default 'grant',
granted_at timestamptz not null default now(),
expires_at timestamptz,
revoked boolean not null default false,
paypal_payer_id text,
paypal_subscription_id text,
paypal_order_id text
);
create index if not exists explication_entitlements_user on public.explication_entitlements (user_id);
create index if not exists explication_entitlements_email on public.explication_entitlements (lower(email));
create unique index if not exists explication_entitlements_paypal_order
on public.explication_entitlements (paypal_order_id) where paypal_order_id is not null;
-- PayPal checkout state. Only the service role (the Edge Functions) can read or
-- write these; they never reach a browser.
create table if not exists public.explication_paypal_orders (
order_id text primary key,
user_id uuid references auth.users(id) on delete cascade,
plan_id text references public.explication_plans(id) on delete set null,
origin text not null default '',
kind text not null default 'one_time',
subscription_id text,
status text not null default 'created',
created_at timestamptz not null default now()
);
create table if not exists public.explication_paypal_billing_plans (
plan_id text not null references public.explication_plans(id) on delete cascade,
mode text not null,
paypal_product_id text,
paypal_plan_id text not null,
updated_at timestamptz not null default now(),
primary key (plan_id, mode)
);
create table if not exists public.explication_paypal_payments (
sale_id text primary key,
subscription_id text,
plan_id text,
amount_cents integer not null default 0,
created_at timestamptz not null default now()
);
create table if not exists public.explication_usage (
id uuid primary key default gen_random_uuid(),
user_id uuid references auth.users(id) on delete cascade,
model_id text,
cost_cents numeric not null default 0,
tokens_in integer not null default 0,
tokens_out integer not null default 0,
created_at timestamptz not null default now()
);
create index if not exists explication_usage_user on public.explication_usage (user_id, created_at desc);
-- ---------------------------------------------------------------------------
-- Row-level security
-- ---------------------------------------------------------------------------
alter table public.explication_settings enable row level security;
alter table public.explication_plans enable row level security;
alter table public.explication_models enable row level security;
alter table public.explication_model_secrets enable row level security;
alter table public.explication_content enable row level security;
alter table public.explication_entitlements enable row level security;
alter table public.explication_usage enable row level security;
alter table public.explication_paypal_orders enable row level security;
alter table public.explication_paypal_billing_plans enable row level security;
alter table public.explication_paypal_payments enable row level security;
drop policy if exists settings_read on public.explication_settings;
create policy settings_read on public.explication_settings for select using (true);
drop policy if exists settings_write on public.explication_settings;
create policy settings_write on public.explication_settings for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists plans_read on public.explication_plans;
create policy plans_read on public.explication_plans for select using (active or public.explication_is_dev());
drop policy if exists plans_write on public.explication_plans;
create policy plans_write on public.explication_plans for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists models_read on public.explication_models;
create policy models_read on public.explication_models for select using (enabled or public.explication_is_dev());
drop policy if exists models_write on public.explication_models;
create policy models_write on public.explication_models for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists secrets_dev on public.explication_model_secrets;
create policy secrets_dev on public.explication_model_secrets for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists content_read on public.explication_content;
create policy content_read on public.explication_content for select using (true);
drop policy if exists content_write on public.explication_content;
create policy content_write on public.explication_content for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists entitlements_read on public.explication_entitlements;
create policy entitlements_read on public.explication_entitlements for select using (
public.explication_is_dev()
or user_id = auth.uid()
or lower(coalesce(email, '')) = lower(coalesce(auth.jwt() ->> 'email', ''))
);
drop policy if exists entitlements_write on public.explication_entitlements;
create policy entitlements_write on public.explication_entitlements for all
using (public.explication_is_dev()) with check (public.explication_is_dev());
drop policy if exists usage_read on public.explication_usage;
create policy usage_read on public.explication_usage for select using (
public.explication_is_dev() or user_id = auth.uid()
);
-- Usage rows are written only by the AI function with the service role,
-- which bypasses row-level security. No insert policy on purpose.
-- ---------------------------------------------------------------------------
-- Seed: settings, plans and models
-- ---------------------------------------------------------------------------
insert into public.explication_settings (id) values (1) on conflict (id) do nothing;
insert into public.explication_plans
(id, name, kind, price_cents, currency, interval, blurb, features, cost_limit_cents, download_tier, byok, sort)
values
('paragraph-lite', 'Paragraph Lite', 'one_time', 400, 'GBP', null,
'Buy the smallest model once and keep it. Nothing you do here costs the site another penny.',
'["Paragraph Lite model, yours to keep","Download it and run it offline","All rule-based marking and the process log","Bring your own API key for any hosted model"]'::jsonb,
0, 1, true, 1),
('student', 'Student', 'subscription', 400, 'GBP', 'month',
'Every model hosted here, with £1.20 of model usage included each month.',
'["Every hosted model, including 3B and 7B","£1.20 of model usage per month","Download Lite and 1.5B for offline use","Bring your own key and pay your provider directly"]'::jsonb,
120, 2, true, 2),
('scholar', 'Scholar', 'subscription', 900, 'GBP', 'month',
'More room for long essays, with £3.50 of model usage each month.',
'["Everything in Student","£3.50 of model usage per month","Download the 3B model as well","Priority when the hosted models are busy"]'::jsonb,
350, 3, true, 3),
('examiner', 'Examiner', 'subscription', 1900, 'GBP', 'month',
'The whole catalogue, £9 of model usage a month, and the best offline model.',
'["Everything in Scholar","£9.00 of model usage per month","Download all four models, including 7B","Early access to new models and marking prompts"]'::jsonb,
900, 4, true, 4)
on conflict (id) do nothing;
insert into public.explication_models
(id, name, host, endpoint_url, model_name, cost_in_per_million, cost_out_per_million, min_tier, download_size, note, sort)
values
('Qwen2.5-0.5B-Instruct-q4f16_1-MLC', 'Paragraph Lite', 'Browser (WebGPU)', '', 'Qwen2.5 0.5B Instruct', 0, 0, 1, '~0.4 GB', 'Quickest to load. Enough for short feedback.', 1),
('Qwen2.5-1.5B-Instruct-q4f16_1-MLC', 'Qwen2.5 1.5B', 'Browser (WebGPU)', '', 'Qwen2.5 1.5B Instruct', 0, 0, 2, '~1.1 GB', 'The default when WebGPU is available.', 2),
('Qwen2.5-3B-Instruct-q4f16_1-MLC', 'Qwen2.5 3B', 'Browser (WebGPU)', '', 'Qwen2.5 3B Instruct', 0, 0, 3, '~2.0 GB', 'Better analysis; needs a stronger GPU.', 3),
('Qwen2.5-7B-Instruct-q4f16_1-MLC', 'Qwen2.5 7B', 'Browser (WebGPU)', '', 'Qwen2.5 7B Instruct', 0, 0, 4, '~4.5 GB', 'Best feedback here, but only on a capable GPU.', 4)
on conflict (id) do nothing;
-- ---------------------------------------------------------------------------
-- The current offer. Safe to run again: it updates the shop front in place.
-- ---------------------------------------------------------------------------
insert into public.explication_plans
(id, name, kind, price_cents, currency, interval, blurb, features, cost_limit_cents, download_tier, byok, sort, active)
values
('paragraph-lite', 'Paragraph Lite', 'one_time', 400, 'GBP', null,
'The little model that started it all, yours to keep. Download it once and mark paragraphs on the bus, on the train, anywhere.',
'["Paragraph Lite model, yours for good","Runs fully offline after one download","Every marking tool, forever","Use your own AI key if you have one"]'::jsonb,
0, 1, true, 1, true),
('model-library', 'Model Library', 'one_time', 1400, 'GBP', null,
'All four models in one go: the quick one, the strong one, and the two in between. Nothing else to buy.',
'["All four offline models unlocked","Download on every device you own","Works on a plane, offline, forever","Free updates when new models arrive"]'::jsonb,
0, 4, true, 2, true),
('exam-season', 'Exam Season', 'one_time', 2400, 'GBP', null,
'Everything unlocked for the run-up to the exams, including a pot of hosted AI for the nights you want the big models.',
'["All four offline models","Priority access to the hosted models","One payment, nothing renews"]'::jsonb,
600, 4, true, 3, true),
('ai-booster', 'AI Booster', 'one_time', 600, 'GBP', null,
'Already have a membership? Drop another pot of hosted AI into your account and keep the big models coming.',
'["Stacks with any plan you already have","Never expires","No subscription, no renewal"]'::jsonb,
500, 0, true, 4, true),
('student', 'Student', 'subscription', 400, 'GBP', 'month',
'The whole shelf of hosted models, plus two offline downloads. About the price of a coffee a month.',
'["Every hosted model, including the big ones","Download Lite and 1.5B for offline use","Use your own key and keep it free of limits"]'::jsonb,
120, 2, true, 5, true),
('scholar', 'Scholar', 'subscription', 900, 'GBP', 'month',
'For long essays and heavy revision weeks: more AI, more downloads, and the 3B model offline.',
'["Everything in Student","Download the 3B model as well","Priority when the hosted models are busy"]'::jsonb,
400, 3, true, 6, true),
('examiner', 'Examiner', 'subscription', 1900, 'GBP', 'month',
'The full shelf, the biggest budget and every model offline. For the year it really matters.',
'["Everything in Scholar","Download all four models, including 7B","Early access to new models and marking prompts"]'::jsonb,
1000, 4, true, 7, true)
on conflict (id) do update set
name = excluded.name,
kind = excluded.kind,
price_cents = excluded.price_cents,
interval = excluded.interval,
blurb = excluded.blurb,
features = excluded.features,
cost_limit_cents = excluded.cost_limit_cents,
download_tier = excluded.download_tier,
byok = excluded.byok,
sort = excluded.sort,
active = excluded.active,
updated_at = now();
|