@segmentstream/ontology 0.158.0 → 0.159.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/CHANGELOG.md +12 -0
- package/README.md +4 -2
- package/dist/migrate/apply.d.ts +3 -2
- package/dist/migrate/apply.js +11 -4
- package/dist/migrate/plan.js +11 -6
- package/dist/migrate/preview.js +4 -1
- package/dist/runtime/chat-messages.d.ts +2 -2
- package/dist/runtime/chat-messages.js +1 -1
- package/dist/storage/attachments.js +15 -16
- package/dist/storage/tables.d.ts +18 -11
- package/dist/storage/tables.js +87 -12
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.159.0
|
|
4
|
+
|
|
5
|
+
The chats' temporary attachments table is `_chat_attachments`, and its chat column is `chat`.
|
|
6
|
+
The next deploy's migration renames a table an earlier SDK created as `_agent_attachments`, with
|
|
7
|
+
its column `conversation`, in place: rows, ids, files and their expiry are kept. The earlier name
|
|
8
|
+
stays as a view of the table, so a workspace, or a preview of one, still on an earlier ontology
|
|
9
|
+
SDK reads and writes the same attachments.
|
|
10
|
+
|
|
11
|
+
Action required: upgrade `@segmentstream/ontology` and deploy before 2027-02-24, when the view
|
|
12
|
+
`_agent_attachments` is removed and Cloud stops deploying earlier ontology SDKs. Workspace code
|
|
13
|
+
that queries the table directly reads `_chat_attachments` and its column `chat` instead.
|
|
14
|
+
|
|
3
15
|
## 0.158.0
|
|
4
16
|
|
|
5
17
|
The built-in `agent_review` is now `agent_draft`, the agent's draft reply awaiting a staff
|
package/README.md
CHANGED
|
@@ -1696,13 +1696,15 @@ compatible albums to fit and returns per-file errors. Group purpose and document
|
|
|
1696
1696
|
still apply; a member's read access alone does not authorize sharing a restricted document in a group.
|
|
1697
1697
|
|
|
1698
1698
|
A file a member sends the agent is a temporary attachment: platform metadata in the runtime
|
|
1699
|
-
table `
|
|
1699
|
+
table `_chat_attachments`, never an ontology record, with its bytes stored once as a revision in
|
|
1700
1700
|
the ontology's file storage, beside uploads. It belongs to the member, the chat it arrived in and
|
|
1701
1701
|
the agent session that first reads it, holds its SHA-256, size, type and name, and expires 24
|
|
1702
1702
|
hours after receipt (`received` → `ready` → `promoted`, `discarded` or `expired`). Files are up
|
|
1703
1703
|
to 20 MB, what Telegram lets a bot download; a member holds at most 50 live attachments. The
|
|
1704
1704
|
table exists on deployments with file storage; the migration adds it beside the file tables, also
|
|
1705
|
-
to a database an earlier SDK migrated
|
|
1705
|
+
to a database an earlier SDK migrated, and renames a table an earlier SDK created as
|
|
1706
|
+
`_agent_attachments`, whose chat column was `conversation`. That earlier name stays a view of the
|
|
1707
|
+
table for a while, so that a workspace still on an earlier SDK keeps its attachments.
|
|
1706
1708
|
|
|
1707
1709
|
Only the agent calls these routes, with its runtime token, `X-SegmentStream-Act-As`, the chat in
|
|
1708
1710
|
`X-SegmentStream-Agent-Chat` (`telegram:<chat id>`) and, inside a turn, the session in
|
package/dist/migrate/apply.d.ts
CHANGED
|
@@ -90,8 +90,9 @@ export declare const RUNTIME_TABLES: ReadonlyArray<string>;
|
|
|
90
90
|
* A grants table without app-session binding is reported incomplete so the idempotent OAuth DDL
|
|
91
91
|
* also upgrades an unchanged manifest, and so is an action attribution table without `chat`, an
|
|
92
92
|
* API keys table without `delegation` and an attachments table whose source check predates one of
|
|
93
|
-
* {@link ATTACHMENT_SOURCES}
|
|
94
|
-
* widen.
|
|
93
|
+
* {@link ATTACHMENT_SOURCES} or that lacks the view of its earlier name, which the idempotent
|
|
94
|
+
* attribution, API key and attachment DDL add and widen. A table still named
|
|
95
|
+
* {@link LEGACY_ATTACHMENTS_TABLE} is reported under that name, so that the plan renames it.
|
|
95
96
|
*/
|
|
96
97
|
export declare const readRuntimeTables: (input: CatalogInput) => Effect.Effect<ReadonlySet<string>, SqlError>;
|
|
97
98
|
/** Only valid, complete covering indexes satisfy the upgrade; failed builds are retried. */
|
package/dist/migrate/apply.js
CHANGED
|
@@ -15,7 +15,7 @@ import { randomUUID } from "node:crypto";
|
|
|
15
15
|
import { recordWorkflowCatalog } from "../runtime/workflow-admission.js";
|
|
16
16
|
import { prepareReplicationEvolution, dropsServingReplicationStorage, refreshReplicationProjection, retainReplicationHistory, renameReplicationHistory, } from "../replication/evolution.js";
|
|
17
17
|
import { migratedReplicationFills } from "../replication/migrate.js";
|
|
18
|
-
import { AGENT_TASKS_TABLE, ATTACHMENT_SOURCE_CHECK, ATTACHMENT_SOURCES, ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
18
|
+
import { AGENT_TASKS_TABLE, ATTACHMENT_SOURCE_CHECK, ATTACHMENT_SOURCES, ATTACHMENTS_TABLE, LEGACY_ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
19
19
|
import { principalsOntology, reconcileWorkflowUsers } from "../runtime/principals.js";
|
|
20
20
|
import { FEDERATION_TABLES } from "../runtime/federation-tables.js";
|
|
21
21
|
import { syncUserIdentities, USER_IDENTITY_ENTITY_ID } from "../runtime/user-identities.js";
|
|
@@ -488,13 +488,15 @@ export const RUNTIME_TABLES = [
|
|
|
488
488
|
* A grants table without app-session binding is reported incomplete so the idempotent OAuth DDL
|
|
489
489
|
* also upgrades an unchanged manifest, and so is an action attribution table without `chat`, an
|
|
490
490
|
* API keys table without `delegation` and an attachments table whose source check predates one of
|
|
491
|
-
* {@link ATTACHMENT_SOURCES}
|
|
492
|
-
* widen.
|
|
491
|
+
* {@link ATTACHMENT_SOURCES} or that lacks the view of its earlier name, which the idempotent
|
|
492
|
+
* attribution, API key and attachment DDL add and widen. A table still named
|
|
493
|
+
* {@link LEGACY_ATTACHMENTS_TABLE} is reported under that name, so that the plan renames it.
|
|
493
494
|
*/
|
|
494
495
|
export const readRuntimeTables = (input) => input.sql
|
|
495
496
|
.unsafe(`SELECT "tablename" AS "name" FROM "pg_catalog"."pg_tables" ` +
|
|
496
497
|
`WHERE "schemaname" = current_schema() ` +
|
|
497
|
-
|
|
498
|
+
// @legacy ontology.agent-attachments-view: a table still under its earlier name is renamed.
|
|
499
|
+
`AND "tablename" IN (${[...RUNTIME_TABLES, LEGACY_ATTACHMENTS_TABLE].map(quoteLiteral).join(", ")}) ` +
|
|
498
500
|
`AND ("tablename" <> '_oauth_grants' OR EXISTS (` +
|
|
499
501
|
`SELECT 1 FROM information_schema.columns WHERE table_schema = current_schema() ` +
|
|
500
502
|
`AND table_name = '_oauth_grants' AND column_name = 'identity_id')) ` +
|
|
@@ -516,6 +518,11 @@ export const readRuntimeTables = (input) => input.sql
|
|
|
516
518
|
`AND ("tablename" <> ${quoteLiteral(API_KEYS_TABLE)} OR EXISTS (` +
|
|
517
519
|
`SELECT 1 FROM information_schema.columns WHERE table_schema = current_schema() ` +
|
|
518
520
|
`AND table_name = ${quoteLiteral(API_KEYS_TABLE)} AND column_name = 'delegation')) ` +
|
|
521
|
+
// @legacy ontology.agent-attachments-view
|
|
522
|
+
// Without the view of the table's earlier name, the attachment DDL runs again and adds it.
|
|
523
|
+
`AND ("tablename" <> ${quoteLiteral(ATTACHMENTS_TABLE)} OR EXISTS (` +
|
|
524
|
+
`SELECT 1 FROM pg_catalog.pg_views WHERE schemaname = current_schema() ` +
|
|
525
|
+
`AND viewname = ${quoteLiteral(LEGACY_ATTACHMENTS_TABLE)})) ` +
|
|
519
526
|
`AND ("tablename" <> ${quoteLiteral(ATTACHMENTS_TABLE)} OR EXISTS (` +
|
|
520
527
|
`SELECT 1 FROM pg_catalog.pg_constraint c ` +
|
|
521
528
|
`JOIN pg_catalog.pg_class t ON t.oid = c.conrelid ` +
|
package/dist/migrate/plan.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isValueMatched, } from "../manifest.js";
|
|
2
2
|
import { FEDERATION_TABLES, FEDERATION_TABLES_DDL } from "../runtime/federation-tables.js";
|
|
3
3
|
import { EMAIL_LINK_FENCE_SQL, SEGMENTSTREAM_LINK_FENCE_SQL, USER_IDENTITY_ENTITY_ID, USER_IDENTITY_SYNC_SQL, } from "../runtime/user-identity-sql.js";
|
|
4
|
-
import { AGENT_TASKS_DDL, AGENT_TASKS_TABLE, SIGN_IN_EMAILS_DDL, SIGN_IN_EMAILS_TABLE, ATTACHMENTS_TABLE, ATTACHMENT_TABLES_DDL, BLOB_TABLES_DDL, WORKFLOW_STARTS_DDL, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
4
|
+
import { AGENT_TASKS_DDL, AGENT_TASKS_TABLE, SIGN_IN_EMAILS_DDL, SIGN_IN_EMAILS_TABLE, ATTACHMENTS_TABLE, ATTACHMENT_TABLES_DDL, LEGACY_ATTACHMENTS_TABLE, BLOB_TABLES_DDL, WORKFLOW_STARTS_DDL, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
5
5
|
import { SNAKE_CASE_NAME, isSystemProperty, toSnakeCase } from "../names.js";
|
|
6
6
|
import { NO_RETAINED_STORAGE, retainedEntityManifest, } from "./model.js";
|
|
7
7
|
import { ACTION_ATTRIBUTION_TABLE, ACTION_ATTRIBUTION_DDL, AGENT_TABLES, AGENT_TABLES_DDL, API_KEYS_DDL, API_KEYS_TABLE, CATALOG_TABLE_DDL, MAX_IDENTIFIER_LENGTH, OAUTH_TABLES, OAUTH_TABLES_DDL, USER_TABLE, addConstraintStatement, columnName, columnDefinition, constraintDefinition, createEnumStatement, createIndexStatement, createTableStatement, defaultExpression, entityStorage, historyActivityIndex, enumTypeName, isStoredProperty, fieldColumnName, joinTable, propertyCheck, blobForeignKey, propertyColumn, propertyEnum, quoteIdentifier, quoteLiteral, relationshipColumn, relationshipColumnName, RUNTIME_TABLES_DDL, relationshipForeignKey, SYSTEM_COLUMNS, storedEntity, tableName, } from "./storage.js";
|
|
@@ -225,17 +225,22 @@ export function planMigration(input) {
|
|
|
225
225
|
draft.runtimeTables.push(step({
|
|
226
226
|
kind: "createCatalog",
|
|
227
227
|
classification: "additive",
|
|
228
|
-
description: "Create the ontology's file revision, upload and
|
|
228
|
+
description: "Create the ontology's file revision, upload and chat attachment tables.",
|
|
229
229
|
sql: [...BLOB_TABLES_DDL, ...ATTACHMENT_TABLES_DDL],
|
|
230
230
|
}));
|
|
231
231
|
}
|
|
232
|
-
else if (
|
|
233
|
-
|
|
232
|
+
else if (
|
|
233
|
+
// @legacy ontology.agent-attachments-view: a table under the earlier name is renamed even
|
|
234
|
+
// where the ontology no longer stores files, so that no row stays behind under that name.
|
|
235
|
+
(storesFiles || input.existingTables?.has(LEGACY_ATTACHMENTS_TABLE)) &&
|
|
236
|
+
input.existingTables?.has(ATTACHMENTS_TABLE) === false) {
|
|
237
|
+
// File storage an earlier SDK created lacks the chats' attachments, their newer sources or
|
|
238
|
+
// their current name.
|
|
234
239
|
draft.runtimeTables.push(step({
|
|
235
240
|
kind: "createCatalog",
|
|
236
241
|
classification: "additive",
|
|
237
|
-
description: "Create the
|
|
238
|
-
|
|
242
|
+
description: "Create the chats' temporary attachment table beside the file tables, rename it from " +
|
|
243
|
+
`${LEGACY_ATTACHMENTS_TABLE}, or widen the sources it accepts.`,
|
|
239
244
|
sql: ATTACHMENT_TABLES_DDL,
|
|
240
245
|
}));
|
|
241
246
|
}
|
package/dist/migrate/preview.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import { AGENT_TASKS_TABLE, ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
2
|
+
import { AGENT_TASKS_TABLE, ATTACHMENTS_TABLE, LEGACY_ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
|
|
3
3
|
import { CONFIRMATION_TABLE } from "../runtime/confirmations.js";
|
|
4
4
|
import { CHANNEL_ORIGIN_SQL, USER_IDENTITY_TABLE } from "../runtime/user-identity-sql.js";
|
|
5
5
|
import { AGENT_COUNTERS_TABLE, AGENT_GUEST_LINKS_TABLE, AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, API_KEYS_TABLE, CHAT_MESSAGES_TABLE, IDENTITY_NOTICES_TABLE, OAUTH_TABLES, quoteIdentifier, } from "./storage.js";
|
|
@@ -39,6 +39,9 @@ export function preparePreviewDatabase({ sql, }) {
|
|
|
39
39
|
SIGN_IN_EMAILS_TABLE,
|
|
40
40
|
WORKFLOW_STARTS_TABLE,
|
|
41
41
|
ATTACHMENTS_TABLE,
|
|
42
|
+
// @legacy ontology.agent-attachments-view: a copy of a database not yet migrated holds
|
|
43
|
+
// them under the earlier name; once renamed, that name is a view and not listed here.
|
|
44
|
+
LEGACY_ATTACHMENTS_TABLE,
|
|
42
45
|
"_blob_action_results",
|
|
43
46
|
"_blob_uploads",
|
|
44
47
|
"_ontology_replication_delivery",
|
|
@@ -27,7 +27,7 @@ export declare const CHAT_CLAIM_MINUTES = 10;
|
|
|
27
27
|
/** The most rows one purge batch deletes or scrubs, and the most batches one purge runs. */
|
|
28
28
|
export declare const CHAT_PURGE_BATCH = 1000;
|
|
29
29
|
export declare const CHAT_PURGE_BATCHES = 20;
|
|
30
|
-
/** A stored file of a message, by its `
|
|
30
|
+
/** A stored file of a message, by its `_chat_attachments` row. */
|
|
31
31
|
export interface ChatMessageFile {
|
|
32
32
|
readonly id: string;
|
|
33
33
|
readonly name?: string;
|
|
@@ -173,7 +173,7 @@ export type ChatMessageChange =
|
|
|
173
173
|
* What a channel read of the message after recording it, with its routing state, in one write
|
|
174
174
|
* that applies only while the message holds the `meta` it replaces (`null` for none): its `text`
|
|
175
175
|
* with what was transcribed of it, such as a voice message's transcript after its caption, and
|
|
176
|
-
* its stored files, by their `
|
|
176
|
+
* its stored files, by their `_chat_attachments` rows. Never an edit: `edited_at` stays.
|
|
177
177
|
*/
|
|
178
178
|
| (Target & {
|
|
179
179
|
readonly change: "content";
|
|
@@ -283,7 +283,7 @@ export function sqlChatMessageStore(sql) {
|
|
|
283
283
|
const [storage] = yield* rows(`SELECT to_regclass('"${ATTACHMENTS_TABLE}"') IS NOT NULL AS "present"`);
|
|
284
284
|
if (storage?.present === true || storage?.present === "t") {
|
|
285
285
|
yield* rows(`UPDATE "${ATTACHMENTS_TABLE}" SET "state" = 'discarded', "expires_at" = now() ` +
|
|
286
|
-
`WHERE "id"::text = ANY($1::text[]) AND "
|
|
286
|
+
`WHERE "id"::text = ANY($1::text[]) AND "chat" = $2 ` +
|
|
287
287
|
`AND "state" IN ('received', 'ready')`, [ids, `${channel}:${chatId}`]);
|
|
288
288
|
}
|
|
289
289
|
});
|
|
@@ -36,8 +36,7 @@ export const ATTACHMENT_TTL_HOURS = 24;
|
|
|
36
36
|
export const MAX_LIVE_ATTACHMENTS = 50;
|
|
37
37
|
export { ATTACHMENT_SOURCES };
|
|
38
38
|
const SHA256 = /^[0-9a-f]{64}$/;
|
|
39
|
-
|
|
40
|
-
const SELECT = `SELECT a.id::text, a.blob_id::text, b.key, a.owner_id::text, a.conversation AS chat,
|
|
39
|
+
const SELECT = `SELECT a.id::text, a.blob_id::text, b.key, a.owner_id::text, a.chat,
|
|
41
40
|
a.session_id, a.filename, a.content_type, a.size::text, a.sha256, a.state, a.provider_upload_id,
|
|
42
41
|
a.promotion,
|
|
43
42
|
to_char(a.created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') AS created_at,
|
|
@@ -204,7 +203,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
204
203
|
if (access === "denied") {
|
|
205
204
|
return yield* missing(id);
|
|
206
205
|
}
|
|
207
|
-
const rows = yield* sql.unsafe(`${SELECT} WHERE a.id = $1::uuid AND (a.owner_id = $2::uuid OR $5) AND a.
|
|
206
|
+
const rows = yield* sql.unsafe(`${SELECT} WHERE a.id = $1::uuid AND (a.owner_id = $2::uuid OR $5) AND a.chat = $3
|
|
208
207
|
AND a.branch_id = $4 FOR UPDATE OF a, b`, [
|
|
209
208
|
id,
|
|
210
209
|
caller.principal.id,
|
|
@@ -262,7 +261,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
262
261
|
yield* authorizeBlobOperation(ontology, sql, caller.principal.id, [], caller.principal.execution);
|
|
263
262
|
yield* sql.unsafe(`SELECT pg_advisory_xact_lock(818724)`);
|
|
264
263
|
const [existing] = yield* sql.unsafe(`SELECT id::text FROM "${ATTACHMENTS_TABLE}"
|
|
265
|
-
WHERE owner_id = $1::uuid AND
|
|
264
|
+
WHERE owner_id = $1::uuid AND chat = $2 AND source_ref = $3`, [caller.principal.id, caller.chat, input.sourceRef]);
|
|
266
265
|
if (existing) {
|
|
267
266
|
const row = yield* load(existing.id, caller, "source");
|
|
268
267
|
if (row.sha256 !== input.sha256 || Number(row.size) !== input.size) {
|
|
@@ -304,7 +303,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
304
303
|
input.size,
|
|
305
304
|
]);
|
|
306
305
|
yield* sql.unsafe(`INSERT INTO "${ATTACHMENTS_TABLE}"
|
|
307
|
-
(id, blob_id, branch_id, owner_id,
|
|
306
|
+
(id, blob_id, branch_id, owner_id, chat, session_id, source, source_ref,
|
|
308
307
|
filename, content_type, size, sha256, state, expires_at)
|
|
309
308
|
VALUES ($1::uuid, $2::uuid, $3, $4::uuid, $5, $6, $7, $8, $9, $10, $11::bigint, $12,
|
|
310
309
|
'received', ${expiry(caller.chat, "now()")})`, [
|
|
@@ -394,7 +393,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
394
393
|
if (access === "denied") {
|
|
395
394
|
return [];
|
|
396
395
|
}
|
|
397
|
-
const rows = yield* sql.unsafe(`${SELECT} WHERE (a.owner_id = $1::uuid OR $5) AND a.
|
|
396
|
+
const rows = yield* sql.unsafe(`${SELECT} WHERE (a.owner_id = $1::uuid OR $5) AND a.chat = $2 AND a.branch_id = $3
|
|
398
397
|
AND a.state IN ('ready', 'promoted') AND a.expires_at > now()
|
|
399
398
|
AND ($5 OR a.session_id IS NULL OR a.session_id = $4)
|
|
400
399
|
ORDER BY a.created_at DESC LIMIT ${MAX_LIVE_ATTACHMENTS}`, [
|
|
@@ -499,7 +498,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
499
498
|
const [earlier] = yield* sql.unsafe(`SELECT id::text, promotion FROM "${ATTACHMENTS_TABLE}"
|
|
500
499
|
WHERE sha256 = $1 AND branch_id = $2 AND state = 'promoted' AND id <> $3::uuid
|
|
501
500
|
AND promotion->>'action' = $4 AND promotion->>'deduplicated_from' IS NULL
|
|
502
|
-
AND
|
|
501
|
+
AND chat = $5 AND (owner_id = $6::uuid OR $7)
|
|
503
502
|
AND ($7 OR $9 OR session_id IS NULL OR session_id = $8)
|
|
504
503
|
ORDER BY created_at LIMIT 1`, [
|
|
505
504
|
row.sha256,
|
|
@@ -640,7 +639,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
640
639
|
// The attachment an earlier import made of the upload, which only the same message's file
|
|
641
640
|
// replays: a consumed upload is no other message's file.
|
|
642
641
|
const adopted = (blobId) => Effect.gen(function* () {
|
|
643
|
-
const [row] = yield* sql.unsafe(`${SELECT.replace("SELECT ", "SELECT (a.owner_id = $3::uuid AND a.
|
|
642
|
+
const [row] = yield* sql.unsafe(`${SELECT.replace("SELECT ", "SELECT (a.owner_id = $3::uuid AND a.chat = $4 AND a.source_ref = $5) AS same, ")} WHERE a.blob_id = $1::uuid AND a.branch_id = $2`, [blobId, binding.branchId, input.ownerId, input.chat, input.sourceRef]);
|
|
644
643
|
if (row !== undefined && row.same !== true) {
|
|
645
644
|
return yield* new Conflict({
|
|
646
645
|
entity: "upload",
|
|
@@ -688,13 +687,13 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
688
687
|
: { attachment: view(held), adopted: true };
|
|
689
688
|
}
|
|
690
689
|
const [inserted] = yield* sql.unsafe(`INSERT INTO "${ATTACHMENTS_TABLE}"
|
|
691
|
-
(id, blob_id, branch_id, owner_id,
|
|
690
|
+
(id, blob_id, branch_id, owner_id, chat, session_id, source, source_ref,
|
|
692
691
|
filename, content_type, size, sha256, state, created_at, expires_at)
|
|
693
692
|
SELECT $1::uuid, u.blob_id, u.branch_id, $3::uuid, $4, NULL, $5, $6, u.filename,
|
|
694
693
|
u.content_type, u.size, $7, 'ready', $8::timestamptz,
|
|
695
694
|
${expiry(input.chat, "$8::timestamptz")}
|
|
696
695
|
FROM "_blob_uploads" u WHERE u.id = $2::uuid
|
|
697
|
-
ON CONFLICT ("owner_id", "
|
|
696
|
+
ON CONFLICT ("owner_id", "chat", "source_ref") DO NOTHING
|
|
698
697
|
RETURNING id::text`, [
|
|
699
698
|
randomUUID(),
|
|
700
699
|
upload.id,
|
|
@@ -706,7 +705,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
706
705
|
input.at,
|
|
707
706
|
]);
|
|
708
707
|
if (inserted === undefined) {
|
|
709
|
-
const [held] = yield* sql.unsafe(`${SELECT} WHERE a.owner_id = $1::uuid AND a.
|
|
708
|
+
const [held] = yield* sql.unsafe(`${SELECT} WHERE a.owner_id = $1::uuid AND a.chat = $2
|
|
710
709
|
AND a.source_ref = $3 AND a.branch_id = $4`, [input.ownerId, input.chat, input.sourceRef, binding.branchId]);
|
|
711
710
|
return { attachment: view(held), adopted: false };
|
|
712
711
|
}
|
|
@@ -723,7 +722,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
723
722
|
if (!/^[0-9a-f-]{36}$/i.test(id)) {
|
|
724
723
|
return yield* missing(id);
|
|
725
724
|
}
|
|
726
|
-
const [row] = yield* sql.unsafe(`SELECT
|
|
725
|
+
const [row] = yield* sql.unsafe(`SELECT chat FROM "${ATTACHMENTS_TABLE}" WHERE id = $1::uuid AND branch_id = $2`, [id, binding.branchId]);
|
|
727
726
|
if (!row) {
|
|
728
727
|
return yield* missing(id);
|
|
729
728
|
}
|
|
@@ -771,7 +770,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
771
770
|
}
|
|
772
771
|
reads = scope === "reader";
|
|
773
772
|
if (scope === "private") {
|
|
774
|
-
const [sent] = yield* sql.unsafe(`SELECT 1 FROM "${ATTACHMENTS_TABLE}" WHERE branch_id = $1 AND
|
|
773
|
+
const [sent] = yield* sql.unsafe(`SELECT 1 FROM "${ATTACHMENTS_TABLE}" WHERE branch_id = $1 AND chat = $2
|
|
775
774
|
AND owner_id = $3::uuid LIMIT 1`, [binding.branchId, filters.chat, principal.id]);
|
|
776
775
|
if (sent === undefined) {
|
|
777
776
|
return yield* missing("chat");
|
|
@@ -782,13 +781,13 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
782
781
|
const rows = yield* sql.unsafe(`${SELECT} WHERE a.branch_id = $1
|
|
783
782
|
AND ($2 OR EXISTS (
|
|
784
783
|
SELECT 1 FROM "agent_chat" c JOIN "agent_chat_members" m ON m.agent_chat_id = c.id
|
|
785
|
-
WHERE c.provider || ':' || c.external_id = a.
|
|
784
|
+
WHERE c.provider || ':' || c.external_id = a.chat AND c.kind::text = 'group'
|
|
786
785
|
AND m.user_id = $3::uuid
|
|
787
786
|
) OR (a.owner_id = $3::uuid AND NOT EXISTS (
|
|
788
787
|
SELECT 1 FROM "agent_chat" c
|
|
789
|
-
WHERE c.provider || ':' || c.external_id = a.
|
|
788
|
+
WHERE c.provider || ':' || c.external_id = a.chat AND c.kind::text = 'group'
|
|
790
789
|
)))
|
|
791
|
-
AND ($4::text IS NULL OR a.
|
|
790
|
+
AND ($4::text IS NULL OR a.chat = $4)
|
|
792
791
|
AND ($5::uuid IS NULL OR a.owner_id = $5::uuid)
|
|
793
792
|
AND ($6::timestamptz IS NULL OR a.created_at >= $6::timestamptz)
|
|
794
793
|
AND ($7::timestamptz IS NULL OR a.created_at <= $7::timestamptz)
|
package/dist/storage/tables.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
/** Storage metadata lives in the ontology database and is migrated by its SDK. */
|
|
2
2
|
export declare const BLOB_TABLES_DDL: ReadonlyArray<string>;
|
|
3
|
-
/** The
|
|
4
|
-
export declare const ATTACHMENTS_TABLE = "
|
|
3
|
+
/** The chats' temporary attachments, beside the blob tables whose storage they share. */
|
|
4
|
+
export declare const ATTACHMENTS_TABLE = "_chat_attachments";
|
|
5
|
+
/**
|
|
6
|
+
* @legacy ontology.agent-attachments-view
|
|
7
|
+
* The table's earlier name. A database migrated by this SDK keeps it as a view of
|
|
8
|
+
* {@link ATTACHMENTS_TABLE} with the chat column named `conversation`, so that a workspace still
|
|
9
|
+
* running an earlier ontology SDK reads and writes the same rows.
|
|
10
|
+
*/
|
|
11
|
+
export declare const LEGACY_ATTACHMENTS_TABLE = "_agent_attachments";
|
|
5
12
|
/**
|
|
6
13
|
* Where an attachment comes from: a chat message on a channel, `channel` for a channel the
|
|
7
14
|
* workspace defines in its own code, or a file the agent produced in its sandbox and stores so
|
|
@@ -9,17 +16,17 @@ export declare const ATTACHMENTS_TABLE = "_agent_attachments";
|
|
|
9
16
|
*/
|
|
10
17
|
export declare const ATTACHMENT_SOURCES: readonly ["telegram", "slack", "whatsapp", "email", "sandbox", "channel"];
|
|
11
18
|
/** The check that limits `source` to {@link ATTACHMENT_SOURCES}; its name is PostgreSQL's default. */
|
|
12
|
-
export declare const ATTACHMENT_SOURCE_CHECK = "
|
|
19
|
+
export declare const ATTACHMENT_SOURCE_CHECK = "_chat_attachments_source_check";
|
|
13
20
|
/**
|
|
14
|
-
* A file a
|
|
15
|
-
* a `_blobs` revision in the same storage as uploads; the row binds
|
|
16
|
-
* and, for private chats, the first agent
|
|
17
|
-
*
|
|
18
|
-
* channel that keeps its chats' files,
|
|
21
|
+
* A file that arrived in a chat or that the agent made in its sandbox: platform metadata, never an
|
|
22
|
+
* ontology record. The bytes are a `_blobs` revision in the same storage as uploads; the row binds
|
|
23
|
+
* them to their owner, the chat (`provider:external_id`) and, for private chats, the first agent
|
|
24
|
+
* session that reads them. Group access follows the chat registry's current members. Attachments
|
|
25
|
+
* expire 24 hours after receipt unless promoted, or, on a channel that keeps its chats' files,
|
|
26
|
+
* with their messages.
|
|
19
27
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* still read and write it under that name.
|
|
28
|
+
* The first statement renames the table an earlier SDK created; the last keeps its earlier name
|
|
29
|
+
* as a view for SDKs that share the database.
|
|
23
30
|
*/
|
|
24
31
|
export declare const ATTACHMENT_TABLES_DDL: ReadonlyArray<string>;
|
|
25
32
|
/** The pending and started workflow starts of action triggers, beside the blob action results. */
|
package/dist/storage/tables.js
CHANGED
|
@@ -53,8 +53,15 @@ export const BLOB_TABLES_DDL = [
|
|
|
53
53
|
PRIMARY KEY ("principal_id", "key")
|
|
54
54
|
)`,
|
|
55
55
|
];
|
|
56
|
-
/** The
|
|
57
|
-
export const ATTACHMENTS_TABLE = "
|
|
56
|
+
/** The chats' temporary attachments, beside the blob tables whose storage they share. */
|
|
57
|
+
export const ATTACHMENTS_TABLE = "_chat_attachments";
|
|
58
|
+
/**
|
|
59
|
+
* @legacy ontology.agent-attachments-view
|
|
60
|
+
* The table's earlier name. A database migrated by this SDK keeps it as a view of
|
|
61
|
+
* {@link ATTACHMENTS_TABLE} with the chat column named `conversation`, so that a workspace still
|
|
62
|
+
* running an earlier ontology SDK reads and writes the same rows.
|
|
63
|
+
*/
|
|
64
|
+
export const LEGACY_ATTACHMENTS_TABLE = "_agent_attachments";
|
|
58
65
|
/**
|
|
59
66
|
* Where an attachment comes from: a chat message on a channel, `channel` for a channel the
|
|
60
67
|
* workspace defines in its own code, or a file the agent produced in its sandbox and stores so
|
|
@@ -71,24 +78,86 @@ export const ATTACHMENT_SOURCES = [
|
|
|
71
78
|
/** The check that limits `source` to {@link ATTACHMENT_SOURCES}; its name is PostgreSQL's default. */
|
|
72
79
|
export const ATTACHMENT_SOURCE_CHECK = `${ATTACHMENTS_TABLE}_source_check`;
|
|
73
80
|
const SOURCE_CHECK = `CHECK ("source" IN (${ATTACHMENT_SOURCES.map((source) => `'${source}'`).join(", ")}))`;
|
|
81
|
+
/** The columns every SDK since the table's first release reads and writes, in table order. */
|
|
82
|
+
const LEGACY_ATTACHMENT_COLUMNS = [
|
|
83
|
+
"id",
|
|
84
|
+
"blob_id",
|
|
85
|
+
"branch_id",
|
|
86
|
+
"owner_id",
|
|
87
|
+
"chat",
|
|
88
|
+
"session_id",
|
|
89
|
+
"source",
|
|
90
|
+
"source_ref",
|
|
91
|
+
"filename",
|
|
92
|
+
"content_type",
|
|
93
|
+
"size",
|
|
94
|
+
"sha256",
|
|
95
|
+
"state",
|
|
96
|
+
"provider_upload_id",
|
|
97
|
+
"promotion",
|
|
98
|
+
"created_at",
|
|
99
|
+
"expires_at",
|
|
100
|
+
"removed_at",
|
|
101
|
+
];
|
|
102
|
+
/**
|
|
103
|
+
* @legacy ontology.agent-attachments-view
|
|
104
|
+
* Renames a table an earlier SDK created as `_agent_attachments`, its `conversation` column and
|
|
105
|
+
* the constraints and indexes PostgreSQL named after both, in place: rows, ids and blob
|
|
106
|
+
* references stay as they are. Does nothing once the table has its name.
|
|
107
|
+
*/
|
|
108
|
+
const RENAME_LEGACY_ATTACHMENTS = `DO $rename$
|
|
109
|
+
DECLARE
|
|
110
|
+
item record;
|
|
111
|
+
BEGIN
|
|
112
|
+
IF to_regclass('"${ATTACHMENTS_TABLE}"') IS NOT NULL OR NOT EXISTS (
|
|
113
|
+
SELECT 1 FROM pg_catalog.pg_class t
|
|
114
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace
|
|
115
|
+
WHERE n.nspname = current_schema() AND t.relname = '${LEGACY_ATTACHMENTS_TABLE}'
|
|
116
|
+
AND t.relkind = 'r'
|
|
117
|
+
) THEN
|
|
118
|
+
RETURN;
|
|
119
|
+
END IF;
|
|
120
|
+
ALTER TABLE "${LEGACY_ATTACHMENTS_TABLE}" RENAME TO "${ATTACHMENTS_TABLE}";
|
|
121
|
+
ALTER TABLE "${ATTACHMENTS_TABLE}" RENAME COLUMN "conversation" TO "chat";
|
|
122
|
+
FOR item IN
|
|
123
|
+
SELECT c.conname AS name FROM pg_catalog.pg_constraint c
|
|
124
|
+
WHERE c.conrelid = '"${ATTACHMENTS_TABLE}"'::regclass
|
|
125
|
+
AND starts_with(c.conname, '${LEGACY_ATTACHMENTS_TABLE}_')
|
|
126
|
+
LOOP
|
|
127
|
+
EXECUTE format('ALTER TABLE %I RENAME CONSTRAINT %I TO %I', '${ATTACHMENTS_TABLE}', item.name,
|
|
128
|
+
replace(replace(item.name, '${LEGACY_ATTACHMENTS_TABLE}_', '${ATTACHMENTS_TABLE}_'),
|
|
129
|
+
'_conversation_', '_chat_'));
|
|
130
|
+
END LOOP;
|
|
131
|
+
FOR item IN
|
|
132
|
+
SELECT i.relname AS name FROM pg_catalog.pg_index x
|
|
133
|
+
JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
|
|
134
|
+
WHERE x.indrelid = '"${ATTACHMENTS_TABLE}"'::regclass
|
|
135
|
+
AND starts_with(i.relname, '${LEGACY_ATTACHMENTS_TABLE}_')
|
|
136
|
+
LOOP
|
|
137
|
+
EXECUTE format('ALTER INDEX %I RENAME TO %I', item.name,
|
|
138
|
+
replace(item.name, '${LEGACY_ATTACHMENTS_TABLE}_', '${ATTACHMENTS_TABLE}_'));
|
|
139
|
+
END LOOP;
|
|
140
|
+
END
|
|
141
|
+
$rename$`;
|
|
74
142
|
/**
|
|
75
|
-
* A file a
|
|
76
|
-
* a `_blobs` revision in the same storage as uploads; the row binds
|
|
77
|
-
* and, for private chats, the first agent
|
|
78
|
-
*
|
|
79
|
-
* channel that keeps its chats' files,
|
|
143
|
+
* A file that arrived in a chat or that the agent made in its sandbox: platform metadata, never an
|
|
144
|
+
* ontology record. The bytes are a `_blobs` revision in the same storage as uploads; the row binds
|
|
145
|
+
* them to their owner, the chat (`provider:external_id`) and, for private chats, the first agent
|
|
146
|
+
* session that reads them. Group access follows the chat registry's current members. Attachments
|
|
147
|
+
* expire 24 hours after receipt unless promoted, or, on a channel that keeps its chats' files,
|
|
148
|
+
* with their messages.
|
|
80
149
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* still read and write it under that name.
|
|
150
|
+
* The first statement renames the table an earlier SDK created; the last keeps its earlier name
|
|
151
|
+
* as a view for SDKs that share the database.
|
|
84
152
|
*/
|
|
85
153
|
export const ATTACHMENT_TABLES_DDL = [
|
|
154
|
+
RENAME_LEGACY_ATTACHMENTS,
|
|
86
155
|
`CREATE TABLE IF NOT EXISTS "${ATTACHMENTS_TABLE}" (
|
|
87
156
|
"id" uuid PRIMARY KEY,
|
|
88
157
|
"blob_id" uuid NOT NULL UNIQUE REFERENCES "_blobs" ("id"),
|
|
89
158
|
"branch_id" text NOT NULL,
|
|
90
159
|
"owner_id" uuid NOT NULL REFERENCES "user" ("id"),
|
|
91
|
-
"
|
|
160
|
+
"chat" text NOT NULL,
|
|
92
161
|
"session_id" text,
|
|
93
162
|
"source" text NOT NULL CONSTRAINT "${ATTACHMENT_SOURCE_CHECK}" ${SOURCE_CHECK},
|
|
94
163
|
"source_ref" text NOT NULL,
|
|
@@ -102,7 +171,7 @@ export const ATTACHMENT_TABLES_DDL = [
|
|
|
102
171
|
"created_at" timestamptz NOT NULL DEFAULT now(),
|
|
103
172
|
"expires_at" timestamptz NOT NULL DEFAULT now() + interval '24 hours',
|
|
104
173
|
"removed_at" timestamptz,
|
|
105
|
-
UNIQUE ("owner_id", "
|
|
174
|
+
UNIQUE ("owner_id", "chat", "source_ref")
|
|
106
175
|
)`,
|
|
107
176
|
`CREATE INDEX IF NOT EXISTS "${ATTACHMENTS_TABLE}_expiry_idx" ON "${ATTACHMENTS_TABLE}" ("state", "expires_at")`,
|
|
108
177
|
`CREATE INDEX IF NOT EXISTS "${ATTACHMENTS_TABLE}_sha256_idx" ON "${ATTACHMENTS_TABLE}" ("sha256")`,
|
|
@@ -110,6 +179,12 @@ export const ATTACHMENT_TABLES_DDL = [
|
|
|
110
179
|
// A table an earlier SDK created accepts fewer sources; widen its check in place.
|
|
111
180
|
`ALTER TABLE "${ATTACHMENTS_TABLE}" DROP CONSTRAINT IF EXISTS "${ATTACHMENT_SOURCE_CHECK}", ` +
|
|
112
181
|
`ADD CONSTRAINT "${ATTACHMENT_SOURCE_CHECK}" ${SOURCE_CHECK}`,
|
|
182
|
+
// @legacy ontology.agent-attachments-view
|
|
183
|
+
// A simple view, so an earlier SDK's inserts, updates, `ON CONFLICT` and row locks reach the
|
|
184
|
+
// table's rows, constraints and defaults. Columns added later stay out of it.
|
|
185
|
+
`CREATE OR REPLACE VIEW "${LEGACY_ATTACHMENTS_TABLE}" AS SELECT ` +
|
|
186
|
+
LEGACY_ATTACHMENT_COLUMNS.map((column) => column === "chat" ? `"chat" AS "conversation"` : `"${column}"`).join(", ") +
|
|
187
|
+
` FROM "${ATTACHMENTS_TABLE}"`,
|
|
113
188
|
];
|
|
114
189
|
/** The pending and started workflow starts of action triggers, beside the blob action results. */
|
|
115
190
|
export const WORKFLOW_STARTS_TABLE = "_workflow_starts";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.159.0",
|
|
4
4
|
"description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|