@miniaxolotl/grove-server 1.11.0 → 1.11.2
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 +466 -1
- package/dist/bootstrap.js.map +4 -4
- package/dist/migrate.js +466 -1
- package/dist/migrate.js.map +4 -4
- package/dist/server.js +33282 -21372
- package/dist/server.js.map +4 -4
- package/dist/telemetry-build.json +1 -1
- package/dist/worker.js +12620 -1593
- package/dist/worker.js.map +4 -4
- package/package.json +4 -1
package/dist/migrate.js
CHANGED
|
@@ -4,6 +4,77 @@ import { createRequire as __groveCreateRequire } from "node:module"; const requi
|
|
|
4
4
|
import knex from "knex";
|
|
5
5
|
import { Model, knexSnakeCaseMappers } from "objection";
|
|
6
6
|
|
|
7
|
+
// lib/database/src/billing-migration.ts
|
|
8
|
+
var billing = {
|
|
9
|
+
name: "20261024_023_billing",
|
|
10
|
+
async up(db2) {
|
|
11
|
+
await db2.raw(`
|
|
12
|
+
CREATE TABLE billing_customers (
|
|
13
|
+
account_id uuid PRIMARY KEY REFERENCES accounts(id), customer_id text UNIQUE,
|
|
14
|
+
checkout_operation uuid NOT NULL DEFAULT gen_random_uuid(),
|
|
15
|
+
checkout_id text, checkout_price text,
|
|
16
|
+
UNIQUE(account_id, customer_id)
|
|
17
|
+
);
|
|
18
|
+
CREATE TABLE billing_subscriptions (
|
|
19
|
+
id text PRIMARY KEY, account_id uuid NOT NULL, customer_id text NOT NULL,
|
|
20
|
+
price_id text NOT NULL, product_id text, status text NOT NULL,
|
|
21
|
+
period_end timestamptz NOT NULL, cancel_at_period_end boolean NOT NULL DEFAULT false,
|
|
22
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
23
|
+
FOREIGN KEY(account_id,customer_id) REFERENCES billing_customers(account_id,customer_id)
|
|
24
|
+
);
|
|
25
|
+
CREATE INDEX billing_subscriptions_account ON billing_subscriptions(account_id,period_end);
|
|
26
|
+
CREATE TABLE billing_events (
|
|
27
|
+
id text PRIMARY KEY, account_id uuid NOT NULL REFERENCES accounts(id),
|
|
28
|
+
received_at timestamptz NOT NULL DEFAULT now()
|
|
29
|
+
);
|
|
30
|
+
`);
|
|
31
|
+
for (const table of [
|
|
32
|
+
"billing_customers",
|
|
33
|
+
"billing_subscriptions",
|
|
34
|
+
"billing_events"
|
|
35
|
+
]) {
|
|
36
|
+
await db2.raw("ALTER TABLE ?? ENABLE ROW LEVEL SECURITY", [table]);
|
|
37
|
+
if (table !== "billing_customers")
|
|
38
|
+
await db2.raw("ALTER TABLE ?? FORCE ROW LEVEL SECURITY", [table]);
|
|
39
|
+
await db2.raw(
|
|
40
|
+
`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)`,
|
|
41
|
+
[table]
|
|
42
|
+
);
|
|
43
|
+
await db2.raw("GRANT SELECT, INSERT, UPDATE ON ?? TO grove_runtime", [
|
|
44
|
+
table
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
await db2.raw(`
|
|
48
|
+
GRANT SELECT ON billing_subscriptions, billing_customers TO grove_worker;
|
|
49
|
+
CREATE FUNCTION grove_billing_account(customer text) RETURNS uuid
|
|
50
|
+
LANGUAGE sql SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
51
|
+
SELECT account_id FROM public.billing_customers WHERE customer_id=customer
|
|
52
|
+
$$;
|
|
53
|
+
REVOKE ALL ON FUNCTION grove_billing_account(text) FROM PUBLIC;
|
|
54
|
+
GRANT EXECUTE ON FUNCTION grove_billing_account(text) TO grove_runtime;
|
|
55
|
+
`);
|
|
56
|
+
},
|
|
57
|
+
async down() {
|
|
58
|
+
throw new Error("Use a forward migration");
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// lib/database/src/speech-voices-migration.ts
|
|
63
|
+
var speechVoices = {
|
|
64
|
+
name: "20261024_022_speech_voices",
|
|
65
|
+
async up(db2) {
|
|
66
|
+
await db2.raw(`
|
|
67
|
+
ALTER TABLE accounts ADD COLUMN read_aloud_voice_id text NOT NULL DEFAULT 'reed'
|
|
68
|
+
CHECK (read_aloud_voice_id IN ('reed','willow','cedar','brook'));
|
|
69
|
+
ALTER TABLE speech_configuration ADD COLUMN synthesis_profiles jsonb NOT NULL DEFAULT '{}'
|
|
70
|
+
CHECK (jsonb_typeof(synthesis_profiles) = 'object');
|
|
71
|
+
`);
|
|
72
|
+
},
|
|
73
|
+
async down() {
|
|
74
|
+
throw new Error("Use a forward migration");
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
7
78
|
// lib/database/src/hardening.ts
|
|
8
79
|
var hardening = {
|
|
9
80
|
name: "20261018_002_lifecycle_bounds",
|
|
@@ -1704,6 +1775,392 @@ var notebookAssistance = {
|
|
|
1704
1775
|
}
|
|
1705
1776
|
};
|
|
1706
1777
|
|
|
1778
|
+
// lib/database/src/notebook-lifecycle-migration.ts
|
|
1779
|
+
var notebookLifecycle = {
|
|
1780
|
+
name: "20261023_016_notebook_lifecycle",
|
|
1781
|
+
async up(db2) {
|
|
1782
|
+
await db2.raw(`
|
|
1783
|
+
ALTER TABLE projects ADD COLUMN status text NOT NULL DEFAULT 'active'
|
|
1784
|
+
CHECK(status IN ('active','archived')), ADD COLUMN archive_operation_id uuid;
|
|
1785
|
+
ALTER TABLE memories ADD COLUMN archive_operation_id uuid;
|
|
1786
|
+
DROP TRIGGER assistance_catalog_changed ON projects;
|
|
1787
|
+
CREATE TRIGGER assistance_catalog_changed AFTER INSERT OR DELETE OR UPDATE OF name,parent_id,status,deleted_at
|
|
1788
|
+
ON projects FOR EACH ROW EXECUTE FUNCTION grove_catalog_changed();
|
|
1789
|
+
|
|
1790
|
+
-- Defense in depth for every writer, including imports, agents and workers.
|
|
1791
|
+
-- Soft deletion/cleanup remains allowed; no authored rows are removed.
|
|
1792
|
+
CREATE FUNCTION grove_notebook_writable() RETURNS trigger LANGUAGE plpgsql
|
|
1793
|
+
SET search_path=pg_catalog,public,pg_temp AS $$
|
|
1794
|
+
BEGIN
|
|
1795
|
+
IF NEW.deleted_at IS NOT NULL THEN RETURN NEW; END IF;
|
|
1796
|
+
IF TG_TABLE_NAME='memories' THEN
|
|
1797
|
+
IF (NEW.project_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM projects
|
|
1798
|
+
WHERE account_id=NEW.account_id AND id=NEW.project_id AND deleted_at IS NULL AND status='active'))
|
|
1799
|
+
OR (TG_OP='UPDATE' AND OLD.project_id IS NOT NULL AND EXISTS(SELECT 1 FROM projects
|
|
1800
|
+
WHERE account_id=OLD.account_id AND id=OLD.project_id AND status='archived'))
|
|
1801
|
+
THEN RAISE EXCEPTION USING ERRCODE='GNB01',MESSAGE='notebook_inactive'; END IF;
|
|
1802
|
+
ELSE
|
|
1803
|
+
IF TG_OP='UPDATE' AND OLD.status='archived'
|
|
1804
|
+
AND (OLD.name IS DISTINCT FROM NEW.name OR OLD.parent_id IS DISTINCT FROM NEW.parent_id)
|
|
1805
|
+
THEN RAISE EXCEPTION USING ERRCODE='GNB01',MESSAGE='notebook_inactive'; END IF;
|
|
1806
|
+
IF NEW.parent_id IS NOT NULL AND (TG_OP='INSERT' OR OLD.parent_id IS DISTINCT FROM NEW.parent_id)
|
|
1807
|
+
AND NOT EXISTS(SELECT 1 FROM projects WHERE account_id=NEW.account_id
|
|
1808
|
+
AND id=NEW.parent_id AND deleted_at IS NULL AND status='active')
|
|
1809
|
+
THEN RAISE EXCEPTION USING ERRCODE='GNB01',MESSAGE='notebook_inactive'; END IF;
|
|
1810
|
+
END IF;
|
|
1811
|
+
RETURN NEW;
|
|
1812
|
+
END $$;
|
|
1813
|
+
REVOKE ALL ON FUNCTION grove_notebook_writable() FROM PUBLIC;
|
|
1814
|
+
CREATE TRIGGER notebook_writable BEFORE INSERT OR UPDATE ON memories
|
|
1815
|
+
FOR EACH ROW EXECUTE FUNCTION grove_notebook_writable();
|
|
1816
|
+
CREATE TRIGGER notebook_writable BEFORE INSERT OR UPDATE ON projects
|
|
1817
|
+
FOR EACH ROW EXECUTE FUNCTION grove_notebook_writable();
|
|
1818
|
+
`);
|
|
1819
|
+
const { rows } = await db2.raw(
|
|
1820
|
+
"SELECT pg_get_functiondef('grove_public_share(text,text,uuid,uuid,uuid,integer,integer)'::regprocedure) AS definition"
|
|
1821
|
+
);
|
|
1822
|
+
await db2.raw(
|
|
1823
|
+
rows[0].definition.replaceAll(
|
|
1824
|
+
"FROM projects WHERE",
|
|
1825
|
+
"FROM projects WHERE status='active' AND"
|
|
1826
|
+
).replaceAll(
|
|
1827
|
+
"WHERE p.account_id = owner_id",
|
|
1828
|
+
"WHERE p.status='active' AND p.account_id = owner_id"
|
|
1829
|
+
).replaceAll(
|
|
1830
|
+
"WHERE p.deleted_at IS NULL",
|
|
1831
|
+
"WHERE p.status='active' AND p.deleted_at IS NULL"
|
|
1832
|
+
)
|
|
1833
|
+
);
|
|
1834
|
+
},
|
|
1835
|
+
async down() {
|
|
1836
|
+
throw new Error(
|
|
1837
|
+
"Use a forward migration; notebook lifecycle preserves retained data"
|
|
1838
|
+
);
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
|
|
1842
|
+
// lib/database/src/media-migration.ts
|
|
1843
|
+
var mediaAttachments = {
|
|
1844
|
+
name: "20261023_017_media_attachments",
|
|
1845
|
+
async up(db2) {
|
|
1846
|
+
await db2.raw(`
|
|
1847
|
+
ALTER TABLE accounts ADD COLUMN media_bytes bigint NOT NULL DEFAULT 0 CHECK(media_bytes>=0),
|
|
1848
|
+
ADD COLUMN media_attempts integer NOT NULL DEFAULT 0 CHECK(media_attempts>=0);
|
|
1849
|
+
CREATE TABLE media_objects (
|
|
1850
|
+
account_id uuid NOT NULL REFERENCES accounts(id), id uuid NOT NULL,
|
|
1851
|
+
memory_id uuid NOT NULL, mime text NOT NULL CHECK(mime IN ('image/png','image/jpeg','image/webp')),
|
|
1852
|
+
bytes integer NOT NULL CHECK(bytes BETWEEN 1 AND 10485760), sha256 text NOT NULL CHECK(sha256 ~ '^[0-9a-f]{64}$'),
|
|
1853
|
+
status text NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','ready','failed')),
|
|
1854
|
+
object_key text NOT NULL, lease_id uuid NOT NULL, lease_until timestamptz NOT NULL,
|
|
1855
|
+
width integer, height integer, created_at timestamptz NOT NULL DEFAULT now(),
|
|
1856
|
+
PRIMARY KEY(account_id,id), UNIQUE(account_id,id,memory_id), UNIQUE(object_key),
|
|
1857
|
+
FOREIGN KEY(account_id,memory_id) REFERENCES memories(account_id,id),
|
|
1858
|
+
CHECK(status<>'ready' OR (width>0 AND height>0))
|
|
1859
|
+
);
|
|
1860
|
+
CREATE TABLE media_upload_attempts (
|
|
1861
|
+
account_id uuid NOT NULL, media_id uuid NOT NULL, id uuid NOT NULL,
|
|
1862
|
+
object_key text NOT NULL UNIQUE, created_at timestamptz NOT NULL DEFAULT now(),
|
|
1863
|
+
PRIMARY KEY(account_id,id), FOREIGN KEY(account_id,media_id) REFERENCES media_objects(account_id,id)
|
|
1864
|
+
);
|
|
1865
|
+
CREATE TABLE media_revision_bindings (
|
|
1866
|
+
account_id uuid NOT NULL, media_id uuid NOT NULL, memory_id uuid NOT NULL, version integer NOT NULL,
|
|
1867
|
+
PRIMARY KEY(account_id,memory_id,version,media_id),
|
|
1868
|
+
FOREIGN KEY(account_id,media_id,memory_id) REFERENCES media_objects(account_id,id,memory_id),
|
|
1869
|
+
FOREIGN KEY(account_id,memory_id,version) REFERENCES memory_revisions(account_id,memory_id,version)
|
|
1870
|
+
);
|
|
1871
|
+
CREATE INDEX media_note ON media_objects(account_id,memory_id);
|
|
1872
|
+
`);
|
|
1873
|
+
for (const table of [
|
|
1874
|
+
"media_objects",
|
|
1875
|
+
"media_upload_attempts",
|
|
1876
|
+
"media_revision_bindings"
|
|
1877
|
+
]) {
|
|
1878
|
+
await db2.raw(
|
|
1879
|
+
`ALTER TABLE ?? ENABLE ROW LEVEL SECURITY; ALTER TABLE ?? FORCE ROW LEVEL SECURITY`,
|
|
1880
|
+
[table, table]
|
|
1881
|
+
);
|
|
1882
|
+
await db2.raw(
|
|
1883
|
+
`CREATE POLICY tenant ON ?? USING(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid)
|
|
1884
|
+
WITH CHECK(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid)`,
|
|
1885
|
+
[table]
|
|
1886
|
+
);
|
|
1887
|
+
await db2.raw(
|
|
1888
|
+
`CREATE POLICY active_owner ON ?? AS RESTRICTIVE USING(EXISTS(SELECT 1 FROM accounts a WHERE a.id=??.account_id AND a.status='active')) WITH CHECK(true)`,
|
|
1889
|
+
[table, table]
|
|
1890
|
+
);
|
|
1891
|
+
await db2.raw(`GRANT SELECT,INSERT ON ?? TO grove_runtime,grove_worker`, [
|
|
1892
|
+
table
|
|
1893
|
+
]);
|
|
1894
|
+
}
|
|
1895
|
+
await db2.raw(`
|
|
1896
|
+
GRANT UPDATE ON media_objects TO grove_runtime,grove_worker;
|
|
1897
|
+
CREATE FUNCTION grove_immutable_media() RETURNS trigger LANGUAGE plpgsql SET search_path=pg_catalog,public,pg_temp AS $$
|
|
1898
|
+
BEGIN
|
|
1899
|
+
IF OLD.status='ready' AND NEW IS DISTINCT FROM OLD THEN RAISE EXCEPTION 'published_media_immutable'; END IF;
|
|
1900
|
+
IF (NEW.account_id,NEW.id,NEW.memory_id,NEW.mime,NEW.bytes,NEW.sha256) IS DISTINCT FROM
|
|
1901
|
+
(OLD.account_id,OLD.id,OLD.memory_id,OLD.mime,OLD.bytes,OLD.sha256) THEN RAISE EXCEPTION 'media_identity_immutable'; END IF;
|
|
1902
|
+
RETURN NEW;
|
|
1903
|
+
END $$;
|
|
1904
|
+
REVOKE ALL ON FUNCTION grove_immutable_media() FROM PUBLIC;
|
|
1905
|
+
CREATE TRIGGER immutable_media BEFORE UPDATE ON media_objects FOR EACH ROW EXECUTE FUNCTION grove_immutable_media();
|
|
1906
|
+
CREATE POLICY visible_note ON media_objects AS RESTRICTIVE USING(EXISTS(
|
|
1907
|
+
SELECT 1 FROM memories m WHERE m.account_id=media_objects.account_id AND m.id=media_objects.memory_id)) WITH CHECK(true);
|
|
1908
|
+
CREATE POLICY visible_note ON media_revision_bindings AS RESTRICTIVE USING(EXISTS(
|
|
1909
|
+
SELECT 1 FROM memories m WHERE m.account_id=media_revision_bindings.account_id AND m.id=media_revision_bindings.memory_id)) WITH CHECK(true);
|
|
1910
|
+
CREATE FUNCTION grove_bind_media() RETURNS trigger LANGUAGE plpgsql SET search_path=pg_catalog,public,pg_temp AS $$
|
|
1911
|
+
DECLARE identifier uuid;
|
|
1912
|
+
BEGIN
|
|
1913
|
+
FOR identifier IN SELECT DISTINCT matches[1]::uuid FROM regexp_matches(NEW.content,
|
|
1914
|
+
'grove-media:([0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})','g') AS matches
|
|
1915
|
+
LOOP
|
|
1916
|
+
IF NOT EXISTS(SELECT 1 FROM media_objects WHERE account_id=NEW.account_id AND id=identifier
|
|
1917
|
+
AND memory_id=NEW.memory_id AND status='ready') THEN
|
|
1918
|
+
RAISE EXCEPTION 'media_reference_unavailable' USING ERRCODE='23514';
|
|
1919
|
+
END IF;
|
|
1920
|
+
INSERT INTO media_revision_bindings(account_id,media_id,memory_id,version)
|
|
1921
|
+
VALUES(NEW.account_id,identifier,NEW.memory_id,NEW.version);
|
|
1922
|
+
END LOOP;
|
|
1923
|
+
RETURN NEW;
|
|
1924
|
+
END $$;
|
|
1925
|
+
REVOKE ALL ON FUNCTION grove_bind_media() FROM PUBLIC;
|
|
1926
|
+
CREATE TRIGGER bind_media AFTER INSERT ON memory_revisions FOR EACH ROW EXECUTE FUNCTION grove_bind_media();
|
|
1927
|
+
|
|
1928
|
+
CREATE FUNCTION grove_public_media(digest text, note_id uuid, media_id uuid) RETURNS jsonb
|
|
1929
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
1930
|
+
DECLARE document jsonb; object jsonb;
|
|
1931
|
+
BEGIN
|
|
1932
|
+
-- The existing reader owns admission, account/share locks, subtree scope,
|
|
1933
|
+
-- current visibility and database-clock expiry. Never issue an owner grant.
|
|
1934
|
+
document:=grove_public_share(digest,'note',note_id,NULL,NULL,NULL,1);
|
|
1935
|
+
IF jsonb_exists(document,'limited') THEN RETURN document; END IF;
|
|
1936
|
+
IF document IS NULL OR document->'note' IS NULL THEN RETURN NULL; END IF;
|
|
1937
|
+
SELECT jsonb_build_object('key',o.object_key,'mime',o.mime,'bytes',o.bytes,'sha256',o.sha256) INTO object
|
|
1938
|
+
FROM media_objects o JOIN memories m ON m.account_id=o.account_id AND m.id=o.memory_id
|
|
1939
|
+
JOIN public_shares s ON s.account_id=o.account_id AND s.token_digest=digest
|
|
1940
|
+
JOIN media_revision_bindings b ON b.account_id=o.account_id AND b.media_id=o.id AND b.memory_id=m.id AND b.version=m.version
|
|
1941
|
+
WHERE m.id=note_id AND o.id=grove_public_media.media_id AND o.status='ready' AND m.deleted_at IS NULL
|
|
1942
|
+
AND m.status='active' AND (m.expires_at IS NULL OR m.expires_at>clock_timestamp());
|
|
1943
|
+
RETURN object;
|
|
1944
|
+
END $$;
|
|
1945
|
+
REVOKE ALL ON FUNCTION grove_public_media(text,uuid,uuid) FROM PUBLIC;
|
|
1946
|
+
GRANT EXECUTE ON FUNCTION grove_public_media(text,uuid,uuid) TO grove_runtime;
|
|
1947
|
+
`);
|
|
1948
|
+
},
|
|
1949
|
+
async down() {
|
|
1950
|
+
throw new Error(
|
|
1951
|
+
"Use a forward migration; retained media cannot be rolled back"
|
|
1952
|
+
);
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
|
|
1956
|
+
// lib/database/src/recordings-migration.ts
|
|
1957
|
+
var recordings = {
|
|
1958
|
+
name: "20261023_018_recordings",
|
|
1959
|
+
async up(db2) {
|
|
1960
|
+
await db2.raw(`
|
|
1961
|
+
ALTER TABLE media_objects DROP CONSTRAINT media_objects_mime_check,
|
|
1962
|
+
DROP CONSTRAINT media_objects_check,
|
|
1963
|
+
ADD COLUMN duration_ms integer CHECK(duration_ms BETWEEN 1 AND 60000),
|
|
1964
|
+
ADD CONSTRAINT media_objects_mime_check CHECK(mime IN ('image/png','image/jpeg','image/webp','audio/wav')),
|
|
1965
|
+
ADD CONSTRAINT media_objects_complete CHECK(status<>'ready' OR
|
|
1966
|
+
(mime='audio/wav' AND duration_ms>0) OR (mime<>'audio/wav' AND width>0 AND height>0));
|
|
1967
|
+
CREATE TABLE media_transcripts (
|
|
1968
|
+
account_id uuid NOT NULL, media_id uuid NOT NULL, version integer NOT NULL DEFAULT 1,
|
|
1969
|
+
text text NOT NULL DEFAULT '' CHECK(length(text)<=6000), operation_id uuid NOT NULL,
|
|
1970
|
+
fingerprint text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(),
|
|
1971
|
+
PRIMARY KEY(account_id,media_id), FOREIGN KEY(account_id,media_id) REFERENCES media_objects(account_id,id)
|
|
1972
|
+
);
|
|
1973
|
+
ALTER TABLE media_transcripts ENABLE ROW LEVEL SECURITY;
|
|
1974
|
+
ALTER TABLE media_transcripts FORCE ROW LEVEL SECURITY;
|
|
1975
|
+
CREATE POLICY tenant ON media_transcripts USING(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid)
|
|
1976
|
+
WITH CHECK(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid);
|
|
1977
|
+
CREATE POLICY visible_media ON media_transcripts AS RESTRICTIVE USING(EXISTS(
|
|
1978
|
+
SELECT 1 FROM media_objects o WHERE o.account_id=media_transcripts.account_id AND o.id=media_transcripts.media_id)) WITH CHECK(true);
|
|
1979
|
+
GRANT SELECT,INSERT,UPDATE ON media_transcripts TO grove_runtime,grove_worker;
|
|
1980
|
+
CREATE TABLE media_transcript_revisions (
|
|
1981
|
+
account_id uuid NOT NULL, media_id uuid NOT NULL, version integer NOT NULL, text text NOT NULL CHECK(length(text)<=6000),
|
|
1982
|
+
created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY(account_id,media_id,version),
|
|
1983
|
+
FOREIGN KEY(account_id,media_id) REFERENCES media_transcripts(account_id,media_id)
|
|
1984
|
+
);
|
|
1985
|
+
ALTER TABLE media_transcript_revisions ENABLE ROW LEVEL SECURITY;
|
|
1986
|
+
ALTER TABLE media_transcript_revisions FORCE ROW LEVEL SECURITY;
|
|
1987
|
+
CREATE POLICY tenant ON media_transcript_revisions USING(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid)
|
|
1988
|
+
WITH CHECK(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid);
|
|
1989
|
+
CREATE POLICY visible_media ON media_transcript_revisions AS RESTRICTIVE USING(EXISTS(
|
|
1990
|
+
SELECT 1 FROM media_objects o WHERE o.account_id=media_transcript_revisions.account_id AND o.id=media_transcript_revisions.media_id)) WITH CHECK(true);
|
|
1991
|
+
GRANT SELECT,INSERT ON media_transcript_revisions TO grove_runtime,grove_worker;
|
|
1992
|
+
`);
|
|
1993
|
+
},
|
|
1994
|
+
async down() {
|
|
1995
|
+
throw new Error("Use a forward migration");
|
|
1996
|
+
}
|
|
1997
|
+
};
|
|
1998
|
+
|
|
1999
|
+
// lib/database/src/speech-migration.ts
|
|
2000
|
+
var speech = {
|
|
2001
|
+
name: "20261023_019_speech",
|
|
2002
|
+
async up(db2) {
|
|
2003
|
+
await db2.raw(`
|
|
2004
|
+
ALTER TABLE accounts
|
|
2005
|
+
ADD COLUMN transcription_enabled boolean NOT NULL DEFAULT false,
|
|
2006
|
+
ADD COLUMN read_aloud_enabled boolean NOT NULL DEFAULT false,
|
|
2007
|
+
ADD COLUMN transcription_version integer NOT NULL DEFAULT 0,
|
|
2008
|
+
ADD COLUMN read_aloud_version integer NOT NULL DEFAULT 0;
|
|
2009
|
+
ALTER TABLE background_tasks DROP CONSTRAINT background_tasks_kind_check,
|
|
2010
|
+
ADD CONSTRAINT background_tasks_kind_check CHECK(kind IN
|
|
2011
|
+
('export','account_cleanup','retention_cleanup','artifact_cleanup','model_backfill','import','connector_sync','speech_transcribe','speech_synthesize'));
|
|
2012
|
+
CREATE TABLE speech_configuration (
|
|
2013
|
+
singleton boolean PRIMARY KEY DEFAULT true CHECK(singleton),
|
|
2014
|
+
transcription text, synthesis text
|
|
2015
|
+
);
|
|
2016
|
+
INSERT INTO speech_configuration(singleton) VALUES(true);
|
|
2017
|
+
GRANT SELECT ON speech_configuration TO grove_runtime,grove_worker;
|
|
2018
|
+
GRANT UPDATE ON speech_configuration TO grove_runtime;
|
|
2019
|
+
CREATE TABLE speech_usage (
|
|
2020
|
+
account_id uuid NOT NULL REFERENCES accounts(id), day date NOT NULL DEFAULT CURRENT_DATE,
|
|
2021
|
+
kind text NOT NULL CHECK(kind IN ('speech_transcribe','speech_synthesize')),
|
|
2022
|
+
requests integer NOT NULL DEFAULT 0 CHECK(requests>=0), units integer NOT NULL DEFAULT 0 CHECK(units>=0),
|
|
2023
|
+
PRIMARY KEY(account_id,day,kind)
|
|
2024
|
+
);
|
|
2025
|
+
ALTER TABLE speech_usage ENABLE ROW LEVEL SECURITY;
|
|
2026
|
+
ALTER TABLE speech_usage FORCE ROW LEVEL SECURITY;
|
|
2027
|
+
CREATE POLICY tenant ON speech_usage USING(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid)
|
|
2028
|
+
WITH CHECK(account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid);
|
|
2029
|
+
GRANT SELECT,INSERT,UPDATE ON speech_usage TO grove_runtime,grove_worker;
|
|
2030
|
+
|
|
2031
|
+
CREATE FUNCTION grove_speech_session_active(wanted uuid) RETURNS boolean LANGUAGE sql
|
|
2032
|
+
SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
2033
|
+
SELECT EXISTS(SELECT 1 FROM background_tasks t JOIN accounts a ON a.id=t.account_id
|
|
2034
|
+
WHERE t.id=wanted AND t.account_id=NULLIF(current_setting('grove.account_id',true),'')::uuid
|
|
2035
|
+
AND t.kind IN ('speech_transcribe','speech_synthesize') AND a.status='active'
|
|
2036
|
+
AND NOT EXISTS(SELECT 1 FROM session_revocations r WHERE r.id=t.payload->>'sessionId'))
|
|
2037
|
+
$$;
|
|
2038
|
+
REVOKE ALL ON FUNCTION grove_speech_session_active(uuid) FROM PUBLIC;
|
|
2039
|
+
GRANT EXECUTE ON FUNCTION grove_speech_session_active(uuid) TO grove_runtime,grove_worker;
|
|
2040
|
+
|
|
2041
|
+
CREATE FUNCTION grove_expire_speech() RETURNS integer LANGUAGE plpgsql
|
|
2042
|
+
SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
2043
|
+
DECLARE changed integer;
|
|
2044
|
+
BEGIN
|
|
2045
|
+
WITH expired AS (SELECT id FROM background_tasks
|
|
2046
|
+
WHERE kind IN ('speech_transcribe','speech_synthesize') AND expires_at<=now()
|
|
2047
|
+
AND result IS NOT NULL ORDER BY expires_at LIMIT 100 FOR UPDATE SKIP LOCKED)
|
|
2048
|
+
UPDATE background_tasks SET result=NULL WHERE id IN (SELECT id FROM expired);
|
|
2049
|
+
GET DIAGNOSTICS changed=ROW_COUNT;
|
|
2050
|
+
RETURN changed;
|
|
2051
|
+
END $$;
|
|
2052
|
+
REVOKE ALL ON FUNCTION grove_expire_speech() FROM PUBLIC;
|
|
2053
|
+
GRANT EXECUTE ON FUNCTION grove_expire_speech() TO grove_worker;
|
|
2054
|
+
|
|
2055
|
+
CREATE FUNCTION grove_speech_preferences() RETURNS trigger LANGUAGE plpgsql
|
|
2056
|
+
SET search_path=pg_catalog,public,pg_temp AS $$
|
|
2057
|
+
BEGIN
|
|
2058
|
+
IF OLD.transcription_enabled IS DISTINCT FROM NEW.transcription_enabled THEN
|
|
2059
|
+
NEW.transcription_version:=OLD.transcription_version+1;
|
|
2060
|
+
UPDATE background_tasks SET status='cancelled',lease_id=NULL,lease_until=NULL,finished_at=now(),updated_at=now()
|
|
2061
|
+
WHERE account_id=NEW.id AND kind='speech_transcribe' AND status IN ('pending','running');
|
|
2062
|
+
END IF;
|
|
2063
|
+
IF OLD.read_aloud_enabled IS DISTINCT FROM NEW.read_aloud_enabled THEN
|
|
2064
|
+
NEW.read_aloud_version:=OLD.read_aloud_version+1;
|
|
2065
|
+
UPDATE background_tasks SET status='cancelled',lease_id=NULL,lease_until=NULL,finished_at=now(),updated_at=now()
|
|
2066
|
+
WHERE account_id=NEW.id AND kind='speech_synthesize' AND status IN ('pending','running');
|
|
2067
|
+
END IF;
|
|
2068
|
+
RETURN NEW;
|
|
2069
|
+
END $$;
|
|
2070
|
+
REVOKE ALL ON FUNCTION grove_speech_preferences() FROM PUBLIC;
|
|
2071
|
+
CREATE TRIGGER speech_preferences BEFORE UPDATE ON accounts FOR EACH ROW EXECUTE FUNCTION grove_speech_preferences();
|
|
2072
|
+
|
|
2073
|
+
-- Short-lived provider slots have no tenant-readable surface. The provider's
|
|
2074
|
+
-- 45s deadline is shorter than the 90s slot even if a process disappears.
|
|
2075
|
+
CREATE TABLE speech_slots(task_id uuid PRIMARY KEY,lease_id uuid NOT NULL,expires_at timestamptz NOT NULL);
|
|
2076
|
+
CREATE FUNCTION grove_speech_acquire(wanted uuid,token uuid,maximum integer) RETURNS boolean
|
|
2077
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
2078
|
+
BEGIN
|
|
2079
|
+
PERFORM pg_advisory_xact_lock(68142020);
|
|
2080
|
+
IF NOT EXISTS(SELECT 1 FROM background_tasks t WHERE t.id=wanted AND t.lease_id=token
|
|
2081
|
+
AND t.status='running' AND t.lease_until>now() AND t.kind IN ('speech_transcribe','speech_synthesize') AND grove_task_allowed(t)) THEN RETURN false; END IF;
|
|
2082
|
+
DELETE FROM speech_slots WHERE expires_at<=now();
|
|
2083
|
+
IF (SELECT count(*) FROM speech_slots)>=greatest(1,least(maximum,16)) THEN RETURN false; END IF;
|
|
2084
|
+
INSERT INTO speech_slots VALUES(wanted,token,now()+interval '90 seconds') ON CONFLICT DO NOTHING;
|
|
2085
|
+
RETURN FOUND;
|
|
2086
|
+
END $$;
|
|
2087
|
+
CREATE FUNCTION grove_speech_release(wanted uuid,token uuid) RETURNS void LANGUAGE sql
|
|
2088
|
+
SECURITY DEFINER SET search_path=pg_catalog,public,pg_temp AS $$
|
|
2089
|
+
DELETE FROM speech_slots WHERE task_id=wanted AND lease_id=token
|
|
2090
|
+
$$;
|
|
2091
|
+
REVOKE ALL ON FUNCTION grove_speech_acquire(uuid,uuid,integer),grove_speech_release(uuid,uuid) FROM PUBLIC;
|
|
2092
|
+
GRANT EXECUTE ON FUNCTION grove_speech_acquire(uuid,uuid,integer),grove_speech_release(uuid,uuid) TO grove_worker;
|
|
2093
|
+
`);
|
|
2094
|
+
},
|
|
2095
|
+
async down() {
|
|
2096
|
+
throw new Error("Use a forward migration");
|
|
2097
|
+
}
|
|
2098
|
+
};
|
|
2099
|
+
|
|
2100
|
+
// lib/database/src/research-migration.ts
|
|
2101
|
+
var researchTools = {
|
|
2102
|
+
name: "20261023_020_ask_tools",
|
|
2103
|
+
async up(db2) {
|
|
2104
|
+
await db2.raw(`
|
|
2105
|
+
ALTER TABLE accounts ADD COLUMN ask_tools jsonb NOT NULL DEFAULT '{"exa":true,"tavily":false,"organize":true}';
|
|
2106
|
+
ALTER TABLE conversations ADD COLUMN tools jsonb NOT NULL DEFAULT '{"exa":true,"tavily":false,"organize":true}',
|
|
2107
|
+
ADD COLUMN tools_version integer NOT NULL DEFAULT 0;
|
|
2108
|
+
ALTER TABLE conversation_turns ADD COLUMN tool_runs jsonb NOT NULL DEFAULT '[]';
|
|
2109
|
+
CREATE TABLE research_account_usage (
|
|
2110
|
+
account_id uuid NOT NULL REFERENCES accounts(id), day date NOT NULL,
|
|
2111
|
+
calls integer NOT NULL DEFAULT 0, PRIMARY KEY(account_id,day)
|
|
2112
|
+
);
|
|
2113
|
+
ALTER TABLE research_account_usage ENABLE ROW LEVEL SECURITY;
|
|
2114
|
+
ALTER TABLE research_account_usage FORCE ROW LEVEL SECURITY;
|
|
2115
|
+
CREATE POLICY tenant ON research_account_usage USING (account_id = NULLIF(current_setting('grove.account_id',true),'')::uuid)
|
|
2116
|
+
WITH CHECK (account_id = NULLIF(current_setting('grove.account_id',true),'')::uuid);
|
|
2117
|
+
GRANT SELECT, DELETE ON research_account_usage TO grove_runtime, grove_worker;
|
|
2118
|
+
CREATE TABLE research_global_usage (month date PRIMARY KEY, microdollars bigint NOT NULL DEFAULT 0);
|
|
2119
|
+
REVOKE ALL ON research_global_usage FROM PUBLIC, grove_runtime, grove_worker;
|
|
2120
|
+
CREATE FUNCTION grove_research_reserve(provider text, operation text, daily_limit integer, monthly_limit bigint)
|
|
2121
|
+
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,public AS $$
|
|
2122
|
+
DECLARE account uuid := NULLIF(current_setting('grove.account_id',true),'')::uuid;
|
|
2123
|
+
charge bigint; used integer; period date := date_trunc('month',now() AT TIME ZONE 'UTC')::date;
|
|
2124
|
+
today date := (now() AT TIME ZONE 'UTC')::date;
|
|
2125
|
+
BEGIN
|
|
2126
|
+
IF account IS NULL OR daily_limit < 1 OR monthly_limit < 1 OR
|
|
2127
|
+
NOT EXISTS(SELECT 1 FROM public.accounts WHERE id=account AND status='active' AND ask_enabled) THEN RETURN false; END IF;
|
|
2128
|
+
charge := CASE WHEN provider='exa' AND operation='search' THEN 4000
|
|
2129
|
+
WHEN provider='exa' AND operation='extract' THEN 3000
|
|
2130
|
+
WHEN provider='tavily' AND operation IN ('search','extract') THEN 8000 ELSE NULL END;
|
|
2131
|
+
IF charge IS NULL THEN RETURN false; END IF;
|
|
2132
|
+
INSERT INTO public.research_account_usage(account_id,day) VALUES(account,today) ON CONFLICT DO NOTHING;
|
|
2133
|
+
SELECT calls INTO used FROM public.research_account_usage WHERE account_id=account AND day=today FOR UPDATE;
|
|
2134
|
+
IF used >= daily_limit THEN RETURN false; END IF;
|
|
2135
|
+
INSERT INTO public.research_global_usage(month) VALUES(period) ON CONFLICT DO NOTHING;
|
|
2136
|
+
UPDATE public.research_global_usage SET microdollars=microdollars+charge
|
|
2137
|
+
WHERE month=period AND microdollars+charge<=monthly_limit;
|
|
2138
|
+
IF NOT FOUND THEN RETURN false; END IF;
|
|
2139
|
+
UPDATE public.research_account_usage SET calls=calls+1 WHERE account_id=account AND day=today;
|
|
2140
|
+
RETURN true;
|
|
2141
|
+
END $$;
|
|
2142
|
+
REVOKE ALL ON FUNCTION grove_research_reserve(text,text,integer,bigint) FROM PUBLIC;
|
|
2143
|
+
GRANT EXECUTE ON FUNCTION grove_research_reserve(text,text,integer,bigint) TO grove_runtime;
|
|
2144
|
+
`);
|
|
2145
|
+
},
|
|
2146
|
+
async down() {
|
|
2147
|
+
throw new Error("Use a forward migration");
|
|
2148
|
+
}
|
|
2149
|
+
};
|
|
2150
|
+
|
|
2151
|
+
// lib/database/src/speech-defaults-migration.ts
|
|
2152
|
+
var speechDefaults = {
|
|
2153
|
+
name: "20261023_021_speech_defaults",
|
|
2154
|
+
async up(db2) {
|
|
2155
|
+
await db2.raw(
|
|
2156
|
+
"ALTER TABLE accounts ALTER COLUMN read_aloud_enabled SET DEFAULT true"
|
|
2157
|
+
);
|
|
2158
|
+
},
|
|
2159
|
+
async down() {
|
|
2160
|
+
throw new Error("Use a forward migration");
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
|
|
1707
2164
|
// lib/database/src/migrations.ts
|
|
1708
2165
|
var migrations = [
|
|
1709
2166
|
{
|
|
@@ -1921,7 +2378,15 @@ var migrations = [
|
|
|
1921
2378
|
shareExpiration,
|
|
1922
2379
|
aiPreferences,
|
|
1923
2380
|
queueProcessing,
|
|
1924
|
-
notebookAssistance
|
|
2381
|
+
notebookAssistance,
|
|
2382
|
+
notebookLifecycle,
|
|
2383
|
+
mediaAttachments,
|
|
2384
|
+
recordings,
|
|
2385
|
+
speech,
|
|
2386
|
+
researchTools,
|
|
2387
|
+
speechDefaults,
|
|
2388
|
+
speechVoices,
|
|
2389
|
+
billing
|
|
1925
2390
|
];
|
|
1926
2391
|
var migrationSource = {
|
|
1927
2392
|
getMigrations: async () => migrations,
|