@segmentstream/ontology 0.159.0 → 0.160.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
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.160.0
|
|
4
|
+
|
|
5
|
+
The agent's decided drafts are deleted on the chat store's retention schedule. The chat store's
|
|
6
|
+
purge (`GET /api/ontology/agent/chats/purge`, the daily cron job) also deletes `agent_draft`
|
|
7
|
+
records decided and held longer ago than `chat.retentionDays`, and answers how many in `drafts`;
|
|
8
|
+
a draft still waiting is kept. The deletion is recorded in history as `agent_draft.retention` and
|
|
9
|
+
replicates, so the warehouse's views drop the draft, and the draft's history keeps who decided
|
|
10
|
+
what and when with the reply's text, the reasons and notes, and its chat scrubbed.
|
|
11
|
+
|
|
3
12
|
## 0.159.0
|
|
4
13
|
|
|
5
14
|
The chats' temporary attachments table is `_chat_attachments`, and its chat column is `chat`.
|
package/dist/runtime/agents.js
CHANGED
|
@@ -5,6 +5,7 @@ import { SqlClient } from "effect/unstable/sql";
|
|
|
5
5
|
import { action } from "../action.js";
|
|
6
6
|
import { AgentChat, agentChatKey } from "../agent-chat.js";
|
|
7
7
|
import { chatChannelKind } from "../chat-channel.js";
|
|
8
|
+
import { historyTableName, quoteIdentifier, tableName } from "../migrate/storage.js";
|
|
8
9
|
import { addressesOfDraft, AgentDraft, participantsOfDraft, draftAddresses, draftParticipants, } from "../agent-draft.js";
|
|
9
10
|
import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
|
|
10
11
|
import { inboxChannelsOf, inboxRolesOf, takeoverChannelsOf, takeoverProvidersOf, } from "../manifest.js";
|
|
@@ -75,6 +76,32 @@ function kindOf(input) {
|
|
|
75
76
|
? "group"
|
|
76
77
|
: "private";
|
|
77
78
|
}
|
|
79
|
+
/** The most drafts one retention batch deletes, and the most batches one purge runs. */
|
|
80
|
+
const DRAFT_PURGE_BATCH = 200;
|
|
81
|
+
const DRAFT_PURGE_BATCHES = 20;
|
|
82
|
+
/** The action a draft's deletion by retention is recorded under in its history. */
|
|
83
|
+
const RETENTION_ACTION = "retention";
|
|
84
|
+
const DRAFTS = quoteIdentifier(tableName(AgentDraft.name));
|
|
85
|
+
const DRAFT_HISTORY = quoteIdentifier(historyTableName(AgentDraft.name));
|
|
86
|
+
/**
|
|
87
|
+
* What a draft's history says of its reply and its chat, scrubbed once retention deletes it: the
|
|
88
|
+
* text, the reasons and notes members wrote, and whom and where it went.
|
|
89
|
+
*/
|
|
90
|
+
const DRAFT_CONTENT_FIELDS = [
|
|
91
|
+
"draft",
|
|
92
|
+
"text",
|
|
93
|
+
"reason",
|
|
94
|
+
"note",
|
|
95
|
+
"to",
|
|
96
|
+
"cc",
|
|
97
|
+
"recipient",
|
|
98
|
+
"participants",
|
|
99
|
+
"chat_id",
|
|
100
|
+
"thread_id",
|
|
101
|
+
"reply_to",
|
|
102
|
+
"message_id",
|
|
103
|
+
"basis",
|
|
104
|
+
];
|
|
78
105
|
function nullableText(value) {
|
|
79
106
|
return value === null || value === undefined ? null : String(value);
|
|
80
107
|
}
|
|
@@ -1026,6 +1053,45 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
1026
1053
|
return true;
|
|
1027
1054
|
}))
|
|
1028
1055
|
.pipe(Effect.orDie),
|
|
1056
|
+
purgeDrafts: ({ retentionDays }) => Effect.gen(function* () {
|
|
1057
|
+
let deleted = 0;
|
|
1058
|
+
let more = false;
|
|
1059
|
+
for (let batch = 0; batch < DRAFT_PURGE_BATCHES; batch++) {
|
|
1060
|
+
// Each batch commits on its own and skips drafts another writer holds.
|
|
1061
|
+
const removed = yield* sql.withTransaction(Effect.gen(function* () {
|
|
1062
|
+
yield* beginReplicationWrite(sql);
|
|
1063
|
+
const expired = yield* rows(`SELECT "id"::text AS "id" FROM ${DRAFTS} WHERE "status"::text <> 'pending' ` +
|
|
1064
|
+
`AND "created_at" < now() - make_interval(days => $1::int) ` +
|
|
1065
|
+
`ORDER BY "created_at" LIMIT $2::int FOR UPDATE SKIP LOCKED`, [retentionDays, DRAFT_PURGE_BATCH]);
|
|
1066
|
+
const ids = expired.map((row) => String(row.id));
|
|
1067
|
+
if (ids.length === 0) {
|
|
1068
|
+
return 0;
|
|
1069
|
+
}
|
|
1070
|
+
const runtime = makeOntologyRuntime(ontology, sql, {
|
|
1071
|
+
action: `${AgentDraft.name}.${RETENTION_ACTION}`,
|
|
1072
|
+
actionId: randomUUID(),
|
|
1073
|
+
principalId: null,
|
|
1074
|
+
});
|
|
1075
|
+
// A delete is history, which replicates it: the warehouse's view drops the draft.
|
|
1076
|
+
for (const id of ids) {
|
|
1077
|
+
yield* runtime.delete({ entity: AgentDraft.name, id }).pipe(Effect.orDie);
|
|
1078
|
+
}
|
|
1079
|
+
// History keeps who decided what and when, never what the reply said.
|
|
1080
|
+
yield* rows(`UPDATE ${DRAFT_HISTORY} SET "changes" = (SELECT coalesce(jsonb_object_agg("key", ` +
|
|
1081
|
+
`CASE WHEN "key" = ANY($2::text[]) ` +
|
|
1082
|
+
`THEN jsonb_build_object('before', NULL, 'after', NULL) ELSE "value" END), '{}'::jsonb) ` +
|
|
1083
|
+
`FROM jsonb_each("changes")) ` +
|
|
1084
|
+
`WHERE "record_id"::text = ANY($1::text[])`, [ids, DRAFT_CONTENT_FIELDS]);
|
|
1085
|
+
return ids.length;
|
|
1086
|
+
}));
|
|
1087
|
+
deleted += removed;
|
|
1088
|
+
more = removed >= DRAFT_PURGE_BATCH;
|
|
1089
|
+
if (!more) {
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
return { deleted, more };
|
|
1094
|
+
}).pipe(Effect.catchTag("SqlError", Effect.die)),
|
|
1029
1095
|
sessionReviews: (sessionId) => Effect.gen(function* () {
|
|
1030
1096
|
const runtime = makeOntologyRuntime(ontology, sql);
|
|
1031
1097
|
// Every reply still waiting, however old, and the latest decisions: a decision on an old
|
|
@@ -2,6 +2,7 @@ import { Effect, Redacted, Schema } from "effect";
|
|
|
2
2
|
import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
|
|
3
3
|
import { type ChatMessageStore } from "../../runtime/chat-messages.js";
|
|
4
4
|
import { RequestId } from "../errors.js";
|
|
5
|
+
import type { AgentStore } from "../runtime-contract.js";
|
|
5
6
|
/** `POST /api/ontology/agent/chats/messages`: one message, recorded once by its keys. */
|
|
6
7
|
export declare const ChatMessageRecordRequest: Schema.Struct<{
|
|
7
8
|
readonly channel: Schema.String;
|
|
@@ -141,11 +142,14 @@ export declare function changeChatMessage(store: ChatMessageStore): (request: Ht
|
|
|
141
142
|
export declare function readChatMessages(store: ChatMessageStore): (request: HttpServerRequest.HttpServerRequest) => Answer;
|
|
142
143
|
/**
|
|
143
144
|
* Deletes the messages the workspace keeps no longer, as its cron calls it with the project's cron
|
|
144
|
-
* secret. The ontology declares the window,
|
|
145
|
+
* secret, and the agent's decided drafts on the same schedule. The ontology declares the window,
|
|
146
|
+
* `chat.retentionDays`.
|
|
145
147
|
*/
|
|
146
148
|
export declare function purgeChatMessages(store: ChatMessageStore, options: {
|
|
147
149
|
readonly cronSecret: Redacted.Redacted<string> | undefined;
|
|
148
150
|
readonly retentionDays: number;
|
|
151
|
+
/** Deletes the drafts decided before the window, with their history's contents. */
|
|
152
|
+
readonly purgeDrafts?: AgentStore["purgeDrafts"];
|
|
149
153
|
}): (request: HttpServerRequest.HttpServerRequest) => Answer;
|
|
150
154
|
/** The routes' paths under the agent's path. */
|
|
151
155
|
export declare const CHAT_MESSAGE_PATHS: {
|
|
@@ -254,7 +254,8 @@ export function readChatMessages(store) {
|
|
|
254
254
|
}
|
|
255
255
|
/**
|
|
256
256
|
* Deletes the messages the workspace keeps no longer, as its cron calls it with the project's cron
|
|
257
|
-
* secret. The ontology declares the window,
|
|
257
|
+
* secret, and the agent's decided drafts on the same schedule. The ontology declares the window,
|
|
258
|
+
* `chat.retentionDays`.
|
|
258
259
|
*/
|
|
259
260
|
export function purgeChatMessages(store, options) {
|
|
260
261
|
return (request) => Effect.gen(function* () {
|
|
@@ -264,8 +265,18 @@ export function purgeChatMessages(store, options) {
|
|
|
264
265
|
return refused(requestId, 401, "cron_secret_required");
|
|
265
266
|
}
|
|
266
267
|
const purged = yield* store.purge({ retentionDays: options.retentionDays });
|
|
267
|
-
|
|
268
|
-
|
|
268
|
+
const drafts = options.purgeDrafts === undefined
|
|
269
|
+
? undefined
|
|
270
|
+
: yield* options.purgeDrafts({ retentionDays: options.retentionDays });
|
|
271
|
+
yield* Effect.logInfo("ontology.agent.chat_messages_purged").pipe(Effect.annotateLogs({
|
|
272
|
+
requestId,
|
|
273
|
+
retentionDays: options.retentionDays,
|
|
274
|
+
...purged,
|
|
275
|
+
...(drafts === undefined
|
|
276
|
+
? {}
|
|
277
|
+
: { draftsDeleted: drafts.deleted, draftsMore: drafts.more }),
|
|
278
|
+
}));
|
|
279
|
+
return json(drafts === undefined ? purged : { ...purged, drafts });
|
|
269
280
|
});
|
|
270
281
|
}
|
|
271
282
|
/** The routes' paths under the agent's path. */
|
|
@@ -613,6 +613,7 @@ export function agentLayer(options) {
|
|
|
613
613
|
const purge = purgeChatMessages(messages, {
|
|
614
614
|
cronSecret: options.cronSecret,
|
|
615
615
|
retentionDays: options.chatRetentionDays ?? CHAT_RETENTION_DAYS.default,
|
|
616
|
+
...(store.purgeDrafts === undefined ? {} : { purgeDrafts: store.purgeDrafts }),
|
|
616
617
|
});
|
|
617
618
|
yield* router.add("POST", `${AGENT_PATH}${CHAT_MESSAGE_PATHS.record}`, asRuntime(recordChatMessage(messages)));
|
|
618
619
|
yield* router.add("POST", `${AGENT_PATH}${CHAT_MESSAGE_PATHS.change}`, asRuntime(changeChatMessage(messages)));
|
|
@@ -723,6 +723,18 @@ export interface AgentStore {
|
|
|
723
723
|
readonly review: string;
|
|
724
724
|
readonly revision: string;
|
|
725
725
|
}) => Effect.Effect<boolean>;
|
|
726
|
+
/**
|
|
727
|
+
* Deletes the drafts decided longer ago than the chat store keeps messages, `retentionDays` after
|
|
728
|
+
* they were held, in bounded batches, with their history's contents scrubbed; a draft still
|
|
729
|
+
* waiting is kept. Absent on stores that predate it.
|
|
730
|
+
*/
|
|
731
|
+
readonly purgeDrafts?: (input: {
|
|
732
|
+
readonly retentionDays: number;
|
|
733
|
+
}) => Effect.Effect<{
|
|
734
|
+
readonly deleted: number;
|
|
735
|
+
/** The last batch was full: older drafts may remain for the next run. */
|
|
736
|
+
readonly more: boolean;
|
|
737
|
+
}>;
|
|
726
738
|
/**
|
|
727
739
|
* The replies an agent session held for review, newest first: what became of each, for the
|
|
728
740
|
* session's next turn. Absent on stores that predate it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.160.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",
|