feat(tools): implement CRUD operations for thoughts and projects

* Add tools for creating, retrieving, updating, and deleting thoughts.
* Implement project management tools for creating and listing projects.
* Introduce linking functionality between thoughts.
* Add search and recall capabilities for thoughts based on semantic queries.
* Implement statistics and summarization tools for thought analysis.
* Create database migrations for thoughts, projects, and links.
* Add helper functions for UUID parsing and project resolution.
This commit is contained in:
Hein
2026-03-24 15:38:59 +02:00
parent 64024193e9
commit 66370a7f0e
68 changed files with 4422 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
create extension if not exists vector;
create extension if not exists pgcrypto;

View File

@@ -0,0 +1,17 @@
create table if not exists thoughts (
id uuid default gen_random_uuid() primary key,
content text not null,
embedding vector(1536),
metadata jsonb default '{}'::jsonb,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
create index if not exists thoughts_embedding_hnsw_idx
on thoughts using hnsw (embedding vector_cosine_ops);
create index if not exists thoughts_metadata_gin_idx
on thoughts using gin (metadata);
create index if not exists thoughts_created_at_idx
on thoughts (created_at desc);

View File

@@ -0,0 +1,13 @@
create table if not exists projects (
id uuid default gen_random_uuid() primary key,
name text not null unique,
description text,
created_at timestamptz default now(),
last_active_at timestamptz default now()
);
alter table thoughts add column if not exists project_id uuid references projects(id);
alter table thoughts add column if not exists archived_at timestamptz;
create index if not exists thoughts_project_id_idx on thoughts (project_id);
create index if not exists thoughts_archived_at_idx on thoughts (archived_at);

View File

@@ -0,0 +1,10 @@
create table if not exists thought_links (
from_id uuid references thoughts(id) on delete cascade,
to_id uuid references thoughts(id) on delete cascade,
relation text not null,
created_at timestamptz default now(),
primary key (from_id, to_id, relation)
);
create index if not exists thought_links_from_idx on thought_links (from_id);
create index if not exists thought_links_to_idx on thought_links (to_id);

View File

@@ -0,0 +1,31 @@
create or replace function match_thoughts(
query_embedding vector(1536),
match_threshold float default 0.7,
match_count int default 10,
filter jsonb default '{}'::jsonb
)
returns table (
id uuid,
content text,
metadata jsonb,
similarity float,
created_at timestamptz
)
language plpgsql
as $$
begin
return query
select
t.id,
t.content,
t.metadata,
1 - (t.embedding <=> query_embedding) as similarity,
t.created_at
from thoughts t
where 1 - (t.embedding <=> query_embedding) > match_threshold
and t.archived_at is null
and (filter = '{}'::jsonb or t.metadata @> filter)
order by t.embedding <=> query_embedding
limit match_count;
end;
$$;

View File

@@ -0,0 +1,5 @@
-- Grant these permissions to the database role used by the application.
-- Replace amcs_user with the actual role in your deployment before applying.
grant select, insert, update, delete on table public.thoughts to amcs_user;
grant select, insert, update, delete on table public.projects to amcs_user;
grant select, insert, update, delete on table public.thought_links to amcs_user;