API for chat project sharing (experiment `chat-projects`). Builds on #30479 (merged). Owners share a project with users, groups, or the whole organization (the Everyone group, ID = organization ID). Chats in a shared project stay private. | Role | Project and instructions | Memories | ACL | Delete | |---------|--------------------------|------------|------|--------| | `use` | read | read/write | read | no | | `admin` | read/write | read/write | edit | no | Only the owner, and administrators whose role grants delete, can delete a project. - **API:** `GET`/`PATCH .../chats/projects/{project}/acl`, same shapes as `/chats/{chat}/acl`, plus SDK methods. Project responses include the caller's `permissions` (`update`, `delete`, `share`); the UI hides edit and delete accordingly. - **Listing:** `GET /chats/projects` returns owned and shared projects. Removes `GetChatProjectsByOwnerID`. - **Chat access:** creating a chat in a project, and loading its memory and instructions on each turn, requires the chat owner to own the project or have an ACL grant (`chatd.ChatProjectUsableBy`). Role grants don't count. If the grant is removed, existing chats lose memory and instructions on their next turn. - **Validation:** users and groups must be in the project's organization; keys must be canonical UUIDs; callers can't change their own role; the owner can't be added. Migration `000624` drops a user's grants when they leave the organization, so rejoining doesn't restore access. - **Instructions:** readers who can't edit get 403 instead of 404, and the UI hides editing. - **Agent prompt:** memories are visible to and editable by sharees, so the agent is told not to save secrets or personal details. Known gap: project deletion still detaches chats, and a sharee's open sidebar keeps showing them until reload. #30683 replaces this. Not included: share notifications, `/acl/available`, sharing UI. <details> <summary>Decision log</summary> - `use` grants memory read/write over HTTP, because agents in a sharee's chats can already write memories. Making the agent read-only for sharees was the alternative (@f0ssel chose sharees changing memories through use). - An administrator with `share` can grant themselves access and then use the project. Accepted to keep access control simple (@f0ssel). </details> > Generated by Coder Agents.
105 lines
3.2 KiB
SQL
105 lines
3.2 KiB
SQL
-- name: InsertChatProject :one
|
|
INSERT INTO chat_projects (id, organization_id, owner_id, name, description, icon)
|
|
VALUES (
|
|
COALESCE(sqlc.narg('id')::uuid, gen_random_uuid()),
|
|
@organization_id::uuid,
|
|
@owner_id::uuid,
|
|
@name::text,
|
|
@description::text,
|
|
@icon::text
|
|
)
|
|
RETURNING *;
|
|
|
|
-- name: GetChatProjectByID :one
|
|
SELECT *
|
|
FROM chat_projects
|
|
WHERE id = @id::uuid;
|
|
|
|
-- name: GetChatProjectByIDForUpdate :one
|
|
SELECT *
|
|
FROM chat_projects
|
|
WHERE id = @id::uuid
|
|
FOR UPDATE;
|
|
|
|
-- name: GetChatProjectsOwnedOrSharedWithUserID :many
|
|
-- Entries match regardless of the actions they grant, so callers must
|
|
-- authorize each row.
|
|
SELECT chat_projects.*
|
|
FROM chat_projects
|
|
WHERE chat_projects.owner_id = @user_id::uuid
|
|
OR (
|
|
(
|
|
chat_projects.user_acl ? (@user_id::uuid)::text
|
|
OR chat_projects.group_acl ?| ARRAY(
|
|
SELECT group_members.group_id::text
|
|
FROM group_members
|
|
WHERE group_members.user_id = @user_id::uuid
|
|
UNION ALL
|
|
SELECT organization_members.organization_id::text
|
|
FROM organization_members
|
|
WHERE organization_members.user_id = @user_id::uuid
|
|
)
|
|
)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM organization_members
|
|
WHERE organization_members.user_id = @user_id::uuid
|
|
AND organization_members.organization_id = chat_projects.organization_id
|
|
)
|
|
)
|
|
ORDER BY lower(chat_projects.name), chat_projects.id;
|
|
|
|
-- name: IsChatProjectUsableByUserID :one
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM chat_projects
|
|
WHERE chat_projects.id = @project_id::uuid
|
|
AND (
|
|
chat_projects.owner_id = @user_id::uuid
|
|
OR (
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM organization_members
|
|
WHERE organization_members.user_id = @user_id::uuid
|
|
AND organization_members.organization_id = chat_projects.organization_id
|
|
)
|
|
AND (
|
|
chat_projects.user_acl -> (@user_id::uuid)::text -> 'permissions' ?| ARRAY['read', '*']
|
|
OR chat_projects.group_acl -> chat_projects.organization_id::text -> 'permissions' ?| ARRAY['read', '*']
|
|
OR EXISTS (
|
|
SELECT 1
|
|
FROM group_members
|
|
WHERE group_members.user_id = @user_id::uuid
|
|
AND chat_projects.group_acl -> group_members.group_id::text -> 'permissions' ?| ARRAY['read', '*']
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)::boolean;
|
|
|
|
-- name: UpdateChatProjectACLByID :exec
|
|
UPDATE chat_projects
|
|
SET
|
|
user_acl = @user_acl,
|
|
group_acl = @group_acl
|
|
WHERE id = @id::uuid;
|
|
|
|
-- name: UpdateChatProjectByID :one
|
|
UPDATE chat_projects
|
|
SET
|
|
name = @name::text,
|
|
description = @description::text,
|
|
icon = @icon::text,
|
|
updated_at = now()
|
|
WHERE id = @id::uuid
|
|
RETURNING *;
|
|
|
|
-- name: DeleteChatProjectByID :exec
|
|
DELETE FROM chat_projects
|
|
WHERE id = @id::uuid;
|
|
|
|
-- name: CountChatProjectsByOwnerID :one
|
|
SELECT COUNT(*)::bigint
|
|
FROM chat_projects
|
|
WHERE owner_id = @owner_id::uuid;
|