Spaces:
Sleeping

File size: 2,669 Bytes
1cc47fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- Flint Phase 1 schema. Idempotent — safe to run multiple times.

-- pgvector extension
create extension if not exists vector;

-- Ideas table
create table if not exists ideas (
    id            uuid primary key default gen_random_uuid(),
    created_at    timestamptz default now(),
    raw_text      text not null,            -- original text (or transcription / photo description)
    summary       text,                     -- 3-word AI summary
    bucket        text,                     -- one of the 7 buckets
    source        text default 'text',      -- 'text' | 'voice' | 'photo'
    expanded_plan text,                     -- filled when /expand is used
    visited       boolean default false,    -- for brief tracking
    embedding     vector(384)               -- sentence-transformers all-MiniLM-L6-v2
);

-- Approximate-NN index for cosine similarity
create index if not exists ideas_embedding_idx
    on ideas using ivfflat (embedding vector_cosine_ops) with (lists = 100);

-- Cosine-similarity search, exposed as an RPC because the Supabase anon client
-- cannot run raw SQL. Called by storage.get_similar_ideas / storage.search_ideas.
create or replace function match_ideas(
    query_embedding vector(384),
    match_count int default 5,
    similarity_threshold float default 0.0
)
returns table (
    id uuid,
    raw_text text,
    summary text,
    bucket text,
    source text,
    created_at timestamptz,
    similarity float
)
language sql stable
as $$
    select
        i.id,
        i.raw_text,
        i.summary,
        i.bucket,
        i.source,
        i.created_at,
        1 - (i.embedding <=> query_embedding) as similarity
    from ideas i
    where i.embedding is not null
      and 1 - (i.embedding <=> query_embedding) >= similarity_threshold
    order by i.embedding <=> query_embedding
    limit match_count;
$$;

-- ---------------------------------------------------------------------------
-- API access. This app is solo-user with no auth, and the Supabase project's
-- "Automatically expose new tables" is off — so grant the Data API roles
-- (anon / authenticated) access to the table + search function explicitly,
-- and keep RLS off. Guarded so the same SQL still runs on a plain Postgres
-- (e.g. a local Docker pgvector) where these roles don't exist.
do $$
begin
  if exists (select 1 from pg_roles where rolname = 'anon') then
    grant usage on schema public to anon, authenticated;
    grant select, insert, update, delete on table ideas to anon, authenticated;
    grant execute on all functions in schema public to anon, authenticated;
  end if;
end $$;

alter table ideas disable row level security;