@miniaxolotl/grove-server 1.8.1 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bootstrap.js +374 -1
- package/dist/bootstrap.js.map +4 -4
- package/dist/migrate.js +374 -1
- package/dist/migrate.js.map +4 -4
- package/dist/server.js +2363 -291
- package/dist/server.js.map +4 -4
- package/dist/telemetry-build.json +1 -1
- package/dist/worker.js +1693 -131
- package/dist/worker.js.map +4 -4
- package/package.json +1 -1
package/dist/migrate.js
CHANGED
|
@@ -409,6 +409,376 @@ var conversations = {
|
|
|
409
409
|
}
|
|
410
410
|
};
|
|
411
411
|
|
|
412
|
+
// lib/database/src/relationship-migration.ts
|
|
413
|
+
var relationships = {
|
|
414
|
+
name: "20261021_010_relationship_evidence",
|
|
415
|
+
async up(db2) {
|
|
416
|
+
await db2.raw(`
|
|
417
|
+
ALTER TABLE projects ADD COLUMN content_version integer NOT NULL DEFAULT 0;
|
|
418
|
+
ALTER TABLE memory_enrichments DROP CONSTRAINT memory_enrichments_kind_check;
|
|
419
|
+
ALTER TABLE memory_enrichments ADD CONSTRAINT memory_enrichments_kind_check CHECK (kind IN ('decision','organization','relationship_scan'));
|
|
420
|
+
CREATE TABLE notebook_profiles (
|
|
421
|
+
account_id uuid NOT NULL, project_id uuid NOT NULL, profile text NOT NULL,
|
|
422
|
+
content_version integer NOT NULL, expires_at timestamptz,
|
|
423
|
+
total integer NOT NULL DEFAULT 0, processed integer NOT NULL DEFAULT 0,
|
|
424
|
+
after_id uuid, complete boolean NOT NULL DEFAULT false, data jsonb NOT NULL DEFAULT '{}',
|
|
425
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
426
|
+
PRIMARY KEY(account_id, project_id, profile),
|
|
427
|
+
FOREIGN KEY(account_id, project_id) REFERENCES projects(account_id, id) ON DELETE CASCADE
|
|
428
|
+
);
|
|
429
|
+
`);
|
|
430
|
+
for (const [table, endpoint] of [
|
|
431
|
+
["memory_relationships", "memories"],
|
|
432
|
+
["notebook_relationships", "projects"]
|
|
433
|
+
]) {
|
|
434
|
+
await db2.raw(`
|
|
435
|
+
CREATE TABLE ${table} (
|
|
436
|
+
id uuid PRIMARY KEY DEFAULT gen_random_uuid(), account_id uuid NOT NULL,
|
|
437
|
+
left_id uuid NOT NULL, right_id uuid NOT NULL, profile text NOT NULL,
|
|
438
|
+
left_version integer NOT NULL, right_version integer NOT NULL,
|
|
439
|
+
revision text NOT NULL, assessment jsonb NOT NULL,
|
|
440
|
+
expires_at timestamptz, updated_at timestamptz NOT NULL DEFAULT now(),
|
|
441
|
+
dismissed_revision text, review_version integer NOT NULL DEFAULT 0,
|
|
442
|
+
CHECK (left_id < right_id), UNIQUE(account_id, left_id, right_id),
|
|
443
|
+
FOREIGN KEY(account_id, left_id) REFERENCES ${endpoint}(account_id, id) ON DELETE CASCADE,
|
|
444
|
+
FOREIGN KEY(account_id, right_id) REFERENCES ${endpoint}(account_id, id) ON DELETE CASCADE
|
|
445
|
+
);
|
|
446
|
+
CREATE INDEX ${table}_right ON ${table}(account_id, right_id);
|
|
447
|
+
`);
|
|
448
|
+
}
|
|
449
|
+
for (const table of [
|
|
450
|
+
"notebook_profiles",
|
|
451
|
+
"memory_relationships",
|
|
452
|
+
"notebook_relationships"
|
|
453
|
+
]) {
|
|
454
|
+
await db2.raw(`ALTER TABLE ?? ENABLE ROW LEVEL SECURITY`, [table]);
|
|
455
|
+
await db2.raw(`ALTER TABLE ?? FORCE ROW LEVEL SECURITY`, [table]);
|
|
456
|
+
await db2.raw(
|
|
457
|
+
`CREATE POLICY tenant ON ?? USING (account_id = NULLIF(current_setting('grove.account_id', true), '')::uuid) WITH CHECK (account_id = NULLIF(current_setting('grove.account_id', true), '')::uuid)`,
|
|
458
|
+
[table]
|
|
459
|
+
);
|
|
460
|
+
await db2.raw(
|
|
461
|
+
`GRANT SELECT, INSERT, UPDATE, DELETE ON ?? TO grove_runtime, grove_worker`,
|
|
462
|
+
[table]
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
await db2.raw(`
|
|
466
|
+
CREATE FUNCTION grove_notebook_content_changed() RETURNS trigger
|
|
467
|
+
LANGUAGE plpgsql SET search_path = pg_catalog, public AS $$
|
|
468
|
+
BEGIN
|
|
469
|
+
IF TG_OP = 'DELETE' THEN
|
|
470
|
+
DELETE FROM notebook_profiles WHERE account_id=OLD.account_id AND project_id=OLD.project_id;
|
|
471
|
+
UPDATE notebook_relationships SET assessment=jsonb_build_object('relationship','unrelated','reason','','basis','none','direction','both','evidence','[]'::jsonb,'coverage',assessment->'coverage','breadth','thread'), profile='rebuild-after-deletion'
|
|
472
|
+
WHERE account_id=OLD.account_id AND (left_id=OLD.project_id OR right_id=OLD.project_id
|
|
473
|
+
OR EXISTS (SELECT 1 FROM jsonb_array_elements(assessment->'evidence') e WHERE e->>'id'=OLD.id::text));
|
|
474
|
+
UPDATE projects SET content_version = content_version + 1 WHERE account_id = OLD.account_id AND id = OLD.project_id;
|
|
475
|
+
UPDATE accounts SET organization_due_at = now() WHERE id = OLD.account_id;
|
|
476
|
+
ELSE
|
|
477
|
+
IF TG_OP = 'UPDATE' AND OLD.project_id IS DISTINCT FROM NEW.project_id THEN
|
|
478
|
+
DELETE FROM notebook_profiles WHERE account_id=OLD.account_id AND project_id=OLD.project_id;
|
|
479
|
+
UPDATE projects SET content_version = content_version + 1 WHERE account_id = OLD.account_id AND id = OLD.project_id;
|
|
480
|
+
END IF;
|
|
481
|
+
DELETE FROM notebook_profiles WHERE account_id=NEW.account_id AND project_id=NEW.project_id;
|
|
482
|
+
UPDATE projects SET content_version = content_version + 1 WHERE account_id = NEW.account_id AND id = NEW.project_id;
|
|
483
|
+
UPDATE accounts SET organization_due_at = now() WHERE id = NEW.account_id;
|
|
484
|
+
END IF;
|
|
485
|
+
RETURN NULL;
|
|
486
|
+
END $$;
|
|
487
|
+
CREATE TRIGGER notebook_content_changed AFTER INSERT OR DELETE OR UPDATE OF content, version, project_id, status, expires_at ON memories
|
|
488
|
+
FOR EACH ROW EXECUTE FUNCTION grove_notebook_content_changed();
|
|
489
|
+
ALTER TABLE jobs DROP CONSTRAINT jobs_kind_check;
|
|
490
|
+
ALTER TABLE jobs ADD CONSTRAINT jobs_kind_check CHECK (kind IN ('embedding','decision','organization','brief','relationship_scan','note_relationship','notebook_profile','notebook_relationship'));
|
|
491
|
+
ALTER TABLE jobs DROP CONSTRAINT job_target;
|
|
492
|
+
ALTER TABLE jobs ADD CONSTRAINT job_target CHECK (
|
|
493
|
+
(kind IN ('brief','note_relationship','notebook_profile','notebook_relationship') AND memory_id IS NULL AND version IS NULL AND scope IS NOT NULL)
|
|
494
|
+
OR (kind IN ('embedding','decision','organization','relationship_scan') AND memory_id IS NOT NULL AND version IS NOT NULL)
|
|
495
|
+
);
|
|
496
|
+
CREATE OR REPLACE FUNCTION grove_claim_job(profiles text[] DEFAULT NULL, wanted uuid DEFAULT NULL) RETURNS SETOF jobs
|
|
497
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
|
498
|
+
BEGIN
|
|
499
|
+
UPDATE jobs SET failed_at = now(), error_code = 'lease_exhausted'
|
|
500
|
+
WHERE failed_at IS NULL AND attempts >= 5 AND (lease_until IS NULL OR lease_until < now());
|
|
501
|
+
RETURN QUERY UPDATE jobs SET lease_id = gen_random_uuid(), lease_until = now() + interval '120 seconds', attempts = attempts + 1
|
|
502
|
+
WHERE id = (SELECT j.id FROM jobs j JOIN accounts a ON a.id = j.account_id
|
|
503
|
+
WHERE a.status = 'active' AND j.failed_at IS NULL AND j.attempts < 5 AND j.due_at <= now()
|
|
504
|
+
AND (wanted IS NULL OR j.id = wanted) AND (profiles IS NULL OR j.profile = ANY(profiles))
|
|
505
|
+
AND (j.kind NOT IN ('decision','relationship_scan') OR NOT EXISTS (SELECT 1 FROM jobs e WHERE e.account_id = j.account_id AND e.target = j.target AND e.kind = 'embedding' AND e.failed_at IS NULL AND (profiles IS NULL OR e.profile = ANY(profiles))))
|
|
506
|
+
AND (j.kind <> 'notebook_profile' OR NOT EXISTS (SELECT 1 FROM jobs e JOIN memories m ON m.account_id=e.account_id AND m.id=e.memory_id WHERE e.account_id=j.account_id AND m.project_id::text=j.target AND e.kind='embedding' AND e.failed_at IS NULL AND (profiles IS NULL OR e.profile=ANY(profiles))))
|
|
507
|
+
AND (j.kind <> 'brief' OR NOT EXISTS (SELECT 1 FROM jobs e WHERE e.account_id = j.account_id AND e.kind IN ('embedding','decision','organization') AND e.failed_at IS NULL AND (profiles IS NULL OR e.profile = ANY(profiles))))
|
|
508
|
+
AND (j.lease_until IS NULL OR j.lease_until < now()) ORDER BY j.due_at, j.id
|
|
509
|
+
FOR UPDATE OF j SKIP LOCKED LIMIT 1) RETURNING *;
|
|
510
|
+
END $$;
|
|
511
|
+
CREATE OR REPLACE FUNCTION grove_reprocess(embedding_profile text, decision_profile text, generation_profile text) RETURNS void
|
|
512
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
|
513
|
+
BEGIN
|
|
514
|
+
DELETE FROM jobs;
|
|
515
|
+
DELETE FROM memory_briefs;
|
|
516
|
+
DELETE FROM notebook_profiles;
|
|
517
|
+
DELETE FROM memory_enrichments WHERE kind='relationship_scan';
|
|
518
|
+
UPDATE accounts SET organization_due_at=now();
|
|
519
|
+
INSERT INTO jobs(account_id, memory_id, version, target, kind, profile)
|
|
520
|
+
SELECT m.account_id, m.id, m.version, m.id::text, p.kind, p.profile
|
|
521
|
+
FROM memories m JOIN accounts a ON a.id=m.account_id
|
|
522
|
+
CROSS JOIN (VALUES ('embedding',embedding_profile),('decision',decision_profile),('organization',generation_profile)) AS p(kind,profile)
|
|
523
|
+
WHERE a.status='active' AND m.status='active' AND (m.expires_at IS NULL OR m.expires_at>now())
|
|
524
|
+
AND p.profile IS NOT NULL AND (p.kind='embedding' OR a.organize_enabled);
|
|
525
|
+
IF generation_profile IS NOT NULL THEN
|
|
526
|
+
INSERT INTO jobs(account_id,kind,target,profile,scope,due_at)
|
|
527
|
+
SELECT DISTINCT m.account_id,'brief',COALESCE(m.project_id::text,'personal'),generation_profile,jsonb_build_object('projectId',m.project_id),now()+interval '5 seconds'
|
|
528
|
+
FROM memories m JOIN accounts a ON a.id=m.account_id
|
|
529
|
+
WHERE a.status='active' AND a.summaries_enabled AND m.status='active' AND (m.expires_at IS NULL OR m.expires_at>now());
|
|
530
|
+
INSERT INTO jobs(account_id,kind,target,profile,scope,due_at)
|
|
531
|
+
SELECT m.account_id,'brief',COALESCE(m.project_id::text,'personal')||':task:'||m.task_id,generation_profile,jsonb_build_object('projectId',m.project_id,'taskId',m.task_id),now()+interval '5 seconds'
|
|
532
|
+
FROM memories m JOIN accounts a ON a.id=m.account_id
|
|
533
|
+
WHERE a.status='active' AND a.summaries_enabled AND m.status='active' AND m.kind='handoff' AND m.expires_at>now();
|
|
534
|
+
END IF;
|
|
535
|
+
END $$;
|
|
536
|
+
`);
|
|
537
|
+
},
|
|
538
|
+
async down() {
|
|
539
|
+
throw new Error("Use a forward migration");
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// lib/database/src/sharing-migration.ts
|
|
544
|
+
var publicSharing = {
|
|
545
|
+
name: "20261021_011_public_sharing",
|
|
546
|
+
async up(db2) {
|
|
547
|
+
await db2.raw(`
|
|
548
|
+
CREATE TABLE public_shares (
|
|
549
|
+
id uuid PRIMARY KEY, account_id uuid NOT NULL REFERENCES accounts(id),
|
|
550
|
+
memory_id uuid, project_id uuid, include_descendants boolean NOT NULL DEFAULT false,
|
|
551
|
+
token_digest text UNIQUE CHECK (token_digest ~ '^[0-9a-f]{64}$'),
|
|
552
|
+
token_ciphertext text, version integer NOT NULL DEFAULT 1 CHECK (version > 0),
|
|
553
|
+
revoked_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(),
|
|
554
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
555
|
+
UNIQUE(account_id, id), UNIQUE(account_id, memory_id), UNIQUE(account_id, project_id),
|
|
556
|
+
FOREIGN KEY(account_id, memory_id) REFERENCES memories(account_id, id) ON DELETE CASCADE,
|
|
557
|
+
FOREIGN KEY(account_id, project_id) REFERENCES projects(account_id, id) ON DELETE CASCADE,
|
|
558
|
+
CHECK (num_nonnulls(memory_id, project_id) = 1),
|
|
559
|
+
CHECK (memory_id IS NULL OR NOT include_descendants),
|
|
560
|
+
CHECK ((revoked_at IS NULL AND token_digest IS NOT NULL AND token_ciphertext IS NOT NULL)
|
|
561
|
+
OR (revoked_at IS NOT NULL AND token_digest IS NULL AND token_ciphertext IS NULL))
|
|
562
|
+
);
|
|
563
|
+
CREATE TABLE public_share_operations (
|
|
564
|
+
account_id uuid NOT NULL, id uuid NOT NULL, fingerprint text NOT NULL,
|
|
565
|
+
share_id uuid NOT NULL, version integer NOT NULL,
|
|
566
|
+
created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY(account_id, id),
|
|
567
|
+
FOREIGN KEY(account_id, share_id) REFERENCES public_shares(account_id, id) ON DELETE CASCADE
|
|
568
|
+
);
|
|
569
|
+
CREATE TABLE public_share_rates (
|
|
570
|
+
bucket text NOT NULL, minute timestamptz NOT NULL, count integer NOT NULL,
|
|
571
|
+
PRIMARY KEY(bucket, minute)
|
|
572
|
+
);
|
|
573
|
+
CREATE INDEX public_share_rates_expiry ON public_share_rates(minute);
|
|
574
|
+
REVOKE ALL ON public_share_rates FROM PUBLIC;
|
|
575
|
+
CREATE FUNCTION grove_share_cleanup() RETURNS trigger
|
|
576
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
|
|
577
|
+
BEGIN
|
|
578
|
+
DELETE FROM public_share_rates WHERE bucket = OLD.id::text;
|
|
579
|
+
RETURN OLD;
|
|
580
|
+
END $$;
|
|
581
|
+
REVOKE ALL ON FUNCTION grove_share_cleanup() FROM PUBLIC;
|
|
582
|
+
CREATE TRIGGER public_share_cleanup AFTER DELETE ON public_shares
|
|
583
|
+
FOR EACH ROW EXECUTE FUNCTION grove_share_cleanup();
|
|
584
|
+
CREATE FUNCTION grove_share_admit(bucket_name text) RETURNS boolean
|
|
585
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
|
|
586
|
+
DECLARE hits integer;
|
|
587
|
+
BEGIN
|
|
588
|
+
IF bucket_name <> 'global' AND bucket_name !~ '^[0-9a-f-]{36}$' THEN RETURN false; END IF;
|
|
589
|
+
DELETE FROM public_share_rates WHERE minute < date_trunc('minute', now()) - interval '1 minute';
|
|
590
|
+
INSERT INTO public_share_rates(bucket, minute, count)
|
|
591
|
+
VALUES(bucket_name, date_trunc('minute', now()), 1)
|
|
592
|
+
ON CONFLICT(bucket, minute) DO UPDATE SET count = LEAST(public_share_rates.count + 1, 1201)
|
|
593
|
+
RETURNING count INTO hits;
|
|
594
|
+
RETURN hits <= CASE WHEN bucket_name = 'global' THEN 1200 ELSE 240 END;
|
|
595
|
+
END $$;
|
|
596
|
+
REVOKE ALL ON FUNCTION grove_share_admit(text) FROM PUBLIC;
|
|
597
|
+
GRANT EXECUTE ON FUNCTION grove_share_admit(text) TO grove_runtime;
|
|
598
|
+
`);
|
|
599
|
+
for (const table of ["public_shares", "public_share_operations"]) {
|
|
600
|
+
await db2.raw("ALTER TABLE ?? ENABLE ROW LEVEL SECURITY", [table]);
|
|
601
|
+
await db2.raw(
|
|
602
|
+
`CREATE POLICY tenant ON ?? USING (account_id = NULLIF(current_setting('grove.account_id', true), '')::uuid)
|
|
603
|
+
WITH CHECK (account_id = NULLIF(current_setting('grove.account_id', true), '')::uuid)`,
|
|
604
|
+
[table]
|
|
605
|
+
);
|
|
606
|
+
await db2.raw(
|
|
607
|
+
"GRANT SELECT, INSERT, UPDATE, DELETE ON ?? TO grove_runtime",
|
|
608
|
+
[table]
|
|
609
|
+
);
|
|
610
|
+
await db2.raw("GRANT SELECT, DELETE ON ?? TO grove_worker", [table]);
|
|
611
|
+
}
|
|
612
|
+
await db2.raw(`
|
|
613
|
+
CREATE FUNCTION grove_public_share(
|
|
614
|
+
secret_digest text, requested_kind text, requested_id uuid,
|
|
615
|
+
after_id uuid, cursor_share uuid, cursor_version integer, page_size integer
|
|
616
|
+
) RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
|
|
617
|
+
DECLARE link public_shares; owner_id uuid; permitted uuid[]; selected uuid;
|
|
618
|
+
root_name text; notebook_name text; note memories; result jsonb; page jsonb; children jsonb;
|
|
619
|
+
BEGIN
|
|
620
|
+
IF page_size IS NULL OR page_size < 1 OR page_size > 100
|
|
621
|
+
OR requested_kind IS NULL OR requested_kind NOT IN ('root', 'note', 'notebook') THEN RETURN NULL; END IF;
|
|
622
|
+
SELECT account_id INTO owner_id FROM public_shares WHERE token_digest = secret_digest AND revoked_at IS NULL;
|
|
623
|
+
IF owner_id IS NULL THEN RETURN NULL; END IF;
|
|
624
|
+
-- Match the service's lock order. Revocation, moves and deletion all take
|
|
625
|
+
-- the account write lock, so a completed revocation precedes later reads.
|
|
626
|
+
PERFORM 1 FROM accounts WHERE id = owner_id AND status = 'active' FOR SHARE;
|
|
627
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
628
|
+
SELECT * INTO link FROM public_shares WHERE account_id = owner_id
|
|
629
|
+
AND token_digest = secret_digest AND revoked_at IS NULL FOR SHARE;
|
|
630
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
631
|
+
IF NOT grove_share_admit(link.id::text) THEN RETURN jsonb_build_object('limited', true); END IF;
|
|
632
|
+
IF cursor_share IS NOT NULL AND (cursor_share <> link.id OR cursor_version IS DISTINCT FROM link.version)
|
|
633
|
+
THEN RETURN jsonb_build_object('stale', true); END IF;
|
|
634
|
+
IF link.project_id IS NOT NULL THEN
|
|
635
|
+
WITH RECURSIVE scope(id) AS (
|
|
636
|
+
SELECT id FROM projects WHERE account_id = owner_id AND id = link.project_id
|
|
637
|
+
UNION
|
|
638
|
+
SELECT p.id FROM projects p JOIN scope s ON p.parent_id = s.id
|
|
639
|
+
WHERE p.account_id = owner_id AND link.include_descendants
|
|
640
|
+
) SELECT array_agg(id) INTO permitted FROM scope;
|
|
641
|
+
SELECT name INTO root_name FROM projects WHERE account_id = owner_id AND id = link.project_id;
|
|
642
|
+
IF root_name IS NULL THEN RETURN NULL; END IF;
|
|
643
|
+
ELSE
|
|
644
|
+
SELECT * INTO note FROM memories WHERE account_id = owner_id AND id = link.memory_id
|
|
645
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now());
|
|
646
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
647
|
+
root_name := left(regexp_replace(split_part(trim(note.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110);
|
|
648
|
+
END IF;
|
|
649
|
+
result := jsonb_build_object('shareId', link.id, 'shareVersion', link.version,
|
|
650
|
+
'kind', CASE WHEN link.memory_id IS NULL THEN 'notebook' ELSE 'note' END,
|
|
651
|
+
'root', jsonb_build_object('id', coalesce(link.project_id, link.memory_id), 'name', root_name),
|
|
652
|
+
'notebook', NULL, 'note', NULL, 'notebooks', '[]'::jsonb, 'notes', '[]'::jsonb);
|
|
653
|
+
IF requested_kind = 'note' OR (requested_kind = 'root' AND link.memory_id IS NOT NULL) THEN
|
|
654
|
+
selected := CASE WHEN requested_kind = 'root' THEN link.memory_id ELSE requested_id END;
|
|
655
|
+
SELECT * INTO note FROM memories WHERE account_id = owner_id AND id = selected
|
|
656
|
+
AND (id = link.memory_id OR project_id = ANY(permitted))
|
|
657
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now());
|
|
658
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
659
|
+
SELECT coalesce(jsonb_agg(m.id), '[]'::jsonb) INTO page FROM memories m
|
|
660
|
+
WHERE m.account_id = owner_id AND (m.id = link.memory_id OR m.project_id = ANY(permitted))
|
|
661
|
+
AND m.status = 'active' AND (m.expires_at IS NULL OR m.expires_at > now())
|
|
662
|
+
AND m.id IN (SELECT matches[1]::uuid FROM regexp_matches(note.content,
|
|
663
|
+
'/app/memories/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})', 'g') AS matches);
|
|
664
|
+
RETURN result || jsonb_build_object('note', jsonb_build_object('id', note.id,
|
|
665
|
+
'title', left(regexp_replace(split_part(trim(note.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110),
|
|
666
|
+
'content', note.content, 'updatedAt', note.updated_at, 'linkedNoteIds', page));
|
|
667
|
+
END IF;
|
|
668
|
+
selected := CASE WHEN requested_kind = 'root' THEN link.project_id ELSE requested_id END;
|
|
669
|
+
IF selected IS NULL OR NOT (selected = ANY(permitted)) OR permitted IS NULL THEN RETURN NULL; END IF;
|
|
670
|
+
SELECT name INTO notebook_name FROM projects WHERE account_id = owner_id AND id = selected;
|
|
671
|
+
SELECT coalesce(jsonb_agg(jsonb_build_object('id', p.id, 'name', p.name) ORDER BY p.name, p.id), '[]'::jsonb)
|
|
672
|
+
INTO children FROM projects p WHERE p.account_id = owner_id AND p.parent_id = selected AND p.id = ANY(permitted);
|
|
673
|
+
SELECT coalesce(jsonb_agg(jsonb_build_object('id', m.id,
|
|
674
|
+
'title', left(regexp_replace(split_part(trim(m.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110),
|
|
675
|
+
'updatedAt', m.updated_at) ORDER BY m.id), '[]'::jsonb) INTO page FROM (
|
|
676
|
+
SELECT id, content, updated_at FROM memories WHERE account_id = owner_id AND project_id = selected
|
|
677
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now())
|
|
678
|
+
AND (after_id IS NULL OR id > after_id) ORDER BY id LIMIT page_size + 1
|
|
679
|
+
) m;
|
|
680
|
+
RETURN result || jsonb_build_object('notebook', jsonb_build_object('id', selected, 'name', notebook_name),
|
|
681
|
+
'notebooks', children, 'notes', page);
|
|
682
|
+
END $$;
|
|
683
|
+
REVOKE ALL ON FUNCTION grove_public_share(text,text,uuid,uuid,uuid,integer,integer) FROM PUBLIC;
|
|
684
|
+
GRANT EXECUTE ON FUNCTION grove_public_share(text,text,uuid,uuid,uuid,integer,integer) TO grove_runtime;
|
|
685
|
+
`);
|
|
686
|
+
},
|
|
687
|
+
async down() {
|
|
688
|
+
throw new Error("Use a forward migration");
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
// lib/database/src/share-expiration-migration.ts
|
|
693
|
+
var shareExpiration = {
|
|
694
|
+
name: "20261022_012_share_expiration",
|
|
695
|
+
async up(db2) {
|
|
696
|
+
await db2.raw(`
|
|
697
|
+
ALTER TABLE public_shares
|
|
698
|
+
ADD COLUMN duration text CHECK (duration IN ('1h','4h','1d','1w','2w','1mo')),
|
|
699
|
+
ADD COLUMN expires_at timestamptz,
|
|
700
|
+
ADD CONSTRAINT public_shares_expiration CHECK ((duration IS NULL) = (expires_at IS NULL));
|
|
701
|
+
|
|
702
|
+
CREATE OR REPLACE FUNCTION grove_public_share(
|
|
703
|
+
secret_digest text, requested_kind text, requested_id uuid,
|
|
704
|
+
after_id uuid, cursor_share uuid, cursor_version integer, page_size integer
|
|
705
|
+
) RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
|
|
706
|
+
DECLARE link public_shares; owner_id uuid; permitted uuid[]; selected uuid;
|
|
707
|
+
root_name text; notebook_name text; note memories; result jsonb; page jsonb; children jsonb;
|
|
708
|
+
BEGIN
|
|
709
|
+
IF page_size IS NULL OR page_size < 1 OR page_size > 100
|
|
710
|
+
OR requested_kind IS NULL OR requested_kind NOT IN ('root', 'note', 'notebook') THEN RETURN NULL; END IF;
|
|
711
|
+
SELECT account_id INTO owner_id FROM public_shares WHERE token_digest = secret_digest AND revoked_at IS NULL;
|
|
712
|
+
IF owner_id IS NULL THEN RETURN NULL; END IF;
|
|
713
|
+
-- Keep account-first locking. Wall-clock expiry must be checked AFTER a
|
|
714
|
+
-- blocked reader acquires its locks, not against transaction-start now().
|
|
715
|
+
PERFORM 1 FROM accounts WHERE id = owner_id AND status = 'active' FOR SHARE;
|
|
716
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
717
|
+
SELECT * INTO link FROM public_shares WHERE account_id = owner_id
|
|
718
|
+
AND token_digest = secret_digest AND revoked_at IS NULL FOR SHARE;
|
|
719
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
720
|
+
IF link.expires_at IS NOT NULL AND link.expires_at <= clock_timestamp() THEN RETURN NULL; END IF;
|
|
721
|
+
IF NOT grove_share_admit(link.id::text) THEN RETURN jsonb_build_object('limited', true); END IF;
|
|
722
|
+
IF cursor_share IS NOT NULL AND (cursor_share <> link.id OR cursor_version IS DISTINCT FROM link.version)
|
|
723
|
+
THEN RETURN jsonb_build_object('stale', true); END IF;
|
|
724
|
+
IF link.project_id IS NOT NULL THEN
|
|
725
|
+
WITH RECURSIVE scope(id) AS (
|
|
726
|
+
SELECT id FROM projects WHERE account_id = owner_id AND id = link.project_id
|
|
727
|
+
UNION
|
|
728
|
+
SELECT p.id FROM projects p JOIN scope s ON p.parent_id = s.id
|
|
729
|
+
WHERE p.account_id = owner_id AND link.include_descendants
|
|
730
|
+
) SELECT array_agg(id) INTO permitted FROM scope;
|
|
731
|
+
SELECT name INTO root_name FROM projects WHERE account_id = owner_id AND id = link.project_id;
|
|
732
|
+
IF root_name IS NULL THEN RETURN NULL; END IF;
|
|
733
|
+
ELSE
|
|
734
|
+
SELECT * INTO note FROM memories WHERE account_id = owner_id AND id = link.memory_id
|
|
735
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now());
|
|
736
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
737
|
+
root_name := left(regexp_replace(split_part(trim(note.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110);
|
|
738
|
+
END IF;
|
|
739
|
+
result := jsonb_build_object('shareId', link.id, 'shareVersion', link.version,
|
|
740
|
+
'kind', CASE WHEN link.memory_id IS NULL THEN 'notebook' ELSE 'note' END,
|
|
741
|
+
'root', jsonb_build_object('id', coalesce(link.project_id, link.memory_id), 'name', root_name),
|
|
742
|
+
'notebook', NULL, 'note', NULL, 'notebooks', '[]'::jsonb, 'notes', '[]'::jsonb);
|
|
743
|
+
IF requested_kind = 'note' OR (requested_kind = 'root' AND link.memory_id IS NOT NULL) THEN
|
|
744
|
+
selected := CASE WHEN requested_kind = 'root' THEN link.memory_id ELSE requested_id END;
|
|
745
|
+
SELECT * INTO note FROM memories WHERE account_id = owner_id AND id = selected
|
|
746
|
+
AND (id = link.memory_id OR project_id = ANY(permitted))
|
|
747
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now());
|
|
748
|
+
IF NOT FOUND THEN RETURN NULL; END IF;
|
|
749
|
+
SELECT coalesce(jsonb_agg(m.id), '[]'::jsonb) INTO page FROM memories m
|
|
750
|
+
WHERE m.account_id = owner_id AND (m.id = link.memory_id OR m.project_id = ANY(permitted))
|
|
751
|
+
AND m.status = 'active' AND (m.expires_at IS NULL OR m.expires_at > now())
|
|
752
|
+
AND m.id IN (SELECT matches[1]::uuid FROM regexp_matches(note.content,
|
|
753
|
+
'/app/memories/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})', 'g') AS matches);
|
|
754
|
+
RETURN result || jsonb_build_object('note', jsonb_build_object('id', note.id,
|
|
755
|
+
'title', left(regexp_replace(split_part(trim(note.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110),
|
|
756
|
+
'content', note.content, 'updatedAt', note.updated_at, 'linkedNoteIds', page));
|
|
757
|
+
END IF;
|
|
758
|
+
selected := CASE WHEN requested_kind = 'root' THEN link.project_id ELSE requested_id END;
|
|
759
|
+
IF selected IS NULL OR NOT (selected = ANY(permitted)) OR permitted IS NULL THEN RETURN NULL; END IF;
|
|
760
|
+
SELECT name INTO notebook_name FROM projects WHERE account_id = owner_id AND id = selected;
|
|
761
|
+
SELECT coalesce(jsonb_agg(jsonb_build_object('id', p.id, 'name', p.name) ORDER BY p.name, p.id), '[]'::jsonb)
|
|
762
|
+
INTO children FROM projects p WHERE p.account_id = owner_id AND p.parent_id = selected AND p.id = ANY(permitted);
|
|
763
|
+
SELECT coalesce(jsonb_agg(jsonb_build_object('id', m.id,
|
|
764
|
+
'title', left(regexp_replace(split_part(trim(m.content), E'\\n', 1), '^#{1,6}\\s+', ''), 110),
|
|
765
|
+
'updatedAt', m.updated_at) ORDER BY m.id), '[]'::jsonb) INTO page FROM (
|
|
766
|
+
SELECT id, content, updated_at FROM memories WHERE account_id = owner_id AND project_id = selected
|
|
767
|
+
AND status = 'active' AND (expires_at IS NULL OR expires_at > now())
|
|
768
|
+
AND (after_id IS NULL OR id > after_id) ORDER BY id LIMIT page_size + 1
|
|
769
|
+
) m;
|
|
770
|
+
RETURN result || jsonb_build_object('notebook', jsonb_build_object('id', selected, 'name', notebook_name),
|
|
771
|
+
'notebooks', children, 'notes', page);
|
|
772
|
+
END $$;
|
|
773
|
+
REVOKE ALL ON FUNCTION grove_public_share(text,text,uuid,uuid,uuid,integer,integer) FROM PUBLIC;
|
|
774
|
+
GRANT EXECUTE ON FUNCTION grove_public_share(text,text,uuid,uuid,uuid,integer,integer) TO grove_runtime;
|
|
775
|
+
`);
|
|
776
|
+
},
|
|
777
|
+
async down() {
|
|
778
|
+
throw new Error("Use a forward migration");
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
|
|
412
782
|
// lib/database/src/migrations.ts
|
|
413
783
|
var migrations = [
|
|
414
784
|
{
|
|
@@ -620,7 +990,10 @@ var migrations = [
|
|
|
620
990
|
memoryLifetimes,
|
|
621
991
|
backgroundServices,
|
|
622
992
|
nestedNotebooks,
|
|
623
|
-
conversations
|
|
993
|
+
conversations,
|
|
994
|
+
relationships,
|
|
995
|
+
publicSharing,
|
|
996
|
+
shareExpiration
|
|
624
997
|
];
|
|
625
998
|
var migrationSource = {
|
|
626
999
|
getMigrations: async () => migrations,
|